forecast: Forecasting Functions for Time Series and Linear Models

> library(forecast)
Loading required package: zoo

Attaching package: 'zoo'
The following objects are masked from 'package:base':

    as.Date, as.Date.numeric
Loading required package: timeDate
This is forecast 7.1

バージョン: 7.1


関数名 概略
Acf (Partial) Autocorrelation and Cross-Correlation Function Estimation
Arima Fit ARIMA model to univariate time series
BoxCox Box Cox Transformation
BoxCox.lambda Automatic selection of Box Cox transformation parameter
CV Cross-validation statistic
accuracy Accuracy measures for forecast model
arfima Fit a fractionally differenced ARFIMA model
arima.errors ARIMA errors
arimaorder Return the order of an ARIMA or ARFIMA model
auto.arima Fit best ARIMA model to univariate time series
autoplot.acf ggplot (Partial) Autocorrelation and Cross-Correlation Function Estimation
autoplot.decomposed.ts ggplot of a decomposed time series object
autoplot.stl ggplot STL object
autoplot.ts Automatically create a ggplot for time series objects
bats BATS model (Exponential smoothing state space model with Box-Cox transformation, ARMA errors, Trend and Seasonal components)
bizdays Number of trading days in each season
croston Forecasts for intermittent demand using Croston's method
dm.test Diebold-Mariano test for predictive accuracy
dshw Double-Seasonal Holt-Winters Forecasting
easter Easter holidays in each season
ets Exponential smoothing state space model
findfrequency Find dominant frequency of a time series
fitted.Arima One-step in-sample forecasts using ARIMA models
forecast Forecasting time series
forecast.Arima Forecasting using ARIMA or ARFIMA models
forecast.HoltWinters Forecasting using Holt-Winters objects
forecast.StructTS Forecasting using Structural Time Series models
forecast.bats Forecasting using BATS and TBATS models
forecast.ets Forecasting using ETS models
forecast.lm Forecast a linear model with possible time series components
forecast.mlm Forecast a multiple linear model with possible time series components
forecast.stl Forecasting using stl objects
fortify.forecast Fortify a forecast object to data.frame for ggplot
gas Australian monthly gas production
geom_forecast Forecast plot
getResponse Get response variable from time series model.
ggmonthplot Create a seasonal subseries ggplot
gold Daily morning gold prices
is.constant Is an object constant?
is.ets Is an object a particular model type?
is.forecast Is an object a particular forecast type?
`logLik.ets Log-Likelihood of an ets object
ma Moving-average smoothing
meanf Mean Forecast
mforecast Forecasting time series
monthdays Number of days in each season
msts Multi-Seasonal Time Series
na.interp Interpolate missing values in a time series
naive Naive forecasts
ndiffs Number of differences required for a stationary series
nnetar Neural Network Time Series Forecasts
plot.Arima Plot characteristic roots from ARIMA model
plot.bats Plot components from BATS model
plot.ets Plot components from ETS model
plot.forecast Forecast plot
plot.mforecast Multivariate forecast plot
rwf Random Walk Forecast
seasadj Seasonal adjustment
seasonaldummy Seasonal dummy variables
seasonplot Seasonal plot
ses Exponential smoothing forecasts
simulate.ets Simulation from a time series model
sindexf Forecast seasonal index
splinef Cubic Spline Forecast
subset.ts Subsetting a time series
taylor Half-hourly electricity demand
tbats TBATS model (Exponential smoothing state space model with Box-Cox transformation, ARMA errors, Trend and Seasonal components)
tbats.components Extract components of a TBATS model
thetaf Theta method forecast
tsclean Identify and replace outliers and missing values in a time series
tsdisplay Time series display
tslm Fit a linear model with time series components
tsoutliers Identify and replace outliers in a time series
wineind Australian total wine sales
woolyrnq Quarterly production of woollen yarn in Australia

autoplot.acf

> ggAcf(wineind)
> ggtaperedacf(wineind)
> ggtaperedpacf(wineind)
> ggCcf(mdeaths, fdeaths)

ets

> (fit <- ets(USAccDeaths))
ETS(A,N,A) 

Call:
 ets(y = USAccDeaths) 

  Smoothing parameters:
    alpha = 0.5891 
    gamma = 0.0001 

  Initial states:
    l = 9196.9696 
    s=-50.3296 -259.3484 256.8254 -87.421 994.235 1659.364
           752.2699 349.7909 -505.1988 -776.3242 -1524.744 -809.1198

  sigma:  264.7529

     AIC     AICc      BIC 
1139.267 1146.635 1171.140
> # forecast(fit) %>% plot()

forecast.stl

> stl(mdeaths, s.window = "periodic", robust = TRUE)
 Call:
 stl(x = mdeaths, s.window = "periodic", robust = TRUE)

Components
            seasonal    trend    remainder
Jan 1974  544.790382 1539.447   49.7622186
Feb 1974  398.486952 1548.667  -84.1535032
Mar 1974  440.879079 1557.886 -121.7647811
Apr 1974  273.260689 1566.452   37.2870305
May 1974 -165.547292 1575.019   82.5284336
Jun 1974 -289.908895 1583.141  -44.2319393
Jul 1974 -336.563852 1591.263   25.3010407
Aug 1974 -433.491123 1599.261  -34.7698782
Sep 1974 -446.830170 1607.259   48.5709782
Oct 1974 -229.698550 1611.397  110.3016551
Nov 1974    7.435031 1615.535   -1.9696284
Dec 1974  237.187719 1612.611   -3.7990132
Jan 1975  544.790382 1609.688  -51.4783735
Feb 1975  398.486952 1603.446  135.0666089
Mar 1975  440.879079 1597.205  114.9160354
Apr 1975  273.260689 1588.730  -28.9907127
May 1975 -165.547292 1580.255  -11.7078692
Jun 1975 -289.908895 1570.861    7.0476207
Jul 1975 -336.563852 1561.467  -38.9035367
Aug 1975 -433.491123 1549.895   16.5957688
Sep 1975 -446.830170 1538.323  -38.4931503
Oct 1975 -229.698550 1524.625   52.0732838
Nov 1975    7.435031 1510.927   26.6377576
Dec 1975  237.187719 1499.658  329.1539731
Jan 1976  544.790382 1488.389  -13.1797870
Feb 1976  398.486952 1479.314  872.1991605
Mar 1976  440.879079 1470.238  371.8825520
Apr 1976  273.260689 1460.039 -254.2995686
May 1976 -165.547292 1449.839  -95.2920977
Jun 1976 -289.908895 1441.275    8.6334224
Jul 1976 -336.563852 1432.712   16.8522952
Aug 1976 -433.491123 1432.518  -29.0271835
Sep 1976 -446.830170 1432.325   13.5051132
Oct 1976 -229.698550 1433.158    4.5409127
Nov 1976    7.435031 1433.990   25.5747518
Dec 1976  237.187719 1434.961  386.8508559
Jan 1977  544.790382 1435.933  259.2769845
Feb 1977  398.486952 1435.474 -199.9611683
Mar 1977  440.879079 1435.016 -153.8948770
Apr 1977  273.260689 1433.437   94.3024478
May 1977 -165.547292 1431.858  -20.3106358
Jun 1977 -289.908895 1430.856   21.0527248
Jul 1977 -336.563852 1429.854   -6.2905618
Aug 1977 -433.491123 1432.591   13.9001512
Sep 1977 -446.830170 1435.328  -29.4973604
Oct 1977 -229.698550 1440.494  -31.7957067
Nov 1977    7.435031 1445.661 -224.0960133
Dec 1977  237.187719 1448.767  -30.9544842
Jan 1978  544.790382 1451.873   22.3370693
Feb 1978  398.486952 1452.916  432.5967239
Mar 1978  440.879079 1453.960   47.1608225
Apr 1978  273.260689 1454.696 -304.9568661
May 1978 -165.547292 1455.432   50.1150369
Jun 1978 -289.908895 1452.224   24.6852664
Jul 1978 -336.563852 1449.015  -14.4511513
Aug 1978 -433.491123 1442.435   -4.9437171
Sep 1978 -446.830170 1435.855  -19.0245076
Oct 1978 -229.698550 1426.502  -56.8030454
Nov 1978    7.435031 1417.149 -314.5835435
Dec 1978  237.187719 1409.590  165.2223051
Jan 1979  544.790382 1402.031  316.1781783
Feb 1979  398.486952 1397.596   23.9168069
Mar 1979  440.879079 1393.161   11.9598797
Apr 1979  273.260689 1386.644 -128.9049536
May 1979 -165.547292 1380.127    0.4198046
Jun 1979 -289.908895 1371.711   -6.8021829
Jul 1979 -336.563852 1363.295   29.2691825
Aug 1979 -433.491123 1354.590   53.9010952
Sep 1979 -446.830170 1345.885   40.9447832
Oct 1979 -229.698550 1336.949  -26.2505972
Nov 1979    7.435031 1328.013  -41.4479381
Dec 1979  237.187719 1318.650 -214.8373254

forecast

時系列データ・モデルの予報

Arguments

  • object
  • h... 予測を行う期間
  • level
  • fan
  • robust
  • lambda
  • find.frequency
  • allow.multiplicative.trend
  • ...
> forecast(fdeaths) %>% str(max.level = 1)
List of 9
 $ model    :List of 18
  ..- attr(*, "class")= chr "ets"
 $ mean     : Time-Series [1:24] from 1980 to 1982: 799 818 748 593 479 ...
 $ level    : num [1:2] 80 95
 $ x        : Time-Series [1:72] from 1974 to 1980: 901 689 827 677 522 406 441 393 387 582 ...
 $ upper    : Time-Series [1:24, 1:2] from 1980 to 1982: 898 920 841 667 539 ...
  ..- attr(*, "dimnames")=List of 2
 $ lower    : Time-Series [1:24, 1:2] from 1980 to 1982: 700 717 656 519 419 ...
  ..- attr(*, "dimnames")=List of 2
 $ fitted   : Time-Series [1:72] from 1974 to 1980: 851 875 790 628 509 ...
 $ method   : chr "ETS(M,N,M)"
 $ residuals: Time-Series [1:72] from 1974 to 1980: 0.0586 -0.2125 0.0462 0.0781 0.0248 ...
 - attr(*, "class")= chr "forecast"

ggmonthplot

> ggmonthplot(mdeaths)

msts

> msts(USAccDeaths, seasonal.periods = 12, start = 1949)
       Jan   Feb   Mar   Apr   May   Jun   Jul   Aug   Sep   Oct   Nov
1949  9007  8106  8928  9137 10017 10826 11317 10744  9713  9938  9161
1950  7750  6981  8038  8422  8714  9512 10120  9823  8743  9129  8710
1951  8162  7306  8124  7870  9387  9556 10093  9620  8285  8466  8160
1952  7717  7461  7767  7925  8623  8945 10078  9179  8037  8488  7874
1953  7792  6957  7726  8106  8890  9299 10625  9302  8314  8850  8265
1954  7836  6892  7791  8192  9115  9434 10484  9827  9110  9070  8633
       Dec
1949  8927
1950  8680
1951  8034
1952  8647
1953  8796
1954  9240

seasonaldummy

Arguments

  • x
  • h
  • K
> seasonaldummy(ldeaths)
      Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov
 [1,]   1   0   0   0   0   0   0   0   0   0   0
 [2,]   0   1   0   0   0   0   0   0   0   0   0
 [3,]   0   0   1   0   0   0   0   0   0   0   0
 [4,]   0   0   0   1   0   0   0   0   0   0   0
 [5,]   0   0   0   0   1   0   0   0   0   0   0
 [6,]   0   0   0   0   0   1   0   0   0   0   0
 [7,]   0   0   0   0   0   0   1   0   0   0   0
 [8,]   0   0   0   0   0   0   0   1   0   0   0
 [9,]   0   0   0   0   0   0   0   0   1   0   0
[10,]   0   0   0   0   0   0   0   0   0   1   0
[11,]   0   0   0   0   0   0   0   0   0   0   1
[12,]   0   0   0   0   0   0   0   0   0   0   0
[13,]   1   0   0   0   0   0   0   0   0   0   0
[14,]   0   1   0   0   0   0   0   0   0   0   0
[15,]   0   0   1   0   0   0   0   0   0   0   0
[16,]   0   0   0   1   0   0   0   0   0   0   0
[17,]   0   0   0   0   1   0   0   0   0   0   0
[18,]   0   0   0   0   0   1   0   0   0   0   0
[19,]   0   0   0   0   0   0   1   0   0   0   0
[20,]   0   0   0   0   0   0   0   1   0   0   0
[21,]   0   0   0   0   0   0   0   0   1   0   0
[22,]   0   0   0   0   0   0   0   0   0   1   0
[23,]   0   0   0   0   0   0   0   0   0   0   1
[24,]   0   0   0   0   0   0   0   0   0   0   0
[25,]   1   0   0   0   0   0   0   0   0   0   0
[26,]   0   1   0   0   0   0   0   0   0   0   0
[27,]   0   0   1   0   0   0   0   0   0   0   0
[28,]   0   0   0   1   0   0   0   0   0   0   0
[29,]   0   0   0   0   1   0   0   0   0   0   0
[30,]   0   0   0   0   0   1   0   0   0   0   0
[31,]   0   0   0   0   0   0   1   0   0   0   0
[32,]   0   0   0   0   0   0   0   1   0   0   0
[33,]   0   0   0   0   0   0   0   0   1   0   0
[34,]   0   0   0   0   0   0   0   0   0   1   0
[35,]   0   0   0   0   0   0   0   0   0   0   1
[36,]   0   0   0   0   0   0   0   0   0   0   0
[37,]   1   0   0   0   0   0   0   0   0   0   0
[38,]   0   1   0   0   0   0   0   0   0   0   0
[39,]   0   0   1   0   0   0   0   0   0   0   0
[40,]   0   0   0   1   0   0   0   0   0   0   0
[41,]   0   0   0   0   1   0   0   0   0   0   0
[42,]   0   0   0   0   0   1   0   0   0   0   0
[43,]   0   0   0   0   0   0   1   0   0   0   0
[44,]   0   0   0   0   0   0   0   1   0   0   0
[45,]   0   0   0   0   0   0   0   0   1   0   0
[46,]   0   0   0   0   0   0   0   0   0   1   0
[47,]   0   0   0   0   0   0   0   0   0   0   1
[48,]   0   0   0   0   0   0   0   0   0   0   0
[49,]   1   0   0   0   0   0   0   0   0   0   0
[50,]   0   1   0   0   0   0   0   0   0   0   0
[51,]   0   0   1   0   0   0   0   0   0   0   0
[52,]   0   0   0   1   0   0   0   0   0   0   0
[53,]   0   0   0   0   1   0   0   0   0   0   0
[54,]   0   0   0   0   0   1   0   0   0   0   0
[55,]   0   0   0   0   0   0   1   0   0   0   0
[56,]   0   0   0   0   0   0   0   1   0   0   0
[57,]   0   0   0   0   0   0   0   0   1   0   0
[58,]   0   0   0   0   0   0   0   0   0   1   0
[59,]   0   0   0   0   0   0   0   0   0   0   1
[60,]   0   0   0   0   0   0   0   0   0   0   0
[61,]   1   0   0   0   0   0   0   0   0   0   0
[62,]   0   1   0   0   0   0   0   0   0   0   0
[63,]   0   0   1   0   0   0   0   0   0   0   0
[64,]   0   0   0   1   0   0   0   0   0   0   0
[65,]   0   0   0   0   1   0   0   0   0   0   0
[66,]   0   0   0   0   0   1   0   0   0   0   0
[67,]   0   0   0   0   0   0   1   0   0   0   0
[68,]   0   0   0   0   0   0   0   1   0   0   0
[69,]   0   0   0   0   0   0   0   0   1   0   0
[70,]   0   0   0   0   0   0   0   0   0   1   0
[71,]   0   0   0   0   0   0   0   0   0   0   1
[72,]   0   0   0   0   0   0   0   0   0   0   0

seasonplot

> seasonplot(AirPassengers, col = rainbow(12), year.labels = TRUE)

tbats

> tbats(USAccDeaths)
TBATS(1, {0,0}, 0.945, {<12,5>})

Call: tbats(y = USAccDeaths)

Parameters
  Alpha: 0.5956123
  Beta: -0.04123801
  Damping Parameter: 0.945006
  Gamma-1 Values: -0.001180936
  Gamma-2 Values: 0.0003080163

Seed States:
             [,1]
 [1,]  9121.15886
 [2,]   -14.79382
 [3,] -1003.76744
 [4,]   276.33171
 [5,]  -197.31584
 [6,]   140.11645
 [7,]   -38.97894
 [8,]  -292.27889
 [9,]  -324.93654
[10,]  -157.58835
[11,]   -72.25990
[12,]  -257.57345

Sigma: 260.3327
AIC: 1142.842

tsdisplay

> diff(WWWusage) %>% tsdisplay()
> ggtsdisplay(USAccDeaths, plot.type = "scatter")

tslm

時系列データの線形モデル

> y <- ts(rnorm(120,0,3) + 1:120 + 20 * sin(2*pi*(1:120)/12), frequency = 12)
> fit <- tslm(y ~ trend + season)
> # plot(forecast(fit, h = 20))