extract_ranef: Extract random effects from a gam

Description Usage Arguments Details Value Note See Also Examples

View source: R/extract_ranef.R

Description

Extract what would be the random effects from a mixed model from a gam object. Assumes an mgcv model of the form gam(... + s(g, bs='re')).

Usage

1
extract_ranef(model, re = NULL, ci_level = 0.95, digits = 3)

Arguments

model

A gam or bam model

re

Filter results to a specific random effect/grouping variable.

ci_level

Level for the confidence interval. Must be between 0 and 1.

digits

Rounding for the output.

Details

Returns a data frame of the the component type, the estimated random effect re, the estimated se, and approximate lower and upper bounds. Note that the standard errors are Bayesian estimates (see gamObject, specifically type Vp). The re will only reflect smooth terms whose basis function is 're', i.e. of class random.effect. Others will be ignored.

Value

A tibble or data frame with the random effect, its standard error, and its lower and upper bounds. The bounds are based on a simple normal approximation using the standard error.

Note

mgcv strips the level names for 're' smooth terms, so this attempts to get them back. This may not work under every circumstance, but the attempt is made to extract the names of random effect groups based on how they are ordered in the data (which is how the model matrix would be constructed), and in the case of random slopes, detect that second variable in the 're' specification would be the grouping variable. This will not work for continuous x continuous smooths of type 're', but I can't think of a reason why you'd use that given your other options with mgcv.

See Also

ranef

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
library(mgcv)
library(lme4)

lmer_model <- lmer(Reaction ~ Days + (Days || Subject), data = sleepstudy)
ga_model <- gam(Reaction ~ Days + s(Subject, bs = "re") + s(Days, Subject, bs = "re"),
  data = sleepstudy,
  method = "REML"
)

ranef(lmer_model)
extract_ranef(ga_model)

m-clark/gammit documentation built on Oct. 22, 2020, 6:53 p.m.