forked from chbianco/GCAM-SoilC-Dynamics
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Meta_Analysis.R
347 lines (282 loc) · 12.4 KB
/
Meta_Analysis.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
library(metafor)
library(dplyr)
library(ggplot2)
#Load GCAM data
soilC <- read.csv(file = 'Data/GCAM_soilC.csv')
timescales <- read.csv(file = 'Data/soil_timescales.csv')
glus <- read.csv(file = 'Data/GLU_codes.csv')
regions <- read.csv('Data/GCAM_regions.csv')
#Load experimental data
PostKwon <- read.csv(file= 'Data/Experimental Data.csv', na.strings = c("", "NA"))
Wei <- read.csv(file= 'Data/Wei et al Data.csv', na.strings = c("", "NA"))
#Join GLU codes with soilC data
soilC %>%
mutate(GLU_code = GLU) %>%
right_join(glus, by='GLU_code') %>%
right_join(regions, by='GCAM_region_ID') %>%
right_join(timescales, by='GCAM_region_ID')-> soilC_regions
#Simplify the data to just the stuff we'll need to compare with experimental data
soilC_regions %>%
select(Land_Type, soil_c, GLU_code, soilTimeScale, GCAM_region_ID, Basin_long_name) -> simple_soilC_regions
#Creating the Post & Kwon comparison data
PostKwon %>%
select(Initial_Land_Use, Final_Land_Use, GLU_code, GCAM_region_ID, Time, Exp_Rate) %>%
na.omit() %>%
mutate(Land_Type = Initial_Land_Use) %>%
right_join(simple_soilC_regions, by = c('GLU_code', 'Land_Type', 'GCAM_region_ID')) %>%
rename(initial_soil_c = soil_c) %>%
select(-Land_Type) %>%
mutate(Land_Type = Final_Land_Use) %>%
right_join(simple_soilC_regions, by = c('GLU_code', 'Land_Type', 'GCAM_region_ID')) %>%
rename(final_soil_c = soil_c) %>%
select(-Land_Type, -soilTimeScale.x, -Basin_long_name.x) %>%
rename(soilTimeScale = soilTimeScale.y, Basin_long_name = Basin_long_name.y) %>%
na.omit() %>%
mutate(GCAM_Rate = (final_soil_c - initial_soil_c)/soilTimeScale, Rate_Difference = Exp_Rate - GCAM_Rate,
Exp_k = -log(abs(Exp_Rate)*Time +1)/Time,
GCAM_k = -log(final_soil_c/initial_soil_c)/soilTimeScale,
source = 'Post & Kwon'
) %>%
#This next line corrects the sign of Exp_k--we had to take the absolute value to avoid NaNs, so this accounts for that
mutate(Exp_k = ifelse(sign(Exp_k) == sign(Exp_Rate), Exp_k*(-1), Exp_k)) -> PostKwon_Comparison
#Creating the Wei et al comparison data
Wei %>%
select(Initial_Land_Use, Final_Land_Use, GLU_code, GCAM_region_ID, OC_decrease, Time) %>%
na.omit() %>%
mutate(Land_Type = Initial_Land_Use) %>%
right_join(simple_soilC_regions, by = c('GLU_code', 'Land_Type', 'GCAM_region_ID')) %>%
rename(initial_soil_c = soil_c) %>%
select(-Land_Type) %>%
mutate(Land_Type = Final_Land_Use) %>%
right_join(simple_soilC_regions, by = c('GLU_code', 'Land_Type', 'GCAM_region_ID')) %>%
rename(final_soil_c = soil_c) %>%
select(-Land_Type, -soilTimeScale.x, -Basin_long_name.x) %>%
rename(soilTimeScale = soilTimeScale.y, Basin_long_name = Basin_long_name.y) %>%
na.omit() %>%
mutate(GCAM_Rate = (final_soil_c - initial_soil_c)/soilTimeScale,
GCAM_k = -log(final_soil_c/initial_soil_c)/soilTimeScale,
Exp_k = -log(1/((abs(OC_decrease)/100) +1))/Time,
source = 'Wei et al'
) %>%
#This next line corrects the sign of Exp_k--we had to take the absolute value to avoid NaNs, so this accounts for that
mutate(Exp_k = ifelse(sign(Exp_k) == sign(OC_decrease), Exp_k, Exp_k*(-1))) -> Wei_Comparison
#Merging the two papers...
#Now, we bind the two rows together. Because the Wei et al data doesn't have any raw rates, we won't include that data from Post & Kwon either
Full_Comparison <- bind_rows(
select(PostKwon_Comparison, -Exp_Rate, -Rate_Difference),
select(Wei_Comparison, -OC_decrease)
)
#Now, we want to create another data frame that has all the repeated regions between the two studies
Full_Comparison %>%
mutate(source_short = ifelse(source == 'Post & Kwon', 'PostKwon', 'Wei')) %>%
mutate(basin_source = paste(Basin_long_name, source_short)) %>%
filter((paste(Basin_long_name, 'PostKwon') %in% basin_source) & (paste(Basin_long_name, 'Wei') %in% basin_source)) -> Duplicate_Comparison
counts = table(Duplicate_Comparison$basin_source)
Duplicate_Comparison %>%
filter(
(counts[paste(Basin_long_name, 'Wei')] > 1) & (counts[paste(Basin_long_name, 'PostKwon')] > 1)
) -> Duplicate_Comparison
#EVERYTHING ABOVE THIS LITERALLY JUST LOADS DATA!!!! DON'T CHANGE IT!!!!!
##ow, we get into the actual meta analysis
#First, we need to get mean, std dev, and n
PostKwon_Comparison %>%
group_by(Basin_long_name) %>%
summarize(mean_control = mean(Exp_Rate), sd_control= sd(Exp_Rate), n_control = n(),
mean_GCAM = mean(GCAM_Rate), sd_GCAM = sd(GCAM_Rate), n_GCAM = n()
) -> PostKwon_MA_data
#Now, we can use escalc to get the standardized mean difference
PostKwon_effect_sizes <-
escalc('SMD',
m1i = mean_control, n1i = n_control, sd1i = sd_control,
m2i = mean_GCAM, n2i = n_GCAM, sd2i = sd_GCAM,
data = PostKwon_MA_data
)
#We're going to use a fixed effect model for this analysis, which we set up below
PostKwon_fixed_effect_results <- rma(yi, vi, method = 'FE',
slab = Basin_long_name,
data = PostKwon_effect_sizes)
#Forest plot for Post & Kwon
forest(
PostKwon_effect_sizes$yi, PostKwon_effect_sizes$vi,
annotate = TRUE,showweights = TRUE,
header = c('Region', 'Weight SMD [95% CI]'),
slab = PostKwon_fixed_effect_results$slab,
xlab = 'Standardized Mean Difference',
#Below sets the size of study labels, shape of bars, and size of x labels
cex = .8, pch = 15, cex.lab = 1
)
#Adding the summary effect size
addpoly(
PostKwon_fixed_effect_results,
col = 'orange', cex = 1, annotate = TRUE, mlab = 'Summary'
)
#Now, we'll do one for the Post & Kwon k values
#First, we need to get mean, std dev, and n
PostKwon_Comparison %>%
group_by(Basin_long_name) %>%
summarize(mean_control = mean(Exp_k), sd_control= sd(Exp_k), n_control = n(),
mean_GCAM = mean(GCAM_k), sd_GCAM = sd(GCAM_k), n_GCAM = n()
) -> PostKwon_MA_k_data
#Now, we can use escalc to get the standardized mean difference
PostKwon_k_effect_sizes <-
escalc('SMD',
m1i = mean_control, n1i = n_control, sd1i = sd_control,
m2i = mean_GCAM, n2i = n_GCAM, sd2i = sd_GCAM,
data = PostKwon_MA_k_data
)
#We're going to use a fixed effect model for this analysis, which we set up below
PostKwon_k_fixed_effect_results <- rma(yi, vi, method = 'FE',
slab = Basin_long_name,
data = PostKwon_k_effect_sizes)
#Forest plot for Post & Kwon k vals
forest(
PostKwon_k_effect_sizes$yi, PostKwon_k_effect_sizes$vi,
annotate = TRUE, showweights = TRUE,
header = c('Region', 'Weight SMD [95% CI]'),
slab = PostKwon_k_fixed_effect_results$slab,
xlab = 'Standardized Mean Difference',
#Below sets the size of study labels, shape of bars, and size of x labels
cex = 1, pch = 15, cex.lab = 1
)
#Adding the summary effect size
addpoly(
PostKwon_k_fixed_effect_results,
col = 'orange', cex = 1, annotate = TRUE, mlab = 'Summary'
)
#Now we do the same for Wei et al.
#First, we need to get mean, std dev, and n
Wei_Comparison %>%
group_by(Basin_long_name) %>%
summarize(mean_control = mean(Exp_k), sd_control= sd(Exp_k), n_control = n(),
mean_GCAM = mean(GCAM_k), sd_GCAM = sd(GCAM_k), n_GCAM = n()
) -> Wei_MA_data
#Now, we can use escalc to get the standardized mean difference
Wei_effect_sizes <-
escalc('SMD',
m1i = mean_control, n1i = n_control, sd1i = sd_control,
m2i = mean_GCAM, n2i = n_GCAM, sd2i = sd_GCAM,
data = Wei_MA_data
)
#We're going to use a fixed effect model for this analysis, which we set up below
Wei_fixed_effect_results <- rma(yi, vi, method = 'FE',
slab = Basin_long_name,
data = Wei_effect_sizes)
#Forest plot for Wei
forest(
Wei_effect_sizes$yi, Wei_effect_sizes$vi,
annotate = TRUE, showweights = TRUE,
header = c('Region', 'Weight SMD [95% CI]'),
slab = Wei_fixed_effect_results$slab,
xlab = 'Standardized Mean Difference',
#Below sets the size of study labels, shape of bars, and size of x labels
cex = 1, pch = 15, cex.lab = 1
)
#Adding the summary effect size
addpoly(
Wei_fixed_effect_results,
col = 'orange', cex = 1, annotate = TRUE, mlab = 'Summary'
)
#Now we do it for all the data...
#First, we need to get mean, std dev, and n
Full_Comparison %>%
group_by(Basin_long_name) %>%
summarize(mean_control = mean(Exp_k), sd_control= sd(Exp_k), n_control = n(),
mean_GCAM = mean(GCAM_k), sd_GCAM = sd(GCAM_k), n_GCAM = n()
) -> Full_MA_data
#Now, we can use escalc to get the standardized mean difference
Full_effect_sizes <-
escalc('SMD',
m1i = mean_control, n1i = n_control, sd1i = sd_control,
m2i = mean_GCAM, n2i = n_GCAM, sd2i = sd_GCAM,
data = Full_MA_data
)
#We're going to use a fixed effect model for this analysis, which we set up below
Full_fixed_effect_results <- rma(yi, vi, method = 'FE',
slab = Basin_long_name,
data = Full_effect_sizes)
#Forest plot for full dataset
forest(
Full_effect_sizes$yi, Full_effect_sizes$vi,
annotate = TRUE,showweights = TRUE,
header = c('Region', 'Weight SMD [95% CI]'),
slab = Full_fixed_effect_results$slab,
xlab = 'Standardized Mean Difference',
#Below sets the size of study labels, shape of bars, and size of x labels
cex = .8, pch = 15, cex.lab = 1
)
#Adding the summary effect size
addpoly(
Full_fixed_effect_results,
col = 'orange', cex = 1, annotate = TRUE, mlab = 'Summary'
)
#Now, a meta analysis by transition type for the entire dataset
#First, we need to create a column in the merged data for transition type
Full_Comparison %>%
mutate(change = paste(Initial_Land_Use, Final_Land_Use, sep = '')) -> Full_Comparison_change
#First, we need to get mean, std dev, and n
Full_Comparison_change %>%
group_by(change) %>%
summarize(mean_control = mean(Exp_k), sd_control= sd(Exp_k), n_control = n(),
mean_GCAM = mean(GCAM_k), sd_GCAM = sd(GCAM_k), n_GCAM = n()
) -> Full_MA_data_change
#Now, we can use escalc to get the standardized mean difference
Full_effect_sizes_change <-
escalc('SMD',
m1i = mean_control, n1i = n_control, sd1i = sd_control,
m2i = mean_GCAM, n2i = n_GCAM, sd2i = sd_GCAM,
data = Full_MA_data_change
)
#We're going to use a fixed effect model for this analysis, which we set up below
Full_fixed_effect_results_change <- rma(yi, vi, method = 'FE',
slab = change,
data = Full_effect_sizes_change)
#Forest plot for full dataset
forest(
Full_effect_sizes_change$yi, Full_effect_sizes_change$vi,
annotate = TRUE, showweights = TRUE,
header = c('Transition Type', 'Weight SMD [95% CI]'),
slab = Full_fixed_effect_results_change$slab,
xlab = 'Standardized Mean Difference',
#Below sets the size of study labels, shape of bars, and size of x labels
cex = 1, pch = 15, cex.lab = 1
)
#Adding the summary effect size
addpoly(
Full_fixed_effect_results_change,
col = 'orange', cex = 1, annotate = TRUE, mlab = 'Summary'
)
#Meta analysis for the shared regions
#First, we need to get mean, std dev, and n
Duplicate_Comparison %>%
group_by(basin_source) %>%
summarize(mean_control = mean(Exp_k), sd_control= sd(Exp_k), n_control = n(),
mean_GCAM = mean(GCAM_k), sd_GCAM = sd(GCAM_k), n_GCAM = n(),
source = source
) -> Duplicate_MA_data
#Now, we can use escalc to get the standardized mean difference
Duplicate_effect_sizes <-
escalc('SMD',
m1i = mean_control, n1i = n_control, sd1i = sd_control,
m2i = mean_GCAM, n2i = n_GCAM, sd2i = sd_GCAM,
data = Duplicate_MA_data
)
#We're going to use a fixed effect model for this analysis, which we set up below
Duplicate_fixed_effect_results <- rma.mv(yi, vi, method = 'FE',
slab = basin_source,
mods = ~ source,
data = Duplicate_effect_sizes)
#Forest plot for duplicates
forest(
Duplicate_effect_sizes$yi, Duplicate_effect_sizes$vi,
annotate = TRUE,showweights = TRUE,
header = c('Region', 'Weight SMD [95% CI]'),
slab = Duplicate_fixed_effect_results$slab,
xlab = 'Standardized Mean Difference',
#Below sets the size of study labels, shape of bars, and size of x labels
cex = .8, pch = 15, cex.lab = 1
)
#Adding the summary effect size
addpoly(
Duplicate_fixed_effect_results,
col = 'orange', cex = 1, annotate = TRUE, mlab = 'Summary'
)