Reputation: 301
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)?
Upvotes: 3
Views: 11373
Reputation: 1481
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:
Upvotes: 4