blk.AUC.by.trapezoid: Compute Area Under a Curve by the Trapezoid Rule.

View source: R/PCSmisc.R

blk.AUC.by.trapezoidR Documentation

Compute Area Under a Curve by the Trapezoid Rule.

Description

Compute area under a curve (AUC) by the trapezoid rule within each block of a block-format data set.

Usage

blk.AUC.by.trapezoid(x, y, id, id2 = id, ind = NULL, fill = NA, diff.op = "-", ...)

Arguments

x,y

A vector in block-format with respect to id.

id

A valid block-format ID.

id2

A valid block-format ID for the return value, or NULL

ind

A logical vector that designates a subset of points. By default all points are included.

fill

A value to use when no other value is appropriate.

diff.op

A function that computes differences in x.

...

Further arguments passed to diff.op.

Details

This function operates on data sets in block-format. Within each block, the points on a curve are determined by x and y. A subset of points to include can be specified with ind. Then, within each block, AUC is computed by the trapezoid rule.

The parameter id2 determines the return value. If id2 is NULL, the returned vector contains one element for each level of id, with fill used for blocks of size zero.

Value

If id2 is NULL, a vector containing one value for each level of id. Otherwise, a vectors in block-format with respect to id2.

Author(s)

Benjamin Rich <mail@benjaminrich.net>

See Also

block-format

Examples

data(pkstudy.PC)

df <- with(pkstudy.PC, data.frame(
    id       = asID(subjid),
    datetime = strptime(pcdtc, "%Y-%m-%dT%H:%M", tz="UTC"),
    reltime  = NA,
    conc     = ifelse(grepl("^BQL", pcstresc), 0, pcstresn)))

# Simulate missing values
df$conc[c(3, 25)] <- NA

df$reltime <- with(df, blk.relativeTime(datetime, id))

# EXAMPLE 1 - id2=NULL
with(df, blk.AUC.by.trapezoid(x=reltime, y=conc, id=id, id2=NULL, ind=!is.na(conc)))

# EXAMPLE 2 - id2=id (default)
df$auc <- with(df, blk.AUC.by.trapezoid(x=reltime, y=conc, id=id, ind=!is.na(conc)))

# EXAMPLE 3 - use datetime directly and speficy diff.op
df$auc2 <- with(df, blk.AUC.by.trapezoid(x=datetime, y=conc, id=id, ind=!is.na(conc), diff.op=difftime.hours))

# See results:
head(df, 30)

benjaminrich/PCSmisc documentation built on Feb. 11, 2024, 9:25 p.m.