-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathboot.js
47 lines (44 loc) · 1.86 KB
/
boot.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
console.log('boot.js loaded')
getRandomIndex = function(length) { // get a random index from a sample
return Math.floor(Math.random()*(length)); }
getRandomSample = function(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}
bootstrappingMeanDiff = function(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
}
getPercent = function(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
// }
confidenceInterval2 = function(array,alpha){ // look up z: https://www.statisticshowto.com/tables/z-table/
var ci = []
var sortedArray = array.sort()
var ciUpper = getPercent(array,alpha)
var ciLower = getPercent(array,100-alpha)
ci.push(ciLower,ciUpper)
return ci
}