Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FR: X2contingency #181

Open
wants to merge 2 commits into
base: developer
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions src/FSharp.Stats/Testing/ChiSquareTest.fs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ module ChiSquareTest =
open System
open FSharp.Stats

/// Computes the Chi-Square test
/// Computes the Chi-Square test.
/// n data points -> degrees of freedom = n - 1
let compute (degreesOfFreedom:int) (expected:seq<float>) (observed:seq<float>) =
//let chechParams =
Expand All @@ -53,4 +53,16 @@ module ChiSquareTest =
TestStatistics.createChiSquare chi2 (float degreesOfFreedom)



// Adapted from https://www.ling.upenn.edu/~clight/chisquared.htm
/// Computes a Chi-Square test for a 2 × 2 contingency table. Aka Cochran-Mantel-Haenszel (CMH) test, ger. Chi-Quadrat-Vierfeldertest.
let contingencyTable2x2Test trait1GroupA trait2GroupA trait1GroupB trait2GroupB =
let columnTrait1 = trait1GroupA + trait1GroupB
let columnTrait2 = trait2GroupA + trait2GroupB
let rowGroupA = trait1GroupA + trait2GroupA
let rowGroupB = trait1GroupB + trait2GroupB
let total = columnTrait1 + columnTrait2
let expFreqA1 = (rowGroupA * columnTrait1) / total
let expFreqA2 = (rowGroupA * columnTrait2) / total
let expFreqB1 = (rowGroupB * columnTrait1) / total
let expFreqB2 = (rowGroupB * columnTrait2) / total
compute 1 [expFreqA1; expFreqA2; expFreqB1; expFreqB2] [trait1GroupA; trait2GroupA; trait1GroupB; trait2GroupB]
23 changes: 22 additions & 1 deletion tests/FSharp.Stats.Tests/Testing.fs
Original file line number Diff line number Diff line change
Expand Up @@ -256,14 +256,35 @@ let chiSquaredTests =
let observed = [315.;101.;80.;32.;50.]
let df = expected.Length - 1
ChiSquareTest.compute df expected observed

// taken from: https://de.wikipedia.org/wiki/Chi-Quadrat-Test#Beispiele_und_Anwendungen
let testCaseContingency1 =
let trait1GroupA = 25.
let trait2GroupA = 25.
let trait1GroupB = 30.
let trait2GroupB = 20.
ChiSquareTest.contingencyTable2x2Test trait1GroupA trait2GroupA trait1GroupB trait2GroupB

let testCaseContingency2 =
let trait1GroupA = 250.
let trait2GroupA = 250.
let trait1GroupB = 300.
let trait2GroupB = 200.
ChiSquareTest.contingencyTable2x2Test trait1GroupA trait2GroupA trait1GroupB trait2GroupB

testList "Testing.ChiSquaredTest" [
testCase "compute" <| fun () ->
Expect.isTrue (0.9254 = Math.Round(testCase1.PValueRight,4)) "pValue should be equal."
Expect.isTrue (0.4700 = Math.Round(testCase1.Statistic,4)) "statistic should be equal."
Expect.isTrue (0.000638 = Math.Round(testCase2.PValueRight,6)) "pValue should be equal."
Expect.isTrue (19.461 = Math.Round(testCase2.Statistic,3)) "statistic should be equal."


testCase "contingencyTable" <| fun () ->
Expect.floatClose Accuracy.veryHigh (round 4 testCaseContingency1.PValueRight) 0.3149 "pValue is not about equal"
Expect.floatClose Accuracy.veryHigh (round 3 testCaseContingency1.Statistic) 1.01 "teststatistic is not about equal"

Expect.floatClose Accuracy.veryHigh (round 4 testCaseContingency2.PValueRight) 0.0015 "pValue is not about equal"
Expect.floatClose Accuracy.veryHigh (round 2 testCaseContingency2.Statistic) 10.1 "teststatistic is not about equal"
]

[<Tests>]
Expand Down