Error in impacts() after lagsarlm (spdep package)

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Error in impacts() after lagsarlm (spdep package)

R-sig-geo mailing list
I posted this question on StackOverflow a while back but received no answer. 

I have 9,150 polygons in my data set. 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. 

> # 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

What am I doing wrong? I am running Windows 10 machine with R 3.5.3 with the most up-to-date spdep package, if it helps.

Thank you very much.

Regards,
Denys Dukhovnov

_______________________________________________
R-sig-Geo mailing list
[hidden email]
https://stat.ethz.ch/mailman/listinfo/r-sig-geo
Reply | Threaded
Open this post in threaded view
|

Re: Error in impacts() after lagsarlm (spdep package)

Roger Bivand
Administrator
On Wed, 13 Nov 2019, Denys Dukhovnov via R-sig-Geo wrote:

> I posted this question on StackOverflow a while back but received no answer. 

I maintain the spatialreg package to which I think you are referring. I
never visit SO, both to save time and because the signal/noise ratio there
is not encouraging (with exceptions where reproducible examples are used).

While your post was pain text (thanks), what you copied from SO got rather
mangled. The spdep package does warn you that the functions you are using
are deprecated and are actually to be called from spatialreg.

>
> I have 9,150 polygons in my data set. 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.
>
>> # 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)
You are completely unnecessarily using the "eigen" method. If your weights
are symmetric, use Cholesky decomposition ("Matrix"), much faster, same
output.

>
>> # 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
>

Never, ever, do this. Did you read LeSage & Pace? 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.

If you provide a reproducible example using built-in data, I can try to
provide a more informative error message.

> What am I doing wrong? I am running Windows 10 machine with R 3.5.3 with
> the most up-to-date spdep package, if it helps.
>

R is at 3.6.1.

Hope this clarifies,

Roger

> Thank you very much.
>
> Regards,
> Denys Dukhovnov
>
> _______________________________________________
> R-sig-Geo mailing list
> [hidden email]
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>
--
Roger Bivand
Department of Economics, Norwegian School of Economics,
Helleveien 30, N-5045 Bergen, Norway.
voice: +47 55 95 93 55; e-mail: [hidden email]
https://orcid.org/0000-0003-2392-6140
https://scholar.google.no/citations?user=AWeghB0AAAAJ&hl=en
_______________________________________________
R-sig-Geo mailing list
[hidden email]
https://stat.ethz.ch/mailman/listinfo/r-sig-geo
Roger Bivand
Department of Economics
Norwegian School of Economics
Helleveien 30
N-5045 Bergen, Norway