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