-
Notifications
You must be signed in to change notification settings - Fork 0
/
convex_hull_during.R
248 lines (212 loc) · 10.2 KB
/
convex_hull_during.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
data <- read.csv(here("Documents","data","temp_collective","roi","convex_hull_during_loom.csv"),header=TRUE,na.strings=c("[nan]"))
# make new dataframe
my_data<-data.frame("temp" = data$Temperature[complete.cases(data$convex_hull_area_600_650)],
"gs" = data$Groupsize[complete.cases(data$convex_hull_area_600_650)],
"loom" = data$Loom[complete.cases(data$convex_hull_area_600_650)],
"kt"=1/(0.00008617*(data$Temperature[complete.cases(data$convex_hull_area_600_650)]+273.1)),
"date"=as.numeric(as.Date(data$Date[complete.cases(data$convex_hull_area_600_650)], format = "%d/%m/%Y")),
"trial" = data$Trial[complete.cases(data$convex_hull_area_600_650)],
"subtrial" = data$Subtrial[complete.cases(data$convex_hull_area_600_650)],
"t1" = as.numeric(as.POSIXct(data$Time_fish_in[complete.cases(data$convex_hull_area_600_650)], format = "%H:%M")),
"t2" = as.numeric(as.POSIXct(data$Time_start_record[complete.cases(data$convex_hull_area_600_650)], format = "%H:%M")),
"t" = as.numeric(as.POSIXct(data$Time_start_record[complete.cases(data$convex_hull_area_600_650)], format = "%H:%M"))
- as.numeric(as.POSIXct(data$Time_fish_in[complete.cases(data$convex_hull_area_600_650)], format = "%H:%M")),
"hull" = data$convex_hull_area_600_650[complete.cases(data$convex_hull_area_600_650)]
)
model_lm <- lm(hull ~ gs + temp + loom, my_data)
summary(model_lm)
plot(fitted(model_lm),residuals(model_lm))
#r sq = 0.4258
extractAIC(model_lm)
#2373
model_lm <- lm(hull ~ gs + temp, my_data)
summary(model_lm)
plot(fitted(model_lm),residuals(model_lm))
#r sq = 0.4266
extractAIC(model_lm)
#2371 #best
model_lm <- lm(hull ~ log(gs,2) + temp, my_data)
summary(model_lm)
plot(fitted(model_lm),residuals(model_lm))
#r sq = 0.4261
extractAIC(model_lm)
#2372
model_lm <- lm(hull ~ I(log(gs,2)^2) + temp, my_data)
summary(model_lm)
plot(fitted(model_lm),residuals(model_lm))
#r sq = 0.43
extractAIC(model_lm)
#2376
model_lm <- lm(hull ~ I(gs^2) + temp, my_data)
summary(model_lm)
plot(fitted(model_lm),residuals(model_lm))
#r sq = 0.407
extractAIC(model_lm)
#2394
model_lm <- lm(hull ~ gs + temp + I(temp^2), my_data)
summary(model_lm)
plot(fitted(model_lm),residuals(model_lm))
#r sq = 0.4263
extractAIC(model_lm)
#2372
model_lm <- lm(hull ~ gs + I(temp^2), my_data)
summary(model_lm)
plot(fitted(model_lm),residuals(model_lm))
#r sq = 0.427
extractAIC(model_lm)
#2370
model_lm <- lm(hull ~ gs + I(temp^2) + date, my_data)
summary(model_lm)
plot(fitted(model_lm),residuals(model_lm))
#r sq = 0.4266
extractAIC(model_lm)
#2372
model_lm <- lm(hull ~ gs + I(temp^2) + t, my_data)
summary(model_lm)
plot(fitted(model_lm),residuals(model_lm))
#r sq = 0.4287
extractAIC(model_lm)
#2369
model_lm <- lm(hull ~ gs + I(temp^2) + t + t1, my_data)
summary(model_lm)
plot(fitted(model_lm),residuals(model_lm))
#r sq = 0.428
extractAIC(model_lm)
#2371
model_lm <- lm(hull ~ gs + t, my_data)
summary(model_lm)
plot(fitted(model_lm),residuals(model_lm))
#r sq = 0.4193
extractAIC(model_lm)
#2380
model_lm <- lm(hull ~ gs + temp + t, my_data)
summary(model_lm)
plot(fitted(model_lm),residuals(model_lm))
#r sq = 0.4282
extractAIC(model_lm)
#2370
model_lm <- lm(hull ~ gs + temp, my_data)
summary(model_lm)
plot(fitted(model_lm),residuals(model_lm))
#r sq = 0.4266
extractAIC(model_lm)
#2371 #best
#conclusion - convex hull area during the loom (600-650) is decreasing with increasing temperature.
# About 42% of the variation is explained but most of the variation is explained with group size. Least AIC is one with temp.
## during loom 500 - 700
# make new dataframe
my_data<-data.frame("temp" = data$Temperature[complete.cases(data$convex_hull_area_500_700)],
"gs" = data$Groupsize[complete.cases(data$convex_hull_area_500_700)],
"loom" = data$Loom[complete.cases(data$convex_hull_area_500_700)],
"kt"=1/(0.00008617*(data$Temperature[complete.cases(data$convex_hull_area_500_700)]+273.1)),
"date"=as.numeric(as.Date(data$Date[complete.cases(data$convex_hull_area_500_700)], format = "%d/%m/%Y")),
"trial" = data$Trial[complete.cases(data$convex_hull_area_500_700)],
"subtrial" = data$Subtrial[complete.cases(data$convex_hull_area_500_700)],
"t1" = as.numeric(as.POSIXct(data$Time_fish_in[complete.cases(data$convex_hull_area_500_700)], format = "%H:%M")),
"t2" = as.numeric(as.POSIXct(data$Time_start_record[complete.cases(data$convex_hull_area_500_700)], format = "%H:%M")),
"t" = as.numeric(as.POSIXct(data$Time_start_record[complete.cases(data$convex_hull_area_500_700)], format = "%H:%M"))
- as.numeric(as.POSIXct(data$Time_fish_in[complete.cases(data$convex_hull_area_500_700)], format = "%H:%M")),
"hull" = data$convex_hull_area_500_700[complete.cases(data$convex_hull_area_500_700)]
)
model_lm <- lm(hull ~ gs + I(temp^2) + t, my_data)
summary(model_lm)
plot(fitted(model_lm),residuals(model_lm))
#r sq = 0.4329
extractAIC(model_lm)
#2362
#try glm?
model_glm <- glm(hull ~ gs + I(temp^2) + t, family = Gamma, my_data)
summary(model_glm)
plot(fitted(model_glm),residuals(model_glm)) #residuals are really bad
#r sq = 0.4329
model_lm <- lm(hull ~ gs + temp + t, my_data)
summary(model_lm)
plot(fitted(model_lm),residuals(model_lm))
#r sq = 0.4321
extractAIC(model_lm)
#2363
model_lm <- lm(hull ~ gs + temp , my_data)
summary(model_lm)
plot(fitted(model_lm),residuals(model_lm))
#r sq = 0.431
extractAIC(model_lm)
#2363 #best
#prediction for 600 - 650
my_data<-data.frame("temp" = data$Temperature[complete.cases(data$convex_hull_area_600_650)],
"gs" = data$Groupsize[complete.cases(data$convex_hull_area_600_650)],
"loom" = data$Loom[complete.cases(data$convex_hull_area_600_650)],
"kt"=1/(0.00008617*(data$Temperature[complete.cases(data$convex_hull_area_600_650)]+273.1)),
"date"=as.numeric(as.Date(data$Date[complete.cases(data$convex_hull_area_600_650)], format = "%d/%m/%Y")),
"trial" = data$Trial[complete.cases(data$convex_hull_area_600_650)],
"subtrial" = data$Subtrial[complete.cases(data$convex_hull_area_600_650)],
"t1" = as.numeric(as.POSIXct(data$Time_fish_in[complete.cases(data$convex_hull_area_600_650)], format = "%H:%M")),
"t2" = as.numeric(as.POSIXct(data$Time_start_record[complete.cases(data$convex_hull_area_600_650)], format = "%H:%M")),
"t" = as.numeric(as.POSIXct(data$Time_start_record[complete.cases(data$convex_hull_area_600_650)], format = "%H:%M"))
- as.numeric(as.POSIXct(data$Time_fish_in[complete.cases(data$convex_hull_area_600_650)], format = "%H:%M")),
"hull" = data$convex_hull_area_600_650[complete.cases(data$convex_hull_area_600_650)]
)
#predictions
model_lm <- lm(hull ~ gs + temp , my_data)
summary(model_lm)
plot(fitted(model_lm),residuals(model_lm))
#r sq = 0.4266
extractAIC(model_lm)
#2371 #best
newData1 <- data.frame(expand.grid(temp = seq(from = 9, to = 29, by = 1),
gs = c(4,8,16)))
boots <- 10000
yest <- matrix(NA,nrow=nrow(newData1),ncol=boots)
for(i in 1:boots){
ynew <- unlist(simulate(model_lm))
ymod <- update(model_lm,ynew ~ .)
yest[,i] <- predict(ymod,newdata = newData1, type="response")
}
results <- matrix(NA,nrow=nrow(newData1),ncol=3)
results[,1] <- predict(model_lm,newData1, type = "response")
for(j in 1:nrow(newData1)){
results[j,2] <- quantile(yest[j,],probs = c(0.025))
results[j,3] <- quantile(yest[j,],probs = c(0.975))
}
newData1$hull <- results[,1]
newData1$hull025 <- results[,2]
newData1$hull975 <- results[,3]
write.csv(newData1,"/home/maria/Documents/data/temp_collective/roi/hull_during_loom_predictions_600_650.csv")
#prediction for 500-700
my_data<-data.frame("temp" = data$Temperature[complete.cases(data$convex_hull_area_500_700)],
"gs" = data$Groupsize[complete.cases(data$convex_hull_area_500_700)],
"loom" = data$Loom[complete.cases(data$convex_hull_area_500_700)],
"kt"=1/(0.00008617*(data$Temperature[complete.cases(data$convex_hull_area_500_700)]+273.1)),
"date"=as.numeric(as.Date(data$Date[complete.cases(data$convex_hull_area_500_700)], format = "%d/%m/%Y")),
"trial" = data$Trial[complete.cases(data$convex_hull_area_500_700)],
"subtrial" = data$Subtrial[complete.cases(data$convex_hull_area_500_700)],
"t1" = as.numeric(as.POSIXct(data$Time_fish_in[complete.cases(data$convex_hull_area_500_700)], format = "%H:%M")),
"t2" = as.numeric(as.POSIXct(data$Time_start_record[complete.cases(data$convex_hull_area_500_700)], format = "%H:%M")),
"t" = as.numeric(as.POSIXct(data$Time_start_record[complete.cases(data$convex_hull_area_500_700)], format = "%H:%M"))
- as.numeric(as.POSIXct(data$Time_fish_in[complete.cases(data$convex_hull_area_500_700)], format = "%H:%M")),
"hull" = data$convex_hull_area_500_700[complete.cases(data$convex_hull_area_500_700)]
)
model_lm <- lm(hull ~ gs + temp , my_data)
summary(model_lm)
plot(fitted(model_lm),residuals(model_lm))
#r sq = 0.431
extractAIC(model_lm)
#2363 #best
newData1 <- data.frame(expand.grid(temp = seq(from = 9, to = 29, by = 1),
gs = c(4,8,16)))
boots <- 10000
yest <- matrix(NA,nrow=nrow(newData1),ncol=boots)
for(i in 1:boots){
ynew <- unlist(simulate(model_lm))
ymod <- update(model_lm,ynew ~ .)
yest[,i] <- predict(ymod,newdata = newData1, type="response")
}
results <- matrix(NA,nrow=nrow(newData1),ncol=3)
results[,1] <- predict(model_lm,newData1, type = "response")
for(j in 1:nrow(newData1)){
results[j,2] <- quantile(yest[j,],probs = c(0.025))
results[j,3] <- quantile(yest[j,],probs = c(0.975))
}
newData1$hull <- results[,1]
newData1$hull025 <- results[,2]
newData1$hull975 <- results[,3]
write.csv(newData1,"/home/maria/Documents/data/temp_collective/roi/hull_during_loom_predictions_500_700.csv")