Debian Codegraph @

R/mgcv.R

raw
1 #' @templateVar class gam
2 #' @template title_desc_tidy
3 #'
4 #' @param x A `gam` object returned from a call to [mgcv::gam()].
5 #' @param parametric Logical indicating if parametric or smooth terms should
6 #' be tidied. Defaults to `FALSE`, meaning that smooth terms are tidied
7 #' by default.
8 #' @template param_confint
9 #' @template param_exponentiate
10 #' @template param_unused_dots
11 #'
12 #' @evalRd return_tidy(
13 #' "term",
14 #' "estimate",
15 #' "std.error",
16 #' "statistic",
17 #' "p.value",
18 #' edf = "The effective degrees of freedom. Only reported when
19 #' `parametric = FALSE`",
20 #' ref.df = "The reference degrees of freedom. Only reported when
21 #' `parametric = FALSE`"
22 #' )
23 #'
24 #' @details When `parametric = FALSE` return columns `edf` and `ref.df` rather
25 #' than `estimate` and `std.error`.
26 #'
27 #'
28 #' @examplesIf rlang::is_installed("mgcv")
29 #'
30 #' # load libraries for models and data
31 #' library(mgcv)
32 #'
33 #' # fit model
34 #' g <- gam(mpg ~ s(hp) + am + qsec, data = mtcars)
35 #'
36 #' # summarize model fit with tidiers
37 #' tidy(g)
38 #' tidy(g, parametric = TRUE)
39 #' glance(g)
40 #' augment(g)
41 #'
42 #' @export
43 #' @aliases mgcv_tidiers gam_tidiers tidy.gam
44 #' @family mgcv tidiers
45 #' @seealso [tidy()], [mgcv::gam()]
46 tidy.gam <- function(
47 x,
48 parametric = FALSE,
49 conf.int = FALSE,
50 conf.level = 0.95,
51 exponentiate = FALSE,
52 ...
53 ) {
54 if (!parametric && conf.int) {
55 cli::cli_inform("Confidence intervals only available for parametric terms.")
56 }
57 if (!parametric && exponentiate) {
58 cli::cli_inform(
59 "Exponentiating coefficients only available for parametric terms."
60 )
61 }
62 if (parametric) {
63 px <- summary(x)$p.table
64 ret <- as_tidy_tibble(
65 px,
66 new_names = c("estimate", "std.error", "statistic", "p.value")
67 )
68 if (conf.int) {
69 # avoid "Waiting for profiling to be done..." message
70 # This message doesn't seem to happen with confint.default
71 CI <- suppressMessages(
72 stats::confint.default(x, level = conf.level)[
73 rownames(px),
74 ,
75 drop = FALSE
76 ]
77 )
78 # Think about rank deficiency
79 colnames(CI) <- c("conf.low", "conf.high")
80 ret <- cbind(ret, unrowname(CI))
81 ret <- as_tibble(ret)
82 }
83 } else {
84 sx <- summary(x)$s.table
85 sx <- as.data.frame(sx)
86 class(sx) <- c("anova", "data.frame")
87 ret <- tidy(sx)
88 }
89
90 if (exponentiate && parametric) {
91 ret <- exponentiate(ret)
92 }
93
94 ret
95 }
96
97 #' @templateVar class gam
98 #' @template title_desc_glance
99 #'
100 #' @inherit tidy.gam params examples
101 #'
102 #' @evalRd return_glance(
103 #' "df",
104 #' "logLik",
105 #' "AIC",
106 #' "BIC",
107 #' "deviance",
108 #' "df.residual",
109 #' "nobs",
110 #' "adj.r.squared",
111 #' "npar"
112 #' )
113 #'
114 #'
115 #' @export
116 #' @family mgcv tidiers
117 #' @seealso [glance()], [mgcv::gam()]
118 glance.gam <- function(x, ...) {
119 s <- summary(x)
120
121 as_glance_tibble(
122 df = sum(x$edf),
123 logLik = as.numeric(stats::logLik(x)),
124 AIC = stats::AIC(x),
125 BIC = stats::BIC(x),
126 deviance = stats::deviance(x),
127 df.residual = stats::df.residual(x),
128 nobs = stats::nobs(x),
129 adj.r.squared = s$r.sq,
130 npar = s$np,
131 na_types = "irrrriiri"
132 )
133 }
134
135
136 #' @templateVar class gam
137 #' @template title_desc_augment
138 #'
139 #' @inherit tidy.gam params examples
140 #' @template param_data
141 #' @template param_newdata
142 #' @template param_type_predict
143 #' @template param_type_residuals
144 #'
145 #' @evalRd return_augment(
146 #' ".se.fit",
147 #' ".resid",
148 #' ".hat",
149 #' ".sigma",
150 #' ".cooksd"
151 #' )
152 #'
153 #' @details For additional details on Cook's distance, see
154 #' [stats::cooks.distance()].
155 #'
156 #' @seealso [augment()], [mgcv::gam()]
157 #' @export
158 augment.gam <- function(
159 x,
160 data = model.frame(x),
161 newdata = NULL,
162 type.predict,
163 type.residuals,
164 ...
165 ) {
166 augment_columns(
167 x,
168 data,
169 newdata,
170 type.predict = type.predict,
171 type.residuals = type.residuals
172 )
173 }