Session Ready
Exercise

Spatial autocorrelation test

The map of "Remain" votes seems to have spatial correlation. Pick any two boroughs that are neighbors - with a shared border - and the chances are they'll be more similar than any two random boroughs. This can be a problem when using statistical models that assume, conditional on the model, that the data points are independent.

The spdep package has functions for measures of spatial correlation, also known as spatial dependency. Computing these measures first requires you to work out which regions are neighbors via the poly2nb() function, short for "polygons to neighbors". The result is an object of class nb. Then you can compute the test statistic and run a significance test on the null hypothesis of no spatial correlation. The significance test can either be done by Monte-Carlo or theoretical models.

In this example you'll use the Moran "I" statistic to test the spatial correlation of the population and the percentage "Remain" vote.

Instructions
100 XP

The london_ref spatial data object is loaded for you.

  • Load the spdep package.

  • Pass london_ref to poly2nb() to find the neighbors of each borough polygon. Assign to borough_nb.

  • Get the center points of each borough by passing london_ref to coordinates(). Assign to borough_centers.

  • Update the basic map of the London boroughs by adding the connections.

    • In the second plot call pass borough_nb and borough_centers.
    • Also pass add = TRUE to add to the existing plot rather than starting a new one.
  • Inspect an [spplot()]() map of TOTAL_POP.

  • Feed the TOTAL_POP vector into moran.test().

    • moran.test() needs a weighted version of the nb object which you get by calling nb2listw(). This runs the test against the theoretical distribution of Moran's I statistic. Find the p-value. Can you reject the null hypothesis of no spatial correlation?
  • Inspect an [spplot()]() map of Pct_Remain.

  • Run another Moran I statistic test, this time on the percent of Remain voters.

    • Use 999 Monte-carlo iterations via moran.mc().
    • The first two arguments are the same as for moran.test().
    • You also need to pass the argument nsim = 999.
    • Note the p-value. Can you reject the null hypothesis this time?