. * do-file for lecture 13 of VHM 802, Winter 2023 . version 17 /* works also with versions 14-16 */ . set more off . 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 ( 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 -70.91587 -56.86191 ------------------------------------------------------------------------------ . * last contrast is for a linear effect of dose (no interaction) . . * Floral scent example . import delimited florallong.csv, clear (encoding automatically selected: UTF-8) (4 vars, 42 obs) . encode tx, gen(Tx) . anova time id per Tx Number of obs = 42 R-squared = 0.8970 Root MSE = 6.46827 Adj R-squared = 0.7778 Source | Partial SS df MS F Prob>F -----------+---------------------------------------------------- Model | 6923.9317 22 314.72417 7.52 0.0000 | id | 6135.3106 20 306.76553 7.33 0.0000 per | 779.00183 1 779.00183 18.62 0.0004 Tx | 3.1290194 1 3.1290194 0.07 0.7874 | Residual | 794.93171 19 41.838511 -----------+---------------------------------------------------- Total | 7718.8634 41 188.26496 . . * Example 13.12 . import delimited ch13ta4.csv, clear (encoding automatically selected: ISO-8859-2) (9 vars, 54 obs) . xi: boxcox yield i.tx i.period i.cow i.tx _Itx_1-3 (naturally coded; _Itx_1 omitted) i.period _Iperiod_1-3 (naturally coded; _Iperiod_1 omitted) i.cow _Icow_1-18 (naturally coded; _Icow_1 omitted) Fitting comparison model Iteration 0: log likelihood = -385.98591 Iteration 1: log likelihood = -384.22867 Iteration 2: log likelihood = -384.22712 Iteration 3: log likelihood = -384.22712 Fitting full model Iteration 0: log likelihood = -314.69985 Iteration 1: log likelihood = -309.99214 Iteration 2: log likelihood = -309.52468 Iteration 3: log likelihood = -309.52451 Iteration 4: log likelihood = -309.52451 Number of obs = 54 LR chi2(21) = 149.41 Log likelihood = -309.52451 Prob > chi2 = 0.000 ------------------------------------------------------------------------------ yield | Coefficient Std. err. z P>|z| [95% conf. interval] -------------+---------------------------------------------------------------- /theta | -.0033825 .3020123 -0.01 0.991 -.5953156 .5885507 ------------------------------------------------------------------------------ Estimates of scale-variant parameters ---------------------------- | Coefficient -------------+-------------- Notrans | _Itx_2 | .1283454 _Itx_3 | .2062503 _Iperiod_2 | -.1368168 _Iperiod_3 | -.3236706 _Icow_2 | .3283881 _Icow_3 | .2962179 _Icow_4 | .2877204 _Icow_5 | .1538402 _Icow_6 | .1675439 _Icow_7 | .1797743 _Icow_8 | .175641 _Icow_9 | .1328143 _Icow_10 | .106585 _Icow_11 | .1235257 _Icow_12 | .0969088 _Icow_13 | .0610728 _Icow_14 | -.0698757 _Icow_15 | .0134546 _Icow_16 | -.0703827 _Icow_17 | -.1146752 _Icow_18 | -.0336909 _cons | 7.090901 -------------+-------------- /sigma | .0523219 ---------------------------- --------------------------------------------------------- Test Restricted LR statistic H0: log likelihood chi2 Prob > chi2 --------------------------------------------------------- theta = -1 -314.64566 10.24 0.001 theta = 0 -309.52458 0.00 0.991 theta = 1 -314.69985 10.35 0.001 --------------------------------------------------------- . gen lnyield=ln(yield) . anova lnyield tx period cow Number of obs = 54 R-squared = 0.9372 Root MSE = .069653 Adj R-squared = 0.8959 Source | Partial SS df MS F Prob>F -----------+---------------------------------------------------- Model | 2.315333 21 .11025395 22.73 0.0000 | tx | .40999477 2 .20499738 42.25 0.0000 period | .99806797 2 .49903398 102.86 0.0000 cow | .90727032 17 .05336884 11.00 0.0000 | Residual | .15524921 32 .00485154 -----------+---------------------------------------------------- Total | 2.4705823 53 .04661476 . * splitting cow effects into squares and cows within squares . anova lnyield square tx period cow|square Number of obs = 54 R-squared = 0.9372 Root MSE = .069653 Adj R-squared = 0.8959 Source | Partial SS df MS F Prob>F -----------+---------------------------------------------------- Model | 2.315333 21 .11025395 22.73 0.0000 | square | .62302927 5 .12460585 25.68 0.0000 tx | .40999477 2 .20499738 42.25 0.0000 period | .99806797 2 .49903398 102.86 0.0000 cow|square | .28424105 12 .02368675 4.88 0.0002 | Residual | .15524921 32 .00485154 -----------+---------------------------------------------------- Total | 2.4705823 53 .04661476 . anova lnyield square tx period cow /* same model fit but wrong SS for squares */ Number of obs = 54 R-squared = 0.9372 Root MSE = .069653 Adj R-squared = 0.8959 Source | Partial SS df MS F Prob>F -----------+---------------------------------------------------- Model | 2.315333 21 .11025395 22.73 0.0000 | square | .10313425 5 .02062685 4.25 0.0045 tx | .40999477 2 .20499738 42.25 0.0000 period | .99806797 2 .49903398 102.86 0.0000 cow | .28424105 12 .02368675 4.88 0.0002 | Residual | .15524921 32 .00485154 -----------+---------------------------------------------------- Total | 2.4705823 53 .04661476 . anova lnyield tx period cow square /* square effects empty */ Number of obs = 54 R-squared = 0.9372 Root MSE = .069653 Adj R-squared = 0.8959 Source | Partial SS df MS F Prob>F -----------+---------------------------------------------------- Model | 2.315333 21 .11025395 22.73 0.0000 | tx | .40999477 2 .20499738 42.25 0.0000 period | .99806797 2 .49903398 102.86 0.0000 cow | .90727032 17 .05336884 11.00 0.0000 square | 0 0 | Residual | .15524921 32 .00485154 -----------+---------------------------------------------------- Total | 2.4705823 53 .04661476 . * period effects nested in squares . anova lnyield square tx period|square cow|square Number of obs = 54 R-squared = 0.9584 Root MSE = .06831 Adj R-squared = 0.8999 Source | Partial SS df MS F Prob>F --------------+---------------------------------------------------- Model | 2.3679239 31 .07638464 16.37 0.0000 | square | .62302927 5 .12460585 26.70 0.0000 tx | .40999477 2 .20499738 43.93 0.0000 period|square | 1.0506588 12 .0875549 18.76 0.0000 cow|square | .28424105 12 .02368675 5.08 0.0005 | Residual | .1026584 22 .00466629 --------------+---------------------------------------------------- Total | 2.4705823 53 .04661476 . anova lnyield square tx period##square cow|square Number of obs = 54 R-squared = 0.9584 Root MSE = .06831 Adj R-squared = 0.8999 Source | Partial SS df MS F Prob>F --------------+---------------------------------------------------- Model | 2.3679239 31 .07638464 16.37 0.0000 | square | .62302927 5 .12460585 26.70 0.0000 tx | .40999477 2 .20499738 43.93 0.0000 period | .99806797 2 .49903398 106.94 0.0000 period#square | .05259081 10 .00525908 1.13 0.3868 cow|square | .28424105 12 .02368675 5.08 0.0005 | Residual | .1026584 22 .00466629 --------------+---------------------------------------------------- Total | 2.4705823 53 .04661476 .