1
2hegy.regressors <- function(x)
3{
4  n <- length(x)
5  S <- frequency(x)
6  isSeven <- (S %% 2) == 0
7  isSevenp2 <- 2 + isSeven
8
9  ML <- x
10  for (i in seq_len(S-1))
11    ML <- cbind(ML, lag(x, -i))
12  ML <- window(ML, end = end(x))
13  ML <- sapply(seq.int(0, S-1), function(x, y, n) c(rep(NA,length.out=x), y[seq_len(n-x)]), y=x, n=n)
14
15  ypi <- matrix(nrow = n, ncol = S)
16  ypi[,1] <- rowSums(ML)
17  if (isSeven)
18    ypi[,2] <- ML %*% rep(c(-1, 1), len = S)
19  seqS <- seq_len(S)
20  j <- 0
21  sinesign <- -1
22  id <- seq.int(isSevenp2, S, 2)
23  ref <- ceiling(S) / 4
24  for (i in id)
25  {
26    j <- j + 1
27    seqw <- seqS * (2 * pi * j / S)
28    ypi[,i] <- ML %*% cos(seqw)
29    ypi[,i+1] <- sinesign * ML %*% sin(seqw)
30    if (j == ref)
31      sinesign <- -1 * sinesign
32  }
33
34  ypi <- rbind(NA, ypi[-n,])
35  colnames(ypi) <- paste("Ypi", seq_len(S), sep="")
36  ypi[-seq_len(S),]
37}
38