Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
4770559
Add matched-null cluster test
haomeng797-ship-it Aug 22, 2026
f08d6d5
Apply repository formatting
haomeng797-ship-it Aug 22, 2026
e0bf6a6
Merge branch 'main' into codex/test-clusters
strengejacke Aug 25, 2026
c9bb0d2
Bump development version
haomeng797-ship-it Aug 25, 2026
37a4e19
Merge branch 'main' into codex/test-clusters
strengejacke Aug 31, 2026
9e49823
Merge branch 'main' into codex/test-clusters
strengejacke Sep 1, 2026
bcbc558
Merge branch 'main' into codex/test-clusters
strengejacke Sep 1, 2026
6f08ca3
Merge branch 'main' into codex/test-clusters
strengejacke Sep 9, 2026
c153639
Merge branch 'main' into codex/test-clusters
strengejacke Sep 10, 2026
3c3b598
Merge branch 'main' into codex/test-clusters
strengejacke Sep 10, 2026
0dc405a
Validate cluster counts, add print method, clarify docs
haomeng797-ship-it Sep 25, 2026
e43e18b
Add new words to WORDLIST
haomeng797-ship-it Sep 25, 2026
bd8859c
Clarify what the twins reproduce in test_clusters docs
haomeng797-ship-it Sep 25, 2026
3a5dcc2
Merge branch 'main' into codex/test-clusters
strengejacke Sep 27, 2026
fb68108
Merge branch 'main' into codex/test-clusters
strengejacke Sep 28, 2026
c9dabf7
Merge branch 'main' into codex/test-clusters
strengejacke Oct 13, 2026
fd27419
Merge branch 'main' into codex/test-clusters
strengejacke Oct 17, 2026
d1e79a7
Merge branch 'main' into codex/test-clusters
strengejacke Oct 30, 2026
9104570
require matchednull 0.2.2 now that it's on CRAN
haomeng797-ship-it Nov 5, 2026
0577ce4
Merge branch 'main' into codex/test-clusters
strengejacke Nov 23, 2026
a440583
Reject one-column and constant-column input, tighten integer checks
haomeng797-ship-it Nov 23, 2026
b412ddb
Merge branch 'main' into codex/test-clusters
strengejacke Nov 24, 2026
844c539
Update package version to 0.20.0.10
strengejacke Nov 24, 2026
f479152
Merge branch 'main' into codex/test-clusters
strengejacke Nov 24, 2026
36aedc8
Add test_clusters function for cluster comparison
strengejacke Nov 24, 2026
506e818
Remove Changes section from NEWS.md
strengejacke Nov 24, 2026
f1650ff
Update NEWS.md to remove deprecated function details
strengejacke Nov 24, 2026
a524012
Enhance model checking functions and overdispersion handling
strengejacke Nov 24, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Type: Package
Package: performance
Title: Assessment of Regression Models Performance
Version: 0.20.0.14
Version: 0.20.0.15
Authors@R:
c(person(given = "Daniel",
family = "Lüdecke",
Expand Down Expand Up @@ -97,6 +97,7 @@ Suggests:
brms,
car,
carData,
cluster,
CompQuadForm,
correlation (>= 0.8.8),
cplm,
Expand Down Expand Up @@ -130,6 +131,7 @@ Suggests:
loo,
MASS,
Matrix,
matchednull (>= 0.2.2),
mclogit,
mclust,
metadat,
Expand Down
2 changes: 2 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -383,6 +383,7 @@ S3method(print,r2_mlm)
S3method(print,r2_nakagawa)
S3method(print,r2_nakagawa_by_group)
S3method(print,r2_pseudo)
S3method(print,test_clusters)
S3method(print,test_likelihoodratio)
S3method(print,test_performance)
S3method(print_html,check_group_variation)
Expand Down Expand Up @@ -701,6 +702,7 @@ export(r2_zeroinflated)
export(rmse)
export(simulate_residuals)
export(test_bf)
export(test_clusters)
export(test_likelihoodratio)
export(test_lrt)
export(test_performance)
Expand Down
7 changes: 7 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@

## Changes

* New function `test_clusters()` uses the `matchednull` package to compare a
selected cluster count with the counts obtained from matched-null data that
keep the observed margins and approximately the observed correlations. It
reports one of three outcomes (no detected excess, departure from the
reference model, below the reference range) and validates the return values
of the clustering function.

* The `ci` argument in `binned_residuals()` is now recognized for
`ci_method = "exact"`.

Expand Down
327 changes: 327 additions & 0 deletions R/test_clusters.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,327 @@
#' Test a cluster count against matched-null data
#'
#' `test_clusters()` runs a cluster-count selector on the observed data and on
#' `iterations` matched-null twins of those data, and compares the observed
#' count with the counts the twins produce. The twins keep the observed values
#' of every variable exactly and reproduce the observed correlations
#' approximately, but contain no cluster structure by construction. The test
#' reports one of three outcomes: no detected excess, a departure from the
#' reference model, or a count below the reference range. The comparison is
#' implemented with [`matchednull::matched_null_test()`]. It asks a different
#' question from [`check_clusterstructure()`], which evaluates clustering
#' tendency against a spatial-uniformity null.
#'
#' @param x A numeric matrix or data frame with at least two columns, no
#' constant columns, and no missing or infinite values.
#' @param cluster_function A function that takes a numeric matrix and returns a
#' single finite number, typically the selected number of clusters. By
#' default, `mclust::Mclust()` selects the number of mixture components by
#' BIC. The function is called once on the observed data and once on every
#' twin, and its return value is checked each time (see 'Details').
#' @param iterations Number of matched-null twins to evaluate.
#' @param n_max Maximum number of clusters considered by the default
#' `mclust::Mclust()` pipeline. Ignored when `cluster_function` is supplied.
#' @param standardize Logical. If `TRUE`, variables are standardized before the
#' observed and matched-null cluster counts are computed.
#' @param ... Additional arguments passed to
#' [`matchednull::matched_null_test()`], such as `copula`, `df`, `probs`,
#' `ridge`, or `parallel`.
#'
#' @return An object of class `"test_clusters"` and `"matched_null_test"`: a
#' list with the observed statistic (`real`), the twin statistics (`null`),
#' the reference interval (`interval`), the Monte Carlo exceedance
#' probability (`p_exceed`), the interval verdict (`within`), the number of
#' twins (`R`), and the copula family (`copula`, with `df` for the t copula).
#' The attribute `"outcome"` is `"within"`, `"above"`, or `"below"`: the
#' position of the observed statistic relative to the reference interval.
#' The attributes `"standardize"` and `"n_max"` record the preprocessing and
#' the maximum number of components of the default pipeline (`n_max` is
#' `NULL` when `cluster_function` is supplied).
#'
#' @details
#' The default cluster-count pipeline fits Gaussian mixture models with one to
#' `n_max` components using `mclust::Mclust()` and returns the BIC-selected
#' number of components. A user-supplied `cluster_function` receives the same
#' preprocessed data matrix as the default pipeline. Its return value is
#' checked on the observed data and on every twin: it must be a single finite
#' number, and any other value (`NA`, a character string, a factor, a logical
#' value, a vector of length two, `NULL`) is an error. The check runs here, on
#' the raw return value, so that the error names `cluster_function` and what it
#' returned.
#'
#' ## Reference model
#'
#' Each twin is drawn from a Gaussian copula whose latent correlation matrix
#' is the observed correlation matrix. Every column of a twin contains exactly
#' the observed values of that variable, rearranged in the rank order of the
#' copula draw. The margins are therefore reproduced exactly, but the
#' correlations only approximately: non-normal margins change the Pearson
#' correlations of the output relative to the latent correlation matrix. With
#' `copula = "t"`, the draw comes from a t copula instead, a sensitivity
#' variant with heavier-tailed dependence. Rerunning with `copula = "t"` shows
#' whether the outcome changes under that reference model; it does not by
#' itself establish or rule out clusters.
#'
#' ## Outcomes
#'
#' The observed count is compared with the interval between the `probs`
#' quantiles of the twin counts (by default the 2.5% and 97.5% quantiles).
#'
#' - A count above the interval is a departure from the specified reference
#' model. The test does not settle what kind of departure it is, and the
#' result is not evidence that clusters exist: typeless data with joint
#' asymmetry, for example, can exceed the reference model as well.
#' - A count inside the interval is no detected excess for the statistic that
#' was compared, the cluster count. It is not evidence that there are no
#' clusters, and it does not establish that the joint distribution belongs
#' to the reference family.
#' - A count below the interval is a diagnostic signal, not a verdict. It
#' calls for checking the pipeline and the reference before any
#' interpretation: whether the fits converged, whether the twins reproduce
#' the intended margins and correlations, and whether data and twins were
#' compared at the same sample size with the same statistic.
#'
#' `p_exceed` is a Monte Carlo exceedance probability under a reference model
#' fitted to the same data. It equals `(1 + k) / (iterations + 1)`, where `k`
#' is the number of twins whose count is at least the observed count. It is
#' not an exact finite-sample p-value.
#'
#' ## Before running the test
#'
#' Inspect the margins for pronounced multimodality first, for example with
#' [`check_multimodal()`] on single variables. Groups that differ only in
#' their means are detected at moderate separations. At large separations the
#' separation shows in the margins themselves. The twins keep the margins
#' exactly, so the pipeline can select as many clusters in the twins (which
#' contain no groups) as in the data, and the comparison may then report no
#' detected excess. This behaviour was observed in the simulation
#' constructions of Meng (2026); it is not asserted as a general rule. Whether
#' the joint distribution has clustering tendency is a different question
#' again; see [`check_clusterstructure()`], which uses a spatial-uniformity
#' null.
#'
#' @examplesIf all(insight::check_if_installed(c("matchednull", "mclust", "cluster"), quietly = TRUE))
#' \donttest{
#' set.seed(42)
#' test_clusters(iris[, 1:4], iterations = 19, n_max = 4)
#'
#' # Any pipeline that returns one number can be tested, for example k-means
#' # with the number of clusters chosen by the mean silhouette width.
#' select_k <- function(data) {
#' ks <- 2:5
#' sil <- vapply(ks, function(k) {
#' cl <- stats::kmeans(data, centers = k, nstart = 5)$cluster
#' mean(cluster::silhouette(cl, stats::dist(data))[, "sil_width"])
#' }, numeric(1))
#' ks[which.max(sil)]
#' }
#' test_clusters(iris[, 1:4], cluster_function = select_k, iterations = 19)
#' }
#'
#' @references
#' - Meng, M. (2026). matchednull: Matched-null tests for cluster-count claims.
#' R package version 0.2.2. <https://CRAN.R-project.org/package=matchednull>
#'
#' - Meng, M. (2026). Types Without Taxa: A Covariance-Matched-Null Multiverse
#' Test of Categorical Versus Continuous Personality Structure.
#' Preregistration, Open Science Framework. \doi{10.17605/OSF.IO/2EKCG}
#'
#' @seealso [`matchednull::matched_null_test()`],
#' [`check_clusterstructure()`], [`check_multimodal()`]
#' @export
test_clusters <- function(
x,
cluster_function = NULL,
iterations = 200,
n_max = 10,
standardize = TRUE,
...
) {
insight::check_if_installed("matchednull", minimum_version = "0.2.2")

if (!is.logical(standardize) || length(standardize) != 1L || is.na(standardize)) {
insight::format_error("`standardize` must be `TRUE` or `FALSE`.")
}
if (!is.null(cluster_function) && !is.function(cluster_function)) {
insight::format_error("`cluster_function` must be a function or `NULL`.")
}
uses_default <- is.null(cluster_function)
if (!.is_positive_integer(iterations)) {
insight::format_error("`iterations` must be a positive integer.")
}
if (uses_default && !.is_positive_integer(n_max)) {
insight::format_error("`n_max` must be a positive integer.")
}

x <- .validate_test_clusters_data(x)
x <- .standardize_test_clusters_data(x, standardize)

if (uses_default) {
insight::check_if_installed("mclust")
# `mclust::Mclust()` rewrites its call to `mclustBIC` and evaluates it in
# the caller's frame, so the symbol must be visible from this function
# when mclust is not attached. Do not remove.
mclustBIC <- mclust::mclustBIC
max_components <- min(as.integer(n_max), nrow(x) - 1L)
cluster_function <- function(data) {
mclust::Mclust(
data,
G = seq_len(max_components),
verbose = FALSE
)$G
}
}

out <- matchednull::matched_null_test(
x,
cluster_fn = .checked_cluster_function(cluster_function),
R = as.integer(iterations),
...
)

attr(out, "standardize") <- standardize
attr(out, "n_max") <- if (uses_default) max_components else NULL
attr(out, "outcome") <- .test_clusters_outcome(out$real, out$interval)
class(out) <- c("test_clusters", class(out))
out
}


#' @export
print.test_clusters <- function(x, digits = 3, ...) {
outcome <- .test_clusters_outcome(x$real, x$interval)
outcome_text <- switch(
outcome,
within = "No detected excess: the count lies within the reference interval.",
above = paste0(
"Departure from the reference model: the count exceeds the interval.\n",
"The test does not settle what kind of departure."
),
below = "Below the reference range: a diagnostic signal, not a verdict."
)
outcome_color <- switch(outcome, within = "green", above = "yellow", below = "yellow")

reference <- if (is.null(x$copula) || x$copula == "gaussian") {
"Gaussian copula"
} else {
sprintf(
"t copula (df = %s)",
insight::format_value(x$df, digits = digits, protect_integers = TRUE)
)
}

interval <- sprintf(
"[%s]",
toString(insight::format_value(x$interval, digits = digits, protect_integers = TRUE))
)
if (!is.null(names(x$interval))) {
interval <- sprintf(
"%s (%s quantiles of the twins)",
interval,
paste(names(x$interval), collapse = " and ")
)
}

insight::print_color("# Matched-null test of the cluster count\n\n", "blue")
# the exceedance counts twins whose statistic is at least the observed one
cat(sprintf(
" Observed statistic: %s\n",
insight::format_value(x$real, digits = digits, protect_integers = TRUE)
))
cat(sprintf(" Reference interval: %s\n", interval))
cat(sprintf(
" MC exceedance (twins >= observed): %s\n",
insight::format_value(x$p_exceed, digits = digits)
))
cat(sprintf(
" Reference model: %s, %s matched-null twins\n",
reference,
insight::format_value(x$R, protect_integers = TRUE)
))
cat("\n")
insight::print_color(paste0(outcome_text, "\n"), outcome_color)
invisible(x)
}


# matchednull calls the clustering function once on the observed data and once
# per twin. The wrapper checks the raw return value itself, before any
# coercion, so that the error speaks of `cluster_function`.
.checked_cluster_function <- function(cluster_function) {
function(data) {
value <- cluster_function(data)
if (!is.numeric(value) || length(value) != 1L || !is.finite(value)) {
insight::format_error(sprintf(
"`cluster_function` must return a single finite number, but returned %s.",
.describe_cluster_value(value)
))
}
unname(as.numeric(value))
}
}


.describe_cluster_value <- function(value) {
if (is.numeric(value) && length(value) == 1L) {
# a number that is not finite: NA, NaN, Inf or -Inf
return(paste0("`", format(value), "`"))
}
sprintf("%s of length %d", class(value)[1], length(value))
}


.test_clusters_outcome <- function(real, interval) {
if (real > interval[2]) {
"above"
} else if (real < interval[1]) {
"below"
} else {
"within"
}
}


.validate_test_clusters_data <- function(x) {
if (!is.matrix(x) && !is.data.frame(x)) {
insight::format_error("`x` must be a numeric matrix or data frame.")
}
if (is.data.frame(x) && !all(vapply(x, is.numeric, logical(1)))) {
insight::format_error("All columns in `x` must be numeric.")
}

x <- as.matrix(x)
if (!is.numeric(x)) {
insight::format_error("`x` must contain only numeric values.")
}
if (nrow(x) < 2L || ncol(x) < 2L) {
insight::format_error("`x` must contain at least two rows and two columns.")
}
if (anyNA(x) || !all(is.finite(x))) {
insight::format_error("`x` must not contain missing or infinite values.")
}
# the twins are built from the correlation matrix, which is undefined for a
# constant column whether or not the data are standardized
column_sd <- apply(x, 2L, stats::sd)
if (!all(is.finite(column_sd)) || any(column_sd == 0)) {
insight::format_error("`x` cannot contain constant columns.")
}
x
}


.standardize_test_clusters_data <- function(x, standardize) {
if (!standardize) {
return(x)
}
scale(x)
}


.is_positive_integer <- function(x) {
is.numeric(x) &&
length(x) == 1L &&
is.finite(x) &&
x >= 1L &&
x <= .Machine$integer.max &&
x %% 1 == 0
}
Loading
Loading