Skip to content

Commit

Permalink
Improve estimation table (#379)
Browse files Browse the repository at this point in the history
  • Loading branch information
ChristianZimpelmann committed Apr 10, 2023
1 parent a770938 commit 2dcfe51
Show file tree
Hide file tree
Showing 6 changed files with 263 additions and 90 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,22 @@
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"Estimagic can create publication quality tables of parameter estimates in LaTeX or HTML. It works with the results from `estimate_ml` and `estimate_msm` but also supports statsmodels results out of the box. \n",
"\n",
"You can get almost limitless flexibility if you split the table generation into two steps. The fist generates a DataFrame which you can customize to your liking, the second renders that DataFrame in LaTeX or HTML."
"You can get almost limitless flexibility if you split the table generation into two steps. The fist generates a DataFrame which you can customize to your liking, the second renders that DataFrame in LaTeX or HTML. If you are interested in this feature, search for \"render_inputs\" below."
]
},
{
"cell_type": "code",
"execution_count": 1,
"execution_count": 24,
"metadata": {},
"outputs": [],
"source": [
"# Make necessary imports\n",
"import estimagic as em\n",
"import pandas as pd\n",
"import statsmodels.formula.api as sm\n",
Expand All @@ -38,7 +40,7 @@
},
{
"cell_type": "code",
"execution_count": 2,
"execution_count": 25,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -50,7 +52,7 @@
},
{
"cell_type": "code",
"execution_count": 3,
"execution_count": 26,
"metadata": {},
"outputs": [
{
Expand Down Expand Up @@ -156,7 +158,7 @@
"<IPython.core.display.HTML object>"
]
},
"execution_count": 3,
"execution_count": 26,
"metadata": {},
"output_type": "execute_result"
}
Expand All @@ -181,7 +183,7 @@
},
{
"cell_type": "code",
"execution_count": 4,
"execution_count": 27,
"metadata": {},
"outputs": [
{
Expand Down Expand Up @@ -240,7 +242,7 @@
"Sex -33.789 1.61800 1.000000e-08"
]
},
"execution_count": 4,
"execution_count": 27,
"metadata": {},
"output_type": "execute_result"
}
Expand All @@ -266,7 +268,7 @@
},
{
"cell_type": "code",
"execution_count": 5,
"execution_count": 28,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -276,7 +278,7 @@
},
{
"cell_type": "code",
"execution_count": 6,
"execution_count": 29,
"metadata": {},
"outputs": [
{
Expand Down Expand Up @@ -398,7 +400,7 @@
"<IPython.core.display.HTML object>"
]
},
"execution_count": 6,
"execution_count": 29,
"metadata": {},
"output_type": "execute_result"
}
Expand Down Expand Up @@ -436,7 +438,7 @@
},
{
"cell_type": "code",
"execution_count": 7,
"execution_count": 30,
"metadata": {},
"outputs": [
{
Expand Down Expand Up @@ -514,7 +516,7 @@
"F Statistic 8.06$^{***}$ 72.90$^{***}$ "
]
},
"execution_count": 7,
"execution_count": 30,
"metadata": {},
"output_type": "execute_result"
}
Expand All @@ -534,7 +536,7 @@
},
{
"cell_type": "code",
"execution_count": 8,
"execution_count": 31,
"metadata": {},
"outputs": [
{
Expand Down Expand Up @@ -638,7 +640,7 @@
"<IPython.core.display.HTML object>"
]
},
"execution_count": 8,
"execution_count": 31,
"metadata": {},
"output_type": "execute_result"
}
Expand All @@ -648,6 +650,140 @@
"HTML(em.render_html(**render_inputs))"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"Using this 2-step-procedure, we can also easily add additional rows to the footer.\n",
"\n",
"Note that we add the row using `.loc[(\"Statsmodels\", )]` since the index of `render_inputs[\"footer\"]` is a MultiIndex.\n",
"\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 32,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<table>\n",
" <thead>\n",
" <tr>\n",
" <th >&nbsp;</th>\n",
" <th colspan=\"3\">target</th>\n",
" </tr>\n",
" <tr>\n",
" <th >&nbsp;</th>\n",
" <th >(1)</th>\n",
" <th >(2)</th>\n",
" <th >(3)</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th >Intercept</th>\n",
" <td >152.00$^{*** }$</td>\n",
" <td >152.00$^{*** }$</td>\n",
" <td >142.00$^{*** }$</td>\n",
" </tr>\n",
" <tr>\n",
" <th ></th>\n",
" <td >(3.61)</td>\n",
" <td >(2.85)</td>\n",
" <td >(3.14)</td>\n",
" </tr>\n",
" <tr>\n",
" <th >Age</th>\n",
" <td >301.00$^{*** }$</td>\n",
" <td >37.20$^{ }$</td>\n",
" <td >51.50$^{*** }$</td>\n",
" </tr>\n",
" <tr>\n",
" <th ></th>\n",
" <td >(77.10)</td>\n",
" <td >(64.10)</td>\n",
" <td >(2.72)</td>\n",
" </tr>\n",
" <tr>\n",
" <th >Sex</th>\n",
" <td >17.40$^{ }$</td>\n",
" <td >-107.00$^{* }$</td>\n",
" <td >-33.80$^{*** }$</td>\n",
" </tr>\n",
" <tr>\n",
" <th ></th>\n",
" <td >(77.10)</td>\n",
" <td >(62.10)</td>\n",
" <td >(1.62)</td>\n",
" </tr>\n",
" <tr>\n",
" <th >BMI</th>\n",
" <td ></td>\n",
" <td >787.00$^{*** }$</td>\n",
" <td ></td>\n",
" </tr>\n",
" <tr>\n",
" <th ></th>\n",
" <td ></td>\n",
" <td >(65.40)</td>\n",
" <td ></td>\n",
" </tr>\n",
" <tr>\n",
" <th >ABP</th>\n",
" <td ></td>\n",
" <td >417.00$^{*** }$</td>\n",
" <td ></td>\n",
" </tr>\n",
" <tr>\n",
" <th ></th>\n",
" <td ></td>\n",
" <td >(69.50)</td>\n",
" <td ></td>\n",
" </tr>\n",
" <tr><td colspan=\"4\" style=\"border-bottom: 1px solid black\">\n",
" </td></tr> <tbody>\n",
" <tr>\n",
" <th >R$^2$</th>\n",
" <td >0.04</td>\n",
" <td >0.40</td>\n",
" <td ></td>\n",
" </tr>\n",
" <tr>\n",
" <th >Observations</th>\n",
" <td >442</td>\n",
" <td >442</td>\n",
" <td >445</td>\n",
" </tr>\n",
" <tr>\n",
" <th >Statsmodels</th>\n",
" <td >Yes</td>\n",
" <td >Yes</td>\n",
" <td >No</td>\n",
" </tr>\n",
" <tr><td colspan=\"4\" style=\"border-bottom: 1px solid black\">\n",
" </td></tr>\n",
" <tr><td style=\"text-align: left\">Note:</td><td colspan=\"3\"\n",
" style=\"text-align: right\"><sup>***</sup>p&lt;0.01; <sup>**</sup>p&lt;0.05; <sup>*</sup>p&lt;0.1 </td></tbody>\n",
"</table>"
],
"text/plain": [
"<IPython.core.display.HTML object>"
]
},
"execution_count": 32,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"render_inputs[\"footer\"].loc[(\"Statsmodels\",)] = [\"Yes\"] * 2 + [\"No\"]\n",
"HTML(em.render_html(**render_inputs))"
]
},
{
"cell_type": "markdown",
"metadata": {},
Expand All @@ -659,7 +795,7 @@
},
{
"cell_type": "code",
"execution_count": 9,
"execution_count": 33,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -675,7 +811,7 @@
},
{
"cell_type": "code",
"execution_count": 10,
"execution_count": 34,
"metadata": {},
"outputs": [
{
Expand Down Expand Up @@ -798,7 +934,7 @@
"<IPython.core.display.HTML object>"
]
},
"execution_count": 10,
"execution_count": 34,
"metadata": {},
"output_type": "execute_result"
}
Expand Down Expand Up @@ -937,6 +1073,11 @@
"\\end{tabular}\n",
"```"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": []
}
],
"metadata": {
Expand All @@ -962,7 +1103,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.8"
"version": "3.10.10"
}
},
"nbformat": 4,
Expand Down
4 changes: 2 additions & 2 deletions src/estimagic/differentiation/derivatives.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ def first_derivative(
error_handling (str): One of "continue" (catch errors and continue to calculate
derivative estimates. In this case, some derivative estimates can be
missing but no errors are raised), "raise" (catch errors and continue
to calculate derivative estimates at fist but raise an error if all
to calculate derivative estimates at first but raise an error if all
evaluations for one parameter failed) and "raise_strict" (raise an error
as soon as a function evaluation fails).
batch_evaluator (str or callable): Name of a pre-implemented batch evaluator
Expand Down Expand Up @@ -360,7 +360,7 @@ def second_derivative(
error_handling (str): One of "continue" (catch errors and continue to calculate
derivative estimates. In this case, some derivative estimates can be
missing but no errors are raised), "raise" (catch errors and continue
to calculate derivative estimates at fist but raise an error if all
to calculate derivative estimates at first but raise an error if all
evaluations for one parameter failed) and "raise_strict" (raise an error
as soon as a function evaluation fails).
batch_evaluator (str or callable): Name of a pre-implemented batch evaluator
Expand Down
2 changes: 1 addition & 1 deletion src/estimagic/optimization/internal_criterion_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ def internal_criterion_and_derivative_template(
derivative.
criterion_and_derivative (callable): Function that returns criterion
and derivative as a tuple. This can be used to exploit synergies in the
evaluation of both functions. The fist element of the tuple has to be
evaluation of both functions. The first element of the tuple has to be
exactly the same as the output of criterion. The second has to be exactly
the same as the output of derivative.
numdiff_options (dict): Keyword arguments for the calculation of numerical
Expand Down
4 changes: 2 additions & 2 deletions src/estimagic/optimization/optimize.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ def maximize(
derivative_kwargs (dict): Additional keyword arguments for derivative.
criterion_and_derivative (callable): Function that returns criterion
and derivative as a tuple. This can be used to exploit synergies in the
evaluation of both functions. The fist element of the tuple has to be
evaluation of both functions. The first element of the tuple has to be
exactly the same as the output of criterion. The second has to be exactly
the same as the output of derivative.
criterion_and_derivative_kwargs (dict): Additional keyword arguments for
Expand Down Expand Up @@ -300,7 +300,7 @@ def minimize(
derivative_kwargs (dict): Additional keyword arguments for derivative.
criterion_and_derivative (callable): Function that returns criterion
and derivative as a tuple. This can be used to exploit synergies in the
evaluation of both functions. The fist element of the tuple has to be
evaluation of both functions. The first element of the tuple has to be
exactly the same as the output of criterion. The second has to be exactly
the same as the output of derivative.
criterion_and_derivative_kwargs (dict): Additional keyword arguments for
Expand Down
Loading

0 comments on commit 2dcfe51

Please sign in to comment.