I'm trying to run a Spatial Durbin Model for a spatial panel data. My idea is to run the dependent variable y
over my independent variables x1
and x2
. I've tried so many options but all of them give me errors. Here are the steps I'm following for the first 2 options I tried:
library("sf")
library("dplyr")
library("spdep")
library("spatialreg")
library("spmodel")
library(plm)
library(GWPR.light)
library(sp)
library(GWmodel)
library(spgwr)
library(SDPDmod)
#Deleting the observations that don't have information for my regression variables y, x1, and x2:
test <- DataTest_shp %>% dplyr::filter(across(c(y , x1,x2),~ !is.na(.)))
#Creating weight matrix:
shapefile_centroid <- sf::st_centroid(test)
coordsW <- test %>% st_centroid()%>% st_geometry()
nb.d125<- dnearneigh(coordsW,0,125000,row.names=coordsW$ID)
mat.d125 <-nb2mat(nb.d125,glist=NULL,style="W",zero.policy=TRUE)
listd125 = mat2listw(mat.d125, style="W")
#Running the regression:
formula.GWPR <- y ~ x1+x2
#1st Option for running the regression:
res3<-blmpSDPD(formula.GWPR,
data = test,
W = mat.d125,
index = c("ID","year"),
model = list("sdm"),
effect = "twoways",
prior = "beta")
Error for 1st option:
Error in blmpSDPD(formula.GWPR, data = test, W = mat.d125, index = c("ID", :
Non conformable spatial weights
#2nd Option for running the regression:
mod5<-SDPDm(formula.GWPR, data = test,
W = mat.d125,
index = c("ID","year"),
model = "sdm",
effect = "twoways",
LYtrans = T,
dynamic = T,
tlaginfo = list(ind = NULL, tl = T, stl = T))
Error for 2nd option:
Error in xtfrm.data.frame(x) : cannot xtfrm data frames
Many thanks in advance!
Please find the data available here
I realized that my mistake was having a different number of observations in my data and in my weights matrix. I solve this by keeping the same number of observations for the dataset as the number present in the shapefile.