If you want to recombine the valid and train DataFrames into a single DataFrame that includes all your original data and also incorporates the MSTL decomposition for trend and seasonal components, there are a few considerations and steps you need to follow. Given that valid is a portion of the data set aside for validation and train is used for training the MSTL model, you'll typically decompose only the training data. However, if the goal is to apply MSTL decomposition to the entire dataset (both train and valid parts), you would usually perform the decomposition again on the entire dataset, instead of trying to manually merge the post-decomposition train and valid results.
Here’s a high-level approach to achieving a unified DataFrame that includes MSTL decomposition for the entire dataset:
-
Re-apply MSTL Decomposition to Entire Dataset:
First, instead of decomposing just the train dataset, apply MSTL decomposition to the entire original dataset (y_cl4_over_19 in your example). This ensures that the decomposition takes into account all data points, providing a more comprehensive view that includes both the training and validation periods.
-
MSTL Decomposition:
Assuming y_cl4_over_19 is your full dataset that you want to decompose, you would adjust the code to something like:
from statsforecast.models import MSTL
from statsforecast.feature_engineering import mstl_decomposition
# Specify parameters
freq = '1w'
season_length = 52 # Adjust according to your data's seasonality
horizon = 4 # Future periods to forecast (might not be directly relevant for just decomposition)
# Apply MSTL to the entire dataset
model = MSTL(season_length=season_length)
transformed_df, X_df = mstl_decomposition(y_cl4_over_19, model=model, freq=freq, h=horizon)
-
Combining Decomposition Results with Original Data:
The transformed_df should now contain your trend and seasonal components for the entire dataset. You can proceed to merge this with your original dataset or simply proceed with transformed_df if it includes all necessary columns. Ensure to carefully align the indices or key columns (datetime and unique_id) during any merge operation.
-
Post-processing and Validation:
After the decomposition and optional merging, validate the results by checking for any anomalies or discrepancies in the trend and seasonal components across the entire timeframe, especially around the originally divided train and valid boundary.
Remember, the MSTL decomposition process relies on the patterns and seasonality inherent in your entire dataset. Decomposing the entire dataset at once rather than in segmented portions (like just train or just valid) leads to a more cohesive analysis and is generally the recommended approach when the objective is to understand underlying trends and seasonality for all data points.