diff --git a/NAMESPACE b/NAMESPACE index ba90c536..13991372 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -37,7 +37,6 @@ export(get_population_allele_frequency) export(get_r_from_lor) export(get_r_from_pn) export(harmonise_data) -export(ios) export(ld_matrix) export(make_dat) export(mr) diff --git a/R/add_metadata.r b/R/add_metadata.r index e99f2888..2859d01d 100644 --- a/R/add_metadata.r +++ b/R/add_metadata.r @@ -20,7 +20,6 @@ add_metadata <- function(dat, cols = c("sample_size", "ncase", "ncontrol", "unit message(what, ": none of the IDs found in database") return(NULL) } - for(col in cols) { if(!col %in% names(info)) @@ -28,9 +27,10 @@ add_metadata <- function(dat, cols = c("sample_size", "ncase", "ncontrol", "unit info[[col]] <- NA } } - - info <- dplyr::select(info, "id", "sample_size", "ncase", "ncontrol", "unit", "sd") - names(info) <- paste0(names(info), ".", what) + info <- subset(info, select=c("id", cols)) + names(info) <- paste0(names(info), ".", what) + index <- grepl("ukb-d", info$id) & is.na(info$sample_size) + info$sample_size[index] <- 300000 return(info) } @@ -61,5 +61,6 @@ add_metadata <- function(dat, cols = c("sample_size", "ncase", "ncontrol", "unit dat <- dat[dat[[order_col]], ] dat <- dat[, !names(dat) %in% order_col] + # dat <- fix_ukb_d(dat) return(dat) } diff --git a/R/ios.R b/R/ios.R deleted file mode 100644 index b4b4963b..00000000 --- a/R/ios.R +++ /dev/null @@ -1,43 +0,0 @@ -#' Calculate index of suspicion -#' -#' If a SNP influences multiple other traits then it could be 'suspicious', and more likely to be pleiotropic. -#' This function implements two basic approaches to estimate IOS -#' \itemize{ -#' \item ios1: A summary of the SNP r2 with the other traits (r2_gu). -#' \item ios2: A summary of the ratio of r2_gu / r2_gx, where r2_gx is the variance explained by the SNP on the exposure. Estimates the index of suspicion, whereupon SNPs which have a larger effect on a set of traits given their effect on the exposure are deemed more suspicious. -#' } -#' -#' Summarising across multiple traits can be dune using mean, sd, iqr, median, 95% value, maximum value. -#' -#' @param exposure_dat Instruments for the exposure, obtained using [`extract_instruments`]. -#' @param background_dat Effects for the instruments on a set of variables, used to calculate index of suspicion. -#' -#' @export -#' @return Data.frame -#' @importFrom stats median quantile sd -ios <- function(exposure_dat, background_dat) -{ - requireNamespace("dplyr", quietly = TRUE) - requireNamespace("reshape2", quietly = TRUE) - background_dat$vgu <- background_dat$beta.outcome^2 * 2 * background_dat$eaf.outcome * (1 - background_dat$eaf.outcome) - exposure_dat$vgx <- exposure_dat$beta.exposure^2 * 2 * exposure_dat$eaf.exposure * (1 - exposure_dat$eaf.exposure) - background_dat <- merge(background_dat, subset(exposure_dat, select=c(SNP, vgx)), by="SNP") - background_dat$r2_ratio <- background_dat$vgu / background_dat$vgx - ios <- dplyr::group_by(background_dat, SNP) %>% - dplyr::summarise( - ios1_mean = sum(vgu, na.rm=TRUE), - ios1_sd = sd(vgu, na.rm=TRUE), - ios1_iqr = quantile(vgu, 0.75, na.rm=TRUE) - quantile(vgu, 0.25, na.rm=TRUE), - ios1_median = median(vgu, na.rm=TRUE), - ios1_95 = quantile(vgu, 0.95, na.rm=TRUE), - ios1_max = max(vgu, na.rm=TRUE), - ios2_mean = sum(r2_ratio, na.rm=TRUE), - ios2_sd = sd(r2_ratio, na.rm=TRUE), - ios2_iqr = quantile(r2_ratio, 0.75, na.rm=TRUE) - quantile(r2_ratio, 0.25, na.rm=TRUE), - ios2_median = median(r2_ratio, na.rm=TRUE), - ios2_95 = quantile(r2_ratio, 0.95, na.rm=TRUE), - ios2_max = max(r2_ratio, na.rm=TRUE) - ) %>% melt - return(ios) -} - diff --git a/man/ios.Rd b/man/ios.Rd deleted file mode 100644 index 56e56786..00000000 --- a/man/ios.Rd +++ /dev/null @@ -1,27 +0,0 @@ -% Generated by roxygen2: do not edit by hand -% Please edit documentation in R/ios.R -\name{ios} -\alias{ios} -\title{Calculate index of suspicion} -\usage{ -ios(exposure_dat, background_dat) -} -\arguments{ -\item{exposure_dat}{Instruments for the exposure, obtained using \code{\link{extract_instruments}}.} - -\item{background_dat}{Effects for the instruments on a set of variables, used to calculate index of suspicion.} -} -\value{ -Data.frame -} -\description{ -If a SNP influences multiple other traits then it could be 'suspicious', and more likely to be pleiotropic. -This function implements two basic approaches to estimate IOS -\itemize{ -\item ios1: A summary of the SNP r2 with the other traits (r2_gu). -\item ios2: A summary of the ratio of r2_gu / r2_gx, where r2_gx is the variance explained by the SNP on the exposure. Estimates the index of suspicion, whereupon SNPs which have a larger effect on a set of traits given their effect on the exposure are deemed more suspicious. -} -} -\details{ -Summarising across multiple traits can be dune using mean, sd, iqr, median, 95\% value, maximum value. -} diff --git a/tests/testthat/test_add_metadata.r b/tests/testthat/test_add_metadata.r index 9d40952d..1da9e407 100644 --- a/tests/testthat/test_add_metadata.r +++ b/tests/testthat/test_add_metadata.r @@ -41,3 +41,9 @@ test_that("no id2", { d <- add_metadata(d) expect_true(!"units.outcome" %in% names(d)) }) + +test_that("ukb-d", { + d <- extract_outcome_data(exposure$SNP, 'ukb-d-30710_irnt') + d <- add_metadata(d) + expect_true("units.outcome" %in% names(d)) +} diff --git a/tests/testthat/test_rsq.r b/tests/testthat/test_rsq.r index 17bb399e..a382bb76 100644 --- a/tests/testthat/test_rsq.r +++ b/tests/testthat/test_rsq.r @@ -28,3 +28,8 @@ test_that("dat 2", { expect_true("rsq.outcome" %in% names(d) & "rsq.exposure" %in% names(d)) }) +test_that("dat ukb-d", { + d <- make_dat(exposure="ukb-d-30710_irnt", proxies=FALSE) %>% add_rsq() + expect_true("rsq.outcome" %in% names(d) & "rsq.exposure" %in% names(d)) +}) +