## permutation test

```> set.seed(2012)
> ctr <- rnorm(15, 5, 2)
> trm <- rnorm(15, 6, 2)
> ctr
[1]  3.4441635  3.8442482  6.3265121  5.1760447  7.5141573  3.7404510
[7]  4.2080860  5.7459793  5.8142129  2.5497654  5.6246073  7.4974758
[13]  3.3182997  5.7448370 -0.4339599
> trm
[1] 12.277560  4.365237  1.645831 10.296847  7.784031  6.853054  6.888757
[8]  7.262824  5.963059  1.868349  6.472083 10.051960  7.878363  5.578179
[15]  7.977236
```

```> t.test(ctr, trm)

Welch Two Sample t-test

data:  ctr and trm
t = -2.4147, df = 25.367, p-value = 0.02328
alternative hypothesis: true difference in means is not equal to 0
95 percent confidence interval:
-4.0810132 -0.3254517
sample estimates:
mean of x mean of y
4.674325  6.877558
```

?View Code RSPLUS
 ```1 2 3 4 5 6 7 8 9 10 11 12 13 14 ``` ```perm.test <- function(d1, d2, nIter=10000) { m <- mean(d2-d1) pooledData <- c(d1, d2) n <- length(d1) meanDiff <- numeric(nIter) for (i in 1:nIter) { idx <- sample(1:length(pooledData), n, replace=FALSE) d1 <- pooledData[idx] d2 <- pooledData[-idx] meanDiff[i] <- mean(d2) - mean(d1) } p <- mean(abs(meanDiff) >= abs(m)) return(p) }```
```> perm.test(ctr, trm)
[1] 0.0218
```

#### Related Posts

Leave a comment ?

## 1 Comments.

1. 大神，太强~spss各种不会用~哎~

Reply