-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlivecode.R
69 lines (45 loc) · 1.78 KB
/
livecode.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
set.seed(1234)
n_birds <- 25
# beta0
avg_nestlings_at_avg_mass <- log(4.2)
#beta1
effect_one_gram <- .2
mother_masses_g <- rnorm(n_birds, mean = 15, sd = 2)
hist(mother_masses_g)
avg_mother_mass <- mean(mother_masses_g)
hist(mother_masses_g - avg_mother_mass)
log_average_nestlings <- avg_nestlings_at_avg_mass + effect_one_gram * (mother_masses_g - avg_mother_mass)
plot(mother_masses_g, log_average_nestlings)
nestlings <- rpois(n = n_birds, lambda = exp(log_average_nestlings))
library(tidyverse)
imaginary_birds <- tibble(mother_masses_g,
nestlings)
ggplot(imaginary_birds, aes(x = mother_masses_g, y = nestlings)) +
geom_point()
glm(nestlings ~ 1 + I(mother_masses_g - mean(mother_masses_g)),
family = poisson())
library(brms)
imaginary_birds_centered <- imaginary_birds |>
mutate(mother_mass_g_cen = mother_masses_g - mean(mother_masses_g))
bird_form <- bf(nestlings ~ 1 + mother_mass_g_cen, family = poisson(link = "log"))
get_prior(bird_form, data = imaginary_birds_centered)
bird_priors <- c(
prior(normal(1, .5), class = "Intercept"),
prior(normal(.1, .1), class = "b", coef = "mother_mass_g_cen" )
)
bird_priors
prior_predictions <- brm(formula = bird_form,
data = imaginary_birds_centered,
prior = bird_priors,
sample_prior = "only")
stancode(prior_predictions)
imaginary_birds_centered |>
add_predicted_draws(prior_predictions, ndraws = 6) |>
ggplot(aes(x = mother_masses_g, y = .prediction)) +
geom_point() +
facet_wrap(~.draw)
bird_posterior <- update(prior_predictions, sample_prior = "yes")
tidybayes::tidy_draws(bird_posterior) |>
select(.draw, b_Intercept:prior_b_mother_mass_g_cen) |>
pivot_longer(-.draw) |>
ggplot(aes(x = value, y = name)) + geom_density_ridges()