blk.locf: Last Observation Carried Forward

View source: R/PCSmisc.R

blk.locfR Documentation

Last Observation Carried Forward

Description

Performs last observation carried forward (LOCF) imputation on a column of data.

Usage

blk.locf(x, id, na.action = c("fill", "carry.back"), fill = NA)
blk.locf2(time, id, observ.x, observ.t, id2 = id, na.action = c("fill", "carry.back"), fill = NA)
blk.nocb(x, id, na.action = c("fill", "carry.forward"), fill = NA)

Arguments

x

A vector in block-format with respect to id, containing both observed values and missing values, which must be ordered chronologically within each block.

id,id2

A valid block-format ID.

time

A vector of times at which to impute values, in block-format with respect to 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.

na.action

How to handle times prior to the first observation. See details.

fill

A value to use when no other value is appropriate.

Details

These functions operate on data sets in block-format. The simpler blk.locf fills in the missing values in x using the observed values. Time is implicit; the time-ordering of x (within blocks of id) is required.

For blk.locf2 observations and corresponding times are specified separately from the times at which imputation is desired. This function is essentially equivalent to blk.nearestMatch with direction = "forward" and tol = Inf.

The parameter na.action is used to control the result when there is no previous non-missing value available. With na.action = "fill" the valued specified with fill is used. With na.action = "carry.back", the earliest available non-missing future value is used. If there is no non-missing observed values for a level of id, the result is NA for that id at all time points.

Less frequently used is next observation carried back (NOCB). Here, future observations are used to fill in the missing values at earlier times.

Value

A vector in block-format with respect to id.

Author(s)

Benjamin Rich <mail@benjaminrich.net>

See Also

  • block-format

  • blk.nearestMatch

Examples

require(nlme)
data(Phenobarb)
dat <- Phenobarb[1:56,] # First 4 subjects
dat <- dat[order(dat$Subject, dat$time),]
attach(dat)

cbind(dat, LOCF=blk.locf(conc, Subject))
cbind(dat, LOCF=blk.locf(conc, Subject, na.action="carry.back"))

detach(dat)

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