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 ]
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.502496 0.469551 10
2 │ 2 0.664859 0.661475 9
3 │ 3 0.75009 0.746062 8
4 │ 4 0.808994 0.800123 7
5 │ 5 0.814076 0.803629 6
6 │ 6 0.816877 0.807073 5
7 │ 7 0.819178 0.809386 3
8 │ 8 0.819249 0.809528 2
9 │ 9 0.819444 0.809719 1
10 │ 10 0.818511 0.808262 4
Best Params:
sparsity => 9
Best Model - Fitted OptimalFeatureSelectionRegressor:
Constant: 0.000340372
Weights:
SeTime1: -0.00724609
SeTime2: -0.00917727
SeTime3: -0.0092368
absRoll: 0.0000578331
curPitch: -0.00000852542
curRoll: -0.0000862067
diffClb: -0.00000312272
diffRollRate: 0.00255865
p: -0.000427066
The model selected a sparsity of 9 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_result_summary(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 see the relative importance of the selected features with variable_importance
:
iai::variable_importance(iai::get_learner(grid))
Feature Importance
1 absRoll 0.34227976
2 p 0.18515383
3 curRoll 0.11988138
4 SeTime3 0.09149589
5 SeTime2 0.09090944
6 SeTime1 0.07144149
7 diffRollRate 0.04879900
8 diffClb 0.04622087
9 curPitch 0.00381835
10 Se 0.00000000
11 SeTime10 0.00000000
12 SeTime11 0.00000000
13 SeTime12 0.00000000
14 SeTime13 0.00000000
15 SeTime14 0.00000000
16 SeTime4 0.00000000
17 SeTime5 0.00000000
18 SeTime6 0.00000000
19 SeTime7 0.00000000
20 SeTime8 0.00000000
21 SeTime9 0.00000000
22 Sgz 0.00000000
23 alpha 0.00000000
24 climbRate 0.00000000
25 diffDiffClb 0.00000000
26 diffSeTime1 0.00000000
27 diffSeTime10 0.00000000
28 diffSeTime11 0.00000000
29 diffSeTime12 0.00000000
30 diffSeTime13 0.00000000
[ reached 'max' / getOption("max.print") -- omitted 10 rows ]
We can make predictions on new data using predict
:
iai::predict(grid, test_X)
[1] -0.0010281378 -0.0012487307 -0.0012003369 -0.0008068566 -0.0011633032
[6] -0.0009652765 -0.0008148358 -0.0007641160 -0.0009406862 -0.0008375387
[11] -0.0007295318 -0.0009047254 -0.0010255249 -0.0009056285 -0.0008283758
[16] -0.0006776709 -0.0005578737 -0.0006099722 -0.0009075772 -0.0008663184
[21] -0.0006002369 -0.0009453024 -0.0008858585 -0.0005869918 -0.0008605741
[26] -0.0009016756 -0.0009060668 -0.0006778240 -0.0008103940 -0.0006123244
[31] -0.0007698186 -0.0008234952 -0.0008685754 -0.0006970097 -0.0008202011
[36] -0.0005869953 -0.0007606435 -0.0009958238 -0.0008238551 -0.0009045685
[41] -0.0009013885 -0.0005860644 -0.0007914302 -0.0006925710 -0.0007125598
[46] -0.0008811342 -0.0005285094 -0.0009730218 -0.0009066615 -0.0009820481
[51] -0.0010676127 -0.0008788254 -0.0006542894 -0.0007466493 -0.0010080440
[56] -0.0009761843 -0.0006997094 -0.0008834828 -0.0008883447 -0.0009920841
[ 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.8164733
Or on the test set:
iai::score(grid, test_X, test_y, criterion = "mse")
[1] 0.8194177