【问题标题】:Predictions of ridge regression in RR中岭回归的预测
【发布时间】:2016-06-18 17:34:24
【问题描述】:

我真的被困在这个问题上,希望有人能帮助我!我有一个包含 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 等评估指标)?

【问题讨论】:

    标签: r regression


    【解决方案1】:

    对于ridgelm 对象确实没有predict 方法:您必须手动完成。顺便说一句,正如您所见,summary 也没有:

    > methods(class = 'ridgelm')
    [1] coef   plot   print  select
    

    这是线性模型,所以拟合只是一个矩阵计算的问题:

    y.pred <- as.matrix(cbind(const=1,trainset)) %*% coef(model)
    

    我们需要加上常数1来与线性模式的常数系数相关联。

    重要提示:要使用岭回归,通常是缩放解释变量,因此减去均值。最佳实践应该是从训练中学习缩放定义,然后使用训练集方法从新数据中集中变量。您可能对交叉验证的相关帖子感兴趣:

    https://stats.stackexchange.com/questions/152203/how-to-calculate-predicted-values-using-an-lm-ridge-object

    【讨论】:

      猜你喜欢
      • 2018-08-31
      • 2016-02-25
      • 1970-01-01
      • 1970-01-01
      • 2016-09-10
      • 2016-10-21
      • 1970-01-01
      • 1970-01-01
      • 2015-08-13
      相关资源
      最近更新 更多