1 | var R=require("lodash");
|
2 | var J=require("jStat").jStat;
|
3 |
|
4 | var repeats=function(f, x, n) {
|
5 | var a=[];
|
6 | for (var i=0; i<n; i++) {
|
7 | a.push(f(x));
|
8 | }
|
9 | return R(a);
|
10 | }
|
11 |
|
12 | R.mixin({repeats:repeats});
|
13 |
|
14 | var calls=function() {
|
15 | var args = Array.prototype.slice.call(arguments);
|
16 | var n = args[0];
|
17 | var f = args[1];
|
18 | var params = args.slice(2, args.length);
|
19 | var a=[];
|
20 | for (var i=0; i<n; i++)
|
21 | a.push(f.apply(null, params));
|
22 | return R(a);
|
23 | }
|
24 |
|
25 | R.mixin({calls:calls});
|
26 |
|
27 | var samples=function(space, size, arg) {
|
28 | var arg = R.defaults(arg, {replace:true});
|
29 | if (arg.replace)
|
30 | return R.chain(R.sampleSize(space, size));
|
31 | else
|
32 | return R.repeats(R.sample, space, size);
|
33 | }
|
34 |
|
35 | R.mixin({samples:samples});
|
36 |
|
37 | R.steps=function(start, end, step) {
|
38 | step = step || 1;
|
39 | return R.range(start,end+0.00001*step, step);
|
40 | }
|
41 |
|
42 |
|
43 | R.precision = 2;
|
44 |
|
45 |
|
46 | R.num2str=function(x) {
|
47 | if (isNaN(x))
|
48 | return "NaN";
|
49 | else if (Math.floor(x) === x)
|
50 | return x.toString();
|
51 | else if (isFinite(x))
|
52 | return R.ctrim(x.toFixed(R.precision), "0", "right");
|
53 | else if (x < 0)
|
54 | return "-Infinity";
|
55 | else
|
56 | return "Infinity";
|
57 | }
|
58 |
|
59 | R.ctrim=function(s, set, side) {
|
60 | side = side||"both";
|
61 | for (var b=0; b<s.length; b++)
|
62 | if (set.indexOf(s[b])<0) break;
|
63 | for (var e=s.length-1; e>=0; e--)
|
64 | if (set.indexOf(s[e])<0) break;
|
65 | if (side === "right") b=0;
|
66 | if (side === "left") e=s.length-1;
|
67 | return s.substring(b, e+1);
|
68 | }
|
69 |
|
70 | R.array2str=function(x) {
|
71 | var s = "";
|
72 | for (var i in x)
|
73 | s+= R.str(x[i])+", ";
|
74 | return "["+R.ctrim(s, ", ")+"]";
|
75 | }
|
76 |
|
77 | R.obj2str=function(x, sp) {
|
78 | var s = "";
|
79 | for (var k in x)
|
80 | s+= k+":"+R.str(x[k])+sp;
|
81 | return "{"+R.ctrim(s,sp)+"}";
|
82 | }
|
83 |
|
84 | var str = function(x) {
|
85 | if (typeof x === "number") return R.num2str(x);
|
86 | else if(typeof x === "string") return '\"'+x.toString()+'\"';
|
87 | else if (x instanceof Array) return R.array2str(x);
|
88 | else if (typeof x === "object") return R.obj2str(x, ",");
|
89 | else return x.toString();
|
90 | }
|
91 |
|
92 | R.mixin({str:str}, {chain:false});
|
93 |
|
94 |
|
95 |
|
96 | var sd = function(a, flag) {
|
97 | flag = flag || 1;
|
98 | return J.stdev(a, flag);
|
99 | }
|
100 |
|
101 | R.mixin({sd:sd}, {chain:false});
|
102 |
|
103 |
|
104 | R.cov = function(x, y) { return J.stdev(x, y); }
|
105 |
|
106 | R.cor = function(x, y) { return J.corrcoeff(x, y); }
|
107 |
|
108 | R.factorial = function(n) { return J.factorial(n); }
|
109 |
|
110 | R.lfactorial = function(n) { return J.factorialln(n); }
|
111 |
|
112 | R.choose = function(n,m) { return J.combination(n, m); }
|
113 |
|
114 | R.lchoose = function(n,m) { return J.combinationln(n, m); }
|
115 |
|
116 | R.permutation = function(n,m) { return J.permutation(n, m); }
|
117 |
|
118 | R.lchoose = function(n,m) { return J.combinationln(n, m); }
|
119 |
|
120 |
|
121 | R.runif=function(n, a, b) { return R.calls(n, J.uniform.sample, a, b); }
|
122 | R.dunif=function(x, a, b) { return J.uniform.pdf(x, a, b); }
|
123 | R.punif=function(q, a, b) { return J.uniform.cdf(q, a, b); }
|
124 | R.qunif=function(p, a, b) { return J.uniform.inv(p, a, b); }
|
125 |
|
126 | R.rnorm=function(n, mean, sd) { return R.calls(n, J.normal.sample, mean, sd); }
|
127 | R.dnorm=function(x, mean, sd) { return J.normal.pdf(x, mean, sd); }
|
128 | R.pnorm=function(q, mean, sd) { return J.normal.cdf(q, mean, sd); }
|
129 | R.qnorm=function(p, mean, sd) { return J.normal.inv(p, mean, sd); }
|
130 |
|
131 | R.rpois=function(n, l) { return R.calls(n, J.poisson.sample, l); }
|
132 | R.dpois=function(x, l) { return J.poisson.pdf(x, l); }
|
133 | R.ppois=function(q, l) { return J.poisson.cdf(q, l); }
|
134 | R.qpois=function(p, l) { return J.poisson.inv(p, l); }
|
135 |
|
136 | R.rf=function(n, df1, df2) { return R.calls(n, J.centralF.sample, df1, df2); }
|
137 | R.df=function(x, df1, df2) { return J.centralF.pdf(x, df1, df2); }
|
138 | R.pf=function(q, df1, df2) { return J.centralF.cdf(q, df1, df2); }
|
139 | R.qf=function(p, df1, df2) { return J.centralF.inv(p, df1, df2); }
|
140 |
|
141 | R.rt=function(n, dof) { return R.calls(n, J.studentt.sample, dof); }
|
142 | R.dt=function(x, dof) { return J.studentt.pdf(x, dof); }
|
143 | R.pt=function(q, dof) { return J.studentt.cdf(q, dof); }
|
144 | R.qt=function(p, dof) { return J.studentt.inv(p, dof); }
|
145 |
|
146 | R.rbeta=function(n, alpha, beta) { return R.calls(n, J.beta.sample, alpha, beta); }
|
147 | R.dbeta=function(x, alpha, beta) { return J.beta.pdf(x, alpha, beta); }
|
148 | R.pbeta=function(q, alpha, beta) { return J.beta.cdf(q, alpha, beta); }
|
149 | R.qbeta=function(p, alpha, beta) { return J.beta.inv(p, alpha, beta); }
|
150 |
|
151 | R.rcauchy=function(n, local, scale) { return R.calls(n, J.cauchy.sample, local, scale); }
|
152 | R.dcauchy=function(x, local, scale) { return J.cauchy.pdf(x, local, scale); }
|
153 | R.pcauchy=function(q, local, scale) { return J.cauchy.cdf(q, local, scale); }
|
154 | R.qcauchy=function(p, local, scale) { return J.cauchy.inv(p, local, scale); }
|
155 |
|
156 | R.rchisq=function(n, dof) { return R.calls(n, J.chisquare.sample, dof); }
|
157 | R.dchisq=function(x, dof) { return J.chisquare.pdf(x, dof); }
|
158 | R.pchisq=function(q, dof) { return J.chisquare.cdf(q, dof); }
|
159 | R.qchisq=function(p, dof) { return J.chisquare.inv(p, dof); }
|
160 |
|
161 | R.rexp=function(n, rate) { return R.calls(n, J.exponential.sample, rate); }
|
162 | R.dexp=function(x, rate) { return J.exponential.pdf(x, rate); }
|
163 | R.pexp=function(q, rate) { return J.exponential.cdf(q, rate); }
|
164 | R.qexp=function(p, rate) { return J.exponential.inv(p, rate); }
|
165 |
|
166 | R.rgamma=function(n, shape, scale) { return R.calls(n, J.gamma.sample, shape, scale); }
|
167 | R.dgamma=function(x, shape, scale) { return J.gamma.pdf(x, shape, scale); }
|
168 | R.pgamma=function(q, shape, scale) { return J.gamma.cdf(q, shape, scale); }
|
169 | R.qgamma=function(p, shape, scale) { return J.gamma.inv(p, shape, scale); }
|
170 |
|
171 | R.rinvgamma=function(n, shape, scale) { return R.calls(n, J.invgamma.sample, shape, scale); }
|
172 | R.dinvgamma=function(x, shape, scale) { return J.invgamma.pdf(x, shape, scale); }
|
173 | R.pinvgamma=function(q, shape, scale) { return J.invgamma.cdf(q, shape, scale); }
|
174 | R.qinvgamma=function(p, shape, scale) { return J.invgamma.inv(p, shape, scale); }
|
175 |
|
176 | R.rlognormal=function(n, mu, sigma) { return R.calls(n, J.lognormal.sample, mu, sigma); }
|
177 | R.dlognormal=function(x, mu, sigma) { return J.lognormal.pdf(x, mu, sigma); }
|
178 | R.plognormal=function(q, mu, sigma) { return J.lognormal.cdf(q, mu, sigma); }
|
179 | R.qlognormal=function(p, mu, sigma) { return J.lognormal.inv(p, mu, sigma); }
|
180 |
|
181 | R.rpareto=function(n, scale, shape) { return R.calls(n, J.pareto.sample, scale, shape); }
|
182 | R.dpareto=function(x, scale, shape) { return J.pareto.pdf(x, scale, shape); }
|
183 | R.ppareto=function(q, scale, shape) { return J.pareto.cdf(q, scale, shape); }
|
184 | R.qpareto=function(p, scale, shape) { return J.pareto.inv(p, scale, shape); }
|
185 |
|
186 | R.rweibull=function(n, scale, shape) { return R.calls(n, J.weibull.sample, scale, shape); }
|
187 | R.dweibull=function(x, scale, shape) { return J.weibull.pdf(x, scale, shape); }
|
188 | R.pweibull=function(q, scale, shape) { return J.weibull.cdf(q, scale, shape); }
|
189 | R.qweibull=function(p, scale, shape) { return J.weibull.inv(p, scale, shape); }
|
190 |
|
191 | R.rtriangular=function(n, a, b, c) { return R.calls(n, J.triangular.sample, a, b, c); }
|
192 | R.dtriangular=function(x, a, b, c) { return J.triangular.pdf(x, a, b, c); }
|
193 | R.ptriangular=function(q, a, b, c) { return J.triangular.cdf(q, a, b, c); }
|
194 | R.qtriangular=function(p, a, b, c) { return J.triangular.inv(p, a, b, c); }
|
195 |
|
196 | R.rkumaraswamy=function(n, alpha, beta) { return R.calls(n, J.kumaraswamy.sample, alpha, beta); }
|
197 | R.dkumaraswamy=function(x, alpha, beta) { return J.kumaraswamy.pdf(x, alpha, beta); }
|
198 | R.pkumaraswamy=function(q, alpha, beta) { return J.kumaraswamy.cdf(q, alpha, beta); }
|
199 | R.qkumaraswamy=function(p, alpha, beta) { return J.kumaraswamy.inv(p, alpha, beta); }
|
200 |
|
201 |
|
202 | var qf=function(cdf, q, N, p) {
|
203 | for (var i=0; i<=N; i++) {
|
204 | if (cdf(i, N, p) > q) return i;
|
205 | }
|
206 | return N;
|
207 | }
|
208 | var rf=function(cdf, n, N, p) {
|
209 | var samples = [];
|
210 | for (var i=0; i<n; i++) {
|
211 | var q = Math.random();
|
212 | samples.push(cdf(q, N, p));
|
213 | }
|
214 | return samples;
|
215 | }
|
216 |
|
217 | R.dbinom=function(x, N, p) { return J.binomial.pdf(x, N, p); }
|
218 | R.pbinom=function(k, N, p) { return J.binomial.cdf(k, N, p); }
|
219 | R.qbinom=function(q, N, p) { return qf(R.pbinom, q, N, p); }
|
220 | R.rbinom=function(n, N, p) { return rf(R.qbinom, n, N, p); }
|
221 |
|
222 | R.dnbinom=function(x, N, p) { return jStat.negbin.pdf(x, N, p); }
|
223 | R.pnbinom=function(k, N, p) { return jStat.negbin.cdf(k, N, p); }
|
224 | R.qnbinom=function(q, N, p) { return qf(R.pnbinom, q, N, p); }
|
225 | R.rnbinom=function(n, N, p) { return rf(R.qnbinom, n, N, p); }
|
226 |
|
227 | R.dhyper=function(x, N, m, n) { return jStat.hypgeom.pdf(x, N, m, n); }
|
228 | R.phyper=function(k, N, m, n) { return jStat.hypgeom.cdf(k, N, m, n); }
|
229 | R.qhyper=function(q, N, m, n) { return qf(R.phyper, q, N, p); }
|
230 | R.rhyper=function(n, N, m, k) { return rf(R.qhyper, n, N, p); }
|
231 |
|
232 |
|
233 |
|
234 |
|
235 | function opAlt(op) {
|
236 | if (op === "=") return "!=";
|
237 | if (op === "<") return ">=";
|
238 | if (op === ">") return "<=";
|
239 | return null;
|
240 | }
|
241 |
|
242 | R.test = function(o) {
|
243 | o = R.defaults(o, {alpha:0.05, op:"="});
|
244 | var alpha = o.alpha;
|
245 | var pvalue, interval;
|
246 | var D = o.D;
|
247 | var q1 = D.o2q(o);
|
248 |
|
249 | if (o.op === "=") {
|
250 | if (q1>0.5) q1 = 1-q1;
|
251 | pvalue= 2*q1;
|
252 | interval = [D.q2p(alpha/2, o, "L"), D.q2p(1-alpha/2, o, "R")];
|
253 | } else {
|
254 | if (o.op === "<") {
|
255 | interval = [ D.q2p(alpha, o, "L"), Infinity ];
|
256 | pvalue = 1-q1;
|
257 | }
|
258 | if (o.op === ">") {
|
259 | interval=[-Infinity, D.q2p(1-alpha, o, "R")];
|
260 | pvalue = q1;
|
261 | }
|
262 | }
|
263 | return {
|
264 | name: o.name,
|
265 | h: D.h(o),
|
266 | alpha: alpha,
|
267 | op: o.op,
|
268 | pvalue: pvalue,
|
269 | ci : interval,
|
270 | df : D.df(o),
|
271 | report: function() { R.report(this) }
|
272 | };
|
273 | }
|
274 |
|
275 | var t1 = {
|
276 | h:function(o) { return "H0:mu"+o.op+o.mu; },
|
277 | o2q:function(o) {
|
278 | var x = o.x, n = x.length;
|
279 | var t = (R.mean(x)-o.mu)/(R.sd(x)/Math.sqrt(n));
|
280 | return R.pt(t, n-1);
|
281 | },
|
282 |
|
283 |
|
284 | q2p:function(q, o) {
|
285 | var x = o.x, n = x.length;
|
286 | return R.mean(x) + R.qt(q, n-1) * R.sd(x) / Math.sqrt(n);
|
287 | },
|
288 | df:function(o) { return o.x.length-1; }
|
289 | }
|
290 |
|
291 | var t2vareq = {
|
292 | h:function(o) { return "H0:mu1"+o.op+"mu2" },
|
293 |
|
294 | sd:function(o) {
|
295 | var x = o.x, n1 = x.length, y=o.y, n2=y.length;
|
296 | var S1= R.sd(x), S2 = R.sd(y);
|
297 | var S = Math.sqrt(((n1-1)*S1*S1+(n2-1)*S2*S2)/(n1-1+n2-1));
|
298 | return S;
|
299 | },
|
300 |
|
301 | o2q:function(o) {
|
302 | var x = o.x, n1 = x.length, y=o.y, n2=y.length;
|
303 | var S = this.sd(o);
|
304 | var t = (R.mean(x)-R.mean(y)-o.mu)/(Math.sqrt(1/n1+1/n2)*S);
|
305 | return R.pt(t, n1+n2-2);
|
306 | },
|
307 |
|
308 | q2p:function(q, o) {
|
309 | var x = o.x, n1 = x.length, y=o.y, n2=y.length;
|
310 | var S = this.sd(o);
|
311 | return R.mean(x)-R.mean(y)+ R.qt(q, n1+n2-2)*Math.sqrt(1/n1+1/n2)*S;
|
312 | },
|
313 | df:function(o) {
|
314 | var x = o.x, n1 = x.length, y=o.y, n2=y.length;
|
315 | return n1+n2-2;
|
316 | }
|
317 | }
|
318 |
|
319 | var t2paired = {
|
320 | h:function(o) { return "H0:mu1"+o.op+"mu2" },
|
321 | sd:function(o) {
|
322 | var x = o.x, n = x.length, y=o.y;
|
323 | var S= R.sd(R.sub(x,y));
|
324 | return S;
|
325 | },
|
326 | o2q:function(o) {
|
327 | var x = o.x, n = x.length, y=o.y;
|
328 | var S = this.sd(o);
|
329 | var t = (R.mean(R.sub(x,y))-o.mu)/(S/Math.sqrt(n));
|
330 | return R.pt(t, n-1);
|
331 | },
|
332 |
|
333 | q2p:function(q, o) {
|
334 | var x = o.x, n = x.length, y=o.y;
|
335 | var S = this.sd(o);
|
336 | return R.mean(R.sub(x,y))+ R.qt(q, n-1)*S/Math.sqrt(n);
|
337 | },
|
338 | df:function(o) {
|
339 | return o.x.length-1;
|
340 | }
|
341 | }
|
342 |
|
343 | var t2varneq = {
|
344 |
|
345 | h:function(o) { return "H0:mu1"+o.op+"mu2" },
|
346 |
|
347 | o2q:function(o) {
|
348 | var x = o.x, n1 = x.length, y=o.y, n2=y.length;
|
349 | var S1 = R.sd(x), S2=R.sd(y);
|
350 | var t = (R.mean(x)-R.mean(y)-o.mu)/Math.sqrt(S1*S1/n1+S2*S2/n2);
|
351 | return R.pt(t, this.df(o));
|
352 | },
|
353 |
|
354 | q2p:function(q, o) {
|
355 | var x = o.x, n1 = x.length, y=o.y, n2=y.length;
|
356 | var S1 = R.sd(x), S2=R.sd(y);
|
357 | return R.mean(x)-R.mean(y)+ R.qt(q, this.df(o))*Math.sqrt(S1*S1/n1+S2*S2/n2);
|
358 | },
|
359 | df:function(o) {
|
360 | var x = o.x, n1 = x.length, y=o.y, n2=y.length;
|
361 | var S1 = R.sd(x), S2=R.sd(y);
|
362 | var Sn1 = S1*S1/n1, Sn2 = S2*S2/n2, Sn12 = Sn1+Sn2;
|
363 | var df = (Sn12*Sn12)/((Sn1*Sn1)/(n1-1)+(Sn2*Sn2)/(n2-1));
|
364 | return df;
|
365 | }
|
366 | }
|
367 |
|
368 | R.ttest = function(o) {
|
369 | var t;
|
370 | if (typeof o.y === "undefined") {
|
371 | o.name = "ttest(X)";
|
372 | o.D = t1;
|
373 | t = R.test(o);
|
374 | t.mean = R.mean(o.x);
|
375 | t.sd = R.sd(o.x);
|
376 | } else {
|
377 | var varequal = R.opt(o, "varequal", false);
|
378 | var paired = R.opt(o, "paired", false);
|
379 | if (varequal) {
|
380 | o.name = "ttest(X,Y,mu="+o.mu+",varequal=true) (pooled)";
|
381 | o.D = t2vareq;
|
382 | t = R.test(o);
|
383 | } else if (paired) {
|
384 | o.name = "ttest(x,y,mu="+o.mu+",paired=true)";
|
385 | o.D = t2paired;
|
386 | t = R.test(o);
|
387 | t.mean = "mean(x-y)="+R.str(R.mean(R.sub(o.x, o.y)));
|
388 | t.sd = "sd(x-y)="+R.str(R.sd(R.sub(o.x, o.y)));
|
389 | } else {
|
390 | o.name = "ttest(x,y,mu="+o.mu+",varequal=false), Welch t-test";
|
391 | o.D = t2varneq;
|
392 | t = R.test(o);
|
393 | }
|
394 | if (typeof t.mean === "undefined") {
|
395 | t.mean = "mean(x)="+R.str(R.mean(o.x))+" mean(y)="+R.str(R.mean(o.y));
|
396 | t.sd = "sd(x)="+R.str(R.sd(o.x))+" sd(y)="+R.str(R.sd(o.y));
|
397 | }
|
398 | }
|
399 | return t;
|
400 | }
|
401 |
|
402 | var f2 = {
|
403 | h:function(o) { return "H0:σ1/σ2"+o.op+"1"; },
|
404 |
|
405 | o2q:function(o) {
|
406 | var x = o.x, n1 = x.length, y=o.y, n2=y.length;
|
407 | var S1 = R.sd(x), S2=R.sd(y);
|
408 | var f = (S1*S1)/(S2*S2);
|
409 | var pf = R.pf(f, n1-1, n2-1);
|
410 | return pf;
|
411 | },
|
412 |
|
413 |
|
414 | q2p:function(q, o) {
|
415 | var x = o.x, n1 = x.length, y=o.y, n2=y.length;
|
416 | var S1 = R.sd(x), S2=R.sd(y);
|
417 | var qf = R.qf(1-q, n1-1, n2-1);
|
418 | return (S1*S1)/(S2*S2*qf);
|
419 | },
|
420 | df:function(o) {
|
421 | var x = o.x, n1 = x.length, y=o.y, n2=y.length;
|
422 | return [n1-1, n2-1];
|
423 | }
|
424 | }
|
425 |
|
426 | R.ftest = function(o) {
|
427 | o.name = "ftest(X, Y)";
|
428 | o.D = f2;
|
429 | var t = R.test(o);
|
430 | var rsd = R.sd(o.x)/R.sd(o.y);
|
431 | t.ratio = (rsd*rsd);
|
432 | return t;
|
433 | }
|
434 |
|
435 |
|
436 | var chisq1 = {
|
437 | h:function(o) { return "H0:σ1"+o.op+"σ"; },
|
438 |
|
439 | o2q:function(o) {
|
440 | var x = o.x, n = x.length, S=R.sd(x);
|
441 | var v = (n-1)*S*S/(o.sd*o.sd);
|
442 | return R.pchisq(v, n-1);
|
443 | },
|
444 |
|
445 | q2p:function(q, o) {
|
446 | var x = o.x, n = x.length, S=R.sd(x);
|
447 | return (n-1)*S*S/R.qchisq(1-q, n-1);
|
448 | },
|
449 | df:function(o) {
|
450 | var x = o.x, n = x.length;
|
451 | return n-1;
|
452 | }
|
453 | }
|
454 |
|
455 | R.chisqtest = function(o) {
|
456 | o.name = "chisqtest(X)";
|
457 | o.D = chisq1;
|
458 | return R.test(o);
|
459 | }
|
460 |
|
461 | R.vartest = function(o) {
|
462 | if (typeof o.y === "undefined")
|
463 | return R.chisqtest(o);
|
464 | else
|
465 | return R.ftest(o);
|
466 | }
|
467 |
|
468 | var z1 = {
|
469 | h:function(o) { return "H0:mu"+o.op+o.mu+" when sd="+o.sd; },
|
470 | o2q:function(o) {
|
471 | var x = o.x, n = x.length;
|
472 | var z = (R.mean(x)-o.mu)/(o.sd/Math.sqrt(n));
|
473 | return R.pnorm(z, 0, 1);
|
474 | },
|
475 | q2p:function(q, o) {
|
476 | var x = o.x, n = x.length;
|
477 | return R.mean(x) + R.qnorm(q, 0, 1) * R.sd(x) / Math.sqrt(n);
|
478 | },
|
479 | df:function(o) { return o.x.length; }
|
480 | }
|
481 |
|
482 | R.ztest = function(o) {
|
483 | o.name = "ztest(X)";
|
484 | o.D = z1;
|
485 | return R.test(o);
|
486 | }
|
487 |
|
488 | var zprop1 = {
|
489 | h:function(o) { return "H0:p"+o.op+o.p; },
|
490 |
|
491 | o2q:function(o) {
|
492 | var x=o.x, n=o.n, p1=x/n, p=R.def(o.p, p1);
|
493 | var z = (p1-p)/Math.sqrt(p*(1-p)/n);
|
494 | return R.pnorm(z, 0, 1);
|
495 | },
|
496 |
|
497 | q2p:function(q, o) {
|
498 | var x=o.x, n=o.n, p1=x/n, p=p1;
|
499 | var z = R.qnorm(q, 0, 1);
|
500 | var z22n = z*z/(2*n);
|
501 | return (p1+z22n+z*Math.sqrt( p*(1-p)/n + z22n/(2*n) ))/(1+2*z22n);
|
502 |
|
503 | },
|
504 | df:function(o) { return 1; }
|
505 | }
|
506 |
|
507 | var zprop2 = {
|
508 | h:function(o) { return "H0:p1-p2"+o.op+o.p; },
|
509 |
|
510 | o2q:function(o) {
|
511 | var x=o.x, y=o.y, n1=o.n1, n2=o.n2, p1=x/n1, p2=y/n2, p=(n1*p1+n2*p2)/(n1+n2);
|
512 | var z = (p1-p2)/Math.sqrt(p*(1-p)*(1/n1+1/n2));
|
513 | return R.pnorm(z, 0, 1);
|
514 | },
|
515 |
|
516 | q2p:function(q, o) {
|
517 | var x=o.x, y=o.y, n1=o.n1, n2=o.n2, p1=x/n1, p2=y/n2, p=(n1*p1+n2*p2)/(n1+n2);
|
518 | var z = R.qnorm(q, 0, 1);
|
519 | return p1-p2+z*Math.sqrt(p*(1-p)*(1/n1+1/n2));
|
520 | },
|
521 | df:function(o) { return 1; }
|
522 | }
|
523 |
|
524 |
|
525 |
|
526 |
|
527 |
|
528 |
|
529 |
|
530 |
|
531 |
|
532 |
|
533 |
|
534 |
|
535 |
|
536 | R.proptest = function(o) {
|
537 | o.p = R.opt(o, "p", 0.5);
|
538 | o.name = "proptest("+R.str(o)+")";
|
539 | o.correct = R.opt(o.correct, false);
|
540 | if (o.correct) {
|
541 | o.name += ", binomtest";
|
542 | o.D += binom1;
|
543 | } else {
|
544 | if (typeof o.y === "undefined") {
|
545 | o.name += ", zprop1";
|
546 | o.D = zprop1;
|
547 | } else {
|
548 | o.p = 0;
|
549 | o.name += ", zprop2";
|
550 | o.D = zprop2;
|
551 | }
|
552 | }
|
553 | var t=R.test(o);
|
554 | if (typeof o.y === "undefined")
|
555 | t.p = o.x/o.n;
|
556 | else
|
557 | t.p = [o.x/o.n1, o.y/o.n2];
|
558 | return t;
|
559 | }
|
560 |
|
561 |
|
562 | var binom1 = {
|
563 | h:function(o) { return "H0:p"+o.op+o.p; },
|
564 |
|
565 |
|
566 | o2q:function(o) {
|
567 | var x=o.x, n=o.n, p = o.p, q;
|
568 | var dx = R.dbinom(x, n, p);
|
569 | if (o.op === "=") {
|
570 | var q = 0;
|
571 | for (var i=0; i<=n; i++) {
|
572 | var di = R.dbinom(i, n, p);
|
573 | if (di > dx+1e-5) q += di;
|
574 | }
|
575 | q=1-((1-q)/2);
|
576 | } else {
|
577 | if (Math.abs(x - n*p)<1e-5)
|
578 | q = 1;
|
579 | else {
|
580 | if (o.op === ">")
|
581 | q = R.pbinom(x, n, p);
|
582 | else
|
583 | q = R.pbinom(x-1, n, p);
|
584 | }
|
585 | }
|
586 | return q;
|
587 | },
|
588 |
|
589 |
|
590 |
|
591 |
|
592 |
|
593 |
|
594 |
|
595 |
|
596 |
|
597 |
|
598 |
|
599 |
|
600 |
|
601 |
|
602 |
|
603 | q2p:function(q, o, side) {
|
604 | var x=o.x, n=o.n, p=o.p, op=o.op;
|
605 | if (side === "L")
|
606 | return qbeta(q, x, n - x + 1);
|
607 | else
|
608 | return qbeta(q, x + 1, n - x);
|
609 | },
|
610 | df:function(o) { return 1; }
|
611 | }
|
612 |
|
613 | R.binomtest = function(o) {
|
614 | o.p = R.opt(o, "p", 0.5);
|
615 | o.name = "binomtest("+R.str(o)+")";
|
616 | o.D = binom1;
|
617 | var t=R.test(o);
|
618 | t.p = o.x/o.n;
|
619 | t.ci[0]=(o.op === ">")?0:t.ci[0];
|
620 | t.ci[1]=(o.op === "<")?1:t.ci[1];
|
621 | return t;
|
622 | }
|
623 |
|
624 | R.report = function(o) {
|
625 | console.log("=========== report ==========");
|
626 | for (var k in o) {
|
627 | if (typeof o[k] !== "function")
|
628 | console.log(k+"\t: "+str(o[k]));
|
629 | }
|
630 | }
|
631 |
|
632 |
|
633 | R.anovaftest = function() {
|
634 | return {
|
635 | h0 : "σ1=σ2=...=σ"+arguments.length,
|
636 | pvalue: J.anovaftest(),
|
637 | score: J.anovafscore(),
|
638 | };
|
639 | }
|
640 |
|
641 | module.exports = R;
|