Hi everyone,
I'm trying to conduct a rolling window regression by using Stata's -rolling- command and generating/saving certain results after each window in a matrix. To achieve this object I wrote a wrapper function by using Stata's -program- command and named it "MyRegression". The Code itself in the function "MyRegression" is working totaly fine.
But the problem is, instead of conducting a real rolling window regression by using only the observations of the window (number of observations 52), in each single regression all observations are used (number of observations 2036). Meaning in each window 2036 observaions are used instead of 52 observations.
I'm using the variable "AssetReturn" as an dependent variable and the two variables "MarketReturn" and "InterestReturn" as regressors.
How do I have to adjust my code to achieve this objective?
Finally, this code should be used in a loop to use different dependent and independent variables in the regression.
In the following the code I used up to now:
Any help is greatly appreciated!
Kind regards,
Carl
I'm trying to conduct a rolling window regression by using Stata's -rolling- command and generating/saving certain results after each window in a matrix. To achieve this object I wrote a wrapper function by using Stata's -program- command and named it "MyRegression". The Code itself in the function "MyRegression" is working totaly fine.
But the problem is, instead of conducting a real rolling window regression by using only the observations of the window (number of observations 52), in each single regression all observations are used (number of observations 2036). Meaning in each window 2036 observaions are used instead of 52 observations.
I'm using the variable "AssetReturn" as an dependent variable and the two variables "MarketReturn" and "InterestReturn" as regressors.
How do I have to adjust my code to achieve this objective?
Finally, this code should be used in a loop to use different dependent and independent variables in the regression.
In the following the code I used up to now:
Code:
program MyRegression, // Defining a local macro, used for the rownames of resultmatrix. local Regressand `Regressand' AssetReturn // Calculating number of lags needed for -newey- regression. quietly summarize AssetReturn scalar Lagorder = floor(4*(r(N)/100)^(2/9)) // Newey-West Regression. newey AssetReturn MarketReturn InterestReturn, lag(`=Lagorder') // Calculation of p-values. scalar p_cons = 2 * ttail(e(df_r), abs(_b[_cons] / _se[_cons])) scalar p_MarketReturn = 2 * ttail(e(df_r), abs(_b[MarketReturn] / _se[MarketReturn])) scalar p_InterestReturn = 2 * ttail(e(df_r), abs(_b[InterestReturn] / _se[InterestReturn])) // Saving the residuals in a variable named "resid". predict resid, res // Square residuals. generate resid2 = resid^2 // Sum squared residuals. egen S_resid2 = sum(resid2) // Calculating root mean squar error "RMSE". // Dividing squared residuals by the number of degrees of freedom and afterwards extracting a root. scalar RMSE = sqrt(S_resid2/e(df_r)) // Saving the degrees of freedom "DF". scalar DF = e(df_r) // Saving results in a matrix named "Results". matrix Results = nullmat(Results) \ _b[_cons], p_cons, _b[MarketReturn], p_MarketReturn, _b[InterestReturn], p_InterestReturn, RMSE, DF // Matrix columnnames. matrix colnames Ergebnisse2 = Cons p_cons B_RM p_RM B_RZ p_RZ RMSE DF // Matrix rownames. matrix rownames Ergebnisse2 = `Regressand' // Delete generated variables. drop resid resid2 S_resid2 exit end // Rolling Window Regression. rolling _b _se e(df_r), window(52) stepsize(52) saving(RWR_Results , replace ): MyRegression
Any help is greatly appreciated!
Kind regards,
Carl
Comment