public
Last active

Latent class model of ANES respondents

  • Download Gist
latent_class_analysis.R
R
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
doInstall <- TRUE
toInstall <- c("ggplot2", "poLCA", "reshape2")
if(doInstall){install.packages(toInstall, repos = "http://cran.us.r-project.org")}
lapply(toInstall, library, character.only = TRUE)
 
ANES <- read.csv("http://www.oberlin.edu/faculty/cdesante/assets/downloads/ANES.csv")
ANES <- ANES[ANES$year == 2008, -c(1, 11, 17)] # Limit to just 2008 respondents,
head(ANES) # remove some non-helpful variables
# Adjust so that 1 is the minimum value for each variable:
ANES <- data.frame(apply(ANES, 2, function(cc){ cc - min(cc, na.rm = T) + 1 }))
 
# Estimate latent class model
lcFormula <- cbind(cohort, female, race6, religion, pid7, trust, ideo7, inerrant, south) ~ 1
lcModel <- poLCA(lcFormula, ANES, nclass = 4,
maxiter = 10000) # Make sure MAX LIKE is found.
plot(lcModel) # poLCA-style 3-D plot.
 
# Make a cleaner plot, first easily converting a list to a DF with melt():
lcModelProbs <- melt(lcModel$probs)
 
# Replicating the poLCA 3-D plot, without the 3-D:
zp1 <- ggplot(lcModelProbs,
aes(x = L1, y = value, fill = Var2))
zp1 <- zp1 + geom_bar(stat = "identity", position = "stack")
zp1 <- zp1 + facet_wrap(~ Var1)
print(zp1)
 
# Suggested alternative, as a possible improvement:
zp2 <- ggplot(lcModelProbs,
aes(x = Var1, y = value, fill = Var2))
zp2 <- zp2 + geom_bar(stat = "identity", position = "stack")
zp2 <- zp2 + facet_wrap(~ L1)
zp2 <- zp2 + scale_x_discrete("Class", expand = c(0, 0))
zp2 <- zp2 + scale_y_continuous("Proportion", expand = c(0, 0))
zp2 <- zp2 + scale_fill_discrete("Factor Level")
zp2 <- zp2 + theme_bw()
print(zp2)

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.