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
|
R version 2.11.0 alpha (2010-03-28 r51461)
Copyright (C) 2010 The R Foundation for Statistical Computing
ISBN 3-900051-07-0
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.
Natural language support but running in an English locale
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.
> pkgname <- "boot"
> source(file.path(R.home("share"), "R", "examples-header.R"))
> options(warn = 1)
> library('boot')
>
> assign(".oldSearch", search(), pos = 'CheckExEnv')
> cleanEx()
> nameEx("Imp.Estimates")
> ### * Imp.Estimates
>
> flush(stderr()); flush(stdout())
>
> ### Name: Imp.Estimates
> ### Title: Importance Sampling Estimates
> ### Aliases: Imp.Estimates imp.moments imp.prob imp.quantile imp.reg
> ### Keywords: htest nonparametric
>
> ### ** Examples
>
> # Example 9.8 of Davison and Hinkley (1997) requires tilting the
> # resampling distribution of the studentized statistic to be centred
> # at the observed value of the test statistic, 1.84. In this example
> # we show how certain estimates can be found using resamples taken from
> # the tilted distribution.
> grav1 <- gravity[as.numeric(gravity[,2])>=7,]
> grav.fun <- function(dat, w, orig)
+ { strata <- tapply(dat[, 2], as.numeric(dat[, 2]))
+ d <- dat[, 1]
+ ns <- tabulate(strata)
+ w <- w/tapply(w, strata, sum)[strata]
+ mns <- tapply(d * w, strata, sum)
+ mn2 <- tapply(d * d * w, strata, sum)
+ s2hat <- sum((mn2 - mns^2)/ns)
+ as.vector(c(mns[2]-mns[1],s2hat,(mns[2]-mns[1]-orig)/sqrt(s2hat)))
+ }
> grav.z0 <- grav.fun(grav1,rep(1,26),0)
> grav.L <- empinf(data=grav1, statistic=grav.fun, stype="w",
+ strata=grav1[,2], index=3, orig=grav.z0[1])
> grav.tilt <- exp.tilt(grav.L,grav.z0[3],strata=grav1[,2])
> grav.tilt.boot <- boot(grav1, grav.fun, R=199, stype="w",
+ strata=grav1[,2], weights=grav.tilt$p,
+ orig=grav.z0[1])
> # Since the weights are needed for all calculations, we shall calculate
> # them once only.
> grav.w <- imp.weights(grav.tilt.boot)
> grav.mom <- imp.moments(grav.tilt.boot, w=grav.w, index=3)
> grav.p <- imp.prob(grav.tilt.boot, w=grav.w, index=3, t0=grav.z0[3])
> grav.q <- imp.quantile(grav.tilt.boot, w=grav.w, index=3,
+ alpha=c(0.9,0.95,0.975,0.99))
>
>
>
> cleanEx()
> nameEx("abc.ci")
> ### * abc.ci
>
> flush(stderr()); flush(stdout())
>
> ### Name: abc.ci
> ### Title: Nonparametric ABC Confidence Intervals
> ### Aliases: abc.ci
> ### Keywords: nonparametric htest
>
> ### ** Examples
>
> # 90% and 95% confidence intervals for the correlation
> # coefficient between the columns of the bigcity data
>
> abc.ci(bigcity, corr, conf=c(0.90,0.95))
conf
[1,] 0.90 0.9581503 0.9917271
[2,] 0.95 0.9493699 0.9930713
>
> # A 95% confidence interval for the difference between the means of
> # the last two samples in gravity
> mean.diff <- function(y, w)
+ { gp1 <- 1:table(as.numeric(y$series))[1]
+ sum(y[gp1,1] * w[gp1]) - sum(y[-gp1,1] * w[-gp1])
+ }
> grav1 <- gravity[as.numeric(gravity[,2])>=7,]
> abc.ci(grav1, mean.diff, strata=grav1$series)
[1] 0.9500000 -6.7075791 -0.3939377
>
>
>
> cleanEx()
> nameEx("boot")
> ### * boot
>
> flush(stderr()); flush(stdout())
>
> ### Name: boot
> ### Title: Bootstrap Resampling
> ### Aliases: boot boot.return
> ### Keywords: nonparametric htest
>
> ### ** Examples
>
> # usual bootstrap of the ratio of means using the city data
> ratio <- function(d, w)
+ sum(d$x * w)/sum(d$u * w)
> boot(city, ratio, R=999, stype="w")
ORDINARY NONPARAMETRIC BOOTSTRAP
Call:
boot(data = city, statistic = ratio, R = 999, stype = "w")
Bootstrap Statistics :
original bias std. error
t1* 1.520313 0.03959996 0.2167146
>
>
> # Stratified resampling for the difference of means. In this
> # example we will look at the difference of means between the final
> # two series in the gravity data.
> diff.means <- function(d, f)
+ { n <- nrow(d)
+ gp1 <- 1:table(as.numeric(d$series))[1]
+ m1 <- sum(d[gp1,1] * f[gp1])/sum(f[gp1])
+ m2 <- sum(d[-gp1,1] * f[-gp1])/sum(f[-gp1])
+ ss1 <- sum(d[gp1,1]^2 * f[gp1]) -
+ (m1 * m1 * sum(f[gp1]))
+ ss2 <- sum(d[-gp1,1]^2 * f[-gp1]) -
+ (m2 * m2 * sum(f[-gp1]))
+ c(m1-m2, (ss1+ss2)/(sum(f)-2))
+ }
> grav1 <- gravity[as.numeric(gravity[,2])>=7,]
> boot(grav1, diff.means, R=999, stype="f", strata=grav1[,2])
STRATIFIED BOOTSTRAP
Call:
boot(data = grav1, statistic = diff.means, R = 999, stype = "f",
strata = grav1[, 2])
Bootstrap Statistics :
original bias std. error
t1* -2.846154 0.002541003 1.546664
t2* 16.846154 -1.457662791 6.759103
>
>
> # In this example we show the use of boot in a prediction from
> # regression based on the nuclear data. This example is taken
> # from Example 6.8 of Davison and Hinkley (1997). Notice also
> # that two extra arguments to statistic are passed through boot.
> nuke <- nuclear[,c(1,2,5,7,8,10,11)]
> nuke.lm <- glm(log(cost)~date+log(cap)+ne+ ct+log(cum.n)+pt, data=nuke)
> nuke.diag <- glm.diag(nuke.lm)
> nuke.res <- nuke.diag$res*nuke.diag$sd
> nuke.res <- nuke.res-mean(nuke.res)
>
>
> # We set up a new data frame with the data, the standardized
> # residuals and the fitted values for use in the bootstrap.
> nuke.data <- data.frame(nuke,resid=nuke.res,fit=fitted(nuke.lm))
>
>
> # Now we want a prediction of plant number 32 but at date 73.00
> new.data <- data.frame(cost=1, date=73.00, cap=886, ne=0,
+ ct=0, cum.n=11, pt=1)
> new.fit <- predict(nuke.lm, new.data)
>
>
> nuke.fun <- function(dat, inds, i.pred, fit.pred, x.pred)
+ {
+ assign(".inds", inds, envir=.GlobalEnv)
+ lm.b <- glm(fit+resid[.inds] ~date+log(cap)+ne+ct+
+ log(cum.n)+pt, data=dat)
+ pred.b <- predict(lm.b,x.pred)
+ remove(".inds", envir=.GlobalEnv)
+ c(coef(lm.b), pred.b-(fit.pred+dat$resid[i.pred]))
+ }
>
>
> nuke.boot <- boot(nuke.data, nuke.fun, R=999, m=1,
+ fit.pred=new.fit, x.pred=new.data)
> # The bootstrap prediction error would then be found by
> mean(nuke.boot$t[,8]^2)
[1] 0.08815734
> # Basic bootstrap prediction limits would be
> new.fit-sort(nuke.boot$t[,8])[c(975,25)]
[1] 6.160255 7.298819
>
>
>
>
> # Finally a parametric bootstrap. For this example we shall look
> # at the air-conditioning data. In this example our aim is to test
> # the hypothesis that the true value of the index is 1 (i.e. that
> # the data come from an exponential distribution) against the
> # alternative that the data come from a gamma distribution with
> # index not equal to 1.
> air.fun <- function(data)
+ { ybar <- mean(data$hours)
+ para <- c(log(ybar),mean(log(data$hours)))
+ ll <- function(k) {
+ if (k <= 0) out <- 1e200 # not NA
+ else out <- lgamma(k)-k*(log(k)-1-para[1]+para[2])
+ out
+ }
+ khat <- nlm(ll,ybar^2/var(data$hours))$estimate
+ c(ybar, khat)
+ }
>
>
> air.rg <- function(data, mle)
+ # Function to generate random exponential variates. mle will contain
+ # the mean of the original data
+ { out <- data
+ out$hours <- rexp(nrow(out), 1/mle)
+ out
+ }
>
> air.boot <- boot(aircondit, air.fun, R=999, sim="parametric",
+ ran.gen=air.rg, mle=mean(aircondit$hours))
>
>
> # The bootstrap p-value can then be approximated by
> sum(abs(air.boot$t[,2]-1) > abs(air.boot$t0[2]-1))/(1+air.boot$R)
[1] 0.461
>
>
>
> cleanEx()
> nameEx("boot.array")
> ### * boot.array
>
> flush(stderr()); flush(stdout())
>
> ### Name: boot.array
> ### Title: Bootstrap Resampling Arrays
> ### Aliases: boot.array
> ### Keywords: nonparametric
>
> ### ** Examples
>
> # A frequency array for a nonparametric bootstrap
> city.boot <- boot(city, corr, R=40, stype="w")
> boot.array(city.boot)
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
[1,] 1 1 3 1 1 0 1 0 1 1
[2,] 0 0 1 3 1 2 1 1 1 0
[3,] 0 0 0 1 3 2 0 1 2 1
[4,] 0 2 2 2 0 1 0 1 0 2
[5,] 1 1 1 0 1 2 0 2 2 0
[6,] 0 1 2 0 2 1 0 1 2 1
[7,] 3 0 0 1 1 2 0 1 1 1
[8,] 0 4 1 1 1 0 1 2 0 0
[9,] 0 1 4 0 1 0 2 2 0 0
[10,] 1 1 1 1 1 1 1 2 0 1
[11,] 0 0 3 0 2 1 1 1 0 2
[12,] 2 3 1 0 0 1 0 0 2 1
[13,] 1 0 0 1 2 0 2 1 1 2
[14,] 0 0 2 3 0 0 2 0 2 1
[15,] 2 0 1 1 1 1 0 2 1 1
[16,] 1 1 0 2 2 1 1 1 1 0
[17,] 1 0 1 2 2 1 2 1 0 0
[18,] 0 2 0 0 2 2 0 1 1 2
[19,] 1 1 0 2 0 1 2 0 1 2
[20,] 0 0 0 1 2 1 1 1 0 4
[21,] 0 0 2 0 1 1 3 0 0 3
[22,] 1 3 2 2 0 1 1 0 0 0
[23,] 0 0 3 1 2 1 2 0 1 0
[24,] 0 1 1 2 1 2 0 1 0 2
[25,] 0 0 2 1 0 0 3 2 1 1
[26,] 0 2 4 1 1 1 0 0 0 1
[27,] 2 3 1 0 2 0 0 2 0 0
[28,] 1 1 0 1 1 0 0 4 2 0
[29,] 2 2 0 1 1 0 1 0 1 2
[30,] 0 1 0 1 1 2 0 1 3 1
[31,] 1 2 0 2 2 0 1 1 0 1
[32,] 1 4 0 1 0 2 0 1 1 0
[33,] 0 1 0 5 1 0 0 1 1 1
[34,] 0 2 0 1 3 1 1 1 0 1
[35,] 0 2 3 1 1 1 0 0 1 1
[36,] 1 2 1 1 1 1 2 0 1 0
[37,] 1 1 1 0 0 1 2 2 2 0
[38,] 2 3 0 3 0 1 0 0 1 0
[39,] 0 0 1 2 3 0 0 2 1 1
[40,] 0 1 1 0 3 0 2 2 0 1
>
> perm.cor <- function(d,i)
+ cor(d$x,d$u[i])
> city.perm <- boot(city, perm.cor, R=40, sim="permutation")
> boot.array(city.perm, indices=TRUE)
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
[1,] 7 2 8 10 6 4 9 3 1 5
[2,] 10 4 6 8 5 3 2 1 9 7
[3,] 5 10 6 2 3 8 1 9 7 4
[4,] 10 1 4 8 9 5 3 6 7 2
[5,] 1 3 5 8 2 9 6 7 10 4
[6,] 10 4 3 2 1 9 8 5 7 6
[7,] 2 5 1 4 10 9 3 8 7 6
[8,] 5 9 6 3 1 2 4 10 8 7
[9,] 8 2 7 10 4 3 1 6 9 5
[10,] 1 9 3 2 8 6 7 4 5 10
[11,] 6 7 10 5 3 9 2 8 4 1
[12,] 9 5 1 6 10 8 3 2 7 4
[13,] 9 7 3 1 8 5 4 6 2 10
[14,] 7 1 3 2 9 6 10 4 5 8
[15,] 4 9 6 3 2 1 10 8 7 5
[16,] 9 6 1 7 5 2 8 4 10 3
[17,] 7 2 3 6 10 4 1 9 5 8
[18,] 2 4 3 1 5 8 6 10 9 7
[19,] 4 9 6 1 10 3 7 2 5 8
[20,] 6 3 7 5 8 4 2 10 1 9
[21,] 9 10 2 6 7 5 8 4 3 1
[22,] 10 4 5 9 8 3 1 2 6 7
[23,] 9 2 5 1 10 4 6 8 7 3
[24,] 9 4 3 6 8 2 1 10 5 7
[25,] 10 3 8 2 5 7 1 9 6 4
[26,] 7 8 3 9 4 1 5 2 10 6
[27,] 4 8 1 5 3 6 10 9 7 2
[28,] 8 5 7 4 10 3 2 1 9 6
[29,] 8 10 2 4 7 3 9 6 1 5
[30,] 1 5 7 9 3 6 4 10 2 8
[31,] 10 9 7 5 4 1 2 6 3 8
[32,] 8 5 1 6 3 7 10 4 9 2
[33,] 1 6 8 5 2 7 9 4 10 3
[34,] 8 5 7 1 9 2 6 3 10 4
[35,] 4 5 9 2 7 6 8 3 1 10
[36,] 5 9 10 1 3 7 2 8 4 6
[37,] 8 2 7 9 10 1 3 4 6 5
[38,] 5 8 1 7 4 3 9 10 6 2
[39,] 10 7 6 4 8 1 3 5 9 2
[40,] 2 1 8 3 4 6 9 10 7 5
>
>
>
> cleanEx()
> nameEx("boot.ci")
> ### * boot.ci
>
> flush(stderr()); flush(stdout())
>
> ### Name: boot.ci
> ### Title: Nonparametric Bootstrap Confidence Intervals
> ### Aliases: boot.ci
> ### Keywords: nonparametric htest
>
> ### ** Examples
>
> # confidence intervals for the city data
> ratio <- function(d, w) sum(d$x * w)/sum(d$u * w)
> city.boot <- boot(city, ratio, R = 999, stype = "w",sim = "ordinary")
> boot.ci(city.boot, conf = c(0.90,0.95),
+ type = c("norm","basic","perc","bca"))
BOOTSTRAP CONFIDENCE INTERVAL CALCULATIONS
Based on 999 bootstrap replicates
CALL :
boot.ci(boot.out = city.boot, conf = c(0.9, 0.95), type = c("norm",
"basic", "perc", "bca"))
Intervals :
Level Normal Basic
90% ( 1.124, 1.837 ) ( 1.059, 1.740 )
95% ( 1.056, 1.905 ) ( 0.932, 1.799 )
Level Percentile BCa
90% ( 1.301, 1.982 ) ( 1.301, 1.984 )
95% ( 1.242, 2.109 ) ( 1.243, 2.110 )
Calculations and Intervals on Original Scale
>
> # studentized confidence interval for the two sample
> # difference of means problem using the final two series
> # of the gravity data.
> diff.means <- function(d, f)
+ { n <- nrow(d)
+ gp1 <- 1:table(as.numeric(d$series))[1]
+ m1 <- sum(d[gp1,1] * f[gp1])/sum(f[gp1])
+ m2 <- sum(d[-gp1,1] * f[-gp1])/sum(f[-gp1])
+ ss1 <- sum(d[gp1,1]^2 * f[gp1]) - (m1 * m1 * sum(f[gp1]))
+ ss2 <- sum(d[-gp1,1]^2 * f[-gp1]) - (m2 * m2 * sum(f[-gp1]))
+ c(m1-m2, (ss1+ss2)/(sum(f)-2))
+ }
> grav1 <- gravity[as.numeric(gravity[,2])>=7,]
> grav1.boot <- boot(grav1, diff.means, R=999, stype="f", strata=grav1[,2])
> boot.ci(grav1.boot, type=c("stud","norm"))
BOOTSTRAP CONFIDENCE INTERVAL CALCULATIONS
Based on 999 bootstrap replicates
CALL :
boot.ci(boot.out = grav1.boot, type = c("stud", "norm"))
Intervals :
Level Normal Studentized
95% (-5.880, 0.183 ) (-7.059, -0.101 )
Calculations and Intervals on Original Scale
>
> # Nonparametric confidence intervals for mean failure time
> # of the air-conditioning data as in Example 5.4 of Davison
> # and Hinkley (1997)
> mean.fun <- function(d, i)
+ { m <- mean(d$hours[i])
+ n <- length(i)
+ v <- (n-1)*var(d$hours[i])/n^2
+ c(m, v)
+ }
> air.boot <- boot(aircondit, mean.fun, R=999)
> boot.ci(air.boot, type = c("norm", "basic", "perc", "stud"))
BOOTSTRAP CONFIDENCE INTERVAL CALCULATIONS
Based on 999 bootstrap replicates
CALL :
boot.ci(boot.out = air.boot, type = c("norm", "basic", "perc",
"stud"))
Intervals :
Level Normal Basic
95% ( 35.5, 181.9 ) ( 26.0, 170.6 )
Level Studentized Percentile
95% ( 47.9, 294.5 ) ( 45.6, 190.2 )
Calculations and Intervals on Original Scale
>
> # Now using the log transformation
> # There are two ways of doing this and they both give the
> # same intervals.
>
> # Method 1
> boot.ci(air.boot, type = c("norm", "basic", "perc", "stud"),
+ h = log, hdot = function(x) 1/x)
BOOTSTRAP CONFIDENCE INTERVAL CALCULATIONS
Based on 999 bootstrap replicates
CALL :
boot.ci(boot.out = air.boot, type = c("norm", "basic", "perc",
"stud"), h = log, hdot = function(x) 1/x)
Intervals :
Level Normal Basic
95% ( 4.035, 5.469 ) ( 4.118, 5.546 )
Level Studentized Percentile
95% ( 3.959, 5.808 ) ( 3.820, 5.248 )
Calculations and Intervals on Transformed Scale
>
> # Method 2
> vt0 <- air.boot$t0[2]/air.boot$t0[1]^2
> vt <- air.boot$t[,2]/air.boot$t[,1]^2
> boot.ci(air.boot, type = c("norm", "basic", "perc", "stud"),
+ t0 = log(air.boot$t0[1]), t = log(air.boot$t[,1]),
+ var.t0 = vt0, var.t = vt)
BOOTSTRAP CONFIDENCE INTERVAL CALCULATIONS
Based on 999 bootstrap replicates
CALL :
boot.ci(boot.out = air.boot, type = c("norm", "basic", "perc",
"stud"), var.t0 = vt0, var.t = vt, t0 = log(air.boot$t0[1]),
t = log(air.boot$t[, 1]))
Intervals :
Level Normal Basic
95% ( 4.069, 5.435 ) ( 4.118, 5.546 )
Level Studentized Percentile
95% ( 3.959, 5.808 ) ( 3.820, 5.248 )
Calculations and Intervals on Original Scale
>
>
>
> cleanEx()
> nameEx("censboot")
> ### * censboot
>
> flush(stderr()); flush(stdout())
>
> ### Name: censboot
> ### Title: Bootstrap for Censored Data
> ### Aliases: censboot cens.return
> ### Keywords: survival
>
> ### ** Examples
>
> data(aml, package="boot")
> library(survival)
Loading required package: splines
Attaching package: 'survival'
The following object(s) are masked _by_ '.GlobalEnv':
aml
The following object(s) are masked from 'package:boot':
aml
> # Example 3.9 of Davison and Hinkley (1997) does a bootstrap on some
> # remission times for patients with a type of leukaemia. The patients
> # were divided into those who received maintenance chemotherapy and
> # those who did not. Here we are interested in the median remission
> # time for the two groups.
> aml.fun <- function(data) {
+ surv <- survfit(Surv(time, cens)~group, data=data)
+ out <- NULL
+ st <- 1
+ for (s in 1:length(surv$strata)) {
+ inds <- st:(st+surv$strata[s]-1)
+ md <- min(surv$time[inds[1-surv$surv[inds]>=0.5]])
+ st <- st+surv$strata[s]
+ out <- c(out,md)
+ }
+ out
+ }
> aml.case <- censboot(aml,aml.fun,R=499,strata=aml$group)
Warning in min(surv$time[inds[1 - surv$surv[inds] >= 0.5]]) :
no non-missing arguments to min; returning Inf
Warning in min(surv$time[inds[1 - surv$surv[inds] >= 0.5]]) :
no non-missing arguments to min; returning Inf
Warning in min(surv$time[inds[1 - surv$surv[inds] >= 0.5]]) :
no non-missing arguments to min; returning Inf
Warning in min(surv$time[inds[1 - surv$surv[inds] >= 0.5]]) :
no non-missing arguments to min; returning Inf
Warning in min(surv$time[inds[1 - surv$surv[inds] >= 0.5]]) :
no non-missing arguments to min; returning Inf
Warning in min(surv$time[inds[1 - surv$surv[inds] >= 0.5]]) :
no non-missing arguments to min; returning Inf
Warning in min(surv$time[inds[1 - surv$surv[inds] >= 0.5]]) :
no non-missing arguments to min; returning Inf
Warning in min(surv$time[inds[1 - surv$surv[inds] >= 0.5]]) :
no non-missing arguments to min; returning Inf
Warning in min(surv$time[inds[1 - surv$surv[inds] >= 0.5]]) :
no non-missing arguments to min; returning Inf
Warning in min(surv$time[inds[1 - surv$surv[inds] >= 0.5]]) :
no non-missing arguments to min; returning Inf
Warning in min(surv$time[inds[1 - surv$surv[inds] >= 0.5]]) :
no non-missing arguments to min; returning Inf
Warning in min(surv$time[inds[1 - surv$surv[inds] >= 0.5]]) :
no non-missing arguments to min; returning Inf
Warning in min(surv$time[inds[1 - surv$surv[inds] >= 0.5]]) :
no non-missing arguments to min; returning Inf
Warning in min(surv$time[inds[1 - surv$surv[inds] >= 0.5]]) :
no non-missing arguments to min; returning Inf
Warning in min(surv$time[inds[1 - surv$surv[inds] >= 0.5]]) :
no non-missing arguments to min; returning Inf
Warning in min(surv$time[inds[1 - surv$surv[inds] >= 0.5]]) :
no non-missing arguments to min; returning Inf
Warning in min(surv$time[inds[1 - surv$surv[inds] >= 0.5]]) :
no non-missing arguments to min; returning Inf
Warning in min(surv$time[inds[1 - surv$surv[inds] >= 0.5]]) :
no non-missing arguments to min; returning Inf
Warning in min(surv$time[inds[1 - surv$surv[inds] >= 0.5]]) :
no non-missing arguments to min; returning Inf
Warning in min(surv$time[inds[1 - surv$surv[inds] >= 0.5]]) :
no non-missing arguments to min; returning Inf
Warning in min(surv$time[inds[1 - surv$surv[inds] >= 0.5]]) :
no non-missing arguments to min; returning Inf
Warning in min(surv$time[inds[1 - surv$surv[inds] >= 0.5]]) :
no non-missing arguments to min; returning Inf
Warning in min(surv$time[inds[1 - surv$surv[inds] >= 0.5]]) :
no non-missing arguments to min; returning Inf
Warning in min(surv$time[inds[1 - surv$surv[inds] >= 0.5]]) :
no non-missing arguments to min; returning Inf
Warning in min(surv$time[inds[1 - surv$surv[inds] >= 0.5]]) :
no non-missing arguments to min; returning Inf
Warning in min(surv$time[inds[1 - surv$surv[inds] >= 0.5]]) :
no non-missing arguments to min; returning Inf
>
> # Now we will look at the same statistic using the conditional
> # bootstrap and the weird bootstrap. For the conditional bootstrap
> # the survival distribution is stratified but the censoring
> # distribution is not.
>
> aml.s1 <- survfit(Surv(time,cens)~group, data=aml)
> aml.s2 <- survfit(Surv(time-0.001*cens,1-cens)~1, data=aml)
> aml.cond <- censboot(aml,aml.fun,R=499,strata=aml$group,
+ F.surv=aml.s1,G.surv=aml.s2,sim="cond")
Warning in min(surv$time[inds[1 - surv$surv[inds] >= 0.5]]) :
no non-missing arguments to min; returning Inf
Warning in min(surv$time[inds[1 - surv$surv[inds] >= 0.5]]) :
no non-missing arguments to min; returning Inf
Warning in min(surv$time[inds[1 - surv$surv[inds] >= 0.5]]) :
no non-missing arguments to min; returning Inf
Warning in min(surv$time[inds[1 - surv$surv[inds] >= 0.5]]) :
no non-missing arguments to min; returning Inf
Warning in min(surv$time[inds[1 - surv$surv[inds] >= 0.5]]) :
no non-missing arguments to min; returning Inf
Warning in min(surv$time[inds[1 - surv$surv[inds] >= 0.5]]) :
no non-missing arguments to min; returning Inf
Warning in min(surv$time[inds[1 - surv$surv[inds] >= 0.5]]) :
no non-missing arguments to min; returning Inf
Warning in min(surv$time[inds[1 - surv$surv[inds] >= 0.5]]) :
no non-missing arguments to min; returning Inf
Warning in min(surv$time[inds[1 - surv$surv[inds] >= 0.5]]) :
no non-missing arguments to min; returning Inf
Warning in min(surv$time[inds[1 - surv$surv[inds] >= 0.5]]) :
no non-missing arguments to min; returning Inf
Warning in min(surv$time[inds[1 - surv$surv[inds] >= 0.5]]) :
no non-missing arguments to min; returning Inf
Warning in min(surv$time[inds[1 - surv$surv[inds] >= 0.5]]) :
no non-missing arguments to min; returning Inf
Warning in min(surv$time[inds[1 - surv$surv[inds] >= 0.5]]) :
no non-missing arguments to min; returning Inf
Warning in min(surv$time[inds[1 - surv$surv[inds] >= 0.5]]) :
no non-missing arguments to min; returning Inf
Warning in min(surv$time[inds[1 - surv$surv[inds] >= 0.5]]) :
no non-missing arguments to min; returning Inf
Warning in min(surv$time[inds[1 - surv$surv[inds] >= 0.5]]) :
no non-missing arguments to min; returning Inf
Warning in min(surv$time[inds[1 - surv$surv[inds] >= 0.5]]) :
no non-missing arguments to min; returning Inf
Warning in min(surv$time[inds[1 - surv$surv[inds] >= 0.5]]) :
no non-missing arguments to min; returning Inf
Warning in min(surv$time[inds[1 - surv$surv[inds] >= 0.5]]) :
no non-missing arguments to min; returning Inf
Warning in min(surv$time[inds[1 - surv$surv[inds] >= 0.5]]) :
no non-missing arguments to min; returning Inf
Warning in min(surv$time[inds[1 - surv$surv[inds] >= 0.5]]) :
no non-missing arguments to min; returning Inf
Warning in min(surv$time[inds[1 - surv$surv[inds] >= 0.5]]) :
no non-missing arguments to min; returning Inf
Warning in min(surv$time[inds[1 - surv$surv[inds] >= 0.5]]) :
no non-missing arguments to min; returning Inf
Warning in min(surv$time[inds[1 - surv$surv[inds] >= 0.5]]) :
no non-missing arguments to min; returning Inf
Warning in min(surv$time[inds[1 - surv$surv[inds] >= 0.5]]) :
no non-missing arguments to min; returning Inf
Warning in min(surv$time[inds[1 - surv$surv[inds] >= 0.5]]) :
no non-missing arguments to min; returning Inf
Warning in min(surv$time[inds[1 - surv$surv[inds] >= 0.5]]) :
no non-missing arguments to min; returning Inf
Warning in min(surv$time[inds[1 - surv$surv[inds] >= 0.5]]) :
no non-missing arguments to min; returning Inf
>
>
> # For the weird bootstrap we must redefine our function slightly since
> # the data will not contain the group number.
> aml.fun1 <- function(data,str) {
+ surv <- survfit(Surv(data[,1],data[,2])~str)
+ out <- NULL
+ st <- 1
+ for (s in 1:length(surv$strata)) {
+ inds <- st:(st+surv$strata[s]-1)
+ md <- min(surv$time[inds[1-surv$surv[inds]>=0.5]])
+ st <- st+surv$strata[s]
+ out <- c(out,md)
+ }
+ out
+ }
> aml.wei <- censboot(cbind(aml$time,aml$cens),aml.fun1,R=499,
+ strata=aml$group, F.surv=aml.s1,sim="weird")
Warning in min(surv$time[inds[1 - surv$surv[inds] >= 0.5]]) :
no non-missing arguments to min; returning Inf
Warning in min(surv$time[inds[1 - surv$surv[inds] >= 0.5]]) :
no non-missing arguments to min; returning Inf
Warning in min(surv$time[inds[1 - surv$surv[inds] >= 0.5]]) :
no non-missing arguments to min; returning Inf
Warning in min(surv$time[inds[1 - surv$surv[inds] >= 0.5]]) :
no non-missing arguments to min; returning Inf
Warning in min(surv$time[inds[1 - surv$surv[inds] >= 0.5]]) :
no non-missing arguments to min; returning Inf
Warning in min(surv$time[inds[1 - surv$surv[inds] >= 0.5]]) :
no non-missing arguments to min; returning Inf
Warning in min(surv$time[inds[1 - surv$surv[inds] >= 0.5]]) :
no non-missing arguments to min; returning Inf
Warning in min(surv$time[inds[1 - surv$surv[inds] >= 0.5]]) :
no non-missing arguments to min; returning Inf
>
> # Now for an example where a cox regression model has been fitted
> # the data we will look at the melanoma data of Example 7.6 from
> # Davison and Hinkley (1997). The fitted model assumes that there
> # is a different survival distribution for the ulcerated and
> # non-ulcerated groups but that the thickness of the tumour has a
> # common effect. We will also assume that the censoring distribution
> # is different in different age groups. The statistic of interest
> # is the linear predictor. This is returned as the values at a
> # number of equally spaced points in the range of interest.
> data(melanoma, package="boot")
> library(splines)# for ns
> mel.cox <- coxph(Surv(time,status==1)~ns(thickness,df=4)+strata(ulcer),
+ data=melanoma)
> mel.surv <- survfit(mel.cox)
> agec <- cut(melanoma$age,c(0,39,49,59,69,100))
> mel.cens <- survfit(Surv(time-0.001*(status==1),status!=1)~
+ strata(agec),data=melanoma)
> mel.fun <- function(d) {
+ t1 <- ns(d$thickness,df=4)
+ cox <- coxph(Surv(d$time,d$status==1) ~ t1+strata(d$ulcer))
+ ind <- !duplicated(d$thickness)
+ u <- d$thickness[!ind]
+ eta <- cox$linear.predictors[!ind]
+ sp <- smooth.spline(u,eta,df=20)
+ th <- seq(from=0.25,to=10,by=0.25)
+ predict(sp,th)$y
+ }
> mel.str<-cbind(melanoma$ulcer,agec)
> # this is slow!
> mel.mod <- censboot(melanoma,mel.fun,R=999,F.surv=mel.surv,
+ G.surv=mel.cens,cox=mel.cox,strata=mel.str,sim="model")
> # To plot the original predictor and a 95% pointwise envelope for it
> mel.env <- envelope(mel.mod)$point
> plot(seq(0.25,10,by=0.25),mel.env[1,], ylim=c(-2,2),
+ xlab="thickness (mm)", ylab="linear predictor",type="n")
> lines(seq(0.25,10,by=0.25),mel.env[1,],lty=2)
> lines(seq(0.25,10,by=0.25),mel.env[2,],lty=2)
> lines(seq(0.25,10,by=0.25),mel.mod$t0,lty=1)
>
>
>
> cleanEx()
detaching ‘package:survival’, ‘package:splines’
> nameEx("control")
> ### * control
>
> flush(stderr()); flush(stdout())
>
> ### Name: control
> ### Title: Control Variate Calculations
> ### Aliases: control
> ### Keywords: nonparametric
>
> ### ** Examples
>
> # Use of control variates for the variance of the air-conditioning data
> mean.fun <- function(d, i)
+ { m <- mean(d$hours[i])
+ n <- nrow(d)
+ v <- (n-1)*var(d$hours[i])/n^2
+ c(m, v)
+ }
> air.boot <- boot(aircondit, mean.fun, R = 999)
> control(air.boot, index = 2, bias.adj = TRUE)
[1] -6.298101
> air.cont <- control(air.boot, index = 2)
Warning in optim(init, K) :
one-diml optimization by Nelder-Mead is unreliable: use optimize
Warning in optim(init, K) :
one-diml optimization by Nelder-Mead is unreliable: use optimize
Warning in optim(init, K) :
one-diml optimization by Nelder-Mead is unreliable: use optimize
Warning in optim(init, K) :
one-diml optimization by Nelder-Mead is unreliable: use optimize
Warning in optim(init, K) :
one-diml optimization by Nelder-Mead is unreliable: use optimize
Warning in optim(init, K) :
one-diml optimization by Nelder-Mead is unreliable: use optimize
Warning in optim(init, K) :
one-diml optimization by Nelder-Mead is unreliable: use optimize
Warning in optim(init, K) :
one-diml optimization by Nelder-Mead is unreliable: use optimize
Warning in optim(init, K) :
one-diml optimization by Nelder-Mead is unreliable: use optimize
Warning in optim(init, K) :
one-diml optimization by Nelder-Mead is unreliable: use optimize
Warning in optim(init, K) :
one-diml optimization by Nelder-Mead is unreliable: use optimize
Warning in optim(init, K) :
one-diml optimization by Nelder-Mead is unreliable: use optimize
Warning in optim(init, K) :
one-diml optimization by Nelder-Mead is unreliable: use optimize
Warning in optim(init, K) :
one-diml optimization by Nelder-Mead is unreliable: use optimize
Warning in optim(init, K) :
one-diml optimization by Nelder-Mead is unreliable: use optimize
Warning in optim(init, K) :
one-diml optimization by Nelder-Mead is unreliable: use optimize
Warning in optim(init, K) :
one-diml optimization by Nelder-Mead is unreliable: use optimize
Warning in optim(init, K) :
one-diml optimization by Nelder-Mead is unreliable: use optimize
Warning in optim(init, K) :
one-diml optimization by Nelder-Mead is unreliable: use optimize
Warning in optim(init, K) :
one-diml optimization by Nelder-Mead is unreliable: use optimize
Warning in optim(init, K) :
one-diml optimization by Nelder-Mead is unreliable: use optimize
Warning in optim(init, K) :
one-diml optimization by Nelder-Mead is unreliable: use optimize
> # Now let us try the variance on the log scale.
> air.cont1 <- control(air.boot, t0=log(air.boot$t0[2]),
+ t=log(air.boot$t[,2]))
Warning in optim(init, K) :
one-diml optimization by Nelder-Mead is unreliable: use optimize
Warning in optim(init, K) :
one-diml optimization by Nelder-Mead is unreliable: use optimize
Warning in optim(init, K) :
one-diml optimization by Nelder-Mead is unreliable: use optimize
Warning in optim(init, K) :
one-diml optimization by Nelder-Mead is unreliable: use optimize
Warning in optim(init, K) :
one-diml optimization by Nelder-Mead is unreliable: use optimize
Warning in optim(init, K) :
one-diml optimization by Nelder-Mead is unreliable: use optimize
Warning in optim(init, K) :
one-diml optimization by Nelder-Mead is unreliable: use optimize
Warning in optim(init, K) :
one-diml optimization by Nelder-Mead is unreliable: use optimize
Warning in optim(init, K) :
one-diml optimization by Nelder-Mead is unreliable: use optimize
Warning in optim(init, K) :
one-diml optimization by Nelder-Mead is unreliable: use optimize
Warning in optim(init, K) :
one-diml optimization by Nelder-Mead is unreliable: use optimize
Warning in optim(init, K) :
one-diml optimization by Nelder-Mead is unreliable: use optimize
Warning in optim(init, K) :
one-diml optimization by Nelder-Mead is unreliable: use optimize
Warning in optim(init, K) :
one-diml optimization by Nelder-Mead is unreliable: use optimize
Warning in optim(init, K) :
one-diml optimization by Nelder-Mead is unreliable: use optimize
Warning in optim(init, K) :
one-diml optimization by Nelder-Mead is unreliable: use optimize
Warning in optim(init, K) :
one-diml optimization by Nelder-Mead is unreliable: use optimize
Warning in optim(init, K) :
one-diml optimization by Nelder-Mead is unreliable: use optimize
Warning in optim(init, K) :
one-diml optimization by Nelder-Mead is unreliable: use optimize
Warning in optim(init, K) :
one-diml optimization by Nelder-Mead is unreliable: use optimize
Warning in optim(init, K) :
one-diml optimization by Nelder-Mead is unreliable: use optimize
Warning in optim(init, K) :
one-diml optimization by Nelder-Mead is unreliable: use optimize
Warning in optim(init, K) :
one-diml optimization by Nelder-Mead is unreliable: use optimize
Warning in optim(init, K) :
one-diml optimization by Nelder-Mead is unreliable: use optimize
>
>
>
> cleanEx()
> nameEx("cv.glm")
> ### * cv.glm
>
> flush(stderr()); flush(stdout())
>
> ### Name: cv.glm
> ### Title: Cross-validation for Generalized Linear Models
> ### Aliases: cv.glm
> ### Keywords: regression
>
> ### ** Examples
>
> # leave-one-out and 6-fold cross-validation prediction error for
> # the mammals data set.
> data(mammals, package="MASS")
> mammals.glm <- glm(log(brain)~log(body),data=mammals)
> cv.err <- cv.glm(mammals,mammals.glm)
> cv.err.6 <- cv.glm(mammals, mammals.glm, K=6)
>
>
> # As this is a linear model we could calculate the leave-one-out
> # cross-validation estimate without any extra model-fitting.
> muhat <- mammals.glm$fitted
> mammals.diag <- glm.diag(mammals.glm)
> cv.err <- mean((mammals.glm$y-muhat)^2/(1-mammals.diag$h)^2)
>
>
> # leave-one-out and 11-fold cross-validation prediction error for
> # the nodal data set. Since the response is a binary variable an
> # appropriate cost function is
> cost <- function(r, pi=0) mean(abs(r-pi)>0.5)
>
> nodal.glm <- glm(r~stage+xray+acid,binomial,data=nodal)
> cv.err <- cv.glm(nodal, nodal.glm, cost, K=nrow(nodal))$delta
> cv.11.err <- cv.glm(nodal, nodal.glm, cost, K=11)$delta
>
>
>
> cleanEx()
> nameEx("empinf")
> ### * empinf
>
> flush(stderr()); flush(stdout())
>
> ### Name: empinf
> ### Title: Empirical Influence Values
> ### Aliases: empinf
> ### Keywords: nonparametric math
>
> ### ** Examples
>
> # The empirical influence values for the ratio of means in
> # the city data.
> ratio <- function(d, w) sum(d$x *w)/sum(d$u*w)
> empinf(data=city,statistic=ratio)
[1] -1.04367815 -0.58417763 -0.37092459 -0.18958996 0.03164142 0.10544878
[7] 0.09236345 0.20365074 1.02178280 0.73381132
> city.boot <- boot(city,ratio,499,stype="w")
> empinf(boot.out=city.boot,type="reg")
1 1 1 1 1 1
-1.13619987 -0.69728210 -0.45301061 -0.27615882 0.02108999 0.14896336
1 1 1 1
0.09746429 0.20622340 1.18798956 0.90092079
>
> # A statistic that may be of interest in the difference of means
> # problem is the t-statistic for testing equality of means. In
> # the bootstrap we get replicates of the difference of means and
> # the variance of that statistic and then want to use this output
> # to get the empirical influence values of the t-statistic.
> grav1 <- gravity[as.numeric(gravity[,2])>=7,]
> grav.fun <- function(dat, w)
+ { strata <- tapply(dat[, 2], as.numeric(dat[, 2]))
+ d <- dat[, 1]
+ ns <- tabulate(strata)
+ w <- w/tapply(w, strata, sum)[strata]
+ mns <- tapply(d * w, strata, sum)
+ mn2 <- tapply(d * d * w, strata, sum)
+ s2hat <- sum((mn2 - mns^2)/ns)
+ c(mns[2]-mns[1],s2hat)
+ }
>
> grav.boot <- boot(grav1, grav.fun, R=499, stype="w", strata=grav1[,2])
>
> # Since the statistic of interest is a function of the bootstrap
> # statistics, we must calculate the bootstrap replicates and pass
> # them to empinf using the t argument.
> grav.z <- (grav.boot$t[,1]-grav.boot$t0[1])/sqrt(grav.boot$t[,2])
> empinf(boot.out=grav.boot,t=grav.z)
1 1 1 1 1 1 1
-2.9326019 -1.3760327 -2.4400720 -1.2175846 0.2795352 -0.8258764 -0.8156286
1 1 1 1 1 1 2
-0.5573332 -1.1275252 -3.1603140 1.2840693 3.5434781 9.3458860 2.6692589
2 2 2 2 2 2 2
4.4496570 3.6948000 0.9929002 -3.0100985 -3.2237464 -2.5493305 -0.6551745
2 2 2 2 2
1.9065308 0.4980530 -1.6219628 -1.6980508 -1.4528364
>
>
>
> cleanEx()
> nameEx("envelope")
> ### * envelope
>
> flush(stderr()); flush(stdout())
>
> ### Name: envelope
> ### Title: Confidence Envelopes for Curves
> ### Aliases: envelope
> ### Keywords: dplot htest
>
> ### ** Examples
>
> # Testing whether the final series of measurements of the gravity data
> # may come from a normal distribution. This is done in Examples 4.7
> # and 4.8 of Davison and Hinkley (1997).
> grav1 <- gravity$g[gravity$series==8]
> grav.z <- (grav1-mean(grav1))/sqrt(var(grav1))
> grav.gen <- function(dat,mle)
+ rnorm(length(dat))
> grav.qqboot <- boot(grav.z,sort,R=999,sim="parametric",ran.gen=grav.gen)
> grav.qq <- qqnorm(grav.z,plot=FALSE)
> grav.qq <- lapply(grav.qq,sort)
> plot(grav.qq,ylim=c(-3.5,3.5),ylab="Studentized Order Statistics",
+ xlab="Normal Quantiles")
> grav.env <- envelope(grav.qqboot,level=0.9)
> lines(grav.qq$x,grav.env$point[1,],lty=4)
> lines(grav.qq$x,grav.env$point[2,],lty=4)
> lines(grav.qq$x,grav.env$overall[1,],lty=1)
> lines(grav.qq$x,grav.env$overall[2,],lty=1)
>
>
>
> cleanEx()
> nameEx("exp.tilt")
> ### * exp.tilt
>
> flush(stderr()); flush(stdout())
>
> ### Name: exp.tilt
> ### Title: Exponential Tilting
> ### Aliases: exp.tilt
> ### Keywords: nonparametric smooth
>
> ### ** Examples
>
> # Example 9.8 of Davison and Hinkley (1997) requires tilting the resampling
> # distribution of the studentized statistic to be centred at the observed
> # value of the test statistic 1.84. This can be achieved as follows.
> grav1 <- gravity[as.numeric(gravity[,2])>=7,]
> grav.fun <- function(dat, w, orig)
+ { strata <- tapply(dat[, 2], as.numeric(dat[, 2]))
+ d <- dat[, 1]
+ ns <- tabulate(strata)
+ w <- w/tapply(w, strata, sum)[strata]
+ mns <- tapply(d * w, strata, sum)
+ mn2 <- tapply(d * d * w, strata, sum)
+ s2hat <- sum((mn2 - mns^2)/ns)
+ as.vector(c(mns[2]-mns[1],s2hat,(mns[2]-mns[1]-orig)/sqrt(s2hat)))
+ }
> grav.z0 <- grav.fun(grav1,rep(1,26),0)
> grav.L <- empinf(data=grav1, statistic=grav.fun, stype="w",
+ strata=grav1[,2], index=3, orig=grav.z0[1])
> grav.tilt <- exp.tilt(grav.L, grav.z0[3], strata=grav1[,2])
> boot(grav1, grav.fun, R=499, stype="w", weights=grav.tilt$p,
+ strata=grav1[,2], orig=grav.z0[1])
STRATIFIED WEIGHTED BOOTSTRAP
Call:
boot(data = grav1, statistic = grav.fun, R = 499, stype = "w",
strata = grav1[, 2], weights = grav.tilt$p, orig = grav.z0[1])
Bootstrap Statistics :
original bias std. error mean(t*)
t1* 2.846154 -0.3661063 1.705171 5.702944
t2* 2.392353 -0.3538294 1.002889 3.444050
t3* 0.000000 -0.5160619 1.314298 1.473456
>
>
>
> cleanEx()
> nameEx("glm.diag.plots")
> ### * glm.diag.plots
>
> flush(stderr()); flush(stdout())
>
> ### Name: glm.diag.plots
> ### Title: Diagnostics plots for generalized linear models
> ### Aliases: glm.diag.plots
> ### Keywords: regression dplot hplot
>
> ### ** Examples
>
> # In this example we look at the leukaemia data which was looked at in
> # Example 7.1 of Davison and Hinkley (1997)
> data(leuk, package="MASS")
> leuk.mod <- glm(time~ag-1+log10(wbc),family=Gamma(log),data=leuk)
> leuk.diag <- glm.diag(leuk.mod)
> glm.diag.plots(leuk.mod,leuk.diag)
>
>
>
> cleanEx()
> nameEx("jack.after.boot")
> ### * jack.after.boot
>
> flush(stderr()); flush(stdout())
>
> ### Name: jack.after.boot
> ### Title: Jackknife-after-Bootstrap Plots
> ### Aliases: jack.after.boot
> ### Keywords: hplot nonparametric
>
> ### ** Examples
>
> # To draw the jackknife-after-bootstrap plot for the head size data as in
> # Example 3.24 of Davison and Hinkley (1997)
> pcorr <- function( x )
+ {
+ # function to find the correlations and partial correlations between
+ # the four measurements.
+ v <- cor(x)
+ v.d <- diag(var(x))
+ iv <- solve(v)
+ iv.d <- sqrt(diag(iv))
+ iv <- - diag(1/iv.d) %*% iv %*% diag(1/iv.d)
+ q <- NULL
+ n <- nrow(v)
+ for (i in 1:(n-1))
+ q <- rbind( q, c(v[i,1:i],iv[i,(i+1):n]) )
+ q <- rbind( q, v[n,] )
+ diag(q) <- round(diag(q))
+ q
+ }
>
>
> frets.fun <- function( data, i )
+ { d <- data[i,]
+ v <- pcorr( d )
+ c(v[1,],v[2,],v[3,],v[4,])
+ }
> frets.boot <- boot(log(as.matrix(frets)), frets.fun, R=999)
> # we will concentrate on the partial correlation between head breadth
> # for the first son and head length for the second. This is the 7th
> # element in the output of frets.fun so we set index=7
> jack.after.boot(frets.boot,useJ=FALSE,stinf=FALSE,index=7)
>
>
>
> cleanEx()
> nameEx("k3.linear")
> ### * k3.linear
>
> flush(stderr()); flush(stdout())
>
> ### Name: k3.linear
> ### Title: Linear Skewness Estimate
> ### Aliases: k3.linear
> ### Keywords: nonparametric
>
> ### ** Examples
>
> # To estimate the skewness of the ratio of means for the city data.
> ratio <- function(d,w)
+ sum(d$x * w)/sum(d$u * w)
> k3.linear(empinf(data=city,statistic=ratio))
[1] 7.831452e-05
>
>
>
> cleanEx()
> nameEx("linear.approx")
> ### * linear.approx
>
> flush(stderr()); flush(stdout())
>
> ### Name: linear.approx
> ### Title: Linear Approximation of Bootstrap Replicates
> ### Aliases: linear.approx
> ### Keywords: nonparametric
>
> ### ** Examples
>
> # Using the city data let us look at the linear approximation to the
> # ratio statistic and its logarithm. We compare these with the
> # corresponding plots for the bigcity data
>
> ratio <- function(d, w)
+ sum(d$x * w)/sum(d$u * w)
> city.boot <- boot(city, ratio, R=499, stype="w")
> bigcity.boot <- boot(bigcity, ratio, R=499, stype="w")
> par(pty="s")
> par(mfrow=c(2,2))
>
> # The first plot is for the city data ratio statistic.
> city.lin1 <- linear.approx(city.boot)
> lim <- range(c(city.boot$t,city.lin1))
> plot(city.boot$t, city.lin1, xlim=lim,ylim=lim,
+ main="Ratio; n=10", xlab="t*", ylab="tL*")
> abline(0,1)
>
> # Now for the log of the ratio statistic for the city data.
> city.lin2 <- linear.approx(city.boot,t0=log(city.boot$t0),
+ t=log(city.boot$t))
> lim <- range(c(log(city.boot$t),city.lin2))
> plot(log(city.boot$t), city.lin2, xlim=lim, ylim=lim,
+ main="Log(Ratio); n=10", xlab="t*", ylab="tL*")
> abline(0,1)
>
> # The ratio statistic for the bigcity data.
> bigcity.lin1 <- linear.approx(bigcity.boot)
> lim <- range(c(bigcity.boot$t,bigcity.lin1))
> plot(bigcity.lin1,bigcity.boot$t, xlim=lim,ylim=lim,
+ main="Ratio; n=49", xlab="t*", ylab="tL*")
> abline(0,1)
>
> # Finally the log of the ratio statistic for the bigcity data.
> bigcity.lin2 <- linear.approx(bigcity.boot,t0=log(bigcity.boot$t0),
+ t=log(bigcity.boot$t))
> lim <- range(c(log(bigcity.boot$t),bigcity.lin2))
> plot(bigcity.lin2,log(bigcity.boot$t), xlim=lim,ylim=lim,
+ main="Log(Ratio); n=49", xlab="t*", ylab="tL*")
> abline(0,1)
>
> par(mfrow=c(1,1))
>
>
>
> graphics::par(get("par.postscript", pos = 'CheckExEnv'))
> cleanEx()
> nameEx("lines.saddle.distn")
> ### * lines.saddle.distn
>
> flush(stderr()); flush(stdout())
>
> ### Name: lines.saddle.distn
> ### Title: Add a Saddlepoint Approximation to a Plot
> ### Aliases: lines.saddle.distn
> ### Keywords: aplot smooth nonparametric
>
> ### ** Examples
>
> # In this example we show how a plot such as that in Figure 9.9 of
> # Davison and Hinkley (1997) may be produced. Note the large number of
> # bootstrap replicates required in this example.
> expdata <- rexp(12)
> vfun <- function(d, i)
+ { n <- length(d)
+ (n-1)/n*var(d[i])
+ }
> exp.boot <- boot(expdata,vfun, R = 9999)
> exp.L <- (expdata-mean(expdata))^2 - exp.boot$t0
> exp.tL <- linear.approx(exp.boot, L = exp.L)
> hist(exp.tL, nclass = 50, prob = TRUE)
> exp.t0 <- c(0,sqrt(var(exp.boot$t)))
> exp.sp <- saddle.distn(A = exp.L/12,wdist = "m", t0 = exp.t0)
Warning in optim(init, K) :
one-diml optimization by Nelder-Mead is unreliable: use optimize
Warning in optim(init, K) :
one-diml optimization by Nelder-Mead is unreliable: use optimize
Warning in optim(init, K) :
one-diml optimization by Nelder-Mead is unreliable: use optimize
Warning in optim(init, K) :
one-diml optimization by Nelder-Mead is unreliable: use optimize
Warning in optim(init, K) :
one-diml optimization by Nelder-Mead is unreliable: use optimize
Warning in optim(init, K) :
one-diml optimization by Nelder-Mead is unreliable: use optimize
Warning in optim(init, K) :
one-diml optimization by Nelder-Mead is unreliable: use optimize
Warning in optim(init, K) :
one-diml optimization by Nelder-Mead is unreliable: use optimize
Warning in optim(init, K) :
one-diml optimization by Nelder-Mead is unreliable: use optimize
Warning in optim(init, K) :
one-diml optimization by Nelder-Mead is unreliable: use optimize
Warning in optim(init, K) :
one-diml optimization by Nelder-Mead is unreliable: use optimize
Warning in optim(init, K) :
one-diml optimization by Nelder-Mead is unreliable: use optimize
Warning in optim(init, K) :
one-diml optimization by Nelder-Mead is unreliable: use optimize
Warning in optim(init, K) :
one-diml optimization by Nelder-Mead is unreliable: use optimize
Warning in optim(init, K) :
one-diml optimization by Nelder-Mead is unreliable: use optimize
Warning in optim(init, K) :
one-diml optimization by Nelder-Mead is unreliable: use optimize
Warning in optim(init, K) :
one-diml optimization by Nelder-Mead is unreliable: use optimize
Warning in optim(init, K) :
one-diml optimization by Nelder-Mead is unreliable: use optimize
Warning in optim(init, K) :
one-diml optimization by Nelder-Mead is unreliable: use optimize
Warning in optim(init, K) :
one-diml optimization by Nelder-Mead is unreliable: use optimize
Warning in optim(init, K) :
one-diml optimization by Nelder-Mead is unreliable: use optimize
Warning in optim(init, K) :
one-diml optimization by Nelder-Mead is unreliable: use optimize
>
> # The saddlepoint approximation in this case is to the density of
> # t-t0 and so t0 must be added for the plot.
> lines(exp.sp,h = function(u,t0) u+t0, J = function(u,t0) 1,
+ t0 = exp.boot$t0)
>
>
>
> cleanEx()
> nameEx("norm.ci")
> ### * norm.ci
>
> flush(stderr()); flush(stdout())
>
> ### Name: norm.ci
> ### Title: Normal Approximation Confidence Intervals
> ### Aliases: norm.ci
> ### Keywords: htest
>
> ### ** Examples
>
> # In Example 5.1 of Davison and Hinkley (1997), normal approximation
> # confidence intervals are found for the air-conditioning data.
> air.mean <- mean(aircondit$hours)
> air.n <- nrow(aircondit)
> air.v <- air.mean^2/air.n
> norm.ci(t0=air.mean, var.t0=air.v)
conf
[1,] 0.95 46.93055 169.2361
> exp(norm.ci(t0=log(air.mean), var.t0=1/air.n)[2:3])
[1] 61.38157 190.31782
>
> # Now a more complicated example - the ratio estimate for the city data.
> ratio <- function(d, w)
+ sum(d$x * w)/sum(d$u *w)
> city.v <- var.linear(empinf(data=city, statistic=ratio))
> norm.ci(t0=ratio(city,rep(0.1,10)), var.t0=city.v)
conf
[1,] 0.95 1.167046 1.873579
>
>
>
> cleanEx()
> nameEx("plot.boot")
> ### * plot.boot
>
> flush(stderr()); flush(stdout())
>
> ### Name: plot.boot
> ### Title: Plots of the Output of a Bootstrap Simulation
> ### Aliases: plot.boot
> ### Keywords: hplot nonparametric
>
> ### ** Examples
>
> # We fit an exponential model to the air-conditioning data and use
> # that for a parametric bootstrap. Then we look at plots of the
> # resampled means.
> air.rg <- function(data, mle)
+ rexp(length(data), 1/mle)
>
> air.boot <- boot(aircondit$hours, mean, R=999, sim="parametric",
+ ran.gen=air.rg, mle=mean(aircondit$hours))
> plot(air.boot)
>
> # In the difference of means example for the last two series of the
> # gravity data
> grav1 <- gravity[as.numeric(gravity[,2])>=7,]
> grav.fun <- function(dat, w)
+ { strata <- tapply(dat[, 2], as.numeric(dat[, 2]))
+ d <- dat[, 1]
+ ns <- tabulate(strata)
+ w <- w/tapply(w, strata, sum)[strata]
+ mns <- tapply(d * w, strata, sum)
+ mn2 <- tapply(d * d * w, strata, sum)
+ s2hat <- sum((mn2 - mns^2)/ns)
+ c(mns[2]-mns[1],s2hat)
+ }
>
>
> grav.boot <- boot(grav1, grav.fun, R=499, stype="w", strata=grav1[,2])
> plot(grav.boot)
> # now suppose we want to look at the studentized differences.
> grav.z <- (grav.boot$t[,1]-grav.boot$t0[1])/sqrt(grav.boot$t[,2])
> plot(grav.boot,t=grav.z,t0=0)
>
>
> # In this example we look at the one of the partial correlations for the
> # head dimensions in the dataset frets.
> pcorr <- function( x )
+ {
+ # Function to find the correlations and partial correlations between
+ # the four measurements.
+ v <- cor(x);
+ v.d <- diag(var(x));
+ iv <- solve(v);
+ iv.d <- sqrt(diag(iv));
+ iv <- - diag(1/iv.d) %*% iv %*% diag(1/iv.d);
+ q <- NULL;
+ n <- nrow(v);
+ for (i in 1:(n-1))
+ q <- rbind( q, c(v[i,1:i],iv[i,(i+1):n]) );
+ q <- rbind( q, v[n,] );
+ diag(q) <- round(diag(q));
+ q
+ }
>
>
> frets.fun <- function( data, i )
+ { d <- data[i,];
+ v <- pcorr( d );
+ c(v[1,],v[2,],v[3,],v[4,])
+ }
> frets.boot <- boot(log(as.matrix(frets)), frets.fun, R=999)
> plot(frets.boot, index=7, jack=TRUE, stinf=FALSE, useJ=FALSE)
>
>
>
> cleanEx()
> nameEx("saddle")
> ### * saddle
>
> flush(stderr()); flush(stdout())
>
> ### Name: saddle
> ### Title: Saddlepoint Approximations for Bootstrap Statistics
> ### Aliases: saddle
> ### Keywords: smooth nonparametric
>
> ### ** Examples
>
> # To evaluate the bootstrap distribution of the mean failure time of
> # air-conditioning equipment at 80 hours
> saddle(A=aircondit$hours/12, u=80)
Warning in optim(init, K) :
one-diml optimization by Nelder-Mead is unreliable: use optimize
$spa
pdf cdf
0.01005866 0.24446677
$zeta.hat
[1] -0.02580078
>
> # Alternatively this can be done using a conditional poisson
> saddle(A=cbind(aircondit$hours/12,1), u=c(80,12), wdist="p", type="cond")
Warning in dpois(y, mu, log = TRUE) : non-integer x = 10.090909
Warning in dpois(y, mu, log = TRUE) : non-integer x = 1.909091
Warning in dpois(y, mu, log = TRUE) : non-integer x = 10.090909
Warning in dpois(y, mu, log = TRUE) : non-integer x = 1.909091
$spa
pdf cdf
0.01005943 0.24438736
$zeta.hat
A1 A2
-0.02580805 0.89261577
$zeta2.hat
[1] 0.6931472
>
> # To use the Lugananni-Rice approximation to this
> saddle(A=cbind(aircondit$hours/12,1), u=c(80,12), wdist="p", type="cond",
+ LR = TRUE)
Warning in dpois(y, mu, log = TRUE) : non-integer x = 10.090909
Warning in dpois(y, mu, log = TRUE) : non-integer x = 1.909091
Warning in dpois(y, mu, log = TRUE) : non-integer x = 10.090909
Warning in dpois(y, mu, log = TRUE) : non-integer x = 1.909091
$spa
pdf cdf
0.01005943 0.24447362
$zeta.hat
A1 A2
-0.02580805 0.89261577
$zeta2.hat
[1] 0.6931472
>
> # Example 9.16 of Davison and Hinkley (1997) calculates saddlepoint
> # approximations to the distribution of the ratio statistic for the
> # city data. Since the statistic is not in itself a linear combination
> # of random Variables, its distribution cannot be found directly.
> # Instead the statistic is expressed as the solution to a linear
> # estimating equation and hence its distribution can be found. We
> # get the saddlepoint approximation to the pdf and cdf evaluated at
> # t=1.25 as follows.
> jacobian <- function(dat,t,zeta)
+ {
+ p <- exp(zeta*(dat$x-t*dat$u))
+ abs(sum(dat$u*p)/sum(p))
+ }
> city.sp1 <- saddle(A=city$x-1.25*city$u, u=0)
Warning in optim(init, K) :
one-diml optimization by Nelder-Mead is unreliable: use optimize
> city.sp1$spa[1] <- jacobian(city, 1.25, city.sp1$zeta.hat) * city.sp1$spa[1]
> city.sp1
$spa
pdf cdf
0.05565040 0.02436306
$zeta.hat
[1] -0.02435547
>
>
>
> cleanEx()
> nameEx("saddle.distn")
> ### * saddle.distn
>
> flush(stderr()); flush(stdout())
>
> ### Name: saddle.distn
> ### Title: Saddlepoint Distribution Approximations for Bootstrap Statistics
> ### Aliases: saddle.distn
> ### Keywords: nonparametric smooth dplot
>
> ### ** Examples
>
> # The bootstrap distribution of the mean of the air-conditioning
> # failure data: fails to find value on R (and probably on S too)
> air.t0 <- c(mean(aircondit$hours), sqrt(var(aircondit$hours)/12))
> ## Not run: saddle.distn(A = aircondit$hours/12, t0 = air.t0)
>
> # alternatively using the conditional poisson
> saddle.distn(A = cbind(aircondit$hours/12, 1), u = 12, wdist = "p",
+ type = "cond", t0 = air.t0)
Warning in dpois(y, mu, log = TRUE) : non-integer x = 11.344718
Warning in dpois(y, mu, log = TRUE) : non-integer x = 0.655282
Warning in dpois(y, mu, log = TRUE) : non-integer x = 11.344718
Warning in dpois(y, mu, log = TRUE) : non-integer x = 0.655282
Warning in dpois(y, mu, log = TRUE) : non-integer x = 11.832240
Warning in dpois(y, mu, log = TRUE) : non-integer x = 0.167760
Warning in dpois(y, mu, log = TRUE) : non-integer x = 11.832240
Warning in dpois(y, mu, log = TRUE) : non-integer x = 0.167760
Warning in dpois(y, mu, log = TRUE) : non-integer x = 7.444538
Warning in dpois(y, mu, log = TRUE) : non-integer x = 4.555462
Warning in dpois(y, mu, log = TRUE) : non-integer x = 7.444538
Warning in dpois(y, mu, log = TRUE) : non-integer x = 4.555462
Warning in dpois(y, mu, log = TRUE) : non-integer x = 5.494449
Warning in dpois(y, mu, log = TRUE) : non-integer x = 6.505551
Warning in dpois(y, mu, log = TRUE) : non-integer x = 5.494449
Warning in dpois(y, mu, log = TRUE) : non-integer x = 6.505551
Warning in dpois(y, mu, log = TRUE) : non-integer x = 1.594269
Warning in dpois(y, mu, log = TRUE) : non-integer x = 10.405731
Warning in dpois(y, mu, log = TRUE) : non-integer x = 1.594269
Warning in dpois(y, mu, log = TRUE) : non-integer x = 10.405731
Warning in dpois(y, mu, log = TRUE) : non-integer x = 3.544359
Warning in dpois(y, mu, log = TRUE) : non-integer x = 8.455641
Warning in dpois(y, mu, log = TRUE) : non-integer x = 3.544359
Warning in dpois(y, mu, log = TRUE) : non-integer x = 8.455641
Warning in dpois(y, mu, log = TRUE) : non-integer x = 4.519404
Warning in dpois(y, mu, log = TRUE) : non-integer x = 7.480596
Warning in dpois(y, mu, log = TRUE) : non-integer x = 4.519404
Warning in dpois(y, mu, log = TRUE) : non-integer x = 7.480596
Warning in dpois(y, mu, log = TRUE) : non-integer x = 11.561394
Warning in dpois(y, mu, log = TRUE) : non-integer x = 0.438606
Warning in dpois(y, mu, log = TRUE) : non-integer x = 11.561394
Warning in dpois(y, mu, log = TRUE) : non-integer x = 0.438606
Warning in dpois(y, mu, log = TRUE) : non-integer x = 11.290549
Warning in dpois(y, mu, log = TRUE) : non-integer x = 0.709451
Warning in dpois(y, mu, log = TRUE) : non-integer x = 11.290549
Warning in dpois(y, mu, log = TRUE) : non-integer x = 0.709451
Warning in dpois(y, mu, log = TRUE) : non-integer x = 11.019703
Warning in dpois(y, mu, log = TRUE) : non-integer x = 0.980297
Warning in dpois(y, mu, log = TRUE) : non-integer x = 11.019703
Warning in dpois(y, mu, log = TRUE) : non-integer x = 0.980297
Warning in dpois(y, mu, log = TRUE) : non-integer x = 10.748857
Warning in dpois(y, mu, log = TRUE) : non-integer x = 1.251143
Warning in dpois(y, mu, log = TRUE) : non-integer x = 10.748857
Warning in dpois(y, mu, log = TRUE) : non-integer x = 1.251143
Warning in dpois(y, mu, log = TRUE) : non-integer x = 10.478011
Warning in dpois(y, mu, log = TRUE) : non-integer x = 1.521989
Warning in dpois(y, mu, log = TRUE) : non-integer x = 10.478011
Warning in dpois(y, mu, log = TRUE) : non-integer x = 1.521989
Warning in dpois(y, mu, log = TRUE) : non-integer x = 10.207165
Warning in dpois(y, mu, log = TRUE) : non-integer x = 1.792835
Warning in dpois(y, mu, log = TRUE) : non-integer x = 10.207165
Warning in dpois(y, mu, log = TRUE) : non-integer x = 1.792835
Warning in dpois(y, mu, log = TRUE) : non-integer x = 9.936320
Warning in dpois(y, mu, log = TRUE) : non-integer x = 2.063680
Warning in dpois(y, mu, log = TRUE) : non-integer x = 9.936320
Warning in dpois(y, mu, log = TRUE) : non-integer x = 2.063680
Warning in dpois(y, mu, log = TRUE) : non-integer x = 9.665474
Warning in dpois(y, mu, log = TRUE) : non-integer x = 2.334526
Warning in dpois(y, mu, log = TRUE) : non-integer x = 9.665474
Warning in dpois(y, mu, log = TRUE) : non-integer x = 2.334526
Warning in dpois(y, mu, log = TRUE) : non-integer x = 8.785225
Warning in dpois(y, mu, log = TRUE) : non-integer x = 3.214775
Warning in dpois(y, mu, log = TRUE) : non-integer x = 8.785225
Warning in dpois(y, mu, log = TRUE) : non-integer x = 3.214775
Warning in dpois(y, mu, log = TRUE) : non-integer x = 8.175822
Warning in dpois(y, mu, log = TRUE) : non-integer x = 3.824178
Warning in dpois(y, mu, log = TRUE) : non-integer x = 8.175822
Warning in dpois(y, mu, log = TRUE) : non-integer x = 3.824178
Warning in dpois(y, mu, log = TRUE) : non-integer x = 7.566419
Warning in dpois(y, mu, log = TRUE) : non-integer x = 4.433581
Warning in dpois(y, mu, log = TRUE) : non-integer x = 7.566419
Warning in dpois(y, mu, log = TRUE) : non-integer x = 4.433581
Warning in dpois(y, mu, log = TRUE) : non-integer x = 6.957016
Warning in dpois(y, mu, log = TRUE) : non-integer x = 5.042984
Warning in dpois(y, mu, log = TRUE) : non-integer x = 6.957016
Warning in dpois(y, mu, log = TRUE) : non-integer x = 5.042984
Warning in dpois(y, mu, log = TRUE) : non-integer x = 6.347613
Warning in dpois(y, mu, log = TRUE) : non-integer x = 5.652387
Warning in dpois(y, mu, log = TRUE) : non-integer x = 6.347613
Warning in dpois(y, mu, log = TRUE) : non-integer x = 5.652387
Warning in dpois(y, mu, log = TRUE) : non-integer x = 5.738210
Warning in dpois(y, mu, log = TRUE) : non-integer x = 6.261790
Warning in dpois(y, mu, log = TRUE) : non-integer x = 5.738210
Warning in dpois(y, mu, log = TRUE) : non-integer x = 6.261790
Warning in dpois(y, mu, log = TRUE) : non-integer x = 5.128807
Warning in dpois(y, mu, log = TRUE) : non-integer x = 6.871193
Warning in dpois(y, mu, log = TRUE) : non-integer x = 5.128807
Warning in dpois(y, mu, log = TRUE) : non-integer x = 6.871193
Saddlepoint Distribution Approximations
Call :
saddle.distn(A = cbind(aircondit$hours/12, 1), u = 12, wdist = "p",
type = "cond", t0 = air.t0)
Quantiles of the Distribution
0.1% 27.4
0.5% 35.4
1.0% 39.7
2.5% 46.7
5.0% 53.5
10.0% 62.5
20.0% 75.3
50.0% 104.5
80.0% 139.0
90.0% 158.8
95.0% 175.9
97.5% 191.2
99.0% 209.6
99.5% 222.4
99.9% 249.5
Smoothing spline used 20 points in the range 9.8 to 304.7.
>
> # Distribution of the ratio of a sample of size 10 from the bigcity
> # data, taken from Example 9.16 of Davison and Hinkley (1997).
> ratio <- function(d, w) sum(d$x *w)/sum(d$u * w)
> city.v <- var.linear(empinf(data = city, statistic = ratio))
> bigcity.t0 <- c(mean(bigcity$x)/mean(bigcity$u), sqrt(city.v))
> Afn <- function(t, data) cbind(data$x - t*data$u, 1)
> ufn <- function(t, data) c(0,10)
> saddle.distn(A = Afn, u = ufn, wdist = "b", type = "cond",
+ t0 = bigcity.t0, data = bigcity)
Warning in eval(expr, envir, enclos) :
non-integer counts in a binomial glm!
Warning in eval(expr, envir, enclos) :
non-integer counts in a binomial glm!
Warning in eval(expr, envir, enclos) :
non-integer counts in a binomial glm!
Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
Warning in eval(expr, envir, enclos) :
non-integer counts in a binomial glm!
Warning in eval(expr, envir, enclos) :
non-integer counts in a binomial glm!
Warning in eval(expr, envir, enclos) :
non-integer counts in a binomial glm!
Warning in eval(expr, envir, enclos) :
non-integer counts in a binomial glm!
Warning in eval(expr, envir, enclos) :
non-integer counts in a binomial glm!
Warning in eval(expr, envir, enclos) :
non-integer counts in a binomial glm!
Warning in eval(expr, envir, enclos) :
non-integer counts in a binomial glm!
Warning in eval(expr, envir, enclos) :
non-integer counts in a binomial glm!
Warning in eval(expr, envir, enclos) :
non-integer counts in a binomial glm!
Warning in eval(expr, envir, enclos) :
non-integer counts in a binomial glm!
Warning in eval(expr, envir, enclos) :
non-integer counts in a binomial glm!
Warning in eval(expr, envir, enclos) :
non-integer counts in a binomial glm!
Warning in eval(expr, envir, enclos) :
non-integer counts in a binomial glm!
Warning in eval(expr, envir, enclos) :
non-integer counts in a binomial glm!
Warning in eval(expr, envir, enclos) :
non-integer counts in a binomial glm!
Warning in eval(expr, envir, enclos) :
non-integer counts in a binomial glm!
Warning in eval(expr, envir, enclos) :
non-integer counts in a binomial glm!
Warning in eval(expr, envir, enclos) :
non-integer counts in a binomial glm!
Warning in eval(expr, envir, enclos) :
non-integer counts in a binomial glm!
Warning in eval(expr, envir, enclos) :
non-integer counts in a binomial glm!
Warning in eval(expr, envir, enclos) :
non-integer counts in a binomial glm!
Warning in eval(expr, envir, enclos) :
non-integer counts in a binomial glm!
Warning in eval(expr, envir, enclos) :
non-integer counts in a binomial glm!
Warning in eval(expr, envir, enclos) :
non-integer counts in a binomial glm!
Warning in eval(expr, envir, enclos) :
non-integer counts in a binomial glm!
Warning in eval(expr, envir, enclos) :
non-integer counts in a binomial glm!
Warning in eval(expr, envir, enclos) :
non-integer counts in a binomial glm!
Warning in eval(expr, envir, enclos) :
non-integer counts in a binomial glm!
Warning in eval(expr, envir, enclos) :
non-integer counts in a binomial glm!
Warning in eval(expr, envir, enclos) :
non-integer counts in a binomial glm!
Warning in eval(expr, envir, enclos) :
non-integer counts in a binomial glm!
Warning in eval(expr, envir, enclos) :
non-integer counts in a binomial glm!
Warning in eval(expr, envir, enclos) :
non-integer counts in a binomial glm!
Warning in eval(expr, envir, enclos) :
non-integer counts in a binomial glm!
Warning in eval(expr, envir, enclos) :
non-integer counts in a binomial glm!
Warning in eval(expr, envir, enclos) :
non-integer counts in a binomial glm!
Warning in eval(expr, envir, enclos) :
non-integer counts in a binomial glm!
Saddlepoint Distribution Approximations
Call :
saddle.distn(A = Afn, u = ufn, wdist = "b", type = "cond", t0 = bigcity.t0,
data = bigcity)
Quantiles of the Distribution
0.1% 1.070
0.5% 1.092
1.0% 1.104
2.5% 1.122
5.0% 1.139
10.0% 1.158
20.0% 1.184
50.0% 1.237
80.0% 1.304
90.0% 1.348
95.0% 1.392
97.5% 1.436
99.0% 1.494
99.5% 1.537
99.9% 1.636
Smoothing spline used 20 points in the range 1.014 to 1.96.
>
> # From Example 9.16 of Davison and Hinkley (1997) again, we find the
> # conditional distribution of the ratio given the sum of city$u.
> Afn <- function(t, data) cbind(data$x-t*data$u, data$u, 1)
> ufn <- function(t, data) c(0, sum(data$u), 10)
> city.t0 <- c(mean(city$x)/mean(city$u), sqrt(city.v))
> saddle.distn(A = Afn, u = ufn, wdist = "p", type = "cond", t0 = city.t0,
+ data = city)
Warning in dpois(y, mu, log = TRUE) : non-integer x = 0.866400
Warning in dpois(y, mu, log = TRUE) : non-integer x = 8.511350
Warning in dpois(y, mu, log = TRUE) : non-integer x = 0.622251
Warning in dpois(y, mu, log = TRUE) : non-integer x = 0.866400
Warning in dpois(y, mu, log = TRUE) : non-integer x = 8.511350
Warning in dpois(y, mu, log = TRUE) : non-integer x = 0.622251
Warning in dpois(y, mu, log = TRUE) : non-integer x = 3.210844
Warning in dpois(y, mu, log = TRUE) : non-integer x = 3.107208
Warning in dpois(y, mu, log = TRUE) : non-integer x = 3.681949
Warning in dpois(y, mu, log = TRUE) : non-integer x = 3.210844
Warning in dpois(y, mu, log = TRUE) : non-integer x = 3.107208
Warning in dpois(y, mu, log = TRUE) : non-integer x = 3.681949
Warning in dpois(y, mu, log = TRUE) : non-integer x = 2.038622
Warning in dpois(y, mu, log = TRUE) : non-integer x = 5.809279
Warning in dpois(y, mu, log = TRUE) : non-integer x = 2.152100
Warning in dpois(y, mu, log = TRUE) : non-integer x = 2.038622
Warning in dpois(y, mu, log = TRUE) : non-integer x = 5.809279
Warning in dpois(y, mu, log = TRUE) : non-integer x = 2.152100
Warning in dpois(y, mu, log = TRUE) : non-integer x = 1.452511
Warning in dpois(y, mu, log = TRUE) : non-integer x = 7.160314
Warning in dpois(y, mu, log = TRUE) : non-integer x = 1.387175
Warning in dpois(y, mu, log = TRUE) : non-integer x = 1.452511
Warning in dpois(y, mu, log = TRUE) : non-integer x = 7.160314
Warning in dpois(y, mu, log = TRUE) : non-integer x = 1.387175
Warning in dpois(y, mu, log = TRUE) : non-integer x = 1.159455
Warning in dpois(y, mu, log = TRUE) : non-integer x = 7.835832
Warning in dpois(y, mu, log = TRUE) : non-integer x = 1.004713
Warning in dpois(y, mu, log = TRUE) : non-integer x = 1.159455
Warning in dpois(y, mu, log = TRUE) : non-integer x = 7.835832
Warning in dpois(y, mu, log = TRUE) : non-integer x = 1.004713
Warning in dpois(y, mu, log = TRUE) : non-integer x = 3.155629
Warning in dpois(y, mu, log = TRUE) : non-integer x = 0.115412
Warning in dpois(y, mu, log = TRUE) : non-integer x = 6.728960
Warning in dpois(y, mu, log = TRUE) : non-integer x = 3.155629
Warning in dpois(y, mu, log = TRUE) : non-integer x = 0.115412
Warning in dpois(y, mu, log = TRUE) : non-integer x = 6.728960
Warning in dpois(y, mu, log = TRUE) : non-integer x = 0.205022
Warning in dpois(y, mu, log = TRUE) : non-integer x = 2.133273
Warning in dpois(y, mu, log = TRUE) : non-integer x = 7.661705
Warning in dpois(y, mu, log = TRUE) : non-integer x = 0.205022
Warning in dpois(y, mu, log = TRUE) : non-integer x = 2.133273
Warning in dpois(y, mu, log = TRUE) : non-integer x = 7.661705
Warning in dpois(y, mu, log = TRUE) : non-integer x = 1.722845
Warning in dpois(y, mu, log = TRUE) : non-integer x = 1.033105
Warning in dpois(y, mu, log = TRUE) : non-integer x = 7.244049
Warning in dpois(y, mu, log = TRUE) : non-integer x = 1.722845
Warning in dpois(y, mu, log = TRUE) : non-integer x = 1.033105
Warning in dpois(y, mu, log = TRUE) : non-integer x = 7.244049
Warning in dpois(y, mu, log = TRUE) : non-integer x = 1.787431
Warning in dpois(y, mu, log = TRUE) : non-integer x = 6.388294
Warning in dpois(y, mu, log = TRUE) : non-integer x = 1.824275
Warning in dpois(y, mu, log = TRUE) : non-integer x = 1.787431
Warning in dpois(y, mu, log = TRUE) : non-integer x = 6.388294
Warning in dpois(y, mu, log = TRUE) : non-integer x = 1.824275
Warning in dpois(y, mu, log = TRUE) : non-integer x = 2.415407
Warning in dpois(y, mu, log = TRUE) : non-integer x = 4.940756
Warning in dpois(y, mu, log = TRUE) : non-integer x = 2.643837
Warning in dpois(y, mu, log = TRUE) : non-integer x = 2.415407
Warning in dpois(y, mu, log = TRUE) : non-integer x = 4.940756
Warning in dpois(y, mu, log = TRUE) : non-integer x = 2.643837
Warning in dpois(y, mu, log = TRUE) : non-integer x = 3.043383
Warning in dpois(y, mu, log = TRUE) : non-integer x = 3.493218
Warning in dpois(y, mu, log = TRUE) : non-integer x = 3.463399
Warning in dpois(y, mu, log = TRUE) : non-integer x = 3.043383
Warning in dpois(y, mu, log = TRUE) : non-integer x = 3.493218
Warning in dpois(y, mu, log = TRUE) : non-integer x = 3.463399
Warning in dpois(y, mu, log = TRUE) : non-integer x = 3.671359
Warning in dpois(y, mu, log = TRUE) : non-integer x = 2.045680
Warning in dpois(y, mu, log = TRUE) : non-integer x = 4.282961
Warning in dpois(y, mu, log = TRUE) : non-integer x = 3.671359
Warning in dpois(y, mu, log = TRUE) : non-integer x = 2.045680
Warning in dpois(y, mu, log = TRUE) : non-integer x = 4.282961
Warning in dpois(y, mu, log = TRUE) : non-integer x = 4.299336
Warning in dpois(y, mu, log = TRUE) : non-integer x = 0.598142
Warning in dpois(y, mu, log = TRUE) : non-integer x = 5.102523
Warning in dpois(y, mu, log = TRUE) : non-integer x = 4.299336
Warning in dpois(y, mu, log = TRUE) : non-integer x = 0.598142
Warning in dpois(y, mu, log = TRUE) : non-integer x = 5.102523
Warning in dpois(y, mu, log = TRUE) : non-integer x = 3.433935
Warning in dpois(y, mu, log = TRUE) : non-integer x = 4.409277
Warning in dpois(y, mu, log = TRUE) : non-integer x = 2.156788
Warning in dpois(y, mu, log = TRUE) : non-integer x = 3.433935
Warning in dpois(y, mu, log = TRUE) : non-integer x = 4.409277
Warning in dpois(y, mu, log = TRUE) : non-integer x = 2.156788
Warning in dpois(y, mu, log = TRUE) : non-integer x = 3.360158
Warning in dpois(y, mu, log = TRUE) : non-integer x = 3.271008
Warning in dpois(y, mu, log = TRUE) : non-integer x = 3.368834
Warning in dpois(y, mu, log = TRUE) : non-integer x = 3.360158
Warning in dpois(y, mu, log = TRUE) : non-integer x = 3.271008
Warning in dpois(y, mu, log = TRUE) : non-integer x = 3.368834
Warning in dpois(y, mu, log = TRUE) : non-integer x = 3.319252
Warning in dpois(y, mu, log = TRUE) : non-integer x = 2.639889
Warning in dpois(y, mu, log = TRUE) : non-integer x = 4.040859
Warning in dpois(y, mu, log = TRUE) : non-integer x = 3.319252
Warning in dpois(y, mu, log = TRUE) : non-integer x = 2.639889
Warning in dpois(y, mu, log = TRUE) : non-integer x = 4.040859
Warning in dpois(y, mu, log = TRUE) : non-integer x = 3.278346
Warning in dpois(y, mu, log = TRUE) : non-integer x = 2.008770
Warning in dpois(y, mu, log = TRUE) : non-integer x = 4.712884
Warning in dpois(y, mu, log = TRUE) : non-integer x = 3.278346
Warning in dpois(y, mu, log = TRUE) : non-integer x = 2.008770
Warning in dpois(y, mu, log = TRUE) : non-integer x = 4.712884
Warning in dpois(y, mu, log = TRUE) : non-integer x = 3.237440
Warning in dpois(y, mu, log = TRUE) : non-integer x = 1.377650
Warning in dpois(y, mu, log = TRUE) : non-integer x = 5.384909
Warning in dpois(y, mu, log = TRUE) : non-integer x = 3.237440
Warning in dpois(y, mu, log = TRUE) : non-integer x = 1.377650
Warning in dpois(y, mu, log = TRUE) : non-integer x = 5.384909
Warning in dpois(y, mu, log = TRUE) : non-integer x = 3.196534
Warning in dpois(y, mu, log = TRUE) : non-integer x = 0.746531
Warning in dpois(y, mu, log = TRUE) : non-integer x = 6.056935
Warning in dpois(y, mu, log = TRUE) : non-integer x = 3.196534
Warning in dpois(y, mu, log = TRUE) : non-integer x = 0.746531
Warning in dpois(y, mu, log = TRUE) : non-integer x = 6.056935
Warning in dpois(y, mu, log = TRUE) : non-integer x = 3.155629
Warning in dpois(y, mu, log = TRUE) : non-integer x = 0.115412
Warning in dpois(y, mu, log = TRUE) : non-integer x = 6.728960
Warning in dpois(y, mu, log = TRUE) : non-integer x = 3.155629
Warning in dpois(y, mu, log = TRUE) : non-integer x = 0.115412
Warning in dpois(y, mu, log = TRUE) : non-integer x = 6.728960
Warning in dpois(y, mu, log = TRUE) : non-integer x = 2.734728
Warning in dpois(y, mu, log = TRUE) : non-integer x = 0.299661
Warning in dpois(y, mu, log = TRUE) : non-integer x = 6.965612
Warning in dpois(y, mu, log = TRUE) : non-integer x = 2.734728
Warning in dpois(y, mu, log = TRUE) : non-integer x = 0.299661
Warning in dpois(y, mu, log = TRUE) : non-integer x = 6.965612
Warning in dpois(y, mu, log = TRUE) : non-integer x = 2.228786
Warning in dpois(y, mu, log = TRUE) : non-integer x = 0.666383
Warning in dpois(y, mu, log = TRUE) : non-integer x = 7.104831
Warning in dpois(y, mu, log = TRUE) : non-integer x = 2.228786
Warning in dpois(y, mu, log = TRUE) : non-integer x = 0.666383
Warning in dpois(y, mu, log = TRUE) : non-integer x = 7.104831
Saddlepoint Distribution Approximations
Call :
saddle.distn(A = Afn, u = ufn, wdist = "p", type = "cond", t0 = city.t0,
data = city)
Quantiles of the Distribution
0.1% 1.216
0.5% 1.236
1.0% 1.248
2.5% 1.272
5.0% 1.301
10.0% 1.340
20.0% 1.393
50.0% 1.502
80.0% 1.618
90.0% 1.680
95.0% 1.732
97.5% 1.777
99.0% 1.830
99.5% 1.866
99.9% 1.938
Smoothing spline used 20 points in the range 1.182 to 2.061.
>
>
>
> cleanEx()
> nameEx("simplex")
> ### * simplex
>
> flush(stderr()); flush(stdout())
>
> ### Name: simplex
> ### Title: Simplex Method for Linear Programming Problems
> ### Aliases: simplex
> ### Keywords: optimize
>
> ### ** Examples
>
> # This example is taken from Exercise 7.5 of Gill, Murray,
> # and Wright (1991).
> enj <- c(200, 6000, 3000, -200)
> fat <- c(800, 6000, 1000, 400)
> vitx <- c(50, 3, 150, 100)
> vity <- c(10, 10, 75, 100)
> vitz <- c(150, 35, 75, 5)
> simplex(a = enj, A1 = fat, b1 = 13800, A2 = rbind(vitx, vity, vitz),
+ b2 = c(600, 300, 550), maxi = TRUE)
Linear Programming Results
Call : simplex(a = enj, A1 = fat, b1 = 13800, A2 = rbind(vitx, vity,
vitz), b2 = c(600, 300, 550), maxi = TRUE)
Maximization Problem with Objective Function Coefficients
x1 x2 x3 x4
200 6000 3000 -200
Optimal solution has the following values
x1 x2 x3 x4
0.0 0.0 13.8 0.0
The optimal value of the objective function is 41400.
>
>
>
> cleanEx()
> nameEx("smooth.f")
> ### * smooth.f
>
> flush(stderr()); flush(stdout())
>
> ### Name: smooth.f
> ### Title: Smooth Distributions on Data Points
> ### Aliases: smooth.f
> ### Keywords: smooth nonparametric
>
> ### ** Examples
>
> # Example 9.8 of Davison and Hinkley (1997) requires tilting the resampling
> # distribution of the studentized statistic to be centred at the observed
> # value of the test statistic 1.84. In the book exponential tilting was used
> # but it is also possible to use smooth.f.
> grav1 <- gravity[as.numeric(gravity[,2])>=7,]
> grav.fun <- function(dat, w, orig)
+ { strata <- tapply(dat[, 2], as.numeric(dat[, 2]))
+ d <- dat[, 1]
+ ns <- tabulate(strata)
+ w <- w/tapply(w, strata, sum)[strata]
+ mns <- tapply(d * w, strata, sum)
+ mn2 <- tapply(d * d * w, strata, sum)
+ s2hat <- sum((mn2 - mns^2)/ns)
+ c(mns[2]-mns[1], s2hat, (mns[2]-mns[1]-orig)/sqrt(s2hat))
+ }
> grav.z0 <- grav.fun(grav1,rep(1,26),0)
> grav.boot <- boot(grav1, grav.fun, R=499, stype="w",
+ strata=grav1[,2], orig=grav.z0[1])
> grav.sm <- smooth.f(grav.z0[3], grav.boot, index=3)
>
>
> # Now we can run another bootstrap using these weights
> grav.boot2 <- boot(grav1, grav.fun, R=499, stype="w",
+ strata=grav1[,2], orig=grav.z0[1],
+ weights=grav.sm)
>
>
> # Estimated p-values can be found from these as follows
> mean(grav.boot$t[,3] >= grav.z0[3])
[1] 0.01402806
> imp.prob(grav.boot2,t0=-grav.z0[3],t=-grav.boot2$t[,3])
$t0
2
-1.840118
$raw
[1] 0.02163715
$rat
[1] 0.02099078
$reg
[1] 0.02174393
>
>
> # Note that for the importance sampling probability we must
> # multiply everything by -1 to ensure that we find the correct
> # probability. Raw resampling is not reliable for probabilities
> # greater than 0.5. Thus
> 1-imp.prob(grav.boot2,index=3,t0=grav.z0[3])$raw
[1] -0.009155757
> # can give very strange results (negative probabilities).
>
>
>
> cleanEx()
> nameEx("tilt.boot")
> ### * tilt.boot
>
> flush(stderr()); flush(stdout())
>
> ### Name: tilt.boot
> ### Title: Non-parametric Tilted Bootstrap
> ### Aliases: tilt.boot
> ### Keywords: nonparametric
>
> ### ** Examples
>
> # Note that these examples can take a while to run.
>
>
> # Example 9.9 of Davison and Hinkley (1997).
> grav1 <- gravity[as.numeric(gravity[,2])>=7,]
> grav.fun <- function(dat, w, orig)
+ { strata <- tapply(dat[, 2], as.numeric(dat[, 2]))
+ d <- dat[, 1]
+ ns <- tabulate(strata)
+ w <- w/tapply(w, strata, sum)[strata]
+ mns <- tapply(d * w, strata, sum)
+ mn2 <- tapply(d * d * w, strata, sum)
+ s2hat <- sum((mn2 - mns^2)/ns)
+ c(mns[2]-mns[1],s2hat,(mns[2]-mns[1]-orig)/sqrt(s2hat))
+ }
> grav.z0 <- grav.fun(grav1,rep(1,26),0)
> tilt.boot(grav1, grav.fun, R=c(249,375,375), stype="w",
+ strata=grav1[,2], tilt=TRUE, index=3, orig=grav.z0[1])
TILTED BOOTSTRAP
Exponential tilting used
First 249 replicates untilted,
Next 375 replicates tilted to -2.821,
Next 375 replicates tilted to 1.636.
Call:
tilt.boot(data = grav1, statistic = grav.fun, R = c(249, 375,
375), stype = "w", strata = grav1[, 2], tilt = TRUE, index = 3,
orig = grav.z0[1])
Bootstrap Statistics :
original bias std. error
t1* 2.846154 -0.4487564 2.500644
t2* 2.392353 -0.3221155 1.187574
t3* 0.000000 -0.8862944 2.208945
>
>
> # Example 9.10 of Davison and Hinkley (1997) requires a balanced
> # importance resampling bootstrap to be run. In this example we
> # show how this might be run.
> acme.fun <- function(data, i, bhat)
+ { d <- data[i,]
+ n <- nrow(d)
+ d.lm <- glm(d$acme~d$market)
+ beta.b <- coef(d.lm)[2]
+ d.diag <- glm.diag(d.lm)
+ SSx <- (n-1)*var(d$market)
+ tmp <- (d$market-mean(d$market))*d.diag$res*d.diag$sd
+ sr <- sqrt(sum(tmp^2))/SSx
+ c(beta.b, sr, (beta.b-bhat)/sr)
+ }
> acme.b <- acme.fun(acme,1:nrow(acme),0)
> acme.boot1 <- tilt.boot(acme, acme.fun, R=c(499, 250, 250),
+ stype="i", sim="balanced", alpha=c(0.05, 0.95),
+ tilt=TRUE, index=3, bhat=acme.b[1])
>
>
>
> cleanEx()
> nameEx("tsboot")
> ### * tsboot
>
> flush(stderr()); flush(stdout())
>
> ### Name: tsboot
> ### Title: Bootstrapping of Time Series
> ### Aliases: tsboot ts.return
> ### Keywords: nonparametric ts
>
> ### ** Examples
>
> lynx.fun <- function(tsb)
+ { ar.fit <- ar(tsb, order.max=25)
+ c(ar.fit$order, mean(tsb), tsb)
+ }
>
> # the stationary bootstrap with mean block length 20
> lynx.1 <- tsboot(log(lynx), lynx.fun, R=99, l=20, sim="geom")
>
> # the fixed block bootstrap with length 20
> lynx.2 <- tsboot(log(lynx), lynx.fun, R=99, l=20, sim="fixed")
>
> # Now for model based resampling we need the original model
> # Note that for all of the bootstraps which use the residuals as their
> # data, we set orig.t to FALSE since the function applied to the residual
> # time series will be meaningless.
> lynx.ar <- ar(log(lynx))
> lynx.model <- list(order=c(lynx.ar$order,0,0),ar=lynx.ar$ar)
> lynx.res <- lynx.ar$resid[!is.na(lynx.ar$resid)]
> lynx.res <- lynx.res - mean(lynx.res)
>
> lynx.sim <- function(res,n.sim, ran.args) {
+ # random generation of replicate series using arima.sim
+ rg1 <- function(n, res)
+ sample(res, n, replace=TRUE)
+ ts.orig <- ran.args$ts
+ ts.mod <- ran.args$model
+ mean(ts.orig)+ts(arima.sim(model=ts.mod, n=n.sim,
+ rand.gen=rg1, res=as.vector(res)))
+ }
>
> lynx.3 <- tsboot(lynx.res, lynx.fun, R=99, sim="model", n.sim=114,
+ orig.t=FALSE, ran.gen=lynx.sim,
+ ran.args=list(ts=log(lynx), model=lynx.model))
>
> # For "post-blackening" we need to define another function
> lynx.black <- function(res, n.sim, ran.args)
+ { ts.orig <- ran.args$ts
+ ts.mod <- ran.args$model
+ mean(ts.orig) + ts(arima.sim(model=ts.mod,n=n.sim,innov=res))
+ }
>
> # Now we can run apply the two types of block resampling again but this
> # time applying post-blackening.
> lynx.1b <- tsboot(lynx.res, lynx.fun, R=99, l=20, sim="fixed",
+ n.sim=114, orig.t=FALSE, ran.gen=lynx.black,
+ ran.args=list(ts=log(lynx), model=lynx.model))
>
> lynx.2b <- tsboot(lynx.res, lynx.fun, R=99, l=20, sim="geom",
+ n.sim=114, orig.t=FALSE, ran.gen=lynx.black,
+ ran.args=list(ts=log(lynx), model=lynx.model))
>
> # To compare the observed order of the bootstrap replicates we
> # proceed as follows.
> table(lynx.1$t[,1])
2 3 4 5 7 8 10 11 12 13 14
16 19 38 4 6 3 1 9 1 1 1
> table(lynx.1b$t[,1])
2 3 4 5 6 7 8 11 12 14 15
6 2 22 6 4 6 3 40 7 1 2
> table(lynx.2$t[,1])
2 3 4 5 6 7 8 10 11 13
12 18 51 5 2 3 1 2 4 1
> table(lynx.2b$t[,1])
2 3 4 5 6 7 8 9 10 11 12 13 15 21
2 1 21 4 1 10 4 1 3 45 3 1 2 1
> table(lynx.3$t[,1])
2 3 4 5 6 7 8 9 10 11 12 13 14 15
4 8 11 2 1 4 2 2 2 54 6 1 1 1
> # Notice that the post-blackened and model-based bootstraps preserve
> # the true order of the model (11) in many more cases than the others.
>
>
>
> cleanEx()
> nameEx("var.linear")
> ### * var.linear
>
> flush(stderr()); flush(stdout())
>
> ### Name: var.linear
> ### Title: Linear Variance Estimate
> ### Aliases: var.linear
> ### Keywords: nonparametric
>
> ### ** Examples
>
> # To estimate the variance of the ratio of means for the city data.
> ratio <- function(d,w)
+ sum(d$x * w)/sum(d$u * w)
> var.linear(empinf(data=city,statistic=ratio))
[1] 0.03248701
>
>
>
> ### * <FOOTER>
> ###
> cat("Time elapsed: ", proc.time() - get("ptime", pos = 'CheckExEnv'),"\n")
Time elapsed: 104.074 0.584 104.987 0 0
> grDevices::dev.off()
null device
1
> ###
> ### Local variables: ***
> ### mode: outline-minor ***
> ### outline-regexp: "\\(> \\)?### [*]+" ***
> ### End: ***
> quit('no')
|