Announcement

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

  • For loops for mata functions over differently named matrices

    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.
    Last edited by Tugba Atasoy; 12 May 2016, 06:55.

  • #2
    You're trying tio apply the concept of Stata macros in Mata where such concept does not exist (there are only variables). This article should help you understand why your approach does not work.

    In your code you can use instead a vector of pointers to store your different matrices

    Code:
    mata
    n=22
    matrix_10 = J(1,22,NULL)
    
    for (i=1; i<=n; i++) {
    
    stri = strofreal(i)
    matrix_10[1,i] = &st_matrix("matrix_10_"+ stri) //Stata matrix into mata matrix
    mean_10 = mean(*matrix_10[1,i])
    sd_10 = sqrt(diagonal(*variance(matrix_10[1,i])))'
    st_matrix("mean_10_"+stri, mean_10) //Mata matrix into stata matrix
    st_matrix("sd_10_"+stri, sd_10) // Mata matrix into stata matrix
    }
    end

    If you're doing some bootstrapping try to use the bootstrap command instead, it's better suited IMHO.

    Comment


    • #3
      I do not know what your aiming at.
      But I think you could also do it like:
      Code:
      mata
          n=22
          for (i=1; i<=n; i++) {
              m=st_matrix(sprintf("matrix_10_%f", i)) //Stata matrix into mata matrix
              mean = mean(m)
              sd = sqrt(diagonal(variance(m)))'
              st_matrix(sprintf("mean_10_%f", i), mean) //Mata matrix into stata matrix
              st_matrix(sprintf("sd_10_%f", i), sd') // Mata matrix into stata matrix
          }
      end
      Kind regards

      nhb

      Comment


      • #4
        Thank you very much for the very helpful answers!

        Both suggestions have worked well for me. I will work myself into how to use the pointers!

        Comment

        Working...
        X