v2.0.0
Loading...
Searching...
No Matches
sts_ftest.cpp
Go to the documentation of this file.
1//=============================================================================================================
23
24//=============================================================================================================
25// INCLUDES
26//=============================================================================================================
27
28#include "sts_ftest.h"
29#include "sts_ttest.h"
30
31#include <cmath>
32
33//=============================================================================================================
34// USED NAMESPACES
35//=============================================================================================================
36
37using namespace STSLIB;
38using namespace Eigen;
39
40//=============================================================================================================
41// DEFINE METHODS
42//=============================================================================================================
43
44StatsFtestResult StatsFtest::oneWay(const QVector<MatrixXd>& groups)
45{
46 const int k = groups.size();
47 const int nVars = static_cast<int>(groups[0].cols());
48
49 // Total number of observations
50 int N = 0;
51 for (int g = 0; g < k; ++g) {
52 N += static_cast<int>(groups[g].rows());
53 }
54
55 // Grand mean
56 RowVectorXd grandMean = RowVectorXd::Zero(nVars);
57 for (int g = 0; g < k; ++g) {
58 grandMean += groups[g].colwise().sum();
59 }
60 grandMean /= static_cast<double>(N);
61
62 // Between-group sum of squares and within-group sum of squares
63 RowVectorXd ssBetween = RowVectorXd::Zero(nVars);
64 RowVectorXd ssWithin = RowVectorXd::Zero(nVars);
65
66 for (int g = 0; g < k; ++g) {
67 int ng = static_cast<int>(groups[g].rows());
68 RowVectorXd groupMean = groups[g].colwise().mean();
69
70 // Between: n_g * (groupMean - grandMean)^2
71 RowVectorXd diff = groupMean - grandMean;
72 ssBetween += static_cast<double>(ng) * diff.array().square().matrix();
73
74 // Within: sum of (x_i - groupMean)^2
75 MatrixXd centered = groups[g].rowwise() - groupMean;
76 ssWithin += centered.colwise().squaredNorm();
77 }
78
79 int dfBetween = k - 1;
80 int dfWithin = N - k;
81
82 RowVectorXd msBetween = ssBetween / static_cast<double>(dfBetween);
83 RowVectorXd msWithin = ssWithin / static_cast<double>(dfWithin);
84
85 // F-statistic
86 RowVectorXd fstat = msBetween.array() / msWithin.array();
87
88 // p-values
89 MatrixXd matPval(1, nVars);
90 for (int j = 0; j < nVars; ++j) {
91 matPval(0, j) = 1.0 - fCdf(fstat(j), dfBetween, dfWithin);
92 }
93
94 StatsFtestResult result;
95 result.matFstat = fstat;
96 result.matPval = matPval;
97 result.dfBetween = dfBetween;
98 result.dfWithin = dfWithin;
99 return result;
100}
101
102//=============================================================================================================
103
104double StatsFtest::fCdf(double f, int df1, int df2)
105{
106 // P(F <= f) using the relationship to the regularized incomplete beta function:
107 // P(F <= f) = I_x(df1/2, df2/2) where x = df1*f / (df1*f + df2)
108 if (f <= 0.0) return 0.0;
109
110 double x = static_cast<double>(df1) * f / (static_cast<double>(df1) * f + static_cast<double>(df2));
111 double a = static_cast<double>(df1) / 2.0;
112 double b = static_cast<double>(df2) / 2.0;
113
114 // Reuse the regularized beta function from StatsTtest
115 return StatsTtest::regularizedBeta(x, a, b);
116}
Frequentist Student's t-tests with exact p-values via the regularised incomplete beta function.
One-way ANOVA F-test with exact p-values for comparing two or more independent groups.
Statistical testing (t-tests, F-tests, cluster permutation, multiple comparison correction).
Per-call output of a one-way ANOVA F-test: F-statistics, p-values, and between/within degrees of free...
Definition sts_ftest.h:65
Eigen::MatrixXd matFstat
Definition sts_ftest.h:66
Eigen::MatrixXd matPval
Definition sts_ftest.h:67
static double fCdf(double f, int df1, int df2)
static StatsFtestResult oneWay(const QVector< Eigen::MatrixXd > &groups)
Definition sts_ftest.cpp:44
static double regularizedBeta(double x, double a, double b)