R glmnet as.matrix() error message

When running the glmnet package, everything works fine until I tried to predict with the test set that does not have a response variable. I read all kind of solutions that do not work such as use as.matrix(), use model.matrix() or use data.matrix().

Does anyone knows?

Thanks,

============================== See code below =====================

library(lasso2)
library(glmnet)
require(methods)
set.seed(12345)



# Ridge Regression

#x.tr<-model.matrix(salePrice~OverallQual+Neighborhood+GarageYrBlt+GrLivArea+GarageCars+YearBuilt+GarageArea+ExterQual+KitchenQual+BsmtQual,data=FS.train)[,-8]
y.tr<-FS.train$salePrice
x.tr<-model.matrix(salePrice~., data = FS.train)[,-51]
#x.val<-model.matrix(salePrice~OverallQual+Neighborhood+GarageYrBlt+GrLivArea+GarageCars+YearBuilt+GarageArea+ExterQual+KitchenQual+BsmtQual,data=FS.test)[,-8]
x.val<-model.matrix(salePrice~., data = FS.test)[,-51]
y.val<-FS.test$salePrice



set.seed(12345)
rr.cv<-cv.glmnet(x.tr,y.tr,alpha=0,type.measure = "mse", nfolds = 10)
plot(rr.cv)

rr.bestlam <- rr.cv$lambda.min
rr.goodlam <- rr.cv$lambda.1se

# predict validation set using best lambda and calculate RMSE
rr.fit <- glmnet(x.tr, y.tr, alpha = 0)
plot(rr.fit, xvar = "lambda", label = TRUE)

rr.pred <- predict(rr.fit, s = rr.bestlam, newx = x.val)
sqrt(mean((rr.pred - y.val)^2))
mean(abs(rr.pred - y.val))

# Lasso Regression
set.seed(12345)

# Train model
las.cv <- cv.glmnet(x.tr, y.tr, alpha = 1, type.measure = "mse", nfolds = 10)
plot(las.cv)

# Select best model
las.bestlam <- las.cv$lambda.min
las.goodlam <- las.cv$lambda.1se

# predict validation set using best lambda and calculate RMSE
las.fit <- glmnet(x.tr, y.tr, alpha = 1)
plot(las.fit, xvar = "lambda", label = TRUE)

las.pred <- predict(las.fit, s = las.bestlam, newx = x.val)
sqrt(mean((las.pred - y.val)^2))
mean(abs(las.pred - y.val))

#coef(las.cv,s=las.cv$lambda.1se)
print("Ridge MSE :")
print(mean(abs(rr.pred - y.val)))
print("LASSO MSE :")
print(mean(abs(las.pred - y.val)))


#test.val<-model.matrix(salePrice~OverallQual+Neighborhood+GarageYrBlt+GrLivArea+GarageCars+YearBuilt+GarageArea+ExterQual+KitchenQual+BsmtQual,data=test)
#test.val<-model.matrix(~.,data=test)
dm<-data.matrix(test)
#dm<-as.matrix(test)

############### The following call fails #################
preds <- predict(las.fit, s = las.bestlam, newx = dm)

preds <- predict(las.fit, s = las.bestlam, newx = dm)
Error in cbind2(1, newx) %*% nbeta : 
  Cholmod error 'X and/or Y have wrong dimensions' at file ../MatrixOps/cholmod_sdmult.c, line 90

Try creating the models and training/test sets in dataframe instead of matrix format and see if it works. Make sure all missing values have been imputed before you run the regression. And double check factor variables in training and test sets have the same levels.