blk.nearestMatch: Impute Values Using the Nearest Match

View source: R/PCSmisc.R

blk.nearestMatchR Documentation

Impute Values Using the Nearest Match

Description

This function imputes the value of a quantity that would have been observed at a given time using the value that was observed nearest in time in a block-format data set.

The imputation can be done forwards of backwards in time, and can be constrained such that the value is only imputed if the observation occurred within a certain time window.

Usage

blk.nearestMatch(time, id, observ.x, observ.t, id2 = id, direction = c("forward", "backward", "both"), tol = Inf, fill = NA, na.rm = FALSE, diff.op = difftime.default)

Arguments

time

A vector of times at which to impute values, in block-format with respect to id.

id,id2

A valid block-format ID.

observ.x

A vector of obseved values in block-format with respect to id2.

observ.t

A vector of observation times corresponding to observ.x, in block-format with respect to id2.

direction

Should value be carried forward in time, backward in time, or both?

tol

A numeric vector of length 1 or 2. The time window or tolerance for matching. See details.

fill

A value to use when no other value is appropriate.

na.rm

Should missing value in observ.x be removed?

diff.op

A function that subtracts one time from another.

Details

For a given list of times, finds the observed values that are closest in time, searching either backward in time or forward in time. This is done for each block of time in a block-format data set.

For a given time, a match is declared if an observation occurs within a specific time window. The size of the time window is specified with the tolerance parameter tol. If tol has length 2 then the first number is the tolerance for matching in the “forward” direction and the second number is the tolerance for matching in the “backward” direction; if tol has length 1 then this is the tolerance for matching in both directions.

If two or more observations that are equidistant from a time value according to diff.op, then one of the values is selected arbitrarily. Forward matching value are selected in preference to backward matching values (when direction == "both").

Value

A vector in block-format with respect to id containig the nearest match imputed values at times time from observ.x.

Author(s)

Benjamin Rich <mail@benjaminrich.net>

See Also

  • block-format

  • blk.locf2


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