library(tidyverse)
library(cowplot)
library(gghighlight)

read_csv("FCEs.csv") %>%
#import HES APC data: primary diagnosis of DCM for 2012-13, 13-14... 18-19 (7 years)
#ICD10 4 character codes: M47.1, M50.0, M99.3, M99.4, M99.5
#Downloadable from https://digital.nhs.uk/data-and-information/publications/statistical/hospital-admitted-patient-care-activity

pivot_longer(cols=2:8, names_to="year", values_to="FCEs") -> FCE
#Reshape to tidy data

filter(FCE, Age >= 15 & Age <=19) %>%
#Filter to the 15-19 year olds, who are in separate bins for the HES data 

group_by(year) %>%
  summarise(FCEs=sum(FCEs)) %>%
add_column("15-19", .before = "year") %>%
rename("Age" = `"15-19"`) -> FCE_15_19
#Group the 15-19 year olds into a single bin

bind_rows(FCE, FCE_15_19) %>%
filter(Age != 15 & Age != 16 & Age != 17 & Age != 18 & Age != 19) -> FCE_binned
#Reinsert the 15-19 bin into the tidy table

filter(FCE_binned, Age == "0" | Age == "1-4") %>%
  group_by(year) %>%
  summarise(FCEs=sum(FCEs)) %>%
  add_column("0-4", .before = "year") %>%
  rename("Age" = `"0-4"`) -> FCE_0_4
#Group the 0-4 year olds into a single bin

bind_rows(FCE_binned, FCE_0_4) %>%
  filter(Age != "1-4" & Age != "0") -> FCE_binned2
#Reinsert the 0-4 bin into the tidy table

read_csv("England_population.csv") %>%
#Import ONS data for England population in 2012, 13, 14... 18 (7 years)
#Downloadable from https://www.ons.gov.uk/peoplepopulationandcommunity/populationandmigration/populationestimates/datasets/populationestimatesforukenglandandwalesscotlandandnorthernireland

  pivot_longer(cols=2:8, names_to="year", values_to="pop") -> Eng_pop
#Reshape to tidy data

Eng_pop %>% 
  mutate(
    Age = dplyr::case_when(
      Age < 5            ~ "0-4",
      Age >= 5 & Age <= 9 ~ "5-9",
      Age >= 10 & Age <= 14 ~ "10-14",
      Age >= 15 & Age <= 19 ~ "15-19",
      Age >= 20 & Age <= 24 ~ "20-24",
      Age >= 25 & Age <= 29 ~ "25-29",
      Age >= 30 & Age <= 34 ~ "30-34",
      Age >= 35 & Age <= 39 ~ "35-39",
      Age >= 40 & Age <= 44 ~ "40-44",
      Age >= 45 & Age <= 49 ~ "45-49",
      Age >= 50 & Age <= 54 ~ "50-54",
      Age >= 55 & Age <= 59 ~ "55-59",
      Age >= 60 & Age <= 64 ~ "60-64",
      Age >= 65 & Age <= 69 ~ "65-69",
      Age >= 70 & Age <= 74 ~ "70-74",
      Age >= 75 & Age <= 79 ~ "75-79",
      Age >= 80 & Age <= 84 ~ "80-84",
      Age >= 85 & Age <= 89 ~ "85-89",
      Age >= 90 ~ "90+"
)) %>%
  group_by(Age, year) %>%
  summarise(pop=sum(pop)) -> Eng_pop_binned
#Create 5-year age bins to match HES data

Eng_pop_binned %>% 
  inner_join(FCE_binned2) -> FCE_pop
#Join the FCE & population tables

relocate(FCE_pop, pop, .after=last_col()) %>%
  #Reorder the columns in the table 
mutate(inc=(FCEs/pop)*100000) -> inc
#Calculate incidence per 100,000 population

inc$Age <- factor(inc$Age,levels = c("0-4", "5-9", "10-14", "15-19", "20-24", "25-29", "30-34", "35-39", "40-44", "45-49", "50-54", "55-59", "60-64", "65-69", "70-74", "75-79", "80-84", "85-89", "90+"))
#Set age as an ordered factor, for plotting purposes

Observed_Inc_2012 <-ggplot(inc, aes(Age, inc, group=year)) +
  #geom_col(position="dodge") +
  geom_line(aes(linetype=year)) +
  gghighlight(year==2012) +
  theme_classic() +
  xlab("Age Group (years)") +
  ylab("DCM Incidence (/100,000 population / year)") +
  ggtitle("Estimated DCM Incidence by Age Group")+
  theme(axis.text.x = element_text(angle = 45, hjust=1))+ 
  theme(legend.position = "none")
#Plotting incidence


read_csv("LE_males_adj.csv") -> LE_males
read_csv("LE_females_adj.csv") -> LE_females
#Import ONS data for UK period life expectancy in 2012, 13, 14... 18 (7 years), for males and females
#Downloadable from https://www.ons.gov.uk/peoplepopulationandcommunity/birthsdeathsandmarriages/lifeexpectancies/datasets/nationallifetablesunitedkingdomreferencetables
#Adjusted for DCM standardised mortality rate (SMR) of 1.18 compared to ONS population - see Supplementary Material LE_adj for working.  

pivot_longer(LE_males, cols=2:8, names_to="year", values_to="LE") -> LE_males
pivot_longer(LE_females, cols=2:8, names_to="year", values_to="LE") -> LE_females
#Reshape to tidy data

add_column(LE_males, "male") %>%
rename("Sex" = `"male"`) -> LE_males

add_column(LE_females, "female") %>%
  rename("Sex" = `"female"`) -> LE_females
#Add a column to each table specifying sex

LE_males %>% 
  inner_join(LE_females, by= c("Age", "year")) -> LE_combined
#Join the male & female LE tables

group_by(LE_combined, Age, year) %>%
  summarise(LE_mean=(LE.x+LE.y)/2) -> LE_summarised
#Average the male & female LE values (assumes 50/50 population split -- rerunning with weighted average to population at each age gives very similar values but slows the whole calculation down.)

LE_summarised %>% 
mutate(
  Age = dplyr::case_when(
    Age < 5            ~ "0-4",
    Age >= 5 & Age <= 9 ~ "5-9",
    Age >= 10 & Age <= 14 ~ "10-14",
    Age >= 15 & Age <= 19 ~ "15-19",
    Age >= 20 & Age <= 24 ~ "20-24",
    Age >= 25 & Age <= 29 ~ "25-29",
    Age >= 30 & Age <= 34 ~ "30-34",
    Age >= 35 & Age <= 39 ~ "35-39",
    Age >= 40 & Age <= 44 ~ "40-44",
    Age >= 45 & Age <= 49 ~ "45-49",
    Age >= 50 & Age <= 54 ~ "50-54",
    Age >= 55 & Age <= 59 ~ "55-59",
    Age >= 60 & Age <= 64 ~ "60-64",
    Age >= 65 & Age <= 69 ~ "65-69",
    Age >= 70 & Age <= 74 ~ "70-74",
    Age >= 75 & Age <= 79 ~ "75-79",
    Age >= 80 & Age <= 84 ~ "80-84",
    Age >= 85 & Age <= 89 ~ "85-89",
    Age >= 90 ~ "90+"
  ),
    Age= factor(
      Age,
      level = c("0-4", "5-9", "10-14", "15-19", "20-24", "25-29", "30-34", "35-39", "40-44", "45-49", "50-54", "55-59", "60-64", "65-69", "70-74", "75-79", "80-84", "85-89", "90+")
    )
) %>%
  group_by(Age, year) %>%
  summarise(LE_mean=mean(LE_mean)) -> LE_binned
#Create 5-year age bins to match HES data
                
inc %>% 
  inner_join(LE_binned, by= c("Age", "year")) -> prev
#Join the inc & LE tables

mutate(prev, prev=inc*LE_mean) -> prev
mutate(prev, prev_pc=prev/1000) -> prev
#Calculate prevalence per 100,000 population & as %

Observed_Prev_2012 <- ggplot(prev, aes(Age, prev, group=year)) +
  #geom_col(position="dodge") +
  geom_line(aes(linetype=year)) +
  gghighlight(year==2012) +
  theme_classic() +
xlab("Age Group (years)") +
  ylab("DCM Prevalence (/100,000 population)") +
  ggtitle("Estimated DCM Prevalence by Age Group")+
  theme(axis.text.x = element_text(angle = 45, hjust=1))+ 
  theme(legend.position = "none")
#Plotting DCM prevalence

FCEs_2012 <- ggplot(prev, aes(Age, FCEs, group=year))+
  #geom_col(aes(fill=year), position="dodge") +
  geom_line(aes(linetype=year)) +
  gghighlight(year==2012) +
  theme_classic() +
  xlab("Age Group (years)") +
  ylab("Finished Consultant Episodes (raw number)") +
  ggtitle("Finished Consultant Episodes by Age Group")+
  theme(axis.text.x = element_text(angle = 45, hjust=1))+ 
  theme(legend.position = "none")
#Plotting FCEs

pop_2012 <- ggplot(prev, aes(Age, pop/1000000, group=year)) +
  geom_line(aes(linetype=year)) +
  gghighlight(year==2012) +
  theme_classic() +
  xlab("Age Group (years)") +
  ylab("Population (millions)") +
  ggtitle("England Population by Age Group")+
  theme(axis.text.x = element_text(angle = 45, hjust=1))+ 
  theme(legend.position = "bottom")
#Plotting England population

legend_observed_prevalence <- get_legend(pop_2012)
pop_2012 <- pop_2012 + theme(legend.position='none')

Fig_Observed_Prevalence <- plot_grid(plot_grid(FCEs_2012, pop_2012, Observed_Inc_2012, Observed_Prev_2012, labels = "AUTO"),
plot_grid(NULL, legend_observed_prevalence, ncol=1), ncol=1,
rel_widths=c(1, 0.2), rel_heights=c(1,0.1))
save_plot("Fig_Observed_Prevalence.pdf", Fig_Observed_Prevalence, base_height = 11, base_width = 10)
#Creating Figure: Observed Prevalence

pop_sum <- group_by(prev, Age) %>%
  summarise(pop_sum=sum(pop))

FCE_mean <- group_by(prev, Age) %>%
  summarise(FCE_mean=mean(FCEs))

group_by(prev, Age) %>%
  summarise(FCE_sd=sd(FCEs)) 

FCE_sum <- group_by(prev, Age) %>%
  summarise(FCE_sum=sum(FCEs))

inc_mean <- group_by(prev, Age) %>%
  summarise(inc_mean=mean(inc))

inc_sum <- group_by(prev, Age) %>%
  summarise(inc_sum=sum(inc))

group_by(prev, Age) %>%
  summarise(inc_sd=sd(inc)) 

prev_mean <- group_by(prev, Age) %>%
  summarise(prev_mean=mean(prev), pop_mean=mean(pop))

prev_sum <- group_by(prev, Age) %>%
  summarise(prev_sum=sum(prev))

summarise(prev_mean, prev_mean_mean=mean(prev_mean))

group_by(prev, Age) %>%
  summarise(prev_sd=sd(prev)) %>%
  summarise(mean_sd=mean(prev_sd))

LE_mean <- group_by(LE_binned, Age) %>%
  summarise(LE_mean = mean(LE_mean))
#Generate summary statistics

FCE_prev_sum <- inner_join(FCE_sum, prev_sum)
prev_CI <-transmute(FCE_prev_sum, CI = (1.96*sqrt(((prev_sum/100000)*(1-(prev_sum/100000)))/FCE_sum))*100000, Age=Age)
#Generate 95% confidence intervals for total prev over total FCEs

prev_mean_CI <- inner_join(prev_mean, prev_CI)
prev_mean_CI_pc <- mutate(prev_mean_CI, CI=CI/1000, prev_mean=prev_mean/1000)
#Transform the prev & CI values from /100,000 to %

ggplot(data=prev_mean_CI, mapping=aes(x=Age, y=prev_mean)) +
  geom_col(fill="gray75") +
  geom_errorbar(mapping=aes(ymin=(prev_mean-CI), ymax=(prev_mean+CI)), width=0.2, size=1)+
  theme_classic() +
  xlab("Age Group (years)") +
  ylab("Mean DCM Prevalence (/100,000 population") +
  ggtitle("Summed DCM Prevalence by Age Group (+-95%CI)")
#Plot mean DCM prev with 95%CI

transmute(Age=Age, prev_mean_CI_pc, DCM_pc=signif(prev_mean,3), CI=signif(CI,3)) -> prev_export
write.table(prev_export, file = "prev_export.txt", sep = ",", quote = FALSE, row.names = F)
#Table 1

#95% CI METHOD 2
prev_CI_2 <- FCE_sum %>%
  inner_join(pop_sum) %>%
  mutate(inc = (FCE_sum / pop_sum) * 100) %>%
  mutate(ln_inc = log(inc)) %>%
  mutate(SE = 1/sqrt(FCE_sum)) %>%
  mutate(ln_inc_plus_1.96SE = ln_inc + (1.96*SE)) %>%
  mutate(ln_inc_minus_1.96SE = ln_inc - (1.96*SE)) %>%
  mutate(upper_CI_inc = exp(ln_inc_plus_1.96SE)) %>%
  mutate(lower_CI_inc = exp(ln_inc_minus_1.96SE)) %>%
  inner_join(LE_mean) %>%
  mutate(prev = inc * LE_mean) %>%
  mutate(upper_CI_prev = upper_CI_inc * LE_mean) %>%
  mutate(lower_CI_prev = lower_CI_inc * LE_mean)

prev_CI_2_graph <- ggplot(data=prev_CI_2, mapping=aes(x=Age, y=prev)) +
  geom_col(fill="gray75") +
  geom_errorbar(mapping=aes(ymin=(lower_CI_prev), ymax=(upper_CI_prev)), width=0.2, size=1)+
  theme_classic() +
  xlab("Age Group (years)") +
  ylab("Mean DCM Prevalence (% population") +
  ggtitle("Summed DCM Prevalence by Age Group (+-95%CI)")

save_plot("Observed_Prevalence_CI2.pdf", prev_CI_2_graph, base_height = 10, base_width = 16.2)
#Plot mean DCM prev with 95%CI method 2

transmute(prev_CI_2, Age=Age, lower_CI=signif(lower_CI_prev,2), upper_CI=signif(upper_CI_prev,2)) -> prev_export_2
write.table(prev_export_2, file = "prev_export_2.txt", sep = ",", quote = FALSE, row.names = F)
#Table 1 with CI

summarise(Eng_pop, total_pop=sum(pop))
summarise(inc_mean, mean_inc=mean(inc_mean))
#Summary statistics
