[R] Help with interpolation of time series
ggrothendieck at gmail.com
Thu Jun 17 16:16:10 CEST 2010
On Thu, Jun 17, 2010 at 6:26 AM, Nicholas R Frazier <nrflaw at gmail.com> wrote:
> I'm quite new to R. I have a time series of annual state population
> estimates from census.gov, and I'd like to get a time series of monthly
> estimates, by a nonlinear interpolation.
Please provide some test data in reproducible form next time. See
We can use na.spline in the zoo package. First create some test data, z.
The time index of our test data is 2000, 2001, 2002 representing three years.
Use na.spline with an xout vector spaced 1/12 apart. This will give a time
index of 2000 (representing Jan), 2000 + 1/12 (representing Feb), etc.
An alternative would be to use the yearmon class. That can be done
using aggregate and then use na.spline as before.
Finally lets plot the data plotting the annual data in red and the
monntly in black. screen = 1 says plot them on the same panel (rather
than using two separate panels).
# test data
N <- 3
z <- zoo(seq(N)^2, seq(2000, length = N)) # annual data
# 1. disaggregate to months using splines
z.mo <- na.spline(z, xout = seq(start(z), end(z), 1/12))
# 2. same but using "yearmon" class
z.ym <- aggregate(z, as.yearmon)
z.ym.m <- na.spline(z, xout = seq(start(z.ym), end(z.ym), 1/12))
plot(cbind(z.ym.m, z.ym), type = "o", col = c("black", "red"), screen = 1)
More information about the R-help