#For example, weekly data come in slides of 4 weeks (1 month)
p <- 4 #number of layers in each slide (data come in a slide)
#We want to analyze the trend clusters within a window of 1 year
w <- 13 #number of slides in each window
s <- w #step to shift a window
#Simulate 26 autoregressive time series with two years of weekly data (52*2 weeks)
N <- 26
T <- 2*p*w
set.seed(123)
phi <- c(0.5) #parameter of autoregression
X <- sapply(1:N, function(x) arima.sim(n=T+100,
list(order=c(length(phi),0,0),ar=phi)))[101:(T+100),]
colnames(X) <- paste("TS", c(1:dim(X)[2]), sep="")
tmp <- CWindowCluster(X, Delta=NULL, Theta=0.8, p=p, w=w, s=s, Epsilon=1)
#Time series were simulated with the same parameters, but based on the clustering parameters,
#not all time series join the same cluster. We can plot the main cluster for each window, and
#time series out of the cluster:
par(mfrow=c(2,2))
ts.plot(X[1:(p*w),tmp[1,]==1], ylim=c(-4,4),
main="Main time series cluster in window 1")
ts.plot(X[1:(p*w),tmp[1,]!=1], ylim=c(-4,4),
main="Time series out of the main cluster in window 1")
ts.plot(X[(p*w+1):(2*p*w),tmp[2,]==1], ylim=c(-4,4),
main="Main time series cluster in window 2")
ts.plot(X[(p*w+1):(2*p*w),tmp[2,]!=1], ylim=c(-4,4),
main="Time series out of the main cluster in window 2")Run the code above in your browser using DataLab