我试图估计一些系数的准确度。这是我的代码:
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)
sselas <- sum((outsampley-yhat)^2)
tss <- sum((outsampley -mean(insampley))^2)
las <- (tss-sselas)/tss
las
有关如何通过将NULL拉入预测函数来解决此问题的任何建议吗?
谢谢!
答案 0 :(得分:0)
该错误也可能是由于vvlassomod $ lambdamin不存在。您改用了vlassomod $ lambda.min吗?