Announcement

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

  • STATA SE 13, how to get marginsplot for all categories of dependent variable: multiply-imputed mlogit, using mimrgns command

    Dear STATA users,

    I would much appreciate your help with my analysis which goes as follows:

    I run mlogit using multiply chained imputed data:

    Code:
    mi set mlong
    
    mi register imputed volba migranti_squared lidr i_prijmu migranti_2011 egal egalitarstvi euroskepticismus_rek populismus apatie narodovectvi subjektivni_politicka_kompetence status vzdelani_rek pohlavi_rek vek_rek bydliste_2kat religiozita_rek cynismus
    
    mi impute chained (mlogit) volba (regress) migranti_squared (regress) lidr (regress) i_prijmu (regress) migranti_2011 (regress) egal (regress) egalitarstvi (regress) euroskepticismus_rek (regress) populismus (regress) apatie (regress) narodovectvi (regress) subjektivni_politicka_kompetence (regress) status (regress) vzdelani_rek (mlogit) pohlavi_rek (regress) vek_rek (mlogit) bydliste_2kat (mlogit) religiozita_rek (regress) cynismus, add(10) rseed (54321) savetrace (trace1, replace)
    
    mi estimate, dots: mlogit volba migranti_squared lidr i_prijmu migranti_2011 egal egalitarstvi euroskepticismus_rek populismus apatie narodovectvi subjektivni_politicka_kompetence status vzdelani_rek i.pohlavi_rek vek_rek i.bydliste_2kat i.religiozita_rek cynismus [pweight= VAHA], base (1)
    Then I want to create marginsplot to plot predictive margins by independent variable "euroskepticismus_rek" (values 0 to 10) for each category of my dependent variable "volba" (3 categories - 1,2,3)
    I am using mimrgns command:

    Code:
    mimrgns, at (euroskepticismus_rek = (0 (1) 10)) predict(pr) cmdmargins vsquish
    marginsplot, noci scheme(sj) name(mimrgnsplot)
    Click image for larger version

Name:	mimrgnsplot.png
Views:	1
Size:	17.9 KB
ID:	1489555


    I am able to get the plot of predictive margins (see the picture above) for the first category of dependent variable (which is set as a base category in mlogit).
    Is there a way to modify my code so it creates the same plot for every category of my dependent variable?
    Thank you in advance for any suggestions.

  • #2
    The default behavior of margins is to calculate predicted probabilities for each value of the dependent variable. I have to believe this behavior would be retained in mimrgns. By including the option predict(pr), though, you actually overrode the default, and asked for the probability of outcome 1. This is detailed in the post-estimation documentation for margins.

    I'd try it without the predict(pr)option, but if that doesn't work, try

    Code:
    mimrgns, at (euroskepticismus_rek = (0 (1) 10)) predict(outcome(1) outcome(2) outcome(3)) cmdmargins vsquish
    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


    • #3
      I unsuccessfully tried both options. If predict (pr) is omitted than the command creates linear predictor (see the picture). Suggested code leads to error message: "only one of outcome(1), outcome(2), or outcome(3) is allowed
      r(198);"

      Click image for larger version

Name:	mimrgnsplot - linear predictor.png
Views:	1
Size:	16.7 KB
ID:	1489564

      Comment


      • #4
        OK, so it's a Stata 13 issue. I think you can plot the 3 individual graphs, save each one, then use the command combomarginsplot (available on SSC).
        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


        • #5
          I tried it and by using the code below I am getting the following error "option saving() not allowed r(198);"

          Could you see where I am making a mistake?

          If I delete saving() and combomarginsplot line then the code works for creating separate graphs.

          Code:
          mimrgns, at (euroskepticismus_rek = (0 (1) 10)) predict(outcome(1)) cmdmargins vsquish saving(file1, replace)
          marginsplot, noci scheme(sj) name(mimrgnsplot1)
          
          mimrgns, at (euroskepticismus_rek = (0 (1) 10)) predict(outcome(2)) cmdmargins vsquish saving(file2, replace)
          marginsplot, noci scheme(sj) name(mimrgnsplot2)
          
          mimrgns, at (euroskepticismus_rek = (0 (1) 10)) predict(outcome(3)) cmdmargins vsquish saving(file3, replace)
          marginsplot, noci scheme(sj) name(mimrgnsplot3)
          
          combomarginsplot file1 file2 file3, labels("volici" "PRR" "nevolici") noci

          Comment


          • #6
            Originally posted by Filip Safr View Post
            I tried it and by using the code below I am getting the following error "option saving() not allowed r(198);"

            Could you see where I am making a mistake?
            You are not making any mistake; mimrgns does not support the saving() option. I am afraid, there is no quick work-around for this.

            Basically, you would need to create the files yourself. After each call to mimrgns, you would need to get the relevant information, that is, point estimates, at-positions, standard errors, etc. from r(table) and save this to a file that resembles what margins with the saving() option would create. I might find half an hour to try this tomorrow, but I cannot promise yet.

            Best
            Daniel

            Comment


            • #7
              Here is an example of how to do what I have outlined in #6.

              Code:
              // example data
              sysuse auto , clear
                  // impute
              set seed 42
              mi set flong
              mi register imputed rep78
              mi impute chained (mlogit , augment) rep78 = mpg i.foreign , add(5)
              
              // create the files for -combomarginsplot-
              
              // Step 1: get margins , saving() file template
              mlogit rep78 mpg i.foreign
              margins , at(mpg = (42 73)) saving(margins_saving)
              
              // Step 2: estimate the model of interest
              mi estimate , saving(miestfile) esample(esample) : ///
                  mlogit rep78 mpg i.foreign
                  
              // Step 3: get predicted values from -mimrgns- 
              forvalues i = 1/5 { // loop over outcome levels
                  mimrgns using miestfile , esample(esample) ///
                          at(mpg =(42 73)) predict(outcome(`i'))
                  matrix rtable = r(table)' // store -mimrgns- results
                  preserve
                  use margins_saving , clear // get template file
                  svmat rtable // add -mimrgns- results
                  replace _margin    = rtable1 // point esimtate
                  replace _se        = rtable2 // se
                  replace _statistic = rtable3 // t/z
                  replace _pvalue    = rtable4 // p
                  replace _ci_lb     = rtable5 // CI
                  replace _ci_ub     = rtable6 // CI
                  drop rtable*
                  save margins_saving`i' // save updated values 
                  restore
              }
              
              // Step 4: feed the files to -combomarginsplot-
              combomarginsplot margins_saving1 ///
                               margins_saving2 ///
                               margins_saving3 ///
                               margins_saving4 ///
                               margins_saving5
              Best
              Daniel

              Comment


              • #8
                I'll definitely try it during next week. Thank you both.

                Comment


                • #9
                  One last question which is slightly off topic considering the name of the thread. Can you advise me how to create a graph to illustrate model-predicted nonlinear relationship? For example between independent variable migranti_squared and one of the categories of dependent variable volba.

                  How should I modify my code?

                  Code:
                  mimrgns, at (migrants_squared = (1 (5) 80)) predict(outcome(2)) cmdmargins vsquish
                  marginsplot, noci scheme(sj) name(mimrgnsplot1)

                  Comment


                  • #10
                    margins cannot correctly recognize squared terms when you do not use factor variable notation, therefore, mimrgns cannot either. There might be a workaround for that, too, but it is less clear than what I have shown above and (much) more cumbersome to implement. Sorry.

                    Best
                    Daniel

                    Comment


                    • #11
                      If I use the variable with factor notation than I am getting the following error "convergence not achieved model did not converge on m=1 r(430)". As far as I know it could happen when many 0s and 1s are present in a dataset so I tried to transform my data (variable+1) to avoid this and see if it helps but it did not.

                      Comment

                      Working...
                      X