-
Notifications
You must be signed in to change notification settings - Fork 0
/
4100_descriptive_tables_omitting_school4.R
367 lines (251 loc) · 13 KB
/
4100_descriptive_tables_omitting_school4.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
rm(list = ls())
#################
# #
# Name #
# #
#################
# 4100 Descriptive tables - omitting school 4 baseline and control
# Mark McCann developed this script
# 3500 latent class sex items also calculated some values for the tables
#############
# Purpose #
#############
##############
# #
# Notes #
# #
##############
#
#########################
# #
# Outstanding actions #
# #
#########################
#########################
# #
# Load packages #
# #
#########################
require(network)
#########################
# #
# Load functions #
# #
#########################
source('T:/projects/stash_trial/09 STASH SNA/DataAnalysis/Syntax/+MELNET+/combineLists.R')
#########################
# #
# Main body of script #
# #
#########################
load("T:/projects/stash_trial/09 STASH SNA/Data/AnonymisedData/working data/baseline_full_network_with_missing.rdata")
load("T:/projects/stash_trial/09 STASH SNA/Data/AnonymisedData/working data/control_full_network_with_missing.rdata")
net <- combine.lists(control.full.network.with.missing,baseline.full.network.with.missing)
###Create table to hold descriptives
###Change nr to number of rows you add
desctable <- data.frame(matrix(NA, nc = 14, nr = 18))
colnames(desctable) <- c("Variable",
"Net1", "Net2", "Net3","Net4",
"Net5", "Net6", "Net7", "Net8",
"Net9", "Net 10", "Net11", "Net12",
"Total")
###add rows with labels here
desctable[1,1] <- "Pupils"
desctable[2,1] <- "Gender"
desctable[3,1] <- "Boy"
desctable[4,1] <- "Girl"
desctable[5,1] <- "Trans / Non-binary"
desctable[6,1] <- "Missing gender"
desctable[7,1] <- "Sexual activity missing"
desctable[8,1] <- "Inactive"
desctable[9,1] <- "Active no experience"
desctable[10,1] <- "Sexually active"
desctable[11,1] <- "Knowledge"
desctable[12,1] <- "Missing Knowledge"
desctable[13,1] <- "Attitudes"
desctable[14,1] <- "Missing Attitudes"
desctable[15,1] <- "Confidence"
desctable[16,1] <- "Missing confidence"
desctable[17,1] <- "Outside school friends"
desctable[18,1] <- "Non-responding pupils"
for (i in c(1:12)){
descnet <- net[[i]]
column <- i + 1
know <- as.numeric(get.vertex.attribute(descnet, "know.var"))
att <- as.numeric(get.vertex.attribute(descnet, "att.var"))
conf <- as.numeric(get.vertex.attribute(descnet, "conf.var"))
gender <- as.numeric(get.vertex.attribute(descnet, "gender"))
sex <- get.vertex.attribute(descnet, "sex.var")
outfrn <- as.numeric(get.vertex.attribute(descnet, "outschool.var"))
tot.in.network <- descnet$gal$n
grand.total <- c(grand.total,tot.in.network)
###Fill in table values
desctable[1,column] <- tot.in.network
#Gender
desctable[3,column] <- paste0(table(gender, useNA = "always")[1] ," (", round((table(gender, useNA = "always")[1] / tot.in.network) *100 ,1) ,")")
desctable[4,column] <- paste0(table(gender, useNA = "always")[2] ," (", round((table(gender, useNA = "always")[2] / tot.in.network) *100 ,1) ,")")
desctable[5,column] <- paste0(table(gender, useNA = "always")[3] ," (", round((table(gender, useNA = "always")[3] / tot.in.network) *100 ,1) ,")")
###If no trans students in the school, replace the above column with zero
if(length(table(gender, useNA = "always")) ==3) desctable[5,column] <- "0 (0)"
desctable[6,column] <- paste0(tail(table(gender, useNA = "always"),1) ," (", round((tail(table(gender, useNA = "always"),1) / tot.in.network) *100 ,1) ,")")
#Sexual activity
desctable[7,column] <- paste0(table(sex, useNA = "always")[4] ," (", round((table(sex, useNA = "always")[4] / tot.in.network) *100 ,1) ,")")
desctable[10,column] <- paste0(table(sex, useNA = "always")[1] ," (", round((table(sex, useNA = "always")[1] / tot.in.network) *100 ,1) ,")")
desctable[8,column] <- paste0(table(sex, useNA = "always")[2] ," (", round((table(sex, useNA = "always")[2] / tot.in.network) *100 ,1) ,")")
desctable[9,column] <- paste0(table(sex, useNA = "always")[3] ," (", round((table(sex, useNA = "always")[3] / tot.in.network) *100 ,1) ,")")
#Knowledge
desctable[11,column] <- paste0(round(mean(know, na.rm = T),2) ," (",round(sd(know, na.rm = T),2),")")
desctable[12,column] <- paste0(tail(table(know, useNA = "always"),1) ," (", round((tail(table(know, useNA = "always"),1) / tot.in.network) *100 ,1) ,")")
#Attitudes
desctable[13,column] <- paste0(round(mean(att, na.rm = T),2) ," (",round(sd(att, na.rm = T),2),")")
desctable[14,column] <- paste0(tail(table(att, useNA = "always"),1) ," (", round((tail(table(att, useNA = "always"),1) / tot.in.network) *100 ,1) ,")")
#Confidence
desctable[15,column] <- paste0(round(mean(conf, na.rm = T),2) ," (",round(sd(conf, na.rm = T),2),")")
desctable[16,column] <- paste0(tail(table(conf, useNA = "always"),1) ," (", round((tail(table(conf, useNA = "always"),1) / tot.in.network) *100 ,1) ,")")
#Outside school friends
desctable[17,column] <- paste0(round(mean(outfrn, na.rm = T),2) ," (",round(sd(outfrn, na.rm = T),2),")")
}
desctable
#Vectors to create grand total
grand.total <- 0
vec.total.boys <- 0
vec.total.girls <- 0
vec.total.trans <- 0
vec.gender.miss <- 0
vec.sex.miss <- 0
vec.inact.miss <- 0
vec.nosex.miss <- 0
vec.active.miss <- 0
for (i in c(1,2,3,5,6,7,8,9,11,12)){
descnet <- net[[i]]
column <- i + 1
know <- as.numeric(get.vertex.attribute(descnet, "know.var"))
att <- as.numeric(get.vertex.attribute(descnet, "att.var"))
conf <- as.numeric(get.vertex.attribute(descnet, "conf.var"))
gender <- as.numeric(get.vertex.attribute(descnet, "gender"))
sex <- get.vertex.attribute(descnet, "sex.var")
outfrn <- as.numeric(get.vertex.attribute(descnet, "outschool.var"))
tot.in.network <- descnet$gal$n
grand.total <- c(grand.total,tot.in.network)
vec.total.boys <- c(vec.total.boys, table(gender, useNA = "always")[1])
vec.total.girls <- c(vec.total.girls, table(gender, useNA = "always")[2])
vec.total.trans <- c(vec.total.trans, ifelse(length(table(gender, useNA = "always")) ==3, 0, table(gender, useNA = "always")[3]))
vec.gender.miss <- c(vec.gender.miss, tail(table(gender, useNA = "always"),1))
vec.sex.miss <- c(vec.sex.miss, tail(table(sex, useNA = "always"),1))
vec.active.miss <- c(vec.active.miss, table(sex, useNA = "always")[1])
vec.inact.miss <- c(vec.inact.miss, table(sex, useNA = "always")[2])
vec.nosex.miss <- c(vec.nosex.miss, table(sex, useNA = "always")[3])
}
####Do sex percep by hand
######################################################################
####################Total columns and absent pupils###################
######################################################################
############Load the networks dataset. The pupil count is higher than respondent count
load("T:/projects/stash_trial/09 STASH SNA/Data/AnonymisedData/working data/control_net_dataset.rdata")
load("T:/projects/stash_trial/09 STASH SNA/Data/AnonymisedData/working data/baseline_net_dataset.rdata")
c.net.desc.df <- rbind(control.net.dataset[[1]],
control.net.dataset[[2]],
control.net.dataset[[3]],
# control.net.dataset[[4]],
control.net.dataset[[5]],
control.net.dataset[[6]])
b.net.desc.df <- rbind(baseline.net.dataset[[1]],
baseline.net.dataset[[2]],
baseline.net.dataset[[3]],
# baseline.net.dataset[[4]],
baseline.net.dataset[[5]],
baseline.net.dataset[[6]])
c.net.desc.df$control <- 1
b.net.desc.df$control <- 0
b.net.desc.df$sex.percep.var <- NA
b.net.desc.df <- b.net.desc.df[,colnames(c.net.desc.df)]
net.desc.df <- rbind(c.net.desc.df, b.net.desc.df)
net.desc.df$know.var <- as.numeric(as.character(net.desc.df$know.var))
net.desc.df$att.var <- as.numeric(as.character(net.desc.df$att.var))
net.desc.df$conf.var <- as.numeric(as.character(net.desc.df$conf.var))
for (i in 1:dim(net.desc.df)[2]){
net.desc.df[,i] <- as.numeric(net.desc.df[,i])
}
count_na <- function(x) sum(is.na(x))
#View(net.desc.df)
net.desc.df$misscount <- NULL
for (i in 1:dim(net.desc.df)[1]){
net.desc.df$misscount[i] <- sum(is.na(net.desc.df[i,c(2:9)]))
}
net.desc.df$misscount
net.desc.df$nodemiss <- net.desc.df$misscount==7
missing <- rep(NA,12)
missing[1] <- table(net.desc.df$nodemiss[which(net.desc.df$control==1 & net.desc.df$school.id == 1)])[2]
missing[2] <- table(net.desc.df$nodemiss[which(net.desc.df$control==1 & net.desc.df$school.id == 2)])[2]
missing[3] <- table(net.desc.df$nodemiss[which(net.desc.df$control==1 & net.desc.df$school.id == 3)])[2]
#missing[4] <- table(net.desc.df$nodemiss[which(net.desc.df$control==1 & net.desc.df$school.id == 4)])[2]
missing[5] <- table(net.desc.df$nodemiss[which(net.desc.df$control==1 & net.desc.df$school.id == 5)])[2]
missing[6] <- table(net.desc.df$nodemiss[which(net.desc.df$control==1 & net.desc.df$school.id == 6)])[2]
missing[7] <- table(net.desc.df$nodemiss[which(net.desc.df$control==0 & net.desc.df$school.id == 1)])[2]
missing[8] <- table(net.desc.df$nodemiss[which(net.desc.df$control==0 & net.desc.df$school.id == 2)])[2]
missing[9] <- table(net.desc.df$nodemiss[which(net.desc.df$control==0 & net.desc.df$school.id == 3)])[2]
#missing[10] <- table(net.desc.df$nodemiss[which(net.desc.df$control==0 & net.desc.df$school.id == 4)])[2]
missing[11] <- table(net.desc.df$nodemiss[which(net.desc.df$control==0 & net.desc.df$school.id == 5)])[2]
missing[12] <- table(net.desc.df$nodemiss[which(net.desc.df$control==0 & net.desc.df$school.id == 6)])[2]
#####################################################
## Total columns ##
###Take total sample from the tables, as this includes the not present
desctable[1,14] <- sum(grand.total)
total.sample <- sum(grand.total)
total.boys <- sum(vec.total.boys)
total.girls <- sum(vec.total.girls)
total.trans <- sum(vec.total.trans)
gender.miss <- sum(vec.gender.miss)
sex.miss <- sum(vec.sex.miss)
inact.miss <- sum(vec.inact.miss)
nosex.miss <- sum(vec.nosex.miss)
active.miss <- sum(vec.active.miss)
totcol <- function(input = NULL){
x <- paste0(input," (", round((input / total.sample * 100),2),")")
return(x)
}
desctable[1,14] <- total.sample
desctable[3,14] <- totcol(total.boys)
desctable[4,14] <- totcol(total.girls)
desctable[5,14] <- totcol(total.trans)
desctable[6,14] <- totcol(gender.miss)
desctable[7,14] <- totcol(sex.miss)
desctable[8,14] <- totcol(inact.miss)
desctable[9,14] <- totcol(nosex.miss)
desctable[10,14] <- totcol(active.miss)
###Drop the omitted schools
desctable <- desctable[,c(1,2,3,4,6,7,8,9,10,12,13,14)]
know.missing <- sum( as.numeric(substr(desctable[12,2:11], 1,2)) )
att.missing <- sum( as.numeric(substr(desctable[14,2:11], 1,2)) )
conf.missing <- sum( as.numeric(substr(desctable[16,2:11], 1,2)) )
paste0("Table 1 knowledge mean (se): ",(round( mean(net.desc.df$know.var, na.rm = T) , 2)),
" (",round( sd(net.desc.df$know.var, na.rm = T) , 2),")")
desctable[11,12] <- paste0(round( mean(net.desc.df$know.var, na.rm = T) , 2),
" (",round( sd(net.desc.df$know.var, na.rm = T) , 2),")")
paste0("Table 1 knowledge missing: ",know.missing," (",
round(know.missing/total.sample,2),")")
desctable[12,12] <- paste0(know.missing," (",
round(know.missing/total.sample,2),")")
paste0("Table 1 att mean (se): ",(round( mean(net.desc.df$att.var, na.rm = T) , 2)),
" (",round( sd(net.desc.df$att.var, na.rm = T) , 2),")")
desctable[13,12] <- paste0(round( mean(net.desc.df$att.var, na.rm = T) , 2),
" (",round( sd(net.desc.df$att.var, na.rm = T) , 2),")")
paste0("Table 1 att missing: ",att.missing," (",round(att.missing/total.sample,2),")")
desctable[14,12] <- paste0(att.missing," (",round(att.missing/total.sample,2),")")
paste0("Table 1 conf mean (se): ",(round( mean(net.desc.df$conf.var, na.rm = T) , 2))," (",round( sd(net.desc.df$conf.var, na.rm = T) , 2),")")
desctable[15,12] <- paste0(round( mean(net.desc.df$conf.var, na.rm = T) , 2)," (",round( sd(net.desc.df$conf.var, na.rm = T) , 2),")")
paste0("Table 1 conf missing: ",conf.missing," (",round(conf.missing/total.sample,2),")")
desctable[16,12] <- paste0(conf.missing," (",round(conf.missing/total.sample,2),")")
grand.total <- grand.total[2:13]
#desctable[18,] <- NA
col <- 1
counter <- 0
for (i in c(1,2,3,5,6,7,8,9,11,12)){
col <- col + 1
counter <- counter + 1
desctable[18,col] <- paste0(missing[[i]]," (", round(missing[[i]] / grand.total[[counter]] * 100,2),")")
}
desctable[18, 12] <-
paste0(sum(missing[c(1, 2, 3, 5, 6, 7, 8, 9, 11, 12)]), " (", round(sum(missing[c(1, 2, 3, 5, 6, 7, 8, 9, 11, 12)]) / total.sample * 100, 2), ")")
setwd("T:/projects/stash_trial/09 STASH SNA/DisseminationAndImpact/Manuscripts_Papers/Soc Sex Dev Paper")
write.csv(desctable, file = "Control school descriptives - omit 4 schools.csv")