Announcement

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

  • Run test by group

    Hi,

    I have data which resembles the following:


    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input double(group v1)
    1 1
    1 0
    1 1
    1 0
    1 1
    2 1
    2 0
    2 1
    2 1
    2 1
    end
    Here, I have a group ID variable, and a variable of interest, v1. Essentially, I wish to perform the Runs Test (https://www.stata.com/manuals/rttest.pdf) on Stata, to test whether or not the sequence of v1 are serially independent, within group. As far as I could tell from the documentation, I am unable to combine that -runtest- command by group. Ideally, I would simply do something like:

    Code:
    by group: runtest v1
    which would be similar to how t-tests for different groups could be done. Unfortunately, the runtest does not seem to have this functionality. Is there an elegant way to do it? The brute-force method would be to do the test one group at a time, and store the results in either a separate dataset (or separate variable), by preserving, dropping all other groups, testing and then restoring.

    Any insight on this is much appreciated!

    Best,
    CS

  • #2
    Try
    Code:
    . statsby, by(group) clear: runtest v
    (running runtest on estimation sample)
    
          command:  runtest v
                p:  r(p)
                z:  r(z)
              Var:  r(Var)
             mean:  r(mean)
          N_above:  r(N_above)
          N_below:  r(N_below)
           n_runs:  r(n_runs)
                N:  r(N)
               by:  group
    
    Statsby groups
    ----+--- 1 ---+--- 2 ---+--- 3 ---+--- 4 ---+--- 5 
    ..
    
    . list
    
         +-------------------------------------------------------------+
         | group   p   z   Var   mean   N_above   N_below   n_runs   N |
         |-------------------------------------------------------------|
      1. |     1   .   .     0      1         0         5        1   5 |
      2. |     2   .   .     0      1         0         5        1   5 |
         +-------------------------------------------------------------+

    Comment


    • #3
      Thanks a lot, Joro Kolev !!

      Comment

      Working...
      X