R Under development (unstable) (2024-04-16 r86430) -- "Unsuffered Consequences"
Copyright (C) 2024 The R Foundation for Statistical Computing
Platform: x86_64-pc-linux-gnu

R is free software and comes with ABSOLUTELY NO WARRANTY.
You are welcome to redistribute it under certain conditions.
Type 'license()' or 'licence()' for distribution details.

R is a collaborative project with many contributors.
Type 'contributors()' for more information and
'citation()' on how to cite R or R packages in publications.

Type 'demo()' for some demos, 'help()' for on-line help, or
'help.start()' for an HTML browser interface to help.
Type 'q()' to quit R.

> ## run reproduction scripts from the NLME book chapters
> testdir <- system.file("scripts", package = "nlme", mustWork = TRUE)
> scripts <- dir(testdir, pattern = "^ch[0-9]*\\.R$")
> for(f in scripts) {
+     writeLines(c("", strrep("=", nchar(f)), basename(f), strrep("=", nchar(f))))
+     set.seed(3)
+     options(warn = 1)  # chapters set digits
+     source(file.path(testdir, f), echo = TRUE,
+            max.deparse.length = Inf, keep.source = TRUE)
+ }

======
ch01.R
======

> #-*- R -*-
> 
> library(nlme)

> pdf(file = 'ch01.pdf')

> options( width = 65, digits = 5 )

> options( contrasts = c(unordered = "contr.helmert", ordered = "contr.poly") )

> # Chapter 1    Linear Mixed-Effects Models: Basic Concepts and Examples
> 
> # 1.1 A Simple Example of Random Effects
> 
> Rail
Grouped Data: travel ~ 1 | Rail
   Rail travel
1     1     55
2     1     53
3     1     54
4     2     26
5     2     37
6     2     32
7     3     78
8     3     91
9     3     85
10    4     92
11    4    100
12    4     96
13    5     49
14    5     51
15    5     50
16    6     80
17    6     85
18    6     83

> fm1Rail.lm <- lm( travel ~ 1, data = Rail )

> fm1Rail.lm

Call:
lm(formula = travel ~ 1, data = Rail)

Coefficients:
(Intercept)  
       66.5  


> fm2Rail.lm <- lm( travel ~ Rail - 1, data = Rail )

> fm2Rail.lm

Call:
lm(formula = travel ~ Rail - 1, data = Rail)

Coefficients:
Rail2  Rail5  Rail1  Rail6  Rail3  Rail4  
 31.7   50.0   54.0   82.7   84.7   96.0  


> fm1Rail.lme <- lme(travel ~ 1, data = Rail, random = ~ 1 | Rail)

> summary( fm1Rail.lme )
Linear mixed-effects model fit by REML
  Data: Rail 
     AIC    BIC  logLik
  128.18 130.68 -61.089

Random effects:
 Formula: ~1 | Rail
        (Intercept) Residual
StdDev:      24.805   4.0208

Fixed effects:  travel ~ 1 
            Value Std.Error DF t-value p-value
(Intercept)  66.5    10.171 12  6.5382       0

Standardized Within-Group Residuals:
      Min        Q1       Med        Q3       Max 
-1.618827 -0.282177  0.035693  0.219558  1.614377 

Number of Observations: 18
Number of Groups: 6 

> fm1Rail.lmeML <- update( fm1Rail.lme, method = "ML" )

> summary( fm1Rail.lmeML )
Linear mixed-effects model fit by maximum likelihood
  Data: Rail 
     AIC    BIC logLik
  134.56 137.23 -64.28

Random effects:
 Formula: ~1 | Rail
        (Intercept) Residual
StdDev:      22.624   4.0208

Fixed effects:  travel ~ 1 
            Value Std.Error DF t-value p-value
(Intercept)  66.5     9.554 12  6.9604       0

Standardized Within-Group Residuals:
      Min        Q1       Med        Q3       Max 
-1.610981 -0.288870  0.034542  0.213728  1.622223 

Number of Observations: 18
Number of Groups: 6 

> plot( fm1Rail.lme )   # produces Figure 1.4

> intervals( fm1Rail.lme )
Approximate 95% confidence intervals

 Fixed effects:
             lower est.  upper
(Intercept) 44.339 66.5 88.661

 Random Effects:
  Level: Rail 
                 lower   est.  upper
sd((Intercept)) 13.274 24.805 46.353

 Within-group standard error:
 lower   est.  upper 
2.6950 4.0208 5.9987 

> anova( fm1Rail.lme )
            numDF denDF F-value p-value
(Intercept)     1    12  42.748  <.0001

> # 1.2 A Randomized Block Design
> 
> plot.design( ergoStool )   # produces Figure 1.6

> contrasts( ergoStool$Type )
   [,1] [,2] [,3]
T1   -1   -1   -1
T2    1   -1   -1
T3    0    2   -1
T4    0    0    3

> ergoStool1 <- ergoStool[ ergoStool$Subject == "1", ]

> model.matrix( effort ~ Type, ergoStool1 )   # X matrix for Subject 1
  (Intercept) Type1 Type2 Type3
1           1    -1    -1    -1
2           1     1    -1    -1
3           1     0     2    -1
4           1     0     0     3
attr(,"assign")
[1] 0 1 1 1
attr(,"contrasts")
attr(,"contrasts")$Type
[1] "contr.helmert"


> fm1Stool <-
+   lme(effort ~ Type, data = ergoStool, random = ~ 1 | Subject)

> summary( fm1Stool )
Linear mixed-effects model fit by REML
  Data: ergoStool 
     AIC    BIC  logLik
  139.49 148.28 -63.743

Random effects:
 Formula: ~1 | Subject
        (Intercept) Residual
StdDev:      1.3325   1.1003

Fixed effects:  effort ~ Type 
              Value Std.Error DF t-value p-value
(Intercept) 10.2500   0.48052 24 21.3309  0.0000
Type1        1.9444   0.25934 24  7.4976  0.0000
Type2        0.0926   0.14973 24  0.6184  0.5421
Type3       -0.3426   0.10588 24 -3.2358  0.0035
 Correlation: 
      (Intr) Type1 Type2
Type1 0                 
Type2 0      0          
Type3 0      0     0    

Standardized Within-Group Residuals:
      Min        Q1       Med        Q3       Max 
-1.802003 -0.643166  0.057831  0.700997  1.631421 

Number of Observations: 36
Number of Groups: 9 

> anova( fm1Stool )
            numDF denDF F-value p-value
(Intercept)     1    24  455.01  <.0001
Type            3    24   22.36  <.0001

> options( contrasts = c( factor = "contr.treatment",
+                         ordered = "contr.poly" ) )

> contrasts( ergoStool$Type )
   T2 T3 T4
T1  0  0  0
T2  1  0  0
T3  0  1  0
T4  0  0  1

> fm2Stool <-
+   lme(effort ~ Type, data = ergoStool, random = ~ 1 | Subject)

> summary( fm2Stool )
Linear mixed-effects model fit by REML
  Data: ergoStool 
     AIC    BIC  logLik
  133.13 141.93 -60.565

Random effects:
 Formula: ~1 | Subject
        (Intercept) Residual
StdDev:      1.3325   1.1003

Fixed effects:  effort ~ Type 
             Value Std.Error DF t-value p-value
(Intercept) 8.5556   0.57601 24 14.8531  0.0000
TypeT2      3.8889   0.51868 24  7.4976  0.0000
TypeT3      2.2222   0.51868 24  4.2843  0.0003
TypeT4      0.6667   0.51868 24  1.2853  0.2110
 Correlation: 
       (Intr) TypeT2 TypeT3
TypeT2 -0.45               
TypeT3 -0.45   0.50        
TypeT4 -0.45   0.50   0.50 

Standardized Within-Group Residuals:
      Min        Q1       Med        Q3       Max 
-1.802003 -0.643166  0.057831  0.700997  1.631421 

Number of Observations: 36
Number of Groups: 9 

> anova( fm2Stool )
            numDF denDF F-value p-value
(Intercept)     1    24  455.01  <.0001
Type            3    24   22.36  <.0001

> model.matrix( effort ~ Type - 1, ergoStool1 )
  TypeT1 TypeT2 TypeT3 TypeT4
1      1      0      0      0
2      0      1      0      0
3      0      0      1      0
4      0      0      0      1
attr(,"assign")
[1] 1 1 1 1
attr(,"contrasts")
attr(,"contrasts")$Type
[1] "contr.treatment"


> fm3Stool <-
+  lme(effort ~ Type - 1, data = ergoStool, random = ~ 1 | Subject)

> summary( fm3Stool )
Linear mixed-effects model fit by REML
  Data: ergoStool 
     AIC    BIC  logLik
  133.13 141.93 -60.565

Random effects:
 Formula: ~1 | Subject
        (Intercept) Residual
StdDev:      1.3325   1.1003

Fixed effects:  effort ~ Type - 1 
         Value Std.Error DF t-value p-value
TypeT1  8.5556   0.57601 24  14.853       0
TypeT2 12.4444   0.57601 24  21.605       0
TypeT3 10.7778   0.57601 24  18.711       0
TypeT4  9.2222   0.57601 24  16.011       0
 Correlation: 
       TypeT1 TypeT2 TypeT3
TypeT2 0.595               
TypeT3 0.595  0.595        
TypeT4 0.595  0.595  0.595 

Standardized Within-Group Residuals:
      Min        Q1       Med        Q3       Max 
-1.802003 -0.643166  0.057831  0.700997  1.631421 

Number of Observations: 36
Number of Groups: 9 

> anova( fm3Stool )
     numDF denDF F-value p-value
Type     4    24  130.52  <.0001

> intervals( fm1Stool )
Approximate 95% confidence intervals

 Fixed effects:
               lower      est.    upper
(Intercept)  9.25825 10.250000 11.24175
Type1        1.40919  1.944444  2.47970
Type2       -0.21644  0.092593  0.40162
Type3       -0.56111 -0.342593 -0.12408

 Random Effects:
  Level: Subject 
                  lower   est.  upper
sd((Intercept)) 0.74962 1.3325 2.3685

 Within-group standard error:
  lower    est.   upper 
0.82957 1.10029 1.45937 

> plot( fm1Stool,   # produces Figure 1.8
+       form = resid(., type = "p") ~ fitted(.) | Subject,
+       abline = 0 )

> # 1.3  Mixed-effects Models for Replicated, Blocked Designs
> 
> with(Machines, interaction.plot( Machine, Worker, score, las = 1))   # Figure 1.10

> fm1Machine <-
+   lme( score ~ Machine, data = Machines, random = ~ 1 | Worker )

> fm1Machine
Linear mixed-effects model fit by REML
  Data: Machines 
  Log-restricted-likelihood: -143.44
  Fixed: score ~ Machine 
(Intercept)    MachineB    MachineC 
    52.3556      7.9667     13.9167 

Random effects:
 Formula: ~1 | Worker
        (Intercept) Residual
StdDev:      5.1466   3.1616

Number of Observations: 54
Number of Groups: 6 

> fm2Machine <- update( fm1Machine, random = ~ 1 | Worker/Machine )

> fm2Machine
Linear mixed-effects model fit by REML
  Data: Machines 
  Log-restricted-likelihood: -107.84
  Fixed: score ~ Machine 
(Intercept)    MachineB    MachineC 
    52.3556      7.9667     13.9167 

Random effects:
 Formula: ~1 | Worker
        (Intercept)
StdDev:       4.781

 Formula: ~1 | Machine %in% Worker
        (Intercept) Residual
StdDev:      3.7295  0.96158

Number of Observations: 54
Number of Groups: 
             Worker Machine %in% Worker 
                  6                  18 

> anova( fm1Machine, fm2Machine )
           Model df    AIC    BIC  logLik   Test L.Ratio p-value
fm1Machine     1  5 296.88 306.54 -143.44                       
fm2Machine     2  6 227.69 239.28 -107.84 1 vs 2  71.191  <.0001

>   ## delete selected rows from the Machines data
> MachinesUnbal <- Machines[ -c(2,3,6,8,9,12,19,20,27,33), ]

>   ## check that the result is indeed unbalanced
> table(MachinesUnbal$Machine, MachinesUnbal$Worker)
   
    6 2 4 1 3 5
  A 3 2 2 1 1 3
  B 3 3 3 1 2 2
  C 3 3 3 3 3 3

> fm1MachinesU <- lme( score ~ Machine, data = MachinesUnbal,
+   random = ~ 1 | Worker/Machine )

> fm1MachinesU
Linear mixed-effects model fit by REML
  Data: MachinesUnbal 
  Log-restricted-likelihood: -90.936
  Fixed: score ~ Machine 
(Intercept)    MachineB    MachineC 
    52.3540      7.9624     13.9182 

Random effects:
 Formula: ~1 | Worker
        (Intercept)
StdDev:      4.7387

 Formula: ~1 | Machine %in% Worker
        (Intercept) Residual
StdDev:      3.7728   0.9332

Number of Observations: 44
Number of Groups: 
             Worker Machine %in% Worker 
                  6                  18 

> intervals( fm1MachinesU )
Approximate 95% confidence intervals

 Fixed effects:
              lower    est.  upper
(Intercept) 47.2345 52.3540 57.474
MachineB     3.0278  7.9624 12.897
MachineC     8.9955 13.9182 18.841

 Random Effects:
  Level: Worker 
                 lower   est.  upper
sd((Intercept)) 2.2162 4.7387 10.132
  Level: Machine 
                 lower   est.  upper
sd((Intercept)) 2.4091 3.7728 5.9085

 Within-group standard error:
  lower    est.   upper 
0.71113 0.93320 1.22463 

> fm4Stool <- lme( effort ~ Type, ergoStool, ~ 1 | Subject/Type )

> if (interactive()) intervals( fm4Stool )

> (fm1Stool$sigma)^2
[1] 1.2106

> (fm4Stool$sigma)^2 + 0.79621^2
[1] 0.84554

> Machine1 <- Machines[ Machines$Worker == "1", ]

> model.matrix( score ~ Machine, Machine1 )   # fixed-effects X_i
   (Intercept) MachineB MachineC
1            1        0        0
2            1        0        0
3            1        0        0
19           1        1        0
20           1        1        0
21           1        1        0
37           1        0        1
38           1        0        1
39           1        0        1
attr(,"assign")
[1] 0 1 1
attr(,"contrasts")
attr(,"contrasts")$Machine
[1] "contr.treatment"


> model.matrix( ~ Machine - 1, Machine1 )   # random-effects Z_i
   MachineA MachineB MachineC
1         1        0        0
2         1        0        0
3         1        0        0
19        0        1        0
20        0        1        0
21        0        1        0
37        0        0        1
38        0        0        1
39        0        0        1
attr(,"assign")
[1] 1 1 1
attr(,"contrasts")
attr(,"contrasts")$Machine
[1] "contr.treatment"


> fm3Machine <- update( fm1Machine, random = ~Machine - 1 |Worker)

> summary( fm3Machine )
Linear mixed-effects model fit by REML
  Data: Machines 
     AIC    BIC  logLik
  228.31 247.63 -104.16

Random effects:
 Formula: ~Machine - 1 | Worker
 Structure: General positive-definite, Log-Cholesky parametrization
         StdDev  Corr         
MachineA 4.07928 MachnA MachnB
MachineB 8.62529 0.803        
MachineC 4.38948 0.623  0.771 
Residual 0.96158              

Fixed effects:  score ~ Machine 
             Value Std.Error DF t-value p-value
(Intercept) 52.356    1.6807 46 31.1508  0.0000
MachineB     7.967    2.4209 46  3.2909  0.0019
MachineC    13.917    1.5401 46  9.0362  0.0000
 Correlation: 
         (Intr) MachnB
MachineB  0.463       
MachineC -0.374  0.301

Standardized Within-Group Residuals:
      Min        Q1       Med        Q3       Max 
-2.393540 -0.513776  0.026908  0.472455  2.533387 

Number of Observations: 54
Number of Groups: 6 

> anova( fm1Machine, fm2Machine, fm3Machine )
           Model df    AIC    BIC  logLik   Test L.Ratio p-value
fm1Machine     1  5 296.88 306.54 -143.44                       
fm2Machine     2  6 227.69 239.28 -107.84 1 vs 2  71.191  <.0001
fm3Machine     3 10 228.31 247.63 -104.16 2 vs 3   7.376  0.1173

> # 1.4 An Analysis of Covariance Model
> 
> names( Orthodont )
[1] "distance" "age"      "Subject"  "Sex"     

> levels( Orthodont$Sex )
[1] "Male"   "Female"

> OrthoFem <- Orthodont[ Orthodont$Sex == "Female", ]

> fm1OrthF.lis <- lmList( distance ~ age, data = OrthoFem )

> coef( fm1OrthF.lis )
    (Intercept)   age
F10       13.55 0.450
F09       18.10 0.275
F06       17.00 0.375
F01       17.25 0.375
F05       19.60 0.275
F07       16.95 0.550
F02       14.20 0.800
F08       21.45 0.175
F03       14.40 0.850
F04       19.65 0.475
F11       18.95 0.675

> intervals( fm1OrthF.lis )
, , (Intercept)

     lower  est.  upper
F10 10.071 13.55 17.029
F09 14.621 18.10 21.579
F06 13.521 17.00 20.479
F01 13.771 17.25 20.729
F05 16.121 19.60 23.079
F07 13.471 16.95 20.429
F02 10.721 14.20 17.679
F08 17.971 21.45 24.929
F03 10.921 14.40 17.879
F04 16.171 19.65 23.129
F11 15.471 18.95 22.429

, , age

      lower  est.  upper
F10  0.1401 0.450 0.7599
F09 -0.0349 0.275 0.5849
F06  0.0651 0.375 0.6849
F01  0.0651 0.375 0.6849
F05 -0.0349 0.275 0.5849
F07  0.2401 0.550 0.8599
F02  0.4901 0.800 1.1099
F08 -0.1349 0.175 0.4849
F03  0.5401 0.850 1.1599
F04  0.1651 0.475 0.7849
F11  0.3651 0.675 0.9849


> plot( intervals ( fm1OrthF.lis ) )   # produces Figure 1.12

> fm2OrthF.lis <- update( fm1OrthF.lis, distance ~ I( age - 11 ) )

> plot( intervals( fm2OrthF.lis ) )    # produces Figure 1.13

> fm1OrthF <-
+   lme( distance ~ age, data = OrthoFem, random = ~ 1 | Subject )

> summary( fm1OrthF )
Linear mixed-effects model fit by REML
  Data: OrthoFem 
     AIC    BIC  logLik
  149.22 156.17 -70.609

Random effects:
 Formula: ~1 | Subject
        (Intercept) Residual
StdDev:      2.0685  0.78003

Fixed effects:  distance ~ age 
              Value Std.Error DF t-value p-value
(Intercept) 17.3727   0.85874 32 20.2304       0
age          0.4795   0.05259 32  9.1186       0
 Correlation: 
    (Intr)
age -0.674

Standardized Within-Group Residuals:
     Min       Q1      Med       Q3      Max 
-2.27365 -0.70902  0.17282  0.41221  1.63252 

Number of Observations: 44
Number of Groups: 11 

> fm1OrthFM <- update( fm1OrthF, method = "ML" )

> summary( fm1OrthFM )
Linear mixed-effects model fit by maximum likelihood
  Data: OrthoFem 
     AIC    BIC  logLik
  146.03 153.17 -69.015

Random effects:
 Formula: ~1 | Subject
        (Intercept) Residual
StdDev:      1.9699  0.76812

Fixed effects:  distance ~ age 
              Value Std.Error DF t-value p-value
(Intercept) 17.3727   0.85063 32 20.4234       0
age          0.4795   0.05301 32  9.0471       0
 Correlation: 
    (Intr)
age -0.685

Standardized Within-Group Residuals:
     Min       Q1      Med       Q3      Max 
-2.30562 -0.71924  0.17636  0.42580  1.66894 

Number of Observations: 44
Number of Groups: 11 

> fm2OrthF <- update( fm1OrthF, random = ~ age | Subject )

> anova( fm1OrthF, fm2OrthF )
         Model df    AIC    BIC  logLik   Test L.Ratio p-value
fm1OrthF     1  4 149.22 156.17 -70.609                       
fm2OrthF     2  6 149.43 159.85 -68.714 1 vs 2  3.7896  0.1503

> random.effects( fm1OrthF )
    (Intercept)
F10   -4.005329
F09   -1.470449
F06   -1.470449
F01   -1.229032
F05   -0.021947
F07    0.340179
F02    0.340179
F08    0.702304
F03    1.064430
F04    2.150807
F11    3.599309

> ranef( fm1OrthFM )
    (Intercept)
F10   -3.995835
F09   -1.466964
F06   -1.466964
F01   -1.226119
F05   -0.021895
F07    0.339372
F02    0.339372
F08    0.700640
F03    1.061907
F04    2.145709
F11    3.590778

> coef( fm1OrthF )
    (Intercept)     age
F10      13.367 0.47955
F09      15.902 0.47955
F06      15.902 0.47955
F01      16.144 0.47955
F05      17.351 0.47955
F07      17.713 0.47955
F02      17.713 0.47955
F08      18.075 0.47955
F03      18.437 0.47955
F04      19.524 0.47955
F11      20.972 0.47955

> plot( compareFits(coef(fm1OrthF), coef(fm1OrthFM)))   # Figure 1.15

> plot( augPred(fm1OrthF), aspect = "xy", grid = TRUE )   # Figure 1.16

> # 1.5  Models for Nested Classification Factors
> 
> fm1Pixel <- lme( pixel ~ day + I(day^2), data = Pixel,
+   random = list( Dog = ~ day, Side = ~ 1 ) )

> intervals( fm1Pixel )
Approximate 95% confidence intervals

 Fixed effects:
                lower       est.     upper
(Intercept) 1053.0968 1073.33914 1093.5814
day            4.3797    6.12960    7.8795
I(day^2)      -0.4349   -0.36735   -0.2998

 Random Effects:
  Level: Dog 
                        lower     est.    upper
sd((Intercept))      15.92760 28.36990 50.53187
sd(day)               1.08139  1.84375  3.14357
cor((Intercept),day) -0.89465 -0.55472  0.19197
  Level: Side 
                 lower   est.  upper
sd((Intercept)) 10.417 16.824 27.173

 Within-group standard error:
  lower    est.   upper 
 7.6345  8.9896 10.5852 

> plot( augPred( fm1Pixel ) )   # produces Figure 1.18

> VarCorr( fm1Pixel )
            Variance       StdDev  Corr  
Dog =       pdLogChol(day)               
(Intercept) 804.8514       28.3699 (Intr)
day           3.3994        1.8438 -0.555
Side =      pdLogChol(1)                 
(Intercept) 283.0572       16.8243       
Residual     80.8130        8.9896       

> summary( fm1Pixel )
Linear mixed-effects model fit by REML
  Data: Pixel 
     AIC    BIC  logLik
  841.21 861.97 -412.61

Random effects:
 Formula: ~day | Dog
 Structure: General positive-definite, Log-Cholesky parametrization
            StdDev  Corr  
(Intercept) 28.3699 (Intr)
day          1.8438 -0.555

 Formula: ~1 | Side %in% Dog
        (Intercept) Residual
StdDev:      16.824   8.9896

Fixed effects:  pixel ~ day + I(day^2) 
              Value Std.Error DF t-value p-value
(Intercept) 1073.34   10.1717 80 105.522       0
day            6.13    0.8793 80   6.971       0
I(day^2)      -0.37    0.0339 80 -10.822       0
 Correlation: 
         (Intr) day   
day      -0.517       
I(day^2)  0.186 -0.668

Standardized Within-Group Residuals:
      Min        Q1       Med        Q3       Max 
-2.829057 -0.449181  0.025549  0.557216  2.751965 

Number of Observations: 102
Number of Groups: 
          Dog Side %in% Dog 
           10            20 

> fm2Pixel <- update( fm1Pixel, random = ~ day | Dog)

> anova( fm1Pixel, fm2Pixel )
         Model df    AIC    BIC  logLik   Test L.Ratio p-value
fm1Pixel     1  8 841.21 861.97 -412.61                       
fm2Pixel     2  7 884.52 902.69 -435.26 1 vs 2  45.309  <.0001

> fm3Pixel <- update( fm1Pixel, random = ~ 1 | Dog/Side )

> anova( fm1Pixel, fm3Pixel )
         Model df    AIC    BIC  logLik   Test L.Ratio p-value
fm1Pixel     1  8 841.21 861.97 -412.61                       
fm3Pixel     2  6 876.84 892.41 -432.42 1 vs 2  39.629  <.0001

> fm4Pixel <- update( fm1Pixel, pixel ~ day + I(day^2) + Side )

> summary( fm4Pixel )
Linear mixed-effects model fit by REML
  Data: Pixel 
     AIC    BIC  logLik
  835.85 859.12 -408.93

Random effects:
 Formula: ~day | Dog
 Structure: General positive-definite, Log-Cholesky parametrization
            StdDev  Corr  
(Intercept) 28.4636 (Intr)
day          1.8438 -0.553

 Formula: ~1 | Side %in% Dog
        (Intercept) Residual
StdDev:      16.507   8.9836

Fixed effects:  pixel ~ day + I(day^2) + Side 
              Value Std.Error DF t-value p-value
(Intercept) 1077.95   10.8627 80  99.234  0.0000
day            6.13    0.8790 80   6.973  0.0000
I(day^2)      -0.37    0.0339 80 -10.829  0.0000
SideR         -9.22    7.6268  9  -1.209  0.2576
 Correlation: 
         (Intr) day    I(d^2)
day      -0.484              
I(day^2)  0.174 -0.667       
SideR    -0.351  0.000  0.000

Standardized Within-Group Residuals:
      Min        Q1       Med        Q3       Max 
-2.809825 -0.471334  0.026103  0.541154  2.774701 

Number of Observations: 102
Number of Groups: 
          Dog Side %in% Dog 
           10            20 

> # 1.6  A Split-Plot Experiment
> 
> fm1Oats <- lme( yield ~ ordered(nitro) * Variety, data = Oats,
+                random = ~ 1 | Block/Variety )

> anova( fm1Oats )
                       numDF denDF F-value p-value
(Intercept)                1    45 245.143  <.0001
ordered(nitro)             3    45  37.686  <.0001
Variety                    2    10   1.485  0.2724
ordered(nitro):Variety     6    45   0.303  0.9322

> fm2Oats <- update( fm1Oats, yield ~ ordered(nitro) + Variety )

> anova( fm2Oats )
               numDF denDF F-value p-value
(Intercept)        1    51 245.145  <.0001
ordered(nitro)     3    51  41.053  <.0001
Variety            2    10   1.485  0.2724

> summary( fm2Oats )
Linear mixed-effects model fit by REML
  Data: Oats 
     AIC    BIC  logLik
  587.46 607.16 -284.73

Random effects:
 Formula: ~1 | Block
        (Intercept)
StdDev:      14.645

 Formula: ~1 | Variety %in% Block
        (Intercept) Residual
StdDev:      10.473    12.75

Fixed effects:  yield ~ ordered(nitro) + Variety 
                    Value Std.Error DF t-value p-value
(Intercept)       104.500    7.7975 51 13.4017  0.0000
ordered(nitro).L   32.945    3.0052 51 10.9627  0.0000
ordered(nitro).Q   -5.167    3.0052 51 -1.7193  0.0916
ordered(nitro).C   -0.447    3.0052 51 -0.1488  0.8823
VarietyMarvellous   5.292    7.0789 10  0.7475  0.4720
VarietyVictory     -6.875    7.0789 10 -0.9712  0.3544
 Correlation: 
                  (Intr) or().L or().Q or().C VrtyMr
ordered(nitro).L   0.000                            
ordered(nitro).Q   0.000  0.000                     
ordered(nitro).C   0.000  0.000  0.000              
VarietyMarvellous -0.454  0.000  0.000  0.000       
VarietyVictory    -0.454  0.000  0.000  0.000  0.500

Standardized Within-Group Residuals:
      Min        Q1       Med        Q3       Max 
-1.841341 -0.662797 -0.066943  0.638225  1.660668 

Number of Observations: 72
Number of Groups: 
             Block Variety %in% Block 
                 6                 18 

> fm3Oats <- update( fm1Oats, yield ~ ordered( nitro ) )

> summary( fm3Oats )
Linear mixed-effects model fit by REML
  Data: Oats 
     AIC    BIC logLik
  597.61 613.14 -291.8

Random effects:
 Formula: ~1 | Block
        (Intercept)
StdDev:      14.506

 Formula: ~1 | Variety %in% Block
        (Intercept) Residual
StdDev:      11.039    12.75

Fixed effects:  yield ~ ordered(nitro) 
                   Value Std.Error DF t-value p-value
(Intercept)      103.972    6.6407 51 15.6569  0.0000
ordered(nitro).L  32.945    3.0052 51 10.9627  0.0000
ordered(nitro).Q  -5.167    3.0052 51 -1.7193  0.0916
ordered(nitro).C  -0.447    3.0052 51 -0.1488  0.8823
 Correlation: 
                 (Intr) or().L or().Q
ordered(nitro).L 0                   
ordered(nitro).Q 0      0            
ordered(nitro).C 0      0      0     

Standardized Within-Group Residuals:
      Min        Q1       Med        Q3       Max 
-1.781556 -0.611689  0.022224  0.622007  1.681382 

Number of Observations: 72
Number of Groups: 
             Block Variety %in% Block 
                 6                 18 

> fm4Oats <-
+   lme( yield ~ nitro, data = Oats, random = ~ 1 | Block/Variety )

> summary( fm4Oats )
Linear mixed-effects model fit by REML
  Data: Oats 
     AIC    BIC  logLik
  603.04 614.28 -296.52

Random effects:
 Formula: ~1 | Block
        (Intercept)
StdDev:      14.506

 Formula: ~1 | Variety %in% Block
        (Intercept) Residual
StdDev:      11.005   12.867

Fixed effects:  yield ~ nitro 
             Value Std.Error DF t-value p-value
(Intercept) 81.872    6.9453 53  11.788       0
nitro       73.667    6.7815 53  10.863       0
 Correlation: 
      (Intr)
nitro -0.293

Standardized Within-Group Residuals:
      Min        Q1       Med        Q3       Max 
-1.743808 -0.664752  0.017104  0.542988  1.802989 

Number of Observations: 72
Number of Groups: 
             Block Variety %in% Block 
                 6                 18 

> VarCorr( fm4Oats )
            Variance     StdDev
Block =     pdLogChol(1)       
(Intercept) 210.42       14.506
Variety =   pdLogChol(1)       
(Intercept) 121.10       11.005
Residual    165.56       12.867

> intervals( fm4Oats )
Approximate 95% confidence intervals

 Fixed effects:
             lower   est.  upper
(Intercept) 67.942 81.872 95.803
nitro       60.065 73.667 87.269

 Random Effects:
  Level: Block 
                 lower   est.  upper
sd((Intercept)) 6.6089 14.506 31.839
  Level: Variety 
                 lower   est.  upper
sd((Intercept)) 6.4081 11.005 18.898

 Within-group standard error:
 lower   est.  upper 
10.637 12.867 15.565 

> plot(augPred(fm4Oats), aspect = 2.5, layout = c(6, 3),
+      between = list(x = c(0, 0, 0.5, 0, 0))) # produces Figure 1.21

> # cleanup
> 
> summary(warnings())
No warnings

======
ch02.R
======

> #-*- R -*-
> 
> library( nlme )

> options( width = 65, digits = 5 )

> options( contrasts = c(unordered = "contr.helmert",
+          ordered = "contr.poly") )

> pdf( file = 'ch02.pdf' )

> # Chapter 2    Theory and Computational Methods for Linear Mixed-Effects Models
> 
> # 2.2   Likelihood Estimation for LME Models
> 
> Xmat <- matrix( c(1, 1, 1, 1, 8, 10, 12, 14), ncol = 2 )

> Xmat
     [,1] [,2]
[1,]    1    8
[2,]    1   10
[3,]    1   12
[4,]    1   14

> Xqr <- qr( Xmat )               # creates a QR structure

> qr.R( Xqr )                     # returns R
     [,1]     [,2]
[1,]   -2 -22.0000
[2,]    0  -4.4721

> qr.Q( Xqr )                     # returns Q-truncated
     [,1]     [,2]
[1,] -0.5  0.67082
[2,] -0.5  0.22361
[3,] -0.5 -0.22361
[4,] -0.5 -0.67082

> qr.Q( Xqr, complete = TRUE )    # returns the full Q
     [,1]     [,2]      [,3]     [,4]
[1,] -0.5  0.67082  0.023607  0.54721
[2,] -0.5  0.22361 -0.439345 -0.71202
[3,] -0.5 -0.22361  0.807869 -0.21760
[4,] -0.5 -0.67082 -0.392131  0.38240

> fm1Rail.lme <- lme( travel ~ 1, data = Rail, random = ~ 1 | Rail,
+        control = list( msVerbose = TRUE ) )
  0:     61.048859: -1.81959
  1:     61.048859: -1.81959

> fm1Rail.lme <- lme( travel ~ 1, data = Rail, random = ~ 1 | Rail,
+    control = list( msVerbose = TRUE, niterEM = 0 ))
  0:     67.893737: -0.431523
  1:     61.612483: -1.43152
  2:     61.138913: -1.98441
  3:     61.050114: -1.83866
  4:     61.048866: -1.81819
  5:     61.048859: -1.81960
  6:     61.048859: -1.81959

> fm1Machine <-
+   lme( score ~ Machine, data = Machines, random = ~ 1 | Worker )

> fm2Machine <- update( fm1Machine, random = ~ 1 | Worker/Machine )

> anova( fm1Machine, fm2Machine )
           Model df    AIC    BIC  logLik   Test L.Ratio p-value
fm1Machine     1  5 300.46 310.12 -145.23                       
fm2Machine     2  6 231.27 242.86 -109.64 1 vs 2  71.191  <.0001

> OrthoFem <- Orthodont[ Orthodont$Sex == "Female", ]

> fm1OrthF <- lme( distance ~ age, data = OrthoFem,
+     random = ~ 1 | Subject )

> fm2OrthF <- update( fm1OrthF, random = ~ age | Subject )

> orthLRTsim <- simulate.lme( fm1OrthF, m2 = fm2OrthF, nsim = 1000 )

> plot( orthLRTsim, df = c(1, 2) )    # produces Figure 2.3

> machineLRTsim <- simulate.lme(fm1Machine, m2 = fm2Machine, nsim= 1000)

> plot( machineLRTsim, df = c(0, 1),      # produces Figure 2.4
+  layout = c(4,1), between = list(x = c(0, 0.5, 0)) )

> stoolLRTsim <-
+   simulate.lme( list(fixed = effort ~ 1, data = ergoStool,
+                      random = ~ 1 | Subject),
+                 m2 = list(fixed = effort ~ Type),
+                 method = "ML", nsim = 1000 )

> plot( stoolLRTsim, df = c(3, 4) )    # Figure 2.5

> data( PBIB, package = 'SASmixed' )

> pbibLRTsim <-
+     simulate.lme(list( fixed = response ~ 1, data = PBIB,
+                        random = ~ 1 | Block ),
+                  m2 = list(fixed = response ~ Treatment, data = PBIB,
+                            random = ~ 1 | Block),
+                  method = "ML", nsim = 1000 )

> plot( pbibLRTsim, df = c(14,16,18), weights = FALSE )    # Figure 2.6

> summary( fm2Machine )
Linear mixed-effects model fit by REML
  Data: Machines 
     AIC    BIC  logLik
  231.27 242.86 -109.64

Random effects:
 Formula: ~1 | Worker
        (Intercept)
StdDev:       4.781

 Formula: ~1 | Machine %in% Worker
        (Intercept) Residual
StdDev:      3.7295  0.96158

Fixed effects:  score ~ Machine 
             Value Std.Error DF t-value p-value
(Intercept) 59.650   2.14467 36 27.8131  0.0000
Machine1     3.983   1.08849 10  3.6595  0.0044
Machine2     3.311   0.62844 10  5.2688  0.0004
 Correlation: 
         (Intr) Machn1
Machine1 0            
Machine2 0      0     

Standardized Within-Group Residuals:
      Min        Q1       Med        Q3       Max 
-2.269587 -0.548466 -0.010706  0.439366  2.540058 

Number of Observations: 54
Number of Groups: 
             Worker Machine %in% Worker 
                  6                  18 

> fm1PBIB <- lme(response ~ Treatment, data = PBIB, random = ~ 1 | Block)

> anova( fm1PBIB )
            numDF denDF F-value p-value
(Intercept)     1    31 1654.21  <.0001
Treatment      14    31    1.53  0.1576

> fm2PBIB <- update( fm1PBIB, method = "ML" )

> fm3PBIB <- update( fm2PBIB, response ~ 1 )

> anova( fm2PBIB, fm3PBIB )
        Model df    AIC    BIC  logLik   Test L.Ratio p-value
fm2PBIB     1 17 56.571 92.174 -11.285                       
fm3PBIB     2  3 52.152 58.435 -23.076 1 vs 2  23.581  0.0514

> anova( fm2Machine )
            numDF denDF F-value p-value
(Intercept)     1    36  773.57  <.0001
Machine         2    10   20.58   3e-04

> # cleanup
> 
> summary(warnings())
No warnings

======
ch03.R
======

> #-*- R -*-
> 
> # initialization
> 
> library(nlme)

> options(width = 65, digits = 5)

> options(contrasts = c(unordered = "contr.helmert", ordered = "contr.poly"))

> pdf(file = 'ch03.pdf')

> # Chapter 3    Describing the Structure of Grouped Data
> 
> # 3.1 The Display Formula and Its Components
> 
> formula( Rail )
travel ~ 1 | Rail

> formula( ergoStool )
effort ~ Type | Subject

> formula( Machines )
score ~ Machine | Worker

> formula( Orthodont )
distance ~ age | Subject

> formula( Pixel )
pixel ~ day | Dog/Side

> formula( Oats )
yield ~ nitro | Block

> table( Oxboys$Subject )

10 26 25  9  2  6  7 17 16 15  8 20  1 18  5 23 11 21  3 24 22 
 9  9  9  9  9  9  9  9  9  9  9  9  9  9  9  9  9  9  9  9  9 
12 13 14 19  4 
 9  9  9  9  9 

> table( getGroups( Oxboys ) )

10 26 25  9  2  6  7 17 16 15  8 20  1 18  5 23 11 21  3 24 22 
 9  9  9  9  9  9  9  9  9  9  9  9  9  9  9  9  9  9  9  9  9 
12 13 14 19  4 
 9  9  9  9  9 

> unique( table( getGroups( Oxboys ) ) )  # a more concise result
[1] 9

> unique( table( getCovariate( Oxboys ), getGroups( Oxboys ) ) )
[1] 1 0

> length( unique( getCovariate( Oxboys ) ) )
[1] 16

> unique( getGroups(Pixel, level = 1) )
 [1] 1  2  3  4  5  6  7  8  9  10
Levels: 1 10 2 3 4 5 6 7 8 9

> unique( getGroups(Pixel, level = 2) )
 [1] 1/R  2/R  3/R  4/R  5/R  6/R  7/R  8/R  9/R  10/R 1/L  2/L 
[13] 3/L  4/L  5/L  6/L  7/L  8/L  9/L  10/L
20 Levels: 1/R < 2/R < 3/R < 4/R < 5/R < 6/R < 7/R < ... < 10/L

> Pixel.groups <- getGroups( Pixel, level = 1:2 )

> class( Pixel.groups )
[1] "data.frame"

> names( Pixel.groups )
[1] "Dog"  "Side"

> unique( Pixel.groups[["Side"]] )
[1] R L
Levels: L R

> formula( PBG )
deltaBP ~ dose | Rabbit

> PBG.log <- update( PBG, formula = deltaBP ~ log(dose) | Rabbit )

> formula(PBG.log)
deltaBP ~ log(dose) | Rabbit
<environment: 0x55aa561e7958>

> unique( getCovariate(PBG.log) )
[1] 1.8326 2.5257 3.2189 3.9120 4.6052 5.2983

> unique( getCovariate(PBG) )
[1]   6.25  12.50  25.00  50.00 100.00 200.00

> # 3.2 Constructing groupedData Objects
> 
> # The next line is not from the book.
> # It is added to ensure that the file is available
> 
> write.table( Oxboys, "oxboys.dat" )

> Oxboys.frm <- read.table( "oxboys.dat", header = TRUE )

> class( Oxboys.frm )        # check the class of the result
[1] "data.frame"

> dim( Oxboys.frm )          # check the dimensions
[1] 234   4

> Oxboys <- groupedData( height ~ age | Subject,
+    data = read.table("oxboys.dat", header = TRUE),
+    labels = list(x = "Centered age", y = "Height"),
+    units = list(y = "(cm)") )

> Oxboys                     # display the object
Grouped Data: height ~ age | Subject
    Subject     age height Occasion
1         1 -1.0000 140.50        1
2         1 -0.7479 143.40        2
3         1 -0.4630 144.80        3
4         1 -0.1643 147.10        4
5         1 -0.0027 147.70        5
6         1  0.2466 150.20        6
7         1  0.5562 151.70        7
8         1  0.7781 153.30        8
9         1  0.9945 155.80        9
10        2 -1.0000 136.90        1
11        2 -0.7479 139.10        2
12        2 -0.4630 140.10        3
13        2 -0.1643 142.60        4
14        2 -0.0027 143.20        5
15        2  0.2466 144.00        6
16        2  0.5562 145.80        7
17        2  0.7781 146.80        8
18        2  0.9945 148.30        9
19        3 -1.0000 150.00        1
20        3 -0.7479 152.10        2
21        3 -0.4630 153.90        3
22        3 -0.1643 155.80        4
23        3 -0.0027 156.00        5
24        3  0.2466 156.90        6
25        3  0.5562 157.40        7
26        3  0.7781 159.10        8
27        3  0.9945 160.60        9
28        4 -1.0000 155.70        1
29        4 -0.7479 158.70        2
30        4 -0.4630 160.60        3
31        4 -0.1643 163.30        4
32        4 -0.0027 164.40        5
33        4  0.2466 167.30        6
34        4  0.5562 170.70        7
35        4  0.7781 172.00        8
36        4  0.9945 174.80        9
37        5 -1.0000 145.80        1
38        5 -0.7479 147.30        2
39        5 -0.4493 148.70        3
40        5 -0.1643 149.78        4
41        5 -0.0027 150.22        5
42        5  0.2466 152.50        6
43        5  0.5562 154.80        7
44        5  0.7781 156.40        8
45        5  0.9973 158.70        9
46        6 -1.0000 142.40        1
47        6 -0.7479 143.80        2
48        6 -0.4630 145.20        3
49        6 -0.1643 146.30        4
50        6 -0.0027 147.10        5
51        6  0.2466 148.10        6
52        6  0.5562 148.90        7
53        6  0.7781 149.10        8
54        6  0.9945 151.00        9
55        7 -1.0000 141.30        1
56        7 -0.7479 142.40        2
57        7 -0.4493 144.30        3
58        7 -0.1643 145.20        4
59        7  0.0000 146.10        5
60        7  0.2466 146.80        6
61        7  0.5562 147.90        7
62        7  0.7945 150.50        8
63        7  0.9945 151.80        9
64        8 -1.0000 141.70        1
65        8 -0.7479 143.70        2
66        8 -0.4630 145.10        3
67        8 -0.1643 147.90        4
68        8 -0.0027 148.10        5
69        8  0.2466 149.60        6
70        8  0.5562 150.99        7
71        8  0.7945 154.10        8
72        8  1.0055 154.90        9
73        9 -1.0000 132.70        1
74        9 -0.7479 134.10        2
75        9 -0.4493 135.30        3
76        9 -0.1643 136.60        4
77        9 -0.0027 137.50        5
78        9  0.2466 139.10        6
79        9  0.5562 140.90        7
80        9  0.7945 143.70        8
81        9  0.9945 144.70        9
82       10 -1.0000 126.20        1
83       10 -0.7479 128.20        2
84       10 -0.4630 129.00        3
85       10 -0.1643 129.40        4
86       10 -0.0027 129.59        5
87       10  0.2466 130.60        6
88       10  0.5562 132.50        7
89       10  0.7781 133.40        8
90       10  0.9945 134.20        9
91       11 -1.0000 142.50        1
92       11 -0.7479 143.80        2
93       11 -0.4630 145.60        3
94       11 -0.1643 148.30        4
95       11 -0.0027 149.40        5
96       11  0.2466 151.60        6
97       11  0.5562 154.80        7
98       11  0.7781 156.90        8
99       11  0.9945 159.20        9
100      12 -1.0000 149.90        1
101      12 -0.7479 151.70        2
102      12 -0.4630 153.30        3
103      12 -0.1643 156.10        4
104      12  0.0000 156.70        5
105      12  0.2466 157.80        6
106      12  0.5562 160.70        7
107      12  0.7781 162.70        8
108      12  0.9945 163.80        9
109      13 -1.0000 148.90        1
110      13 -0.7150 149.80        2
111      13 -0.4630 151.70        3
112      13 -0.1643 154.40        4
113      13 -0.0027 155.50        5
114      13  0.2466 156.40        6
115      13  0.5562 161.40        7
116      13  0.7781 163.90        8
117      13  0.9945 164.60        9
118      14 -1.0000 151.60        1
119      14 -0.7479 153.20        2
120      14 -0.4630 155.20        3
121      14 -0.1643 157.30        4
122      14  0.0000 159.10        5
123      14  0.2466 160.90        6
124      14  0.5562 164.40        7
125      14  0.7781 166.90        8
126      14  0.9945 168.40        9
127      15 -1.0000 137.50        1
128      15 -0.7479 139.30        2
129      15 -0.4630 140.90        3
130      15 -0.1643 142.70        4
131      15 -0.0027 144.20        5
132      15  0.2466 145.70        6
133      15  0.5562 147.09        7
134      15  0.7781 150.20        8
135      15  0.9945 152.30        9
136      16 -1.0000 142.80        1
137      16 -0.7479 144.90        2
138      16 -0.4630 145.00        3
139      16 -0.1643 146.70        4
140      16 -0.0027 147.20        5
141      16  0.2466 148.90        6
142      16  0.5562 150.10        7
143      16  0.7781 151.00        8
144      16  0.9945 152.20        9
145      17 -1.0000 134.90        1
146      17 -0.7479 137.40        2
147      17 -0.4630 138.20        3
148      17 -0.1643 140.20        4
149      17 -0.0027 143.60        5
150      17  0.2466 144.20        6
151      17  0.5562 147.90        7
152      17  0.7781 150.30        8
153      17  0.9945 151.80        9
154      18 -1.0000 145.50        1
155      18 -0.7479 146.20        2
156      18 -0.4630 148.20        3
157      18 -0.1643 150.30        4
158      18 -0.0027 152.00        5
159      18  0.2466 152.30        6
160      18  0.5562 154.30        7
161      18  0.7781 156.20        8
162      18  0.9945 156.80        9
163      19 -1.0000 156.90        1
164      19 -0.7479 157.90        2
165      19 -0.4630 160.30        3
166      19 -0.1643 161.90        4
167      19  0.0000 163.80        5
168      19  0.2466 165.50        6
169      19  0.5562 169.90        7
170      19  0.7781 172.40        8
171      19  0.9945 174.40        9
172      20 -1.0000 146.50        1
173      20 -0.7479 148.40        2
174      20 -0.4630 149.30        3
175      20 -0.1643 151.20        4
176      20 -0.0027 152.10        5
177      20  0.2466 152.40        6
178      20  0.5562 153.90        7
179      20  0.7781 154.90        8
180      20  0.9945 155.40        9
181      21 -1.0000 143.90        1
182      21 -0.7479 145.10        2
183      21 -0.4630 147.00        3
184      21 -0.1643 149.20        4
185      21 -0.0027 149.80        5
186      21  0.2466 151.50        6
187      21  0.5562 153.17        7
188      21  0.7781 156.90        8
189      21  0.9945 159.60        9
190      22 -1.0000 147.40        1
191      22 -0.7479 148.80        2
192      22 -0.4630 150.10        3
193      22 -0.1643 152.50        4
194      22 -0.0027 154.70        5
195      22  0.2466 156.00        6
196      22  0.5562 158.40        7
197      22  0.7781 161.50        8
198      22  0.9945 163.30        9
199      23 -1.0000 144.50        1
200      23 -0.7479 146.00        2
201      23 -0.4630 147.40        3
202      23 -0.1643 149.20        4
203      23 -0.0027 150.80        5
204      23  0.2466 152.50        6
205      23  0.5562 155.00        7
206      23  0.7781 156.80        8
207      23  0.9945 158.80        9
208      24 -1.0000 147.80        1
209      24 -0.7479 148.20        2
210      24 -0.4630 150.20        3
211      24 -0.1643 151.00        4
212      24 -0.0027 152.20        5
213      24  0.2466 153.60        6
214      24  0.5562 155.80        7
215      24  0.7781 159.20        8
216      24  0.9945 161.60        9
217      25 -1.0000 135.50        1
218      25 -0.7479 136.60        2
219      25 -0.4630 137.30        3
220      25 -0.1643 138.20        4
221      25 -0.0027 139.00        5
222      25  0.2466 139.50        6
223      25  0.5562 141.00        7
224      25  0.7808 142.70        8
225      25  0.9945 143.90        9
226      26 -1.0000 132.20        1
227      26 -0.7479 134.30        2
228      26 -0.4630 135.10        3
229      26 -0.1643 136.70        4
230      26 -0.0027 138.40        5
231      26  0.2466 138.90        6
232      26  0.5562 141.80        7
233      26  0.7781 142.60        8
234      26  1.0055 143.10        9

> unique( getGroups( Oxboys ) )
 [1] 1  2  3  4  5  6  7  8  9  10 11 12 13 14 15 16 17 18 19 20
[21] 21 22 23 24 25 26
26 Levels: 10 < 26 < 25 < 9 < 2 < 6 < 7 < 17 < 16 < 15 < ... < 4

> plot( BodyWeight, outer = ~ Diet, aspect = 3 )  # Figure 3.3

> plot( BodyWeight, outer = TRUE, aspect = 3 )

> plot( Soybean, outer = ~ Year * Variety )       # Figure 6.10

> plot( Soybean, outer = ~ Variety * Year )

> gsummary( BodyWeight, invar = TRUE )
   Rat Diet
2    2    1
3    3    1
4    4    1
1    1    1
8    8    1
5    5    1
6    6    1
7    7    1
11  11    2
9    9    2
10  10    2
12  12    2
13  13    3
15  15    3
14  14    3
16  16    3

> plot( PBG, inner = ~ Treatment, scales = list(x = list(log = 2)))

> ergoStool.mat <- asTable( ergoStool )

> ergoStool.mat
   
    T1 T2 T3 T4
  8  7 11  8  7
  5  8 11  8  7
  4  7 11 10  9
  9  9 13 10  8
  6  9 11 11 10
  3  7 14 13  9
  7  8 12 12 11
  1 12 15 12 10
  2 10 14 13 12

> ergoStool.new <- balancedGrouped( effort ~ Type | Subject,
+                                    data = ergoStool.mat )

> ergoStool.new
Grouped Data: effort ~ Type | Subject
   Type Subject effort
1    T1       8      7
2    T2       8     11
3    T3       8      8
4    T4       8      7
5    T1       5      8
6    T2       5     11
7    T3       5      8
8    T4       5      7
9    T1       4      7
10   T2       4     11
11   T3       4     10
12   T4       4      9
13   T1       9      9
14   T2       9     13
15   T3       9     10
16   T4       9      8
17   T1       6      9
18   T2       6     11
19   T3       6     11
20   T4       6     10
21   T1       3      7
22   T2       3     14
23   T3       3     13
24   T4       3      9
25   T1       7      8
26   T2       7     12
27   T3       7     12
28   T4       7     11
29   T1       1     12
30   T2       1     15
31   T3       1     12
32   T4       1     10
33   T1       2     10
34   T2       2     14
35   T3       2     13
36   T4       2     12

> # 3.3 Controlling Trellis Graphics Presentations of Grouped Data
> 
> plot(CO2, layout=c(6,2), between=list(x=c(0,0,0.5,0,0)))

> plot( Spruce, layout = c(7, 4, 3),
+        skip = c(rep(FALSE, 27), TRUE, rep(FALSE, 27), TRUE,
+                 rep(FALSE, 12), rep(TRUE, 2), rep(FALSE,13)) )

> plot( Spruce, layout = c(9, 3, 3),
+        skip = c(rep(FALSE, 66), TRUE, TRUE, rep(FALSE, 13)) )

> unique( getCovariate(DNase) )
[1]  0.048828  0.195312  0.390625  0.781250  1.562500  3.125000
[7]  6.250000 12.500000

> log( unique(getCovariate(DNase)), 2 )
[1] -4.35614 -2.35614 -1.35614 -0.35614  0.64386  1.64386
[7]  2.64386  3.64386

> plot( DNase, layout=c(6,2), scales = list(x=list(log=2)) )

> plot(Pixel, layout = c(4,5),
+      between = list(x = c(0, 0.5, 0), y = 0.5))

> plot( Pixel, displayLevel = 1 )

> plot( Wafer, display = 1, collapse = 1 )

> plot( Wafer, display = 1, collapse = 1,
+        FUN = function(x) sqrt(var(x)), layout = c(10,1) )

> # 3.4 Summaries
> 
> sapply( ergoStool, data.class )
   effort      Type   Subject 
"numeric"  "factor" "ordered" 

> gsummary( Theoph, inv = TRUE )
   Subject   Wt Dose
6        6 80.0 4.00
7        7 64.6 4.95
8        8 70.5 4.53
11      11 65.0 4.92
3        3 70.5 4.53
2        2 72.4 4.40
4        4 72.7 4.40
9        9 86.4 3.10
12      12 60.5 5.30
10      10 58.2 5.50
1        1 79.6 4.02
5        5 54.6 5.86

> gsummary( Theoph, omit = TRUE, inv = TRUE )
     Wt Dose
6  80.0 4.00
7  64.6 4.95
8  70.5 4.53
11 65.0 4.92
3  70.5 4.53
2  72.4 4.40
4  72.7 4.40
9  86.4 3.10
12 60.5 5.30
10 58.2 5.50
1  79.6 4.02
5  54.6 5.86

> is.null(gsummary(Theoph, inv = TRUE, omit = TRUE)) # invariants present
[1] FALSE

> is.null(gsummary(Oxboys, inv = TRUE, omit = TRUE)) # no invariants
[1] TRUE

> gsummary( Theoph )
   Subject   Wt Dose   Time   conc
6        6 80.0 4.00 5.8882 3.5255
7        7 64.6 4.95 5.8655 3.9109
8        8 70.5 4.53 5.8900 4.2718
11      11 65.0 4.92 5.8718 4.5109
3        3 70.5 4.53 5.9073 5.0864
2        2 72.4 4.40 5.8691 4.8236
4        4 72.7 4.40 5.9400 4.9400
9        9 86.4 3.10 5.8682 4.8936
12      12 60.5 5.30 5.8764 5.4100
10      10 58.2 5.50 5.9155 5.9309
1        1 79.6 4.02 5.9500 6.4391
5        5 54.6 5.86 5.8936 5.7827

> gsummary( Theoph, FUN = max, omit = TRUE )
     Wt Dose  Time  conc
6  80.0 4.00 23.85  6.44
7  64.6 4.95 24.22  7.09
8  70.5 4.53 24.12  7.56
11 65.0 4.92 24.08  8.00
3  70.5 4.53 24.17  8.20
2  72.4 4.40 24.30  8.33
4  72.7 4.40 24.65  8.60
9  86.4 3.10 24.43  9.03
12 60.5 5.30 24.15  9.75
10 58.2 5.50 23.70 10.21
1  79.6 4.02 24.37 10.50
5  54.6 5.86 24.35 11.40

> Quin.sum <- gsummary( Quinidine, omit = TRUE, FUN = mean )

> dim( Quin.sum )
[1] 136  13

> Quin.sum[1:10, ]
        time conc dose interval Age Height  Weight      Race
109  30.2633   NA   NA       NA  70     67  58.000 Caucasian
70    0.7500   NA   NA       NA  68     69  75.000 Caucasian
23   52.0262   NA   NA       NA  75     72 108.000 Caucasian
92    8.8571   NA   NA       NA  68     72  65.000 Caucasian
111  18.1638   NA   NA       NA  68     66  56.000     Latin
5    24.3750   NA   NA       NA  62     71  66.000 Caucasian
18  196.8438   NA   NA       NA  87     69  85.375 Caucasian
24   31.2500   NA   NA       NA  55     69  89.000     Latin
2    12.2000   NA   NA       NA  58     69  85.000     Latin
88    4.7900   NA   NA       NA  85     72  77.000 Caucasian
    Smoke Ethanol    Heart Creatinine   glyco
109    no    none  No/Mild      >= 50 0.46000
70     no  former  No/Mild      >= 50 1.15000
23    yes    none  No/Mild      >= 50 0.83875
92    yes  former  No/Mild      >= 50 1.27000
111   yes  former  No/Mild      >= 50 1.23000
5     yes    none   Severe      >= 50 1.39000
18     no    none  No/Mild       < 50 1.26000
24     no  former  No/Mild      >= 50 0.57000
2      no current Moderate      >= 50 0.82000
88     no    none Moderate      >= 50 0.61000

> Quinidine[Quinidine[["Subject"]] == 3, 1:8]
Grouped Data: conc ~ time | Subject
   Subject    time conc dose interval Age Height Weight
17       3    0.00   NA  201       NA  67     69     69
18       3    8.00   NA  201       NA  67     69     69
19       3   16.00   NA  201       NA  67     69     69
20       3   24.00   NA  201       NA  67     69     69
21       3   32.00   NA  201       NA  67     69     69
22       3   41.25  2.4   NA       NA  67     69     69
23       3  104.00   NA  201        8  67     69     69
24       3  113.00  2.3   NA       NA  67     69     69
25       3 3865.00   NA  201        6  67     69     62
26       3 3873.00   NA  201       NA  67     69     62
27       3 3881.00   NA  201       NA  67     69     62
28       3 3889.00   NA  201       NA  67     69     62
29       3 3897.00   NA  201       NA  67     69     62
30       3 3900.00   NA   NA       NA  67     69     62
31       3 3905.00   NA  201       NA  67     69     62
32       3 3909.00  4.7   NA       NA  67     69     62
33       3 4073.00   NA  201        8  67     69     62

> Quin.sum1 <- gsummary( Quinidine, omit = TRUE )

> Quin.sum1[1:10, 1:7]
        time    conc   dose interval Age Height  Weight
109  30.2633 0.50000 100.00      NaN  70     67  58.000
70    0.7500 0.60000 201.00        8  68     69  75.000
23   52.0262 0.56667 166.00        6  75     72 108.000
92    8.8571 0.70000  83.00      NaN  68     72  65.000
111  18.1638 0.90000 249.00      NaN  68     66  56.000
5    24.3750 0.70000 301.00      NaN  62     71  66.000
18  196.8438 0.93333 201.00        6  87     69  85.375
24   31.2500 1.10000 187.88      NaN  55     69  89.000
2    12.2000 1.20000 166.00      NaN  58     69  85.000
88    4.7900 1.20000 201.00        8  85     72  77.000

> summary( Quin.sum1 )
      time             conc           dose        interval    
 Min.   :   0.1   Min.   :0.50   Min.   : 83   Min.   : 5.00  
 1st Qu.:  19.3   1st Qu.:1.70   1st Qu.:198   1st Qu.: 6.00  
 Median :  47.2   Median :2.24   Median :201   Median : 6.00  
 Mean   : 251.5   Mean   :2.36   Mean   :224   Mean   : 6.99  
 3rd Qu.: 171.1   3rd Qu.:2.92   3rd Qu.:249   3rd Qu.: 8.00  
 Max.   :5364.9   Max.   :5.77   Max.   :498   Max.   :12.00  
                                               NA's   :29     
      Age           Height         Weight             Race   
 Min.   :42.0   Min.   :60.0   Min.   : 41.0   Caucasian:91  
 1st Qu.:61.0   1st Qu.:67.0   1st Qu.: 67.8   Latin    :35  
 Median :66.0   Median :70.0   Median : 79.2   Black    :10  
 Mean   :66.9   Mean   :69.6   Mean   : 79.2                 
 3rd Qu.:73.0   3rd Qu.:72.0   3rd Qu.: 88.2                 
 Max.   :92.0   Max.   :79.0   Max.   :119.0                 
                                                             
 Smoke       Ethanol        Heart    Creatinine      glyco      
 no :94   none   :90   No/Mild :55   < 50 : 36   Min.   :0.390  
 yes:42   current:16   Moderate:40   >= 50:100   1st Qu.:0.885  
          former :30   Severe  :41               Median :1.174  
                                                 Mean   :1.212  
                                                 3rd Qu.:1.453  
                                                 Max.   :2.995  
                                                                

> summary( Quinidine )
    Subject          time           conc           dose    
 223    :  47   Min.   :   0   Min.   :0.40   Min.   : 83  
 110    :  41   1st Qu.:  16   1st Qu.:1.60   1st Qu.:166  
 81     :  40   Median :  60   Median :2.30   Median :201  
 136    :  32   Mean   : 373   Mean   :2.45   Mean   :225  
 7      :  31   3rd Qu.: 241   3rd Qu.:3.00   3rd Qu.:249  
 76     :  28   Max.   :8096   Max.   :9.40   Max.   :603  
 (Other):1252                  NA's   :1110   NA's   :443  
    interval          Age           Height         Weight     
 Min.   : 4.00   Min.   :42.0   Min.   :60.0   Min.   : 41.0  
 1st Qu.: 6.00   1st Qu.:60.0   1st Qu.:67.0   1st Qu.: 69.5  
 Median : 6.00   Median :66.0   Median :69.0   Median : 78.0  
 Mean   : 7.11   Mean   :66.7   Mean   :69.2   Mean   : 79.7  
 3rd Qu.: 8.00   3rd Qu.:74.0   3rd Qu.:72.0   3rd Qu.: 89.0  
 Max.   :12.00   Max.   :92.0   Max.   :79.0   Max.   :119.0  
 NA's   :1222                                                 
        Race     Smoke         Ethanol         Heart    
 Caucasian:968   no :1024   none   :991   No/Mild :598  
 Latin    :384   yes: 447   current:191   Moderate:375  
 Black    :119              former :289   Severe  :498  
                                                        
                                                        
                                                        
                                                        
 Creatinine       glyco     
 < 50 : 418   Min.   :0.39  
 >= 50:1053   1st Qu.:0.93  
              Median :1.23  
              Mean   :1.28  
              3rd Qu.:1.54  
              Max.   :3.16  
                            

> sum( ifelse(is.na(Quinidine[["conc"]]), 0, 1) )
[1] 361

> sum( !is.na(Quinidine[["conc"]]) )
[1] 361

> sum( !is.na(Quinidine[["dose"]]) )
[1] 1028

> gapply( Quinidine, "conc", function(x) sum(!is.na(x)) )
109  70  23  92 111   5  18  24   2  88  91 117 120  13  89  27 
  1   1   3   1   1   2   3   1   1   1   1   3   2   1   3   1 
 53 122 129 132  16 106  15  22  57  77 115 121 123  11  48 126 
  1   1   2   3   1   1   1   1   3   1   4   1   1   2   2   2 
223  19  38  42  52  56  63  83 104 118 137  17  29  34  46  73 
  6   1   1   2   1   1   4   1   2   2   1   1   1   1   3   2 
 87 103 138  45  44  97  36  37  72 100   8  71   6  14  26  75 
  2   1   2   3   7   2   2   3   1   3   1   5   1   3   1   3 
 20  96  99 134  12  49  67  85 112 127  55  68 124   1  35  47 
  2   3   2   1   1   3   3   1   3   3   6   3   1   2   2   5 
 79  95 114 135 105 116  62  65 107 130  66 139  33  80 125 110 
  3   3   2   2   1   3   4   7   4   3   1   3   3   2   1  11 
128 136  21  43  90 102  40  84  98  30  82  93 108 119  32 133 
  2  11   2   1   1   2   2   6   2   1   3   4   1   3   1   2 
  7   9  76  94  58 113  50  39  78  25  61   3  64  60  59  10 
  6   2   6   5   1   2   3   2  10   2   2   3   4   4   3   6 
 69   4  81  54  41  74  28  51 
  2   6  11   4   3   3   4   6 

> table( gapply(Quinidine, "conc", function(x) sum(!is.na(x))) )

 1  2  3  4  5  6  7 10 11 
46 33 31  9  3  8  2  1  3 

> changeRecords <- gapply( Quinidine, FUN = function(frm)
+     any(is.na(frm[["conc"]]) & is.na(frm[["dose"]])) )

> changeRecords
  109    70    23    92   111     5    18    24     2    88 
FALSE FALSE FALSE FALSE FALSE FALSE  TRUE FALSE FALSE FALSE 
   91   117   120    13    89    27    53   122   129   132 
FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE  TRUE 
   16   106    15    22    57    77   115   121   123    11 
FALSE FALSE FALSE FALSE FALSE  TRUE FALSE FALSE FALSE FALSE 
   48   126   223    19    38    42    52    56    63    83 
 TRUE FALSE  TRUE FALSE FALSE FALSE FALSE FALSE  TRUE FALSE 
  104   118   137    17    29    34    46    73    87   103 
FALSE  TRUE FALSE FALSE FALSE FALSE  TRUE FALSE FALSE FALSE 
  138    45    44    97    36    37    72   100     8    71 
FALSE  TRUE  TRUE  TRUE FALSE  TRUE FALSE FALSE FALSE  TRUE 
    6    14    26    75    20    96    99   134    12    49 
FALSE  TRUE FALSE  TRUE FALSE  TRUE FALSE FALSE FALSE FALSE 
   67    85   112   127    55    68   124     1    35    47 
FALSE FALSE  TRUE  TRUE  TRUE FALSE FALSE FALSE FALSE  TRUE 
   79    95   114   135   105   116    62    65   107   130 
 TRUE  TRUE  TRUE  TRUE FALSE FALSE  TRUE  TRUE FALSE FALSE 
   66   139    33    80   125   110   128   136    21    43 
FALSE  TRUE  TRUE  TRUE FALSE  TRUE FALSE  TRUE FALSE FALSE 
   90   102    40    84    98    30    82    93   108   119 
FALSE FALSE  TRUE  TRUE  TRUE FALSE  TRUE FALSE FALSE  TRUE 
   32   133     7     9    76    94    58   113    50    39 
FALSE  TRUE  TRUE FALSE  TRUE  TRUE FALSE FALSE  TRUE FALSE 
   78    25    61     3    64    60    59    10    69     4 
FALSE FALSE  TRUE  TRUE  TRUE FALSE FALSE  TRUE FALSE  TRUE 
   81    54    41    74    28    51 
 TRUE  TRUE  TRUE FALSE  TRUE FALSE 

> sort( as.numeric( names(changeRecords)[changeRecords] ) )
 [1]   3   4   7  10  14  18  28  33  37  40  41  44  45  46  47
[16]  48  50  54  55  61  62  63  64  65  71  75  76  77  79  80
[31]  81  82  84  94  95  96  97  98 110 112 114 118 119 127 132
[46] 133 135 136 139 223

> Quinidine[29:31,]
Grouped Data: conc ~ time | Subject
   Subject time conc dose interval Age Height Weight      Race
29       3 3897   NA  201       NA  67     69     62 Caucasian
30       3 3900   NA   NA       NA  67     69     62 Caucasian
31       3 3905   NA  201       NA  67     69     62 Caucasian
   Smoke Ethanol    Heart Creatinine glyco
29   yes  former Moderate       < 50  1.71
30   yes  former Moderate       < 50  1.71
31   yes  former Moderate       < 50  1.71

> Quinidine[Quinidine[["Subject"]] == 4, ]
Grouped Data: conc ~ time | Subject
   Subject   time conc dose interval Age Height Weight  Race
45       4   0.00   NA  332       NA  88     66    103 Black
46       4   7.00   NA  332       NA  88     66    103 Black
47       4  13.00   NA  332       NA  88     66    103 Black
48       4  19.00   NA  332       NA  88     66    103 Black
49       4  21.50  3.1   NA       NA  88     66    103 Black
50       4  85.00   NA  249        6  88     66    103 Black
51       4  91.00  5.8   NA       NA  88     66    103 Black
52       4  91.08   NA  249       NA  88     66    103 Black
53       4  97.00   NA  249       NA  88     66    103 Black
54       4 103.00   NA  249       NA  88     66    103 Black
55       4 105.00   NA   NA       NA  88     66     92 Black
56       4 109.00   NA  249       NA  88     66     92 Black
57       4 115.00   NA  249       NA  88     66     92 Black
58       4 145.00   NA  166       NA  88     66     92 Black
59       4 151.00   NA  166       NA  88     66     92 Black
60       4 156.00  3.1   NA       NA  88     66     92 Black
61       4 157.00   NA  166       NA  88     66     92 Black
62       4 163.00   NA  166       NA  88     66     92 Black
63       4 169.00   NA  166       NA  88     66     92 Black
64       4 174.75   NA  201       NA  88     66     92 Black
65       4 177.00   NA   NA       NA  88     66     92 Black
66       4 181.50  3.1   NA       NA  88     66     92 Black
67       4 245.00   NA  201        8  88     66     92 Black
68       4 249.00   NA   NA       NA  88     66     86 Black
69       4 252.50  3.2   NA       NA  88     66     86 Black
70       4 317.00   NA  201        8  88     66     86 Black
71       4 326.00  1.9   NA       NA  88     66     86 Black
   Smoke Ethanol  Heart Creatinine glyco
45   yes    none Severe      >= 50  1.48
46   yes    none Severe      >= 50  1.48
47   yes    none Severe      >= 50  1.48
48   yes    none Severe      >= 50  1.48
49   yes    none Severe      >= 50  1.48
50   yes    none Severe      >= 50  1.61
51   yes    none Severe      >= 50  1.61
52   yes    none Severe      >= 50  1.61
53   yes    none Severe      >= 50  1.61
54   yes    none Severe      >= 50  1.61
55   yes    none Severe      >= 50  1.61
56   yes    none Severe      >= 50  1.61
57   yes    none Severe      >= 50  1.61
58   yes    none Severe      >= 50  1.88
59   yes    none Severe      >= 50  1.88
60   yes    none Severe      >= 50  1.88
61   yes    none Severe      >= 50  1.88
62   yes    none Severe      >= 50  1.88
63   yes    none Severe      >= 50  1.88
64   yes    none Severe      >= 50  1.88
65   yes    none Severe      >= 50  1.68
66   yes    none Severe      >= 50  1.68
67   yes    none Severe      >= 50  1.87
68   yes    none Severe      >= 50  1.87
69   yes    none Severe      >= 50  1.87
70   yes    none Severe      >= 50  1.83
71   yes    none Severe      >= 50  1.83

> # cleanup
> 
> summary(warnings())
No warnings

======
ch04.R
======

> #-*- R -*-
> 
> # initialization
> 
> library(nlme)

> library(lattice)

> options(width = 65,
+         ## reduce platform dependence in printed output when testing
+         digits = if(nzchar(Sys.getenv("R_TESTS"))) 3 else 5)

> options(contrasts = c(unordered = "contr.helmert", ordered = "contr.poly"))

> pdf(file = 'ch04.pdf')

> # Chapter 4    Fitting Linear Mixed-Effects Models
> 
> # 4.1 Fitting Linear Models in S with lm and lmList
> 
> fm1Orth.lm <- lm(distance ~ age, Orthodont)

> fm1Orth.lm

Call:
lm(formula = distance ~ age, data = Orthodont)

Coefficients:
(Intercept)          age  
      16.76         0.66  


> par(mfrow=c(2,2))

> plot(fm1Orth.lm)                               # Figure 4.1

> fm2Orth.lm <- update(fm1Orth.lm, formula = distance ~ Sex*age)

> summary(fm2Orth.lm)

Call:
lm(formula = distance ~ Sex + age + Sex:age, data = Orthodont)

Residuals:
   Min     1Q Median     3Q    Max 
-5.616 -1.322 -0.168  1.330  5.247 

Coefficients:
            Estimate Std. Error t value Pr(>|t|)    
(Intercept)  16.8567     1.1094   15.19  < 2e-16 ***
Sex1          0.5161     1.1094    0.47     0.64    
age           0.6320     0.0988    6.39  4.7e-09 ***
Sex1:age     -0.1524     0.0988   -1.54     0.13    
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Residual standard error: 2.26 on 104 degrees of freedom
Multiple R-squared:  0.423,	Adjusted R-squared:  0.406 
F-statistic: 25.4 on 3 and 104 DF,  p-value: 2.11e-12


> fm3Orth.lm <- update(fm2Orth.lm, formula = . ~ . - Sex)

> summary(fm3Orth.lm)

Call:
lm(formula = distance ~ age + Sex:age, data = Orthodont)

Residuals:
   Min     1Q Median     3Q    Max 
-5.742 -1.242 -0.189  1.268  5.267 

Coefficients:
            Estimate Std. Error t value Pr(>|t|)    
(Intercept)  16.7611     1.0861   15.43  < 2e-16 ***
age           0.6403     0.0968    6.61  1.6e-09 ***
age:Sex1     -0.1074     0.0196   -5.47  3.0e-07 ***
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Residual standard error: 2.25 on 105 degrees of freedom
Multiple R-squared:  0.422,	Adjusted R-squared:  0.411 
F-statistic: 38.3 on 2 and 105 DF,  p-value: 3.31e-13


> bwplot(getGroups(Orthodont)~resid(fm2Orth.lm)) # Figure 4.2

> fm1Orth.lis <- lmList(distance ~ age | Subject, Orthodont)

> getGroupsFormula(Orthodont)
~Subject

> fm1Orth.lis <- lmList(distance ~ age, Orthodont)

> formula(Orthodont)
distance ~ age | Subject

> fm1Orth.lis <- lmList(Orthodont)

> fm1Orth.lis
Call:
  Model: distance ~ age | Subject 
   Data: Orthodont 

Coefficients:
    (Intercept)   age
M16        17.0 0.550
M05        13.7 0.850
M02        14.9 0.775
M11        20.1 0.325
M07        15.0 0.800
M08        19.8 0.375
M03        16.0 0.750
M12        13.2 1.000
M13         2.8 1.950
M14        19.1 0.525
M09        14.4 0.975
M15        13.5 1.125
M06        19.0 0.675
M04        24.7 0.175
M01        17.3 0.950
M10        21.2 0.750
F10        13.5 0.450
F09        18.1 0.275
F06        17.0 0.375
F01        17.2 0.375
F05        19.6 0.275
F07        16.9 0.550
F02        14.2 0.800
F08        21.4 0.175
F03        14.4 0.850
F04        19.7 0.475
F11        19.0 0.675

Degrees of freedom: 108 total; 54 residual
Residual standard error: 1.31

> summary(fm1Orth.lis)
Call:
  Model: distance ~ age | Subject 
   Data: Orthodont 

Coefficients:
   (Intercept) 
    Estimate Std. Error t value Pr(>|t|)
M16     17.0       3.29   5.155 3.70e-06
M05     13.7       3.29   4.151 1.18e-04
M02     14.9       3.29   4.516 3.46e-05
M11     20.1       3.29   6.098 1.19e-07
M07     15.0       3.29   4.547 3.12e-05
M08     19.8       3.29   6.006 1.67e-07
M03     16.0       3.29   4.866 1.03e-05
M12     13.2       3.29   4.030 1.76e-04
M13      2.8       3.29   0.852 3.98e-01
M14     19.1       3.29   5.809 3.45e-07
M09     14.4       3.29   4.379 5.51e-05
M15     13.5       3.29   4.106 1.37e-04
M06     19.0       3.29   5.763 4.08e-07
M04     24.7       3.29   7.512 6.08e-10
M01     17.3       3.29   5.261 2.52e-06
M10     21.2       3.29   6.463 3.07e-08
F10     13.5       3.29   4.121 1.31e-04
F09     18.1       3.29   5.505 1.05e-06
F06     17.0       3.29   5.170 3.50e-06
F01     17.2       3.29   5.246 2.67e-06
F05     19.6       3.29   5.961 1.97e-07
F07     16.9       3.29   5.155 3.70e-06
F02     14.2       3.29   4.319 6.76e-05
F08     21.4       3.29   6.523 2.44e-08
F03     14.4       3.29   4.379 5.51e-05
F04     19.7       3.29   5.976 1.86e-07
F11     19.0       3.29   5.763 4.08e-07
   age 
    Estimate Std. Error t value Pr(>|t|)
M16    0.550      0.293   1.878 6.58e-02
M05    0.850      0.293   2.902 5.36e-03
M02    0.775      0.293   2.646 1.07e-02
M11    0.325      0.293   1.109 2.72e-01
M07    0.800      0.293   2.731 8.51e-03
M08    0.375      0.293   1.280 2.06e-01
M03    0.750      0.293   2.560 1.33e-02
M12    1.000      0.293   3.414 1.22e-03
M13    1.950      0.293   6.657 1.49e-08
M14    0.525      0.293   1.792 7.87e-02
M09    0.975      0.293   3.328 1.58e-03
M15    1.125      0.293   3.840 3.25e-04
M06    0.675      0.293   2.304 2.51e-02
M04    0.175      0.293   0.597 5.53e-01
M01    0.950      0.293   3.243 2.03e-03
M10    0.750      0.293   2.560 1.33e-02
F10    0.450      0.293   1.536 1.30e-01
F09    0.275      0.293   0.939 3.52e-01
F06    0.375      0.293   1.280 2.06e-01
F01    0.375      0.293   1.280 2.06e-01
F05    0.275      0.293   0.939 3.52e-01
F07    0.550      0.293   1.878 6.58e-02
F02    0.800      0.293   2.731 8.51e-03
F08    0.175      0.293   0.597 5.53e-01
F03    0.850      0.293   2.902 5.36e-03
F04    0.475      0.293   1.622 1.11e-01
F11    0.675      0.293   2.304 2.51e-02

Residual standard error: 1.31 on 54 degrees of freedom


> pairs(fm1Orth.lis, id = 0.01, adj = -0.5)      # Figure 4.3

> fm2Orth.lis <- update(fm1Orth.lis, distance ~ I(age-11))

> intervals(fm2Orth.lis)
, , (Intercept)

    lower est. upper
M16  21.7 23.0  24.3
M05  21.7 23.0  24.3
M02  22.1 23.4  24.7
M11  22.3 23.6  24.9
M07  22.4 23.8  25.1
M08  22.6 23.9  25.2
M03  22.9 24.2  25.6
M12  22.9 24.2  25.6
M13  22.9 24.2  25.6
M14  23.6 24.9  26.2
M09  23.8 25.1  26.4
M15  24.6 25.9  27.2
M06  25.1 26.4  27.7
M04  25.3 26.6  27.9
M01  26.4 27.8  29.1
M10  28.2 29.5  30.8
F10  17.2 18.5  19.8
F09  19.8 21.1  22.4
F06  19.8 21.1  22.4
F01  20.1 21.4  22.7
F05  21.3 22.6  23.9
F07  21.7 23.0  24.3
F02  21.7 23.0  24.3
F08  22.1 23.4  24.7
F03  22.4 23.8  25.1
F04  23.6 24.9  26.2
F11  25.1 26.4  27.7

, , I(age - 11)

      lower  est. upper
M16 -0.0373 0.550 1.137
M05  0.2627 0.850 1.437
M02  0.1877 0.775 1.362
M11 -0.2623 0.325 0.912
M07  0.2127 0.800 1.387
M08 -0.2123 0.375 0.962
M03  0.1627 0.750 1.337
M12  0.4127 1.000 1.587
M13  1.3627 1.950 2.537
M14 -0.0623 0.525 1.112
M09  0.3877 0.975 1.562
M15  0.5377 1.125 1.712
M06  0.0877 0.675 1.262
M04 -0.4123 0.175 0.762
M01  0.3627 0.950 1.537
M10  0.1627 0.750 1.337
F10 -0.1373 0.450 1.037
F09 -0.3123 0.275 0.862
F06 -0.2123 0.375 0.962
F01 -0.2123 0.375 0.962
F05 -0.3123 0.275 0.862
F07 -0.0373 0.550 1.137
F02  0.2127 0.800 1.387
F08 -0.4123 0.175 0.762
F03  0.2627 0.850 1.437
F04 -0.1123 0.475 1.062
F11  0.0877 0.675 1.262


> plot(intervals(fm2Orth.lis))                   # Figure 4.5

> IGF
Grouped Data: conc ~ age | Lot
    Lot age conc
1     1   7 4.90
2     1   7 5.68
3     1   8 5.32
4     1   8 5.50
5     1  13 4.94
6     1  13 5.19
7     1  14 5.18
8     1  14 5.67
9     1  15 5.02
10    1  15 5.88
11    1  22 5.12
12    1  23 5.24
13    1  24 5.88
14    1  27 5.40
15    1  28 5.59
16    1  28 5.77
17    1  30 5.57
18    1  34 5.86
19    1  34 5.87
20    1  35 4.65
21    1  35 5.34
22    1  36 4.93
23    1  36 5.33
24    1  36 4.99
25    1  41 3.38
26    1  42 5.44
27    1  42 5.24
28    1  43 5.39
29    2   3 5.34
30    2   3 5.27
31    2   3 5.48
32    2   6 5.15
33    2  11 4.23
34    2  11 5.77
35    2  11 5.06
36    2  12 5.33
37    2  12 5.78
38    2  13 5.01
39    2  13 4.85
40    2  13 4.94
41    2  18 5.14
42    2  24 5.43
43    2  24 5.66
44    2  25 5.62
45    2  25 5.53
46    2  26 6.20
47    2  27 5.30
48    2  27 4.09
49    2  32 5.78
50    2  32 5.66
51    2  34 5.07
52    2  38 5.45
53    2  40 4.76
54    2  42 4.81
55    2  45 4.92
56    2  46 4.32
57    2  47 3.30
58    3   1 5.88
59    3   2 5.91
60    3   5 0.86
61    3   6 5.40
62    3   7 4.94
63    3   8 5.42
64    3  13 5.40
65    3  15 5.68
66    3  15 5.71
67    3  21 9.55
68    3  21 5.94
69    3  21 6.17
70    3  22 5.34
71    3  22 8.14
72    3  27 5.51
73    3  28 5.31
74    3  28 4.81
75    3  28 5.26
76    3  29 4.72
77    3  30 5.08
78    3  30 3.99
79    3  33 4.87
80    3  34 4.92
81    3  34 6.13
82    3  35 6.30
83    3  36 5.97
84    3  37 5.98
85    3  41 6.68
86    3  42 5.33
87    3  43 6.08
88    3  44 4.76
89    3  47 5.31
90    3  47 6.66
91    3  48 5.52
92    3  49 5.48
93    3  50 5.10
94    4   5 5.12
95    4   5 5.08
96    4   5 4.63
97    4   5 5.38
98    4   7 5.78
99    4   9 9.34
100   4  11 5.58
101   4  11 5.19
102   4  12 5.25
103   4  12 5.44
104   4  14 5.31
105   4  14 4.71
106   4  14 5.67
107   4  14 4.65
108   4  14 5.05
109   4  15 4.23
110   4  19 5.02
111   4  19 4.98
112   4  20 5.08
113   4  20 4.84
114   4  22 4.84
115   4  22 5.53
116   4  25 5.85
117   4  25 5.32
118   4  26 5.47
119   5   1 5.49
120   5   2 5.43
121   5   6 5.02
122   5   6 5.29
123   5   7 6.25
124   5   9 4.63
125   5  10 5.18
126   5  15 5.17
127   5  15 4.98
128   5  15 5.38
129   5  15 3.76
130   5  17 5.63
131   5  21 6.12
132   5  22 4.00
133   5  23 6.53
134   5  24 4.67
135   5  24 5.55
136   5  24 5.62
137   5  29 4.58
138   5  30 5.41
139   5  35 4.84
140   5  37 4.83
141   5  37 5.36
142   5  37 4.81
143   5  37 5.35
144   5  42 5.46
145   5  43 5.09
146   5  44 4.78
147   5  44 4.44
148   5  45 4.67
149   5  48 4.98
150   6   2 4.56
151   6   3 5.83
152   6   3 5.27
153   6   4 4.90
154   7   1 4.94
155   7   2 4.78
156   7   3 5.42
157   7   4 5.42
158   7   5 5.38
159   7   7 5.55
160   7  10 5.81
161   7  10 5.62
162   7  11 6.08
163   7  15 4.80
164   7  16 5.32
165   7  17 4.95
166   7  17 5.44
167   7  18 5.48
168   7  21 5.26
169   7  22 5.21
170   7  23 4.65
171   7  24 4.62
172   7  24 5.15
173   7  26 4.71
174   7  27 5.02
175   7  29 5.38
176   7  31 5.34
177   7  31 5.10
178   7  32 5.69
179   7  36 5.00
180   7  37 5.02
181   7  38 9.74
182   7  38 9.60
183   7  39 5.58
184   7  42 4.94
185   7  43 4.66
186   7  43 5.23
187   7  45 5.62
188   7  45 5.53
189   7  45 5.45
190   7  45 4.63
191   7  47 5.01
192   7  50 5.43
193   8   1 6.17
194   8   1 5.57
195   8   2 4.82
196   8   3 5.84
197   8   6 5.55
198   8   9 5.17
199   8   9 6.50
200   8   9 5.36
201   9   4 5.47
202   9   4 5.57
203   9   5 5.36
204   9   7 4.93
205   9   8 5.49
206   9  11 3.25
207   9  13 5.53
208   9  13 4.91
209   9  13 5.74
210   9  14 4.95
211   9  15 5.07
212   9  19 5.54
213   9  20 5.29
214   9  21 4.59
215   9  25 5.66
216   9  26 4.69
217   9  26 5.18
218   9  27 5.19
219   9  27 5.35
220   9  29 5.28
221   9  29 5.50
222   9  29 5.00
223   9  30 5.47
224   9  33 5.55
225   9  34 5.75
226   9  35 5.41
227   9  35 5.65
228   9  35 5.25
229   9  36 5.81
230   9  40 4.71
231   9  41 4.95
232  10   4 6.00
233  10   5 5.74
234  10   6 5.68
235  10   6 5.83
236  10  11 5.30
237  10  13 5.63

> plot(IGF)                                      # Figure 4.6

> fm1IGF.lis <- lmList(IGF)

> coef(fm1IGF.lis)
   (Intercept)      age
9         5.10  0.00573
6         4.63  0.17000
1         5.49 -0.00779
10        6.05 -0.04733
2         5.48 -0.01443
8         5.59  0.00606
5         5.37 -0.00951
4         5.58 -0.01666
3         5.28  0.01008
7         5.21  0.00931

> plot(intervals(fm1IGF.lis))                    # Figure 4.7

> fm1IGF.lm <- lm(conc ~ age, data = IGF)

> summary(fm1IGF.lm)

Call:
lm(formula = conc ~ age, data = IGF)

Residuals:
   Min     1Q Median     3Q    Max 
-4.488 -0.374 -0.009  0.258  4.414 

Coefficients:
             Estimate Std. Error t value Pr(>|t|)    
(Intercept)  5.351059   0.103734   51.58   <2e-16 ***
age         -0.000669   0.003943   -0.17     0.87    
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Residual standard error: 0.833 on 235 degrees of freedom
Multiple R-squared:  0.000123,	Adjusted R-squared:  -0.00413 
F-statistic: 0.0288 on 1 and 235 DF,  p-value: 0.865


> # 4.2 Fitting Linear Mixed-Effects Models with lme
> 
> fm1Orth.lme <- lme(distance ~ I(age-11), data = Orthodont,
+                      random = ~ I(age-11) | Subject)

> fm1Orth.lme <- lme(distance ~ I(age-11), data = Orthodont)

> fm1Orth.lme <- lme(fm2Orth.lis)

> fm1Orth.lme
Linear mixed-effects model fit by REML
  Data: Orthodont 
  Log-restricted-likelihood: -221
  Fixed: distance ~ I(age - 11) 
(Intercept) I(age - 11) 
      24.02        0.66 

Random effects:
 Formula: ~I(age - 11) | Subject
 Structure: General positive-definite, Log-Cholesky parametrization
            StdDev Corr  
(Intercept) 2.134  (Intr)
I(age - 11) 0.226  0.503 
Residual    1.310        

Number of Observations: 108
Number of Groups: 27 

> fm2Orth.lme <- update(fm1Orth.lme, distance~Sex*I(age-11))

> summary(fm2Orth.lme)
Linear mixed-effects model fit by REML
  Data: Orthodont 
  AIC BIC logLik
  451 473   -218

Random effects:
 Formula: ~I(age - 11) | Subject
 Structure: General positive-definite, Log-Cholesky parametrization
            StdDev Corr  
(Intercept) 1.83   (Intr)
I(age - 11) 0.18   0.206 
Residual    1.31         

Fixed effects:  distance ~ Sex + I(age - 11) + Sex:I(age - 11) 
                 Value Std.Error DF t-value p-value
(Intercept)      23.81     0.381 79    62.5  0.0000
Sex1             -1.16     0.381 25    -3.0  0.0054
I(age - 11)       0.63     0.067 79     9.4  0.0000
Sex1:I(age - 11) -0.15     0.067 79    -2.3  0.0264
 Correlation: 
                 (Intr) Sex1  I(-11)
Sex1             0.185              
I(age - 11)      0.102  0.019       
Sex1:I(age - 11) 0.019  0.102 0.185 

Standardized Within-Group Residuals:
    Min      Q1     Med      Q3     Max 
-3.1681 -0.3859  0.0071  0.4452  3.8495 

Number of Observations: 108
Number of Groups: 27 

> fitted(fm2Orth.lme, level = 0:1)
    fixed Subject
1    22.6    24.8
2    24.2    26.6
3    25.8    28.3
4    27.3    30.0
5    22.6    21.3
6    24.2    22.8
7    25.8    24.3
8    27.3    25.8
9    22.6    22.0
10   24.2    23.6
11   25.8    25.1
12   27.3    26.6
13   22.6    24.5
14   24.2    25.8
15   25.8    27.0
16   27.3    28.3
17   22.6    20.9
18   24.2    22.5
19   25.8    24.0
20   27.3    25.6
21   22.6    23.9
22   24.2    25.4
23   25.8    27.0
24   27.3    28.5
25   22.6    21.6
26   24.2    23.1
27   25.8    24.7
28   27.3    26.2
29   22.6    22.0
30   24.2    23.3
31   25.8    24.6
32   27.3    26.0
33   22.6    22.6
34   24.2    24.3
35   25.8    26.0
36   27.3    27.6
37   22.6    26.5
38   24.2    28.1
39   25.8    29.8
40   27.3    31.5
41   22.6    21.8
42   24.2    23.1
43   25.8    24.4
44   27.3    25.7
45   22.6    21.8
46   24.2    23.5
47   25.8    25.2
48   27.3    26.8
49   22.6    21.2
50   24.2    23.3
51   25.8    25.5
52   27.3    27.7
53   22.6    22.7
54   24.2    24.2
55   25.8    25.6
56   27.3    27.0
57   22.6    23.1
58   24.2    24.9
59   25.8    26.7
60   27.3    28.5
61   22.6    21.1
62   24.2    22.5
63   25.8    23.9
64   27.3    25.3
65   21.2    20.2
66   22.2    21.1
67   23.1    21.9
68   24.1    22.8
69   21.2    21.3
70   22.2    22.4
71   23.1    23.6
72   24.1    24.7
73   21.2    21.9
74   22.2    23.1
75   23.1    24.2
76   24.1    25.4
77   21.2    23.1
78   22.2    24.1
79   23.1    25.1
80   24.1    26.1
81   21.2    21.3
82   22.2    22.2
83   23.1    23.0
84   24.1    23.9
85   21.2    20.0
86   22.2    20.9
87   23.1    21.7
88   24.1    22.6
89   21.2    21.5
90   22.2    22.5
91   23.1    23.5
92   24.1    24.5
93   21.2    22.0
94   22.2    22.9
95   23.1    23.7
96   24.1    24.5
97   21.2    20.1
98   22.2    20.9
99   23.1    21.7
100  24.1    22.5
101  21.2    17.7
102  22.2    18.6
103  23.1    19.4
104  24.1    20.2
105  21.2    24.2
106  22.2    25.4
107  23.1    26.5
108  24.1    27.7

> resid(fm2Orth.lme, level = 1)
     M01      M01      M01      M01      M02      M02      M02 
 1.15428 -1.57649  0.69275  0.96198  0.22522 -0.29641 -1.31803 
     M02      M03      M03      M03      M03      M04      M04 
 0.66034  0.96689 -1.06449 -1.09588  0.87274  1.03549  1.74867 
     M04      M04      M05      M05      M05      M05      M06 
-0.53814 -1.32495 -0.90249  1.04571 -1.50610  0.44210  0.61473 
     M06      M06      M06      M07      M07      M07      M07 
 0.06728  0.01983 -0.02762  0.42649 -1.11840 -0.16330  0.29181 
     M08      M08      M08      M08      M09      M09      M09 
 2.00813 -1.81291 -0.13395 -0.45500  0.39248 -3.78229  5.04295 
     M09      M10      M10      M10      M10      M11      M11 
-1.63182  1.02728 -0.14284  1.18705  0.01693  1.18276 -0.10495 
     M11      M11      M12      M12      M12      M12      M13 
-0.89267 -0.68038 -0.34919 -0.01420 -1.17920  1.15579 -4.15031 
     M13      M13      M13      M14      M14      M14      M14 
 1.17692  0.50416  1.83139 -0.22716  1.34520 -0.08244 -1.01008 
     M15      M15      M15      M15      M16      M16      M16 
-0.13140 -0.40616 -0.68091  1.54433  0.87681 -1.01465 -0.40610 
     M16      F01      F01      F01      F01      F02      F02 
-0.29756  0.79027 -1.07931 -0.44889  0.18152 -0.27124 -0.91092 
     F02      F02      F03      F03      F03      F03      F04 
 0.44940  0.80973 -1.36869  0.94509  0.25887  0.57265  0.40409 
     F04      F04      F04      F05      F05      F05      F05 
 0.38858 -0.12694  0.35754  0.15965  0.81049 -0.53868 -0.38784 
     F06      F06      F06      F06      F07      F07      F07 
 0.00168  0.13870 -0.72427 -0.08725  0.04484  0.03879 -0.46727 
     F07      F08      F08      F08      F08      F09      F09 
 0.52667  0.95185  0.13632 -0.17921 -0.49475 -0.07189  0.11859 
     F09      F09      F10      F10      F10      F10      F11 
 0.30906 -1.00047 -1.22334  0.44296 -0.39073 -0.72443  0.28277 
     F11      F11      F11 
-0.37929  1.45866  0.29661 
attr(,"label")
[1] "Residuals (mm)"

> resid(fm2Orth.lme, level = 1, type = "pearson")
     M01      M01      M01      M01      M02      M02      M02 
 0.88111 -1.20339  0.52880  0.73431  0.17192 -0.22626 -1.00610 
     M02      M03      M03      M03      M03      M04      M04 
 0.50406  0.73806 -0.81257 -0.83652  0.66619  0.79042  1.33482 
     M04      M04      M05      M05      M05      M05      M06 
-0.41078 -1.01139 -0.68890  0.79822 -1.14966  0.33747  0.46925 
     M06      M06      M06      M07      M07      M07      M07 
 0.05136  0.01514 -0.02108  0.32556 -0.85372 -0.12465  0.22275 
     M08      M08      M08      M08      M09      M09      M09 
 1.53288 -1.38386 -0.10225 -0.34732  0.29959 -2.88715  3.84946 
     M09      M10      M10      M10      M10      M11      M11 
-1.24562  0.78416 -0.10903  0.90612  0.01293  0.90284 -0.08012 
     M11      M11      M12      M12      M12      M12      M13 
-0.68140 -0.51936 -0.26655 -0.01084 -0.90013  0.88226 -3.16808 
     M13      M13      M13      M14      M14      M14      M14 
 0.89839  0.38484  1.39796 -0.17340  1.02684 -0.06293 -0.77103 
     M15      M15      M15      M15      M16      M16      M16 
-0.10030 -0.31003 -0.51977  1.17884  0.66930 -0.77452 -0.30999 
     M16      F01      F01      F01      F01      F02      F02 
-0.22714  0.60324 -0.82388 -0.34266  0.13856 -0.20705 -0.69534 
     F02      F02      F03      F03      F03      F03      F04 
 0.34305  0.61809 -1.04477  0.72142  0.19761  0.43712  0.30846 
     F04      F04      F04      F05      F05      F05      F05 
 0.29661 -0.09690  0.27293  0.12187  0.61867 -0.41119 -0.29605 
     F06      F06      F06      F06      F07      F07      F07 
 0.00128  0.10588 -0.55286 -0.06660  0.03423  0.02961 -0.35668 
     F07      F08      F08      F08      F08      F09      F09 
 0.40203  0.72658  0.10406 -0.13680 -0.37766 -0.05488  0.09052 
     F09      F09      F10      F10      F10      F10      F11 
 0.23592 -0.76369 -0.93382  0.33813 -0.29826 -0.55298  0.21585 
     F11      F11      F11 
-0.28952  1.11345  0.22641 
attr(,"label")
[1] "Standardized residuals"

> newOrth <- data.frame(Subject = rep(c("M11","F03"), c(3, 3)),
+                       Sex = rep(c("Male", "Female"), c(3, 3)),
+                       age = rep(16:18, 2))

> predict(fm2Orth.lme, newdata = newOrth)
 M11  M11  M11  F03  F03  F03 
27.0 27.6 28.3 26.6 27.2 27.8 
attr(,"label")
[1] "Predicted values (mm)"

> predict(fm2Orth.lme, newdata = newOrth, level = 0:1)
  Subject predict.fixed predict.Subject
1     M11          28.9            27.0
2     M11          29.7            27.6
3     M11          30.5            28.3
4     F03          25.0            26.6
5     F03          25.5            27.2
6     F03          26.0            27.8

> fm2Orth.lmeM <- update(fm2Orth.lme, method = "ML")

> summary(fm2Orth.lmeM)
Linear mixed-effects model fit by maximum likelihood
  Data: Orthodont 
  AIC BIC logLik
  444 465   -214

Random effects:
 Formula: ~I(age - 11) | Subject
 Structure: General positive-definite, Log-Cholesky parametrization
            StdDev Corr  
(Intercept) 1.752  (Intr)
I(age - 11) 0.154  0.234 
Residual    1.310        

Fixed effects:  distance ~ Sex + I(age - 11) + Sex:I(age - 11) 
                 Value Std.Error DF t-value p-value
(Intercept)      23.81     0.373 79    63.8  0.0000
Sex1             -1.16     0.373 25    -3.1  0.0046
I(age - 11)       0.63     0.066 79     9.6  0.0000
Sex1:I(age - 11) -0.15     0.066 79    -2.3  0.0237
 Correlation: 
                 (Intr) Sex1  I(-11)
Sex1             0.185              
I(age - 11)      0.102  0.019       
Sex1:I(age - 11) 0.019  0.102 0.185 

Standardized Within-Group Residuals:
    Min      Q1     Med      Q3     Max 
-3.3360 -0.4154  0.0104  0.4917  3.8582 

Number of Observations: 108
Number of Groups: 27 

> compOrth <-
+       compareFits(coef(fm2Orth.lis), coef(fm1Orth.lme))

> compOrth
, , (Intercept)

    coef(fm2Orth.lis) coef(fm1Orth.lme)
M16              23.0              23.1
M05              23.0              23.1
M02              23.4              23.5
M11              23.6              23.6
M07              23.8              23.8
M08              23.9              23.8
M03              24.2              24.2
M12              24.2              24.3
M13              24.2              24.4
M14              24.9              24.8
M09              25.1              25.1
M15              25.9              25.8
M06              26.4              26.2
M04              26.6              26.3
M01              27.8              27.4
M10              29.5              29.0
F10              18.5              19.0
F09              21.1              21.3
F06              21.1              21.4
F01              21.4              21.6
F05              22.6              22.7
F07              23.0              23.1
F02              23.0              23.1
F08              23.4              23.4
F03              23.8              23.8
F04              24.9              24.8
F11              26.4              26.2

, , I(age - 11)

    coef(fm2Orth.lis) coef(fm1Orth.lme)
M16             0.550             0.591
M05             0.850             0.686
M02             0.775             0.675
M11             0.325             0.541
M07             0.800             0.695
M08             0.375             0.565
M03             0.750             0.696
M12             1.000             0.775
M13             1.950             1.074
M14             0.525             0.646
M09             0.975             0.796
M15             1.125             0.868
M06             0.675             0.743
M04             0.175             0.594
M01             0.950             0.876
M10             0.750             0.871
F10             0.450             0.410
F09             0.275             0.442
F06             0.375             0.474
F01             0.375             0.482
F05             0.275             0.492
F07             0.550             0.591
F02             0.800             0.670
F08             0.175             0.486
F03             0.850             0.711
F04             0.475             0.630
F11             0.675             0.743


> plot(compOrth, mark = fixef(fm1Orth.lme)) # Figure 4.8

> ## Figure 4.9
> plot(comparePred(fm2Orth.lis, fm1Orth.lme, length.out = 2),
+      layout = c(8,4), between = list(y = c(0, 0.5, 0)))

> plot(compareFits(ranef(fm2Orth.lme), ranef(fm2Orth.lmeM)),
+      mark = c(0, 0))

> fm4Orth.lm <- lm(distance ~ Sex * I(age-11), Orthodont)

> summary(fm4Orth.lm)

Call:
lm(formula = distance ~ Sex * I(age - 11), data = Orthodont)

Residuals:
   Min     1Q Median     3Q    Max 
-5.616 -1.322 -0.168  1.330  5.247 

Coefficients:
                 Estimate Std. Error t value Pr(>|t|)    
(Intercept)       23.8082     0.2210  107.73  < 2e-16 ***
Sex1              -1.1605     0.2210   -5.25  8.1e-07 ***
I(age - 11)        0.6320     0.0988    6.39  4.7e-09 ***
Sex1:I(age - 11)  -0.1524     0.0988   -1.54     0.13    
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Residual standard error: 2.26 on 104 degrees of freedom
Multiple R-squared:  0.423,	Adjusted R-squared:  0.406 
F-statistic: 25.4 on 3 and 104 DF,  p-value: 2.11e-12


> anova(fm2Orth.lme, fm4Orth.lm)
            Model df AIC BIC logLik   Test L.Ratio p-value
fm2Orth.lme     1  8 451 473   -218                       
fm4Orth.lm      2  5 496 510   -243 1 vs 2      51  <.0001

> #fm1IGF.lme <- lme(fm1IGF.lis)
> #fm1IGF.lme
> #intervals(fm1IGF.lme)
> #summary(fm1IGF.lme)
> pd1 <- pdDiag(~ age)

> pd1
Uninitialized positive definite matrix structure of class pdDiag.

> formula(pd1)
~age

> #fm2IGF.lme <- update(fm1IGF.lme, random = pdDiag(~age))
> (fm2IGF.lme <- lme(conc ~ age, IGF,
+                    random = pdDiag(~age)))
Linear mixed-effects model fit by REML
  Data: IGF 
  Log-restricted-likelihood: -297
  Fixed: conc ~ age 
(Intercept)         age 
    5.36904    -0.00193 

Random effects:
 Formula: ~age | Lot
 Structure: Diagonal
        (Intercept)     age Residual
StdDev:    3.62e-05 0.00537    0.822

Number of Observations: 237
Number of Groups: 10 

> #anova(fm1IGF.lme, fm2IGF.lme)
> anova(fm2IGF.lme)
            numDF denDF F-value p-value
(Intercept)     1   226    6439  <.0001
age             1   226       0   0.673

> #update(fm1IGF.lme, random = list(Lot = pdDiag(~ age)))
> pd2 <- pdDiag(value = diag(2), form = ~ age)

> pd2
Positive definite matrix structure of class pdDiag representing
     [,1] [,2]
[1,]    1    0
[2,]    0    1

> formula(pd2)
~age

> lme(conc ~ age, IGF, pdDiag(diag(2), ~age))
Linear mixed-effects model fit by REML
  Data: IGF 
  Log-restricted-likelihood: -297
  Fixed: conc ~ age 
(Intercept)         age 
    5.36904    -0.00193 

Random effects:
 Formula: ~age | Lot
 Structure: Diagonal
        (Intercept)     age Residual
StdDev:    3.12e-05 0.00537    0.822

Number of Observations: 237
Number of Groups: 10 

> fm4OatsB <- lme(yield ~ nitro, data = Oats,
+                  random =list(Block = pdCompSymm(~ Variety - 1)))

> summary(fm4OatsB)
Linear mixed-effects model fit by REML
  Data: Oats 
  AIC BIC logLik
  603 614   -297

Random effects:
 Formula: ~Variety - 1 | Block
 Structure: Compound Symmetry
                   StdDev Corr       
VarietyGolden Rain 18.2              
VarietyMarvellous  18.2   0.635      
VarietyVictory     18.2   0.635 0.635
Residual           12.9              

Fixed effects:  yield ~ nitro 
            Value Std.Error DF t-value p-value
(Intercept)  81.9      6.95 65    11.8       0
nitro        73.7      6.78 65    10.9       0
 Correlation: 
      (Intr)
nitro -0.293

Standardized Within-Group Residuals:
    Min      Q1     Med      Q3     Max 
-1.7438 -0.6648  0.0171  0.5430  1.8030 

Number of Observations: 72
Number of Groups: 6 

> corMatrix(fm4OatsB$modelStruct$reStruct$Block)[1,2]
[1] 0.635

> fm4OatsC <- lme(yield ~ nitro, data = Oats,
+         random=list(Block=pdBlocked(list(pdIdent(~ 1),
+                                          pdIdent(~ Variety-1)))))

> summary(fm4OatsC)
Linear mixed-effects model fit by REML
  Data: Oats 
  AIC BIC logLik
  603 614   -297

Random effects:
 Composite Structure: Blocked

 Block 1: (Intercept)
 Formula: ~1 | Block
        (Intercept)
StdDev:        14.5

 Block 2: VarietyGolden Rain, VarietyMarvellous, VarietyVictory
 Formula: ~Variety - 1 | Block
 Structure: Multiple of an Identity
        VarietyGolden Rain VarietyMarvellous VarietyVictory
StdDev:                 11                11             11
        Residual
StdDev:     12.9

Fixed effects:  yield ~ nitro 
            Value Std.Error DF t-value p-value
(Intercept)  81.9      6.95 65    11.8       0
nitro        73.7      6.78 65    10.9       0
 Correlation: 
      (Intr)
nitro -0.293

Standardized Within-Group Residuals:
    Min      Q1     Med      Q3     Max 
-1.7438 -0.6648  0.0171  0.5430  1.8030 

Number of Observations: 72
Number of Groups: 6 

> ## establishing the desired parameterization for contrasts
> options(contrasts = c("contr.treatment", "contr.poly"))

> fm1Assay <- lme(logDens ~ sample * dilut, Assay,
+                 random = pdBlocked(list(pdIdent(~ 1), pdIdent(~ sample - 1),
+                 pdIdent(~ dilut - 1))))

> fm1Assay
Linear mixed-effects model fit by REML
  Data: Assay 
  Log-restricted-likelihood: 38.5
  Fixed: logDens ~ sample * dilut 
   (Intercept)        sampleb        samplec        sampled 
      -0.18279        0.08075        0.13398        0.20770 
       samplee        samplef         dilut2         dilut3 
      -0.02367        0.07357        0.20443        0.40586 
        dilut4         dilut5 sampleb:dilut2 samplec:dilut2 
       0.57319        0.72064        0.00894       -0.00850 
sampled:dilut2 samplee:dilut2 samplef:dilut2 sampleb:dilut3 
       0.00108       -0.04192        0.01935       -0.02507 
samplec:dilut3 sampled:dilut3 samplee:dilut3 samplef:dilut3 
       0.01865        0.00399       -0.02771        0.05432 
sampleb:dilut4 samplec:dilut4 sampled:dilut4 samplee:dilut4 
       0.06079        0.00526       -0.01649        0.04980 
samplef:dilut4 sampleb:dilut5 samplec:dilut5 sampled:dilut5 
       0.06337       -0.04576       -0.07260       -0.17776 
samplee:dilut5 samplef:dilut5 
       0.01361        0.00402 

Random effects:
 Composite Structure: Blocked

 Block 1: (Intercept)
 Formula: ~1 | Block
        (Intercept)
StdDev:     0.00981

 Block 2: samplea, sampleb, samplec, sampled, samplee, samplef
 Formula: ~sample - 1 | Block
 Structure: Multiple of an Identity
        samplea sampleb samplec sampled samplee samplef
StdDev:  0.0253  0.0253  0.0253  0.0253  0.0253  0.0253

 Block 3: dilut1, dilut2, dilut3, dilut4, dilut5
 Formula: ~dilut - 1 | Block
 Structure: Multiple of an Identity
         dilut1  dilut2  dilut3  dilut4  dilut5 Residual
StdDev: 0.00913 0.00913 0.00913 0.00913 0.00913   0.0416

Number of Observations: 60
Number of Groups: 2 

> anova(fm1Assay)
             numDF denDF F-value p-value
(Intercept)      1    29     538  <.0001
sample           5    29      11  <.0001
dilut            4    29     421  <.0001
sample:dilut    20    29       2   0.119

> formula(Oxide)
Thickness ~ 1 | Lot/Wafer

> fm1Oxide <- lme(Thickness ~ 1, Oxide)

> fm1Oxide
Linear mixed-effects model fit by REML
  Data: Oxide 
  Log-restricted-likelihood: -227
  Fixed: Thickness ~ 1 
(Intercept) 
       2000 

Random effects:
 Formula: ~1 | Lot
        (Intercept)
StdDev:        11.4

 Formula: ~1 | Wafer %in% Lot
        (Intercept) Residual
StdDev:        5.99     3.55

Number of Observations: 72
Number of Groups: 
           Lot Wafer %in% Lot 
             8             24 

> intervals(fm1Oxide, which = "var-cov")
Approximate 95% confidence intervals

 Random Effects:
  Level: Lot 
                lower est. upper
sd((Intercept))  6.39 11.4  20.3
  Level: Wafer 
                lower est. upper
sd((Intercept))  4.06 5.99  8.82

 Within-group standard error:
lower  est. upper 
 2.90  3.55  4.33 

> fm2Oxide <- update(fm1Oxide, random = ~ 1 | Lot)

> anova(fm1Oxide, fm2Oxide)
         Model df AIC BIC logLik   Test L.Ratio p-value
fm1Oxide     1  4 462 471   -227                       
fm2Oxide     2  3 497 504   -246 1 vs 2    37.1  <.0001

> coef(fm1Oxide, level = 1)
  (Intercept)
1        1997
2        1989
3        2001
4        1996
5        2014
6        2020
7        1992
8        1994

> coef(fm1Oxide, level = 2)
    (Intercept)
1/1        2003
1/2        1985
1/3        2001
2/1        1990
2/2        1988
2/3        1986
3/1        2002
3/2        2000
3/3        2000
4/1        1996
4/2        1999
4/3        1991
5/1        2009
5/2        2017
5/3        2019
6/1        2031
6/2        2022
6/3        2011
7/1        1990
7/2        1991
7/3        1992
8/1        1994
8/2        1995
8/3        1991

> ranef(fm1Oxide, level = 1:2)
Level: Lot 
  (Intercept)
1      -3.463
2     -11.222
3       0.869
4      -4.471
5      13.463
6      19.408
7      -8.199
8      -6.385

Level: Wafer %in% Lot 
    (Intercept)
1/1      6.5460
1/2    -11.9589
1/3      4.4567
2/1      0.6586
2/2     -0.8337
2/3     -2.9230
3/1      1.4728
3/2     -0.6164
3/3     -0.6164
4/1     -0.0135
4/2      3.2696
4/3     -4.4905
5/1     -4.4318
5/2      3.0298
5/3      5.1191
6/1     11.7350
6/2      2.1841
6/3     -8.5607
7/1     -1.7494
7/2     -0.5556
7/3      0.0414
8/1     -0.0902
8/2      1.4021
8/3     -3.0749

> fm1Wafer <- lme(current ~ voltage + I(voltage^2), data = Wafer,
+                 random = list(Wafer = pdDiag(~voltage + I(voltage^2)),
+                 Site = pdDiag(~voltage + I(voltage^2))))

> ## IGNORE_RDIFF_BEGIN
> summary(fm1Wafer)
Linear mixed-effects model fit by REML
  Data: Wafer 
   AIC  BIC logLik
  -282 -242    151

Random effects:
 Formula: ~voltage + I(voltage^2) | Wafer
 Structure: Diagonal
        (Intercept) voltage I(voltage^2)
StdDev:    2.81e-05   0.187        0.025

 Formula: ~voltage + I(voltage^2) | Site %in% Wafer
 Structure: Diagonal
        (Intercept) voltage I(voltage^2) Residual
StdDev:    8.17e-06   0.136     2.45e-08    0.115

Fixed effects:  current ~ voltage + I(voltage^2) 
             Value Std.Error  DF t-value p-value
(Intercept)  -4.46    0.0513 318   -87.0       0
voltage       5.90    0.0927 318    63.7       0
I(voltage^2)  1.17    0.0230 318    51.0       0
 Correlation: 
             (Intr) voltag
voltage      -0.735       
I(voltage^2)  0.884 -0.698

Standardized Within-Group Residuals:
    Min      Q1     Med      Q3     Max 
-1.8966 -0.5354  0.0249  0.7985  1.7777 

Number of Observations: 400
Number of Groups: 
          Wafer Site %in% Wafer 
             10              80 

> ## IGNORE_RDIFF_END
> fitted(fm1Wafer, level = 0)
    1     1     1     1     1     1     1     1     1     1 
 1.01  4.31  7.98 12.03 16.45  1.01  4.31  7.98 12.03 16.45 
    1     1     1     1     1     1     1     1     1     1 
 1.01  4.31  7.98 12.03 16.45  1.01  4.31  7.98 12.03 16.45 
    1     1     1     1     1     1     1     1     1     1 
 1.01  4.31  7.98 12.03 16.45  1.01  4.31  7.98 12.03 16.45 
    1     1     1     1     1     1     1     1     1     1 
 1.01  4.31  7.98 12.03 16.45  1.01  4.31  7.98 12.03 16.45 
    2     2     2     2     2     2     2     2     2     2 
 1.01  4.31  7.98 12.03 16.45  1.01  4.31  7.98 12.03 16.45 
    2     2     2     2     2     2     2     2     2     2 
 1.01  4.31  7.98 12.03 16.45  1.01  4.31  7.98 12.03 16.45 
    2     2     2     2     2     2     2     2     2     2 
 1.01  4.31  7.98 12.03 16.45  1.01  4.31  7.98 12.03 16.45 
    2     2     2     2     2     2     2     2     2     2 
 1.01  4.31  7.98 12.03 16.45  1.01  4.31  7.98 12.03 16.45 
    3     3     3     3     3     3     3     3     3     3 
 1.01  4.31  7.98 12.03 16.45  1.01  4.31  7.98 12.03 16.45 
    3     3     3     3     3     3     3     3     3     3 
 1.01  4.31  7.98 12.03 16.45  1.01  4.31  7.98 12.03 16.45 
    3     3     3     3     3     3     3     3     3     3 
 1.01  4.31  7.98 12.03 16.45  1.01  4.31  7.98 12.03 16.45 
    3     3     3     3     3     3     3     3     3     3 
 1.01  4.31  7.98 12.03 16.45  1.01  4.31  7.98 12.03 16.45 
    4     4     4     4     4     4     4     4     4     4 
 1.01  4.31  7.98 12.03 16.45  1.01  4.31  7.98 12.03 16.45 
    4     4     4     4     4     4     4     4     4     4 
 1.01  4.31  7.98 12.03 16.45  1.01  4.31  7.98 12.03 16.45 
    4     4     4     4     4     4     4     4     4     4 
 1.01  4.31  7.98 12.03 16.45  1.01  4.31  7.98 12.03 16.45 
    4     4     4     4     4     4     4     4     4     4 
 1.01  4.31  7.98 12.03 16.45  1.01  4.31  7.98 12.03 16.45 
    5     5     5     5     5     5     5     5     5     5 
 1.01  4.31  7.98 12.03 16.45  1.01  4.31  7.98 12.03 16.45 
    5     5     5     5     5     5     5     5     5     5 
 1.01  4.31  7.98 12.03 16.45  1.01  4.31  7.98 12.03 16.45 
    5     5     5     5     5     5     5     5     5     5 
 1.01  4.31  7.98 12.03 16.45  1.01  4.31  7.98 12.03 16.45 
    5     5     5     5     5     5     5     5     5     5 
 1.01  4.31  7.98 12.03 16.45  1.01  4.31  7.98 12.03 16.45 
    6     6     6     6     6     6     6     6     6     6 
 1.01  4.31  7.98 12.03 16.45  1.01  4.31  7.98 12.03 16.45 
    6     6     6     6     6     6     6     6     6     6 
 1.01  4.31  7.98 12.03 16.45  1.01  4.31  7.98 12.03 16.45 
    6     6     6     6     6     6     6     6     6     6 
 1.01  4.31  7.98 12.03 16.45  1.01  4.31  7.98 12.03 16.45 
    6     6     6     6     6     6     6     6     6     6 
 1.01  4.31  7.98 12.03 16.45  1.01  4.31  7.98 12.03 16.45 
    7     7     7     7     7     7     7     7     7     7 
 1.01  4.31  7.98 12.03 16.45  1.01  4.31  7.98 12.03 16.45 
    7     7     7     7     7     7     7     7     7     7 
 1.01  4.31  7.98 12.03 16.45  1.01  4.31  7.98 12.03 16.45 
    7     7     7     7     7     7     7     7     7     7 
 1.01  4.31  7.98 12.03 16.45  1.01  4.31  7.98 12.03 16.45 
    7     7     7     7     7     7     7     7     7     7 
 1.01  4.31  7.98 12.03 16.45  1.01  4.31  7.98 12.03 16.45 
    8     8     8     8     8     8     8     8     8     8 
 1.01  4.31  7.98 12.03 16.45  1.01  4.31  7.98 12.03 16.45 
    8     8     8     8     8     8     8     8     8     8 
 1.01  4.31  7.98 12.03 16.45  1.01  4.31  7.98 12.03 16.45 
    8     8     8     8     8     8     8     8     8     8 
 1.01  4.31  7.98 12.03 16.45  1.01  4.31  7.98 12.03 16.45 
    8     8     8     8     8     8     8     8     8     8 
 1.01  4.31  7.98 12.03 16.45  1.01  4.31  7.98 12.03 16.45 
    9     9     9     9     9     9     9     9     9     9 
 1.01  4.31  7.98 12.03 16.45  1.01  4.31  7.98 12.03 16.45 
    9     9     9     9     9     9     9     9     9     9 
 1.01  4.31  7.98 12.03 16.45  1.01  4.31  7.98 12.03 16.45 
    9     9     9     9     9     9     9     9     9     9 
 1.01  4.31  7.98 12.03 16.45  1.01  4.31  7.98 12.03 16.45 
    9     9     9     9     9     9     9     9     9     9 
 1.01  4.31  7.98 12.03 16.45  1.01  4.31  7.98 12.03 16.45 
   10    10    10    10    10    10    10    10    10    10 
 1.01  4.31  7.98 12.03 16.45  1.01  4.31  7.98 12.03 16.45 
   10    10    10    10    10    10    10    10    10    10 
 1.01  4.31  7.98 12.03 16.45  1.01  4.31  7.98 12.03 16.45 
   10    10    10    10    10    10    10    10    10    10 
 1.01  4.31  7.98 12.03 16.45  1.01  4.31  7.98 12.03 16.45 
   10    10    10    10    10    10    10    10    10    10 
 1.01  4.31  7.98 12.03 16.45  1.01  4.31  7.98 12.03 16.45 
attr(,"label")
[1] "Fitted values (mA)"

> resid(fm1Wafer, level = 1:2)
        Wafer      Site
1    0.061492  0.068062
2   -0.189869 -0.180013
3   -0.015086 -0.001944
4    0.103762  0.120189
5   -0.053726 -0.034014
6    0.192612  0.073736
7    0.044131 -0.134183
8    0.275914  0.038163
9    0.431762  0.134573
10   0.306274 -0.050353
11   0.084612  0.060177
12  -0.150069 -0.186722
13   0.045314 -0.003556
14   0.185762  0.124675
15   0.054274 -0.019031
16   0.042212  0.073671
17  -0.237069 -0.189880
18  -0.077086 -0.014167
19   0.035762  0.114410
20  -0.121726 -0.027348
21   0.092692  0.076696
22  -0.149069 -0.173062
23   0.033314  0.001324
24   0.159762  0.119774
25   0.014274 -0.033712
26  -0.057768  0.111841
27  -0.453669 -0.199256
28  -0.379286 -0.040068
29  -0.339838  0.084185
30  -0.553726 -0.044899
31   0.047012  0.088048
32  -0.238069 -0.176514
33  -0.090886 -0.008812
34   0.007762  0.110354
35  -0.165726 -0.042616
36   0.079392  0.084841
37  -0.172269 -0.164095
38  -0.006486  0.004413
39   0.101762  0.115385
40  -0.063726 -0.047378
41   0.038702  0.065476
42  -0.209573 -0.169412
43  -0.048411  0.005137
44   0.036789  0.103724
45  -0.117373 -0.037052
46   0.266102  0.151852
47   0.114027 -0.057349
48   0.302789  0.074288
49   0.390789  0.105163
50   0.226627 -0.116125
51   0.299502  0.205135
52   0.129627 -0.011925
53   0.280989  0.092254
54   0.324789  0.088870
55   0.120627 -0.162477
56  -0.032838  0.072449
57  -0.343973 -0.186043
58  -0.225411 -0.014838
59  -0.171211  0.092005
60  -0.353373 -0.037514
61   0.262902  0.160786
62   0.095827 -0.057348
63   0.274189  0.069956
64   0.356789  0.101497
65   0.188627 -0.117724
66   0.000342  0.087867
67  -0.298573 -0.167286
68  -0.178211 -0.003161
69  -0.127211  0.091601
70  -0.315373 -0.052799
71   0.100502  0.127285
72  -0.153973 -0.113800
73  -0.025611  0.027954
74   0.022789  0.089745
75  -0.169373 -0.089027
76   0.032102  0.097186
77  -0.244373 -0.146748
78  -0.120611  0.009556
79  -0.071211  0.091498
80  -0.261373 -0.066123
81  -0.004099  0.052717
82  -0.278076 -0.192852
83  -0.127696 -0.014064
84  -0.029418  0.112622
85  -0.197444 -0.026995
86   0.052321  0.089249
87  -0.208276 -0.152884
88  -0.067296  0.006561
89   0.014582  0.106902
90  -0.171444 -0.060659
91   0.118641  0.062782
92  -0.064476 -0.148264
93   0.134904  0.023187
94   0.266582  0.126935
95   0.120556 -0.047019
96  -0.041079  0.051265
97  -0.346676 -0.208160
98  -0.212496 -0.027807
99  -0.121418  0.109442
100 -0.297444 -0.020411
101  0.128041  0.079868
102 -0.066076 -0.138336
103  0.121104  0.024758
104  0.240582  0.120149
105  0.088556 -0.055963
106 -0.091839  0.070304
107 -0.452476 -0.209261
108 -0.361896 -0.037608
109 -0.311418  0.093941
110 -0.519444 -0.033013
111  0.286041  0.146703
112  0.154524 -0.054483
113  0.353704  0.075028
114  0.468582  0.120237
115  0.298556 -0.119458
116  0.253641  0.183845
117  0.066124 -0.038569
118  0.211104  0.071514
119  0.274582  0.100094
120  0.062556 -0.146829
121  0.113168  0.059522
122 -0.082704 -0.163173
123  0.123749  0.016457
124  0.262907  0.128791
125  0.124569 -0.036370
126  0.199348  0.075597
127  0.057096 -0.128531
128  0.288549  0.041047
129  0.444907  0.135529
130  0.316569 -0.054685
131  0.010568  0.105606
132 -0.309104 -0.166546
133 -0.198251 -0.008174
134 -0.139093  0.098502
135 -0.349431 -0.064316
136  0.000368  0.076116
137 -0.314704 -0.201082
138 -0.178051 -0.026555
139 -0.083093  0.106277
140 -0.251431 -0.024187
141  0.016268  0.116152
142 -0.315904 -0.166078
143 -0.212251 -0.012483
144 -0.155093  0.094617
145 -0.363431 -0.063779
146  0.004348  0.054446
147 -0.286504 -0.211357
148 -0.125651 -0.025456
149 -0.009093  0.116151
150 -0.161431 -0.011138
151  0.096848  0.080552
152 -0.138304 -0.162749
153  0.039349  0.006756
154  0.158907  0.118165
155  0.006569 -0.042321
156  0.118788  0.080347
157 -0.096904 -0.154565
158  0.090949  0.014067
159  0.218907  0.122804
160  0.068569 -0.046754
161 -0.029651  0.042434
162 -0.299821 -0.191694
163 -0.157165 -0.012996
164 -0.067684  0.112527
165 -0.246778 -0.030524
166  0.116949  0.129128
167 -0.114421 -0.096153
168  0.013235  0.037592
169  0.072316  0.102762
170 -0.146778 -0.110243
171  0.197149  0.101805
172  0.049179 -0.093837
173  0.245235  0.054547
174  0.362316  0.123955
175  0.195222 -0.090810
176  0.048749  0.058063
177 -0.177021 -0.163051
178 -0.010365  0.008261
179  0.094316  0.117599
180 -0.072778 -0.044839
181  0.214149  0.102694
182  0.073779 -0.093404
183  0.277635  0.054723
184  0.402316  0.123676
185  0.249222 -0.085146
186 -0.092031  0.056118
187 -0.426021 -0.203798
188 -0.326965 -0.030668
189 -0.271684  0.098687
190 -0.478778 -0.034333
191  0.187949  0.129497
192  0.004979 -0.082699
193  0.168635  0.051730
194  0.256316  0.110185
195  0.069222 -0.106135
196  0.095349  0.120157
197 -0.145621 -0.108410
198 -0.019765  0.029849
199  0.040316  0.102333
200 -0.174778 -0.100357
201  0.115311  0.075105
202 -0.097595 -0.157904
203  0.094646  0.014234
204  0.223635  0.123120
205  0.077572 -0.043047
206  0.121051  0.100980
207 -0.110195 -0.140302
208  0.058846  0.018704
209  0.165635  0.115457
210 -0.004428 -0.064642
211  0.079591  0.081229
212 -0.172795 -0.170338
213 -0.001954  0.001323
214  0.113635  0.117730
215 -0.046428 -0.041514
216  0.007011  0.076714
217 -0.304595 -0.200040
218 -0.163954 -0.024547
219 -0.066365  0.107893
220 -0.234428 -0.025319
221  0.066991  0.085934
222 -0.202595 -0.174180
223 -0.042754 -0.004867
224  0.065635  0.112994
225 -0.096428 -0.039598
226 -0.020549  0.093776
227 -0.371395 -0.199907
228 -0.261754 -0.033103
229 -0.188365  0.097449
230 -0.376428 -0.033452
231  0.124251  0.092105
232 -0.097195 -0.145414
233  0.081646  0.017355
234  0.199635  0.119271
235  0.039572 -0.056865
236  0.104871  0.083043
237 -0.123995 -0.156738
238  0.055046  0.011389
239  0.173635  0.119064
240  0.017572 -0.047914
241  0.227356  0.097058
242  0.136724 -0.058724
243  0.348539  0.087942
244  0.457002  0.131256
245  0.268913 -0.121982
246 -0.049644 -0.001886
247 -0.250476 -0.178840
248 -0.082661  0.012853
249  0.007002  0.126395
250 -0.185087 -0.041815
251  0.491556  0.164445
252  0.535924  0.045257
253  0.814739  0.160517
254  0.963002  0.145224
255  0.798913 -0.182420
256  0.035556 -0.000644
257 -0.106476 -0.160777
258  0.103139  0.030738
259  0.229002  0.138501
260  0.066913 -0.041688
261  0.084356  0.047445
262 -0.064476 -0.119844
263  0.122139  0.048316
264  0.219002  0.126723
265  0.030913 -0.079821
266 -0.102844 -0.008156
267 -0.348676 -0.206645
268 -0.197861 -0.008486
269 -0.112998  0.123721
270 -0.311087 -0.027023
271 -0.104044  0.032614
272 -0.381676 -0.176689
273 -0.275861 -0.002545
274 -0.234998  0.106647
275 -0.471087 -0.061112
276 -0.127844  0.030339
277 -0.422076 -0.184802
278 -0.325461 -0.009095
279 -0.292998  0.102459
280 -0.531087 -0.056538
281  0.272748  0.047840
282  0.262060 -0.075302
283  0.546385  0.096569
284  0.718724  0.156454
285  0.586276 -0.088447
286  0.249948  0.062457
287  0.206660 -0.074577
288  0.464785  0.089803
289  0.616724  0.147996
290  0.466276 -0.096197
291 -0.032652  0.011344
292 -0.243540 -0.177547
293 -0.075615  0.012376
294  0.014724  0.124713
295 -0.175724 -0.043737
296 -0.108452 -0.012938
297 -0.355740 -0.212470
298 -0.201215 -0.010187
299 -0.113276  0.125508
300 -0.309724 -0.023182
301 -0.096052  0.018185
302 -0.362540 -0.191185
303 -0.234015 -0.005541
304 -0.171276  0.114316
305 -0.387724 -0.045013
306 -0.123652 -0.009999
307 -0.389340 -0.218861
308 -0.245215 -0.017909
309 -0.163276  0.120856
310 -0.359724 -0.018765
311 -0.108452  0.037755
312 -0.402940 -0.183630
313 -0.300215 -0.007802
314 -0.259276  0.106241
315 -0.497724 -0.059104
316  0.285348  0.119276
317  0.217660 -0.031449
318  0.435785  0.103641
319  0.548724  0.133543
320  0.356276 -0.141940
321  0.066249  0.061990
322 -0.106937 -0.113325
323  0.058058  0.049541
324  0.133235  0.122588
325 -0.084807 -0.097583
326  0.058049  0.013390
327 -0.080137 -0.147125
328  0.128858  0.039540
329  0.251235  0.139588
330  0.077193 -0.056784
331  0.004649  0.041019
332 -0.199737 -0.145182
333 -0.044542  0.028198
334  0.029235  0.120160
335 -0.182807 -0.073697
336  0.088449 -0.002738
337 -0.008937 -0.145718
338  0.230458  0.048083
339  0.377235  0.149266
340  0.225193 -0.048369
341  0.017249  0.032907
342 -0.167737 -0.144250
343  0.000858  0.032174
344  0.085235  0.124380
345 -0.116807 -0.069833
346 -0.084751 -0.026585
347 -0.303337 -0.216088
348 -0.124142 -0.007810
349 -0.012765  0.132649
350 -0.184807 -0.010310
351 -0.104351  0.042779
352 -0.394337 -0.173642
353 -0.296142 -0.001881
354 -0.264765  0.103060
355 -0.508807 -0.067416
356  0.278649  0.082220
357  0.247263 -0.047380
358  0.498058  0.105201
359  0.635235  0.144163
360  0.469193 -0.120093
361 -0.107404 -0.012451
362 -0.354401 -0.211972
363 -0.199807 -0.009901
364 -0.112820  0.124562
365 -0.307642 -0.022784
366  0.231396  0.061385
367  0.179399 -0.075617
368  0.428793  0.088772
369  0.571180  0.146153
370  0.410358 -0.099674
371  0.162596  0.050096
372  0.066399 -0.102351
373  0.292993  0.067994
374  0.421180  0.139931
375  0.252358 -0.085141
376 -0.022804 -0.020045
377 -0.198801 -0.194662
378  0.005393  0.010912
379  0.131180  0.138078
380 -0.027642 -0.019364
381  0.015396  0.011892
382 -0.160401 -0.165658
383  0.030993  0.023985
384  0.141180  0.132419
385 -0.035642 -0.046155
386 -0.105404  0.014675
387 -0.373401 -0.193283
388 -0.246407 -0.006248
389 -0.184820  0.115377
390 -0.405642 -0.045405
391  0.164196  0.113448
392  0.015399 -0.060723
393  0.177393  0.075897
394  0.243180  0.116310
395  0.016358 -0.135886
396 -0.008404  0.037007
397 -0.216401 -0.148285
398 -0.065007  0.025815
399  0.005180  0.118707
400 -0.207642 -0.071409

> newWafer <-
+     data.frame(Wafer = rep(1, 4), voltage = c(1, 1.5, 3, 3.5))

> predict(fm1Wafer, newWafer, level = 0:1)
  Wafer predict.fixed predict.Wafer
1     1          2.61          2.40
2     1          7.03          6.72
3     1         23.78         23.23
4     1         30.54         29.92

> newWafer2 <- data.frame(Wafer = rep(1, 4), Site = rep(3, 4),
+                         voltage = c(1, 1.5, 3, 3.5))

> predict(fm1Wafer, newWafer2, level = 0:2)
  Wafer Site predict.fixed predict.Wafer predict.Site
1     1  1/3          2.61          2.40         2.43
2     1  1/3          7.03          6.72         6.77
3     1  1/3         23.78         23.23        23.32
4     1  1/3         30.54         29.92        30.03

> # 4.3 Examining a Fitted Model
> 
> plot(fm2Orth.lme, Subject~resid(.), abline = 0)

> plot(fm2Orth.lme, resid(., type = "p") ~ fitted(.) | Sex,
+       id = 0.05, adj = -0.3)

> fm3Orth.lme <-
+   update(fm2Orth.lme, weights = varIdent(form = ~ 1 | Sex))

> fm3Orth.lme
Linear mixed-effects model fit by REML
  Data: Orthodont 
  Log-restricted-likelihood: -206
  Fixed: distance ~ Sex + I(age - 11) + Sex:I(age - 11) 
          (Intercept)             SexFemale 
               24.969                -2.321 
          I(age - 11) SexFemale:I(age - 11) 
                0.784                -0.305 

Random effects:
 Formula: ~I(age - 11) | Subject
 Structure: General positive-definite, Log-Cholesky parametrization
            StdDev Corr  
(Intercept) 1.855  (Intr)
I(age - 11) 0.157  0.394 
Residual    1.630        

Variance function:
 Structure: Different standard deviations per stratum
 Formula: ~1 | Sex 
 Parameter estimates:
  Male Female 
 1.000  0.409 
Number of Observations: 108
Number of Groups: 27 

> plot(fm3Orth.lme, distance ~ fitted(.),
+       id = 0.05, adj = -0.3)

> anova(fm2Orth.lme, fm3Orth.lme)
            Model df AIC BIC logLik   Test L.Ratio p-value
fm2Orth.lme     1  8 451 473   -218                       
fm3Orth.lme     2  9 430 453   -206 1 vs 2    23.8  <.0001

> qqnorm(fm3Orth.lme, ~resid(.) | Sex)

> plot(fm2IGF.lme, resid(., type = "p") ~ fitted(.) | Lot,
+       layout = c(5,2))

> qqnorm(fm2IGF.lme, ~ resid(.), id = 0.05, adj = -0.75)

> plot(fm1Oxide)

> qqnorm(fm1Oxide)

> plot(fm1Wafer, resid(.) ~ voltage | Wafer)

> plot(fm1Wafer, resid(.) ~ voltage | Wafer,
+       panel = function(x, y, ...) {
+                  panel.grid()
+                  panel.xyplot(x, y)
+                  panel.loess(x, y, lty = 2)
+                  panel.abline(0, 0)
+               })

> with(Wafer,
+      coef(lm(resid(fm1Wafer) ~ cos(4.19*voltage)+sin(4.19*voltage)-1)))
cos(4.19 * voltage) sin(4.19 * voltage) 
            -0.0519              0.1304 

> nls(resid(fm1Wafer) ~ b3*cos(w*voltage) + b4*sin(w*voltage), Wafer,
+       start = list(b3 = -0.0519, b4 = 0.1304, w = 4.19))
Nonlinear regression model
  model: resid(fm1Wafer) ~ b3 * cos(w * voltage) + b4 * sin(w * voltage)
   data: Wafer
     b3      b4       w 
-0.1117  0.0777  4.5679 
 residual sum-of-squares: 0.729

Number of iterations to convergence: 6 
Achieved convergence tolerance: 1.12e-06

> fm2Wafer <- update(fm1Wafer,
+       . ~ . + cos(4.5679*voltage) + sin(4.5679*voltage),
+       random = list(Wafer=pdDiag(~voltage+I(voltage^2)),
+              Site=pdDiag(~voltage+I(voltage^2))))

> summary(fm2Wafer)
Linear mixed-effects model fit by REML
  Data: Wafer 
    AIC   BIC logLik
  -1233 -1185    628

Random effects:
 Formula: ~voltage + I(voltage^2) | Wafer
 Structure: Diagonal
        (Intercept) voltage I(voltage^2)
StdDev:       0.129   0.349       0.0491

 Formula: ~voltage + I(voltage^2) | Site %in% Wafer
 Structure: Diagonal
        (Intercept) voltage I(voltage^2) Residual
StdDev:      0.0397   0.234       0.0475   0.0113

Fixed effects:  current ~ voltage + I(voltage^2) + cos(4.5679 * voltage) + sin(4.5679 *      voltage) 
                      Value Std.Error  DF t-value p-value
(Intercept)           -4.26    0.0422 316  -100.8       0
voltage                5.62    0.1142 316    49.2       0
I(voltage^2)           1.26    0.0170 316    74.2       0
cos(4.5679 * voltage) -0.10    0.0011 316   -85.0       0
sin(4.5679 * voltage)  0.10    0.0015 316    69.4       0
 Correlation: 
                      (Intr) voltag I(v^2) c(4.*v
voltage               -0.029                     
I(voltage^2)           0.060 -0.031              
cos(4.5679 * voltage)  0.162 -0.082  0.172       
sin(4.5679 * voltage)  0.200 -0.101  0.212  0.567

Standardized Within-Group Residuals:
    Min      Q1     Med      Q3     Max 
-2.4272 -0.4032  0.0253  0.3936  2.8427 

Number of Observations: 400
Number of Groups: 
          Wafer Site %in% Wafer 
             10              80 

> ## IGNORE_RDIFF_BEGIN
> intervals(fm2Wafer)
Approximate 95% confidence intervals

 Fixed effects:
                        lower    est.   upper
(Intercept)           -4.3385 -4.2554 -4.1723
voltage                5.3977  5.6224  5.8470
I(voltage^2)           1.2251  1.2585  1.2919
cos(4.5679 * voltage) -0.0978 -0.0956 -0.0933
sin(4.5679 * voltage)  0.1014  0.1043  0.1073

 Random Effects:
  Level: Wafer 
                  lower   est. upper
sd((Intercept))  0.0802 0.1289 0.207
sd(voltage)      0.2135 0.3487 0.569
sd(I(voltage^2)) 0.0290 0.0491 0.083
  Level: Site 
                  lower   est.  upper
sd((Intercept))  0.0220 0.0397 0.0717
sd(voltage)      0.1909 0.2344 0.2878
sd(I(voltage^2)) 0.0383 0.0475 0.0590

 Within-group standard error:
  lower    est.   upper 
0.00927 0.01133 0.01383 

> ## IGNORE_RDIFF_END
> qqnorm(fm2Wafer)

> qqnorm(fm2Orth.lme, ~ranef(.), id = 0.10, cex = 0.7)

> pairs(fm2Orth.lme, ~ranef(.) | Sex,
+       id = ~ Subject == "M13", adj = -0.3)

> fm2IGF.lme
Linear mixed-effects model fit by REML
  Data: IGF 
  Log-restricted-likelihood: -297
  Fixed: conc ~ age 
(Intercept)         age 
    5.36904    -0.00193 

Random effects:
 Formula: ~age | Lot
 Structure: Diagonal
        (Intercept)     age Residual
StdDev:    3.62e-05 0.00537    0.822

Number of Observations: 237
Number of Groups: 10 

> c(0.00031074, 0.0053722)/abs(fixef(fm2IGF.lme))
(Intercept)         age 
   5.79e-05    2.78e+00 

> fm3IGF.lme <- update(fm2IGF.lme, random = ~ age - 1)

> anova(fm2IGF.lme, fm3IGF.lme)
           Model df AIC BIC logLik   Test  L.Ratio p-value
fm2IGF.lme     1  5 605 622   -297                        
fm3IGF.lme     2  4 603 617   -297 1 vs 2 1.47e-07       1

> qqnorm(fm1Oxide, ~ranef(., level = 1), id=0.10)

> qqnorm(fm1Oxide, ~ranef(., level = 2), id=0.10)

> #fm3Wafer <- update(fm2Wafer,
> #              random = list(Wafer = ~voltage+I(voltage^2),
> #                            Site = pdDiag(~voltage+I(voltage^2))),
> #                   control = list(msVerbose = TRUE, msMaxIter = 200)
> #                   )
> #fm3Wafer
> #anova(fm2Wafer, fm3Wafer)
> #fm4Wafer <- update(fm2Wafer,
> #                   random = list(Wafer = ~ voltage + I(voltage^2),
> #                   Site = pdBlocked(list(~1,
> #                   ~voltage+I(voltage^2) - 1))),
> #                   control = list(msVerbose = TRUE,
> #                   msMaxIter = 200))
> #fm4Wafer
> #anova(fm3Wafer, fm4Wafer)
> #qqnorm(fm4Wafer, ~ranef(., level = 2), id = 0.05,
> #        cex = 0.7, layout = c(3, 1))
> 
> # The next line is not in the book but is needed to get fm1Machine
> 
> fm1Machine <-
+   lme(score ~ Machine, data = Machines, random = ~ 1 | Worker)

> (fm3Machine <- update(fm1Machine, random = ~Machine-1|Worker))
Linear mixed-effects model fit by REML
  Data: Machines 
  Log-restricted-likelihood: -104
  Fixed: score ~ Machine 
(Intercept)    MachineB    MachineC 
      52.36        7.97       13.92 

Random effects:
 Formula: ~Machine - 1 | Worker
 Structure: General positive-definite, Log-Cholesky parametrization
         StdDev Corr         
MachineA 4.079  MachnA MachnB
MachineB 8.625  0.803        
MachineC 4.389  0.623  0.771 
Residual 0.962               

Number of Observations: 54
Number of Groups: 6 

> # cleanup
> 
> summary(warnings())
No warnings

======
ch05.R
======

> #-*- R -*-
> 
> # initialization
> 
> library(nlme)

> options(width = 65,
+         ## reduce platform dependence in printed output when testing
+         digits = if(nzchar(Sys.getenv("R_TESTS"))) 3 else 5)

> options(contrasts = c(unordered = "contr.helmert", ordered = "contr.poly"))

> pdf(file = "ch05.pdf")

> # Chapter 5    Extending the Basic Linear Mixed-Effects Models
> 
> # 5.1 General Formulation of the Extended Model
> 
> vf1Fixed <- varFixed(~ age)

> vf1Fixed <- Initialize(vf1Fixed, data = Orthodont)

> varWeights(vf1Fixed)
  [1] 0.354 0.316 0.289 0.267 0.354 0.316 0.289 0.267 0.354 0.316
 [11] 0.289 0.267 0.354 0.316 0.289 0.267 0.354 0.316 0.289 0.267
 [21] 0.354 0.316 0.289 0.267 0.354 0.316 0.289 0.267 0.354 0.316
 [31] 0.289 0.267 0.354 0.316 0.289 0.267 0.354 0.316 0.289 0.267
 [41] 0.354 0.316 0.289 0.267 0.354 0.316 0.289 0.267 0.354 0.316
 [51] 0.289 0.267 0.354 0.316 0.289 0.267 0.354 0.316 0.289 0.267
 [61] 0.354 0.316 0.289 0.267 0.354 0.316 0.289 0.267 0.354 0.316
 [71] 0.289 0.267 0.354 0.316 0.289 0.267 0.354 0.316 0.289 0.267
 [81] 0.354 0.316 0.289 0.267 0.354 0.316 0.289 0.267 0.354 0.316
 [91] 0.289 0.267 0.354 0.316 0.289 0.267 0.354 0.316 0.289 0.267
[101] 0.354 0.316 0.289 0.267 0.354 0.316 0.289 0.267

> vf1Ident <- varIdent(c(Female = 0.5), ~ 1 | Sex)

> vf1Ident <- Initialize(vf1Ident, Orthodont)

> varWeights(vf1Ident)
  Male   Male   Male   Male   Male   Male   Male   Male   Male 
     1      1      1      1      1      1      1      1      1 
  Male   Male   Male   Male   Male   Male   Male   Male   Male 
     1      1      1      1      1      1      1      1      1 
  Male   Male   Male   Male   Male   Male   Male   Male   Male 
     1      1      1      1      1      1      1      1      1 
  Male   Male   Male   Male   Male   Male   Male   Male   Male 
     1      1      1      1      1      1      1      1      1 
  Male   Male   Male   Male   Male   Male   Male   Male   Male 
     1      1      1      1      1      1      1      1      1 
  Male   Male   Male   Male   Male   Male   Male   Male   Male 
     1      1      1      1      1      1      1      1      1 
  Male   Male   Male   Male   Male   Male   Male   Male   Male 
     1      1      1      1      1      1      1      1      1 
  Male Female Female Female Female Female Female Female Female 
     1      2      2      2      2      2      2      2      2 
Female Female Female Female Female Female Female Female Female 
     2      2      2      2      2      2      2      2      2 
Female Female Female Female Female Female Female Female Female 
     2      2      2      2      2      2      2      2      2 
Female Female Female Female Female Female Female Female Female 
     2      2      2      2      2      2      2      2      2 
Female Female Female Female Female Female Female Female Female 
     2      2      2      2      2      2      2      2      2 

> vf2Ident <- varIdent(form =  ~ 1 | Sex, fixed = c(Female = 0.5))

> vf2Ident <- Initialize(vf2Ident, Orthodont)

> varWeights(vf2Ident)
  Male   Male   Male   Male   Male   Male   Male   Male   Male 
     1      1      1      1      1      1      1      1      1 
  Male   Male   Male   Male   Male   Male   Male   Male   Male 
     1      1      1      1      1      1      1      1      1 
  Male   Male   Male   Male   Male   Male   Male   Male   Male 
     1      1      1      1      1      1      1      1      1 
  Male   Male   Male   Male   Male   Male   Male   Male   Male 
     1      1      1      1      1      1      1      1      1 
  Male   Male   Male   Male   Male   Male   Male   Male   Male 
     1      1      1      1      1      1      1      1      1 
  Male   Male   Male   Male   Male   Male   Male   Male   Male 
     1      1      1      1      1      1      1      1      1 
  Male   Male   Male   Male   Male   Male   Male   Male   Male 
     1      1      1      1      1      1      1      1      1 
  Male Female Female Female Female Female Female Female Female 
     1      2      2      2      2      2      2      2      2 
Female Female Female Female Female Female Female Female Female 
     2      2      2      2      2      2      2      2      2 
Female Female Female Female Female Female Female Female Female 
     2      2      2      2      2      2      2      2      2 
Female Female Female Female Female Female Female Female Female 
     2      2      2      2      2      2      2      2      2 
Female Female Female Female Female Female Female Female Female 
     2      2      2      2      2      2      2      2      2 

> vf3Ident <- varIdent(form = ~ 1 | Sex * age)

> vf3Ident <- Initialize(vf3Ident, Orthodont)

> varWeights(vf3Ident)
   Male*8   Male*10   Male*12   Male*14    Male*8   Male*10 
        1         1         1         1         1         1 
  Male*12   Male*14    Male*8   Male*10   Male*12   Male*14 
        1         1         1         1         1         1 
   Male*8   Male*10   Male*12   Male*14    Male*8   Male*10 
        1         1         1         1         1         1 
  Male*12   Male*14    Male*8   Male*10   Male*12   Male*14 
        1         1         1         1         1         1 
   Male*8   Male*10   Male*12   Male*14    Male*8   Male*10 
        1         1         1         1         1         1 
  Male*12   Male*14    Male*8   Male*10   Male*12   Male*14 
        1         1         1         1         1         1 
   Male*8   Male*10   Male*12   Male*14    Male*8   Male*10 
        1         1         1         1         1         1 
  Male*12   Male*14    Male*8   Male*10   Male*12   Male*14 
        1         1         1         1         1         1 
   Male*8   Male*10   Male*12   Male*14    Male*8   Male*10 
        1         1         1         1         1         1 
  Male*12   Male*14    Male*8   Male*10   Male*12   Male*14 
        1         1         1         1         1         1 
   Male*8   Male*10   Male*12   Male*14  Female*8 Female*10 
        1         1         1         1         1         1 
Female*12 Female*14  Female*8 Female*10 Female*12 Female*14 
        1         1         1         1         1         1 
 Female*8 Female*10 Female*12 Female*14  Female*8 Female*10 
        1         1         1         1         1         1 
Female*12 Female*14  Female*8 Female*10 Female*12 Female*14 
        1         1         1         1         1         1 
 Female*8 Female*10 Female*12 Female*14  Female*8 Female*10 
        1         1         1         1         1         1 
Female*12 Female*14  Female*8 Female*10 Female*12 Female*14 
        1         1         1         1         1         1 
 Female*8 Female*10 Female*12 Female*14  Female*8 Female*10 
        1         1         1         1         1         1 
Female*12 Female*14  Female*8 Female*10 Female*12 Female*14 
        1         1         1         1         1         1 

> vf1Power <- varPower(1)

> formula(vf1Power)
~fitted(.)
<environment: 0x55aa58bbc708>

> vf2Power <- varPower(fixed = 0.5)

> vf3Power <- varPower(form = ~ fitted(.) | Sex,
+   fixed = list(Male = 0.5, Female = 0))

> vf1Exp <- varExp(form = ~ age | Sex, fixed = c(Female = 0))

> vf1ConstPower <- varConstPower(power = 0.5,
+       fixed = list(const = 1))

> vf1Comb <- varComb(varIdent(c(Female = 0.5), ~ 1 | Sex),
+                      varExp(1, ~ age))

> vf1Comb <- Initialize(vf1Comb, Orthodont)

> varWeights(vf1Comb)
  [1] 3.35e-04 4.54e-05 6.14e-06 8.32e-07 3.35e-04 4.54e-05
  [7] 6.14e-06 8.32e-07 3.35e-04 4.54e-05 6.14e-06 8.32e-07
 [13] 3.35e-04 4.54e-05 6.14e-06 8.32e-07 3.35e-04 4.54e-05
 [19] 6.14e-06 8.32e-07 3.35e-04 4.54e-05 6.14e-06 8.32e-07
 [25] 3.35e-04 4.54e-05 6.14e-06 8.32e-07 3.35e-04 4.54e-05
 [31] 6.14e-06 8.32e-07 3.35e-04 4.54e-05 6.14e-06 8.32e-07
 [37] 3.35e-04 4.54e-05 6.14e-06 8.32e-07 3.35e-04 4.54e-05
 [43] 6.14e-06 8.32e-07 3.35e-04 4.54e-05 6.14e-06 8.32e-07
 [49] 3.35e-04 4.54e-05 6.14e-06 8.32e-07 3.35e-04 4.54e-05
 [55] 6.14e-06 8.32e-07 3.35e-04 4.54e-05 6.14e-06 8.32e-07
 [61] 3.35e-04 4.54e-05 6.14e-06 8.32e-07 6.71e-04 9.08e-05
 [67] 1.23e-05 1.66e-06 6.71e-04 9.08e-05 1.23e-05 1.66e-06
 [73] 6.71e-04 9.08e-05 1.23e-05 1.66e-06 6.71e-04 9.08e-05
 [79] 1.23e-05 1.66e-06 6.71e-04 9.08e-05 1.23e-05 1.66e-06
 [85] 6.71e-04 9.08e-05 1.23e-05 1.66e-06 6.71e-04 9.08e-05
 [91] 1.23e-05 1.66e-06 6.71e-04 9.08e-05 1.23e-05 1.66e-06
 [97] 6.71e-04 9.08e-05 1.23e-05 1.66e-06 6.71e-04 9.08e-05
[103] 1.23e-05 1.66e-06 6.71e-04 9.08e-05 1.23e-05 1.66e-06

> fm1Dial.lme <-
+     lme(rate ~(pressure + I(pressure^2) + I(pressure^3) + I(pressure^4))*QB,
+         Dialyzer, ~ pressure + I(pressure^2))

> fm1Dial.lme
Linear mixed-effects model fit by REML
  Data: Dialyzer 
  Log-restricted-likelihood: -326
  Fixed: rate ~ (pressure + I(pressure^2) + I(pressure^3) + I(pressure^4)) *      QB 
      (Intercept)          pressure     I(pressure^2) 
         -16.5980           88.6733          -42.7320 
    I(pressure^3)     I(pressure^4)               QB1 
           9.2165           -0.7756           -0.6317 
     pressure:QB1 I(pressure^2):QB1 I(pressure^3):QB1 
           0.3104            1.5742            0.0509 
I(pressure^4):QB1 
          -0.0860 

Random effects:
 Formula: ~pressure + I(pressure^2) | Subject
 Structure: General positive-definite, Log-Cholesky parametrization
              StdDev Corr         
(Intercept)   1.50   (Intr) pressr
pressure      4.91   -0.507       
I(pressure^2) 1.47    0.311 -0.944
Residual      1.82                

Number of Observations: 140
Number of Groups: 20 

> plot(fm1Dial.lme, resid(.) ~ pressure, abline = 0)

> fm2Dial.lme <- update(fm1Dial.lme,
+                         weights = varPower(form = ~ pressure))

> fm2Dial.lme
Linear mixed-effects model fit by REML
  Data: Dialyzer 
  Log-restricted-likelihood: -310
  Fixed: rate ~ (pressure + I(pressure^2) + I(pressure^3) + I(pressure^4)) *      QB 
      (Intercept)          pressure     I(pressure^2) 
          -17.680            93.711           -49.187 
    I(pressure^3)     I(pressure^4)               QB1 
           12.245            -1.243            -0.921 
     pressure:QB1 I(pressure^2):QB1 I(pressure^3):QB1 
            1.353             0.480             0.491 
I(pressure^4):QB1 
           -0.146 

Random effects:
 Formula: ~pressure + I(pressure^2) | Subject
 Structure: General positive-definite, Log-Cholesky parametrization
              StdDev Corr         
(Intercept)   1.86   (Intr) pressr
pressure      5.33   -0.522       
I(pressure^2) 1.65    0.362 -0.954
Residual      1.26                

Variance function:
 Structure: Power of variance covariate
 Formula: ~pressure 
 Parameter estimates:
power 
0.749 
Number of Observations: 140
Number of Groups: 20 

> anova(fm1Dial.lme, fm2Dial.lme)
            Model df AIC BIC logLik   Test L.Ratio p-value
fm1Dial.lme     1 17 687 736   -326                       
fm2Dial.lme     2 18 655 707   -310 1 vs 2    33.8  <.0001

> plot(fm2Dial.lme, resid(., type = "p") ~ pressure,
+      abline = 0)

> ## IGNORE_RDIFF_BEGIN
> intervals(fm2Dial.lme)
Approximate 95% confidence intervals

 Fixed effects:
                    lower    est.   upper
(Intercept)       -19.148 -17.680 -16.212
pressure           87.231  93.711 100.192
I(pressure^2)     -57.616 -49.187 -40.757
I(pressure^3)       7.967  12.245  16.523
I(pressure^4)      -1.953  -1.243  -0.533
QB1                -2.478  -0.921   0.636
pressure:QB1       -5.127   1.353   7.833
I(pressure^2):QB1  -7.949   0.480   8.910
I(pressure^3):QB1  -3.787   0.491   4.769
I(pressure^4):QB1  -0.856  -0.146   0.564

 Random Effects:
  Level: Subject 
                                lower   est.   upper
sd((Intercept))                 1.256  1.857  2.7466
sd(pressure)                    3.623  5.328  7.8363
sd(I(pressure^2))               1.091  1.648  2.4909
cor((Intercept),pressure)      -0.803 -0.522 -0.0525
cor((Intercept),I(pressure^2)) -0.166  0.362  0.7292
cor(pressure,I(pressure^2))    -0.985 -0.954 -0.8624

 Variance function:
      lower  est. upper
power 0.508 0.749 0.991

 Within-group standard error:
lower  est. upper 
 1.06  1.26  1.50 

> ## IGNORE_RDIFF_END
> plot(fm2Dial.lme, resid(.) ~ pressure|QB, abline = 0)

> fm3Dial.lme <- update(fm2Dial.lme,
+                       weights=varPower(form = ~ pressure | QB))

> fm3Dial.lme
Linear mixed-effects model fit by REML
  Data: Dialyzer 
  Log-restricted-likelihood: -309
  Fixed: rate ~ (pressure + I(pressure^2) + I(pressure^3) + I(pressure^4)) *      QB 
      (Intercept)          pressure     I(pressure^2) 
          -17.695            93.759           -49.231 
    I(pressure^3)     I(pressure^4)               QB1 
           12.260            -1.244            -1.017 
     pressure:QB1 I(pressure^2):QB1 I(pressure^3):QB1 
            1.840            -0.194             0.827 
I(pressure^4):QB1 
           -0.200 

Random effects:
 Formula: ~pressure + I(pressure^2) | Subject
 Structure: General positive-definite, Log-Cholesky parametrization
              StdDev Corr         
(Intercept)   1.82   (Intr) pressr
pressure      5.24   -0.502       
I(pressure^2) 1.64    0.338 -0.951
Residual      1.26                

Variance function:
 Structure: Power of variance covariate, different strata
 Formula: ~pressure | QB 
 Parameter estimates:
  200   300 
0.648 0.838 
Number of Observations: 140
Number of Groups: 20 

> anova(fm2Dial.lme, fm3Dial.lme)
            Model df AIC BIC logLik   Test L.Ratio p-value
fm2Dial.lme     1 18 655 707   -310                       
fm3Dial.lme     2 19 656 711   -309 1 vs 2   0.711   0.399

> fm4Dial.lme <- update(fm2Dial.lme,
+                       weights = varConstPower(form = ~ pressure))

> anova(fm2Dial.lme, fm4Dial.lme)
            Model df AIC BIC logLik   Test L.Ratio p-value
fm2Dial.lme     1 18 655 707   -310                       
fm4Dial.lme     2 19 657 711   -309 1 vs 2   0.159    0.69

> plot(augPred(fm2Dial.lme), grid = TRUE)

> anova(fm2Dial.lme)
                 numDF denDF F-value p-value
(Intercept)          1   112     553  <.0001
pressure             1   112    2329  <.0001
I(pressure^2)        1   112    1175  <.0001
I(pressure^3)        1   112     360  <.0001
I(pressure^4)        1   112      12  0.0006
QB                   1    18       5  0.0414
pressure:QB          1   112      80  <.0001
I(pressure^2):QB     1   112       1  0.2476
I(pressure^3):QB     1   112       2  0.1370
I(pressure^4):QB     1   112       0  0.6839

> anova(fm2Dial.lme, Terms = 8:10)
F-test for: I(pressure^2):QB, I(pressure^3):QB, I(pressure^4):QB 
  numDF denDF F-value p-value
1     3   112    1.25   0.294

> options(contrasts = c("contr.treatment", "contr.poly"))

> fm1BW.lme <- lme(weight ~ Time * Diet, BodyWeight,
+                    random = ~ Time)

> fm1BW.lme
Linear mixed-effects model fit by REML
  Data: BodyWeight 
  Log-restricted-likelihood: -576
  Fixed: weight ~ Time * Diet 
(Intercept)        Time       Diet2       Diet3  Time:Diet2 
    251.652       0.360     200.665     252.072       0.606 
 Time:Diet3 
      0.298 

Random effects:
 Formula: ~Time | Rat
 Structure: General positive-definite, Log-Cholesky parametrization
            StdDev Corr  
(Intercept) 36.939 (Intr)
Time         0.248 -0.149
Residual     4.444       

Number of Observations: 176
Number of Groups: 16 

> fm2BW.lme <- update(fm1BW.lme, weights = varPower())

> fm2BW.lme
Linear mixed-effects model fit by REML
  Data: BodyWeight 
  Log-restricted-likelihood: -571
  Fixed: weight ~ Time * Diet 
(Intercept)        Time       Diet2       Diet3  Time:Diet2 
    251.602       0.361     200.777     252.170       0.602 
 Time:Diet3 
      0.295 

Random effects:
 Formula: ~Time | Rat
 Structure: General positive-definite, Log-Cholesky parametrization
            StdDev Corr  
(Intercept) 36.898 (Intr)
Time         0.244 -0.145
Residual     0.175       

Variance function:
 Structure: Power of variance covariate
 Formula: ~fitted(.) 
 Parameter estimates:
power 
0.543 
Number of Observations: 176
Number of Groups: 16 

> anova(fm1BW.lme, fm2BW.lme)
          Model df  AIC  BIC logLik   Test L.Ratio p-value
fm1BW.lme     1 10 1172 1203   -576                       
fm2BW.lme     2 11 1164 1198   -571 1 vs 2     9.8  0.0017

> summary(fm2BW.lme)
Linear mixed-effects model fit by REML
  Data: BodyWeight 
   AIC  BIC logLik
  1164 1198   -571

Random effects:
 Formula: ~Time | Rat
 Structure: General positive-definite, Log-Cholesky parametrization
            StdDev Corr  
(Intercept) 36.898 (Intr)
Time         0.244 -0.145
Residual     0.175       

Variance function:
 Structure: Power of variance covariate
 Formula: ~fitted(.) 
 Parameter estimates:
power 
0.543 
Fixed effects:  weight ~ Time * Diet 
            Value Std.Error  DF t-value p-value
(Intercept) 251.6     13.07 157   19.25  0.0000
Time          0.4      0.09 157    4.09  0.0001
Diet2       200.8     22.66  13    8.86  0.0000
Diet3       252.2     22.66  13   11.13  0.0000
Time:Diet2    0.6      0.16 157    3.87  0.0002
Time:Diet3    0.3      0.16 157    1.89  0.0601
 Correlation: 
           (Intr) Time   Diet2  Diet3  Tm:Dt2
Time       -0.152                            
Diet2      -0.577  0.088                     
Diet3      -0.577  0.088  0.333              
Time:Diet2  0.087 -0.569 -0.157 -0.050       
Time:Diet3  0.086 -0.567 -0.050 -0.158  0.322

Standardized Within-Group Residuals:
    Min      Q1     Med      Q3     Max 
-2.9374 -0.4439  0.0799  0.5808  2.2649 

Number of Observations: 176
Number of Groups: 16 

> anova(fm2BW.lme, L = c("Time:Diet2" = 1, "Time:Diet3" = -1))
F-test for linear combination(s)
Time:Diet2 Time:Diet3 
         1         -1 
  numDF denDF F-value p-value
1     1   157    2.86  0.0926

> cs1CompSymm <- corCompSymm(value = 0.3, form = ~ 1 | Subject)

> cs2CompSymm <- corCompSymm(value = 0.3, form = ~ age | Subject)

> cs1CompSymm <- Initialize(cs1CompSymm, data = Orthodont)

> corMatrix(cs1CompSymm)
$M01
     [,1] [,2] [,3] [,4]
[1,]  1.0  0.3  0.3  0.3
[2,]  0.3  1.0  0.3  0.3
[3,]  0.3  0.3  1.0  0.3
[4,]  0.3  0.3  0.3  1.0

$M02
     [,1] [,2] [,3] [,4]
[1,]  1.0  0.3  0.3  0.3
[2,]  0.3  1.0  0.3  0.3
[3,]  0.3  0.3  1.0  0.3
[4,]  0.3  0.3  0.3  1.0

$M03
     [,1] [,2] [,3] [,4]
[1,]  1.0  0.3  0.3  0.3
[2,]  0.3  1.0  0.3  0.3
[3,]  0.3  0.3  1.0  0.3
[4,]  0.3  0.3  0.3  1.0

$M04
     [,1] [,2] [,3] [,4]
[1,]  1.0  0.3  0.3  0.3
[2,]  0.3  1.0  0.3  0.3
[3,]  0.3  0.3  1.0  0.3
[4,]  0.3  0.3  0.3  1.0

$M05
     [,1] [,2] [,3] [,4]
[1,]  1.0  0.3  0.3  0.3
[2,]  0.3  1.0  0.3  0.3
[3,]  0.3  0.3  1.0  0.3
[4,]  0.3  0.3  0.3  1.0

$M06
     [,1] [,2] [,3] [,4]
[1,]  1.0  0.3  0.3  0.3
[2,]  0.3  1.0  0.3  0.3
[3,]  0.3  0.3  1.0  0.3
[4,]  0.3  0.3  0.3  1.0

$M07
     [,1] [,2] [,3] [,4]
[1,]  1.0  0.3  0.3  0.3
[2,]  0.3  1.0  0.3  0.3
[3,]  0.3  0.3  1.0  0.3
[4,]  0.3  0.3  0.3  1.0

$M08
     [,1] [,2] [,3] [,4]
[1,]  1.0  0.3  0.3  0.3
[2,]  0.3  1.0  0.3  0.3
[3,]  0.3  0.3  1.0  0.3
[4,]  0.3  0.3  0.3  1.0

$M09
     [,1] [,2] [,3] [,4]
[1,]  1.0  0.3  0.3  0.3
[2,]  0.3  1.0  0.3  0.3
[3,]  0.3  0.3  1.0  0.3
[4,]  0.3  0.3  0.3  1.0

$M10
     [,1] [,2] [,3] [,4]
[1,]  1.0  0.3  0.3  0.3
[2,]  0.3  1.0  0.3  0.3
[3,]  0.3  0.3  1.0  0.3
[4,]  0.3  0.3  0.3  1.0

$M11
     [,1] [,2] [,3] [,4]
[1,]  1.0  0.3  0.3  0.3
[2,]  0.3  1.0  0.3  0.3
[3,]  0.3  0.3  1.0  0.3
[4,]  0.3  0.3  0.3  1.0

$M12
     [,1] [,2] [,3] [,4]
[1,]  1.0  0.3  0.3  0.3
[2,]  0.3  1.0  0.3  0.3
[3,]  0.3  0.3  1.0  0.3
[4,]  0.3  0.3  0.3  1.0

$M13
     [,1] [,2] [,3] [,4]
[1,]  1.0  0.3  0.3  0.3
[2,]  0.3  1.0  0.3  0.3
[3,]  0.3  0.3  1.0  0.3
[4,]  0.3  0.3  0.3  1.0

$M14
     [,1] [,2] [,3] [,4]
[1,]  1.0  0.3  0.3  0.3
[2,]  0.3  1.0  0.3  0.3
[3,]  0.3  0.3  1.0  0.3
[4,]  0.3  0.3  0.3  1.0

$M15
     [,1] [,2] [,3] [,4]
[1,]  1.0  0.3  0.3  0.3
[2,]  0.3  1.0  0.3  0.3
[3,]  0.3  0.3  1.0  0.3
[4,]  0.3  0.3  0.3  1.0

$M16
     [,1] [,2] [,3] [,4]
[1,]  1.0  0.3  0.3  0.3
[2,]  0.3  1.0  0.3  0.3
[3,]  0.3  0.3  1.0  0.3
[4,]  0.3  0.3  0.3  1.0

$F01
     [,1] [,2] [,3] [,4]
[1,]  1.0  0.3  0.3  0.3
[2,]  0.3  1.0  0.3  0.3
[3,]  0.3  0.3  1.0  0.3
[4,]  0.3  0.3  0.3  1.0

$F02
     [,1] [,2] [,3] [,4]
[1,]  1.0  0.3  0.3  0.3
[2,]  0.3  1.0  0.3  0.3
[3,]  0.3  0.3  1.0  0.3
[4,]  0.3  0.3  0.3  1.0

$F03
     [,1] [,2] [,3] [,4]
[1,]  1.0  0.3  0.3  0.3
[2,]  0.3  1.0  0.3  0.3
[3,]  0.3  0.3  1.0  0.3
[4,]  0.3  0.3  0.3  1.0

$F04
     [,1] [,2] [,3] [,4]
[1,]  1.0  0.3  0.3  0.3
[2,]  0.3  1.0  0.3  0.3
[3,]  0.3  0.3  1.0  0.3
[4,]  0.3  0.3  0.3  1.0

$F05
     [,1] [,2] [,3] [,4]
[1,]  1.0  0.3  0.3  0.3
[2,]  0.3  1.0  0.3  0.3
[3,]  0.3  0.3  1.0  0.3
[4,]  0.3  0.3  0.3  1.0

$F06
     [,1] [,2] [,3] [,4]
[1,]  1.0  0.3  0.3  0.3
[2,]  0.3  1.0  0.3  0.3
[3,]  0.3  0.3  1.0  0.3
[4,]  0.3  0.3  0.3  1.0

$F07
     [,1] [,2] [,3] [,4]
[1,]  1.0  0.3  0.3  0.3
[2,]  0.3  1.0  0.3  0.3
[3,]  0.3  0.3  1.0  0.3
[4,]  0.3  0.3  0.3  1.0

$F08
     [,1] [,2] [,3] [,4]
[1,]  1.0  0.3  0.3  0.3
[2,]  0.3  1.0  0.3  0.3
[3,]  0.3  0.3  1.0  0.3
[4,]  0.3  0.3  0.3  1.0

$F09
     [,1] [,2] [,3] [,4]
[1,]  1.0  0.3  0.3  0.3
[2,]  0.3  1.0  0.3  0.3
[3,]  0.3  0.3  1.0  0.3
[4,]  0.3  0.3  0.3  1.0

$F10
     [,1] [,2] [,3] [,4]
[1,]  1.0  0.3  0.3  0.3
[2,]  0.3  1.0  0.3  0.3
[3,]  0.3  0.3  1.0  0.3
[4,]  0.3  0.3  0.3  1.0

$F11
     [,1] [,2] [,3] [,4]
[1,]  1.0  0.3  0.3  0.3
[2,]  0.3  1.0  0.3  0.3
[3,]  0.3  0.3  1.0  0.3
[4,]  0.3  0.3  0.3  1.0


> cs1Symm <- corSymm(value = c(0.2, 0.1, -0.1, 0, 0.2, 0),
+                    form = ~ 1 | Subject)

> cs1Symm <- Initialize(cs1Symm, data = Orthodont)

> corMatrix(cs1Symm)
$M01
     [,1]     [,2]      [,3]      [,4]
[1,]  1.0 2.00e-01  1.00e-01 -1.00e-01
[2,]  0.2 1.00e+00  9.02e-17  2.00e-01
[3,]  0.1 9.02e-17  1.00e+00 -1.04e-16
[4,] -0.1 2.00e-01 -1.04e-16  1.00e+00

$M02
     [,1]     [,2]      [,3]      [,4]
[1,]  1.0 2.00e-01  1.00e-01 -1.00e-01
[2,]  0.2 1.00e+00  9.02e-17  2.00e-01
[3,]  0.1 9.02e-17  1.00e+00 -1.04e-16
[4,] -0.1 2.00e-01 -1.04e-16  1.00e+00

$M03
     [,1]     [,2]      [,3]      [,4]
[1,]  1.0 2.00e-01  1.00e-01 -1.00e-01
[2,]  0.2 1.00e+00  9.02e-17  2.00e-01
[3,]  0.1 9.02e-17  1.00e+00 -1.04e-16
[4,] -0.1 2.00e-01 -1.04e-16  1.00e+00

$M04
     [,1]     [,2]      [,3]      [,4]
[1,]  1.0 2.00e-01  1.00e-01 -1.00e-01
[2,]  0.2 1.00e+00  9.02e-17  2.00e-01
[3,]  0.1 9.02e-17  1.00e+00 -1.04e-16
[4,] -0.1 2.00e-01 -1.04e-16  1.00e+00

$M05
     [,1]     [,2]      [,3]      [,4]
[1,]  1.0 2.00e-01  1.00e-01 -1.00e-01
[2,]  0.2 1.00e+00  9.02e-17  2.00e-01
[3,]  0.1 9.02e-17  1.00e+00 -1.04e-16
[4,] -0.1 2.00e-01 -1.04e-16  1.00e+00

$M06
     [,1]     [,2]      [,3]      [,4]
[1,]  1.0 2.00e-01  1.00e-01 -1.00e-01
[2,]  0.2 1.00e+00  9.02e-17  2.00e-01
[3,]  0.1 9.02e-17  1.00e+00 -1.04e-16
[4,] -0.1 2.00e-01 -1.04e-16  1.00e+00

$M07
     [,1]     [,2]      [,3]      [,4]
[1,]  1.0 2.00e-01  1.00e-01 -1.00e-01
[2,]  0.2 1.00e+00  9.02e-17  2.00e-01
[3,]  0.1 9.02e-17  1.00e+00 -1.04e-16
[4,] -0.1 2.00e-01 -1.04e-16  1.00e+00

$M08
     [,1]     [,2]      [,3]      [,4]
[1,]  1.0 2.00e-01  1.00e-01 -1.00e-01
[2,]  0.2 1.00e+00  9.02e-17  2.00e-01
[3,]  0.1 9.02e-17  1.00e+00 -1.04e-16
[4,] -0.1 2.00e-01 -1.04e-16  1.00e+00

$M09
     [,1]     [,2]      [,3]      [,4]
[1,]  1.0 2.00e-01  1.00e-01 -1.00e-01
[2,]  0.2 1.00e+00  9.02e-17  2.00e-01
[3,]  0.1 9.02e-17  1.00e+00 -1.04e-16
[4,] -0.1 2.00e-01 -1.04e-16  1.00e+00

$M10
     [,1]     [,2]      [,3]      [,4]
[1,]  1.0 2.00e-01  1.00e-01 -1.00e-01
[2,]  0.2 1.00e+00  9.02e-17  2.00e-01
[3,]  0.1 9.02e-17  1.00e+00 -1.04e-16
[4,] -0.1 2.00e-01 -1.04e-16  1.00e+00

$M11
     [,1]     [,2]      [,3]      [,4]
[1,]  1.0 2.00e-01  1.00e-01 -1.00e-01
[2,]  0.2 1.00e+00  9.02e-17  2.00e-01
[3,]  0.1 9.02e-17  1.00e+00 -1.04e-16
[4,] -0.1 2.00e-01 -1.04e-16  1.00e+00

$M12
     [,1]     [,2]      [,3]      [,4]
[1,]  1.0 2.00e-01  1.00e-01 -1.00e-01
[2,]  0.2 1.00e+00  9.02e-17  2.00e-01
[3,]  0.1 9.02e-17  1.00e+00 -1.04e-16
[4,] -0.1 2.00e-01 -1.04e-16  1.00e+00

$M13
     [,1]     [,2]      [,3]      [,4]
[1,]  1.0 2.00e-01  1.00e-01 -1.00e-01
[2,]  0.2 1.00e+00  9.02e-17  2.00e-01
[3,]  0.1 9.02e-17  1.00e+00 -1.04e-16
[4,] -0.1 2.00e-01 -1.04e-16  1.00e+00

$M14
     [,1]     [,2]      [,3]      [,4]
[1,]  1.0 2.00e-01  1.00e-01 -1.00e-01
[2,]  0.2 1.00e+00  9.02e-17  2.00e-01
[3,]  0.1 9.02e-17  1.00e+00 -1.04e-16
[4,] -0.1 2.00e-01 -1.04e-16  1.00e+00

$M15
     [,1]     [,2]      [,3]      [,4]
[1,]  1.0 2.00e-01  1.00e-01 -1.00e-01
[2,]  0.2 1.00e+00  9.02e-17  2.00e-01
[3,]  0.1 9.02e-17  1.00e+00 -1.04e-16
[4,] -0.1 2.00e-01 -1.04e-16  1.00e+00

$M16
     [,1]     [,2]      [,3]      [,4]
[1,]  1.0 2.00e-01  1.00e-01 -1.00e-01
[2,]  0.2 1.00e+00  9.02e-17  2.00e-01
[3,]  0.1 9.02e-17  1.00e+00 -1.04e-16
[4,] -0.1 2.00e-01 -1.04e-16  1.00e+00

$F01
     [,1]     [,2]      [,3]      [,4]
[1,]  1.0 2.00e-01  1.00e-01 -1.00e-01
[2,]  0.2 1.00e+00  9.02e-17  2.00e-01
[3,]  0.1 9.02e-17  1.00e+00 -1.04e-16
[4,] -0.1 2.00e-01 -1.04e-16  1.00e+00

$F02
     [,1]     [,2]      [,3]      [,4]
[1,]  1.0 2.00e-01  1.00e-01 -1.00e-01
[2,]  0.2 1.00e+00  9.02e-17  2.00e-01
[3,]  0.1 9.02e-17  1.00e+00 -1.04e-16
[4,] -0.1 2.00e-01 -1.04e-16  1.00e+00

$F03
     [,1]     [,2]      [,3]      [,4]
[1,]  1.0 2.00e-01  1.00e-01 -1.00e-01
[2,]  0.2 1.00e+00  9.02e-17  2.00e-01
[3,]  0.1 9.02e-17  1.00e+00 -1.04e-16
[4,] -0.1 2.00e-01 -1.04e-16  1.00e+00

$F04
     [,1]     [,2]      [,3]      [,4]
[1,]  1.0 2.00e-01  1.00e-01 -1.00e-01
[2,]  0.2 1.00e+00  9.02e-17  2.00e-01
[3,]  0.1 9.02e-17  1.00e+00 -1.04e-16
[4,] -0.1 2.00e-01 -1.04e-16  1.00e+00

$F05
     [,1]     [,2]      [,3]      [,4]
[1,]  1.0 2.00e-01  1.00e-01 -1.00e-01
[2,]  0.2 1.00e+00  9.02e-17  2.00e-01
[3,]  0.1 9.02e-17  1.00e+00 -1.04e-16
[4,] -0.1 2.00e-01 -1.04e-16  1.00e+00

$F06
     [,1]     [,2]      [,3]      [,4]
[1,]  1.0 2.00e-01  1.00e-01 -1.00e-01
[2,]  0.2 1.00e+00  9.02e-17  2.00e-01
[3,]  0.1 9.02e-17  1.00e+00 -1.04e-16
[4,] -0.1 2.00e-01 -1.04e-16  1.00e+00

$F07
     [,1]     [,2]      [,3]      [,4]
[1,]  1.0 2.00e-01  1.00e-01 -1.00e-01
[2,]  0.2 1.00e+00  9.02e-17  2.00e-01
[3,]  0.1 9.02e-17  1.00e+00 -1.04e-16
[4,] -0.1 2.00e-01 -1.04e-16  1.00e+00

$F08
     [,1]     [,2]      [,3]      [,4]
[1,]  1.0 2.00e-01  1.00e-01 -1.00e-01
[2,]  0.2 1.00e+00  9.02e-17  2.00e-01
[3,]  0.1 9.02e-17  1.00e+00 -1.04e-16
[4,] -0.1 2.00e-01 -1.04e-16  1.00e+00

$F09
     [,1]     [,2]      [,3]      [,4]
[1,]  1.0 2.00e-01  1.00e-01 -1.00e-01
[2,]  0.2 1.00e+00  9.02e-17  2.00e-01
[3,]  0.1 9.02e-17  1.00e+00 -1.04e-16
[4,] -0.1 2.00e-01 -1.04e-16  1.00e+00

$F10
     [,1]     [,2]      [,3]      [,4]
[1,]  1.0 2.00e-01  1.00e-01 -1.00e-01
[2,]  0.2 1.00e+00  9.02e-17  2.00e-01
[3,]  0.1 9.02e-17  1.00e+00 -1.04e-16
[4,] -0.1 2.00e-01 -1.04e-16  1.00e+00

$F11
     [,1]     [,2]      [,3]      [,4]
[1,]  1.0 2.00e-01  1.00e-01 -1.00e-01
[2,]  0.2 1.00e+00  9.02e-17  2.00e-01
[3,]  0.1 9.02e-17  1.00e+00 -1.04e-16
[4,] -0.1 2.00e-01 -1.04e-16  1.00e+00


> cs1AR1 <- corAR1(0.8, form = ~ 1 | Subject)

> cs1AR1 <- Initialize(cs1AR1, data = Orthodont)

> corMatrix(cs1AR1)
$M01
      [,1] [,2] [,3]  [,4]
[1,] 1.000 0.80 0.64 0.512
[2,] 0.800 1.00 0.80 0.640
[3,] 0.640 0.80 1.00 0.800
[4,] 0.512 0.64 0.80 1.000

$M02
      [,1] [,2] [,3]  [,4]
[1,] 1.000 0.80 0.64 0.512
[2,] 0.800 1.00 0.80 0.640
[3,] 0.640 0.80 1.00 0.800
[4,] 0.512 0.64 0.80 1.000

$M03
      [,1] [,2] [,3]  [,4]
[1,] 1.000 0.80 0.64 0.512
[2,] 0.800 1.00 0.80 0.640
[3,] 0.640 0.80 1.00 0.800
[4,] 0.512 0.64 0.80 1.000

$M04
      [,1] [,2] [,3]  [,4]
[1,] 1.000 0.80 0.64 0.512
[2,] 0.800 1.00 0.80 0.640
[3,] 0.640 0.80 1.00 0.800
[4,] 0.512 0.64 0.80 1.000

$M05
      [,1] [,2] [,3]  [,4]
[1,] 1.000 0.80 0.64 0.512
[2,] 0.800 1.00 0.80 0.640
[3,] 0.640 0.80 1.00 0.800
[4,] 0.512 0.64 0.80 1.000

$M06
      [,1] [,2] [,3]  [,4]
[1,] 1.000 0.80 0.64 0.512
[2,] 0.800 1.00 0.80 0.640
[3,] 0.640 0.80 1.00 0.800
[4,] 0.512 0.64 0.80 1.000

$M07
      [,1] [,2] [,3]  [,4]
[1,] 1.000 0.80 0.64 0.512
[2,] 0.800 1.00 0.80 0.640
[3,] 0.640 0.80 1.00 0.800
[4,] 0.512 0.64 0.80 1.000

$M08
      [,1] [,2] [,3]  [,4]
[1,] 1.000 0.80 0.64 0.512
[2,] 0.800 1.00 0.80 0.640
[3,] 0.640 0.80 1.00 0.800
[4,] 0.512 0.64 0.80 1.000

$M09
      [,1] [,2] [,3]  [,4]
[1,] 1.000 0.80 0.64 0.512
[2,] 0.800 1.00 0.80 0.640
[3,] 0.640 0.80 1.00 0.800
[4,] 0.512 0.64 0.80 1.000

$M10
      [,1] [,2] [,3]  [,4]
[1,] 1.000 0.80 0.64 0.512
[2,] 0.800 1.00 0.80 0.640
[3,] 0.640 0.80 1.00 0.800
[4,] 0.512 0.64 0.80 1.000

$M11
      [,1] [,2] [,3]  [,4]
[1,] 1.000 0.80 0.64 0.512
[2,] 0.800 1.00 0.80 0.640
[3,] 0.640 0.80 1.00 0.800
[4,] 0.512 0.64 0.80 1.000

$M12
      [,1] [,2] [,3]  [,4]
[1,] 1.000 0.80 0.64 0.512
[2,] 0.800 1.00 0.80 0.640
[3,] 0.640 0.80 1.00 0.800
[4,] 0.512 0.64 0.80 1.000

$M13
      [,1] [,2] [,3]  [,4]
[1,] 1.000 0.80 0.64 0.512
[2,] 0.800 1.00 0.80 0.640
[3,] 0.640 0.80 1.00 0.800
[4,] 0.512 0.64 0.80 1.000

$M14
      [,1] [,2] [,3]  [,4]
[1,] 1.000 0.80 0.64 0.512
[2,] 0.800 1.00 0.80 0.640
[3,] 0.640 0.80 1.00 0.800
[4,] 0.512 0.64 0.80 1.000

$M15
      [,1] [,2] [,3]  [,4]
[1,] 1.000 0.80 0.64 0.512
[2,] 0.800 1.00 0.80 0.640
[3,] 0.640 0.80 1.00 0.800
[4,] 0.512 0.64 0.80 1.000

$M16
      [,1] [,2] [,3]  [,4]
[1,] 1.000 0.80 0.64 0.512
[2,] 0.800 1.00 0.80 0.640
[3,] 0.640 0.80 1.00 0.800
[4,] 0.512 0.64 0.80 1.000

$F01
      [,1] [,2] [,3]  [,4]
[1,] 1.000 0.80 0.64 0.512
[2,] 0.800 1.00 0.80 0.640
[3,] 0.640 0.80 1.00 0.800
[4,] 0.512 0.64 0.80 1.000

$F02
      [,1] [,2] [,3]  [,4]
[1,] 1.000 0.80 0.64 0.512
[2,] 0.800 1.00 0.80 0.640
[3,] 0.640 0.80 1.00 0.800
[4,] 0.512 0.64 0.80 1.000

$F03
      [,1] [,2] [,3]  [,4]
[1,] 1.000 0.80 0.64 0.512
[2,] 0.800 1.00 0.80 0.640
[3,] 0.640 0.80 1.00 0.800
[4,] 0.512 0.64 0.80 1.000

$F04
      [,1] [,2] [,3]  [,4]
[1,] 1.000 0.80 0.64 0.512
[2,] 0.800 1.00 0.80 0.640
[3,] 0.640 0.80 1.00 0.800
[4,] 0.512 0.64 0.80 1.000

$F05
      [,1] [,2] [,3]  [,4]
[1,] 1.000 0.80 0.64 0.512
[2,] 0.800 1.00 0.80 0.640
[3,] 0.640 0.80 1.00 0.800
[4,] 0.512 0.64 0.80 1.000

$F06
      [,1] [,2] [,3]  [,4]
[1,] 1.000 0.80 0.64 0.512
[2,] 0.800 1.00 0.80 0.640
[3,] 0.640 0.80 1.00 0.800
[4,] 0.512 0.64 0.80 1.000

$F07
      [,1] [,2] [,3]  [,4]
[1,] 1.000 0.80 0.64 0.512
[2,] 0.800 1.00 0.80 0.640
[3,] 0.640 0.80 1.00 0.800
[4,] 0.512 0.64 0.80 1.000

$F08
      [,1] [,2] [,3]  [,4]
[1,] 1.000 0.80 0.64 0.512
[2,] 0.800 1.00 0.80 0.640
[3,] 0.640 0.80 1.00 0.800
[4,] 0.512 0.64 0.80 1.000

$F09
      [,1] [,2] [,3]  [,4]
[1,] 1.000 0.80 0.64 0.512
[2,] 0.800 1.00 0.80 0.640
[3,] 0.640 0.80 1.00 0.800
[4,] 0.512 0.64 0.80 1.000

$F10
      [,1] [,2] [,3]  [,4]
[1,] 1.000 0.80 0.64 0.512
[2,] 0.800 1.00 0.80 0.640
[3,] 0.640 0.80 1.00 0.800
[4,] 0.512 0.64 0.80 1.000

$F11
      [,1] [,2] [,3]  [,4]
[1,] 1.000 0.80 0.64 0.512
[2,] 0.800 1.00 0.80 0.640
[3,] 0.640 0.80 1.00 0.800
[4,] 0.512 0.64 0.80 1.000


> cs1ARMA <- corARMA(0.4, form = ~ 1 | Subject, q = 1)

> cs1ARMA <- Initialize(cs1ARMA, data = Orthodont)

> corMatrix(cs1ARMA)
$M01
      [,1]  [,2]  [,3]  [,4]
[1,] 1.000 0.345 0.000 0.000
[2,] 0.345 1.000 0.345 0.000
[3,] 0.000 0.345 1.000 0.345
[4,] 0.000 0.000 0.345 1.000

$M02
      [,1]  [,2]  [,3]  [,4]
[1,] 1.000 0.345 0.000 0.000
[2,] 0.345 1.000 0.345 0.000
[3,] 0.000 0.345 1.000 0.345
[4,] 0.000 0.000 0.345 1.000

$M03
      [,1]  [,2]  [,3]  [,4]
[1,] 1.000 0.345 0.000 0.000
[2,] 0.345 1.000 0.345 0.000
[3,] 0.000 0.345 1.000 0.345
[4,] 0.000 0.000 0.345 1.000

$M04
      [,1]  [,2]  [,3]  [,4]
[1,] 1.000 0.345 0.000 0.000
[2,] 0.345 1.000 0.345 0.000
[3,] 0.000 0.345 1.000 0.345
[4,] 0.000 0.000 0.345 1.000

$M05
      [,1]  [,2]  [,3]  [,4]
[1,] 1.000 0.345 0.000 0.000
[2,] 0.345 1.000 0.345 0.000
[3,] 0.000 0.345 1.000 0.345
[4,] 0.000 0.000 0.345 1.000

$M06
      [,1]  [,2]  [,3]  [,4]
[1,] 1.000 0.345 0.000 0.000
[2,] 0.345 1.000 0.345 0.000
[3,] 0.000 0.345 1.000 0.345
[4,] 0.000 0.000 0.345 1.000

$M07
      [,1]  [,2]  [,3]  [,4]
[1,] 1.000 0.345 0.000 0.000
[2,] 0.345 1.000 0.345 0.000
[3,] 0.000 0.345 1.000 0.345
[4,] 0.000 0.000 0.345 1.000

$M08
      [,1]  [,2]  [,3]  [,4]
[1,] 1.000 0.345 0.000 0.000
[2,] 0.345 1.000 0.345 0.000
[3,] 0.000 0.345 1.000 0.345
[4,] 0.000 0.000 0.345 1.000

$M09
      [,1]  [,2]  [,3]  [,4]
[1,] 1.000 0.345 0.000 0.000
[2,] 0.345 1.000 0.345 0.000
[3,] 0.000 0.345 1.000 0.345
[4,] 0.000 0.000 0.345 1.000

$M10
      [,1]  [,2]  [,3]  [,4]
[1,] 1.000 0.345 0.000 0.000
[2,] 0.345 1.000 0.345 0.000
[3,] 0.000 0.345 1.000 0.345
[4,] 0.000 0.000 0.345 1.000

$M11
      [,1]  [,2]  [,3]  [,4]
[1,] 1.000 0.345 0.000 0.000
[2,] 0.345 1.000 0.345 0.000
[3,] 0.000 0.345 1.000 0.345
[4,] 0.000 0.000 0.345 1.000

$M12
      [,1]  [,2]  [,3]  [,4]
[1,] 1.000 0.345 0.000 0.000
[2,] 0.345 1.000 0.345 0.000
[3,] 0.000 0.345 1.000 0.345
[4,] 0.000 0.000 0.345 1.000

$M13
      [,1]  [,2]  [,3]  [,4]
[1,] 1.000 0.345 0.000 0.000
[2,] 0.345 1.000 0.345 0.000
[3,] 0.000 0.345 1.000 0.345
[4,] 0.000 0.000 0.345 1.000

$M14
      [,1]  [,2]  [,3]  [,4]
[1,] 1.000 0.345 0.000 0.000
[2,] 0.345 1.000 0.345 0.000
[3,] 0.000 0.345 1.000 0.345
[4,] 0.000 0.000 0.345 1.000

$M15
      [,1]  [,2]  [,3]  [,4]
[1,] 1.000 0.345 0.000 0.000
[2,] 0.345 1.000 0.345 0.000
[3,] 0.000 0.345 1.000 0.345
[4,] 0.000 0.000 0.345 1.000

$M16
      [,1]  [,2]  [,3]  [,4]
[1,] 1.000 0.345 0.000 0.000
[2,] 0.345 1.000 0.345 0.000
[3,] 0.000 0.345 1.000 0.345
[4,] 0.000 0.000 0.345 1.000

$F01
      [,1]  [,2]  [,3]  [,4]
[1,] 1.000 0.345 0.000 0.000
[2,] 0.345 1.000 0.345 0.000
[3,] 0.000 0.345 1.000 0.345
[4,] 0.000 0.000 0.345 1.000

$F02
      [,1]  [,2]  [,3]  [,4]
[1,] 1.000 0.345 0.000 0.000
[2,] 0.345 1.000 0.345 0.000
[3,] 0.000 0.345 1.000 0.345
[4,] 0.000 0.000 0.345 1.000

$F03
      [,1]  [,2]  [,3]  [,4]
[1,] 1.000 0.345 0.000 0.000
[2,] 0.345 1.000 0.345 0.000
[3,] 0.000 0.345 1.000 0.345
[4,] 0.000 0.000 0.345 1.000

$F04
      [,1]  [,2]  [,3]  [,4]
[1,] 1.000 0.345 0.000 0.000
[2,] 0.345 1.000 0.345 0.000
[3,] 0.000 0.345 1.000 0.345
[4,] 0.000 0.000 0.345 1.000

$F05
      [,1]  [,2]  [,3]  [,4]
[1,] 1.000 0.345 0.000 0.000
[2,] 0.345 1.000 0.345 0.000
[3,] 0.000 0.345 1.000 0.345
[4,] 0.000 0.000 0.345 1.000

$F06
      [,1]  [,2]  [,3]  [,4]
[1,] 1.000 0.345 0.000 0.000
[2,] 0.345 1.000 0.345 0.000
[3,] 0.000 0.345 1.000 0.345
[4,] 0.000 0.000 0.345 1.000

$F07
      [,1]  [,2]  [,3]  [,4]
[1,] 1.000 0.345 0.000 0.000
[2,] 0.345 1.000 0.345 0.000
[3,] 0.000 0.345 1.000 0.345
[4,] 0.000 0.000 0.345 1.000

$F08
      [,1]  [,2]  [,3]  [,4]
[1,] 1.000 0.345 0.000 0.000
[2,] 0.345 1.000 0.345 0.000
[3,] 0.000 0.345 1.000 0.345
[4,] 0.000 0.000 0.345 1.000

$F09
      [,1]  [,2]  [,3]  [,4]
[1,] 1.000 0.345 0.000 0.000
[2,] 0.345 1.000 0.345 0.000
[3,] 0.000 0.345 1.000 0.345
[4,] 0.000 0.000 0.345 1.000

$F10
      [,1]  [,2]  [,3]  [,4]
[1,] 1.000 0.345 0.000 0.000
[2,] 0.345 1.000 0.345 0.000
[3,] 0.000 0.345 1.000 0.345
[4,] 0.000 0.000 0.345 1.000

$F11
      [,1]  [,2]  [,3]  [,4]
[1,] 1.000 0.345 0.000 0.000
[2,] 0.345 1.000 0.345 0.000
[3,] 0.000 0.345 1.000 0.345
[4,] 0.000 0.000 0.345 1.000


> cs2ARMA <- corARMA(c(0.8, 0.4), form = ~ 1 | Subject, p=1, q=1)

> cs2ARMA <- Initialize(cs2ARMA, data = Orthodont)

> corMatrix(cs2ARMA)
$M01
      [,1]  [,2]  [,3]  [,4]
[1,] 1.000 0.880 0.704 0.563
[2,] 0.880 1.000 0.880 0.704
[3,] 0.704 0.880 1.000 0.880
[4,] 0.563 0.704 0.880 1.000

$M02
      [,1]  [,2]  [,3]  [,4]
[1,] 1.000 0.880 0.704 0.563
[2,] 0.880 1.000 0.880 0.704
[3,] 0.704 0.880 1.000 0.880
[4,] 0.563 0.704 0.880 1.000

$M03
      [,1]  [,2]  [,3]  [,4]
[1,] 1.000 0.880 0.704 0.563
[2,] 0.880 1.000 0.880 0.704
[3,] 0.704 0.880 1.000 0.880
[4,] 0.563 0.704 0.880 1.000

$M04
      [,1]  [,2]  [,3]  [,4]
[1,] 1.000 0.880 0.704 0.563
[2,] 0.880 1.000 0.880 0.704
[3,] 0.704 0.880 1.000 0.880
[4,] 0.563 0.704 0.880 1.000

$M05
      [,1]  [,2]  [,3]  [,4]
[1,] 1.000 0.880 0.704 0.563
[2,] 0.880 1.000 0.880 0.704
[3,] 0.704 0.880 1.000 0.880
[4,] 0.563 0.704 0.880 1.000

$M06
      [,1]  [,2]  [,3]  [,4]
[1,] 1.000 0.880 0.704 0.563
[2,] 0.880 1.000 0.880 0.704
[3,] 0.704 0.880 1.000 0.880
[4,] 0.563 0.704 0.880 1.000

$M07
      [,1]  [,2]  [,3]  [,4]
[1,] 1.000 0.880 0.704 0.563
[2,] 0.880 1.000 0.880 0.704
[3,] 0.704 0.880 1.000 0.880
[4,] 0.563 0.704 0.880 1.000

$M08
      [,1]  [,2]  [,3]  [,4]
[1,] 1.000 0.880 0.704 0.563
[2,] 0.880 1.000 0.880 0.704
[3,] 0.704 0.880 1.000 0.880
[4,] 0.563 0.704 0.880 1.000

$M09
      [,1]  [,2]  [,3]  [,4]
[1,] 1.000 0.880 0.704 0.563
[2,] 0.880 1.000 0.880 0.704
[3,] 0.704 0.880 1.000 0.880
[4,] 0.563 0.704 0.880 1.000

$M10
      [,1]  [,2]  [,3]  [,4]
[1,] 1.000 0.880 0.704 0.563
[2,] 0.880 1.000 0.880 0.704
[3,] 0.704 0.880 1.000 0.880
[4,] 0.563 0.704 0.880 1.000

$M11
      [,1]  [,2]  [,3]  [,4]
[1,] 1.000 0.880 0.704 0.563
[2,] 0.880 1.000 0.880 0.704
[3,] 0.704 0.880 1.000 0.880
[4,] 0.563 0.704 0.880 1.000

$M12
      [,1]  [,2]  [,3]  [,4]
[1,] 1.000 0.880 0.704 0.563
[2,] 0.880 1.000 0.880 0.704
[3,] 0.704 0.880 1.000 0.880
[4,] 0.563 0.704 0.880 1.000

$M13
      [,1]  [,2]  [,3]  [,4]
[1,] 1.000 0.880 0.704 0.563
[2,] 0.880 1.000 0.880 0.704
[3,] 0.704 0.880 1.000 0.880
[4,] 0.563 0.704 0.880 1.000

$M14
      [,1]  [,2]  [,3]  [,4]
[1,] 1.000 0.880 0.704 0.563
[2,] 0.880 1.000 0.880 0.704
[3,] 0.704 0.880 1.000 0.880
[4,] 0.563 0.704 0.880 1.000

$M15
      [,1]  [,2]  [,3]  [,4]
[1,] 1.000 0.880 0.704 0.563
[2,] 0.880 1.000 0.880 0.704
[3,] 0.704 0.880 1.000 0.880
[4,] 0.563 0.704 0.880 1.000

$M16
      [,1]  [,2]  [,3]  [,4]
[1,] 1.000 0.880 0.704 0.563
[2,] 0.880 1.000 0.880 0.704
[3,] 0.704 0.880 1.000 0.880
[4,] 0.563 0.704 0.880 1.000

$F01
      [,1]  [,2]  [,3]  [,4]
[1,] 1.000 0.880 0.704 0.563
[2,] 0.880 1.000 0.880 0.704
[3,] 0.704 0.880 1.000 0.880
[4,] 0.563 0.704 0.880 1.000

$F02
      [,1]  [,2]  [,3]  [,4]
[1,] 1.000 0.880 0.704 0.563
[2,] 0.880 1.000 0.880 0.704
[3,] 0.704 0.880 1.000 0.880
[4,] 0.563 0.704 0.880 1.000

$F03
      [,1]  [,2]  [,3]  [,4]
[1,] 1.000 0.880 0.704 0.563
[2,] 0.880 1.000 0.880 0.704
[3,] 0.704 0.880 1.000 0.880
[4,] 0.563 0.704 0.880 1.000

$F04
      [,1]  [,2]  [,3]  [,4]
[1,] 1.000 0.880 0.704 0.563
[2,] 0.880 1.000 0.880 0.704
[3,] 0.704 0.880 1.000 0.880
[4,] 0.563 0.704 0.880 1.000

$F05
      [,1]  [,2]  [,3]  [,4]
[1,] 1.000 0.880 0.704 0.563
[2,] 0.880 1.000 0.880 0.704
[3,] 0.704 0.880 1.000 0.880
[4,] 0.563 0.704 0.880 1.000

$F06
      [,1]  [,2]  [,3]  [,4]
[1,] 1.000 0.880 0.704 0.563
[2,] 0.880 1.000 0.880 0.704
[3,] 0.704 0.880 1.000 0.880
[4,] 0.563 0.704 0.880 1.000

$F07
      [,1]  [,2]  [,3]  [,4]
[1,] 1.000 0.880 0.704 0.563
[2,] 0.880 1.000 0.880 0.704
[3,] 0.704 0.880 1.000 0.880
[4,] 0.563 0.704 0.880 1.000

$F08
      [,1]  [,2]  [,3]  [,4]
[1,] 1.000 0.880 0.704 0.563
[2,] 0.880 1.000 0.880 0.704
[3,] 0.704 0.880 1.000 0.880
[4,] 0.563 0.704 0.880 1.000

$F09
      [,1]  [,2]  [,3]  [,4]
[1,] 1.000 0.880 0.704 0.563
[2,] 0.880 1.000 0.880 0.704
[3,] 0.704 0.880 1.000 0.880
[4,] 0.563 0.704 0.880 1.000

$F10
      [,1]  [,2]  [,3]  [,4]
[1,] 1.000 0.880 0.704 0.563
[2,] 0.880 1.000 0.880 0.704
[3,] 0.704 0.880 1.000 0.880
[4,] 0.563 0.704 0.880 1.000

$F11
      [,1]  [,2]  [,3]  [,4]
[1,] 1.000 0.880 0.704 0.563
[2,] 0.880 1.000 0.880 0.704
[3,] 0.704 0.880 1.000 0.880
[4,] 0.563 0.704 0.880 1.000


> spatDat <- data.frame(x = (0:4)/4, y = (0:4)/4)

> cs1Exp <- corExp(1, form = ~ x + y)

> cs1Exp <- Initialize(cs1Exp, spatDat)

> corMatrix(cs1Exp)
      [,1]  [,2]  [,3]  [,4]  [,5]
[1,] 1.000 0.702 0.493 0.346 0.243
[2,] 0.702 1.000 0.702 0.493 0.346
[3,] 0.493 0.702 1.000 0.702 0.493
[4,] 0.346 0.493 0.702 1.000 0.702
[5,] 0.243 0.346 0.493 0.702 1.000

> cs2Exp <- corExp(1, form = ~ x + y, metric = "man")

> cs2Exp <- Initialize(cs2Exp, spatDat)

> corMatrix(cs2Exp)
      [,1]  [,2]  [,3]  [,4]  [,5]
[1,] 1.000 0.607 0.368 0.223 0.135
[2,] 0.607 1.000 0.607 0.368 0.223
[3,] 0.368 0.607 1.000 0.607 0.368
[4,] 0.223 0.368 0.607 1.000 0.607
[5,] 0.135 0.223 0.368 0.607 1.000

> cs3Exp <- corExp(c(1, 0.2), form = ~ x + y, nugget = TRUE)

> cs3Exp <- Initialize(cs3Exp, spatDat)

> corMatrix(cs3Exp)
      [,1]  [,2]  [,3]  [,4]  [,5]
[1,] 1.000 0.562 0.394 0.277 0.194
[2,] 0.562 1.000 0.562 0.394 0.277
[3,] 0.394 0.562 1.000 0.562 0.394
[4,] 0.277 0.394 0.562 1.000 0.562
[5,] 0.194 0.277 0.394 0.562 1.000

> fm1Ovar.lme <- lme(follicles ~ sin(2*pi*Time) + cos(2*pi*Time),
+                    data = Ovary, random = pdDiag(~sin(2*pi*Time)))

> fm1Ovar.lme
Linear mixed-effects model fit by REML
  Data: Ovary 
  Log-restricted-likelihood: -813
  Fixed: follicles ~ sin(2 * pi * Time) + cos(2 * pi * Time) 
       (Intercept) sin(2 * pi * Time) cos(2 * pi * Time) 
            12.182             -3.299             -0.862 

Random effects:
 Formula: ~sin(2 * pi * Time) | Mare
 Structure: Diagonal
        (Intercept) sin(2 * pi * Time) Residual
StdDev:        3.05               2.08     3.11

Number of Observations: 308
Number of Groups: 11 

> ACF(fm1Ovar.lme)
   lag     ACF
1    0  1.0000
2    1  0.3795
3    2  0.1797
4    3  0.0357
5    4  0.0598
6    5  0.0021
7    6  0.0643
8    7  0.0716
9    8  0.0486
10   9  0.0278
11  10 -0.0343
12  11 -0.0772
13  12 -0.1611
14  13 -0.1960
15  14 -0.2893

> plot(ACF(fm1Ovar.lme,  maxLag = 10), alpha = 0.01)

> fm2Ovar.lme <- update(fm1Ovar.lme, correlation = corAR1())

> anova(fm1Ovar.lme, fm2Ovar.lme)
            Model df  AIC  BIC logLik   Test L.Ratio p-value
fm1Ovar.lme     1  6 1638 1660   -813                       
fm2Ovar.lme     2  7 1563 1589   -775 1 vs 2    76.6  <.0001

> if (interactive()) intervals(fm2Ovar.lme)

> fm3Ovar.lme <- update(fm1Ovar.lme, correlation = corARMA(q = 2))

> fm3Ovar.lme
Linear mixed-effects model fit by REML
  Data: Ovary 
  Log-restricted-likelihood: -778
  Fixed: follicles ~ sin(2 * pi * Time) + cos(2 * pi * Time) 
       (Intercept) sin(2 * pi * Time) cos(2 * pi * Time) 
            12.194             -3.115             -0.869 

Random effects:
 Formula: ~sin(2 * pi * Time) | Mare
 Structure: Diagonal
        (Intercept) sin(2 * pi * Time) Residual
StdDev:        2.97               1.67     3.24

Correlation Structure: ARMA(0,2)
 Formula: ~1 | Mare 
 Parameter estimate(s):
Theta1 Theta2 
 0.475  0.257 
Number of Observations: 308
Number of Groups: 11 

> anova(fm2Ovar.lme, fm3Ovar.lme, test = F)
            Model df  AIC  BIC logLik
fm2Ovar.lme     1  7 1563 1589   -775
fm3Ovar.lme     2  8 1571 1601   -778

> fm4Ovar.lme <- update(fm1Ovar.lme,
+                        correlation = corCAR1(form = ~Time))

> anova(fm2Ovar.lme, fm4Ovar.lme, test = F)
            Model df  AIC  BIC logLik
fm2Ovar.lme     1  7 1563 1589   -775
fm4Ovar.lme     2  7 1566 1592   -776

> (fm5Ovar.lme <- update(fm1Ovar.lme,
+                        corr = corARMA(p = 1, q = 1)))
Linear mixed-effects model fit by REML
  Data: Ovary 
  Log-restricted-likelihood: -772
  Fixed: follicles ~ sin(2 * pi * Time) + cos(2 * pi * Time) 
       (Intercept) sin(2 * pi * Time) cos(2 * pi * Time) 
            12.125             -2.920             -0.849 

Random effects:
 Formula: ~sin(2 * pi * Time) | Mare
 Structure: Diagonal
        (Intercept) sin(2 * pi * Time) Residual
StdDev:        2.61                  1     3.73

Correlation Structure: ARMA(1,1)
 Formula: ~1 | Mare 
 Parameter estimate(s):
  Phi1 Theta1 
 0.787 -0.279 
Number of Observations: 308
Number of Groups: 11 

> anova(fm2Ovar.lme, fm5Ovar.lme)
            Model df  AIC  BIC logLik   Test L.Ratio p-value
fm2Ovar.lme     1  7 1563 1589   -775                       
fm5Ovar.lme     2  8 1560 1590   -772 1 vs 2    5.55  0.0184

> plot(ACF(fm5Ovar.lme,  maxLag = 10, resType = "n"), alpha = 0.01)

> Variogram(fm2BW.lme, form = ~ Time)
   variog dist n.pairs
1   0.345    1      16
2   0.993    6      16
3   0.762    7     144
4   0.685    8      16
5   0.682   13      16
6   0.951   14     128
7   0.900   15      16
8   1.694   20      16
9   1.125   21     112
10  1.088   22      16
11  0.897   28      96
12  0.932   29      16
13  0.851   35      80
14  0.755   36      16
15  1.082   42      64
16  1.567   43      16
17  0.644   49      48
18  0.674   56      32
19  0.587   63      16

> plot(Variogram(fm2BW.lme, form = ~ Time, maxDist = 42))

> fm3BW.lme <- update(fm2BW.lme,
+                     correlation = corExp(form = ~ Time))

> ## IGNORE_RDIFF_BEGIN
> intervals(fm3BW.lme)
Approximate 95% confidence intervals

 Fixed effects:
               lower    est.   upper
(Intercept) 2.26e+02 251.487 277.336
Time        1.93e-01   0.363   0.532
Diet2       1.52e+02 200.786 249.841
Diet3       2.04e+02 252.590 301.667
Time:Diet2  3.22e-01   0.624   0.926
Time:Diet3  2.63e-03   0.307   0.610

 Random Effects:
  Level: Rat 
                       lower   est.  upper
sd((Intercept))       25.023 36.919 54.471
sd(Time)               0.147  0.233  0.368
cor((Intercept),Time) -0.637 -0.147  0.428

 Correlation structure:
      lower est. upper
range  2.46 4.89   9.7

 Variance function:
      lower  est. upper
power 0.244 0.594 0.944

 Within-group standard error:
 lower   est.  upper 
0.0181 0.1384 1.0593 

> ## IGNORE_RDIFF_END
> anova(fm2BW.lme, fm3BW.lme)
          Model df  AIC  BIC logLik   Test L.Ratio p-value
fm2BW.lme     1 11 1164 1198   -571                       
fm3BW.lme     2 12 1145 1183   -561 1 vs 2    20.8  <.0001

> fm4BW.lme <-
+       update(fm3BW.lme, correlation = corExp(form =  ~ Time,
+                         nugget = TRUE))

> anova(fm3BW.lme, fm4BW.lme)
          Model df  AIC  BIC logLik   Test L.Ratio p-value
fm3BW.lme     1 12 1145 1183   -561                       
fm4BW.lme     2 13 1138 1178   -556 1 vs 2     9.5  0.0021

> plot(Variogram(fm3BW.lme, form = ~ Time, maxDist = 42))

> plot(Variogram(fm3BW.lme, form = ~ Time, maxDist = 42,
+                resType = "n", robust = TRUE))

> fm5BW.lme <- update(fm3BW.lme, correlation = corRatio(form = ~ Time))

> fm6BW.lme <- update(fm3BW.lme, correlation = corSpher(form = ~ Time))

> fm7BW.lme <- update(fm3BW.lme, correlation = corLin(form = ~ Time))

> fm8BW.lme <- update(fm3BW.lme, correlation = corGaus(form = ~ Time))

> anova(fm3BW.lme, fm5BW.lme, fm6BW.lme, fm7BW.lme, fm8BW.lme)
          Model df  AIC  BIC logLik
fm3BW.lme     1 12 1145 1183   -561
fm5BW.lme     2 12 1149 1186   -562
fm6BW.lme     3 12 1151 1188   -563
fm7BW.lme     4 12 1151 1188   -563
fm8BW.lme     5 12 1151 1188   -563

> fm1Orth.gls <- gls(distance ~ Sex * I(age - 11), Orthodont,
+                    correlation = corSymm(form = ~ 1 | Subject),
+                    weights = varIdent(form = ~ 1 | age))

> fm1Orth.gls
Generalized least squares fit by REML
  Model: distance ~ Sex * I(age - 11) 
  Data: Orthodont 
  Log-restricted-likelihood: -212

Coefficients:
          (Intercept)             SexFemale 
               24.937                -2.272 
          I(age - 11) SexFemale:I(age - 11) 
                0.827                -0.350 

Correlation Structure: General
 Formula: ~1 | Subject 
 Parameter estimate(s):
 Correlation: 
  1     2     3    
2 0.568            
3 0.659 0.581      
4 0.522 0.725 0.740
Variance function:
 Structure: Different standard deviations per stratum
 Formula: ~1 | age 
 Parameter estimates:
    8    10    12    14 
1.000 0.879 1.074 0.959 
Degrees of freedom: 108 total; 104 residual
Residual standard error: 2.33 

> ## IGNORE_RDIFF_BEGIN
> intervals(fm1Orth.gls)
Approximate 95% confidence intervals

 Coefficients:
                       lower   est.  upper
(Intercept)           23.999 24.937 25.875
SexFemale             -3.741 -2.272 -0.803
I(age - 11)            0.664  0.827  0.990
SexFemale:I(age - 11) -0.606 -0.350 -0.095

 Correlation structure:
         lower  est. upper
cor(1,2) 0.253 0.568 0.774
cor(1,3) 0.385 0.659 0.826
cor(1,4) 0.184 0.522 0.749
cor(2,3) 0.272 0.581 0.781
cor(2,4) 0.481 0.725 0.865
cor(3,4) 0.512 0.740 0.870

 Variance function:
   lower  est. upper
10 0.633 0.879  1.22
12 0.801 1.074  1.44
14 0.686 0.959  1.34

 Residual standard error:
lower  est. upper 
 1.77  2.33  3.07 

> ## IGNORE_RDIFF_END
> fm2Orth.gls <-
+    update(fm1Orth.gls, corr = corCompSymm(form = ~ 1 | Subject))

> anova(fm1Orth.gls, fm2Orth.gls)
            Model df AIC BIC logLik   Test L.Ratio p-value
fm1Orth.gls     1 14 453 490   -212                       
fm2Orth.gls     2  9 450 474   -216 1 vs 2    7.43   0.191

> intervals(fm2Orth.gls)
Approximate 95% confidence intervals

 Coefficients:
                       lower   est.   upper
(Intercept)           23.930 24.868 25.8071
SexFemale             -3.668 -2.197 -0.7266
I(age - 11)            0.642  0.794  0.9470
SexFemale:I(age - 11) -0.555 -0.316 -0.0763

 Correlation structure:
    lower  est. upper
Rho 0.446 0.635 0.778

 Variance function:
   lower  est. upper
10 0.638 0.862  1.17
12 0.771 1.034  1.39
14 0.683 0.920  1.24

 Residual standard error:
lower  est. upper 
 1.81  2.39  3.15 

> fm3Orth.gls <- update(fm2Orth.gls, weights = NULL)

> anova(fm2Orth.gls, fm3Orth.gls)
            Model df AIC BIC logLik   Test L.Ratio p-value
fm2Orth.gls     1  9 450 474   -216                       
fm3Orth.gls     2  6 446 462   -217 1 vs 2    1.78   0.618

> plot(fm3Orth.gls, resid(., type = "n") ~ age | Sex)

> fm4Orth.gls <- update(fm3Orth.gls,
+                       weights = varIdent(form = ~ 1 | Sex))

> anova(fm3Orth.gls, fm4Orth.gls)
            Model df AIC BIC logLik   Test L.Ratio p-value
fm3Orth.gls     1  6 446 462   -217                       
fm4Orth.gls     2  7 436 455   -211 1 vs 2    11.6   7e-04

> qqnorm(fm4Orth.gls, ~resid(., type = "n"))

> # not in book but needed for the following command
> fm3Orth.lme <-
+     lme(distance~Sex*I(age-11), data = Orthodont,
+         random = ~ I(age-11) | Subject,
+         weights = varIdent(form = ~ 1 | Sex))

> anova(fm3Orth.lme, fm4Orth.gls, test = FALSE)
            Model df AIC BIC logLik
fm3Orth.lme     1  9 430 453   -206
fm4Orth.gls     2  7 436 455   -211

> fm1Dial.gls <-
+   gls(rate ~(pressure + I(pressure^2) + I(pressure^3) + I(pressure^4))*QB,
+       Dialyzer)

> plot(fm1Dial.gls, resid(.) ~ pressure, abline = 0)

> fm2Dial.gls <- update(fm1Dial.gls,
+                       weights = varPower(form = ~ pressure))

> anova(fm1Dial.gls, fm2Dial.gls)
            Model df AIC BIC logLik   Test L.Ratio p-value
fm1Dial.gls     1 11 761 793   -370                       
fm2Dial.gls     2 12 738 773   -357 1 vs 2    24.9  <.0001

> ACF(fm2Dial.gls, form = ~ 1 | Subject)
  lag    ACF
1   0 1.0000
2   1 0.7709
3   2 0.6323
4   3 0.4083
5   4 0.2007
6   5 0.0731
7   6 0.0778

> plot(ACF(fm2Dial.gls, form = ~ 1 | Subject), alpha = 0.01)

> (fm3Dial.gls <- update(fm2Dial.gls,
+                       corr = corAR1(0.771, form = ~ 1 | Subject)))
Generalized least squares fit by REML
  Model: rate ~ (pressure + I(pressure^2) + I(pressure^3) + I(pressure^4)) *      QB 
  Data: Dialyzer 
  Log-restricted-likelihood: -308

Coefficients:
        (Intercept)            pressure       I(pressure^2) 
            -16.818              92.334             -49.265 
      I(pressure^3)       I(pressure^4)               QB300 
             11.400              -1.020              -1.594 
     pressure:QB300 I(pressure^2):QB300 I(pressure^3):QB300 
              1.705               2.127               0.480 
I(pressure^4):QB300 
             -0.221 

Correlation Structure: AR(1)
 Formula: ~1 | Subject 
 Parameter estimate(s):
  Phi 
0.753 
Variance function:
 Structure: Power of variance covariate
 Formula: ~pressure 
 Parameter estimates:
power 
0.518 
Degrees of freedom: 140 total; 130 residual
Residual standard error: 3.05 

> intervals(fm3Dial.gls)
Approximate 95% confidence intervals

 Coefficients:
                     lower    est.    upper
(Intercept)         -18.90 -16.818 -14.7401
pressure             81.91  92.334 102.7541
I(pressure^2)       -63.10 -49.265 -35.4263
I(pressure^3)         4.56  11.400  18.2345
I(pressure^4)        -2.12  -1.020   0.0856
QB300                -4.76  -1.594   1.5681
pressure:QB300      -13.64   1.705  17.0518
I(pressure^2):QB300 -17.95   2.127  22.2020
I(pressure^3):QB300  -9.35   0.480  10.3097
I(pressure^4):QB300  -1.80  -0.221   1.3608

 Correlation structure:
    lower  est. upper
Phi 0.628 0.753 0.839

 Variance function:
      lower  est. upper
power 0.381 0.518 0.656

 Residual standard error:
lower  est. upper 
 2.50  3.05  3.71 

> anova(fm2Dial.gls, fm3Dial.gls)
            Model df AIC BIC logLik   Test L.Ratio p-value
fm2Dial.gls     1 12 738 773   -357                       
fm3Dial.gls     2 13 643 680   -308 1 vs 2    97.5  <.0001

> anova(fm3Dial.gls, fm2Dial.lme, test = FALSE)
            Model df AIC BIC logLik
fm3Dial.gls     1 13 643 680   -308
fm2Dial.lme     2 18 655 707   -310

> fm1Wheat2 <- gls(yield ~ variety - 1, Wheat2)

> Variogram(fm1Wheat2, form = ~ latitude + longitude)
   variog  dist n.pairs
1   0.370  4.30    1143
2   0.396  5.61    1259
3   0.470  8.39    1263
4   0.508  9.32    1241
5   0.545 10.52    1242
6   0.640 12.75    1241
7   0.612 13.39    1283
8   0.657 14.76    1252
9   0.738 16.18    1221
10  0.728 17.37    1261
11  0.751 18.46    1288
12  0.875 20.24    1254
13  0.805 21.63    1256
14  0.871 22.67    1182
15  0.868 24.62    1257
16  0.859 26.24    1264
17  0.971 28.56    1235
18  0.993 30.79    1226
19  1.096 34.59    1263
20  1.341 39.36    1234

> plot(Variogram(fm1Wheat2, form = ~ latitude + longitude,
+       maxDist = 32), xlim = c(0,32))

> fm2Wheat2 <- update(fm1Wheat2, corr = corSpher(c(28, 0.2),
+                                form = ~ latitude + longitude,
+                                nugget = TRUE))

> fm2Wheat2
Generalized least squares fit by REML
  Model: yield ~ variety - 1 
  Data: Wheat2 
  Log-restricted-likelihood: -534

Coefficients:
  varietyARAPAHOE      varietyBRULE   varietyBUCKSKIN 
             26.7              25.8              34.8 
   varietyCENTURA  varietyCENTURK78   varietyCHEYENNE 
             25.1              26.3              24.7 
      varietyCODY       varietyCOLT       varietyGAGE 
             22.5              25.2              24.3 
 varietyHOMESTEAD   varietyKS831374     varietyLANCER 
             21.7              26.9              23.3 
   varietyLANCOTA    varietyNE83404    varietyNE83406 
             21.3              24.0              25.3 
   varietyNE83407    varietyNE83432    varietyNE83498 
             25.2              21.8              28.7 
   varietyNE83T12    varietyNE84557    varietyNE85556 
             22.1              21.8              28.0 
   varietyNE85623    varietyNE86482    varietyNE86501 
             23.9              25.0              25.0 
   varietyNE86503    varietyNE86507    varietyNE86509 
             27.2              27.5              22.4 
   varietyNE86527    varietyNE86582    varietyNE86606 
             25.9              22.6              26.8 
   varietyNE86607   varietyNE86T666    varietyNE87403 
             25.9              16.8              21.5 
   varietyNE87408    varietyNE87409    varietyNE87446 
             24.3              26.3              22.2 
   varietyNE87451    varietyNE87457    varietyNE87463 
             24.2              23.5              23.2 
   varietyNE87499    varietyNE87512    varietyNE87513 
             22.2              22.6              21.8 
   varietyNE87522    varietyNE87612    varietyNE87613 
             19.5              27.4              27.6 
   varietyNE87615    varietyNE87619    varietyNE87627 
             23.8              28.5              18.5 
    varietyNORKAN    varietyREDLAND varietyROUGHRIDER 
             22.1              28.0              25.7 
   varietySCOUT66  varietySIOUXLAND     varietyTAM107 
             26.9              25.7              22.8 
    varietyTAM200       varietyVONA 
             18.8              24.8 

Correlation Structure: Spherical spatial correlation
 Formula: ~latitude + longitude 
 Parameter estimate(s):
 range nugget 
27.457  0.209 
Degrees of freedom: 224 total; 168 residual
Residual standard error: 7.41 

> fm3Wheat2 <- update(fm1Wheat2,
+                     corr = corRatio(c(12.5, 0.2),
+                     form = ~ latitude + longitude, nugget = TRUE))

> fm3Wheat2
Generalized least squares fit by REML
  Model: yield ~ variety - 1 
  Data: Wheat2 
  Log-restricted-likelihood: -533

Coefficients:
  varietyARAPAHOE      varietyBRULE   varietyBUCKSKIN 
             26.5              26.3              35.0 
   varietyCENTURA  varietyCENTURK78   varietyCHEYENNE 
             24.9              26.7              24.4 
      varietyCODY       varietyCOLT       varietyGAGE 
             23.4              25.2              24.5 
 varietyHOMESTEAD   varietyKS831374     varietyLANCER 
             21.5              26.5              23.0 
   varietyLANCOTA    varietyNE83404    varietyNE83406 
             21.2              24.6              25.7 
   varietyNE83407    varietyNE83432    varietyNE83498 
             25.5              21.8              29.1 
   varietyNE83T12    varietyNE84557    varietyNE85556 
             21.6              21.3              27.9 
   varietyNE85623    varietyNE86482    varietyNE86501 
             23.7              24.4              24.9 
   varietyNE86503    varietyNE86507    varietyNE86509 
             27.3              27.4              22.2 
   varietyNE86527    varietyNE86582    varietyNE86606 
             25.0              23.3              27.3 
   varietyNE86607   varietyNE86T666    varietyNE87403 
             25.7              17.3              21.8 
   varietyNE87408    varietyNE87409    varietyNE87446 
             24.7              26.3              22.1 
   varietyNE87451    varietyNE87457    varietyNE87463 
             24.4              23.6              23.4 
   varietyNE87499    varietyNE87512    varietyNE87513 
             21.9              22.7              21.6 
   varietyNE87522    varietyNE87612    varietyNE87613 
             19.6              28.3              27.7 
   varietyNE87615    varietyNE87619    varietyNE87627 
             24.0              28.7              19.1 
    varietyNORKAN    varietyREDLAND varietyROUGHRIDER 
             22.7              27.7              25.6 
   varietySCOUT66  varietySIOUXLAND     varietyTAM107 
             26.3              25.7              22.5 
    varietyTAM200       varietyVONA 
             18.7              25.0 

Correlation Structure: Rational quadratic spatial correlation
 Formula: ~latitude + longitude 
 Parameter estimate(s):
 range nugget 
13.461  0.194 
Degrees of freedom: 224 total; 168 residual
Residual standard error: 8.85 

> anova(fm2Wheat2, fm3Wheat2)
          Model df  AIC  BIC logLik
fm2Wheat2     1 59 1186 1370   -534
fm3Wheat2     2 59 1183 1368   -533

> anova(fm1Wheat2, fm3Wheat2)
          Model df  AIC  BIC logLik   Test L.Ratio p-value
fm1Wheat2     1 57 1355 1533   -620                       
fm3Wheat2     2 59 1183 1368   -533 1 vs 2     176  <.0001

> plot(Variogram(fm3Wheat2, resType = "n"))

> plot(fm3Wheat2, resid(., type = "n") ~ fitted(.), abline = 0)

> qqnorm(fm3Wheat2, ~ resid(., type = "n"))

> fm4Wheat2 <- update(fm3Wheat2, model = yield ~ variety)

> anova(fm4Wheat2)
Denom. DF: 168 
            numDF F-value p-value
(Intercept)     1   30.40  <.0001
variety        55    1.85  0.0015

> anova(fm3Wheat2, L = c(-1, 0, 1))
Denom. DF: 168 
 F-test for linear combination(s)
varietyARAPAHOE varietyBUCKSKIN 
             -1               1 
  numDF F-value p-value
1     1     7.7  0.0062

> # cleanup
> 
> summary(warnings())
No warnings

======
ch06.R
======

> #-*- R -*-
> 
> # initialization
> 
> library(nlme)

> options(width = 65,
+         ## reduce platform dependence in printed output when testing
+         digits = if(nzchar(Sys.getenv("R_TESTS"))) 3 else 5)

> options(contrasts = c(unordered = "contr.helmert", ordered = "contr.poly"))

> pdf(file = "ch06.pdf")

> # Chapter 6    Nonlinear Mixed-Effects Models:
> #              Basic Concepts and Motivating Examples
> 
> # 6.2 Indomethicin Kinetics
> 
> plot(Indometh)

> fm1Indom.nls <- nls(conc ~ SSbiexp(time, A1, lrc1, A2, lrc2),
+                     data = Indometh)

> summary(fm1Indom.nls)

Formula: conc ~ SSbiexp(time, A1, lrc1, A2, lrc2)

Parameters:
     Estimate Std. Error t value Pr(>|t|)    
A1      2.773      0.253   10.95    4e-16 ***
lrc1    0.886      0.222    3.99  0.00018 ***
A2      0.607      0.267    2.27  0.02660 *  
lrc2   -1.092      0.409   -2.67  0.00966 ** 
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Residual standard error: 0.174 on 62 degrees of freedom

Number of iterations to convergence: 0 
Achieved convergence tolerance: 3.3e-07


> plot(fm1Indom.nls, Subject ~ resid(.), abline = 0)

> (fm1Indom.lis <- nlsList(conc ~ SSbiexp(time, A1, lrc1, A2, lrc2),
+                         data = Indometh))
Call:
  Model: conc ~ SSbiexp(time, A1, lrc1, A2, lrc2) | Subject 
   Data: Indometh 

Coefficients:
    A1  lrc1    A2   lrc2
1 2.03 0.579 0.192 -1.788
4 2.20 0.242 0.255 -1.603
2 2.83 0.801 0.499 -1.635
5 3.57 1.041 0.291 -1.507
6 3.00 1.088 0.969 -0.873
3 5.47 1.750 1.676 -0.412

Degrees of freedom: 66 total; 42 residual
Residual standard error: 0.0756

> plot(intervals(fm1Indom.lis))

> ## IGNORE_RDIFF_BEGIN
> (fm1Indom.nlme <- nlme(fm1Indom.lis,
+                       random = pdDiag(A1 + lrc1 + A2 + lrc2 ~ 1),
+                       control = list(tolerance = 0.0001)))
Nonlinear mixed-effects model fit by maximum likelihood
  Model: conc ~ SSbiexp(time, A1, lrc1, A2, lrc2) 
  Data: Indometh 
  Log-likelihood: 54.6
  Fixed: list(A1 ~ 1, lrc1 ~ 1, A2 ~ 1, lrc2 ~ 1) 
    A1   lrc1     A2   lrc2 
 2.828  0.774  0.461 -1.344 

Random effects:
 Formula: list(A1 ~ 1, lrc1 ~ 1, A2 ~ 1, lrc2 ~ 1)
 Level: Subject
 Structure: Diagonal
           A1  lrc1    A2     lrc2 Residual
StdDev: 0.571 0.158 0.112 7.32e-06   0.0815

Number of Observations: 66
Number of Groups: 6 

> ## IGNORE_RDIFF_END
> fm2Indom.nlme <- update(fm1Indom.nlme,
+                         random = pdDiag(A1 + lrc1 + A2 ~ 1))

> anova(fm1Indom.nlme, fm2Indom.nlme)
              Model df   AIC   BIC logLik   Test L.Ratio p-value
fm1Indom.nlme     1  9 -91.2 -71.5   54.6                       
fm2Indom.nlme     2  8 -93.2 -75.7   54.6 1 vs 2 0.00871   0.926

> (fm3Indom.nlme <- update(fm2Indom.nlme, random = A1+lrc1+A2 ~ 1))
Warning in (function (model, data = sys.frame(sys.parent()), fixed, random,  :
  Iteration 1, LME step: nlminb() did not converge (code = 1). Do increase 'msMaxIter'!
Nonlinear mixed-effects model fit by maximum likelihood
  Model: conc ~ SSbiexp(time, A1, lrc1, A2, lrc2) 
  Data: Indometh 
  Log-likelihood: 58.5
  Fixed: list(A1 ~ 1, lrc1 ~ 1, A2 ~ 1, lrc2 ~ 1) 
    A1   lrc1     A2   lrc2 
 2.815  0.829  0.561 -1.141 

Random effects:
 Formula: list(A1 ~ 1, lrc1 ~ 1, A2 ~ 1)
 Level: Subject
 Structure: General positive-definite, Log-Cholesky parametrization
         StdDev Corr       
A1       0.6904 A1    lrc1 
lrc1     0.1790 0.932      
A2       0.1537 0.471 0.118
Residual 0.0781            

Number of Observations: 66
Number of Groups: 6 

> fm4Indom.nlme <-
+     update(fm3Indom.nlme,
+            random = pdBlocked(list(A1 + lrc1 ~ 1, A2 ~ 1)))

> ## IGNORE_RDIFF_BEGIN
> anova(fm3Indom.nlme, fm4Indom.nlme)
              Model df   AIC   BIC logLik   Test L.Ratio p-value
fm3Indom.nlme     1 11 -94.9 -70.9   58.5                       
fm4Indom.nlme     2  9 -98.2 -78.4   58.1 1 vs 2   0.789   0.674

> ## IGNORE_RDIFF_END
> anova(fm2Indom.nlme, fm4Indom.nlme)
              Model df   AIC   BIC logLik   Test L.Ratio p-value
fm2Indom.nlme     1  8 -93.2 -75.7   54.6                       
fm4Indom.nlme     2  9 -98.2 -78.4   58.1 1 vs 2    6.97  0.0083

> plot(fm4Indom.nlme, id = 0.05, adj = -1)

> qqnorm(fm4Indom.nlme)

> plot(augPred(fm4Indom.nlme, level = 0:1))

> summary(fm4Indom.nlme)
Nonlinear mixed-effects model fit by maximum likelihood
  Model: conc ~ SSbiexp(time, A1, lrc1, A2, lrc2) 
  Data: Indometh 
    AIC   BIC logLik
  -98.2 -78.4   58.1

Random effects:
 Composite Structure: Blocked

 Block 1: A1, lrc1
 Formula: list(A1 ~ 1, lrc1 ~ 1)
 Level: Subject
 Structure: General positive-definite
     StdDev Corr
A1   0.720  A1  
lrc1 0.149  1   

 Block 2: A2
 Formula: A2 ~ 1 | Subject
           A2 Residual
StdDev: 0.213   0.0782

Fixed effects:  list(A1 ~ 1, lrc1 ~ 1, A2 ~ 1, lrc2 ~ 1) 
      Value Std.Error DF t-value p-value
A1    2.783     0.327 57    8.51       0
lrc1  0.898     0.111 57    8.11       0
A2    0.658     0.143 57    4.61       0
lrc2 -1.000     0.150 57   -6.67       0
 Correlation: 
     A1     lrc1   A2    
lrc1  0.602              
A2   -0.058  0.556       
lrc2 -0.109  0.570  0.702

Standardized Within-Group Residuals:
   Min     Q1    Med     Q3    Max 
-3.459 -0.437  0.110  0.504  3.057 

Number of Observations: 66
Number of Groups: 6 

> # 6.3 Growth of Soybean Plants
> 
> head(Soybean)
Grouped Data: weight ~ Time | Plot
    Plot Variety Year Time weight
1 1988F1       F 1988   14  0.106
2 1988F1       F 1988   21  0.261
3 1988F1       F 1988   28  0.666
4 1988F1       F 1988   35  2.110
5 1988F1       F 1988   42  3.560
6 1988F1       F 1988   49  6.230

> plot(Soybean, outer = ~ Year * Variety)

> (fm1Soy.lis <- nlsList(weight ~ SSlogis(Time, Asym, xmid, scal),
+                        data = Soybean,
+                        ## in R >= 3.4.3, more iterations are needed for "1989P5"
+                        ## due to a change of initial values in SSlogis();
+                        ## control is passed to getInitial() only since R 4.1.0
+                        control = list(maxiter = 60)))
Warning: 1 error caught in nls(y ~ 1/(1 + exp((xmid - x)/scal)), data = xy, start = list(xmid = aux[[1L]], scal = aux[[2L]]), algorithm = "plinear", ...): step factor 0.000488281 reduced below 'minFactor' of 0.000976562
Call:
  Model: weight ~ SSlogis(Time, Asym, xmid, scal) | Plot 
   Data: Soybean 

Coefficients:
         Asym  xmid  scal
1988F4  15.15  52.8  5.18
1988F2  19.75  56.6  8.41
1988F1  20.34  57.4  9.60
1988F7  19.87  56.2  8.07
1988F5  30.65  64.1 11.26
1988F8  22.78  59.3  9.00
1988F6  23.29  59.6  9.72
1988F3  23.70  56.4  7.64
1988P1  17.30  48.8  6.36
1988P5  17.70  51.3  6.81
1988P4  24.01  57.8 11.74
1988P8  28.25  63.0 10.95
1988P7  27.49  61.5 10.18
1988P3  24.94  56.3  8.32
1988P2  36.66  66.6 11.92
1988P6 163.70 105.0 17.93
1989F6   8.51  55.3  8.86
1989F5   9.67  51.3  7.21
1989F4  11.25  53.8  6.49
1989F1  11.25  56.6  6.07
1989F2  11.23  52.2  7.02
1989F7  10.07  51.4  5.50
1989F8  10.61  48.0  5.96
1989F3  18.42  66.1  9.22
1989P7  15.47  46.3  5.39
1989P4  18.18  57.2  8.40
1989P6  20.50  58.2 10.61
1989P5  17.89  54.1  6.05
1989P1  21.68  59.7  9.97
1989P3  22.28  53.4  7.90
1989P2  28.30  67.2 12.52
1989P8     NA    NA    NA
1990F2  19.46  66.3 13.16
1990F3  19.87  58.3 12.80
1990F4  27.44  70.3 14.56
1990F5  18.72  51.3  7.76
1990F1  19.79  55.7  9.62
1990F8  20.29  55.5  7.77
1990F7  19.84  54.7  6.79
1990F6  21.20  54.6  9.26
1990P8  18.51  52.4  8.58
1990P7  19.16  54.8 10.85
1990P3  19.20  49.7  9.32
1990P1  18.45  47.9  6.61
1990P6  17.69  50.2  6.63
1990P5  19.54  51.2  7.29
1990P2  25.79  62.4 11.66
1990P4  26.13  61.2 10.97

Degrees of freedom: 404 total; 263 residual
Residual standard error: 1.04

> ## IGNORE_RDIFF_BEGIN
> (fm1Soy.nlme <- nlme(fm1Soy.lis))
Nonlinear mixed-effects model fit by maximum likelihood
  Model: weight ~ SSlogis(Time, Asym, xmid, scal) 
  Data: Soybean 
  Log-likelihood: -740
  Fixed: list(Asym ~ 1, xmid ~ 1, scal ~ 1) 
Asym xmid scal 
19.3 55.0  8.4 

Random effects:
 Formula: list(Asym ~ 1, xmid ~ 1, scal ~ 1)
 Level: Plot
 Structure: General positive-definite, Log-Cholesky parametrization
         StdDev Corr       
Asym     5.20   Asym  xmid 
xmid     4.20   0.721      
scal     1.40   0.711 0.959
Residual 1.12              

Number of Observations: 412
Number of Groups: 48 

> ## IGNORE_RDIFF_END
> fm2Soy.nlme <- update(fm1Soy.nlme, weights = varPower())
Warning in (function (model, data = sys.frame(sys.parent()), fixed, random,  :
  Iteration 1, LME step: nlminb() did not converge (code = 1). Do increase 'msMaxIter'!
Warning in (function (model, data = sys.frame(sys.parent()), fixed, random,  :
  Iteration 6, LME step: nlminb() did not converge (code = 1). PORT message: false convergence (8)

> anova(fm1Soy.nlme, fm2Soy.nlme)
            Model df  AIC  BIC logLik   Test L.Ratio p-value
fm1Soy.nlme     1 10 1500 1540   -740                       
fm2Soy.nlme     2 11  746  790   -362 1 vs 2     756  <.0001

> plot(ranef(fm2Soy.nlme, augFrame = TRUE),
+      form = ~ Year * Variety, layout = c(3,1))

> soyFix <- fixef(fm2Soy.nlme)

> options(contrasts = c("contr.treatment", "contr.poly"))

> ## IGNORE_RDIFF_BEGIN
> (fm3Soy.nlme <-
+  update(fm2Soy.nlme,
+         fixed = Asym + xmid + scal ~ Year,
+         start = c(soyFix[1], 0, 0, soyFix[2], 0, 0, soyFix[3], 0, 0)))
Warning in (function (model, data = sys.frame(sys.parent()), fixed, random,  :
  Iteration 1, LME step: nlminb() did not converge (code = 1). Do increase 'msMaxIter'!
Warning in (function (model, data = sys.frame(sys.parent()), fixed, random,  :
  Iteration 6, LME step: nlminb() did not converge (code = 1). PORT message: false convergence (8)
Nonlinear mixed-effects model fit by maximum likelihood
  Model: weight ~ SSlogis(Time, Asym, xmid, scal) 
  Data: Soybean 
  Log-likelihood: -326
  Fixed: Asym + xmid + scal ~ Year 
Asym.(Intercept)    Asym.Year1989    Asym.Year1990 
          20.208           -6.303           -3.465 
xmid.(Intercept)    xmid.Year1989    xmid.Year1990 
          54.099           -2.480           -4.848 
scal.(Intercept)    scal.Year1989    scal.Year1990 
           8.051           -0.932           -0.662 

Random effects:
 Formula: list(Asym ~ 1, xmid ~ 1, scal ~ 1)
 Level: Plot
 Structure: General positive-definite, Log-Cholesky parametrization
                 StdDev   Corr         
Asym.(Intercept) 2.71e+00 As.(I) xm.(I)
xmid.(Intercept) 8.34e-12 0.992        
scal.(Intercept) 1.08e-01 0.999  0.993 
Residual         2.16e-01              

Variance function:
 Structure: Power of variance covariate
 Formula: ~fitted(.) 
 Parameter estimates:
power 
 0.95 
Number of Observations: 412
Number of Groups: 48 

> ## IGNORE_RDIFF_END
> anova(fm3Soy.nlme)
                 numDF denDF F-value p-value
Asym.(Intercept)     1   356    2057  <.0001
Asym.Year            2   356     103  <.0001
xmid.(Intercept)     1   356   11420  <.0001
xmid.Year            2   356       9   1e-04
scal.(Intercept)     1   356    7967  <.0001
scal.Year            2   356      11  <.0001

> # The following line is not in the book but needed to fit the model
> fm4Soy.nlme <-
+     nlme(weight ~ SSlogis(Time, Asym, xmid, scal),
+          data = Soybean,
+          fixed = list(Asym ~ Year*Variety, xmid ~ Year + Variety, scal ~ Year),
+          random = Asym ~ 1,
+          start = c(17, 0, 0, 0, 0, 0, 52, 0, 0, 0, 7.5, 0, 0),
+          weights = varPower(0.95), control = list(verbose = TRUE))

> # FIXME: An update doesn't work for the fixed argument when fixed is a list
> ## p. 293-4 :
> summary(fm4Soy.nlme)
Nonlinear mixed-effects model fit by maximum likelihood
  Model: weight ~ SSlogis(Time, Asym, xmid, scal) 
  Data: Soybean 
  AIC BIC logLik
  616 681   -292

Random effects:
 Formula: Asym ~ 1 | Plot
        Asym.(Intercept) Residual
StdDev:             1.04    0.218

Variance function:
 Structure: Power of variance covariate
 Formula: ~fitted(.) 
 Parameter estimates:
power 
0.943 
Fixed effects:  list(Asym ~ Year * Variety, xmid ~ Year + Variety, scal ~ Year) 
                       Value Std.Error  DF t-value p-value
Asym.(Intercept)        19.4     0.954 352    20.4  0.0000
Asym.Year1989           -8.8     1.072 352    -8.2  0.0000
Asym.Year1990           -3.7     1.177 352    -3.1  0.0018
Asym.VarietyP            1.6     1.038 352     1.6  0.1189
Asym.Year1989:VarietyP   5.6     1.171 352     4.8  0.0000
Asym.Year1990:VarietyP   0.1     1.176 352     0.1  0.9004
xmid.(Intercept)        54.8     0.755 352    72.6  0.0000
xmid.Year1989           -2.2     0.972 352    -2.3  0.0218
xmid.Year1990           -5.0     0.974 352    -5.1  0.0000
xmid.VarietyP           -1.3     0.414 352    -3.1  0.0019
scal.(Intercept)         8.1     0.147 352    54.8  0.0000
scal.Year1989           -0.9     0.201 352    -4.4  0.0000
scal.Year1990           -0.7     0.212 352    -3.2  0.0016
 Correlation: 
                       As.(I) As.Y1989 As.Y1990 Asy.VP A.Y1989:
Asym.Year1989          -0.831                                  
Asym.Year1990          -0.736  0.646                           
Asym.VarietyP          -0.532  0.374    0.304                  
Asym.Year1989:VarietyP  0.339 -0.403   -0.249   -0.662         
Asym.Year1990:VarietyP  0.318 -0.273   -0.447   -0.627  0.533  
xmid.(Intercept)        0.729 -0.595   -0.523   -0.144  0.007  
xmid.Year1989          -0.488  0.603    0.394   -0.021  0.133  
xmid.Year1990          -0.489  0.433    0.661   -0.016  0.020  
xmid.VarietyP          -0.337  0.127    0.052    0.572 -0.114  
scal.(Intercept)        0.432 -0.381   -0.345    0.023 -0.029  
scal.Year1989          -0.311  0.369    0.252   -0.025  0.090  
scal.Year1990          -0.296  0.263    0.398   -0.023  0.022  
                       A.Y1990: xm.(I) x.Y198 x.Y199 xmd.VP
Asym.Year1989                                              
Asym.Year1990                                              
Asym.VarietyP                                              
Asym.Year1989:VarietyP                                     
Asym.Year1990:VarietyP                                     
xmid.(Intercept)       -0.011                              
xmid.Year1989           0.021   -0.705                     
xmid.Year1990           0.054   -0.706  0.545              
xmid.VarietyP          -0.057   -0.308  0.006  0.015       
scal.(Intercept)       -0.031    0.817 -0.629 -0.628 -0.022
scal.Year1989           0.023   -0.593  0.855  0.459  0.002
scal.Year1990           0.048   -0.563  0.437  0.840  0.004
                       sc.(I) s.Y198
Asym.Year1989                       
Asym.Year1990                       
Asym.VarietyP                       
Asym.Year1989:VarietyP              
Asym.Year1990:VarietyP              
xmid.(Intercept)                    
xmid.Year1989                       
xmid.Year1990                       
xmid.VarietyP                       
scal.(Intercept)                    
scal.Year1989          -0.731       
scal.Year1990          -0.694  0.507

Standardized Within-Group Residuals:
   Min     Q1    Med     Q3    Max 
-2.628 -0.608 -0.124  0.570  3.919 

Number of Observations: 412
Number of Groups: 48 

> plot(augPred(fm4Soy.nlme))# Fig 6.14, p. 295

> # 6.4 Clinical Study of Phenobarbital Kinetics
> 
> (fm1Pheno.nlme <-
+  nlme(conc ~ phenoModel(Subject, time, dose, lCl, lV),
+       data = Phenobarb, fixed = lCl + lV ~ 1,
+       random = pdDiag(lCl + lV ~ 1), start = c(-5, 0),
+       na.action = NULL, naPattern = ~ !is.na(conc)))
Nonlinear mixed-effects model fit by maximum likelihood
  Model: conc ~ phenoModel(Subject, time, dose, lCl, lV) 
  Data: Phenobarb 
  Log-likelihood: -505
  Fixed: lCl + lV ~ 1 
   lCl     lV 
-5.093  0.343 

Random effects:
 Formula: list(lCl ~ 1, lV ~ 1)
 Level: Subject
 Structure: Diagonal
         lCl   lV Residual
StdDev: 0.44 0.45     2.79

Number of Observations: 155
Number of Groups: 59 

> fm1Pheno.ranef <- ranef(fm1Pheno.nlme, augFrame = TRUE)

> # (These plots used to encounter difficulties, now fine):
> plot(fm1Pheno.ranef, form = lCl ~ Wt + ApgarInd)

> plot(fm1Pheno.ranef, form = lV  ~ Wt + ApgarInd)

> options(contrasts = c("contr.treatment", "contr.poly"))

> if(FALSE)## This fit just "ping-pongs" until max.iterations error
+ fm2Pheno.nlme <-
+    update(fm1Pheno.nlme,
+           fixed = list(lCl ~ Wt, lV ~ Wt + ApgarInd),
+           start = c(-5.0935, 0, 0.34259, 0, 0),
+           control = list(pnlsTol = 1e-4, maxIter = 500,
+           msVerbose = TRUE, opt = "nlm"))

> ##summary(fm2Pheno.nlme)
> ##fm3Pheno.nlme <-
> ##    update(fm2Pheno.nlme,
> ##           fixed = lCl + lV ~ Wt,
> ##           start = fixef(fm2Pheno.nlme)[-5])
> ##plot(fm3Pheno.nlme, conc ~ fitted(.), abline = c(0,1))
> 
> # cleanup
> 
> summary(warnings())
No warnings

======
ch08.R
======

> #-*- R -*-
> 
> # initialization
> 
> library(nlme)

> library(lattice)

> options(width = 65,
+         ## reduce platform dependence in printed output when testing
+         digits = if(nzchar(Sys.getenv("R_TESTS"))) 3 else 5)

> options(contrasts = c(unordered = "contr.helmert", ordered = "contr.poly"))

> pdf(file = "ch08.pdf")

> # Chapter 8    Fitting Nonlinear Mixed-Effects Models
> 
> # 8.1 Fitting Nonlinear Models in S with nls and nlsList
> 
> ## outer = ~1 is used to display all five curves in one panel
> plot(Orange, outer = ~1)

> logist <-
+    function(x, Asym, xmid, scal) Asym/(1 + exp(-(x - xmid)/scal))

> logist <- deriv(~Asym/(1+exp(-(x-xmid)/scal)),
+     c("Asym", "xmid", "scal"), function(x, Asym, xmid, scal){})

> Asym <- 180; xmid <- 700; scal <- 300

> logist(Orange$age[1:7], Asym, xmid, scal)
[1]  22.6  58.9  84.6 132.1 153.8 162.7 171.0
attr(,"gradient")
      Asym    xmid    scal
[1,] 0.126 -0.0659  0.1279
[2,] 0.327 -0.1321  0.0951
[3,] 0.470 -0.1495  0.0179
[4,] 0.734 -0.1172 -0.1188
[5,] 0.854 -0.0746 -0.1321
[6,] 0.904 -0.0522 -0.1169
[7,] 0.950 -0.0286 -0.0841

> fm1Oran.nls <- nls(circumference ~ logist(age, Asym, xmid, scal),
+    data = Orange, start = c(Asym = 170, xmid = 700, scal = 500))

> summary(fm1Oran.nls)

Formula: circumference ~ logist(age, Asym, xmid, scal)

Parameters:
     Estimate Std. Error t value Pr(>|t|)    
Asym    192.7       20.2    9.52  7.5e-11 ***
xmid    728.8      107.3    6.79  1.1e-07 ***
scal    353.5       81.5    4.34  0.00013 ***
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Residual standard error: 23.4 on 32 degrees of freedom

Number of iterations to convergence: 5 
Achieved convergence tolerance: 4.39e-06


> plot(fm1Oran.nls)

> plot(fm1Oran.nls, Tree ~ resid(.), abline = 0)

> Orange.sortAvg <- sortedXyData("age", "circumference", Orange)

> Orange.sortAvg
     x     y
1  118  31.0
2  484  57.8
3  664  93.2
4 1004 134.2
5 1231 145.6
6 1372 173.4
7 1582 175.8

> NLSstClosestX(Orange.sortAvg, 130)
[1] 969

> logistInit <- function(mCall, LHS, data) {
+     xy <- sortedXyData(mCall[["x"]], LHS, data)
+     if(nrow(xy) < 3) {
+         stop("Too few distinct input values to fit a logistic")
+     }
+     Asym <- max(abs(xy[,"y"]))
+     if (Asym != max(xy[,"y"])) Asym <- -Asym  # negative asymptote
+     xmid <- NLSstClosestX(xy, 0.5 * Asym)
+     scal <- NLSstClosestX(xy, 0.75 * Asym) - xmid
+     value <- c(Asym, xmid, scal)
+     names(value) <- mCall[c("Asym", "xmid", "scal")]
+     value
+ }

> logist <- selfStart(logist, initial = logistInit)

> class(logist)
[1] "selfStart"

> logist <- selfStart(~ Asym/(1 + exp(-(x - xmid)/scal)),
+    initial = logistInit, parameters = c("Asym", "xmid", "scal"))

> getInitial(circumference ~ logist(age, Asym, xmid, scal), Orange)
Warning in getInitial.selfStart(func, data, mCall = as.list(match.call(func,  :
  selfStart initializing functions should have a final '...' argument since R 4.1.0
Asym xmid scal 
 176  637  347 

> nls(circumference ~ logist(age, Asym, xmid, scal), Orange)
Warning in getInitial.selfStart(func, data, mCall = as.list(match.call(func,  :
  selfStart initializing functions should have a final '...' argument since R 4.1.0
Nonlinear regression model
  model: circumference ~ logist(age, Asym, xmid, scal)
   data: Orange
Asym xmid scal 
 193  729  354 
 residual sum-of-squares: 17480

Number of iterations to convergence: 4 
Achieved convergence tolerance: 8.63e-07

> getInitial(circumference ~ SSlogis(age,Asym,xmid,scal), Orange)
Asym xmid scal 
 193  729  354 

> nls(circumference ~ SSlogis(age, Asym, xmid, scal), Orange)
Nonlinear regression model
  model: circumference ~ SSlogis(age, Asym, xmid, scal)
   data: Orange
Asym xmid scal 
 193  729  354 
 residual sum-of-squares: 17480

Number of iterations to convergence: 0 
Achieved convergence tolerance: 2.2e-06

> fm1Oran.lis <-
+    nlsList(circumference ~ SSlogis(age, Asym, xmid, scal) | Tree,
+            data = Orange)

> fm1Oran.lis <- nlsList(SSlogis, Orange)

> fm1Oran.lis.noSS <-
+     nlsList(circumference ~ Asym/(1+exp(-(age-xmid)/scal)),
+              data = Orange,
+              start = c(Asym = 170, xmid = 700, scal = 500))

> fm1Oran.lis
Call:
  Model: circumference ~ SSlogis(age, Asym, xmid, scal) | Tree 
   Data: Orange 

Coefficients:
  Asym xmid scal
3  159  735  401
1  154  627  363
5  207  861  380
2  219  700  332
4  225  711  303

Degrees of freedom: 35 total; 20 residual
Residual standard error: 7.98

> summary(fm1Oran.lis)
Call:
  Model: circumference ~ SSlogis(age, Asym, xmid, scal) | Tree 
   Data: Orange 

Coefficients:
   Asym 
  Estimate Std. Error t value Pr(>|t|)
3      159       19.2    8.26 0.000460
1      154       13.6   11.34 0.000169
5      207       22.0    9.41 0.000738
2      219       13.4   16.39 0.000105
4      225       11.8   19.03 0.000104
   xmid 
  Estimate Std. Error t value Pr(>|t|)
3      735      130.8    5.62 0.002011
1      627       92.9    6.75 0.001263
5      861      108.0    7.98 0.001389
2      700       61.4   11.42 0.000435
4      711       51.2   13.89 0.000358
   scal 
  Estimate Std. Error t value Pr(>|t|)
3      401       94.8    4.23  0.00571
1      363       81.2    4.46  0.00586
5      380       66.8    5.69  0.00487
2      332       49.4    6.73  0.00324
4      303       41.6    7.29  0.00415

Residual standard error: 7.98 on 20 degrees of freedom


> plot(intervals(fm1Oran.lis), layout = c(3,1))

> plot(fm1Oran.lis, Tree ~ resid(.), abline = 0)

> Theoph[1:4,]
Grouped Data: conc ~ Time | Subject
  Subject   Wt Dose Time  conc
1       1 79.6 4.02 0.00  0.74
2       1 79.6 4.02 0.25  2.84
3       1 79.6 4.02 0.57  6.57
4       1 79.6 4.02 1.12 10.50

> fm1Theo.lis <- nlsList(conc ~ SSfol(Dose, Time, lKe, lKa, lCl),
+    data = Theoph)

> fm1Theo.lis
Call:
  Model: conc ~ SSfol(Dose, Time, lKe, lKa, lCl) | Subject 
   Data: Theoph 

Coefficients:
     lKe    lKa   lCl
6  -2.31  0.152 -2.97
7  -2.28 -0.386 -2.96
8  -2.39  0.319 -3.07
11 -2.32  1.348 -2.86
3  -2.51  0.898 -3.23
2  -2.29  0.664 -3.11
4  -2.44  0.158 -3.29
9  -2.45  2.182 -3.42
12 -2.25 -0.183 -3.17
10 -2.60 -0.363 -3.43
1  -2.92  0.575 -3.92
5  -2.43  0.386 -3.13

Degrees of freedom: 132 total; 96 residual
Residual standard error: 0.7

> plot(intervals(fm1Theo.lis), layout = c(3,1))

> pairs(fm1Theo.lis, id = 0.1)

> # 8.2 Fitting Nonlinear Mixed-Effects Models with nlme
> 
> ## no need to specify groups, as Orange is a groupedData object
> ## random is omitted - by default it is equal to fixed
> (fm1Oran.nlme <-
+    nlme(circumference ~ SSlogis(age, Asym, xmid, scal),
+        data = Orange,
+        fixed = Asym + xmid + scal ~ 1,
+        start = fixef(fm1Oran.lis)))
Warning in nlme.formula(circumference ~ SSlogis(age, Asym, xmid, scal),  :
  Iteration 1, LME step: nlminb() did not converge (code = 1). Do increase 'msMaxIter'!
Nonlinear mixed-effects model fit by maximum likelihood
  Model: circumference ~ SSlogis(age, Asym, xmid, scal) 
  Data: Orange 
  Log-likelihood: -130
  Fixed: Asym + xmid + scal ~ 1 
Asym xmid scal 
 192  728  357 

Random effects:
 Formula: list(Asym ~ 1, xmid ~ 1, scal ~ 1)
 Level: Tree
 Structure: General positive-definite, Log-Cholesky parametrization
         StdDev Corr         
Asym     27.05  Asym   xmid  
xmid     24.25  -0.328       
scal     36.60  -0.992  0.443
Residual  7.32               

Number of Observations: 35
Number of Groups: 5 

> summary(fm1Oran.nlme)
Nonlinear mixed-effects model fit by maximum likelihood
  Model: circumference ~ SSlogis(age, Asym, xmid, scal) 
  Data: Orange 
  AIC BIC logLik
  280 296   -130

Random effects:
 Formula: list(Asym ~ 1, xmid ~ 1, scal ~ 1)
 Level: Tree
 Structure: General positive-definite, Log-Cholesky parametrization
         StdDev Corr         
Asym     27.05  Asym   xmid  
xmid     24.25  -0.328       
scal     36.60  -0.992  0.443
Residual  7.32               

Fixed effects:  Asym + xmid + scal ~ 1 
     Value Std.Error DF t-value p-value
Asym   192      14.1 28    13.7       0
xmid   728      34.6 28    21.0       0
scal   357      30.5 28    11.7       0
 Correlation: 
     Asym   xmid  
xmid  0.277       
scal -0.193  0.665

Standardized Within-Group Residuals:
   Min     Q1    Med     Q3    Max 
-1.819 -0.522  0.174  0.518  1.645 

Number of Observations: 35
Number of Groups: 5 

> summary(fm1Oran.nls)

Formula: circumference ~ logist(age, Asym, xmid, scal)

Parameters:
     Estimate Std. Error t value Pr(>|t|)    
Asym    192.7       20.2    9.52  7.5e-11 ***
xmid    728.8      107.3    6.79  1.1e-07 ***
scal    353.5       81.5    4.34  0.00013 ***
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Residual standard error: 23.4 on 32 degrees of freedom

Number of iterations to convergence: 5 
Achieved convergence tolerance: 4.39e-06


> pairs(fm1Oran.nlme)

> fm2Oran.nlme <- update(fm1Oran.nlme, random = Asym ~ 1)

> anova(fm1Oran.nlme, fm2Oran.nlme)
             Model df AIC BIC logLik   Test L.Ratio p-value
fm1Oran.nlme     1 10 280 296   -130                       
fm2Oran.nlme     2  5 273 281   -132 1 vs 2    3.19   0.671

> plot(fm1Oran.nlme)

> ## level = 0:1 requests fixed (0) and within-group (1) predictions
> plot(augPred(fm2Oran.nlme, level = 0:1),
+      layout = c(5,1))

> qqnorm(fm2Oran.nlme, abline = c(0,1))

> (fm1Theo.nlme <- nlme(fm1Theo.lis))
Warning in (function (model, data = sys.frame(sys.parent()), fixed, random,  :
  Iteration 2, LME step: nlminb() did not converge (code = 1). Do increase 'msMaxIter'!
Nonlinear mixed-effects model fit by maximum likelihood
  Model: conc ~ SSfol(Dose, Time, lKe, lKa, lCl) 
  Data: Theoph 
  Log-likelihood: -173
  Fixed: list(lKe ~ 1, lKa ~ 1, lCl ~ 1) 
   lKe    lKa    lCl 
-2.433  0.451 -3.214 

Random effects:
 Formula: list(lKe ~ 1, lKa ~ 1, lCl ~ 1)
 Level: Subject
 Structure: General positive-definite, Log-Cholesky parametrization
         StdDev Corr         
lKe      0.131  lKe    lKa   
lKa      0.638   0.012       
lCl      0.251   0.995 -0.089
Residual 0.682               

Number of Observations: 132
Number of Groups: 12 

> ## IGNORE_RDIFF_BEGIN
> try( intervals(fm1Theo.nlme, which="var-cov") ) ## could fail: Non-positive definite...
Approximate 95% confidence intervals

 Random Effects:
  Level: Subject 
               lower    est. upper
sd(lKe)       0.0574  0.1310 0.299
sd(lKa)       0.3845  0.6378 1.058
sd(lCl)       0.1557  0.2512 0.405
cor(lKe,lKa) -0.9302  0.0116 0.933
cor(lKe,lCl) -0.9950  0.9948 1.000
cor(lKa,lCl) -0.7711 -0.0892 0.688

 Within-group standard error:
lower  est. upper 
0.596 0.682 0.780 

> ## IGNORE_RDIFF_END
> (fm2Theo.nlme <- update(fm1Theo.nlme,
+   random = pdDiag(lKe + lKa + lCl ~ 1)))
Nonlinear mixed-effects model fit by maximum likelihood
  Model: conc ~ SSfol(Dose, Time, lKe, lKa, lCl) 
  Data: Theoph 
  Log-likelihood: -177
  Fixed: list(lKe ~ 1, lKa ~ 1, lCl ~ 1) 
   lKe    lKa    lCl 
-2.455  0.466 -3.227 

Random effects:
 Formula: list(lKe ~ 1, lKa ~ 1, lCl ~ 1)
 Level: Subject
 Structure: Diagonal
             lKe   lKa   lCl Residual
StdDev: 1.93e-05 0.644 0.167    0.709

Number of Observations: 132
Number of Groups: 12 

> fm3Theo.nlme <-
+   update(fm2Theo.nlme, random = pdDiag(lKa + lCl ~ 1))

> anova(fm1Theo.nlme, fm3Theo.nlme, fm2Theo.nlme)
             Model df AIC BIC logLik   Test L.Ratio p-value
fm1Theo.nlme     1 10 367 395   -173                       
fm3Theo.nlme     2  6 366 383   -177 1 vs 2     7.4   0.116
fm2Theo.nlme     3  7 368 388   -177 2 vs 3     0.0   0.949

> plot(fm3Theo.nlme)

> qqnorm(fm3Theo.nlme, ~ ranef(.))

> CO2
Grouped Data: uptake ~ conc | Plant
   Plant        Type  Treatment conc uptake
1    Qn1      Quebec nonchilled   95   16.0
2    Qn1      Quebec nonchilled  175   30.4
3    Qn1      Quebec nonchilled  250   34.8
4    Qn1      Quebec nonchilled  350   37.2
5    Qn1      Quebec nonchilled  500   35.3
6    Qn1      Quebec nonchilled  675   39.2
7    Qn1      Quebec nonchilled 1000   39.7
8    Qn2      Quebec nonchilled   95   13.6
9    Qn2      Quebec nonchilled  175   27.3
10   Qn2      Quebec nonchilled  250   37.1
11   Qn2      Quebec nonchilled  350   41.8
12   Qn2      Quebec nonchilled  500   40.6
13   Qn2      Quebec nonchilled  675   41.4
14   Qn2      Quebec nonchilled 1000   44.3
15   Qn3      Quebec nonchilled   95   16.2
16   Qn3      Quebec nonchilled  175   32.4
17   Qn3      Quebec nonchilled  250   40.3
18   Qn3      Quebec nonchilled  350   42.1
19   Qn3      Quebec nonchilled  500   42.9
20   Qn3      Quebec nonchilled  675   43.9
21   Qn3      Quebec nonchilled 1000   45.5
22   Qc1      Quebec    chilled   95   14.2
23   Qc1      Quebec    chilled  175   24.1
24   Qc1      Quebec    chilled  250   30.3
25   Qc1      Quebec    chilled  350   34.6
26   Qc1      Quebec    chilled  500   32.5
27   Qc1      Quebec    chilled  675   35.4
28   Qc1      Quebec    chilled 1000   38.7
29   Qc2      Quebec    chilled   95    9.3
30   Qc2      Quebec    chilled  175   27.3
31   Qc2      Quebec    chilled  250   35.0
32   Qc2      Quebec    chilled  350   38.8
33   Qc2      Quebec    chilled  500   38.6
34   Qc2      Quebec    chilled  675   37.5
35   Qc2      Quebec    chilled 1000   42.4
36   Qc3      Quebec    chilled   95   15.1
37   Qc3      Quebec    chilled  175   21.0
38   Qc3      Quebec    chilled  250   38.1
39   Qc3      Quebec    chilled  350   34.0
40   Qc3      Quebec    chilled  500   38.9
41   Qc3      Quebec    chilled  675   39.6
42   Qc3      Quebec    chilled 1000   41.4
43   Mn1 Mississippi nonchilled   95   10.6
44   Mn1 Mississippi nonchilled  175   19.2
45   Mn1 Mississippi nonchilled  250   26.2
46   Mn1 Mississippi nonchilled  350   30.0
47   Mn1 Mississippi nonchilled  500   30.9
48   Mn1 Mississippi nonchilled  675   32.4
49   Mn1 Mississippi nonchilled 1000   35.5
50   Mn2 Mississippi nonchilled   95   12.0
51   Mn2 Mississippi nonchilled  175   22.0
52   Mn2 Mississippi nonchilled  250   30.6
53   Mn2 Mississippi nonchilled  350   31.8
54   Mn2 Mississippi nonchilled  500   32.4
55   Mn2 Mississippi nonchilled  675   31.1
56   Mn2 Mississippi nonchilled 1000   31.5
57   Mn3 Mississippi nonchilled   95   11.3
58   Mn3 Mississippi nonchilled  175   19.4
59   Mn3 Mississippi nonchilled  250   25.8
60   Mn3 Mississippi nonchilled  350   27.9
61   Mn3 Mississippi nonchilled  500   28.5
62   Mn3 Mississippi nonchilled  675   28.1
63   Mn3 Mississippi nonchilled 1000   27.8
64   Mc1 Mississippi    chilled   95   10.5
65   Mc1 Mississippi    chilled  175   14.9
66   Mc1 Mississippi    chilled  250   18.1
67   Mc1 Mississippi    chilled  350   18.9
68   Mc1 Mississippi    chilled  500   19.5
69   Mc1 Mississippi    chilled  675   22.2
70   Mc1 Mississippi    chilled 1000   21.9
71   Mc2 Mississippi    chilled   95    7.7
72   Mc2 Mississippi    chilled  175   11.4
73   Mc2 Mississippi    chilled  250   12.3
74   Mc2 Mississippi    chilled  350   13.0
75   Mc2 Mississippi    chilled  500   12.5
76   Mc2 Mississippi    chilled  675   13.7
77   Mc2 Mississippi    chilled 1000   14.4
78   Mc3 Mississippi    chilled   95   10.6
79   Mc3 Mississippi    chilled  175   18.0
80   Mc3 Mississippi    chilled  250   17.9
81   Mc3 Mississippi    chilled  350   17.9
82   Mc3 Mississippi    chilled  500   17.9
83   Mc3 Mississippi    chilled  675   18.9
84   Mc3 Mississippi    chilled 1000   19.9

> plot(CO2, outer = ~Treatment*Type, layout = c(4,1))

> (fm1CO2.lis <- nlsList(SSasympOff, CO2))
Call:
  Model: uptake ~ SSasympOff(conc, Asym, lrc, c0) | Plant 
   Data: CO2 

Coefficients:
    Asym   lrc    c0
Qn1 38.1 -4.38  51.2
Qn2 42.9 -4.67  55.9
Qn3 44.2 -4.49  54.6
Qc1 36.4 -4.86  31.1
Qc3 40.7 -4.95  35.1
Qc2 39.8 -4.46  72.1
Mn3 28.5 -4.59  47.0
Mn2 32.1 -4.47  56.0
Mn1 34.1 -5.06  36.4
Mc2 13.6 -4.56  13.1
Mc3 18.5 -3.47  67.8
Mc1 21.8 -5.14 -20.4

Degrees of freedom: 84 total; 48 residual
Residual standard error: 1.8

> ## IGNORE_RDIFF_BEGIN
> (fm1CO2.nlme <- nlme(fm1CO2.lis))
Warning in (function (model, data = sys.frame(sys.parent()), fixed, random,  :
  Iteration 1, LME step: nlminb() did not converge (code = 1). Do increase 'msMaxIter'!
Nonlinear mixed-effects model fit by maximum likelihood
  Model: uptake ~ SSasympOff(conc, Asym, lrc, c0) 
  Data: CO2 
  Log-likelihood: -201
  Fixed: list(Asym ~ 1, lrc ~ 1, c0 ~ 1) 
 Asym   lrc    c0 
32.47 -4.64 43.55 

Random effects:
 Formula: list(Asym ~ 1, lrc ~ 1, c0 ~ 1)
 Level: Plant
 Structure: General positive-definite, Log-Cholesky parametrization
         StdDev Corr         
Asym      9.51  Asym   lrc   
lrc       0.13  -0.165       
c0       10.33   0.999 -0.133
Residual  1.77               

Number of Observations: 84
Number of Groups: 12 

> ## IGNORE_RDIFF_END
> (fm2CO2.nlme <- update(fm1CO2.nlme, random = Asym + lrc ~ 1))
Nonlinear mixed-effects model fit by maximum likelihood
  Model: uptake ~ SSasympOff(conc, Asym, lrc, c0) 
  Data: CO2 
  Log-likelihood: -203
  Fixed: list(Asym ~ 1, lrc ~ 1, c0 ~ 1) 
 Asym   lrc    c0 
32.41 -4.56 49.34 

Random effects:
 Formula: list(Asym ~ 1, lrc ~ 1)
 Level: Plant
 Structure: General positive-definite, Log-Cholesky parametrization
         StdDev Corr  
Asym     9.66   Asym  
lrc      0.20   -0.777
Residual 1.81         

Number of Observations: 84
Number of Groups: 12 

> anova(fm1CO2.nlme, fm2CO2.nlme)
            Model df AIC BIC logLik   Test L.Ratio p-value
fm1CO2.nlme     1 10 423 447   -201                       
fm2CO2.nlme     2  7 420 437   -203 1 vs 2     2.9   0.408

> plot(fm2CO2.nlme,id = 0.05,cex = 0.8,adj = -0.5)

> fm2CO2.nlmeRE <- ranef(fm2CO2.nlme, augFrame = TRUE)

> fm2CO2.nlmeRE
       Asym      lrc        Type  Treatment conc uptake
Qn1   6.172  0.04836      Quebec nonchilled  435   33.2
Qn2  10.533 -0.17284      Quebec nonchilled  435   35.2
Qn3  12.218 -0.05799      Quebec nonchilled  435   37.6
Qc1   3.352 -0.07559      Quebec    chilled  435   30.0
Qc3   7.474 -0.19242      Quebec    chilled  435   32.6
Qc2   7.928 -0.18032      Quebec    chilled  435   32.7
Mn3  -4.073  0.03345 Mississippi nonchilled  435   24.1
Mn2  -0.142  0.00565 Mississippi nonchilled  435   27.3
Mn1   0.241 -0.19386 Mississippi nonchilled  435   26.4
Mc2 -18.799  0.31937 Mississippi    chilled  435   12.1
Mc3 -13.117  0.29943 Mississippi    chilled  435   17.3
Mc1 -11.787  0.16676 Mississippi    chilled  435   18.0

> class(fm2CO2.nlmeRE)
[1] "ranef.lme"  "data.frame"

> plot(fm2CO2.nlmeRE, form = ~ Type * Treatment)

> contrasts(CO2$Type)
            [,1]
Quebec        -1
Mississippi    1

> contrasts(CO2$Treatment)
           [,1]
nonchilled   -1
chilled       1

> fm3CO2.nlme <- update(fm2CO2.nlme,
+   fixed = list(Asym ~ Type * Treatment, lrc + c0 ~ 1),
+   start = c(32.412, 0, 0, 0, -4.5603, 49.344))

> summary(fm3CO2.nlme)
Nonlinear mixed-effects model fit by maximum likelihood
  Model: uptake ~ SSasympOff(conc, Asym, lrc, c0) 
  Data: CO2 
  AIC BIC logLik
  394 418   -187

Random effects:
 Formula: list(Asym ~ 1, lrc ~ 1)
 Level: Plant
 Structure: General positive-definite, Log-Cholesky parametrization
                 StdDev Corr  
Asym.(Intercept) 2.930  As.(I)
lrc              0.164  -0.906
Residual         1.850        

Fixed effects:  list(Asym ~ Type * Treatment, lrc + c0 ~ 1) 
                      Value Std.Error DF t-value p-value
Asym.(Intercept)       32.4      0.94 67    34.7  0.0000
Asym.Type1             -7.1      0.60 67   -11.9  0.0000
Asym.Treatment1        -3.8      0.59 67    -6.5  0.0000
Asym.Type1:Treatment1  -1.2      0.59 67    -2.0  0.0462
lrc                    -4.6      0.08 67   -54.1  0.0000
c0                     49.5      4.46 67    11.1  0.0000
 Correlation: 
                      As.(I) Asym.Ty1 Asym.Tr1 A.T1:T lrc   
Asym.Type1            -0.044                                
Asym.Treatment1       -0.021  0.151                         
Asym.Type1:Treatment1 -0.023  0.161    0.225                
lrc                   -0.660  0.202    0.113    0.132       
c0                    -0.113  0.060    0.018    0.063  0.653

Standardized Within-Group Residuals:
    Min      Q1     Med      Q3     Max 
-2.8929 -0.4616 -0.0328  0.5208  2.8877 

Number of Observations: 84
Number of Groups: 12 

> anova(fm3CO2.nlme, Terms = 2:4)
F-test for: Asym.Type, Asym.Treatment, Asym.Type:Treatment 
  numDF denDF F-value p-value
1     3    67    54.8  <.0001

> fm3CO2.nlmeRE <- ranef(fm3CO2.nlme, aug = TRUE)

> plot(fm3CO2.nlmeRE, form = ~ Type * Treatment)

> fm3CO2.fix <- fixef(fm3CO2.nlme)

> fm4CO2.nlme <- update(fm3CO2.nlme,
+   fixed = list(Asym + lrc ~ Type * Treatment, c0 ~ 1),
+   start = c(fm3CO2.fix[1:5], 0, 0, 0, fm3CO2.fix[6]))
Warning in (function (model, data = sys.frame(sys.parent()), fixed, random,  :
  Iteration 1, LME step: nlminb() did not converge (code = 1). Do increase 'msMaxIter'!

> ## IGNORE_RDIFF_BEGIN
> summary(fm4CO2.nlme)
Nonlinear mixed-effects model fit by maximum likelihood
  Model: uptake ~ SSasympOff(conc, Asym, lrc, c0) 
  Data: CO2 
  AIC BIC logLik
  388 420   -181

Random effects:
 Formula: list(Asym ~ 1, lrc ~ 1)
 Level: Plant
 Structure: General positive-definite, Log-Cholesky parametrization
                 StdDev Corr  
Asym.(Intercept) 2.3496 As.(I)
lrc.(Intercept)  0.0796 -0.92 
Residual         1.7920       

Fixed effects:  list(Asym + lrc ~ Type * Treatment, c0 ~ 1) 
                      Value Std.Error DF t-value p-value
Asym.(Intercept)       32.3      0.78 64    41.2  0.0000
Asym.Type1             -8.0      0.78 64   -10.3  0.0000
Asym.Treatment1        -4.2      0.78 64    -5.4  0.0000
Asym.Type1:Treatment1  -2.7      0.78 64    -3.5  0.0008
lrc.(Intercept)        -4.5      0.08 64   -55.7  0.0000
lrc.Type1               0.1      0.06 64     2.4  0.0185
lrc.Treatment1          0.1      0.06 64     1.8  0.0746
lrc.Type1:Treatment1    0.2      0.06 64     3.3  0.0014
c0                     50.5      4.36 64    11.6  0.0000
 Correlation: 
                      As.(I) Asym.Ty1 Asym.Tr1 A.T1:T lr.(I)
Asym.Type1            -0.017                                
Asym.Treatment1       -0.010 -0.017                         
Asym.Type1:Treatment1 -0.020 -0.006   -0.011                
lrc.(Intercept)       -0.471  0.004    0.001    0.009       
lrc.Type1             -0.048 -0.548   -0.005   -0.018  0.402
lrc.Treatment1        -0.031 -0.004   -0.551   -0.033  0.322
lrc.Type1:Treatment1  -0.026 -0.015   -0.032   -0.547  0.351
c0                    -0.133  0.038    0.020    0.019  0.735
                      lrc.Ty1 lrc.Tr1 l.T1:T
Asym.Type1                                  
Asym.Treatment1                             
Asym.Type1:Treatment1                       
lrc.(Intercept)                             
lrc.Type1                                   
lrc.Treatment1         0.375                
lrc.Type1:Treatment1   0.395   0.487        
c0                     0.104   0.083   0.140

Standardized Within-Group Residuals:
    Min      Q1     Med      Q3     Max 
-2.8621 -0.4944 -0.0422  0.5661  3.0405 

Number of Observations: 84
Number of Groups: 12 

> ## IGNORE_RDIFF_END
> fm5CO2.nlme <- update(fm4CO2.nlme, random = Asym ~ 1)

> anova(fm4CO2.nlme, fm5CO2.nlme)
            Model df AIC BIC logLik   Test L.Ratio p-value
fm4CO2.nlme     1 13 388 420   -181                       
fm5CO2.nlme     2 11 387 414   -182 1 vs 2    2.64   0.268

> CO2$type <- 2 * (as.integer(CO2$Type) - 1.5)

> CO2$treatment <- 2 * (as.integer(CO2$Treatment) - 1.5)

> fm1CO2.nls <- nls(uptake ~ SSasympOff(conc, Asym.Intercept +
+   Asym.Type * type + Asym.Treatment * treatment +
+   Asym.TypeTreatment * type * treatment, lrc.Intercept +
+   lrc.Type * type + lrc.Treatment * treatment +
+   lrc.TypeTreatment * type * treatment, c0), data = CO2,
+   start = c(Asym.Intercept = 32.371, Asym.Type = -8.0086,
+     Asym.Treatment = -4.2001, Asym.TypeTreatment = -2.7253,
+     lrc.Intercept = -4.5267, lrc.Type =  0.13112,
+     lrc.Treatment = 0.093928, lrc.TypeTreatment = 0.17941,
+     c0 = 50.126))

> anova(fm5CO2.nlme, fm1CO2.nls)
            Model df AIC BIC logLik   Test L.Ratio p-value
fm5CO2.nlme     1 11 387 414   -182                       
fm1CO2.nls      2 10 418 443   -199 1 vs 2    33.3  <.0001

> # plot(augPred(fm5CO2.nlme, level = 0:1),  ## FIXME: problem with levels
> #      layout = c(6,2))  ## Actually a problem with contrasts.
> ## This fit just ping-pongs.
> #fm1Quin.nlme <-
> #  nlme(conc ~ quinModel(Subject, time, conc, dose, interval,
> #                        lV, lKa, lCl),
> #       data = Quinidine, fixed = lV + lKa + lCl ~ 1,
> #       random = pdDiag(lV + lCl ~ 1), groups =  ~ Subject,
> #       start = list(fixed = c(5, -0.3, 2)),
> #       na.action = NULL, naPattern =  ~ !is.na(conc), verbose = TRUE)
> #fm1Quin.nlme
> #fm1Quin.nlmeRE <- ranef(fm1Quin.nlme, aug = TRUE)
> #fm1Quin.nlmeRE[1:3,]
> # plot(fm1Quin.nlmeRE, form = lCl ~  Age + Smoke + Ethanol +  ## FIXME: problem in max
> #      Weight + Race + Height + glyco + Creatinine + Heart,
> #      control = list(cex.axis = 0.7))
> #fm1Quin.fix <- fixef(fm1Quin.nlme)
> #fm2Quin.nlme <- update(fm1Quin.nlme,
> #  fixed = list(lCl ~ glyco, lKa + lV ~ 1),
> #  start = c(fm1Quin.fix[3], 0, fm1Quin.fix[2:1]))
> fm2Quin.nlme <-
+     nlme(conc ~ quinModel(Subject, time, conc, dose, interval,
+                           lV, lKa, lCl),
+          data = Quinidine, fixed = list(lCl ~ glyco, lV + lKa ~ 1),
+          random = pdDiag(diag(c(0.3,0.3)), form = lV + lCl ~ 1),
+          groups =  ~ Subject,
+          start = list(fixed = c(2.5, 0, 5.4, -0.2)),
+          na.action = NULL, naPattern =  ~ !is.na(conc))

> summary(fm2Quin.nlme)  # wrong values
Nonlinear mixed-effects model fit by maximum likelihood
  Model: conc ~ quinModel(Subject, time, conc, dose, interval, lV, lKa,      lCl) 
  Data: Quinidine 
  AIC BIC logLik
  892 919   -439

Random effects:
 Formula: list(lV ~ 1, lCl ~ 1)
 Level: Subject
 Structure: Diagonal
              lV lCl.(Intercept) Residual
StdDev: 0.000263           0.271    0.651

Fixed effects:  list(lCl ~ glyco, lV + lKa ~ 1) 
                Value Std.Error  DF t-value p-value
lCl.(Intercept)  3.12    0.0655 222    47.7   0.000
lCl.glyco       -0.50    0.0428 222   -11.7   0.000
lV               5.27    0.0948 222    55.6   0.000
lKa             -0.84    0.3039 222    -2.8   0.006
 Correlation: 
          lC.(I) lCl.gl lV    
lCl.glyco -0.880              
lV        -0.072  0.027       
lKa       -0.272  0.149  0.538

Standardized Within-Group Residuals:
    Min      Q1     Med      Q3     Max 
-2.5458 -0.5342 -0.0221  0.5053  3.5016 

Number of Observations: 361
Number of Groups: 136 

> options(contrasts = c("contr.treatment", "contr.poly"))

> fm2Quin.fix <- fixef(fm2Quin.nlme)

> ## subsequent fits don't work
> #fm3Quin.nlme <- update(fm2Quin.nlme,
> #  fixed = list(lCl ~ glyco + Creatinine, lKa + lV ~ 1),
> #  start = c(fm2Quin.fix[1:2], 0.2, fm2Quin.fix[3:4]))
> #summary(fm3Quin.nlme)
> #fm3Quin.fix <- fixef(fm3Quin.nlme)
> #fm4Quin.nlme <- update(fm3Quin.nlme,
> #  fixed = list(lCl ~ glyco + Creatinine + Weight, lKa + lV ~ 1),
> #  start = c(fm3Quin.fix[1:3], 0, fm3Quin.fix[4:5]))
> #summary(fm4Quin.nlme)
> ## This fit just ping-pongs
> ##fm1Wafer.nlmeR <-
> ##    nlme(current ~ A + B * cos(4.5679 * voltage) +
> ##         C * sin(4.5679 * voltage), data = Wafer,
> ##         fixed = list(A ~ voltage + I(voltage^2), B + C ~ 1),
> ##         random = list(Wafer = A ~ voltage + I(voltage^2),
> ##         Site = pdBlocked(list(A~1, A~voltage+I(voltage^2)-1))),
> ###  start = fixef(fm4Wafer), method = "REML", control = list(tolerance=1e-2))
> ##         start = c(-4.255, 5.622, 1.258, -0.09555, 0.10434),
> ##         method = "REML", control = list(tolerance = 1e-2))
> ##fm1Wafer.nlmeR
> ##fm1Wafer.nlme <- update(fm1Wafer.nlmeR, method = "ML")
> 
> (fm2Wafer.nlme <-
+  nlme(current ~ A + B * cos(w * voltage + pi/4),
+       data = Wafer,
+       fixed = list(A ~ voltage + I(voltage^2), B + w ~ 1),
+       random = list(Wafer = pdDiag(list(A ~ voltage + I(voltage^2), B + w ~ 1)),
+       Site = pdDiag(list(A ~ voltage+I(voltage^2), B ~ 1))),
+       start = c(-4.255, 5.622, 1.258, -0.09555, 4.5679)))
Nonlinear mixed-effects model fit by maximum likelihood
  Model: current ~ A + B * cos(w * voltage + pi/4) 
  Data: Wafer 
  Log-likelihood: 663
  Fixed: list(A ~ voltage + I(voltage^2), B + w ~ 1) 
 A.(Intercept)      A.voltage A.I(voltage^2)              B 
        -4.265          5.633          1.256         -0.141 
             w 
         4.593 

Random effects:
 Formula: list(A ~ voltage + I(voltage^2), B ~ 1, w ~ 1)
 Level: Wafer
 Structure: Diagonal
        A.(Intercept) A.voltage A.I(voltage^2)       B        w
StdDev:         0.127     0.337         0.0488 0.00506 5.44e-05

 Formula: list(A ~ voltage + I(voltage^2), B ~ 1)
 Level: Site %in% Wafer
 Structure: Diagonal
        A.(Intercept) A.voltage A.I(voltage^2)        B Residual
StdDev:        0.0618     0.269         0.0559 4.46e-06  0.00786

Number of Observations: 400
Number of Groups: 
          Wafer Site %in% Wafer 
             10              80 

> plot(fm2Wafer.nlme, resid(.) ~ voltage | Wafer,
+      panel = function(x, y, ...) {
+          panel.grid()
+          panel.xyplot(x, y)
+          panel.loess(x, y, lty = 2)
+          panel.abline(0, 0)
+      })

> ## anova(fm1Wafer.nlme, fm2Wafer.nlme, test = FALSE)
> # intervals(fm2Wafer.nlme)
> 
> # 8.3  Extending the Basic nlme Model
> 
> #fm4Theo.nlme <- update(fm3Theo.nlme,
> #   weights = varConstPower(power = 0.1))
> # this fit is way off
> #fm4Theo.nlme
> #anova(fm3Theo.nlme, fm4Theo.nlme)
> #plot(fm4Theo.nlme)
> ## xlim used to hide an unusually high fitted value and enhance
> ## visualization of the heteroscedastic pattern
> # plot(fm4Quin.nlme, xlim = c(0, 6.2))
> #fm5Quin.nlme <- update(fm4Quin.nlme, weights = varPower())
> #summary(fm5Quin.nlme)
> #anova(fm4Quin.nlme, fm5Quin.nlme)
> #plot(fm5Quin.nlme, xlim = c(0, 6.2))
> var.nlme <- nlme(follicles ~ A + B * sin(2 * pi * w * Time) +
+                      C * cos(2 * pi * w *Time), data = Ovary,
+                      fixed = A + B + C + w ~ 1, random = pdDiag(A + B + w ~ 1),
+                                     #  start = c(fixef(fm5Ovar.lme), 1))
+                      start = c(12.18, -3.298, -0.862, 1))

> ##fm1Ovar.nlme
> ##ACF(fm1Ovar.nlme)
> ##plot(ACF(fm1Ovar.nlme,  maxLag = 10), alpha = 0.05)
> ##fm2Ovar.nlme <- update(fm1Ovar.nlme, correlation = corAR1(0.311))
> ##fm3Ovar.nlme <- update(fm1Ovar.nlme, correlation = corARMA(p=0, q=2))
> ##anova(fm2Ovar.nlme, fm3Ovar.nlme, test = FALSE)
> ##intervals(fm2Ovar.nlme)
> ##fm4Ovar.nlme <- update(fm2Ovar.nlme, random = A ~ 1)
> ##anova(fm2Ovar.nlme, fm4Ovar.nlme)
> ##if (interactive()) fm5Ovar.nlme <- update(fm4Ovar.nlme, correlation = corARMA(p=1, q=1))
> # anova(fm4Ovar.nlme, fm5Ovar.nlme)
> # plot(ACF(fm5Ovar.nlme,  maxLag = 10, resType = "n"),
> #        alpha = 0.05)
> # fm5Ovar.lmeML <- update(fm5Ovar.lme, method = "ML")
> # intervals(fm5Ovar.lmeML)
> # fm6Ovar.lmeML <- update(fm5Ovar.lmeML, random = ~1)
> # anova(fm5Ovar.lmeML, fm6Ovar.lmeML)
> # anova(fm6Ovar.lmeML, fm5Ovar.nlme)
> # intervals(fm5Ovar.nlme, which = "fixed")
> fm1Dial.lis <-
+   nlsList(rate ~ SSasympOff(pressure, Asym, lrc, c0) | QB,
+            data = Dialyzer)

> fm1Dial.lis
Call:
  Model: rate ~ SSasympOff(pressure, Asym, lrc, c0) | QB 
   Data: Dialyzer 

Coefficients:
    Asym   lrc    c0
200 45.0 0.765 0.224
300 62.2 0.253 0.225

Degrees of freedom: 140 total; 134 residual
Residual standard error: 3.8

> plot(intervals(fm1Dial.lis))

> fm1Dial.gnls <- gnls(rate ~ SSasympOff(pressure, Asym, lrc, c0),
+   data = Dialyzer, params = list(Asym + lrc ~ QB, c0 ~ 1),
+   start = c(53.6, 8.6, 0.51, -0.26, 0.225))

> fm1Dial.gnls
Generalized nonlinear least squares fit
  Model: rate ~ SSasympOff(pressure, Asym, lrc, c0) 
  Data: Dialyzer 
  Log-likelihood: -383

Coefficients:
Asym.(Intercept)       Asym.QB300  lrc.(Intercept) 
          44.986           17.240            0.766 
       lrc.QB300               c0 
          -0.514            0.224 

Degrees of freedom: 140 total; 135 residual
Residual standard error: 3.79 

> Dialyzer$QBcontr <- 2 * (Dialyzer$QB == 300) - 1

> fm1Dial.nls <-
+   nls(rate ~ SSasympOff(pressure, Asym.Int + Asym.QB * QBcontr,
+   lrc.Int + lrc.QB * QBcontr, c0), data = Dialyzer,
+   start = c(Asym.Int = 53.6, Asym.QB = 8.6, lrc.Int = 0.51,
+   lrc.QB = -0.26, c0 = 0.225))

> ## IGNORE_RDIFF_BEGIN
> summary(fm1Dial.nls)

Formula: rate ~ SSasympOff(pressure, Asym.Int + Asym.QB * QBcontr, lrc.Int + 
    lrc.QB * QBcontr, c0)

Parameters:
         Estimate Std. Error t value Pr(>|t|)    
Asym.Int  53.6065     0.7054   75.99  < 2e-16 ***
Asym.QB    8.6201     0.6792   12.69  < 2e-16 ***
lrc.Int    0.5087     0.0552    9.21  5.5e-16 ***
lrc.QB    -0.2568     0.0450   -5.70  7.0e-08 ***
c0         0.2245     0.0106   21.13  < 2e-16 ***
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Residual standard error: 3.79 on 135 degrees of freedom

Number of iterations to convergence: 4 
Achieved convergence tolerance: 7.24e-06


> ## IGNORE_RDIFF_END
> logLik(fm1Dial.nls)
'log Lik.' -383 (df=6)

> plot(fm1Dial.gnls, resid(.) ~ pressure, abline = 0)

> fm2Dial.gnls <- update(fm1Dial.gnls,
+                        weights = varPower(form = ~ pressure))

> anova(fm1Dial.gnls, fm2Dial.gnls)
             Model df AIC BIC logLik   Test L.Ratio p-value
fm1Dial.gnls     1  6 777 795   -383                       
fm2Dial.gnls     2  7 748 769   -367 1 vs 2    30.8  <.0001

> ACF(fm2Dial.gnls, form = ~ 1 | Subject)
  lag      ACF
1   0  1.00000
2   1  0.71567
3   2  0.50454
4   3  0.29481
5   4  0.20975
6   5  0.13857
7   6 -0.00202

> plot(ACF(fm2Dial.gnls, form = ~ 1 | Subject), alpha = 0.05)

> fm3Dial.gnls <-
+  update(fm2Dial.gnls, corr = corAR1(0.716, form = ~ 1 | Subject))

> fm3Dial.gnls
Generalized nonlinear least squares fit
  Model: rate ~ SSasympOff(pressure, Asym, lrc, c0) 
  Data: Dialyzer 
  Log-likelihood: -323

Coefficients:
Asym.(Intercept)       Asym.QB300  lrc.(Intercept) 
          46.911           16.400            0.542 
       lrc.QB300               c0 
          -0.339            0.215 

Correlation Structure: AR(1)
 Formula: ~1 | Subject 
 Parameter estimate(s):
  Phi 
0.744 
Variance function:
 Structure: Power of variance covariate
 Formula: ~pressure 
 Parameter estimates:
power 
0.572 
Degrees of freedom: 140 total; 135 residual
Residual standard error: 3.18 

> intervals(fm3Dial.gnls)
Approximate 95% confidence intervals

 Coefficients:
                  lower   est.  upper
Asym.(Intercept) 43.877 46.911 49.945
Asym.QB300       11.633 16.400 21.167
lrc.(Intercept)   0.435  0.542  0.648
lrc.QB300        -0.487 -0.339 -0.192
c0                0.206  0.215  0.223

 Correlation structure:
    lower  est. upper
Phi 0.622 0.744 0.831

 Variance function:
      lower  est. upper
power 0.443 0.572 0.702

 Residual standard error:
lower  est. upper 
 2.59  3.13  3.77 

> anova(fm2Dial.gnls, fm3Dial.gnls)
             Model df AIC BIC logLik   Test L.Ratio p-value
fm2Dial.gnls     1  7 748 769   -367                       
fm3Dial.gnls     2  8 661 685   -323 1 vs 2    89.4  <.0001

> # restore two fitted models
> fm2Dial.lme <-
+   lme(rate ~(pressure + I(pressure^2) + I(pressure^3) + I(pressure^4))*QB,
+       Dialyzer, ~ pressure + I(pressure^2),
+       weights = varPower(form = ~ pressure))

> fm2Dial.lmeML <- update(fm2Dial.lme, method = "ML")

> fm3Dial.gls <-
+   gls(rate ~(pressure + I(pressure^2) + I(pressure^3) + I(pressure^4))*QB,
+       Dialyzer, weights = varPower(form = ~ pressure),
+       corr = corAR1(0.771, form = ~ 1 | Subject))

> fm3Dial.glsML <- update(fm3Dial.gls, method = "ML")

> anova( fm2Dial.lmeML, fm3Dial.glsML, fm3Dial.gnls, test = FALSE)
              Model df AIC BIC logLik
fm2Dial.lmeML     1 18 652 705   -308
fm3Dial.glsML     2 13 648 686   -311
fm3Dial.gnls      3  8 661 685   -323

> # cleanup
> 
> summary(warnings())
No warnings
> 
> proc.time()
   user  system elapsed 
 61.627   0.112  61.765