R中岭回归的预测 [英] Predictions of ridge regression in R

查看:76
本文介绍了R中岭回归的预测的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直被困在这个问题上,希望任何人都可以帮助我!我有一个包含 54 列的数据集,我想用岭回归对测试集进行预测.

I've been really stuck on this, hope anyone can help me! I have a dataset with 54 columns and I want to make predictions on a test set with ridge regression.

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)))
}

执行此操作时,我收到以下错误消息:

When I do this I get the following error message:

Error in UseMethod("predict") : 
  no applicable method for 'predict' applied to an object of class "ridgelm"

用岭回归进行预测的另一种方法是什么(也可以使用 rsquared 和 MAE 等评估指标)?

What could be another way to make predictions with ridge regression that works (also with evaluation metrics such as rsquared and MAE)?

推荐答案

对于 ridgelm 对象确实没有 predict 方法:您必须手动完成.顺便说一句,你可以看到,没有 summary :

There is indeed no predict method for ridgelm object: you will have to do it by hands. By the way, there is neither summary as you can see with:

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

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

This is linear model, so that fitting is just a question of matricial computation:

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

我们需要添加常数 1 以与线性模式的常数系数相关联.

We need to add the constant 1 to be associated with the constant coefficient of the linear mode.

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

Important: to use ridge regression, one usually scale explanatory variables, so that means are substracted. The best practice should be to learn scaling definition from training and then to use training set means to center variables from new data. You may be interested in this related post on cross-validated:

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

这篇关于R中岭回归的预测的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆