Hello guys,
I am having trouble with creating a simple loop that creates a new variable out of the residuals after a regression.
For instance, I have 5 different stock returns over a period of 25 years. I would like to regress the market return on each of these 5 different stock returns and create a new variable which includes the residuals.
Manually you would do:
1. regress "stockX" "Marketreturn"
2. Predict 'varname' residuals.
But now, I have a bigger dataset than the 5 different stocks, how can I create a loop out of this such that I do not have to do this manually.
I have tried this loop (assuming an example with 5 stocks):
1. foreach var of varlist stock1 stock2 stock3 stock4 stock5 {
2. regress `var' marketreturn
3. forvalues j = 1/5 {
4. predict res`j', residuals
5. }
6. }
But it only loops once and creates the same residual value of every res'j' created.
Who can help me out creating the right loop?
Thank you in advance!
I am having trouble with creating a simple loop that creates a new variable out of the residuals after a regression.
For instance, I have 5 different stock returns over a period of 25 years. I would like to regress the market return on each of these 5 different stock returns and create a new variable which includes the residuals.
Manually you would do:
1. regress "stockX" "Marketreturn"
2. Predict 'varname' residuals.
But now, I have a bigger dataset than the 5 different stocks, how can I create a loop out of this such that I do not have to do this manually.
I have tried this loop (assuming an example with 5 stocks):
1. foreach var of varlist stock1 stock2 stock3 stock4 stock5 {
2. regress `var' marketreturn
3. forvalues j = 1/5 {
4. predict res`j', residuals
5. }
6. }
But it only loops once and creates the same residual value of every res'j' created.
Who can help me out creating the right loop?
Thank you in advance!