Question

I'm new to R and I am attempting to take a set of time series and run them through a Conditional Inference Tree to help classify the shape of the time series. The problem is that not all of the time sereis are of the same number of periods. I am trying to expand each time series to be 30 periods long, but still maintain the same "shape". This is as far as I have got

     Require(zoo)

                test<-c(606,518,519,541,624,728,560,512,777,728,1014,1100,930,798,648,589,680,635,607,544,566)
            accordion<-function(A,N){
                x<-ts(scale(A), start=c(1,1), frequency=1)
                X1 <- zoo(x,seq(from = 1, to = N, by =(N-1)/(length(x)-1) ))
                X2<-merge(X1, zoo(order.by=seq(start(X1), end(X1)-1, by=((N-1)/length(x))/(N/length(x)))))
            X3<-na.approx(X2)
            return(X3)}
        expand.test<-accordion(test,30)
plot(expand.test); lines(scale(test))
        length(expand.test)

The above code, scales the time series and then evenly spaces it out to 30 periods and interpolates the missing values. However, the length of the returned series is 42 units and not 30, however it retains the same "shape" as the orignal time series. Does anyone know how to modify this so that the results produced by the function accordian are 30 periods long and the time series shape remains relatively unchanged?

Was it helpful?

Solution

I think there's a base R solution here. Check out approx(), which does linear (or constant) interpolation with as many points n as you specify. Here I think you want n = 30.

test2 <- approx(test, n=30)
plot(test2)
points(test, pch="*")

This returns a list test2 where the second element y is your interpolated values. I haven't yet used your time series object, but it seems that was entirely interior to your function, correct?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top