| 1 |
#' @templateVar class clm |
| 2 |
#' @template title_desc_tidy |
| 3 |
#' |
| 4 |
#' @param x A `clm` object returned from [ordinal::clm()]. |
| 5 |
#' @template param_confint |
| 6 |
#' @template param_exponentiate |
| 7 |
#' @param conf.type Whether to use `"profile"` or `"Wald"` confidence |
| 8 |
#' intervals, passed to the `type` argument of [ordinal::confint.clm()]. |
| 9 |
#' Defaults to `"profile"`. |
| 10 |
#' @template param_unused_dots |
| 11 |
#' |
| 12 |
#' @examplesIf rlang::is_installed("ordinal") |
| 13 |
#' |
| 14 |
#' # load libraries for models and data |
| 15 |
#' library(ordinal) |
| 16 |
#' |
| 17 |
#' # fit model |
| 18 |
#' fit <- clm(rating ~ temp * contact, data = wine) |
| 19 |
#' |
| 20 |
#' # summarize model fit with tidiers |
| 21 |
#' tidy(fit) |
| 22 |
#' tidy(fit, conf.int = TRUE, conf.level = 0.9) |
| 23 |
#' tidy(fit, conf.int = TRUE, conf.type = "Wald", exponentiate = TRUE) |
| 24 |
#' |
| 25 |
#' glance(fit) |
| 26 |
#' augment(fit, type.predict = "prob") |
| 27 |
#' augment(fit, type.predict = "class") |
| 28 |
#' |
| 29 |
#' # ...and again with another model specification |
| 30 |
#' fit2 <- clm(rating ~ temp, nominal = ~contact, data = wine) |
| 31 |
#' |
| 32 |
#' tidy(fit2) |
| 33 |
#' glance(fit2) |
| 34 |
#' |
| 35 |
#' @evalRd return_tidy(regression = TRUE) |
| 36 |
#' |
| 37 |
#' @details In `broom 0.7.0` the `coefficient_type` column was renamed to |
| 38 |
#' `coef.type`, and the contents were changed as well. |
| 39 |
#' |
| 40 |
#' Note that `intercept` type coefficients correspond to `alpha` |
| 41 |
#' parameters, `location` type coefficients correspond to `beta` |
| 42 |
#' parameters, and `scale` type coefficients correspond to `zeta` |
| 43 |
#' parameters. |
| 44 |
#' |
| 45 |
#' @aliases ordinal_tidiers |
| 46 |
#' @export |
| 47 |
#' @seealso [tidy], [ordinal::clm()], [ordinal::confint.clm()] |
| 48 |
#' @family ordinal tidiers |
| 49 |
tidy.clm <- function( |
| 50 |
x, |
| 51 |
conf.int = FALSE, |
| 52 |
conf.level = 0.95, |
| 53 |
conf.type = c("profile", "Wald"), |
| 54 |
exponentiate = FALSE, |
| 55 |
... |
| 56 |
) { |
| 57 |
conf.type <- rlang::arg_match(conf.type) |
| 58 |
ret <- as_tibble(coef(summary(x)), rownames = "term") |
| 59 |
colnames(ret) <- c("term", "estimate", "std.error", "statistic", "p.value") |
| 60 |
|
| 61 |
if (conf.int) { |
| 62 |
ci <- broom_confint_terms(x, level = conf.level, type = conf.type) |
| 63 |
ret <- dplyr::left_join(ret, ci, by = "term") |
| 64 |
} |
| 65 |
|
| 66 |
if (exponentiate) { |
| 67 |
ret <- exponentiate(ret) |
| 68 |
} |
| 69 |
|
| 70 |
types <- c("alpha", "beta", "zeta") |
| 71 |
new_types <- c("intercept", "location", "scale") |
| 72 |
ret$coef.type <- rep(new_types, vapply(x[types], length, numeric(1))) |
| 73 |
as_tibble(ret) |
| 74 |
} |
| 75 |
|
| 76 |
#' @templateVar class clm |
| 77 |
#' @template title_desc_glance |
| 78 |
#' |
| 79 |
#' @inherit tidy.clm params examples |
| 80 |
#' |
| 81 |
#' @evalRd return_glance( |
| 82 |
#' "edf", |
| 83 |
#' "AIC", |
| 84 |
#' "BIC", |
| 85 |
#' "logLik", |
| 86 |
#' "df.residual", |
| 87 |
#' "nobs" |
| 88 |
#' ) |
| 89 |
#' |
| 90 |
#' @export |
| 91 |
#' @seealso [tidy], [ordinal::clm()] |
| 92 |
#' @family ordinal tidiers |
| 93 |
glance.clm <- function(x, ...) { |
| 94 |
as_glance_tibble( |
| 95 |
edf = x$edf, |
| 96 |
AIC = stats::AIC(x), |
| 97 |
BIC = stats::BIC(x), |
| 98 |
logLik = stats::logLik(x), |
| 99 |
df.residual = stats::df.residual(x), |
| 100 |
nobs = stats::nobs(x), |
| 101 |
na_types = "irrrii" |
| 102 |
) |
| 103 |
} |
| 104 |
|
| 105 |
#' @templateVar class clm |
| 106 |
#' @template title_desc_augment |
| 107 |
#' |
| 108 |
#' @inherit tidy.clm params examples |
| 109 |
#' @template param_data |
| 110 |
#' @template param_newdata |
| 111 |
#' |
| 112 |
#' @param type.predict Which type of prediction to compute, either `"prob"` |
| 113 |
#' or `"class"`, passed to [ordinal::predict.clm()]. Defaults to `"prob"`. |
| 114 |
#' |
| 115 |
#' @export |
| 116 |
#' @seealso [tidy], [ordinal::clm()], [ordinal::predict.clm()] |
| 117 |
#' @family ordinal tidiers |
| 118 |
#' |
| 119 |
augment.clm <- function( |
| 120 |
x, |
| 121 |
data = model.frame(x), |
| 122 |
newdata = NULL, |
| 123 |
type.predict = c("prob", "class"), |
| 124 |
... |
| 125 |
) { |
| 126 |
type.predict <- rlang::arg_match(type.predict) |
| 127 |
|
| 128 |
df <- if (is.null(newdata)) data else newdata |
| 129 |
df <- as_augment_tibble(df) |
| 130 |
|
| 131 |
df$.fitted <- predict(object = x, newdata = df, type = type.predict)$fit |
| 132 |
|
| 133 |
df |
| 134 |
} |