ecotyper.rcompat.stats

R base/stats numerics reproduced to the last bit where practical.

R accumulates sum/mean/colMeans in long double; on Apple silicon (the reference platform) that is the same as double so a plain sequential loop reproduces R exactly. cov/cor/var follow src/library/stats/src/cov.c (two-pass means, FMA-contracted cross products as compiled by clang on arm64).

Functions

apply_mean_rows(X[, na_rm])

apply(X, 1, mean[, na.rm=TRUE]).

col_apply_mean(X[, na_rm])

apply(X, 2, mean[, na.rm = TRUE]).

col_means(X)

col_normalize(X[, na_rm])

apply(X, 2, function(x) x / sum(x[, na.rm = TRUE])).

cor_matrix(X)

cor(X): Pearson correlation between the columns of X.

cor_vectors(x, y)

cor(x, y) for two numeric vectors (cov_na_2 path).

fast_cor(X)

HiClimR::fastCor(X): correlation between the columns of X via crossprod of centred, unit-norm columns (BLAS; ULP-level differences).

log2p1(X)

log2(X + 1) with libm's log2 (as R calls it), not numpy's SIMD log2.

p_adjust_bh(p)

p.adjust(p, "BH") (NAs kept; n = number of non-NA p-values).

phyper_upper(q, m, n, k)

1 - phyper(q, m, n, k) as computed by EcoTyper.

pnorm(x[, lower_tail])

pnorm(x, lower.tail) (standard normal, R's algorithm).

pt(t, df[, lower_tail])

pt(t, df) following R's formulation (pbeta via scipy).

quantile7(x, probs[, na_rm])

quantile(x, probs, type = 7, names = FALSE).

r_mad(x)

mad(x) (constant 1.4826, center = median).

r_mean(x[, na_rm])

mean(x) (two-pass, as in summary.c).

r_median(x)

median(x) (mean(sort(x, partial = half + 0:1)[half + 0:1])).

r_sd(x)

r_sum(x)

sum(x) (sequential accumulation).

r_var(x)

var(x) for a numeric vector.

rank_average(x)

rank(x, ties.method = "average") (no NAs).

row_means(X)

rowMeans(X) (column-by-column accumulation; same per-row order).

row_ranks(X)

Per-row average ranks and tie sums of X for row_wilcoxon_from_ranks().

row_var(X)

apply(X, 1, var).

row_wilcoxon_from_ranks(ranks, ties, in_x[, ...])

row_wilcoxon_twosample(X[:, in_x], X[:, ~in_x]) from row_ranks() of X: identical results (the ranks of a row do not depend on how its columns are split, and the rank sum runs over the x columns in their original order), without re-ranking X for every split.

row_wilcoxon_twosample(x, y[, correct])

p-values of matrixTests::row_wilcoxon_twosample(x, y) (two-sided).

scale_rows(X)

t(apply(X, 1, scale)).

scale_vec(x)

as.vector(scale(x)).

welch_t_test_p(x, y)

t.test(x, y)$p.value (Welch, two-sided); raises TTestError where R errors.

which_max(x)

which.max(x) → 0-based index of the first maximum ignoring NaN; -1 if none.

Exceptions

TTestError

Raised where R's t.test() stops (caught by EcoTyper's tryCatch).

ecotyper.rcompat.stats.r_sum(x)[source]

sum(x) (sequential accumulation).

Return type:

float

ecotyper.rcompat.stats.r_mean(x, na_rm=False)[source]

mean(x) (two-pass, as in summary.c).

Parameters:

na_rm (bool)

Return type:

float

ecotyper.rcompat.stats.apply_mean_rows(X, na_rm=False)[source]

apply(X, 1, mean[, na.rm=TRUE]).

Parameters:

na_rm (bool)

Return type:

ndarray

ecotyper.rcompat.stats.col_means(X)[source]
Return type:

ndarray

ecotyper.rcompat.stats.row_means(X)[source]

rowMeans(X) (column-by-column accumulation; same per-row order).

Return type:

ndarray

ecotyper.rcompat.stats.r_var(x)[source]

var(x) for a numeric vector.

Return type:

float

ecotyper.rcompat.stats.r_sd(x)[source]
Return type:

float

ecotyper.rcompat.stats.row_var(X)[source]

apply(X, 1, var).

Return type:

ndarray

ecotyper.rcompat.stats.cor_matrix(X)[source]

cor(X): Pearson correlation between the columns of X.

Return type:

ndarray

ecotyper.rcompat.stats.cor_vectors(x, y)[source]

cor(x, y) for two numeric vectors (cov_na_2 path).

Return type:

float

ecotyper.rcompat.stats.fast_cor(X)[source]

HiClimR::fastCor(X): correlation between the columns of X via crossprod of centred, unit-norm columns (BLAS; ULP-level differences).

Return type:

ndarray

ecotyper.rcompat.stats.scale_rows(X)[source]

t(apply(X, 1, scale)).

Return type:

ndarray

ecotyper.rcompat.stats.scale_vec(x)[source]

as.vector(scale(x)).

Return type:

ndarray

ecotyper.rcompat.stats.log2p1(X)[source]

log2(X + 1) with libm’s log2 (as R calls it), not numpy’s SIMD log2.

Return type:

ndarray

ecotyper.rcompat.stats.col_normalize(X, na_rm=False)[source]

apply(X, 2, function(x) x / sum(x[, na.rm = TRUE])).

Parameters:

na_rm (bool)

Return type:

ndarray

ecotyper.rcompat.stats.which_max(x)[source]

which.max(x) → 0-based index of the first maximum ignoring NaN; -1 if none.

Return type:

int

ecotyper.rcompat.stats.col_apply_mean(X, na_rm=False)[source]

apply(X, 2, mean[, na.rm = TRUE]).

Parameters:

na_rm (bool)

Return type:

ndarray

ecotyper.rcompat.stats.quantile7(x, probs, na_rm=False)[source]

quantile(x, probs, type = 7, names = FALSE).

Parameters:

na_rm (bool)

ecotyper.rcompat.stats.r_median(x)[source]

median(x) (mean(sort(x, partial = half + 0:1)[half + 0:1])).

Return type:

float

ecotyper.rcompat.stats.r_mad(x)[source]

mad(x) (constant 1.4826, center = median).

Return type:

float

ecotyper.rcompat.stats.rank_average(x)[source]

rank(x, ties.method = "average") (no NAs).

Return type:

ndarray

ecotyper.rcompat.stats.p_adjust_bh(p)[source]

p.adjust(p, "BH") (NAs kept; n = number of non-NA p-values).

Return type:

ndarray

ecotyper.rcompat.stats.pnorm(x, lower_tail=True)[source]

pnorm(x, lower.tail) (standard normal, R’s algorithm).

Parameters:

lower_tail (bool)

ecotyper.rcompat.stats.pt(t, df, lower_tail=True)[source]

pt(t, df) following R’s formulation (pbeta via scipy).

Parameters:
Return type:

float

exception ecotyper.rcompat.stats.TTestError[source]

Bases: ValueError

Raised where R’s t.test() stops (caught by EcoTyper’s tryCatch).

ecotyper.rcompat.stats.welch_t_test_p(x, y)[source]

t.test(x, y)$p.value (Welch, two-sided); raises TTestError where R errors.

Return type:

float

ecotyper.rcompat.stats.phyper_upper(q, m, n, k)[source]

1 - phyper(q, m, n, k) as computed by EcoTyper.

Parameters:
Return type:

float

ecotyper.rcompat.stats.row_ranks(X)[source]

Per-row average ranks and tie sums of X for row_wilcoxon_from_ranks().

Parameters:

X (ndarray)

ecotyper.rcompat.stats.row_wilcoxon_from_ranks(ranks, ties, in_x, correct=True)[source]

row_wilcoxon_twosample(X[:, in_x], X[:, ~in_x]) from row_ranks() of X: identical results (the ranks of a row do not depend on how its columns are split, and the rank sum runs over the x columns in their original order), without re-ranking X for every split.

Parameters:
Return type:

ndarray

ecotyper.rcompat.stats.row_wilcoxon_twosample(x, y, correct=True)[source]

p-values of matrixTests::row_wilcoxon_twosample(x, y) (two-sided).

Exact p-values (pwilcox) are used when both groups have < 50 observations and the row has no ties, as in matrixTests; otherwise the normal approximation with continuity and tie correction.

Parameters:
Return type:

ndarray