I'm trying to estimate some accuracy for coefficients. Here's my code:
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)
Here I get an error: Error in UseMethod("predict") : no applicable method for 'predict' applied to an object of class "NULL"
sselas <- sum((outsampley-yhat)^2)
tss <- sum((outsampley -mean(insampley))^2)
las <- (tss-sselas)/tss
las
Any suggestions on how to correct this from pulling a NULL into the predict function?
Thanks!