Debian Codegraph @

R/survey.R

raw
1 #' @templateVar class svyolr
2 #' @template title_desc_tidy
3 #'
4 #' @param x A `svyolr` object returned from [survey::svyolr()].
5 #' @template param_confint
6 #' @template param_exponentiate
7 #' @template param_unused_dots
8 #'
9 #' @details
10 #'
11 #' The `tidy.svyolr()` tidier is a light wrapper around
12 #' [tidy.polr()]. However, the implementation for p-value calculation
13 #' in [tidy.polr()] is both computationally intensive and specific to that
14 #' model, so the `p.values` argument to `tidy.svyolr()` is currently ignored,
15 #' and will raise a warning when passed.
16 #'
17 #' @examplesIf rlang::is_installed(c("survey", "MASS"))
18 #' library(broom)
19 #' library(survey)
20 #'
21 #' data(api)
22 #' dclus1 <- svydesign(id = ~dnum, weights = ~pw, data = apiclus1, fpc = ~fpc)
23 #' dclus1 <- update(dclus1, mealcat = cut(meals, c(0, 25, 50, 75, 100)))
24 #'
25 #' m <- svyolr(mealcat ~ avg.ed + mobility + stype, design = dclus1)
26 #'
27 #' m
28 #'
29 #' tidy(m, conf.int = TRUE)
30 #'
31 #' @export
32 #'
33 #' @evalRd return_tidy(regression = TRUE)
34 #'
35 #' @aliases svyolr_tidiers
36 #' @export
37 #' @seealso [tidy], [survey::svyolr()]
38 #' @family ordinal tidiers
39 tidy.svyolr <- function(
40 x,
41 conf.int = FALSE,
42 conf.level = 0.95,
43 exponentiate = FALSE,
44 ...
45 ) {
46 check_ellipses("p.values", "tidy", "svyolr", ...)
47
48 return(
49 tidy.polr(
50 x,
51 conf.int = conf.int,
52 conf.level = conf.level,
53 exponentiate = exponentiate
54 )
55 )
56 }
57
58 #' @templateVar class svyolr
59 #' @template title_desc_glance
60 #'
61 #' @inherit tidy.svyolr params examples
62 #'
63 #' @evalRd return_glance(
64 #' "edf",
65 #' "df.residual",
66 #' "nobs"
67 #' )
68 #'
69 #' @export
70 #' @seealso [tidy], [survey::svyolr()]
71 #' @family ordinal tidiers
72 glance.svyolr <- function(x, ...) {
73 tibble(
74 edf = x$edf,
75 df.residual = stats::df.residual(x),
76 nobs = stats::nobs(x)
77 )
78 }
79
80 #' @templateVar class svyglm
81 #' @template title_desc_tidy
82 #'
83 #' @param x A `svyglm` object returned from [survey::svyglm()].
84 #' @template param_confint
85 #' @template param_exponentiate
86 #' @template param_unused_dots
87 #'
88 #' @export
89 #' @family survey tidiers
90 #' @seealso [survey::svyglm()], [stats::glm()]
91 tidy.svyglm <- function(
92 x,
93 conf.int = FALSE,
94 conf.level = 0.95,
95 exponentiate = FALSE,
96 ...
97 ) {
98 ret <- as_tibble(summary(x)$coefficients, rownames = "term")
99 colnames(ret) <- c("term", "estimate", "std.error", "statistic", "p.value")
100
101 # summary(x)$coefficients misses rank deficient rows (i.e. coefs that
102 # summary.lm() sets to NA), catch them here and add them back
103
104 coefs <- tibble::enframe(stats::coef(x), name = "term", value = "estimate")
105 ret <- left_join(coefs, ret, by = c("term", "estimate"))
106
107 if (conf.int) {
108 ci <- broom_confint_terms(x, level = conf.level, ...)
109 ret <- dplyr::left_join(ret, ci, by = "term")
110 }
111
112 if (exponentiate) {
113 ret <- exponentiate(ret)
114 }
115
116 ret
117 }
118
119 #' @templateVar class svyglm
120 #' @template title_desc_glance
121 #'
122 #' @param x A `svyglm` object returned from [survey::svyglm()].
123 #' @param maximal A `svyglm` object corresponding to the maximal
124 #' model against which to compute the BIC. See Lumley and Scott
125 #' (2015) for details. Defaults to `x`, which is equivalent
126 #' to not using a maximal model.
127 #'
128 #' @template param_unused_dots
129 #'
130 #' @evalRd return_glance(
131 #' "null.deviance",
132 #' "df.null",
133 #' "AIC",
134 #' "BIC",
135 #' "deviance",
136 #' "df.residual"
137 #' )
138 #'
139 #' @examplesIf rlang::is_installed("survey")
140 #'
141 #' # load libraries for models and data
142 #' library(survey)
143 #'
144 #' set.seed(123)
145 #' data(api)
146 #'
147 #' # survey design
148 #' dstrat <-
149 #' svydesign(
150 #' id = ~1,
151 #' strata = ~stype,
152 #' weights = ~pw,
153 #' data = apistrat,
154 #' fpc = ~fpc
155 #' )
156 #'
157 #' # model
158 #' m <- svyglm(
159 #' formula = sch.wide ~ ell + meals + mobility,
160 #' design = dstrat,
161 #' family = quasibinomial()
162 #' )
163 #'
164 #' glance(m)
165 #'
166 #' @references Lumley T, Scott A (2015). AIC and BIC for modelling with complex
167 #' survey data. *Journal of Survey Statistics and Methodology*, 3(1).
168 #'
169 #' @export
170 #' @family lm tidiers
171 #' @seealso [survey::svyglm()], [stats::glm()], [survey::anova.svyglm]
172 glance.svyglm <- function(x, maximal = x, ...) {
173 # NOTES:
174 #
175 # (1) log-likelihood does not apply (returns deviance instead)
176 # logLik.svyglm() should result in a warning:
177 #
178 # > Warning in logLik.svyglm(x) : svyglm not fitted by maximum likelihood.
179 #
180 # (2) AIC is not always directly computed by svyglm,
181 # e.g. if family = quasibinomial()
182
183 as_glance_tibble(
184 null.deviance = x$null.deviance,
185 df.null = x$df.null,
186 AIC = stats::AIC(x)["AIC"],
187 BIC = stats::BIC(x, maximal = maximal)["BIC"],
188 deviance = x$deviance,
189 df.residual = x$df.residual,
190 nobs = stats::nobs(x),
191 na_types = "rirrrii"
192 )
193 }