Skip to content

Instantly share code, notes, and snippets.

@vincentarelbundock
Last active April 20, 2021 14:28
Show Gist options
  • Save vincentarelbundock/d304388cde49987665dbcaa1578e6e98 to your computer and use it in GitHub Desktop.
Save vincentarelbundock/d304388cde49987665dbcaa1578e6e98 to your computer and use it in GitHub Desktop.
`modelsummary` regression tables in a Jupyter notebook
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"id": "22675799",
"metadata": {},
"source": [
"The `modelsummary` package for `R` can produce regression tables in Jupyter notebook. This feature is only available in the development version on Github, but will be released as part of version 0.7.1."
]
},
{
"cell_type": "markdown",
"id": "c0731878",
"metadata": {},
"source": [
"First we load the package and set the default output format to \"jupyter\":"
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "aa039596",
"metadata": {},
"outputs": [],
"source": [
"library(modelsummary)\n",
"options(modelsummary_default = \"jupyter\")"
]
},
{
"cell_type": "markdown",
"id": "26f7ff06",
"metadata": {},
"source": [
"Then we estimate the model and draw a table:"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "ca7c08c7",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<table class=\"table\" style=\"width: auto !important; margin-left: auto; margin-right: auto;\">\n",
" <thead>\n",
" <tr>\n",
" <th style=\"text-align:left;\"> </th>\n",
" <th style=\"text-align:center;\"> Model 1 </th>\n",
" <th style=\"text-align:center;\"> Model 2 </th>\n",
" </tr>\n",
" </thead>\n",
"<tbody>\n",
" <tr>\n",
" <td style=\"text-align:left;\"> (Intercept) </td>\n",
" <td style=\"text-align:center;\"> 324.082 </td>\n",
" <td style=\"text-align:center;\"> 278.515 </td>\n",
" </tr>\n",
" <tr>\n",
" <td style=\"text-align:left;\"> </td>\n",
" <td style=\"text-align:center;\"> (27.433) </td>\n",
" <td style=\"text-align:center;\"> (55.415) </td>\n",
" </tr>\n",
" <tr>\n",
" <td style=\"text-align:left;\"> mpg </td>\n",
" <td style=\"text-align:center;\"> -8.830 </td>\n",
" <td style=\"text-align:center;\"> -9.985 </td>\n",
" </tr>\n",
" <tr>\n",
" <td style=\"text-align:left;\"> </td>\n",
" <td style=\"text-align:center;\"> (1.310) </td>\n",
" <td style=\"text-align:center;\"> (1.792) </td>\n",
" </tr>\n",
" <tr>\n",
" <td style=\"text-align:left;\"> drat </td>\n",
" <td style=\"text-align:center;\"> </td>\n",
" <td style=\"text-align:center;\"> 19.126 </td>\n",
" </tr>\n",
" <tr>\n",
" <td style=\"text-align:left;\"> </td>\n",
" <td style=\"text-align:center;\"> </td>\n",
" <td style=\"text-align:center;\"> (20.198) </td>\n",
" </tr>\n",
" <tr>\n",
" <td style=\"text-align:left;\"> Num.Obs. </td>\n",
" <td style=\"text-align:center;\"> 32 </td>\n",
" <td style=\"text-align:center;\"> 32 </td>\n",
" </tr>\n",
" <tr>\n",
" <td style=\"text-align:left;\"> R2 </td>\n",
" <td style=\"text-align:center;\"> 0.602 </td>\n",
" <td style=\"text-align:center;\"> 0.614 </td>\n",
" </tr>\n",
" <tr>\n",
" <td style=\"text-align:left;\"> R2 Adj. </td>\n",
" <td style=\"text-align:center;\"> 0.589 </td>\n",
" <td style=\"text-align:center;\"> 0.588 </td>\n",
" </tr>\n",
" <tr>\n",
" <td style=\"text-align:left;\"> AIC </td>\n",
" <td style=\"text-align:center;\"> 336.9 </td>\n",
" <td style=\"text-align:center;\"> 337.9 </td>\n",
" </tr>\n",
" <tr>\n",
" <td style=\"text-align:left;\"> BIC </td>\n",
" <td style=\"text-align:center;\"> 341.3 </td>\n",
" <td style=\"text-align:center;\"> 343.7 </td>\n",
" </tr>\n",
" <tr>\n",
" <td style=\"text-align:left;\"> Log.Lik. </td>\n",
" <td style=\"text-align:center;\"> -165.428 </td>\n",
" <td style=\"text-align:center;\"> -164.940 </td>\n",
" </tr>\n",
" <tr>\n",
" <td style=\"text-align:left;\"> F </td>\n",
" <td style=\"text-align:center;\"> 45.460 </td>\n",
" <td style=\"text-align:center;\"> 23.100 </td>\n",
" </tr>\n",
"</tbody>\n",
"</table>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"mod <- list(\n",
" lm(hp ~ mpg, mtcars),\n",
" lm(hp ~ mpg + drat, mtcars))\n",
"\n",
"\n",
"modelsummary(mod)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "R",
"language": "R",
"name": "ir"
},
"language_info": {
"codemirror_mode": "r",
"file_extension": ".r",
"mimetype": "text/x-r-source",
"name": "R",
"pygments_lexer": "r",
"version": "4.0.4"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment