. * do-file for lecture 8 of VHM 802, Winter 2025 (expanded version) . version 18 /* works also with versions 14-17 */ . set more off . set scheme stcolor_alt . cd "r:\" r:\ . . * Example 13.1 . import delimited ch13ta1.csv, clear (encoding automatically selected: ISO-8859-1) (4 vars, 15 obs) . anova change tx plant Number of obs = 15 R-squared = 0.8875 Root MSE = 4.21011 Adj R-squared = 0.8031 Source | Partial SS df MS F Prob>F -----------+---------------------------------------------------- Model | 1118.4333 6 186.40556 10.52 0.0020 | tx | 432.03333 2 216.01667 12.19 0.0037 plant | 686.4 4 171.6 9.68 0.0037 | Residual | 141.8 8 17.725 -----------+---------------------------------------------------- Total | 1260.2333 14 90.016667 . margins tx Predictive margins Number of obs = 15 Expression: Linear prediction, predict() ------------------------------------------------------------------------------ | Delta-method | Margin std. err. t P>|t| [95% conf. interval] -------------+---------------------------------------------------------------- tx | 1 | 4.3 1.882817 2.28 0.052 -.0417839 8.641784 2 | 5.9 1.882817 3.13 0.014 1.558216 10.24178 3 | 16.4 1.882817 8.71 0.000 12.05822 20.74178 ------------------------------------------------------------------------------ . pwcompare tx, pv mcomp(bon) Pairwise comparisons of marginal linear predictions Margins: asbalanced --------------------------- | Number of | comparisons -------------+------------- tx | 3 --------------------------- ----------------------------------------------------- | Bonferroni | Contrast Std. err. t P>|t| -------------+--------------------------------------- tx | 2 vs 1 | 1.6 2.662705 0.60 1.000 3 vs 1 | 12.1 2.662705 4.54 0.006 3 vs 2 | 10.5 2.662705 3.94 0.013 ----------------------------------------------------- . . * Mangold example . import delimited mangold.csv, clear (encoding automatically selected: ISO-8859-1) (4 vars, 25 obs) . encode tx, gen(Tx) /* needed because tx has character values */ . anova yield row col Tx Number of obs = 25 R-squared = 0.7503 Root MSE = 12.091 Adj R-squared = 0.5007 Source | Partial SS df MS F Prob>F -----------+---------------------------------------------------- Model | 5272.32 12 439.36 3.01 0.0341 | row | 4240.24 4 1060.06 7.25 0.0033 col | 701.84 4 175.46 1.20 0.3604 Tx | 330.24 4 82.56 0.56 0.6930 | Residual | 1754.32 12 146.19333 -----------+---------------------------------------------------- Total | 7026.64 24 292.77667 . margins Tx, asbalanced /* asbalanced actually not needed here */ Adjusted predictions Number of obs = 25 Expression: Linear prediction, predict() At: row (asbalanced) col (asbalanced) Tx (asbalanced) ------------------------------------------------------------------------------ | Delta-method | Margin std. err. t P>|t| [95% conf. interval] -------------+---------------------------------------------------------------- Tx | A | 333.6 5.407279 61.69 0.000 321.8186 345.3814 B | 331.2 5.407279 61.25 0.000 319.4186 342.9814 C | 334.4 5.407279 61.84 0.000 322.6186 346.1814 D | 342 5.407279 63.25 0.000 330.2186 353.7814 E | 334.4 5.407279 61.84 0.000 322.6186 346.1814 ------------------------------------------------------------------------------ . lincom 4*4.Tx-1.Tx-2.Tx-3.Tx-5.Tx ( 1) - 1b.Tx - 2.Tx - 3.Tx + 4*4.Tx - 5.Tx = 0 ------------------------------------------------------------------------------ yield | Coefficient Std. err. t P>|t| [95% conf. interval] -------------+---------------------------------------------------------------- (1) | 34.4 24.18209 1.42 0.180 -18.28824 87.08824 ------------------------------------------------------------------------------ . di 146.193333*(34.4/24.18209)^2 /* SS for contrast */ 295.83993 . . * Example 14.2 . import delimited ch14ta1.csv, clear (encoding automatically selected: ISO-8859-1) (4 vars, 36 obs) . anova plates session tx Number of obs = 36 R-squared = 0.9913 Root MSE = .907785 Adj R-squared = 0.9809 Source | Partial SS df MS F Prob>F -----------+---------------------------------------------------- Model | 1499.5648 19 78.924464 95.77 0.0000 | session | 10.064815 11 .91498316 1.11 0.4127 tx | 1086.8148 8 135.85185 164.85 0.0000 | Residual | 13.185185 16 .82407407 -----------+---------------------------------------------------- Total | 1512.75 35 43.221429 . anova plates session tx, sequential Number of obs = 36 R-squared = 0.9913 Root MSE = .907785 Adj R-squared = 0.9809 Source | Seq. SS df MS F Prob>F -----------+---------------------------------------------------- Model | 1499.5648 19 78.924464 95.77 0.0000 | session | 412.75 11 37.522727 45.53 0.0000 tx | 1086.8148 8 135.85185 164.85 0.0000 | Residual | 13.185185 16 .82407407 -----------+---------------------------------------------------- Total | 1512.75 35 43.221429 . tabstat plates, statistics (mean) by(tx) Summary for variables: plates Group variable: tx tx | Mean ---------+---------- 1 | 19.75 2 | 16.75 3 | 13.25 4 | 6.5 5 | 25.5 6 | 23.25 7 | 20.75 8 | 19.25 9 | 29.75 ---------+---------- Total | 19.41667 -------------------- . margins tx, asbalanced /* asbalanced not needed here either */ Adjusted predictions Number of obs = 36 Expression: Linear prediction, predict() At: session (asbalanced) tx (asbalanced) ------------------------------------------------------------------------------ | Delta-method | Margin std. err. t P>|t| [95% conf. interval] -------------+---------------------------------------------------------------- tx | 1 | 19.75 .5167795 38.22 0.000 18.65448 20.84552 2 | 17.19444 .5167795 33.27 0.000 16.09892 18.28997 3 | 13.19444 .5167795 25.53 0.000 12.09892 14.28997 4 | 6.527778 .5167795 12.63 0.000 5.432254 7.623301 5 | 25.30556 .5167795 48.97 0.000 24.21003 26.40108 6 | 22.97222 .5167795 44.45 0.000 21.8767 24.06775 7 | 21.08333 .5167795 40.80 0.000 19.98781 22.17886 8 | 19.19444 .5167795 37.14 0.000 18.09892 20.28997 9 | 29.52778 .5167795 57.14 0.000 28.43225 30.6233 ------------------------------------------------------------------------------ . margins, over(tx) /* simple means */ Predictive margins Number of obs = 36 Expression: Linear prediction, predict() Over: tx ------------------------------------------------------------------------------ | Delta-method | Margin std. err. t P>|t| [95% conf. interval] -------------+---------------------------------------------------------------- tx | 1 | 19.75 .4538926 43.51 0.000 18.78779 20.71221 2 | 16.75 .4538926 36.90 0.000 15.78779 17.71221 3 | 13.25 .4538926 29.19 0.000 12.28779 14.21221 4 | 6.5 .4538926 14.32 0.000 5.537791 7.462209 5 | 25.5 .4538926 56.18 0.000 24.53779 26.46221 6 | 23.25 .4538926 51.22 0.000 22.28779 24.21221 7 | 20.75 .4538926 45.72 0.000 19.78779 21.71221 8 | 19.25 .4538926 42.41 0.000 18.28779 20.21221 9 | 29.75 .4538926 65.54 0.000 28.78779 30.71221 ------------------------------------------------------------------------------ . margins, over(tx) asbalanced /* least squares means */ Adjusted predictions Number of obs = 36 Expression: Linear prediction, predict() Over: tx At: 1.tx session (asbalanced) tx (asbalanced) 2.tx session (asbalanced) tx (asbalanced) 3.tx session (asbalanced) tx (asbalanced) 4.tx session (asbalanced) tx (asbalanced) 5.tx session (asbalanced) tx (asbalanced) 6.tx session (asbalanced) tx (asbalanced) 7.tx session (asbalanced) tx (asbalanced) 8.tx session (asbalanced) tx (asbalanced) 9.tx session (asbalanced) tx (asbalanced) ------------------------------------------------------------------------------ | Delta-method | Margin std. err. t P>|t| [95% conf. interval] -------------+---------------------------------------------------------------- tx | 1 | 19.75 .5167795 38.22 0.000 18.65448 20.84552 2 | 17.19444 .5167795 33.27 0.000 16.09892 18.28997 3 | 13.19444 .5167795 25.53 0.000 12.09892 14.28997 4 | 6.527778 .5167795 12.63 0.000 5.432254 7.623301 5 | 25.30556 .5167795 48.97 0.000 24.21003 26.40108 6 | 22.97222 .5167795 44.45 0.000 21.8767 24.06775 7 | 21.08333 .5167795 40.80 0.000 19.98781 22.17886 8 | 19.19444 .5167795 37.14 0.000 18.09892 20.28997 9 | 29.52778 .5167795 57.14 0.000 28.43225 30.6233 ------------------------------------------------------------------------------ . * same contrasts as in GO . lincom 1.tx+2.tx+3.tx+4.tx+5.tx+6.tx+7.tx+8.tx-8*9.tx ( 1) 1b.tx + 2.tx + 3.tx + 4.tx + 5.tx + 6.tx + 7.tx + 8.tx - 8*9.tx = 0 ------------------------------------------------------------------------------ plates | Coefficient Std. err. t P>|t| [95% conf. interval] -------------+---------------------------------------------------------------- (1) | -91 4.447221 -20.46 0.000 -100.4277 -81.57231 ------------------------------------------------------------------------------ . lincom 1.tx+2.tx+3.tx+4.tx-5.tx-6.tx-7.tx-8.tx ( 1) 1b.tx + 2.tx + 3.tx + 4.tx - 5.tx - 6.tx - 7.tx - 8.tx = 0 ------------------------------------------------------------------------------ plates | Coefficient Std. err. t P>|t| [95% conf. interval] -------------+---------------------------------------------------------------- (1) | -31.88889 1.482407 -21.51 0.000 -35.03145 -28.74633 ------------------------------------------------------------------------------ . lincom 3*1.tx+2.tx-3.tx-3*4.tx+3*5.tx+6.tx-7.tx-3*8.tx /* coef -3 -1 1 3 */ ( 1) 3*1b.tx + 2.tx - 3.tx - 3*4.tx + 3*5.tx + 6.tx - 7.tx - 3*8.tx = 0 ------------------------------------------------------------------------------ plates | Coefficient Std. err. t P>|t| [95% conf. interval] -------------+---------------------------------------------------------------- (1) | 63.88889 3.314763 19.27 0.000 56.86191 70.91587 ------------------------------------------------------------------------------ . * last contrast is for a linear effect of dose (no interaction) . lincom -1.tx+2.tx+3.tx-4.tx-5.tx+6.tx+7.tx-8.tx /* coeff 1 -1 -1 1 */ ( 1) - 1b.tx + 2.tx + 3.tx - 4.tx - 5.tx + 6.tx + 7.tx - 8.tx = 0 ------------------------------------------------------------------------------ plates | Coefficient Std. err. t P>|t| [95% conf. interval] -------------+---------------------------------------------------------------- (1) | 3.666667 1.482407 2.47 0.025 .524104 6.809229 ------------------------------------------------------------------------------ . lincom 1.tx-3*2.tx+3*3.tx-4.tx+5.tx-3*6.tx+3*7.tx-8.tx /* coeff -1 3 -3 1 */ ( 1) 1b.tx - 3*2.tx + 3*3.tx - 4.tx + 5.tx - 3*6.tx + 3*7.tx - 8.tx = 0 ------------------------------------------------------------------------------ plates | Coefficient Std. err. t P>|t| [95% conf. interval] -------------+---------------------------------------------------------------- (1) | 1.666667 3.314763 0.50 0.622 -5.360317 8.69365 ------------------------------------------------------------------------------ . * extra code to parametrize as a simplified regression model (5 parameters instead of 8) . gen dose=0 . replace dose=3 if tx_txt=="A"|tx_txt=="E" (8 real changes made) . replace dose=2 if tx_txt=="B"|tx_txt=="F" (8 real changes made) . replace dose=1 if tx_txt=="C"|tx_txt=="G" (8 real changes made) . gen detergent=0 . replace detergent=1 if tx_txt=="A"|tx_txt=="B"|tx_txt=="C"|tx_txt=="D" (16 real changes made) . replace detergent=2 if tx_txt=="E"|tx_txt=="F"|tx_txt=="G"|tx_txt=="H" (16 real changes made) . regress plates i.session ib1.detergent c.dose##c.dose##c.dose Source | SS df MS Number of obs = 36 -------------+---------------------------------- F(16, 19) = 27.68 Model | 1450.51389 16 90.6571181 Prob > F = 0.0000 Residual | 62.2361111 19 3.2755848 R-squared = 0.9589 -------------+---------------------------------- Adj R-squared = 0.9242 Total | 1512.75 35 43.2214286 Root MSE = 1.8099 -------------------------------------------------------------------------------------- plates | Coefficient Std. err. t P>|t| [95% conf. interval] ---------------------+---------------------------------------------------------------- session | 2 | -1.222222 1.596144 -0.77 0.453 -4.562991 2.118546 3 | .4305556 1.692967 0.25 0.802 -3.112865 3.973976 4 | -.25 1.538083 -0.16 0.873 -3.469245 2.969245 5 | 1.111111 1.596144 0.70 0.495 -2.229658 4.45188 6 | 1.013889 1.581829 0.64 0.529 -2.296917 4.324695 7 | 1.069444 1.619722 0.66 0.517 -2.320674 4.459562 8 | -.1666667 1.596144 -0.10 0.918 -3.507435 3.174102 9 | .6388889 1.615034 0.40 0.697 -2.741417 4.019195 10 | 1.444444 1.596144 0.90 0.377 -1.896324 4.785213 11 | -.5694444 1.581829 -0.36 0.723 -3.88025 2.741362 12 | -.3333333 1.596144 -0.21 0.837 -3.674102 3.007435 | detergent | 0 | 20.65278 1.332019 15.50 0.000 17.86483 23.44073 2 | 7.972222 .7388713 10.79 0.000 6.425747 9.518698 | dose | 5.222222 2.835016 1.84 0.081 -.711534 11.15598 | c.dose#c.dose | -1.083333 2.505634 -0.43 0.670 -6.327687 4.16102 | c.dose#c.dose#c.dose | .1388889 .5507222 0.25 0.804 -1.013786 1.291564 | _cons | 8.611111 1.348988 6.38 0.000 5.787646 11.43458 -------------------------------------------------------------------------------------- . regress plates i.session ib1.detergent c.dose##c.dose Source | SS df MS Number of obs = 36 -------------+---------------------------------- F(15, 20) = 30.97 Model | 1450.30556 15 96.687037 Prob > F = 0.0000 Residual | 62.4444444 20 3.12222222 R-squared = 0.9587 -------------+---------------------------------- Adj R-squared = 0.9278 Total | 1512.75 35 43.2214286 Root MSE = 1.767 ------------------------------------------------------------------------------- plates | Coefficient Std. err. t P>|t| [95% conf. interval] --------------+---------------------------------------------------------------- session | 2 | -1.277778 1.543418 -0.83 0.418 -4.497292 1.941736 3 | .4444444 1.651985 0.27 0.791 -3.001535 3.890424 4 | -.2222222 1.49779 -0.15 0.884 -3.346557 2.902113 5 | 1.055556 1.543418 0.68 0.502 -2.163959 4.27507 6 | 1 1.543418 0.65 0.524 -2.219514 4.219514 7 | 1.083333 1.580436 0.69 0.501 -2.213398 4.380065 8 | -.2222222 1.543418 -0.14 0.887 -3.441736 2.997292 9 | .6388889 1.576773 0.41 0.690 -2.650203 3.92798 10 | 1.388889 1.543418 0.90 0.379 -1.830625 4.608403 11 | -.6388889 1.520775 -0.42 0.679 -3.81117 2.533393 12 | -.25 1.524572 -0.16 0.871 -3.430202 2.930202 | detergent | 0 | 20.61111 1.290421 15.97 0.000 17.91934 23.30288 2 | 7.972222 .721367 11.05 0.000 6.467477 9.476967 | dose | 4.569444 1.129118 4.05 0.001 2.214146 6.924743 | c.dose#c.dose | -.4583333 .3606835 -1.27 0.218 -1.210706 .2940393 | _cons | 8.666667 1.299351 6.67 0.000 5.956268 11.37707 ------------------------------------------------------------------------------- . regress plates i.session ib1.detergent dose Source | SS df MS Number of obs = 36 -------------+---------------------------------- F(14, 21) = 32.12 Model | 1445.26389 14 103.233135 Prob > F = 0.0000 Residual | 67.4861111 21 3.21362434 R-squared = 0.9554 -------------+---------------------------------- Adj R-squared = 0.9256 Total | 1512.75 35 43.2214286 Root MSE = 1.7927 ------------------------------------------------------------------------------ plates | Coefficient Std. err. t P>|t| [95% conf. interval] -------------+---------------------------------------------------------------- session | 2 | -1.583333 1.546727 -1.02 0.318 -4.799928 1.633262 3 | .2916667 1.671547 0.17 0.863 -3.184505 3.767838 4 | -.5277778 1.499846 -0.35 0.728 -3.646878 2.591322 5 | .75 1.546727 0.48 0.633 -2.466595 3.966595 6 | 1.152778 1.561089 0.74 0.468 -2.093684 4.39924 7 | .625 1.561089 0.40 0.693 -2.621462 3.871462 8 | .0833333 1.546727 0.05 0.958 -3.133262 3.299928 9 | .3333333 1.580976 0.21 0.835 -2.954487 3.621153 10 | 1.083333 1.546727 0.70 0.491 -2.133262 4.299928 11 | -.7916667 1.538046 -0.51 0.612 -3.990208 2.406874 12 | -.25 1.546727 -0.16 0.873 -3.466595 2.966595 | detergent | 0 | 20.15278 1.256993 16.03 0.000 17.53872 22.76684 2 | 7.972222 .7318497 10.89 0.000 6.450257 9.494187 | dose | 3.194444 .3272932 9.76 0.000 2.513801 3.875088 _cons | 9.277778 1.224619 7.58 0.000 6.731043 11.82451 ------------------------------------------------------------------------------ . . * reload Mangold data . import delimited mangold.csv, clear (encoding automatically selected: ISO-8859-1) (4 vars, 25 obs) . drop if col==5 /* subdataset for Youden square */ (5 observations deleted) . encode tx, gen(Tx) . anova yield row col Tx Number of obs = 20 R-squared = 0.7964 Root MSE = 12.4332 Adj R-squared = 0.5165 Source | Partial SS df MS F Prob>F -----------+---------------------------------------------------- Model | 4838.2833 11 439.84394 2.85 0.0742 | row | 4220.1333 4 1055.0333 6.83 0.0108 col | 366.95 3 122.31667 0.79 0.5320 Tx | 224.13333 4 56.033333 0.36 0.8289 | Residual | 1236.6667 8 154.58333 -----------+---------------------------------------------------- Total | 6074.95 19 319.73421 . margins Tx, asbalanced /* asbalanced not needed here either */ Adjusted predictions Number of obs = 20 Expression: Linear prediction, predict() At: row (asbalanced) col (asbalanced) Tx (asbalanced) ------------------------------------------------------------------------------ | Delta-method | Margin std. err. t P>|t| [95% conf. interval] -------------+---------------------------------------------------------------- Tx | A | 340.35 6.380199 53.34 0.000 325.6372 355.0628 B | 333.55 6.380199 52.28 0.000 318.8372 348.2628 C | 334.75 6.380199 52.47 0.000 320.0372 349.4628 D | 341.8833 6.380199 53.59 0.000 327.1706 356.5961 E | 334.2167 6.380199 52.38 0.000 319.5039 348.9294 ------------------------------------------------------------------------------ .