-
Notifications
You must be signed in to change notification settings - Fork 0
/
6200_control_paper_perception_model.R
248 lines (190 loc) · 9.32 KB
/
6200_control_paper_perception_model.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
rm(list = ls())
#################
# #
# Name #
# #
#################
# 6000 Ergms
# Chiara Broccatelli developed ergm script
# Mark McCann modified to loop over imputations
#############
# Purpose #
#############
# Running ergms across imp samples
##############
# #
# Notes #
# #
##############
#
#########################
# #
# Outstanding actions #
# #
#########################
#########################
# #
# Load packages #
# #
#########################
require(parallel)
library(network)
library(ergm)
library(mice)
#########################
# #
# Load functions #
# #
#########################
###############################################################################
perception.ergm.6.iter <- function(dataset = NULL, savename = NULL, start_iter = NA, end_iter = NA) {
#If iters are specified, use that number of iterations.
# Otherwise, use the whole length of the file
start <- ifelse(is.na(start_iter) , 1, start_iter )
end <- ifelse(is.na(end_iter) , length(dataset), end_iter )
for (j in start:end) {
df1 <- dataset[[j]]
for (i in c(1,2,3,5,6) ) {
df <- df1[[i]]
school.result.list[[i]] <- ergm(df ~ edges
+ mutual
+ gwesp(0.25, fixed = T)
+ idegree1.5()
#Homophily terms
+ nodematch("sex.var", diff = T)
+ nodematch("gender")
+ nodematch("sex.percep.var")
+ absdiff("std.know")
+ absdiff("std.att")
+ absdiff("std.conf")
#Indegree terms
+ nodeifactor("gender")
+ nodeifactor("sex.var")
+ nodeicov("std.know")
+ nodeicov("std.att")
+ nodeicov("std.conf")
+ nodeicov("sex.percep.var")
#Outdegree terms
# trans coef not converging - just estimate men : women / trans
+ nodeofactor("gender", levels = -(2:3))
+ nodeofactor("sex.var")
+ nodeocov("std.know")
+ nodeocov("std.att")
+ nodeocov("std.conf")
+ nodeocov("sex.percep.var")
,
directed=T, verbose = T,
constraints=~bd(maxout=6),
control=control.ergm(main.method=c("MCMLE"), MCMLE.maxit = 6,
force.main=F , seed = 274,
parallel=30, parallel.type="PSOCK"),
eval.loglik = F ###This cuts computation time but doesnt update screen
)
print(paste0("Imputation",j,"School",i,"completed"))
}
imputation.list[[j]] <- school.result.list
print(j)
print(paste0("All schools for imputation",j,"finished"))
write.table(1, file = paste0("completed perception iteration ",j,"so far.txt"))
save(imputation.list, file = paste0("T:/projects/stash_trial/09 STASH SNA/Data/AnonymisedData/working data/perception_ergm_imp",j,"_",savename,".rdata"))
}
return(imputation.list)
}
###############################################################################
###############################################################################
perception.ergm.restart <- function(dataset = NULL, savename = NULL, start_iter = NA, end_iter = NA, prev = NA) {
#If iters are specified, use that number of iterations.
# Otherwise, use the whole length of the file
start <- ifelse(is.na(start_iter) , 1, start_iter )
end <- ifelse(is.na(end_iter) , length(dataset), end_iter )
for (j in start:end) {
df1 <- dataset[[j]]
for (i in c(1,2,3,5,6) ) {
df <- df1[[i]]
school.result.list[[i]] <- ergm(df ~ edges
+ mutual
+ gwesp(0.25, fixed = T)
+ idegree1.5()
#Homophily terms
+ nodematch("sex.var", diff = T)
+ nodematch("gender")
+ nodematch("sex.percep.var")
+ absdiff("std.know")
+ absdiff("std.att")
+ absdiff("std.conf")
#Indegree terms
+ nodeifactor("gender")
+ nodeifactor("sex.var")
+ nodeicov("std.know")
+ nodeicov("std.att")
+ nodeicov("std.conf")
+ nodeicov("sex.percep.var")
#Outdegree terms
# trans coef not converging - just estimate men : women / trans
+ nodeofactor("gender", levels = -(2:3))
+ nodeofactor("sex.var")
+ nodeocov("std.know")
+ nodeocov("std.att")
+ nodeocov("std.conf")
+ nodeocov("sex.percep.var")
,
directed=T, verbose = T,
constraints=~bd(maxout=6),
control=control.ergm(main.method=c("MCMLE"), MCMLE.maxit = 60,
force.main=F , seed = 274,
parallel=30, parallel.type="PSOCK",
init = coef(prev[[j]][[i]])),
eval.loglik = F ###This cuts computation time but doesnt update screen
)
print(paste0("Imputation",j,"School",i,"completed"))
}
imputation.list[[j]] <- school.result.list
print(j)
print(paste0("All schools for imputation",j,"finished"))
write.table(1, file = paste0("completed perception restart iteration ",j,"so far.txt"))
save(imputation.list, file = paste0("T:/projects/stash_trial/09 STASH SNA/Data/AnonymisedData/working data/perception_ergm_imp",j,"_",savename,".rdata"))
}
return(imputation.list)
}
#########################
# #
# Main body of script #
# #
#########################
setwd("T:/projects/stash_trial/09 STASH SNA/Data/AnonymisedData/working data/")
load("T:/projects/stash_trial/09 STASH SNA/Data/AnonymisedData/working data/ergm_data_control_imputed_5501.rdata")
load("T:/projects/stash_trial/09 STASH SNA/Data/AnonymisedData/working data/ergm_data_baseline_imputed_5501.rdata")
#####Run these before calling the run.imputed.ergms function
imputation.list <- list()
school.result.list <- list()
control.perception.start <- perception.ergm.6.iter(dataset = ergm.data.control.imputed,
savename = "control.perception",
start_iter = 1, end_iter = 20)
save(control.perception.start , file= "control.perception.start.rdata")
control.perception.restart <- perception.ergm.restart(dataset = ergm.data.control.imputed,
savename = "control.perception.restart",
start_iter = 1, end_iter = 20,
prev = control.perception.start)
control.perception.restart <- imputation.list
save(control.perception.restart , file= "control.perception.rdata")
control.perception.restart[[1]]
###Perception question wasn't included in baseline so not run on baseline schools.
# Nb Baseline data was included in multiple imputation
# #####Code to patch together iterations that stalled
# load the temp files
load("perception_ergm_imp20_control.perception.rdata")
cp_11to20 <- imputation.list
load("perception_ergm_imp10_control.perception.rdata")
cp_10 <- imputation.list
load("perception_ergm_imp9_control.perception.rdata")
cp_1to9 <- imputation.list
##Load the temp files into a single list object
control.perception <- list()
for (i in 1:9){
control.perception[[i]] <- cp_1to9[[i]]
}
control.perception[[10]] <- cp_10[[10]]
for (i in 11:20){
control.perception[[i]] <- cp_11to20[[i]]
}
save(control.perception , file= "control.perception.rdata")