#------------------------------------------------------------------- # 1.1 SIMPLE REGRESSION USING SPLUS #------------------------------------------------------------------- > x <- c(6, 6.3, 6.5, 6.8, 7, 7.1, 7.5, 7.5, 7.6) > y <- c(39, 58, 49, 53, 80, 86, 115, 124, 104) > plot(x, y, main = "Figure 1.1 Simple regression") > glm.out <- glm(y ~ x) > glm.out Call: glm(formula = y ~ x) Coefficients: (Intercept) x -270.3485 50.41952 Degrees of Freedom: 9 Total; 7 Residual Residual Deviance: 1013.765 > summary(glm.out) Call: glm(formula = y ~ x) Deviance Residuals: Min 1Q Median 3Q Max -19.50428 -8.378425 -1.630137 7.202055 16.20205 Coefficients: Value Std. Error t value (Intercept) -270.34846 51.862458 -5.212797 x 50.41952 7.469724 6.749851 (Dispersion Parameter for Gaussian family taken to be 144.8236 ) Null Deviance: 7612 on 8 degrees of freedom Residual Deviance: 1013.765 on 7 degrees of freedom Number of Fisher Scoring Iterations: 1 Correlation of Coefficients: (Intercept) x -0.9970042 > lines(x, glm.out$fitted.values) > glm(y ~ 1) Call: glm(formula = y ~ 1) Coefficients: (Intercept) 78.66667 Degrees of Freedom: 9 Total; 8 Residual Residual Deviance: 7612 > r <- (7612 - 1013.765)/7612 > r [1] 0.8668202 > tstat <- 50.41952/7.469724 > tstat [1] 6.74985 > tstat^2/(7 + tstat^2) [1] 0.8668201 #------------------------------------------------------------------- # 1.2 MAYER'S ESTIMATE OF THE LIBRATION OF THE MOON #------------------------------------------------------------------- > m <- matrix(scan("/glim/datasets/moon.dat"), byrow = T, ncol = 5) > m [,1] [,2] [,3] [,4] [,5] [1,] 13 10 0.8836 -0.4682 1 [2,] 13 8 0.9996 -0.0282 1 [3,] 13 12 0.9899 0.1421 1 [4,] 14 15 0.2221 0.9750 3 [5,] 14 42 0.0006 1.0000 3 [6,] 13 1 0.9308 -0.3654 1 [7,] 14 31 0.0602 0.9982 3 [8,] 14 57 -0.1570 0.9876 2 [9,] 13 5 0.9097 -0.4152 1 [10,] 13 2 1.0000 0.0055 1 [11,] 13 12 0.9689 0.2476 1 [12,] 13 11 0.8878 0.4602 1 [13,] 13 34 0.7549 0.6558 3 [14,] 13 53 0.5755 0.8178 3 [15,] 13 58 0.3608 0.9326 3 [16,] 14 14 0.1302 0.9915 3 [17,] 14 56 -0.1068 0.9943 3 [18,] 14 47 -0.3363 0.9418 2 [19,] 15 56 -0.8560 0.5170 2 [20,] 13 29 0.8002 0.5997 3 [21,] 15 55 -0.9952 -0.0982 2 [22,] 15 39 -0.8409 0.5412 2 [23,] 16 9 -0.9429 0.3330 2 [24,] 16 22 -0.9768 0.2141 2 [25,] 15 38 -0.6262 -0.7797 2 [26,] 14 54 -0.4091 -0.9125 2 [27,] 13 7 0.9284 -0.3716 1 > y <- - m[, 1] - m[, 2]/60 > y [1] -13.16667 -13.13333 -13.20000 -14.25000 -14.70000 -13.01667 -14.51667 [8] -14.95000 -13.08333 -13.03333 -13.20000 -13.18333 -13.56667 -13.88333 [15] -13.96667 -14.23333 -14.93333 -14.78333 -15.93333 -13.48333 -15.91667 [22] -15.65000 -16.15000 -16.36667 -15.63333 -14.90000 -13.11667 > alpha <- m[, 3] > asin <- m[, 4] > grp <- m[, 5] > glm.out <- glm(y ~ alpha + asin) > glm.out Call: glm(formula = y ~ alpha + asin) Coefficients: (Intercept) alpha asin -14.55825 1.505795 -0.07192118 Degrees of Freedom: 27 Total; 24 Residual Residual Deviance: 0.5688748 > summary(glm.out) Call: glm(formula = y ~ alpha + asin) Deviance Residuals: Min 1Q Median 3Q Max -0.3221622 -0.09243684 0.01687288 0.09446685 0.3490464 Coefficients: Value Std. Error t value (Intercept) -14.55824555 0.03539838 -411.268737 alpha 1.50579501 0.04173283 36.081784 asin -0.07192118 0.05083137 -1.414897 (Dispersion Parameter for Gaussian family taken to be 0.0237031 ) Null Deviance: 32.13944 on 26 degrees of freedom Residual Deviance: 0.5688748 on 24 degrees of freedom Number of Fisher Scoring Iterations: 1 Correlation of Coefficients: (Intercept) alpha alpha -0.2780981 asin -0.4993381 0.1116904 > x <- cbind(rep(1, 27), alpha, asin) > x alpha asin [1,] 1 0.8836 -0.4682 [2,] 1 0.9996 -0.0282 [3,] 1 0.9899 0.1421 [4,] 1 0.2221 0.9750 [5,] 1 0.0006 1.0000 [6,] 1 0.9308 -0.3654 [7,] 1 0.0602 0.9982 [8,] 1 -0.1570 0.9876 [9,] 1 0.9097 -0.4152 [10,] 1 1.0000 0.0055 [11,] 1 0.9689 0.2476 [12,] 1 0.8878 0.4602 [13,] 1 0.7549 0.6558 [14,] 1 0.5755 0.8178 [15,] 1 0.3608 0.9326 [16,] 1 0.1302 0.9915 [17,] 1 -0.1068 0.9943 [18,] 1 -0.3363 0.9418 [19,] 1 -0.8560 0.5170 [20,] 1 0.8002 0.5997 [21,] 1 -0.9952 -0.0982 [22,] 1 -0.8409 0.5412 [23,] 1 -0.9429 0.3330 [24,] 1 -0.9768 0.2141 [25,] 1 -0.6262 -0.7797 [26,] 1 -0.4091 -0.9125 [27,] 1 0.9284 -0.3716 > beta.ls <- solve(t(x) %*% x, t(x) %*% y) > beta.ls [,1] -14.55824555 alpha 1.50579501 asin -0.07192118 > g <- cbind(grp == 1, grp == 2, grp == 3) * 1 > g [,1] [,2] [,3] [1,] 1 0 0 [2,] 1 0 0 [3,] 1 0 0 [4,] 0 0 1 [5,] 0 0 1 [6,] 1 0 0 [7,] 0 0 1 [8,] 0 1 0 [9,] 1 0 0 [10,] 1 0 0 [11,] 1 0 0 [12,] 1 0 0 [13,] 0 0 1 [14,] 0 0 1 [15,] 0 0 1 [16,] 0 0 1 [17,] 0 0 1 [18,] 0 1 0 [19,] 0 1 0 [20,] 0 0 1 [21,] 0 1 0 [22,] 0 1 0 [23,] 0 1 0 [24,] 0 1 0 [25,] 0 1 0 [26,] 0 1 0 [27,] 1 0 0 > beta.m <- solve(t(g) %*% x, t(g) %*% y) > beta.m [,1] -14.54719339 alpha 1.49580457 asin -0.09961272 > sum((y - x %*% beta.m)^2) [1] 0.5766745 > summary(glm.out)$cov.unscaled * summary(glm.out)$dispersion (Intercept) alpha asin (Intercept) 0.0012530451 -0.0004108272 -0.0008984831 alpha -0.0004108272 0.0017416292 0.0002369330 asin -0.0008984831 0.0002369330 0.0025838286 > a <- solve(t(g) %*% x, t(g)) > a %*% t(a) * summary(glm.out)$dispersion alpha asin 0.0016040221 -0.0005550267 -0.0018779456 alpha -0.0005550267 0.0020089745 0.0005190049 asin -0.0018779456 0.0005190049 0.0053867812 > plot(asin, alpha, type = "n", main = "Figure 1.2 Mayer's groups") > for(i in 1:3) text(asin[grp == i], alpha[grp == i], i) #------------------------------------------------------------------- # 1.3 MULTIPLE REGRESSION #------------------------------------------------------------------- > m <- matrix(scan("/glim/datasets/peru"), byrow = T, ncol = 10) > m [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [1,] 21 1 71.0 1629 8.0 7.0 12.7 88 170 76 [2,] 22 6 56.5 1569 3.3 5.0 8.0 64 120 60 [3,] 24 5 56.0 1561 3.3 1.3 4.3 68 125 75 [4,] 24 1 61.0 1619 3.7 3.0 4.3 52 148 120 [5,] 25 1 65.0 1566 9.0 12.7 20.7 72 140 78 [6,] 27 19 62.0 1639 3.0 3.3 5.7 72 106 72 [7,] 28 5 53.0 1494 7.3 4.7 8.0 64 120 76 [8,] 28 25 53.0 1568 3.7 4.3 0.0 80 108 62 [9,] 31 6 65.0 1540 10.3 9.0 10.0 76 124 70 [10,] 32 13 57.0 1530 5.7 4.0 6.0 60 134 64 [11,] 33 13 66.5 1622 6.0 5.7 8.3 68 116 76 [12,] 33 10 59.1 1486 6.7 5.3 10.3 72 114 74 [13,] 34 15 64.0 1578 3.3 5.3 7.0 88 130 80 [14,] 35 18 69.5 1645 9.3 5.0 7.0 60 118 68 [15,] 35 2 64.0 1648 3.0 3.7 6.7 60 138 78 [16,] 36 12 56.5 1521 3.3 5.0 11.7 72 134 86 [17,] 36 15 57.0 1547 3.0 3.0 6.0 84 120 70 [18,] 37 16 55.0 1505 4.3 5.0 7.0 64 120 76 [19,] 37 17 57.0 1473 6.0 5.3 11.7 72 114 80 [20,] 38 10 58.0 1538 8.7 6.0 13.0 64 124 64 [21,] 38 18 59.5 1513 5.3 4.0 7.7 80 114 66 [22,] 38 11 61.0 1653 4.0 3.3 4.0 76 136 78 [23,] 38 11 57.0 1566 3.0 3.0 3.0 60 126 72 [24,] 39 21 57.5 1580 4.0 3.0 5.0 64 124 62 [25,] 39 24 74.0 1647 7.3 6.3 15.7 64 128 84 [26,] 39 14 72.0 1620 6.3 7.7 13.3 68 134 92 [27,] 41 25 62.5 1637 6.0 5.3 8.0 76 112 80 [28,] 41 32 68.0 1528 10.0 5.0 11.3 60 128 82 [29,] 41 5 63.4 1647 5.3 4.3 13.7 76 134 92 [30,] 42 12 68.0 1605 11.0 7.0 10.7 88 128 90 [31,] 43 25 69.0 1625 5.0 3.0 6.0 72 140 72 [32,] 43 26 73.0 1615 12.0 4.0 5.7 68 138 74 [33,] 43 10 64.0 1640 5.7 3.0 7.0 60 118 66 [34,] 44 19 65.0 1610 8.0 6.7 7.7 74 110 70 [35,] 44 18 71.0 1572 3.0 4.7 4.3 72 142 84 [36,] 45 10 60.2 1534 3.0 3.0 3.3 56 134 70 [37,] 47 1 55.0 1536 3.0 3.0 4.0 64 116 54 [38,] 50 43 70.0 1630 4.0 6.0 11.7 72 132 90 [39,] 54 40 87.0 1542 11.3 11.7 11.3 92 152 88 > age <- m[, 1] > mig <- m[, 2] > wt <- m[, 3] > ht <- m[, 4] > chin <- m[, 5] > arm <- m[, 6] > calf <- m[, 7] > pulse <- m[, 8] > sys <- m[, 9] > dias <- m[, 10] > plot(mig, sys, main = "Figure 1.3 Peru, effect of mig") > summary(glm(sys ~ mig)) Call: glm(formula = sys ~ mig) Deviance Residuals: Min 1Q Median 3Q Max -20.92662 -9.494748 -1.835627 7.152835 41.02811 Coefficients: Value Std. Error t value (Intercept) 129.0855179 3.7851478 34.1031642 mig -0.1136264 0.2127156 -0.5341708 (Dispersion Parameter for Gaussian family taken to be 175.1744 ) Null Deviance: 6531.436 on 38 degrees of freedom Residual Deviance: 6481.452 on 37 degrees of freedom Number of Fisher Scoring Iterations: 1 Correlation of Coefficients: (Intercept) mig -0.8285518 > pt(-0.5341708, 37) [1] 0.2982093 > f <- (6531.436 - 6481.452)/175.1744 > c(f, 1 - pf(f, 1, 37)) [1] 0.2853385 0.5964185 > - 0.5341708^2 [1] -0.2853384 > 0.2982093 * 2 [1] 0.5964186 > plot(wt, sys, main = "Figure 1.4 Peru, effect of wt") > summary(glm(sys ~ wt)) Call: glm(formula = sys ~ wt) Deviance Residuals: Min 1Q Median 3Q Max -20.29432 -8.491018 0.4457115 6.661655 35.03992 Coefficients: Value Std. Error t value (Intercept) 66.5968679 16.4639030 4.045023 wt 0.9628622 0.2590843 3.716405 (Dispersion Parameter for Gaussian family taken to be 128.5421 ) Null Deviance: 6531.436 on 38 degrees of freedom Residual Deviance: 4756.056 on 37 degrees of freedom Number of Fisher Scoring Iterations: 1 Correlation of Coefficients: (Intercept) wt -0.9939017 > summary(glm(sys ~ wt + mig)) Call: glm(formula = sys ~ wt + mig) Deviance Residuals: Min 1Q Median 3Q Max -17.46914 -7.877931 1.076102 6.292216 24.11318 Coefficients: Value Std. Error t value (Intercept) 50.319127 15.8183899 3.181052 wt 1.354078 0.2672237 5.067208 mig -0.571845 0.1879405 -3.042691 (Dispersion Parameter for Gaussian family taken to be 105.0877 ) Null Deviance: 6531.436 on 38 degrees of freedom Residual Deviance: 3783.157 on 36 degrees of freedom Number of Fisher Scoring Iterations: 1 Correlation of Coefficients: (Intercept) wt wt -0.9826751 mig 0.3382003 -0.4811534 > (4756.056 - 3783.157)/105.0877 [1] 9.257972 > - 3.042691^2 [1] -9.257969 > pt(-3.042691, 36) [1] 0.002179931 > f <- (6531.436 - 3783.157)/2/105.0877 > c(f, 1 - pf(f, 2, 36)) [1] 1.307612e+001 5.385457e-005 > plot(wt, mig, main = "Figure 1.5 mig vs wt") > rmig <- glm(mig ~ wt)$residuals > rsys <- glm(sys ~ wt)$residuals > plot(rmig, rsys, main = "Figure 1.6 Peru") > summary(glm(rsys ~ rmig - 1)) Call: glm(formula = rsys ~ rmig - 1) Deviance Residuals: Min 1Q Median 3Q Max -17.46914 -7.877931 1.076102 6.292216 24.11318 Coefficients: Value Std. Error t value rmig -0.571845 0.1829279 -3.126068 (Dispersion Parameter for Gaussian family taken to be 99.55677 ) Null Deviance: 4756.056 on 39 degrees of freedom Residual Deviance: 3783.157 on 38 degrees of freedom Number of Fisher Scoring Iterations: 1 > 3783.157/36 [1] 105.0877 > plot(mig, sys, type = "n", main = "Figure 1.7 Point label = wt/10") > wf <- floor(wt/10) > wf [1] 7 5 5 6 6 6 5 5 6 5 6 5 6 6 6 5 5 5 5 5 5 6 5 5 7 7 6 6 6 6 6 7 6 6 7 6 [37] 5 7 8 > for(i in 5:8) text(mig[wf == i], sys[wf == i], i) > summary(glm(sys ~ ht + mig)) Call: glm(formula = sys ~ ht + mig) Deviance Residuals: Min 1Q Median 3Q Max -24.22351 -8.420779 -1.742898 7.136828 37.91044 Coefficients: Value Std. Error t value (Intercept) 40.36194370 63.5716898 0.6349044 ht 0.05639203 0.0403358 1.3980640 mig -0.13499837 0.2105792 -0.6410814 (Dispersion Parameter for Gaussian family taken to be 170.7686 ) Null Deviance: 6531.436 on 38 degrees of freedom Residual Deviance: 6147.671 on 36 degrees of freedom Number of Fisher Scoring Iterations: 1 Correlation of Coefficients: (Intercept) ht ht -0.9982705 mig 0.0238883 -0.0725942 > plot(ht, mig, main = "Figure 1.8 mig vs ht") > summary(glm(sys ~ ht + mig + wt + chin + arm + calf + age)) Call: glm(formula = sys ~ ht + mig + wt + chin + arm + calf + age) Deviance Residuals: Min 1Q Median 3Q Max -20.14399 -6.882728 0.9545735 5.927987 21.01179 Coefficients: Value Std. Error t value (Intercept) 129.38413078 56.56907142 2.2871885 ht -0.06730789 0.04254969 -1.5818658 mig -0.56787497 0.22040028 -2.5765619 wt 2.11855825 0.46215933 4.5840430 chin -1.35274359 0.86636913 -1.5613940 arm -0.98327913 1.33833364 -0.7347040 calf 0.23114353 0.61968775 0.3730000 age -0.27770205 0.28560475 -0.9723299 (Dispersion Parameter for Gaussian family taken to be 105.4732 ) Null Deviance: 6531.436 on 38 degrees of freedom Residual Deviance: 3269.668 on 31 degrees of freedom Number of Fisher Scoring Iterations: 1 Correlation of Coefficients: (Intercept) ht mig wt chin arm ht -0.9492313 mig -0.0195593 0.1514594 wt 0.4267813 -0.6610032 -0.3033072 chin -0.1357329 0.2090154 0.0033894 -0.3403611 arm -0.2916775 0.3552127 0.0082804 -0.4188035 -0.2131882 calf 0.0661143 -0.0787768 0.1433470 0.0266347 -0.1175914 -0.5784998 age -0.2698258 0.2157697 -0.4093112 -0.3363726 0.0517231 0.2003633 calf ht mig wt chin arm calf age -0.0273552 > f <- (3783.157 - 3269.668)/5/105.4732 > c(f, 1 - pf(f, 5, 31)) [1] 0.9736862 0.4491722 #------------------------------------------------------------------- # 2.1 FACTORS AND INTERACTION #------------------------------------------------------------------- > m <- matrix(scan("/glim/datasets/educexp"), ncol = 5, byrow = T) > m [,1] [,2] [,3] [,4] [,5] [1,] 508 235 3944 325 1 [2,] 564 231 4578 323 1 [3,] 322 270 4011 328 1 [4,] 846 261 5233 305 1 [5,] 871 300 4780 303 1 [6,] 774 317 5889 307 1 [7,] 856 387 5663 301 1 [8,] 889 285 5759 310 1 [9,] 715 300 4894 300 1 [10,] 753 221 5012 324 2 [11,] 649 264 4908 329 2 [12,] 830 308 5753 320 2 [13,] 738 379 5439 337 2 [14,] 659 342 4634 328 2 [15,] 664 378 4921 330 2 [16,] 572 232 4869 318 2 [17,] 701 231 4672 309 2 [18,] 443 246 4782 333 2 [19,] 446 230 4296 330 2 [20,] 615 268 4827 318 2 [21,] 661 337 5057 304 2 [22,] 722 344 5540 328 3 [23,] 766 330 5331 323 3 [24,] 631 261 4715 317 3 [25,] 390 214 3828 310 3 [26,] 450 245 4120 321 3 [27,] 476 233 3817 342 3 [28,] 603 250 4243 339 3 [29,] 805 243 4647 287 3 [30,] 523 216 3967 325 3 [31,] 588 212 3946 315 3 [32,] 584 208 3724 332 3 [33,] 445 215 3448 358 3 [34,] 500 221 3680 320 3 [35,] 661 244 3825 355 3 [36,] 680 234 4189 306 3 [37,] 797 269 4336 335 3 [38,] 534 302 4418 335 4 [39,] 541 268 4323 344 4 [40,] 605 323 4813 331 4 [41,] 785 304 5046 324 4 [42,] 698 317 3764 366 4 [43,] 796 332 4504 340 4 [44,] 804 315 4005 378 4 [45,] 809 291 5560 330 4 [46,] 726 312 4989 313 4 [47,] 671 316 4697 305 4 [48,] 909 332 5438 307 4 > urb <- m[, 1] > exp <- m[, 2] > inc <- m[, 3] > n18 <- m[, 4] > reg <- m[, 5] > plot(reg, exp, main = "Figure 2.1 Education") > r1 <- (reg == 1) * 1 > r2 <- (reg == 2) * 1 > r3 <- (reg == 3) * 1 > r4 <- (reg == 4) * 1 > cbind(r, r1, r2, r3, r4) reg r1 r2 r3 r4 [1,] 1 1 0 0 0 [2,] 1 1 0 0 0 [3,] 1 1 0 0 0 [4,] 1 1 0 0 0 [5,] 1 1 0 0 0 [6,] 1 1 0 0 0 [7,] 1 1 0 0 0 [8,] 1 1 0 0 0 [9,] 1 1 0 0 0 [10,] 2 0 1 0 0 [11,] 2 0 1 0 0 [12,] 2 0 1 0 0 [13,] 2 0 1 0 0 [14,] 2 0 1 0 0 [15,] 2 0 1 0 0 [16,] 2 0 1 0 0 [17,] 2 0 1 0 0 [18,] 2 0 1 0 0 [19,] 2 0 1 0 0 [20,] 2 0 1 0 0 [21,] 2 0 1 0 0 [22,] 3 0 0 1 0 [23,] 3 0 0 1 0 [24,] 3 0 0 1 0 [25,] 3 0 0 1 0 [26,] 3 0 0 1 0 [27,] 3 0 0 1 0 [28,] 3 0 0 1 0 [29,] 3 0 0 1 0 [30,] 3 0 0 1 0 [31,] 3 0 0 1 0 [32,] 3 0 0 1 0 [33,] 3 0 0 1 0 [34,] 3 0 0 1 0 [35,] 3 0 0 1 0 [36,] 3 0 0 1 0 [37,] 3 0 0 1 0 [38,] 4 0 0 0 1 [39,] 4 0 0 0 1 [40,] 4 0 0 0 1 [41,] 4 0 0 0 1 [42,] 4 0 0 0 1 [43,] 4 0 0 0 1 [44,] 4 0 0 0 1 [45,] 4 0 0 0 1 [46,] 4 0 0 0 1 [47,] 4 0 0 0 1 [48,] 4 0 0 0 1 > summary(glm(exp ~ 1)) Call: glm(formula = exp ~ 1) Deviance Residuals: Min 1Q Median 3Q Max -70.60417 -44.85417 -10.10417 37.64583 108.3958 Coefficients: Value Std. Error t value (Intercept) 278.6042 7.096582 39.25892 (Dispersion Parameter for Gaussian family taken to be 2417.351 ) Null Deviance: 113615.5 on 47 degrees of freedom Residual Deviance: 113615.5 on 47 degrees of freedom Number of Fisher Scoring Iterations: 0 > summary(glm(exp ~ r1 + r2 + r3 + r4)) Call: glm(formula = exp ~ r1 + r2 + r3 + r4) Deviance Residuals: Min 1Q Median 3Q Max -65.33333 -30.4375 -2.760417 16.52604 99.66667 Coefficients: (1 not defined because of singularities) Value Std. Error t value (Intercept) 310.18182 13.20771 23.484900 r1 -22.84848 19.68890 -1.160476 r2 -23.84848 18.28525 -1.304247 r3 -63.99432 17.15732 -3.729855 r4 NA NA NA (Dispersion Parameter for Gaussian family taken to be 1918.88 ) Null Deviance: 113615.5 on 47 degrees of freedom Residual Deviance: 84430.74 on 44 degrees of freedom Number of Fisher Scoring Iterations: 1 Correlation of Coefficients: (Intercept) r1 r2 r3 r1 -0.6708204 r2 -0.7223151 0.4845437 r3 -0.7698004 0.5163978 0.5560384 r4 NA NA NA NA > f <- (113615.5 - 84430.74)/3/1918.88 > c(f, 1 - pf(f, 3, 44)) [1] 5.069755969 0.004210708 > summary(glm(exp ~ r1 + r2 + r3 + r4 - 1)) Call: glm(formula = exp ~ r1 + r2 + r3 + r4 - 1) Deviance Residuals: Min 1Q Median 3Q Max -65.33333 -30.4375 -2.760417 16.52604 99.66667 Coefficients: Value Std. Error t value r1 287.3333 14.60168 19.67811 r2 286.3333 12.64542 22.64324 r3 246.1875 10.95126 22.48030 r4 310.1818 13.20771 23.48490 (Dispersion Parameter for Gaussian family taken to be 1918.88 ) Null Deviance: 3839389 on 48 degrees of freedom Residual Deviance: 84430.74 on 44 degrees of freedom Number of Fisher Scoring Iterations: 1 Correlation of Coefficients: r1 r2 r3 r2 0 r3 0 0 r4 0 0 0 > for(i in 1:4) print(c(i, mean(exp[reg == i]))) [1] 1.0000 287.3333 [1] 2.0000 286.3333 [1] 3.0000 246.1875 [1] 4.0000 310.1818 > Reg <- factor(reg) > options(contrasts = c("contr.treatment", "contr.poly")) > summary(glm(exp ~ Reg)) Call: glm(formula = exp ~ Reg) Deviance Residuals: Min 1Q Median 3Q Max -65.33333 -30.4375 -2.760417 16.52604 99.66667 Coefficients: Value Std. Error t value (Intercept) 287.33333 14.60168 19.67810642 Reg2 -1.00000 19.31620 -0.05177001 Reg3 -41.14583 18.25209 -2.25430743 Reg4 22.84848 19.68890 1.16047568 (Dispersion Parameter for Gaussian family taken to be 1918.88 ) Null Deviance: 113615.5 on 47 degrees of freedom Residual Deviance: 84430.74 on 44 degrees of freedom Number of Fisher Scoring Iterations: 1 Correlation of Coefficients: (Intercept) Reg2 Reg3 Reg2 -0.7559289 Reg3 -0.8000000 0.6047432 Reg4 -0.7416198 0.5606119 0.5932959 > summary(glm(exp ~ r2 + r3 + r4 - 1)) Call: glm(formula = exp ~ r2 + r3 + r4 - 1) Deviance Residuals: Min 1Q Median 3Q Max -65.33333 -23.04688 4.315341 85.77604 387 Coefficients: Value Std. Error t value r2 286.3333 39.14538 7.314614 r3 246.1875 33.90089 7.261977 r4 310.1818 40.88601 7.586502 (Dispersion Parameter for Gaussian family taken to be 18388.33 ) Null Deviance: 3839389 on 48 degrees of freedom Residual Deviance: 827474.7 on 45 degrees of freedom Number of Fisher Scoring Iterations: 1 Correlation of Coefficients: r2 r3 r3 0 r4 0 0 > plot(inc, exp, type = "n", main = "Figure 2.2 No interaction") > for(i in 1:4) text(inc[reg == i], exp[reg == i], i) > summary(glm(exp ~ inc)) Call: glm(formula = exp ~ inc) Deviance Residuals: Min 1Q Median 3Q Max -75.23057 -28.05267 -7.670606 21.77592 86.10897 Coefficients: Value Std. Error t value (Intercept) 57.22197239 42.010935109 1.362073 inc 0.04768727 0.008967382 5.317859 (Dispersion Parameter for Gaussian family taken to be 1529.565 ) Null Deviance: 113615.5 on 47 degrees of freedom Residual Deviance: 70359.97 on 46 degrees of freedom Number of Fisher Scoring Iterations: 1 Correlation of Coefficients: (Intercept) inc -0.9909313 > glm1 <- glm(exp ~ inc + Reg) > summary(glm1) Call: glm(formula = exp ~ inc + Reg) Deviance Residuals: Min 1Q Median 3Q Max -68.88931 -22.01834 -7.302083 18.23428 92.09747 Coefficients: Value Std. Error t value (Intercept) 69.49173206 50.060696970 1.38814951 inc 0.04381074 0.009764791 4.48660274 Reg2 0.81814570 16.131272080 0.05071799 Reg3 -7.73649343 16.959973309 -0.45616189 Reg4 35.34914924 16.671793785 2.12029669 (Dispersion Parameter for Gaussian family taken to be 1337.419 ) Null Deviance: 113615.5 on 47 degrees of freedom Residual Deviance: 57509.02 on 43 degrees of freedom Number of Fisher Scoring Iterations: 1 Correlation of Coefficients: (Intercept) inc Reg2 Reg3 inc -0.9698986 Reg2 -0.2083828 0.0251213 Reg3 -0.6008710 0.4390613 0.5541941 Reg4 -0.3401428 0.1671218 0.5567515 0.5989309 > f <- (70359.97 - 57509.02)/3/1337.419 > c(f, 1 - pf(f, 3, 43)) [1] 3.20292294 0.03250252 > for(i in 1:4) lines(inc[reg == i], glm1$fitted.values[reg == i]) > ir1 <- inc * r1 > ir2 <- inc * r2 > ir3 <- inc * r3 > ir4 <- inc * r4 > cbind(inc, ir1, ir2, ir3, ir4) inc ir1 ir2 ir3 ir4 [1,] 3944 3944 0 0 0 [2,] 4578 4578 0 0 0 [3,] 4011 4011 0 0 0 [4,] 5233 5233 0 0 0 [5,] 4780 4780 0 0 0 [6,] 5889 5889 0 0 0 [7,] 5663 5663 0 0 0 [8,] 5759 5759 0 0 0 [9,] 4894 4894 0 0 0 [10,] 5012 0 5012 0 0 [11,] 4908 0 4908 0 0 [12,] 5753 0 5753 0 0 [13,] 5439 0 5439 0 0 [14,] 4634 0 4634 0 0 [15,] 4921 0 4921 0 0 [16,] 4869 0 4869 0 0 [17,] 4672 0 4672 0 0 [18,] 4782 0 4782 0 0 [19,] 4296 0 4296 0 0 [20,] 4827 0 4827 0 0 [21,] 5057 0 5057 0 0 [22,] 5540 0 0 5540 0 [23,] 5331 0 0 5331 0 [24,] 4715 0 0 4715 0 [25,] 3828 0 0 3828 0 [26,] 4120 0 0 4120 0 [27,] 3817 0 0 3817 0 [28,] 4243 0 0 4243 0 [29,] 4647 0 0 4647 0 [30,] 3967 0 0 3967 0 [31,] 3946 0 0 3946 0 [32,] 3724 0 0 3724 0 [33,] 3448 0 0 3448 0 [34,] 3680 0 0 3680 0 [35,] 3825 0 0 3825 0 [36,] 4189 0 0 4189 0 [37,] 4336 0 0 4336 0 [38,] 4418 0 0 0 4418 [39,] 4323 0 0 0 4323 [40,] 4813 0 0 0 4813 [41,] 5046 0 0 0 5046 [42,] 3764 0 0 0 3764 [43,] 4504 0 0 0 4504 [44,] 4005 0 0 0 4005 [45,] 5560 0 0 0 5560 [46,] 4989 0 0 0 4989 [47,] 4697 0 0 0 4697 [48,] 5438 0 0 0 5438 > glm2 <- glm(exp ~ inc + Reg + ir1 + ir2 + ir3 + ir4) > summary(glm2) Call: glm(formula = exp ~ inc + Reg + ir1 + ir2 + ir3 + ir4) Deviance Residuals: Min 1Q Median 3Q Max -71.10784 -18.304 -2.566215 15.17373 92.36625 Coefficients: (1 not defined because of singularities) Value Std. Error t value (Intercept) 77.481242319 85.73285946 0.90375199 inc 0.001163078 0.01979001 0.05877096 Reg2 -141.946115061 163.02488170 -0.87070215 Reg3 -94.885015199 107.75007952 -0.88060274 Reg4 227.249229791 126.74681964 1.79293832 ir1 0.041040869 0.02614275 1.56987602 ir2 0.069980719 0.03432589 2.03871531 ir3 0.061451394 0.02505425 2.45273369 ir4 NA NA NA (Dispersion Parameter for Gaussian family taken to be 1220.987 ) Null Deviance: 113615.5 on 47 degrees of freedom Residual Deviance: 48839.48 on 40 degrees of freedom Number of Fisher Scoring Iterations: 1 Correlation of Coefficients: (Intercept) inc Reg2 Reg3 Reg4 ir1 inc 0.0000000 Reg2 -0.5258882 0.0000000 Reg3 -0.7956640 0.0000000 0.4184303 Reg4 -0.6764103 -0.7318194 0.3557162 0.5381954 ir1 -0.6473587 -0.7569982 0.3404383 0.5150800 0.9918660 ir2 0.0000000 -0.5765330 -0.6931239 0.0000000 0.4219180 0.4364345 ir3 0.0000000 -0.7898865 0.0000000 -0.3681286 0.5780542 0.5979426 ir4 NA NA NA NA NA NA ir2 ir3 inc Reg2 Reg3 Reg4 ir1 ir2 ir3 0.4553956 ir4 NA NA > f <- (57509.02 - 48839.48)/3/1220.987 > c(f, 1 - pf(f, 3, 40)) [1] 2.36681199 0.08520352 > plot(inc, exp, type = "n", main = "Figure 2.3 With interaction") > for(i in 1:4) { text(inc[reg == i], exp[reg == i], i) lines(inc[reg == i], glm2$fitted.values[reg == i]) } > summary(glm(exp ~ inc + Reg + inc:Reg)) Call: glm(formula = exp ~ inc + Reg + inc:Reg) Deviance Residuals: Min 1Q Median 3Q Max -71.10784 -18.304 -2.566215 15.17373 92.36625 Coefficients: Value Std. Error t value (Intercept) 77.48124232 85.73285946 0.9037520 inc 0.04220395 0.01708211 2.4706513 Reg2 -141.94611506 163.02488170 -0.8707022 Reg3 -94.88501520 107.75007952 -0.8806027 Reg4 227.24922979 126.74681964 1.7929383 incReg2 0.02893985 0.03283932 0.8812561 incReg3 0.02041053 0.02297541 0.8883640 incReg4 -0.04104087 0.02614275 -1.5698760 (Dispersion Parameter for Gaussian family taken to be 1220.987 ) Null Deviance: 113615.5 on 47 degrees of freedom Residual Deviance: 48839.48 on 40 degrees of freedom Number of Fisher Scoring Iterations: 1 Correlation of Coefficients: (Intercept) inc Reg2 Reg3 Reg4 incReg2 inc -0.9907282 Reg2 -0.5258882 0.5210123 Reg3 -0.7956640 0.7882868 0.4184303 Reg4 -0.6764103 0.6701388 0.3557162 0.5381954 incReg2 0.5153497 -0.5201726 -0.9955165 -0.4100452 -0.3485879 incReg3 0.7366021 -0.7434956 -0.3873703 -0.9875250 -0.4982453 0.3867460 incReg4 0.6473587 -0.6534170 -0.3404383 -0.5150800 -0.9918660 0.3398896 incReg3 inc Reg2 Reg3 Reg4 incReg2 incReg3 incReg4 0.4858126 > summary(glm(exp ~ Reg + inc:Reg - 1)) Call: glm(formula = exp ~ Reg + inc:Reg - 1) Deviance Residuals: Min 1Q Median 3Q Max -71.10784 -18.304 -2.566215 15.17373 92.36625 Coefficients: Value Std. Error t value Reg1 77.481242319 85.73285946 0.90375199 Reg2 -64.464872742 138.66141808 -0.46490851 Reg3 -17.403772880 65.26834184 -0.26664953 Reg4 304.730472110 93.35219921 3.26430951 Reg1inc 0.042203947 0.01708211 2.47065126 Reg2inc 0.071143797 0.02804679 2.53661127 Reg3inc 0.062614472 0.01536459 4.07524410 Reg4inc 0.001163078 0.01979001 0.05877096 (Dispersion Parameter for Gaussian family taken to be 1220.987 ) Null Deviance: 3839389 on 48 degrees of freedom Residual Deviance: 48839.48 on 40 degrees of freedom Number of Fisher Scoring Iterations: 1 Correlation of Coefficients: Reg1 Reg2 Reg3 Reg4 Reg1inc Reg2inc Reg2 0.0000000 Reg3 0.0000000 0.0000000 Reg4 0.0000000 0.0000000 0.0000000 Reg1inc -0.9907282 0.0000000 0.0000000 0.0000000 Reg2inc 0.0000000 -0.9973505 0.0000000 0.0000000 0.0000000 Reg3inc 0.0000000 0.0000000 -0.9910027 0.0000000 0.0000000 0.0000000 Reg4inc 0.0000000 0.0000000 0.0000000 -0.9936111 0.0000000 0.0000000 Reg3inc Reg2 Reg3 Reg4 Reg1inc Reg2inc Reg3inc Reg4inc 0.0000000 > summary(glm(exp ~ inc, weight = r1)) Warning messages: Warning in summary.glm(glm(exp ~ inc, weight = r1)): 39 rows with zero weights not counted Call: glm(formula = exp ~ inc, weights = r1) Deviance Residuals: Min 1Q Median 3Q Max -39.69091 0 0 0 70.51781 Coefficients: Value Std. Error t value (Intercept) 77.48124232 95.44457988 0.811793 inc 0.04220395 0.01901716 2.219256 (Dispersion Parameter for Gaussian family taken to be 1513.279 ) Null Deviance: 18046 on 47 degrees of freedom Residual Deviance: 10592.95 on 7 degrees of freedom Number of Fisher Scoring Iterations: 1 Correlation of Coefficients: (Intercept) inc -0.9907282 > summary(glm(exp ~ inc + urb + n18)) Call: glm(formula = exp ~ inc + urb + n18) Deviance Residuals: Min 1Q Median 3Q Max -81.96069 -22.36668 -6.033605 23.39755 80.15106 Coefficients: Value Std. Error t value (Intercept) -290.33668153 135.58423780 -2.141375 inc 0.04895980 0.01230704 3.978193 urb 0.06896144 0.04989909 1.382018 n18 0.91352747 0.33745160 2.707136 (Dispersion Parameter for Gaussian family taken to be 1302.831 ) Null Deviance: 113615.5 on 47 degrees of freedom Residual Deviance: 57324.58 on 44 degrees of freedom Number of Fisher Scoring Iterations: 1 Correlation of Coefficients: (Intercept) inc urb inc -0.5951377 urb 0.1058776 -0.6508137 n18 -0.9580623 0.4104263 -0.0913325 > summary(glm(exp ~ inc + urb + n18 + Reg)) Call: glm(formula = exp ~ inc + urb + n18 + Reg) Deviance Residuals: Min 1Q Median 3Q Max -74.71885 -20.26357 -3.283488 19.76425 86.61468 Coefficients: Value Std. Error t value (Intercept) -183.45163885 149.47226712 -1.2273289 inc 0.04517646 0.01429142 3.1610905 urb 0.04841085 0.05295610 0.9141695 n18 0.68101569 0.36872950 1.8469249 Reg2 -4.35640569 16.53297870 -0.2634979 Reg3 -11.53595442 16.60137148 -0.6948796 Reg4 19.82306934 17.80601741 1.1132792 (Dispersion Parameter for Gaussian family taken to be 1264.852 ) Null Deviance: 113615.5 on 47 degrees of freedom Residual Deviance: 51858.94 on 41 degrees of freedom Number of Fisher Scoring Iterations: 1 Correlation of Coefficients: (Intercept) inc urb n18 Reg2 Reg3 inc -0.6211937 urb 0.1070452 -0.6530034 n18 -0.9441418 0.4021751 -0.0603741 Reg2 0.1943574 -0.1988268 0.1873184 -0.2649526 Reg3 -0.1253995 0.3111585 -0.0785160 -0.0775442 0.5286755 Reg4 0.2427622 0.0627117 -0.1545769 -0.3733907 0.5494991 0.5855525 > f <- (57324.58 - 51858.94)/3/1264.852 > c(f, 1 - pf(f, 3, 41)) [1] 1.4403899 0.2449547 > summary(glm(exp ~ inc + urb + n18 + Reg + (inc + urb + n18):Reg)) Call: glm(formula = exp ~ inc + urb + n18 + Reg + (inc + urb + n18):Reg) Deviance Residuals: Min 1Q Median 3Q Max -84.67835 -13.21772 -1.81002 7.623693 78.3683 Coefficients: Value Std. Error t value (Intercept) 1.503278e+003 792.69107055 1.896424099 inc 4.046002e-002 0.02955810 1.368830201 urb -1.930183e-001 0.15273764 -1.263724361 n18 -4.114715e+000 2.27281142 -1.810407542 Reg2 -2.038133e+003 875.86105576 -2.327005358 Reg3 -1.799109e+003 818.28033513 -2.198645888 Reg4 -9.441369e+002 886.50883277 -1.065005674 incReg2 1.012932e-004 0.05209864 0.001944258 incReg3 3.101351e-002 0.03656007 0.848289274 incReg4 -7.031679e-002 0.04762851 -1.476359242 urbReg2 3.305324e-001 0.21007751 1.573383044 urbReg3 1.849460e-001 0.17938882 1.030978341 urbReg4 3.202425e-001 0.18872859 1.696841690 n18Reg2 5.761912e+000 2.53109015 2.276454686 n18Reg3 4.869708e+000 2.33223334 2.088001851 n18Reg4 3.515340e+000 2.41742097 1.454169635 (Dispersion Parameter for Gaussian family taken to be 1185.193 ) Null Deviance: 113615.5 on 47 degrees of freedom Residual Deviance: 37926.18 on 32 degrees of freedom Number of Fisher Scoring Iterations: 1 Correlation of Coefficients: (Intercept) inc urb n18 Reg2 Reg3 inc -0.1270216 urb -0.6632512 -0.5655106 n18 -0.9927012 0.0206478 0.7082907 Reg2 -0.9050420 0.1149599 0.6002702 0.8984363 Reg3 -0.9687280 0.1230494 0.6425100 0.9616575 0.8767396 Reg4 -0.8941717 0.1135791 0.5930604 0.8876453 0.8092629 0.8662091 incReg2 0.0720656 -0.5673487 0.3208417 -0.0117145 -0.1166385 -0.0698119 incReg3 0.1026945 -0.8084804 0.4572042 -0.0166933 -0.0929428 -0.1653962 incReg4 0.0788292 -0.6205967 0.3509540 -0.0128139 -0.0713437 -0.0763641 urbReg2 0.4822193 0.4111566 -0.7270537 -0.5149654 -0.4749818 -0.4671393 urbReg3 0.5647142 0.4814946 -0.8514334 -0.6030624 -0.5110901 -0.5474405 urbReg4 0.5367678 0.4576665 -0.8092978 -0.5732181 -0.4857974 -0.5199820 n18Reg2 0.8914035 -0.0185408 -0.6360150 -0.8979575 -0.9777081 -0.8635275 n18Reg3 0.9674086 -0.0201217 -0.6902445 -0.9745215 -0.8755455 -0.9899300 n18Reg4 0.9333181 -0.0194126 -0.6659209 -0.9401802 -0.8446921 -0.9041313 Reg4 incReg2 incReg3 incReg4 urbReg2 urbReg3 inc urb n18 Reg2 Reg3 Reg4 incReg2 -0.0644390 incReg3 -0.0918265 0.4586903 incReg4 -0.3835638 0.3520947 0.5017403 urbReg2 -0.4311868 -0.6656962 -0.3324120 -0.2551624 urbReg3 -0.5049515 -0.2731753 -0.5951629 -0.2988140 0.6190378 urbReg4 -0.3929194 -0.2596565 -0.3700144 -0.5441492 0.5884030 0.6890632 n18Reg2 -0.7970678 -0.0757992 0.0149899 0.0115064 0.5614581 0.5415244 n18Reg3 -0.8650294 0.0114160 0.0463207 0.0124875 0.5018448 0.5907671 n18Reg4 -0.9825408 0.0110137 0.0156947 0.2293998 0.4841603 0.5669873 urbReg4 n18Reg2 n18Reg3 inc urb n18 Reg2 Reg3 Reg4 incReg2 incReg3 incReg4 urbReg2 urbReg3 urbReg4 n18Reg2 0.5147255 n18Reg3 0.5586134 0.8750789 n18Reg4 0.4574955 0.8442419 0.9162258 > f <- (51858.94 - 37926.18)/9/1185.193 > c(f, 1 - pf(f, 9, 32)) [1] 1.3061876 0.2722586 #------------------------------------------------------------------- # 2.2 ORTHOGONALITY AND BALANCED DESIGNS #------------------------------------------------------------------- > m <- matrix(scan("/glim/datasets/medcare"), ncol = 3, byrow = T) > m [,1] [,2] [,3] [1,] 2 1 1 [2,] 5 1 1 [3,] 8 1 1 [4,] 6 1 1 [5,] 2 1 1 [6,] 4 1 1 [7,] 3 1 1 [8,] 10 1 1 [9,] 7 1 2 [10,] 5 1 2 [11,] 8 1 2 [12,] 6 1 2 [13,] 3 1 2 [14,] 5 1 2 [15,] 6 1 2 [16,] 4 1 2 [17,] 5 1 2 [18,] 6 1 2 [19,] 8 1 2 [20,] 9 1 2 [21,] 4 2 1 [22,] 6 2 1 [23,] 3 2 1 [24,] 3 2 1 [25,] 7 2 2 [26,] 7 2 2 [27,] 8 2 2 [28,] 6 2 2 [29,] 4 2 2 [30,] 9 2 2 [31,] 8 2 2 [32,] 7 2 2 [33,] 8 3 1 [34,] 7 3 1 [35,] 5 3 1 [36,] 9 3 1 [37,] 9 3 1 [38,] 10 3 1 [39,] 8 3 1 [40,] 6 3 1 [41,] 8 3 1 [42,] 10 3 1 [43,] 5 3 2 [44,] 8 3 2 [45,] 6 3 2 [46,] 6 3 2 [47,] 9 3 2 [48,] 7 3 2 [49,] 7 3 2 [50,] 8 3 2 > sat <- m[, 1] > com <- factor(m[, 2]) > worry <- factor(m[, 3]) > glm0 <- glm(sat ~ 1) > summary(glm0) Call: glm(formula = sat ~ 1) Deviance Residuals: Min 1Q Median 3Q Max -4.4 -1.4 0.1 1.6 3.6 Coefficients: Value Std. Error t value (Intercept) 6.4 0.3010187 21.26114 (Dispersion Parameter for Gaussian family taken to be 4.530612 ) Null Deviance: 222 on 49 degrees of freedom Residual Deviance: 222 on 49 degrees of freedom Number of Fisher Scoring Iterations: 0 > glmc <- glm(sat ~ com) > summary(glmc) Call: glm(formula = sat ~ com) Deviance Residuals: Min 1Q Median 3Q Max -3.6 -1.555556 0.4 1.433333 4.4 Coefficients: Value Std. Error t value (Intercept) 5.600000 0.4415211 12.6834250 com2 0.400000 0.7210010 0.5547843 com3 1.955556 0.6415153 3.0483381 (Dispersion Parameter for Gaussian family taken to be 3.898818 ) Null Deviance: 222 on 49 degrees of freedom Residual Deviance: 183.2444 on 47 degrees of freedom Number of Fisher Scoring Iterations: 1 Correlation of Coefficients: (Intercept) com2 com2 -0.6123724 com3 -0.6882472 0.4214636 > glm0$deviance - glmc$deviance [1] 38.75556 > glmw <- glm(sat ~ worry) > summary(glmw) Call: glm(formula = sat ~ worry) Deviance Residuals: Min 1Q Median 3Q Max -4.181818 -1.571429 0.1233766 1.428571 3.818182 Coefficients: Value Std. Error t value (Intercept) 6.1818182 0.4565702 13.5396893 worry 0.3896104 0.6101175 0.6385825 (Dispersion Parameter for Gaussian family taken to be 4.586039 ) Null Deviance: 222 on 49 degrees of freedom Residual Deviance: 220.1299 on 48 degrees of freedom Number of Fisher Scoring Iterations: 1 Correlation of Coefficients: (Intercept) worry -0.7483315 > glmcw <- glm(sat ~ com + worry) > summary(glmcw) Call: glm(formula = sat ~ com + worry) Deviance Residuals: Min 1Q Median 3Q Max -3.179104 -1.460199 0.08706468 1.103234 4.820896 Coefficients: Value Std. Error t value (Intercept) 5.1791045 0.5561975 9.3116288 com2 0.3532338 0.7180479 0.4919363 com3 2.0646766 0.6441058 3.2054929 worry 0.7014925 0.5689853 1.2328834 (Dispersion Parameter for Gaussian family taken to be 3.856154 ) Null Deviance: 222 on 49 degrees of freedom Residual Deviance: 177.3831 on 46 degrees of freedom Number of Fisher Scoring Iterations: 1 Correlation of Coefficients: (Intercept) com2 com3 com2 -0.4503469 com3 -0.6225368 0.4096234 worry -0.6137949 -0.0528271 0.1374135 > glmw$deviance - glmcw$deviance [1] 42.74679 > summary(glm(sat ~ com + worry + com:worry)) Call: glm(formula = sat ~ com + worry + com:worry) Deviance Residuals: Min 1Q Median 3Q Max -3 -1 -5.329071e-015 1 5 Coefficients: Value Std. Error t value (Intercept) 5 0.6527912 7.6594169 com2 -1 1.1306675 -0.8844333 com3 3 0.8758113 3.4253954 worry 1 0.8427498 1.1865918 com2worry 2 1.4101902 1.4182484 com3worry -2 1.2154311 -1.6455067 (Dispersion Parameter for Gaussian family taken to be 3.409091 ) Null Deviance: 222 on 49 degrees of freedom Residual Deviance: 150 on 44 degrees of freedom Number of Fisher Scoring Iterations: 1 Correlation of Coefficients: (Intercept) com2 com3 worry com2worry com2 -0.5773503 com3 -0.7453560 0.4303315 worry -0.7745967 0.4472136 0.5773503 com2worry 0.4629100 -0.8017837 -0.3450328 -0.5976143 com3worry 0.5370862 -0.3100868 -0.7205767 -0.6933752 0.4143710 > summary(glm(sat ~ com:worry - 1)) Call: glm(formula = sat ~ com:worry - 1) Deviance Residuals: Min 1Q Median 3Q Max -3 -1 0 1 5 Coefficients: Value Std. Error t value com1worry1 5 0.6527912 7.659417 com2worry1 4 0.9231862 4.332820 com3worry1 8 0.5838742 13.701581 com1worry2 6 0.5330018 11.256998 com2worry2 7 0.6527912 10.723184 com3worry2 7 0.6527912 10.723184 (Dispersion Parameter for Gaussian family taken to be 3.409091 ) Null Deviance: 2270 on 50 degrees of freedom Residual Deviance: 150 on 44 degrees of freedom Number of Fisher Scoring Iterations: 1 Correlation of Coefficients: com1worry1 com2worry1 com3worry1 com1worry2 com2worry2 com2worry1 0 com3worry1 0 0 com1worry2 0 0 0 com2worry2 0 0 0 0 com3worry2 0 0 0 0 0 > m <- matrix(scan("/glim/datasets/medcareb"), ncol = 3, byrow = T) > m [,1] [,2] [,3] [1,] 2 1 1 [2,] 5 1 1 [3,] 8 1 1 [4,] 6 1 1 [5,] 7 1 2 [6,] 5 1 2 [7,] 8 1 2 [8,] 6 1 2 [9,] 4 2 1 [10,] 6 2 1 [11,] 3 2 1 [12,] 3 2 1 [13,] 7 2 2 [14,] 7 2 2 [15,] 8 2 2 [16,] 6 2 2 [17,] 8 3 1 [18,] 7 3 1 [19,] 5 3 1 [20,] 9 3 1 [21,] 5 3 2 [22,] 8 3 2 [23,] 6 3 2 [24,] 6 3 2 > sat <- m[, 1] > com <- factor(m[, 2]) > worry <- factor(m[, 3]) > glm0 <- glm(sat ~ 1) > summary(glm0) Call: glm(formula = sat ~ 1) Deviance Residuals: Min 1Q Median 3Q Max -4.041667 -1.041667 -0.04166667 1.208333 2.958333 Coefficients: Value Std. Error t value (Intercept) 6.041667 0.3685025 16.39519 (Dispersion Parameter for Gaussian family taken to be 3.259058 ) Null Deviance: 74.95833 on 23 degrees of freedom Residual Deviance: 74.95833 on 23 degrees of freedom Number of Fisher Scoring Iterations: 0 > glmc <- glm(sat ~ com) > summary(glmc) Call: glm(formula = sat ~ com) Deviance Residuals: Min 1Q Median 3Q Max -3.875 -1.03125 0.1875 1.3125 2.5 Coefficients: Value Std. Error t value (Intercept) 5.875 0.6379609 9.2090290 com2 -0.375 0.9022129 -0.4156447 com3 0.875 0.9022129 0.9698376 (Dispersion Parameter for Gaussian family taken to be 3.255952 ) Null Deviance: 74.95833 on 23 degrees of freedom Residual Deviance: 68.375 on 21 degrees of freedom Number of Fisher Scoring Iterations: 1 Correlation of Coefficients: (Intercept) com2 com2 -0.7071068 com3 -0.7071068 0.5000000 > glm0$deviance - glmc$deviance [1] 6.583333 > glmw <- glm(sat ~ worry) > summary(glmw) Call: glm(formula = sat ~ worry) Deviance Residuals: Min 1Q Median 3Q Max -3.5 -0.8125 -0.04166667 1.416667 3.5 Coefficients: Value Std. Error t value (Intercept) 5.500000 0.5072081 10.843674 worry 1.083333 0.7173006 1.510292 (Dispersion Parameter for Gaussian family taken to be 3.087121 ) Null Deviance: 74.95833 on 23 degrees of freedom Residual Deviance: 67.91667 on 22 degrees of freedom Number of Fisher Scoring Iterations: 1 Correlation of Coefficients: (Intercept) worry -0.7071068 > glmcw <- glm(sat ~ com + worry) > summary(glmcw) Call: glm(formula = sat ~ com + worry) Deviance Residuals: Min 1Q Median 3Q Max -3.333333 -1.291667 0.2708333 0.9791667 2.791667 Coefficients: Value Std. Error t value (Intercept) 5.333333 0.7149204 7.4600385 com2 -0.375000 0.8755950 -0.4282802 com3 0.875000 0.8755950 0.9993204 worry 1.083333 0.7149204 1.5153203 (Dispersion Parameter for Gaussian family taken to be 3.066667 ) Null Deviance: 74.95833 on 23 degrees of freedom Residual Deviance: 61.33333 on 20 degrees of freedom Number of Fisher Scoring Iterations: 1 Correlation of Coefficients: (Intercept) com2 com3 com2 -0.6123724 com3 -0.6123724 0.5000000 worry -0.5000000 0.0000000 0.0000000 > glmw$deviance - glmcw$deviance [1] 6.583333 > m <- matrix(scan("/glim/datasets/glue"), ncol = 3, byrow = T) > m [,1] [,2] [,3] [1,] 190 80 40 [2,] 189 80 40 [3,] 192 90 40 [4,] 190 90 40 [5,] 196 80 60 [6,] 193 80 60 [7,] 195 90 60 [8,] 196 90 60 [9,] 201 80 80 [10,] 200 80 80 [11,] 203 90 80 [12,] 205 90 80 > stren <- m[, 1] > temp <- m[, 2] > humid <- m[, 3] > glm0 <- glm(stren ~ 1) > summary(glm0) Call: glm(formula = stren ~ 1) Deviance Residuals: Min 1Q Median 3Q Max -6.833333 -4.333333 -0.3333333 4.416667 9.166667 Coefficients: Value Std. Error t value (Intercept) 195.8333 1.551311 126.2373 (Dispersion Parameter for Gaussian family taken to be 28.87879 ) Null Deviance: 317.6667 on 11 degrees of freedom Residual Deviance: 317.6667 on 11 degrees of freedom Number of Fisher Scoring Iterations: 0 > glmt <- glm(stren ~ temp) > summary(glmt) Call: glm(formula = stren ~ temp) Deviance Residuals: Min 1Q Median 3Q Max -6.833333 -4.833333 -1.333333 5.416667 8.166667 Coefficients: Value Std. Error t value (Intercept) 178.8333 27.1789338 6.5798509 temp 0.2000 0.3192004 0.6265657 (Dispersion Parameter for Gaussian family taken to be 30.56667 ) Null Deviance: 317.6667 on 11 degrees of freedom Residual Deviance: 305.6667 on 10 degrees of freedom Number of Fisher Scoring Iterations: 1 Correlation of Coefficients: (Intercept) temp -0.9982744 > glm0$deviance - glmt$deviance [1] 12 > glmh <- glm(stren ~ humid) > summary(glmh) Call: glm(formula = stren ~ humid) Deviance Residuals: Min 1Q Median 3Q Max -2.833333 -0.8333333 0.1666667 0.4166667 3.166667 Coefficients: Value Std. Error t value (Intercept) 177.8333 1.89333627 93.92591 humid 0.3000 0.03044804 9.85285 (Dispersion Parameter for Gaussian family taken to be 2.966667 ) Null Deviance: 317.6667 on 11 degrees of freedom Residual Deviance: 29.66667 on 10 degrees of freedom Number of Fisher Scoring Iterations: 1 Correlation of Coefficients: (Intercept) humid -0.9649013 > glmht <- glm(stren ~ humid + temp) > glmht Call: glm(formula = stren ~ humid + temp) Coefficients: (Intercept) humid temp 160.8333 0.3 0.2 Degrees of Freedom: 12 Total; 9 Residual Residual Deviance: 17.66667 > glmh$deviance - glmht$deviance [1] 12 > xstren <- c(190, 189, 190, 188, 196, 192, 195, 197, 203, 203, 203, 204) > xtemp <- c(rep(80, 6), rep(90, 6)) > cbind(xstren, xtemp, humid) xstren xtemp humid [1,] 190 80 40 [2,] 189 80 40 [3,] 190 80 40 [4,] 188 80 40 [5,] 196 80 60 [6,] 192 80 60 [7,] 195 90 60 [8,] 197 90 60 [9,] 203 90 80 [10,] 203 90 80 [11,] 203 90 80 [12,] 204 90 80 > glm0 <- glm(xstren ~ 1) > summary(glm0) Call: glm(formula = xstren ~ 1) Deviance Residuals: Min 1Q Median 3Q Max -7.833333 -5.833333 -0.3333333 7.166667 8.166667 Coefficients: Value Std. Error t value (Intercept) 195.8333 1.770265 110.6237 (Dispersion Parameter for Gaussian family taken to be 37.60606 ) Null Deviance: 413.6667 on 11 degrees of freedom Residual Deviance: 413.6667 on 11 degrees of freedom Number of Fisher Scoring Iterations: 0 > glmt <- glm(xstren ~ xtemp) > summary(glmt) Call: glm(formula = xstren ~ xtemp) Deviance Residuals: Min 1Q Median 3Q Max -5.833333 -2.083333 0.1666667 2.166667 5.166667 Coefficients: Value Std. Error t value (Intercept) 110.8333 16.5739085 6.687218 xtemp 1.0000 0.1946507 5.137408 (Dispersion Parameter for Gaussian family taken to be 11.36667 ) Null Deviance: 413.6667 on 11 degrees of freedom Residual Deviance: 113.6667 on 10 degrees of freedom Number of Fisher Scoring Iterations: 1 Correlation of Coefficients: (Intercept) xtemp -0.9982744 > glm0$deviance - glmt$deviance [1] 300 > glmh <- glm(xstren ~ humid) > summary(glmh) Call: glm(formula = xstren ~ humid) Deviance Residuals: Min 1Q Median 3Q Max -3.833333 -0.08333333 0.1666667 1.166667 1.166667 Coefficients: Value Std. Error t value (Intercept) 174.8333 1.61804065 108.05250 humid 0.3500 0.02602082 13.45076 (Dispersion Parameter for Gaussian family taken to be 2.166667 ) Null Deviance: 413.6667 on 11 degrees of freedom Residual Deviance: 21.66667 on 10 degrees of freedom Number of Fisher Scoring Iterations: 1 Correlation of Coefficients: (Intercept) humid -0.9649013 > glmht <- glm(xstren ~ humid + xtemp) > glmht Call: glm(formula = xstren ~ humid + xtemp) Coefficients: (Intercept) humid xtemp 160.8333 0.3 0.2 Degrees of Freedom: 12 Total; 9 Residual Residual Deviance: 17.66667 > glmh$deviance - glmht$deviance [1] 4