2

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!

Elle
  • 97
  • 2
  • 6
  • 14
  • 2
    `cvlassomod` does not have an element `glmnet`: `cvlassomod$glmnet` is `NULL`. The first argument to `predict` should (most likely) be `cvlassomod`. You're going to run into another error with `predict` because `outsamplex` also appears to be `NULL` as is `cvlassomod$lambdamin`. – mattdevlin Aug 22 '15 at 05:30

1 Answers1

0

The error might also be coming from the fact that vvlassomod$lambdamin does not exist. Have you tried vlassomod$lambda.min instead?