www.gusucode.com > stats 源码程序 matlab案例代码 > stats/PredictTrainingSampleMediansExample.m
%% Predict Training Sample Medians %% % 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. rng(1); % For reproducibility Mdl = TreeBagger(100,Displacement,MPG,'Method','regression'); %% % |Mdl| is a |TreeBagger| ensemble. %% % Perform quantile regression to predict the median MPG for all sorted % training observations. medianMPG = quantilePredict(Mdl,sort(Displacement)); %% % |medianMPG| is an |n|-by-1 numeric vector of medians corresponding to the % conditional distribution of the response given the sorted observations in % |Displacement|. |n| is the number of observations in |Displacement|. %% % Plot the observations and the estimated medians on the same figure. % Compare the median and mean responses. meanMPG = predict(Mdl,sort(Displacement)); figure; plot(Displacement,MPG,'k.'); hold on plot(sort(Displacement),medianMPG); plot(sort(Displacement),meanMPG,'r--'); ylabel('Fuel economy'); xlabel('Engine displacement'); legend('Data','Median','Mean'); hold off;