UNPKG

@stdlib/esm

Version:

ES module distribution for Stdlib, a standard library for JavaScript and Node.js.

3 lines 6.24 kB
// This file is a part of stdlib. License is Apache-2.0: http://www.apache.org/licenses/LICENSE-2.0 import e from"./../assert/is-number-array.js";import t from"./../assert/is-typed-array-like.js";import i from"./../utils/define-read-only-property.js";import r from"./../assert/is-plain-object.js";import o from"./ranks.js";import a from"./base/dists/normal/cdf.js";import n from"./base/dists/signrank/cdf.js";import s from"./../utils/tabulate.js";import l from"./../math/base/special/signum.js";import p from"./../math/base/special/sqrt.js";import m from"./../math/base/special/abs.js";import u from"./../array/float64.js";import f from"./../assert/contains.js";import h from"./../assert/is-boolean.js";import c from"./../assert/is-number.js";import d from"./../assert/is-string.js";import v from"./../assert/is-nan.js";import g from"./../assert/has-own-property.js";import w from"./../assert/is-positive-integer.js";import b from"./../math/base/special/roundn.js";var j=f,y=h.isPrimitive,E=c.isPrimitive,x=r,T=d.isPrimitive,O=v,z=g,M=["two-sided","less","greater"],V=["pratt","wilcox","zsplit"];function k(e,t){if(!x(t))return new TypeError("invalid argument. Options argument must be an object. Value: `"+t+"`.");if(z(t,"alpha")){if(e.alpha=t.alpha,!E(e.alpha)||O(e.alpha))return new TypeError("invalid option. `alpha` option must be a number primitive. Option: `"+e.alpha+"`.");if(e.alpha<0||e.alpha>1)return new RangeError("invalid argument. Option `alpha` must be a number in the range 0 to 1. Value: `"+e.alpha+"`.")}if(z(t,"alternative")){if(e.alternative=t.alternative,!T(e.alternative))return new TypeError("invalid option. `alternative` option must be a string primitive. Option: `"+e.alternative+"`.");if(!j(M,e.alternative))return new Error("invalid option. `alternative` option must be one of "+M.join(", ")+". Option: `"+e.alternative+"`.")}if(z(t,"correction")&&(e.correction=t.correction,!y(e.correction)||O(e.correction)))return new TypeError("invalid option. `correction` option must be a boolean primitive. Option: `"+e.alpha+"`.");if(z(t,"exact")&&(e.exact=t.exact,!y(e.exact)||O(e.exact)))return new TypeError("invalid option. `exact` option must be a boolean primitive. Option: `"+e.alpha+"`.");if(z(t,"mu")&&(e.mu=t.mu,!E(e.mu)||O(e.mu)))return new TypeError("invalid option. `mu` option must be a number primitive. Option: `"+e.mu+"`.");if(z(t,"zeroMethod")){if(e.zeroMethod=t.zeroMethod,!T(e.zeroMethod))return new TypeError("invalid option. `zeroMethod` option must be a string primitive. Option: `"+e.alternative+"`.");if(!j(V,e.zeroMethod))return new Error("invalid option. `zeroMethod` option must be one of "+V.join(", ")+". Option: `"+e.zeroMethod+"`.")}return null}function P(e,t){return e-t}function F(e){var t,i,r,o;for((e=e.slice()).sort(P),t=e.length,r=1,o=0;r<t;r++)i=e[r],e[o]!==i&&(e[o+=1]=i);return e.length=o+1,e}var q=w,R=r,W=h.isPrimitive,A=g,D=b;function S(e){var t,i,r;if(i=4,t=!0,arguments.length>0){if(!R(e))throw new TypeError("invalid argument. First argument must be an options object. Value: `"+e+"`.");if(A(e,"digits")){if(!q(e.digits))throw new TypeError("invalid option. `digits` option must be a positive integer. Option: `"+e.digits+"`.");i=e.digits}if(A(e,"decision")){if(!W(e.decision))throw new TypeError("invalid option. `decision` option must be a boolean primitive. Option: `"+e.decision+"`.");t=e.decision}}switch(r="",r+=this.method,r+="\n\n",r+="Alternative hypothesis: ","Paired Wilcoxon signed rank test"===this.method?r+="Median of the difference `x - y` is ":r+="Median of `x` is ",this.alternative){case"two-sided":default:r+="not equal to ";break;case"less":r+="less than ";break;case"greater":r+="greater than "}return r+=this.nullValue,r+="\n\n",r+=" pValue: "+D(this.pValue,-i)+"\n",r+=" statistic: "+D(this.statistic,-i)+"\n",r+="\n",t&&(r+="Test Decision: ",this.rejected?r+="Reject null in favor of alternative at "+100*this.alpha+"% significance level":r+="Fail to reject null in favor of alternative at "+100*this.alpha+"% significance level",r+="\n"),r}var B=e.primitives,C=t,G=i,H=r,I=o,J=a.factory,K=n,L=s,N=l,Q=p,U=m,X=u,Y=k,Z=F,$=S,_=J(0,1);function ee(){var e,t,i,r,o,a,n,s,l,p,m,u,f,h,c,d,v,g,w,b,j,y,E,x,T,O,z,M,V;if(!C(M=arguments[0])&&!B(M))throw new TypeError("invalid argument. First argument must be a numeric array. Value: `"+M+"`.");if(d=M.length,arguments.length>1)if(H(arguments[1]))i=arguments[1];else{if(!C(V=arguments[1])&&!B(V))throw new TypeError("invalid argument. `y` argument must be a numeric array. Value: `"+V+"`.");if(d!==V.length)throw new Error("invalid arguments. The first and second arguments must have the same length.");arguments.length>2&&(i=arguments[2])}if(u={},i&&(c=Y(u,i)))throw c;if(b=u.mu||0,e=void 0===u.correction||u.correction,p=void 0===u.alpha?.05:u.alpha,d<2)throw new Error("invalid argument. First argument must contain at least two elements. Value: `"+M+"`");if(h=u.alternative||"two-sided","wilcox"===(t=u.zeroMethod||"wilcox")){if(E=[],V)for(x=0;x<d;x++)0!==(z=M[x]-V[x]-b)&&E.push(z);else for(x=0;x<d;x++)0!==M[x]&&E.push(M[x]-b);s=M.length-E.length}else if(E=new X(d),s=0,V)for(x=0;x<d;x++)E[x]=M[x]-V[x]-b,0===E[x]&&(s+=1);else for(x=0;x<d;x++)E[x]=M[x]-b,0===E[x]&&(s+=1);if(s===d)throw new Error("`x` or `x - y` cannot be zero for all elements.");for(d=E.length,w=new X(d),x=0;x<d;x++)w[x]=U(E[x]);for(T=I(w),n=0,l=0,x=0;x<d;x++)E[x]>0?n+=T[x]:0===E[x]&&(l+=T[x]);if(r=Z(T).length!==T.length,"zsplit"===t&&(n+=l/2),O=n,j=d*(d+1)*.25,y=d*(d+1)*(2*d+1),"pratt"===t){for(v=[],x=0;x<d;x++)0!==E[x]&&v.push(T[x]);T=v,j-=s*(s+1)*.25,y-=s*(s+1)*(2*s+1)}for(o=L(T),a=0,x=0;x<o.length;x++)o[x][1]>1&&(a+=(z=o[x][1])*(z*z-1));if(a>0&&(y-=.5*a),y=Q(y/24),d>50&&!u.exact||s>0||r){if(E=0,e)switch(h){case"two-sided":E=.5*N(O-j);break;case"less":E=-.5;break;default:E=.5}f=(O-j-E)/y,m="two-sided"===h?2*(1-_(U(f))):"greater"===h?1-_(f):_(f)}else f=O,m="two-sided"===h?f>d*(d+1)/4?2*(1-K(f-1,d)):2*K(f,d):"greater"===h?1-K(f-1,d):K(f,d);return G(g={},"rejected",m<=p),G(g,"alpha",p),G(g,"pValue",m),G(g,"statistic",O),G(g,"nullValue",b),G(g,"alternative",h),G(g,"method",(V?"Paired":"One-Sample")+" Wilcoxon signed rank test"),G(g,"print",$),g}var te=ee;export default te; //# sourceMappingURL=wilcoxon.js.map