R中的错误:没有适用于'预测'应用于类的对象" NULL"

时间:2015-08-22 02:56:21

标签: r simulation

我试图估计一些系数的准确度。这是我的代码:

    set.seed(12)
n <- 200
coefs <- rep(c(2, -1, 0), each=30)
epsilon <- rnorm(n, 0, 10)
x <- matrix(nrow=n, ncol=90)
for(i in 1:90){
  x[, i] <- rnorm(200, 0, 1)
}
y <- rep(NA, n)
for(i in 1:n){
  y[i] = sum(x[i,]*coefs) + epsilon[i]
}
data <- data.frame(x=x,y=y)

dim(data)
head(data)
numericvars <- c(1:90)
insample <- data[1:100,]
outsample <- data[101,200]
x <- as.matrix(insample[,numericvars])
y <- insample$y
insamplex <- insample[x>0]
outsamplex <- outsample[x,]
insampley <- insample[y>0,]
outsampley <- outsample[y,]
newdata <- data.frame(x=x,y=y)
newdata1 <- na.omit(newdata)

lambdalevels <- 10^seq(7,-2,length=100)

cvlassomod <- glmnet(x, y, alpha=1,lambda=lambdalevels)
yhat <- predict(cvlassomod$glmnet, s=cvlassomod$lambdamin, newx=outsamplex) 

这里我收到一个错误:UseMethod中的错误(&#34;预测&#34;):没有适用的方法来预测&#39;应用于类&#34; NULL&#34;

的对象
sselas <- sum((outsampley-yhat)^2)
tss <- sum((outsampley -mean(insampley))^2)
las <- (tss-sselas)/tss
las

有关如何通过将NULL拉入预测函数来解决此问题的任何建议吗?

谢谢!

1 个答案:

答案 0 :(得分:0)

该错误也可能是由于vvlassomod $ lambdamin不存在。您改用了vlassomod $ lambda.min吗?