-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path6100_control_paper_interaction_model.R
194 lines (141 loc) · 5.91 KB
/
6100_control_paper_interaction_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
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 #
# #
#########################
###############################################################################
interaction.ergm <- 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")
+ absdiff("std.know")
+ absdiff("std.att")
+ absdiff("std.conf")
+ absdiff("know.int")
+ absdiff("att.int")
+ absdiff("conf.int")
#Indegree terms
+ nodeifactor("gender")
+ nodeifactor("sex.var")
+ nodeicov("std.know")
+ nodeicov("std.att")
+ nodeicov("std.conf")
+ nodeicov("know.int")
+ nodeicov("att.int")
+ nodeicov("conf.int")
#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("know.int")
+ nodeocov("att.int")
+ nodeocov("conf.int")
,
directed=T, verbose = T,
constraints=~bd(maxout=6),
control=control.ergm(main.method=c("MCMLE"), MCMLE.maxit = 60,
force.main=F , seed = 274,
parallel=50, 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)
write.table(1, file = paste0("baseline interaction completed iteration ",j,"so far.txt"))
#save(imputation.list, file = paste0("T:/projects/stash_trial/09 STASH SNA/Data/AnonymisedData/working data/ergm_imp",j,"_",savename,".rdata"))
save(imputation.list, file = paste0("T:/projects/stash_trial/09 STASH SNA/Data/AnonymisedData/working data/interaction_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 ergm functions
imputation.list <- list()
school.result.list <- list()
##This was run without the start end code
control.interaction <-
interaction.ergm(
dataset = ergm.data.control.imputed,
savename = "control.interaction",
start_iter = 1,
end_iter = 20
)
###Save failed at iter 17
control.interaction <-
interaction.ergm(
dataset = ergm.data.control.imputed,
savename = "control.interaction",
start_iter = 17,
end_iter = 20
)
save(control.interaction , file= "control.interaction 17 to 20.rdata")
##put 1 to 16 in with 17 to 20
load("interaction_ergm_imp16_control.interaction.rdata")
for (i in 1:16){
control.interaction[[i]] <- imputation.list[[i]]
}
save(control.interaction , file= "control.interaction.rdata")
baseline.interaction <-
interaction.ergm(
dataset = ergm.data.baseline.imputed,
savename = "baseline.interaction",
start_iter = 1,
end_iter = 20
)
save(baseline.interaction , file= "baseline.interaction.rdata")