Learn R Programming

gratia (version 0.9.0)

add_fitted_samples: Add posterior draws from a model to a data object

Description

Adds draws from the posterior distribution of model to the data object using one of fitted_samples(), predicted_samples(), or posterior_samples().

Usage

add_fitted_samples(object, model, n = 1, seed = NULL, ...)

add_predicted_samples(object, model, n = 1, seed = NULL, ...)

add_posterior_samples(object, model, n = 1, seed = NULL, ...)

add_smooth_samples(object, model, n = 1, seed = NULL, select = NULL, ...)

Arguments

object

a data frame or tibble to which the posterior draws will be added.

model

a fitted GAM (or GAM-like) object for which a posterior draw method exists.

n

integer; the number of posterior draws to add.

seed

numeric; a value to seed the random number generator.

...

arguments are passed to the posterior draw function, currently one of fitted_samples(), predicted_samples(), or posterior_samples(). n and seed are already specified here as arguments and are also passed on to the posterior sampling function.

select

character; select which smooth's posterior to draw from. The default, NULL, means the posteriors of all smooths in model wil be sampled from individually. If supplied, a character vector of requested smooth terms.

Examples

Run this code
# \dontshow{
op <- options(pillar.sigfig = 3)
# }
load_mgcv()

df <- data_sim("eg1", n = 400, seed = 42)

m <- gam(y ~ s(x0) + s(x1) + s(x2) + s(x3), data = df, method = "REML")

# add fitted samples (posterior draws of the expected value of the response)
# note that there are 800 rows in the output: 400 data by `n = 2` samples.
df |>
  add_fitted_samples(m, n = 2, seed = 84)

# add posterior draws from smooth s(x2)
df |>
  add_smooth_samples(m, n = 2, seed = 2, select= "s(x2)")
# \dontshow{
  options(op)
# }

Run the code above in your browser using DataLab