From cecf205cdc27cc1dd1367a153a9e2d661fe1191f Mon Sep 17 00:00:00 2001 From: SpatLyu Date: Tue, 11 Aug 2026 00:00:01 +0800 Subject: [PATCH 01/14] draft r interface for dmi --- R/dmi.R | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 R/dmi.R diff --git a/R/dmi.R b/R/dmi.R new file mode 100644 index 0000000..e69de29 From 694e35ffac1f2502cdd45e880c0c4d7aab3cc2e5 Mon Sep 17 00:00:00 2001 From: SpatLyu Date: Tue, 11 Aug 2026 00:00:01 +0800 Subject: [PATCH 02/14] draft r interface for dmi --- R/Agenerics.R | 2 +- R/dmi.R | 39 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+), 1 deletion(-) diff --git a/R/Agenerics.R b/R/Agenerics.R index ccd1766..7945f0a 100644 --- a/R/Agenerics.R +++ b/R/Agenerics.R @@ -7,6 +7,6 @@ register_generic = \(name, def = NULL) { } } -for (gen in c("pc", "ops", "fnn")) { +for (gen in c("pc", "ops", "fnn", "dmi")) { register_generic(gen) } diff --git a/R/dmi.R b/R/dmi.R index e69de29..919917e 100644 --- a/R/dmi.R +++ b/R/dmi.R @@ -0,0 +1,39 @@ +.dmi_ts = \(data, target, tau = 1:10, pred = NULL, k = 3, + base = exp(1), normalize = FALSE, threads = 1) { + tv = .validate_var(data, target)[[1]] + if (is.null(pred)) pred = which(!is.na(tv)) + return(RcppFNN(tv, rt, eps, lib, pred, E, tau, style, dist.metric, + k, threads, higher.parallel, NULL, NULL)) +} + +#' False Nearest Neighbors +#' +#' @param data Observation data. +#' @param target Integer of column indice for the target variable. +#' @param E (optional) Embedding dimensions. +#' @param k (optional) Number of nearest neighbors used for evaluation. +#' @param tau (optional) Step of lag. +#' @param style (optional) Embedding style (`0` includes current state, `1` excludes it). +#' @param lib (optional) Libraries indices. +#' @param pred (optional) Predictions indices. +#' @param dist.metric (optional) Distance measure to be used. +#' @param rt (optional) Relative distance threshold. +#' @param eps (optional) Absolute distance threshold. +#' @param threads (optional) Number of threads used. +#' @param higher.parallel (optional) Whether to use a higher level of parallelism. +#' @param detrend (optional) Whether to remove the linear trend. +#' @param nb (optional) Neighbours list. +#' @param ... Additional arguments to absorb unused inputs in method dispatch. +#' +#' @return A vector. +#' @export +#' @name fnn +#' @aliases fnn,data.frame-method +#' @references +#' Kennel, M.B., Brown, R., Abarbanel, H.D.I., 1992. Determining embedding dimension for phase-space reconstruction using a geometrical construction. Physical Review A 45, 3403–3411. +#' +#' @examples +#' crash = sf::read_sf(system.file("case/crash.gpkg", package = "pc")) +#' pc::fnn(crash, 1, threads = 1) +#' +methods::setMethod("dmi", "data.frame", .dmi_ts) From 4090797955e91bc2ac6632a7fb97275376dcb325 Mon Sep 17 00:00:00 2001 From: SpatLyu Date: Tue, 11 Aug 2026 00:00:01 +0800 Subject: [PATCH 03/14] draft r interface for dmi --- R/dmi.R | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/R/dmi.R b/R/dmi.R index 919917e..afafee8 100644 --- a/R/dmi.R +++ b/R/dmi.R @@ -6,7 +6,7 @@ k, threads, higher.parallel, NULL, NULL)) } -#' False Nearest Neighbors +#' Delayed Mutual Information #' #' @param data Observation data. #' @param target Integer of column indice for the target variable. @@ -27,13 +27,13 @@ #' #' @return A vector. #' @export -#' @name fnn -#' @aliases fnn,data.frame-method +#' @name dmi +#' @aliases dmi,data.frame-method #' @references #' Kennel, M.B., Brown, R., Abarbanel, H.D.I., 1992. Determining embedding dimension for phase-space reconstruction using a geometrical construction. Physical Review A 45, 3403–3411. #' #' @examples -#' crash = sf::read_sf(system.file("case/crash.gpkg", package = "pc")) -#' pc::fnn(crash, 1, threads = 1) +#' abun = readr::read_csv(system.file("case/abundance.csv", package = "pc")) +#' pc::dmi(abun, 2) #' methods::setMethod("dmi", "data.frame", .dmi_ts) From 1f5af16b1c8acf18c34ac17cbfa14097a314468d Mon Sep 17 00:00:00 2001 From: SpatLyu Date: Tue, 11 Aug 2026 00:00:01 +0800 Subject: [PATCH 04/14] register dmi generic --- _pkgdown.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/_pkgdown.yml b/_pkgdown.yml index 95ed274..62d94db 100644 --- a/_pkgdown.yml +++ b/_pkgdown.yml @@ -30,6 +30,7 @@ reference: - subtitle: embedding dimension choice contents: - fnn + - dmi - subtitle: optimal parameters search contents: From 97746d9f1207d4ebc202af23b071b647a363b0ea Mon Sep 17 00:00:00 2001 From: SpatLyu Date: Tue, 11 Aug 2026 00:00:01 +0800 Subject: [PATCH 05/14] register dmi generic --- _pkgdown.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_pkgdown.yml b/_pkgdown.yml index 62d94db..737f41f 100644 --- a/_pkgdown.yml +++ b/_pkgdown.yml @@ -29,8 +29,8 @@ reference: - subtitle: embedding dimension choice contents: - - fnn - dmi + - fnn - subtitle: optimal parameters search contents: From e2f8b9c6aa55f0116f56cd0fa532df49eb8cd7a4 Mon Sep 17 00:00:00 2001 From: SpatLyu Date: Tue, 11 Aug 2026 00:00:01 +0800 Subject: [PATCH 06/14] polish title --- _pkgdown.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_pkgdown.yml b/_pkgdown.yml index 737f41f..9613b7c 100644 --- a/_pkgdown.yml +++ b/_pkgdown.yml @@ -27,7 +27,7 @@ reference: - title: Pattern Causality Analysis -- subtitle: embedding dimension choice +- subtitle: Phase Space Reconstruction contents: - dmi - fnn From 956a7e172d271b58731f81c4f725eba289ce0fd6 Mon Sep 17 00:00:00 2001 From: SpatLyu Date: Tue, 11 Aug 2026 00:00:01 +0800 Subject: [PATCH 07/14] draft r interface for dmi --- R/dmi.R | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/R/dmi.R b/R/dmi.R index afafee8..306d601 100644 --- a/R/dmi.R +++ b/R/dmi.R @@ -1,9 +1,8 @@ .dmi_ts = \(data, target, tau = 1:10, pred = NULL, k = 3, - base = exp(1), normalize = FALSE, threads = 1) { + base = exp(1), normalize = FALSE, threads = length(tau), ...) { tv = .validate_var(data, target)[[1]] if (is.null(pred)) pred = which(!is.na(tv)) - return(RcppFNN(tv, rt, eps, lib, pred, E, tau, style, dist.metric, - k, threads, higher.parallel, NULL, NULL)) + return(RcppDMI(tv, tau, pred, k, 0, base, normalize, threads)) } #' Delayed Mutual Information From e2151906e8921d14d62f0b717e759efb098d45bf Mon Sep 17 00:00:00 2001 From: SpatLyu Date: Tue, 11 Aug 2026 00:00:01 +0800 Subject: [PATCH 08/14] draft r interface for dmi --- R/dmi.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/R/dmi.R b/R/dmi.R index 306d601..aed4363 100644 --- a/R/dmi.R +++ b/R/dmi.R @@ -29,7 +29,7 @@ #' @name dmi #' @aliases dmi,data.frame-method #' @references -#' Kennel, M.B., Brown, R., Abarbanel, H.D.I., 1992. Determining embedding dimension for phase-space reconstruction using a geometrical construction. Physical Review A 45, 3403–3411. +#' Fraser, A.M., Swinney, H.L., 1986. Independent coordinates for strange attractors from mutual information. Physical Review A 33, 1134–1140. #' #' @examples #' abun = readr::read_csv(system.file("case/abundance.csv", package = "pc")) From 04a7ead66ab226c92cceba78a4a70bb1ed6e26d3 Mon Sep 17 00:00:00 2001 From: SpatLyu Date: Wed, 12 Aug 2026 00:00:01 +0800 Subject: [PATCH 09/14] draft r interface for dmi --- R/dmi.R | 1 + 1 file changed, 1 insertion(+) diff --git a/R/dmi.R b/R/dmi.R index aed4363..2ec1f35 100644 --- a/R/dmi.R +++ b/R/dmi.R @@ -30,6 +30,7 @@ #' @aliases dmi,data.frame-method #' @references #' Fraser, A.M., Swinney, H.L., 1986. Independent coordinates for strange attractors from mutual information. Physical Review A 33, 1134–1140. +#' Kraskov, A., Stogbauer, H., Grassberger, P., 2004. Estimating mutual information. Physical Review E 69, 066138. #' #' @examples #' abun = readr::read_csv(system.file("case/abundance.csv", package = "pc")) From 25fac3fa11e95f259a6bd34550eb3f58506a5c1b Mon Sep 17 00:00:00 2001 From: SpatLyu Date: Wed, 12 Aug 2026 00:00:01 +0800 Subject: [PATCH 10/14] draft r interface for dmi --- R/dmi.R | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/R/dmi.R b/R/dmi.R index 2ec1f35..269c463 100644 --- a/R/dmi.R +++ b/R/dmi.R @@ -1,5 +1,5 @@ -.dmi_ts = \(data, target, tau = 1:10, pred = NULL, k = 3, - base = exp(1), normalize = FALSE, threads = length(tau), ...) { +.dmi_ts = \(data, target, tau = 1:10, pred = NULL, k = 3, base = exp(1), + normalize = FALSE, threads = length(tau), ...) { tv = .validate_var(data, target)[[1]] if (is.null(pred)) pred = which(!is.na(tv)) return(RcppDMI(tv, tau, pred, k, 0, base, normalize, threads)) @@ -19,9 +19,6 @@ #' @param rt (optional) Relative distance threshold. #' @param eps (optional) Absolute distance threshold. #' @param threads (optional) Number of threads used. -#' @param higher.parallel (optional) Whether to use a higher level of parallelism. -#' @param detrend (optional) Whether to remove the linear trend. -#' @param nb (optional) Neighbours list. #' @param ... Additional arguments to absorb unused inputs in method dispatch. #' #' @return A vector. From 1c0371b477897bc3ce87bc52ac459c3eb78957e1 Mon Sep 17 00:00:00 2001 From: SpatLyu Date: Wed, 12 Aug 2026 00:00:01 +0800 Subject: [PATCH 11/14] draft r interface for dmi --- R/dmi.R | 16 +++------------- 1 file changed, 3 insertions(+), 13 deletions(-) diff --git a/R/dmi.R b/R/dmi.R index 269c463..b3a04cb 100644 --- a/R/dmi.R +++ b/R/dmi.R @@ -7,19 +7,9 @@ #' Delayed Mutual Information #' -#' @param data Observation data. -#' @param target Integer of column indice for the target variable. -#' @param E (optional) Embedding dimensions. -#' @param k (optional) Number of nearest neighbors used for evaluation. -#' @param tau (optional) Step of lag. -#' @param style (optional) Embedding style (`0` includes current state, `1` excludes it). -#' @param lib (optional) Libraries indices. -#' @param pred (optional) Predictions indices. -#' @param dist.metric (optional) Distance measure to be used. -#' @param rt (optional) Relative distance threshold. -#' @param eps (optional) Absolute distance threshold. -#' @param threads (optional) Number of threads used. -#' @param ... Additional arguments to absorb unused inputs in method dispatch. +#' @inheritParams fnn +#' @param base (optional) Logarithm base of the entropy. +#' @param normalize (optional) Whether to normalize MI values. #' #' @return A vector. #' @export From 2106f1cf2018e58b2de8e679b7a3057d670a1647 Mon Sep 17 00:00:00 2001 From: SpatLyu Date: Wed, 12 Aug 2026 00:00:01 +0800 Subject: [PATCH 12/14] export r interface for dmi --- NAMESPACE | 1 + man/dmi.Rd | 53 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 54 insertions(+) create mode 100644 man/dmi.Rd diff --git a/NAMESPACE b/NAMESPACE index 69178c8..1147e9d 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -5,6 +5,7 @@ S3method(plot,pc_single) S3method(print,pc_boot) S3method(print,pc_ops) S3method(print,pc_single) +exportMethods(dmi) exportMethods(fnn) exportMethods(ops) exportMethods(pc) diff --git a/man/dmi.Rd b/man/dmi.Rd new file mode 100644 index 0000000..9612921 --- /dev/null +++ b/man/dmi.Rd @@ -0,0 +1,53 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/dmi.R +\name{dmi} +\alias{dmi} +\alias{dmi,data.frame-method} +\title{Delayed Mutual Information} +\usage{ +\S4method{dmi}{data.frame}( + data, + target, + tau = 1:10, + pred = NULL, + k = 3, + base = exp(1), + normalize = FALSE, + threads = length(tau), + ... +) +} +\arguments{ +\item{data}{Observation data.} + +\item{target}{Integer of column indice for the target variable.} + +\item{tau}{(optional) Step of lag.} + +\item{pred}{(optional) Predictions indices.} + +\item{k}{(optional) Number of nearest neighbors used for evaluation.} + +\item{base}{(optional) Logarithm base of the entropy.} + +\item{normalize}{(optional) Whether to normalize MI values.} + +\item{threads}{(optional) Number of threads used.} + +\item{...}{Additional arguments to absorb unused inputs in method dispatch.} +} +\value{ +A vector. +} +\description{ +Delayed Mutual Information +} +\examples{ +abun = readr::read_csv(system.file("case/abundance.csv", package = "pc")) +pc::dmi(abun, 2) + +} +\references{ +Fraser, A.M., Swinney, H.L., 1986. Independent coordinates for strange attractors from mutual information. Physical Review A 33, 1134–1140. +Kraskov, A., Stogbauer, H., Grassberger, P., 2004. Estimating mutual information. Physical Review E 69, 066138. +} From 7556447d61a317e4fef84c5d2463ae6da91d9096 Mon Sep 17 00:00:00 2001 From: SpatLyu Date: Wed, 12 Aug 2026 00:00:01 +0800 Subject: [PATCH 13/14] document dmi in news --- NEWS.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/NEWS.md b/NEWS.md index d434908..26fc1d0 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,5 +1,9 @@ # pc 0.3 +## new + +* Provide `dmi` generic for *delayed mutual information* method (#77). + ### enhancements * Clarify output indexing and boundary handling in `fnn` generic (#69). From 4694093885a9bfd9a23acff3da2009221f4534e8 Mon Sep 17 00:00:00 2001 From: SpatLyu Date: Wed, 12 Aug 2026 00:00:01 +0800 Subject: [PATCH 14/14] update docs for dmi --- R/dmi.R | 1 + man/dmi.Rd | 1 + 2 files changed, 2 insertions(+) diff --git a/R/dmi.R b/R/dmi.R index b3a04cb..7112008 100644 --- a/R/dmi.R +++ b/R/dmi.R @@ -17,6 +17,7 @@ #' @aliases dmi,data.frame-method #' @references #' Fraser, A.M., Swinney, H.L., 1986. Independent coordinates for strange attractors from mutual information. Physical Review A 33, 1134–1140. +#' #' Kraskov, A., Stogbauer, H., Grassberger, P., 2004. Estimating mutual information. Physical Review E 69, 066138. #' #' @examples diff --git a/man/dmi.Rd b/man/dmi.Rd index 9612921..bb3bdd4 100644 --- a/man/dmi.Rd +++ b/man/dmi.Rd @@ -49,5 +49,6 @@ pc::dmi(abun, 2) } \references{ Fraser, A.M., Swinney, H.L., 1986. Independent coordinates for strange attractors from mutual information. Physical Review A 33, 1134–1140. + Kraskov, A., Stogbauer, H., Grassberger, P., 2004. Estimating mutual information. Physical Review E 69, 066138. }