I am making spatial panel models from the splm library but I cannot make a fixed or random effects model because it comes out that it is an unbalanced panel.
Data
Reproducible example
library(dplyr)
library(Hmisc)
library(sf)
library(plm)
library(splm)
library(spdep)
data = read.csv("data.csv", colClasses=c(id="factor"))
map = st_read("nxparroquias.shp")
colnames(map)[1] = "id"
balanced = data %>%
group_by(id) %>%
#drop_na() %>%
filter(all(c(2013:2018) %in% year)) %>%
ungroup() %>%
filter(id %in% unique(map$id))
map = map %>%
filter(id %in% unique(balanced$id))
pt = spdep::poly2nb(map)
ptw = spdep::nb2listw(pt, zero.policy = TRUE)
describe(balanced$id)
#balanced$id
#n missing distinct
#5832 0 972
describe(map$id)
#map$id
#n missing distinct
#972 0 972
#checks if the data are balanced
plm::is.pbalanced(balanced$id,balanced$year)
#[1] TRUE
p_balanced = pdata.frame(balanced, index = c("id", "year")) #same issues
Pooling
md1 = splm::spml(data = balanced,
y ~ x1+x2+x3,
index = c("id", "year"),
zero.policy = TRUE,
model = "pooling",
listw = ptw)
summary(md1)
#ML panel with , spatial error correlation
#
#Call:
# spreml(formula = formula, data = data, index = index, w = listw2mat(listw),
# w2 = listw2mat(listw2), lag = lag, errors = errors, cl = cl,
# zero.policy = TRUE)
#
#Residuals:
# Min. 1st Qu. Median Mean 3rd Qu. Max.
#-14.860 -3.213 0.539 0.003 3.194 13.181
#
#Error variance parameters:
# Estimate Std. Error t-value Pr(>|t|)
#rho 0.087841 0.019622 4.4767 7.58e-06 ***
#
# Coefficients:
# Estimate Std. Error t-value Pr(>|t|)
#(Intercept) 5.1373431 0.0972298 52.8371 < 2.2e-16 ***
# x1 0.0423977 0.0098276 4.3141 1.602e-05 ***
# x2 0.1109780 0.0083800 13.2432 < 2.2e-16 ***
# x3 -0.0444174 0.0096522 -4.6018 4.189e-06 ***
# ---
# Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
FE
md2 = splm::spml(data = balanced,
y ~ x1+x2+x3,
index = c("id", "year"),
zero.policy = TRUE,
model = "within",
listw = ptw)
With reproducible example Fixed effects
Error in lag.listw(listw, u, zero.policy = zero.policy) : object lengths differ
With my original data
Error in spfeml(formula = formula, data = data, index = index, listw = listw, : Estimation method unavailable for unbalanced panels
RE
with random effects
Error in .C64("aplsb1", SIGNATURE = c(SS$signature, SS$signature, "double", : NAs in argument 7 and 'NAOK = FALSE' (dotCall64)