Announcement

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

  • forval loop, mata

    Dear Statalisters,

    My data is a panel : timeid is yrqtr which is generated from year and quarter for each observation , I attached a small sample data.

    I was able to run the following code which contains mata (see block 1), it works if I run it for each quarter individually,

    However, when I put a forval loop at the very beginning, mata does not seem to like it. it breaks : --Break--
    r(1);



    I kind of got around it by creating two do files, put the block that contains mata in a standalone do file (see attached file named "mataitself"). and call this do file from another do file. the program runs, but in the log it has red ink output saying "expression invalid makesym() already exists", but the output data seems fine.

    Question:
    1. if you can add a forval loop cycle through all quarters (my panel qyrqtr ranges from 81 to 206 , calendar year 1981 to 2012 ) , my sample only shows 2 quarters. and save my output from getmata

    2. using two do files - using one to call the mataitself,should I be concerned with "expression invalid makesym() already exists" in the log?

    Thank you,
    Rochelle






    Attached Files

  • #2
    The end statement within the loop is giving trouble. Either you can write a function which returns the matrix x and call the function within the loop (with one line of code) or you can put the mata code between parentheses

    Sketch of code
    Either
    Code:
    clear mata
    mata:
    real myfunction(string scalar A)
    {
           Your mata code
    
           return(x)
    
    }
    
    forval n = {
           mata:  x = myfucnction("A")
    }
    or

    Code:
    forval n = {
           mata (
               Mata Code
           )
    
    }

    Comment


    • #3
      Thank you , Christophe !

      I do not know how to write the function. Let me ask about your second suggestion instead,

      1. do I remove the word end in this block ?

      mata (

      M = st_matrix("A")

      void function makesym(matrix M) {
      if (rows(M)!=cols(M)) {
      exit(error(1))
      }
      for(i=1;i<=rows(M);i++) {
      for(j=1;j<i;j++) {
      mx=max(M[i,j] \ M[j,i])
      M[i,j]=mx
      M[j,i]=mx
      }
      }
      }

      makesym(M)

      X=.
      L=.
      symeigensystem(M, X, L)
      x = X[.,1]
      l=L[.,1]

      end )


      2. did I put the parentheses in the right place? I did on and got an error. If you could use my attached code block1_adding forval.txt to show me how, that will be greatly appreciated.

      Rochelle

      Comment


      • #4
        ok. I have now read your program more carefully. What I wrote before was not relevant for your problem so put it aside. In the do-file block1_adding_forval.txt write clear mata at the top and try to run the program. Aftr you have run your program for the first time makesym() is in memory and you cannot define it twice unless you clear mata. Actually I have just run your program just by doing this and it worked.
        Attached Files

        Comment


        • #5
          ...and don't look at the attachment. It was not supposed to be part of my post :-)

          Comment


          • #6
            could you please take a look at attached (I inserted clear mata, but got
            --Break--
            r(1);

            I was looping over yrqtr 84 and 85 (2 quarters)

            Thank you,
            Rochelle
            Attached Files

            Comment

            Working...
            X