Dear statalist members,
I am very new to mata environment. I am currently facing a problem with the for loops option in mata environment, as I try to work with differently named matrices which have been defined in stata in a loop (e.g., the matrix named as "matrix_10_i" in the below example).
I am trying to compute bootstrapped standard errors through resampling the residuals for estimates that are based on the initial regression results in my stata code. I do this in stata, in a loop with x replications (defined as j=1/x).
To do the bootstrapping: a) I first create a response variable, b) run the regressions with this new response variable, c) compute the estimates based on these new regressions, d) I save the newly computed estimates into new matrices and in a last step e) I compute the variance and the standard errors from these new estimates that are saved into matrices.
For the last step e, I switch in my code from stata to mata environment (which I am not sure whether is the most practical thing to do), where I try to loop over matrices that are already defined in a loop over two different parameters; i (i=1/22) as well as n (foreach n of numlist 10 20).
In the below code, I am trying to do the computation in a loop in mata for the case of n==10. However except for the third command line - which reads the stata matrix into the mata matrix - the code seems to not work.
mata
n=22
for (i=1; i<=n; i++) {
matrix_10_i=st_matrix("matrix_10_i") //Stata matrix into mata matrix
mean_10_i = mean(matrix_10_i)
sd_10_i = sqrt(diagonal(variance(matrix_10_i)))'
st_matrix("mean_10_i", mean_10_i) //Mata matrix into stata matrix
st_matrix("sd_10_i", sd_10_i') // Mata matrix into stata matrix
}
end
I would really appreciate if someone can help me with how to execute such a loop in mata and/or provide me with suggestions on how to do a similar computation in stata.
I am very new to mata environment. I am currently facing a problem with the for loops option in mata environment, as I try to work with differently named matrices which have been defined in stata in a loop (e.g., the matrix named as "matrix_10_i" in the below example).
I am trying to compute bootstrapped standard errors through resampling the residuals for estimates that are based on the initial regression results in my stata code. I do this in stata, in a loop with x replications (defined as j=1/x).
To do the bootstrapping: a) I first create a response variable, b) run the regressions with this new response variable, c) compute the estimates based on these new regressions, d) I save the newly computed estimates into new matrices and in a last step e) I compute the variance and the standard errors from these new estimates that are saved into matrices.
For the last step e, I switch in my code from stata to mata environment (which I am not sure whether is the most practical thing to do), where I try to loop over matrices that are already defined in a loop over two different parameters; i (i=1/22) as well as n (foreach n of numlist 10 20).
In the below code, I am trying to do the computation in a loop in mata for the case of n==10. However except for the third command line - which reads the stata matrix into the mata matrix - the code seems to not work.
mata
n=22
for (i=1; i<=n; i++) {
matrix_10_i=st_matrix("matrix_10_i") //Stata matrix into mata matrix
mean_10_i = mean(matrix_10_i)
sd_10_i = sqrt(diagonal(variance(matrix_10_i)))'
st_matrix("mean_10_i", mean_10_i) //Mata matrix into stata matrix
st_matrix("sd_10_i", sd_10_i') // Mata matrix into stata matrix
}
end
I would really appreciate if someone can help me with how to execute such a loop in mata and/or provide me with suggestions on how to do a similar computation in stata.
Comment