I'm trying to figure out how to find the marginal effect of an interaction term from a restricted cubic spline in a non-linear model. The post Nonlinear effect in an interaction term is a good start on modeling the nonlinear effects and how to get plots, but does not address finding the marginal effect.

The package postrcspline in STATA has a function mfxrcspline which "displays the marginal effect of a restricted cubic spline," which is exactly what I am after. (See Figure 1 below)

R does not seem to offer this feature as conveniently ,so I'm trying to figure out how to get these same results.

As I understand it, suppose I have a multi-variable regression with restricted cubic splines and an interaction:

$$y = \beta_{0} + \beta_{1}x1 + \beta_{2} \mathcal{f}(x2) + \beta_{3} \mathcal{f}(x2) \cdot x1 + \epsilon$$

where $\mathcal{f}(x2)$ is a spline of the time-series (year)

The marginal effect of $\frac{\partial y}{\partial x1}$ is:

$$\frac{\partial y}{\partial x1} = \beta_{1} + \beta_{3} \mathcal{f}(x2)$$

where $\beta_{3}$ is the coefficient on the spline and $ \mathcal{f}(x2)$ is a design matrix for each year in the regression that causes the slope to change for each $y$.

To say in words, I would like to find the marginal effect of $y$ for each year $x2$ in the spline given $\beta_{3}$.

In other words, it shows for each value of the spline variable how much the expected value of your explained variable changes for a unit change in the spline variable. It is the first derivative of the curve.

This appears to be simple matrix multiplication to plot the marginal effect, but I'm not sure how to statistically do this.

Here is a plot to illustrate what I'm after:

Figure 1: The left plot shows the results of the regression using a restricted cubic spline and the right provides the marginal effect--note the changes on the y-axis. Figure 1

Here is an R example to demonstrate the nonlinear effect from the regression (left plot in Figure 1):

# Fit a complex model and approximate it with a simple one
x1 <- runif(200)
x2 <- runif(200)
y <- x1 + x2 + rnorm(200)
f <- ols(y ~ x1 + rcs(x2,4)  + rcs(x2,4)*x1)
ddist <- datadist(x1,x2)

enter image description here

  • 1
    $\begingroup$ Note that such marginal effects seem to only be of interest in econometrics and I'm not completely sure why they are of such interest there. In other fields we tend to rely on partial effects, which are very easy to interpret. $\endgroup$ – Frank Harrell Feb 27 '16 at 13:14
  • 1
    $\begingroup$ My Internet searching just now suggests that the terminology partial effect and marginal effect--which I had taken for granted in these matters--may not be as well established as I had thought, nor entirely clear. This Stata Journal article, for example, employs the more descriptive terms marginal effect at the mean (MEM) and average marginal effect (AME); are these what are meant respectively by 'partial effect' and 'marginal effect' as used in this post? Also, Amstell, is it fair to say you are seeking a causal effect? $\endgroup$ – David C. Norris Feb 29 '16 at 11:45
  • 1
    $\begingroup$ Amstell, I think your difficulty arises from putting the 'cart' of splines ahead of the 'horse' of a well examined and clearly defined basic concept. The presentation you cite is laser-focused on splines, to the exclusion of any close examination of what is meant by 'marginal effects'. In particular, nowhere does it address the distinction between MEM and AME. I believe the first step for you will be to decide precisely which type of 'marginal effect' you are looking for. Once you've done that, I predict that the matrix math will all fall into place for you. $\endgroup$ – David C. Norris Mar 1 '16 at 5:27
  • 1
    $\begingroup$ @DavidC.Norris Thank you for you input. I believe I have clearly defined the marginal effect as "for each value of the spline variable how much the expected value of your explained variable changes for a unit change in the spline variable. It is the first derivative of the curve." This is defined in the presentation and is further clarifying of my question. The issue that arises after defining marginal effect, as you suggest, is to find the matrix math that accomplishes this impact; this is where I am stuck and am reaching out for help. $\endgroup$ – Amstell Mar 1 '16 at 18:01
  • 2
    $\begingroup$ A 1 unit change is not a first derivative. $\endgroup$ – Frank Harrell Nov 21 '16 at 16:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.