-
Notifications
You must be signed in to change notification settings - Fork 0
/
speed_small_gs.R
483 lines (385 loc) · 16 KB
/
speed_small_gs.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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
#goal - stats on speed before loom only for small group sizes.
data <- read.csv("../../data/temp_collective/roi/all_params_wo_loom.csv",
header=TRUE,na.strings=c("[nan]"))
my_data<-data.frame("temp" = data$Temperature[complete.cases(data$avg_speed)],
"gs" = data$Groupsize[complete.cases(data$avg_speed)],
"kt"=1/(0.00008617*(data$Temperature[complete.cases(data$avg_speed)]+273.1)),
"date"=as.numeric(as.Date(data$Date[complete.cases(data$avg_speed)], format = "%d/%m/%Y")),
"trial" = data$Trial[complete.cases(data$avg_speed)],
"subtrial" = data$Subtrial[complete.cases(data$avg_speed)],
"t1" = as.numeric(as.POSIXct(data$Time_fish_in[complete.cases(data$avg_speed)], format = "%H:%M")),
"t2" = as.numeric(as.POSIXct(data$Time_start_record[complete.cases(data$avg_speed)], format = "%H:%M")),
"speed" = data$avg_speed[complete.cases(data$avg_speed)]
)
hist(my_data$speed,20)
hist(log(my_data$speed),20)
my_data1 <-data.frame("temp" = data$Temperature[data$Groupsize == 1],
"gs" = data$Groupsize[data$Groupsize == 1],
"kt"=1/(0.00008617*(data$Temperature[data$Groupsize == 1]+273.1)),
"date"=as.numeric(as.Date(data$Date[data$Groupsize == 1], format = "%d/%m/%Y")),
"trial" = data$Trial[data$Groupsize == 1],
"subtrial" = data$Subtrial[data$Groupsize == 1],
"t1" = as.numeric(as.POSIXct(data$Time_fish_in[data$Groupsize == 1], format = "%H:%M")),
"t2" = as.numeric(as.POSIXct(data$Time_start_record[data$Groupsize == 1], format = "%H:%M")),
"speed" = data$avg_speed[data$Groupsize == 1]
)
#linear model with all gs
model_lm <- lm((speed)^0.5 ~ temp + I(temp^2) + log(gs,2) + I(log(gs,2)^2),my_data)
summary(model_lm)
plot(fitted(model_lm),residuals(model_lm))
#r sq =0.12
qqnorm(residuals(model_lm), main= "")
qqline(residuals(model_lm))
#linear model with all gs -log transformation
model_lm <- lm(log(speed+1) ~ temp + I(temp^2) + log(gs,2) + I(log(gs,2)^2),my_data)
summary(model_lm)
plot(fitted(model_lm),residuals(model_lm))
#r sq =0.12
qqnorm(residuals(model_lm), main= "")
qqline(residuals(model_lm))
#linear model with all gs -log transformation + interaction
model_lm <- lm(log(speed+1) ~ temp*log(gs,2) + I(temp^2) + I(log(gs,2)^2),my_data)
summary(model_lm)
plot(fitted(model_lm),residuals(model_lm))
#r sq =0.05
qqnorm(residuals(model_lm), main= "")
qqline(residuals(model_lm))
#linear model with all gs -log transformation + interaction
model_lm <- lm(log(speed+1) ~ temp + I(temp^2)*log(gs,2) + I(log(gs,2)^2),my_data)
summary(model_lm)
plot(fitted(model_lm),residuals(model_lm))
#r sq =0.05
qqnorm(residuals(model_lm), main= "")
qqline(residuals(model_lm))
#linear model with all gs -log transformation + interaction
model_lm <- lm(log(speed+1) ~ temp + log(gs,2) + I(temp^2)*I(log(gs,2)^2),my_data)
summary(model_lm)
plot(fitted(model_lm),residuals(model_lm))
#r sq =0.05
qqnorm(residuals(model_lm), main= "")
qqline(residuals(model_lm))
#linear model with all gs -log transformation + date
model_lm <- lm(log(speed+1) ~ temp*log(gs,2) + I(temp^2) + I(log(gs,2)^2) + date ,my_data)
summary(model_lm)
plot(fitted(model_lm),residuals(model_lm))
#r sq =0.05
qqnorm(residuals(model_lm), main= "")
qqline(residuals(model_lm))
#linear model with all gs -log transformation + date
model_lm <- lm(log(speed+1) ~ temp*log(gs,2) + date ,my_data)
summary(model_lm)
plot(fitted(model_lm),residuals(model_lm))
#r sq =0.0426
qqnorm(residuals(model_lm), main= "")
qqline(residuals(model_lm))
#linear model with all gs -log transformation + all variables
model_lm <- lm(log(speed+1) ~ temp*log(gs,2) + date + trial + t1,my_data)
summary(model_lm)
plot(fitted(model_lm),residuals(model_lm))
#r sq =0.035
qqnorm(residuals(model_lm), main= "")
qqline(residuals(model_lm))
#linear model with all gs -log transformation + all variables
model_lm <- lm(log(speed+1) ~ temp*log(gs,2) + date + trial + t1 + t2 + subtrial,my_data)
summary(model_lm)
plot(fitted(model_lm),residuals(model_lm))
#r sq =0.04185
qqnorm(residuals(model_lm), main= "")
qqline(residuals(model_lm))
#### for gs = 1 ######
######################
#linear model with gs 1 -log transformation + all variables
model_lm <- lm(log(speed+1) ~ temp + date + trial + t1 + t2 + subtrial ,my_data1)
summary(model_lm)
plot(fitted(model_lm),residuals(model_lm))
#r sq =0.095
qqnorm(residuals(model_lm), main= "")
qqline(residuals(model_lm))
#linear model with gs 1 -log transformation + date
model_lm <- lm(log(speed+1) ~ temp + date + t1 ,my_data1)
summary(model_lm)
plot(fitted(model_lm),residuals(model_lm))
#r sq =0.13
qqnorm(residuals(model_lm), main= "")
qqline(residuals(model_lm))
#### gs = 2 #########
#####################
my_data2 <-data.frame("temp" = data$Temperature[data$Groupsize == 2],
"gs" = data$Groupsize[data$Groupsize == 2],
"kt"=1/(0.00008617*(data$Temperature[data$Groupsize == 2]+273.1)),
"date"=as.numeric(as.Date(data$Date[data$Groupsize == 2], format = "%d/%m/%Y")),
"trial" = data$Trial[data$Groupsize == 2],
"subtrial" = data$Subtrial[data$Groupsize == 2],
"t1" = as.numeric(as.POSIXct(data$Time_fish_in[data$Groupsize == 2], format = "%H:%M")),
"t2" = as.numeric(as.POSIXct(data$Time_start_record[data$Groupsize == 2], format = "%H:%M")),
"speed" = data$avg_speed[data$Groupsize == 2]
)
model_lm <- lm(log(speed+1) ~ temp + date + t1 ,my_data2)
summary(model_lm)
plot(fitted(model_lm),residuals(model_lm))
#r sq =0.2451
qqnorm(residuals(model_lm), main= "")
qqline(residuals(model_lm))
model_lm <- lm(log(speed+1) ~ temp,my_data2)
summary(model_lm)
plot(fitted(model_lm),residuals(model_lm))
#r sq =0.1252
qqnorm(residuals(model_lm), main= "")
qqline(residuals(model_lm))
# with temp sq
model_lm <- lm(log(speed+1) ~ temp + I(temp^2),my_data2)
summary(model_lm)
plot(fitted(model_lm),residuals(model_lm))
#r sq =0.1085
qqnorm(residuals(model_lm), main= "")
qqline(residuals(model_lm))
# neither significant
# with temp sq and other variables
model_lm <- lm(log(speed+1) ~ temp + I(temp^2) + date + trial + t1,my_data2)
summary(model_lm)
plot(fitted(model_lm),residuals(model_lm))
#r sq =0.2387
qqnorm(residuals(model_lm), main= "")
qqline(residuals(model_lm))
# neither significant
model_lm <- lm(log(speed+1) ~ temp + date + trial + t1,my_data2)
summary(model_lm)
plot(fitted(model_lm),residuals(model_lm))
#r sq =0.2486
qqnorm(residuals(model_lm), main= "")
qqline(residuals(model_lm))
# temp significant
model_lm <- lm(log(speed+1) ~ temp + date + t1,my_data2)
summary(model_lm)
plot(fitted(model_lm),residuals(model_lm))
#r sq =0.2451
qqnorm(residuals(model_lm), main= "")
qqline(residuals(model_lm))
# temp and date significant
model_lm <- lm(log(speed+1) ~ I(temp^2) + date + t1,my_data2)
summary(model_lm)
plot(fitted(model_lm),residuals(model_lm))
#r sq =0.2535
qqnorm(residuals(model_lm), main= "")
qqline(residuals(model_lm))
# temp sq and date significant
# best maybe?
model_lm <- lm(log(speed+1) ~ date + t1,my_data2)
summary(model_lm)
plot(fitted(model_lm),residuals(model_lm))
#r sq =0.1581
qqnorm(residuals(model_lm), main= "")
qqline(residuals(model_lm))
#date significant
#predictions
newData1<- data.frame(expand.grid(temp = seq(from = 9, to = 29, by = 1),
date = unique(my_data2$date), t1 = unique(my_data2$t1)))
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$avg_speed <- results[,1]
newData1$avg_speed025 <- results[,2]
newData1$avg_speed975 <- results[,3]
write.csv(newData1,"/home/maria/Documents/data/temp_collective/roi/avg_speed_before_loom_predictions.csv")
#t1 - 1616979600, date 18110
intersection <- intersect(which(newData1$t1 == 1616979600),which(newData1$date == 18110))
temp <- newData1$temp[intersection]
plot(my_data2$temp, my_data2$speed)
lines(temp, exp(results[intersection,1])-1,lty = "solid")
lines(temp, exp(results[intersection,2])-1,lty = "dashed")
lines(temp, exp(results[intersection,3])-1,lty = "dashed")
##################### try model with all the gs for all the speed variable ################3
###### avg speed ############
my_data<-data.frame("temp" = data$Temperature[complete.cases(data$avg_speed)],
"gs" = data$Groupsize[complete.cases(data$avg_speed)],
"kt"=1/(0.00008617*(data$Temperature[complete.cases(data$avg_speed)]+273.1)),
"date"=as.numeric(as.Date(data$Date[complete.cases(data$avg_speed)], format = "%d/%m/%Y")),
"trial" = data$Trial[complete.cases(data$avg_speed)],
"subtrial" = data$Subtrial[complete.cases(data$avg_speed)],
"t1" = as.numeric(as.POSIXct(data$Time_fish_in[complete.cases(data$avg_speed)], format = "%H:%M")),
"t2" = as.numeric(as.POSIXct(data$Time_start_record[complete.cases(data$avg_speed)], format = "%H:%M")),
"speed" = data$avg_speed[complete.cases(data$avg_speed)]
)
hist(my_data$speed,20)
hist(log(my_data$speed),20)
model_lm <- lm(log(speed+1) ~ date + t1,my_data)
summary(model_lm)
plot(fitted(model_lm),residuals(model_lm))
#r sq =-0.0002 ??
qqnorm(residuals(model_lm), main= "")
qqline(residuals(model_lm))
# add temp
model_lm <- lm(log(speed+1) ~ temp + date + t1,my_data)
summary(model_lm)
plot(fitted(model_lm),residuals(model_lm))
#r sq =0.039
qqnorm(residuals(model_lm), main= "")
qqline(residuals(model_lm))
# replace with temp^2
model_lm <- lm(log(speed+1) ~ I(temp^2) + date + t1,my_data)
summary(model_lm)
plot(fitted(model_lm),residuals(model_lm))
#r sq =0.033
qqnorm(residuals(model_lm), main= "")
qqline(residuals(model_lm))
extractAIC(model_lm) #-831
# add gs to temp
model_lm <- lm(log(speed+1) ~ gs + temp + date + t1,my_data)
summary(model_lm)
plot(fitted(model_lm),residuals(model_lm))
#r sq =0.038
qqnorm(residuals(model_lm), main= "")
qqline(residuals(model_lm))
# add log(gs) to temp
model_lm <- lm(log(speed+1) ~ log(gs,2) + temp + date + t1,my_data)
summary(model_lm)
plot(fitted(model_lm),residuals(model_lm))
#r sq =0.036
qqnorm(residuals(model_lm), main= "")
qqline(residuals(model_lm))
# add interaction between log(gs) and temp
model_lm <- lm(log(speed+1) ~ log(gs,2)*temp + date + t1,my_data)
summary(model_lm)
plot(fitted(model_lm),residuals(model_lm))
#r sq =0.038
qqnorm(residuals(model_lm), main= "")
qqline(residuals(model_lm))
#interaction not sigificant
model_lm <- lm(log(speed+1) ~ temp,my_data)
summary(model_lm)
plot(fitted(model_lm),residuals(model_lm))
#r sq =0.04
extractAIC(model_lm) #-834
model_lm <- lm(log(speed+1) ~ I(temp^2),my_data)
summary(model_lm)
plot(fitted(model_lm),residuals(model_lm))
#r sq =0.04
extractAIC(model_lm) #-833
# only 99th percentile of speed
my_data<-data.frame("temp" = data$Temperature[complete.cases(data$speed_percentile99)],
"gs" = data$Groupsize[complete.cases(data$speed_percentile99)],
"kt"=1/(0.00008617*(data$Temperature[complete.cases(data$speed_percentile99)]+273.1)),
"date"=as.numeric(as.Date(data$Date[complete.cases(data$speed_percentile99)], format = "%d/%m/%Y")),
"trial" = data$Trial[complete.cases(data$speed_percentile99)],
"subtrial" = data$Subtrial[complete.cases(data$speed_percentile99)],
"t1" = as.numeric(as.POSIXct(data$Time_fish_in[complete.cases(data$speed_percentile99)], format = "%H:%M")),
"t2" = as.numeric(as.POSIXct(data$Time_start_record[complete.cases(data$speed_percentile99)], format = "%H:%M")),
"speed" = data$speed_percentile99[complete.cases(data$speed_percentile99)]
)
hist(my_data$speed,20)
hist(log(my_data$speed),20)
# add interaction between log(gs) and temp
model_lm <- lm(log(speed+1) ~ log(gs,2)*temp + date + t1,my_data)
summary(model_lm)
plot(fitted(model_lm),residuals(model_lm))
#r sq =0.099
qqnorm(residuals(model_lm), main= "")
qqline(residuals(model_lm))
#interaction not sigificant
#this is best
# add interaction between log(gs) and temp^2
model_lm <- lm(log(speed+1) ~ log(gs,2)*I(temp^2) + date + t1,my_data)
summary(model_lm)
plot(fitted(model_lm),residuals(model_lm))
#r sq =0.08574
qqnorm(residuals(model_lm), main= "")
qqline(residuals(model_lm))
#interaction not sigificant
extractAIC(model_lm)
#use kt instead
model_lm <- lm(log(speed+1) ~ log(gs,2)*kt + date + t1,my_data)
summary(model_lm)
plot(fitted(model_lm),residuals(model_lm))
#r sq =0.1013
qqnorm(residuals(model_lm), main= "")
qqline(residuals(model_lm))
#interaction not sigificant
#this is best
model_lm <- lm(log(speed+1) ~ kt,my_data)
summary(model_lm)
plot(fitted(model_lm),residuals(model_lm))
#r sq =0.1141
qqnorm(residuals(model_lm), main= "")
qqline(residuals(model_lm))
extractAIC(model_lm)
#interaction not sigificant
#this is best
## predictions for 99th percentile of speed with temp model
# add interaction between log(gs) and temp
model_lm <- lm(log(speed+1) ~ temp,my_data)
summary(model_lm)
plot(fitted(model_lm),residuals(model_lm))
#r sq =0.1127
qqnorm(residuals(model_lm), main= "")
qqline(residuals(model_lm))
extractAIC(model_lm)
#this is best
newData1<- data.frame(expand.grid(temp = seq(from = 9, to = 29, by = 1)))
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$speed99 <- results[,1]
newData1$speed99_025 <- results[,2]
newData1$speed99_975 <- results[,3]
write.csv(newData1,"/home/maria/Documents/data/temp_collective/roi/speed99_before_loom_predictions.csv")
#t1 - 1616979600, date 18110
#intersection <- intersect(which(newData1$t1 == 1616979600),which(newData1$date == 18110))
temp <- newData1$temp
plot(my_data$temp, my_data$speed)
lines(temp, exp(results[,1])-1,lty = "solid")
lines(temp, exp(results[,2])-1,lty = "dashed")
lines(temp, exp(results[,3])-1,lty = "dashed")
## predictions for 99th percentile of speed with kt model
# add interaction between log(gs) and temp
model_lm <- lm(log(speed+1) ~ kt,my_data)
summary(model_lm)
plot(fitted(model_lm),residuals(model_lm))
#r sq =0.1141
qqnorm(residuals(model_lm), main= "")
qqline(residuals(model_lm))
#this is best
newData1<- data.frame(expand.grid(kt = seq(from = 38, to = 42, by = 0.1)))
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$speed99 <- results[,1]
newData1$speed99_025 <- results[,2]
newData1$speed99_975 <- results[,3]
write.csv(newData1,"/home/maria/Documents/data/temp_collective/roi/speed99_kt_before_loom_predictions.csv")
#t1 - 1616979600, date 18110
#intersection <- intersect(which(newData1$t1 == 1616979600),which(newData1$date == 18110))
temp <- 1/(newData1$kt*0.00008617) - 273.1
plot(my_data$temp, my_data$speed)
lines(temp, exp(results[,1])-1,lty = "solid")
lines(temp, exp(results[,2])-1,lty = "dashed")
lines(temp, exp(results[,3])-1,lty = "dashed")