Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .Rbuildignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,5 @@
^README\.qmd$
^LICENSE\.md$
^\.positai$
^README\.Rmd$
^README\.html$
2 changes: 2 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ S3method(print,cv_density_lp)
S3method(print,density_lp)
S3method(print,density_lp_ppp)
S3method(print,domain_lp)
S3method(summary,cv_density_lp)
S3method(summary,density_lp)
S3method(summary,density_lp_ppp)
export(cv_density_lp)
export(cv_density_lp_ppp)
export(density_lp)
Expand Down
17 changes: 15 additions & 2 deletions R/cv.R
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,13 @@ new_cv_density_lp <- function(m_hat, h_hat, scores, grids, call) {
#' - `$scores`: matrix `|m_grid| x |h_grid|` of CV scores,
#' - `$grids`: list containing `$m` and `$h` grids used.
#' @seealso [density_lp()], [cv_density_lp_ppp()]
#' @examples
#' set.seed(1)
#' X <- matrix(runif(30 * 2), ncol = 2)
#' dom <- domain_Rd(2)
#' cv <- cv_density_lp(X, h_grid = c(0.2, 0.4), m_grid = 0:1, domain = dom, N_quad = 100L)
#' print(cv)
#'
#' @export
cv_density_lp <- function(X, h_grid, m_grid = 0:3, domain, N_quad = 500L) {
check_X(X)
Expand Down Expand Up @@ -99,7 +106,7 @@ cv_density_lp <- function(X, h_grid, m_grid = 0:3, domain, N_quad = 500L) {
warning(sprintf(
paste0(
"%d/%d LOO evaluations failed for the selected parameters",
" (m = %d, h = %g).",
" (m = %d, h = %g): V(h) empty or Gram matrix singular.",
" The CV score is unreliable: consider a larger h or a coarser grid."
),
n_fail[idx[1L], idx[2L]],
Expand All @@ -111,7 +118,7 @@ cv_density_lp <- function(X, h_grid, m_grid = 0:3, domain, N_quad = 500L) {
warning(sprintf(
paste0(
"%d/%d LOO evaluations failed for the selected parameters",
" (m = %d, h = %g).",
" (m = %d, h = %g): V(h) empty or Gram matrix singular.",
" Results should be interpreted with caution."
),
n_fail[idx[1L], idx[2L]],
Expand Down Expand Up @@ -143,6 +150,12 @@ cv_density_lp <- function(X, h_grid, m_grid = 0:3, domain, N_quad = 500L) {
#' @param N_quad Number of quadrature points per evaluation (default 500).
#' @return An S3 object of class `"cv_density_lp"` (see [cv_density_lp()]).
#' @seealso [cv_density_lp()], [density_lp_ppp()]
#' @examples
#' set.seed(1)
#' pp <- spatstat.geom::ppp(runif(30), runif(30), window = spatstat.geom::owin())
#' cv <- cv_density_lp_ppp(pp, h_grid = c(0.2, 0.4), m_grid = 0:1, N_quad = 100L)
#' print(cv)
#'
#' @importFrom spatstat.geom Window
#' @export
cv_density_lp_ppp <- function(pp, h_grid, m_grid = 0:3, N_quad = 500L) {
Expand Down
15 changes: 13 additions & 2 deletions R/density_lp.R
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,17 @@ new_density_lp <- function(
#'
#' @return An S3 object of class `"density_lp"`.
#'
#' @examples
#' set.seed(1)
#' X <- matrix(runif(50 * 2), ncol = 2)
#' t_grid <- as.matrix(expand.grid(
#' x = seq(0.1, 0.9, length.out = 5),
#' y = seq(0.1, 0.9, length.out = 5)
#' ))
#' dom <- domain_Rd(2)
#' fit <- density_lp(X, t_grid = t_grid, h = 0.3, m = 1L, domain = dom, N_quad = 100L)
#' print(fit)
#'
#' @export
density_lp <- function(X, t_grid, h, m = 0L, domain, N_quad = 500L) {
check_X(X)
Expand Down Expand Up @@ -106,9 +117,9 @@ density_lp <- function(X, t_grid, h, m = 0L, domain, N_quad = 500L) {
t_grid = t_grid,
X = X,
h = h,
m = m,
m = as.integer(m),
domain = domain,
N_quad = N_quad,
N_quad = as.integer(N_quad),
n_fail = n_fail,
call = match.call()
)
Expand Down
8 changes: 7 additions & 1 deletion R/density_lp_ppp.R
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,19 @@ new_density_lp_ppp <- function(im_obj, params, stats, call) {
#'
#' @param pp A `ppp` object (spatstat.geom).
#' @param h Bandwidth (scalar > 0).
#' @param m Polynomial degree (integer >= 0, default 1).
#' @param m Polynomial degree (integer >= 0, default 0).
#' @param N_quad Number of quadrature points per target point (default 500).
#' @param nx Number of pixel columns (default 128).
#' @param ny Number of pixel rows (default 128).
#' @return An S3 object of class `"density_lp_ppp"`, which also inherits from
#' `spatstat.geom::im`. Pixels outside `Window(pp)` are `NA`.
#' @seealso [density_lp()], [domain_from_owin()]
#' @examples
#' set.seed(1)
#' pp <- spatstat.geom::ppp(runif(50), runif(50), window = spatstat.geom::owin())
#' fit <- density_lp_ppp(pp, h = 0.3, m = 1L, N_quad = 100L, nx = 20L, ny = 20L)
#' print(fit)
#'
#' @importFrom spatstat.geom Window inside.owin im unitname
#' @export
density_lp_ppp <- function(pp, h, m = 0L, N_quad = 500L, nx = 128L, ny = 128L) {
Expand Down
11 changes: 11 additions & 0 deletions R/domain.R
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ print.domain_lp <- function(x, ...) {
#'
#' @param d Space dimension (integer >= 1).
#' @return An `"domain_lp"` object.
#' @examples
#' dom <- domain_Rd(2)
#' print(dom)
#' @export
domain_Rd <- function(d) {
stopifnot(is.numeric(d), length(d) == 1L, is.finite(d), d >= 1, d == floor(d))
Expand All @@ -44,6 +47,10 @@ domain_Rd <- function(d) {
#' coordinate axis), returning a logical vector of length `n`. The dimension
#' `d` is inferred from `length(formals(is_in_domain))`.
#' @return An `"domain_lp"` object.
#' @examples
#' is_in <- function(x, y) x^2 + y^2 <= 1
#' dom <- domain_from_indicator(is_in)
#' print(dom)
#' @export
domain_from_indicator <- function(is_in_domain) {
stopifnot(is.function(is_in_domain))
Expand All @@ -66,6 +73,10 @@ domain_from_indicator <- function(is_in_domain) {
#'
#' @param win An `owin` object (spatstat.geom).
#' @return An `"domain_lp"` object (d = 2).
#' @examples
#' win <- spatstat.geom::owin(c(0, 1), c(0, 1))
#' dom <- domain_from_owin(win)
#' print(dom)
#' @export
domain_from_owin <- function(win) {
stopifnot(inherits(win, "owin"))
Expand Down
3 changes: 3 additions & 0 deletions R/domain_sector.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
#'
#' @param k Sector exponent (scalar > 0).
#' @return An `"domain_lp"` object (d = 2).
#' @examples
#' dom <- domain_sector(k = 2)
#' print(dom)
#' @export
domain_sector <- function(k) {
stopifnot(is.numeric(k), length(k) == 1L, is.finite(k), k > 0)
Expand Down
62 changes: 61 additions & 1 deletion R/methods.R
Original file line number Diff line number Diff line change
Expand Up @@ -139,15 +139,75 @@ plot.density_lp <- function(
invisible(x)
}

#' Summary of a density_lp_ppp object
#' @param object A `"density_lp_ppp"` object.
#' @param ... Ignored.
#' @return `object` invisibly.
#' @export
summary.density_lp_ppp <- function(object, ...) {
cat("Local Polynomial Density Estimation Summary (spatstat)\n")
cat("-------------------------------------------------------\n")
cat(sprintf(
"Call : %s\n",
paste(deparse(object$call), collapse = "\n")
))
cat(sprintf("Domain : %s\n", object$params$domain$label))
cat(sprintf("Observations : n = %d\n", object$stats$n_obs))
cat(sprintf(
"Grid : %d x %d pixels\n",
length(object$xcol),
length(object$yrow)
))
cat("\nParameters:\n")
cat(sprintf(" Bandwidth (h) : %g\n", object$params$h))
cat(sprintf(" Degree (m) : %d\n", object$params$m))
cat(sprintf(" Quadrature : %d MC points\n", object$params$N_quad))
if (!is.null(object$stats$n_fail) && object$stats$n_fail > 0L) {
cat(sprintf(" Note : %d failure(s)\n", object$stats$n_fail))
}
cat("\nResults (estimate):\n")
print(summary(as.vector(object$v), na.rm = TRUE))
invisible(object)
}

# S3 methods for "cv_density_lp" ------------------------------------------------

#' Summary of a cv_density_lp object
#' @param object A `"cv_density_lp"` object.
#' @param ... Ignored.
#' @return `object` invisibly.
#' @export
summary.cv_density_lp <- function(object, ...) {
row_idx <- which(object$grids$m == object$m_hat)
col_idx <- which(object$grids$h == object$h_hat)
score <- object$scores[row_idx, col_idx]
cat("LOO Cross-Validation Summary -- local polynomial density\n")
cat("---------------------------------------------------------\n")
cat(sprintf(
"Call : %s\n",
paste(deparse(object$call), collapse = "\n")
))
cat(sprintf("m grid : %s\n", paste(object$grids$m, collapse = ", ")))
cat(sprintf(
"h grid : %s\n",
paste(round(object$grids$h, 4L), collapse = ", ")
))
cat("\nCV score matrix (lower is better):\n")
print(round(object$scores, 4L))
cat(sprintf("\nSelected : m = %d, h = %.6g\n", object$m_hat, object$h_hat))
cat(sprintf("CV score : %.5g\n", score))
invisible(object)
}

#' Print a cv_density_lp object
#' @param x A \code{"cv_density_lp"} object.
#' @param ... Ignored.
#' @return \code{x} invisibly.
#' @export
print.cv_density_lp <- function(x, ...) {
score <- x$scores[paste0("m=", x$m_hat), as.character(round(x$h_hat, 6L))]
row_idx <- which(x$grids$m == x$m_hat)
col_idx <- which(x$grids$h == x$h_hat)
score <- x$scores[row_idx, col_idx]
cat("LOO cross-validation -- local polynomial density\n")
cat(sprintf(" Selected m : %d\n", x$m_hat))
cat(sprintf(" Selected h : %.6g\n", x$h_hat))
Expand Down
15 changes: 14 additions & 1 deletion R/rfactory.R
Original file line number Diff line number Diff line change
Expand Up @@ -75,14 +75,27 @@ rfactory <- function(f, is_in_domain = NULL, box, M = NULL, N_pilot = 2000L) {
}
stopifnot(is.numeric(M), length(M) == 1L, is.finite(M), M > 0)

function(n) {
function(n, max_iter = 10000L) {
n <- as.integer(n)
stopifnot(n >= 1L)
batch <- max(1000L, n * 10L)
out <- matrix(NA_real_, n, d)
filled <- 0L
iter <- 0L

while (filled < n) {
iter <- iter + 1L
if (iter > max_iter) {
stop(
sprintf(
"rfactory: collected only %d/%d points after %d iterations. ",
filled,
n,
max_iter
),
"Acceptance rate may be too low: check `box`, `f`, and `is_in_domain`."
)
}
cands <- .sample_box(batch)
if (!is.null(is_in_domain)) {
cands <- cands[.call_fn(is_in_domain, cands), , drop = FALSE]
Expand Down
8 changes: 8 additions & 0 deletions man/cv_density_lp.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions man/cv_density_lp_ppp.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions man/density_lp.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 8 additions & 1 deletion man/density_lp_ppp.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions man/domain_Rd.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions man/domain_from_indicator.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions man/domain_from_owin.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions man/domain_sector.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 19 additions & 0 deletions man/summary.cv_density_lp.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading