-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample.R
More file actions
executable file
·47 lines (39 loc) · 1.35 KB
/
example.R
File metadata and controls
executable file
·47 lines (39 loc) · 1.35 KB
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
library(cmdstanr)
library(retrospectr)
file <- file.path(cmdstan_path(), "examples", "bernoulli", "bernoulli.stan")
mod <- cmdstan_model(file)
old_data <- list(N = 10, y = c(0, 1, 0, 0, 0, 0, 0, 0, 0, 1))
new_data <- list(N= 20, y = c(0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 1, 1, 1, 0, 1, 0, 1))
fit <- mod$sample(
data = old_data,
seed = 123,
chains = 4,
parallel_chains = 4,
iter_warmup = 2000,
iter_sampling = 2000,
refresh = 500 # print update every 500 iters
)
fit2 <- mod$sample(
data = new_data,
seed = 123,
chains = 4,
parallel_chains = 4,
iter_warmup = 2000,
iter_sampling = 2000,
refresh = 500 # print update every 500 iters
)
log_weights <- calculate_log_weights(model=file, fit, old_data, new_data)
samples <- fit$draws()
new_samples <- fit2$draws()
resampled_samples <- resample(fit, log_weights)
resampled_samples
posterior_1 <- posterior::as_draws_df(samples) |> dplyr::mutate(model = "Original")
posterior_2 <- posterior::as_draws_df(resampled_samples) |> dplyr::mutate(model = "Resampled")
posterior_3 <- posterior::as_draws_df(new_samples) |> dplyr::mutate(model = "New")
posteriors <- rbind(posterior_1, posterior_2, posterior_3)
g <- posteriors |> ggplot2::ggplot(ggplot2::aes(x = theta, colour = model)) +
ggplot2::geom_density() +
ggplot2::theme_bw() +
ggplot2::ylab("Density")
g
ggplot2::ggsave("example.png", plot = g)