-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathSpatialAnalysisCode.Rmd
More file actions
93 lines (78 loc) · 1.92 KB
/
Copy pathSpatialAnalysisCode.Rmd
File metadata and controls
93 lines (78 loc) · 1.92 KB
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
---
title: "Spatial Taxi Code"
author: "Emilie Bruzelius"
date: "April 22, 2015"
output: html_document
---
```{r}
rm(list=ls())
```
Read and plot block shapefile
```{r}
library(maptools)
library(ggplot2)
block <- readShapePoly('/Users/emiliebruzelius/Desktop/git/taxi/taxi/nycb2010_15a/nycb2010.shp')
ggplot() + geom_polygon(data=block,
aes(x=long, y=lat, group=group))
```
Create neighbors
```{r}
library(sp)
library(spdep)
length(block)
block.nb <- poly2nb(block)
head(block.nb)
plot(block.nb, coordinates(block),
col = "red", pch = ".")
plot(block, add = T)
coords <- coordinates(block)
IDs <- row.names(coords)
block.nb3 <- graph2nb(gabrielneigh(coords),
row.names = IDs)
plot(block.nb3, coords, pch = ".", col = "red")
plot(block.nb3, add = T)
```
Create block weights
```{r}
block.wts <- nb2listw(block.nb3,
zero.policy = T, style = "B")
print(block.wts, zero.policy = T)
names(block.wts)
```
Moran's Scatterplot
```{r}
set.seed(987654)
n <- length(block.nb3)
uncorr.x <- rnorm(n)
rho <- 0.05
autocorr.x <- invIrW(block.wts, rho,
feasible = TRUE) %*% uncorr.x
plot(autocorr.x, lag(nycZIPS.wts, autocorr.x),
xlab = "autocorrelated random variable",
ylab = "spatial lag",
main = "Autocorrelated random variable",
cex.main = 0.8, cex.lab = 0.8)
```
moran.plot(outcome.variable, listw = nb2listw(neighbor.list))
Moran's I
```{r}
moran.x <- moran.test(autocorr.x,
listw = block.wts, zero.policy = T)
moran.x
summary(moran.x)
moran.x$p.value
```
Code for geoid
```{r}
https://github.com/nygeog/nyc_gis_tools/blob/master/calc_geoid_from_boro.py\
```
Merge
```{r}
library(sp)
block.df <- merge(blocks, taxi, all.x = T, all.y = F, by.x = "geoid", by.y = "geoid")
```
```{r}
###moran.cases <- moran.test(nycZIPS2$cases,
#listw = block.wts, zero.policy = TRUE)
#moran.cases
```