-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #32 from humaniverse/homelessness-data
Added homelessness and crime severity data
- Loading branch information
Showing
17 changed files
with
468 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Oops, something went wrong.