Announcement

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

  • How to capture SignRank Test results?

    I have 253 observations and each observation had 100 rounded experiment.

    What I would like to do is that: I want to run signrank test to test whether the those 100 sequence is random from average.

    I want to loop and capture the results in a matrics. From the forum, I summarized the code below:

    Code:
    foreach var in P {
    
    bysort P: signrank Bid = AvgP
            matrix signrankf = (r(z))
            matrix rownames signrankf = `var'
            matrix colnames signrankf = "z-value"
            mat2txt, matrix(signrankf) saving(signrankf) append
    }

    However it does not work. On the other hand, I cannot capture the Prob(z) and the sign of the test.

    Any idea to improve the code?
    Last edited by Ahmet Cakir; 12 Jul 2018, 18:23.

  • #2
    Here's an example of how to do this
    Code:
    sysuse auto, clear
    rename length Bid
    rename displacement AvgP
    rename rep78 P
    
    capture program drop one_test
    program define one_test
        signrank Bid = AvgP
        gen z = r(z)
        exit
    end
    
    runby one_test, by(P)
    To run this, you need the -runby- command, by Robert Picard and me, and available from SSC.

    In the future, when asking for help with code, please provide example data. Here I have just pressed the auto.dta dataset into service and renamed some variables to follow yours. But workarounds like this sometimes result in datasets that differ in important ways from what you actually have. There is no substitute for an example taken from your actual data and presented using the -dataex- command to show it. If you are running version 15.1 or a fully updated version 14.2, it is already part of your official Stata installation. If not, run -ssc install dataex- to get it. Either way, run -help dataex- to read the simple instructions for using it. -dataex- will save you time; it is easier and quicker than typing out tables. It includes complete information about aspects of the data that are often critical to answering your question but cannot be seen from tabular displays or screenshots. It also makes it possible for those who want to help you to create a faithful representation of your example to try out their code, which in turn makes it more likely that their answer will actually work in your data.

    When asking for help with code, always show example data. When showing example data, always use -dataex-.

    Comment


    • #3
      Thank you for the answer. It worked perfectly!
      And sorry for my mistake.


      However I have a question: Is it also possible to capture the obs and sign? Because my final goal is to count how many of them are Positive or Negative signed.


      Code:
      input int P byte Bid float AvgP
      1301  82 80.42
      1301  91 80.42
      1301  83 80.42
      1301  86 80.42
      1301  73 80.42
      1301  73 80.42
      1301  94 80.42
      1301  83 80.42
      1301  71 80.42
      1301  95 80.42
      1301  82 80.42
      1301  58 80.42
      1301  95 80.42
      1301  72 80.42
      1301  81 80.42
      1301  91 80.42
      1301  85 80.42
      1301  39 80.42
      1301  63 80.42
      1301  45 80.42
      1301  73 80.42
      Last edited by Ahmet Cakir; 12 Jul 2018, 18:33.

      Comment


      • #4
        I solved the problem.

        First, I have run the test, then applied
        Code:
         return list, all
        to see which results, I can return.

        Actually, that helped me to run

        Code:
        capture program drop one_test
        program define one_test
            signrank Bid = AvgP
            gen z = r(z)
            exit
        end
        runby one_test, by(P)
        
        
        capture program drop one_test
        program define one_test
            signrank Bid = AvgP
            gen Neg = r(N_neg)
            exit
        end
        runby one_test, by(P)
        
        
        capture program drop one_test
        program define one_test
            signrank Bid = AvgP
            gen Pos = r(N_pos)
            exit
        end
        runby one_test, by(P)
        bysort P: sum if Neg < Pos
        Probably there is short cut for one step runby, but copy-paste is straightforward.

        Thanks to Cylde since they wrote such a handy package!

        Comment


        • #5
          Yes, you can indeed make it a one-step process:

          Code:
          capture program drop one_test
          program define one_test
              signrank Bid = AvgP
              gen z = r(z)
              gen Neg = r(N_neg)
              gen Pos = r(N_pos)
              exit
          end
          
          runby one_test, by(P)

          Comment

          Working...
          X