[R] interpolating a lot of data at once

2006-05-31 Thread David Hugh-Jones
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?

Cheers
David

__
R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html


Re: [R] interpolating a lot of data at once

2006-05-31 Thread Gabor Grothendieck
On 5/31/06, David Hugh-Jones [EMAIL PROTECTED] 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

__
R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html