Hello,
I've got a question regarding the writing of loops: What I would like to do is to write a loop that picks every variable I've got (except for one) consecutively, then calculates the correlation of the variable that is not included with each of the other included variables and writes the correlation coefficient into an empty matrix, that I have created beforehand.
My problem is the following:
If I write:
foreach k in `varlist' {
forvalues i = 1/10 {
forvalues j = 1/100 {
quietly corr g_russia `k'
matrix corr_with_russia [`i',`j']=r(rho)
}
}
}
Then Stata picks the first entry of my variable list, e.g. g_usa, and fills the whole matrix with the values of the correlation of Russia and the USA. But what I would rather like to do is to compute this correlation and put it in the first row and first column-entry of the matrix. The correlation coefficient for e.g. Spain should then be listed in the second column, first row.
But I don't know how to tell Stata to pick the first variable out of a variable list of 100 variables and only change the first entry of the matrix with the values of this variable;
then it should pick the second variable, compute a correlation coefficient and insert this as the second entry and so....
Wouldn't I have to assign numbers to the variables in the varlist to achieve this?
Hope it's halfway clear what I'm struggling with.
Thank's for the support.
Related Posts with How to write loops that fill a matrix successively
How to check if value labels are consistently defined across multiple data sets using a loop?Hi, I'm trying to append some 35 data sets, one for each state. I used a loop to append them and sa…
CI 95% Graph mixed model predictionI can't put IC95 on this graph. Can you help me, please? Thank you very much twoway connected ava t…
Specifying xtpmg optimal lags (2 3 1 2)dear community, i am having trouble specifying the optimal lags (2 3 1 2) chosen by AIC in my pmg mo…
Store matrix as r()Hello, Is there a way to store a hand-written matrix as if it is a result of a comment (those that a…
psmatch2 and matching treatment with two different yearsI would greatly appreciate your advice regarding using psmatch2 for matching treatment from two diff…
Subscribe to:
Post Comments (Atom)
0 Response to How to write loops that fill a matrix successively
Post a Comment