An application of HSAR for asking prices in the municipality of Athens

An application of hsar(), based on rel data, will be illustrated. The design of the weight matrices needed and the random effect design matrix will be explained.

Libraries

We start by loading the libraries that will be used.

library(sf)
library(tidyverse)
library(spdep)
library(HSAR)

Reading the datasets

At the higher level, we have the seven departments of the municipality of Athens and at the lower level we have the point data of the properties.

data(depmunic)
data(properties)
plot(st_geometry(depmunic),col = sf.colors(12, categorical = TRUE), border = 'grey')
plot(st_geometry(properties),add=TRUE,col="red",pch=16,cex=0.6)

The characteristics that come with the areal data are the id of the department, the number of airbnb properties, the number of museums, the population, the number of citizens with origin a non european union country, the area of the green space (m^2) and the area of the polygon (km^2).

attributes(depmunic)$names
depmunic$pop_rest

The characteristics of the properties are the size (m^2), the asking price (euros), the price per square meter, the age (years) and the shortest distance to metro/train station (m).

attributes(properties)$names
hist(properties$age, xlab = "Age", main="Age of the properties")

Now we are going to create two more variables at the higher, municipality department, level. The first one is the population density per 10k citizens, and the second one is the percentage of non EU citizens.

depmunic$popdens <- depmunic$population/ (10000*depmunic$area)
depmunic$foreigners <- 100 * depmunic$pop_rest/ depmunic$population

The next step is to create the model data that are going to use in the hsar model. For that, we need for each property (lower data), the data from the relevant department(higher level).

properties_in_dd <- st_join(properties, depmunic, join = st_within)

So now, we know each property, in which department resides and the coresponding data for that polygon. We also need that data in sorting order.

model.data <- properties_in_dd[order(properties_in_dd$num_dep),]

Create matrices used in the hsar function

In order to run the model we need to create the effect design matrix (Delta), the weight matrix for the high-level - polygon data (M), and the weight matrix for the lower level - point data (W).

In order to define the random effect matrix, we start with estimating the number of properties in each municipality department

properties_count <- count(as_tibble(model.data), num_dep)
MM <- as.data.frame(properties_count)

and by geting the total number of municipality departments (7), we define a vector with the number of municipality department that each property belongs

Utotal <- dim(MM)[1]
Unum <- MM[,2]
Uid <- rep(c(1:Utotal),Unum)

We then define the random effect matrix (Delta) wich has a dimension of 1000x7

n <- nrow(properties)
Delta <- matrix(0,nrow=n,ncol=Utotal)
for(i in 1:Utotal) {
  Delta[Uid==i,i] <- 1
}

Delta <- as(Delta,"dgCMatrix")

Now we estimate the spatial weight matrix at the higher level which in our case is the municipality departments (polygons). So we start with poly2nb which constructs the neighbours list for polygons and then with nb2mat we generate the weight matrix for the neighbours list previously created. Then we transform the weight matrix in a sparse matrix format.

nb.list <- poly2nb(depmunic)
mat.list <- nb2mat(nb.list,style="W")
M <- as(mat.list,"dgCMatrix")

to have a closer look at M , we can visualize it

plot(st_geometry(depmunic),border = 'grey')
plot( st_centroid(depmunic$geometry), add=TRUE )
plot(nb.list, st_centroid(depmunic$geometry), add= TRUE )

Similarly, we create the spatial weight matrix at the lower level of properties (point data). So we create the neighbour list at a distance of 1300 meters

nb.1300 <- dnearneigh(properties,0,1300)

and the weights matrix W as follows

mat.1300 <- nb2mat(nb.1300,style="W")
W <- as(mat.1300,"dgCMatrix")

For the W matrix, we can check the neighbours statistics

nb.1300

Run the models

So, having ready the matrices Delta, M and W, we wun the hsar() function

res.formula <- prpsqm ~ size + age + foreigners + greensp + popdens + museums + airbnb
res <- hsar(res.formula,data=model.data,W=W,M=M,Delta=Delta,
            burnin=500, Nsim=1000)
summary(res)

and the two simpler models defined for rho = 0 and lambda=0. So, firstly, assuming rho = 0 (no interaction effects at the lower level) we get

res_1 <- hsar(res.formula,data=model.data,W=NULL,M=M,Delta=Delta,burnin=500, Nsim=1000)
summary(res_1)

and secondly, given lambda = 0 (no interaction at the higher level) we get

res_2 <- hsar(res.formula,data=model.data,W=W,M=NULL,Delta=Delta,burnin=500, Nsim=1000)
summary(res_2)


Try the HSAR package in your browser

Any scripts or data that you put into this service are public.

HSAR documentation built on July 2, 2020, 3:13 a.m.