R中岭回归的预测
Predictions of ridge regression in R
我一直被困在这个问题上,希望有人能帮助我!我有一个包含 54 列的数据集,我想用岭回归对测试集进行预测。
nn <-nrow(longley)
index <- 1:nrow(longley)
testindex <- sample(index, trunc(length(index)/3))
testset <- longley[testindex,]
trainset <-longley[-testindex,]
trainset1 <- trainset[,-7]
# Fit the ridge regression model:
mod <- lm.ridge(y ~., data = trainset, lambda = 0.661)
# Predict and evaluate it by using MAE function:
mae <- function(model) {
y = trainset$Employed
y.pred <- predict(model, trainset)
return(mean(abs(y-y.pred)))
}
当我这样做时,我收到以下错误消息:
Error in UseMethod("predict") :
no applicable method for 'predict' applied to an object of class "ridgelm"
还有什么方法可以使用有效的岭回归进行预测(也可以使用 rsquared 和 MAE 等评估指标)?
ridgelm
对象确实没有 predict
方法:您必须手动完成。顺便说一下,没有 summary
正如你看到的那样:
> methods(class = 'ridgelm')
[1] coef plot print select
这是线性模型,所以拟合只是一个矩阵计算的问题:
y.pred <- as.matrix(cbind(const=1,trainset)) %*% coef(model)
我们需要加上常量1来关联线性模的常数系数
重要提示:要使用岭回归,通常要对解释变量进行缩放,以便减去均值。最佳实践应该是从训练中学习缩放定义,然后使用训练集方法从新数据中集中变量。您可能对交叉验证的相关 post 感兴趣:
我一直被困在这个问题上,希望有人能帮助我!我有一个包含 54 列的数据集,我想用岭回归对测试集进行预测。
nn <-nrow(longley)
index <- 1:nrow(longley)
testindex <- sample(index, trunc(length(index)/3))
testset <- longley[testindex,]
trainset <-longley[-testindex,]
trainset1 <- trainset[,-7]
# Fit the ridge regression model:
mod <- lm.ridge(y ~., data = trainset, lambda = 0.661)
# Predict and evaluate it by using MAE function:
mae <- function(model) {
y = trainset$Employed
y.pred <- predict(model, trainset)
return(mean(abs(y-y.pred)))
}
当我这样做时,我收到以下错误消息:
Error in UseMethod("predict") :
no applicable method for 'predict' applied to an object of class "ridgelm"
还有什么方法可以使用有效的岭回归进行预测(也可以使用 rsquared 和 MAE 等评估指标)?
ridgelm
对象确实没有 predict
方法:您必须手动完成。顺便说一下,没有 summary
正如你看到的那样:
> methods(class = 'ridgelm')
[1] coef plot print select
这是线性模型,所以拟合只是一个矩阵计算的问题:
y.pred <- as.matrix(cbind(const=1,trainset)) %*% coef(model)
我们需要加上常量1来关联线性模的常数系数
重要提示:要使用岭回归,通常要对解释变量进行缩放,以便减去均值。最佳实践应该是从训练中学习缩放定义,然后使用训练集方法从新数据中集中变量。您可能对交叉验证的相关 post 感兴趣: