{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Stargazer Examples" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import pandas as pd\n", "from sklearn import datasets\n", "import statsmodels.api as sm\n", "from stargazer.stargazer import Stargazer, LineLocation" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "/home/pietro/.local/lib/python3.9/site-packages/statsmodels/tsa/tsatools.py:142: FutureWarning: In a future version of pandas all arguments of concat except for the argument 'objs' will be keyword-only\n", " x = pd.concat(x[::order], 1)\n" ] } ], "source": [ "diabetes = datasets.load_diabetes()\n", "df = pd.DataFrame(diabetes.data)\n", "df.columns = ['Age', 'Sex', 'BMI', 'ABP', 'S1', 'S2', 'S3', 'S4', 'S5', 'S6']\n", "df['target'] = diabetes.target\n", "\n", "est = sm.OLS(endog=df['target'], exog=sm.add_constant(df[df.columns[0:4]])).fit()\n", "est2 = sm.OLS(endog=df['target'], exog=sm.add_constant(df[df.columns[0:6]])).fit()\n", "\n", "\n", "stargazer = Stargazer([est, est2])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Standard" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "
Dependent variable: target
(1)(2)
ABP416.674***397.583***
(69.495)(70.870)
Age37.24124.704
(64.117)(65.411)
BMI787.179***789.742***
(65.424)(66.887)
S1197.852
(143.812)
S2-169.251
(142.744)
Sex-106.578*-82.862
(62.125)(64.851)
const152.133***152.133***
(2.853)(2.853)
Observations442442
R20.4000.403
Adjusted R20.3950.395
Residual Std. Error59.976 (df=437)59.982 (df=435)
F Statistic72.913*** (df=4; 437)48.915*** (df=6; 435)
Note:*p<0.1; **p<0.05; ***p<0.01
" ], "text/plain": [ "" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Equivalent to:\n", "# from IPython.core.display import HTML\n", "# HTML(stargazer.render_html())\n", "\n", "stargazer" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Custom Title" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/html": [ "Diabetes Study
\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "
Dependent variable: target
(1)(2)
ABP416.674***397.583***
(69.495)(70.870)
Age37.24124.704
(64.117)(65.411)
BMI787.179***789.742***
(65.424)(66.887)
S1197.852
(143.812)
S2-169.251
(142.744)
Sex-106.578*-82.862
(62.125)(64.851)
const152.133***152.133***
(2.853)(2.853)
Observations442442
R20.4000.403
Adjusted R20.3950.395
Residual Std. Error59.976 (df=437)59.982 (df=435)
F Statistic72.913*** (df=4; 437)48.915*** (df=6; 435)
Note:*p<0.1; **p<0.05; ***p<0.01
" ], "text/plain": [ "" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "stargazer.title('Diabetes Study')\n", "stargazer" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Custom Model Names" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/html": [ "Diabetes Study
\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "
Dependent variable: target
Model 1Model 2
(1)(2)
ABP416.674***397.583***
(69.495)(70.870)
Age37.24124.704
(64.117)(65.411)
BMI787.179***789.742***
(65.424)(66.887)
S1197.852
(143.812)
S2-169.251
(142.744)
Sex-106.578*-82.862
(62.125)(64.851)
const152.133***152.133***
(2.853)(2.853)
Observations442442
R20.4000.403
Adjusted R20.3950.395
Residual Std. Error59.976 (df=437)59.982 (df=435)
F Statistic72.913*** (df=4; 437)48.915*** (df=6; 435)
Note:*p<0.1; **p<0.05; ***p<0.01
" ], "text/plain": [ "" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "stargazer.custom_columns(['Model 1', 'Model 2'], [1, 1])\n", "stargazer" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/html": [ "Diabetes Study
\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "
Dependent variable: target
Test model name
(1)(2)
ABP416.674***397.583***
(69.495)(70.870)
Age37.24124.704
(64.117)(65.411)
BMI787.179***789.742***
(65.424)(66.887)
S1197.852
(143.812)
S2-169.251
(142.744)
Sex-106.578*-82.862
(62.125)(64.851)
const152.133***152.133***
(2.853)(2.853)
Observations442442
R20.4000.403
Adjusted R20.3950.395
Residual Std. Error59.976 (df=437)59.982 (df=435)
F Statistic72.913*** (df=4; 437)48.915*** (df=6; 435)
Note:*p<0.1; **p<0.05; ***p<0.01
" ], "text/plain": [ "" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "stargazer.custom_columns('Test model name')\n", "stargazer" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Remove Model Numbers" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/html": [ "Diabetes Study
\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "
Dependent variable: target
Test model name
ABP416.674***397.583***
(69.495)(70.870)
Age37.24124.704
(64.117)(65.411)
BMI787.179***789.742***
(65.424)(66.887)
S1197.852
(143.812)
S2-169.251
(142.744)
Sex-106.578*-82.862
(62.125)(64.851)
const152.133***152.133***
(2.853)(2.853)
Observations442442
R20.4000.403
Adjusted R20.3950.395
Residual Std. Error59.976 (df=437)59.982 (df=435)
F Statistic72.913*** (df=4; 437)48.915*** (df=6; 435)
Note:*p<0.1; **p<0.05; ***p<0.01
" ], "text/plain": [ "" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "stargazer.show_model_numbers(False)\n", "stargazer" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Specify Significant Digits" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "text/html": [ "Diabetes Study
\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "
Dependent variable: target
Test model name
ABP416.67***397.58***
(69.49)(70.87)
Age37.2424.70
(64.12)(65.41)
BMI787.18***789.74***
(65.42)(66.89)
S1197.85
(143.81)
S2-169.25
(142.74)
Sex-106.58*-82.86
(62.13)(64.85)
const152.13***152.13***
(2.85)(2.85)
Observations442442
R20.400.40
Adjusted R20.390.39
Residual Std. Error59.98 (df=437)59.98 (df=435)
F Statistic72.91*** (df=4; 437)48.91*** (df=6; 435)
Note:*p<0.1; **p<0.05; ***p<0.01
" ], "text/plain": [ "" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "stargazer.significant_digits(2)\n", "stargazer" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Show Confidence Intervals Over Standard Errors" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "text/html": [ "Diabetes Study
\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "
Dependent variable: target
Test model name
ABP416.67***397.58***
(280.09 , 553.26)(258.29 , 536.87)
Age37.2424.70
(-88.78 , 163.26)(-103.86 , 153.26)
BMI787.18***789.74***
(658.59 , 915.76)(658.28 , 921.20)
S1197.85
(-84.80 , 480.51)
S2-169.25
(-449.80 , 111.30)
Sex-106.58*-82.86
(-228.68 , 15.52)(-210.32 , 44.60)
const152.13***152.13***
(146.53 , 157.74)(146.53 , 157.74)
Observations442442
R20.400.40
Adjusted R20.390.39
Residual Std. Error59.98 (df=437)59.98 (df=435)
F Statistic72.91*** (df=4; 437)48.91*** (df=6; 435)
Note:*p<0.1; **p<0.05; ***p<0.01
" ], "text/plain": [ "" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "stargazer.show_confidence_intervals(True)\n", "stargazer" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Subset Covariates and/or Choose Covariate Order" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "data": { "text/html": [ "Diabetes Study
\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "
Dependent variable: target
Test model name
BMI787.18***789.74***
(658.59 , 915.76)(658.28 , 921.20)
Age37.2424.70
(-88.78 , 163.26)(-103.86 , 153.26)
S1197.85
(-84.80 , 480.51)
Sex-106.58*-82.86
(-228.68 , 15.52)(-210.32 , 44.60)
Observations442442
R20.400.40
Adjusted R20.390.39
Residual Std. Error59.98 (df=437)59.98 (df=435)
F Statistic72.91*** (df=4; 437)48.91*** (df=6; 435)
Note:*p<0.1; **p<0.05; ***p<0.01
" ], "text/plain": [ "" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "stargazer.covariate_order(['BMI', 'Age', 'S1', 'Sex'])\n", "stargazer" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Rename Covariates" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "data": { "text/html": [ "Diabetes Study
\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "
Dependent variable: target
Test model name
BMI787.18***789.74***
(658.59 , 915.76)(658.28 , 921.20)
Oldness37.2424.70
(-88.78 , 163.26)(-103.86 , 153.26)
S1197.85
(-84.80 , 480.51)
Sex-106.58*-82.86
(-228.68 , 15.52)(-210.32 , 44.60)
Observations442442
R20.400.40
Adjusted R20.390.39
Residual Std. Error59.98 (df=437)59.98 (df=435)
F Statistic72.91*** (df=4; 437)48.91*** (df=6; 435)
Note:*p<0.1; **p<0.05; ***p<0.01
" ], "text/plain": [ "" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "stargazer.rename_covariates({'Age': 'Oldness'})\n", "stargazer" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Hide/Show Degrees of Freedom" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "data": { "text/html": [ "Diabetes Study
\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "
Dependent variable: target
Test model name
BMI787.18***789.74***
(658.59 , 915.76)(658.28 , 921.20)
Oldness37.2424.70
(-88.78 , 163.26)(-103.86 , 153.26)
S1197.85
(-84.80 , 480.51)
Sex-106.58*-82.86
(-228.68 , 15.52)(-210.32 , 44.60)
Observations442442
R20.400.40
Adjusted R20.390.39
Residual Std. Error59.9859.98
F Statistic72.91***48.91***
Note:*p<0.1; **p<0.05; ***p<0.01
" ], "text/plain": [ "" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "stargazer.show_degrees_of_freedom(False)\n", "stargazer" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Show Custom Notes" ] }, { "cell_type": "code", "execution_count": 13, "metadata": { "pycharm": { "name": "#%%\n" } }, "outputs": [ { "data": { "text/html": [ "Diabetes Study
\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "
Dependent variable: target
Test model name
BMI787.18***789.74***
(658.59 , 915.76)(658.28 , 921.20)
Oldness37.2424.70
(-88.78 , 163.26)(-103.86 , 153.26)
S1197.85
(-84.80 , 480.51)
Sex-106.58*-82.86
(-228.68 , 15.52)(-210.32 , 44.60)
Observations442442
R20.400.40
Adjusted R20.390.39
Residual Std. Error59.9859.98
F Statistic72.91***48.91***
Note:*p<0.1; **p<0.05; ***p<0.01
First note
Second note
" ], "text/plain": [ "" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "stargazer.add_custom_notes(['First note', 'Second note'])\n", "stargazer" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Add Custom Lines" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "data": { "text/html": [ "Diabetes Study
\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "
Dependent variable: target
Test model name
BMI787.18***789.74***
(658.59 , 915.76)(658.28 , 921.20)
Oldness37.2424.70
(-88.78 , 163.26)(-103.86 , 153.26)
S1197.85
(-84.80 , 480.51)
Sex-106.58*-82.86
(-228.68 , 15.52)(-210.32 , 44.60)
More controlsNoNo
PreferredNoYes
Observations442442
R20.400.40
Adjusted R20.390.39
Residual Std. Error59.9859.98
F Statistic72.91***48.91***
Note:*p<0.1; **p<0.05; ***p<0.01
First note
Second note
" ], "text/plain": [ "" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "stargazer.add_line('More controls', ['No', 'No'])\n", "stargazer.add_line('Preferred', ['No', 'Yes'], LineLocation.FOOTER_TOP)\n", "stargazer" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Change Statistical Significance Cutoffs" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "data": { "text/html": [ "Diabetes Study
\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "
Dependent variable: target
Test model name
BMI787.18***789.74***
(658.59 , 915.76)(658.28 , 921.20)
Oldness37.2424.70
(-88.78 , 163.26)(-103.86 , 153.26)
S1197.85
(-84.80 , 480.51)
Sex-106.58*-82.86
(-228.68 , 15.52)(-210.32 , 44.60)
More controlsNoNo
PreferredNoYes
Observations442442
R20.400.40
Adjusted R20.390.39
Residual Std. Error59.9859.98
F Statistic72.91***48.91***
Note:*p<0.1; **p<0.07; ***p<0.05
First note
Second note
" ], "text/plain": [ "" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "stargazer.significance_levels([0.1, 0.05, 0.07])\n", "stargazer" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Don't Show Statistical Significance Levels In Notes" ] }, { "cell_type": "code", "execution_count": 16, "metadata": { "scrolled": false }, "outputs": [ { "data": { "text/html": [ "Diabetes Study
\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "
Dependent variable: target
Test model name
BMI787.18***789.74***
(658.59 , 915.76)(658.28 , 921.20)
Oldness37.2424.70
(-88.78 , 163.26)(-103.86 , 153.26)
S1197.85
(-84.80 , 480.51)
Sex-106.58*-82.86
(-228.68 , 15.52)(-210.32 , 44.60)
More controlsNoNo
PreferredNoYes
Observations442442
R20.400.40
Adjusted R20.390.39
Residual Std. Error59.9859.98
F Statistic72.91***48.91***
Note:
First note
Second note
" ], "text/plain": [ "" ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "stargazer.append_notes(False)\n", "stargazer" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Add spacing between rows" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [ { "data": { "text/html": [ "Diabetes Study
\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "
Dependent variable: target
Test model name
BMI787.18***789.74***
(658.59 , 915.76)(658.28 , 921.20)
Oldness37.2424.70
(-88.78 , 163.26)(-103.86 , 153.26)
S1197.85
(-84.80 , 480.51)
Sex-106.58*-82.86
(-228.68 , 15.52)(-210.32 , 44.60)
More controlsNoNo
PreferredNoYes
Observations442442
R20.400.40
Adjusted R20.390.39
Residual Std. Error59.9859.98
F Statistic72.91***48.91***
Note:
First note
Second note
" ], "text/plain": [ "" ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "stargazer.cov_spacing = 2\n", "stargazer" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.9.2" } }, "nbformat": 4, "nbformat_minor": 2 }