Error in R: no applicable method for 'predict' applied to an object of class “NULL”

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



Error in R: no applicable method for 'predict' applied to an object of class “NULL”



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)


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!





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 5:30



cvlassomod


glmnet


cvlassomod$glmnet


NULL


predict


cvlassomod


predict


outsamplex


NULL


cvlassomod$lambdamin




1 Answer
1



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






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

How to determine optimal route across keyboard