/
04_ReassessConvergence.R
181 lines (123 loc) · 7.02 KB
/
04_ReassessConvergence.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
#########################################################
## Script for integrated modelling of hoverfly data
## Author: Francesca
## Date created: 2019/02/14
## Date modified: 2020-01-24
########################################################
library(R2jags)
library(dplyr)
library(sparta)
library(ggplot2)
## Assess convergence across all chains ----
source("./Functions/combine_chains_modified.R")
source("./Functions/extract_metrics.R")
dir.create("./Outputs/HRS/csv_results")
dir.create("./Outputs/Integrated/csv_results")
combine_chains(data_dir = "Outputs/HRS",
output_dir = "Outputs/HRS/csv_results",
target = 16666)
combine_chains(data_dir = "Outputs/Integrated",
output_dir = "Outputs/Integrated/csv_results", model_type = "integrated",
target = 16666)
## Extract model results and plot ----
load("./Data/HRS_formatted.rdata")
load("./Data/POMS_formatted.rdata")
hrs_2017_sp_names <- read.csv("./Data/HRS_2017_spNames.csv",
header = TRUE, stringsAsFactors = FALSE)
hov_POMS_2017 <- read.csv("./Data/POMS_2017_5Km.csv",
header = TRUE, stringsAsFactors = FALSE)
species_list <- read.csv("./Data/common_spNames.csv",
header = TRUE, stringsAsFactors = FALSE)
model_outputs <- lapply(species_list$Species[-1], extract_metrics,
path = "./Outputs/")
model_outputs_df <- do.call(rbind, model_outputs)
str(model_outputs_df)
# write.csv(model_outputs_df, "./Outputs/hoverflies_model_metrics.csv", row.names = F)
model_outputs_df <- read.csv("./Output/hoverflies_model_metrics_best_species.csv",
header = T)
str(model_outputs_df)
# subset the results to only keep those that converged
# and plot some main stats
Prec_a <- ggplot(model_outputs_df%>%
filter_at(vars(contains("Rhat_a")), all_vars(.<=1.15))) +
geom_point(aes(x = Prec_a_HRS, y = Prec_a_integrated)) +
geom_smooth(aes(x = Prec_a_HRS, y = Prec_a_integrated),method = "lm", colour = "black") +
geom_abline(intercept = 0, slope = 1,linetype = "dashed") +
coord_equal()
Prec_a_log <- ggplot(model_outputs_df%>%
filter_at(vars(contains("Rhat_a")), all_vars(.<=1.15))) +
geom_point(aes(x = log(Prec_a_HRS), y = log(Prec_a_integrated))) +
stat_smooth(aes(x = log(Prec_a_HRS), y = log(Prec_a_integrated)),method = "lm", colour = "black") +
geom_abline(intercept = 0, slope = 1,linetype = "dashed")
Prec_psi <- ggplot(model_outputs_df%>%
filter_at(vars(contains("Rhat_psi")), all_vars(.<=1.15))) +
geom_point(aes(x = Prec_psi_HRS, y = Prec_psi_integrated)) +
stat_smooth(aes(x = Prec_psi_HRS, y = Prec_psi_integrated),method = "lm") +
geom_abline(intercept = 0, slope = 1,linetype = "dashed") +
coord_equal()
Prec_psi_log <- ggplot(model_outputs_df%>%
filter_at(vars(contains("Rhat_psi")), all_vars(.<=1.15))) +
geom_point(aes(x = log(Prec_psi_HRS), y = log(Prec_psi_integrated))) +
stat_smooth(aes(x = log(Prec_psi_HRS), y = log(Prec_psi_integrated)),method = "lm") +
geom_abline(intercept = 0, slope = 1,linetype = "dashed") +
coord_equal()
Prec_beta1 <- ggplot(model_outputs_df%>%
filter_at(vars(contains("Rhat_beta1")), all_vars(.<=1.15))) +
geom_point(aes(x = Prec_beta1_HRS, y = Prec_beta1_integrated)) +
stat_smooth(aes(x = Prec_beta1_HRS, y = Prec_beta1_integrated),method = "lm") +
geom_abline(intercept = 0, slope = 1,linetype = "dashed")
Prec_beta2 <- ggplot(model_outputs_df%>%
filter_at(vars(contains("Rhat_beta2")), all_vars(.<=1.15))) +
geom_point(aes(x = Prec_beta2_HRS, y = Prec_beta2_integrated)) +
stat_smooth(aes(x = Prec_beta2_HRS, y = Prec_beta2_integrated),method = "lm") +
geom_abline(intercept = 0, slope = 1,linetype = "dashed")
Prec_beta3 <- ggplot(model_outputs_df%>%
filter_at(vars(contains("Rhat_beta3")), all_vars(.<=1.15))) +
geom_point(aes(x = Prec_beta3_HRS, y = Prec_beta3_integrated)) +
stat_smooth(aes(x = Prec_beta3_HRS, y = Prec_beta3_integrated),method = "lm") +
geom_abline(intercept = 0, slope = 1,linetype = "dashed")
source("../Descriptive Stats/multiplot.R")
convergence_psi <- ggplot(model_outputs_df) +
geom_point(aes(x = Rhat_psi_HRS, y = Rhat_psi_integrated)) +
stat_smooth(aes(x = Rhat_psi_HRS, y = Rhat_psi_integrated),method = "lm") +
geom_abline(intercept = 0, slope = 1,linetype = "dashed")
convergence_a <- ggplot(model_outputs_df) +
geom_point(aes(x = Rhat_a_HRS, y = Rhat_a_integrated)) +
stat_smooth(aes(x = Rhat_a_HRS, y = Rhat_a_integrated),method = "lm") +
geom_abline(intercept = 0, slope = 1,linetype = "dashed")
conv_a_records_integrated <- ggplot(model_outputs_df) +
geom_point(aes(x = n_records_PoMS, y = Rhat_a_integrated)) +
stat_smooth(aes(x = n_records_PoMS, y = Rhat_a_integrated),method = "lm")
conv_a_sites_integrated <- ggplot(model_outputs_df) +
geom_point(aes(x = n_sites_PoMS, y = Rhat_a_integrated)) +
stat_smooth(aes(x = n_sites_PoMS, y = Rhat_a_integrated),method = "lm")
conv_a_records_HRS <- ggplot(model_outputs_df) +
geom_point(aes(x = n_records_HRS, y = Rhat_a_integrated)) +
stat_smooth(aes(x = n_records_HRS, y = Rhat_a_integrated),method = "lm")
conv_a_sites_HRS <- ggplot(model_outputs_df) +
geom_point(aes(x = n_sites_HRS, y = Rhat_a_integrated)) +
stat_smooth(aes(x = n_sites_HRS, y = Rhat_a_integrated),method = "lm")
conv_a_records_total <- ggplot(model_outputs_df) +
geom_point(aes(x = (n_records_PoMS + n_records_HRS), y = Rhat_a_integrated)) +
stat_smooth(aes(x = (n_records_PoMS + n_records_HRS), y = Rhat_a_integrated),method = "lm")
conv_a_sites_total <- ggplot(model_outputs_df) +
geom_point(aes(x = (n_sites_PoMS + n_sites_HRS), y = Rhat_a_integrated)) +
stat_smooth(aes(x = (n_sites_PoMS + n_sites_HRS), y = Rhat_a_integrated),method = "lm")
Prec_a_records_PoMS <- ggplot(model_outputs_df) +
geom_point(aes(x = n_records_PoMS, y = Prec_a_integrated)) +
stat_smooth(aes(x = n_records_PoMS, y = Prec_a_integrated),method = "lm")
Prec_a_sites_PoMS <- ggplot(model_outputs_df) +
geom_point(aes(x = n_sites_PoMS, y = Prec_a_integrated)) +
stat_smooth(aes(x = n_sites_PoMS, y = Prec_a_integrated),method = "lm")
Prec_a_records_HRS <- ggplot(model_outputs_df) +
geom_point(aes(x = n_records_HRS, y = Prec_a_integrated)) +
stat_smooth(aes(x = n_records_HRS, y = Prec_a_integrated),method = "lm")
Prec_a_sites_HRS <- ggplot(model_outputs_df) +
geom_point(aes(x = n_sites_HRS, y = Prec_a_integrated)) +
stat_smooth(aes(x = n_sites_HRS, y = Prec_a_integrated),method = "lm")
Prec_a_records_total <- ggplot(model_outputs_df) +
geom_point(aes(x = (n_records_PoMS + n_records_HRS), y = Prec_a_integrated)) +
stat_smooth(aes(x = (n_records_PoMS + n_records_HRS), y = Prec_a_integrated),method = "lm")
Prec_a_sites_total <- ggplot(model_outputs_df) +
geom_point(aes(x = (n_sites_PoMS + n_sites_HRS), y = Prec_a_integrated)) +
stat_smooth(aes(x = (n_sites_PoMS + n_sites_HRS), y = Prec_a_integrated),method = "lm")