Skip to contents

Fits a generalized additive model with penalized regression spline smooths and automatic smoothness selection, reproducing the default fits of mgcv::gam: the same thin plate regression spline and cubic regression spline bases, the same identifiability constraints and penalty scaling, and smoothing parameters chosen by GCV/UBRE ("GCV.Cp") or by Laplace-approximate REML ("REML") with Newton's method on the log smoothing parameters. Binomial (logit) and Poisson (log) responses are fitted by penalized iteratively re-weighted least squares (P-IRLS).

Usage

morie_gam(
  formula,
  data,
  family = c("gaussian", "binomial", "poisson"),
  method = c("GCV.Cp", "REML"),
  weights = NULL,
  sp = NULL,
  ...
)

Arguments

formula

A model formula such as y ~ s(x) + s(z, bs = "cr") + w.

data

A data frame containing the variables in the formula.

family

One of "gaussian" (identity link), "binomial" (logit link, 0/1 or logical or two-level factor response) or "poisson" (log link).

method

Smoothness selection criterion: "GCV.Cp" (GCV when the scale is unknown, UBRE/Mallows' Cp for binomial and Poisson) or "REML".

weights

Optional non-negative prior weights, one per row of data.

sp

Optional fixed smoothing parameters, one per penalized smooth in formula order (as mgcv::gam's sp); NULL (default) estimates them. With method = "REML" and a Gaussian response the scale is still estimated.

...

Unused; present for compatibility with the bridge calling convention.

Value

An object of class "morie_gam": a list with coefficients, Vp (Bayesian posterior covariance), Ve (frequentist covariance), edf (per coefficient), edf_smooth, edf_total, sp (smoothing parameters), score (the minimised GCV/UBRE or REML criterion), scale, fitted.values, linear.predictors, residuals, deviance, null.deviance, dev.expl, r.sq, aic, smooths (basis information used by predict) and optimiser diagnostics.

Details

The right-hand side may contain ordinary parametric terms (anything model.matrix understands) and smooth terms written s(x), s(x, k = 20), s(x, bs = "cr") or the two-dimensional isotropic thin plate smooth s(x, z). The s() terms are read from the formula as data; no function s is called. For more than 2000 unique covariate values a thin plate basis is built from 2000 knots sampled with the fixed seed 1, as mgcv does, without disturbing the caller's random number stream.

References

Wood, S. N. (2017) Generalized Additive Models: An Introduction with R, 2nd ed., CRC Press. Wood, S. N. (2003) Thin plate regression splines, JRSS B 65, 95-114. Wood, S. N. (2011) Fast stable restricted maximum likelihood and marginal likelihood estimation of semiparametric generalized linear models, JRSS B 73, 3-36. Wood, S. N. (2013) On p-values for smooth components of an extended generalized additive model, Biometrika 100, 221-228.

Examples

set.seed(1)
d <- data.frame(x = runif(200), z = runif(200))
d$y <- sin(2 * pi * d$x) + d$z + rnorm(200, 0, 0.3)
fit <- morie_gam(y ~ s(x) + z, data = d)
fit
#> 
#> Family: gaussian 
#> Link function: identity 
#> 
#> Formula:
#> y ~ s(x) + z
#> <environment: 0x562176affb88>
#> 
#> Estimated degrees of freedom:
#>   s(x) 
#> 5.9288 
#>  total = 7.9288
#> 
#> GCV score: 0.097038062   scale est. = 0.0931911
#> n = 200
summary(fit)
#> 
#> Family: gaussian 
#> Link function: identity 
#> 
#> Formula:
#> y ~ s(x) + z
#> <environment: 0x562176affb88>
#> 
#> Parametric coefficients:
#>              Estimate Std. Error t value Pr(>|t|)    
#> (Intercept) -0.054121   0.041983 -1.2891   0.1989    
#> z            1.082555   0.076775 14.1003   <2e-16 ***
#> ---
#> Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
#> 
#> Approximate significance of smooth terms:
#>         edf Ref.df      F   p-value    
#> s(x) 5.9288 7.1039 150.87 < 2.2e-16 ***
#> ---
#> Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
#> 
#> R-sq.(adj) = 0.86   Deviance explained = 86.4%
#> GCV = 0.0970381   Scale est. = 0.0931911   n = 200
head(predict(fit, newdata = data.frame(x = c(0.1, 0.5), z = 0.5)))
#>        1        2 
#> 1.152791 0.528741