stargazer icon indicating copy to clipboard operation
stargazer copied to clipboard

Includes option for showing Pseudo R2 Adjusted (used on Logit)

Open cnavarreteliz opened this issue 4 years ago • 3 comments

Currently, when you want to compare discrete models such as Logit and Probit, no values are shown for R² / R² Adjusted, because statsmodels for these models returns Pseudo-R² Adjusted. This PR includes the ability to display that coefficient.

cnavarreteliz avatar Mar 28 '21 20:03 cnavarreteliz

OK, only problem: the (empty) row gets generated even when only OLS is used. This by the way is not just a problem of your PR - the same applies to standard R² being generated for Logit (although maybe it's less annoying).

You should verify that at least one of the models has a non-null md['p_r2'], and if none does, not print the line.

Two options:

  • you do it for Pseudo-R² (either do the check directly inside generate_p_r2, or relying on a new Renderer method), we later generalize to other parameters
  • we first solve the fix for R² and adjusted R², you rebase this afterwards by reusing the same mechanism

I can do the general fix, just let me know how to prefer to proceed.

toobaz avatar May 03 '21 14:05 toobaz

I prefer the second option. For example, this is a logit panel that I'm testing now, and I would like to remove those R2/Adjusted R2 in favor of Pseudo R2 image

cnavarreteliz avatar May 04 '21 07:05 cnavarreteliz

I prefer the second option. For example, this is a logit panel that I'm testing now, and I would like to remove those R2/Adjusted R2 in favor of Pseudo R2

OK, commit https://github.com/mwburke/stargazer/commit/eb8b4fb48e9ba1e0f9e714d50c6cc23f2fb050bb should accomplish this - in addition to reducing code duplication.

You can use the same scheme as R2/Adjusted R2 for Pseudo R2.

toobaz avatar May 14 '21 00:05 toobaz

@cnavarreteliz I actually did it today in https://github.com/StatsReporting/stargazer/commit/8bdc068cd6c2ffed6fa4692fbbcf5616108c2ed9

I would have rather pinged you here again... if I hadn't completely forgot about this PR, sorry.

toobaz avatar Jul 29 '23 15:07 toobaz

How do I get Stargazer to actually show the PseudoR2 for a Statsmodels Logit model? By default (v.0.0.6) it does not include it in the table.

model = logit("dv ~ iv", data=data).fit()
(Stargazer([model]).render_html())

phipz avatar Aug 06 '23 09:08 phipz

How do I get Stargazer to actually show the PseudoR2 for a Statsmodels Logit model? By default (v.0.0.6) it does not include it in the table.

The following code results in a table that correctly features the pseudo-R² for the logit model:

import pandas as pd
from sklearn import datasets
from statsmodels.api import OLS, Logit
from stargazer.stargazer import Stargazer

diabetes = datasets.load_diabetes()

df = pd.DataFrame(diabetes.data, columns=diabetes['feature_names'])
df['target'] = (diabetes.target > 100).astype(int)

ols = OLS.from_formula('target ~ age + sex + bmi', data=df).fit()
lg = Logit.from_formula('target ~ age + sex + bmi', data=df).fit()

table = Stargazer([ols, lg])

@phipz if it doesn't in your code, please provide a complete copypastable example and open a new issue, thanks.

toobaz avatar Aug 07 '23 13:08 toobaz