-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathdataDistribution_GUI.m
2182 lines (1815 loc) · 76.4 KB
/
dataDistribution_GUI.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
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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
function data = dataDistribution_GUI(input)
% DATADISTRIBUTION Plot weighted histogram and normality probability plots
% to evaluate meta-analytic data distributions
% [DATACOLLECTION] = DATADISTRIBUTION(DATACOLLECTION, EFFECTSIZE, DATATRANSFORMATION, TAU2ESTIMATOR, EXCLUDESTUDIES)
% SAVESET) reads data from DATACOLLECTION and plots histograms and
% normality probability plots of the EFFECTSIZE of interest, according
% the DATATRANSFORMATION.
% Optionally, alternate Tau2 estimator can be specifies.
% Optionally, studies can be excluded from plots with EXCLUDESTUDIES.
% Input Arguments:
%
% DATACOLLECTION
% String specifying name of input data colection (see PREPDATA). (ex. 'myDataCollection')
% EFFECTSIZE
% String specifying effect measure of interest.
% EFFECTSIZE can be:
% 'absolute' - evaluates distribution of absolute effect. If
% difference computed if negative control is
% reported
% 'normalized' - evaluates distribution of normalized effect.
% *basal control must be reported.
% 'standardized' - evaluates distribution of standardized effect, using Hedge's g.
% *basal control must be reported.
% 'ratio' - evaluates distribution of response ratio.
% *basal control must be reported.
% DATATRANSFORMATION
% String that specifies data transformation
% DATATRANSFORMATION can be:
% 'raw' - no transformation
% 'log' - log10 transformation
% TAU2ESTIMATOR
% String that specifies which tau2 estimator is used in for
% random effect model
% TAU2ESTIMATOR can be:
% 'DL' - DerSimonian-Laird estimator (Recommended)
% 'HS' - Hunter-Schmidt estimator
% 'H' - Hedges estimator
% 'HM' - Hatung-Makambi estimator
% 'SJ' - Sidik-Jonkman estimator
% PRECISIONMEASURE
% String that specifies precision measure used for y-axis of
% funnel plot
% avaialble options:
% 'ISE' - Inverse Standard Error (1/se)
% 'SE' - untransformed error (plotted in reverse order)
% 'N' - sampple size
% EXCLUDESTUDIES
% Numeric array specifying which studies to remove. If all
% studies are to be included, EXCLUDESTUDIES = [].
% WEIGHTS
% String specifying the precision measure used for effect size
% calculations. options:
% 'IV' - inverse variance (default)
% 'IVS' - inverse standardized variance
% 'N' - sample size
%
% last update: 14.09.17
%---------------------------------------------------------------------------
try;
input
try; properties.dataCollection = input.dataCollection; catch; error('import data not specified'); end
try; properties.effectSize = input.effectSize; catch; properties.effectSize = 'absolute'; end
try; properties.dataTransformation = input.dataTransformation; catch; properties.dataTransformation = 'raw';end
try; properties.tau2estimator = input.tau2estimator; catch; properties.tau2estimator = 'DL';end
try; properties.precisionMeasure = input.precisionMeasure; catch; properties.precisionMeasure = 'ISE';end
try; properties.excludeStudies = input.excludeStudies; catch; properties.excludeStudies = [];end
try; properties.weights = input.weights; catch; properties.weights = 'IV'; end;
try; properties.nClusters = input.nClusters; catch; properties.nClusters = 2; end;
try; properties.A1 = input.A1; catch; properties.A1 = false; end
try; properties.A2 = input.A2; catch; properties.A2 = false; end
try; properties.A3 = input.A3; catch; properties.A3 = false; end
try; properties.A4 = input.A4; catch; properties.A4 = false; end
try; properties.A5 = input.A5; catch; properties.A5 = false; end
try; properties.A6 = input.A6; catch; properties.A6 = false; end
try; properties.A7 = input.A7; catch; properties.A7 = false; end
try; properties.A8 = input.A8; catch; properties.A8 = false; end
properties
load(properties.dataCollection);
for k = 1:length(D)
% for k = 1;
%% part 1: data extraction
Sheet = D(k).description; % dataset name
data = dataExtraction(D(k).data, [properties.excludeStudies]); % extact data
covariates = D(k).covariates; % extract covariates
assignin('base', 'D', D);
%% part 2: compute effect size with appropriate transformations
switch properties.effectSize
case 'absolute'
data = AbsoluteDifference(data); % absolute difference/effect
case 'normalized'
data = NormalizedDifference(data); % normalized difference
case 'standardized'
data = hedgesG(data); % standardized difference
case 'ratio'
data = respRatio(data); % response ratio
end
try; if properties.A1; evaluateDistribution (data, properties, Sheet); end; catch; display('error with distribution evaluation'); end % evaluate weighted distributions
try; if properties.A2; multimodalStratification(data, covariates, properties, Sheet); end; catch;display('error with covariate-cluster analysis'); end
try; if properties.A3; funnelPlot(data, properties, Sheet); end; catch; display('error with funnel plot');end % evaluate funnel plot
try; if properties.A4; varianceAnalysis(data, properties, Sheet); end; catch; display('error with variance analysis'); end % variance analysis
try; if properties.A5; weightsAnalysis(data, properties, Sheet); end; catch; display('error with analysis of weights');end % analysis of weighting schemes
try; if properties.A8; comparet2Estimator(data, covariates, properties, Sheet); end ; catch; display('error with analysis of heterogeneity estimators');end % comparison of tau2 estimators
try; if properties.A6; baujatAnalysis(data, properties, Sheet); end; catch; display('error with baujat plot');end % baujat plot
% monteCarloSampleEstimtation(data, properties, Sheet); % monte carlo data resampling
try; if properties.A7; [exSens, cumulativeExSens] = sensitivityAnalysis(data, properties, Sheet); end; catch; display('error with sensitivity analysis');end % sensitivty analyses
% if properties.export % exports results
% fields2rmv = {'xi', 'wi_fe', 'sei_fe', 'ni','id'}; % remove array elements from data structure
% for r = 1:length(fields2rmv);
% try; FINAL = rmfield(FINAL, fields2rmv{r}); catch; end; % remove specified arrays
% try; exSens = rmfield(exSens, fields2rmv{r}); catch; end
% end
% inputSheet = ['inputData_' SheetName{1}]; % input sheet name
% [inputSheet] = truncateSheet(inputSheet); % truncate sheetname if exceeds limits
%
% eS_Sheet = ['eS_' SheetName{1}];
% [eS_Sheet] = truncateSheet(eS_Sheet);
% esCum_Sheet = ['eS_cumul_' SheetName{1}];
% [esCum_Sheet] = truncateSheet(esCum_Sheet);
% try; writetable(struct2table(exSens), properties.file, 'Sheet', eS_Sheet); catch; end
% try; writetable(struct2table(cumulativeExSens), properties.file, 'Sheet', esCum_Sheet); catch; end
% end
end
catch;
msgbox({'Error while running heterogeneity module',...
' ',...
'Ensure inputs are complete and correct'},'Error', 'Error');
end
end
function comparet2Estimator(data, covariates, properties, Sheet);
% data transformation (if specified)
[z,sz,ID, Ni, ind] = transData (data, properties, Sheet);
% assign data
wi = 1./(sz.^2); zw = z.*wi;
S.ESi = z; S.sei_fe = sz; S.wi_fe = wi;
curInd = 1;
% store original estimator
origT2 = properties.tau2estimator;
%DL
properties.tau2estimator = 'DL';
heterogeneity = tau2Estimator(S, properties);
t2Est(curInd) = heterogeneity.t2; clear('heterogeneity');
curInd = curInd + 1;
%HS
properties.tau2estimator = 'HS';
heterogeneity = tau2Estimator(S, properties);
t2Est(curInd) = heterogeneity.t2; clear('heterogeneity');
curInd = curInd + 1;
%H
properties.tau2estimator = 'H';
heterogeneity = tau2Estimator(S, properties);
t2Est(curInd) = heterogeneity.t2; clear('heterogeneity');
curInd = curInd + 1;
%HM
properties.t2estimator = 'HM';
heterogeneity = tau2Estimator(S, properties);
t2Est(curInd) = heterogeneity.t2; clear('heterogeneity');
curInd = curInd + 1;
%SJ
properties.tau2estimator = 'SJ';
heterogeneity = tau2Estimator(S, properties);
t2Est(curInd) = heterogeneity.t2; clear('heterogeneity');
curInd = curInd + 1;
%PM
properties.tau2estimator = 'PM';
heterogeneity = tau2Estimator(S, properties);
t2Est(curInd) = heterogeneity.t2; clear('heterogeneity');
figure;
plot (1:length(t2Est), t2Est, 'ro','MarkerFaceColor','red');
xlabel('t^2 estimators'); ylabel('between-study variance, t^2'); title('Comparison of t^2 Estimators');
set(gca, 'Xtick', 1:1:6);
set(gca,'xticklabel', {'DL', 'HS', 'H', 'HM', 'SJ', 'PM'});
% reassign original estimator
properties.tau2estimator = origT2;
end
function multimodalStratification(data, covariates, properties, Sheet);
% data transformation (if specified)
[z,sz,ID, Ni, ind] = transData (data, properties, Sheet);
k =properties.nClusters; % number of clusters
% cluster data and assign membership
membership = kmeans(z', k);
figure; hold on;
for i = 1:k
histogram(z(membership == i));
legName{i} = ['cluster ' num2str(i)];
end
xlabel('effect size'); ylabel('count'); legend(legName);
title(['"' Sheet '" exploratory cluster analysis']);
for i = 1:length(covariates)
sourceID = []; curCov = []; covCodes = []; T1 = []; T2 = []; indep = [];
unCode = []; freqC = []; rmvCode = [];
try;
% extract codes for given covariate
curCov = [data.(covariates{i})];
covCodes = curCov(ind);
%pearson's chi square test for independence
%check whether cluster membership is dependent on any of the covariates.
% whos
[fin(i).tbl,fin(i).chi2,fin(i).p,fin(i).labels] = crosstab(membership', covCodes);
fin(i).covariate = covariates{i};
sampleSizeCriteria(i) = sum(sum(fin(i).tbl>4));
[n(i), m(i)] = size(fin(i).tbl);
fin(i).sizeCriteriaSatisfied = round(100*sampleSizeCriteria(i)/(n(i)*m(i)))/100; % proportion of cells that have atleast 5 values (100% ~ valid results)
unCode = unique(covCodes);
for j = 1:length(unCode);
freqC(j) = sum(covCodes == unCode(j));
end
rmvCode = unCode(freqC<=(k*4));
membershipUpdated = [];
for j = 1:length(rmvCode)
indk = [];
indk = find(covCodes ~= rmvCode(j));
membershipUpdated = membership(indk);
covCodes= covCodes(indk);
end
[fin(i).tbl2,fin(i).chi22,fin(i).p2,fin(i).labels2] = crosstab(membershipUpdated', covCodes);
catch;
% display(['no independence test for "' covariates{i} '"']);
end
end
assignin('base', 'fin', fin);
counter1 = 1;
try;
for i = 1:length(fin);
if ~isnan(fin(i).p) & ~isempty(fin(i).p) & fin(i).sizeCriteriaSatisfied > 0.75
pCov{counter1} = fin(i).covariate;
pEl(counter1) = fin(i).p;
counter1 = counter1 + 1;
end
end
pEl = pEl(~isnan(pEl));
catch
for i = 1:length(fin);
if ~isnan(fin(i).p) & ~isempty(fin(i).p)
pCov{counter1} = fin(i).covariate;
pEl(counter1) = fin(i).p;
counter1 = counter1 + 1;
end
end
pEl = pEl(~isnan(pEl));
end
[pEl, indCov] = sort(pEl);
pCov = pCov(indCov);
assignin('base', 'pCov', pCov);
figure;
bar(pEl, 'k'); set(gca, 'yscale', 'log'); hold on;
h1 = hline(0.05,'r'); xlabel('covariates'); ylabel('Chi-Squared Independence Test, p-value'); title(['"' Sheet '" Cluster-Covariate Dependency']);
set(gca,'xticklabel', pCov); xtickangle(45);
end
function monteCarloSampleEstimtation(data, properties, Sheet);
% data transformation (if specified)
[z,sz,ID, Ni] = transData (data, properties, Sheet);
% heterogeneity statistisc
S.ESi = z; S.sei_fe = sz; S.wi_fe = 1./(sz.^2); S.ni = Ni;
heterogeneity = tau2Estimator(S, properties);
wf = 1./(sz.^2);
% standard deviation (study-level)
sd = sz.*sqrt(Ni);
% monte-carlo sampling of study-level data
nTrials = 700;
for j = 1:nTrials
pseudoRaw = [];
for i = 1:length(z)
temp = [];
temp = z(i) + sd(i)*randn(Ni(i), 1);
pseudoRaw = [pseudoRaw temp'];
end
trialMean(j) = mean(pseudoRaw);
trialMedian(j) = median(pseudoRaw);
trialStd(j) = std(pseudoRaw);
overallMean(j) = mean(trialMean);
overallStd(j) = mean(trialStd);
overallMedian(j) = mean(trialMedian);
loRank(j) = (length(pseudoRaw)/2)-((1.96*sqrt(length(pseudoRaw)))/2);
hiRank(j) = 1+(length(pseudoRaw)/2)+((1.96*sqrt(length(pseudoRaw)))/2);
pseudoRaw = sort(pseudoRaw);
loCi(j) = pseudoRaw(floor(loRank(j)));
hiCi(j) = pseudoRaw(ceil(hiRank(j)));
overallLo(j) = mean(loCi);
overallHi(j) = mean(hiCi);
end
med.mean = overallMean(end);
med.med = overallMedian(end);
med.lo = overallLo(end);
med.hi = overallHi(end);
assignin('base', 'med', med);
%plot monte carlo simulation history
figure;
subplot(121); plot([1:nTrials], overallMean, 'b'); xlabel('trial number'); ylabel('cumulative estimate'); hold on; title('mean vs median convergence');
subplot(121); plot([1:nTrials], overallMedian, 'r'); xlabel('trial number'); legend('mean', 'median');
subplot(122); plot([1:nTrials], overallStd, 'k'); xlabel('trial number'); ylabel('cumulative std'); title('std convergence');
% pseudoraw and study-level effect size distributions
NM = 'probability';
figure; histogram(z, 'Normalization',NM); hold on; histogram(pseudoRaw, 'Normalization',NM); title([Sheet{1} 'Reconstructed Data']);
legend('study-level', 'reconstructed; sample-level');
xlabel('effect size'); ylabel('count');
%pseudoraw variance statistics
% standard deviations
pseudoStd = overallStd(end);
pseudoSEM = pseudoStd/sqrt(length(z));
% variance
pVsd = pseudoStd^2;
pVsem = pseudoSEM^2;
%assign monte-carlo results to workspace.
monteCarlo.pseudoRaw = pseudoRaw;
monteCarlo.z = z;
monteCarlo.pseudoStd = pseudoStd;
monteCarlo.pseudoSEM = pseudoSEM;
monteCarlo.pVsd = pVsd;
monteCarlo.pVsem = pVsem;
monteCarlo.emp_t2 = heterogeneity.t2;
monteCarlo.pseudoMean = overallMean(end);
assignin('base', 'monteCarlo', monteCarlo);
end
function weightsAnalysis(data, properties, Sheet);
% log transfomration (if specified)
[z,sz,ID, Ni] = transData (data, properties, Sheet);
% assign data
wi = 1./(sz.^2); zw = z.*wi;
S.ESi = z; S.sei_fe = sz; S.wi_fe = wi;
holdThis = properties.weights;
% heterogeneity statistics (inverse variance method)
properties.weights = 'IV';
heterogeneity = tau2Estimator(S, properties);
% heterogeneity statistics (inverse standardized variance method)
properties.weights = 'IVS'
heterogeneityCV = tau2Estimator(S, properties);
properties.weights = holdThis;
% assign heterogeneity statistics
% Q = heterogeneity.Q;
t2 = heterogeneity.t2;
CV = abs(sz./z);
%fixed and random effects weights (absolute)
fixedW = 1./(sz.^2); randomW = 1./((sz.^2)+t2);
%fe, re and sample size weights (normalized)
fixedW = fixedW/(sum(fixedW)); % fixed effects weights
randomW = randomW/(sum(randomW)); % random effects weights
sizeW = Ni; sizeW = sizeW/(sum(sizeW)); % sample size weights
cvW = 1./(CV.^2); cvW = cvW/(sum(cvW)); % standardized variance weights (coefficient of variance, using standard error as precision)
unweightedW = ones(1,length(z)) .*(1/length(z)); % equal weights (unweighted)
% estimation of fe and re model variances using standardized variance weights
F.cvE = sum(z .* cvW)/(sum(cvW));
F.cvSEi = abs(F.cvE .*CV);
cv2feW = 1./([F.cvSEi].^2);
cv2reW = 1./(([F.cvSEi].^2)+heterogeneityCV.t2);
% distribution of weights
% figure;
% subplot(321); hist(log10(fixedW)); title(['"' Sheet{1} '" fe weights']);
% subplot(322); hist(log10(randomW)); title(['"' Sheet{1} '" re weights']);
% subplot(323); hist(log10(sizeW)); title(['"' Sheet{1} '" n weights']);
% subplot(324); hist(log10(cvW)); title(['"' Sheet{1} '" cv weights']);
% subplot(325); hist(log10(cv2feW./(sum(cv2feW)))); title(['"' Sheet{1} '" cv2feW weights']);
% subplot(326); hist(log10(cv2reW./(sum(cv2reW)))); title(['"' Sheet{1} '" cv2reW weights']);
wCorel.fixedW = fixedW;
wCorel.randomW = randomW;
wCorel.sizeW = sizeW;
wCorel.fevsre = corr(fixedW, randomW, 'type', 'spearman');
wCorel.fevsn = corr(fixedW, sizeW, 'type', 'spearman');
assignin('base', 'wCorel', wCorel);
% relationship between standardized and absolute fixed and random effects weights
% figure;
%
% subplot(121); plot ((cv2feW/sum(cv2feW)), fixedW,'ro'); hold on;
% title(['"' Sheet{1} '" cvfe vs fe']); xlabel('cv fe weights'); ylabel('fe weights');
% equality = linspace(0,1);
% plot(equality,equality,'r','LineWidth',1);
% xlim([min((cv2feW/sum(cv2feW))) max((cv2feW/sum(cv2feW)))]); ylim([min(fixedW) max(fixedW)]);
% legend('weights', 'line of equality', 'location', 'best');
%
% subplot(122); plot ((cv2reW/sum(cv2reW)), randomW,'bo'); hold on;
% plot(equality,equality,'r','LineWidth',1);
% title(['"' Sheet{1} '" cvre vs re']); xlabel('cv re weights'); ylabel('re weights');
% xlim([ min((cv2reW/sum(cv2reW))) max((cv2reW/sum(cv2reW)))]); ylim([min(randomW) max(randomW)]);
% legend('weights', 'line of equality', 'location', 'best');
% illustration of relative weight contribuetion
figure;
% subplot(311);
plot ([1:length(z)], unweightedW); hold on;
plot ([1:length(z)], fixedW);
plot ([1:length(z)], randomW);
plot ([1:length(z)], sizeW);
legend('unweighted', 'fe', 're', 'n'); title (['"' Sheet{1} '" comparison of relative contribution of weights']);
xlabel ('study index'); ylabel('Proportion of total weight');
% subplot(312);
% plot ([1:length(z)], unweightedW); hold on;
% plot ([1:length(z)], fixedW);
% plot ([1:length(z)], (cv2feW./(sum(cv2feW))));
% legend('unweighted', 'fe', 'cv2feW');
% subplot(313);
% plot ([1:length(z)], unweightedW); hold on;
% plot ([1:length(z)], randomW);
% plot ([1:length(z)], (cv2reW./(sum(cv2reW))));
% legend('unweighted', 're', 'cv2reW'); xlabel('study');
% effect size estimtes under various models/weightig schemes
E.fixE = sum(z .* fixedW)/(sum(fixedW));
E.randomE = sum(z .* randomW)/(sum(randomW));
E.sizeE = sum(z .* sizeW)/(sum(sizeW));
E.unweightedE = sum(z .* unweightedW)/(sum(unweightedW));
E.fixCV_E = sum(z .* cv2feW)/(sum(cv2feW));
E.randomCV_E = sum(z .* cv2reW)/(sum(cv2reW));
% monte carlo effect size
sd = sz.*sqrt(Ni);
% monte-carlo sampling of study-level data
nTrials = 700;
for j = 1:nTrials
pseudoRaw = [];
for i = 1:length(z)
temp = [];
temp = z(i) + sd(i)*randn(Ni(i), 1);
pseudoRaw = [pseudoRaw temp'];
end
trialMean(j) = mean(pseudoRaw);
trialStd(j) = std(pseudoRaw);
trialMedian(j) = median(pseudoRaw);
overallMean(j) = median(trialMean);
overallStd(j) = median(trialStd);
overallMedian(j) = median(trialMedian);
loRank(j) = (length(pseudoRaw)/2)-((1.96*sqrt(length(pseudoRaw)))/2);
hiRank(j) = 1+(length(pseudoRaw)/2)+((1.96*sqrt(length(pseudoRaw)))/2);
pseudoRaw = sort(pseudoRaw);
loCi(j) = pseudoRaw(round(loRank(j)));
hiCi(j) = pseudoRaw(round(hiRank(j)));
overallLo(j) = median(loCi);
overallHi(j) = median(hiCi);
end
E.mc_E = overallMean(end);
E.mc_med = overallMedian(end);
pseudoStd = overallStd(end);
pseudoSEM = pseudoStd/sqrt(length(z));
Wcv = 1./(abs(F.cvE .*CV).^2);
%
reW = 1./((sz.^2)+t2);
feW = 1./((sz.^2));
F.reSEi = sqrt(1./reW);
F.feSEi = sqrt(1./feW);
F.cvSEi = abs(F.cvE .*CV);
F.sizeSEi = sqrt(sum((Ni-1).*(sz.^2))/(sum(Ni)-length(Ni)));
F.unweightedSEi = std(z)/sqrt(length(Ni));
F.reSE = sqrt(1./sum(reW));
F.feSE = sqrt(1./sum(feW));
F.cv2feSE = sqrt(1./sum(cv2feW));
F.cv2reSE = sqrt(1./sum(cv2reW));
F.mcSE = pseudoSEM;
F.mcMedup = overallHi(end);
F.mcMedlo = overallLo(end);
FINAL(1).ESre = E.fixE; FINAL(1).SEre = F.feSE;
FINAL(2).ESre = E.randomE; FINAL(2).SEre = F.reSE;
FINAL(3).ESre = E.sizeE; FINAL(3).SEre = F.sizeSEi;
FINAL(4).ESre = E.unweightedE; FINAL(4).SEre = F.unweightedSEi;
FINAL(5).ESre = E.fixCV_E; FINAL(5).SEre = F.cv2feSE;
FINAL(6).ESre = E.randomCV_E; FINAL(6).SEre = F.cv2reSE;
FINAL(7).ESre = E.mc_E; FINAL(7).SEre = F.mcSE;
FINAL(8).ESre = E.mc_med; FINAL(8).SEre = (overallHi(end)-overallLo(end))/2;
for i = 1:length(FINAL); FINAL(i).critValue = 1.96; FINAL(i).df = length(z)-1; end
figure;
switch properties.dataTransformation
case 'log'
properties.log2raw_method = 'naive';
FINAL = log2raw(FINAL, properties);
transformationMethod = properties.log2raw_method;
assignin('base', 'FINAL', FINAL);
errorbar([1:4],...
[FINAL(4).ESre_raw, FINAL(1).ESre_raw, FINAL(2).ESre_raw, FINAL(3).ESre_raw],...
[FINAL(4).ciLre_raw, FINAL(1).ciLre_raw, FINAL(2).ciLre_raw, FINAL(3).ciLre_raw],...
[FINAL(4).ciUre_raw, FINAL(1).ciUre_raw, FINAL(2).ciUre_raw, FINAL(3).ciUre_raw],...
'ro',...
'MarkerFaceColor','red');
case'raw'
zCrit = 1.96;
errorbar([1:4],...
[E.unweightedE, E.fixE, E.randomE, E.sizeE],...
[zCrit*F.unweightedSEi, zCrit*F.feSE, zCrit*F.reSE, zCrit*F.sizeSEi],...
[ zCrit*F.unweightedSEi, zCrit*F.feSE, zCrit*F.reSE, zCrit*F.sizeSEi],...
'ro',...
'MarkerFaceColor','red');
end
title(['"' Sheet{1} '" comparison of effect size estimators']); ylabel('effect size +- 95% CI, raw scale');
xlabel('weighting schemes');
% set(axr1, 'Ytick', -0.1:0.1:1);
% set(axr1, 'YtickLabel', {' ', '0', '0.1', '0.2', '0.3', '0.4', '0.5','0.6', '0.7', '0.8','0.9', '1'});
set(gca, 'Xtick', 1:1:4);
set(gca,'xticklabel', {'unweighted', 'FE', 'RE', 'N'});
modelEv = modelEval(E, z, F, sz, Sheet);
assignin('base', 'F', F);
assignin('base', 'E', E);
assignin('base', 'modelEv', modelEv);
end
function pooledSE = pooledVar(weights, se, es, z);
%% notes
% understand relationship between total variance = true variance + error
% variance.
%Reliability is a function of both the total variance and the error variance. True variance is a
%population characteristic; error variance is a characteristic of the measuring instrument.
%Comparisons of any sort can be distorted by differential reliability of variables
v1 = sum(weights);
v2 = sum(weights.^2);
% a = sum(weights.*((es-z).^2));
a = sum(weights.*(se.^2));
% b = v1/((v1^2)-v2);
% pooledSE = sqrt(a*b);
pooledSE = sqrt(a/v1);
end
function varianceAnalysis(data, properties, Sheet);
[z,sz,ID, Ni] = transData (data, properties, Sheet);
wi = 1./(sz.^2); zw = z.*wi;
CV = abs(sz./z);
assignin('base','data', data);
figure;
subplot(121); histogram(sz.*(sqrt(Ni)), 'FaceColor', 'k', 'EdgeColor', 'k'); title (['"' Sheet{1} '" Sample Variance, SD^2']); xlabel('SD^2'); ylabel('count');
subplot(122); plot (Ni, (sz.^2), 'ko'); xlabel('N'); ylabel('SE^2'); title(['"' Sheet{1} '" Effect of Sample Size on Sampling Error']);
Var = (sz.*sqrt(Ni)).^2;
a = length(ID); N = sum(Ni);
for i = 1:length(ID);
sT(i) = ((Ni(i)-1)*(Var(i)));
secQ(i) = (Ni(i)-1)*log(Var(i));
Nho(i) = 1/(Ni(i)-1);
end
sTsum = sum(sT); s2p = sTsum/(N-a);
q = ((N-a)*(log(s2p))) - sum(secQ);
c = 1 + ((1/(3*(a-1)))*(sum(Nho) - (1/(N-a))));
Xo2 = (q/c);
Qinv = chi2inv(0.95, a-1);
Qp = 1-chi2cdf(Xo2, a-1);
figure;
[b, stats] = robustfit(Ni, Var);
[spearR, spearP] = corr(Ni', Var', 'type', 'Spearman');
aa.spearR = spearR; aa.spearP = spearP; aa.Ni = Ni; aa.Var = Var;
assignin('base', 'aa', aa);
plot(Ni, Var, 'ko'); hold on;
ni = linspace(min(Ni), max(Ni));
plot(ni,b(1)+b(2)*ni,'r','LineWidth',1);
Bp = [stats.p(2)];
xlabel('Sample Size (n)'); ylabel('Sample Variance'); title(['"' Sheet{1} '" Sample Size & Variance']);
if Qp < 0.05; Qtest = 1; else; Qtest = 0; end
if Bp < 0.05; Btest = 1; else; Btest = 0; end
if Qtest && Btest
result = {'Variance dependent on sample size'; 'Study-level heteroscedasticity, Bartlet p < 0.05';'Potential problem with reporting of sample errors'; 'Data consistent with random effects';['spearman rho = ' num2str(spearR)]; ['spearman p = ' num2str(spearP)]};
elseif Qtest==1 && Btest==0
result = {'Variance independent of sample size';'Study-level heteroscedasticity, Bartlet p < 0.05';'Data consistent with random effects';['spearman rho = ' num2str(spearR)]; ['spearman p = ' num2str(spearP)]};
elseif Qtest==0 && Btest==1
result = {'Variance dependent on sample size';'Study-level homoscedasticity, Bartlet p > 0.05'; 'Potential problem with reporting of sample errors';'Consider sample size-weighting scheme';['spearman rho = ' num2str(spearR)]; ['spearman p = ' num2str(spearP)]};
elseif Qtest==0 && Btest==0
result = {'Variance independent of sample size';'Study-level homoscedasticity, Bartlet p > 0.05';'Data consistent with fixed effect';['spearman rho = ' num2str(spearR)]; ['spearman p = ' num2str(spearP)]};
end
ylim=get(gca,'ylim');
xlim=get(gca,'xlim');
text(xlim(2), ((ylim(2)-ylim(1))*0.75) + ylim(1),result, ...
'HorizontalAlignment', 'right', ...
'VerticalAlignment', 'top');
legend('Study-level data', ['n vs var, p = ' num2str(Bp)], 'location', 'northeast');
end
%--------------------------------------------------------------------------
function data = AbsoluteDifference(data)
for i = 1:length(data);
if ~isempty(data(i).nc) && ~isempty(data(i).sec) && ~isempty(data(i).xc)
nr(i) = data(i).nr; nc(i) = data(i).nc;
sec(i) = data(i).sec; ser(i) = data(i).ser;
xr(i) = data(i).xr; xc(i) = data(i).xc;
if ~isnan(nr(i)) && ~isnan(nc(i)); N(i) = nr(i) + nc(i); end
if ~isnan(nr(i)) && isnan(nc(i)); N(i) = nr(i) + nr(i); end
if isnan(nr(i)) && ~isnan(nc(i)); N(i) = nc(i) + nc(i); end
sdc(i) = sec(i) * sqrt(nc(i)); sdr(i) = ser(i) * sqrt(nr(i));
sp(i) = sqrt((((nc(i)-1)*(sdc(i)^2))+((nr(i)-1)*(sdr(i)^2)))/(N(i)-2));
se(i) = sqrt ((N(i)*(sp(i)^2))/(nr(i)*nc(i)));
es(i) = xr(i) - xc(i); % absolute difference
w(i) = 1/(se(i)^2);
esw(i) = es(i)*w(i);
data(i).Ni = N(i);
data(i).SEi = se(i);
data(i).ESi = es(i);
% data(i).Wi = w(i);
% data(i).ESWi = esw(i);
else
try;
nr(i) = data(i).nr;
ser(i) = data(i).ser;
xr(i) = data(i).xr;
N(i) = nr(i);
sdr(i) = ser(i) * sqrt(nr(i));
sp(i) = sdr(i);
se(i) =sp(i) / sqrt(N(i));
es(i) = xr(i); % absolute difference
w(i) = 1/(se(i)^2);
esw(i) = es(i)*w(i);
data(i).Ni = N(i);
data(i).SEi = se(i);
data(i).ESi = es(i);
% data(i).Wi = w(i);
% data(i).ESWi = esw(i);
catch ME
data(i).Ni = [];
data(i).SEi = [];
data(i).ESi = [];
% data(i).Wi = [];
% data(i).ESWi = [];
end
end
end
end
%% normalized difference
function data = NormalizedDifference(data)
for i = 1:length(data);
try;
nr(i) = data(i).nr; nc(i) = data(i).nc;
sec(i) = data(i).sec; ser(i) = data(i).ser;
xr(i) = data(i).xr; xc(i) = data(i).xc;
if ~isnan(nr(i)) && ~isnan(nc(i)); N(i) = nr(i) + nc(i); end
if ~isnan(nr(i)) && isnan(nc(i)); N(i) = nr(i) + nr(i); end
if isnan(nr(i)) && ~isnan(nc(i)); N(i) = nc(i) + nc(i); end
sdc(i) = (sec(i) * sqrt(nc(i)))/xc(i);
sdr(i) = (ser(i) * sqrt(nr(i)))/xr(i); %noramlized SD
se(i) = sqrt (((sdc(i)^2)/nc(i)) + ((sdr(i)^2)/nr(i))); % normalized SE
es(i) = (xr(i) - xc(i))/xc(i); % normalized difference
w(i) = 1/(se(i)^2);
esw(i) = es(i)*w(i);
if isinf(es(i)); es(i) = nan(); end
data(i).Ni = N(i);
data(i).SDci = sdc(i);
data(i).SDrxi = sdr(i);
data(i).Spooli = se(i) * sqrt(nr(i)+nc(i));
data(i).SEi = se(i);
data(i).ESi = es(i);
% data(i).Wi = w(i);
% data(i).ESWi = esw(i);
catch ME;
end
end
end
function data = hedgesG(data)
for i = 1:length(data);
try;
nr(i) = data(i).nr; nc(i) = data(i).nc;
sec(i) = data(i).sec; ser(i) = data(i).ser;
xr(i) = data(i).xr; xc(i) = data(i).xc;
if ~isnan(nr(i)) && ~isnan(nc(i)); N(i) = nr(i) + nc(i); Np(i) = nr(i) * nc(i);end
if ~isnan(nr(i)) && isnan(nc(i)); N(i) = nr(i) + nr(i); Np(i) = nr(i) * nr(i); end
if isnan(nr(i)) && ~isnan(nc(i)); N(i) = nc(i) + nc(i); Np(i) = nc(i) * nc(i);end
sdc(i) = (sec(i) * sqrt(nc(i)));
sdr(i) = (ser(i) * sqrt(nr(i)));
sp(i) = sqrt((((nc(i)-1)*(sdc(i)^2))+((nr(i)-1)*(sdr(i)^2)))/(N(i)-2));
es(i) = ((xr(i)-xc(i))/sp(i)) * (1-(3/((4*N(i))-9)));
se(i) = sqrt((N(i)/Np(i))+((es(i)^2)/(2*(N(i)-3.94))));
w(i) = 1/(se(i)^2);
esw(i) = es(i)*w(i);
data(i).Ni = N(i);
data(i).SEi = se(i);
data(i).ESi = es(i);
% data(i).Wi = w(i);
catch ME;
end
end
end
%% response ratio
function data = respRatio(data);
for i = 1:length(data);
try;
nr(i) = data(i).nr; nc(i) = data(i).nc;
sec(i) = data(i).sec; ser(i) = data(i).ser;
xr(i) = data(i).xr; xc(i) = data(i).xc;
if ~isnan(nr(i)) && ~isnan(nc(i)); N(i) = nr(i) + nc(i); end
if ~isnan(nr(i)) && isnan(nc(i)); N(i) = nr(i) + nr(i); end
if isnan(nr(i)) && ~isnan(nc(i)); N(i) = nc(i) + nc(i); end
sdc(i) = (sec(i) * sqrt(nc(i)));
sdr(i) = (ser(i) * sqrt(nr(i)));
% es(i) = log(xr(i)) - log(xc(i));
% se(i) = sqrt(((sdr(i)^2)/(nr(i)*(xr(i)^2))) + ((sdc(i)^2)/(nc(i)*(xc(i)^2))));
es(i) = xr(i)/xc(i);
se(i) = sqrt(((xr(i)^2)/(xc(i)^2))* (((sdr(i)^2)/(nr(i)*(xr(i)^2))) + ((sdc(i)^2)/(nc(i)*(xc(i)^2)))));
% standard error computed using tailor approximation, under assumption cov(xr,xc) = 0.
w(i) = 1/(se(i)^2);
data(i).Ni = N(i);
data(i).SEi = se(i);
data(i).ESi = es(i);
% data(i).Wi = w(i);
catch;
end
end
end
function [data] = dataExtraction(data, exStudies);
n = 1;
xr = []; e = []; ser = []; xc = []; sec = []; nc = [];
for i = 1:length(data)
if ~isempty(exStudies); exclude = any(exStudies == data(i).ID); else; exclude = 0; end
if ~isempty(data(i).nr) && ~isempty(data(i).ser) && ~isempty(data(i).xr) && ~isnan(data(i).nr) && ~isnan(data(i).ser) && ~isnan(data(i).xr) && exclude == 0;
nr(n) = data(i).nr;
ser(n) = data(i).ser;
xr(n) = data(i).xr;
data(i).xr = xr(n);
data(i).nr = nr(n);
data(i).ser = ser(n);
end
try;
if ~isempty(data(i).xc) && ~isempty(data(i).nc) && ~isempty(data(i).sec) && ~isnan(data(i).xc) && ~isnan(data(i).nc) && ~isnan(data(i).sec) && exclude == 0;
xc(n) = data(i).xc;
try; nc(n) = data(i).nc; catch; nc(n) = nrx(n); end
sec(n) = data(i).sec;
data(i).xc = xc(n);
data(i).sec = sec(n);
data(i).nc = nc(n);
else
data(i).xc = [];
data(i).sec = [];
data(i).nc = [];
end
catch;
data(i).xc = [];
data(i).sec = [];
data(i).nc = [];
end
n = n+1;
end
end
function funnelPlot(data, properties, Sheet);
%transform data (if log tranform selected)
[z,sz, ID, Ni] = transData (data, properties, Sheet);
% compute heterogeneity
wi = 1./(sz.^2); zw = z.*wi; S.ESi = z; S.sei_fe = sz; S.wi_fe = wi; S.ni = Ni;
heterogeneity = tau2Estimator(S, properties);
%update with new weights
sz = []; sz = [heterogeneity.SEi];
wi = []; wi = 1./(sz.^2);
switch properties.precisionMeasure
case 'N'
wi_re = Ni;
S.ESre = sum(wi_re .* z)/sum(wi_re);
S.SEre = sqrt(sum((Ni-1).*(sz.^2))/(sum(Ni)-length(Ni)));
esFE = []; seFE = [];
esRE = S.ESre; seRE = S.SEre;
otherwise
%fixed effects
ES_fix = sum(z .* wi)/(sum(wi));
SE_fix = sqrt(1/sum(wi)); % original
% random effects
try; wi_re = 1./(heterogeneity.t2 + (sz.^2)); catch; wi_re = []; end
sei_re = sqrt(1./wi_re);
S.ESre = sum(wi_re .* z)/sum(wi_re);
S.SEre = sqrt(1/sum(wi_re));
% define assign to new variables
esFE = ES_fix; seFE = SE_fix;
esRE = S.ESre; seRE = S.SEre;
end
% funnel plot precision measure
switch properties.precisionMeasure
case 'SE'
prf = sz;
prr = sei_re;
maxP = max(prf); minP = min(prf);
% ES ref line
Yes = linspace(minP, maxP, 100000);
Xes = ones(1,length(Yes))*esRE;
Xesf = ones(1,length(Yes))*esFE;
% 95% CI ref lines
XciL95 = Xes - 1.96.*(Yes); XciU95 = Xes + 1.96.*(Yes);
case 'ISE'
prf = 1./sz; % precision (FE);
prr = 1./sei_re; % precision (RE);
maxP = max(prf); minP = min(prf);
% ES ref line
Yes = linspace(minP, maxP, 100000);
Xes = ones(1,length(Yes))*esRE;
Xesf = ones(1,length(Yes))*esFE;
% 95% CI ref lines
XciL95 = Xesf - 1.96.*(1./Yes); XciU95 = Xesf + 1.96.*(1./Yes);
case 'N'
prf = Ni;
maxP = max(prf); minP = min(prf);
% ES ref line
Yes = linspace(minP, maxP, 100000);
Xes = ones(1,length(Yes))*esRE;
% Xesf = ones(1,length(Yes))*esFE;
% 95% CI ref lines
XciL95 = Xes - 1.96.*(Yes); XciU95 = Xes + 1.96.*(Yes);
end
figure;
funP.z = z;
funP.se = prf;
assignin('base', 'funP', funP);
plot (z, prf,'ko'); hold on; % study-level data
switch properties.precisionMeasure
case 'N'
plot (Xes, Yes, 'r--'); hold on; % random effect (red)
otherwise
plot (Xesf, Yes, 'b--'); hold on; % fixed effects (blue)
plot (Xes, Yes, 'r--'); hold on; % random effect (red)
plot (XciL95, Yes, 'k--'); plot (XciU95, Yes, 'k--'); % 95% CI
end
xlim([min(z) max(z)]); ylim([0 inf]); % axes limits
xlabel('Effect Size'); % x axis label
title (['"' Sheet '" Funnel Plot']); % title
% reverse y axis if standard error precision is used
h = gca;
switch properties.precisionMeasure
case 'SE'
set(h, 'yDir', 'reverse');
ylabel('Precision (SE)');
legend('Study-Level Data', 'Fixed Effects', 'Random Effects', 'location', 'best'); % figure legend
case 'ISE'
ylabel('Precision (1/SE)');
legend('Study-Level Data','Fixed Effects', 'Random Effects', 'location', 'best'); % figure legend
case 'N'
ylabel('Precision (N)');
legend('Study-Level Data','Random Effects', 'location', 'best'); % figure legend
end
end
function [z,sz,ID, Ni, ind] = transData (data, properties, Sheet)
switch properties.dataTransformation
case 'log';
for i = 1:length(data);
if ~isempty(data(i).SEi) && ~isempty(data(i).ESi) && data(i).ESi > 0;
sx(i) = data(i).SEi;
ESi(i) = data(i).ESi;
sz(i) = sqrt(log10(((sx(i)^2 )/ (ESi(i)^2)) + 1));
z(i) = log10(ESi(i)) - (0.5 * (sz(i)^2));
ID(i) = data(i).ID;
Ni(i) = data(i).Ni;
ind(i) = i;
end
end
case 'raw'
for i = 1:length(data);
if ~isempty(data(i).SEi) && ~isempty(data(i).ESi)
sz(i) = data(i).SEi;
z(i) = data(i).ESi;
ID(i) = data(i).ID;
Ni(i) = data(i).Ni;
ind(i) = i;
end
end
sz = sz(z~=0); z = z(z~=0);
end
keeper = 1;
for i = 1:length(z);
if ~isempty(z(i)) && ~isnan(z(i)) && sz(i)~=0