regression - computing ridge estimate manually in R, simple -


i'm trying learn ridge regression, , using r. understand following should same beta.r1 , beta.r2 in code below same

library(mass) n=50 v1=runif(n) v2=v1+2 v=cbind(1,v1,v2) w=3+v1+v2 i=diag(3)  lambda=2 #arbitrarily chosen  beta.r1=solve(t(v)%*%v+lambda*i)%*%t(v)%*%w  #using library(mass) fit=lm.ridge(w~v1+v2,lambda=2, inter=false) beta.r2=coef(fit) #shouldn't beta.r1 , beta.r2 same? 

i think variable scaling performed in lm.ridge code (which can access typing lm.ridge r console) cause differences. code scales each variable root-mean-squared value:

xscale <- drop(rep(1/n, n) %*% x^2)^0.5 x <- x/rep(xscale, rep(n, p)) 

your code not perform variable scaling.

the variable scaling hinted @ on ?lm.ridge page in description of returned lm.ridge:

scales: scalings used on x matrix.

therefore can access scaling used lm.ridge:

fit$scales #        v1        v2  # 0.2650311 0.2650311  

Comments

Popular posts from this blog

node.js - Using Node without global install -

How to access a php class file from PHPFox framework into javascript code written in simple HTML file? -

java - Null response to php query in android, even though php works properly -