I am confused about how random effects work in a Generalised Additive Model, specifically in mgcv
. I want to specify a factor as a random effect. (Specifically, a binomial outcome for the type of job, with a factor for the region the job is in)
Do they work the same way as in a linear mixed effects model (such as lme4
)?
Reading through the package guide I see they specify a random effect in the s() function as a smooth. This does not make sense for a factor, I thought it would only make sense to use s() on a continuous variable like time?
I have fit a model with the random effects below. Could some one interpret the effects for me so I can gain an understanding of what they do here?
My data structure if relevant, I am modelling Secondary_yes
.
$ Month : num [1:1141] 1 1 1 1 1 1 1 1 1 1 ...
$ Year : num [1:1141] 2011 2011 2011 2011 2011 ...
$ Primary : Factor w/ 10 levels "Foot & Ankle",..: 10 10 7 8 8 9 9 7 3 1 ...
$ LocSub : Factor w/ 2 levels "Locum","Substantive": 2 2 1 2 2 2 2 2 2 2 ...
$ Region : Factor w/ 14 levels "East Midlands",..: 8 11 5 2 2 8 8 2 8 8 ...
$ Secondary_yes: Factor w/ 2 levels "0","1": 2 2 1 2 2 1 1 1 1 1 ...
I have the following model which seems to fit quite well, does this make sense?:
mod = gam(Secondary_yes ~ s(Year) + s(Region, bs = "re") +
LocSub + s(Primary, bs = "re"), data = df , family = binomial,
method = "REML")
Summary:
Family: binomial
Link function: logit
Formula:
Secondary_yes ~ s(Year) + s(Region, bs = "re") + LocSub +
s(Primary, bs = "re")
Parametric coefficients:
Estimate Std. Error z value Pr(>|z|)
(Intercept) -2.2682 0.5081 -4.464 8.05e-06 ***
LocSubSubstantive 0.8329 0.3031 2.748 0.006 **
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
Approximate significance of smooth terms:
edf Ref.df Chi.sq p-value
s(Year) 2.229 2.78 3.16 0.314
s(Region) 8.282 13.00 38.92 5.26e-05 ***
s(Primary) 8.105 9.00 166.46 < 2e-16 ***
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
R-sq.(adj) = 0.208 Deviance explained = 20%
-REML = 518.17 Scale est. = 1 n = 1141