exercise: Click_here
solution 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/Rpackages")
.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_2015/exercise/data/GIS_data"
results <-"E:/Dropbox/postdoc/course/PhD_course_2015/exercise/exercise_4"
# 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)
# transforming
data.set$lprice<-log(data.set$price)
ols_model<-lm(lprice~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)
###########################################################################
### spatial error model gmm
###########################################################################
coordinates(data.set)<-c("coord_x","coord_y")
coord_data.set<-coordinates(data.set)
class(data.set)
### nearest neighbour weights
neighbours <-knn2nb(knearneigh(coord_data.set, k=10))
weight <- nb2listw(neighbours, style="W")
# model estimations
error_model<-gstslshet(lprice~area
+cpark
+cnature
+factor(date_year),
data=data.set,
listw=weight,
initial.value=0.73,
sarar=FALSE)
summary(error_model)
# r-square
R2<-1-sum(error_model$residuals^2)/(sum((data.set$price-mean(data.set$price))^2))
print(c('R square',R2))
###########################################################################
### spatial lag + error model sarar
###########################################################################
# model estimations
sarar_model<-gstslshet(lprice~area
+cpark
+cnature
+factor(date_year),
data=data.set,
listw=weight,
initial.value=0.43,
sarar=TRUE)
summary(sarar_model)
# r-square
R2<-1-sum(sarar_model$residuals^2)/(sum((data.set$price-mean(data.set$price))^2))
print(c('R square',R2))
###########################################################################
### impact measure
###########################################################################
# creating a sparse trace matrix
W_weight<-as(as_dgRMatrix_listw(weight), "CsparseMatrix")
tr_weight<- trW(W_weight, type="mult")
set.seed(110606)
# calculating impact estimates
sarar_model_impact <- impacts(sarar_model, tr=tr_weight, R=500)
sarar_model_impact
summary(sarar_model_impact)
###########################################################################
### exercise: play with the models
###########################################################################
# In your group play around with the model, change the weight matrix, add new variables
# and change functional form.
# Do your model reduce spatial autocorrelation?
# choose the model which your group think is the best and present your model to another group
# sent your model result to toke - tepp@ifro.ku.dk
###########################################################################
### spatial error model - maximum likelihood spdep packages
###########################################################################
# creating a trace matrix
W <- as(as_dgRMatrix_listw(weight), "CsparseMatrix")
set.seed(110531)
trMat <- trW(W, type="MC")
# defining base equation
form<-formula(lprice~area
+cpark
+cnature
+factor(date_year))
# estimating an error model
error_model<-errorsarlm(form,
data=data.set,
listw=weight,
method="LU",
trs=trMat,
interval=c(0.01,0.4)
)
summary(error_model)
# correcting for heteroscedasticity
lm.target <- lm(error_model$tary ~ error_model$tarX - 1)
robust_table<-if (require(lmtest) && require(sandwich)) {
coeftest(lm.target, vcov=vcovHC(lm.target, type="HC0"), df=Inf)
}
# creating a relevant table
rownames(robust_table)<-names(error_model$coefficients)
table<-robust_table
table<-data.frame(table[,1:4])
colnames(table)[colnames(table)=="Pr...z.."]<-"P-value"
table$stars<-table$"P-value"
table$stars <- ifelse(table$stars < .001, "***",
ifelse(table$stars < .01, "** ",
ifelse(table$stars < .05, "* ",
ifelse(table$stars < .1, ". ",
" ")))) # so 5 categories
# output table
table
#####################################################################################################
## Exercsie: Spatial lag model - maximum likelihood spdep packages
#####################################################################################################
# Exercise attempt to estimate the spatial lag model using
# the spdep packages
###########################################################################
### end of script
###########################################################################