Skip to content

Commit dfe054c

Browse files
Merge pull request #33 from USEPA/develop
CRAN v0.10.0
2 parents 40ce490 + 0e871b1 commit dfe054c

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

93 files changed

+340
-116
lines changed

DESCRIPTION

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Package: spmodel
22
Title: Spatial Statistical Modeling and Prediction
3-
Version: 0.9.0
3+
Version: 0.10.0
44
Authors@R: c(
55
person(given = "Michael",
66
family = "Dumelle",

NEWS.md

+9
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,12 @@
1+
# spmodel 0.10.0
2+
3+
## Minor updates
4+
5+
* Added a robust semivariogram option to `esv()`; see the `robust`argument to `esv()` ([#28](https://github.com/USEPA/spmodel/issues/28)).
6+
* Added `"none"` and `"ie"` spatial covariance types (via `spcov_type` or `spcov_initial`) to `spautor()`, `spgautor()`, and `spautorRF()` ([#27](https://github.com/USEPA/spmodel/issues/27)).
7+
* Fixed the aspect ratio at one for level curve plots of fitted model objects (anisotropic and isotropic).
8+
* Changed the title for level curve plots of fitted model objects from "Anisotropic level curve" to "Isotropic level curve" when the model is isotropic.
9+
110
# spmodel 0.9.0
211

312
## Major Updates

R/esv.R

+17-4
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,13 @@
1515
#' are binned and averaged within distance classes. When \code{cloud} = TRUE,
1616
#' all pairwise semivariances and distances are returned (this is known as
1717
#' the "cloud" semivariogram).
18-
#' @param dist_matrix A distance matrix to be used instead of providing coordinate names.
18+
#' @param robust A logical indicating whether the robust semivariogram
19+
#' (Cressie and Hawkins, 1980) is used. The default is \code{FALSE}.
1920
#' @param bins The number of equally spaced bins. The default is 15. Ignored if
2021
#' \code{cloud = TRUE}.
2122
#' @param cutoff The maximum distance considered.
2223
#' The default is half the diagonal of the bounding box from the coordinates.
24+
#' @param dist_matrix A distance matrix to be used instead of providing coordinate names.
2325
#' @param partition_factor An optional formula specifying the partition factor.
2426
#' If specified, semivariances are only computed for observations sharing the
2527
#' same level of the partition factor.
@@ -36,7 +38,8 @@
3638
#' \eqn{1/N(h) \sum (r1 - r2)^2}, where \eqn{N(h)} is the number of (unique)
3739
#' pairs in the set of observations whose distance separation is \code{h} and
3840
#' \code{r1} and \code{r2} are residuals corresponding to observations whose
39-
#' distance separation is \code{h}. In spmodel, these distance bins actually
41+
#' distance separation is \code{h}. The robust version is described by
42+
#' Cressie and Hawkins (1980). In spmodel, these distance bins actually
4043
#' contain observations whose distance separation is \code{h +- c},
4144
#' where \code{c} is a constant determined implicitly by \code{bins}. Typically,
4245
#' only observations whose distance separation is below some cutoff are used
@@ -59,7 +62,10 @@
5962
#' @examples
6063
#' esv(sulfate ~ 1, sulfate)
6164
#' plot(esv(sulfate ~ 1, sulfate))
62-
esv <- function(formula, data, xcoord, ycoord, cloud = FALSE, bins = 15, cutoff, dist_matrix, partition_factor) {
65+
#' @references Cressie, N & Hawkins, D.M. 1980. Robust estimation of the variogram.
66+
#' \emph{Journal of the International Association for Mathematical Geology},
67+
#' \strong{12}, 115-125.
68+
esv <- function(formula, data, xcoord, ycoord, cloud = FALSE, robust = FALSE, bins = 15, cutoff, dist_matrix, partition_factor) {
6369

6470
# filter out missing response values
6571
na_index <- is.na(data[[all.vars(formula)[1]]])
@@ -166,9 +172,16 @@ esv <- function(formula, data, xcoord, ycoord, cloud = FALSE, bins = 15, cutoff,
166172
if (cloud) {
167173
esv_out <- get_esv_cloud(residual_vector2, dist_vector, formula)
168174
} else {
169-
esv_out <- get_esv(residual_vector2, dist_vector, bins, cutoff)
175+
if (robust) {
176+
residual_vector12 <- sqrt(residual_vector)
177+
esv_out <- get_esv_robust(residual_vector12, dist_vector, bins, cutoff)
178+
} else {
179+
esv_out <- get_esv(residual_vector2, dist_vector, bins, cutoff)
180+
}
170181
}
171182

183+
184+
172185
# remove NA
173186
# esv_out <- na.omit(esv_out)
174187
esv_out <- structure(esv_out, class = c("esv", class(esv_out)), call = match.call(), cloud = cloud)

R/get_esv.R

+33
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,39 @@ get_esv <- function(residual_vector2, dist_vector, bins, cutoff, formula) {
2929
esv_out
3030
}
3131

32+
get_esv_robust <- function(residual_vector12, dist_vector, bins, cutoff, formula) {
33+
34+
# compute semivariogram classes
35+
dist_classes <- cut(dist_vector, breaks = seq(0, cutoff, length.out = bins + 1))
36+
37+
# compute squared differences within each class
38+
gamma <- tapply(residual_vector12, dist_classes, function(x) {
39+
1 / (0.914 + (0.988 / length(x))) * (mean(x)^4)
40+
})
41+
42+
# compute pairs within each class
43+
np <- tapply(residual_vector12, dist_classes, length)
44+
45+
# set as zero if necessary
46+
np <- ifelse(is.na(np), 0, np)
47+
48+
# compute average distance within each class
49+
dist <- tapply(dist_vector, dist_classes, mean)
50+
51+
# return output
52+
esv_out <- tibble::tibble(
53+
bins = factor(levels(dist_classes), levels = levels(dist_classes)),
54+
dist = as.numeric(dist),
55+
gamma = as.numeric(gamma),
56+
np = as.numeric(np)
57+
)
58+
59+
# set row names to NULL
60+
# row.names(esv_out) <- NULL
61+
62+
esv_out
63+
}
64+
3265
get_esv_cloud <- function(residual_vector2, dist_vector, formula) {
3366

3467
esv_out <- tibble::tibble(dist = dist_vector, gamma = residual_vector2)

R/get_esv_dotlist.R

+6-1
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,13 @@ get_esv_dotlist <- function(..., max_halfdist) {
1717
if (!("cutoff" %in% names(dotlist))) {
1818
dotlist$cutoff <- max_halfdist
1919
}
20+
21+
if (!("robust" %in% names(dotlist))) {
22+
dotlist$robust <- FALSE
23+
}
24+
2025
# make dotlist esv
21-
dotlist_esv <- list(bins = dotlist$bins, cutoff = dotlist$cutoff)
26+
dotlist_esv <- list(robust = dotlist$robust, bins = dotlist$bins, cutoff = dotlist$cutoff)
2227
}
2328

2429

R/plot.R

+8-2
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
#' For [splm()] and [spglm()] fitted model objects, there are two additional values of \code{which}:
2828
#' \itemize{
2929
#' \item 7: Fitted spatial covariance function vs distance
30-
#' \item 8: Fitted anisotropic level curve of equal correlation
30+
#' \item 8: Fitted anisotropic (or isotropic) level curve of equal correlation
3131
#' }
3232
#'
3333
#' @return No return value. Function called for plotting side effects.
@@ -195,17 +195,23 @@ plot.splm <- function(x, which, ...) {
195195
x_new <- x_orig
196196
y_new <- y_orig
197197
}
198+
if (x$anisotropy) {
199+
main_new <- "Anisotropic level curve"
200+
} else {
201+
main_new <- "Isotropic level curve"
202+
}
198203
plot(
199204
x = x_new,
200205
y = y_new,
201206
xlab = "x-distance",
202207
ylab = "y-distance",
203-
main = "Anisotropic level curve", # of equal correlation
208+
main = main_new,
204209
type = "l",
205210
xlim = c(-1, 1),
206211
ylim = c(-1, 1),
207212
xaxt = "n", # remove axis information
208213
yaxt = "n", # remove axis information
214+
asp = 1, # set equal aspect ratio
209215
...
210216
)
211217
title(sub = sub.caption)

R/spautor.R

+17-2
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,15 @@
1111
#' the variables in \code{fixed}, \code{random}, and \code{partition_factor},
1212
#' as well as potentially geographical information.
1313
#' @param spcov_type The spatial covariance type. Available options include
14-
#' \code{"car"} and \code{"sar"}. Parameterizations of each spatial covariance type are
14+
#' \code{"car"}, \code{"sar"}, and \code{"none"}. Parameterizations of each spatial covariance type are
1515
#' available in Details. When \code{spcov_type} is specified, relevant spatial
1616
#' covariance parameters are assumed unknown, requiring estimation.
1717
#' \code{spcov_type} is not required (and is
1818
#' ignored) if \code{spcov_initial} is provided. Multiple values can be
1919
#' provided in a character vector. Then \code{spautor()} is called iteratively
2020
#' for each element and a list is returned for each model fit.
21-
#' The default for \code{spcov_type} is \code{"car"}.
21+
#' The default for \code{spcov_type} is \code{"car"}. When \code{spcov_type}
22+
#' is \code{"none"}, [splm()] is called.
2223
#' @param spcov_initial An object from [spcov_initial()] specifying initial and/or
2324
#' known values for the spatial covariance parameters.
2425
#' Not required if \code{spcov_type} is provided. Multiple [spcov_initial()]
@@ -91,6 +92,7 @@
9192
#' symmetry condition matrix \eqn{M}
9293
#' \item sar: \eqn{[(I - range * W)(I - range * W)^T]^{-1}},
9394
#' weights matrix \eqn{W}, \eqn{^T} indicates matrix transpose
95+
#' \item none: \eqn{0}
9496
#' }
9597
#' If there are observations with no neighbors, they are given a unique variance
9698
#' parameter called \code{extra}, which must be non-negative.
@@ -187,6 +189,19 @@ spautor <- function(formula, data, spcov_type, spcov_initial, estmethod = "reml"
187189
return(new_spautor_out)
188190
}
189191

192+
193+
194+
# call splm if spcov_type is none (works on an individual element of spcov_type or initial)
195+
if ((!missing(spcov_type) && spcov_type %in% c("none", "ie")) || (!missing(spcov_initial) && inherits(spcov_initial, c("none", "ie")))) {
196+
call_list <- as.list(match.call())[-1]
197+
# remove spautor specific arguments
198+
args_remove <- c("W", "row_st", "range_positive", "cutoff")
199+
call_list <- call_list[!names(call_list) %in% args_remove]
200+
penv <- parent.frame()
201+
splm_out <- do.call("splm", call_list, envir = penv)
202+
return(splm_out)
203+
}
204+
190205
# replace initial values with appropriate NA's
191206
if (missing(spcov_initial)) {
192207
spcov_initial <- spmodel::spcov_initial(spcov_type)

R/spautorRF.R

+5-1
Original file line numberDiff line numberDiff line change
@@ -116,10 +116,14 @@ spautorRF <- function(formula, data, ...) {
116116
data <- data[order(c(which(!na_index), which(na_index))), , drop = FALSE]
117117
# perform splm
118118
spautor_out <- do.call(spmodel::spautor, c(list(formula = .ranger_resid ~ 1, data = data), spautor_args), envir = penv)
119-
if (inherits(spautor_out, "spautor")) {
119+
if (inherits(spautor_out, c("spautor"))) {
120120
spautor_out$call <- NA
121121
# output list with names and class
122122
sprf_out <- structure(list(call = match.call(), ranger = ranger_out, spautor = spautor_out, newdata = newdata), class = "spautorRF")
123+
} else if (inherits(spautor_out, c("splm"))) { # splm for none and ie covariance
124+
spautor_out$call <- NA
125+
# output list with names and class
126+
sprf_out <- structure(list(call = match.call(), ranger = ranger_out, spautor = spautor_out, newdata = newdata), class = "splmRF")
123127
} else {
124128
spautor_out <- lapply(spautor_out, function(x) {
125129
x$call <- NA

R/spgautor.R

+16-2
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,15 @@
1919
#' the variables in \code{fixed}, \code{random}, and \code{partition_factor},
2020
#' as well as potentially geographical information.
2121
#' @param spcov_type The spatial covariance type. Available options include
22-
#' \code{"car"} and \code{"sar"}. Parameterizations of each spatial covariance type are
22+
#' \code{"car"}, \code{"sar"}, \code{"none"}, and \code{"ie"}. Parameterizations of each spatial covariance type are
2323
#' available in Details. When \code{spcov_type} is specified, relevant spatial
2424
#' covariance parameters are assumed unknown, requiring estimation.
2525
#' \code{spcov_type} is not required (and is
2626
#' ignored) if \code{spcov_initial} is provided. Multiple values can be
2727
#' provided in a character vector. Then \code{spgautor()} is called iteratively
2828
#' for each element and a list is returned for each model fit.
29-
#' The default for \code{spcov_type} is \code{"car"}.
29+
#' The default for \code{spcov_type} is \code{"car"}. When \code{spcov_type}
30+
#' is \code{"none"} or \code{"ie"}, [splm()] is called.
3031
#' @param spcov_initial An object from [spcov_initial()] specifying initial and/or
3132
#' known values for the spatial covariance parameters.
3233
#' Not required if \code{spcov_type} is provided. Multiple [spcov_initial()]
@@ -165,6 +166,8 @@
165166
#' symmetry condition matrix \eqn{M}
166167
#' \item sar: \eqn{[(I - range * W)(I - range * W)^T]^{-1}},
167168
#' weights matrix \eqn{W}, \eqn{^T} indicates matrix transpose
169+
#' \item none: \eqn{0}
170+
#' \item ie: \eqn{0} (see [spglm()] for differences compared to none)
168171
#' }
169172
#' If there are observations with no neighbors, they are given a unique variance
170173
#' parameter called \code{extra}, which must be non-negative.
@@ -267,6 +270,17 @@ spgautor <- function(formula, family, data, spcov_type, spcov_initial, dispersio
267270
return(new_spgautor_out)
268271
}
269272

273+
# call spglm if spcov_type is none (works on an individual element of spcov_type or initial)
274+
if ((!missing(spcov_type) && spcov_type %in% c("none", "ie")) || (!missing(spcov_initial) && inherits(spcov_initial, c("none", "ie")))) {
275+
call_list <- as.list(match.call())[-1]
276+
# remove spautor specific arguments
277+
args_remove <- c("W", "row_st", "range_positive", "cutoff")
278+
call_list <- call_list[!names(call_list) %in% args_remove]
279+
penv <- parent.frame()
280+
spglm_out <- do.call("spglm", call_list, envir = penv)
281+
return(spglm_out)
282+
}
283+
270284
# set dispersion initial
271285
if (missing(dispersion_initial)) dispersion_initial <- NULL else family <- class(dispersion_initial)
272286

docs/404.html

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/LICENSE.html

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/articles/SPGLMs.html

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Loading

0 commit comments

Comments
 (0)