Changeset View
Changeset View
Standalone View
Standalone View
tests/stats/anova/AnovaTest.cpp
- This file was added.
1 | /*************************************************************************** | ||||
---|---|---|---|---|---|
2 | File : AnovaTest.cpp | ||||
3 | Project : LabPlot | ||||
4 | Description : Tests for data correlation | ||||
5 | -------------------------------------------------------------------- | ||||
6 | Copyright : (C) 2019 Devanshu Agarwal (agarwaldevanshu8@gmail.com) | ||||
7 | ***************************************************************************/ | ||||
8 | | ||||
9 | /*************************************************************************** | ||||
10 | * * | ||||
11 | * This program is free software; you can redistribute it and/or modify * | ||||
12 | * it under the terms of the GNU General Public License as published by * | ||||
13 | * the Free Software Foundation; either version 2 of the License, or * | ||||
14 | * (at your option) any later version. * | ||||
15 | * * | ||||
16 | * This program is distributed in the hope that it will be useful, * | ||||
17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * | ||||
18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * | ||||
19 | * GNU General Public License for more details. * | ||||
20 | * * | ||||
21 | * You should have received a copy of the GNU General Public License * | ||||
22 | * along with this program; if not, write to the Free Software * | ||||
23 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, * | ||||
24 | * Boston, MA 02110-1301 USA * | ||||
25 | * * | ||||
26 | ***************************************************************************/ | ||||
27 | | ||||
28 | #include "AnovaTest.h" | ||||
29 | #include "backend/hypothesisTest/HypothesisTest.h" | ||||
30 | | ||||
31 | #include "backend/core/AbstractColumn.h" | ||||
32 | #include "backend/core/column/Column.h" | ||||
33 | | ||||
34 | void AnovaTest::oneWayAnova_data() { | ||||
35 | QTest::addColumn<QVector<QString>>("col1Data"); | ||||
36 | QTest::addColumn<QVector<double>>("col2Data"); | ||||
37 | QTest::addColumn<double>("fValue_expected"); | ||||
38 | QTest::addColumn<double>("pValue_expected"); | ||||
39 | | ||||
40 | // First Sample | ||||
41 | QVector<QString> col1Data = {"1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", | ||||
42 | "2", "2", "2", "2", "2", "2", "2", "2", "2", "2", "2", "2", "2", "2", "2", "2", "2", "2", "2", "2", | ||||
43 | "3", "3", "3", "3", "3", "3", "3", "3", "3", "3", "3", "3", "3", "3", "3", "3", "3", "3", "3", "3", | ||||
44 | "4", "4", "4", "4", "4", "4", "4", "4", "4", "4", "4", "4", "4", "4", "4", "4", "4", "4", "4", "4", | ||||
45 | "5", "5", "5", "5", "5", "5", "5", "5", "5", "5", "5", "5", "5", "5", "5", "5", "5", "5", "5", "5"}; | ||||
46 | | ||||
47 | QVector<double> col2Data = {1, 43, 15, 40, 8, 17, 30, 34, 34, 26, 1, 7, 22, 30, 40, 15, 20, 9, 14, 15, | ||||
48 | 6, 30, 15, 30, 12, 17, 21, 23, 20, 27, -19, -18, -8, -6, -6, -9, -17, -12, -11, -6, | ||||
49 | 5, 8, 12, 19, 8, 15, 21, 28, 26, 27, -10, 6, 4, 3, 0, 4, 9, -5, 7, 13, | ||||
50 | 38, 20, 20, 28, 11, 17, 15, 27, 24, 23, 28, 26, 34, 32, 24, 29, 30, 24, 34, 23, | ||||
51 | -5, -12, -15, -4, -2, -6, -2, -7, -10, -15, -13, -16, -23, -22, -9, -18, -17, -15, -14, -15}; | ||||
52 | double fValue_expected = 33.1289; | ||||
53 | double pValue_expected = 0; | ||||
54 | | ||||
55 | QTest::newRow("socscistatistics") << col1Data << col2Data << fValue_expected << pValue_expected; | ||||
56 | } | ||||
57 | | ||||
58 | void AnovaTest::oneWayAnova() { | ||||
59 | QFETCH(QVector<QString>, col1Data); | ||||
60 | QFETCH(QVector<double>, col2Data); | ||||
61 | QFETCH(double, fValue_expected); | ||||
62 | QFETCH(double, pValue_expected); | ||||
63 | | ||||
64 | Column* col1 = new Column("col1", AbstractColumn::Text); | ||||
65 | Column* col2 = new Column("col2", AbstractColumn::Numeric); | ||||
66 | | ||||
67 | col1->replaceTexts(0, col1Data); | ||||
68 | col2->replaceValues(0, col2Data); | ||||
69 | | ||||
70 | QVector<Column*> cols; | ||||
71 | cols << col1 << col2; | ||||
72 | | ||||
73 | HypothesisTest anovaTest("One Way Anova"); | ||||
74 | anovaTest.setColumns(cols); | ||||
75 | | ||||
76 | HypothesisTest::Test test; | ||||
77 | test.type = HypothesisTest::Test::Type::Anova; | ||||
78 | test.subtype = HypothesisTest::Test::SubType::OneWay; | ||||
79 | test.tail = HypothesisTest::Test::Tail::Two; | ||||
80 | | ||||
81 | bool categoricalVariable = true; | ||||
82 | bool equalVariance = true; | ||||
83 | | ||||
84 | anovaTest.performTest(test, categoricalVariable, equalVariance); | ||||
85 | | ||||
86 | double fValue = anovaTest.statisticValue()[0]; | ||||
87 | double pValue = anovaTest.pValue()[0]; | ||||
88 | | ||||
89 | QDEBUG("fValue is " << fValue); | ||||
90 | QDEBUG("pValue is: " << pValue); | ||||
91 | QDEBUG("fValue_expected is " << fValue_expected); | ||||
92 | QDEBUG("pValue_expected is: " << pValue_expected); | ||||
93 | | ||||
94 | FuzzyCompare(fValue, fValue_expected, 0.1); | ||||
95 | FuzzyCompare(pValue, pValue_expected, 0.1); | ||||
96 | } | ||||
97 | | ||||
98 | void AnovaTest::twoWayAnova_data() { | ||||
99 | QTest::addColumn<QVector<QString>>("col1Data"); | ||||
100 | QTest::addColumn<QVector<QString>>("col2Data"); | ||||
101 | QTest::addColumn<QVector<double>>("col3Data"); | ||||
102 | QTest::addColumn<double>("fCol1Value_expected"); | ||||
103 | QTest::addColumn<double>("fCol2Value_expected"); | ||||
104 | QTest::addColumn<double>("fInteractionValue_expected"); | ||||
105 | QTest::addColumn<double>("pCol1Value_expected"); | ||||
106 | QTest::addColumn<double>("pCol2Value_expected"); | ||||
107 | | ||||
108 | // First Sample | ||||
109 | // This data set is taken from: http://statweb.stanford.edu/~susan/courses/s141/exanova.pdf | ||||
110 | QVector<QString> col1Data = {"Super", "Super", "Super", "Super", "Super", "Super", "Super", "Super", "Super", "Super", "Super", "Super", "Best", "Best", "Best", "Best", "Best", "Best", "Best", "Best", "Best", "Best", "Best", "Best"}; | ||||
111 | QVector<QString> col2Data = {"cold", "cold", "cold", "cold", "warm", "warm", "warm", "warm", "hot", "hot", "hot", "hot", "cold", "cold", "cold", "cold", "warm", "warm", "warm", "warm", "hot", "hot", "hot", "hot"}; | ||||
112 | QVector<double> col3Data = {4, 5, 6, 5, 7, 9, 8, 12, 10, 12, 11, 9, 6, 6, 4, 4, 13, 15, 12, 12, 12, 13, 10, 13}; | ||||
113 | double fCol1Value_expected = 9.8108; | ||||
114 | double fCol2Value_expected = 48.7297; | ||||
115 | double fInteractionValue_expected = 3.9730; | ||||
116 | double pCol1Value_expected = 0.005758; | ||||
117 | double pCol2Value_expected = 5.44e-08; | ||||
118 | // double pInteractionValue_expected = 0.037224; | ||||
119 | | ||||
120 | QTest::newRow("detergent vs temperature") << col1Data << col2Data << col3Data << | ||||
121 | fCol1Value_expected << fCol2Value_expected << fInteractionValue_expected << | ||||
122 | pCol1Value_expected << pCol2Value_expected; | ||||
123 | } | ||||
124 | | ||||
125 | //TODO: check for pValue. In document probabilty is Pr(>F) | ||||
126 | void AnovaTest::twoWayAnova() { | ||||
127 | QFETCH(QVector<QString>, col1Data); | ||||
128 | QFETCH(QVector<QString>, col2Data); | ||||
129 | QFETCH(QVector<double>, col3Data); | ||||
130 | QFETCH(double, fCol1Value_expected); | ||||
131 | QFETCH(double, fCol2Value_expected); | ||||
132 | QFETCH(double, fInteractionValue_expected); | ||||
133 | QFETCH(double, pCol1Value_expected); | ||||
134 | QFETCH(double, pCol2Value_expected); | ||||
135 | | ||||
136 | Column* col1 = new Column("col1", AbstractColumn::Text); | ||||
137 | Column* col2 = new Column("col2", AbstractColumn::Text); | ||||
138 | Column* col3 = new Column("col3", AbstractColumn::Numeric); | ||||
139 | | ||||
140 | col1->replaceTexts(0, col1Data); | ||||
141 | col2->replaceTexts(0, col2Data); | ||||
142 | col3->replaceValues(0, col3Data); | ||||
143 | | ||||
144 | QVector<Column*> cols; | ||||
145 | cols << col1 << col2 << col3; | ||||
146 | | ||||
147 | HypothesisTest anovaTest("Two Way Anova"); | ||||
148 | anovaTest.setColumns(cols); | ||||
149 | | ||||
150 | HypothesisTest::Test test; | ||||
151 | test.type = HypothesisTest::Test::Type::Anova; | ||||
152 | test.subtype = HypothesisTest::Test::SubType::TwoWay; | ||||
153 | test.tail = HypothesisTest::Test::Tail::Two; | ||||
154 | | ||||
155 | anovaTest.performTest(test); | ||||
156 | double fCol1Value = anovaTest.statisticValue()[0]; | ||||
157 | double fCol2Value = anovaTest.statisticValue()[1]; | ||||
158 | double fInteractionValue = anovaTest.statisticValue()[2]; | ||||
159 | | ||||
160 | double pCol1Value = anovaTest.pValue()[0]; | ||||
161 | double pCol2Value = anovaTest.pValue()[1]; | ||||
162 | | ||||
163 | QDEBUG("size of statistic value is " << anovaTest.statisticValue().size()); | ||||
164 | QDEBUG("fCol1Value is " << fCol1Value); | ||||
165 | QDEBUG("fCol1Value_expected is " << fCol1Value_expected); | ||||
166 | QDEBUG("fCol2Value is " << fCol2Value); | ||||
167 | QDEBUG("fCol2Value_expected is " << fCol2Value_expected); | ||||
168 | QDEBUG("fInteractionValue is " << fInteractionValue); | ||||
169 | QDEBUG("fInteractionValue_expected is " << fInteractionValue_expected); | ||||
170 | | ||||
171 | QDEBUG("pCol1Value is " << pCol1Value); | ||||
172 | QDEBUG("pCol1Value_expected is " << pCol1Value_expected); | ||||
173 | QDEBUG("pCol2Value is " << pCol2Value); | ||||
174 | QDEBUG("pCol2Value_expected is " << pCol2Value_expected); | ||||
175 | | ||||
176 | FuzzyCompare(fCol1Value, fCol1Value_expected, 0.1); | ||||
177 | FuzzyCompare(fCol2Value, fCol2Value_expected, 0.1); | ||||
178 | FuzzyCompare(fInteractionValue, fInteractionValue_expected, 0.1); | ||||
179 | FuzzyCompare(pCol1Value, pCol1Value_expected, 0.1); | ||||
180 | FuzzyCompare(pCol2Value, pCol2Value_expected, 0.1); | ||||
181 | } | ||||
182 | | ||||
183 | QTEST_MAIN(AnovaTest) |