exercise 3:Click_here
solutions to exercise: Click_here
############################################################################
### the purpose of this is to learn to construct spatial weight matrix and
### do some simple GIS calculations
############################################################################
###########################################################################
### installing and downloading relavant libraries
###########################################################################
#removing ervery ting in R
rm(list=ls())
# sometimes you need to define the loaction of the libraries
.libPaths("E:/Dropbox/R_packges")
.libPaths()
# list of relevant libraries
pack<-c("rgdal", "classInt", "GISTools", "maps", "dismo", "raster", "foreign", "shape",
"ggmap","knitr", "reshape", "rgeos","spdep","sphet", "tripack", "pgirmess")
# installing relavnt libraries from list
# for (i in pack){
# install.packages(pkgs=i, dependencies=TRUE)
# }
# update.packages()
# calling libraries
lapply(pack, require, character.only=T)
###########################################################################
### setting working directory
###########################################################################
# defining location
data <-"E:/Dropbox/postdoc/course/PHD_course/PHD_GIS_2017/exercises/data/GIS_data"
results <-"E:/Dropbox/postdoc/course/PHD_course/PHD_GIS_2017/exercises/exercise_2"
# what is my working directory
getwd()
# changing directory
setwd(data)
# looking in the directory folder
dir()
###########################################################################
### loading data
###########################################################################
load("Ex_data.RData")
###########################################################################
### create a simple OLS model
###########################################################################
names(data.set)
str(data.set)
ols_model<-lm(price~area+cpark+cnature+factor(date_year), data=data.set)
summary(ols_model)
# fast look at the distribution of the residuals
hist(ols_model$residuals, n=40)
###########################################################################
### Exercise 3.1 create a spatial dataframe of the house price data
###########################################################################
###########################################################################
### weight matrix
###########################################################################
## defining the coordinates of the data.set
coord_data.set<-coordinates(data.set)
### nearest neighbour spatial weight matrix
# the neighbour
neighbours <-knn2nb(knearneigh(coord_data.set, k=10))
# looking at the relationship
plot(data.set)
plot(neighbours, coord_data.set, pch=19, cex=0.6, add=TRUE)
# creating the weights
weight <- nb2listw(neighbours, style="W")
# look at the weight
weight
### within a distance of spatial weight matrix
neighbours<-dnearneigh(coord_data.set,
0,
200,
row.names = NULL,
longlat = NULL)
weight <- nb2listw(neighbours, style="W", zero.policy=TRUE)
### inverse distance
nbs <- dnearneigh(coord_data.set, 0, 1000)
dsts <- nbdists(nbs, coord_data.set)
summary(unlist(dsts))
idw <- lapply(dsts, function(x) 1/(x))
weight<- nb2listw(nbs, glist=idw, style="B")
###########################################################################
### Global Morans I - spatial autocorrelation
###########################################################################
# creating weights
neighbours <-knn2nb(knearneigh(coord_data.set, k=10))
weight <- nb2listw(neighbours, style="W")
# addiing the residuals the house price data.set
data.set$residuals<-ols_model$residuals
# global Moran's I test
moran.test(data.set$residuals,
weight,
zero.policy=NULL,
alternative="two.sided")
###########################################################################
### exercise 3.2; Global Morans I - spatial autocorrelation
###########################################################################
# test the sensitivity of different weights on the Morans I index
# discuss in the group which weight is appropriate
###########################################################################
### local Morans I
###########################################################################
# creating local morans I test results
single_moran <-localmoran(data.set$residuals,weight, zero.policy=TRUE)
# adding test results to data
data.set$moransI<-single_moran[,1]
data.set$moransIPR<-single_moran[,5]
### Plotting residuals point ###
pal <- brewer.pal(10,"RdBu")
q10 <- classIntervals(data.set$residuals, n=6, style="quantile", dataPrecision =2)
q10Colours <- findColours(q10, pal)
plot(data.set, axes=TRUE, col = q10Colours, pch =17, cex=1)
# plot(sea, add=TRUE)
# title("Detrended Logged House Prices")
legend("topright",
fill = attr(q10Colours, "palette"),
border="black",
legend=names(attr(q10Colours, "table")),
bg ="white",
title="Residuals")
### map moran I values: point ###
pal <- brewer.pal(6,"RdBu")
q10 <- classIntervals(data.set$moransI, n=6, style="quantile", dataPrecision =2)
q10Colours <- findColours(q10, pal)
plot(data.set, axes=TRUE, col = q10Colours, pch =18, cex=1)
title("Local Moran's I - Cluster Analysis")
legend("topright",
fill = attr(q10Colours, "palette"),
legend=names(attr(q10Colours, "table")),
border="black",
# bty="n",
bg="white",
title="Moran's I values")
######### plotting significant clusters #####
plot(data.set, col="grey", pch=18, cex=1, add=TRUE)
data.set_sigclust<-subset(data.set, moransIPR<0.1)
plot(data.set_sigclust, col="red", pch=20, cex=0.4, add=TRUE)
##################################################################################
### exercise 3.3 plot signigicant spatial patterns using voronoi polygon of data.set
##################################################################################
# hint: voronoi poligons can be created in QGIS
# to create neighbours and spatial weights using these functions poly2nb, nblag, nblag_cumul, nb2listw
###########################################################################
### spatial correlogram
###########################################################################
## looking into to the extent of the spatial correlation
cor8_rook_W <- sp.correlogram(neighbours=neighbours ,
var=voronoi_polygons$price,
order=8, method="I", style="W")
plot(cor8_rook_W, main="Contiguity lag orders",
labcex=3,
cex.axis=2,
cex.main =2,
cex.sub=3,
cex.lab=4)
require(pgirmess)
spatial_correlation<-correlog(coord_data.set, data.set$price, method="Moran")
plot(spatial_correlation)
## exercise: which extent do you think the the weight matrix should be?
###########################################################################
### end of script
###########################################################################