-
Notifications
You must be signed in to change notification settings - Fork 0
/
functions.R
69 lines (62 loc) · 1.89 KB
/
functions.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
# plot coefficients
plot_coef <- function(coef){
coef_p <- tidyr::pivot_longer(coef, cols = c("estimate", "lower", "upper"))
coef_p$term %<>% as.character()
ggplot(data = coef_p, aes(x = value, y = term, group = term)) +
geom_point() +
geom_line()
}
library(purrr)
library(dplyr)
### convert mcmc sample to df
samples_to_df <- function(samples){
if(is.null(names(samples)))
samples <- list(chain1 = samples)
sample_df <- map_df(seq_along(samples), function(x){
df <- as.data.frame(samples[[x]])
df %>%
tidyr::gather(key="parameter", value="value") %>%
group_by(parameter) %>%
mutate(index = 1:n()) %>%
ungroup() %>%
mutate(chain = factor(x))
})
sample_df
}
### plot mcmc sample trace
plot_sample_trace <- function(samples){
df <- samples_to_df(samples)
ggplot(data = df, aes(y = value, x = index,
color = chain)) +
geom_line(size = 0.2) +
facet_wrap(~parameter, scales = "free_y")
}
### plot mcmc sample posterior density
plot_sample_density <- function(samples){
df <- samples_to_df(samples)
ggplot(data = df, aes(x = value,
color = chain,
fill = chain)) +
geom_density(alpha = 0.3) +
facet_wrap(~parameter, scales = "free")
}
### plot parameter estimates
summary_to_df <- function(summary){
df <- as_tibble(summary)
df$Parameter <- rownames(summary)
df %>%
select(Parameter, everything()) %>%
rename(Lower95 = `95%CI_low`,
Upper95 = `95%CI_upp`,
StDev = `St.Dev.`)
}
plot_estimates <- function(summary, median = TRUE){
summary_df <- summary_to_df(summary)
gp <- ggplot(summary_df, aes(y = Mean, x = Parameter))
if(median)
gp <- ggplot(summary_df, aes(y = Median, x = Parameter))
gp +
geom_point() +
geom_errorbar(aes(ymin = Lower95, ymax = Upper95),
colour="black", width=.3)
}