r - Expand a Time Series to a specific number of periods -
i'm new r , attempting take set of time series , run them through conditional inference tree classify shape of time series. problem not of time sereis of same number of periods. trying expand each time series 30 periods long, still maintain same "shape". far 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, = n, =(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 time series , evenly spaces out 30 periods , interpolates missing values. however, length of returned series 42 units , not 30, retains same "shape" orignal time series. know how modify results produced function accordian 30 periods long , time series shape remains relatively unchanged?
i think there's base r solution here. check out approx()
, linear (or constant) interpolation many points n
specify. here think want n = 30
.
test2 <- approx(test, n=30) plot(test2) points(test, pch="*")
this returns list test2
second element y
interpolated values. haven't yet used time series object, seems entirely interior function, correct?
Comments
Post a Comment