Skip to content

Commit

Permalink
return fig from plotting functions
Browse files Browse the repository at this point in the history
  • Loading branch information
teanijarv committed Mar 7, 2024
1 parent a08171c commit 6aed7dc
Show file tree
Hide file tree
Showing 10 changed files with 78 additions and 47 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
/example
*__pycache__
*.DS_Store
*.ipynb
2 changes: 1 addition & 1 deletion AUTHORS.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ Development Lead
----------------

* Toomas Erik Anijärv <[email protected]>
* Rory Boyle <[email protected]>

Contributors
------------

* Jules Mitchell
* Rory Boyle
* Cate Scanlon
7 changes: 6 additions & 1 deletion HISTORY.rst
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,9 @@ History
0.2.2 (2024-03-3)
------------------

* Updated documentation
* Updated documentation

0.2.3 (2024-03-7)
------------------

* Added that the plotting functions return matplotlib figure object
2 changes: 1 addition & 1 deletion HLR/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

__author__ = """Toomas Erik Anijärv"""
__email__ = '[email protected]'
__version__ = '0.2.0'
__version__ = '0.2.3'

from .diagnostic_tests import (test_durbin_watson,
test_pearsons_r,
Expand Down
9 changes: 1 addition & 8 deletions HLR/diagnostic_tests.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,4 @@
"""Functions for diagnostic tests to test for assumptions.
Authors
-------
Toomas Erik Anijärv [email protected] github.com/teanijarv
Rory Boyle [email protected] github.com/rorytboyle
"""

"""Functions for diagnostic tests to test for assumptions."""
import scipy.stats
import numpy as np
from statsmodels.stats.stattools import durbin_watson
Expand Down
4 changes: 2 additions & 2 deletions HLR/plots.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
"""Functions for plotting."""
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
Expand Down Expand Up @@ -45,5 +46,4 @@ def create_subplot_partial_regression(model, fig_size, level):
sm.graphics.plot_partregress_grid(model, fig=fig)
fig.suptitle(f'Partial Regression Plots (Model Level {level})', y=1)

plt.tight_layout()
plt.show()
return fig
60 changes: 46 additions & 14 deletions HLR/regression.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,4 @@
"""Functions for running hierarchical linear regression.
Authors
-------
Toomas Erik Anijärv [email protected] github.com/teanijarv
Rory Boyle [email protected] github.com/rorytboyle
"""
"""HLR - Hierarchical Linear Regression."""
import pandas as pd
import statsmodels.api as sm
import scipy.stats
Expand Down Expand Up @@ -215,7 +209,11 @@ def diagnostics(self, verbose=True):
return diagnostics_results

def plot_studentized_residuals_vs_fitted(self):
"""Plots studentized residuals against fitted values for all model levels."""
"""Plots studentized residuals against fitted values for all model levels.
Returns:
matplotlib.figure.Figure: The matplotlib figure object.
"""
model_results = self.fit_models()
num_levels = len(model_results)

Expand All @@ -229,9 +227,15 @@ def plot_studentized_residuals_vs_fitted(self):

plt.tight_layout()
plt.show()

return fig

def plot_qq_residuals(self):
"""Plots Normal QQ Plots for all model levels."""
"""Plots Normal QQ Plots for all model levels.
Returns:
matplotlib.figure.Figure: The matplotlib figure object.
"""
model_results = self.fit_models()
fig, axs = plt.subplots(len(model_results), 1, figsize=(8, 4 * len(model_results)))
if len(model_results) == 1:
Expand All @@ -243,8 +247,14 @@ def plot_qq_residuals(self):
plt.tight_layout()
plt.show()

return fig

def plot_influence(self):
"""Plots Influence Plots for all model levels."""
"""Plots Influence Plots for all model levels.
Returns:
matplotlib.figure.Figure: The matplotlib figure object.
"""
model_results = self.fit_models()
fig, axs = plt.subplots(len(model_results), 1, figsize=(8, 4 * len(model_results)))
if len(model_results) == 1:
Expand All @@ -256,8 +266,14 @@ def plot_influence(self):
plt.tight_layout()
plt.show()

return fig

def plot_std_residuals(self):
"""Plots Box Plots of Standardized Residuals for all model levels."""
"""Plots Box Plots of Standardized Residuals for all model levels.
Returns:
matplotlib.figure.Figure: The matplotlib figure object.
"""
model_results = self.fit_models()
fig, axs = plt.subplots(len(model_results), 1, figsize=(8, 4 * len(model_results)))
if len(model_results) == 1:
Expand All @@ -270,9 +286,15 @@ def plot_std_residuals(self):

plt.tight_layout()
plt.show()

return fig

def plot_histogram_std_residuals(self):
"""Plots Histogram of Standardized Residuals for all model levels."""
"""Plots Histogram of Standardized Residuals for all model levels.
Returns:
matplotlib.figure.Figure: The matplotlib figure object.
"""
model_results = self.fit_models()
fig, axs = plt.subplots(len(model_results), 1, figsize=(8, 4 * len(model_results)))
if len(model_results) == 1:
Expand All @@ -286,11 +308,21 @@ def plot_histogram_std_residuals(self):
plt.tight_layout()
plt.show()

return fig

def plot_partial_regression(self):
"""Plots Partial Regression Plots for all model levels."""
"""Plots Partial Regression Plots for all model levels.
Returns:
list: A list of matplotlib.figure.Figure objects, one for each model level.
"""
model_results = self.fit_models()
num_ivs = max(len(ivs) for ivs in self.models.values())
fig_size = (15, min(10, 5 * num_ivs))

fig_list = []
for level, model in model_results.items():
plots.create_subplot_partial_regression(model, fig_size, level)
fig = plots.create_subplot_partial_regression(model, fig_size, level)
fig_list.append(fig)

return fig_list
23 changes: 11 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,12 @@ hreg.summary()
hreg.diagnostics(verbose=True)

# Different plots (see docs for more)
hreg.plot_studentized_residuals_vs_fitted()
hreg.plot_qq_residuals()
hreg.plot_influence()
hreg.plot_std_residuals()
hreg.plot_histogram_std_residuals()
hreg.plot_partial_regression()
fig1 = hreg.plot_studentized_residuals_vs_fitted()
fig2 = hreg.plot_qq_residuals()
fig3 = hreg.plot_influence()
fig4 = hreg.plot_std_residuals()
fig5 = hreg.plot_histogram_std_residuals()
fig_list = hreg.plot_partial_regression()
```
Output:
| | Model Level | Predictors | N (observations) | DF (residuals) | DF (model) | R-squared | F-value | P-value (F) | SSE | SSTO | MSE (model) | MSE (residuals) | MSE (total) | Beta coefs | P-values (beta coefs) | Failed assumptions (check!) | R-squared change | F-value change | P-value (F change) |
Expand Down Expand Up @@ -129,30 +129,29 @@ Please use Zenodo DOI for citing the package in your work.

#### Example

Anijärv, T. E., Mitchell, J. and Boyle, R. (2024) ‘teanijarv/HLR: v0.2.2’. Zenodo. https://doi.org/10.5281/zenodo.7683808
Anijärv, T. E., Mitchell, J. and Boyle, R. (2024) ‘teanijarv/HLR: v0.2.3’. Zenodo. https://doi.org/10.5281/zenodo.7683808
```
@software{toomas_erik_anijarv_2024_7683808,
author = {Toomas Erik Anijärv, Jules Mitchell, Rory Boyle},
title = {teanijarv/HLR: v0.2.2},
title = {teanijarv/HLR: v0.2.3},
month = mar,
year = 2024,
publisher = {Zenodo},
version = {v0.2.2},
version = {v0.2.3},
doi = {10.5281/zenodo.7683808},
url = {https://doi.org/10.5281/zenodo.7683808}
}
```

## Development
HLR was created by [Toomas Erik Anijärv](https://www.toomaserikanijarv.com) using original code by [Rory Boyle](https://github.com/rorytboyle). The package is maintained by Toomas during his spare time, thereby contributions are more than welcome!
The HLR package was created and is maintained by [Toomas Erik Anijärv](https://www.toomaserikanijarv.com). It is updated during spare time, thereby contributions are more than welcome!

This program is provided with no warranty of any kind and it is still under development. However, this code has been checked and validated against multiple same analyses conducted in SPSS.

#### 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.
- dict valus within df hard to read
- dict values within df hard to read
- add t stats for coefficients
- give option for output only some columns not all
- add regression type option (eg, for logistic regression)

#### Contributors
Expand Down
14 changes: 7 additions & 7 deletions docs/usage.rst
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ Plotting options for all model levels

.. code-block:: python
hlr.plot_studentized_residuals_vs_fitted()
fig = hlr.plot_studentized_residuals_vs_fitted()
Output:

Expand All @@ -113,7 +113,7 @@ Output:

.. code-block:: python
hlr.plot_qq_residuals()
fig = hlr.plot_qq_residuals()
Output:

Expand All @@ -124,7 +124,7 @@ Output:

.. code-block:: python
hlr.plot_influence()
fig = hlr.plot_influence()
Output:

Expand All @@ -135,7 +135,7 @@ Output:

.. code-block:: python
hlr.plot_std_residuals()
fig = hlr.plot_std_residuals()
Output:

Expand All @@ -146,7 +146,7 @@ Output:

.. code-block:: python
hlr.plot_histogram_std_residuals()
fig = hlr.plot_histogram_std_residuals()
Output:

Expand All @@ -157,7 +157,7 @@ Output:

.. code-block:: python
hlr.plot_partial_regression()
fig_list = hlr.plot_partial_regression()
Output:

Expand All @@ -166,4 +166,4 @@ Output:
:align: center
:width: 50%

(only Model Level 1 displayed, but actual output would plot all levels)
(the fig_list contains a fig for each Model Level; only Model Level 1 displayed (i.e., fig_list[0]))
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.2',
version='0.2.3',
zip_safe=False,
)

0 comments on commit 6aed7dc

Please sign in to comment.