Draw a 3-D regression plot for two predictors from any linear or nonlinear lm or glm object
Draw a 3-D regression plot for two predictors from any linear or nonlinear lm or glm object
This allows user to fit a regression model with many variables and then plot 2 of its predictors and the output plane for those predictors with other variables set at mean or mode (numeric or factor). This is a front-end (wrapper) for R's persp function. Persp does all of the hard work, this function reorganizes the information for the user in a more readily understood way. It intended as a convenience for students (or others) who do not want to fight their way through the details needed to use persp to plot a regression plane. The fitted model can have any number of input variables, this will display only two of them. And, at least for the moment, I insist these predictors must be numeric variables. They can be transformed in any of the usual ways, such as poly, log, and so forth.
plotx1: name of one variable to be used on the x1 axis
plotx2: name of one variable to be used on the x2 axis
drawArrows: draw red arrows from prediction plane toward observed values TRUE or FALSE
plotPoints: Should the plot include scatter of observed scores?
npp: number of points at which to calculate prediction
x1lab: optional label
x2lab: optional label
ylab: optional label
x1lim: optional lower and upper bounds for x1, as vector like c(0,1)
x2lim: optional lower and upper bounds for x2, as vector like c(0,1)
x1floor: Default=5. Number of "floor" lines to be drawn for variable x1
x2floor: Default=5. Number of "floor" lines to be drawn for variable x2
pch: plot character, passed on to the "points" function
pcol: color for points, col passed to "points" function
plwd: line width, lwd passed to "points" function
pcex: character expansion, cex passed to "points" function
llwd: line width, lwd passed to the "lines" function
lcol: line color, col passed to the "lines" function
llty: line type, lty passed to the "lines" function
acol: color for arrows, col passed to "arrows" function
alty: arrow line type, lty passed to the "arrows" function
alwd: arrow line width, lwd passed to the "arrows" function
alength: arrow head length, length passed to "arrows" function
linesFrom: object with information about "highlight" lines to be added to the 3d plane (output from plotCurves or plotSlopes)
lflwd: line widths for linesFrom highlight lines
envir: environment from whence to grab data
...: additional parameters that will go to persp
Returns
The main point is the plot that is drawn, but for record keeping the return object is a list including 1) res: the transformation matrix that was created by persp 2) the call that was issued, 3) x1seq, the "plot sequence" for the x1 dimension, 4) x2seq, the "plot sequence" for the x2 dimension, 5) zplane, the values of the plane corresponding to locations x1seq and x2seq.
Details
Besides a fitted model object, plotPlane requires two additional arguments, plotx1 and plotx2. These are the names of the plotting variables. Please note, that if the term in the regression is something like poly(fish,2) or log(fish), then the argument to plotx1 should be the quoted name of the variable "fish". plotPlane will handle the work of re-organizing the information so that R's predict functions can generate the desired information. This might be thought of as a 3D version of "termplot", with a significant exception. The calculation of predicted values depends on predictors besides plotx1 and plotx2 in a different ways. The sample averages are used for numeric variables, but for factors the modal value is used.
This function creates an empty 3D drawing and then fills in the pieces. It uses the R functions lines, points, and arrows. To allow customization, several parameters are introduced for the users to choose colors and such. These options are prefixed by "l" for the lines that draw the plane, "p" for the points, and "a" for the arrows. Of course, if plotPoints=FALSE or drawArrows=FALSE, then these options are irrelevant.