《正文》
R
软件中的
时间序列分析
程序包
Base R ships with a lot of functionality useful for time series, in particular in the stats package. This is complemented by many packages on CRAN, which are briefly summarized below.
There is also a considerable overlap between the tools for time series and those in the
Econometrics
and
Finance
task views. The packages in this view can be roughly structured into the following topics. If you think that some package is missing from the list, please let us know.
Basics
·
Infrastructure
: Base R contains substantial infrastructure for representing and analyzing time series data. The fundamental class is
"ts"
that can represent regularly spaced time series (using numeric time stamps). Hence, it is particularly well-suited for annual, monthly, quarterly data, etc.
·
Rolling statistics
: Moving averages are computed by
ma
from
forecast
, and
rollmean
from
zoo
. The latter also provides a general function
rollapply
, along with other specific rolling statistics functions.
roll
provides parallel functions for computing rolling statistics.
·
Graphics
: Time series plots are obtained with
plot()
applied to
ts
objects. (Partial) autocorrelation functions plots are implemented in
acf()
and
pacf()
. Alternative versions are provided by
Acf()
and
Pacf()
in
forecast
, along with a combination display using
tsdisplay()
.
SDD
provides more general serial dependence diagrams, while
dCovTS
computes and plots the distance covariance and correlation functions of time series. Seasonal displays are obtained using
monthplot()
in stats and
seasonplot
in
forecast
.
Wats
implements wrap-around time series graphics.
ggseas
provides ggplot2 graphics for seasonally adjusted series and rolling statistics.
dygraphs
provides an interface to the Dygraphs interactive time series charting library.
ZRA
plots forecast objects from the
forecast
package using dygraphs. Basic fan plots of forecast distributions are provided by
forecast
and
vars
. More flexible fan plots of any sequential distributions are implemented in
fanplot
.
Times and Dates
·
Class
"ts"
can only deal with numeric time stamps, but many more classes are available for storing time/date information and computing with it. For an overview see
R Help Desk: Date and Time Classes in R
by Gabor Grothendieck and Thomas Petzoldt in
R News 4(1)
, 29-32.
·
Classes
"yearmon"
and
"yearqtr"
from
zoo
allow for more convenient computation with monthly and quarterly observations, respectively.
·
Class
"Date"
from the base package is the basic class for dealing with dates in daily data. The dates are internally stored as the number of days since 1970-01-01.
·
The
chron
package provides classes for
dates()
,
hours()
and date/time (intra-day) in
chron()
. There is no support for time zones and daylight savings time. Internally,
"chron"
objects are (fractional) days since 1970-01-01.
·
Classes
"POSIXct"
and
"POSIXlt"
implement the POSIX standard for date/time (intra-day) information and also support time zones and daylight savings time. However, the time zone computations require some care and might be system-dependent. Internally,
"POSIXct"
objects are the number of seconds since 1970-01-01 00:00:00 GMT. Package
lubridate
provides functions that facilitate certain POSIX-based computations. The
timetk
package contains a collection of tools for working with time series including coercing between time-based tibbles, xts, zoo and ts objects.
wktmo
converts weekly data to monthly data in several different ways.
·
Class
"timeDate"
is provided in the
timeDate
package (previously: fCalendar). It is aimed at financial time/date information and deals with time zones and daylight savings times via a new concept of "financial centers". Internally, it stores all information in
"POSIXct"
and does all computations in GMT only. Calendar functionality, e.g., including information about weekends and holidays for various stock exchanges, is also included.
·
The
tis
package provides the
"ti"
class for time/date information.
·
The
"mondate"
class from the
mondate
package facilitates computing with dates in terms of months.
·
The
tempdisagg
package includes methods for temporal disaggregation and interpolation of a low frequency time series to a higher frequency series.
·
Time series disaggregation is also provided by
tsdisagg2
.
·
TimeProjection
extracts useful time components of a date object, such as day of week, weekend, holiday, day of month, etc, and put it in a data frame.
Time Series Classes
·
As mentioned above,
"ts"
is the basic class for regularly spaced time series using numeric time stamps.
·
The
zoo
package provides infrastructure for regularly and irregularly spaced time series using arbitrary classes for the time stamps (i.e., allowing all classes from the previous section). It is designed to be as consistent as possible with
"ts"
. Coercion from and to
"zoo"
is available for all other classes mentioned in this section.
·
The package
xts
is based on
zoo
and provides uniform handling of R's different time-based data classes.
·
Various packages implement irregular time series based on
"POSIXct"
time stamps, intended especially for financial applications. These include
"irts"
from
tseries
, and
"fts"
from
fts
.
·
The class
"timeSeries"
in
timeSeries
(previously: fSeries) implements time series with
"timeDate"
time stamps.
·
The class
"tis"
in
tis
implements time series with
"ti"
time stamps.
·
The package
tframe
contains infrastructure for setting time frames in different formats.
Forecasting and Univariate Modeling
·
The
forecast
package provides a class and methods for univariate time series forecasts, and provides many functions implementing different forecasting models including all those in the stats package.
·
Exponential smoothing
:
HoltWinters()
in stats provides some basic models with partial optimization,
ets()
from the
forecast
package provides a larger set of models and facilities with full optimization.
robets
provides a robust alternative to the
ets()
function.
smooth
implements some generalizations of exponential smoothing. The
MAPA
package combines exponential smoothing models at different levels of temporal aggregation to improve forecast accuracy.
·
prophet
forecasts time series based on an additive model where non-linear trends are fit with yearly and weekly seasonality, plus holidays. It works best with daily data.
·
The theta method is implemented in the
thetaf
function from the
forecast
package. An alternative and extended implementation is provided in
forecTheta
.
·
Autoregressive models
:
ar()
in stats (with model selection) and
FitAR
for subset AR models.
·
ARIMA models
:
arima()
in stats is the basic function for ARIMA, SARIMA, ARIMAX, and subset ARIMA models. It is enhanced in the
forecast
package via the function
Arima()
along with
auto.arima()
for automatic order selection.
arma()
in the
tseries
package provides different algorithms for ARMA and subset ARMA models.
FitARMA
implements a fast MLE algorithm for ARMA models. Package
gsarima
contains functionality for Generalized SARIMA time series simulation. Robust ARIMA modeling is provided in the
robustarima
package. The
mar1s
package handles multiplicative AR(1) with seasonal processes.
TSTutorial
provides an interactive tutorial for Box-Jenkins modelling. Improved prediction intervals for ARIMA and structural time series models are provided by
tsPI
.
·
Periodic ARMA models
:
pear
and
partsm
for periodic autoregressive time series models, and
perARMA
for periodic ARMA modelling and other procedures for periodic time series analysis.
·
ARFIMA models
: Some facilities for fractional differenced ARFIMA models are provided in the
fracdiff
package. The
arfima
package has more advanced and general facilities for ARFIMA and ARIMA models, including dynamic regression (transfer function) models. Fractional Gaussian noise and simple models for hyperbolic decay time series are handled in the
FGN
package.
·
Transfer function
models are provided by the
arimax
function in the
TSA
package, and the
arfima
function in the
arfima
package.
·
Outlier detection following the Chen-Liu approach is provided by
tsoutliers
.
·
Structural models
are implemented in
StructTS()
in stats, and in
stsm
and
stsm.class
.
KFKSDS
provides a naive implementation of the Kalman filter and smoothers for univariate state space models. Bayesian structural time series models are implemented in
bsts
·
Non-Gaussian time series can be handled with GLARMA state space models via
glarma
, and using Generalized Autoregressive Score models in the
GAS
package. Conditional Auto-Regression models using Monte Carlo Likelihood methods are implemented in
mclcar
.
·
GARCH models
:
garch()
from
tseries
fits basic GARCH models. Many variations on GARCH models are provided by
rugarch
. Other univariate GARCH packages include
fGarch
which implements ARIMA models with a wide class of GARCH innovations. There are many more GARCH packages described in the
Finance
task view.
·
Stochastic volatility
models are handled by
stochvol
in a Bayesian framework.
·
Count time series
models are handled in the
acp
package.
ZIM
provides for Zero-Inflated Models for count time series.
tsintermittent
implements various models for analysing and forecasting intermittent demand time series.
·
Censored time series
can be modelled using
cents
and
carx
.
·
Portmanteau tests
are provided via
Box.test()
in the stats package. Additional tests are given by
portes
and
WeightedPortTest
.
·
Change point detection
is provided in
strucchange
(using linear regression models), in
trend
(using nonparametric tests), and in
wbsts
(using wild binary segmentation). The
changepoint
package provides many popular changepoint methods, and
ecp
does nonparametric changepoint detection for univariate and multivariate series.
InspectChangepoint
uses sparse projection to estimate changepoints in high-dimensional time series.
·
Tests for possibly non-monotonic trends are provided by
funtimes
.
·
Time series imputation
is provided by the
imputeTS
package. Some more limited facilities are available using
na.interp()
from the
forecast
package.
·
Forecasts can be combined using
ForecastCombinations
which supports the most frequently used methods to combine forecasts.
forecastHybrid
provides functions for ensemble forecasts, combining approaches from the
forecast
package.
GeomComb
provides eigenvector-based (geometric) forecast combination methods, as well as other approaches.
opera
has facilities for online predictions based on combinations of forecasts provided by the user.
mafs
fits several forecast models and selects the best one according to an error metric.
·
Forecast evaluation is provided in the
accuracy()
function from
forecast
. Distributional forecast evaluation using scoring rules is available in
scoringRules
·
Tidy tools for forecasting are provided by
sweep
, converting objects produced in
forecast
to "tidy" data frames.
·
Miscellaneous
:
ltsa
contains methods for linear time series analysis,
timsac
for time series analysis and control, and
tsbugs
for time series BUGS models.
Frequency analysis
·
Spectral density estimation
is provided by
spectrum()
in the stats package, including the periodogram, smoothed periodogram and AR estimates. Bayesian spectral inference is provided by
bspec
.
quantspec
includes methods to compute and plot Laplace periodograms for univariate time series. The Lomb-Scargle periodogram for unevenly sampled time series is computed by
lomb
.
spectral
uses Fourier and Hilbert transforms for spectral filtering.
psd
produces adaptive, sine-multitaper spectral density estimates.
kza
provides Kolmogorov-Zurbenko Adaptive Filters including break detection, spectral analysis, wavelets and KZ Fourier Transforms.
multitaper
also provides some multitaper spectral analysis tools.
·
Wavelet methods
: The
wavelets
package includes computing wavelet filters, wavelet transforms and multiresolution analyses. Wavelet methods for time series analysis based on Percival and Walden (2000) are given in
wmtsa
.
WaveletComp
provides some tools for wavelet-based analysis of univariate and bivariate time series including cross-wavelets, phase-difference and significanc tests.
biwavelet
can be used to plot and compute the wavelet spectra, cross-wavelet spectra, and wavelet coherence of non-stationary time series. It also includes functions to cluster time series based on the (dis)similarities in their spectrum. Tests of white noise using wavelets are provided by
hwwntest
. Further wavelet methods can be found in the packages
brainwaver
,
rwt
,
waveslim
,
wavethresh
and
mvcwt
.
·
Harmonic regression
using Fourier terms is implemented in
HarmonicRegression
. The
forecast
package also provides some simple harmonic regression facilities via the
fourier
function.
Decomposition and Filtering
·
Filters and smoothing
:
filter()
in stats provides autoregressive and moving average linear filtering of multiple univariate time series. The
robfilter
package provides several robust time series filters, while
mFilter
includes miscellaneous time series filters useful for smoothing and extracting trend and cyclical components.
smooth()
from the stats package computes Tukey's running median smoothers, 3RS3R, 3RSS, 3R, etc.
sleekts
computes the 4253H twice smoothing method.
·
Decomposition
: Seasonal decomposition is discussed below. Autoregressive-based decomposition is provided by
ArDec
.
tsdecomp
implements ARIMA-based decomposition of quarterly and monthly data.
rmaf
uses a refined moving average filter for decomposition.
·
Singular Spectrum Analysis
is implemented in
Rssa
and
spectral.methods
.
·
Empirical Mode Decomposition
(EMD) and Hilbert spectral analysis is provided by
EMD
. Additional tools, including ensemble EMD, are available in
hht