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"), 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"
)

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 family's canonical link.

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", or a numeric vector of fixed smoothing parameters, one per smooth.

Value

A gam_model with properties coefficients, lambdas, edf, dispersion, deviance, score 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.991027
predict(m, data.frame(x = 0.5)) - (sin(3) + 2)
#> [1] -0.0001048471