Announcement

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

  • Matrix Command for Storing Signrank results (looped)

    Hi everyone,

    I am just new in using the matrix command for storing results in tables and I am practicing quite a bit. I have ran a Mann-Whitney Test on my data and would just like to store the z scores. I know that I could simply copy and paste it as tables, but I'd really like to learn how to use the "matrix" command (for future purposes) so I decided to try it out. Here is my code:

    Code:
    forvalues i = 1/5 {
        signrank f1_`i'_after = f1_`i'_before
        matrix signrankf1 = (r(z))
        matrix rownames signrankf1 = `var'
        matrix colnames signrankf1 = "z-value"
        mat2txt, matrix(signrankf1) saving(signrankf1) append
        signrank f2_`i'_after = f2_`i'_before
        matrix signrankf2 = (r(z))
        matrix rownames signrankf2 = `var'
        matrix colnames signrankf2 = "z-value"
        mat2txt, matrix(signrankf2) saving(signrankf2) append
        signrank f3_`i'_after = f3_`i'_before
        matrix signrankf3 = (r(z))
        matrix rownames signrankf3 = `var'
        matrix colnames signrankf3 = "z-value"
        mat2txt, matrix(signrankf3) saving(signrankf3) append
        signrank f4_`i'_after = f4_`i'_before
        matrix signrankf4 = (r(z))
        matrix rownames signrankf4 = `var'
        matrix colnames signrankf4 = "z-value"
        mat2txt, matrix(signrankf4) saving(signrankf4) append
        signrank f5_`i'_after = f5_`i'_before
        matrix signrankf5 = (r(z))
        matrix rownames signrankf5 = `var'
        matrix colnames signrankf5 = "z-value"
        mat2txt, matrix(signrankf5) saving(signrankf5) append
        signrank f6_`i'_after = f6_`i'_before
        matrix signrankf6 = (r(z))
        matrix rownames signrankf6 = `var'
        matrix colnames signrankf6 = "z-value"
        mat2txt, matrix(signrankf6) saving(signrankf6) append
    }
    It is very long (and still repetitive despite being in a loop), but it does the job. Also, this stores the results of the rank sum in individual text files. I was hoping to store all the results it in just one text file. I just like to ask if there is any other way that I could make this one shorter and more efficient?

    Thank you in advance for your help.

    Best regards,
    Lovely



  • #2
    It sounds like you want something like this:
    Code:
    forvalues i = 1/5 {
        forvalues j = 1/6 {
            signrank f`j'_`i'_after = f`j'_`i'_before
            matrix signrankf = (r(z))
            matrix rownames signrankf = `var'
            matrix colnames signrankf = "z-value"
            mat2txt, matrix(signrankf) saving(signrankf) append
        }
    }
    This will put all 30 of your signrank test results into a single file, signrankf.txt. The key points are:

    1. You can have one loop inside another.
    2. There is no need to have separate matrices for each of the signrankf*_`i' variables (unless you plan to use them later). For present purposes, re-using the same matrix, signrankf, will do.
    3. To get everything into one file, you have to repeatedly use the same filename.

    I note the reference to a local macro var which is not defined within the code you show. Presumably you have defined it somewhere earlier.

    I also note that you need to be careful what you wish for. The above code will put all 30 results into a single file, but it will not really tell you which is which.

    Comment


    • #3
      When I started saving results from multiple runs of similar code I followed the path you are on. However, I discovered the post set of commands provides a more flexible framework for this sort of thing. When you read the output of help post don't be put off by the description of the commands as utilities to assist programmers in performing Monte Carlo-type experiments. That may have been the original justification, but it's not a limitation to their utility. Here is what I hope is a self-evident example of their use, based on the tightened code that Clyde provided to answer your original question.
      Code:
      postfile results str20 var int (i j) float r using signrankf, replace
      
      foreach var in moose squirrel {
          forvalues i = 1/2 {
              forvalues j = 1/3 {
                  local rz = runiform()
                  post results ("`var'") (`i') (`j') (`rz')
              }
          }
      }
      
      postclose results
      use signrankf, clear
      list, clean
      Code:
      . list, clean
      
                  var   i   j          r  
        1.      moose   1   1   .3488717  
        2.      moose   1   2   .2668857  
        3.      moose   1   3   .1366463  
        4.      moose   2   1   .0285569  
        5.      moose   2   2   .8689333  
        6.      moose   2   3   .3508549  
        7.   squirrel   1   1   .0711051  
        8.   squirrel   1   2    .323368  
        9.   squirrel   1   3   .5551032  
       10.   squirrel   2   1    .875991  
       11.   squirrel   2   2   .2047095  
       12.   squirrel   2   3   .8927587

      Comment


      • #4
        I complete agree with William that postfiles are a much more convenient way to accumulate results of calculations than matrices. In fact, in responses to posts like this I ordinarily recommend avoiding matrices and using postfiles. The only reason I didn't do that in #2 is because I read the original post as expressing an interest in learning to work with matrices.

        Comment


        • #5
          And the only reason I didn't propose postfiles earlier was that I thought it was important that the original question should be answered, but I'm too rusty on this sort of use of matrices to answer confidently.

          I will add that when I used matrices for this purpose, I was in the habit of appending each set of results to an existing matrix, adding columns to the matrix. For the record, it was something like the following, which I will not explain further because it's been too long and I don't care to spend the time to work up an example to test and correct my syntax..
          Code:
          foreach v of varlist `vars' {
              dosomething `v'
              matrix A = nullmat(A) \ `r(whatever)'
              }
          mat2txt ...
          Last edited by William Lisowski; 24 Dec 2017, 12:17.

          Comment

          Working...
          X