Skip to content

Commit

Permalink
OLS additional parameters + docs test
Browse files Browse the repository at this point in the history
- added option to add custom OLS parameters to the HLR object
- testing docs
  • Loading branch information
teanijarv committed Mar 3, 2024
1 parent caf2657 commit 75d8e79
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 59 deletions.
45 changes: 1 addition & 44 deletions .github/workflows/publish-to-pypi.yml
Original file line number Diff line number Diff line change
Expand Up @@ -47,47 +47,4 @@ jobs:
name: python-package-distributions
path: dist/
- name: Publish distribution 📦 to PyPI
uses: pypa/gh-action-pypi-publish@release/v1

github-release:
name: >-
Sign the Python 🐍 distribution 📦 with Sigstore
and upload them to GitHub Release
needs:
- publish-to-pypi
runs-on: ubuntu-latest

permissions:
contents: write # IMPORTANT: mandatory for making GitHub Releases
id-token: write # IMPORTANT: mandatory for sigstore

steps:
- name: Download all the dists
uses: actions/download-artifact@v3
with:
name: python-package-distributions
path: dist/
- name: Sign the dists with Sigstore
uses: sigstore/[email protected]
with:
inputs: >-
./dist/*.tar.gz
./dist/*.whl
- name: Create GitHub Release
env:
GITHUB_TOKEN: ${{ github.token }}
run: >-
gh release create
'${{ github.ref_name }}'
--repo '${{ github.repository }}'
--notes ""
- name: Upload artifact signatures to GitHub Release
env:
GITHUB_TOKEN: ${{ github.token }}
# Upload to GitHub Release using the `gh` CLI.
# `dist/` contains the built packages, and the
# sigstore-produced signatures and certificates.
run: >-
gh release upload
'${{ github.ref_name }}' dist/**
--repo '${{ github.repository }}'
uses: pypa/gh-action-pypi-publish@release/v1
35 changes: 35 additions & 0 deletions .readthedocs.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# Read the Docs configuration file for Sphinx projects
# See https://docs.readthedocs.io/en/stable/config-file/v2.html for details

# Required
version: 2

# Set the OS, Python version and other tools you might need
build:
os: ubuntu-22.04
tools:
python: "3.12"
# You can also specify other tool versions:
# nodejs: "20"
# rust: "1.70"
# golang: "1.20"

# Build documentation in the "docs/" directory with Sphinx
sphinx:
configuration: docs/conf.py
# You can configure Sphinx to use a different builder, for instance use the dirhtml builder for simpler URLs
# builder: "dirhtml"
# Fail on all warnings to avoid broken references
# fail_on_warning: true

# Optionally build your docs in additional formats such as PDF and ePub
# formats:
# - pdf
# - epub

# Optional but recommended, declare the Python requirements required
# to build your documentation
# See https://docs.readthedocs.io/en/stable/guides/reproducible-builds.html
# python:
# install:
# - requirements: docs/requirements.txt
16 changes: 14 additions & 2 deletions HLR/regression.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,15 @@
class HierarchicalLinearRegression:
"""Class for performing hierarchical linear regression analysis."""

def __init__(self, df, ivs_dict, dv):
def __init__(self, df, ivs_dict, dv, missing_data=None, ols_params=None):
"""Initializes the HierarchicalLinearRegression class.
Args:
df (pd.DataFrame): The dataset to be used in the regression.
ivs_dict (dict): Dictionary mapping model levels to lists of independent variables.
dv (str): The dependent variable.
missing_data (str, optional): Handling of missing data in OLS
ols_params (dict, optional): Optional parameters to pass to the OLS fit method.
"""
if not isinstance(df, pd.DataFrame):
raise ValueError("Data is not a pandas DataFrame.")
Expand All @@ -42,6 +44,16 @@ def __init__(self, df, ivs_dict, dv):
raise ValueError(f"{dv} is not a column in the DataFrame.")
self.outcome_var = dv

if missing_data is not None:
if missing_data not in ['none', 'drop', 'raise']:
raise ValueError("missing_data must be either 'none', 'drop', or 'raise'.")
self.missing_data = missing_data if missing_data is not None else 'none'

if ols_params is not None:
if not isinstance(ols_params, dict):
raise ValueError("ols_params must be a dictionary.")
self.ols_params = ols_params if ols_params is not None else {}

def fit_models(self):
"""Fits OLS models for each level of independent variables.
Expand All @@ -53,7 +65,7 @@ def fit_models(self):
X = self.data[predictors]
X_const = sm.add_constant(X)
y = self.data[self.outcome_var]
model = sm.OLS(y, X_const).fit()
model = sm.OLS(y, X_const, missing=self.missing_data).fit(**self.ols_params)
results[level] = model
return results

Expand Down
22 changes: 10 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,11 @@ X = {1: ['PTS'],
# Define the outcome variable
y = 'W'

# Initiate the HLR object
hreg = HierarchicalLinearRegression(df, X, y)
# Initiate the HLR object (missing_data and ols_params are optional parameters)
hreg = HierarchicalLinearRegression(df, X, y, missing_data='drop', ols_params=None)

# Generate a summarised report as a dataframe which shows all linear regression models parameters and difference between the models
summary_report = hreg.summary()
display(summary_report)
# Generate a summarised report of HLR
hreg.summary()

# Run diagnostics on all the models (displayed output below only shows the first model)
hreg.diagnostics(verbose=True)
Expand Down Expand Up @@ -119,7 +118,8 @@ Model Level 2 Diagnostics:
:-------------------------:|:-------------------------:

#### Documentation (WIP)
Docs is currently outdated - it currently displays the old version of the package. See the Usage above for all available functionality.
Documentation is currently work in progress.

<https://hlr-hierarchical-linear-regression.readthedocs.io>

## Citation
Expand All @@ -129,16 +129,15 @@ Please use Zenodo DOI for citing the package in your work.

#### Example

Toomas Erik Anijärv, & Rory Boyle. (2023). teanijarv/HLR: v0.2.0 (v0.2.0). Zenodo. https://doi.org/10.5281/zenodo.7683808
Anijärv, T. E., Mitchell, J. and Boyle, R. (2024) ‘teanijarv/HLR: v0.2.1’. Zenodo. https://doi.org/10.5281/zenodo.7683808
```
@software{toomas_erik_anijarv_2024_7683808,
author = {Toomas Erik Anijärv and
Rory Boyle},
title = {teanijarv/HLR: v0.2.0},
author = {Toomas Erik Anijärv, Jules Mitchell, Rory Boyle},
title = {teanijarv/HLR: v0.2.1},
month = mar,
year = 2024,
publisher = {Zenodo},
version = {v0.2.0},
version = {v0.2.1},
doi = {10.5281/zenodo.7683808},
url = {https://doi.org/10.5281/zenodo.7683808}
}
Expand All @@ -152,7 +151,6 @@ This program is provided with no warranty of any kind and it is still under deve
#### To-do
Would be great if someone with more experience with packages would contribute with testing and the whole deployment process. Also, if someone would want to write documentation, that would be amazing.
- docs
- testing
- dict valus within df hard to read
- ability to change OLS parameters
- add t stats for coefficients
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,6 @@
test_suite='tests',
tests_require=test_requirements,
url='https://github.com/teanijarv/HLR',
version='0.2.0',
version='0.2.1',
zip_safe=False,
)

0 comments on commit 75d8e79

Please sign in to comment.