-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path9000_combined_forest_plots.R
436 lines (353 loc) · 17 KB
/
9000_combined_forest_plots.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
rm(list = ls())
#################
# #
# Name #
# #
#################
# Mark McCann developed the script
#############
# Purpose #
#############
#Plots of metafor results
##############
# #
# Notes #
# #
##############
#########################
# #
# Outstanding actions #
# #
#########################
#########################
# #
# Load packages #
# #
#########################
library("metafor")
#########################
# #
# Load functions #
# #
#########################
####################################################
forestplot <- function(data = NULL,
coefrow = NULL ,
variable = "",
varname = "",
conf = 95
){
##Create a 12 * 3 table, 12 schools in rows, coefficient, SE, and baseline/control in columns
coef.table <- matrix(NA, nr=10, nc = 3)
for (i in 1:length(indata)){
#Fill the table with varname row 1st col coef
coef.table[i,1] <- indata[[i]]$pool.est[which(indata[[i]]$variable==varname)]
#Fill the table with varname row 2nd col SE
coef.table[i,2] <- indata[[i]]$pool.se[which(indata[[i]]$variable==varname)]
}
###Add a baseline / control dummy var
coef.table[1:5,3] <- 0
coef.table[6:10,3] <- 1
colnames(coef.table) <- c("coef","se","control")
coef.table <- coef.table[!is.na(coef.table[,1]),]
#Run meta analysis
metareg <- rma(yi=coef.table[,1],
sei=coef.table[,2],
slab=c("Sch 1 baseline", "Sch 2 baseline", "Sch 3 baseline", "Sch 5 baseline", "Sch 6 baseline"
, "Sch 1 control", "Sch 2 control","Sch 3 control","Sch 5 control","Sch 6 control")
, method = "REML", level = conf)
metareg.wave <- rma(yi=coef.table[,1],
sei=coef.table[,2],
slab=c("Sch 1 baseline", "Sch 2 baseline", "Sch 3 baseline", "Sch 5 baseline", "Sch 6 baseline"
, "Sch 1 control", "Sch 2 control","Sch 3 control","Sch 5 control","Sch 6 control")
, method = "REML"
, mods = coef.table[,3])
print(metareg.wave)
#Plot results
setwd("T:/projects/stash_trial/09 STASH SNA/DisseminationAndImpact/Manuscripts_Papers/Soc Sex Dev Paper/")
pdf(paste0(variable," forest 10 networks main model.pdf"))
forest(metareg,
main = paste0("Tie probability by difference in ",variable),
xlab = "Odds ratio: forming a tie",
transf = exp,
refline = 1)
dev.off()
return(metareg)
}
####################################################
################################################################
################################################################
######## Combined Plots for Article
################################################################
################################################################
##################################################
###Load in the pooled imputation files for each school
indata <- list()
setwd("T:/projects/stash_trial/09 STASH SNA/Data/AnonymisedData/working data")
filename <- list.files(pattern = "main.6000.sch*")
for (sch in 1:10) {
load(filename[sch])
}
indata <- list()
indata[[1]] <- baseline.main.6000.sch1[[3]]
indata[[2]] <- baseline.main.6000.sch2[[3]]
##Gender 3 is NA as no nonbinary/trans in this network
# Fix to zero to allow plot
indata[[2]][13,2:3] <- c(.001, 0.1)
indata[[3]] <- baseline.main.6000.sch3[[3]]
indata[[4]] <- baseline.main.6000.sch5[[3]]
indata[[5]] <- baseline.main.6000.sch6[[3]]
##Gender 3 is NA as no nonbinary/trans in this network
# Fix to zero to allow plot
indata[[5]][13,2:3] <- c(.001, 0.1)
indata[[6]] <- control.main.6000.sch1[[3]]
indata[[7]] <- control.main.6000.sch2[[3]]
indata[[8]] <- control.main.6000.sch3[[3]]
##Gender 3 is NA as no nonbinary/trans in this network
# Fix to zero to allow plot
indata[[8]][13,2:3] <- c(.001, 0.1)
indata[[9]] <- control.main.6000.sch5[[3]]
indata[[10]] <- control.main.6000.sch6[[3]]
### To use forestplot
# Variable is the name of the file and title of plot
# Varname must match that in the indata output
# Conf is confidence intervals for the plot
# It saves a pdf plot in the control schools folder
# And outputs a metaregression with control school dummy to the console
###########################
## Three sex var plots ##
###########################
###########################
#Create a 10 * 3 table, 12 schools in rows, coefficient, SE, and baseline/control in columns
inact.coef.table <- matrix(NA, nr=10, nc = 3)
for (i in 1:length(indata)){
#Fill the table with varname row 1st col coef
inact.coef.table[i,1] <- indata[[i]]$pool.est[which(indata[[i]]$variable=="nodematch.sex.var.1")]
#Fill the table with varname row 2nd col SE
inact.coef.table[i,2] <- indata[[i]]$pool.se[which(indata[[i]]$variable=="nodematch.sex.var.1")]
}
mid.coef.table <- matrix(NA, nr=10, nc = 3)
for (i in 1:length(indata)){
#Fill the table with varname row 1st col coef
mid.coef.table[i,1] <- indata[[i]]$pool.est[which(indata[[i]]$variable=="nodematch.sex.var.2")]
#Fill the table with varname row 2nd col SE
mid.coef.table[i,2] <- indata[[i]]$pool.se[which(indata[[i]]$variable=="nodematch.sex.var.2")]
}
orvag.coef.table <- matrix(NA, nr=10, nc = 3)
for (i in 1:length(indata)){
#Fill the table with varname row 1st col coef
orvag.coef.table[i,1] <- indata[[i]]$pool.est[which(indata[[i]]$variable=="nodematch.sex.var.3")]
#Fill the table with varname row 2nd col SE
orvag.coef.table[i,2] <- indata[[i]]$pool.se[which(indata[[i]]$variable=="nodematch.sex.var.3")]
}
#Run meta analysis
inact.meta <- rma(yi=inact.coef.table[,1],
sei=inact.coef.table[,2],
slab=c("Sch 1 baseline", "Sch 2 baseline", "Sch 3 baseline", "Sch 5 baseline", "Sch 6 baseline"
, "Sch 1 control", "Sch 2 control","Sch 3 control","Sch 5 control","Sch 6 control")
, method = "REML", level = 95)
mid.meta <- rma(yi=mid.coef.table[,1],
sei=mid.coef.table[,2],
slab=c("Sch 1 baseline", "Sch 2 baseline", "Sch 3 baseline", "Sch 5 baseline", "Sch 6 baseline"
, "Sch 1 control", "Sch 2 control","Sch 3 control","Sch 5 control","Sch 6 control") ,
method = "REML", level = 95)
orvag.meta <- rma(yi=orvag.coef.table[,1],
sei=orvag.coef.table[,2],
slab=c("Sch 1 baseline", "Sch 2 baseline", "Sch 3 baseline", "Sch 5 baseline", "Sch 6 baseline"
, "Sch 1 control", "Sch 2 control","Sch 3 control","Sch 5 control","Sch 6 control") ,
method = "REML", level = 95)
full.coef.table <- rbind(inact.coef.table,
mid.coef.table,
orvag.coef.table)
full.coef.table[,3] <- 1:30
all.meta <- rma(yi =full.coef.table[,1],
sei=full.coef.table[,2],
slab=c("Sch 1 baseline", "Sch 2 baseline", "Sch 3 baseline", "Sch 5 baseline", "Sch 6 baseline",
"Sch 1 control", "Sch 2 control","Sch 3 control","Sch 5 control","Sch 6 control",
"Sch 1 baseline ", "Sch 2 baseline ", "Sch 3 baseline ", "Sch 5 baseline ", "Sch 6 baseline ",
"Sch 1 control ", "Sch 2 control ","Sch 3 control ","Sch 5 control ","Sch 6 control ",
"Sch 1 baseline ", "Sch 2 baseline ", "Sch 3 baseline ", "Sch 5 baseline ", "Sch 6 baseline ",
"Sch 1 control ", "Sch 2 control ","Sch 3 control ","Sch 5 control ","Sch 6 control ")
,
method = "REML", level = 95)
setwd("T:/projects/stash_trial/09 STASH SNA/DisseminationAndImpact/Manuscripts_Papers/Soc Sex Dev Paper/")
# bmp("Sexual Activity combined Forest plot.bmp", width = 1200 , height = 1200)
#
# forest(all.meta, main = "",
# xlab = "Odds ratio: forming a tie",
# transf = exp,
# refline = 1,
# rows=c(2:11,14:23,26:35), # Which rows to put estimates - leave space for the subgroup diamonds and titles
# top = 1, # How many blank rows at top of plot for a header
# cex = 1.25,
# ylim = c(1,37),
# xlim = c(-2,6), # Limit of whole plot
# alim = c(-0.5,4.5), # Limit of forest axes
# at = c(0,0.5,1,2,3.25,4), # Points on forest x axis
# addfit = FALSE)
#
# ### switch to bold font
# par(font=2)
#
# text(-2, c(36), pos=4, font = 2, cex=1.25, c("Oral/Vaginal"))
# addpoly(orvag.meta, row= 25 , cex=1.25, mlab="", transf = exp)
# text(-2, 25, pos=4, font = 4, cex=1.25, paste("RE Model. I^2 = ",formatC(orvag.meta$I2, digits=1, format="f"), "%"))
#
# par(font=2)
# text(-2, c(24), pos=4, cex=1.25, c("Active no sex"))
# addpoly(mid.meta , row= 13, cex=1.25, mlab="", transf = exp)
# text(-2, 13, pos=4, font = 4, cex=1.25, paste("RE Model. I^2 = ",formatC(mid.meta$I2, digits=1, format="f"), "%"))
#
# par(font=2)
# text(-2, c(12), pos=4, cex=1.25, c("Sexually inactive"))
# addpoly(inact.meta, row= 1, cex=1.25, mlab="", transf = exp)
# text(-2, 1, pos=4, font = 4, cex=1.25, paste("RE Model. I^2 = ",formatC(inact.meta$I2, digits=1, format="f"), "%"))
# dev.off()
#
# ###########################
tiff("Figure 1 Sexual Activity combined Forest plot.tif", width = 1200 , height = 1200)
forest(all.meta, main = "",
xlab = "Odds ratio of forming a tie: above 1 favours homophily",
transf = exp,
refline = 1,
rows=c(2:11,14:23,26:35), # Which rows to put estimates - leave space for the subgroup diamonds and titles
top = 1, # How many blank rows at top of plot for a header
cex = 1.25,
ylim = c(1,37),
xlim = c(-2,6), # Limit of whole plot
alim = c(-0.5,4.5), # Limit of forest axes
at = c(0,0.5,1,2,3.25,4), # Points on forest x axis
addfit = FALSE)
### switch to bold font
par(font=2)
text(-2, c(36), pos=4, font = 2, cex=1.25, c("Oral/Vaginal"))
addpoly(orvag.meta, row= 25 , cex=1.25, mlab="", transf = exp)
text(-2, 25, pos=4, font = 4, cex=1.25, paste("RE Model. I^2 = ",formatC(orvag.meta$I2, digits=1, format="f"), "%"))
par(font=2)
text(-2, c(24), pos=4, cex=1.25, c("Active no sex"))
addpoly(mid.meta , row= 13, cex=1.25, mlab="", transf = exp)
text(-2, 13, pos=4, font = 4, cex=1.25, paste("RE Model. I^2 = ",formatC(mid.meta$I2, digits=1, format="f"), "%"))
par(font=2)
text(-2, c(12), pos=4, cex=1.25, c("Sexually inactive"))
addpoly(inact.meta, row= 1, cex=1.25, mlab="", transf = exp)
text(-2, 1, pos=4, font = 4, cex=1.25, paste("RE Model. I^2 = ",formatC(inact.meta$I2, digits=1, format="f"), "%"))
dev.off()
###########################
#############################
#### Know att conf plots ####
#############################
###########################
#Create a 10 * 3 table, 12 schools in rows, coefficient, SE, and baseline/control in columns
know.coef.table <- matrix(NA, nr=10, nc = 3)
for (i in 1:length(indata)){
#Fill the table with varname row 1st col coef
know.coef.table[i,1] <- indata[[i]]$pool.est[which(indata[[i]]$variable=="absdiff.std.know")]
#Fill the table with varname row 2nd col SE
know.coef.table[i,2] <- indata[[i]]$pool.se[which(indata[[i]]$variable=="absdiff.std.know")]
}
att.coef.table <- matrix(NA, nr=10, nc = 3)
for (i in 1:length(indata)){
#Fill the table with varname row 1st col coef
att.coef.table[i,1] <- indata[[i]]$pool.est[which(indata[[i]]$variable=="absdiff.std.att")]
#Fill the table with varname row 2nd col SE
att.coef.table[i,2] <- indata[[i]]$pool.se[which(indata[[i]]$variable=="absdiff.std.att")]
}
conf.coef.table <- matrix(NA, nr=10, nc = 3)
for (i in 1:length(indata)){
#Fill the table with varname row 1st col coef
conf.coef.table[i,1] <- indata[[i]]$pool.est[which(indata[[i]]$variable=="absdiff.std.conf")]
#Fill the table with varname row 2nd col SE
conf.coef.table[i,2] <- indata[[i]]$pool.se[which(indata[[i]]$variable=="absdiff.std.conf")]
}
#Run meta analysis
know.meta <- rma(yi=know.coef.table[,1],
sei=know.coef.table[,2],
slab=c("Sch 1 baseline", "Sch 2 baseline", "Sch 3 baseline", "Sch 5 baseline", "Sch 6 baseline"
, "Sch 1 control", "Sch 2 control","Sch 3 control","Sch 5 control","Sch 6 control")
, method = "REML", level = 95)
att.meta <- rma(yi=att.coef.table[,1],
sei=att.coef.table[,2],
# slab=c("Sch 1 baseline", "Sch 2 baseline", "Sch 3 baseline", "Sch 5 baseline", "Sch 6 baseline"
# , "Sch 1 control", "Sch 2 control","Sch 3 control","Sch 5 control","Sch 6 control")
method = "REML", level = 95)
conf.meta <- rma(yi=conf.coef.table[,1],
sei=conf.coef.table[,2],
# slab=c("Sch 1 baseline", "Sch 2 baseline", "Sch 3 baseline", "Sch 5 baseline", "Sch 6 baseline"
# , "Sch 1 control", "Sch 2 control","Sch 3 control","Sch 5 control","Sch 6 control")
method = "REML", level = 95)
full.coef.table <- rbind(know.coef.table,
att.coef.table,
conf.coef.table)
full.coef.table[,3] <- 1:30
all.meta <- rma(yi =full.coef.table[,1],
sei=full.coef.table[,2],
slab=c("Sch 1 baseline", "Sch 2 baseline", "Sch 3 baseline", "Sch 5 baseline", "Sch 6 baseline",
"Sch 1 control", "Sch 2 control","Sch 3 control","Sch 5 control","Sch 6 control",
"Sch 1 baseline ", "Sch 2 baseline ", "Sch 3 baseline ", "Sch 5 baseline ", "Sch 6 baseline ",
"Sch 1 control ", "Sch 2 control ","Sch 3 control ","Sch 5 control ","Sch 6 control ",
"Sch 1 baseline ", "Sch 2 baseline ", "Sch 3 baseline ", "Sch 5 baseline ", "Sch 6 baseline ",
"Sch 1 control ", "Sch 2 control ","Sch 3 control ","Sch 5 control ","Sch 6 control ")
,
method = "REML", level = 95)
# bmp("Know Att Conf combined Forest plot.bmp", width = 1200 , height = 1200)
#
# forest(all.meta, main = "",
# xlab = "Odds ratio: forming a tie",
# transf = exp,
# refline = 1,
# rows=c(2:11,14:23,26:35), # Which rows to put estimates - leave space for the subgroup diamonds and titles
# top = 1, # How many blank rows at top of plot for a header
# cex = 1.25,
# ylim = c(1,37),
# xlim = c(0.5,1.5), # Limit of whole plot
# # alim = c(-0.5,4.5), # Limit of forest axes
# # at = c(0,0.5,1,2,3.25,4), # Points on forest x axis
# addfit = FALSE)
#
# ### switch to bold font
# par(font=2)
#
# text(0.5, c(36), pos=4, font = 2, cex=1.25, c("Confidence"))
# addpoly(conf.meta, row= 25 , cex=1.25, mlab="", transf = exp)
# text(0.5, 25, pos=4, font = 4, cex=1.25, paste("RE Model. I^2 = ",formatC(conf.meta$I2, digits=1, format="f"), "%"))
#
# par(font=2)
# text(0.5, c(24), pos=4, cex=1.25, c("Norms"))
# addpoly(att.meta , row= 13, cex=1.25, mlab="", transf = exp)
# text(0.5, 13, pos=4, font = 4, cex=1.25, paste("RE Model. I^2 = ",formatC(att.meta$I2, digits=1, format="f"), "%"))
#
# par(font=2)
# text(0.5, c(12), pos=4, cex=1.25, c("Knowledge"))
# addpoly(know.meta, row= 1, cex=1.25, mlab="", transf = exp)
# text(0.5, 1, pos=4, font = 4, cex=1.25, paste("RE Model. I^2 = ",formatC(know.meta$I2, digits=1, format="f"), "%"))
# dev.off()
#
# ###########################
tiff("Figure 2 Know Att Conf combined Forest plot.tif", width = 1200 , height = 1200)
forest(all.meta, main = "",
xlab = "Odds ratio of forming a tie: below 1 favours homophily",
transf = exp,
refline = 1,
rows=c(2:11,14:23,26:35), # Which rows to put estimates - leave space for the subgroup diamonds and titles
top = 1, # How many blank rows at top of plot for a header
cex = 1.25,
ylim = c(1,37),
xlim = c(0.5,1.5), # Limit of whole plot
# alim = c(-0.5,4.5), # Limit of forest axes
# at = c(0,0.5,1,2,3.25,4), # Points on forest x axis
addfit = FALSE)
### switch to bold font
par(font=2)
text(0.5, c(36), pos=4, font = 2, cex=1.25, c("Confidence"))
addpoly(conf.meta, row= 25 , cex=1.25, mlab="", transf = exp)
text(0.5, 25, pos=4, font = 4, cex=1.25, paste("RE Model. I^2 = ",formatC(conf.meta$I2, digits=1, format="f"), "%"))
par(font=2)
text(0.5, c(24), pos=4, cex=1.25, c("Norms"))
addpoly(att.meta , row= 13, cex=1.25, mlab="", transf = exp)
text(0.5, 13, pos=4, font = 4, cex=1.25, paste("RE Model. I^2 = ",formatC(att.meta$I2, digits=1, format="f"), "%"))
par(font=2)
text(0.5, c(12), pos=4, cex=1.25, c("Knowledge"))
addpoly(know.meta, row= 1, cex=1.25, mlab="", transf = exp)
text(0.5, 1, pos=4, font = 4, cex=1.25, paste("RE Model. I^2 = ",formatC(know.meta$I2, digits=1, format="f"), "%"))
dev.off()
###########################