Fix OLS Summary and lint

This commit is contained in:
Igor Radovanovic
2023-10-10 16:51:25 +02:00
parent 7ab38168a8
commit 7eca47eb33
2 changed files with 45 additions and 15 deletions

View File

@@ -230,7 +230,6 @@ def test_econometrics_coint(params, data_type):
"data": "",
"y_column": "volume",
"x_column": "close",
"lag": "",
},
"stocks",
),
@@ -239,7 +238,6 @@ def test_econometrics_coint(params, data_type):
"data": "",
"y_column": "volume",
"x_column": "close",
"lag": "2",
},
"crypto",
),

View File

@@ -1,3 +1,4 @@
import re
from itertools import combinations
from typing import Dict, List, Literal
@@ -14,11 +15,7 @@ from linearmodels.panel import (
)
from openbb_core.app.model.obbject import OBBject
from openbb_core.app.router import Router
from openbb_core.app.utils import (
basemodel_to_df,
get_target_column,
get_target_columns,
)
from openbb_core.app.utils import basemodel_to_df, get_target_column, get_target_columns
from openbb_provider.abstract.data import Data
from pydantic import PositiveInt
from statsmodels.stats.diagnostic import acorr_breusch_godfrey
@@ -113,8 +110,39 @@ def ols_summary(
"""
X = sm.add_constant(get_target_columns(basemodel_to_df(data), x_columns))
y = get_target_column(basemodel_to_df(data), y_column)
results = sm.OLS(y, X).fit()
return OBBject(results=Data(summary=results.summary()))
results_summary = results.summary()
results = {}
for item in results_summary.tables[0].data:
results[item[0].strip()] = item[1].strip()
results[item[2].strip()] = str(item[3]).strip()
table_1 = results_summary.tables[1]
headers = table_1.data[0] # Assuming the headers are in the first row
for i, row in enumerate(table_1.data):
if i == 0: # Skipping the header row
continue
for j, cell in enumerate(row):
if j == 0: # Skipping the row index
continue
key = f"{row[0].strip()}_{headers[j].strip()}" # Combining row index and column header
results[key] = cell.strip()
for item in results_summary.tables[2].data:
results[item[0].strip()] = item[1].strip()
results[item[2].strip()] = str(item[3]).strip()
results = {k: v for k, v in results.items() if v}
clean_results = {}
for k, v in results.items():
new_key = re.sub(r"[.,\]\[:-]", "", k).lower().strip().replace(" ", "_")
clean_results[new_key] = v
clean_results["raw"] = str(results_summary)
return OBBject(results=clean_results)
@router.command(methods=["POST"])
@@ -123,7 +151,7 @@ def dwat(
y_column: str,
x_columns: List[str],
) -> OBBject[Dict]:
"""Perform Durbin-Watson test for autocorrelation
"""Perform Durbin-Watson test for autocorrelation.
Parameters
----------
@@ -164,6 +192,7 @@ def bgot(
List of columns to use as exogenous variables.
lags: PositiveInt
Number of lags to use in the test.
Returns
-------
OBBject[Data]
@@ -193,6 +222,7 @@ def coint(
Data columns to check cointegration
maxlag: PositiveInt
Number of lags to use in the test.
Returns
-------
OBBject[Data]
@@ -243,6 +273,7 @@ def granger(
Columns to use as exogenous variables.
lag: PositiveInt
Number of lags to use in the test.
Returns
-------
OBBject[Data]
@@ -289,6 +320,7 @@ def unitroot(
regression: str
Regression type to use in the test. Either "c" for constant only, "ct" for constant and trend, or "ctt" for
constant, trend, and trend-squared.
Returns
-------
OBBject[Data]
@@ -313,7 +345,7 @@ def panelre(
y_column: str,
x_columns: List[str],
) -> OBBject[Dict]:
"""Perform One-way Random Effects model for panel data
"""Perform One-way Random Effects model for panel data.
Parameters
----------
@@ -342,7 +374,7 @@ def panelbols(
y_column: str,
x_columns: List[str],
) -> OBBject[Dict]:
"""Perform a Between estimator regression on panel data
"""Perform a Between estimator regression on panel data.
Parameters
----------
@@ -371,7 +403,7 @@ def panelpols(
y_column: str,
x_columns: List[str],
) -> OBBject[Dict]:
"""Perform a Pooled coefficvient estimator regression on panel data
"""Perform a Pooled coefficvient estimator regression on panel data.
Parameters
----------
@@ -400,7 +432,7 @@ def panelols(
y_column: str,
x_columns: List[str],
) -> OBBject[Dict]:
"""One- and two-way fixed effects estimator for panel data
"""One- and two-way fixed effects estimator for panel data.
Parameters
----------
@@ -429,7 +461,7 @@ def panelfd(
y_column: str,
x_columns: List[str],
) -> OBBject[Dict]:
"""Perform a first-difference estimate for panel data
"""Perform a first-difference estimate for panel data.
Parameters
----------
@@ -458,7 +490,7 @@ def panelfmac(
y_column: str,
x_columns: List[str],
) -> OBBject[Dict]:
"""Fama-MacBeth estimator for panel data
"""Fama-MacBeth estimator for panel data.
Parameters
----------