Plot 2-way GAM smooths Plot 2-dimensional smooth terms

plot_gam_2d(
  model,
  main_var,
  second_var,
  conditional_data = NULL,
  n_plot = 100,
  force_2d = FALSE,
  ...
)

plot_gam_by(
  model,
  main_var,
  by_var,
  conditional_data = NULL,
  n_plot = 500,
  ...
)

Arguments

model

The mgcv GAM.

main_var

Which variable do you want to plot? Uses bare variable names and can take multiple variables via vars().

second_var

Required for plot_gam_2d. The second continuous variable of interest.

conditional_data

This is the same as the newdata argument for predict and passed to create_prediction_data(). Supply a data frame with desired values of the model covariates. Given the nature of this plot, this should likely only be a single row for other covariate values.

n_plot

How many plotting points for the main_var/second_var? Default is 100, creating a 100 x 100 grid of points.

force_2d

If the second_var has <= 5 values, the plot_gam_by is called. This will override that.

...

Options to scale_fill_scico for plot_gam_2d or scale_color_viridis_d for plot_gam_by (scale_color_scico_d if using scico development version).

by_var

Required for plot_gam_by. The categorical variable of interest.

Value

A ggplot of the 2d effect.

Details

These functions plot the predictions for two covariates in a GAM model produced by the mgcv package. The plot_gam_2d function is used for plotting two continuous predictors, while plot_gam_by is used in the case where one of the variables is categorical. If plot_gam_2d is called with the second variable being categorical or of very few distinct values, a message will follow along with a switch to plot_gam_by. One can override this with the force_2d argument.

Note

Any attempt to use a non-numeric variable for the main_var will result in failure.

If you are using gamm or gamm4 then you need to supply the mgcv model as the model object.

See also

Examples

library(mgcv); library(dplyr) set.seed(0) d = gamSim(2, scale=.1)$data
#> Bivariate smoothing example
mod <- gam(y ~ s(x, z), data = d) plot_gam_2d(mod, main_var = x, second_var = z)
plot_gam_2d(mod, main_var = x, second_var = z, palette='oslo')
d2 = gamSim(4)
#> Factor `by' variable example
mod_by <- gam(y ~ s(x2, by=fac), data = d2) plot_gam_by(mod_by, main_var = x2, by_var = fac)