Cross-validate a quantile neural network regression model using the default 10-fold cross-validation.
Load the carbig data set, which contains measurements of cars made in the 1970s and early 1980s. Create a matrix X containing the predictor variables Acceleration, Displacement, Horsepower, and Weight. Store the response variable MPG in the variable Y.
Delete rows of X and Y where either array has missing values.
Train a quantile neural network regression model using the fitrqnet function. Specify to use the 0.25, 0.50, and 0.75 quantiles (that is, the lower quartile, median, and upper quartile). To improve the model fit, standardize the numeric predictors. Use a ridge (L2) regularization term of 0.05.
Mdl =
RegressionQuantileNeuralNetwork
ResponseName: 'Y'
CategoricalPredictors: []
LayerSizes: 10
Activations: 'relu'
OutputLayerActivation: 'none'
Quantiles: [0.2500 0.5000 0.7500]
Properties, Methods
Mdl is a trained RegressionQuantileNeuralNetwork model object.
Cross-validate the regression model using 10-fold cross-validation. During cross-validation, the software completes these steps:
Randomly partition the data into 10 sets (folds) of equal size.
For each set, reserve the set as validation data, and train a quantile neural network regression model on the other nine sets.
Store the 10 trained models in a 10-by-1 cell vector in the Trained property of the cross-validated model object.
CVMdl =
RegressionPartitionedQuantileNeuralNetwork
CrossValidatedModel: 'QuantileNeuralNetwork'
PredictorNames: {'x1' 'x2' 'x3' 'x4'}
ResponseName: 'Y'
NumObservations: 392
KFold: 10
Partition: [1×1 cvpartition]
ResponseTransform: 'none'
Quantiles: [0.2500 0.5000 0.7500]
Properties, Methods
CVMdl is a RegressionPartitionedQuantileNeuralNetwork object.
Display the first model in CVMdl.Trained.
FirstModel =
CompactRegressionQuantileNeuralNetwork
PredictorNames: {'x1' 'x2' 'x3' 'x4'}
ResponseName: 'Y'
CategoricalPredictors: []
LayerSizes: 10
Activations: 'relu'
OutputLayerActivation: 'none'
Quantiles: [0.2500 0.5000 0.7500]
Properties, Methods
FirstModel is the first of the 10 trained models contained in CVMdl. The trained models are all CompactRegressionQuantileNeuralNetwork objects.
After creating a cross-validated quantile regression model object, you can estimate the generalized quantile loss by passing the model to kfoldLoss.