-
Notifications
You must be signed in to change notification settings - Fork 31
/
main.Rnw
298 lines (224 loc) · 8.57 KB
/
main.Rnw
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
\documentclass{article}
\usepackage{hyperref}
\usepackage[T1]{fontenc}
\usepackage{geometry} % to change the page dimensions
\geometry{letterpaper} % or letterpaper (US) or a5paper or....
\geometry{margin=0.75in} % for example, change the margins to 2 inches all round
<<setup, include=FALSE>>=
library(knitr)
options(width=50)
# an old solution by @r2d3 was at https://github.com/yihui/knitr/issues/210
# but since knitr 0.9, we can do it through the chunk option tidy.opts
@
\begin{document}
\title{Introduction to spatial data analysis with R}
\author{Edgar Dobriban}
\maketitle
This script presents an introduction to spatial data analysis with R. It is based on Chapters 2 and 9 of our book: \textbf{Applied Spatial Data Analysis with R}, Roger S. Bivand, Edzer Pebesma and V. Gomez-Rubio
UseR! Series, Springer, 2nd ed. 2013.
Please consult the textbook for more extensive reference, it is a great resource.
Running this script requires that the code and data bundles from the book be unzipped in the same folder as the script. For instance the zip bundle for chapter 2 is available at:
\url{http://www.asdar-book.org/data2ed.php?chapter=2}
Similarly, get the zip bundle for chapter 9.
In the first part of the lecture, we will work with existing spatial data in R. Second we will also understand how to create such data.
Okay, let's get started: clear up the workspace and change directory to where the data is!
<<>>=
#set up: clear all & set wd
rm(list=ls())
setwd("C:/Dropbox/253/spatial data analysis with R")
@
Let's load two required libraries:
rgdal: processes GDAL - Geospatial Data Analysis files
spdep: Spatial dependence: weighting schemes, statistics and models.
<<>>=
library(rgdal)
library(spdep)
@
The data set consists of Leukemia incidence in 281 census tracts in 8 central NY state counties (and was collected in the 1980's)
This is how we read the data sets:
<<>>=
NY8 <- readOGR(".", "NY8_utm18")
# read Shapefile (spdep package),
# arguments (1)"." - source: a directory; here current wd
# (2) layer: shapefile name
# Note: find shapefiles by googling "[location] shapefile"
@
Some additional data:
<<>>=
#city names
cities <- readOGR(".", "NY8cities")
#locations of 11 inactive hazardous waste sites;
# TCE: Trichloroethylene
TCE <- readOGR(".", "TCE")
@
Let's look at how the data is stored.
<<>>=
# How is the data stored?
class(NY8)
getClass("SpatialPolygonsDataFrame")
#it's a SpatialPolygonsDataFrame - a data.frame on polygons
#spatial polygons contain polygons and Spatial* characteristics
getClass("SpatialPolygons")
@
<<>>=
# a polygon is a sequence of closed lines;
# point coordinates where the first point equals the last
getClass("Polygon")
#labpt - label point, centroid of polygon
# a line is an ordered list of coordinates
getClass("Line")
@
<<>>=
#finally... Spatial is the mother class of all Spatial* classes
# used in in the sp package
getClass("Spatial")
@
What does the data contain?
<<>>=
summary(NY8)
#Note the special slots of this class
#Coordinates
#proj4string
#Spatial data.frame - with coordinates X,Y
@
What about the 'cities' data?
<<>>=
summary(cities)
@
Let's make some plots:
<<>>=
#plot cities & TCE locations
plot(NY8, border="grey60", axes=TRUE)
text(coordinates(cities), labels=as.character(cities$names), font=2, cex=1.5)
plot(NY8, border="grey60", axes=TRUE)
points(TCE, pch=2, cex=1.5)
text(coordinates(TCE), labels=as.character(TCE$name), cex=1.5,
font=1, pos=c(4,1,4,1,4,4,4,2,3,4,2), offset=0.3)
@
Let's plot one of the features - percent age > 65.
<<>>=
#plot one of the features - percent age > 65
spplot(NY8, c("PCTAGE65P"))#, col="transparent"
spplot(NY8, c("PCTAGE65P"), col="transparent")
@
Let's make a different plot, with a new color palette:
<<>>=
#different plot: new color palette
#load package
library("RColorBrewer")
#color palette creator function
rds <- colorRampPalette(brewer.pal(8, "RdBu"))
#get a range for the values
tr_at <- seq(min(NY8$PCTAGE65P), max(NY8$PCTAGE65P), length.out=20)
#create a color interpolating function taking the required
#number of shades as argument
tr_rds <- rds(20)
#parameters
# at - at which values colors change
# col.regions - specify fill colors
tr_pl <- spplot(NY8, c("PCTAGE65P"), at=tr_at, col="transparent",
col.regions=tr_rds, main=list(label="Age>65", cex=0.8))
plot(tr_pl)
@
Finally, let's read the last piece of data - a list of neighbors that specifies the lattice structure.
<<>>=
# reads a GAL lattice file into a neighbors list
NY_nb <- read.gal("NY_nb.gal", region.id=row.names(NY8))
summary(NY_nb) #which states are neighbors?
@
<<>>=
plot(NY8, border="grey60", axes=TRUE)
plot(NY_nb, coordinates(NY8), pch=19, cex=0.6, add=TRUE)
@
We finally get to the data analysis part!
Let's first fit a simple linear model, where we regress the leukemia incidence on covariates of interest.
The leukemia incidence is transformed the following way:
$$ z = log(1000(Y_i+1)/n_i) $$
The covariates are:
\begin{itemize}
\item exposure to TCE- log inverse distance from nearest site
\item percent aged >65
\item percent owning home
\end{itemize}
<<>>=
nylm <- lm(Z~PEXPOSURE+PCTAGE65P+PCTOWNHOME, data=NY8)
summary(nylm)
@
Let's plot the fit and residual.
<<>>=
NY8$lm_fit <- nylm$fit
NY8$lm_residual <- nylm$residuals
rds <- colorRampPalette(brewer.pal(8, "RdBu"))
fit_pl <- spplot(NY8, c("lm_fit"), col="transparent", cex=0.8)
res_pl <- spplot(NY8, c("lm_residual"), col="transparent", cex=0.8)
plot(fit_pl, split=c(1,1,2,1), more=TRUE)
plot(res_pl, split=c(2,1,2,1), more=FALSE)
@
Let's move to a more sophisticated autoregressive model:
$$
(I - \lambda W)(Y- X\beta) = \varepsilon
$$
In order to specify such a model, we need to create the adjacency matrix $W$. We generate this as a 'spatial weight object' from the neighbor list object we loaded.
<<>>=
# generate weight object from neighbor list object
# "B" - generates binary weights
NYlistw<-nb2listw(NY_nb, style = "B")
# fit model (I - lambda* W)(Y- X* beta) = epsilon
nysar<-spautolm(Z~PEXPOSURE+PCTAGE65P+PCTOWNHOME, data=NY8, listw=NYlistw)
summary(nysar)
@
Note: lambda significantly different from 0 indicates that there is significant reduction in RSS by modelling the spatial correlations.
For more understanding, let's plot the trend and stochastic component, which are the first and second element on the RHS of the following equation:
$$
Y = X* \beta + \lambda W)(Y-X\beta) + \varepsilon
$$
<<>>=
#plot trend and stochastic component
NY8$sar_trend <- nysar$fit$signal_trend
NY8$sar_stochastic <- nysar$fit$signal_stochastic
rds <- colorRampPalette(brewer.pal(8, "RdBu"))
tr_at <- seq(-1, 1.3, length.out=21)
tr_rds <- rds(sum(tr_at >= 0)*2)[-(1:(sum(tr_at >= 0)-sum(tr_at < 0)))]
tr_pl <- spplot(NY8, c("sar_trend"), at=tr_at, col="transparent",
col.regions=tr_rds, main=list(label="Trend", cex=0.8))
st_at <- seq(-0.16, 0.39, length.out=21)
st_rds <- rds(sum(st_at >= 0)*2)[-(1:(sum(st_at >= 0)-sum(st_at < 0)))]
st_pl <- spplot(NY8, c("sar_stochastic"), at=st_at, col="transparent",
col.regions=st_rds, main=list(label="Stochastic", cex=0.8))
plot(tr_pl, split=c(1,1,2,1), more=TRUE)
plot(st_pl, split=c(2,1,2,1), more=FALSE)
@
Ok this is great, but how do I transform my data into spatial format? Let's examine how to create spatial data.
There are two important structures, \verb+"SpatialPointsDataFrame"+ and \verb+listw+.
<<>>=
#Key data type #1: "SpatialPointsDataFrame"
getClass("SpatialPointsDataFrame")
#read data matrix
CRAN_df <- read.table("CRAN051001a.txt", header=TRUE)
CRAN_mat <- cbind(CRAN_df$long, CRAN_df$lat)
row.names(CRAN_mat) <- 1:nrow(CRAN_mat)
str(CRAN_mat)
#set CRS
llCRS <- CRS("+proj=longlat +ellps=WGS84")
CRAN_sp <- SpatialPoints(CRAN_mat, proj4string=llCRS)
summary(CRAN_sp)
#if you don't need CRS
llCRS <- CRS(as.character(NA))
CRAN_spdf <- SpatialPointsDataFrame(CRAN_sp, CRAN_df)
summary(CRAN_spdf)
@
The second key data type is the class of Spatial Weights - listw.
It is based on the Spatial neighbors \verb+("nb")+ class. This class is a list of length $n$, where $n$ is the number of our nodes,
with the index numbers of neighbors of each component stored as an integer vector. Run the following: \verb+vignette("nb", package = "spdep")+ to read more on neighbors.
Recall that we already have \verb+NY_nb+ as an object of nb class.
On top of it we can add nonzero weights for each pair of neighbors, getting the listw class.
<<>>=
NY_w <-nb2listw(NY_nb)
summary(NY_w)
@
By default each node is normalized to have total sum of out-weights equal to 1. We can also choose to use binary weights.
<<>>=
NY_w <-nb2listw(NY_nb, style = "B")
summary(NY_w)
@
\end{document}