R/plot_cov_chi.R

Defines functions plot_4 dname_conv plot_res

Documented in dname_conv plot_4 plot_res

#' Plot Creator for Coverage and Half-length Results
#'
#' @param res_list a list object produced by Monte Carlo simulations
#' @param ebci equal to 1 if EBCI result is added; 0 otherwise
#' @param xTitle .
#' @param title_cov .
#' @param title_chi .
#' @param ylab_cov .
#' @param ylab_chi .
#' @param cov_theme .
#' @param chi_theme .
#'
#' @return a list of ggplot objects
#' @export
#'
plot_res <- function(res_list, ebci = 0, xTitle = "Measure of parameter dispersion",
                     title_cov = paste("Sample size: n = ", res_list$n),
                     title_chi = paste("Sample size: n = ", res_list$n),
                     ylab_cov = "Average coverage",
                     ylab_chi = "Length ratio",
                     cov_theme = ggplot2::theme(plot.title = ggplot2::element_text(hjust = 0.5)),
                     chi_theme = ggplot2::theme(plot.title = ggplot2::element_text(hjust = 0.5))){

  n <- res_list$n
  cov_chi_data <- res_list$res_data
  cov_data <- cov_chi_data$cov_data
  chi_data <- cov_chi_data$chi_data
  spec_list <- res_list$spec_list
  alpha <- spec_list$alpha

  covplot <- ggplot2::ggplot() +
    ggplot2::geom_line(data = cov_data, mapping = ggplot2::aes(x = M, y = covval, color = ind)) +
    ggplot2::ylim(0.9, 1) +
    ggplot2::labs(title = title_cov, x = xTitle, y = ylab_cov, color = "Method") +
    ggplot2::geom_hline(yintercept = 1 - alpha, linetype = "dashed", size = 0.5, color = "gray") +
    cov_theme +
    theme(panel.background = ggplot2::element_blank(),
          strip.background = ggplot2::element_rect(colour=NA, fill=NA),
          panel.border = ggplot2::element_rect(fill = NA, color = "black"))


  chiplot <- ggplot2::ggplot() +
    ggplot2::geom_line(data = chi_data, mapping = ggplot2::aes(x = M, y = chival / stats::qnorm(1 - alpha/2),
                                                               color = ind)) +
    ggplot2::ylim(0, 1) +
    ggplot2::labs(title = title_chi, x = xTitle, y = ylab_chi, color = "Method") +
    chi_theme +
    theme(panel.background = ggplot2::element_blank(),
          strip.background = ggplot2::element_rect(colour=NA, fill=NA),
          panel.border = ggplot2::element_rect(fill = NA, color = "black"))

  return(list(cov_plot = covplot, chi_plot = chiplot))

}

#' Design Name Converter
#'
#' @param dtitle abbreviated design name.
#'
#' @return full design name.
#'
dname_conv <- function(dtitle){

  if(dtitle == "norm"){

    res <- "Normal"

  }else if(dtitle == "t"){

    res <- "Generalized t"

  }else if(dtitle == "sk-t"){

    res <- "Skewed normal"

  }else if(dtitle == "unif"){

    res <- "Uniform"
  }

  return(res)
}


#' Plot 4 Figures
#'
#' @param res_list_4 .
#' @param title_opt .
#' @param cov_theme .
#' @param chi_theme .
#' @param xTitle .
#' @param ylab_cov .
#' @param ylab_chi .
#'
#' @return a list of ggplot objects
#' @export
#'
plot_4 <- function(res_list_4, title_opt = c("n", "Design"), cov_theme, chi_theme,
                   xTitle = "Measure of parameter dispersion",
                   ylab_cov = "Average coverage",
                   ylab_chi = "Length ratio"){

  cov_list <- list()
  chi_list <- list()

  title_opt <- match.arg(title_opt)

  for(j in 1:4){

    res_list <- res_list_4[[j]]
    cov_chi_data <- res_list$res_data
    cov_data <- cov_chi_data$cov_data
    chi_data <- cov_chi_data$chi_data
    n <- res_list$n
    design_title <- dname_conv(res_list$design_name)

    title_list <- list(paste("n = ", n), design_title)

    if(title_opt == "n"){
      title_main = title_list[[1]]
    }else if(title_opt == "Design"){
      title_main = title_list[[2]]
    }

    title_cov <- title_main
    title_chi <- title_main

    plot_res_j <- plot_res(res_list, ebci = 0, xTitle, title_cov, title_chi, ylab_cov, ylab_chi,
                           cov_theme, chi_theme)

    p_cov = plot_res_j$cov_plot
    p_chi = plot_res_j$chi_plot

    cov_list[[j]] <- p_cov
    chi_list[[j]] <- p_chi
  }
  gridExtra::grid.arrange(cov_list[[1]],cov_list[[2]],cov_list[[3]],cov_list[[4]], nrow = 2)
  p1 <- gridExtra::arrangeGrob(cov_list[[1]],cov_list[[2]],cov_list[[3]],cov_list[[4]], nrow = 2)
  p2 <- gridExtra::grid.arrange(chi_list[[1]],chi_list[[2]],chi_list[[3]],chi_list[[4]], nrow = 2)

  return(list(cov4 = p1, chi4 = p2))

}
koohyun-kwon/OptACI documentation built on Oct. 6, 2020, 8:09 a.m.