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)
## Linking to GEOS 3.12.1, GDAL 3.8.4, PROJ 9.3.1; sf_use_s2() is TRUE
library(spdep)
## Loading required package: spData
library(tidyverse)
## ── Attaching core tidyverse packages ─────────────────────────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr 1.1.4 ✔ readr 2.1.5
## ✔ forcats 1.0.0 ✔ stringr 1.5.1
## ✔ ggplot2 3.5.1 ✔ tibble 3.2.1
## ✔ lubridate 1.9.3 ✔ tidyr 1.3.1
## ✔ purrr 1.0.2
## ── Conflicts ───────────────────────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag() masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
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).
names(depmunic)
## [1] "num_dep" "airbnb" "museums" "population" "pop_rest" "greensp" "area"
## [8] "geometry"
depmunic$pop_rest
## [1] 8202 5009 2735 4167 5099 16531 8017
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).
names(properties)
## [1] "id" "size" "price" "prpsqm" "age" "dist_metro" "geometry"
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
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.
to have a closer look at M , we can visualize it
plot(st_geometry(depmunic),border = 'grey')
plot(st_centroid(depmunic), add = TRUE)
## Warning: st_centroid assumes attributes are constant over geometries
## Warning in plot.sf(st_centroid(depmunic), add = TRUE): ignoring all but the first attribute
plot(nb.list, st_centroid(depmunic), add = TRUE)
## Warning: st_centroid assumes attributes are constant over geometries
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
For the W matrix, we can check the neighbours statistics
nb.1300
## Neighbour list object:
## Number of regions: 1000
## Number of nonzero links: 170254
## Percentage nonzero weights: 17.0254
## Average number of links: 170.254
Run the models
So, having ready the matrices Delta, M and W, we wun the
hsar()
function
res.formula <- prpsqm ~ size + age + greensp + population + museums + airbnb
res <- hsar(res.formula,data=model.data,W=W,M=M,Delta=Delta,
burnin=500, Nsim=1000)
## Warning in spdep::mat2listw(W): style is M (missing); style should be set to a valid value
## Warning in sn2listw(df, style = style, zero.policy = zero.policy, from_mat2listw = TRUE): style is
## M (missing); style should be set to a valid value
## Warning in spdep::mat2listw(W): style is M (missing); style should be set to a valid value
## Warning in sn2listw(df, style = style, zero.policy = zero.policy, from_mat2listw = TRUE): style is
## M (missing); style should be set to a valid value
summary(res)
##
## Call:
## hsar(formula = res.formula, data = model.data, W = W, M = M,
## Delta = Delta, burnin = 500, Nsim = 1000)
## Type: hsar
##
## Coefficients:
## Mean SD
## (Intercept) 1.880878e+03 9.9085321261
## size 4.308784e+00 0.4857075117
## age -1.992973e+01 1.3218947297
## greensp 7.638049e-04 0.0006136726
## population -9.642519e-03 0.0022135920
## museums -4.488692e+01 9.5710324827
## airbnb 5.575054e-01 0.1645700213
##
## Spatial Coefficients:
## rho lambda
## [1,] 0.188356 0.14771
##
## Diagnostics
## Deviance information criterion (DIC): 28196.61
## Effective number of parameters (pd): -1.524159
## Log likelihood: -14099.83
## Pseudo R squared: 0.3588787
##
## Impacts:
## direct indirect total
## (Intercept) 1.881439e+03 4.358260e+02 2.317264e+03
## size 4.310068e+00 9.984060e-01 5.308474e+00
## age -1.993567e+01 -4.618001e+00 -2.455368e+01
## greensp 7.640326e-04 1.769844e-04 9.410169e-04
## population -9.645393e-03 -2.234308e-03 -1.187970e-02
## museums -4.490030e+01 -1.040093e+01 -5.530124e+01
## airbnb 5.576716e-01 1.291819e-01 6.868535e-01
##
## Quantiles:
## 5% 25% 50% 75% 95%
## (Intercept) 1.865108e+03 1.873915e+03 1.880482e+03 1.887116e+03 1.899108e+03
## size 3.474374e+00 3.985585e+00 4.313592e+00 4.660815e+00 5.054106e+00
## age -2.199631e+01 -2.079239e+01 -1.997034e+01 -1.906072e+01 -1.767202e+01
## greensp -2.965393e-04 3.974262e-04 7.551037e-04 1.132355e-03 1.722759e-03
## population -1.320664e-02 -1.116246e-02 -9.764824e-03 -8.101470e-03 -5.738580e-03
## museums -6.031927e+01 -5.151993e+01 -4.444317e+01 -3.817029e+01 -3.079498e+01
## airbnb 2.722792e-01 4.595209e-01 5.553124e-01 6.682065e-01 8.220889e-01
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)
## Warning in spdep::mat2listw(W): style is M (missing); style should be set to a valid value
## Warning in sn2listw(df, style = style, zero.policy = zero.policy, from_mat2listw = TRUE): style is
## M (missing); style should be set to a valid value
summary(res_1)
##
## Call:
## hsar(formula = res.formula, data = model.data, W = NULL, M = M,
## Delta = Delta, burnin = 500, Nsim = 1000)
## Type: hsar with rho = 0
##
## Coefficients:
## Mean SD
## (Intercept) 1.881134e+03 1.002537e+01
## size 4.315024e+00 4.218945e-01
## age -2.002619e+01 1.287527e+00
## greensp 7.226078e-04 5.564728e-04
## population -7.121008e-03 1.120733e-03
## museums -4.585389e+01 1.025743e+01
## airbnb 6.433655e-01 1.604121e-01
##
## Spatial Coefficients:
## lambda
## -0.026046
##
## Diagnostics
## Deviance information criterion (DIC): 28189.43
## Effective number of parameters (pd): -1.792601
## Log likelihood: -14096.51
## Pseudo R squared: 0.3578193
##
## Quantiles:
## 5% 25% 50% 75% 95%
## (Intercept) 1.864436e+03 1.874832e+03 1.881650e+03 1.887990e+03 1.896975e+03
## size 3.644688e+00 4.041712e+00 4.329345e+00 4.602022e+00 5.023365e+00
## age -2.219428e+01 -2.086044e+01 -2.001777e+01 -1.921052e+01 -1.781857e+01
## greensp -5.217643e-05 3.655865e-04 6.542170e-04 1.009572e-03 1.786107e-03
## population -8.760368e-03 -7.847762e-03 -7.202664e-03 -6.476834e-03 -5.157352e-03
## museums -6.156313e+01 -5.381063e+01 -4.567536e+01 -3.870049e+01 -2.860034e+01
## airbnb 3.738831e-01 5.369197e-01 6.492991e-01 7.430972e-01 9.062610e-01
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)
## Warning in spdep::mat2listw(W): style is M (missing); style should be set to a valid value
## Warning in sn2listw(df, style = style, zero.policy = zero.policy, from_mat2listw = TRUE): style is
## M (missing); style should be set to a valid value
summary(res_2)
##
## Call:
## hsar(formula = res.formula, data = model.data, W = W, M = NULL,
## Delta = Delta, burnin = 500, Nsim = 1000)
## Type: hsar with lambda = 0
##
## Coefficients:
## Mean SD
## (Intercept) 1.881433e+03 9.7787251890
## size 4.320724e+00 0.5145981502
## age -2.006309e+01 1.3671298243
## greensp 9.191971e-04 0.0006723134
## population -9.097623e-03 0.0022338151
## museums -4.564746e+01 9.6967267461
## airbnb 5.445758e-01 0.2287821477
##
## Spatial Coefficients:
## rho
## 0.173088
##
## Diagnostics
## Deviance information criterion (DIC): 28198.6
## Effective number of parameters (pd): -1.704043
## Log likelihood: -14101
## Pseudo R squared: 0.3623617
##
## Quantiles:
## 5% 25% 50% 75% 95%
## (Intercept) 1.865152e+03 1.875127e+03 1.881216e+03 1.887290e+03 1.897579e+03
## size 3.458822e+00 3.993053e+00 4.325490e+00 4.657485e+00 5.126591e+00
## age -2.231385e+01 -2.093929e+01 -2.011275e+01 -1.917424e+01 -1.780022e+01
## greensp -1.478657e-04 4.673335e-04 9.304327e-04 1.372135e-03 1.963982e-03
## population -1.280245e-02 -1.064503e-02 -9.044582e-03 -7.595252e-03 -5.335030e-03
## museums -6.121924e+01 -5.253706e+01 -4.566362e+01 -3.913182e+01 -2.949665e+01
## airbnb 1.861284e-01 3.930978e-01 5.466702e-01 6.996989e-01 9.133508e-01