ftsa: An R Package for Analyzing Functional Time Series

Abstract:

Recent advances in computer recording and storing technology have tremendously increased the presence of functional data, whose graphical representation can be infinite-dimensional curve, image, or shape. When the same functional object is observed over a period of time, such data are known as functional time series. This article makes first attempt to describe several techniques (centered around functional principal component analysis) for modeling and forecasting functional time series from a computational aspect, using a readily-available R addon package. These methods are demonstrated using age-specific Australian fertility rate data from 1921 to 2006, and monthly sea surface temperature data from January 1950 to December 2011.

Cite PDF Tweet

Published

June 2, 2013

Received

Feb 3, 2012

Citation

Shang, 2013

Volume

Pages

5/1

64 - 72


1 Introduction

The aim of this article is to describe the R functions that are readily-available in the ftsa package , for modeling and forecasting functional time series. This article was motivated by recent advances in computer recording and storing technology that have enabled researchers to collect and store (ultra) high-dimensional data. When the high-dimensional data are repeatedly measured on the same object over a period of time, a time series of continuous functions is observed within a common bounded interval .

Analyzing functional time series has received increasing attention in the functional data analysis literature . presented a rainbow plot for visualizing functional time series, where the distant past data are shown in red and most recent data are shown in purple. proposed functional principal component regression (FPCR) to model and forecast functional time series.

Before reviewing the FPCR, we first define the problem more precisely. Let yt(x) denote a function, such as age-specific fertility rates for the continuous age variable x in year t, or monthly sea surface temperatures for the continuous time variable x in year t. In the latter example, functional time series techniques allow us to capture the underlying dynamic of the multiple seasonality in the data . We assume that there is an underlying smooth function ft(x) that observes with error at discretized grid points of x. In practice, we observe {xi,yt(xi)} for t=1,2,,n and i=1,2,,p, from which we extract a smooth function ft(x), given by

(1)yt(xi)=ft(xi)+σt(xi)εt,i,

where εt,i is an independent and identically distributed (iid) standard normal random variable, σt(xi) allows the amount of noise to vary with xi, and {x1,x2,,xp} is a set of discrete data points. Given a set of functional data f(x)=[f1(x),f2(x),,fn(x)], we are interested in finding underlying patterns using the FPCR, from which we obtain forecasts of yn+h(x), where h denotes the forecast horizon.

This article proceeds as follows. Techniques for modeling and forecasting functional time series are reviewed and their implementations using the ftsa package are described. Conclusions are given at the end.

2 Functional time series modeling and forecasting techniques

Functional principal component regression

The theoretical, methodological and practical aspects of functional principal component analysis (FPCA) have been extensively studied in the functional data analysis literature, since it allows finite dimensional analysis of a problem that is intrinsically infinite-dimensional . Numerous examples of using FPCA as an estimation tool in regression problem can be found in different fields of applications, such as breast cancer mortality rate modeling and forecasting , call volume forecasting , climate forecasting , demographical modeling and forecasting , and electricity demand forecasting .

At a population level, a stochastic process denoted by f can be decomposed into the mean function and the sum of the products of orthogonal functional principal components and uncorrelated principal component scores. It can be expressed as f=μ+k=1βkϕk, where μ is the unobservable population mean function, βk is the kth principal component scores, and ϕk is the kth population functional principal component.

In practice, we can only observe n realizations of f evaluated on a compact interval x[0,τ], denoted by ft(x) for t=1,2,,n. At a sample level, the functional principal component decomposition can be written as

(2)ft(x)=f¯(x)+k=1Kβ^t,kϕ^k(x)+ε^t(x),

where f¯(x)=1nt=1nft(x) is the estimated mean function, ϕ^k(x) is the kth estimated orthonormal eigenfunction of the empirical covariance operator Γ^(x)=1nt=1n[ft(x)f¯(x)][ft(x)f¯(x)], the coefficient β^t,k is the kth principal component score for year t, it is given by the projection of ft(x)f¯(x) in the direction of kth eigenfunction ϕ^k(x), that is, β^t,k=<ft(x)f¯(x),ϕ^k(x)>=x[ft(x)f¯(x)]ϕ^k(x)dx, ε^t(x) is the residual, and K is the optimal number of components, which can be chosen by cross validation. studied the impact on forecast accuracy with a smaller or larger than the optimal value of K.

The functional principal component decomposition is first demonstrated using the age-specific Australian fertility rate data between ages 15 and 49 observed from 1921 to 2006. This data set was obtained from the Australian Bureau of Statistics (Cat No, 3105.0.65.001, Table 38). A functional graphical display is given in .

Figure 1 presents the first two functional principal components and their associated principal component scores. The bottom panel of Figure 1 also plots the forecasted principal component scores, and their 80% prediction intervals (in yellow color), using an exponential smoothing state-space model . As pointed out by a referee, the forecasts of principal component scores appear to quickly level off, and the prediction intervals widen very quickly. This reflects the difficulty of our model in forecasting medium or long term horizon, as a result of the increase in variability.

graphic without alt text
Figure 1: The first two functional principal components and their associated principal component scores for the Australian fertility rate data from 1921 to 2006.

Figure 1 was produced by the following code.

# load the package used throughout this article
library("ftsa")
# Fit and plot functional principal components  
# order specifies the number of principal components
# h specifies the forecast horizon
plot(forecast(ftsm(Australiasmoothfertility, order=2), h=20), "components")

By conditioning on the set of smoothed functions f(x)=[f1(x),f2(x),,fn(x)] and the fixed functional principal components B=[ϕ^1(x),ϕ^2(x),,ϕ^K(x)], the h-step-ahead forecasts of yn+h(x) can be obtained as

y^n+h|n(x)=E[yn+h(x)|f(x),B]=f¯(x)+k=1Kβ^n+h|n,kϕ^k(x),

where β^n+h|n,k denotes the h-step-ahead forecasts of βn+h,k using a univariate time series.

Figure 2 shows the forecasts of Australian fertility rate data from 2007 to 2026 highlighted in rainbow color, while the data used for estimation are grayed out. Both the multi-step-ahead and iterative one-step-ahead forecasts exhibit a continuing shift to older ages of peak fertility rates, caused by a recent tendency to postpone child-bearing while pursuing careers.

graphic without alt text (a) Multiple-step-ahead forecasts. Based on the historical data from 1921 to 2006, we obtain 20-step-ahead forecasts for 2007 to 2026. graphic without alt text (b) Iterative one-step-ahead forecasts. Based on the historical data from 1921 to 2006, we obtain iterative one-step-ahead forecasts for 2007 to 2026 using the rolling origin approach.

Figure 2: Forecasts of the Australian fertility rates from 2007 to 2026, based on the first two functional principal components and their associated principal component scores as an illustration.

Figure 2 was produced by the following code.

# Plot the historical data in gray
plot(Australiasmoothfertility, col = gray(0.8), xlab = "Age", 
     ylab = "Count of live birth (per 1,000 females)", 
     main = "Forecasted fertility rates (2007-2026)")
# Plot the forecasts in rainbow color for Fig. 4(a)
plot(forecast(ftsm(Australiasmoothfertility, order = 2), h = 20), add = TRUE)
legend("topright", c("2007", "2026"), col = c("red", "blue"), lty = 1)

plot(Australiasmoothfertility, col = gray(0.8), xlab = "Age",
     ylab = "Count of live birth (per 1,000 females)", 
     main = "Forecasted fertility rates (2007-2026)")
# Plot the forecasts in rainbow color for Fig. 4(b)
plot(ftsmiterativeforecasts(Australiasmoothfertility, components = 2, iteration = 20), 
     add = TRUE)
legend("topright", c("2007", "2026"), col = c("red", "blue"), lty = 1)

To construct prediction interval, we calculate the forecast variance that follows from (1) and (2). Because of orthogonality, the forecast variance can be approximated by the sum of component variances ξn+h(x)=Var[yn+h(x)|f(x),B]=σ^μ2(x)+k=1Kun+h,kϕ^k2(x)+v(x)+σn+h2(x), where un+h,k=Var(βn+h,k|β1,k,β2,k,,βn,k) can be obtained from the time series model, and the model error variance v(x) is estimated by averaging {ε^12(x),ε^22(x),,ε^n2(x)} for each x, and σ^μ2(x) and σn+h2(x) can be obtained from the nonparametric smoothing method used.

Based on the normality assumption, the 100(1α)% prediction interval for yn+h(x) is constructed as y^n+h|n(x)±zαξn+h(x), where zα is the (1α/2) standard normal quantile.

graphic without alt text
Figure 3: Forecasts of fertility rates in 2007, along with the 80% prediction interval.

Figure 3 displays the forecasts of fertility rates in 2007, along with the 80% prediction interval. It was created by the following code.

# Plot the point forecast
aus = forecast(ftsm(Australiasmoothfertility, order = 2), h = 1)
plot(aus, ylim = c(0, 140))
# Plot the lower and upper bounds
lines(aus$lower, col = 2); lines(aus$upper, col = 2)
# Add a legend to the plot
legend("topright", c("Point forecasts", "Interval forecasts"), col = c(1, 2), lty = 1, 
       cex = 0.9)

For this Australian fertility rate data, the point and interval forecast accuracy obtained by the FPCR have already been studied by .

Updating point and interval forecasts

A special case of functional time series is when the continuous variable is also a time variable, such as the monthly sea surface temperature data from 1950 to 2011, obtained from National Oceanic and Atmospheric Administration (http://www.cpc.noaa.gov/ data/indices/sstoi.indices). A similar type of functional graphical display is given in . Such data originate from a univariate seasonal time series. Let {Zw,w[1,N]} be a seasonal time series which has been observed at N equispaced times. We divide the observed time series into n trajectories, and then consider each trajectory of length p as a curve rather than p distinct data points. The functional time series is given by yt(x)={Zw,w(p(t1),pt]},t=1,2,,n. The problem of interest is to forecast yn+h(x), where h denotes forecast horizon. In the sea surface temperature data, we consider {Zw} to be monthly sea surface temperatures from 1950 to 2011, so that p=12 and N=62×12=744, and we are interested in forecasting sea surface temperatures in 2012 and beyond.

When N=np, all trajectories are complete, and forecasts can be obtained by the FPCR. However, when Nnp, we revisited the block moving (BM) and penalized least squares (PLS) proposed by to update point and interval forecasts, when the most recent curve is partially observed.

When functional time series are segments of a univariate time series, the most recent trajectory is observed sequentially . When we observe the first m0 time period of yn+1(xl), denoted by yn+1(xe)=[yn+1(x1),yn+1(x2),,yn+1(xm0)], we are interested in forecasting the data in the remaining time period, denoted by yn+1(xl) for m0<lp. By using the FPCR, the partially observed data in the most recent curve are not incorporated into the forecasts of yn+1(xl). Indeed, the point forecasts obtained from the FPCR can be expressed as y^n+1|n(xl)=E[yn+1(xl)|f(xl),Bl]=f¯(xl)+k=1Kβ^n+1|n,kϕ^k(xl), for m0<lp, where f(xl) denotes the historical data corresponding to the remaining time periods; f¯(xl) is the mean function corresponding to the remaining time periods; and Bl={ϕ^1(xl),ϕ^2(xl),, ϕ^K(xl)} is a set of the estimated functional principal components corresponding to the remaining time periods.

In order to improve point forecast accuracy, it is desirable to dynamically update the point and interval forecasts for the rest of year n+1 by incorporating the partially observed data. In what follows, I shall revisit two methods for updating point and interval forecasts.

Block moving (BM)

The BM method re-defines the start and end points of trajectories. Because time is a continuous variable, we can change the function support from [1,p] to [m0+1,p][1,m0]. The re-defined functional time series forms a complete block, at the cost of losing some observations in the first year. With the complete data block, the FPCR can then be applied to update the point and interval forecasts.

The re-defined data are shown diagrammatically in Figure 4, where the bottom box has moved to become the top box. The cyan colored region shows the data loss in the first year. The partially observed last trajectory under the old “year" completes the last trajectory under the new year.

graphic without alt text
Figure 4: Dynamic update via the BM approach. The colored region shows the data loss in the first year. The forecasts for the remaining months in year n+1 can be updated by the forecasts using the TS method applied to the upper block.

As an illustration, suppose we observe the monthly sea surface temperature data from January 1950 to May 2011, we aim to update the point and interval forecasts from June 2011 to December 2011. Figure 5 displays the point and interval forecasts for the remaining months of 2011, by using the BM method.

graphic without alt text
Figure 5: Prediction interval of the sea surface temperature data between June 2011 and December 2011. By incorporating the sea surface temperature data between January 2011 and May 2011, the 80% prediction interval can be updated using the BM method.

Figure 5 was created by the following code

# Name history to represent historical data,
history <- ElNino2011smooth
# Name obs to represent partially observed data,
obs <- ElNino2011smooth$y[1:5,62]
# Name fore to represent the forecasting period
fore <- ElNino2011smooth$y[6:12,62]
int <- dynupdate(data = history, newdata = obs, holdoutdata = fore, 
  method = "block", interval = TRUE, level = 80)
bmupdate <- dynupdate(data = history, newdata = obs, holdoutdata = fore, 
  method = "block", value = TRUE)
plot(6:12, fore, type = "l", ylim = c(19, 26), xlab = "Month", 
  ylab = "Sea surface temperature")
lines(6:12, bmupdate, col = 4)
lines(6:12, int$low$y, col = 2); lines(6:12, int$up$y, col = 2)
legend("topright", c("True observations", "Point forecasts", "Interval forecasts"),
   col=c(1, 4, 2), lty = 1, cex = 0.8)

Penalized least squares (PLS)

We can also update the remaining part of the trajectory by using regression-based approaches. Let Fe be m0×K matrix, whose (j,k)th entry is ϕ^k(xj) for 1jm0. Let βn+1=[βn+1,1,βn+1,2,,βn+1,K], f¯(xe)=[f¯(x1),f¯(x2),,f¯(xm0)], and ϵn+1(xe)=[ϵn+1(x1),ϵn+1(x2),,ϵn+1(xm0)]. As the mean-adjusted y^n+1(xe)=yn+1(xe)f¯(xe) become available, a regression can be expressed as y^n+1(xe)=Feβn+1+ϵn+1(xe). The βn+1 can be estimated by ordinary least squares, assuming (FeFe) is invertible, β^n+1OLS=(FeFe)1Fey^n+1(xe). However, if (FeFe) is not invertible, then a regularized approach can be implemented, such as the ridge regression (RR) and penalized least squares (PLS). The regression coefficients of the RR and PLS are

(3)β^n+1RR=(FeFe+λIK)1Fey^n+1(xe),β^n+1PLS=(FeFe+λIK)1(Fey^n+1(xe)+λβ^n+1|n),

where β^n+1RR0 as λ, and β^n+1RRβ^n+1OLS as λ0. In contrast, the β^n+1PLSβ^n+1|n as λ, and β^n+1PLSβ^n+1OLS as λ0.

The point forecasts of yn+1(xl) obtained by the RR and PLS methods are given by y^n+1RR(xl)=f¯(xl)+k=1Kβ^n+1,kRRϕ^k(xl),y^n+1PLS(xl)=f¯(xl)+k=1Kβ^n+1,kPLSϕ^k(xl).

Among these regression-based approaches, the PLS method can also update the interval forecasts. Let the one-step-ahead forecast errors of the principal component scores be given by ξ^j,k=β^nj+1,kβ^nj+1|nj,k,forj=1,2,,nK. {ξ^1,k,ξ^2,k,,ξ^nK,k} can then be sampled with replacement to give a bootstrap sample of βn+1|n,k: β^n+1|n,kb=β^n+1|n,k+ξ^,kb,forb=1,2,,B, where ξ^,kb denotes the bootstrap samples, and B is the number of bootstrap replications. Based on (3), the bootstrapped β^n+1|nb leads to the bootstrapped β^n+1b,PLS, we obtain B replications of y^n+1b,PLS(xl)=f¯(xl)+k=1Kβ^n+1,kb,PLSϕ^k(xl)+ϵ^n+1b(xl), where ϵ^n+1b(xl) is obtained by sampling with replacement from {ϵ^1(xl),ϵ^2(xl),,ϵ^n(xl)}. Hence, the 100(1α)% prediction interval for the updated forecasts are defined as α/2 and (1α/2) quantiles of y^n+1b,PLS(xl).

graphic without alt text
Figure 6: Prediction interval of the sea surface temperature data between June 2011 and December 2011. By incorporating the sea surface temperature data between January 2011 and May 2011, the 80% prediction interval can be updated using the PLS method.

Figure 6 displays the point and interval forecasts for the remaining time period of year 2011, by using the PLS method. It was created by the following code

history <- ElNino2011smooth
obs <- ElNino2011smooth$y[1:5, 62]
fore <- ElNino2011smooth$y[6:12, 62]
# Implement the ridge and PLS regressions,
# The tuning parameter lambda=100 as an
# illustration
rrmethod <- dynupdate(history, newdata = obs, holdoutdata = fore, method = "ridge",
  value = TRUE, lambda = 100, level = 80)
plsmethod <- dynupdate(history, newdata = obs, holdoutdata = fore, method = "pls",
  value = TRUE, lambda = 100, level = 80)
plsmethodint <- dynupdate(history, newdata = obs, holdoutdata = fore, method = "pls",
  interval = TRUE, lambda = 100, level = 80)
# Plot the true observations for forecasting period
plot(6:12, fore, type = "l", ylim = c(19, 26), xlab = "Month", 
  ylab = "Sea surface temperature")
# Plot point forecasts obtained by RR and PLS
lines(6:12, plsmethod, col = 4); lines(6:12, rrmethod, col = "purple")
# Plot interval forecasts obtained by PLS
lines(6:12, plsmethodint$low$y, col = 2); lines(6:12, plsmethodint$up$y, col = 2)
legend("topright",c("True observations", "PLS point forecasts", "RR point forecasts",
  "PLS interval forecasts"), col = c(1, 4, "purple", 2), lty = 1, cex = 0.8)

For this sea surface temperature data set, the point and interval forecast accuracy obtained by the FPCR, BM and PLS methods have already been studied by .

3 Conclusion

This article described several techniques in the ftsa package, for modeling and forecasting functional time series. These methods centered on the FPCR, which is a common dimension reduction technique in the functional data analysis literature. FPCR reduces intrinsically infinite number of variables to several orthogonal regressors, which captures the main mode of variation in data. As illustrated by the Australian fertility rate data, FPCR is able to model and forecast annual Australian fertility rates through either multi-step-ahead forecasts or iterative one-step-ahead forecasts using the rolling origin approach. When the continuous variable in a functional time series is also a time variable, a new observation arrives sequentially. As shown using the monthly sea surface temperature data, the BM and PLS methods can update the point and interval forecasts based on the FPCR.

To sum up, the methods reviewed in this article focus on extracting patterns from a set of functional time series, and should be considered when the interest lies in modeling and forecasting the future realizations of a stochastic process.

4 Acknowledgements

I would like to thank the editors and two reviewers for constructive comments and suggestions that have significantly improved this article. Thanks also go to Professor Rob Hyndman for helping with R code.


CRAN packages used

ftsa

CRAN Task Views implied by cited packages

FunctionalData, TimeSeries

Note

This article is converted from a Legacy LaTeX article using the texor package. The pdf version is the official version. To report a problem with the html, refer to CONTRIBUTE on the R Journal homepage.

Footnotes

    References

    A. Aguilera, F. Ocana, and M. Valderrama. Forecasting time series by functional PCA. Discussion of several weighted approaches. Computational Statistics 14 (3):, 1999.
    J. Antoch, L. Prchal, M. R. De Rosa, and P. Sarda. Functional linear regression with functional response: application to prediction of electricity consumption. In S. Dabo-Niang; F. Ferraty editors Functional andOperatorial Statistics pages Physica-Verlag Heidelberg, 2008.
    B. Erbas, R. J. Hyndman, and D. M. Gertig. Forecasting age-specific breast cancer mortality using functional data models. Statistics in Medicine 26 (2):, 2007.
    P. Hall and M. Hosseini-Nasab. On properties of functional principal components analysis. Journal of the Royal Statistical Society: Series B,68 (1):, 2006.
    S. Hörmann and P. Kokoszka. Weakly dependent functional data. The Annals of Statistics 38 (3):, 2010.
    L. Horváth and P. Kokoszka. Inference for Functional Data with Applications. Springer New York, 2012.
    L. Horváth, M. Hušková, and P. Kokoszka. Testing the stability of the functional autoregressive process. Journal of Multivariate Analysis 101 (2):, 2010.
    R. J. Hyndman, A. B. Koehler, J. K. Ord, and R. D. Snyder. Forecasting with Exponential Smoothing: the State Space Approach. Springer Berlin, 2008.
    R. J. Hyndman and H. Booth. Stochastic population forecasts using functional data models for mortality, fertility and migration. International Journal of Forecasting 24(3):, 2008.
    R. Hyndman and H. L. Shang. ftsa: Functional time series analysis, . R package version 3.8, 2013. URL http://CRAN.R-project.org/package=ftsa.
    R. J. Hyndman and H. L. Shang. Forecasting functional time series (with discussion). Journal of the Korean Statistical Society 38(3):, 2009.
    R. J. Hyndman and H. L. Shang. Rainbow plots, bagplots, and boxplots for functional data. Journal of Computational; Graphical Statistics 19(1):, 2010.
    H. L. Shang and R. J. Hyndman. Nonparametric time series forecasting with dynamic updating. Mathematics; Computers in Simulation 81(7):, 2011.
    H. L. Shang. Functional time series approach for forecasting very short-term electricity demand. Journal of Applied Statistics 40 (1):, 2013.
    H. L. Shang. Point and interval forecasts of age-specific fertility rates: a comparison of functional principal component methods. Journal of Population Research 29 (3):, 2012.
    H. L. Shang. rainbow: an R package for visualizing functional time series. The R Journal 3 (2):, 2011.
    H. Shen and J. Z. Huang. Interday forecasting and intraday updating of call center arrivals. Manufacturing & Service Operations Management 10(3):, 2008.

    Reuse

    Text and figures are licensed under Creative Commons Attribution CC BY 4.0. The figures that have been reused from other sources don't fall under this license and can be recognized by a note in their caption: "Figure from ...".

    Citation

    For attribution, please cite this work as

    Shang, "ftsa: An R Package for Analyzing Functional Time Series", The R Journal, 2013

    BibTeX citation

    @article{RJ-2013-006,
      author = {Shang, Han Lin},
      title = {ftsa: An R Package for Analyzing Functional Time Series},
      journal = {The R Journal},
      year = {2013},
      note = {https://rjournal.github.io/},
      volume = {5},
      issue = {1},
      issn = {2073-4859},
      pages = {64-72}
    }