* do-file for lecture 7 of VHM 802, Winter 2015
version 13
set more off
cd "h:\vhm\vhm802\data_csv"

insheet using ch08ta6.csv, clear /* Example 8.6 */
anova tfaa r50 r21 r50#r21 /* same with r50##r21 only */
regress  /* Stata parametrization; note different P-values! */
* interaction plot
margins r50#r21
marginsplot, noci /* CIs often look messy */
* contrasts for SS decomposition computed manually
egen tx=group(r50 r21) /* combined tx variable */
table tx r50 r21 /* check of coding: 2 ~ r50=0, r21=1, etc. */
anova tfaa tx
lincom 1.tx+2.tx-3.tx-4.tx /* r50 */
lincom 1.tx-2.tx+3.tx-4.tx /* r21 */
lincom 1.tx-2.tx-3.tx+4.tx /* r50*r21 */

insheet using ch08ta7.csv, clear /* Example 8.8 */
generate lnfault=ln(fault)
anova lnfault alg##seq##size##alloc /* DFE=0 */
anova lnfault alg##seq##size alg##seq##alloc alg##size##alloc seq##size##alloc

insheet using ch10ta1.csv, clear /* Example 10.3 */
anova y a##b /* partial SS */
anova y a##b, sequential /* SS for b without interaction */
anova y b##a, sequential /* SS for a without interaction */

insheet using ch09ta2.csv, clear /* Example 9.2 */
anova y a##b##c##d
anova y a b c d a#b#c#d
regress /* some indication that cell (1,1,1,1) is extreme */
generate cell_1111=(a==1)*(b==1)*(c==1)*(d==1)
anova y a b c d cell_1111 a##b##c##d, sequential /* note: unnatural order */
anova y a b c d cell_1111

insheet using ch08ta9.csv, clear /* Example 8.10 */
egen tx=group(at gt v)
xi: boxcox amylase i.tx
generate lnam=ln(amylase)
anova lnam atemp##gtemp##v
anova lnam atemp##gtemp gtemp##v
* atemp modelled as continuous
anova lnam c.atemp##gtemp gtemp##v
regress
anova lnam c.atemp##c.atemp##gtemp gtemp##v
regress
anova lnam c.atemp##c.atemp##c.atemp##gtemp gtemp##v

* Example 10.1: analysis with missing value
anova lnam atemp##gtemp gtemp##v if _n>1
margins gtemp#v, asbalanced /* least squares means */
margins gtemp#v /* all estimates different */
