Announcement

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

  • gsem: Same code, no longer working

    Hello,

    I am using gsem in Stata 15 for an LCA, and although I have been using Stata for some time, I am new to LCAs and sem/gsem. I would like to identify the underlying latent class variable (categorical) using 7 observed binary variables: pthink_par_qual_bin cthink_par_qual_bin know_datr felt_nlove talk_diff resp_hpe wor_depr using weighted survey data. I am working on identifying the number of classes that would be most appropriate for the data and from a theoretical standpoint, but I have 2 issues:

    1. I am unable to use the command
    Code:
     estat lcgof
    I receive an error message that the command is unrecognized. I'm sorry I cannot provide additional details on the precise error code and message because . . .

    2. I am no longer able to use gsem. I was running the following model earlier in the day:

    Code:
    svy: gsem (pthink_par_qual_bin cthink_par_qual_bin know_datr felt_nlove talk_diff resp_hpe wor_depr <-), logit lclass(C 2)
    It ran beautifully, and I was able to compute some postestimation stats, e.g., estat lcmean, estat lcprob, etc. with no issues (but not estat lcgof). However, a few hours later I ran the same code and received the error message
    Code:
    ����DB�ж�V���_�C�P��W�H� invalid name
    invalid syntax
    invalid syntax
    an error occurred when svy executed gsem
    r(198);
    I tried shutting Stata and the computer down, restarting, installing available updates, but nothing has worked. It is the exact same code and exact same data, so I do not understand why it will not run. (Also, I tried running it without using the survey prefix, but this did not help at all.)

    Does anyone have any ideas what may be going on and how I can fix it?

    Thank you in advance!

  • #2
    You should start by running examples in the documentation to determine whether it is a problem with Stata or it is an issue with your code. Sometimes invisible characters in the code can lead to "invalid name" errors, and this can arise, e.g., from copying and pasting the code from a third party editor.

    Code:
    help gsem
    Last edited by Andrew Musau; 23 Sep 2021, 16:40.

    Comment


    • #3
      Thank you, Andrew, that was a great idea! I ran the example 50g in Stata's sem manual and it worked exactly as it should have. This is good news, as now I know that it is not an issue with Stata (or my specific version). However, I still do not understand why my code will not run, especially because I ran the exact same code earlier that day. I have gone over my code many times, e.g., checking spaces, as well as checking my variables to ensure there is nothing wrong there.

      Any other advice would be greatly appreciated!!

      Comment


      • #4
        It is a relatively short command, so I would try retyping it from scratch. Like Andrew, i suspect your code got corrupted somehow and may have invisible characters you can’t see.
        -------------------------------------------
        Richard Williams, Notre Dame Dept of Sociology
        StataNow Version: 18.5 MP (2 processor)

        EMAIL: [email protected]
        WWW: https://www3.nd.edu/~rwilliam

        Comment


        • #5
          Thank you for your advice, Richard. Following your suggestion, I have tried retyping it several times and in different ways (e.g., without the survey command, in the command window, in the do-file, with and without the " / / / " line connectors), and still no luck. I added (from the variable list window using the arrow) my own variables into the command from 50g in the sem manual (the example data and code in 50g worked just fine), and nothing has worked. I have no idea what else to try.

          Thank you for your time and advice!

          Comment


          • #6
            Weird. Do you have another machine you can try? Also, are you sure your data set did not somehow get corrupted? My guess is that something got corrupted somewhere.
            -------------------------------------------
            Richard Williams, Notre Dame Dept of Sociology
            StataNow Version: 18.5 MP (2 processor)

            EMAIL: [email protected]
            WWW: https://www3.nd.edu/~rwilliam

            Comment


            • #7
              Another good idea. I tried the virtual Stata on my company's VPN but with no more luck. Also, I should note that I checked all pertinent variables closely, including looking at their distribution, total n, and total missing and verified that it matched my original data that I was able to use previously and successfully with the command specified above. I will continue digging into the data and the issue.

              Thank you again for your time and your advice!

              Comment


              • #8
                Code:
                log using gsem.log, text replace
                set trace on
                //!! run your commands here
                log close
                set trace off
                post the gsem.log or the section before the error, make sure you delete any sensible information before post.

                Comment


                • #9
                  The one thing that I noticed after posting is that you have very long variable names, which in some contexts can cause problems. Try

                  Code:
                  preserve
                  rename (pthink_par_qual_bin cthink_par_qual_bin know_datr felt_nlove talk_diff resp_hpe wor_depr) (var#), addnumber(1)
                  svy: gsem (var1 var2 var3 var4 var5 var6 var7 <-), logit lclass(C 2)
                  restore

                  If this still results in the error, follow Hua Peng (StataCorp)'s advice and post the log.

                  Comment


                  • #10
                    Thank you, Hua and Andrew, for your advice! I renamed the variables var1-7, but that did not work, indicating that it is something besides the variable names that is the issue.

                    Then I set the trace on but did not learn anything. I tried it without the survey command and got a little farther. The log is below (I was unable to upload it):


                    Code:
                    . set trace on
                    
                    . svy: gsem (var1 var2 var3 var4 var5 var6 var7 <- ), logit lclass(C 2)
                    (running gsem on estimation sample)
                    ¬ÉßØDB*жâVŠ‘»_ìC¥PøèWÛH invalid name
                    invalid syntax
                    invalid syntax
                    an error occurred when svy executed gsem
                    r(198);
                    
                    end of do-file
                    
                    r(198);
                    
                    . do "C:\Users\HUDAK-~1\AppData\Local\Temp\STD1758_000000.tmp"
                    
                    . *try without svy
                    . gsem (var1 var2 var3 var4 var5 var6 var7 <- ), logit lclass(C 2)
                    ¬ÉßØDB*жâVŠ‘»_ìC¥PøèWÛH invalid name
                    invalid syntax
                    
                    Fitting class model:
                    
                    Iteration 0:   (class) log likelihood = -885.14856  
                    Iteration 1:   (class) log likelihood = -885.14856  
                    
                    Fitting outcome model:
                    
                    Iteration 0:   (outcome) log likelihood = -3853.6137  
                    Iteration 1:   (outcome) log likelihood = -3777.1893  
                    Iteration 2:   (outcome) log likelihood = -3772.8617  
                    Iteration 3:   (outcome) log likelihood = -3772.7736  
                    Iteration 4:   (outcome) log likelihood = -3772.7733  
                    
                    Refining starting values:
                    
                    Iteration 0:   (EM) log likelihood =  -4637.765
                    Iteration 1:   (EM) log likelihood = -4631.9045
                    Iteration 2:   (EM) log likelihood = -4629.7688
                    Iteration 3:   (EM) log likelihood = -4628.8446
                    Iteration 4:   (EM) log likelihood = -4628.4517
                    Iteration 5:   (EM) log likelihood = -4628.3138
                    Iteration 6:   (EM) log likelihood = -4628.2989
                    Iteration 7:   (EM) log likelihood = -4628.3407
                    Iteration 8:   (EM) log likelihood = -4628.4052
                    Iteration 9:   (EM) log likelihood =  -4628.475
                    Iteration 10:  (EM) log likelihood =  -4628.542
                    Iteration 11:  (EM) log likelihood = -4628.6023
                    Iteration 12:  (EM) log likelihood = -4628.6549
                    Iteration 13:  (EM) log likelihood = -4628.6996
                    Iteration 14:  (EM) log likelihood = -4628.7372
                    Iteration 15:  (EM) log likelihood = -4628.7688
                    Iteration 16:  (EM) log likelihood = -4628.7945
                    Iteration 17:  (EM) log likelihood = -4628.8157
                    Iteration 18:  (EM) log likelihood =  -4628.833
                    Iteration 19:  (EM) log likelihood = -4628.8471
                    Iteration 20:  (EM) log likelihood = -4628.8586
                    Note: EM algorithm reached maximum iterations.
                    invalid syntax
                    r(198);
                    
                    end of do-file
                    
                    r(198);
                    I would love to know what may be going on and how to move forward, especially because I know:
                    1) gsem works on my version of Stata
                    2) I was able to run the exact same code on the same data last week
                    3) the data does not seem to have changed because all my summary stats are the same

                    Thank you in advance!!

                    Comment


                    • #11
                      Unable to see your data, I think we need to try a more systematic approach. First let's try to eliminate other possibilities by:

                      1. fully update your Stata 15, type

                      update all

                      in Stata command window and follow the instruction

                      2. After fully update, download Stata installation qualification tool from https://www.stata.com/support/instal...lification/15/
                      install and run it to check if the Stata installation on you machine is good.

                      3. If problem persists, report back and we can move forward from there.

                      Comment


                      • #12
                        Also, can you show the output of

                        desc var1 var2 var3 var4 var5 var6 var7

                        Comment


                        • #13
                          Update: I solved the issue of the gsem command not running. The dataset I was working with was too large. Once I cut it to the bare necessities, the gsem command ran without any issues.

                          However, I am still unable to use the
                          Code:
                          estat lcgof
                          to compare models to determine the optimal number of classes. The code I am using is:
                          Code:
                          svy: gsem (pthink_par_qual_bin cthink_par_qual_bin know_datr felt_nlove talk_diff ///
                          resp_hpe wor_depr <-), logit lclass(C 2)
                          
                          estat lcgof
                          where all of my observed variables are binary indicators. The error message I get is
                          Code:
                           estat lcgof
                          invalid subcommand lcgof
                          r(321);
                          
                          end of do-file
                          
                          r(321);
                          I have tried it in both Stata 15, which I have on my machine, and in Stata 16, using a VPN. However, the results and the error message are the same.

                          Thank you for your time and assistance!!

                          Comment


                          • #14
                            I should note that I've also tried this with the estimates store command
                            Code:
                            svy: gsem (pthink_par_qual_bin cthink_par_qual_bin know_datr felt_nlove talk_diff ///
                            resp_hpe wor_depr <-), logit lclass(C 1)
                            
                            estimates store oneclass
                            
                            estat lcgof
                            with the same results and error message.

                            Comment


                            • #15
                              Code:
                              help svy_estat
                              does not indicate that estat lcgof is supported after svy: gsem. I do not know why this is the case, but what is allowed is

                              estat gof reports a goodness-of-fit test for binary response models using survey data. This command is available only after svy: logistic, svy: logit, and svy:
                              probit; see [R] logistic, [R] logit, and [R] probit.
                              Have you ever successfully gotten results previously? The following replicates your error:

                              Code:
                              webuse nhanes2f
                              svyset psuid [pweight=finalwgt], strata(stratid)
                              svy: gsem (highlead zinc age age2 weight <- ), logit lclass(C 1)
                              estat lcgof
                              Res.:

                              Code:
                              . estat lcgof
                              invalid subcommand lcgof
                              r(321);

                              Comment

                              Working...
                              X