Skip to content

Instantly share code, notes, and snippets.

@explodecomputer
Last active August 29, 2015 14:28
Show Gist options
  • Save explodecomputer/4bf1b57dde4b8f29cef8 to your computer and use it in GitHub Desktop.
Save explodecomputer/4bf1b57dde4b8f29cef8 to your computer and use it in GitHub Desktop.
Power calculation
# Use this library - it handles linear regression instead of assuming balanced ANOVA
library(pwr)
# Just gonna simulate some data but you can obviously use the actual allele frequencies, effect sizes and phenotypic variances
# Allele frequencies:
nsnp <- 29
p <- runif(nsnp)
# Effect sizes:
eff <- rnorm(nsnp)
# Variance of the SNP effects
snpvar <- 2 * p * (1-p) * eff^2
# Phenotypic variance
# e.g. if all SNPs explain 2% of variance then
totvar <- sum(snpvar) / 0.02
# Variance explained by all SNPs (R^2)
r2_total <- sum(snpvar) / totvar
r2_each_snp <- snpvar / totvar
# Other parameters
samplesize <- 5800
multiple_tests <- 50
# Test for all SNPs
pwr.f2.test(
u = 1,
v = samplesize - 2,
f2 = r2_total / (1 - r2_total),
sig.level = 0.05 / multiple_tests
)
# Test for each SNP
pwr.f2.test(
u = 1,
v = samplesize - 2,
f2 = r2_each_snp / (1 - r2_total),
sig.level = 0.05 / multiple_tests
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment