[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

Re: st: cycling paired t test (and signed rank sum test)

From   Maarten buis <>
Subject   Re: st: cycling paired t test (and signed rank sum test)
Date   Sun, 21 Oct 2007 10:45:25 +0100 (BST)

--- Diego Bellavia <> wrote:
> I have many (~ 500) variables to test in a paired design (I would
> like to perform both a paired t-test and a signed rank sum test).

First comment is that you need to apply an adjustment of the p-values
to correct for the fact that you are performing that many tests. You
can let Stata do that for you using the undocumented command -_mtest-
To find out more about -_mtest-, type -help _mtest-. (In Stata-speak
undocumenten means not in the paper manual, but there is a help file,
not-documenten means there is neither an entry in the paper manual nor
a help file) 

> To avoid typing all the variables I would like to define a double
> loop to perform the test for all the variables. 
> Since all the variables, after the reshaping in wide format of the
> dataset are in the format a1 b1 c1... and a2 b2 c2, basing on STATA 
> documentation I tried to implement arrays, and I ended up with
> something like 

The example below does what you want, in addition it also gives you
adjusted p-values (in this case Sidak adjust p-values, but you can
choose your own favourite flavor), and I have used -matlist- to give
you a nice display of the result. In the colnames you see that I call
columns something like ttest:something, or ranksum:something, ttest and
ranksum are the equation names and the something the column names, it
gives nice output from -matlist-. Another trick I use is refering to
the local i as `++i', this means: first add 1 and than use the new
content of the local i. Otherwise I haven't used any trick that aren't
explained in -help forvalues-, -help levelsof-, -help macrolists-,
-help matrix-, -help matrix substitution-, -help matlist-, -help
_mtest-, -help ttest-, and -help ranksum-.

*------------------ begin example --------------------
set more off
use "";, clear
keep idcode year ln_wage
levelsof year
local levs = "`r(levels)'"
reshape wide ln_wage, i(idcode) j(year)
local i = 0
mat rest = J(105,2,.)
mat resr = J(105,4,.)
foreach lev1 of local levs{
	local left "`left' `lev1'"
	local second : list levs - left
	foreach lev2 of local second {
		ttest ln_wage`lev1' = ln_wage`lev2'
		local diff = r(mu_2)-r(mu_1)
		matrix rest[`++i',1] = `diff', r(p)
		signtest ln_wage`lev1' = ln_wage`lev2'
		matrix resr[`i',1] = r(N_neg), r(N_pos), ///
                                     r(N_tie), r(p_neg) 
		local rowname "`rowname' `lev1'_v_`lev2'"
_mtest adjust rest, mtest(sidak) pindex(2)
matrix res = rest, r(result)
_mtest adjust resr, mtest(sidak) pindex(4)
matrix res = res, resr, r(result)
#delimit ;
matrix colnames res = ttest:diff ttest:unadj_p ttest:adj_p
                      ranksum:N_neg ranksum:N_pos ranksum:N_tie
                      ranksum:unadj_p ranksum:adj_p ;
#delimit cr
matrix rownames res = `rowname'
matlist res, underscore format(%8.0g)
*------------------ end example ---------------------
(For more on how to use examples I sent to the Statalist, see )

> Last question: to perform a paired t-test or signed rank sum test in
> STATA, I am forced to  reshape the dataset in wide format? 


Hope this helps,

Maarten L. Buis
Department of Social Research Methodology
Vrije Universiteit Amsterdam
Boelelaan 1081
1081 HV Amsterdam
The Netherlands

visiting address:
Buitenveldertselaan 3 (Metropolitan), room Z434

+31 20 5986715

Want ideas for reducing your carbon footprint? Visit Yahoo! For Good
*   For searches and help try:

© Copyright 1996–2017 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index