simulate_gam: Simulate responses from a generalized additive linear model...

View source: R/simulate_gam.R

simulate_gamR Documentation

Simulate responses from a generalized additive linear model gam

Description

By sampling from the vector of coefficients it is possible to simulate data from a ‘gam’ model.

Usage

simulate_gam(
  object,
  nsim = 1,
  psim = 1,
  resid.type = c("none", "resample", "normal", "wild"),
  value = c("matrix", "data.frame"),
  ...
)

Arguments

object

object of class gam or glm.

nsim

number of simulations to perform

psim

parameter simulation level (an integer, 0, 1, 2 or 3).

resid.type

type of residual to use. ‘none’, ‘resample’, ‘normal’ or ‘wild’.

value

either ‘matrix’ or ‘data.frame’

...

additional arguments (none used at the moment)

Details

This function is probably redundant. Simply using simulate generates data from the correct distribution for objects which inherit class lm. The difference is that I'm trying to add the uncertainty in the parameter estimates.

These are the options that control the parameter simulation level

psim = 0

returns the fitted values

psim = 1

simulates from a beta vector (mean response)

psim = 2

simulates observations according to the residual type (similar to observed data)

psim = 3

simulates a beta vector, considers uncertainty in the variance covariance matrix of beta and adds residuals (prediction)

The residual type (resid.type) controls how the residuals are generated. They are either resampled, simulated from a normal distribution or ‘wild’ where the Rademacher distribution is used (https://en.wikipedia.org/wiki/Rademacher_distribution). Resampled and normal both assume iid, but ‘normal’ makes the stronger assumption of normality. ‘wild’ does not assume constant variance, but it assumes symmetry.

Value

matrix or data.frame with responses

Note

psim = 3 is not implemented at the moment.

The purpose of this function is to make it compatible with other functions in this package. It has some limitations compared to the functions in the ‘see also’ section.

References

Generalized Additive Models. An Introduction with R. Second Edition. (2017) Simon N. Wood. CRC Press.

See Also

predict, predict.gam, simulate and simulate_lm.

Examples


require(ggplot2)
require(mgcv)
## These count data are from GAM book by Simon Wood (pg. 132) - see reference
y <- c(12, 14, 33, 50, 67, 74, 123, 141, 165, 204, 253, 246, 240)
t <- 1:13
dat <- data.frame(y = y, t = t)
fit <- gam(y ~ t + I(t^2), family = poisson, data = dat)
sims <- simulate_gam(fit, nsim = 100, value = "data.frame")

ggplot(data = sims) + 
  geom_line(aes(x = t, y = sim.y, group = ii), 
            color = "gray", alpha = 0.5) + 
  geom_point(aes(x = t, y = y)) 



nlraa documentation built on July 9, 2023, 6:08 p.m.