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

Description Usage Arguments Details Value Note See Also Examples

View source: R/plot_gam_2d.R

Description

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

Usage

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
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.

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.

Value

A ggplot of the 2d effect.

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

scale_fill_scico scale_colour_viridis_d

Other model visualization: plot_coefficients.brmsfit(), plot_coefficients.lm(), plot_coefficients.merMod(), plot_coefficients(), plot_gam_3d(), plot_gam_check(), plot_gam()

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
library(mgcv); library(dplyr)
set.seed(0)

d = gamSim(2, scale=.1)$data
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)
mod_by <- gam(y ~ s(x2, by=fac), data = d2)
plot_gam_by(mod_by, main_var = x2, by_var = fac)

m-clark/visibly documentation built on Oct. 28, 2020, 5:33 p.m.