| 1 |
% Generated by roxygen2: do not edit by hand |
| 2 |
% Please edit documentation in R/polca.R |
| 3 |
\name{augment.poLCA} |
| 4 |
\alias{augment.poLCA} |
| 5 |
\title{Augment data with information from a(n) poLCA object} |
| 6 |
\usage{ |
| 7 |
\method{augment}{poLCA}(x, data = NULL, ...) |
| 8 |
} |
| 9 |
\arguments{ |
| 10 |
\item{x}{A \code{poLCA} object returned from \code{\link[poLCA:poLCA]{poLCA::poLCA()}}.} |
| 11 |
|
| 12 |
\item{data}{A \link[base:data.frame]{base::data.frame} or \code{\link[tibble:tibble]{tibble::tibble()}} containing the original |
| 13 |
data that was used to produce the object \code{x}. Defaults to |
| 14 |
\code{stats::model.frame(x)} so that \code{augment(my_fit)} returns the augmented |
| 15 |
original data. \strong{Do not} pass new data to the \code{data} argument. |
| 16 |
Augment will report information such as influence and cooks distance for |
| 17 |
data passed to the \code{data} argument. These measures are only defined for |
| 18 |
the original training data.} |
| 19 |
|
| 20 |
\item{...}{Additional arguments. Not used. Needed to match generic |
| 21 |
signature only. \strong{Cautionary note:} Misspelled arguments will be |
| 22 |
absorbed in \code{...}, where they will be ignored. If the misspelled |
| 23 |
argument has a default value, the default value will be used. |
| 24 |
For example, if you pass \code{conf.lvel = 0.9}, all computation will |
| 25 |
proceed using \code{conf.level = 0.95}. Two exceptions here are: |
| 26 |
\itemize{ |
| 27 |
\item \code{tidy()} methods will warn when supplied an \code{exponentiate} argument if |
| 28 |
it will be ignored. |
| 29 |
\item \code{augment()} methods will warn when supplied a \code{newdata} argument if it |
| 30 |
will be ignored. |
| 31 |
}} |
| 32 |
} |
| 33 |
\description{ |
| 34 |
Augment accepts a model object and a dataset and adds |
| 35 |
information about each observation in the dataset. Most commonly, this |
| 36 |
includes predicted values in the \code{.fitted} column, residuals in the |
| 37 |
\code{.resid} column, and standard errors for the fitted values in a \code{.se.fit} |
| 38 |
column. New columns always begin with a \code{.} prefix to avoid overwriting |
| 39 |
columns in the original dataset. |
| 40 |
|
| 41 |
Users may pass data to augment via either the \code{data} argument or the |
| 42 |
\code{newdata} argument. If the user passes data to the \code{data} argument, |
| 43 |
it \strong{must} be exactly the data that was used to fit the model |
| 44 |
object. Pass datasets to \code{newdata} to augment data that was not used |
| 45 |
during model fitting. This still requires that at least all predictor |
| 46 |
variable columns used to fit the model are present. If the original outcome |
| 47 |
variable used to fit the model is not included in \code{newdata}, then no |
| 48 |
\code{.resid} column will be included in the output. |
| 49 |
|
| 50 |
Augment will often behave differently depending on whether \code{data} or |
| 51 |
\code{newdata} is given. This is because there is often information |
| 52 |
associated with training observations (such as influences or related) |
| 53 |
measures that is not meaningfully defined for new observations. |
| 54 |
|
| 55 |
For convenience, many augment methods provide default \code{data} arguments, |
| 56 |
so that \code{augment(fit)} will return the augmented training data. In these |
| 57 |
cases, augment tries to reconstruct the original data based on the model |
| 58 |
object with varying degrees of success. |
| 59 |
|
| 60 |
The augmented dataset is always returned as a \link[tibble:tibble]{tibble::tibble} with the |
| 61 |
\strong{same number of rows} as the passed dataset. This means that the passed |
| 62 |
data must be coercible to a tibble. If a predictor enters the model as part |
| 63 |
of a matrix of covariates, such as when the model formula uses |
| 64 |
\code{\link[splines:ns]{splines::ns()}}, \code{\link[stats:poly]{stats::poly()}}, or \code{\link[survival:Surv]{survival::Surv()}}, it is represented |
| 65 |
as a matrix column. |
| 66 |
|
| 67 |
We are in the process of defining behaviors for models fit with various |
| 68 |
\code{na.action} arguments, but make no guarantees about behavior when data is |
| 69 |
missing at this time. |
| 70 |
} |
| 71 |
\details{ |
| 72 |
If the \code{data} argument is given, those columns are included in |
| 73 |
the output (only rows for which predictions could be made). |
| 74 |
Otherwise, the \code{y} element of the poLCA object, which contains the |
| 75 |
manifest variables used to fit the model, are used, along with any |
| 76 |
covariates, if present, in \code{x}. |
| 77 |
|
| 78 |
Note that while the probability of all the classes (not just the predicted |
| 79 |
modal class) can be found in the \code{posterior} element, these are not |
| 80 |
included in the augmented output. |
| 81 |
} |
| 82 |
\examples{ |
| 83 |
\dontshow{if (rlang::is_installed(c("poLCA", "ggplot2"))) withAutoprint(\{ # examplesIf} |
| 84 |
|
| 85 |
# load libraries for models and data |
| 86 |
library(poLCA) |
| 87 |
library(dplyr) |
| 88 |
|
| 89 |
# generate data |
| 90 |
data(values) |
| 91 |
|
| 92 |
f <- cbind(A, B, C, D) ~ 1 |
| 93 |
|
| 94 |
# fit model |
| 95 |
M1 <- poLCA(f, values, nclass = 2, verbose = FALSE) |
| 96 |
|
| 97 |
M1 |
| 98 |
|
| 99 |
# summarize model fit with tidiers + visualization |
| 100 |
tidy(M1) |
| 101 |
augment(M1) |
| 102 |
glance(M1) |
| 103 |
|
| 104 |
library(ggplot2) |
| 105 |
|
| 106 |
ggplot(tidy(M1), aes(factor(class), estimate, fill = factor(outcome))) + |
| 107 |
geom_bar(stat = "identity", width = 1) + |
| 108 |
facet_wrap(~variable) |
| 109 |
|
| 110 |
# three-class model with a single covariate. |
| 111 |
data(election) |
| 112 |
|
| 113 |
f2a <- cbind( |
| 114 |
MORALG, CARESG, KNOWG, LEADG, DISHONG, INTELG, |
| 115 |
MORALB, CARESB, KNOWB, LEADB, DISHONB, INTELB |
| 116 |
) ~ PARTY |
| 117 |
|
| 118 |
nes2a <- poLCA(f2a, election, nclass = 3, nrep = 5, verbose = FALSE) |
| 119 |
|
| 120 |
td <- tidy(nes2a) |
| 121 |
td |
| 122 |
|
| 123 |
ggplot(td, aes(outcome, estimate, color = factor(class), group = class)) + |
| 124 |
geom_line() + |
| 125 |
facet_wrap(~variable, nrow = 2) + |
| 126 |
theme(axis.text.x = element_text(angle = 90, hjust = 1)) |
| 127 |
|
| 128 |
au <- augment(nes2a) |
| 129 |
|
| 130 |
au |
| 131 |
|
| 132 |
count(au, .class) |
| 133 |
|
| 134 |
# if the original data is provided, it leads to NAs in new columns |
| 135 |
# for rows that weren't predicted |
| 136 |
au2 <- augment(nes2a, data = election) |
| 137 |
|
| 138 |
au2 |
| 139 |
|
| 140 |
dim(au2) |
| 141 |
\dontshow{\}) # examplesIf} |
| 142 |
} |
| 143 |
\seealso{ |
| 144 |
\code{\link[=augment]{augment()}}, \code{\link[poLCA:poLCA]{poLCA::poLCA()}} |
| 145 |
|
| 146 |
Other poLCA tidiers: |
| 147 |
\code{\link{glance.poLCA}()}, |
| 148 |
\code{\link{tidy.poLCA}()} |
| 149 |
} |
| 150 |
\concept{poLCA tidiers} |
| 151 |
\value{ |
| 152 |
A \code{\link[tibble:tibble]{tibble::tibble()}} with columns: |
| 153 |
\item{.class}{Predicted class.} |
| 154 |
\item{.probability}{Class probability of modal class.} |
| 155 |
|
| 156 |
} |