R/PRS.R

Defines functions PRS

Documented in PRS

#' Polygenic risk score
#'
#' A Function to calculate polygenic risk score (PRS). PRS is calculated by summing risk alleles, which are weighted by the effect size of the risk alleles (i.e. beta coefficient for continuous traits or log(OR) for binary traits).
#' @param genotype [data.frame/matrix] The genotype data is a data frame or matrix with rows corresponding to individuals and columns to SNPs. SNP genotypes are encoded as the number of minor allele ({0, 1, 2}).
#' @param beta [vector] The effect size of each SNP. If \code{beta} is missing, assuming that all SNPs have the same effect size.
#' @return \code{PRS} return a vector of polygenic risk scores.
#' @export
#' @examples
#' input.dir <- system.file("extdata", package="pv")
#' output.dir <- system.file("extdata", package="pv")
#' path2plink <- '/path/to/plink'
#' \dontrun{
#' lr.result <- plink.lr(input.dir = input.dir,
#' output.dir = output.dir,
#' genotype = "train",
#' phenotype = "train.phenotypes.txt",
#' covar.number = c(2, 3),
#' plink.path = path2plink,
#' verbose = TRUE)
#'
#' beta <- lr.result[which(lr.result$TEST == "ADD"), ]$BETA
#' beta[is.na(beta)] <- 0
#' genotype.path <- file.path(input.dir, "train.raw")
#' genotype.path <- gsub('\\\\', '/', genotype.path)
#' genotype <- data.table::fread(genotype.path, data.table = FALSE)[, -c(1,2,3,4,5,6)]
#' prs <- PRS(genotype, beta)
#' }

PRS <- function(genotype, beta){
  if(missing(beta)){
    beta <- rep(1, ncol(genotype))
  }
  prs <- apply(genotype, 1, function(x) sum(x * beta))
  return(prs)
}
abnerzyx/pv documentation built on Feb. 27, 2022, 12:06 a.m.