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