Dear Statalisters,
I want to call a mata function inside a stata-loop since calling mata inside the stata loop will not be feasible.
I understand that I have to define the mata function outside the loop. The function should take up 25 variables with 25 observations each, create a Matrix A, and form an inverse L of that matrix.
Then I would like to store the columns of matrix L either as new variables in Stata or make them accessible with "getmata". Does the latter even work?
My code looks like this:
When I want to implement the function I get the error
Inside the loop I want to call the function as follows
I am new to Mata programming and grateful for any hints.
Best wishes,
Johannes Eigner
I want to call a mata function inside a stata-loop since calling mata inside the stata loop will not be feasible.
I understand that I have to define the mata function outside the loop. The function should take up 25 variables with 25 observations each, create a Matrix A, and form an inverse L of that matrix.
Then I would like to store the columns of matrix L either as new variables in Stata or make them accessible with "getmata". Does the latter even work?
My code looks like this:
Code:
mata function inversion() // The variables in the loop will always have the same name but different values. Therefore I tried to "hardcode" the function without arguments { A = st_data(.,("var1","var2","var3") // Instead of 3 variables I specify 25 here. L = invsym(I(25)-A) // This creates the inversion matrix L return L // Here the code throws an error } end
Code:
'return' found where almost anything else expected
Code:
forvalues z=1(1)10000 { // Some code putmata var1 var2 var3, replace // all 25 variables inversion() // calling the function getmata (new_var1 new_var2 new_var3)=L, replace // storing the columns of L in 25 new variables.
I am new to Mata programming and grateful for any hints.
Best wishes,
Johannes Eigner
Comment