-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathUnsafe_Sex_Rcode.R
More file actions
250 lines (196 loc) · 9.02 KB
/
Unsafe_Sex_Rcode.R
File metadata and controls
250 lines (196 loc) · 9.02 KB
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
## Official Analysis: mating project
getwd()
source("/Users/paulknoops/Bioinformatics/R-projects_git/mating_under_predation/packages.R")
######################
## Courtship Analysis
courtship <- read.csv("/Users/paulknoops/Bioinformatics/R-projects_git/mating_under_predation/Immature.csv",h=T)
#Remove Temp, Humidity and BP
courtship <- subset(courtship, select =
-c(Temp, Humidity, BP.12.00.am, BP.8.00.Am, BP.Room ))
head(courtship)
# Change time (in HH:MM:SS) format to seconds (One had a value of -1, not sure, but changed to 0)
# Start time of behaviours will be the time at end of courtship bout of full trial (trial_latency_behav_end minus the courtship duration)
courtship$startTimeSeconds <- (courtship$trial_latency_behav_end - courtship$court_duration)
courtship$startTimeSeconds[1339] = 0
# Create new column of relative values for courtship start times (The start of observations are relative to the observation initiation of the certain vial (subtract observation initiation)
courtship$relativeStartTimeSeconds <- (courtship$startTimeSeconds - courtship$Observation.Initiation)
# New column for relative values of trial duration at end of behaviour (the length of behaviours)
courtship$relativeTrial_latency_end <- (courtship$relativeStartTimeSeconds + courtship$court_duration)
#Remove some unneeded columns:
courtship <- subset(courtship, select =
-c(Day, start_time_program, Activity,
trial_latency_behav_end, Video_Initition,
Video_End, startTimeSeconds) )
# Need to get all courtship under 900 seconds (relative court duration)
#First, transition step for finding values ending abov 900
courtship$nineHundredTransition <- (900 - courtship$relativeTrial_latency_end)
# Second, if value for nineHundredTransition is negative, equate it to 900, all else stay as relativeTrial_latency_end
courtship$relativeTrialLatency900 <- ifelse(courtship$nineHundredTransition<0,
900,
courtship$relativeTrial_latency_end)
# Third, relative courtship duration (not including values over 900)
courtship$relativeCourtDuration <- (courtship$relativeTrialLatency900 - courtship$relativeStartTimeSeconds)
#Rename Box to treatment type, and make characters to factor (to run later for plot(effect()))
courtship$Treatment <- ifelse(courtship$Box=="A",
"Predator",
"Control")
courtship$Treatment <- factor(courtship$Treatment)
courtship <- subset(courtship, select =
-c(Box, court_duration, relativeTrial_latency_end,
nineHundredTransition))
courtship2 <- subset(courtship, relativeCourtDuration>0)
courtship2 <- courtship2 %>%
group_by(Date,Replicate,Vial_number,Observation.Initiation, Treatment) %>%
summarise(Court_sum=sum(relativeCourtDuration),
count=n(),
court_prop=sum(relativeCourtDuration/900))
#The Models:
head(courtship2)
courtship_model1 <- lmer(court_prop ~ Treatment +
(1|Date),
data=courtship2 )
corprop_eff <- effect("Treatment", courtship_model1)
corprop_eff <- as.data.frame(corprop_eff)
corprop_eff$Behaviour <- "Proportion Time Courting"
summary(courtship_model1)
car::Anova(courtship_model1)
gg_courtProp <- ggplot(corprop_eff, aes(x=Treatment, y=fit, colour=Treatment))
gg_courtProp2 <- gg_courtProp +
ylab("Proportion") +
xlab("") +
ylim(0,1) +
geom_point(stat="identity",
position = position_dodge(.9), size=5, show.legend = F) +
geom_errorbar(aes(ymin = lower, ymax = upper),
position = position_dodge(.9),
size = 1.2,
width = 0.2, show.legend = F) +
theme(text = element_text(size=20),
# axis.text.x=element_blank(),
axis.text.x= element_text(size=15),
axis.ticks.x=element_blank()) +
scale_color_manual(values=c("#999999", "#E69F00"))
print(gg_courtProp2)
courtship_model2 <- lmer(count ~ Treatment +
(1|Date),
data = courtship2)
summary(courtship_model2)
corcount_eff<- effect("Treatment", courtship_model2)
corcount_eff <- as.data.frame(corcount_eff)
corcount_eff$Behaviour <- "Courtship Bouts"
summary(courtship_model2)
car::Anova(courtship_model2)
gg_courtcount <- ggplot(corcount_eff, aes(x=Treatment, y=fit, colour=Treatment))
#gg_courtcount <- ggplot(corcount_eff, aes(x=Treatment, y=fit))
gg_courtcount2 <- gg_courtcount +
ylab("Behavioural Count") +
xlab("") +
ylim(9,20) +
geom_point(stat="identity",
position = position_dodge(.9), size=5, show.legend = F) +
geom_errorbar(aes(ymin = lower, ymax = upper),
position = position_dodge(.9),
size = 1.2,
width = 0.2, show.legend = F) +
theme(text = element_text(size=20),
# axis.text.x=element_blank(),
axis.text.x= element_text(size=15),
axis.ticks.x=element_blank()) +
scale_color_manual(values=c("#999999", "#E69F00"))
print(gg_courtcount2)
multiplot(gg_courtProp2, gg_courtcount2, cols=2)
###############
## Copulation Analysis
copulation <- read.csv("/Users/paulknoops/Bioinformatics/R-projects_git/mating_under_predation/Mature.csv",h=T)
#Change box to treatment and to Predator vs. Control
copulation$Treatment <- ifelse(copulation$Box=="C",
"Predator",
"Control")
copulation$Treatment <- factor(copulation$Treatment)
#Remove Temp, and other unneccessary columns)
copulation <- subset(copulation, select =
-c(Box, File, Time_In, Cop_start, Cop_end,
Temp, Humidity, BP.12.00.am, BP.8.00.Am, BP.Room))
#Copulation Proportion:
copulation$copulationSuccess <- ifelse(copulation$Cop_latency==0,
0, 1)
#Removing all values with no copulation (new data frame)
copulation2 <- copulation[!(copulation$Cop_latency==0),]
## Models:
#copulation proportion
copprop_mod <- glmer(copulationSuccess ~ Treatment + (1|Date),
data = copulation, family = "binomial")
copprop_eff <- effect("Treatment", copprop_mod)
copprop_eff <- as.data.frame(copprop_eff)
copprop_eff$Behaviour <- "Proportion Copulation"
summary(copprop_mod)
car::Anova(copprop_mod)
gg_copprop <- ggplot(copprop_eff, aes(x=Treatment, y=fit, colour=Treatment))
gg_copprop2 <- gg_copprop +
ylab("Proportion") +
xlab("") +
ylim(0,1) +
geom_point(stat="identity",
position = position_dodge(.9), size=5, show.legend = F) +
geom_errorbar(aes(ymin = lower, ymax = upper),
position = position_dodge(.9),
size = 1.2,
width = 0.2, show.legend = F) +
theme(text = element_text(size=20),
# axis.text.x=element_blank(),
axis.text.x= element_text(size=15),
axis.ticks.x=element_blank()) +
scale_color_manual(values=c("#999999", "#E69F00"))
print(gg_copprop2)
# Copulation Latency
copul_lat_mod <- lmer(Cop_latency ~ Treatment + (1|Date),
data = copulation2)
coplat_0_eff <- effect("Treatment", copul_lat_mod)
coplat_0_eff <- as.data.frame(coplat_0_eff)
coplat_0_eff$Behaviour <- "Copulation Latency"
summary(copul_lat_mod)
car::Anova(copul_lat_mod)
summary(copulation2)
gg_coplat <- ggplot(coplat_0_eff, aes(x=Treatment, y=fit, colour=Treatment))
gg_coplat2 <- gg_coplat +
ylab("Time (sec)") +
xlab("") +
ylim(300,800) +
geom_point(stat="identity",
position = position_dodge(.9), size=5, show.legend = F) +
geom_errorbar(aes(ymin = lower, ymax = upper),
position = position_dodge(.9),
size = 1.2,
width = 0.2, show.legend = F) +
theme(text = element_text(size=20),
# axis.text.x=element_blank(),
axis.text.x= element_text(size=15),
axis.ticks.x=element_blank()) +
scale_color_manual(values=c("#999999", "#E69F00"))
print(gg_coplat2)
# Copulation Duration:
copul_dur_Mod <- lmer(Cop_Duration ~ Treatment + (1|Date),
data = copulation2)
copdur_0_eff <- effect("Treatment", copul_dur_Mod)
copdur_0_eff <- as.data.frame(copdur_0_eff)
copdur_0_eff$Behaviour <- "Copulation Duration"
summary(copul_dur_Mod)
car::Anova(copul_dur_Mod)
gg_copdur <- ggplot(copdur_0_eff, aes(x=Treatment, y=fit, colour=Treatment))
gg_copdur2 <- gg_copdur +
ylab("Time (sec)") +
xlab("") +
ylim(700,1000) +
geom_point(stat="identity",
position = position_dodge(.9), size=5, show.legend = F) +
geom_errorbar(aes(ymin = lower, ymax = upper),
position = position_dodge(.9),
size = 1.2,
width = 0.2, show.legend = F) +
theme(text = element_text(size=20),
# axis.text.x=element_blank(),
axis.text.x= element_text(size=15),
axis.ticks.x=element_blank()) +
scale_color_manual(values=c("#999999", "#E69F00"))
print(gg_copdur2)
multiplot(gg_copdur2, gg_coplat2, gg_copprop2, cols=2)