[R-sig-Geo] Question on symmetric for nb2mat

Roger Bivand Roger@B|v@nd @end|ng |rom nhh@no
Wed Jul 29 17:50:59 CEST 2020


On Wed, 29 Jul 2020, Andrea Gilardi wrote:

> Dear all, I think the following message should work. Excuse me for the
> previous message.
>
> library(spdep)
> library(rgdal)
> library(Matrix)
>
> nc_sids <- readOGR(system.file("shapes/sids.shp", package = "spData"))

Do things in steps:

adj <- poly2nb(nc_sids)
is.symmetric.nb(adj, force=TRUE)
# [1] TRUE


> W <- as(nb2mat(adj, style = "B"), "Matrix")

W0 <- nb2mat(adj, style = "B")
all(W0 == t(W0))
# [1] TRUE



> Matrix::isSymmetric(W)
> chol(W)

W <- as(W0, "symmetricMatrix")
isSymmetric(W)
#[1] TRUE
res <- chol(W)
#Error in asMethod(object) : not a positive definite matrix

This step makes little sense - why is it included?

I don't know why coercing to Matrix does not see the matrix as symmetric. 
Or:

lw <- nb2listw(adj, style="B")
library(spatialreg)
W <- as(lw, "CsparseMatrix")
#> isSymmetric(W)
#[1] TRUE

This works and is what you might need:

res <- chol((Diagonal(nrow(W)) - 0.1 * W))

or see ?Matrix::USCounties.

Roger


>
> If it's still not working I added a reprex here:
> https://gist.github.com/agila5/59d3a173df9b1efabe13800f748a2d48

Not useful, not in thread nor do any answers stay in thread.

>
> Kind regards
> Andrea
>
>
> Il giorno mer 29 lug 2020 alle ore 17:19 Roger Bivand <Roger.Bivand using nhh.no>
> ha scritto:
>
>> Please follow-up in this thread posting in plain text (not HTML). As you
>> can see below, HTML garbles your message.
>>
>> On Wed, 29 Jul 2020, Andrea Gilardi wrote:
>>
>>> Dear all, probably I'm missing something obvious but I have a question on
>>> the matrix built using nb2mat with argument style = "B". More precisely,
>> I
>>> don't understand why the following code says that the matrix *W* (which
>>> should be a binary first-order adjacency matrix) is not symmetric:
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>> *library(spdep)library(rgdal)library(Matrix)# datanc.sids <-
>>> readOGR(system.file("shapes/sids.shp", package = "spData"))adj <-
>>> poly2nb(nc.sids)W <- as(nb2mat(adj, style = "B"),
>>> "Matrix")Matrix::isSymmetric(W)#> [1] FALSEchol(W)#> Error in
>>> asMethod(object): not a symmetric matrix; consider forceSymmetric() or
>>> symmpart()*
>>>
>>> Kind regards
>>> Andrea
>>>
>>>       [[alternative HTML version deleted]]
>>>
>>> _______________________________________________
>>> R-sig-Geo mailing list
>>> R-sig-Geo using r-project.org
>>> 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: Roger.Bivand using nhh.no
>> https://orcid.org/0000-0003-2392-6140
>> https://scholar.google.no/citations?user=AWeghB0AAAAJ&hl=en
>>
>

-- 
Roger Bivand
Department of Economics, Norwegian School of Economics,
Helleveien 30, N-5045 Bergen, Norway.
voice: +47 55 95 93 55; e-mail: Roger.Bivand using nhh.no
https://orcid.org/0000-0003-2392-6140
https://scholar.google.no/citations?user=AWeghB0AAAAJ&hl=en



More information about the R-sig-Geo mailing list