【问题标题】:predict.glmnet() gives same predictions for type = "link" and "response" using family = "binomial"predict.glmnet() 使用 family = "binomial" 对 type = "link" 和 "response" 给出相同的预测
【发布时间】:2016-08-19 03:26:37
【问题描述】:

拿这个案例(逻辑回归的经典螃蟹数据):

> library(glmnet)
> X <- read.table("http://www.da.ugent.be/datasets/crab.dat", header=T)[1:10,]
> Y <- factor(ifelse(X$Sa > 0, 1, 0))
> Xnew <- data.frame('W'=20,'Wt'=2000)
> fit.glmnet <- glmnet(x = data.matrix(X[,c('W','Wt')]), y = Y, family = "binomial")

现在我想预测来自Xnew 的新值:

根据docs我可以使用predict.glmnet

输入

所需的预测类型。类型“链接”给出线性预测器 对于“二项式”、“多项式”、“泊松”或“考克斯”模型;为了 “高斯”模型它给出了拟合值。类型“响应”给出 “二项式”或“多项式”的拟合概率,[...]

所以这就是我的工作:

> predict.glmnet(object = fit.glmnet, type="response", newx = as.matrix(Xnew))[,1:5]
        s0         s1         s2         s3         s4 
-0.8472979 -0.9269763 -1.0057390 -1.0836919 -1.1609386 
> predict.glmnet(object = fit.glmnet, type="link", newx = as.matrix(Xnew))[,1:5]
        s0         s1         s2         s3         s4 
-0.8472979 -0.9269763 -1.0057390 -1.0836919 -1.1609386 

linkresponse 预测的值相同,这不是我所期望的。使用 predict 似乎给了我正确的值:

> predict(object = fit.glmnet, type="response", newx = as.matrix(Xnew))[,1:5]
       s0        s1        s2        s3        s4 
0.3000000 0.2835386 0.2678146 0.2528080 0.2384968 
> predict(object = fit.glmnet, type="link", newx = as.matrix(Xnew))[,1:5] 
        s0         s1         s2         s3         s4 
-0.8472979 -0.9269763 -1.0057390 -1.0836919 -1.1609386 

这是一个错误,还是我以错误的方式使用predict.glmnet

【问题讨论】:

  • 检查拟合模型的类别,然后阅读predict.lognet的代码。

标签: r regression glmnet


【解决方案1】:

在数据包glmnet 中,您的对象属于lognet 类:

> class(object)
[1] "lognet" "glmnet"

这就是为什么使用predict.glmnet 没有得到正确结果的原因,它在内部不支持type="response",但是如果你使用predict.lognet,你会得到它:

> predict.lognet(object = fit.glmnet, newx = as.matrix(Xnew), type="response")[,1:5]
       s0        s1        s2        s3        s4 
0.3000000 0.2835386 0.2678146 0.2528080 0.2384968 
> predict.lognet(object = fit.glmnet, newx = as.matrix(Xnew), type="link")[,1:5]
        s0         s1         s2         s3         s4 
-0.8472979 -0.9269763 -1.0057390 -1.0836919 -1.1609386 

无论如何,我建议你使用predict,并让 R 在内部解析要使用的函数。

希望对你有帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-11-28
    • 2016-11-14
    • 2020-10-22
    • 2019-02-21
    • 1970-01-01
    • 1970-01-01
    • 2017-01-04
    • 2018-09-02
    相关资源
    最近更新 更多