forked from yetianmed/subcortex
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinormal.m
120 lines (110 loc) · 3.29 KB
/
inormal.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
function Z = inormal(varargin)
% Applies a rank-based inverse normal transformation.
%
% Usage: Z = inormal(X)
% inormal(X,c)
% inormal(X,method)
% inormal(X,...,quanti)
%
% Inputs:
% X : Original data. Can be a vector or an array.
% c : Constant to be used in the transformation.
% Default c=3/8 (Blom).
% method : Method to choose c. Accepted values are:
% 'Blom' (c=3/8),
% 'Tukey' (c=1/3),
% 'Bliss', (c=1/2) and
% 'Waerden' or 'SOLAR' (c=0).
% quanti : All data guaranteed to be quantitative and
% without NaN?
% This can be a true/false. If true, the function
% runs much faster if X is an array.
% Default is false.
%
% Outputs:
% Z : Transformed data.
%
% References:
% * Van der Waerden BL. Order tests for the two-sample
% problem and their power. Proc Koninklijke Nederlandse
% Akademie van Wetenschappen. Ser A. 1952; 55:453-458
% * Blom G. Statistical estimates and transformed
% beta-variables. Wiley, New York, 1958.
% * Tukey JW. The future of data analysis.
% Ann Math Stat. 1962; 33:1-67.
% * Bliss CI. Statistics in biology. McGraw-Hill,
% New York, 1967.
%
% _____________________________________
% Anderson M. Winkler
% Yale University / Institute of Living
% Aug/2011 (first version)
% Jun/2014 (this version)
% http://brainder.org
% Accept inputs & defaults
c0 = 3/8; % Default (Blom, 1958)
quanti = false;
if nargin == 1,
c = c0;
elseif nargin > 1 && ischar(varargin{2}),
switch lower(varargin{2}),
case 'blom'
c = 3/8;
case 'tukey'
c = 1/3;
case 'bliss'
c = 1/2;
case 'waerden'
c = 0;
case 'solar'
c = 0; % SOLAR is the same as Van der Waerden
otherwise
error('Method %s unknown. Use either ''Blom'', ''Tukey'', ''Bliss'', ''Waerden'' or ''SOLAR''.',varargin{2});
end
elseif nargin > 1 && isscalar(varargin{2}),
c = varargin{2}; % For a user-specified value for c
end
if nargin == 3,
quanti = varargin{3};
if isempty(varargin{2}),
c = c0;
end
end
X = varargin{1};
% If the trait is quantitative, avoid the loop
if quanti,
% Get the rank for each value
[~,iX] = sort(X);
[~,ri] = sort(iX);
% Do the actual transformation
N = size(X,1);
p = ((ri-c)/(N-2*c+1));
Z = sqrt(2)*erfinv(2*p-1);
else
Z = nan(size(X));
for x = 1:size(X,2),
% Remove NaNs
XX = X(:,x);
ynan = ~isnan(XX);
XX = XX(ynan);
% Get the rank for each value
[~,iX] = sort(XX);
[~,ri] = sort(iX);
% Do the actual transformation
N = size(XX,1);
p = ((ri-c)/(N-2*c+1));
Y = sqrt(2)*erfinv(2*p-1);
% Check for repeated values
[U,~,IC] = unique(XX);
if numel(U) < N,
sIC = sort(IC);
dIC = diff(vertcat(sIC,1));
U = unique(sIC(~dIC));
for u = 1:numel(U),
Y(IC == U(u)) = mean(Y(IC == U(u)));
end
end
% Put the NaNs back
Z(ynan,x) = Y;
end
end