From cf5a0294d3104856d2cc8467ed6bd9f2df5a1b89 Mon Sep 17 00:00:00 2001 From: Dirk Sliwka <49401450+dsliwka@users.noreply.github.com> Date: Tue, 9 Jan 2024 18:54:21 +0100 Subject: [PATCH 1/3] Currently, the coefficients are alphatically ordered (when not actively reordered). This change preserves the order of the coefficients as passed over from the models. --- stargazer/stargazer.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/stargazer/stargazer.py b/stargazer/stargazer.py index 0bdc65e..359c94d 100644 --- a/stargazer/stargazer.py +++ b/stargazer/stargazer.py @@ -151,7 +151,12 @@ def extract_data(self): covs = [] for md in self.model_data: covs = covs + list(md['cov_names']) - self.cov_names = sorted(set(covs)) + # Drop duplicates but keep order of first appearance + self.cov_names = [] + [self.cov_names.append(x) for x in covs if x not in self.cov_names] + # Move constant/Intercept to the end of the list + const = ['Intercept', 'const'] + self.cov_names = [x for x in self.cov_names if x not in const] + [x for x in self.cov_names if x in const] self.validate_input() From 798f7019753ce459ff8f871d3c67e5afa4682116 Mon Sep 17 00:00:00 2001 From: Dirk Sliwka <49401450+dsliwka@users.noreply.github.com> Date: Tue, 9 Jan 2024 18:56:56 +0100 Subject: [PATCH 2/3] This extension of the rename_covariate method allows to automatically also rename interaction terms based on the renamed variables that are interacted. --- stargazer/stargazer.py | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/stargazer/stargazer.py b/stargazer/stargazer.py index 359c94d..06d9e20 100644 --- a/stargazer/stargazer.py +++ b/stargazer/stargazer.py @@ -255,8 +255,22 @@ def covariate_order(self, cov_names, restrict=True): self.original_cov_names = self.cov_names self.cov_names = cov_names - def rename_covariates(self, cov_map): + def rename_covariates(self, cov_map, + formula=False, + interaction = Label({'html' : ' × ', 'LaTeX' : ' $\\times$ '})): + + def _formatInteraction(cov, fmt): + with Label.context(fmt): + return interaction.join([str(cov_map.get(k, k)) for k in cov.split(":")]) + if hasattr(cov_map, "get"): + if formula: + # Rename interaction terms generated from formula: add respective items to the cov_map dictionary + # that combine the names of the interacted variables & link with interaction symbol + for cov in self.cov_names: + if ":" in cov: + cov_map[cov]= Label({'html' : _formatInteraction(cov, 'html'), + 'LaTeX' : _formatInteraction(cov, 'LaTeX')}) self.cov_map = lambda k : cov_map.get(k, k) elif callable(cov_map): self.cov_map = cov_map From 8b2052ae599c62f564aad5e0de40e0446a1aa54a Mon Sep 17 00:00:00 2001 From: Dirk Sliwka <49401450+dsliwka@users.noreply.github.com> Date: Tue, 9 Jan 2024 19:00:48 +0100 Subject: [PATCH 3/3] Added examples for renaming with interaction terms --- examples.ipynb | 256 ++++++++++++++++++++++++++++++------------------- 1 file changed, 158 insertions(+), 98 deletions(-) diff --git a/examples.ipynb b/examples.ipynb index 6cb7861..8f956ef 100644 --- a/examples.ipynb +++ b/examples.ipynb @@ -46,7 +46,7 @@ }, { "cell_type": "code", - "execution_count": 3, + "execution_count": 4, "metadata": {}, "outputs": [ { @@ -56,18 +56,18 @@ "Dependent variable: target(1)(2)\n", "\n", "\n", - "ABP416.673***397.581***\n", - "(69.495)(70.870)\n", "Age37.24124.703\n", "(64.117)(65.411)\n", + "Sex-106.576*-82.862\n", + "(62.125)(64.851)\n", "BMI787.182***789.744***\n", "(65.424)(66.887)\n", + "ABP416.673***397.581***\n", + "(69.495)(70.870)\n", "S1197.848\n", "(143.812)\n", "S2-169.243\n", "(142.744)\n", - "Sex-106.576*-82.862\n", - "(62.125)(64.851)\n", "const152.133***152.133***\n", "(2.853)(2.853)\n", "\n", @@ -76,10 +76,10 @@ "Note:*p<0.1; **p<0.05; ***p<0.01" ], "text/plain": [ - "" + "" ] }, - "execution_count": 3, + "execution_count": 4, "metadata": {}, "output_type": "execute_result" } @@ -94,7 +94,7 @@ }, { "cell_type": "code", - "execution_count": 4, + "execution_count": 5, "metadata": {}, "outputs": [ { @@ -109,18 +109,18 @@ "\\cr \\cline{2-3}\n", "\\\\[-1.8ex] & (1) & (2) \\\\\n", "\\hline \\\\[-1.8ex]\n", - " ABP & 416.673$^{***}$ & 397.581$^{***}$ \\\\\n", - "& (69.495) & (70.870) \\\\\n", " Age & 37.241$^{}$ & 24.703$^{}$ \\\\\n", "& (64.117) & (65.411) \\\\\n", + " Sex & -106.576$^{*}$ & -82.862$^{}$ \\\\\n", + "& (62.125) & (64.851) \\\\\n", " BMI & 787.182$^{***}$ & 789.744$^{***}$ \\\\\n", "& (65.424) & (66.887) \\\\\n", + " ABP & 416.673$^{***}$ & 397.581$^{***}$ \\\\\n", + "& (69.495) & (70.870) \\\\\n", " S1 & & 197.848$^{}$ \\\\\n", "& & (143.812) \\\\\n", " S2 & & -169.243$^{}$ \\\\\n", "& & (142.744) \\\\\n", - " Sex & -106.576$^{*}$ & -82.862$^{}$ \\\\\n", - "& (62.125) & (64.851) \\\\\n", " const & 152.133$^{***}$ & 152.133$^{***}$ \\\\\n", "& (2.853) & (2.853) \\\\\n", "\\hline \\\\[-1.8ex]\n", @@ -150,7 +150,7 @@ }, { "cell_type": "code", - "execution_count": 5, + "execution_count": 6, "metadata": {}, "outputs": [ { @@ -160,18 +160,18 @@ "Dependent variable: target(1)(2)\n", "\n", "\n", - "ABP416.673***397.581***\n", - "(69.495)(70.870)\n", "Age37.24124.703\n", "(64.117)(65.411)\n", + "Sex-106.576*-82.862\n", + "(62.125)(64.851)\n", "BMI787.182***789.744***\n", "(65.424)(66.887)\n", + "ABP416.673***397.581***\n", + "(69.495)(70.870)\n", "S1197.848\n", "(143.812)\n", "S2-169.243\n", "(142.744)\n", - "Sex-106.576*-82.862\n", - "(62.125)(64.851)\n", "const152.133***152.133***\n", "(2.853)(2.853)\n", "\n", @@ -180,10 +180,10 @@ "Note:*p<0.1; **p<0.05; ***p<0.01" ], "text/plain": [ - "" + "" ] }, - "execution_count": 5, + "execution_count": 6, "metadata": {}, "output_type": "execute_result" } @@ -202,7 +202,7 @@ }, { "cell_type": "code", - "execution_count": 6, + "execution_count": 7, "metadata": {}, "outputs": [ { @@ -212,18 +212,18 @@ "Dependent variable: targetModel 1Model 2(1)(2)\n", "\n", "\n", - "ABP416.673***397.581***\n", - "(69.495)(70.870)\n", "Age37.24124.703\n", "(64.117)(65.411)\n", + "Sex-106.576*-82.862\n", + "(62.125)(64.851)\n", "BMI787.182***789.744***\n", "(65.424)(66.887)\n", + "ABP416.673***397.581***\n", + "(69.495)(70.870)\n", "S1197.848\n", "(143.812)\n", "S2-169.243\n", "(142.744)\n", - "Sex-106.576*-82.862\n", - "(62.125)(64.851)\n", "const152.133***152.133***\n", "(2.853)(2.853)\n", "\n", @@ -232,10 +232,10 @@ "Note:*p<0.1; **p<0.05; ***p<0.01" ], "text/plain": [ - "" + "" ] }, - "execution_count": 6, + "execution_count": 7, "metadata": {}, "output_type": "execute_result" } @@ -247,7 +247,7 @@ }, { "cell_type": "code", - "execution_count": 7, + "execution_count": 8, "metadata": {}, "outputs": [ { @@ -257,18 +257,18 @@ "Dependent variable: targetTest model name(1)(2)\n", "\n", "\n", - "ABP416.673***397.581***\n", - "(69.495)(70.870)\n", "Age37.24124.703\n", "(64.117)(65.411)\n", + "Sex-106.576*-82.862\n", + "(62.125)(64.851)\n", "BMI787.182***789.744***\n", "(65.424)(66.887)\n", + "ABP416.673***397.581***\n", + "(69.495)(70.870)\n", "S1197.848\n", "(143.812)\n", "S2-169.243\n", "(142.744)\n", - "Sex-106.576*-82.862\n", - "(62.125)(64.851)\n", "const152.133***152.133***\n", "(2.853)(2.853)\n", "\n", @@ -277,10 +277,10 @@ "Note:*p<0.1; **p<0.05; ***p<0.01" ], "text/plain": [ - "" + "" ] }, - "execution_count": 7, + "execution_count": 8, "metadata": {}, "output_type": "execute_result" } @@ -299,7 +299,7 @@ }, { "cell_type": "code", - "execution_count": 8, + "execution_count": 9, "metadata": {}, "outputs": [ { @@ -309,18 +309,18 @@ "Dependent variable: targetTest model name\n", "\n", "\n", - "ABP416.673***397.581***\n", - "(69.495)(70.870)\n", "Age37.24124.703\n", "(64.117)(65.411)\n", + "Sex-106.576*-82.862\n", + "(62.125)(64.851)\n", "BMI787.182***789.744***\n", "(65.424)(66.887)\n", + "ABP416.673***397.581***\n", + "(69.495)(70.870)\n", "S1197.848\n", "(143.812)\n", "S2-169.243\n", "(142.744)\n", - "Sex-106.576*-82.862\n", - "(62.125)(64.851)\n", "const152.133***152.133***\n", "(2.853)(2.853)\n", "\n", @@ -329,10 +329,10 @@ "Note:*p<0.1; **p<0.05; ***p<0.01" ], "text/plain": [ - "" + "" ] }, - "execution_count": 8, + "execution_count": 9, "metadata": {}, "output_type": "execute_result" } @@ -351,7 +351,7 @@ }, { "cell_type": "code", - "execution_count": 9, + "execution_count": 10, "metadata": {}, "outputs": [ { @@ -361,18 +361,18 @@ "Dependent variable: targetTest model name\n", "\n", "\n", - "ABP416.67***397.58***\n", - "(69.49)(70.87)\n", "Age37.2424.70\n", "(64.12)(65.41)\n", + "Sex-106.58*-82.86\n", + "(62.13)(64.85)\n", "BMI787.18***789.74***\n", "(65.42)(66.89)\n", + "ABP416.67***397.58***\n", + "(69.49)(70.87)\n", "S1197.85\n", "(143.81)\n", "S2-169.24\n", "(142.74)\n", - "Sex-106.58*-82.86\n", - "(62.13)(64.85)\n", "const152.13***152.13***\n", "(2.85)(2.85)\n", "\n", @@ -381,10 +381,10 @@ "Note:*p<0.1; **p<0.05; ***p<0.01" ], "text/plain": [ - "" + "" ] }, - "execution_count": 9, + "execution_count": 10, "metadata": {}, "output_type": "execute_result" } @@ -403,7 +403,7 @@ }, { "cell_type": "code", - "execution_count": 10, + "execution_count": 11, "metadata": {}, "outputs": [ { @@ -413,18 +413,18 @@ "Dependent variable: targetTest model name\n", "\n", "\n", - "ABP416.67***397.58***\n", - "(280.09 , 553.26)(258.29 , 536.87)\n", "Age37.2424.70\n", "(-88.78 , 163.26)(-103.86 , 153.26)\n", + "Sex-106.58*-82.86\n", + "(-228.68 , 15.52)(-210.32 , 44.60)\n", "BMI787.18***789.74***\n", "(658.60 , 915.77)(658.28 , 921.20)\n", + "ABP416.67***397.58***\n", + "(280.09 , 553.26)(258.29 , 536.87)\n", "S1197.85\n", "(-84.81 , 480.50)\n", "S2-169.24\n", "(-449.80 , 111.31)\n", - "Sex-106.58*-82.86\n", - "(-228.68 , 15.52)(-210.32 , 44.60)\n", "const152.13***152.13***\n", "(146.53 , 157.74)(146.53 , 157.74)\n", "\n", @@ -433,10 +433,10 @@ "Note:*p<0.1; **p<0.05; ***p<0.01" ], "text/plain": [ - "" + "" ] }, - "execution_count": 10, + "execution_count": 11, "metadata": {}, "output_type": "execute_result" } @@ -455,7 +455,7 @@ }, { "cell_type": "code", - "execution_count": 11, + "execution_count": 12, "metadata": {}, "outputs": [ { @@ -479,10 +479,10 @@ "Note:*p<0.1; **p<0.05; ***p<0.01" ], "text/plain": [ - "" + "" ] }, - "execution_count": 11, + "execution_count": 12, "metadata": {}, "output_type": "execute_result" } @@ -501,7 +501,7 @@ }, { "cell_type": "code", - "execution_count": 12, + "execution_count": 13, "metadata": {}, "outputs": [ { @@ -525,10 +525,10 @@ "Note:*p<0.1; **p<0.05; ***p<0.01" ], "text/plain": [ - "" + "" ] }, - "execution_count": 12, + "execution_count": 13, "metadata": {}, "output_type": "execute_result" } @@ -547,7 +547,7 @@ }, { "cell_type": "code", - "execution_count": 13, + "execution_count": 14, "metadata": {}, "outputs": [ { @@ -571,10 +571,10 @@ "Note:*p<0.1; **p<0.05; ***p<0.01" ], "text/plain": [ - "" + "" ] }, - "execution_count": 13, + "execution_count": 14, "metadata": {}, "output_type": "execute_result" } @@ -594,7 +594,7 @@ }, { "cell_type": "code", - "execution_count": 14, + "execution_count": 15, "metadata": {}, "outputs": [], "source": [ @@ -603,7 +603,7 @@ }, { "cell_type": "code", - "execution_count": 15, + "execution_count": 16, "metadata": {}, "outputs": [ { @@ -627,10 +627,10 @@ "Note:*p<0.1; **p<0.05; ***p<0.01" ], "text/plain": [ - "" + "" ] }, - "execution_count": 15, + "execution_count": 16, "metadata": {}, "output_type": "execute_result" } @@ -644,6 +644,66 @@ "stargazer" ] }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "... also rename interaction terms automatically when using formulas" + ] + }, + { + "cell_type": "code", + "execution_count": 35, + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "
Dependent variable: target
(1)(2)
Age52.45256.917
(64.228)(63.702)
Body Mass Index813.161***818.959***
(66.233)(66.073)
Age × Body Mass Index2809.479**2486.949*
(1291.944)(1294.873)
Female-102.705*-120.044*
(61.887)(62.208)
ABP413.120***417.803***
(69.219)(68.816)
Age × Female3384.230***
(1294.381)
Body Mass Index × Female2280.831*
(1303.691)
Age × Body Mass Index × Female23021.023
(27374.170)
Intercept150.957***149.454***
(2.892)(2.905)
Observations442442
R20.4070.422
Adjusted R20.4000.411
Residual Std. Error59.721 (df=436)59.147 (df=433)
F Statistic59.774*** (df=5; 436)39.525*** (df=8; 433)
Note:*p<0.1; **p<0.05; ***p<0.01
" + ], + "text/plain": [ + "" + ] + }, + "execution_count": 35, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "estint1=sm.OLS.from_formula('target ~ Age * BMI + Sex + ABP', data=df).fit()\n", + "estint2=sm.OLS.from_formula('target ~ Age * BMI * Sex + ABP', data=df).fit()\n", + "tabInt= Stargazer([estint1,estint2])\n", + "tabInt.rename_covariates({\"BMI\":\"Body Mass Index\",\n", + " 'Sex':\"Female\"}, formula=True)\n", + "tabInt" + ] + }, { "cell_type": "markdown", "metadata": {}, @@ -653,7 +713,7 @@ }, { "cell_type": "code", - "execution_count": 16, + "execution_count": 17, "metadata": {}, "outputs": [ { @@ -677,10 +737,10 @@ "Note:*p<0.1; **p<0.05; ***p<0.01" ], "text/plain": [ - "" + "" ] }, - "execution_count": 16, + "execution_count": 17, "metadata": {}, "output_type": "execute_result" } @@ -699,7 +759,7 @@ }, { "cell_type": "code", - "execution_count": 17, + "execution_count": 18, "metadata": { "pycharm": { "name": "#%%\n" @@ -727,10 +787,10 @@ "Note:*p<0.1; **p<0.05; ***p<0.01First noteSecond note" ], "text/plain": [ - "" + "" ] }, - "execution_count": 17, + "execution_count": 18, "metadata": {}, "output_type": "execute_result" } @@ -749,7 +809,7 @@ }, { "cell_type": "code", - "execution_count": 18, + "execution_count": 19, "metadata": {}, "outputs": [ { @@ -773,10 +833,10 @@ "Largest R2YesYesNote:*p<0.1; **p<0.05; ***p<0.01First noteSecond note" ], "text/plain": [ - "" + "" ] }, - "execution_count": 18, + "execution_count": 19, "metadata": {}, "output_type": "execute_result" } @@ -803,7 +863,7 @@ }, { "cell_type": "code", - "execution_count": 19, + "execution_count": 20, "metadata": {}, "outputs": [ { @@ -827,10 +887,10 @@ "Largest R2YesYesNote:*p<0.1; **p<0.07; ***p<0.05First noteSecond note" ], "text/plain": [ - "" + "" ] }, - "execution_count": 19, + "execution_count": 20, "metadata": {}, "output_type": "execute_result" } @@ -849,7 +909,7 @@ }, { "cell_type": "code", - "execution_count": 20, + "execution_count": 21, "metadata": {}, "outputs": [ { @@ -873,10 +933,10 @@ "Largest R2YesYesNote:First noteSecond note" ], "text/plain": [ - "" + "" ] }, - "execution_count": 20, + "execution_count": 21, "metadata": {}, "output_type": "execute_result" } @@ -895,7 +955,7 @@ }, { "cell_type": "code", - "execution_count": 21, + "execution_count": 22, "metadata": {}, "outputs": [ { @@ -919,10 +979,10 @@ "Largest R2YesYesNote:First noteSecond note" ], "text/plain": [ - "" + "" ] }, - "execution_count": 21, + "execution_count": 22, "metadata": {}, "output_type": "execute_result" } @@ -948,7 +1008,7 @@ }, { "cell_type": "code", - "execution_count": 22, + "execution_count": 23, "metadata": {}, "outputs": [ { @@ -980,10 +1040,10 @@ "Note:*p<0.1; **p<0.05; ***p<0.01" ], "text/plain": [ - "" + "" ] }, - "execution_count": 22, + "execution_count": 23, "metadata": {}, "output_type": "execute_result" } @@ -1030,27 +1090,27 @@ "OLSOLSIV(1)(2)(3)\n", "\n", "\n", - "Intercept5.861***5.861***6.771***\n", - "(0.157)(0.157)(0.257)\n", + "totchr0.440***0.440***0.450***\n", + "(0.009)(0.009)(0.010)\n", + "female0.058**0.058**\n", + "(0.025)(0.025)\n", "age-0.004*-0.004*-0.013***\n", "(0.002)(0.002)(0.003)\n", + "linc0.0100.0100.087***\n", + "(0.014)(0.014)(0.023)\n", "blhisp-0.151***-0.151***-0.217***\n", "(0.034)(0.034)(0.039)\n", - "female0.058**0.058**\n", - "(0.025)(0.025)\n", "hi_empunion0.074***0.074***-0.889***\n", "(0.026)(0.026)(0.213)\n", - "linc0.0100.0100.087***\n", - "(0.014)(0.014)(0.023)\n", - "totchr0.440***0.440***0.450***\n", - "(0.009)(0.009)(0.010)\n", + "Intercept5.861***5.861***6.771***\n", + "(0.157)(0.157)(0.257)\n", "\n", "\n", "Observations100891008910089R20.1770.1770.066Residual Std. Error0.573 (df=10082)0.573 (df=10082)0.350 (df=10083)F Statistic2262.644*** (df=7; 10082)2262.644*** (df=7; 10082)2004.325*** (df=6; 10083)\n", "Note:*p<0.1; **p<0.05; ***p<0.01" ], "text/plain": [ - "" + "" ] }, "execution_count": 24, @@ -1101,7 +1161,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.11.2" + "version": "3.9.13" } }, "nbformat": 4,