0

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
Denys D.
  • 11
  • 3

1 Answers1

0

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.

David Buck
  • 3,752
  • 35
  • 31
  • 35
  • Thank you for the answer, but the problem is that my `polyset` is already an `sp` type `SpatialPolygonsDataFrame`. I thought that perhaps there is another solution to the one commonly floating online. I posted on R-sig-geo and got a reply from Roger Bivand. He comments on my `lagsarlm()` "If your weights are symmetric, use Cholesky decomposition ("Matrix"), much faster, same output." Then he adds in response to my `impacts()` line: "Use traces, not the weights themselves. With the `listw` object, you need to invert an nxn matrix once in this case, 1+R times if you run Monte Carlo simulations" – Denys D. May 19 '20 at 16:59