R/PBayes.R

#' calculate a probability from Bayes Theorem.
#' 
#' \code{PBayes(Dis, TruePos = NULL, FalsePos= NULL, TrueNeg= NULL, FalseNeg= NULL)}
#' 
#' @param Dis the occurence of the disease
#' @param TruePos proportion of true positives (sensitivity)
#' @param FalsePos proportion of false positives
#' @param TrueNeg proportion of true negatives (specificity)
#' @param FalseNeg proportion of false negatives
#' 
#' @details Three probabilities are required required:
#' probability of someone having the disease and then two others,
#' e.g. true positive and false positive probabilities.
#' 
#' @examples
#' # Say 1 in 10,000 have a disease. A test correctly identifies it 98% of the time
#' # but also suggests it is present when it isn't in 3% of cases. If someone gets
#' # positive result from the test, what's the probability they have the disease?
#' 
#' PBayes(1/10000, TruePos=0.98, FalsePos=0.03)
#' 
#' # answer is 0.33%

PBayes = function(Dis, TruePos = NULL, FalsePos= NULL, TrueNeg= NULL, FalseNeg= NULL){
  if ((is.null(TruePos)&is.null(FalseNeg))|(is.null(TrueNeg)&is.null(FalsePos))){
    print("Error: insufficient information")
  } else {
    if (is.null(TruePos)) TruePos = 1 - FalseNeg else FalseNeg = 1 - TruePos
    if (is.null(FalsePos)) FalsePos = 1 - TrueNeg else TrueNeg = 1 - FalsePos
    NotDis = 1 - Dis
    BP = (TruePos*Dis)/(TruePos*Dis + FalsePos*NotDis)
    return(BP)
  }
}
helophilus/ColsTools documentation built on May 30, 2019, 4:03 p.m.