Debian Codegraph @

man/glance.lm.Rd

raw
1 % Generated by roxygen2: do not edit by hand
2 % Please edit documentation in R/stats-lm.R
3 \name{glance.lm}
4 \alias{glance.lm}
5 \title{Glance at a(n) lm object}
6 \usage{
7 \method{glance}{lm}(x, ...)
8 }
9 \arguments{
10 \item{x}{An \code{lm} object created by \code{\link[stats:lm]{stats::lm()}}.}
11
12 \item{...}{Additional arguments. Not used. Needed to match generic
13 signature only. \strong{Cautionary note:} Misspelled arguments will be
14 absorbed in \code{...}, where they will be ignored. If the misspelled
15 argument has a default value, the default value will be used.
16 For example, if you pass \code{conf.lvel = 0.9}, all computation will
17 proceed using \code{conf.level = 0.95}. Two exceptions here are:
18 \itemize{
19 \item \code{tidy()} methods will warn when supplied an \code{exponentiate} argument if
20 it will be ignored.
21 \item \code{augment()} methods will warn when supplied a \code{newdata} argument if it
22 will be ignored.
23 }}
24 }
25 \description{
26 Glance accepts a model object and returns a \code{\link[tibble:tibble]{tibble::tibble()}}
27 with exactly one row of model summaries. The summaries are typically
28 goodness of fit measures, p-values for hypothesis tests on residuals,
29 or model convergence information.
30
31 Glance never returns information from the original call to the modeling
32 function. This includes the name of the modeling function or any
33 arguments passed to the modeling function.
34
35 Glance does not calculate summary measures. Rather, it farms out these
36 computations to appropriate methods and gathers the results together.
37 Sometimes a goodness of fit measure will be undefined. In these cases
38 the measure will be reported as \code{NA}.
39
40 Glance returns the same number of columns regardless of whether the
41 model matrix is rank-deficient or not. If so, entries in columns
42 that no longer have a well-defined value are filled in with an \code{NA}
43 of the appropriate type.
44 }
45 \examples{
46 \dontshow{if (rlang::is_installed("ggplot2")) withAutoprint(\{ # examplesIf}
47
48 library(ggplot2)
49 library(dplyr)
50
51 mod <- lm(mpg ~ wt + qsec, data = mtcars)
52
53 tidy(mod)
54 glance(mod)
55
56 # coefficient plot
57 d <- tidy(mod, conf.int = TRUE)
58
59 ggplot(d, aes(estimate, term, xmin = conf.low, xmax = conf.high, height = 0)) +
60 geom_point() +
61 geom_vline(xintercept = 0, lty = 4) +
62 geom_errorbarh()
63
64 # aside: There are tidy() and glance() methods for lm.summary objects too.
65 # this can be useful when you want to conserve memory by converting large lm
66 # objects into their leaner summary.lm equivalents.
67 s <- summary(mod)
68 tidy(s, conf.int = TRUE)
69 glance(s)
70
71 augment(mod)
72 augment(mod, mtcars, interval = "confidence")
73
74 # predict on new data
75 newdata <- mtcars |>
76 head(6) |>
77 mutate(wt = wt + 1)
78 augment(mod, newdata = newdata)
79
80 # ggplot2 example where we also construct 95\% prediction interval
81
82 # simpler bivariate model since we're plotting in 2D
83 mod2 <- lm(mpg ~ wt, data = mtcars)
84
85 au <- augment(mod2, newdata = newdata, interval = "prediction")
86
87 ggplot(au, aes(wt, mpg)) +
88 geom_point() +
89 geom_line(aes(y = .fitted)) +
90 geom_ribbon(aes(ymin = .lower, ymax = .upper), col = NA, alpha = 0.3)
91
92 # predict on new data without outcome variable. Output does not include .resid
93 newdata <- newdata |>
94 select(-mpg)
95
96 augment(mod, newdata = newdata)
97
98 au <- augment(mod, data = mtcars)
99
100 ggplot(au, aes(.hat, .std.resid)) +
101 geom_vline(size = 2, colour = "white", xintercept = 0) +
102 geom_hline(size = 2, colour = "white", yintercept = 0) +
103 geom_point() +
104 geom_smooth(se = FALSE)
105
106 plot(mod, which = 6)
107
108 ggplot(au, aes(.hat, .cooksd)) +
109 geom_vline(xintercept = 0, colour = NA) +
110 geom_abline(slope = seq(0, 3, by = 0.5), colour = "white") +
111 geom_smooth(se = FALSE) +
112 geom_point()
113
114 # column-wise models
115 a <- matrix(rnorm(20), nrow = 10)
116 b <- a + rnorm(length(a))
117 result <- lm(b ~ a)
118
119 tidy(result)
120 \dontshow{\}) # examplesIf}
121 }
122 \seealso{
123 \code{\link[=glance]{glance()}}, \code{\link[=glance.summary.lm]{glance.summary.lm()}}
124
125 Other lm tidiers:
126 \code{\link{augment.glm}()},
127 \code{\link{augment.lm}()},
128 \code{\link{glance.glm}()},
129 \code{\link{glance.summary.lm}()},
130 \code{\link{glance.svyglm}()},
131 \code{\link{tidy.glm}()},
132 \code{\link{tidy.lm}()},
133 \code{\link{tidy.lm.beta}()},
134 \code{\link{tidy.mlm}()},
135 \code{\link{tidy.summary.lm}()}
136 }
137 \concept{lm tidiers}
138 \value{
139 A \code{\link[tibble:tibble]{tibble::tibble()}} with exactly one row and columns:
140 \item{adj.r.squared}{Adjusted R squared statistic, which is like the R squared statistic except taking degrees of freedom into account.}
141 \item{AIC}{Akaike's Information Criterion for the model.}
142 \item{BIC}{Bayesian Information Criterion for the model.}
143 \item{deviance}{Deviance of the model.}
144 \item{df.residual}{Residual degrees of freedom.}
145 \item{logLik}{The log-likelihood of the model. [stats::logLik()] may be a useful reference.}
146 \item{nobs}{Number of observations used.}
147 \item{p.value}{P-value corresponding to the test statistic.}
148 \item{r.squared}{R squared statistic, or the percent of variation explained by the model. Also known as the coefficient of determination.}
149 \item{sigma}{Estimated standard error of the residuals.}
150 \item{statistic}{Test statistic.}
151 \item{df}{The degrees for freedom from the numerator of the overall
152 F-statistic. This is new in broom 0.7.0. Previously, this reported
153 the rank of the design matrix, which is one more than the numerator
154 degrees of freedom of the overall F-statistic.}
155
156 }