[R] Randomization/Permutation Tests

Torsten Hothorn Torsten.Hothorn at rzmail.uni-erlangen.de
Wed Nov 28 09:02:12 CET 2001


> Dear R-Users,
> 
> I was wondering if anybody out there has written R/S code or knows of a
> library for doing randomization tests (for example, permutating group
> assignments to find a p-value that does not require making a normality
> assumption). I looked through the available functions and the packages,
> but did not find anything applicable. As an example of what I am
> referring to, please see the rudimentary function below, which should
> provide an approximate p-value for a 2-independent samples scenario
> (this is only an approximation, since the function does not generate all
> possible combinations, but instead just generate random permutations;
> but with a large number of permuts, the error should be quite small). If
> some similar code is already available, I would like to hear about it.
> Thanks!

the function `perm.test' for the paired and independent sample situation
will be available in the next version of `exactRankTests' (the function is
able to deal with arbitrary integer valued scores and I can send you a
snapshot). 

However, the current version
of `exactRankTests' allows you to compute the conditional p-values using
the function `pperm', for a description you may also have a look at

@article{on-exact-r:2001,   
   key       = {118},
   author    = {Torsten Hothorn},
   title     = {On Exact Rank Tests in {R}},
   journal   = {R News},
   year      = {2001},
   volume    = {1},   
   number    = {1},
   pages     = {11-12}
}
 
Torsten

> 
> -------------
> 
> ran2ind <- function(x, y, permuts = 1000) {
> 	nx	<- length(x)
> 	ny	<- length(y)
> 	ntot	<- nx + ny
> 	xy	<- c(x, y)
> 
> 	truet	<- ( mean(x) - mean(y) ) / ( sqrt( ( (nx-1)*var(x) + (ny-1)*var(y) ) / (nx + ny - 2) ) * sqrt( 1/nx + 1/ny ) )
> 	tstats	<- matrix(NA, 1, permuts)
> 
> 	for (i in 1:permuts) {
> 		permute	<- sample(c(1:ntot), ntot, replace=F)
> 		group1	<- xy[permute[1:nx]]
> 		group2	<- xy[permute[(nx+1):ntot]]
> 		tstats[1,i]	<- ( mean(group1) - mean(group2) ) / ( sqrt( ( (nx-1)*var(group1) + (ny-1)*var(group2) ) / (nx + ny - 2) ) * sqrt( 1/nx + 1/ny ) )
> 	}
> list(tvalues=tstats, truet=truet, pvalue=mean( abs(tstats) >= abs(truet) ) )
> }
> 
> 
> ---------------------------------------------------------------------
> Wolfgang Viechtbauer                       wviechtb at s.psych.uiuc.edu
> Department of Quantitative Psychology      viechtba at students.uiuc.edu
> 
> University of Illinois, Champaign-Urbana   "Your  mouse   has  moved.
> Room 409, Psychology Building               Windows must be restarted
> 603 E. Daniel Street                        for  the  change  to take
> Champaign, IL 61820                         effect. Reboot now? [OK]"
> 
> -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
> r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
> Send "info", "help", or "[un]subscribe"
> (in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
> _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
> 

-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._



More information about the R-help mailing list