-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
405 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
#' Plot the behavior of the model selection scores vs the number of clusters | ||
#' | ||
#' @description Plot the BIC, Log Likelihood, ICL scores obtatined after the fit for each number of cluster. | ||
#' | ||
#' @param model_selection_tibble a tibble with 3 scores and k_max values, one for each inference | ||
#' @param best_K the integer corresponding to the best number of components | ||
#' | ||
#' @return model_selection_plot | ||
#' @export | ||
|
||
plot_model_selection_h <- function(model_selection_tibble, best_K) { | ||
create_plot <- function(data, x, y, best_K, y_label, score_name) { | ||
ggplot2::ggplot(data, ggplot2::aes(x = !!ggplot2::sym(x), y = !!ggplot2::sym(y))) + | ||
ggplot2::geom_line(color = "steelblue", linewidth = 1) + | ||
ggplot2::geom_point(color = "steelblue", size = 3) + | ||
ggplot2::geom_point(data = data[data[[x]] == best_K, ], | ||
ggplot2::aes(x = !!ggplot2::sym(x), y = !!ggplot2::sym(y)), color = "firebrick", size = 4) + | ||
ggplot2::labs(y = y_label, x = "Number of Clusters (K)", title = score_name) + | ||
ggplot2::theme_minimal(base_size = 14) + | ||
ggplot2::theme( | ||
legend.position = "none", | ||
plot.title = ggplot2::element_text(face = "bold", size = 14, hjust = 0.5), | ||
axis.title.x = ggplot2::element_text(margin = ggplot2::margin(t = 10)), | ||
axis.title.y = ggplot2::element_text(margin = ggplot2::margin(r = 10)), | ||
panel.grid.minor = ggplot2::element_blank() | ||
) | ||
} | ||
|
||
bic_plot <- create_plot(model_selection_tibble, "K", "BIC", best_K, "BIC", "BIC vs K") | ||
log_lik_plot <- create_plot(model_selection_tibble, "K", "Log_lik", best_K, "Log-Likelihood", "Log-Likelihood vs K") | ||
icl_plot <- create_plot(model_selection_tibble, "K", "ICL", best_K, "ICL", "ICL vs K") | ||
|
||
model_selection_plot <- (bic_plot) / icl_plot / (log_lik_plot) + | ||
patchwork::plot_annotation( | ||
title = "Model Selection Graphs: Scores vs Number of Clusters", | ||
caption = "Source: Your Data", | ||
theme = ggplot2::theme_minimal(base_size = 12) + | ||
ggplot2::theme( | ||
plot.title = ggplot2::element_text(size = 16, hjust = 0.5), | ||
plot.caption = ggplot2::element_text(size = 10, hjust = 0.5, margin = ggplot2::margin(t = 10)) | ||
) | ||
) | ||
|
||
return(model_selection_plot) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
#' Plot elbo Function | ||
#' | ||
#' @description Plot the ELBO behaviour for each fit fixing the number of cluster. | ||
#' @param elbo_iteration data.frame': #iterations until convergence of elbo obs. of 2 variables: iteration: int, elbo : num | ||
#' one of the element list elbo_iteration obtained from fit_h "results" | ||
#' | ||
#' @return p | ||
#' @export | ||
plot_elbo_h <- function(elbo_iteration){ | ||
|
||
data <- elbo_iteration | ||
|
||
p <- ggplot2::ggplot(data, ggplot2::aes(x = .data$iteration, y = .data$elbo)) + | ||
ggplot2::geom_line(color = "blue", size = 0.2) + | ||
# geom_point(color = "black", size = 1.5) + | ||
ggplot2::labs( | ||
title = paste0("ELBO Convergence for K = ", nrow(data) - 1), | ||
x = "Iteration", | ||
y = "ELBO" | ||
) + | ||
# theme_classic(base_size = 12) + | ||
ggplot2::theme( | ||
axis.title = ggplot2::element_text(size = 14), | ||
axis.text = ggplot2::element_text(size = 12), | ||
panel.grid = ggplot2::element_line(color = "gray100"), | ||
) + | ||
ggplot2::scale_x_continuous( | ||
breaks = seq(min(data$iteration), max(data$iteration), by = 1) # Ensure only integers appear | ||
) + | ||
ggplot2::scale_y_continuous(labels = scales::comma) | ||
|
||
|
||
return(p) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
#' Plot posterior clocks distributions obtained from the hierarchical model fit | ||
#' | ||
#' @param results list of 4: $data, $draws_and_summary, $log_lik_matrix_list and $elbo_iterations | ||
#' @param K index of inference whose results want to be plotted | ||
#' | ||
#' @return areas_tau | ||
#' @export | ||
#' | ||
plot_posterior_clocks_h <- function(results, K){ | ||
draws <- results$draws_and_summary[[K]]$draws | ||
|
||
names <- paste("tau[", 1:K, "]", sep = "") | ||
|
||
areas_tau <- bayesplot::mcmc_areas( | ||
draws, | ||
pars = names, | ||
prob = 0.8, # 80% intervals | ||
prob_outer = 0.95, # 99% | ||
point_est = "median" | ||
)+ | ||
ggplot2::labs( | ||
title = "Approximate Posterior distributions", | ||
subtitle = "With median and 80% and 95% intervals" | ||
)+ | ||
ggplot2::xlim(0, 1) | ||
|
||
return(areas_tau) | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
#' Plot posterior weights distributions obtained from the hierarchical model fit | ||
#' | ||
#' @param results list of 4: $data, $draws_and_summary, $log_lik_matrix_list and $elbo_iterations | ||
#' @param K index of inference whose results want to be plotted | ||
#' | ||
#' @return areas_tau | ||
#' @export | ||
|
||
plot_posterior_weights_h <- function(results, K){ | ||
|
||
draws <- results$draws_and_summary[[K]]$draws | ||
S = nrow(results$data$accepted_cna) | ||
|
||
intervals_weigths_per_tau <- list() | ||
for (k in 1:K){ | ||
names_weights <- paste("w[",1:S,",", k, "]", sep = "") | ||
p <- bayesplot::mcmc_intervals(draws, pars = names_weights, point_est = "median", prob = 0.8, prob_outer = 0.95)+ | ||
ggplot2::labs( | ||
title = stringr::str_wrap( paste0("Posterior distributions of the weigths for tau ",k), width = 30 + K + sqrt(S)), | ||
subtitle = "With median and 80% and 95% intervals" | ||
) | ||
intervals_weigths_per_tau[[k]] <- ggplot2::ggplotGrob(p) | ||
} | ||
p <- gridExtra::grid.arrange(grobs = intervals_weigths_per_tau, ncol = K) #add global title | ||
|
||
return(p) | ||
} | ||
|
||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Binary file added
BIN
+12.5 KB
vignettes/a4_Multiple_segment_timing_files/figure-html/unnamed-chunk-6-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Oops, something went wrong.