Display regression results from statsmodels in Dash

Hello, I am just loving plotly/dash. I was just wondering how I can just show a raw table as I get it in Python as output. Below an example. My wish would be just to show the print(res.summary()).

# Load modules and data
In [1]: import numpy as np

In [2]: import statsmodels.api as sm

In [3]: spector_data = sm.datasets.spector.load(as_pandas=False)

In [4]: spector_data.exog = sm.add_constant(spector_data.exog, prepend=False)

# Fit and summarize OLS model
In [5]: mod = sm.OLS(spector_data.endog, spector_data.exog)

In [6]: res = mod.fit()

In [7]: print(res.summary())
                            OLS Regression Results                            
==============================================================================
Dep. Variable:                      y   R-squared:                       0.416
Model:                            OLS   Adj. R-squared:                  0.353
Method:                 Least Squares   F-statistic:                     6.646
Date:                Fri, 21 Feb 2020   Prob (F-statistic):            0.00157
Time:                        13:59:19   Log-Likelihood:                -12.978
No. Observations:                  32   AIC:                             33.96
Df Residuals:                      28   BIC:                             39.82
Df Model:                           3                                         
Covariance Type:            nonrobust                                         
==============================================================================
                 coef    std err          t      P>|t|      [0.025      0.975]
------------------------------------------------------------------------------
x1             0.4639      0.162      2.864      0.008       0.132       0.796
x2             0.0105      0.019      0.539      0.594      -0.029       0.050
x3             0.3786      0.139      2.720      0.011       0.093       0.664
const         -1.4980      0.524     -2.859      0.008      -2.571      -0.425
==============================================================================
Omnibus:                        0.176   Durbin-Watson:                   2.346
Prob(Omnibus):                  0.916   Jarque-Bera (JB):                0.167
Skew:                           0.141   Prob(JB):                        0.920
Kurtosis:                       2.786   Cond. No.                         176.
==============================================================================

Warnings:
[1] Standard Errors assume that the covariance matrix of the errors is correctly specified.

Hi @Rumiko welcome to the forum! You can get the raw string produced by printing the model by calling model.__repr__(), and then you can put this string in a html.P component (after doing some search and replace to make the Python special characters become their equivalent ones, for example \n -> <br>).

For example

import plotly.express as px

df = px.data.tips()
fig = px.scatter(df, x="total_bill", y="tip", facet_col="smoker", color="sex", trendline="ols")

results = px.get_trendline_results(fig)

model = results.query("sex == 'Male' and smoker == 'Yes'").px_fit_results.iloc[0].summary()
model.__repr__()

Output

'<class \'statsmodels.iolib.summary.Summary\'>\n"""\n                            OLS Regression Results                            \n==============================================================================\nDep. Variable:                      y   R-squared:                       0.232\nModel:                            OLS   Adj. R-squared:                  0.219\nMethod:                 Least Squares   F-statistic:                     17.56\nDate:                Fri, 03 Apr 2020   Prob (F-statistic):           9.61e-05\nTime:                        23:40:04   Log-Likelihood:                -101.03\nNo. Observations:                  60   AIC:                             206.1\nDf Residuals:                      58   BIC:                             210.2\nDf Model:                           1                                         \nCovariance Type:            nonrobust                                         \n==============================================================================\n                 coef    std err          t      P>|t|      [0.025      0.975]\n------------------------------------------------------------------------------\nconst          1.4253      0.424      3.361      0.001       0.576       2.274\nx1             0.0730      0.017      4.190      0.000       0.038       0.108\n==============================================================================\nOmnibus:                       21.841   Durbin-Watson:                   1.383\nProb(Omnibus):                  0.000   Jarque-Bera (JB):               33.031\nSkew:                           1.315   Prob(JB):                     6.72e-08\nKurtosis:                       5.510   Cond. No.                         60.4\n==============================================================================\n\nWarnings:\n[1] Standard Errors assume that the covariance matrix of the errors is correctly specified.\n"""'
1 Like

Thank you very much Emmanuelle, I will explore this a bit today!

Hi

I am new to plotly - apologies if this question is not correctly phrased.

I am trying to output to plotly the regression results table @Rumiko generated above from statsmodels as a plotly table.

I have generated the raw string from model. __ repr __ () and replaced the \n with < br >.

@Emmanuelle, could you explain how to incorporate this into the html.P component and then surface those results to plotly?

Thanks

Hello

I don’t know if an answer has been giving for this question, but i manage this problem using html.Pre.

html.Pre(children=res.summary().as_text(), style={‘whiteSpace’: ‘pre-wrap’,“background-color”:“lightgray”})