Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add lambda (genomic inflation factor) #3

Open
sinarueeger opened this issue Oct 16, 2018 · 0 comments
Open

Add lambda (genomic inflation factor) #3

sinarueeger opened this issue Oct 16, 2018 · 0 comments
Labels
qqplot this issue is related to stat_qqplot function

Comments

@sinarueeger
Copy link
Owner

sinarueeger commented Oct 16, 2018

## Geom Inflation factor
## ------------------
# https://academic.oup.com/hmg/article/17/R2/R122/2527210
lambda_gc <- function(x)
{
  # x = P values
  ## turn P values into Zstats, then square them
  chisq <- qnorm(x/2)^2
  
  lambda <- median(chisq)/qchisq(0.5,1)
  return(lambda)
}
  

StatLambdaGC <- ggproto("StatLambdaGC", Stat,
                      compute_group = function(data, scales, params, p.thresh) {
                        data < - 
                       gc.val = lambda_gc(data$y)
                       label = glue::glue("lambda = {format(gc.val, digits = 3)}")
                       data.frame(x = 1, y = 1, label = label)
                      },
                      
                      required_aes = c("y")
)

stat_lambda_gc <- function(mapping = NULL, data = NULL, geom = "text",
                        position = "identity", na.rm = FALSE, show.legend = NA, 
                        inherit.aes = TRUE, parse = TRUE, hjust = "left", vjust = "outward", p.thresh = 0, ...) {
  layer(
    stat = StatLambdaGC, data = data, mapping = mapping, geom = geom, 
    position = position, show.legend = show.legend, inherit.aes = inherit.aes,
    params = list(na.rm = na.rm, parse = parse, 
                  hjust = hjust, vjust = vjust, p.thresh = p.thresh, ...)
  )
}

n.sample <- 10000
df <- data.frame(P = runif(n.sample), GWAS = sample(c("a","b"), n.sample, replace = TRUE))

## Example 
ggplot(df, aes(y = P)) + 
    stat_qqplot() + 
    geom_abline(intercept = 0, slope = 1) + 
    stat_lambda_gc()
@sinarueeger sinarueeger added the qqplot this issue is related to stat_qqplot function label Oct 16, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
qqplot this issue is related to stat_qqplot function
Projects
None yet
Development

No branches or pull requests

1 participant