-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6f2c254
commit c6a911a
Showing
69 changed files
with
372 additions
and
501 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,138 @@ | ||
#' Plot Total Causality | ||
#' | ||
#' @param x An object | ||
#' @param ... Additional arguments | ||
#' @export | ||
plot_total <- function(x, ...) { | ||
UseMethod("plot_total") | ||
} | ||
|
||
#' Plot Causality Components | ||
#' | ||
#' @param x An object | ||
#' @param ... Additional arguments | ||
#' @export | ||
plot_components <- function(x, ...) { | ||
UseMethod("plot_components") | ||
} | ||
|
||
#' Pattern Causality Fit Object | ||
#' | ||
#' @description Create a pattern causality fit object | ||
#' @param total Total causality strength | ||
#' @param positive Positive causality strength | ||
#' @param negative Negative causality strength | ||
#' @param dark Dark causality strength | ||
#' @return An object of class "pc_fit" | ||
#' @export | ||
pc_fit <- function(total = NULL, positive = NULL, negative = NULL, dark = NULL) { | ||
structure( | ||
list( | ||
total = total, | ||
positive = positive, | ||
negative = negative, | ||
dark = dark | ||
), | ||
class = "pc_fit" | ||
) | ||
} | ||
|
||
#' Print Pattern Causality Results | ||
#' | ||
#' @param x A pc_fit object | ||
#' @param ... Additional arguments passed to print | ||
#' @return Invisibly returns the input object | ||
#' @export | ||
#' @method print pc_fit | ||
print.pc_fit <- function(x, ...) { | ||
cat("Pattern Causality Analysis Results:\n") | ||
cat(sprintf("Total: %.4f\n", x$total)) | ||
cat(sprintf("Positive: %.4f\n", x$positive)) | ||
cat(sprintf("Negative: %.4f\n", x$negative)) | ||
cat(sprintf("Dark: %.4f\n", x$dark)) | ||
invisible(x) | ||
} | ||
|
||
#' Summarize Pattern Causality Results | ||
#' | ||
#' @param object A pc_fit object | ||
#' @param ... Additional arguments passed to summary | ||
#' @return Invisibly returns the input object | ||
#' @export | ||
#' @method summary pc_fit | ||
summary.pc_fit <- function(object, ...) { | ||
cat("Pattern Causality Summary:\n\n") | ||
cat("Causality Strengths:\n") | ||
res <- data.frame( | ||
Type = c("Total", "Positive", "Negative", "Dark"), | ||
Value = c(object$total, object$positive, object$negative, object$dark) | ||
) | ||
print(res) | ||
invisible(object) | ||
} | ||
|
||
#' Plot Total Causality | ||
#' | ||
#' @param x A pc_fit object | ||
#' @param ... Additional arguments passed to plotting functions | ||
#' @return A ggplot object | ||
#' @export | ||
#' @method plot_total pc_fit | ||
plot_total.pc_fit <- function(x, ...) { | ||
df1 <- data.frame( | ||
name = c("Non-causal", "Causal"), | ||
value = c(1 - x$total, x$total) | ||
) | ||
|
||
ggplot2::ggplot(df1, ggplot2::aes(x = name, y = value, fill = name)) + | ||
ggplot2::geom_bar(stat = "identity", width = 0.4) + | ||
ggplot2::scale_fill_manual(values = c("grey80", "grey20")) + | ||
ggplot2::labs(x = "", y = "Strength") + | ||
ggplot2::theme_bw() + | ||
ggplot2::theme(legend.position = "none") | ||
} | ||
|
||
#' Plot Causality Components | ||
#' | ||
#' @param x A pc_fit object | ||
#' @param ... Additional arguments passed to plotting functions | ||
#' @return A ggplot object | ||
#' @export | ||
#' @method plot_components pc_fit | ||
plot_components.pc_fit <- function(x, ...) { | ||
df2 <- data.frame( | ||
name = c("Positive", "Negative", "Dark"), | ||
value = c(x$positive, x$negative, x$dark), | ||
color = c("#5BA3CF", "#F6583E", "#6A51A3") | ||
) | ||
|
||
ggplot2::ggplot(df2, ggplot2::aes(x = name, y = value, fill = name)) + | ||
ggplot2::geom_bar(stat = "identity", width = 0.4) + | ||
ggplot2::scale_fill_manual(values = df2$color) + | ||
ggplot2::labs(x = "", y = "Strength") + | ||
ggplot2::theme_bw() + | ||
ggplot2::theme(legend.position = "none") | ||
} | ||
|
||
#' Plot Pattern Causality Results | ||
#' | ||
#' @param x A pc_fit object | ||
#' @param ... Additional arguments passed to plotting functions | ||
#' @return A list of two ggplot objects | ||
#' @export | ||
#' @method plot pc_fit | ||
plot.pc_fit <- function(x, ...) { | ||
# 创建两个图形对象 | ||
p1 <- plot_total.pc_fit(x) | ||
p2 <- plot_components.pc_fit(x) | ||
|
||
# 使用 gridExtra 包的 arrangeGrob 函数来组合图形 | ||
combined_plot <- gridExtra::arrangeGrob(p1, p2, ncol = 2) | ||
|
||
# 在新的设备上显示组合后的图形 | ||
grid::grid.newpage() | ||
grid::grid.draw(combined_plot) | ||
|
||
# 返回图形对象列表 | ||
invisible(list(total = p1, components = p2)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,63 +1,18 @@ | ||
#' Compute Distance Matrix for an Embedded Matrix | ||
#' @title Calculate Distance Matrix | ||
#' | ||
#' This function computes the distance matrix from a given embedded matrix, `M`, using a specified metric. The distance matrix is essential for exploring the underlying structure in complex systems by quantifying the distances between different points (or states) in the embedded space. This matrix can be crucial for further analysis like clustering, nearest neighbor searches, or causality inference in complex systems. | ||
#' @description This function computes the distance matrix from a given embedded matrix, `M`, using a specified metric. The distance matrix is essential for exploring the underlying structure in complex systems by quantifying the distances between different points (or states) in the embedded space. This matrix can be crucial for further analysis like clustering, nearest neighbor searches, or causality inference in complex systems. | ||
#' | ||
#' @param M Numeric matrix, the embedded state space matrix where each row represents a point in the reconstructed state space of a time series or any multidimensional data. | ||
#' @param metric Character, the distance metric to be used for computing distances. Common metrics include "euclidean", "maximum", "manhattan", "canberra", "binary" or "minkowski". | ||
#' | ||
#' @return An object of class `dist`, representing the distance matrix of the embedded matrix `M`. This distance matrix can optionally be converted to a full matrix format if needed for subsequent analyses. | ||
#' @export | ||
#' @examples | ||
#' # Assume M is an already constructed state space matrix of a time series | ||
#' M <- matrix(rnorm(100), nrow = 10) | ||
#' distanceMat <- distanceMatrix(M, "euclidean") | ||
#' print(distanceMat) # Optionally convert to a full matrix for display | ||
distanceMatrix <- function(M, metric) { | ||
# d <- dist(M, metric, upper = TRUE) | ||
d <- as.matrix(stats::dist(M, metric, upper = T)) | ||
return(d) | ||
} | ||
|
||
#' Calculate Generalized Minkowski Distance Between Two Vectors | ||
#' | ||
#' This function calculates the generalized Minkowski distance of order 'n' between two numeric vectors. This distance is a metric in a normed vector space which generalizes the Euclidean and Manhattan distances. It is used for various data science applications, particularly in clustering, optimization, and outlier detection in complex systems. | ||
#' | ||
#' @param vec1 Numeric vector, the first vector for which the distance will be calculated. | ||
#' @param vec2 Numeric vector, the second vector for which the distance will be calculated. | ||
#' @param n Integer, the order of the Minkowski distance. When n=2, it becomes the Euclidean distance; when n=1, it becomes the Manhattan distance. | ||
#' | ||
#' @return Numeric, the computed Minkowski distance between the two vectors. | ||
#' | ||
#' @export | ||
#' @examples | ||
#' vec1 <- c(1, 2, 3) | ||
#' vec2 <- c(4, 5, 6) | ||
#' n <- 2 | ||
#' distance <- metricDistance(vec1, vec2, n) | ||
#' print(distance) | ||
metricDistance <- function(vec1, vec2, n) { | ||
res <- as.numeric(vec1 - vec2) | ||
distance <- (sum(abs(res)^n))^(1 / n) | ||
return(distance) | ||
} | ||
|
||
#' Calculate Distances Between a Reference Point and Multiple Candidates | ||
#' | ||
#' This function applies the 'metricDistance' function to calculate distances from a given reference point to each row in a matrix of candidate nearest neighbors. It is particularly useful in the situation between matrix and a vector | ||
#' | ||
#' @param point Numeric vector, the reference point from which distances are calculated. | ||
#' @param candidateNNs Matrix, rows represent candidate points to which distance from the reference point is calculated. | ||
#' @param n Integer, the order of the Minkowski distance to use. | ||
#' | ||
#' @return Numeric vector, distances from the reference point to each of the candidate points. | ||
#' | ||
#' @export | ||
#' @examples | ||
#' point <- c(1, 2, 3) | ||
#' candidateNNs <- matrix(c(4, 5, 6, 7, 8, 9), nrow = 2, byrow = TRUE) | ||
#' n <- 2 | ||
#' distances <- distanceVector(point, candidateNNs, n) | ||
#' print(distances) | ||
distanceVector <- function(point, candidateNNs, n) { | ||
apply(X = candidateNNs, MARGIN = 1, FUN = metricDistance, vec2 = point, n = n) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.