【问题标题】:Matthews Correlation Coefficient in Lasso model, logistic regression with RLasso 模型中的 Matthews 相关系数,使用 R 进行逻辑回归
【发布时间】:2021-02-14 03:55:17
【问题描述】:

我正在使用库 (MASS) 中的数据 (birthwt),我想在 Lasso 模型中使用 R 计算 Mathews 相关系数 (MCC)。我真的很困惑。提前考虑

birthwt=birthwt[,-10]
boot=sample(nrow(birthwt), 40)
train.data=birthwt[-boot, ]
test.data=birthwt[boot, ]
x =model.matrix(low~., train.data)[,-1]
y =train.data$low

套索模型:

library(glmnet)
set.seed(123)
cv.lasso = cv.glmnet(x, y, alpha = 1, family = "binomial")
model.lasso=glmnet(x,y,alpha=1,family="binomial",lambda= cv.lasso$lambda.min)
coef(model.lasso)
 x.test = model.matrix(low ~., test.data)[,-1]
proba.lasso = predict(model.lasso,newx = x.test)
class.lasso = ifelse(proba.lasso > 0.5, 1, 0)
class.obs = test.data$low

【问题讨论】:

    标签: r mcc


    【解决方案1】:

    如果您对 glmnet 对象进行预测,则默认响应是 logit 链接,但在您的情况下,您需要这样做:

    class.lasso = predict(model.lasso,newx = x.test,type="class")
    class.lasso = as.numeric(class.lasso)
    class.obs = test.data$low
    

    你也可以这样使用概率:

    class.lasso = ifelse(predict(model.lasso,newx = x.test,type="response") > 0.5,1,0)
    

    要计算mcc,你可以这样做:

    library(mltools)
    mcc(pred = class.lasso, actual = class.obs)
    [1] 0.2581989
    

    或者使用计算皮尔逊 phi 的东西:

    library(psych)
    phi(table(class.lasso,class.obs),digits=7)
    
    [1] 0.2581989
    

    或者,如果您使用 the formula from wiki 从头开始​​派生它:

    cm = table(class.lasso,class.obs)
    TP = cm[2,2]
    FP = cm[2,1]
    TN = cm[1,1]
    FN = cm[1,2]
    
    (TP * TN - FP*FN)/sqrt((TP+FP)*(TP+FN)*(TN+FP)*(TN+FN))
    [1] 0.2581989
    

    【讨论】:

    • 非常清晰快速。谢谢你。有什么方法可以计算模型的准确度、灵敏度和敏感性?
    • 您可以使用包插入符号中的confusionMatrix。做confusionMatrix(table(class.lasso,class.obs))
    • 它不起作用:confusionMatrix_(actual, predicted, cutoff) 中的错误:缺少参数“predicted”,没有默认值否则,对于第一个代码:class.lasso = predict(model. lasso,newx = x.test,type="class"),是 porba.lasso(我写的)还是 class.lasso?
    • 您缺少 table() 。这是类套索。嘿,我不得不说,如果您还有其他问题,请发布另一个问题。如果你重复我上面的代码,你肯定可以得到 MCC。你能试试吗,不要绕道
    猜你喜欢
    • 2020-12-22
    • 2020-01-05
    • 2021-02-07
    • 2021-04-28
    • 1970-01-01
    • 2017-02-15
    • 1970-01-01
    • 2018-01-26
    • 2016-12-11
    相关资源
    最近更新 更多