Pearson correlation 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.PearsonCorrelation(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.PearsonCorrelation(data)
>>> result = test.run_test("X", "Y", ["Z"])
>>> result.statistic
-0.02499
>>> result.p_value
0.5777
>>> result.dof
497
>>> result.effect_size
0.02499
pearson_correlation(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 <- pearson_correlation(data)
res <- run_test(test, "X", "Y", "Z")
print(res$statistic)
#> [1] -0.007868
print(res$p_value)
#> [1] 0.8608
print(res$dof)
#> [1] 497
print(res$effect_size)
#> [1] 0.007868
new PearsonCorrelation(data)
const { Dataset, PearsonCorrelation } = 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 PearsonCorrelation(data);
const result = test.runTest("X", "Y", ["Z"]);
console.log(result.statistic);
// -0.04219
console.log(result.pValue);
// 0.3470
console.log(result.dof);
// 497
console.log(result.effectSize);
// 0.04219
fastcit_core::ci_tests::PearsonCorrelation::new()
use std::f64::consts::PI;
use fastcit_core::ci_tests::PearsonCorrelation;
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 = PearsonCorrelation::new().test(&data, x, y, &[z])?;
println!("{:?}", result.statistic);
// Some(-0.04219)
println!("{:?}", result.p_value);
// 0.3470
println!("{:?}", result.dof);
// Some(497)
println!("{:?}", result.effect_size);
// Some(0.04219)
Ok(())
}
Method#
FastCIT regresses X and Y on \([1, Z]\) with ordinary least squares. \(r\) is the Pearson correlation of the two residual vectors. \(n\) is the number of rows, and \(|Z|\) is the number of columns in Z.
\[\begin{split}\begin{aligned}
\mathrm{dof} &= n - |Z| - 2, \\
t &= r \sqrt{\frac{\mathrm{dof}}{1 - r^2}}, \\
p &= 2 \, P\left(T_{\mathrm{dof}} \geq |t|\right).
\end{aligned}\end{split}\]
\(T_{\mathrm{dof}}\) has Student’s t-distribution. The statistic is \(r\), not \(t\). The effect size is \(|r|\).