get ols regression results with for loop for dataframe against a constant

The solution for “get ols regression results with for loop for dataframe against a constant” can be found here. The following code will assist you in solving the problem.

import pandas as pd
import numpy as np
import statsmodels.api as sm

df = pd.DataFrame(np.random.randint(low=0, high=10, size=(5, 5)),
columns=[‘Historic_Rate’, ‘Overnight’, ‘1M’, ‘3M’, ‘6M’])

fit_d = {} # This will hold all of the fit results and summaries
for col in [x for x in df.columns if x != ‘Historic_Rate’]:
Y = df[‘Historic_Rate’] – df[‘Historic_Rate’].shift(1)
# Need to remove the NaN for fit
Y = Y[Y.notnull()]

X = df[col] – df[col].shift(1)
X = X[X.notnull()]

X = sm.add_constant(X) # Add a constant to the fit

fit_d[col] = sm.OLS(Y,X).fit()

Thank you for using DeclareCode; We hope you were able to resolve the issue.

More questions on [categories-list]

0
inline scripts encapsulated in