-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRinA CH09 Code.txt
executable file
·174 lines (131 loc) · 4.57 KB
/
RinA CH09 Code.txt
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
#------------------------------------------------------------------ #
# R in Action: Chapter 9 #
# requires that the multcomp, gplots, car, HH, effects, #
# rrcov, mvoutlier, MASS packages have been installed #
# install.packages(c('multcomp', 'gplots', 'car', 'HH', 'effects', #
# 'rrcov', 'mvoutlier', 'MASS')) #
#-------------------------------------------------------------------#
# pause for each graph
par(ask = TRUE)
# save original graphical parameters
opar <- par(no.readonly = TRUE)
# Listing 9.1 - One-way ANOVA
library(multcomp)
attach(cholesterol)
table(trt)
aggregate(response, by = list(trt), FUN = mean)
aggregate(response, by = list(trt), FUN = sd)
fit <- aov(response ~ trt)
summary(fit)
library(gplots)
plotmeans(response ~ trt, xlab = "Treatment", ylab = "Response",
main = "Mean Plot\nwith 95% CI")
detach(cholesterol)
# Listing 9.2 - Tukey HSD pairwise group comparisons
TukeyHSD(fit)
par(las = 2)
par(mar = c(5, 8, 4, 2))
plot(TukeyHSD(fit))
par(opar)
# Multiple comparisons the multcomp package
library(multcomp)
par(mar = c(5, 4, 6, 2))
tuk <- glht(fit, linfct = mcp(trt = "Tukey"))
plot(cld(tuk, level = 0.05), col = "lightgrey")
par(opar)
# Assessing normality
library(car)
qqPlot(lm(response ~ trt, data = cholesterol), simulate = TRUE,
main = "QQ Plot", labels = FALSE)
# Assessing homogeneity of variances
bartlett.test(response ~ trt, data = cholesterol)
# Assessing outliers
library(car)
outlierTest(fit)
# Listing 9.3 - One-way ANCOVA
data(litter, package = "multcomp")
attach(litter)
table(dose)
aggregate(weight, by = list(dose), FUN = mean)
fit <- aov(weight ~ gesttime + dose)
summary(fit)
# Obtaining adjusted means
library(effects)
effect("dose", fit)
# Listing 9.4 - Multiple comparisons using user supplied contrasts
library(multcomp)
contrast <- rbind(`no drug vs. drug` = c(3, -1, -1, -1))
summary(glht(fit, linfct = mcp(dose = contrast)))
# Listing 9.5 - Testing for Homegeneity of Regression Slopes
library(multcomp)
fit2 <- aov(weight ~ gesttime * dose)
summary(fit2)
# Visualizing a one-way ANCOVA
library(HH)
ancova(weight ~ gesttime + dose, data = litter)
# Listing 9.6 - Two way ANOVA
attach(ToothGrowth)
table(supp, dose)
aggregate(len, by = list(supp, dose), FUN = mean)
aggregate(len, by = list(supp, dose), FUN = sd)
fit <- aov(len ~ supp * dose)
summary(fit)
# plotting interactions
interaction.plot(dose, supp, len, type = "b", col = c("red",
"blue"), pch = c(16, 18),
main = "Interaction between Dose and Supplement Type")
library(gplots)
plotmeans(len ~ interaction(supp, dose, sep = " "),
connect = list(c(1, 3, 5), c(2, 4, 6)),
col = c("red", "darkgreen"),
main = "Interaction Plot with 95% CIs",
xlab = "Treatment and Dose Combination")
library(HH)
interaction2wt(len ~ supp * dose)
# Listing 9.7 - Repeated measures ANOVA with one between
# and within groups factor
w1b1 <- subset(CO2, Treatment == "chilled")
fit <- aov(uptake ~ (conc * Type) + Error(Plant/(conc)),
w1b1)
summary(fit)
par(las = 2)
par(mar = c(10, 4, 4, 2))
with(w1b1, interaction.plot(conc, Type, uptake, type = "b",
col = c("red", "blue"), pch = c(16, 18), main = "Interaction Plot for Plant Type and Concentration"))
boxplot(uptake ~ Type * conc, data = w1b1, col = (c("gold",
"green")), main = "Chilled Quebec and Mississippi Plants",
ylab = "Carbon dioxide uptake rate (umol/m^2 sec)")
par(opar)
# Listing 9.8 - One-way MANOVA
library(MASS)
attach(UScereal)
y <- cbind(calories, fat, sugars)
aggregate(y, by = list(shelf), FUN = mean)
cov(y)
fit <- manova(y ~ shelf)
summary(fit)
summary.aov(fit)
# Listing 9.9 - Assessing multivariate normality
# identify points interactively with the mouse
center <- colMeans(y)
n <- nrow(y)
p <- ncol(y)
cov <- cov(y)
d <- mahalanobis(y, center, cov)
coord <- qqplot(qchisq(ppoints(n), df = p), d, main = "QQ Plot Assessing Multivariate Normality",
ylab = "Mahalanobis D2")
abline(a = 0, b = 1)
identify(coord$x, coord$y, labels = row.names(UScereal))
# multivariate outliers
library(mvoutlier)
outliers <- aq.plot(y)
outliers
# Listing 9.10 - Robust one-way MANOVA
# this may take a while...
library(rrcov)
Wilks.test(y, shelf, method = "mcd")
# Listing 9.11 - A regression approach to the ANOVA
# problem in section 9.3
fit.lm <- lm(response ~ trt, data=cholesterol)
summary(fit.lm)
contrasts(cholesterol$trt)