34template <
typename Iterator>
35double cohens_d(Iterator first, Iterator last,
double mu0,
double sigma)
38 throw std::invalid_argument(
"statcpp::cohens_d: sigma must be positive");
43 throw std::invalid_argument(
"statcpp::cohens_d: empty range");
47 return (mean_val - mu0) / sigma;
62template <
typename Iterator>
63double cohens_d(Iterator first, Iterator last,
double mu0)
67 throw std::invalid_argument(
"statcpp::cohens_d: need at least 2 elements");
74 throw std::invalid_argument(
"statcpp::cohens_d: zero variance");
77 return (mean_val - mu0) / s;
95template <
typename Iterator1,
typename Iterator2>
97 Iterator2 first2, Iterator2 last2)
102 if (n1 < 2 || n2 < 2) {
103 throw std::invalid_argument(
"statcpp::cohens_d_two_sample: need at least 2 elements in each sample");
112 double sp = std::sqrt(((n1 - 1) * var1 + (n2 - 1) * var2) / (n1 + n2 - 2));
115 throw std::invalid_argument(
"statcpp::cohens_d_two_sample: zero pooled variance");
118 return (mean1 - mean2) / sp;
136 return 1.0 - 3.0 / (4.0 * df - 1.0);
151template <
typename Iterator>
152double hedges_g(Iterator first, Iterator last,
double mu0)
156 throw std::invalid_argument(
"statcpp::hedges_g: need at least 2 elements");
159 double d =
cohens_d(first, last, mu0);
160 double df =
static_cast<double>(n - 1);
178template <
typename Iterator1,
typename Iterator2>
180 Iterator2 first2, Iterator2 last2)
185 if (n1 < 2 || n2 < 2) {
186 throw std::invalid_argument(
"statcpp::hedges_g_two_sample: need at least 2 elements in each sample");
190 double df =
static_cast<double>(n1 + n2 - 2);
213template <
typename Iterator1,
typename Iterator2>
214double glass_delta(Iterator1 control_first, Iterator1 control_last,
215 Iterator2 treatment_first, Iterator2 treatment_last)
221 throw std::invalid_argument(
"statcpp::glass_delta: control group needs at least 2 elements");
224 throw std::invalid_argument(
"statcpp::glass_delta: treatment group is empty");
228 double mean2 =
statcpp::mean(treatment_first, treatment_last);
232 throw std::invalid_argument(
"statcpp::glass_delta: control group has zero variance");
235 return (mean2 - mean1) / s1;
251 return t / std::sqrt(t * t + df);
263 return d / std::sqrt(d * d + 4.0);
276 if (std::abs(r) >= 1.0) {
277 throw std::invalid_argument(
"statcpp::r_to_d: |r| must be less than 1");
279 return 2.0 * r / std::sqrt(1.0 - r * r);
298 if (ss_total <= 0.0) {
299 throw std::invalid_argument(
"statcpp::eta_squared: ss_total must be positive");
301 return ss_effect / ss_total;
314 return (f * df1) / (f * df1 + df2);
333inline double omega_squared(
double ss_effect,
double ss_total,
double ms_error,
double df_effect)
335 if (ss_total <= 0.0) {
336 throw std::invalid_argument(
"statcpp::omega_squared: ss_total must be positive");
338 return (ss_effect - df_effect * ms_error) / (ss_total + ms_error);
357 if (p1 < 0.0 || p1 > 1.0 || p2 < 0.0 || p2 > 1.0) {
358 throw std::invalid_argument(
"statcpp::cohens_h: proportions must be in [0, 1]");
362 return 2.0 * (std::asin(std::sqrt(p1)) - std::asin(std::sqrt(p2)));
381inline double odds_ratio(
double a,
double b,
double c,
double d)
384 if (b == 0.0 || c == 0.0) {
385 throw std::invalid_argument(
"statcpp::odds_ratio: cell b or c is zero");
387 return (a * d) / (b * c);
402inline double risk_ratio(
double a,
double b,
double c,
double d)
406 if (a + b == 0.0 || c + d == 0.0) {
407 throw std::invalid_argument(
"statcpp::risk_ratio: row total is zero");
409 double risk1 = a / (a + b);
410 double risk2 = c / (c + d);
413 throw std::invalid_argument(
"statcpp::risk_ratio: risk in group 2 is zero");
416 return risk1 / risk2;
443 double abs_d = std::abs(d);
460 double abs_r = std::abs(r);
Basic statistical computation functions.
Dispersion and variance calculation functions.
double risk_ratio(double a, double b, double c, double d)
Relative risk (risk ratio)
effect_size_magnitude interpret_cohens_d(double d)
Interpret Cohen's d.
double omega_squared(const one_way_anova_result &result)
Calculate Omega-squared for one-way ANOVA.
double cohens_d_two_sample(Iterator1 first1, Iterator1 last1, Iterator2 first2, Iterator2 last2)
Cohen's d (two-sample, pooled standard deviation)
double sample_stddev(Iterator first, Iterator last)
Sample standard deviation.
double sample_variance(Iterator first, Iterator last)
Sample variance (unbiased variance)
odds_ratio_result odds_ratio(const std::vector< std::vector< std::size_t > > &table)
Calculate odds ratio from a 2x2 contingency table.
double d_to_r(double d)
Convert Cohen's d to correlation coefficient.
double t_to_r(double t, double df)
Convert t-value to correlation coefficient.
double r_to_d(double r)
Convert correlation coefficient to Cohen's d.
double glass_delta(Iterator1 control_first, Iterator1 control_last, Iterator2 treatment_first, Iterator2 treatment_last)
Glass's Delta (using control group's standard deviation)
double hedges_g_two_sample(Iterator1 first1, Iterator1 last1, Iterator2 first2, Iterator2 last2)
Hedges' g (two-sample)
double eta_squared(const one_way_anova_result &result)
Calculate Eta-squared for one-way ANOVA.
double partial_eta_squared(double f, double df1, double df2)
Calculate partial eta-squared from F-test.
double mean(Iterator first, Iterator last)
Arithmetic mean.
double cohens_h(double p1, double p2)
Cohen's h (effect size for difference between two proportions)
effect_size_magnitude
Enumeration for effect size magnitude.
effect_size_magnitude interpret_eta_squared(double eta2)
Interpret eta-squared.
double cohens_d(Iterator first, Iterator last, double mu0, double sigma)
Cohen's d (one-sample, known population standard deviation)
effect_size_magnitude interpret_correlation(double r)
Interpret correlation coefficient.
double hedges_g(Iterator first, Iterator last, double mu0)
Hedges' g (one-sample)
std::size_t count(Iterator first, Iterator last)
Data count.
double hedges_correction_factor(double df)
Hedges' bias correction factor J.