diff --git a/tests/stats/correlation_coefficient/CorrelationCoefficientTest.h b/tests/stats/correlation_coefficient/CorrelationCoefficientTest.h --- a/tests/stats/correlation_coefficient/CorrelationCoefficientTest.h +++ b/tests/stats/correlation_coefficient/CorrelationCoefficientTest.h @@ -35,5 +35,11 @@ private slots: void pearsonCoefficient_data(); void pearsonCoefficient(); + + void kendallCoefficient_data(); + void kendallCoefficient(); + + void spearmanCoefficient_data(); + void spearmanCoefficient(); }; #endif // CORRELATIONCOEFFICIENTTEST_H diff --git a/tests/stats/correlation_coefficient/CorrelationCoefficientTest.cpp b/tests/stats/correlation_coefficient/CorrelationCoefficientTest.cpp --- a/tests/stats/correlation_coefficient/CorrelationCoefficientTest.cpp +++ b/tests/stats/correlation_coefficient/CorrelationCoefficientTest.cpp @@ -42,8 +42,8 @@ QVector col1Data = {56, 56, 65, 65, 50, 25, 87, 44, 35}; QVector col2Data = {87, 91, 85, 91, 75, 28, 122, 66, 58}; - double correlationValue_expected = 0.966; - double zValue_expected = 0; + double correlationValue_expected = 0.96619424909; + double zValue_expected = 0.; QTest::newRow("Sample 1") << col1Data << col2Data << correlationValue_expected << zValue_expected; @@ -53,8 +53,8 @@ col1Data = {43, 21, 25, 42, 57, 59}; col2Data = {99, 65, 79, 75, 87, 81}; - correlationValue_expected = 0.529809; - zValue_expected = 0; + correlationValue_expected = 0.52980897305; + zValue_expected = 0.; QTest::newRow("Sample 2") << col1Data << col2Data << correlationValue_expected << zValue_expected; @@ -64,8 +64,8 @@ col1Data = {8, 8, 6, 5, 7, 6}; col2Data = {81, 80, 75, 65, 91, 80}; - correlationValue_expected = 0.65; - zValue_expected = 0; + correlationValue_expected = 0.64755960039; + zValue_expected = 0.; QTest::newRow("Sample 3") << col1Data << col2Data << correlationValue_expected << zValue_expected; } @@ -103,8 +103,155 @@ QDEBUG("Z Value is: " << zValue); QDEBUG("Z Value Expected is: " << zValue_expected); - FuzzyCompare(correlationValue, correlationValue_expected, 0.1); - FuzzyCompare(zValue, zValue_expected, 0.1); + FuzzyCompare(correlationValue, correlationValue_expected, 1.e-5); + FuzzyCompare(zValue, zValue_expected); +} + +void CorrelationCoefficientTest::kendallCoefficient_data() { + QTest::addColumn>("col1Values"); + QTest::addColumn>("col2Values"); + QTest::addColumn>("col1Texts"); + QTest::addColumn>("col2Texts"); + QTest::addColumn("isDouble"); + QTest::addColumn("correlationValue_expected"); + QTest::addColumn("zValue_expected"); + + // First Sample + // This sample is taken from: + // https://www.statsdirect.com/help/nonparametric_methods/kendall_correlation.htm + QVector col1Values = {4, 10, 3, 1, 9, 2, 6, 7, 8, 5}; + QVector col2Values = {5, 8, 6, 2, 10, 3, 9, 4, 7, 1}; + QVector col1Texts; + QVector col2Texts; + + bool isDouble = true; + + double correlationValue_expected = 0.51111114025116; + double zValue_expected = 2.05718265659; + + QTest::newRow("Sample 1") << col1Values << col2Values << col1Texts << col2Texts << isDouble << correlationValue_expected << zValue_expected; + + // Second Sample + // This sample is taken from: + // https://www.statisticshowto.datasciencecentral.com/kendalls-tau/ + col1Texts = {"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L"}; + col2Texts = {"A", "B", "D", "C", "F", "E", "H", "G", "J", "I", "L", "K"}; + col1Values = {}; + col2Values = {}; + + correlationValue_expected = 0.84848484848; + zValue_expected = 3.84676339286; + + QTest::newRow("Sample 2") << col1Values << col2Values << col1Texts << col2Texts << isDouble << correlationValue_expected << zValue_expected; +} + +void CorrelationCoefficientTest::kendallCoefficient() { + QFETCH(QVector, col1Values); + QFETCH(QVector, col2Values); + QFETCH(QVector, col1Texts); + QFETCH(QVector, col2Texts); + QFETCH(bool, isDouble); + QFETCH(double, correlationValue_expected); + QFETCH(double, zValue_expected); + + Column* col1; + Column* col2; + + if (isDouble){ + col1 = new Column("col1", AbstractColumn::Numeric); + col2 = new Column("col2", AbstractColumn::Numeric); + + col1->replaceValues(0, col1Values); + col2->replaceValues(0, col2Values); + } else { + col1 = new Column("col1", AbstractColumn::Text); + col2 = new Column("col2", AbstractColumn::Text); + + col1->replaceTexts(0, col1Texts); + col2->replaceTexts(0, col2Texts); + } + + QVector cols; + cols << col1 << col2; + + CorrelationCoefficient correlationCoefficientTest("Kendall's Tau"); + correlationCoefficientTest.setColumns(cols); + + CorrelationCoefficient::Test test; + test = CorrelationCoefficient::Test::Kendall; + + bool categoricalVariable = false; + correlationCoefficientTest.performTest(test, categoricalVariable); + + double correlationValue = correlationCoefficientTest.correlationValue(); + double zValue = correlationCoefficientTest.statisticValue()[0]; + + QDEBUG("Correlation Value is " << correlationValue); + QDEBUG("Correlation Value Expected is " << correlationValue_expected); + QDEBUG("Z Value is: " << zValue); + QDEBUG("Z Value Expected is: " << zValue_expected); + + FuzzyCompare(correlationValue, correlationValue_expected, 1.e-7); + FuzzyCompare(zValue, zValue_expected, 1.e-7); +} + +void CorrelationCoefficientTest::spearmanCoefficient_data() { + QTest::addColumn>("col1Data"); + QTest::addColumn>("col2Data"); + QTest::addColumn("correlationValue_expected"); + + // First Sample + // This sample is taken: + // https://statistics.laerd.com/statistical-guides/spearmans-rank-order-correlation-statistical-guide-2.php + QVector col1Data = {56, 75, 45, 71, 62, 64, 58, 80, 76, 61}; + QVector col2Data = {66, 70, 40, 60, 65, 56, 59, 77, 67, 63}; + + double correlationValue_expected = 0.67272727272; + + QTest::newRow("Sample 1") << col1Data << col2Data << correlationValue_expected; + + // Second Sample + // This sample is taken from: + // https://en.wikipedia.org/wiki/Spearman%27s_rank_correlation_coefficient + col1Data = {106, 86, 100, 101, 99, 103, 97, 113, 112, 110}; + col2Data = {7, 0, 27, 50, 28, 29, 20, 12, 6, 17}; + + correlationValue_expected = -0.17575757575; + + QTest::newRow("Sample 2") << col1Data << col2Data << correlationValue_expected; + +} + +void CorrelationCoefficientTest::spearmanCoefficient() { + QFETCH(QVector, col1Data); + QFETCH(QVector, col2Data); + QFETCH(double, correlationValue_expected); + + Column* col1 = new Column("col1", AbstractColumn::Numeric); + Column* col2 = new Column("col2", AbstractColumn::Numeric); + + col1->replaceValues(0, col1Data); + col2->replaceValues(0, col2Data); + + QVector cols; + cols << col1 << col2; + + CorrelationCoefficient correlationCoefficientTest("Spearman Rank"); + correlationCoefficientTest.setColumns(cols); + + CorrelationCoefficient::Test test; + test = CorrelationCoefficient::Test::Spearman; + + bool categoricalVariable = false; + + correlationCoefficientTest.performTest(test, categoricalVariable); + + double correlationValue = correlationCoefficientTest.correlationValue(); + + QDEBUG("Correlation Value is " << correlationValue); + QDEBUG("Correlation Value Expected is " << correlationValue_expected); + + FuzzyCompare(correlationValue, correlationValue_expected, 1.e-5); } QTEST_MAIN(CorrelationCoefficientTest)