Title: | Basic Calculations for PK/PD Modeling |
---|---|
Description: | Essentials for PK/PD (pharmacokinetics/pharmacodynamics) such as area under the curve, (geometric) coefficient of variation, and other calculations that are not part of base R. This is not a noncompartmental analysis (NCA) package. |
Authors: | Philip Delff [aut, cre] |
Maintainer: | Philip Delff <[email protected]> |
License: | MIT + file LICENSE |
Version: | 0.0.4 |
Built: | 2025-02-17 02:43:42 UTC |
Source: | https://github.com/philipdelff/nmcalc |
Calculate coefficient of variation of data
CV(x, log = FALSE)
CV(x, log = FALSE)
x |
The data |
log |
If TRUE, the geometric coefficient of variation is calculated. This is sqrt(exp(var(log(x))-1). |
This function is intended to be used on data. For a log-normal THETA1*EXP(ETA(1)) 'Nonmem' parameter, do CV=sqrt(exp(OMEGA[1,1])-1).
A numeric
set.seed(139) x1 <- rnorm(1000,mean=5) CV(x1) CV(x1,log=TRUE) x2 <- exp(x1) CV(x2) CV(x2,log=TRUE)
set.seed(139) x1 <- rnorm(1000,mean=5) CV(x1) CV(x1,log=TRUE) x2 <- exp(x1) CV(x2) CV(x2,log=TRUE)
OMEGA
metrix.CV of log-normal dist baed on omega parameters
CV based variance like provided in Nonmem's OMEGA
metrix.
CVlnorm(omega)
CVlnorm(omega)
omega |
A variance as provided in diagonal om the Nonmem OMEGA matrix |
This is a very simple function. All it does is
sqrt(exp(omega)-1)
.
CV of the distribution (numeric)
Inverse logit function
invlogit(x)
invlogit(x)
x |
a number to transform |
A numeric
logit
Logit function
logit(x)
logit(x)
x |
a number to transform |
A numeric
invlogit
calculate arithmetic or geometric mean and confidence intervals
means( x, type = "arithmetic", na.rm = FALSE, z.rm = FALSE, ci = FALSE, dist.ci = "t", p.ci = 0.95, colnames = c("est", "ll", "ul"), format = "df" )
means( x, type = "arithmetic", na.rm = FALSE, z.rm = FALSE, ci = FALSE, dist.ci = "t", p.ci = 0.95, colnames = c("est", "ll", "ul"), format = "df" )
x |
vector to calculate the geometric mean of |
type |
type of mean or median. Default is arithmetic, geometric and median are available as well. Only first letters needed, so say "geo" or even "g" is enough. |
na.rm |
Remove NA's before doing calculations? |
z.rm |
removes zeros before calculation? Default is FALSE. Can only be TRUE if type="geometric". |
ci |
if TRUE, a data.frame including point estimate and confidence interval returned. If FALSE, a numeric representing the mean value returned. |
dist.ci |
The distribution to use for the confidence interval. Default and only supported is "t". If type=geometric, this is applied after transformation to gaussian. |
p.ci |
probability covered by confidence interval. Default is 0.95 |
colnames |
If ci, this defines the column names of the resulting data frame. Default is c("est","ll","ul"). |
format |
The format of the result. Possible values are df and num. |
If ci=FALSE, a numeric. If ci=TRUE, a data.frame.
x <- 1:100 means(x, type="arithmetic", ci=TRUE) means(x, type="geometric", ci=TRUE) means(x, type="median", ci=TRUE) library(data.table) ## CRAN requires examples to run on a single thread data.table::setDTthreads(1) data.table(x=x)[,append(means(x,ci=TRUE),list(N=.N))]
x <- 1:100 means(x, type="arithmetic", ci=TRUE) means(x, type="geometric", ci=TRUE) means(x, type="median", ci=TRUE) library(data.table) ## CRAN requires examples to run on a single thread data.table::setDTthreads(1) data.table(x=x)[,append(means(x,ci=TRUE),list(N=.N))]
This is simple stuff, but I can never remember the exact quantile and findInterval/cut commands to use. quantbin finds quantiles using quantile and then assigns bins using either findInterval or cut.
quantbin(x, nbins, probs, label = "num", ...)
quantbin(x, nbins, probs, label = "num", ...)
x |
The observations |
nbins |
Number of bins to use |
probs |
Quantiles for construvtion of bins (optional). The
default is to spread |
label |
label="num" gives a numeric bin number (findInterval). label="interval" gives a character representation of the interval (cut). |
... |
additional arguments passed to quantile. |
quantbin uses stats::quantile for quantile estimation. Except for x and probs, all parameters can be controlled using na.rm and ... arguments. See ?stats::quantile for details.
na.rm na.rm=TRUE is needed for quantile to be able to estimate the distribution if x contains NA's. Notice, if na.rm=T, an NA element in x will still result in an NA element in return. If na.rm=F and there are NA's in x, all elements will be NA in result (quantiles cannot be determined, nor can the binning of x by those quantiles).
If data is not continuous, this method may not lead to balanced distributions.
If label="num", integers. If label="interval", factors.
set.seed(134) library(data.table) ## CRAN requires examples to run on a single thread data.table::setDTthreads(1) dt1 <- data.table(x=rnorm(n=1000)) dt1[,bin:=quantbin(x,nbins=4,label="num")] dt1[,int:=quantbin(x,nbins=4,label="interval")] ## perfect - flat distribution dt1[,.N,keyby=.(bin,int)] dt2 <- data.table(x=c(rnorm(n=100000),NA)) dt2[,bin:=quantbin(x,nbins=4,label="num",na.rm=TRUE)] dt2[,int:=quantbin(x,nbins=4,label="interval",na.rm=TRUE)] ## perfect - flat distribution dt2[,.N,keyby=.(bin,int)] unique(dt2[,.(bin,int)])[order(bin)] ## we may not get a flat distribution in case of discrete observations dt3 <- data.table(x=c(sample(1:3,100,replace=TRUE))) dt3[,bin:=quantbin(x,nbins=2,label="num",na.rm=TRUE)] dt3[,int:=quantbin(x,nbins=2,label="interval",na.rm=TRUE)] ## Not a flat distribution dt3[,.N,keyby=.(x,bin,int)]
set.seed(134) library(data.table) ## CRAN requires examples to run on a single thread data.table::setDTthreads(1) dt1 <- data.table(x=rnorm(n=1000)) dt1[,bin:=quantbin(x,nbins=4,label="num")] dt1[,int:=quantbin(x,nbins=4,label="interval")] ## perfect - flat distribution dt1[,.N,keyby=.(bin,int)] dt2 <- data.table(x=c(rnorm(n=100000),NA)) dt2[,bin:=quantbin(x,nbins=4,label="num",na.rm=TRUE)] dt2[,int:=quantbin(x,nbins=4,label="interval",na.rm=TRUE)] ## perfect - flat distribution dt2[,.N,keyby=.(bin,int)] unique(dt2[,.(bin,int)])[order(bin)] ## we may not get a flat distribution in case of discrete observations dt3 <- data.table(x=c(sample(1:3,100,replace=TRUE))) dt3[,bin:=quantbin(x,nbins=2,label="num",na.rm=TRUE)] dt3[,int:=quantbin(x,nbins=2,label="interval",na.rm=TRUE)] ## Not a flat distribution dt3[,.N,keyby=.(x,bin,int)]
Useful for generating sequences to be plotted on log scale. This is really simple - seq is run on from and to after log transformation, then the exponential is reported.
seqlog(from, to, length.out)
seqlog(from, to, length.out)
from |
start of sequence |
to |
end of sequence |
length.out |
length of sequence |
A numeric vector.
df <- data.frame(x=seqlog(1,100,100)) df <- transform(df, y=x/(10+x)) ## Not run: library(ggplot2) ## the points are equidistant on the log x scale ggplot(df,aes(x,y))+geom_point()+scale_x_log10() ## End(Not run)
df <- data.frame(x=seqlog(1,100,100)) df <- transform(df, y=x/(10+x)) ## Not run: library(ggplot2) ## the points are equidistant on the log x scale ggplot(df,aes(x,y))+geom_point()+scale_x_log10() ## End(Not run)
Even if theoretically correct, the built-in 'R' functions 'round' and 'signif' can be confusing (see examples). 'signif2' is a simple solution that can be used for reporting results consistently.
signif2(x, digits = 1, add, ...)
signif2(x, digits = 1, add, ...)
x |
a numeric vector. |
digits |
number of significant digits to round to. Must be an integer larger than 0. |
add |
pad with zeros where digits>nchar(x[i]). Currently not used. |
... |
additional arguments passed to formatC. |
A character vector.
x <- c(1.24e-4,1.1334e6,1.1,22.00000,10.00,1) data.frame(x,s.3=signif(x,3),sc.3=as.character(signif(x,3)),s2.3=signif2(x,3)) signif2(c(.2,11.84),2) ## digits has no effect when x==0 signif2(0,1) signif2(0,3)
x <- c(1.24e-4,1.1334e6,1.1,22.00000,10.00,1) data.frame(x,s.3=signif(x,3),sc.3=as.character(signif(x,3)),s2.3=signif2(x,3)) signif2(c(.2,11.84),2) ## digits has no effect when x==0 signif2(0,1) signif2(0,3)
This is a numerical integration of y with respect to x by the trapezoidal method on linear scale.
trapez(x, y, cum = FALSE, na.rm = FALSE)
trapez(x, y, cum = FALSE, na.rm = FALSE)
x |
The vector to integrate y with respect to (typically TIME to get area under the curve). |
y |
The variable to integrate. |
cum |
Return the cumulative trapezoidal area under the curve? If false (default) a single number is returned. If true, a vector is returned. Notice, the vector is one element shorter than x and y. |
na.rm |
Remove indexes in x and y wherever x or y are NA. |
a numeric