-
Notifications
You must be signed in to change notification settings - Fork 0
/
Point_pattern.Rmd
executable file
·385 lines (294 loc) · 12.1 KB
/
Point_pattern.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
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
---
title: Point pattern
author:
- ESR 512 - GIS in Geostatistics
- Postgraduate Institute of Science
- University of Peradeniya
- Thusitha Wagalawatta & Daniel Knitter
date: 10/2015
email: [email protected]
bibliography: Course_Statistics_SriLanka.bib
csl: harvard1.csl
output:
html_document:
toc: true
toc_depth: 3
theme: united
pandoc_args: [
"+RTS", "-K64m",
"-RTS"
]
pdf_document:
toc: true
toc_depth: 3
number_sections: true
pandoc_args: [
"+RTS", "-K64m",
"-RTS"
]
highlight: pygments
---
# Point pattern analyses #
```{r setup, eval=TRUE, echo=FALSE}
setwd("/media/daniel/homebay/Teachings/WS_2015-2016/Statistics_SriLanka/")
library(raster)
```
_THE_ references for this subject are:
- [@bivand2008]
- [@baddeley2008]
- [@diggle2013]
And the best thing is: the authors are active developers of `R`. The package we will use in this topic very often is `spatstat`. A package that powerful that the manual alone has over 1400 pages (as of 2015-09-26).
If you use the package in your research, do not forget to cite it (this holds true for every package used, including the base software `R`):
```{r}
library(spatstat)
citation(package = "spatstat")
```
[following text largely from Knitter & Nakoinz (submitted)]
Point patterns are the result of processes that are influenced by (a) first-order effects, i.e. the location of the point is influenced by the underlying structure of the area but not by the location of other points [@wiegand2004, 210]; (b) second-order effects that occur when the location of a point is influenced by the presence or absence of other points [@wiegand2004, 210]. Point pattern analyses are common in ecological studies [e.g. @legendre2012; @wiegand2013]; up to now there are only few applications in archaeological contexts [e.g. @knitter2014].
## Warm up - some exploratory calculations ##
### Data preparation ###
Load shapefiles and create point dataset of the city polygons.
```{r data, eval=TRUE, echo=TRUE}
library(rgdal)
boundary <- readOGR(dsn = "./data/DSL250-Shp/", layer = "Boundary")
cities <- readOGR(dsn = "./data/DSL250-Shp", layer = "Cities")
library(rgeos)
cities.p <- gCentroid(cities, byid = TRUE)
par(mfrow = c(1,2))
plot(boundary)
plot(cities, add=TRUE)
plot(boundary)
plot(cities.p, add=TRUE)
```
### Mean center and standard distance ###
```{r mc-sd}
## prepare the data
x.co <- cities.p@coords[,1]
y.co <- cities.p@coords[,2]
n <- length(cities.p@coords[,1])
## calculate mean center
mc <- SpatialPoints(coords = cbind(sum(x.co)/n,sum(y.co)/n), proj4string = cities.p@proj4string)
mc
## calculate standard distance
sd <- sqrt(sum((x.co-mean(x.co))^2+(y.co-mean(y.co))^2)/n)
sd
## plot it
plot(boundary)
points(cities.p, pch = 19, cex = .5)
points(mc, col="red", pch = 19)
library(plotrix)
draw.circle(x = mc@coords[,1], y = mc@coords[,2], radius = sd, border = "red")
title("Mean center and standard distance \nof cities.p in Sri Lanka")
```
### Global intensity
```{r gintensity}
## get Area of Sri Lanka and change it to sqkm
area <- gArea(boundary)
area <- area/1000000
## calculate intensity
intensity <- length(cities.p)/area
intensity
```
> Exercise: what about the villages?!
>
> 1. Calculate mean centre and standard distance for the village dataset.
> 2. Plot the results
> 3. Calculate the global intensity of the village dataset. What does the value mean?
```{r ex1, eval=FALSE, echo=FALSE}
vil <- readOGR(dsn = "./data/DSL250-Shp/", layer = "Villname")
plot(vil)
mc2 <- SpatialPoints(coords = cbind(sum(vil@coords[,1])/length(vil),sum(vil@coords[,2])/length(vil)), proj4string = vil@proj4string)
mc2
sd2 <- sqrt(sum((vil@coords[,1]-mean(vil@coords[,1]))^2+(vil@coords[,2]-mean(vil@coords[,2]))^2)/length(vil))
sd2
plot(boundary)
points(vil, pch = 3, cex = .5)
points(mc, col="red", pch = 19, cex=2)
points(mc, col="blue", pch = 19)
library(plotrix)
draw.circle(x = mc@coords[,1], y = mc@coords[,2], radius = sd, border = "red")
draw.circle(x = mc2@coords[,1], y = mc2@coords[,2], radius = sd2, border = "blue")
title("Mean center and standard distance \nof cities.p (red) and villages (blue) in Sri Lanka")
area <- gArea(boundary)
area <- area/1000000
intensity2 <- length(vil)/area
intensity2
```
## Working with `ppp` objects -- Conducting Point Pattern Analysis ##
First, let us create the point pattern.
```{r create_pp}
## another window -- when "boundary" is too detailed
# library(mapdata)
# sl <- map("worldHires","Sri Lanka")
# sl <- data.frame(x=sl$x,y=sl$y)
# sl <- sl[is.na(sl$x)==FALSE & is.na(sl$y)==FALSE,]
# sl.owin <- owin(poly = sl)
# plot(sl.owin) ## quite rough, i know... but it's fast
library(spatstat)
library(maptools)
sl.owin <- as.owin.SpatialPolygons(unionSpatialPolygons(boundary, IDs = boundary@data$OUTERB_ID))
pp.cit <- ppp(x = cities.p@coords[,1], y = cities.p@coords[,2], window = sl.owin)
#str(pp.cit)
pp.vil <- ppp(x = vil@coords[,1], y = vil@coords[,2], window = sl.owin)
#plot(pp.vil, pch = 3, cex = .5)
plot(pp.cit, pch = 19, cols = "red")
```
As you can see the plot of `pp.cit` already shows the boundary of Sri Lanka. Why? And: why is this important? (hint: `?owin`)
### Quadrat count
```{r qc}
qc.cit <- quadratcount(X = pp.cit)
qc.vil <- quadratcount(X = pp.vil)
par(mfrow = c(1,2))
plot(qc.cit)
plot(qc.vil)
```
So the question is: does the quadratcount indicates CSR? To check we use a $\chi^2$ test approach (remember: Relation between observed (i.e. empirical) and expected (i.e. theoretical, here CSR) amounts of points in quadrants)
```{r qt}
qt.cit <- quadrat.test(X = pp.cit)
qt.cit
qt.vil <- quadrat.test(X = pp.vil)
qt.vil
# par(mfrow = c(1,2))
# plot(qt.cit)
# plot(qt.vil)
```
> **Exercise:**
>
> What influence does a change in the amount and density of the quadrants have? Is the current approach useful?
### Nearest-neighbor distance
```{r nn}
nn.cit <- nndist(X = pp.cit)
nn.vil <- nndist(X = pp.vil)
str(nn.cit)
mean(nn.cit)
mean(nn.vil)
hist(nn.cit)
abline(v=mean(nn.cit))
abline(v=median(nn.cit), lty=2)
hist(nn.vil)
abline(v=mean(nn.vil))
abline(v=median(nn.vil), lty=2)
```
### Nearest neighbor Distance -- Clark and Evans’ R
"An R value of less than 1 indicates of a tendency toward clustering, since it shows that observed nearest neighbor distances are shorter than expected. An R value of more than 1 indicatives of a tendency toward evenly spaced events" (O'Sullivan & Unwin 2010, 144)
```{r clarkevans}
nnE <- 1/(2*sqrt((pp.vil$n/gArea(boundary))))
nnE
R.vil <- mean(nn.vil)/nnE
R.vil
nnE <- 1/(2*sqrt((pp.cit$n/gArea(boundary))))
nnE
R.cit <- mean(nn.cit)/nnE
R.cit
```
## First order effects ##
### Density calculation - 1st approach ###
Amount of events within pixel...in imprecise language: a histogram in space. This is a step by step example but the code is rather inefficient.
```{r density1}
cs <- 50000 # cellsize
# enlarge the study area by ONE pixel (in E-W and N-S direction)
xmin <- pp.cit$window$xrange[1] - cs/2 # enlarge the study area by cs/2
xmax <- pp.cit$window$xrange[2] + cs/2 # enlarge the study area by cs/2
ymin <- pp.cit$window$yrange[1] - cs/2 # enlarge the study area by cs/2
ymax <- pp.cit$window$yrange[2] + cs/2 # enlarge the study area by cs/2
rows <- round((ymax-ymin)/cs, 0) + 1 # calculate the number of rows (add 1 just because a pixl might get lost through the rounding operation)
columns <- round((xmax-xmin)/cs, 0) + 1 # calculate the number of columns (add 1 just because a pixl might get lost through the rounding operation)
z <- cbind(1:(columns*rows)) # create a vector with all grid cells
df <- data.frame(z) # create a data.frame of it
gt <- GridTopology(c(pp.cit$window$xrange[1] - cs/2,pp.cit$window$yrange[1] -
cs/2), c(cs,cs), c(columns,rows)) # create a topological description of a grid and aftecsards...
gt # ...have a look at it and...
sgdf <- SpatialGridDataFrame(gt, df, proj4string = cities.p@proj4string) # ...create the grid.
for (i in seq(along=coordinates(gt)[,1])){ # loop for every cell
x <- coordinates(gt)[i,1] - cs/2 # because the coordinate is define for the center of the cell, the half cellsize is substracted
y <- coordinates(gt)[i,2] - cs/2 # because the coordinate is define for the center of the cell, the half cellsize is substracted
xi <- which(pp.cit$x>x & pp.cit$x<x+cs) # which events lie within the x direction?
yi <- which(pp.cit$y>y & pp.cit$y<y+cs) # which events lie within the y direction?
pz <- length(intersect(xi,yi)) # how many objects in x and y direction intersect?
sgdf@data$z[i]<- pz / (cs/1000)^2 # divide the number of points by the area
}
plot(raster(sgdf), col = gray.colors(25, start = 0.97, end = 0.4))
plot(boundary, add=TRUE)
points(pp.cit$x, pp.cit$y, pch=16, cex=0.4)
```
### Density calculation - 2nd approach ###
Kernel density estimation; again here performed step by step and rather inefficiently coded. Use it to understand but do not run the code.
```{r density2}
sgdf_kde <- sgdf # copy the grid into a new variable
sd <- 100000 # define the bandwidth of the kernel - it is NOT the pixel size; this is defined in the beginning of the 1str approach
# ...and think of it, the bandwidth shall not be smaller than the pixelsize, cause points outside the kernel are "underestimated"
for (i in seq(along=coordinates(gt)[,1])){ # loop through all pixel
x <- coordinates(gt)[i,1]
y <- coordinates(gt)[i,2]
g2 <- 0
for (j in seq(along=pp.cit$x)){ # we use ALL points
distanz <- sqrt((pp.cit$x[j] - x)^2 + (pp.cit$y[j] - y)^2) # calculate this distance of one point to the centre of the raster cell. this is necessary for the...
g1 <- dnorm(distanz, mean=0, sd=sd) #...weighting of point in th e cell using a normal distributed kernel
g2 <- g2 + g1 # now collect the weights for the different points under in the pixel to get the density value for the pixel
}
sgdf_kde@data$z[i]<- g2
}
plot(raster(sgdf_kde), col = gray.colors(25, start = 0.97, end = 0.4))
plot(boundary, add=TRUE)
points(pp.cit$x, pp.cit$y, pch=16, cex=0.4)
```
### Density calculation - 3rd approach ###
Kernel density estimation using the built in function of `spatstat`. Since the package is great, it is perfectly coded and fast. Play around with the values. Calculate also a KDE for the village data set.
```{r density3}
cs <- 1000
sd <- 50000
dens_r5 <- density(pp.cit, sd, eps=cs, edge=TRUE, at="pixels")
plot(dens_r5, col = gray.colors(25, start = 0.97, end = 0.4))
#contour(dens_r5, add=T)
points(pp.cit$x, pp.cit$y, pch=16, cex=0.4)
# use another measure for sd - in this case three times the mean nearest neighbor distance
sdev <- 3*mean(nndist(pp.cit))
dens_r6 <- density(pp.cit, sdev, eps=cs, edge=TRUE, at="pixels")
plot(dens_r6, col = gray.colors(25, start = 0.97, end = 0.4))
#contour(dens_r6, add=T)
points(pp.cit$x, pp.cit$y, pch=16, cex=0.4)
```
> **Exercise**
>
> 1. Use the `density` function to calculate the KDE for the village data set.
> 2. Change the size of the kernel and interpret/discuss the results
> 3. Get familiar with the function and concepts using the `spatstat` manual
## Second order effects ##
### G and F function
First approach based on theoretical assumptions, i.e. CSR
```{r GF1}
g.cit <- Gest(pp.cit)
plot(g.cit)
f.cit <- Fest(pp.cit)
plot(f.cit)
```
Second approach based on simulations of a theortical process, i.e. CSR
```{r GF2}
g.cit.env <- envelope(pp.cit, fun = Gest, nsim = 10)
plot(g.cit.env)
f.cit.env <- envelope(pp.cit, fun = Fest, nsim = 10)
plot(f.cit.env)
```
> **Question:** Interpret the results. Why two approaches? What are the advantages?
### K and L function
First approach based on theoretical assumptions, i.e. CSR
```{r kl1}
k.cit <- Kest(pp.cit)
plot(k.cit)
l.cit <- Lest(pp.cit)
plot(l.cit)
```
Second approach based on simulations of a theortical process, i.e. CSR
```{r kl2}
k.cit.env <- envelope(pp.cit, fun = Kest, nsim = 10)
plot(k.cit.env)
l.cit.env <- envelope(pp.cit, fun = Lest, nsim = 10)
plot(l.cit.env)
```
> **Question:** Interpret the results. Why two approaches? What are the advantages?
> **Exercise**
>
> 1. Calculate G,F,K, and L function for the village data set
> 2. Interpret the results. Which approach do you chose? Why?
> 3. Get familiar with the function and concepts using the `spatstat` manual
# References #