lca {lcca} | R Documentation |
Fits a latent-class model to a set of categorical items using an EM algorithm.
lca( formula, data, freq, groups, nclass = 2, constrain.rhos = F, constrain.gammas = F, iseeds = NULL, iter.max = 5000, tol = 1e-06, starting.values = NULL, flatten.rhos = 0, flatten.gammas = 0, se.method = "STANDARD", weights, clusters, strata, subpop)
formula |
an object of class |
data |
an optional data frame, list or environment containing
the variables in the model. If |
freq |
optional frequency or count variable, to be used when data are aggregated. |
groups |
optional categorical grouping variable for multi-group analysis; see DETAILS. |
nclass |
number of latent classes to be fit. Response items are assumed to be conditionally independent within classes. |
constrain.rhos |
if |
constrain.gammas |
if |
iseeds |
two integers to initialize the random number generator; see DETAILS. |
iter.max |
maximum number of iterations to be performed. Each iteration consists of an Expectation or E-step followed by a Maximization or M-step. The procedure halts if it has not converged by this many iterations. |
tol |
convergence criterion. The procedure halts if the maximum absolute change in all parameters (gammas and rhos) from one iteration to the next falls below this value. |
starting.values |
optional starting values for the model
parameters. This must be a list with two named components,
|
flatten.rhos |
optional flattening constant for estimation of rho-paramaters (item-response probabilities); see DETAILS. |
flatten.gammas |
optional flattening constant for estimation of gamma-parameters (class prevalences); see DETAILS. |
se.method |
method to use for computing standard errors; see DETAILS. |
weights |
optional numeric variable containing sampling weights for survey data; see DETAILS. |
clusters |
optional integer or factor variable containing sampling cluster identifiers; see DETAILS. |
strata |
optional integer or factor variable containing sampling stratum identifiers; see DETAILS. |
subpop |
optional logical variable indicating a subpopulation to which the model is to be fit; see DETAILS. |
This function fits a standard latent-class model without covariates.
Under this model, polytomous response variables are assumed to be
conditionally independent given a latent classification variable.
For models with covariates, use lcacov
.
The formula
argument should have the form
cbind(Y1,Y2,...)~1
. The term on the left-hand side of ~
is a matrix (not a data frame) of variables to be
used as responses. Each response variable should consist of integer
codes 1,2,...
. Response variables may
also be factors, in which case they will be automatically converted to
integer codes (as in the function unclass
), and the
levels
of the factors will be ignored. The right-hand side of
formula
should
be 1
, indicating that the only predictor is a constant; any
other predictors in the model formula will be ignored.
Missing values in response variables are allowed and should be
conveyed by the R missing value code NA
. Cases with missing
responses are retained in the fitting procedure, and the missing
values are assumed to be ignorably missing or missing at random.
The number of latent classes to be fit is determined by nclass
,
with nclass=1
indicating that the response variables are
jointly independent.
By default, each case (row) of data
or the model environment is
assumed to represent one observational unit or individual. Data may
also be aggegated, with individuals bearing identical responses to all
variables (including NA
's, if present) collapsed into a single
case, with frequencies conveyed through the numeric variable
freq
.
The unknown parameters to be estimated are the class prevalences, which we call gamma's and item-response probabilities, which we call rho's. Estimation proceeds by an EM algorithm which, by default, computes maximum-likelihood estimates of these parameters. EM may converge to a global or local maximum, possibly on the boundary of the parameter space where one or more gamma- or rho-parameters are zero.
This function supports multi-group analyses in which the
gamma- or rho-parameters are allowed to vary across levels of a
categorical variable groups
. This feature is useful
for testing invariance of measurement (i.e., equality of
rho-paraneters) across groups. The groups
variable, if present,
should be integers coded as 1,2,...,ngroups
, where
ngroups
is the number of distinct groups. The groups
variable may also be a factor, and the levels
of this factor
will appear in the printed output summaries. If groups
is not
present, then ngroups
is taken to be 1
. Constraints on
the group-specific parameters are conveyed by constrain.rhos
and constrain.gammas
.
Optional starting values for parameters are provided through
starting.values
. This argument should be a list with two named
components, rho
and gamma
. The component rho
should be an array of dimension
c(nitems,maxlevs,nclass,ngroups)
, where nitems
is the
number of response variables on the left-hand side of formula
,
maxlevs
is the maximum number of levels (distinct response
categories) among the response variables, nclass
is the number
of latent classes, and ngroups
is the number of groups (equal
to 1
if groups
is not supplied). The element
starting.values$rho[j,k,c,g]
is the probability that an
individual in group g
and class c
supplies a response of
k
to item j
. The component gamma
should be a
matrix with nclass
rows and ngroups
columns, with
starting.values$gamma[c,g]
containing the prevalence of class
c
within group g
.
If starting.values
is not supplied, or if
starting.values$rho=NULL
, then starting values will be randomly
generated. This function uses its own internal random number generator
which is seeded by two integers, for example, seeds=c(123,456)
,
which allows results to be reproduced in the future. If
seeds=NULL
then the function will seed itself with two random
integers from R. Therefore, results can also be made reproducible by
calling set.seed
beforehand and taking
seeds=NULL
. Different starting values for the rhos's may lead
to solutions in which the classes have different orderings. To
reorder the classes in the printed output summaries, use the function
permute.class
. If starting.values
is not
supplied, or if starting.values$gamma=NULL
, then starting
values for the gamma's will be uniform across the classes within each
group.
Rho-parameters at or near a boundary, which are commonplace in
latent-class analysis, create difficulty when computing
derivative-based standard errors, because the likelihood function at
the solution may not be log-concave. The argument flatten.rhos
allows the user to supply a positive flattening constant to smooth the
estimated rho's toward the interior of the parameter space. A value
of flatten.rhos=1
, which should be adequate in most cases,
supplies information equivalent to one prior observation for each
response item in each class and each group, distributed fractionally
across the response categories in equal amounts. Estimated
gamma-parameters at or near zero are also problematic, because
rho-parameters for an empty group are not identified. A value of
flatten.gammas=1
, which should be adequate for most purposes,
adds information equivalent to one prior observation in each group,
distributed fractionally across the classes in equal amounts.
Starting values that are poor in the sense that they are far away from the
global maximum (and random starting values are often poor) may lead to a
local maximum or a sub-optimal solution on the boundary, and supplying
small flattening constants for the rho's and the gamma's often helps
the EM algorithm to find a better solution.
Acceptable values for the argument se.method
are
"STANDARD"
,"FAST"
, "SANDWICH"
or "NONE"
.
If "STANDARD"
, then standard errors are obtained by inverting the
matrix of (minus one times) the second derivatives of the loglikelihood
function (plus penalty terms for flattening constants, if present) at
the solution. If "FAST"
, then the matrix of second derivatives
is approximated by the sum of the outer products of individuals'
contributions to the score functions (first derivatives). If
"SANDWICH"
, then the sum of the score outer-products is pre-
and post-multiplied by the inverse of the second derivatives. If
"NONE"
, then computation of first- and second-derivatives and
standard errors is suppressed.
Survey weights may be supplied via the argument weights
.
Weights should not be confused with frequencies as supplied by
freq
. A frequency of 10
indicates that ten individuals
in the sample exhibited the given pattern of responses, but a survey
weight of 10
indicates that one sampled individual is
representing ten individuals in the population. The same variable
supplied as freq
or weights
will lead to identical
estimates, but the standard errors may be drastically different. You
cannot supply both freq
and weights
; data from a survey
with unequal probabilities of selection must be supplied in
disaggregated form. If a weights
variable is present, then
se.method
is automatically set to "SANDWICH"
, and the
inner matrix of the sandich formula (i.e., the meat of the sandwich)
is an estimated covariance matrix for the total quasi-score.
Weights provided with large, nationally-representative survey datasets
are often very large, because their sum estimates the size of the
population. Many data analysts are accustomed to rescaling survey
weights to have a mean of one, so that they sum to the sample size
rather than the population size. Rescaling a weights
variable
will have no effect on estimates or standard errors, because the scale
factor cancels out when se.method="SANDWICH"
.
Additional information about the sample design may be supplied via
clusters
and strata
. If weights
is supplied,
then the sampling plan is assumed to fall within the general class of
with-replacement (WR) designs. At the first stage, clusters are drawn
with replacement. Then individuals are selected within clusters,
possibly with unequal probabilities, possibly in multiple stages. The
clusters
and strata
variables should be integers or
factors. The integers serve merely as identifiers; the actual values
are unimportant. Cluster identifiers are assumed to be unique within
strata, so that cluster 1
in stratum 1
and cluster
1
in stratum 2
are assumed to be different. If
clusters
is not supplied, then each sampled individual is
assumed to be a cluster. If strata
is not supplied, then one
stratum is assumed for the whole population. Note a weight variable is
required for complex survey data; if weights=NULL
, then
clusters
and strata
will be ignored.
It is often useful to fit a model that describes only a part of the
full population (e.g., females). With a simple random sample, it is
acceptable to remove the sampled individuals who are not in this
subpopulation (e.g, males) from the data frame or model environent,
because those who remain will be then a simple random sample of the
subpopulation of interest. With a complex survey design, however,
discarding individuals who do not belong to the subpopulation may lead
to incorrect standard errors, because the overall design may not scale
down to the subpopulation. To fit a model to a subpopulation when
weights
is present, supply a logical variable subpop
whose elements are TRUE
for members of the subpopulation. If
subpop
is supplied and weights=NULL
, individuals outside
of the subpopulation will be ignored when computing estimates and
standard errors.
A list whose class
attribute has been set to "lca"
.
A nicely formatted summary of this object may be seen by applying
the summary
method, but its components may also be accessed
directly. Components which may be of interest include:
ncases |
number of ncases (rows) from the data frame or model environment used in the procedure. |
nitems |
number of response variables appearing in the model. |
nlevs |
integer vector of length |
ngroups |
number of groups present in a multi-group analysis,
or |
iter |
number of EM iterations performed. |
converged |
logical value indicating whether the algorithm
converged by |
loglik |
vector of length |
loglik.final |
value of the loglikelihood or pseudo-loglikelihood function after the final iteration. |
logpost |
vector of length |
logpost.final |
value of the log-posterior density after the final iteration. |
AIC |
Akaike's information criterion (smaller is better). Will
be |
BIC |
Bayesian information criterion (smaller is better). Will
be |
param |
estimated parameters after the final
iteration. This is a list with two named components, |
post.probs |
matrix of estimated posterior probabilities of class membership given the observed items. This is a matrix with rows corresponding to cases or rows of the dataset and columns corresponding to classes. |
se.fail |
logical value equal to |
se.rho |
array with same dimensions as |
se.gamma |
array with same dimensions as |
dim.theta |
number of free parameters estimated in the
model. The free parameters correspond to the elements of
|
theta |
vector of length |
cov.theta |
estimated covariance matrix for the
parameters in |
score |
vector of first derivatives of the loglikelihood of
pseudo-loglikelihood (plus penalty terms for flattening constants,
if present)
with respect to the free parameters in |
hessian |
matrix of (minus one times) the second derivatives of
the loglikelihood or pseudo-loglikehood (plus penalty terms for
flattening constants, if present) with respect to the free parameters in
|
sandwich.meat |
inner matrix of the sandwich variance formula.
This is an empirical estimated covariance matrix for |
deff.trace |
the design-effect trace, if |
Joe Schafer
Send questions to mchelpdesk@psu.edu
For more information on using this function and other functions in
the LCCA package, see the manual LCCA Package for R, Version 1
in the subdirectory doc
.
compare.fit
,
lcacov
,
lca.datasim
,
permute.class
,
summary.lca
## fit a two-class model to HIV test data data(hivtest) set.seed(123) fit <- lca( cbind(A,B,C,D)~1, data=hivtest, freq=COUNT ) summary(fit, show.all=TRUE)