[R] interpolating a lot of data at once

Gabor Grothendieck ggrothendieck at gmail.com
Wed May 31 19:26:59 CEST 2006


On 5/31/06, David Hugh-Jones <davidhughjones at gmail.com> wrote:
> I have a big dataset containing a lot of values for 1970, 1980 and
> 1990. I want to interpolate values for the years in between, and also
> if possible to extrapolate to 1968 and 1969. The method doesn't have
> to be clever but I am looking for a function that will do all the data
> at once. (Doing foreach, or apply, is just too slow!) Is there
> something that will take
>
> list(df$val.1970, df$val.1980, df$val.1990)
>
> as inputs and output an interpolated matrix?
>

Here are a variety of approaches:

# 1. using lm

y.lm <- lm(y ~ tt, list(y = 1:3, tt = seq(1970, 1990, 10)))
predict(y.lm, list(tt = 1968:1990))

# 2. using "ts" objects:and "dyn" package

library(dyn)
w <- ts(1:3, start = 1970, delta = 10)
w.lm <- dyn$lm(w ~ tt, list(tt = time(w)))
predict(w.lm, list(tt = 1968:1990))

# if only interpolation needed using na.approx.zoo

library(zoo)
z <- zoo(1:3, c(1970, 1980, 1990))
na.approx(merge(as.zoo(z), zoo(, 1970:1990))) # ignore warning



More information about the R-help mailing list