apatools 0.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.
- apatools-0.1.0/.gitignore +140 -0
- apatools-0.1.0/CHANGELOG.md +23 -0
- apatools-0.1.0/LICENSE +21 -0
- apatools-0.1.0/PKG-INFO +327 -0
- apatools-0.1.0/README.md +250 -0
- apatools-0.1.0/apatools/__init__.py +15 -0
- apatools-0.1.0/apatools/_deps.py +25 -0
- apatools-0.1.0/apatools/calc.py +366 -0
- apatools-0.1.0/apatools/citation.py +5 -0
- apatools-0.1.0/apatools/format.py +88 -0
- apatools-0.1.0/apatools/lib.py +415 -0
- apatools-0.1.0/apatools/lm.py +589 -0
- apatools-0.1.0/apatools/plm.py +1245 -0
- apatools-0.1.0/apatools/psm.py +135 -0
- apatools-0.1.0/apatools/sem.py +355 -0
- apatools-0.1.0/apatools/slm.py +490 -0
- apatools-0.1.0/apatools/utils.py +106 -0
- apatools-0.1.0/pyproject.toml +114 -0
- apatools-0.1.0/tests/__init__.py +0 -0
- apatools-0.1.0/tests/conftest.py +27 -0
- apatools-0.1.0/tests/test_calc.py +404 -0
- apatools-0.1.0/tests/test_format.py +110 -0
- apatools-0.1.0/tests/test_imports.py +175 -0
- apatools-0.1.0/tests/test_lib.py +365 -0
- apatools-0.1.0/tests/test_lm.py +203 -0
- apatools-0.1.0/tests/test_plm.py +583 -0
- apatools-0.1.0/tests/test_psm.py +103 -0
- apatools-0.1.0/tests/test_sem.py +137 -0
- apatools-0.1.0/tests/test_slm.py +328 -0
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
# Byte-compiled / optimized / DLL files
|
|
2
|
+
# manually
|
|
3
|
+
*.json
|
|
4
|
+
*.lock
|
|
5
|
+
*.png
|
|
6
|
+
*.pdf
|
|
7
|
+
*.csv
|
|
8
|
+
|
|
9
|
+
__pycache__/
|
|
10
|
+
.venv
|
|
11
|
+
.conda
|
|
12
|
+
.vscode
|
|
13
|
+
.ruff_cache
|
|
14
|
+
*.py[cod]
|
|
15
|
+
*$py.class
|
|
16
|
+
|
|
17
|
+
# C extensions
|
|
18
|
+
*.so
|
|
19
|
+
|
|
20
|
+
# Distribution / packaging
|
|
21
|
+
.Python
|
|
22
|
+
build/
|
|
23
|
+
develop-eggs/
|
|
24
|
+
dist/
|
|
25
|
+
downloads/
|
|
26
|
+
eggs/
|
|
27
|
+
.eggs/
|
|
28
|
+
lib/
|
|
29
|
+
lib64/
|
|
30
|
+
parts/
|
|
31
|
+
sdist/
|
|
32
|
+
var/
|
|
33
|
+
wheels/
|
|
34
|
+
pip-wheel-metadata/
|
|
35
|
+
share/python-wheels/
|
|
36
|
+
*.egg-info/
|
|
37
|
+
.installed.cfg
|
|
38
|
+
*.egg
|
|
39
|
+
MANIFEST
|
|
40
|
+
|
|
41
|
+
# PyInstaller
|
|
42
|
+
# Usually these files are written by a python script from a template
|
|
43
|
+
# before PyInstaller builds the exe, so as to inject date/other infos into it.
|
|
44
|
+
*.manifest
|
|
45
|
+
*.spec
|
|
46
|
+
|
|
47
|
+
# Installer logs
|
|
48
|
+
pip-log.txt
|
|
49
|
+
pip-delete-this-directory.txt
|
|
50
|
+
|
|
51
|
+
# Unit test / coverage reports
|
|
52
|
+
htmlcov/
|
|
53
|
+
.tox/
|
|
54
|
+
.nox/
|
|
55
|
+
.coverage
|
|
56
|
+
.coverage.*
|
|
57
|
+
.cache
|
|
58
|
+
nosetests.xml
|
|
59
|
+
coverage.xml
|
|
60
|
+
*.cover
|
|
61
|
+
*.py,cover
|
|
62
|
+
.hypothesis/
|
|
63
|
+
.pytest_cache/
|
|
64
|
+
|
|
65
|
+
# Translations
|
|
66
|
+
*.mo
|
|
67
|
+
*.pot
|
|
68
|
+
|
|
69
|
+
# Django stuff:
|
|
70
|
+
*.log
|
|
71
|
+
local_settings.py
|
|
72
|
+
db.sqlite3
|
|
73
|
+
db.sqlite3-journal
|
|
74
|
+
|
|
75
|
+
# Flask stuff:
|
|
76
|
+
instance/
|
|
77
|
+
.webassets-cache
|
|
78
|
+
|
|
79
|
+
# Scrapy stuff:
|
|
80
|
+
.scrapy
|
|
81
|
+
|
|
82
|
+
# Sphinx documentation
|
|
83
|
+
docs/_build/
|
|
84
|
+
|
|
85
|
+
# PyBuilder
|
|
86
|
+
target/
|
|
87
|
+
|
|
88
|
+
# Jupyter Notebook
|
|
89
|
+
.ipynb_checkpoints
|
|
90
|
+
|
|
91
|
+
# IPython
|
|
92
|
+
profile_default/
|
|
93
|
+
ipython_config.py
|
|
94
|
+
|
|
95
|
+
# pyenv
|
|
96
|
+
.python-version
|
|
97
|
+
|
|
98
|
+
# pipenv
|
|
99
|
+
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
|
|
100
|
+
# However, in case of collaboration, if having platform-specific dependencies or dependencies
|
|
101
|
+
# having no cross-platform support, pipenv may install dependencies that don't work, or not
|
|
102
|
+
# install all needed dependencies.
|
|
103
|
+
#Pipfile.lock
|
|
104
|
+
|
|
105
|
+
# PEP 582; used by e.g. github.com/David-OConnor/pyflow
|
|
106
|
+
__pypackages__/
|
|
107
|
+
|
|
108
|
+
# Celery stuff
|
|
109
|
+
celerybeat-schedule
|
|
110
|
+
celerybeat.pid
|
|
111
|
+
|
|
112
|
+
# SageMath parsed files
|
|
113
|
+
*.sage.py
|
|
114
|
+
|
|
115
|
+
# Environments
|
|
116
|
+
.env
|
|
117
|
+
.venv
|
|
118
|
+
env/
|
|
119
|
+
venv/
|
|
120
|
+
ENV/
|
|
121
|
+
env.bak/
|
|
122
|
+
venv.bak/
|
|
123
|
+
|
|
124
|
+
# Spyder project settings
|
|
125
|
+
.spyderproject
|
|
126
|
+
.spyproject
|
|
127
|
+
|
|
128
|
+
# Rope project settings
|
|
129
|
+
.ropeproject
|
|
130
|
+
|
|
131
|
+
# mkdocs documentation
|
|
132
|
+
/site
|
|
133
|
+
|
|
134
|
+
# mypy
|
|
135
|
+
.mypy_cache/
|
|
136
|
+
.dmypy.json
|
|
137
|
+
dmypy.json
|
|
138
|
+
|
|
139
|
+
# Pyre type checker
|
|
140
|
+
.pyre/
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
## 0.1.0
|
|
4
|
+
|
|
5
|
+
First release on PyPI.
|
|
6
|
+
|
|
7
|
+
The base installation (`pip install apatools`) contains `apatools.calc` and `apatools.format` and needs
|
|
8
|
+
only numpy and scipy. The other modules are installed through extras named after the module
|
|
9
|
+
(`apatools[lm]`, `[slm]`, `[plm]`, `[sem]`, `[psm]`, `[lib]`; `[linearmodels]`, `[graphviz]`,
|
|
10
|
+
`[krippendorff]`; `[all]`). Importing a module without its extra raises an `ImportError` that names the extra.
|
|
11
|
+
|
|
12
|
+
- `apatools.calc`: conversions between r, d, t, z, F, p and odds ratios, confidence intervals, `bootstrap`.
|
|
13
|
+
- `apatools.format`: APA formatting of p-values, correlations and significance stars.
|
|
14
|
+
- `apatools.lm`: OLS, RLM, GLM, QuantReg and MixedLM with *statsmodels*, APA tables, leave-one-out metrics, VIF.
|
|
15
|
+
- `apatools.slm`: OLS, spatial lag and spatial error models with *spreg*, APA tables, leave-one-out
|
|
16
|
+
prediction metrics (also leaving out the neighbours of the held-out observation).
|
|
17
|
+
- `apatools.plm`: panel regressions (pooled, fixed and random effects, optionally with a spatial lag or
|
|
18
|
+
spatial error) with *spreg* or, optionally, *linearmodels*; APA tables, leave-one-group-out prediction
|
|
19
|
+
metrics and specification tests (Hausman, Mundlak, F test of entity effects, Baltagi-Song-Koh and
|
|
20
|
+
Anselin LM tests).
|
|
21
|
+
- `apatools.sem`: structural equation models with *semopy*, reporting and path diagrams.
|
|
22
|
+
- `apatools.psm`: propensity score matching with *psmpy* and effect sizes.
|
|
23
|
+
- `apatools.lib`: inter-rater agreement, pooling and comparing correlations, HTMT ratio, Gini coefficient.
|
apatools-0.1.0/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2023 Danila Valko
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
apatools-0.1.0/PKG-INFO
ADDED
|
@@ -0,0 +1,327 @@
|
|
|
1
|
+
Metadata-Version: 2.5
|
|
2
|
+
Name: apatools
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: A toolbox for meta-analysis and APA-compliant reporting of regression, spatial, panel, SEM and propensity score matching results.
|
|
5
|
+
Project-URL: Homepage, https://github.com/Ellariel/apatools
|
|
6
|
+
Project-URL: Repository, https://github.com/Ellariel/apatools
|
|
7
|
+
Project-URL: Issues, https://github.com/Ellariel/apatools/issues
|
|
8
|
+
Project-URL: Changelog, https://github.com/Ellariel/apatools/blob/main/CHANGELOG.md
|
|
9
|
+
Author-email: Danila Valko <danila.valko@proton.me>
|
|
10
|
+
License-Expression: MIT
|
|
11
|
+
License-File: LICENSE
|
|
12
|
+
Keywords: MLM,RLM,SEM,apa,effect size,meta-analysis,panel data,propensity score matching,regression,reporting,spatial regression,statsmodels
|
|
13
|
+
Classifier: Development Status :: 4 - Beta
|
|
14
|
+
Classifier: Intended Audience :: Science/Research
|
|
15
|
+
Classifier: Operating System :: OS Independent
|
|
16
|
+
Classifier: Programming Language :: Python :: 3
|
|
17
|
+
Classifier: Programming Language :: Python :: 3 :: Only
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
19
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
20
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
21
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
22
|
+
Classifier: Topic :: Scientific/Engineering
|
|
23
|
+
Classifier: Topic :: Scientific/Engineering :: Information Analysis
|
|
24
|
+
Requires-Python: >=3.10
|
|
25
|
+
Requires-Dist: numpy>=2.0.2
|
|
26
|
+
Requires-Dist: scipy>=1.13.1
|
|
27
|
+
Provides-Extra: all
|
|
28
|
+
Requires-Dist: graphviz>=0.20.3; extra == 'all'
|
|
29
|
+
Requires-Dist: krippendorff; extra == 'all'
|
|
30
|
+
Requires-Dist: libpysal; extra == 'all'
|
|
31
|
+
Requires-Dist: linearmodels>=6.0; extra == 'all'
|
|
32
|
+
Requires-Dist: lxml>=5.3.0; extra == 'all'
|
|
33
|
+
Requires-Dist: pandas>=2.2.3; extra == 'all'
|
|
34
|
+
Requires-Dist: patsy; extra == 'all'
|
|
35
|
+
Requires-Dist: psmpy>=0.3.13; extra == 'all'
|
|
36
|
+
Requires-Dist: semopy>=2.3.11; extra == 'all'
|
|
37
|
+
Requires-Dist: spreg>=1.9.0; extra == 'all'
|
|
38
|
+
Requires-Dist: statsmodels>=0.14.4; extra == 'all'
|
|
39
|
+
Provides-Extra: graphviz
|
|
40
|
+
Requires-Dist: graphviz>=0.20.3; extra == 'graphviz'
|
|
41
|
+
Requires-Dist: pandas>=2.2.3; extra == 'graphviz'
|
|
42
|
+
Requires-Dist: semopy>=2.3.11; extra == 'graphviz'
|
|
43
|
+
Provides-Extra: krippendorff
|
|
44
|
+
Requires-Dist: krippendorff; extra == 'krippendorff'
|
|
45
|
+
Requires-Dist: pandas>=2.2.3; extra == 'krippendorff'
|
|
46
|
+
Requires-Dist: statsmodels>=0.14.4; extra == 'krippendorff'
|
|
47
|
+
Provides-Extra: lib
|
|
48
|
+
Requires-Dist: pandas>=2.2.3; extra == 'lib'
|
|
49
|
+
Requires-Dist: statsmodels>=0.14.4; extra == 'lib'
|
|
50
|
+
Provides-Extra: linearmodels
|
|
51
|
+
Requires-Dist: libpysal; extra == 'linearmodels'
|
|
52
|
+
Requires-Dist: linearmodels>=6.0; extra == 'linearmodels'
|
|
53
|
+
Requires-Dist: pandas>=2.2.3; extra == 'linearmodels'
|
|
54
|
+
Requires-Dist: patsy; extra == 'linearmodels'
|
|
55
|
+
Requires-Dist: spreg>=1.9.0; extra == 'linearmodels'
|
|
56
|
+
Provides-Extra: lm
|
|
57
|
+
Requires-Dist: lxml>=5.3.0; extra == 'lm'
|
|
58
|
+
Requires-Dist: pandas>=2.2.3; extra == 'lm'
|
|
59
|
+
Requires-Dist: statsmodels>=0.14.4; extra == 'lm'
|
|
60
|
+
Provides-Extra: plm
|
|
61
|
+
Requires-Dist: libpysal; extra == 'plm'
|
|
62
|
+
Requires-Dist: pandas>=2.2.3; extra == 'plm'
|
|
63
|
+
Requires-Dist: patsy; extra == 'plm'
|
|
64
|
+
Requires-Dist: spreg>=1.9.0; extra == 'plm'
|
|
65
|
+
Provides-Extra: psm
|
|
66
|
+
Requires-Dist: pandas>=2.2.3; extra == 'psm'
|
|
67
|
+
Requires-Dist: psmpy>=0.3.13; extra == 'psm'
|
|
68
|
+
Provides-Extra: sem
|
|
69
|
+
Requires-Dist: pandas>=2.2.3; extra == 'sem'
|
|
70
|
+
Requires-Dist: semopy>=2.3.11; extra == 'sem'
|
|
71
|
+
Provides-Extra: slm
|
|
72
|
+
Requires-Dist: libpysal; extra == 'slm'
|
|
73
|
+
Requires-Dist: pandas>=2.2.3; extra == 'slm'
|
|
74
|
+
Requires-Dist: spreg>=1.9.0; extra == 'slm'
|
|
75
|
+
Requires-Dist: statsmodels>=0.14.4; extra == 'slm'
|
|
76
|
+
Description-Content-Type: text/markdown
|
|
77
|
+
|
|
78
|
+
# A Toolbox for Meta-Analysis and APA-Compliant Reporting
|
|
79
|
+
|
|
80
|
+
## About
|
|
81
|
+
This toolbox is a curated collection of utilities I frequently use for statistical analysis and nicely reporting research findings in accordance with APA standards. While some of the included libraries may be slightly outdated, they remain functional and reliable, making them my go-to tools.
|
|
82
|
+
|
|
83
|
+
## Install
|
|
84
|
+
```shell
|
|
85
|
+
pip install apatools
|
|
86
|
+
```
|
|
87
|
+
Requires Python 3.10 or newer. The latest development version is available with `pip install git+https://github.com/ellariel/apatools.git`.
|
|
88
|
+
|
|
89
|
+
The base installation is light: it contains `apatools.calc` and `apatools.format` and needs only *numpy* and *scipy*. Every other module has its own heavier dependencies and is installed through an extra of the same name:
|
|
90
|
+
|
|
91
|
+
| install | module | brings in |
|
|
92
|
+
|---|---|---|
|
|
93
|
+
| `pip install apatools` | `calc`, `format` | numpy, scipy |
|
|
94
|
+
| `pip install "apatools[lm]"` | `lm` | pandas, statsmodels, lxml |
|
|
95
|
+
| `pip install "apatools[slm]"` | `slm` | pandas, statsmodels, spreg, libpysal |
|
|
96
|
+
| `pip install "apatools[plm]"` | `plm` | pandas, spreg, libpysal, patsy |
|
|
97
|
+
| `pip install "apatools[sem]"` | `sem` | pandas, semopy |
|
|
98
|
+
| `pip install "apatools[psm]"` | `psm` | pandas, psmpy |
|
|
99
|
+
| `pip install "apatools[lib]"` | `lib` | pandas, statsmodels |
|
|
100
|
+
| `pip install "apatools[linearmodels]"` | `plm` with the *linearmodels* backend | the `plm` extra and linearmodels |
|
|
101
|
+
| `pip install "apatools[graphviz]"` | `sem_plot` path diagrams | the `sem` extra and the Python package *graphviz*; the [Graphviz executables](https://graphviz.org/download/) must also be installed and on the `PATH` |
|
|
102
|
+
| `pip install "apatools[krippendorff]"` | `lib.krippendorffs_alpha` | the `lib` extra and *krippendorff* (GPL-3.0-or-later) |
|
|
103
|
+
| `pip install "apatools[all]"` | everything | all of the above |
|
|
104
|
+
|
|
105
|
+
Extras can be combined, e.g. `pip install "apatools[lm,plm]"`. Importing a module without its extra gives an `ImportError` that names the extra to install.
|
|
106
|
+
|
|
107
|
+
## Content and examples
|
|
108
|
+
* ***apatools.calc*** - *a basic module for statistical conversions and bootstrapping*
|
|
109
|
+
* ***apatools.format*** - *simple APA-compliant formatting functions for numerical results*
|
|
110
|
+
* ***apatools.lm*** (extra `lm`) - *a module offering wrapper functions for statsmodels, streamlining the fitting of OLS, RLM, and GLM models, as well as generating detailed tables*
|
|
111
|
+
* ***apatools.slm*** (extra `slm`) - *a module offering wrapper functions for spreg, streamlining the fitting of OLS, spatial lag (SLM) and spatial error (SEM) models, as well as generating detailed tables*
|
|
112
|
+
* ***apatools.plm*** (extra `plm`) - *a module offering wrapper functions for panel regressions (pooled, fixed and random effects, with optional spatial lag/error terms) with spreg or, optionally, linearmodels, as well as generating detailed tables and specification tests*
|
|
113
|
+
* ***apatools.sem*** (extra `sem`) - *a module for performing structural equation modeling, complete with result reporting and path diagram visualization*
|
|
114
|
+
* ***apatools.psm*** (extra `psm`) - *a module for performing propensity score matching, complete with effect sizes reporting*
|
|
115
|
+
* ***apatools.lib*** (extra `lib`) - *assorted helpers: inter-rater agreement (Fleiss' kappa, Krippendorff's alpha), pooling and comparing correlations, HTMT ratio, Gini coefficient*
|
|
116
|
+
|
|
117
|
+
### *apatools.calc*
|
|
118
|
+
|
|
119
|
+
This module provides essential conversion functions alongside tools for bootstrapping means, confidence intervals, and related statistics. It includes a comprehensive set of approximate conversion functions for bidirectional transformations between t-, z-, and F-statistics, as well as conversions involving Cohen's r, Cohen's d, and p-values.
|
|
120
|
+
|
|
121
|
+
#### Conversion examples
|
|
122
|
+
```python
|
|
123
|
+
from apatools.calc import cohen_r_from_z, cohen_d_from_z
|
|
124
|
+
# calculation Cohen's r and d from z-score:
|
|
125
|
+
# there is also a shorten aliases r_from_z and d_from_z
|
|
126
|
+
z = -1.472
|
|
127
|
+
r = cohen_r_from_z(z) # r = -0.9
|
|
128
|
+
d = cohen_d_from_z(z) # d = -4.1
|
|
129
|
+
```
|
|
130
|
+
|
|
131
|
+
```python
|
|
132
|
+
from apatools.calc import p_from_r
|
|
133
|
+
# calculation p-value for Cohen's r or correlation coefficient:
|
|
134
|
+
r = -0.79
|
|
135
|
+
n = 100
|
|
136
|
+
p = p_from_r(r, n) # p = 0.0
|
|
137
|
+
```
|
|
138
|
+
|
|
139
|
+
#### Bootstrap examples
|
|
140
|
+
```python
|
|
141
|
+
import numpy as np
|
|
142
|
+
from apatools.calc import bootstrap
|
|
143
|
+
# bootstraping confidence intervals for a mean value:
|
|
144
|
+
cil, m, cir = bootstrap([1, 2, 3, 4, 5, 6, 7, 8, 9],
|
|
145
|
+
func=np.mean,
|
|
146
|
+
n_rep=1000) # cil = 3.3, m = 5.0, cir = 6.7
|
|
147
|
+
```
|
|
148
|
+
|
|
149
|
+
```python
|
|
150
|
+
from apatools.calc import bootstrap
|
|
151
|
+
from scipy.stats import spearmanr
|
|
152
|
+
# bootstraping value for Spearman's correlation:
|
|
153
|
+
def func(*args):
|
|
154
|
+
return spearmanr(*args).statistic
|
|
155
|
+
|
|
156
|
+
_, m, _ = bootstrap(
|
|
157
|
+
[1, 2, 3, 4, 5, 6, 7, 8, 9],
|
|
158
|
+
[4, 5, 8, 4, 5, 6, 7, 8, 9],
|
|
159
|
+
func=func,
|
|
160
|
+
n_rep=1000) # m = 0.75
|
|
161
|
+
```
|
|
162
|
+
|
|
163
|
+
|
|
164
|
+
### *apatools.lm*
|
|
165
|
+
Fitting of OLS, RLM, GLM, QuantReg and MixedLM models using *statsmodels*. Arguments for the model constructor and for `.fit()` are passed as `<model>_<stage>_<argument>`: the model prefix (`ols_`, `rlm_`, `glm_`, `qlm_`, `mlm_`) followed by `model_` (constructor) or `fit_` (the `.fit()` call), e.g. `ols_fit_cov_type`, `glm_model_family`, `rlm_model_M`.
|
|
166
|
+
|
|
167
|
+
Specific parameters:
|
|
168
|
+
* `qlm_fit_q` – adds a quantile for QuantReg, e.g. 0.5
|
|
169
|
+
* `mlm_model_groups` – adds a grouping variable for MixedLM, e.g. `country`
|
|
170
|
+
* `vif` - adds a VIF column to the metrics dictionary and reported table
|
|
171
|
+
* `formula` - a formula string as in *statsmodels*
|
|
172
|
+
* `pred_r_sq` - calculates [predicted R²](https://blog.minitab.com/en/adventures-in-statistics-2/multiple-regession-analysis-use-adjusted-r-squared-and-predicted-r-squared-to-include-the-correct-number-of-variables#:~:text=What%20Is%20the%20Predicted%20R,valid%20predictions%20for%20new%20observations.)
|
|
173
|
+
|
|
174
|
+
```python
|
|
175
|
+
from apatools.lm import lm, lm_report
|
|
176
|
+
results, metrics = lm(
|
|
177
|
+
data,
|
|
178
|
+
y, x,
|
|
179
|
+
model=["ols", "rlm", "glm"],
|
|
180
|
+
verbose=True,
|
|
181
|
+
vif=True,
|
|
182
|
+
pred_r_sq=True,
|
|
183
|
+
ols_fit_cov_type="HC1",
|
|
184
|
+
rlm_model_M=sm.robust.norms.RamsayE(),
|
|
185
|
+
glm_model_family=sm.families.Gaussian(),
|
|
186
|
+
)
|
|
187
|
+
results_rep = lm_report(results, metrics)
|
|
188
|
+
print(results_rep, metrics)
|
|
189
|
+
```
|
|
190
|
+
|
|
191
|
+
|
|
192
|
+
### *apatools.slm*
|
|
193
|
+
Fitting of OLS, SLM, and SEM spatial models using *spreg*. Arguments for the spreg model constructor are passed as `<model>_model_<argument>` with the model prefix `ols_`, `slm_` or `sem_`, e.g. `ols_model_spat_diag`.
|
|
194
|
+
|
|
195
|
+
Specific parameters:
|
|
196
|
+
* `w` - a spatial matrix, created with *libpysal*; its rows must correspond to the rows of `data` (rows dropped because of NAs are removed from `w` automatically)
|
|
197
|
+
* `formula` - a formula string as in *statsmodels*
|
|
198
|
+
* `pred_metrics` - adds leave-one-out prediction metrics (`R²pred`, `MAEpred`, `MADpred`) to the metrics dictionary and the reported table. Every observation is held out in turn, the model and the weights are refitted without it, and the held-out value is predicted: `x·β` for OLS and SEM, and `ρ·(Wy)ᵢ + x·β` for SLM (observed neighbour values enter through the weights, so SLM is not strictly comparable with the other two). It needs one fit per observation and model; for large samples pass e.g. `slm_model_method="LU"`.
|
|
199
|
+
* `pred_exclude_neighbors` - with `pred_metrics`: also remove the neighbours of the held-out observation from the training sample
|
|
200
|
+
|
|
201
|
+
> **Caveat:** plain leave-one-out is *optimistic* for spatial data. The neighbours of the held-out observation stay in the training sample and are correlated with it, so `R²pred` overestimates how well the model predicts a new, unseen region. `pred_exclude_neighbors=True` gives a more honest (slightly pessimistic) estimate; it may create islands in the training weights, which is reported with a warning.
|
|
202
|
+
|
|
203
|
+
|
|
204
|
+
```python
|
|
205
|
+
from apatools.slm import slm, slm_report
|
|
206
|
+
results, metrics = slm(
|
|
207
|
+
data,
|
|
208
|
+
y, x, w,
|
|
209
|
+
model=["ols", "slm", "sem"],
|
|
210
|
+
verbose=True,
|
|
211
|
+
ols_model_spat_diag=True,
|
|
212
|
+
ols_model_nonspat_diag=True,
|
|
213
|
+
pred_metrics=True, # leave-one-out R²pred etc. (optimistic, see the caveat)
|
|
214
|
+
pred_exclude_neighbors=True, # leave the neighbourhood out as well
|
|
215
|
+
)
|
|
216
|
+
results_rep = slm_report(results, metrics)
|
|
217
|
+
print(results_rep, metrics)
|
|
218
|
+
```
|
|
219
|
+
|
|
220
|
+
|
|
221
|
+
### *apatools.plm*
|
|
222
|
+
Panel regressions (pooled, fixed effects, random effects, optionally with a spatial lag or spatial error) in the same format as `lm` and `slm`. Two backends: *spreg* (default; all models) and the optional *linearmodels* (`pip install "apatools[linearmodels]"`; non-spatial models, adds e.g. clustered standard errors and two-way fixed effects).
|
|
223
|
+
|
|
224
|
+
Data are an ordinary *long* data frame with an entity and a time column (or an `(entity, time)` index). The module checks that the panel is balanced, aligns the entities with the rows of the spatial weights `w` (the entity values must be the ids `w.id_order`, otherwise pass `w_ids`) and brings the rows into the layout spreg needs, so the row order of your data does not matter.
|
|
225
|
+
|
|
226
|
+
Models (`model=`): `pooled`, `fe`, `re` (both backends), `slm_fe`, `slm_re`, `sem_pooled`, `sem_fe`, `sem_re`, `sem_pooled_gm`, `sem_re_gm` (spreg: spatial lag / spatial error with ML or GMM estimation). `sem_re` (ML) failed with older numpy/scipy versions (numpy 2.0-2.2, scipy 1.13-1.15) in *spreg* 1.9.0, but worked with recent ones; `sem_re_gm` worked everywhere.
|
|
227
|
+
|
|
228
|
+
Specific parameters:
|
|
229
|
+
* `entity`, `time` - columns (or index levels) identifying entity and period; `balance="drop"` drops entities that are not observed in all periods instead of raising an error
|
|
230
|
+
* `backend` - `"spreg"` (default) or `"linearmodels"`
|
|
231
|
+
* `slx_lags` - number of spatial lags of the predictors (spreg; `slx_lags=1` gives a Durbin-type model)
|
|
232
|
+
* `<model>_model_<arg>` / `<model>_fit_<arg>` - arguments for the model class / `.fit()`, e.g. `slm_fe_model_method="LU"`, or for linearmodels `fe_fit_cov_type="clustered"`, `fe_fit_cluster_entity=True`, `fe_model_time_effects=True`
|
|
233
|
+
* `pred_metrics` - adds leave-one-*group*-out prediction metrics (`R²pred`, `MAEpred`, `MADpred`). Leaving out single observations is not meaningful in panels, so whole entities (`pred_group="entity"`, predicts a new entity; not possible for fixed-effects models) or whole periods (`pred_group="time"`) are held out, the model and the weights are refitted, and the held-out rows are predicted (`x·β`, plus `ρ·Wy` for spatial lag and the estimated entity effect for fixed effects). With spatial data the neighbours of a held-out entity stay in the training sample, which is optimistic; `pred_exclude_neighbors=True` removes them as well (see the caveat for `slm`).
|
|
234
|
+
|
|
235
|
+
```python
|
|
236
|
+
from apatools.plm import plm, plm_report, plm_tests
|
|
237
|
+
results, metrics = plm(
|
|
238
|
+
data,
|
|
239
|
+
"y", ["x1", "x2"],
|
|
240
|
+
w=w,
|
|
241
|
+
entity="county", time="year",
|
|
242
|
+
model=["pooled", "fe", "re", "slm_fe"],
|
|
243
|
+
verbose=True,
|
|
244
|
+
pred_metrics=True,
|
|
245
|
+
pred_group="time",
|
|
246
|
+
)
|
|
247
|
+
results_rep = plm_report(results, metrics)
|
|
248
|
+
print(results_rep, metrics)
|
|
249
|
+
print(plm_tests(results)) # Hausman, Mundlak, F test of entity effects, spatial LM tests
|
|
250
|
+
```
|
|
251
|
+
|
|
252
|
+
`plm_tests()` returns a tidy table: Hausman test (FE vs RE; undefined when `V_fe - V_re` is not positive definite, which is typical when the effects *are* correlated with the predictors), the regression-based Mundlak test (robust to that problem), the F test of entity effects, and, for pooled spreg models with weights, the Baltagi-Song-Koh and Anselin LM tests for random effects and spatial dependence.
|
|
253
|
+
|
|
254
|
+
Notes: R² is `R²within` for fixed effects, `R²overall` for random effects with linearmodels, `R²pseudo` (squared correlation of observed and predicted y) for spatial and spreg random-effects models and the plain R² for pooled OLS. spreg's `PanelFE` reports residuals and a pseudo R² that ignore the fixed effects and uses `N·T - N` instead of `N·T - N - k` degrees of freedom for the standard errors (slightly too small); the within R², the residual statistics and the F test use the correct values and agree with linearmodels. AIC/BIC from spreg do not count the fixed effects.
|
|
255
|
+
|
|
256
|
+
|
|
257
|
+
### *apatools.sem*
|
|
258
|
+
Structural equation modeling implemented with *semopy*. The module provides modeling function `sem()`, formatting `sem_report()` and plotting `sem_plot()` functions.
|
|
259
|
+
|
|
260
|
+
```python
|
|
261
|
+
from apatools.sem import sem, sem_report, sem_plot
|
|
262
|
+
stats, metrics, model = sem(
|
|
263
|
+
data,
|
|
264
|
+
formula,
|
|
265
|
+
method="MLW", # MLW ULS GLS FIML DWLS WLS
|
|
266
|
+
return_model=True,
|
|
267
|
+
)
|
|
268
|
+
results_rep = sem_report(stats, metrics)
|
|
269
|
+
print(results_rep)
|
|
270
|
+
fig = sem_plot(
|
|
271
|
+
stats,
|
|
272
|
+
save_to_file="fig.png",
|
|
273
|
+
return_fig=True,
|
|
274
|
+
)
|
|
275
|
+
```
|
|
276
|
+
|
|
277
|
+
|
|
278
|
+
### *apatools.psm*
|
|
279
|
+
Propensity score matching implemented with *psmpy*. The module provides modeling function `psm()` and `psm_effect_size()` to report differences.
|
|
280
|
+
|
|
281
|
+
```python
|
|
282
|
+
from apatools.psm import psm, psm_effect_size
|
|
283
|
+
results, model = psm(
|
|
284
|
+
data,
|
|
285
|
+
treatment_variable='vs',
|
|
286
|
+
include_vars=['age', 'gender'],
|
|
287
|
+
balance=True,
|
|
288
|
+
replacement=True,
|
|
289
|
+
return_model=True,
|
|
290
|
+
)
|
|
291
|
+
print(psm_effect_size(model))
|
|
292
|
+
```
|
|
293
|
+
|
|
294
|
+
|
|
295
|
+
### *apatools.format*
|
|
296
|
+
Simple APA-compliant functions for formatting numeric results, such as formatting R and r values, p-values, etc.
|
|
297
|
+
|
|
298
|
+
|
|
299
|
+
#### Formatting examples
|
|
300
|
+
```python
|
|
301
|
+
from apatools.format import get_stars, format_p, format_r
|
|
302
|
+
# formatting numeric results
|
|
303
|
+
r = format_r(0.999, keep_spaces=True, use_letter="r", no_equals=False) # r = 'r = 1.'
|
|
304
|
+
r = format_r(0.011, keep_spaces=True, use_letter="R", no_equals=False) # r = 'R = .01'
|
|
305
|
+
p = format_p(0.0004, use_letter="p", keep_spaces=True, no_equals=False) # p = 'p < .001'
|
|
306
|
+
s = get_stars(0.04, p001="***", p01="**", p05="*", p10="⁺", p_="") # s = '*'
|
|
307
|
+
```
|
|
308
|
+
|
|
309
|
+
|
|
310
|
+
## Sources and references
|
|
311
|
+
* [statsmodels](https://www.statsmodels.org/stable/) (BSD License)
|
|
312
|
+
* [linearmodels](https://pypi.org/project/linearmodels/) (NCSA License)
|
|
313
|
+
* [semopy](https://pypi.org/project/semopy/) (MIT License)
|
|
314
|
+
* [spreg](https://pypi.org/project/spreg/) (BSD License)
|
|
315
|
+
* [psmpy](https://pypi.org/project/psmpy/) (MIT License)
|
|
316
|
+
* [scipy](https://pypi.org/project/scipy/) (BSD License)
|
|
317
|
+
|
|
318
|
+
Note that APA citations of the original packages can be imported from each specific implementation as follows:
|
|
319
|
+
```python
|
|
320
|
+
# e.g., for psmpy
|
|
321
|
+
from apatools.psm import CITATION
|
|
322
|
+
print(CITATION.APA)
|
|
323
|
+
```
|
|
324
|
+
|
|
325
|
+
## License
|
|
326
|
+
MIT, see [LICENSE](LICENSE). Note that the optional dependency *krippendorff* is licensed under GPL-3.0-or-later.
|
|
327
|
+
|