-
Notifications
You must be signed in to change notification settings - Fork 0
/
jembrown.js
215 lines (186 loc) · 5.32 KB
/
jembrown.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
// General utility javascript library created by Jeremy M. Brown
function drawPermutHist(xCoor,yCoor,h,w,svg,vals){
// First element of vals is observed diff
// "..." is the spread operator - converts array into its component values
var spread = Math.max(...vals) - Math.min(...vals);
if (vals.length > 1){
var xMin = Math.min(...vals) - (spread * 0.1);
var xMax = Math.max(...vals) + (spread * 0.1);
spread = xMax - xMin;
} else {
var xMin = vals[0]-1.0;
var xMax = vals[0]+1.0;
spread = xMax - xMin;
}
// Setting cutoffs for histogram boxes
var nBoxes = 20;
var cutoffs = [xMin];
for (let i = 1; i < (nBoxes+1); i++){
cutoffs.push( xMin + (spread * (1.0/nBoxes) * i) );
}
console.log(vals);
console.log(cutoffs);
// Counting number of observations in each bin
var counts = [];
for (let i = 0; i < nBoxes; i++){
counts.push(0);
}
for (let i = 1; i < (nBoxes+1); i++){ // Looping across bins
for (let j = 1; j < vals.length; j++){ // Skips first "empirical" value
if ( (vals[j] <= cutoffs[i]) && (vals[j] > cutoffs[i-1]) ){
counts[i-1] += 1;
}
}
}
console.log(counts);
var freqs = [];
for (let i = 0; i < nBoxes; i++){
freqs.push(0);
}
for (let i = 0; i < nBoxes; i++){
freqs[i] = counts[i]/(vals.length-1);
}
// Draw x-axis
svg.append("line")
.attr("x1",xCoor-(w/2))
.attr("x2",xCoor+(w/2))
.attr("y1",yCoor)
.attr("y2",yCoor)
.attr("stroke","black")
.attr("stroke-width",2);
var xScaleFactor = w/(xMax-xMin);
// Add x-axis ticks and values
if (vals.length > 1){
svg.append("text")
.attr("y",yCoor+15)
.attr("x",(xCoor-(w/2))+((xMin-xMin)*xScaleFactor)-10)
.attr("font-family","sans-serif")
.attr("font-size","14px")
.attr("fill","black")
.text(Math.round(xMin*100)/100)
svg.append("text")
.attr("y",yCoor+15)
.attr("x",(xCoor-(w/2))+((xMax-xMin)*xScaleFactor)-10)
.attr("font-family","sans-serif")
.attr("font-size","14px")
.attr("fill","black")
.text(Math.round(xMax*100)/100)
svg.append("text")
.attr("y",yCoor+15)
.attr("x",(xCoor-(w/2))+((xMax-xMin)*0.5*xScaleFactor)-10)
.attr("font-family","sans-serif")
.attr("font-size","14px")
.attr("fill","black")
.text(Math.round(((xMin+xMax)/2.0)*100)/100)
} else {
svg.append("text")
.attr("y",yCoor+15)
.attr("x",(xCoor-(w/2))+((vals[0]-xMin)*xScaleFactor)-10)
.attr("font-family","sans-serif")
.attr("font-size","14px")
.attr("fill","black")
.text(Math.round(vals[0]*10)/10)
}
// Label x-axis
svg.append("text")
.attr("x",xCoor-50)
.attr("y",yCoor+40)
.attr("font-family","sans-serif")
.attr("font-size","16px")
.attr("fill","black")
.text("Difference in Means")
// Draw y-axis
svg.append("line")
.attr("x1",xCoor-(w/2))
.attr("x2",xCoor-(w/2))
.attr("y1",yCoor)
.attr("y2",10)
.attr("stroke","black")
.attr("stroke-width",2);
// Label y-axis
svg.append("text")
.attr("x",xCoor-(w/2)-10)
.attr("y",yCoor-25)
.attr("font-family","sans-serif")
.attr("font-size","16px")
.attr("fill","black")
.attr("transform","rotate(-90,"+(xCoor-(w/2)-10)+","+(yCoor-25)+")")
.text("Frequency")
// Label y-axis values
if (vals.length > 1){
svg.append("text")
.attr("x",xCoor-(w/2)-33)
.attr("y",18)
.attr("font-family","sans-serif")
.attr("font-size","16px")
.attr("fill","black")
.text(Math.round(Math.max(...freqs)*100)/100);
svg.append("text")
.attr("x",xCoor-(w/2)-33)
.attr("y",yCoor+5)
.attr("font-family","sans-serif")
.attr("font-size","16px")
.attr("fill","black")
.text("0.0");
}
// Draw histogram boxes
if (vals.length > 1){
for (let i = 1; i < (nBoxes+1); i++){
svg.append("rect")
.attr("x",xCoor-(w/2)+(w * (1.0/nBoxes) * (i-1)))
.attr("y",yCoor-((freqs[i-1]/Math.max(...freqs)) * (yCoor-10)))
.attr("width",(1.0/nBoxes) * w)
.attr("height",(freqs[i-1]/Math.max(...freqs)) * (yCoor-10) )
.attr("fill","rgba(0,0,255,0.5)")
}
}
// Show observed diff
svg.append("line")
.attr("x1",(xCoor-(w/2))+((vals[0]-xMin)*xScaleFactor))
.attr("x2",(xCoor-(w/2))+((vals[0]-xMin)*xScaleFactor))
.attr("y1",yCoor)
.attr("y2",10)
.attr("stroke","red")
.attr("stroke-width",3)
}
function permuteLabels(perLabels,origLabels){
var newLabels = [];
while (origLabels.length > 0){
var index = Math.floor(Math.random()*origLabels.length);
newLabels.push(origLabels.splice(index,1)[0]);
}
perLabels.push(newLabels);
}
function calcPerDiffs(vals,diffArray,newLabels,mArray,fArray){
var mVals = [];
var fVals = [];
for (let i = 0; i < newLabels.length; i++){
if (newLabels[i] == "M"){
mVals.push(vals[i]);
} else {
fVals.push(vals[i]);
}
}
var newDiff = avg(fVals) - avg(mVals);
mArray.push(avg(mVals));
fArray.push(avg(fVals));
diffArray.push(newDiff);
}
// Function from: https://www.codementor.io/avijitgupta/deep-copying-in-js-7x6q8vh5d
// Although those authors also copied it from an (unknown) source?
function copy(o) {
var output, v, key;
output = Array.isArray(o) ? [] : {};
for (key in o) {
v = o[key];
output[key] = (typeof v === "object") ? copy(v) : v;
}
return output;
}
function avg(arr){
let mySum = 0.0;
for (let i = 0; i < arr.length; i++){
mySum += arr[i];
}
return mySum/arr.length;
}