regtabletotext


Nameregtabletotext JSON
Version 0.0.12 PyPI version JSON
download
home_pagehttps://github.com/christophscheuch/regtabletotext
SummaryHelpers to print regression output as well-formated text
upload_time2023-12-19 08:38:33
maintainer
docs_urlNone
authorChristoph Scheuch
requires_python
licenseMIT
keywords regression table formatting quarto text
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # regtabletotext: helpers to print regression output as text

## What is regtabletotext?

This package is a collection of helper functions to print regression output of the Python packages `statsmodels`, `linearmodels`, and `arch` as text strings. The helpers are particularly useful for users who want to render regression output in [Quarto](https://quarto.org/) to HTML and PDF.

If you want to export your results to LaTeX or HTML, please check out [stargazer](https://pypi.org/project/stargazer/).

## How to install regtabletotext?

The package is available on [pypi.org/project/regtabletotext](https://pypi.org/project/regtabletotext/):

```
pip install regtabletotext
```

## How to use regtabletotext?

Currently supported model types:

- `statsmodels.regression.linear_model.RegressionResultsWrapper`
- `linearmodels.panel.results.PanelEffectsResults`
- `arch.univariate.base.ARCHModelResult`

### For statsmodels regression output

The following code chunk
```
import pandas as pd
import numpy as np
import statsmodels.formula.api as smf
from regtabletotext import prettify_result

n = 100 
data = pd.DataFrame({
    'age': np.random.randint(18, 65, n),
    'income': np.random.normal(50, 20, n) ,
    'education': np.random.randint(12, 22, n),
    'hours_worked': np.random.randint(20, 60, n),
    'satisfaction': np.random.randint(1, 11, n) 
})

mod = smf.ols(formula='income ~ age + education + hours_worked', data=data)
res = mod.fit()

prettify_result(res)
```

returns this text
```
OLS Model:
Lottery ~ Literacy + Wealth + Region

Coefficients:
             Estimate  Std. Error  Statistic  p-Value
Intercept      38.652       9.456      4.087    0.000
Region[T.E]   -15.428       9.727     -1.586    0.117
Region[T.N]   -10.017       9.260     -1.082    0.283
Region[T.S]    -4.548       7.279     -0.625    0.534
Region[T.W]   -10.091       7.196     -1.402    0.165
Literacy       -0.186       0.210     -0.886    0.378
Wealth          0.452       0.103      4.390    0.000

Summary statistics:
- Number of observations: 85
- R-squared: 0.338, Adjusted R-squared: 0.287
- F-statistic: 6.636 on 6 and 78 DF, p-value: 0.000
```

### For linearmodels regression output

This code chunk
```
import pandas as pd
import numpy as np
from linearmodels import PanelOLS
from regtabletotext import prettify_result

np.random.seed(42)
n_entities = 100
n_time_periods = 5

data = {
    'entity': np.repeat(np.arange(n_entities), n_time_periods),
    'time': np.tile(np.arange(n_time_periods), n_entities),
    'y': np.random.randn(n_entities * n_time_periods),
    'x1': np.random.randn(n_entities * n_time_periods),
    'x2': np.random.randn(n_entities * n_time_periods)
}

df = pd.DataFrame(data)
df.set_index(['entity', 'time'], inplace=True)

formula = 'y ~ x1 + x2 + EntityEffects + TimeEffects'
model = PanelOLS.from_formula(formula, df)
result = model.fit()

prettify_result(result, options={'digits':2, 'include_residuals': True})
```
produces this output
```
Panel OLS Model:
y ~ x1 + x2 + EntityEffects + TimeEffects

Covariance Type: Unadjusted

Residuals:
 Mean  Std  Min   25%  50%  75%  Max
  0.0 0.87 -2.7 -0.62  0.0 0.58 3.16

Coefficients:
    Estimate  Std. Error  t-Statistic  p-Value

x1     -0.06        0.05        -1.12     0.26
x2     -0.04        0.05        -0.80     0.42

Included Fixed Effects:
        Total
Entity  100.0
Time      5.0

Summary statistics:
- Number of observations: 500
- R-squared (incl. FE): 0.21, Within R-squared: 0.01
- F-statistic: 1.05, p-value: 0.35
```
For multiple models, you can also use `prettify_result`:
```
formula = 'y ~ x1 + x2'
model = PanelOLS.from_formula(formula, df)
result1 = model.fit()

formula = 'y ~ x2 + EntityEffects'
model = PanelOLS.from_formula(formula, df)
result2 = model.fit()

formula = 'y ~ x1 + x2 + EntityEffects + TimeEffects'
model = PanelOLS.from_formula(formula, df)
result3 = model.fit()

results = [result1, result2, result3]

prettify_result(results)
```
The result is:
```
Dependent var.        y               y               y

x1              -0.072 (-1.59)                  -0.058 (-1.12)
x2              -0.049 (-1.14)  -0.049 (-0.98)  -0.041 (-0.8)

Fixed effects                       Entity       Entity, Time
VCOV type         Unadjusted      Unadjusted      Unadjusted
Observations         500             500             500
R2 (incl. FE)       0.008           0.198           0.208
Within R2           0.006           0.002           0.006
```

### For arch estimation output

For arch estimations like these:
```
import datetime as dt
import pandas_datareader.data as web
from arch import arch_model
import arch.data.sp500
from regtabletotext import prettify_result

start = dt.datetime(1988, 1, 1)
end = dt.datetime(2018, 1, 1)
data = arch.data.sp500.load()
returns = 100 * data['Adj Close'].pct_change().dropna()
am_fit = arch_model(returns).fit(update_freq=5)

prettify_result(am_fit)
```
you get:
```
Constant Mean - GARCH Model Results

Mean Coefficients:
    Estimate  Std. Error  Statistic  p-Value
mu     0.056       0.011      4.906      0.0

Coefficients for GARCH(p: 1, q: 1):
          Estimate  Std. Error  Statistic  p-Value
omega        0.018       0.005      3.738      0.0
alpha[1]     0.102       0.013      7.852      0.0
beta[1]      0.885       0.014     64.125      0.0

Summary statistics:
- Number of observations: 5,030
- Distribution: Normal distribution
- Multiple R-squared: 0.000, Adjusted R-squared: 0.000
- BIC: 13,907.530,  AIC: 13,881.437
```

# Version 0.0.12
- Added note that table contains t-statistics
- `prettify_result()` now passes lists to `prettify_results()`

# Version 0.0.11
- Replaced reporting of F-statistic nan with "F-statistic not available"

# Version 0.0.10
- Increased default `max_width` from 64 to 80

# Version 0.0.9
- Added support for list of `linearmodels.panel.result.PanelEffectsresult` results 

# Version 0.0.8
- Added support for 'arch.univariate.base.ARCHModelResult'
- Simplified and extended `calculate_residuals_statistics()`
- Added covariance type and fixed effects table to `linearmodels.panel.result.PanelEffectsresult` print result
- Moved default values to function definitions
- Updated required install to pandas only
- Added support for `arch.univariate.base.ARCHModelResult`

# Version 0.0.7
- Moved type checks to functions and globals
- Removed unnecessary whitespace from model formula
- Introduced `max_width` with default to 64 characters 

# Version 0.0.6
- Introduced check if 't' column is present, otherwise use 'z' column to handle models with only one coefficient

# Version 0.0.5
- Replaced explicit options by flexible `**options` parameter
- Supported classes: `statsmodels.regression.linear_model.RegressionResultsWrapper` and `linearmodels.panel.result.PanelEffectsresult`
- Introduced `ALLOWED_OPTIONS` global

# Version 0.0.4
- First working version with statsmodels support

            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/christophscheuch/regtabletotext",
    "name": "regtabletotext",
    "maintainer": "",
    "docs_url": null,
    "requires_python": "",
    "maintainer_email": "",
    "keywords": "Regression,Table,Formatting,Quarto,Text",
    "author": "Christoph Scheuch",
    "author_email": "christoph.scheuch@gmail.com",
    "download_url": "https://files.pythonhosted.org/packages/81/b3/e84a07cd8182494d34fe24a9fc254fdc9578aba48f83db4199445b376fd7/regtabletotext-0.0.12.tar.gz",
    "platform": null,
    "description": "# regtabletotext: helpers to print regression output as text\n\n## What is regtabletotext?\n\nThis package is a collection of helper functions to print regression output of the Python packages `statsmodels`, `linearmodels`, and `arch` as text strings. The helpers are particularly useful for users who want to render regression output in [Quarto](https://quarto.org/) to HTML and PDF.\n\nIf you want to export your results to LaTeX or HTML, please check out [stargazer](https://pypi.org/project/stargazer/).\n\n## How to install regtabletotext?\n\nThe package is available on [pypi.org/project/regtabletotext](https://pypi.org/project/regtabletotext/):\n\n```\npip install regtabletotext\n```\n\n## How to use regtabletotext?\n\nCurrently supported model types:\n\n- `statsmodels.regression.linear_model.RegressionResultsWrapper`\n- `linearmodels.panel.results.PanelEffectsResults`\n- `arch.univariate.base.ARCHModelResult`\n\n### For statsmodels regression output\n\nThe following code chunk\n```\nimport pandas as pd\nimport numpy as np\nimport statsmodels.formula.api as smf\nfrom regtabletotext import prettify_result\n\nn = 100 \ndata = pd.DataFrame({\n    'age': np.random.randint(18, 65, n),\n    'income': np.random.normal(50, 20, n) ,\n    'education': np.random.randint(12, 22, n),\n    'hours_worked': np.random.randint(20, 60, n),\n    'satisfaction': np.random.randint(1, 11, n) \n})\n\nmod = smf.ols(formula='income ~ age + education + hours_worked', data=data)\nres = mod.fit()\n\nprettify_result(res)\n```\n\nreturns this text\n```\nOLS Model:\nLottery ~ Literacy + Wealth + Region\n\nCoefficients:\n             Estimate  Std. Error  Statistic  p-Value\nIntercept      38.652       9.456      4.087    0.000\nRegion[T.E]   -15.428       9.727     -1.586    0.117\nRegion[T.N]   -10.017       9.260     -1.082    0.283\nRegion[T.S]    -4.548       7.279     -0.625    0.534\nRegion[T.W]   -10.091       7.196     -1.402    0.165\nLiteracy       -0.186       0.210     -0.886    0.378\nWealth          0.452       0.103      4.390    0.000\n\nSummary statistics:\n- Number of observations: 85\n- R-squared: 0.338, Adjusted R-squared: 0.287\n- F-statistic: 6.636 on 6 and 78 DF, p-value: 0.000\n```\n\n### For linearmodels regression output\n\nThis code chunk\n```\nimport pandas as pd\nimport numpy as np\nfrom linearmodels import PanelOLS\nfrom regtabletotext import prettify_result\n\nnp.random.seed(42)\nn_entities = 100\nn_time_periods = 5\n\ndata = {\n    'entity': np.repeat(np.arange(n_entities), n_time_periods),\n    'time': np.tile(np.arange(n_time_periods), n_entities),\n    'y': np.random.randn(n_entities * n_time_periods),\n    'x1': np.random.randn(n_entities * n_time_periods),\n    'x2': np.random.randn(n_entities * n_time_periods)\n}\n\ndf = pd.DataFrame(data)\ndf.set_index(['entity', 'time'], inplace=True)\n\nformula = 'y ~ x1 + x2 + EntityEffects + TimeEffects'\nmodel = PanelOLS.from_formula(formula, df)\nresult = model.fit()\n\nprettify_result(result, options={'digits':2, 'include_residuals': True})\n```\nproduces this output\n```\nPanel OLS Model:\ny ~ x1 + x2 + EntityEffects + TimeEffects\n\nCovariance Type: Unadjusted\n\nResiduals:\n Mean  Std  Min   25%  50%  75%  Max\n  0.0 0.87 -2.7 -0.62  0.0 0.58 3.16\n\nCoefficients:\n    Estimate  Std. Error  t-Statistic  p-Value\n\nx1     -0.06        0.05        -1.12     0.26\nx2     -0.04        0.05        -0.80     0.42\n\nIncluded Fixed Effects:\n        Total\nEntity  100.0\nTime      5.0\n\nSummary statistics:\n- Number of observations: 500\n- R-squared (incl. FE): 0.21, Within R-squared: 0.01\n- F-statistic: 1.05, p-value: 0.35\n```\nFor multiple models, you can also use `prettify_result`:\n```\nformula = 'y ~ x1 + x2'\nmodel = PanelOLS.from_formula(formula, df)\nresult1 = model.fit()\n\nformula = 'y ~ x2 + EntityEffects'\nmodel = PanelOLS.from_formula(formula, df)\nresult2 = model.fit()\n\nformula = 'y ~ x1 + x2 + EntityEffects + TimeEffects'\nmodel = PanelOLS.from_formula(formula, df)\nresult3 = model.fit()\n\nresults = [result1, result2, result3]\n\nprettify_result(results)\n```\nThe result is:\n```\nDependent var.        y               y               y\n\nx1              -0.072 (-1.59)                  -0.058 (-1.12)\nx2              -0.049 (-1.14)  -0.049 (-0.98)  -0.041 (-0.8)\n\nFixed effects                       Entity       Entity, Time\nVCOV type         Unadjusted      Unadjusted      Unadjusted\nObservations         500             500             500\nR2 (incl. FE)       0.008           0.198           0.208\nWithin R2           0.006           0.002           0.006\n```\n\n### For arch estimation output\n\nFor arch estimations like these:\n```\nimport datetime as dt\nimport pandas_datareader.data as web\nfrom arch import arch_model\nimport arch.data.sp500\nfrom regtabletotext import prettify_result\n\nstart = dt.datetime(1988, 1, 1)\nend = dt.datetime(2018, 1, 1)\ndata = arch.data.sp500.load()\nreturns = 100 * data['Adj Close'].pct_change().dropna()\nam_fit = arch_model(returns).fit(update_freq=5)\n\nprettify_result(am_fit)\n```\nyou get:\n```\nConstant Mean - GARCH Model Results\n\nMean Coefficients:\n    Estimate  Std. Error  Statistic  p-Value\nmu     0.056       0.011      4.906      0.0\n\nCoefficients for GARCH(p: 1, q: 1):\n          Estimate  Std. Error  Statistic  p-Value\nomega        0.018       0.005      3.738      0.0\nalpha[1]     0.102       0.013      7.852      0.0\nbeta[1]      0.885       0.014     64.125      0.0\n\nSummary statistics:\n- Number of observations: 5,030\n- Distribution: Normal distribution\n- Multiple R-squared: 0.000, Adjusted R-squared: 0.000\n- BIC: 13,907.530,  AIC: 13,881.437\n```\n\n# Version 0.0.12\n- Added note that table contains t-statistics\n- `prettify_result()` now passes lists to `prettify_results()`\n\n# Version 0.0.11\n- Replaced reporting of F-statistic nan with \"F-statistic not available\"\n\n# Version 0.0.10\n- Increased default `max_width` from 64 to 80\n\n# Version 0.0.9\n- Added support for list of `linearmodels.panel.result.PanelEffectsresult` results \n\n# Version 0.0.8\n- Added support for 'arch.univariate.base.ARCHModelResult'\n- Simplified and extended `calculate_residuals_statistics()`\n- Added covariance type and fixed effects table to `linearmodels.panel.result.PanelEffectsresult` print result\n- Moved default values to function definitions\n- Updated required install to pandas only\n- Added support for `arch.univariate.base.ARCHModelResult`\n\n# Version 0.0.7\n- Moved type checks to functions and globals\n- Removed unnecessary whitespace from model formula\n- Introduced `max_width` with default to 64 characters \n\n# Version 0.0.6\n- Introduced check if 't' column is present, otherwise use 'z' column to handle models with only one coefficient\n\n# Version 0.0.5\n- Replaced explicit options by flexible `**options` parameter\n- Supported classes: `statsmodels.regression.linear_model.RegressionResultsWrapper` and `linearmodels.panel.result.PanelEffectsresult`\n- Introduced `ALLOWED_OPTIONS` global\n\n# Version 0.0.4\n- First working version with statsmodels support\n",
    "bugtrack_url": null,
    "license": "MIT",
    "summary": "Helpers to print regression output as well-formated text",
    "version": "0.0.12",
    "project_urls": {
        "Download": "https://pypi.org/project/regtabletotext/",
        "Homepage": "https://github.com/christophscheuch/regtabletotext"
    },
    "split_keywords": [
        "regression",
        "table",
        "formatting",
        "quarto",
        "text"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "75138bbc22000d1b46553220fea0ff27b36cbbfdb9306fe6d40251e5e313cf71",
                "md5": "ff26d10456204226ff40975f71569c6e",
                "sha256": "b6f7ef3c2101cd4b2d150eada5de599f4f8764ac5e03224f11b64b46f432d44e"
            },
            "downloads": -1,
            "filename": "regtabletotext-0.0.12-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "ff26d10456204226ff40975f71569c6e",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": null,
            "size": 9895,
            "upload_time": "2023-12-19T08:38:26",
            "upload_time_iso_8601": "2023-12-19T08:38:26.291255Z",
            "url": "https://files.pythonhosted.org/packages/75/13/8bbc22000d1b46553220fea0ff27b36cbbfdb9306fe6d40251e5e313cf71/regtabletotext-0.0.12-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "81b3e84a07cd8182494d34fe24a9fc254fdc9578aba48f83db4199445b376fd7",
                "md5": "3e527c8898db6aff144b08926410aa18",
                "sha256": "e02268ef40c7a00d790c11f7fb18be9ca5b232fe57b599b59b60883baadba0f0"
            },
            "downloads": -1,
            "filename": "regtabletotext-0.0.12.tar.gz",
            "has_sig": false,
            "md5_digest": "3e527c8898db6aff144b08926410aa18",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": null,
            "size": 2216855,
            "upload_time": "2023-12-19T08:38:33",
            "upload_time_iso_8601": "2023-12-19T08:38:33.722098Z",
            "url": "https://files.pythonhosted.org/packages/81/b3/e84a07cd8182494d34fe24a9fc254fdc9578aba48f83db4199445b376fd7/regtabletotext-0.0.12.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2023-12-19 08:38:33",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "christophscheuch",
    "github_project": "regtabletotext",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": false,
    "lcname": "regtabletotext"
}
        
Elapsed time: 0.32649s