Announcement

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

  • Marginal effects results - want to calculate the difference between two variables marginal effects

    Hi all,

    I would like to save my marginal effects results (coefficients + CI's) ideally, as a model. I need to graph the difference in marginal effects between two scenarios (exactly what the marginsplot output gives but I need to do this as the difference). So, I was hoping I can save the results as model1 and model2 and generate a new model12 = model1-model2 (I am not sure if this is something I can do in STATA) and then plot the results. If this is not possible, any other solution would be appreciated!

    I also tried storing my results with the command 'est store' but this only saved the coefficients + SE and, I need CI's. I also don't think I can manipulate anything with the 'store' command. I know there have been similar post to this however, the code posted on these forums did not work for me.

    The code I have:
    (Scenario 1)
    oprobit ncat1 i.nsex i.nage i.nincome

    margin
    marginsplot

    (Scenario 2)
    oprobit ncat2 i.nsex i.nage i.nincome

    margin
    marginsplot


    If there is a way I can plot the difference between these two plots in a more simpler way, that would also work! p.s. ncat1 and ncat2 have the exact same levels/orders, they are just separated by scenarios.

  • #2
    I know of no really simple way to do this. What you can do is a series of steps:

    1. The -margins- command has an undocumented -saving()- option. So add that to your -margins- commands, specifying a a file to save each set of margins results in. The margins results will be saved as a Stata data set.

    2. The variable names in those data sets are not convenient to work with. But if you open them, you will easily be able to recognize which variables in the data set correspond to the various variables in your -margins- command and the results, and so on. Rename those variables to something convenient for you. Then re-save each of those data sets.

    3. -merge- the two data sets together. Now you have everything in one place and you can calculate the differences the way you would calculate differences between any variables in Stata.

    4. Use the -graph twoway- command to make the plots you need.

    Comment


    • #3
      I suggest you check out this excellent paper by Mize, Doan, and Long, "A General Framework for Comparing Predictions and Marginal Effects across Models"

      https://journals.sagepub.com/doi/ful...81175019852763

      They have this ingenious approach where they use gsem to test hypotheses like yours. I believe your question falls under example 6.4, which compares effects of the same predictor on different outcome variables.

      Mize is great about providing replication materials for all of his work at

      https://www.trentonmize.com/research

      Specifically, check out

      https://www.trentonmize.com/software/mecompare

      https://drive.google.com/drive/folde...fmxmfOB41AyA7D

      Here is the code he presents for 6.4, I don't think it would be hard to adapt. You also need to install Long & Freese's spost13 (findit spost13_ado).

      Code:
      capture log close
      log using cme-6_4-Variable-Choice-Outcomes, replace text
      
      // CME: Comparing Marginal Effects Across Models
      // Ex 6.4 - Variable choice: Comparing effects on different outcomes
      
      version     14.2 // allow non-15 users
      clear       all
      set more     off
      set         linesize 80
      set         scheme cleanplots
      graph       set eps fontface Times
      graph       set window fontface Times
      
      local pgm "cme-6_4-Variable-Choice-Outcomes"
      local dte "2019-03-20"
      local who "TDM JSL TDM JSL TDM" // add last person making change
      local tag "`pgm'.do `who' `dte'"
      
      //  ===============================================================
      //  #1 Data management
      
      use gss_cme, clear
      
      drop if missing(mntlhlth, physhlth, woman, married, age, faminc, ///
                      race, college, parent, reltrad)
      
      codebook mntlhlth physhlth woman married age faminc ///
               race college parent reltrad, compact
      
      //  ===============================================================
      //  #2 Simultaneous Estimation with GSEM
       
      //  Note: vce(robust) required when same observations across models
      
      gsem (mntlhlth <- i.woman i.married c.age faminc ///
                        i.race i.college i.parent i.year, nbreg) ///
           (physhlth <- i.woman i.married c.age faminc ///
                        i.race i.college i.parent i.year, nbreg), ///
           vce(robust)
        
      est store gsemmodel
      
      //  ===============================================================
      //  #2 Compute and compare marginal effects
      
      //  ===============================================================
      //  #2a Effects for binary regressors
      
      mlincom, clear
      foreach var in woman married parent college {
          qui est restore gsemmodel
          margins, dydx(`var') post        
          qui mlincom 1,   rowname(`var':MentHlth) stat(est se p) add
          qui mlincom 2,   rowname(`var':PhysHlth) stat(est se p) add
          qui mlincom 1-2, rowname(`var':Difference) stat(est se p) add
      }
      
      //  ===============================================================
      //  #2b Effect of continuous regressors + SD
      
      foreach var in age faminc {
          qui est restore gsemmodel
          qui sum `var'
          margins, at(`var'=gen(`var')) at(`var'=gen(`var' + `r(sd)')) post    
          qui mlincom 2-1, rowname(`var' + SD:MentHlth) stat(est se p) add
          qui mlincom 4-3, rowname(`var' + SD:PhysHlth) stat(est se p) add
          qui mlincom (2-1)-(4-3), rowname(`var' + SD:Difference) stat(est se p) add
      }
        
      //  ===============================================================
      //  #2c Effects for multicategory regressors
      
      est restore gsemmodel
      margins i.race, post
      
      qui {
          mlincom 2-1, rowname(blackVwhite:MentHlth) stat(est se p) add
          mlincom 5-4, rowname(blackVwhite:PhysHlth) stat(est se p) add
          mlincom (2-1)-(5-4), rowname(blackVwhite:Difference) stat(est se p) add
      
          mlincom 3-1, rowname(otherVwhite:MentHlth) stat(est se p) add
          mlincom 6-4, rowname(otherVwhite:PhysHlth) stat(est se p) add
          mlincom (3-1)-(6-4), rowname(otherVwhite:Difference) stat(est se p) add
      
          mlincom 3-2, rowname(otherVblack:MentHlth) stat(est se p) add
          mlincom 6-5, rowname(otherVblack:PhysHlth) stat(est se p) add
          mlincom (3-2)-(6-5), rowname(otherVblack:Difference) stat(est se p) add
      }
      
      mlincom, twidth(20) ///
          title(AMEs for mental and physical health and cross model differences)    
      
      est restore gsemmodel
      
      log close
      exit
      Last edited by Richard Williams; 19 Aug 2021, 13:21.
      -------------------------------------------
      Richard Williams, Notre Dame Dept of Sociology
      StataNow Version: 19.5 MP (2 processor)

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

      Comment

      Working...
      X