[R] how to interpolate time series data with missingness

Matthew Keller mckellercran at gmail.com
Wed Jun 17 23:54:39 CEST 2009


Hi all,

I have a vector, most of which is missing. The data is always
increasing, but may do so in jumps. I would like to interpolate the
NAs with 'best guesses', using something like filter(), which doesn't
work due to the NAs. Here is an example:

> x <- c(2,3,NA,NA,NA,3.2,3.5,NA,NA,6,NA)
> x
 [1] 2.0 3.0  NA  NA  NA 3.2 3.5  NA  NA 6.0  NA

I would like a function that would take the NAs and fill in the
average values around the NAs. E.g., make a new vector x.new that
looks like:
> x.new
[1] 2.0 3.0 3.1 3.1 3.1 3.2 3.5 4.75 4.75 6 6

Or, alternatively, that could figure out a more likely value than just
the average. There must be something simple I'm overlooking, like some
kind of loess y-hat or something? Any help would be appreciated,

Matt

-- 
Matthew C Keller
Asst. Professor of Psychology
University of Colorado at Boulder
www.matthewckeller.com




More information about the R-help mailing list