I am estimating a spatial panel model for a dataset that runs for 3100 US counties for 4 periods (1995, 2000, 2010, 2015). Please see the code below. I get the following error while running regression:
Error in lag.listw(listw, xt, ...) : object lengths differ
########################################
setwd("C:/Users/ritik/Dropbox/IrrigationEfficiency/Data_Irrigation")
#Import data
data_panel <- read.csv("Irrigation_LL.csv")
# Import shapefile as a "SpatialPolygonDataFrame"
usshp <-readOGR(dsn="C:/Users/ritik/Dropbox/IrrigationEfficiency/Data_Irrigation/tl_2016_us_county", layer="ContiguousUS")
#coordinates
coords <-coordinates(usshp)
#Creating weighting matrix W through
contnbQueen <- poly2nb(usshp, queen = TRUE)
contnbk4 <- knn2nb(knearneigh(coords, k = 4))
#Choosing the matrix to apply:
W <- nb2listw(contnbk4, glist = NULL, style = "W")
#Converting data frame into p.dataframe
dfSPX <- pdata.frame(data_panel, index = c("panelid","year"))
#Creating specific pdata.frame spatial vectors:
dfdir<-dfSPX$direff
dfadj<-dfSPX$adjeff
dfdr0<-dfSPX$dr0
dfdr1<-dfSPX$dr1
dfdr2<-dfSPX$dr2
dfdr3<-dfSPX$dr3
dfdr3<-dfSPX$dr4
#attach data
attach(dfSPX)
#Specifying a formula for these models:
dirSP <- irwfrto ~ dir + dr0 + dr1+ dr2 + dr3+ dr4 + slag(dfdir, listw = W) + slag(dfdr0, listw = W) + slag(dfdr1, listw = W) + slag(dfdr2, listw = W) + slag(dfdr3, listw = W) + slag(dfdr3, listw = W) + slag(dfdr4, listw = W)
irSP <- irwfrto ~ dir + lpop +lirri+ lfarminc +lfarmval+ lhire +lgovtusd +lexpusd+ dr0 + dr1+ dr2 + dr3+ dr4 + slag(dfdir, listw = W) + slag(dfdr0, listw = W) + slag(dfdr1, listw = W) + slag(dfdr2, listw = W) + slag(dfdr3, listw = W) + slag(dfdr3, listw = W) + slag(dfdr4, listw = W)
#Applying plm and splm functions on the original data (data.table data.frame object):
irSLX<-plm(irSP,data=dfSPX,listw=W,index=c("panelid","year"),model="within",effect="twoways",spatial.error="none",lag=F)
irSARX<-spml(dirSP,data=dfSPX,listw=W,index=c("panelid","year"),model="within",effect="twoways",spatial.error="none",lag=T)
irSEMX<-spml(dirSP,data=dfSPX,listw=W,index=c("panelid","year"),model="within",effect="twoways",spatial.error="b",lag=F)
Can someone please tell me how to fix the regression ?
Is it because the time period is at 5 year gap?