【发布时间】: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
link 与 response 预测的值相同,这不是我所期望的。使用 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