| lvcf | R Documentation |
Replace missing values in a covariate with the last non-missing value, separately for each id.
lvcf(id, x, time)
id |
subject identifier |
x |
the covariate which will be modified |
time |
optional; used to sort observations within subject |
If an analysis includes a covariate that was measured repeatedly, cholesterol say, the data may often include visits where this was not measured. It is convenient in that case to use, for each subject, the most recently measured value. When a value is extended beyond the last measurement, rather that just replacement of intermediate values, there is the potential for bias, e.g., if the reason for discontinuation is predictive of the future trajectory of values.
If time is missing, then the order of the values within subject
is assumed to be the calendar time order. It is not necessary for the
the data to be sorted by id.
an updated copy of x
Other R packages also implement an locf function, e.g., na.locf
in the zoo package or LOCF in DescTools.
However, they do not include the id argument, so per subjct application
requires further processing.
The tdc operation in tmerge also performs lvcf substitution.
We have used the phrase "last value" rather than "last observation" since this function only addresses a single variable with each call, not the entire row of a data frame (observation) as some others do.
Terry Therneau
tmerge
newplat <- with(pbcseq, lvcf(id, platelet))
table(is.na(pbcseq$platelet), is.na(newplat))
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.