-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.html
530 lines (312 loc) · 12.1 KB
/
index.html
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
<!DOCTYPE html>
<html>
<head>
<title>Geographic Data Science in R</title>
<meta charset="utf-8">
<meta name="author" content="Slides: katiejolly.io/rnorth-19" />
<meta name="date" content="2019-08-16" />
<link rel="stylesheet" href="xaringan-themer.css" type="text/css" />
</head>
<body>
<textarea id="source">
class: center, middle, inverse, title-slide
# Geographic Data Science in R
## Katie Jolly
### Slides: katiejolly.io/rnorth-19
### August 16, 2019
---
### Framing Geographic Data Science (GDS)
<br> <br>
As opposed to traditional Geographic Information Science (GIS), GDS is:
<br> <br>
<ul>
<li> Interdisciplinary with Geography, Computing, and Statistics <br><br>
<li> Code-based workflow <br><br>
<li> Maximally reproducible
</ul>
.footnote[Source: https://www.robinlovelace.net/2017/05/02/can-geographic-data-save-the-world/]
---
class: center, middle
## Geographic data is a large category!
---
background-image: url("https://pvsmt99345.i.lithium.com/t5/image/serverpage/image-id/50441iC2825B417745932D?v=1.0")
## Raster data 📷
Think: aerial imagery, elevation models, remote sensing
.footnote[Source: https://community.alteryx.com/t5/Data-Science-Blog/Vector-and-Raster-A-Tale-of-Two-Spatial-Data-Types/ba-p/336141]
---
background-image: url("https://pvsmt99345.i.lithium.com/t5/image/serverpage/image-id/49570i26EF3FAEACD21BD4/image-size/medium?v=1.0&px=400")
## Vector data 📍
Think: Census data, points on a map, roads
.footnote[Source: https://community.alteryx.com/t5/Data-Science-Blog/Vector-and-Raster-A-Tale-of-Two-Spatial-Data-Types/ba-p/336141]
---
class: inverse, center, middle
### Quirks (fun parts) of working with geographic data
---
### Projections and coordinate reference systems
How do you **translate** your data from a 3D shape to a 2D map...
<img src="https://media.opennews.org/cache/06/37/0637aa2541b31f526ad44f7cb2db7b6c.jpg" style="display: block; margin: auto;" />
---
[West Wing video](https://youtu.be/OH1bZ0F3zVU?t=34)
![](images/ww.PNG)
---
### Two common projections for US data
When working with something around the size of a **state** or **smaller**, it can often be a good idea to use state plane or UTM projections. <br>
<img src="images/projections.png" width="1200" style="display: block; margin: auto;" />
<br>
Many states and cities also have **custom** or **recommended** projections, so it's worth doing some research before picking something! <br>
.footnote[Projection resources: [ArcGIS help](http://desktop.arcgis.com/en/arcmap/10.3/guide-books/map-projections/what-are-map-projections.htm), [Geocomputation with R: Reprojecting data](https://geocompr.robinlovelace.net/reproj-geo-data.html)]
<br>
---
class: inverse, center, middle
## Honeybee Permits in Minneapolis 🐝
---
## I'll talk about...
* Reading in spatial data
* Reprojecting data
* Basic maps
* Spatial join
* Neighborhood definition
* Spatial clustering (Moran's I)
* Modifiable areal unit problem
---
## Reading in spatial data
### Shapefiles
The most common file format is the **shapefile**, which is actually a collection of files. It's important to keep all of these parts in one directory!
<img src="images/honeybee-files.PNG" width="993" style="display: block; margin: auto;" />
But when you read in the data, it only looks like you're using the *.shp* file.
```r
library(sf)
honeybees <- st_read("data/honeybees/Honey_Bee_Permits_2017.shp")
```
---
## Reading in spatial data
### APIs
If you're getting data from somewhere like an open data portal, using the API endpoint can often be easier.
* Easier for people trying to run your file
* Easier for your file management
```r
honeybees <- st_read("https://opendata.arcgis.com/datasets/f99ce43936d74f718e92a37a560ad875_0.geojson")
```
There are reasons for one way over another, but I prefer APIs when possible. Either way you'll get the same data.
---
## Reprojecting data
We should first check the current projection.
```r
st_crs(honeybees)
```
```
## Coordinate Reference System:
## EPSG: 4326
## proj4string: "+proj=longlat +datum=WGS84 +no_defs"
```
--
When I look at this, I notice +proj=<mark>longlat</mark>, which is a geographic, not projected, coordinate system.
--
I'll use a UTM projection.
```r
honeybees_utm <- honeybees %>%
st_transform(26915) # UTM 15N zone
st_crs(honeybees_utm)
```
```
## Coordinate Reference System:
## EPSG: 26915
## proj4string: "+proj=utm +zone=15 +ellps=GRS80 +towgs84=0,0,0,0,0,0,0 +units=m +no_defs"
```
---
## Basic maps
`sf` objects have a `plot` function.
```r
plot(honeybees_utm %>% dplyr::select(HiveType))
```
<img src="index_files/figure-html/unnamed-chunk-8-1.png" style="display: block; margin: auto;" />
---
## Basic maps
But they also work nicely with `ggplot2`
```r
ggplot(honeybees_utm) +
geom_sf()
```
<img src="index_files/figure-html/unnamed-chunk-9-1.png" style="display: block; margin: auto;" />
---
## Basic maps
We can use the ggplot layering logic to add contextual data, like Minneapolis neighborhood boundaries.
```r
neighborhoods <- st_read("https://opendata.arcgis.com/datasets/055ca54e5fcc47329f081c9ef51d038e_0.geojson") %>%
st_transform(26915)
```
<img src="index_files/figure-html/unnamed-chunk-11-1.png" style="display: block; margin: auto;" />
---
## Basic maps
<img src="index_files/figure-html/unnamed-chunk-12-1.png" style="display: block; margin: auto;" />
<br>
When I look at this map as a geographer, I look for patterns of clustering or dispersion. I'll now walk through how to quantify that.
---
## Spatial join
One way to think about clustering is to ask whether or not the permits are clustered **by neighborhood**.
<img src="https://i.stack.imgur.com/CVVSH.png" style="display: block; margin: auto;" />
---
## Spatial join
```r
permits_per_nb <- neighborhoods %>%
st_join(honeybees_utm) %>% # which neighborhood is each permit in?
group_by(BDNAME) %>% # and when we sum by neighborhood
summarise(permits = sum(!is.na(OBJECTID))) # how many permits total?
summary(permits_per_nb$permits)
```
```
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## 0.000 0.000 1.000 1.034 2.000 4.000
```
<br>
On average, there are **1.034** honeybee permits per neighborhood.
<br>
But is this equally likely everywhere in the city? Or are permits more likely to be in certain areas?
---
## Spatial join
<img src="index_files/figure-html/unnamed-chunk-15-1.png" style="display: block; margin: auto;" />
---
## Neighbor definition
<img src="http://geohealthinnovations.org/wp-content/uploads/2013/01/toblerquote.png" style="display: block; margin: auto;" />
---
## Neighbor definition
One common and straightforward way to define neighbors is **queen contiguity**.
<img src="https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcRxG5jeEH-7MmKzlzOcooJMOeMAMzZKqrgePBFpNP43w9W8ACq35g" style="display: block; margin: auto;" />
```r
library(spdep)
# need to convert to SpatialPolygons object
honeybees_sp <- as(permits_per_nb, "Spatial")
# define neighbor structure
nb_queen <- poly2nb(honeybees_sp, queen = TRUE)
# create a matrix of binary spatial weights
# (connected or not connected)
weights <- nb2listw(nb_queen, style = "B")
```
---
## Neighbor definiton
<img src="index_files/figure-html/unnamed-chunk-19-1.png" style="display: block; margin: auto;" />
On average, each of the neighborhoods in Minneapolis has **5.6** queen's case neighbors.
---
## Spatial clustering
<br>
<br>
<img src="https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcTlm-7-YfREIPTXXfloomGr0jdZk1GhPimm7WH8lZGWwVQIHzDA" width="500px" style="display: block; margin: auto;" />
---
## Spatial clustering
### Moran's I
> The Moran’s I statistic is the correlation coefficient for the relationship between a variable [like honeybee permits] and its surrounding values (Gimond)
<img src="https://mgimond.github.io/Spatial/img/MoranI_scatter_plot.png" width="400px" style="display: block; margin: auto;" />
---
## Spatial clustering
### Moran's I
```r
set.seed(123)
# 10000 simulations
moran.mc(honeybees_sp$permits, weights, nsim=9999)
```
```
##
## Monte-Carlo simulation of Moran I
##
## data: honeybees_sp$permits
## weights: weights
## number of simulations + 1: 10000
##
## statistic = 0.13366, observed rank = 9869, p-value = 0.0131
## alternative hypothesis: greater
```
---
## Spatial clustering
### Moran's I
statistic = <mark>0.13366</mark>, observed rank = 9869, p-value = <mark>0.0131</mark>
Based on this, we reject our null hypothesis and say that the permits are **slightly clustered** across neighborhoods.
<img src="index_files/figure-html/unnamed-chunk-23-1.png" width="250px" style="display: block; margin: auto;" />
Does this make sense?
---
## Spatial clustering
<img src="index_files/figure-html/unnamed-chunk-24-1.png" style="display: block; margin: auto;" />
---
## Modifiable areal unit problem
Grouping points by an areal unit may distort or exaggerate the actual data pattern!
<img src="http://4.bp.blogspot.com/-_pvMVmjCTQU/TsY9QaWcJaI/AAAAAAAAA30/TGhvGkcnMPY/s1600/Penn+State+Map.gif" width="200px" style="display: block; margin: auto;" />
---
## Modifiable areal unit problem
What if we look at honeybee permits by community instead?
<img src="index_files/figure-html/unnamed-chunk-27-1.png" style="display: block; margin: auto;" />
Does this look more or less clustered?
---
## Modifiable areal unit problem
According to Moran's I, it's spatially random. But we found that it was clustered by neighborhood?
<br>
<br>
```
##
## Monte-Carlo simulation of Moran I
##
## data: bg_sp$permits
## weights: weights
## number of simulations + 1: 10000
##
## statistic = -0.10302, observed rank = 5470, p-value = 0.453
## alternative hypothesis: greater
```
<br>
<br>
What could be some consequences of this?
---
## Modifiable areal unit problem
Gerrymandering, one of my research areas, is largely an application of the modifiable areal unit problem. How we carve up our space matters!
<img src="https://pbs.twimg.com/media/B-8ljgjU0AASq8g.jpg" width="500x" style="display: block; margin: auto;" />
---
class: center, middle
### Thank you for listening and happy to answer any questions!
@katiejolly6
katiejolly6@gmail.com
katiejolly.io
</textarea>
<script src="https://remarkjs.com/downloads/remark-latest.min.js"></script>
<script>var slideshow = remark.create({
"highlightStyle": "github",
"highlightLines": true,
"countIncrementalSlides": false
});
if (window.HTMLWidgets) slideshow.on('afterShowSlide', function (slide) {
window.dispatchEvent(new Event('resize'));
});
(function() {
var d = document, s = d.createElement("style"), r = d.querySelector(".remark-slide-scaler");
if (!r) return;
s.type = "text/css"; s.innerHTML = "@page {size: " + r.style.width + " " + r.style.height +"; }";
d.head.appendChild(s);
})();</script>
<script>
(function() {
var i, text, code, codes = document.getElementsByTagName('code');
for (i = 0; i < codes.length;) {
code = codes[i];
if (code.parentNode.tagName !== 'PRE' && code.childElementCount === 0) {
text = code.textContent;
if (/^\\\((.|\s)+\\\)$/.test(text) || /^\\\[(.|\s)+\\\]$/.test(text) ||
/^\$\$(.|\s)+\$\$$/.test(text) ||
/^\\begin\{([^}]+)\}(.|\s)+\\end\{[^}]+\}$/.test(text)) {
code.outerHTML = code.innerHTML; // remove <code></code>
continue;
}
}
i++;
}
})();
</script>
<!-- dynamically load mathjax for compatibility with self-contained -->
<script>
(function () {
var script = document.createElement('script');
script.type = 'text/javascript';
script.src = 'https://cdn.bootcss.com/mathjax/2.7.1/MathJax.js?config=TeX-MML-AM_CHTML';
if (location.protocol !== 'file:' && /^https?:/.test(script.src))
script.src = script.src.replace(/^https?:/, '');
document.getElementsByTagName('head')[0].appendChild(script);
})();
</script>
</body>
</html>