From 9e66c0300ab63c36c25b51627ea4e29141bc0ce5 Mon Sep 17 00:00:00 2001 From: FBartos <38475991+FBartos@users.noreply.github.com> Date: Sun, 8 May 2022 11:51:31 +0200 Subject: [PATCH] adding JAGS diagnostics functions --- .github/workflows/R-CMD-check.yaml | 35 +- .github/workflows/pkgdown.yaml | 2 +- .github/workflows/test-coverage.yaml | 2 +- DESCRIPTION | 2 +- NAMESPACE | 4 + NEWS.md | 6 +- R/JAGS-diagnostics.R | 584 ++++++++++++++++++ R/JAGS-formula.R | 2 +- R/summary-tables.R | 2 + inst/REFERENCES.bib | 2 +- man/JAGS_diagnostics.Rd | 117 ++++ man/contr.orthonormal.Rd | 2 +- .../diagnostics-ggplot-autocorrelation-1.svg | 211 +++++++ ...diagnostics-ggplot-autocorrelation-2-1.svg | 212 +++++++ ...diagnostics-ggplot-autocorrelation-2-2.svg | 212 +++++++ ...diagnostics-ggplot-autocorrelation-2-3.svg | 212 +++++++ ...diagnostics-ggplot-autocorrelation-3-1.svg | 219 +++++++ ...diagnostics-ggplot-autocorrelation-3-2.svg | 219 +++++++ .../diagnostics-ggplot-density-1.svg | 83 +++ .../diagnostics-ggplot-density-2-1.svg | 92 +++ .../diagnostics-ggplot-density-2-2.svg | 84 +++ .../diagnostics-ggplot-density-2-3.svg | 84 +++ .../diagnostics-ggplot-density-3-1.svg | 91 +++ .../diagnostics-ggplot-density-3-2.svg | 91 +++ .../diagnostics-ggplot-trace-1.svg | 83 +++ .../diagnostics-ggplot-trace-2-1.svg | 88 +++ .../diagnostics-ggplot-trace-2-2.svg | 80 +++ .../diagnostics-ggplot-trace-2-3.svg | 80 +++ .../diagnostics-ggplot-trace-3-1.svg | 91 +++ .../diagnostics-ggplot-trace-3-2.svg | 91 +++ .../diagnostics-plot-autocorrelation-1.svg | 185 ++++++ .../diagnostics-plot-autocorrelation-2.svg | 185 ++++++ .../diagnostics-plot-autocorrelation-3.svg | 184 ++++++ .../diagnostics-plot-autocorrelation-4.svg | 368 +++++++++++ .../diagnostics-plot-autocorrelation-5.svg | 543 ++++++++++++++++ .../diagnostics-plot-autocorrelation-6.svg | 186 ++++++ .../diagnostics-plot-autocorrelation-7.svg | 382 ++++++++++++ .../diagnostics-plot-density-1.svg | 63 ++ .../diagnostics-plot-density-2.svg | 55 ++ .../diagnostics-plot-density-3.svg | 64 ++ .../diagnostics-plot-density-4.svg | 130 ++++ .../diagnostics-plot-density-5.svg | 165 +++++ .../diagnostics-plot-density-6.svg | 64 ++ .../diagnostics-plot-density-7.svg | 130 ++++ .../diagnostics-plot-trace-1.svg | 61 ++ .../diagnostics-plot-trace-2.svg | 57 ++ .../diagnostics-plot-trace-3.svg | 60 ++ .../diagnostics-plot-trace-4.svg | 120 ++++ .../diagnostics-plot-trace-5.svg | 165 +++++ .../diagnostics-plot-trace-6.svg | 66 ++ .../diagnostics-plot-trace-7.svg | 134 ++++ tests/testthat/test-JAGS-diagnostics.R | 152 +++++ 52 files changed, 6589 insertions(+), 13 deletions(-) create mode 100644 R/JAGS-diagnostics.R create mode 100644 man/JAGS_diagnostics.Rd create mode 100644 tests/testthat/_snaps/JAGS-diagnostics/diagnostics-ggplot-autocorrelation-1.svg create mode 100644 tests/testthat/_snaps/JAGS-diagnostics/diagnostics-ggplot-autocorrelation-2-1.svg create mode 100644 tests/testthat/_snaps/JAGS-diagnostics/diagnostics-ggplot-autocorrelation-2-2.svg create mode 100644 tests/testthat/_snaps/JAGS-diagnostics/diagnostics-ggplot-autocorrelation-2-3.svg create mode 100644 tests/testthat/_snaps/JAGS-diagnostics/diagnostics-ggplot-autocorrelation-3-1.svg create mode 100644 tests/testthat/_snaps/JAGS-diagnostics/diagnostics-ggplot-autocorrelation-3-2.svg create mode 100644 tests/testthat/_snaps/JAGS-diagnostics/diagnostics-ggplot-density-1.svg create mode 100644 tests/testthat/_snaps/JAGS-diagnostics/diagnostics-ggplot-density-2-1.svg create mode 100644 tests/testthat/_snaps/JAGS-diagnostics/diagnostics-ggplot-density-2-2.svg create mode 100644 tests/testthat/_snaps/JAGS-diagnostics/diagnostics-ggplot-density-2-3.svg create mode 100644 tests/testthat/_snaps/JAGS-diagnostics/diagnostics-ggplot-density-3-1.svg create mode 100644 tests/testthat/_snaps/JAGS-diagnostics/diagnostics-ggplot-density-3-2.svg create mode 100644 tests/testthat/_snaps/JAGS-diagnostics/diagnostics-ggplot-trace-1.svg create mode 100644 tests/testthat/_snaps/JAGS-diagnostics/diagnostics-ggplot-trace-2-1.svg create mode 100644 tests/testthat/_snaps/JAGS-diagnostics/diagnostics-ggplot-trace-2-2.svg create mode 100644 tests/testthat/_snaps/JAGS-diagnostics/diagnostics-ggplot-trace-2-3.svg create mode 100644 tests/testthat/_snaps/JAGS-diagnostics/diagnostics-ggplot-trace-3-1.svg create mode 100644 tests/testthat/_snaps/JAGS-diagnostics/diagnostics-ggplot-trace-3-2.svg create mode 100644 tests/testthat/_snaps/JAGS-diagnostics/diagnostics-plot-autocorrelation-1.svg create mode 100644 tests/testthat/_snaps/JAGS-diagnostics/diagnostics-plot-autocorrelation-2.svg create mode 100644 tests/testthat/_snaps/JAGS-diagnostics/diagnostics-plot-autocorrelation-3.svg create mode 100644 tests/testthat/_snaps/JAGS-diagnostics/diagnostics-plot-autocorrelation-4.svg create mode 100644 tests/testthat/_snaps/JAGS-diagnostics/diagnostics-plot-autocorrelation-5.svg create mode 100644 tests/testthat/_snaps/JAGS-diagnostics/diagnostics-plot-autocorrelation-6.svg create mode 100644 tests/testthat/_snaps/JAGS-diagnostics/diagnostics-plot-autocorrelation-7.svg create mode 100644 tests/testthat/_snaps/JAGS-diagnostics/diagnostics-plot-density-1.svg create mode 100644 tests/testthat/_snaps/JAGS-diagnostics/diagnostics-plot-density-2.svg create mode 100644 tests/testthat/_snaps/JAGS-diagnostics/diagnostics-plot-density-3.svg create mode 100644 tests/testthat/_snaps/JAGS-diagnostics/diagnostics-plot-density-4.svg create mode 100644 tests/testthat/_snaps/JAGS-diagnostics/diagnostics-plot-density-5.svg create mode 100644 tests/testthat/_snaps/JAGS-diagnostics/diagnostics-plot-density-6.svg create mode 100644 tests/testthat/_snaps/JAGS-diagnostics/diagnostics-plot-density-7.svg create mode 100644 tests/testthat/_snaps/JAGS-diagnostics/diagnostics-plot-trace-1.svg create mode 100644 tests/testthat/_snaps/JAGS-diagnostics/diagnostics-plot-trace-2.svg create mode 100644 tests/testthat/_snaps/JAGS-diagnostics/diagnostics-plot-trace-3.svg create mode 100644 tests/testthat/_snaps/JAGS-diagnostics/diagnostics-plot-trace-4.svg create mode 100644 tests/testthat/_snaps/JAGS-diagnostics/diagnostics-plot-trace-5.svg create mode 100644 tests/testthat/_snaps/JAGS-diagnostics/diagnostics-plot-trace-6.svg create mode 100644 tests/testthat/_snaps/JAGS-diagnostics/diagnostics-plot-trace-7.svg create mode 100644 tests/testthat/test-JAGS-diagnostics.R diff --git a/.github/workflows/R-CMD-check.yaml b/.github/workflows/R-CMD-check.yaml index 4df40fe7..9b069bdd 100644 --- a/.github/workflows/R-CMD-check.yaml +++ b/.github/workflows/R-CMD-check.yaml @@ -5,10 +5,12 @@ on: branches: - main - master + - pre-release pull_request: branches: - main - master + - pre-release name: R-CMD-check @@ -23,9 +25,9 @@ jobs: matrix: config: - {os: windows-latest, r: 'release'} - - {os: ubuntu-20.04, r: 'release', rspm: "https://packagemanager.rstudio.com/cran/__linux__/focal/latest"} - - {os: ubuntu-20.04, r: 'devel', rspm: "https://packagemanager.rstudio.com/cran/__linux__/focal/latest", http-user-agent: "R/4.1.0 (ubuntu-20.04) R (4.1.0 x86_64-pc-linux-gnu x86_64 linux-gnu) on GitHub Actions" } - #- {os: macOS-latest, r: 'release'} + - {os: ubuntu-20.04, r: 'release', rspm: "https://packagemanager.rstudio.com/cran/__linux__/focal/latest"} + - {os: ubuntu-20.04, r: 'devel', rspm: "https://packagemanager.rstudio.com/cran/__linux__/focal/latest", http-user-agent: "R/4.1.0 (ubuntu-20.04) R (4.1.0 x86_64-pc-linux-gnu x86_64 linux-gnu) on GitHub Actions" } + - {os: macOS-latest, r: 'release'} env: R_REMOTES_NO_ERRORS_FROM_WARNINGS: true @@ -66,7 +68,7 @@ jobs: - name: Install JAGS (windows-latest) if: runner.os == 'Windows' run: | - curl.exe -o wjags.exe --url https://deac-fra.dl.sourceforge.net/project/mcmc-jags/JAGS/4.x/Windows/JAGS-4.3.0.exe + curl.exe -o wjags.exe --url https://deac-fra.dl.sourceforge.net/project/mcmc-jags/JAGS/4.x/Windows/JAGS-4.2.0-Rtools33.exe wjags.exe /S del wjags.exe shell: cmd @@ -75,7 +77,7 @@ jobs: if: runner.os == 'macOS' run : | rm '/usr/local/bin/gfortran' - brew install jags + brew install automake jags - name: Install dependencies run: | @@ -84,13 +86,34 @@ jobs: shell: Rscript {0} - name: Check + if: runner.os != 'Windows' env: _R_CHECK_CRAN_INCOMING_REMOTE_: false run: | options(crayon.enabled = TRUE) - rcmdcheck::rcmdcheck(args = c("--no-manual", "--as-cran"), error_on = "warning", check_dir = "check") + rcmdcheck::rcmdcheck( + args = c("--no-manual", "--as-cran", "--ignore-vignettes"), + build_args = c("--no-build-vignettes"), + error_on = "warning", + check_dir = "check") + shell: Rscript {0}" + + - name: Check + if: runner.os == 'Windows' + env: + _R_CHECK_CRAN_INCOMING_REMOTE_: false + JAGS_ROOT: "/c/progra~1/JAGS/JAGS-4.2.0" + JAGS_MAJOR_VERSION: 4 + run: | + options(crayon.enabled = TRUE) + rcmdcheck::rcmdcheck( + args = c("--no-manual", "--as-cran", "--ignore-vignettes"), + build_args = c("--no-build-vignettes"), + error_on = "warning", + check_dir = "check") shell: Rscript {0} + - name: Upload check results if: failure() uses: actions/upload-artifact@main diff --git a/.github/workflows/pkgdown.yaml b/.github/workflows/pkgdown.yaml index 7d53a98a..25cdc7a6 100644 --- a/.github/workflows/pkgdown.yaml +++ b/.github/workflows/pkgdown.yaml @@ -62,7 +62,7 @@ jobs: - name: Install JAGS (windows-latest) if: runner.os == 'Windows' run: | - curl.exe -o wjags.exe --url https://deac-fra.dl.sourceforge.net/project/mcmc-jags/JAGS/4.x/Windows/JAGS-4.3.0.exe + curl.exe -o wjags.exe --url https://deac-fra.dl.sourceforge.net/project/mcmc-jags/JAGS/4.x/Windows/JAGS-4.2.0-Rtools33.exe wjags.exe /S del wjags.exe shell: cmd diff --git a/.github/workflows/test-coverage.yaml b/.github/workflows/test-coverage.yaml index 56e3e725..6406aee0 100644 --- a/.github/workflows/test-coverage.yaml +++ b/.github/workflows/test-coverage.yaml @@ -34,7 +34,7 @@ jobs: - name: Install JAGS (windows-latest) if: runner.os == 'Windows' run: | - curl.exe -o wjags.exe --url https://deac-fra.dl.sourceforge.net/project/mcmc-jags/JAGS/4.x/Windows/JAGS-4.3.0.exe + curl.exe -o wjags.exe --url https://deac-fra.dl.sourceforge.net/project/mcmc-jags/JAGS/4.x/Windows/JAGS-4.2.0-Rtools33.exe wjags.exe /S del wjags.exe shell: cmd diff --git a/DESCRIPTION b/DESCRIPTION index 1a59d135..6172e89e 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,6 +1,6 @@ Package: BayesTools Title: Tools for Bayesian Analyses -Version: 0.2.9 +Version: 0.2.10 Description: Provides tools for conducting Bayesian analyses. The package contains functions for creating a wide range of prior distribution objects, mixing posterior samples from 'JAGS' and 'Stan' models, plotting posterior distributions, and etc... diff --git a/NAMESPACE b/NAMESPACE index 679e38d6..b732e723 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -29,6 +29,10 @@ export(JAGS_bridgesampling_posterior) export(JAGS_check_and_list_autofit_settings) export(JAGS_check_and_list_fit_settings) export(JAGS_check_convergence) +export(JAGS_diagnostics) +export(JAGS_diagnostics_autocorrelation) +export(JAGS_diagnostics_density) +export(JAGS_diagnostics_trace) export(JAGS_evaluate_formula) export(JAGS_fit) export(JAGS_formula) diff --git a/NEWS.md b/NEWS.md index 847f95e9..4bc613f4 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,6 +1,10 @@ +## version 0.2.10 +### Features +- trace, density, and autocorrelation diagnostic plots for JAGS models + ## version 0.2.9 ### Fixes -- dealing with NaNs due to overflow with very large marginal likelihoods +- dealing with NaNs in inclusion Bayes factors due to overflow with very large marginal likelihoods ## version 0.2.8 ### Fixes diff --git a/R/JAGS-diagnostics.R b/R/JAGS-diagnostics.R new file mode 100644 index 00000000..aeeba160 --- /dev/null +++ b/R/JAGS-diagnostics.R @@ -0,0 +1,584 @@ +#' @title Plot diagnostics of a 'JAGS' model +#' +#' @description Creates density plots, trace plots, and autocorrelation plots +#' for a given parameter of a JAGS model. +#' +#' @param fit a JAGS model fitted via [JAGS_fit()] +#' @param parameter parameter to be plotted +#' @param type what type of model diagnostic should be plotted. The available +#' options are \code{"density"}, \code{"trace"}, and \code{"autocorrelation"} +#' @param plot_type whether to use a base plot \code{"base"} +#' or ggplot2 \code{"ggplot"} for plotting. +#' @param xlim x plotting range +#' @param ylim y plotting range +#' @param lags number of lags to be shown for the autocorrelation plot. +#' Defaults to \code{30}. +#' @param ... additional arguments +#' @inheritParams density.prior +#' @inheritParams plot.prior +#' @inheritParams print.prior +#' @inheritParams BayesTools_model_tables +#' +#' @seealso [JAGS_fit()] [JAGS_check_convergence()] +#' +#' @return \code{diagnostics} returns either \code{NULL} if \code{plot_type = "base"} +#' or an object/list of objects (depending on the number of parameters to be plotted) +#' of class 'ggplot2' if \code{plot_type = "ggplot2"}. +#' +#' @name JAGS_diagnostics +#' @aliases JAGS_diagnostics_density JAGS_diagnostics_autocorrelation JAGS_diagnostics_trace +#' @export JAGS_diagnostics +#' @export JAGS_diagnostics_density +#' @export JAGS_diagnostics_autocorrelation +#' @export JAGS_diagnostics_trace + +#' @rdname JAGS_diagnostics +JAGS_diagnostics <- function(fit, parameter, type, plot_type = "base", + xlim = NULL, ylim = NULL, lags = 30, n_points = 1000, + transformations = NULL, transform_orthonormal = FALSE, + short_name = FALSE, parameter_names = FALSE, formula_prefix = TRUE, ...){ + + + # check fits + if(!inherits(fit, "runjags")) + stop("'fit' must be a runjags fit") + if(!inherits(fit, "BayesTools_fit")) + stop("'fit' must be a BayesTools fit") + + check_char(type, "type", allow_values = c("density", "trace", "autocorrelation")) + check_char(plot_type, "plot_type", allow_values = c("base", "ggplot")) + prior_list <- attr(fit, "prior_list") + check_list(prior_list, "prior_list") + if(!all(sapply(prior_list, is.prior))) + stop("'prior_list' must be a list of priors.") + check_char(parameter, "parameter", allow_values = names(prior_list)) + if(!is.null(transformations)) + check_char(names(transformations), "names(transformations)", allow_values = parameter) + + # do not produce diagnostics for a spike prior + if(is.prior.point(prior_list[[parameter]])){ + message("No diagnostic plots are produced for a spike prior distribution") + return(NULL) + } + + # prepare the plot data + plot_data <- .diagnostics_plot_data(fit = fit, parameter = parameter, prior_list = prior_list, transformations = transformations, transform_orthonormal = transform_orthonormal) + plot_data <- switch( + type, + "density" = .diagnostics_plot_data_density(plot_data, n_points, xlim), + "trace" = .diagnostics_plot_data_trace(plot_data, n_points, ylim), + "autocorrelation" = .diagnostics_plot_data_autocorrelation(plot_data, n_points, lags) + ) + + + # prepare nice parameter names + if(!is.null(attr(prior_list[[parameter]], "parameter"))){ + parameter_name <- format_parameter_names(attr(plot_data, "parameter_name"), attr(prior_list[[parameter]], "parameter"), formula_prefix = formula_prefix) + }else{ + parameter_name <- attr(plot_data, "parameter_name") + } + + # get default plot settings + dots <- list(...) + main <- print(prior_list[[parameter]], plot = TRUE, short_name = short_name, parameter_names = parameter_names) + xlab <- switch( + type, + "density" = parameter_name, + "trace" = "Iteration", + "autocorrelation" = "Lag" + ) + ylab <- switch( + type, + "density" = "Density", + "trace" = parameter_name, + "autocorrelation" = paste0("Autocorrelation(", parameter_name, ")") + ) + + + if(is.null(dots[["main"]])) dots$main <- main + if(is.null(dots[["xlab"]])) dots$xlab <- xlab + if(is.null(dots[["ylab"]])) dots$ylab <- ylab + + if(is.null(dots[["col"]])) dots$col <- "black" + if(is.null(dots[["lty"]])) dots$lty <- 1 + if(is.null(dots[["lwd"]])) dots$lwd <- 1 + + if(length(dots[["col"]]) == 1) dots$col <- rep(dots$col, attr(plot_data, "chains")) + if(length(dots[["lty"]]) == 1) dots$lty <- rep(dots$lty, attr(plot_data, "chains")) + if(length(dots[["lwd"]]) == 1) dots$lwd <- rep(dots$lwd, attr(plot_data, "chains")) + + plots <- list() + + for(i in seq_along(plot_data)){ + + temp_dots <- dots + + temp_dots[["xlab"]] <- if(length(temp_dots[["xlab"]]) > 1) temp_dots[["xlab"]][i] else temp_dots[["xlab"]] + temp_dots[["ylab"]] <- if(length(temp_dots[["ylab"]]) > 1) temp_dots[["ylab"]][i] else temp_dots[["ylab"]] + + if(is.null(temp_dots[["xlim"]])) temp_dots$xlim <- attr(plot_data[[i]], "x_range") + if(is.null(temp_dots[["ylim"]])) temp_dots$ylim <- attr(plot_data[[i]], "y_range") + + + if(plot_type == "ggplot"){ + plots[[i]] <- .ggplot.prior_empty("simple", temp_dots) + }else{ + .plot.prior_empty("simple", temp_dots) + } + + for(j in seq_along(plot_data[[i]])){ + + temp_args <- list() + temp_args[["plot_data"]] <- plot_data[[i]][[j]] + temp_args[["col"]] <- dots[["col"]][j] + temp_args[["lwd"]] <- dots[["lwd"]][j] + temp_args[["lty"]] <- dots[["lty"]][j] + + if(plot_type == "ggplot"){ + plots[[i]] <- plots[[i]] + do.call(switch( + type, + "density" = .geom_diagnostics.density, + "trace" = .geom_diagnostics.trace, + "autocorrelation" = .geom_diagnostics.autocorrelation + ), temp_args) + }else{ + do.call(switch( + type, + "density" = .lines_diagnostics.density, + "trace" = .lines_diagnostics.trace, + "autocorrelation" = .lines_diagnostics.autocorrelation + ), temp_args) + } + + } + } + + + # return the plots + if(plot_type == "base"){ + return(invisible()) + }else if(plot_type == "ggplot"){ + if(length(plots) == 1){ + plots <- plots[[1]] + } + return(plots) + } + + +} + +#' @rdname JAGS_diagnostics +JAGS_diagnostics_density <- function(fit, parameter, plot_type = "base", + xlim = NULL, n_points = 1000, + transformations = NULL, transform_orthonormal = FALSE, + short_name = FALSE, parameter_names = FALSE, formula_prefix = TRUE, ...){ + + JAGS_diagnostics(fit = fit, parameter = parameter, plot_type = plot_type, type = "density", + xlim = xlim, n_points = n_points, + transformations = transformations, transform_orthonormal = transform_orthonormal, + short_name = short_name, parameter_names = parameter_names, formula_prefix = formula_prefix, ...) +} + +#' @rdname JAGS_diagnostics +JAGS_diagnostics_trace <- function(fit, parameter, plot_type = "base", + ylim = NULL, + transformations = NULL, transform_orthonormal = FALSE, + short_name = FALSE, parameter_names = FALSE, formula_prefix = TRUE, ...){ + + JAGS_diagnostics(fit = fit, parameter = parameter, plot_type = plot_type, type = "trace", + ylim = ylim, + transformations = transformations, transform_orthonormal = transform_orthonormal, + short_name = short_name, parameter_names = parameter_names, formula_prefix = formula_prefix, ...) +} + +#' @rdname JAGS_diagnostics +JAGS_diagnostics_autocorrelation <- function(fit, parameter, plot_type = "base", + lags = 30, + transformations = NULL, transform_orthonormal = FALSE, + short_name = FALSE, parameter_names = FALSE, formula_prefix = TRUE, ...){ + + JAGS_diagnostics(fit = fit, parameter = parameter, plot_type = plot_type, type = "autocorrelation", + lags = lags, + transformations = transformations, transform_orthonormal = transform_orthonormal, + short_name = short_name, parameter_names = parameter_names, formula_prefix = formula_prefix, ...) +} + + +.diagnostics_plot_data <- function(fit, parameter, prior_list, transformations, transform_orthonormal){ + + check_list(transformations, "transformations", allow_NULL = TRUE) + if(!is.null(transformations) && any(!sapply(transformations, function(trans)is.function(trans[["fun"]])))) + stop("'transformations' must be list of functions in the 'fun' element.") + + model_samples <- coda::as.mcmc.list(fit) + samples_chain <- lapply(seq_along(model_samples), function(i) { + return(rep(i, nrow(model_samples[[i]]))) + }) + samples_iter <- lapply(seq_along(model_samples), function(i) { + return(1:nrow(model_samples[[i]])) + }) + model_samples <- do.call(rbind, model_samples) + + + # extract the relevant parameters + if(is.prior.factor(prior_list[[parameter]])){ + if(attr(prior_list[[parameter]], "levels") > 2){ + model_samples <- model_samples[,paste0(parameter, "[", 1:(attr(prior_list[[parameter]], "levels")-1), "]"),drop = FALSE] + }else{ + model_samples <- model_samples[,parameter,drop = FALSE] + } + }else if(is.prior.weightfunction(prior_list[[parameter]])){ + model_samples <- model_samples[,paste0("omega", "[", (length(weightfunctions_mapping(list(prior_list[[parameter]]), cuts_only = TRUE)) - 2):1, "]"),drop = FALSE] + }else if(is.prior.vector(prior_list[[parameter]])){ + if(prior_list[[parameter]]$parameters[["K"]] > 1){ + model_samples <- model_samples[,paste0(parameter, "[", 1:prior_list[[parameter]]$parameters[["K"]], "]"),drop = FALSE] + }else{ + model_samples <- model_samples[,parameter,drop = FALSE] + } + }else{ + if(is.prior.PET(prior_list[[parameter]])){ + parameter <- "PET" + }else if(is.prior.PEESE(prior_list[[parameter]])){ + parameter <- "PEESE" + } + model_samples <- model_samples[,parameter,drop = FALSE] + } + + prior_list <- prior_list[parameter] + parameter_names <- parameter + + # mostly adapted from runjags_estimates_table + # apply transformations + if(!is.null(transformations)){ + for(par in names(transformations)){ + model_samples[,par] <- do.call(transformations[[par]][["fun"]], c(list(model_samples[,par]), transformations[[par]][["arg"]])) + } + } + + # transform orthonormal factors to differences from runjags_estimates_table + if(transform_orthonormal & any(sapply(prior_list, is.prior.orthonormal))){ + for(par in names(prior_list)[sapply(prior_list, is.prior.orthonormal)]){ + + if((attr(prior_list[[par]], "levels") - 1) == 1){ + par_names <- par + }else{ + par_names <- paste0(par, "[", 1:(attr(prior_list[[par]], "levels") - 1), "]") + } + + orthonormal_samples <- model_samples[,par_names,drop = FALSE] + model_samples <- orthonormal_samples %*% t(contr.orthonormal(1:attr(prior_list[[par]], "levels"))) + + if(attr(prior_list[[par]], "interaction")){ + if(length(attr(prior_list[[par]], "level_names")) == 1){ + parameter_names <- paste0(par, " [dif: ", attr(prior_list[[par]], "level_names")[[1]],"]") + }else{ + stop("orthonormal de-transformation for interaction of multiple factors is not implemented.") + } + }else{ + parameter_names <- paste0(par, " [dif: ", attr(prior_list[[par]], "level_names"),"]") + } + } + }else if(any(sapply(prior_list, is.prior.orthonormal))){ + for(par in names(prior_list)[sapply(prior_list, is.prior.orthonormal)]){ + if((attr(prior_list[[par]], "levels") - 1) == 1){ + parameter_names <- par + }else{ + parameter_names <- paste0(par, "[", 1:(attr(prior_list[[par]], "levels") - 1), "]") + } + } + } + + # rename treatment factor levels + if(any(sapply(prior_list, is.prior.dummy))){ + for(par in names(prior_list)[sapply(prior_list, is.prior.dummy)]){ + if(!attr(prior_list[[par]], "interaction")){ + if(attr(prior_list[[par]], "levels") == 2){ + parameter_names <- paste0(par,"[",attr(prior_list[[par]], "level_names")[-1], "]") + }else{ + parameter_names <- paste0(par,"[",attr(prior_list[[par]], "level_names")[-1], "]") + } + }else if(length(attr(prior_list[[par]], "levels")) == 1){ + parameter_names <- paste0(par,"[",attr(prior_list[[par]], "level_names")[[1]][-1], "]") + } + } + } + + # rename weightfunctions factor levels + if(any(sapply(prior_list, is.prior.weightfunction))){ + for(par in names(prior_list)[sapply(prior_list, is.prior.weightfunction)]){ + omega_cuts <- weightfunctions_mapping(prior_list[par], cuts_only = TRUE) + parameter_names <- sapply(1:(length(omega_cuts)-1), function(i)paste0("omega[",omega_cuts[i],",",omega_cuts[i+1],"]")) + parameter_names <- parameter_names[-1] + } + } + + # attach the relevant attributes + colnames(model_samples) <- parameter_names + attr(model_samples, "chain") <- do.call(c, samples_chain) + attr(model_samples, "iter") <- do.call(c, samples_iter) + attr(model_samples, "parameter") <- parameter + attr(model_samples, "prior") <- prior_list[[parameter]] + + return(model_samples) +} +.diagnostics_plot_data_density <- function(plot_data, n_points, xlim){ + + chain <- attr(plot_data, "chain") + prior <- attr(plot_data, "prior") + + if(is.prior.weightfunction(prior)){ + prior_lower <- 0 + prior_upper <- 1 + }else{ + prior_lower <- prior$truncation[["lower"]] + prior_upper <- prior$truncation[["upper"]] + } + + out <- list() + + for(i in 1:ncol(plot_data)){ + + if(is.null(xlim)){ + x_range <- range(plot_data[,i]) + }else{ + x_range <- xlim + } + + for(j in seq_along(unique(chain))){ + + temp_args <- list(x = plot_data[chain == j,i], n = n_points, from = x_range[1], to = x_range[2]) + temp_density <- do.call(stats::density, temp_args) + + x_den <- temp_density$x + y_den <- temp_density$y + + # check for truncation + if(isTRUE(all.equal(prior_lower, x_den[1])) | prior_lower >= x_den[1]){ + y_den <- c(0, y_den) + x_den <- c(x_den[1], x_den) + } + if(isTRUE(all.equal(prior_upper, x_den[length(x_den)])) | prior_upper <= x_den[length(x_den)]){ + y_den <- c(y_den, 0) + x_den <- c(x_den, x_den[length(x_den)]) + } + + temp_density <- list( + call = call("density"), + bw = NULL, + n = n_points, + x = x_den, + y = y_den + ) + + class(temp_density) <- c("density") + attr(temp_density, "x_range") <- range(x_den) + attr(temp_density, "y_range") <- c(0, max(y_den)) + attr(temp_density, "chain") <- j + attr(temp_density, "parameter") <- attr(plot_data, "parameter") + attr(temp_density, "parameter_name") <- colnames(plot_data)[i] + + out[[colnames(plot_data)[[i]]]][[j]] <- temp_density + } + + attr(out[[colnames(plot_data)[[i]]]], "x_range") <- x_range + attr(out[[colnames(plot_data)[[i]]]], "y_range") <- c(0, max(sapply(out[[i]], function(x) attr(x, "y_range")))) + attr(out[[colnames(plot_data)[[i]]]], "chains") <- length(unique(chain)) + attr(out[[colnames(plot_data)[[i]]]], "parameter") <- attr(plot_data, "parameter") + attr(out[[colnames(plot_data)[[i]]]], "parameter_name") <- colnames(plot_data)[i] + } + + attr(out, "chains") <- length(unique(chain)) + attr(out, "parameter") <- attr(plot_data, "parameter") + attr(out, "parameter_name") <- colnames(plot_data) + + return(out) +} +.diagnostics_plot_data_trace <- function(plot_data, n_points, ylim){ + + chain <- attr(plot_data, "chain") + iter <- attr(plot_data, "iter") + prior <- attr(plot_data, "prior") + + out <- list() + + for(i in 1:ncol(plot_data)){ + + if(is.null(ylim)){ + y_range <- range(plot_data[,i]) + }else{ + y_range <- ylim + } + x_range <- range(iter) + + for(j in seq_along(unique(chain))){ + + temp_x <- iter[chain == j] + temp_y <- plot_data[chain == j,i] + + temp_chain <- list( + x = temp_x, + y = temp_y + ) + + class(temp_chain) <- c("BayesTools_chain") + attr(temp_chain, "x_range") <- x_range + attr(temp_chain, "y_range") <- y_range + attr(temp_chain, "chain") <- j + attr(temp_chain, "parameter") <- attr(plot_data, "parameter") + attr(temp_chain, "parameter_name") <- colnames(plot_data)[i] + + out[[colnames(plot_data)[[i]]]][[j]] <- temp_chain + } + + attr(out[[colnames(plot_data)[[i]]]], "x_range") <- x_range + attr(out[[colnames(plot_data)[[i]]]], "y_range") <- y_range + attr(out[[colnames(plot_data)[[i]]]], "chains") <- length(unique(chain)) + attr(out[[colnames(plot_data)[[i]]]], "parameter") <- attr(plot_data, "parameter") + attr(out[[colnames(plot_data)[[i]]]], "parameter_name") <- colnames(plot_data)[i] + } + + attr(out, "chains") <- length(unique(chain)) + attr(out, "parameter") <- attr(plot_data, "parameter") + attr(out, "parameter_name") <- colnames(plot_data) + + return(out) +} +.diagnostics_plot_data_autocorrelation <- function(plot_data, n_points, lags){ + + chain <- attr(plot_data, "chain") + iter <- attr(plot_data, "iter") + prior <- attr(plot_data, "prior") + + out <- list() + + for(i in 1:ncol(plot_data)){ + + x_range <- + + for(j in seq_along(unique(chain))){ + + temp_x <- 0:lags + temp_y <- stats::acf(plot_data[chain == j,i], lag.max = lags, plot = FALSE)$acf[, , 1L] + + + temp_autocor <- list( + x = temp_x, + y = temp_y + ) + + class(temp_autocor) <- c("BayesTools_autocorrelation") + attr(temp_autocor, "x_range") <- c(0, lags) + attr(temp_autocor, "y_range") <- range(c(0, temp_y)) + attr(temp_autocor, "chain") <- j + attr(temp_autocor, "parameter") <- attr(plot_data, "parameter") + attr(temp_autocor, "parameter_name") <- colnames(plot_data)[i] + + out[[colnames(plot_data)[[i]]]][[j]] <- temp_autocor + } + + attr(out[[colnames(plot_data)[[i]]]], "x_range") <- c(0, lags) + attr(out[[colnames(plot_data)[[i]]]], "y_range") <- c(0, max(sapply(out[[i]], function(x) attr(x, "y_range")))) + attr(out[[colnames(plot_data)[[i]]]], "chains") <- length(unique(chain)) + attr(out[[colnames(plot_data)[[i]]]], "parameter") <- attr(plot_data, "parameter") + attr(out[[colnames(plot_data)[[i]]]], "parameter_name") <- colnames(plot_data)[i] + } + + attr(out, "chains") <- length(unique(chain)) + attr(out, "parameter") <- attr(plot_data, "parameter") + attr(out, "parameter_name") <- colnames(plot_data) + + return(out) +} + + +.lines_diagnostics.density <- function(plot_data, ...){ + + dots <- list(...) + col <- if(!is.null(dots[["col"]])) dots[["col"]] else .plot.prior_settings()[["col"]] + lwd <- if(!is.null(dots[["lwd"]])) dots[["lwd"]] else .plot.prior_settings()[["lwd"]] + lty <- if(!is.null(dots[["lty"]])) dots[["lty"]] else .plot.prior_settings()[["lty"]] + + + graphics::lines(x = plot_data$x, y = plot_data$y, type = "l", lwd = lwd, lty = lty, col = col) + + return(invisible()) +} +.lines_diagnostics.trace <- function(plot_data, ...){ + + dots <- list(...) + col <- if(!is.null(dots[["col"]])) dots[["col"]] else .plot.prior_settings()[["col"]] + lwd <- if(!is.null(dots[["lwd"]])) dots[["lwd"]] else .plot.prior_settings()[["lwd"]] + lty <- if(!is.null(dots[["lty"]])) dots[["lty"]] else .plot.prior_settings()[["lty"]] + + + graphics::lines(x = plot_data$x, y = plot_data$y, type = "l", lwd = lwd, lty = lty, col = col) + + return(invisible()) +} +.lines_diagnostics.autocorrelation <- function(plot_data, ...){ + + dots <- list(...) + col <- if(!is.null(dots[["col"]])) dots[["col"]] else .plot.prior_settings()[["col"]] + + graphics::rect( + xleft = plot_data$x + 0.075 - 0.5, + ybottom = 0, + xright = plot_data$x + 0.925 - 0.5, + ytop = plot_data$y, + col = col) + + return(invisible()) +} + +.geom_diagnostics.density <- function(plot_data, ...){ + + dots <- list(...) + col <- if(!is.null(dots[["col"]])) dots[["col"]] else .plot.prior_settings()[["col"]] + lwd <- if(!is.null(dots[["size"]])) dots[["size"]] else if(!is.null(dots[["lwd"]])) dots[["lwd"]] else .plot.prior_settings()[["lwd"]] + lty <- if(!is.null(dots[["linetype"]])) dots[["linetype"]] else if(!is.null(dots[["lty"]])) dots[["lty"]] else .plot.prior_settings()[["lty"]] + + geom <- ggplot2::geom_line( + data = data.frame( + x = plot_data$x, + y = plot_data$y), + mapping = ggplot2::aes_string( + x = "x", + y = "y"), + size = lwd, linetype = lty, color = col) + + return(geom) +} +.geom_diagnostics.trace <- function(plot_data, ...){ + + dots <- list(...) + col <- if(!is.null(dots[["col"]])) dots[["col"]] else .plot.prior_settings()[["col"]] + lwd <- if(!is.null(dots[["size"]])) dots[["size"]] else if(!is.null(dots[["lwd"]])) dots[["lwd"]] else .plot.prior_settings()[["lwd"]] + lty <- if(!is.null(dots[["linetype"]])) dots[["linetype"]] else if(!is.null(dots[["lty"]])) dots[["lty"]] else .plot.prior_settings()[["lty"]] + + geom <- ggplot2::geom_line( + data = data.frame( + x = plot_data$x, + y = plot_data$y), + mapping = ggplot2::aes_string( + x = "x", + y = "y"), + size = lwd, linetype = lty, color = col) + + return(geom) +} +.geom_diagnostics.autocorrelation <- function(plot_data, ...){ + + dots <- list(...) + col <- if(!is.null(dots[["col"]])) dots[["col"]] else .plot.prior_settings()[["col"]] + + geom <- ggplot2::geom_bar( + data = data.frame( + x = plot_data$x, + y = plot_data$y), + mapping = ggplot2::aes_string( + x = "x", + weight = "y"), + color = col, fill = col) + + return(geom) +} diff --git a/R/JAGS-formula.R b/R/JAGS-formula.R index f8432560..f5ec7b16 100644 --- a/R/JAGS-formula.R +++ b/R/JAGS-formula.R @@ -417,7 +417,7 @@ transform_orthonormal_samples <- function(samples){ } -#' @title Orthornomal contrast natrix +#' @title Orthornomal contrast matrix #' #' @description Return a matrix of orthornomal contrasts. #' Code is based on \code{stanova::contr.bayes} and corresponding to description diff --git a/R/summary-tables.R b/R/summary-tables.R index 0bd0124d..77eb2f6b 100644 --- a/R/summary-tables.R +++ b/R/summary-tables.R @@ -524,6 +524,8 @@ model_summary_table <- function(model, model_description = NULL, title = NULL, f #' @rdname BayesTools_model_tables runjags_estimates_table <- function(fit, transformations = NULL, title = NULL, footnotes = NULL, warnings = NULL, remove_spike_0 = TRUE, transform_orthonormal = FALSE, formula_prefix = TRUE){ + # most of the code is shared with .diagnostics_plot_data function (keep them in sync on update) + # check fits if(!inherits(fit, "runjags")) stop("'fit' must be a runjags fit") diff --git a/inst/REFERENCES.bib b/inst/REFERENCES.bib index 8a76ef04..e0b570c5 100644 --- a/inst/REFERENCES.bib +++ b/inst/REFERENCES.bib @@ -197,7 +197,7 @@ @article{vevea1995general @article{rouder2012default, title = {Default {B}ayes factors for {ANOVA} designs}, author = {Rouder, Jeffrey N and Morey, Richard D and Speckman, Paul L and Province, Jordan M}, - journal = {Journal of mathematical psychology}, + journal = {Journal of Mathematical Psychology}, volume = {56}, number = {5}, pages = {356--374}, diff --git a/man/JAGS_diagnostics.Rd b/man/JAGS_diagnostics.Rd new file mode 100644 index 00000000..81c645be --- /dev/null +++ b/man/JAGS_diagnostics.Rd @@ -0,0 +1,117 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/JAGS-diagnostics.R +\name{JAGS_diagnostics} +\alias{JAGS_diagnostics} +\alias{JAGS_diagnostics_density} +\alias{JAGS_diagnostics_autocorrelation} +\alias{JAGS_diagnostics_trace} +\title{Plot diagnostics of a 'JAGS' model} +\usage{ +JAGS_diagnostics( + fit, + parameter, + type, + plot_type = "base", + xlim = NULL, + ylim = NULL, + lags = 30, + n_points = 1000, + transformations = NULL, + transform_orthonormal = FALSE, + short_name = FALSE, + parameter_names = FALSE, + formula_prefix = TRUE, + ... +) + +JAGS_diagnostics_density( + fit, + parameter, + plot_type = "base", + xlim = NULL, + n_points = 1000, + transformations = NULL, + transform_orthonormal = FALSE, + short_name = FALSE, + parameter_names = FALSE, + formula_prefix = TRUE, + ... +) + +JAGS_diagnostics_trace( + fit, + parameter, + plot_type = "base", + ylim = NULL, + transformations = NULL, + transform_orthonormal = FALSE, + short_name = FALSE, + parameter_names = FALSE, + formula_prefix = TRUE, + ... +) + +JAGS_diagnostics_autocorrelation( + fit, + parameter, + plot_type = "base", + lags = 30, + transformations = NULL, + transform_orthonormal = FALSE, + short_name = FALSE, + parameter_names = FALSE, + formula_prefix = TRUE, + ... +) +} +\arguments{ +\item{fit}{a JAGS model fitted via \code{\link[=JAGS_fit]{JAGS_fit()}}} + +\item{parameter}{parameter to be plotted} + +\item{type}{what type of model diagnostic should be plotted. The available +options are \code{"density"}, \code{"trace"}, and \code{"autocorrelation"}} + +\item{plot_type}{whether to use a base plot \code{"base"} +or ggplot2 \code{"ggplot"} for plotting.} + +\item{xlim}{x plotting range} + +\item{ylim}{y plotting range} + +\item{lags}{number of lags to be shown for the autocorrelation plot. +Defaults to \code{30}.} + +\item{n_points}{number of equally spaced points +in the \code{x_range} if \code{x_seq} is unspecified} + +\item{transformations}{named list of transformations to be applied +to specific parameters} + +\item{transform_orthonormal}{whether factors with orthonormal prior +distributions should be transformed to differences from the grand +mean} + +\item{short_name}{whether prior distribution names should be +shorted} + +\item{parameter_names}{whether parameter names should be +printed} + +\item{formula_prefix}{whether the parameter prefix from formula should +be printed. Defaults to \code{TRUE}.} + +\item{...}{additional arguments} +} +\value{ +\code{diagnostics} returns either \code{NULL} if \code{plot_type = "base"} +or an object/list of objects (depending on the number of parameters to be plotted) +of class 'ggplot2' if \code{plot_type = "ggplot2"}. +} +\description{ +Creates density plots, trace plots, and autocorrelation plots +for a given parameter of a JAGS model. +} +\seealso{ +\code{\link[=JAGS_fit]{JAGS_fit()}} \code{\link[=JAGS_check_convergence]{JAGS_check_convergence()}} +} diff --git a/man/contr.orthonormal.Rd b/man/contr.orthonormal.Rd index 121b7d79..f71a2c7f 100644 --- a/man/contr.orthonormal.Rd +++ b/man/contr.orthonormal.Rd @@ -2,7 +2,7 @@ % Please edit documentation in R/JAGS-formula.R \name{contr.orthonormal} \alias{contr.orthonormal} -\title{Orthornomal contrast natrix} +\title{Orthornomal contrast matrix} \usage{ contr.orthonormal(n, contrasts = TRUE) } diff --git a/tests/testthat/_snaps/JAGS-diagnostics/diagnostics-ggplot-autocorrelation-1.svg b/tests/testthat/_snaps/JAGS-diagnostics/diagnostics-ggplot-autocorrelation-1.svg new file mode 100644 index 00000000..5a151df2 --- /dev/null +++ b/tests/testthat/_snaps/JAGS-diagnostics/diagnostics-ggplot-autocorrelation-1.svg @@ -0,0 +1,211 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +0.0 +0.2 +0.4 +0.6 +0.8 +1.0 + + + + + + + + + + + + + +0 +5 +10 +15 +20 +25 +30 +Lag +Autocorrelation(x_cont1) +Normal +(0, 1) + + diff --git a/tests/testthat/_snaps/JAGS-diagnostics/diagnostics-ggplot-autocorrelation-2-1.svg b/tests/testthat/_snaps/JAGS-diagnostics/diagnostics-ggplot-autocorrelation-2-1.svg new file mode 100644 index 00000000..37e63e2a --- /dev/null +++ b/tests/testthat/_snaps/JAGS-diagnostics/diagnostics-ggplot-autocorrelation-2-1.svg @@ -0,0 +1,212 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +0.0 +0.2 +0.4 +0.6 +0.8 +1.0 + + + + + + + + + + + + + +0 +5 +10 +15 +20 +25 +30 +Lag +Autocorrelation((mu) x_fac3o [dif: A]) +orthonormal contrast: +mNormal +(0, 1) + + diff --git a/tests/testthat/_snaps/JAGS-diagnostics/diagnostics-ggplot-autocorrelation-2-2.svg b/tests/testthat/_snaps/JAGS-diagnostics/diagnostics-ggplot-autocorrelation-2-2.svg new file mode 100644 index 00000000..5f9dd3d6 --- /dev/null +++ b/tests/testthat/_snaps/JAGS-diagnostics/diagnostics-ggplot-autocorrelation-2-2.svg @@ -0,0 +1,212 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +0.0 +0.2 +0.4 +0.6 +0.8 +1.0 + + + + + + + + + + + + + +0 +5 +10 +15 +20 +25 +30 +Lag +Autocorrelation((mu) x_fac3o [dif: B]) +orthonormal contrast: +mNormal +(0, 1) + + diff --git a/tests/testthat/_snaps/JAGS-diagnostics/diagnostics-ggplot-autocorrelation-2-3.svg b/tests/testthat/_snaps/JAGS-diagnostics/diagnostics-ggplot-autocorrelation-2-3.svg new file mode 100644 index 00000000..cecee2f9 --- /dev/null +++ b/tests/testthat/_snaps/JAGS-diagnostics/diagnostics-ggplot-autocorrelation-2-3.svg @@ -0,0 +1,212 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +0.0 +0.2 +0.4 +0.6 +0.8 +1.0 + + + + + + + + + + + + + +0 +5 +10 +15 +20 +25 +30 +Lag +Autocorrelation((mu) x_fac3o [dif: C]) +orthonormal contrast: +mNormal +(0, 1) + + diff --git a/tests/testthat/_snaps/JAGS-diagnostics/diagnostics-ggplot-autocorrelation-3-1.svg b/tests/testthat/_snaps/JAGS-diagnostics/diagnostics-ggplot-autocorrelation-3-1.svg new file mode 100644 index 00000000..a4903b04 --- /dev/null +++ b/tests/testthat/_snaps/JAGS-diagnostics/diagnostics-ggplot-autocorrelation-3-1.svg @@ -0,0 +1,219 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +0.0 +0.2 +0.4 +0.6 +0.8 +1.0 + + + + + + + + + + + + + +0 +5 +10 +15 +20 +25 +30 +Lag +Autocorrelation(omega[0.05,0.1]) +ω +one-sided: +.1, .05 + +~ + +CumDirichlet +( +1, 1, 1 +) + + diff --git a/tests/testthat/_snaps/JAGS-diagnostics/diagnostics-ggplot-autocorrelation-3-2.svg b/tests/testthat/_snaps/JAGS-diagnostics/diagnostics-ggplot-autocorrelation-3-2.svg new file mode 100644 index 00000000..d18265eb --- /dev/null +++ b/tests/testthat/_snaps/JAGS-diagnostics/diagnostics-ggplot-autocorrelation-3-2.svg @@ -0,0 +1,219 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +0.0 +0.2 +0.4 +0.6 +0.8 +1.0 + + + + + + + + + + + + + +0 +5 +10 +15 +20 +25 +30 +Lag +Autocorrelation(omega[0.1,1]) +ω +one-sided: +.1, .05 + +~ + +CumDirichlet +( +1, 1, 1 +) + + diff --git a/tests/testthat/_snaps/JAGS-diagnostics/diagnostics-ggplot-density-1.svg b/tests/testthat/_snaps/JAGS-diagnostics/diagnostics-ggplot-density-1.svg new file mode 100644 index 00000000..54b50915 --- /dev/null +++ b/tests/testthat/_snaps/JAGS-diagnostics/diagnostics-ggplot-density-1.svg @@ -0,0 +1,83 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +0 +1 +2 +3 +4 + + + + + + + + + + + +1.0 +1.2 +1.4 +1.6 +1.8 +2.0 +x_cont1 +Density +Normal +(0, 1) + + diff --git a/tests/testthat/_snaps/JAGS-diagnostics/diagnostics-ggplot-density-2-1.svg b/tests/testthat/_snaps/JAGS-diagnostics/diagnostics-ggplot-density-2-1.svg new file mode 100644 index 00000000..9f66fc5c --- /dev/null +++ b/tests/testthat/_snaps/JAGS-diagnostics/diagnostics-ggplot-density-2-1.svg @@ -0,0 +1,92 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +0 +1 +2 +3 +4 +5 + + + + + + + + + + + + + +-0.6 +-0.4 +-0.2 +0.0 +0.2 +0.4 +0.6 +(mu) x_fac3o [dif: A] +Density +orthonormal contrast: +mNormal +(0, 1) + + diff --git a/tests/testthat/_snaps/JAGS-diagnostics/diagnostics-ggplot-density-2-2.svg b/tests/testthat/_snaps/JAGS-diagnostics/diagnostics-ggplot-density-2-2.svg new file mode 100644 index 00000000..5bf6eaf9 --- /dev/null +++ b/tests/testthat/_snaps/JAGS-diagnostics/diagnostics-ggplot-density-2-2.svg @@ -0,0 +1,84 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +0 +1 +2 +3 +4 +5 + + + + + + + + + + + +-0.6 +-0.4 +-0.2 +0.0 +0.2 +(mu) x_fac3o [dif: B] +Density +orthonormal contrast: +mNormal +(0, 1) + + diff --git a/tests/testthat/_snaps/JAGS-diagnostics/diagnostics-ggplot-density-2-3.svg b/tests/testthat/_snaps/JAGS-diagnostics/diagnostics-ggplot-density-2-3.svg new file mode 100644 index 00000000..91370f79 --- /dev/null +++ b/tests/testthat/_snaps/JAGS-diagnostics/diagnostics-ggplot-density-2-3.svg @@ -0,0 +1,84 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +0 +1 +2 +3 +4 +5 + + + + + + + + + + + +-0.2 +0.0 +0.2 +0.4 +0.6 +(mu) x_fac3o [dif: C] +Density +orthonormal contrast: +mNormal +(0, 1) + + diff --git a/tests/testthat/_snaps/JAGS-diagnostics/diagnostics-ggplot-density-3-1.svg b/tests/testthat/_snaps/JAGS-diagnostics/diagnostics-ggplot-density-3-1.svg new file mode 100644 index 00000000..a2eaf2d2 --- /dev/null +++ b/tests/testthat/_snaps/JAGS-diagnostics/diagnostics-ggplot-density-3-1.svg @@ -0,0 +1,91 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +0.0 +0.5 +1.0 +1.5 +2.0 + + + + + + + + + + + +0.0 +0.2 +0.4 +0.6 +0.8 +1.0 +omega[0.05,0.1] +Density +ω +one-sided: +.1, .05 + +~ + +CumDirichlet +( +1, 1, 1 +) + + diff --git a/tests/testthat/_snaps/JAGS-diagnostics/diagnostics-ggplot-density-3-2.svg b/tests/testthat/_snaps/JAGS-diagnostics/diagnostics-ggplot-density-3-2.svg new file mode 100644 index 00000000..fdadc8d3 --- /dev/null +++ b/tests/testthat/_snaps/JAGS-diagnostics/diagnostics-ggplot-density-3-2.svg @@ -0,0 +1,91 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +0.0 +0.5 +1.0 +1.5 +2.0 + + + + + + + + + + + +0.0 +0.2 +0.4 +0.6 +0.8 +1.0 +omega[0.1,1] +Density +ω +one-sided: +.1, .05 + +~ + +CumDirichlet +( +1, 1, 1 +) + + diff --git a/tests/testthat/_snaps/JAGS-diagnostics/diagnostics-ggplot-trace-1.svg b/tests/testthat/_snaps/JAGS-diagnostics/diagnostics-ggplot-trace-1.svg new file mode 100644 index 00000000..ffdd6a3b --- /dev/null +++ b/tests/testthat/_snaps/JAGS-diagnostics/diagnostics-ggplot-trace-1.svg @@ -0,0 +1,83 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +1.0 +1.2 +1.4 +1.6 +1.8 +2.0 + + + + + + + + + + + +0 +1000 +2000 +3000 +4000 +Iteration +x_cont1 +Normal +(0, 1) + + diff --git a/tests/testthat/_snaps/JAGS-diagnostics/diagnostics-ggplot-trace-2-1.svg b/tests/testthat/_snaps/JAGS-diagnostics/diagnostics-ggplot-trace-2-1.svg new file mode 100644 index 00000000..ad900953 --- /dev/null +++ b/tests/testthat/_snaps/JAGS-diagnostics/diagnostics-ggplot-trace-2-1.svg @@ -0,0 +1,88 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +-0.6 +-0.4 +-0.2 +0.0 +0.2 +0.4 +0.6 + + + + + + + + + + + + +0 +1000 +2000 +3000 +4000 +Iteration +(mu) x_fac3o [dif: A] +orthonormal contrast: +mNormal +(0, 1) + + diff --git a/tests/testthat/_snaps/JAGS-diagnostics/diagnostics-ggplot-trace-2-2.svg b/tests/testthat/_snaps/JAGS-diagnostics/diagnostics-ggplot-trace-2-2.svg new file mode 100644 index 00000000..e5b9a6d2 --- /dev/null +++ b/tests/testthat/_snaps/JAGS-diagnostics/diagnostics-ggplot-trace-2-2.svg @@ -0,0 +1,80 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +-0.6 +-0.4 +-0.2 +0.0 +0.2 + + + + + + + + + + +0 +1000 +2000 +3000 +4000 +Iteration +(mu) x_fac3o [dif: B] +orthonormal contrast: +mNormal +(0, 1) + + diff --git a/tests/testthat/_snaps/JAGS-diagnostics/diagnostics-ggplot-trace-2-3.svg b/tests/testthat/_snaps/JAGS-diagnostics/diagnostics-ggplot-trace-2-3.svg new file mode 100644 index 00000000..981f66b5 --- /dev/null +++ b/tests/testthat/_snaps/JAGS-diagnostics/diagnostics-ggplot-trace-2-3.svg @@ -0,0 +1,80 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +-0.2 +0.0 +0.2 +0.4 +0.6 + + + + + + + + + + +0 +1000 +2000 +3000 +4000 +Iteration +(mu) x_fac3o [dif: C] +orthonormal contrast: +mNormal +(0, 1) + + diff --git a/tests/testthat/_snaps/JAGS-diagnostics/diagnostics-ggplot-trace-3-1.svg b/tests/testthat/_snaps/JAGS-diagnostics/diagnostics-ggplot-trace-3-1.svg new file mode 100644 index 00000000..a656298a --- /dev/null +++ b/tests/testthat/_snaps/JAGS-diagnostics/diagnostics-ggplot-trace-3-1.svg @@ -0,0 +1,91 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +0.0 +0.2 +0.4 +0.6 +0.8 +1.0 + + + + + + + + + + + +0 +1000 +2000 +3000 +4000 +Iteration +omega[0.05,0.1] +ω +one-sided: +.1, .05 + +~ + +CumDirichlet +( +1, 1, 1 +) + + diff --git a/tests/testthat/_snaps/JAGS-diagnostics/diagnostics-ggplot-trace-3-2.svg b/tests/testthat/_snaps/JAGS-diagnostics/diagnostics-ggplot-trace-3-2.svg new file mode 100644 index 00000000..4a6dc661 --- /dev/null +++ b/tests/testthat/_snaps/JAGS-diagnostics/diagnostics-ggplot-trace-3-2.svg @@ -0,0 +1,91 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +0.0 +0.2 +0.4 +0.6 +0.8 +1.0 + + + + + + + + + + + +0 +1000 +2000 +3000 +4000 +Iteration +omega[0.1,1] +ω +one-sided: +.1, .05 + +~ + +CumDirichlet +( +1, 1, 1 +) + + diff --git a/tests/testthat/_snaps/JAGS-diagnostics/diagnostics-plot-autocorrelation-1.svg b/tests/testthat/_snaps/JAGS-diagnostics/diagnostics-plot-autocorrelation-1.svg new file mode 100644 index 00000000..41ea915b --- /dev/null +++ b/tests/testthat/_snaps/JAGS-diagnostics/diagnostics-plot-autocorrelation-1.svg @@ -0,0 +1,185 @@ + + + + + + + + + + + + + + + + + + + + +0 +5 +10 +15 +20 +25 +30 + + + + + + + +0.0 +0.2 +0.4 +0.6 +0.8 +1.0 +Normal +(0, 1) +Lag +Autocorrelation(x_cont1) + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/tests/testthat/_snaps/JAGS-diagnostics/diagnostics-plot-autocorrelation-2.svg b/tests/testthat/_snaps/JAGS-diagnostics/diagnostics-plot-autocorrelation-2.svg new file mode 100644 index 00000000..9cef07fe --- /dev/null +++ b/tests/testthat/_snaps/JAGS-diagnostics/diagnostics-plot-autocorrelation-2.svg @@ -0,0 +1,185 @@ + + + + + + + + + + + + + + + + + + + + +0 +5 +10 +15 +20 +25 +30 + + + + + + + +0.0 +0.2 +0.4 +0.6 +0.8 +1.0 +Normal +(0, 1) +Lag +Autocorrelation(x_cont1) + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/tests/testthat/_snaps/JAGS-diagnostics/diagnostics-plot-autocorrelation-3.svg b/tests/testthat/_snaps/JAGS-diagnostics/diagnostics-plot-autocorrelation-3.svg new file mode 100644 index 00000000..c3b0c8bb --- /dev/null +++ b/tests/testthat/_snaps/JAGS-diagnostics/diagnostics-plot-autocorrelation-3.svg @@ -0,0 +1,184 @@ + + + + + + + + + + + + + + + + + + + + +0 +5 +10 +15 +20 +25 +30 + + + + + + + +0.0 +0.2 +0.4 +0.6 +0.8 +1.0 +Treatment +Values +Smth + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/tests/testthat/_snaps/JAGS-diagnostics/diagnostics-plot-autocorrelation-4.svg b/tests/testthat/_snaps/JAGS-diagnostics/diagnostics-plot-autocorrelation-4.svg new file mode 100644 index 00000000..b3ff76f8 --- /dev/null +++ b/tests/testthat/_snaps/JAGS-diagnostics/diagnostics-plot-autocorrelation-4.svg @@ -0,0 +1,368 @@ + + + + + + + + + + + + + + + + + + + + +0 +5 +10 +15 +20 +25 +30 + + + + + + + +0.0 +0.2 +0.4 +0.6 +0.8 +1.0 + + + + + + + +orthonormal contrast: +mNormal +(0, 1) +Lag +Autocorrelation((mu) x_fac3o[1]) + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +0 +5 +10 +15 +20 +25 +30 + + + + + + + +0.0 +0.2 +0.4 +0.6 +0.8 +1.0 + + + + + + + +orthonormal contrast: +mNormal +(0, 1) +Lag +Autocorrelation((mu) x_fac3o[2]) + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/tests/testthat/_snaps/JAGS-diagnostics/diagnostics-plot-autocorrelation-5.svg b/tests/testthat/_snaps/JAGS-diagnostics/diagnostics-plot-autocorrelation-5.svg new file mode 100644 index 00000000..cbba3293 --- /dev/null +++ b/tests/testthat/_snaps/JAGS-diagnostics/diagnostics-plot-autocorrelation-5.svg @@ -0,0 +1,543 @@ + + + + + + + + + + + + + + + + + + + + +0 +5 +10 +15 +20 +25 +30 + + + + + + + +0.0 +0.2 +0.4 +0.6 +0.8 +1.0 + + + + + + + +orthonormal contrast: +mNormal +(0, 1) +Lag +Autocorrelation(x_fac3o [dif: A]) + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +0 +5 +10 +15 +20 +25 +30 + + + + + + + +0.0 +0.2 +0.4 +0.6 +0.8 +1.0 + + + + + + + +orthonormal contrast: +mNormal +(0, 1) +Lag +Autocorrelation(x_fac3o [dif: B]) + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +0 +5 +10 +15 +20 +25 +30 + + + + + + + +0.0 +0.2 +0.4 +0.6 +0.8 +1.0 + + + + + + + +orthonormal contrast: +mNormal +(0, 1) +Lag +Autocorrelation(x_fac3o [dif: C]) + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/tests/testthat/_snaps/JAGS-diagnostics/diagnostics-plot-autocorrelation-6.svg b/tests/testthat/_snaps/JAGS-diagnostics/diagnostics-plot-autocorrelation-6.svg new file mode 100644 index 00000000..900d9bdc --- /dev/null +++ b/tests/testthat/_snaps/JAGS-diagnostics/diagnostics-plot-autocorrelation-6.svg @@ -0,0 +1,186 @@ + + + + + + + + + + + + + + + + + + + + +0 +5 +10 +15 +20 +25 +30 + + + + + + + +0.0 +0.2 +0.4 +0.6 +0.8 +1.0 +PET ~ +Gamma +(2, 2) +Lag +Autocorrelation(PET) + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/tests/testthat/_snaps/JAGS-diagnostics/diagnostics-plot-autocorrelation-7.svg b/tests/testthat/_snaps/JAGS-diagnostics/diagnostics-plot-autocorrelation-7.svg new file mode 100644 index 00000000..207e2103 --- /dev/null +++ b/tests/testthat/_snaps/JAGS-diagnostics/diagnostics-plot-autocorrelation-7.svg @@ -0,0 +1,382 @@ + + + + + + + + + + + + + + + + + + + + +0 +5 +10 +15 +20 +25 +30 + + + + + + + +0.0 +0.2 +0.4 +0.6 +0.8 +1.0 + + + + + + + +ω +one-sided: +.1, .05 + +~ + +CumDirichlet +( +1, 1, 1 +) +Lag +Autocorrelation(omega[0.05,0.1]) + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +0 +5 +10 +15 +20 +25 +30 + + + + + + + +0.0 +0.2 +0.4 +0.6 +0.8 +1.0 + + + + + + + +ω +one-sided: +.1, .05 + +~ + +CumDirichlet +( +1, 1, 1 +) +Lag +Autocorrelation(omega[0.1,1]) + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/tests/testthat/_snaps/JAGS-diagnostics/diagnostics-plot-density-1.svg b/tests/testthat/_snaps/JAGS-diagnostics/diagnostics-plot-density-1.svg new file mode 100644 index 00000000..19c9f9f3 --- /dev/null +++ b/tests/testthat/_snaps/JAGS-diagnostics/diagnostics-plot-density-1.svg @@ -0,0 +1,63 @@ + + + + + + + + + + + + + + + + + + + +0.1 +0.2 +0.3 +0.4 +0.5 +0.6 + + + + + + + +0 +1 +2 +3 +4 +5 +Normal +(0, 1) +x_cont1 +Density + + + + + + + + + + + + + diff --git a/tests/testthat/_snaps/JAGS-diagnostics/diagnostics-plot-density-2.svg b/tests/testthat/_snaps/JAGS-diagnostics/diagnostics-plot-density-2.svg new file mode 100644 index 00000000..b85346ad --- /dev/null +++ b/tests/testthat/_snaps/JAGS-diagnostics/diagnostics-plot-density-2.svg @@ -0,0 +1,55 @@ + + + + + + + + + + + + + + + + + +1.2 +1.4 +1.6 +1.8 + + + + + +0 +1 +2 +3 +Normal +(0, 1) +x_cont1 +Density + + + + + + + + + + + + + diff --git a/tests/testthat/_snaps/JAGS-diagnostics/diagnostics-plot-density-3.svg b/tests/testthat/_snaps/JAGS-diagnostics/diagnostics-plot-density-3.svg new file mode 100644 index 00000000..2db87e33 --- /dev/null +++ b/tests/testthat/_snaps/JAGS-diagnostics/diagnostics-plot-density-3.svg @@ -0,0 +1,64 @@ + + + + + + + + + + + + + + + + + + + +-0.4 +-0.2 +0.0 +0.2 +0.4 +0.6 + + + + + + + + +0.0 +0.5 +1.0 +1.5 +2.0 +2.5 +3.0 +Treatment +Values +Smth + + + + + + + + + + + + + diff --git a/tests/testthat/_snaps/JAGS-diagnostics/diagnostics-plot-density-4.svg b/tests/testthat/_snaps/JAGS-diagnostics/diagnostics-plot-density-4.svg new file mode 100644 index 00000000..17655c79 --- /dev/null +++ b/tests/testthat/_snaps/JAGS-diagnostics/diagnostics-plot-density-4.svg @@ -0,0 +1,130 @@ + + + + + + + + + + + + + + + + + + + +-0.2 +0.0 +0.2 +0.4 +0.6 +0.8 + + + + + + + + +0.0 +0.5 +1.0 +1.5 +2.0 +2.5 +3.0 + + + + + + + +orthonormal contrast: +mNormal +(0, 1) +(mu) x_fac3o[1] +Density + + + + + + + + + + + + + + + + + + + + + + + + + + + +-0.4 +-0.2 +0.0 +0.2 +0.4 +0.6 + + + + + + + + + +0.0 +0.5 +1.0 +1.5 +2.0 +2.5 +3.0 +3.5 + + + + + + + +orthonormal contrast: +mNormal +(0, 1) +(mu) x_fac3o[2] +Density + + + + + + + + diff --git a/tests/testthat/_snaps/JAGS-diagnostics/diagnostics-plot-density-5.svg b/tests/testthat/_snaps/JAGS-diagnostics/diagnostics-plot-density-5.svg new file mode 100644 index 00000000..574f6ec3 --- /dev/null +++ b/tests/testthat/_snaps/JAGS-diagnostics/diagnostics-plot-density-5.svg @@ -0,0 +1,165 @@ + + + + + + + + + + + + + + + + + + +-0.4 +-0.2 +0.0 +0.2 +0.4 + + + + + + +0 +1 +2 +3 +4 + + + + + + + +orthonormal contrast: +mNormal +(0, 1) +x_fac3o [dif: A] +Density + + + + + + + + + + + + + + + + + + + + + + + + + + +-0.6 +-0.4 +-0.2 +0.0 +0.2 + + + + + + +0 +1 +2 +3 +4 + + + + + + + +orthonormal contrast: +mNormal +(0, 1) +x_fac3o [dif: B] +Density + + + + + + + + + + + + + + + + + + + + + +-0.2 +0.0 +0.2 +0.4 +0.6 + + + + + + +0 +1 +2 +3 +4 + + + + + + + +orthonormal contrast: +mNormal +(0, 1) +x_fac3o [dif: C] +Density + + + + + + + + diff --git a/tests/testthat/_snaps/JAGS-diagnostics/diagnostics-plot-density-6.svg b/tests/testthat/_snaps/JAGS-diagnostics/diagnostics-plot-density-6.svg new file mode 100644 index 00000000..4170af73 --- /dev/null +++ b/tests/testthat/_snaps/JAGS-diagnostics/diagnostics-plot-density-6.svg @@ -0,0 +1,64 @@ + + + + + + + + + + + + + + + + + + + + + +0 +1 +2 +3 +4 +5 +6 +7 + + + + + +0.0 +0.2 +0.4 +0.6 +PET ~ +Gamma +(2, 2) +PET +Density + + + + + + + + + + + + + diff --git a/tests/testthat/_snaps/JAGS-diagnostics/diagnostics-plot-density-7.svg b/tests/testthat/_snaps/JAGS-diagnostics/diagnostics-plot-density-7.svg new file mode 100644 index 00000000..0e008643 --- /dev/null +++ b/tests/testthat/_snaps/JAGS-diagnostics/diagnostics-plot-density-7.svg @@ -0,0 +1,130 @@ + + + + + + + + + + + + + + + + + + + +0.0 +0.2 +0.4 +0.6 +0.8 +1.0 + + + + + +0.0 +0.5 +1.0 +1.5 + + + + + + + +ω +one-sided: +.1, .05 + +~ + +CumDirichlet +( +1, 1, 1 +) +omega[0.05,0.1] +Density + + + + + + + + + + + + + + + + + + + + + + + + + + + +0.0 +0.2 +0.4 +0.6 +0.8 +1.0 + + + + + +0.0 +0.5 +1.0 +1.5 + + + + + + + +ω +one-sided: +.1, .05 + +~ + +CumDirichlet +( +1, 1, 1 +) +omega[0.1,1] +Density + + + + + + + + diff --git a/tests/testthat/_snaps/JAGS-diagnostics/diagnostics-plot-trace-1.svg b/tests/testthat/_snaps/JAGS-diagnostics/diagnostics-plot-trace-1.svg new file mode 100644 index 00000000..d40226a7 --- /dev/null +++ b/tests/testthat/_snaps/JAGS-diagnostics/diagnostics-plot-trace-1.svg @@ -0,0 +1,61 @@ + + + + + + + + + + + + + + + + + + +0 +1000 +2000 +3000 +4000 + + + + + + + +0.1 +0.2 +0.3 +0.4 +0.5 +0.6 +Normal +(0, 1) +Iteration +x_cont1 + + + + + + + + + + + + + diff --git a/tests/testthat/_snaps/JAGS-diagnostics/diagnostics-plot-trace-2.svg b/tests/testthat/_snaps/JAGS-diagnostics/diagnostics-plot-trace-2.svg new file mode 100644 index 00000000..a4844bfb --- /dev/null +++ b/tests/testthat/_snaps/JAGS-diagnostics/diagnostics-plot-trace-2.svg @@ -0,0 +1,57 @@ + + + + + + + + + + + + + + + + + + +0 +1000 +2000 +3000 +4000 + + + + + +1.2 +1.4 +1.6 +1.8 +Normal +(0, 1) +Iteration +x_cont1 + + + + + + + + + + + + + diff --git a/tests/testthat/_snaps/JAGS-diagnostics/diagnostics-plot-trace-3.svg b/tests/testthat/_snaps/JAGS-diagnostics/diagnostics-plot-trace-3.svg new file mode 100644 index 00000000..a80065d4 --- /dev/null +++ b/tests/testthat/_snaps/JAGS-diagnostics/diagnostics-plot-trace-3.svg @@ -0,0 +1,60 @@ + + + + + + + + + + + + + + + + + + +0 +1000 +2000 +3000 +4000 + + + + + + + +-0.4 +-0.2 +0.0 +0.2 +0.4 +0.6 +Treatment +Values +Smth + + + + + + + + + + + + + diff --git a/tests/testthat/_snaps/JAGS-diagnostics/diagnostics-plot-trace-4.svg b/tests/testthat/_snaps/JAGS-diagnostics/diagnostics-plot-trace-4.svg new file mode 100644 index 00000000..957c2ab8 --- /dev/null +++ b/tests/testthat/_snaps/JAGS-diagnostics/diagnostics-plot-trace-4.svg @@ -0,0 +1,120 @@ + + + + + + + + + + + + + + + + + + +0 +1000 +2000 +3000 +4000 + + + + + + + +-0.2 +0.0 +0.2 +0.4 +0.6 +0.8 + + + + + + + +orthonormal contrast: +mNormal +(0, 1) +Iteration +x_fac3o[1] + + + + + + + + + + + + + + + + + + + + + + + + + + +0 +1000 +2000 +3000 +4000 + + + + + + + +-0.4 +-0.2 +0.0 +0.2 +0.4 +0.6 + + + + + + + +orthonormal contrast: +mNormal +(0, 1) +Iteration +x_fac3o[2] + + + + + + + + diff --git a/tests/testthat/_snaps/JAGS-diagnostics/diagnostics-plot-trace-5.svg b/tests/testthat/_snaps/JAGS-diagnostics/diagnostics-plot-trace-5.svg new file mode 100644 index 00000000..e9754ca6 --- /dev/null +++ b/tests/testthat/_snaps/JAGS-diagnostics/diagnostics-plot-trace-5.svg @@ -0,0 +1,165 @@ + + + + + + + + + + + + + + + + + + +0 +1000 +2000 +3000 +4000 + + + + + + +-0.4 +-0.2 +0.0 +0.2 +0.4 + + + + + + + +orthonormal contrast: +mNormal +(0, 1) +Iteration +(mu) x_fac3o [dif: A] + + + + + + + + + + + + + + + + + + + + + + + + + + +0 +1000 +2000 +3000 +4000 + + + + + + +-0.6 +-0.4 +-0.2 +0.0 +0.2 + + + + + + + +orthonormal contrast: +mNormal +(0, 1) +Iteration +(mu) x_fac3o [dif: B] + + + + + + + + + + + + + + + + + + + + + +0 +1000 +2000 +3000 +4000 + + + + + + +-0.2 +0.0 +0.2 +0.4 +0.6 + + + + + + + +orthonormal contrast: +mNormal +(0, 1) +Iteration +(mu) x_fac3o [dif: C] + + + + + + + + diff --git a/tests/testthat/_snaps/JAGS-diagnostics/diagnostics-plot-trace-6.svg b/tests/testthat/_snaps/JAGS-diagnostics/diagnostics-plot-trace-6.svg new file mode 100644 index 00000000..291c3f00 --- /dev/null +++ b/tests/testthat/_snaps/JAGS-diagnostics/diagnostics-plot-trace-6.svg @@ -0,0 +1,66 @@ + + + + + + + + + + + + + + + + + + +0 +1000 +2000 +3000 +4000 + + + + + + + + + +0 +1 +2 +3 +4 +5 +6 +7 +PET ~ +Gamma +(2, 2) +Iteration +PET + + + + + + + + + + + + + diff --git a/tests/testthat/_snaps/JAGS-diagnostics/diagnostics-plot-trace-7.svg b/tests/testthat/_snaps/JAGS-diagnostics/diagnostics-plot-trace-7.svg new file mode 100644 index 00000000..91ef7b64 --- /dev/null +++ b/tests/testthat/_snaps/JAGS-diagnostics/diagnostics-plot-trace-7.svg @@ -0,0 +1,134 @@ + + + + + + + + + + + + + + + + + + +0 +1000 +2000 +3000 +4000 + + + + + + + +0.0 +0.2 +0.4 +0.6 +0.8 +1.0 + + + + + + + +ω +one-sided: +.1, .05 + +~ + +CumDirichlet +( +1, 1, 1 +) +Iteration +omega[0.05,0.1] + + + + + + + + + + + + + + + + + + + + + + + + + + +0 +1000 +2000 +3000 +4000 + + + + + + + +0.0 +0.2 +0.4 +0.6 +0.8 +1.0 + + + + + + + +ω +one-sided: +.1, .05 + +~ + +CumDirichlet +( +1, 1, 1 +) +Iteration +omega[0.1,1] + + + + + + + + diff --git a/tests/testthat/test-JAGS-diagnostics.R b/tests/testthat/test-JAGS-diagnostics.R new file mode 100644 index 00000000..2ecc4efa --- /dev/null +++ b/tests/testthat/test-JAGS-diagnostics.R @@ -0,0 +1,152 @@ +test_that("JAGS diagnostics work", { + + skip_on_os(c("mac", "linux", "solaris")) # multivariate sampling does not exactly match across OSes + skip_on_cran() + + set.seed(1) + + data_formula <- data.frame( + x_cont1 = rnorm(150), + x_fac2t = factor(rep(c("A", "B"), 75), levels = c("A", "B")), + x_fac3o = factor(rep(c("A", "B", "C"), 50), levels = c("A", "B", "C")) + ) + data <- list( + y = rnorm(150, .4 * data_formula$x_cont1 + ifelse(data_formula$x_fac3o == "A", 0.0, ifelse(data_formula$x_fac3o == "B", -0.2, 0.4)), ifelse(data_formula$x_fac2t == "A", 0.5, 1)), + N = 150 + ) + + + # create model with mix of a formula and free parameters --- + formula_list <- list( + mu = ~ x_cont1 + x_fac2t + x_fac3o + ) + formula_data_list <- list( + mu = data_formula + ) + formula_prior_list <- list( + mu = list( + "intercept" = prior("normal", list(0, 5)), + "x_cont1" = prior("normal", list(0, 1)), + "x_fac2t" = prior_factor("normal", contrast = "treatment", list(0, 1)), + "x_fac3o" = prior_factor("mnormal", contrast = "orthonormal", list(0, 1)) + ) + ) + prior_list <- list( + sigma = prior("lognormal", list(0, 1)), + omega = prior_weightfunction("onesided", list(c(0.05, 0.10), c(1,1,1))), + PET = prior_PET("gamma", list(2, 2)) + ) + model_syntax <- paste0( + "model{\n", + "for(i in 1:N){\n", + " y[i] ~ dnorm(mu[i], 1/pow(sigma, 2))\n", + "}\n", + "}" + ) + + fit <- JAGS_fit( + model_syntax = model_syntax, data = data, prior_list = prior_list, + formula_list = formula_list, formula_data_list = formula_data_list, formula_prior_list = formula_prior_list) + + + ### density plots + expect_doppelganger("diagnostics-plot-density-1", function() JAGS_diagnostics_density(fit, parameter = "mu_x_cont1", formula_prefix = FALSE)) + expect_doppelganger("diagnostics-plot-density-2", function() JAGS_diagnostics_density(fit, parameter = "mu_x_cont1", col = c("red", "green", "blue", "yellow"), formula_prefix = FALSE, transformations = list(mu_x_cont1 = list(fun = function(x) exp(x))))) + expect_doppelganger("diagnostics-plot-density-3", function() JAGS_diagnostics_density(fit, parameter = "mu_x_fac2t", main = "Treatment", xlab = "Values", formula_prefix = FALSE, ylab = "Smth")) + expect_doppelganger("diagnostics-plot-density-4", function(){ + oldpar <- graphics::par(no.readonly = TRUE) + on.exit(graphics::par(mfrow = oldpar[["mfrow"]])) + par(mfrow = c(1, 2)) + JAGS_diagnostics_density(fit, parameter = "mu_x_fac3o") + }) + expect_doppelganger("diagnostics-plot-density-5", function(){ + oldpar <- graphics::par(no.readonly = TRUE) + on.exit(graphics::par(mfrow = oldpar[["mfrow"]])) + par(mfrow = c(1, 3)) + JAGS_diagnostics_density(fit, parameter = "mu_x_fac3o", formula_prefix = FALSE, transform_orthonormal = TRUE) + }) + expect_doppelganger("diagnostics-plot-density-6", function()JAGS_diagnostics_density(fit, parameter = "PET")) + expect_doppelganger("diagnostics-plot-density-7", function(){ + oldpar <- graphics::par(no.readonly = TRUE) + on.exit(graphics::par(mfrow = oldpar[["mfrow"]])) + par(mfrow = c(1, 2)) + JAGS_diagnostics_density(fit, parameter = "omega") + }) + + expect_doppelganger("diagnostics-ggplot-density-1", JAGS_diagnostics_density(fit, plot_type = "ggplot", parameter = "mu_x_cont1", col = c("red", "green", "blue", "yellow"), formula_prefix = FALSE, transformations = list(mu_x_cont1 = list(fun = function(x) exp(x))))) + temp_plot <- JAGS_diagnostics_density(fit, plot_type = "ggplot", parameter = "mu_x_fac3o", transform_orthonormal = TRUE) + expect_doppelganger("diagnostics-ggplot-density-2.1",temp_plot[[1]]) + expect_doppelganger("diagnostics-ggplot-density-2.2",temp_plot[[2]]) + expect_doppelganger("diagnostics-ggplot-density-2.3",temp_plot[[3]]) + temp_plot <- JAGS_diagnostics_density(fit, plot_type = "ggplot", parameter = "omega") + expect_doppelganger("diagnostics-ggplot-density-3.1",temp_plot[[1]]) + expect_doppelganger("diagnostics-ggplot-density-3.2",temp_plot[[2]]) + + + ### trace plots + expect_doppelganger("diagnostics-plot-trace-1", function() JAGS_diagnostics_trace(fit, parameter = "mu_x_cont1", formula_prefix = FALSE)) + expect_doppelganger("diagnostics-plot-trace-2", function() JAGS_diagnostics_trace(fit, parameter = "mu_x_cont1", col = c("red", "green", "blue", "yellow"), formula_prefix = FALSE, transformations = list(mu_x_cont1 = list(fun = function(x) exp(x))))) + expect_doppelganger("diagnostics-plot-trace-3", function() JAGS_diagnostics_trace(fit, parameter = "mu_x_fac2t", main = "Treatment", xlab = "Values", formula_prefix = FALSE, ylab = "Smth")) + expect_doppelganger("diagnostics-plot-trace-4", function(){ + oldpar <- graphics::par(no.readonly = TRUE) + on.exit(graphics::par(mfrow = oldpar[["mfrow"]])) + par(mfrow = c(1, 2)) + JAGS_diagnostics_trace(fit, parameter = "mu_x_fac3o", formula_prefix = FALSE) + }) + expect_doppelganger("diagnostics-plot-trace-5", function(){ + oldpar <- graphics::par(no.readonly = TRUE) + on.exit(graphics::par(mfrow = oldpar[["mfrow"]])) + par(mfrow = c(1, 3)) + JAGS_diagnostics_trace(fit, parameter = "mu_x_fac3o", transform_orthonormal = TRUE) + }) + expect_doppelganger("diagnostics-plot-trace-6", function() JAGS_diagnostics_trace(fit, parameter = "PET")) + expect_doppelganger("diagnostics-plot-trace-7", function(){ + oldpar <- graphics::par(no.readonly = TRUE) + on.exit(graphics::par(mfrow = oldpar[["mfrow"]])) + par(mfrow = c(1, 2)) + JAGS_diagnostics_trace(fit, parameter = "omega") + }) + + expect_doppelganger("diagnostics-ggplot-trace-1", JAGS_diagnostics_trace(fit, plot_type = "ggplot", parameter = "mu_x_cont1", col = c("red", "green", "blue", "yellow"), formula_prefix = FALSE, transformations = list(mu_x_cont1 = list(fun = function(x) exp(x))))) + temp_plot <- JAGS_diagnostics_trace(fit, plot_type = "ggplot", parameter = "mu_x_fac3o", transform_orthonormal = TRUE) + expect_doppelganger("diagnostics-ggplot-trace-2.1",temp_plot[[1]]) + expect_doppelganger("diagnostics-ggplot-trace-2.2",temp_plot[[2]]) + expect_doppelganger("diagnostics-ggplot-trace-2.3",temp_plot[[3]]) + temp_plot <- JAGS_diagnostics_trace(fit, plot_type = "ggplot", parameter = "omega") + expect_doppelganger("diagnostics-ggplot-trace-3.1",temp_plot[[1]]) + expect_doppelganger("diagnostics-ggplot-trace-3.2",temp_plot[[2]]) + + + ### autocorrelation plots + expect_doppelganger("diagnostics-plot-autocorrelation-1", function() JAGS_diagnostics_autocorrelation(fit, parameter = "mu_x_cont1", formula_prefix = FALSE)) + expect_doppelganger("diagnostics-plot-autocorrelation-2", function() JAGS_diagnostics_autocorrelation(fit, parameter = "mu_x_cont1", col = c("red", "green", "blue", "yellow"), formula_prefix = FALSE, transformations = list(mu_x_cont1 = list(fun = function(x) exp(x))))) + expect_doppelganger("diagnostics-plot-autocorrelation-3", function() JAGS_diagnostics_autocorrelation(fit, parameter = "mu_x_fac2t", main = "Treatment", xlab = "Values", ylab = "Smth")) + expect_doppelganger("diagnostics-plot-autocorrelation-4", function(){ + oldpar <- graphics::par(no.readonly = TRUE) + on.exit(graphics::par(mfrow = oldpar[["mfrow"]])) + par(mfrow = c(1, 2)) + JAGS_diagnostics_autocorrelation(fit, parameter = "mu_x_fac3o") + }) + expect_doppelganger("diagnostics-plot-autocorrelation-5", function(){ + oldpar <- graphics::par(no.readonly = TRUE) + on.exit(graphics::par(mfrow = oldpar[["mfrow"]])) + par(mfrow = c(1, 3)) + JAGS_diagnostics_autocorrelation(fit, parameter = "mu_x_fac3o", formula_prefix = FALSE, transform_orthonormal = TRUE) + }) + expect_doppelganger("diagnostics-plot-autocorrelation-6", function() JAGS_diagnostics_autocorrelation(fit, parameter = "PET")) + expect_doppelganger("diagnostics-plot-autocorrelation-7", function(){ + oldpar <- graphics::par(no.readonly = TRUE) + on.exit(graphics::par(mfrow = oldpar[["mfrow"]])) + par(mfrow = c(1, 2)) + JAGS_diagnostics_autocorrelation(fit, parameter = "omega") + }) + + expect_doppelganger("diagnostics-ggplot-autocorrelation-1", JAGS_diagnostics_autocorrelation(fit, plot_type = "ggplot", parameter = "mu_x_cont1", col = c("red", "green", "blue", "yellow"), formula_prefix = FALSE, transformations = list(mu_x_cont1 = list(fun = function(x) exp(x))))) + temp_plot <- JAGS_diagnostics_autocorrelation(fit, plot_type = "ggplot", parameter = "mu_x_fac3o", transform_orthonormal = TRUE) + expect_doppelganger("diagnostics-ggplot-autocorrelation-2.1",temp_plot[[1]]) + expect_doppelganger("diagnostics-ggplot-autocorrelation-2.2",temp_plot[[2]]) + expect_doppelganger("diagnostics-ggplot-autocorrelation-2.3",temp_plot[[3]]) + temp_plot <- JAGS_diagnostics_autocorrelation(fit, plot_type = "ggplot", parameter = "omega") + expect_doppelganger("diagnostics-ggplot-autocorrelation-3.1",temp_plot[[1]]) + expect_doppelganger("diagnostics-ggplot-autocorrelation-3.2",temp_plot[[2]]) +})