Skip to content

Commit

Permalink
Merge pull request #32 from humaniverse/homelessness-data
Browse files Browse the repository at this point in the history
Added homelessness and crime severity data
  • Loading branch information
jennajt authored May 23, 2024
2 parents 8dc4ab7 + 6bca1e7 commit 5a02f0a
Show file tree
Hide file tree
Showing 17 changed files with 468 additions and 0 deletions.
69 changes: 69 additions & 0 deletions R/data.R
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,34 @@ NULL
#' @source \url{https://www.england.nhs.uk/}
"england_rtt_flow_stp"

#' Statutory homelessness in England
#'
#' A dataset containing statistics on households assessed as homeless and
#' households in temporary accommodation, by Local Authority.
#'
#' @format A data frame with 3 variables:
#' \describe{
#' \item{ltla23_code}{Local Authority code}
#' \item{Households assessed as homeless per (000s)}{Households assessed as homeless per 1,000 households in the Local Authority}
#' \item{Households in temporary accommodation per 1,000}{Households in temporary accommodation per 1,000 households in the Local Authority}
#' }
#' @source \url{https://www.gov.uk/government/statistical-data-sets/live-tables-on-homelessness}
"england_homelessness"

#' Crime severity score in England
#'
#' Crime severity score is based on sentencing data, community order and fines.
#' Higher scores mean greater severity within an area.
#'
#' @format A data frame containing 2 columns.
#' \describe{
#' \item{ltla23_code}{Local Authority code}
#' \item{crime_severity_score}{Crime severity score (higher = more severe)}
#' }
#' @source \url{https://www.ons.gov.uk/peoplepopulationandcommunity/crimeandjustice/datasets/crimeseverityscoreexperimentalstatistics}
"england_crime_severity"


#' Bed Availability in Northern Ireland
#'
#' A dataset containing hospital statistics on bed availability, bed occupancy
Expand Down Expand Up @@ -444,6 +472,20 @@ NULL
#' @source \url{https://www.opendata.nhs.scot/}
"scotland_rtt_hb"

#' Homelessness in Scotland
#'
#' A dataset containing statistics on households assessed as homeless and
#' households in temporary accommodation, by Local Authority.
#'
#' @format A data frame with 3 variables:
#' \describe{
#' \item{ltla21_code}{Local Authority code}
#' \item{Assessed as homeless or threatened with homelessness per 1,000}{Households assessed as homeless per 1,000 households in the Local Authority}
#' \item{Households in temporary accommodation per 1,000}{Households in temporary accommodation per 1,000 households in the Local Authority}
#' }
#' @source \url{https://www.gov.scot/collections/homelessness-statistics/}
"scotland_homelessness"

#' Consultant-led Referral to Treatment (RTT) by Local Health Board
#'
#' A dataset containing RTT data for Wales.
Expand Down Expand Up @@ -522,3 +564,30 @@ NULL
#' }
#' @source \url{https://statswales.gov.wales/}
"wales_hospitals_critical_general_acute_beds"

#' Homelessness in Wales
#'
#' A dataset containing statistics on households assessed as homeless and
#' households in temporary accommodation, by Local Authority.
#'
#' @format A data frame with 3 variables:
#' \describe{
#' \item{ltla21_code}{Local Authority code}
#' \item{Homeless or threatened with homelessness per 1,000}{Households assessed as homeless per 1,000 households in the Local Authority}
#' \item{Households in temporary accommodation per 1,000}{Households in temporary accommodation per 1,000 households in the Local Authority}
#' }
#' @source \url{https://statswales.gov.wales/}
"wales_homelessness"

#' Crime severity score in Wales
#'
#' Crime severity score is based on sentencing data, community order and fines.
#' Higher scores mean greater severity within an area.
#'
#' @format A data frame containing 2 columns.
#' \describe{
#' \item{ltla23_code}{Local Authority code}
#' \item{crime_severity_score}{Crime severity score (higher = more severe)}
#' }
#' @source \url{https://www.ons.gov.uk/peoplepopulationandcommunity/crimeandjustice/datasets/crimeseverityscoreexperimentalstatistics}
"wales_crime_severity"
Binary file modified R/sysdata.rda
Binary file not shown.
51 changes: 51 additions & 0 deletions data-raw/england-homelessness.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
library(tidyverse)
library(compositr)
library(readODS)
library(devtools)

# ---- Load internal sysdata.rda file with URLs ----
load_all(".")

# ---- Download data ----
query_url <-
query_urls |>
filter(id == "england_homelessness") |>
pull(query)

tf <- download_file(query_url, ".ods")

# ---- Households assessed as homeless ----
england_homeless_raw <- read_ods(tf, sheet = "A1", skip = 5)

england_homeless <- england_homeless_raw[, c(1, 18)]
names(england_homeless) <- c("ltla23_code", "Households assessed as homeless per (000s)")

# Keep only Local Authority Districts
england_homeless <-
england_homeless |>
as_tibble() |>
filter(str_detect(ltla23_code, "^E0[6-9]")) |>
mutate(
`Households assessed as homeless per (000s)` = as.numeric(`Households assessed as homeless per (000s)`)
)

# ---- Temporary accommodation ----
england_temp_accomm_raw <- read_ods(tf, sheet = "TA1", skip = 6)

england_temp_accomm <- england_temp_accomm_raw[, c(1, 2, 7)]
names(england_temp_accomm) <- c("ltla23_code", "ltla21_name", "Total number of households in TA per (000s)")

# Keep only Local Authority Districts
england_temp_accomm <-
england_temp_accomm |>
as_tibble() |>
filter(str_detect(ltla23_code, "^E0[6-9]")) |>
mutate(`Households in temporary accommodation per 1,000` = as.numeric(`Total number of households in TA per (000s)`)) |>
select(ltla23_code, `Households in temporary accommodation per 1,000`)

# ---- Combine and save data ----
england_homelessness <-
england_homeless |>
left_join(england_temp_accomm)

usethis::use_data(england_homelessness, overwrite = TRUE)
47 changes: 47 additions & 0 deletions data-raw/england-wales-crime-severity.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
library(tidyverse)
library(compositr)
library(readxl)
library(sf)

# ---- Load internal sysdata.rda file with URL's ----
load_all(".")

# ---- Load lookup table: Local Authority to Community Safety Partnership ----
#TODO: Move this code to {geographr}
# Source: https://geoportal.statistics.gov.uk/search?q=LUP_LAD_CSP_PFA&sort=Date%20Created%7Ccreated%7Cdesc
lookup_ltla_csp <- read_sf("https://services1.arcgis.com/ESMARspQHYMw9BZ9/arcgis/rest/services/LAD23_CSP23_PFA23_EW_LU/FeatureServer/0/query?outFields=*&where=1%3D1&f=geojson")

lookup_ltla_csp <-
lookup_ltla_csp |>
st_drop_geometry() |>
select(ltla23_code = LAD23CD, csp23_code = CSP23CD)

# ---- Load crime severity data ----
query_url <-
query_urls |>
filter(id == "england_wales_crime_severity") |>
pull(query)

tf <- download_file(query_url, ".xls")

crime_severity_raw <- read_excel(tf, sheet = "Data - csp", skip = 1)

crime_severity <-
crime_severity_raw |>
filter(`Offence group` == "Total recorded crime") |>
select(csp23_code = Code, crime_severity_score = `Apr '22 to \nMar '23...25`) |>

left_join(lookup_ltla_csp) |>
select(ltla23_code, crime_severity_score)

england_crime_severity <-
crime_severity |>
filter(str_detect(ltla23_code, "^E"))

wales_crime_severity <-
crime_severity |>
filter(str_detect(ltla23_code, "^W"))

# ---- Save output to data/ folder ----
usethis::use_data(england_crime_severity, overwrite = TRUE)
usethis::use_data(wales_crime_severity, overwrite = TRUE)
2 changes: 2 additions & 0 deletions data-raw/query-urls.R
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ query_urls <-
"A&E Attendances and Emergency Admissions", "nhs_accident_emergency_november_23", "November 2023", "OGLv3", "https://www.england.nhs.uk/statistics/wp-content/uploads/sites/2/2023/12/November-2023-AE-by-provider-gPRdZ.xls", "https://www.england.nhs.uk/statistics/statistical-work-areas/ae-waiting-times-and-activity/ae-attendances-and-emergency-admissions-2023-24/",
"A&E Attendances and Emergency Admissions", "nhs_accident_emergency_december_23", "December 2023", "OGLv3", "https://www.england.nhs.uk/statistics/wp-content/uploads/sites/2/2024/01/December-2023-AE-by-provider-a5iwg.xls", "https://www.england.nhs.uk/statistics/statistical-work-areas/ae-waiting-times-and-activity/ae-attendances-and-emergency-admissions-2023-24/",
"A&E Providers to Acute Trusts Lookup", "acute_trust_attribution", "June 2022", "OGLv3", "https://www.england.nhs.uk/statistics/wp-content/uploads/sites/2/2023/02/June-2022-Acute-Trust-Attribution-File.xls", "https://www.england.nhs.uk/statistics/statistical-work-areas/ae-waiting-times-and-activity/ae-attendances-and-emergency-admissions-2022-23/",
"Crime severity", "england_wales_crime_severity", "", "", "https://www.ons.gov.uk/file?uri=/peoplepopulationandcommunity/crimeandjustice/datasets/crimeseverityscoreexperimentalstatistics/current/datatool.xls", "https://www.ons.gov.uk/peoplepopulationandcommunity/crimeandjustice/datasets/crimeseverityscoreexperimentalstatistics",
"Critical care and General & Acute Beds – Urgent and Emergency Care Daily Situation Reports", "nhs_critical_general_acute_beds_april_22", "April 2022", "OGLv3", "https://www.england.nhs.uk/statistics/wp-content/uploads/sites/2/2022/05/202204-April-2022-sitrep-data-FINAL.xlsx", "https://www.england.nhs.uk/statistics/statistical-work-areas/bed-availability-and-occupancy/critical-care-and-general-acute-beds-urgent-and-emergency-care-daily-situation-reports/critical-care-and-general-acute-beds-urgent-and-emergency-care-daily-situation-reports-2022-23/",
"Critical care and General & Acute Beds – Urgent and Emergency Care Daily Situation Reports", "nhs_critical_general_acute_beds_may_22", "May 2022", "OGLv3", "https://www.england.nhs.uk/statistics/wp-content/uploads/sites/2/2022/06/202205-May-2022-sitrep-data-FINAL.xlsx", "https://www.england.nhs.uk/statistics/statistical-work-areas/bed-availability-and-occupancy/critical-care-and-general-acute-beds-urgent-and-emergency-care-daily-situation-reports/critical-care-and-general-acute-beds-urgent-and-emergency-care-daily-situation-reports-2022-23/",
"Critical care and General & Acute Beds – Urgent and Emergency Care Daily Situation Reports", "nhs_critical_general_acute_beds_june_22", "June 2022", "OGLv3", "https://www.england.nhs.uk/statistics/wp-content/uploads/sites/2/2022/07/202206-June-2022-sitrep-data-FINAL.xlsx", "https://www.england.nhs.uk/statistics/statistical-work-areas/bed-availability-and-occupancy/critical-care-and-general-acute-beds-urgent-and-emergency-care-daily-situation-reports/critical-care-and-general-acute-beds-urgent-and-emergency-care-daily-situation-reports-2022-23/",
Expand All @@ -61,6 +62,7 @@ query_urls <-
"Critical care and General & Acute Beds – Urgent and Emergency Care Daily Situation Reports", "nhs_critical_general_acute_beds_december_23", "December 2023", "OGLv3", "https://www.england.nhs.uk/statistics/wp-content/uploads/sites/2/2024/01/202312-December-2023-sitrep-data-FINAL.xlsx", "https://www.england.nhs.uk/statistics/statistical-work-areas/bed-availability-and-occupancy/critical-care-and-general-acute-beds-urgent-and-emergency-care-daily-situation-reports/critical-care-and-general-acute-beds-urgent-and-emergency-care-daily-situation-reports-2023-24/",
"Health Index", "england_health_index_2021", "June 2021", "OGLv3", "https://www.ons.gov.uk/file?uri=/peoplepopulationandcommunity/healthandsocialcare/healthandwellbeing/datasets/healthindexscoresengland/current/healthindexscoresengland.xlsx", "https://www.ons.gov.uk/peoplepopulationandcommunity/healthandsocialcare/healthandwellbeing/datasets/healthindexscoresengland",
"Health Index underlying indicators", "england_health_index_2021_indicators", "June 2021", "OGLv3", "https://www.ons.gov.uk/file?uri=/peoplepopulationandcommunity/healthandsocialcare/healthandwellbeing/datasets/healthindexunderlyingdataengland/current/healthindexunderlyingdataenglandcanscreenfix.xlsx", "https://www.ons.gov.uk/peoplepopulationandcommunity/healthandsocialcare/healthandwellbeing/datasets/healthindexunderlyingdataengland",
"Homelessness", "england_homelessness", "October to December 2023", "", "https://assets.publishing.service.gov.uk/media/662f6eccce557c60ed19ad15/Detailed_LA_202312.ods", "https://www.gov.uk/government/statistical-data-sets/live-tables-on-homelessness",
"Hospital discharge (criteria to reside)", "nhs_hospital_discharge_data_april_22", "April 2022", "OGLv3", "https://www.england.nhs.uk/statistics/wp-content/uploads/sites/2/2022/08/Daily-discharge-sitrep-monthly-data-webfile-April2022-v2.xlsx", "https://www.england.nhs.uk/statistics/statistical-work-areas/discharge-delays-acute-data/",
"Hospital discharge (criteria to reside)", "nhs_hospital_discharge_data_may_22", "May 2022", "OGLv3", "https://www.england.nhs.uk/statistics/wp-content/uploads/sites/2/2022/08/Daily-discharge-sitrep-monthly-data-webfile-May2022-v2.xlsx", "https://www.england.nhs.uk/statistics/statistical-work-areas/discharge-delays-acute-data/",
"Hospital discharge (criteria to reside)", "nhs_hospital_discharge_data_june_22", "June 2022", "OGLv3", "https://www.england.nhs.uk/statistics/wp-content/uploads/sites/2/2022/08/Daily-discharge-sitrep-monthly-data-webfile-June2022-v2.xlsx", "https://www.england.nhs.uk/statistics/statistical-work-areas/discharge-delays-acute-data/",
Expand Down
90 changes: 90 additions & 0 deletions data-raw/scotland_homelessness.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
library(tidyverse)
library(compositr)
library(readxl)
library(devtools)

# ---- Load internal sysdata.rda file with URLs ----
load_all(".")

# ---- Council Area names and codes ----
#TODO: Move this code to {geographr}
scottish_ltla_names_codes <-
geographr::boundaries_ltla21 |>
sf::st_drop_geometry() |>
filter(str_detect(ltla21_code, "^S")) |>

# Match to LA names in the homelessness data
mutate(ltla21_name = str_replace(ltla21_name, " and ", " & ")) |>
mutate(
ltla21_name = case_match(
ltla21_name,
"Na h-Eileanan Siar" ~ "Eilean Siar",
"City of Edinburgh" ~ "Edinburgh",
"Shetland Islands" ~ "Shetland",
.default = ltla21_name
)
)

# ---- Household estimates ----
# Source: https://statistics.gov.scot/resource?uri=http%3A%2F%2Fstatistics.gov.scot%2Fdata%2Fhousehold-estimates
#TODO: Move this code to {demographr}
scotland_households <- read_csv("https://statistics.gov.scot/slice/observations.csv?&dataset=http%3A%2F%2Fstatistics.gov.scot%2Fdata%2Fhousehold-estimates&http%3A%2F%2Fpurl.org%2Flinked-data%2Fcube%23measureType=http%3A%2F%2Fstatistics.gov.scot%2Fdef%2Fmeasure-properties%2Fcount&http%3A%2F%2Fpurl.org%2Flinked-data%2Fsdmx%2F2009%2Fdimension%23refPeriod=http%3A%2F%2Freference.data.gov.uk%2Fid%2Fyear%2F2021", skip = 7)

scotland_households <-
scotland_households |>
mutate(ltla21_code = str_extract(`http://purl.org/linked-data/sdmx/2009/dimension#refArea`, "S[0-9]+")) |>
filter(str_detect(ltla21_code, "^S12")) |>
select(
ltla21_code,
`Number of households` = `Which Are Occupied`
)

# ---- Homelessness applications by local authority ----
# Source: https://www.gov.scot/publications/homelessness-in-scotland-update-to-30-september-2023/documents/
# Main page: https://www.gov.scot/collections/homelessness-statistics/
tf <- download_file("https://www.gov.scot/binaries/content/documents/govscot/publications/statistics/2024/02/homelessness-in-scotland-update-to-30-september-2023/documents/tables-to-homelessness-in-scotland-update-to-30-sep-2023/tables-to-homelessness-in-scotland-update-to-30-sep-2023/govscot%3Adocument/Homelessness%2Bin%2BScotland%2Bupdate%2Bto%2B30%2BSep%2B2023%2B-%2BFinal.xlsx", ".xlsx")

scotland_homelessness <- read_excel(tf, sheet = "T8", range = "A5:P38")

scotland_homelessness <-
scotland_homelessness |>
select(ltla21_name = `Local Authority`, `2022\r\nOct-Dec`:`2023\r\nJul-Sep`) |>
filter(ltla21_name != "Scotland") |>

rowwise() |>
mutate(applications = sum(c_across(where(is.double)))) |>
ungroup() |>

left_join(scottish_ltla_names_codes) |>

# Calculate rate
left_join(scotland_households) |>
mutate(`Assessed as homeless or threatened with homelessness per 1,000` = applications / `Number of households` * 1000) |>
select(ltla21_code, `Assessed as homeless or threatened with homelessness per 1,000`)

# ---- Temporary accommodation ----
# Use more up-to-date data
scotland_temp_accom_raw <- read_excel(tf, sheet = "T15", range = "A5:P38")

scotland_temp_accom <-
scotland_temp_accom_raw |>
select(ltla21_name = `Local Authority`, `2022\r\n31 Dec`:`2023\r\n30 Sep`) |>
filter(ltla21_name != "Scotland") |>

rowwise() |>
mutate(temp_accomm = sum(c_across(where(is.double)))) |>
ungroup() |>

left_join(scottish_ltla_names_codes) |>

# Calculate rate
left_join(scotland_households) |>
mutate(`Households in temporary accommodation per 1,000` = temp_accomm / `Number of households` * 1000) |>
select(ltla21_code, `Households in temporary accommodation per 1,000`)

# ---- Combine and save data ----
scotland_homelessness <-
scotland_homelessness |>
left_join(scotland_temp_accom)

usethis::use_data(scotland_homelessness, overwrite = TRUE)
86 changes: 86 additions & 0 deletions data-raw/wales-homelessness.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
library(tidyverse)

source("https://github.com/matthewgthomas/brclib/raw/master/R/download_wales.R")

# ---- Household estimates ----
# Dwelling stock estimates by local authority and tenure
# Source: https://statswales.gov.wales/Catalogue/Housing/Dwelling-Stock-Estimates
wales_households_raw <- download_wales("http://open.statswales.gov.wales/en-gb/dataset/hous0501")

wales_households <-
wales_households_raw |>
mutate(Year_Code = as.integer(Year_Code)) |>
filter(
Year_Code == max(Year_Code) &
!Area_Hierarchy %in% c("0", "596") & # Keep only Local Authorities
Tenure_ItemName_ENG == "All tenures (Number)"
) |>
select(
Area_Code,
ltla21_name = Area_ItemName_ENG,
`Number of households` = Data
) |>
mutate(`Number of households` = as.numeric(`Number of households`))

# ---- Homelessness ----
# Households for which assistance has been provided by outcome and household type
# Source: https://statswales.gov.wales/Catalogue/Housing/Homelessness/Statutory-Homelessness-Prevention-and-Relief
wales_homelessness_raw <- download_wales("http://open.statswales.gov.wales/en-gb/dataset/hous0413")

#!! I don't think `number of outcomes` is what we want - needs to be number of households
wales_homelessness <-
wales_homelessness_raw |>
filter(
Period_Code == "2022230",
str_detect(Area_AltCode1, "^W06"),
Household_ItemName_ENG == "Total",
Outcomes_ItemName_ENG == "Total prevention / Relief"
# str_detect(Outcomes_ItemName_ENG, "Number of outcomes")
) |>

mutate(Data = if_else(Data >= 0, Data, NA_real_)) |>

group_by(Area_AltCode1, Area_Code) |>
summarise(`Homeless or threatened with homelessness` = sum(Data, na.rm = TRUE)) |>
ungroup()

# Calculate homelessness rate
wales_homelessness <-
wales_homelessness |>
left_join(wales_households) |>
mutate(`Homeless or threatened with homelessness per 1,000` = `Homeless or threatened with homelessness` / `Number of households` * 1000) |>
select(ltla21_code = Area_AltCode1, `Homeless or threatened with homelessness per 1,000`)

# ---- Temporary accommodation ----
# Households accommodated temporarily by accommodation type and household type
# Source: https://statswales.gov.wales/Catalogue/Housing/Homelessness/Temporary-Accommodation
wales_temp_accom_raw <- download_wales("http://open.statswales.gov.wales/en-gb/dataset/hous0420")

wales_temp_accom <-
wales_temp_accom_raw |>
filter(
Period_Code == "202324Q2",
str_detect(Area_AltCode1, "^W06"),
Household_ItemName_ENG == "Total",
Time_ItemName_ENG == "Total"
) |>

mutate(Data = if_else(Data >= 0, Data, NA_real_)) |>

group_by(Area_AltCode1, Area_Code) |>
summarise(`Households in temporary accommodation` = sum(Data, na.rm = TRUE)) |>
ungroup()

# Calculate rate
wales_temp_accom <-
wales_temp_accom |>
left_join(wales_households) |>
mutate(`Households in temporary accommodation per 1,000` = `Households in temporary accommodation` / `Number of households` * 1000) |>
select(ltla21_code = Area_AltCode1, `Households in temporary accommodation per 1,000`)

# ---- Combine and save data ----
wales_homelessness <-
wales_homelessness |>
left_join(wales_temp_accom)

usethis::use_data(wales_homelessness, overwrite = TRUE)
Binary file added data/england_crime_severity.rda
Binary file not shown.
Binary file added data/england_homelessness.rda
Binary file not shown.
Binary file added data/scotland_homelessness.rda
Binary file not shown.
Binary file added data/wales_crime_severity.rda
Binary file not shown.
Binary file added data/wales_homelessness.rda
Binary file not shown.
Loading

0 comments on commit 5a02f0a

Please sign in to comment.