Code covered by the BSD License  

Highlights from
Owlstone Lonestar PLS Regression Analysis

image thumbnail

Owlstone Lonestar PLS Regression Analysis

by

 

Program to perform PLS regression analysis on Owlstone's Lonestar FAIMS analyser.

FAIMS_OFFLINE(varargin)
function varargout = FAIMS_OFFLINE(varargin)

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @FAIMS_OFFLINE_OpeningFcn, ...
                   'gui_OutputFcn',  @FAIMS_OFFLINE_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

% End initialization code - DO NOT EDIT


% --- Executes just before FAIMS_OFFLINE is made visible.
function FAIMS_OFFLINE_OpeningFcn(hObject, eventdata, handles, varargin)

handles.output = hObject;

% Update handles structure
guidata(hObject, handles);

clc;

% --- Outputs from this function are returned to the command line.
function varargout = FAIMS_OFFLINE_OutputFcn(hObject, eventdata, handles) 

varargout{1} = handles.output;


% --- Executes on button press in load_positive.
function load_positive_Callback(hObject, eventdata, handles)

set(handles.text5, 'String', 'Select the last file FIRST and the first file LAST (in reverse order to the corresponding ''y'' data).');

fclose('all'); % Close all open files
        
%%% Load positive file %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
[FileName,PathName] = uigetfile({'*.asc';'*.txt'},'Select x files to load...','MultiSelect', 'on');

if iscell(FileName)
   
[size_a, size_b] = size(FileName);
set(handles.load_positive, 'UserData',size_b);

if size_b == 1
    set(handles.text5, 'String', 'No data loaded, load some data first.');
    return
end

NAME = [PathName, FileName];

[PathName_Size, FileName_Size] = size(FileName);

set(handles.load_positive,'UserData',FileName_Size);
h = waitbar(0,['Please Wait.... ','Data Loading.']);
for j = 1:FileName_Size
    fclose('all'); % Close all open files

%%% Computation %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
waitbar(j / FileName_Size,h, ['Data Loading. Sample: ',num2str(j)] )

global jcross_validate;
jcross_validate = floor(((j/10)*9)-1);

set(handles.max_samples, 'String', j);
set(handles.max_comp_gui, 'String', jcross_validate);
NAME = [PathName, FileName{1,j}(1,:)];

fid = fopen(NAME,'r');

DATA=[];

while(1)
    
    line = fgetl(fid); % read line by line

if feof(fid), break, end;

if(strcmp(line, '// Positive Ion Current'))
    
    for i=1:1000; % end of line
    line = fgetl(fid);
    if line == -1
    elseif (strcmp(line, '// Negative Ion Current'))
        break;
    else
    DATA = [DATA;textscan(line, '%s', 'BufSize', 20000,'EmptyValue', -Inf)]; % original
    end
    end    

bs=[1,1];
m=cat(1,DATA{:});
data_double = str2double(m);
AllData(j,:)=data_double;

end
end
    
    fclose(fid); % Close all open files
end
close(h)

AllData_exist = exist('AllData');
if AllData_exist == 1
AllData_trans = AllData';
AllData_trans(isnan(AllData_trans(:,1)),:)=[]; % delete empty values

% save LAST_X_DATA.mat AllData_trans; %plots

figure();
plot(AllData_trans);
title('''x'' Data');
% AllData_trans
set(FAIMS_OFFLINE, 'UserData', AllData_trans);
        set(handles.text5, 'String', 'x Data Loaded.');
else

        set(handles.text5, 'String', 'No data loaded, load some data first. Check acquisition parameters.');
    return
end
end


% --- Executes on button press in load_y.
function load_y_Callback(hObject, eventdata, handles)

[FileName_y,PathName_y] = uigetfile('*.csv','Load y data file...');
NAME_y = [PathName_y, FileName_y]; %path and name

if PathName_y == 0 % if cancelled
    return
end

fid_y=load(NAME_y); % open the file (as a struct)

load_y = fid_y;

set(handles.load_y, 'UserData', load_y);
set(handles.text5, 'String', 'y Data Loaded.');


% --- Executes on button press in percent_variance_explained_in_y.
function percent_variance_explained_in_y_Callback(hObject, eventdata, handles)

AllData_trans = get(FAIMS_OFFLINE, 'UserData'); % get data from load button
AllData = AllData_trans';

FileName_Size_for_y = get(handles.load_positive, 'UserData');

[size_a, size_b] = size(AllData_trans);

if size_b == 1
    set(handles.text5, 'String', 'No data loaded, load some data first.');
    return
end
if size_b < 10
    set(handles.text5, 'String', 'At least 10 samples are required to build the calibration.');
    return
end


%% Set Demo Edition
% if size_b > 10
%     set(handles.text5, 'String', 'Demo edition limited to 10 reference samples. Please contact author for full version.');
%     return
% end


%% PLS %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
z = get(handles.load_y, 'UserData');
z2 = get(handles.load_positive, 'UserData');
[nt_1 mt_1] = size(z);
y = z;

[nt_1 mt_1] = size(z);
y = z;
y_emp=isempty(y);
z2_emp=isempty(z2);
if y_emp == 1
    set(handles.text5, 'String', 'Load ''y'' Data.');
else
    
if z2_emp == 1
    set(handles.text5, 'String', 'Load ''x'' Data.');
else

y= y(1:1:FileName_Size_for_y);

nocomponents1 = get(handles.number_of_components, 'String');
nocomponents = str2num(nocomponents1);

X=AllData;

[AllData_x AllData_y] = size(AllData);

AllData_xb=AllData_x-1;
            
if nocomponents >= AllData_x
    set(handles.text5, 'String', 'Too many components selected.');
return
end

if nocomponents <= 0
    set(handles.text5, 'String', 'Too few components selected. Must be >= 1.');
return
end

if str2num(nocomponents1) <= AllData_xb

global jcross_validate;
if nocomponents > jcross_validate
set(handles.text5, 'String', 'Too many components selected to run cross validation, please choose <= the maximum number of components for cross validation.');
return
end

%%% using cross validation
[Xl,Yl,Xs,Ys,beta,pctVar,PLSmsep] = plsregress(X,y,nocomponents,'CV',10); % SIMPLS algorithm, first centering X and Y. estimate the mean squared prediction error (MSEP) by cross-validation, in this case using 10-fold C-V.

figure()
plot(0:nocomponents,PLSmsep(2,:),'b-o');
title('Mean squared error of prediction (MSEP)');
xlabel('Number of components');
ylabel('Estimated Mean Squared Prediction Error');

%%

% K-fold cross-validationIn k-fold cross-validation, the original sample is randomly partitioned into k subsamples. Of the k subsamples, a single subsample is retained as the validation data for testing the model, and the remaining k ? 1 subsamples are used as training data. The cross-validation process is then repeated k times (the folds), with each of the k subsamples used exactly once as the validation data. The k results from the folds then can be averaged (or otherwise combined) to produce a single estimation. The advantage of this method over repeated random sub-sampling is that all observations are used for both training and validation, and each observation is used for validation exactly once. 10-fold cross-validation is commonly used[5], but in general k remains an unfixed parameter [1].
% 
% In stratified k-fold cross-validation, the folds are selected so that the mean response value is approximately equal in all the folds. In the case of a dichotomous classification, this means that each fold contains roughly the same proportions of the two types of class labels.

%%%%

%%

for i = 1:AllData_x
    
    result=[1 (X(i,1:AllData_y))]*beta;
    
    % append result to matrix
	result_matrix(i) = result;
    result_matrix2=result_matrix';
    myVector_result = 1:1:mt_1;
    myVector(i) = result;
    myVector_result = 1:1:mt_1;

end
     
for i = 1:1:mt_1
    
    dat={
        myVector(1,i), z(1,i);
        };

data = 5;
newdata = cell(size(data)+[1,0]);
   
end

difference = result_matrix2 - y;
dat=[y,result_matrix2,difference];
set(handles.uitable1,'Data',dat);
createfigure(y,result_matrix2,'black','black')

set(handles.percent_variance_explained_in_y, 'UserData', beta);

else
    set(handles.text5, 'String', 'Number of components must be less than number of data points.');
end
end
end


% --- Executes on button press in observations_residuals.
function observations_residuals_Callback(hObject, eventdata, handles)

AllData_trans = get(FAIMS_OFFLINE, 'UserData'); % get data from load button
AllData = AllData_trans';

FileName_Size_for_y = get(handles.load_positive, 'UserData');

[size_a, size_b] = size(AllData_trans);

if size_b == 1
    set(handles.text5, 'String', 'No data loaded, load some data first.');
    return
end

%%% PLS %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
z = get(handles.load_y, 'UserData');
z2 = get(handles.load_positive, 'UserData');
[nt_1 mt_1] = size(z);
y = z;

[nt_1 mt_1] = size(z);
y = z;
y_emp=isempty(y);
z2_emp=isempty(z2);
if y_emp == 1
    set(handles.text5, 'String', 'Load ''y'' Data.');
else
    
if z2_emp == 1
    set(handles.text5, 'String', 'Load ''x'' Data.');
else
    
y= y(1:1:FileName_Size_for_y);

nocomponents1 = get(handles.number_of_components, 'String');
nocomponents = str2num(nocomponents1);

X=AllData;
[AllData_x AllData_y] = size(AllData);
nocomponents1;

AllData_xb=AllData_x-1;

if str2num(nocomponents1) <= AllData_xb

    [XL,YL,XS,YS, beta, PctVar, MSE, stats]=plsregress(X,y,nocomponents); % SIMPLS algorithm, first centering X and Y.

for i = 1:AllData_x
    
    result=[1 (X(i,1:AllData_y))]*beta;
    
end

set(FAIMS_OFFLINE, 'UserData', AllData_trans)

figure()
yfit = [ones(size(X,1),1) X]*beta;
residuals = y-yfit;
stem(residuals)
xlabel('Observation');
ylabel('Residual');

else
    set(handles.text5,'String', 'Number of components must be less than number of data points.');
end
end
end


function number_of_components_Callback(hObject, eventdata, handles)


% --- Executes during object creation, after setting all properties.
function number_of_components_CreateFcn(hObject, eventdata, handles)

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


% --- Executes on button press in checkbox1.
function checkbox1_Callback(hObject, eventdata, handles)


% --- Executes on button press in checkbox2.
function checkbox2_Callback(hObject, eventdata, handles)


% --- Executes on button press in difference.
function difference_Callback(hObject, eventdata, handles)

AllData_trans = get(FAIMS_OFFLINE, 'UserData'); % get data from load button

AllData = AllData_trans';

FileName_Size_for_y = get(handles.load_positive, 'UserData');

[size_a, size_b] = size(AllData_trans);

if size_b == 1
set(handles.text5, 'String', 'No data loaded, load some data first.');
    return
end

%%% PLS %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
z = get(handles.load_y, 'UserData');
z2 = get(handles.load_positive, 'UserData');
[nt_1 mt_1] = size(z);
y = z;

[nt_1 mt_1] = size(z);
y = z;
y_emp=isempty(y);
z2_emp=isempty(z2);
if y_emp == 1
    set(handles.text5, 'String', 'Load ''y'' Data.');
else
    
if z2_emp == 1
    set(handles.text5, 'String', 'Load ''x'' Data.');
else
    
y= y(1:1:FileName_Size_for_y);

nocomponents1 = get(handles.number_of_components, 'String');
nocomponents = str2num(nocomponents1);

X=AllData;

[AllData_x AllData_y] = size(AllData);

AllData_xb=AllData_x-1;

if str2num(nocomponents1) <= AllData_xb

[XL,YL,XS,YS, beta, PctVar, MSE, stats]=plsregress(X,y,nocomponents); % uses the SIMPLS algorithm, first centering X and Y.

for i = 1:AllData_x
    
    result=[1 (X(i,1:AllData_y))]*beta;
    
    % append result to matrix
	result_matrix(i) = result;
    result_matrix2=result_matrix';
    myVector_result = 1:1:mt_1;
    myVector(i) = result;

myVector_result = 1:1:mt_1;

end
     
for i = 1:1:mt_1
    
    dat={
        myVector(1,i), z(1,i);
        };

data = 5;
newdata = cell(size(data)+[1,0]);
   
end

difference = result_matrix2 - y;
dat=[y,result_matrix2,difference];
dat1=[y,result_matrix2];
set(handles.uitable1,'Data',dat);

figure ()
plot(dat1)
title('Variation: Actual v Predicted');
xlabel('Data Set Number');
ylabel('Value');
legend('Actual Data','Predicted Data');

end
end
end


% --- Executes on button press in variation_in_y.
function variation_in_y_Callback(hObject, eventdata, handles)

AllData_trans = get(FAIMS_OFFLINE, 'UserData'); % get data from load button
AllData = AllData_trans';

FileName_Size_for_y = get(handles.load_positive, 'UserData');

[size_a, size_b] = size(AllData_trans);

if size_b == 1
set(handles.text5, 'String', 'No data loaded, load some data first.');
    return
end

%%% PLS %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
z = get(handles.load_y, 'UserData');
z2 = get(handles.load_positive, 'UserData');
[nt_1 mt_1] = size(z);
y = z;

[nt_1 mt_1] = size(z);
y = z;
y_emp=isempty(y);
z2_emp=isempty(z2);
if y_emp == 1
    set(handles.text5, 'String', 'Load ''y'' Data.');
else
    
if z2_emp == 1
    set(handles.text5, 'String', 'Load ''x'' Data.');
else
    
y= y(1:1:FileName_Size_for_y);
nt_1;

nocomponents1 = get(handles.number_of_components, 'String');
nocomponents = str2num(nocomponents1);

X=AllData;

[AllData_x AllData_y] = size(AllData);

AllData_xb=AllData_x-1;

if str2num(nocomponents1) <= AllData_xb

[XL,YL,XS,YS, beta, PctVar, MSE, stats]=plsregress(X,y,nocomponents); % uses the SIMPLS algorithm, first centering X and Y.

for i = 1:AllData_x
    
    result=[1 (X(i,1:AllData_y))]*beta;
    
    % append result to matrix
	result_matrix(i) = result;
    result_matrix2=result_matrix';
    myVector_result = 1:1:mt_1;
    myVector(i) = result;

myVector_result = 1:1:mt_1;

end
     
for i = 1:1:mt_1
    
    dat={
        myVector(1,i), z(1,i);
        };

data = 5;
newdata = cell(size(data)+[1,0]);
   
end

difference = result_matrix2 - y;
dat=[y,result_matrix2,difference];
dat1=[y,result_matrix2];
set(handles.uitable1,'Data',dat);

figure ()
plot(1:nocomponents,cumsum(100*PctVar(2,:)),'-bo');
xlabel('Number of PLS components');
ylabel('Percent Variance Explained in y');

end
end
end


% --- Executes on button press in Load_Spectra_single_positive.
function Load_Spectra_single_positive_Callback(hObject, eventdata, handles)

beta = get(handles.percent_variance_explained_in_y, 'UserData');
beta_emp=isempty(beta);

if beta_emp == 1
    set(handles.text5,'String','Process a model first.');
    return
else
end
    
set(handles.text5,'String','Unseen data. Re-process model after changing the number of components.');

[FileName,PathName] = uigetfile({'*.asc';'*.txt'},'FAIMS data loader. Select file to load...','MultiSelect', 'off');

NAME = [PathName, FileName];

[PathName_Size, FileName_Size] = size(FileName);

[size_a, size_b] = size(FileName);


if size_b == 1
    set(handles.text5,'String','No data loaded, load some data first.');
    return
end

for j = 1:1
    
    h = waitbar(0,['Please Wait....','FAIMS Data Loading ',num2str(j)]);
    steps = j;
    
for step = 1:j
    
    waitbar(step / steps)

    NAME = [PathName, FileName];

    fid = fopen(NAME,'r'); % open file

    DATA=[];

while(1)
    
    line = fgetl(fid); %read line by line
    
if feof(fid), break, end; %until the end of file
if(strcmp(line, '// Positive Ion Current')) % // ion current values // Positive Ion Current
    
    for i=1:1000; % change 4 to end of line
    line = fgetl(fid);
    if line == -1
    elseif (strcmp(line, '// Negative Ion Current')) % Negative ion current mode amplitude matrix // Negative Ion Current
        break;
    else
%     DATA = [DATA;textscan(fid, '%s', 'BufSize', 20000, 'delimiter', ',','EmptyValue', -Inf)];
    DATA = [DATA;textscan(line, '%s', 'BufSize', 20000,'EmptyValue', -Inf)]; % original
    end
    end
    
end
end

cs=size(DATA);
bs=[1,1];
m=cat(1,DATA{:});

data_double = str2double(m);

AllData=data_double;
AllData(isnan(AllData(:,1)),:)=[]; % delete empty values

end
    close(h)
end

beta = get(handles.percent_variance_explained_in_y, 'UserData');
beta_emp=isempty(beta);

if beta_emp == 1
    set(handles.text5,'String','Process a model first.');
else
    
X1=AllData';

[AllData1_x AllData1_y] = size(X1);
[beta_x beta_y] = size(beta);
[X_x X_y] = size(X1);
beta_x = beta_x-1;

result=[1 (X1(1:beta_x))]*beta;

set(handles.single_result_pos, 'String', result);

end


function single_result_pos_Callback(hObject, eventdata, handles)

% --- Executes during object creation, after setting all properties.
function single_result_pos_CreateFcn(hObject, eventdata, handles)

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


% --- Executes on button press in pushbutton7.
function pushbutton7_Callback(hObject, eventdata, handles)


% --- Executes on button press in pushbutton8.
function pushbutton8_Callback(hObject, eventdata, handles)


% --------------------------------------------------------------------
function Untitled_1_Callback(hObject, eventdata, handles)


% --------------------------------------------------------------------
function load_model_Callback(hObject, eventdata, handles)

%%% Load Settings %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
[filename, pathname] = uigetfile('*.fig', 'Choose a model to load');
 
loadDataName = fullfile(pathname,filename);

theCurrentGUI = gcf;  
 
hgload(loadDataName); 
 
close(theCurrentGUI);


% --------------------------------------------------------------------
function save_model_Callback(hObject, eventdata, handles)

%%% Save Settings %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
[filename,pathname] = uiputfile('default','Save your settings');
 
if pathname == 0 %if cancelled
    return
end

saveDataName = fullfile(pathname,filename); 

hgsave(saveDataName);


% --------------------------------------------------------------------
function Untitled_4_Callback(hObject, eventdata, handles)


% --------------------------------------------------------------------
function Untitled_7_Callback(hObject, eventdata, handles)

winopen('Help.chm');

% --------------------------------------------------------------------
function Untitled_8_Callback(hObject, eventdata, handles)

set(handles.text5, 'String', 'Owlstone Lonestar FAIMS PLS regression analysis v1.0 with 10-fold-cross-validation. ------------------------------------ Developed by Giuseppe Elia.');

% --------------------------------------------------------------------
function exit_Callback(hObject, eventdata, handles)

selection = questdlg('Do you really wish to Exit?',...
                     'Close Program',...
                     'Yes','No','Yes');
                 
switch selection,
   case 'Yes',
    delete(gcf)
    close all force;
   case 'No'
     return
end

function createfigure(X1, Y1, S1,C1)

figure1 = figure;

axes1 = axes('Parent',figure1);
hold(axes1,'all');

scatter1 = scatter(X1,Y1,S1,C1,'Marker','x','Parent',axes1,...
    'DisplayName','data 1');

title('Fitted vs. Observed Response');
xlabel('Observed Response');
ylabel('Fitted Response');

xdata1 = get(scatter1, 'xdata');
ydata1 = get(scatter1, 'ydata');

xdata1 = xdata1(:);
ydata1 = ydata1(:);

% Remove NaN values and warn
nanMask1 = isnan(xdata1(:)) | isnan(ydata1(:));
if any(nanMask1)
    warning('GenerateMFile:IgnoringNaNs', ...
        'Data points with NaN coordinates will be ignored.');
    xdata1(nanMask1) = [];
    ydata1(nanMask1) = [];
end

axesLimits1 = xlim(axes1);

xplot1 = linspace(axesLimits1(1), axesLimits1(2));

coeffs1 = cell(1,1);

fitResults1 = polyfit(xdata1, ydata1, 1);

yplot1 = polyval(fitResults1, xplot1);

fittypesArray1(1) = 2;

coeffs1{1} = fitResults1;

% Plot the fit

fitLine1 = plot(xplot1,yplot1,'DisplayName','   linear','Parent',axes1,...
    'Tag','linear',...
    'Color',[1 0 0]);

setLineOrder(axes1, fitLine1, scatter1);

showEquations(fittypesArray1, coeffs1, 5, axes1);


function setLineOrder(axesh1, newLine1, associatedLine1)

hChildren = get(axesh1,'Children');

hChildren(hChildren==newLine1) = [];

lineIndex = find(hChildren==associatedLine1);

hNewChildren = [hChildren(1:lineIndex-1);newLine1;hChildren(lineIndex:end)];

set(axesh1,'Children',hNewChildren);


function showEquations(fittypes1, coeffs1, digits1, axesh1)
n = length(fittypes1);
txt = cell(length(n + 1) ,1);
txt{1,:} = ' ';

for i = 1:n
    txt{i + 1,:} = getEquationString(fittypes1(i),coeffs1{i},digits1,axesh1);
end

text(.05,.95,txt,'parent',axesh1, ...
    'verticalalignment','top','units','normalized');


function [s1] = getEquationString(fittype1, coeffs1, digits1, axesh1)

if isequal(fittype1, 0)
    s1 = 'Cubic spline interpolant';
elseif isequal(fittype1, 1)
    s1 = 'Shape-preserving interpolant';
else
    op = '+-';
    format1 = ['%s %0.',num2str(digits1),'g*x^{%s} %s'];
    
    format2 = ['%s %0.',num2str(digits1),'g'];
    xl = get(axesh1, 'xlim');
    fit =  fittype1 - 1;
    s1 = sprintf('y =');
    th = text(xl*[.95;.05],1,s1,'parent',axesh1, 'vis','off');
    if abs(coeffs1(1) < 0)
        s1 = [s1 ' -'];
    end
    for i = 1:fit
        sl = length(s1);
        if ~isequal(coeffs1(i),0) % if exactly zero, skip it
            s1 = sprintf(format1,s1,abs(coeffs1(i)),num2str(fit+1-i), op((coeffs1(i+1)<0)+1));
            
            r2_data_1 = abs(coeffs1(i));

            
            
        end
        if (i==fit) && ~isequal(coeffs1(i),0)
            s1(end-5:end-2) = []; % change x^1 to x.
        end
        set(th,'string',s1);
        et = get(th,'extent');
        if et(1)+et(3) > xl(2)
            s1 = [s1(1:sl) sprintf('\n     ') s1(sl+1:end)];
        end
    end
    if ~isequal(coeffs1(fit+1),0)
        sl = length(s1);
        s1 = sprintf(format2,s1,abs(coeffs1(fit+1)));
        set(th,'string',s1);
        et = get(th,'extent');
        if et(1)+et(3) > xl(2)
            s1 = [s1(1:sl) sprintf('\n     ') s1(sl+1:end)];
        end
    end
    delete(th);
    if isequal(s1(end),'+')
        s1(end-1:end) = []; % There is always a space before the +.
    end
    if length(s1) == 3
        s1 = sprintf(format2,s1,0);
    end
   
end



function edit4_Callback(hObject, eventdata, handles)


% --- Executes during object creation, after setting all properties.
function edit4_CreateFcn(hObject, eventdata, handles)

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


% --- Executes during object creation, after setting all properties.
function difference_CreateFcn(hObject, eventdata, handles)


% --------------------------------------------------------------------
function Untitled_9_Callback(hObject, eventdata, handles)
% hObject    handle to Untitled_9 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)


% --------------------------------------------------------------------
function Untitled_10_Callback(hObject, eventdata, handles)
% hObject    handle to Untitled_10 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

set(handles.text5, 'String', '');
[FileName,PathName] = uigetfile({'*.asc';'*.txt'});

if ischar(FileName) 
 
eval(['!notepad ' PathName FileName]) 

else 

end


% --- Executes on button press in lonestar.
function lonestar_Callback(hObject, eventdata, handles)
% hObject    handle to lonestar (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 lonestar


% --- Executes on button press in nexsense.
function nexsense_Callback(hObject, eventdata, handles)
% hObject    handle to nexsense (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 nexsense


% --------------------------------------------------------------------
function parameters_Callback(hObject, eventdata, handles)
% hObject    handle to parameters (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)


% --------------------------------------------------------------------
function plots_Callback(hObject, eventdata, handles)
% hObject    handle to plots (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)


% --------------------------------------------------------------------
function truevpred_Callback(hObject, eventdata, handles)
% hObject    handle to truevpred (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
AllData_trans = get(FAIMS_OFFLINE, 'UserData'); % get data from load button
AllData = AllData_trans';

FileName_Size_for_y = get(handles.load_positive, 'UserData');

[size_a, size_b] = size(AllData_trans);

if size_b == 1
    set(handles.text5, 'String', 'No data loaded, load some data first.');
    return
end

%%% PLS %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
z = get(handles.load_y, 'UserData');
z2 = get(handles.load_positive, 'UserData');
[nt_1 mt_1] = size(z);
y = z;

[nt_1 mt_1] = size(z);
y = z;
y_emp=isempty(y);
z2_emp=isempty(z2);
if y_emp == 1
    set(handles.text5, 'String', 'Load ''y'' Data.');
else
    
if z2_emp == 1
    set(handles.text5, 'String', 'Load ''x'' Data.');
else

y= y(1:1:FileName_Size_for_y);

nocomponents1 = get(handles.number_of_components, 'String');
nocomponents = str2num(nocomponents1);

X=AllData;
[AllData_x AllData_y] = size(AllData);

AllData_xb=AllData_x-1;

if str2num(nocomponents1) <= AllData_xb

[XL,YL,XS,YS, beta, PctVar, MSE, stats]=plsregress(X,y,nocomponents); % SIMPLS algorithm, first centering X and Y.

for i = 1:AllData_x
    
    result=[1 (X(i,1:AllData_y))]*beta;
    
    % append result to matrix
	result_matrix(i) = result;
    result_matrix2=result_matrix';
    myVector_result = 1:1:mt_1;
    myVector(i) = result;
    myVector_result = 1:1:mt_1;

end
     
for i = 1:1:mt_1
    
    dat={
        myVector(1,i), z(1,i);
        };

data = 5;
newdata = cell(size(data)+[1,0]);
   
end

difference = result_matrix2 - y;
dat=[y,result_matrix2,difference];
set(handles.uitable1,'Data',dat);
createfigure(y,result_matrix2,'black','black')

set(handles.percent_variance_explained_in_y, 'UserData', beta);
        set(handles.text5, 'String', 'True v Predicted (xy).');
else
    set(handles.text5, 'String', 'Number of components must be less than number of data points.');

    
end
end
end


% --------------------------------------------------------------------
function obsvres_Callback(hObject, eventdata, handles)
% hObject    handle to obsvres (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
AllData_trans = get(FAIMS_OFFLINE, 'UserData'); % get data from load button
AllData = AllData_trans';

FileName_Size_for_y = get(handles.load_positive, 'UserData');

[size_a, size_b] = size(AllData_trans);

if size_b == 1
        set(handles.text5, 'String', 'No data loaded, load some data first.');
    return
end

%%% PLS %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
z = get(handles.load_y, 'UserData');
z2 = get(handles.load_positive, 'UserData');
[nt_1 mt_1] = size(z);
y = z;

[nt_1 mt_1] = size(z);
y = z;
y_emp=isempty(y);
z2_emp=isempty(z2);
if y_emp == 1
    set(handles.text5, 'String', 'Load ''y'' Data.');
else
    
if z2_emp == 1
    set(handles.text5, 'String', 'Load ''x'' Data.');
else
    
y= y(1:1:FileName_Size_for_y);

nocomponents1 = get(handles.number_of_components, 'String');
nocomponents = str2num(nocomponents1);

X=AllData;
[AllData_x AllData_y] = size(AllData);
nocomponents1;

AllData_xb=AllData_x-1;

if str2num(nocomponents1) <= AllData_xb

    [XL,YL,XS,YS, beta, PctVar, MSE, stats]=plsregress(X,y,nocomponents); % SIMPLS algorithm, first centering X and Y.

for i = 1:AllData_x
    
    result=[1 (X(i,1:AllData_y))]*beta;
    
end

set(FAIMS_OFFLINE, 'UserData', AllData_trans)

figure()
yfit = [ones(size(X,1),1) X]*beta;
residuals = y-yfit;
stem(residuals)
xlabel('Observation');
ylabel('Residual');
        set(handles.text5, 'String', 'Observations V Residuals.');
else
    set(handles.text5,'String', 'Number of components must be less than number of data points.');
end
end
end


% --------------------------------------------------------------------
function trupredline_Callback(hObject, eventdata, handles)
% hObject    handle to trupredline (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
AllData_trans = get(FAIMS_OFFLINE, 'UserData'); % get data from load button

AllData = AllData_trans';

FileName_Size_for_y = get(handles.load_positive, 'UserData');

[size_a, size_b] = size(AllData_trans);

if size_b == 1
    set(handles.text5,'String', 'No data loaded, load some data first.');
    return
end

%%% PLS %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
z = get(handles.load_y, 'UserData');
z2 = get(handles.load_positive, 'UserData');
[nt_1 mt_1] = size(z);
y = z;

[nt_1 mt_1] = size(z);
y = z;
y_emp=isempty(y);
z2_emp=isempty(z2);
if y_emp == 1
    set(handles.text5, 'String', 'Load ''y'' Data.');
else
    
if z2_emp == 1
    set(handles.text5, 'String', 'Load ''x'' Data.');
else
    
y= y(1:1:FileName_Size_for_y);

nocomponents1 = get(handles.number_of_components, 'String');
nocomponents = str2num(nocomponents1);

X=AllData;

[AllData_x AllData_y] = size(AllData);

AllData_xb=AllData_x-1;

if str2num(nocomponents1) <= AllData_xb

[XL,YL,XS,YS, beta, PctVar, MSE, stats]=plsregress(X,y,nocomponents); % uses the SIMPLS algorithm, first centering X and Y.

for i = 1:AllData_x
    
    result=[1 (X(i,1:AllData_y))]*beta;
    
    % append result to matrix
	result_matrix(i) = result;
    result_matrix2=result_matrix';
    myVector_result = 1:1:mt_1;
    myVector(i) = result;

myVector_result = 1:1:mt_1;

end
     
for i = 1:1:mt_1
    
    dat={
        myVector(1,i), z(1,i);
        };

data = 5;
newdata = cell(size(data)+[1,0]);
   
end

difference = result_matrix2 - y;
dat=[y,result_matrix2,difference];
dat1=[y,result_matrix2];
set(handles.uitable1,'Data',dat);

figure ()
plot(dat1)
title('Variation: Actual v Predicted');
xlabel('Data Set Number');
ylabel('Value');
legend('Actual Data','Predicted Data');
        set(handles.text5, 'String', 'True v Predicted (line).');
end
end
end


% --------------------------------------------------------------------
function varbyy_Callback(hObject, eventdata, handles)
% hObject    handle to varbyy (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

AllData_trans = get(FAIMS_OFFLINE, 'UserData'); % get data from load button
AllData = AllData_trans';

FileName_Size_for_y = get(handles.load_positive, 'UserData');

[size_a, size_b] = size(AllData_trans);

if size_b == 1
    set(handles.text5,'String','No data loaded, load some data first.');
    return
end

%%% PLS %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
z = get(handles.load_y, 'UserData');
z2 = get(handles.load_positive, 'UserData');
[nt_1 mt_1] = size(z);
y = z;

[nt_1 mt_1] = size(z);
y = z;
y_emp=isempty(y);
z2_emp=isempty(z2);
if y_emp == 1
    set(handles.text5, 'String', 'Load ''y'' Data.');
else
    
if z2_emp == 1
    set(handles.text5, 'String', 'Load ''x'' Data.');
else
    
y= y(1:1:FileName_Size_for_y);
nt_1;

nocomponents1 = get(handles.number_of_components, 'String');
nocomponents = str2num(nocomponents1);

X=AllData;

[AllData_x AllData_y] = size(AllData);

AllData_xb=AllData_x-1;

if str2num(nocomponents1) <= AllData_xb

[XL,YL,XS,YS, beta, PctVar, MSE, stats]=plsregress(X,y,nocomponents); % uses the SIMPLS algorithm, first centering X and Y.

for i = 1:AllData_x
    
    result=[1 (X(i,1:AllData_y))]*beta;
    
    % append result to matrix
	result_matrix(i) = result;
    result_matrix2=result_matrix';
    myVector_result = 1:1:mt_1;
    myVector(i) = result;

myVector_result = 1:1:mt_1;

end
     
for i = 1:1:mt_1
    
    dat={
        myVector(1,i), z(1,i);
        };

data = 5;
newdata = cell(size(data)+[1,0]);
   
end

difference = result_matrix2 - y;
dat=[y,result_matrix2,difference];
dat1=[y,result_matrix2];
set(handles.uitable1,'Data',dat);

figure ()
%%%%%

plot(1:nocomponents,cumsum(100*PctVar(2,:)),'-bo');
xlabel('Number of PLS components');
ylabel('Percent Variance Explained in y');
set(handles.text5,'String', 'Variation Explained by ''y'' (%).');
end
end
end


% --------------------------------------------------------------------
function close_plots_Callback(hObject, eventdata, handles)
% hObject    handle to close_plots (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
close all;
% z = get(handles.UserData, 'UserData');
% 
% [nt_1 mt_1] = size(z);
% y = z;
% y_emp=isempty(y);
% 
% if y_emp == 1
%     set(handles.text5,'String','Load ''x'' Data.');
% else
%     
% % if FileName_Size_for_y == 0
% %     set(handles.text5,'String','Load ''x'' Data.');
% % else
%     
% figure(1);plot(AllData_trans);
% end


% --- Executes on slider movement.
function slider2_Callback(hObject, eventdata, handles)
% hObject    handle to slider2 (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,'Value') returns position of slider
%        get(hObject,'Min') and get(hObject,'Max') to determine range of slider


% --- Executes during object creation, after setting all properties.
function slider2_CreateFcn(hObject, eventdata, handles)
% hObject    handle to slider2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: slider controls usually have a light gray background.
if isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor',[.9 .9 .9]);
end


% --- Executes on slider movement.
function slider1_Callback(hObject, eventdata, handles)
% hObject    handle to slider1 (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,'Value') returns position of slider
%        get(hObject,'Min') and get(hObject,'Max') to determine range of slider


% --- Executes during object creation, after setting all properties.
function slider1_CreateFcn(hObject, eventdata, handles)
% hObject    handle to slider1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: slider controls usually have a light gray background.
if isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor',[.9 .9 .9]);
end


% --- Executes during object creation, after setting all properties.
function max_comp_gui_CreateFcn(hObject, eventdata, handles)
% hObject    handle to max_comp_gui (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called


% --- Executes during object creation, after setting all properties.
function datar_CreateFcn(hObject, eventdata, handles)
% hObject    handle to datar (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called


% --- Executes during object creation, after setting all properties.
function max_samples_CreateFcn(hObject, eventdata, handles)
% hObject    handle to max_samples (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

Contact us