library("gamlss2")
## orthdontic measurement data
data("Orthodont", package = "nlme")
## model using lme()
<- lme(distance ~ I(age-11), data = Orthodont,
m random =~ I(age-11) | Subject, method = "ML")
## using re(), function I() is not supported,
## please transform all variables in advance
$age11 <- Orthodont$age - 11
Orthodont
## estimation using the re() constructor,
## setup formula first
<- distance ~ s(age,k=3) + re(random =~ age11 | Subject)
f
## estimate model
<- gamlss2(f, data = Orthodont) b
GAMLSS-RS iteration 1: Global Deviance = 326.6566 eps = 0.392364
GAMLSS-RS iteration 2: Global Deviance = 326.0942 eps = 0.001721
GAMLSS-RS iteration 3: Global Deviance = 326.0782 eps = 0.000049
GAMLSS-RS iteration 4: Global Deviance = 326.0769 eps = 0.000004
## compare fitted values
plot(fitted(b, model = "mu"), fitted(m))
abline(0, 1, col = 4)
## extract summary for re() model term
<- specials(b, model = "mu", term = "age11", elements = "model")
st summary(st)
Linear mixed-effects model fit by maximum likelihood
Data: x$data
AIC BIC logLik
448.9844 462.395 -219.4922
Random effects:
Formula: ~age11 | Subject
Structure: General positive-definite, Log-Cholesky parametrization
StdDev Corr
(Intercept) 2.0910678 (Intr)
age11 0.2157607 0.518
Residual 1.1939543
Variance function:
Structure: fixed weights
Formula: ~weights_w
Fixed effects: x$fixed
Value Std.Error DF t-value p-value
(Intercept) -2.074577e-07 0.404892 81 -5.123779e-07 1
Standardized Within-Group Residuals:
Min Q1 Med Q3 Max
-3.31636025 -0.47633680 0.02042094 0.46926855 3.94312339
Number of Observations: 108
Number of Groups: 27
## random intercepts and slopes with s() using AIC
<- gamlss2(distance ~ s(age,k=3) + s(Subject, bs = "re") + s(Subject, age11, bs = "re"),
a data = Orthodont)
GAMLSS-RS iteration 1: Global Deviance = 427.4959 eps = 0.204786
GAMLSS-RS iteration 2: Global Deviance = 370.4092 eps = 0.133537
GAMLSS-RS iteration 3: Global Deviance = 340.4089 eps = 0.080992
GAMLSS-RS iteration 4: Global Deviance = 325.4676 eps = 0.043892
GAMLSS-RS iteration 5: Global Deviance = 319.3778 eps = 0.018710
GAMLSS-RS iteration 6: Global Deviance = 317.1286 eps = 0.007042
GAMLSS-RS iteration 7: Global Deviance = 316.3204 eps = 0.002548
GAMLSS-RS iteration 8: Global Deviance = 316.0276 eps = 0.000925
GAMLSS-RS iteration 9: Global Deviance = 315.913 eps = 0.000362
GAMLSS-RS iteration 10: Global Deviance = 315.865 eps = 0.000151
GAMLSS-RS iteration 11: Global Deviance = 315.8413 eps = 0.000075
GAMLSS-RS iteration 12: Global Deviance = 315.828 eps = 0.000042
GAMLSS-RS iteration 13: Global Deviance = 315.8178 eps = 0.000032
GAMLSS-RS iteration 14: Global Deviance = 315.8112 eps = 0.000020
GAMLSS-RS iteration 15: Global Deviance = 315.8054 eps = 0.000018
GAMLSS-RS iteration 16: Global Deviance = 315.8023 eps = 0.000009
## compare fitted values
plot(fitted(b, model = "mu"), fitted(m))
points(fitted(a, model = "mu"), fitted(m), col = 2)
abline(0, 1, col = 4)
## more complicated correlation structures.
data("Ovary", package = "nlme")
## ARMA model
<- lme(follicles ~ sin(2 * pi * Time) + cos(2 * pi * Time), data = Ovary,
m random = pdDiag(~sin(2*pi*Time)), correlation = corARMA(q = 2))
## now with gamlss2(), transform in advance
$sin1 <- sin(2 * pi * Ovary$Time)
Ovary$cos1 <- cos(2 * pi * Ovary$Time)
Ovary
## model formula
<- follicles ~ s(Time) + re(random =~ sin1 | Mare,
f correlation = corARMA(q = 2), control = lmeControl(maxIter = 100))
## estimate model
<- gamlss2(f, data = Ovary) b
GAMLSS-RS iteration 1: Global Deviance = 1560.1177 eps = 0.165409
GAMLSS-RS iteration 2: Global Deviance = 1551.2682 eps = 0.005672
GAMLSS-RS iteration 3: Global Deviance = 1551.132 eps = 0.000087
GAMLSS-RS iteration 4: Global Deviance = 1551.1212 eps = 0.000006
## smooth random effects
<- follicles ~ ti(Time) + ti(Mare, bs = "re") +
f ti(Mare, Time, bs = c("re", "cr"), k = c(11, 5))
<- gamlss2(f, data = Ovary) g
GAMLSS-RS iteration 1: Global Deviance = 1522.1073 eps = 0.185743
GAMLSS-RS iteration 2: Global Deviance = 1436.5015 eps = 0.056241
GAMLSS-RS iteration 3: Global Deviance = 1426.6605 eps = 0.006850
GAMLSS-RS iteration 4: Global Deviance = 1425.7658 eps = 0.000627
GAMLSS-RS iteration 5: Global Deviance = 1425.6839 eps = 0.000057
GAMLSS-RS iteration 6: Global Deviance = 1425.6744 eps = 0.000006
## compare fitted values
par(mfrow = n2mfrow(nlevels(Ovary$Mare)), mar = c(4, 4, 1, 1))
for(j in levels(Ovary$Mare)) {
<- subset(Ovary, Mare == j)
ds
plot(follicles ~ Time, data = ds)
<- fitted(b, model = "mu")[Ovary$Mare == j]
f lines(f ~ ds$Time, col = 4, lwd = 2)
<- fitted(g, model = "mu")[Ovary$Mare == j]
f lines(f ~ ds$Time, col = 3, lwd = 2)
<- fitted(m)[Ovary$Mare == j]
f lines(f ~ ds$Time, col = 2, lwd = 2)
}