Skip to content

Instantly share code, notes, and snippets.

@explodecomputer
Created March 10, 2016 17:05
Show Gist options
  • Save explodecomputer/b308732a1734866bc2f9 to your computer and use it in GitHub Desktop.
Save explodecomputer/b308732a1734866bc2f9 to your computer and use it in GitHub Desktop.
calculate effect size and se from z scores
get_beta_se_from_p_z_n_vary <- function(z, n, vy, maf)
{
qval <- qchisq(pnorm(abs(z), low=FALSE)*2, 1, low=F) / (qchisq(pnorm(abs(z), low=FALSE)*2, n-2, low=F)/(n-2))
r <- sqrt(sum(qval / (n - qval)))
b <- sign(z) * sqrt(r^2 * vy / (2*maf*(1-maf)))
se <- b / z
return(list(b=b, se=se))
}
n <- 100000
x <- rbinom(n, 2, 0.2)
y <- rnorm(n) * 50 + x
mod <- summary(lm(y ~ x))
z <- mod$coefficients[2,1] / mod$coefficients[2,2]
p <- mean(x)/2
vy <- var(y)
get_beta_se_from_p_z_n_vary(z, n, var(y), p)
mod$coefficients
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment