[R] merging, interpolating two simulataneous time series

Steve Powers smpowers at wisc.edu
Tue Oct 2 04:21:03 CEST 2007


No, it's a little more complex. I do not want to interpolate var2 as a 
function of time. I want interpolate var2 as a function of var1, but in 
an iterative chronological sequence that does require matching times 
first. So the two var1 values for the interpolation are chosen such that 
var1 times = var2 times. But every consecutive pair in the var2 time 
series is fitted separately to the corresponding, simultaneously 
measured var1 pair. By chance, on quick glance it looks like the 
suggested method yields the desired outputs for these example data, but 
it doesn't actually work for my real data set.

The idea here is that I have a very high frequency, smoothly changing 
time series for var 1, but var 2 is the variable of interest and it was 
measured at a far lower frequency. Fortunately, there is a strongly 
linear var2~var1 relationship (changes in var2 reflect change in var1), 
_but the relationship shifts somewhat over time_, so in order to 
accurately predict var2 from var1, we need a slightly different function 
for different portions of the timeseries. Using the method suggested 
earlier, the interpolated var2 time series plot is "chunky" even though 
it should be smooth like the var1 time series. That's how I know it 
didn't work.

What I'm thinking of is sort of like quantile regression between var1 
and var2, but at each iteration we are regressing at the smallest 
possible grain (between two data points). Maybe use approxfun in a for 
loop somehow for each consecutive pair in the var2 time series? Arrgh! 
I'm a reasonably good R coder but I've spent hours on this one and it's 
REALLY driving me completely nuts.---steve



jim holtman wrote:
> 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.0, 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))
>>
>> combined
>>     
>    time  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.
>>
>>     
>
>
>



More information about the R-help mailing list