Debian Codegraph @

R/mass-polr.R

raw
1 #' @templateVar class polr
2 #' @template title_desc_tidy
3 #'
4 #' @param x A `polr` object returned from [MASS::polr()].
5 #' @template param_confint
6 #' @template param_exponentiate
7 #' @param p.values Logical. Should p-values be returned,
8 #' based on chi-squared tests from [MASS::dropterm()]. Defaults to FALSE.
9 #' @template param_unused_dots
10 #'
11 #' @examplesIf rlang::is_installed("MASS")
12 #'
13 #' # load libraries for models and data
14 #' library(MASS)
15 #'
16 #' # fit model
17 #' fit <- polr(Sat ~ Infl + Type + Cont, weights = Freq, data = housing)
18 #'
19 #' # summarize model fit with tidiers
20 #' tidy(fit, exponentiate = TRUE, conf.int = TRUE)
21 #'
22 #' glance(fit)
23 #' augment(fit, type.predict = "class")
24 #'
25 #' fit2 <- polr(factor(gear) ~ am + mpg + qsec, data = mtcars)
26 #'
27 #' tidy(fit, p.values = TRUE)
28 #'
29 #' @evalRd return_tidy(regression = TRUE)
30 #'
31 #' @details In `broom 0.7.0` the `coefficient_type` column was renamed to
32 #' `coef.type`, and the contents were changed as well. Now the contents
33 #' are `coefficient` and `scale`, rather than `coefficient` and `zeta`.
34 #'
35 #' Calculating p.values with the `dropterm()` function is the approach
36 #' suggested by the MASS package author. This
37 #' approach is computationally intensive so that p.values are only
38 #' returned if requested explicitly. Additionally, it only works for
39 #' models containing no variables with more than two categories. If this
40 #' condition is not met, a message is shown and NA is returned instead of
41 #' p-values.
42 #'
43 #' @aliases polr_tidiers
44 #' @export
45 #' @seealso [tidy], [MASS::polr()]
46 #' @family ordinal tidiers
47 tidy.polr <- function(
48 x,
49 conf.int = FALSE,
50 conf.level = 0.95,
51 exponentiate = FALSE,
52 p.values = FALSE,
53 ...
54 ) {
55 ret <- as_tibble(coef(summary(x)), rownames = "term")
56 colnames(ret) <- c("term", "estimate", "std.error", "statistic")
57
58 if (conf.int) {
59 ci <- broom_confint_terms(x, level = conf.level)
60 ret <- dplyr::left_join(ret, ci, by = "term")
61 }
62
63 if (exponentiate) {
64 ret <- exponentiate(ret)
65 }
66
67 if (p.values) {
68 all_but_last <- function(x) {
69 x[-1]
70 }
71 sig <- MASS::dropterm(x, test = "Chisq")
72 p <- sig |>
73 dplyr::select(`Pr(Chi)`) |>
74 dplyr::pull() |>
75 all_but_last()
76 terms <- purrr::map(rownames(sig)[-1], function(x) {
77 ret$term[stringr::str_detect(ret$term, stringr::fixed(x))]
78 }) |>
79 unlist()
80 if (length(p) == length(terms)) {
81 ret <- dplyr::left_join(
82 ret,
83 tibble::tibble(term = terms, p.value = p),
84 by = "term"
85 )
86 } else {
87 cli::cli_inform(
88 "p-values can presently only be returned for models that contain
89 no categorical variables with more than two levels."
90 )
91 ret$p.value <- NA
92 }
93 }
94
95 mutate(
96 ret,
97 coef.type = if_else(term %in% names(x$zeta), "scale", "coefficient")
98 )
99 }
100
101 #' @templateVar class polr
102 #' @template title_desc_glance
103 #'
104 #' @inherit tidy.polr params examples
105 #'
106 #' @evalRd return_glance(
107 #' "edf",
108 #' "logLik",
109 #' "AIC",
110 #' "BIC",
111 #' "deviance",
112 #' "df.residual",
113 #' "nobs"
114 #' )
115 #'
116 #' @export
117 #' @seealso [tidy], [MASS::polr()]
118 #' @family ordinal tidiers
119 glance.polr <- function(x, ...) {
120 as_glance_tibble(
121 edf = x$edf,
122 logLik = as.numeric(stats::logLik(x)),
123 AIC = stats::AIC(x),
124 BIC = stats::BIC(x),
125 deviance = stats::deviance(x),
126 df.residual = stats::df.residual(x),
127 nobs = stats::nobs(x),
128 na_types = "irrrrii"
129 )
130 }
131
132 #' @templateVar class polr
133 #' @template title_desc_augment
134 #'
135 #' @inherit tidy.polr params examples
136 #' @template param_data
137 #' @template param_newdata
138 #'
139 #' @param type.predict Which type of prediction to compute,
140 #' passed to `MASS:::predict.polr()`. Only supports `"class"` at
141 #' the moment.
142 #'
143 #' @export
144 #' @seealso [tidy()], [MASS::polr()]
145 #' @family ordinal tidiers
146 #'
147 augment.polr <- function(
148 x,
149 data = model.frame(x),
150 newdata = NULL,
151 type.predict = c("class"),
152 ...
153 ) {
154 type <- rlang::arg_match(type.predict)
155
156 df <- if (is.null(newdata)) data else newdata
157 df <- as_augment_tibble(df)
158
159 df$.fitted <- predict(object = x, newdata = df, type = type)
160
161 df
162 }