knitr::opts_chunk$set( collapse = TRUE, comment = "#>" )
library(socialFrontiers) library(dplyr)
The package contains an example dataset of London split into spatial polygons. Each polygon represents a census unit called lower super output area. The london data contains the number of UK-born and non-UK-born residents in a LSOA. For the purposes of this vignette, I select all areas in the borough of Barnet since London is so large.
The data is in the form of a sf object but the frontier detection function also accepts sp objects.
data(london) ## Filter to the borough of Barnet barnet <- london %>% filter(substr(LSOAname, 1, 6) %in% 'Barnet')
Currently the only method for finding frontiers is the localised binomial model used in Dean et al. The model needs total counts of an event occuring (e.g. number of non-UK-born residents) and the total number of trials (e.g. total number of residents). The name of the variables denoting the column containing these counts must be entered as a string.
y <- 'nonUK' # 'nonUK' # Number of foreign n.trials <- 'totalPop' #total population (per zone?)
Now we run the frontier_detect routine. You can see the underlying code used for the statistical model in using socialFrontiers:::binomial_localisedINLA.
frontier_model <- frontier_detect( data = barnet, y = y, n.trials = n.trials) class(frontier_model) # Outputs a frontier_model object
The output saves as a 'frontier_model' object which can be used with other methods such as summary.
summary(frontier_model) ## This calls up summary.frontier_model
We can extract the frontier (as well as non-frontier) borders as a sf object for further graphing or gis methods using frontier_as_sf. Note that this function throws up ignorable warnings that come from using sf::st_intersects.
suppressWarnings(borders_sf <- frontier_as_sf(frontier_model, silent = T)) class(borders_sf)
Example graph using tmap showing all frontiers.
library(tmap) ## Create a variable for prop-non-UK barnet <- barnet %>% mutate(propNonUK = nonUK/totalPop) tm_shape(barnet) + tm_fill(col= 'propNonUK') + qtm(borders_sf) # works
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.