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

📄 nipalsgui.m

📁 这是国外一个PLS工具箱
💻 M
📖 第 1 页 / 共 2 页
字号:
function varargout = NipalsGUI(varargin)
% NIPALSGUI M-file for NipalsGUI.fig
% Algoritm in Brereton
% GUI for program created by Riccardo Leardi
%      NIPALSGUI, by itself, creates a new NIPALSGUI or raises the existing
%      singleton*.
%
%      H = NIPALSGUI returns the handle to a new NIPALSGUI or the handle to
%      the existing singleton*.
%
%      NIPALSGUI('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in NIPALSGUI.M with the given input arguments.
%
%      NIPALSGUI('Property','Value',...) creates a new NIPALSGUI or raises the
%      existing singleton*.  Starting from the left, property value pairs are
%      applied to the GUI before NipalsGUI_OpeningFunction gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to NipalsGUI_OpeningFcn via varargin.
%
%      *See GUI Options on GUIDE's Tools menu.  Choose "GUI allows only one
%      instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES

% Edit the above text to modify the response to help NipalsGUI

% Last Modified by GUIDE v2.5 03-Apr-2006 15:44:05

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @NipalsGUI_OpeningFcn, ...
                   'gui_OutputFcn',  @NipalsGUI_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 NipalsGUI is made visible.
function NipalsGUI_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
% varargin   command line arguments to NipalsGUI (see VARARGIN)

% Choose default command line output for NipalsGUI
handles.output = hObject;

% Update handles structure
guidata(hObject, handles);

% UIWAIT makes NipalsGUI wait for user response (see UIRESUME)
% uiwait(handles.figure1);


% --- Outputs from this function are returned to the command line.
function varargout = NipalsGUI_OutputFcn(hObject, eventdata, handles) 
% varargout  cell array for returning output args (see VARARGOUT);
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Get default command line output from handles structure
varargout{1} = handles.output;

vars = evalin('base','who');
set(handles.Xmatrix,'String',vars)
set(handles.XTest,'String',vars)

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


% --- Executes on selection change in Xmatrix.
function Xmatrix_Callback(hObject, eventdata, handles)
% hObject    handle to Xmatrix (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: contents = get(hObject,'String') returns Xmatrix contents as cell array
%        contents{get(hObject,'Value')} returns selected item from Xmatrix


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

% Hint: listbox 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


% --- Executes on selection change in XTest.
function XTest_Callback(hObject, eventdata, handles)
% hObject    handle to XTest (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: contents = get(hObject,'String') returns XTest contents as cell array
%        contents{get(hObject,'Value')} returns selected item from XTest


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

% Hint: listbox 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 st_Callback(hObject, eventdata, handles)
% hObject    handle to st (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 st as text
%        str2double(get(hObject,'String')) returns contents of st as a double


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

% 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 components_Callback(hObject, eventdata, handles)
% hObject    handle to components (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 components as text
%        str2double(get(hObject,'String')) returns contents of components as a double


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

% 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


% --- Executes on button press in FirstRun.
function FirstRun_Callback(hObject, eventdata, handles)


listX = get(handles.Xmatrix,'String');
indexX = get(handles.Xmatrix,'Value');
x = evalin('base',listX{indexX(1)});

[r,c]=size(x);
if get(handles.TestsetCheckbox,'Value') == get(handles.TestsetCheckbox,'Max')
listXTest = get(handles.XTest,'String');
indexXTest = get(handles.XTest,'Value');
x2 = evalin('base',listXTest{indexXTest(1)});
[r2,c2]=size(x2);
end
if get(handles.TestsetCheckbox,'Value') == get(handles.TestsetCheckbox,'min')
  xtot=x;
else
  xtot=[x;x2]; 
end
[o,v]=size(xtot);
if get(handles.Autoscale,'Value') == get(handles.Autoscale,'Max')
  xnorm=(xtot-ones(o,1)*mean(x))./(ones(o,1)*std(x));
  if get(handles.TestsetCheckbox,'Value') == get(handles.TestsetCheckbox,'min')
    x=xnorm;
  else
    x=xnorm(1:r,:);
    x2=xnorm(r+1:r+r2,:);
  end
end  

xt=x;
t=0;
vp=[];
vartot=sum(sum(x.^2));
st=str2double(get(handles.St,'String'));

while sum(vp)<st
  t=t+1;
  ss=sum(xt.^2);
  [a,b]=sort(ss);
  xmax=xt(:,b(c));
  s=xmax'*xmax;
  diff=1000;
  while diff>0.0000001
    rmax=xmax'*xt/s;
    rmaxsq=rmax*rmax';
    rmax=rmax/sqrt(rmaxsq);
    xmax=xt*rmax';
    s2=xmax'*xmax;
    diff=abs(s2-s);
    s=s2;
  end
  smat(:,t)=xmax;
  lmat(t,:)=rmax;
  varexp(t)=xmax'*xmax;
  vp(t)=varexp(t)/vartot*100;
  xt=xt-xmax*rmax;
end

disp(vp)
disp(cumsum(vp))
figure
plot(vp,'-r')
hold on
plot(vp,'or')
set(gca,'xlabel',text(0,0,['Component number' ]));
set(gca,'ylabel',text(0,0,['% of explained variance' ]));
set(gca,'XLim',[.5 size(vp,2)+.5])
figure(gcf)


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


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


% --- Executes on button press in pushbutton2.
function pushbutton2_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
vars = evalin('base','who');
set(handles.Xmatrix,'String',vars)
set(handles.XTest,'String',vars)


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

listX = get(handles.Xmatrix,'String');
indexX = get(handles.Xmatrix,'Value');
x = evalin('base',listX{indexX(1)});

[r,c]=size(x);
if get(handles.TestsetCheckbox,'Value') == get(handles.TestsetCheckbox,'Max')
listXTest = get(handles.XTest,'String');
indexXTest = get(handles.XTest,'Value');
x2 = evalin('base',listXTest{indexXTest(1)});
[r2,c2]=size(x2);
end
if get(handles.TestsetCheckbox,'Value') == get(handles.TestsetCheckbox,'min')
  xtot=x;
else
  xtot=[x;x2]; 
end
[o,v]=size(xtot);
if get(handles.Autoscale,'Value') == get(handles.Autoscale,'Max')
  xnorm=(xtot-ones(o,1)*mean(x))./(ones(o,1)*std(x));
  if get(handles.TestsetCheckbox,'Value') == get(handles.TestsetCheckbox,'min')
    x=xnorm;
  else
    x=xnorm(1:r,:);
    x2=xnorm(r+1:r+r2,:);
  end
end  

xt=x;
t=0;
vp=[];
vartot=sum(sum(x.^2));
st=str2double(get(handles.St,'String'));

while sum(vp)<st
  t=t+1;
  ss=sum(xt.^2);
  [a,b]=sort(ss);
  xmax=xt(:,b(c));
  s=xmax'*xmax;
  diff=1000;
  while diff>0.0000001
    rmax=xmax'*xt/s;
    rmaxsq=rmax*rmax';
    rmax=rmax/sqrt(rmaxsq);
    xmax=xt*rmax';
    s2=xmax'*xmax;
    diff=abs(s2-s);
    s=s2;
  end
  smat(:,t)=xmax;
  lmat(t,:)=rmax;
  varexp(t)=xmax'*xmax;
  vp(t)=varexp(t)/vartot*100;
  xt=xt-xmax*rmax;
end

%disp(vp)
%disp(cumsum(vp))
%figure
%plot(vp,'-r')
%hold on
%plot(vp,'or')
%set(gca,'xlabel',text(0,0,['Component number' ]));
%set(gca,'ylabel',text(0,0,['% of explained variance' ]));
%set(gca,'XLim',[.5 size(st,2)+.5])
%figure(gcf)
nc=str2double(get(handles.components,'String'));
lmat=lmat(1:nc,:); %matrix of loadings
smat=smat(:,1:nc); %matrix of component
[a,b]=size(smat);
if get(handles.TestsetCheckbox,'Value') == get(handles.TestsetCheckbox,'Max')
  smat2=x2*lmat';
end

t2=zeros(r,1);
vvv=zeros(nc,nc);
for i=1:nc
vvv(i,i)=varexp(i)/(r-1);
end
for i=1:r
  t2(i)=smat(i,1:nc)*inv(vvv)*smat(i,1:nc)';
end

for i=1:nc;  % T^2 contributions
  ssq(i,1)=vvv(i,i);
end  
tcont=x*lmat'*diag(1./sqrt(ssq))*lmat;

if get(handles.TestsetCheckbox,'Value') == get(handles.TestsetCheckbox,'Max')
  t22=zeros(r2,1);
  for i=1:r2
    t22(i)=smat2(i,1:nc)*inv(vvv)*smat2(i,1:nc)';
  end
  tcont2=x2*lmat'*diag(1./sqrt(ssq))*lmat;
end

% Computation of Q values based on cross-validation (ng deletion groups)
ng=str2double(get(handles.ng,'String'));
q=zeros(r,1);
qcont=zeros(r,v);
for g=1:ng
  smattr=[];
  lmattr=[];
  t=[g:ng:r];
  xtr=x;
  xtr(t,:)=[];
  xev=x(t,:);
  [rtr,c]=size(xtr);
  
  %%%%%%%%%%
  if get(handles.Autoscale,'Value') == get(handles.Autoscale,'Max')
    sst=std(xtr);
    [jj1,jj2]=min(sst);
    if jj1==0
      disp(['Error: variable ' int2str(jj2) ' constant in group ' int2str(g)])
      stop
    end 
    aa=autosc(xtr,xev);
    xtr=aa(1:rtr,:);
    xev=aa(rtr+1:size(aa,1),:);
  end
  xttr=xtr;
  tt=0;
  while tt<nc
    lmattr=zeros(nc,v);
    tt=tt+1;
    ss=sum(xttr.^2);
    [a2,b2]=sort(ss);
    xmax=xttr(:,b2(c));

⌨️ 快捷键说明

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