-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathboral_data_extraction.R
348 lines (270 loc) · 11.9 KB
/
boral_data_extraction.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
#########################################
#########################################
### DATA EXTRACTION FROM boral MODELS ###
#########################################
#########################################
library(boral)
library(abind)
library(matrixStats)
library(ineq)
################################################
### Define species and coefficient names/ids ###
################################################
species.id <- 1:ncol(PA) # vector of species ids [for extraction]
species.names <- colnames(PA) # vector of species names
Env.id <- 1:ncol(Env) # vector of covariate ids (no intercept) [for extraction]
Env.names <- c("intercept", colnames(Env)) # vector of covariate names incl. intercept
model <- "boral"
############################################
### Extract MCMC samples from jags model ###
############################################
mcmc.samp <- as.mcmc(JSDM$jags.model) # extracts mcmc from boral object
colnames(mcmc.samp[[1]]) # column names from mcmc object
#######################################
### Standardise posteriors post-hoc ###
#######################################
dataset_sd <- read.csv("Butterflies_sd.csv") # load in original data sd
dataset_sd <- dataset_sd[,2]
# dataset_sd <- c(1, dataset_sd) # add intercept ##NOT REQUIRED FOR BORAL
mcmc.samp_standardised <- mcmc.samp
# for(i in seq(length(full_standardised$trace$B))){ # species ## EASIER TO DO DURING EXTRACTON FOR BORAL
# for(j in seq(length(dataset_sd))){ #covariate
# full_standardised$trace$B[[i]][,j] <- (full_standardised$trace$B[[i]][,j])/dataset_sd[j]
# }
# }
##############################################
### Extract regression coefficient samples ###
##############################################
# Not standardised
beta.samp <- matrix(nrow = 1000)
col.names <- vector()
for(i in species.id){ # species
column.name <- sprintf("lv.coefs[%s,1]", i) # 3 lines code to get species intercept samples
col.names <- c(col.names, column.name)
command <- sprintf('mcmc.samp[[1]][1:1000,"%s"]', column.name)
tmp <- eval(parse(text = command))
for(j in Env.id){ # variables
column.name2 <- sprintf("X.coefs[%s,%s]", i, j) # 3 lines code to get variables samples (by species)
col.names <- c(col.names, column.name2)
command2 <- sprintf('mcmc.samp[[1]][1:1000, "%s"]', column.name2)
tmp2 <- eval(parse(text = command2))
tmp <- cbind(tmp, tmp2) # create entire matrix for one species
}
beta.samp <- cbind(beta.samp, tmp) # bind all species matrcies together
}
beta.samp <- beta.samp[,-(1)] # remove NA column from beginning
colnames(beta.samp) <- col.names # set column names
# Standardised
beta.samp_standardised <- matrix(nrow = 1000)
col.names_standardised <- vector()
for(i in species.id){ # species
column.name <- sprintf("lv.coefs[%s,1]", i) # 3 lines code to get species intercept samples
col.names_standardised <- c(col.names_standardised, column.name)
command <- sprintf('mcmc.samp[[1]][1:1000,"%s"]', column.name)
tmp <- eval(parse(text = command))
for(j in Env.id){ # variables
column.name2 <- sprintf("X.coefs[%s,%s]", i, j) # 3 lines code to get variables samples (by species)
col.names_standardised <- c(col.names_standardised, column.name2)
command2 <- sprintf('(mcmc.samp[[1]][1:1000, "%s"])/dataset_sd[%s]', column.name2, j) # added sd correction
tmp2 <- eval(parse(text = command2))
tmp <- cbind(tmp, tmp2) # create entire matrix for one species
}
beta.samp_standardised <- cbind(beta.samp_standardised, tmp) # bind all species matrcies together
}
beta.samp_standardised <- beta.samp_standardised[,-(1)] # remove NA column from beginning
colnames(beta.samp_standardised) <- col.names_standardised # set column names
#####################################################
### Extract Mean/SD/Quantiles and into Data Frame ###
#####################################################
# Not standardised
beta.mean <- colMeans(beta.samp)
beta.mean <- matrix(beta.mean, ncol = length(Env.names), byrow = T)
colnames(beta.mean) <- Env.names
rownames(beta.mean) <- species.names
beta.sd <- colSds(beta.samp)
beta.sd <- matrix(beta.sd, ncol = length(Env.names), byrow = T)
colnames(beta.sd) <- Env.names
rownames(beta.sd) <- species.names
beta.lower <- colQuantiles(beta.samp, probs = 0.025)
beta.lower <- matrix(beta.lower, ncol = length(Env.names), byrow = T)
colnames(beta.lower) <- Env.names
rownames(beta.lower) <- species.names
beta.upper <- colQuantiles(beta.samp, probs = 0.975)
beta.upper <- matrix(beta.upper, ncol = length(Env.names), byrow = T)
colnames(beta.upper) <- Env.names
rownames(beta.upper) <- species.names
coefVar <- function(vector){
sd.vec <- sd(vector)
mean.vec <- mean(vector)
cv <- sd.vec/mean.vec
return(cv)
}
beta.cv <- numeric(0)
for(i in seq(ncol(beta.samp))){
tmp <- coefVar(beta.samp[,i])
beta.cv <- c(beta.cv, tmp)
}
beta.cv <- matrix(beta.cv, ncol = length(Env.names), byrow = TRUE)
colnames(beta.cv) <- Env.names
rownames(beta.cv) <- species.names
qcd <- function(vector){
q1 <- quantile(vector, probs = 0.25)
q3 <- quantile(vector, probs = 0.75)
qcd <- (q3-q1)/(q3+q1)
return(qcd)
}
beta.qcd <- numeric(0)
for(i in seq(ncol(beta.samp))){
tmp <- qcd(beta.samp[,i])
beta.qcd <- c(beta.qcd, tmp)
}
beta.qcd <- matrix(beta.qcd, ncol = length(Env.names), byrow = TRUE)
colnames(beta.qcd) <- Env.names
rownames(beta.qcd) <- species.names
qcd2 <- function(vector){
q1 <- quantile(vector, probs = 0.025)
q3 <- quantile(vector, probs = 0.975)
qcd <- (q3-q1)/(q3+q1)
return(qcd)
}
beta.qcd2 <- numeric(0)
for(i in seq(ncol(beta.samp))){
tmp <- qcd2(beta.samp[,i])
beta.qcd2 <- c(beta.qcd2, tmp)
}
beta.qcd2 <- matrix(beta.qcd2, ncol = length(Env.names), byrow = TRUE)
colnames(beta.qcd2) <- Env.names
rownames(beta.qcd2) <- species.names
beta.gini <- numeric(0)
for(i in seq(ncol(beta.samp))){
tmp <- ineq(beta.samp[,i], type = "Gini")
beta.gini <- c(beta.gini, tmp)
}
beta.gini <- matrix(beta.gini, ncol = length(Env.names), byrow = TRUE)
colnames(beta.gini) <- Env.names
rownames(beta.gini) <- species.names
# Standardised
beta.mean_standardised <- colMeans(beta.samp_standardised)
beta.mean_standardised <- matrix(beta.mean_standardised, ncol = length(Env.names), byrow = T)
colnames(beta.mean_standardised) <- Env.names
rownames(beta.mean_standardised) <- species.names
beta.sd_standardised <- colSds(beta.samp_standardised)
beta.sd_standardised <- matrix(beta.sd_standardised, ncol = length(Env.names), byrow = T)
colnames(beta.sd_standardised) <- Env.names
rownames(beta.sd_standardised) <- species.names
beta.lower_standardised <- colQuantiles(beta.samp_standardised, probs = 0.025)
beta.lower_standardised <- matrix(beta.lower_standardised, ncol = length(Env.names), byrow = T)
colnames(beta.lower_standardised) <- Env.names
rownames(beta.lower_standardised) <- species.names
beta.upper_standardised <- colQuantiles(beta.samp_standardised, probs = 0.975)
beta.upper_standardised <- matrix(beta.upper_standardised, ncol = length(Env.names), byrow = T)
colnames(beta.upper_standardised) <- Env.names
rownames(beta.upper_standardised) <- species.names
coefVar <- function(vector){
sd.vec <- sd(vector)
mean.vec <- mean(vector)
cv <- sd.vec/mean.vec
return(cv)
}
beta.cv_standardised <- numeric(0)
for(i in seq(ncol(beta.samp_standardised))){
tmp <- coefVar(beta.samp_standardised[,i])
beta.cv_standardised <- c(beta.cv_standardised, tmp)
}
beta.cv_standardised <- matrix(beta.cv_standardised, ncol = length(Env.names), byrow = TRUE)
colnames(beta.cv_standardised) <- Env.names
rownames(beta.cv_standardised) <- species.names
qcd <- function(vector){
q1 <- quantile(vector, probs = 0.25)
q3 <- quantile(vector, probs = 0.75)
qcd <- (q3-q1)/(q3+q1)
return(qcd)
}
beta.qcd_standardised <- numeric(0)
for(i in seq(ncol(beta.samp_standardised))){
tmp <- qcd(beta.samp_standardised[,i])
beta.qcd_standardised <- c(beta.qcd_standardised, tmp)
}
beta.qcd_standardised <- matrix(beta.qcd_standardised, ncol = length(Env.names), byrow = TRUE)
colnames(beta.qcd_standardised) <- Env.names
rownames(beta.qcd_standardised) <- species.names
qcd2 <- function(vector){
q1 <- quantile(vector, probs = 0.025)
q3 <- quantile(vector, probs = 0.975)
qcd <- (q3-q1)/(q3+q1)
return(qcd)
}
beta.qcd2_standardised <- numeric(0)
for(i in seq(ncol(beta.samp_standardised))){
tmp <- qcd2(beta.samp_standardised[,i])
beta.qcd2_standardised <- c(beta.qcd2_standardised, tmp)
}
beta.qcd2_standardised <- matrix(beta.qcd2_standardised, ncol = length(Env.names), byrow = TRUE)
colnames(beta.qcd2_standardised) <- Env.names
rownames(beta.qcd2_standardised) <- species.names
beta.gini_standardised <- numeric(0)
for(i in seq(ncol(beta.samp_standardised))){
tmp <- ineq(beta.samp_standardised[,i], type = "Gini")
beta.gini_standardised <- c(beta.gini_standardised, tmp)
}
beta.gini_standardised <- matrix(beta.gini_standardised, ncol = length(Env.names), byrow = TRUE)
colnames(beta.gini_standardised) <- Env.names
rownames(beta.gini_standardised) <- species.names
##############################
### Create blank dataframe ### # Not standardised
##############################
df <- data.frame(coefficient = numeric(0), posterior.mean = numeric(0), lower = numeric(0),
upper = numeric(0), sd = numeric(0), coefVar = numeric(0), qcd = numeric(0),
qcd2 = numeric(0), gini = numeric(0), model = numeric(0), species = numeric(0))
############################
### Extract to dataframe ### # Not standardised
############################
for(i in 1:ncol(PA)){
dfr <- cbind(Env.names, beta.mean[i,], beta.lower[i,], beta.upper[i,], beta.sd[i,],
beta.cv[i,], beta.qcd[i,], beta.qcd2[i,], beta.gini[i,],
rep("boral", (length(Env.names))), rep(species.names[i], length(Env.names)))
colnames(dfr) <- c("coefficient", "posterior.mean", "lower", "upper", "sd",
"coefVar", "qcd", "qcd2", "gini", "model", "species")
dfr <- as.data.frame(dfr)
df <- rbind(df, dfr)
}
rownames(df) <- NULL
##############################
### Create blank dataframe ### # Standardised
##############################
df_standardised <- data.frame(coefficient = numeric(0), posterior.mean = numeric(0), lower = numeric(0),
upper = numeric(0), sd = numeric(0), coefVar = numeric(0), qcd = numeric(0),
qcd2 = numeric(0), gini = numeric(0), model = numeric(0), species = numeric(0))
############################
### Extract to dataframe ### # Standardised
############################
for(i in 1:ncol(PA)){
dfr <- cbind(Env.names, beta.mean_standardised[i,], beta.lower_standardised[i,],
beta.upper_standardised[i,], beta.sd_standardised[i,],
beta.cv_standardised[i,], beta.qcd_standardised[i,], beta.qcd2_standardised[i,],
beta.gini_standardised[i,], rep("boral", (length(Env.names))),
rep(species.names[i], length(Env.names)))
colnames(dfr) <- c("coefficient", "posterior.mean", "lower", "upper", "sd",
"coefVar", "qcd", "qcd2", "gini", "model", "species")
dfr <- as.data.frame(dfr)
df_standardised <- rbind(df_standardised, dfr)
}
rownames(df_standardised) <- NULL
df_merge <- df
df_merge[,5:9] <- df_standardised[,5:9]
#############################
### Rho Mean/Sd/Quantiles ###
#############################
source("get_residual_corr_new_function.R") # implement modified version of get.residual.cor() to provide sd and quantile values
rho.mean <- get.residual.cor.new(JSDM, est = "mean")$cor
rho.sd <- get.residual.cor.new(JSDM, est = "sd")$cor
rho.lower <- get.residual.cor.new(JSDM, est = "q_lower")$cor
rho.upper <- get.residual.cor.new(JSDM, est = "q_upper")$cor
####################
#### Write CSVs ####
####################
write.csv(df_merge, "Beta_Butterfly_boral.csv")
write.csv(rho.mean, "Rho_mean_Butterfly_boral.csv")
write.csv(rho.lower, "Rho_lower_Butterfly_boral.csv")
write.csv(rho.upper, "Rho_upper_Butterfly_boral.csv")
write.csv(rho.sd, "Rho_sd_Butterfly_boral.csv")