image thumbnail

SimpleScores

by

 

17 Jul 2012 (Updated )

Plots the PCA scores using the princomp(X) function. Loads files direct from .csv.

SimpleScores(varargin)
% ------------------------------------------------------------------------
% Simple Scores Program
% Author: Giuseppe Elia
% Version: 1.2
% Version Control;
%   V1:     Initial Release
%   V1.1:   1.Added exception handling. Program now recognises number of PC and
%           does not let you try and calculate over the number of
%           variables contained in data.
%           2.Added maximum number of components counter.
%           3.Added function to plot raw data
%   V1.2:   Corrected Scores on x axis
% Release Date: May 2011
% ------------------------------------------------------------------------

function varargout = SimpleScores(varargin)
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @SimpleScores_OpeningFcn, ...
                   'gui_OutputFcn',  @SimpleScores_OutputFcn, ...
                   'gui_LayoutFcn',  [] , ...
                   'gui_Callback',   []);
if nargin && ischar(varargin{1})
    gui_State.gui_Callback = str2func(varargin{1});
end

if nargout
    [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
    gui_mainfcn(gui_State, varargin{:});
end

function SimpleScores_OpeningFcn(hObject, ~, handles, varargin)
handles.output = hObject;
guidata(hObject, handles);
set(handles.figure1,'CloseRequestFcn',@closeGUI);

function varargout = SimpleScores_OutputFcn(~, ~, handles) 
varargout{1} = handles.output;

function Plot_Data_Button_Callback(~, ~, handles)

clc

% ------------------------------------------------------------------------
% Main
% ------------------------------------------------------------------------

A = get(SimpleScores, 'UserData'); % get data from load button

PCY_a = get(handles.PCY,'Value');
PCY_b = (PCY_a);

PCX_a = get(handles.PCX,'Value');
PCX_b = (PCX_a);


[size_a, size_b] = size(A);
if size_b == 0
    msgbox(sprintf('No data loaded, load some data first!'),'WARNING');
    return
end

[nt mt] = size(A); % Find size of matrix
    
for i=1:nt
    sample(i,:)=['  ',sprintf('%04.0f',i)]; % generic label - sample number. insert text into 'here' to label
end

clear i;

% get functions
scores_a = get(handles.scores_1,'Value');
scores_b = (scores_a);

[nt mt] = size(A); % find size of matrix

% basic stats
AMean = mean((A(1:nt,1:mt))); % mean
AStd = std(A(1:nt,1:mt)); % std dev

A = (A(1:nt,1:mt));

As = (A - repmat(AMean,[nt 1])) ./ repmat(AStd,[nt 1]); % matrix As is the standardised data                    

AMean_Column_Flip = AMean'; % mean of variable

AStd_Column_Flip = AStd'; % standard deviation of variables                                                

% ------------------------------------------------------------------------
% Calculate the principal components
% ------------------------------------------------------------------------

P = corrcoef(As); % matrix 'P' is the matrix of correlation coefficients                                                         

[coefs,scores,variances,t2] = princomp(A);

[U D V] = svd(P); % matrix 'V' = matrix of eigenvectors, 'D' = matrix of  eigenvalues                                                  

sum_of_diagonal_D = sum(diag(D)); % sum of eigenvalues in matrix 'D'                                 

values_of_principle_components = ((diag(D)/sum_of_diagonal_D) * 100); % values of principal components     

PC = mt; % evaluating the scores for first 2 PC will show greatest changes                                                                     

t = As*V(:,1:PC); % score of the PCA model using 'PC' principal components                                                     

S = cov(t); % covariance matrix  

% Build Matrices
if PCX_a > mt
    msgbox(sprintf('Too many ''X'' components selected.'),'WARNING');
else
    x = t(1:nt,PCX_b);
if PCY_a > mt
    msgbox(sprintf('Too many ''Y'' components selected.'),'WARNING');
else
    y = t(1:nt,PCY_b);

database_scores = [x, y];

% ------------------------------------------------------------------------
% Plots
% ------------------------------------------------------------------------

if scores_b == 1;
figure(1)
x=-x; % flip values of x

        plot(x,y,'+'); 
       
        text(x,y,sample); 
    
        hold;
        x=get(gca,'XLim'); y=get(gca,'YLim');
        plot(x,zeros(size(x)),'g'); plot(zeros(size(y)),y,'r');
        
        xlabel_a = 'Principal Component';
        xlabel_b = num2str(PCX_b);
        title_comp_x = strcat(xlabel_a,':', xlabel_b);
        xlabel(title_comp_x);
        
        ylabel_a = 'Principal Component';
        ylabel_b = num2str(PCY_b);
        title_comp_y = strcat(ylabel_a,':',ylabel_b);
        ylabel(title_comp_y);
        
        title_A = 'PC';
        title_B = num2str(PCX_b);
        title_C = ' v';
        title_D = ' PC';
        title_E = num2str(PCY_b);
        title_comp = strcat(title_A,title_B,title_C,title_D,title_E);
        title(title_comp); 
        
        hold off; figure(gcf);
        clc;
        
else
end
end
end

function scores_1_Callback(hObject, eventdata, handles)

function Load_2D_Data_Button_Callback(hObject, eventdata, handles)

% load file...
[FileName,PathName] = uigetfile({'*.csv';'*.asc'},'Simple Scores Data Loader. Select file to load...');
NAME = [PathName, FileName];

if PathName == 0 % if cancel then exit
    msgbox(sprintf('You did not select a file to load!'),'WARNING');
    return
end

fid=load(NAME); A = fid; % 'A' is the dataset

% Wait Time
h = waitbar(0,'Please Wait....','Name','Data Loading');
steps = 50;

for step = 1:steps
    waitbar(step / steps)
end

close(h)

[nt mt] = size(A); % find size of matrix
set(handles.max_y,'String',mt);
set(handles.max_x,'String',mt);
set(SimpleScores, 'UserData', A)

function plot_data_line_Callback(hObject, eventdata, handles)
A = get(SimpleScores, 'UserData'); % get data from load button

[size_a, size_b] = size(A);
if size_b == 0
    msgbox(sprintf('No data loaded, load some data first!'),'WARNING');
    return
end

plot (A);
title('Dataset');
xlabel('Data Row');
ylabel('Value');
figure(gcf);

function Exit_Button_Callback(hObject, eventdata, handles)
selection = questdlg('Do you want to close Simple Scores?',...
                     'Close Simple Scores',...
                     'Yes','No','Yes');
switch selection,
   case 'Yes',
    delete(gcf);
    close all force;
   case 'No'
     return
end
function PCX_Callback(hObject, eventdata, handles)

function PCX_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end
function PCY_Callback(hObject, eventdata, handles)

function PCY_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end

function Labels_in_First_Row_Callback(hObject, eventdata, handles)

function Untitled_1_Callback(hObject, eventdata, handles)
% file menu

function author_Callback(hObject, eventdata, handles)
web('http://uk.linkedin.com/pub/giuseppe-elia/13/544/486/')

function exit_Callback(hObject, eventdata, handles)
selection = questdlg('Do you want to close Simple Scores?',...
                     'Close Simple Scores',...
                     'Yes','No','Yes');
switch selection,
   case 'Yes',
       clc;
       close force;
    delete(gcf);
   case 'No'
     return
end

function closeGUI(src,evnt)
selection = questdlg('Do you want to close Simple Scores?',...
                     'Close Simple Scores',...
                     'Yes','No','Yes');
switch selection,
   case 'Yes',
       clc;
       close force;
    delete(gcf);
   case 'No'
     return
end

function max_y_Callback(hObject, eventdata, handles)

function max_y_CreateFcn(hObject, eventdata, handles)

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end

function max_x_Callback(hObject, eventdata, handles)

function max_x_CreateFcn(hObject, eventdata, handles)

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end

Contact us