R/plot_i_squared.R

Defines functions combine_plots bloxplot_unbiased boxplot_biased compile_plot_data

Documented in bloxplot_unbiased boxplot_biased combine_plots compile_plot_data

# I^2 Boxplots ------------------------------------------------------------

# Compile plotting data ---------------------------------------------------
# Compute summary statistics of I^2 value for plotting observed I^2 against
# intended

#' Compile plotting data for I^2 plot
#'
#' @param sim_data simulated data
#' @param scenarios df with scenarios
#'
#' @return a data frame suitable for the I^2 plotting
#' @export
#' @importFrom magrittr "%>%"

compile_plot_data <- function(sim_data, scenarios) {

  ma_level_df <- sim_data %>%
    group_by(job_id, scenario_id) %>%
      summarize(i_squared_unbiased = mean(i_squared_unbiased),
             i_squared_biased = mean(i_squared_biased))

  plot_data <-
    left_join(ma_level_df, scenarios, by = "scenario_id") %>%
    mutate(bias_type_fct = factor(bias_type,
                                  levels = c("p","es"),
                                  labels = c("p-value", "effect size")))
}


# Plotting I^2 after publication bias ------------------------------------

#' Plot a boxplot of the biased I^2 values
#'
#' @param plot_data plotting data as generated by \code{compile_plot_data()}
#'
#' @return a ggplot2 object
#' @export
#'

boxplot_biased <- function(plot_data) {

  ggplot(plot_data, aes(x = factor(heterogeneity),
                                             y = i_squared_biased)) +
    geom_violin(aes(color = bias_type_fct, fill = bias_type_fct),
                alpha = 0.2,
                draw_quantiles = c(0.5)) +
    geom_violin(fill = NA,
                size = 0.7,
                draw_quantiles = c(0.5)) +
    geom_point(data = data.frame(x = factor(c(0, 0.2, 1.5, 5)),
                                 y = c(0, 0.16666, 0.6, 0.83)),
               aes(x = x, y = y),
               size = 3,
               color ="red") +
    theme_classic() +
    labs(
      title = "Intended vs observed heterogeneity after publication bias",
      x = "Intended heterogeneity",
      y = "Observed heterogeneity",
      fill = "Bias Type",
      color = "Bias Type"
    )
}


# Plotting I^2 before publication bias -------------------------------------

#' Plot a boxplot of the unbiased I^2 values
#'
#' @param plot_data plotting data as generated by \code{compile_plot_data()}
#'
#' @return a ggplot2 object
#' @export
#'

bloxplot_unbiased <- function(plot_data) {

  ggplot(plot_data, aes(x = factor(heterogeneity),
                                           y = i_squared_unbiased)) +
    geom_violin(aes(color = bias_type_fct, fill = bias_type_fct),
                alpha = 0.2,
                draw_quantiles = c(0.5)) +
    geom_violin(fill = NA,
                size = 0.7,
                draw_quantiles = c(0.5)) +
    geom_point(data = data.frame(x = factor(c(0, 0.2, 1.5, 5)),
                                 y = c(0, 0.16666, 0.6, 0.83)),
               aes(x = x, y = y),
               size = 3,
               color ="red") +
    theme_classic() +
    labs(
      title = "Intended vs observed heterogeneity before publication bias",
      x = "Intended heterogeneity",
      y = "Observed heterogeneity",
      fill = "Bias Type",
      color = "Bias Type"
    )
}

# Combining both plots ----------------------------------------------------

#' Combine biased and unbiased I^2 boxplots
#'
#' @param bloxplot_biased
#' @param bloxplot_unbiased
#'
#' @return
#' @export
#'
#' @import patchwork

combine_plots <- function(boxplot_biased, boxplot_unbiased) {
  pdf("i_squared_plot.pdf", width = 16, height = 8)

  boxplot_biased + boxplot_unbiased +
    plot_layout(guides = 'collect')

  dev.off()
}
replisims/peters-2016 documentation built on Aug. 6, 2020, 12:15 a.m.