Title: | Streamlines the Process of Fitting Areal Spatial Models |
---|---|
Description: | Helpers for addressing the issue of disconnected spatial units. It allows for convenient adding and removal of neighbourhood connectivity between areal units prior to modelling, with the visual aid of maps. Post-modelling, it reduces the human workload for extracting, tidying and mapping predictions from areal models. |
Authors: | Kevin Horan [aut, cre, cph] |
Maintainer: | Kevin Horan <[email protected]> |
License: | MIT + file LICENSE |
Version: | 1.1.2 |
Built: | 2025-03-02 17:18:16 UTC |
Source: | https://github.com/horankev/sfislands |
Augment dataframe with predictions of model
st_augment(model, df)
st_augment(model, df)
model |
an 'mgcv', 'lme4' or 'nlme' model. |
df |
an 'sf' data frame to be augmented with model predictions. |
An augmented 'sf' data frame with extra columns showing estimates of random effects from model.
prepdata <- st_bridges(uk_election,"constituency_name") mgcv::gam(health_not_good ~ s(constituency_name, bs='mrf', xt=list(nb=prepdata$nb), k=100), data=prepdata, method="REML") |> st_augment(uk_election)
prepdata <- st_bridges(uk_election,"constituency_name") mgcv::gam(health_not_good ~ s(constituency_name, bs='mrf', xt=list(nb=prepdata$nb), k=100), data=prepdata, method="REML") |> st_augment(uk_election)
Create first-order queen contiguity neighbourhood structure with additional connections when islands are present, ensuring that there are no unconnected units
st_bridges( df, row_identifier, remove_islands = FALSE, link_islands_k = 1, nb_structure = "list", add_to_dataframe = TRUE, threshold = 1.001, geom_col_name = lifecycle::deprecated() )
st_bridges( df, row_identifier, remove_islands = FALSE, link_islands_k = 1, nb_structure = "list", add_to_dataframe = TRUE, threshold = 1.001, geom_col_name = lifecycle::deprecated() )
df |
an 'sf' or 'sfc' object. |
row_identifier |
name of a column from 'df' containing names (or unique identifiers) for each row. |
remove_islands |
default 'FALSE'. Whether or not to omit islands from contiguity construction. |
link_islands_k |
an integer, k. The number of nearest units to which each island should be connected. |
nb_structure |
default '"list"'. Can also be '"matrix"'. The format in which to return the named contiguity structure. |
add_to_dataframe |
default 'TRUE'. Whether or not to augment existing df with contiguity output as '"nb"' column. 'FALSE' returns only the contiguity structure. |
threshold |
default 1.001. factor by which to change the size of buffer automatically generated around islands to account for imprecisions which may arise from sf::st_buffer() functionality. This can be increased if connections to islands are not occurring as expected. |
geom_col_name |
name of a column from 'df' containing names (or unique identifiers) for each row. This argument is now deprecated and the new "row_identifier" argument replaces it with the same functionality. |
Either a named neighbourhood list or matrix, or an 'sf' dataframe with list or matrix included as '"nb"' column.
st_bridges(uk_election,"constituency_name")
st_bridges(uk_election,"constituency_name")
Examine contiguity actions which have been performed on islands by 'st_bridges()'
st_check_islands(data)
st_check_islands(data)
data |
an 'sf' dataframe with a neighbourhood column called '"nb"' such as the output of 'st_bridges()'. |
A dataframe reporting non-contiguous connections made by 'st_bridges()'.
st_bridges(uk_election,"constituency_name") |> st_check_islands()
st_bridges(uk_election,"constituency_name") |> st_check_islands()
Remove contiguity between pairs of areas
st_force_cut_nb(nb, x = NULL, y = NULL, xy_df = NULL)
st_force_cut_nb(nb, x = NULL, y = NULL, xy_df = NULL)
nb |
a neighbourhood '"list"' or '"matrix"', or an 'sf' dataframe with a neighbourhood column called '"nb"'. |
x |
name or number of first area (optional if 'xy_df' is provided). |
y |
name or number of second area (optional if 'xy_df' is provided). |
xy_df |
(optional) a dataframe with two columns: 'x' and 'y', containing the names or numbers of areas to cut. |
An amended neighbourhood '"list"', '"matrix"', or 'sf' dataframe with a neighbourhood column called '"nb"'.
# For individual x and y st_bridges(uk_election,"constituency_name") |> st_force_cut_nb(x = "Llanelli", y = "Swansea West") # For multiple x and y pairs st_bridges(uk_election,"constituency_name") |> st_force_cut_nb(xy_df = data.frame( x = c("Llanelli", "Swansea West"), y = c("Bridgend", "Vale Of Glamorgan")))
# For individual x and y st_bridges(uk_election,"constituency_name") |> st_force_cut_nb(x = "Llanelli", y = "Swansea West") # For multiple x and y pairs st_bridges(uk_election,"constituency_name") |> st_force_cut_nb(xy_df = data.frame( x = c("Llanelli", "Swansea West"), y = c("Bridgend", "Vale Of Glamorgan")))
Enforce contiguity between pairs of areas
st_force_join_nb(nb, x = NULL, y = NULL, xy_df = NULL)
st_force_join_nb(nb, x = NULL, y = NULL, xy_df = NULL)
nb |
a neighbourhood '"list"' or '"matrix"', or an 'sf' dataframe with a neighbourhood column called '"nb"'. |
x |
name or number of first area (optional if 'xy_df' is provided). |
y |
name or number of second area (optional if 'xy_df' is provided). |
xy_df |
(optional) a dataframe with two columns: 'x' and 'y', containing the names or numbers of areas to join. |
An amended neighbourhood '"list"', '"matrix"', or 'sf' dataframe with a neighbourhood column called '"nb"'.
# For individual x and y st_bridges(uk_election,"constituency_name") |> st_force_join_nb(x = "Gower", y = "Bridgend") # For multiple x and y pairse") |> st_bridges(uk_election,"constituency_name") |> st_force_join_nb(xy_df = data.frame( x = c("Gower", "Llanelli"), y = c("Bridgend", "Vale Of Glamorgan")))
# For individual x and y st_bridges(uk_election,"constituency_name") |> st_force_join_nb(x = "Gower", y = "Bridgend") # For multiple x and y pairse") |> st_bridges(uk_election,"constituency_name") |> st_force_join_nb(xy_df = data.frame( x = c("Gower", "Llanelli"), y = c("Bridgend", "Vale Of Glamorgan")))
Manual remove contiguity between two areas
st_manual_cut_nb(nb, x, y)
st_manual_cut_nb(nb, x, y)
nb |
a neighbourhood '"list"' or '"matrix"', or an 'sf' dataframe with a neighbourhood column called '"nb"'. |
x |
name or number of first area. |
y |
name or number of second area. |
An amended neighbourhood '"list"', '"matrix"', or 'sf' dataframe with a neighbourhood column called '"nb"'.
st_bridges(uk_election,"constituency_name") |> st_manual_cut_nb("Ynys Mon","Arfon") |> st_manual_cut_nb(292,378)
st_bridges(uk_election,"constituency_name") |> st_manual_cut_nb("Ynys Mon","Arfon") |> st_manual_cut_nb(292,378)
Manually enforce contiguity between two areas
st_manual_join_nb(nb, x, y)
st_manual_join_nb(nb, x, y)
nb |
a neighbourhood '"list"' or '"matrix"', or an 'sf' dataframe with a neighbourhood column called '"nb"'. |
x |
name or number of first area. |
y |
name or number of second area. |
An amended neighbourhood '"list"', '"matrix"', or 'sf' dataframe with a neighbourhood column called '"nb"'.
st_bridges(uk_election,"constituency_name") |> st_manual_join_nb("Gower","St Ives")
st_bridges(uk_election,"constituency_name") |> st_manual_join_nb("Gower","St Ives")
Visualise a neighbourhood structure on a map
st_quickmap_nb( nbsf, linkcol = "dodgerblue", bordercol = "gray7", pointcol = "darkred", fillcol = "gray95", linksize = 0.2, bordersize = 0.1, pointsize = 0.8, title = NULL, subtitle = NULL, nodes = "point", numericsize = 5, numericcol = "black", concavehull = FALSE, hullratio = 0.8, hullcol = "darkgreen", hullsize = 0.5 )
st_quickmap_nb( nbsf, linkcol = "dodgerblue", bordercol = "gray7", pointcol = "darkred", fillcol = "gray95", linksize = 0.2, bordersize = 0.1, pointsize = 0.8, title = NULL, subtitle = NULL, nodes = "point", numericsize = 5, numericcol = "black", concavehull = FALSE, hullratio = 0.8, hullcol = "darkgreen", hullsize = 0.5 )
nbsf |
an 'sf' dataframe with a neighbourhood column called "nb", such as the output of 'st_bridges()' |
linkcol |
colour of lines connecting neighbours. |
bordercol |
colour of boundary lines between areas. |
pointcol |
colour of centroid points if nodes are '"point"'. |
fillcol |
fill of areas. |
linksize |
linewidth of lines connecting neighbours. |
bordersize |
linewidth of borders between areas. |
pointsize |
size of centroid points if nodes are '"point"'. |
title |
plot title. |
subtitle |
plot subtitle. |
nodes |
default '"point"'. Can also be '"numeric"'. |
numericsize |
font size if nodes are '"numeric"'. |
numericcol |
font colour if nodes are '"numeric"'. |
concavehull |
default 'FALSE'. Whether or not to show concave hulls. |
hullratio |
value between 0 and 1. 1 returns the convex hulls, 0 maximally concave hulls. |
hullcol |
colour of concave hull lines. |
hullsize |
line width of concave hull lines. |
A 'ggplot' showing areas and neighbourhood structure.
st_bridges(uk_election,"constituency_name") |> st_quickmap_nb()
st_bridges(uk_election,"constituency_name") |> st_quickmap_nb()
Visualise the predictions generated by the 'st_augment()' function
st_quickmap_preds( output, scale_low = "firebrick4", scale_mid = "white", scale_high = "darkblue", scale_midpoint = 0, borderwidth = 0.05, bordercol = "black", legendlimits = "individual", titlesize = 12, subtitlesize = 10, framefill = "white", frameline = "black", framesize = 1 )
st_quickmap_preds( output, scale_low = "firebrick4", scale_mid = "white", scale_high = "darkblue", scale_midpoint = 0, borderwidth = 0.05, bordercol = "black", legendlimits = "individual", titlesize = 12, subtitlesize = 10, framefill = "white", frameline = "black", framesize = 1 )
output |
an augmented 'sf' dataframe produced by 'st_augment()'. |
scale_low |
fill of lowest extreme of scale. |
scale_mid |
fill of midpoint of scale. |
scale_high |
fill of highest extreme of scale. |
scale_midpoint |
value of midpoint of scale. |
borderwidth |
linewidth of borders between units. |
bordercol |
colour of borders between units. |
legendlimits |
default '"individual"'. legend of each plot scaled within its own limits. '"minmax"' means all plot have common legend limits according to the global min-max. |
titlesize |
font size for title. |
subtitlesize |
font size for subtitle. |
framefill |
colour for background fill. |
frameline |
colour for frame. |
framesize |
line width of frame. |
A list of ggplots.
prepdata <- st_bridges(uk_election,"constituency_name") mgcv::gam(health_not_good ~ s(constituency_name, bs='mrf', xt=list(nb=prepdata$nb), k=100), data=prepdata, method="REML") |> st_augment(uk_election) |> st_quickmap_preds()
prepdata <- st_bridges(uk_election,"constituency_name") mgcv::gam(health_not_good ~ s(constituency_name, bs='mrf', xt=list(nb=prepdata$nb), k=100), data=prepdata, method="REML") |> st_augment(uk_election) |> st_quickmap_preds()
Swing and socio-economic data for England, Scotland & Wales Census and voting data sourced from parlitools R package Spatial data sourced from UK government geoportal
uk_election
uk_election
## 'uk_election' An sf and data.frame object with 632 rows and 9 columns
Percentage of constituency population with level 4 qualifications or higher, scaled to mean 0 and standard deviation 1
Percentage of constituency of population reporting health to be fair, bad, or very bad, scaled to mean 0 and standard deviation 1
Percentage of constituency of population of exclusively white ethnicity, scaled to mean 0 and standard deviation 1
Butler swing to the Conservative Party from the Labour Party from election 2019 to election 2019
Constituency population
Regions
Counties
Westminster parliamentary constituencies, as of 2019
sfc polygons column
...
<https://geoportal.statistics.gov.uk/datasets/ons::wpc-dec-2019-ultra-generalised-clipped-boundaries-uk>, <https://docs.evanodell.com/parlitools/>