Saturday, April 19, 2014

Class-Conditional Response Probabilities

One issue that I've been trying to resolve is how to graph class-conditional response probabilities manually using the package 'poLCA' in R. I've figured out one approach using the code below:

# extracting response probabilities
R <- br="" lc="" length="" p="" probs="" ti="" y="">R <- matrix="" nrow="length(probs),ncol=R)<br" pi.class="" probs="">for (j in 1:length(probs))
  pi.class[j,] <- br="" category="" first="" for="" j="" probability="" probs="">dimnames(pi.class) <- br="" list="" names="" round="" y=""># if you want to specify your own rownames: rownames(pi.class) <- br="" row.names="">
# extracting standard errors
probs.se <- lc="" matrix="" nrow="length(probs.se),ncol=R)<br" probs.se="" se.class="">for (j in 1:length(probs.se))
  se.class[j,] <- br="" category="" j="" level="" probs.se="" specifies="">dimnames(se.class) <- br="" list="" names="" round="" y=""># if you want to specify your own rownames: rownames(se.class) <- br="" row.names="">
## creating an augmented dataset
# class-conditional probabilities and standard errors
df.probs <- data.frame="" lasses="as.vector(col(pi.class)),<br">                            Manifest.variables=as.vector(row(pi.class)),
                            value=as.vector(pi.class),names=rownames(pi.class),
                            se=as.vector(se.class))

## (1) LINE PLOT (No Std. Errors): line plot of latent classes
win.graph()
p <- aes="" br="" df.probs="" ggplot="" x="factor(Manifest.variables)," y="value,">                          color=factor(Classes)))
p + geom_freqpoly(stat="identity",aes(group=Classes)) + #NB!!!
  geom_point(stat="identity",aes(group=Classes)) + #NB!!!
  scale_color_hue(name="Latent Class") + xlab("Manifest Variables") +
  ylab('P(Y = "Too Little")') +
  ggtitle("Class-Conditional Response Probabilities by Latent Class") +
  theme_bw() + scale_x_discrete(labels=unique(df.probs$names)) +
  coord_flip()
  # to add variable names manually for the manifest variables:
  # + scale_x_discrete(labels=c(""))
dev.off()

## (2) RIBBON PLOT (has Std. Errors): ribbon plot of response probabilities
# (with standard errors) using ggplot to graph the predicted probabilities

df.probs$lower <- -="" br="" df.probs="" se="" value="">df.probs$upper <- br="" df.probs="" se="" value="">df.probs$Classes <- br="" df.probs="" factor="" lasses="">
# using ggplot to graph the predicted probabilities
win.graph()
ggplot(df.probs, aes(x = Manifest.variables, y = value, group=Classes)) +
  geom_ribbon(aes(ymin = lower, ymax = upper, fill=Classes),
              alpha = 0.2) +
  geom_line(aes(colour = Classes), size = 1) + theme_bw() +
  ggtitle("Class-Conditional Response Probabilities by Latent Class") +
  xlab("Manifest Variables") +   ylab('P(Y = "Too Little")') +
  scale_fill_discrete("Latent Class") +
  scale_linetype_discrete("Latent Class") +
  scale_shape_discrete("Latent Class") +
  scale_colour_discrete("Latent Class") +
  scale_x_discrete(labels=unique(df.probs$names)) + coord_flip()
  # to add variable names manually for the manifest variables:
  # + scale_x_discrete(labels=c(""))
dev.off()