# Latent Class Analysis with Stata

#### jsuazo

##### New Member
Hi,
Have anyone used Stata for Latent Class Analysis? I want to estimate willingness to pay with it, but I'm not sure it is possible with this software...

Thanks x

#### RedOwl

##### New Member
See LCAplugin for Stata at http://methodology.psu.edu/downloads/lcastata .

You can also use gllamm ado for LCA. Just google gllamm LCA for info.

Having said that, Mplus and Latent Gold are great for LCA and I recommend them over Stata for LCA. I am a Stata fan, but Stata's matsize limits for the IC version can be a problem for LCA. If you have SE or MP version of Stata, that should not be a problem.

#### jsuazo

##### New Member
Thanks a lot! I'd Latent Gold but they don't have it at my uni. Appreciate your help though!

#### jsuazo

##### New Member
Hi RedOwl,

I still haven't been able to do the LCA analysis because I am not sure how to install the plugin you suggested.

Could you help me out in how to do that?

Thanks a lot from Germany!

Cheers,
Maggie

#### RedOwl

##### New Member
1. Download the LCA Stata plugin from the Methodology Center at Penn State ( http://methodology.psu.edu/downloads/lcastata ). (This plugin works only on Windows machines, so the following assumes you are using Windows on a PC.)

2. Check whether you are using the 32-bit or 64-bit version of Stata by typing in Stata the command -about- .

3. Extract either the 32-bit or 64-bit version of the LCA plugin (whichever matches the bit version of your Stata software).

4. Create a new folder named "LCAPlugin" in the root directory of your C:\ drive.

5. Place the contents of the extracted version of the LCA plugin in the newly created C:\LCAPlugin directory.

6. Using Notepad (or any other plain text editor, but not Word), open the file named doLCA.ado .

7. Change the last line in the doLCA.ado file to: program lca, plugin using("C:/LCAPlugin/lca.dll") .

8. Save the modified version of doLCA.ado and do not change its name.

9. Using Notepad (or any other plain text editor, but not Word), open the file named LCA.do .

10. Find this line: cd D:\project\Stata_lca\Release64\ /*CHANGE THIS PATH TO MATCH THE FILE LOCATION ON YOUR MACHINE!*/

11. Change that line to: cd C:/LCAPlugin/ /*CHANGED TO LCA Plugin Directory ON LOCAL MACHINE!*/

12. In that same file, find -rhoprior(1.0)- in the first example.

13. If you have Stata/IC rather than Stata/SE or higher, change -rhoprior(1.0) in the first example to -rhoprior(1.0) ///- .

14. If you have Stata/IC rather than Stata/SE or higher, then add -maxiter(800)- on a new line below -rhoprior(1.0) ///- in Example 1. (That's necessary because of the maximum matrix size limitation of 800 in Stata/IC.)

15. If you have Stata/IC rather than Stata/SE or higher, then change -maxiter(5000)- to -maxiter(800)- in Examples 2-5. (Again, that is necessary to satisfy the maximum matrix size limitation in Stata/IC.)

16. If you have Stata/IC rather than Stata/SE or higher, find this line near the top of the file: infile using LcaSampleDataset. Then change that line to: infile using LcaSampleDataset in 1/750. (That's necessary because the matrix size limitation in Stata/IC also limits the number of cases you can analyze. This uses only the first 750 lines in the data file.)

17. Save the modified version of LCA.do with the name myLCA.do.

18. In Stata/IC type -set matsize 800, permanently- to increase the mat size to its maximum. In higher versions of Stata, this may not be necessary.

19. Run the examples to test your installation of the LCA plugin by running the myLCA.do file.

20. If you have problems, send me a private message via TalkStats and I'll send you my email address via a private message.

Note: If you have Stata/SE or Stata/MP instead of Stata/IC, you can skip steps 12-14 above, because the maximum matrix size limitations in the higher versions of Stata are much less restrictive. (I don't believe you can use Small Stata with the LCAPlugin.)

Good luck.

(By the way, I have successfully replicated the LCA Stata Plugin results in both Mplus and LatentGold using my own data.)

Last edited:

#### jsuazo

##### New Member
Thanks a lot for your help! I try to send you a message now

#### Fulton

##### New Member
Thank you for providing these excellent steps. I've followed them, but I keep getting this error message "unrecognized command" when I run the examples to test your installation of the LCA plugin. The error occurs when running Example #1.

Any thoughts on what might be causing this problem?

Thanks

#### RedOwl

##### New Member
@Fulton, are you running the Example 1 code in a do-file window? If not, the /// line breaks would cause the error you mentioned.

To open a do-file window, just enter -doedit- in Stata. Then you can paste the code from Example 1 and run it by pressing the right arrow button on the menu bar.

Another problem might be that you perhaps did not change the path to the LCAplugin on your local computer in the Example 1 code. That line looks like the following, but should have your drive and LCAPlugin directory specified:

Code:
cd C:/LCAPlugin/ /*CHANGE THIS PATH TO MATCH THE FILE LOCATION ON YOUR MACHINE!*/
Still another problem might be that you might have tried to run the LCA Example 1 code on the full data set. Because Stata/IC can only handle a maximum matrix size of 800, you may use no more than 750 cases with the LCAPlugin. In the Example 1, you need to limit the data to the first 750 cases, as shown here:

Code:
infile using LcaSampleDataset in 1/750, clear
If you are using Stata/SE or Stata/MP, you don't have to limit the cases in the Example 1, because the matrix size limits are much, much larger.

If none of that works for you and you are still having problems, send me a private message via TalkStats and I'll send you my email address to discuss this further.

By the way, I just tested Example 1 again, and it does work, so you've probably just got a minor problem to fix.

Good luck.

#### RedOwl

##### New Member
Yes, as I mentioned above, LCA is possible with Stata using the gllamm ado program written by Sophia Rabe-Hesketh (-findit gllamm-).

As I wrote in my post in this thread on 6-13-2013, you can find information on how to code LCA with gllamm in Stata by googling "gllamm LCA".

Also see slides 13-14 at http://www.gllamm.org/rss5.pdf (which I found by googling).

#### melmogavero

##### New Member
Hello,

I need to conduct a LCA on my dataset. I have I downloaded STATA (never used it before) and the Plug in from Methodology Center at Penn State. From there, how to conduct the analysis is not clear to me. Can someone assist me with this?

#### sprocket

##### New Member
Hi. I have got the lca plugin to work with stata. Does anyone know how to do the bootstrap likelihood ratio test for a lca in stata. It is not part of the plugin. Also, when I run the command list r(post_prob), do you know how to get the values into a new variable. Currently I copy them into excel and merge them with the dataset. I hoping there is an easier way.

Thank you.

#### RedOwl

##### New Member
After running the LCA, do the following to add variables named post_prob1,
post_prob2, etc., to the active data set in memory:

Code:
  return list
matrix list r(gamma)
cap matrix list r(gammaSTD)
matrix list r(rho)
cap matrix list r(rhoSTD)
matrix list r(post_prob)

mat PPMAT=r(post_prob)
svmat PPMAT, name(post_prob)
Only the last two lines of the code above are really necessary.
I just wanted to give you the context by showing the results
of -return list-.

Of course, you will need to save the active data set to retain the new
variables, because they are only in memory after running the code
above.

Regarding bootstrapping the log likelihood, just put the following on
the same line as the doLCA estimation command in front of -doLCA-:

Code:
bootstrap bootloglik = r(loglikelihood):
To run the LL test, just obtain the loglihood of the previous model
with 1 less latent class and put that value in a scalar named model1LL

Then obtain the loglihood of the model with an additional latent class
specified and put that value in a scalar named model2LL.

Then run the following code:

Code:
di "chi-square(2) = " 2*(model2LL-model1LL) _n
di "Prob of chi-square = " chi2tail(2, 2*(model2LL-model1LL)) _n

Last edited:

#### gowri

##### New Member
Hi
I tried to use doLCA in stata. Always endup with an error "Could not load plugin: C:\LCAPlugin\lca.dll". What should i do. I'm using the Stata IC

#### RedOwl

##### New Member
I can't know exactly what is causing your problem, but if you perform the following steps, one of them may fix it. (Of course, you must be using Windows, since the LCA Stata Plugin only works with Windows.)

1. Create a new folder named "LCAPlugin" in the root directory of your C:\ drive if one does not already exist. (Be sure it is spelled exactly that way, including capitalization.)

2. Place the contents of the extracted version of the LCA plugin in the C:\LCAPlugin directory.

3. Using Notepad (or any other plain text editor, but not Word), open the file named doLCA.ado that you will find in the C:\LCAPlugin directory.

4. Change the last line in the doLCA.ado file to the following:
Code:
program lca, plugin using("C:/LCAPlugin/lca.dll")
5. Save the modified version of doLCA.ado and do not change its name.

6. Using Notepad (or any other plain text editor, but not Word), open the file named LCA.do in the C:\LCAPlugin directory.

7. In LCA.do find this line:
Code:
cd D:\project\Stata_lca\Release64\ /*CHANGE THIS PATH TO MATCH THE FILE LOCATION ON YOUR MACHINE!*/
8. Change that line to:
Code:
cd C:/LCAPlugin/ /*CHANGED TO LCA Plugin Directory ON LOCAL MACHINE!*/
Hopefully that will do it for you.

There is more extensive advice in my post dated 07-15-2013 above in this topic.

#### accalexico

##### New Member
Dear RedOwl,
I am also a new user to STATA and getting familiar with STATA 13. I heard that this new version improved SEM and thus LPA/LCA could be easily run. I wonder if you have any thoughts/experience on this
Thank you for your time
AC

#### RedOwl

##### New Member
accalexico,

I am not aware of any new LPA/LCA features in Stata 13/13.1 even though the SEM features have been substantially expanded to handle discrete endogenous variables -- which is what is needed for LPA/LCA. I had expected those would be added in the new version 13, but it didn't happen.

I expect it may happen in version 14 or that we may see a new user-written ado program for LCA sooner that takes advantage of the new SEM features in Stata 13. We can always hope!

The LCA Plugin from Penn State's Methodology Center works fine with Stata versions above IC, and it works OK with IC if there are fewer than about 750 variables (due to the matrix size limits of IC) and relatively modest sample sizes. Beyond that, LCA/LPA can be done with GLLAMM, so StataCorp may not see adding a built-in LCA/LPA command as really needed.

For now, I am using the Plugin for teaching LCA with IC, and for my research requiring LCA I am using either GLLAMM or R with the poLCA package or Mplus 7.1 or Latent Gold (recently updated to version 5).

Let's keep our fingers crossed for either a new user-written program for version 13/13.1 or for a built-in command in version 14!

#### alin

##### New Member
Thanks for providing help on using the plugin, I found it to be extremely useful.
So I just got started with using this plugin. I downloaded the plugin and the sample provided by the Methodology center at Penn worked like a charm. However, with my own dataset it doesn't seem to work. I get the error msg "option using() incorrectly specified".
The syntax I used is:
doLCA CB_DISCUSS_HOME CB_NEWS_ACCESS CB_QUAL_HELP_COMMUNITY CIVIC_ATTITUDES, ///
nclass(5) ///
seed(100000) ///
categories(5 5 5 5) ///
criterion(0.000001) ///
rhoprior(1.0)

I noticed that the example (LcaSampleDataset) supplied with the plugin is a dct file. Does that mean that my dataset also has to be in this dct format?

I have STATA SE 12.1 for Windows (64-bit)
Thanks,

Alex

#### andreabtst

##### New Member
Hi
I tried to use doLCA in stata. Always endup with an error "Could not load plugin: C:\LCAPlugin\lca.dll". What should i do. I'm using the Stata IC
That happened to me, you have to verify if you are loading the correct plug-in for a 32 bit or 64. I was trying the 64 but I double check in the command line of stata using "about" and found that it was really 32bit.