-
Notifications
You must be signed in to change notification settings - Fork 0
/
12_sensitivity_model-selection_range.R
617 lines (512 loc) · 24.4 KB
/
12_sensitivity_model-selection_range.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
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
## creating null model for niche filling in geographic space
library(tidyverse)
library(sf)
library(raster)
library(nlme)
library(MuMIn)
library(car)
select <- dplyr::select
##~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~##
##### Calculate proportion of potential range that is occupied #####
##~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~##
## read in results:
rf <- read.csv("data-processed/potential-ranges/range-filling/rangefilling-metrics_model-ready.csv")
nf <- read.csv("data-processed/thermal-niches/niche-filling/thermal-niche-filling-metrics_model-ready.csv")
## calculate the proportion of occupied cells in the potential range
rf$prop_occupied <- (rf$pr_cells - rf$u_cells) / rf$pr_cells
rf$log_prop_occupied <- log(rf$prop_occupied)
rf <- filter(rf, !is.infinite(log_prop_occupied))
## re-order factors to give desired contrasts
rf$realm <- relevel(factor(rf$realm), ref = "Terrestrial")
## calculate difference between proportion of cells underfilled in poleward versus equatorward range half
rf$bias_in_uf <- (1- rf$equ_fill) - (1-rf$pol_fill)
## split by type:
types <- group_split(rf, type)
acc <- types[[1]]
te <- types[[2]]
te_subset_acc <- filter(te, te$species %in% acc$species)
acc <- filter(acc, acc$species %in% te_subset_acc$species)
## give priority to IUCN ranges, then GARD, then GBIF
te_subset_acc <- te_subset_acc %>%
select(range, species, source, everything()) %>%
mutate(source = factor(.$source, levels = c("IUCN", "GARD", "GBIF"), ordered = TRUE)) %>%
arrange(species, type, source) %>%
filter(!duplicated(species))
acc <- acc %>%
select(range, species, source, everything()) %>%
mutate(source = factor(.$source, levels = c("IUCN", "GARD", "GBIF"), ordered = TRUE)) %>%
arrange(species, type, source) %>%
filter(!duplicated(species))
## add traits
traits <- read.csv("data-processed/traits/rangetherm-traits_all-spp.csv") %>%
rename("realm" = Realm) %>%
select(-limit_type) %>%
mutate(species = paste(Genus, Species, sep = "_"))
te_subset_acc = left_join(te_subset_acc, traits)
acc = left_join(acc, traits)
##~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~##
##### Subset WITH adjustment #####
##~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~##
#check for multicolinearity among variables (in a linear model)
modvif <- lm(log_prop_occupied ~ abs_lat_mp + realm +
dispersal_distance_continuous +
log_maximum_body_size,
data = acc)
vif(modvif)
## removed: dispersal_ability_category, metric, aliased
# get complete cases
##~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~##
## select only columns we care about:
acc <- select(acc, c(log_prop_occupied, abs_lat_mp, realm,
dispersal_distance_continuous,
log_maximum_body_size, rr_cells, metric,
Class, Order, Family, Genus, Species, range, bias_in_uf))
# get complete cases
acc <- subset(acc, complete.cases(acc))
dim(acc)
saveRDS(acc, "data-processed/potential-ranges/range-filling/range-complete-cases_unscaled_acc.rds")
## re-order factors to give desired contrasts
acc$realm <- relevel(factor(acc$realm), ref = "Terrestrial")
#Second, rescale variables and get complete cases
##~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~##
uf <- acc
# rescale the continuous variables
uf = as.data.frame(uf)
ufnorm <- uf
for (i in c(2,4,5)) {
ufnorm[,i] <- (uf[,i]-mean(c(uf[,i]), na.rm = T))/sd(uf[,i], na.rm = T)
}
dim(ufnorm)
uf = ufnorm
#Now, run the full models and check out the residuals:
##~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~##
#run full lme model
model_uf <- lme(log_prop_occupied ~ abs_lat_mp*realm + dispersal_distance_continuous +
log_maximum_body_size,
random = ~1|Class/Order/Family/Genus,
data = uf
)
#check residuals
hist(resid(model_uf))
plot(model_uf)
#against predictors:
E1 <- resid(model_uf)
plot(E1 ~ abs_lat_mp, data = uf)
plot(E1 ~ log_range_area, data = uf)
plot(E1 ~ realm, data = uf)
plot(E1 ~ dispersal_distance_continuous, data = uf)
plot(E1 ~ log_maximum_body_size, data = uf)
## check for heteroskedasticity
library(lmtest)
bptest(model_uf)
#Now, fit all the models using dredge, select the confidence set and average the models:
##~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~##
#fit all models
suppressWarnings(allmodels_uf <- dredge(model_uf, extra="R^2"))
confset.95p_uf <- get.models(allmodels_uf, subset = cumsum(weight)<=.95) #get confidence set
avgm_uf <- model.avg(confset.95p_uf) #do averaging
summary(avgm_uf)
##~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~##
##### Exporting tables ######
##~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~##
library(htmlTable)
## make data frame of details I want to include in our table:
sum <- summary(avgm_uf)
sum_warm <- sum$coefmat.full
eff_type <- c("intercept", "slope", "slope", "slope", "slope", "slope",
"intercept","intercept")
coefs <- sum_warm[c(1,4,5,6,7,8,2,3),1] # reorder
f_effects <- c("(Intercept)",
"Abs. realized range latitudinal midpoint",
"Abs. realized range latitudinal midpoint x realm: subtidal",
"Abs. realized range latitudinal midpoint x realm: intertidal",
"Dispersal distance (km)",
"Maximum body size (log cm)",
"Realm: intertidal",
"Realm: marine")
std_err <- sum_warm[c(1,4,5,6,7,8,2,3),2] # reorder fixed effects
z_val <- sum_warm[c(1,4,5,6,7,8,2,3),4]
p_val <- sum_warm[c(1,4,5,6,7,8,2,3),5]
names(coefs) <- names(std_err) <- names(z_val) <- names(p_val) <- NULL
## put all into a table:
results <- data.frame("fixed effects" = f_effects,
"effect type" = eff_type,
"estimate" = coefs,
"s.e." = std_err,
"z-value" = z_val,
"p-value" = p_val)
colnames(results) = c("fixed effects", "effect type", "estimate", "s.e.",'z-value', "p-value")
results[c(c(3,4,5))] <- round(results[,c(3,4,5)], digits = 2) # round estimates to 2 decimal places
results[6] <- round(results[,6], digits = 3) # round p values to 3 decimal places
## add significance indicators
results$`p-value` <- ifelse(results$`p-value` == 0, "<0.001 **",
ifelse(results$`p-value` <= 0.01, paste(results$`p-value`, " **", sep= ""),
ifelse(results$`p-value` <= 0.05, paste(results$`p-value`, " *", sep= ""),
ifelse(results$`p-value` <= 0.1, paste(results$`p-value`, " ", sep= ""),
as.character(results$`p-value`)))))
table <- results %>%
addHtmlTableStyle(col.rgroup = c("none", "#F7F7F7")) %>%
htmlTable(., rnames = rep("", 8))
## save model for whisker plot:
avgm_uf_acc <- avgm_uf
##~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~##
##### Subset WITHOUT adjustment ######
##~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~##
#check for multicolinearity among variables (in a linear model)
modvif <- lm(log_prop_occupied ~ abs_lat_mp + realm +
dispersal_distance_continuous +
log_maximum_body_size,
data = te_subset_acc)
vif(modvif)
## removed: dispersal_ability_category, metric, aliased
# get complete cases
##~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~##
## select only columns we care about:
te_subset_acc <- select(te_subset_acc, c(log_prop_occupied, abs_lat_mp, realm,
dispersal_distance_continuous,
log_maximum_body_size, rr_cells, metric,
Class, Order, Family, Genus, Species, range, bias_in_uf))
# get complete cases
te_subset_acc <- subset(te_subset_acc, complete.cases(te_subset_acc))
dim(te_subset_acc)
## re-order factors to give desired contrasts
te_subset_acc$realm <- relevel(factor(te_subset_acc$realm), ref = "Terrestrial")
#Second, rescale variables and get complete cases
##~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~##
uf <- te_subset_acc
# rescale the continuous variables
uf = as.data.frame(uf)
ufnorm <- uf
for (i in c(2,4,5)) {
ufnorm[,i] <- (uf[,i]-mean(c(uf[,i]), na.rm = T))/sd(uf[,i], na.rm = T)
}
dim(ufnorm)
uf = ufnorm
#Now, run the full models and check out the residuals:
##~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~##
#run full lme model
model_uf <- lme(log_prop_occupied ~ abs_lat_mp*realm + dispersal_distance_continuous +
log_maximum_body_size,
random = ~1|Class/Order/Family/Genus,
data = uf
)
#check residuals
hist(resid(model_uf))
plot(model_uf)
#against predictors:
E1 <- resid(model_uf)
plot(E1 ~ abs_lat_mp, data = uf)
plot(E1 ~ log_range_area, data = uf)
plot(E1 ~ realm, data = uf)
plot(E1 ~ dispersal_distance_continuous, data = uf)
plot(E1 ~ log_maximum_body_size, data = uf)
## check for heteroskedasticity
library(lmtest)
bptest(model_uf)
#Now, fit all the models using dredge, select the confidence set and average the models:
##~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~##
#fit all models
suppressWarnings(allmodels_uf <- dredge(model_uf, extra="R^2"))
confset.95p_uf <- get.models(allmodels_uf, subset = cumsum(weight)<=.95) #get confidence set
avgm_uf <- model.avg(confset.95p_uf) #do averaging
summary(avgm_uf)
##~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~##
##### Exporting tables ######
##~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~##
library(htmlTable)
## make data frame of details I want to include in our table:
sum <- summary(avgm_uf)
sum_warm <- sum$coefmat.full
eff_type <- c("intercept", "slope", "slope", "slope", "slope", "slope",
"intercept","intercept")
coefs <- sum_warm[c(1,4,5,6,7,8,2,3),1] # reorder
f_effects <- c("(Intercept)",
"Abs. realized range latitudinal midpoint",
"Abs. realized range latitudinal midpoint x realm: subtidal",
"Abs. realized range latitudinal midpoint x realm: intertidal",
"Dispersal distance (km)",
"Maximum body size (log cm)",
"Realm: intertidal",
"Realm: marine")
std_err <- sum_warm[c(1,4,5,6,7,8,2,3),2] # reorder fixed effects
z_val <- sum_warm[c(1,4,5,6,7,8,2,3),4]
p_val <- sum_warm[c(1,4,5,6,7,8,2,3),5]
names(coefs) <- names(std_err) <- names(z_val) <- names(p_val) <- NULL
## put all into a table:
results <- data.frame("fixed effects" = f_effects,
"effect type" = eff_type,
"estimate" = coefs,
"s.e." = std_err,
"z-value" = z_val,
"p-value" = p_val)
colnames(results) = c("fixed effects", "effect type", "estimate", "s.e.",'z-value', "p-value")
results[c(c(3,4,5))] <- round(results[,c(3,4,5)], digits = 2) # round estimates to 2 decimal places
results[6] <- round(results[,6], digits = 3) # round p values to 3 decimal places
## add significance indicators
results$`p-value` <- ifelse(results$`p-value` == 0, "<0.001 **",
ifelse(results$`p-value` <= 0.01, paste(results$`p-value`, " **", sep= ""),
ifelse(results$`p-value` <= 0.05, paste(results$`p-value`, " *", sep= ""),
ifelse(results$`p-value` <= 0.1, paste(results$`p-value`, " ", sep= ""),
as.character(results$`p-value`)))))
table <- results %>%
addHtmlTableStyle(col.rgroup = c("none", "#F7F7F7")) %>%
htmlTable(., rname = rep("", 8))
## save model for whisker plot:
avgm_uf_no_acc <- avgm_uf
##~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~##
##### Making whisker plots #####
##~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~##
library(dotwhisker)
## acclimatization vs. no acclimatization:
acc_uf <- list(avgm_uf_acc, avgm_uf_no_acc)
names(acc_uf) <- c("Acclimatization", "No acclimatization")
acc_uf_dw <- dwplot(acc_uf,
vline = geom_vline(xintercept = 0, colour = "grey50"),
show_intercept = TRUE) +
scale_y_discrete(labels = c("Maximum body size (log cm)",
"Dispersal distance (km)",
"Abs. realized range latitudinal midpoint x realm: subtidal",
"Abs. realized range latitudinal midpoint x realm: intertidal",
"Abs. realized range latitudinal midpoint",
"Realm: subtidal",
"Realm: intertidal",
"Reference")) +
labs(colour = "", x = "Effect of variable on range filling") +
theme_light() +
scale_colour_manual(values = c("grey", "black")) +
theme(legend.text = element_text(size = 8), legend.key.size = unit(0.75, 'lines'),
axis.text.y = element_text(size = 8), legend.position = "bottom",
panel.grid = element_blank()) +
scale_x_continuous(limits = c(-5, 6.25))
ggsave(acc_uf_dw, width = 6, height = 3, path = "figures/extended-data",
filename = "whisker-plot_acclimatization_range.png",
device = "png")
saveRDS(acc_uf_dw, "data-processed/intermediate-files/whisker_acc_range.rds")
## combine with other acc plots:
acc_cold_dw <- readRDS("data-processed/intermediate-files/whisker_acc_cold.rds")
acc_warm_dw <- readRDS("data-processed/intermediate-files/whisker_acc_warm.rds")
acc_cold_dw <- acc_cold_dw + theme(legend.position = "none")
acc_warm_dw <- acc_warm_dw + theme(legend.position = "none")
acc_uf_dw <- acc_uf_dw + theme(legend.position = "none")
acc_dw <- ggdraw() +
draw_plot(acc_warm_dw, x = 0, y = 0.63, width = 1, height = 0.37) +
draw_plot(acc_cold_dw, x = 0, y = 0.26, width = 1, height = 0.37) +
draw_plot(acc_uf_dw, x = 0, y = 0, width = 1, height = 0.26) +
draw_plot_label(label = c("b)", "c)", "d)"),
x = c(0, 0,0),
y = c(1, 0.63, 0.26), size = 10,
color = "grey30")
ggsave(acc_dw, width = 6, height = 4.5, path = "figures/extended-data",
filename = "whisker-plot_acclimatization_all.png",
device = "png")
##~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~##
##### Subset WITH adjustment #####
##~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~##
## bias model:
uf = ufnorm
#Now, run the full models and check out the residuals:
##~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~##
#run full lme model
model_asym <- lme(bias_in_uf ~ abs_lat_mp*realm + dispersal_distance_continuous +
log_maximum_body_size,
random = ~1|Class/Order/Family/Genus,
data = uf
)
#check residuals
hist(resid(model_asym))
plot(model_asym)
#against predictors:
E1 <- resid(model_asym)
plot(E1 ~ abs_lat_mp, data = uf)
plot(E1 ~ realm, data = uf)
plot(E1 ~ dispersal_distance_continuous, data = uf)
plot(E1 ~ log_maximum_body_size, data = uf)
## check for heteroskedasticity
library(lmtest)
bptest(model_asym)
#Now, fit all the models using dredge, select the confidence set and average the models:
##~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~##
#fit all models
suppressWarnings(allmodels_asym <- dredge(model_asym, extra="R^2", subset = (`abs_lat_mp:realm`)))
confset.95p_asym <- get.models(allmodels_asym, subset = cumsum(weight)<=.95) #get confidence set
avgm_asym <- model.avg(confset.95p_asym) #do averaging
summary(confset.95p_asym[[1]])
avgm_asym <- confset.95p_asym[[1]]
##~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~##
##### Exporting tables ######
##~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~##
library(htmlTable)
## make data frame of details I want to include in our table:
sum <- summary(avgm_asym)
sum_warm <- sum$coefficients$fixed
eff_type <- c("intercept", "slope", "slope", "slope",
"intercept","intercept")
coefs <- sum_warm[c(1,2,5,6,3,4)] # reorder
f_effects <- c("(Intercept)",
"Abs. realized range latitudinal midpoint",
"Abs. realized range latitudinal midpoint x realm: intertidal",
"Abs. realized range latitudinal midpoint x realm: subtidal",
"Realm: intertidal",
"Realm: subtidal")
ttable <- as.data.frame(sum$tTable)
std_err <- ttable[c(1,2,5,6,3,4),2] # reorder fixed effects
z_val <- ttable[c(1,2,5,6,3,4),4]
p_val <- ttable[c(1,2,5,6,3,4),5]
names(coefs) <- names(std_err) <- names(z_val) <- names(p_val) <- NULL
## put all into a table:
results <- data.frame("fixed effects" = f_effects,
"effect type" = eff_type,
"estimate" = coefs,
"s.e." = std_err,
"z-value" = z_val,
"p-value" = p_val)
colnames(results) = c("fixed effects", "effect type", "estimate", "s.e.",'z-value', "p-value")
results[c(c(3,4,5))] <- round(results[,c(3,4,5)], digits = 2) # round estimates to 2 decimal places
results[6] <- round(results[,6], digits = 3) # round p values to 3 decimal places
## add significance indicators
results$`p-value` <- ifelse(results$`p-value` == 0, "<0.001 **",
ifelse(results$`p-value` <= 0.01, paste(results$`p-value`, " **", sep= ""),
ifelse(results$`p-value` <= 0.05, paste(results$`p-value`, " *", sep= ""),
ifelse(results$`p-value` <= 0.1, paste(results$`p-value`, " ", sep= ""),
as.character(results$`p-value`)))))
table <- results %>%
addHtmlTableStyle(col.rgroup = c("none", "#F7F7F7")) %>%
htmlTable(., rname = rep("", 6))
## save model for whisker plot:
avgm_asym_acc <- avgm_asym
##~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~##
##### Subset WITHOUT adjustment #####
##~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~##
#Now, run the full models and check out the residuals:
##~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~##
#run full lme model
model_asym <- lme(bias_in_uf ~ abs_lat_mp*realm,
random = ~1|Class/Order/Family/Genus,
data = te_subset_acc
)
#check residuals
hist(resid(model_asym))
plot(model_asym)
#against predictors:
E1 <- resid(model_asym)
plot(E1 ~ abs_lat_mp, data = te_subset_acc)
plot(E1 ~ realm, data = te_subset_acc)
plot(E1 ~ dispersal_distance_continuous, data = te_subset_acc)
plot(E1 ~ log_maximum_body_size, data = te_subset_acc)
## check for heteroskedasticity
library(lmtest)
bptest(model_asym)
avgm_asym <- model_asym
##~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~##
##### Exporting tables ######
##~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~##
library(htmlTable)
## make data frame of details I want to include in our table:
sum <- summary(avgm_asym)
sum_warm <- sum$coefficients$fixed
eff_type <- c("intercept", "slope", "slope", "slope",
"intercept","intercept")
coefs <- sum_warm[c(1,2,5,6,3,4)] # reorder
f_effects <- c("(Intercept)",
"Abs. realized range latitudinal midpoint",
"Abs. realized range latitudinal midpoint x realm: intertidal",
"Abs. realized range latitudinal midpoint x realm: subtidal",
"Realm: intertidal",
"Realm: subtidal")
ttable <- as.data.frame(sum$tTable)
std_err <- ttable[c(1,2,5,6,3,4),2] # reorder fixed effects
z_val <- ttable[c(1,2,5,6,3,4),4]
p_val <- ttable[c(1,2,5,6,3,4),5]
names(coefs) <- names(std_err) <- names(z_val) <- names(p_val) <- NULL
## put all into a table:
results <- data.frame("fixed effects" = f_effects,
"effect type" = eff_type,
"estimate" = coefs,
"s.e." = std_err,
"z-value" = z_val,
"p-value" = p_val)
colnames(results) = c("fixed effects", "effect type", "estimate", "s.e.",'z-value', "p-value")
results[c(c(3,4,5))] <- round(results[,c(3,4,5)], digits = 2) # round estimates to 2 decimal places
results[6] <- round(results[,6], digits = 3) # round p values to 3 decimal places
## add significance indicators
results$`p-value` <- ifelse(results$`p-value` == 0, "<0.001 **",
ifelse(results$`p-value` <= 0.01, paste(results$`p-value`, " **", sep= ""),
ifelse(results$`p-value` <= 0.05, paste(results$`p-value`, " *", sep= ""),
ifelse(results$`p-value` <= 0.1, paste(results$`p-value`, " ", sep= ""),
as.character(results$`p-value`)))))
table <- results %>%
addHtmlTableStyle(col.rgroup = c("none", "#F7F7F7")) %>%
htmlTable(., rname = rep("", 6))
## save model for whisker plot:
avgm_asym_no_acc <- avgm_asym
##~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~##
##### Making whisker plots #####
##~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~##
library(dotwhisker)
## acclimatization vs. no acclimatization:
acc_uf <- list(avgm_uf_acc, avgm_uf_no_acc)
names(acc_uf) <- c("Acclimatization", "No acclimatization")
acc_uf_dw <- dwplot(acc_uf,
vline = geom_vline(xintercept = 0, colour = "grey50"),
show_intercept = TRUE) +
scale_y_discrete(labels = c("intertidal Abs. realized range latitudinal midpoint",
"Maximum body size (log cm)",
"Dispersal distance (km)",
"Realm: subtidal",
"Realm: intertidal",
"Reference")) +
labs(colour = "", x = "Effect of variable on range filling") +
theme_light() +
scale_colour_manual(values = c("grey", "black")) +
theme(legend.text = element_text(size = 8), legend.key.size = unit(0.75, 'lines'),
axis.text.y = element_text(size = 8), legend.position = "bottom",
panel.grid = element_blank()) +
scale_x_continuous(limits = c(-5, 6.25))
ggsave(acc_uf_dw, width = 6, height = 3, path = "figures/extended-data",
filename = "whisker-plot_acclimatization_range.png",
device = "png")
saveRDS(acc_uf_dw, "data-processed/intermediate-files/whisker_acc_range.rds")
## bias
## acclimatization vs. no acclimatization:
acc_asym <- list(avgm_asym_acc, avgm_asym_no_acc)
names(acc_asym) <- c("Acclimatization", "No acclimatization")
acc_asym_dw <- dwplot(acc_asym,
vline = geom_vline(xintercept = 0, colour = "grey50"),
show_intercept = TRUE)+
scale_y_discrete(labels = c("SD obs.",
"SD int", "Abs. realized range latitudinal midpoint x realm: subtidal",
"Abs. realized range latitudinal midpoint x realm: intertidal",
"Realm: subtidal",
"Realm: intertidal",
"Abs. realized range latitudinal midpoint",
"Reference")) +
labs(colour = "", x = "Effect of variable on equatorward bias in underfilling") +
theme_light() +
scale_colour_manual(values = c("grey", "black")) +
theme(legend.text = element_text(size = 8), legend.key.size = unit(0.75, 'lines'),
axis.text.y = element_text(size = 8), legend.position = "bottom",
panel.grid = element_blank()) +
scale_x_continuous(limits = c(-0.65, 0.8))
ggsave(acc_asym_dw, width = 6, height = 3, path = "figures/extended-data",
filename = "whisker-plot_acclimatization_asym.png",
device = "png")
saveRDS(acc_asym_dw, "data-processed/intermediate-files/whisker_acc_asym.rds")
## combine whisker plots:
acc_cold_dw <- readRDS("data-processed/intermediate-files/whisker_acc_cold.rds")
acc_warm_dw <- readRDS("data-processed/intermediate-files/whisker_acc_warm.rds")
acc_cold_dw <- acc_cold_dw + theme(legend.position = "none")
acc_warm_dw <- acc_warm_dw + theme(legend.position = "none")
acc_uf_dw <- acc_uf_dw + theme(legend.position = "none")
acc_asym_dw <- acc_asym_dw + theme(legend.position = "none")
acc_dw <- ggdraw() +
draw_plot(acc_warm_dw, x = 0, y = 0.73, width = 1, height = 0.27) +
draw_plot(acc_cold_dw, x = 0, y = 0.46, width = 1, height = 0.27) +
draw_plot(acc_uf_dw, x = 0.07, y = 0.25, width = 0.93, height = 0.21) +
draw_plot(acc_asym_dw, x = 0, y = 0, width = 1, height = 0.25) +
draw_plot_label(label = c("b)", "c)", "d)", "e)"),
x = c(0, 0, 0, 0),
y = c(1, 0.73, 0.46, 0.25), size = 10,
color = "grey30")
ggsave(acc_dw, width = 6.6, height = 6.5, path = "figures/extended-data",
filename = "whisker-plot_acclimatization_all.png",
device = "png")