Cross-validated kernel model for regression


RegressionPartitionedKernel is a set of kernel regression models trained on cross-validated folds. To obtain a cross-validated, kernel regression model, use fitrkernel and specify one of the cross-validation options. You can estimate the predictive quality of the model, or how well the linear regression model generalizes, using one or more of these “kfold” methods: kfoldPredict and kfoldLoss.

Every “kfold” method uses models trained on training-fold observations to predict the response for validation-fold observations. For example, suppose that you cross-validate using five folds. In this case, the software randomly assigns each observation into five groups of equal size (roughly). The training fold contains four of the groups (that is, roughly 4/5 of the data) and the validation fold contains the other group (that is, roughly 1/5 of the data). In this case, cross-validation proceeds as follows:

  1. The software trains the first model (stored in CVMdl.Trained{1}) using the observations in the last four groups and reserves the observations in the first group for validation.

  2. The software trains the second model (stored in CVMdl.Trained{2}) using the observations in the first group and the last three groups. The software reserves the observations in the second group for validation.

  3. The software proceeds in a similar fashion for the third through the fifth models.

If you validate by calling kfoldPredict, it computes predictions for the observations in group 1 using the first model, group 2 for the second model, and so on. In short, the software estimates a response for every observation using the model trained without that observation.


RegressionPartitionedKernel model objects do not store the predictor data set.


Create a RegressionPartitionedKernel object using the fitrkernel function. Use one of the 'CrossVal', 'CVPartition', 'Holdout', 'KFold', or 'Leaveout' name-value pair arguments in the call to fitrkernel. For details, see the fitrkernel function reference page.


expand all

Cross-Validation Properties

This property is read-only.

Cross-validated model name, specified as a character vector.

For example, 'Kernel' specifies a cross-validated kernel model.

Data Types: char

This property is read-only.

Number of cross-validated folds, specified as a positive integer scalar.

Data Types: double

This property is read-only.

Cross-validation parameter values, specified as an object. The parameter values correspond to the name-value pair argument values used to cross-validate the kernel regression model. ModelParameters does not contain estimated parameters.

This property is read-only.

Number of observations in the training data, specified as a positive numeric scalar.

Data Types: double

This property is read-only.

Data partition indicating how the software splits the data into cross-validation folds, specified as a cvpartition model.

This property is read-only.

Kernel regression models trained on cross-validation folds, specified as a cell array of RegressionKernel models. Trained has k cells, where k is the number of folds.

Data Types: cell

This property is read-only.

Observation weights used to cross-validate the model, specified as a numeric vector. W has NumObservations elements.

The software normalizes the weights used for training so that nansum(W) is 1.

Data Types: single | double

This property is read-only.

Observed response values used to cross-validate the model, specified as a numeric vector. Y has NumObservations elements.

Each row of Y represents the observed response of the corresponding observation in the predictor data.

Data Types: single | double

Other Regression Properties

This property is read-only.

Categorical predictor indices, specified as an empty numeric value. In general, CategoricalPredictors contains index values corresponding to the columns of the predictor data that contain categorical predictors. Because RegressionKernel models can only be trained on numeric predictor data, this property is empty ([]).

This property is read-only.

Predictor names in order of their appearance in predictor data (X), specified as a cell array of character vectors of the form {'x1','x2',...}. The length of PredictorNames is equal to the number of columns in X.

Data Types: cell

This property is read-only.

Response variable name, specified as 'Y'. Because RegressionKernel models cannot be trained using tabular data, this property is always 'Y'.

Data Types: char

Response transformation function, specified as 'none' or a function handle. ResponseTransform describes how the software transforms raw response values predicted by the model.

For a MATLAB® function, or a function that you define, enter its function handle. For example, you can enter Mdl.ResponseTransform = @function, where function accepts a numeric vector of the original responses and returns a numeric vector of the same size containing the transformed responses.

Data Types: char | string | function_handle

Object Functions

kfoldLossRegression loss for cross-validated kernel regression model
kfoldPredictPredict responses for observations in cross-validated kernel regression model


collapse all

Simulate sample data.

rng(0,'twister'); % For reproducibility
n = 1000;
x = linspace(-10,10,n)';
y = 1 + x*2e-2 + sin(x)./x + 0.2*randn(n,1);

Cross-validate a kernel regression model.

CVMdl = fitrkernel(x,y,'Kfold',5);

CVMdl is a RegressionPartitionedKernel 5-fold cross-validated model. CVMdl.Trained contains a cell vector of five RegressionKernel models. Display the trained property.

ans=5×1 cell array
    {1x1 RegressionKernel}
    {1x1 RegressionKernel}
    {1x1 RegressionKernel}
    {1x1 RegressionKernel}
    {1x1 RegressionKernel}

Each cell contains a kernel regression model trained on four folds, then tested on the remaining fold.

Predict responses for observations in the validation folds and estimate the generalization error by passing CVMdl to kfoldPredict and kfoldLoss, respectively.

yHat = kfoldPredict(CVMdl);
L = kfoldLoss(CVMdl)
L = 0.1887

kfoldLoss computes the average mean squared error for all the folds by default. The estimated mean squared error is 0.1887.

Introduced in R2018b