Last active
July 28, 2018 10:54
-
-
Save vankesteren/e8b60e0e23005c04688560a43349ce71 to your computer and use it in GitHub Desktop.
Pretty ridges plot for stan parameters
This file contains hidden or 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
| # Pretty ridges plot for stan parameters | |
| # MIT license (c) Erik-Jan van Kesteren 2018 | |
| # WARNING: sourcing this function will install/update packages | |
| if (requireNamespace("devtools", quietly = TRUE)) { | |
| suppressMessages(devtools::install_github("vankesteren/firatheme")) | |
| suppressMessages(devtools::install_cran(c("dplyr", "tidyr", "forcats", | |
| "ggplot2", "ggridges"))) | |
| param_ridges <- function(stanfit, params, ...) { | |
| stopifnot(class(stanfit) == "stanfit") | |
| # create df | |
| df <- dplyr::bind_rows(lapply(stanfit@sim$samples, dplyr::as_tibble)) | |
| df <- tidyr::gather(data = df, key = "parameter", value = "samples") | |
| df <- dplyr::mutate(df, paramname = as.factor( | |
| gsub(x = parameter, pattern = "\\[[0-9]+\\]", replacement = "", perl = TRUE) | |
| )) | |
| if (!missing(params)) { | |
| df <- dplyr::filter(df, paramname %in% params) | |
| df$paramname <- forcats::fct_drop(df$paramname) | |
| df$paramname <- forcats::fct_relevel(df$paramname, rev(params)) | |
| df <- dplyr::arrange(df, paramname) | |
| df$parameter <- forcats::as_factor(df$parameter) | |
| } | |
| ggplot2::ggplot(df, ggplot2::aes(samples, parameter, fill = paramname)) + | |
| ggridges::geom_density_ridges2(alpha = 0.6, ...) + | |
| firatheme::theme_fira() + | |
| ggplot2::theme(legend.position = "none") + | |
| ggplot2::labs(x = "Parameter value", y = "") | |
| } | |
| } else { | |
| warning("Package devtools needed for this function, install it first!") | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment