[R-sig-Geo] Question on symmetric for nb2mat
Andrea Gilardi
@@g||@rd|5 @end|ng |rom c@mpu@@un|m|b@|t
Wed Jul 29 21:38:38 CEST 2020
Dear Prof. Bivand, thank you very much for the super quick reply and fix.
The approach using *spatialreg* and as(x, "CsparseMatrix") works perfectly.
This step makes little sense - why is it included?
>
I included the Cholesky decomposition since I found this problem while I
was estimating the Cholesky decomposition of a precision matrix in a
multivariate CAR model (as you probably guessed) and the function *chol *was
complaining that D - alpha * W was not symmetric (where D is a diagonal
matrix with the total number of neighbours for each spatial unit and alpha
is the parameter controlling the strength in the proper CAR model). D is
clearly symmetric by construction so the problem was with W and I couldn't
understand why. I did not include all the details about how or why I found
the error because I think they were not important but, unfortunately, the
example with *chol(W)* is also meaningless.
Anyway thank you very much for the solution and the other suggestions.
Kind regards
Andrea
Il giorno mer 29 lug 2020 alle ore 17:51 Roger Bivand <Roger.Bivand using nhh.no>
ha scritto:
> 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
>
[[alternative HTML version deleted]]
More information about the R-sig-Geo
mailing list