goodekat / ggResidpanel

An R package for creating a panel of diagnostic plots for residuals from a model.
Other
36 stars 3 forks source link

Add worm-plot? #8

Open mattansb opened 4 years ago

mattansb commented 4 years ago

Might consider adding worm-plots - an upgraded qqplot with CI boudries (from https://doi.org/10.1002/sim.746).

Example code (from https://gist.github.com/mattansb/ba2f1b9dbd9b4479ae31826a6cc32a80) that also works with non-normal:

qq_worm_plot <- function(x, distribution = "norm", ...) {
  d <- match.fun(paste0("d",distribution))
  p <- match.fun(paste0("p",distribution))
  q <- match.fun(paste0("q",distribution))
  dparams <- list(...)

  if (!require(ggplot2)) stop("Need 'ggplot2' to use this function")

  worm_ci_UL <- function(nx){
    1.96 * sqrt(p(nx, ...) * (1 - p(nx, ...)) / length(nx)) / d(nx, ...)
  }

  worm_ci_LL <- function(nx){
    -1.96 * sqrt(p(nx, ...) * (1 - p(nx, ...)) / length(nx)) / d(nx, ...)
  }

  ggplot(mapping = aes(sample = x)) + 
    stat_qq(aes(y = stat(sample - theoretical)), distribution = q, dparams = dparams) + 
    stat_qq(aes(y = stat(worm_ci_UL(theoretical))), geom = "line", distribution = q, dparams = dparams) + 
    stat_qq(aes(y = stat(worm_ci_LL(theoretical))), geom = "line", distribution = q, dparams = dparams) + 
    labs(x = paste0("theoretical ", distribution, " quantiles")) + 
    NULL
}

x <- rt(200, df = 4)
qq_worm_plot(x)
#> Loading required package: ggplot2


qq_worm_plot(x, distribution = "t", df = 4)

Created on 2020-01-31 by the reprex package (v0.3.0)

goodekat commented 4 years ago

Thanks for the suggestion! This is a good idea. We do have confidence interval boundaries available with the current implementation of the qqplot. These make use of the computations available from the qqplotr package. We could even work on adjusting the regular qqplot to allow for non-normal boundaries.

mattansb commented 4 years ago

Missed the qqbands option, thanks!

Would it be possible to add the detrend argument from qqplotr?

goodekat commented 4 years ago

Oh! Good idea. I will add that to the to do list.