Skip to contents

A glm_fit() model in which each name in smooths (a numeric term of the formula) becomes a cubic P-spline (mgcv's s(x, bs = "ps")), with smoothing chosen by GCV when the dispersion is estimated and UBRE when it is fixed (mgcv's method = "GCV.Cp"), by REML (smoothing = "reml", mgcv's method = "REML", which estimates an unknown dispersion with the smoothing parameters as reml_scale), or fixed by smoothing.

Usage

gam_fit(
  formula,
  data,
  smooths,
  n_basis = 10,
  family = "poisson",
  link = NULL,
  offset = NULL,
  weights = NULL,
  dispersion = NULL,
  theta = NULL,
  power = NULL,
  smoothing = "auto",
  tensors = list(),
  tensor_basis = 5,
  bs = "ps",
  period = list()
)

Arguments

formula

A model formula; offset(...) terms are honoured.

data

A data frame.

smooths

Names of numeric terms to smooth.

n_basis

Basis functions per smooth (recycled; 10 by default).

family

"gaussian", "poisson", "gamma", "inverse_gaussian", "binomial" (response a proportion, weights the trials), "negative_binomial" (needs theta) or "tweedie" (needs power).

"identity", "log", "logit", "probit", "cloglog", "inverse", "inverse_squared" or "power" (needs link_power); NULL for the log link for the gamma, inverse Gaussian and Tweedie and the canonical link otherwise. R's own glm() defaults to the canonical link (Gamma() is the inverse link), so give link when porting a model from it.

offset

Optional offset added to any offset() terms.

weights

Optional prior weights.

dispersion

NULL (1 for the Poisson, binomial and negative binomial; Pearson's estimate otherwise), "pearson" (with the Poisson, the over-dispersed Poisson, which accepts negative responses as long as the fitted means stay positive), "deviance" or a fixed number.

theta

Negative binomial theta (variance mu + mu^2 / theta).

power

Tweedie power in (1, 2).

smoothing

"auto", "gcv", "ubre", "reml", or a numeric vector of fixed smoothing parameters: one per smooth, then one per tensor margin.

tensors

A list of tensor product smooths (mgcv's te(..., bs = "ps", np = FALSE)), each a character vector of two or more numeric terms. Each margin has its own smoothing parameter.

tensor_basis

Basis functions per margin of each tensor: one number (recycled) or a list with one vector per tensor (5 by default).

bs

The basis of each smooth (recycled): "ps", a P-spline; "cp", a cyclic P-spline (mgcv's bs = "cp"), whose ends meet in value and slope over its period; or "mpi" / "mpd", a monotone increasing or decreasing P-spline (scam's), which takes GCV, UBRE or fixed smoothing but not REML.

period

A named list of c(lo, hi) periods for cyclic smooths (mgcv's knots = list(x = c(lo, hi))); a cyclic smooth left out takes the data's range.

Value

A gam_model with properties coefficients, lambdas, edf, dispersion, deviance, score, reml_scale (NA unless REML) and fitted.

Examples

d <- data.frame(x = seq(0, 1, length.out = 100))
d$y <- sin(6 * d$x) + 2
m <- gam_fit(y ~ x, d, smooths = "x", family = "gaussian")
m@edf
#> [1] 9.991026
predict(m, data.frame(x = 0.5)) - (sin(3) + 2)
#> [1] -0.0001048472
# A seasonal effect whose ends meet: months 0 to 12.
s <- data.frame(month = seq(0.25, 11.75, by = 0.5))
s$y <- 2 + sin(2 * pi * s$month / 12)
cm <- gam_fit(y ~ month, s, smooths = "month", family = "gaussian", bs = "cp",
              period = list(month = c(0, 12)))
predict(cm, data.frame(month = c(0, 12)))
#> [1] 2 2