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

📄 method_b4.m

📁 Variable Reduction Testbench通过对变量进行相关性分析来实现减少变量的目的。
💻 M
字号:

function [ranking, load_x, cut_off] = method_B4(X, is_corrmatrix)

% -------------------------------------------------------------------------
% this code is part of the 'Reduction Testbench' suite
% developed by A. Manganaro, R. Todeschini, A. Ballabio, D. Mauri
% 2006 - Milano Chemometrics and QSAR Research Group
% -------------------------------------------------------------------------
%
%
% [ranking, load_x, cut_off] = method_B4(X)
%
% method_B4 uses Jolliffe's B4 algorithm to to evaluate an elimination ranking 
% of variables without any loss of data; it is suggested to delete
% until m variables are left, where m is calculated with the BS algorithm
% This routine also outputs a plot of the ranking
%
% Input:
% X = data set [n x p]  n objects, p variables
% is_corrmatrix = if set to 'y', X is seen as the dataset's correlation
%   matrix
%
% Output:
% ranking = elimination ranking [1 x p] of the variables
% load_x = values [1 x p] of the EV% associated with the PC of each variable
% cut_off = number of variables suggested to be retained


echo off;

[n,p] = size(X);

if ( (p<2) | (n<2) )
    disp('Wrong matrix dimension - execution aborted');
    ranking=0; load_x=0; return;
end


% Sets the progressbar
screensize = [0 0 1 1];
width = screensize(3)/3;
height = screensize(4)/25;
left = screensize(3)/2 - width/2;
bottom = screensize(4)/2 - height/2;
progress_win = figure('Units','normalized','Position',[[left bottom] width height],...
                          'MenuBar','none','Resize','off','Name','Algorithm working...',...
                          'NumberTitle','off','WindowStyle','modal');
progress_axes = axes('Position',[0.02 0.15 0.96 0.70],'XLim',[0 1],'YLim',[0 1],'Box','on',...
                         'xtick',[],'ytick',[]);
progress_patch = patch('XData',[0 1 1 0],'YData',[0 0 1 1],'FaceColor',[1 0 0]);
drawnow;

p_tot = p;

set(progress_win,'Pointer','watch');


% Calculate the correlation matrix
if (is_corrmatrix)
    C = X;
else
    C = corrcoef(X);
end


% Calculate the number m of significative PC using the BS algorithm
[L,Lambda] = svd(C);
m = 0;
cur_ev = 1;
cur_dm = 0;
while (m<p) & (cur_ev>cur_dm)
    m = m + 1;
    cur_ev = (Lambda(m,m) / trace(Lambda)) * 100;
    tmpsum=0;
    for idx = m:p
        tmpsum = tmpsum + 1/idx;
    end
    cur_dm = (100/p)*tmpsum;
end
m = m - 1;


ranking = [];
rank_idx = 1;
    
for p_idx = 1:p
    
    % Updates the progressbar
    set(progress_win,'Name',['Algorithm working... ',int2str(p-p_idx),' variables left']);
    set(progress_patch,'XData',[0 (p-p_idx)/p_tot (p-p_idx)/p_tot 0],'Facecolor',...
        [1 (1-(p-p_idx)/p_tot) 0]);
    drawnow;

    cur_pc(:,1) = [1:length(L(:,p_idx))]';
    cur_pc(:,2) = abs(L(:,p_idx));
        
    cur_pc = sortrows(cur_pc,2);

    max_var = length(cur_pc);
        
    while (max_var>1)
        flag = 0;
        for i=1:length(ranking)
            if (ranking(i)==cur_pc(max_var,1))
                flag=1;
                break;
            end
        end
            
        if (flag==0)
            break;
        end
            
        max_var = max_var - 1;
    end

    % Updates ranking and selected
    ranking(rank_idx) = cur_pc(max_var,1);
    load_x(rank_idx) = (Lambda(p_idx,p_idx)/sum(diag(Lambda)))*100;
        
    % Updates indexes
    rank_idx = rank_idx + 1;
    
end

for idx=1:length(ranking)
    inv_temp(idx) = ranking(length(ranking)+1-idx);
end
ranking = inv_temp;
inv_temp = [];
for idx=1:length(load_x)
    inv_temp(idx) = load_x(length(load_x)+1-idx);
end
load_x = inv_temp;

% Closes the progressbar
set(progress_win,'Pointer','arrow');
close(progress_win);

cut_off = m;

%%%% Fig_1: graph of resulting ranking %%%%

fig_1 = figure('Name','Variables elimination ranking - B4 method');
title('Variables ranking by B4 method');
xlabel('Variables ranking');
ylabel('EV% of the correspondent PC');
ylim([0 max(load_x)+(10*max(load_x)/100)]);
xlim([0 length(load_x)+1]);

hold on;

line([(length(load_x)-m+0.6) (length(load_x)-m+0.6)],ylim,'LineStyle','--','Color','r');
plot(load_x,'o-b','MarkerFaceColor','b');
for i=1:length(ranking)
    text(i,load_x(i)+(5*max(load_x)/100),[' ',num2str(ranking(i))]);
end
text((length(load_x)-m+0.7),(5*max(load_x)/100),['variables = ',num2str(m)]);

hold off;


echo on;

⌨️ 快捷键说明

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