Become an expert in R — Interactive courses, Cheat Sheets, certificates and more!
Get Started for Free

predicted_samples

Draw new response values from the conditional distribution of the response


Description

Predicted values of the response (new response data) are drawn from the fitted model, created via simulate() (e.g. simulate.gam()) and returned in a tidy, long, format. These predicted values do not include the uncertainty in the estimated model; they are simply draws from the conditional distribution of the response.

Usage

predicted_samples(model, ...)

## S3 method for class 'gam'
predicted_samples(
  model,
  n = 1,
  newdata = NULL,
  seed = NULL,
  weights = NULL,
  ...
)

Arguments

model

a fitted model of the supported types

...

arguments passed to other methods. For fitted_samples(), these are passed on to predict.gam().

n

numeric; the number of posterior samples to return.

newdata

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 newdata, if available in model.

seed

numeric; a random seed for the simulations.

weights

numeric; a vector of prior weights. If newdata is null then defaults to object[["prior.weights"]], otherwise a vector of ones.

Value

A tibble (data frame) with 3 columns containing the posterior predicted values in long format. The columns are

  • row (integer) the row of newdata that each posterior draw relates to,

  • draw (integer) an index, in range 1:n, indicating which draw each row relates to,

  • response (numeric) the predicted response for the indicated row of newdata.

Author(s)

Gavin L. Simpson

Examples

load_mgcv()

dat <- gamSim(1, n = 1000, dist = "normal", scale = 2)
m <- gam(y ~ s(x0) + s(x1) + s(x2) + s(x3), data = dat, method = "REML")

predicted_samples(m, n = 5, seed = 42)

## Can pass arguments to predict.gam()
newd <- data.frame(x0 = runif(10), x1 = runif(10), x2 = runif(10),
                   x3 = runif(10))

## Exclude s(x2)
predicted_samples(m, n = 5, newd, exclude = "s(x2)", seed = 25)

## Exclude s(x1)
predicted_samples(m, n = 5, newd, exclude = "s(x1)", seed = 25)

## Select which terms --- result same as previous
predicted_samples(m, n = 5, newd, seed = 25,
                  terms = c("s(x0)", "s(x2)", "s(x3)"))

gratia

Graceful 'ggplot'-Based Graphics and Other Functions for GAMs Fitted Using 'mgcv'

v0.6.0
MIT + file LICENSE
Authors
Gavin L. Simpson [aut, cre] (<https://orcid.org/0000-0002-9084-8413>), Henrik Singmann [ctb] (<https://orcid.org/0000-0002-4842-3657>)
Initial release
2021-04-17

We don't support your browser anymore

Please choose more modern alternatives, such as Google Chrome or Mozilla Firefox.