StatsReporting / stargazer

Python implementation of the R stargazer multiple regression model creation tool
Other
198 stars 48 forks source link

Output for get_margeff (marginal effects of poisson estimates) #86

Closed B-Benja closed 1 year ago

B-Benja commented 2 years ago

Firs of all, thank you very much for the very useful library. It works very well with OLS outputs, but is it also possible to get marginal effects for a poisson regression as a stargazer (table & latex) output?

Sample code: reg_ols = smf.ols(formula= 'Count_data ~ Variable1Variable2', data=df).fit(disp=0) reg_poisson = smf.poisson(formula= 'Count_data ~ Variable1Variable2', data=df).fit() margins_poisson = reg_poisson.get_margeff(at='overall', method='dydx') reg_results = Stargazer([reg_ols, margins_poisson])

Currently, it results in 'Please use trained OLS models as inputs'. Thanks in advance for any advise and help!

phipz commented 1 year ago

This is certainly not an elegant solution, but a quick fix:

import copy
from statsmodels.formula.api import logit

def ameWrapper(input_model):
    ame_model = copy.deepcopy(input_model)
    ame_model.params[1:] = input_model.get_margeff().margeff
    ame_model.pvalues[1:] = input_model.get_margeff().pvalues
    ame_model.bse[1:] = input_model.get_margeff().margeff_se
    ame_model.params[0] = 0
    ame_model.pvalues[0] = 1
    ame_model.bse[0] = 0
    return ame_model

model1 = logit("dv ~ iv1", data=modeldata).fit()
model2 = logit("dv ~ iv1 + iv2", data=modeldata).fit()

ame_models = [ameWrapper(model1), ameWrapper(model2)]
ame_stargazer = Stargazer(ame_models)

Don't forget to delete the Intecept row in your output.

toobaz commented 1 year ago

@phipz Thanks! I implemented something similar, to be used as follows:

from utils import MarginalEffects

mest = MarginalEffects(est)
ame_stargazer = Stargazer([mest])

This is just a commit as of now, will be included in next release. Feedback welcome!