R/qqnormsim.R

Defines functions qqnormsim

Documented in qqnormsim

#' Generate simulated QQ plots
#'
#' Create a 3 x 3 grid of quantile-quantile plots, the first of which corresponds
#' to the input data. The other eight plots arise from simulating random normal
#' data with the same mean, standard deviation, and length as the data. For use
#' in comparing known-normal qqplots to an observed qqplot to assess normality.
#'
#' @param sample the variable to be plotted.
#' @param data data frame to use.
#'
#' @importFrom ggplot2 qplot
#' @return A 3 x 3 grid of qqplots.
#' @export
qqnormsim <- function(sample, data) {
  y <- eval(substitute(sample), data)
  simnorm <- rnorm(n = length(y) * 8, mean = mean(y),
                   sd = sd(y))
  df <- data.frame(x       = c(y, simnorm),
                   plotnum = rep(c("data", "sim 1", "sim 2",
                                   "sim 3", "sim 4", "sim 5",
                                   "sim 6", "sim 7", "sim 8"),
                                 each = length(y)))
  qplot(sample = x, data = df, stat = "qq", facets =  ~ plotnum)
}
aaronbaggett/labs4316 documentation built on Sept. 20, 2019, 5:21 a.m.