Skip to content

Commit

Permalink
function join_count
Browse files Browse the repository at this point in the history
  • Loading branch information
emdelponte committed Oct 17, 2023
1 parent 0ced717 commit 1038870
Show file tree
Hide file tree
Showing 12 changed files with 463 additions and 15 deletions.
3 changes: 2 additions & 1 deletion NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ export(DSI)
export(DSI2)
export(doublets.test)
export(fit_gradients)
export(oruns.test)
export(join_count)
export(oruns_test)
export(plot_AFSD)
export(theme_r4pde)
importFrom(dplyr,"%>%")
Expand Down
117 changes: 117 additions & 0 deletions R/join_count.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@

#' Test for Spatial Join Count Statistics
#'
#' @description
#' The function `join_count` calculates spatial statistics for a matrix.
#' It identifies patterns of aggregation for values in a binary matrix based on
#' join count statistics. The results determine whether the observed spatial
#' arrangement is aggregated, random, or uniform regarding the presence of 1s
#' ("D") and 0s ("H").
#'
#' @param matrix_data A binary matrix (with elements 0 and 1) representing the
#' spatial distribution of two types of points. Rows and columns of the matrix
#' indicate coordinates while the 0s and 1s represent different categories of
#' observations.
#'
#' @return A list containing several statistical measures:
#' \itemize{
#' \item Observed_01_10: The number of observed 01 or 10 sequences.
#' \item Observed_11: The number of observed 11 sequences.
#' \item Expected_ER_01: The expected number of 01 or 10 sequences under
#' random distribution.
#' \item Expected_ER_11: The expected number of 11 sequences under random
#' distribution.
#' \item sR_01: Standard deviation for 01 or 10 sequences.
#' \item sR_11: Standard deviation for 11 sequences.
#' \item ZHD: The Z-score for 01 or 10 sequences.
#' \item ZDD: The Z-score for 11 sequences.
#' \item PatternHD: Descriptive result for 01 or 10 sequences - "Aggregated"
#' or "Not Aggregated".
#' \item PatternDD: Descriptive result for 11 sequences - "Aggregated" or
#' "Not Aggregated".
#' }
#'
#' @details
#' The function first calculates the count of specific binary sequences in the
#' matrix both horizontally and vertically. It then computes expected values
#' and standard deviations based on the spatial arrangement dimensions and uses
#' these for Z-score calculations to identify the pattern of the matrix.
#'
#' @examples
#' matrix_data <- matrix(c(1,1,1,0,0,
#' 1,1,1,0,0,
#' 1,1,1,0,0,
#' 1,1,1,0,0,
#' 0,0,0,0,0), ncol = 5, byrow = TRUE)
#' join_count(matrix_data)
#'
#' @export
join_count <- function(matrix_data) {

count_sequences <- function(matrix_data) {
count_01_10 <- 0 # previously count_HD_DH
count_11 <- 0 # previously count_DD

for (i in 1:nrow(matrix_data)) {
for (j in 1:(ncol(matrix_data)-1)) {
if ((matrix_data[i,j] == 0 && matrix_data[i,j+1] == 1) ||
(matrix_data[i,j] == 1 && matrix_data[i,j+1] == 0)) {
count_01_10 <- count_01_10 + 1
} else if (matrix_data[i,j] == 1 && matrix_data[i,j+1] == 1) {
count_11 <- count_11 + 1
}
}
}

for (j in 1:ncol(matrix_data)) {
for (i in 1:(nrow(matrix_data)-1)) {
if ((matrix_data[i,j] == 0 && matrix_data[i+1,j] == 1) ||
(matrix_data[i,j] == 1 && matrix_data[i+1,j] == 0)) {
count_01_10 <- count_01_10 + 1
} else if (matrix_data[i,j] == 1 && matrix_data[i+1,j] == 1) {
count_11 <- count_11 + 1
}
}
}

return(list(One_Zero_or_Zero_One = count_01_10, One_One = count_11))
}

results <- count_sequences(matrix_data)

# Calculate matrix dimensions and S-values
num_rows <- nrow(matrix_data)
num_cols <- ncol(matrix_data)

S0 <- 2 * ((2*num_rows * num_cols) - num_rows - num_cols)
S1 <- 2 * S0
S2 <- 8 * ((8*num_rows * num_cols) - (7 * num_rows) - (7 * num_cols) + 4)

# Calculate y as the mean of the numeric matrix
y <- mean(matrix_data)

# Calculate expected values and standard deviations
ER_01 <- S0 * y * (1 - y) # Expected random 01 or 10 sequences
sR_01 <- sqrt(S2 * y * (1 - y) + 4 * (S1 - S2) * y^2 * (1 - y)^2) / 2 # Std dev

ER_11 <- S0 * y^2 / 2 # Expected random 11 sequences
sR_11 <- sqrt(S1 * y^2 + (S2 - 2 * S1) * y^3 + (S1 - S2) * y^4) / 2 # Std dev

# Calculate Z value
ZHD <- (results$One_Zero_or_Zero_One - ER_01 - 0.5) / sR_01

pattern1 <- ifelse(ZHD < -1.64, "Aggregated", "Not Aggregated")

ZDD <- (results$One_One - ER_11 + 0.5) / sR_11

pattern2 <- ifelse(ZDD > 1.64, "Aggregated", "Not Aggregated")

# Return results
return(list(Observed_01_10 = results$One_Zero_or_Zero_One, Observed_11 = results$One_One,
Expected_ER_01 = ER_01, Expected_ER_11 = ER_11,
sR_01 = sR_01, sR_11 = sR_11,
ZHD = ZHD, ZDD = ZDD, PatternHD = pattern1, PatternDD = pattern2))
}



4 changes: 2 additions & 2 deletions R/oruns.test.R → R/oruns_test.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
#' @param x A numeric vector representing the input data
#' @return A list with elements U (number of runs), EU (expected number of runs), pvalue, and result (either "clustering" or "randomness").
#' @examples
#' oruns.test(c(1, 0, 1, 1, 0, 1, 0, 0, 1, 1))
#' oruns_test(c(1, 0, 1, 1, 0, 1, 0, 0, 1, 1))
#'
#' @importFrom stats pnorm
#' @export
oruns.test <- function(x) {
oruns_test <- function(x) {
# S is the input data
S <- x

Expand Down
2 changes: 1 addition & 1 deletion docs/pkgdown.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@ pandoc: 2.19.2
pkgdown: 2.0.7
pkgdown_sha: ~
articles: {}
last_built: 2023-10-14T16:07Z
last_built: 2023-10-17T01:04Z

15 changes: 11 additions & 4 deletions docs/reference/DidymellaWatermelon.html

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

7 changes: 6 additions & 1 deletion docs/reference/index.html

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

Loading

0 comments on commit 1038870

Please sign in to comment.