Search code examples
pythonstatsmodels

statsmodel summary col getting a latex key error?


I've been getting a key error when using the summary_col function.

import statsmodels.api as sm
from statsmodels.iolib.summary2 import summary_col
Y = [0,1,0,0,1,1,1]
X = [5,10,15,20,25,2,7]
logit = sm.Logit(Y,X)
fit = logit.fit()
print(fit.summary())
logit_output = summary_col([fit],stars=True)
print(logit_output.as_latex())

gets me a "Key Error: '\m'". Surprisingly, fit.summary().as_latex() does not return this error.


Solution

  • I was reading a bit in the code and I think you are triggering a bug in the code of statsmodels.

    Here is my wild explanation. The function summary_col returns an object of the Summary class and sets _merge_latex = True. In .as_latex() the next if-caluse is enabled. Here is the link to the source where I found the code below:

    if self._merge_latex:
        # create single tabular object for summary_col
        tab = re.sub(to_replace, r'\\midrule\n', tab)
    

    If you call fit.summary().as_latex() then _merge_latex = False by default. So you don't get into this part and don't get the same error.

    Right now I am not sure what is wrong. I could think of two cases:

    1. re.sub() is only called once and there is a leftover of stuff you want to replace
    2. r'\\midrule\n' is wrong in this line and it shoul be '\\midrule\n' instead.

    To make some progress you have to build a minimal example. To disable the error to see if I am on the right trake, please add

    logit_output._merge_latex = False
    

    before your rerun

    print(logit_output.as_latex())
    

    your code and check if the error changes. This may generate an output you don't want.