Announcement

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

  • margins, dydx() question

    Dear Statalist,

    I ran a zero-inflated negative binomial model, and want to show average marginal effects (AME) for main independent variables. I am seeking to use actual observations in the dataset and get the average, and believe that AME achieves the goal (unlike Marginal Effects at Means (MEM)). Assuming this is correct, I first ran ZINB:

    zinb DV IVlist i.year, inflate(IVlist) robust

    Some of the variables in the list of independent variables are dichotomous. Let's say it is a gender variable indicating male/female, with gender=0 male and gender=1 female.

    I now want to show the change as the gender variable goes from 0 to 1, both numerically and visually (the latter involves marginsplot). However, I am confused about the following: What exactly is the difference between margins, dydx(gender) and margins, dydx(gender) at(gender=(0 1)) ? To show the average marginal effects as gender goes from 0 to 1, I think I should do the latter, correct? But when I calculate the change associated with the latter code, it does not match the number I get as a result of margins, dydx(gender).

    Sorry that I couldn't use real data as it is confidential, but I hopefully provided enough background conceptually for a discussion. Thank you very much!

  • #2
    First question is whether your -zinb- command was set up correctly for use with margins. If the variable was specified as just gender, then your -margins- results will be incorrect. It has to be specified as i.gender so that -margins- will know that this is a discrete variable. The definition of marginal effects for discrete variables is different from the definition for continuous variables. If your -zinb- command was set up correctly, then the command you want to use to get the marginal effect of gender is
    Code:
    margins, dydx(gender)
    which will give you the expected difference in DV between gender = 1 and gender = 0, which is the correct definition for the marginal effect of a discrete variable.

    The command -margins, dydx(gender) at (gender = (0 1))- does not make sense. You are then asking Stata to pretend that gender could also take on the value 2, and calculate the change in expected value of DV between gender = 1 and gender = 2. It will, in fact, do those calculations, but the result doesn't mean anything.

    Comment


    • #3
      Thank you so much! It clears up some confusion. Yes ZINB was correctly specified with an i prefix but it is always good to reconfirm that.

      If I may ask you one follow-up question, is there any way we can visualize the change as gender goes from 0 to 1? As margins, dydx(gender) just gives the number associated with the change, and the marginsplot command after that produces what is basically a datapoint on a plot. What I mean by visualization is something akin to what would be produced when we run:

      . margins, at(gender=(0 1)) atmeans
      . marginsplot

      In this case, we produce a plot that visualizes a change as gender goes from 0 to 1. I think I am just being super confused, but is it possible to something similar when using margins, dydx(), based on real observations in the dataset?

      Again, thank you so very much for your kind response.

      Comment


      • #4
        Just as an update, I just realized that running margins, at(gender=(0 1))produced the same result as: margins, dydx(gender).

        In which case I think I can produce a plot that I was envisioning!

        Comment


        • #5
          I don't understand what you want. The code you show in #3 looks to me like it would be exactly the right way to visualize the change in outcome as you go from gender = 0 to gender = 1. What about it is not satisfactory?

          When you run -margins, dydx(gender)-, you are just getting a single marginal effect. It is, in fact, identical to the difference between the outcome when gender = 1 and when gender = 0 (adjusted for any other variables in your regression model). The marginal effect doesn't take on two values: it is the single difference between the two values.

          Added: Crossed with #4.

          Comment


          • #6
            Thank you very much for clearing up some confusion!

            If I may bring up one final issue -- I know that margins calculates dydx() differently for continuous and for factor variables, as the Stata manual nontes. We've been talking about a discrete variable (gender). Count variables, as far as I understand, is also discrete, but is it considered continuous by Stata? For example, I have a count variable, called number_of_events.

            margins, dydx(number_of_events) produces a number, but obviously it is different from the difference in the outcome of when number_of_events=1 and when number_of_events= 0. How do we correctly interpret dydx result when the variable is a count variable?

            Sorry, I did read the stata manual for margins multiple times, but can't seem to be able to wrap my head around it. Thank you again Dr. Schechter! If there's a suggestion for further reading (other than Stata manual and Dr. Williamson's manual) I'd love to read it.

            Comment


            • #7
              Stata has two ways of calculating marginal effects:

              1. Evaluate the outcome at the specified value(s) of x and then do so again at x+1 and subtract the results.

              2. Apply calculus to determine the first derivative of the outcome with respect to the predictor variable at the specified value of x.

              The first is appropriate for discrete variables, and the second is appropriate for continuous variables. The only way -margins- can tell which variables are continuous and which are discrete is through factor-variable notation. If you specify the variable as i.x, it is discrete. If you specify it as c.x it is continuous. If you don't specify, then, by default, x is taken to be continuous, except if it appears in an interaction term x#u (or x##u), in which case it is taken to be discrete.

              So if you specify i.number_of_events in the regression command, you will get #1. If you specify c.number_of_events, or just number_of_events, you will get #2.

              Comment


              • #8
                Right. I believe the count variable I'm concerned about is considered as a continuous variable by Stata. Thank you very, very much for your time and insight. I really appreciate it!

                Comment


                • #9
                  Count independent variables are probably usually treated as continuous. But continuous variables do not always have linear effects. So, you may do some sort of transformation. For example, you might do i.nevents, treating it as a series of dummy variables. Or, you might add nevents^2 to the model. And so on.

                  So, as independent variables go, I don't think count variables are really much different than continuous variables, but keep in mind that continuous variables do not necessarily have linear effects.
                  -------------------------------------------
                  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