Predictions of ridge regression in R

笑着哭i 提交于 2019-12-10 18:45:04

问题


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"

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


回答1:


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)

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



来源:https://stackoverflow.com/questions/37895372/predictions-of-ridge-regression-in-r

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!