www.gusucode.com > stats 源码程序 matlab案例代码 > stats/PredictTestSampleResponsesExample.m
%% Predict Test-Sample Responses %% % 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); %% % Train a linear regression model. Reserve 30% of the observations as a % holdout sample. CVMdl = fitrlinear(X,Y,'Holdout',0.3); Mdl = CVMdl.Trained{1} %% % |CVMdl| is a |RegressionPartitionedLinear| model. It contains % the property |Trained|, which is a 1-by-1 cell array holding a % |RegressionLinear| model that the software trained using the % training set. %% % Extract the training and test data from the partition definition. trainIdx = training(CVMdl.Partition); testIdx = test(CVMdl.Partition); %% % Predict the training- and test-sample responses. yHatTrain = predict(Mdl,X(trainIdx,:)); yHatTest = predict(Mdl,X(testIdx,:)); %% % Because there is one regularization strength in |Mdl|, |yHatTrain| and % |yHatTest| are numeric vectors.