Skip to content

Instantly share code, notes, and snippets.

@Sarkom
Created October 19, 2014 22:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Sarkom/1101d76054422625aa41 to your computer and use it in GitHub Desktop.
Save Sarkom/1101d76054422625aa41 to your computer and use it in GitHub Desktop.
Simple permutation testing code from http://spark.rstudio.com/ahmed/permutation/ (used to show the same effect as the more complex code: https://gist.github.com/Sarkom/95e3c7ecfcdbddeec959)
# Copy of code from http://spark.rstudio.com/ahmed/permutation/
a = rnorm(1000, 0, 1)
b = rnorm(1000, 100, 1.5)
# Combine the two datasets into a single dataset
# i.e., under the null hypothesis, there is no difference between the two groups
combined = c(a,b)
# Observed difference
diff.observed = var(b) - var(a)
number_of_permutations = 1000
diff.random = NULL
for (i in 1 : number_of_permutations) {
# Sample from the combined dataset without replacement
shuffled = sample (combined, length(combined))
a.random = shuffled[1 : length(a)]
b.random = shuffled[(length(a) + 1) : length(combined)]
# Null (permuated) difference
diff.random[i] = var(b.random) - var(a.random)
}
# P-value is the fraction of how many times the permuted difference is equal or more extreme than the observed difference
pvalue = sum(abs(diff.random) >= abs(diff.observed)) / number_of_permutations
print (pvalue)
# Combine the two datasets into a single dataset
# i.e., under the null hypothesis, there is no difference between the two groups
combined = c(a,b)
# Observed difference
diff.observed = sd(b) - sd(a)
number_of_permutations = 1000
diff.random = NULL
for (i in 1 : number_of_permutations) {
# Sample from the combined dataset without replacement
shuffled = sample (combined, length(combined))
a.random = shuffled[1 : length(a)]
b.random = shuffled[(length(a) + 1) : length(combined)]
# Null (permuated) difference
diff.random[i] = sd(b.random) - sd(a.random)
}
# P-value is the fraction of how many times the permuted difference is equal or more extreme than the observed difference
pvalue = sum(abs(diff.random) >= abs(diff.observed)) / number_of_permutations
print (pvalue)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment