www.gusucode.com > mbctools 工具箱 matlab 源码程序 > mbctools/@mdev_local/BestModel.m

    function [bm,msg]=BestModel(mdev,TSIndex,Climb)
%BESTMODEL best model for local node
%
% [bm,msg]=BestModel(mdev,TSIndex,Climb)
%   TSIndex is the index to the best two-stage model in
%   mdev_local.TwoStage. This is different from modeldev.BestModel which is
%   normally a pointer to the best child.
%   Climb assigns the node as best if it is the only child.

%  Copyright 2000-2010 The MathWorks, Inc. and Ford Global Technologies, Inc.


msg= '';
if nargin==1
    if isempty(mdev.TwoStage)
        bm= [];
    else
        bmIndex= BestModel(mdev.modeldev);
        if isnumeric(bmIndex) && bmIndex~=0
            bm= mdev.TwoStage{bmIndex};
        else
            bm= [];
        end
    end
else
    if nargin<3
        Climb=0;
    end


    pResp= Parent(mdev);
    L=model(mdev);
    if isnumeric(TSIndex) && TSIndex~=0
        if TSIndex~=2
            %MLE model is always second (univariate is first).
            mdev.MLE.BestModel= 0;
        end
        mdev.modeldev= BestModel(mdev.modeldev,TSIndex);

        pointer(mdev);
        % select response feature models as best
        rfind= mdev.ResponseFeatures(TSIndex,:);
        ch=children(mdev);
        if RFstart(L);
            rfind= [1 rfind+1];
        end
        children(mdev,rfind,@status,2);
        % this will select the two-stage model properly
        mdev= status(mdev,1);
        
        % set other response features
        NotBest= setdiff(1:length(ch),rfind);
        if ~isempty(NotBest)
            % don't update response features that are not fitted
            % (status==0)
            st = children(mdev,NotBest,@status);
            NotBest([st{:}]==0) = [];
            % now update status of response features
            children(mdev,NotBest,@status,1);
        end

        mdev= statistics(mdev,mdev.TSstatistics.Summary(TSIndex,:));
        %tidy up MLE data
        if isfield(mdev.MLE,'Init')
            mdev.MLE= rmfield(mdev.MLE,'Init');
        end
        if isfield(mdev.MLE,'Model')
            mdev.MLE= rmfield(mdev.MLE,{'Model','Solution'});
        end

        if isbest(mdev)
            pResp.BestModel(xregpointer);
        end

        % make sure mlerf status is correct
        mdev= makemlerf(mdev);
        if Climb==0
            % this clears the status properly
            mdev= status(mdev,1,0);
            mdev= status(mdev,1);
        end

        
    else
        % unset best model

        % assign twostage summary stats to NaN
        s= statistics(mdev);
        s(2:end)= NaN;
        mdev= statistics(mdev,s);
        if isbest(mdev)
            pResp.BestModel(xregpointer);
            pResp.statistics(s);
        end
        mdev.MLE.BestModel= 0;
        mdev.modeldev= BestModel(mdev.modeldev,0);
        pointer(mdev);
        
        % change status of children to 1 if they are 2
        ch= children(mdev);
        for i=1:numChildren(mdev)
            % change them back to modeldev
            ch(i).modeldev;
            st= ch(i).status;
            if st==2
                % set status to 1 (not part of a two-stage model)
                ch(i).status(1,0);
            end
        end
        
        if isBrowserProject(mdev)
            mbH= MBrowser;
            % this updates the icon on the modelbrowser tree.
            try
                mbH.doDrawTree(address(mdev));
            end
        end
    end
    
    
    if pResp.childindex==1 && get(L,'datumtype')  
        % update datum link required if mdev is selected as best
        prf= children(mdev);
        pdatum= datumlink(mdev);
        % changing datum link
        if pdatum~= prf(1)
            % assign datum to new value
            pResp.AssignData('Data',prf(1));
        end
        
        % update datum link models in linked responses
        mdev= UpdateLinks(mdev,1);
        
        ptp= pResp.Parent;
        pr= ptp.children(@datumlink);
        if sum([pr{:}]==pdatum)>1
            msg= 'The datum model has changed. All responses using datum links have been updated.';
        end
    end

    
    pointer(mdev);
    bm= mdev;
end