Prior:
\(prob \sim Uniform(0,1)\)
Prior:
\(\mu \sim Normal(0,1000)\)
\(\sigma \sim U(0,50)\)
Mean StdDev |0.89 0.89|
height 154.60 7.74 142.24 166.37
weight 44.99 6.46 35.47 56.02
age 41.14 15.97 18.00 64.00
male 0.47 0.50 0.00 1.00
Prior:
\(\mu \sim Normal(150, 20)\) From the data
\(\sigma \sim U(0,50)\) Wide range of possibilities
Prior:
\(\mu \sim Normal(150, 20)\) From the data
\(\sigma \sim U(0,50)\) Wide range of possibilities
Prior:
\(\mu \sim Normal(150, 20)\) From the data
\(\sigma \sim U(0,50)\) Wide range of possibilities
# Make the grid
grid <- crossing(mu = seq(140, 160, length.out=200),
sigma = seq(4, 9, length.out=200)) %>%
#Calculate the log-likelihoods for each row
group_by(1:n()) %>%
mutate(log_lik = sum(dnorm(Howell1_Adult$height, mu, sigma, log=TRUE))) %>%
ungroup() %>%
# Use these and our posteriors to get the numerator
# of Bayes theorem
mutate(numerator = log_lik +
dnorm(mu, 150, 20, log=TRUE) +
dunif(sigma, 0,50, log=TRUE)) %>%
#Now calculate the posterior (approximate)
mutate(posterior = exp(numerator - max(numerator)))
# Make the grid
grid <- crossing(mu = seq(140, 160, length.out=200),
sigma = seq(4, 9, length.out=200)) %>%
#Calculate the log-likelihoods for each row
group_by(1:n()) %>%
mutate(log_lik = sum(dnorm(Howell1_Adult$height, mu, sigma, log=TRUE))) %>%
ungroup() %>%
# Use these and our posteriors to get the numerator
# of Bayes theorem
mutate(numerator = log_lik +
dnorm(mu, 150, 20, log=TRUE) +
dunif(sigma, 0,50, log=TRUE)) %>%
# Make the grid
grid <- crossing(mu = seq(140, 160, length.out=200),
sigma = seq(4, 9, length.out=200)) %>%
#Calculate the log-likelihoods for each row
group_by(1:n()) %>%
mutate(log_lik = sum(dnorm(Howell1_Adult$height, mu, sigma, log=TRUE))) %>%
ungroup() %>%
# Use these and our posteriors to get the numerator
# of Bayes theorem
mutate(numerator = log_lik +
dnorm(mu, 150, 20, log=TRUE) +
dunif(sigma, 0,50, log=TRUE)) %>%
#Now calculate the posterior (approximate)
mutate(posterior = exp(numerator - max(numerator)))
height ~ dnorm(mu, sigma)
Prior:
\(\mu \sim Normal(150, 20)\) mu ~ dnorm(150, 200)
\(\sigma \sim U(0,50)\) sigma ~ dunif(0,50)
Likelihood:
\(h_i \sim Normal(\mu_i, \sigma)\)
Data Generating Process
\(\mu_i = \alpha + \beta x_i\)
Prior:
\(\alpha \sim Normal(178, 100)\) Reasonable Range from data
\(\beta \sim Normal(0, 10)\) Weakly Informative
\(\sigma \sim U(0,50)\) Wide range of possibilities
[1] 31.07105 62.99259
Eh?
Try a log-normal to guaruntee a positive value!
[1] 7.208450 -3.946306 9.826626 -14.043020 8.002034 -7.507425
[7] -17.869624 6.412382 -7.564241 6.945962
[1] 2.8380974 0.8909155 0.2886832 0.1285018 1.1239655 1.4806061 0.8809009
[8] 0.7026157 3.6836816 0.4159838
We use link
to extract from the link (prediction) function:
[1] 10000 352
Mean StdDev 5.5% 94.5% alpha beta sigma
alpha 113.98 1.90 110.94 117.03 1.00 -0.99 0
beta 0.90 0.04 0.84 0.97 -0.99 1.00 0
sigma 5.07 0.19 4.77 5.38 0.00 0.00 1
alpha beta sigma
1 113.2631 0.9113468 5.447617
2 111.5392 0.9552925 5.374237
3 118.2644 0.8187566 5.204558
4 114.1050 0.9063995 4.928162
5 114.1539 0.9037442 5.189243
6 110.6849 0.9780145 5.147052
#1) Make a fake data frame over the relevant range
pred_df <- data.frame(weight=seq(30,65, length.out=200))
#2) Get the fit values & interval
pred <- link(weight_fit, data=pred_df, refresh=0)
pred_hpdi <- apply(pred, 2, HPDI)
#3) Put it all back together and plot
pred_df <- mutate(pred_df,
height = apply(pred, 2, mean),
lwr_fit = pred_hpdi[1,],
upr_fit = pred_hpdi[2,])
geom_line
and geom_ribbon
for plotting
This is not linear
Likelihood:
\(h_i \sim Normal(\mu_i, \sigma)\)
Data Generating Process
\(\mu_i = \alpha + \beta_1 x_i + \beta_2 x_i^2 + \beta_3 x_i^3\)
Prior:
\(\alpha \sim Normal(178, 100)\)
\(\beta_j \sim Normal(0, 10)\)
\(\sigma \sim U(0,50)\)
link
or the posterior to plot fit error