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

    %% Train Regression Trees Using classregtree
% This example uses the data on cars in |carsmall.mat| to create a regression
% tree for predicting mileage using measurements of weight and the number
% of cylinders as predictors. Here, one predictor (weight) is continuous
% and the other (cylinders) is categorical. The response (mileage) is
% continuous.
%%
% Load the data and use the |classregtree| constructor of the
% |classregtree| class to create the regression tree:
load carsmall

t = classregtree([Weight, Cylinders],MPG,...
                 'Categorical',2,'MinParent',20,...
                 'Names',{'W','C'})
%%
% |t| is a |classregtree| object and can be operated on with any of the
% methods of the class.
%%
% Use the |type| method of the |classregtree| class to show the type of the
% tree:
treetype = type(t)
%%
% |classregtree| creates a regression tree because MPG is a numerical vector,
% and the response is assumed to be continuous.
%%
% To view the tree, use the |view| method of the |classregtree| class:
view(t)
%%
% The tree predicts the response values at the circular leaf nodes based on
% a series of questions about the car at the triangular branching nodes. A
% |true| answer to any question follows the branch to the left; a |false|
% follows the branch to the right.
%%
% Use the tree to predict the mileage for a 2000-pound car with either 4,
% 6, or 8 cylinders:
mileage2K = t([2000 4; 2000 6; 2000 8])
%%
% The object allows for functional evaluation, of the form |t(X)|. This is
% a shorthand way of calling the |eval| method of the |classregtree| class.
%%
% The predicted responses computed above are all the same. This is
% because they follow a series of splits in the tree that depend only on
% weight, terminating at the leftmost leaf node in the view above. A
% 4000-pound car, following the right branch from the top of the tree,
% leads to different predicted responses:
mileage4K = t([4000 4; 4000 6; 4000 8])
%%
% You can use a variety of other methods of the |classregtree| class, such
% as |cutvar|, |cuttype|, and |cutcategories|, to get more information about the
% split at node 3 that distinguishes the 8-cylinder car:
var3 = cutvar(t,3)      % What variable determines the split?
type3 = cuttype(t,3)    % What type of split is it?
c = cutcategories(t,3); % Which classes are sent to the left 
                        % child node, and which to the right?
leftChildNode = c{1}
rightChildNode = c{2}
%%
% Regression trees fit the original (training) data well, but may do a poor
% job of predicting new values. Lower branches, especially, may be strongly
% affected by outliers. A simpler tree often avoids overfitting. To find
% the best regression tree, employing the techniques of resubstitution and
% cross validation, use the |test| method of the |classregtree| class.