Create a regression model with more than one response variable by using fitrchains
.
Load the carbig
data set, which contains measurements of cars made in the 1970s and early 1980s. Create a table containing the predictor variables Displacement
, Horsepower
, and so on, as well as the response variables Acceleration
and MPG
. Display the first eight rows of the table.
Displacement Horsepower Model_Year Origin Weight Acceleration MPG
____________ __________ __________ _______ ______ ____________ ___
307 130 70 USA 3504 12 18
350 165 70 USA 3693 11.5 15
318 150 70 USA 3436 11 18
304 150 70 USA 3433 12 16
302 140 70 USA 3449 10.5 17
429 198 70 USA 4341 10 15
454 220 70 USA 4354 9 14
440 215 70 USA 4312 8.5 14
Categorize the cars based on whether they were made in the USA.
Partition the data into training and test sets. Use approximately 85% of the observations to train a multiresponse model, and 15% of the observations to test the performance of the trained model on new data. Use cvpartition
to partition the data.
Train a multiresponse regression model by passing the carsTrain
training data to the fitrchains
function. By default, the function uses bagged ensembles of trees in the regression chains.
Mdl =
RegressionChainEnsemble
PredictorNames: {'Displacement' 'Horsepower' 'Model_Year' 'Origin' 'Weight'}
ResponseName: ["Acceleration" "MPG"]
CategoricalPredictors: 4
NumChains: 2
LearnedChains: {2×2 cell}
NumObservations: 338
Properties, Methods
Mdl
is a trained RegressionChainEnsemble
model object. You can use dot notation to access the properties of Mdl
. For example, you can specify Mdl.Learners
to see the bagged ensembles used to train the model.
Evaluate the performance of the regression model on the test set by computing the test mean squared error (MSE). Smaller MSE values indicate better performance. Return the loss for each response variable separately by setting the OutputType
name-value argument to "per-response"
.
testMSE = 1×2
2.4921 9.0568
Predict the response values for the observations in the test set. Return the predicted response values as a table.
predictedY=60×2 table
Acceleration MPG
____________ ______
12.573 16.109
10.78 13.988
11.282 12.963
15.185 21.066
12.203 13.773
13.216 14.216
17.117 30.199
16.478 29.033
13.439 14.208
11.552 13.066
13.398 13.271
14.848 20.927
16.552 24.603
12.501 15.359
15.778 19.328
12.343 13.185
⋮