rolling estimation
by Theodoros Panagiotidis
Dear All,
I am trying to write script for running OLS and then plotting the
coefficients
# Define your sample and dataset
open capm.gdt # Replace with your dataset file
# Define the rolling window size
scalar window_size = 50 # Set the size of the rolling window (e.g., 60
periods)
# Define the variable names
series y = ERFORD # Replace with your dependent variable
series x = ERSANDP # Replace with your independent variable
# Initialize series to store rolling estimates
series coef_x = NA # Stores coefficient for x
series intercept = NA # Stores intercept (constant term)
# Run rolling regression
loop i = window_size .. $nobs # Loop from the end of the initial window to
the last observation
scalar start = i - window_size + 1 # Calculate the starting point of
the window
sample start i # Set the sample range for the current rolling window
ols y const x # Run OLS on the defined sample
# Store results
coef_x[i] = $coeff(x) # Store the coefficient for x
intercept[i] = $coeff(const) # Store the intercept (constant term)
endloop
# Reset the sample to full range after the loop
sample 1 $nobs # Reset to full sample
# Plot results or save as desired
gnuplot coef_x intercept --with-lines --time-series
I am getting this:
# Define the rolling window size
? scalar window_size = 50 # Set the size of the rolling window (e.g., 60 \
periods)
Generated scalar window_size = 50
# Define the variable names
? series y = ERFORD # Replace with your dependent variable
Generated series y (ID 17)
? series x = ERSANDP # Replace with your independent variable
Generated series x (ID 18)
# Initialize series to store rolling estimates
? series coef_x = NA # Stores coefficient for x
Generated series coef_x (ID 19)
? series intercept = NA # Stores intercept (constant term)
Generated series intercept (ID 20)
# Run rolling regression
? loop i = window_size .. $nobs # Loop from the end of the initial window
to \
the last observation
> scalar start = i - window_size + 1 # Calculate the starting point of the
\
window
> sample start i # Set the sample range for the current rolling window
> ols y const x # Run OLS on the defined sample
> > # Store results
> coef_x[i] = $coeff(x) # Store the coefficient for x
> intercept[i] = $coeff(const) # Store the intercept (constant term)
> endloop
Parse error at unexpected token 'sample'
>> sample start I
regards
Theo