# Does Mathematica have a command for producing an "Added Variable Plot" or "Partial Regression Plot"?

Stata (and maybe other packages) enables the production of an "added variable plot" (also known as partial regression plot) which displays the residuals for the dependent variable against one of the independent variables in a multiple regression, while adjusting for all of the other variables (that are not being plotted) while the slope of the line of the model is the effect of the displayed independent variable. The command in stata is avplot, discussed here and explained here and here. I see that in Mathematica LinearModelFit allows the plotting of residuals in several ways but I cannot immediately see that it can produce this plot.

• There are a couple of versions of a partial regression plot. Adding some data and the resulting image of the plot would get your question more attention. Nov 2, 2016 at 9:50

Since Edmund asks me to be specific, let me show how one creates such plot in Mathematica the hard way, assuming that Mathematica does not have a command for it.

Here are some data with strong correlation between the independent variables:

data = {{2, 2, 2}, {2, 2, 1}, {1, 3, 2}, {4, 4, 4}, {4, 4, 5}, {5, 3, 4}};

Running a linear regression reveals the coefficient for the first variable is .5:

lm = LinearModelFit[data, {x1, x2}, {x1, x2}]

We can graph everything in three dimensions with

Show[{Plot3D[lm[x, y], {x, 1, 5}, {y, 1, 5}], Graphics3D[{PointSize[.05], Point@data}]}, Axes -> True]

and we see that the model predicts the points at 1 and at 5 perfectly. But a partial residual plot hides the correlation between the first and second variables.

partialresidualplotdata = Table[{data[[j, 1]], data[[j, 3]] - Normal[lm] /. {x1 -> data[[j, 1]], x2 -> data[[j, 2]]}}, {j, Length@data}] Graphics[{PointSize[.05], Point@partialresidualplotdata}, Axes -> True]

By the way, the help page's ListPlot[lm["FitResiduals"], PlotStyle -> PointSize[.03]] is something different and does not relate the residuals to any values of the independent variables, it is just a list of them.

The partial regression plot requires us to run two more regressions, of the first against the second variable and of the third against the second, then plot those residuals. Drawing the trendline requires a third regression, of the results of the prior two:

lm1 = LinearModelFit[data[[;; , {2, 1}]], x, x];

lm2 = LinearModelFit[data[[;; , {2, 3}]], x, x];

partialregressionplotdata = Table[ {data[[j, 1]] - Normal[lm1] /. x -> data[[j, 2]], data[[j, 3]] - Normal[lm2] /. x -> data[[j, 2]]}, {j, Length@data}];

lm3 = LinearModelFit[partialregressionplotdata, x, x]

As expected, the coefficient of this last one is the same as for the variable in question in the original multiple regression. Then we graph everything:

Graphics[{PointSize[.02], Point@partialregressionplotdata, Line@Table[{n, Normal[lm3] /. x -> n}, {n, {-2, 2}}]}, Axes -> True] Well, this does it but not elegantly. Granted, doing it manually means the user retains a lot more control but this is also a lot more coding. I was asking whether Mathematica could do this more easily. I gather the answer is Negative.