-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsummaryTable.m
More file actions
303 lines (236 loc) · 8 KB
/
summaryTable.m
File metadata and controls
303 lines (236 loc) · 8 KB
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
function Res=summaryTable(ta,grp)
%
% Makes a summary table of the variables in the table ta. If grp is assigned the
% summary statistics are done in each grp
%
% Res=summaryTable(ta,grp)
% ta: Table including the variable for analyse. Suported datatypes are: categorical, Logical and numeric
% grp: Grouping variable
%
% Res: Results table
%
% * For categorical variables numbers and propotions are counted
% * Logical variable numbers and propotions are counted
% * For continues variables a mean,STD, Median,(IQR), Min & max are calualted
%
%
% Examples using Matlab dataset:
% load patients
% T = table(Gender,Age,Height,Weight,Smoker,Systolic,Diastolic);
% T=convertvars(T,{'Gender'},'categorical') % makes 'Gender' categorical
%
% To get summary statistics of the whole dataset
%
% Res=summaryTable(T)
%
% To get the summary statistics of the whole dataset acording to
% gender
%
% Res=summaryTable(T,T.Gender)
%
%
% To get the summary statistics of only Age, Diastolic & Systolic acording to
% gender
%
% Res=summaryTable(T(:,{'Age','Systolic','Diastolic'}),T.Gender)
%
%
% Tip: To replace varibale names with more describing names use
% VariableDescriptions in the table, like
%
% T.Properties.VariableDescriptions('Age')={'Subject age'};
%
% Use VariableUnits to include units in the results
%
% T.Properties.VariableUnits('Age')={'Years'};
%
% Author: Samuel E Schmidt sschmidt@hst.aau.dk
k=0;
Res=table;
if nargin>1
if ~iscategorical(grp)
grp=categorical(grp);
else
grp=removecats(grp);
end
if sum(isundefined(grp) )>0
idx_ex=isundefined(grp) ;
disp(['Warning! ' num2str(sum(idx_ex) ) ' records excluded from analyse due to missing grp data'])
ta(idx_ex,:)=[];
grp(idx_ex)=[];
end
ugrp=categories(grp);
nugrp=length(ugrp);
else
ugrp=0;
nugrp=0;
end
warning('off','MATLAB:table:RowsAddedExistingVars');
for i=1:size(ta,2)
% check if pseudoLogical
if isnumeric(ta{:,i}) & sum( ~ismember(ta{:,i},[ 0 1]) & ~isnan(ta{:,i}))<=0
if sum(isnan(ta{:,i}))==0
ta.(ta.Properties.VariableNames{i})=logical( ta{:,i});
else
ta.(ta.Properties.VariableNames{i})=categorical( ta{:,i},[nan 0 1],{'Undefined','N' ,'Y'});
end
end
% Make table headings
if strcmp(ta{1,i},'Heading')
k=k+1;
if isempty(ta.Properties.VariableDescriptions) | isempty(ta.Properties.VariableDescriptions{i})
Res(k,1)={ta.Properties.VariableNames(i)};
else
Res(k,1)={ta.Properties.VariableDescriptions(i)};
end
Res{end,2}={''};
if nugrp>0
for j=1:nugrp
Res{end,j+2}={''};
end
end
elseif isnumeric(ta{:,i}) % in case of numeric data calculate statistics
k=k+1;
% estimate statistics for all data
u=nanmean(ta{:,i}) ;
m=nanmedian(ta{:,i});
mi=nanmin(ta{:,i});
ma=nanmax(ta{:,i});
sd=nanstd(ta{:,i});
n=sum(isnan(ta{:,i})==0);
q(1:2,1)=quantile(ta{:,i},[0.25 0.75])';
% estimate statistics for groups data
if nugrp>0
for j=1:nugrp
x=ta{grp==ugrp(j),i};
if ~isempty(x)
u(1,j+1)=nanmean(x) ;
m(1,j+1)=nanmedian(x);
mi(1,j+1)=nanmin(x);
ma(1,j+1)=nanmax(x);
sd(1,j+1)=nanstd(x);
n(1,j+1)=sum(isnan(x)==0);
q(1:2,j+1)=quantile(x,[0.25 0.75])';
else
u(1,j+1)=nan;
m(1,j+1)=nan;
mi(1,j+1)=nan;
ma(1,j+1)=nan;
sd(1,j+1)=nan;
q(1:2,j+1)=nan;
n(1,j+1)=sum(isnan(x)==0);
end
end
end
% add statics for numeric datat to table
if isempty(ta.Properties.VariableDescriptions) | isempty(ta.Properties.VariableDescriptions{i})
Res(k,1)={ta.Properties.VariableNames(i)};
else
Res(k,1)={ta.Properties.VariableDescriptions(i)};
end
if isempty(ta.Properties.VariableUnits) | isempty(ta.Properties.VariableUnits{i})
else
e=cellstr([ char(Res{k,1}) ' (' ta.Properties.VariableUnits{i} ')']);
Res(k,1)={e};
end
Res{end,2}={''};
if nugrp>0
Res{end,2:nugrp+2}={''};
end
Res{end+1,1}={' N'};
for j=1:nugrp+1
Res{end,1+j}= {num2str(n(j))};
end
% estiate p-value
if nugrp>1
[p]=anova1(ta{:,i},grp,'off');
Res{end+1,1}={[' Mean' setstr(177) 'SD (p=' num2str(p,3) ')']};
else
Res{end+1,1}={[ ' Mean' setstr(177) 'SD' ]};
end
for j=1:nugrp+1
Res{end,1+j}= {[ num2str(u(j),3) setstr(177) num2str(sd(j),3)]};
end
Res{end+1,1}={' Median(IQR)'};
for j=1:nugrp+1
Res{end,1+j}= {[num2str(m(j),3) '(' num2str(q(1,j),3) '-' num2str(q(2,j),3) ')']};
end
Res{end+1,1}={' Min-Max'};
for j=1:nugrp+1
cif=floor( log10(ma(j)));
if cif>=2
deci=cif+2;
else
deci=3;
end
Res{end,1+j}= {['(' num2str(mi(j),deci) '-' num2str(ma(j),deci) ')']};
end
elseif iscategorical(ta{:,i}) % In case of categorical datatype
k=k+1;
if isempty(ta.Properties.VariableDescriptions) | isempty(ta.Properties.VariableDescriptions{i})
Res(k,1)={cellstr([ta.Properties.VariableNames{i} '(N,(%))'])};
else
Res(k,1)={cellstr([ta.Properties.VariableDescriptions{i} '(N,(%))'])};
end
if nugrp>1 % estimate p-value for categorical group data
x=removecats(ta{:,i});
[c ,ch,p]=crosstab(x,grp);
temp_str=Res{k,1}{:};
Res(k,1)={cellstr([temp_str ' (p=' num2str(p) ')'] )};
end
Res{end,2}={''};
C = categories(ta{:,i});
N=countcats(ta{:,i});
Res{end,2}={[num2str(sum(N))]};
if nugrp>0 % estimate n for categorical group data
for j=1:nugrp
n=countcats(ta{ grp==ugrp(j),i});
N(:,j+1)=n;
Res{end,j+2}={[num2str(sum(N(:,j+1)))]};
end
end
% Set n in Res
for k=1:length(C)
Res{end+1,1}={[' ' C{k}]};
for j=1:nugrp+1
if j>1
Res{end,1+j}= {[num2str(N(k,j)) ' (' num2str(100*N(k,j)/sum(grp==ugrp(j-1)),3) '%)']};
else
Res{end,1+j}= {[num2str(N(k,j)) ' (' num2str(100*N(k,j)/size(ta,1),3) '%)']};
end
end
end
elseif islogical(ta{:,i}) % In case of logical datatype
k=k+1;
% write variable name in Res
if isempty(ta.Properties.VariableDescriptions) | isempty(ta.Properties.VariableDescriptions{i})
Res(k,1)={cellstr([ta.Properties.VariableNames{i} '(N,(%))'])};
else
str=cellstr([ta.Properties.VariableDescriptions{i} ' (N,(%))']);
Res(k,1)={str};
end
n=sum(ta{:,i}==1);
Res{end,2}={[num2str(n) ' (' num2str(100*n/length(ta{:,i}),3) '%)']};
if nugrp>1
for j=1:nugrp
n=sum(ta{ grp==ugrp(j),i}==1);
Res{end,j+2}={[num2str(n) ' (' num2str(100*n/sum(grp==ugrp(j)),3) '%)']};
end
end
else
disp(['Warning datatype not recognized for variable: -' ta.Properties.VariableNames{i} '- Consider to convert to categorical '] )
end
k=size(Res,1);
end
warning('on','MATLAB:table:RowsAddedExistingVars')
% set table col names
grpNames=ugrp;
if nugrp>0
for i=1:length(ugrp)
grpNames{i}=strrep(grpNames{i},'-','_');
end
colNames={'Vars','All',grpNames{:}};
else
colNames={'Vars','All'} ;
end
Res.Properties.VariableNames=colNames;