-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path2.1_imputation_cohort1_DC.Rmd
390 lines (302 loc) · 14.9 KB
/
2.1_imputation_cohort1_DC.Rmd
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
---
title: "2.1_imputation_cohort1"
author: "Judith Gilsbach"
date: "2023-12-02"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
# Packages
```{r}
library(haven)
library(tidyverse)
library(tidygraph)
library(network)
library(igraph)
library(RSiena)
library(knitr)
library(tableHTML)
library(stargazer)
```
# Load environment from initial setup
```{r}
#load cleaned data
```
# Define helper functions
```{r}
#Define Function that will hopefully ensure convergence in RSiena (this is from Siena Manual)
siena07ToConvergence <- function(alg, dat, eff, ans0 = NULL, threshold = 0.25,
nodes = 1, ...){
# parameters are:
# alg, dat, eff: Arguments for siena07: algorithm, data, effects object.
# ans0: previous answer, if available; used as prevAns in siena07.
# threshold: largest satisfactory value
# for overall maximum convergence ratio (indicating convergence).
# nodes: number of processes for parallel processing.
if (!is.null(ans0)) {
alg$nsub = 6
}
numr <- 0 # number of repetitions
ans <- siena07(alg, data = dat, effects = eff, prevAns = ans0,
nbrNodes = nodes, returnDeps = TRUE,
useCluster = (nodes >= 2), ...) # the first run
repeat {
save(ans, file = paste("ans",numr,".RData",sep = "")) # to be safe
numr <- numr + 1 # count number of repeated runs
tm <- ans$tconv.max # convergence indicator
cat(numr, tm,"\n") # report how far we are
if (tm < threshold) {break} # success
if (tm > 10) {break} # divergence without much hope
# of returning to good parameter values
if (numr > 20) {break} # now it has lasted too long
alg$nsub <- 1
alg$n2start <- 2 * (sum(eff$include) + 7) * 2.52**4
alg$n3 <- 3000
if (numr == 1) {alg$firstg <- alg$firstg/5}
ans <- siena07(alg, data = dat, effects = eff, prevAns = ans,
nbrNodes = nodes, returnDeps = TRUE,
useCluster = (nodes >= 2),...)
}
if (tm > threshold)
{
stop("Convergence inadequate.\n")
}
ans
}
#Since version 1.2-12, Maximum Likelihood (ML) estimation by Rsiena with returnDeps = TRUE returns an edgelist of the final network at the end of the phase 3 simulation.
#The following function, getNet(), uses this edgelist to impute the data.
getNet <- function(observedNet,edgeList) {
# observedNet = observed network as adjacency with missing data
# edgeList = edgelist that is returned by siena07(...)$sims
observedNet[is.na(observedNet)] <- 0
for (i in 1:nrow(edgeList)) {
observedNet[edgeList[i,1],edgeList[i,2]] <- 1
}
return(observedNet)
}
```
# Imputation
All 63 students who started their freshman year in sociology in fall 2020 were invited to take part in the study but only 42 took part in the study (66 out of 83 for the 2nd cohort in fall 2021). We consider those as "taking part" who allowed us to take a photo of themselves or handed one in to be identified by their fellow students in the online survey and who additionally handed in the data protection agreement before the beginning of the field work of wave 1.
Missing data occurred due to some students not taking part in all waves of the study. This missing data on the tie level shall be imputed because it does not only affect the knowledge about the missing actor but also about the other actors potentially nominated by the missing actor.
## Cohort 1 <br/>(2020/21)
### Defining the imputation model for the first wave
For a detailed introduction to imputation using SAOM see the script prepared by Robert Krause: https://www.stats.ox.ac.uk/~snijders/siena/AdSUMMissingDataMD.html
```{r}
imp_W1_K1 <- list() #create list to store the imputed networks
D <- 50 #number of imputations
## Imputation with a stationary SAOM
relation_K1_full <- sienaDependent(array(c(relation_W1_K1_ma, relation_W1_K1_ma), dim = c(42, 42, 2)) ,
allowOnly = FALSE)
Female_v <- as.vector(Attributes_K1$Female)
Statistik_v <- as.vector(Attributes_K1$Statistik)
WiW_v <- as.vector(Attributes_K1$WiW)
both_tutorials_v <- as.vector(Attributes_K1$both_tutorials)
Female <- coCovar(as.vector(Female_v),centered=FALSE)
Statistik <- coCovar(as.vector(Statistik_v),centered=FALSE)
WiW <- coCovar(as.vector(WiW_v),centered=FALSE)
both_tutorials <- coCovar(as.vector(both_tutorials_v),centered=FALSE)
w2 <- coDyadCovar(relation_W2_K1_ma) #wave 1 is especially sparse, therefore wave 2 is used as a dyadic predictor
#eithertut <- coDyadCovar(either_tutorial_K1) #either WiW or Statistics tutorial group shared:only for appendix
Data.stationary <- sienaDataCreate(relation_K1_full,Statistik,WiW, both_tutorials,w2, Female)
effects.stationary <- getEffects(Data.stationary)
effects.stationary <- includeEffects(effects.stationary, recip, density, outActSqrt,inPopSqrt, inActSqrt, outIso)
effects.stationary <- setEffect(effects.stationary, gwespFF, parameter = 138)
effects.stationary <- setEffect(effects.stationary, outTrunc, parameter=2)
effects.stationary <- includeEffects(effects.stationary, sameX,
interaction1 = "Statistik")
effects.stationary <- includeEffects(effects.stationary, sameX,
interaction1 = "WiW")
effects.stationary <- includeEffects(effects.stationary, sameX,
interaction1 = "both_tutorials")
effects.stationary <- includeEffects(effects.stationary, X, name = "relation_K1_full",
interaction1 = "w2")
# effects.stationary <- includeEffects(effects.stationary, X, name = "relation_K1_full",
# interaction1 = "eithertut")
#Gender
effects.stationary <- includeEffects(effects.stationary, sameX,
interaction1 = "Female")
effects.stationary <- includeEffects(effects.stationary, egoX,
interaction1 = "Female")
effects.stationary <- includeEffects(effects.stationary, altX,
interaction1 = "Female")
effects.stationary <- setEffect(effects.stationary, Rate, initialValue = 50,
name = "relation_K1_full", fix = TRUE, type = "rate")
#Now we can estimate the stationary SAOM with Methods of Moments (MoM) estimation. One converged estimate will suffice.
estimation.options <- sienaAlgorithmCreate(useStdInits = FALSE,
seed = 2214,
n3 = 1000, maxlike = FALSE,
cond = FALSE) #n3 =number of datasets simulated
period0saom_K1 <- siena07ToConvergence(alg = estimation.options,
dat = Data.stationary,
eff = effects.stationary, threshold = 0.25)
#Now change the RSiena algorithm to impuatation
imputation.options <- sienaAlgorithmCreate(seed = 13848,
useStdInits = FALSE,
maxlike = TRUE,
cond = FALSE,
nsub = 0,
simOnly = TRUE,
n3 = 10)
######################## Goodness of fit should ideally be evaluated before using the model for imputation ###################
#Show results of the above model (the one that converged)
period0saom_K1
#t-ratio of the basic rate parameter is high and SE is NA, this is because the effect is fixed
#check for autocorrelation
period0saom_K1$ac
#t-statistics = parameter estimate divided by standard error, if they are bigger than 1.96 they are significant at the 0.05-level
significance <- ifelse((abs(period0saom_K1$theta/period0saom_K1$se)) > 1.96, TRUE, FALSE) #MIND: Absolute value
significance
#check for collinearity
summary(period0saom_K1)
```
### Imputing the first wave
```{r}
set.seed(142)
for (i in 1:D) {
cat('imputation',i,'\n')
n1 <- relation_W1_K1_ma
n1 <- n1 + 10
diag(n1) <- 0
n2 <- n1
tieList <- c(1:(nrow(n1)**2))[c(n1 == 11)]
tieList <- tieList[!is.na(tieList)]
changedTie <- sample(tieList,1)
n1[changedTie] <- 0
n2[changedTie] <- 1
relation_K1_full <- sienaDependent(array(c(n1,n2), dim = c(42, 42, 2)),
allowOnly = FALSE )
Data.stationary <- sienaDataCreate(relation_K1_full,Statistik, WiW, both_tutorials,w2, Female)
sims <- siena07(imputation.options, data = Data.stationary,
effects = effects.stationary,
prevAns = period0saom_K1,
returnDeps = TRUE)$sims[[10]][[1]]
imp_W1_K1[[i]] = getNet(relation_W1_K1_ma,sims)
}
```
### Imputing the later waves
```{r}
#create lists for the imputed networks of wave 2 and 3
imp_W2_K1 <- list()
imp_W3_K1 <- list()
#For each wave estimation is done With Method of Moments (MoM) and imputation with Maximum Likelihood (ML)
#Imputation is repeated as often as the first wave had been imputed (D times)
set.seed(1307)
for (i in 1:D) {
cat('imputation',i,'\n')
# now impute wave2
relation_K1_full <- sienaDependent(array(c(imp_W1_K1[[i]],relation_W2_K1_ma),
dim = c(42,42,2)))
Data.w2 <- sienaDataCreate(relation_K1_full, Statistik, WiW, both_tutorials, Female)
effects.twoWaves <- getEffects(Data.w2)
effects.twoWaves <- includeEffects(effects.twoWaves,
recip, density, outActSqrt, inPopSqrt, inActSqrt)
effects.stationary <- setEffect(effects.stationary, gwespFF, parameter = 138)
effects.twoWaves <- setEffect(effects.twoWaves, outTrunc, parameter=2)
effects.twoWaves <- includeEffects(effects.twoWaves, sameX,
interaction1 = "Statistik")
effects.twoWaves <- includeEffects(effects.twoWaves, sameX,
interaction1 = "WiW")
effects.twoWaves <- includeEffects(effects.twoWaves, sameX,
interaction1 = "both_tutorials")
# effects.twoWaves <- includeEffects(effects.twoWaves, X, name = "relation_K1_full",
# interaction1 = "eithertut")
#Gender
effects.twoWaves <- includeEffects(effects.twoWaves, sameX,
interaction1 = "Female")
effects.twoWaves <- includeEffects(effects.twoWaves, egoX,
interaction1 = "Female")
effects.twoWaves <- includeEffects(effects.twoWaves, altX,
interaction1 = "Female")
if (i == 1) {
period1saom_K1 <- siena07ToConvergence(alg = estimation.options,
dat = Data.w2,
eff = effects.twoWaves,
threshold = 0.25)
} else {
period1saom_K1 <- siena07ToConvergence(alg = estimation.options,
dat = Data.w2,
eff = effects.twoWaves,
threshold = 0.25,
ans0 = period1saom_K1)
}
sims <- siena07(imputation.options, data = Data.w2,
effects = effects.twoWaves,
prevAns = period1saom_K1,
returnDeps = TRUE)$sims[[10]][[1]]
imp_W2_K1[[i]] <- getNet(relation_W2_K1_ma,sims)
########
# impute wave 3
relation_K1_full <- sienaDependent(array(c(imp_W2_K1[[i]], relation_W3_K1_ma),
dim = c( 42, 42, 2)))
Data.w3 <- sienaDataCreate(relation_K1_full, Statistik, WiW, both_tutorials, Female)
if (i == 1) {
period2saom_K1 <- siena07ToConvergence(alg = estimation.options,
dat = Data.w3,
eff = effects.twoWaves,
threshold = 0.25)
} else {
period2saom_K1 <- siena07ToConvergence(alg = estimation.options,
dat = Data.w3,
eff = effects.twoWaves,
threshold = 0.25,
ans0 = period2saom_K1)
}
sims <- siena07(imputation.options, data = Data.w3,
effects = effects.twoWaves,
prevAns = period2saom_K1,
returnDeps = TRUE)$sims[[10]][[1]]
imp_W3_K1[[i]] = getNet(relation_W3_K1_ma,sims)
save.image('mi.RData')
}
#check results:
period1saom_K1
period2saom_K1
summary(period1saom_K1)
summary(period2saom_K1)
#check for autocorrelation
period1saom_K1$ac
period2saom_K1$ac
#t-statistics = parameter estimate devided by standard error, if they are bigger than 1.96 they are significant at the 0.05-level
significance_p1_K1 <- ifelse((abs(period1saom_K1$theta/period1saom_K1$se)) > 1.96, TRUE, FALSE) #MIND: Absolute value
significance_p1_K1
significance_p2_K1 <- ifelse((abs(period2saom_K1$theta/period2saom_K1$se)) > 1.96, TRUE, FALSE) #MIND: Absolute value
significance_p2_K1
#check for collinearity
summary(period0saom_K1)
summary(period1saom_K1)
summary(period2saom_K1)
```
# Assessing _Goodness of Fit_ for the impuation models
## Cohort 1<br/>2020/21
```{r}
## Period 0
# 1: Goodness of Fit for the last imputation
saom.model_K1.results.gof <- list()
saom.model_K1.results.gof[[1]] <- plot(sienaGOF(period0saom_K1, varName="relation_K1_full", OutdegreeDistribution))
saom.model_K1.results.gof[[2]] <- plot(sienaGOF(period0saom_K1, varName="relation_K1_full", IndegreeDistribution))
#View results
saom.model_K1.results.gof[[1]]
saom.model_K1.results.gof[[2]]
## Period 1
saom.model_K1.results.gof1 <- list()
saom.model_K1.results.gof1[[1]] <- plot(sienaGOF(period1saom_K1, varName="relation_K1_full", OutdegreeDistribution))
saom.model_K1.results.gof1[[2]] <- plot(sienaGOF(period1saom_K1, varName="relation_K1_full", IndegreeDistribution))
#View results
saom.model_K1.results.gof1[[1]]
saom.model_K1.results.gof1[[2]]
## Period 2
saom.model_K1.results.gof2 <- list()
saom.model_K1.results.gof2[[1]] <- plot(sienaGOF(period2saom_K1, varName="relation_K1_full", OutdegreeDistribution))
saom.model_K1.results.gof2[[2]] <- plot(sienaGOF(period2saom_K1, varName="relation_K1_full", IndegreeDistribution))
#View results
saom.model_K1.results.gof2[[1]]
saom.model_K1.results.gof2[[2]]
```
```{r}
todaysdate <- format(Sys.Date(),"%y%m%d")
save(list = ls(.GlobalEnv), file = paste0(todaysdate,"_imputation_cohort1_environment_",D,"imp.Rdata"))
```
```{r}
sessionInfo()
```