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
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ S3method(print,cv_density_lp)
S3method(print,density_lp)
S3method(print,density_lp_ppp)
S3method(print,domain_lp)
S3method(summary,density_lp)
export(cv_density_lp)
export(cv_density_lp_ppp)
export(density_lp)
Expand Down
53 changes: 24 additions & 29 deletions R/cv.R
Original file line number Diff line number Diff line change
@@ -1,5 +1,20 @@
# LOO cross-validation for bandwidth and degree selection -----------------------

#' Internal constructor for cv_density_lp objects
#' @keywords internal
new_cv_density_lp <- function(m_hat, h_hat, scores, grids, call) {
structure(
list(
m_hat = m_hat,
h_hat = h_hat,
scores = scores,
grids = grids,
call = call
),
class = "cv_density_lp"
)
}

#' LOO cross-validation for the local polynomial density estimator
#'
#' For each pair \eqn{(m, h)} in the grid, computes the leave-one-out
Expand All @@ -20,7 +35,7 @@
#' @return An S3 object of class `"cv_density_lp"` containing:
#' - `$m_hat`, `$h_hat`: selected degree and bandwidth,
#' - `$scores`: matrix `|m_grid| x |h_grid|` of CV scores,
#' - `$m_grid`, `$h_grid`: the grids used.
#' - `$grids`: list containing `$m` and `$h` grids used.
#' @seealso [density_lp()], [cv_density_lp_ppp()]
#' @export
cv_density_lp <- function(X, h_grid, m_grid = 0:3, domain, N_quad = 500L) {
Expand Down Expand Up @@ -105,16 +120,15 @@ cv_density_lp <- function(X, h_grid, m_grid = 0:3, domain, N_quad = 500L) {
h_grid[idx[2L]]
))
}
structure(
list(
m_hat = m_grid[idx[1L]],
h_hat = h_grid[idx[2L]],
scores = scores,
m_grid = m_grid,
h_grid = h_grid,
call = match.call()
new_cv_density_lp(
m_hat = m_grid[idx[1L]],
h_hat = h_grid[idx[2L]],
scores = scores,
grids = list(
m = m_grid,
h = h_grid
),
class = "cv_density_lp"
call = match.call()
)
}

Expand Down Expand Up @@ -143,22 +157,3 @@ cv_density_lp_ppp <- function(pp, h_grid, m_grid = 0:3, N_quad = 500L) {
N_quad = N_quad
)
}

#' Print a cv_density_lp object
#' @param x A `"cv_density_lp"` object.
#' @param ... Ignored.
#' @return `x` invisibly.
#' @export
print.cv_density_lp <- function(x, ...) {
score <- x$scores[paste0("m=", x$m_hat), as.character(round(x$h_hat, 6L))]
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))
cat(sprintf(" CV score : %.5g\n", score))
cat(sprintf(
" Grid : %d degree(s) x %d bandwidth(s)\n",
length(x$m_grid),
length(x$h_grid)
))
invisible(x)
}
67 changes: 50 additions & 17 deletions R/density_lp.R
Original file line number Diff line number Diff line change
@@ -1,5 +1,43 @@
# Grid estimator (exported) -----------------------------------------------------

#' Internal constructor for density_lp objects
#' @keywords internal
new_density_lp <- function(
estimate,
variance,
t_grid,
X,
h,
m,
domain,
N_quad,
n_fail,
call
) {
structure(
list(
estimate = estimate,
variance = variance,
t_grid = t_grid,
X = X,
params = list(
h = h,
m = m,
domain = domain,
N_quad = N_quad
),
stats = list(
n_fail = n_fail,
n_obs = nrow(X),
p = nrow(t_grid),
d = ncol(X)
),
call = call
),
class = "density_lp"
)
}

#' Local polynomial density estimation on a grid of target points
#'
#' @param X Numeric matrix `n x d` of observations. Must be a matrix; no
Expand All @@ -12,11 +50,7 @@
#' @param N_quad Number of Monte Carlo quadrature points per target point
#' (default: 500).
#'
#' @return An S3 object of class `"density_lp"` containing:
#' - `$estimate`: numeric vector of length `p` (estimated values),
#' - `$variance`: numeric vector of length `p` (variance estimates),
#' - `$t_grid`: the matrix of target points,
#' - `$h`, `$m`, `$domain`, `$N_quad`: parameters used.
#' @return An S3 object of class `"density_lp"`.
#'
#' @export
density_lp <- function(X, t_grid, h, m = 0L, domain, N_quad = 500L) {
Expand Down Expand Up @@ -66,17 +100,16 @@ density_lp <- function(X, t_grid, h, m = 0L, domain, N_quad = 500L) {
)
}

structure(
list(
estimate = estimate,
variance = variance,
t_grid = t_grid,
h = h,
m = m,
domain = domain,
N_quad = N_quad,
call = match.call()
),
class = "density_lp"
new_density_lp(
estimate = estimate,
variance = variance,
t_grid = t_grid,
X = X,
h = h,
m = m,
domain = domain,
N_quad = N_quad,
n_fail = n_fail,
call = match.call()
)
}
34 changes: 27 additions & 7 deletions R/density_lp_ppp.R
Original file line number Diff line number Diff line change
@@ -1,5 +1,21 @@
# spatstat wrapper ---------------------------------------------------------------

#' Internal constructor for density_lp_ppp objects
#' @keywords internal
new_density_lp_ppp <- function(im_obj, params, stats, call) {
structure(
c(
unclass(im_obj),
list(
params = params,
stats = stats,
call = call
)
),
class = c("density_lp_ppp", "im")
)
}

#' Local polynomial density estimation for a spatial point pattern
#'
#' Convenience wrapper around [density_lp()] for `ppp` objects (spatstat).
Expand All @@ -13,8 +29,8 @@
#' @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 `im` object (spatstat.geom). Pixels outside `Window(pp)` are
#' `NA`.
#' @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()]
#' @importFrom spatstat.geom Window inside.owin im unitname
#' @export
Expand Down Expand Up @@ -68,11 +84,15 @@ density_lp_ppp <- function(pp, h, m = 0L, N_quad = 500L, nx = 128L, ny = 128L) {
unitname = spatstat.geom::unitname(pp)
)

structure(
c(
unclass(im_obj),
list(h = h, m = as.integer(m), N_quad = as.integer(N_quad), call = mc)
new_density_lp_ppp(
im_obj = im_obj,
params = list(
h = h,
m = as.integer(m),
N_quad = as.integer(N_quad),
domain = dom
),
class = c("density_lp_ppp", "im")
stats = fit$stats,
call = mc
)
}
91 changes: 76 additions & 15 deletions R/methods.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,19 @@
#' @export
print.density_lp_ppp <- function(x, ...) {
cat("Local polynomial density estimate (spatstat)\n")
cat(sprintf(" h : %g\n", x$h))
cat(sprintf(" m : %d\n", x$m))
cat(sprintf(" N_quad : %d\n", x$N_quad))
cat(sprintf(" Points : %d\n", x$stats$n_obs))
cat(sprintf(" h : %g\n", x$params$h))
cat(sprintf(" m : %d\n", x$params$m))
cat(sprintf(" N_quad : %d\n", x$params$N_quad))
cat(sprintf(" Grid : %d x %d pixels\n", length(x$xcol), length(x$yrow)))
rng <- range(x$v, na.rm = TRUE)
cat(sprintf(" Range : [%.5g, %.5g]\n", rng[1L], rng[2L]))
if (!is.null(x$stats$n_fail) && x$stats$n_fail > 0L) {
cat(sprintf(
" Note : %d failure(s) during calculation\n",
x$stats$n_fail
))
}
invisible(x)
}

Expand All @@ -35,18 +42,51 @@ plot.density_lp_ppp <- function(x, ...) {
#' @export
print.density_lp <- function(x, ...) {
cat("Local polynomial density estimate\n")
cat(sprintf(" Domain : %s\n", x$domain$label))
cat(sprintf(" d : %d\n", x$domain$d))
cat(sprintf(" h : %g\n", x$h))
cat(sprintf(" m : %d\n", x$m))
cat(sprintf(" N_quad : %d\n", x$N_quad))
cat(sprintf(" Points : %d\n", nrow(x$t_grid)))
cat(sprintf(" Points : %d\n", x$stats$p))
cat(sprintf(" h : %g\n", x$params$h))
cat(sprintf(" m : %d\n", x$params$m))
rng <- range(x$estimate, na.rm = TRUE)
cat(sprintf(" Range : [%.5g, %.5g]\n", rng[1L], rng[2L]))
if (x$stats$n_fail > 0L) {
cat(sprintf(
" Note : %d failure(s) during calculation\n",
x$stats$n_fail
))
}
invisible(x)
}

#' Summary of a density_lp object
#' @param object A `"density_lp"` object.
#' @param ... Ignored.
#' @return `object` invisibly.
#' @export
summary.density_lp <- function(object, ...) {
cat("Local Polynomial Density Estimation Summary\n")
cat("-------------------------------------------\n")
cat(sprintf(
" Range : [%.5g, %.5g]\n",
min(x$estimate, na.rm = TRUE),
max(x$estimate, na.rm = TRUE)
"Call : %s\n",
paste(deparse(object$call), collapse = "\n")
))
invisible(x)
cat(sprintf(
"Domain : %s (d = %d)\n",
object$params$domain$label,
object$stats$d
))
cat(sprintf("Observations : n = %d\n", object$stats$n_obs))
cat(sprintf(
"Grid points : p = %d (%d success, %d failure)\n",
object$stats$p,
object$stats$p - object$stats$n_fail,
object$stats$n_fail
))
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))
cat("\nResults (estimate):\n")
print(summary(object$estimate))
invisible(object)
}

#' Plot a density_lp object
Expand All @@ -59,10 +99,10 @@ print.density_lp <- function(x, ...) {
#' @export
plot.density_lp <- function(
x,
main = paste0("densityLP (h = ", x$h, ", m = ", x$m, ")"),
main = paste0("densityLP (h = ", x$params$h, ", m = ", x$params$m, ")"),
...
) {
d <- x$domain$d
d <- x$stats$d
if (d == 1L) {
plot(
x$t_grid[, 1L],
Expand Down Expand Up @@ -98,3 +138,24 @@ plot.density_lp <- function(
}
invisible(x)
}

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

#' 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))]
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))
cat(sprintf(" CV score : %.5g\n", score))
cat(sprintf(
" Grid : %d degree(s) x %d bandwidth(s)\n",
length(x$grids$m),
length(x$grids$h)
))
invisible(x)
}
2 changes: 1 addition & 1 deletion man/cv_density_lp.Rd

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

8 changes: 1 addition & 7 deletions man/density_lp.Rd

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

4 changes: 2 additions & 2 deletions man/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/new_cv_density_lp.Rd

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

Loading
Loading