-
Notifications
You must be signed in to change notification settings - Fork 3
/
colldiag.m
175 lines (161 loc) · 5.38 KB
/
colldiag.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
% COLLDIAG Belsley, Kuh, & Welsch collinearity diagnostics for design matrix
%
% info = colldiag(X,labels,fuzz,add_intercept,normalize);
%
% Returns inflation factors, condition indices and variance decomposition
% factors that can be used to determine the degree of collinearity present
% in the design matrix of a multiple regression.
%
% Condition index Degree of Collinearity
% ------------------------------------------------
% condind < 10 Weak
% 10 < condind < 30 Moderate to strong
% condind > 100 Severe
% ------------------------------------------------
%
% The joint condition of a high condition index and at least 2 high variance
% decomposition proportions (Belsley, Kuh & Welch suggest >0.5) indicate
% potential estimation problems.
%
% Be aware that some references define ranges using the square root of the
% condition index.
%
% INPUTS
% X - design matrix [data points x variables]
%
% OPTIONAL
% labels - cell array of variable names (strings) for labelling output
% fuzz - Threshold for printing vdps; defaults to 0.5. Vdps less
% than fuzz are not printed to info.str. Idea from Hendrickx's
% R package 'perturb'.
% add_intercept - boolean (defaults false) to add a column of ones to X
% normalize - boolean (defaults true) for indicating whether to scale
% columns of X to have unit length.
%
% OUTPUTS
% info - structure with following fields
% .n - # of data points
% .p - # of variables
% .add_intercept - boolean if intercept was added or present
% .normalize - boolean if columns normed to unit length
% .vif - variance inflation factors
% .condind - condition indices (sorted in ascending order)
% .vdp - variance decomposition proportions
% .fuzz - threshold value for vdps in .str
% .str - string table where the first column lists the
% condition indices. The remainder of the table
% lists the variance decomposition proportions.
%
% REFERENCES
% Belsley, Kuh, & Welsch (1980).
% Regression Diagnostics: Identifying Influential Data and Sources of
% Collinearity (Chapter 3)
%
% SEE ALSO
% colldiag_tableplot
% $ Copyright (C) 2014 Brian Lau http://www.subcortex.net/ $
%
% This program is free software: you can redistribute it and/or modify
% it under the terms of the GNU General Public License as published by
% the Free Software Foundation, either version 3 of the License, or
% (at your option) any later version.
%
% This program is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
% GNU General Public License for more details.
%
% You should have received a copy of the GNU General Public License
% along with this program. If not, see <http://www.gnu.org/licenses/>.
%
% brian 10.01.06
%
function info = colldiag(X,labels,fuzz,add_intercept,normalize)
if nargin < 5
normalize = true;
end
if nargin < 4
add_intercept = false;
end
if nargin < 3
fuzz = 0.5;
end
if nargin < 2
for i = 1:size(X,2)
labels{i} = ['X' num2str(i)];
end
elseif exist('labels','var')
if isempty(labels)
for i = 1:size(X,2)
labels{i} = ['X' num2str(i)];
end
end
end
if add_intercept
if any(all(X==1))
fprintf('Intercept already present in design matrix. ADD_INTERCEPT parameter ignored.\n');
else
X = [ones(size(X,1),1) , X];
labels = {'int' labels{:}};
end
end
[n,p] = size(X);
if p ~= length(labels)
error('Labels don''t match design matrix.');
end
if normalize
% Normalize each column to unit length (pg 183 in Belsley et al)
len = sum(X.^2).^0.5;
%X = X./repmat(len,n,1); %
X = bsxfun(@rdivide,X,len);
end
[~,S,V] = svd(X,0);
lambda = diag(S);
% Ratio of largest singular value to all singular values
%condind = repmat(S(1,1),p,1) ./ lambda;
condind = bsxfun(@rdivide,S(1,1),lambda);
% variance decomposition proportions
%phi_mat = (V'.*V') ./ repmat(lambda.^2,1,p);
phi_mat = bsxfun(@rdivide,V'.*V',lambda.^2);
phi = sum(phi_mat);
%vdp = phi_mat ./ repmat(phi,p,1);
vdp = bsxfun(@rdivide,phi_mat,phi);
% Variance inflation factors
vif = diag(inv(corr(X)));
s = sprintf('\n Variance decomposition proportions\n\t');
for i = 1:p
temp = sprintf('%s',[labels{i}(1:min(5,length(labels{i}))) repmat(' ',1,7-min(5,length(labels{i})))]);
s = [s temp];
end
s = [s sprintf('\nCondInd\n')];
for i = 1:p
s = [s sprintf('%g\t',round(condind(i)))];
for j = 1:p
if vdp(i,j) <= fuzz
s = [s sprintf('----- ')];
else
s = [s sprintf('%1.3f ',vdp(i,j))];
end
end
s = [s sprintf('\n')];
end
s = [s sprintf('\n VIF: ')];
for i = 1:p
s = [s sprintf('%5.1f ',vif(i))];
end
s = [s sprintf('\n')];
if nargout == 0
disp(s);
return;
else
info.n = n;
info.p = p;
info.add_intercept = add_intercept;
info.normalize = normalize;
info.labels = labels;
info.vif = vif;
info.condind = condind;
info.vdp = vdp;
info.fuzz = fuzz;
info.str = s;
end