Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Specifying and calling a mata function inside stata loop

    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:
    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
    When I want to implement the function I get the error

    Code:
    'return' found where almost anything else expected
    Inside the loop I want to call the function as follows

    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



  • #2
    Well, I believe return L would be return(L). And then, it will be problematic calling directly from Stata a function that returns something. You'd probably want to wrap your Mata code in a Stata program (in an ADO-file, probably, and one that would deliver everything neat-and-tidy back to Stata when the program exits) and call that in your Stata loop.

    By the way, doesn't Stata, itself, have a invsym(),as well as a svmat?

    Comment


    • #3
      Thank you very much Joseph! I out-sourced the mata bit of the loop in a do-file which I called from within the loop. This works perfectly fine. I haven't tried to do it all in Stata but I will try it next time.

      Comment


      • #4
        You don't have to define the mata function inside the loop. You define it once and then call it from the Stata loop. Below some suggestions of modification to your code. I think it would be more efficicent to store the whole data in Mata (not just 25 observations) and then make the loop in Mata, where you select 25 observations, instead of using putmata and getmata back and forth (10000 times). But I can't figure out exactly what you are trying to achieve, so I might well be mistaken .

        Code:
        clear mata
        mata
        real matrix 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
        
        forvalues z=1(1)10000 {
        
        // Some code
        
        putmata var1 var2 var3, replace // all 25 variables
        
        mata: L=inversion() // calling the function
        
        
        getmata (new_var1 new_var2 new_var3)=L, replace // storing the columns of L in 25 new variables.
        }

        Comment


        • #5
          Originally posted by Christophe Kolodziejczyk View Post
          Code:
          . . .
          forvalues z=1(1)10000 {
          
          . . .
          
          mata: L=inversion() // calling the function
          
          . . .
          }
          I don't think that you can do that. I believe that you'll get an error if you try to call from Stata a Mata function that returns a matrix and also try to assign it to a Stata name like that.

          You can do stuff like this
          Code:
          mata: inversion()
          but that will just display the returned matrix.

          I think that trying to assign a returned result (string or real scalar or matrix) from a Mata function to a token in Stata with a statement like
          Code:
          mata: my_Stata_name = my_Mata_function()
          will give you an error.

          Comment


          • #6
            Joseph
            But L is not a Stata name, but a Mata object

            the following code works fine

            Code:
            clear mata
            mata:
            real matrix foo()
            {
            
            return(1)
            }
            
            end
            
            forval i = 1/10 {
            mata: L = foo()
            getmata (new_var1)=L , replace
            }

            Comment


            • #7
              I'm mistaken. Thanks for setting me straight on this, Christophe.

              Comment

              Working...
              X