smooth_samples | R Documentation |
Returns draws from the posterior distributions of smooth functions in a GAM. Useful, for example, for visualising the uncertainty in individual estimated functions.
smooth_samples(model, ...) ## S3 method for class 'gam' smooth_samples( model, term = NULL, n = 1, data = newdata, seed = NULL, freq = FALSE, unconditional = FALSE, ncores = 1L, n_vals = 200, ..., newdata = NULL )
model |
a fitted model of the supported types |
... |
arguments passed to other methods. For |
term |
character; select which smooth's posterior to draw from.
The default ( |
n |
numeric; the number of posterior samples to return. |
data |
data frame; new observations at which the posterior draws
from the model should be evaluated. If not supplied, the data used to fit
the model will be used for |
seed |
numeric; a random seed for the simulations. |
freq |
logical; |
unconditional |
logical; if |
ncores |
number of cores for generating random variables from a
multivariate normal distribution. Passed to |
n_vals |
numeric; how many locations to evaluate the smooth at if
|
newdata |
Deprecated: use |
A tibble with additional classes "smooth_samples"
and
'"posterior_samples".
For the "gam"
method, the columns currently returned (not in this order)
are:
smooth
; character vector. Indicates the smooth function for that
particular draw,
term
; character vector. Similar to smooth
, but will contain the
full label for the smooth, to differentiate factor-by smooths for
example.
by_variable
; character vector. If the smooth involves a by
term, the
by variable will be named here, NA_character_
otherwise.
row
; integer. A vector of values seq_len(n_vals)
, repeated if
n > 1L
. Indexes the row in data
for that particular draw.
draw
; integer. A vector of integer values indexing the particular
posterior draw that each row belongs to.
value
; numeric. The value of smooth function for this posterior draw
and covariate combination.
xxx
; numeric. A series of one or more columns containing data required
for the smooth, named as per the variables involved in the respective
smooth.
Additional columns will be present in the case of factor by smooths,
which will contain the level for the factor named in by_variable
for
that particular posterior draw.
The set of variables returned and their order in the tibble is subject to change in future versions. Don't rely on position.
Gavin L. Simpson
load_mgcv() dat <- data_sim("eg1", n = 400, seed = 2) m1 <- gam(y ~ s(x0) + s(x1) + s(x2) + s(x3), data = dat, method = "REML") sms <- smooth_samples(m1, term = "s(x0)", n = 5, seed = 42) sms ## A factor by example (with a spurious covariate x0) dat <- data_sim("eg4", n = 1000, seed = 2) ## fit model... m2 <- gam(y ~ fac + s(x2, by = fac) + s(x0), data = dat) sms <- smooth_samples(m2, n = 5, seed = 42) draw(sms)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.