7

I wonder is there a way to output summary for ridge regression in R? It is a result of lm.ridge{MASS} function.

For standard linear model you just do summary(lm_model) but what about ridge regression model? Thanks for help.

smci
  • 32,567
  • 20
  • 113
  • 146
Marcin
  • 7,834
  • 8
  • 52
  • 99

3 Answers3

10

I just added a method that summarizes (or more precisely, tidies) "ridgelm" objects to my broom package. This takes the form of two S3 generics: tidy and glance. You can install it with devtools::install_github("dgrtwo/broom") (though you'll need to install devtools first).

As an example, let's set up a ridge regression:

library(MASS)
names(longley)[1] <- "y"
fit <- lm.ridge(y ~ ., longley, lambda = seq(0.001, .05, .001))

The tidy function provides a data frame that shows each combination of lambda and the estimated term:

library(broom)
td <- tidy(fit)
head(td)
##   lambda    GCV term estimate
## 1  0.001 0.1240  GNP    23.02
## 2  0.002 0.1217  GNP    21.27
## 3  0.003 0.1205  GNP    19.88
## 4  0.004 0.1199  GNP    18.75
## 5  0.005 0.1196  GNP    17.80
## 6  0.006 0.1196  GNP    16.99

While the glance function creates a one-row summary, particularly the choices of lambda by various methods:

g <- glance(fit)
g
##       kHKB     kLW lambdaGCV
## 1 0.006837 0.05267     0.006

This is useful because it makes it easy to plot and explore the data yourself rather than relying on MASS's plotters:

library(ggplot2)
ggplot(td, aes(lambda, estimate, color = term)) + geom_line()

enter image description here

# plot of GCV versus lambda
ggplot(td, aes(lambda, GCV)) + geom_line() +
    geom_vline(xintercept = g$lambdaGCV, col = "red", lty = 2)

enter image description here

For more on these methods, see ?ridgelm_tidiers, or see the package's vignettes for more about the tidy and glance methods in general.

David Robinson
  • 77,383
  • 16
  • 167
  • 187
  • Instead of glance You can use builtin function 'select' - it works the same. The ggplot function works the same as regular plot.lmridge (but of course looks more pritty). – Marcin Oct 15 '14 at 19:43
  • 2
    @MarcinKosinski: `select` doesn't work the same way because it *prints* the selected values rather than returning them (so there's no way to save them, extract them, combine them, or add them to a graph, for example). `plot.ridgelm` produces a similar graph, but it's worth noting that it doesn't come with a legend, or a way to choose the colors. (Actually since the `plot.ridgelm` doesn't pass along its `...` to `matplot`, you have limited ability to customize it at all). – David Robinson Oct 15 '14 at 20:40
  • Ok I get it. Thanks :) – Marcin Oct 23 '14 at 12:48
2

There is no summary method for the ridgelm class:

> methods(class = 'ridgelm')
[1] coef.ridgelm*   plot.ridgelm*   print.ridgelm*  select.ridgelm*

What should this summary return? You can extract all the information you need from the ridgelm-object.

However, you also could write your own summary methods for your purposes (check the code for summary.lm() for a start). If you're happy with it, you could send it to the maintainers of MASS.

EDi
  • 13,160
  • 2
  • 48
  • 57
  • You really think they would care and publish this function in their package? – Marcin Oct 14 '14 at 15:53
  • 1
    You might get an interesting reply. Generally Prof Ripley will put in support functions when he deems them appropriate, so there may be reasons that he did not. – IRTFM Oct 14 '14 at 22:03
1

you can use my lmridge package from CRAN.

itfeature.com
  • 380
  • 4
  • 16
  • I would appreciate extending the answer with a short use case and probably the literature I can refer to! Thanks in advance. – Marcin Aug 10 '17 at 11:30