Title: | Hierarchical Spatial Autoregressive Model |
---|---|
Description: | A Hierarchical Spatial Autoregressive Model (HSAR), based on a Bayesian Markov Chain Monte Carlo (MCMC) algorithm (Dong and Harris (2014) <doi:10.1111/gean.12049>). The creation of this package was supported by the Economic and Social Research Council (ESRC) through the Applied Quantitative Methods Network: Phase II, grant number ES/K006460/1. |
Authors: | Guanpeng Dong [aut, cph] , Wenbo Lv [aut, cre] , Richard Harris [aut], Angelos Mimis [aut] |
Maintainer: | Wenbo Lv <[email protected]> |
License: | GPL (>= 2) |
Version: | 0.6.0 |
Built: | 2024-12-05 03:09:33 UTC |
Source: | https://github.com/SpatLyu/HSAR |
Boundaries of districts in Beijing
Beijingdistricts
Beijingdistricts
An object of class sf
(inherits from data.frame
) with 111 rows and 2 columns.
Municipality departments of Athens
depmunic
depmunic
An object of class sf
(inherits from data.frame
) with 7 rows and 8 columns.
An sf object of 7 polygons with the following 7 variables:
An unique identifier for each municipality department.
The number of airbnb properties in 2017
The number of museums
The population recorded in census at 2011.
The number of citizens that the origin is a non european country.
The area of green spaces (unit: square meters).
The area of the polygon (unit: square kilometers).
The specification of a HSAR model is as follows:
where and
are indicators of lower- and higher-level spatial units.
is the number of lower-level units in the
higher level unit and
.
and
represent vectors of lower- and higher-level independent variables.
and
are regression coefficients to estimate.
, a
vector of higher-level random effects, also follows a simultaneous autoregressive process.
and
are two spatial weights matrices (or neighbourhood connection matrices) at the lower and higher levels, defining how spatial units at each level are connected.
and
are two spatial autoregressive parameters measuring the strength of the dependencies/correlations at the two spatial scales.
A succinct matrix formulation of the model is,
It is also useful to note that the HSAR model nests a standard (random intercept) multilevel model model when and
are both equal to zero and a standard spaital econometric model when
and
are both equal to zero.
hsar( formula, data = NULL, W = NULL, M = NULL, Delta, burnin = 5000, Nsim = 10000, thinning = 1, parameters.start = NULL )
hsar( formula, data = NULL, W = NULL, M = NULL, Delta, burnin = 5000, Nsim = 10000, thinning = 1, parameters.start = NULL )
formula |
A symbolic description of the model to fit. A formula for the covariate part of the model using the syntax of the lm() function fitting standard linear regression models. Neither the response variable nor the explanatory variables are allowed to contain NA values. |
data |
A |
W |
The N by N lower-level spatial weights matrix or neighbourhood matrix where N is the total number of lower-level spatial units. The formulation of W could be based on geographical distances separating units or based on geographical contiguity. To ensure the maximum value of the spatial autoregressive parameter |
M |
The J by J higher-level spatial weights matrix or neighbourhood matrix where J is the total number of higher-level spatial units. Similar with W, the formulation of M could be based on geographical distances separating units or based on geographical contiguity. To ensure the maximum value of the spatial autoregressive parameter |
Delta |
The N by J random effect design matrix that links the J by 1 higher-level random effect vector back to the N by 1 response variable under investigation. It is simply how lower-level units are grouped into each high-level units with columns of the matrix being each higher-level units. As with W and M, |
burnin |
The number of MCMC samples to discard as the burnin period. |
Nsim |
The total number of MCMC samples to generate. |
thinning |
MCMC thinning factor. |
parameters.start |
A list with names "rho", "lambda", "sigma2e", "sigma2u" and "beta" corresponding to initial values for the model parameters |
A list
.
A matrix with the MCMC samples of the draws for the coefficients.
A vector of estimated mean values of regression coefficients.
The standard deviations of estimated regression coefficients.
The estimated mean of the lower-level spatial autoregressive parameter .
The standard deviation of the estimated lower-level spatial autoregressive parameter.
The estimated mean of the higher-level spatial autoregressive parameter .
The standard deviation of the estimated higher-level spatial autoregressive parameter.
The estimated mean of the lower-level variance parameter .
The standard deviation of the estimated lower-level variance parameter .
The estimated mean of the higher-level variance parameter .
The standard deviation of the estimated higher-level variance parameter .
Mean values of
Standard deviation of
The deviance information criterion (DIC) of the fitted model.
The effective number of parameters of the fitted model.
The log-likelihood of the fitted model.
A pseudo R square model fit indicator.
Summaries of the direct impact of a covariate effect on the outcome variable.
Summaries of the indirect impact of a covariate effect on the outcome variable.
Summaries of the total impact of a covariate effect on the outcome variable.
In order to use the hsar() function, users need to specify the two spatial weights matrices W and M and the random effect design matrix . However, it is very easy to extract such spatial weights matrices from spatial data using the package spdep. Geographic distance-based or contiguity-based spatial weights matrix for both spatial points data and spatial polygons data are available in the spdep package.
Before the extraction of W and M, it is better to first sort the data using the higher-level unit identifier. Then, the random effect design matrix can be extracted simply (see the following example) and so are the two spatial weights matrices. Make sure the order of higher-level units in the weights matrix M is in line with that in the
matrix.
Two simpler versions of the HSAR model can also be fitted using the hsar() function. The first is a HSAR model with
equal to zero, indicating an assumption of independence in the higher-level random effect
. The second is a HSAR with
equal to zero, indicating an independence assumption in the outcome variable conditioning on the hgiher-level random effect. This model is useful in situations where we are interested in the neighbourhood/contextual effect on individual's outcomes and have good reasons to suspect the effect from geographical contexts upon individuals to be dependent. Meanwhile we have no information on how lower-level units are connnected.
Dong, G. and Harris, R. 2015. Spatial Autoregressive Models for Geographically Hierarchical Data Structures. Geographical Analysis, 47:173-191.
LeSage, J. P., and R. K. Pace. (2009). Introduction to Spatial Econometrics. Boca Raton, FL: CRC Press/Taylor & Francis.
library(spdep) # Running the hsar() function using the Beijing land price data data(landprice) # load shapefiles of Beijing districts and land parcels data(Beijingdistricts) data(land) plot(Beijingdistricts,border="green") plot(land,add=TRUE,col="red",pch=16,cex=0.8) # Define the random effect matrix model.data <- landprice[order(landprice$district.id),] head(model.data,50) # the number of individuals within each neighbourhood MM <- as.data.frame(table(model.data$district.id)) # the total number of neighbourhood, 100 Utotal <- dim(MM)[1] Unum <- MM[,2] Uid <- rep(c(1:Utotal),Unum) n <- nrow(model.data) Delta <- matrix(0,nrow=n,ncol=Utotal) for(i in 1:Utotal) { Delta[Uid==i,i] <- 1 } rm(i) # Delta[1:50,1:10] Delta <- as(Delta,"dgCMatrix") # extract the district level spatial weights matrix using the queen's rule nb.list <- spdep::poly2nb(Beijingdistricts) mat.list <- spdep::nb2mat(nb.list,style="W") M <- as(mat.list,"dgCMatrix") # extract the land parcel level spatial weights matrix nb.25 <- spdep::dnearneigh(land,0,2500) # to a weights matrix dist.25 <- spdep::nbdists(nb.25,land) dist.25 <- lapply(dist.25,function(x) exp(-0.5 * (x / 2500)^2)) mat.25 <- spdep::nb2mat(nb.25,glist=dist.25,style="W") W <- as(mat.25,"dgCMatrix") ## run the hsar() function res.formula <- lnprice ~ lnarea + lndcbd + dsubway + dpark + dele + popden + crimerate + as.factor(year) betas= coef(lm(formula=res.formula,data=landprice)) pars=list( rho = 0.5,lambda = 0.5, sigma2e = 2.0, sigma2u = 2.0, betas = betas ) ## Not run: res <- hsar(res.formula,data=model.data,W=W,M=M,Delta=Delta, burnin=500, Nsim=1000, thinning = 1, parameters.start=pars) summary(res) # visualise the district level random effect library(classInt) library(RColorBrewer) x <- as.numeric(res$Mus) breaks <- classIntervals(x,4,"fisher")$brks groups <- cut(x,breaks,include.lowest=TRUE,labels=FALSE) palette <- brewer.pal(4, "Blues") plot(Beijingdistricts,col=palette[groups],border="grey") ## End(Not run)
library(spdep) # Running the hsar() function using the Beijing land price data data(landprice) # load shapefiles of Beijing districts and land parcels data(Beijingdistricts) data(land) plot(Beijingdistricts,border="green") plot(land,add=TRUE,col="red",pch=16,cex=0.8) # Define the random effect matrix model.data <- landprice[order(landprice$district.id),] head(model.data,50) # the number of individuals within each neighbourhood MM <- as.data.frame(table(model.data$district.id)) # the total number of neighbourhood, 100 Utotal <- dim(MM)[1] Unum <- MM[,2] Uid <- rep(c(1:Utotal),Unum) n <- nrow(model.data) Delta <- matrix(0,nrow=n,ncol=Utotal) for(i in 1:Utotal) { Delta[Uid==i,i] <- 1 } rm(i) # Delta[1:50,1:10] Delta <- as(Delta,"dgCMatrix") # extract the district level spatial weights matrix using the queen's rule nb.list <- spdep::poly2nb(Beijingdistricts) mat.list <- spdep::nb2mat(nb.list,style="W") M <- as(mat.list,"dgCMatrix") # extract the land parcel level spatial weights matrix nb.25 <- spdep::dnearneigh(land,0,2500) # to a weights matrix dist.25 <- spdep::nbdists(nb.25,land) dist.25 <- lapply(dist.25,function(x) exp(-0.5 * (x / 2500)^2)) mat.25 <- spdep::nb2mat(nb.25,glist=dist.25,style="W") W <- as(mat.25,"dgCMatrix") ## run the hsar() function res.formula <- lnprice ~ lnarea + lndcbd + dsubway + dpark + dele + popden + crimerate + as.factor(year) betas= coef(lm(formula=res.formula,data=landprice)) pars=list( rho = 0.5,lambda = 0.5, sigma2e = 2.0, sigma2u = 2.0, betas = betas ) ## Not run: res <- hsar(res.formula,data=model.data,W=W,M=M,Delta=Delta, burnin=500, Nsim=1000, thinning = 1, parameters.start=pars) summary(res) # visualise the district level random effect library(classInt) library(RColorBrewer) x <- as.numeric(res$Mus) breaks <- classIntervals(x,4,"fisher")$brks groups <- cut(x,breaks,include.lowest=TRUE,labels=FALSE) palette <- brewer.pal(4, "Blues") plot(Beijingdistricts,col=palette[groups],border="grey") ## End(Not run)
The spatial locations of the Beijing land price data
land
land
An object of class sf
(inherits from data.frame
) with 1117 rows and 3 columns.
Leased residential land parcels, from 2003 to 2009 in Beijing, China
landprice
landprice
An object of class data.frame
with 1117 rows and 11 columns.
A data.frame
with 1117 observations on the following 11 variables.
An unique identifier for each land parcel.
The log of the leasing price per square metre of each residential land parcel (unit: RMB, Chinese yuan)
The log of the distance of each land parcel to the nearest railway station (unit:meters)
The log of the distance of each land parcel to the nearest elementary school (unit:meters)
The log of the distance of each land parcel to the nearest green park (unit:meters)
The log of the size of each land parcel (unit: square meters).
The log of the distance of each land parcel to the CBD (centre business district) in Beijing (unit:meters)
The year when each land parcel was leased with values of 0,1,2,3,4,5,6 representing year 2003,2004,2005,2006,2007,2008,2009
The population density of each district (unit: 1000 persons per square kilometers)
The number of reported serious crimes committed in each district per 1000 persons.
The identifier of the district where each land parcel is located.
A dataset of apartments in the municipality of Athens for 2017. Point location of the properties is given together with their main characteristics and the distance to the closest metro/train station.
properties
properties
An object of class sf
(inherits from data.frame
) with 1000 rows and 7 columns.
An sf object of 1000 points with the following 6 variables.
An unique identifier for each property.
The size of the property (unit: square meters)
The asking price (unit: euros)
The asking price per squre meter (unit: euroes/square meter).
Age of property in 2017 (unit: years).
The distance to closest train/metro station (unit: meters).
The sar()
function implements a standard spatial econometrics model (SAR) or a spatially lagged dependent
variable model using the Markov chain Monte Carlo (McMC) simulation approach.
sar( formula, data = NULL, W, burnin = 5000, Nsim = 10000, thinning = 1, parameters.start = NULL )
sar( formula, data = NULL, W, burnin = 5000, Nsim = 10000, thinning = 1, parameters.start = NULL )
formula |
A symbolic description of the model to fit. A formula for the covariate part of the model
using the syntax of the |
data |
A |
W |
The N by N spatial weights matrix or neighbourhood matrix where N is the number of spatial units.
The formulation of W could be based on geographical distances separating units or based on geographical contiguity.
To ensure the maximum value of the spatial autoregressive parameter |
burnin |
The number of McMC samples to discard as the burnin period. |
Nsim |
The total number of McMC samples to generate. |
thinning |
MCMC thinning factor. |
parameters.start |
A list with names "rho", "sigma2e", and "beta" corresponding to initial values for the model parameters
|
A list
.
A matrix with the MCMC samples of the draws for the coefficients.
A vector of estimated mean values of regression coefficients.
The standard deviations of estimated regression coefficients.
The estimated mean of the lower-level spatial autoregressive parameter .
The standard deviation of the estimated lower-level spatial autoregressive parameter.
The estimated mean of the lower-level variance parameter .
The standard deviation of the estimated lower-level variance parameter .
The deviance information criterion (DIC) of the fitted model.
The effective number of parameters of the fitted model.
The log-likelihood of the fitted model.
A pseudo R square model fit indicator.
Summaries of the direct impact of a covariate effect on the outcome variable.
Summaries of the indirect impact of a covariate effect on the outcome variable.
Summaries of the total impact of a covariate effect on the outcome variable.
Anselin, L. (1988). Spatial Econometrics: Methods and Models. Dordrecht: Kluwer Academic Publishers.
LeSage, J. P., and R. K. Pace. (2009). Introduction to Spatial Econometrics. Boca Raton, FL: CRC Press/Taylor & Francis
data(landprice) head(landprice) data(land) # extract the land parcel level spatial weights matrix library(spdep) library(Matrix) nb.25 <- spdep::dnearneigh(land,0,2500) # to a weights matrix dist.25 <- spdep::nbdists(nb.25,land) dist.25 <- lapply(dist.25,function(x) exp(-0.5 * (x / 2500)^2)) mat.25 <- spdep::nb2mat(nb.25,glist=dist.25,style="W") W <- as(mat.25,"dgCMatrix") ## run the sar() function res.formula <- lnprice ~ lnarea + lndcbd + dsubway + dpark + dele + popden + crimerate + as.factor(year) betas= coef(lm(formula=res.formula,data=landprice)) pars=list(rho = 0.5, sigma2e = 2.0, betas = betas) ## Not run: res <- sar(res.formula,data=landprice,W=W, burnin=500, Nsim=1000, thinning=1, parameters.start=pars) summary(res) ## End(Not run)
data(landprice) head(landprice) data(land) # extract the land parcel level spatial weights matrix library(spdep) library(Matrix) nb.25 <- spdep::dnearneigh(land,0,2500) # to a weights matrix dist.25 <- spdep::nbdists(nb.25,land) dist.25 <- lapply(dist.25,function(x) exp(-0.5 * (x / 2500)^2)) mat.25 <- spdep::nb2mat(nb.25,glist=dist.25,style="W") W <- as(mat.25,"dgCMatrix") ## run the sar() function res.formula <- lnprice ~ lnarea + lndcbd + dsubway + dpark + dele + popden + crimerate + as.factor(year) betas= coef(lm(formula=res.formula,data=landprice)) pars=list(rho = 0.5, sigma2e = 2.0, betas = betas) ## Not run: res <- sar(res.formula,data=landprice,W=W, burnin=500, Nsim=1000, thinning=1, parameters.start=pars) summary(res) ## End(Not run)