File: scripts.Rout.save

package info (click to toggle)
nlme 3.1.168-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 3,732 kB
  • sloc: ansic: 3,048; fortran: 393; makefile: 2
file content (7823 lines) | stat: -rw-r--r-- 230,589 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5162
5163
5164
5165
5166
5167
5168
5169
5170
5171
5172
5173
5174
5175
5176
5177
5178
5179
5180
5181
5182
5183
5184
5185
5186
5187
5188
5189
5190
5191
5192
5193
5194
5195
5196
5197
5198
5199
5200
5201
5202
5203
5204
5205
5206
5207
5208
5209
5210
5211
5212
5213
5214
5215
5216
5217
5218
5219
5220
5221
5222
5223
5224
5225
5226
5227
5228
5229
5230
5231
5232
5233
5234
5235
5236
5237
5238
5239
5240
5241
5242
5243
5244
5245
5246
5247
5248
5249
5250
5251
5252
5253
5254
5255
5256
5257
5258
5259
5260
5261
5262
5263
5264
5265
5266
5267
5268
5269
5270
5271
5272
5273
5274
5275
5276
5277
5278
5279
5280
5281
5282
5283
5284
5285
5286
5287
5288
5289
5290
5291
5292
5293
5294
5295
5296
5297
5298
5299
5300
5301
5302
5303
5304
5305
5306
5307
5308
5309
5310
5311
5312
5313
5314
5315
5316
5317
5318
5319
5320
5321
5322
5323
5324
5325
5326
5327
5328
5329
5330
5331
5332
5333
5334
5335
5336
5337
5338
5339
5340
5341
5342
5343
5344
5345
5346
5347
5348
5349
5350
5351
5352
5353
5354
5355
5356
5357
5358
5359
5360
5361
5362
5363
5364
5365
5366
5367
5368
5369
5370
5371
5372
5373
5374
5375
5376
5377
5378
5379
5380
5381
5382
5383
5384
5385
5386
5387
5388
5389
5390
5391
5392
5393
5394
5395
5396
5397
5398
5399
5400
5401
5402
5403
5404
5405
5406
5407
5408
5409
5410
5411
5412
5413
5414
5415
5416
5417
5418
5419
5420
5421
5422
5423
5424
5425
5426
5427
5428
5429
5430
5431
5432
5433
5434
5435
5436
5437
5438
5439
5440
5441
5442
5443
5444
5445
5446
5447
5448
5449
5450
5451
5452
5453
5454
5455
5456
5457
5458
5459
5460
5461
5462
5463
5464
5465
5466
5467
5468
5469
5470
5471
5472
5473
5474
5475
5476
5477
5478
5479
5480
5481
5482
5483
5484
5485
5486
5487
5488
5489
5490
5491
5492
5493
5494
5495
5496
5497
5498
5499
5500
5501
5502
5503
5504
5505
5506
5507
5508
5509
5510
5511
5512
5513
5514
5515
5516
5517
5518
5519
5520
5521
5522
5523
5524
5525
5526
5527
5528
5529
5530
5531
5532
5533
5534
5535
5536
5537
5538
5539
5540
5541
5542
5543
5544
5545
5546
5547
5548
5549
5550
5551
5552
5553
5554
5555
5556
5557
5558
5559
5560
5561
5562
5563
5564
5565
5566
5567
5568
5569
5570
5571
5572
5573
5574
5575
5576
5577
5578
5579
5580
5581
5582
5583
5584
5585
5586
5587
5588
5589
5590
5591
5592
5593
5594
5595
5596
5597
5598
5599
5600
5601
5602
5603
5604
5605
5606
5607
5608
5609
5610
5611
5612
5613
5614
5615
5616
5617
5618
5619
5620
5621
5622
5623
5624
5625
5626
5627
5628
5629
5630
5631
5632
5633
5634
5635
5636
5637
5638
5639
5640
5641
5642
5643
5644
5645
5646
5647
5648
5649
5650
5651
5652
5653
5654
5655
5656
5657
5658
5659
5660
5661
5662
5663
5664
5665
5666
5667
5668
5669
5670
5671
5672
5673
5674
5675
5676
5677
5678
5679
5680
5681
5682
5683
5684
5685
5686
5687
5688
5689
5690
5691
5692
5693
5694
5695
5696
5697
5698
5699
5700
5701
5702
5703
5704
5705
5706
5707
5708
5709
5710
5711
5712
5713
5714
5715
5716
5717
5718
5719
5720
5721
5722
5723
5724
5725
5726
5727
5728
5729
5730
5731
5732
5733
5734
5735
5736
5737
5738
5739
5740
5741
5742
5743
5744
5745
5746
5747
5748
5749
5750
5751
5752
5753
5754
5755
5756
5757
5758
5759
5760
5761
5762
5763
5764
5765
5766
5767
5768
5769
5770
5771
5772
5773
5774
5775
5776
5777
5778
5779
5780
5781
5782
5783
5784
5785
5786
5787
5788
5789
5790
5791
5792
5793
5794
5795
5796
5797
5798
5799
5800
5801
5802
5803
5804
5805
5806
5807
5808
5809
5810
5811
5812
5813
5814
5815
5816
5817
5818
5819
5820
5821
5822
5823
5824
5825
5826
5827
5828
5829
5830
5831
5832
5833
5834
5835
5836
5837
5838
5839
5840
5841
5842
5843
5844
5845
5846
5847
5848
5849
5850
5851
5852
5853
5854
5855
5856
5857
5858
5859
5860
5861
5862
5863
5864
5865
5866
5867
5868
5869
5870
5871
5872
5873
5874
5875
5876
5877
5878
5879
5880
5881
5882
5883
5884
5885
5886
5887
5888
5889
5890
5891
5892
5893
5894
5895
5896
5897
5898
5899
5900
5901
5902
5903
5904
5905
5906
5907
5908
5909
5910
5911
5912
5913
5914
5915
5916
5917
5918
5919
5920
5921
5922
5923
5924
5925
5926
5927
5928
5929
5930
5931
5932
5933
5934
5935
5936
5937
5938
5939
5940
5941
5942
5943
5944
5945
5946
5947
5948
5949
5950
5951
5952
5953
5954
5955
5956
5957
5958
5959
5960
5961
5962
5963
5964
5965
5966
5967
5968
5969
5970
5971
5972
5973
5974
5975
5976
5977
5978
5979
5980
5981
5982
5983
5984
5985
5986
5987
5988
5989
5990
5991
5992
5993
5994
5995
5996
5997
5998
5999
6000
6001
6002
6003
6004
6005
6006
6007
6008
6009
6010
6011
6012
6013
6014
6015
6016
6017
6018
6019
6020
6021
6022
6023
6024
6025
6026
6027
6028
6029
6030
6031
6032
6033
6034
6035
6036
6037
6038
6039
6040
6041
6042
6043
6044
6045
6046
6047
6048
6049
6050
6051
6052
6053
6054
6055
6056
6057
6058
6059
6060
6061
6062
6063
6064
6065
6066
6067
6068
6069
6070
6071
6072
6073
6074
6075
6076
6077
6078
6079
6080
6081
6082
6083
6084
6085
6086
6087
6088
6089
6090
6091
6092
6093
6094
6095
6096
6097
6098
6099
6100
6101
6102
6103
6104
6105
6106
6107
6108
6109
6110
6111
6112
6113
6114
6115
6116
6117
6118
6119
6120
6121
6122
6123
6124
6125
6126
6127
6128
6129
6130
6131
6132
6133
6134
6135
6136
6137
6138
6139
6140
6141
6142
6143
6144
6145
6146
6147
6148
6149
6150
6151
6152
6153
6154
6155
6156
6157
6158
6159
6160
6161
6162
6163
6164
6165
6166
6167
6168
6169
6170
6171
6172
6173
6174
6175
6176
6177
6178
6179
6180
6181
6182
6183
6184
6185
6186
6187
6188
6189
6190
6191
6192
6193
6194
6195
6196
6197
6198
6199
6200
6201
6202
6203
6204
6205
6206
6207
6208
6209
6210
6211
6212
6213
6214
6215
6216
6217
6218
6219
6220
6221
6222
6223
6224
6225
6226
6227
6228
6229
6230
6231
6232
6233
6234
6235
6236
6237
6238
6239
6240
6241
6242
6243
6244
6245
6246
6247
6248
6249
6250
6251
6252
6253
6254
6255
6256
6257
6258
6259
6260
6261
6262
6263
6264
6265
6266
6267
6268
6269
6270
6271
6272
6273
6274
6275
6276
6277
6278
6279
6280
6281
6282
6283
6284
6285
6286
6287
6288
6289
6290
6291
6292
6293
6294
6295
6296
6297
6298
6299
6300
6301
6302
6303
6304
6305
6306
6307
6308
6309
6310
6311
6312
6313
6314
6315
6316
6317
6318
6319
6320
6321
6322
6323
6324
6325
6326
6327
6328
6329
6330
6331
6332
6333
6334
6335
6336
6337
6338
6339
6340
6341
6342
6343
6344
6345
6346
6347
6348
6349
6350
6351
6352
6353
6354
6355
6356
6357
6358
6359
6360
6361
6362
6363
6364
6365
6366
6367
6368
6369
6370
6371
6372
6373
6374
6375
6376
6377
6378
6379
6380
6381
6382
6383
6384
6385
6386
6387
6388
6389
6390
6391
6392
6393
6394
6395
6396
6397
6398
6399
6400
6401
6402
6403
6404
6405
6406
6407
6408
6409
6410
6411
6412
6413
6414
6415
6416
6417
6418
6419
6420
6421
6422
6423
6424
6425
6426
6427
6428
6429
6430
6431
6432
6433
6434
6435
6436
6437
6438
6439
6440
6441
6442
6443
6444
6445
6446
6447
6448
6449
6450
6451
6452
6453
6454
6455
6456
6457
6458
6459
6460
6461
6462
6463
6464
6465
6466
6467
6468
6469
6470
6471
6472
6473
6474
6475
6476
6477
6478
6479
6480
6481
6482
6483
6484
6485
6486
6487
6488
6489
6490
6491
6492
6493
6494
6495
6496
6497
6498
6499
6500
6501
6502
6503
6504
6505
6506
6507
6508
6509
6510
6511
6512
6513
6514
6515
6516
6517
6518
6519
6520
6521
6522
6523
6524
6525
6526
6527
6528
6529
6530
6531
6532
6533
6534
6535
6536
6537
6538
6539
6540
6541
6542
6543
6544
6545
6546
6547
6548
6549
6550
6551
6552
6553
6554
6555
6556
6557
6558
6559
6560
6561
6562
6563
6564
6565
6566
6567
6568
6569
6570
6571
6572
6573
6574
6575
6576
6577
6578
6579
6580
6581
6582
6583
6584
6585
6586
6587
6588
6589
6590
6591
6592
6593
6594
6595
6596
6597
6598
6599
6600
6601
6602
6603
6604
6605
6606
6607
6608
6609
6610
6611
6612
6613
6614
6615
6616
6617
6618
6619
6620
6621
6622
6623
6624
6625
6626
6627
6628
6629
6630
6631
6632
6633
6634
6635
6636
6637
6638
6639
6640
6641
6642
6643
6644
6645
6646
6647
6648
6649
6650
6651
6652
6653
6654
6655
6656
6657
6658
6659
6660
6661
6662
6663
6664
6665
6666
6667
6668
6669
6670
6671
6672
6673
6674
6675
6676
6677
6678
6679
6680
6681
6682
6683
6684
6685
6686
6687
6688
6689
6690
6691
6692
6693
6694
6695
6696
6697
6698
6699
6700
6701
6702
6703
6704
6705
6706
6707
6708
6709
6710
6711
6712
6713
6714
6715
6716
6717
6718
6719
6720
6721
6722
6723
6724
6725
6726
6727
6728
6729
6730
6731
6732
6733
6734
6735
6736
6737
6738
6739
6740
6741
6742
6743
6744
6745
6746
6747
6748
6749
6750
6751
6752
6753
6754
6755
6756
6757
6758
6759
6760
6761
6762
6763
6764
6765
6766
6767
6768
6769
6770
6771
6772
6773
6774
6775
6776
6777
6778
6779
6780
6781
6782
6783
6784
6785
6786
6787
6788
6789
6790
6791
6792
6793
6794
6795
6796
6797
6798
6799
6800
6801
6802
6803
6804
6805
6806
6807
6808
6809
6810
6811
6812
6813
6814
6815
6816
6817
6818
6819
6820
6821
6822
6823
6824
6825
6826
6827
6828
6829
6830
6831
6832
6833
6834
6835
6836
6837
6838
6839
6840
6841
6842
6843
6844
6845
6846
6847
6848
6849
6850
6851
6852
6853
6854
6855
6856
6857
6858
6859
6860
6861
6862
6863
6864
6865
6866
6867
6868
6869
6870
6871
6872
6873
6874
6875
6876
6877
6878
6879
6880
6881
6882
6883
6884
6885
6886
6887
6888
6889
6890
6891
6892
6893
6894
6895
6896
6897
6898
6899
6900
6901
6902
6903
6904
6905
6906
6907
6908
6909
6910
6911
6912
6913
6914
6915
6916
6917
6918
6919
6920
6921
6922
6923
6924
6925
6926
6927
6928
6929
6930
6931
6932
6933
6934
6935
6936
6937
6938
6939
6940
6941
6942
6943
6944
6945
6946
6947
6948
6949
6950
6951
6952
6953
6954
6955
6956
6957
6958
6959
6960
6961
6962
6963
6964
6965
6966
6967
6968
6969
6970
6971
6972
6973
6974
6975
6976
6977
6978
6979
6980
6981
6982
6983
6984
6985
6986
6987
6988
6989
6990
6991
6992
6993
6994
6995
6996
6997
6998
6999
7000
7001
7002
7003
7004
7005
7006
7007
7008
7009
7010
7011
7012
7013
7014
7015
7016
7017
7018
7019
7020
7021
7022
7023
7024
7025
7026
7027
7028
7029
7030
7031
7032
7033
7034
7035
7036
7037
7038
7039
7040
7041
7042
7043
7044
7045
7046
7047
7048
7049
7050
7051
7052
7053
7054
7055
7056
7057
7058
7059
7060
7061
7062
7063
7064
7065
7066
7067
7068
7069
7070
7071
7072
7073
7074
7075
7076
7077
7078
7079
7080
7081
7082
7083
7084
7085
7086
7087
7088
7089
7090
7091
7092
7093
7094
7095
7096
7097
7098
7099
7100
7101
7102
7103
7104
7105
7106
7107
7108
7109
7110
7111
7112
7113
7114
7115
7116
7117
7118
7119
7120
7121
7122
7123
7124
7125
7126
7127
7128
7129
7130
7131
7132
7133
7134
7135
7136
7137
7138
7139
7140
7141
7142
7143
7144
7145
7146
7147
7148
7149
7150
7151
7152
7153
7154
7155
7156
7157
7158
7159
7160
7161
7162
7163
7164
7165
7166
7167
7168
7169
7170
7171
7172
7173
7174
7175
7176
7177
7178
7179
7180
7181
7182
7183
7184
7185
7186
7187
7188
7189
7190
7191
7192
7193
7194
7195
7196
7197
7198
7199
7200
7201
7202
7203
7204
7205
7206
7207
7208
7209
7210
7211
7212
7213
7214
7215
7216
7217
7218
7219
7220
7221
7222
7223
7224
7225
7226
7227
7228
7229
7230
7231
7232
7233
7234
7235
7236
7237
7238
7239
7240
7241
7242
7243
7244
7245
7246
7247
7248
7249
7250
7251
7252
7253
7254
7255
7256
7257
7258
7259
7260
7261
7262
7263
7264
7265
7266
7267
7268
7269
7270
7271
7272
7273
7274
7275
7276
7277
7278
7279
7280
7281
7282
7283
7284
7285
7286
7287
7288
7289
7290
7291
7292
7293
7294
7295
7296
7297
7298
7299
7300
7301
7302
7303
7304
7305
7306
7307
7308
7309
7310
7311
7312
7313
7314
7315
7316
7317
7318
7319
7320
7321
7322
7323
7324
7325
7326
7327
7328
7329
7330
7331
7332
7333
7334
7335
7336
7337
7338
7339
7340
7341
7342
7343
7344
7345
7346
7347
7348
7349
7350
7351
7352
7353
7354
7355
7356
7357
7358
7359
7360
7361
7362
7363
7364
7365
7366
7367
7368
7369
7370
7371
7372
7373
7374
7375
7376
7377
7378
7379
7380
7381
7382
7383
7384
7385
7386
7387
7388
7389
7390
7391
7392
7393
7394
7395
7396
7397
7398
7399
7400
7401
7402
7403
7404
7405
7406
7407
7408
7409
7410
7411
7412
7413
7414
7415
7416
7417
7418
7419
7420
7421
7422
7423
7424
7425
7426
7427
7428
7429
7430
7431
7432
7433
7434
7435
7436
7437
7438
7439
7440
7441
7442
7443
7444
7445
7446
7447
7448
7449
7450
7451
7452
7453
7454
7455
7456
7457
7458
7459
7460
7461
7462
7463
7464
7465
7466
7467
7468
7469
7470
7471
7472
7473
7474
7475
7476
7477
7478
7479
7480
7481
7482
7483
7484
7485
7486
7487
7488
7489
7490
7491
7492
7493
7494
7495
7496
7497
7498
7499
7500
7501
7502
7503
7504
7505
7506
7507
7508
7509
7510
7511
7512
7513
7514
7515
7516
7517
7518
7519
7520
7521
7522
7523
7524
7525
7526
7527
7528
7529
7530
7531
7532
7533
7534
7535
7536
7537
7538
7539
7540
7541
7542
7543
7544
7545
7546
7547
7548
7549
7550
7551
7552
7553
7554
7555
7556
7557
7558
7559
7560
7561
7562
7563
7564
7565
7566
7567
7568
7569
7570
7571
7572
7573
7574
7575
7576
7577
7578
7579
7580
7581
7582
7583
7584
7585
7586
7587
7588
7589
7590
7591
7592
7593
7594
7595
7596
7597
7598
7599
7600
7601
7602
7603
7604
7605
7606
7607
7608
7609
7610
7611
7612
7613
7614
7615
7616
7617
7618
7619
7620
7621
7622
7623
7624
7625
7626
7627
7628
7629
7630
7631
7632
7633
7634
7635
7636
7637
7638
7639
7640
7641
7642
7643
7644
7645
7646
7647
7648
7649
7650
7651
7652
7653
7654
7655
7656
7657
7658
7659
7660
7661
7662
7663
7664
7665
7666
7667
7668
7669
7670
7671
7672
7673
7674
7675
7676
7677
7678
7679
7680
7681
7682
7683
7684
7685
7686
7687
7688
7689
7690
7691
7692
7693
7694
7695
7696
7697
7698
7699
7700
7701
7702
7703
7704
7705
7706
7707
7708
7709
7710
7711
7712
7713
7714
7715
7716
7717
7718
7719
7720
7721
7722
7723
7724
7725
7726
7727
7728
7729
7730
7731
7732
7733
7734
7735
7736
7737
7738
7739
7740
7741
7742
7743
7744
7745
7746
7747
7748
7749
7750
7751
7752
7753
7754
7755
7756
7757
7758
7759
7760
7761
7762
7763
7764
7765
7766
7767
7768
7769
7770
7771
7772
7773
7774
7775
7776
7777
7778
7779
7780
7781
7782
7783
7784
7785
7786
7787
7788
7789
7790
7791
7792
7793
7794
7795
7796
7797
7798
7799
7800
7801
7802
7803
7804
7805
7806
7807
7808
7809
7810
7811
7812
7813
7814
7815
7816
7817
7818
7819
7820
7821
7822
7823

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