def decompose(x, type='additive'): ''' Performs a classical seasonal decomposition of a time series into season, trend and remainder components. Args: x: an R time series, obtained from converters.ts(), or a Pandas Series with the correct index (e.g. from converters.sequence_as_series(). The series should be seasonal. type: Type of seasonal decomposition to perform. Default is 'additive', other option is 'multiplicative'. Returns: If x is an R ts object, an R object of class 'decomposed.ts' is returned. If x is a Pandas Series, a Pandas Data Frame is returned. ''' x, is_pandas = converters.to_ts(x) out = stats.decompose(x, type=type) return converters.decomposition_out(out, is_pandas)
def stl(x, s_window, **kwargs): ''' Perform a decomposition of the time series x into seasonal, trend and remainder components using loess. Most of the arguments listed below are in **kwargs, and all of those arguments have sensible defaults. Usually only the mandatory s_window paramter has to be set. Args: x: an R time series, obtained from converters.ts(), or a Pandas Series with the correct index (e.g. from converters.sequence_as_series(). s_window : either 'periodic' or the span (in lags) of the loess window for seasonal extraction, which should be odd. This has no default. s_degree : Default 0, should be 0 or 1. Degree of local polynomial for seasonal extraction. t_window : The span (in lags) of the loess window for trend extraction, which should be odd. Default is a sensible, data-dependent value. See the R docs for the details. t_degree : Default 0, should be 0 or 1. Degree of local polynomial for trend extraction. l_window : Span in lags of the loess window used to low-pass filter each seasonal subseries. The default is first odd number greater than or equal to frequency, which is recommmended. s_jump, t_jump, l_jump : integer parameters (min. 1) to increase speed of each smoother by skipping data points. l_degree : Default is t.window, must be 0 or 1. Degree of local polynomial for subseries low-pass filter. robust : Default is False. If True, robust loess fitting used. inner : number of backfitting iterations outer : number of outer robustness iterations na_action : Default is na.fail, which means that the user has to fill or remove any missing values. If used, it must be an object that maps to an R function, obtained from rpy2. Returns: If x is an R ts object, an R object of class 'stl' is returned. If x is a Pandas Series, a Pandas Data Frame is returned. ''' x, is_pandas = converters.to_ts(x) kwargs['s.window'] = s_window kwargs = converters.translate_kwargs(**kwargs) out = stats.stl(x, **kwargs) return converters.decomposition_out(out, is_pandas)