# st: RE: Binomial Sign Test for Two Dependent Samples

 From "Scott Merryman" <[email protected]> To <[email protected]> Subject st: RE: Binomial Sign Test for Two Dependent Samples Date Sat, 15 Jan 2005 08:31:31 -0600

```> -----Original Message-----
> From: [email protected] [mailto:owner-
> [email protected]] On Behalf Of Thomas Speidel
> Sent: Friday, January 14, 2005 4:19 PM
> To: [email protected]
> Subject: st: Binomial Sign Test for Two Dependent Samples
>
> I am trying to create little program for a "Binomial Sign Test for Two
> Dependant Samples".
> This test is somewhere between a signrank test and a binomial.  The
> computations are fairly simple:
>
> 1. Find the differences among the pairs
> 2. Ignore all null differences
> 3. Count the number of non-zero, non-missing differences (i.e. n)
> 4. Count the number of positive differences (call it x)
> 5. Compute: Binomial(n, k, 0.5)
>
> I am trying to implement this into a little program so that I can loop
> it through a number of variables/groups.  However, because I am not an
> expert programmer, I came to a roadblock...  Following is a sample of my
> data:

<snip>

> I am trying to find the binomial for each variable by group
> This is what I have thus far, but does not work:
>
> foreach x of var mp1posneg mp2posneg mp3posneg {
>                foreach i in 1 2 3 {
>                 qui count if `x'posneg==1 & group == `i'
>                 local pos_`i'`x' = r(N)
>                 qui count if `x'posneg==1 | `x'posneg==-1 & group == `i'
>                 local n_`i'`x'=r(N)
>                 display Binomial(n_`i'`x', pos_`i'`x', 0.5)
>                 }
>         }

There are two problems with this:

1.  In the lines 3 and 5 x'posneg will resolve to mp1posnegposneg, but this
variable does not exist.

2.  In line 7 the local macros n_ and pos_ need to be surrounded by `'.

So you could rewrite this as

foreach x of var mp1posneg mp2posneg mp3posneg {
foreach i in 1 2 3 {
qui count if `x'==1 & group == `i'
local pos_`i'`x' = r(N)
qui count if `x'==1 | `x'==-1 & group == `i'
local n_`i'`x'=r(N)
display Binomial(`n_`i'`x'', `pos_`i'`x'', 0.5)
}
}

Below is a slight generalization of your program:

program binsigntest
version 8.2
syntax varlist [,prob(real .5)]
local num : word count `varlist'
tempfile results
tempname hold

postfile `hold'  trials success  p  using `results'
foreach x of varlist  m* {
forv i = 1/`num'   {
qui count if `x'==1 & group == `i'
local pos_`i'`x' = r(N)
qui count if `x'==1 | `x'==-1 & group == `i'
local n_`i'`x'=r(N)
local p = Binomial(`n_`i'`x'', `pos_`i'`x'', `prob')
post `hold' (`n_`i'`x'') (`pos_`i'`x'') (`p')
}
}

postclose `hold'
preserve
use `results', clear
disp ""
disp in yel "Success on a single trial is " `prob'
l, sepby(trials) noobs

restore
end

Which gives:

. binsigntest m*

Success on a single trial is .5

+-----------------------------+
| trials   success          p |
|-----------------------------|
|      9         6   .2539063 |
|      9         1   .9980469 |
|      9         2   .9804688 |
|-----------------------------|
|      8         3   .8554688 |
|      8         2   .9648438 |
|      8         3   .8554688 |
|-----------------------------|
|     15         9   .3036194 |
|     15         4   .9824219 |
|     15         2   .9995117 |
+-----------------------------+

.

Hope this helps,
Scott

*
*   For searches and help try:
*   http://www.stata.com/support/faqs/res/findit.html
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/
```