CopyPastor

Detecting plagiarism made easy.

Score: 1.8678078135724676; Reported for: String similarity, Exact paragraph match Open both answers

Possible Plagiarism

Reposted on 2023-03-17
by Misha

Original Post

Original - Posted on 2023-03-17
by Misha



            
Present in both answers; Present only in the new answer; Present only in the old answer;

Below is a working example with RollingOLS from statsmodels. The inspiration is from the [answer to this question on Rolling OLS Regressions and Predictions by Group](https://stackoverflow.com/questions/72119449/rolling-ols-regressions-and-predictions-by-group).
This can easily be modified for your panel data to perform a rolling window regression. Specify the covariance structure in `model.fit()`. See https://www.statsmodels.org/dev/generated/statsmodels.regression.rolling.RollingOLS.fit.html#statsmodels.regression.rolling.RollingOLS.fit.

```python from statsmodels.regression.rolling import RollingOLS from statsmodels.tools.tools import add_constant import statsmodels.api as sm import pandas as pd import numpy as np
data = sm.datasets.grunfeld.load() df_grunfeld = pd.DataFrame(data.data) df_grunfeld.set_index(['firm'], append=True, inplace=True)
# Simple Model # $$invest = \beta_0 + \beta_1 value$$
def invest_params(df_gf, intercept=False):
""" Function to operate on the data of a single firm. Assumes df_gf has the columns 'invest' and 'value' available. Returns a dataframe containing model parameters """
# we should have at least k + 1 observations min_obs = 3 if intercept else 2 wndw = 8
# if there are less than min_obs rows in df_gf, RollingOLS will throw an error # Instead, handle this case separately
if df_gf.shape[0] < min_obs: cols = ['coef_intercept', 'coef_value'] if intercept else ['coef_value'] return pd.DataFrame(index=df_gf.index, columns=cols)
y = df_gf['invest'] x = add_constant(df_gf['value']) if intercept else df_gf['value']
model = RollingOLS(y, x, expanding=True, min_nobs=min_obs, window=wndw).fit()
parameters = model.params params_shifted = model.params.shift(1) mse = model.mse_resid parameters['invest_hat'] = (parameters.mul(add_constant(df_gf['value']), axis=0)\ .sum(axis=1, min_count=1)).to_frame('invest_hat')
parameters['invest_hat_shift'] = (params_shifted.mul(add_constant(df_gf['value']), axis=0)\ .sum(axis=1, min_count=1)).to_frame('invest_hat_shift')
parameters['mse'] = mse parameters['rmse'] = np.sqrt(mse) parameters['nobs'] = model.nobs parameters['ssr'] = model.ssr parameters['t_const'] = model.tvalues['const'] parameters['t_value'] = model.tvalues['value'] parameters.rename(columns = {'const' : 'b0', 'value' : 'b1'}, inplace = True) parameters['r2_adj'] = model.rsquared_adj return parameters
grouped = df_grunfeld.groupby('firm') df_params = grouped.apply(lambda x: invest_params(x, True)) df_grunfeld_output = df_grunfeld.join(df_params, rsuffix='_coef') ```
Below is a working example with RollingOLS from statsmodels. The inspiration is from the [answer to this question on Rolling OLS Regressions and Predictions by Group](https://stackoverflow.com/questions/72119449/rolling-ols-regressions-and-predictions-by-group).
This can easily be modified for your panel data to perform a rolling window regression. `model.params` will get you the coefficients, in particular the monthly intercept fund wise as output.

```python from statsmodels.regression.rolling import RollingOLS from statsmodels.tools.tools import add_constant import statsmodels.api as sm import pandas as pd import numpy as np
data = sm.datasets.grunfeld.load() df_grunfeld = pd.DataFrame(data.data) df_grunfeld.set_index(['firm'], append=True, inplace=True)
# Simple Model # $$invest = \beta_0 + \beta_1 value$$
def invest_params(df_gf, intercept=False):
""" Function to operate on the data of a single firm. Assumes df_gf has the columns 'invest' and 'value' available. Returns a dataframe containing model parameters """
# we should have at least k + 1 observations min_obs = 3 if intercept else 2 wndw = 8
# if there are less than min_obs rows in df_gf, RollingOLS will throw an error # Instead, handle this case separately
if df_gf.shape[0] < min_obs: cols = ['coef_intercept', 'coef_value'] if intercept else ['coef_value'] return pd.DataFrame(index=df_gf.index, columns=cols)
y = df_gf['invest'] x = add_constant(df_gf['value']) if intercept else df_gf['value']
model = RollingOLS(y, x, expanding=True, min_nobs=min_obs, window=wndw).fit()
parameters = model.params params_shifted = model.params.shift(1) mse = model.mse_resid parameters['invest_hat'] = (parameters.mul(add_constant(df_gf['value']), axis=0)\ .sum(axis=1, min_count=1)).to_frame('invest_hat')
parameters['invest_hat_shift'] = (params_shifted.mul(add_constant(df_gf['value']), axis=0)\ .sum(axis=1, min_count=1)).to_frame('invest_hat_shift')
parameters['mse'] = mse parameters['rmse'] = np.sqrt(mse) parameters['nobs'] = model.nobs parameters['ssr'] = model.ssr parameters['t_const'] = model.tvalues['const'] parameters['t_value'] = model.tvalues['value'] parameters.rename(columns = {'const' : 'b0', 'value' : 'b1'}, inplace = True) parameters['r2_adj'] = model.rsquared_adj return parameters
grouped = df_grunfeld.groupby('firm') df_params = grouped.apply(lambda x: invest_params(x, True)) df_grunfeld_output = df_grunfeld.join(df_params, rsuffix='_coef') ```

        
Present in both answers; Present only in the new answer; Present only in the old answer;