[R] Sparse Matrices and glmnet

Lorenzo Isella lorenzo.isella at gmail.com
Sat Jun 22 21:06:00 CEST 2013


Dear All,
I am not going to discuss in detail the implementation of a model, but I  
am rather puzzled because, even if I manage to train a model, then I do  
not succeed in applying it to some test data.
I am sure I am making some trivial mistake, but so far I have been banging  
my head against the floor.
When I run the following code



###############################################
library(glmnet)



test <- read.csv("test.csv", header=TRUE)

train <- read.csv("train.csv", header=TRUE)

for (i in seq(dim(train)[2])){

train[[i]] <- as.factor(train[[i]])

}




for (i in seq(dim(test)[2])){

test[[i]] <- as.factor(test[[i]])

}





X = sparse.model.matrix(as.formula(paste("ACTION ~",  
paste(colnames(train[,-1]),
     sep = "", collapse=" +"))), data = train)

  model = cv.glmnet(X, train[,1], family = "binomial")

print("glmnet model completed")



predict(model,newx=test[,2:10], s="lambda.min")

######################################################
I get the error

Error in as.matrix(cbind2(1, newx) %*% nbeta) :
   error in evaluating the argument 'x' in selecting a method for function  
'as.matrix': Error in cbind2(1, newx) %*% nbeta :
   not-yet-implemented method for <data.frame> %*% <dgCMatrix>

However, even converting test to a matrix does not help.
Essentially, I am trying to train a linear model to deal with a set of  
predictors that consist only of categorical variables.

The train and test datasets can be downloaded from

http://db.tt/23DzlIt3


http://db.tt/3GIcNpSE

Any suggestion is welcome.
Cheers

Lorenzo



More information about the R-help mailing list