-
Notifications
You must be signed in to change notification settings - Fork 197
/
Copy pathbaci-crabs-mod.r
321 lines (250 loc) · 10 KB
/
baci-crabs-mod.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
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
# BACI design with multiple controls; 2 factor; interaction;
# 2015-07-15 CJS update misc topics
# 2014-11-27 CJS added sf.autoplot.lmer
# 2014-11-26 CJS split; ggplot; ##--- problem; use lmerTest;
# A BACI design was used to assess the impact
# of cooling water discharge on the density of
# shore crabs.
# The beach near the outlet of the cooling water
# was sampled using several quadrats
# before and after the plant started operation.
# Two control beaches at other locations
# in the inlet were also sampled.
options(useFancyQuotes=FALSE) # renders summary output corrects
library(ggplot2)
library(lmerTest)
library(lsmeans)
library(plyr)
library(reshape2)
source("http://www.stat.sfu.ca/~cschwarz/Stat-650/Notes/MyPrograms/schwarz.functions.r")
# Read in the actual data
sink("baci-crabs-mod-R-001.txt", split=TRUE)
##***part001b;
cat(" BACI design measuring crab Density with multiple sites but one year before/after \n\n")
crabs <- read.csv("baci-crabs-mod.csv", header=TRUE, as.is=TRUE, strip.white=TRUE)
crabs$trt <- interaction(crabs$SiteClass, crabs$Site, crabs$Period)
crabs$Site <- factor(crabs$Site)
crabs$SiteClass <- factor(crabs$SiteClass)
crabs$Period <- factor(crabs$Period, levels=c("Before","After"), ordered=TRUE)
crabs$trt <- factor(crabs$trt)
cat("Listing of part of the raw data \n")
crabs[1:10,]
##***part001e;
sink()
str(crabs)
# Preliminary plot
# Get side-by-side dot plots
##***part010b;
prelimplot <- ggplot(data=crabs, aes(x=trt, y=Density))+
ggtitle("Preliminary plot to look for outliers etc")+
geom_point(position=position_jitter(w=0.1))+
geom_boxplot(alpha=0.1)
prelimplot
##***part010e;
ggsave(plot=prelimplot, file="baci-crabs-mod-R-010.png",
h=4, w=6, units="in", dpi=300)
# Get some simple summary statistics
sink('baci-crabs-mod-R-020.txt', split=TRUE)
##***part020b;
report <- ddply(crabs, c("Period","SiteClass","Site"), function(x){
res <- sf.simple.summary(x, "Density", crd=TRUE)
return(res)
})
cat("\n\n Summary report \n")
report
##***part020e;
sink()
# Draw a profile plot
##***part030b;
profileplot <- ggplot(data=report, aes(x=Period, y=mean,
group=Site, color=SiteClass, shape=Site))+
ggtitle("Profile plot of crab density")+
ylab("Density with mean and 95% ci")+
geom_point(position=position_dodge(w=0.1))+
geom_line(position=position_dodge(w=0.1))+
geom_errorbar(aes(ymax=ucl, ymin=lcl), width=0.2, position=position_dodge(w=0.1))+
geom_point(data=crabs, aes(y=Density), position=position_dodge(w=0.2))
profileplot
##***part030e;
ggsave(plot=profileplot, file="baci-crabs-mod-R-030.png",
h=4, w=6, units="in", dpi=300)
# There are several ways in which this BACI design can be analyzed.
#################################################################################
#################################################################################
# Do a t-test on the differeces of the averages for each site
sink('baci-crabs-mod-R-100.txt', split=TRUE)
##***part100b;
# The averages are available in the report dataframe
crabs.avg <- report[,c("Site","SiteClass","Period","mean")]
crabs.avg
##***part100e;
sink()
sink('baci-crabs-mod-R-101.txt', split=TRUE)
##***part101b;
# Reshape the file to get the Before and After measurements on the same line
#
crabs.site <- dcast(crabs.avg, Site+SiteClass ~ Period, value.var="mean" )
crabs.site$diff <- crabs.site$After - crabs.site$Before
crabs.site
##***part101e;
sink()
# do the two sample t-test not assuming equal variances
# Unfortunately, you need at least 2 sites in EACH SiteClass, so this does not work here
sink('baci-crabs-mod-R-104.txt', split=TRUE)
##***part104b;
result <- try(t.test(diff ~ SiteClass, data=crabs.site),silent=TRUE)
if(class(result)=="try-error")
{cat("Unable to do unequal variance t-test because of small sample size\n")} else
{
result$diff.in.means <- sum(result$estimate*c(1,-1))
names(result$diff.in.means)<- "diff.in.means"
result$se.diff <- result$statistic / abs(result$diff.in.means)
names(result$se.diff) <- 'SE.diff'
print(result)
print(result$diff.in.means)
print(result$se.diff)
}
##***part104e;
sink()
# do the two sample t-test assuming equal variances
# This only requires at least 2 sites in at least one of the SiteClasses
sink('baci-crabs-mod-R-105.txt', split=TRUE)
##***part105b;
result <- t.test(diff ~ SiteClass, data=crabs.site, var.equal=TRUE)
result$diff.in.means <- sum(result$estimate*c(1,-1))
names(result$diff.in.means)<- "diff.in.means"
result$se.diff <- result$statistic / abs(result$diff.in.means)
names(result$se.diff) <- 'SE.diff'
result
result$diff.in.means
result$se.diff
##***part105e;
sink()
#################################################################################
#################################################################################
# Do a Mixed effect linear model on the averages for each site
crabs.avg <- report[,c("Site","SiteClass","Period","mean")]
crabs.avg
sink('baci-crabs-mod-R-200-type3.txt', split=TRUE)
##***part200b;
result.lmer <- lmerTest::lmer(mean ~ SiteClass+Period+SiteClass:Period +
(1|Site), data=crabs.avg)
anova(result.lmer, ddf="Kenward-Roger")
##***part200e;
sink()
sink('baci-crabs-mod-R-200-vc.txt', split=TRUE)
##***part200vcb;
# Extract the variance components
vc <- VarCorr(result.lmer)
vc
##***part200vce;
sink()
# Extract the BACI effect
# You could get this from the summary table looking at the
# interaction term, but it is more robust to get this from the
# lsmeans
summary(result.lmer)
# LSmeans after a lm() fit
# Note that there is a lsmeans() function in both the lsmeans and lmerTest package
# so we must specify which one we want
sink('baci-crabs-mod-R-s00LSM-SiteClass.txt', split=TRUE)
##***parts200LSM-SiteClassb;
result.lmer.lsmo.S <- lsmeans::lsmeans(result.lmer, ~SiteClass)
cat("\n\n Estimated marginal means for SiteClass \n\n")
summary(result.lmer.lsmo.S)
##***parts200LSM-SiteClasse;
sink()
sink('baci-crabs-mod-R-200LSM-Period.txt', split=TRUE)
##***part200LSM-Periodb;
result.lmer.lsmo.P <- lsmeans::lsmeans(result.lmer, ~Period)
cat("\n\n Estimated marginal means for Period \n\n")
summary(result.lmer.lsmo.P)
##***part200LSM-Periode;
sink()
sink('baci-crabs-mod-R-200LSM-int.txt', split=TRUE)
##***part200LSM-intb;
result.lmer.lsmo.SP <- lsmeans::lsmeans(result.lmer, ~SiteClass:Period)
cat("\n\n Estimated marginal means for SiteClass:Period \n\n")
summary(result.lmer.lsmo.SP)
##***part200LSM-inte;
sink()
# You could look at the entry in the summary table from the model fit, but
# this is dangerous as these entries depend on the contrast matrix.
# It is far safer to the contrast function applied to an lsmeans object
temp <- summary(result.lmer)$coefficients # get all the coefficients
temp[grepl("SiteClass",rownames(temp)) & grepl("Period", rownames(temp)),]
sink("baci-crabs-mod-R-200baci.txt", split=TRUE)
##***part200bacib;
# Estimate the BACI contrast along with a se
contrast(result.lmer.lsmo.SP, list(baci=c(1,-1,-1,1)))
confint(contrast(result.lmer.lsmo.SP, list(baci=c(1,-1,-1,1))))
##***part200bacie;
sink()
# Check the residuals etc
##***part200diagnosticb;
diagplot <- sf.autoplot.lmer(result.lmer)
diagplot
##***part200diagnostice;
ggsave(plot=diagplot, file='baci-crabs-mod-R-200-diagnostic.png',
h=4, w=6, unit="in", dpi=300)
#################################################################################
#################################################################################
# Do a Mixed effect linear model on the individual values
# Results will differ slightly from above analyeses on the averages because
# the design is not balanced.
sink('baci-crabs-mod-R-300-type3.txt', split=TRUE)
##***part300b;
result.all.lmer <- lmer(Density ~ SiteClass+Period+SiteClass:Period +
(1|Site) + (1|Site:Period), data=crabs)
anova(result.all.lmer, ddf="Kenward-Roger")
##***part300e;
sink()
summary(result.all.lmer)
sink('baci-crabs-mod-R-300-vc.txt', split=TRUE)
##***part300vcb;
# Extract the variance components
vc <- VarCorr(result.all.lmer)
vc
##***part300vce;
sink()
# LSmeans after a lm() fit
sink('baci-crabs-mod-R-s300LSM-SiteClass.txt', split=TRUE)
##***parts300LSM-SiteClassb;
result.all.lmer.lsmo.S <- lsmeans::lsmeans(result.all.lmer, ~SiteClass)
cat("\n\n Estimated marginal means for SiteClass\n\n")
summary(result.all.lmer.lsmo.S)
##***parts300LSM-SiteClasse;
sink()
sink('baci-crabs-mod-R-300LSM-Period.txt', split=TRUE)
##***part300LSM-Periodb;
result.all.lmer.lsmo.P <- lsmeans::lsmeans(result.all.lmer, ~ Period)
cat("\n\n Estimated marginal means for Period \n\n")
summary(result.all.lmer.lsmo.P)
##***part300LSM-Periode;
sink()
sink('baci-crabs-mod-R-300LSM-int.txt', split=TRUE)
##***part300LSM-intb;
result.all.lmer.lsmo.SP <- lsmeans::lsmeans(result.all.lmer, ~SiteClass:Period)
cat("\n\n Estimated marginal means for SiteClass:Period \n\n")
summary(result.all.lmer.lsmo.SP)
##***part300LSM-inte;
sink()
# You could look at the entry in the summary table from the model fit, but
# this is dangerous as these entries depend on the contrast matrix.
# It is far safer to the contrast function applied to an lsmeans object
temp <- summary(result.all.lmer)$coefficients # get all the coefficients
temp[grepl("SiteClass",rownames(temp)) & grepl("Period", rownames(temp)),]
sink("baci-crabs-mod-R-300baci.txt", split=TRUE)
##***part300bacib;
# Estimate the BACI contrast along with a se
contrast(result.lmer.lsmo.SP, list(baci=c(1,-1,-1,1)))
confint(contrast(result.lmer.lsmo.SP, list(baci=c(1,-1,-1,1))))
##***part300bacie;
sink()
# Check the residuals etc
##***part300diagnosticb;
diagplot <- sf.autoplot.lmer(result.all.lmer)
diagplot
##***part300diagnostice;
ggsave(plot=diagplot, file='baci-crabs-mod-R-300-diagnostic.png',
h=4, w=6, units="in", dpi=300)