Skip to content

Commit

Permalink
Return a custom S3 list object and use custom print method for oruns_…
Browse files Browse the repository at this point in the history
…test
  • Loading branch information
adamhsparks committed Jun 23, 2024
1 parent c427e0e commit 14a2815
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 16 deletions.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# Generated by roxygen2: do not edit by hand

S3method(print,r4pde.oruns_test)
export(AFSD)
export(BPL)
export(CompMuCens)
Expand Down
52 changes: 37 additions & 15 deletions R/oruns_test.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,20 @@
#' Perform a runs test on the input data to test for clustering or randomness.
#'
#' @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))
#'
#' @importFrom stats pnorm
#' @return an `r4pde.oruns` object.
#'
#' An `r4pde.oruns` object is a `list` containing:
#' * U, number of runs,
#' * EU, expected number of runs,
#' * sU, standard deviation of the expected number of runs
#' * Z, Z-score of the observed number of runs,
#' * pvalue, the p-value of the Z-score, and
#' * result, the test result of either "aggregation or clustering" or "randomness"
#'
#' @export
oruns_test <- function(x) {
# S is the input data
Expand Down Expand Up @@ -37,18 +46,31 @@ oruns_test <- function(x) {
result <- ifelse(Z < -1.64,
c("aggregation or clustering"),
c("randomness"))
message <- paste(
"Ordinary Runs Test of Data Sequence:\n",
"-------------------------------------\n",
sprintf("Total Number of Runs (U): %d\n", U),
sprintf("Expected Number of Runs (EU): %.2f\n", EU),
sprintf("Standard Deviation of Runs (sU): %.2f\n", sU),
sprintf("Z-score: %.2f\n", Z),
sprintf("P-value: %.4f\n\n", pvalue),
"Interpretation:\n",
sprintf("Based on the Z-score, the sequence exhibits '%s'.\n", result)

)

cat(message)

out <- list(U = U, EU = EU, sU = sU, Z = Z, pvalue = pvalue, result = result)
class(out) <- union("r4pde.oruns_test", class(out))
return(out)
}

#' Prints r4pde.oruns Object
#'
#' Custom [print()] method for `r4pde.oruns_test` objects.
#'
#' @param x a `r4pde.oruns_test` object
#' @param ... ignored
#' @export
#' @noRd

print.r4pde.oruns_test <- function(x, ...) {
cat("Ordinary Runs Test of Data Sequence:\n")
cat("-------------------------------------\n")
cat(sprintf("Total Number of Runs (U): %d\n", x[[1]]))
cat(sprintf("Expected Number of Runs (EU): %.2f\n", x[[2]]))
cat(sprintf("Standard Deviation of Runs (sU): %.2f\n", x[[3]]))
cat(sprintf("Z-score: %.2f\n", x[[4]]))
cat(sprintf("P-value: %.4f\n\n", x[[4]]))
cat("Interpretation:\n")
cat(sprintf("Based on the Z-score, the sequence exhibits '%s'.\n", x[[6]]))

invisible(x)
}
12 changes: 11 additions & 1 deletion man/oruns_test.Rd

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

0 comments on commit 14a2815

Please sign in to comment.