www.gusucode.com > stats 源码程序 matlab案例代码 > stats/EstimatekFoldMeanSquaredErrorExample.m

    %% Estimate _k_-Fold Mean Squared Error
%%
% Simulate 10000 observations from this model
%
% $$y = x_{100} + 2x_{200} + e.$$
%
%
% * $X = \{x_1,...,x_{1000}\}$ is a 10000-by-1000 sparse matrix with 10%
% nonzero standard normal elements.
% * _e_ is random normal error with mean 0 and standard deviation
% 0.3.
%
rng(1) % For reproducibility
n = 1e4;
d = 1e3;
nz = 0.1;
X = sprandn(n,d,nz);
Y = X(:,100) + 2*X(:,200) + 0.3*randn(n,1);
%%
% Cross-validate a linear regression model using SVM learners.
rng(1); % For reproducibility 
CVMdl = fitrlinear(X,Y,'CrossVal','on');
%%
% |CVMdl| is a |RegressionPartitionedLinear| model. By default, the
% software implements 10-fold cross validation.  You can alter the number
% of folds using the |'KFold'| name-value pair argument.
%%
% Estimate the average of the test-sample MSEs.
mse = kfoldLoss(CVMdl)
%%
% Alternatively, you can obtain the per-fold MSEs by specifying the
% name-value pair |'Mode','individual'| in |kfoldLoss|.