Announcement

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

  • Why can't I run the margins dydx command twice for different sets of covariates?

    So for some context, here is my regression model (a GLM estimation):

    Code:
    glm racial_support_index i.immigrant_status i.francophone_status i.region i.gender_status i.age_group i.education_status i.religious_status i.income_group i.urban_status i.party_id_status i.soc_net_vis_status c.ideology_index, family(binomial) vce(robust)
    and here are the margins commands I run on this regression (demonstrating average marginal effects):

    Code:
    margins, dydx(immigrant_status region francophone_status gender_status age_group education_status) post
    estimates store Marg3P1
    margins, dydx(religious_status income_group urban_status party_id_status soc_net_vis_status) post
    estimates store Marg3P2
    When I get to the second margins command line, however, I get the following error:
    Code:
    option dydx() not allowed
    The margins dydx command works fine if I run all the covariates in a single margins dydx command, but separating them into two results in the error mentioned. What gives? I am trying to run them separately since when I run
    coefplot on the margins, I can break up the covariates into two plots (each with a separate coefplot command run on them).
    Last edited by MD Mubtasim-Fuad; 02 Apr 2022, 19:19.

  • #2
    The post option wipes out the estimation results and replaces them with the margins results. After you run the glm command, do something like

    estimates store model1

    Then, before running the 2nd margins command, do

    estimates restore model1
    -------------------------------------------
    Richard Williams, Notre Dame Dept of Sociology
    StataNow Version: 18.5 MP (2 processor)

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

    Comment


    • #3
      The problem here is a misunderstanding of how margins works.
      The way it works is
      1) uses the e(b) and e(V) matrixes from a previously estimated model, as well as all the data referred to by these matrices.
      2) Prepares and estimates numerical derivatives to obtain the marginal effects of interest.
      Now, when you use "post" , margins will replace e(b) and e(V) with something new. The margins you estimated. So, when you try to call on margins again, it doesn't know what to use to make marginal effects, because the original e(b) and e(V) are not there.

      Solution: Store your original results

      Code:
      reg y x1 x2 c.x1#c.x2
      est sto m1
      margins, dydx(x1) post
      est sto m2
      est restore m1
      margins, dydx(x2) post
      est sto m3
      HTH

      Comment

      Working...
      X