Debian Codegraph @

R/stats-summary-lm.R

raw
1 #' @templateVar class summary.lm
2 #' @template title_desc_tidy
3 #'
4 #' @param x A `summary.lm` object created by [stats::summary.lm()].
5 #' @template param_confint
6 #' @template param_unused_dots
7 #'
8 #' @evalRd return_tidy(regression = TRUE)
9 #'
10 #' @details The `tidy.summary.lm()` method is a potentially useful alternative
11 #' to [tidy.lm()]. For instance, if users have already converted large `lm`
12 #' objects into their leaner `summary.lm` equivalents to conserve memory.
13 #'
14 #' @examples
15 #'
16 #' # fit model
17 #' mod <- lm(mpg ~ wt + qsec, data = mtcars)
18 #' modsumm <- summary(mod)
19 #'
20 #' # summarize model fit with tidiers
21 #' tidy(mod, conf.int = TRUE)
22 #'
23 #' # equivalent to the above
24 #' tidy(modsumm, conf.int = TRUE)
25 #'
26 #' glance(mod)
27 #'
28 #' # mostly the same, except for a few missing columns
29 #' glance(modsumm)
30 #'
31 #' @export
32 #' @seealso [tidy()], [stats::summary.lm()]
33 #' @family lm tidiers
34 tidy.summary.lm <- function(x, conf.int = FALSE, conf.level = 0.95, ...) {
35 ret <- as_tibble(x$coefficients, rownames = "term")
36 colnames(ret) <- c("term", "estimate", "std.error", "statistic", "p.value")
37 # aside: The x$coefficients will miss rank deficient rows (i.e. coefs that
38 # summary.lm() sets to NA). We can't do much about that here, though since the
39 # user has already passed the summary(x) object into the tidy call...
40
41 # There's no confint() method for summary.lm object, but these are easy enough
42 # to calculate manually
43 if (conf.int) {
44 ret <- ret |>
45 dplyr::mutate(
46 conf.low = estimate -
47 stats::qt(1 - (1 - conf.level) / 2, df = x$df[2]) * std.error,
48 conf.high = estimate +
49 stats::qt(1 - (1 - conf.level) / 2, df = x$df[2]) * std.error
50 )
51 }
52
53 ret
54 }
55
56
57 # additional glance method for summary.lm objects
58 #' @templateVar class summary.lm
59 #' @template title_desc_glance
60 #'
61 #' @inherit tidy.lm params examples
62 #'
63 #' @evalRd return_glance(
64 #' "r.squared",
65 #' "adj.r.squared",
66 #' "sigma",
67 #' "statistic",
68 #' "p.value",
69 #' df = "The degrees for freedom from the numerator of the overall
70 #' F-statistic. This is new in broom 0.7.0. Previously, this reported
71 #' the rank of the design matrix, which is one more than the numerator
72 #' degrees of freedom of the overall F-statistic.",
73 #' "df.residual",
74 #' "nobs"
75 #' )
76 #' @details The `glance.summary.lm()` method is a potentially useful alternative
77 #' to [glance.lm()]. For instance, if users have already converted large `lm`
78 #' objects into their leaner `summary.lm` equivalents to conserve memory. Note,
79 #' however, that this method does not return all of the columns of the
80 #' non-summary method (e.g. AIC and BIC will be missing.)
81 #' @export
82 #' @seealso [glance()], [glance.summary.lm()]
83 #' @family lm tidiers
84 glance.summary.lm <- function(x, ...) {
85 # check whether the model was fitted with only an intercept, in which
86 # case drop the fstatistic related columns
87 int_only <- nrow(x$coefficients) == 1
88 with(
89 x,
90 tibble(
91 r.squared = r.squared,
92 adj.r.squared = adj.r.squared,
93 sigma = sigma,
94 statistic = if (!int_only) {
95 fstatistic["value"]
96 } else {
97 NA_real_
98 },
99 p.value = if (!int_only) {
100 stats::pf(
101 fstatistic["value"],
102 fstatistic["numdf"],
103 fstatistic["dendf"],
104 lower.tail = FALSE
105 )
106 } else {
107 NA_real_
108 },
109 df = if (!int_only) {
110 fstatistic["numdf"]
111 } else {
112 NA_real_
113 },
114 # We can back out one or two more stats that would normally come with tidy.lm
115 df.residual = as.integer(x$fstatistic["dendf"]),
116 nobs = sum(as.integer(x$fstatistic[c("numdf", "dendf")])) + 1
117 )
118 )
119 }