Skip to main content

Support Vector Regression: How Loud Is This Wing?

Support vector regression in R with e1071, on NASA’s wind tunnel measurements of airfoil noise: the epsilon-insensitive loss, cost, the kernel trick and support vectors by hand, tuning cost, gamma and epsilon by cross-validation, why splitting rows at random is a mistake when they come from the same experiment, a comparison with linear regression, a GAM and a random forest, partial dependence, scaling, outliers and APA reporting.

The question: noise from the edge of a wing

When air flows over the blade of a fan, a wind turbine or a wing, it makes noise, even if nothing vibrates. In the 1980s NASA measured it in a wind tunnel: blades of different sizes, at different speeds and angles, and the sound level at many frequencies (Brooks et al., 1989). Can we predict the noise of a blade that was not measured?

The relation is smooth, and complicated: the noise has a peak at some frequency, and the peak moves with the size, the speed and the angle. A linear regression cannot follow it. Support vector regression (SVR; Cortes & Vapnik, 1995; Smola & Schölkopf, 2004) is one of the classic machine-learning methods for such problems. It has two ideas:

  1. Errors below some size do not count. Only the points that lie outside a tube around the curve have a say.
  2. Curves from distances. The prediction for a new case is a weighted sum of its similarities to some of the old ones.

This page also shows something that has nothing to do with SVR and decides whether any of the results are true: how the test data are separated from the training data.

Code
library(e1071)      # svm()
library(mgcv)       # gam(), for comparison (comes with R)
library(ranger)     # random forest, for comparison
library(ggplot2)

# number formats for the text
f1   <- function(x) formatC(x, format = "f", digits = 1, big.mark = ",")
f2   <- function(x) formatC(x, format = "f", digits = 2, big.mark = ",")
num  <- function(x) trimws(format(x, big.mark = ","))
pct  <- function(x, d = 0) paste0(formatC(100 * x, format = "f", digits = d), "%")
rmse <- function(a, b) sqrt(mean((a - b)^2))

1. The data

Code
zip <- tempfile(fileext = ".zip")
download.file(
  "https://archive.ics.uci.edu/static/public/291/airfoil+self+noise.zip",
  zip,
  mode = "wb",
  quiet = TRUE
)
wing <- read.table(
  unz(zip, "airfoil_self_noise.dat"),
  col.names = c("freq", "angle", "chord", "speed", "thick", "db")
)
wing$lf <- log10(wing$freq) # frequency in factors of ten
wing$lt  <- log10(wing$thick)
# one experiment: the same blade, speed and angle
wing$run <- paste(wing$angle, wing$chord, wing$speed)
c(rows = nrow(wing), experiments = length(unique(wing$run)))
       rows experiments 
       1503         106 
Code
summary(wing$db)
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
  103.4   120.2   125.7   124.8   130.0   141.0 

The file is in the UCI machine learning repository. db is the sound level in decibels; the predictors are the frequency (log, lf), the angle of the blade (degrees), its length (chord, meters), the speed of the air (m/s) and the thickness of the boundary layer at the edge (log, lt), which is a property of the flow.

The 1,503 rows are not 1,503 experiments. Each experiment, one blade at one speed and angle, gave a whole spectrum of about 14 frequencies. There are 106 experiments.

Split by experiment, not by row

If we put single rows at random into the training and the test set, then for nearly every test row its neighbors in the same spectrum are in the training set. A method that only connects the dots would look brilliant. But the question is about blades that were not measured. So whole experiments go to one side or the other:

Code
set.seed(151)
runs  <- unique(wing$run)
test_runs <- sample(runs, round(0.25 * length(runs)))
train <- subset(wing, !(run %in% test_runs))
test  <- subset(wing, run %in% test_runs)
c(
  train_rows = nrow(train),
  test_rows = nrow(test),
  train_experiments = length(unique(train$run)),
  test_experiments = length(unique(test$run))
)
       train_rows         test_rows train_experiments  test_experiments 
             1127               376                80                26 

2. Look at the data

Code
set.seed(152)
shown <- subset(wing, run %in% sample(runs, 9))
ggplot(shown, aes(freq, db)) +
  geom_line(colour = "grey60") +
  geom_point(size = 1.3, colour = "#2a78d6") +
  facet_wrap(~run) +
  scale_x_log10(breaks = c(500, 2000, 8000)) +
  labs(
    title = "Nine experiments (angle, chord, speed)",
    x = "frequency (Hz, log scale)",
    y = "sound level (dB)"
  ) +
  theme_bw()

Plot: 2. Look at the data

Each spectrum is smooth, with one hump. The position and the height of the hump change from experiment to experiment.


3. The method step by step, with formulas

Step 1: a tube instead of a line

Least squares punishes every error, by its square. SVR does not punish errors smaller than \(\varepsilon\) at all, and larger ones only in proportion:

\[L_\varepsilon(e) = \max(0,\; |e| - \varepsilon)\]

Code
e <- seq(-3, 3, by = 0.01)
losses <- rbind(
  data.frame(e, loss = e^2, kind = "squared (least squares)"),
  data.frame(e, loss = abs(e), kind = "absolute (median regression)"),
  data.frame(e, loss = pmax(0, abs(e) - 1), kind = "ε-insensitive, ε = 1 (SVR)")
)
ggplot(losses, aes(e, loss, colour = kind)) +
  geom_line(linewidth = 1) +
  coord_cartesian(ylim = c(0, 4)) +
  scale_colour_manual(values = c("#1baf7a", "#eb6834", "#2a78d6"), name = NULL) +
  labs(title = "Three ways to count an error", x = "error", y = "loss") +
  theme_bw() +
  theme(legend.position = "bottom")

Loss chart – Step 1: a tube instead of a line

Step 2: as flat as possible

For a linear function \(f(x) = w^\top x + b\), SVR minimizes

\[\frac{1}{2}\lVert w \rVert^2 + C \sum_i L_\varepsilon\big(y_i - f(x_i)\big)\]

The first part asks for a flat function (a penalty like that of ridge regression), the second for small errors outside the tube. The cost \(C\) is the exchange rate: a large \(C\) follows the data, a small \(C\) stays flat.

Step 3: support vectors

The solution can be written with one weight per training case:

\[f(x) = \sum_i \alpha_i\, (x_i^\top x) + b, \qquad -C \le \alpha_i \le C\]

Cases strictly inside the tube have \(\alpha_i = 0\): they could be deleted without changing anything. The others are the support vectors. In the formula the predictors appear only in products \(x_i^\top x\) of two cases.

Step 4: the kernel

So replace the product by any other measure of similarity, a kernel \(K(x_i, x)\). The most used is the radial one,

\[K(x_i, x) = \exp\!\left(-\gamma\, \lVert x_i - x \rVert^2\right)\]

which is 1 for identical cases and falls to 0 with the distance. Now \(f\) is a sum of bumps centered at the support vectors, and can take any smooth shape, without ever naming a formula. \(\gamma\) decides how wide the bumps are: a small \(\gamma\) gives a stiff function, a large one a wiggly function.

Step 5: check the formula by hand

Code
small <- svm(
  db ~ lf + angle + chord + speed + lt,
  data = train,
  cost = 10,
  gamma = 0.2,
  epsilon = 0.1
) # predictors and outcome are standardized inside
vars  <- c("lf", "angle", "chord", "speed", "lt")
Zt <- scale(
  as.matrix(test[, vars]),
  center = small$x.scale$`scaled:center`,
  scale = small$x.scale$`scaled:scale`
)
SV <- small$SV # the support vectors, standardized
# exp(-gamma * squared distance)
K <- exp(-0.2 * outer(rowSums(Zt^2), rowSums(SV^2), "+") + 2 * 0.2 * Zt %*% t(SV))
f_std <- drop(K %*% small$coefs) - small$rho
by_hand <- f_std * small$y.scale$`scaled:scale` + small$y.scale$`scaled:center`
max(abs(by_hand - predict(small, test)))
[1] 2.444267e-12
Code
c(support_vectors = small$tot.nSV, training_rows = nrow(train))
support_vectors   training_rows 
            684            1127 

The prediction is what step 4 says. 684 of the 1,127 training rows are support vectors.


4. Tuning: cost, gamma, epsilon

The three settings are chosen by cross-validation in the training set, again by experiment:

Code
set.seed(153)
train_runs <- unique(train$run)
fold_of <- setNames(sample(rep(1:5, length.out = length(train_runs))), train_runs)
fold <- fold_of[train$run]
form <- db ~ lf + angle + chord + speed + lt
cv_error <- function(cost, gamma, epsilon, folds = fold) {
  err <- unlist(lapply(1:5, function(k) {
    m <- svm(form, data = train[folds != k, ], cost = cost, gamma = gamma, epsilon = epsilon)
    predict(m, train[folds == k, ]) - train$db[folds == k]
  }))
  sqrt(mean(err^2))
}
grid <- expand.grid(cost = c(0.5, 2, 10, 50, 200), gamma = c(0.02, 0.05, 0.1, 0.2, 0.5, 1))
grid$rmse <- mapply(cv_error, grid$cost, grid$gamma, 0.1)
best <- grid[which.min(grid$rmse), ]
eps  <- c(0.01, 0.05, 0.1, 0.2, 0.4)
eps_rmse <- sapply(eps, function(e) cv_error(best$cost, best$gamma, e))
best_eps <- eps[which.min(eps_rmse)]
best; rbind(epsilon = eps, rmse = eps_rmse)
   cost gamma     rmse
13   10   0.1 2.858887
            [,1]     [,2]     [,3]     [,4]     [,5]
epsilon 0.010000 0.050000 0.100000 0.200000 0.400000
rmse    2.914118 2.918295 2.858887 2.929316 3.016123
Code
ggplot(grid, aes(factor(gamma), factor(cost), fill = rmse)) +
  geom_tile(colour = "white") +
  geom_text(aes(label = f2(rmse)), size = 3.4) +
  scale_fill_gradient(low = "#1baf7a", high = "white", name = "dB") +
  labs(title = "Cross-validated error, whole experiments left out", x = "γ", y = "cost C") +
  theme_minimal() +
  theme(panel.grid = element_blank())

4. Tuning: cost, gamma, epsilon

The best setting is \(C\) = 10, \(\gamma\) = 0.1 (error 2.86 dB), a fairly stiff function. Large \(\gamma\) with a large \(C\), the most flexible corner, is bad. \(\varepsilon\) matters little. (\(\varepsilon\) is in standard deviations of the outcome: 0.1 is 0.7 dB.)

The same tuning with rows at random

Code
set.seed(154)
random_fold <- sample(rep(1:5, length.out = nrow(train)))
grid$rmse_random <- mapply(
  cv_error,
  grid$cost,
  grid$gamma,
  0.1,
  MoreArgs = list(folds = random_fold)
)
best_random <- grid[which.min(grid$rmse_random), ]
best_random
   cost gamma     rmse rmse_random
29   50     1 3.243006    1.431678

If single rows are left out, the cross-validation promises an error of 1.43 dB, and chooses \(C\) = 50 and \(\gamma\) = 1: the most flexible model. Whose promise is kept?


5. The test

Code
svr      <- svm(form, data = train, cost = best$cost, gamma = best$gamma, epsilon = best_eps)
svr_rand <- svm(
  form,
  data = train,
  cost = best_random$cost,
  gamma = best_random$gamma,
  epsilon = 0.1
)
linear   <- lm(form, data = train)
poly2    <- lm(db ~ polym(lf, angle, chord, speed, lt, degree = 2, raw = TRUE), data = train)
gam_fit <- gam(
  db ~ te(lf, lt, k = 6) + s(angle, k = 6) + s(chord, k = 5) + speed,
  data = train,
  method = "REML"
)
set.seed(155)
forest   <- ranger(form, data = train, num.trees = 1000)
preds <- list(
  `Linear regression` = predict(linear, test),
  `Quadratic regression` = predict(poly2, test),
  `GAM` = predict(gam_fit, test),
  `Random forest` = predict(forest, test)$predictions,
  `SVR, tuned by experiment` = predict(svr, test),
  `SVR, tuned by rows at random` = predict(svr_rand, test)
)
results <- data.frame(
  model = names(preds),
  test_rmse = sapply(preds, rmse, b = test$db),
  R2 = sapply(preds, function(p) 1 - sum((test$db - p)^2) / sum((test$db - mean(train$db))^2)),
  row.names = NULL
)
# the uncertainty comes from the experiments: bootstrap whole experiments of the test set
set.seed(156)
boot <- replicate(1000, {
  pick <- sample(test_runs, replace = TRUE)
  rows <- unlist(lapply(pick, function(r) which(test$run == r)))
  sapply(preds, function(p) rmse(p[rows], test$db[rows]))
})
results$low <- apply(boot, 1, quantile, 0.025); results$high <- apply(boot, 1, quantile, 0.975)
print(
  transform(
    results,
    test_rmse = round(test_rmse, 2),
    R2 = round(R2, 2),
    low = round(low, 2),
    high = round(high, 2)
  ),
  row.names = FALSE
)
                        model test_rmse   R2  low high
            Linear regression      5.16 0.35 4.37 5.98
         Quadratic regression      3.37 0.72 2.89 3.82
                          GAM      3.83 0.64 3.03 4.52
                Random forest      2.75 0.82 2.31 3.18
     SVR, tuned by experiment      2.60 0.84 2.30 2.87
 SVR, tuned by rows at random      3.51 0.70 2.73 4.23
Code
get <- function(m, col = "test_rmse") results[results$model == m, col]
Code
results$model <- factor(results$model, levels = results$model[order(-results$test_rmse)])
ggplot(results, aes(test_rmse, model)) +
  geom_pointrange(aes(xmin = low, xmax = high), colour = "#2a78d6", linewidth = 0.9) +
  geom_vline(xintercept = best_random$rmse_random, linetype = "dashed", colour = "#eb6834") +
  labs(
    title = "Error on experiments that no model has seen",
    subtitle = "95% bootstrap intervals over experiments. Dashed: what tuning by rows at random promised",
    x = "root mean squared error (dB)",
    y = NULL
  ) +
  theme_bw() +
  theme(plot.title.position = "plot")

5. The test

  • The SVR tuned by experiment misses new experiments by 2.60 dB, linear regression by 5.16.
  • The SVR tuned by rows had promised 1.43 and delivers 3.51.
  • The quadratic regression has 3.37, the GAM 3.83, and the random forest 2.75. The test set has 26 experiments, the intervals are wide and overlap, and the ranking of the good models is not certain.

With random splits, errors of 1 to 2 dB are easy to get on these data. They answer a question that nobody has: predicting a frequency of a spectrum of which the neighboring frequencies are known.

Code
few <- test_runs[1:6]
show <- subset(test, run %in% few)
show$SVR <- predict(svr, show); show$linear <- predict(linear, show)
long <- rbind(
  data.frame(show[, c("run", "freq")], db = show$SVR, what = "SVR"),
  data.frame(show[, c("run", "freq")], db = show$linear, what = "linear regression")
)
ggplot(show, aes(freq, db)) +
  geom_point(size = 1.4, colour = "grey30") +
  geom_line(data = long, aes(colour = what), linewidth = 0.8) +
  facet_wrap(~run) +
  scale_colour_manual(values = c(SVR = "#2a78d6", `linear regression` = "#eb6834"), name = NULL) +
  scale_x_log10(breaks = c(500, 2000, 8000)) +
  labs(
    title = "Six experiments of the test set: measured (points) and predicted",
    x = "frequency (Hz, log scale)",
    y = "dB"
  ) +
  theme_bw() +
  theme(legend.position = "bottom")

Spectra chart – 5. The test


6. What has the model learned?

SVR has no coefficients to read. Partial dependence shows the average prediction as one predictor changes:

Code
pd <- function(v, values) {
  sapply(values, function(x) {
    z <- train
    z[[v]] <- x
    mean(predict(svr, z))
  })
}
pd_data <- do.call(
  rbind,
  lapply(
    list(
      c("lf", "log10 frequency"),
      c("angle", "angle (degrees)"),
      c("chord", "chord (m)"),
      c("speed", "speed (m/s)")
    ),
    function(p) {
      values <- seq(min(train[[p[1]]]), max(train[[p[1]]]), length.out = 25)
      data.frame(x = values, db = pd(p[1], values), what = p[2])
    }
  )
)
pd_data$what <- factor(pd_data$what, levels = unique(pd_data$what))
ggplot(pd_data, aes(x, db)) +
  geom_line(colour = "#2a78d6", linewidth = 1) +
  facet_wrap(~what, scales = "free_x") +
  labs(title = "Partial dependence of the sound level", x = NULL, y = "predicted dB") +
  theme_bw()

Partial – 6. What has the model learned?

Code
range_of <- tapply(pd_data$db, pd_data$what, function(v) diff(range(v)))
round(range_of, 1)
log10 frequency angle (degrees)       chord (m)     speed (m/s) 
           20.3             7.0             8.2             3.0 

The frequency changes the prediction most (by 20.3 dB over its range), then the length of the blade (8.2 dB) and the angle (7.0), and the speed least (3.0). The peak that moves with the conditions is an interaction, which such averages hide.


7. Scaling, outliers, size

Code
noscale <- svm(
  form,
  data = train,
  cost = best$cost,
  gamma = best$gamma,
  epsilon = best_eps,
  scale = FALSE
)
set.seed(157)
spoiled <- train
bad <- sample(nrow(spoiled), 20)
spoiled$db[bad] <- spoiled$db[bad] + 60 # 20 wrong measurements
out_svr <- svm(form, data = spoiled, cost = best$cost, gamma = best$gamma, epsilon = best_eps)
out_gam <- update(gam_fit, data = spoiled)
c(
  SVR = get("SVR, tuned by experiment"),
  SVR_unscaled = rmse(predict(noscale, test), test$db),
  SVR_with_outliers = rmse(predict(out_svr, test), test$db),
  GAM = get("GAM"),
  GAM_with_outliers = rmse(predict(out_gam, test), test$db)
)
              SVR      SVR_unscaled SVR_with_outliers               GAM GAM_with_outliers 
         2.602715          5.711961          2.543278          3.833790          4.111864 
Code
sv_share <- sapply(c(0.01, 0.1, 0.4, 0.8), function(e) {
  svm(form, data = train, cost = best$cost, gamma = best$gamma, epsilon = e)$tot.nSV / nrow(train)
})
setNames(round(sv_share, 2), paste("epsilon", c(0.01, 0.1, 0.4, 0.8)))
epsilon 0.01  epsilon 0.1  epsilon 0.4  epsilon 0.8 
        0.97         0.66         0.18         0.06 
  • Scale the predictors. The kernel adds squared distances. Without scaling, degrees of angle count thousands of times more than meters of chord, and the error rises to 5.7 dB. svm() scales by default; the same center and scale must be used for new data, as predict() does.
  • Outliers count in proportion and not by their square. Twenty wrong values moved the error of the SVR to 2.54 and that of the GAM to 4.11 dB.
  • \(\varepsilon\) decides how many support vectors there are: 97% of the rows with 0.01, 18% with 0.4. Fewer support vectors make a smaller and faster model.

8. Things to keep in mind

  • Keep together what belongs together: experiments, patients, schools, days. In the split and in the tuning.
  • Tune on a grid of factors (1, 10, 100, …), then refine. The three settings interact.
  • No intervals, no tests. SVR gives a number. For uncertainty use bootstrap, conformal prediction, or a Gaussian process, its probabilistic relative.
  • No extrapolation. Far from all support vectors the radial kernel gives 0 and the prediction returns to the constant \(b\).
  • Slow for large data: the time grows with the square to the cube of the rows. Above some ten thousand rows take boosting, forests or neural networks.
  • Categorical predictors must be coded as dummies, and they are then measured by the same distance as the rest.
  • Other kernels: linear (many predictors, few rows), polynomial. The radial one is the usual start.
  • A simple model is often close. Always fit one.

9. Reporting the results in APA style

A report (APA 7th edition) needs the unit of splitting, the preprocessing, the kernel, the tuning grid and its validation, the chosen settings, and the test error with its uncertainty and with simpler models. The numbers come from the R objects:

Code
o <- order(results$test_rmse)
apa_table <- data.frame(
  Model = as.character(results$model)[o],
  rmse = f2(results$test_rmse[o]),
  ci = paste0("[", f2(results$low[o]), ", ", f2(results$high[o]), "]"),
  r2 = f2(results$R2[o])
)

Method (data analysis):

We predicted the sound level of airfoils (1,503 measurements from 106 wind tunnel experiments; Brooks et al., 1989) from log frequency, angle of attack, chord length, air speed and the log thickness of the boundary layer with support vector regression with a radial kernel (Smola & Schölkopf, 2004). Predictors and outcome were standardized with the training data. A random quarter of the experiments was the test set. Cost, γ and ε were chosen by five-fold cross-validation in the training set, with whole experiments in one fold. Linear and quadratic regression, a generalized additive model and a random forest served for comparison. Confidence intervals of the test error are from 1,000 bootstrap samples of experiments. Analyses were conducted in R (Version 4.6.2; R Core Team, 2026) with the package e1071.

Results:

The selected settings were C = 10, γ = 0.1 and ε = 0.1 (cross-validated RMSE = 2.86 dB), with 742 support vectors. On 26 experiments that were not used for training, the RMSE was 2.60 dB, 95% CI [2.30, 2.87], compared with 5.16 for linear regression (Table 1). Cross-validation with single measurements left out at random would have suggested an RMSE of 1.43 dB, and the model chosen in this way had 3.51 dB on new experiments.

Table 1

Prediction of Sound Level in Experiments Not Used for Training

Model RMSE (dB) 95% CI R²
SVR, tuned by experiment 2.60 [2.30, 2.87] 0.84
Random forest 2.75 [2.31, 3.18] 0.82
Quadratic regression 3.37 [2.89, 3.82] 0.72
SVR, tuned by rows at random 3.51 [2.73, 4.23] 0.70
GAM 3.83 [3.03, 4.52] 0.64
Linear regression 5.16 [4.37, 5.98] 0.35

Note. 376 measurements of 26 experiments. Intervals from a bootstrap of experiments.

Points to remember:

  • Say what was kept together in the split.
  • Report the kernel, the grid and the chosen settings.
  • Give the test error in the unit of the outcome, with an interval, next to a simple model.

10. Summary

When Prediction of a continuous outcome with smooth nonlinear relations, up to some ten thousand rows
Loss Errors below \(\varepsilon\) are free, larger ones count in proportion
The function \(f(x) = \sum_i \alpha_i K(x_i, x) + b\), a sum over the support vectors
How (R) e1071::svm(y ~ x, data, cost, gamma, epsilon)
Settings Cost (fit against flatness), \(\gamma\) (width of the bumps), \(\varepsilon\) (width of the tube)
Needs Scaling; tuning with independent units left out
Does not give Coefficients, tests, intervals, extrapolation
Related pages Regularized regression, random forest, gradient boosting, splines and GAMs

References

Brooks, T. F., Pope, D. S., & Marcolini, M. A. (1989). Airfoil self-noise and prediction (NASA Reference Publication 1218). National Aeronautics and Space Administration.

Brooks, T., Pope, D., & Marcolini, M. (1989). Airfoil self-noise [Data set]. UCI Machine Learning Repository. https://doi.org/10.24432/C5VW2C

Cortes, C., & Vapnik, V. (1995). Support-vector networks. Machine Learning, 20(3), 273–297. https://doi.org/10.1007/BF00994018

Smola, A. J., & Schölkopf, B. (2004). A tutorial on support vector regression. Statistics and Computing, 14(3), 199–222. https://doi.org/10.1023/B:STCO.0000035301.49549.88