R/plot_rdf_estimate_density.R

Defines functions plot_rdf_estimate_density

Documented in plot_rdf_estimate_density

#' Plot Researcher Degrees of Freedom Estimates' Density
#'
#' Visualizes the estimates generated by exhausting all documented researchers degrees of freedom.
#'
#' @param df The data frame as generated by \code{exhaust_design()}.
#' @param est A character value to indicate the variable in \code{df} that contains the pont estimate
#'   that you want to plot.
#' @param lb A character value to indicate the variable in \code{df} that contains
#'   the lower bound of your estimate.
#' @param ub A character value to indicate the variable in \code{df} that contains
#'   the upper bound of your estimate.
#' @param color Which color do you want your estimate density to have?
#' @param n The condidence interval is currently naively estimate by uniformly distributing \code{n}
#'   observations between \code{lb} and \code{ub}
#' @return A \code{ggplot} object containing the plot.
#' @details Currently naively assumes a uniform distribution between lower and upper bound
#'   of the estimate to display the confidence interval.
#'   See the vignette of the package for further details on how to implement the RDF workflow.
#' @examples
#' \dontrun{
#'   print("Sorry. No examples yet.")
#' }
#' @export

plot_rdf_estimate_density <- function(df, est, lb, ub, color = "blue", n = 30) {
  ci <- NULL # for devtools::check()

  extend <- function(x) data.frame(est = rep(x[1],n),
                                   ci = seq(x[2], x[3], length.out = n))
  extdf <- do.call("rbind", apply(df[, c(est, lb, ub)], 1, extend))
  ggplot2::ggplot(extdf) +
    ggplot2::geom_density(ggplot2::aes(x = ci), fill = "gray", color = NA) +
    ggplot2::geom_density(ggplot2::aes(x = est), fill = color, color = NA) +
    ggplot2::ylab("Density") +
    ggplot2::xlab(sprintf("Density of estimates and resp. CIs (gray), %s model variants",
                 formatC(nrow(df), big.mark = ","))) +
    ggplot2::expand_limits(x = range(stats::density(extdf$ci)$x))
}
joachim-gassen/rdfanalysis documentation built on Aug. 22, 2023, 5:29 p.m.