Ridge regression - how to create an lm object from a ridgelm object
I am having problems creating an lm object from a ridgelm object.
x1 <- rnorm(20)
x2 <- rnorm(20, mean=x1, sd=.01)
y <- rnorm(20, mean=2+x1+x2)
data <- data.frame(x1=x1, x2=x2, y=y)
model <- lm.ridge(y~x1+x2, lambda=1, data=data)
predict(model, newdata = data)
Gives an error:
Error in UseMethod("predict") :
no applicable method for 'predict' applied to an object of class "ridgelm"
I am trying to create an lm object from ridgelm so I can use it with a prediction function. This turned out to be more difficult than I expected.
My attempt:
m <- lm(y~x1+x2, data=data)
m$coefficients[["r1"]] <- model$coef[["r1"]]
m$coefficients[["r2"]] <- model$coef[["r1"]]
m$coefficients[["r3"]] <- model$coef[["r3"]]
m$coefficients[["(Intercept)"]] <- ???
Somehow I can't read the intercept from the lm.ridge model.
+3
source to share
1 answer
Since you are working with a linear model, I believe this solution is a simple matrix product of the coefficients and your new data.
Note that you need to add a column of 1 indicating the intercept to match the first coefficient of the model.
Try the following:
set.seed(1)
newdat <- data.frame(1, x1 = rnorm(5), x2=rnorm(5))
as.matrix(newdat) %*% coef(model)
[,1]
[1,] 0.1648573
[2,] 2.5017097
[3,] 1.7058644
[4,] 4.0967116
[5,] 1.7597485
+3
source to share