1
拿这个情况下(logistic回归经典蟹数据)相同的预测:predict.glmnet()给出了适用于type =“链接”和“响应”使用家庭=“二项式”
> 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
:
型
需要的预测类型。类型“链接”给出了“二项式”,“多项式”,“泊松”或“cox”模型的线性预测变量 ;对于 “高斯”模型,它给出了拟合值。键入“响应”给出 为“二项”或“多项”拟合概率,[...]
所以这是我做的:
> 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'的代码。 – Scortchi