Announcement

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

  • Running a loop with nested multinomial logit models

    Dear members of the list,

    In order to save time and make my do-file shorter and clearer, I'd like to run a loop that would include a list of nested models, generating marginal effects and storing them after each model.The final goal is to save the subsequent table in an Excel file.

    The list of models and coding is as follows:

    PHP Code:
    mlogit vardep X1baseoutcome(2)
    estpost marginsdydx(*) predict(outcome(2))
    estimates store m1title (Model 1)

    mlogit vardep X1 X2baseoutcome(2)
    estpost marginsdydx(*) predict(outcome(Other_studies))
    estimates store m2title (Model 2)

    mlogit vardep X1 X2 X3baseoutcome(2)
    estpost marginsdydx(*) predict(outcome(Other_studies))
    estimates store m3title (Model 3)

    mlogit vardep X1 X2 X3 X4baseoutcome(2)
    estpost marginsdydx(*) predict(outcome(Other_studies))
    estimates store m4title (Model 4)

    mlogit vardep X1 X2 X3 X4 X5baseoutcome(2)
    estpost marginsdydx(*) predict(outcome(Other_studies))
    estimates store m5title (Model 5)

    esttab m1 m2 m3 m4 m5 using "[PATH]...\Expectativas_ocuma1.csv"///
        
    cells(b(star fmt(3)) se(par fmt(2))) legend label varlabels(_cons Constantstats(r2nobaselevels unstack noomitted replace 

    It is easy to see that the lines after each model are common to all the models.Thus, it should be easy to make a loop summarising all this coding. But I do not know how to include the different models in the loop. Shall I use foreach, or different globals capturing each one the list of variables in each model?

    Thanks for your attention

    Kind regards

    Luis Ortiz



  • #2
    estout is from Stata Journal, as you are asked to explain in FAQ Advice # 12.

    Code:
    estimates clear
    local vars X1 X2 X3 X4 X5
    local regressors
    forval i=1/`=wordcount("`vars'")'{
        local regressors "`regressors' `=word("`vars'", `i')'"
        mlogit vardep `regressors', baseoutcome(2)
        estpost margins, dydx(*) predict(outcome(2))
        estimates store m`i', title (Model `i')
    }

    Comment


    • #3
      Many thanks for such a swift and effective response, Andrew.

      It works beautifully...¡ It really does.

      But I would like very to understand why, so as to reproduce the logic in other contexts.

      As far as I understand your code, "wordcount" counts the words in the first local; right? Therefore, "forval" is telling that, as far as there is an additional word in the first local, another multinomial logit model should be launched. Then, Stata needs to understand which regressors to include in each successive model. Is this the point of the additional local ('regressors')? The local 'regressors' seems to tell that there should be always an additional regressor to be included in the model (=word( `vars', `i'). Am I taking it right?

      I apologise for not having mentioned that estout comes from Stata Journal. I was not aware this is the case. Many thanks for reminding me of the need to revise the FAQs.

      All the best and thanks for your attention

      Luis Ortiz

      Comment


      • #4
        As far as I understand your code, "wordcount" counts the words in the first local; right?
        Correct.

        "forval" is telling that, as far as there is an additional word in the first local, another multinomial logit model should be launched. Then, Stata needs to understand which regressors to include in each successive model. Is this the point of the additional local ('regressors')? The local 'regressors' seems to tell that there should be always an additional regressor to be included in the model (=word( `vars', `i'). Am I taking it right?
        A few points:The first regression contains 1 regressor, that is X1. The second, two, X1 and X2, and so on.

        1. We have the word() function which will pick up the specified word in a list

        Code:
        local vars X1 X2 X3 X4 X5
        display word("`vars'", 1)
        display word("`vars'", 3)
        Res.:

        Code:
        . local vars X1 X2 X3 X4 X5
        
        . display word("`vars'", 1)
        X1
        
        . display word("`vars'", 3)
        X3

        2. We define an incremental local macro "regressors"

        Code:
        local vars X1 X2 X3 X4 X5
        local regressors
        forval i=1/5{
             local regressors "`regressors' `=word("`vars'", `i')'"
             display "i= `i'"
             display "`regressors'"
        }
        Res.:

        Code:
        . forval i=1/5{
          2.
        .      local regressors "`regressors' `=word("`vars'", `i')'"
          3.
        .      display "i= `i'"
          4.
        .      display "`regressors'"
          5.
        . }
        i= 1
         X1
        i= 2
         X1 X2
        i= 3
         X1 X2 X3
        i= 4
         X1 X2 X3 X4
        i= 5
         X1 X2 X3 X4 X5
        At the ith iteration, the macro adds the ith word of local vars to the list.
        Last edited by Andrew Musau; 16 Oct 2020, 07:21.

        Comment


        • #5
          Brilliant...¡

          Many thanks

          Luis

          Comment

          Working...
          X