Regression

Quick Start Guide: Optimal Feature Selection for Regression

This is an R version of the corresponding OptimalFeatureSelection quick start guide.

In this example we will use Optimal Feature Selection on the Ailerons dataset, which addresses a control problem, namely flying a F16 aircraft. The attributes describe the status of the aeroplane, while the goal is to predict the control action on the ailerons of the aircraft.

First we load in the data and split into training and test datasets:

df <- read.table("ailerons.csv", header=TRUE, sep=",")
  climbRate Sgz     p     q curPitch curRoll absRoll diffClb diffRollRate
1         2 -56 -0.33 -0.09      0.9     0.2     -11      12        0.004
  diffDiffClb SeTime1 SeTime2 SeTime3 SeTime4 SeTime5 SeTime6 SeTime7 SeTime8
1        -0.1   0.032   0.032   0.032   0.032   0.032   0.032   0.032   0.032
  SeTime9 SeTime10 SeTime11 SeTime12 SeTime13 SeTime14 diffSeTime1 diffSeTime2
1   0.032    0.032    0.032    0.032    0.032    0.032           0           0
  diffSeTime3 diffSeTime4 diffSeTime5 diffSeTime6 diffSeTime7 diffSeTime8
1           0           0           0           0           0           0
  diffSeTime9 diffSeTime10 diffSeTime11 diffSeTime12 diffSeTime13 diffSeTime14
1           0            0            0            0            0            0
  alpha    Se   goal
1   0.9 0.032 -9e-04
 [ reached 'max' / getOption("max.print") -- omitted 13749 rows ]
iai::iai_setup()
X <- df[, 1:40]
y <- df[, 41]
split <- iai::split_data("regression", X, y, seed=1)
train_X <- split$train$X
train_y <- split$train$y
test_X <- split$test$X
test_y <- split$test$y

Model Fitting

We will use a grid_search to fit an optimal_feature_selection_regressor:

grid <- iai::grid_search(
    iai::optimal_feature_selection_regressor(
        random_seed=1,
    ),
    sparsity=1:10,
)
iai::fit(grid, train_X, train_y)
Julia Object of type GridSearch{OptimalFeatureSelectionRegressor,IAIBase.NullGridResult}.
All Grid Results:

│ Row │ sparsity │ train_score │ valid_score │ rank_valid_score │
│     │ Int64    │ Float64     │ Float64     │ Int64            │
├─────┼──────────┼─────────────┼─────────────┼──────────────────┤
│ 1   │ 1        │ 0.496049    │ 0.482452    │ 10               │
│ 2   │ 2        │ 0.669033    │ 0.658196    │ 9                │
│ 3   │ 3        │ 0.752872    │ 0.735493    │ 8                │
│ 4   │ 4        │ 0.807733    │ 0.80285     │ 7                │
│ 5   │ 5        │ 0.810867    │ 0.805916    │ 6                │
│ 6   │ 6        │ 0.815567    │ 0.811869    │ 1                │
│ 7   │ 7        │ 0.812824    │ 0.810027    │ 4                │
│ 8   │ 8        │ 0.81547     │ 0.808385    │ 5                │
│ 9   │ 9        │ 0.817183    │ 0.811309    │ 3                │
│ 10  │ 10       │ 0.817365    │ 0.811355    │ 2                │

Best Params:
  sparsity => 6

Best Model - Fitted OptimalFeatureSelectionRegressor:
  Constant: 0.000340755
  Weights:
    SeTime1: -0.0113283
    SeTime2: -0.0149811
    absRoll:  0.0000570265
    curRoll: -0.000107629
    diffClb: -0.00000229311
    p:       -0.000406711

The model selected a sparsity of 6 as the best parameter, but we observe that the validation scores are close for many of the parameters. We can use the results of the grid search to explore the tradeoff between the complexity of the regression and the quality of predictions:

results <- iai::get_grid_results(grid)
plot(results$sparsity, results$valid_score, type="l", xlab="Sparsity",
     ylab="Validation R-Squared")

We see that the quality of the model quickly increases with additional terms until we reach 4, and then only small increases afterwards. Depending on the application, we might decide to choose a lower sparsity for the final model than the value chosen by the grid search.

We can make predictions on new data using predict:

iai::predict(grid, test_X)
 [1] -0.0010432664 -0.0011999848 -0.0010974412 -0.0009334249 -0.0010097908
 [6] -0.0009290516 -0.0008761747 -0.0008764562 -0.0009084373 -0.0006841039
[11] -0.0008628487 -0.0009910481 -0.0008162965 -0.0008469993 -0.0006437190
[16] -0.0005430096 -0.0006045113 -0.0009663189 -0.0010006863 -0.0007485105
[21] -0.0006155603 -0.0009603741 -0.0006464340 -0.0008591355 -0.0007574091
[26] -0.0008977398 -0.0009019168 -0.0009455207 -0.0006610207 -0.0006055662
[31] -0.0008116220 -0.0007514605 -0.0009829932 -0.0009315550 -0.0005810658
[36] -0.0009860860 -0.0008351934 -0.0007785348 -0.0007767920 -0.0008042048
[41] -0.0007607774 -0.0008561190 -0.0008047970 -0.0011554387 -0.0009544098
[46] -0.0009940641 -0.0007918035 -0.0009921629 -0.0010688591 -0.0007959818
[51] -0.0011073903 -0.0009939759 -0.0007969292 -0.0008671913 -0.0007549672
[56] -0.0006911991 -0.0008673286 -0.0009764629 -0.0006833452 -0.0007313701
 [ reached getOption("max.print") -- omitted 4065 entries ]

We can evaluate the quality of the model using score with any of the supported loss functions. For example, the $R^2$ on the training set:

iai::score(grid, train_X, train_y, criterion="mse")
[1] 0.811926

Or on the test set:

iai::score(grid, test_X, test_y, criterion="mse")
[1] 0.8186486