Skip to contents

Summarises the posterior of every smooth over a grid of its own variables, in the shape brms::conditional_smooths() returns. Its plot() method draws a smooth of one variable as a line and ribbon, and a smooth of two as a heatmap.

Usage

# S3 method for class 'mvgam'
conditional_smooths(
  x,
  smooths = NULL,
  int_conditions = NULL,
  prob = 0.95,
  spaghetti = FALSE,
  surface = TRUE,
  facets = 3L,
  resolution = 100L,
  too_far = 0,
  ndraws = NULL,
  draw_ids = NULL,
  resp = NULL,
  ...
)

# S3 method for class 'mvgam_conditional_smooths'
plot(x, plot = TRUE, ask = FALSE, ...)

# S3 method for class 'mvgam_conditional_smooths'
print(x, ...)

Arguments

x

An object of class mvgam_conditional_smooths returned by conditional_smooths.mvgam().

smooths

The terms to summarise, as smooths.mvgam() lists them, or NULL for every smooth of every predictor, the trend's included.

int_conditions

A named list setting the grid of a variable: a vector of values, or a function of the variable's training values, such as stats::quantile().

prob

A number in [0, 1]: the mass of the interval in lower__ and upper__.

spaghetti

Logical. TRUE keeps the draws of a smooth of one variable, which the plot draws as one line each.

surface

Logical. TRUE evaluates a smooth of two variables on a resolution by resolution grid, drawn as a heatmap. FALSE draws lines over the first variable at facets values of the second.

facets

An integer of at least 2: the number of values taken by the second variable and by a numeric by variable. They run evenly from one standard deviation below the mean to one above.

resolution

An integer of at least 2: the number of grid points along each variable.

too_far

A number in [0, 1]. On a surface, grid points further than this from the training data, in the unit square, are dropped by mgcv::exclude.too.far(). 0 keeps every point.

ndraws, draw_ids

The number of posterior draws, or their indices. Give at most one. NULL for both uses every draw.

resp

The response whose smooths to summarise, or NULL for every response.

...

Unused. Anything passed here is refused.

plot

Logical. TRUE draws each panel. FALSE only returns them.

ask

Logical. TRUE prompts before each panel after the first.

Value

A list of class mvgam_conditional_smooths, one data frame per smooth, named by predictor and term. Each holds its grid with the columns estimate__, se__, lower__, upper__, effect1__ and cond__. A smooth of two variables adds effect2__.

The list of ggplot panels, one per smooth, invisibly.

Author

Nicholas J Clark

Examples

# \dontrun{
set.seed(13)
simdat <- sim_mvgam(family = poisson(), n_series = 1L,
                     n_timepoints = 120L, trend_model = AR())
mod <- mvgam(y ~ s(x, k = 5), trend_formula = ~ AR(p = 1),
              data    = simdat$data_train,
              family  = poisson(),
              chains  = 2, silent = 2)
#> Warning: 8 of 2000 (0.0%) transitions ended with a divergence.
#> See https://mc-stan.org/misc/warnings for details.

# The smooth with 30 of its draws
cs <- conditional_smooths(mod, spaghetti = TRUE, ndraws = 30)
plot(cs)

head(cs[[1]])
#>           x effect1__ cond__ estimate__      se__     lower__   upper__
#> 1 -1.956267 -1.956267      1  0.3120155 0.1896215 -0.12565254 0.6177492
#> 2 -1.916330 -1.916330      1  0.3407099 0.2008300 -0.06082007 0.6401446
#> 3 -1.876393 -1.876393      1  0.3773745 0.1854515  0.00392799 0.6624951
#> 4 -1.836457 -1.836457      1  0.4132541 0.1785957  0.06845771 0.6847287
#> 5 -1.796520 -1.796520      1  0.4474188 0.1755073  0.14088681 0.7067439
#> 6 -1.756583 -1.756583      1  0.4811837 0.1719354  0.21984178 0.7284141

# Next, plot(mod, type = "smooths") draws the same panels in one
# figure
# }