Trend tests#
Williams’ trend test as BMDExpress 2 computes it, which NTP’s and ETAP’s workflows run before fitting curves; Williams’ trend test as BMDExpress runs it explains it.
Test whether each gene’s mean rises or falls steadily with dose, by Williams’ trend test as BMDExpress 2 computes it.
NTP’s and ETAP’s workflows keep a gene for curve fitting when its mean rises or
falls monotonically with dose, by Williams’ trend test as BMDExpress 2 runs it (NTP
2018; US EPA 2024; Phillips et al. 2019), whose p-values come from random
relabellings of the samples’ doses. williams_trend_test() computes that test
for every gene of a samples x genes matrix of log-scale values, and
WilliamsTrendTest holds each gene’s statistic, direction and p-value. How the
test works, how it differs from the Williams-type contrasts of
dose_response() and how to read it are in
https://docs.nygen.io/CytePool/statistics/dose-response#williams-trend-test.
- The statistic:
With ybar_k the mean of the n_k samples of dose group k, the vehicle k = 0, and s the standard deviation pooled within the K dose groups on N - K degrees of freedom, isotonic regression (Bartholomew 1959) fits the means of all K groups, the vehicle included, by pool-adjacent-violators with weights n_k: once non-decreasing, f, and once non-increasing, g. The two sides’ statistics are t_up = (f_top - f_0) / (s sqrt(1/n_top + 1/n_0)) and t_down = (g_0 - g_top) / (s sqrt(1/n_top + 1/n_0)), both at least 0, and the gene’s direction is the side of the larger, up on a tie; a side whose fit is flat scores 0, and the other side is infinite, or very large where rounding leaves a trace of spread, when no group has any spread within it. Williams (1971) fits the treated groups alone and compares them with the vehicle’s observed mean; BMDExpress 2.30.520 fits the vehicle with them, as found by comparing its p-values with exact enumeration of every relabelling of small designs.
- The p-value:
B random relabellings of the samples’ dose groups, drawn once and shared by every gene, each give the two statistics. Of the n relabellings whose larger statistic lies on the gene’s side, b reach its statistic, and the p-value is (b + 1) / (n + 1), which counts the observed labelling among them, so that it is never 0 (Phipson and Smyth 2010). About half the relabellings fall on each side, so the smallest p-value is about 2 / B; BMDExpress also counts only the relabellings on the gene’s side. A relabelling reaches the statistic when its own is at least the gene’s less a relative 1e-9, so that relabellings whose statistics equal the gene’s mathematically, such as those that move samples between equal groups that pool-adjacent-violators pools, count as reaching it whatever the rounding. Each gene’s values are centred on their mean first, which leaves the statistics as they are and keeps the within-group sums of squares accurate where the spread is small beside the level.
References
Williams DA (1971). A test for differences between treatment means when several dose levels are compared with a zero dose control. Biometrics 27, 103. doi:10.2307/2528930
Bartholomew DJ (1959). A test of homogeneity for ordered alternatives. Biometrika 46, 36-48. doi:10.1093/biomet/46.1-2.36
Phipson B, Smyth GK (2010). Permutation p-values should never be zero: calculating exact p-values when permutations are randomly drawn. Statistical Applications in Genetics and Molecular Biology 9, article 39. doi:10.2202/1544-6115.1585
Phillips JR, Svoboda DL, Tandon A, Patel S, Sedykh A, Mav D, Kuo B, Yauk CL, Yang L, Thomas RS, Gift JS, Davis JA, Olszyk L, Merrick BA, Paules RS, Parham F, Saddler T, Shah RR, Auerbach SS (2019). BMDExpress 2: enhanced transcriptomic dose-response analysis workflow. Bioinformatics 35, 1780-1782. doi:10.1093/bioinformatics/bty878
NTP (2018). NTP research report on National Toxicology Program approach to genomic dose-response modeling. NTP Research Report 5. National Institute of Environmental Health Sciences. doi:10.22427/ntp-rr-5
US EPA (2024). Standard Methods for Development of EPA Transcriptomic Assessment Products (ETAPs). EPA/600/X-23/083, US Environmental Protection Agency, Office of Research and Development. doi:10.23645/epacomptox.25365496
- class cytepool.stats.trend_tests.WilliamsTrendTest(doses, n_samples, statistic, direction, p_value, isotonic_means, n_permutations, seed)#
Williams’ trend test of every gene, as BMDExpress 2 computes it.
Arrays are read-only; genes are in the order of the columns of the values. https://docs.nygen.io/CytePool/statistics/dose-response#williams-trend-test says how to read them.
- Variables:
doses –
(K,)the dose of each dose group, ascending, the vehicle (0) first.n_samples –
(K,)the samples of each dose group.statistic –
(n_genes,)the larger of the two sides’ statistics, t_up and t_down; infinite, or very large after rounding, where the groups’ means differ and no group has any spread within it, NaN where every sample of the gene has one value.direction –
(n_genes,)"up"where t_up is at least t_down, otherwise"down"; empty where the statistic is NaN.p_value –
(n_genes,)the permutation p-value, (b + 1) / (n + 1) for b of the n relabellings on the gene’s side that reach its statistic; NaN where the statistic is.isotonic_means –
(n_genes, K)the dose groups’ means fitted by isotonic regression in the gene’s direction, the vehicle first; the means themselves where the statistic is NaN.n_permutations – B, the relabellings drawn.
seed – The seed of the relabellings; passing it back as
rngdraws the same ones.
Explained in: Dose response
- table(*, feature_ids=None)#
Return the test as a table, one row per gene, in order.
Columns:
feature_id,direction,statisticandp_value.- Parameters:
feature_ids – One id per gene, as text; by default
feature_0,feature_1, …- Raises:
ValueError – If
feature_idsdoes not give one id per gene, or an id is empty.TypeError – If an id is not text, or
feature_idsis a numpy masked array.
Explained in: Dose response
- cytepool.stats.trend_tests.williams_trend_test(doses, values, *, n_permutations=10_000, rng=None, n_threads=None)#
Test every gene for a monotone trend with dose, by Williams’ trend test as BMDExpress 2 computes it.
The statistic compares the vehicle and the top dose after isotonic regression of the dose groups’ means, in the direction the data take, and the p-value counts the random relabellings of the samples’ doses, among those that point the same way, whose statistic reaches the gene’s. NTP’s and ETAP’s rule sets in
dose_response()run this test on log2(CPM + 1) values; its p-values are not adjusted for the number of genes there, as BMDExpress’s are not. https://docs.nygen.io/CytePool/statistics/dose-response#williams-trend-test explains the test and how it differs from the Williams-type contrasts ofdose_response(tests=["williams"]).- Parameters:
doses – One dose per sample, finite and at least 0, with 0 for the vehicle; the dose groups are the distinct doses.
values –
(n_samples, n_genes)log-scale values, such as log2(CPM + 1), finite and at most 1e100 in size.n_permutations – B, the random relabellings of the samples’ dose groups, at least 1; the smallest p-value is about 2 / B.
rng – A
numpy.random.Generator, an integer seed or None (fresh entropy) for the relabellings; the seed is recorded in the result.n_threads – The most threads to use; None keeps numba’s setting. Results do not depend on it.
- Returns:
Each gene’s statistic, direction, p-value and isotonic means.
- Raises:
TypeError – If
dosesorvaluesdo not hold real numbers or are numpy masked arrays,n_permutationsis not an integer, orrngis not a generator, an integer seed or None.ValueError – If
valuesis not 2-D with one row per dose and a column or more, a value or dose is not finite, a value is larger than 1e100 in size, a dose is negative, two distinct doses differ by less than 1e-8 of the dose, no dose is 0, the doses form one group, no dose group has two samples,n_permutationsis below 1, or the seed is negative.
Example
>>> import numpy as np >>> doses = np.repeat([0.0, 1.0, 3.0, 10.0], 3) >>> rng = np.random.default_rng(1) >>> values = np.column_stack( ... [rng.normal(0.0, 0.2, 12) + 0.1 * doses, rng.normal(0.0, 0.2, 12)] ... ) >>> test = williams_trend_test(doses, values, rng=1) >>> test.direction.tolist(), bool(test.p_value[0] < 0.01) (['up', 'up'], True)
Explained in: Choosing a method, Dose response