Search code examples
pythonpandasdataframestatsmodels

Extract values from WaldTestResults pandas DataFrame


After applying statsmodels.api.Logit I get the output of the logistic model.

LogModel = sm.Logit(y_01, Xdata_WithDummies).fit()        
walds = LogModel.wald_test_terms()

Among the computed statistics there is a Wald Test table given as a pandas Dataframe (<class 'statsmodels.stats.contrast.WaldTestResults'>), that I can print and see it. However, I need to extract specific elements of this table for further analysis. According to the help page the Wald table is a pandas DataFrame.

I did not manage to transform this table into a list (list(wald), TypeError: 'WaldTestResults' object is not iterable) nor to see its values by wald.values (error: AttributeError: 'WaldTestResults' object has no attribute 'values') nor to extract a specific column (TypeError: 'WaldTestResults' object is not subscriptable), or even to extract specific values out of the table following relevant advice (e.g. here).

How could I read specific values out of this table? Or (equivalently) how could I transform this table into a list?


Solution

  • To convert it to a pandas.DataFrame you need to use the .summary_frame() method.

    Sample Data

    import statsmodels.api as sm 
    import numpy as np
    
    np.random.seed(123)
    y = np.random.randint(0, 2, 50)
    x = np.random.randint(0, 2, (50, 3))
    

    Code

    LogModel = sm.Logit(y, x).fit()        
    walds = LogModel.wald_test_terms()
    
    df = walds.summary_frame()
    #    chi2  P>chi2  df constraint
    #x1   0.0     1.0              1
    #x2   0.0     1.0              1
    #x3   0.0     1.0              1
    
    type(df)
    #pandas.core.frame.DataFrame