Search code examples
rglmnet

glmnet predict method throwing cryptic error


I am trying to make a prediction using glmnet, and getting a very cryptic error message. I have not encountered this before when using glmnet, and Googling for the error was not fruitful. The error happens when the last line is un-commented.

library(ISLR)
library(glmnet)


Hitters=na.omit(Hitters)
Hitters$Salary = log(Hitters$Salary)

Hitters.train = Hitters[1:200,]
Hitters.test = Hitters[201:dim(Hitters)[1],]

x=model.matrix(Salary~.,Hitters)[,-1]
cv.out=cv.glmnet(x, Hitters$Salary, alpha=0)
bestlam=cv.out$lambda.min
ridge.mod=glmnet(x, Hitters$Salary, alpha=0,lambda=bestlam)

newx = data.matrix(Hitters.test)
#ridge.pred=predict(ridge.mod,s=bestlam,newx=newx)

Error output:

Loading required package: Matrix
Loading required package: methods
Loaded glmnet 1.9-5

Error in as.matrix(cbind2(1, newx) %*% nbeta) : 
  error in evaluating the argument 'x' in selecting a method for function 'as.matrix': Error in t(.Call(Csparse_dense_crossprod, y, t(x))) : 
  error in evaluating the argument 'x' in selecting a method for function 't': Error: Cholmod error 'X and/or Y have wrong dimensions' at file ../MatrixOps/cholmod_sdmult.c, line 90
Calls: %*% -> %*% -> t
Calls: predict ... predict.elnet -> NextMethod -> predict.glmnet -> as.matrix
Execution halted

Note that changing newx = data.matrix(Hitters.test) to newx = model.matrix(Salary~.,Hitters.test) did not help.

As requested, here is the output of sessionInfo() before running.

> sessionInfo()
R version 3.0.2 (2013-09-25)
Platform: x86_64-unknown-linux-gnu (64-bit)

locale:
[1] C

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base

Here is the output after running:

> sessionInfo()
R version 3.0.2 (2013-09-25)
Platform: x86_64-unknown-linux-gnu (64-bit)

locale:
[1] C

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base

other attached packages:
[1] glmnet_1.9-5 Matrix_1.1-0 ISLR_1.0

loaded via a namespace (and not attached):
[1] grid_3.0.2      lattice_0.20-23

Solution

  • It turns out that I must NULL out the response. The following works without error:

    library(ISLR)
    library(glmnet)
    
    
    Hitters=na.omit(Hitters)
    Hitters$Salary = log(Hitters$Salary)
    
    Hitters.train = Hitters[1:200,]
    Hitters.test = Hitters[201:dim(Hitters)[1],]
    
    x=model.matrix(Salary~.,Hitters)[,-1]
    cv.out=cv.glmnet(x, Hitters$Salary, alpha=0)
    bestlam=cv.out$lambda.min
    ridge.mod=glmnet(x, Hitters$Salary, alpha=0,lambda=bestlam)
    
    Hitters.test$Salary <- NULL
    newx = data.matrix(Hitters.test)
    ridge.pred=predict(ridge.mod,s=bestlam,newx=newx)