| 1 |
#' @templateVar class poLCA |
| 2 |
#' @template title_desc_tidy |
| 3 |
#' |
| 4 |
#' @param x A `poLCA` object returned from [poLCA::poLCA()]. |
| 5 |
#' @template param_unused_dots |
| 6 |
#' |
| 7 |
#' @evalRd return_tidy( |
| 8 |
#' variable = "Manifest variable", |
| 9 |
#' "class", |
| 10 |
#' "outcome", |
| 11 |
#' estimate = "Estimated class-conditional response probability", |
| 12 |
#' "std.error" |
| 13 |
#' ) |
| 14 |
#' |
| 15 |
#' @examplesIf rlang::is_installed(c("poLCA", "ggplot2")) |
| 16 |
#' |
| 17 |
#' # load libraries for models and data |
| 18 |
#' library(poLCA) |
| 19 |
#' library(dplyr) |
| 20 |
#' |
| 21 |
#' # generate data |
| 22 |
#' data(values) |
| 23 |
#' |
| 24 |
#' f <- cbind(A, B, C, D) ~ 1 |
| 25 |
#' |
| 26 |
#' # fit model |
| 27 |
#' M1 <- poLCA(f, values, nclass = 2, verbose = FALSE) |
| 28 |
#' |
| 29 |
#' M1 |
| 30 |
#' |
| 31 |
#' # summarize model fit with tidiers + visualization |
| 32 |
#' tidy(M1) |
| 33 |
#' augment(M1) |
| 34 |
#' glance(M1) |
| 35 |
#' |
| 36 |
#' library(ggplot2) |
| 37 |
#' |
| 38 |
#' ggplot(tidy(M1), aes(factor(class), estimate, fill = factor(outcome))) + |
| 39 |
#' geom_bar(stat = "identity", width = 1) + |
| 40 |
#' facet_wrap(~variable) |
| 41 |
#' |
| 42 |
#' # three-class model with a single covariate. |
| 43 |
#' data(election) |
| 44 |
#' |
| 45 |
#' f2a <- cbind( |
| 46 |
#' MORALG, CARESG, KNOWG, LEADG, DISHONG, INTELG, |
| 47 |
#' MORALB, CARESB, KNOWB, LEADB, DISHONB, INTELB |
| 48 |
#' ) ~ PARTY |
| 49 |
#' |
| 50 |
#' nes2a <- poLCA(f2a, election, nclass = 3, nrep = 5, verbose = FALSE) |
| 51 |
#' |
| 52 |
#' td <- tidy(nes2a) |
| 53 |
#' td |
| 54 |
#' |
| 55 |
#' ggplot(td, aes(outcome, estimate, color = factor(class), group = class)) + |
| 56 |
#' geom_line() + |
| 57 |
#' facet_wrap(~variable, nrow = 2) + |
| 58 |
#' theme(axis.text.x = element_text(angle = 90, hjust = 1)) |
| 59 |
#' |
| 60 |
#' au <- augment(nes2a) |
| 61 |
#' |
| 62 |
#' au |
| 63 |
#' |
| 64 |
#' count(au, .class) |
| 65 |
#' |
| 66 |
#' # if the original data is provided, it leads to NAs in new columns |
| 67 |
#' # for rows that weren't predicted |
| 68 |
#' au2 <- augment(nes2a, data = election) |
| 69 |
#' |
| 70 |
#' au2 |
| 71 |
#' |
| 72 |
#' dim(au2) |
| 73 |
#' |
| 74 |
#' @aliases poLCA_tidiers |
| 75 |
#' @export |
| 76 |
#' @seealso [tidy()], [poLCA::poLCA()] |
| 77 |
#' @family poLCA tidiers |
| 78 |
#' |
| 79 |
tidy.poLCA <- function(x, ...) { |
| 80 |
probs <- purrr::map2_df(x$probs, names(x$probs), reshape_probs) |> |
| 81 |
mutate(variable = as.character(variable)) |> |
| 82 |
transmute( |
| 83 |
variable, |
| 84 |
class = stringr::str_match(Var1, "class (.*):")[, 2], |
| 85 |
outcome = Var2, |
| 86 |
estimate = value |
| 87 |
) |
| 88 |
|
| 89 |
if (all(stringr::str_detect(probs$outcome, "^Pr\\(\\d*\\)$"))) { |
| 90 |
probs$outcome <- as.numeric(stringr::str_match( |
| 91 |
probs$outcome, |
| 92 |
"Pr\\((\\d*)\\)" |
| 93 |
)[, 2]) |
| 94 |
} |
| 95 |
|
| 96 |
probs <- probs |> |
| 97 |
mutate(class = utils::type.convert(class, as.is = TRUE)) |
| 98 |
|
| 99 |
probs_se <- purrr::map2_df(x$probs.se, names(x$probs.se), reshape_probs) |> |
| 100 |
mutate(variable = as.character(variable)) |> |
| 101 |
mutate_if(is.factor, as.integer) |
| 102 |
|
| 103 |
probs$std.error <- probs_se$value |
| 104 |
|
| 105 |
as_tibble(probs) |
| 106 |
} |
| 107 |
|
| 108 |
#' @templateVar class poLCA |
| 109 |
#' @template title_desc_augment |
| 110 |
#' |
| 111 |
#' @inherit tidy.poLCA params examples |
| 112 |
#' @template param_data |
| 113 |
#' |
| 114 |
#' @evalRd return_augment( |
| 115 |
#' .fitted = FALSE, |
| 116 |
#' .resid = FALSE, |
| 117 |
#' ".class", |
| 118 |
#' ".probability" |
| 119 |
#' ) |
| 120 |
#' |
| 121 |
#' @details If the `data` argument is given, those columns are included in |
| 122 |
#' the output (only rows for which predictions could be made). |
| 123 |
#' Otherwise, the `y` element of the poLCA object, which contains the |
| 124 |
#' manifest variables used to fit the model, are used, along with any |
| 125 |
#' covariates, if present, in `x`. |
| 126 |
#' |
| 127 |
#' Note that while the probability of all the classes (not just the predicted |
| 128 |
#' modal class) can be found in the `posterior` element, these are not |
| 129 |
#' included in the augmented output. |
| 130 |
#' |
| 131 |
#' @export |
| 132 |
#' @seealso [augment()], [poLCA::poLCA()] |
| 133 |
#' @family poLCA tidiers |
| 134 |
augment.poLCA <- function(x, data = NULL, ...) { |
| 135 |
check_ellipses("newdata", "augment", "poLCA", ...) |
| 136 |
|
| 137 |
indices <- cbind(seq_len(nrow(x$posterior)), x$predclass) |
| 138 |
|
| 139 |
ret <- tibble( |
| 140 |
.class = x$predclass, |
| 141 |
.probability = x$posterior[indices] |
| 142 |
) |
| 143 |
|
| 144 |
if (is.null(data)) { |
| 145 |
data <- bind_cols(x$y, x$x) |
| 146 |
} else { |
| 147 |
if (nrow(data) != nrow(ret)) { |
| 148 |
# Rows may have been removed for NAs. For those rows, the new columns NAs |
| 149 |
ret$.rownames <- rownames(x$y) |
| 150 |
ret <- ret[rownames(data), ] |
| 151 |
} |
| 152 |
} |
| 153 |
|
| 154 |
as_tibble(bind_cols(data, ret)) |
| 155 |
} |
| 156 |
|
| 157 |
#' @templateVar class poLCA |
| 158 |
#' @template title_desc_glance |
| 159 |
#' |
| 160 |
#' @inherit tidy.poLCA params examples |
| 161 |
#' |
| 162 |
#' @evalRd return_glance( |
| 163 |
#' "logLik", |
| 164 |
#' "AIC", |
| 165 |
#' "BIC", |
| 166 |
#' "chi.squared", |
| 167 |
#' "df", |
| 168 |
#' "df.residual", |
| 169 |
#' g.squared = "The likelihood ratio/deviance statistic", |
| 170 |
#' "nobs" |
| 171 |
#' ) |
| 172 |
#' |
| 173 |
#' @export |
| 174 |
#' @seealso [glance()], [poLCA::poLCA()] |
| 175 |
#' @family poLCA tidiers |
| 176 |
glance.poLCA <- function(x, ...) { |
| 177 |
as_glance_tibble( |
| 178 |
logLik = x$llik, |
| 179 |
AIC = x$aic, |
| 180 |
BIC = x$bic, |
| 181 |
g.squared = x$Gsq, |
| 182 |
chi.squared = x$Chisq, |
| 183 |
df = x$npar, |
| 184 |
df.residual = x$resid.df, |
| 185 |
nobs = stats::nobs(x), |
| 186 |
na_types = "rrrrriii" |
| 187 |
) |
| 188 |
} |
| 189 |
|
| 190 |
|
| 191 |
# a function to reshape each element of the probs and probs.se lists |
| 192 |
reshape_probs <- function(x_, x_name) { |
| 193 |
res <- as.data.frame.table(x_, responseName = "value") |
| 194 |
cbind(variable = x_name, res) |
| 195 |
} |