Developing Spatial Weights for points with panel data

31 Views Asked by At

I have a bit of puzzle that I don't know my way around that I feel might be intuitive to others who have done this work.

I am trying to do some spatial specification tests (Hausman, langrage multiplier) for some spatial panel data (hospital-week data, ideally aggregated into county-month fixed effects). I am trying to use spml for this purpose, and am using inverse distance weighting fixed effects. I have been able to successfully adapt this data to plm but every time I try to do any spatial test I get the following error:

Error in lag.listw(listw, u, zero.policy = zero.policy) : 
  object lengths differ

Note that I have an equivalent number of items in my weight matrix as I do hospitals. I'm not certain whether I'm transforming the data wrong, but see my code below. Is the issue that I'm trying to adapt a weight matrix that has multiple time-series values per "location"?

library(plm)
library(splm)
library(tidyverse)
library(sf)
finalprepostdataset3<-finalprepostdataset2%>%
  group_by(monthyear, County.FIPS) %>%
  mutate(group = row_number())%>%
  ungroup()%>%
  mutate(county_id=paste(County.FIPS,group, sep="."))%>%
  rename(period=monthyear)%>%
  mutate(finalmatch4=case_when(finalmatch2==1~1,
                               finalmatch2==0~0,
                               TRUE~NA))%>%
  select(hospital_pk, county_id, County.FIPS, period, icucrowded,icucrowded2, crowded, crowded2,  finalmatch2,finalmatch3,finalmatch4,minority_majority, vaccineperc, RPL_THEMES, icupercap, bedspercap, geometry)%>%
  mutate(icucrowded=as.numeric(icucrowded),
         icucrowded2=as.numeric(icucrowded2),
         crowded=as.numeric(crowded),
         crowded2=as.numeric(crowded2),
         finalmatch2=as.numeric(finalmatch2),
         finalmatch3=as.numeric(finalmatch3),
         finalmatch4=as.numeric(finalmatch4))%>%
  select(county_id,period, everything())%>%
  filter(!if_any(everything(),is.na))
 

#weighting
coords<-finalprepostdataset3%>%
  distinct(geometry, .keep_all = TRUE) %>%
  st_as_sf(crs=4326)%>%
  st_coordinates()

knn6 <- knn2nb(knearneigh(coords, k =6))
knn6
str(knn6)
wknn_10 <- nb2listw(knn6, style="W")

dist_idw <- nbdists(knn6, coords, longlat = TRUE)
dist_idw[1]
ids <- lapply(dist_idw, function(x) 1/(x))
length(ids)
ids[1]
invd1a <- lapply(dist_idw, function(x) (1/(x/100)))
invd1a[1]
invd.weights <- nb2listw(knn6,glist = invd1a,style = "B", zero.policy=TRUE)
summary(invd.weights)
invd.weights$weights[1]
plot(invd.weights, coords, lwd=.2, col="blue", cex = .5)

##panel data creation
finalprepostdataset3<-finalprepostdataset3%>%
  select(-c(geometry))

formula<-icucrowded2~finalmatch4*minority_majority+ minority_majority+finalmatch4 + vaccineperc+ icupercap

pdata <- plm::pdata.frame(finalprepostdataset3, index = c("county_id", "period"))
pdata<-make.pbalanced(pdata, balance.type = "shared.individuals")

sphtest(formula,data=pdata,listw =invd.weights,index = c("county_id", "period"), spatial.model = "error", method="ML")

Error in lag.listw(listw, u, zero.policy = zero.policy) : 
  object lengths differ
0

There are 0 best solutions below