Modified log-likelihood test#

Data types:

X, Y and Z must be discrete.

API:

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

Usage#

fastcit.ModifiedLikelihood(data, yates=True)

>>> 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 and Y each copy Z about 40% of the time, so they depend on Z only.
>>> x = np.where(rng.random(n) < 0.4, z, rng.integers(0, 3, n))
>>> y = np.where(rng.random(n) < 0.4, z, rng.integers(0, 3, n))
>>> data = fastcit.Dataset({
...     "X": ("discrete", x.astype(float)),
...     "Y": ("discrete", y.astype(float)),
...     "Z": ("discrete", z.astype(float)),
... })

>>> test = fastcit.ModifiedLikelihood(data)
>>> result = test.run_test("X", "Y", ["Z"])
>>> result.statistic
8.001
>>> result.p_value
0.7851
>>> result.dof
12
>>> result.effect_size
0.08945

modified_likelihood(data, yates = TRUE)

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 and Y each copy Z about 40% of the time, so they depend on Z only.
x <- ifelse(runif(n) < 0.4, z, sample(0:2, n, replace = TRUE))
y <- ifelse(runif(n) < 0.4, z, sample(0:2, n, replace = TRUE))
df <- data.frame(X = as.integer(x), Y = as.integer(y), Z = as.integer(z))

data <- dataset(df)
test <- modified_likelihood(data)
res <- run_test(test, "X", "Y", "Z")
print(res$statistic)
#> [1] 13.14
print(res$p_value)
#> [1] 0.3591
print(res$dof)
#> [1] 12
print(res$effect_size)
#> [1] 0.1146

new ModifiedLikelihood(data, { yates: true })

const { Dataset, ModifiedLikelihood } = 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 draw = (n, f) => Array.from({ length: n }, f);

const n = 500;
const z = draw(n, () => Math.floor(uniform() * 3));
// X and Y each copy Z about 40% of the time, so they depend on Z only.
const copy = (v) => (uniform() < 0.4 ? v : Math.floor(uniform() * 3));
const x = z.map(copy);
const y = z.map(copy);

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

const test = new ModifiedLikelihood(data);
const result = test.runTest("X", "Y", ["Z"]);
console.log(result.statistic);
// 16.79
console.log(result.pValue);
// 0.1577
console.log(result.dof);
// 12
console.log(result.effectSize);
// 0.1296

fastcit_core::ci_tests::ModifiedLikelihood { yates: true }

use fastcit_core::ci_tests::ModifiedLikelihood;
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();
    // X and Y each copy Z about 40% of the time, so they depend on Z only.
    let mut copy = |v: f64| {
        if uniform() < 0.4 {
            v
        } else {
            (uniform() * 3.0).floor()
        }
    };
    let x: Vec<f64> = z.iter().map(|&v| copy(v)).collect();
    let y: Vec<f64> = z.iter().map(|&v| copy(v)).collect();

    let data = Dataset::from_columns(vec![
        ("X".into(), ColumnKind::Discrete, x),
        ("Y".into(), ColumnKind::Discrete, 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 = ModifiedLikelihood::new().test(&data, x, y, &[z])?;
    println!("{:?}", result.statistic);
    // Some(16.79)
    println!("{:?}", result.p_value);
    // 0.1577
    println!("{:?}", result.dof);
    // Some(12)
    println!("{:?}", result.effect_size);
    // Some(0.1296)
    Ok(())
}

Method#

Each combination \(z\) of Z values gives one table. \(O_{ij}\) is the number of rows with \(X = i\) and \(Y = j\). \(R_i\) and \(C_j\) are the row and column sums, and \(n_z\) is the sum of all counts in table \(z\).

\[\begin{split}\begin{aligned} E_{ij} &= \frac{R_i \, C_j}{n_z}, \\ G_M^2 &= 2 \sum_{z} \sum_{i,j} E_{ij} \ln \frac{E_{ij}}{O_{ij}}, \\ \mathrm{dof} &= \sum_{z} \left(r_z - 1\right)\left(c_z - 1\right), \\ p &= P\left(\chi^2_{\mathrm{dof}} \geq G_M^2\right). \end{aligned}\end{split}\]

\(r_z\) and \(c_z\) are the numbers of X and Y categories in table \(z\). A cell with \(O_{ij} = 0\) makes \(G_M^2\) infinite, and then \(p = 0\). Yates’ correction prevents this in a 2 × 2 table. With yates on, a 2 × 2 table uses \(O'_{ij} = O_{ij} - \operatorname{clip}\left(O_{ij} - E_{ij}, -0.5, 0.5\right)\). The effect size is Cramér’s V, \(\sqrt{G_M^2 / \left(n \left(k - 1\right)\right)}\). \(n\) is the number of rows in the data, and \(k\) is the smaller of the numbers of X and Y categories in the data.