#multimodel bayes theorem
P_A <- 0.4 
P_B <- 0.6
P_data_given_A <- 0.7
P_data_given_B <- 0.2
P_data <- P_data_given_A * P_A + P_data_given_B * P_B
P_A_given_data <- (P_data_given_A * P_A) / P_data 
P_B_given_data <- (P_data_given_B * P_B) / P_data
cat("Posterior probability of disease A: ", round(P_A_given_data, 4), "\n") 
## Posterior probability of disease A:  0.7
cat("Posterior probability of disease B: ", round(P_B_given_data, 4), "\n")
## Posterior probability of disease B:  0.3
P_A <- 0.01 
P_notA <- 1 - P_A 
P_B_given_A <- 0.99 
P_B_given_notA <- 0.05
P_B <- P_B_given_A * P_A + P_B_given_notA * P_notA
#bayes theorem
P_A_given_B <- (P_B_given_A * P_A) / P_B
#output result
cat("probability of having disease given a positive test is ", round(P_A_given_B, 4))
## probability of having disease given a positive test is  0.1667
models = c("model 1", "model 2", "model 3")
priors = c(0.3, 0.4, 0.3)
likelihoods = c(0.2, 0.1, 0.4)

unnormalized_posteriors = likelihoods * priors
evidence = sum(unnormalized_posteriors)

posterior_probs = unnormalized_posteriors / evidence

result = data.frame(Model = models,
                    Priors = priors,
                    Likelihood = likelihoods,
                    Posterior = round(posterior_probs, 4))
print(result)
##     Model Priors Likelihood Posterior
## 1 model 1    0.3        0.2    0.2727
## 2 model 2    0.4        0.1    0.1818
## 3 model 3    0.3        0.4    0.5455