diff --git a/GeneralTest.h b/GeneralTest.h new file mode 100644 --- /dev/null +++ b/GeneralTest.h @@ -0,0 +1,135 @@ +/*************************************************************************** + File : GeneralTest.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 GENERALTEST_H +#define GENERALTEST_H + +#include "backend/core/AbstractPart.h" +#include "backend/lib/macros.h" + +class HypothesisTestView; +class Spreadsheet; +class QString; +class Column; +class QVBoxLayout; +class QLabel; + +class GeneralTest : public AbstractPart { + Q_OBJECT + +public: + explicit GeneralTest(const QString& name); + ~GeneralTest() override; + + enum DataSourceType {DataSourceSpreadsheet, DataSourceDatabase}; + + struct Cell { + QString data; + int level; + bool isHeader; + QString tooltip; + int rowSpanCount; + int columnSpanCount; + Cell(QVariant data = "", int level = 0, bool isHeader = false, QString tooltip = "", int rowSpanCount = 1, int columnSpanCount = 1) { + this->data = data.toString(); + this->level = level; + this->isHeader = isHeader; + this->tooltip = tooltip; + this->rowSpanCount = rowSpanCount; + this->columnSpanCount = columnSpanCount; + } + }; + + enum ErrorType {ErrorUnqualSize, ErrorEmptyColumn, NoError}; + + 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(); + QString testName(); + QString statsTable(); + QMap tooltips(); + + 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; + +signals: + void changed(); + void requestProjectContextMenu(QMenu*); + void dataSourceTypeChanged(GeneralTest::DataSourceType); + void dataSourceSpreadsheetChanged(Spreadsheet*); + +protected: + DataSourceType m_dataSourceType{GeneralTest::DataSourceSpreadsheet}; + Spreadsheet* m_dataSourceSpreadsheet{nullptr}; + QVector m_columns; + QStringList m_allColumns; + + QString m_currTestName{"Result Table"}; + QString m_statsTable; + + QVBoxLayout* m_summaryLayout{nullptr}; + QLabel* m_resultLine[10]; + QMap m_tooltips; + + bool isNumericOrInteger(Column* column); + QString round(QVariant number, int precision = 3); + + void countPartitions(Column* column, int& np, int& totalRows); + ErrorType findStats(const Column* column,int& count, double& sum, double& mean, double& std); + ErrorType findStatsPaired(const Column* column1, const Column* column2, int& count, double& sum, double& mean, double& std); + ErrorType findStatsCategorical(Column* column1, Column* column2, int n[], double sum[], double mean[], double std[], QMap& colName, const int& np, const int& totalRows); + + QString getHtmlTable(int row, int column, QVariant* rowMajor); + QString getHtmlTable3(const QList& rowMajor); + + QString getLine(const QString& msg, const QString& color = "black"); + void printLine(const int& index, const QString& msg, const QString& color = "black"); + void printTooltip(const int& index, const QString& msg); + void printError(const QString& errorMsg); + + bool m_dbCreated{false}; + mutable HypothesisTestView* m_view{nullptr}; +}; + +#endif // GeneralTest_H diff --git a/HypothesisTest.h b/HypothesisTest.h new file mode 100644 --- /dev/null +++ b/HypothesisTest.h @@ -0,0 +1,100 @@ +/*************************************************************************** + 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 "GeneralTest.h" +#include "backend/lib/macros.h" + +class HypothesisTestView; +class Spreadsheet; +class QString; +class Column; +class QVBoxLayout; +class QLabel; + +class HypothesisTest : public GeneralTest { + 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; + }; + + void setPopulationMean(QVariant populationMean); + void setSignificanceLevel(QVariant alpha); + + void performTest(Test m_test, bool categoricalVariable = true, bool equalVariance = true); + void performLeveneTest(bool categoricalVariable); + + QList statisticValue(); + QList pValue(); + QWidget* view() const override; + +private: + void performTwoSampleIndependentTest(HypothesisTest::Test::Type test, bool categoricalVariable = false, bool equalVariance = true); + void performTwoSamplePairedTest(HypothesisTest::Test::Type test); + void performOneSampleTest(HypothesisTest::Test::Type test); + void performOneWayAnova(); + void performTwoWayAnova(); + void m_performLeveneTest(bool categoricalVariable); + + double getPValue(const HypothesisTest::Test::Type& test, double& value, + const QString& col1Name, const QString& col2name, + const double mean, const double sp, const int df); + + double m_populationMean; + double m_significanceLevel; + HypothesisTest::Test::Tail m_tailType; + QList m_pValue; + QList m_statisticValue; +}; + +#endif // HypothesisTest_H