diff --git a/src/backend/hypothesisTest/HypothesisTest.h b/src/backend/hypothesisTest/HypothesisTest.h index 456af58b3..33aef536d 100644 --- a/src/backend/hypothesisTest/HypothesisTest.h +++ b/src/backend/hypothesisTest/HypothesisTest.h @@ -1,126 +1,126 @@ /*************************************************************************** File : HypothesisTest.h Project : LabPlot Description : Doing Hypothesis-Test on data provided -------------------------------------------------------------------- Copyright : (C) 2019 Devanshu Agarwal(agarwaldevanshu8@gmail.com) ***************************************************************************/ /*************************************************************************** * * * This program is free software; you can redistribute it and/or modify * * it under the terms of the GNU General Public License as published by * * the Free Software Foundation; either version 2 of the License, or * * (at your option) any later version. * * * * This program is distributed in the hope that it will be useful, * * but WITHOUT ANY WARRANTY; without even the implied warranty of * * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * * GNU General Public License for more details. * * * * You should have received a copy of the GNU General Public License * * along with this program; if not, write to the Free Software * * Foundation, Inc., 51 Franklin Street, Fifth Floor, * * Boston, MA 02110-1301 USA * * * ***************************************************************************/ #ifndef HYPOTHESISTEST_H #define HYPOTHESISTEST_H #include "backend/core/AbstractPart.h" #include "backend/lib/macros.h" class HypothesisTestPrivate; class HypothesisTestView; class Spreadsheet; class QString; class Column; class QVBoxLayout; class QLabel; class HypothesisTest : public AbstractPart { Q_OBJECT public: explicit HypothesisTest(const QString& name); ~HypothesisTest() override; struct Test { enum Type { NoneType = 0, TTest = 1 << 0, ZTest = 1 << 1, Anova = 1 << 2 }; enum SubType { NoneSubType = 0, TwoSampleIndependent = 1 << 0, TwoSamplePaired = 1 << 1, OneSample = 1 << 2, OneWay = 1 << 3, TwoWay = 1 << 4 }; enum Tail {Positive, Negative, Two}; Type type = NoneType; SubType subtype = NoneSubType; Tail tail; }; enum DataSourceType {DataSourceSpreadsheet, DataSourceDatabase}; void setDataSourceType(DataSourceType type); DataSourceType dataSourceType() const; void setDataSourceSpreadsheet(Spreadsheet* spreadsheet); Spreadsheet* dataSourceSpreadsheet() const; void setColumns(const QVector& cols); void setColumns(QStringList cols); QStringList allColumns(); void setPopulationMean(QVariant populationMean); void setSignificanceLevel(QVariant alpha); QString testName(); QString statsTable(); - void performTest(Test m_test, bool categoricalVariable, bool equalVariance); + void performTest(Test m_test, bool categoricalVariable = true, bool equalVariance = true); // void performTwoSampleIndependentTTest(bool categorical_variable, bool equal_variance); // void performTwoSamplePairedTTest(); // void performOneSampleTTest(); // void performTwoSampleIndependentZTest(); // void performTwoSamplePairedZTest(); // void performOneSampleZTest(); // void performOneWayAnova(); void performLeveneTest(bool categorical_variable); double statisticValue(); double pValue(); QVBoxLayout* summaryLayout(); //virtual methods // QIcon icon() const override; QMenu* createContextMenu() override; QWidget* view() const override; bool exportView() const override; bool printView() override; bool printPreview() const override; void save(QXmlStreamWriter*) const override; bool load(XmlStreamReader*, bool preview) override; private: HypothesisTestPrivate* const d; mutable HypothesisTestView* m_view{nullptr}; friend class HypothesisTestPrivate; signals: void changed(); void requestProjectContextMenu(QMenu*); void dataSourceTypeChanged(HypothesisTest::DataSourceType); void dataSourceSpreadsheetChanged(Spreadsheet*); }; #endif // HypothesisTest_H diff --git a/tests/stats/ttest/TTestTest.cpp b/tests/stats/ttest/TTestTest.cpp index 6f3b3003d..48f0afccf 100644 --- a/tests/stats/ttest/TTestTest.cpp +++ b/tests/stats/ttest/TTestTest.cpp @@ -1,89 +1,201 @@ -///*************************************************************************** -// File : CorrelationTest.cpp -// Project : LabPlot -// Description : Tests for data correlation -// -------------------------------------------------------------------- -// Copyright : (C) 2019 Devanshu Agarwal (agarwaldevanshu8@gmail.com) -// ***************************************************************************/ - -///*************************************************************************** -// * * -// * This program is free software; you can redistribute it and/or modify * -// * it under the terms of the GNU General Public License as published by * -// * the Free Software Foundation; either version 2 of the License, or * -// * (at your option) any later version. * -// * * -// * This program is distributed in the hope that it will be useful, * -// * but WITHOUT ANY WARRANTY; without even the implied warranty of * -// * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * -// * GNU General Public License for more details. * -// * * -// * You should have received a copy of the GNU General Public License * -// * along with this program; if not, write to the Free Software * -// * Foundation, Inc., 51 Franklin Street, Fifth Floor, * -// * Boston, MA 02110-1301 USA * -// * * -// ***************************************************************************/ +/*************************************************************************** + File : CorrelationTest.cpp + Project : LabPlot + Description : Tests for data correlation + -------------------------------------------------------------------- + Copyright : (C) 2019 Devanshu Agarwal (agarwaldevanshu8@gmail.com) + ***************************************************************************/ + +/*************************************************************************** + * * + * This program is free software; you can redistribute it and/or modify * + * it under the terms of the GNU General Public License as published by * + * the Free Software Foundation; either version 2 of the License, or * + * (at your option) any later version. * + * * + * This program is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * + * GNU General Public License for more details. * + * * + * You should have received a copy of the GNU General Public License * + * along with this program; if not, write to the Free Software * + * Foundation, Inc., 51 Franklin Street, Fifth Floor, * + * Boston, MA 02110-1301 USA * + * * + ***************************************************************************/ #include "TTestTest.h" #include "backend/hypothesisTest/HypothesisTest.h" #include "backend/core/AbstractColumn.h" #include "backend/core/column/Column.h" +//TODO: Change values of delta in fuzzy compare: That is causing the failure of tests + void TTestTest::twoSampleIndependent_data() { QTest::addColumn>("col1Data"); QTest::addColumn>("col2Data"); QTest::addColumn("tValue_expected"); QTest::addColumn("pValue_expected"); + // This data set is taken from "JASP" QVector col1Data = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}; QVector col2Data = {3, 1, 5, 4, 6, 4, 6, 2, 0, 5, 4, 5, 4, 3, 6, 6, 8, 5, 5, 4, 2, 5, 7, 5}; - QTest::newRow("ttest1") << col1Data << col2Data << -1.713 << 0.101; + double tValue_expected = -1.713; + double pValue_expected = 0.101; + + QTest::newRow("invisible cloak") << col1Data << col2Data << tValue_expected << pValue_expected; + // This data set is taken from "JASP" col1Data = {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; col2Data = {42, 46, 43, 10, 55, 17, 26, 60, 62, 53, 37, 42, 33, 37, 41, 42, 19, 55, 54, 28, 20, 48, 85, 24, 56, 43, 59, 58, 52, 71, 62, 43, 54, 49, 57, 61, 33, 44, 46, 67, 43, 49, 57, 53}; - QTest::newRow("ttest2") << col1Data << col2Data << -2.26 << 0.028; + tValue_expected = -2.26; + pValue_expected = 0.028; + QTest::newRow("directed control activities") << col1Data << col2Data << tValue_expected << pValue_expected; } void TTestTest::twoSampleIndependent() { - QFETCH(QVector, col1Data); QFETCH(QVector, col2Data); QFETCH(double, tValue_expected); QFETCH(double, pValue_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; - HypothesisTest tTest("Test"); + HypothesisTest tTest("Two Sample Independent"); tTest.setColumns(cols); HypothesisTest::Test test; test.type = HypothesisTest::Test::Type::TTest; test.subtype = HypothesisTest::Test::SubType::TwoSampleIndependent; test.tail = HypothesisTest::Test::Tail::Two; bool categoricalVariable = true; bool equalVariance = true; tTest.performTest(test, categoricalVariable, equalVariance); double tValue = tTest.statisticValue(); double pValue = tTest.pValue(); qDebug() << "tValue is " << tValue; qDebug() << "pValue is: " << pValue; qDebug() << "tValue_expected is " << tValue_expected; qDebug() << "pValue_expected is: " << pValue_expected; FuzzyCompare(tValue, tValue_expected, 0.1); FuzzyCompare(pValue, pValue_expected, 0.1); } +void TTestTest::twoSamplePaired_data() { + QTest::addColumn>("col1Data"); + QTest::addColumn>("col2Data"); + QTest::addColumn("tValue_expected"); + QTest::addColumn("pValue_expected"); + + // This data set is taken from "JASP" + // DATA SET:: Moon and Aggression + QVector col1Data = {3.33, 3.67, 2.67, 3.33, 3.33, 3.67, 4.67, 2.67, 6, 4.33, 3.33, 0.67, 1.33, 0.33, 2}; + QVector col2Data = {0.27, 0.59, 0.32, 0.19, 1.26, 0.11, 0.3, 0.4, 1.59, 0.6, 0.65, 0.69, 1.26, 0.23, 0.38}; + double tValue_expected = 6.452; + double pValue_expected = 0.001; + + QTest::newRow("Moon and Aggression") << col1Data << col2Data << tValue_expected << pValue_expected; +} + +void TTestTest::twoSamplePaired() { + QFETCH(QVector, col1Data); + QFETCH(QVector, col2Data); + QFETCH(double, tValue_expected); + QFETCH(double, pValue_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; + + HypothesisTest tTest("Two Sample Paried"); + tTest.setColumns(cols); + + HypothesisTest::Test test; + test.type = HypothesisTest::Test::Type::TTest; + test.subtype = HypothesisTest::Test::SubType::TwoSamplePaired; + test.tail = HypothesisTest::Test::Tail::Two; + + tTest.performTest(test); + double tValue = tTest.statisticValue(); + double pValue = tTest.pValue(); + + qDebug() << "tValue is " << tValue; + qDebug() << "pValue is: " << pValue; + qDebug() << "tValue_expected is " << tValue_expected; + qDebug() << "pValue_expected is: " << pValue_expected; + + FuzzyCompare(tValue, tValue_expected, 0.1); + FuzzyCompare(pValue, pValue_expected, 0.1); +} + +void TTestTest::oneSample_data() { + QTest::addColumn>("col1Data"); + QTest::addColumn("populationMean"); + QTest::addColumn("tValue_expected"); + QTest::addColumn("pValue_expected"); + + // This data set is taken from "JASP" + // DATA SET:: Weight Gain; + + // weight before; + QVector col1Data = {13.2, 8.58, 14.08, 8.58, 10.56, 14.74, 7.92, 13.2, 12.76, 5.72, 11.66, 7.04, 3.08, 15.62, 14.3, 5.5}; + // weight after; + double populationMean = 16; + double tValue_expected = -5.823; + double pValue_expected = 0.001; + QTest::newRow("weight gain") << col1Data << populationMean << tValue_expected << pValue_expected; +} + +void TTestTest::oneSample() { + QFETCH(QVector, col1Data); + QFETCH(double, populationMean); + QFETCH(double, tValue_expected); + QFETCH(double, pValue_expected); + + Column* col1 = new Column("col1", AbstractColumn::Numeric); + col1->replaceValues(0, col1Data); + + QVector cols; + cols << col1; + + HypothesisTest tTest("One Sample"); + tTest.setColumns(cols); + tTest.setPopulationMean(populationMean); + + HypothesisTest::Test test; + test.type = HypothesisTest::Test::Type::TTest; + test.subtype = HypothesisTest::Test::SubType::OneSample; + test.tail = HypothesisTest::Test::Tail::Two; + + tTest.performTest(test); + double tValue = tTest.statisticValue(); + double pValue = tTest.pValue(); + + qDebug() << "tValue is " << tValue; + qDebug() << "pValue is: " << pValue; + qDebug() << "tValue_expected is " << tValue_expected; + qDebug() << "pValue_expected is: " << pValue_expected; + + FuzzyCompare(tValue, tValue_expected, 0.1); + FuzzyCompare(pValue, pValue_expected, 0.1); +} + QTEST_MAIN(TTestTest) diff --git a/tests/stats/ttest/TTestTest.h b/tests/stats/ttest/TTestTest.h index 65d41a600..995f6e77e 100644 --- a/tests/stats/ttest/TTestTest.h +++ b/tests/stats/ttest/TTestTest.h @@ -1,40 +1,46 @@ /*************************************************************************** File : CorrelationTest.h Project : LabPlot Description : Tests for data correlation -------------------------------------------------------------------- Copyright : (C) 2019 Devanshu Agarwal (agarwaldevanshu8@gmail.com) ***************************************************************************/ /*************************************************************************** * * * This program is free software; you can redistribute it and/or modify * * it under the terms of the GNU General Public License as published by * * the Free Software Foundation; either version 2 of the License, or * * (at your option) any later version. * * * * This program is distributed in the hope that it will be useful, * * but WITHOUT ANY WARRANTY; without even the implied warranty of * * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * * GNU General Public License for more details. * * * * You should have received a copy of the GNU General Public License * * along with this program; if not, write to the Free Software * * Foundation, Inc., 51 Franklin Street, Fifth Floor, * * Boston, MA 02110-1301 USA * * * ***************************************************************************/ #ifndef TTESTTEST_H #define TTESTTEST_H #include <../../CommonTest.h> class TTestTest : public CommonTest { Q_OBJECT private slots: // Two Sample Independent Test - void twoSampleIndependent(); void twoSampleIndependent_data(); + void twoSampleIndependent(); + + void twoSamplePaired_data(); + void twoSamplePaired(); + + void oneSample_data(); + void oneSample(); }; #endif