|
| 1 | +# No need to run this file; it is just provided for sake of giving an idea to people |
| 2 | +# what was done in paper in terms of pre-processing |
| 3 | +library(tidyr) |
| 4 | +library(dplyr) |
| 5 | +library(readr) |
| 6 | +library(janitor) |
| 7 | +library(lubridate) |
| 8 | +library(magrittr) |
| 9 | +library(ggpubr) |
| 10 | +library(tidyr) |
| 11 | +library(readxl) |
| 12 | +library(tools) |
| 13 | +library(ggplot2) |
| 14 | +library(flextable) |
| 15 | +library(stringr) |
| 16 | +library(here) |
| 17 | + |
| 18 | +# Import .rds |
| 19 | +DATA <- read.csv(file = here('tiers/data/npis_23Mar_02Dec.csv')) |
| 20 | +DATA$date = as.Date(DATA$date, format = '%d/%m/%Y') |
| 21 | + |
| 22 | +Earliest = TRUE # if true, assessing which interventions consitute tier X based on earliest date that Tier X introduced. If false, then based on latest date that Tier X applies. |
| 23 | +#Earliest = FALSE # if true, assessing which interventions consitute tier X based on earliest date that Tier X introduced. If false, then based on latest date that Tier X applies. |
| 24 | + |
| 25 | +Interventions = c( |
| 26 | + |
| 27 | + "limited_to_groups_of_6_indoors" , |
| 28 | + "limited_to_groups_of_6_outdoors" , |
| 29 | + "curfew_of_10pm_for_hospitality_venues" , |
| 30 | + "instruction_to_work_from_home_where_possible" , |
| 31 | + "travel_discouraged" , |
| 32 | + "no_indoor_mixing" , |
| 33 | + "overnight_stays_discouraged" , |
| 34 | + "residents_cannot_leave_the_local_area" , |
| 35 | + "non-essential_retail_closures" , |
| 36 | + "schools_closed" , |
| 37 | + "places_of_worship_closed" , |
| 38 | + "weddings_not_allowed" , |
| 39 | + "organised_sport_not_allowed" , |
| 40 | + "tourist_attractions_closed" , |
| 41 | + "gyms_closed" , |
| 42 | + "public_buildings_closed" , |
| 43 | + "personal_care_contact_services_closed" , |
| 44 | + "arts_venues_closed" , |
| 45 | + "sit-down_hospitality_closed_takeaway_only" , |
| 46 | + "pubs_and_bars_closed_table_service_only" , |
| 47 | + "essential_travel_only" |
| 48 | +) |
| 49 | + |
| 50 | +# tibbles (apparently) better so re-write below you become familiar |
| 51 | +DATA = as.data.frame(DATA) |
| 52 | +str(DATA$date) |
| 53 | + |
| 54 | +# how many ltlas? |
| 55 | +N_LTLAs = length(unique(DATA$ltla)) |
| 56 | + |
| 57 | +## Define nested tiers |
| 58 | +DATA$tier_3_nested = DATA$tier_3 |
| 59 | +DATA$tier_2_nested = DATA$tier_2 + DATA$tier_3 |
| 60 | +DATA$tier_1_nested = DATA$tier_1 + DATA$tier_2 + DATA$tier_3 |
| 61 | +DATA$Tier = DATA$tier_1_nested + DATA$tier_2_nested + DATA$tier_3_nested |
| 62 | + |
| 63 | +# Subset to post 11th September |
| 64 | +DATA_postSept = DATA[DATA$date >= as.Date("2020-09-11"),] |
| 65 | + |
| 66 | +# check defs |
| 67 | +#TierDF = data.frame(date = DATA_postSept$date, TierCat = DATA_postSept$Tier, |
| 68 | +# tier_1 = DATA_postSept$tier_1, tier_2 = DATA_postSept$tier_2, tier_3 = DATA_postSept$tier_3, |
| 69 | +# tier_1_nested = DATA_postSept$tier_1_nested, tier_2_nested = DATA_postSept$tier_2_nested, tier_3_nested = DATA_postSept$tier_3_nested) |
| 70 | +## Statements below should be false. |
| 71 | +#all(DATA$tier_1[which(DATA$tier_2 == 1)] == 1) |
| 72 | +#all(DATA$tier_1[which(DATA$tier_3 == 1)] == 1) |
| 73 | +#all(DATA$tier_2[which(DATA$tier_3 == 1)] == 1) |
| 74 | +## Statements below should be true |
| 75 | +#all(DATA$tier_1_nested[which(DATA$tier_2_nested == 1)] == 1) |
| 76 | +#all(DATA$tier_1_nested[which(DATA$tier_3_nested == 1)] == 1) |
| 77 | +#all(DATA$tier_2_nested[which(DATA$tier_3_nested == 1)] == 1) |
| 78 | + |
| 79 | + |
| 80 | +# which interventions are included in Tiers 1, 2, and 3, at first date Tier introduced? i.e. for each ltla, which interventions are on when tier 1 is on, when tier 2 is on etc. |
| 81 | +Nested_Tier = 1 |
| 82 | +ltla_index = 2 |
| 83 | + |
| 84 | +Tier_List = list() |
| 85 | +Tier_List[[1]] = matrix (0, nrow = length(Interventions), ncol = N_LTLAs) |
| 86 | +Tier_List[[2]] = matrix (0, nrow = length(Interventions), ncol = N_LTLAs) |
| 87 | +Tier_List[[3]] = matrix (0, nrow = length(Interventions), ncol = N_LTLAs) |
| 88 | + |
| 89 | +for (Nested_Tier in 1:3) colnames(Tier_List[[Nested_Tier]]) = unique(DATA_postSept$ltla) |
| 90 | + |
| 91 | +Interventions_ColNames = gsub("-", ".", Interventions) ## account for hypens in colnames. |
| 92 | +#all(Interventions %in% colnames(DATA)) |
| 93 | +#all(Interventions_ColNames %in% colnames(DATA)) |
| 94 | + |
| 95 | +for (ltla_index in 1:N_LTLAs) |
| 96 | +{ |
| 97 | + # subset to this LTLA |
| 98 | + DATA_ltla = DATA_postSept[DATA_postSept$ltla == unique(DATA_postSept$ltla)[ltla_index], ] |
| 99 | + |
| 100 | + for (Nested_Tier in 1:3) |
| 101 | + if (any(DATA_ltla$Tier >= Nested_Tier)) # if this ltla was ever in this nested tier |
| 102 | + { |
| 103 | + EarliestDate = min(DATA_ltla$date[DATA_ltla$Tier >= Nested_Tier]) |
| 104 | + LatestDate = max(DATA_ltla$date[DATA_ltla$Tier >= Nested_Tier]) |
| 105 | + |
| 106 | + if (Earliest) Date_dummy = EarliestDate else Date_dummy = LatestDate |
| 107 | + |
| 108 | + # which interventions turned on at earliest/latest date? Add these to matrix |
| 109 | + Tier_List[[Nested_Tier]][which(DATA_ltla[DATA_ltla$date == Date_dummy, Interventions_ColNames] == 1), ltla_index] = 1 |
| 110 | + } |
| 111 | +} |
| 112 | + |
| 113 | + |
| 114 | +#### #### #### #### #### #### #### #### #### #### #### #### #### #### #### #### #### #### #### #### #### #### #### #### |
| 115 | +#### Make plots of interventions by region and tier. |
| 116 | +#### #### #### #### #### #### #### #### #### #### #### #### #### #### #### #### #### #### #### #### #### #### #### #### |
| 117 | + |
| 118 | +dir.create(here("tiers/figures/"), showWarnings = FALSE) |
| 119 | + |
| 120 | +AllPlotsInSameFigure = TRUE |
| 121 | +#AllPlotsInSameFigure = FALSE |
| 122 | + |
| 123 | +if (Earliest) Suffix = "_Earliest" else Suffix = "_Latest" |
| 124 | + |
| 125 | +if (AllPlotsInSameFigure) |
| 126 | +{ |
| 127 | + pdf(here(paste0("tiers/figures/IntsByRegion_Tiers_123", Suffix, ".pdf")), height = 10, width = 24) |
| 128 | + par(mfrow = c(1,3), mar = c(10.1, 25.1, 4.1, 2.1)) |
| 129 | + CEXLAB = 2; CEX_Text = 1.75; CEXMAIN = 2.5; CEXAXIS = 1.75 |
| 130 | + |
| 131 | +} else { CEXLAB = 1.25; CEX_Text = 1; CEXMAIN = 1; CEXAXIS = 1 } |
| 132 | + |
| 133 | +if (AllPlotsInSameFigure) Alpha = 1 else Alpha = 0.6 |
| 134 | + |
| 135 | +COLS = c("green", "orange", "red") |
| 136 | +adjCOLS = adjustcolor(c("green", "orange", "red"), alpha.f = Alpha) |
| 137 | + |
| 138 | +labs <- gsub("_", " ", Interventions) |
| 139 | + |
| 140 | + |
| 141 | +# Tier 1 only |
| 142 | +if (!AllPlotsInSameFigure) |
| 143 | +{ |
| 144 | + png(here(paste0("tiers/figures/IntsByRegion_Tier_1", Suffix, ".png")), units = "in", res = 200, height = 10, width = 7.5) |
| 145 | + par(mar = c(8.1, 13.1, 4.1, 2.1)) |
| 146 | + LetterChar = "" |
| 147 | +} else LetterChar = "(A) " |
| 148 | +x <- barplot(rowSums(Tier_List[[1]]), horiz = TRUE, col = adjCOLS[1], border = NA, cex.main = CEXMAIN, cex.axis = CEXAXIS, |
| 149 | + main = paste0(LetterChar, "Interventions by region (Tier 1)"), xlab = "Num LTLAs where intervention applied", cex.lab = CEXLAB) |
| 150 | +text(x = -7, y = x, labs, xpd = TRUE, srt = 45, adj = 1, cex = CEX_Text) |
| 151 | +if (!AllPlotsInSameFigure) dev.off() |
| 152 | + |
| 153 | +# Tier 2 only |
| 154 | +if (!AllPlotsInSameFigure) |
| 155 | +{ |
| 156 | + png(here(paste0("tiers/figures/IntsByRegion_Tier_2", Suffix, ".png")), units = "in", res = 200, height = 10, width = 7.5) |
| 157 | + par(mar = c(8.1, 13.1, 4.1, 2.1)) |
| 158 | + LetterChar = "" |
| 159 | +} else LetterChar = "(B) " |
| 160 | +x <- barplot(rowSums(Tier_List[[2]]), horiz = TRUE, col = adjCOLS[2], border = NA, cex.main = CEXMAIN,cex.axis = CEXAXIS, |
| 161 | + main = paste0(LetterChar, "Interventions by region (Tier 2)"), xlab = "Num LTLAs where intervention applied", cex.lab = CEXLAB) |
| 162 | +text(x = -7, y = x, labs, xpd = TRUE, srt = 45, adj = 1, cex = CEX_Text) |
| 163 | +if (!AllPlotsInSameFigure) dev.off() |
| 164 | + |
| 165 | +# Tier 3 only |
| 166 | +if (!AllPlotsInSameFigure) |
| 167 | +{ |
| 168 | + png(here(paste0("tiers/figures/IntsByRegion_Tier_3", Suffix, ".png")), units = "in", res = 200, height = 10, width = 7.5) |
| 169 | + par(mar = c(8.1, 13.1, 4.1, 2.1)) |
| 170 | + LetterChar = "" |
| 171 | +} else LetterChar = "(C) " |
| 172 | +x <- barplot(rowSums(Tier_List[[3]]), horiz = TRUE, col = adjCOLS[3], border = NA, cex.main = CEXMAIN,cex.axis = CEXAXIS, |
| 173 | + main = paste0(LetterChar, "Interventions by region (Tier 3)"), xlab = "Num LTLAs where intervention applied", cex.lab = CEXLAB) |
| 174 | +text(x = -1, y = x, labs, xpd = TRUE, srt = 45, adj = 1, cex = CEX_Text) |
| 175 | +if (!AllPlotsInSameFigure) dev.off() |
| 176 | + |
| 177 | +if (AllPlotsInSameFigure) dev.off() |
| 178 | + |
| 179 | + |
| 180 | + |
| 181 | + |
| 182 | +#### #### #### #### #### #### #### #### #### #### #### #### #### #### #### #### #### #### #### #### #### #### #### #### |
| 183 | +#### With above plots, define which Ints comprise Tiers 1, 2, and 3. Distinction clearest when using Earliest date. |
| 184 | +#### #### #### #### #### #### #### #### #### #### #### #### #### #### #### #### #### #### #### #### #### #### #### #### |
| 185 | + |
| 186 | +Tier_1_ints = c( |
| 187 | + "limited_to_groups_of_6_indoors" , |
| 188 | + "limited_to_groups_of_6_outdoors" , |
| 189 | + "curfew_of_10pm_for_hospitality_venues" , |
| 190 | + "instruction_to_work_from_home_where_possible" ) |
| 191 | +Tier_2_ints = c( |
| 192 | + "limited_to_groups_of_6_indoors" , |
| 193 | + "limited_to_groups_of_6_outdoors" , |
| 194 | + "curfew_of_10pm_for_hospitality_venues" , |
| 195 | + "instruction_to_work_from_home_where_possible" , |
| 196 | + "travel_discouraged" , |
| 197 | + "no_indoor_mixing" , |
| 198 | + "overnight_stays_discouraged" ) |
| 199 | +Tier_3_ints = c( |
| 200 | + "limited_to_groups_of_6_indoors" , |
| 201 | + "limited_to_groups_of_6_outdoors" , |
| 202 | + "curfew_of_10pm_for_hospitality_venues" , |
| 203 | + "instruction_to_work_from_home_where_possible" , |
| 204 | + "travel_discouraged" , |
| 205 | + "no_indoor_mixing" , |
| 206 | + "overnight_stays_discouraged" , |
| 207 | + "residents_cannot_leave_the_local_area" , |
| 208 | + "pubs_and_bars_closed_table_service_only" ) |
| 209 | + |
| 210 | + |
| 211 | +#### #### #### #### #### #### #### #### #### #### #### #### #### #### #### #### #### #### #### #### #### #### #### #### |
| 212 | +#### back-date tiers in full dataset |
| 213 | +#### #### #### #### #### #### #### #### #### #### #### #### #### #### #### #### #### #### #### #### #### #### #### #### |
| 214 | + |
| 215 | +DATA$tier_1_BD_nested = rep(0, dim(DATA)[1]) |
| 216 | +DATA$tier_2_BD_nested = rep(0, dim(DATA)[1]) |
| 217 | +DATA$tier_3_BD_nested = rep(0, dim(DATA)[1]) |
| 218 | + |
| 219 | +for (row in 1:dim(DATA)[1]) |
| 220 | +{ |
| 221 | + if (all(DATA[row, Tier_1_ints] == 1)) DATA$tier_1_BD_nested[row] = 1 |
| 222 | + if (all(DATA[row, Tier_2_ints] == 1)) DATA$tier_2_BD_nested[row] = 1 |
| 223 | + if (all(DATA[row, Tier_3_ints] == 1)) DATA$tier_3_BD_nested[row] = 1 |
| 224 | +} |
| 225 | +DATA$Tier_BD = DATA$tier_1_BD_nested + DATA$tier_2_BD_nested + DATA$tier_3_BD_nested |
| 226 | + |
| 227 | +# the above are nested defintiions. Make non-nested version for downstream flexibility |
| 228 | +DATA$tier_1_BD = DATA$tier_1_BD_nested |
| 229 | +DATA$tier_2_BD = DATA$tier_2_BD_nested |
| 230 | +DATA$tier_3_BD = DATA$tier_3_BD_nested |
| 231 | + |
| 232 | +DATA$tier_1_BD[which(DATA$tier_2_BD_nested == 1)] = 0 |
| 233 | +DATA$tier_1_BD[which(DATA$tier_3_BD_nested == 1)] = 0 |
| 234 | +DATA$tier_2_BD[which(DATA$tier_3_BD_nested == 1)] = 0 |
| 235 | + |
| 236 | +DATA$date <- format(DATA$date, "%d/%m/%Y") |
| 237 | +write.csv(DATA, file = here("tiers/data/npis_23Mar_02Dec.csv"),row.names = FALSE) |
| 238 | + |
0 commit comments