* do-file for Additional exercise 10.9, VHM 802, Winter 2014
version 13
set more off
cd "h:\vhm\vhm802"

insheet using data_csv\hs10_9.csv, clear
encode eye, gen(Eye)
encode power, gen(Power)

* anova-based analysis
anova acuity Eye person / Eye#person Power##Eye
predict yhat
predict stdres, rstandard
scatter stdres yhat
predict delres, rstudent
summarize delres
* redo without outlier; note that tests do not take into account unbalancedness
generate acuity_1=acuity
replace acuity_1=. if person==6 & acuity==94 & Eye==1
anova acuity_1 Eye person / Eye#person Power##Eye
predict yhat2
predict stdres2, rstandard
scatter stdres2 yhat2
qnorm stdres2
swilk stdres2

* repeated measures ANOVA; * does not take into account the unbalancedness
anova acuity_1 Eye person / Eye#person Power##Eye, repeated(Power)

* likelihood-based analysis (with random person effects, but fixed person effects are possible as well)
* accounts fully for unbalancedness
gen eyeid=Eye*10+person
mixed acuity_1 Power##Eye || person: || eyeid:, reml
testparm i.Power#i.Eye
mixed acuity_1 i.Power i.Eye || person: || eyeid:, reml
testparm i.Power
* contrast analysis
lincom 1.Power+2.Power+3.Power-3*4.Power

* model with additional person*power random effect is difficult to fit in Stata
* but it is always possible to run the anova command if one can figure out the table
