estout
  1. utils
  • estout
  • core
  • utils
  • statsmodels_results
  • linearmodels_results

On this page

  • rgetattr
  • rsetattr
  • default_formats
  • get_stars
  • model_groups
  • tex_table_env
  • df_to_tex
  • make_pdf_from_tex
  • open_pdf_file
  • Report an issue

utils

Helper functions for estout package

source

rgetattr

 rgetattr (obj, attr, *args)

Recursive getattr (for nested attributes).


source

rsetattr

 rsetattr (obj, attr, val)

Recursive setattr (for nested attributes).


source

default_formats

 default_formats ()

Default output formats for some very common statistics.

default_formats()
{'params': '{:.2f}',
 'tstats': '{:.2f}',
 'pvalues': '{:.3f}',
 'se': '{:.2f}',
 'r2': '{:.3f}',
 'nobs': '{:.0f}'}

source

get_stars

 get_stars (pvalues:pandas.core.series.Series, stars:dict={0.1: '*', 0.05:
            '**', 0.01: '***'})

For each pvalue, check the lowest key in stars for which the pvalue is smaller than that key, and return the corresponding nr of stars.

Type Default Details
pvalues Series This is compared to key of stars param to determine how many stars should be added
stars dict {0.1: ‘’, 0.05: ’’, 0.01: ’’} Todo: default values to the left are star symbols that are not rendered correctly in markdown
Returns Series
get_stars(pvalues= pd.Series([0.5, .03, 0.002], index=['x1','x2','x3']))
x1       
x2     **
x3    ***
dtype: object
get_stars(pvalues= pd.Series([0.5, .03, 0.002], index=['x1','x2','x3']), 
          stars={0.4: '**', 0.9: '***'})
x1    ***
x2     **
x3     **
dtype: object

source

model_groups

 model_groups (column_group_names:Dict[str,List[int]],
               add_clines:bool=True)

Returns LaTex code needed to add at the top of the table in order to give names to groups of columns in the table.

Type Default Details
column_group_names Dict Keys are group titles, values are lists of column indices included in each group
add_clines bool True If True, adds lines below group names
Returns str
print(model_groups(column_group_names={'Group 1':[0,1], 'Group 2':[2,3]}))
& \multicolumn{2}{c}{Group 1} & \multicolumn{2}{c}{Group 2}  \\ 
\cline{1-2} \cline{3-4}  

source

tex_table_env

 tex_table_env (nr_columns:int,
                env:Literal['tabularx','tabular*']='tabular*')

Creates LaTex code to add at the top of the table to create the correct tabular environment.

Type Default Details
nr_columns int Number of columns in the table
env Literal tabular* Latex tabular environment specification
Returns Tuple
header,footer = tex_table_env(nr_columns=4, env='tabularx')
print(header)
print(footer)
\begin{tabularx}{\textwidth}{@{}l *{4}{>{\centering\arraybackslash}X}@{}}
\end{tabularx}
header,footer = tex_table_env(nr_columns=4, env='tabular*')
print(header)
print(footer)
\begin{tabular*}{\textwidth}{@{\extracolsep{\fill}}l*{4}{c}}
\end{tabular*}

source

df_to_tex

 df_to_tex (df:pandas.core.frame.DataFrame, panel_title:str='',
            palign:Literal['l','r','c']='l',
            col_groups:Dict[str,List[int]]=None,
            col_names:Union[List[str],bool]=True, hlines:List[int]=[],
            tabular_env:str='tabular*')

Creates LaTeX-formatted table from DataFrame.

Type Default Details
df DataFrame If this has a MultiIndex, only the first level is kept
panel_title str
palign Literal l Alignment of panel title
col_groups Dict None Keys are group names; values are lists of consecutive indices of columns in the group
col_names Union True If False, none; if True, use df column names; if list, gives custom column names
hlines List [] Row indices under which to place hline (use 0 for topline). adds under each line
tabular_env str tabular* LaTex tabular environment
Returns str

Panel title (if any) will appear first, then column group names(if any), then column names (if any), then the body of the table.

df = pd.DataFrame(np.random.rand(5,4), index=['Var1','','Var2','','R2'], columns=list('ABCD'))
df
A B C D
Var1 0.153388 0.740063 0.318769 0.741419
0.981220 0.349820 0.750357 0.868395
Var2 0.987855 0.133089 0.928428 0.600271
0.120149 0.177721 0.108870 0.844516
R2 0.696665 0.187199 0.966317 0.972379
tex_string = df_to_tex(df, panel_title='Dependent variable is Y',
                        col_groups={'Group 1':[1,2], 'Group 2': [3,4]},
                        hlines=[0,2,7])
print(tex_string)
\begin{tabular*}{\textwidth}{@{\extracolsep{\fill}}l*{4}{c}} 
 \hline \noalign{\smallskip} 
\multicolumn{5}{@{} l}{Dependent variable is Y} \\ 
& \multicolumn{2}{c}{Group 1} & \multicolumn{2}{c}{Group 2}  \\ 
 \hline \noalign{\smallskip} 
\cline{2-3} \cline{4-5}  
 & A & B & C & D \\ 
Var1 & 0.15338774256333254 & 0.7400625640619886 & 0.31876894791132926 & 0.741418501204769 \\ 
 & 0.981219771218718 & 0.34981974576456365 & 0.7503573114766038 & 0.8683948957807631 \\ 
Var2 & 0.9878546825283399 & 0.1330892261743063 & 0.9284283603986899 & 0.6002709049430831 \\ 
 \hline \noalign{\smallskip} 
 & 0.12014853367450873 & 0.17772097286209887 & 0.10887029741896503 & 0.8445164146587155 \\ 
R2 & 0.6966651718648458 & 0.18719853238388184 & 0.9663168811371031 & 0.9723793915445124 \\ 
\end{tabular*}

source

make_pdf_from_tex

 make_pdf_from_tex (tex_file_path:pathlib.Path|str)

Output PDF is created in the same folder as source tex file. Requires TexLive and its pdflatex utility


source

open_pdf_file

 open_pdf_file (file_path)
  • Report an issue