Skip to content

Commit e8ee765

Browse files
committed
Changed the name for the forecast package reference to 'fc' from 'forecast'
in wrappers.py. Changed 'forecast_ts' to 'forecast' in wrappers. Changed the test for forecast_ts in test_all to reflect the name change.
1 parent 0ac5dc5 commit e8ee765

File tree

3 files changed

+60
-38
lines changed

3 files changed

+60
-38
lines changed

examples/livestock.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
'''
2+
This example shows automatic fitting of an arima model with a linear
3+
trend as a regressor. It is based on a post on Hyndsight, a blog by
4+
R Forecast package author Rob J. Hyndman.
5+
6+
See: http://robjhyndman.com/hyndsight/piecewise-linear-trends/#more-3413
7+
'''
8+
from rforecast import ts_io
9+
from rforecast import wrappers
10+
from rforecast import converters
11+
from rforecast import plots
12+
13+
stock = ts_io.read_ts('livestock', 'fpp')
14+
n = len(stock)
15+
xreg = converters.matrix(range(n))
16+
newxreg = converters.matrix(range(n, n + 10))
17+
fc = wrappers.auto_arima(stock, xreg=xreg, newxreg=newxreg)
18+
print fc
19+
plots.plot_forecast(fc)

rforecast/wrappers.py

Lines changed: 38 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
import converters
1010

1111

12-
forecast = importr('forecast')
12+
fc = importr('forecast')
1313
stats = importr('stats')
1414
NULL = robjects.NULL
1515
NA = robjects.NA_Real
@@ -71,7 +71,7 @@ def meanf(x, h=10, level=(80,95), lam=NULL):
7171
'''
7272
x, is_pandas = converters.to_ts(x)
7373
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})
7575
return converters.forecast_out(out, is_pandas)
7676

7777

@@ -96,7 +96,7 @@ def thetaf(x, h=10, level=(80, 95)):
9696
'''
9797
x, is_pandas = converters.to_ts(x)
9898
level = converters.map_arg(level)
99-
out = forecast.thetaf(x, h, level=level)
99+
out = fc.thetaf(x, h, level=level)
100100
return converters.forecast_out(out, is_pandas)
101101

102102

@@ -122,7 +122,7 @@ def naive(x, h=10, level=(80, 95), lam=NULL):
122122
'''
123123
x, is_pandas = converters.to_ts(x)
124124
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})
126126
return converters.forecast_out(out, is_pandas)
127127

128128

@@ -151,7 +151,7 @@ def snaive(x, h=None, level=(80, 95), lam=NULL):
151151
x, is_pandas = converters.to_ts(x)
152152
h = _get_horizon(x, h)
153153
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})
155155
return converters.forecast_out(out, is_pandas)
156156

157157

@@ -178,7 +178,7 @@ def rwf(x, h=10, drift=False, level=(80, 95), lam=NULL):
178178
'''
179179
x, is_pandas = converters.to_ts(x)
180180
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})
182182
return converters.forecast_out(out, is_pandas)
183183

184184

@@ -210,7 +210,7 @@ def ses(x, h=10, level=(80, 95), alpha=NULL, lam=NULL):
210210
raise ValueError('alpha must be between 0.0001 and 0.9999, if given')
211211
x, is_pandas = converters.to_ts(x)
212212
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,
214214
initial='simple', **{'lambda' : lam})
215215
return converters.forecast_out(out, is_pandas)
216216

@@ -251,7 +251,7 @@ def holt(x, h=10, level=(80, 95), alpha=NULL,
251251
raise ValueError('beta must be between 0.0001 and 0.9999, if given')
252252
x, is_pandas = converters.to_ts(x)
253253
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,
255255
damped=damped, initial='simple', **{'lambda' : lam})
256256
return converters.forecast_out(out, is_pandas)
257257

@@ -299,12 +299,12 @@ def hw(x, h=None, level=(80, 95), alpha=NULL, beta=NULL,
299299
x, is_pandas = converters.to_ts(x)
300300
h = _get_horizon(x, h)
301301
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,
303303
damped=damped, initial='simple', **{'lambda' : lam})
304304
return converters.forecast_out(out, is_pandas)
305305

306306

307-
def forecast_ts(x, h=None, **kwargs):
307+
def forecast(x, h=None, **kwargs):
308308
'''
309309
Generate a forecast for the time series x, using ets if x is non-seasonal
310310
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):
334334
x, is_pandas = converters.to_ts(x)
335335
h = _get_horizon(x, h)
336336
kwargs = converters.translate_kwargs(**kwargs)
337-
out = forecast.forecast(x, h=h, **kwargs)
337+
out = fc.forecast(x, h=h, **kwargs)
338338
return converters.forecast_out(out, is_pandas)
339339

340340

@@ -394,12 +394,12 @@ def ets(x, h=None, model_spec='ZZZ', damped=NULL, alpha=NULL,
394394
'additive.only' : additive_only,
395395
'opt.crit' : opt_crit,
396396
'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,
398398
beta=beta, gamma=gamma, phi=phi, ic=ic, **kwargs)
399399
h = _get_horizon(x, h)
400400
level = converters.map_arg(level)
401401
# 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)
403403
return converters.forecast_out(out, is_pandas)
404404

405405

@@ -413,7 +413,8 @@ def arima(x, h=None, level=(80,95), order=(0,0,0), seasonal=(0,0,0),
413413
Args:
414414
x: an R time series, obtained from converters.ts(), or a Pandas Series
415415
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.
417418
level: A number or list/tuple of prediction interval confidence values.
418419
Default is 80% and 95% intervals.
419420
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),
444445
order = converters.map_arg(order)
445446
seasonal = converters.map_arg(seasonal)
446447
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)
449450
return converters.forecast_out(out, is_pandas)
450451

451452

453+
# TODO: convert xreg and newxreg if needed
452454
def auto_arima(x, h=None, d=NA, D=NA, max_p=5, max_q=5, max_P=2, max_Q=2,
453455
max_order=5, max_d=2, max_D=1, start_p=2, start_q=2,
454456
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,
484486
xreg : An optional vector or matrix of regressors, which must have one
485487
row/element for each point in x. Default is NULL, for no regressors.
486488
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.
488491
test : Test to use to determine number of first differences. Default
489492
is 'kpss', for the KPSS test. Other values are 'adf' for augmented
490493
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,
507510
'max.D' : max_D, 'start.p' : start_p, 'start.q' : start_q,
508511
'start.P' : start_P, 'start.Q' : start_Q,
509512
'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,
511514
seasonal=seasonal, ic=ic, xreg=xreg,
512515
test=test, **kwargs)
513516
h = _get_horizon(x, h)
514517
level = converters.map_arg(level)
515518
# 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)
517520
return converters.forecast_out(out, is_pandas)
518521

519522

@@ -561,7 +564,7 @@ def stlf(x, h=None, s_window=7, robust=False, lam=NULL, method='ets',
561564
kwargs = {'s.window' : s_window,
562565
'lambda' : lam}
563566
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,
565568
etsmodel=etsmodel, xreg=xreg, newxreg=newxreg, **kwargs)
566569
return converters.forecast_out(out, is_pandas)
567570

@@ -644,7 +647,7 @@ def seasadj(decomp):
644647
an object that maps an R time series of the seasonally adjusted
645648
values of the series that decomp was formed from
646649
'''
647-
return forecast.seasadj(decomp)
650+
return fc.seasadj(decomp)
648651

649652

650653
def sindexf(decomp, h):
@@ -660,7 +663,7 @@ def sindexf(decomp, h):
660663
an object that maps to am R time series containing the seasonal component
661664
of decomp, projected naively forward h steps.
662665
'''
663-
return forecast.sindexf(x, h)
666+
return fc.sindexf(x, h)
664667

665668

666669
def BoxCox(x, lam):
@@ -681,7 +684,7 @@ def BoxCox(x, lam):
681684
If x is a Pandas Series, a Pandas Series is returned.
682685
'''
683686
x, is_pandas = converters.to_ts(x)
684-
out = forecast.BoxCox(x, **{'lambda' : lam})
687+
out = fc.BoxCox(x, **{'lambda' : lam})
685688
return converters.series_out(out, is_pandas)
686689

687690

@@ -702,7 +705,7 @@ def InvBoxCox(x, lam):
702705
If x is a Pandas Series, a Pandas Series is returned.
703706
'''
704707
x, is_pandas = converters.to_ts(x)
705-
out = forecast.InvBoxCox(x, **{'lambda' : lam})
708+
out = fc.InvBoxCox(x, **{'lambda' : lam})
706709
return converters.series_out(out, is_pandas)
707710

708711

@@ -722,7 +725,7 @@ def BoxCox_lambda(x, method='guerrero', lower=-1, upper=2):
722725
value of lambda for the series x, as calculated by the selected method
723726
'''
724727
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]
726729

727730

728731
def na_interp(x, lam=NULL):
@@ -746,11 +749,11 @@ def na_interp(x, lam=NULL):
746749
In either case, missing values are filled.
747750
'''
748751
x, is_pandas = converters.to_ts(x)
749-
out = forecast.na_interp(x, **{'lambda' : lam})
752+
out = fc.na_interp(x, **{'lambda' : lam})
750753
return converters.series_out(out, is_pandas)
751754

752755

753-
def accuracy(fc, x=None, **kwargs):
756+
def accuracy(result, x=None, **kwargs):
754757
'''
755758
Computes an R matrix of forecast accuracy measures. Must take an R forecast
756759
object for input, since the residuals are not included in the Pandas
@@ -769,7 +772,7 @@ def accuracy(fc, x=None, **kwargs):
769772
* Theil's U (only if x provided)
770773
771774
Args:
772-
fc: an R forecast object
775+
result: an R forecast object
773776
x: optional R vector of true values for the forecast (test data)
774777
d: Number of first differences taken in forecast, default is none.
775778
D: Number of seasonal differences taken in forecast, default is none.
@@ -780,7 +783,7 @@ def accuracy(fc, x=None, **kwargs):
780783
'''
781784
if x is not None:
782785
kwargs['x'] = x
783-
return forecast.accuracy(fc, **kwargs)
786+
return fc.accuracy(result, **kwargs)
784787

785788

786789
def tsclean(x, **kwargs):
@@ -802,7 +805,7 @@ def tsclean(x, **kwargs):
802805
'''
803806
x, is_pandas = converters.to_ts(x)
804807
kwargs = converters.translate_kwargs(**kwargs)
805-
out = forecast.tsclean(x, **kwargs)
808+
out = fc.tsclean(x, **kwargs)
806809
return converters.series_out(out, is_pandas)
807810

808811

@@ -818,7 +821,7 @@ def findfrequency(x):
818821
The dominant frequency in x, or 1 if there isn't one.
819822
'''
820823
x, _ = converters.to_ts(x)
821-
return forecast.findfrequency(x)[0]
824+
return fc.findfrequency(x)[0]
822825

823826

824827
def ndiffs(x, **kwargs):
@@ -839,7 +842,7 @@ def ndiffs(x, **kwargs):
839842
'''
840843
x, _ = converters.to_ts(x)
841844
kwargs = converters.translate_kwargs(**kwargs)
842-
return forecast.ndiffs(x, **kwargs)[0]
845+
return fc.ndiffs(x, **kwargs)[0]
843846

844847

845848
def nsdiffs(x, **kwargs):
@@ -860,7 +863,7 @@ def nsdiffs(x, **kwargs):
860863
'''
861864
x, _ = converters.to_ts(x)
862865
kwargs = converters.translate_kwargs(**kwargs)
863-
return forecast.nsdiffs(x, **kwargs)[0]
866+
return fc.nsdiffs(x, **kwargs)[0]
864867

865868

866869
def acf(x, lag_max=NULL):
@@ -878,7 +881,7 @@ def acf(x, lag_max=NULL):
878881
'''
879882
x, is_pandas = converters.to_ts(x)
880883
kwargs = {'lag.max' : lag_max}
881-
out = forecast.Acf(x, plot=False, **kwargs)
884+
out = fc.Acf(x, plot=False, **kwargs)
882885
return converters.acf_out(out, is_pandas)
883886

884887

@@ -897,7 +900,7 @@ def pacf(x, lag_max=NULL):
897900
'''
898901
x, is_pandas = converters.to_ts(x)
899902
kwargs = {'lag.max' : lag_max}
900-
out = forecast.Pacf(x, plot=False, **kwargs)
903+
out = fc.Pacf(x, plot=False, **kwargs)
901904
return converters.acf_out(out, is_pandas)
902905

903906

test/test_all.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,13 @@ def test_rwf(self):
3838
self.assertAlmostEqual(fc.lower80[2011], 404.7558, places=3)
3939
self.assertAlmostEqual(fc.upper95[2020], 772.5385, places=3)
4040

41-
def test_forecast_ts(self):
42-
fc = wrappers.forecast_ts(self.oil)
41+
def test_forecast(self):
42+
fc = wrappers.forecast(self.oil)
4343
self.assertAlmostEqual(fc.point_fc[2011], 467.7721, places=3)
4444
self.assertAlmostEqual(fc.point_fc[2020], 467.7721, places=3)
4545
self.assertAlmostEqual(fc.lower80[2011], 405.3255, places=3)
4646
self.assertAlmostEqual(fc.upper95[2020], 769.7543, places=3)
47-
fc = wrappers.forecast_ts(self.aus)
47+
fc = wrappers.forecast(self.aus)
4848
self.assertAlmostEqual(fc.point_fc[(2011, 1)], 57.87294, places=3)
4949
self.assertAlmostEqual(fc.point_fc[(2012, 4)], 52.84327, places=3)
5050
self.assertAlmostEqual(fc.lower80[(2011, 1)], 53.30794, places=3)

0 commit comments

Comments
 (0)