Session Ready
Exercise

Bandwidth selection

We can get a more principled measure of the violent crime ratio using a spatial segregation model. The spatialkernel package implements the theory of spatial segregation.

The first step is to compute the optimal bandwidth for kernel smoothing under the segregation model. A small bandwidth would result in a density that is mostly zero, with spikes at the event locations. A large bandwidth would flatten out any structure in the events, resulting in a large "blob" across the whole window. Somewhere between these extremes is a bandwidth that best represents an underlying density for the process.

spseg() will scan over a range of bandwidths and compute a test statistic using a cross-validation method. The bandwidth that maximizes this test statistic is the one to use. The returned value from spseg() in this case is a list, with h and cv elements giving the values of the statistic over the input h values. The spatialkernel package supplies a plotcv function to show how the test value varies. The hcv element has the value of the best bandwidth.

Instructions
100 XP

spatstat is loaded and the preston_crime object is read in.

  • Set h, the bandwidth values to try, then call spseg().
    • You need to provide the start value, 500, then end value, 1000, and the step size, 50.
    • Assign the result to bw_choice.
  • Plot the test statistic vs. the bandwidth.
    • Call plotcv() on bw_choice.
    • Highlight the best bandwidth by adding a vertical line where the test statistic is maximized.