GCM test#
- Data types:
X, Y and Z must be continuous.
- API:
All tests use the same API. See one API for all tests.
Usage#
fastcit.GCM(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.standard_normal(n)
>>> x = z + rng.standard_normal(n) # X and Y depend on Z only
>>> y = z + rng.standard_normal(n)
>>> data = fastcit.Dataset({
... "X": ("continuous", x),
... "Y": ("continuous", y),
... "Z": ("continuous", z),
... })
>>> test = fastcit.GCM(data)
>>> result = test.run_test("X", "Y", ["Z"])
>>> result.statistic
-0.5699
>>> result.p_value
0.5687
>>> print(result.dof)
None
>>> result.effect_size
0.02499
gcm(data)
library(fastcit)
# Each language makes its own random data, so the values differ from the other tabs.
set.seed(0)
n <- 500
z <- rnorm(n)
x <- z + rnorm(n) # X and Y depend on Z only
y <- z + rnorm(n)
df <- data.frame(X = x, Y = y, Z = z)
data <- dataset(df)
test <- gcm(data)
res <- run_test(test, "X", "Y", "Z")
print(res$statistic)
#> [1] -0.1727
print(res$p_value)
#> [1] 0.8629
print(res$dof)
#> NULL
print(res$effect_size)
#> [1] 0.007868
new GCM(data)
const { Dataset, GCM } = 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, normal);
const x = z.map((v) => v + normal()); // X and Y depend on Z only
const y = z.map((v) => v + normal());
const data = new Dataset({
X: { kind: "continuous", values: x },
Y: { kind: "continuous", values: y },
Z: { kind: "continuous", values: z },
});
const test = new GCM(data);
const result = test.runTest("X", "Y", ["Z"]);
console.log(result.statistic);
// -0.9832
console.log(result.pValue);
// 0.3255
console.log(result.dof);
// null
console.log(result.effectSize);
// 0.04219
fastcit_core::ci_tests::Gcm::new()
use std::f64::consts::PI;
use fastcit_core::ci_tests::Gcm;
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 mut normal = || (-2.0 * uniform().ln()).sqrt() * (2.0 * PI * uniform()).cos();
let z: Vec<f64> = (0..500).map(|_| normal()).collect();
let x: Vec<f64> = z.iter().map(|v| v + normal()).collect(); // X and Y depend on Z only
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::Continuous, z),
])?;
let x = data.index_of("X").unwrap();
let y = data.index_of("Y").unwrap();
let z = data.index_of("Z").unwrap();
let result = Gcm::new().test(&data, x, y, &[z])?;
println!("{:?}", result.statistic);
// Some(-0.9832)
println!("{:?}", result.p_value);
// 0.3255
println!("{:?}", result.dof);
// None
println!("{:?}", result.effect_size);
// Some(0.04219)
Ok(())
}
Method#
FastCIT regresses X and Y on \([1, Z]\) with ordinary least squares. \(r_X\) and \(r_Y\) are the residuals, \(n\) is the number of rows, and \(R_i = r_{X,i} \, r_{Y,i}\).
\[\begin{split}\begin{aligned}
\hat\sigma_R &= \sqrt{\frac{1}{n} \sum_{i} \left( R_i - \bar R \right)^2}, \\
T &= \frac{\sum_{i} R_i}{\sqrt{n} \, \hat\sigma_R}, \\
p &= 2 \left( 1 - \Phi\left( |T| \right) \right).
\end{aligned}\end{split}\]
\(\Phi\) is the standard normal distribution function. The effect size is the absolute Pearson correlation of \(r_X\) and \(r_Y\).