# Load R packages ----
library(NHSRpopulation)
library(dplyr)
library(ggplot2)
library(tidyr)
library(patchwork)
This vignette refers to data that is no longer in this package and is waiting on updates to be made to be able to search for local authority codes as the package currently only matches on postcodes.
Indices of Multiple Deprivation (IMD) describe the deprivation of Lower layer Super Output Areas (LSOAs).
In this example we are looking at the IMD scores and decile ranks for Local Authorities (LAs) in Nottingham City and Nottinghamshire County. First, we define the corresponding Local Authority Codes to make it easier to work with only these regions.
# Nottingham City Local Authority and Nottinghamshire County Council LA areas
# often have their data combined, particularly for the Provider
# Trusts like Nottinghamshire Healthcare NHS Foundation Trust.
# Define code for Nottingham City LA
la_code_nott_city <- c("E06000018")
# Define codes for Nottinghamshire County Council LA districts (further details
# can be found here
# https://cdu-data-science-team.github.io/team-blog/posts/2021-05-14-index-of-multiple-deprivation/#imd-in-sql)
la_code_notts_county <- c(
"E06000018", "E07000170", "E07000171", "E07000172",
"E07000173", "E07000174", "E07000175", "E07000176"
)
# Combine all districts into one object
la_code_nott_city_notts_county <- c(la_code_nott_city, la_code_notts_county)
Lower IMD scores indicate ‘less deprived’ areas while higher IMD scores indicate ‘higher deprivation’. Figure 1 shows the raw scores for each of the different Local Authorities with colour codings for the IMD decile ranks that were calculated using data from all Local Authorities in England.
imd |>
mutate(imd_decile_factor = factor(imd_decile,
levels = 1:10,
labels = c("1 - Most deprived", 2:9, "10 - Least deprived")
)) |>
filter(la_code %in% la_code_nott_city_notts_county) |>
ggplot(aes(x = imd_score, y = la_name, fill = imd_decile_factor)) +
geom_jitter(shape = 21, height = .2, alpha = 0.8, size = 2) +
scale_fill_viridis_d(direction = -1) +
labs(
x = "IMD score",
fill = "ENG IMD decile",
size = "ENG IMD decile",
y = NULL
) +
ggtitle("Fig. 1. IMD scores for LSOAs in Nott City and Notts County") +
scale_x_reverse()
IMD ranks
As mentioned above, these raw IMD scores can be grouped based on their rank, usually 10 groups (deciles) are calculated based on the IMD scores of the entire country. For the decile scores, lower deciles are representing ‘more deprived’ areas while higher deciles represent ‘less deprived’. This can be confusing because IMD scores and IMD deciles are scored in opposite directions.
England IMD deciles
Figure 2.1 below illustrates that the distribution of IMD deciles calculated based on the whole country (England) can be relatively unequal when comparing individual LAs (e.g., Nottingham (City) versus Rushcliffe). In some cases using the deciles calculated based on IMD scores for the entire country may miss important regional differences and it could be useful to recalculate IMD deciles within Local Authorities.
Whilst the Ministry of Housing, Communities & Local Government (2019) has given some guidance on how to calculate and analyse IMD scores, there are no clear examples or guidance when and how to calculate local IMD ranks.
# Plot deciles ----
imd |>
mutate(imd_decile = factor(imd_decile,
levels = 1:10,
labels = c("1 - Most deprived", 2:9, "10 - Least deprived")
)) |>
filter(la_code %in% la_code_nott_city_notts_county) |>
ggplot(aes(factor(imd_decile), fill = factor(imd_decile))) +
geom_bar() +
scale_fill_viridis_d() +
scale_x_discrete(labels = 1:10) +
labs(x = "IMD decile", y = "Count", fill = "IMD decile") +
ggtitle("Fig. 2.1. Combined ENG IMD deciles for LSOAs in Nott City and Notts County")
imd |>
mutate(imd_decile = factor(imd_decile,
levels = 1:10,
labels = c("1 - Most deprived", 2:9, "10 - Least deprived")
)) |>
filter(la_code %in% la_code_nott_city_notts_county) |>
ggplot(aes(factor(imd_decile), fill = factor(imd_decile))) +
geom_bar() +
scale_fill_viridis_d() +
labs(x = "IMD decile", y = "Count", fill = "IMD decile") +
facet_wrap(~la_name, ncol = 2) +
scale_x_discrete(labels = 1:10) +
ggtitle("Fig. 2.2. Individual ENG IMD deciles for LSOAs in Nott City and Notts County")
Calculate ‘local’ IMD deciles within Local Authorities
The following example illustrates how local IMD ranks can be
calculated within a selected group of LAs (see first code chunk where a
list of LA codes is defined:
la_code_nott_city_notts_county
).
To recalculate IMD deciles for Local Authorities the data needs to be grouped by a defined group of Local Authorities (see code below, the variable
la_group
gets calculated for this example) and year of the IMD score (‘group_by(la_code, imd_year)
’).Next, calculate deciles using the function ‘
dplyr::ntile()
’ by specifying ‘n = 10
’.To calculate quintiles (5 groups) the argument would need to be changed to
n = 5
.
To take into account that IMD scores and IMD ranks are scored in the
opposite direction, we need to add a minus before the IMD score:
ntile(-imd_score, n = 10)
.
# Calculate IMD decile ranks (n = 10) for local authorities ----
imd_by_la <- imd |>
mutate(la_groups = case_when(
la_code %in% la_code_nott_city_notts_county ~ "notts_city_county"
)) |>
filter(la_groups == "notts_city_county") |>
group_by(la_groups, imd_year) |>
mutate(imd_decile_la = ntile(-imd_score, n = 10))
Local IMD deciles for Nott City and Notts County
As expected, Figure 3.1 shows an equal distribution of recalculated ‘local’ IMD deciles for the LAs Nottingham City and Nottinghamshire County.
# Plot ENG and NOTTS deciles ----
imd_by_la |>
mutate(imd_decile_la = factor(imd_decile_la,
levels = 1:10,
labels = c("1 - Most deprived", 2:9, "10 - Least deprived")
)) |>
filter(la_code %in% la_code_nott_city_notts_county) |>
ggplot(aes(factor(imd_decile_la), fill = factor(imd_decile_la))) +
geom_bar() +
scale_fill_viridis_d() +
labs(x = "IMD decile", y = "Count", fill = "IMD decile") +
scale_x_discrete(labels = 1:10) +
ggtitle("Fig. 3.1. Combined 'local' IMD deciles for LSOAs in Nott City and Notts County")
Figure 3.2 shows the recalculated ‘local’ IMD ranks for each individual selected LA. There are sill differences in the distributions of ‘local’ IMD ranks across the different LAs. However, these rankings were recalculated using only the IMD scores of the LAs that were of interest and may therefore be a better presentation of the variation across these LAs.
# Plot ENG and NOTTS deciles ----
imd_by_la |>
mutate(imd_decile_la = factor(imd_decile_la,
levels = 1:10,
labels = c("1 - Most deprived", 2:9, "10 - Least deprived")
)) |>
filter(la_code %in% la_code_nott_city_notts_county) |>
ggplot(aes(factor(imd_decile_la), fill = factor(imd_decile_la))) +
geom_bar() +
scale_fill_viridis_d() +
labs(x = "IMD decile", y = "Count", fill = "IMD decile") +
facet_wrap(~la_name, ncol = 2) +
scale_x_discrete(labels = 1:10) +
ggtitle("Fig. 3.2. Individual 'local' IMD deciles for LSOAs in Nott City and Notts County")
Compare distributions of IMD deciles
The changes in IMD decile ranks can be seen when comparing the bar plots shown above (e.g., Fig 2.2 vs. Fig 3.2) – but they can be hard to spot.
Changes in the distribution of IMD deciles (see density plots in Figure 4) illustrate the effect of recalculating ‘local’ IMD scores a bit clearer.
The plot on the left with the title ‘ENG IMD’ shows the distribution of IMD deciles when all IMD scores in England were used to calculate the decile ranks.
The plot on the right (Local IMD) shows the distribution of IMD deciles when only the IMD scores of the selected LAs were used.
The effect of recalculating local IMD scores can be seen in changes in the shape of the density distribution (e.g., narrow shape vs flat shape, changes of the peak on the x axis) for the individual IMD deciles.
imd_by_la |>
pivot_longer(
cols = c(imd_decile, imd_decile_la),
names_to = "imd_method", values_to = "imd_value"
) |>
mutate(imd_value = factor(imd_value,
levels = 1:10,
labels = c("1 - Most deprived", 2:9, "10 - Least deprived")
)) |>
filter(la_code %in% la_code_nott_city_notts_county) |>
ggplot(aes(x = imd_score, fill = factor(imd_value))) +
scale_fill_viridis_d(direction = 1) +
labs(x = "IMD score", y = NULL, fill = "IMD decile") +
geom_density(alpha = .6, adjust = 2) +
facet_wrap(~ factor(imd_method,
levels = c(
"imd_decile",
"imd_decile_la"
),
labels = c(
"END IMD",
"Local IMD"
)
)) +
ggtitle("Fig. 4. Distributions of ENG and local IMD deciles")
Additional resources
A blog post by Turner (2021) also discusses how to calculate local IMD scores illustrating examples in SQL and R.
Notes for anyone contributing to the html and vignettes
The pages built here https://nhs-r-community.github.io/NHSRpopulation/ have
been done using the package {pkgdown}. In order to rebuild the
pages after any changes use pkgdown::build_site()
in the
console. There will be a number of changes to the supporting files in
the folder docs/
which all then need to be committed.