# [R] Randomization/Permutation Tests

Wolfgang Viechtbauer wviechtb at s.psych.uiuc.edu
Tue Nov 27 23:29:22 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!

-------------

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
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._

```