我可以用glmnet制作许多不同的型号。然后,我将模型保存在一个列表中,以便在以后的使用中使用这个模型列表。
library(glmnet)
x1=matrix(rnorm(100*20),100,20)
y1=matrix(rnorm(100*3),100,3)
fit1m=glmnet(x1,y1,family="mgaussian")
x2=matrix(rnorm(100*20),100,20)
y2=matrix(rnorm(100*3),100,3)
fit2m=glmnet(x2,y2,family="mgaussian")
x3=matrix(rnorm(100*20),100,20)
y3=matrix(rnorm(100*3),100,3)
fit3m=glmnet(x3,y3,family="mgaussian")
listmodels <-list(fit1m,fit2m,fit3m)
listmodels然而,当我试图从这个列表中检索一个模型时,我得到了一个类错误。
fit1 <- listmodels[1]
fit1
xnew=matrix(rnorm(100*20),100,20)
pred1 <- as.data.frame(predict(fit1,newx=xnew),s="lambda.min")
pred1为了使列表中的模型正确工作,我需要做些什么?谢谢你的帮助。
发布于 2016-09-05 10:48:16
如果我们正确提取list元素,它将工作,即'listmodels1‘仍然是一个list,我们需要使用'listmodels[1]’来提取元素
fit1 <- listmodels[[1]]
xnew=matrix(rnorm(100*20),100,20)
pred1 <- as.data.frame(predict(fit1,newx=xnew),s="lambda.min")
pred1如果我们想对所有的list元素这样做,我们可以在list (lapply)上循环并执行相同的过程
lapply(listmodels, function(x) as.data.frame(predict(x,
newx = matrix(rnorm(100*20), 100, 20)), s = "lambda.min"))https://stackoverflow.com/questions/39328917
复制相似问题