In longitudinal studies it's common that individuals drop out before all responses can be obtained. Measurements obtained before the individual dropped out can be used to impute the unknown measurement(s). The last observation carried forward method is one way to impute values for the missing observations. For the last observation carried forward (LOCF) approach the missing values are replaced by the last observed value of that variable for each individual regardless of when it occurred.

LOCF() replaces NAs with the most recent non-NA prior to it.

LOCF(x)

# S3 method for default
LOCF(x)
# S3 method for data.frame
LOCF(x)
# S3 method for matrix
LOCF(x)

Arguments

x

a vector, a data.frame or a matrix containing NAs.

Details

The function will replace all NAs found in a vector with the last earlier value not being NA. In data.frames each column will be treated as described.

It should be noted, that the last observation carried forward approach may result in biased estimates and may underestimate the variability.

Value

a vector with the same dimension as x.

Author

Daniel Wollschlaeger <dwoll@psychologie.uni-kiel.de>

See also

See also the package Hmisc for less coarse imputation functions.

Examples

d.frm <- data.frame( tag=rep(c("mo", "di", "mi", "do", "fr", "sa", "so"), 4) , val=rep(c(runif(5), rep(NA,2)), 4) ) d.frm$locf <- LOCF( d.frm$val ) d.frm
#> tag val locf #> 1 mo 0.3191548 0.3191548 #> 2 di 0.2087307 0.2087307 #> 3 mi 0.8284840 0.8284840 #> 4 do 0.9706503 0.9706503 #> 5 fr 0.6544848 0.6544848 #> 6 sa NA 0.6544848 #> 7 so NA 0.6544848 #> 8 mo 0.3191548 0.3191548 #> 9 di 0.2087307 0.2087307 #> 10 mi 0.8284840 0.8284840 #> 11 do 0.9706503 0.9706503 #> 12 fr 0.6544848 0.6544848 #> 13 sa NA 0.6544848 #> 14 so NA 0.6544848 #> 15 mo 0.3191548 0.3191548 #> 16 di 0.2087307 0.2087307 #> 17 mi 0.8284840 0.8284840 #> 18 do 0.9706503 0.9706503 #> 19 fr 0.6544848 0.6544848 #> 20 sa NA 0.6544848 #> 21 so NA 0.6544848 #> 22 mo 0.3191548 0.3191548 #> 23 di 0.2087307 0.2087307 #> 24 mi 0.8284840 0.8284840 #> 25 do 0.9706503 0.9706503 #> 26 fr 0.6544848 0.6544848 #> 27 sa NA 0.6544848 #> 28 so NA 0.6544848