Skip to content

Latest commit

 

History

History
139 lines (88 loc) · 4.96 KB

chapter2_04.md

File metadata and controls

139 lines (88 loc) · 4.96 KB
type
slides

Visualizing GAMs

Noam Ross Senior Research Scientist, EcoHealth Alliance

Notes: One of the most important things to do when interpreting and checking models is to visualize them. This allows us to inspect them and gain intuition for the model relationships. And of course, visualizations are often the most powerful way to communicate our results.


The Plot Command

plot(gam_model)
?plot.gam

Notes: We have already been using the central function for visualizing GAMs, plot(). The mgcv package has a powerful set of plot methods built into this function. We will go through options for plotting now, and you can also look them up in R help under ?plot.gam.


Partial Effect Plots

Notes: The plots generated by mgcv's plot() function are partial effect plots. That is, they show the component effect of each of the smooth or linear terms in the model, which add up to the overall prediction.


Selecting partial effects

plot(gam_model, select = c(2, 3))
plot(gam_model, pages = 1)
plot(gam_model, pages = 1, all.terms = TRUE)

Notes: The first option we have when making our plots is which partial effects to show. The select argument chooses which terms we plot, with the default being all of them.

Normally, each plot gets its own page, but using the pages argument, you can decide how many total pages to spread plots across. Using pages = 1, you show all your partial effects together.

Finally, by default we only see the smooth plots, but by setting all.terms = TRUE, we can display partial effects of linear or categorical terms, as well.


Showing data on the plots

plot(gam_model, rug = TRUE)

Notes: We often want to show data alongside model predictions. There are two ways to do this. First, the rug argument puts X-values along the bottom of the plot.


Showing data on the plots (2)

plot(gam_model, residuals = TRUE)

Notes: The residuals argument puts partial residuals on the plots. Partial residuals are the difference between the partial effect and the data, after all other partial effects have been accounted for.


Showing data on the plots (3)

plot(gam_model, rug = TRUE, residuals = TRUE,
     pch = 1, cex = 1)

Notes: It's often helpful to make these more visible with the pch argument, which changes the shape of the residuals points and the cex argument, which changes the size.


Showing Standard Errors

plot(gam_model, se = TRUE)

Notes: By default, plot will put standard errors on your plots. These show the 95% confidence interval for the mean shape of the effect.


Showing Standard Errors (2)

plot(gam_model, shade = TRUE)

Notes: It's often preferable to use shading rather than lines to show these intervals, and you can do so with the shade argument.


Showing Standard Errors

plot(gam_model, shade = TRUE, shade.col = "lightblue")

Notes: You can also change the color of shading with the shade.col argument.


Transforming Standard Errors

plot(gam_model, seWithMean = TRUE)

Notes: It's often useful to plot the standard errors of a partial effect term combined with the standard errors of the model intercept. This is because confidence intervals at the mean value of a variable can be very tiny, and don't reflect overall uncertainty in our model. Using the seWithMean argument adds in this uncertainty.


Transforming Standard Errors (2)

plot(gam_model, seWithMean = TRUE, shift = coef(gam_model)[1])

Notes: To make the plots even more interpretable, it's useful to shift the scale so that the intercept is included. Using the shift argument, we can shift the scale by value of the intercept, which is the first coefficient of the model. Note how the y-axis has changed. Now, the partial effect plot has a more natural interpretation - it shows us the prediction of the output, assuming other variables are at their average value. For instance, this plot shows that the miles per gallon of a 2000 pound car is about 30, all else being equal.