Skip to content

Commit 4cf8d85

Browse files
authored
Merge pull request #45 from SpatLyu/dev
export surd generic
2 parents 77eed7c + 705352d commit 4cf8d85

7 files changed

Lines changed: 178 additions & 4 deletions

File tree

DESCRIPTION

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ LinkingTo:
1919
Rcpp,
2020
RcppThread,
2121
Imports:
22+
methods,
2223
sdsfun,
2324
sf,
2425
terra
@@ -27,4 +28,6 @@ Suggests:
2728
NlinTS,
2829
Rcpp,
2930
RcppThread,
30-
readr
31+
readr,
32+
spEDM,
33+
tEDM

NAMESPACE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,5 @@ export(entropy)
77
export(je)
88
export(mi)
99
export(te)
10+
exportMethods(surd)
1011
useDynLib(infoxtr, .registration = TRUE)

R/Agenerics.R

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
register_generic = \(name, def = NULL) {
2+
if (!methods::isGeneric(name)){
3+
if (is.null(def)) {
4+
def = eval(bquote(function(data, ...) standardGeneric(.(name))))
5+
}
6+
methods::setGeneric(name, def)
7+
}
8+
}
9+
10+
for (gen in c("surd")) {
11+
register_generic(gen)
12+
}

R/surd.R

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
.surd_ts = \(data, target, agent, lag = 1, bin = 5, method = "equal",
2+
max.combs = 3, threads = 1, base = 2.0, normalize = TRUE) {
3+
mat = .convert2mat(data)
4+
return(RcppSURD(mat, target, agent, lag, bin, max.combs,
5+
threads, base, normalize, method))
6+
}
7+
8+
.surd_lattice = \(data, target, agent, lag = 1, bin = 5, method = "equal",
9+
max.combs = 3, threads = 1, base = 2.0, normalize = TRUE, nb = NULL) {
10+
if (is.null(nb)) nb = sdsfun::spdep_nb(data)
11+
mat = .convert2mat(data)
12+
return(RcppSURD(mat, target, agent, lag, bin, max.combs,
13+
threads, base, normalize, method, nb))
14+
}
15+
16+
.surd_grid = \(data, target, agent, lag = 1, bin = 5, method = "equal",
17+
max.combs = 3, threads = 1, base = 2.0, normalize = TRUE) {
18+
mat = .convert2mat(data)
19+
return(RcppSURD(mat, target, agent, lag, bin, max.combs,
20+
threads, base, normalize, method, NULL, terra::nrow(data[[1]])))
21+
}
22+
23+
#' SURD
24+
#'
25+
#' Synergistic-Unique-Redundant Decomposition of causality
26+
#'
27+
#' @note SURD only support numeric data.
28+
#'
29+
#' @inheritParams te
30+
#' @param lag (optional) Lag of the agent variables.
31+
#' @param bin (optional) Number of discretization bins.
32+
#' @param method (optional) Discretization method. One of
33+
#' `"sd"`, `"equal"`, `"geometric"`, `"quantile"`,
34+
#' `"natural("jenks")"`, or `"headtail"("headtails")`.
35+
#' @param max.combs (optional) Maximum combination order.
36+
#' @param threads (optional) Number of threads used.
37+
#' @param nb (optional) Neighbours list.
38+
#'
39+
#' @return A list.
40+
#' \describe{
41+
#' \item{vars}{Character vector indicating the variable combination associated with each information component.}
42+
#' \item{types}{Character vector indicating the information type of each component.}
43+
#' \item{values}{Numeric vector giving the magnitude of each information component.}
44+
#' }
45+
#'
46+
#' @export
47+
#' @name surd
48+
#' @aliases surd,data.frame-method
49+
#' @references
50+
#' Martinez-Sanchez, A., Arranz, G. & Lozano-Duran, A. Decomposing causality into its synergistic, unique, and redundant components. Nat Commun 15, 9296 (2024).
51+
#'
52+
#' @examples
53+
#' columbus = sf::read_sf(system.file("case/columbus.gpkg", package="spEDM"))
54+
#' infoxtr::surd(columbus, 1, 2:3)
55+
#'
56+
methods::setMethod("surd", "data.frame", .surd_ts)
57+
58+
#' @rdname surd
59+
methods::setMethod("surd", "sf", .surd_lattice)
60+
61+
#' @rdname surd
62+
methods::setMethod("surd", "SpatRaster", .surd_grid)

_pkgdown.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ reference:
3232
- subtitle: Information flow
3333
contents:
3434
- te
35+
- surd
3536

3637
- title: Miscellaneous Utility Functions
3738
contents:

man/surd.Rd

Lines changed: 97 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/SURD.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,11 +71,9 @@ Rcpp::List RcppSURD(const Rcpp::NumericMatrix& mat,
7171
);
7272
for (size_t j = 0; j < ag.size(); ++j)
7373
{
74-
size_t col_id = ag[j];
75-
7674
for (size_t r = 0; r < n_obs; ++r)
7775
{
78-
cppMat[r][col_id] = mat(r, col_id);
76+
cppMat[r][j] = mat(r, ag[j]);
7977
}
8078
}
8179

0 commit comments

Comments
 (0)