-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1.2_Data_tidying.R
296 lines (254 loc) · 11.4 KB
/
1.2_Data_tidying.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
########################################################################################################################
## GOSSIP IN HUNGARIAN HIGH SCHOOLS
## Data tidying (1.2)
## R script written by Jose Luis Estevez (Linkoping University)
## Date: October 19th, 2020
########################################################################################################################
# DATA LOADING
rm(list=ls())
load('data.RData')
########################################################################################################################
# DATA CLEANING AND VARIALBE CREATION
# 1) ATTRIBUTES (gender and ethnicity)
# 1.1) Gender (female 1, male 0)
std$gender_1 <- std$gender_1 - 1
for(i in 2:4){
std[[paste('gender',i,sep='_')]] <- std$gender_1
}
# 1.2) Roma: whether the student self-identified as Roma or Roma-Hungarian (1) or not (0)
# Dichotomisation of ethnicity as either Roma or not
for(i in seq_along(networks)){
# Roma and Roma & Hungarian to 1, 0 otherwise
std[[paste('ethnic',i,sep='_')]][std[[paste('ethnic',i,sep='_')]] %in% c(1,4)] <- 0
std[[paste('ethnic',i,sep='_')]][std[[paste('ethnic',i,sep='_')]] %in% c(2,3)] <- 1
}
# if a student did not report their ethnicity but reported being member of a Roma group, the ethnicity is Roma
for(i in 1:nrow(std)){
if(is.na(std$ethnic_1[i]) & !is.na(std$romag1_1[i])){
std$ethnic_1[i] <- 1
}
if(is.na(std$ethnic_3[i]) & !is.na(std$romag1_3[i])){
std$ethnic_3[i] <- 1
}
}
# If the student reported at least one time being Roma, her/his ethnicity is always Roma (no matter the wave)
std$roma <- NA
for(i in 1:nrow(std)){
std$roma[i] <- max(c(std$ethnic_1[i],std$ethnic_2[i],std$ethnic_3[i],std$ethnic_4[i]),na.rm=TRUE)
}
std$roma[std$roma == -Inf] <- NA
for(i in seq_along(networks)){
std[[paste('roma',i,sep='_')]] <- std$roma
}
########################################################################################################################
# 2) RELATIONAL VARIABLES
# 2.1) Ensure that all classrooms have the same subjects in all the matrices of the same wave
names_nodes <- vector('list',length(networks))
for(wave in seq_along(networks)){
for(item in seq_along(networks[[wave]])){
names_nodes[[wave]] <- vector('list',length(networks[[wave]][[item]]))
}
}
for(wave in seq_along(networks)){
for(item in seq_along(networks[[wave]])){
for(room in seq_along(networks[[wave]][[item]])){
names_nodes[[wave]][[room]][[item]] <- rownames(networks[[wave]][[item]][[room]])
}
}
}
nodes_class <- vector('list',length(networks))
for(wave in seq_along(names_nodes)){
for(room in seq_along(names_nodes[[wave]])){
nodes_class[[wave]][[room]] <- names_nodes[[wave]][[room]][[1]]
}
}
for(wave in seq_along(names_nodes)){
for(room in seq_along(names_nodes[[wave]])){
for(nodes in 2:length(names_nodes[[wave]][[room]])){
# the intersection of nodes across all networks in the same wave and classroom
nodes_class[[wave]][[room]] <- intersect(nodes_class[[wave]][[room]],names_nodes[[wave]][[room]][[nodes]])
}
}
}
for(wave in seq_along(networks)){
for(item in seq_along(networks[[wave]])){
for(room in seq_along(networks[[wave]][[item]])){
# leave only the nodes in the intersection across all items in the same wave and classroom
networks[[wave]][[item]][[room]] <- networks[[wave]][[item]][[room]][nodes_class[[wave]][[room]],
nodes_class[[wave]][[room]]]
}
}
}
# 2.2) Relational dimensions: friendship, dislike from affect
dichotomise <- function(mtx,zero,one,na){
for(i in rownames(mtx)){
for(j in colnames(mtx)){
if(mtx[i,j] %in% zero){
mtx[i,j] <- 0
} else if(mtx[i,j] %in% one){
mtx[i,j] <- 1
} else if(mtx[i,j] %in% na)
mtx[i,j] <- NA
}
}
return(as.matrix(mtx))
}
for(wave in seq_along(networks)){
networks[[wave]]$friend <- lapply(networks[[wave]]$affect,dichotomise,zero=-2:1,one=2,na=NA)
networks[[wave]]$dislike <- lapply(networks[[wave]]$affect,dichotomise,zero=0:2,one=c(-2,-1),na=NA)
networks[[wave]]$affect <- NULL
}
########################################################################################################################
# 3) ATTRIBUTES: For the non-relational data, split by wave, and select only corresponding students and variables
students <- vector('list',length(networks))
names(students) <- names(networks)
for(wave in seq_along(students)){
students[[wave]] <- std[rownames(std) %in% unlist(nodes_class[[wave]]),endsWith(names(std),paste(wave))]
}
########################################################################################################################
## IMPUTATION OF MISSING TIES
# Creation of large matrices with all ties in the same wave (no matter in which classroom exactly)
dataimp <- vector('list',length=length(networks))
names(dataimp) <- names(networks)
for(wave in seq_along(dataimp)){
dataimp[[wave]] <- vector('list',6)
names(dataimp[[wave]]) <- c('friend','dislike','otherslookup','othersscorn')
}
for(wave in seq_along(networks)){
for(item in c('friend','dislike','otherslookup','othersscorn')){
for(room in seq_along(networks[[wave]][[item]])){
dataimp[[wave]][[item]][[room]] <- rownames(networks[[wave]][[item]][[room]])
}
dataimp[[wave]][[item]] <- unlist(dataimp[[wave]][[item]])
dataimp[[wave]][[item]] <- matrix(NA,nrow=length(dataimp[[wave]][[item]]),ncol=length(dataimp[[wave]][[item]]),
dimnames=list(dataimp[[wave]][[item]],dataimp[[wave]][[item]]))
}
}
for(wave in seq_along(networks)){
for(item in c('friend','dislike','otherslookup','othersscorn')){
for(room in names(networks[[wave]][[item]])){
for(i in rownames(networks[[wave]][[item]][[room]])){
for(j in colnames(networks[[wave]][[item]][[room]])){
networks[[wave]][[item]][[room]][i,j] -> dataimp[[wave]][[item]][i,j]
}
}
}
}
}
'%!in%' <- function(x,y)!('%in%'(x,y))
for(wave in 2:3){
for(item in c('friend','dislike','otherslookup','othersscorn')){
for(room in names(networks[[wave]][[item]])){
for(i in rownames(networks[[wave]][[item]][[room]])){
for(j in colnames(networks[[wave]][[item]][[room]])){
if(is.na(networks[[wave]][[item]][[room]][i,j])){
# If information from both previous and posterior wave is available
if(i %in% rownames(dataimp[[wave-1]][[item]]) & j %in% rownames(dataimp[[wave-1]][[item]]) &
i %in% rownames(dataimp[[wave+1]][[item]]) & j %in% rownames(dataimp[[wave+1]][[item]])){
x <- dataimp[[wave-1]][[item]][i,j]
y <- dataimp[[wave+1]][[item]][i,j]
if((!is.na(x) & x == 1 & !is.na(y) & y == 1) |
(is.na(x) & !is.na(y) & y == 1) |
(!is.na(x) & x == 1 & is.na(y))){
networks[[wave]][[item]][[room]][i,j] <- 1
}else{
networks[[wave]][[item]][[room]][i,j] <- 0
}
}
# If only information from the previous wave is available
else if(i %in% rownames(dataimp[[wave-1]][[item]]) & j %in% rownames(dataimp[[wave-1]][[item]]) &
(i %!in% rownames(dataimp[[wave+1]][[item]]) | j %!in% rownames(dataimp[[wave+1]][[item]]))){
networks[[wave]][[item]][[room]][i,j] <- dataimp[[wave-1]][[item]][i,j]
}
# If only information from the posterior wave is available
else if((i %!in% rownames(dataimp[[wave-1]][[item]]) | j %!in% rownames(dataimp[[wave-1]][[item]])) &
i %in% rownames(dataimp[[wave+1]][[item]]) & j %in% rownames(dataimp[[wave+1]][[item]])){
networks[[wave]][[item]][[room]][i,j] <- dataimp[[wave+1]][[item]][i,j]
}
}
}
}
}
}
}
# For wave 4, ties imputed only from from wave 3
for(wave in 4){
for(item in c('friend','dislike','otherslookup','othersscorn')){
for(room in names(networks[[wave]][[item]])){
for(i in rownames(networks[[wave]][[item]][[room]])){
for(j in colnames(networks[[wave]][[item]][[room]])){
if(is.na(networks[[wave]][[item]][[room]][i,j])){
if(i %in% rownames(dataimp[[wave-1]][[item]]) & j %in% rownames(dataimp[[wave-1]][[item]])){
networks[[wave]][[item]][[room]][i,j] <- dataimp[[wave-1]][[item]][i,j]
}
}
}
}
}
}
}
########################################################################################################################
# SAMPLE SELECTION
# Exclusion of wave 1
networks$wave1 <- NULL
students$wave1 <- NULL
# Exclusion of classrooms with MISSING-TIE FRACTION LARGER THAN 20%
go_missing <- go_density <- go_gossip_resp <- vector('list',length(networks))
for(wave in seq_along(networks)){
for(room in seq_along(networks[[wave]]$gossip)){
# extraction of gossip missing-tie fraction per classroom and wave
go_missing[[wave]][[room]] <- sum(is.na(networks[[wave]]$gossip[[room]])/
(nrow(networks[[wave]]$gossip[[room]])*(nrow(networks[[wave]]$gossip[[room]])-1)))
}
}
for(wave in seq_along(networks)){
for(item in seq_along(networks[[wave]])){
networks[[wave]][[item]] <- networks[[wave]][[item]][go_missing[[wave]]<.20]
}
}
# Exclusion of classrooms with a fewer than 25 ties
for(wave in seq_along(networks)){
for(room in seq_along(networks[[wave]]$gossip)){
# extraction of gossip ties per classroom and wave
go_density[[wave]][[room]] <- sum(networks[[wave]]$gossip[[room]],na.rm=TRUE)
}
}
for(wave in seq_along(networks)){
for(item in seq_along(networks[[wave]])){
networks[[wave]][[item]] <- networks[[wave]][[item]][go_density[[wave]]>=25]
}
}
# Exclusion of classrooms that will not converge using BERGMs
for(item in seq_along(networks$wave2)){
networks$wave2[[item]] <- networks$wave2[[item]][c(2,3,5,6,8,9)]
}
for(item in seq_along(networks$wave3)){
networks$wave3[[item]] <- networks$wave3[[item]][c(1,7,9,10,11,13)]
}
for(item in seq_along(networks$wave4)){
networks$wave4[[item]] <- networks$wave4[[item]][c(2,3,4,5,6)]
}
# Sample selection in the attribute object
for(wave in seq_along(students)){
students[[wave]] <- students[[wave]][students[[wave]]$class %in% names(networks[[wave]]$gossip),]
}
########################################################################################################################
# Renaming
names(networks) <- names(students) <- c('wave1','wave2','wave3')
for(wave in seq_along(networks)){
for(item in seq_along(networks[[wave]])){
names(networks[[wave]][[item]]) <- names(networks[[wave]]$gossip)
}
}
students$wave1 <- students$wave1[c('class_2','gender_2','roma_2')]
students$wave2 <- students$wave2[c('class_3','gender_3','roma_3')]
students$wave3 <- students$wave3[c('class_4','gender_4','roma_4')]
names(students$wave1) <- names(students$wave2) <- names(students$wave3) <- c('class','female','roma')
# Rearrange of the variables
for(wave in seq_along(networks)){
networks[[wave]] <- networks[[wave]][c('gossip','friend','dislike','otherslookup','othersscorn')]
}
# Removal of unnecessary objects
rm(list=ls()[-c(11,16)])
# Save image
save.image('tidieddata.RData')