Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • margins 'too few variables specified'

    Hi I ran a LCA

    Code:
    gsem(postq1 <-preq1, ologit) ///
    (postq2 <-preq2, ologit) ///
    (postq3 <-preq3, ologit) ///
    (postq4 <-preq4, ologit) ///
    (C <- treatment) [fw=fw], lclass(C3)
    
    /// Trying to plot line graph for each class by  treatment (treatment is binary 0/1 (see attached)
    Click image for larger version

Name:	Capture.PNG
Views:	1
Size:	229.4 KB
ID:	1743071
    margins, predict(postq1) class(1)) /// predict(postq2) class(1)) /// predict(postq3) class(1)) /// predict(postq4) class(1)) /// ///ERROR: too few variables specified
    Of note I have searched Statalist entirely and looked at every post about LCA + margins plot

    I followed these notes too

    https://www.stata.com/meeting/uk18/s..._MacDonald.pdf

  • #2
    Update:

    I have come slightly closer, but it still didn't produce what i wanted
    Code:
    margins i.treatment, at(treatment=(0/1)) predict(classpr class(1)) predict(classpr class(2)) predict(classpr class 3)) 
    marginsplot, by(treatment) ytitle ("Probability of class membership") byopts(title("Predicted atent class probabilities with 95% CI")) legend(order(4 "Class 1" 5 "Class 2" 6 "Class 3")
    Any help appreciated.
    As the x axis is the treatment, when I wanted the x axis to be Q1-Q4 (as seen in post 1 - drawn graph)

    Comment


    • #3
      I am not sure you can get your desired graph from margins. You have two major pieces of information from the model that margins gives you - the means on the items for each latent class and then the predicted probability of class membership based on whether one was in treatment or not. See the code below. The data is from the sem manual example 50g:
      Code:
      use https://www.stata-press.com/data/r16/gsem_lca1, clear
      generate byte treatment = rbinomial(1, 0.5)     // generate a treatment variable
      
      *Fit model
      gsem (accident play insurance stock <- ,logit) ///
          (C <- i.treatment),  lclass(C 2)
      eststo m1
      
      *marginal effect of treatment by latent class
      margins treatment, predict(classpr class(1)) predict(classpr class(2))  
      
      Adjusted predictions                            Number of obs     =        216
      Model VCE    : OIM
      
      1._predict   : Predicted probability (1.C), predict(classpr class(1))
      2._predict   : Predicted probability (2.C), predict(classpr class(2))
      
      ------------------------------------------------------------------------------------
                         |            Delta-method
                         |     Margin   Std. Err.      z    P>|z|     [95% Conf. Interval]
      -------------------+----------------------------------------------------------------
      _predict#treatment |
                    1 0  |   .7043118   .0673365    10.46   0.000     .5723348    .8362889
                    1 1  |   .7423467   .0706903    10.50   0.000     .6037963    .8808972
                    2 0  |   .2956882   .0673365     4.39   0.000     .1637111    .4276652
                    2 1  |   .2576533   .0706903     3.64   0.000     .1191028    .3962037
      ------------------------------------------------------------------------------------
      
      marginsplot, xdimension(_predict)
      
      *marginal effect of treatment by item
      margins treatment
      
      Adjusted predictions                            Number of obs     =        216
      Model VCE    : OIM
      
      1._predict   : Predicted mean (would testify against friend in accident case), using class prob, predict(mu
                     outcome(accident))
      2._predict   : Predicted mean (would give negative review of friend's play), using class probab, predict(mu
                     outcome(play))
      3._predict   : Predicted mean (would disclose health concerns to friend's insurance company), u, predict(mu
                     outcome(insurance))
      4._predict   : Predicted mean (would keep company secret from friend), using class probabilitie, predict(mu
                     outcome(stock))
      
      ------------------------------------------------------------------------------------
                         |            Delta-method
                         |     Margin   Std. Err.      z    P>|z|     [95% Conf. Interval]
      -------------------+----------------------------------------------------------------
      _predict#treatment |
                    1 0  |   .7965832   .0288452    27.62   0.000     .7400476    .8531188
                    1 1  |   .7858562   .0304571    25.80   0.000     .7261615     .845551
                    2 0  |   .5105412   .0397122    12.86   0.000     .4327068    .5883756
                    2 1  |   .4875422   .0417527    11.68   0.000     .4057085     .569376
                    3 0  |   .5239414   .0393451    13.32   0.000     .4468264    .6010565
                    3 1  |   .5020086   .0413498    12.14   0.000     .4209645    .5830527
                    4 0  |    .321293   .0387917     8.28   0.000     .2452627    .3973234
                    4 1  |   .2970577   .0400977     7.41   0.000     .2184677    .3756478
      ------------------------------------------------------------------------------------
      
      marginsplot, xdimension(_predict)
      
      *marginal effect of treatment for latent class 1 and by item?
      margins treatment, predict(outcome(accident) class(1)) ///
              predict(outcome(play) class(1)) ///
              predict(outcome(insurance) class(1)) ///
              predict(outcome(stock) class(1)) //
      
      Warning: prediction constant over observations.
      Warning: prediction constant over observations.
      Warning: prediction constant over observations.
      Warning: prediction constant over observations.
      
      Adjusted predictions                            Number of obs     =        216
      Model VCE    : OIM
      
      1._predict   : Predicted mean (would testify against friend in accident case in class 1.C), predict(outcome(accident)
                     class(1))
      2._predict   : Predicted mean (would give negative review of friend's play in class 1.C), predict(outcome(play)
                     class(1))
      3._predict   : Predicted mean (would disclose health concerns to friend's insurance company in ,
                     predict(outcome(insurance) class(1))
      4._predict   : Predicted mean (would keep company secret from friend in class 1.C), predict(outcome(stock) class(1))
      
      ------------------------------------------------------------------------------------
                         |            Delta-method
                         |     Margin   Std. Err.      z    P>|z|     [95% Conf. Interval]
      -------------------+----------------------------------------------------------------
      _predict#treatment |
                    1 0  |   .7131905   .0403155    17.69   0.000     .6341737    .7922074
                    1 1  |   .7131905   .0403155    17.69   0.000     .6341737    .7922074
                    2 0  |   .3317442   .0500327     6.63   0.000     .2336819    .4298066
                    2 1  |   .3317442   .0500327     6.63   0.000     .2336819    .4298066
                    3 0  |   .3534326   .0483493     7.31   0.000     .2586698    .4481955
                    3 1  |   .3534326   .0483493     7.31   0.000     .2586698    .4481955
                    4 0  |   .1328846   .0384768     3.45   0.001     .0574715    .2082976
                    4 1  |   .1328846   .0384768     3.45   0.001     .0574715    .2082976
      ------------------------------------------------------------------------------------
      As you can see, the last margins call does not give you the information you want in your hand-drawn graph, as the prediction is constant over observations. If you want to know the treatment effect by item, then you might need a different model entirely.
      Last edited by Erik Ruzek; 13 Feb 2024, 15:27. Reason: Clarification

      Comment

      Working...
      X