I have a dataset that has multiple observations per county, and I am interested in estimating the effect of a county-level variable on individual-level outcomes. I would like to account for adjacency between counties in this model. I am using errorsarlm() from package spdep. It seems errorsarlm() will only accept data frames that have the same number of rows as the length of the listw object. Since the listw object is the length of the number of unique counties, and the data frame has multiple observations per county, I receive the following error:
Spatial autoregressive error model Error in errorsarlm(ss_form, data = temp, listw = w, method = "eigen", : Input data and neighbourhood list have different dimensions
Can someone please help me figure out a way to fit this model?
I have provided example code below.
#Get spatial polygons data frame from rgeos spdf <- getData('GADM',country='USA',level=2) #Create unique county IDs that will match SSA ID's spdf$State_County <- paste(toupper(spdf$NAME_1),toupper(spdf$NAME_2),sep='') #Create neighborhood object nb <- poly2nb(spdf, row.names = "State_County") #Create listw object w <- nb2listw(nb, style = "B") #Estimate model with multiple observations per county (this is just a mockup of the model I am fitting, it will not actually run) model1 <- errorsarlm(injury_outcome ~ county_ambulance_use + age , data=trauma, listw=w, method="eigen", quiet=FALSE)