Professional Documents
Culture Documents
1. Spatial analysis in R
D G Rossiter
Spatial analysis with R 2
Spatial analysis in R
1. Spatial data
D G Rossiter
Spatial analysis with R 3
Spatial data
And they need special data structures which recognize the special status of
coördinates
D G Rossiter
Spatial analysis with R 4
R approaches
Add-in packages for spatial analysis; statisticians can implement their own ideas;
including:
* spatial (Ripley, based on 1981 book)
* geoR, geoRglm (Ribeiro & Diggle, basis for 2007 book)
* gstat (Pebesma)
* spatstat (Baddeley & Turner): point patterns
* circular: directional statistics
* RandomFields (Schlather)
D G Rossiter
Spatial analysis with R 5
Interface to GIS
Add-in packages:
D G Rossiter
Spatial analysis with R 6
Geostatistics
Spatial regression
Ecological analysis
D G Rossiter
Spatial analysis with R 7
Analysis sequence
Use the most suitable tool for each phase of the analysis:
2. Import to R
3. Perform analysis in R
D G Rossiter
Spatial analysis with R 8
The sp package
Motivation: “The advantage of having multiple R packages for spatial statistics seemed
to be hindered by a lack of a uniform interface for handling spatial data.”
This package provides classes and methods for dealing with spatial data in S
D G Rossiter
Spatial analysis with R 9
* points
* lines
* polygons
* grids (rasters)
D G Rossiter
Spatial analysis with R 10
D G Rossiter
Spatial analysis with R 11
The most common way to assign coordinates is to use the coordinates method as the
LHS of an expression.
First we see how spatial data looks when it is not treated as spatial data:
Fields x and y are coordinates but they have no special status in the dataframe. The
method spsample expects a spatial object but does not find it.
D G Rossiter
Spatial analysis with R 12
The S4 class has slots (@) for different kinds of information (coordinates, dimensions, data,
bounding box, projection)
D G Rossiter
Spatial analysis with R 13
SpatialPoints:
x y
[1,] 179246 330766
[2,] 180329 330055
[3,] 181280 329798
[4,] 180391 330627
[5,] 178725 330202
D G Rossiter
Spatial analysis with R 14
maptools
rgdal
Projections
D G Rossiter
Spatial analysis with R 15
These are restricted to just the named formats; a more generic solution was needed, which
is provided by . . .
D G Rossiter
Spatial analysis with R 16
readGDAL; writeGDAL: Read/write between GDAL grid maps and Spatial objects
readOGR, writeOGR: Read/write spatial vector data using OGR (including KML for
Google Earth)
* OGR: C++ open source library providing read (and sometimes write) access to a
variety of vector file formats including ESRI Shapefiles, S-57, SDTS, PostGIS,
Oracle Spatial, and Mapinfo mid/mif and TAB formats
1 http://www.gdal.org/
D G Rossiter
Spatial analysis with R 17
Projections
Until a projection is defined, the coördinates are just numbers; they are not related to the
Earth’s surface. This can be useful for “spatial” analysis of non-Earth objects, e.g. an image
from a microscope.
For true geographic analysis, the object must be related to the Earth’s figure.
D G Rossiter
Spatial analysis with R 18
For example, to specify the datum, elipsoid, projection, and coördinate system of the Dutch
Rijksdriehoek (RDH) system:
Most systems are included in the European Petroleum Survey Group (EPSG) database3, in
which case just the system’s numbrer in that database is enough to specify it:
2 http://trac.osgeo.org/proj/
3 http://www.epsg.org/
D G Rossiter
Spatial analysis with R 19
Highly-developed suite of tools with rich analytical possibilities; uses the sp spatial data
structures
Note: there are other R packages with largely overlapping aims but with different
philosophies and interfaces (notably, geoR and spatial).
D G Rossiter
Spatial analysis with R 20
Modelling
D G Rossiter
Spatial analysis with R 21
Example: cokriging
D G Rossiter
Spatial analysis with R 22
1171
●
15
1243
●
1266 1334
●
●
1115
●
1170
●
●
749
●
966
●
784
10
●
543
semivariance
●
744
●
656
●
401
5
●
209
●
281
distance
D G Rossiter
Spatial analysis with R 23
Variogram map
Variogram map, Meuse River, log(zinc)
var1
500
1.5
1.0
dy
0.5
−500
0.0
−500 0 500
dx
D G Rossiter
Spatial analysis with R 24
Authorized models
0.0 0.5 1.0 1.5 2.0 2.5 3.0 0.0 0.5 1.0 1.5 2.0 2.5 3.0
0 ● ● ● ● ●
1
semivariance
● ● ● ● ● 0
vgm(1,"Per",1) vgm(1,"Hol",1) vgm(1,"Log",1) vgm(1,"Pow",1) vgm(1,"Spl",1)
3
0 ● ● ● ● ●
vgm(1,"Err",0) vgm(1,"Int",0)
3
● 1
● 0
distance
D G Rossiter
Spatial analysis with R 25
Prediction
D G Rossiter
Spatial analysis with R 26
Kriging prediction
Predicted values, Co (ppm)
12 11
9 16
8 10
7
5 6
5
4
14
3
13
4 12
13 14 13
4 14
14 12 10
UTM N
11
3 13
12
13
87 4
6 56
11 10 7 8
12 9
10
12
2
13 8
7
12
6
6
9 11
10 13 12
7
8 6 12 2
9 10 113
14
4 10 15 4
1 5 11
76 12
11
12
2
1 2 3 4 5
UTM E
D G Rossiter
Spatial analysis with R 27
16
15
3
15
3
14
5 34 4 14
110
1
1211 3 4
9
109
3
78 12
14
13
3
4 4 1213
4 3
3
6 3 4 3 10
3 4
4
4
4 3 4 3 4
UTM N
4
3 3 8
3 4
4 3
5
3 4 4
4 4 3 4
4
12
5 4 6
4 3 4 4
3 4 3 4
2 4 4 3 4
4
3
4 4 78
4 4
4 4
3 3 4
3 4 3 4
4 4
10 3 3
4 4 3
4 2
4 4 4
1112
1 4 3 4
7
4
1098
13
1
4
14
1312
1
7 11
0
1 2 3 4 5
UTM E
D G Rossiter
Spatial analysis with R 28
Simulation
Shows possible states of nature, given the known data and the fitted model
D G Rossiter
Spatial analysis with R 29
D G Rossiter
Spatial analysis with R 30
Validation
D G Rossiter
Spatial analysis with R 31
Non-parametric methods
D G Rossiter
Spatial analysis with R 32
●
●
●●
● ●
●
● ●
●
● ●
● ● ●●
●
●
●
●
●
●
●
● 0.8
●
●
●
● ●●
●
●
●
●
●● ●
● ●
●
●
● ● ●
● ●● ●
●
●● ● ● ●
● ●
●
●
●●
●
●● ● ●●
●
●
0.6
●
●
●
●
●
●
●
● ●
● ●●
●
● ● ●
●
●
● ●
●
● ●
● ●
●
●
●
●
●
●
● ●
●● ●
● ●
● ●
●
● ●●
●
● ●
●
●
● 0.4
●
●
● ●
● ●
● ●
●
● ●
●
●
●
●●
● ●
●
● ● ●
●●
● ● ●●
● ●
●
● ●●
● ●
●
●
● ●
●●
●
●
● ●
●
● ●● ●
●
● ●
● ●
● ● ●
●
● ●
●
● ●●
●● ●
0.2
● ●
● ●●
●
● ●●
●
● ● ●
● ● ●● ●
●
● ●●●
● ● ●
●
● ●●● ●
● ●
●● ● ●
●● ●
●
●
●
●
●
●
●
● ●
0.0
A typical analysis
> # load an example dataset into the workspace
> data(meuse)
> # convert it to a spatial object
> coordinates(meuse) <- ~ x + y
> # compute the default experimental variogram
> v <- variogram(log(lead) ~ 1, meuse)
> # plot the variogram and estimate the model by eye
> plot(v)
> vm <- vgm(0.5, "Sph", 1000, 0.1)
> plot(v, model=vm)
> # fit the model with the default automatic fit
> (vmf <- fit.variogram(v, vm))
> # load a prediction grid
> data(meuse.grid)
> # convert it to a spatial object
> coordinates(meuse.grid) <- ~ 1
> # predict on the grid by Ordinary Kriging
> ko <- krige(log(lead) ~ 1, meuse, newdata=meuse.grid, model=vmf)
> summary(ko)
> # plot the map
> spplot(ko, zcol="var1.pred")
> # leave-one-out cross-validation
> kcv <- krige.cv(log(lead) ~ 1, meuse, model=vmf)
> summary(kcv)
D G Rossiter
Spatial analysis with R 34
D G Rossiter
Spatial analysis with R 35
D G Rossiter
Spatial analysis with R 36
Conclusion
The main advantage of doing spatial analysis in R is that the full power of the R
environment (data manipulation, non-spatial modelling, user-defined functions, graphics
. . . ) can be brought to bear on spatial analyses
D G Rossiter