-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.js
204 lines (184 loc) · 6.53 KB
/
main.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
dataObj = {}
dataArr= []
// https://www.geeksforgeeks.org/how-to-read-a-local-text-file-using-javascript/
// Read local text file
document.getElementById('inputfile')
.addEventListener('change', function() {
var fr=new FileReader();
fr.onload=function(e){
document.getElementById('output')
.textContent=fr.result;
// push file data to array
dataArr.push(e.target.result)
}
fr.readAsText(this.files[0]);
})
// Get text input------------------------------------------------------------------
function getInputValue(){
// Selecting the input element and get its value
var inputVal = document.getElementById("inputtext").value;
var inputVal2 = document.getElementById("inputtext2").value;
// Displaying the value
dataObj.sample1 = inputVal
dataObj.sample2 = inputVal2
console.log(dataObj);
}
// Two samples ---------------------------------------
var y1 = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,1,2,3,4,5,6,7,8,9,10,11,12,13,4,1] // sample1
var y2 = [1,2,3,4,5,6,7,8,9,10,22,22,13,20,19,1,2,3,4,5,6,7,8,9,10,22,22,13,20,20] // sample2
// get 300 bootstrapped difference of means
var boot = bootstrappingMeanDiff(y1,y2,1000)
// Get stats------------------------------------------------------------------
var div = document.getElementById('myDivStats');
div.innerHTML += "95% Confidence Interval of the difference of means: "
div.innerHTML += "<br>"
div.innerHTML += confidenceInterval2(boot ,2.5);
// Get data from file---------------------------------------------------------
// https://sebhastian.com/javascript-textbox/
// Read from text box
// // create label
// const label = document.createElement("label");
// label.setAttribute("for", "inputtext");
// label.innerHTML = "inputtext: ";
// // insert label
// document.body.appendChild(label);
// create textbox
//const input = document.createElement("input");
//input.setAttribute("id", "inputtext");
//input.setAttribute("type", "text");
// insert textbox
//document.body.appendChild(input);
//----------------------------------------------------------------------------
function getRandomIndex(length) { // get a random index from a sample
return math.floor(math.random()*(length)); }
function getRandomSample(array1) { // get a random sample with replacement
var length = array1.length;
var newArray = []
for(var i = length; i--;) {
var index = getRandomIndex(length);
var temp = array1[index];
newArray.push(temp);}
return newArray}
function bootstrappingMeanDiff(arr1, arr2, size){ // get 2*n sets of random samples and subtract each pair
var meanDiffArray = []
for (var i = 0; i < size; i++) {
// get the mean of sample 1 and 2
var mean1 = math.mean(getRandomSample(arr1))
var mean2 = math.mean(getRandomSample(arr2))
var meanDiff = (math.abs(mean1-mean2)).toFixed(2)
meanDiffArray.push(meanDiff)
}
return meanDiffArray
}
function getPercent(array, percent) {
//return array.slice(0, Math.ceil(array.length * percent / 100));
return array[(math.floor(array.length * percent / 100))-1];
}
//If you don’t know your population mean (μ) but you do know the standard deviation (σ), you can find a confidence //interval for the population mean, with the formula:
//x̄ ± z* σ / (√n),
// function confidenceInterval(array,z){ // look up z: https://www.statisticshowto.com/tables/z-table/
// var ci = []
// var stdev = math.std(array)
// var ciUpper = math.mean(array)+((z*stdev)/math.sqrt(array.length))
// var ciLower = math.mean(array)-((z*stdev)/math.sqrt(array.length))
// ci.push(ciLower,ciUpper)
// return ci
// }
function confidenceInterval2(array,alpha){ // look up z: https://www.statisticshowto.com/tables/z-table/
var ci = []
var sortedArray = array.sort()
var ciUpper = getPercent(sortedArray,alpha)
var ciLower = getPercent(sortedArray,100-alpha)
ci.push(ciLower,ciUpper)
return ci
}
// Define plotly traces and layout
var layout = {
//xaxis: {rangemode: "tozero", showgrid: true, showspikes: true, spikethickness:8, tick0:8},
legend: {
x: 1.2,
y: 1,
traceorder: 'normal'
},
shapes: [ //https://plotly.com/javascript/shapes/
{ type: 'line',
yref: 'y2',
y0: math.mean(boot),
y1: math.mean(boot),
line: {color: 'black', width: 2}
}],
title: 'Altman-Gardner Plot in Plotly',
yaxis: {title: 'Measurement', dtick: 5},
// add a y-axis on the right side
yaxis2: {
showspikes: true, spikethickness:3, tick0:30, ticklen: 0,
dtick: 5,
showgrid: false,
autorange: false,
rangemode: 'tozero',
range: [1,(math.mean(y1))],// (math.mean(y1))],
scaleanchor: 'y', //https://github.com/plotly/plotly.js/issues/3539
title: 'Unpaired Mean Difference',
overlaying: 'y',
side: 'right'
},
};
var boxPlot1 = {
y:y1,
type: 'box',
boxmean: 'sd',
name: 'Group 1',
jitter: 0.3,
pointpos: -1.8,
marker: {
color: 'rgb(7,40,89)'
},
boxpoints: 'all'
};
var boxPlot2 = {
y: y2,
type: 'box',
boxmean: 'sd',
name: 'Group 2',
jitter: 0.3,
pointpos: -1.8,
marker: {
color: 'rgb(100,56,125)'
},
boxpoints: 'all'
};
var trace = {
x: 2,
y: boot,
marker: {
color: 'rgb(120, 120, 120)'
},
//spanmode: 'soft',
yaxis: 'y2',
type: 'violin',
side: 'positive', //positive side means right for vertical violin plots
name: 'Group 2 minus Group 1',
hoveron: "points+kde",
}
var traceLine = {
x: ['Group 2 minus Group 1'],
y: [math.mean(boot)], // mean of distribution
yaxis: 'y2',
name: 'Group 2 minus Group 1',
error_y: {
type: 'data',
symmetric: false,
array: [confidenceInterval2(boot,2.5 )[0]],//confidenceInterval2(boot,2.5 ), // upper and lower confidence intervals
arrayminus: [math.mean(boot)-confidenceInterval2(boot,2.5 )[1]],
visible: true
},
type: 'markers'
};
var plotData = [ boxPlot1,boxPlot2,trace,traceLine];
const myDiv = document.getElementById("myDiv");
Plotly.newPlot(myDiv, plotData, layout);
// for require-----------
if(typeof(define)!='undefined'){
//define({getRandomIndex:getRandomIndex})
define({hello:"world"})
}