Denys D.
Denys D.

Reputation: 25

How to solve "impacts()" neighbors length error after running spdep::lagsarlm (Spatial Autoregressive Regression model)?

I have 9,150 polygons in my dataset. I was trying to run a spatial autoregressive model (SAR) in spdep to test spatial dependence of my outcome variable. After running the model, I wanted to examine the direct/indirect impacts, but encountered an error that seems to have something to do with the length of neighbors in the weights matrix not being equal to n.

I tried running the very same equation as SLX model (Spatial Lag X), and impacts() worked fine, even though there were some polygons in my set that had no neighbors. I Googled and looked at spdep documentation, but couldn't find a clue on how to solve this error.

# Defining queen contiguity neighbors for polyset and storing the matrix as list
q.nbrs <- poly2nb(polyset) 
listweights <- nb2listw(q.nbrs, zero.policy = TRUE)

# Defining the model
model.equation <- TIME ~ A + B + C

# Run SAR model
reg <- lagsarlm(model.equation, data = polyset, listw = listweights, zero.policy = TRUE)

# Run impacts() to show direct/indirect impacts
impacts(reg, listw = listweights, zero.policy = TRUE)

Error in intImpacts(rho = rho, beta = beta, P = P, n = n, mu = mu, Sigma = Sigma,  : 
  length(listweights$neighbours) == n is not TRUE

Upvotes: 0

Views: 496

Answers (1)

Perla Rivadeneyra
Perla Rivadeneyra

Reputation: 11

I know that this is a question from 2019, but maybe it can help people dealing with the same problem. I found out that in my case the problem was the type of dataset, your data=polyset should be of type "SpatialPolygonsDataFrame". Which can be achieved by converting your data:

polyset_spatial_sf <- sf::as_Spatial(polyset, IDs = polyset$ID)

Then rerun your code.

Upvotes: 0

Related Questions