lmer
Demo
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)
$height.C <- gutten$height - mean(gutten$height)
gutten
$age.base.C <- gutten$age.base - mean(gutten$age.base) gutten
3 Graph
library(ggplot2)
library(patchwork)
<- ggplot(gutten,
p_uncentered 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
<- ggplot(gutten,
p_centered 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_centered p_uncentered
4 lmer
library(lme4) # MLM
Loading required package: Matrix
4.1 Unconditional Model
<- lmer(height ~ (1 | tree.ID),
fit0 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
<- lmer(height ~ age.base + (1 | tree.ID),
fit1 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