certara/tidyvpc

Failure for group with single x-level

billdenney opened this issue · 2 comments

The default of "jenks" binning fails when there is a single value for a group. My first thought is that if there is a single x-value for a group then perhaps a narrow range around the x-range and do the VPC calculations just on that value with the binning as that value by itself, but there definitely could be other ideas.

The underlying error below comes from the classInt package, but I think that makes sense for classInt to give an error and we should probably detect single-value groups before sending it for interval selection.

library(tidyverse)
#> Warning: package 'ggplot2' was built under R version 4.3.1
#> Warning: package 'purrr' was built under R version 4.3.1
library(tidyvpc)
#> tidyvpc is part of Certara.R!
#> Follow the link below to learn more about PMx R package development at Certara.
#> https://certara.github.io/R-Certara/

d_obs <-
  data.frame(
    group = rep(c("Patient", "Healthy"), each = 5),
    conc = c(rep(0, 5), 1:5),
    value = 1:10
  )

d_sim <-
  d_obs[rep(1:nrow(d_obs), 5), ]

observed(d_obs, x = conc, yobs = value) %>%
  simulated(d_sim, xsim = conc, ysim = value) %>%
  stratify(~group) %>%
  binning(bin = "jenks") %>%
  vpcstats(qpred = c(0.1, 0.5, 0.9))
#> Error in (function (var, n, style = "quantile", rtimes = 3, ..., intervalClosure = c("left", : single unique value

Created on 2023-09-12 with reprex v2.0.2

FYI, I'm working on a PR that should address this.

Alright, the potential fixes are in the indicated PRs. I made it as two separate PRs since the calculation part seemed straight-forward and may require less thought/discussion while the plotting part seemed more opinion-based, so it may require more thought/discussion.