-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
anovaCRFpq.R
105 lines (84 loc) · 4.02 KB
/
anovaCRFpq.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
## ------------------------------------------------------------------------
wants <- c("car", "DescTools", "multcomp", "phia")
has <- wants %in% rownames(installed.packages())
if(any(!has)) install.packages(wants[!has])
## ------------------------------------------------------------------------
set.seed(123)
Njk <- 8
P <- 2
Q <- 3
muJK <- c(rep(c(1, -1), Njk), rep(c(2, 1), Njk), rep(c(3, 3), Njk))
dfCRFpq <- data.frame(IV1=factor(rep(1:P, times=Njk*Q)),
IV2=factor(rep(1:Q, each=Njk*P)),
DV =rnorm(Njk*P*Q, muJK, 2))
## ------------------------------------------------------------------------
dfCRFpq$IVcomb <- interaction(dfCRFpq$IV1, dfCRFpq$IV2)
## ------------------------------------------------------------------------
aovCRFpq <- aov(DV ~ IV1*IV2, data=dfCRFpq)
summary(aovCRFpq)
## ------------------------------------------------------------------------
# change contrasts for SS type III
fitIII <- lm(DV ~ IV1 + IV2 + IV1:IV2, data=dfCRFpq,
contrasts=list(IV1=contr.sum, IV2=contr.sum))
library(car) # for Anova()
Anova(fitIII, type="III")
## ----rerAnovaCRFpq01-----------------------------------------------------
plot.design(DV ~ IV1*IV2, data=dfCRFpq, main="Marginal means")
interaction.plot(dfCRFpq$IV1, dfCRFpq$IV2, dfCRFpq$DV,
main="Cell means", col=c("red", "blue", "green"), lwd=2)
## ------------------------------------------------------------------------
library(DescTools)
EtaSq(aovCRFpq, type=1)
## ------------------------------------------------------------------------
library(phia)
testInteractions(aovCRFpq, fixed="IV2", across="IV1", adjustment="none")
testInteractions(aovCRFpq, fixed="IV1", across="IV2", adjustment="none")
## ------------------------------------------------------------------------
cMat <- rbind("c1"=c( 1/2, 1/2, -1),
"c2"=c( -1, 0, 1))
library(multcomp)
summary(glht(aovCRFpq, linfct=mcp(IV2=cMat), alternative="two.sided"),
test=adjusted("bonferroni"))
## ------------------------------------------------------------------------
aovCRF <- aov(DV ~ IV1 + IV2, data=dfCRFpq)
TukeyHSD(aovCRF, which="IV2")
## ------------------------------------------------------------------------
library(multcomp)
tukey <- glht(aovCRF, linfct=mcp(IV2="Tukey"))
summary(tukey)
confint(tukey)
## ------------------------------------------------------------------------
(aovCRFpqA <- aov(DV ~ IVcomb, data=dfCRFpq))
cntrMat <- rbind("c1"=c(-1/2, 1/4, -1/2, 1/4, 1/4, 1/4),
"c2"=c( 0, 0, -1, 0, 1, 0),
"c3"=c(-1/2, -1/2, 1/4, 1/4, 1/4, 1/4))
## ------------------------------------------------------------------------
library(multcomp)
summary(glht(aovCRFpqA, linfct=mcp(IVcomb=cntrMat), alternative="greater"),
test=adjusted("none"))
## ------------------------------------------------------------------------
library(DescTools)
ScheffeTest(aovCRFpqA, which="IVcomb", contrasts=t(cntrMat))
## ------------------------------------------------------------------------
library(DescTools)
ScheffeTest(aovCRFpq, which="IV2", contrasts=c(-1, 1/2, 1/2))
## ----rerAnovaCRFpq02-----------------------------------------------------
Estud <- rstudent(aovCRFpq)
qqnorm(Estud, pch=20, cex=2)
qqline(Estud, col="gray60", lwd=2)
## ------------------------------------------------------------------------
shapiro.test(Estud)
## ----rerAnovaCRFpq03-----------------------------------------------------
plot(Estud ~ dfCRFpq$IVcomb, main="Residuals per group")
## ------------------------------------------------------------------------
library(car)
leveneTest(aovCRFpq)
## ------------------------------------------------------------------------
try(detach(package:phia))
try(detach(package:car))
try(detach(package:multcomp))
try(detach(package:survival))
try(detach(package:mvtnorm))
try(detach(package:splines))
try(detach(package:TH.data))
try(detach(package:DescTools))