```
library(magrittr)
library(nlme)
library(lme4)
library(ggplot2)
data(BodyWeight)
ggplot(data = BodyWeight, mapping = aes(x = Time, y = weight, group = Rat, color = Diet)) +
geom_line() +
theme_bw() + theme(legend.key = element_blank())
## Random intercepts model
lmer1 <- lmer(formula = weight ~ Diet + Time + (1 | Rat),
data = BodyWeight)
## Print
lmer1
## Linear mixed model fit by REML ['lmerMod']
## Formula: weight ~ Diet + Time + (1 | Rat)
## Data: BodyWeight
## REML criterion at convergence: 1304.284
## Random effects:
## Groups Name Std.Dev.
## Rat (Intercept) 36.577
## Residual 8.176
## Number of obs: 176, groups: Rat, 16
## Fixed Effects:
## (Intercept) Diet2 Diet3 Time
## 244.0689 220.9886 262.0795 0.5857
## Summary
lmer1
## Linear mixed model fit by REML ['lmerMod']
## Formula: weight ~ Diet + Time + (1 | Rat)
## Data: BodyWeight
## REML criterion at convergence: 1304.284
## Random effects:
## Groups Name Std.Dev.
## Rat (Intercept) 36.577
## Residual 8.176
## Number of obs: 176, groups: Rat, 16
## Fixed Effects:
## (Intercept) Diet2 Diet3 Time
## 244.0689 220.9886 262.0795 0.5857
## Estimated variance-covariance matrix for fixed effects estimates.
## Fixed effects are fixed quantities, thus, their true values do not have
## variance. Their estimates, however, are functions of data, which are
## considered random. The estimates, thus, have variability from data to data.
## The true variance of the fixed effects estimates is another set of unknown
## quantities, thus, they are estimated.
vcov(lmer1)
## 4 x 4 Matrix of class "dpoMatrix"
## (Intercept) Diet2 Diet3 Time
## (Intercept) 169.12400730 -1.679945e+02 -1.679945e+02 -3.367172e-02
## Diet2 -167.99447412 5.039834e+02 1.679945e+02 2.240254e-16
## Diet3 -167.99447412 1.679945e+02 5.039834e+02 2.240254e-16
## Time -0.03367172 2.240254e-16 2.240254e-16 1.003764e-03
## Estimated variance for the random intercepts. The true intercepts vary across
## individuals. Thus, their true values have variance. The estimate of this true
## variance, has its own uncertainty (variance), but lmer does not seem to give it.
summary(lmer1)$varcor$Rat
## (Intercept)
## (Intercept) 1337.878
## attr(,"stddev")
## (Intercept)
## 36.57702
## attr(,"correlation")
## (Intercept)
## (Intercept) 1