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

    %% Predict Out-of-Bag Medians Using Quantile Regression
%%
% Load the |carsmall| data set.  Consider a model that predicts the fuel
% economy of a car given its engine displacement.
load carsmall
%%
% Train an ensemble of bagged regression trees using the entire data set.
% Specify 100 weak learners and save out-of-bag indices.
rng(1); % For reproducibility
Mdl = TreeBagger(100,Displacement,MPG,'Method','regression',...
    'OOBPrediction','on');
%%
% |Mdl| is a |TreeBagger| ensemble.
%%
% Perform quantile regression to predict the out-of-bag median MPG for all
% training observations.
oobMedianMPG = oobQuantilePredict(Mdl);
%%
% |oobMedianMPG| is an |n|-by-1 numeric vector of medians corresponding to the
% conditional distribution of the response given the sorted observations in
% |Mdl.X|. |n| is the number of observations, |size(Mdl.X,1)|.
%%
% Sort the observations in ascending order.  Plot the observations and
% the estimated medians on the same figure. Compare the out-of-bag median
% and mean responses.
[sX,idx] = sort(Mdl.X);
oobMeanMPG = oobPredict(Mdl);

figure;
plot(Displacement,MPG,'k.');
hold on
plot(sX,oobMedianMPG(idx));
plot(sX,oobMeanMPG(idx),'r--');
ylabel('Fuel economy');
xlabel('Engine displacement');
legend('Data','Out-of-bag median','Out-of-bag mean');
hold off;