Steve Powers
2007-Oct-01 08:03 UTC
[R] merging, interpolating two simulataneous time series
I have a time series for two variables measured simultaneously, but at different intervals. The variables are not independent, so the patterns in the times series are very similar (one variable goes up when the other goes up, etc). For example, let's pretend my data look something like this (but with more noise)... var1_times<-c(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10) var1_values<-c(0, 0.5, 1.0, 1.5, 2.0, 2.5, 2.0, 1.5, 1.0, 0.5, 0) var2_times<-c(0, 2, 5.0, 7, 10.0) var2_values<-c(50, 55, 60, 55, 50) Note that var2 is always sampled at a time when a corresponding var1 measurement was taken, but some var1 measurements were taken alone. I'm trying to increase the frequency of the var2 time series in a model by taking into account the parallel information in the var1 time series. Essentially, this means I want to do a local regression of var2_values~var1_values by fitting a different relationship for each time interval. I've used approx and approxfun in the past and found them helpful, but can't figure out how to employ them in this case. Any thoughts on how to do this? In the end, I want interpolated values for var2 that line up with the var1 times.---steve I'm using R version 2.4 in Windows XP.
jim holtman
2007-Oct-01 11:53 UTC
[R] merging, interpolating two simulataneous time series
Is this what you want?> var1_times<-c(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10) > var1_values<-c(0, 0.5, 1.0, 1.5, 2.0, 2.5, 2.0, 1.5, 1.0, 0.5, 0) > > var2_times<-c(0, 2, 5.0, 7, 10.0) > var2_values<-c(50, 55, 60, 55, 50) > > new_v2 <- approxfun(var2_times, var2_values, rule=2) > > combined <- data.frame(time=var1_times, v1=var1_values, v2=new_v2(var1_times)) > > combinedtime v1 v2 1 0 0.0 50.00000 2 1 0.5 52.50000 3 2 1.0 55.00000 4 3 1.5 56.66667 5 4 2.0 58.33333 6 5 2.5 60.00000 7 6 2.0 57.50000 8 7 1.5 55.00000 9 8 1.0 53.33333 10 9 0.5 51.66667 11 10 0.0 50.00000 On 10/1/07, Steve Powers <smpowers at wisc.edu> wrote:> I have a time series for two variables measured simultaneously, but at > different intervals. The variables are not independent, so the patterns > in the times series are very similar (one variable goes up when the > other goes up, etc). > > For example, let's pretend my data look something like this (but with > more noise)... > > var1_times<-c(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10) > var1_values<-c(0, 0.5, 1.0, 1.5, 2.0, 2.5, 2.0, 1.5, 1.0, 0.5, 0) > > var2_times<-c(0, 2, 5.0, 7, 10.0) > var2_values<-c(50, 55, 60, 55, 50) > > > Note that var2 is always sampled at a time when a corresponding var1 > measurement was taken, but some var1 measurements were taken alone. I'm > trying to increase the frequency of the var2 time series in a model by > taking into account the parallel information in the var1 time series. > Essentially, this means I want to do a local regression of > var2_values~var1_values by fitting a different relationship for each > time interval. I've used approx and approxfun in the past and found them > helpful, but can't figure out how to employ them in this case. Any > thoughts on how to do this? In the end, I want interpolated values for > var2 that line up with the var1 times.---steve > > > I'm using R version 2.4 in Windows XP. > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >-- Jim Holtman Cincinnati, OH +1 513 646 9390 What is the problem you are trying to solve?