-
Notifications
You must be signed in to change notification settings - Fork 0
/
S5_O2_prep.Rmd
658 lines (564 loc) · 33.5 KB
/
S5_O2_prep.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
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
---
title: "STEPS Study: Data preparation for analysis of O2"
author: "C. Konstantinou and X. Andrianou @ Water and Health Laboratory, CII-CUT"
date: "`r format(Sys.time(), '%d %B, %Y')`"
output:
rmdformats::downcute:
self_contained: true
thumbnails: true
lightbox: true
gallery: false
highlight: tango
editor_options:
chunk_output_type: console
---
```{r packages, echo=FALSE, include=FALSE}
rm(list=ls())
ipak <- function(pkg){
new.pkg <- pkg[!(pkg %in% installed.packages()[, "Package"])]
if (length(new.pkg))
install.packages(new.pkg, dependencies = TRUE,
repos = "http://cran.us.r-project.org")
sapply(pkg, require, character.only = TRUE)
}
# usage
packages <- c("tidyverse","lubridate","officer", "rvg","glue", "patchwork","janitor",
"tableone","dplyr","lme4","readxl","scales","readr", "lubridate", "knitr")
ipak(packages)
```
# Dataset preparation for objective 2
```{r readdata, echo=FALSE, message=FALSE, warning=FALSE}
# read questionnaire_sensor files
questPA <- readRDS("produced_data/questPA.rds", refhook = NULL)
questMCF <- readRDS("produced_data/questMCF.rds", refhook = NULL)
```
```{r dataprep, echo=FALSE, message=FALSE, warning=FALSE}
# rename variables so that names are similar in MCF and PA files
questMCF <- questMCF %>%
dplyr::rename(EESTDateTime = time, final_value = value)
questPA <- questPA %>%
dplyr::rename(name = PM)
# create variables needed for models
questPA_1 <- questPA %>%
# create hour variable - check that it is correct
mutate(hour=case_when(time_hm >= 0 & time_hm < 1 ~ "1",
time_hm >= 1 & time_hm < 2 ~ "2",
time_hm >= 2 & time_hm < 3 ~ "3",
time_hm >= 3 & time_hm < 4 ~ "4",
time_hm >= 4 & time_hm < 5 ~ "5",
time_hm >= 5 & time_hm < 6 ~ "6",
time_hm >= 6 & time_hm < 7 ~ "7",
time_hm >= 7 & time_hm < 8 ~ "8",
time_hm >= 8 & time_hm < 9 ~ "9",
time_hm >= 9 & time_hm < 10 ~ "10",
time_hm >= 10 & time_hm < 11 ~ "11",
time_hm >= 11 & time_hm < 12 ~ "12",
time_hm >= 12 & time_hm < 13 ~ "13",
time_hm >= 13 & time_hm < 14 ~ "14",
time_hm >= 14 & time_hm < 15 ~ "15",
time_hm >= 15 & time_hm < 16 ~ "16",
time_hm >= 16 & time_hm < 17 ~ "17",
time_hm >= 17 & time_hm < 18 ~ "18",
time_hm >= 18 & time_hm < 19 ~ "19",
time_hm >= 19 & time_hm < 20 ~ "20",
time_hm >= 20 & time_hm < 21 ~ "21",
time_hm >= 21 & time_hm < 22 ~ "22",
time_hm >= 22 & time_hm < 23 ~ "23",
time_hm >= 23 ~ "24")) %>%
# create period for times between 7:00 - 7:45 & 13:05 - 15:00
mutate(period_new = case_when(time_hm>= "7" & time_hm < "7.75" ~ "bef_classes",
time_hm>= "13.1" & time_hm < "15" ~ "after_classes",
TRUE ~ period_win)) %>%
# add period start time for before and after classes
mutate(period_start_time = case_when(period_new == "bef_classes" ~ "7:00",
period_new == "after_classes" ~ "13:05",
TRUE ~ period_start)) %>%
# for no school hours add O in open_closed_sum and 0 in percent_open, assuming that windows are closed in no school hours
mutate(open_closed_sum_new = case_when(period_new == "no_school" ~ "0",
TRUE ~ as.character(open_closed_sum)),
percent_open_new = case_when(period_new == "no_school" ~ "0",
TRUE ~ as.character(percent_open)),
# create time_type so that you can separate class hours, breaks, no school hours
time_type = case_when(period_new == "5" | period_new == "10" | period_new == "15" ~ "break",
period_new == "no_school" ~ "no_school",
period_new == "bef_classes" ~ "bef_classes",
period_new == "after_classes" ~ "after_classes",
TRUE ~ "class")) %>%
separate(EESTDateTime,
into = c("remove", "samplingtime"),
sep = " ", remove = FALSE, extra = "merge", fill = "right") %>%
select(-remove) %>%
#added a date to the time
mutate(samplingtime = as.POSIXct(glue("2020-01-01 {samplingtime}")))
# create variables needed for models
questMCF_1 <- questMCF %>%
# create hour variable - check that it is correct
mutate(hour=case_when(time_hm >= 0 & time_hm < 1 ~ "1",
time_hm >= 1 & time_hm < 2 ~ "2",
time_hm >= 2 & time_hm < 3 ~ "3",
time_hm >= 3 & time_hm < 4 ~ "4",
time_hm >= 4 & time_hm < 5 ~ "5",
time_hm >= 5 & time_hm < 6 ~ "6",
time_hm >= 6 & time_hm < 7 ~ "7",
time_hm >= 7 & time_hm < 8 ~ "8",
time_hm >= 8 & time_hm < 9 ~ "9",
time_hm >= 9 & time_hm < 10 ~ "10",
time_hm >= 10 & time_hm < 11 ~ "11",
time_hm >= 11 & time_hm < 12 ~ "12",
time_hm >= 12 & time_hm < 13 ~ "13",
time_hm >= 13 & time_hm < 14 ~ "14",
time_hm >= 14 & time_hm < 15 ~ "15",
time_hm >= 15 & time_hm < 16 ~ "16",
time_hm >= 16 & time_hm < 17 ~ "17",
time_hm >= 17 & time_hm < 18 ~ "18",
time_hm >= 18 & time_hm < 19 ~ "19",
time_hm >= 19 & time_hm < 20 ~ "20",
time_hm >= 20 & time_hm < 21 ~ "21",
time_hm >= 21 & time_hm < 22 ~ "22",
time_hm >= 22 & time_hm < 23 ~ "23",
time_hm >= 23 ~ "24")) %>%
# create period for times between 7:00 - 7:45 & 13:05 - 15:00 - School open by cleaning ladies
mutate(period_new = case_when(time_hm>= "7" & time_hm < "7.75" ~ "bef_classes",
time_hm>= "13.1" & time_hm < "15" ~ "after_classes",
TRUE ~ period_win)) %>%
# add period start time for before and after classes
mutate(period_start_time = case_when(period_new == "bef_classes" ~ "7:00",
period_new == "after_classes" ~ "13:05",
TRUE ~ period_start)) %>%
# for no school hours add O in open_closed_sum and 0 in percent_open, assuming that windows are closed in no school hours
mutate(open_closed_sum_new = case_when(period_new == "no_school" ~ "0",
TRUE ~ as.character(open_closed_sum)),
percent_open_new = case_when(period_new == "no_school" ~ "0",
TRUE ~ as.character(percent_open)),
# create time_type so that you can separate class hours, breaks, no school hours
time_type = case_when(period_new == "5" | period_new == "10" | period_new == "15" ~ "break",
period_new == "no_school" ~ "no_school",
period_new == "bef_classes" ~ "bef_classes",
period_new == "after_classes" ~ "after_classes",
TRUE ~ "class")) %>%
# create sampling time variable to use for plots..
separate(EESTDateTime,
into = c("remove", "samplingtime"),
sep = " ", remove = FALSE, extra = "merge", fill = "right") %>%
select(-remove) %>%
#added a date to the time
mutate(samplingtime = as.POSIXct(glue("2020-01-01 {samplingtime}")))
saveRDS(questPA_1, file="produced_data/questPA_1.rds")
saveRDS(questMCF_1, file="produced_data/questMCF_1.rds")
```
Period mean for indoor measurements
```{r readdata_part2, echo=FALSE, message=FALSE, warning=FALSE}
# read study log file
Study_log_210510_DA_copy <- read_excel("rawData/Study_log_210510_DA_copy.xlsx") %>%
clean_names() %>%
dplyr::rename(SchoolID=school_id)
# read headmasters questionnaire file so that to add selected variables for regression models
q_headmasters <- readRDS("produced_data/q_headmasters.rds") %>%
dplyr::rename(chlorine_freq = chlorium_2)
```
```{r dataset_reg_prep_only_school_PA, echo=FALSE, message=FALSE, warning=FALSE}
# dataset for regression with only school hours including 6:30 - 15:00
dataset_prep_for_regression <- questPA_1 %>%
# filter only school opening closing hours
filter(period_new!="no_school") %>%
# calculate period mean
group_by(name, SchoolID, indoor_outdoor, location, samplingdate, period_new, time_type, period_start_time) %>%
dplyr::summarise(period_mean=mean(final_value, na.rm=TRUE), .groups="drop")
# mean for outdoor
dataset_regression_outdoor <- dataset_prep_for_regression %>%
filter(indoor_outdoor=="outdoors") %>%
dplyr::rename(mean_outdoors=period_mean) %>%
select(name, SchoolID, samplingdate, period_new, mean_outdoors)
# dataset with windows
dataset_PMQUEST_for_regression <- questPA_1 %>%
filter(class_venttype == "window") %>%
# calculate period mean
group_by(name, SchoolID, indoor_outdoor, location, samplingdate, period_new) %>%
dplyr::summarise(open_closed_sum_new=mean(as.numeric(open_closed_sum_new)),
percent_open_new=mean(as.numeric(percent_open_new)),
.groups="drop")
# dataset with doors
dataset_PMQUEST_for_regression_d <- questPA_1 %>%
filter(class_venttype == "door") %>%
# calculate period mean
group_by(name, SchoolID, indoor_outdoor, location, samplingdate, period_new) %>%
dplyr::summarise(open_closed_sum_new_doors=mean(as.numeric(open_closed_sum_new)),
percent_open_new_doors=mean(as.numeric(percent_open_new)),
.groups="drop")
# dataset with fans
dataset_PMQUEST_for_regression_f <- questPA_1 %>%
filter(class_venttype == "fan") %>%
# calculate period mean
group_by(name, SchoolID, indoor_outdoor, location, samplingdate, period_new) %>%
dplyr::summarise(open_closed_sum_new_fans=mean(as.numeric(open_closed_sum_new)),
percent_open_new_fans=mean(as.numeric(percent_open_new)),
.groups="drop")
# mean for indoor
dataset_regression_indoor <- dataset_prep_for_regression %>%
filter(indoor_outdoor=="indoors") %>%
dplyr::rename(mean_indoors=period_mean) %>%
select(name, SchoolID, location, samplingdate, period_new, period_start_time, mean_indoors, time_type) %>%
# merge with dataset with windows data
left_join(., select(dataset_PMQUEST_for_regression, name, SchoolID, location,
samplingdate, period_new,
open_closed_sum_new, percent_open_new),
by = c("name", "SchoolID",
"samplingdate", "location", "period_new")) %>%
# merge with dataset with fans data
left_join(., select(dataset_PMQUEST_for_regression_f, name, SchoolID, location,
samplingdate, period_new,
open_closed_sum_new_fans, percent_open_new_fans),
by = c("name", "SchoolID",
"samplingdate", "location", "period_new")) %>%
# merge with dataset with doors data
left_join(., select(dataset_PMQUEST_for_regression_d, name, SchoolID, location,
samplingdate, period_new,
open_closed_sum_new_doors, percent_open_new_doors),
by = c("name", "SchoolID",
"samplingdate", "location", "period_new"))
# final dataset for regression
dataset_final_regression <- left_join(dataset_regression_indoor,
dataset_regression_outdoor,
by = c("name", "SchoolID",
"samplingdate", "period_new")) %>%
left_join(., select(Study_log_210510_DA_copy, SchoolID, exclude, district, degurba), by="SchoolID") %>%
mutate(degurba_classification=case_when(degurba=="1"~"Densely populated areas",
degurba=="2"~"Intermediate density areas")) %>%
left_join(., select(q_headmasters, SchoolID, paint_places, chlorine_freq), by="SchoolID") %>%
mutate(chlorinefreq = case_when(chlorine_freq == "Once_week" ~ "<=Three times_week",
chlorine_freq == "Twice_week" ~ "<=Three times_week",
chlorine_freq == "Three times_week" ~ "<=Three times_week",
TRUE ~ "Five times_week")) %>%
mutate(paint_indoors = case_when(paint_places == "Indoors" ~ "Yes",
paint_places == "Indoors_and_Outdoors" ~ "Yes",
TRUE ~ "No")) %>%
mutate(location=as.factor(location),
period_new=as.factor(period_new),
time_type=as.factor(time_type),
district=as.factor(district),
degurba_classification=as.factor(degurba_classification),
name=as.factor(name),
paint_indoors=as.factor(paint_indoors),
chlorine_freq=as.factor(chlorine_freq),
chlorinefreq=as.factor(chlorinefreq),
period_start_time = as.POSIXct(period_start_time,format="%H:%M"))
dataset_final_regression$period_new = factor(dataset_final_regression$period_new,
levels = c("bef_classes", "1", "2", "3", "4", "5", "6",
"7", "8", "9", "10", "after_classes"))
# relevel time_type, district, chlorine_freq
dataset_final_regression <- within(dataset_final_regression, time_type <- relevel(time_type, ref = "class"))
dataset_final_regression <- within(dataset_final_regression, district <- relevel(district, ref = "Nicosia"))
# prepare exclusions index dataset with classes that are deemed problematic based on teachers
exclusions_index <- Study_log_210510_DA_copy %>%
select(SchoolID, exclude) %>%
filter(!is.na(exclude)) %>%
separate(exclude, into=c("one", "two"), sep = "_") %>%
pivot_longer(cols = c("one", "two"), values_to = "classroom_to_exclude") %>%
filter(!is.na(classroom_to_exclude)) %>%
select(-name) %>%
mutate(M_codes=paste0(SchoolID, "_", classroom_to_exclude, "_M"),
P_codes=paste0(SchoolID, "_", classroom_to_exclude, "_P"),
school_class=paste0(SchoolID, "_", classroom_to_exclude),
exclusion="yes")
# dataset with group for problematic and normal classes based on teachers assessment
dataset_final_regression_probandnorm <- dataset_final_regression %>%
mutate(exclude_final=case_when(exclude=="C1_C2"~"school",
exclude=="C1"|exclude=="C2"~"one classroom",
TRUE~NA_character_)) %>%
mutate(school_class=paste0(SchoolID, "_", location)) %>%
mutate(class_type = case_when(exclude_final == "school" |exclude_final=="one classroom" ~ "problematic",
TRUE~"normal"))
# descriptive table for problematic and normal classes
table_PA_normprob <- dataset_final_regression_probandnorm %>%
filter(period_new!="bef_classes" & period_new!="after_classes") %>%
group_by(name, class_type) %>%
dplyr::summarise(n=n(),
mean=round(mean(mean_indoors, na.rm=TRUE), digits=1),
sd=round(sd(mean_indoors, na.rm=TRUE), digits=1),
min=round(min(mean_indoors, na.rm=TRUE), digits=1),
p25=round(quantile(mean_indoors, probs=0.25, na.rm=TRUE), digits=1),
median=round(median(mean_indoors, na.rm=TRUE), digits=1),
p75=round(quantile(mean_indoors, probs=0.75, na.rm=TRUE), digits=1),
max=round(max(mean_indoors, na.rm=TRUE), digits=1), .groups="drop")
# dataset where problematic classes based on teachers assessment are excluded
dataset_final_regression_excl <- dataset_final_regression %>%
mutate(exclude_final=case_when(exclude=="C1_C2"~"school",
exclude=="C1"|exclude=="C2"~"one classroom",
TRUE~NA_character_)) %>%
mutate(school_class=paste0(SchoolID, "_", location)) %>%
# exclude classes based on exclusions index
filter(!(school_class %in% exclusions_index$school_class))
saveRDS(dataset_final_regression, file="produced_data/dataset_final_regression.rds")
saveRDS(dataset_final_regression_excl, file="produced_data/dataset_final_regression_excl.rds")
saveRDS(dataset_final_regression_probandnorm, file="produced_data/dataset_final_regression_probandnorm.rds")
dataset_final_regression_onlysch <- dataset_final_regression %>%
filter(time_type == "break" | time_type == "class")
dataset_final_regression_excl_onlysch <- dataset_final_regression_excl %>%
filter(time_type == "break" | time_type == "class")
saveRDS(dataset_final_regression_onlysch, file="produced_data/dataset_final_regression_onlysch.rds")
saveRDS(dataset_final_regression_excl_onlysch, file="produced_data/dataset_final_regression_excl_onlysch.rds")
```
### Comparison of "normal" and "problematic" classes based on teachers' assessment
`r kable(print(table_PA_normprob), caption = "PA data (prob-normal classes comparison)")`
```{r dataset_reg_prep_only_school_MCF, echo=FALSE, message=FALSE, warning=FALSE}
rm(list = setdiff(ls(), c("dataset_final_regression","dataset_final_regression_excl",
"questPA_1","questMCF_1","Study_log_210510_DA_copy",
"exclusions_index","q_headmasters")))
# dataset for regression with only school hours including 6:30 - 15:00
dataset_prep_for_regression_M <- questMCF_1 %>%
# filter only school opening closing hours
filter(period_new!="no_school") %>%
# calculate period mean
group_by(name, SchoolID, indoor_outdoor, location, samplingdate, period_new, period_start_time, time_type) %>%
dplyr::summarise(period_mean=mean(final_value, na.rm=TRUE), .groups="drop")
# mean for outdoor
dataset_regression_outdoor_M <- dataset_prep_for_regression_M %>%
filter(indoor_outdoor=="outdoors") %>%
dplyr::rename(mean_outdoors=period_mean) %>%
select(name, SchoolID, samplingdate, period_new, mean_outdoors)
# dataset with windows
dataset_MQUEST_for_regression <- questMCF_1 %>%
filter(class_venttype == "window") %>%
# calculate period mean
group_by(name, SchoolID, indoor_outdoor, location, samplingdate, period_new) %>%
dplyr::summarise(open_closed_sum_new=mean(as.numeric(open_closed_sum_new)),
percent_open_new=mean(as.numeric(percent_open_new)),
.groups="drop")
# dataset with doors
dataset_MQUEST_for_regression_d <- questMCF_1 %>%
filter(class_venttype == "door") %>%
# calculate period mean
group_by(name, SchoolID, indoor_outdoor, location, samplingdate, period_new) %>%
dplyr::summarise(open_closed_sum_new_doors=mean(as.numeric(open_closed_sum_new)),
percent_open_new_doors=mean(as.numeric(percent_open_new)),
.groups="drop")
# dataset with fans
dataset_MQUEST_for_regression_f <- questMCF_1 %>%
filter(class_venttype == "fan") %>%
# calculate period mean
group_by(name, SchoolID, indoor_outdoor, location, samplingdate, period_new) %>%
dplyr::summarise(open_closed_sum_new_fans=mean(as.numeric(open_closed_sum_new)),
percent_open_new_fans=mean(as.numeric(percent_open_new)),
.groups="drop")
# mean for indoor
dataset_regression_indoor_M <- dataset_prep_for_regression_M %>%
filter(indoor_outdoor=="indoors") %>%
dplyr::rename(mean_indoors=period_mean) %>%
select(name, SchoolID, location, samplingdate, period_new, period_start_time, mean_indoors, time_type) %>%
# merge with dataset with windows data
left_join(., select(dataset_MQUEST_for_regression, name, SchoolID, location,
samplingdate, period_new, percent_open_new,
open_closed_sum_new),
by = c("name", "SchoolID",
"samplingdate", "location", "period_new")) %>%
# merge with dataset with fans data
left_join(., select(dataset_MQUEST_for_regression_f, name, SchoolID, location,
samplingdate, period_new,
open_closed_sum_new_fans,percent_open_new_fans),
by = c("name", "SchoolID",
"samplingdate", "location", "period_new")) %>%
# merge with dataset with doors data
left_join(., select(dataset_MQUEST_for_regression_d, name, SchoolID, location,
samplingdate, period_new,
open_closed_sum_new_doors,percent_open_new_doors),
by = c("name", "SchoolID",
"samplingdate", "location", "period_new"))
# final dataset for regression
dataset_final_regression_M <- left_join(dataset_regression_indoor_M,
dataset_regression_outdoor_M,
by = c("name", "SchoolID",
"samplingdate", "period_new")) %>%
left_join(., select(Study_log_210510_DA_copy, SchoolID, exclude, district, degurba), by="SchoolID") %>%
mutate(degurba_classification=case_when(degurba=="1"~"Densely populated areas",
degurba=="2"~"Intermediate density areas")) %>%
left_join(., select(q_headmasters, SchoolID, paint_places, chlorine_freq), by="SchoolID") %>%
mutate(chlorinefreq = case_when(chlorine_freq == "Once_week" ~ "<=Three times_week",
chlorine_freq == "Twice_week" ~ "<=Three times_week",
chlorine_freq == "Three times_week" ~ "<=Three times_week",
TRUE ~ "Five times_week")) %>%
mutate(paint_indoors = case_when(paint_places == "Indoors" ~ "Yes",
paint_places == "Indoors_and_Outdoors" ~ "Yes",
TRUE ~ "No")) %>%
mutate(location=as.factor(location),
period_new=as.factor(period_new),
time_type=as.factor(time_type),
district=as.factor(district),
degurba_classification=as.factor(degurba_classification),
name=as.factor(name),
paint_indoors=as.factor(paint_indoors),
chlorine_freq=as.factor(chlorine_freq),
chlorinefreq=as.factor(chlorinefreq),
period_start_time = as.POSIXct(period_start_time,format="%H:%M"))
dataset_final_regression_M$period_new = factor(dataset_final_regression_M$period_new,
levels = c("bef_classes", "1", "2", "3", "4", "5", "6",
"7", "8", "9", "10", "after_classes"))
# relevel time_type, district
dataset_final_regression_M <- within(dataset_final_regression_M, time_type <- relevel(time_type, ref = "class"))
dataset_final_regression_M <- within(dataset_final_regression_M, district <- relevel(district, ref = "Nicosia"))
# dataset with group for problematic and normal classes based on teachers assessment
dataset_final_regression_M_probandnorm <- dataset_final_regression_M %>%
mutate(exclude_final=case_when(exclude=="C1_C2"~"school",
exclude=="C1"|exclude=="C2"~"one classroom",
TRUE~NA_character_)) %>%
mutate(school_class=paste0(SchoolID, "_", location)) %>%
mutate(class_type = case_when(exclude_final == "school" |exclude_final=="one classroom" ~ "problematic",
TRUE~"normal"))
# descriptive table for problematic and normal classes
table_MCF_normprob <- dataset_final_regression_M_probandnorm %>% filter(period_new!="bef_classes" & period_new!="after_classes") %>%
group_by(name, class_type) %>%
dplyr::summarise(n=n(),
mean=round(mean(mean_indoors, na.rm=TRUE), digits=1),
sd=round(sd(mean_indoors, na.rm=TRUE), digits=1),
min=round(min(mean_indoors, na.rm=TRUE), digits=1),
p25=round(quantile(mean_indoors, probs=0.25, na.rm=TRUE), digits=1),
median=round(median(mean_indoors, na.rm=TRUE), digits=1),
p75=round(quantile(mean_indoors, probs=0.75, na.rm=TRUE), digits=1),
max=round(max(mean_indoors, na.rm=TRUE), digits=1), .groups="drop")
# dataset where problematic classes based on teachers assessment are excluded
dataset_final_regression_excl_M <- dataset_final_regression_M %>%
mutate(exclude_final=case_when(exclude=="C1_C2"~"school",
exclude=="C1"|exclude=="C2"~"one classroom",
TRUE~NA_character_)) %>%
mutate(school_class=paste0(SchoolID, "_", location)) %>%
# exclude classes based on exclusions index
filter(!(school_class %in% exclusions_index$school_class))
saveRDS(dataset_final_regression_M, file="produced_data/dataset_final_regression_M.rds")
saveRDS(dataset_final_regression_excl_M, file="produced_data/dataset_final_regression_excl_M.rds")
saveRDS(dataset_final_regression_M_probandnorm, file="produced_data/dataset_final_regression_M_probandnorm.rds")
dataset_final_regression_onlysch_M <- dataset_final_regression_M %>%
filter(time_type == "break" | time_type == "class")
dataset_final_regression_excl_onlysch_M <- dataset_final_regression_excl_M %>%
filter(time_type == "break" | time_type == "class")
saveRDS(dataset_final_regression_onlysch_M, file="produced_data/dataset_final_regression_onlysch_M.rds")
saveRDS(dataset_final_regression_excl_onlysch_M, file="produced_data/dataset_final_regression_excl_onlysch_M.rds")
```
`r kable(print(table_MCF_normprob), caption = "MCF data (prob-normal classes comparison)")`
```{r dataset_reg_prep_hourly_PA, echo=FALSE}
rm(list = setdiff(ls(), c("questPA_1","questMCF_1","Study_log_210510_DA_copy",
"exclusions_index","q_headmasters")))
# dataset for regression with all datapoints
dataset_prep_for_regression_all <- questPA_1 %>%
# filter(period == "no_school") %>%
# calculate hour mean
group_by(name, SchoolID, indoor_outdoor, location, samplingdate, hour) %>%
dplyr::summarise(hourly_mean=mean(final_value, na.rm=TRUE), .groups="drop")
# mean for outdoor
dataset_regression_outdoor_all <- dataset_prep_for_regression_all %>%
filter(indoor_outdoor=="outdoors") %>%
dplyr::rename(mean_outdoors=hourly_mean) %>%
select(name, SchoolID, samplingdate, hour, mean_outdoors)
# mean for indoor
dataset_regression_indoor_all <- dataset_prep_for_regression_all %>%
filter(indoor_outdoor=="indoors") %>%
dplyr::rename(mean_indoors=hourly_mean) %>%
select(name, SchoolID, location, samplingdate, hour, mean_indoors)
# final dataset for regression
dataset_final_regression_all <- left_join(dataset_regression_indoor_all,
dataset_regression_outdoor_all,
by = c("name", "SchoolID",
"samplingdate", "hour")) %>%
left_join(., select(Study_log_210510_DA_copy, SchoolID, exclude, district, degurba), by="SchoolID") %>%
mutate(degurba_classification=case_when(degurba=="1"~"Densely populated areas",
degurba=="2"~"Intermediate density areas")) %>%
left_join(., select(q_headmasters, SchoolID, paint_places, chlorine_freq), by="SchoolID") %>%
mutate(chlorinefreq = case_when(chlorine_freq == "Once_week" ~ "<=Three times_week",
chlorine_freq == "Twice_week" ~ "<=Three times_week",
chlorine_freq == "Three times_week" ~ "<=Three times_week",
TRUE ~ "Five times_week")) %>%
mutate(paint_indoors = case_when(paint_places == "Indoors" ~ "Yes",
paint_places == "Indoors_and_Outdoors" ~ "Yes",
TRUE ~ "No")) %>%
mutate(location=as.factor(location),
hour=as.factor(hour),
district=as.factor(district),
degurba_classification=as.factor(degurba_classification),
name=as.factor(name),
paint_indoors=as.factor(paint_indoors),
chlorine_freq=as.factor(chlorine_freq),
chlorinefreq=as.factor(chlorinefreq))
#relevel hour variable
dataset_final_regression_all$hour = factor(dataset_final_regression_all$hour,
levels = c("1", "2", "3", "4", "5", "6",
"7", "8", "9", "10", "11","12",
"13","14","15","16","17",
"18","19","20","21","22","23","24"))
# relevel district
dataset_final_regression_all <- within(dataset_final_regression_all, district <- relevel(district, ref = "Nicosia"))
# dataset where problematic classes based on teachers assessment are excluded
dataset_final_regression_excl_all <- dataset_final_regression_all %>%
mutate(exclude_final=case_when(exclude=="C1_C2"~"school",
exclude=="C1"|exclude=="C2"~"one classroom",
TRUE~NA_character_)) %>%
mutate(school_class=paste0(SchoolID, "_", location)) %>%
# exclude classes
filter(!(school_class %in% exclusions_index$school_class))
saveRDS(dataset_final_regression_all, file="produced_data/dataset_final_regression_all.rds")
saveRDS(dataset_final_regression_excl_all, file="produced_data/dataset_final_regression_excl_all.rds")
```
```{r dataset_reg_prep_hourly_MCF, echo=FALSE}
rm(list = setdiff(ls(), c("questPA_1","questMCF_1",
"Study_log_210510_DA_copy","exclusions_index"
,"q_headmasters")))
# dataset for regression with no school hours
dataset_prep_for_regression_all_M <- questMCF_1 %>%
# filter(period == "no_school") %>%
# calculate hour mean
group_by(name, SchoolID, indoor_outdoor, location, samplingdate, hour) %>%
dplyr::summarise(hourly_mean=mean(final_value, na.rm=TRUE), .groups="drop")
# mean for outdoor
dataset_regression_outdoor_all_M <- dataset_prep_for_regression_all_M %>%
filter(indoor_outdoor=="outdoors") %>%
dplyr::rename(mean_outdoors=hourly_mean) %>%
select(name, SchoolID, samplingdate, hour, mean_outdoors)
# mean for indoor
dataset_regression_indoor_all_M <- dataset_prep_for_regression_all_M %>%
filter(indoor_outdoor=="indoors") %>%
dplyr::rename(mean_indoors=hourly_mean) %>%
select(name, SchoolID, location, samplingdate, hour, mean_indoors)
# final dataset for regression
dataset_final_regression_all_M <- left_join(dataset_regression_indoor_all_M,
dataset_regression_outdoor_all_M,
by = c("name", "SchoolID",
"samplingdate", "hour")) %>%
left_join(., select(Study_log_210510_DA_copy, SchoolID, exclude, district, degurba), by="SchoolID") %>%
mutate(degurba_classification=case_when(degurba=="1"~"Densely populated areas",
degurba=="2"~"Intermediate density areas")) %>%
left_join(., select(q_headmasters, SchoolID, paint_places, chlorine_freq), by="SchoolID") %>%
mutate(chlorinefreq = case_when(chlorine_freq == "Once_week" ~ "<=Three times_week",
chlorine_freq == "Twice_week" ~ "<=Three times_week",
chlorine_freq == "Three times_week" ~ "<=Three times_week",
TRUE ~ "Five times_week")) %>%
mutate(paint_indoors = case_when(paint_places == "Indoors" ~ "Yes",
paint_places == "Indoors_and_Outdoors" ~ "Yes",
TRUE ~ "No")) %>%
mutate(location=as.factor(location),
hour=as.factor(hour),
district=as.factor(district),
degurba_classification=as.factor(degurba_classification),
name=as.factor(name),
paint_indoors=as.factor(paint_indoors),
chlorine_freq=as.factor(chlorine_freq),
chlorinefreq=as.factor(chlorinefreq))
#relevel hour variable
dataset_final_regression_all_M$hour = factor(dataset_final_regression_all_M$hour,
levels = c("1", "2", "3", "4", "5", "6",
"7", "8", "9", "10", "11","12",
"13","14","15","16","17",
"18","19","20","21","22","23","24"))
# relevel district
dataset_final_regression_all_M <- within(dataset_final_regression_all_M, district <- relevel(district, ref = "Nicosia"))
# dataset where problematic classes based on teachers assessment are excluded
dataset_final_regression_excl_all_M <- dataset_final_regression_all_M %>%
mutate(exclude_final=case_when(exclude=="C1_C2"~"school",
exclude=="C1"|exclude=="C2"~"one classroom",
TRUE~NA_character_)) %>%
mutate(school_class=paste0(SchoolID, "_", location)) %>%
# exclude classes
filter(!(school_class %in% exclusions_index$school_class))
saveRDS(dataset_final_regression_all_M, file="produced_data/dataset_final_regression_all_M.rds")
saveRDS(dataset_final_regression_excl_all_M, file="produced_data/dataset_final_regression_excl_all_M.rds")
```
# Session Information
```{r S5 }
sessionInfo()
```