-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path8000_metafor_combine_models.R
383 lines (291 loc) · 11.4 KB
/
8000_metafor_combine_models.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
rm(list = ls())
#################
# #
# Name #
# #
#################
# 8000 metafor combine models
# Mark McCann developed the script
#############
# Purpose #
#############
#Metafor meta analysis of final ERGM estimates
##############
# #
# 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 12 networks.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)
}
####################################################
####################################################
perception.forestplot <- function(data = NULL,
coefrow = NULL ,
variable = "",
varname = "",
conf = 95
){
##Create a 5 * 2 table, 5 schools in rows, coefficient and SE in columns
coef.table <- matrix(NA, nr=5, nc = 2)
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)]
}
#Run meta analysis
metareg <- rma(yi=coef.table[,1],
sei=coef.table[,2],
slab=c("Sch 1 control", "Sch 2 control","Sch 3 control","Sch 5 control","Sch 6 control")
, method = "REML", level = conf)
#Plot results
setwd("T:/projects/stash_trial/09 STASH SNA/DisseminationAndImpact/Manuscripts_Papers/Soc Sex Dev Paper/")
pdf(paste0(variable," forest 5 networks.pdf"))
forest(metareg,
main = paste0("Tie probability by ",variable),
xlab = "Odds ratio: forming a tie",
transf = exp,
refline = 1)
dev.off()
return(metareg)
}
####################################################
#########################
# #
# Main body of script #
# #
#########################
#########################
# #
# Main models #
# #
#########################
##################################################
###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
meta.list <- list()
counter <- 1
for (i in indata[[3]]$variable){
meta.list[[counter]] <- forestplot(data = indata ,variable = paste0("Main 6000 ", i," param"), varname = i)
counter <- counter + 1
}
#####layout of meta analysis summary table
# Variable labels
varnames <- indata[[3]]$variable
##Intercept lci uci
# I squared variability
meta.table <- data.frame(matrix(NA, nr = length(varnames), nc = 3))
colnames(meta.table) <- c("Variable","beta (95% CI)", "I Squared")
meta.table[,1] <- varnames
counter <- 1
for (i in 1:length(varnames)){
xp <- predict(meta.list[[i]], transf = exp, digits = 4)
meta.table[counter,2] <- paste0(round(xp$pred,2)," (",round(xp$ci.lb,2),", ",round(xp$ci.ub,2),")")
meta.table[counter,3] <- round(meta.list[[i]]$I2,2)
counter <- counter + 1
}
write.csv(meta.table, file = "main model - metaanalysis table 10 schools 20 imputations omit sch 4.csv")
##################################################
##################################################
#########################
# #
# interaction models #
# #
#########################
##################################################
###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 = "interaction.6100.sch*")
for (sch in 1:10) {
load(filename[sch])
}
indata <- list()
indata[[1]] <- baseline.interaction.6100.sch1[[3]]
indata[[2]] <- baseline.interaction.6100.sch2[[3]]
##Gender 3 is NA as no nonbinary/trans in this network
# Fix to zero to allow plot
indata[[2]][16,2:3] <- c(.001, 0.1)
indata[[3]] <- baseline.interaction.6100.sch3[[3]]
indata[[4]] <- baseline.interaction.6100.sch5[[3]]
indata[[5]] <- baseline.interaction.6100.sch6[[3]]
##Gender 3 is NA as no nonbinary/trans in this network
# Fix to zero to allow plot
indata[[5]][16,2:3] <- c(.001, 0.1)
indata[[6]] <- control.interaction.6100.sch1[[3]]
indata[[7]] <- control.interaction.6100.sch2[[3]]
indata[[8]] <- control.interaction.6100.sch3[[3]]
##Gender 3 is NA as no nonbinary/trans in this network
# Fix to zero to allow plot
indata[[8]][16,2:3] <- c(.001, 0.1)
indata[[9]] <- control.interaction.6100.sch5[[3]]
indata[[10]] <- control.interaction.6100.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
meta.list <- list()
counter <- 1
for (i in indata[[3]]$variable){
meta.list[[counter]] <- forestplot(data = indata ,variable = paste0("6100 ", i," param"), varname = i)
counter <- counter + 1
}
#####layout of meta analysis summary table
# Variable labels
varnames <- indata[[3]]$variable
##Intercept lci uci
# I squared variability
meta.table <- data.frame(matrix(NA, nr = length(varnames), nc = 3))
colnames(meta.table) <- c("Variable","beta (95% CI)", "I Squared")
meta.table[,1] <- varnames
counter <- 1
for (i in 1:length(varnames)){
xp <- predict(meta.list[[i]], transf = exp, digits = 4)
meta.table[counter,2] <- paste0(round(xp$pred,2)," (",round(xp$ci.lb,2),", ",round(xp$ci.ub,2),")")
meta.table[counter,3] <- round(meta.list[[i]]$I2,2)
counter <- counter + 1
}
write.csv(meta.table, file = "Interaction model - metaanalysis table 10 schools 20 imputations omit sch 4.csv")
##################################################
##################################################
#########################
# #
# perception models #
# #
#########################
###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 = "perception.6200.sch*")
for (sch in 1:5) {
load(filename[sch])
}
indata <- list()
indata[[1]] <- control.perception.6200.sch1[[3]]
indata[[2]] <- control.perception.6200.sch2[[3]]
indata[[3]] <- control.perception.6200.sch3[[3]]
##Gender 3 is NA as no nonbinary/trans in this network
# Fix to zero to allow plot
indata[[3]][14,2:3] <- c(.001, 0.1)
indata[[4]] <- control.perception.6200.sch5[[3]]
indata[[5]] <- control.perception.6200.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
meta.list <- list()
counter <- 1
for (i in indata[[3]]$variable){
meta.list[[counter]] <- perception.forestplot(data = indata ,variable = paste0("6200 perception ", i," param"), varname = i)
counter <- counter + 1
}
#####layout of meta analysis summary table
# Variable labels
varnames <- indata[[3]]$variable
##Intercept lci uci
# I squared variability
meta.table <- data.frame(matrix(NA, nr = length(varnames), nc = 3))
colnames(meta.table) <- c("Variable","beta (95% CI)", "I Squared")
meta.table[,1] <- varnames
counter <- 1
for (i in 1:length(varnames)){
xp <- predict(meta.list[[i]], transf = exp, digits = 4)
meta.table[counter,2] <- paste0(round(xp$pred,2)," (",round(xp$ci.lb,2),", ",round(xp$ci.ub,2),")")
meta.table[counter,3] <- round(meta.list[[i]]$I2,2)
counter <- counter + 1
}
write.csv(meta.table, file = "Perception model - metaanalysis table 5 control schools 20 imputations omit sch 4.csv")
##################################################