Skip to content

Commit

Permalink
more zero.policy adapations
Browse files Browse the repository at this point in the history
  • Loading branch information
rsbivand committed Nov 7, 2023
1 parent 469f402 commit f3a85e6
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 6 deletions.
10 changes: 7 additions & 3 deletions R/listw2sn.R
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ listw2sn <- function(listw) {
res
}

sn2listw <- function(sn, style=NULL, zero.policy=NULL) {
sn2listw <- function(sn, style=NULL, zero.policy=NULL, from_mat2listw=FALSE) {
if(!inherits(sn, "spatial.neighbour"))
stop("not a spatial.neighbour object")
if (is.null(zero.policy))
Expand Down Expand Up @@ -66,8 +66,12 @@ sn2listw <- function(sn, style=NULL, zero.policy=NULL) {
res <- list(style=style, neighbours=nlist, weights=vlist)
class(res) <- c("listw", "nb")
if (any(card(res$neighbours) == 0L)) {
if (!zero.policy) {
stop("no-neighbour observations found, set zero.policy to TRUE")
if (!from_mat2listw) {
if (!zero.policy) {
stop("no-neighbour observations found, set zero.policy to TRUE")
}
} else {
warning("no-neighbour observations found, set zero.policy to TRUE;\nthis warning will soon become an error")
}
}
if (!(is.null(attr(sn, "GeoDa"))))
Expand Down
5 changes: 3 additions & 2 deletions R/nb2mat.R
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,8 @@ mat2listw <- function(x, row.names=NULL, style=NULL, zero.policy=NULL) {
class(df) <- c(class(df), "spatial.neighbour")
attr(df, "region.id") <- row.names
attr(df, "n") <- dim(xC)[1]
res <- sn2listw(df, style=style, zero.policy=zero.policy)
res <- sn2listw(df, style=style, zero.policy=zero.policy,
from_mat2listw=TRUE)
neighbours <- res$neighbours
weights <- res$weights
} else {
Expand All @@ -96,7 +97,7 @@ mat2listw <- function(x, row.names=NULL, style=NULL, zero.policy=NULL) {
verbose=FALSE, force=TRUE)
if (any(card(neighbours) == 0L)) {
if (!zero.policy) {
stop("no-neighbour observations found, set zero.policy to TRUE")
warning("no-neighbour observations found, set zero.policy to TRUE;\nthis warning will soon become an error")
}
}
res <- list(style=style, neighbours=neighbours, weights=weights)
Expand Down
3 changes: 2 additions & 1 deletion man/listw2sn.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,14 @@ The function makes a \code{"spatial neighbour"} object representation
\code{"listw"} spatial weights object. \code{sn2listw()} is the inverse function to \code{listw2sn()}, creating a \code{"listw"} object from a \code{"spatial neighbour"} object. }
\usage{
listw2sn(listw)
sn2listw(sn, style = NULL, zero.policy = NULL)
sn2listw(sn, style = NULL, zero.policy = NULL, from_mat2listw=FALSE)
}
\arguments{
\item{listw}{a \code{listw} object from for example \code{nb2listw}}
\item{sn}{a \code{spatial.neighbour} object}
\item{style}{default NULL, missing, set to "M" and warning given; if not "M", passed to \code{\link{nb2listw}} to re-build the object}
\item{zero.policy}{default NULL, use global option value; if FALSE stop with error for any empty neighbour sets, if TRUE permit the weights list to be formed with zero-length weights vectors}
\item{from_mat2listw}{default FALSE, set TRUE if called from \code{mat2listw}}
}

\value{
Expand Down

0 comments on commit f3a85e6

Please sign in to comment.