lmer Demo

Author

Andy Grogan-Kaylor

Published

October 8, 2025

Norway Spruce and Larch Forest in Austrian Alps, https://ec.europa.eu/jrc/en/research-topic/forestry/qr-tree-project/norway-spruce

Norway Spruce and Larch Forest in Austrian Alps, https://ec.europa.eu/jrc/en/research-topic/forestry/qr-tree-project/norway-spruce

1 Get Data 🌲

Data are from von Guttenberg’s Norway spruce (Picea abies [L.] Karst) tree measurement data, from Robinson and Hamann (2020).

β€œThe data are measures from 107 trees. The trees were selected as being of average size from healthy and well stocked stands in the Alps.”

Show the code
library(FAwR) # Forest Analytics with R

data("gutten") # Von Guttenberg Tree Data

2 Variables 🌲

site Growth quality class of the tree’s habitat. 5 levels.

location Distinguishes tree location. 7 levels.

tree An identifier for the tree within location.

age.base The tree age taken at ground level.

height Tree height, m.

dbh.cm Tree diameter, cm.

volume Tree volume.

age.bh Tree age taken at 1.3 m.

tree.ID A factor uniquely identifying the tree.

3 Data Wrangling (Centering) 🌲

Show the code
gutten$height.C <- gutten$height - mean(gutten$height)

gutten$age.base.C <- gutten$age.base - mean(gutten$age.base) 

4 Graph 🌲

Show the code
library(ggplot2)

library(patchwork)
Show the code
p_uncentered <- ggplot(gutten,
                       aes(x = age.base,
                           y = height,
                           color = tree.ID)) +
  geom_line() +
  labs(title = "Tree Height By Tree Age",
       subtitle = "Uncentered Data") +
  scale_color_viridis_d() +
  theme_minimal() +
  theme(legend.position = "none")

# p_uncentered

p_centered <- ggplot(gutten,
                     aes(x = age.base.C,
                         y = height.C,
                         color = tree.ID)) +
  geom_line() +
  labs(title = "Tree Height By Tree Age",
       subtitle = "Centered Data") +
  scale_color_viridis_d() +
  theme_minimal() +
  theme(legend.position = "none")

# p_centered

p_uncentered + p_centered 

Tree Height by Tree Age

Tree Height by Tree Age

5 lmer 🌲

Show the code
library(lme4) # MLM

library(sjPlot) # nice tables for MLM

5.1 Unconditional Model 🌲

Show the code
fit0 <- lmer(height ~ (1 | tree.ID), 
             data = gutten)

tab_model(fit0)
  height
Predictors Estimates CI p
(Intercept) 17.23 16.35 β€“ 18.11 <0.001
Random Effects
Οƒ2 69.70
Ο„00 tree.ID 15.08
ICC 0.18
N tree.ID 107
Observations 1200
Marginal R2 / Conditional R2 0.000 / 0.178

5.2 One Independent Variable; Random Intercept Only 🌲

Show the code
fit1 <- lmer(height ~ age.base + (1 | tree.ID), 
             data = gutten)

tab_model(fit1)
  height
Predictors Estimates CI p
(Intercept) 2.10 1.07 β€“ 3.13 <0.001
age base 0.21 0.21 β€“ 0.22 <0.001
Random Effects
Οƒ2 8.41
Ο„00 tree.ID 25.75
ICC 0.75
N tree.ID 107
Observations 1200
Marginal R2 / Conditional R2 0.654 / 0.915

5.3 One Independent Variable; Random Intercept and Random Slope (Correlated) 🌲

Show the code
fit2 <- lmer(height ~ age.base + (1 + age.base | tree.ID), 
             data = gutten)

tab_model(fit2)
  height
Predictors Estimates CI p
(Intercept) 1.20 0.76 β€“ 1.65 <0.001
age base 0.24 0.23 β€“ 0.25 <0.001
Random Effects
Οƒ2 3.38
Ο„00 tree.ID 3.62
Ο„11 tree.ID.age.base 0.01
ρ01 tree.ID -0.12
ICC 0.92
N tree.ID 107
Observations 1200
Marginal R2 / Conditional R2 0.657 / 0.972

5.4 One Independent Variable; Random Intercept and Random Slope (Uncorrelated) 🌲

Important

Converges only with grand mean centered independent variable.

Show the code
fit3 <- lmer(height ~ age.base.C + (1 + age.base.C || tree.ID), 
             data = gutten)

tab_model(fit3)
  height
Predictors Estimates CI p
(Intercept) 18.75 17.69 β€“ 19.82 <0.001
age base C 0.24 0.23 β€“ 0.26 <0.001
Random Effects
Οƒ2 3.38
Ο„00 tree.ID 31.04
Ο„11 tree.ID.age.base.C 0.01
ρ01  
ρ01  
ICC 0.90
N tree.ID 107
Observations 1200
Marginal R2 / Conditional R2 0.703 / 0.971

References

Robinson, Andrew, and Jeff Hamann. 2020. FAwR: Functions and Datasets for "Forest Analytics with r". https://doi.org/10.32614/CRAN.package.FAwR.