@stdlib/esm
Version:
ES module distribution for Stdlib, a standard library for JavaScript and Node.js.
3 lines • 4.87 kB
JavaScript
// This file is a part of stdlib. License is Apache-2.0: http://www.apache.org/licenses/LICENSE-2.0
import t from"./../assert/is-number-array.js";import e from"./../assert/is-typed-array-like.js";import i from"./../utils/define-read-only-property.js";import a from"./../assert/is-plain-object.js";import r from"./base/dists/t/cdf.js";import n from"./base/dists/t/quantile.js";import s from"./../math/base/special/sqrt.js";import o from"./../math/base/special/abs.js";import m from"./base/mean.js";import l from"./base/variance.js";import p from"./../blas/base/gcopy.js";import u from"./../constants/math/float64-ninf.js";import f from"./../constants/math/float64-pinf.js";import h from"./../array/float64.js";import d from"./../assert/is-number.js";import v from"./../assert/is-string.js";import c from"./../math/base/assert/is-nan.js";import g from"./../assert/has-own-property.js";import b from"./../assert/contains.js";import w from"./../assert/is-positive-integer.js";import j from"./../assert/is-boolean.js";import y from"./../math/base/special/roundn.js";var E=d.isPrimitive,T=a,V=v.isPrimitive,O=c,P=g,F=b,k=["two-sided","less","greater"];function q(t,e){if(!T(e))return new TypeError("invalid argument. Options argument must be an object. Value: `"+e+"`.");if(P(e,"alpha")){if(t.alpha=e.alpha,!E(t.alpha)||O(t.alpha))return new TypeError("invalid option. `alpha` option must be a number. Option: `"+t.alpha+"`.");if(t.alpha<0||t.alpha>1)return new RangeError("invalid option. `alpha` option must be between `0` and `1` (inclusive). Option: `"+t.alpha+"`.")}if(P(e,"alternative")){if(t.alternative=e.alternative,!V(t.alternative))return new TypeError("invalid option. `alternative` option must be a string. Option: `"+t.alternative+"`.");if(!F(k,t.alternative))return new Error("invalid option. `alternative` option must be one of the following: `"+k.join(", ")+"`.")}return P(e,"mu")&&(t.mu=e.mu,!E(t.mu)||O(t.mu))?new TypeError("invalid option. `mu` option must be a number. Option: `"+t.mu+"`."):null}var R=w,S=a,x=j.isPrimitive,A=g,D=y;function z(t){var e,i,a;if(i=4,e=!0,arguments.length>0){if(!S(t))throw new TypeError("invalid argument. First argument must be an options object. Value: `"+t+"`.");if(A(t,"digits")){if(!R(t.digits))throw new TypeError("invalid option. `digits` option must be a positive integer. Option: `"+t.digits+"`.");i=t.digits}if(A(t,"decision")){if(!x(t.decision))throw new TypeError("invalid option. `decision` option must be a boolean primitive. Option: `"+t.decision+"`.");e=t.decision}}switch(a="",a+=this.method,a+="\n\n",a+="Alternative hypothesis: ","Paired t-test"===this.method?a+="True difference in means is ":a+="True mean is ",this.alternative){case"two-sided":default:a+="not equal to ";break;case"less":a+="less than ";break;case"greater":a+="greater than "}return a+=this.nullValue,a+="\n\n",a+=" pValue: "+D(this.pValue,-i)+"\n",a+=" statistic: "+D(this.statistic,-i)+"\n",a+=" df: "+this.df+"\n",a+=" "+100*(1-this.alpha)+"% confidence interval: ["+D(this.ci[0],-i)+","+D(this.ci[1],-i)+"]",a+="\n\n",e&&(a+="Test Decision: ",this.rejected?a+="Reject null in favor of alternative at "+100*this.alpha+"% significance level":a+="Fail to reject null in favor of alternative at "+100*this.alpha+"% significance level",a+="\n"),a}var B=t.primitives,C=e,G=i,H=a,I=r,J=n,K=s,L=o,M=m,N=l,Q=p,U=u,W=f,X=h,Y=q,Z=z;function $(t){var e,i,a,r,n,s,o,m,l,p,u,f,h;if(!C(t)&&!B(t))throw new TypeError("invalid argument. First argument must be a numeric array. Value: `"+t+"`.");if((m=t.length)<2)throw new Error("invalid argument. First argument must have at least two elements. Value: `"+t+"`.");if(n={mu:0,alpha:.05,alternative:"two-sided"},2===arguments.length){if(H(arguments[1])){if(o=Y(n,arguments[1]))throw o}else if(!C(f=arguments[1])&&!B(f))throw new TypeError("invalid argument. Second argument must be either a numeric array or an options object. Value: `"+f+"`.")}else if(arguments.length>2){if(!C(f=arguments[1])&&!B(f))throw new TypeError("invalid argument. Second argument must be a numeric array. Value: `"+f+"`.");if(o=Y(n,arguments[2]))throw o}if(f){if(f.length!==m)throw new Error("invalid arguments. The first and second arguments must have the same length.");for(t=Q(m,t,1,new X(m),1),h=0;h<m;h++)t[h]-=f[h]}return e=K(N(m,1,t,1)/m),s=((i=M(m,t,1))-n.mu)/e,p=m-1,"two-sided"===n.alternative?(r=2*I(-L(s),p),u=J(1-n.alpha/2,p),a=[n.mu+(s-u)*e,n.mu+(s+u)*e]):"greater"===n.alternative?(r=1-I(s,p),u=J(1-n.alpha,p),a=[n.mu+(s-u)*e,W]):(r=I(s,p),u=J(1-n.alpha,p),a=[U,n.mu+(s+u)*e]),G(l={},"rejected",r<=n.alpha),G(l,"alpha",n.alpha),G(l,"pValue",r),G(l,"statistic",s),G(l,"ci",a),G(l,"df",p),G(l,"nullValue",n.mu),G(l,"mean",i),G(l,"sd",e),G(l,"alternative",n.alternative),G(l,"method",f?"Paired t-test":"One-sample t-test"),G(l,"print",Z),l}var _=$;export default _;
//# sourceMappingURL=ttest.js.map