Skip to content

Commit

Permalink
additional documentation and clarified code
Browse files Browse the repository at this point in the history
  • Loading branch information
MThomas91 authored and MThomas91 committed Jan 24, 2025
1 parent 6e158b8 commit b8987e2
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 21 deletions.
28 changes: 16 additions & 12 deletions R/Mods.R
Original file line number Diff line number Diff line change
Expand Up @@ -311,8 +311,8 @@ plot.Mods <- function(x, nPoints = 200, superpose = FALSE, xlab = "Dose",
}


#' Calculate dose estimates for a fitted dose-response model (via \code{\link{fitMod}} or \code{\link{bFitMod}}) or a
#' \code{\link{Mods}} object
#' Calculate dose estimates for a fitted dose-response model (via \code{\link{fitMod}}, \code{\link{bFitMod}})
#' or \code{\link{maFitMod}}) or a \code{\link{Mods}} object
#'
#' @description The TD (target dose) is defined as the dose that achieves a target effect of Delta over placebo (if
#' there are multiple such doses, the smallest is chosen):
Expand Down Expand Up @@ -348,7 +348,7 @@ plot.Mods <- function(x, nPoints = 200, superpose = FALSE, xlab = "Dose",
#' @param direction Direction to be used in defining the TD. This depends on whether an increasing
#' or decreasing of the response variable is beneficial. In case of ED calculation only needed for maFit objects.
#' @param doses Dose levels to be used if \samp{TDtype} or \samp{EDtype} are
#' equal to \samp{"discrete"}. Needs to include placebo, .
#' equal to \samp{"discrete"}. Needs to include placebo, and may not exceed the dose range of the model(s) provided in \samp{object}.
#'
#' @return Returns the dose estimate
#'
Expand Down Expand Up @@ -389,7 +389,8 @@ TD <- function(object, Delta, TDtype = c("continuous", "discrete"),
scal <- attr(object, "scal")
nodes <- attr(object, "doses")
maxD <- max(attr(object, "doses"))
if(any(doses > maxD))
TDtype <- match.arg(TDtype)
if(TDtype == "discrete" & any(doses > maxD))
stop("Doses provided may not exceed the observed dose range")
## loop through list
for(nam in names(object)){
Expand Down Expand Up @@ -417,7 +418,8 @@ TD <- function(object, Delta, TDtype = c("continuous", "discrete"),
nodes <- attr(object, "nodes")
doseNam <- attr(object, "doseRespNam")[1]
maxD <- max(attr(object,"data")[[doseNam]])
if(any(doses > maxD))
TDtype <- match.arg(TDtype)
if(TDtype == "discrete" & any(doses > maxD))
stop("Doses provided may not exceed the observed dose range")
if(attr(object, "placAdj")){
par <- c(0, par)
Expand All @@ -435,7 +437,8 @@ TD <- function(object, Delta, TDtype = c("continuous", "discrete"),
nodes <- attr(object, "nodes")
doseNam <- attr(object, "doseRespNam")[1]
maxD <- max(attr(object,"data")[[doseNam]])
if(any(doses > maxD))
TDtype <- match.arg(TDtype)
if(TDtype == "discrete" & any(doses > maxD))
stop("Doses provided may not exceed the observed dose range")
if(attr(object, "placAdj")){
if(nam == "linInt")
Expand All @@ -456,7 +459,7 @@ TD <- function(object, Delta, TDtype = c("continuous", "discrete"),
TDtype <- match.arg(TDtype)
maxD <- max(object$args$dose)
if(TDtype == "discrete"){
if(missing(doses))
if(is.null(doses))
stop("For TDtype = \"discrete\" need the possible doses in doses argument")
if(doses[1] != 0)
stop("need placebo dose for TD calculation")
Expand Down Expand Up @@ -560,7 +563,7 @@ ED <- function(object, p, EDtype = c("continuous", "discrete"),
EDtype <- match.arg(EDtype)
maxD <- max(object$args$dose)
if(EDtype == "discrete"){
if(missing(doses))
if(is.null(doses))
stop("For EDtype = \"discrete\" need the possible doses in doses argument")
if(!any(doses == 0))
stop("need placebo dose for ED calculation")
Expand All @@ -584,11 +587,12 @@ ED <- function(object, p, EDtype = c("continuous", "discrete"),

difs <- (pred_med - pred_med[1])
ind <- which(difs > p*max(difs))
if(EDtype == "discrete")
ind <- ind[ind <= length(doses)] ## only include doses that where initially included
if(length(ind) == 0)
return(NA)

if (length(ind)>0) {
return(min(doseSeq[ind]))
edose <- min(doseSeq[ind])
if (EDtype == "continuous" | edose %in% doses) {## don't return maxD if it was not in originally provided doses for discrete type
return(edose)
} else {
return(NA)
}
Expand Down
9 changes: 5 additions & 4 deletions R/Mods_helpers.R
Original file line number Diff line number Diff line change
Expand Up @@ -605,11 +605,12 @@ calcED <- function(model, pars, p, maxD, EDtype = c("continuous", "discrete"),
}
}
ind <- which(resp > p*maxResp)
if(EDtype == "discrete")
ind <- ind[ind <= length(doses)] ## only include doses that where initially included
if(length(ind) == 0)
return(NA)

if(length(ind) > 0){ ## ED does exist return smallest dose fulfilling threshold
return(min(doseSeq[ind]))
edose <- min(doseSeq[ind])
if (EDtype == "continuous" | edose %in% doses) {## don't return maxD if it was not in originally provided doses for discrete type
return(edose)
} else {
return(NA)
}
Expand Down
4 changes: 2 additions & 2 deletions R/maFitMod.R
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,8 @@ maFitMod <- function(dose, resp, S, models,
#' @param doseSeq Where to calculate predictions.
#' @param ... Further arguments (currently ignored)
#' @rdname maFitMod
#' @method predict maFitMod
#' #' @export
#' @method predict maFit
#' @export
predict.maFit <- function(object,
summaryFct = function(x) quantile(x, probs = c(0.025, 0.25, 0.5, 0.75, 0.975)),
doseSeq = NULL,
Expand Down
6 changes: 3 additions & 3 deletions man/targdose.Rd

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

0 comments on commit b8987e2

Please sign in to comment.