# permutations (was Re: [R] (no subject))

Wed Jul 14 03:17:06 CEST 2004

```I think I may know what you want. Try this :

# code
strata.restricted.sample <- function( grp ){
K       <- length(unique(grp))     # number of levels
new.grp <- sample(grp)

xtab    <- table(grp, new.grp)
propA   <- apply(xtab, 1, function(x) max(x) / sum(x))
no.A    <- sum( propA == 1 )

if(no.A == K){ Recall(grp=grp) }
return(new.grp)
}

# description
You cross-tabulate original and permuted groups (line4) to calculate the
proportion of agreement (line5) and find out how many groups have
perfect agreement (line6). If ALL groups have perfect agreement, you
discard and re-sample (line8). Look at matchClasses in e1071 package for
perhaps a more elegant method.

# usage
> g1 <- rep(1:4, each=3); n  <- length(g1)
> g2 <- strata.restricted.sample(g1)
> g2  # new groupings
 1 1 2 3 4 2 4 1 4 3 2 3

To show the indice (with some random tie breaking), do this :
> c(1:n) [ order( g2 + 10e-6 * rnorm(n) ) ]
  8  1  2  3  6 11 12 10  4  7  5  9

# detail
Just in case anyone is wondering why this might be of use, consider the
following two lines which gives the same goodness of fit test measures.
g <- rep(1:4, each=3); lm( x ~ g, each=3) )
g <- rep(4:1, each=3); lm( x ~ g, each=3) )

PS : Please do not change the subject unless you have a good reason !

On Tue, 2004-07-13 at 23:41, Jordi Altirriba GutiÃ©rrez wrote:
>   Dear R users,
>   First of all, thanks for the incredibly fast answers and help of Rolf,
> Marc and Robert.
>   Yes, I noticed that it was a lot of permutacions, but my intention was to
> make this process automatic and take only 5.000 - 10.000 permutations.
> Therefore, I wanted only to take that "interesting permutations" with "some
> information" [inter-block permutations].
>   The reason why I'm interested in these permutations is because I'm using
> some packages of Bioconductor to analyse my data from some microarrays and I
> thought that perhaps could be interesting to see what happens when I permute
> my data and I compare it against the not permuted data.
>   Thanks again for your time and suggestions.
>
> Jordi Altirriba
> Ph. D. Student
>
> Hospital Clinic-Barcelona-Spain
>
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://www.stat.math.ethz.ch/mailman/listinfo/r-help