Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add option for generating guest function outputs for residualVsObserv… #176

Merged
2 changes: 1 addition & 1 deletion R/aaa-utilities.R
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ parseAddLineLayer <- function(type, value, position) {
parse(text = paste0(
"plotObject <- plotObject + ",
switch(type,
"horizontal" = paste0("ggplot2::geom_hline(yintercept = ", value, ","),
"horizontal" = paste0("ggplot2::geom_abline(slope=0, intercept = ", value, ","),
"vertical" = paste0("ggplot2::geom_vline(xintercept = ", value, ","),
"diagonal" = paste0("ggplot2::geom_abline(slope=1, intercept = ", value, ",")
),
Expand Down
9 changes: 7 additions & 2 deletions R/ddiratio-datamapping.R
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,21 @@ DDIRatioDataMapping <- R6::R6Class(
minRange = NULL,
#' @field deltaGuest Value of `delta` in Guest et al. equation
deltaGuest = NULL,
#' @field residualsVsObserved if true, returns a mapping for generating a residuals (predicted/observed) vs observed DDI ratio plot. If false (default) returns a mapping for predicted vs observed DDI ratio plot.
residualsVsObserved = NULL,

#' @description Create a new `DDIRatioDataMapping` object
#' @param deltaGuest Value of parameter `delta` in Guest et al. equation.
#' Default value is 1.
#' @param minRange Mininmum range for guest lines
#' @param lines list of ratio limits to plot as diagonal lines
#' @param ... parameters inherited from `PKRatioDataMapping`
#' @return A new `DDIRatioDataMapping` object
#' @param residualsVsObserved if true, returns a mapping for generating a residuals (predicted/observed) vs observed DDI ratio plot. If false (default) returns a mapping for predicted vs observed DDI ratio plot.
#' @param ... parameters inherited from \code{PKRatioDataMapping}
#' @return A new \code{DDIRatioDataMapping} object
initialize = function(deltaGuest = 1,
minRange = c(1e-2, 1e2),
lines = DefaultDataMappingValues$ddiRatio,
residualsVsObserved = FALSE,
...) {
super$initialize(...)
# Apply log10 transformation to lines because
Expand All @@ -28,6 +32,7 @@ DDIRatioDataMapping <- R6::R6Class(
self$lines <- lapply(lines, log10)
self$minRange <- minRange
self$deltaGuest <- deltaGuest
self$residualsVsObserved <- residualsVsObserved
}
)
)
6 changes: 5 additions & 1 deletion R/obs-vs-pred-datamapping.R
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,11 @@ ObsVsPredDataMapping <- R6::R6Class(
validateIsIncluded(smoother, c("lm", "loess"), nullAllowed = TRUE)

super$initialize(...)
self$lines <- lines

# Apply log10 transformation to lines because
# plot is log scaled in by default and geom_abline
# requires the log transformed values in input of intercept
self$lines <- lapply(lines, log10)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does default value in DefaultDataMappingValues include the log10 ?
If so, line 27 may apply twice the log.
This means that either we need lapply(lines, log10) %||% DefaultDataMappingValues$obsVsPred with lines = NULL in input or that DefaultDataMappingValues account for scale somehow

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we may want to apply the log10 directly within the plotXX functions after checking the plotConfiguration scale is included in Scaling$log (obs vs pred and res vs pred might be in linear scale).
Otherwise, users can get unwanted log transformation of their diagonal/horizontal lines.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If possible could you document the scaling in the @param lines and maybe in the pk-ratio vignette

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reverting changes to obs-vs-pred-datamapping.R and pkratio-datamapping.R for now.

self$smoother <- smoother
}
)
Expand Down
6 changes: 5 additions & 1 deletion R/pkratio-datamapping.R
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,11 @@ PKRatioDataMapping <- R6::R6Class(
...) {
validateIsString(uncertainty, nullAllowed = TRUE)
super$initialize(...)
self$lines <- lines

# Apply log10 transformation to lines because
# plot is log scaled in by default and geom_abline
# requires the log transformed values in input of intercept
self$lines <- lapply(lines, log10)
self$uncertainty <- uncertainty
},

Expand Down
18 changes: 14 additions & 4 deletions R/plot-ddiratio.R
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,15 @@ plotDDIRatio <- function(data,

plotObject <- plotObject %||% initializePlot(plotConfiguration)

lineOrientation <- "diagonal"
if(dataMapping$residualsVsObserved){
lineOrientation <- "horizontal"
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add a shortcut option for users in the function input as well: e.g. residuals = NULL ?
and internally do something like

validateIsLogical(residuals, nullAllowed = TRUE)
residuals <- residuals %||% dataMapping$residualsVsObserved`
lineOrientation <- "diagonal"
if(residuals){lineOrientation <- "horizontal"}

I think such options are quite convenient for plotTornado and plotBoxWhisker and might be valuable for users in plotDDIRatio

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe use residualsVsObserved instead of residuals to keep it consistent with dataMapping

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done, but with residualsVsObserved = FALSE by default and nullAllowed = FALSE


# Include horizontal lines
for (lineIndex in seq_along(dataMapping$lines)) {
# position correspond to the number of layer lines already added
eval(parseAddLineLayer("diagonal", dataMapping$lines[[lineIndex]], lineIndex - 1))
eval(parseAddLineLayer(lineOrientation, dataMapping$lines[[lineIndex]], lineIndex - 1))
}
if (isOfLength(lineIndex, 0)) {
lineIndex <- 0
Expand Down Expand Up @@ -81,22 +86,27 @@ getGuestValuesFromDataMapping <- function(data,
xmax <- max(dataMapping$minRange, xData[xData > 0])
# By default, use 500 points to get enough discretization for the plot
x <- 10^(seq(log10(xmin), log10(xmax), length.out = 5e2))
return(getGuestValues(x, delta = dataMapping$deltaGuest))
return(getGuestValues(x, delta = dataMapping$deltaGuest, residualsVsObserved = dataMapping$residualsVsObserved))
}

#' @title getGuestValues
#' @description Get a data.frame with Guest et al. ratio limits
#' @param x input values of Guest function
#' @param delta parameter of Guest function
#' @param residualsVsObserved if true, returns a dataframe for a residuals (predicted/observed) vs observed DDI ratio plot
#' @return A data.frame with x, ymin and ymax defining Guest et al. limits
#' @export
getGuestValues <- function(x, delta = 1) {
getGuestValues <- function(x, delta = 1, residualsVsObserved = FALSE) {
xSym <- x
xSym[x < 1] <- 1 / x[x < 1]
limit <- (delta + 2 * (xSym - 1)) / xSym
ymin <- x / limit
ymax <- x * limit

guestLines <- data.frame(x, ymin, ymax)
if(residualsVsObserved){
guestLines <- data.frame(x = x, ymin =1/limit, ymax = limit)
return(guestLines)
}
guestLines <- data.frame(x = x, ymin = ymin, ymax = ymax)
return(guestLines)
}
5 changes: 5 additions & 0 deletions man/DDIRatioDataMapping.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions man/enum.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion man/getGuestValues.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions tests/testthat/test-pkratio.R
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ test_that("PK Ratio default settings work", {
expect_null(pkRatioMapping$x)
expect_null(pkRatioMapping$y)
expect_equal(pkRatioMapping$lines, list(
pkRatio1 = 1,
pkRatio2 = c(1.5, 1 / 1.5),
pkRatio3 = c(2, 1 / 2)
pkRatio1 = log10(1),
pkRatio2 = log10(c(1.5, 1 / 1.5)),
pkRatio3 = log10(c(2, 1 / 2))
))

expect_is(pkRatioConfig$labels$title, "Label")
Expand Down