singlecase
Advanced tools
| from typing import Union, Dict | ||
| import pandas as pd | ||
| class Data: | ||
| """ | ||
| A class used to represent single-case data in a structured format. | ||
| This class accepts data either as a pandas DataFrame or as a dictionary and | ||
| stores it internally as a DataFrame for further manipulation and analysis. It | ||
| provides properties to manage the phase variable and dependent variables present | ||
| in the data. | ||
| Any variable with datatype of float is assumed to be a dependent variable. The | ||
| Args: | ||
| data (Union[pd.DataFrame, Dict]): The data set. Must be either a pandas DataFrame | ||
| or a dictionary. | ||
| Raises: | ||
| ValueError: If the provided data is not a pandas DataFrame or a dictionary. | ||
| """ | ||
| def __init__(self, data: Union[pd.DataFrame, Dict]): | ||
| """ | ||
| Constructs and initializes the Data object. | ||
| """ | ||
| if isinstance(data, dict): | ||
| self._df = pd.DataFrame(data) | ||
| elif isinstance(data, pd.DataFrame): | ||
| self._df = data.copy() | ||
| else: | ||
| raise ValueError("data must be a pd.DataFrame or a dict") | ||
| self._pvar = None | ||
| # float columns are assumend to be dependent variables | ||
| self._dvars = [col for col in self._df.columns if self._df[col].dtype == float] | ||
| @property | ||
| def pvar(self): | ||
| """ | ||
| Property that gets the phase variable of the Data object. | ||
| Returns: | ||
| str: The name of the phase variable column in the data frame. | ||
| """ | ||
| return self._pvar | ||
| @pvar.setter | ||
| def pvar(self, pvar: str): | ||
| """ | ||
| Property that sets the phase variable of the Data object. | ||
| Args: | ||
| pvar (str): The name of the phase variable column to be set. | ||
| Raises: | ||
| ValueError: If the provided pvar is not a column name in the DataFrame. | ||
| """ | ||
| if pvar not in self._df.columns: | ||
| raise ValueError("pvar must be the name of a column in the data frame") | ||
| self._pvar = pvar | ||
| @property | ||
| def dvars(self): | ||
| """ | ||
| Property that gets the names of the dependent variable columns. | ||
| These are identified as any columns in the DataFrame with float datatype. | ||
| Returns: | ||
| List[str]: The names of the dependent variable columns. | ||
| """ | ||
| return self._dvars | ||
+73
-54
| Metadata-Version: 2.1 | ||
| Name: singlecase | ||
| Version: 0.1.0 | ||
| Summary: Work with data from single case study designs | ||
| Version: 0.2.0 | ||
| Summary: A tool for single-case design data managment, statistical analysis and visualization. | ||
| Author-email: Casper Wilstrup <casper.wilstrup@abzu.ai> | ||
@@ -36,8 +36,16 @@ License: BSD 3-Clause License | ||
| Project-URL: Homepage, https://github.com/wilstrup/singlecase | ||
| Keywords: single case,permutation test,pnd,nap,effect size | ||
| Keywords: single case,permutation test,pnd,nap,effect size,non-overlapping,statistical analysis,research | ||
| Classifier: License :: OSI Approved :: BSD License | ||
| Classifier: Programming Language :: Python | ||
| Classifier: Programming Language :: Python :: 3 | ||
| Classifier: Programming Language :: Python :: 3.8 | ||
| Requires-Python: >=3.9 | ||
| Classifier: Programming Language :: Python :: 3.9 | ||
| Classifier: Programming Language :: Python :: 3.10 | ||
| Classifier: Intended Audience :: Science/Research | ||
| Classifier: Intended Audience :: Healthcare Industry | ||
| Classifier: Intended Audience :: Education | ||
| Classifier: Topic :: Scientific/Engineering :: Mathematics | ||
| Classifier: Topic :: Scientific/Engineering :: Visualization | ||
| Classifier: Topic :: Scientific/Engineering :: Information Analysis | ||
| Classifier: Operating System :: OS Independent | ||
| Requires-Python: >=3.8 | ||
| Description-Content-Type: text/markdown | ||
@@ -49,3 +57,3 @@ Provides-Extra: dev | ||
| The Single Case Research package is a Python library designed to assist in conducting single-case research studies. It provides functions to calculate effect sizes and perform permutation tests between two phases in a single-case data frame. | ||
| The Single Case Research package is a Python library designed to assist in conducting single-case research studies. The package provides tools to analyze single-case data sets. It is designed for simplicity and ease of use, with a range of methods to calculate various statistical measures for single-case data sets. The package is perfect for analysts and researchers dealing with single-case designs, providing a framework to load, manage, and manipulate such data, along with robust statistical functions to interpret the data. | ||
@@ -62,81 +70,92 @@ More functionality will be added as the package is further developed | ||
| ## Functionality | ||
| The package currently includes the following functions: | ||
| ## Usage | ||
| ### Effect Size Calculation | ||
| To use `singlecase`, first import the package into your Python environment: | ||
| #### `nap(data: pd.DataFrame, dvars: Union[List[str], str], pvar: str, decreasing: bool = False, phases: Tuple[str, str] = ("A", "B")) -> pd.Series` | ||
| ```python | ||
| from singlecase.data import Data | ||
| ``` | ||
| Calculate the Nonoverlap Pairs (NAP) between two phases in a single-case data frame. | ||
| Then, create a `Data` object with either a pandas DataFrame or a dictionary: | ||
| - `data`: A single-case data frame. | ||
| - `dvars`: One or more dependent variables to calculate NAP for. | ||
| - `pvar`: The name of the phase variable. | ||
| - `decreasing`: If you expect data to be lower in the second phase, set `decreasing=True`. Default is `decreasing=False`. | ||
| - `phases`: A tuple of two column names in the data frame indicating the two phases that should be compared. Default is `("A", "B")`. | ||
| ```python | ||
| df = pd.DataFrame({...}) # Or load your data from a CSV, database, etc. | ||
| data = Data(df) | ||
| ``` | ||
| Returns the calculated NAP values in a Pandas Series. | ||
| Now you're ready to perform single-case data analysis! | ||
| #### `pnd(data: pd.DataFrame, dvars: Union[List[str], str], pvar: str, decreasing: bool = False, phases: Tuple[str, str] = ("A", "B")) -> pd.Series` | ||
| ## Core Features | ||
| Calculate the Percent Non-overlapping Data (PND) between two phases in a single-case data frame. | ||
| ### Data Class | ||
| - `data`: A single-case data frame. | ||
| - `dvars`: One or more dependent variables to calculate PND for. | ||
| - `pvar`: The name of the phase variable. | ||
| - `decreasing`: If you expect data to be lower in the second phase, set `decreasing=True`. Default is `decreasing=False`. | ||
| - `phases`: A tuple of two column names in the data frame indicating the two phases that should be compared. Default is `("A", "B")`. | ||
| The `Data` class provides an object-oriented interface to represent your single-case data. It assumes any variable with datatype of float as a dependent variable. The dependent and phase variables can be accessed and modified using properties. For example: | ||
| Returns the calculated PND values in a Pandas Series. | ||
| ```python | ||
| data.pvar = 'column_name' # Set the phase variable | ||
| ``` | ||
| ### Permutation Test | ||
| ### Nonoverlap Pairs (NAP) | ||
| #### `permutation_test(data: pd.DataFrame, dvars: Union[List[str], str], pvar: str, statistic: Union[str, Callable] = 'mean', phases: Tuple[str, str] = ("A", "B"), num_rounds: int = 10000, seed: int = None) -> pd.Series` | ||
| The `nap` function computes the Nonoverlap Pairs between two phases in a single-case data frame. It returns a pandas Series containing the NAP values for each dependent variable in the data set. | ||
| Perform a permutation test between two phases in a single-case data frame. | ||
| ```python | ||
| from singlecase.effectsize import nap | ||
| nap_values = nap(data) | ||
| ``` | ||
| - `data`: A single-case data frame. | ||
| - `dvars`: One or more dependent variables to perform the permutation test on. | ||
| - `pvar`: The name of the phase variable. | ||
| - `statistic`: The statistic to be used in the permutation test (either 'mean', 'median', or a custom callable). Default is `'mean'`. | ||
| - `phases`: A tuple of two column names in the data frame indicating the two phases that should be compared. Default is `("A", "B")`. | ||
| - `num_rounds`: The number of iterations for the permutation test. Default is `10000`. | ||
| - `seed`: Random seed for reproducibility. Default is `None`. | ||
| ### Percent Non-overlapping Data (PND) | ||
| Returns the calculated p-values in a Pandas Series. | ||
| The `pnd` function computes the Percent Non-overlapping Data between two phases in a single-case data frame. It returns a pandas Series containing the PND values for each dependent variable in the data set. | ||
| ## Usage | ||
| ```python | ||
| from singlecase.effectsize import pnd | ||
| pnd_values = pnd(data) | ||
| ``` | ||
| Here's a basic example demonstrating how to use the functions in the Single Case Research package: | ||
| ### Permutation Test | ||
| The `permutation_test` function performs a permutation test between two phases in a single-case data frame. It returns a pandas Series containing the p-values for each dependent variable in the data set. | ||
| ```python | ||
| import pandas as pd | ||
| from singlecase.effectsize import nap, pnd | ||
| from singlecase.permtest import permutation_test | ||
| p_values = permutation_test(data) | ||
| ``` | ||
| # Load your single-case data into a Pandas DataFrame | ||
| ## Complete example | ||
| This complete example creates a new `singlecase.Data` from a Python dictionary. The dataset has two dependent variables `dvar1` and `dvar2`. The phase variable is called `phase` and consists of the two phases "A" and "B". Based on this data, the PND and NAP values are calculated and printed. | ||
| # Calculate NAP | ||
| nap_values = nap(data, dvars=['dependent_var1', 'dependent_var2'], pvar='phase_var') | ||
| ```python | ||
| import pandas as pd | ||
| from singlecase.data import Data | ||
| from singlecase.effectsize import pnd, nap | ||
| # Calculate PND | ||
| pnd_values = pnd(data, dvars='dependent_var1', pvar='phase_var') | ||
| # Create a sample data | ||
| data_dict = { | ||
| 'dvar1': [1.0, 2.5, 3.2, 4.6, 2.8, 5.6, 3.7, 4.2, 5.5, 6.2, 7.3, 8.5], | ||
| 'dvar2': [2.5, 3.2, 4.6, 5.1, 4.8, 5.2, 6.7, 5.6, 6.2, 7.8, 8.4, 7.2], | ||
| 'phase': ['A', 'A', 'A', 'A', 'A', 'A', 'B', 'B', 'B', 'B', 'B', 'B'] | ||
| } | ||
| # Perform permutation test | ||
| p_values = permutation_test(data, dvars='dependent_var1', pvar='phase_var') | ||
| df = pd.DataFrame(data_dict) | ||
| # Print the results | ||
| print("NAP Values:") | ||
| print(nap_values) | ||
| # Instantiate Data object | ||
| data = Data(df) | ||
| print("PND Values:") | ||
| print(pnd_values) | ||
| # Set phase variable | ||
| data.pvar = 'phase' | ||
| print("P-Values:") | ||
| print(p_values) | ||
| # Calculate PND | ||
| pnd_values = pnd(data) | ||
| print(f"PND values: \n{pnd_values}") | ||
| # Calculate NAP | ||
| nap_values = nap(data) | ||
| print(f"NAP values: \n{nap_values}") | ||
| ``` | ||
| ## License | ||
@@ -143,0 +162,0 @@ |
+13
-5
@@ -11,4 +11,4 @@ [build-system] | ||
| name = "singlecase" | ||
| version = "0.1.0" | ||
| description = "Work with data from single case study designs" | ||
| version = "0.2.0" | ||
| description = "A tool for single-case design data managment, statistical analysis and visualization." | ||
| readme = "README.md" | ||
@@ -20,6 +20,14 @@ authors = [{ name = "Casper Wilstrup", email = "casper.wilstrup@abzu.ai" }] | ||
| "Programming Language :: Python", | ||
| "Programming Language :: Python :: 3", | ||
| "Programming Language :: Python :: 3.8", | ||
| "Programming Language :: Python :: 3.9", | ||
| "Programming Language :: Python :: 3.10", | ||
| "Intended Audience :: Science/Research", | ||
| "Intended Audience :: Healthcare Industry", | ||
| "Intended Audience :: Education", | ||
| "Topic :: Scientific/Engineering :: Mathematics", | ||
| "Topic :: Scientific/Engineering :: Visualization", | ||
| "Topic :: Scientific/Engineering :: Information Analysis", | ||
| "Operating System :: OS Independent" | ||
| ] | ||
| keywords = ["single case", "permutation test", "pnd", "nap", "effect size"] | ||
| keywords = ["single case", "permutation test", "pnd", "nap", "effect size", "non-overlapping", "statistical analysis", "research"] | ||
| dependencies = [ | ||
@@ -30,3 +38,3 @@ "numpy", | ||
| ] | ||
| requires-python = ">=3.9" | ||
| requires-python = ">=3.8" | ||
@@ -33,0 +41,0 @@ [project.optional-dependencies] |
+60
-49
| # Single Case Research Package | ||
| The Single Case Research package is a Python library designed to assist in conducting single-case research studies. It provides functions to calculate effect sizes and perform permutation tests between two phases in a single-case data frame. | ||
| The Single Case Research package is a Python library designed to assist in conducting single-case research studies. The package provides tools to analyze single-case data sets. It is designed for simplicity and ease of use, with a range of methods to calculate various statistical measures for single-case data sets. The package is perfect for analysts and researchers dealing with single-case designs, providing a framework to load, manage, and manipulate such data, along with robust statistical functions to interpret the data. | ||
@@ -15,81 +15,92 @@ More functionality will be added as the package is further developed | ||
| ## Functionality | ||
| The package currently includes the following functions: | ||
| ## Usage | ||
| ### Effect Size Calculation | ||
| To use `singlecase`, first import the package into your Python environment: | ||
| #### `nap(data: pd.DataFrame, dvars: Union[List[str], str], pvar: str, decreasing: bool = False, phases: Tuple[str, str] = ("A", "B")) -> pd.Series` | ||
| ```python | ||
| from singlecase.data import Data | ||
| ``` | ||
| Calculate the Nonoverlap Pairs (NAP) between two phases in a single-case data frame. | ||
| Then, create a `Data` object with either a pandas DataFrame or a dictionary: | ||
| - `data`: A single-case data frame. | ||
| - `dvars`: One or more dependent variables to calculate NAP for. | ||
| - `pvar`: The name of the phase variable. | ||
| - `decreasing`: If you expect data to be lower in the second phase, set `decreasing=True`. Default is `decreasing=False`. | ||
| - `phases`: A tuple of two column names in the data frame indicating the two phases that should be compared. Default is `("A", "B")`. | ||
| ```python | ||
| df = pd.DataFrame({...}) # Or load your data from a CSV, database, etc. | ||
| data = Data(df) | ||
| ``` | ||
| Returns the calculated NAP values in a Pandas Series. | ||
| Now you're ready to perform single-case data analysis! | ||
| #### `pnd(data: pd.DataFrame, dvars: Union[List[str], str], pvar: str, decreasing: bool = False, phases: Tuple[str, str] = ("A", "B")) -> pd.Series` | ||
| ## Core Features | ||
| Calculate the Percent Non-overlapping Data (PND) between two phases in a single-case data frame. | ||
| ### Data Class | ||
| - `data`: A single-case data frame. | ||
| - `dvars`: One or more dependent variables to calculate PND for. | ||
| - `pvar`: The name of the phase variable. | ||
| - `decreasing`: If you expect data to be lower in the second phase, set `decreasing=True`. Default is `decreasing=False`. | ||
| - `phases`: A tuple of two column names in the data frame indicating the two phases that should be compared. Default is `("A", "B")`. | ||
| The `Data` class provides an object-oriented interface to represent your single-case data. It assumes any variable with datatype of float as a dependent variable. The dependent and phase variables can be accessed and modified using properties. For example: | ||
| Returns the calculated PND values in a Pandas Series. | ||
| ```python | ||
| data.pvar = 'column_name' # Set the phase variable | ||
| ``` | ||
| ### Permutation Test | ||
| ### Nonoverlap Pairs (NAP) | ||
| #### `permutation_test(data: pd.DataFrame, dvars: Union[List[str], str], pvar: str, statistic: Union[str, Callable] = 'mean', phases: Tuple[str, str] = ("A", "B"), num_rounds: int = 10000, seed: int = None) -> pd.Series` | ||
| The `nap` function computes the Nonoverlap Pairs between two phases in a single-case data frame. It returns a pandas Series containing the NAP values for each dependent variable in the data set. | ||
| Perform a permutation test between two phases in a single-case data frame. | ||
| ```python | ||
| from singlecase.effectsize import nap | ||
| nap_values = nap(data) | ||
| ``` | ||
| - `data`: A single-case data frame. | ||
| - `dvars`: One or more dependent variables to perform the permutation test on. | ||
| - `pvar`: The name of the phase variable. | ||
| - `statistic`: The statistic to be used in the permutation test (either 'mean', 'median', or a custom callable). Default is `'mean'`. | ||
| - `phases`: A tuple of two column names in the data frame indicating the two phases that should be compared. Default is `("A", "B")`. | ||
| - `num_rounds`: The number of iterations for the permutation test. Default is `10000`. | ||
| - `seed`: Random seed for reproducibility. Default is `None`. | ||
| ### Percent Non-overlapping Data (PND) | ||
| Returns the calculated p-values in a Pandas Series. | ||
| The `pnd` function computes the Percent Non-overlapping Data between two phases in a single-case data frame. It returns a pandas Series containing the PND values for each dependent variable in the data set. | ||
| ## Usage | ||
| ```python | ||
| from singlecase.effectsize import pnd | ||
| pnd_values = pnd(data) | ||
| ``` | ||
| Here's a basic example demonstrating how to use the functions in the Single Case Research package: | ||
| ### Permutation Test | ||
| The `permutation_test` function performs a permutation test between two phases in a single-case data frame. It returns a pandas Series containing the p-values for each dependent variable in the data set. | ||
| ```python | ||
| import pandas as pd | ||
| from singlecase.effectsize import nap, pnd | ||
| from singlecase.permtest import permutation_test | ||
| p_values = permutation_test(data) | ||
| ``` | ||
| # Load your single-case data into a Pandas DataFrame | ||
| ## Complete example | ||
| This complete example creates a new `singlecase.Data` from a Python dictionary. The dataset has two dependent variables `dvar1` and `dvar2`. The phase variable is called `phase` and consists of the two phases "A" and "B". Based on this data, the PND and NAP values are calculated and printed. | ||
| # Calculate NAP | ||
| nap_values = nap(data, dvars=['dependent_var1', 'dependent_var2'], pvar='phase_var') | ||
| ```python | ||
| import pandas as pd | ||
| from singlecase.data import Data | ||
| from singlecase.effectsize import pnd, nap | ||
| # Calculate PND | ||
| pnd_values = pnd(data, dvars='dependent_var1', pvar='phase_var') | ||
| # Create a sample data | ||
| data_dict = { | ||
| 'dvar1': [1.0, 2.5, 3.2, 4.6, 2.8, 5.6, 3.7, 4.2, 5.5, 6.2, 7.3, 8.5], | ||
| 'dvar2': [2.5, 3.2, 4.6, 5.1, 4.8, 5.2, 6.7, 5.6, 6.2, 7.8, 8.4, 7.2], | ||
| 'phase': ['A', 'A', 'A', 'A', 'A', 'A', 'B', 'B', 'B', 'B', 'B', 'B'] | ||
| } | ||
| # Perform permutation test | ||
| p_values = permutation_test(data, dvars='dependent_var1', pvar='phase_var') | ||
| df = pd.DataFrame(data_dict) | ||
| # Print the results | ||
| print("NAP Values:") | ||
| print(nap_values) | ||
| # Instantiate Data object | ||
| data = Data(df) | ||
| print("PND Values:") | ||
| print(pnd_values) | ||
| # Set phase variable | ||
| data.pvar = 'phase' | ||
| print("P-Values:") | ||
| print(p_values) | ||
| # Calculate PND | ||
| pnd_values = pnd(data) | ||
| print(f"PND values: \n{pnd_values}") | ||
| # Calculate NAP | ||
| nap_values = nap(data) | ||
| print(f"NAP values: \n{nap_values}") | ||
| ``` | ||
| ## License | ||
@@ -96,0 +107,0 @@ |
| Metadata-Version: 2.1 | ||
| Name: singlecase | ||
| Version: 0.1.0 | ||
| Summary: Work with data from single case study designs | ||
| Version: 0.2.0 | ||
| Summary: A tool for single-case design data managment, statistical analysis and visualization. | ||
| Author-email: Casper Wilstrup <casper.wilstrup@abzu.ai> | ||
@@ -36,8 +36,16 @@ License: BSD 3-Clause License | ||
| Project-URL: Homepage, https://github.com/wilstrup/singlecase | ||
| Keywords: single case,permutation test,pnd,nap,effect size | ||
| Keywords: single case,permutation test,pnd,nap,effect size,non-overlapping,statistical analysis,research | ||
| Classifier: License :: OSI Approved :: BSD License | ||
| Classifier: Programming Language :: Python | ||
| Classifier: Programming Language :: Python :: 3 | ||
| Classifier: Programming Language :: Python :: 3.8 | ||
| Requires-Python: >=3.9 | ||
| Classifier: Programming Language :: Python :: 3.9 | ||
| Classifier: Programming Language :: Python :: 3.10 | ||
| Classifier: Intended Audience :: Science/Research | ||
| Classifier: Intended Audience :: Healthcare Industry | ||
| Classifier: Intended Audience :: Education | ||
| Classifier: Topic :: Scientific/Engineering :: Mathematics | ||
| Classifier: Topic :: Scientific/Engineering :: Visualization | ||
| Classifier: Topic :: Scientific/Engineering :: Information Analysis | ||
| Classifier: Operating System :: OS Independent | ||
| Requires-Python: >=3.8 | ||
| Description-Content-Type: text/markdown | ||
@@ -49,3 +57,3 @@ Provides-Extra: dev | ||
| The Single Case Research package is a Python library designed to assist in conducting single-case research studies. It provides functions to calculate effect sizes and perform permutation tests between two phases in a single-case data frame. | ||
| The Single Case Research package is a Python library designed to assist in conducting single-case research studies. The package provides tools to analyze single-case data sets. It is designed for simplicity and ease of use, with a range of methods to calculate various statistical measures for single-case data sets. The package is perfect for analysts and researchers dealing with single-case designs, providing a framework to load, manage, and manipulate such data, along with robust statistical functions to interpret the data. | ||
@@ -62,81 +70,92 @@ More functionality will be added as the package is further developed | ||
| ## Functionality | ||
| The package currently includes the following functions: | ||
| ## Usage | ||
| ### Effect Size Calculation | ||
| To use `singlecase`, first import the package into your Python environment: | ||
| #### `nap(data: pd.DataFrame, dvars: Union[List[str], str], pvar: str, decreasing: bool = False, phases: Tuple[str, str] = ("A", "B")) -> pd.Series` | ||
| ```python | ||
| from singlecase.data import Data | ||
| ``` | ||
| Calculate the Nonoverlap Pairs (NAP) between two phases in a single-case data frame. | ||
| Then, create a `Data` object with either a pandas DataFrame or a dictionary: | ||
| - `data`: A single-case data frame. | ||
| - `dvars`: One or more dependent variables to calculate NAP for. | ||
| - `pvar`: The name of the phase variable. | ||
| - `decreasing`: If you expect data to be lower in the second phase, set `decreasing=True`. Default is `decreasing=False`. | ||
| - `phases`: A tuple of two column names in the data frame indicating the two phases that should be compared. Default is `("A", "B")`. | ||
| ```python | ||
| df = pd.DataFrame({...}) # Or load your data from a CSV, database, etc. | ||
| data = Data(df) | ||
| ``` | ||
| Returns the calculated NAP values in a Pandas Series. | ||
| Now you're ready to perform single-case data analysis! | ||
| #### `pnd(data: pd.DataFrame, dvars: Union[List[str], str], pvar: str, decreasing: bool = False, phases: Tuple[str, str] = ("A", "B")) -> pd.Series` | ||
| ## Core Features | ||
| Calculate the Percent Non-overlapping Data (PND) between two phases in a single-case data frame. | ||
| ### Data Class | ||
| - `data`: A single-case data frame. | ||
| - `dvars`: One or more dependent variables to calculate PND for. | ||
| - `pvar`: The name of the phase variable. | ||
| - `decreasing`: If you expect data to be lower in the second phase, set `decreasing=True`. Default is `decreasing=False`. | ||
| - `phases`: A tuple of two column names in the data frame indicating the two phases that should be compared. Default is `("A", "B")`. | ||
| The `Data` class provides an object-oriented interface to represent your single-case data. It assumes any variable with datatype of float as a dependent variable. The dependent and phase variables can be accessed and modified using properties. For example: | ||
| Returns the calculated PND values in a Pandas Series. | ||
| ```python | ||
| data.pvar = 'column_name' # Set the phase variable | ||
| ``` | ||
| ### Permutation Test | ||
| ### Nonoverlap Pairs (NAP) | ||
| #### `permutation_test(data: pd.DataFrame, dvars: Union[List[str], str], pvar: str, statistic: Union[str, Callable] = 'mean', phases: Tuple[str, str] = ("A", "B"), num_rounds: int = 10000, seed: int = None) -> pd.Series` | ||
| The `nap` function computes the Nonoverlap Pairs between two phases in a single-case data frame. It returns a pandas Series containing the NAP values for each dependent variable in the data set. | ||
| Perform a permutation test between two phases in a single-case data frame. | ||
| ```python | ||
| from singlecase.effectsize import nap | ||
| nap_values = nap(data) | ||
| ``` | ||
| - `data`: A single-case data frame. | ||
| - `dvars`: One or more dependent variables to perform the permutation test on. | ||
| - `pvar`: The name of the phase variable. | ||
| - `statistic`: The statistic to be used in the permutation test (either 'mean', 'median', or a custom callable). Default is `'mean'`. | ||
| - `phases`: A tuple of two column names in the data frame indicating the two phases that should be compared. Default is `("A", "B")`. | ||
| - `num_rounds`: The number of iterations for the permutation test. Default is `10000`. | ||
| - `seed`: Random seed for reproducibility. Default is `None`. | ||
| ### Percent Non-overlapping Data (PND) | ||
| Returns the calculated p-values in a Pandas Series. | ||
| The `pnd` function computes the Percent Non-overlapping Data between two phases in a single-case data frame. It returns a pandas Series containing the PND values for each dependent variable in the data set. | ||
| ## Usage | ||
| ```python | ||
| from singlecase.effectsize import pnd | ||
| pnd_values = pnd(data) | ||
| ``` | ||
| Here's a basic example demonstrating how to use the functions in the Single Case Research package: | ||
| ### Permutation Test | ||
| The `permutation_test` function performs a permutation test between two phases in a single-case data frame. It returns a pandas Series containing the p-values for each dependent variable in the data set. | ||
| ```python | ||
| import pandas as pd | ||
| from singlecase.effectsize import nap, pnd | ||
| from singlecase.permtest import permutation_test | ||
| p_values = permutation_test(data) | ||
| ``` | ||
| # Load your single-case data into a Pandas DataFrame | ||
| ## Complete example | ||
| This complete example creates a new `singlecase.Data` from a Python dictionary. The dataset has two dependent variables `dvar1` and `dvar2`. The phase variable is called `phase` and consists of the two phases "A" and "B". Based on this data, the PND and NAP values are calculated and printed. | ||
| # Calculate NAP | ||
| nap_values = nap(data, dvars=['dependent_var1', 'dependent_var2'], pvar='phase_var') | ||
| ```python | ||
| import pandas as pd | ||
| from singlecase.data import Data | ||
| from singlecase.effectsize import pnd, nap | ||
| # Calculate PND | ||
| pnd_values = pnd(data, dvars='dependent_var1', pvar='phase_var') | ||
| # Create a sample data | ||
| data_dict = { | ||
| 'dvar1': [1.0, 2.5, 3.2, 4.6, 2.8, 5.6, 3.7, 4.2, 5.5, 6.2, 7.3, 8.5], | ||
| 'dvar2': [2.5, 3.2, 4.6, 5.1, 4.8, 5.2, 6.7, 5.6, 6.2, 7.8, 8.4, 7.2], | ||
| 'phase': ['A', 'A', 'A', 'A', 'A', 'A', 'B', 'B', 'B', 'B', 'B', 'B'] | ||
| } | ||
| # Perform permutation test | ||
| p_values = permutation_test(data, dvars='dependent_var1', pvar='phase_var') | ||
| df = pd.DataFrame(data_dict) | ||
| # Print the results | ||
| print("NAP Values:") | ||
| print(nap_values) | ||
| # Instantiate Data object | ||
| data = Data(df) | ||
| print("PND Values:") | ||
| print(pnd_values) | ||
| # Set phase variable | ||
| data.pvar = 'phase' | ||
| print("P-Values:") | ||
| print(p_values) | ||
| # Calculate PND | ||
| pnd_values = pnd(data) | ||
| print(f"PND values: \n{pnd_values}") | ||
| # Calculate NAP | ||
| nap_values = nap(data) | ||
| print(f"NAP values: \n{nap_values}") | ||
| ``` | ||
| ## License | ||
@@ -143,0 +162,0 @@ |
@@ -11,3 +11,4 @@ LICENSE | ||
| singlecase.egg-info/top_level.txt | ||
| singlecase/data/__init__.py | ||
| singlecase/effectsize/__init__.py | ||
| singlecase/permtest/__init__.py |
@@ -0,2 +1,7 @@ | ||
| from . import data | ||
| from . import effectsize | ||
| from . import permtest | ||
| from . import permtest | ||
| from .data import Data | ||
| __all__ = ['Data', 'data', 'effectsize', 'permtest'] |
@@ -1,11 +0,6 @@ | ||
| import numpy as np | ||
| from typing import List, Tuple, Union | ||
| import pandas as pd | ||
| def nap(data: pd.DataFrame, | ||
| dvars: Union[List[str], str], | ||
| pvar: str, | ||
| decreasing: bool = False, | ||
| phases: Tuple[str, str] = ("A", "B")) -> pd.Series: | ||
| from singlecase.data import Data | ||
| def nap(data: Data, decreasing: bool = False) -> pd.Series: | ||
| """ | ||
@@ -15,9 +10,5 @@ Calculate the Nonoverlap Pairs (NAP) between two phases in a single-case data frame. | ||
| Args: | ||
| data (pd.DataFrame): A single-case data frame. | ||
| dvar (str): One or more dependent variables to calculate NAP for. | ||
| pvar (str): The name of the phase variable. | ||
| data (singlecase.Data): A single-case data set. | ||
| decreasing (bool, optional): If you expect data to be lower in the second phase, | ||
| set decreasing=True. Default is decreasing=False. | ||
| phases (Tuple[str, int], optional): A tuple of two column names in the data frame | ||
| indicating the two phases that should be compared. Default is ("A", "B"). | ||
@@ -28,24 +19,15 @@ Returns: | ||
| if isinstance(dvars, str): | ||
| dvars = [dvars] | ||
| if pvar not in data.columns: | ||
| raise ValueError("pvar must be the name of a column in the data frame") | ||
| pvar = data.pvar | ||
| phases = data._df[pvar].unique() | ||
| if len(phases) != 2: | ||
| raise ValueError("phases must be a tuple of two phase names") | ||
| raise ValueError(f"Only two phases are supported. The following phases were found in the phase variable : {pvar}: {phases}") | ||
| if phases[0] not in data[pvar].values or phases[1] not in data[pvar].values: | ||
| raise ValueError("phases must be a tuple of two phase names that are present in the data frame") | ||
| nap_values = {} | ||
| for dvar in dvars: | ||
| if dvar not in data.columns: | ||
| raise ValueError("dvar must be the name of a column in the data frame") | ||
| df = data._df | ||
| for dvar in data.dvars: | ||
| phase1_data = data.loc[data[pvar] == phases[0], dvar].values | ||
| phase2_data = data.loc[data[pvar] == phases[1], dvar].values | ||
| phase1_data = df.loc[df[pvar] == phases[0], dvar].dropna().values | ||
| phase2_data = df.loc[df[pvar] == phases[1], dvar].dropna().values | ||
@@ -70,7 +52,3 @@ non_overlapping_pairs = 0 | ||
| def pnd(data: pd.DataFrame, | ||
| dvars: Union[List[str], str], | ||
| pvar: str, | ||
| decreasing: bool = False, | ||
| phases: Tuple[str, str] = ("A", "B")) -> pd.Series: | ||
| def pnd(data: Data, decreasing: bool = False) -> pd.Series: | ||
| """ | ||
@@ -80,10 +58,5 @@ Calculate the Percent Non-overlapping Data (PND) between two phases in a single-case data frame. | ||
| Args: | ||
| data (pd.DataFrame): A single-case data frame. | ||
| dvars (Union[List[str], str]): One or more dependent variables to calculate PND for. | ||
| pvar (str): The name of the phase variable. | ||
| data (singlecase.Data): A single-case data set. | ||
| decreasing (bool, optional): If you expect data to be lower in the second phase, | ||
| set decreasing=True. Default is decreasing=False. | ||
| phases (Tuple[str, str], optional): A tuple of two column names in the data frame | ||
| indicating the two phases that should be compared. Default is ("A", "B"). | ||
| Returns: | ||
@@ -93,21 +66,13 @@ pnd_values (pd.Series): The calculated PND values in a pd.Series. | ||
| if isinstance(dvars, str): | ||
| dvars = [dvars] | ||
| if pvar not in data.columns: | ||
| raise ValueError("pvar must be the name of a column in the data frame") | ||
| pvar = data.pvar | ||
| phases = data._df[pvar].unique() | ||
| if len(phases) != 2: | ||
| raise ValueError("phases must be a tuple of two phase names") | ||
| raise ValueError(f"Only two phases are supported. The following phases were found in the phase variable : {pvar}: {phases}") | ||
| if phases[0] not in data[pvar].values or phases[1] not in data[pvar].values: | ||
| raise ValueError("phases must be a tuple of two phase names that are present in the data frame") | ||
| pnd_values = {} | ||
| for dvar in dvars: | ||
| if dvar not in data.columns: | ||
| raise ValueError("dvar must be the name of a column in the data frame") | ||
| phase1_data = data.loc[data[pvar] == phases[0], dvar].values | ||
| phase2_data = data.loc[data[pvar] == phases[1], dvar].values | ||
| df = data._df | ||
| for dvar in data.dvars: | ||
| phase1_data = df.loc[df[pvar] == phases[0], dvar].dropna().values | ||
| phase2_data = df.loc[df[pvar] == phases[1], dvar].dropna().values | ||
@@ -114,0 +79,0 @@ extreme_value = max(phase1_data) if decreasing else min(phase1_data) |
@@ -5,7 +5,6 @@ import numpy as np | ||
| def permutation_test(data: pd.DataFrame, | ||
| dvars: Union[List[str], str], | ||
| pvar: str, | ||
| from singlecase.data import Data | ||
| def permutation_test(data: Data, | ||
| statistic: Union[str, Callable] = 'mean', | ||
| phases: Tuple[str, str] = ("A", "B"), | ||
| num_rounds: int = 10000, | ||
@@ -17,9 +16,5 @@ seed: int = None) -> pd.Series: | ||
| Args: | ||
| data (pd.DataFrame): A single-case data frame. | ||
| dvars (Union[List[str], str]): One or more dependent variables to perform the permutation test on. | ||
| pvar (str): The name of the phase variable. | ||
| data (singlecase.Data): A single-case data set. | ||
| statistic (Union[str, Callable], optional): The statistic to be used in the permutation test | ||
| (either 'mean', 'median', or a custom callable). Default is 'mean'. | ||
| phases (Tuple[str, str], optional): A tuple of two column names in the data frame | ||
| indicating the two phases that should be compared. Default is ("A", "B"). | ||
| num_rounds (int, optional): The number of iterations for the permutation test. Default is 10000. | ||
@@ -32,14 +27,7 @@ seed (int, optional): Random seed for reproducibility. Default is None. | ||
| if isinstance(dvars, str): | ||
| dvars = [dvars] | ||
| if pvar not in data.columns: | ||
| raise ValueError("pvar must be the name of a column in the data frame") | ||
| pvar = data.pvar | ||
| phases = data._df[pvar].unique() | ||
| if len(phases) != 2: | ||
| raise ValueError("phases must be a tuple of two phase names") | ||
| raise ValueError(f"Only two phases are supported. The following phases were found in the phase variable : {pvar}: {phases}") | ||
| if phases[0] not in data[pvar].values or phases[1] not in data[pvar].values: | ||
| raise ValueError("phases must be a tuple of two phase names that are present in the data frame") | ||
| if isinstance(statistic, str): | ||
@@ -57,12 +45,7 @@ if statistic == 'mean': | ||
| for dvar in dvars: | ||
| if dvar not in data.columns: | ||
| raise ValueError("dvar must be the name of a column in the data frame") | ||
| df = data._df | ||
| for dvar in data.dvars: | ||
| phase1_data = df.loc[df[pvar] == phases[0], dvar].dropna().values | ||
| phase2_data = df.loc[df[pvar] == phases[1], dvar].dropna().values | ||
| phase1_data = data.loc[data[pvar] == phases[0], dvar].values | ||
| phase2_data = data.loc[data[pvar] == phases[1], dvar].values | ||
| phase1_data = phase1_data[~np.isnan(phase1_data)] | ||
| phase2_data = phase2_data[~np.isnan(phase2_data)] | ||
| observed_diff = statistic(phase2_data) - statistic(phase1_data) | ||
@@ -85,1 +68,3 @@ | ||
| return pd.Series(p_values, name="p_value") | ||
Alert delta unavailable
Currently unable to show alert delta for PyPI packages.
27430
3.26%15
7.14%169
14.19%