简体   繁体   中英

Ridge regression - how to create lm object from ridgelm object

I am having problems with creating lm object from the 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 a lm object from the ridgelm in order to use it with predict function. That turns 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 am unable to read the intercept from lm.ridge model.

Since you are working with a linear model, I think the solution is a simple matrix product of the coefficients and your new data.

Note that you have to add a column consisting entiry of 1, indicating the intercept, to correspond to the first model coefficient.

Try this:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM