Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using stargazer to output Latex code from linearmodels model fit

I need linearmodels for 2 way clustering, that's not properly implemented in statsmodels. I was wondering if it was possible to use the stargazer python library with the linearmodels package, rather than with statsmodels. But when I plug the model from linearmodels it throws an error: Please use trained OLS models as inputs

example:

from linearmodels.panel import PanelOLS
import pandas as pd 
df.set_index(['entity', 'time'], inplace = True)
X = df[["Exog1","Exog2","Exog3"]]
y = df["Dep"]
model = PanelOLS(y, X, entity_effects=True, time_effects=True).fit(cov_type='clustered', cluster_entity=True, cluster_time=True)   
print(model)

This outputs model as expected. However when I plug it in int stargazer, it throws the following error

stargazer = Stargazer([model])

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-149-75027b8621a2> in <module>
----> 1 stargazer = Stargazer([model])

~\AppData\Local\Continuum\anaconda3\lib\site-packages\stargazer\stargazer.py in __init__(self, models)
     29         self.models = models
     30         self.num_models = len(models)
---> 31         self.extract_data()
     32         self.reset_params()
     33 

~\AppData\Local\Continuum\anaconda3\lib\site-packages\stargazer\stargazer.py in extract_data(self)
     91         be modified by any rendering parameters.
     92         """
---> 93         self.validate_input()
     94         self.model_data = []
     95         for m in self.models:

~\AppData\Local\Continuum\anaconda3\lib\site-packages\stargazer\stargazer.py in validate_input(self)
     43         for m in self.models:
     44             if not isinstance(m, RegressionResultsWrapper):
---> 45                 raise ValueError('Please use trained OLS models as inputs')
     46             targets.append(m.model.endog_names)
     47 

ValueError: Please use trained OLS models as inputs

I understand that stargazer might not support linearmodels, but perhaps there is a workaround, that will allow me to have linearmodels model output in Latex?

like image 234
LORDER Avatar asked Mar 10 '20 14:03

LORDER


1 Answers

As of November 2021, stargazer still offers no support for linearmodels.

However, you can output your summary as latex with the following code:

# Declare model
model = PanelOLS(y, X, entity_effects=True, time_effect=True)

# Fit model
res = model.fit(cov_type='clustered', cluster_entity=True, cluster_time=True)

# Print as latex
print(res.summary.as_latex())
like image 134
Arturo Sbr Avatar answered Oct 02 '22 05:10

Arturo Sbr