-
Notifications
You must be signed in to change notification settings - Fork 2
/
chi.html
269 lines (244 loc) · 8.32 KB
/
chi.html
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
<html>
<head>
<title>Statistical significance calculator (JavaScript chi-squared test)</title>
</head>
<body onclick="doit()">
<h1>Statistical significance calculator</h1>
<p>This tool calculates statistical significance for little experiments.</p>
<table onkeyup="doit()" onchange="doit()" onblur="doit()">
<tr>
<td></td>
<th>Sample</th>
<th>Affected</th>
<th>Conversion Rate</th>
</tr>
<tr>
<th>Group A</th>
<td><input type="text" id="n1" value="5000" /></td>
<td><input type="text" id="x1" value="100" /></td>
<td><input type="text" id="c1" /></td>
</tr>
<tr>
<th>Group B</th>
<td><input type="text" id="n2" value="5000" /></td>
<td><input type="text" id="x2" value="130" /></td>
<td><input type="text" id="c2" /></td>
</tr>
<tr>
<td></td>
<td></td>
<td></td>
<td><input type="text" id="diff" /></td></td>
</tr>
</table>
<div id="output" onclick="doit()" style="height: 5.75em"></div>
<p><em>Just so you know...</em><br />
<strong style='color: #a00'>89%</strong> or less could just be a fluke<br />
<strong style='color: #aa0'>90%</strong> is pretty good for a rough sense<br />
<strong style='color: #0a0'>95%</strong> is good enough for academic publishing<br />
<strong style='color: #0d0'>99%</strong> or higher is <em>awesome</em>
</p>
<p><small>...but this is all assuming you set up the experiment correctly.</small></p>
<!--TODO:
- how big a sample should I use? calculator
-->
</body>
<script type="text/javascript">
function degrees_of_freedom(table) { return (table.length - 1) * (table[0].length - 1) }
function expected_value(table, row, column) {
var sum_row, sum_column, sum_total, i, j;
sum_row = 0;
for (i in table[row]) { sum_row += table[row][i]; }
sum_column = 0;
for (i in table) { sum_column += table[i][column]; }
sum_total = 0;
for (i in table) { for (j in table[i]) { sum_total += table[i][j]; } }
return (sum_row * sum_column) / sum_total;
}
function chisq(table) {
var chisq, i, j, unexpected;
chisq = 0;
for (i in table) {
for (j in table[i]) {
unexpected = table[i][j] - expected_value(table, i, j)
chisq += Math.pow(unexpected, 2)/expected_value(table, i, j)
}
}
return chisq;
}
/* from http://www.fourmilab.ch/rpkp/experiments/analysis/chiCalc.js */
function poz(z) {
var y, x, w;
var Z_MAX = 6.0; /* Maximum meaningful z value */
if (z == 0.0) {
x = 0.0;
} else {
y = 0.5 * Math.abs(z);
if (y >= (Z_MAX * 0.5)) {
x = 1.0;
} else if (y < 1.0) {
w = y * y;
x = ((((((((0.000124818987 * w
- 0.001075204047) * w + 0.005198775019) * w
- 0.019198292004) * w + 0.059054035642) * w
- 0.151968751364) * w + 0.319152932694) * w
- 0.531923007300) * w + 0.797884560593) * y * 2.0;
} else {
y -= 2.0;
x = (((((((((((((-0.000045255659 * y
+ 0.000152529290) * y - 0.000019538132) * y
- 0.000676904986) * y + 0.001390604284) * y
- 0.000794620820) * y - 0.002034254874) * y
+ 0.006549791214) * y - 0.010557625006) * y
+ 0.011630447319) * y - 0.009279453341) * y
+ 0.005353579108) * y - 0.002141268741) * y
+ 0.000535310849) * y + 0.999936657524;
}
}
return z > 0.0 ? ((x + 1.0) * 0.5) : ((1.0 - x) * 0.5);
}
var BIGX = 20.0; /* max value to represent exp(x) */
function ex(x) {
return (x < -BIGX) ? 0.0 : Math.exp(x);
}
function pochisq(x, df) {
var a, y, s;
var e, c, z;
var even; /* True if df is an even number */
var LOG_SQRT_PI = 0.5723649429247000870717135; /* log(sqrt(pi)) */
var I_SQRT_PI = 0.5641895835477562869480795; /* 1 / sqrt(pi) */
if (x <= 0.0 || df < 1) {
return 1.0;
}
a = 0.5 * x;
even = !(df & 1);
if (df > 1) {
y = ex(-a);
}
s = (even ? y : (2.0 * poz(-Math.sqrt(x))));
if (df > 2) {
x = 0.5 * (df - 1.0);
z = (even ? 1.0 : 0.5);
if (a > BIGX) {
e = (even ? 0.0 : LOG_SQRT_PI);
c = Math.log(a);
while (z <= x) {
e = Math.log(z) + e;
s += ex(c * z - a - e);
z += 1.0;
}
return s;
} else {
e = (even ? 1.0 : (I_SQRT_PI / Math.sqrt(a)));
c = 0.0;
while (z <= x) {
e = e * (a / z);
c = c + e;
z += 1.0;
}
return c * y + s;
}
} else {
return s;
}
}
function trimfloat(ov, d) {
var o = "", v = ov.toString();
var c, i, n = 0, indec = false, aftdec = false;
for (i = 0; i < v.length; i++) {
c = v.charAt(i);
if (!indec) {
if (c == '.') {
indec = true;
}
if (d > 0 || c != '.') { o += c; }
} else {
if (aftdec) {
o += c;
} else {
if ((c >= '0') && (c <= '9')) {
if (n < d) {
o += c;
}
n++;
} else {
aftdec = true;
o += c;
}
}
}
}
return o;
}
function significance(table) { return 1 - pochisq(chisq(table), degrees_of_freedom(table)) }
</script>
<script type="text/javascript">
/* want to validate that this works? here's the R code to compare against:
> data <- matrix(c(11,32,206-11,1374-32), nrow=2)
> chisq.test(data, correct=FALSE)
*/
function v(eid) {
return parseInt(document.getElementById(eid).value);
}
function doit(){
var table, result, nice_result, output, after, bigger_group;
table = [[v('x1'), v('n1')-v('x1')], [v('x2'), v('n2')-v('x2')]];
result = significance(table);
niceresult = trimfloat(result*100, 2)
if (niceresult == '100') { niceresult = '99.999'}
if (v('x1')/v('n1') > v('x2')/v('n2')) {
bigger_group = 'A';
improvement = v('x1')/v('n1') - v('x2')/v('n2');
} else {
bigger_group = 'B';
improvement = v('x2')/v('n2') - v('x1')/v('n1');
}
after = " chance that the difference isn't just due to chance.</p><p><small>In other words, while it looks like Group " + bigger_group + " did better, " + trimfloat((1-result)*100, 2) + "% of the time that will just be a coincidence.</small></p>"
if (v('n1') < v('x1') || v('n2') < v('x2')) {
output = "<strong style='color: #f00'>Oops:</strong> I think you got your <strong>Sample</strong> and your <strong>Number affected</strong> mixed up. <a href='javascript:flipit()'>Want me to flip them for you?</a>"
} else if (isNaN(result)) {
output = "Please enter your test data above."
} else if ( result < .5 ) {
output = "Dude, there's only a <strong style='color: #f00'>" + niceresult + "%</strong>" + after;
} else if (result < .9) {
output = "Hmm, there's only a <strong style='color: #a00'>" + niceresult + "%</strong>" + after;
} else if ( result < .95) {
output = "Nice, there's a <strong style='color: #aa0'>" + niceresult + "%</strong>" + after;
} else if ( result < .99) {
output = "Yay! There's a <strong style='color: #0a0'>" + niceresult + "%</strong>" + after;
} else if ( result < 1.1) {
output = "<em>W00t!</em> There's a <strong style='color: #0d0'>" + niceresult + "%</strong>" + after;
} else {
output = result;
}
document.getElementById('output').innerHTML = '<p>' + output;
var n1 = document.getElementById("n1").value;
var x1 = document.getElementById("x1").value;
var n2 = document.getElementById("n2").value;
var x2 = document.getElementById("x2").value;
var c1 = (x1 / n1) * 100;
var c2 = (x2 / n2) * 100;
document.getElementById('c1').value = Math.round(c1 * 100) / 100 + '%';
document.getElementById('c2').value = Math.round(c2 * 100) / 100 + '%';
var diff = ((c2 - c1) / c1) * 100;
document.getElementById('diff').value = Math.round(diff * 100) / 100 + '%';;
}
function flipit() {
var n1, n2, x1, x2;
if (v('n1') < v('x1')) {
n1 = v('n1');
x1 = v('x1');
document.getElementById('n1').value = x1;
document.getElementById('x1').value = n1;
}
if (v('n2') < v('x2')) {
n2 = v('n2');
x2 = v('x2');
document.getElementById('n2').value = x2;
document.getElementById('x2').value = n2;
}
doit()
}
doit();
</script>
</html>