我正在尝试使用R的glmnet
包中的岭回归来预测汽车的mpg
,基于一些变量。我已经将数据分成了训练和测试数据,并对分类变量进行了虚拟编码。
我按照以下方式拟合了交叉验证模型:
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")
虽然这很好,但当我尝试在拟合模型的测试数据上使用predict()
函数时,问题就出现了:
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
有人能告诉我我做错了什么吗?谢谢!