# 第 64 章 縱向研究數據 longitudinal data 3

## 64.1 第一層級的異質性 level 1 heterogeneity

$\text{log} (\sigma_e) = \delta_1I_{\text{gender = boy}} + \delta_2I_{\text{gender=girl}}$

M_growth_l1 <- lme(fixed = weight ~ age + age2 + gender, random = ~ age | id, weights = varIdent(form=~1|gender), data = growth, method = "REML", na.action = na.omit)
summary(M_growth_l1)
## Linear mixed-effects model fit by REML
##  Data: growth
##         AIC       BIC     logLik
##   533.06033 562.47105 -257.53016
##
## Random effects:
##  Formula: ~age | id
##  Structure: General positive-definite, Log-Cholesky parametrization
##             StdDev     Corr
## (Intercept) 0.64492216 (Intr)
## age         0.49336371 0.129
## Residual    0.64221192
##
## Variance function:
##  Structure: Different standard deviations per stratum
##  Formula: ~1 | gender
##  Parameter estimates:
##       Boys      Girls
## 1.00000000 0.77212888
## Fixed effects: weight ~ age + age2 + gender
##                  Value   Std.Error  DF    t-value p-value
## (Intercept)  3.8294124 0.175581118 128  21.809933  0.0000
## age          7.6297282 0.234375571 128  32.553428  0.0000
## age2        -1.6350689 0.086535227 128 -18.894836  0.0000
## genderGirls -0.6043803 0.204516527  66  -2.955166  0.0043
##  Correlation:
##             (Intr) age    age2
## age         -0.523
## age2         0.471 -0.930
## genderGirls -0.630  0.011  0.008
##
## Standardized Within-Group Residuals:
##          Min           Q1          Med           Q3          Max
## -1.972410889 -0.451503839 -0.063191963  0.468696896  2.959946331
##
## Number of Observations: 198
## Number of Groups: 68
# 和之間默認男女兒童的誤差方差相等時的模型做比較
# 沒有顯著差異 (p = 0.09)
anova(M_growth_l1, M_growth_mix)
##              Model df       AIC       BIC     logLik   Test   L.Ratio p-value
## M_growth_l1      1  9 533.06033 562.47105 -257.53017
## M_growth_mix     2  8 533.95003 560.09290 -258.97501 1 vs 2 2.8897013  0.0891

## 64.2 第二層級異質性 level 2 heterogeneity

M_growth_l2 <- lme(fixed = weight ~ age + age2 + gender,
random = ~ age*gender | id,
data = growth, method = "REML", na.action = na.omit)
summary(M_growth_l2)
## Linear mixed-effects model fit by REML
##  Data: growth
##         AIC       BIC     logLik
##   539.92465 588.94252 -254.96233
##
## Random effects:
##  Formula: ~age * gender | id
##  Structure: General positive-definite, Log-Cholesky parametrization
##                 StdDev     Corr
## (Intercept)     0.55670307 (Intr) age    gndrGr
## age             0.69451399  0.037
## genderGirls     0.85245185 -0.550 -0.002
## age:genderGirls 0.72523247 -0.095 -0.948  0.131
## Residual        0.56947555
##
## Fixed effects: weight ~ age + age2 + gender
##                  Value   Std.Error  DF    t-value p-value
## (Intercept)  3.8204364 0.160115052 128  23.860570  0.0000
## age          7.6149743 0.235176612 128  32.379811  0.0000
## age2        -1.6464243 0.087451619 128 -18.826688  0.0000
## genderGirls -0.6088234 0.203186635  66  -2.996375  0.0038
##  Correlation:
##             (Intr) age    age2
## age         -0.543
## age2         0.520 -0.945
## genderGirls -0.531 -0.048  0.011
##
## Standardized Within-Group Residuals:
##           Min            Q1           Med            Q3           Max
## -2.0240494008 -0.4399955398 -0.0095445836  0.4564970984  2.7439593591
##
## Number of Observations: 198
## Number of Groups: 68
growth <- growth %>%
mutate(boy = as.numeric(gender == "Boys"),
girl = as.numeric(gender == "Girls")) %>%
mutate(age_boy = age*boy,
age_girl = age*girl)

#M <- lmer(weight ~ age + age2 + girl + (age_boy |id) + (age_girl| id), data = growth, REML = TRUE)

#growth <- growth %>%
#  mutate(boy = ifelse(gender == "Boys", 1, 0),
#         girl = ifelse(gender == "Girls", 1, 0),
#         age_boy = age*boy,
#         age_girl = age*girl)
#M_growth_l22 <- lme(fixed = weight ~ age + age2 + girl,
#                    random = list( ~ girl + age_girl | id,
#                                   ~ boy + age_boy | id),
#                   data = growth, method = "REML", na.action = na.omit)
#summary(M_growth_l22)
M_growth <- lme(fixed = weight ~ age + age2 + gender, random = ~ age|id, data = growth, method = "REML",na.action = na.omit)
anova(M_growth_l2, M_growth)
##             Model df       AIC       BIC     logLik   Test   L.Ratio p-value
## M_growth_l2     1 15 539.92465 588.94252 -254.96233
## M_growth        2  8 533.95003 560.09290 -258.97501 1 vs 2 8.0253784  0.3304

## 64.3 分析策略

1. 是想了解某一個共變量在層內 (同一個體不同時間，或者統一學校不同學生之間) 的條件效應 (conditional effect)?
2. 是想探索層內和層間數據的變化程度?
3. 是想了解一個共變量的邊際效應 (marginal effect) 嗎?

### 64.3.1 模型選擇和建模步驟

• 第一步:

• 第二步:

1. 用 Wald 檢驗 (當使用 REML 時)，或者 LRT (使用 ML 時) 來精簡化固定效應部分。
2. 反復檢查殘差，異常值，以及擬合值跟觀測值
3. 使用模型的預測軌跡和觀測值的點做視覺比較
4. 用人話把你的模型解釋給老奶奶聽懂

### References

Verbeke, Geert. 1997. Linear Mixed Models for Longitudinal Data. Springer.