MASS :: lm.ridge odds
I am trying to perform ridge regression using the method lm.ridge
. My question is, how can I get the coefficients of the model? I get different results from the call:
-
model$coef
-
coef(model)
Which one is correct? Also, why am I getting different results from the call:
-
coef(model)
and looking at the 1st odds, vs. -
coef(model)[1]
?
As ?lm.ridge
says (in element description of $coef
returned object) [in italics]
coef: matrix of coefficients, one row for each lambda value. Note that they are not at original scale and are intended to be used by the coef method.
This means that the item is $coef
not intended for end users ("if you have to ask ..."). (If you want to see how the translation is $coef
, check it out MASS:::coef.ridgelm
.) In general, it is better to use an accessor, such coef()
as when it exists, than to fetch components from the guts of a returned object using $
(or @
for S4 objects) - precisely for this reason. Package authors provide methods coef()
for a specific reason ...
I cannot repeat your second question. Using the from model ?lm.ridge
, the answers appear to be identical except for the precision with which they are printed ...
> m1 <- lm.ridge(y ~ ., longley)
> coef(m1)
GNP Unemployed Armed.Forces Population
2946.85636017 0.26352725 0.03648291 0.01116105 -1.73702984
Year Employed
-1.41879853 0.23128785
> coef(m1)[1]
2946.856
source to share