class: center, middle, title-slide .title[ # Practical 9 ] .author[ ### Olivier Gimenez ] .date[ ### last updated: 2026-11-09 ] --- ## Longitudinal study on coral reef and GLMM (Poisson) > A survey of a coral reef uses 10 predefined linear transects covered by divers once every week. The response variable of interest is the abundance of a particular species of anemone as a function of water temperature. Counts of anemones are recorded at 20 regular line segments along the transect. The following piece of code will generate a data set with realistic properties according to the above design. Make sure you understand what it is doing. You might want to explain the script to the colleague next to you. Also, to try and make sense of the code of others, it is always good to plot and/or run small sections of the code. From Jason Matthiopoulos' book. --- ## Data generation .small-font[ ``` r transects <- 10 data <- NULL for (tr in 1:transects){ # random effect (intercept) ref <- rnorm(1,0,.5) # water temperature gradient t <- runif(1, 18,22) + runif(1,-.2,0.2)*1:20 # Anemone gradient (expected response) ans <- exp(ref -14 + 1.8 * t - 0.045 * t^2) # actual counts on 20 segments of the current transect an <- rpois(20, ans) data <- rbind(data, cbind(rep(tr, 20), t, an)) } ``` ] --- * Generate a data set using the anemone code * Using Jags, fit a GLMM with quadratic effect of temperature and a random intercept. * Fit the same model to the same data in a Frequentist framework using function `lme4::glmer()`. * Compare the estimates. --- # Solution --- ## Make sense of the code * Always difficult to make sense of the code of others. * Good to plot and/or run small sections of the code. --- .tiny-font[ ``` r # random effect (intercept) ref <- rnorm(1,0,.5) # water temperature gradient t <- runif(1, 18,22) + runif(1,-.2,0.2)*1:20 plot(t,type='l') ``` <img src="practical9_files/figure-html/unnamed-chunk-2-1.svg" alt="" style="display: block; margin: auto;" /> ] --- .tiny-font[ ``` r ans <- exp(ref -14 + 1.8 * t - 0.045 * t^2) # Anemone gradient (expected response) plot(t,log(ans),type='l') ``` <img src="practical9_files/figure-html/unnamed-chunk-3-1.svg" alt="" style="display: block; margin: auto;" /> ] --- .tiny-font[ ``` r data <- data.frame(Transect = data[,1], Temperature = data[,2], Anemones = data[,3]) plot(data$Temperature, data$Anemones) ``` <img src="practical9_files/figure-html/unnamed-chunk-4-1.svg" alt="" style="display: block; margin: auto;" /> ] --- ## Write down model `\begin{align*} \text{Count}_i &\sim \text{Poisson(}\lambda_i) &\text{[likelihood]}\\ \text{log}(\lambda_i) &= a_{\text{TRANSECT[i]}} + b_1 \; \text{temp}_{i} + b_2 \; \text{temp}^2_{i} &\text{[linear model]} \\ a_j &\sim \text{Normal}(\bar{a}, \sigma) &\text{[prior for varying intercepts}] \\ \bar{a} &\sim \text{Normal}(0, 1000) &\text{[prior for population mean}] \\ \sigma &\sim \text{Uniform}(0, 100) &\text{[prior for standard deviation}] \\ b_1, b_2 &\sim \text{Normal}(0, 1000) &\text{[prior for slopes}] \\ \end{align*}` --- Standardize Temperature covariate. .small-font[ ``` r boo <- data$Temperature data$Temp <- (boo - mean(boo)) / sd(boo) head(data) ``` ``` ## Transect Temperature Anemones Temp ## 1 1 19.05705 85 -0.6969835 ## 2 1 19.05958 85 -0.6957129 ## 3 1 19.06211 96 -0.6944422 ## 4 1 19.06464 94 -0.6931716 ## 5 1 19.06716 92 -0.6919010 ## 6 1 19.06969 80 -0.6906304 ``` ] --- .small-font[ ``` r model <- paste(" model { for (i in 1:n){ count[i] ~ dpois(lambda[i]) log(lambda[i]) <- a[transect[i]] + b[1] * x[i] + b[2] * pow(x[i],2) } for (j in 1:nbtransects){ a[j] ~ dnorm (mu.a, tau.a) } mu.a ~ dnorm (0, 0.001) tau.a <- pow(sigma.a, -2) sigma.a ~ dunif (0, 100) b[1] ~ dnorm (0, 0.001) b[2] ~ dnorm (0, 0.001) } ") writeLines(model,here::here("slides","code","GLMMpoisson.bug")) ``` ] --- .small-font[ ``` r dat <- list(n = nrow(data), nbtransects = transects, x = data$Temp, count = data$Anemones, transect = data$Transect) init1 <- list(a = rnorm(transects), b = rnorm(2), mu.a = rnorm(1), sigma.a = runif(1)) init2 <- list(a = rnorm(transects), b = rnorm(2), mu.a = rnorm(1), sigma.a = runif(1)) inits <- list(init1, init2) par <- c ("a", "b", "mu.a", "sigma.a") ``` ] --- .tiny-font[ ``` r library(R2jags) fit <- jags(data = dat, inits = inits, parameters.to.save = par, n.iter = 2500, model.file=here::here("slides","code","GLMMpoisson.bug"), n.chains = 2, n.burn = 1000) ``` ``` ## Compiling model graph ## Resolving undeclared variables ## Allocating nodes ## Graph information: ## Observed stochastic nodes: 200 ## Unobserved stochastic nodes: 14 ## Total graph size: 1622 ## ## Initializing model ``` ] --- .tiny-font[ ``` r round(fit$BUGSoutput$summary[, -c(4,6)], 3) ``` ``` ## mean sd 2.5% 50% 97.5% Rhat n.eff ## a[1] 4.496 0.044 4.411 4.496 4.579 1.001 3000 ## a[2] 4.085 0.034 4.019 4.085 4.150 1.003 630 ## a[3] 3.325 0.043 3.238 3.325 3.408 1.004 480 ## a[4] 5.030 0.048 4.935 5.030 5.127 1.001 3000 ## a[5] 3.906 0.044 3.822 3.906 3.989 1.002 1500 ## a[6] 3.522 0.055 3.414 3.522 3.631 1.001 2300 ## a[7] 3.937 0.068 3.798 3.937 4.064 1.001 1800 ## a[8] 3.740 0.034 3.671 3.740 3.806 1.002 1700 ## a[9] 3.165 0.058 3.050 3.166 3.276 1.002 1400 ## a[10] 3.443 0.104 3.232 3.445 3.645 1.003 760 ## b[1] -0.082 0.044 -0.168 -0.081 0.002 1.001 2400 ## b[2] -0.174 0.018 -0.210 -0.174 -0.139 1.001 3000 ## deviance 1302.383 4.827 1294.665 1301.789 1312.999 1.001 3000 ## mu.a 3.860 0.226 3.414 3.860 4.310 1.003 830 ## sigma.a 0.673 0.205 0.409 0.632 1.184 1.009 1900 ``` ] --- Convert regression coefficients from scaled to non-scaled and compare to values used to generate data (from <https://stats.stackexchange.com/questions/361995/how-to-convert-coefficients-from-quadratic-function-from-scaled-to-not-scaled-co>) ``` r sbzero <- fit$BUGSoutput$sims.matrix[,'mu.a'] sbun <- fit$BUGSoutput$sims.matrix[,'b[1]'] sbdeux <- fit$BUGSoutput$sims.matrix[,'b[2]'] mu <- mean(boo) sg <- sd(boo) ``` --- .center[ ``` r bzero <- sbzero - sbun*mu/sg + sbdeux*mu^2/(sg^2) hist(bzero) abline(v = -14, col = "red", lwd = 2) ``` <!-- --> ``` r mean(bzero) ``` ``` ## [1] -13.70693 ``` ] --- .center[ ``` r bun <- sbdeux/sg - 2 * sbdeux * mu / (sg^2) hist(bun) abline(v = 1.8, col = "red", lwd = 2) ``` <!-- --> ``` r mean(bun) ``` ``` ## [1] 1.713372 ``` ] --- .center[ ``` r bdeux <- sbdeux/(sg^2) hist(bdeux) abline(v = - 0.045, col = "red", lwd = 2) ``` <!-- --> ``` r mean(bdeux) ``` ``` ## [1] -0.04404831 ``` ] --- ## Frequentist approach .tiny-font[ ``` r library(lme4) fit_lme4 <- glmer(Anemones ~ Temp + I(Temp^2) + (1 | Transect), data = data, family = poisson) fit_lme4 ``` ``` ## Generalized linear mixed model fit by maximum likelihood (Laplace ## Approximation) [glmerMod] ## Family: poisson ( log ) ## Formula: Anemones ~ Temp + I(Temp^2) + (1 | Transect) ## Data: data ## AIC BIC logLik -2*log(L) df.resid ## 1363.0548 1376.2481 -677.5274 1355.0548 196 ## Random effects: ## Groups Name Std.Dev. ## Transect (Intercept) 0.5394 ## Number of obs: 200, groups: Transect, 10 ## Fixed Effects: ## (Intercept) Temp I(Temp^2) ## 3.86587 -0.08138 -0.17407 ``` ] Parameter estimates obtained with both approaches are very similar. --- .center[ ``` r visreg::visreg(fit_lme4, xvar = 'Temp') ``` <!-- --> ]