Combining Rolling Origin Forecast Resampling and Group V-Fold Cross-Validation in rsample
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
When your data is both grouped and time-ordered, a single resampling strategy is usually not enough. Group v-fold protects against leakage between entities such as stores, patients, or devices, while rolling-origin style resampling respects time order inside the training data. In rsample, the practical solution is not one built-in function that does both at once, but a nested workflow: split by group first, then create time-based rolling splits inside each analysis set.
Why You Need Two Constraints
Suppose you forecast demand for many stores over time. If you use only rolling windows, the same store may appear in both training and assessment sets in a way that overstates generalization to unseen stores. If you use only group v-fold, you may accidentally train on future observations relative to the assessment period.
So the two rules are:
- groups should not leak across outer folds
- time should not leak within each training workflow
That is why nesting makes sense. The outer resampling handles groups. The inner resampling handles the forecast timeline.
Use group_vfold_cv() for the Outer Split
In rsample, the outer layer can be created with group_vfold_cv().
Each outer split keeps entire groups together. That means if store_id == "A" is in the assessment set for one fold, the training portion for that fold does not contain any rows from store A.
Create Rolling Time Splits Inside Each Analysis Set
Older rsample workflows often used rolling_origin(), but current practice is usually based on the sliding helpers. For date-based series, sliding_period() is a good fit.
This gives each outer training set its own rolling sequence of time-aware resamples.
The important detail is that analysis(.x) extracts only the training rows from the outer split. Then sliding_period() creates inner forecast-style splits without peeking forward in time.
What This Nested Design Achieves
The outer split answers: "Does the model generalize to unseen groups?"
The inner split answers: "Within the training groups, does the model generalize forward in time?"
That is a stronger evaluation than using either method alone. It is especially useful for panel forecasting problems where you want to avoid both entity leakage and temporal leakage.
Be Careful With the Time Index
This pattern assumes the time index is meaningful across the rows inside each outer analysis set. If your grouped data has separate timelines that are not aligned, you may need to create inner rolling splits per group before fitting, rather than one global sliding index across all remaining groups.
A simple way to inspect one inner split is:
That check is worth doing because nested resampling can look correct at the object level while still leaking time if the data was not sorted or indexed properly.
When You Might Flatten the Results
Some modeling workflows expect a single resample object. In that case, you may need to iterate over the nested structure manually with purrr and fit models fold by fold rather than passing one object into a high-level tuning function.
That extra plumbing is normal. The core idea is still clean: outer grouped splits, inner rolling splits.
Common Pitfalls
- Using only
group_vfold_cv()and forgetting that time leakage can still exist inside each fold. - Using only rolling-origin style splits and letting the same group appear in both training and assessment contexts.
- Applying
sliding_period()before sorting by the time index. - Assuming
rsamplehas one built-in helper that automatically composes grouped and rolling resampling. - Forgetting to inspect
analysis()andassessment()rows from the nested objects.
Summary
- Combine grouped and time-aware validation by nesting the resampling steps.
- Use
group_vfold_cv()for the outer split to prevent group leakage. - Use
sliding_period()or another sliding helper inside each outer analysis set to respect time order. - Inspect the resulting inner splits to verify that the date index behaves as expected.
- For grouped time series, nested resampling is usually the correct mental model in
rsample.

