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

    %% Compute Regression Loss for Test Data  

%% 
% Load the sample data. 
load(fullfile(matlabroot,'examples','stats','gprdata.mat'))

%%
% The data has 8 predictor variables and contains 500 observations in training
% data and 100 observations in test data. This is simulated data.  

%% 
% Fit a GPR model using the squared exponential kernel function with separate
% length scales for each predictor. Standardize the predictor values in
% the training data. Use the exact method for fitting and prediction. 
gprMdl = fitrgp(Xtrain,ytrain,'FitMethod','exact',...
'PredictMethod','exact','KernelFunction','ardsquaredexponential',...
'Standardize',1);  

%% 
% Compute the regression error for the test data. 
L = loss(gprMdl,Xtest,ytest)  

%% 
% Predict the responses for test data. 
ypredtest = predict(gprMdl,Xtest);  

%% 
% Plot the test response along with the predictions. 
figure;
plot(ytest,'r');
hold on;
plot(ypredtest,'b');
legend('Data','Predictions','Location','Best');     

%% 
% Manually compute the regression loss. 
L = (ytest - ypredtest)'*(ytest - ypredtest)/length(ytest)