Tutorials › Regression Analysis (Undergraduate) › Predicting Bike-Trip Duration from Route Conditions

Integrated Applied Case Studies · Tutorial 990 of 991

Predicting Bike-Trip Duration from Route Conditions

Learn to develop a route-based duration model, check for nonlinear and interaction patterns, and report prediction accuracy on trips not used to fit it.

Advanced 10 min read

What You'll Learn

  • Define a bike-trip duration model using distance, elevation gain, surface type, curvature, and an interaction.
  • Interpret the model’s terms in context, including how the distance relationship changes with route conditions.
  • Check whether a more flexible model is supported by patterns in the training data.
  • Calculate and interpret held-out mean absolute error and root mean squared error.
  • Describe the data conditions and route ranges to which the model’s predictions apply.

From Route Information to a Useful Prediction

A bike-trip duration model can help a rider compare possible routes or help a service estimate arrival times. But a good fit to the trips used to build a model does not, by itself, show that the model will predict new trips well. A useful analysis connects route characteristics to duration, checks whether the chosen relationship is plausible, and evaluates prediction errors on trips kept separate from model fitting.

The example throughout this tutorial is hypothetical. Suppose trip records include duration in minutes, distance in kilometres, elevation gain in hundreds of metres, and whether the route is gravel. The goal is to predict duration for trips in a defined service area, not to claim that changing a route characteristic causes a particular change in riding time.

Definition: A training set is the set of observations used to choose and fit a model. A held-out test set is a separate set of observations not used to fit the model or make its modeling choices; it is used to assess predictions. A prediction error for a held-out trip is its observed duration minus its predicted duration.

The split must match the intended use. Here, suppose the aim is to predict future trips in the same service area and season. The hypothetical data are split by trip, with each trip appearing only once; test trips are not used to select terms or tune the model. Their distances and route characteristics fall within the training data’s ranges. These conditions make the test set relevant to the stated aim, although they do not establish accuracy for other cities, seasons, or kinds of routes. If the intended task were prediction on entirely new routes, trips on the same route should not be divided between training and test sets.

A Model for Distance, Climbing, and Surface

Let \(T\) denote trip duration in minutes, \(D\) distance in kilometres, \(E\) elevation gain in units of 100 metres, and \(R\) an indicator equal to 1 for a gravel route and 0 otherwise. One possible model allows duration to curve with distance and allows the distance relationship to differ for gravel routes:

$$ T_i=\beta_0+\beta_1D_i+\beta_2E_i+\beta_3R_i+\beta_4D_i^2+\beta_5(D_iR_i)+\varepsilon_i $$

The subscript \(i\) identifies a trip. The \(\beta\) terms are coefficients to be estimated from the training data, and \(\varepsilon_i\) represents the difference between the model’s mean duration and the observed duration for trip \(i\). The fitted prediction replaces the unknown coefficients with their estimates and does not include a new random error term.

This model treats elevation gain as having a constant association with duration, while allowing the association with distance to change gradually and to differ by surface. Those are modeling choices, not facts guaranteed by the data. They should be considered only when the training data contain enough variation in route lengths, climbing, and surface type to distinguish the terms.

Model conditions: Interpret the fitted relationship over route conditions represented by the training data. Check that the recorded durations and route characteristics are measured consistently, the observations represent the prediction task, and no information from a test trip has entered fitting or model selection. Use residual plots to look for systematic patterns that the fitted mean relationship misses. Dependence between trips on the same route or day can also make a random trip-level split an inadequate check for predictions on genuinely new routes or days.

Worked Example: Predicting One Hypothetical Trip

Suppose fitting the model to the training set gives the following equation, with duration measured in minutes:

$$ \widehat{T}=2+3.0D+1.4E+1.8R+0.08D^2+0.35DR $$

These hypothetical estimates are used only for trips within the training data’s route-condition ranges. A gravel trip is 8 km long and gains 300 m, so \(D=8\), \(E=3\), and \(R=1\). Substituting each value gives:

$$ \begin{aligned} \widehat{T} &=2+3.0(8)+1.4(3)+1.8(1)+0.08(8^2)+0.35(8)(1)\\ &=2+24+4.2+1.8+5.12+2.8\\ &=39.92\text{ minutes}. \end{aligned} $$

The model predicts about 39.9 minutes for this hypothetical trip. This is a prediction for a trip with the stated characteristics, not a guarantee that the actual ride will take that long. Traffic, stops, rider pace, weather, and other unrecorded factors can make the observed duration different.

Before using this prediction, we would check that an 8 km gravel trip with 300 m of climbing is represented by the training data and that the model’s training residuals do not show a conspicuous pattern at similar route conditions. If such trips are outside the observed range, the calculation is an extrapolation and should not be presented as a validated prediction.

Reading Curvature and Interaction Terms

The squared-distance term \(D^2\) permits the predicted change per additional kilometre to vary with trip length. The interaction \(DR\) permits that distance relationship to vary by surface. These terms are useful only when they address patterns that matter for prediction and are supported by the available data.

For the fitted equation above, compare two routes at the same elevation gain. On a paved route (\(R=0\)), the distance-related part of the prediction is \(3.0D+0.08D^2\). On a gravel route (\(R=1\)), it is \(3.0D+0.08D^2+0.35D\). The extra contribution associated with gravel therefore depends on distance; it is not a single fixed difference for every route length.

Worked Example: Interpreting Curvature and the Interaction

Using the hypothetical fitted model, compare predicted durations for paved and gravel routes at 5 km and 10 km, holding elevation gain fixed. The surface-related difference is the gravel prediction minus the paved prediction at the same distance and elevation.

At 5 km, the distance-related contribution for a paved route is:

$$ 3.0(5)+0.08(5^2)=15+2=17\text{ minutes}. $$

For gravel, the interaction adds \(0.35(5)=1.75\) minutes to that contribution. Including the gravel main-effect term, the predicted gravel-versus-paved difference is:

$$ 1.8+0.35(5)=3.55\text{ minutes}. $$

At 10 km, the distance-related contribution for pavement is:

$$ 3.0(10)+0.08(10^2)=30+8=38\text{ minutes}, $$

and the gravel-versus-paved difference is \(1.8+0.35(10)=5.3\) minutes. Thus the fitted model predicts a larger gravel-associated difference for the 10 km route than for the 5 km route, at the same elevation gain. This is an association encoded in the fitted model; it does not establish a causal effect of changing a route’s surface.

Curvature also changes the distance contribution as routes get longer. For pavement, the distance contribution rises from 17 minutes at 5 km to 38 minutes at 10 km, an increase of 21 minutes over that five-kilometre difference. This model’s quadratic term represents a changing distance relationship; it should not be taken to imply that duration will keep increasing at that same pattern beyond the distances observed in training.

In practice, inspect plots that compare observed duration with distance and fitted values, and inspect residuals against distance, fitted values, elevation gain, and surface. A systematic bend in residuals can suggest that a straight-line distance term is inadequate. A different distance pattern by surface can motivate examining an interaction. These plots guide model checking; they do not, on their own, prove that a more complex model will predict better.

When deciding whether to keep added terms, compare plausible candidate models using training data for fitting and selection, then evaluate the chosen model on the untouched test set. Do not repeatedly adjust the model in response to test-set errors and continue to describe that same set as an independent test: once it guides model choices, it is no longer an untouched assessment set.

Measuring Accuracy on Held-Out Trips

For held-out trip \(i\), define the prediction error as \(e_i=T_i-\widehat{T}_i\), where \(T_i\) is observed duration and \(\widehat{T}_i\) is predicted duration. A positive error means the model underpredicted the trip’s duration; a negative error means it overpredicted.

Two useful summaries are the mean absolute error (MAE) and root mean squared error (RMSE). Both are measured in minutes. MAE averages the sizes of the errors, while RMSE gives relatively more weight to large errors because it squares them before averaging.

$$ \text{MAE}=\frac{1}{n}\sum_{i=1}^{n}|e_i|, \qquad \text{RMSE}=\sqrt{\frac{1}{n}\sum_{i=1}^{n}e_i^2} $$

Here, \(n\) is the number of held-out trips. These are descriptive measures of prediction error on this test set, not guarantees about future trips. Reporting them with the test-set size and the trips’ context helps readers judge how far the results can reasonably be generalized.

Worked Example: Calculating Held-Out Prediction Accuracy

Suppose the hypothetical test set contains five trips, and the equation from the first example produces the predictions below. The trips were kept out of fitting and model selection; their route conditions are within the training ranges and they represent the same service area and season. Calculate each error as observed minus predicted.

TripObserved duration (min)Predicted duration (min)Error (min)
118.0016.681.32
232.0029.582.42
342.0045.60-3.60
412.0014.57-2.57
548.0042.725.28

The absolute errors sum to \(1.32+2.42+3.60+2.57+5.28=15.19\) minutes. Therefore:

$$ \text{MAE}=\frac{15.19}{5}=3.038\text{ minutes}. $$

For RMSE, square the errors. Their squared values are \(1.7424\), \(5.8564\), \(12.9600\), \(6.6049\), and \(27.8784\), which sum to \(55.0421\). Thus:

$$ \text{RMSE}=\sqrt{\frac{55.0421}{5}} =\sqrt{11.00842} \approx3.318\text{ minutes}. $$

On these five hypothetical held-out trips, the average absolute prediction error is about 3.04 minutes, and the RMSE is about 3.32 minutes. The largest absolute error is 5.28 minutes, so the averages do not mean that every trip’s error is near three minutes. With only five test trips, these results are also imprecise as a guide to performance on a broader population of trips.

Communicating the Model’s Limits

A useful report states what the model predicts, how it was evaluated, and what the evaluation does not cover. For this example, a concise account would identify the outcome as trip duration in minutes; name distance, elevation gain, surface, curvature, and the distance-by-surface interaction as predictors; and report that five held-out trips had an MAE of 3.04 minutes and an RMSE of 3.32 minutes. It would also say that the test trips came from the same service area and season and had route conditions within the training range.

The reported errors describe the performance of this fitted model on those held-out trips. They do not show that every future prediction will be within 3.04 minutes, nor do they establish performance on longer routes, different weather, other regions, or entirely new routes if the test set did not represent them. Even when average errors are small, individual trips can be substantially over- or underpredicted.

Common Mistake: Treating a Test Set as a Tuning Set

  • Wrong reasoning: “The test-set RMSE was disappointing, so I added terms and kept adjusting them until the same test-set RMSE improved. The final RMSE is still an independent test of performance.”
  • Correction: Once test-set results guide model choices, the test set has influenced the fitted procedure and no longer provides an untouched assessment. Use training data for fitting and selection, and reserve a separate test set for final evaluation. Explain which trips and route conditions the test set represents.
  • Another limitation: A random split by trip can put trips from the same route in both sets. That can be appropriate for predicting further trips on familiar routes, but it may give an overly optimistic assessment for entirely new routes. Choose the split to match the intended prediction task.
Key takeaway: A credible duration prediction combines a clearly specified model, checks for missed patterns, and an evaluation on data not used to choose the model. Report held-out accuracy in minutes alongside the size and scope of the evaluation set.

Check Your Understanding

Try these questions before reviewing the examples.

  1. In the model, what does \(R=1\) represent, and why does the interaction term mean the gravel-associated difference can depend on distance?
  2. Why should an 8 km route be treated cautiously if the training data contain no routes longer than 6 km?
  3. If a held-out trip has observed duration 25 minutes and predicted duration 28 minutes, what is its prediction error, and does the model underpredict or overpredict?
  4. Why can RMSE be larger than MAE for the same set of prediction errors?
  5. If the intended use is prediction on new routes, why might splitting individual trips at random give an unrepresentative accuracy assessment?