Skip to contents

Get difference between two smooths

Usage

get_difference(
  model,
  series,
  compare,
  values = NULL,
  exclude_terms = NULL,
  length_out = 25,
  ci_z = 1.96
)

Arguments

model

A gam or bam model object.

series

A string specifying the variable that corresponds to the series to be plotted on the $x$-axis. If a string is given, the other numeric variables in the model are set to their mean value, unless specific values are given in values. If a character vector of two strings is given, the two variables will be taken as the elements of a tensor product smooth. This allows the user to plot 2D raster plots.

compare

A named list of factor levels to compare.

values

User supplied values for specific variables as a named list.

exclude_terms

Terms to be excluded from the prediction. Term names should be given as they appear in the model summary (for example, "s(x0,x1)").

length_out

An integer indicating how many values to use along the numeric variables for predicting the response (the default is 10).

ci_z

The z-value for calculating the CIs (the default is 1.96 for 95 percent CI).

Value

A tibble with the difference smooth.

Examples

library(mgcv)
#> Loading required package: nlme
#> This is mgcv 1.8-42. For overview type 'help("mgcv-package")'.
set.seed(10)
data <- gamSim(4)
#> Factor `by' variable example
model <- gam(y ~ s(x2, by = fac) + s(x0), data = data)

get_difference(model, "x2", list(fac = c("1", "2")))
#> Warning: Could not recover model data from environment. Please make sure your
#>   data is available in your workspace.
#>   Trying to retrieve data from the model frame now.
#> # A tibble: 26 × 7
#>         x2 fac      x0  diff    se lower_ci upper_ci
#>      <dbl> <chr> <dbl> <dbl> <dbl>    <dbl>    <dbl>
#>  1 0.00134 1-2   0.499  2.83 0.701    1.45      4.20
#>  2 0.0413  1-2   0.499  2.65 0.589    1.49      3.80
#>  3 0.0812  1-2   0.499  2.47 0.498    1.49      3.44
#>  4 0.121   1-2   0.499  2.29 0.429    1.45      3.13
#>  5 0.161   1-2   0.499  2.11 0.378    1.37      2.85
#>  6 0.201   1-2   0.499  1.94 0.343    1.27      2.61
#>  7 0.241   1-2   0.499  1.78 0.318    1.15      2.40
#>  8 0.281   1-2   0.499  1.61 0.300    1.02      2.20
#>  9 0.321   1-2   0.499  1.44 0.286    0.884     2.01
#> 10 0.361   1-2   0.499  1.27 0.276    0.734     1.82
#> # ℹ 16 more rows