Hi,
I want to run repeated regressions on a bootstrapped sample and save regression output to a file. I am using Stata/SE 11.2.
More precisely, I want to:
1. draw a bootstrap sample using bsample.
2. run my first regression on this sample and save selected regression output. Each regression is run on different regions.
3. run my second regression on the same sample and save the regression output to the same file as above. Again, each regression is run on different regions.
4. repeat the procedure above 1000 times and append the new regression output to the file above.
My first question is whether I can use a loop such as forval or foreach to have Stata repeat the regressions, etc., 1000 times, even if I don't have any identifier for my loop? My second question is how to most efficiently save the selected regression output to a file?
The code below runs a loop on the 5 different regions on the same bsample and it saves the regression output of interest in the matrix var`i' (the last element `i' is simply included to identify the region in the new data set). I have tried different version on how to then save this matrix as a data file, for instance regsave, without finding the solution I want. Therefore, after svmat I have left the loop "empty".
use data, clear
forval i= 1/5{
bsample if region==`i'
reg y x1 x2 x3
matrix b`i'=e(b)
reg x1 z
matrix pi`i'=e(b)
scalar sig`i'=e(rmse)
matrix var`i'=[b`i', pi`i', sig`i', `i']
svmat var`i', names(testvar)
}
I am afraid that these are trivial problems for many of you, but I would be very grateful for any help. I would really like to use Stata for this analysis and not switch to Fortran as my coauthor is proposing.
Thank you in advance
Henrik
I want to run repeated regressions on a bootstrapped sample and save regression output to a file. I am using Stata/SE 11.2.
More precisely, I want to:
1. draw a bootstrap sample using bsample.
2. run my first regression on this sample and save selected regression output. Each regression is run on different regions.
3. run my second regression on the same sample and save the regression output to the same file as above. Again, each regression is run on different regions.
4. repeat the procedure above 1000 times and append the new regression output to the file above.
My first question is whether I can use a loop such as forval or foreach to have Stata repeat the regressions, etc., 1000 times, even if I don't have any identifier for my loop? My second question is how to most efficiently save the selected regression output to a file?
The code below runs a loop on the 5 different regions on the same bsample and it saves the regression output of interest in the matrix var`i' (the last element `i' is simply included to identify the region in the new data set). I have tried different version on how to then save this matrix as a data file, for instance regsave, without finding the solution I want. Therefore, after svmat I have left the loop "empty".
use data, clear
forval i= 1/5{
bsample if region==`i'
reg y x1 x2 x3
matrix b`i'=e(b)
reg x1 z
matrix pi`i'=e(b)
scalar sig`i'=e(rmse)
matrix var`i'=[b`i', pi`i', sig`i', `i']
svmat var`i', names(testvar)
}
I am afraid that these are trivial problems for many of you, but I would be very grateful for any help. I would really like to use Stata for this analysis and not switch to Fortran as my coauthor is proposing.
Thank you in advance
Henrik
Comment