Modelling running injuries with trees and neural networks
A transparent split, a smooth response, and the importance of keeping prediction separate from interpretation.
I investigated how runners’ injury counts varied with changes in their training distance and the terrain they used. The dataset offered a useful comparison between two very different modelling approaches: a regression tree that expresses its prediction through a few explicit decisions, and a small neural network that can represent a smoother relationship.
Both fitted models associated higher values of the distance-change measure with higher injury counts, but only the neural-network responses differed by terrain.
Looking at the measurements first
The response was InjuryCount. The predictors were CompoundDist, the largest two-week average weekly mileage increase, and Terrain, with categories Both, RoadMostly and TrailMostly.
The scatter plot suggested a positive association between CompoundDist and injury count. It did not show a clean separation by terrain. That made it useful to examine the terrain distributions directly rather than trying to infer everything from the coloured points.
The trail group had a higher median and a wider upper range in the original box plot. Those descriptive differences motivated including terrain in the models; they did not establish that terrain caused the observed injuries.
A regression tree with an interpretable decision
I first fitted a regression tree using both predictors. The initial fit allowed relatively small nodes, with minsplit = 5 and a complexity parameter of 0.01.
library(rpart)
library(rpart.plot)
set.seed(1)
model = rpart(InjuryCount ~ CompoundDist + Terrain,
control = rpart.control(minsplit = 5, cp = 0.01))
plotcp(model)
abline(v = 2, lty = 2, lwd = 1.5)
model_pruned = prune.rpart(model, cp = 0.15)
rpart.plot(model_pruned)
I used the one-standard-deviation rule on the cross-validation plot and pruned at cp = 0.15, leaving one split.
The root node’s displayed mean injury count was 1.6. Approximately 78% of observations fell below the distance-change threshold and had a terminal-node prediction of 1.1; the remaining 22% had a prediction of 3.4.
Expressing the displayed tree mathematically, its fitted response is approximately
\[ \widehat{\mathrm{InjuryCount}}(x)= \begin{cases} 1.1,&x<0.054,\\ 3.4,&x\geq0.054. \end{cases} \]
The displayed node values are rounded. The threshold corresponds to an increase of approximately 5.4% in the distance-change measure.
What happened to terrain?
Terrain was available to the fitting procedure but did not survive pruning. The selected tree therefore gives the same prediction for all three terrain categories at a given value of CompoundDist.
A small neural network
The second approach used a neural network with hidden layers of five and two units. I encoded the terrain categories as indicator columns alongside CompoundDist, used a hyperbolic-tangent hidden activation, and fitted with a squared-error objective.
The original preparation split the observations into training and validation sets of approximately equal size:
set.seed(2025)
X = model.matrix(InjuryCount ~ CompoundDist + Terrain - 1,
data = dat)
Y = cbind(dat$InjuryCount)
N = nrow(X)
ss = sample(1:N, floor(0.5 * N), replace = FALSE)
Xtrain = X[ss, , drop = FALSE]
Ytrain = Y[ss, , drop = FALSE]
Xval = X[-ss, , drop = FALSE]
Yval = Y[-ss, , drop = FALSE]The fitting settings in the report were:
| Setting | Value |
|---|---|
| Hidden-layer widths | 5, 2 |
| Hidden activation | tanh |
| Objective | squared error |
| Learning rate | 0.01 |
| Optimisation iterations | 15,000 |
| Initial regularisation strength | 0 |
| Network initialisation seed | 1234 |
The network was fitted with the course’s babynets implementation:
library(babynets)
training_model = babynet.fit(Xtrain, Ytrain,
h_nodes = c(5, 2), lam_learn = 0.01, iterations = 15000,
nu_reg = 0, reg = c('L2', 'L1'), h_activation = c('tanh'),
objective = c('squared_error'), seed = 1234)The report uses its optimisation history to examine whether the fit had settled before interpreting its predictions.
Checking the optimisation
The objective declined and then flattened. A flat objective alone is not a complete convergence check, so I also examined the gradients.
The maximum absolute gradient fell below the displayed threshold and the final parameter gradients lay within the displayed bounds. The report took these diagnostics as evidence that this run appeared to have converged.
Selecting regularisation on validation data
I next varied the L2 regularisation strength rather than treating the unregularised model as the final choice. The candidate values were equally spaced on a logarithmic scale:
nu_vals = exp(seq(-5, -2, length = 10))For each candidate, the report refitted the same network configuration on the training set and evaluated the validation objective using babynet.predict. The validation calculation retained the returned obj value:
val_error = rep(NA, length(nu_vals))
for (i in 1:length(nu_vals)) {
regularised_model = babynet.fit(Xtrain, Ytrain,
h_nodes = c(5, 2), lam_learn = 0.01, iterations = 15000,
nu_reg = nu_vals[i], reg = c('L2'), h_activation = c('tanh'),
objective = c('squared_error'), seed = 1234)
res_val = babynet.predict(Xval, Yval, model = regularised_model)
val_error[i] = res_val$obj
}The candidate fits used the same hidden-layer widths, activation, learning rate and iteration budget.
The selected strength was
\[ \nu=0.02556153\approx0.0256. \]
I used that value to refit the regularised model on the training split.
Reading the fitted responses
To examine the fitted network, I evaluated it over a grid of 1,000 values of CompoundDist, holding each terrain category fixed in turn. The original indicator construction was:
final_model = babynet.fit(Xtrain, Ytrain,
h_nodes = c(5, 2), lam_learn = 0.01, iterations = 15000,
nu_reg = 0.02556153, reg = c('L2'), h_activation = c('tanh'),
objective = c('squared_error'), seed = 1234)
compound_dist = seq(min(CompoundDist), max(CompoundDist), length = 1000)
XX_both = cbind(compound_dist, 1, 0, 0)
XX_road = cbind(compound_dist, 0, 1, 0)
XX_trail = cbind(compound_dist, 0, 0, 1)
res_pred_both = babynet.predict(XX_both, model = final_model)
res_pred_road = babynet.predict(XX_road, model = final_model)
res_pred_trail = babynet.predict(XX_trail, model = final_model)These columns correspond to the fitted design matrix’s ordering: the distance measure followed by the three terrain indicators. The rows are input combinations for examining the fitted function, not additional observed runners.
The response increased with CompoundDist for all three terrain categories. In the reported curves, the trail prediction was highest, the road prediction lowest, and mixed terrain lay between them.
The contrast with the tree is informative. The tree represents the relationship with a single abrupt change and no retained terrain distinction. The network represents it with smooth curves and a terrain-dependent response. Neither visual form, by itself, establishes that its predictions are more accurate.
What I could conclude
The exploratory plots and both fitted approaches supported a positive relationship between the distance-change measure and injury count in this dataset. The selected models expressed that relationship differently, particularly in their treatment of terrain.
The models were not compared on an independent test set. Their relative predictive accuracy remains an open question.
Originally completed as part of STA3047S · Introduction to Machine Learning, University of Cape Town, 2026.