[R] Code find exact distribution for runs test?
Peter Ehlers
ehlers at ucalgary.ca
Fri Feb 12 22:41:04 CET 2010
Here's a simple function (same idea):
f <- function(x){
lenx <- length(x)
negx <- sum(x < 0)
mat <- matrix(1, lenx, choose(lenx,negx))
for(i in seq_len(choose(lenx, negx))){
mat[combn(lenx,negx)[, i], i] <- -1
}
mat
}
x <- c(-1,-1,-1,1,1,1,1)
f(x)
[combn() is now in utils]
-Peter Ehlers
Greg Snow wrote:
> Here is one quick way using the combinat package:
>
>> library(combinat)
>>
>> tmpfun <- function(x) {
> + tmp <- rep(1,5)
> + tmp[x] <- -1
> + tmp
> + }
>> combn(5,2, tmpfun)
> [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
> [1,] -1 -1 -1 -1 1 1 1 1 1 1
> [2,] -1 1 1 1 -1 -1 -1 1 1 1
> [3,] 1 -1 1 1 -1 1 1 -1 -1 1
> [4,] 1 1 -1 1 1 -1 1 -1 1 -1
> [5,] 1 1 1 -1 1 1 -1 1 -1 -1
>
> Of course in this case the tmpfun function needs to be rewritten for each vector size, so is not generalizable.
>
More information about the R-help
mailing list