Introduction

Download local health data

First, we will use fingertipsR to download ward level data from localhealth.

source("../phds-article/ftips_utilities.R")
data <- fingertips_missing(ProfileID = 143, AreaTypeID = 8, target = 0.1)
## Working...
data1 <- fingertips_data(ProfileID = 143, AreaTypeID = 8, ParentAreaTypeID = 102 )
data2 <- select(data1, 8:1) %>%
  filter(AreaType == "Ward") %>%
  janitor::clean_names() %>%
  count(parent_name, area_name, area_code)

data3 <- data %>%
  clean_names() %>%
  as_tibble() %>%
  left_join(data2)

data

Review data

Preparing data for modelling

missing data

Identify categories, Select variables and remove missing data values

Model premature mortality

  • Remove death and life expectancy data
library(tidyr)

data3$outcome <- data3$life_expectancy_at_birth_upper_age_band_90_all_ages_male
data_lm <- data3 %>%
  select(-contains("death")) %>%
  select(-contains("expect")) %>%
  select(parent_name, outcome, 3:42)

data_grp <- data_lm %>%
  group_by(parent_name) %>%
  nest()

dim(data_grp)
## [1] 153   2
data_grp$data[[1]]
full_model <- glm(outcome~., data = data_lm[, -1])

broom::glance(full_model)
broom::tidy(full_model) %>%
  filter(p.value < 0.05) %>%
  arrange(-estimate)
tidy_lm <- data_lm %>%
  #select(parent_name, outcome, 3:42) %>%
  group_by(parent_name) %>%
  #filter(parent_name == "Barnet" ) %>%
  #group_modify(~ broom::augment(lm(outcome~., data =.x)))
  group_modify(~ broom::tidy(lm(outcome~ black_and_minority_ethnic_bme_population_all_ages_persons +      emergency_hospital_admissions_for_all_causes_all_ages_standardised_admission_ratio_all_ages_persons + gcse_achievement_5a_c_including_english_maths_15_16_yrs_persons + income_deprivation_english_indices_of_deprivation_2015_all_ages_persons +
 incidences_of_all_cancers_standardised_incidence_ratio_all_ages_persons, data =.x))) %>%
  #filter(p.value < .05) %>%
  arrange(parent_name) %>%
  mutate_if(is.numeric, round, 3) %>%
  select(parent_name, term, estimate, p.value)

glance_lm <- data_lm %>%
  #select(parent_name, outcome, 3:42) %>%
  group_by(parent_name) %>%
  #filter(parent_name == "Barnet" ) %>%
  #group_modify(~ broom::augment(lm(outcome~., data =.x)))
  group_modify(~ broom::glance(lm(outcome~ black_and_minority_ethnic_bme_population_all_ages_persons +      emergency_hospital_admissions_for_all_causes_all_ages_standardised_admission_ratio_all_ages_persons + gcse_achievement_5a_c_including_english_maths_15_16_yrs_persons + income_deprivation_english_indices_of_deprivation_2015_all_ages_persons +
 incidences_of_all_cancers_standardised_incidence_ratio_all_ages_persons, data =.x))) %>%
  #filter(p.value < .05) %>%
  arrange(parent_name)
  
augment_lm <- data_lm %>%
  #select(parent_name, outcome, 3:42) %>%
  group_by(parent_name) %>%
  #filter(parent_name == "Barnet" ) %>%
  #group_modify(~ broom::augment(lm(outcome~., data =.x)))
  group_modify(~ broom::augment(lm(outcome~ black_and_minority_ethnic_bme_population_all_ages_persons +      emergency_hospital_admissions_for_all_causes_all_ages_standardised_admission_ratio_all_ages_persons + gcse_achievement_5a_c_including_english_maths_15_16_yrs_persons + income_deprivation_english_indices_of_deprivation_2015_all_ages_persons +
 incidences_of_all_cancers_standardised_incidence_ratio_all_ages_persons, data =.x))) %>%
  #filter(p.value < .05) %>%
  arrange(parent_name) 

library(gganimate)
plot <- augment_lm %>%
  left_join(glance_lm) %>%
  na.omit() %>%
  ggplot(aes(outcome, .fitted, colour = r.squared, alpha = r.squared)) +
  geom_point() +
  geom_smooth(method = "lm", se = FALSE, colour = "red") +
  geom_abline(intercept = 0, slope = 1, colour = "grey") +
  #facet_wrap(~parent_name) +
  theme_minimal() +
  labs(x = "Life expectancy: m", 
       y = "Predicted life exptectancy") +
  viridis::scale_color_viridis(direction = -1) +
  transition_states(parent_name,
                    transition_length = 3,
                    state_length = 1) +
  ease_aes("linear") + 
  ggtitle("District: {next_state}")
  
animate(plot, nframes = 304, fps = 1.5)