poLCA.posterior: Posterior probabilities from a latent class model

View source: R/poLCA.posterior.R

poLCA.posteriorR Documentation

Posterior probabilities from a latent class model

Description

Calculates the posterior probability that cases belong to each latent class.

Usage

 poLCA.posterior(lc,y,x=NULL) 

Arguments

lc

A model object estimated using the poLCA function.

y

A vector or matrix containing series of responses on the manifest variables in lc.

x

An optional vector or matrix of covariate values, if lc was specified as a latent class regression model.

Details

From the parameters estimated by the latent class model, this function calculates the "posterior" probability that a specified case – characterized by values of the manifest variables y, and, if a latent class regression model, concomitant variables x – "belongs to" each latent class in lc. For observed cases, this information is also contained in the lc model object as lc$posterior. The added benefit of this function is that it can calculate posterior class membership probabilities for arbitrary values of x and y, whether or observed or not.

Value

A matrix containing posterior probabilities corresponding to the specified sets of responses y, based on the estimated latent class model lc. For each row (one case), the first column gives the posterior probability of being in class 1, the second column gives the posterior probability of being in class 2, and so forth. Across rows, these probabilities sum to one.

See Also

poLCA

Examples

data(election)

## Basic latent class model with three classes
f1 <- cbind(MORALG,CARESG,KNOWG,LEADG,DISHONG,INTELG,
            MORALB,CARESB,KNOWB,LEADB,DISHONB,INTELB)~1
lc1 <- poLCA(f1,election,nclass=3)  # log-likelihood: -16714.66

# The first observed case
lc1$y[1,]
lc1$posterior[1,]
poLCA.posterior(lc=lc1,y=as.numeric(lc1$y[1,]))

# A hypothetical case
poLCA.posterior(lc=lc1,y=rep(2,12))

# Entering y as a matrix
lc1$posterior[1:10,]
poLCA.posterior(lc=lc1,y=mapply(as.numeric,lc1$y[1:10,]))


## Latent class regression model with three classes
f2 <- cbind(MORALG,CARESG,KNOWG,LEADG,DISHONG,INTELG,
            MORALB,CARESB,KNOWB,LEADB,DISHONB,INTELB)~AGE+EDUC+GENDER
lc2 <- poLCA(f2,election,nclass=3)  # log-likelihood: -16598.38

# Posteriors for case number 97 (poorly classified)
lc2$y[97,]
lc2$x[97,]
lc2$posterior[97,]
poLCA.posterior(lc=lc2,y=as.numeric(lc2$y[97,]),x=c(41,6,1))

# If x is not specified, the posterior is calculated using the population average
poLCA.posterior(lc=lc2,y=as.numeric(lc2$y[97,]))

# Entering y and x as matrices
round(lc2$posterior[95:100,],2)
round(poLCA.posterior(lc=lc2,y=mapply(as.numeric,lc2$y[95:100,]),
                             x=as.matrix(lc2$x[95:100,-1])),2)

poLCA documentation built on April 25, 2022, 5:06 p.m.