# slimp: linear models with Stan and Pandas
*slimp* estimates linear models using [Stan](https://mc-stan.org/) and [Pandas](https://pandas.pydata.org/). Think [rstanarm](https://mc-stan.org/rstanarm/) or [brms](https://mc-stan.org/users/interfaces/brms), but in Python and faster.
Create the model:
```python
import matplotlib.pyplot
import numpy
import pandas
import slimp
y, x = numpy.mgrid[0:10, 0:10]
z = 10 + x + 2*y + numpy.random.normal(0, 2, (10, 10))
data = pandas.DataFrame({"x": x.ravel(), "y": y.ravel(), "z": z.ravel()})
model = slimp.Model("z ~ 1 + x + y", data, num_chains=4)
# Also possible to specify random seed
# model = slimp.Model("z ~ 1 + x + y", data, seed=42)
```
Sample the parameters, check the results:
```python
model.sample()
print(model.hmc_diagnostics)
print(model.summary()[["N_Eff", "R_hat"]].describe().loc[["min", "max"], :])
r_squared = slimp.r_squared(model)
print(r_squared.quantile([0.05, 0.95]))
```
Plot prior and posterior predictive checks:
```python
figure, plots = matplotlib.pyplot.subplots(1, 2, layout="tight", figsize=(8, 4))
slimp.predictive_plot(model, use_prior=True, plot_kwargs={"ax":plots[0]})
slimp.predictive_plot(model, use_prior=False, plot_kwargs={"ax":plots[1]})
```
Plot the credible intervals of the parameters and their distributions:
```
slimp.parameters_plot(model, include=["x", "y"])
slimp.KDEPlot(model.draws["sigma"], prob=0.90)
```
Use a custom Stan model: have a look [here](custom_model_example/README.md)
Raw data
{
"_id": null,
"home_page": "https://github.com/lamyj/slimp",
"name": "slimp",
"maintainer": null,
"docs_url": null,
"requires_python": null,
"maintainer_email": null,
"keywords": "statistics, bayesian, linear-models, stan, pandas, matplotlib",
"author": "Julien Lamy",
"author_email": "lamy@unistra.fr",
"download_url": "https://files.pythonhosted.org/packages/bf/7a/e3311086768b88bf846adddc37051b9c2f76b532bebb52594396d2d2fdc5/slimp-0.5.0.tar.gz",
"platform": null,
"description": "# slimp: linear models with Stan and Pandas\n\n*slimp* estimates linear models using [Stan](https://mc-stan.org/) and [Pandas](https://pandas.pydata.org/). Think [rstanarm](https://mc-stan.org/rstanarm/) or [brms](https://mc-stan.org/users/interfaces/brms), but in Python and faster.\n\nCreate the model:\n\n```python\nimport matplotlib.pyplot\nimport numpy\nimport pandas\nimport slimp\n\ny, x = numpy.mgrid[0:10, 0:10]\nz = 10 + x + 2*y + numpy.random.normal(0, 2, (10, 10))\ndata = pandas.DataFrame({\"x\": x.ravel(), \"y\": y.ravel(), \"z\": z.ravel()})\n\nmodel = slimp.Model(\"z ~ 1 + x + y\", data, num_chains=4)\n# Also possible to specify random seed\n# model = slimp.Model(\"z ~ 1 + x + y\", data, seed=42)\n```\n\nSample the parameters, check the results:\n\n```python\nmodel.sample()\nprint(model.hmc_diagnostics)\nprint(model.summary()[[\"N_Eff\", \"R_hat\"]].describe().loc[[\"min\", \"max\"], :])\nr_squared = slimp.r_squared(model)\nprint(r_squared.quantile([0.05, 0.95]))\n```\n\nPlot prior and posterior predictive checks:\n\n```python\nfigure, plots = matplotlib.pyplot.subplots(1, 2, layout=\"tight\", figsize=(8, 4))\nslimp.predictive_plot(model, use_prior=True, plot_kwargs={\"ax\":plots[0]})\nslimp.predictive_plot(model, use_prior=False, plot_kwargs={\"ax\":plots[1]})\n```\n\nPlot the credible intervals of the parameters and their distributions:\n\n```\nslimp.parameters_plot(model, include=[\"x\", \"y\"])\nslimp.KDEPlot(model.draws[\"sigma\"], prob=0.90)\n```\n\nUse a custom Stan model: have a look [here](custom_model_example/README.md)\n",
"bugtrack_url": null,
"license": "MIT",
"summary": "Linear models with Stan and Pandas",
"version": "0.5.0",
"project_urls": {
"Homepage": "https://github.com/lamyj/slimp"
},
"split_keywords": [
"statistics",
" bayesian",
" linear-models",
" stan",
" pandas",
" matplotlib"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "ed7c6adf88bfd2da1bc20cfb7d909f06c8817fe56092fcc8a19bda8d455a9f72",
"md5": "eebd6d7e8f3a8c60094355d79cfc19e5",
"sha256": "9a64c7e80bb1826989a0d939f7f2397a2ecefb30291ce4baa86cf19dc24dee45"
},
"downloads": -1,
"filename": "slimp-0.5.0-py3-none-any.whl",
"has_sig": false,
"md5_digest": "eebd6d7e8f3a8c60094355d79cfc19e5",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": null,
"size": 1224504,
"upload_time": "2024-05-17T15:32:37",
"upload_time_iso_8601": "2024-05-17T15:32:37.679049Z",
"url": "https://files.pythonhosted.org/packages/ed/7c/6adf88bfd2da1bc20cfb7d909f06c8817fe56092fcc8a19bda8d455a9f72/slimp-0.5.0-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "bf7ae3311086768b88bf846adddc37051b9c2f76b532bebb52594396d2d2fdc5",
"md5": "5f998b469858386ee839db3cf92f6066",
"sha256": "bcaef21ea9efbc69f88ca63ca34652db33a4eb21c1914377aed66583718a18dc"
},
"downloads": -1,
"filename": "slimp-0.5.0.tar.gz",
"has_sig": false,
"md5_digest": "5f998b469858386ee839db3cf92f6066",
"packagetype": "sdist",
"python_version": "source",
"requires_python": null,
"size": 21213,
"upload_time": "2024-05-17T15:32:39",
"upload_time_iso_8601": "2024-05-17T15:32:39.822477Z",
"url": "https://files.pythonhosted.org/packages/bf/7a/e3311086768b88bf846adddc37051b9c2f76b532bebb52594396d2d2fdc5/slimp-0.5.0.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-05-17 15:32:39",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "lamyj",
"github_project": "slimp",
"travis_ci": false,
"coveralls": false,
"github_actions": true,
"lcname": "slimp"
}