Announcement

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

  • latent class models with loclogit

    Hello everyone, I'm conducting a DCE, and i am trying to run multiple latent class models with the command lclogit, as described by Pacifico, D. and Yoo, H. (2013), so as to choose the one that has better BIC and CAIC. The intention is to have a table with all the BIC and CAIC so as to compare, but I keep getting a notation error , although I am following the exact commands as described in the paper

    Code:
    forvalues c = 2/10 {
    lclogit choice bycatch freshness origin fcert , group(card) id(id) nclasses(`c´) membership( age sex educ inc ) seed(1234567890)
     matrix b = e(b)
     matrix ic = nullmat(ic)\`e(nclasses)´,`e(ll)´,`=colsof(b)´,`e(caic)´,`e(bic)´
     }
    
     matrix colnames ic = "Classes" "LLF" "Nparam" "CAIC" "BIC"
     matlist ic, name(columns)
    But i keep getting the "option nclasses() invalid" error message. When I run individual lclogits models, manually choosing the number of "Nclasses" (i did it for 3 4 and 5) the code runs, but I am trying to get it in one go and within one table.

    I would much appreciate any input ! My idea after choosing the right number of latent class models is to proceed to play with restrictions of coefficients so as to simulate attribute non-attendance (and compare results with eaalogit model)

    Best regards,

    Michael


  • #2
    The problem is with your punctuation in the -nclasses()- option:

    Code:
    `c´
    // SHOULD BE
    `c'
    There is no´ character in Stata syntax. Just use the ordinary quote mark that, on a US keyboard, is to the right of the semi-colon [; ]. It appears that you have pulled your code from an article or book where the editors have substituted the "closed-quote" ´ character (or perhaps their software just did it automatically, like "smart quotes" in Word). That's unfortunate. (And even the Stata manuals sometimes do that!) But always, when you see that character in Stata code, replace it with an ordinary quote mark.

    Added: You have to fix this everywhere it occurs in your code. The -matrix ic = ...- command has several of these errors too. Probably best to do a global find and replace operation: it's easy to miss this when scanning by eye.

    Some hints to improve your posting in the future. In this case, the solution to your problem was simple and the source of the problem fairly easy to spot. But in general, you will improve your chances of getting timely and helpful responses if you adhere to some rules that make it easier for others to understand your situation. You can find these guidelines in the Forum FAQ, which I urge you to read. But in particular:

    1. Don't use abbreviations that are not common in statistics, Stata, or ordinary life. What on earth is a DCE? Spell it out in words. If you need to use the term frequently, then give the abbreviation in parentheses after the spell-out on first use, and then use the abbreviation after that.

    2. When you provide references, make them full references. It may be that in your field everybody knows what "
    Pacifico, D. and Yoo, H. (2013)" is. But this is a multi-disciplinary, international forum, and the rest of us haven't the vaguest idea what you're talking about here. Make the reference complete enough that a person who has never heard of it could find it. Better still, provide a link (if one is publicly available) to the article itself.
    Last edited by Clyde Schechter; 16 Aug 2018, 21:20.

    Comment


    • #3
      Thank you for the quick answer Clyde, I much appreciate the help. And sorry, I should have been more explicit, DCE are Discrete Choice Experiments, I am looking to calculate the willingness to pay for certified (sustainable) tuna, taking into account possible heuristic strategies taken by respondents during their choice experiment (called attribute non-attendance in the specialized literature).

      Here is the link for the Pacifico, D. and Yoo, H. (2013) paper entitled "A Stata module for estimating latent class conditional logit models via the Expectation-Maximization algorithm". http://dro.dur.ac.uk/19354/1/19354.pdf

      My goal is to compare the results of the lclogit (after constraining coefficients so as to model for non-attendance of attributes) vs the results of the eaalogit stata command which does it automatically - same as looking into clogit results and mixlogit command results which don't take into consideration attendance... and finally compare WTP, look for probabilities of non-attendance of certain attributes, and the policy implications for the fisheries certification scheme!

      Thanks,

      Michael

      Comment


      • #4
        I have replaced the wrong symbol for the one you corrected me for, and the code actually runs the model, but it fails at the -matrix ic = line (where i also replaced all the faulty symbols), this is how the code ended up looking.

        Code:
        forvalues c = 2/10 {
        lclogit choice bycatch freshness origin fcert , group(card) id(id) nclasses(`c') membership( age sex educ inc ) seed(1234567890)
         matrix b = e(b)
         matrix ic = nullmat(ic)\`e(nclasses)',`e(ll)',`=colsof(b)',`e(caic)',`e(bic)'
         }
        
         matrix colnames ic = "Classes" "LLF" "Nparam" "CAIC" "BIC"
         matlist ic, name(columns)
        But i get the "matrix ic)`e(nclasses not found" error message, which most likely might also stem from notation... as coding is something that I am working on, I have been advancing by running each lclogit model manually, but still, i would much appreciate some light into the issue.

        Again, Thanks.

        Comment


        • #5
          Code:
          matrix ic = nullmat(ic)\`e(nclasses)',`e(ll)',`=colsof(b)',`e(caic)',`e(bic)'
          is tricky. The backslash character, \, in addition to being a matrix adjoining operator, is also an "escape" character. So, whenever Stata encounters the sequence \`, it interprets it not as the backslash followed by the start of a local macro reference, but rather as a literal ` character. Notice that the error message you got refers to "matrix ic)`e(nclasses," which does not actually occur in your code. That's because Stata has changed matrix adjunction + start of a macro into literal character `, and the result is not legitimate syntax. (This problem arises most often with file pathnames in Windows, where \ is still further overloaded as the path separator. This is the first time I've seen it with matrix adjunction.)

          The solution is simple: just put a blank space between the \ and the ` and you'll be fine.

          Alternative solution, which works more generally in situations: use \ to "escape" \'s role as an escape character. That is, \\` will be interpreted as literal (not escape) \ followed by `.

          When this problem arises in Windows path names in Stata, the best solution is actually to use / instead of \ in the pathname. Although Windows doesn't recognize / as a path separator, Stata's file handling routines for Windows all know about this problem and appropriately feed \ to Windows when you type / in a file pathname, so you don't have to worry about the escape character problem when you write pathnames if you use / instead of \. N.B.: file pathnames are the only place where you can substitute / for \ in Stata. Anywhere else it will cause errors.

          Comment


          • #6
            Thank you for the help, as I write this the code has automatically jumped into the second model. Much appreciated.

            Comment


            • #7
              I can confirm that it all worked! the 10 models appeared in a nicely built table, Is it possible to export that table using outreg2?

              I am familiar with exporting the outputs of regressions and using the append command to build a final table, but that is a different process than what I am attempting to do here.

              Again, any help much appreciated!

              Michael

              Comment


              • #8
                I don't know. I don't use -outreg2- (nor -outreg-, nor other such programs). Perhaps somebody who uses these regularly will respond.

                Comment


                • #9
                  Originally posted by Mike Tanner View Post
                  I can confirm that it all worked! the 10 models appeared in a nicely built table, Is it possible to export that table using outreg2?

                  I am familiar with exporting the outputs of regressions and using the append command to build a final table, but that is a different process than what I am attempting to do here.

                  Again, any help much appreciated!

                  Michael
                  I am also not familiar with outreg2. But it looks like it may only post results from commands that output in the usual format. WHat I mean is that most regression commands post e(b), a matrix of regression coefficients, and e(V), the variance-covariance matrix of those coefficients. The standard errors are drawn from the diagonal of e(V) (i.e. the variance of the regression coefficients; anything off-diagonal is the covariance of one coefficient with another). Most commands also post something called r(table), which is a formatted table. It's the basis of the output you see on your screen.

                  All of these things - e(b), e(V), and r(table) - are matrices. You can use putexcel to write a matrix directly to Excel. You can also use the user-written command estout (Ben Jann, available on SSC) to output those. From what I can tell (I installed lclogit and ran the example data), you want to output the matrix e(B) - note the capitalization of B!! Or, actually, you want to output e(B)' - that apostrophe indicates the transpose of that matrix. For example,

                  Code:
                  ssc install lclogit /*If not already installed*/
                  clear
                  use http://fmwww.bc.edu/repec/bocode/t/traindata.dta
                  lclogit y price contract local wknown tod seasonal, id(pid) gr(gid) ncl(3)
                  matrix list e(B)
                  So, you'll see that the matrix you just listed looks a lot like the formatted output table, only it's been transposed. Using putexcel:

                  Code:
                  /*This will output to your working directory; type cd to display that directory, or cd "some valid path name" to change directory*/
                  putexcel set output.xls
                  matrix b = e(B)'
                  putexcel A1 = matrix(b), names
                  Because I'm not familiar with this application of latent class analysis, where you are dealing with choice patterns, I'm uncertain where all the variances are. You can, in principle, calculate a standard error (and hence a p-value and 95% CI) directly from just the coefficient and its variance. But this will get you the table that lclogit displayed.
                  Be aware that it can be very hard to answer a question without sample data. You can use the dataex command for this. Type help dataex at the command line.

                  When presenting code or results, please use the code delimiters format them. Use the # button on the formatting toolbar, between the " (double quote) and <> buttons.

                  Comment


                  • #10
                    You should be able to pass the matrix containing all of your results to -estout-. While it is a different command it tends to be very good/flexible for cases like this.

                    Comment


                    • #11
                      Thanks for the input! Sadly it seems lclogit will not allow me to do all the restrictions i need to do across latent classes, thus I might have to go into gsem to be able to do what i need to do (model for attribute non-attendance in a discrete choice experiment).

                      Comment


                      • #12
                        Hi Mike,
                        I came across this post and wanted to ask a quick question. I am trying to incorporate attribute non-attendance in latent class model. Seems like you had a similar question here. Were you able to figure out how to model latent class attribute non-attendance?

                        Comment

                        Working...
                        X