function varargout = pvaluesim(varargin) % PVALUESIM MATLAB code for pvaluesim.fig % PVALUESIM, by itself, creates a new PVALUESIM or raises the existing % singleton*. % % H = PVALUESIM returns the handle to a new PVALUESIM or the handle to % the existing singleton*. % % PVALUESIM('CALLBACK',hObject,eventData,handles,...) calls the local % function named CALLBACK in PVALUESIM.M with the given input arguments. % % PVALUESIM('Property','Value',...) creates a new PVALUESIM or raises the % existing singleton*. Starting from the left, property value pairs are % applied to the GUI before pvaluesim_OpeningFcn gets called. An % unrecognized property name or invalid value makes property application % stop. All inputs are passed to pvaluesim_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 pvaluesim % Last Modified by GUIDE v2.5 20-Nov-2013 18:58:19 % Begin initialization code - DO NOT EDIT gui_Singleton = 1; gui_State = struct('gui_Name', mfilename, ... 'gui_Singleton', gui_Singleton, ... 'gui_OpeningFcn', @pvaluesim_OpeningFcn, ... 'gui_OutputFcn', @pvaluesim_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 end % --- Executes just before pvaluesim is made visible. function pvaluesim_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 pvaluesim (see VARARGIN) % Choose default command line output for pvaluesim handles.output = hObject; % Update handles structure guidata(hObject, handles); % UIWAIT makes pvaluesim wait for user response (see UIRESUME) % uiwait(handles.figure1); % Prepare plots nlines = get(handles.nlines,'Value'); axes(handles.Samples) cla set(handles.Samples,'YLim',[0,nlines+1],'XLim',[-3,3]) line([0 0],[0,nlines+1],'color','k') axes(handles.ConfIntervals) cla set(handles.ConfIntervals,'YLim',[0,nlines+1],'XLim',[0,25]) for i=0:5:20 line((2.5+i)*[1 1],[0,nlines+1],'color','k') end xlabel('Condition') axes(handles.NoRejections); ylabel(handles.NoRejections,'Proportion rejected') hold on plot([0 1],[0.05 0.05],'-', 'linewidth', 1) axis([0 1 0 1]) hold off end % --- Outputs from this function are returned to the command line. function varargout = pvaluesim_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; end % --- Executes on button press in ConditionA. function ConditionA_Callback(hObject, eventdata, handles) % hObject handle to ConditionA (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 ConditionA end % --- Executes on button press in ConditionB. function ConditionB_Callback(hObject, eventdata, handles) % hObject handle to ConditionB (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 ConditionB end % --- Executes on button press in ConditionC. function ConditionC_Callback(hObject, eventdata, handles) % hObject handle to ConditionC (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 ConditionC end % --- Executes on button press in ConditionD. function ConditionD_Callback(hObject, eventdata, handles) % hObject handle to ConditionD (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 ConditionD end function n_Callback(hObject, eventdata, handles) % hObject handle to n (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 n as text % str2double(get(hObject,'String')) returns contents of n as a double end % --- Executes during object creation, after setting all properties. function n_CreateFcn(hObject, eventdata, handles) % hObject handle to n (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 end function r_Callback(hObject, eventdata, handles) % hObject handle to r (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 r as text % str2double(get(hObject,'String')) returns contents of r as a double end % --- Executes during object creation, after setting all properties. function r_CreateFcn(hObject, eventdata, handles) % hObject handle to r (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 end function NoRuns_Callback(hObject, eventdata, handles) % hObject handle to NoRuns (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 NoRuns as text % str2double(get(hObject,'String')) returns contents of NoRuns as a double end % --- Executes during object creation, after setting all properties. function NoRuns_CreateFcn(hObject, eventdata, handles) % hObject handle to NoRuns (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 end % --- Executes on button press in Run. function Run_Callback(hObject, eventdata, handles) % hObject handle to Run (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) % This starts the simulations NoRuns = str2double(get(findobj('Tag','NoRuns'),'String')); n = str2double(get(findobj('Tag','n'),'String')); r = str2double(get(findobj('Tag','r'),'String')); ConditionA = get(handles.ConditionA,'Value'); ConditionB = get(handles.ConditionB,'Value'); ConditionC = get(handles.ConditionC,'Value'); ConditionD = get(handles.ConditionD,'Value'); HandlesInput = [handles.ConditionA, ... handles.ConditionB,... handles.ConditionC, ... handles.ConditionD,... handles.r,... handles.n,... handles.NoRuns,... handles.nlines,... handles.Run ... ]; HandlesWhileRunning = [handles.Continue,... handles.Cancel,... handles.Next]; set(HandlesWhileRunning,'Enable','on') set(HandlesInput,'Enable','off') setappdata(handles.Cancel,'canceling',0) setappdata(handles.Continue,'continue',0) Sigma = (1-r)*eye(3)+r; % Make correlation matrix wit correlation r Sigma5 = Sigma^.5; [E,D] = eig(Sigma); axes(handles.NoRejections); ylabel(handles.NoRejections,'Proportion rejected') hold on plot([0 1],[0.05 0.05],'-', 'linewidth', 1) axis([0 1 0 1]) hold off % Preparation of the plots nlines = str2double(get(handles.nlines,'String')); %nlines = nlines(2)-1; axes(handles.Samples) cla set(handles.Samples,'YLim',[0,nlines+1],'XLim',[-3,3]) line([0 0],[0,nlines+1],'color','k') axes(handles.ConfIntervals) cla set(handles.ConfIntervals,'YLim',[0,nlines+1],'XLim',[0,25]) for i=0:5:20 line((2.5+i)*[1 1],[0,nlines+1],'color','k') end xlabel('Condition') axes(handles.NoRejections) hold on HandleBar = findobj('Tag','Bar'); if isempty(HandleBar) HandleBar = rectangle('Position',[0.4,0,.2,0.0001],'Tag','Bar','FaceColor','r'); end hold off nA = 1 + 2*ConditionA; nB = 1 + ConditionB; nC = 1 + 2*ConditionC; nD = 1 + 3*ConditionD; TotalNoTests = nA*nB*nD*nC; String = cell(2,1); String{1} = sprintf('Total number of tests: %i',TotalNoTests); String{2} = sprintf('%5.1f%% of runs done.',0); String{3} = sprintf('Proportion rejected: %6.4f',0); set(handles.Result,'String',String) drawnow NullHypRej = zeros(NoRuns,1); indn1 = 1:n; indn1p10= 1:(n+10); indn2 = (n+10)+(1:n); indn2p10= (n+10)+(1:(n+10)); indn3 = 2*(n+10)+(1:n); indn3p10= 2*(n+10)+(1:(n+10)); indn12 = [indn1,indn2]; indn12p10 = [indn1p10,indn2p10]; indn123 = [indn12,indn3]; tcritn = tinv(0.975,2*n-1); tcritn10 = tinv(0.975,2*(n+10)-1); tcritnm = tinv(0.975,2*n-(2:4)); tcritnm10= tinv(0.975,2*(n+10)-(2:4)); tcritnm3 = tinv(0.975,3*n-2); tcritnm3p10 = tinv(0.975,3*(n+10)-2); Onesn = ones(2*n,1); Onesn3 = ones(3*n,1); Onesnp10 = ones(2*(n+10),1); Onesn3p10 = ones(3*(n+10),1); Groupsn = ones(3*n,1); Groupsn(indn1) = 0; Groupsn(2*n+indn1) = 2; Groupsn3p10 = ones(3*(n+10),1); Groupsn3p10(indn1p10) = 0; Groupsn3p10(2*(n+10)+indn1) = 2; alpha = .5; Color = [1 0 0; 0 0 1; 0 1 0]; ColorLight = alpha*Color + (1-alpha); % Filling variables that vary between conditions Reject = logical(zeros(nA,nB,nC,nD)); Onesn = cell(nB); tcritn = zeros(nB); tcritnm = zeros(nB,3); FactorB = [0,10]; indn = cell(nB,nD); indn12 = cell(nB,nD); for iB=1:nB Onesn{iB} = ones(2*(n+FactorB(iB)),1); tcritn(iB) = tinv(0.975,2*(n+FactorB(iB))-1); tcritnm(iB,:) = tinv(0.975,2*(n+FactorB(iB))-(2:4)); indn{iB} = (1:(n+FactorB(iB)))'*[1 1 1] + ones(n+FactorB(iB),1)*((n+FactorB(iB))*[0 1 2]); indn12{iB,1} = [indn{iB}(:,1);indn{iB}(:,2)]; indn12{iB,2} = [indn{iB}(:,1);indn{iB}(:,3)]; indn12{iB,3} = [indn{iB}(:,2);indn{iB}(:,3)]; indn12{iB,4} = [indn{iB}(:,1);indn{iB}(:,2);indn{iB}(:,3)]; end for i = 1:NoRuns if getappdata(handles.Cancel,'canceling') set(HandlesWhileRunning,'enable','off') set(HandlesInput,'enable','on') setappdata(handles.Cancel,'canceling',0) break end %if i==nlines % HandleWaitbar = waitbar(0,'Please wait...'); %end Y = randn(3*n+30,3)*E*D^.5; Y(:,3) = .5*Y(:,1)+.5*Y(:,2); Gender = rand(3*n+30,1)>0.5; % Plot the first nlines draws if i<=nlines axes(handles.Samples) line(Y(indn1,1),i*ones(n,1)+0.05,'color',ColorLight(1,:),'marker','o','linestyle','none','MarkerFaceColor',ColorLight(1,:),'markersize',5) line(Y(indn2,1),i*ones(n,1),'color',ColorLight(2,:),'marker','o','linestyle','none','MarkerFaceColor',ColorLight(2,:),'markersize',5) line(mean(Y(indn1,1)),i+0.15,'color',Color(1,:),'marker','o','linestyle','none','MarkerFaceColor',Color(1,:),'markersize',7) line(mean(Y(indn2,1)),i+0.15,'color',Color(2,:),'marker','o','linestyle','none','MarkerFaceColor',Color(2,:),'markersize',7) if ConditionD line(Y(indn{1}(:,3),1),i*ones(n,1),'color',ColorLight(3,:),'marker','o','linestyle','none','MarkerFaceColor',ColorLight(3,:),'markersize',5) line(mean(Y(indn{1}(:,3),1)),i+0.15,'color',Color(3,:),'marker','o','linestyle','none','MarkerFaceColor',Color(3,:),'markersize',7) end axes(handles.ConfIntervals) end offset = zeros(5,1); for iA = 1:nA % Loop over Condition A for iB = 1:nB % Loop over Condition B iD = 0; for kj=2:(2+ConditionD) % Do condition D for ki=1:(kj-1) iD = iD + 1; iC = 1; Column = max([1,(iA>1)*2,(iB>1)*3,(iC>1)*4,(iD>1)*5]); % Determines the column in whichthe CI need to be printed Reject(iA,iB,iC,iD) = ttest(Y(indn{iB}(:,ki),iA),Y(indn{iB}(:,kj),iA),tcritn(iB)); if i<=nlines %[ki,kj] offset(Column) = offset(Column) + 0.1; PlotCI(Y(indn{iB}(:,ki),iA),Y(indn{iB}(:,kj),iA),tcritn(iB),Column,Reject(iA,iB,iC,iD),offset(Column),i) end if ConditionC [Reject(iA,iB,:,iD),t] = Regression(Y(indn12{iB,iD},iA),Onesn{iB},Groupsn(indn12{iB}),Gender(indn12{iB,iD}),tcritnm(iB,:)); % Plot the first nlines under ConditionC if i<=nlines tt = [tcritnm(iB,:),tcritnm(iB,3)]; for k=1:3 iC = iC + 1; Column = max([1,(iA>1)*2,(iB>1)*3,(iC>1)*4,(iD>1)*5]); % Determines the column in whichthe CI need to be printed PlotCIt(t(k),tt(k),Column,Reject(iA,iB,k,iD),offset(Column),i) offset(Column) = offset(Column) + 0.1; end end end end end % if ConditionD % Reject13 = ttest(Y(indn1,1),Y(indn3,1),tcritn); % Reject23 = ttest(Y(indn2,1),Y(indn3,1),tcritn); % [RejectLin,t] = RegressionLinTrend(Y(indn123,1),Onesn3,Groupsn,tcritnm3); % RejectAny = RejectAny | any([Reject13,Reject23,RejectLin]); % % Plot the first nlines under ConditionD % if i<=nlines % PlotCI(Y(indn1,1),Y(indn3,1),tcritn10,5,Reject13,0.1,i) % PlotCI(Y(indn2,1),Y(indn3,1),tcritn10,5,Reject23,0.2,i) % PlotCIt(t,tcritnm3,5,RejectLin,.2,i) % end % if ConditionB % Reject13 = ttest(Y(indn1p10,1),Y(indn3p10,1),tcritn); % Reject23 = ttest(Y(indn2p10,1),Y(indn3p10,1),tcritn); % [RejectLin,t] = RegressionLinTrend(Y(:,1),Onesn3p10,Groupsn3p10,tcritnm3p10); % RejectAny = RejectAny | any([Reject13,Reject23,RejectLin]); % % Plot the first nlines under ConditionD % if i<=nlines % PlotCI(Y(indn1p10,1),Y(indn3p10,1),tcritn10,5,Reject13,0.4,i) % PlotCI(Y(indn2p10,1),Y(indn3p10,1),tcritn10,5,Reject23,0.5,i) % PlotCIt(t,tcritnm3p10,5,RejectLin,.6,i) % end % end % end end end NullHypRej(i) = any(Reject(:)); PropNullRej = sum(NullHypRej')/i; if mod(i,100)==0 || i<=nlines || i==NoRuns set(HandleBar,'Position',[0.4,0,.2,max(0.0001,PropNullRej)]) String{2} = sprintf('%5.1f%% of runs done.',i*100/NoRuns); String{3} = sprintf('Proportion rejected: %7.2f%%',PropNullRej*100); set(handles.Result,'String',String); drawnow end if i<=nlines if getappdata(handles.Continue,'continue')==0 uiwait end end end %delete(HandleWaitbar) set(HandlesWhileRunning,'Enable','off') set(HandlesInput,'Enable','on') end function Reject = ttest(y1,y2,tcrit) n = size(y1,1); m1 = sum(y1)/n; m2 = sum(y2)/n; var1 = sum((y1-m1).^2)/(n-1); var2 = sum((y2-m2).^2)/(n-1); t = abs(m1-m2)/((var1+var2)/n)^.5; %p = 1-tcdf(abs(t),2*n-1); %Reject = p < 0.025; Reject = t>tcrit; end function [lb,ub] = tCI(y1,y2,tcrit) % Computes the t confidence interval n = size(y1,1); m1 = sum(y1)/n; m2 = sum(y2)/n; var1 = sum((y1-m1).^2)/(n-1); var2 = sum((y2-m2).^2)/(n-1); t = (m1-m2)/((var1+var2)/n)^.5; lb = t - tcrit; ub = t + tcrit; end function PlotCI(y1,y2,tcrit,Cond,Reject,offset,i) if Reject ColorCILine = [1 0 0]; else ColorCILine = [0 1 0]; end [lb,ub]=tCI(y1,y2,tcrit); line(2.5+(Cond-1)*5+0.2*[lb,ub],i*[1 1]+offset,'color',ColorCILine,'linestyle','-','LineWidth',2) end function PlotCIt(t,tcrit,Cond,Reject,offset,i) if Reject ColorCILine = [1 0 0]; else ColorCILine = [0 1 0]; end lb = t - tcrit; ub = t + tcrit; line(2.5+(Cond-1)*5+0.2*[lb,ub],i*[1 1]+offset,'color',ColorCILine,'linestyle','-','LineWidth',2) end function [Reject,t] = Regression(y,Ones,C,G,tcrit) n = size(y,1); Reject = logical(zeros(3,1)); t = zeros(3,1); X = [Ones,C]; %mdl = LinearModel.fit(X,y) % Check efffect of Condition only (C) % if false % m = size(X,2); % XX = X'*X; % invXX = inv(XX); % b = invXX*(X'*y); % s = (sum((y-X*b).^2))/(n-m); % tt = b./(diag(invXX)*s).^.5; % t(1) = tt(2); % Reject(1) = (abs(t(1))>tcrit(1)); % else % t(1)=0; % Reject(1) = false; % end % Check efffect of Condition (C) with G as covariate X = [Ones,C,G]; m = size(X,2); XX = X'*X; invXX = inv(XX); b = invXX*(X'*y); s = (sum((y-X*b).^2))/(n-m); tt = b./(diag(invXX)*s).^.5; t(1) = tt(1); Reject(2) = (abs(t(2))>tcrit(2)); % Check efffect of Condition (C) with G as covariate and CG interaction X = [Ones,C,G,C.*G]; m = size(X,2); XX = X'*X; invXX = inv(XX); b = invXX*(X'*y); s = (sum((y-X*b).^2))/(n-m); tt = b./(diag(invXX)*s).^.5; t(2:3) = tt([2 4]); Reject(2:3) = (abs(t(2:3))>tcrit(3)); end function [Reject,t] = RegressionLinTrend(y,Ones,C,tcrit) n = size(y,1); X = [Ones,C]; %mdl = LinearModel.fit(X,y) % Check efffect of Condition only (C) m = size(X,2); XX = X'*X; invXX = inv(XX); b = invXX*(X'*y); s = (sum((y-X*b).^2))/(n-m); tt = b./(diag(invXX)*s).^.5; t = tt(2); Reject = (abs(t)>tcrit(1)); end function Result_Callback(hObject, eventdata, handles) % hObject handle to Result (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 Result as text % str2double(get(hObject,'String')) returns contents of Result as a double end % --- Executes during object creation, after setting all properties. function Result_CreateFcn(hObject, eventdata, handles) % hObject handle to Result (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 end % --- Executes on button press in Cancel. function Cancel_Callback(hObject, eventdata, handles) % hObject handle to Cancel (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) setappdata(handles.Cancel,'canceling',1) end function nlines_Callback(hObject, eventdata, handles) % hObject handle to nlines (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 nlines as text % str2double(get(hObject,'String')) returns contents of nlines as a double end % --- Executes during object creation, after setting all properties. function nlines_CreateFcn(hObject, eventdata, handles) % hObject handle to nlines (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 end % --- Executes on button press in Next. function Next_Callback(hObject, eventdata, handles) % hObject handle to Next (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) uiresume %drawnow end % --- Executes on button press in Continue. function Continue_Callback(hObject, eventdata, handles) % hObject handle to Continue (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) setappdata(handles.Continue,'continue',1) uiresume end