-
Notifications
You must be signed in to change notification settings - Fork 1
/
README.Rmd
executable file
·144 lines (116 loc) · 4.88 KB
/
README.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
---
output: github_document
---
<!-- README.md is generated from README.Rmd. Please edit that file -->
```{r, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "man/figures/README-",
out.width = "100%"
)
```
# sfhotspot <img src='man/figures/logo.png' align="right" height="139">
<!-- badges: start -->
[![CRAN status](https://www.r-pkg.org/badges/version/sfhotspot)](https://CRAN.R-project.org/package=sfhotspot)
[![R-CMD-check](https://github.com/mpjashby/sfhotspot/workflows/R-CMD-check/badge.svg)](https://github.com/mpjashby/sfhotspot/actions)
[![codecov](https://codecov.io/gh/mpjashby/sfhotspot/branch/main/graph/badge.svg?token=KPS526F0V1)](https://app.codecov.io/gh/mpjashby/sfhotspot)
[![Lifecycle: stable](https://img.shields.io/badge/lifecycle-stable-brightgreen.svg)](https://lifecycle.r-lib.org/articles/stages.html#stable)
<!-- badges: end -->
sfhotspot provides functions to identify and understand clusters of points
(typically representing the locations of places or events). All the functions in
the package work on and produce [simple
features](https://r-spatial.github.io/sf/) (SF) objects, which means they can be
used as part of modern spatial analysis in R.
## Installation
You can install the development version of sfhotspot from
[GitHub](https://github.com/) with:
``` r
# install.packages("remotes")
remotes::install_github("mpjashby/sfhotspot")
```
## Functions
sfhotspot has the following functions. All can be used by just supplying an SF
object containing points, or can be configured using the optional arguments to
each function.
```{r functions-table, echo=FALSE}
knitr::kable(data.frame(
"name" = c(
"`hotspot_count()`",
"`hotspot_change()`",
"`hotspot_kde()`",
"`hotspot_dual_kde()`",
"`hotspot_gistar()`",
"`hotspot_classify()`"
),
"use" = c(
paste(
"Count the number of points in each cell of a regular grid. Cell size",
"can be set by the user or chosen automatically."
),
paste(
"Measure the change in the count of points in each cell between two",
"periods of time."
),
paste(
"Estimate kernel density for each cell in a regular grid. Cell size and",
"bandwidth can be set by the user or chosen automatically."
),
paste(
"Compare the kernel density of two layers of points, e.g. to estimate",
"the local risk of an event occurring relative to local population."
),
paste(
"Calculate the Getis–Ord $G_i^*$ statistic for each cell in a regular",
"grid, while optionally estimating kernel density. Cell size, bandwidth",
"and neighbour distance can be set by the user or chosen automatically."
),
paste(
"Classify grid cells according to whether they have had significant",
"clusters of points at different time periods. All parameters can be",
"chosen automatically or be set by the user using the",
"`hotspot_classify_params()` helper function."
)
)
))
```
The results produced by `hotspot_count()`, `hotspot_change()`, `hotspot_kde()`,
`hotspot_dual_kde()` and `hotspot_classify()` can be easily plotted using
included methods for`autoplot()` and `autolayer()`.
There are also included datasets:
* `memphis_robberies`, containing records of
`r format(nrow(sfhotspot::memphis_robberies), big.mark = ",")` robberies in
Memphis, TN, in 2019.
* `memphis_robberies_jan`, containing the same data but only for the
`r format(nrow(sfhotspot::memphis_robberies_jan), big.mark = ",")` robberies
recorded in January 2019.
* `memphis_population`, containing population counts for the centroids of
`r format(nrow(sfhotspot::memphis_population), big.mark = ",")` census
blocks in Memphis, TN, in 2020.
## Example
We can use the `hotspot_gistar()` function to identify cells in a regular grid
in which there are more/fewer points than would be expected if the points were
distributed randomly. In this example, the points represent the locations of
personal robberies in Memphis, which is a dataset included with the package.
```{r example}
# Load packages
library(sf)
library(sfhotspot)
library(tidyverse)
# Transform data to UTM zone 15N so that we can think in metres, not decimal
# degrees
memphis_robberies_utm <- st_transform(memphis_robberies, 32615)
# Identify hotspots, set all the parameters automatically by not specifying cell
# size, bandwidth, etc.
memphis_robberies_hotspots <- hotspot_gistar(memphis_robberies_utm)
# Visualise the hotspots by showing only those cells that have significantly
# more points than expected by chance. For those cells, show the estimated
# density of robberies.
memphis_robberies_hotspots %>%
filter(gistar > 0, pvalue < 0.05) %>%
ggplot(aes(colour = kde, fill = kde)) +
geom_sf() +
scale_colour_distiller(aesthetics = c("colour", "fill"), direction = 1) +
labs(title = "Density of robberies in Memphis, 2019") +
theme_void()
```