lmer Demo

Author

Andy Grogan-Kaylor

Published

May 12, 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: Andrew Robinson and Jeff Hamann (2016). FAwR: Functions and Datasets for “Forest Analytics with R”, R package version 1.1.1., https://CRAN.R-project.org/package=FAwR

“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.”

library(FAwR) # Forest Analytics with R

data("gutten") # Von Guttenberg Tree Data

2 Data Wrangling (Centering)

gutten$height.C <- gutten$height - mean(gutten$height)

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

3 Graph

library(ggplot2)

library(patchwork)
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

4 lmer

library(lme4) # MLM
Loading required package: Matrix

4.1 Unconditional Model

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

summary(fit0)
Linear mixed model fit by REML ['lmerMod']
Formula: height ~ (1 | tree.ID)
   Data: gutten

REML criterion at convergence: 8627.5

Scaled residuals: 
    Min      1Q  Median      3Q     Max 
-2.6675 -0.7242  0.1305  0.7758  2.0311 

Random effects:
 Groups   Name        Variance Std.Dev.
 tree.ID  (Intercept) 15.08    3.883   
 Residual             69.70    8.349   
Number of obs: 1200, groups:  tree.ID, 107

Fixed effects:
            Estimate Std. Error t value
(Intercept)  17.2328     0.4489   38.38

4.2 One Independent Variable; Random Intercept Only

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

summary(fit1)
Linear mixed model fit by REML ['lmerMod']
Formula: height ~ age.base + (1 | tree.ID)
   Data: gutten

REML criterion at convergence: 6346.7

Scaled residuals: 
    Min      1Q  Median      3Q     Max 
-3.3814 -0.5359  0.2145  0.7030  2.3443 

Random effects:
 Groups   Name        Variance Std.Dev.
 tree.ID  (Intercept) 25.747   5.074   
 Residual              8.409   2.900   
Number of obs: 1200, groups:  tree.ID, 107

Fixed effects:
            Estimate Std. Error t value
(Intercept) 2.102195   0.525768   3.998
age.base    0.214830   0.002406  89.287

Correlation of Fixed Effects:
         (Intr)
age.base -0.320

4.3 One Independent Variable; Random Intercept and Random Slope (Correlated)

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

summary(fit2)
Linear mixed model fit by REML ['lmerMod']
Formula: height ~ age.base + (1 + age.base | tree.ID)
   Data: gutten

REML criterion at convergence: 5489.7

Scaled residuals: 
    Min      1Q  Median      3Q     Max 
-3.3808 -0.5447  0.0590  0.5834  2.4378 

Random effects:
 Groups   Name        Variance Std.Dev. Corr 
 tree.ID  (Intercept) 3.624478 1.90381       
          age.base    0.005557 0.07455  -0.12
 Residual             3.381275 1.83882       
Number of obs: 1200, groups:  tree.ID, 107

Fixed effects:
            Estimate Std. Error t value
(Intercept) 1.204973   0.225294   5.348
age.base    0.239925   0.007454  32.186

Correlation of Fixed Effects:
         (Intr)
age.base -0.222

4.4 One Independent Variable; Random Intercept and Random Slope (Uncorrelated)

Converges only with grand mean centered independent variable.

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

summary(fit3)
Linear mixed model fit by REML ['lmerMod']
Formula: height ~ age.base.C + ((1 | tree.ID) + (0 + age.base.C | tree.ID))
   Data: gutten

REML criterion at convergence: 5682.6

Scaled residuals: 
    Min      1Q  Median      3Q     Max 
-3.9528 -0.5310  0.0659  0.5991  2.2450 

Random effects:
 Groups    Name        Variance  Std.Dev.
 tree.ID   (Intercept) 31.040110 5.57137 
 tree.ID.1 age.base.C   0.005648 0.07515 
 Residual               3.381118 1.83878 
Number of obs: 1200, groups:  tree.ID, 107

Fixed effects:
             Estimate Std. Error t value
(Intercept) 18.750851   0.542814   34.54
age.base.C   0.241264   0.007528   32.05

Correlation of Fixed Effects:
           (Intr)
age.base.C 0.013