Estimate probability of a parameter
State degree of believe in specific parameter values
Evaluate probability of hypothesis given the data
Incorporate prior knowledge
I have a bag with 4 stones. Some are black. Some are white.
I’m going to draw stones, one at a time, with replacement, and let’s see the number of ways that the draw could have been produced.
After 4 draws, let’s calculate the probability of W white stones and B black stones. Let’s formalize how we made this calculation - and derive Bayes Theorem!
\[p(H_i | Draw) = \frac{Likelihood\,* \,Prior}{Average\,\,Likelihood}\]
\[p(H_i | Draw) = \frac{p(Draw | H_i) p(H_i)}{P(Draw)}\]
\[p(Hypothesis | Data) = \frac{p(Data | Hypothesis) p(Hypothesis)}{P(Data)}\]
Now let’s do that over again! And again!
Use dplyr
and mutate()
for the following.
library(rethinking)
#alist is a list for used to define a model
some_model <- alist(
#our likelihood
#our prior - can be something else if you want!
)
#define the data - you fill in the probability
some_data <- list(...)
#We will use map - maximum a posteriori sampling
#Note, I use rethinking:: in case you've loaded purrr
some_fit <- rethinking::map(some_model,
data = some_data)
This is from the Rcode on page 42, box 2.6. Assume 100 draws.
library(rethinking)
#alist is a list for used to define a model
draws_mod <- alist(
#our likelihood
w ~ dbinom(100, p),
#our prior - can be something else if you want!
p ~ dunif(0,1)
)
#define the data - you fill in the number of successes
draws_data <- list(w = 32)
#We will use map - maximum a posteriori sampling
#Note, I use rethinking:: in case you've loaded purrr
draws_fit <- rethinking::map(draws_mod,
data = draws_data)
Maximum a posteriori (MAP) model fit
Formula:
w ~ dbinom(100, p)
p ~ dunif(0, 1)
MAP values:
p
0.3200005
Log-likelihood: -2.46
Mean StdDev 5.5% 94.5%
p 0.32 0.05 0.25 0.39
Mean StdDev 20.0% 80.0%
p 0.32 0.05 0.28 0.36
In Bayesian analyses, the 95% Credible Interval is the region in which we find 95% of the possible parameter values. The observed parameter is drawn from this distribution. For normally distributed parameters:
\[\hat{\beta} - 2*\hat{SD} \le \hat{\beta} \le \hat{\beta} +2*\hat{SD}\]
where \(\hat{SD}\) is the SD of the posterior distribution of the parameter \(\beta\). Note, for non-normal posteriors, the distribution may be different.
\[\hat{\beta} - t(\alpha, df)SE_{\beta} \le \beta \le \hat{\beta} +t(\alpha, df)SE_{\beta}\]
p
1 0.3544378
2 0.2834933
3 0.3437578
4 0.2504989
5 0.3051117
6 0.3089026