BAYESIAN INFERENCE USING R, Exercises of Mathematics

ITS ABOUT AYESIAN INFERENCE USING R, A PRACTICE ON CODING USING R STUDIO, SO THAT YOU CAN FIND THE INFERENCE, USING THESE SOFTWARE, YOU CAN EASILY IDENTIFY THE RELATIONSHIP OF THE VARIABLES

Typology: Exercises

2025/2026

Available from 04/23/2026

abelo-jeralyn
abelo-jeralyn 🇵🇭

6 documents

1 / 7

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Bayesian Inference Exercises Using R
I.
1: Coin Toss Posterior
A coin is tossed 12 times and 8 heads are observed. Prior distribution: Beta(2,2). Find the posterior
distribution.
R code:
alpha <- 2
beta <- 2
heads <- 8
tails <- 4
posterior_alpha <- alpha + heads
posterior_beta <- beta + tails
posterior_alpha
posterior_beta
2. Use the posterior from Exercise 1 to compute the posterior mean.
R code:
alpha <- 10
beta <- 6
posterior_mean <- alpha/(alpha+beta)
posterior_mean
3. Generate 5000 posterior samples.
R code:
samples <- rbeta(5000,10,6)
mean(samples)
pf3
pf4
pf5

Partial preview of the text

Download BAYESIAN INFERENCE USING R and more Exercises Mathematics in PDF only on Docsity!

Bayesian Inference Exercises Using R

I.

1: Coin Toss Posterior A coin is tossed 12 times and 8 heads are observed. Prior distribution: Beta(2,2). Find the posterior distribution. R code: alpha <- 2 beta <- 2 heads <- 8 tails <- 4 posterior_alpha <- alpha + heads posterior_beta <- beta + tails posterior_alpha posterior_beta

  1. Use the posterior from Exercise 1 to compute the posterior mean. R code: alpha <- 10 beta <- 6 posterior_mean <- alpha/(alpha+beta) posterior_mean
  2. Generate 5000 posterior samples. R code: samples <- rbeta(5000,10,6) mean(samples)
  1. Find the probability that the coin bias is greater than 0.5. R code: mean(samples > 0.5)
  2. Posterior Plot R code: curve(dbeta(x,10,6), from=0,to=1, col="blue", main="Posterior Distribution")
  3. Suppose prior probability of disease = 0.1. Test sensitivity = 0.9, specificity = 0.8. Compute posterior probability after a positive test. R code: prior <- 0. sensitivity <- 0. specificity <- 0. posterior <- (sensitivityprior)/ ((sensitivityprior)+((1-specificity)*(1-prior))) Posterior
  1. Bayesian Mean Estimate. Given: Prior mean = 50, Sample mean = 55 and Sample size = 25. Simulate posterior mean. R code: prior_mean <- 50 sample_mean <- 55 posterior_estimate <- (prior_mean + sample_mean)/ posterior_estimate II: Perform the following: Step 1: Install R packages install.packages("ggplot2") library(ggplot2) Step 2: Define Prior Distribution Example: Beta(2,2) curve(dbeta(x,2,2), from=0,to=1, main="Prior Distribution") Step 3: Collect Data Example: Heads = 7 and Tails = 3 Step 4: Compute Posterior

alpha <- 2+ beta <- 2+ Step 5: Plot Posterior curve(dbeta(x,9,5), from=0,to=1, main="Posterior Distribution", col="red") Step 6: Generate Posterior Samples samples <- rbeta(10000,9,5) hist(samples)

The prior Beta(2,2) was symmetric, centered at 0.5 (no strong bias), after observing 7

heads and 3 tails, the posterior Beta(9,5):

a. shifts to the right (toward higher values)

b. becomes more concentrated

This shows increased belief that the coin is biased toward heads.