www.gusucode.com > stats 源码程序 matlab案例代码 > stats/CreateCrossValidatedLinearRegressionModelExample.m
%% Create Cross-Validated Linear Regression Model %% % 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. To increase execution speed, % transpose the predictor data and specify that the observations are in % columns. X = X'; CVMdl = fitrlinear(X,Y,'CrossVal','on','ObservationsIn','columns'); %% % |CVMdl| is a |RegressionPartitionedLinear| cross-validated model. Because % |fitrlinear| implements 10-fold cross-validation by default, % |CVMdl.Trained| contains a cell vector of ten |RegressionLinear| models. % Each cell contains a linear regression model trained on nine folds, and % then tested on the remaining fold. %% % Predict responses for out-of-fold observations and estimate the % generalization error by passing |CVMdl| to |kfoldPredict| and % |kfoldLoss|, respectively. oofYHat = kfoldPredict(CVMdl); ge = kfoldLoss(CVMdl) %% % The estimated, generalization, mean squared error is 0.1748.