uk <- data$overview

nations <- data$nations

regions <- data$regions

nhs_regions <- data$nhsRegions

la <- data$utlas

UK

nameUk <- map(uk, "name")

ukdf <- map(uk$K02000001, enframe)
ukdf_names <- map(uk$K02000001[2:32], colnames) %>%
  enframe()
  
create_metrics <- function(i){
  j <- i -1
  ukdf[[i]] %>%
  unnest("value") %>%
  mutate(metric = ukdf_names$name[j]) %>%
  group_by(name) %>%
  mutate(id = row_number(),
         type = ifelse(id %% 2 == 0, "val", "date")) %>%
  spread(type, value) %>%
  fill(date, .direction = "down") %>%
  fill(val, .direction = "up") %>%
  select(-id) %>%
  distinct() %>%
  mutate(date = as.Date(date), 
         val = as.numeric(val))
}
  
uk_metrics <- map_dfr(2:32, create_metrics)

UK summary

date_range_uk <- uk_metrics %>%
  group_by(metric) %>%
  summarise(range = range(date)) %>%
  mutate(ranges = c("start", "end")) %>%
  spread(ranges, range) %>%
  select(metric, start, end)


range_plot <- function(df){
  df %>%
  ggplot() +
  geom_point(aes(start, forcats::fct_rev(metric)), colour = "blue") +
  geom_point(aes(end, forcats::fct_rev(metric)), colour = "goldenrod") +
  geom_segment(aes(y = metric, yend = metric, x = start, xend = end), lty = "dotted" ) +
  labs(title = "Date ranges for UK dashboard metrics", 
       y = "") +
  theme(plot.title.position = "plot",
        plot.background = element_blank(),
        axis.text.y = element_text(size = 8)) 
}

ukp <- range_plot(date_range_uk)
ggplotly(ukp)

Regions

nameRegions <- map(regions, "name")

regiondf <- map(regions$E12000005, enframe)

regiondf_names <- map(regions$E12000005[2:13], colnames) %>%
  enframe()

create_metrics_region <- function(i){
  j <- i -1
  regiondf[[i]] %>%
  unnest("value") %>%
  mutate(metric = regiondf_names$name[j]) %>%
  group_by(name) %>%
  mutate(id = row_number(),
         type = ifelse(id %% 2 == 0, "val", "date")) %>%
  spread(type, value) %>%
  fill(date, .direction = "down") %>%
  fill(val, .direction = "up") %>%
  select(-id) %>%
  distinct() %>%
  mutate(date = as.Date(date), 
         val = as.numeric(val))
}
  
region_metrics <- map_dfr(2:13, create_metrics)
date_range_region <- region_metrics %>%
  group_by(metric) %>%
  summarise(range = range(date)) %>%
  mutate(ranges = c("start", "end")) %>%
  spread(ranges, range) %>%
  select(metric, start, end)


range_plot <- function(df){
  df %>%
  ggplot() +
  geom_point(aes(start, forcats::fct_rev(metric)), colour = "blue") +
  geom_point(aes(end, forcats::fct_rev(metric)), colour = "goldenrod") +
  geom_segment(aes(y = metric, yend = metric, x = start, xend = end), lty = "dotted" ) +
  labs(title = "Date ranges for dashboard metrics", 
       y = "") +
  theme(plot.title.position = "plot",
        plot.background = element_blank(),
        axis.text.y = element_text(size = 8)) 
}

rp <- range_plot(date_range_region)
ggplotly(rp)

England

nameNations <- map(nations, "name")

englanddf <- map(nations$E92000001, enframe)

englanddf_names <- map(nations$E92000001[2:length(englanddf)], colnames) %>%
  enframe()

create_metrics_england <- function(i){
  j <- i -1
  englanddf[[i]] %>%
  unnest("value") %>%
  mutate(metric = englanddf_names$name[j]) %>%
  group_by(name) %>%
  mutate(id = row_number(),
         type = ifelse(id %% 2 == 0, "val", "date")) %>%
  spread(type, value) %>%
  fill(date, .direction = "down") %>%
  fill(val, .direction = "up") %>%
  select(-id) %>%
  distinct() %>%
  mutate(date = as.Date(date), 
         val = as.numeric(val))
}
  
england_metrics <- map_dfr(2:length(englanddf), create_metrics_england)

england_metrics %>%
  #count(metric)
  filter(metric ==  "newAdmissions")
date_range_england <- england_metrics %>%
  group_by(metric) %>%
  summarise(range = range(date)) %>%
  mutate(ranges = c("start", "end")) %>%
  spread(ranges, range) %>%
  select(metric, start, end)


range_plot <- function(df){
  df %>%
  ggplot() +
  geom_point(aes(start, forcats::fct_rev(metric)), colour = "blue") +
  geom_point(aes(end, forcats::fct_rev(metric)), colour = "goldenrod") +
  geom_segment(aes(y = metric, yend = metric, x = start, xend = end), lty = "dotted" ) +
  labs(title = "Date ranges for dashboard metrics", 
       y = "") +
  theme(plot.title.position = "plot",
        plot.background = element_blank(),
        axis.text.y = element_text(size = 8)) 
}

ep <- range_plot(date_range_england)

ggplotly(ep)

Scotland

nameNations <- map(nations, "name")

scotdf <- map(nations$S92000003, enframe)

scotdf_names <- map(nations$S92000003[2:length(scotdf)], colnames) %>%
  enframe()

create_metrics_scotland <- function(i){
  j <- i -1
  scotdf[[i]] %>%
  unnest("value") %>%
  mutate(metric = scotdf_names$name[j]) %>%
  group_by(name) %>%
  mutate(id = row_number(),
         type = ifelse(id %% 2 == 0, "val", "date")) %>%
  spread(type, value) %>%
  fill(date, .direction = "down") %>%
  fill(val, .direction = "up") %>%
  select(-id) %>%
  distinct() %>%
  mutate(date = as.Date(date), 
         val = as.numeric(val))
}
  
scotland_metrics <- map_dfr(2:length(scotdf), create_metrics_scotland)

sm <- data.table::setDT(scotland_metrics)

sm <- sm[, .(metric, date, val)] %>%
  filter(!str_detect(metric, "[Mm]ale")) %>%
  spread(metric, val, fill = 0) 

sm %>%
  write_csv("dashboard_scotland.csv")

c <- sm %>%
  select(date, cumPillarOnePeopleTestedByPublishDate, cumCasesBySpecimenDate) %>%
  mutate(pos_case_rate = cumCasesBySpecimenDate/cumPillarOnePeopleTestedByPublishDate) %>%
  filter(cumPillarOnePeopleTestedByPublishDate > 0) %>%
  ggplot(aes(date, pos_case_rate)) +
  geom_line() +
  ggtitle("Scotland: positivity rate")
  
d <- sm %>%
  select(date, cumPillarOnePeopleTestedByPublishDate, newCasesBySpecimenDate) %>%
  #mutate(pos_case_rate = cumCasesBySpecimenDate/cumPillarOnePeopleTestedByPublishDate) %>%
  filter(cumPillarOnePeopleTestedByPublishDate > 0) %>%
  ggplot(aes(date, newCasesBySpecimenDate)) +
  geom_col() +
  geom_smooth(method = "gam", se = FALSE) +
  ggtitle("Scotland: new cases")

c + d

date_range_scotland <- scotland_metrics %>%
  group_by(metric) %>%
  summarise(range = range(date)) %>%
  mutate(ranges = c("start", "end")) %>%
  spread(ranges, range) %>%
  select(metric, start, end)


range_plot <- function(df){
  df %>%
  ggplot() +
  geom_point(aes(start, forcats::fct_rev(metric)), colour = "blue") +
  geom_point(aes(end, forcats::fct_rev(metric)), colour = "goldenrod") +
  geom_segment(aes(y = metric, yend = metric, x = start, xend = end), lty = "dotted" ) +
  labs(title = "Date ranges for dashboard metrics", 
       y = "") +
  theme(plot.title.position = "plot",
        plot.background = element_blank(),
        axis.text.y = element_text(size = 8)) 
}

sp <- range_plot(date_range_scotland)

ggplotly(sp)

Wales

nameNations <- map(nations, "name")

walesdf <- map(nations$W92000004, enframe)


walesdf_names <- map(nations$W92000004[2:length(walesdf)], colnames) %>%
  enframe()

create_metrics_wales <- function(i){
  j <- i -1
  walesdf[[i]] %>%
  unnest("value") %>%
  mutate(metric = walesdf_names$name[j]) %>%
  group_by(name) %>%
  mutate(id = row_number(),
         type = ifelse(id %% 2 == 0, "val", "date")) %>%
  spread(type, value) %>%
  fill(date, .direction = "down") %>%
  fill(val, .direction = "up") %>%
  select(-id) %>%
  distinct() %>%
  mutate(date = as.Date(date), 
         val = as.numeric(val))
}
  
wales_metrics <- map_dfr(2:length(walesdf), create_metrics_wales)

wm <- data.table::setDT(wales_metrics)

wm <- wm[, .(metric, date, val)] %>%
  filter(!str_detect(metric, "[Mm]ale")) %>%
  spread(metric, val, fill = 0) 

wm %>%
  write_csv("dashboard_wales.csv")


a <- wm %>%
  select(date, cumPillarOnePeopleTestedByPublishDate, cumCasesBySpecimenDate) %>%
  mutate(pos_case_rate = cumCasesBySpecimenDate/cumPillarOnePeopleTestedByPublishDate) %>%
  filter(cumPillarOnePeopleTestedByPublishDate > 0) %>%
  ggplot(aes(date, pos_case_rate)) +
  geom_line() +
  ggtitle("Wales: positivity rate")
  
b <- wm %>%
  select(date, cumPillarOnePeopleTestedByPublishDate, newCasesBySpecimenDate) %>%
  #mutate(pos_case_rate = cumCasesBySpecimenDate/cumPillarOnePeopleTestedByPublishDate) %>%
  filter(cumPillarOnePeopleTestedByPublishDate > 0) %>%
  ggplot(aes(date, newCasesBySpecimenDate)) +
  geom_col() +
  geom_smooth(method = "gam", se = FALSE) +
  ggtitle("Wales: new cases")

a + b

date_range_wales <- wales_metrics %>%
  group_by(metric) %>%
  summarise(range = range(date)) %>%
  mutate(ranges = c("start", "end")) %>%
  spread(ranges, range) %>%
  select(metric, start, end)


range_plot <- function(df){
  df %>%
  ggplot() +
  geom_point(aes(start, forcats::fct_rev(metric)), colour = "blue") +
  geom_point(aes(end, forcats::fct_rev(metric)), colour = "goldenrod") +
  geom_segment(aes(y = metric, yend = metric, x = start, xend = end), lty = "dotted" ) +
  labs(title = "Date ranges for dashboard metrics", 
       y = "") +
  theme(plot.title.position = "plot",
        plot.background = element_blank(),
        axis.text.y = element_text(size = 8)) 
}

wp <- range_plot(date_range_wales)

ggplotly(wp)

Northern Ireland

nameNations <- map(nations, "name")

nidf <- map(nations$N92000002, enframe)

nidf_names <- map(nations$N92000002[2:length(nidf)], colnames) %>%
  enframe()

create_metrics_ni <- function(i){
  j <- i -1
  nidf[[i]] %>%
  unnest("value") %>%
  mutate(metric = nidf_names$name[j]) %>%
  group_by(name) %>%
  mutate(id = row_number(),
         type = ifelse(id %% 2 == 0, "val", "date")) %>%
  spread(type, value) %>%
  fill(date, .direction = "down") %>%
  fill(val, .direction = "up") %>%
  select(-id) %>%
  distinct() %>%
  mutate(date = as.Date(date), 
         val = as.numeric(val))
}
  
ni_metrics <- map_dfr(2:length(nidf), create_metrics_ni)

ni_metrics %>%
  #count(metric)
  filter(metric ==  "newAdmissions")
date_range_ni <- ni_metrics %>%
  group_by(metric) %>%
  summarise(range = range(date)) %>%
  mutate(ranges = c("start", "end")) %>%
  spread(ranges, range) %>%
  select(metric, start, end)


range_plot <- function(df){
  df %>%
  ggplot() +
  geom_point(aes(start, forcats::fct_rev(metric)), colour = "blue") +
  geom_point(aes(end, forcats::fct_rev(metric)), colour = "goldenrod") +
  geom_segment(aes(y = metric, yend = metric, x = start, xend = end), lty = "dotted" ) +
  labs(title = "Date ranges for dashboard metrics", 
       y = "") +
  theme(plot.title.position = "plot",
        plot.background = element_blank(),
        axis.text.y = element_text(size = 8)) 
}

nip <- range_plot(date_range_ni)

ggplotly(nip)

LA - England

nameLA <- map(la, "name")
  
eladf <- map(la$E06000018, enframe)

eladf_names <- map(la$E06000018[2:length(eladf)], colnames) %>%
  enframe()

create_metrics_ela <- function(i){
  j <- i -1
  eladf[[i]] %>%
  unnest("value") %>%
  mutate(metric = eladf_names$name[j]) %>%
  group_by(name) %>%
  mutate(id = row_number(),
         type = ifelse(id %% 2 == 0, "val", "date")) %>%
  spread(type, value) %>%
  fill(date, .direction = "down") %>%
  fill(val, .direction = "up") %>%
  select(-id) %>%
  distinct() %>%
  mutate(date = as.Date(date), 
         val = as.numeric(val))
}
  
ela_metrics <- map_dfr(2:length(eladf), create_metrics_ela)
date_range_ela <- ela_metrics %>%
  group_by(metric) %>%
  summarise(range = range(date)) %>%
  mutate(ranges = c("start", "end")) %>%
  spread(ranges, range) %>%
  select(metric, start, end)


range_plot <- function(df){
  df %>%
  ggplot() +
  geom_point(aes(start, forcats::fct_rev(metric)), colour = "blue") +
  geom_point(aes(end, forcats::fct_rev(metric)), colour = "goldenrod") +
  geom_segment(aes(y = metric, yend = metric, x = start, xend = end), lty = "dotted" ) +
  labs(title = "Date ranges for dashboard metrics", 
       y = "") +
  theme(plot.title.position = "plot",
        plot.background = element_blank(),
        axis.text.y = element_text(size = 8)) 
}

elap <- range_plot(date_range_ela)

ggplotly(elap)

LA - Wales

nameLA <- map(la, "name")
  
wladf <- map(la$W06000024, enframe)

wladf_names <- map(la$W06000024[2:length(wladf)], colnames) %>%
  enframe()

create_metrics_wla <- function(i){
  j <- i -1
  wladf[[i]] %>%
  unnest("value") %>%
  mutate(metric = wladf_names$name[j]) %>%
  group_by(name) %>%
  mutate(id = row_number(),
         type = ifelse(id %% 2 == 0, "val", "date")) %>%
  spread(type, value) %>%
  fill(date, .direction = "down") %>%
  fill(val, .direction = "up") %>%
  select(-id) %>%
  distinct() %>%
  mutate(date = as.Date(date), 
         val = as.numeric(val))
}
  
wla_metrics <- map_dfr(2:length(wladf), create_metrics_wla)
date_range_wla <- wla_metrics %>%
  group_by(metric) %>%
  summarise(range = range(date)) %>%
  mutate(ranges = c("start", "end")) %>%
  spread(ranges, range) %>%
  select(metric, start, end)


range_plot <- function(df){
  df %>%
  ggplot() +
  geom_point(aes(start, forcats::fct_rev(metric)), colour = "blue") +
  geom_point(aes(end, forcats::fct_rev(metric)), colour = "goldenrod") +
  geom_segment(aes(y = metric, yend = metric, x = start, xend = end), lty = "dotted" ) +
  labs(title = "Date ranges for dashboard metrics", 
       y = "") +
  theme(plot.title.position = "plot",
        plot.background = element_blank(),
        axis.text.y = element_text(size = 8)) 
}

wlap <- range_plot(date_range_wla)

ggplotly(wlap)

LA - Scotland

nameLA <- map(la, "name")
  
sladf <- map(la$S12000005, enframe)

sladf_names <- map(la$S12000041[2:length(sladf)], colnames) %>%
  enframe()

create_metrics_sla <- function(i){
  j <- i -1
  sladf[[i]] %>%
  unnest("value") %>%
  mutate(metric = sladf_names$name[j]) %>%
  group_by(name) %>%
  mutate(id = row_number(),
         type = ifelse(id %% 2 == 0, "val", "date")) %>%
  spread(type, value) %>%
  fill(date, .direction = "down") %>%
  fill(val, .direction = "up") %>%
  select(-id) %>%
  distinct() %>%
  mutate(date = as.Date(date), 
         val = as.numeric(val))
}
  
sla_metrics <- map_dfr(2:length(sladf), create_metrics_sla)
date_range_sla <- sla_metrics %>%
  group_by(metric) %>%
  summarise(range = range(date)) %>%
  mutate(ranges = c("start", "end")) %>%
  spread(ranges, range) %>%
  select(metric, start, end)


range_plot <- function(df){
  df %>%
  ggplot() +
  geom_point(aes(start, forcats::fct_rev(metric)), colour = "blue") +
  geom_point(aes(end, forcats::fct_rev(metric)), colour = "goldenrod") +
    geom_segment(aes(y = metric, yend = metric, x = start, xend = end), lty = "dotted" ) +
  labs(title = "Date ranges for dashboard metrics", 
       y = "") +
  theme(plot.title.position = "plot",
        plot.background = element_blank(),
        axis.text.y = element_text(size = 8)) 
}

slap <- range_plot(date_range_sla)

ggplotly(slap)

NHS regions

nameNHSR <- map(nhs_regions, "name")

nhsrdf <- map(nhs_regions$E40000006, enframe)

nhsrdf_names <- map(nhs_regions$E40000006[2:length(nhsrdf)], colnames) %>%
  enframe()

create_metrics_nhsr <- function(i){
  j <- i -1
  nhsrdf[[i]] %>%
  unnest("value") %>%
  mutate(metric = nhsrdf_names$name[j]) %>%
  group_by(name) %>%
  mutate(id = row_number(),
         type = ifelse(id %% 2 == 0, "val", "date")) %>%
  spread(type, value) %>%
  fill(date, .direction = "down") %>%
  fill(val, .direction = "up") %>%
  select(-id) %>%
  distinct() %>%
  mutate(date = as.Date(date), 
         val = as.numeric(val))
}
  
nhsr_metrics <- map_dfr(2:length(nhsrdf), create_metrics_nhsr)
date_range_nhsr <- nhsr_metrics %>%
  group_by(metric) %>%
  summarise(range = range(date)) %>%
  mutate(ranges = c("start", "end")) %>%
  spread(ranges, range) %>%
  select(metric, start, end)


range_plot <- function(df){
  df %>%
  ggplot() +
  geom_point(aes(start, forcats::fct_rev(metric)), colour = "blue") +
  geom_point(aes(end, forcats::fct_rev(metric)), colour = "goldenrod") +
  geom_segment(aes(y = metric, yend = metric, x = start, xend = end), lty = "dotted" ) +
  labs(title = "Date ranges for dashboard metrics", 
       y = "") +
  theme(plot.title.position = "plot",
        plot.background = element_blank(),
        axis.text.y = element_text(size = 8)) 
}

g <- range_plot(date_range_nhsr)

ggplotly(g)