Plot a Model's Residuals, Response, and Partial Dependence Plots
Plot model surfaces for a wide variety of models
using partial dependence plots and other techniques.
Also plot model residuals and other information on the model.
The plotmo package: Plotting model surfaces
After building a regression or classification model, it's often useful to plot the model response as the predictors vary. These model surface plots are helpful for visualizing "black box" models.
An example model surface
Let's generate a randomForest model from the well-known ozone dataset. (We use a random forest for this example, but any model could be used.)
library(earth) # for the ozone1 data data(ozone1) oz <- ozone1[, c("O3", "humidity", "temp")] # simple dataset for illustration library(randomForest) mod <- randomForest(O3 ~ ., data=oz)
We now have a model, but what does it tell us about the relationship
between ozone pollution (O3) and humidity and temperature?
We can visualize this relationship with
From the plots, we see that ozone increases with humidity and temperature, although humidity doesn't have much effect at low temperatures.
The top two plots in the above figure are generated by plotting the predicted response as a variable changes. Variables that don't appear in a plot are held fixed at their median values. Plotmo automatically creates a separate plot for each variable in the model.
The lower interaction plot shows the predicted response as two variables are changed (once again with other variables if any held at their median values). Plotmo draws just one interaction plot for this model, since there are only two variables.
Partial dependence plots
We can generate
partial dependence plots by specifying
pmethod="partdep" when invoking
In partial dependence plots, the effect of the background variables is
averaged (instead of simply holding the background variables at their
Partial dependence plots can be very slow, but they do incorporate
more information about the distribution of the response.
Plotting model residuals
plotres function is also included in the
This function shows residuals and other useful information
about the model, if available.
Using the above model as an example:
Note the "<" shape in the residuals plot in the lower left. This suggests that we should transform the response before building the model, maybe by taking the square or cube-root. Cases 53, 237, and 258 have the largest residuals and perhaps should be investigated. This kind of information is not obvious without plotting the residuals
More details and examples may be found in the package vignettes:
Which models work with plotmo?
Any model that conforms to standard S3 model guidelines will work
Plotmo knows how to deal with logistic, classification,
and multiple response models.
It knows how to handle different
type arguments to
Package authors may want to look at
Guidelines for S3 Regression Models.
plotres doesn't work with your model, contact the
plotmo package maintainer.
Often a minor tweak to the model code is all that is needed.
Functions in plotmo
|plotmo||Plot a model's response over a range of predictor values (the model surface)|
|plot_gbm||Plot a gbm model|
|plot_glmnet||Plot a glmnet model|
|plotres||Plot the residuals of a regression model|
Last month downloads
|Packaged||2019-03-16 18:05:18 UTC; milbo|
|Date/Publication||2019-03-16 18:50:03 UTC|
|suggests||C50 (>= 0.1.0-24) , earth (>= 4.5.2) , gbm (>= 2.1.1) , glmnet (>= 2.0.5) , glmnetUtils (>= 1.0.3) , MASS (>= 7.3-45) , mlr (>= 2.12.1) , neuralnet (>= 1.33) , partykit (>= 1.2-2) , pre (>= 0.5.0) , rpart (>= 4.1-10) , rpart.plot (>= 2.1.0)|
|depends||Formula (>= 1.2-3) , plotrix , R (>= 3.2.0) , TeachingDemos|
Include our badge in your README