-
Notifications
You must be signed in to change notification settings - Fork 0
/
gammarus_short.R
198 lines (172 loc) · 6.95 KB
/
gammarus_short.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
setwd(dirname(rstudioapi::getActiveDocumentContext()$path))
data <- read.csv('fullData.csv', h = T)
datab <- read.csv('fullDataZoomed.csv', h = T)
shapiro.test(data$NearestNeighbor)
shapiro.test(data$PairwiseDistance)
library(car)
leveneTest(NearestNeighbor ~ Treatment,data = data)
leveneTest(PairwiseDistance ~ Treatment, data = data)
##Assumptions are good
##Full 5 minutes (21 frames, each 15 seconds##
##############################################
library(MASS)
library(nlme)
fitPWaD.aut <- glmmPQL(PairwiseDistance~ Treatment,
random = list(~1|Day, ~1|Frame, ~1|Pointer),
correlation = corAR1(),
data = data,
family = 'gaussian')
summary(fitPWaD.aut)
fitNN.aut <- glmmPQL(NearestNeighbor~ Treatment,
random = list(~1|Day, ~1|Frame, ~1|Pointer),
correlation = corAR1(),
data = data,
family = 'gaussian')
summary(fitNN.aut) #no signifcance
#PostHoc
#install.packages("multcomp")
library(multcomp)
model.matrix.gls <- function(object, ...) {model.matrix(terms(object), data = getData(object), ...)} # nodig voor posthoc
model.frame.gls <- function(object, ...) {model.frame(formula(object), data = getData(object), ...)} # nodig voor posthoc
terms.gls <- function(object, ...) {terms(model.frame(object), ...)} #--> nodig voor posthoc
#PWaD
summary(glht(fitPWaD.aut, linfct = mcp(Treatment = "Tukey")), test = adjusted("holm")) # de posthoc test
#combined vs conspecific is significant w P = 0.00673, Consp -Contr P = 0.04887
##########
##GGPlot##
##########
coll <- c('#9cacbc','#3c6c7c', '#144c9c', '#000c2e') #some nice blue colors
#Plot NN through Frame
library(ggplot2)
ggplot(data = data, aes(y = NearestNeighbor,
x = Frame,
color = Treatment)) +
geom_point()+
geom_smooth(se = T)+
ggtitle('Average Nearest Neighbor')+
theme_classic()+
scale_color_manual(values = coll)
#Plot PWaD
ggplot(data = data, aes(y = PairwiseDistance,
x = Frame,
color = Treatment)) +
geom_point()+
geom_smooth(se = T)+
ggtitle('Average Pairwise Distance')+
theme_classic()+
scale_color_manual(values = coll)
#Barplot
library(effects)
#install.packages("ggsignif")
library(ggsignif)
#PWaD
dataCorrPWaD <- data.frame(effect(mod =fitPWaD.aut, term = 'Treatment'))
lvl_pwad <- factor(dataCorrPWaD$Treatment, level = c('Control', 'Predator', 'Conspecific', 'Combined'))
ggplot(data=dataCorrPWaD, aes(x= lvl_pwad, y = fit, color = Treatment))+
geom_bar(stat="identity", fill=coll, color = 'black', alpha = 0.6,
position=position_dodge()) +
geom_errorbar(aes(ymin=fit-se, ymax=fit+se), width=.2,
position=position_dodge(.9), color = 'black') +
theme_classic() +shapiro.test(data$NearestNeighbor)
##Detailed Analysis##
#####################
#1 frame every 5 seconds from 2,5 to 5 minutes
##MODELS
#PWaD
fitPWaDb.aut <- glmmPQL(PairwiseDistance~ Treatment,
random = list(~1|Day, ~1|Frame, ~1|Pointer),
correlation = corAR1(),
data = datab,
family = 'gaussian')
summary(fitPWaDb.aut)
#Post Hoc
summary(glht(fitPWaDb.aut, linfct = mcp(Treatment = "Tukey")), test = adjusted("holm")) # de posthoc test
#many significant combos
#NN
fitNNb.aut <- glmmPQL(NearestNeighbor~ Treatment,
random = list(~1|Day, ~1|Frame, ~1|Pointer),
correlation = corAR1(),
data = datab,
family = 'gaussian')
summary(fitNNb.aut) #No signifcance
#autocorrelation plot
#PWaD
x = seq(1,length(fitPWaDb.aut$residuals))
ggplot() +
geom_line(aes(x = x, y = fitPWaDb.aut$residuals), color = "red") +
geom_hline(yintercept=0, linetype="dashed", color='blue')+
theme_classic()+
ylab('Residuals')+
ggtitle('PWD - Autocorrelation Plot - Per 25 Frames')
#NN
x = seq(1,length(fitNNb.aut$residuals))
ggplot() +
geom_line(aes(x = x, y = fitNNb.aut$residuals), color = "red") +
geom_hline(yintercept=0, linetype="dashed", color='blue')+
theme_classic()+
ylab('Residuals')+
ggtitle('NN - Autocorrelation Plot - Per 25 Frames')
##########
##GGPlot##
##########
str(datab)
data$Treatment <- as.factor(data$Treatment)
#Plot NN
ggplot(data = datab, aes(y = NearestNeighbor,
x = Frame,
color = Treatment)) +
geom_point(alpha = 0.4, size = 0.8)+
geom_smooth(se = T)+
ggtitle('Average Nearest Neighbor - Detailed')+
theme_classic()+
scale_color_manual(values = coll)
#Plot PWaD
ggplot(data = datab, aes(y = PairwiseDistance,
x = Frame,
color = Treatment)) +
geom_point(alpha = 0.4, size = 0.8)+
geom_smooth(se = T)+
ggtitle('Average Pairwise Distance - Detailed')+
theme_classic()+
scale_color_manual(values = coll)
#Barplot w error
#install.packages('ggsignif')
library(ggsignif)
library(effects)
#NN
dataCorrNNb <- data.frame(effect(mod =fitNNb.aut, term = 'Treatment'))
lvl_NNb <- factor(dataCorrNNb$Treatment, level = c('Control', 'Predator', 'Conspecific', 'Combined'))
ggplot(data=dataCorrNNb, aes(x= lvl_NNb, y = fit, color = Treatment))+
geom_bar(stat="identity", fill=coll, color = 'black', alpha = 0.6,
position=position_dodge()) +
geom_errorbar(aes(ymin=fit-se, ymax=fit+se), width=.2,
position=position_dodge(.9), color = 'black') +
theme_classic() +
ggtitle('Nearest Neighbor - Detailed')+
ylab('Average Nearest Neighbor') +
scale_fill_manual(values = coll)+
xlab('Treatment')+
theme(legend.position = 'none')
#PWaD
dataCorrPWaDb <- data.frame(effect(mod =fitPWaDb.aut, term = 'Treatment'))
lvl_PWaDb <- factor(dataCorrPWaDb$Treatment, level = c('Control', 'Predator', 'Conspecific', 'Combined'))
ggplot(data=dataCorrPWaDb, aes(x= lvl_PWaDb, y = fit, color = Treatment))+
geom_bar(stat="identity", fill=coll, color = 'black', alpha = 0.6,
position=position_dodge()) +
geom_errorbar(aes(ymin=fit-se, ymax=fit+se), width=.2,
position=position_dodge(.9), color = 'black') +
theme_classic() +
ggtitle('Pairwise Distance - Detailed')+
ylab('Average Pairwise Distance') +
geom_signif(comparisons = list(c("Control", "Conspecific"), c("Conspecific", "Combined"), c("Conspecific","Predator"), c("Combined", "Predator")),
annotations = c("***", "***", "*", "**"), textsize = 5,
y_position = 450, extend_line = -0.01, step_increase = 0.3,
color = 'black', vjust = 0.7)+
scale_fill_manual(values = coll)+
xlab('Treatment')+
theme(legend.position = 'none')
#some correlation between nearest neighbor and average pairwaise distance
cor.test(data[,7], data[,8])
scatterplot(data[,7], data[,8], xlab = 'Nearest Neighbor', ylab = 'Pairwise Distance')
cor.test(datab[,7], datab[,8])
scatterplot(datab[,7], datab[,8], xlab = 'Nearest Neighbor', ylab = 'Pairwise Distance')