blk.time.above.threshold: Compute Time Above a Threshold by Linear Interpolation.

View source: R/PCSmisc.R

blk.time.above.thresholdR Documentation

Compute Time Above a Threshold by Linear Interpolation.

Description

Compute the time above a threshold by linear interpolation within each block of a block-format data set.

Usage

blk.time.above.threshold(x, y, threshold, id, id2 = id, ind = NULL, fill = NA, diff.op.x = "-", diff.op.y = "-", ...)

Arguments

x,y

A vector in block-format with respect to id.

threshold

A numeric vector of length one specifying the threshold.

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.x

A function that computes differences in x.

diff.op.y

A function that computes differences in y.

...

Further arguments passed to diff.op.x and diff.op.y.

Details

This function operates on data sets in block-format. Within each block, the points on a graph are determined by x and y. A subset of points to include can be specified with ind. Then, within each block, the time above threshold is computed by linear interpolation.

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

threshold <- 5
x0 <- 1:20
y0 <- 0.1 + runif(20, 0, 0.9)
y0[6] <- y0[5]
y0[14] <- y0[11]
y0[c(16)] <- 0
y0[c(2, 3, 4, 7, 8, 9, 15, 16, 18)] <- 0
y0[c(1, 10, 11, 12, 13, 14, 15, 19, 20)] <- -y0[c(1, 10, 11, 12, 13, 14, 15, 19, 20)]
y0[c(1, 10, 19)] <- -y0[c(5, 6, 17)]
y0 <-(1 + y0)*threshold
x <- x0[c(1, 5, 6, 10, 11, 14, 16, 17, 19, 20)]
y <- y0[c(1, 5, 6, 10, 11, 14, 16, 17, 19, 20)]
plot(x0, y0, type="n", ylim=c(0, 2*threshold)); abline(h=threshold, col="red", lwd=3)
points(x0, rep(threshold, length(x0)), col="red", pch=19, cex=1)
lines(x, y, type="b", col="blue", lwd=2)
t <- blk.time.above.threshold(x, y, threshold, id=asID(rep(1, length(x2))), id2=NULL)
stopifnot(t == 7)

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