-
Notifications
You must be signed in to change notification settings - Fork 0
/
pt.js
executable file
·255 lines (238 loc) · 9.23 KB
/
pt.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
"use strict";
function init() {
var n = 4;
var t = 1.5;
var mt = -1.5;
var p = 0.104;
document.getElementById("df").value = n;
document.getElementById("t").value = t;
document.getElementById("p-value").value = p;
document.getElementById("ptype0").checked = false;
document.getElementById("ptype1").checked = true;
document.getElementById("ptype2").checked = false;
document.getElementById("ptype3").checked = false;
document.getElementById("input").style.color = "black";
document.getElementById("input").innerHTML = "t = "+t+", df = "+n;
document.getElementById("output").innerHTML = "Right-tail p-value is "+p;
document.getElementById("Rcommand").innerHTML = "R command: \xa0 \xa0 \xa0 pt("+mt+", "+n+") \xa0 \xa0 \xa0 or \xa0 \xa0 \xa0 pt("+t+", "+n+", lower.tail=FALSE)";
drawCurve(t,n, 1);
}
function changePtype(form,type) {
var ptype = parseInt(type);
if (ptype==0) {
document.getElementById("ptype0").checked = true;
document.getElementById("ptype1").checked = false;
document.getElementById("ptype2").checked = false;
document.getElementById("ptype3").checked = false;
form.pbutton.value = "Compute p-value";
document.getElementById("pdisplay").innerHTML = "p-value";
} else if (ptype==1) {
document.getElementById("ptype0").checked = false;
document.getElementById("ptype1").checked = true;
document.getElementById("ptype2").checked = false;
document.getElementById("ptype3").checked = false;
form.pbutton.value = "Compute p-value";
document.getElementById("pdisplay").innerHTML = "p-value";
} else if (ptype==2) {
document.getElementById("ptype0").checked = false;
document.getElementById("ptype1").checked = false;
document.getElementById("ptype2").checked = true;
document.getElementById("ptype3").checked = false;
form.pbutton.value = "Compute p-value";
document.getElementById("pdisplay").innerHTML = "p-value";
} else {
document.getElementById("ptype0").checked = false;
document.getElementById("ptype1").checked = false;
document.getElementById("ptype2").checked = false;
document.getElementById("ptype3").checked = true;
form.pbutton.value = "Compute middle area";
document.getElementById("pdisplay").innerHTML = "middle area";
}
var outputStr = document.getElementById("output").innerHTML;
if (outputStr.indexOf("p-value") != -1 || outputStr.indexOf("Middle area") != -1) {
Pfromt_t(form);
} else if (outputStr.indexOf("t-value") != -1) {
tfromP_t(form);
} else {
var n = 4;
var t = 1.5;
document.getElementById("df").value = n;
document.getElementById("t").value = t;
Pfromt_t(form);
}
}
function Pfromt_t(form) {
var n = parseInt(form.df.value);
var t = parseFloat(form.t.value);
// Sanity check
if (!isFinite(n) || n<1) {
var x = document.getElementById("input");
x.style.color = "red";
document.getElementById("input").innerHTML = "Invalid input! The degree of freedom must be a positive integer.";
document.getElementById("output").innerHTML = "";
document.getElementById("Rcommand").innerHTML = "";
form.df.value = "";
form.p_value.value = "";
clearCanvas('tcurve');
return;
}
if (n != parseFloat(form.df.value)) {
form.df.value = n;
}
if (!isFinite(t)) {
var x = document.getElementById("input");
x.style.color = "red";
document.getElementById("input").innerHTML = "Invalid input! You entered a non-numerical value for t.";
document.getElementById("output").innerHTML = "";
document.getElementById("Rcommand").innerHTML = "";
form.t.value = "";
clearCanvas('tcurve');
return;
}
var ptype;
if (document.getElementById("ptype0").checked) {
ptype = 0;
} else if (document.getElementById("ptype1").checked) {
ptype = 1;
} else if (document.getElementById("ptype2").checked) {
ptype = 2;
} else {
ptype = 3;
}
var abst = Math.abs(t);
var mt= -t;
if (ptype>1) { mt = -abst;}
var p = pt(t,n,ptype);
var pval = 1.0*p.toPrecision(4);
form.p_value.value = pval;
document.getElementById("input").style.color = "black";
document.getElementById("input").innerHTML = "t = "+t+", df = "+n;
if (ptype==0) {
document.getElementById("output").innerHTML = "Left-tail p-value is "+pval;
document.getElementById("Rcommand").innerHTML = "R command: \xa0 \xa0 \xa0 pt("+t+", "+n+")";
} else if (ptype==1) {
document.getElementById("output").innerHTML = "Right-tail p-value is "+pval;
document.getElementById("Rcommand").innerHTML = "R command: \xa0 \xa0 \xa0 pt("+mt+", "+n+") \xa0 \xa0 \xa0 or \xa0 \xa0 \xa0 pt("+t+", "+n+", lower.tail=FALSE)";
} else if (ptype==2) {
document.getElementById("output").innerHTML = "Two-tails p-value is "+pval;
document.getElementById("Rcommand").innerHTML = "R command: \xa0 \xa0 \xa0 2*pt("+mt+", "+n+") \xa0 \xa0 \xa0 or \xa0 \xa0 \xa0 2*pt("+abst+", "+n+", lower.tail=FALSE)";
} else {
document.getElementById("output").innerHTML = "Middle area is "+pval;
document.getElementById("Rcommand").innerHTML = "R command: \xa0 \xa0 \xa0 1-2*pt("+mt+", "+n+")";
}
drawCurve(t, n, ptype);
}
function tfromP_t(form) {
var n = parseInt(form.df.value);
var p = parseFloat(form.p_value.value);
// Sanity check
if (!isFinite(n) || n<1) {
var x = document.getElementById("input");
x.style.color = "red";
document.getElementById("input").innerHTML = "Invalid input! The degree of freedom must be a positive integer.";
document.getElementById("output").innerHTML = "";
document.getElementById("Rcommand").innerHTML = "";
form.df.value = "";
clearCanvas('tcurve');
return;
}
if (n != parseFloat(form.df.value)) {
form.df.value = n;
}
if (!isFinite(p) || p < 0 || p>1) {
var x = document.getElementById("input");
x.style.color = "red";
document.getElementById("input").innerHTML = "Invalid input! P-value must be between 0 and 1.";
document.getElementById("output").innerHTML = "";
document.getElementById("Rcommand").innerHTML = "";
form.t.value = "";
clearCanvas('tcurve');
return;
}
var ptype;
if (document.getElementById("ptype0").checked) {
ptype = 0;
} else if (document.getElementById("ptype1").checked) {
ptype = 1;
} else if (document.getElementById("ptype2").checked) {
ptype = 2;
} else {
ptype = 3;
}
var t = qt(p,n,ptype);
var tval = 1.0*t.toPrecision(4);
form.t.value = tval;
document.getElementById("input").style.color = "black";
document.getElementById("output").innerHTML = "t-value is "+tval;
if (ptype==0) {
document.getElementById("input").innerHTML = "Left-tail p-value is "+p+", df = "+n;
document.getElementById("Rcommand").innerHTML = "R command: \xa0 \xa0 \xa0 qt("+p+", "+n+")";
} else if (ptype==1) {
document.getElementById("input").innerHTML = "Right-tail p-value is "+p+", df = "+n;
document.getElementById("Rcommand").innerHTML = "R command: \xa0 \xa0 \xa0 -qt("+p+", "+n+") \xa0 \xa0 \xa0 or \xa0 \xa0 \xa0 qt("+p+", "+n+", lower.tail=FALSE)";
} else if (ptype==2) {
document.getElementById("input").innerHTML = "Two-tails p-value is "+p+", df = "+n;
document.getElementById("Rcommand").innerHTML = "R command: \xa0 \xa0 \xa0 -qt("+p+"/2, "+n+") \xa0 \xa0 \xa0 or \xa0 \xa0 \xa0 qt("+p+"/2, "+n+", lower.tail=FALSE)";
} else {
document.getElementById("input").innerHTML = "Middle area is "+p+", df = "+n;
document.getElementById("Rcommand").innerHTML = "R command: \xa0 \xa0 \xa0 -qt( (1-"+p+")/2, "+n+")";
}
drawCurve(t, n, ptype);
}
// Draw t curve
function drawCurve(t,n, ptype) {
var Canvas = document.getElementById('tcurve');
var Ctx = null;
var Width = Canvas.width;
var Height = Canvas.height;
var sd;
if (n > 2) {
sd = Math.sqrt(1.0*n/(n-2));
} else {
sd = 2.0;
}
var maxX = 3.2*sd;
var deltaX = 1.0;
var abst = Math.abs(t);
if (abst > 3) {
maxX = abst*1.1;
maxX = Math.min(maxX, 15);
deltaX = Math.floor(maxX/3);
}
var minX = -maxX;
var maxY = 1.1;
var minY = -0.15;
// t density function
var ft = function(x) { return Math.pow(1.0+x*x/n, -0.5*(n+1));}
if (Canvas.getContext) {
// Set up the canvas:
Ctx = Canvas.getContext('2d');
Ctx.clearRect(0,0,Width,Height);
// setup graph parameters
var gpar = {"minX":minX, "maxX":maxX, "minY":minY, "maxY":maxY,
"Width":Width, "Height":Height};
// Draw:
var messages = {message:"t value out of plotting range!",
x1Message: 5, y1Message: 0.5,
x2Message: 5, y2Message: 0.5};
var fillColor = "#87CEEB";
if (ptype==0) {
if (t < 0) {messages.x1Message = -14;}
ShadeCurve(Ctx, ft, t,0, 1, fillColor, messages, gpar);
} else if (ptype==1) {
if (t < 0) {messages.x1Message = -14;}
ShadeCurve(Ctx, ft, t,0, 2, fillColor, messages, gpar);
} else if (ptype==2) {
ShadeCurve(Ctx, ft, abst,0, 2, fillColor, messages, gpar);
messages.x1Message = -14;
ShadeCurve(Ctx, ft, -abst,0, 1, fillColor, messages, gpar);
} else {
messages.x1Message = -14;
ShadeCurve(Ctx, ft, -abst,abst, 3, fillColor, messages, gpar);
}
DrawAxes(Ctx, gpar, deltaX, false);
RenderFunction(Ctx, ft, gpar);
} else {
// Do nothing.
}
}