R/diagnplot.R

Defines functions diagnplot

Documented in diagnplot

#' 4-Plot diagnostic plots
#' 
#' Prints 4-plot that test the typical measurement processassumptions that
#' the data from the process at hand "behave like": (1) random drawings;
#' (2) from a fixed distribution; (3) with the distribution having fixed
#' location; (4) and with the distribution having fixed variation.
#' 
#' @param x     a vector.
#' @param plots a vector of names of plots: "runseq", "lag", "acf",
#'              "hist", "density", "qqnorm" or simply "all".
#' @param ask   logical; if \code{TRUE}, the user is asked before each plot.
#' @param \dots additional graphical parameters.
#' @param vnam  a name of the variable to apperar on plots.
#' 
#' @references NIST/SEMATECH e-Handbook of Statistical Methods, April, 2012. \href{4-plot}{http://www.itl.nist.gov/div898/handbook/eda/section3/4plot.htm}
#' 
#' @importFrom stats acf density qqnorm qnorm
#' @importFrom graphics lines plot
#' @export

diagnplot <- function(x, plots = c("runseq", "lag", "hist", "qqnorm"),
                      ask = TRUE, ..., vnam) {
   stopifnot(is.numeric(x))
   
   if ("all" %in% plots)
      plots <- c("runseq", "lag", "acf", "hist", "density", "qqnorm")
   
   if (missing(vnam))
      vnam <- paste(deparse(substitute(x), 500), collapse = "\n")
   
   k <- length(plots)
   
   for (i in 1:k) {
      switch(plots[i],
            "runseq" = {
                  plot(x, main=sprintf("Run sequence plot of %s", vnam), ylab=vnam, ...)
                  lines(1:length(x), loe(x), col="red")
                  if (ask && i < k) readline("Hit <Return> to see next plot: ")
               },
            "lag" = {
                  eval(substitute({
                     lag.plot(x, main=sprintf("Lag plot of %s", vnam), ...)
                  }))
                  if (ask && i < k) readline("Hit <Return> to see next plot: ")
               },
            "acf" = {
                  acf(x, main=sprintf("Autocorrelation plot of %s", vnam), ...)
                  if (ask && i < k) readline("Hit <Return> to see next plot: ")
               },
            "hist" = {
                  hist(x, main=sprintf("Histogram of %s", vnam), xlab=vnam, ...)
                  if (ask && i < k) readline("Hit <Return> to see next plot: ")
               },
            "density" = {
                  plot(density(x), main=sprintf("Density plot of %s", vnam), ...)
                  if (ask && i < k) readline("Hit <Return> to see next plot: ")
               },
            "qqnorm" = {
                  qqnorm(x, main=sprintf("Normal Q-Q plot of %s", vnam), ...)
                  if (ask && i < k) readline("Hit <Return> to see next plot: ")
               }
            )
   }
}
twolodzko/twextras documentation built on May 3, 2019, 1:52 p.m.