ABalytics 1.2.1__tar.gz → 2.1.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- abalytics-2.1.0/PKG-INFO +133 -0
- abalytics-2.1.0/README.md +117 -0
- abalytics-2.1.0/abalytics/__init__.py +10 -0
- abalytics-2.1.0/abalytics/analysis.py +271 -0
- abalytics-2.1.0/abalytics/significance_tests.py +697 -0
- abalytics-2.1.0/abalytics/utils.py +125 -0
- abalytics-2.1.0/abalytics.egg-info/PKG-INFO +133 -0
- {abalytics-1.2.1 → abalytics-2.1.0}/abalytics.egg-info/SOURCES.txt +1 -0
- {abalytics-1.2.1 → abalytics-2.1.0}/setup.py +1 -1
- abalytics-1.2.1/PKG-INFO +0 -99
- abalytics-1.2.1/README.md +0 -83
- abalytics-1.2.1/abalytics/__init__.py +0 -9
- abalytics-1.2.1/abalytics/analysis.py +0 -248
- abalytics-1.2.1/abalytics/significance_tests.py +0 -324
- abalytics-1.2.1/abalytics.egg-info/PKG-INFO +0 -99
- {abalytics-1.2.1 → abalytics-2.1.0}/LICENSE +0 -0
- {abalytics-1.2.1 → abalytics-2.1.0}/abalytics.egg-info/dependency_links.txt +0 -0
- {abalytics-1.2.1 → abalytics-2.1.0}/abalytics.egg-info/requires.txt +0 -0
- {abalytics-1.2.1 → abalytics-2.1.0}/abalytics.egg-info/top_level.txt +0 -0
- {abalytics-1.2.1 → abalytics-2.1.0}/setup.cfg +0 -0
abalytics-2.1.0/PKG-INFO
ADDED
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
Metadata-Version: 2.1
|
|
2
|
+
Name: abalytics
|
|
3
|
+
Version: 2.1.0
|
|
4
|
+
Summary: Advanced A/B Testing Statistical Analytics
|
|
5
|
+
Author: Janis Berneker
|
|
6
|
+
License: LICENSE
|
|
7
|
+
Project-URL: Repository, https://github.com/BigBerny/abalytics
|
|
8
|
+
Requires-Python: >=3.8
|
|
9
|
+
Description-Content-Type: text/markdown
|
|
10
|
+
License-File: LICENSE
|
|
11
|
+
Requires-Dist: pandas>=1.5.2
|
|
12
|
+
Requires-Dist: scipy>=1.10.1
|
|
13
|
+
Requires-Dist: statsmodels>=0.14.1
|
|
14
|
+
Requires-Dist: scikit-posthocs>=0.8.1
|
|
15
|
+
Requires-Dist: pingouin>=0.5.4
|
|
16
|
+
|
|
17
|
+
# ABalytics: Advanced A/B Testing Statistical Analytics
|
|
18
|
+
|
|
19
|
+
ABalytics is a Python package designed for statistical analysis, particularly for assessing the significance of A/B testing results. Its goal is to provide high-quality analysis by selecting the appropriate statistical tests based on the type of variable being analyzed. It offers a suite of tools to perform various significance tests and posthoc analyses on experimental data.
|
|
20
|
+
|
|
21
|
+
## Features
|
|
22
|
+
|
|
23
|
+
- **Boolean and Numeric Analysis**: Supports analysis of both boolean and numeric data types, ensuring the use of correct statistical methods for each.
|
|
24
|
+
- **Significance Tests**: Includes a variety of significance tests such as Chi-Square, Welch's ANOVA, and Kruskal-Wallis, to accurately determine the significance of results.
|
|
25
|
+
- **Posthoc Analysis**: Offers posthoc analysis methods like Tukey's HSD, Dunn's test, and Games-Howell, for detailed examination following significance tests.
|
|
26
|
+
- **Normality and Homogeneity Checks**: Performs checks for Gaussian distribution and homogeneity of variances using Levene's test, which are critical for selecting the right tests.
|
|
27
|
+
- **Data Transformation**: Provides functionality to convert data from long to wide format, facilitating analysis of dependent groups.
|
|
28
|
+
- **Pretty Text Output**: Generates a formatted text output with the results of the statistical tests, facilitating interpretation and reporting.
|
|
29
|
+
|
|
30
|
+
## Installation
|
|
31
|
+
|
|
32
|
+
To install ABalytics, use pip:
|
|
33
|
+
```bash
|
|
34
|
+
pip install abalytics
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
## Usage
|
|
38
|
+
### Analyzing Results
|
|
39
|
+
|
|
40
|
+
ABalytics provides two main functions for analyzing A/B testing results: `analyze_independent_groups` and `analyze_dependent_groups`.
|
|
41
|
+
|
|
42
|
+
#### Independent Groups Analysis
|
|
43
|
+
|
|
44
|
+
`analyze_independent_groups` is used for analyzing data where the groups are independent of each other. It takes a pandas DataFrame, the name of the column containing the variable to analyze, the name of the column containing the grouping variable, and optional parameters for p-value threshold and minimum sample size.
|
|
45
|
+
|
|
46
|
+
Example:
|
|
47
|
+
```python
|
|
48
|
+
from abalytics import analyze_independent_groups
|
|
49
|
+
import pandas as pd
|
|
50
|
+
|
|
51
|
+
# Load your data into a pandas DataFrame
|
|
52
|
+
df = pd.read_csv('your_data.csv')
|
|
53
|
+
|
|
54
|
+
# Analyze the results
|
|
55
|
+
analysis_results = analyze_independent_groups(
|
|
56
|
+
df,
|
|
57
|
+
variable_to_analyze="order_value",
|
|
58
|
+
group_column="ab_test_group",
|
|
59
|
+
)
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
#### Dependent Groups Analysis
|
|
64
|
+
|
|
65
|
+
`analyze_dependent_groups` is used for analyzing data where the groups are dependent, such as repeated measures on the same subjects. It requires data in wide format. If your data is in long format, you can use the `convert_long_to_wide` function in `abalytics.utils` to convert it. The `analyze_dependent_groups` function takes a pandas DataFrame, the names of the columns to compare, and optional parameters for p-value threshold and minimum sample size.
|
|
66
|
+
|
|
67
|
+
Example:
|
|
68
|
+
```python
|
|
69
|
+
from abalytics import analyze_dependent_groups
|
|
70
|
+
import pandas as pd
|
|
71
|
+
|
|
72
|
+
# Load your data into a pandas DataFrame
|
|
73
|
+
df = pd.read_csv('your_data.csv')
|
|
74
|
+
|
|
75
|
+
# Analyze the results
|
|
76
|
+
analysis_results = analyze_dependent_groups(
|
|
77
|
+
df,
|
|
78
|
+
variables_to_compare=["pre_test_score", "post_test_score"],
|
|
79
|
+
)
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
### Data Transformation
|
|
83
|
+
|
|
84
|
+
The `convert_long_to_wide` function in `abalytics.utils` is designed to transform data from long format to wide format, with an option to keep multi-level columns or flatten them. `analyze_dependent_groups` requires data in wide format to operate correctly.
|
|
85
|
+
|
|
86
|
+
Example:
|
|
87
|
+
```python
|
|
88
|
+
from abalytics.utils import convert_long_to_wide
|
|
89
|
+
import pandas as pd
|
|
90
|
+
|
|
91
|
+
# Assuming 'df_long' is your pandas DataFrame in long format
|
|
92
|
+
df_wide = convert_long_to_wide(
|
|
93
|
+
df_long,
|
|
94
|
+
index_col="subject_id",
|
|
95
|
+
columns_col="condition",
|
|
96
|
+
flatten_columns=True # Set to False if you wish to keep multi-level columns
|
|
97
|
+
)
|
|
98
|
+
```
|
|
99
|
+
|
|
100
|
+
### Generating Pretty Text Output
|
|
101
|
+
|
|
102
|
+
To get a formatted text output of your results, you can use the `utils.format_results_as_table` function.
|
|
103
|
+
|
|
104
|
+
Example:
|
|
105
|
+
```python
|
|
106
|
+
from abalytics.utils import format_results_as_table
|
|
107
|
+
from abalytics import analyze_independent_groups
|
|
108
|
+
import pandas as pd
|
|
109
|
+
|
|
110
|
+
# Load your data into a pandas DataFrame
|
|
111
|
+
df = pd.read_csv('your_data.csv')
|
|
112
|
+
|
|
113
|
+
# Analyze the results
|
|
114
|
+
analysis_results = analyze_independent_groups(
|
|
115
|
+
df,
|
|
116
|
+
variable_to_analyze="order_value",
|
|
117
|
+
group_column="ab_test_group",
|
|
118
|
+
)
|
|
119
|
+
|
|
120
|
+
# Generate pretty text output
|
|
121
|
+
pretty_text = format_results_as_table(
|
|
122
|
+
abalytics_results=[analysis_results],
|
|
123
|
+
identifiers_list=[["A/B Test 1", "Mobile"]],
|
|
124
|
+
)
|
|
125
|
+
print(pretty_text)
|
|
126
|
+
```
|
|
127
|
+
Executing this code will output a neatly formatted table displaying the outcomes of the statistical significance tests. The table includes the sample size, indicators for Levene's test and Gaussian distribution, and the test results.
|
|
128
|
+
|
|
129
|
+
A further example of how to use ABalytics can be found in `examples/example.py`.
|
|
130
|
+
|
|
131
|
+
## Contributing
|
|
132
|
+
|
|
133
|
+
Contributions to ABalytics are welcome. If you have suggestions for improvements or find any issues, please open an issue or submit a pull request.
|
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
# ABalytics: Advanced A/B Testing Statistical Analytics
|
|
2
|
+
|
|
3
|
+
ABalytics is a Python package designed for statistical analysis, particularly for assessing the significance of A/B testing results. Its goal is to provide high-quality analysis by selecting the appropriate statistical tests based on the type of variable being analyzed. It offers a suite of tools to perform various significance tests and posthoc analyses on experimental data.
|
|
4
|
+
|
|
5
|
+
## Features
|
|
6
|
+
|
|
7
|
+
- **Boolean and Numeric Analysis**: Supports analysis of both boolean and numeric data types, ensuring the use of correct statistical methods for each.
|
|
8
|
+
- **Significance Tests**: Includes a variety of significance tests such as Chi-Square, Welch's ANOVA, and Kruskal-Wallis, to accurately determine the significance of results.
|
|
9
|
+
- **Posthoc Analysis**: Offers posthoc analysis methods like Tukey's HSD, Dunn's test, and Games-Howell, for detailed examination following significance tests.
|
|
10
|
+
- **Normality and Homogeneity Checks**: Performs checks for Gaussian distribution and homogeneity of variances using Levene's test, which are critical for selecting the right tests.
|
|
11
|
+
- **Data Transformation**: Provides functionality to convert data from long to wide format, facilitating analysis of dependent groups.
|
|
12
|
+
- **Pretty Text Output**: Generates a formatted text output with the results of the statistical tests, facilitating interpretation and reporting.
|
|
13
|
+
|
|
14
|
+
## Installation
|
|
15
|
+
|
|
16
|
+
To install ABalytics, use pip:
|
|
17
|
+
```bash
|
|
18
|
+
pip install abalytics
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
## Usage
|
|
22
|
+
### Analyzing Results
|
|
23
|
+
|
|
24
|
+
ABalytics provides two main functions for analyzing A/B testing results: `analyze_independent_groups` and `analyze_dependent_groups`.
|
|
25
|
+
|
|
26
|
+
#### Independent Groups Analysis
|
|
27
|
+
|
|
28
|
+
`analyze_independent_groups` is used for analyzing data where the groups are independent of each other. It takes a pandas DataFrame, the name of the column containing the variable to analyze, the name of the column containing the grouping variable, and optional parameters for p-value threshold and minimum sample size.
|
|
29
|
+
|
|
30
|
+
Example:
|
|
31
|
+
```python
|
|
32
|
+
from abalytics import analyze_independent_groups
|
|
33
|
+
import pandas as pd
|
|
34
|
+
|
|
35
|
+
# Load your data into a pandas DataFrame
|
|
36
|
+
df = pd.read_csv('your_data.csv')
|
|
37
|
+
|
|
38
|
+
# Analyze the results
|
|
39
|
+
analysis_results = analyze_independent_groups(
|
|
40
|
+
df,
|
|
41
|
+
variable_to_analyze="order_value",
|
|
42
|
+
group_column="ab_test_group",
|
|
43
|
+
)
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
#### Dependent Groups Analysis
|
|
48
|
+
|
|
49
|
+
`analyze_dependent_groups` is used for analyzing data where the groups are dependent, such as repeated measures on the same subjects. It requires data in wide format. If your data is in long format, you can use the `convert_long_to_wide` function in `abalytics.utils` to convert it. The `analyze_dependent_groups` function takes a pandas DataFrame, the names of the columns to compare, and optional parameters for p-value threshold and minimum sample size.
|
|
50
|
+
|
|
51
|
+
Example:
|
|
52
|
+
```python
|
|
53
|
+
from abalytics import analyze_dependent_groups
|
|
54
|
+
import pandas as pd
|
|
55
|
+
|
|
56
|
+
# Load your data into a pandas DataFrame
|
|
57
|
+
df = pd.read_csv('your_data.csv')
|
|
58
|
+
|
|
59
|
+
# Analyze the results
|
|
60
|
+
analysis_results = analyze_dependent_groups(
|
|
61
|
+
df,
|
|
62
|
+
variables_to_compare=["pre_test_score", "post_test_score"],
|
|
63
|
+
)
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
### Data Transformation
|
|
67
|
+
|
|
68
|
+
The `convert_long_to_wide` function in `abalytics.utils` is designed to transform data from long format to wide format, with an option to keep multi-level columns or flatten them. `analyze_dependent_groups` requires data in wide format to operate correctly.
|
|
69
|
+
|
|
70
|
+
Example:
|
|
71
|
+
```python
|
|
72
|
+
from abalytics.utils import convert_long_to_wide
|
|
73
|
+
import pandas as pd
|
|
74
|
+
|
|
75
|
+
# Assuming 'df_long' is your pandas DataFrame in long format
|
|
76
|
+
df_wide = convert_long_to_wide(
|
|
77
|
+
df_long,
|
|
78
|
+
index_col="subject_id",
|
|
79
|
+
columns_col="condition",
|
|
80
|
+
flatten_columns=True # Set to False if you wish to keep multi-level columns
|
|
81
|
+
)
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
### Generating Pretty Text Output
|
|
85
|
+
|
|
86
|
+
To get a formatted text output of your results, you can use the `utils.format_results_as_table` function.
|
|
87
|
+
|
|
88
|
+
Example:
|
|
89
|
+
```python
|
|
90
|
+
from abalytics.utils import format_results_as_table
|
|
91
|
+
from abalytics import analyze_independent_groups
|
|
92
|
+
import pandas as pd
|
|
93
|
+
|
|
94
|
+
# Load your data into a pandas DataFrame
|
|
95
|
+
df = pd.read_csv('your_data.csv')
|
|
96
|
+
|
|
97
|
+
# Analyze the results
|
|
98
|
+
analysis_results = analyze_independent_groups(
|
|
99
|
+
df,
|
|
100
|
+
variable_to_analyze="order_value",
|
|
101
|
+
group_column="ab_test_group",
|
|
102
|
+
)
|
|
103
|
+
|
|
104
|
+
# Generate pretty text output
|
|
105
|
+
pretty_text = format_results_as_table(
|
|
106
|
+
abalytics_results=[analysis_results],
|
|
107
|
+
identifiers_list=[["A/B Test 1", "Mobile"]],
|
|
108
|
+
)
|
|
109
|
+
print(pretty_text)
|
|
110
|
+
```
|
|
111
|
+
Executing this code will output a neatly formatted table displaying the outcomes of the statistical significance tests. The table includes the sample size, indicators for Levene's test and Gaussian distribution, and the test results.
|
|
112
|
+
|
|
113
|
+
A further example of how to use ABalytics can be found in `examples/example.py`.
|
|
114
|
+
|
|
115
|
+
## Contributing
|
|
116
|
+
|
|
117
|
+
Contributions to ABalytics are welcome. If you have suggestions for improvements or find any issues, please open an issue or submit a pull request.
|
|
@@ -0,0 +1,271 @@
|
|
|
1
|
+
from typing import Optional, List
|
|
2
|
+
|
|
3
|
+
import pandas as pd
|
|
4
|
+
from pydantic import BaseModel, Field
|
|
5
|
+
|
|
6
|
+
from .significance_tests import (
|
|
7
|
+
is_dichotomous,
|
|
8
|
+
is_numeric,
|
|
9
|
+
is_levene_significant,
|
|
10
|
+
is_gaussian,
|
|
11
|
+
get_chi_square_significance,
|
|
12
|
+
get_chi_square_posthoc_results,
|
|
13
|
+
get_welch_anova_significance,
|
|
14
|
+
get_oneway_anova_significance,
|
|
15
|
+
get_crushal_wallis_significance,
|
|
16
|
+
get_games_howell_posthoc_results,
|
|
17
|
+
get_tukeyhsd_posthoc_results,
|
|
18
|
+
get_dunn_posthoc_results,
|
|
19
|
+
get_mcnemar_results,
|
|
20
|
+
get_repeated_measures_anova_significance,
|
|
21
|
+
get_repeated_measures_anova_posthoc_results,
|
|
22
|
+
get_wilcoxon_results,
|
|
23
|
+
get_friedman_significance,
|
|
24
|
+
get_nemenyi_results,
|
|
25
|
+
)
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
class AnalysisResults(BaseModel):
|
|
29
|
+
significant_results: Optional[List] = Field(
|
|
30
|
+
default=[],
|
|
31
|
+
description="A list of significant results from the statistical tests.",
|
|
32
|
+
)
|
|
33
|
+
info: Optional[str] = Field(
|
|
34
|
+
default=None,
|
|
35
|
+
description="Additional information or notes regarding the analysis.",
|
|
36
|
+
)
|
|
37
|
+
sample_size: Optional[int] = Field(
|
|
38
|
+
default=None, description="The sample size used in the analysis."
|
|
39
|
+
)
|
|
40
|
+
dichotomous_flag: Optional[bool] = Field(
|
|
41
|
+
default=None,
|
|
42
|
+
description="Indicates if the data analyzed is dichotomous.",
|
|
43
|
+
)
|
|
44
|
+
levene_flag: Optional[bool] = Field(
|
|
45
|
+
default=None,
|
|
46
|
+
description="Indicates if Levene's test for homogeneity of variances is significant.",
|
|
47
|
+
)
|
|
48
|
+
gaussian_flag: Optional[bool] = Field(
|
|
49
|
+
default=None,
|
|
50
|
+
description="Indicates if the data follows a Gaussian distribution.",
|
|
51
|
+
)
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
def analyze_independent_groups(
|
|
55
|
+
df: pd.DataFrame,
|
|
56
|
+
variable_to_analyze: str,
|
|
57
|
+
group_column: str,
|
|
58
|
+
p_value_threshold: float = 0.05,
|
|
59
|
+
min_sample_size: int = 25,
|
|
60
|
+
) -> Optional[AnalysisResults]:
|
|
61
|
+
"""
|
|
62
|
+
Returns a text with the results of the statistical significance tests.
|
|
63
|
+
|
|
64
|
+
Parameters:
|
|
65
|
+
df (pandas.DataFrame): The dataframe containing the data to analyze.
|
|
66
|
+
variable_to_analyze (str): The column name in df that is to be analyzed for statistical significance.
|
|
67
|
+
group_column (str): The name of the column in df that contains the grouping variable.
|
|
68
|
+
p_value_threshold (float, optional): The threshold for determining statistical significance. Defaults to 0.05.
|
|
69
|
+
min_sample_size (int, optional): The minimum sample size for the data. Defaults to 25.
|
|
70
|
+
|
|
71
|
+
Returns:
|
|
72
|
+
AnalysisResults: An instance of the AnalysisResults class containing the following attributes:
|
|
73
|
+
- significant_results: A list of results of the statistical significance tests.
|
|
74
|
+
- info: A string containing information about the data if no results were found.
|
|
75
|
+
- sample_size: The sample size of the data.
|
|
76
|
+
- dichotomous_flag: A boolean flag indicating if the data is dichotomous (e.g. boolean).
|
|
77
|
+
- levene_flag: A boolean flag indicating if Levene's test for homogeneity of variances is significant.
|
|
78
|
+
- gaussian_flag: A boolean flag indicating if the data has a Gaussian distribution.
|
|
79
|
+
"""
|
|
80
|
+
if p_value_threshold > 0.05:
|
|
81
|
+
print(
|
|
82
|
+
"Warning: p_value_threshold is set to a value higher than the conventional alpha level of 0.05."
|
|
83
|
+
)
|
|
84
|
+
|
|
85
|
+
if variable_to_analyze not in df.columns or group_column not in df.columns:
|
|
86
|
+
print(
|
|
87
|
+
f"Both columns {variable_to_analyze} and {group_column} have to be present in the DataFrame."
|
|
88
|
+
)
|
|
89
|
+
return None
|
|
90
|
+
|
|
91
|
+
df = df.dropna(subset=[variable_to_analyze])
|
|
92
|
+
sample_size = len(df)
|
|
93
|
+
pvalue = None
|
|
94
|
+
info = None
|
|
95
|
+
dichotomous_flag = False
|
|
96
|
+
levene_flag = False
|
|
97
|
+
gaussian_flag = False
|
|
98
|
+
# Check if any group has less than the minimum sample size
|
|
99
|
+
if (
|
|
100
|
+
df.groupby(group_column)[variable_to_analyze]
|
|
101
|
+
.apply(lambda x: len(x) < min_sample_size)
|
|
102
|
+
.any()
|
|
103
|
+
):
|
|
104
|
+
info = "not enough data"
|
|
105
|
+
# Check if the variable is dichotomous (e.g. boolean)
|
|
106
|
+
elif dichotomous_flag := is_dichotomous(df, variable_to_analyze):
|
|
107
|
+
# Perform chi-square test
|
|
108
|
+
pvalue = get_chi_square_significance(
|
|
109
|
+
df, group_column, variable_to_analyze
|
|
110
|
+
)
|
|
111
|
+
if pvalue < p_value_threshold:
|
|
112
|
+
results = get_chi_square_posthoc_results(
|
|
113
|
+
df, group_column, variable_to_analyze, p_value_threshold
|
|
114
|
+
)
|
|
115
|
+
elif is_numeric(df, variable_to_analyze):
|
|
116
|
+
# Check homogeneity of variances. If significant, use non-parametric tests
|
|
117
|
+
if levene_flag := is_levene_significant(
|
|
118
|
+
df, group_column, variable_to_analyze, p_value_threshold
|
|
119
|
+
):
|
|
120
|
+
pvalue = get_welch_anova_significance(
|
|
121
|
+
df, group_column, variable_to_analyze
|
|
122
|
+
)
|
|
123
|
+
if pvalue < p_value_threshold:
|
|
124
|
+
results = get_games_howell_posthoc_results(
|
|
125
|
+
df, group_column, variable_to_analyze, p_value_threshold
|
|
126
|
+
)
|
|
127
|
+
# Check if the data has Gaussian distribution, if not use non-parametric tests
|
|
128
|
+
elif gaussian_flag := is_gaussian(
|
|
129
|
+
df, variable_to_analyze, p_value_threshold, group_column
|
|
130
|
+
):
|
|
131
|
+
pvalue = get_oneway_anova_significance(
|
|
132
|
+
df, group_column, variable_to_analyze
|
|
133
|
+
)
|
|
134
|
+
if pvalue < p_value_threshold:
|
|
135
|
+
results = get_tukeyhsd_posthoc_results(
|
|
136
|
+
df, group_column, variable_to_analyze, p_value_threshold
|
|
137
|
+
)
|
|
138
|
+
else:
|
|
139
|
+
pvalue = get_crushal_wallis_significance(
|
|
140
|
+
df, group_column, variable_to_analyze
|
|
141
|
+
)
|
|
142
|
+
if pvalue < p_value_threshold:
|
|
143
|
+
results = get_dunn_posthoc_results(
|
|
144
|
+
df, group_column, variable_to_analyze, p_value_threshold
|
|
145
|
+
)
|
|
146
|
+
else:
|
|
147
|
+
print("Variable has to be dichotomous (e.g. boolean) or numeric")
|
|
148
|
+
info = "Variable has to be dichotomous (e.g. boolean) or numeric"
|
|
149
|
+
|
|
150
|
+
# If no results were found, return a message
|
|
151
|
+
if not "results" in locals() or len(results.significant_results) == 0:
|
|
152
|
+
if pvalue is not None:
|
|
153
|
+
info = f"not significant (p={pvalue:.3f})"
|
|
154
|
+
# If results were found, return them
|
|
155
|
+
if "results" in locals():
|
|
156
|
+
significant_results = results.significant_results
|
|
157
|
+
else:
|
|
158
|
+
significant_results = []
|
|
159
|
+
|
|
160
|
+
return AnalysisResults(
|
|
161
|
+
significant_results=significant_results,
|
|
162
|
+
info=info,
|
|
163
|
+
sample_size=sample_size,
|
|
164
|
+
dichotomous_flag=dichotomous_flag,
|
|
165
|
+
levene_flag=levene_flag,
|
|
166
|
+
gaussian_flag=gaussian_flag,
|
|
167
|
+
)
|
|
168
|
+
|
|
169
|
+
|
|
170
|
+
def analyze_dependent_groups(
|
|
171
|
+
df: pd.DataFrame,
|
|
172
|
+
variables_to_compare: List[str],
|
|
173
|
+
p_value_threshold: float = 0.05,
|
|
174
|
+
min_sample_size: int = 25,
|
|
175
|
+
) -> Optional[AnalysisResults]:
|
|
176
|
+
"""
|
|
177
|
+
Analyzes dependent groups using appropriate statistical tests and returns an AnalysisResults object.
|
|
178
|
+
|
|
179
|
+
Parameters:
|
|
180
|
+
df (pandas.DataFrame): The dataframe containing the data to analyze.
|
|
181
|
+
variables_to_compare (List[str]): The column names in df that are to be analyzed for statistical significance.
|
|
182
|
+
p_value_threshold (float, optional): The threshold for determining statistical significance. Defaults to 0.05.
|
|
183
|
+
min_sample_size (int, optional): The minimum sample size for the data. Defaults to 25.
|
|
184
|
+
|
|
185
|
+
Returns:
|
|
186
|
+
AnalysisResults: An instance of the AnalysisResults class containing the following attributes:
|
|
187
|
+
- significant_results: A list of results of the statistical significance tests.
|
|
188
|
+
- info: A string containing information about the data if no results were found.
|
|
189
|
+
- sample_size: The sample size of the data.
|
|
190
|
+
- dichotomous_flag: A boolean flag indicating if the data is dichotomous (e.g. boolean).
|
|
191
|
+
- levene_flag: A boolean flag indicating if Levene's test for homogeneity of variances is significant.
|
|
192
|
+
- gaussian_flag: A boolean flag indicating if the data has a Gaussian distribution.
|
|
193
|
+
"""
|
|
194
|
+
if p_value_threshold > 0.05:
|
|
195
|
+
print(
|
|
196
|
+
"Warning: p_value_threshold is set to a value higher than the conventional alpha level of 0.05."
|
|
197
|
+
)
|
|
198
|
+
|
|
199
|
+
missing_columns = [
|
|
200
|
+
variable
|
|
201
|
+
for variable in variables_to_compare
|
|
202
|
+
if variable not in df.columns
|
|
203
|
+
]
|
|
204
|
+
if missing_columns:
|
|
205
|
+
print(f"Columns not found in DataFrame: {', '.join(missing_columns)}")
|
|
206
|
+
return None
|
|
207
|
+
|
|
208
|
+
df = df.dropna(subset=variables_to_compare)
|
|
209
|
+
sample_size = len(df)
|
|
210
|
+
pvalue = None
|
|
211
|
+
info = None
|
|
212
|
+
dichotomous_flag = False
|
|
213
|
+
levene_flag = False
|
|
214
|
+
gaussian_flag = False
|
|
215
|
+
results = None
|
|
216
|
+
|
|
217
|
+
# Check if any variable has less than the minimum sample size
|
|
218
|
+
if any(
|
|
219
|
+
df[variable].count() < min_sample_size
|
|
220
|
+
for variable in variables_to_compare
|
|
221
|
+
):
|
|
222
|
+
info = "not enough data"
|
|
223
|
+
# Check if the variables are dichotomous (e.g. boolean)
|
|
224
|
+
elif all(is_dichotomous(df, variable) for variable in variables_to_compare):
|
|
225
|
+
dichotomous_flag = True
|
|
226
|
+
# Perform McNemar's test or its extension for more than two groups
|
|
227
|
+
pvalue, results = get_mcnemar_results(
|
|
228
|
+
df, variables_to_compare, p_value_threshold
|
|
229
|
+
)
|
|
230
|
+
elif all(is_numeric(df, variable) for variable in variables_to_compare):
|
|
231
|
+
# Check if the data has Gaussian distribution, if not use non-parametric tests
|
|
232
|
+
if gaussian_flag := all(
|
|
233
|
+
is_gaussian(df, variable, p_value_threshold)
|
|
234
|
+
for variable in variables_to_compare
|
|
235
|
+
):
|
|
236
|
+
pvalue = get_repeated_measures_anova_significance(
|
|
237
|
+
df, variables_to_compare
|
|
238
|
+
)
|
|
239
|
+
if pvalue < p_value_threshold:
|
|
240
|
+
results = get_repeated_measures_anova_posthoc_results(
|
|
241
|
+
df, variables_to_compare, p_value_threshold
|
|
242
|
+
)
|
|
243
|
+
elif len(variables_to_compare) == 2:
|
|
244
|
+
pvalue, results = get_wilcoxon_results(
|
|
245
|
+
df, variables_to_compare, p_value_threshold
|
|
246
|
+
)
|
|
247
|
+
else:
|
|
248
|
+
pvalue = get_friedman_significance(df, variables_to_compare)
|
|
249
|
+
if pvalue < p_value_threshold:
|
|
250
|
+
results = get_nemenyi_results(
|
|
251
|
+
df, variables_to_compare, p_value_threshold
|
|
252
|
+
)
|
|
253
|
+
else:
|
|
254
|
+
print("All variables have to be dichotomous (e.g. boolean) or numeric")
|
|
255
|
+
info = "All variables have to be dichotomous (e.g. boolean) or numeric"
|
|
256
|
+
|
|
257
|
+
# If no results were found, return a message
|
|
258
|
+
if not results or len(results.significant_results) == 0:
|
|
259
|
+
if pvalue is not None:
|
|
260
|
+
info = f"not significant (p={pvalue:.3f})"
|
|
261
|
+
# If results were found, return them
|
|
262
|
+
significant_results = results.significant_results if results else []
|
|
263
|
+
|
|
264
|
+
return AnalysisResults(
|
|
265
|
+
significant_results=significant_results,
|
|
266
|
+
info=info,
|
|
267
|
+
sample_size=sample_size,
|
|
268
|
+
dichotomous_flag=dichotomous_flag,
|
|
269
|
+
levene_flag=levene_flag,
|
|
270
|
+
gaussian_flag=gaussian_flag,
|
|
271
|
+
)
|