Sie sind auf Seite 1von 5

Gianni Gorgoglione

1
Spatial Analysis Clustering
Work example in R
y<-readShapePoly("Stordal.shp")
proj4string(y) <- CRS("+proj=utm +zone=30 +ellps=WGS84 +datum=WGS84 +units=m +no_defs")
a <- raster("twi_ascii.txt")
ps <- raster("stordal_image.img")

This first block of code read the data from a shape file polygon and then assigns the reference
coordinate system projection with UTM zone 30. A variable called y is assigned to this shape-file. After
we have a raster file as ASCII assigned to the variable a. Another raster as image file is assigned to the
variable ps.
a1 <- focal(a, w=matrix(1/9,nrow=3,ncol=3))
b <- rasterize(y,a1)
a2<-mask(a1,b)
a4 <- trim(a2)
a7 <- rasterToPolygons(a4)
proj4string(a7) <- CRS("+proj=utm +zone=30 +ellps=WGS84 +datum=WGS84 +units=m +no_defs")
a1 is a variable that takes values from raster a and use the function focal that uses a matrix of weights
for the neighborhood of the cells where function default is sum. It works like a moving window
throughout the raster to extrapolate new values from raster a. Afterwards, We define new variable,
b(rasterize of y and a1), a2(the mask), a4(a trim of a2), a7(conversion of a4 to polygon with specific
reference system). That is made in order to cut the area of interest above the DEM.
a7_mbq <- poly2nb(a7)
a7_mbq_w <- nb2listw(a7_mbq)

In this step the variable a7 gets a list of neighbors calculated according to the relationship between
contiguous boundaries that share same points. The next line gives more data about the spatial weights
with a neighbors list.
Install.packages(spdep)
a7_moral <- localmoran(a7@data[,1],a7_mbq_w)
z.value.class <- a7_moral[,4]
a7_moran_class <- a7
a7_moran_class@data[,1] <- z.value.class

Gianni Gorgoglione

2
The Moran I-value is calculated by using the variable a7 from vector list that has the neighbours list of
spatial weights of a7_mbq. The returned values contain even a z-value.
Then, it has been defined variables z.value.class, a7_moran_class and, a7_moran_class@data[,1] by
extracting the interesting values for the object a7_moran_class z-values.
Install.packages(spdep)
a7_getis <- localG(a7@data[,1],a7_mbq_w)
z.value.class <- a7_getis
a7_getis_class <- a7
a7_getis_class@data[,1] <- z.value.class

The statistic function localG gives as result (z-values) high values or low values for respective clusters
values. Then z.value.class gets those results from localG. Then it has been created new variable again
with values from a7.
rgb.palette <- colorRampPalette(c("blue","khaki2", "red"), space = "rgb")
brks<-c(-1000,-2.58,-1.96,-1.65,1.65,1.96,2.58,1000)

A color ramp palette is defined and assigned to the variable rgb.palette. Breaks are decided to.
plot(ps, col=grey(0:256/256), legend=FALSE, main="Local Morans
I",ext=c(665850,667100,7587400,7588600))
par(bg="transparent")
plot(y, border="white", col="transparent", add=TRUE)
par(bg="transparent")
plot(a7_moran_class,col=rgb.palette(7)[cut(a7_moral[,4],breaks=brks)],add=TRUE)
legend("topleft", legend=leglabs(brks), fill=rgb.palette(7), bty="white",bg="white")
Gianni Gorgoglione

3


The clustering method of this data sample is Morans I. The spot representing values above +1 are those
area where we can expect a strong spatial pattern, thus we can reject the null hypothesis. In other
words, in those area there is clustering. On the other hand, the yellow zones represent those area
where Morans I is below 1 or O. These are the cases where there is not pattern or it is extremely rare to
find one.



plot(ps, col=grey(0:256/256), legend=FALSE, main="Getis/Ord
G*",ext=c(665850,667100,7587400,7588600))
par(bg="transparent")
plot(y, border="white", col="transparent", add=TRUE)
666000 666200 666400 666600 666800 667000
7
5
8
7
4
0
0
7
5
8
7
8
0
0
7
5
8
8
2
0
0
7
5
8
8
6
0
0
Local Morans I
under -2.58
-2.58 - -1.96
-1.96 - -1.65
-1.65 - 1.65
1.65 - 1.96
1.96 - 2.58
over 2.58
Gianni Gorgoglione

4
par(bg="transparent")
plot(a7_getis_class,col=rgb.palette(8)[cut(a7_getis,breaks=brks)],add=TRUE)
legend("topleft", legend=leglabs(brks), fill=rgb.palette(7), bty="white",bg="white")


In this case, we can see both the zone with a high and low TWI value. That was possible by using the
statistical function LocalG that calculated points values according to high and low values of clusters
values.

Morans I takes into account weight term that represents the relationship between two elements in the
dataset and tell about their spatial relationship. So, its possible to decide the weight by using a simple
approach where in case of adjacency the weight will be 1 and in other case 0. (Binary table).
Then, the weight is multiplied with an expression that compares attribute values for each pair of values
(the mean and standard deviation of all dataset), with the z-scores of the variable values for each value.
666000 666200 666400 666600 666800 667000
7
5
8
7
4
0
0
7
5
8
7
6
0
0
7
5
8
7
8
0
0
7
5
8
8
0
0
0
7
5
8
8
2
0
0
7
5
8
8
4
0
0
7
5
8
8
6
0
0
Getis/Ord G*
under -2.58
-2.58 - -1.96
-1.96 - -1.65
-1.65 - 1.65
1.65 - 1.96
1.96 - 2.58
over 2.58
Gianni Gorgoglione

5
At the end, Morans I value as +1 indicate a very strong spatial pattern. Morans I closer to -1 indicate a
rare spatial pattern and values around 0 indicate an absence of spatial pattern. We can say that the
spatial process was random.
The Getis-Ord Gi* give as result a z-score. This is significant to understand if high values and low values
are clustered. The larger the z-score the more intense are the positive clustered values. Vice-versa, the
smaller the z-score is, the more intense the clustered low values. Thus, we have hot spot and cold spot
depending of the z-value.
The main difference between these global statistics test is the Getis G* tells more about the clustered
type value. It is possible to detect if clusters have high values or not.

Das könnte Ihnen auch gefallen