Skip to content

Commit 85ed3fb

Browse files
committed
execute defunct
1 parent 2b18f46 commit 85ed3fb

14 files changed

+811
-10
lines changed

DESCRIPTION

+6-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Package: spatialreg
2-
Version: 1.1-9
3-
Date: 2021-09-28
2+
Version: 1.2-1
3+
Date: 2021-11-10
44
Title: Spatial Regression Analysis
55
Encoding: UTF-8
66
Authors@R: c(person("Roger", "Bivand", role = c("cre", "aut"), email = "[email protected]", comment=c(ORCID="0000-0003-2392-6140")),
@@ -15,6 +15,7 @@ Authors@R: c(person("Roger", "Bivand", role = c("cre", "aut"), email = "Roger.Bi
1515
person("Rein", "Halbersma", role = "ctb"),
1616
person("James", "LeSage", role = "ctb"),
1717
person("Angela", "Li", role = "ctb"),
18+
person("Hongfei", "Li", role = "ctb"),
1819
person("Jielai", "Ma", role = "ctb"),
1920
person("Abhirup", "Mallik", role = c("ctb", "trl")),
2021
person("Giovanni", "Millo", role = "ctb"),
@@ -24,11 +25,11 @@ Authors@R: c(person("Roger", "Bivand", role = c("cre", "aut"), email = "Roger.Bi
2425
person(given = "Mauricio", family = "Sarrias", role = c("ctb"), email = "[email protected]"),
2526
person(given = "JuanTomas", family = "Sayago", role = c("ctb"), email = "[email protected]"),
2627
person("Michael", "Tiefelsdorf", role = "ctb"))
27-
Depends: R (>= 3.3.0), spData, Matrix
28+
Depends: R (>= 3.3.0), spData, Matrix, sf
2829
Imports: spdep, expm, coda, methods, MASS, boot, splines, LearnBayes,
2930
nlme, gmodels
30-
Suggests: parallel, RSpectra, sf, tmap, foreign, spam, knitr, lmtest,
31-
sandwich, rmarkdown
31+
Suggests: parallel, RSpectra, tmap, foreign, spam, knitr, lmtest,
32+
sandwich, rmarkdown, igraph
3233
Description: A collection of all the estimation functions for spatial cross-sectional models (on lattice/areal data using spatial weights matrices) contained up to now in 'spdep', 'sphet' and 'spse'. These model fitting functions include maximum likelihood methods for cross-sectional models proposed by 'Cliff' and 'Ord' (1973, ISBN:0850860369) and (1981, ISBN:0850860814), fitting methods initially described by 'Ord' (1975) <doi:10.1080/01621459.1975.10480272>. The models are further described by 'Anselin' (1988) <doi:10.1007/978-94-015-7799-1>. Spatial two stage least squares and spatial general method of moment models initially proposed by 'Kelejian' and 'Prucha' (1998) <doi:10.1023/A:1007707430416> and (1999) <doi:10.1111/1468-2354.00027> are provided. Impact methods and MCMC fitting methods proposed by 'LeSage' and 'Pace' (2009) <doi:10.1201/9781420064254> are implemented for the family of cross-sectional spatial regression models. Methods for fitting the log determinant term in maximum likelihood and MCMC fitting are compared by 'Bivand et al.' (2013) <doi:10.1111/gean.12008>, and model fitting methods by 'Bivand' and 'Piras' (2015) <doi:10.18637/jss.v063.i18>; both of these articles include extensive lists of references. 'spatialreg' >= 1.1-* correspond to 'spdep' >= 1.1-1, in which the model fitting functions are deprecated and pass through to 'spatialreg', but will mask those in 'spatialreg'. From versions 1.2-*, the functions will be made defunct in 'spdep'.
3334
License: GPL-2
3435
URL: https://github.com/r-spatial/spatialreg/, https://r-spatial.github.io/spatialreg/

NAMESPACE

+5-2
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,11 @@ importFrom("stats", "lm.fit", "as.formula", "coef", "lm", "model.extract",
1515

1616
importFrom("spdep", "is.symmetric.glist", "lag.listw", "card",
1717
"nb2listw", "n.comp.nb", "listw2U", "listw2mat", "Szero", "listw2sn",
18-
"invIrW", "mat2listw", "is.symmetric.nb", "chkIDs", "get.spChkOption",
18+
"mat2listw", "is.symmetric.nb", "chkIDs", "get.spChkOption",
1919
"subset.listw")
2020

2121
import(Matrix, except=c("expm"))
22+
import(sf)
2223
importFrom(expm, expAtv, expm)
2324
importFrom(coda, as.mcmc, HPDinterval)
2425
importFrom(MASS, mvrnorm)
@@ -150,7 +151,7 @@ exportMethods(coerce)
150151
export(as_dgRMatrix_listw, as_dsTMatrix_listw, as_dsCMatrix_I,
151152
as_dsCMatrix_IrW, Jacobian_W, listw2U_Matrix)
152153

153-
export(powerWeights)
154+
export(powerWeights, invIrW, invIrM)
154155

155156
export(as.spam.listw, listw2U_spam)
156157

@@ -175,4 +176,6 @@ export(set.mcOption, get.mcOption, set.coresOption, get.coresOption,
175176
export(set.VerboseOption, get.VerboseOption, set.ZeroPolicyOption,
176177
get.ZeroPolicyOption)
177178

179+
export(aple.plot, localAple, aple, aple.mc)
180+
178181

NEWS.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1-
# Version 1.1-9 (development)
1+
# Version 1.2-1 (development)
22

33
* Add Fortran character handling USE_FC_LEN_T WRE §6.6.1
44

5+
* Add **spdep** split-out functionality
6+
57
# Version 1.1-8 (2021-05-03)
68

79
* #18 standardize use of `coef()` methods for (some) fitted model summary objects

R/AAA.R

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ assign("verbose", spdep::get.VerboseOption(), envir = .spatialregOptions)
88
assign("mc", spdep::get.mcOption(), envir = .spatialregOptions)
99
assign("cores", spdep::get.coresOption(), envir = .spatialregOptions)
1010
assign("cluster", spdep::get.ClusterOption(), envir = .spatialregOptions)
11-
#assign("rlecuyerSeed", rep(12345, 6), envir = .spatialregOptions)
11+
assign("rlecuyerSeed", rep(12345, 6), envir = .spatialregOptions)
1212
#assign("listw_is_CsparseMatrix", FALSE, envir = .spatialregOptions)
1313

1414
setOldClass(c("listw"))

R/aple.R

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
preAple <- function(x, listw, override_similarity_check=FALSE, useTrace=TRUE) {
2+
stopifnot(isTRUE(all.equal(mean(x), 0.0)))
3+
stopifnot(is.vector(x))
4+
# if (!requireNamespace("spatialreg", quietly=TRUE))
5+
# stop("install the spatialreg package")
6+
W <- as(listw, "CsparseMatrix")
7+
n <- dim(W)[1]
8+
if (useTrace) {
9+
trWW <- sum(diag(W %*% W))
10+
} else {
11+
if (listw$style %in% c("W", "S") && !override_similarity_check) {
12+
can.sim <- can.be.simmed(listw)
13+
eig <- eigenw(similar.listw(listw))
14+
} else {
15+
can.sim <- FALSE
16+
eig <- eigenw(listw)
17+
}
18+
if (is.complex(eig)) trWW <- Re(crossprod(eig))
19+
else trWW <- crossprod(eig)
20+
# modified 110414 RSB
21+
# eig <- Re(eig)
22+
# trWW <- crossprod(eig)
23+
}
24+
corterm <- (trWW/n) * Diagonal(n)
25+
corterm <- as(corterm, "CsparseMatrix")
26+
WU <- ((W + t(W))/2)
27+
W2 <- crossprod(W) + corterm
28+
res <- list(W=W, corterm=corterm, W2=W2, WU=WU, n=n)
29+
res
30+
}
31+
32+
inAple <- function(x, pre) {
33+
xwx <- crossprod(x, (pre$WU %*% x))
34+
xwwx <- crossprod(x, (pre$W2 %*% x))
35+
res <- c(as.matrix(xwx/xwwx))
36+
res
37+
}
38+
39+
aple <- function(x, listw, override_similarity_check=FALSE, useTrace=TRUE) {
40+
pre <- preAple(x=x, listw=listw,
41+
override_similarity_check=override_similarity_check, useTrace=useTrace)
42+
res <- inAple(x=x, pre=pre)
43+
res
44+
}
45+
46+

R/aple.mc.R

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
aple.mc <- function(x, listw, nsim, override_similarity_check=FALSE,
2+
useTrace=TRUE) {
3+
aple.boot <- function(var, i, ...) {
4+
var <- var[i]
5+
return(inAple(x=var, ...))
6+
}
7+
pre <- preAple(x=x, listw=listw,
8+
override_similarity_check=override_similarity_check, useTrace=useTrace)
9+
10+
cores <- get.coresOption()
11+
if (is.null(cores)) {
12+
parallel <- "no"
13+
} else {
14+
parallel <- ifelse (get.mcOption(), "multicore", "snow")
15+
}
16+
ncpus <- ifelse(is.null(cores), 1L, cores)
17+
cl <- NULL
18+
if (parallel == "snow") {
19+
cl <- get.ClusterOption()
20+
if (is.null(cl)) {
21+
parallel <- "no"
22+
warning("no cluster in ClusterOption, parallel set to no")
23+
}
24+
}
25+
res <- boot(x, statistic=aple.boot, R=nsim, sim="permutation", pre=pre,
26+
parallel=parallel, ncpus=ncpus, cl=cl)
27+
28+
res
29+
}
30+
31+
boot_wrapper_in <- function(cl, nsim) {
32+
if (requireNamespace("parallel", quietly = TRUE)) {
33+
# require(rlecuyer)
34+
rlseed <- get("rlecuyerSeed", envir = .spatialregOptions)
35+
if (storage.mode(rlseed) != "integer") rlseed <- as.integer(rlseed)
36+
if (length(rlseed) != 6L) rlseed <- rep(12345L, 6)
37+
parallel::clusterSetRNGStream(cl, iseed=rlseed)
38+
parallel::clusterEvalQ(cl, library(spdep))
39+
nnsim <- ceiling(nsim/length(cl))
40+
nnsim
41+
} else {
42+
stop("parallel not available")
43+
}
44+
}
45+
46+
boot_wrapper_out <- function(lres, mcall) {
47+
res <- list()
48+
res$t0 <- lres[[1]]$t0
49+
res$t <- matrix(c(sapply(lres, function(x) x$t)), ncol=1)
50+
res$R <- sum(sapply(lres, function(x) x$R))
51+
res$data <- lres[[1]]$data
52+
res$seed <- c(sapply(lres, function(x) x$seed))
53+
res$statistic <- lres[[1]]$statistic
54+
res$sim <- lres[[1]]$sim
55+
res$call <- mcall
56+
res$stype <- lres[[1]]$stype
57+
res$strata <- lres[[1]]$strata
58+
class(res) <- "boot"
59+
res
60+
}

R/apleplot.R

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
aple.plot <- function(x, listw, override_similarity_check=FALSE, useTrace=TRUE,
2+
do.plot=TRUE, ...) {
3+
pre <- preAple(x=x, listw=listw,
4+
override_similarity_check=override_similarity_check, useTrace=useTrace)
5+
W2e <- eigen(pre$W2)
6+
SQRTW2 <- W2e$vectors %*% (diag(W2e$values^(0.5)) %*% t(W2e$vectors))
7+
X <- drop(SQRTW2 %*% x)
8+
NSQRTW2 <- W2e$vectors %*% (diag(W2e$values^(-0.5)) %*% t(W2e$vectors))
9+
Y <- drop(NSQRTW2 %*% pre$WU %*% x)
10+
if (do.plot) {
11+
plot(X, Y, ...)
12+
}
13+
list(X=X, Y=Y)
14+
}
15+
16+
localAple <- function(x, listw, override_similarity_check=FALSE, useTrace=TRUE) {
17+
aplepl <- aple.plot(x, listw,
18+
override_similarity_check=override_similarity_check,
19+
useTrace=useTrace, do.plot=FALSE)
20+
res <- (length(aplepl$X) * aplepl$Y * aplepl$X) / c(crossprod(aplepl$X))
21+
res
22+
}

R/sparse_mat.R

+45
Original file line numberDiff line numberDiff line change
@@ -114,3 +114,48 @@ powerWeights <- function(W, rho, order=250, X, tol=.Machine$double.eps^(3/5)) {
114114
acc
115115
}
116116

117+
invIrM <- function(neighbours, rho, glist=NULL, style="W", method="solve",
118+
feasible=NULL) {
119+
if(class(neighbours) != "nb") stop("Not a neighbours list")
120+
invIrW(nb2listw(neighbours, glist=glist, style=style), rho=rho,
121+
method=method, feasible=feasible)
122+
}
123+
124+
invIrW <- function(x, rho, method="solve", feasible=NULL) {
125+
if(inherits(x, "listw")) {
126+
n <- length(x$neighbours)
127+
V <- listw2mat(x)
128+
} else if (inherits(x, "Matrix") || inherits(x, "matrix")) {
129+
if (method == "chol" && all(t(x) == x))
130+
stop("No Cholesky method for matrix or sparse matrix object")
131+
n <- dim(x)[1]
132+
V <- x
133+
} else stop("Not a weights list or a Sparse Matrix")
134+
if (is.null(feasible) || (is.logical(feasible) && !feasible)) {
135+
e <- eigen(V, only.values = TRUE)$values
136+
if (is.complex(e)) feasible <- 1/(range(Re(e)))
137+
else feasible <- 1/(range(e))
138+
if (rho <= feasible[1] || rho >= feasible[2])
139+
stop(paste("Rho", rho, "outside feasible range:",
140+
paste(feasible, collapse=":")))
141+
}
142+
if (method == "chol"){
143+
if (x$style %in% c("W", "S") && !(spatialreg::can.be.simmed(x)))
144+
stop("Cholesky method requires symmetric weights")
145+
if (x$style %in% c("B", "C", "U") &&
146+
!(is.symmetric.glist(x$neighbours, x$weights)))
147+
stop("Cholesky method requires symmetric weights")
148+
if (x$style %in% c("W", "S")) {
149+
V <- listw2mat(listw2U(spatialreg::similar.listw(x)))
150+
}
151+
mat <- diag(n) - rho * V
152+
res <- chol2inv(chol(mat))
153+
} else if (method == "solve") {
154+
mat <- diag(n) - rho * V
155+
res <- solve(mat)
156+
} else stop("unknown method")
157+
attr(res, "call") <- match.call()
158+
res
159+
}
160+
161+

man/aple.Rd

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
\name{aple}
2+
\alias{aple}
3+
%- Also NEED an '\alias' for EACH other topic documented here.
4+
\title{Approximate profile-likelihood estimator (APLE)}
5+
\description{
6+
The Approximate profile-likelihood estimator (APLE) of the simultaneous autoregressive model's spatial dependence parameter was introduced in Li et al. (2007). It employs a correction term using the eigenvalues of the spatial weights matrix, and consequently should not be used for large numbers of observations. It also requires that the variable has a mean of zero, and it is assumed that it has been detrended. The spatial weights object is assumed to be row-standardised, that is using default \code{style="W"} in \code{nb2listw}.
7+
}
8+
\usage{
9+
aple(x, listw, override_similarity_check=FALSE, useTrace=TRUE)
10+
}
11+
%- maybe also 'usage' for other objects documented here.
12+
\arguments{
13+
\item{x}{a zero-mean detrended continuous variable}
14+
\item{listw}{a \code{listw} object from for example \code{nb2listw}}
15+
\item{override\_similarity\_check}{default FALSE, if TRUE - typically for row-standardised weights with asymmetric underlying general weights - similarity is not checked}
16+
\item{useTrace}{default TRUE, use trace of sparse matrix \code{W \%*\% W} (Li et al. (2010)), if FALSE, use crossproduct of eigenvalues of \code{W} as in Li et al. (2007)}
17+
}
18+
\details{
19+
This implementation has been checked with Hongfei Li's own implementation using her data; her help was very valuable.
20+
}
21+
\value{
22+
A scalar APLE value.
23+
}
24+
\references{Li, H, Calder, C. A. and Cressie N. A. C. (2007) Beyond Moran's I: testing for spatial dependence based on the spatial autoregressive model. Geographical Analysis 39, 357-375; Li, H, Calder, C. A. and Cressie N. A. C. (2012) One-step estimation of spatial dependence parameters: Properties and extensions of the APLE statistic, Journal of Multivariate Analysis 105, 68-84.}
25+
\author{Roger Bivand \email{[email protected]}}
26+
27+
\seealso{\code{\link{nb2listw}}, \code{\link{aple.mc}}, \code{\link{aple.plot}}
28+
}
29+
\examples{
30+
wheat <- st_read(system.file("shapes/wheat.shp", package="spData")[1], quiet=TRUE)
31+
library(spdep)
32+
nbr1 <- poly2nb(wheat, queen=FALSE)
33+
nbrl <- nblag(nbr1, 2)
34+
nbr12 <- nblag_cumul(nbrl)
35+
cms0 <- with(as.data.frame(wheat), tapply(yield, c, median))
36+
cms1 <- c(model.matrix(~ factor(c) -1, data=wheat) \%*\% cms0)
37+
wheat$yield_detrend <- wheat$yield - cms1
38+
isTRUE(all.equal(c(with(as.data.frame(wheat),
39+
tapply(yield_detrend, c, median))), rep(0.0, 25),
40+
check.attributes=FALSE))
41+
moran.test(wheat$yield_detrend, nb2listw(nbr12, style="W"))
42+
aple(as.vector(scale(wheat$yield_detrend, scale=FALSE)), nb2listw(nbr12, style="W"))
43+
\dontrun{
44+
errorsarlm(yield_detrend ~ 1, wheat, nb2listw(nbr12, style="W"))
45+
}
46+
}
47+
% Add one or more standard keywords, see file 'KEYWORDS' in the
48+
% R documentation directory.
49+
\keyword{spatial}
50+

man/aple.mc.Rd

+72
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
\name{aple.mc}
2+
\alias{aple.mc}
3+
%- Also NEED an '\alias' for EACH other topic documented here.
4+
\title{Approximate profile-likelihood estimator (APLE) permutation test}
5+
\description{
6+
A permutation bootstrap test for the approximate profile-likelihood estimator (APLE).
7+
}
8+
\usage{
9+
aple.mc(x, listw, nsim, override_similarity_check=FALSE, useTrace=TRUE)
10+
}
11+
%- maybe also 'usage' for other objects documented here.
12+
\arguments{
13+
\item{x}{a zero-mean detrended continuous variable}
14+
\item{listw}{a \code{listw} object from for example \code{nb2listw}}
15+
\item{nsim}{number of simulations}
16+
\item{override\_similarity\_check}{default FALSE, if TRUE - typically for row-standardised weights with asymmetric underlying general weights - similarity is not checked}
17+
\item{useTrace}{default TRUE, use trace of sparse matrix \code{W \%*\% W} (Li et al. (2010)), if FALSE, use crossproduct of eigenvalues of \code{W} as in Li et al. (2007)}
18+
}
19+
20+
\value{
21+
A \code{boot} object as returned by the \code{boot} function.
22+
}
23+
\references{Li, H, Calder, C. A. and Cressie N. A. C. (2007) Beyond Moran's I: testing for spatial dependence based on the spatial autoregressive model. Geographical Analysis 39, 357-375; Li, H, Calder, C. A. and Cressie N. A. C. (2012) One-step estimation of spatial dependence parameters: Properties and extensions of the APLE statistic, Journal of Multivariate Analysis 105, 68-84.}
24+
\author{Roger Bivand \email{[email protected]}}
25+
26+
\seealso{\code{\link{aple}}, \code{\link[boot]{boot}}}
27+
\examples{
28+
\dontrun{
29+
wheat <- st_read(system.file("shapes/wheat.shp", package="spData")[1], quiet=TRUE)
30+
nbr1 <- poly2nb(wheat, queen=FALSE)
31+
nbrl <- nblag(nbr1, 2)
32+
nbr12 <- nblag_cumul(nbrl)
33+
wheat_g <- wheat
34+
st_geometry(wheat_g) <- NULL
35+
cms0 <- with(wheat_g, tapply(yield, c, median))
36+
cms1 <- c(model.matrix(~ factor(c) -1, data=wheat) \%*\% cms0)
37+
wheat$yield_detrend <- wheat$yield - cms1
38+
oldRNG <- RNGkind()
39+
RNGkind("L'Ecuyer-CMRG")
40+
set.seed(1L)
41+
boot_out_ser <- aple.mc(as.vector(scale(wheat$yield_detrend, scale=FALSE)),
42+
nb2listw(nbr12, style="W"), nsim=500)
43+
plot(boot_out_ser)
44+
boot_out_ser
45+
library(parallel)
46+
oldCores <- set.coresOption(NULL)
47+
nc <- detectCores(logical=FALSE)
48+
# set nc to 1L here
49+
if (nc > 1L) nc <- 1L
50+
invisible(set.coresOption(nc))
51+
set.seed(1L)
52+
if (!get.mcOption()) {
53+
cl <- makeCluster(nc)
54+
set.ClusterOption(cl)
55+
} else{
56+
mc.reset.stream()
57+
}
58+
boot_out_par <- aple.mc(as.vector(scale(wheat$yield_detrend, scale=FALSE)),
59+
nb2listw(nbr12, style="W"), nsim=500)
60+
if (!get.mcOption()) {
61+
set.ClusterOption(NULL)
62+
stopCluster(cl)
63+
}
64+
boot_out_par
65+
invisible(set.coresOption(oldCores))
66+
RNGkind(oldRNG[1], oldRNG[2])
67+
}
68+
}
69+
% Add one or more standard keywords, see file 'KEYWORDS' in the
70+
% R documentation directory.
71+
\keyword{spatial}
72+

0 commit comments

Comments
 (0)