forked from LIMO-EEG-Toolbox/limo_tools
-
Notifications
You must be signed in to change notification settings - Fork 1
/
limo_bootttest1.m
133 lines (118 loc) · 3.84 KB
/
limo_bootttest1.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
function [m,dfe,ci,sd,n,t,p] = limo_bootttest1(varargin)
% implement the one-sample t-test using bootstrap.
% The ttest is performed on the last non-singleton dimension,
% which can be subjects or trials. If vectors are compared they
% should have dimensions (1,N).
%
% [m,dfe,ci,sd,n,t,p] = limo_bootttest1(data,alpha,boot)
%
% INPUTS:
%
% data = a matrix of data to be used in the one sample t-test
% alpha = test performed at the (100*alpha) significance level
% boot = matrix of bootstrap index or N the number of bootstrap to do.
%
% OUTPUTS:
%
% [m,ci,sd,n,t,p] = means, dfe, confidence interavals, std,
% number of observations, t values, p values
%
% Based on limo_ttest
% Cyril 01-03-2011
% removed electrode loop in 3D case: GAR 06-12-2011
% -----------------------------
% Copyright (C) LIMO Team 2010
%% check inputs
if nargin == 1
data = varargin{1};
nd = numel(size(data));
alpha = 5/100;
Nboot = 1000;
if isvector(data)
boot = ceil(rand(Nboot,length(data)).*length(data))';
else
boot = ceil(rand(Nboot,size(data,nd)).*size(data,nd))';
end
elseif nargin == 2
data = varargin{1};
nd = numel(size(data));
alpha = varargin{2};
Nboot = 1000;
boot = ceil(rand(Nboot,size(data,nd)).*size(data,nd))';
elseif nargin == 3
data = varargin{1};
nd = numel(size(data));
alpha = varargin{2};
tmp = varargin{3};
if size(tmp,1) == 1 && size(tmp,2) == 1
Nboot = tmp;
boot = ceil(rand(Nboot,size(data,nd)).*size(data,nd))';
elseif size(tmp,1) == size(data,nd)
Nboot = size(tmp,2);
boot = tmp;
elseif size(tmp,2) == size(data,nd)
disp('boot matrix transposed')
boot = tmp';
Nboot = size(boot,2);
else
error('error in boot input')
end
else
error('wrong number of arguments')
end
clear tmp
if nd > 3
error('max data dimension = 3')
end
%% compute
n = size(data,nd);
dfe = n-1;
switch (nd)
case(1) % 1 dim
m = NaN(Nboot);
sd = NaN(Nboot);
t = NaN(Nboot);
ci = NaN(Nboot,2);
p = NaN(Nboot);
for B=1:Nboot
boot_data = data(boot(:,B));
m(B) = mean(boot_data);
sd(B) = std(boot_data,0);
t(B) = m(B) ./ (sd(B) ./ sqrt(n));
c = tinv((1 - alpha / 2), n - 1) .* (sd(B) ./ sqrt(n));
ci(B,:) = [(m(B) - c) (m(B) + c)];
dfe(B) = n-1;
p(B) = 2 * tcdf(-abs(t(B)), n - 1); % two tailed
end
case(2) % 2 dim
m = NaN(size(data,1),Nboot);
sd = NaN(size(data,1),Nboot);
t = NaN(size(data,1),Nboot);
ci = NaN(size(data,1),Nboot,2);
p = NaN(size(data,1),Nboot);
for B=1:Nboot
boot_data = squeeze(data(:,boot(:,B)));
m(:,B) = mean(boot_data,nd);
sd(:,B) = std(boot_data,0,nd);
t(:,B) = m(:,B) ./ (sd(:,B) ./ sqrt(n));
c = tinv((1 - alpha / 2), n - 1) .* (sd(:,B) ./ sqrt(n));
ci(:,B,:) = [(m(:,B) - c) (m(:,B) + c)];
p(:,B) = 2 * tcdf(-abs(t(:,B)), n - 1); % two tailed
end
case(3) % 3 dim
m = NaN(size(data,1),size(data,2),Nboot);
sd = NaN(size(data,1),size(data,2),Nboot);
t = NaN(size(data,1),size(data,2),Nboot);
ci = NaN(size(data,1),size(data,2),Nboot,2);
p = NaN(size(data,1),size(data,2),Nboot);
for B=1:Nboot
boot_data = squeeze(data(:,:,boot(:,B)));
m(:,:,B) = mean(boot_data,nd);
sd(:,:,B) = std(boot_data,0,nd);
t(:,:,B) = m(:,:,B) ./ (sd(:,:,B) ./ sqrt(n));
c = tinv((1 - alpha / 2), n - 1) .* (sd(:,:,B) ./ sqrt(n));
ci(:,:,B,1) = (m(:,:,B) - c);
ci(:,:,B,2) = (m(:,:,B) + c);
p(:,:,B) = 2 * tcdf(-abs(t(:,:,B)), n - 1); % two tailed
end
end