for row in forecasts_df.to_dicts():
forecasts_list.append(row)
Print the list
for forecast in forecasts_list:
print(forecast) RemoteTraceback Traceback (most recent call last)
RemoteTraceback:
"""
Traceback (most recent call last):
File "/Users/tungnguyen/anaconda3/lib/python3.10/multiprocessing/pool.py", line 125, in worker
result = (True, func(*args, **kwds))
File "/Users/tungnguyen/anaconda3/lib/python3.10/site-packages/statsforecast/core.py", line 322, in cross_validation
raise error
File "/Users/tungnguyen/anaconda3/lib/python3.10/site-packages/statsforecast/core.py", line 319, in cross_validation
res_i = model.forecast(**forecast_kwargs)
File "/Users/tungnguyen/anaconda3/lib/python3.10/site-packages/statsforecast/models.py", line 1292, in forecast
mod = auto_theta(
File "/Users/tungnguyen/anaconda3/lib/python3.10/site-packages/statsforecast/theta.py", line 633, in auto_theta
y_decompose = seasonal_decompose(y, model=decomposition_type, period=m).seasonal
File "/Users/tungnguyen/anaconda3/lib/python3.10/site-packages/statsmodels/tsa/seasonal.py", line 171, in seasonal_decompose
raise ValueError(
ValueError: x must have 2 complete cycles requires 26 observations. x only has 22 observation(s)
"""
The above exception was the direct cause of the following exception:
ValueError Traceback (most recent call last)
Cell In[12], line 21
18 sf = StatsForecast(models=models, freq='1w', n_jobs=-1)
20 # Perform cross-validation with a step size of 1 to mimic an expanding window
---> 21 crossvalidation_df = sf.cross_validation(df=y_cl4_over_27, h=10, step_size=1, n_windows=10, sort_df=True)
23 # Calculate the ensemble mean
24 ensemble = crossvalidation_df[['AutoARIMA', 'AutoETS', 'DynamicOptimizedTheta']].mean(axis=1)
File ~/anaconda3/lib/python3.10/site-packages/statsforecast/core.py:1248, in _StatsForecast._cross_validation_parallel(self, h, test_size, step_size, input_size, fitted, level, refit, target_col)
1232 future = executor.apply_async(
1233 ga.cross_validation,
1234 (
(...)
1245 ),
1246 )
1247 futures.append(future)
-> 1248 out = [f.get() for f in futures]
1249 fcsts = [d["forecasts"] for d in out]
1250 fcsts = np.vstack(fcsts)
File ~/anaconda3/lib/python3.10/site-packages/statsforecast/core.py:1248, in (.0)
1232 future = executor.apply_async(
1233 ga.cross_validation,
1234 (
(...)
1245 ),
1246 )
1247 futures.append(future)
-> 1248 out = [f.get() for f in futures]
1249 fcsts = [d["forecasts"] for d in out]
1250 fcsts = np.vstack(fcsts)
ValueError: x must have 2 complete cycles requires 26 observations. x only has 22 observation(s) the crossvalidation is run on y_cl4_over_27 which is 28 weeks and up in series, how can I still get this error
The error message you're receiving from the StatsForecast package is due to the DynamicOptimizedTheta model, which requires at least 2 complete cycles of seasonal data to perform its seasonal decomposition.
The error states:
ValueError: x must have 2 complete cycles requires 26 observations. x only has 22 observation(s)
This implies that at least one series in y_cl4_over_27 has fewer data points than required for the DynamicOptimizedTheta model's season_length of 13. While y_cl4_over_27 might contain series with 28 weeks or more in general, there is likely at least one series with fewer than the required 26 observations (two full cycles of season_length=13) for the seasonal decomposition part of the model to work.
To resolve this issue, you have a few options:
Filter Series with Enough Data Points: Before performing the cross-validation, make sure that every unique_id in your y_cl4_over_27 DataFrame has at least 26 observations. You can do this by adjusting your group_count_sort() function and the corresponding filtering logic to ensure the condition is met.
Adjust the Season Length: If possible, revise the season_length parameter of the DynamicOptimizedTheta model to match the actual seasonality in your data or to require fewer data points for decomposition. However, this may not be appropriate if your actual seasonality is indeed 13.
Remove or Replace the Affected Model: If the DynamicOptimizedTheta model is incompatible with your data due to the seasonal constraints, consider removing this model from your ensemble or replacing it with another method that handles the available data better.
Here's how you might implement the first option, filtering series with enough data points:
# Lengths greater than double the season length for DynamicOptimizedTheta
season_length = 13
min_length_for_model = season_length * 2
lengths_over_min, counts_for_over_min = group_count_sort(y_cl4, min_length_for_model)
y_cl4_over_min = filter_and_sort(y_cl4, lengths_over_min)
# Now you can run your cross-validation and other code using y_cl4_over_min,
# as it should only contain series with enough data points.
By using the y_cl4_over_min DataFrame, which is now guaranteed to have series that meet the requirement of the DynamicOptimizedTheta model, the cross-validation call should work without triggering the ValueError. Make sure you also adjust your subsequent code to use y_cl4_over_min rather than other filtered DataFrames when doing forecasting with the model in question.