-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathezstatistics-0.30.js
4790 lines (4164 loc) · 140 KB
/
ezstatistics-0.30.js
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
"use strict";
/* Library version */
var VERSION = "0.30";
/*****************************************************
Util functions.
******************************************************/
/**
Modified code from:
https://cmatskas.com/importing-csv-files-using-jquery-and-html5/
*/
$(document).ready(function() {
// The event listener for the file upload
if (document.getElementById('txtFileUpload') != null) {
document.getElementById('txtFileUpload').addEventListener('change', upload, false);
// Method that checks that the browser supports the HTML5 File API
function browserSupportFileUpload() {
let isCompatible = false;
if (window.File && window.FileReader && window.FileList && window.Blob) {
isCompatible = true;
}
return isCompatible;
}
// Method that reads and processes the selected file
function upload(evt) {
if (!browserSupportFileUpload()) {
alert("The File APIs are not fully supported in this browser!");
}
else {
let data = null;
let file = evt.target.files[0];
let reader = new FileReader();
reader.readAsText(file);
reader.onload = function(event) {
let csvData = event.target.result;
data = $.csv.toArrays(csvData);
if (data && data.length > 0) {
// Import data entries
let n = data[0].length;
// Fill string array
let arrs = [];
for (let i = 0; i < n; i++){
arrs.push("");
}
// Add data values to samples array
for (let i in data) {
let e = data[i];
for (let vi in e) {
let v = parseFloat(e[vi]);
if (!isNaN(v)) {
arrs[vi] += v + ",";
}
}
}
for (let i in arrs) {
let str = arrs[i];
// Remove last comma
str = str.substr(0,str.length - 1);
// Set data
let key = "samp" + (parseInt(i) + 1);
let inp = document.getElementById(key);
if (inp != null) {
inp.value = str;
}
}
}
else {
alert("No data to import!");
}
};
reader.onerror = function() {
alert("Unable to read " + file.fileName);
};
}
}
}
});
/**
Updates the checked test type radio buttons.
*/
function set_test_type() {
let sel_type = get_param("type");
if (sel_type != null) {
document.forms["testtype"][sel_type].checked=true;
}
}
/**
Returns the requested URL parameter, or null if not found.
*/
function get_param(name) {
let param = null;
// Read all params
let parts = window.location.href.replace(/[?&]+([^=&]+)=([^&]*)/gi, function(m,key,value) {
if (key == name) {
param = value;
}
});
return param;
}
/**
Converts a comma-separated string to an array of float values.
*/
function stringToFloatArray(str) {
let arr = str.split(",");
for (let i in arr) {
let val = arr[i];
val = val.trim();
arr[i] = parseFloat(val);
if (isNaN(arr[i])) {
return NaN;
}
}
return arr;
}
/**
Shows the div element with the specified id.
*/
function show(id) {
let e = document.getElementById(id);
if (e != null) {
e.style.display = "block";
}
}
/**
Hides the div element with the specified id.
*/
function hide(id) {
let e = document.getElementById(id);
if (e != null) {
e.style.display = "none";
}
}
/**
Toggles visibility of the div element with the specified id.
*/
function toggle(id) {
let e = document.getElementById(id);
if (e.style.display == "none") {
e.style.display = "block";
}
else {
e.style.display = "none";
}
}
/**
Fills the descriptive statistics table with sample arrays.
*/
function fill_descriptive_stats(arrs) {
// Number of samples
let k = arrs.length;
for (let s = 1; s <= k; s++) {
let v = arrs[s-1];
// Sample mean
let mean = jStat.mean(v);
// Sample size
let n = v.length;
// Standard deviations for the sample
let std = jStat.stdev(v, true);
// Set values
document.getElementById("n" + s).innerHTML = n;
document.getElementById("mean" + s).innerHTML = mean.toFixed(2);
document.getElementById("stdev" + s).innerHTML = std.toFixed(3);
}
}
/**
Clears the data fields.
*/
function clear_fields(no) {
if (no == -1) {
// Get number of fields
let no = get_no_samples();
}
for (let i = 1; i <= no; i++) {
document.getElementById("samp" + i).value = "";
}
if (document.getElementById("spmean") != null) {
document.getElementById("spmean").value = "";
}
if (document.getElementById("alpha") != null) {
document.getElementById("alpha").value = "0.05";
}
hide("test_results");
clear_error();
}
/**
Shows an error.
*/
function show_error(error_str) {
hide("test_results");
document.getElementById("error").innerHTML = "<br/><font color='red'><b>ERROR: " + error_str + "</b></font>";
}
/**
Clears the error field.
*/
function clear_error() {
document.getElementById("error").innerHTML = "";
}
/**
Reads the sample arrays from the html elements.
*/
function get_sample_arrays(k) {
// Array of sample arrays
let arrs = [];
for (let s = 1; s <= k; s++) {
let str = document.getElementById("samp" + s).value;
let v = stringToFloatArray(str);
if (!Array.isArray(v)) {
let id = String.fromCharCode(65 + s - 1);
throw("Invalid format in sample " + id);
}
arrs.push(v);
}
return arrs;
}
/**
Returns the number of samples.
*/
function get_no_samples() {
let no_str = document.getElementById("no").value;
no_str = no_str.trim();
let no = parseInt(no_str);
if (isNaN(no)) {
throw("Invalid number of samples: " + no_str);
}
return no;
}
/**
Returns the selected test type.
*/
function get_type() {
let v = document.querySelector('input[name="type"]:checked').value;
let type = parseInt(v);
if (isNaN(type)) {
throw("Invalid type: " + v);
}
return type;
}
/**
Returns number of sides (2 for two-tailed, 1 for one-tailed tests).
*/
function get_sides() {
let v = document.querySelector('input[name="hyp"]:checked').value;
if (v == "1") {
return 2;
}
return 1;
}
/**
Returns the tail (1 = left, 2 = right) for one-tailed test.
*/
function get_tail() {
let v = document.querySelector('input[name="hyp"]:checked').value;
if (v == "2") {
return 1;
}
if (v == "3") {
return 2;
}
return 0;
}
/**
Returns the significance level (alpha).
*/
function get_alpha() {
let strA = document.getElementById("alpha").value;
let alpha = parseFloat(strA);
if (isNaN(alpha)) {
throw("Invalid alpha: " + strA);
}
// Set alpha
let e = document.getElementById("sign_level");
if (e != null) {
e.innerHTML = alpha;
}
return alpha;
}
/**
Updates number of samples fields.
*/
function update_no_samples() {
try {
// Clear error
clear_error();
hide("test_results");
// Get number of samples
let no = get_no_samples();
// Samples table
let html = "";
for (let i = 1; i <= no; i++) {
// Convert number to char
let s = String.fromCharCode(65 + i - 1);
let samp = document.getElementById("samp" + i);
let old_vals = "";
if (samp != null) {
old_vals = samp.value;
}
html += "<tr><td width=110'>Sample " + s + ":</td>";
html += "<td><input class='sample' name='samp" + s + "' id='samp" + i + "' value='" + old_vals + "'></td></tr>";
}
// Set samples
let tcont = document.getElementById("samples");
tcont.innerHTML = html;
// Summary table
html = "";
for (let i = 1; i <= no; i++) {
// Convert number to char
let s = String.fromCharCode(65 + i - 1);
html += "<tr><th class='dark'>" + s + "</th>";
html += "<td class='border' id='n" + i + "'> </td>";
html += "<td class='border' id='mean" + i + "'> </td>";
html += "<td class='border' id='stdev" + i + "'> </td>";
html += "</tr>";
}
// Set summary
tcont = document.getElementById("summary");
tcont.innerHTML = html;
}
catch (e) {
show_error(e);
}
}
/**
Visualizes the samples in a box-and-whiskers plot.
*/
function visualize(arrs, names) {
let data = [];
for (let i = 0; i < arrs.length; i++) {
let name = "Sample " + String.fromCharCode(65 + i);
if (names != null) {
name = names[i];
}
let trace = {
x: arrs[i],
type: "box",
name: name
};
data.unshift(trace); //push unshift
}
let layout = {
legend: {
traceorder: "reversed",
}
}
Plotly.newPlot("chart", data, layout);
}
/**
Visualizes the samples in a histogram.
*/
function visualize_histogram(arrs) {
let data = [];
for (let i = 0; i < arrs.length; i++) {
let name = "Sample " + String.fromCharCode(65 + i);
let trace = {
x: arrs[i],
type: "histogram",
name: name
};
data.unshift(trace); //push unshift
}
let layout = {
bargap: 0.05,
yaxis: {
title: "y Axis",
titlefont: {
family: "Courier New, monospace",
size: 18,
color: "#7f7f7f"
}
}
}
Plotly.newPlot("hist", data, layout);
}
/**
Visualizes linear regression.
*/
function visualize_regression(x_vals, y_vals, a, b) {
let data = [];
// Calculate line points
let line_y = [];
let line_x = [];
let min_x = Math.floor(jStat.min(x_vals));
let max_x = Math.ceil(jStat.max(x_vals));
for (let i = min_x; i <= max_x; i++) {
let y = a + b * i;
line_x.push(i);
line_y.push(y.toFixed(2));
}
// Data points
let trace = {
y: y_vals,
x: x_vals,
mode: "markers",
name: "Data points"
};
data.unshift(trace);
// Line of best fit
trace = {
y: line_y,
x: line_x,
mode: "lines",
name: "Line of best fit"
};
data.unshift(trace);
let layout = {
showlegend: true,
legend: {
traceorder: "reversed",
},
xaxis: {
title: {
text: "Sample X"
}
},
yaxis: {
title: {
text: "Sample Y"
}
}
}
Plotly.newPlot("chart", data, layout);
}
/**
Visualizes correlation between two samples.
*/
function visualize_correlation(x_vals, y_vals) {
let data = [];
// Data points
let trace = {
y: y_vals,
x: x_vals,
mode: "markers",
name: ""
};
data.unshift(trace);
let layout = {
showlegend: false,
xaxis: {
title: {
text: "Sample A"
}
},
yaxis: {
title: {
text: "Sample B"
}
}
}
Plotly.newPlot("chart", data, layout);
}
/*****************************************************
Main functions for running statistical tests.
******************************************************/
/**
Global status texts.
*/
// 2 samples, 2 sided
var diff_2p_2s = "<font color='green'>The means of the samples are different</font>";
var nodiff_2p_2s = "<font color='red'>There is no difference between the means of the samples</font>";
// 2 samples, 1 sided
var diff_2p_1s_AltB = "<font color='green'>The mean of sample A is less than the mean of sample B</font>";
var diff_2p_1s_AgtB = "<font color='green'>The mean of sample A is greater than the mean of sample B</font>";
var nodiff_2p_1s_AltB = "<font color='red'>The mean of sample A is not less than the mean of sample B</font>";
var nodiff_2p_1s_AgtB = "<font color='red'>The mean of sample A is not greater than the mean of sample B</font>";
var nodiff_2p_1s_eq = "<font color='red'>There is no difference between the means of the samples</font>";
// 3 or more samples
var diff_3s = "<font color='green'>There is a difference between the means of the samples</font>";
var nodiff_3s = "<font color='red'>There is no difference between the means of the samples</font>";
// Post-test
var post_diff = "<font color='green'>Difference</font>";
var post_nodiff = "<font color='red'>No difference</font>";
// 1 sample, 2 sided
var diff_2p_1s = "<font color='green'>The mean of the sample is different than the specified mean</font>";
var nodiff_2p_1s = "<font color='red'>There is no difference between the mean of the sample and the specified mean</font>";
// 2 sample, 1 sided
var diff_1p_1s_AltB = "<font color='green'>The mean of the sample is less than the specified mean</font>";
var diff_1p_1s_AgtB = "<font color='green'>The mean of the sample is greater than the specified mean</font>";
var nodiff_1p_1s_AltB = "<font color='red'>The mean of the sample is not less than the specified mean</font>";
var nodiff_1p_1s_AgtB = "<font color='red'>The mean of the sample is not greater than the specified mean</font>";
var nodiff_1p_1s_eq = "<font color='red'>There is no difference between the mean of the sample and the specified mean</font>";
// Equal variances
var eq_var = "<font color='green'>The samples have equal variances</font>";
var uneq_var = "<font color='red'>The sample variances are unequal</font>";
// Normally distributed samples
var norm_dist = "<font color='green'>The sample is normally distributed</font>";
var not_norm_dist = "<font color='red'>The sample is not normally distributed</font>";
// Correlation coefficient
var corr_sign = "<font color='green'>There is a relationship between the samples</font>";
var corr_not_sign = "<font color='red'>There is no relationship between the samples</font>";
/**
Runs the calculate confidence intervals test.
See: confintervals.html
*/
function calc_confidence_intervals() {
// Clear error
clear_error();
try {
// Get sample array
let arrs = get_sample_arrays(1);
let v = arrs[0];
// Sample mean
let mean = jStat.mean(v);
// Sample size
let n = v.length;
// Standard deviations for the sample
let std = jStat.stdev(v, true);
// Set values
document.getElementById("n").innerHTML = n;
document.getElementById("mean").innerHTML = mean.toFixed(2);
document.getElementById("stdev").innerHTML = std.toFixed(3);
let carrs = [];
let cnames = [];
let res = confidence_interval(v, 0.90);
document.getElementById("ci90a").innerHTML = " ±" + res[0].toFixed(3) + " ";
document.getElementById("ci90b").innerHTML = " " + res[1].toFixed(3) +" to " + res[2].toFixed(3) + " ";
document.getElementById("ci90c").innerHTML = " <font color='green'>90% certain that the true population mean is between " + res[1].toFixed(3) +" and " + res[2].toFixed(3) + " </font>";
carrs.push( [jStat.min(v), res[1].toFixed(2), res[1].toFixed(2), jStat.median(v), res[2].toFixed(2), res[2].toFixed(2), jStat.max(v)] );
cnames.push( "90%" );
res = confidence_interval(v, 0.95);
document.getElementById("ci95a").innerHTML = " ±" + res[0].toFixed(3) + " ";
document.getElementById("ci95b").innerHTML = " " + res[1].toFixed(3) +" to " + res[2].toFixed(3) + " ";
document.getElementById("ci95c").innerHTML = " <font color='green'>95% certain that the true population mean is between " + res[1].toFixed(3) +" and " + res[2].toFixed(3) + " </font>";
carrs.push( [jStat.min(v), res[1].toFixed(2), res[1].toFixed(2), jStat.median(v), res[2].toFixed(2), res[2].toFixed(2), jStat.max(v)] );
cnames.push( "95%" );
res = confidence_interval(v, 0.98);
document.getElementById("ci98a").innerHTML = " ±" + res[0].toFixed(3) + " ";
document.getElementById("ci98b").innerHTML = " " + res[1].toFixed(3) +" to " + res[2].toFixed(3) + " ";
document.getElementById("ci98c").innerHTML = " <font color='green'>98% certain that the true population mean is between " + res[1].toFixed(3) +" and " + res[2].toFixed(3) + " </font>";
carrs.push( [jStat.min(v), res[1].toFixed(2), res[1].toFixed(2), jStat.median(v), res[2].toFixed(2), res[2].toFixed(2), jStat.max(v)] );
cnames.push( "98%" );
// Data visualization
visualize( carrs, cnames );
show("test_results");
}
catch (e) {
show_error(e);
}
}
/**
Runs the correlation test.
See: correlation.html
*/
function calc_correlation() {
// Clear error
clear_error();
try {
// Get sample arrays
let arrs = get_sample_arrays(2);
// Fill in descriptice statistics
fill_descriptive_stats(arrs);
// Significance level, alpha
let alpha = get_alpha();
let res = correlation_R(arrs[0], arrs[1], alpha);
document.getElementById("corr").innerHTML = res[0].toFixed(3);
document.getElementById("p").innerHTML = res[1].toFixed(5);
if (res[1] <= alpha) {
document.getElementById("res").innerHTML = corr_sign;
}
else {
document.getElementById("res").innerHTML = corr_not_sign;
}
// Score
if (res[2] < 0) {
document.getElementById("score").innerHTML = res[2].toFixed(2) + "<br>Correlation coefficient is significant if T < -" + res[3].toFixed(2);
}
else {
document.getElementById("score").innerHTML = res[2].toFixed(2) + "<br>Correlation coefficient is significant if T > " + res[3].toFixed(2);
}
// Normally distributed samples
// Run Shapiro-Wilk test
if (arrs[0].length < 10) {
document.getElementById("sw_p_1").innerHTML = "";
document.getElementById("sw_res_1").innerHTML = "<font color='blue'>Requires a sample size of at least 10</font>";
}
else {
let res = shapiro_wilk(arrs[0], alpha);
// Set results
document.getElementById("sw_p_1").innerHTML = res[0].toFixed(5) + "<br>Normally distributed if P > " + alpha;
if (res[0] <= alpha) {
document.getElementById("sw_res_1").innerHTML = not_norm_dist;
}
else {
document.getElementById("sw_res_1").innerHTML = norm_dist;
}
}
if (arrs[1].length < 10) {
document.getElementById("sw_p_2").innerHTML = "";
document.getElementById("sw_res_2").innerHTML = "<font color='blue'>Requires a sample size of at least 10</font>";
}
else {
let res = shapiro_wilk(arrs[1], alpha);
// Set results
document.getElementById("sw_p_2").innerHTML = res[0].toFixed(5) + "<br>Normally distributed if P > " + alpha;
if (res[0] <= alpha) {
document.getElementById("sw_res_2").innerHTML = not_norm_dist;
}
else {
document.getElementById("sw_res_2").innerHTML = norm_dist;
}
}
// Data visualization
//visualize(arrs);
visualize_correlation(arrs[0], arrs[1]);
show("test_results");
}
catch (e) {
show_error(e);
}
}
/**
Runs the linear regression test.
See: linearregression.html
*/
function calc_linearregression() {
// Clear error
clear_error();
try {
// Get sample arrays
let arrs = get_sample_arrays(2);
// Fill in descriptice statistics
fill_descriptive_stats(arrs);
// Significance level, alpha
let alpha = get_alpha();
let res = linear_regression(arrs[0], arrs[1], alpha);
if (res[1] >= 0) {
document.getElementById("line").innerHTML = "y = " + res[0].toFixed(3) + " + " + res[1].toFixed(3) + "x";
}
else {
document.getElementById("line").innerHTML = "y = " + res[0].toFixed(3) + " - " + Math.abs(res[1].toFixed(3)) + "x";
}
let R2 = res[2];
let R2p = R2 * 100;
document.getElementById("r2").innerHTML = R2.toFixed(3) + "<br>" + R2p.toFixed(1) + "% of the variation in B is explained by A";
document.getElementById("p").innerHTML = res[3].toFixed(5);
if (res[3] <= alpha) {
document.getElementById("res").innerHTML = corr_sign;
}
else {
document.getElementById("res").innerHTML = corr_not_sign;
}
// Score
if (res[4] < 0) {
document.getElementById("score").innerHTML = res[4].toFixed(2) + "<br>Correlation coefficient is significant if T < -" + res[5].toFixed(2);
}
else {
document.getElementById("score").innerHTML = res[4].toFixed(2) + "<br>Correlation coefficient is significant if T > " + res[5].toFixed(2);
}
// Data visualization
visualize_regression(arrs[0], arrs[1], res[0], res[1]);
show("test_results");
}
catch (e) {
show_error(e);
}
}
/**
Runs the ESD test to find outliers.
See: outliers.html
*/
function find_outliers() {
// Clear error
clear_error();
try {
// Get sample array
let arrs = get_sample_arrays(1);
let v = arrs[0];
// Fill in descriptice statistics
fill_descriptive_stats(arrs);
// Significance level, alpha
let alpha = get_alpha();
let res = outliers_esd(v, alpha);
let vals = res[1];
// Create result HTML
let new_v = [];
let html = "";
for (let i = 0; i < vals.length; i++) {
let val = vals[i];
if (val[1] == false) {
html += "<font color='green'>" + val[0] + "</font>";
new_v.push(val[0]);
}
else {
html += "<font color='red'><b>" + val[0] + "</b></font>";
}
if (i < vals.length - 1) {
html += ", ";
}
if ((i+1) % 20 == 0) {
html += "<br>";
}
}
document.getElementById("vals").innerHTML = html;
// Create trials HTML
let tlist = res[0];
if (tlist.length > 0) {
let html = "";
let tlist = res[0];
for (let i = 0; i < tlist.length; i++) {
let te = tlist[i];
html += "<tr>";
html += "<td class='border'>" + (i+1) + "</td>";
html += "<td class='border'>" + te[0] + "</td>";
html += "<td class='border'>" + te[3].toFixed(3) + "</td>";
html += "<td class='border'>" + te[4].toFixed(3) + "</td>";
if (te[2] == false) {
html += "<td class='border'><font color='green'>No</font></td>";
}
else {
html += "<td class='border'><font color='red'>Yes</font></td>";
}
html += "</tr>";
}
document.getElementById("trials").innerHTML = html;
document.getElementById("restext").innerHTML = "The sample values marked as <font color='red'><b>red</b></font> are potential outliers.";
show("trials_tab");
}
else {
// No outliers found
document.getElementById("restext").innerHTML = "No outliers found in the sample.";
hide("trials_tab");
}
// Data visualization
visualize( [v, new_v], ["Original sample", "Outliers removed"] );
show("test_results");
}
catch (e) {
show_error(e);
}
}
/**
Runs the two-sample t-tests.
See: ttest.html
*/
function run_ttest() {
// Clear error
clear_error();
try {
// Get sample arrays
let arrs = get_sample_arrays(2);
// Fill in descriptice statistics
fill_descriptive_stats(arrs);
// Test type
let type = get_type();
// Significance level, alpha
let alpha = get_alpha();
// Sides (one-tailed or two-tailed) and tail (left or right)
let sides = get_sides();
let tail = get_tail();
// Set mean diff
let mean1 = jStat.mean(arrs[0]);
let mean2 = jStat.mean(arrs[1]);
document.getElementById("mean").innerHTML = (mean1 - mean2).toFixed(3);
// Run test
let res = ttest(arrs[0], arrs[1], sides, tail, alpha, type, 0);
// Set results
document.getElementById("p").innerHTML = res[0].toFixed(5);
if (sides == 2) {
if (res[0] <= alpha) {
document.getElementById("res").innerHTML = diff_2p_2s;
}
else {
document.getElementById("res").innerHTML = nodiff_2p_2s;
}
}
else {
if (res[0] <= alpha) {
let txt = diff_2p_1s_AltB;
if (tail == 2) {
txt = diff_2p_1s_AgtB;
}
document.getElementById("res").innerHTML = txt;
}
else {
let txt = nodiff_2p_1s_AltB;
if (tail == 2) {
txt = nodiff_2p_1s_AgtB;
}
document.getElementById("res").innerHTML = txt;
}
}
// Show T-score
ttest_score(res, sides, tail, "t");
// Independent or paired?
let dep = false;
if (type == 3) {
// Paired
dep = true;
}
// Power analysis
let minN = power_2s(arrs[0], arrs[1], alpha, sides, dep, true);
document.getElementById("n_low").innerHTML = minN[0];
document.getElementById("n_medium").innerHTML = minN[1];
document.getElementById("n_high").innerHTML = minN[2];
document.getElementById("pwr").innerHTML = minN[3].toFixed(2) + "%";
// Equal variances
if (type != 3) {
show("vartest");
// Run F-test
let res = ftest(arrs[0], arrs[1], alpha);
// Set results
document.getElementById("f_p").innerHTML = res[0].toFixed(5) + "<br>Variances are equal if P > " + alpha;
if (res[0] <= alpha) {
document.getElementById("f_res").innerHTML = uneq_var;
}
else {
document.getElementById("f_res").innerHTML = eq_var;
}
}
else {
hide("vartest");
}
// Normally distributed samples
// Run Shapiro-Wilk test
if (arrs[0].length < 10) {
document.getElementById("sw_p_1").innerHTML = "";
document.getElementById("sw_res_1").innerHTML = "<font color='blue'>Requires a sample size of at least 10</font>";
}
else {
let res = shapiro_wilk(arrs[0], alpha);
// Set results
document.getElementById("sw_p_1").innerHTML = res[0].toFixed(5) + "<br>Normally distributed if P > " + alpha;
if (res[0] <= alpha) {
document.getElementById("sw_res_1").innerHTML = not_norm_dist;
}
else {
document.getElementById("sw_res_1").innerHTML = norm_dist;
}
}
if (arrs[1].length < 10) {
document.getElementById("sw_p_2").innerHTML = "";
document.getElementById("sw_res_2").innerHTML = "<font color='blue'>Requires a sample size of at least 10</font>";
}
else {
let res = shapiro_wilk(arrs[1], alpha);
// Set results
document.getElementById("sw_p_2").innerHTML = res[0].toFixed(5) + "<br>Normally distributed if P > " + alpha;
if (res[0] <= alpha) {
document.getElementById("sw_res_2").innerHTML = not_norm_dist;
}
else {
document.getElementById("sw_res_2").innerHTML = norm_dist;
}
}
// Data visualization
visualize(arrs);
show("test_results");
}
catch (e) {
show_error(e);
}
}
/**
Helper function for showing t-test scores.
*/
function ttest_score(res, sides, tail, id) {
if (sides == 2) {
document.getElementById(id).innerHTML = res[1].toFixed(2) + "<br>Reject H<sub>0</sub> (accept H<sub>1</sub>) if T is outside ±" + res[2].toFixed(2);
}
if (sides == 1 && tail == 1) {
document.getElementById(id).innerHTML = res[1].toFixed(2) + "<br>Reject H<sub>0</sub> (accept H<sub>1</sub>) if T < " + res[2].toFixed(2);
}
if (sides == 1 && tail == 2) {
document.getElementById(id).innerHTML = res[1].toFixed(2) + "<br>Reject H<sub>0</sub> (accept H<sub>1</sub>) if T > " + res[2].toFixed(2);
}
}
/**
Runs the two-sample Wilcoxon tests.
See: wilcoxon.html
*/
function run_wilcoxon() {
// Clear error
clear_error();
try {
// Get sample arrays
let arrs = get_sample_arrays(2);