9
9
import converters
10
10
11
11
12
- forecast = importr ('forecast' )
12
+ fc = importr ('forecast' )
13
13
stats = importr ('stats' )
14
14
NULL = robjects .NULL
15
15
NA = robjects .NA_Real
@@ -71,7 +71,7 @@ def meanf(x, h=10, level=(80,95), lam=NULL):
71
71
'''
72
72
x , is_pandas = converters .to_ts (x )
73
73
level = converters .map_arg (level )
74
- out = forecast .meanf (x , h , level = level , ** {'lambda' : lam })
74
+ out = fc .meanf (x , h , level = level , ** {'lambda' : lam })
75
75
return converters .forecast_out (out , is_pandas )
76
76
77
77
@@ -96,7 +96,7 @@ def thetaf(x, h=10, level=(80, 95)):
96
96
'''
97
97
x , is_pandas = converters .to_ts (x )
98
98
level = converters .map_arg (level )
99
- out = forecast .thetaf (x , h , level = level )
99
+ out = fc .thetaf (x , h , level = level )
100
100
return converters .forecast_out (out , is_pandas )
101
101
102
102
@@ -122,7 +122,7 @@ def naive(x, h=10, level=(80, 95), lam=NULL):
122
122
'''
123
123
x , is_pandas = converters .to_ts (x )
124
124
level = converters .map_arg (level )
125
- out = forecast .naive (x , h , level = level , ** {'lambda' : lam })
125
+ out = fc .naive (x , h , level = level , ** {'lambda' : lam })
126
126
return converters .forecast_out (out , is_pandas )
127
127
128
128
@@ -151,7 +151,7 @@ def snaive(x, h=None, level=(80, 95), lam=NULL):
151
151
x , is_pandas = converters .to_ts (x )
152
152
h = _get_horizon (x , h )
153
153
level = converters .map_arg (level )
154
- out = forecast .snaive (x , h , level = level , ** {'lambda' : lam })
154
+ out = fc .snaive (x , h , level = level , ** {'lambda' : lam })
155
155
return converters .forecast_out (out , is_pandas )
156
156
157
157
@@ -178,7 +178,7 @@ def rwf(x, h=10, drift=False, level=(80, 95), lam=NULL):
178
178
'''
179
179
x , is_pandas = converters .to_ts (x )
180
180
level = converters .map_arg (level )
181
- out = forecast .rwf (x , h , drift , level = level , ** {'lambda' : lam })
181
+ out = fc .rwf (x , h , drift , level = level , ** {'lambda' : lam })
182
182
return converters .forecast_out (out , is_pandas )
183
183
184
184
@@ -210,7 +210,7 @@ def ses(x, h=10, level=(80, 95), alpha=NULL, lam=NULL):
210
210
raise ValueError ('alpha must be between 0.0001 and 0.9999, if given' )
211
211
x , is_pandas = converters .to_ts (x )
212
212
level = converters .map_arg (level )
213
- out = forecast .ses (x , h , level = level , alpha = alpha ,
213
+ out = fc .ses (x , h , level = level , alpha = alpha ,
214
214
initial = 'simple' , ** {'lambda' : lam })
215
215
return converters .forecast_out (out , is_pandas )
216
216
@@ -251,7 +251,7 @@ def holt(x, h=10, level=(80, 95), alpha=NULL,
251
251
raise ValueError ('beta must be between 0.0001 and 0.9999, if given' )
252
252
x , is_pandas = converters .to_ts (x )
253
253
level = converters .map_arg (level )
254
- out = forecast .holt (x , h , level = level , alpha = alpha , beta = beta ,
254
+ out = fc .holt (x , h , level = level , alpha = alpha , beta = beta ,
255
255
damped = damped , initial = 'simple' , ** {'lambda' : lam })
256
256
return converters .forecast_out (out , is_pandas )
257
257
@@ -299,12 +299,12 @@ def hw(x, h=None, level=(80, 95), alpha=NULL, beta=NULL,
299
299
x , is_pandas = converters .to_ts (x )
300
300
h = _get_horizon (x , h )
301
301
level = converters .map_arg (level )
302
- out = forecast .hw (x , h , level = level , alpha = alpha , beta = beta , gamma = gamma ,
302
+ out = fc .hw (x , h , level = level , alpha = alpha , beta = beta , gamma = gamma ,
303
303
damped = damped , initial = 'simple' , ** {'lambda' : lam })
304
304
return converters .forecast_out (out , is_pandas )
305
305
306
306
307
- def forecast_ts (x , h = None , ** kwargs ):
307
+ def forecast (x , h = None , ** kwargs ):
308
308
'''
309
309
Generate a forecast for the time series x, using ets if x is non-seasonal
310
310
or has frequency less than 13, and stlf if x is periodic with frequency
@@ -334,7 +334,7 @@ def forecast_ts(x, h=None, **kwargs):
334
334
x , is_pandas = converters .to_ts (x )
335
335
h = _get_horizon (x , h )
336
336
kwargs = converters .translate_kwargs (** kwargs )
337
- out = forecast .forecast (x , h = h , ** kwargs )
337
+ out = fc .forecast (x , h = h , ** kwargs )
338
338
return converters .forecast_out (out , is_pandas )
339
339
340
340
@@ -394,12 +394,12 @@ def ets(x, h=None, model_spec='ZZZ', damped=NULL, alpha=NULL,
394
394
'additive.only' : additive_only ,
395
395
'opt.crit' : opt_crit ,
396
396
'lambda' : lam }
397
- ets_model = forecast .ets (x , model = model_spec , damped = damped , alpha = alpha ,
397
+ ets_model = fc .ets (x , model = model_spec , damped = damped , alpha = alpha ,
398
398
beta = beta , gamma = gamma , phi = phi , ic = ic , ** kwargs )
399
399
h = _get_horizon (x , h )
400
400
level = converters .map_arg (level )
401
401
# NB: default lambda is correct - it will be taken from model
402
- out = forecast .forecast_ets (ets_model , h , level = level )
402
+ out = fc .forecast_ets (ets_model , h , level = level )
403
403
return converters .forecast_out (out , is_pandas )
404
404
405
405
@@ -413,7 +413,8 @@ def arima(x, h=None, level=(80,95), order=(0,0,0), seasonal=(0,0,0),
413
413
Args:
414
414
x: an R time series, obtained from converters.ts(), or a Pandas Series
415
415
with the correct index (e.g. from converters.sequence_as_series().
416
- h: the forecast horizon, default 10
416
+ h: the forecast horizon, default 10 if fitting a non-seasonal model,
417
+ 2 * the frequency of the series for seasonal models.
417
418
level: A number or list/tuple of prediction interval confidence values.
418
419
Default is 80% and 95% intervals.
419
420
order: the non-seasonal part of the arima model
@@ -444,11 +445,12 @@ def arima(x, h=None, level=(80,95), order=(0,0,0), seasonal=(0,0,0),
444
445
order = converters .map_arg (order )
445
446
seasonal = converters .map_arg (seasonal )
446
447
kwargs ['lambda' ] = lam
447
- model = forecast .Arima (x , order = order , seasonal = seasonal , ** kwargs )
448
- out = forecast .forecast (model , h = h , level = level )
448
+ model = fc .Arima (x , order = order , seasonal = seasonal , ** kwargs )
449
+ out = fc .forecast (model , h = h , level = level )
449
450
return converters .forecast_out (out , is_pandas )
450
451
451
452
453
+ # TODO: convert xreg and newxreg if needed
452
454
def auto_arima (x , h = None , d = NA , D = NA , max_p = 5 , max_q = 5 , max_P = 2 , max_Q = 2 ,
453
455
max_order = 5 , max_d = 2 , max_D = 1 , start_p = 2 , start_q = 2 ,
454
456
start_P = 1 , start_Q = 1 , stationary = False , seasonal = True ,
@@ -484,7 +486,8 @@ def auto_arima(x, h=None, d=NA, D=NA, max_p=5, max_q=5, max_P=2, max_Q=2,
484
486
xreg : An optional vector or matrix of regressors, which must have one
485
487
row/element for each point in x. Default is NULL, for no regressors.
486
488
newxreg : If regressors were used to fit the model, then they must be
487
- supplied for the forecast period as newxreg.
489
+ supplied for the forecast period as newxreg. If newxreg is present,
490
+ h is ignored.
488
491
test : Test to use to determine number of first differences. Default
489
492
is 'kpss', for the KPSS test. Other values are 'adf' for augmented
490
493
Dickey-Fuller, or 'pp' for Phillips-Perron.
@@ -507,13 +510,13 @@ def auto_arima(x, h=None, d=NA, D=NA, max_p=5, max_q=5, max_P=2, max_Q=2,
507
510
'max.D' : max_D , 'start.p' : start_p , 'start.q' : start_q ,
508
511
'start.P' : start_P , 'start.Q' : start_Q ,
509
512
'seasonal.test' : seasonal_test , 'lambda' : lam }
510
- arima_model = forecast .auto_arima (x , d = d , D = D , stationary = stationary ,
513
+ arima_model = fc .auto_arima (x , d = d , D = D , stationary = stationary ,
511
514
seasonal = seasonal , ic = ic , xreg = xreg ,
512
515
test = test , ** kwargs )
513
516
h = _get_horizon (x , h )
514
517
level = converters .map_arg (level )
515
518
# NB: default lambda is correct - it will be taken from model
516
- out = forecast .forecast_Arima (arima_model , h , level = level , xreg = newxreg )
519
+ out = fc .forecast_Arima (arima_model , h , level = level , xreg = newxreg )
517
520
return converters .forecast_out (out , is_pandas )
518
521
519
522
@@ -561,7 +564,7 @@ def stlf(x, h=None, s_window=7, robust=False, lam=NULL, method='ets',
561
564
kwargs = {'s.window' : s_window ,
562
565
'lambda' : lam }
563
566
level = converters .map_arg (level )
564
- out = forecast .stlf (x , h , level = level , robust = robust , method = method ,
567
+ out = fc .stlf (x , h , level = level , robust = robust , method = method ,
565
568
etsmodel = etsmodel , xreg = xreg , newxreg = newxreg , ** kwargs )
566
569
return converters .forecast_out (out , is_pandas )
567
570
@@ -644,7 +647,7 @@ def seasadj(decomp):
644
647
an object that maps an R time series of the seasonally adjusted
645
648
values of the series that decomp was formed from
646
649
'''
647
- return forecast .seasadj (decomp )
650
+ return fc .seasadj (decomp )
648
651
649
652
650
653
def sindexf (decomp , h ):
@@ -660,7 +663,7 @@ def sindexf(decomp, h):
660
663
an object that maps to am R time series containing the seasonal component
661
664
of decomp, projected naively forward h steps.
662
665
'''
663
- return forecast .sindexf (x , h )
666
+ return fc .sindexf (x , h )
664
667
665
668
666
669
def BoxCox (x , lam ):
@@ -681,7 +684,7 @@ def BoxCox(x, lam):
681
684
If x is a Pandas Series, a Pandas Series is returned.
682
685
'''
683
686
x , is_pandas = converters .to_ts (x )
684
- out = forecast .BoxCox (x , ** {'lambda' : lam })
687
+ out = fc .BoxCox (x , ** {'lambda' : lam })
685
688
return converters .series_out (out , is_pandas )
686
689
687
690
@@ -702,7 +705,7 @@ def InvBoxCox(x, lam):
702
705
If x is a Pandas Series, a Pandas Series is returned.
703
706
'''
704
707
x , is_pandas = converters .to_ts (x )
705
- out = forecast .InvBoxCox (x , ** {'lambda' : lam })
708
+ out = fc .InvBoxCox (x , ** {'lambda' : lam })
706
709
return converters .series_out (out , is_pandas )
707
710
708
711
@@ -722,7 +725,7 @@ def BoxCox_lambda(x, method='guerrero', lower=-1, upper=2):
722
725
value of lambda for the series x, as calculated by the selected method
723
726
'''
724
727
x , _ = converters .to_ts (x )
725
- return forecast .BoxCox_lambda (x , method = method , lower = lower , upper = upper )[0 ]
728
+ return fc .BoxCox_lambda (x , method = method , lower = lower , upper = upper )[0 ]
726
729
727
730
728
731
def na_interp (x , lam = NULL ):
@@ -746,11 +749,11 @@ def na_interp(x, lam=NULL):
746
749
In either case, missing values are filled.
747
750
'''
748
751
x , is_pandas = converters .to_ts (x )
749
- out = forecast .na_interp (x , ** {'lambda' : lam })
752
+ out = fc .na_interp (x , ** {'lambda' : lam })
750
753
return converters .series_out (out , is_pandas )
751
754
752
755
753
- def accuracy (fc , x = None , ** kwargs ):
756
+ def accuracy (result , x = None , ** kwargs ):
754
757
'''
755
758
Computes an R matrix of forecast accuracy measures. Must take an R forecast
756
759
object for input, since the residuals are not included in the Pandas
@@ -769,7 +772,7 @@ def accuracy(fc, x=None, **kwargs):
769
772
* Theil's U (only if x provided)
770
773
771
774
Args:
772
- fc : an R forecast object
775
+ result : an R forecast object
773
776
x: optional R vector of true values for the forecast (test data)
774
777
d: Number of first differences taken in forecast, default is none.
775
778
D: Number of seasonal differences taken in forecast, default is none.
@@ -780,7 +783,7 @@ def accuracy(fc, x=None, **kwargs):
780
783
'''
781
784
if x is not None :
782
785
kwargs ['x' ] = x
783
- return forecast .accuracy (fc , ** kwargs )
786
+ return fc .accuracy (result , ** kwargs )
784
787
785
788
786
789
def tsclean (x , ** kwargs ):
@@ -802,7 +805,7 @@ def tsclean(x, **kwargs):
802
805
'''
803
806
x , is_pandas = converters .to_ts (x )
804
807
kwargs = converters .translate_kwargs (** kwargs )
805
- out = forecast .tsclean (x , ** kwargs )
808
+ out = fc .tsclean (x , ** kwargs )
806
809
return converters .series_out (out , is_pandas )
807
810
808
811
@@ -818,7 +821,7 @@ def findfrequency(x):
818
821
The dominant frequency in x, or 1 if there isn't one.
819
822
'''
820
823
x , _ = converters .to_ts (x )
821
- return forecast .findfrequency (x )[0 ]
824
+ return fc .findfrequency (x )[0 ]
822
825
823
826
824
827
def ndiffs (x , ** kwargs ):
@@ -839,7 +842,7 @@ def ndiffs(x, **kwargs):
839
842
'''
840
843
x , _ = converters .to_ts (x )
841
844
kwargs = converters .translate_kwargs (** kwargs )
842
- return forecast .ndiffs (x , ** kwargs )[0 ]
845
+ return fc .ndiffs (x , ** kwargs )[0 ]
843
846
844
847
845
848
def nsdiffs (x , ** kwargs ):
@@ -860,7 +863,7 @@ def nsdiffs(x, **kwargs):
860
863
'''
861
864
x , _ = converters .to_ts (x )
862
865
kwargs = converters .translate_kwargs (** kwargs )
863
- return forecast .nsdiffs (x , ** kwargs )[0 ]
866
+ return fc .nsdiffs (x , ** kwargs )[0 ]
864
867
865
868
866
869
def acf (x , lag_max = NULL ):
@@ -878,7 +881,7 @@ def acf(x, lag_max=NULL):
878
881
'''
879
882
x , is_pandas = converters .to_ts (x )
880
883
kwargs = {'lag.max' : lag_max }
881
- out = forecast .Acf (x , plot = False , ** kwargs )
884
+ out = fc .Acf (x , plot = False , ** kwargs )
882
885
return converters .acf_out (out , is_pandas )
883
886
884
887
@@ -897,7 +900,7 @@ def pacf(x, lag_max=NULL):
897
900
'''
898
901
x , is_pandas = converters .to_ts (x )
899
902
kwargs = {'lag.max' : lag_max }
900
- out = forecast .Pacf (x , plot = False , ** kwargs )
903
+ out = fc .Pacf (x , plot = False , ** kwargs )
901
904
return converters .acf_out (out , is_pandas )
902
905
903
906
0 commit comments