Skip to main content
Statistics LibreTexts

21.1: A Simple Example (Section 20.3)

  • Page ID
    8829
  • bayes_df = data.frame(prior=NA, 
                          likelihood=NA, 
                          marginal_likelihood=NA, 
                          posterior=NA)
    
    bayes_df$prior <- 1/1000000 
    
    
    nTests <- 3
    nPositives <- 3
    sensitivity <- 0.99
    specificity <- 0.99
    
    bayes_df$likelihood <- dbinom(nPositives, nTests, 0.99)
    
    bayes_df$marginal_likelihood <- 
      dbinom(
        x = nPositives, 
        size = nTests, 
        prob = sensitivity
      ) * bayes_df$prior + 
      dbinom(
        x = nPositives, 
        size = nTests, 
        prob = 1 - specificity
      ) * 
      (1 - bayes_df$prior)
    
    bayes_df$posterior <- 
      (bayes_df$likelihood * bayes_df$prior) / 
      bayes_df$marginal_likelihood