Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.3k views
in Technique[技术] by (71.8m points)

pandas - LOOP univariate rolling window regression on entire DF Python

I have a dataframe of 24 variables (24 columns x 4580 rows) from 2008 to 2020.

My independant variable is the first one in the DF and the dependant variables are the 23 others.

I've done a test for one rolling window regression, it works well, here is my code :

import statsmodels.api as sm

from statsmodels.regression.rolling import RollingOLS

import seaborn

seaborn.set_style('darkgrid')

pd.plotting.register_matplotlib_converters()

 

x = sm.add_constant(df[['DIFFSWAP']])

y = df[['CADUSD']]

rols = RollingOLS(y,x, window=60)

rres = rols.fit()

params = rres.params

r_sq = rres.rsquared

Now, what i want to do, i'd like to do a loop to regress (rolling window) all the dependant variables of the DF (columns 2:24) on the independant variable (column 1) and store the coefficients and the rsquareds.

My ultimate goal is to extract Rsquareds and Coefficients and put them in dataframes(or lists or whatever) and then graph them.

I'm new to Python so I'd be very gratefull for any help.

Thank you!

question from:https://stackoverflow.com/questions/65836299/loop-univariate-rolling-window-regression-on-entire-df-python

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Can you throw it all in a loop and store the results in some other object like a dict?

Potential solution:

data = {}
for column in list(df.columns)[2:]:  # iterate over columns 2 to 24

    x = sm.add_constant(df[column])
    y = df[['CADUSD']] ## This never changes from CADUSD, right?
    rols = RollingOLS(y, x, window=60)
    rres = rols.fit()
    params = rres.params
    r_sq = rres.rsquared
    # Store results from each column's fit as a new dict entry
    data[column] = {'params':params, 'r_sq':r_sq}

results_df = pd.DataFrame(data).T

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...