Announcement

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

  • Plotting non-linear marginal effects with an interaction term using -Marginsplot-

    Hi all,

    I have a regression model with two variables that have quadratic terms for each and includes an interaction term between them. I am trying to create something similar to the following figure which shows the marginal effect of X1 on Y as X1 changes:

    Click image for larger version

Name:	ME_SEQuestion.png
Views:	4
Size:	6.5 KB
ID:	1556990


    where I can observe the nonlinearity of X1 across its own range and also look at how it changes with different values of X2, the variable it is interacted with (hence the curve shifting for different values of X2). I am hoping to be able to create something like this using Margins plot. I have the following MWE below and have attached the excel file I use as well.

    Code:
    // Data!
    
    import excel "~\SE Question ME.xlsx", sheet("SE Question ME") firstrow clear
    
    egen countryid=group(Country)
    
    foreach v in Y { //log a set of variables
        gen l`v' = log(`v')
    }
    
    xtset countryid Year
    
    // Regression Analysis
    
    xtreg lY c.X1##c.X2 c.X1#c.X1 c.X2#c.X2, fe
    
    margins, dydx(X1) at(X2=(350(25)550)) level(90)
    marginsplot, yline(0)
    
    margins, dydx(X1) at(X1=(0(2)30)) level(90)
    marginsplot, yline(0)
    This code generates the following regression output:

    Click image for larger version

Name:	ME_SEQuestion2.png
Views:	1
Size:	81.2 KB
ID:	1556983


    And the two margins plot, in order of X1 over X2 and X1 over X1 are

    Click image for larger version

Name:	ME_SEQuestionX1X2.png
Views:	3
Size:	56.1 KB
ID:	1556988 Click image for larger version

Name:	ME_SEQuestionX1X1.png
Views:	3
Size:	57.4 KB
ID:	1556989

    My main questions are:
    1. Why does the second -marginsplot- not show any non-linearity even though the regression output clearly shows diminishing returns/a negative quadratic term?
    2. How would I be able to combine these two plots in a way that is similar to the first figure?
    Attached Files

  • #2
    Because you are asking for the derivative with respect to x1, which in linear in x1. Perhaps you want:

    Code:
    margins, at(x1 = (0(2)30) x2 =(350 450 550))
    marginsplot
    Click image for larger version

Name:	Graph.png
Views:	1
Size:	72.3 KB
ID:	1557112

    Comment

    Working...
    X