[Rd] as.data.frame() methods for model objects

SOEIRO Thomas Thom@@@SOEIRO @end|ng |rom @p-hm@|r
Thu Jan 16 14:36:02 CET 2025


Hello all,

Would there be any interest for adding as.data.frame() methods for model objects?
Of course there is packages (e.g. broom), but I think providing methods would be more discoverable (and the patch would be small).
It is really useful for exporting model results or for plotting.

e.g.:

as.data.frame.lm <- function(x) { # could get other arguments, e.g. exp = TRUE/FALSE to exponentiate estimate, conf.low, conf.high
  cf <- x |> summary() |> stats::coef()
  ci <- stats::confint(x)
  data.frame(
    term = row.names(cf),
    estimate = cf[, "Estimate"],
    p.value = cf[, 4], # magic number because name changes between lm() and glm(*, family = *)
    conf.low = ci[, "2.5 %"],
    conf.high = ci[, "97.5 %"],
    row.names = NULL
  )
}

> lm(breaks ~ wool + tension, warpbreaks) |> as.data.frame()
         term   estimate      p.value  conf.low  conf.high
1 (Intercept)  39.277778 6.681866e-17  32.92715 45.6284061
2       woolB  -5.777778 7.361367e-02 -12.12841  0.5728505
3    tensionM -10.000000 1.278683e-02 -17.77790 -2.2221006
4    tensionH -14.722222 3.913842e-04 -22.50012 -6.9443228

> glm(breaks < 20 ~ wool + tension, data = warpbreaks) |> as.data.frame()
Waiting for profiling to be done...
         term   estimate    p.value    conf.low conf.high
1 (Intercept) 0.07407407 0.54849393 -0.16624575 0.3143939
2       woolB 0.07407407 0.54849393 -0.16624575 0.3143939
3    tensionM 0.22222222 0.14520270 -0.07210825 0.5165527
4    tensionH 0.33333333 0.03100435  0.03900286 0.6276638

Thank you.

Best regards,
Thomas



More information about the R-devel mailing list