@numero-uno-jpn/rstats 1.9.0 → 1.11.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +17 -0
- package/README.ja.md +6 -5
- package/README.md +6 -5
- package/dist/chunks/chunk-3K5KP3M2.js +2 -0
- package/dist/chunks/chunk-46DELNLN.js +2 -0
- package/dist/chunks/chunk-6VIOWV53.js +2 -0
- package/dist/chunks/{chunk-MT7XELUM.js → chunk-BA32XHDA.js} +2 -2
- package/dist/chunks/{chunk-DEAU6HWA.js → chunk-BGY7Y3P4.js} +1 -1
- package/dist/chunks/chunk-BKH5GQNA.js +2 -0
- package/dist/chunks/{chunk-BX47HGID.js → chunk-BNHMQU7V.js} +2 -2
- package/dist/chunks/chunk-BZBPPRSH.js +2 -0
- package/dist/chunks/{chunk-G3XMR7PA.js → chunk-DMT423BG.js} +2 -2
- package/dist/chunks/chunk-EV7ZQC6D.js +2 -0
- package/dist/chunks/chunk-HCHZ6M34.js +2 -0
- package/dist/chunks/chunk-HIPKYBZ3.js +2 -0
- package/dist/chunks/chunk-HPWRADJM.js +2 -0
- package/dist/chunks/chunk-IRO6MXW2.js +2 -0
- package/dist/chunks/{chunk-5C7XHQKP.js → chunk-JD6JHIVV.js} +1 -1
- package/dist/chunks/{chunk-UUZVQS4O.js → chunk-JFZOKMZA.js} +2 -2
- package/dist/chunks/chunk-JWMVP7XO.js +2 -0
- package/dist/chunks/chunk-KICTA3JY.js +3 -0
- package/dist/chunks/{chunk-2SO7Z3NV.js → chunk-M4SLTLOX.js} +2 -2
- package/dist/chunks/chunk-MJLK2RIQ.js +2 -0
- package/dist/chunks/{chunk-XCKQQVN7.js → chunk-NO675KX4.js} +2 -2
- package/dist/chunks/chunk-OG5FTIWF.js +2 -0
- package/dist/chunks/chunk-OWOBNS75.js +2 -0
- package/dist/chunks/chunk-P23KCD67.js +2 -0
- package/dist/chunks/chunk-PF6LEBKO.js +2 -0
- package/dist/chunks/chunk-QRUQEJJ5.js +2 -0
- package/dist/chunks/chunk-QY4OBLXK.js +2 -0
- package/dist/chunks/{chunk-RNK6EHEK.js → chunk-S5JZMHAW.js} +2 -2
- package/dist/chunks/chunk-SV6SLUVB.js +2 -0
- package/dist/chunks/{chunk-2COJEI44.js → chunk-TNU4SMV3.js} +2 -2
- package/dist/chunks/chunk-W27XXELU.js +2 -0
- package/dist/chunks/chunk-W2D6IDBY.js +2 -0
- package/dist/chunks/{chunk-MNIICUNF.js → chunk-WHKUEZQI.js} +2 -2
- package/dist/chunks/{chunk-ANGKH5D4.js → chunk-XKQSKVCL.js} +2 -2
- package/dist/chunks/chunk-XLBSUUWY.js +2 -0
- package/dist/chunks/{chunk-7F7PY35D.js → chunk-Y2SS3GX2.js} +2 -2
- package/dist/chunks/chunk-YPDFCKL4.js +2 -0
- package/dist/chunks/{chunk-I7E5J5PG.js → chunk-Z6UI6ON2.js} +2 -2
- package/dist/chunks/{chunk-4IHHZ3F4.js → chunk-ZW5OCUIY.js} +1 -1
- package/dist/descriptive.js +2 -2
- package/dist/distributions.js +2 -2
- package/dist/entries/multivariate.d.ts +8 -0
- package/dist/entries/multivariate.js +8 -0
- package/dist/entries/timeseries.d.ts +1 -0
- package/dist/entries/timeseries.js +2 -0
- package/dist/linalg/symmetricLeading.d.ts +8 -0
- package/dist/linalg/symmetricLeading.js +231 -0
- package/dist/linalg.js +2 -2
- package/dist/mixed.js +2 -2
- package/dist/multivariate/ca.d.ts +120 -0
- package/dist/multivariate/ca.js +230 -0
- package/dist/multivariate/cmdscale.d.ts +40 -0
- package/dist/multivariate/cmdscale.js +171 -0
- package/dist/multivariate/dist.d.ts +63 -0
- package/dist/multivariate/dist.js +218 -0
- package/dist/multivariate/hclust.d.ts +58 -0
- package/dist/multivariate/hclust.js +264 -0
- package/dist/multivariate/isoMDS.d.ts +55 -0
- package/dist/multivariate/isoMDS.js +409 -0
- package/dist/multivariate/kmeans.d.ts +58 -0
- package/dist/multivariate/kmeans.js +392 -0
- package/dist/multivariate/mjca.d.ts +125 -0
- package/dist/multivariate/mjca.js +243 -0
- package/dist/multivariate/sammon.d.ts +66 -0
- package/dist/multivariate/sammon.js +231 -0
- package/dist/multivariate.js +2 -2
- package/dist/numerics.js +2 -2
- package/dist/regression.js +2 -2
- package/dist/rstats.js +1 -1
- package/dist/special.js +2 -2
- package/dist/tests.js +2 -2
- package/dist/timeseries/acf.d.ts +80 -0
- package/dist/timeseries/acf.js +314 -0
- package/dist/timeseries.js +2 -0
- package/llms.txt +53 -2
- package/package.json +6 -1
- package/dist/chunks/chunk-3J2HFC4A.js +0 -2
- package/dist/chunks/chunk-3LDUKHJZ.js +0 -3
- package/dist/chunks/chunk-4QWW62QN.js +0 -2
- package/dist/chunks/chunk-575F6RXJ.js +0 -2
- package/dist/chunks/chunk-7VPO2LTR.js +0 -2
- package/dist/chunks/chunk-CWS62KPG.js +0 -2
- package/dist/chunks/chunk-D3CWLSLS.js +0 -2
- package/dist/chunks/chunk-DRSA7ITF.js +0 -2
- package/dist/chunks/chunk-IEATFACS.js +0 -2
- package/dist/chunks/chunk-IXK3JA2R.js +0 -2
- package/dist/chunks/chunk-J3TS4PGN.js +0 -2
- package/dist/chunks/chunk-OGHJJKUR.js +0 -2
- package/dist/chunks/chunk-T3NUXZRL.js +0 -2
- package/dist/chunks/chunk-TKMNASCD.js +0 -2
- package/dist/chunks/chunk-UA66IB37.js +0 -2
- package/dist/chunks/chunk-VO2NIKVB.js +0 -2
- package/dist/chunks/chunk-WFE52E6F.js +0 -2
- package/dist/chunks/chunk-WOZEQS2I.js +0 -2
- package/dist/chunks/chunk-XNLZFKL5.js +0 -2
- package/dist/chunks/chunk-YFZBGUIT.js +0 -2
- package/dist/chunks/chunk-YK7Q6CNX.js +0 -2
- package/dist/chunks/chunk-Z47NJMYU.js +0 -2
package/dist/tests.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
/*! rStats.js 1.
|
|
2
|
-
import{a as tn,b as zn,c as nr,d as vr,e as xs,f as Fa}from"./chunks/chunk-UUZVQS4O.js";import{A as gs,a as bn,c as sn,h as $r,i as dl,m as gl,n as zr,o as wl,s as hs,t as bl,u as bo,w as xl,y as ps,z as ds}from"./chunks/chunk-YK7Q6CNX.js";import{a as Wa,e as Ba}from"./chunks/chunk-WOZEQS2I.js";import{a as bs,b as tr,c as yo,d as Al,e as ql,f as vs,g as Ze,h as cn,i as vn,j as _t,k as Ll,l as sp,n as Ts,o as Ro,p as Ss}from"./chunks/chunk-7VPO2LTR.js";import"./chunks/chunk-OGHJJKUR.js";import{a as hl,d as us}from"./chunks/chunk-J3TS4PGN.js";import{d as nt}from"./chunks/chunk-WFE52E6F.js";import{a as Rs,e as Bt,f as Wl}from"./chunks/chunk-XNLZFKL5.js";import"./chunks/chunk-7F7PY35D.js";import{a as Mo,b as Oa,c as Da,d as $l,e as Eo,f as Va,g as Wn,h as $a,i as xo,j as zl,k as za,l as ap,m as ja,n as jl,o as ip,p as cp,q as lp}from"./chunks/chunk-4QWW62QN.js";import{b as It,c as On}from"./chunks/chunk-MNIICUNF.js";import{a as kl,c as Il,d as _l,e as br,g as To}from"./chunks/chunk-I7E5J5PG.js";import{b as La}from"./chunks/chunk-DEAU6HWA.js";import{a as ct,b as Ns,c as ys}from"./chunks/chunk-ANGKH5D4.js";import{a as Dl,b as Pl,c as Es,d as Pa}from"./chunks/chunk-3LDUKHJZ.js";import{a as jn,b as Wr}from"./chunks/chunk-G3XMR7PA.js";import{d as ss,e as as,g as is,h as Vr,m as hn,n as Dn}from"./chunks/chunk-CWS62KPG.js";import{d as ht,e as un,g as Rn,h as Gn,j as Xn}from"./chunks/chunk-D3CWLSLS.js";import{a as Vl}from"./chunks/chunk-MT7XELUM.js";import{c as Cl,h as Fl,j as Lt,m as Ca,n as Ol,o as Ms,p as jr}from"./chunks/chunk-TKMNASCD.js";import{a as er,b as Tl,g as ws}from"./chunks/chunk-2SO7Z3NV.js";import{a as _a,b as dt,c as gt,d as pl,e as Qt,f as Wt}from"./chunks/chunk-VO2NIKVB.js";import{i as wt,l as os,t as fl}from"./chunks/chunk-UA66IB37.js";import{a as Tn,c as Sl}from"./chunks/chunk-BX47HGID.js";import{b as vl,c as Nl,d as yl,e as Ml,f as El,i as vo}from"./chunks/chunk-DRSA7ITF.js";import{a as op,c as Q,d as ot,e as Rt}from"./chunks/chunk-575F6RXJ.js";import{a as No,b as $n,c as Rl,d as Pt,e as vt}from"./chunks/chunk-5C7XHQKP.js";import{a as Nt,b as dn,c as an}from"./chunks/chunk-4IHHZ3F4.js";import"./chunks/chunk-Z47NJMYU.js";import{b as il,c as cl}from"./chunks/chunk-T3NUXZRL.js";import{A as ol,B as sl,D as al,M as ll,e as Aa,q as Xt,r as se,s as le}from"./chunks/chunk-3J2HFC4A.js";import{A as ml,C as cs,D as xt,E as ls,F as ul,G as fs,H as ka,I as De,J as bt,K as wn,L as Dt,M as Jn,N as Vt,O as ms,P as qe,Q as Ia,b as Ot,c as jt,d as he,e as nl,f as Le,g as _e,h as Et,i as rt,j as ft,k as rl,l as B,m as Fn,p as qa,q as wo,r as Pn,s as pn,t as Vn,u as V,v as Ve,w as Qn,x as it,y as at,z as Zn}from"./chunks/chunk-IEATFACS.js";var Bl=["alternative","mu","paired","varEqual","confLevel"];function Nn(e,t,n={}){if(typeof e!="string"&&ct(t)&&B(t,Bl,"tTest"),B(n,Bl,"tTest"),typeof e=="string")return fp(e,t,n);let r;t!==void 0&&!ArrayBuffer.isView(t)&&!Array.isArray(t)?n=t:r=t;let o=Ze(n.alternative),s=n.mu??0,i=n.paired??!1,c=n.varEqual??!1,a=n.confLevel??.95;if(Number.isNaN(s))throw new RangeError("mu must be a number");if(cn(a),i&&r===void 0)throw new TypeError("y is required when paired is true");let l,f,m;if(i){let[A,C]=Jn(Array.from(e),Array.from(r));({d:l,k:m}=ls(A,C))}else{let A=qe(r===void 0?[Dt(Array.from(e))]:[Dt(Array.from(e)),Dt(Array.from(r))]);m=A.k,l=A.groups[0],f=A.groups[1]}let u=V(s,-m),h,p,w,d,b,N,v,E=l.length,g=f!==void 0&&c;if(E<1||!g&&E<2)throw new Error("not enough 'x' observations");let y=it(l),x=E>1?at(l):NaN;if(f===void 0){if(p=E-1,w=Math.sqrt(x/E),w===0||w<10*Number.EPSILON*Math.abs(y))throw new Error("data are essentially constant");h=(y-u)/w,N=i?"Paired t-test":"One Sample t-test",d=y,b=i?"mean difference":"mean of x",v=i?"mean difference":"mean"}else{let A=f.length;if(A<1||!c&&A<2)throw new Error("not enough 'y' observations");if(c&&E+A<3)throw new Error("not enough observations");let C=it(f),L=A>1?at(f):NaN;if(N=`${c?"":"Welch"} Two Sample t-test`,d=[y,C],b=["mean of x","mean of y"],v="difference in means",c){p=E+A-2;let k=0;E>1&&(k+=(E-1)*x),A>1&&(k+=(A-1)*L),k/=p,w=Math.sqrt(k*(1/E+1/A))}else{let k=x/E,F=L/A;w=Math.sqrt(Math.sqrt(k)**2+Math.sqrt(F)**2),p=fs([k,F],[E-1,A-1])}if(w===0||w<10*Number.EPSILON*Math.max(Math.abs(y),Math.abs(C)))throw new Error("data are essentially constant");h=(y-C-u)/w}let M=typeof d=="number"?d:d[0]-d[1],R=(A,C)=>{let L=u+(h+C*A)*w;return Number.isFinite(L)&&Math.max(Math.abs(u),Math.abs(h*w))<=16*(Math.abs(M)+A*w)?L:M+C*A*w},T,S;if(o==="less")T=dt(h,p,!0,!1),S=[-1/0,R(gt(a,p,!0,!1),1)];else if(o==="greater")T=dt(h,p,!1,!1),S=[R(gt(a,p,!0,!1),-1),1/0];else{T=2*dt(-Math.abs(h),p,!0,!1);let A=1-a,C=A/2<1e-8?gt(A/2,p,!1,!1):gt(1-A/2,p,!0,!1);S=[R(C,-1),R(C,1)]}return S=[V(S[0],m),V(S[1],m)],d=typeof d=="number"?V(d,m):d.map(A=>V(A,m)),w=V(w,m),{method:N,alternative:o,statistic:h,statisticName:"t",parameter:p,parameterName:"df",pValue:T,confInt:S,confLevel:a,estimate:d,estimateNames:b,nullValue:s,nullValueName:v,stdErr:w,stdError:w,warnings:[]}}function fp(e,t,n){if(n.paired!==void 0)throw new Error("cannot use 'paired' in formula method");let r=bs(e,t,!0);if(r.kind==="one")return Nn(r.x,n);if(r.kind==="paired")return Nn(r.x,r.y,{...n,paired:!0});let[o,s]=r.levels;return{...Nn(r.x,r.y,n),estimateNames:[`mean in group ${o}`,`mean in group ${s}`],nullValueName:`difference in means between group ${o} and group ${s}`}}var mp=new Set(["n","delta","sd","sigLevel","power","type","alternative","strict","tol"]),up=["n","p1","p2","sigLevel","power","alternative","strict","tol"],hp=["groups","n","betweenVar","withinVar","sigLevel","power"],Ul=Number.EPSILON**.25;function Se(e,t){if(e===void 0)return t[0];let n=t.find(o=>o===e);if(n!==void 0)return n;let r=t.filter(o=>o.startsWith(e));if(e.length===0||r.length!==1)throw new RangeError(`'arg' should be one of ${t.map(o=>`\u201C${o}\u201D`).join(", ")}`);return r[0]}var ye=e=>e==null;function ln(e,t,n){if(!ye(e)&&!t(e))throw new RangeError(n)}var So=e=>e>=0&&e<=1,As=e=>e>0&&e<1/0;function nn(e,t){if(e.filter(ye).length!==1)throw new RangeError(t)}function Ye(e,t,n,r={}){return _l(e,t,n,r).root}var Nr=(e,t,n,r)=>Qt(e,t,{ncp:n,lowerTail:r});function Ka(e,t){if(e!=null&&!(typeof e=="number"&&e>=0&&e<=1))throw new RangeError(t)}var Ua="'sig.level' must be numeric in [0, 1]";function pp(e={}){B(e,mp,"powerTTest");let{n:t,delta:n,power:r}=e,o=e.sd===void 0?1:e.sd,s=e.sigLevel===void 0?.05:e.sigLevel;nn([t,n,o,r,s],"exactly one of 'n', 'delta', 'sd', 'power', and 'sig.level' must be NULL"),Ka(s,Ua),ln(r,So,"'power' must be a probability"),ln(o,As,"'sd' must be a positive finite number"),ln(t,w=>w>=2,"number of observations in each group must be at least 2"),ln(n,w=>!Number.isNaN(w),"'delta' must not be NaN"),ln(e.tol,As,"'tol' must be a positive finite number");let i=Se(e.type,["two.sample","one.sample","paired"]),c=Se(e.alternative,["two.sided","one.sided"]),a=e.strict??!1,l=e.tol??Ul,f=i==="two.sample"?2:1,m=c==="two.sided"?2:1;m===2&&!ye(n)&&(n=Math.abs(n));let u=(w,d,b,N)=>{let v=Math.max(1e-7,w-1)*f,E=Wt(N/m,v,{lowerTail:!1}),g=Math.sqrt(w/f)*d/b;Number.isFinite(g)||(g=Math.sqrt(w/f)*(d/b));let y=Nr(E,v,g,!1);return a&&m===2?y+Nr(-E,v,g,!0):y};if(ye(r))r=u(t,n,o,s);else{let w=r;if(ye(t)){if(n===0)throw new RangeError("'n' cannot be determined when 'delta' is 0: the power does not depend on it");t=Ye(d=>u(d,n,o,s)-w,2,1e7,{tol:l,extendInt:"upX"})}else if(ye(o)){if(n===0)throw new RangeError("'sd' cannot be determined when 'delta' is 0: the power does not depend on it");let d=Math.abs(n);o=Ye(b=>u(t,n,b,s)-w,d*1e-7,d*1e7,{tol:l,extendInt:n>0?"downX":"upX"})}else ye(n)?n=Ye(d=>u(t,d,o,s)-w,o*1e-7,o*1e7,{tol:l,extendInt:"upX"}):s=Ye(d=>u(t,n,o,d)-w,1e-10,1-1e-10,{tol:l,extendInt:"yes"})}let h=i==="paired"?"n is number of *pairs*, sd is std.dev. of *differences* within pairs":i==="two.sample"?"n is number in *each* group":void 0,p={"two.sample":"Two-sample","one.sample":"One-sample",paired:"Paired"}[i]+" t test power calculation";return{n:t,delta:n,sd:o,sigLevel:s,power:r,alternative:c,...h===void 0?{}:{note:h},method:p,warnings:[]}}function dp(e={}){B(e,up,"powerPropTest");let{n:t,p1:n,p2:r,power:o}=e,s=e.sigLevel===void 0?.05:e.sigLevel;nn([t,n,r,o,s],"exactly one of 'n', 'p1', 'p2', 'power', and 'sig.level' must be NULL"),Ka(s,Ua),ln(o,So,"'power' must be a probability"),ln(n,So,"'p1' must be a probability"),ln(r,So,"'p2' must be a probability"),ln(t,u=>u>0,"number of observations in each group must be positive"),ln(e.tol,As,"'tol' must be a positive finite number");let i=Se(e.alternative,["two.sided","one.sided"]),c=e.strict??!1,a=e.tol??Ul,l=i==="two.sided"?2:1,f=(u,h,p,w)=>{let d=le(w/l,{lowerTail:!1});if(c&&l===2){let b=(h+p)/2,N=h*(1-h),v=p*(1-p),E=b*(1-b),g=Math.abs(h-p),y=Math.sqrt(N+v);return se((Math.sqrt(u)*g-d*Math.sqrt(2*E))/y)+se((Math.sqrt(u)*g+d*Math.sqrt(2*E))/y,{lowerTail:!1})}return se((Math.sqrt(u)*Math.abs(h-p)-d*Math.sqrt((h+p)*(1-(h+p)/2)))/Math.sqrt(h*(1-h)+p*(1-p)))},m=[];if(ye(o))o=f(t,n,r,s);else{let u=o;if(ye(t)){if(n===r)throw new RangeError("'n' cannot be determined when 'p1' equals 'p2': the power does not depend on it");t=Ye(h=>f(h,n,r,s)-u,1,1e7,{tol:a,extendInt:"upX"})}else if(ye(n)){if(r===0)throw new RangeError("'p1' cannot be determined when 'p2' is 0: no p1 in [0, p2] differs from p2");n=Ye(h=>f(t,h,r,s)-u,0,r,{tol:a,extendInt:"yes"}),n<0&&m.push("No p1 in [0, p2] can be found to achieve the desired power")}else if(ye(r)){if(n===1)throw new RangeError("'p2' cannot be determined when 'p1' is 1: no p2 in [p1, 1] differs from p1");r=Ye(h=>f(t,n,h,s)-u,n,1,{tol:a,extendInt:"yes"}),r>1&&m.push("No p2 in [p1, 1] can be found to achieve the desired power")}else s=Ye(h=>f(t,n,r,h)-u,1e-10,1-1e-10,{tol:a,extendInt:"upX"}),(s<0||s>1)&&m.push("No significance level [0, 1] can be found to achieve the desired power")}return Q("powerPropTest",{n:t,p1:n,p2:r,sigLevel:s,power:o,alternative:i,note:"n is number in *each* group",method:"Two-sample comparison of proportions power calculation",warnings:m})}var Kl=1e16,qs=(e,t)=>e>Kl&&e<1/0&&t>Kl;function ks(e,t,n,r){let o=le(r,{lowerTail:!1}),s=n/e,i=Math.sqrt(2/e+2/t),c=Math.hypot(Math.sqrt(2*(1+2*s)/e),Math.sqrt(2/t)*(1+s));return se((o*i-s)/c,{lowerTail:!1})}function gp(e,t,n,r){if(n===0)return 0;let o=l=>Math.floor(Math.log2(l)),[s,i,c,a]=[o(e),o(t),o(n),o(r)];return V(V(e,-s)*V(t,-i)*V(n,-c)/V(r,-a),s+i+c-a)}function wp(e={}){B(e,hp,"powerAnovaTest");let{groups:t,n,betweenVar:r,withinVar:o,power:s}=e,i=e.sigLevel===void 0?.05:e.sigLevel;if(nn([t,n,r,o,s,i],"exactly one of 'groups', 'n', 'between.var', 'within.var', 'power', and 'sig.level' must be NULL"),!ye(t)&&!(t>=2))throw new RangeError("number of groups must be at least 2");if(!ye(n)&&!(n>=2))throw new RangeError("number of observations in each group must be at least 2");Ka(i,Ua),ln(s,So,"'power' must be a probability"),ln(r,a=>a>=0&&a<1/0,"'betweenVar' must be a nonnegative finite number"),ln(o,As,"'withinVar' must be a positive finite number");let c=(a,l,f,m,u)=>{let h=(a-1)*l*f/m;h<1/0||(h=(a-1)*l*(f/m)),!(h<1/0)&&Number.isFinite(a)&&Number.isFinite(l)&&(h=gp(a-1,l,f,m));let p=a-1,w=(l-1)*a;return h===1/0?1:qs(p,w)?ks(p,w,h,u):Rn(Gn(u,p,w,{lowerTail:!1}),p,w,{ncp:h,lowerTail:!1})};if(ye(s))s=c(t,n,r,o,i);else{let a=s;if(ye(t))t=Ye(l=>c(l,n,r,o,i)-a,2,100);else if(ye(n)){if(r===0)throw new RangeError("'n' cannot be determined when 'betweenVar' is 0: the power does not depend on it");n=Ye(l=>c(t,l,r,o,i)-a,2,1e5)}else if(ye(o)){if(r===0)throw new RangeError("'withinVar' cannot be determined when 'betweenVar' is 0: the power does not depend on it");o=Ye(l=>c(t,n,r,l,i)-a,r*1e-7,r*1e7)}else ye(r)?r=Ye(l=>c(t,n,l,o,i)-a,o*1e-7,o*1e7):i=Ye(l=>c(t,n,r,o,l)-a,1e-10,1-1e-10)}return{groups:t,n,betweenVar:r,withinVar:o,sigLevel:i,power:s,note:"n is number in each group",method:"Balanced one-way analysis of variance power calculation",warnings:[]}}var bp=["correct","p","rescaleP"],vp=["confLevel"],Np=["alternative","rowScores","colScores"];function Zt(e){let t=[0,0];for(let n of e)t=he(t,[n,0]);return t}function Ya(e){let t=e.map(r=>{if(typeof r!="object"||r===null||typeof r.length!="number")throw new TypeError("'x' must be an array of numbers or an array of rows");return Array.from(r)}),n=t[0].length;if(t.some(r=>r.length!==n))throw new TypeError("all rows of the matrix must have the same length");if(t.flat().some(r=>typeof r!="number"))throw new TypeError("the entries of 'x' must be numbers");return t}var Ga=e=>e.length>0&&typeof e[0]!="number",Xa=(e,t)=>Number.isNaN(e)?NaN:wt(e,t/2,2,!1,!1);function Hl(e,t){if(!(e>=0&&t>=0&&e<1/0&&t<1/0))return NaN;let n=Math.sqrt(e),r=Math.sqrt(e+1),o=n*r+t,s=(e-t)*(e+t);if(Number.isFinite(s)){let c=o===0?0:(s+e)/o;return(2*(e-t)+2*c)/(n+r+Math.sqrt(4*t+1))}let i=(e-t)*((e+t)/o)+e/o;return(.5*(e-t)+.5*i)/((n+r+2*Math.sqrt(t+.25))/4)}function Ao(e,t){return e.length>0&&Array.isArray(e[0])?e.map((n,r)=>n.map((o,s)=>Hl(o,t[r][s]))):e.map((n,r)=>Hl(n,t[r]))}var yp=["none","williams","yates"],Gl="Log likelihood ratio (G-test)";function Mp(e,t={}){B(t,bp,"gTest");let n=Se(t.correct,yp);if(typeof e!="object"||e===null||typeof e.length!="number")throw new TypeError("'x' must be an array of counts or of rows");if(Ga(e)){let o=Ya(e);return Yl(o.flat()),o.length===1?Q("gTest",Ha(o[0],n,t)):o[0].length===1?Q("gTest",Ha(o.map(s=>s[0]),n,t)):Q("gTest",Ep(o,n))}let r=Array.from(e);if(r.some(o=>typeof o!="number"))throw new TypeError("the entries of 'x' must be numbers");return Yl(r),Q("gTest",Ha(r,n,t))}function Yl(e){if(e.some(t=>!(t>=0&&t<1/0)))throw new RangeError("all entries of 'x' must be nonnegative and finite");if(!e.some(t=>t>0))throw new RangeError("at least one entry of 'x' must be positive")}function Xl(e,t){return e<1/0?0:Math.ceil(Math.log2(t))+1}function Ql(e,t){if(e.some(r=>r<0))return NaN;let n=[0,0];for(let r=0;r<e.length;r++){let o=e[r],s=t[r];if(o>0&&s[0]===0)return 1/0;n=he(n,sl(o,s))}return n[0]+n[1]}function Ep(e,t){let n=e.length,r=e[0].length;if(t==="yates"&&(n!==2||r!==2))throw new RangeError("Yates' correction requires a 2 x 2 matrix");let o=Zt(e.flat()),s=Xl(o[0],n*r),i=s===0?e:e.map(v=>v.map(E=>V(E,-s))),c=s===0?o:Zt(i.flat()),a=i.map(v=>Zt(v)),l=i[0].map((v,E)=>Zt(i.map(g=>g[E]))),f=a.map(v=>l.map(E=>_e(v,rt(E,c)))),m=i;if(t==="yates"){let[[v,E],[g,y]]=i,x=v*y-E*g>0?-.5:.5,M=V(.5,-s),R=x<0?-M:M;m=[[v+R,E-R],[g-R,y+R]]}let u=Ql(m.flat(),f.flat()),h=Number.isNaN(u)?["NaNs produced"]:[],p=1;if(t==="williams"){if(a.some(y=>y[0]===0)||l.some(y=>y[0]===0))throw new RangeError("Williams' correction is not defined for a table with an empty row or column");let v=c[0]+c[1],E=a.reduce((y,x)=>y+1/(x[0]+x[1]),0),g=l.reduce((y,x)=>y+1/(x[0]+x[1]),0);p=1+V((v*E-1)*(v*g-1)/(6*v*(n-1)*(r-1)),-s)}let w=V(2*u,s)/p,d=(n-1)*(r-1),b=f.map(v=>v.map(E=>V(E[0]+E[1],s)));return{method:`${Gl} test of independence ${t==="none"?"without correction":t==="williams"?"with Williams' correction":"with Yates' correction"}`,statistic:w,statisticName:"G",parameter:d,parameterName:"X-squared df",pValue:Xa(w,d),observed:s===0?m:m.map(v=>v.map(E=>V(E,s))),expected:b,ftResiduals:Ao(e,b),warnings:h}}function Ha(e,t,n){let r=e.length;if(r<2)throw new RangeError("'x' must have at least 2 elements");if(t==="yates"&&r!==2)throw new RangeError("Yates' correction requires 2 data values");let o=n.rescaleP??!1;if(typeof o!="boolean")throw new TypeError("'rescaleP' must be a boolean");let s=n.p===void 0?e.map(()=>1/r):Array.from(n.p);if(s.length!==r)throw new RangeError("'x' and 'p' must have the same number of elements");if(s.some(v=>typeof v!="number"||Number.isNaN(v)))throw new RangeError("'p' must not contain missing values");if(s.some(v=>v<0))throw new RangeError("probabilities must be non-negative");if(s.some(v=>v===1/0))throw new RangeError("probabilities must be finite");let i=Zt(s);if(o){if(!(i[0]>0&&i[0]<1/0))throw new RangeError("the probabilities to rescale must have a positive finite sum")}else if(Math.abs(i[0]-1+i[1])>Math.sqrt(Number.EPSILON))throw new RangeError("probabilities must sum to 1");let c=Zt(e),a=Xl(c[0],r),l=a===0?e:e.map(v=>V(v,-a)),f=a===0?c:Zt(l),m=s.map(v=>o?rt(Et(f,v),i):Et(f,v)),u=m.map(v=>v[0]+v[1]),h=l;if(t==="yates"){let v=V(.5,-a),E=l[0]-u[0];E>V(.25,-a)?h=[l[0]-v,l[1]+v]:-E>V(.25,-a)&&(h=[l[0]+v,l[1]-v])}let p=Ql(h,m);if(!o&&!Number.isNaN(p)){let v=f[0]+f[1];p-=v*(i[0]-1+i[1])}let w=Number.isNaN(p)?["NaNs produced"]:[],d=t==="williams"?1+(r+1)/(6*V(f[0]+f[1],a)):1,b=V(2*p,a)/d,N=u.map(v=>V(v,a));return{method:`${Gl} goodness of fit test`,statistic:b,statisticName:"G",parameter:r-1,parameterName:"X-squared df",pValue:Xa(b,r-1),observed:a===0?h:h.map(v=>V(v,a)),expected:N,ftResiduals:Ao(e,N),warnings:w}}function xp(e){if(typeof e!="object"||e===null||!Ga(e))throw new RangeError("'x' must be square with at least two rows and columns");let t=Ya(e),n=t.length;if(n<2||t[0].length!==n)throw new RangeError("'x' must be square with at least two rows and columns");if(t.flat().some(r=>!(r>=0&&r<1/0)))throw new RangeError("all entries of 'x' must be nonnegative and finite");return t}function Rp(e){let t=xp(e),n=Zt(t.flat())[0],r=n>2**400||n>0&&n<2**-400?Math.round(Math.log2(n)):0,o=r===0?t:t.map(g=>g.map(y=>V(y,-r))),s=o.length,i=o.map(g=>Zt(g)),c=o.map((g,y)=>Zt(o.map(x=>x[y]))),a=o.map((g,y)=>o[y].some((x,M)=>M!==y&&x>0)||o.some((x,M)=>M!==y&&x[y]>0)).flatMap((g,y)=>g?[y]:[]),l=a.length;if(l<2)throw new RangeError("Too many equal marginals, cannot compute");let f=a.map((g,y)=>y),m=g=>f[g]===g?g:f[g]=m(f[g]);for(let g=0;g<l;g++)for(let y=g+1;y<l;y++)o[a[g]][a[y]]+o[a[y]][a[g]]>0&&(f[m(g)]=m(y));if(a.some((g,y)=>m(y)!==m(0)))throw new RangeError("the categories fall into groups without counts between them: the covariance matrix is singular");let u=l-1,h=a.slice(0,u).map(g=>{let y=he(i[g],[-c[g][0],-c[g][1]]);return y[0]+y[1]}),p=a.slice(0,u).map(g=>a.slice(0,u).map(y=>{if(g!==y)return-(o[g][y]+o[y][g]);let x=0;for(let M=0;M<s;M++)M!==g&&(x+=o[g][M]+o[M][g]);return x})),w=p.map(g=>g.map(()=>0)),d=[];for(let g=0;g<u;g++){let y=p[g][g];for(let x=0;x<g;x++)y-=w[g][x]*w[g][x]*d[x];d.push(y);for(let x=g+1;x<u;x++){let M=p[x][g];for(let R=0;R<g;R++)M-=w[x][R]*w[g][R]*d[R];w[x][g]=M/y}}let b=0,N=[];for(let g=0;g<u;g++){let y=h[g];for(let x=0;x<g;x++)y-=w[g][x]*N[x];N.push(y),b+=y*y/d[g]}let v=Zt(a.flatMap(g=>a.map(y=>t[g][y]))),E=l-1;return b=V(b,r),Q("stuartMaxwellTest",{method:"Stuart-Maxwell test",statistic:b,statisticName:"chi-squared",parameter:E,parameterName:"df",pValue:Xa(b,E),N:v[0]+v[1],warnings:[]})}function Tp(e,t={}){B(t,vp,"goodmanKruskalGamma");let n=t.confLevel??.95;if(!(typeof n=="number"&&n>0&&n<1))throw new RangeError("'confLevel' must be a single number in (0, 1)");let r=Zl(e),o=Zt(r.flat())[0],s=0;(o>2**250||o>0&&o<2**-250)&&(s=2*Math.round((Math.log2(o)-250)/2));let i=s===0?r:r.map(g=>g.map(y=>V(y,-s))),{pc:c,pd:a}=Sp(i),l=[0,0],f=[0,0];i.forEach((g,y)=>g.forEach((x,M)=>{l=he(l,Et(c[y][M],x)),f=he(f,Et(a[y][M],x))})),l=[l[0]/2,l[1]/2],f=[f[0]/2,f[1]/2];let m=he(l,f),u=rt(Le(l,f),m)[0],h=_e(m,m),p=i.map((g,y)=>g.map((x,M)=>2*rt(Le(_e(f,c[y][M]),_e(l,a[y][M])),h)[0])),w=Math.max(...p.flat().map(Math.abs)),d=w>0&&w<1/0?Math.floor(Math.log2(w)):0,b=0;i.forEach((g,y)=>g.forEach((x,M)=>{let R=V(p[y][M],-d);b+=x*R*R}));let N=V(Math.sqrt(b),d-s/2),v=(1-n)/2,E=v<1e-8?le(v,{lowerTail:!1}):le(1-v);return Q("goodmanKruskalGamma",{estimate:u,confInt:[Math.max(-1,u-E*N),Math.min(1,u+E*N)],confLevel:n,ase:N,warnings:[]})}function Zl(e){if(typeof e!="object"||e===null||!Ga(e))throw new TypeError("'table' must be an array of rows");let t=Ya(e);if(t[0].length===0)throw new RangeError("'table' must have at least one column");if(t.flat().some(n=>!(n>=0&&n<1/0)))throw new RangeError("all entries of 'table' must be nonnegative and finite");return t}function Sp(e){let t=e.length,n=e[0].length,r=[0,0],o=e.map(p=>{let w=[],d=r;for(let b=0;b<n;b++)w.push(d),d=he(d,[p[b],0]);return w}),s=e.map(p=>{let w=new Array(n),d=r;for(let b=n-1;b>=0;b--)w[b]=d,d=he(d,[p[b],0]);return w}),i=p=>{let w=[],d=new Array(n).fill(r);for(let b=0;b<t;b++)w.push(d),d=d.map((N,v)=>he(N,p[b][v]));return w},c=p=>{let w=new Array(t),d=new Array(n).fill(r);for(let b=t-1;b>=0;b--)w[b]=d,d=d.map((N,v)=>he(N,p[b][v]));return w},a=i(o),l=c(s),f=i(s),m=c(o),u=a.map((p,w)=>p.map((d,b)=>he(d,l[w][b]))),h=f.map((p,w)=>p.map((d,b)=>he(d,m[w][b])));return{pc:u,pd:h}}function Ap(e,t={}){B(t,Np,"lblTest");let n=Ze(t.alternative),r=Zl(e),o=r.length,s=r[0].length,i=(R,T,S)=>{if(R===void 0)return Array.from({length:T},(C,L)=>L+1);let A=Array.from(R);if(A.length!==T)throw new RangeError(`'${S}' must have one score for each ${S==="rowScores"?"row":"column"}`);if(A.some(C=>typeof C!="number"||!Number.isFinite(C)))throw new RangeError(`'${S}' must be finite numbers`);return A},c=i(t.rowScores,o,"rowScores"),a=i(t.colScores,s,"colScores"),f=Zt(r.flat())[0]>2**1e3?2*Math.ceil((Math.log2(Math.max(...r.flat()))-990)/2):0,m=f===0?r:r.map(R=>R.map(T=>V(T,-f))),u=m.map(R=>Zt(R)).map(R=>R[0]+R[1]),h=m[0].map((R,T)=>Zt(m.map(S=>S[T]))).map(R=>R[0]+R[1]),p=u.reduce((R,T)=>R+T,0);if(!(f>0||p>1))throw new RangeError("the table must have a total above 1");let w=u.reduce((R,T,S)=>R+T*c[S],0)/p,d=h.reduce((R,T,S)=>R+T*a[S],0)/p,b=c.map(R=>R-w),N=a.map(R=>R-d),v=u.reduce((R,T,S)=>R+T*b[S]*b[S],0),E=h.reduce((R,T,S)=>R+T*N[S]*N[S],0),g=0;if(m.forEach((R,T)=>R.forEach((S,A)=>g+=S*b[T]*N[A])),!(v>0&&E>0))throw new RangeError("the row and column scores must vary over the observations (the variance of the statistic is 0)");let y=g/Math.sqrt(v)/Math.sqrt(E),x=f===0?Math.sqrt(p-1)*y:V(Math.sqrt(p-V(1,-f))*y,f/2),M=n==="less"?se(x):n==="greater"?se(x,{lowerTail:!1}):2*se(-Math.abs(x));return Q("lblTest",{method:"Linear-by-Linear Association Test",alternative:n,statistic:x,statisticName:"Z",pValue:M,warnings:[]})}var Jl=["correct","p","rescaleP","simulatePValue","B","nMinus1"];function gn(e){let t=[0,0],n=0;for(let r of e)t=he(t,[r,0]),n+=r;return Number.isFinite(n)?t[0]:n}function qp(e,t,n){return ct(t)&&B(t,Jl,"chisqTest"),B(n,Jl,"chisqTest"),Array.isArray(t)?Q("chisqTest",kp(e,t,n??{})):Q("chisqTest",Ip(e,t??{}))}var nf="Chi-squared approximation may be incorrect";function kp(e,t,n){if(e.length!==t.length)throw new RangeError("'x' and 'y' must have the same length");let r=u=>u==null;for(let u of[...e,...t])if(!r(u)&&typeof u!="string")throw new TypeError("factor values must be strings (or null for missing values)");let o=[],s=[];e.forEach((u,h)=>{let p=t[h];!r(u)&&!r(p)&&(o.push(u),s.push(p))});let i=[...new Set(o)].sort(Tn),c=[...new Set(s)].sort(Tn);if(i.length<2||c.length<2)throw new RangeError("'x' and 'y' must have at least 2 levels");let a=new Map(i.map((u,h)=>[u,h])),l=new Map(c.map((u,h)=>[u,h])),f=i.map(()=>c.map(()=>0));return o.forEach((u,h)=>f[a.get(u)][l.get(s[h])]++),{...cf(f,n),rowLevels:i,colLevels:c}}function Ip(e,t){if(e.length>0&&typeof e[0]!="number"){let r=e.map(s=>Array.from(s)),o=r[0].length;if(r.some(s=>s.length!==o))throw new TypeError("all rows of the matrix must have the same length");return r.length===1?Qa(r[0],t):o===1?Qa(r.map(s=>s[0]),t):cf(r,t)}return Qa(Array.from(e),t)}function rf(e){let t=e.B??2e3;if(!(Number.isInteger(t)&&t>=1&&t<2**31))throw new RangeError("'B' must be a positive integer");return t}function of(e,t,n){let r=(1-64*Number.EPSILON)*t,o=0;for(let s=0;s<e.length;s++)e[s]>=r&&o++;return(1+o)/(Math.trunc(n)+1)}function sf(e,t){return Number.isNaN(e)?NaN:wt(e,t/2,2,!1,!1)}function af(e){if(!e.some(t=>t<5)&&e.some(Number.isNaN))throw new RangeError("missing value where TRUE/FALSE needed (the expected counts contain NaN)")}function Qa(e,t){if(t.nMinus1??!1)throw new RangeError("'nMinus1' requires a contingency table with at least two rows and columns");let n=e.length;if(e.some(w=>!(w>=0&&w<1/0)))throw new RangeError("all entries of 'x' must be non-negative and finite");if(n<2)throw new TypeError("'x' must have at least 2 elements");let r=gn(e);if(!(r>0))throw new RangeError("at least one entry of 'x' must be positive");let o=t.p===void 0?e.map(()=>1/n):Array.from(t.p);if(o.length!==n)throw new TypeError("'x' and 'p' must have the same number of elements");if(o.some(Number.isNaN))throw new RangeError("p contains missing values");if(o.some(w=>w<0))throw new RangeError("probabilities must be non-negative");if(o.some(w=>w===1/0))throw new RangeError("probabilities must be finite");if(t.rescaleP??!1){let w=gn(o),d=w<1/0?0:Math.ceil(Math.log2(n)),b=d===0?o:o.map(v=>V(v,-d)),N=d===0?w:gn(b);o=b.map(v=>v/N)}else if(Math.abs(gn(o)-1)>Math.sqrt(Number.EPSILON))throw new RangeError("probabilities must sum to 1");let s=r<1/0?0:2*Math.ceil(Math.log2(n)/2),i=s===0?e:e.map(w=>V(w,-s)),c=s===0?r:gn(i),a=o.map(w=>c*w),l=s===0?a:a.map(w=>V(w,s));af(l);let f=o.map((w,d)=>a[d]*(1-w)),m=V(gn(i.map((w,d)=>Vt(w-a[d],a[d]))),s),u="Chi-squared test for given probabilities",h=n-1,p;if(t.simulatePValue??!1){let w=rf(t);if(!Number.isInteger(r))throw new RangeError("the simulation needs counts summing to an integer");u+=No(w),h=NaN,p=of(_p(r,o,l,w),m,w)}else p=sf(m,h);return{method:u,statistic:m,statisticName:"X-squared",parameter:h,parameterName:"df",pValue:p,observed:e,expected:l,residuals:i.map((w,d)=>V((w-a[d])/Math.sqrt(a[d]),s/2)),stdres:i.map((w,d)=>V((w-a[d])/Math.sqrt(f[d]),s/2)),ftResiduals:Ao(e,l),warnings:Number.isFinite(h)&&l.some(w=>w<5)?[nf]:[]}}function _p(e,t,n,r){let o=t.length;if(r*e>2**27)throw new RangeError("the simulation needs B * sum(x) <= 2^27 (134217728) draws");let s=xl(o,t);if(r*e===0)return new Float64Array(0);let i=new Float64Array(r),c=new Float64Array(o);for(let a=0;a<r;a++){c.fill(0);for(let f=0;f<e;f++)c[s()]++;let l=0;for(let f=0;f<o;f++)l+=(c[f]-n[f])**2/n[f];i[a]=l}return i}function Lp(e,t,n,r){let o=e.length,s=t.length,i=e.reduce((f,m)=>f+m,0);if(i>ps)throw new RangeError("the simulation needs a total count of at most 16777216 (2^24)");let c=ds(i),a=new Int32Array(o*s),l=new Float64Array(Math.trunc(r));for(let f=0;f<l.length;f++){gs(e,t,c,a);let m=0;for(let u=0;u<s;u++)for(let h=0;h<o;h++){let p=n[h][u];m+=(a[h+u*o]-p)**2/p}l[f]=m}return l}function cf(e,t){let n=t.nMinus1??!1;if(typeof n!="boolean")throw new TypeError("'nMinus1' must be a boolean");if(n&&t.correct===!0)throw new RangeError("'nMinus1' uses no continuity correction: leave 'correct' out or set it to false");if(n&&(t.simulatePValue??!1))throw new RangeError("'nMinus1' cannot be combined with 'simulatePValue'");let r=!n&&(t.correct??!0),o=e.length,s=e[0].length,i=e.flat();if(i.some(v=>!(v>=0&&v<1/0)))throw new RangeError("all entries of 'x' must be non-negative and finite");let c=gn(i);if(!(c>0))throw new RangeError("at least one entry of 'x' must be positive");if(n&&!(c>1))throw new RangeError("'nMinus1' needs a table with a total above 1");let a=e.map(v=>gn(v)),l=e[0].map((v,E)=>gn(e.map(g=>g[E]))),f=a.map(v=>l.map(E=>ms(v,E,c)));af(f.flat());let m=e.map((v,E)=>v.map((g,y)=>g-f[E][y])),u="Pearson's Chi-squared test",h,p=(o-1)*(s-1),w,d=(t.simulatePValue??!1)&&a.every(v=>v>0)&&l.every(v=>v>0),b=!d&&r&&o===2&&s===2?Math.min(.5,...m.flat().map(Math.abs)):0,N=[];if((t.simulatePValue??!1)&&!d&&N.push("cannot compute simulated p-value with zero marginals"),d){if(e.some(g=>g.some(y=>!Number.isInteger(y))))throw new RangeError("the simulation needs integer counts");let v=rf(t);u+=No(v),h=gn(m.flatMap((g,y)=>g.map((x,M)=>Vt(x,f[y][M])))),p=NaN;let E=Lp(a.map(g=>Math.trunc(g)),l.map(g=>Math.trunc(g)),f,v);w=of(E,h,v)}else if(b>0)u+=" with Yates' continuity correction",h=gn(m.flatMap((v,E)=>v.map((g,y)=>Vt(Math.abs(g)-b,f[E][y]))));else if(h=gn(m.flatMap((v,E)=>v.map((g,y)=>Vt(g,f[E][y])))),n){u="Pearson's 'N-1' Chi-squared test";let v=h*(c-1);h=Number.isFinite(v)?v/c:h*((c-1)/c)}return{method:u,statistic:h,statisticName:"X-squared",parameter:p,parameterName:"df",pValue:w??sf(h,p),observed:e,expected:f,residuals:m.map((v,E)=>v.map((g,y)=>g/Math.sqrt(f[E][y]))),stdres:Cp(e,c,a,l,f,m),ftResiduals:Ao(e,f),warnings:Number.isFinite(p)&&f.some(v=>v.some(E=>E<5))?[...N,nf]:N}}function Cp(e,t,n,r,o,s){if(!Number.isFinite(t))return s.map((p,w)=>p.map((d,b)=>d/Math.sqrt(o[w][b]*(1-n[w]/t)*(1-r[b]/t))));let i=e.map(p=>ef(p)),c=e[0].map((p,w)=>ef(e.map(d=>d[w]))),a=Op(i),l=tf(i),f=tf(c),m=l.map(p=>rt(p,a)),u=c.map(p=>rt(p,a)),h=f.map(p=>rt(p,a));return e.map((p,w)=>p.map((d,b)=>{let N=_e(i[w],u[b]),v=Le([d,0],N),E=v[0]+v[1],g=_e(_e(N,m[w]),h[b]),y=g[0]+g[1];if(y>=Fp||E===0)return E/Math.sqrt(y);let x=N[0]+N[1],M=m[w][0]+m[w][1],R=h[b][0]+h[b][1];return E/Math.sqrt(x)/Math.sqrt(M)/Math.sqrt(R)}))}var Fp=2**-1022;function ef(e){let t=[0,0];for(let n of e)t=he(t,[n,0]);return t}function Op(e){let t=[0,0];for(let n of e)t=he(t,n);return t}function tf(e){let t=e.length,n=new Array(t+1);n[t]=[0,0];for(let s=t-1;s>=0;s--)n[s]=he(e[s],n[s+1]);let r=[],o=[0,0];for(let s=0;s<t;s++)r.push(he(o,n[s+1])),o=he(o,e[s]);return r}var Dp=Math.pow(Number.EPSILON,.25);function Kt(e,t,n,r={}){let o={tol:r.tol??Dp,maxiter:r.maxIter??1e3};return r.fLower!==void 0&&(o.fLower=r.fLower),r.fUpper!==void 0&&(o.fUpper=r.fUpper),br(e,t,n,o).root}function rr(e,t){let n=a=>e(Math.exp(a)),r=0,o=n(0),s=t?-1:1,i=n(s);for(;i!==0&&Math.sign(i)===Math.sign(o);){if(Math.abs(s)>745)return t?0:1/0;r=s,o=i,s*=2,i=n(s)}if(i===0)return Math.exp(s);let c=r<s?Kt(n,r,s,{tol:2**-50,fLower:o,fUpper:i}):Kt(n,s,r,{tol:2**-50,fLower:i,fUpper:o});return Math.exp(c)}var Pp=["alternative","confInt","confLevel","or","simulatePValue","B","midp"];function Io(e,t={}){B(t,Pp,"fisherTest");let n=e.length,r=n===0?0:e[0].length;if(e.some(f=>f.length!==r))throw new TypeError("all rows of the matrix must have the same length");if(n<2||r<2)throw new TypeError("'x' must have at least 2 rows and columns");let o=e.map(f=>Array.from(f));if(o.flat().some(f=>Number.isNaN(f)||f<0))throw new RangeError("all entries of 'x' must be non-negative and finite");if(o.flat().some(f=>f===1/0))throw new RangeError("'x' has entries too large to be integer");let s=o.map(f=>f.map(cl));if(s.flat().some(f=>f>2147483647))throw new RangeError("'x' has entries too large to be integer");let i=[],c=Vp(o,s);c!==null&&i.push(`'x' has been rounded to integer: ${c}`);let a="Fisher's Exact Test for Count Data";if(t.midp!==void 0&&typeof t.midp!="boolean")throw new TypeError("'midp' must be true or false");if((t.midp??!1)&&!(n===2&&r===2))throw new RangeError("the mid-p version ('midp') needs a 2 x 2 table");if(n===2&&r===2)return Q("fisherTest",{...Wp(s,t,a),warnings:i});let l=t.alternative??"two.sided";if(t.simulatePValue??!1){let f=t.B??2e3;if(!(Number.isInteger(f)&&f>=1&&f<2**31))throw new RangeError("'B' must be a positive integer");return Q("fisherTest",{method:a+No(f),alternative:l,pValue:$p(s,f),warnings:i})}return Q("fisherTest",{method:a,alternative:l,pValue:Bp(s),warnings:i})}function Vp(e,t){let n=Math.sqrt(Number.EPSILON),r=[],o=[];for(let l=0;l<e[0].length;l++)for(let f=0;f<e.length;f++)e[f][l]!==t[f][l]&&(r.push(e[f][l]),o.push(t[f][l]));let s=r.length;if(s===0)return null;let i=0;for(let l of r)i+=Math.abs(l)/s;let c="relative";Number.isFinite(i)&&i>n||(i=1,c="absolute");let a=0;for(let l=0;l<s;l++)a+=Math.abs(r[l]-o[l])/(s*i);return Number.isNaN(a)||a>n?`Mean ${c} difference: ${Rl(a)}`:null}function $p(e,t){let n=e.filter(d=>d.some(b=>b>0)),r=e[0].map((d,b)=>b).filter(d=>e.some(b=>b[d]>0));if(n.length<=1)throw new RangeError("need 2 or more non-zero row marginals");if(r.length<=1)throw new RangeError("need 2 or more non-zero column marginals");let o=n.map(d=>r.map(b=>d[b])),s=o.map(d=>d.reduce((b,N)=>b+N,0)),i=o[0].map((d,b)=>o.reduce((N,v)=>N+v[b],0)),c=s.length,a=i.length,l=[0,0];for(let d of o.flat())l=he(l,[-us(d),0]);let f=l[0]/(1+64*Number.EPSILON),m=s.reduce((d,b)=>d+b,0);if(m>ps)throw new RangeError("the simulation needs a total count of at most 16777216 (2^24)");let u=ds(m),h=new Int32Array(c*a),p=0,w=Math.trunc(t);for(let d=0;d<w;d++){gs(s,i,u,h);let b=0;for(let N=0;N<h.length;N++)b-=u[h[N]];b<=f&&p++}return Math.max(0,Math.min(1,(1+p)/(w+1)))}var lf=1+1e-7,zp=16,jp=2**24;function qo(e,t,n){let r=[0,0];for(let o=Math.max(0,t);o<=Math.min(n,e.length-1);o++)e[o]!==0&&(r=he(r,[e[o],0]));return r[0]}var Br=class{m;n;k;lo;hi;logCentral;constructor(t,n,r){this.m=t,this.n=n,this.k=r,this.lo=Math.max(0,r-n),this.hi=Math.min(r,t),this.logCentral=new Float64Array(this.hi-this.lo+1);let o=0,s=1;for(let i=this.lo;i<=this.hi;i++){let c=i-this.lo;c%zp===0||!(s>0&&s<1/0)?(o=bn(i,t,n,r,!0),s=1,this.logCentral[c]=o):this.logCentral[c]=o+Math.log(s),s*=(t-i)*(r-i)/((i+1)*(n-r+i+1))}}density(t){let n=this.logCentral.length,r=Math.log(t),o=new Float64Array(n),s=-1/0;for(let c=0;c<n;c++){let a=this.logCentral[c]+(this.lo+c)*r;o[c]=a,a>s&&(s=a)}for(let c=0;c<n;c++){let a=o[c]-s;o[c]=a<-746?0:Math.exp(a)}let i=qo(o,0,n-1);for(let c=0;c<n;c++)o[c]!==0&&(o[c]=o[c]/i);return o}mean(t){if(t===0)return this.lo;if(t===1/0)return this.hi;let n=this.density(t),r=[0,0];for(let o=0;o<n.length;o++)n[o]!==0&&(r=he(r,[(this.lo+o)*n[o],0]));return r[0]}tail(t,n,r){if(n===1)return r?sn(t-1,this.m,this.n,this.k,!1,!1):sn(t,this.m,this.n,this.k,!0,!1);if(n===0||n===1/0){let i=n===0?this.lo:this.hi;return(r?t<=i:t>=i)?1:0}let o=this.density(n),s=t-this.lo;return r?qo(o,s,o.length-1):qo(o,0,s)}midTail(t,n,r){if(n===1){let i=.5*bn(t,this.m,this.n,this.k,!1);return(r?sn(t,this.m,this.n,this.k,!1,!1):sn(t-1,this.m,this.n,this.k,!0,!1))+i}if(n===0||n===1/0){let i=n===0?this.lo:this.hi;return t===i?.5:(r?t<i:t>i)?1:0}let o=this.density(n),s=t-this.lo;return(r?qo(o,s+1,o.length-1):qo(o,0,s-1))+.5*o[s]}};function Ja(e,t,n){let r=e.density(n),o=r[t-e.lo]*lf,s=[0,0];for(let i of r)i<=o&&(s=he(s,[i,0]));return s[0]}function ff(e,t,n,r){let o=Math.max(0,r-n),s=Math.min(r,t),i=w=>bn(w,t,n,r,!0),c=i(e)+Math.log(lf),a=Math.min(s,Math.max(o,Math.floor((r+1)*(t+1)/(t+n+2))));for(;a<s&&i(a+1)>i(a);)a++;for(;a>o&&i(a-1)>i(a);)a--;let l=(w,d)=>{let b=w,N=d+1;for(;N-b>1;){let v=b+Math.floor((N-b)/2);i(v)<=c?b=v:N=v}return b},f=(w,d)=>{let b=w-1,N=d+1;for(;N-b>1;){let v=b+Math.floor((N-b)/2);i(v)<=c?N=v:b=v}return N},m,u;e<=a?(m=l(e,a),u=f(a+1,s)):(u=f(a,e),m=a-1<o||i(o)>c?o-1:l(o,a-1));let h=m<o?0:sn(m,t,n,r,!0,!1),p=u>s?0:sn(u-1,t,n,r,!1,!1);return h+p}function ko(e,t){return rr(e,t)}function Wp(e,t,n){let r=Ze(t.alternative),o=t.or??1;if(!(o>=0))throw new RangeError("'or' must be a single number between 0 and Inf");let s=t.confLevel??.95;if(!(s>0&&s<1))throw new RangeError("confLevel must be a single number between 0 and 1");let[[i,c],[a,l]]=e,f=i,m=Math.min(i,l)+Math.min(c,a)+1;if(m>jp)throw new RangeError(`the exact distribution of the 2 x 2 table has ${m} values, more than 2^24`);let u=new Br(i+a,c+l,i+c),{lo:h,hi:p}=u,w=t.midp??!1,d;w?(r==="less"?d=u.midTail(f,o,!1):r==="greater"?d=u.midTail(f,o,!0):d=Math.min(1,2*Math.min(u.midTail(f,o,!1),u.midTail(f,o,!0))),n=r==="two.sided"?"Central Fisher's Exact Test (mid-p version)":"One-sided Fisher's Exact Test (mid-p version)"):r==="less"?d=u.tail(f,o,!1):r==="greater"?d=u.tail(f,o,!0):o===0?d=f===h?1:0:o===1/0?d=f===p?1:0:d=Ja(u,f,o);let b;if(f===h)b=0;else if(f===p)b=1/0;else{let y=u.mean(1);b=y===f?1:ko(x=>u.mean(x)-f,y>f)}let N={method:n,alternative:r,pValue:d,estimate:b,estimateNames:"odds ratio",nullValue:o,nullValueName:"odds ratio",warnings:[]};if(!(t.confInt??!0))return N;let v=y=>{if(f===p)return 1/0;if(w){if(f===h&&y>=.5)return 0;let M=u.midTail(f,1,!1);return M===y?1:ko(R=>u.midTail(f,R,!1)-y,M<y)}let x=u.tail(f,1,!1);return x===y?1:ko(M=>u.tail(f,M,!1)-y,x<y)},E=y=>{if(f===h)return 0;if(w){if(f===p&&y>=.5)return 1/0;let M=u.midTail(f,1,!0);return M===y?1:ko(R=>u.midTail(f,R,!0)-y,M>y)}let x=u.tail(f,1,!0);return x===y?1:ko(M=>u.tail(f,M,!0)-y,x>y)},g;if(r==="less")g=[0,v(1-s)];else if(r==="greater")g=[E(1-s),1/0];else{let y=(1-s)/2;g=[E(y),v(y)]}return{...N,confInt:g,confLevel:s}}var Is=[[0,0]];function Sn(e){for(let t=Is.length;t<=e;t++)Is.push(he(Is[t-1],rl([t,0])));return Is[e]}function _s(e,t,n,r){let o=e+n,s=o-e,i=e-(o-s)+(n-s)+t+r,c=o+i;return[c,i-(c-o)]}var Za=class{scale=-1/0;hi=0;lo=0;add(t,n,r){if(t>this.scale){let a=Number.isFinite(this.scale)?Math.exp(this.scale-t):0;this.hi*=a,this.lo*=a,this.scale=t}let o=r*Math.exp(t-this.scale)*(1+n),s=this.hi+o,i=s-this.hi,c=this.hi-(s-i)+(o-i)+this.lo;this.hi=s+c,this.lo=c-(this.hi-s)}value(){return this.scale===-1/0?0:this.hi*Math.exp(this.scale)}};function Bp(e){let t=e.filter(v=>v.some(E=>E>0));if(t.length===0)throw new RangeError("all entries of the table are zero");let n=t[0].map((v,E)=>t.some(g=>g[E]>0));if(t=t.map(v=>v.filter((E,g)=>n[g])),t.length<2||t[0].length<2)return 1;t.length>t[0].length&&(t=t[0].map((v,E)=>t.map(g=>g[E])));let r=t.length,o=t.map(v=>v.reduce((E,g)=>E+g,0)),s=t[0].map((v,E)=>t.reduce((g,y)=>g+y[E],0)).sort((v,E)=>E-v),i=s.length,c=o.reduce((v,E)=>v+E,0),a=nl(Sn(c));for(let v of o)a=he(a,Sn(v));for(let v of s)a=he(a,Sn(v));let l=[0,0];for(let v of t.flat())l=Le(l,Sn(v));let f=he(l,[Math.log1p(1e-7),0]),m=1e-9*(1+Math.abs(f[0])),[u,h]=f,p=Array.from({length:i},()=>new Map),w=(v,E)=>{let g=E.join(","),y=p[v].get(g);if(y!==void 0)return y;let x=he(a,Sn(E.reduce((C,L)=>C+L,0)));for(let C of E)x=Le(x,Sn(C));for(let C=v;C<i;C++)x=Le(x,Sn(s[C]));let M={R:E,KZ:x,max:0,min:0,edges:[],pastHi:[],pastLo:[],count:[],index:new Map};if(p[v].set(g,M),v===i-1){let C=[0,0];for(let L of E)C=Le(C,Sn(L));return M.max=M.min=C[0],M.edges.push({to:M,hi:C[0],lo:C[1],mult:1}),M}let R=new Map,T=new Array(r+1).fill(0);for(let C=r-1;C>=0;C--)T[C]=T[C+1]+E[C];let S=new Array(r).fill(0),A=(C,L,k)=>{if(C===r-1){S[C]=L;let O=Le(k,Sn(L)),q=E.map((K,X)=>K-S[X]).sort((K,X)=>K-X),$=w(v+1,q),_=`${q.join(",")}|${O[0]}`,P=R.get(_);P===void 0?R.set(_,{to:$,hi:O[0],lo:O[1],mult:1}):P.mult++;return}let F=Math.max(0,L-T[C+1]),I=Math.min(E[C],L);for(let O=F;O<=I;O++)S[C]=O,A(C+1,L-O,Le(k,Sn(O)))};if(A(0,s[v],[0,0]),M.edges=[...R.values()],M.max=Nt(M.edges.map(C=>C.hi+C.to.max)),M.min=an(M.edges.map(C=>C.hi+C.to.min)),v===i-2){let C=M.edges.map($=>{let _=$.to.edges[0];return{v:he([$.hi,$.lo],[_.hi,_.lo]),mult:$.mult}});C.sort(($,_)=>$.v[0]-_.v[0]||$.v[1]-_.v[1]);let L=C.length,k=C[L-1].v[0],F=new Float64Array(L),I=new Float64Array(L),O=new Float64Array(L),q=[0,0];C.forEach(({v:$,mult:_},P)=>{F[P]=$[0],I[P]=$[1];let K=Le($,[k,0]);q=he(q,[_*Math.exp(K[0])*(1+K[1]),0]),O[P]=q[0]}),M.dist={hi:F,lo:I,cum:O,vmax:k,kv:he(a,[k,0])}}return M},d=w(0,[...o].sort((v,E)=>v-E)),b=new Za,N=(v,E,g,y)=>{if(E+v.max<=u-m){let[A,C]=_s(v.KZ[0],v.KZ[1],E,g);return b.add(A,C,y),!0}if(E+v.min>u+m)return!0;let x=v.dist;if(x===void 0)return!1;let[M,R]=_s(u,h,-E,-g),T=0,S=x.hi.length;for(;T<S;){let A=T+S>>>1,C=x.hi[A];C<M||C===M&&x.lo[A]<=R?T=A+1:S=A}if(T>0){let[A,C]=_s(x.kv[0],x.kv[1],E,g);b.add(A,C,y*x.cum[T-1])}return!0};N(d,0,0,1)||(d.pastHi.push(0),d.pastLo.push(0),d.count.push(1));for(let v=0;v<i-2;v++)for(let E of p[v].values()){let{pastHi:g,pastLo:y,count:x}=E;if(x.length!==0){for(let{to:M,hi:R,lo:T,mult:S}of E.edges)for(let A=0;A<x.length;A++){let[C,L]=_s(g[A],y[A],R,T),k=x[A]*S;if(N(M,C,L,k))continue;let F=M.index.get(C);F===void 0?(M.index.set(C,M.count.length),M.pastHi.push(C),M.pastLo.push(L),M.count.push(k)):M.count[F]+=k}E.pastHi=E.pastLo=E.count=[],E.index.clear()}}return Math.min(1,b.value())}function Ls(e){let t=e.every(n=>Number.isInteger(n))?1:2;return[t,e.map(n=>n*t).sort((n,r)=>n-r)]}function ti(e){return[...e].sort((t,n)=>t-n).join(",")}function ei(e,t,n){let r=n.length,o=new Float64Array(r),s=new Float64Array(r),i=0;for(let c=0;c<r;c++)o[c]=i+=n[c];i=0;for(let c=r-1;c>=0;c--)s[c]=i+=n[c];return{f:e,offset:t,lower:o,upper:s}}function ni(e){let[,t]=Ls(e),n=0,r=0;for(let o of t)r+=n+=o;return{steps:r,size:n+1}}function ri(e){let[t,n]=Ls(e),r=0;for(let i of n)r+=i;let o=new Float64Array(r+1);o[0]=1;let s=0;for(let i of n){s+=i;for(let c=s;c>=i;c--)o[c]=.5*(o[c]+o[c-i]);for(let c=i-1;c>=0;c--)o[c]=.5*o[c]}return ei(t,0,o)}function oi(e,t){let[,n]=Ls(e),r=n.length,o=Math.min(t,r-t),s=mf(n),i=0;for(let c=1;c<=r;c++)for(let a=Math.max(1,o-(r-c));a<=Math.min(c,o);a++)i+=s[c]-s[c-a]-s[a]+1;return{steps:i,size:s[r]-s[r-o]-s[o]+1}}function mf(e){let t=[0];for(let n of e)t.push(t[t.length-1]+n);return t}function si(e,t){let[n,r]=Ls(e),o=r.length,s=Math.min(t,o-t),i=mf(r),c=[];for(let h=0;h<=s;h++)c.push(new Float64Array(i[o]-i[o-h]-i[h]+1));c[0][0]=1;for(let h=1;h<=o;h++){let p=r[h-1];for(let w=Math.min(h,s);w>=Math.max(1,s-(o-h));w--){let d=c[w],b=c[w-1],N=w/h,v=(h-w)/h,E=i[w],g=i[h]-i[h-w],y=i[w-1]+p,x=Math.max(E,y);for(let M=g;M>=x;M--)d[M-E]=v*d[M-E]+N*b[M-y];for(let M=x-1;M>=E;M--)d[M-E]=v*d[M-E]}}let a=c[s],l=t*(t+1)/2;if(s===t)return ei(n,i[s]/n-l,a);let f=a.length,m=new Float64Array(f);for(let h=0;h<f;h++)m[h]=a[f-1-h];let u=i[o]-i[o-s];return ei(n,(i[o]-u)/n-l,m)}function or(e,t){let n=Math.ceil(e.f*(t-e.offset)+e.f*1e-8)-1;return n<0?0:n>=e.lower.length?1:Math.min(1,e.lower[n])}function yr(e,t){let n=or(e,t);if(Math.abs(n-1)>=1/64)return 1-n;let r=Math.ceil(e.f*(t-e.offset)+e.f*1e-8);return r>=e.upper.length?0:Math.min(1,e.upper[Math.max(0,r)])}var uf=["alternative","mu","paired","exact","correct","confInt","confLevel","tolRoot"];function _o(e,t,n={}){if(typeof e!="string"&&ct(t)&&B(t,uf,"wilcoxTest"),B(n,uf,"wilcoxTest"),typeof e=="string"){if(n.paired!==void 0)throw new Error("cannot use 'paired' in formula method");let w=bs(e,t,!0);return w.kind==="one"?_o(w.x,n):_o(w.x,w.y,w.kind==="paired"?{...n,paired:!0}:n)}let r;t!==void 0&&!ArrayBuffer.isView(t)&&!Array.isArray(t)?n=t:r=t;let o=Ze(n.alternative),s=n.mu??0,i=n.paired??!1,c=n.exact??null,a=Up(n.correct??!0),l=n.confInt??!1,f=n.confLevel??.95,m=n.tolRoot??1e-4;if(!Number.isFinite(s))throw new RangeError("mu must be a single finite number");if(l&&!(f>0&&f<1))throw new RangeError("confLevel must be a number strictly between 0 and 1");if(i&&r===void 0)throw new TypeError("y is required when paired is true");let u,h;if(i){let[w,d]=Jn(Array.from(e),Array.from(r));u=Dt(w.map((b,N)=>b-d[N]))}else u=Dt(Array.from(e)),r!==void 0&&(h=Dt(Array.from(r)));if(u.length<1)throw new Error("not enough (non-missing) 'x' observations");if(h!==void 0&&h.length<1)throw new Error("not enough (non-missing) 'y' observations");let p={alternative:o,mu:s,correct:a,confInt:l,confLevel:f,tolRoot:m,warnings:new Rt};return Q("wilcoxTest",h===void 0?Qp(u,c,i,p):sd(u,h,c,p))}function Up(e){if(typeof e=="boolean")return e?0:-1;if(!(Number.isInteger(e)&&e>=0&&e<=3))throw new RangeError("'correct' must be an integer between 0 and 3");return e}function Cs(e,t,n){return n?t==="two.sided"?Math.sign(e)*.5:t==="greater"?.5:-.5:0}var df=10*Number.EPSILON;function gf(e,t,n=0){let r=a=>Math.min(1,Math.max(a,0)),o=()=>n===0?se(e):r(se(e)-n*Xt(e)),s=()=>n===0?se(e,{lowerTail:!1}):r(se(e,{lowerTail:!1})+n*Xt(e));if(t==="less")return o();if(t==="greater")return s();let i=o();if(i<=.5)return 2*i;let c=s();return c>=1/64?2*(1-i):2*c}function Hp(e){let t=e*e;return[e*(t-3),e*(t*(t-10)+15),e*(t*(t*(t-21)+105)-105)]}function Yp(e,t,n){let r=t*(t+1)*(2*t+1),o=-(3*t*t+3*t-1)/(10*r),s=4*(3*t**4+6*t**3-3*t+1)/(35*r*r);return wf(e,o,s,n)}function Gp(e,t,n,r){let o=t+n,s=t*n,i=-(o*(o+1)-s)/(20*s*(o+1)),c=(2*(t**4+n**4)+4*s*(t*t+n*n)+6*s*s+4*(t**3+n**3)+(7*s+o-1)*o)/(210*s*s*(o+1)**2);return wf(e,i,c,r)}function wf(e,t,n,r){let[o,s,i]=Hp(e),c=t*o;return r>=2&&(c+=n*s),r>=3&&(c+=t*t/2*i),c}function ci(e){let t=e.length;if(t===0)return NaN;let n=Math.floor(t/2);return t%2===1?e[n]:it([e[n-1],e[n]])}var li=(e,t)=>e-t,ai=5e9,Xp=40;function ii(e){return new RangeError(`wilcoxTest: the exact conditional ${e} would take too long or too much memory: use exact: false`)}function Fs(e,t){if(e.size>16777216||e.steps>ai)throw ii(t);return e.steps}function bf(e){let t=e.length,n=bt(e.map(Math.abs)),r=0;for(let i=0;i<t;i++)e[i]>0&&(r+=n[i]);let o=0;for(let i of wn(n))o+=i*i*i-i;let s=Math.sqrt(t*(t+1)*(2*t+1)/24-o/48);return[r,r-t*(t+1)/4,s]}function Qp(e,t,n,r){let{alternative:o,mu:s}=r,i=e.map(g=>g-s);i.some((g,y)=>!Number.isFinite(g)&&Number.isFinite(e[y]))&&(i=e.map(g=>g/2-s/2));let a=i.some(g=>g===0),l=t??i.length<50,f=(g,y,x)=>({method:g,alternative:o,statistic:y,statisticName:"V",pValue:x,nullValue:s,nullValueName:n?"location shift":"location",warnings:r.warnings.list});if(l){let g=bt(i.map(Math.abs)),y=wn(g).length>0,x=0;for(let C=0;C<i.length;C++)i[C]>0&&(x+=g[C]);let M="Wilcoxon signed rank exact test";if(!y&&!a){let C=i.length,L=C*(C+1)/2,k;if(o==="less")k=$r(x,C,!0,!1);else if(o==="greater")k=$r(x-1,C,!1,!1);else{let I=x>L/2?$r(x-1,C,!1,!1):$r(x,C,!0,!1);k=Math.min(1,2*I)}let F=f(M,x,k);return r.confInt?{...F,...Zp(e,r)}:F}let R=g.filter((C,L)=>i[L]!==0);Fs(ni(R),"distribution");let T=ri(R),S;if(o==="less")S=or(T,x);else if(o==="greater")S=yr(T,x-.25);else{let C=0;for(let L of R)C+=L/2;S=Math.min(1,2*(x>C?yr(T,x-.25):or(T,x)))}let A=f(M,x,S);return r.confInt?{...A,...ed(e,r)}:A}let m=a?i.filter(g=>g!==0):i,u=m.length;if(u===0)throw new RangeError("no non-zero differences for the signed rank test");let[h,p,w]=bf(m),b=wn(bt(m.map(Math.abs))).length>0||a?Math.min(r.correct,0):r.correct,N=(p-Cs(p,o,b>=0))/w,v=gf(N,o,b>=1?Yp(N,u,b):0),E=f(`Wilcoxon signed rank test${r.correct>=0?" with continuity correction":""}`,h,v);return r.confInt?{...E,...td(e,r)}:E}function fi(e,t,n){return e==="less"?[-1/0,n]:e==="greater"?[t,1/0]:[t,n]}function hf(e,t,n){let r={confInt:fi(e,NaN,NaN),confLevel:0,estimate:t};return n!==void 0&&(r.estimateNames=n),r}function vf(e){if(!(e>0&&e<1/0))throw new RangeError("tolRoot must be a positive finite number")}function Nf(e){let t=e.length,n=[];for(let r=0;r<t;r++)for(let o=r;o<t;o++){let s=dn(e[r],e[o]);Number.isNaN(s)||n.push(s)}return n.sort(li)}function yf(e,t,n,r,o){let{alternative:s,confLevel:i}=r,c=e.length,a=s==="two.sided"?(1-i)/2:1-i,l=n(a);t(l)<=a+df&&l++;let f=ci(e);if(l===0)return{confInt:[-1/0,1/0],confLevel:1,estimate:f,estimateNames:o};let m=t(l-1);return{confInt:fi(s,e[l-1],e[c-l]),confLevel:1-(s==="two.sided"?2*m:m),estimate:f,estimateNames:o}}function Zp(e,t){let n=e.length;return yf(Nf(e),r=>$r(r,n,!0,!1),r=>dl(r,n,!0,!1),t,"(pseudo)median")}function Mf(e){let t=e.length,n=Float64Array.from(e).sort(),r=new Array(t);for(let s=0;s<t;s++){let i=e[s],c=0,a=t;for(;c<a;){let f=c+a>>>1;n[f]<i?c=f+1:a=f}let l=c;for(a=t;l<a;){let f=l+a>>>1;n[f]<=i?l=f+1:a=f}r[s]=(c+1+l)/2}let o="";for(let s=0;s<t;){let i=s;for(;i+1<t&&n[i+1]===n[s];)i++;i>s&&(o+=`${s}:${i-s+1},`),s=i+1}return{ranks:r,key:o}}function Ef(e,t,n,r,o,s,i,c,a,l){let{alternative:f,confLevel:m}=a,u=e.length,p=(f==="two.sided"?(1-m)/2:1-m)+df,w=f!=="less",d=f!=="greater",b=k=>dn(e[k-1],e[k]),N=Math.min(u-1,Math.max(1,Math.ceil(i.mean+le(Math.min(p,1))*i.sd+.1*i.sd+3))),v=8,E=k=>k===0?e[0]-1:b(k),g=k=>k===0?e[u-1]+1:b(u-k),y=[new Map,new Map];{let k=new Map,F=0,I=Xp*c;for(let[O,q]of[[0,E],[1,g]])if(!(O===0?!w:!d))for(let $=0;$<=N;$++){if(F+=I,F>ai)throw ii("interval");let _;try{_=t(q($))}catch{break}if(y[O].set($,[_.stat,_.key]),k.has(_.key)){k.delete(_.key),k.set(_.key,!0);continue}if(F+=r(_.scores),F>ai)throw ii("interval");k.set(_.key,!0),k.size>v&&k.delete(k.keys().next().value)}}let x=new Map,M=(k,F)=>{let I=k===0?E(F):g(F),O=y[k].get(F),q;if(O===void 0){let K=t(I);O=[K.stat,K.key],q=K.scores}let[$,_]=O,P=x.get(_);return P!==void 0?x.delete(_):(P=n(q??t(I).scores),x.size>=v&&x.delete(x.keys().next().value)),x.set(_,P),[$,P]},R=k=>{let[F,I]=M(0,k);return o(I,F)},T=k=>{let[F,I]=M(1,k);return s(I,F)},S=-1/0,A=0;if(w){let k=R(0);if(!(k>p)){let F=k,I=1;for(;I<=u-1;I++){let O=R(I);if(O>p)break;F=O}S=e[I-1],A=F}}let C=1/0,L=0;if(d){let k=T(0);if(!(k>p)){let F=k,I=u-1;for(;I>=1;I--){let O=T(u-I);if(O>p)break;F=O}C=e[I],L=F}}return{confInt:[S,C],confLevel:1-(A+L),estimate:ci(e),estimateNames:l}}function Jp(e,t){let n=e.map(r=>r-t);if(n.some(r=>Number.isNaN(r)))throw new Error("cannot compute the exact confidence interval: the shifted data have undefined values (infinite observations)");return Number.isFinite(t)&&n.some((r,o)=>!Number.isFinite(r)&&Number.isFinite(e[o]))&&(n=e.map(r=>r/2-t/2)),n}function ed(e,t){let n=e.length,r=s=>{let i=Jp(e,s),{ranks:c,key:a}=Mf(i.map(Math.abs)),l=0;for(let f=0;f<n;f++)i[f]>0&&(l+=c[f]);return{stat:l,scores:c,key:a}},o=n*(n+1)/2;return Ef(Nf(e),r,ri,s=>Fs(ni(s),"interval"),(s,i)=>yr(s,i-.25),or,{mean:o/2,sd:Math.sqrt(n*(n+1)*(2*n+1)/24)},n,t,"(pseudo)median")}function td(e,t){let{alternative:n,confLevel:r,tolRoot:o}=t,s=t.correct>=0,i=e.length;if(i===0)return hf(n,NaN);let c=an(e),a=Nt(e);if(!Number.isFinite(c)||!Number.isFinite(a)||i===1||c===a)return Number.isFinite(c)&&c===a&&t.warnings.add("cannot compute confidence interval when all observations are zero or tied"),hf(n,dn(c,a),"midrange");let l=(v,E)=>{let[,g,y]=bf(e.map(x=>x-v).filter(x=>x!==0));return(g-Cs(g,n,E))/y},f=l(c,s),m=l(a,s),u=v=>-le(v),h=v=>n==="greater"?!(f<u(v)):n==="less"?!(m>le(v/2)):!(f<u(v/2))&&!(m>le(v/2)),p=1-r,w=!1;for(;!h(p)&&p<1;)p*=2,w=!0;w&&t.warnings.add("requested conf.level not achievable"),vf(o);let d=v=>Kt(E=>l(E,s)-v,c,a,{tol:o,fLower:f-v,fUpper:m-v}),b;if(p>=1){let v=ci([...e].sort(li));b=fi(n,v,v)}else n==="greater"?b=[d(u(p)),1/0]:n==="less"?b=[-1/0,d(le(p))]:b=[d(u(p/2)),d(le(p/2))];let N=Kt(v=>l(v,!1),c,a,{tol:o,fLower:l(c,!1),fUpper:l(a,!1)});return{confInt:b,confLevel:w?1-Math.min(1,p):r,estimate:N,estimateNames:"(pseudo)median"}}function xf(e,t){let n=e.length,r=n-t,o=0;for(let a=0;a<t;a++)o+=e[a];let s=o-t*(t+1)/2,i=0;for(let a of wn(e))i+=a*a*a-a;let c=Math.sqrt(t*r/12*(n+1-i/(n*(n-1))));return[s,s-t*r/2,c,i>0]}function nd(e,t){let[n,r,o]=xf(bt([...e,...t]),e.length);return[n,r,o]}function rd(e,t){let n=e.length,r=n+t.length,o=(l,f)=>{let m=l===0||!Number.isFinite(l)||!Number.isFinite(f)?f:f/2;return l<m?-1:l>m?1:0},s=(l,f)=>l<n&&f<n?e[l]<e[f]?-1:e[l]>e[f]?1:0:l>=n&&f>=n?t[l-n]<t[f-n]?-1:t[l-n]>t[f-n]?1:0:l<n?o(e[l],t[f-n]):-o(e[f],t[l-n]),i=Array.from({length:r},(l,f)=>f);i.sort((l,f)=>s(l,f)||l-f);let c=new Array(r),a=0;for(;a<r;){let l=a;for(;l+1<r&&s(i[l+1],i[a])===0;)l++;let f=(a+l+2)/2;for(let m=a;m<=l;m++)c[i[m]]=f;a=l+1}return c}function od(e,t,n){return Rf(e,t,n).ranks}function Rf(e,t,n){let r=e.map(o=>o-n);if(r.some(o=>Number.isNaN(o))){let o=r.map((a,l)=>[a,l]).filter(([a])=>!Number.isNaN(a)),s=bt([...o.map(([a])=>a),...t]),i=new Array(e.length+t.length);o.forEach(([,a],l)=>i[a]=s[l]),t.forEach((a,l)=>i[e.length+l]=s[o.length+l]);let c=o.length+t.length;return r.forEach((a,l)=>{Number.isNaN(a)&&(i[l]=++c)}),{ranks:i,key:ti(i)}}if(Number.isFinite(n)&&r.some((o,s)=>!Number.isFinite(o)&&Number.isFinite(e[s]))){let o=rd(e.map(s=>s/2-n/2),t);return{ranks:o,key:ti(o)}}return Mf([...r,...t])}function sd(e,t,n,r){let{alternative:o,mu:s}=r,i=e.length,c=t.length,a=n??(i<50&&c<50),l=od(e,t,s),[f,m,u,h]=xf(l,i),p,w;if(a)if(w="Wilcoxon rank sum exact test",!h)p=pf(()=>o==="less"?zr(f,i,c,!0,!1):o==="greater"?zr(f-1,i,c,!1,!1):Math.min(2*zr(f,i,c,!0,!1),2*zr(f-1,i,c,!1,!1),1));else{Fs(oi(l,i),"distribution");let N=si(l,i);o==="less"?p=or(N,f):o==="greater"?p=yr(N,f-.25):p=Math.min(2*or(N,f),2*yr(N,f-.25),1)}else{w=`Wilcoxon rank sum test${r.correct>=0?" with continuity correction":""}`;let N=h?Math.min(r.correct,0):r.correct,v=l.every(g=>g===l[0]),E=(m-Cs(m,o,N>=0))/u;p=v?1:gf(E,o,N>=1?Gp(E,i,c,N):0)}let d={method:w,alternative:o,statistic:f,statisticName:"W",pValue:p,nullValue:s,nullValueName:"location shift",warnings:r.warnings.list};if(!r.confInt)return d;let b=a?h?id(e,t,r):pf(()=>ad(e,t,r)):cd(e,t,r);return{...d,...b}}function pf(e){try{return e()}catch(t){throw t instanceof RangeError&&t.message.startsWith("wilcox:")?new RangeError(`${t.message}: use exact: false`):t}}function Tf(e,t,n){let r=[];for(let o of e)for(let s of t){let i=o-s;n&&Number.isNaN(i)||r.push(i)}return r.sort(li)}function ad(e,t,n){let r=e.length,o=t.length;return yf(Tf(e,t,!1),s=>zr(s,r,o,!0,!1),s=>wl(s,r,o,!0,!1),n,"difference in location")}function id(e,t,n){let r=e.length,o=t.length,s=i=>{let{ranks:c,key:a}=Rf(e,t,i),l=0;for(let f=0;f<r;f++)l+=c[f];return{stat:l-r*(r+1)/2,scores:c,key:a}};return Ef(Tf(e,t,!0),s,i=>si(i,r),i=>Fs(oi(i,r),"interval"),(i,c)=>yr(i,c-.25),or,{mean:r*o/2,sd:Math.sqrt(r*o*(r+o+1)/12)},r+o,n,"difference in location")}function cd(e,t,n){let{alternative:r,confLevel:o,tolRoot:s}=n,i=n.correct>=0,c=an(e)-Nt(t),a=Nt(e)-an(t);if(!Number.isFinite(c)||!Number.isFinite(a))throw new Error("cannot compute the confidence interval because the differences are not finite");let l=(h,p)=>{let[,w,d]=nd(e.map(b=>b-h),t);return(w-Cs(w,r,p))/d},f=(h,p)=>{let w=l(c,p)-h,d=l(a,p)-h;if(Number.isNaN(w)||Number.isNaN(d))throw new Error("cannot compute the confidence interval because all observations are tied");return w<=0?c:d>=0?a:(vf(s),Kt(b=>l(b,p)-h,c,a,{tol:s,fLower:w,fUpper:d}))},m=1-o,u;return r==="greater"?u=[f(-le(m),i),1/0]:r==="less"?u=[-1/0,f(le(m),i)]:u=[f(-le(m/2),i),f(le(m/2),i)],{confInt:u,confLevel:o,estimate:f(0,!1),estimateNames:"difference in location"}}var ld=["method","n"],Sf=["holm","hochberg","hommel","bonferroni","BH","BY","fdr","none"];function fd(e){if(Sf.includes(e))return e;let t=Sf.filter(n=>n.startsWith(e));if(e.length===0||t.length!==1)throw new RangeError(`invalid method "${e}"`);return t[0]}function Kr(e,t={}){B(t,ld,"pAdjust");let n=typeof t=="string"?{method:t}:t,r=fd(n.method??"holm"),o=Array.from(e),s=[];for(let m=0;m<o.length;m++)Number.isNaN(o[m])||s.push(m);let i=s.length,c=n.n??i;if(!(c>=i))throw new RangeError("n must be at least the number of non-NaN p-values");if(c===1/0)throw new RangeError("n must be finite");if(c<=1||i===0)return o;let a=s.map(m=>o[m]),l=qf(a,r,c),f=o.slice();return s.forEach((m,u)=>f[m]=l[u]),f}function Os(e,t){let n=e.map((r,o)=>o);return n.sort((r,o)=>(t?e[o]-e[r]:e[r]-e[o])||r-o),n}function qf(e,t,n){let r=e.length,o=new Array(r);switch(t){case"none":return e.slice();case"bonferroni":return e.map(s=>Math.min(1,n*s));case"holm":{let s=Os(e,!1),i=-1/0;return s.forEach((c,a)=>{i=Math.max(i,(n-a)*e[c]),o[c]=Math.min(1,i)}),o}case"hochberg":{let s=Os(e,!0),i=1/0;return s.forEach((c,a)=>{let l=r-a;i=Math.min(i,(n-l+1)*e[c]),o[c]=Math.min(1,i)}),o}case"BH":case"fdr":case"BY":{let s=1;if(t==="BY"){let m=Pn(),u=Math.floor(n);if(u<=1e6){for(let h=1;h<=u;h++)pn(m,1/h,u);s=Vn(m)}else{let h=u*u;s=Math.log(u)+.5772156649015329+1/(2*u)-1/(12*h)+1/(120*h*h)}}let i=s*n,c=Number.isFinite(i)?1:1024,a=Number.isFinite(i)?i:s*(n/1024),l=Os(e,!0),f=1/0;return l.forEach((m,u)=>{let h=r-u;f=Math.min(f,a/h*e[m]*c),o[m]=Math.min(1,f)}),o}case"hommel":return n===2?qf(e,"hochberg",n):md(e,n)}}var Af=2**16;function md(e,t){if(!Number.isInteger(t))throw new RangeError("n must be an integer for the hommel method");if(t>Af)throw new RangeError(`n must be at most ${Af} for the hommel method (O(n^2) work)`);let n=e.length,r=[...e,...new Array(t-n).fill(1)],o=Os(r,!1),s=o.map(a=>r[a]),i=s.slice();for(let a=t;a>=2;a--){let l=1/0;for(let f=1;f<=a;f++)l=Math.min(l,a*s[t-a+f-1]/f);for(let f=t-a;f<t;f++)i[f]=Math.max(i[f],l);for(let f=0;f<t-a;f++)i[f]=Math.max(i[f],Math.min(a*s[f],l))}let c=new Array(n);return o.forEach((a,l)=>{a<n&&(c[a]=i[l])}),c}var kf=["p","alternative","confLevel"],ud=1+1e-7;function mi(e,t,n){let r=e-1;return e===0||e-r===1&&Ot(t,-r)[1]===0?Dn(r,t,n,!1,!1):Dn(e,t,n,!1,!1)+hn(e,t,n,!1)}function hd(e,t,n){if(n===0)return e===0?1:0;if(n===1)return e===t?1:0;let r=t*n;if(e===r)return 1;let o=hn(e,t,n,!1)*ud,s=a=>hn(a,t,n,!1)<=o;if(e<r){let a=ss(s,Math.ceil(r),t),l=a>t?0:mi(a,t,n);return Math.min(1,Dn(e,t,n,!0,!1)+l)}let i=as(s,0,Math.floor(r)),c=i<0?0:Dn(i,t,n,!0,!1);return Math.min(1,mi(e,t,n)+c)}function pd(e,t,n={}){B(t,kf,"binomTest"),B(n,kf,"binomTest");let r,o;if(typeof e!="number"){if(e.length!==2)throw new RangeError("incorrect length of 'x'");if(t!==void 0&&typeof t!="number"&&(n=t),!vn(e[0])||!vn(e[1]))throw new RangeError("'x' must be nonnegative and integer");if(r=Math.round(e[0]),o=Math.round(e[0])+Math.round(e[1]),o===0||o===1/0)throw new RangeError("'n' must be a positive integer >= 'x'")}else{if(typeof t!="number")throw new TypeError("'n' is required when 'x' is a single number");if(!vn(e))throw new RangeError("'x' must be nonnegative and integer");if(r=Math.round(e),!(vn(t)&&Math.round(t)>=r&&t>0))throw new RangeError("'n' must be a positive integer >= 'x'");o=Math.round(t)}return ui(r,o,n)}function ui(e,t,n={}){let r=n.p??.5;if(!(r>=0&&r<=1))throw new RangeError("'p' must be a single number between 0 and 1");let o=Ze(n.alternative),s=n.confLevel??.95;_t(s);let i;o==="less"?i=Dn(e,t,r,!0,!1):o==="greater"?i=mi(e,t,r):i=hd(e,t,r);let c=f=>e===0?0:Xn(f,e,t-e+1,!0,!1),a=f=>e===t?1:f<1e-8?Xn(f,e+1,t-e,!1,!1):Xn(1-f,e+1,t-e,!0,!1),l;if(o==="less")l=[0,a(1-s)];else if(o==="greater")l=[c(1-s),1];else{let f=(1-s)/2;l=[c(f),a(f)]}return{method:"Exact binomial test",alternative:o,statistic:e,statisticName:"number of successes",parameter:t,parameterName:"number of trials",pValue:i,confInt:l,confLevel:s,estimate:e/t,estimateNames:"probability of success",nullValue:r,nullValueName:"probability of success",warnings:[]}}var dd=["T","r","alternative","confLevel"],gd=1+1e-7;function wd(e,t){if(t===0)return e===0?1:0;if(e===t)return 1;let n=is(e,t,!1)*gd,r=i=>is(i,t,!1)<=n,o=Math.floor(t);if(e<t){let i=ss(r,Math.max(e+1,o),1/0);return Math.min(1,Vr(e,t,!0,!1)+hi(i,t))}let s=as(r,0,Math.min(e-1,o));return Math.min(1,Vr(s,t,!0,!1)+hi(e,t))}var Lo=2**-1022;function hi(e,t){return e-(e-1)===1?Vr(e-1,t,!1,!1):Vr(e,t,!1,!1)+is(e,t,!1)}function Mr(e){let t=e<9332636185032189e-317?600:0,n=Math.floor(Math.log2(e*2**t));return[e*2**t*2**-n,n-t]}function bd(e,t,n,r){let o=n+t*r,s=e*t*n/o;if(Number.isFinite(s)&&Number.isFinite(o)&&o>=Lo&&(s!==0||e===0||t===0))return s;let i=e*t,c=1+t*(r/n);if(e===0||t===0)return 0;if(Number.isFinite(i)&&Number.isFinite(c))return i/c;let[a,l]=Mr(e),[f,m]=Mr(t),[u,h]=Mr(n),[p,w]=Mr(r),d=f*p,b=h-m-w,N=b>=0?u/(u+V(d,-b)):u/(V(u,b)+d);return V(a*f*N,l+m+Math.min(b,0))}function vd(e,t,n,r){let o=e/n,s=t/r,i=o/s;return t===0||Number.isFinite(o)&&Number.isFinite(s)&&s>=Lo&&(o===0||o>=Lo)?i:e===0?0:e/t*(r/n)}function Nd(e,t={}){B(t,dd,"poissonTest");let n=typeof e=="number"?[e]:Array.from(e);if(n.length<1||n.length>2)throw new RangeError("incorrect length of 'x'");if(!n.every(vn))throw new RangeError("'x' must be finite, nonnegative, and integer");let r=n.map(Math.round),o=r.length,s=t.T??1,i=typeof s=="number"?new Array(o).fill(s):Array.from(s);if(i.length!==o)throw new RangeError("'x' and 'T' have incompatible length");if(!i.every(b=>Number.isFinite(b)&&b>0))throw new RangeError("'T' must be positive");let c=t.r??1;if(!(Number.isFinite(c)&&c>=0))throw new RangeError("'r' must be a single positive number");let a=Ze(t.alternative),l=t.confLevel??.95;if(o===2?_t(l):cn(l),o===2){let[b,N]=r,[v,E]=i,g=b+N;if(g===1/0)throw new RangeError("'x' must be finite, nonnegative, and integer");let y=c*v,x=y+E,M=Number.isFinite(x)&&x>=Lo&&(y===0||y>=Lo)?y/x:1/(1+E/v/c),R=ui(Math.round(b),Math.round(b)+Math.round(N),{p:M,alternative:a,confLevel:l}),T=E/v,S=A=>{if(A===0)return 0;let C=A/(1-A);if(Number.isFinite(T)||C===1/0)return C*T;let[L,k]=Mr(C),[F,I]=Mr(v),[O,q]=Mr(E);return V(L*O/F,k+q-I)};return{method:"Comparison of Poisson rates",alternative:a,statistic:b,statisticName:"count1",parameter:bd(g,c,v,E),parameterName:"expected count1",pValue:R.pValue,confInt:[S(R.confInt[0]),S(R.confInt[1])],confLevel:l,estimate:vd(b,N,v,E),estimateNames:"rate ratio",nullValue:c,nullValueName:"rate ratio",warnings:[]}}let f=r[0],m=i[0],u=c*m,h;a==="less"?h=Vr(f,u,!0,!1):a==="greater"?h=hi(f,u):h=wd(f,u);let p=b=>f===0?0:os(b,f,1,!0,!1),w=b=>b<1e-8?os(b,f+1,1,!1,!1):os(1-b,f+1,1,!0,!1),d;if(a==="less")d=[0,w(1-l)];else if(a==="greater")d=[p(1-l),1/0];else{let b=(1-l)/2;d=[p(b),w(b)]}return{method:"Exact Poisson test",alternative:a,statistic:f,statisticName:"number of events",parameter:m,parameterName:"time base",pValue:h,confInt:[d[0]/m,d[1]/m],confLevel:l,estimate:f/m,estimateNames:"event rate",nullValue:c,nullValueName:"event rate",warnings:[]}}var If=["p","alternative","confLevel","correct"],yd="Chi-squared approximation may be incorrect";function pi(e,t,n={}){ct(t)&&B(t,If,"propTest"),B(n,If,"propTest");let r,o,s=n;if(typeof e!="number"&&e.length>0&&Array.isArray(e[0])){let S=e;if(S.some(A=>A.length!==2))throw new RangeError("'x' must have 2 columns");r=S.map(A=>A[0]),o=S.map(A=>A[0]+A[1]),t!==void 0&&typeof t=="object"&&!Array.isArray(t)&&(s=t)}else{if(t===void 0||typeof t=="object"&&!Array.isArray(t))throw new RangeError("'n' must be given");r=typeof e=="number"?[e]:Array.from(e),o=typeof t=="number"?[t]:Array.from(t)}if(r.length!==o.length)throw new RangeError("'x' and 'n' must have the same length");let i=r.length,c=[];for(let S=0;S<i;S++)!Number.isNaN(r[S])&&!Number.isNaN(o[S])&&c.push(S);let a=c.length;if(a<1)throw new RangeError("not enough data");if(r=c.map(S=>r[S]),o=c.map(S=>o[S]),o.some(S=>!(S>0)))throw new RangeError("elements of 'n' must be positive");if(o.some(S=>S===1/0))throw new RangeError("elements of 'n' must be finite");if(r.some(S=>!(S>=0)))throw new RangeError("elements of 'x' must be nonnegative");if(r.some((S,A)=>S>o[A]))throw new RangeError("elements of 'x' must not be greater than those of 'n'");let l;if(s.p!==void 0){let S=typeof s.p=="number"?[s.p]:Array.from(s.p);if(S.length!==i)throw new RangeError("'p' must have the same length as 'x' and 'n'");if(l=c.map(A=>S[A]),l.some(A=>!(A>0&&A<1)))throw new RangeError("elements of 'p' must be in (0,1)")}let f=l!==void 0;a===1&&l===void 0&&(l=[.5]);let m=s.confLevel??.95;_t(m);let u=Ze(s.alternative);(a>2||a===2&&f)&&(u="two.sided");let h=r.map((S,A)=>S/o[A]),p=a===2?h[0]-h[1]:0,w=a===2?1/o[0]+1/o[1]:0,d=(s.correct??!0)&&a<=2?.5:0,b=!1;if(d>0&&a===1){let S=Math.abs(r[0]-o[0]*l[0]);S<=d&&([d,b]=[S,!0])}else if(d>0&&a===2&&!f){let S=Math.abs(p)/w;S<=d&&([d,b]=[S,!0])}let N=Md(r,o),v=o.some((S,A)=>{let C=l?.[A]??N;return S*C<5||S*(1-C)<5}),E=0;if(!b)for(let S=0;S<a;S++){let A=l?.[S]??N,C=o[S]*A,L=o[S]*(1-A);E+=Vt(Math.abs(r[S]-C)-d,C),E+=Vt(Math.abs(o[S]-r[S]-L)-d,L)}let g=l!==void 0?a:a-1,y;if(u==="two.sided")y=wt(E,g/2,2,!1,!1);else{let S=Math.sign(a===1?h[0]-l[0]:p);y=se(S*Math.sqrt(E),{lowerTail:u==="less"})}let x;if(a===1||a===2&&!f){let S=(1-m)/2,A=u==="two.sided"&&S<1e-8?le(S,{lowerTail:!1}):le(u==="two.sided"?(1+m)/2:m);if(a===1){let C=o[0],L=A*A,k=($,_)=>($+L/(2*C)+_*A*Math.sqrt($*(1-$)/C+L/(4*C*C)))/(1+L/C),F=h[0]+d/C,I=h[0]-d/C,O=Math.min(1,Math.max(0,F>=1?1:k(F,1))),q=Math.min(1,Math.max(0,I<=0?0:k(I,-1)));x=u==="two.sided"?[q,O]:u==="greater"?[q,1]:[0,O]}else{let C=Math.sqrt(h.reduce((I,O,q)=>I+O*(1-O)/o[q],0)),L=A*C+d*w,k=Math.max(-1,p-L),F=Math.min(1,p+L);x=u==="two.sided"?[k,F]:u==="greater"?[k,1]:[-1,F]}}let M=a===1?"p":c.map(S=>`prop ${S+1}`),T={method:`${a===1?"1-sample proportions test":`${a}-sample test for ${f?"given proportions":"equality of proportions"}`} ${d!==0?"with":"without"} continuity correction`,alternative:u,statistic:E,statisticName:"X-squared",parameter:g,parameterName:"df",pValue:y,estimate:a===1?h[0]:h,estimateNames:M,warnings:v?[yd]:[]};return l!==void 0&&(T.nullValue=a===1?l[0]:l,T.nullValueName=M),x!==void 0&&(T.confInt=x,T.confLevel=m),Q("propTest",T)}function Md(e,t){let n=t.reduce((o,s)=>o+s,0);if(n<1/0)return e.reduce((o,s)=>o+s,0)/n;let r=Math.ceil(Math.log2(t.length));return e.reduce((o,s)=>o+V(s,-r),0)/t.reduce((o,s)=>o+V(s,-r),0)}var Ed=["method","confLevel","sides"],xd=["wilson","wald","wilsoncc","agresti-coull","jeffreys","clopper-pearson","midp"],Rd=["two.sided","left","right"];function Co(e,t){let n=Se(t,Rd),r=e??.95;if(!(typeof r=="number"&&r>0&&r<1))throw new RangeError("'confLevel' must be a single number in (0, 1)");let o=n==="two.sided"?r:1-2*(1-r);if(!(o>=0))throw new RangeError("a one-sided 'confLevel' must be at least 0.5");let s=n!=="two.sided"&&1-r<1e-8?2*(1-r):1-o;return{...di(s,o),sides:n}}function di(e,t=1-e){let n=e/2<1e-8,r=n?le(e/2,{lowerTail:!1}):le(1-e/2),o=n?On(e,1,{lowerTail:!1}):On(t,1);return{sides:"two.sided",level:t,alpha:e,kappa:r,chisq:o}}function Ur(e,t){if(typeof e!="number")throw new TypeError(`'${t}' must be a number`);if(!vn(e))throw new RangeError(`'${t}' must be a nonnegative integer`);return Math.round(e)+0}function Bn(e,t,n,r){let o=Ur(e,n),s=Ur(t,r);if(s===0)throw new RangeError(`'${r}' must be positive`);if(s>2**53)throw new RangeError(`'${r}' must be at most 2^53`);if(o>s)throw new RangeError(`'${n}' must not be greater than '${r}'`);return[o,s]}function Ps(e,t,n){let r=e/t,o=1-r,s=n*n,i=(e+s/2)/(t+s),c=n*Math.sqrt(t)/(t+s)*Math.sqrt(r*o+s/(4*t)),a=i+c,l=i-c;return e===0?l=0:l<i/2&&(l=r*(e/(t+s))/a),[l,e===t?1:a]}function Ds(e,t,n,r){return r?e/2<1e-8?Xn(e/2,t,n,!1,!1):Xn(1-e/2,t,n,!0,!1):Xn(e/2,t,n,!0,!1)}function _f(e,t,n,r){let o=n/2,s=r?f=>{let m=Math.exp(f);return .5*hn(e,t,m,!1)+Dn(e-1,t,m,!0,!1)-o}:f=>{let m=Math.exp(f);return .5*hn(e,t,m,!1)+Dn(e,t,m,!1,!1)-o},i=r?-1:1,c=Math.log(e/t),a=i*s(c);if(a===0)return e/t;if(a>0){let f=c,m=a,u=1;for(;;){let h=c-u;if(h<-745)return 0;let p=i*s(h);if(p<=0)return Math.exp(p===0?h:Kt(w=>i*s(w),h,f,{tol:2**-60,fLower:p,fUpper:m}));f=h,m=p,u*=2}}let l=i*s(0);return l<=0?1:Math.exp(Kt(f=>i*s(f),c,0,{tol:2**-60,fLower:a,fUpper:l}))}function gi(e,t,n={}){B(n,Ed,"binomCI");let r=Se(n.method,xd),o=Co(n.confLevel,n.sides),[s,i]=Bn(e,t,"x","n"),{alpha:c,kappa:a}=o,l=s/i,f=1-l,m,u;switch(r){case"wald":{let h=a*Math.sqrt(l*f/i);[m,u]=[l-h,l+h];break}case"wilson":[m,u]=Ps(s,i,a);break;case"wilsoncc":{let h=a*a,p=2*(i+h),w=2*s+h-1,d=a*Math.sqrt(h-2-1/i+4*l*(i*f+1));m=l===0?0:w-d<w/2?(2*s-1)/(2*i)*(2*s-1)/(w+d):(w-d)/p,u=l===1?1:(2*s+h+1+a*Math.sqrt(h+2-1/i+4*l*(i*f-1)))/p;break}case"agresti-coull":{let h=a*a,p=i+h,w=(s+h/2)/p,d=a*Math.sqrt(w*(1-w)/p);[m,u]=[w-d,w+d];break}case"jeffreys":m=s===0?0:Ds(c,s+.5,i-s+.5,!1),u=s===i?1:Ds(c,s+.5,i-s+.5,!0);break;case"clopper-pearson":m=s===0?0:Ds(c,s,i-s+1,!1),u=s===i?1:Ds(c,s+1,i-s,!0);break;case"midp":m=s===0?0:s===i?Math.exp(Math.log(c)/i):_f(s,i,c,!1),u=s===i?1:s===0?-Math.expm1(Math.log(c)/i):_f(s,i,c,!0);break}return m=Math.max(0,m),u=Math.min(1,u),o.sides==="left"?u=1:o.sides==="right"&&(m=0),Q("binomCI",{estimate:l,confInt:[m,u],confLevel:n.confLevel??.95,method:r,sides:o.sides,warnings:[]})}function Fo(e){return e===1/0?1:e/(1+e)}function Oo(e,t){return e===0?0:t>0?e*e/t:1/0}function Td(e,t,n,r){return e/t+n/r>1}function Sd(e,t,n,r){return $s(e,t,n,r,0)}function vi(e,t,n,r,o){if(Td(e,t,n,r)){let[p,w,d,b]=vi(t-e,t,r-n,r,-o);return[w,p,b,d]}let s=o<=0,[i,c,a,l]=s?[e,t,n,r]:[n,r,e,t],f=Math.abs(o),m=1-f,u=0;if(m>0){let p=b=>{let N=0;return i>0&&(N+=i/b),i<c&&(N-=(c-i)/(1-b)),a>0&&(N+=a/(b+f)),a<l&&(N-=(l-a)/(m-b)),N},w=b=>{let N=0;return i>0&&(N-=i/(b*b)),i<c&&(N-=(c-i)/((1-b)*(1-b))),a>0&&(N-=a/((b+f)*(b+f))),a<l&&(N-=(l-a)/((m-b)*(m-b))),N};if(p(0)<=0)u=0;else if(p(m)>=0)u=m;else{let b=0,N=m,v=Ad(e,t,n,r,o);u=s?v:v-o,u>b&&u<N||(u=N/2);for(let E=0;E<2e3;E++){let g=p(u);if(g===0)break;g>0?b=u:N=u;let y=u-g/w(u);if(y>b&&y<N||(y=b===0?N/2:N>2*b?Math.sqrt(b*N):b+(N-b)/2),!(y>b&&y<N))break;let x=Math.abs(y-u);if(u=y,x<=2**-53*u)break}}}let h=[u,1-u,Math.min(1,u+f),Math.max(0,m-u)];return s?h:[h[2],h[3],h[0],h[1]]}function Ad(e,t,n,r,o){let s=e/t,i=n/r,c=r/t,a=1+c,l=-(1+c+s+c*i+o*(c+2)),f=o*o+o*(2*s+c+1)+s+c*i,m=-s*o*(1+o),u=l/(3*a),h=u*u*u-l*f/(6*a*a)+m/(2*a),p=Math.sign(h)*Math.sqrt(Math.max(0,u*u-f/(3*a)));if(p===0)return-u;let w=(Math.PI+Math.acos(Math.max(-1,Math.min(1,h/(p*p*p)))))/3;return 2*p*Math.cos(w)-u}function Ni(e,t,n,r,o,s){if(s==="wald")return e/t*((t-e)/t)/t+n/r*((r-n)/r)/r;let[i,c,a,l]=vi(e,t,n,r,o),f=i*c/t+a*l/r;if(s==="mee")return f;let m=t+r;return f*m/(m-1)}function qd(e,t,n,r,o,s,i){let c=Le(Le(ft([e,0],t),ft([n,0],r)),[o,0]),[a,l,f,m]=vi(e,t,n,r,o),u=he(ft(jt(a,l),t),ft(jt(f,m),r));return s==="mn"&&(u=ft(Et(u,t+r),t+r-1)),Le(_e(c,c),Et(u,i))[0]}function $s(e,t,n,r,o){return Le(Le(ft([e,0],t),ft([n,0],r)),[o,0])[0]}function kd(e,t,n,r,o,s){return Oo($s(e,t,n,r,o),Ni(e,t,n,r,o,s))}var Vs=new Float64Array(1),wi=new BigInt64Array(Vs.buffer);function Lf(e){return Vs[0]=Math.abs(e),e<0?-wi[0]:wi[0]}function sr(e){return wi[0]=e<0n?-e:e,e<0n?-Vs[0]:Vs[0]}function bi(e,t,n,r,o){let s=e(t);if(s===0||Number.isNaN(s))return t;let i=s<0===o,c=Lf(i?r:n),a=Lf(t),l=s,f=a,m=s,u=1n;for(;;){if(f=i?a+u:a-u,(i?f>=c:f<=c)&&(f=c),m=e(sr(f)),m===0)return sr(f);if(m<0!=l<0||f===c)break;if(a=f,l=m,u*=2n,u>1n<<40n)return sr(a)}if(m<0==l<0||Number.isNaN(m))return sr(a);for(;f-a>1n||a-f>1n;){let h=(a+f)/2n,p=e(sr(h));if(p===0)return sr(h);p<0==l<0?(a=h,l=p):(f=h,m=p)}return Math.abs(m)<Math.abs(l)?sr(f):sr(a)}function Ff(e,t,n,r,o,s){let i=Sd(e,t,n,r);if(o===0)return[i,i];let c=Fo(o),a=p=>(p===i?0:Fo(kd(e,t,n,r,p,s)))-c,l=(p,w,d,b)=>{if(d===0)return p;if(b===0)return w;let N=Kt(a,p,w,{tol:1e-300,fLower:d,fUpper:b});return s==="wald"?N:bi(v=>qd(e,t,n,r,v,s,o),N,p,w,d<0)},f=a(-1),m=a(1),u=i<=-1||f<=0?-1:l(-1,i,f,-c),h=i>=1||m<=0?1:l(i,1,-c,m);return[u,h]}function Cf(e,t,n){let r=t,o=e(t);if(o>=0)return Math.exp(t);let s=1;for(;;){let i=t+n*s;if(Math.abs(i)>745)return n<0?0:1/0;let c=e(i);if(c>=0){if(c===0)return Math.exp(i);let a=n>0?Kt(e,r,i,{tol:2**-60,fLower:o,fUpper:c}):Kt(e,i,r,{tol:2**-60,fLower:c,fUpper:o});return Math.exp(a)}r=i,o=c,s*=2}}function Id(e,t,n,r,o){let s=t+r,i=o*(t+n)+e+r,c=o*(e+n),a=Math.sqrt(Math.max(0,i*i-4*s*c)),l=Math.min(1,o,c===0?0:2*c/(i+a));return[l,Math.min(1,l/o)]}function _d(e,t,n,r,o){let[s,i]=Id(e,t,n,r,o);if(!(o>0&&o<1/0))return[s,1-s,i,1-i];let c=s<=.5&&i<=.5?0:o*(1-i)<=1-s?2:3,a=c===0?i:c===2?1-i:1-s,l=y=>c===0?[y,0]:c===2?Ot(1,-y):ft(Ot(1,-y),o),f=c===3?1/o:1,m=c===0?Math.min(1,1/o):1,u=c===2?o>1?ft(Ot(o,-1),o)[0]:0:c===3&&o<1?Ot(1,-o)[0]:0,h=y=>{let x=l(y),M=Et(x,o);return[M[0],Le([1,0],M)[0],x[0],Le([1,0],x)[0]]},p=e+n,w=c<2?1:-1,d=y=>{let[,x,M,R]=h(y),T=0,S=0;return p>0&&(T+=M>0?p/M:1/0,S-=p/(M*M)),e<t&&(T-=x>0?o*(t-e)/x:1/0,S-=o*(t-e)/x*(o/x)),n<r&&(T-=R>0?(r-n)/R:1/0,S-=(r-n)/(R*R)),[w*T,S*f]},b;if(!(m>u))b=u;else if(!(d(u)[0]>0))b=u;else if(!(d(m)[0]<0))b=m;else{let y=u,x=m;b=a,b>y&&b<x||(b=y+(x-y)/2);for(let M=0;M<2e3;M++){let[R,T]=d(b);if(R===0)break;R>0?y=b:x=b;let S=b-R/T;if(S>y&&S<x||(S=y<=0?x/2:x>2*y?Math.sqrt(y*x):y+(x-y)/2),!(S>y&&S<x))break;let A=Math.abs(S-b);if(b=S,A<=2**-53*b)break}}let[N,v,E,g]=h(b);return[Math.min(1,Math.max(0,N)),Math.min(1,Math.max(0,v)),Math.min(1,Math.max(0,E)),Math.min(1,Math.max(0,g))]}function Ld(e,t,n,r,o){let[s,i,c,a]=_d(e,t,n,r,o),l=(f,m,u,h)=>u>.5?m*h-(m-f):f-m*u;return Oo(l(e,t,s,i),t*s*i)+Oo(l(n,r,c,a),r*c*a)}function Of(e,t,n,r,o){let s=e/t/(n/r);return Pf(s,o,i=>Ld(e,t,n,r,i))}function Cd(e,t,n,r,o){let s=e+n,i=r*(o-1),c=t*o+r-s*(o-1),a=s===0?0:Math.min(1,2*s/(c+Math.sqrt(Math.max(0,c*c+4*i*s))));return[o===1/0?a>0?1:0:Math.min(1,a*o/(1+a*(o-1))),a]}function Fd(e,t,n,r,o){let[s,i]=Cd(e,t,n,r,o),c=e-t*s;return Oo(c,t*s*(1-s))+Oo(c,r*i*(1-i))}function Df(e,t,n,r,o){let s=e*(r-n)/((t-e)*n);return Pf(s,o,i=>Fd(e,t,n,r,i))}function Pf(e,t,n){if(t===0)return[e,e];let r=Fo(t),o=l=>n(l)-t,s=l=>l>0&&l<e?bi(o,l,Number.MIN_VALUE,e,!1):l,i=l=>l>e&&l<1/0?bi(o,l,e,Number.MAX_VALUE,!0):l;if(e===0||e===1/0){let l=u=>Fo(n(u))-r,f=l(1),m=f===0?1:rr(l,e===0?f>0:f<0);return e===0?[0,i(m)]:[s(m),1/0]}let c=Math.log(e),a=l=>(l===c?0:Fo(n(Math.exp(l))))-r;return[s(Cf(a,c,-1)),i(Cf(a,c,1))]}var Od=["method","confLevel","sides"],Dd=["method","confLevel","sides"],Pd=["method","confLevel"],Vd=["ac","wald","score","mn","mee"];function yi(e,t,n,r,o,s){let{kappa:i}=s,c=e/t,a=n/r,l=c-a,f,m;switch(o){case"wald":{let u=i*Math.sqrt(c*(1-c)/t+a*(1-a)/r);[f,m]=[Math.max(-1,l-u),Math.min(1,l+u)];break}case"ac":{let u=(e+1)/(t+2),h=(n+1)/(r+2),p=u-h,w=i*Math.sqrt(u*(1-u)/(t+2)+h*(1-h)/(r+2));[f,m]=[Math.max(-1,p-w),Math.min(1,p+w)];break}case"score":{let[u,h]=Ps(e,t,i),[p,w]=Ps(n,r,i);f=l-i*Math.sqrt(u*(1-u)/t+w*(1-w)/r),m=l+i*Math.sqrt(h*(1-h)/t+p*(1-p)/r);break}case"mn":case"mee":[f,m]=Ff(e,t,n,r,s.chisq,o);break}return[Math.min(f,m),Math.max(f,m)]}function $d(e,t,n,r,o={}){B(o,Od,"binomDiffCI");let s=Se(o.method,Vd),i=Co(o.confLevel,o.sides),[c,a]=Bn(e,t,"x1","n1"),[l,f]=Bn(n,r,"x2","n2"),m=c/a-l/f,[u,h]=yi(c,a,l,f,s,i);return i.sides==="left"?h=1:i.sides==="right"&&(u=-1),Q("binomDiffCI",{estimate:m,confInt:[u,h],confLevel:o.confLevel??.95,method:s,sides:i.sides,warnings:[]})}var zd=["katz.log","koopman"];function zs(e,t,n,r,o){let s=e/t/(n/r),i=o*Math.sqrt(1/e-1/t+1/n-1/r);return[s*Math.exp(-i),s*Math.exp(i)]}function jd(e,t,n,r,o={}){B(o,Dd,"binomRatioCI");let s=Se(o.method,zd),i=Co(o.confLevel,o.sides),[c,a]=Bn(e,t,"x1","n1"),[l,f]=Bn(n,r,"x2","n2"),m=i.kappa,u=c===0?0:c/a/(l/f),h,p;return c===0&&l===0?[h,p]=[0,1/0]:s==="katz.log"?c===0?[h,p]=[0,zs(.5,a,l,f,m)[1]]:l===0?[h,p]=[zs(c,a,.5,f,m)[0],1/0]:c===a&&l===f?[h,p]=zs(a-.5,a,f-.5,f,m):[h,p]=zs(c,a,l,f,m):[h,p]=Of(c,a,l,f,i.chisq),i.sides==="left"?p=1/0:i.sides==="right"&&(h=0),Q("binomRatioCI",{estimate:u,confInt:[h,p],confLevel:o.confLevel??.95,method:s,sides:i.sides,warnings:[]})}var Wd=["midp","fisher","wald","score"],Bd={midp:"median-unbiased estimate & mid-p exact CI",fisher:"Conditional MLE & exact CI from 'fisher.test'",wald:"Unconditional MLE & normal approximation (Wald) CI",score:"Unconditional MLE & score CI"},Kd=2**23,Ud=2**20;function Vf(e,t,n){let r=[0,0];for(let o=Math.max(0,t);o<=Math.min(n,e.length-1);o++)e[o]!==0&&(r=he(r,[e[o],0]));return r[0]}function Hd(e,t={}){B(t,Pd,"oddsRatio");let n=Se(t.method,Wd);if(e.length!==2||e.some(S=>S.length!==2))throw new TypeError("'table' must be a 2 x 2 table");let r=t.confLevel??.95,{alpha:o,kappa:s,chisq:i}=Co(r,void 0),c=e.map((S,A)=>Array.from(S,(C,L)=>Ur(C,`table[${A}][${L}]`))),[[a,l],[f,m]]=c,u=a+l+f+m;if(u===0)throw new RangeError("all entries of the table are zero");if(!(u<=2**53))throw new RangeError("the total of the table must be at most 2^53");let h=Math.max(0,a-m),p=a+Math.min(l,f),w=p-h+1;if(n==="midp"||n==="fisher"){if(w>Kd)throw new RangeError(`the exact distribution of the 2 x 2 table has ${w} values, more than 2^23: use method "wald" or "score"`);if(n==="fisher"&&Math.max(a,l,f,m)>2147483647)throw new RangeError(`'table' has entries too large for the conditional MLE (above 2^31 - 1): use method "wald" or "score"`)}let d,b,N;try{d=sn(a-1,a+f,l+m,a+l,!0,!1)+.5*bn(a,a+f,l+m,a+l,!1),b=sn(a,a+f,l+m,a+l,!1,!1)+.5*bn(a,a+f,l+m,a+l,!1),N=w<=Ud?Ja(new Br(a+f,l+m,a+l),a,1):ff(a,a+f,l+m,a+l)}catch(S){throw S instanceof RangeError?new RangeError(`the exact p-values of the table are beyond reach: ${S.message}`):S}let v=2*Math.min(d,b),E=[a+l,f+m],g=[a+f,l+m],y=0,x=!1;for(let S=0;S<2;S++)for(let A=0;A<2;A++){let C=E[A]*g[S]/u;C<5&&(x=!0);let L=Math.abs(c[A][S]-C);y+=L*L/C}let M=wt(y,.5,2,!1,!1),R,T;switch(n){case"wald":{R=a*m/(l*f);let S=s*Math.sqrt(1/a+1/l+1/f+1/m);T=[Math.exp(Math.log(R)-S),Math.exp(Math.log(R)+S)];break}case"fisher":{let S=ot(()=>Io(c,{confLevel:r}));R=S.estimate,T=S.confInt;break}case"score":{R=a*m/(l*f),T=(a===0||m===0)&&(l===0||f===0)?[0,1/0]:Df(a,a+l,f,f+m,i);break}case"midp":{let S=new Br(a+f,l+m,a+l),A=F=>{if(F===1)return[d,b];let I=S.density(F),O=a-h,q=.5*I[O];return[Vf(I,0,O-1)+q,Vf(I,O+1,I.length-1)+q]},C=(F,I)=>{let O=F(1);return O===0?1:rr(F,I?O>0:O<0)};R=a===h?0:a===p?1/0:C(F=>A(F)[0]-.5,!1);let L=a===h?0:C(F=>A(F)[1]-o/2,!0),k=a===p?1/0:C(F=>A(F)[0]-o/2,!1);T=[L,k];break}}return Q("oddsRatio",{estimate:R,confInt:T,confLevel:r,method:Bd[n],pValue:{midp:v,fisher:N,chiSquare:M},warnings:x?["Chi-squared approximation may be incorrect"]:[]})}var Yd=["epsilon","paired","varEqual","confLevel","alpha"],Gd=["margin","method","alpha"];function $f(e,t){if(!(typeof e=="number"&&e>0&&e<.5))throw new RangeError(`'${t}' must be a single number in (0, 0.5)`)}function Xd(e,t,n={}){B(n,Yd,"tost");let r=n.epsilon??1;if(!(typeof r=="number"&&r>0&&r<1/0))throw new RangeError("'epsilon' must be a positive number");let o,s;if(n.alpha!==void 0)$f(n.alpha,"alpha"),o=1-n.alpha,s=n.alpha;else{if(o=n.confLevel??.95,_t(o),!(o>.5))throw new RangeError("'confLevel' must be greater than 0.5");s=1-o}if(t===void 0)throw new TypeError("'y' is required");let i=n.paired??!1,c=n.varEqual??!1,a=ot(()=>Nn(e,t,{paired:i,varEqual:c})),l=a.stdErr,f=a.parameter,m=typeof a.estimate=="number"?a.estimate:a.estimate[0]-a.estimate[1],u=dt((r-Math.abs(m))/l,f,!1,!1),h=s<1e-8?gt(s,f,!1,!1):gt(o,f,!0,!1),p=[m-h*l,m+h*l];return Q("tost",{method:a.method.replace("t-test","TOST"),parameter:f,parameterName:"df",pValue:u,result:p[0]>-r&&p[1]<r?"rejected":"not rejected",confInt:p,confLevel:o,estimate:a.estimate,estimateNames:i?"mean of the differences":a.estimateNames,epsilon:r,seDiff:l,warnings:[]})}var Qd=["mn","mee","wald"];function Zd(e,t,n,r,o={}){B(o,Gd,"binomDiffEquivalenceTest");let s=Se(o.method,Qd),i=o.margin;if(i===void 0)throw new TypeError("'margin' is required");if(!(typeof i=="number"&&i>0&&i<1))throw new RangeError("'margin' must be a single number in (0, 1)");let c=o.alpha??.05;$f(c,"alpha");let[a,l]=Bn(e,t,"x1","n1"),[f,m]=Bn(n,r,"x2","n2"),u=a/l-f/m,h=E=>$s(a,l,f,m,E)/Math.sqrt(Ni(a,l,f,m,E,s)),p=h(-i),w=h(i),d=[se(p,{lowerTail:!1}),se(w)],b=Math.max(d[0],d[1]),N=1-2*c,v=yi(a,l,f,m,s,di(2*c,N));return Q("binomDiffEquivalenceTest",{method:s,estimate:u,margin:i,alpha:c,statistic:[p,w],statisticName:"z",pValues:d,pValue:b,result:d[0]<c&&d[1]<c?"rejected":"not rejected",confInt:v,confLevel:N,warnings:[]})}var Ut=new WeakMap;function Ws(e){let t=(n,r)=>n.weights===void 0?Array.from(r):r.filter((o,s)=>n.weights[s]!==0);return qe(e.flatMap(n=>[t(n,n.fittedValues),t(n,n.residuals)])).k}function Bs(e,t){return e.map(n=>{if(n.rss>=Jd&&n.rss<1/0)return V(n.rss,-2*t);let r=0;return n.residuals.forEach((o,s)=>{let i=n.weights?.[s]??1;if(i===0)return;let c=V(o,-t);r+=i*c*c}),r})}var Jd=2**-1022;function Ei(e,t){return e.y.flatMap((n,r)=>(e.weights?.[r]??1)===0?[]:[[e.weights?.[r]??1,V(n,-t)]])}function Ks(e){let t=Math.floor(Math.log2(e.reduce((n,[r])=>Math.max(n,r),0)));return t-(t%2+2)%2}function Er(e,t,n=0){let r=i=>e.reduce((c,[a,l])=>c+V(a,-i)*l*l,0),o=n,s=r(o);return Number.isFinite(s)||(o=Ks(e),s=r(o)),!(V(t,-o)>=1e-24*s)}function zf(e){let t=Ws([e]),{pivot:n}=e.qr,r=[],o=new Map;for(let s=0;s<e.rank;s++){let i=e.assign[n[s]],c=o.get(i);c===void 0&&(c={term:i,df:0,ss:0},o.set(i,c),r.push(c)),c.df++,c.ss+=V(e.effects[s],-t)**2}return{terms:r,rdf:e.dfResidual,rss:Bs([e],t)[0],k:t}}var jf=(e,t,n)=>ht(e,t,n,!1,!1),xi="essentially perfect fit for \u20181\u2019: summary may be unreliable";function Ri(e,t,n){if(!(n>0))return!1;let r=e.length,o=0;for(let a of e)o+=a;o/=r;let s=0;for(let a of e)s+=(a-o)**2;let i=r>1?s/(r-1):0,c=t/n;return Number.isFinite(c)&&c<(o*o+i)*1e-30}var Ti=new WeakMap;function e0(e){let t=zf(e),n=t.terms.filter(o=>o.term!==0),r=0;return e.fittedValues.forEach((o,s)=>{let i=e.weights?.[s]??1;i!==0&&(r+=i*V(o,-t.k)**2)}),{names:n.map(o=>e.termLabels[o.term-1]),rows:n,rdf:t.rdf,rss:t.rss,k:t.k,perfect:t.rss<1e-10*r}}function Mi(e){let t=e0(e),{names:n,rdf:r,k:o,perfect:s}=t,{rows:i,rss:c}=t;if(r>0){let w=Ei(e,o);Er(w,c)&&(c=0,i=i.map(d=>Er(w,d.ss)?{...d,ss:0}:d))}let a=c/r,l=w=>V(w,2*o),f=i.map(({df:w,ss:d})=>{let b=d/w/a;return[w,l(d),l(d/w),b,jf(b,w,r)]});f.push([r,l(c),r===0?NaN:l(a),NaN,NaN]);let m=["Df","Sum Sq","Mean Sq","F value","Pr(>F)"],u=[...n,"Residuals"],h={colNames:m,rowNames:u,table:f,warnings:s?["ANOVA F-tests on an essentially perfect fit are unreliable"]:[]},p=i.map(({df:w,ss:d})=>[w,d]);return p.push([r,c]),Ut.set(h,{colNames:["Df","Sum Sq"],rowNames:u,table:p}),h}function t0(e){let t=e.map(f=>f.dfResidual),n=Ws(e),r=Bs(e,n),o=f=>V(f,2*n),s=0;t.forEach((f,m)=>{f<t[s]&&(s=m)});let i=r[s]/t[s],c=0;e[s].fittedValues.forEach((f,m)=>{let u=e[s].weights?.[m]??1;u!==0&&(c+=u*V(f,-n)**2)});let a=r[s]<1e-10*c,l=e.map((f,m)=>{if(m===0)return[t[0],o(r[0]),NaN,NaN,NaN,NaN];let u=t[m-1]-t[m],h=r[m-1]-r[m],[p,w]=u>0?[e[m-1],e[m]]:[e[m],e[m-1]],d=N=>N.filter((v,E)=>(p.weights?.[E]??1)!==0),b=u===0||!Es(d(p.x),d(w.x))?NaN:h/u/i;return b<0&&(b=NaN),[t[m],o(r[m]),u,o(h),b,Number.isNaN(b)?NaN:jf(b,Math.abs(u),t[s])]});return{colNames:["Res.Df","RSS","Df","Sum of Sq","F","Pr(>F)"],rowNames:e.map((f,m)=>String(m+1)),table:l,warnings:a?["ANOVA F-tests on an essentially perfect fit are unreliable"]:[]}}function js(...e){let t=[];for(let n of e)for(let r of n??[])t.includes(r)||t.push(r);return t}function Si(e,...t){let n=[e,...t];for(let c of n)if(typeof c!="object"||c===null||!Array.isArray(c.effects))throw new TypeError("anova() needs fits from lm() or aov() with a formula");if(n.length===1){let c=Mi(e),a={...c,warnings:js(e.warnings,c.warnings)},l=Ut.get(c);return l!==void 0&&Ut.set(a,l),Q("anova",a,e)}if(n.some(c=>c.rows.length!==e.rows.length))throw new Error("models were not all fitted to the same size of dataset");let r=(c,a)=>c===a||c!==void 0&&a!==void 0&&c.length===a.length&&c.every((l,f)=>Object.is(l,a[f]));if(n.some(c=>!r(c.rows,e.rows)))throw new Error("models were not all fitted to the same rows of the data");let o=n.filter(c=>c.response===e.response);if(o.some(c=>!r(c.y,e.y)))throw new Error("models were not all fitted to the same response values");if(o.some(c=>!r(c.weights,e.weights)))throw new Error("models were not all fitted with the same weights");let s=[];if(o.length<n.length){let c=[...new Set(n.filter(a=>a.response!==e.response).map(a=>a.response))];s.push(`models with response ${c.map(a=>`\u2018${a}\u2019`).join(", ")} removed because response differs from model 1`)}let i=o.length===1?Mi(e):t0(o);return i.warnings=js(...n.map(c=>c.warnings),s,i.warnings),Q("anova",i,...n)}function Ai(e){if(typeof e=="object"&&e!==null&&e.multistratum===!0){let i=e,c=js(i.warnings,Ti.get(i)),a={strata:i.strata.filter(f=>f.stratum!=="Error: (Intercept)").map(f=>({...f})),warnings:c},l=Ut.get(i);return Array.isArray(l)&&Ut.set(a,l.filter((f,m)=>i.strata[m].stratum!=="Error: (Intercept)")),Q("aovSummary",a,e)}if(typeof e!="object"||e===null||!Array.isArray(e.effects))throw new TypeError("aovSummary() needs a fit from aov() with a formula");let t=e,n=Mi(t),r=zf(t),o=Ri(t.fittedValues.map(i=>V(i,-r.k)),r.rss,r.rdf),s=js(t.warnings,n.warnings,o?[xi]:[]);if(t.dfResidual>0){let i={...n,warnings:s},c=Ut.get(n);return c!==void 0&&Ut.set(i,c),Q("aovSummary",i,e)}return Q("aovSummary",{colNames:n.colNames.slice(0,3),rowNames:n.rowNames.slice(0,-1),table:n.table.slice(0,-1).map(i=>i.slice(0,3)),warnings:s},e)}var n0=["pooledSd","mu","paired","ci","alternative","adjust"],r0=["pooledSd","mu","paired","ci","alternative"],o0=["generalized","partial","ci","alternative"],s0=["partial","ci","alternative"],a0=["partial","ci","alternative"];function Ii(e){if(e!==null&&!(e>0&&e<1))throw new RangeError("ci must be a single numeric value between (0, 1)")}var i0="Missing values detected. NAs dropped.",c0=`'y' is numeric but has only 2 unique values.
|
|
1
|
+
/*! rStats.js 1.11.0 | MIT License | includes code ported from SciPy, Boost.Math, statsmodels, LAPACK and others: see THIRD_PARTY_NOTICES.md at https://github.com/numero-uno-jpn/rStats.js */
|
|
2
|
+
import{a as tn,b as zn,c as nr,d as vr,e as xs,f as Fa}from"./chunks/chunk-JFZOKMZA.js";import{a as bn,c as sn,h as $r,i as dl,m as gl,n as zr,o as wl,s as hs,t as bl,u as bo,v as ps,w as ds,x as gs}from"./chunks/chunk-HCHZ6M34.js";import{b as xl}from"./chunks/chunk-OG5FTIWF.js";import{a as Wa,e as Ba}from"./chunks/chunk-W27XXELU.js";import{a as bs,b as tr,c as yo,d as Al,e as ql,f as vs,g as Ze,h as cn,i as vn,j as _t,k as Ll,l as sp,n as Ts,o as Ro,p as Ss}from"./chunks/chunk-P23KCD67.js";import"./chunks/chunk-SV6SLUVB.js";import{a as hl,d as us}from"./chunks/chunk-JWMVP7XO.js";import{d as nt}from"./chunks/chunk-HIPKYBZ3.js";import{a as Rs,e as Bt,f as Wl}from"./chunks/chunk-HPWRADJM.js";import"./chunks/chunk-Y2SS3GX2.js";import{a as Mo,b as Oa,c as Da,d as $l,e as Eo,f as Va,g as Wn,h as $a,i as xo,j as zl,k as za,l as ap,m as ja,n as jl,o as ip,p as cp,q as lp}from"./chunks/chunk-YPDFCKL4.js";import{b as It,c as On}from"./chunks/chunk-WHKUEZQI.js";import{a as kl,c as Il,d as _l,e as br,g as To}from"./chunks/chunk-Z6UI6ON2.js";import{b as La}from"./chunks/chunk-BGY7Y3P4.js";import{a as ct,b as Ns,c as ys}from"./chunks/chunk-XKQSKVCL.js";import{a as Dl,b as Pl,c as Es,d as Pa}from"./chunks/chunk-KICTA3JY.js";import{a as jn,b as Wr}from"./chunks/chunk-DMT423BG.js";import{d as ss,e as as,g as is,h as Vr,m as hn,n as Dn}from"./chunks/chunk-XLBSUUWY.js";import{d as ht,e as un,g as Rn,h as Gn,j as Xn}from"./chunks/chunk-BKH5GQNA.js";import{a as Vl}from"./chunks/chunk-BA32XHDA.js";import{c as Cl,h as Fl,j as Lt,m as Ca,n as Ol,o as Ms,p as jr}from"./chunks/chunk-46DELNLN.js";import{a as er,b as Tl,g as ws}from"./chunks/chunk-M4SLTLOX.js";import{a as _a,b as dt,c as gt,d as pl,e as Qt,f as Wt}from"./chunks/chunk-3K5KP3M2.js";import{i as wt,l as os,t as fl}from"./chunks/chunk-EV7ZQC6D.js";import{a as Tn,c as Sl}from"./chunks/chunk-BNHMQU7V.js";import{b as vl,c as Nl,d as yl,e as Ml,f as El,i as vo,l as op,n as Q,o as ot,p as Rt}from"./chunks/chunk-6VIOWV53.js";import{a as No,b as $n,c as Rl,d as Pt,e as vt}from"./chunks/chunk-JD6JHIVV.js";import{a as Nt,b as dn,c as an}from"./chunks/chunk-ZW5OCUIY.js";import"./chunks/chunk-W2D6IDBY.js";import{b as il,c as cl}from"./chunks/chunk-QY4OBLXK.js";import{h as ol,i as sl,k as al,t as ll}from"./chunks/chunk-OWOBNS75.js";import{f as Aa,r as Xt,s as se,t as le}from"./chunks/chunk-BZBPPRSH.js";import{A as Vt,B as ms,C as qe,D as Ia,c as qa,d as wo,e as Pn,f as pn,g as Vn,h as V,i as Ve,j as Qn,k as it,l as at,m as Zn,n as ml,p as cs,q as xt,r as ls,s as ul,t as fs,u as ka,v as De,w as bt,x as wn,y as Dt,z as Jn}from"./chunks/chunk-PF6LEBKO.js";import{b as Ot,c as jt,d as he,e as nl,f as Le,g as _e,h as Et,i as rt,j as ft,k as rl,l as B,m as Fn}from"./chunks/chunk-MJLK2RIQ.js";var Bl=["alternative","mu","paired","varEqual","confLevel"];function Nn(e,t,n={}){if(typeof e!="string"&&ct(t)&&B(t,Bl,"tTest"),B(n,Bl,"tTest"),typeof e=="string")return fp(e,t,n);let r;t!==void 0&&!ArrayBuffer.isView(t)&&!Array.isArray(t)?n=t:r=t;let o=Ze(n.alternative),s=n.mu??0,i=n.paired??!1,c=n.varEqual??!1,a=n.confLevel??.95;if(Number.isNaN(s))throw new RangeError("mu must be a number");if(cn(a),i&&r===void 0)throw new TypeError("y is required when paired is true");let l,f,m;if(i){let[A,C]=Jn(Array.from(e),Array.from(r));({d:l,k:m}=ls(A,C))}else{let A=qe(r===void 0?[Dt(Array.from(e))]:[Dt(Array.from(e)),Dt(Array.from(r))]);m=A.k,l=A.groups[0],f=A.groups[1]}let u=V(s,-m),h,p,w,d,b,N,v,E=l.length,g=f!==void 0&&c;if(E<1||!g&&E<2)throw new Error("not enough 'x' observations");let y=it(l),x=E>1?at(l):NaN;if(f===void 0){if(p=E-1,w=Math.sqrt(x/E),w===0||w<10*Number.EPSILON*Math.abs(y))throw new Error("data are essentially constant");h=(y-u)/w,N=i?"Paired t-test":"One Sample t-test",d=y,b=i?"mean difference":"mean of x",v=i?"mean difference":"mean"}else{let A=f.length;if(A<1||!c&&A<2)throw new Error("not enough 'y' observations");if(c&&E+A<3)throw new Error("not enough observations");let C=it(f),L=A>1?at(f):NaN;if(N=`${c?"":"Welch"} Two Sample t-test`,d=[y,C],b=["mean of x","mean of y"],v="difference in means",c){p=E+A-2;let k=0;E>1&&(k+=(E-1)*x),A>1&&(k+=(A-1)*L),k/=p,w=Math.sqrt(k*(1/E+1/A))}else{let k=x/E,F=L/A;w=Math.sqrt(Math.sqrt(k)**2+Math.sqrt(F)**2),p=fs([k,F],[E-1,A-1])}if(w===0||w<10*Number.EPSILON*Math.max(Math.abs(y),Math.abs(C)))throw new Error("data are essentially constant");h=(y-C-u)/w}let M=typeof d=="number"?d:d[0]-d[1],R=(A,C)=>{let L=u+(h+C*A)*w;return Number.isFinite(L)&&Math.max(Math.abs(u),Math.abs(h*w))<=16*(Math.abs(M)+A*w)?L:M+C*A*w},T,S;if(o==="less")T=dt(h,p,!0,!1),S=[-1/0,R(gt(a,p,!0,!1),1)];else if(o==="greater")T=dt(h,p,!1,!1),S=[R(gt(a,p,!0,!1),-1),1/0];else{T=2*dt(-Math.abs(h),p,!0,!1);let A=1-a,C=A/2<1e-8?gt(A/2,p,!1,!1):gt(1-A/2,p,!0,!1);S=[R(C,-1),R(C,1)]}return S=[V(S[0],m),V(S[1],m)],d=typeof d=="number"?V(d,m):d.map(A=>V(A,m)),w=V(w,m),{method:N,alternative:o,statistic:h,statisticName:"t",parameter:p,parameterName:"df",pValue:T,confInt:S,confLevel:a,estimate:d,estimateNames:b,nullValue:s,nullValueName:v,stdErr:w,stdError:w,warnings:[]}}function fp(e,t,n){if(n.paired!==void 0)throw new Error("cannot use 'paired' in formula method");let r=bs(e,t,!0);if(r.kind==="one")return Nn(r.x,n);if(r.kind==="paired")return Nn(r.x,r.y,{...n,paired:!0});let[o,s]=r.levels;return{...Nn(r.x,r.y,n),estimateNames:[`mean in group ${o}`,`mean in group ${s}`],nullValueName:`difference in means between group ${o} and group ${s}`}}var mp=new Set(["n","delta","sd","sigLevel","power","type","alternative","strict","tol"]),up=["n","p1","p2","sigLevel","power","alternative","strict","tol"],hp=["groups","n","betweenVar","withinVar","sigLevel","power"],Ul=Number.EPSILON**.25;function Se(e,t){if(e===void 0)return t[0];let n=t.find(o=>o===e);if(n!==void 0)return n;let r=t.filter(o=>o.startsWith(e));if(e.length===0||r.length!==1)throw new RangeError(`'arg' should be one of ${t.map(o=>`\u201C${o}\u201D`).join(", ")}`);return r[0]}var ye=e=>e==null;function ln(e,t,n){if(!ye(e)&&!t(e))throw new RangeError(n)}var So=e=>e>=0&&e<=1,As=e=>e>0&&e<1/0;function nn(e,t){if(e.filter(ye).length!==1)throw new RangeError(t)}function Ye(e,t,n,r={}){return _l(e,t,n,r).root}var Nr=(e,t,n,r)=>Qt(e,t,{ncp:n,lowerTail:r});function Ka(e,t){if(e!=null&&!(typeof e=="number"&&e>=0&&e<=1))throw new RangeError(t)}var Ua="'sig.level' must be numeric in [0, 1]";function pp(e={}){B(e,mp,"powerTTest");let{n:t,delta:n,power:r}=e,o=e.sd===void 0?1:e.sd,s=e.sigLevel===void 0?.05:e.sigLevel;nn([t,n,o,r,s],"exactly one of 'n', 'delta', 'sd', 'power', and 'sig.level' must be NULL"),Ka(s,Ua),ln(r,So,"'power' must be a probability"),ln(o,As,"'sd' must be a positive finite number"),ln(t,w=>w>=2,"number of observations in each group must be at least 2"),ln(n,w=>!Number.isNaN(w),"'delta' must not be NaN"),ln(e.tol,As,"'tol' must be a positive finite number");let i=Se(e.type,["two.sample","one.sample","paired"]),c=Se(e.alternative,["two.sided","one.sided"]),a=e.strict??!1,l=e.tol??Ul,f=i==="two.sample"?2:1,m=c==="two.sided"?2:1;m===2&&!ye(n)&&(n=Math.abs(n));let u=(w,d,b,N)=>{let v=Math.max(1e-7,w-1)*f,E=Wt(N/m,v,{lowerTail:!1}),g=Math.sqrt(w/f)*d/b;Number.isFinite(g)||(g=Math.sqrt(w/f)*(d/b));let y=Nr(E,v,g,!1);return a&&m===2?y+Nr(-E,v,g,!0):y};if(ye(r))r=u(t,n,o,s);else{let w=r;if(ye(t)){if(n===0)throw new RangeError("'n' cannot be determined when 'delta' is 0: the power does not depend on it");t=Ye(d=>u(d,n,o,s)-w,2,1e7,{tol:l,extendInt:"upX"})}else if(ye(o)){if(n===0)throw new RangeError("'sd' cannot be determined when 'delta' is 0: the power does not depend on it");let d=Math.abs(n);o=Ye(b=>u(t,n,b,s)-w,d*1e-7,d*1e7,{tol:l,extendInt:n>0?"downX":"upX"})}else ye(n)?n=Ye(d=>u(t,d,o,s)-w,o*1e-7,o*1e7,{tol:l,extendInt:"upX"}):s=Ye(d=>u(t,n,o,d)-w,1e-10,1-1e-10,{tol:l,extendInt:"yes"})}let h=i==="paired"?"n is number of *pairs*, sd is std.dev. of *differences* within pairs":i==="two.sample"?"n is number in *each* group":void 0,p={"two.sample":"Two-sample","one.sample":"One-sample",paired:"Paired"}[i]+" t test power calculation";return{n:t,delta:n,sd:o,sigLevel:s,power:r,alternative:c,...h===void 0?{}:{note:h},method:p,warnings:[]}}function dp(e={}){B(e,up,"powerPropTest");let{n:t,p1:n,p2:r,power:o}=e,s=e.sigLevel===void 0?.05:e.sigLevel;nn([t,n,r,o,s],"exactly one of 'n', 'p1', 'p2', 'power', and 'sig.level' must be NULL"),Ka(s,Ua),ln(o,So,"'power' must be a probability"),ln(n,So,"'p1' must be a probability"),ln(r,So,"'p2' must be a probability"),ln(t,u=>u>0,"number of observations in each group must be positive"),ln(e.tol,As,"'tol' must be a positive finite number");let i=Se(e.alternative,["two.sided","one.sided"]),c=e.strict??!1,a=e.tol??Ul,l=i==="two.sided"?2:1,f=(u,h,p,w)=>{let d=le(w/l,{lowerTail:!1});if(c&&l===2){let b=(h+p)/2,N=h*(1-h),v=p*(1-p),E=b*(1-b),g=Math.abs(h-p),y=Math.sqrt(N+v);return se((Math.sqrt(u)*g-d*Math.sqrt(2*E))/y)+se((Math.sqrt(u)*g+d*Math.sqrt(2*E))/y,{lowerTail:!1})}return se((Math.sqrt(u)*Math.abs(h-p)-d*Math.sqrt((h+p)*(1-(h+p)/2)))/Math.sqrt(h*(1-h)+p*(1-p)))},m=[];if(ye(o))o=f(t,n,r,s);else{let u=o;if(ye(t)){if(n===r)throw new RangeError("'n' cannot be determined when 'p1' equals 'p2': the power does not depend on it");t=Ye(h=>f(h,n,r,s)-u,1,1e7,{tol:a,extendInt:"upX"})}else if(ye(n)){if(r===0)throw new RangeError("'p1' cannot be determined when 'p2' is 0: no p1 in [0, p2] differs from p2");n=Ye(h=>f(t,h,r,s)-u,0,r,{tol:a,extendInt:"yes"}),n<0&&m.push("No p1 in [0, p2] can be found to achieve the desired power")}else if(ye(r)){if(n===1)throw new RangeError("'p2' cannot be determined when 'p1' is 1: no p2 in [p1, 1] differs from p1");r=Ye(h=>f(t,n,h,s)-u,n,1,{tol:a,extendInt:"yes"}),r>1&&m.push("No p2 in [p1, 1] can be found to achieve the desired power")}else s=Ye(h=>f(t,n,r,h)-u,1e-10,1-1e-10,{tol:a,extendInt:"upX"}),(s<0||s>1)&&m.push("No significance level [0, 1] can be found to achieve the desired power")}return Q("powerPropTest",{n:t,p1:n,p2:r,sigLevel:s,power:o,alternative:i,note:"n is number in *each* group",method:"Two-sample comparison of proportions power calculation",warnings:m})}var Kl=1e16,qs=(e,t)=>e>Kl&&e<1/0&&t>Kl;function ks(e,t,n,r){let o=le(r,{lowerTail:!1}),s=n/e,i=Math.sqrt(2/e+2/t),c=Math.hypot(Math.sqrt(2*(1+2*s)/e),Math.sqrt(2/t)*(1+s));return se((o*i-s)/c,{lowerTail:!1})}function gp(e,t,n,r){if(n===0)return 0;let o=l=>Math.floor(Math.log2(l)),[s,i,c,a]=[o(e),o(t),o(n),o(r)];return V(V(e,-s)*V(t,-i)*V(n,-c)/V(r,-a),s+i+c-a)}function wp(e={}){B(e,hp,"powerAnovaTest");let{groups:t,n,betweenVar:r,withinVar:o,power:s}=e,i=e.sigLevel===void 0?.05:e.sigLevel;if(nn([t,n,r,o,s,i],"exactly one of 'groups', 'n', 'between.var', 'within.var', 'power', and 'sig.level' must be NULL"),!ye(t)&&!(t>=2))throw new RangeError("number of groups must be at least 2");if(!ye(n)&&!(n>=2))throw new RangeError("number of observations in each group must be at least 2");Ka(i,Ua),ln(s,So,"'power' must be a probability"),ln(r,a=>a>=0&&a<1/0,"'betweenVar' must be a nonnegative finite number"),ln(o,As,"'withinVar' must be a positive finite number");let c=(a,l,f,m,u)=>{let h=(a-1)*l*f/m;h<1/0||(h=(a-1)*l*(f/m)),!(h<1/0)&&Number.isFinite(a)&&Number.isFinite(l)&&(h=gp(a-1,l,f,m));let p=a-1,w=(l-1)*a;return h===1/0?1:qs(p,w)?ks(p,w,h,u):Rn(Gn(u,p,w,{lowerTail:!1}),p,w,{ncp:h,lowerTail:!1})};if(ye(s))s=c(t,n,r,o,i);else{let a=s;if(ye(t))t=Ye(l=>c(l,n,r,o,i)-a,2,100);else if(ye(n)){if(r===0)throw new RangeError("'n' cannot be determined when 'betweenVar' is 0: the power does not depend on it");n=Ye(l=>c(t,l,r,o,i)-a,2,1e5)}else if(ye(o)){if(r===0)throw new RangeError("'withinVar' cannot be determined when 'betweenVar' is 0: the power does not depend on it");o=Ye(l=>c(t,n,r,l,i)-a,r*1e-7,r*1e7)}else ye(r)?r=Ye(l=>c(t,n,l,o,i)-a,o*1e-7,o*1e7):i=Ye(l=>c(t,n,r,o,l)-a,1e-10,1-1e-10)}return{groups:t,n,betweenVar:r,withinVar:o,sigLevel:i,power:s,note:"n is number in each group",method:"Balanced one-way analysis of variance power calculation",warnings:[]}}var bp=["correct","p","rescaleP"],vp=["confLevel"],Np=["alternative","rowScores","colScores"];function Zt(e){let t=[0,0];for(let n of e)t=he(t,[n,0]);return t}function Ya(e){let t=e.map(r=>{if(typeof r!="object"||r===null||typeof r.length!="number")throw new TypeError("'x' must be an array of numbers or an array of rows");return Array.from(r)}),n=t[0].length;if(t.some(r=>r.length!==n))throw new TypeError("all rows of the matrix must have the same length");if(t.flat().some(r=>typeof r!="number"))throw new TypeError("the entries of 'x' must be numbers");return t}var Ga=e=>e.length>0&&typeof e[0]!="number",Xa=(e,t)=>Number.isNaN(e)?NaN:wt(e,t/2,2,!1,!1);function Hl(e,t){if(!(e>=0&&t>=0&&e<1/0&&t<1/0))return NaN;let n=Math.sqrt(e),r=Math.sqrt(e+1),o=n*r+t,s=(e-t)*(e+t);if(Number.isFinite(s)){let c=o===0?0:(s+e)/o;return(2*(e-t)+2*c)/(n+r+Math.sqrt(4*t+1))}let i=(e-t)*((e+t)/o)+e/o;return(.5*(e-t)+.5*i)/((n+r+2*Math.sqrt(t+.25))/4)}function Ao(e,t){return e.length>0&&Array.isArray(e[0])?e.map((n,r)=>n.map((o,s)=>Hl(o,t[r][s]))):e.map((n,r)=>Hl(n,t[r]))}var yp=["none","williams","yates"],Gl="Log likelihood ratio (G-test)";function Mp(e,t={}){B(t,bp,"gTest");let n=Se(t.correct,yp);if(typeof e!="object"||e===null||typeof e.length!="number")throw new TypeError("'x' must be an array of counts or of rows");if(Ga(e)){let o=Ya(e);return Yl(o.flat()),o.length===1?Q("gTest",Ha(o[0],n,t)):o[0].length===1?Q("gTest",Ha(o.map(s=>s[0]),n,t)):Q("gTest",Ep(o,n))}let r=Array.from(e);if(r.some(o=>typeof o!="number"))throw new TypeError("the entries of 'x' must be numbers");return Yl(r),Q("gTest",Ha(r,n,t))}function Yl(e){if(e.some(t=>!(t>=0&&t<1/0)))throw new RangeError("all entries of 'x' must be nonnegative and finite");if(!e.some(t=>t>0))throw new RangeError("at least one entry of 'x' must be positive")}function Xl(e,t){return e<1/0?0:Math.ceil(Math.log2(t))+1}function Ql(e,t){if(e.some(r=>r<0))return NaN;let n=[0,0];for(let r=0;r<e.length;r++){let o=e[r],s=t[r];if(o>0&&s[0]===0)return 1/0;n=he(n,sl(o,s))}return n[0]+n[1]}function Ep(e,t){let n=e.length,r=e[0].length;if(t==="yates"&&(n!==2||r!==2))throw new RangeError("Yates' correction requires a 2 x 2 matrix");let o=Zt(e.flat()),s=Xl(o[0],n*r),i=s===0?e:e.map(v=>v.map(E=>V(E,-s))),c=s===0?o:Zt(i.flat()),a=i.map(v=>Zt(v)),l=i[0].map((v,E)=>Zt(i.map(g=>g[E]))),f=a.map(v=>l.map(E=>_e(v,rt(E,c)))),m=i;if(t==="yates"){let[[v,E],[g,y]]=i,x=v*y-E*g>0?-.5:.5,M=V(.5,-s),R=x<0?-M:M;m=[[v+R,E-R],[g-R,y+R]]}let u=Ql(m.flat(),f.flat()),h=Number.isNaN(u)?["NaNs produced"]:[],p=1;if(t==="williams"){if(a.some(y=>y[0]===0)||l.some(y=>y[0]===0))throw new RangeError("Williams' correction is not defined for a table with an empty row or column");let v=c[0]+c[1],E=a.reduce((y,x)=>y+1/(x[0]+x[1]),0),g=l.reduce((y,x)=>y+1/(x[0]+x[1]),0);p=1+V((v*E-1)*(v*g-1)/(6*v*(n-1)*(r-1)),-s)}let w=V(2*u,s)/p,d=(n-1)*(r-1),b=f.map(v=>v.map(E=>V(E[0]+E[1],s)));return{method:`${Gl} test of independence ${t==="none"?"without correction":t==="williams"?"with Williams' correction":"with Yates' correction"}`,statistic:w,statisticName:"G",parameter:d,parameterName:"X-squared df",pValue:Xa(w,d),observed:s===0?m:m.map(v=>v.map(E=>V(E,s))),expected:b,ftResiduals:Ao(e,b),warnings:h}}function Ha(e,t,n){let r=e.length;if(r<2)throw new RangeError("'x' must have at least 2 elements");if(t==="yates"&&r!==2)throw new RangeError("Yates' correction requires 2 data values");let o=n.rescaleP??!1;if(typeof o!="boolean")throw new TypeError("'rescaleP' must be a boolean");let s=n.p===void 0?e.map(()=>1/r):Array.from(n.p);if(s.length!==r)throw new RangeError("'x' and 'p' must have the same number of elements");if(s.some(v=>typeof v!="number"||Number.isNaN(v)))throw new RangeError("'p' must not contain missing values");if(s.some(v=>v<0))throw new RangeError("probabilities must be non-negative");if(s.some(v=>v===1/0))throw new RangeError("probabilities must be finite");let i=Zt(s);if(o){if(!(i[0]>0&&i[0]<1/0))throw new RangeError("the probabilities to rescale must have a positive finite sum")}else if(Math.abs(i[0]-1+i[1])>Math.sqrt(Number.EPSILON))throw new RangeError("probabilities must sum to 1");let c=Zt(e),a=Xl(c[0],r),l=a===0?e:e.map(v=>V(v,-a)),f=a===0?c:Zt(l),m=s.map(v=>o?rt(Et(f,v),i):Et(f,v)),u=m.map(v=>v[0]+v[1]),h=l;if(t==="yates"){let v=V(.5,-a),E=l[0]-u[0];E>V(.25,-a)?h=[l[0]-v,l[1]+v]:-E>V(.25,-a)&&(h=[l[0]+v,l[1]-v])}let p=Ql(h,m);if(!o&&!Number.isNaN(p)){let v=f[0]+f[1];p-=v*(i[0]-1+i[1])}let w=Number.isNaN(p)?["NaNs produced"]:[],d=t==="williams"?1+(r+1)/(6*V(f[0]+f[1],a)):1,b=V(2*p,a)/d,N=u.map(v=>V(v,a));return{method:`${Gl} goodness of fit test`,statistic:b,statisticName:"G",parameter:r-1,parameterName:"X-squared df",pValue:Xa(b,r-1),observed:a===0?h:h.map(v=>V(v,a)),expected:N,ftResiduals:Ao(e,N),warnings:w}}function xp(e){if(typeof e!="object"||e===null||!Ga(e))throw new RangeError("'x' must be square with at least two rows and columns");let t=Ya(e),n=t.length;if(n<2||t[0].length!==n)throw new RangeError("'x' must be square with at least two rows and columns");if(t.flat().some(r=>!(r>=0&&r<1/0)))throw new RangeError("all entries of 'x' must be nonnegative and finite");return t}function Rp(e){let t=xp(e),n=Zt(t.flat())[0],r=n>2**400||n>0&&n<2**-400?Math.round(Math.log2(n)):0,o=r===0?t:t.map(g=>g.map(y=>V(y,-r))),s=o.length,i=o.map(g=>Zt(g)),c=o.map((g,y)=>Zt(o.map(x=>x[y]))),a=o.map((g,y)=>o[y].some((x,M)=>M!==y&&x>0)||o.some((x,M)=>M!==y&&x[y]>0)).flatMap((g,y)=>g?[y]:[]),l=a.length;if(l<2)throw new RangeError("Too many equal marginals, cannot compute");let f=a.map((g,y)=>y),m=g=>f[g]===g?g:f[g]=m(f[g]);for(let g=0;g<l;g++)for(let y=g+1;y<l;y++)o[a[g]][a[y]]+o[a[y]][a[g]]>0&&(f[m(g)]=m(y));if(a.some((g,y)=>m(y)!==m(0)))throw new RangeError("the categories fall into groups without counts between them: the covariance matrix is singular");let u=l-1,h=a.slice(0,u).map(g=>{let y=he(i[g],[-c[g][0],-c[g][1]]);return y[0]+y[1]}),p=a.slice(0,u).map(g=>a.slice(0,u).map(y=>{if(g!==y)return-(o[g][y]+o[y][g]);let x=0;for(let M=0;M<s;M++)M!==g&&(x+=o[g][M]+o[M][g]);return x})),w=p.map(g=>g.map(()=>0)),d=[];for(let g=0;g<u;g++){let y=p[g][g];for(let x=0;x<g;x++)y-=w[g][x]*w[g][x]*d[x];d.push(y);for(let x=g+1;x<u;x++){let M=p[x][g];for(let R=0;R<g;R++)M-=w[x][R]*w[g][R]*d[R];w[x][g]=M/y}}let b=0,N=[];for(let g=0;g<u;g++){let y=h[g];for(let x=0;x<g;x++)y-=w[g][x]*N[x];N.push(y),b+=y*y/d[g]}let v=Zt(a.flatMap(g=>a.map(y=>t[g][y]))),E=l-1;return b=V(b,r),Q("stuartMaxwellTest",{method:"Stuart-Maxwell test",statistic:b,statisticName:"chi-squared",parameter:E,parameterName:"df",pValue:Xa(b,E),N:v[0]+v[1],warnings:[]})}function Tp(e,t={}){B(t,vp,"goodmanKruskalGamma");let n=t.confLevel??.95;if(!(typeof n=="number"&&n>0&&n<1))throw new RangeError("'confLevel' must be a single number in (0, 1)");let r=Zl(e),o=Zt(r.flat())[0],s=0;(o>2**250||o>0&&o<2**-250)&&(s=2*Math.round((Math.log2(o)-250)/2));let i=s===0?r:r.map(g=>g.map(y=>V(y,-s))),{pc:c,pd:a}=Sp(i),l=[0,0],f=[0,0];i.forEach((g,y)=>g.forEach((x,M)=>{l=he(l,Et(c[y][M],x)),f=he(f,Et(a[y][M],x))})),l=[l[0]/2,l[1]/2],f=[f[0]/2,f[1]/2];let m=he(l,f),u=rt(Le(l,f),m)[0],h=_e(m,m),p=i.map((g,y)=>g.map((x,M)=>2*rt(Le(_e(f,c[y][M]),_e(l,a[y][M])),h)[0])),w=Math.max(...p.flat().map(Math.abs)),d=w>0&&w<1/0?Math.floor(Math.log2(w)):0,b=0;i.forEach((g,y)=>g.forEach((x,M)=>{let R=V(p[y][M],-d);b+=x*R*R}));let N=V(Math.sqrt(b),d-s/2),v=(1-n)/2,E=v<1e-8?le(v,{lowerTail:!1}):le(1-v);return Q("goodmanKruskalGamma",{estimate:u,confInt:[Math.max(-1,u-E*N),Math.min(1,u+E*N)],confLevel:n,ase:N,warnings:[]})}function Zl(e){if(typeof e!="object"||e===null||!Ga(e))throw new TypeError("'table' must be an array of rows");let t=Ya(e);if(t[0].length===0)throw new RangeError("'table' must have at least one column");if(t.flat().some(n=>!(n>=0&&n<1/0)))throw new RangeError("all entries of 'table' must be nonnegative and finite");return t}function Sp(e){let t=e.length,n=e[0].length,r=[0,0],o=e.map(p=>{let w=[],d=r;for(let b=0;b<n;b++)w.push(d),d=he(d,[p[b],0]);return w}),s=e.map(p=>{let w=new Array(n),d=r;for(let b=n-1;b>=0;b--)w[b]=d,d=he(d,[p[b],0]);return w}),i=p=>{let w=[],d=new Array(n).fill(r);for(let b=0;b<t;b++)w.push(d),d=d.map((N,v)=>he(N,p[b][v]));return w},c=p=>{let w=new Array(t),d=new Array(n).fill(r);for(let b=t-1;b>=0;b--)w[b]=d,d=d.map((N,v)=>he(N,p[b][v]));return w},a=i(o),l=c(s),f=i(s),m=c(o),u=a.map((p,w)=>p.map((d,b)=>he(d,l[w][b]))),h=f.map((p,w)=>p.map((d,b)=>he(d,m[w][b])));return{pc:u,pd:h}}function Ap(e,t={}){B(t,Np,"lblTest");let n=Ze(t.alternative),r=Zl(e),o=r.length,s=r[0].length,i=(R,T,S)=>{if(R===void 0)return Array.from({length:T},(C,L)=>L+1);let A=Array.from(R);if(A.length!==T)throw new RangeError(`'${S}' must have one score for each ${S==="rowScores"?"row":"column"}`);if(A.some(C=>typeof C!="number"||!Number.isFinite(C)))throw new RangeError(`'${S}' must be finite numbers`);return A},c=i(t.rowScores,o,"rowScores"),a=i(t.colScores,s,"colScores"),f=Zt(r.flat())[0]>2**1e3?2*Math.ceil((Math.log2(Math.max(...r.flat()))-990)/2):0,m=f===0?r:r.map(R=>R.map(T=>V(T,-f))),u=m.map(R=>Zt(R)).map(R=>R[0]+R[1]),h=m[0].map((R,T)=>Zt(m.map(S=>S[T]))).map(R=>R[0]+R[1]),p=u.reduce((R,T)=>R+T,0);if(!(f>0||p>1))throw new RangeError("the table must have a total above 1");let w=u.reduce((R,T,S)=>R+T*c[S],0)/p,d=h.reduce((R,T,S)=>R+T*a[S],0)/p,b=c.map(R=>R-w),N=a.map(R=>R-d),v=u.reduce((R,T,S)=>R+T*b[S]*b[S],0),E=h.reduce((R,T,S)=>R+T*N[S]*N[S],0),g=0;if(m.forEach((R,T)=>R.forEach((S,A)=>g+=S*b[T]*N[A])),!(v>0&&E>0))throw new RangeError("the row and column scores must vary over the observations (the variance of the statistic is 0)");let y=g/Math.sqrt(v)/Math.sqrt(E),x=f===0?Math.sqrt(p-1)*y:V(Math.sqrt(p-V(1,-f))*y,f/2),M=n==="less"?se(x):n==="greater"?se(x,{lowerTail:!1}):2*se(-Math.abs(x));return Q("lblTest",{method:"Linear-by-Linear Association Test",alternative:n,statistic:x,statisticName:"Z",pValue:M,warnings:[]})}var Jl=["correct","p","rescaleP","simulatePValue","B","nMinus1"];function gn(e){let t=[0,0],n=0;for(let r of e)t=he(t,[r,0]),n+=r;return Number.isFinite(n)?t[0]:n}function qp(e,t,n){return ct(t)&&B(t,Jl,"chisqTest"),B(n,Jl,"chisqTest"),Array.isArray(t)?Q("chisqTest",kp(e,t,n??{})):Q("chisqTest",Ip(e,t??{}))}var nf="Chi-squared approximation may be incorrect";function kp(e,t,n){if(e.length!==t.length)throw new RangeError("'x' and 'y' must have the same length");let r=u=>u==null;for(let u of[...e,...t])if(!r(u)&&typeof u!="string")throw new TypeError("factor values must be strings (or null for missing values)");let o=[],s=[];e.forEach((u,h)=>{let p=t[h];!r(u)&&!r(p)&&(o.push(u),s.push(p))});let i=[...new Set(o)].sort(Tn),c=[...new Set(s)].sort(Tn);if(i.length<2||c.length<2)throw new RangeError("'x' and 'y' must have at least 2 levels");let a=new Map(i.map((u,h)=>[u,h])),l=new Map(c.map((u,h)=>[u,h])),f=i.map(()=>c.map(()=>0));return o.forEach((u,h)=>f[a.get(u)][l.get(s[h])]++),{...cf(f,n),rowLevels:i,colLevels:c}}function Ip(e,t){if(e.length>0&&typeof e[0]!="number"){let r=e.map(s=>Array.from(s)),o=r[0].length;if(r.some(s=>s.length!==o))throw new TypeError("all rows of the matrix must have the same length");return r.length===1?Qa(r[0],t):o===1?Qa(r.map(s=>s[0]),t):cf(r,t)}return Qa(Array.from(e),t)}function rf(e){let t=e.B??2e3;if(!(Number.isInteger(t)&&t>=1&&t<2**31))throw new RangeError("'B' must be a positive integer");return t}function of(e,t,n){let r=(1-64*Number.EPSILON)*t,o=0;for(let s=0;s<e.length;s++)e[s]>=r&&o++;return(1+o)/(Math.trunc(n)+1)}function sf(e,t){return Number.isNaN(e)?NaN:wt(e,t/2,2,!1,!1)}function af(e){if(!e.some(t=>t<5)&&e.some(Number.isNaN))throw new RangeError("missing value where TRUE/FALSE needed (the expected counts contain NaN)")}function Qa(e,t){if(t.nMinus1??!1)throw new RangeError("'nMinus1' requires a contingency table with at least two rows and columns");let n=e.length;if(e.some(w=>!(w>=0&&w<1/0)))throw new RangeError("all entries of 'x' must be non-negative and finite");if(n<2)throw new TypeError("'x' must have at least 2 elements");let r=gn(e);if(!(r>0))throw new RangeError("at least one entry of 'x' must be positive");let o=t.p===void 0?e.map(()=>1/n):Array.from(t.p);if(o.length!==n)throw new TypeError("'x' and 'p' must have the same number of elements");if(o.some(Number.isNaN))throw new RangeError("p contains missing values");if(o.some(w=>w<0))throw new RangeError("probabilities must be non-negative");if(o.some(w=>w===1/0))throw new RangeError("probabilities must be finite");if(t.rescaleP??!1){let w=gn(o),d=w<1/0?0:Math.ceil(Math.log2(n)),b=d===0?o:o.map(v=>V(v,-d)),N=d===0?w:gn(b);o=b.map(v=>v/N)}else if(Math.abs(gn(o)-1)>Math.sqrt(Number.EPSILON))throw new RangeError("probabilities must sum to 1");let s=r<1/0?0:2*Math.ceil(Math.log2(n)/2),i=s===0?e:e.map(w=>V(w,-s)),c=s===0?r:gn(i),a=o.map(w=>c*w),l=s===0?a:a.map(w=>V(w,s));af(l);let f=o.map((w,d)=>a[d]*(1-w)),m=V(gn(i.map((w,d)=>Vt(w-a[d],a[d]))),s),u="Chi-squared test for given probabilities",h=n-1,p;if(t.simulatePValue??!1){let w=rf(t);if(!Number.isInteger(r))throw new RangeError("the simulation needs counts summing to an integer");u+=No(w),h=NaN,p=of(_p(r,o,l,w),m,w)}else p=sf(m,h);return{method:u,statistic:m,statisticName:"X-squared",parameter:h,parameterName:"df",pValue:p,observed:e,expected:l,residuals:i.map((w,d)=>V((w-a[d])/Math.sqrt(a[d]),s/2)),stdres:i.map((w,d)=>V((w-a[d])/Math.sqrt(f[d]),s/2)),ftResiduals:Ao(e,l),warnings:Number.isFinite(h)&&l.some(w=>w<5)?[nf]:[]}}function _p(e,t,n,r){let o=t.length;if(r*e>2**27)throw new RangeError("the simulation needs B * sum(x) <= 2^27 (134217728) draws");let s=xl(o,t);if(r*e===0)return new Float64Array(0);let i=new Float64Array(r),c=new Float64Array(o);for(let a=0;a<r;a++){c.fill(0);for(let f=0;f<e;f++)c[s()]++;let l=0;for(let f=0;f<o;f++)l+=(c[f]-n[f])**2/n[f];i[a]=l}return i}function Lp(e,t,n,r){let o=e.length,s=t.length,i=e.reduce((f,m)=>f+m,0);if(i>ps)throw new RangeError("the simulation needs a total count of at most 16777216 (2^24)");let c=ds(i),a=new Int32Array(o*s),l=new Float64Array(Math.trunc(r));for(let f=0;f<l.length;f++){gs(e,t,c,a);let m=0;for(let u=0;u<s;u++)for(let h=0;h<o;h++){let p=n[h][u];m+=(a[h+u*o]-p)**2/p}l[f]=m}return l}function cf(e,t){let n=t.nMinus1??!1;if(typeof n!="boolean")throw new TypeError("'nMinus1' must be a boolean");if(n&&t.correct===!0)throw new RangeError("'nMinus1' uses no continuity correction: leave 'correct' out or set it to false");if(n&&(t.simulatePValue??!1))throw new RangeError("'nMinus1' cannot be combined with 'simulatePValue'");let r=!n&&(t.correct??!0),o=e.length,s=e[0].length,i=e.flat();if(i.some(v=>!(v>=0&&v<1/0)))throw new RangeError("all entries of 'x' must be non-negative and finite");let c=gn(i);if(!(c>0))throw new RangeError("at least one entry of 'x' must be positive");if(n&&!(c>1))throw new RangeError("'nMinus1' needs a table with a total above 1");let a=e.map(v=>gn(v)),l=e[0].map((v,E)=>gn(e.map(g=>g[E]))),f=a.map(v=>l.map(E=>ms(v,E,c)));af(f.flat());let m=e.map((v,E)=>v.map((g,y)=>g-f[E][y])),u="Pearson's Chi-squared test",h,p=(o-1)*(s-1),w,d=(t.simulatePValue??!1)&&a.every(v=>v>0)&&l.every(v=>v>0),b=!d&&r&&o===2&&s===2?Math.min(.5,...m.flat().map(Math.abs)):0,N=[];if((t.simulatePValue??!1)&&!d&&N.push("cannot compute simulated p-value with zero marginals"),d){if(e.some(g=>g.some(y=>!Number.isInteger(y))))throw new RangeError("the simulation needs integer counts");let v=rf(t);u+=No(v),h=gn(m.flatMap((g,y)=>g.map((x,M)=>Vt(x,f[y][M])))),p=NaN;let E=Lp(a.map(g=>Math.trunc(g)),l.map(g=>Math.trunc(g)),f,v);w=of(E,h,v)}else if(b>0)u+=" with Yates' continuity correction",h=gn(m.flatMap((v,E)=>v.map((g,y)=>Vt(Math.abs(g)-b,f[E][y]))));else if(h=gn(m.flatMap((v,E)=>v.map((g,y)=>Vt(g,f[E][y])))),n){u="Pearson's 'N-1' Chi-squared test";let v=h*(c-1);h=Number.isFinite(v)?v/c:h*((c-1)/c)}return{method:u,statistic:h,statisticName:"X-squared",parameter:p,parameterName:"df",pValue:w??sf(h,p),observed:e,expected:f,residuals:m.map((v,E)=>v.map((g,y)=>g/Math.sqrt(f[E][y]))),stdres:Cp(e,c,a,l,f,m),ftResiduals:Ao(e,f),warnings:Number.isFinite(p)&&f.some(v=>v.some(E=>E<5))?[...N,nf]:N}}function Cp(e,t,n,r,o,s){if(!Number.isFinite(t))return s.map((p,w)=>p.map((d,b)=>d/Math.sqrt(o[w][b]*(1-n[w]/t)*(1-r[b]/t))));let i=e.map(p=>ef(p)),c=e[0].map((p,w)=>ef(e.map(d=>d[w]))),a=Op(i),l=tf(i),f=tf(c),m=l.map(p=>rt(p,a)),u=c.map(p=>rt(p,a)),h=f.map(p=>rt(p,a));return e.map((p,w)=>p.map((d,b)=>{let N=_e(i[w],u[b]),v=Le([d,0],N),E=v[0]+v[1],g=_e(_e(N,m[w]),h[b]),y=g[0]+g[1];if(y>=Fp||E===0)return E/Math.sqrt(y);let x=N[0]+N[1],M=m[w][0]+m[w][1],R=h[b][0]+h[b][1];return E/Math.sqrt(x)/Math.sqrt(M)/Math.sqrt(R)}))}var Fp=2**-1022;function ef(e){let t=[0,0];for(let n of e)t=he(t,[n,0]);return t}function Op(e){let t=[0,0];for(let n of e)t=he(t,n);return t}function tf(e){let t=e.length,n=new Array(t+1);n[t]=[0,0];for(let s=t-1;s>=0;s--)n[s]=he(e[s],n[s+1]);let r=[],o=[0,0];for(let s=0;s<t;s++)r.push(he(o,n[s+1])),o=he(o,e[s]);return r}var Dp=Math.pow(Number.EPSILON,.25);function Kt(e,t,n,r={}){let o={tol:r.tol??Dp,maxiter:r.maxIter??1e3};return r.fLower!==void 0&&(o.fLower=r.fLower),r.fUpper!==void 0&&(o.fUpper=r.fUpper),br(e,t,n,o).root}function rr(e,t){let n=a=>e(Math.exp(a)),r=0,o=n(0),s=t?-1:1,i=n(s);for(;i!==0&&Math.sign(i)===Math.sign(o);){if(Math.abs(s)>745)return t?0:1/0;r=s,o=i,s*=2,i=n(s)}if(i===0)return Math.exp(s);let c=r<s?Kt(n,r,s,{tol:2**-50,fLower:o,fUpper:i}):Kt(n,s,r,{tol:2**-50,fLower:i,fUpper:o});return Math.exp(c)}var Pp=["alternative","confInt","confLevel","or","simulatePValue","B","midp"];function Io(e,t={}){B(t,Pp,"fisherTest");let n=e.length,r=n===0?0:e[0].length;if(e.some(f=>f.length!==r))throw new TypeError("all rows of the matrix must have the same length");if(n<2||r<2)throw new TypeError("'x' must have at least 2 rows and columns");let o=e.map(f=>Array.from(f));if(o.flat().some(f=>Number.isNaN(f)||f<0))throw new RangeError("all entries of 'x' must be non-negative and finite");if(o.flat().some(f=>f===1/0))throw new RangeError("'x' has entries too large to be integer");let s=o.map(f=>f.map(cl));if(s.flat().some(f=>f>2147483647))throw new RangeError("'x' has entries too large to be integer");let i=[],c=Vp(o,s);c!==null&&i.push(`'x' has been rounded to integer: ${c}`);let a="Fisher's Exact Test for Count Data";if(t.midp!==void 0&&typeof t.midp!="boolean")throw new TypeError("'midp' must be true or false");if((t.midp??!1)&&!(n===2&&r===2))throw new RangeError("the mid-p version ('midp') needs a 2 x 2 table");if(n===2&&r===2)return Q("fisherTest",{...Wp(s,t,a),warnings:i});let l=t.alternative??"two.sided";if(t.simulatePValue??!1){let f=t.B??2e3;if(!(Number.isInteger(f)&&f>=1&&f<2**31))throw new RangeError("'B' must be a positive integer");return Q("fisherTest",{method:a+No(f),alternative:l,pValue:$p(s,f),warnings:i})}return Q("fisherTest",{method:a,alternative:l,pValue:Bp(s),warnings:i})}function Vp(e,t){let n=Math.sqrt(Number.EPSILON),r=[],o=[];for(let l=0;l<e[0].length;l++)for(let f=0;f<e.length;f++)e[f][l]!==t[f][l]&&(r.push(e[f][l]),o.push(t[f][l]));let s=r.length;if(s===0)return null;let i=0;for(let l of r)i+=Math.abs(l)/s;let c="relative";Number.isFinite(i)&&i>n||(i=1,c="absolute");let a=0;for(let l=0;l<s;l++)a+=Math.abs(r[l]-o[l])/(s*i);return Number.isNaN(a)||a>n?`Mean ${c} difference: ${Rl(a)}`:null}function $p(e,t){let n=e.filter(d=>d.some(b=>b>0)),r=e[0].map((d,b)=>b).filter(d=>e.some(b=>b[d]>0));if(n.length<=1)throw new RangeError("need 2 or more non-zero row marginals");if(r.length<=1)throw new RangeError("need 2 or more non-zero column marginals");let o=n.map(d=>r.map(b=>d[b])),s=o.map(d=>d.reduce((b,N)=>b+N,0)),i=o[0].map((d,b)=>o.reduce((N,v)=>N+v[b],0)),c=s.length,a=i.length,l=[0,0];for(let d of o.flat())l=he(l,[-us(d),0]);let f=l[0]/(1+64*Number.EPSILON),m=s.reduce((d,b)=>d+b,0);if(m>ps)throw new RangeError("the simulation needs a total count of at most 16777216 (2^24)");let u=ds(m),h=new Int32Array(c*a),p=0,w=Math.trunc(t);for(let d=0;d<w;d++){gs(s,i,u,h);let b=0;for(let N=0;N<h.length;N++)b-=u[h[N]];b<=f&&p++}return Math.max(0,Math.min(1,(1+p)/(w+1)))}var lf=1+1e-7,zp=16,jp=2**24;function qo(e,t,n){let r=[0,0];for(let o=Math.max(0,t);o<=Math.min(n,e.length-1);o++)e[o]!==0&&(r=he(r,[e[o],0]));return r[0]}var Br=class{m;n;k;lo;hi;logCentral;constructor(t,n,r){this.m=t,this.n=n,this.k=r,this.lo=Math.max(0,r-n),this.hi=Math.min(r,t),this.logCentral=new Float64Array(this.hi-this.lo+1);let o=0,s=1;for(let i=this.lo;i<=this.hi;i++){let c=i-this.lo;c%zp===0||!(s>0&&s<1/0)?(o=bn(i,t,n,r,!0),s=1,this.logCentral[c]=o):this.logCentral[c]=o+Math.log(s),s*=(t-i)*(r-i)/((i+1)*(n-r+i+1))}}density(t){let n=this.logCentral.length,r=Math.log(t),o=new Float64Array(n),s=-1/0;for(let c=0;c<n;c++){let a=this.logCentral[c]+(this.lo+c)*r;o[c]=a,a>s&&(s=a)}for(let c=0;c<n;c++){let a=o[c]-s;o[c]=a<-746?0:Math.exp(a)}let i=qo(o,0,n-1);for(let c=0;c<n;c++)o[c]!==0&&(o[c]=o[c]/i);return o}mean(t){if(t===0)return this.lo;if(t===1/0)return this.hi;let n=this.density(t),r=[0,0];for(let o=0;o<n.length;o++)n[o]!==0&&(r=he(r,[(this.lo+o)*n[o],0]));return r[0]}tail(t,n,r){if(n===1)return r?sn(t-1,this.m,this.n,this.k,!1,!1):sn(t,this.m,this.n,this.k,!0,!1);if(n===0||n===1/0){let i=n===0?this.lo:this.hi;return(r?t<=i:t>=i)?1:0}let o=this.density(n),s=t-this.lo;return r?qo(o,s,o.length-1):qo(o,0,s)}midTail(t,n,r){if(n===1){let i=.5*bn(t,this.m,this.n,this.k,!1);return(r?sn(t,this.m,this.n,this.k,!1,!1):sn(t-1,this.m,this.n,this.k,!0,!1))+i}if(n===0||n===1/0){let i=n===0?this.lo:this.hi;return t===i?.5:(r?t<i:t>i)?1:0}let o=this.density(n),s=t-this.lo;return(r?qo(o,s+1,o.length-1):qo(o,0,s-1))+.5*o[s]}};function Ja(e,t,n){let r=e.density(n),o=r[t-e.lo]*lf,s=[0,0];for(let i of r)i<=o&&(s=he(s,[i,0]));return s[0]}function ff(e,t,n,r){let o=Math.max(0,r-n),s=Math.min(r,t),i=w=>bn(w,t,n,r,!0),c=i(e)+Math.log(lf),a=Math.min(s,Math.max(o,Math.floor((r+1)*(t+1)/(t+n+2))));for(;a<s&&i(a+1)>i(a);)a++;for(;a>o&&i(a-1)>i(a);)a--;let l=(w,d)=>{let b=w,N=d+1;for(;N-b>1;){let v=b+Math.floor((N-b)/2);i(v)<=c?b=v:N=v}return b},f=(w,d)=>{let b=w-1,N=d+1;for(;N-b>1;){let v=b+Math.floor((N-b)/2);i(v)<=c?N=v:b=v}return N},m,u;e<=a?(m=l(e,a),u=f(a+1,s)):(u=f(a,e),m=a-1<o||i(o)>c?o-1:l(o,a-1));let h=m<o?0:sn(m,t,n,r,!0,!1),p=u>s?0:sn(u-1,t,n,r,!1,!1);return h+p}function ko(e,t){return rr(e,t)}function Wp(e,t,n){let r=Ze(t.alternative),o=t.or??1;if(!(o>=0))throw new RangeError("'or' must be a single number between 0 and Inf");let s=t.confLevel??.95;if(!(s>0&&s<1))throw new RangeError("confLevel must be a single number between 0 and 1");let[[i,c],[a,l]]=e,f=i,m=Math.min(i,l)+Math.min(c,a)+1;if(m>jp)throw new RangeError(`the exact distribution of the 2 x 2 table has ${m} values, more than 2^24`);let u=new Br(i+a,c+l,i+c),{lo:h,hi:p}=u,w=t.midp??!1,d;w?(r==="less"?d=u.midTail(f,o,!1):r==="greater"?d=u.midTail(f,o,!0):d=Math.min(1,2*Math.min(u.midTail(f,o,!1),u.midTail(f,o,!0))),n=r==="two.sided"?"Central Fisher's Exact Test (mid-p version)":"One-sided Fisher's Exact Test (mid-p version)"):r==="less"?d=u.tail(f,o,!1):r==="greater"?d=u.tail(f,o,!0):o===0?d=f===h?1:0:o===1/0?d=f===p?1:0:d=Ja(u,f,o);let b;if(f===h)b=0;else if(f===p)b=1/0;else{let y=u.mean(1);b=y===f?1:ko(x=>u.mean(x)-f,y>f)}let N={method:n,alternative:r,pValue:d,estimate:b,estimateNames:"odds ratio",nullValue:o,nullValueName:"odds ratio",warnings:[]};if(!(t.confInt??!0))return N;let v=y=>{if(f===p)return 1/0;if(w){if(f===h&&y>=.5)return 0;let M=u.midTail(f,1,!1);return M===y?1:ko(R=>u.midTail(f,R,!1)-y,M<y)}let x=u.tail(f,1,!1);return x===y?1:ko(M=>u.tail(f,M,!1)-y,x<y)},E=y=>{if(f===h)return 0;if(w){if(f===p&&y>=.5)return 1/0;let M=u.midTail(f,1,!0);return M===y?1:ko(R=>u.midTail(f,R,!0)-y,M>y)}let x=u.tail(f,1,!0);return x===y?1:ko(M=>u.tail(f,M,!0)-y,x>y)},g;if(r==="less")g=[0,v(1-s)];else if(r==="greater")g=[E(1-s),1/0];else{let y=(1-s)/2;g=[E(y),v(y)]}return{...N,confInt:g,confLevel:s}}var Is=[[0,0]];function Sn(e){for(let t=Is.length;t<=e;t++)Is.push(he(Is[t-1],rl([t,0])));return Is[e]}function _s(e,t,n,r){let o=e+n,s=o-e,i=e-(o-s)+(n-s)+t+r,c=o+i;return[c,i-(c-o)]}var Za=class{scale=-1/0;hi=0;lo=0;add(t,n,r){if(t>this.scale){let a=Number.isFinite(this.scale)?Math.exp(this.scale-t):0;this.hi*=a,this.lo*=a,this.scale=t}let o=r*Math.exp(t-this.scale)*(1+n),s=this.hi+o,i=s-this.hi,c=this.hi-(s-i)+(o-i)+this.lo;this.hi=s+c,this.lo=c-(this.hi-s)}value(){return this.scale===-1/0?0:this.hi*Math.exp(this.scale)}};function Bp(e){let t=e.filter(v=>v.some(E=>E>0));if(t.length===0)throw new RangeError("all entries of the table are zero");let n=t[0].map((v,E)=>t.some(g=>g[E]>0));if(t=t.map(v=>v.filter((E,g)=>n[g])),t.length<2||t[0].length<2)return 1;t.length>t[0].length&&(t=t[0].map((v,E)=>t.map(g=>g[E])));let r=t.length,o=t.map(v=>v.reduce((E,g)=>E+g,0)),s=t[0].map((v,E)=>t.reduce((g,y)=>g+y[E],0)).sort((v,E)=>E-v),i=s.length,c=o.reduce((v,E)=>v+E,0),a=nl(Sn(c));for(let v of o)a=he(a,Sn(v));for(let v of s)a=he(a,Sn(v));let l=[0,0];for(let v of t.flat())l=Le(l,Sn(v));let f=he(l,[Math.log1p(1e-7),0]),m=1e-9*(1+Math.abs(f[0])),[u,h]=f,p=Array.from({length:i},()=>new Map),w=(v,E)=>{let g=E.join(","),y=p[v].get(g);if(y!==void 0)return y;let x=he(a,Sn(E.reduce((C,L)=>C+L,0)));for(let C of E)x=Le(x,Sn(C));for(let C=v;C<i;C++)x=Le(x,Sn(s[C]));let M={R:E,KZ:x,max:0,min:0,edges:[],pastHi:[],pastLo:[],count:[],index:new Map};if(p[v].set(g,M),v===i-1){let C=[0,0];for(let L of E)C=Le(C,Sn(L));return M.max=M.min=C[0],M.edges.push({to:M,hi:C[0],lo:C[1],mult:1}),M}let R=new Map,T=new Array(r+1).fill(0);for(let C=r-1;C>=0;C--)T[C]=T[C+1]+E[C];let S=new Array(r).fill(0),A=(C,L,k)=>{if(C===r-1){S[C]=L;let O=Le(k,Sn(L)),q=E.map((K,X)=>K-S[X]).sort((K,X)=>K-X),$=w(v+1,q),_=`${q.join(",")}|${O[0]}`,P=R.get(_);P===void 0?R.set(_,{to:$,hi:O[0],lo:O[1],mult:1}):P.mult++;return}let F=Math.max(0,L-T[C+1]),I=Math.min(E[C],L);for(let O=F;O<=I;O++)S[C]=O,A(C+1,L-O,Le(k,Sn(O)))};if(A(0,s[v],[0,0]),M.edges=[...R.values()],M.max=Nt(M.edges.map(C=>C.hi+C.to.max)),M.min=an(M.edges.map(C=>C.hi+C.to.min)),v===i-2){let C=M.edges.map($=>{let _=$.to.edges[0];return{v:he([$.hi,$.lo],[_.hi,_.lo]),mult:$.mult}});C.sort(($,_)=>$.v[0]-_.v[0]||$.v[1]-_.v[1]);let L=C.length,k=C[L-1].v[0],F=new Float64Array(L),I=new Float64Array(L),O=new Float64Array(L),q=[0,0];C.forEach(({v:$,mult:_},P)=>{F[P]=$[0],I[P]=$[1];let K=Le($,[k,0]);q=he(q,[_*Math.exp(K[0])*(1+K[1]),0]),O[P]=q[0]}),M.dist={hi:F,lo:I,cum:O,vmax:k,kv:he(a,[k,0])}}return M},d=w(0,[...o].sort((v,E)=>v-E)),b=new Za,N=(v,E,g,y)=>{if(E+v.max<=u-m){let[A,C]=_s(v.KZ[0],v.KZ[1],E,g);return b.add(A,C,y),!0}if(E+v.min>u+m)return!0;let x=v.dist;if(x===void 0)return!1;let[M,R]=_s(u,h,-E,-g),T=0,S=x.hi.length;for(;T<S;){let A=T+S>>>1,C=x.hi[A];C<M||C===M&&x.lo[A]<=R?T=A+1:S=A}if(T>0){let[A,C]=_s(x.kv[0],x.kv[1],E,g);b.add(A,C,y*x.cum[T-1])}return!0};N(d,0,0,1)||(d.pastHi.push(0),d.pastLo.push(0),d.count.push(1));for(let v=0;v<i-2;v++)for(let E of p[v].values()){let{pastHi:g,pastLo:y,count:x}=E;if(x.length!==0){for(let{to:M,hi:R,lo:T,mult:S}of E.edges)for(let A=0;A<x.length;A++){let[C,L]=_s(g[A],y[A],R,T),k=x[A]*S;if(N(M,C,L,k))continue;let F=M.index.get(C);F===void 0?(M.index.set(C,M.count.length),M.pastHi.push(C),M.pastLo.push(L),M.count.push(k)):M.count[F]+=k}E.pastHi=E.pastLo=E.count=[],E.index.clear()}}return Math.min(1,b.value())}function Ls(e){let t=e.every(n=>Number.isInteger(n))?1:2;return[t,e.map(n=>n*t).sort((n,r)=>n-r)]}function ti(e){return[...e].sort((t,n)=>t-n).join(",")}function ei(e,t,n){let r=n.length,o=new Float64Array(r),s=new Float64Array(r),i=0;for(let c=0;c<r;c++)o[c]=i+=n[c];i=0;for(let c=r-1;c>=0;c--)s[c]=i+=n[c];return{f:e,offset:t,lower:o,upper:s}}function ni(e){let[,t]=Ls(e),n=0,r=0;for(let o of t)r+=n+=o;return{steps:r,size:n+1}}function ri(e){let[t,n]=Ls(e),r=0;for(let i of n)r+=i;let o=new Float64Array(r+1);o[0]=1;let s=0;for(let i of n){s+=i;for(let c=s;c>=i;c--)o[c]=.5*(o[c]+o[c-i]);for(let c=i-1;c>=0;c--)o[c]=.5*o[c]}return ei(t,0,o)}function oi(e,t){let[,n]=Ls(e),r=n.length,o=Math.min(t,r-t),s=mf(n),i=0;for(let c=1;c<=r;c++)for(let a=Math.max(1,o-(r-c));a<=Math.min(c,o);a++)i+=s[c]-s[c-a]-s[a]+1;return{steps:i,size:s[r]-s[r-o]-s[o]+1}}function mf(e){let t=[0];for(let n of e)t.push(t[t.length-1]+n);return t}function si(e,t){let[n,r]=Ls(e),o=r.length,s=Math.min(t,o-t),i=mf(r),c=[];for(let h=0;h<=s;h++)c.push(new Float64Array(i[o]-i[o-h]-i[h]+1));c[0][0]=1;for(let h=1;h<=o;h++){let p=r[h-1];for(let w=Math.min(h,s);w>=Math.max(1,s-(o-h));w--){let d=c[w],b=c[w-1],N=w/h,v=(h-w)/h,E=i[w],g=i[h]-i[h-w],y=i[w-1]+p,x=Math.max(E,y);for(let M=g;M>=x;M--)d[M-E]=v*d[M-E]+N*b[M-y];for(let M=x-1;M>=E;M--)d[M-E]=v*d[M-E]}}let a=c[s],l=t*(t+1)/2;if(s===t)return ei(n,i[s]/n-l,a);let f=a.length,m=new Float64Array(f);for(let h=0;h<f;h++)m[h]=a[f-1-h];let u=i[o]-i[o-s];return ei(n,(i[o]-u)/n-l,m)}function or(e,t){let n=Math.ceil(e.f*(t-e.offset)+e.f*1e-8)-1;return n<0?0:n>=e.lower.length?1:Math.min(1,e.lower[n])}function yr(e,t){let n=or(e,t);if(Math.abs(n-1)>=1/64)return 1-n;let r=Math.ceil(e.f*(t-e.offset)+e.f*1e-8);return r>=e.upper.length?0:Math.min(1,e.upper[Math.max(0,r)])}var uf=["alternative","mu","paired","exact","correct","confInt","confLevel","tolRoot"];function _o(e,t,n={}){if(typeof e!="string"&&ct(t)&&B(t,uf,"wilcoxTest"),B(n,uf,"wilcoxTest"),typeof e=="string"){if(n.paired!==void 0)throw new Error("cannot use 'paired' in formula method");let w=bs(e,t,!0);return w.kind==="one"?_o(w.x,n):_o(w.x,w.y,w.kind==="paired"?{...n,paired:!0}:n)}let r;t!==void 0&&!ArrayBuffer.isView(t)&&!Array.isArray(t)?n=t:r=t;let o=Ze(n.alternative),s=n.mu??0,i=n.paired??!1,c=n.exact??null,a=Up(n.correct??!0),l=n.confInt??!1,f=n.confLevel??.95,m=n.tolRoot??1e-4;if(!Number.isFinite(s))throw new RangeError("mu must be a single finite number");if(l&&!(f>0&&f<1))throw new RangeError("confLevel must be a number strictly between 0 and 1");if(i&&r===void 0)throw new TypeError("y is required when paired is true");let u,h;if(i){let[w,d]=Jn(Array.from(e),Array.from(r));u=Dt(w.map((b,N)=>b-d[N]))}else u=Dt(Array.from(e)),r!==void 0&&(h=Dt(Array.from(r)));if(u.length<1)throw new Error("not enough (non-missing) 'x' observations");if(h!==void 0&&h.length<1)throw new Error("not enough (non-missing) 'y' observations");let p={alternative:o,mu:s,correct:a,confInt:l,confLevel:f,tolRoot:m,warnings:new Rt};return Q("wilcoxTest",h===void 0?Qp(u,c,i,p):sd(u,h,c,p))}function Up(e){if(typeof e=="boolean")return e?0:-1;if(!(Number.isInteger(e)&&e>=0&&e<=3))throw new RangeError("'correct' must be an integer between 0 and 3");return e}function Cs(e,t,n){return n?t==="two.sided"?Math.sign(e)*.5:t==="greater"?.5:-.5:0}var df=10*Number.EPSILON;function gf(e,t,n=0){let r=a=>Math.min(1,Math.max(a,0)),o=()=>n===0?se(e):r(se(e)-n*Xt(e)),s=()=>n===0?se(e,{lowerTail:!1}):r(se(e,{lowerTail:!1})+n*Xt(e));if(t==="less")return o();if(t==="greater")return s();let i=o();if(i<=.5)return 2*i;let c=s();return c>=1/64?2*(1-i):2*c}function Hp(e){let t=e*e;return[e*(t-3),e*(t*(t-10)+15),e*(t*(t*(t-21)+105)-105)]}function Yp(e,t,n){let r=t*(t+1)*(2*t+1),o=-(3*t*t+3*t-1)/(10*r),s=4*(3*t**4+6*t**3-3*t+1)/(35*r*r);return wf(e,o,s,n)}function Gp(e,t,n,r){let o=t+n,s=t*n,i=-(o*(o+1)-s)/(20*s*(o+1)),c=(2*(t**4+n**4)+4*s*(t*t+n*n)+6*s*s+4*(t**3+n**3)+(7*s+o-1)*o)/(210*s*s*(o+1)**2);return wf(e,i,c,r)}function wf(e,t,n,r){let[o,s,i]=Hp(e),c=t*o;return r>=2&&(c+=n*s),r>=3&&(c+=t*t/2*i),c}function ci(e){let t=e.length;if(t===0)return NaN;let n=Math.floor(t/2);return t%2===1?e[n]:it([e[n-1],e[n]])}var li=(e,t)=>e-t,ai=5e9,Xp=40;function ii(e){return new RangeError(`wilcoxTest: the exact conditional ${e} would take too long or too much memory: use exact: false`)}function Fs(e,t){if(e.size>16777216||e.steps>ai)throw ii(t);return e.steps}function bf(e){let t=e.length,n=bt(e.map(Math.abs)),r=0;for(let i=0;i<t;i++)e[i]>0&&(r+=n[i]);let o=0;for(let i of wn(n))o+=i*i*i-i;let s=Math.sqrt(t*(t+1)*(2*t+1)/24-o/48);return[r,r-t*(t+1)/4,s]}function Qp(e,t,n,r){let{alternative:o,mu:s}=r,i=e.map(g=>g-s);i.some((g,y)=>!Number.isFinite(g)&&Number.isFinite(e[y]))&&(i=e.map(g=>g/2-s/2));let a=i.some(g=>g===0),l=t??i.length<50,f=(g,y,x)=>({method:g,alternative:o,statistic:y,statisticName:"V",pValue:x,nullValue:s,nullValueName:n?"location shift":"location",warnings:r.warnings.list});if(l){let g=bt(i.map(Math.abs)),y=wn(g).length>0,x=0;for(let C=0;C<i.length;C++)i[C]>0&&(x+=g[C]);let M="Wilcoxon signed rank exact test";if(!y&&!a){let C=i.length,L=C*(C+1)/2,k;if(o==="less")k=$r(x,C,!0,!1);else if(o==="greater")k=$r(x-1,C,!1,!1);else{let I=x>L/2?$r(x-1,C,!1,!1):$r(x,C,!0,!1);k=Math.min(1,2*I)}let F=f(M,x,k);return r.confInt?{...F,...Zp(e,r)}:F}let R=g.filter((C,L)=>i[L]!==0);Fs(ni(R),"distribution");let T=ri(R),S;if(o==="less")S=or(T,x);else if(o==="greater")S=yr(T,x-.25);else{let C=0;for(let L of R)C+=L/2;S=Math.min(1,2*(x>C?yr(T,x-.25):or(T,x)))}let A=f(M,x,S);return r.confInt?{...A,...ed(e,r)}:A}let m=a?i.filter(g=>g!==0):i,u=m.length;if(u===0)throw new RangeError("no non-zero differences for the signed rank test");let[h,p,w]=bf(m),b=wn(bt(m.map(Math.abs))).length>0||a?Math.min(r.correct,0):r.correct,N=(p-Cs(p,o,b>=0))/w,v=gf(N,o,b>=1?Yp(N,u,b):0),E=f(`Wilcoxon signed rank test${r.correct>=0?" with continuity correction":""}`,h,v);return r.confInt?{...E,...td(e,r)}:E}function fi(e,t,n){return e==="less"?[-1/0,n]:e==="greater"?[t,1/0]:[t,n]}function hf(e,t,n){let r={confInt:fi(e,NaN,NaN),confLevel:0,estimate:t};return n!==void 0&&(r.estimateNames=n),r}function vf(e){if(!(e>0&&e<1/0))throw new RangeError("tolRoot must be a positive finite number")}function Nf(e){let t=e.length,n=[];for(let r=0;r<t;r++)for(let o=r;o<t;o++){let s=dn(e[r],e[o]);Number.isNaN(s)||n.push(s)}return n.sort(li)}function yf(e,t,n,r,o){let{alternative:s,confLevel:i}=r,c=e.length,a=s==="two.sided"?(1-i)/2:1-i,l=n(a);t(l)<=a+df&&l++;let f=ci(e);if(l===0)return{confInt:[-1/0,1/0],confLevel:1,estimate:f,estimateNames:o};let m=t(l-1);return{confInt:fi(s,e[l-1],e[c-l]),confLevel:1-(s==="two.sided"?2*m:m),estimate:f,estimateNames:o}}function Zp(e,t){let n=e.length;return yf(Nf(e),r=>$r(r,n,!0,!1),r=>dl(r,n,!0,!1),t,"(pseudo)median")}function Mf(e){let t=e.length,n=Float64Array.from(e).sort(),r=new Array(t);for(let s=0;s<t;s++){let i=e[s],c=0,a=t;for(;c<a;){let f=c+a>>>1;n[f]<i?c=f+1:a=f}let l=c;for(a=t;l<a;){let f=l+a>>>1;n[f]<=i?l=f+1:a=f}r[s]=(c+1+l)/2}let o="";for(let s=0;s<t;){let i=s;for(;i+1<t&&n[i+1]===n[s];)i++;i>s&&(o+=`${s}:${i-s+1},`),s=i+1}return{ranks:r,key:o}}function Ef(e,t,n,r,o,s,i,c,a,l){let{alternative:f,confLevel:m}=a,u=e.length,p=(f==="two.sided"?(1-m)/2:1-m)+df,w=f!=="less",d=f!=="greater",b=k=>dn(e[k-1],e[k]),N=Math.min(u-1,Math.max(1,Math.ceil(i.mean+le(Math.min(p,1))*i.sd+.1*i.sd+3))),v=8,E=k=>k===0?e[0]-1:b(k),g=k=>k===0?e[u-1]+1:b(u-k),y=[new Map,new Map];{let k=new Map,F=0,I=Xp*c;for(let[O,q]of[[0,E],[1,g]])if(!(O===0?!w:!d))for(let $=0;$<=N;$++){if(F+=I,F>ai)throw ii("interval");let _;try{_=t(q($))}catch{break}if(y[O].set($,[_.stat,_.key]),k.has(_.key)){k.delete(_.key),k.set(_.key,!0);continue}if(F+=r(_.scores),F>ai)throw ii("interval");k.set(_.key,!0),k.size>v&&k.delete(k.keys().next().value)}}let x=new Map,M=(k,F)=>{let I=k===0?E(F):g(F),O=y[k].get(F),q;if(O===void 0){let K=t(I);O=[K.stat,K.key],q=K.scores}let[$,_]=O,P=x.get(_);return P!==void 0?x.delete(_):(P=n(q??t(I).scores),x.size>=v&&x.delete(x.keys().next().value)),x.set(_,P),[$,P]},R=k=>{let[F,I]=M(0,k);return o(I,F)},T=k=>{let[F,I]=M(1,k);return s(I,F)},S=-1/0,A=0;if(w){let k=R(0);if(!(k>p)){let F=k,I=1;for(;I<=u-1;I++){let O=R(I);if(O>p)break;F=O}S=e[I-1],A=F}}let C=1/0,L=0;if(d){let k=T(0);if(!(k>p)){let F=k,I=u-1;for(;I>=1;I--){let O=T(u-I);if(O>p)break;F=O}C=e[I],L=F}}return{confInt:[S,C],confLevel:1-(A+L),estimate:ci(e),estimateNames:l}}function Jp(e,t){let n=e.map(r=>r-t);if(n.some(r=>Number.isNaN(r)))throw new Error("cannot compute the exact confidence interval: the shifted data have undefined values (infinite observations)");return Number.isFinite(t)&&n.some((r,o)=>!Number.isFinite(r)&&Number.isFinite(e[o]))&&(n=e.map(r=>r/2-t/2)),n}function ed(e,t){let n=e.length,r=s=>{let i=Jp(e,s),{ranks:c,key:a}=Mf(i.map(Math.abs)),l=0;for(let f=0;f<n;f++)i[f]>0&&(l+=c[f]);return{stat:l,scores:c,key:a}},o=n*(n+1)/2;return Ef(Nf(e),r,ri,s=>Fs(ni(s),"interval"),(s,i)=>yr(s,i-.25),or,{mean:o/2,sd:Math.sqrt(n*(n+1)*(2*n+1)/24)},n,t,"(pseudo)median")}function td(e,t){let{alternative:n,confLevel:r,tolRoot:o}=t,s=t.correct>=0,i=e.length;if(i===0)return hf(n,NaN);let c=an(e),a=Nt(e);if(!Number.isFinite(c)||!Number.isFinite(a)||i===1||c===a)return Number.isFinite(c)&&c===a&&t.warnings.add("cannot compute confidence interval when all observations are zero or tied"),hf(n,dn(c,a),"midrange");let l=(v,E)=>{let[,g,y]=bf(e.map(x=>x-v).filter(x=>x!==0));return(g-Cs(g,n,E))/y},f=l(c,s),m=l(a,s),u=v=>-le(v),h=v=>n==="greater"?!(f<u(v)):n==="less"?!(m>le(v/2)):!(f<u(v/2))&&!(m>le(v/2)),p=1-r,w=!1;for(;!h(p)&&p<1;)p*=2,w=!0;w&&t.warnings.add("requested conf.level not achievable"),vf(o);let d=v=>Kt(E=>l(E,s)-v,c,a,{tol:o,fLower:f-v,fUpper:m-v}),b;if(p>=1){let v=ci([...e].sort(li));b=fi(n,v,v)}else n==="greater"?b=[d(u(p)),1/0]:n==="less"?b=[-1/0,d(le(p))]:b=[d(u(p/2)),d(le(p/2))];let N=Kt(v=>l(v,!1),c,a,{tol:o,fLower:l(c,!1),fUpper:l(a,!1)});return{confInt:b,confLevel:w?1-Math.min(1,p):r,estimate:N,estimateNames:"(pseudo)median"}}function xf(e,t){let n=e.length,r=n-t,o=0;for(let a=0;a<t;a++)o+=e[a];let s=o-t*(t+1)/2,i=0;for(let a of wn(e))i+=a*a*a-a;let c=Math.sqrt(t*r/12*(n+1-i/(n*(n-1))));return[s,s-t*r/2,c,i>0]}function nd(e,t){let[n,r,o]=xf(bt([...e,...t]),e.length);return[n,r,o]}function rd(e,t){let n=e.length,r=n+t.length,o=(l,f)=>{let m=l===0||!Number.isFinite(l)||!Number.isFinite(f)?f:f/2;return l<m?-1:l>m?1:0},s=(l,f)=>l<n&&f<n?e[l]<e[f]?-1:e[l]>e[f]?1:0:l>=n&&f>=n?t[l-n]<t[f-n]?-1:t[l-n]>t[f-n]?1:0:l<n?o(e[l],t[f-n]):-o(e[f],t[l-n]),i=Array.from({length:r},(l,f)=>f);i.sort((l,f)=>s(l,f)||l-f);let c=new Array(r),a=0;for(;a<r;){let l=a;for(;l+1<r&&s(i[l+1],i[a])===0;)l++;let f=(a+l+2)/2;for(let m=a;m<=l;m++)c[i[m]]=f;a=l+1}return c}function od(e,t,n){return Rf(e,t,n).ranks}function Rf(e,t,n){let r=e.map(o=>o-n);if(r.some(o=>Number.isNaN(o))){let o=r.map((a,l)=>[a,l]).filter(([a])=>!Number.isNaN(a)),s=bt([...o.map(([a])=>a),...t]),i=new Array(e.length+t.length);o.forEach(([,a],l)=>i[a]=s[l]),t.forEach((a,l)=>i[e.length+l]=s[o.length+l]);let c=o.length+t.length;return r.forEach((a,l)=>{Number.isNaN(a)&&(i[l]=++c)}),{ranks:i,key:ti(i)}}if(Number.isFinite(n)&&r.some((o,s)=>!Number.isFinite(o)&&Number.isFinite(e[s]))){let o=rd(e.map(s=>s/2-n/2),t);return{ranks:o,key:ti(o)}}return Mf([...r,...t])}function sd(e,t,n,r){let{alternative:o,mu:s}=r,i=e.length,c=t.length,a=n??(i<50&&c<50),l=od(e,t,s),[f,m,u,h]=xf(l,i),p,w;if(a)if(w="Wilcoxon rank sum exact test",!h)p=pf(()=>o==="less"?zr(f,i,c,!0,!1):o==="greater"?zr(f-1,i,c,!1,!1):Math.min(2*zr(f,i,c,!0,!1),2*zr(f-1,i,c,!1,!1),1));else{Fs(oi(l,i),"distribution");let N=si(l,i);o==="less"?p=or(N,f):o==="greater"?p=yr(N,f-.25):p=Math.min(2*or(N,f),2*yr(N,f-.25),1)}else{w=`Wilcoxon rank sum test${r.correct>=0?" with continuity correction":""}`;let N=h?Math.min(r.correct,0):r.correct,v=l.every(g=>g===l[0]),E=(m-Cs(m,o,N>=0))/u;p=v?1:gf(E,o,N>=1?Gp(E,i,c,N):0)}let d={method:w,alternative:o,statistic:f,statisticName:"W",pValue:p,nullValue:s,nullValueName:"location shift",warnings:r.warnings.list};if(!r.confInt)return d;let b=a?h?id(e,t,r):pf(()=>ad(e,t,r)):cd(e,t,r);return{...d,...b}}function pf(e){try{return e()}catch(t){throw t instanceof RangeError&&t.message.startsWith("wilcox:")?new RangeError(`${t.message}: use exact: false`):t}}function Tf(e,t,n){let r=[];for(let o of e)for(let s of t){let i=o-s;n&&Number.isNaN(i)||r.push(i)}return r.sort(li)}function ad(e,t,n){let r=e.length,o=t.length;return yf(Tf(e,t,!1),s=>zr(s,r,o,!0,!1),s=>wl(s,r,o,!0,!1),n,"difference in location")}function id(e,t,n){let r=e.length,o=t.length,s=i=>{let{ranks:c,key:a}=Rf(e,t,i),l=0;for(let f=0;f<r;f++)l+=c[f];return{stat:l-r*(r+1)/2,scores:c,key:a}};return Ef(Tf(e,t,!0),s,i=>si(i,r),i=>Fs(oi(i,r),"interval"),(i,c)=>yr(i,c-.25),or,{mean:r*o/2,sd:Math.sqrt(r*o*(r+o+1)/12)},r+o,n,"difference in location")}function cd(e,t,n){let{alternative:r,confLevel:o,tolRoot:s}=n,i=n.correct>=0,c=an(e)-Nt(t),a=Nt(e)-an(t);if(!Number.isFinite(c)||!Number.isFinite(a))throw new Error("cannot compute the confidence interval because the differences are not finite");let l=(h,p)=>{let[,w,d]=nd(e.map(b=>b-h),t);return(w-Cs(w,r,p))/d},f=(h,p)=>{let w=l(c,p)-h,d=l(a,p)-h;if(Number.isNaN(w)||Number.isNaN(d))throw new Error("cannot compute the confidence interval because all observations are tied");return w<=0?c:d>=0?a:(vf(s),Kt(b=>l(b,p)-h,c,a,{tol:s,fLower:w,fUpper:d}))},m=1-o,u;return r==="greater"?u=[f(-le(m),i),1/0]:r==="less"?u=[-1/0,f(le(m),i)]:u=[f(-le(m/2),i),f(le(m/2),i)],{confInt:u,confLevel:o,estimate:f(0,!1),estimateNames:"difference in location"}}var ld=["method","n"],Sf=["holm","hochberg","hommel","bonferroni","BH","BY","fdr","none"];function fd(e){if(Sf.includes(e))return e;let t=Sf.filter(n=>n.startsWith(e));if(e.length===0||t.length!==1)throw new RangeError(`invalid method "${e}"`);return t[0]}function Kr(e,t={}){B(t,ld,"pAdjust");let n=typeof t=="string"?{method:t}:t,r=fd(n.method??"holm"),o=Array.from(e),s=[];for(let m=0;m<o.length;m++)Number.isNaN(o[m])||s.push(m);let i=s.length,c=n.n??i;if(!(c>=i))throw new RangeError("n must be at least the number of non-NaN p-values");if(c===1/0)throw new RangeError("n must be finite");if(c<=1||i===0)return o;let a=s.map(m=>o[m]),l=qf(a,r,c),f=o.slice();return s.forEach((m,u)=>f[m]=l[u]),f}function Os(e,t){let n=e.map((r,o)=>o);return n.sort((r,o)=>(t?e[o]-e[r]:e[r]-e[o])||r-o),n}function qf(e,t,n){let r=e.length,o=new Array(r);switch(t){case"none":return e.slice();case"bonferroni":return e.map(s=>Math.min(1,n*s));case"holm":{let s=Os(e,!1),i=-1/0;return s.forEach((c,a)=>{i=Math.max(i,(n-a)*e[c]),o[c]=Math.min(1,i)}),o}case"hochberg":{let s=Os(e,!0),i=1/0;return s.forEach((c,a)=>{let l=r-a;i=Math.min(i,(n-l+1)*e[c]),o[c]=Math.min(1,i)}),o}case"BH":case"fdr":case"BY":{let s=1;if(t==="BY"){let m=Pn(),u=Math.floor(n);if(u<=1e6){for(let h=1;h<=u;h++)pn(m,1/h,u);s=Vn(m)}else{let h=u*u;s=Math.log(u)+.5772156649015329+1/(2*u)-1/(12*h)+1/(120*h*h)}}let i=s*n,c=Number.isFinite(i)?1:1024,a=Number.isFinite(i)?i:s*(n/1024),l=Os(e,!0),f=1/0;return l.forEach((m,u)=>{let h=r-u;f=Math.min(f,a/h*e[m]*c),o[m]=Math.min(1,f)}),o}case"hommel":return n===2?qf(e,"hochberg",n):md(e,n)}}var Af=2**16;function md(e,t){if(!Number.isInteger(t))throw new RangeError("n must be an integer for the hommel method");if(t>Af)throw new RangeError(`n must be at most ${Af} for the hommel method (O(n^2) work)`);let n=e.length,r=[...e,...new Array(t-n).fill(1)],o=Os(r,!1),s=o.map(a=>r[a]),i=s.slice();for(let a=t;a>=2;a--){let l=1/0;for(let f=1;f<=a;f++)l=Math.min(l,a*s[t-a+f-1]/f);for(let f=t-a;f<t;f++)i[f]=Math.max(i[f],l);for(let f=0;f<t-a;f++)i[f]=Math.max(i[f],Math.min(a*s[f],l))}let c=new Array(n);return o.forEach((a,l)=>{a<n&&(c[a]=i[l])}),c}var kf=["p","alternative","confLevel"],ud=1+1e-7;function mi(e,t,n){let r=e-1;return e===0||e-r===1&&Ot(t,-r)[1]===0?Dn(r,t,n,!1,!1):Dn(e,t,n,!1,!1)+hn(e,t,n,!1)}function hd(e,t,n){if(n===0)return e===0?1:0;if(n===1)return e===t?1:0;let r=t*n;if(e===r)return 1;let o=hn(e,t,n,!1)*ud,s=a=>hn(a,t,n,!1)<=o;if(e<r){let a=ss(s,Math.ceil(r),t),l=a>t?0:mi(a,t,n);return Math.min(1,Dn(e,t,n,!0,!1)+l)}let i=as(s,0,Math.floor(r)),c=i<0?0:Dn(i,t,n,!0,!1);return Math.min(1,mi(e,t,n)+c)}function pd(e,t,n={}){B(t,kf,"binomTest"),B(n,kf,"binomTest");let r,o;if(typeof e!="number"){if(e.length!==2)throw new RangeError("incorrect length of 'x'");if(t!==void 0&&typeof t!="number"&&(n=t),!vn(e[0])||!vn(e[1]))throw new RangeError("'x' must be nonnegative and integer");if(r=Math.round(e[0]),o=Math.round(e[0])+Math.round(e[1]),o===0||o===1/0)throw new RangeError("'n' must be a positive integer >= 'x'")}else{if(typeof t!="number")throw new TypeError("'n' is required when 'x' is a single number");if(!vn(e))throw new RangeError("'x' must be nonnegative and integer");if(r=Math.round(e),!(vn(t)&&Math.round(t)>=r&&t>0))throw new RangeError("'n' must be a positive integer >= 'x'");o=Math.round(t)}return ui(r,o,n)}function ui(e,t,n={}){let r=n.p??.5;if(!(r>=0&&r<=1))throw new RangeError("'p' must be a single number between 0 and 1");let o=Ze(n.alternative),s=n.confLevel??.95;_t(s);let i;o==="less"?i=Dn(e,t,r,!0,!1):o==="greater"?i=mi(e,t,r):i=hd(e,t,r);let c=f=>e===0?0:Xn(f,e,t-e+1,!0,!1),a=f=>e===t?1:f<1e-8?Xn(f,e+1,t-e,!1,!1):Xn(1-f,e+1,t-e,!0,!1),l;if(o==="less")l=[0,a(1-s)];else if(o==="greater")l=[c(1-s),1];else{let f=(1-s)/2;l=[c(f),a(f)]}return{method:"Exact binomial test",alternative:o,statistic:e,statisticName:"number of successes",parameter:t,parameterName:"number of trials",pValue:i,confInt:l,confLevel:s,estimate:e/t,estimateNames:"probability of success",nullValue:r,nullValueName:"probability of success",warnings:[]}}var dd=["T","r","alternative","confLevel"],gd=1+1e-7;function wd(e,t){if(t===0)return e===0?1:0;if(e===t)return 1;let n=is(e,t,!1)*gd,r=i=>is(i,t,!1)<=n,o=Math.floor(t);if(e<t){let i=ss(r,Math.max(e+1,o),1/0);return Math.min(1,Vr(e,t,!0,!1)+hi(i,t))}let s=as(r,0,Math.min(e-1,o));return Math.min(1,Vr(s,t,!0,!1)+hi(e,t))}var Lo=2**-1022;function hi(e,t){return e-(e-1)===1?Vr(e-1,t,!1,!1):Vr(e,t,!1,!1)+is(e,t,!1)}function Mr(e){let t=e<9332636185032189e-317?600:0,n=Math.floor(Math.log2(e*2**t));return[e*2**t*2**-n,n-t]}function bd(e,t,n,r){let o=n+t*r,s=e*t*n/o;if(Number.isFinite(s)&&Number.isFinite(o)&&o>=Lo&&(s!==0||e===0||t===0))return s;let i=e*t,c=1+t*(r/n);if(e===0||t===0)return 0;if(Number.isFinite(i)&&Number.isFinite(c))return i/c;let[a,l]=Mr(e),[f,m]=Mr(t),[u,h]=Mr(n),[p,w]=Mr(r),d=f*p,b=h-m-w,N=b>=0?u/(u+V(d,-b)):u/(V(u,b)+d);return V(a*f*N,l+m+Math.min(b,0))}function vd(e,t,n,r){let o=e/n,s=t/r,i=o/s;return t===0||Number.isFinite(o)&&Number.isFinite(s)&&s>=Lo&&(o===0||o>=Lo)?i:e===0?0:e/t*(r/n)}function Nd(e,t={}){B(t,dd,"poissonTest");let n=typeof e=="number"?[e]:Array.from(e);if(n.length<1||n.length>2)throw new RangeError("incorrect length of 'x'");if(!n.every(vn))throw new RangeError("'x' must be finite, nonnegative, and integer");let r=n.map(Math.round),o=r.length,s=t.T??1,i=typeof s=="number"?new Array(o).fill(s):Array.from(s);if(i.length!==o)throw new RangeError("'x' and 'T' have incompatible length");if(!i.every(b=>Number.isFinite(b)&&b>0))throw new RangeError("'T' must be positive");let c=t.r??1;if(!(Number.isFinite(c)&&c>=0))throw new RangeError("'r' must be a single positive number");let a=Ze(t.alternative),l=t.confLevel??.95;if(o===2?_t(l):cn(l),o===2){let[b,N]=r,[v,E]=i,g=b+N;if(g===1/0)throw new RangeError("'x' must be finite, nonnegative, and integer");let y=c*v,x=y+E,M=Number.isFinite(x)&&x>=Lo&&(y===0||y>=Lo)?y/x:1/(1+E/v/c),R=ui(Math.round(b),Math.round(b)+Math.round(N),{p:M,alternative:a,confLevel:l}),T=E/v,S=A=>{if(A===0)return 0;let C=A/(1-A);if(Number.isFinite(T)||C===1/0)return C*T;let[L,k]=Mr(C),[F,I]=Mr(v),[O,q]=Mr(E);return V(L*O/F,k+q-I)};return{method:"Comparison of Poisson rates",alternative:a,statistic:b,statisticName:"count1",parameter:bd(g,c,v,E),parameterName:"expected count1",pValue:R.pValue,confInt:[S(R.confInt[0]),S(R.confInt[1])],confLevel:l,estimate:vd(b,N,v,E),estimateNames:"rate ratio",nullValue:c,nullValueName:"rate ratio",warnings:[]}}let f=r[0],m=i[0],u=c*m,h;a==="less"?h=Vr(f,u,!0,!1):a==="greater"?h=hi(f,u):h=wd(f,u);let p=b=>f===0?0:os(b,f,1,!0,!1),w=b=>b<1e-8?os(b,f+1,1,!1,!1):os(1-b,f+1,1,!0,!1),d;if(a==="less")d=[0,w(1-l)];else if(a==="greater")d=[p(1-l),1/0];else{let b=(1-l)/2;d=[p(b),w(b)]}return{method:"Exact Poisson test",alternative:a,statistic:f,statisticName:"number of events",parameter:m,parameterName:"time base",pValue:h,confInt:[d[0]/m,d[1]/m],confLevel:l,estimate:f/m,estimateNames:"event rate",nullValue:c,nullValueName:"event rate",warnings:[]}}var If=["p","alternative","confLevel","correct"],yd="Chi-squared approximation may be incorrect";function pi(e,t,n={}){ct(t)&&B(t,If,"propTest"),B(n,If,"propTest");let r,o,s=n;if(typeof e!="number"&&e.length>0&&Array.isArray(e[0])){let S=e;if(S.some(A=>A.length!==2))throw new RangeError("'x' must have 2 columns");r=S.map(A=>A[0]),o=S.map(A=>A[0]+A[1]),t!==void 0&&typeof t=="object"&&!Array.isArray(t)&&(s=t)}else{if(t===void 0||typeof t=="object"&&!Array.isArray(t))throw new RangeError("'n' must be given");r=typeof e=="number"?[e]:Array.from(e),o=typeof t=="number"?[t]:Array.from(t)}if(r.length!==o.length)throw new RangeError("'x' and 'n' must have the same length");let i=r.length,c=[];for(let S=0;S<i;S++)!Number.isNaN(r[S])&&!Number.isNaN(o[S])&&c.push(S);let a=c.length;if(a<1)throw new RangeError("not enough data");if(r=c.map(S=>r[S]),o=c.map(S=>o[S]),o.some(S=>!(S>0)))throw new RangeError("elements of 'n' must be positive");if(o.some(S=>S===1/0))throw new RangeError("elements of 'n' must be finite");if(r.some(S=>!(S>=0)))throw new RangeError("elements of 'x' must be nonnegative");if(r.some((S,A)=>S>o[A]))throw new RangeError("elements of 'x' must not be greater than those of 'n'");let l;if(s.p!==void 0){let S=typeof s.p=="number"?[s.p]:Array.from(s.p);if(S.length!==i)throw new RangeError("'p' must have the same length as 'x' and 'n'");if(l=c.map(A=>S[A]),l.some(A=>!(A>0&&A<1)))throw new RangeError("elements of 'p' must be in (0,1)")}let f=l!==void 0;a===1&&l===void 0&&(l=[.5]);let m=s.confLevel??.95;_t(m);let u=Ze(s.alternative);(a>2||a===2&&f)&&(u="two.sided");let h=r.map((S,A)=>S/o[A]),p=a===2?h[0]-h[1]:0,w=a===2?1/o[0]+1/o[1]:0,d=(s.correct??!0)&&a<=2?.5:0,b=!1;if(d>0&&a===1){let S=Math.abs(r[0]-o[0]*l[0]);S<=d&&([d,b]=[S,!0])}else if(d>0&&a===2&&!f){let S=Math.abs(p)/w;S<=d&&([d,b]=[S,!0])}let N=Md(r,o),v=o.some((S,A)=>{let C=l?.[A]??N;return S*C<5||S*(1-C)<5}),E=0;if(!b)for(let S=0;S<a;S++){let A=l?.[S]??N,C=o[S]*A,L=o[S]*(1-A);E+=Vt(Math.abs(r[S]-C)-d,C),E+=Vt(Math.abs(o[S]-r[S]-L)-d,L)}let g=l!==void 0?a:a-1,y;if(u==="two.sided")y=wt(E,g/2,2,!1,!1);else{let S=Math.sign(a===1?h[0]-l[0]:p);y=se(S*Math.sqrt(E),{lowerTail:u==="less"})}let x;if(a===1||a===2&&!f){let S=(1-m)/2,A=u==="two.sided"&&S<1e-8?le(S,{lowerTail:!1}):le(u==="two.sided"?(1+m)/2:m);if(a===1){let C=o[0],L=A*A,k=($,_)=>($+L/(2*C)+_*A*Math.sqrt($*(1-$)/C+L/(4*C*C)))/(1+L/C),F=h[0]+d/C,I=h[0]-d/C,O=Math.min(1,Math.max(0,F>=1?1:k(F,1))),q=Math.min(1,Math.max(0,I<=0?0:k(I,-1)));x=u==="two.sided"?[q,O]:u==="greater"?[q,1]:[0,O]}else{let C=Math.sqrt(h.reduce((I,O,q)=>I+O*(1-O)/o[q],0)),L=A*C+d*w,k=Math.max(-1,p-L),F=Math.min(1,p+L);x=u==="two.sided"?[k,F]:u==="greater"?[k,1]:[-1,F]}}let M=a===1?"p":c.map(S=>`prop ${S+1}`),T={method:`${a===1?"1-sample proportions test":`${a}-sample test for ${f?"given proportions":"equality of proportions"}`} ${d!==0?"with":"without"} continuity correction`,alternative:u,statistic:E,statisticName:"X-squared",parameter:g,parameterName:"df",pValue:y,estimate:a===1?h[0]:h,estimateNames:M,warnings:v?[yd]:[]};return l!==void 0&&(T.nullValue=a===1?l[0]:l,T.nullValueName=M),x!==void 0&&(T.confInt=x,T.confLevel=m),Q("propTest",T)}function Md(e,t){let n=t.reduce((o,s)=>o+s,0);if(n<1/0)return e.reduce((o,s)=>o+s,0)/n;let r=Math.ceil(Math.log2(t.length));return e.reduce((o,s)=>o+V(s,-r),0)/t.reduce((o,s)=>o+V(s,-r),0)}var Ed=["method","confLevel","sides"],xd=["wilson","wald","wilsoncc","agresti-coull","jeffreys","clopper-pearson","midp"],Rd=["two.sided","left","right"];function Co(e,t){let n=Se(t,Rd),r=e??.95;if(!(typeof r=="number"&&r>0&&r<1))throw new RangeError("'confLevel' must be a single number in (0, 1)");let o=n==="two.sided"?r:1-2*(1-r);if(!(o>=0))throw new RangeError("a one-sided 'confLevel' must be at least 0.5");let s=n!=="two.sided"&&1-r<1e-8?2*(1-r):1-o;return{...di(s,o),sides:n}}function di(e,t=1-e){let n=e/2<1e-8,r=n?le(e/2,{lowerTail:!1}):le(1-e/2),o=n?On(e,1,{lowerTail:!1}):On(t,1);return{sides:"two.sided",level:t,alpha:e,kappa:r,chisq:o}}function Ur(e,t){if(typeof e!="number")throw new TypeError(`'${t}' must be a number`);if(!vn(e))throw new RangeError(`'${t}' must be a nonnegative integer`);return Math.round(e)+0}function Bn(e,t,n,r){let o=Ur(e,n),s=Ur(t,r);if(s===0)throw new RangeError(`'${r}' must be positive`);if(s>2**53)throw new RangeError(`'${r}' must be at most 2^53`);if(o>s)throw new RangeError(`'${n}' must not be greater than '${r}'`);return[o,s]}function Ps(e,t,n){let r=e/t,o=1-r,s=n*n,i=(e+s/2)/(t+s),c=n*Math.sqrt(t)/(t+s)*Math.sqrt(r*o+s/(4*t)),a=i+c,l=i-c;return e===0?l=0:l<i/2&&(l=r*(e/(t+s))/a),[l,e===t?1:a]}function Ds(e,t,n,r){return r?e/2<1e-8?Xn(e/2,t,n,!1,!1):Xn(1-e/2,t,n,!0,!1):Xn(e/2,t,n,!0,!1)}function _f(e,t,n,r){let o=n/2,s=r?f=>{let m=Math.exp(f);return .5*hn(e,t,m,!1)+Dn(e-1,t,m,!0,!1)-o}:f=>{let m=Math.exp(f);return .5*hn(e,t,m,!1)+Dn(e,t,m,!1,!1)-o},i=r?-1:1,c=Math.log(e/t),a=i*s(c);if(a===0)return e/t;if(a>0){let f=c,m=a,u=1;for(;;){let h=c-u;if(h<-745)return 0;let p=i*s(h);if(p<=0)return Math.exp(p===0?h:Kt(w=>i*s(w),h,f,{tol:2**-60,fLower:p,fUpper:m}));f=h,m=p,u*=2}}let l=i*s(0);return l<=0?1:Math.exp(Kt(f=>i*s(f),c,0,{tol:2**-60,fLower:a,fUpper:l}))}function gi(e,t,n={}){B(n,Ed,"binomCI");let r=Se(n.method,xd),o=Co(n.confLevel,n.sides),[s,i]=Bn(e,t,"x","n"),{alpha:c,kappa:a}=o,l=s/i,f=1-l,m,u;switch(r){case"wald":{let h=a*Math.sqrt(l*f/i);[m,u]=[l-h,l+h];break}case"wilson":[m,u]=Ps(s,i,a);break;case"wilsoncc":{let h=a*a,p=2*(i+h),w=2*s+h-1,d=a*Math.sqrt(h-2-1/i+4*l*(i*f+1));m=l===0?0:w-d<w/2?(2*s-1)/(2*i)*(2*s-1)/(w+d):(w-d)/p,u=l===1?1:(2*s+h+1+a*Math.sqrt(h+2-1/i+4*l*(i*f-1)))/p;break}case"agresti-coull":{let h=a*a,p=i+h,w=(s+h/2)/p,d=a*Math.sqrt(w*(1-w)/p);[m,u]=[w-d,w+d];break}case"jeffreys":m=s===0?0:Ds(c,s+.5,i-s+.5,!1),u=s===i?1:Ds(c,s+.5,i-s+.5,!0);break;case"clopper-pearson":m=s===0?0:Ds(c,s,i-s+1,!1),u=s===i?1:Ds(c,s+1,i-s,!0);break;case"midp":m=s===0?0:s===i?Math.exp(Math.log(c)/i):_f(s,i,c,!1),u=s===i?1:s===0?-Math.expm1(Math.log(c)/i):_f(s,i,c,!0);break}return m=Math.max(0,m),u=Math.min(1,u),o.sides==="left"?u=1:o.sides==="right"&&(m=0),Q("binomCI",{estimate:l,confInt:[m,u],confLevel:n.confLevel??.95,method:r,sides:o.sides,warnings:[]})}function Fo(e){return e===1/0?1:e/(1+e)}function Oo(e,t){return e===0?0:t>0?e*e/t:1/0}function Td(e,t,n,r){return e/t+n/r>1}function Sd(e,t,n,r){return $s(e,t,n,r,0)}function vi(e,t,n,r,o){if(Td(e,t,n,r)){let[p,w,d,b]=vi(t-e,t,r-n,r,-o);return[w,p,b,d]}let s=o<=0,[i,c,a,l]=s?[e,t,n,r]:[n,r,e,t],f=Math.abs(o),m=1-f,u=0;if(m>0){let p=b=>{let N=0;return i>0&&(N+=i/b),i<c&&(N-=(c-i)/(1-b)),a>0&&(N+=a/(b+f)),a<l&&(N-=(l-a)/(m-b)),N},w=b=>{let N=0;return i>0&&(N-=i/(b*b)),i<c&&(N-=(c-i)/((1-b)*(1-b))),a>0&&(N-=a/((b+f)*(b+f))),a<l&&(N-=(l-a)/((m-b)*(m-b))),N};if(p(0)<=0)u=0;else if(p(m)>=0)u=m;else{let b=0,N=m,v=Ad(e,t,n,r,o);u=s?v:v-o,u>b&&u<N||(u=N/2);for(let E=0;E<2e3;E++){let g=p(u);if(g===0)break;g>0?b=u:N=u;let y=u-g/w(u);if(y>b&&y<N||(y=b===0?N/2:N>2*b?Math.sqrt(b*N):b+(N-b)/2),!(y>b&&y<N))break;let x=Math.abs(y-u);if(u=y,x<=2**-53*u)break}}}let h=[u,1-u,Math.min(1,u+f),Math.max(0,m-u)];return s?h:[h[2],h[3],h[0],h[1]]}function Ad(e,t,n,r,o){let s=e/t,i=n/r,c=r/t,a=1+c,l=-(1+c+s+c*i+o*(c+2)),f=o*o+o*(2*s+c+1)+s+c*i,m=-s*o*(1+o),u=l/(3*a),h=u*u*u-l*f/(6*a*a)+m/(2*a),p=Math.sign(h)*Math.sqrt(Math.max(0,u*u-f/(3*a)));if(p===0)return-u;let w=(Math.PI+Math.acos(Math.max(-1,Math.min(1,h/(p*p*p)))))/3;return 2*p*Math.cos(w)-u}function Ni(e,t,n,r,o,s){if(s==="wald")return e/t*((t-e)/t)/t+n/r*((r-n)/r)/r;let[i,c,a,l]=vi(e,t,n,r,o),f=i*c/t+a*l/r;if(s==="mee")return f;let m=t+r;return f*m/(m-1)}function qd(e,t,n,r,o,s,i){let c=Le(Le(ft([e,0],t),ft([n,0],r)),[o,0]),[a,l,f,m]=vi(e,t,n,r,o),u=he(ft(jt(a,l),t),ft(jt(f,m),r));return s==="mn"&&(u=ft(Et(u,t+r),t+r-1)),Le(_e(c,c),Et(u,i))[0]}function $s(e,t,n,r,o){return Le(Le(ft([e,0],t),ft([n,0],r)),[o,0])[0]}function kd(e,t,n,r,o,s){return Oo($s(e,t,n,r,o),Ni(e,t,n,r,o,s))}var Vs=new Float64Array(1),wi=new BigInt64Array(Vs.buffer);function Lf(e){return Vs[0]=Math.abs(e),e<0?-wi[0]:wi[0]}function sr(e){return wi[0]=e<0n?-e:e,e<0n?-Vs[0]:Vs[0]}function bi(e,t,n,r,o){let s=e(t);if(s===0||Number.isNaN(s))return t;let i=s<0===o,c=Lf(i?r:n),a=Lf(t),l=s,f=a,m=s,u=1n;for(;;){if(f=i?a+u:a-u,(i?f>=c:f<=c)&&(f=c),m=e(sr(f)),m===0)return sr(f);if(m<0!=l<0||f===c)break;if(a=f,l=m,u*=2n,u>1n<<40n)return sr(a)}if(m<0==l<0||Number.isNaN(m))return sr(a);for(;f-a>1n||a-f>1n;){let h=(a+f)/2n,p=e(sr(h));if(p===0)return sr(h);p<0==l<0?(a=h,l=p):(f=h,m=p)}return Math.abs(m)<Math.abs(l)?sr(f):sr(a)}function Ff(e,t,n,r,o,s){let i=Sd(e,t,n,r);if(o===0)return[i,i];let c=Fo(o),a=p=>(p===i?0:Fo(kd(e,t,n,r,p,s)))-c,l=(p,w,d,b)=>{if(d===0)return p;if(b===0)return w;let N=Kt(a,p,w,{tol:1e-300,fLower:d,fUpper:b});return s==="wald"?N:bi(v=>qd(e,t,n,r,v,s,o),N,p,w,d<0)},f=a(-1),m=a(1),u=i<=-1||f<=0?-1:l(-1,i,f,-c),h=i>=1||m<=0?1:l(i,1,-c,m);return[u,h]}function Cf(e,t,n){let r=t,o=e(t);if(o>=0)return Math.exp(t);let s=1;for(;;){let i=t+n*s;if(Math.abs(i)>745)return n<0?0:1/0;let c=e(i);if(c>=0){if(c===0)return Math.exp(i);let a=n>0?Kt(e,r,i,{tol:2**-60,fLower:o,fUpper:c}):Kt(e,i,r,{tol:2**-60,fLower:c,fUpper:o});return Math.exp(a)}r=i,o=c,s*=2}}function Id(e,t,n,r,o){let s=t+r,i=o*(t+n)+e+r,c=o*(e+n),a=Math.sqrt(Math.max(0,i*i-4*s*c)),l=Math.min(1,o,c===0?0:2*c/(i+a));return[l,Math.min(1,l/o)]}function _d(e,t,n,r,o){let[s,i]=Id(e,t,n,r,o);if(!(o>0&&o<1/0))return[s,1-s,i,1-i];let c=s<=.5&&i<=.5?0:o*(1-i)<=1-s?2:3,a=c===0?i:c===2?1-i:1-s,l=y=>c===0?[y,0]:c===2?Ot(1,-y):ft(Ot(1,-y),o),f=c===3?1/o:1,m=c===0?Math.min(1,1/o):1,u=c===2?o>1?ft(Ot(o,-1),o)[0]:0:c===3&&o<1?Ot(1,-o)[0]:0,h=y=>{let x=l(y),M=Et(x,o);return[M[0],Le([1,0],M)[0],x[0],Le([1,0],x)[0]]},p=e+n,w=c<2?1:-1,d=y=>{let[,x,M,R]=h(y),T=0,S=0;return p>0&&(T+=M>0?p/M:1/0,S-=p/(M*M)),e<t&&(T-=x>0?o*(t-e)/x:1/0,S-=o*(t-e)/x*(o/x)),n<r&&(T-=R>0?(r-n)/R:1/0,S-=(r-n)/(R*R)),[w*T,S*f]},b;if(!(m>u))b=u;else if(!(d(u)[0]>0))b=u;else if(!(d(m)[0]<0))b=m;else{let y=u,x=m;b=a,b>y&&b<x||(b=y+(x-y)/2);for(let M=0;M<2e3;M++){let[R,T]=d(b);if(R===0)break;R>0?y=b:x=b;let S=b-R/T;if(S>y&&S<x||(S=y<=0?x/2:x>2*y?Math.sqrt(y*x):y+(x-y)/2),!(S>y&&S<x))break;let A=Math.abs(S-b);if(b=S,A<=2**-53*b)break}}let[N,v,E,g]=h(b);return[Math.min(1,Math.max(0,N)),Math.min(1,Math.max(0,v)),Math.min(1,Math.max(0,E)),Math.min(1,Math.max(0,g))]}function Ld(e,t,n,r,o){let[s,i,c,a]=_d(e,t,n,r,o),l=(f,m,u,h)=>u>.5?m*h-(m-f):f-m*u;return Oo(l(e,t,s,i),t*s*i)+Oo(l(n,r,c,a),r*c*a)}function Of(e,t,n,r,o){let s=e/t/(n/r);return Pf(s,o,i=>Ld(e,t,n,r,i))}function Cd(e,t,n,r,o){let s=e+n,i=r*(o-1),c=t*o+r-s*(o-1),a=s===0?0:Math.min(1,2*s/(c+Math.sqrt(Math.max(0,c*c+4*i*s))));return[o===1/0?a>0?1:0:Math.min(1,a*o/(1+a*(o-1))),a]}function Fd(e,t,n,r,o){let[s,i]=Cd(e,t,n,r,o),c=e-t*s;return Oo(c,t*s*(1-s))+Oo(c,r*i*(1-i))}function Df(e,t,n,r,o){let s=e*(r-n)/((t-e)*n);return Pf(s,o,i=>Fd(e,t,n,r,i))}function Pf(e,t,n){if(t===0)return[e,e];let r=Fo(t),o=l=>n(l)-t,s=l=>l>0&&l<e?bi(o,l,Number.MIN_VALUE,e,!1):l,i=l=>l>e&&l<1/0?bi(o,l,e,Number.MAX_VALUE,!0):l;if(e===0||e===1/0){let l=u=>Fo(n(u))-r,f=l(1),m=f===0?1:rr(l,e===0?f>0:f<0);return e===0?[0,i(m)]:[s(m),1/0]}let c=Math.log(e),a=l=>(l===c?0:Fo(n(Math.exp(l))))-r;return[s(Cf(a,c,-1)),i(Cf(a,c,1))]}var Od=["method","confLevel","sides"],Dd=["method","confLevel","sides"],Pd=["method","confLevel"],Vd=["ac","wald","score","mn","mee"];function yi(e,t,n,r,o,s){let{kappa:i}=s,c=e/t,a=n/r,l=c-a,f,m;switch(o){case"wald":{let u=i*Math.sqrt(c*(1-c)/t+a*(1-a)/r);[f,m]=[Math.max(-1,l-u),Math.min(1,l+u)];break}case"ac":{let u=(e+1)/(t+2),h=(n+1)/(r+2),p=u-h,w=i*Math.sqrt(u*(1-u)/(t+2)+h*(1-h)/(r+2));[f,m]=[Math.max(-1,p-w),Math.min(1,p+w)];break}case"score":{let[u,h]=Ps(e,t,i),[p,w]=Ps(n,r,i);f=l-i*Math.sqrt(u*(1-u)/t+w*(1-w)/r),m=l+i*Math.sqrt(h*(1-h)/t+p*(1-p)/r);break}case"mn":case"mee":[f,m]=Ff(e,t,n,r,s.chisq,o);break}return[Math.min(f,m),Math.max(f,m)]}function $d(e,t,n,r,o={}){B(o,Od,"binomDiffCI");let s=Se(o.method,Vd),i=Co(o.confLevel,o.sides),[c,a]=Bn(e,t,"x1","n1"),[l,f]=Bn(n,r,"x2","n2"),m=c/a-l/f,[u,h]=yi(c,a,l,f,s,i);return i.sides==="left"?h=1:i.sides==="right"&&(u=-1),Q("binomDiffCI",{estimate:m,confInt:[u,h],confLevel:o.confLevel??.95,method:s,sides:i.sides,warnings:[]})}var zd=["katz.log","koopman"];function zs(e,t,n,r,o){let s=e/t/(n/r),i=o*Math.sqrt(1/e-1/t+1/n-1/r);return[s*Math.exp(-i),s*Math.exp(i)]}function jd(e,t,n,r,o={}){B(o,Dd,"binomRatioCI");let s=Se(o.method,zd),i=Co(o.confLevel,o.sides),[c,a]=Bn(e,t,"x1","n1"),[l,f]=Bn(n,r,"x2","n2"),m=i.kappa,u=c===0?0:c/a/(l/f),h,p;return c===0&&l===0?[h,p]=[0,1/0]:s==="katz.log"?c===0?[h,p]=[0,zs(.5,a,l,f,m)[1]]:l===0?[h,p]=[zs(c,a,.5,f,m)[0],1/0]:c===a&&l===f?[h,p]=zs(a-.5,a,f-.5,f,m):[h,p]=zs(c,a,l,f,m):[h,p]=Of(c,a,l,f,i.chisq),i.sides==="left"?p=1/0:i.sides==="right"&&(h=0),Q("binomRatioCI",{estimate:u,confInt:[h,p],confLevel:o.confLevel??.95,method:s,sides:i.sides,warnings:[]})}var Wd=["midp","fisher","wald","score"],Bd={midp:"median-unbiased estimate & mid-p exact CI",fisher:"Conditional MLE & exact CI from 'fisher.test'",wald:"Unconditional MLE & normal approximation (Wald) CI",score:"Unconditional MLE & score CI"},Kd=2**23,Ud=2**20;function Vf(e,t,n){let r=[0,0];for(let o=Math.max(0,t);o<=Math.min(n,e.length-1);o++)e[o]!==0&&(r=he(r,[e[o],0]));return r[0]}function Hd(e,t={}){B(t,Pd,"oddsRatio");let n=Se(t.method,Wd);if(e.length!==2||e.some(S=>S.length!==2))throw new TypeError("'table' must be a 2 x 2 table");let r=t.confLevel??.95,{alpha:o,kappa:s,chisq:i}=Co(r,void 0),c=e.map((S,A)=>Array.from(S,(C,L)=>Ur(C,`table[${A}][${L}]`))),[[a,l],[f,m]]=c,u=a+l+f+m;if(u===0)throw new RangeError("all entries of the table are zero");if(!(u<=2**53))throw new RangeError("the total of the table must be at most 2^53");let h=Math.max(0,a-m),p=a+Math.min(l,f),w=p-h+1;if(n==="midp"||n==="fisher"){if(w>Kd)throw new RangeError(`the exact distribution of the 2 x 2 table has ${w} values, more than 2^23: use method "wald" or "score"`);if(n==="fisher"&&Math.max(a,l,f,m)>2147483647)throw new RangeError(`'table' has entries too large for the conditional MLE (above 2^31 - 1): use method "wald" or "score"`)}let d,b,N;try{d=sn(a-1,a+f,l+m,a+l,!0,!1)+.5*bn(a,a+f,l+m,a+l,!1),b=sn(a,a+f,l+m,a+l,!1,!1)+.5*bn(a,a+f,l+m,a+l,!1),N=w<=Ud?Ja(new Br(a+f,l+m,a+l),a,1):ff(a,a+f,l+m,a+l)}catch(S){throw S instanceof RangeError?new RangeError(`the exact p-values of the table are beyond reach: ${S.message}`):S}let v=2*Math.min(d,b),E=[a+l,f+m],g=[a+f,l+m],y=0,x=!1;for(let S=0;S<2;S++)for(let A=0;A<2;A++){let C=E[A]*g[S]/u;C<5&&(x=!0);let L=Math.abs(c[A][S]-C);y+=L*L/C}let M=wt(y,.5,2,!1,!1),R,T;switch(n){case"wald":{R=a*m/(l*f);let S=s*Math.sqrt(1/a+1/l+1/f+1/m);T=[Math.exp(Math.log(R)-S),Math.exp(Math.log(R)+S)];break}case"fisher":{let S=ot(()=>Io(c,{confLevel:r}));R=S.estimate,T=S.confInt;break}case"score":{R=a*m/(l*f),T=(a===0||m===0)&&(l===0||f===0)?[0,1/0]:Df(a,a+l,f,f+m,i);break}case"midp":{let S=new Br(a+f,l+m,a+l),A=F=>{if(F===1)return[d,b];let I=S.density(F),O=a-h,q=.5*I[O];return[Vf(I,0,O-1)+q,Vf(I,O+1,I.length-1)+q]},C=(F,I)=>{let O=F(1);return O===0?1:rr(F,I?O>0:O<0)};R=a===h?0:a===p?1/0:C(F=>A(F)[0]-.5,!1);let L=a===h?0:C(F=>A(F)[1]-o/2,!0),k=a===p?1/0:C(F=>A(F)[0]-o/2,!1);T=[L,k];break}}return Q("oddsRatio",{estimate:R,confInt:T,confLevel:r,method:Bd[n],pValue:{midp:v,fisher:N,chiSquare:M},warnings:x?["Chi-squared approximation may be incorrect"]:[]})}var Yd=["epsilon","paired","varEqual","confLevel","alpha"],Gd=["margin","method","alpha"];function $f(e,t){if(!(typeof e=="number"&&e>0&&e<.5))throw new RangeError(`'${t}' must be a single number in (0, 0.5)`)}function Xd(e,t,n={}){B(n,Yd,"tost");let r=n.epsilon??1;if(!(typeof r=="number"&&r>0&&r<1/0))throw new RangeError("'epsilon' must be a positive number");let o,s;if(n.alpha!==void 0)$f(n.alpha,"alpha"),o=1-n.alpha,s=n.alpha;else{if(o=n.confLevel??.95,_t(o),!(o>.5))throw new RangeError("'confLevel' must be greater than 0.5");s=1-o}if(t===void 0)throw new TypeError("'y' is required");let i=n.paired??!1,c=n.varEqual??!1,a=ot(()=>Nn(e,t,{paired:i,varEqual:c})),l=a.stdErr,f=a.parameter,m=typeof a.estimate=="number"?a.estimate:a.estimate[0]-a.estimate[1],u=dt((r-Math.abs(m))/l,f,!1,!1),h=s<1e-8?gt(s,f,!1,!1):gt(o,f,!0,!1),p=[m-h*l,m+h*l];return Q("tost",{method:a.method.replace("t-test","TOST"),parameter:f,parameterName:"df",pValue:u,result:p[0]>-r&&p[1]<r?"rejected":"not rejected",confInt:p,confLevel:o,estimate:a.estimate,estimateNames:i?"mean of the differences":a.estimateNames,epsilon:r,seDiff:l,warnings:[]})}var Qd=["mn","mee","wald"];function Zd(e,t,n,r,o={}){B(o,Gd,"binomDiffEquivalenceTest");let s=Se(o.method,Qd),i=o.margin;if(i===void 0)throw new TypeError("'margin' is required");if(!(typeof i=="number"&&i>0&&i<1))throw new RangeError("'margin' must be a single number in (0, 1)");let c=o.alpha??.05;$f(c,"alpha");let[a,l]=Bn(e,t,"x1","n1"),[f,m]=Bn(n,r,"x2","n2"),u=a/l-f/m,h=E=>$s(a,l,f,m,E)/Math.sqrt(Ni(a,l,f,m,E,s)),p=h(-i),w=h(i),d=[se(p,{lowerTail:!1}),se(w)],b=Math.max(d[0],d[1]),N=1-2*c,v=yi(a,l,f,m,s,di(2*c,N));return Q("binomDiffEquivalenceTest",{method:s,estimate:u,margin:i,alpha:c,statistic:[p,w],statisticName:"z",pValues:d,pValue:b,result:d[0]<c&&d[1]<c?"rejected":"not rejected",confInt:v,confLevel:N,warnings:[]})}var Ut=new WeakMap;function Ws(e){let t=(n,r)=>n.weights===void 0?Array.from(r):r.filter((o,s)=>n.weights[s]!==0);return qe(e.flatMap(n=>[t(n,n.fittedValues),t(n,n.residuals)])).k}function Bs(e,t){return e.map(n=>{if(n.rss>=Jd&&n.rss<1/0)return V(n.rss,-2*t);let r=0;return n.residuals.forEach((o,s)=>{let i=n.weights?.[s]??1;if(i===0)return;let c=V(o,-t);r+=i*c*c}),r})}var Jd=2**-1022;function Ei(e,t){return e.y.flatMap((n,r)=>(e.weights?.[r]??1)===0?[]:[[e.weights?.[r]??1,V(n,-t)]])}function Ks(e){let t=Math.floor(Math.log2(e.reduce((n,[r])=>Math.max(n,r),0)));return t-(t%2+2)%2}function Er(e,t,n=0){let r=i=>e.reduce((c,[a,l])=>c+V(a,-i)*l*l,0),o=n,s=r(o);return Number.isFinite(s)||(o=Ks(e),s=r(o)),!(V(t,-o)>=1e-24*s)}function zf(e){let t=Ws([e]),{pivot:n}=e.qr,r=[],o=new Map;for(let s=0;s<e.rank;s++){let i=e.assign[n[s]],c=o.get(i);c===void 0&&(c={term:i,df:0,ss:0},o.set(i,c),r.push(c)),c.df++,c.ss+=V(e.effects[s],-t)**2}return{terms:r,rdf:e.dfResidual,rss:Bs([e],t)[0],k:t}}var jf=(e,t,n)=>ht(e,t,n,!1,!1),xi="essentially perfect fit for \u20181\u2019: summary may be unreliable";function Ri(e,t,n){if(!(n>0))return!1;let r=e.length,o=0;for(let a of e)o+=a;o/=r;let s=0;for(let a of e)s+=(a-o)**2;let i=r>1?s/(r-1):0,c=t/n;return Number.isFinite(c)&&c<(o*o+i)*1e-30}var Ti=new WeakMap;function e0(e){let t=zf(e),n=t.terms.filter(o=>o.term!==0),r=0;return e.fittedValues.forEach((o,s)=>{let i=e.weights?.[s]??1;i!==0&&(r+=i*V(o,-t.k)**2)}),{names:n.map(o=>e.termLabels[o.term-1]),rows:n,rdf:t.rdf,rss:t.rss,k:t.k,perfect:t.rss<1e-10*r}}function Mi(e){let t=e0(e),{names:n,rdf:r,k:o,perfect:s}=t,{rows:i,rss:c}=t;if(r>0){let w=Ei(e,o);Er(w,c)&&(c=0,i=i.map(d=>Er(w,d.ss)?{...d,ss:0}:d))}let a=c/r,l=w=>V(w,2*o),f=i.map(({df:w,ss:d})=>{let b=d/w/a;return[w,l(d),l(d/w),b,jf(b,w,r)]});f.push([r,l(c),r===0?NaN:l(a),NaN,NaN]);let m=["Df","Sum Sq","Mean Sq","F value","Pr(>F)"],u=[...n,"Residuals"],h={colNames:m,rowNames:u,table:f,warnings:s?["ANOVA F-tests on an essentially perfect fit are unreliable"]:[]},p=i.map(({df:w,ss:d})=>[w,d]);return p.push([r,c]),Ut.set(h,{colNames:["Df","Sum Sq"],rowNames:u,table:p}),h}function t0(e){let t=e.map(f=>f.dfResidual),n=Ws(e),r=Bs(e,n),o=f=>V(f,2*n),s=0;t.forEach((f,m)=>{f<t[s]&&(s=m)});let i=r[s]/t[s],c=0;e[s].fittedValues.forEach((f,m)=>{let u=e[s].weights?.[m]??1;u!==0&&(c+=u*V(f,-n)**2)});let a=r[s]<1e-10*c,l=e.map((f,m)=>{if(m===0)return[t[0],o(r[0]),NaN,NaN,NaN,NaN];let u=t[m-1]-t[m],h=r[m-1]-r[m],[p,w]=u>0?[e[m-1],e[m]]:[e[m],e[m-1]],d=N=>N.filter((v,E)=>(p.weights?.[E]??1)!==0),b=u===0||!Es(d(p.x),d(w.x))?NaN:h/u/i;return b<0&&(b=NaN),[t[m],o(r[m]),u,o(h),b,Number.isNaN(b)?NaN:jf(b,Math.abs(u),t[s])]});return{colNames:["Res.Df","RSS","Df","Sum of Sq","F","Pr(>F)"],rowNames:e.map((f,m)=>String(m+1)),table:l,warnings:a?["ANOVA F-tests on an essentially perfect fit are unreliable"]:[]}}function js(...e){let t=[];for(let n of e)for(let r of n??[])t.includes(r)||t.push(r);return t}function Si(e,...t){let n=[e,...t];for(let c of n)if(typeof c!="object"||c===null||!Array.isArray(c.effects))throw new TypeError("anova() needs fits from lm() or aov() with a formula");if(n.length===1){let c=Mi(e),a={...c,warnings:js(e.warnings,c.warnings)},l=Ut.get(c);return l!==void 0&&Ut.set(a,l),Q("anova",a,e)}if(n.some(c=>c.rows.length!==e.rows.length))throw new Error("models were not all fitted to the same size of dataset");let r=(c,a)=>c===a||c!==void 0&&a!==void 0&&c.length===a.length&&c.every((l,f)=>Object.is(l,a[f]));if(n.some(c=>!r(c.rows,e.rows)))throw new Error("models were not all fitted to the same rows of the data");let o=n.filter(c=>c.response===e.response);if(o.some(c=>!r(c.y,e.y)))throw new Error("models were not all fitted to the same response values");if(o.some(c=>!r(c.weights,e.weights)))throw new Error("models were not all fitted with the same weights");let s=[];if(o.length<n.length){let c=[...new Set(n.filter(a=>a.response!==e.response).map(a=>a.response))];s.push(`models with response ${c.map(a=>`\u2018${a}\u2019`).join(", ")} removed because response differs from model 1`)}let i=o.length===1?Mi(e):t0(o);return i.warnings=js(...n.map(c=>c.warnings),s,i.warnings),Q("anova",i,...n)}function Ai(e){if(typeof e=="object"&&e!==null&&e.multistratum===!0){let i=e,c=js(i.warnings,Ti.get(i)),a={strata:i.strata.filter(f=>f.stratum!=="Error: (Intercept)").map(f=>({...f})),warnings:c},l=Ut.get(i);return Array.isArray(l)&&Ut.set(a,l.filter((f,m)=>i.strata[m].stratum!=="Error: (Intercept)")),Q("aovSummary",a,e)}if(typeof e!="object"||e===null||!Array.isArray(e.effects))throw new TypeError("aovSummary() needs a fit from aov() with a formula");let t=e,n=Mi(t),r=zf(t),o=Ri(t.fittedValues.map(i=>V(i,-r.k)),r.rss,r.rdf),s=js(t.warnings,n.warnings,o?[xi]:[]);if(t.dfResidual>0){let i={...n,warnings:s},c=Ut.get(n);return c!==void 0&&Ut.set(i,c),Q("aovSummary",i,e)}return Q("aovSummary",{colNames:n.colNames.slice(0,3),rowNames:n.rowNames.slice(0,-1),table:n.table.slice(0,-1).map(i=>i.slice(0,3)),warnings:s},e)}var n0=["pooledSd","mu","paired","ci","alternative","adjust"],r0=["pooledSd","mu","paired","ci","alternative"],o0=["generalized","partial","ci","alternative"],s0=["partial","ci","alternative"],a0=["partial","ci","alternative"];function Ii(e){if(e!==null&&!(e>0&&e<1))throw new RangeError("ci must be a single numeric value between (0, 1)")}var i0="Missing values detected. NAs dropped.",c0=`'y' is numeric but has only 2 unique values.
|
|
3
3
|
If this is a grouping variable, convert it to a factor.`,Uf={tol:0,maxiter:5e3};function xr(e){return e===1/0?1:Math.exp(ll(.5,(e-1)/2)-.5*Math.log(e/2))}function Wf(e,t,n){let r=e-1<e+1?1:Math.abs(e)*Number.EPSILON;return br(o=>Qt(e,t,{ncp:o})-n,e-r,e+r,{...Uf,extendInt:"downX"}).root}function Hf(e,t,n,r){let o=n.pooledSd??!0,s=n.mu??0,i=n.paired??!1,c=n.ci===void 0?.95:n.ci;Ii(c);let a=Se(n.alternative,["two.sided","less","greater"]),l=Array.from(e),f=t===void 0?void 0:Array.from(t),m=[];f!==void 0&&new Set(Dt(f)).size===2&&m.push(c0),(l.some(Number.isNaN)||(f?.some(Number.isNaN)??!1))&&m.push(i0);let u=S=>S===1/0||S===-1/0;if(l.some(u)||(f?.some(u)??!1))throw new RangeError("the observations must be finite (NaN is dropped as missing)");let h,p,w,d,b,N;if(f===void 0||i){let S;if(f===void 0)({groups:[S],k:N}=qe([Dt(l)]));else{let[C,L]=Jn(l,f);({d:S,k:N}=ls(C,L))}let A=S.length;if(A<2)throw new RangeError(f===void 0?"not enough observations: at least 2 are needed":"not enough complete pairs: at least 2 are needed");h=it(S),p=Math.sqrt(at(S)),w=p/Math.sqrt(A),d=1/A,b=A-1}else{let S=xt([Dt(l),Dt(f)]);N=S.k;let[A,C]=S.n;if(A<2||C<2)throw new RangeError("not enough observations: at least 2 are needed in each group");h=S.mean[0]-S.mean[1];let L=Math.sqrt(S.variance[0]),k=Math.sqrt(S.variance[1]);o?(p=Math.sqrt(((A-1)*L**2+(C-1)*k**2)/(A+C-2)),w=p*Math.sqrt(1/A+1/C),d=1/A+1/C,b=A+C-2):(p=Math.sqrt((L**2+k**2)/2),d=2*(C*L**2+A*k**2)/(A*C*(L**2+k**2)),w=Math.sqrt(Math.sqrt(L**2/A)**2+Math.sqrt(k**2/C)**2),b=fs([L**2/A,k**2/C],[A-1,C-1]))}let v=r?xr(b):1,E=V(s,-N),g=(h-E)/p*v,y=r?"Hedges_g":"Cohens_d";if(c===null)return{name:y,value:g,warnings:m};let x=a==="two.sided"?c:2*c-1,M=(h-E)/w,R=Wf(M,b,(1+x)/2)*Math.sqrt(d)*v,T=Wf(M,b,(1-x)/2)*Math.sqrt(d)*v;return a==="less"&&(R=-1/0),a==="greater"&&(T=1/0),{name:y,value:g,ci:c,ciLow:R,ciHigh:T,warnings:m}}function l0(e,t,n={}){return B(n,n0,"cohensD"),Q("cohensD",Hf(e,t,n,n.adjust??!1))}function f0(e,t,n={}){return B(n,r0,"hedgesG"),Q("hedgesG",Hf(e,t,n,!0))}var m0=e=>Array.isArray(e.effects),qi=e=>e.multistratum===!0,Yf=e=>Array.isArray(e.strata),Gf=e=>{let t=e;return typeof t.univariate=="object"&&typeof t.anova=="object"},ki=e=>({test:t=>t.split(":").includes(e)});function u0(e){let t=e.strata.filter(o=>o.stratum!=="Error: (Intercept)").map(o=>{let s=o.stratum.replace(/^Error: /,""),i=o.colNames.indexOf("Df"),c=o.colNames.indexOf("Sum Sq"),a=o.rowNames.map((f,m)=>({name:f,df:o.table[m][i],ss:o.table[m][c]})),l=a.find(f=>f.name==="Residuals");if(l===void 0)throw new Error(`the error stratum '${s}' has no residual degrees of freedom`);return{name:s,terms:a.filter(f=>f!==l),sse:l.ss,dfe:l.df}});if(t.length===0)throw new TypeError("the fit has no error strata");let n=t[0].name.split(":")[0],r=t.find(o=>o.name===n);if(r===void 0)throw new Error(`there is no error stratum '${n}'`);return{strata:t,subject:r}}function h0(e){let t=e.univariate,n=t.colNames.indexOf("Sum Sq"),r=[],o=(a,l,f)=>{let m=r.find(u=>u.name===a);return m===void 0&&r.push(m={name:a,terms:[],sse:l,dfe:f}),m},s=new Map;if(t.colNames.includes("Error SS")){let a=t.colNames.indexOf("num Df"),l=t.colNames.indexOf("Error SS"),f=t.colNames.indexOf("den Df");t.rowNames.forEach((m,u)=>{let h=t.table[u],p=e.within.filter(b=>ki(b).test(m)),w=o(p.length===0?"Subject":`Subject:${p.join(":")}`,h[l],h[f]);if(m==="(Intercept)")return;let d={name:m,df:h[a],ss:h[n]};w.terms.push(d),s.set(m,d)})}else{let a=t.colNames.indexOf("Df"),l=t.rowNames.indexOf("Residuals"),f=o("Subject",t.table[l][n],t.table[l][a]);t.rowNames.forEach((m,u)=>{if(m==="(Intercept)"||m==="Residuals")return;let h={name:m,df:t.table[u][a],ss:t.table[u][n]};f.terms.push(h),s.set(m,h)})}let i=r.find(a=>a.name==="Subject");if(i===void 0)throw new Error("the result has no subject stratum");let c=e.anova.rowNames.map(a=>{let l=s.get(a);if(l===void 0)throw new Error(`there is no univariate test of '${a}'`);return l});return{strata:r,subject:i,order:c}}function Xf(e,t){let n=Array.isArray(e)?e:[],r=n.filter(s=>!t.some(i=>ki(s).test(i.name)));if(r.length>0)throw new RangeError(`Observed variable not in data: ${r.join(", ")}`);let o=s=>n.some(i=>ki(i).test(s));return{isObserved:o,ssObserved:t.reduce((s,i)=>o(i.name)?s+i.ss:s,0)}}function Bf(e,t,n,r){let o=i=>Rn(e,t,n,{ncp:i})-r,s=o(0);return Number.isNaN(s)?NaN:s>0?e===1/0?1/0:br(o,0,Math.max(10,e*t*4),{...Uf,extendInt:"downX"}).root:0}function Qf(e,t,n,r,o){let s=o==="two.sided"?r:2*r-1,i=Math.max(0,e),c=i/t/((1-i)/n),a=Bf(c,t,n,(1+s)/2),l=Bf(c,t,n,(1-s)/2);c<=Gn((1-s)/2,t,n)&&(l=0),c<=Gn((1+s)/2,t,n)&&(a=0);let f=a===1/0?1:a/(a+n),m=l===1/0?1:l/(l+n);return o==="less"&&(f=0),o==="greater"&&(m=1),[e,r,f,m]}function p0(e){let t=qe([e.map(n=>n.ss)]).k;if(t!==0)for(let n of e)n.ss=V(n.ss,-t)}var Kf="needs a table of anova() or Anova(), a fit from lm() or aov() with a formula, its aovSummary(), or a result of aovEz()";function _i(e,t,n){if(typeof e!="object"||e===null)throw new TypeError(Kf);if(qi(e)||Yf(e)||Gf(e))return d0(e,t,n);let r=m0(e)?ot(()=>Si(e)):e;if(!Array.isArray(r.colNames)||!Array.isArray(r.table))throw new TypeError(Kf);let o=Ut.get(r),s=o!==void 0&&!Array.isArray(o)?o:r,i=n.ci===void 0?.95:n.ci;Ii(i);let c=Se(n.alternative??"greater",["two.sided","less","greater"]),a=s.colNames.indexOf("Df"),l=s.colNames.indexOf("Sum Sq");if(a<0||l<0)throw new TypeError("the table needs the columns 'Df' and 'Sum Sq'");let f=s.rowNames.map((R,T)=>({name:R,df:s.table[T][a],ss:s.table[T][l]})).filter(R=>R.name!=="(Intercept)");p0(f);let m=f.find(R=>R.name==="Residuals");if(m===void 0)throw new TypeError("the table has no row 'Residuals'");if(!(m.df>0))throw new Error("the model has no residual degrees of freedom: the effect sizes are not defined");let u=f.filter(R=>R!==m),h=m.df,p=m.ss,w=p/h,d=f.reduce((R,T)=>R+T.ss,0),b=f.reduce((R,T)=>R+T.df,0)+1,N=t==="Eta2"?n.generalized??!1:!1,E=u.length===1?"plain":N!==!1?"generalized":n.partial??!0?"partial":"plain",{isObserved:g,ssObserved:y}=Xf(N,u),x=R=>{let{ss:T,df:S}=R;if(t==="Eta2")return E==="partial"?T/(T+p):E==="plain"?T/d:T/(T+p+y-(g(R.name)?T:0));if(t==="Omega2"){let C=E==="partial"?(T-S*w)/(T+(b-S)*w):(T-S*w)/(d+w);return Math.max(0,C)}let A=E==="partial"?(T-S*w)/(T+p):(T-S*w)/d;return Math.max(0,A)},M=`${t}${E==="plain"?"":`_${E}`}`;return{colNames:i===null?[M]:[M,"CI","CI_low","CI_high"],rowNames:u.map(R=>R.name),table:u.map(R=>i===null?[x(R)]:Qf(x(R),R.df,h,i,c)),warnings:Array.isArray(r.warnings)?[...r.warnings]:[]}}function d0(e,t,n){let r=n.ci===void 0?.95:n.ci;Ii(r);let o=Se(n.alternative??"greater",["two.sided","less","greater"]),s=Gf(e),i,c,a;if(s){let g=Ut.get(e),y=h0(g!==void 0&&!Array.isArray(g)?{...e,univariate:g}:e);({strata:i,subject:c}=y),a=y.order.map(x=>({term:x,stratum:y.strata.find(M=>M.terms.includes(x))}))}else{let g=qi(e)?ot(()=>Ai(e)):e,y=Ut.get(g);({strata:i,subject:c}=u0(Array.isArray(y)?{...g,strata:y.map((x,M)=>({...g.strata[M],...x}))}:g)),a=i.flatMap(x=>x.terms.map(M=>({term:M,stratum:x})))}let l=qe([[...i.flatMap(g=>g.terms.map(y=>y.ss)),...i.map(g=>g.sse)]]).k;for(let g of i){g.sse=V(g.sse,-l);for(let y of g.terms)y.ss=V(y.ss,-l)}let f=i.flatMap(g=>g.terms),m=i.reduce((g,y)=>g+y.sse,0),u=f.reduce((g,y)=>g+y.ss,0)+m,h=c.sse/c.dfe,p=t==="Eta2"?n.generalized??!1:!1,w=p!==!1?"generalized":n.partial??!0?"partial":"plain",{isObserved:d,ssObserved:b}=Xf(p,f),N=(g,y)=>{let{ss:x,df:M}=g,R=y.sse/y.dfe;if(t==="Eta2")return w==="partial"?x/(x+y.sse):w==="plain"?x/u:x/(x+m+b-(d(g.name)?x:0));if(t==="Omega2"){let S=y===c?0:1,A=w==="partial"?(x-M*R)/(x+S*y.sse+c.sse+h):(x-M*R)/(u+h);return Math.max(0,A)}let T=w==="partial"?(x-M*R)/(x+y.sse):(x-M*R)/u;return Math.max(0,T)},v=`${t}${w==="plain"?"":`_${w}`}`,E={colNames:r===null?[v]:[v,"CI","CI_low","CI_high"],rowNames:a.map(g=>g.term.name),table:a.map(({term:g,stratum:y})=>{let x=N(g,y);return r===null?[x]:Qf(x,g.df,y.dfe,r,o)}),warnings:[]};return s||(E.groups=a.map(g=>g.stratum.name),E.warnings=[...qi(e)||Yf(e)?e.warnings:[]]),E}function g0(e,t={}){return B(t,o0,"etaSquared"),Q("etaSquared",_i(e,"Eta2",t),e)}function w0(e,t={}){return B(t,s0,"omegaSquared"),Q("omegaSquared",_i(e,"Omega2",t),e)}function b0(e,t={}){return B(t,a0,"epsilonSquared"),Q("epsilonSquared",_i(e,"Epsilon2",t),e)}var v0=new Set(["eqb","lowEqbound","highEqbound","hypothesis","paired","varEqual","alpha","mu","eqboundType","biasCorrection","rmCorrection","glass","smdCi"]),N0=["m1","sd1","n1","m2","sd2","n2","r12"],y0=new Set(["eqb","lowEqbound","highEqbound","hypothesis","paired","varEqual","alpha","mu","eqboundType","biasCorrection","rmCorrection","glass","smdCi"]),M0="Equivalence interval does not include zero.",E0="Warning: setting bound type to SMD produces biased results!",x0="MET test may have higher error rates than a nil two-tailed test. Consider wider equivalence bounds.";function Zf(e,t,n,r){if(e===1/0||e===-1/0)return e;let o=e-1<e+1?1:Math.abs(e)*Number.EPSILON;return br(s=>Qt(e,t,{ncp:s,lowerTail:!r})-n,e-o,e+o,{tol:0,maxiter:5e3,extendInt:r?"upX":"downX"}).root}function R0(e,t){let{alpha:n,smdCi:r}=t,o=Math.abs(e.delta),s=(M,R,T)=>(1-(T-2)/(T*M*M))*R*R,i,c,a,l,f,m,u,h,p,w,d,b,N=t.biasCorrection;if(e.design==="two"){let{n1:M,n2:R,s1:T,s2:S}=e,A=T*T,C=S*S,L=2*M*R/(M+R);if(t.glass!==void 0){let k=t.glass==="glass1",[F,I,O,q]=k?[T,M,S,R]:[S,R,T,M];c=F,a=I-1,i=N?"Glass's delta(g)":"Glass's delta(d)",l=N?xr(a):1,f=l*o/c,p=1/R+1/M;let $=O/F;m=Math.sqrt($*$/(q-1)+1/(I-1)+f*f/(2*(I-1))),u=o/(F*Math.sqrt(1/I+$*$/q)),h=M+R-2,w=Math.sqrt(2/L)}else if(t.varEqual)c=Math.sqrt(((M-1)*A+(R-1)*C)/(M+R-2)),a=M+R-2,i=N?"Hedges's g":"Cohen's d",l=N?xr(a):1,f=l*o/c,p=1/M+1/R,m=Math.sqrt(1/M+1/R+s(l,f,a)),u=o/(c*Math.sqrt(p)),h=a,w=Math.sqrt(2*(R*A+M*C)/(M*R*(A+C)));else{c=Math.sqrt((A+C)/2),a=(M-1)*(R-1)*(A+C)**2/((R-1)*A*A+(M-1)*C*C),i=N?"Hedges's g(av)":"Cohen's d(av)",l=N?xr(a):1,f=l*o/c,p=2*(R*A+M*C)/(M*R*(A+C));let k=c**4;m=Math.sqrt(f*f*(A*A/(M-1)+C*C/(R-1))/(8*k)+(A/(M-1)+C/(R-1))/(c*c)),u=o/Math.sqrt(A/M+C/R),h=a,w=Math.sqrt(2/L)}d=a,b=(k,F)=>Math.sqrt(d/(d-2)*(2/L)*(1+k*k*L/2)-k*k/(F*F))}else if(e.design==="paired"){let{n:M,s1:R,s2:T,sdif:S,oneMinusR:A}=e,C=t.glass!==void 0?"":t.rmCorrection?"(rm)":"(z)";if(t.glass!==void 0?c=t.glass==="glass1"?R:T:c=t.rmCorrection?S/Math.sqrt(2*A):S,i=t.glass!==void 0?N?"Glass's delta(g)":"Glass's delta(d)":N?`Hedges's g${C}`:`Cohen's d${C}`,a=M-1,d=2*M-2,l=N?xr(r==="goulet"?d:a):1,f=l*o/c,p=1/M,u=o*Math.sqrt(M)/c,h=a,w=Math.sqrt(2*A/M),b=(L,k)=>{let F=r==="goulet"?d:a,I=Math.sqrt(F/(F-2)*(2*A/M)*(1+L*L*M/(2*A))-L*L/(k*k));return t.rmCorrection?I:I*Math.sqrt(2*A)},t.glass!==void 0){let L=S/c;m=Math.sqrt(L*L/(M-1)+f*f/(2*(M-1)))}else!t.rmCorrection&&r==="nct"?m=Math.sqrt(1/M+s(l,f,a)):m=b(f,l)}else{let{n:M,s:R}=e;c=R,a=M-1,d=a,i=N?"Hedges's g":"Cohen's d",l=N?xr(a):1,f=l*o/c,p=1/M,m=Math.sqrt(1/M+f*f/(2*M)),u=o*Math.sqrt(M)/R,h=a,w=1/Math.sqrt(M),b=(T,S)=>Math.sqrt(a/(a-2)*(1/M)*(1+T*T*M)-T*T/(S*S))}let v,E,g,y=a;if(r==="nct"){g=m,y=h;let M=Zf(u,h,n,!0),R=Zf(u,h,n,!1);v=M*Math.sqrt(p)*l,E=R*Math.sqrt(p)*l}else if(r==="goulet"){g=b(f,l),y=d;let M=f/w;v=Wt(n,d,{ncp:M})*w,E=Wt(n,d,{ncp:M,lowerTail:!1})*w}else{g=m;let M=r==="t"?gt(n,a,!1,!1):le(n,{lowerTail:!1});v=f-M*g,E=f+M*g}let x=e.delta<0;return{label:i,denom:c,estimate:x?-f:f,se:g,confInt:x?[-E,-v]:[v,E],df:y,J:l}}function Jf(e,t){if(!(Array.isArray(e)||ArrayBuffer.isView(e)))throw new TypeError(`'${t}' must be an array of numbers`);let n=e;for(let r=0;r<n.length;r++){let o=n[r];if(typeof o!="number")throw new TypeError(`'${t}' must be numeric`);if(o===1/0||o===-1/0)throw new RangeError(`'${t}' must not contain infinite values`)}}function em(e){let{eqb:t}=e,n;if(t!==void 0){let r=typeof t=="number"?[t]:Array.from(t);if(!r.every(o=>typeof o=="number"))throw new TypeError("'eqb' must be a number or an array of 1 or 2 numbers");if(r.length===1)n=[-Math.abs(r[0]),Math.abs(r[0])];else if(r.length===2)n=[Math.min(r[0],r[1]),Math.max(r[0],r[1])];else throw new RangeError("'eqb' must be a number or an array of 1 or 2 numbers")}else{if(e.lowEqbound===void 0||e.highEqbound===void 0)throw new TypeError("the equivalence bounds are required: 'eqb', or 'lowEqbound' and 'highEqbound'");n=[e.lowEqbound,e.highEqbound]}if(!n.every(r=>typeof r=="number"&&Number.isFinite(r)))throw new RangeError("the equivalence bounds must be finite numbers");if(n[0]>n[1])throw new RangeError("'lowEqbound' must not be greater than 'highEqbound'");return n}function tm(e){let t=Se(e.hypothesis,["EQU","MET"]),n=Se(e.eqboundType,["raw","SMD"])==="SMD",r=Se(e.smdCi,["nct","goulet","t","z"]),o=e.glass===void 0?void 0:Se(e.glass,["glass1","glass2"]),s=e.alpha??.05;if(!(typeof s=="number"&&s>0&&s<.5))throw new RangeError("'alpha' must be a single number in (0, 0.5)");let i=e.mu??0;if(!(typeof i=="number"&&Number.isFinite(i)))throw new RangeError("'mu' must be a finite number");let c=(a,l,f)=>{if(a!==void 0&&typeof a!="boolean")throw new TypeError(`'${l}' must be true or false`);return a??f};return{hypothesis:t,smdBounds:n,smdCi:r,glass:o,alpha:s,mu:i,paired:c(e.paired,"paired",!1),varEqual:c(e.varEqual,"varEqual",!1),biasCorrection:c(e.biasCorrection,"biasCorrection",!0),rmCorrection:c(e.rmCorrection,"rmCorrection",!1)}}function nm(e,t,n,r,o,s,i,c,a,l,f,m){let{alpha:u,hypothesis:h}=l,p=R0(c,l),w=V(p.denom,a),d=l.smdBounds?[f[0]*w,f[1]*w]:f,b=O=>{if(O===0)return[0,n];let q=Math.floor(Math.log2(Math.abs(O))),$=Ve(O,-q);return l.smdBounds?[$*p.denom,q+a]:[$,q]},N=l.smdBounds?f:[f[0]/w,f[1]/w];l.smdBounds&&m.push(E0),d[0]<=0&&d[1]>=0||m.push(M0);let v=O=>V(O/r,n-o),E=O=>{let[q,$]=b(O),_=Math.max(n,$);return Ve((Ve(t,n-_)-Ve(q,$-_))/r,_-o)},g=v(t),y=E(f[0]),x=E(f[1]),M=V(r,o),R=h==="EQU",T={t:g,se:M,df:s,pValue:2*dt(-Math.abs(g),s,!0,!1)},S={t:y,se:M,df:s,pValue:dt(y,s,!R,!1)},A={t:x,se:M,df:s,pValue:dt(x,s,R,!1)},C=R?Math.max(S.pValue,A.pValue):Math.min(S.pValue,A.pValue);!R&&d[0]<=0&&d[1]>=0&&C<=T.pValue&&m.push(x0);let L=gt(u,s,!1,!1),k=Math.max(n,o),[F,I]=[V(t,n-k),V(r,o-k)];return Q(e,{tTest:T,lower:S,upper:A,pValue:C,bounds:{raw:d,smd:N},raw:{estimate:V(t,n),se:M,confInt:[V(F-L*I,k),V(F+L*I,k)]},smd:{label:p.label,estimate:p.estimate,se:p.se,confInt:p.confInt,df:p.df,J:p.J},confLevel:1-2*u,method:i,hypothesis:h,warnings:m})}function T0(e,t,n={}){B(n,v0,"tTOST");let r=tm(n),o=em(n),{paired:s,varEqual:i,mu:c}=r,a=t===void 0;if(s&&a)throw new TypeError("'y' is required when paired is true");Jf(e,"x"),a||Jf(t,"y");let l=ot(()=>a?Nn(e,{mu:c}):Nn(e,t,{paired:s,varEqual:i,mu:c})),f=l.parameter,m,u,h,p;if(a){let w=qe([Dt(Array.from(e))]);u=w.k;let d=w.groups[0],b=at(d);h=it(d)-V(c,-u),p=Math.sqrt(b/d.length),m={design:"one",n:d.length,delta:h,s:Math.sqrt(b)}}else if(s){let[w,d]=Jn(Array.from(e),Array.from(t)),b=qe([w,d]);u=b.k;let[N,v]=b.groups,E=N.map((T,S)=>T-v[S]),g=Math.sqrt(at(N)),y=Math.sqrt(at(v)),x=at(E),M=Math.sqrt(x),R=(M-(g-y))*(M+(g-y))/(2*g*y);h=it(E)-V(c,-u),p=Math.sqrt(x/E.length),m={design:"paired",n:E.length,delta:it(N)-it(v)-V(c,-u),s1:g,s2:y,sdif:M,oneMinusR:R}}else{let w=qe([Dt(Array.from(e)),Dt(Array.from(t))]);u=w.k;let[d,b]=w.groups;if(d.length<2||b.length<2)throw new RangeError("not enough observations: at least 2 are needed in each sample");let[N,v,E,g]=[d.length,b.length,at(d),at(b)];h=it(d)-it(b)-V(c,-u),p=Math.sqrt(i?((N-1)*E+(v-1)*g)/(N+v-2)*(1/N+1/v):Math.sqrt(E/N)**2+Math.sqrt(g/v)**2),m={design:"two",n1:N,n2:v,delta:h,s1:Math.sqrt(E),s2:Math.sqrt(g)}}return nm("tTOST",h,u,p,u,f,l.method,m,u,r,o,[])}function S0(e,t={}){B(e,N0,"tsumTOST"),B(t,y0,"tsumTOST");let n=tm(t),r=em(t),{m1:o,sd1:s,n1:i,m2:c,sd2:a,n2:l,r12:f}=e,m=(k,F)=>{if(typeof k!="number"||!Number.isFinite(k))throw new RangeError(`'${F}' must be a finite number`);return k},u=(k,F)=>{let I=m(k,F);if(!(Number.isInteger(I)&&I>=2))throw new RangeError(`'${F}' must be an integer of at least 2`);return I},h=(k,F)=>{let I=m(k,F);if(!(I>0))throw new RangeError(`'${F}' must be positive`);return I};m(o,"m1"),h(s,"sd1"),u(i,"n1");let p=c===void 0&&a===void 0&&l===void 0;if(n.paired&&p)throw new TypeError("'m2', 'sd2' and 'n2' are required when paired is true");p||(m(c,"m2"),h(a,"sd2"),u(l,"n2"));let w=[],{groups:d,k:b}=qe([[o,...p?[]:[c],n.mu]]),{groups:N,k:v}=qe([[s,...p?[]:[a]]]),[E,g=NaN]=p?[d[0][0]]:[d[0][0],d[0][1]],y=d[0][d[0].length-1],[x,M=NaN]=N[0],R=k=>V(k,b-v),T,S,A,C,L;if(p)T=E-y,S=x/Math.sqrt(i),A=i-1,C="One Sample t-test",L={design:"one",n:i,delta:R(T),s:x};else if(n.paired){let k=m(f,"r12");if(!(k>-1&&k<1))throw new RangeError("'r12' must be in (-1, 1)");let F=i;i!==l&&(w.push("Unequal number of pairs; using smallest n"),F=Math.min(i,l));let I=Math.sqrt(x*x+M*M-2*k*x*M);if(!(I>0))throw new RangeError("the standard deviation of the differences is 0");T=E-g-y,S=I/Math.sqrt(F),A=F-1,C="Paired t-test",L={design:"paired",n:F,delta:R(T),s1:x,s2:M,sdif:I,oneMinusR:1-k}}else{let k=l,F=x*x,I=M*M;if(T=E-g-y,n.varEqual){let O=((i-1)*F+(k-1)*I)/(i+k-2);S=Math.sqrt(O*(1/i+1/k)),A=i+k-2,C=" Two Sample t-test"}else{let O=F/i,q=I/k;S=Math.sqrt(O+q),A=(O+q)**2/(O*O/(i-1)+q*q/(k-1)),C="Welch Two Sample t-test"}L={design:"two",n1:i,n2:k,delta:R(T),s1:x,s2:M}}return nm("tsumTOST",T,b,S,v,A,C,L,v,n,r,w)}var A0=["alternative","tsmethod"],q0=["square","central"],k0=1e3,I0=32,_0=.9,rm=1+1e-7,L0=2**24,C0=34e7;function F0(e,t={}){if(B(t,A0,"boschlooTest"),e.length!==2||e.some(y=>y.length!==2))throw new TypeError("'table' must be a 2 x 2 table");let n=Ze(t.alternative),r=Se(t.tsmethod,q0),o=e.map((y,x)=>Array.from(y,(M,R)=>Ur(M,`table[${x}][${R}]`))),[[s,i],[c,a]]=o,l=s+i,f=c+a;if(l===0||f===0)throw new RangeError("each row (group) of 'table' must have at least one observation");if((l+1)*(f+1)>L0)throw new RangeError(`Boschloo's test of these group sizes has ${(l+1)*(f+1)} possible tables, more than 2^24`);let m=l+f,u=Math.max(k0,Math.ceil(I0*Math.sqrt(m))),h=y=>Math.min(y+1,40*Math.sqrt(y));if(u*(h(l)+h(f)+4)>C0)throw new RangeError(`Boschloo's test of these group sizes (${l} and ${f}) would take too long`);let p=s/l-c/f,w;n!=="two.sided"?w=n:r==="square"?w="square":w=p>0?"greater":"less";let d=s+c,b;w==="less"?b=sn(s,l,f,d,!0,!1):w==="greater"?b=sn(s-1,l,f,d,!1,!1):b=ot(()=>Io(o)).pValue;let N=O0(l,f,s,c,w),{value:v,at:E}=D0(N,l,f,u),g=n==="two.sided"&&r==="central"?Math.min(1,2*v):v;return Q("boschlooTest",{method:"Boschloo's Exact Test",alternative:n,statistic:b,statisticName:"test statistic",pValue:g,estimate:p,estimateNames:"difference in proportion",nullValue:0,nullValueName:"difference in proportion",nuisanceParameter:E,warnings:[]})}function O0(e,t,n,r,o){let s=e+t,i=new Float64Array(s+1);for(let b=2;b<=s;b++)i[b]=us(b);let c=t+1,a=new Float64Array((e+1)*c),l=Math.min(e,t)+1,f=new Float64Array(l),m=new Float64Array(l),u=new Float64Array(l);for(let b=0;b<=s;b++){let N=Math.max(0,b-t),E=Math.min(b,e)-N+1,g=-1/0;for(let M=0;M<E;M++){let R=N+M,T=-i[R]-i[e-R]-i[b-R]-i[t-b+R];f[M]=T,T>g&&(g=T)}let y=0;for(let M=0;M<E;M++)f[M]=Math.exp(f[M]-g),y+=f[M];for(let M=0;M<E;M++)f[M]=f[M]/y;let x=0;for(let M=0;M<E;M++)m[M]=x+=f[M];x=0;for(let M=E-1;M>=0;M--)u[M]=x+=f[M];if(o==="less")for(let M=0;M<E;M++)a[(N+M)*c+b-N-M]=m[M];else if(o==="greater")for(let M=0;M<E;M++)a[(N+M)*c+b-N-M]=u[M];else{let M=0;for(let R=1;R<E;R++)f[R]>f[M]&&(M=R);for(let R=0;R<E;R++){let T=f[R]*rm,S=-1,A=M;for(;A-S>1;){let F=S+A>>1;f[F]<=T?S=F:A=F}let C=M,L=E;for(;L-C>1;){let F=C+L>>1;f[F]<=T?L=F:C=F}let k=f[M]<=T?1:(S>=0?m[S]:0)+(L<E?u[L]:0);a[(N+R)*c+b-N-R]=Math.min(1,k)}}}let h=a[n*c+r]*rm,p=new Int32Array(e+2),w=[],d=[];for(let b=0;b<=e;b++){p[b]=w.length;let N=0;for(;N<=t;){if(a[b*c+N]<=h){let v=N;for(;N+1<=t&&a[b*c+N+1]<=h;)N++;w.push(v),d.push(N)}N++}}return p[e+1]=w.length,{starts:p,from:Int32Array.from(w),to:Int32Array.from(d)}}function om(e,t,n){if(t===0||t===1){let h=t===0?0:e;return n[h]=1,[h,h]}let r=Math.min(e,Math.floor((e+1)*t));n[r]=hn(r,e,t,!1);let o=Ot(1,-t),s=rt([t,0],o),i=rt(o,[t,0]),c=s[0],a=i[0],l=r;for(let h=r+1;h<=e;h++){let p=n[h-1]*((e-h+1)/h*c);if(p===0)break;n[h]=p,l=h}let f=r;for(let h=r-1;h>=0;h--){let p=n[h+1]*((h+1)/(e-h)*a);if(p===0)break;n[h]=p,f=h}let m=s[1]/c,u=i[1]/a;for(let h=r+1;h<=l;h++)n[h]=n[h]*(1+(h-r)*m);for(let h=f;h<r;h++)n[h]=n[h]*(1+(r-h)*u);return[f,l]}function D0(e,t,n,r){let{starts:o,from:s,to:i}=e,c=new Float64Array(t+1),a=new Float64Array(n+1),l=new Float64Array(n+1),f=new Float64Array(n+1),m=v=>{let[E,g]=om(t,v,c),[y,x]=om(n,v,a),M=0;for(let T=y;T<=x;T++)l[T]=M+=a[T];M=0;for(let T=x;T>=y;T--)f[T]=M+=a[T];let R=0;for(let T=E;T<=g;T++){let S=c[T];if(S===0)continue;let A=0;for(let C=o[T];C<o[T+1];C++){let L=s[C],k=i[C];if(!(k<y||L>x))if(L===0)A+=l[Math.min(k,x)];else if(k===n)A+=f[Math.max(L,y)];else for(let F=Math.max(L,y);F<=Math.min(k,x);F++)A+=a[F]}R+=S*A}return Math.min(1,R)},u=Math.PI/2,h=v=>v<=0?0:v>=u?1:Math.sin(v)**2,p=new Float64Array(r+2),w=0;for(let v=0;v<=r+1;v++)p[v]=m(h(u*v/(r+1))),p[v]>p[w]&&(w=v);let d=p[w],b=h(u*w/(r+1));if(d>=1)return{value:1,at:b};let N=_0*d;for(let v=0;v<=r+1;v++){let E=p[v];if(E<N||v>0&&!(E>p[v-1])||v<=r&&!(E>=p[v+1]))continue;let g=u*Math.max(0,v-1)/(r+1),y=u*Math.min(r+1,v+1)/(r+1),M=kl(S=>-m(h(S)),g,y,1e-14).x,R=m(h(M)),T=1e-4/Math.sqrt(2*(t+n));for(let S=0;S<2&&(M-T>g&&M+T<y);S++,T*=.01){let A=m(h(M-T)),C=m(h(M+T)),L=A-2*R+C;if(!(L<0))break;let k=T*(A-C)/(2*L);if(!(Math.abs(k)<T))break;let F=m(h(M+k));F>R*(1+2**-50)&&(M+=k,R=F)}R>d&&(d=R,b=h(M))}return{value:d,at:b}}var sm=["correct"];function P0(e,t,n={}){ct(t)&&B(t,sm,"mcnemarTest"),B(n,sm,"mcnemarTest");let r;if(e.length>0&&Array.isArray(e[0])){t!==void 0&&!Array.isArray(t)&&(n=t),r=e.map(f=>Array.from(f));let l=r.length;if(l<2||r.some(f=>f.length!==l))throw new RangeError("'x' must be square with at least two rows and columns");if(r.some(f=>f.some(m=>!Number.isFinite(m)||m<0)))throw new RangeError("all entries of 'x' must be nonnegative and finite")}else{if(!Array.isArray(t))throw new TypeError("'y' is required when 'x' is not a table");r=V0(e,t)}let o=n.correct??!0,s=r.length,i,c=o&&s===2&&r[0][1]!==r[1][0];if(c){let l=r[0][1],f=r[1][0];i=am(Math.max(0,Math.abs(l-f)-1),l,f)}else{i=0;let l=0;for(let f=0;f<s;f++)for(let m=f+1;m<s;m++){let u=r[f][m],h=r[m][f];u+h!==0&&(i+=am(u-h,u,h),l++)}if(l===0)throw new RangeError("the table has no discordant pairs")}let a=s*(s-1)/2;return{method:c?"McNemar's Chi-squared test with continuity correction":"McNemar's Chi-squared test",statistic:i,statisticName:"McNemar's chi-squared",parameter:a,parameterName:"df",pValue:wt(i,a/2,2,!1,!1),warnings:[]}}function am(e,t,n){let r=t+n;return r<1/0?Vt(e,r):Vt(V(e,-520),V(t,-1040)+V(n,-1040))}function V0(e,t){if(e.length!==t.length)throw new RangeError("'x' and 'y' must have the same length");let n=[],r=tn(e),o=tn(t);for(let m=0;m<e.length;m++){let u=r[m],h=o[m];typeof u=="number"&&Number.isNaN(u)||typeof h=="number"&&Number.isNaN(h)||n.push([u,h])}let s=m=>Array.from(new Set(m)).sort(zn),i=s(n.map(m=>m[0])),c=s(n.map(m=>m[1]));if(i.length<2||i.length!==c.length)throw new RangeError("'x' and 'y' must have the same number of levels (minimum 2)");let a=new Map(i.map((m,u)=>[m,u])),l=new Map(c.map((m,u)=>[m,u])),f=i.map(()=>new Array(i.length).fill(0));for(let[m,u]of n)f[a.get(m)][l.get(u)]++;return f}var $0=["ratio","alternative","confLevel"],Li=2**-1022;function im(e,t,n={}){if(B(n,$0,"varTest"),typeof e=="string"){let v=tr(e,t);return im(v.x,v.y,n)}let r=Array.from(e).filter(Number.isFinite),o=Array.from(t).filter(Number.isFinite),s=n.ratio??1;if(!(Number.isFinite(s)&&s>0))throw new RangeError("'ratio' must be a single positive number");let i=Ze(n.alternative),c=n.confLevel??.95;_t(c);let a=r.length-1,l=o.length-1;if(a<1)throw new Error("not enough 'x' observations");if(l<1)throw new Error("not enough 'y' observations");let[f,m]=xt([r,o]).variance,u=0;if(!(f>=Li&&m>=Li)){let v=xt([r]),E=xt([o]);f=v.variance[0],m=E.variance[0],u=2*(v.k-E.k)}if(f===0&&m===0)throw new RangeError("both samples have zero variance");let h=u===0?f/m:Ve(f/m,u),p=h/s,w=ht(p,a,l,!0,!1),d=ht(p,a,l,!1,!1),b,N;if(i==="less")b=w,N=[0,h/un(1-c,a,l,!0,!1)];else if(i==="greater")b=d,N=[h/un(c,a,l,!0,!1),1/0];else{b=Math.min(1,2*Math.min(w,d));let v=(1-c)/2,E=v<1e-8?un(v,a,l,!1,!1):un(1-v,a,l,!0,!1);N=[h/E,h/un(v,a,l,!0,!1)]}return{method:"F test to compare two variances",alternative:i,statistic:p,statisticName:"F",parameter:[a,l],parameterName:["num df","denom df"],pValue:b,confInt:N,confLevel:c,estimate:h,estimateNames:"ratio of variances",nullValue:s,nullValueName:"ratio of variances",warnings:[]}}function cm(e,t){if(typeof e=="string"){let p=yo(e,t);return cm(Al(p.values,p.group))}let n=e,r=n.length;if(r<2)throw new RangeError("'x' must be a list with at least 2 elements");let o=n.map(p=>Array.from(p).filter(Number.isFinite));if(o.some(p=>p.length<2))throw new RangeError("there must be at least 2 observations in each group");let s=o.map(p=>p.length-1),i=xt(o).variance,c=0,a=0,l=0,f=0;for(let p=0;p<r;p++)c+=s[p],a+=s[p]*i[p],l+=s[p]*Math.log(i[p]),f+=1/s[p];a/=c;let m=c*Math.log(a)-l;if(i.some(p=>p<Li)){let p=o.map(E=>{let g=xt([E]);return{w:g.variance[0],e:g.k}}),w=Math.max(...p.map(E=>E.e)),d=p.map(E=>Math.log(E.w)+2*(E.e-w)*Math.LN2),b=d.map((E,g)=>Math.log(s[g])+E),N=Math.max(...b),v=N+Math.log(b.reduce((E,g)=>E+Math.exp(g-N),0))-Math.log(c);m=c*v-d.reduce((E,g,y)=>E+s[y]*g,0)}let u=m/(1+(f-1/c)/(3*(r-1))),h=r-1;return{method:"Bartlett test of homogeneity of variances",statistic:u,statisticName:"Bartlett's K-squared",parameter:h,parameterName:"df",pValue:wt(u,h/2,2,!1,!1),warnings:[]}}var z0=["alternative"],Ci=["alternative"];function Fi(e,t,n,r){return{method:e,statistic:n,statisticName:t,parameter:r,parameterName:"df",pValue:wt(n,r/2,2,!1,!1),warnings:[]}}function lm(e){let t=0;for(let n of wn(e))t+=n*n*n-n;return t}function Us(e,t){if(typeof e=="string"){let l=yo(e,t);return Us(l.values,l.group.codes)}let n=vr(e,t,"error"),r=n.length;if(r<2)throw new RangeError("all observations are in the same group");let o=n.flat(),s=o.length,i=bt(o),c=0,a=0;for(let l of n){let f=0;for(let m=0;m<l.length;m++)f+=i[a+m];a+=l.length,c+=f*f/l.length}return c=(12*c/(s*(s+1))-3*(s+1))/(1-lm(o)/(s*s*s-s)),Fi("Kruskal-Wallis rank sum test","Kruskal-Wallis chi-squared",c,r-1)}function Oi(e,t){if(typeof e=="string")return Oi(vs(e,t));let n=Fa(e),r=n.length,o=e[0]?.length??0;if(r<2||o<2)throw new RangeError("'y' must have at least two complete rows and two columns");let s=new Array(o).fill(0),i=0;for(let l of n){let f=bt(l);for(let m=0;m<o;m++)s[m]+=f[m];i+=lm(l)}let c=0;for(let l of s)c+=(l-r*(o+1)/2)**2;let a=12*c/(r*o*(o+1)-i/(o-1));return Fi("Friedman rank sum test","Friedman chi-squared",a,o-1)}function j0(e,t={},n,r={}){typeof e!="string"&&ct(t)&&B(t,Ci,"pageTest"),ct(n)&&B(n,Ci,"pageTest"),B(r,Ci,"pageTest");let o,s;if(typeof e=="string")o=vs(e,t),s=n??{};else if(Array.isArray(n)){let w=Array.from(e),d=t,b=n;if(w.length!==d.length||w.length!==b.length)throw new RangeError("'x', 'groups' and 'blocks' must have the same length");if(!w.every(Number.isFinite))throw new RangeError("missing or non-finite values are not allowed");let N=tn(d),v=tn(b);if(N.some(M=>typeof M=="number"&&Number.isNaN(M))||v.some(M=>typeof M=="number"&&Number.isNaN(M)))throw new RangeError("missing treatment or block labels are not allowed");let E=Array.from(new Set(N)).sort(zn),g=Array.from(new Set(v)).sort(zn);if(E.length<2||g.length<1)throw new RangeError("at least two treatments and one block are required");let y=new Map(E.map((M,R)=>[M,R])),x=new Map(g.map((M,R)=>[M,R]));o=g.map(()=>new Array(E.length).fill(NaN));for(let M=0;M<w.length;M++){let R=x.get(v[M]),T=y.get(N[M]);if(!Number.isNaN(o[R][T]))throw new RangeError("not an unreplicated complete block design");o[R][T]=w[M]}if(o.some(M=>M.some(R=>Number.isNaN(R))))throw new RangeError("not an unreplicated complete block design");s=r}else o=e.map(w=>Array.from(w)),s=t;let i=o.length,c=o[0]?.length??0;if(i<1||c<2||o.some(w=>w.length!==c))throw new RangeError("'y' must be a non-empty matrix with at least two columns");if(o.some(w=>w.some(d=>!Number.isFinite(d))))throw new RangeError("missing or non-finite values are not allowed in a block");let a=Ze(s.alternative),l=new Array(c).fill(0);for(let w of o){let d=bt(w);for(let b=0;b<c;b++)l[b]+=d[b]}let f=0;for(let w=0;w<c;w++)f+=(w+1)*l[w];let m=i*c*(c+1)**2/4,u=i*c**2*(c-1)*(c+1)**2/144,h=(f-m-.5)/Math.sqrt(u),p=a==="less"?se(h):a==="greater"?se(h,{lowerTail:!1}):2*se(-Math.abs(h));return{method:"Page's ordered aligned rank sum test",alternative:a,statistic:h,statisticName:"z",pValue:p,estimate:f,warnings:[]}}function W0(e){if(e.some(t=>t.some(n=>!Number.isNaN(n)&&n!==0&&n!==1)))throw new RangeError("the responses must be 0 or 1 (or NaN)");return{...Oi(e),method:"Cochran's Q test",statisticName:"Q"}}function fm(e,t){if(typeof e=="string")return fm(vs(e,t));let n=Fa(e),r=n.length,o=e[0]?.length??0;if(n.length<2||o<2)throw new RangeError("'y' must have at least two complete rows and two columns");let s=n.map(w=>Nt(w)-an(w));s.some(w=>w===1/0)&&n.every(w=>w.every(Number.isFinite))&&(s=qe(n).groups.map(w=>Nt(w)-an(w)));let i=bt(s),c=new Array(o).fill(0),a=0;n.forEach((w,d)=>{let b=bt(w);for(let N=0;N<o;N++){let v=i[d]*(b[N]-(o+1)/2);a+=v*v,c[N]+=v}});let l=0;for(let w of c)l+=w*w;l/=r;let f=o-1,m=(r-1)*(o-1),u,h,p=[f,m];if(a===l){u=NaN,p=[NaN,NaN];let w=0;for(let d=2;d<=o;d++)w+=Math.log(d);h=Math.exp((1-r)*w)}else u=(r-1)*l/(a-l),h=ht(u,f,m,!1,!1);return{method:"Quade test",statistic:u,statisticName:"Quade F",parameter:p,parameterName:["num df","denom df"],pValue:h,warnings:[]}}function mm(e,t){if(typeof e=="string"){let h=yo(e,t);return mm(h.values,h.group.codes)}let n=vr(e,t,"error"),r=n.length;if(r<2)throw new RangeError("all observations are in the same group");let o=h=>h.flatMap(p=>{let w=xs(p);return p.map(d=>Math.abs(d-w))}),s=o(n);if(s.some(Number.isNaN))throw new RangeError("a group has an infinite median: its deviations are undefined");s.some(h=>h===1/0)&&n.every(h=>h.every(Number.isFinite))&&(s=o(qe(n).groups));let i=s.length,a=bt(s).map(h=>le((1+h/(i+1))/2)),l=0;for(let h of a)l+=h;let f=l/i,m=0,u=0;for(let h of n){let p=0;for(let w=0;w<h.length;w++)p+=a[u+w];u+=h.length,m+=h.length*(p/h.length-f)**2}return m/=at(a),Fi("Fligner-Killeen test of homogeneity of variances","Fligner-Killeen:med chi-squared",m,r-1)}function um(e,t,n={}){if(B(n,z0,"moodTest"),typeof e=="string"){let E=tr(e,t);return um(E.x,E.y,n)}let r=Array.from(e).filter(Number.isFinite),o=Array.from(t).filter(Number.isFinite),s=Ze(n.alternative),i=r.length,c=o.length,a=i+c;if(a<3)throw new RangeError("not enough observations");if(i===0||c===0)throw new RangeError("not enough observations in each sample");let l=r.concat(o),f=Array.from({length:a},(E,g)=>g).sort((E,g)=>l[E]-l[g]),m=new Array(a),u=0;for(;u<a;){let E=u;for(;E+1<a&&l[f[E+1]]===l[f[u]];)E++;let g=0;for(let y=u;y<=E;y++)g+=(y+1-(a+1)/2)**2;g/=E-u+1;for(let y=u;y<=E;y++)m[f[y]]=g;u=E+1}let h=0;for(let E=0;E<i;E++)h+=m[E];let p=0;for(let E of m)p+=E;p/=a;let w=0;for(let E of m)w+=(E-p)**2;let d=i*p,b=i*c/(a*(a-1))*w,N=(h-d)/Math.sqrt(b),v=s==="less"?se(N):s==="greater"?se(N,{lowerTail:!1}):2*se(-Math.abs(N));return{method:"Mood two-sample test of scale",alternative:s,statistic:N,statisticName:"Z",pValue:v,warnings:[]}}function Rr(e,t){let n=0;for(let r=e.length-1;r>=0;r--)n=n*t+e[r];return n}var B0=[0,.221157,-.147981,-2.07119,4.434685,-2.706056],K0=[0,.042981,-.293762,-1.752461,5.682633,-3.582633],U0=[.544,-.39978,.025054,-6714e-7],H0=[1.3822,-.77857,.062767,-.0020322],Y0=[-1.5861,-.31082,-.083751,.0038915],G0=[-.4803,-.082676,.0030302],X0=[-2.273,.459];function Q0(e){if(!(e>=3)||e!==Math.floor(e))throw new RangeError("n must be an integer of at least 3");let t=new Float64Array(e);if(e===3)return t[0]=-Math.SQRT1_2,t[2]=Math.SQRT1_2,t;let n=Math.floor(e/2),r=new Float64Array(n),o=0;for(let m=0;m<n;m++)r[m]=-le((m+1-.375)/(e+.25)),o+=r[m]*r[m];o*=2;let s=Math.sqrt(o),i=1/Math.sqrt(e),c=r[0]/s+Rr(B0,i),a=new Float64Array(n);a[0]=c;let l,f;if(e>5){let m=r[1]/s+Rr(K0,i);a[1]=m,l=2,f=Math.sqrt((o-2*r[0]*r[0]-2*r[1]*r[1])/(1-2*c*c-2*m*m))}else l=1,f=Math.sqrt((o-2*r[0]*r[0])/(1-2*c*c));for(let m=l;m<n;m++)a[m]=r[m]/f;for(let m=0;m<n;m++)t[e-1-m]=a[m],t[m]=-a[m];return t}function hm(e){let t=qe([e]).groups[0],n=t.length,r=Q0(n),o=t[n-1]-t[0],s=0;for(let f of t)s+=f/o;s/=n;let i=0,c=0,a=0;for(let f=0;f<n;f++){let m=t[f]/o-s;i+=r[f]*r[f],c+=m*m,a+=r[f]*m}let l=Math.sqrt(i*c);return(l-a)*(l+a)/(i*c)}var Z0=1.90985931710274,J0=1.0471975511966;function pm(e,t){if(Number.isNaN(e)||!(t>=3))return NaN;if(t===3){let i=Z0*(Math.asin(Math.sqrt(1-e))-J0);return Math.max(i,0)}let n=Math.log(e);if(t<=11){let i=Rr(X0,t);if(n>=i)return 1e-99;let c=Rr(U0,t),a=Math.exp(Rr(H0,t));return se(-Math.log(i-n),{mean:c,sd:a,lowerTail:!1})}let r=Math.log(t),o=Rr(Y0,r),s=Math.exp(Rr(G0,r));return se(n,{mean:o,sd:s,lowerTail:!1})}var bm=Math.log(2*Math.PI),dm=2e-14,gm=500;function eg(e){return Math.exp(-.5*e*e-.5*bm)}function tg(e){return se(e,{logP:!0})}function vm(e,t=-1/0,n=!1){let r=t===-1/0?La(e,0,2,0,dm,gm):La(e,t,1,0,dm,gm),o=r.ier===1||r.ier===3||r.ier===5;return n&&o&&!(r.abserr<=1e-6*Math.abs(r.result))?NaN:r.result}function wm(e,t,n){let r=t.map(Math.sqrt),o=t.map(i=>Math.sqrt(1-i));return vm(i=>{let c=0;for(let a=0;a<r.length;a++){let l=(r[a]*i+e)/o[a];if(!n){c+=tg(l);continue}let f=(r[a]*i-e)/o[a],m=se(f)+se(l,{lowerTail:!1});m<.5?c+=Math.log1p(-m):c+=Math.log(Math.max(0,l+f<0?se(l)-se(f):se(f,{lowerTail:!1})-se(l,{lowerTail:!1})))}return eg(i)*-Math.expm1(c)})}function Tr(e,t,n,r){if(Number.isNaN(e)||Number.isNaN(n))return NaN;if(r&&(e=Math.abs(e)),e===1/0)return 0;if(e===-1/0||r&&e===0)return 1;if(n===1/0)return Math.min(1,wm(e,t,r));let o=n/2,s=Math.log(2)+.5*Math.log(o)-.5*bm-ol(o);return Math.min(1,vm(c=>{let a=s-o*(Math.expm1(2*c)-2*c);return a<-745?0:Math.exp(a)*wm(e*Math.exp(c),t,r)},-1/0,!0))}var ng=["pAdjustMethod"],rg=["pAdjustMethod","alternative"],og=["alternative"],sg=["pAdjustMethod","alternative"],ag=["method","altp"],ig=["alternative"],cg=["welch"],Nm="Ties are present. z-quantiles were corrected for ties.";function An(e,t){let{labels:n,groups:r}=nr(e,t),o=r.length;if(o<2)throw new RangeError("there must be at least two groups");let s=r.map(i=>i.length);return{labels:n,groups:r,n:s,N:s.reduce((i,c)=>i+c,0),k:o}}function Di(e){let t=e.groups.flat(),n=bt(t),r=[],o=0;for(let i of e.n){let c=0;for(let a=0;a<i;a++)c+=n[o+a];o+=i,r.push(c/i)}let s=0;for(let i of wn(t))s+=i*i*i-i;return{R:r,C:s/(12*(e.N-1))}}function Sr(e,t){let n=Array.from({length:e-1},()=>new Array(e-1).fill(NaN));for(let r=0;r<e-1;r++)for(let o=r+1;o<e;o++)n[o-1][r]=t(o,r);return n}function lg(e,t){let n=e.length+1,r=[];for(let c=0;c<n-1;c++)for(let a=c+1;a<n;a++)r.push(e[a-1][c]);let o=Kr(r,t),s=0,i=e.map(c=>c.slice());for(let c=0;c<n-1;c++)for(let a=c+1;a<n;a++)i[a-1][c]=o[s++];return i}function Do(e,t,n,r,o,s=[]){return{method:t,statistic:n,pValue:r,rowNames:e.labels.slice(1),colNames:e.labels.slice(0,-1),pAdjustMethod:o,warnings:s}}function Pi(e,t,n,r,o,s=[]){return{method:t,statistic:n.map(i=>[i]),pValue:r.map(i=>[i]),rowNames:e.labels.slice(1),colNames:e.labels.slice(0,1),pAdjustMethod:o,warnings:s}}var Hs=e=>se(e,{lowerTail:!1}),Vi=(e,t,n)=>hs(Math.abs(e),t,n,{lowerTail:!1});function $i(e){let t=xt(e.groups),n=0;for(let r of t.n)r>1&&(n+=r-1);return Po(n,t.ssWithin),{m:t.mean,s2:t.ssWithin/n,df:e.N-e.k,k:t.k}}function Po(e,t){if(e===0)throw new RangeError("no residual degrees of freedom");if(t===0)throw new RangeError("the pooled within-group variance is 0")}function fg(e,t,n={}){B(n,ng,"kwAllPairsDunnTest");let r=n.pAdjustMethod??"holm",o=An(e,t),{R:s,C:i}=Di(o),c=o.N*(o.N+1)/12-i,a=Sr(o.k,(f,m)=>Math.abs(s[f]-s[m])/Math.sqrt(c*(1/o.n[f]+1/o.n[m]))),l=lg(a.map(f=>f.map(m=>2*Hs(m))),r);return Q("kwAllPairsDunnTest",Do(o,"Dunn's all-pairs test",a,l,r,i!==0?[Nm]:[]))}function mg(e,t,n={}){B(n,rg,"kwManyOneDunnTest");let r=Ze(n.alternative),o=n.pAdjustMethod??"single-step",s=An(e,t),{R:i,C:c}=Di(s),a=s.N*(s.N+1)/12-c,l=s.n.slice(1).map((h,p)=>p+1),f=l.map(h=>(i[h]-i[0])/Math.sqrt(a*(1/s.n[0]+1/s.n[h]))),m=l.map(h=>s.n[h]/(s.n[h]+s.n[0])),u=ym(f,m,r,o);return Q("kwManyOneDunnTest",Pi(s,"Dunn's many-to-one test",f,u,o,c!==0?[Nm]:[]))}function ym(e,t,n,r){return r==="single-step"?e.map(o=>n==="two.sided"?Tr(o,t,1/0,!0):Tr(n==="greater"?o:-o,t,1/0,!1)):Kr(e.map(o=>n==="two.sided"?2*Hs(Math.abs(o)):Hs(n==="greater"?o:-o)),r)}function ug(e,t,n={}){B(n,og,"dunnettTest");let r=Ze(n.alternative),o=An(e,t),{m:s,s2:i,df:c}=$i(o),a=Math.sqrt(i),l=o.n.slice(1).map((h,p)=>p+1),f=l.map(h=>(s[h]-s[0])/(a*Math.sqrt(1/o.n[h]+1/o.n[0]))),m=l.map(h=>o.n[h]/(o.n[h]+o.n[0])),u=f.map(h=>r==="two.sided"?Math.min(1,2*Tr(Math.abs(h),m,c,!1)):Tr(r==="greater"?h:-h,m,c,!1));return Q("dunnettTest",Pi(o,`Dunnett's-test for multiple
|
|
4
4
|
comparisons with one control`,f,u,"single-step"))}function hg(e,t,n={}){B(n,sg,"manyOneUTest");let r=Ze(n.alternative),o=n.pAdjustMethod??"single-step",s=An(e,t);if(o==="single-step"&&s.k<3)throw new RangeError(`for 'p.adjust.method = single-step' at least
|
|
5
5
|
|