⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 gedagui.m

📁 常用ROBUST STATISTICAL
💻 M
📖 第 1 页 / 共 4 页
字号:
% Set up the line handles - need n of these
Hline = zeros(1,n);
for i = 1:n
    Hline(i) = line('xdata',nan,'ydata',nan,'linestyle','-');
end

theta = -pi:0.1:pi;    %this defines the domain that will be plotted
y = zeros(n,p);       %there will n curves plotted, one for each obs
ang = zeros(length(theta),p);   %each row must be dotted w/ observation
% Get the string to evaluate function.
fstr = ['[1/sqrt(2) '];   %Initialize the string.
for i = 2:p
    if rem(i,2) == 0
        fstr = [fstr,' sin(',int2str(i/2), '*i) '];
    else
        fstr = [fstr,' cos(',int2str((i-1)/2),'*i) '];
    end
end
fstr = [fstr,' ]'];
k=0;
% evaluate sin and cos functions at each angle theta
for i=theta
    k=k+1;
    ang(k,:)=eval(fstr);
end
% inside the funciton.

% Now generate a y for each observation
for i=1:n     %loop over each observation
    for j=1:length(theta)
        y(i,j)=data(i,:)*ang(j,:)'; 
    end
end
% Display the curve
if colflag == 0
    % then display without class colors
    for i=1:n
        set(Hline(i),'xdata',theta,'ydata',y(i,:));
    end
elseif colflag == 1
    % then display WITH group colors.
    % Can only display up to 7 groups by color. this should be sufficient.
    % We will use the default MATLAB colors
    cols = {'b';'g';'r';'c';'m';'y';'k'};
    clab = unique(g);
    if length(clab) > length(cols)
        errordlg('The maximum number of allowed groups is 7.')
        return
    end
    for k = 1:length(clab)
        % loop over all of the different colors and display
        inds = find(g==clab(k));
        for i=1:length(inds)
            set(Hline(inds(i)),'xdata',theta,'ydata',y(inds(i),:),'color',cols{k});
        end  
    end
end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%       DO PARALLEL COORDINATES
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function doparallel

tg = findobj('tag','gedagui');
H = get(tg,'userdata');
ud = get(0,'userdata');
if isempty(ud.X)
    errordlg('You must load some data first.')
    return
end
% Get the dataset to be used.
tmp = get(H.data,'string');
dataset = tmp{get(H.data,'value')};
switch dataset
    case 'X'
        % use the original data ud.X
        data = ud.X;
        xstr = 'X';
    case 'PCA'
        % use PCA field. this is the projection matrix so data =
        % ud.X*ud.pca(:,1:d). 
        data = ud.X*ud.pca;
        xstr = 'PCA';

    case 'LLE'
        data = ud.LLE;
        xstr = 'LLE';
    case 'HLLE'
        data = ud.hLLE;
        xstr = 'HLLE';
    case 'ISOMAP'
        % Check to see what is saved there. Might be the whole thing or
        % might be just what the user outputs. Depends on whether the other
        % gui is open or not.
        % if iscell - then get the right number of dimensions wanted - like
        % pca. else it is double - correct array to just use.
        [n,p] = size(ud.X);
        if ~iscell(ud.isomap)
            data = ud.isomap;
        else
            data = ud.isomap{end}';
        end
        xstr = 'ISOMAP';
                       
    case 'MDS'
        % Added 11-12-05 - Process the MDS data.
        data = ud.mds;
        xstr = 'MDS';
        
    case 'CMDS'
        % Added 11-12-05 - Process the CMDS data.
        data = ud.cmds;
        xstr = 'CMDS';
        
    case 'PPEDA'
        % Added 11-12-05 - Process the PPEDA data.
        Z = sphere(ud.X);
        data = Z*ud.ppeda;
        xstr = 'PPEDA';
       
end
[n,p] = size(data);
% Get the information for plotting: Dimensions to use.
tmp = get(H.dimparallel,'string');
if strcmp(lower(tmp),'all')
    % user wants all dimensions to display.
    dims = 1:p;
else
    eval(['dims = [' tmp '];']);
    dims = round(dims);
end
if any(dims < 1) | any(dims > p)
    errordlg(['Dimensions must be between 1 and ' int2str(p)])
    return
end
if length(unique(dims)) < length(dims)
    errordlg('Dimensions must be different.')
    return
end
% Get the color by groups flag.
colflag = get(H.popmode,'value');
% OK to plot
hf = figure;
set(hf,'tag','parallel','numbertitle','off','name','EDA: Parallel Coordinates')
set(hf,'backingstore','off','renderer','painters','DoubleBuffer','on')
% Upon figure close, this should delete from the array.
set(hf,'CloseRequestFcn',...
    'gedagui(''update'');tg = findobj(''tag'',''gedagui''); H = get(tg,''userdata''); H.plots(find(H.plots == gcf)) = []; set(tg,''userdata'',H); delete(gcf)')
H.plots = [H.plots, hf];
set(tg,'userdata',H)
figure(hf)
fud = [];
switch colflag
    case 1
        % just do a plain plot
        Hline = parallel(data(:,dims),0);
        % This is a vector of handles - each observation has a handle.
        ud.linkptrap = [ud.linkptrap, hf];
        ud.linkap = [ud.linkap, Hline(:)];
        set(0,'userdata',ud)

    case 2
        % Color by groups - must be loaded by the user. Not from
        % clustering.
        if isempty(ud.classlab)
            errordlg('You must load some group labels.')
            close(hf)
            return
        end
        parallel(data(:,dims),1,ud.classlab);
    case 3
        % Color by clusters. 
        if isempty(ud.kmeansids) & isempty(ud.agcids) & isempty(ud.mbcids)
            % haven't clustered anything yet.
            errordlg('You must create some clusters first.')
            close(hf)
            return
        end
        menustrg = [];
        cidstr = [];
        if ~isempty(ud.kmeansids)
            menustrg = [menustrg, {'k-Means Clusters'}];
            cidstr = [cidstr, {'ud.kmeansids'}];
        end
        if ~isempty(ud.agcids)
            menustrg = [menustrg, {'Agglomerative Clustering'}];
            cidstr = [cidstr, {'ud.agcids'}];
        end
        if ~isempty(ud.mbcids)
            menustrg = [menustrg, {'Model-Based Clustering'}];
            cidstr = [cidstr, {'ud.mbcids'}];
        end
        if length(cidstr) == 1
            % no need for a listbox. Just plot.
            eval(['parallel(data(:,dims),1, ' cidstr{1} ' );'])
        else
            [sel,ok] = listdlg('PromptString','Select a type of clustering:',...
                'SelectionMode','single',...
                'ListString',menustrg);
            if ok == 1
                % person selected something.
                eval(['parallel(data(:,dims),1, ' cidstr{sel} ' );'])
            end
            
        end
end
title(xstr)

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function Hline = parallel(x,colflag,g)
% Hline is a vector of handles to lines - for brushing/linking
% colflag is a flag indicating color by groups.
% g is a vector of group labels.
[n,p] = size(x);
% Calling part
% Set up the line handles - need n of these
Hline = zeros(1,n);
for i = 1:n
    Hline(i) = line('xdata',nan,'ydata',nan,'linestyle','-');
end
% get the axes lines
ypos = linspace(0,1,p);
xpos = [0 1];
% THe following gets the axis lines.
% Save the text handles, so they can be permuted, too.
% Save the text strings, too.
Htext = zeros(1,p);
k = p;
for i=1:p            
    line(xpos,[ypos(i) ypos(i)],'color','k')
    Htext(i) = text(-0.05,ypos(i), ['x' num2str(k)]);
    k=k-1;
end
axis off
% parallel(Hline,X,Htext,1:p);

% Inside the funciton.

% If parallel coordinates, then change range.
% map all values onto the interval 0 to 1
% lines will extend over the range 0 to 1
md = min(x(:)); % find the min of all the data
rng = range(x(:));  %find the range of the data
xn = (x-md)/rng;
[n,p] = size(x);
ypos = linspace(0,1,p);
if colflag == 0
    % then display without class colors
    for i=1:n
        set(Hline(i),'xdata',xn(i,:),'ydata',fliplr(ypos))
    end   
elseif colflag == 1
    % then display WITH group colors.
    % Can only display up to 7 groups by color. this should be sufficient.
    % We will use the default MATLAB colors
    cols = {'b';'g';'r';'c';'m';'y';'k'};
    clab = unique(g);
    if length(clab) > length(cols)
        errordlg('The maximum number of allowed groups is 7.')
        return
    end
    for k = 1:length(clab)
        % loop over all of the different colors and display
        inds = find(g==clab(k));
        
        try
            for i=1:length(inds)
                set(Hline(inds(i)),'xdata',xn(inds(i),:),'ydata',fliplr(ypos),'color',cols{k})
            end
        catch
            keyboard
        end

    end
end

%%%%%%%%%%%%%%%%%%%%%%
function update(fig)
% fig should be the handle to the plot that is closing.
% Use this function to update the brush/link information
ud = get(0,'userdata');

% the following are andrews/parallel, where we have n lines per plot.
inds = find(ud.linkptrap == fig);
if ~isempty(inds)
    ud.linkptrap(inds) = [];
    ud.linkap(:,inds) = [];
end
% The following array keeps the highlighted axes for all figures. 
% Reset to empty. 
Haxes = findobj(fig,'type','axes');
[c,inda,indb] = intersect(ud.highlight,Haxes);
ud.highlight(inda) = [];
% see if this is the current brushing plot.
Hbgui = findobj('tag','brushgui');
if ~isempty(Hbgui)
    H = get(Hbgui,'userdata');
    if H.brushplot == fig
        % then this is the current plot being brushed.
        H.brushplot = [];
        H.Hbrush = [];
        H.BrushPrevX = [];
        H.BrushPrevY = [];
        % RESET ALL PLOTs to the normal one.
        brushgui('reset')
        % reset the userdata in brushgui
        set(Hbgui,'userdata',H)
    end
end

inds = find(ud.brushptr == fig);
% Now update the brushgui popupmenu
if ~isempty(Hbgui)
    if length(ud.brushptr) == 1
        strg = {'None'};
    else
        strg = get(H.popplot,'string');
        strg(inds) = [];        % delete from the menu string.
    end
        if isempty(ud.brushptr)
        strg = {'None'};
    end
    set(H.popplot,'string',strg)
    % reset the userdata in brushgui
    set(Hbgui,'userdata',H)
end

% update the brushptr array.
if ~isempty(inds)
    ud.brushptr(inds) = [];
    ud.brush(inds) = [];    % these are all single handles to line objects
end

set(0,'userdata',ud)


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function Z = sphere(X)
% This function will sphere the data. This means the data will now have a
% mean of 0 and a covariance matrix of 1.
[n,p] = size(X);
muhat = mean(X);
[V,D] = eig(cov(X));
Xc = X - ones(n,1)*muhat;
Z = ((D)^(-1/2)*V'*Xc')';

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -