Agree with Gavin's answer. This type of plot produced by mgcv
is useful for quickly inspecting whether a Gaussian assumption for the hierarchical effects (random intercepts in this case) is appropriate. If you find big outliers, then perhaps something like a Student's T distribution might be more appropriate. But given that you cannot use a T distribution with s(..., bs = 're')
, it is probably more useful to rely on this plot to inspect shrinkage.
But you may find it more helpful to actually see which factor level each effect corresponds to, and you will probably want to inspect uncertainty in those estimates. The marginaleffects
package is your friend here. Below I show how you can use this package to plot your random effect estimates on both the link scale and the log scale, which will hopefully be more useful to you as you diagnose and critique your model.
library(mgcv)
#> Loading required package: nlme
#> This is mgcv 1.8-42. For overview type 'help("mgcv-package")'.
library(marginaleffects)
library(ggplot2); theme_set(theme_classic())
#> Warning: package 'ggplot2' was built under R version 4.3.3
# Simulate some hierarchical data
set.seed(0)
N <- 30
sites <- letters[1:6]
N_sites <- length(sites)
hyper_mu <- 0.5
hyper_sigma <- 1
dat <- do.call(rbind, lapply(1:N_sites, function(site){
# Sample the site-specific random intercept from the population
# distribution
site_mu <- rnorm(1, hyper_mu, hyper_sigma)
# Sample count observations for this specific site
data.frame(y = rpois(N, exp(site_mu)),
site = sites[site])
}))
dat$site <- as.factor(dat$site)
head(dat, 12)
#> y site
#> 1 5 a
#> 2 6 a
#> 3 9 a
#> 4 4 a
#> 5 9 a
#> 6 10 a
#> 7 7 a
#> 8 6 a
#> 9 2 a
#> 10 4 a
#> 11 4 a
#> 12 7 a
tail(dat, 12)
#> y site
#> 169 2 f
#> 170 0 f
#> 171 0 f
#> 172 2 f
#> 173 1 f
#> 174 2 f
#> 175 0 f
#> 176 1 f
#> 177 1 f
#> 178 2 f
#> 179 1 f
#> 180 1 f
# Fit a model that uses a random effect basis for hierarhical
# intercepts
mod <- gam(y ~ s(site, bs = 're'),
family = poisson(),
data = dat)
# Typical Gaussian quantiles plot of random intercepts
plot(mod)
# Now make a more useful plot using marginaleffects::plot_predictions();
# first plot random intercepts on the link (log) scale
plot_predictions(mod,
condition = 'site',
type = 'link') +
labs(y = 'log(mu)')
# And now plot them on the outcome scale
plot_predictions(mod,
condition = 'site') +
labs(y = 'mu')
Created on 2024-08-27 with reprex v2.1.0