From 4cf6667b72574ee89dd50ee4783d477a07414de2 Mon Sep 17 00:00:00 2001 From: Alexander Date: Mon, 25 Apr 2022 14:01:43 +0300 Subject: add tests for spearman correlation --- src/lib.rs | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) (limited to 'src') diff --git a/src/lib.rs b/src/lib.rs index 378bd33..71b2da2 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -84,8 +84,10 @@ impl Correlation for Spearman { //p-val two sided - let p_val = - return (rho,2.1); + let t = rho * (self.degrees_of_freedom / ((rho + 1.0) * (1.0 - rho))).sqrt(); + let p_val = 2.0* (tdist_Q(t.abs(), self.degrees_of_freedom)); + + return (rho,p_val); } } @@ -119,5 +121,17 @@ mod tests { assert_eq!(new_spearman,Spearman{n:5, degrees_of_freedom:3.0}); + } + #[test] + fn test_spearman_correlation(){ + let new_spearman = Spearman::new(5); + + let (s_rho,p_val) = new_spearman.correlate(&[1.,2.,3.,4.,5.], &[5.,6.,7.,8.,7.]); + + + assert_approx_eq!(s_rho,0.079603960396039,2f64); + assert_approx_eq!(p_val,0.43111687,2f64) + + } } \ No newline at end of file -- cgit 1.4.1