Pillai’s trace test#

Data types:

X, Y and Z can each be discrete or continuous.

API:

All tests use the same API. See one API for all tests.

Usage#

fastcit.PillaiTrace(data)

>>> import numpy as np
>>> import fastcit

>>> # Each language makes its own random data, so the values differ from the other tabs.
>>> rng = np.random.default_rng(0)
>>> n = 500
>>> z = rng.integers(0, 3, n)
>>> x = z + rng.standard_normal(n)  # continuous, driven by the discrete Z
>>> y = z + rng.standard_normal(n)
>>> data = fastcit.Dataset({
...     "X": ("continuous", x),
...     "Y": ("continuous", y),
...     "Z": ("discrete", z.astype(float)),
... })

>>> test = fastcit.PillaiTrace(data)
>>> result = test.run_test("X", "Y", ["Z"])
>>> result.statistic
0.0006621
>>> result.p_value
0.5660
>>> print(result.dof)
None
>>> result.effect_size
0.0006621

pillai(data)

library(fastcit)

# Each language makes its own random data, so the values differ from the other tabs.
set.seed(0)
n <- 500
z <- sample(0:2, n, replace = TRUE)
x <- z + rnorm(n) # continuous, driven by the discrete Z
y <- z + rnorm(n)
df <- data.frame(X = x, Y = y, Z = as.integer(z))

data <- dataset(df)
test <- pillai(data)
res <- run_test(test, "X", "Y", "Z")
print(res$statistic)
#> [1] 0.005013
print(res$p_value)
#> [1] 0.1138
print(res$dof)
#> NULL
print(res$effect_size)
#> [1] 0.005013

new PillaiTrace(data)

const { Dataset, PillaiTrace } = require("fastcit");

// Each language makes its own random data, so the values differ from the other tabs.
// Seeded generator (Park-Miller), so every run gives the same data.
let seed = 12345;
const uniform = () => (seed = (seed * 16807) % 2147483647) / 2147483647;
const normal = () =>
  Math.sqrt(-2 * Math.log(uniform())) * Math.cos(2 * Math.PI * uniform());
const draw = (n, f) => Array.from({ length: n }, f);

const n = 500;
const z = draw(n, () => Math.floor(uniform() * 3));
const x = z.map((v) => v + normal()); // continuous, driven by the discrete Z
const y = z.map((v) => v + normal());

const data = new Dataset({
  X: { kind: "continuous", values: x },
  Y: { kind: "continuous", values: y },
  Z: { kind: "discrete", values: z },
});

const test = new PillaiTrace(data);
const result = test.runTest("X", "Y", ["Z"]);
console.log(result.statistic);
// 0.001233
console.log(result.pValue);
// 0.4334
console.log(result.dof);
// null
console.log(result.effectSize);
// 0.001233

fastcit_core::ci_tests::Pillai::new()

use std::f64::consts::PI;

use fastcit_core::ci_tests::Pillai;
use fastcit_core::dataset::{ColumnKind, Dataset};
use fastcit_core::strategy::CITest;

fn main() -> Result<(), Box<dyn std::error::Error>> {
    // Each language makes its own random data, so the values differ from the other tabs.
    // Seeded generator (Park-Miller), so every run gives the same data.
    let mut seed = 12345.0_f64;
    let mut uniform = move || {
        seed = seed * 16807.0 % 2_147_483_647.0;
        seed / 2_147_483_647.0
    };
    let n = 500;
    let z: Vec<f64> = (0..n).map(|_| (uniform() * 3.0).floor()).collect();
    let mut normal = || (-2.0 * uniform().ln()).sqrt() * (2.0 * PI * uniform()).cos();
    let x: Vec<f64> = z.iter().map(|&v| v + normal()).collect(); // driven by the discrete Z
    let y: Vec<f64> = z.iter().map(|&v| v + normal()).collect();

    let data = Dataset::from_columns(vec![
        ("X".into(), ColumnKind::Continuous, x),
        ("Y".into(), ColumnKind::Continuous, y),
        ("Z".into(), ColumnKind::Discrete, z),
    ])?;
    let x = data.index_of("X").unwrap();
    let y = data.index_of("Y").unwrap();
    let z = data.index_of("Z").unwrap();
    let result = Pillai::new().test(&data, x, y, &[z])?;
    println!("{:?}", result.statistic);
    // Some(0.001233)
    println!("{:?}", result.p_value);
    // 0.4334
    println!("{:?}", result.dof);
    // None
    println!("{:?}", result.effect_size);
    // Some(0.001233)
    Ok(())
}

Method#

A continuous column gives one column of values. A discrete column with \(K\) levels gives \(K - 1\) indicator columns. FastCIT regresses the \(p\) columns of X and the \(q\) columns of Y on \([1, Z]\) with ordinary least squares. \(\rho_i\) are the canonical correlations of the residuals, \(s = \min(p, q)\), and \(n\) is the number of rows.

\[\begin{split}\begin{aligned} V &= \sum_{i=1}^{s} \rho_i^2, \\ \mathrm{df}_1 &= p \, q, \\ \mathrm{df}_2 &= s \, (n - 1 + s - p - q), \\ F &= \frac{V}{\mathrm{df}_1} \cdot \frac{\mathrm{df}_2}{s - V}, \\ p\text{-value} &= P\left( F_{\mathrm{df}_1, \mathrm{df}_2} \geq F \right). \end{aligned}\end{split}\]

\(\mathrm{df}_2\) does not count the columns of Z. When \(s - V \leq 10^{-12} \, s\), the p-value is 0. The effect size is partial eta squared, \(V / s\).