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

📄 reduction.m

📁 Variable Reduction Testbench通过对变量进行相关性分析来实现减少变量的目的。
💻 M
📖 第 1 页 / 共 3 页
字号:
        handles.cur_expl = 'FirstEig Method (''value'' field stands for the loading in the 1st PC) ';
         handles.result_cutoff = -999;
    elseif (m6==1)
        if (data_corrmat)
            warning_box('Unable to proceed: algorithm needs the original dataset');
            set(handles.reduction_form,'Pointer','arrow');
            return;
        end
        [n p] = size(handles.loaded_data);
        if ~(n>p)
            warning_box('Unable to proceed: in order to calculate VIF the N dimension must be greater than the P dimension');
            set(handles.reduction_form,'Pointer','arrow');
            return;
        end
        [handles.result_ranking handles.result_values handles.result_cutoff] = method_R2(handles.loaded_data);        
        handles.cur_expl = 'VIF Method (''value'' field stands for VIF value) ';
    elseif (m7==1)
        if (data_corrmat)
            warning_box('Unable to proceed: algorithm needs the original dataset');
            set(handles.reduction_form,'Pointer','arrow');
            return;
        end
        [n p] = size(handles.loaded_data);
        if ~(n>p)
            warning_box('Unable to proceed: in order to calculate VIF the N dimension must be greater than the P dimension');
            set(handles.reduction_form,'Pointer','arrow');
            return;
        end
        [handles.result_ranking handles.result_values handles.result_cutoff] = method_R2(handles.loaded_data,'i');        
        handles.cur_expl = 'VIF Method (''value'' field stands for VIF value) ';
    elseif (m8==1)
        [handles.result_ranking handles.result_values] = method_firsteig(handles.loaded_data,data_corrmat,'i');        
        handles.cur_expl = 'FirstEig iterative Method (''value'' field stands for the loading in the 1st PC) ';
         handles.result_cutoff = -999;
    elseif (m9==1)
        [handles.result_ranking handles.result_values handles.result_cutoff] = method_KIF(handles.loaded_data,data_corrmat,'n','n');
        handles.cur_expl = 'KIF Method non-iterative(''value'' field stands for the KIF value) ';
    elseif (m10==1)
        [handles.result_ranking handles.result_values handles.result_cutoff] = method_simplecorr(handles.loaded_data,data_corrmat);
        handles.cur_expl = 'Simple Correlation Method (''value'' field stands for the mean correlation value) ';
    end

    % Calculate the entropy value for each var
    [n p] = size(handles.loaded_data);
    if (data_corrmat)
        handles.result_entropy = [];
        for idx=1:p
           handles.result_entropy(idx) = NaN;
        end
    else
        var_entropy = [];
        for idx=1:p
           var_entropy = [var_entropy entropy(handles.loaded_data(:,idx))];
        end        handles.result_entropy = [];
        for idx=1:p
           handles.result_entropy(idx) = var_entropy(handles.result_ranking(idx));
        end
    end
    
    set(handles.reduction_form,'Pointer','arrow');

    % Update handles structure
    guidata(hObject, handles);

    exec_output(handles);
    
end


% --------------------------------------------------------------------
function save_txt_checkbox_Callback(hObject, eventdata, handles)% Executes on button press in save_txt_checkbox.
% hObject    handle to save_txt_checkbox (see GCBO)% eventdata  reserved - to be defined in a future version of MATLAB% handles    structure with handles and user data (see GUIDATA)% Hint: get(hObject,'Value') returns toggle state of save_txt_checkbox
if (get(hObject,'Value') == get(hObject,'Max'))
    set(handles.save_txt_button,'Enable','on');
else
    set(handles.save_txt_button,'Enable','off');
end
    
% --------------------------------------------------------------------

function save_mat_checkbox_Callback(hObject, eventdata, handles)% Executes on button press in save_mat_checkbox.
% hObject    handle to save_mat_checkbox (see GCBO)% eventdata  reserved - to be defined in a future version of MATLAB% handles    structure with handles and user data (see GUIDATA)% Hint: get(hObject,'Value') returns toggle state of save_mat_checkboxif (get(hObject,'Value') == get(hObject,'Max'))
    set(handles.save_mat_button,'Enable','on');
else
    set(handles.save_mat_button,'Enable','off');
end

% --------------------------------------------------------------------
function save_txt_edit_Callback(hObject, eventdata, handles)% hObject    handle to save_txt_edit (see GCBO)% eventdata  reserved - to be defined in a future version of MATLAB% handles    structure with handles and user data (see GUIDATA)% Hints: get(hObject,'String') returns contents of save_txt_edit as text%        str2double(get(hObject,'String')) returns contents of save_txt_edit as a double
% --------------------------------------------------------------------

function save_txt_button_Callback(hObject, eventdata, handles)% Executes on button press in save_txt_button.
% hObject    handle to save_txt_button (see GCBO)% eventdata  reserved - to be defined in a future version of MATLAB% handles    structure with handles and user data (see GUIDATA)
% Gets the path and filename from the standard file dialog
[cur_filename,cur_pathname] = uiputfile('result.txt','Save output as text');

if (cur_filename==0)
    cur_txt_file = ' ';
else
    cur_txt_file = strcat(cur_pathname,cur_filename);  
end

set(handles.save_txt_edit,'String',cur_txt_file);

handles.txt_file = cur_txt_file;

% Update handles structure
guidata(hObject, handles);


% --------------------------------------------------------------------
function save_mat_edit_Callback(hObject, eventdata, handles)% hObject    handle to save_mat_edit (see GCBO)% eventdata  reserved - to be defined in a future version of MATLAB% handles    structure with handles and user data (see GUIDATA)% Hints: get(hObject,'String') returns contents of save_mat_edit as text%        str2double(get(hObject,'String')) returns contents of save_mat_edit as a double
% --------------------------------------------------------------------

function save_mat_button_Callback(hObject, eventdata, handles)% Executes on button press in save_mat_button.
% hObject    handle to save_mat_button (see GCBO)% eventdata  reserved - to be defined in a future version of MATLAB% handles    structure with handles and user data (see GUIDATA)
% Gets the path and filename from the standard file dialog
[cur_filename,cur_pathname] = uiputfile('result.mat','Save output as mat file');

if (cur_filename==0)
    cur_mat_file = ' ';
else
    cur_mat_file = strcat(cur_pathname,cur_filename);  
end

set(handles.save_mat_edit,'String',cur_mat_file);

handles.mat_file = cur_mat_file;

% Update handles structure
guidata(hObject, handles);

% --------------------------------------------------------------------


function clear_data_button_Callback(hObject, eventdata, handles)% Executes on button press in clear_data_button.
% hObject    handle to clear_data_button (see GCBO)% eventdata  reserved - to be defined in a future version of MATLAB% handles    structure with handles and user data (see GUIDATA)set(handles.loaded_data_text,'String','');
handles.data_present=0;

% Update handles structure
guidata(hObject, handles);


% --------------------------------------------------------------------

function clear_headers_button_Callback(hObject, eventdata, handles)% Executes on button press in clear_headers_button.
% hObject    handle to clear_headers_button (see GCBO)% eventdata  reserved - to be defined in a future version of MATLAB% handles    structure with handles and user data (see GUIDATA)set(handles.loaded_headers_text,'String','');
handles.headers_present=0;
% Update handles structure
guidata(hObject, handles);

% --------------------------------------------------------------------

function view_res_checkbox_Callback(hObject, eventdata, handles)% Executes on button press in view_res_checkbox.
% hObject    handle to view_res_checkbox (see GCBO)% eventdata  reserved - to be defined in a future version of MATLAB% handles    structure with handles and user data (see GUIDATA)% Hint: get(hObject,'Value') returns toggle state of view_res_checkbox

% --------------------------------------------------------------------


function corrmat_checkbox_Callback(hObject, eventdata, handles)
% Executes on button press in corrmat_checkbox.
% hObject    handle to corrmat_checkbox (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hint: get(hObject,'Value') returns toggle state of corrmat_checkbox

if (get(hObject,'Value') == get(hObject,'Max'))
    set(handles.radio_method_6,'Enable','off');
    set(handles.radio_method_7,'Enable','off');
else
    set(handles.radio_method_6,'Enable','on');
    set(handles.radio_method_7,'Enable','on');
end




%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% Other code
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


function mutual_exclude(off)
% Implements the mutual exclusion on the radio buttons
% off   array of handlers of the radio buttons

set(off,'Value',0);


% --------------------------------------------------------------------


function exec_output(handles)
% Produces all the outputs
% handles   'handles' var of the form

ranking = handles.result_ranking;
values = handles.result_values;
H = handles.result_entropy;
if (handles.result_cutoff == -999)
    cutoff = 'n.a.';
else
    cutoff = num2str(handles.result_cutoff);
end

if (handles.headers_present)
    unordered_headers = handles.loaded_headers;
    for idx=1:length(unordered_headers)
        headers(idx) = unordered_headers(ranking(idx));
    end
end    


% Output for the TXT file

if (get(handles.save_txt_checkbox,'Value') == get(handles.save_txt_checkbox,'Max'))
    try
        [file_id mess] = fopen(handles.txt_file, 'wt');
        if (file_id==-1)
            warning_box('Error opening the output file');
            return;
        end
        fprintf(file_id,'Variables reduction - variables elimination ranking\n');
        fprintf(file_id,'%s\n',handles.cur_expl);
        fprintf(file_id,'Number of variable to retain: %s\n\n',cutoff);
    
        if (handles.headers_present)

            % Output with var headers
            
            % Formats the headers
            tmp_h = [{'Name'} headers];
            [tmp_h max_len] = strings_format(tmp_h, 4, 35);
            headers_title = tmp_h{1};
            headers = tmp_h(2:end);
            
            % Writes all to the output file
            
            fprintf(file_id,' Rank   Var\t%s\tValue \tstd.H\n',headers_title);
            fprintf(file_id,'-----+--------------------------------------------------\n');
            for i=1:length(ranking)
                fprintf(file_id,'%4.0d | %4.0d\t%s\t%2.3f\t%1.3f\n',i,ranking(i), ...
                    headers{i},values(i),H(i));
            end
            
        else
        
            % Output without var headers
            
            fprintf(file_id,' Rank   Var\tValue \tH\n');
            fprintf(file_id,'-----+------------------------------\n');
            for i=1:length(ranking)
                fprintf(file_id,'%4.0d | %4.0d\t%2.3f\t%1.3f\n',i,ranking(i),values(i),H(i));
            end
            
        end    
    
        fclose(file_id);
    catch
        fclose('all');
        warning_box('Error while writing the output file');
    end
end


% Output for the MAT file

if (get(handles.save_mat_checkbox,'Value') == get(handles.save_mat_checkbox,'Max'))
    if (handles.headers_present)
        save(handles.mat_file,'ranking','values','H','headers');
    else
        save(handles.mat_file,'ranking','values','H');
    end
end


% Output for the ARRAY EDITOR

if (get(handles.view_res_checkbox,'Value') == get(handles.view_res_checkbox,'Max'))
    assignin('base','result_ranking',ranking');
    assignin('base','result_values',values');
    assignin('base','result_H',H');
    openvar('result_ranking');
    openvar('result_values');
    openvar('result_H');
    if (handles.headers_present)
        assignin('base','result_headers',headers');
        openvar('result_headers');        
    end
end


% --------------------------------------------------------------------


function show_correlation_matrix(X);
% Calculates and shows the correlation matrix for the given dataset
% X     dataset (m x n matrix)

[n,p] = size(X);

% Autoscaling of X
X = (X-(ones(n,1)*mean(X))) ./ (ones(n,1)*std(X));

% Calculates the correlation matrix
C = (X'*X) / (n-1);

% Shows the resulting correlation matrix
assignin('base','result_corr_matrix',C);
openvar('result_corr_matrix');

    
% --------------------------------------------------------------------
    
    
function hBox = warning_box(cur_msg)
% Calls the msgbox function to show a warning
% cur_msg   message string

hBox = msgbox({cur_msg});

⌨️ 快捷键说明

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