Announcement

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

  • Marginal effects of interaction between continuous and factor variables in fixed effects models

    Hello. How may I test the marginal effects of my variables when there is a continuous-factor-interaction in my fixed effects model? I tried various commands (namely margins, lincom and contrast), but havn't found any hint on this issue.


    Code:
    * load data
    use http://www.stata-press.com/data/r13/nlswork
    describe
    
    * set panel structure
    xtset idcode year
    
    * fixed effects regression, with interaction of continuous and factor variable
    xtreg ln_wage c.wks_ue##i.occ_code, fe
    
    * investigate marginal effects of weeks worked last year
    
    // only factor variables are allowed
    margins wks_ue
    
    // only for continuous interaction
    lincom c.wks_ue + i.occ_code
    
    // only for factor variables
    contrast occ_code, nowald effects
    Last edited by Marco Kuehne; 28 Jan 2020, 06:21. Reason: added tags

  • #2
    To get the marginal effets of wks_ue conditional on each value of occ_code:
    Code:
    margins occ_code, dydx(wks_ue)
    The marginal effects of occ_code, if they are of interest, can be calculated conditional on whatever list of values of wks_ue you are interested in. So, for the sake of illustration, suppose you are interested in the marginal effects of occ_code when wks_ue = 1, 5, 25, and 100, you would write:

    Code:
    margins, dydx(occ_code) at(wks_ue = (1 5 25 100))

    Comment


    • #3
      Those commands work great. Thank you Clyde. But in order to understand marginal effects better, I would like to use lincom. Perhaps/probably there is a way to get equal outcomes from lincom and margins? My models extends to a sqaured term (+ interaction):

      Code:
      * load data
      use http://www.stata-press.com/data/r13/nlswork
      describe
      
      * set panel structure
      xtset idcode year
      
      * extended model
      xtreg ln_wage c.wks_ue##i.occ_code c.wks_ue##c.wks_ue##i.occ_code, fe coeflegend
      
      * this works
      qui sum wks_ue if e(sample)
      local mean_wks_ue = r(mean)
      lincom wks_ue + 2 * c.wks_ue#c.wks_ue * `mean_wks_ue'
      
      * full marginal effect does not work
      qui sum wks_ue if e(sample)
      local mean_wks_ue = r(mean)s
      lincom wks_ue + 2 * c.wks_ue#c.wks_ue * `mean_wks_ue' + 1.occ_code#c.wks_ue * 1.occ_code + 2 * 1.occ_code#c.wks_ue#c.wks_ue  * `mean_wks_ue'
      I figures out a way to save means locally. I also checked the exact names of interactions with coeflegend. But I am still not able to put everything together. Best regards

      Comment


      • #4
        -local mean_wks_ue = r(mean)s- is a syntax error: that s at the end shouldn't be there.

        You can simplify your -xtreg- command: the c.wks_ue#i.occ_code term is redundant, because it is already implied by c.wks_ue##c.wks_ue##i.occ_code.

        Let's say we want to get the marginal effect of wks_ue conditional on occ_code == 3 and wks_ue == 10. Then we want the partial derivative of the outcome with respect to wks_ue conditional on occ_code == 2. So in -lincom- that would be:

        Code:
        lincom _b[wks_ue] + _b[3.occ_code#c.wks_ue]  + 2*_b[c.wks_ue#c.wks_ue]*10 + 2* _b[3.occ_code#c.wks_ue#c.wks_ue] * 10
        That should match the outcome from
        Code:
        margins, dydx(wks_ue) at (occ_code = 3 wks_ue = 10)

        Comment


        • #5
          Awesome, thanks for improving the code and reducing it to the threefold interaction. There is a slight different between lincom and margins estimations (6th decimal). I found that it's due to the underlying distribution (lincom with t and margins with z). Should I be worried any time about this difference?


          lincom is great in understanding the marginal effect, but margins is easier to handle if I want to repeat the calculation for all factor levels and different statistical moments.

          I know that I can pass a vector/list for the factor, as:

          Code:
           
           margins, dydx(wks_ue) at (occ_code = (1 2 3) wks_ue = 10)
          Further I want to check all effects at different statistical moments, e.g. mean, median, percentiles. What is the best practice to calculate and store those? In locals? Since there are many combinations I need to automate as much as possible of this procedure.
          Code:
             
           margins, dydx(wks_ue) at (occ_code = (1 2 3) wks_ue = (mean, median, perc10, perc99))
          Kind regards, Marco

          Comment


          • #6
            There is a slight different between lincom and margins estimations (6th decimal). I found that it's due to the underlying distribution (lincom with t and margins with z). Should I be worried any time about this difference?
            Almost certainly this is negligible. But if the first five decimal places are all zero, then the 6th decimal place is the only significant figure, so, yes, it would matter. But there is almost nothing in the world where 6 sigificant figures of accuracy is necessary. So first, worry about how many significant figures you go out to to find the discrepancy. Then, you have to ask yourself whether from a practical perspective, that difference matters. Most things in the world are fine when given to 2 or 3 significant figures--but you have to know what uses you will put the results to in order to be sure.

            lincom is great in understanding the marginal effect, but margins is easier to handle if I want to repeat the calculation for all factor levels and different statistical moments.
            Yes. That's why, in my view, the -margins- command is the single most important Stata improvement of the decade! I think that everybody should do a small number of marginal effects using -lincom- to learn what they are; after that -margins- should always be used. It's easier, faster, and far less error-prone.

            What is the best practice to calculate and store those? In locals?

            Yes, local macros are your best bet, by far.

            Note also that for the kinds of statistics that are allowable in -collapse- you can also use notation like:
            Code:
            margins, dydx(wks_ue) at(occ_code = (1 2 3) (mean) wks_ue)
            (For percentiles, the notation is (p10), not perc10.) However, -margins- will accept only one of these at a time. I think it's a matter of taste whether to calculate these statistics first and store them in local macros to make a numlist for a single run of -margins-, or do a series of separate -margins- commands one at a time. The former approach probably runs faster in a large data set with a complicated model, but the latter produces a series of shorter output tables that are probably easier to read than one long one.

            Comment

            Working...
            X