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
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 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) ) ]
[1] 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
> PLEASE do read the posting guide!
http://www.R-project.org/posting-guide.html
>
Jordi
try this
R> x <- c(1,2,3, 10,11,12, 41,42,43, 81,82,83)
R> dim(x) <- c(3,4)
R> x
[,1] [,2] [,3] [,4]
[1,] 1 10 41 81
[2,] 2 11 42 82
[3,] 3 12 43 83
R> jj <- t(apply(x,1,sample))
R> jj
[,1] [,2] [,3] [,4]
[1,] 1 41 10 81
[2,] 2 11 82 42
[3,] 12 3 43 83
R> as.vector(jj)
R>
[1] 1 2 12 41 11 3 10 82 43 81 42 83
and I think that does what you want...
We take the vector, rearrange it into a matrix with three rows, then
sample *within* the rows,
then rearrange into a vector again.
There will be one forbidden permutation, namely the identity (which
may or may not be
desirable).
This method doesn't allow "intra block" permutations.
best
rksh
> 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
>PLEASE do read the posting guide!
http://www.R-project.org/posting-guide.html
--
Robin Hankin
Uncertainty Analyst
Southampton Oceanography Centre
SO14 3ZH
tel +44(0)23-8059-7743
initialDOTsurname at soc.soton.ac.uk (edit in obvious way; spam precaution)