Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Healthy life expectancy #37

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions R/data.R
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,22 @@
#' @source \url{https://www.ons.gov.uk/datasets/wellbeing-local-authority/editions/time-series/versions/4}
"people_happiness"

#' Average Life Expectancy for Men and Women (2020-2022)
#'
#' A dataset containing statistics on average life expectancy for men
#' and women, by Northern Irish Council (2020-2022).
#'
#' @format A data frame with 11 rows and 3 variables:
#' \describe{
#' \item{ltla24_code}{Local Authority Code}
#' \item{life_expectancy_combined}{Average life expectancy for men and women}
#' \item{year}{Time period - three year aggregate}
#'
#' ...
#' }
#' @source \url{https://www.ons.gov.uk/peoplepopulationandcommunity/healthandsocialcare/healthandlifeexpectancies/datasets/lifeexpectancyforlocalareasinenglandnorthernirelandandwalesbetween2001to2003and2020to2022}
"people_life_expectancy"

#' Average Measurement of Life Worthwhileness Out of 10 (2022-23)
#'
#' A dataset containing statistics of personal ratings on feelings of
Expand Down
142 changes: 39 additions & 103 deletions data-raw/healthy-people/healthy-life-expectancy.R
Original file line number Diff line number Diff line change
@@ -1,124 +1,60 @@
# ---- Load libraries ----
# ---- Load packages ----
library(tidyverse)
library(httr)
library(readxl)
library(geographr)
library(sf)

source("R/utils.R")
# ---- Get and clean data ----
# Life Expectancy (2020-2022)
# Source: https://www.ons.gov.uk/peoplepopulationandcommunity/healthandsocialcare/healthandlifeexpectancies/datasets/lifeexpectancyforlocalareasinenglandnorthernirelandandwalesbetween2001to2003and2020to2022

# ---- Retrieve data ----
GET(
"https://www.ons.gov.uk/file?uri=%2fpeoplepopulationandcommunity%2fhealthandsocialcare%2fhealthandlifeexpectancies%2fdatasets%2fhealthstatelifeexpectancyatbirthandatage65bylocalareasuk%2fcurrent/hsleatbirthandatage65byukla201618.xlsx",
write_disk(tf <- tempfile(fileext = ".xlsx"))
)
url <- "https://www.ons.gov.uk/file?uri=/peoplepopulationandcommunity/healthandsocialcare/healthandlifeexpectancies/datasets/lifeexpectancyforlocalareasinenglandnorthernirelandandwalesbetween2001to2003and2020to2022/between2001to2003and2020to2022/lifeexpectancylocalareas.xlsx"
temp_file <- tempfile(fileext = ".xlsx")
download.file(url, temp_file, mode = "wb")

raw_males <-
read_excel(
tf,
sheet = "HE - Male at birth",
range = "A4:I490"
)
le_raw <- read_excel(temp_file, sheet = 5, skip = 11)

hle_males <-
raw_males |>
select(
lad_code = `Area Codes`,
healthy_life_expectancy_male = HLE
) |>
filter_codes(lad_code, "^N")

raw_females <-
read_excel(
tf,
sheet = "HE - Female at birth",
range = "A4:I490"
)
# Life Expectancy (Male)

hle_females <-
raw_females |>
select(
lad_code = `Area Codes`,
healthy_life_expectancy_female = HLE
male_le <- le_raw |>
filter(
Sex == "Male",
str_starts(`Area code`, "N"),
`Age group` == "<1",
`Area code` != "N92000002"
) |>
filter_codes(lad_code, "^N")

hle_joined <-
hle_males |>
left_join(hle_females)

# ---- Calculate female/male weighted population estimates ----
GET(
"https://www.ons.gov.uk/file?uri=/peoplepopulationandcommunity/populationandmigration/populationestimates/datasets/populationestimatesforukenglandandwalesscotlandandnorthernireland/mid2019april2020localauthoritydistrictcodes/ukmidyearestimates20192020ladcodes.xls",
write_disk(tf <- tempfile(fileext = ".xls"))
)

# Total pop
raw_pop_total <-
read_excel(
tf,
sheet = "MYE2 - Persons",
range = "A5:D431"
)

pop_total <-
raw_pop_total |>
select(
lad_code = Code, pop_total = `All ages`
) |>
filter_codes(lad_code, "^N")

# Males
raw_pop_male <-
read_excel(
tf,
sheet = "MYE2 - Males",
range = "A5:D431"
ltla24_code = `Area code`,
life_expectancy_male = `Life expectancy (years)...62`
)

pop_male <-
raw_pop_male |>
select(
lad_code = Code, pop_male = `All ages`
) |>
filter_codes(lad_code, "^N")
# Life Expectancy (Female)

# Females
raw_pop_female <-
read_excel(
tf,
sheet = "MYE2 - Females",
range = "A5:D431"
female_le <- le_raw |>
filter(
Sex == "Female",
str_starts(`Area code`, "N"),
`Age group` == "<1",
`Area code` != "N92000002"
) |>
select(
ltla24_code = `Area code`,
life_expectancy_female = `Life expectancy (years)...62`
)

pop_female <-
raw_pop_female |>
select(
lad_code = Code, pop_female = `All ages`
) |>
filter_codes(lad_code, "^N")

# Calculate proportions
pop_proportions <-
pop_total |>
left_join(pop_male) |>
left_join(pop_female) |>
# Join datasets
people_life_expectancy <- male_le |>
left_join(female_le, by = "ltla24_code") |>
rowwise() |>
mutate(
proportion_male = pop_male / pop_total,
proportion_female = pop_female / pop_total
life_expectancy_combined = (life_expectancy_male + life_expectancy_female) / 2,
year = "2020-2022"
) |>
ungroup() |>
select(lad_code, starts_with("proportion"))

# Compute population weighted mean HLE
hle <-
hle_joined |>
left_join(pop_proportions) |>
rowwise(lad_code) |>
summarise(
healthy_life_expectancy = (healthy_life_expectancy_male * proportion_male) + (healthy_life_expectancy_female * proportion_female),
.groups = "drop"
select(
ltla24_code,
life_expectancy_combined,
year
)

write_rds(hle, "data/vulnerability/health-inequalities/northern-ireland/healthy-people/healthy-life-expectancy.rds")
# ---- Save output to data/ folder ----
usethis::use_data(people_life_expectancy, overwrite = TRUE)
Binary file added data/people_life_expectancy.rda
Binary file not shown.
27 changes: 27 additions & 0 deletions man/people_life_expectancy.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion metadata.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
| Healthy People | Suicides | 2019 | [NISRA](https://www.ninis2.nisra.gov.uk) | [OGL3](https://www.nisra.gov.uk/crown-copyright) | :heavy_exclamation_mark: Data unavailable - contact info@nisra.gov.uk and ask for Deaths by Cause (administrative geographies) | :x: |
| Healthy People | Avoidable Deaths | 2015-19 | [NISRA](https://www.ninis2.nisra.gov.uk) | [OGL3](https://www.nisra.gov.uk/crown-copyright) | :heavy_exclamation_mark: Data unavailable - contact healthinequalities@health-ni.gov.uk and ask for Standardised Death Rate - Avoidable (administrative geographies) | :x: |
| Healthy People | Mortality from All Causes | 2020-22 | [NISRA](https://data.nisra.gov.uk/) | [OGL3](https://www.nationalarchives.gov.uk/doc/open-government-licence/version/3/) | :heavy_check_mark: Indicator: Age/sex-standardised all mortality rates per 100k | :heavy_check_mark: |
| Healthy People | Healthy Life Expectancy | 2016-2018 | [ONS](https://www.ons.gov.uk/peoplepopulationandcommunity/healthandsocialcare/healthandlifeexpectancies/datasets/healthstatelifeexpectancyatbirthandatage65bylocalareasuk) | [OGL3](https://www.nationalarchives.gov.uk/doc/open-government-licence/version/3/) | :heavy_check_mark: | :heavy_check_mark: |
| Healthy People | Life Expectancy | 2020-2022 | [ONS](https://www.ons.gov.uk/peoplepopulationandcommunity/healthandsocialcare/healthandlifeexpectancies/datasets/lifeexpectancyforlocalareasinenglandnorthernirelandandwalesbetween2001to2003and2020to2022) | [OGL3](https://www.nationalarchives.gov.uk/doc/open-government-licence/version/3/) | :heavy_check_mark: Average healthy life expectancy for men and women combined. | :heavy_check_mark: |
| Healthy People | Happiness | 2022-23 | [ONS](https://www.ons.gov.uk/datasets/wellbeing-local-authority/editions/time-series/versions/4) | [OGL3](https://www.nationalarchives.gov.uk/doc/open-government-licence/version/3/) | :heavy_check_mark: Indicator: Average personal ratings on feelings of happiness out of 10.| :heavy_check_mark: |
| Healthy People | Anxiety | 2022-23 | [ONS](https://www.ons.gov.uk/datasets/wellbeing-local-authority/editions/time-series/versions/4) | [OGL3](https://www.nationalarchives.gov.uk/doc/open-government-licence/version/3/) | :heavy_check_mark: Indicator: Average personal ratings on feelings of anxiety out of 10. | :heavy_check_mark: |
| Healthy People | Life Satisfaction | 2022-23 | [ONS](https://www.ons.gov.uk/datasets/wellbeing-local-authority/editions/time-series/versions/4) | [OGL3](https://www.nationalarchives.gov.uk/doc/open-government-licence/version/3/) | :heavy_check_mark: Indicator: Average personal ratings on feelings of life satisfaction out of 10.| :heavy_check_mark: |
Expand Down