Rev 81944 | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed
R Under development (unstable) (2022-03-19 r81942) -- "Unsuffered Consequences"Copyright (C) 2022 The R Foundation for Statistical ComputingPlatform: x86_64-pc-linux-gnu (64-bit)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.> ## tests of the simulate.lm method, added Feb 2009>> options(digits = 5)>> ## cases should be named> hills <- readRDS("hills.rds") # copied from package MASS> fit1 <- lm(time ~ dist, data = hills)> set.seed(1)> simulate(fit1, nsim = 3)sim_1 sim_2 sim_3Greenmantle 3.4841 7.7039 25.4746Carnethy 48.8068 37.2737 30.9745Craig Dunain 28.4665 43.9584 57.3295Ben Rha 89.4727 79.5895 38.9971Ben Lomond 68.3785 77.0326 36.7858Goatfell 45.4299 58.5197 67.6189Bens of Jura 138.1736 123.3906 119.6004Cairnpapple 59.8758 59.0504 45.1641Scolty 48.3017 47.9202 38.2951Traprain 39.0478 31.3974 33.3777Lairig Ghru 258.5807 214.2935 217.0639Dollar 44.5912 44.0871 34.1140Lomonds 61.9013 89.6352 97.8082Cairn Table 0.9461 42.9001 14.7382Eildon Two 55.0951 50.2295 44.4990Cairngorm 77.5672 86.4083 85.1081Seven Hills 111.4626 99.5722 133.0006Knock Hill 38.9856 26.9579 14.0804Black Hill 49.0344 10.1091 40.0303Creag Beag 52.8285 69.5738 46.3069Kildcon Hill 38.4895 59.6709 9.3243Meall Ant-Suidhe 39.9240 16.9877 48.4197Half Ben Nevis 46.6300 24.3056 68.2987Cow Hill -27.8787 23.1894 25.7935N Berwick Law 32.5197 17.4555 51.8170Creag Dubh 27.3610 76.4071 39.6261Burnswark 42.0330 44.3590 19.6667Largo Law 7.4616 50.5758 25.3716Criffel 39.7654 49.8660 24.8692Acmony 45.1519 21.9790 27.3645Ben Nevis 105.5773 82.2313 66.0840Knockfarrel 43.0908 9.1228 45.9825Two Breweries 152.8438 174.3537 126.9294Cockleroi 31.5726 35.7046 35.7999Moffat Chase 134.2882 205.1244 148.7057>> ## and weights should be taken into account> fit2 <- lm(time ~ -1 + dist + climb, hills[-18, ], weight = 1/dist^2)> coef(summary(fit2))Estimate Std. Error t value Pr(>|t|)dist 4.8999847 0.4737032 10.3440 9.8468e-12climb 0.0084718 0.0016869 5.0221 1.8636e-05> set.seed(1); ( ys <- simulate(fit2, nsim = 3) )sim_1 sim_2 sim_3Greenmantle 15.754 13.355 18.247Carnethy 51.988 47.396 67.247Craig Dunain 30.614 34.000 40.672Ben Rha 58.825 42.959 36.719Ben Lomond 68.579 76.460 71.455Goatfell 55.088 71.287 53.926Bens of Jura 151.910 138.573 116.292Cairnpapple 41.841 34.234 38.413Scolty 34.958 35.733 28.443Traprain 32.564 39.177 34.915Lairig Ghru 209.113 130.333 157.652Dollar 43.936 36.921 37.675Lomonds 57.642 69.616 58.280Cairn Table 16.646 39.532 32.599Eildon Two 41.230 34.111 41.536Cairngorm 73.841 85.681 54.935Seven Hills 86.948 94.364 97.870Black Hill 35.952 27.000 32.437Creag Beag 37.808 34.432 39.509Kildcon Hill 19.520 12.910 16.075Meall Ant-Suidhe 33.970 36.271 31.514Half Ben Nevis 54.038 63.231 50.087Cow Hill 17.615 16.486 16.037N Berwick Law 12.152 15.778 24.416Creag Dubh 39.714 39.457 42.478Burnswark 35.747 35.141 41.549Largo Law 31.552 47.902 42.693Criffel 34.452 46.350 51.317Acmony 25.679 33.145 20.575Ben Nevis 91.620 86.634 78.946Knockfarrel 44.906 28.781 25.088Two Breweries 129.888 136.598 121.358Cockleroi 31.482 18.866 25.682Moffat Chase 138.983 177.836 141.436> for(i in seq_len(3))+ print(coef(summary(update(fit2, ys[, i] ~ .))))Estimate Std. Error t value Pr(>|t|)dist 4.8759333 0.4295826 11.3504 9.3646e-13climb 0.0091824 0.0015298 6.0023 1.0781e-06Estimate Std. Error t value Pr(>|t|)dist 4.6969341 0.4406227 10.66 4.6417e-12climb 0.0099795 0.0015691 6.36 3.8442e-07Estimate Std. Error t value Pr(>|t|)dist 4.8215499 0.420077 11.4778 7.0162e-13climb 0.0090388 0.001496 6.0422 9.6065e-07> ## should be identical to glm(*, gaussian):> fit2. <- glm(time ~ -1 + dist + climb, family=gaussian, data=hills[-18, ],+ weight = 1/dist^2)> set.seed(1); ys. <- simulate(fit2., nsim = 3)> stopifnot(all.equal(ys, ys.))>> ## Poisson fit> load("anorexia.rda") # copied from package MASS> fit3 <- glm(Postwt ~ Prewt + Treat + offset(Prewt),+ family = gaussian, data = anorexia)> coef(summary(fit3))Estimate Std. Error t value Pr(>|t|)(Intercept) 49.77111 13.39096 3.7168 0.00041011Prewt -0.56554 0.16118 -3.5087 0.00080343TreatCont -4.09707 1.89349 -2.1638 0.03399931TreatFT 4.56306 2.13334 2.1389 0.03603508> set.seed(1)> simulate(fit3, nsim = 8)sim_1 sim_2 sim_3 sim_4 sim_5 sim_6 sim_7 sim_81 76.364 84.997 72.948 78.581 83.762 62.645 70.046 87.6552 85.796 77.997 79.276 75.769 91.529 93.684 84.340 95.6383 79.726 76.809 100.122 90.039 82.835 81.123 89.696 75.9794 88.956 79.858 77.946 77.512 80.451 74.824 76.441 76.0825 81.905 76.512 70.629 67.511 81.309 78.424 85.830 87.6966 78.312 84.045 72.589 84.052 74.084 88.309 83.858 76.2627 87.004 84.121 86.744 79.204 96.013 88.336 79.083 65.9588 83.454 74.188 78.173 75.923 79.240 82.265 82.812 71.7719 84.710 76.723 78.472 72.621 86.034 76.696 77.664 73.94210 77.605 78.792 73.251 92.318 86.401 70.223 92.105 80.06711 89.938 87.609 69.008 77.078 79.035 76.676 79.261 76.57112 86.931 73.579 76.708 73.007 82.077 86.150 90.162 85.82613 76.661 85.140 87.974 82.372 87.232 75.252 82.427 78.04814 64.151 81.929 75.270 81.442 72.297 79.125 58.615 82.21615 84.154 83.722 66.643 69.424 90.060 68.155 66.771 73.74916 78.944 77.135 92.302 59.098 76.581 79.200 76.298 87.56317 82.577 85.272 85.657 78.221 94.233 83.589 84.343 77.54518 89.624 84.902 81.372 87.019 93.590 82.020 66.690 85.06619 87.943 78.426 89.599 81.795 82.791 81.068 88.923 76.03820 84.445 88.729 86.486 79.615 84.259 92.607 76.083 81.75221 89.233 90.918 78.499 86.734 75.671 88.142 77.567 82.48722 87.800 87.229 97.737 74.063 84.597 90.098 71.487 70.58823 80.777 91.330 78.478 87.911 87.540 73.815 70.112 79.25124 65.463 83.242 69.404 79.307 80.036 80.492 79.738 87.58125 81.411 68.177 76.078 82.021 73.917 85.144 80.640 81.84126 83.949 80.341 85.789 91.557 79.765 83.947 69.702 85.34127 83.658 76.200 100.851 86.305 84.495 69.886 77.737 76.42528 76.394 83.353 87.395 80.525 94.118 89.063 90.396 94.81629 81.843 80.851 88.369 93.295 81.802 71.887 82.018 85.73230 88.574 85.951 85.119 71.700 84.813 79.997 100.768 82.50531 93.966 78.128 82.154 80.683 75.454 93.724 93.178 95.94332 87.591 89.411 88.065 86.524 91.757 92.604 92.463 82.93733 93.707 86.434 96.498 89.842 100.128 98.619 91.036 93.11834 82.545 95.253 97.402 90.041 93.367 85.060 84.870 91.86535 75.353 89.964 92.132 85.913 90.648 84.194 80.037 89.16536 81.849 91.097 93.174 87.587 71.698 78.295 89.128 82.60337 83.949 89.381 78.108 86.214 90.064 97.816 97.030 83.78138 88.111 100.264 95.391 86.797 91.708 88.839 96.085 91.00339 92.769 80.657 86.627 89.946 82.627 80.103 79.418 88.67640 88.333 79.786 72.769 91.006 84.197 89.045 71.711 83.13741 79.035 90.178 83.819 63.414 74.154 87.681 79.418 89.38442 82.934 80.161 83.594 88.698 89.442 97.930 87.778 84.24243 90.825 84.515 96.182 88.577 83.679 81.754 95.389 81.07544 89.716 83.090 80.486 82.864 74.882 83.104 76.630 89.58145 83.067 85.640 84.871 94.510 85.309 84.969 90.416 72.50946 81.416 84.405 79.890 83.637 95.874 83.731 87.982 89.08847 89.853 90.757 86.073 85.324 84.976 84.750 95.640 90.77648 88.370 81.770 85.813 88.991 88.121 80.944 82.813 81.43849 83.831 81.084 79.509 96.615 91.220 94.676 82.122 76.81950 94.065 97.289 93.711 89.801 87.947 83.049 79.914 85.16051 88.740 84.464 77.532 83.016 83.503 83.253 82.351 96.77752 80.127 83.145 77.085 76.100 80.701 88.951 81.871 79.20953 88.863 85.784 96.540 84.173 91.644 94.332 102.886 70.21254 76.995 89.849 77.787 78.317 77.455 79.488 101.948 90.54455 97.743 87.230 90.618 85.936 89.461 84.197 86.580 84.24556 104.562 90.479 88.083 93.494 88.722 94.396 83.459 87.17757 87.962 85.768 93.382 84.580 74.720 97.627 76.757 82.04458 84.412 89.435 103.483 110.184 81.867 89.945 95.289 91.54059 94.153 90.597 101.249 91.266 96.569 80.198 82.567 95.07160 91.060 87.893 89.693 99.889 90.667 103.929 107.945 87.90261 105.676 92.626 72.970 72.943 94.523 98.931 82.737 84.68362 87.470 77.149 105.173 92.915 100.915 82.787 88.520 95.39763 100.074 97.400 99.915 86.075 105.545 94.806 121.849 93.53364 86.419 75.502 90.001 92.642 90.950 73.946 78.485 85.10865 84.122 87.208 89.215 92.087 91.960 93.284 91.455 84.94166 91.104 86.100 93.346 86.942 88.441 101.037 82.062 96.07067 77.408 85.453 88.856 99.244 101.014 78.578 92.429 83.06668 98.275 87.651 90.984 83.155 92.209 82.608 81.955 93.97569 91.681 77.253 87.819 86.560 82.422 86.137 91.151 96.23470 108.553 101.603 83.831 86.407 92.306 88.639 91.321 90.12971 95.016 80.079 98.591 87.035 78.307 77.509 83.441 97.61872 87.308 89.028 102.868 98.858 90.900 95.758 92.341 99.148>> ## two-column binomial fit> ldose <- rep(0:5, 2)> numdead <- c(1, 4, 9, 13, 18, 20, 0, 2, 6, 10, 12, 16)> sex <- factor(rep(c("M", "F"), c(6, 6)))> SF <- cbind(numdead, numalive = 20 - numdead)> fit4 <- glm(SF ~ sex + ldose - 1, family = binomial)> coef(summary(fit4))Estimate Std. Error z value Pr(>|z|)sexF -3.4732 0.46852 -7.4130 1.2344e-13sexM -2.3724 0.38551 -6.1539 7.5579e-10ldose 1.0642 0.13108 8.1190 4.7015e-16> set.seed(1)> ( ys <- simulate(fit4, nsim = 3) )sim_1.numdead sim_1.numalive sim_2.numdead sim_2.numalive sim_3.numdead1 1 19 2 18 12 4 16 4 16 43 9 11 10 10 44 11 9 14 6 155 19 1 17 3 166 18 2 16 4 207 2 18 0 20 08 2 18 3 17 29 5 15 7 13 410 5 15 7 13 711 15 5 13 7 1212 19 1 19 1 17sim_3.numalive1 192 163 164 55 46 07 208 189 1610 1311 812 3> for(i in seq_len(3))+ print(coef(summary(update(fit4, ys[, i] ~ .))))Estimate Std. Error z value Pr(>|z|)sexF -3.3079 0.45218 -7.3154 2.5656e-13sexM -2.5067 0.39240 -6.3880 1.6812e-10ldose 1.0482 0.12869 8.1454 3.7800e-16Estimate Std. Error z value Pr(>|z|)sexF -2.84478 0.40661 -6.9963 2.6289e-12sexM -2.11845 0.35818 -5.9145 3.3286e-09ldose 0.90935 0.11578 7.8541 4.0273e-15Estimate Std. Error z value Pr(>|z|)sexF -3.9838 0.52156 -7.6384 2.1996e-14sexM -2.8717 0.43047 -6.6712 2.5376e-11ldose 1.1487 0.14102 8.1459 3.7655e-16>> ## same via proportions> fit5 <- glm(numdead/20 ~ sex + ldose - 1, family = binomial,+ weights = rep(20, 12))> set.seed(1)> ( ys <- simulate(fit5, nsim = 3) )sim_1 sim_2 sim_31 0.05 0.10 0.052 0.20 0.20 0.203 0.45 0.50 0.204 0.55 0.70 0.755 0.95 0.85 0.806 0.90 0.80 1.007 0.10 0.00 0.008 0.10 0.15 0.109 0.25 0.35 0.2010 0.25 0.35 0.3511 0.75 0.65 0.6012 0.95 0.95 0.85> for(i in seq_len(3))+ print(coef(summary(update(fit5, ys[, i] ~ .))))Estimate Std. Error z value Pr(>|z|)sexF -3.3079 0.45218 -7.3154 2.5656e-13sexM -2.5067 0.39240 -6.3880 1.6812e-10ldose 1.0482 0.12869 8.1454 3.7800e-16Estimate Std. Error z value Pr(>|z|)sexF -2.84478 0.40661 -6.9963 2.6289e-12sexM -2.11845 0.35818 -5.9145 3.3286e-09ldose 0.90935 0.11578 7.8541 4.0273e-15Estimate Std. Error z value Pr(>|z|)sexF -3.9838 0.52156 -7.6384 2.1996e-14sexM -2.8717 0.43047 -6.6712 2.5376e-11ldose 1.1487 0.14102 8.1459 3.7655e-16>>> ## factor binomial fit> load("birthwt.rda") # copied from package MASS> bwt <- with(birthwt, {+ race <- factor(race, labels = c("white", "black", "other"))+ table(ptl)+ ptd <- factor(ptl > 0)+ table(ftv)+ ftv <- factor(ftv)+ levels(ftv)[-(1:2)] <- "2+"+ data.frame(low = factor(low), age, lwt, race,+ smoke = (smoke > 0), ptd, ht = (ht > 0), ui = (ui > 0), ftv)+ })> fit6 <- glm(low ~ ., family = binomial, data = bwt)> coef(summary(fit6))Estimate Std. Error z value Pr(>|z|)(Intercept) 0.823019 1.2447143 0.66121 0.5084769age -0.037234 0.0387024 -0.96207 0.3360159lwt -0.015653 0.0070804 -2.21075 0.0270532raceblack 1.192413 0.5359646 2.22480 0.0260948raceother 0.740685 0.4617443 1.60410 0.1086916smokeTRUE 0.755528 0.4250166 1.77764 0.0754623ptdTRUE 1.343763 0.4806207 2.79589 0.0051757htTRUE 1.913166 0.7207369 2.65446 0.0079436uiTRUE 0.680195 0.4643403 1.46486 0.1429580ftv1 -0.436380 0.4793936 -0.91027 0.3626779ftv2+ 0.179009 0.4563778 0.39224 0.6948827> set.seed(1)> ys <- simulate(fit6, nsim = 3)> ys[1:10, ]sim_1 sim_2 sim_31 0 0 02 0 0 03 0 0 04 1 0 15 0 1 16 1 0 17 1 0 08 0 0 09 0 0 010 0 0 1> for(i in seq_len(3))+ print(coef(summary(update(fit6, ys[, i] ~ .))))Estimate Std. Error z value Pr(>|z|)(Intercept) -0.284354 1.2297301 -0.23123 0.817134age -0.011485 0.0389166 -0.29513 0.767897lwt -0.012314 0.0069041 -1.78353 0.074500raceblack 0.844051 0.5514350 1.53064 0.125857raceother 0.565993 0.4665978 1.21302 0.225122smokeTRUE 1.024337 0.4253416 2.40827 0.016028ptdTRUE 1.578045 0.4887012 3.22906 0.001242htTRUE 1.171556 0.7060321 1.65935 0.097045uiTRUE 0.384214 0.4835840 0.79451 0.426897ftv1 -0.790699 0.5271049 -1.50008 0.133594ftv2+ 0.522245 0.4498448 1.16094 0.245665Estimate Std. Error z value Pr(>|z|)(Intercept) 0.916922 1.433819 0.63950 5.2250e-01age 0.034662 0.041752 0.83019 4.0643e-01lwt -0.036299 0.009577 -3.79021 1.5052e-04raceblack 2.588992 0.631372 4.10058 4.1211e-05raceother 1.073046 0.522576 2.05338 4.0036e-02smokeTRUE 0.710930 0.478870 1.48460 1.3765e-01ptdTRUE 0.679588 0.503299 1.35027 1.7693e-01htTRUE 1.988224 0.801811 2.47967 1.3151e-02uiTRUE 1.080176 0.489033 2.20880 2.7189e-02ftv1 -0.418153 0.549009 -0.76165 4.4627e-01ftv2+ 0.657962 0.500605 1.31433 1.8873e-01Estimate Std. Error z value Pr(>|z|)(Intercept) 1.4927579 1.2779154 1.168119 0.24275859age -0.0986485 0.0415094 -2.376532 0.01747625lwt -0.0088426 0.0070635 -1.251885 0.21061194raceblack 0.2106382 0.5622160 0.374657 0.70791549raceother 0.8608985 0.4814556 1.788116 0.07375727smokeTRUE 0.7884781 0.4449757 1.771958 0.07640158ptdTRUE 1.9686209 0.5435116 3.622040 0.00029229htTRUE 1.7835637 0.7279712 2.450047 0.01428375uiTRUE 1.4816743 0.5139743 2.882779 0.00394184ftv1 -0.7388479 0.5090637 -1.451386 0.14667244ftv2+ -0.0097438 0.4670024 -0.020865 0.98335371>> ## This requires MASS::gamma.shape> if(!require("MASS", quietly = TRUE)) {+ message("skipping tests requiring the MASS package")+ q()+ }>> ## gamma fit, from example(glm)> clotting <- data.frame(u = c(5,10,15,20,30,40,60,80,100),+ lot1 = c(118,58,42,35,27,25,21,19,18))> fit7 <- glm(lot1 ~ log(u), data = clotting, family = Gamma)> coef(summary(fit7))Estimate Std. Error t value Pr(>|t|)(Intercept) -0.016554 0.00092755 -17.847 4.2791e-07log(u) 0.015343 0.00041496 36.975 2.7512e-09> set.seed(1)> ( ys <- simulate(fit7, nsim = 3) )sim_1 sim_2 sim_31 119.451 118.552 123.1402 56.310 51.798 48.7473 42.193 39.456 40.8434 34.581 33.905 35.5805 26.208 26.971 27.2106 25.476 25.840 24.4377 22.287 22.151 21.3758 20.206 20.503 19.6269 18.224 19.094 17.952> for(i in seq_len(3))+ print(coef(summary(update(fit7, ys[, i] ~ .))))Estimate Std. Error t value Pr(>|t|)(Intercept) -0.016008 0.00077887 -20.553 1.6197e-07log(u) 0.015044 0.00034611 43.465 8.9084e-10Estimate Std. Error t value Pr(>|t|)(Intercept) -0.015483 0.00050102 -30.903 9.5910e-09log(u) 0.014935 0.00021999 67.889 3.9547e-11Estimate Std. Error t value Pr(>|t|)(Intercept) -0.016850 0.00075120 -22.431 8.8554e-08log(u) 0.015569 0.00033659 46.256 5.7704e-10>>> proc.time()user system elapsed0.597 0.096 0.670