Debian Codegraph @

man/tidy.lm.Rd

raw
1 % Generated by roxygen2: do not edit by hand
2 % Please edit documentation in R/stats-lm.R
3 \name{tidy.lm}
4 \alias{tidy.lm}
5 \alias{lm_tidiers}
6 \title{Tidy a(n) lm object}
7 \usage{
8 \method{tidy}{lm}(x, conf.int = FALSE, conf.level = 0.95, exponentiate = FALSE, ...)
9 }
10 \arguments{
11 \item{x}{An \code{lm} object created by \code{\link[stats:lm]{stats::lm()}}.}
12
13 \item{conf.int}{Logical indicating whether or not to include a confidence
14 interval in the tidied output. Defaults to \code{FALSE}.}
15
16 \item{conf.level}{The confidence level to use for the confidence interval
17 if \code{conf.int = TRUE}. Must be strictly greater than 0 and less than 1.
18 Defaults to 0.95, which corresponds to a 95 percent confidence interval.}
19
20 \item{exponentiate}{Logical indicating whether or not to exponentiate the
21 the coefficient estimates. This is typical for logistic and multinomial
22 regressions, but a bad idea if there is no log or logit link. Defaults
23 to \code{FALSE}.}
24
25 \item{...}{Additional arguments. Not used. Needed to match generic
26 signature only. \strong{Cautionary note:} Misspelled arguments will be
27 absorbed in \code{...}, where they will be ignored. If the misspelled
28 argument has a default value, the default value will be used.
29 For example, if you pass \code{conf.lvel = 0.9}, all computation will
30 proceed using \code{conf.level = 0.95}. Two exceptions here are:
31 \itemize{
32 \item \code{tidy()} methods will warn when supplied an \code{exponentiate} argument if
33 it will be ignored.
34 \item \code{augment()} methods will warn when supplied a \code{newdata} argument if it
35 will be ignored.
36 }}
37 }
38 \description{
39 Tidy summarizes information about the components of a model.
40 A model component might be a single term in a regression, a single
41 hypothesis, a cluster, or a class. Exactly what tidy considers to be a
42 model component varies across models but is usually self-evident.
43 If a model has several distinct types of components, you will need to
44 specify which components to return.
45 }
46 \details{
47 If the linear model is an \code{mlm} object (multiple linear model),
48 there is an additional column \code{response}. See \code{\link[=tidy.mlm]{tidy.mlm()}}.
49 }
50 \examples{
51 \dontshow{if (rlang::is_installed("ggplot2")) withAutoprint(\{ # examplesIf}
52
53 library(ggplot2)
54 library(dplyr)
55
56 mod <- lm(mpg ~ wt + qsec, data = mtcars)
57
58 tidy(mod)
59 glance(mod)
60
61 # coefficient plot
62 d <- tidy(mod, conf.int = TRUE)
63
64 ggplot(d, aes(estimate, term, xmin = conf.low, xmax = conf.high, height = 0)) +
65 geom_point() +
66 geom_vline(xintercept = 0, lty = 4) +
67 geom_errorbarh()
68
69 # aside: There are tidy() and glance() methods for lm.summary objects too.
70 # this can be useful when you want to conserve memory by converting large lm
71 # objects into their leaner summary.lm equivalents.
72 s <- summary(mod)
73 tidy(s, conf.int = TRUE)
74 glance(s)
75
76 augment(mod)
77 augment(mod, mtcars, interval = "confidence")
78
79 # predict on new data
80 newdata <- mtcars |>
81 head(6) |>
82 mutate(wt = wt + 1)
83 augment(mod, newdata = newdata)
84
85 # ggplot2 example where we also construct 95\% prediction interval
86
87 # simpler bivariate model since we're plotting in 2D
88 mod2 <- lm(mpg ~ wt, data = mtcars)
89
90 au <- augment(mod2, newdata = newdata, interval = "prediction")
91
92 ggplot(au, aes(wt, mpg)) +
93 geom_point() +
94 geom_line(aes(y = .fitted)) +
95 geom_ribbon(aes(ymin = .lower, ymax = .upper), col = NA, alpha = 0.3)
96
97 # predict on new data without outcome variable. Output does not include .resid
98 newdata <- newdata |>
99 select(-mpg)
100
101 augment(mod, newdata = newdata)
102
103 au <- augment(mod, data = mtcars)
104
105 ggplot(au, aes(.hat, .std.resid)) +
106 geom_vline(size = 2, colour = "white", xintercept = 0) +
107 geom_hline(size = 2, colour = "white", yintercept = 0) +
108 geom_point() +
109 geom_smooth(se = FALSE)
110
111 plot(mod, which = 6)
112
113 ggplot(au, aes(.hat, .cooksd)) +
114 geom_vline(xintercept = 0, colour = NA) +
115 geom_abline(slope = seq(0, 3, by = 0.5), colour = "white") +
116 geom_smooth(se = FALSE) +
117 geom_point()
118
119 # column-wise models
120 a <- matrix(rnorm(20), nrow = 10)
121 b <- a + rnorm(length(a))
122 result <- lm(b ~ a)
123
124 tidy(result)
125 \dontshow{\}) # examplesIf}
126 }
127 \seealso{
128 \code{\link[=tidy]{tidy()}}, \code{\link[stats:summary.lm]{stats::summary.lm()}}
129
130 Other lm tidiers:
131 \code{\link{augment.glm}()},
132 \code{\link{augment.lm}()},
133 \code{\link{glance.glm}()},
134 \code{\link{glance.lm}()},
135 \code{\link{glance.summary.lm}()},
136 \code{\link{glance.svyglm}()},
137 \code{\link{tidy.glm}()},
138 \code{\link{tidy.lm.beta}()},
139 \code{\link{tidy.mlm}()},
140 \code{\link{tidy.summary.lm}()}
141 }
142 \concept{lm tidiers}
143 \value{
144 A \code{\link[tibble:tibble]{tibble::tibble()}} with columns:
145 \item{conf.high}{Upper bound on the confidence interval for the estimate.}
146 \item{conf.low}{Lower bound on the confidence interval for the estimate.}
147 \item{estimate}{The estimated value of the regression term.}
148 \item{p.value}{The two-sided p-value associated with the observed statistic.}
149 \item{statistic}{The value of a T-statistic to use in a hypothesis that the regression term is non-zero.}
150 \item{std.error}{The standard error of the regression term.}
151 \item{term}{The name of the regression term.}
152
153 }