I am trying to predict an MPG car based on a number of variables using ridge regression in the GLMNET R package. I already divided the data into training and test data, and the dummy coded categorical variables.
I approach the cross-validation model as follows:
require("glmnet")
x <- as.matrix(data.frame(cylinderDummy[,2:ncol(cylinderDummy)], trainData$displacement,
trainData$horsepower, trainData$weight, trainData$acceleration,
originDummy[,2:ncol(originDummy)]))
y <- trainData$mpg
cv.fit <- cv.glmnet(x, y, alpha = 1, nfolds=5,type.measure="mse")
This is all good and good. However, the problem arises when I try to use the "predict" () function for test data from the installed model:
prediction <- predict(cv.fit, testData$mpg, s="lambda.1se")
I get the following 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 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
Can someone tell me what I am doing wrong? Thank!