# Example 8.22: latent class modeling using randomLCA

January 24, 2011
By

[This article was first published on SAS and R, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

In Example 8.21 we described how to fit a latent class model to data from the HELP dataset using SAS and R. Subjects were classified based on their observed (manifest) status on the following variables (on street or in shelter in past 180 days [homeless], CESD scores above 20, received substance abuse treatment [satreat], or linked to primary care [linkstatus]). We arbitrarily specify a three class solution.

In this example, we fit the same model using the randomLCA() function within the package of the same name.

R

We begin by reading in the data.

`ds = read.csv("http://www.math.smith.edu/r/data/help.csv")attach(ds)library(randomLCA)`

We start by creating a dichotomous variable with high scores on the CESD, and put this together as part of a dataframe to be given as input.

`cesdcut = ifelse(cesd>20, 1, 0)smallds = na.omit(data.frame(homeless, cesdcut, satreat, linkstatus))results = randomLCA(smallds, nclass=3, notrials=1000)summary(results)`

This generates the following output:

`  Classes      AIC      BIC    logLik        3 2092.968 2149.893 -1032.484Class probabilities Class  1 Class  2 Class  3  0.07846  0.70534  0.21620 Outcome probabilities           homeless cesdcut   satreat linkstatusClass  1 9.465e-06  0.5786 1.000e+00  9.538e-06Class  2 4.375e-01  0.8322 9.988e-06  4.145e-01Class  3 7.297e-01  0.8846 1.000e+00  3.971e-01`

The results are equivalent to the results from the prior example, though the scientific notation for the observed prevalences in each class are hard to read. Other objects are available from the returned value, though they are also not in an easily digestible form:

`> names(results) [1] "fit"        "nclass"     "classp"     "outcomep"   [5] "se"         "np"         "nobs"       "logLik"     [9] "observed"   "fitted"     "deviance"   "classprob" [13] "bics"       "random"     "level2"     "byclass"   [17] "blocksize"  "call"       "probit"     "quadpoints"[21] "patterns"   "notrials"   "freq"      > results\$patterns   homeless cesdcut satreat linkstatus1         0       0       0          02         0       0       0          13         0       0       1          04         0       0       1          15         0       1       0          06         0       1       0          17         0       1       1          08         0       1       1          19         1       0       0          010        1       0       0          111        1       0       1          012        1       0       1          113        1       1       0          014        1       1       0          115        1       1       1          016        1       1       1          1> results\$freq [1] 17 10 16  1 82 62 33  9 15  9  4  4 64 45 37 23`

We’ll address workarounds for these shortcomings in a future entry.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

Tags: , ,