@numero-uno-jpn/rstats 1.13.0 → 1.14.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.
Files changed (63) hide show
  1. package/CHANGELOG.md +6 -0
  2. package/README.ja.md +3 -3
  3. package/README.md +3 -3
  4. package/dist/causal.js +2 -2
  5. package/dist/chunks/{chunk-JJGRQ22M.js → chunk-2426YL2X.js} +2 -2
  6. package/dist/chunks/{chunk-GRXOLZ6J.js → chunk-3746VXPM.js} +2 -2
  7. package/dist/chunks/{chunk-2N7PIFPJ.js → chunk-3DA7CMFN.js} +2 -2
  8. package/dist/chunks/{chunk-24UIPUSE.js → chunk-3XUHCWLU.js} +1 -1
  9. package/dist/chunks/{chunk-JTARV4D3.js → chunk-4O6XLQBN.js} +2 -2
  10. package/dist/chunks/{chunk-TQ4P6W3P.js → chunk-62Y36UKH.js} +2 -2
  11. package/dist/chunks/{chunk-KOF2HBUV.js → chunk-6HUTVWPO.js} +2 -2
  12. package/dist/chunks/{chunk-CCYGRSQW.js → chunk-6VY3XNAF.js} +2 -2
  13. package/dist/chunks/{chunk-VO65CBCV.js → chunk-7BBIQY4W.js} +2 -2
  14. package/dist/chunks/{chunk-CKQOWTX7.js → chunk-7FGVMDJE.js} +2 -2
  15. package/dist/chunks/{chunk-2SOBG73L.js → chunk-7VMGP2U7.js} +2 -2
  16. package/dist/chunks/{chunk-LS55YUPZ.js → chunk-A5DZG7FH.js} +2 -2
  17. package/dist/chunks/{chunk-QWXSJEDI.js → chunk-AONDWNMF.js} +2 -2
  18. package/dist/chunks/{chunk-XRB552MP.js → chunk-AQHG7BEZ.js} +2 -2
  19. package/dist/chunks/{chunk-3T4TVNXS.js → chunk-B6NPCYFO.js} +2 -2
  20. package/dist/chunks/{chunk-NWWY2WMF.js → chunk-BCXYAILN.js} +2 -2
  21. package/dist/chunks/{chunk-C7NEFIAH.js → chunk-CKUBQW2A.js} +2 -2
  22. package/dist/chunks/{chunk-5Y425VAJ.js → chunk-D25AVVVI.js} +2 -2
  23. package/dist/chunks/{chunk-NRBTDLVW.js → chunk-DZQWGBTW.js} +2 -2
  24. package/dist/chunks/{chunk-UMLJFZJ5.js → chunk-EDRBKZ3C.js} +2 -2
  25. package/dist/chunks/{chunk-IIKFLH2E.js → chunk-EWNJT27V.js} +2 -2
  26. package/dist/chunks/{chunk-DZLOU6HK.js → chunk-F6KKW5YN.js} +2 -2
  27. package/dist/chunks/{chunk-RGEW3YLB.js → chunk-FPJ2CWLK.js} +2 -2
  28. package/dist/chunks/{chunk-ST6I33C6.js → chunk-H5GBKP57.js} +1 -1
  29. package/dist/chunks/{chunk-CEY7FHMZ.js → chunk-I2OTBVRK.js} +2 -2
  30. package/dist/chunks/{chunk-EWJS4VYF.js → chunk-IXFWR3LH.js} +2 -2
  31. package/dist/chunks/{chunk-M3LK23YR.js → chunk-JCGCJT7T.js} +1 -1
  32. package/dist/chunks/{chunk-GQ4HYF4L.js → chunk-KITNZUYC.js} +2 -2
  33. package/dist/chunks/{chunk-TTBKW2KL.js → chunk-MCEZRRQO.js} +2 -2
  34. package/dist/chunks/{chunk-7V6FRF32.js → chunk-MJHFGGPU.js} +2 -2
  35. package/dist/chunks/{chunk-CAOEREY3.js → chunk-MXOBCQQP.js} +2 -2
  36. package/dist/chunks/{chunk-OWD2GUXJ.js → chunk-NA7EDZ4Q.js} +2 -2
  37. package/dist/chunks/{chunk-2BXB6DRK.js → chunk-PHDTVTKV.js} +1 -1
  38. package/dist/chunks/{chunk-P6HSXCGZ.js → chunk-QLKZ2OFQ.js} +2 -2
  39. package/dist/chunks/{chunk-OVJU5WCL.js → chunk-SDIIGAY5.js} +2 -2
  40. package/dist/chunks/{chunk-CBAEKGP7.js → chunk-SEKOIW43.js} +2 -2
  41. package/dist/chunks/{chunk-AH6IVJRF.js → chunk-TGXUU5NB.js} +1 -1
  42. package/dist/chunks/{chunk-7ZKPFHAO.js → chunk-VWCCPBBG.js} +2 -2
  43. package/dist/chunks/{chunk-CSZCDPF3.js → chunk-XXH5OIUO.js} +2 -2
  44. package/dist/chunks/{chunk-Y4UWFH4Q.js → chunk-Y7VD43TO.js} +2 -2
  45. package/dist/chunks/{chunk-XRBNTYQW.js → chunk-YLUF4WTL.js} +2 -2
  46. package/dist/chunks/{chunk-P2UT6OCU.js → chunk-ZJJJDLRL.js} +2 -2
  47. package/dist/chunks/{chunk-DQ2MIFRQ.js → chunk-ZWSHIUBZ.js} +2 -2
  48. package/dist/descriptive.js +2 -2
  49. package/dist/distributions.js +2 -2
  50. package/dist/entries/regression.d.ts +1 -0
  51. package/dist/entries/regression.js +2 -0
  52. package/dist/linalg.js +2 -2
  53. package/dist/mixed.js +2 -2
  54. package/dist/models/multinom.d.ts +250 -0
  55. package/dist/models/multinom.js +520 -0
  56. package/dist/multivariate.js +2 -2
  57. package/dist/numerics.js +2 -2
  58. package/dist/regression.js +3 -3
  59. package/dist/rstats.js +1 -1
  60. package/dist/special.js +2 -2
  61. package/dist/tests.js +2 -2
  62. package/dist/timeseries.js +2 -2
  63. package/package.json +1 -1
package/dist/tests.js CHANGED
@@ -1,5 +1,5 @@
1
- /*! rStats.js 1.13.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 nn,b as zn,c as nr,d as Nr,e as ks,f as Pa}from"./chunks/chunk-XRBNTYQW.js";import{a as vn,c as an,h as Br,i as vl,m as Nl,n as Wr,o as yl,s as bs,t as Ml,u as xo,v as vs,w as Ns,x as ys}from"./chunks/chunk-NWWY2WMF.js";import{b as Sl}from"./chunks/chunk-CAOEREY3.js";import{a as Wa,e as Ua}from"./chunks/chunk-C7NEFIAH.js";import{a as Es,b as tr,c as Ao,d as _l,e as Ll,f as xs,g as Je,h as ln,i as Nn,j as _t,k as Dl,l as fp,n as Is,o as Io}from"./chunks/chunk-TTBKW2KL.js";import{a as _s}from"./chunks/chunk-5Y425VAJ.js";import"./chunks/chunk-CSZCDPF3.js";import"./chunks/chunk-DQ2MIFRQ.js";import{a as wl,d as ws}from"./chunks/chunk-JTARV4D3.js";import{b as Ut,c as Hl}from"./chunks/chunk-GRXOLZ6J.js";import{d as rt}from"./chunks/chunk-CBAEKGP7.js";import{a as qs}from"./chunks/chunk-2N7PIFPJ.js";import"./chunks/chunk-Y4UWFH4Q.js";import{a as So,b as Va,c as $a,d as Bl,e as ko,f as Yr,g as Kn,h as ja,i as qo,j as Wl,k as Ka,l as mp,m as Ba,n as Ul,o as up,p as hp,q as pp}from"./chunks/chunk-QWXSJEDI.js";import{b as Rt,c as On}from"./chunks/chunk-OWD2GUXJ.js";import{a as Cl,c as Fl,d as Ol,e as vr,g as _o}from"./chunks/chunk-CKQOWTX7.js";import{b as Oa}from"./chunks/chunk-ST6I33C6.js";import{a as ct,b as Rs,c as Ts}from"./chunks/chunk-P6HSXCGZ.js";import{a as jn,b as Hr}from"./chunks/chunk-XRB552MP.js";import{a as zl,b as jl,c as Ss,d as za}from"./chunks/chunk-GQ4HYF4L.js";import{d as ht,e as hn,g as Tn,h as Gn,j as Xn}from"./chunks/chunk-LS55YUPZ.js";import{d as fs,e as ms,g as us,h as Kr,m as pn,n as Dn}from"./chunks/chunk-DZLOU6HK.js";import{a as Kl}from"./chunks/chunk-IIKFLH2E.js";import{c as Pl,h as Vl,j as Lt,m as Da,n as $l,o as As,p as Ur}from"./chunks/chunk-CEY7FHMZ.js";import{a as er,b as ql,g as Ms}from"./chunks/chunk-JJGRQ22M.js";import{a as Fa,b as gt,c as wt,d as bl,e as Bt,f as Wt}from"./chunks/chunk-7V6FRF32.js";import{i as bt,l as ls,t as pl}from"./chunks/chunk-UMLJFZJ5.js";import{a as An,c as Il}from"./chunks/chunk-NRBTDLVW.js";import{b as El,c as xl,d as Rl,e as Tl,f as Al,i as Ro,l as lp,n as J,o as Ze,p as At}from"./chunks/chunk-CCYGRSQW.js";import{a as To,b as $n,c as kl,d as Pt,e as Nt}from"./chunks/chunk-2BXB6DRK.js";import{a as yt,b as gn,c as cn}from"./chunks/chunk-24UIPUSE.js";import"./chunks/chunk-TQ4P6W3P.js";import"./chunks/chunk-7ZKPFHAO.js";import{b as ml,c as ul}from"./chunks/chunk-M3LK23YR.js";import{h as cl,i as ll,k as fl,t as hl}from"./chunks/chunk-KOF2HBUV.js";import{f as Ia,r as Zt,s as ae,t as ie}from"./chunks/chunk-P2UT6OCU.js";import{A as Vt,B as gs,C as Ie,D as Ca,c as _a,d as Eo,e as Pn,f as dn,g as Vn,h as $,i as Ve,j as Qn,k as it,l as at,m as Zn,n as dl,p as hs,q as Tt,r as ps,s as gl,t as ds,u as La,v as Pe,w as vt,x as bn,y as Dt,z as Jn}from"./chunks/chunk-3T4TVNXS.js";import{b as Ot,c as jt,d as pe,e as al,f as De,g as Ce,h as xt,i as ot,j as ft,k as il,l as B,m as Kt}from"./chunks/chunk-AH6IVJRF.js";var Yl=["alternative","mu","paired","varEqual","confLevel"];function yn(e,t,n={}){if(typeof e!="string"&&ct(t)&&B(t,Yl,"tTest"),B(n,Yl,"tTest"),typeof e=="string")return dp(e,t,n);let r;t!==void 0&&!ArrayBuffer.isView(t)&&!Array.isArray(t)?n=t:r=t;let o=Je(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(ln(a),i&&r===void 0)throw new TypeError("y is required when paired is true");let l,f,m;if(i){let[k,C]=Jn(Array.from(e),Array.from(r));({d:l,k:m}=ps(k,C))}else{let k=Ie(r===void 0?[Dt(Array.from(e))]:[Dt(Array.from(e)),Dt(Array.from(r))]);m=k.k,l=k.groups[0],f=k.groups[1]}let u=$(s,-m),h,p,g,d,v,N,b,E=l.length,w=f!==void 0&&c;if(E<1||!w&&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,g=Math.sqrt(x/E),g===0||g<10*Number.EPSILON*Math.abs(y))throw new Error("data are essentially constant");h=(y-u)/g,N=i?"Paired t-test":"One Sample t-test",d=y,v=i?"mean difference":"mean of x",b=i?"mean difference":"mean"}else{let k=f.length;if(k<1||!c&&k<2)throw new Error("not enough 'y' observations");if(c&&E+k<3)throw new Error("not enough observations");let C=it(f),_=k>1?at(f):NaN;if(N=`${c?"":"Welch"} Two Sample t-test`,d=[y,C],v=["mean of x","mean of y"],b="difference in means",c){p=E+k-2;let I=0;E>1&&(I+=(E-1)*x),k>1&&(I+=(k-1)*_),I/=p,g=Math.sqrt(I*(1/E+1/k))}else{let I=x/E,F=_/k;g=Math.sqrt(Math.sqrt(I)**2+Math.sqrt(F)**2),p=ds([I,F],[E-1,k-1])}if(g===0||g<10*Number.EPSILON*Math.max(Math.abs(y),Math.abs(C)))throw new Error("data are essentially constant");h=(y-C-u)/g}let M=typeof d=="number"?d:d[0]-d[1],R=(k,C)=>{let _=u+(h+C*k)*g;return Number.isFinite(_)&&Math.max(Math.abs(u),Math.abs(h*g))<=16*(Math.abs(M)+k*g)?_:M+C*k*g},T,A;if(o==="less")T=gt(h,p,!0,!1),A=[-1/0,R(wt(a,p,!0,!1),1)];else if(o==="greater")T=gt(h,p,!1,!1),A=[R(wt(a,p,!0,!1),-1),1/0];else{T=2*gt(-Math.abs(h),p,!0,!1);let k=1-a,C=k/2<1e-8?wt(k/2,p,!1,!1):wt(1-k/2,p,!0,!1);A=[R(C,-1),R(C,1)]}return A=[$(A[0],m),$(A[1],m)],d=typeof d=="number"?$(d,m):d.map(k=>$(k,m)),g=$(g,m),{method:N,alternative:o,statistic:h,statisticName:"t",parameter:p,parameterName:"df",pValue:T,confInt:A,confLevel:a,estimate:d,estimateNames:v,nullValue:s,nullValueName:b,stdErr:g,stdError:g,warnings:[]}}function dp(e,t,n){if(n.paired!==void 0)throw new Error("cannot use 'paired' in formula method");let r=Es(e,t,!0);if(r.kind==="one")return yn(r.x,n);if(r.kind==="paired")return yn(r.x,r.y,{...n,paired:!0});let[o,s]=r.levels;return{...yn(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 gp=new Set(["n","delta","sd","sigLevel","power","type","alternative","strict","tol"]),wp=["n","p1","p2","sigLevel","power","alternative","strict","tol"],bp=["groups","n","betweenVar","withinVar","sigLevel","power"],Xl=Number.EPSILON**.25;function ke(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 Re=e=>e==null;function fn(e,t,n){if(!Re(e)&&!t(e))throw new RangeError(n)}var Lo=e=>e>=0&&e<=1,Ls=e=>e>0&&e<1/0;function rn(e,t){if(e.filter(Re).length!==1)throw new RangeError(t)}function Ye(e,t,n,r={}){return Ol(e,t,n,r).root}var yr=(e,t,n,r)=>Bt(e,t,{ncp:n,lowerTail:r});function Ha(e,t){if(e!=null&&!(typeof e=="number"&&e>=0&&e<=1))throw new RangeError(t)}var Ya="'sig.level' must be numeric in [0, 1]";function vp(e={}){B(e,gp,"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;rn([t,n,o,r,s],"exactly one of 'n', 'delta', 'sd', 'power', and 'sig.level' must be NULL"),Ha(s,Ya),fn(r,Lo,"'power' must be a probability"),fn(o,Ls,"'sd' must be a positive finite number"),fn(t,g=>g>=2,"number of observations in each group must be at least 2"),fn(n,g=>!Number.isNaN(g),"'delta' must not be NaN"),fn(e.tol,Ls,"'tol' must be a positive finite number");let i=ke(e.type,["two.sample","one.sample","paired"]),c=ke(e.alternative,["two.sided","one.sided"]),a=e.strict??!1,l=e.tol??Xl,f=i==="two.sample"?2:1,m=c==="two.sided"?2:1;m===2&&!Re(n)&&(n=Math.abs(n));let u=(g,d,v,N)=>{let b=Math.max(1e-7,g-1)*f,E=Wt(N/m,b,{lowerTail:!1}),w=Math.sqrt(g/f)*d/v;Number.isFinite(w)||(w=Math.sqrt(g/f)*(d/v));let y=yr(E,b,w,!1);return a&&m===2?y+yr(-E,b,w,!0):y};if(Re(r))r=u(t,n,o,s);else{let g=r;if(Re(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)-g,2,1e7,{tol:l,extendInt:"upX"})}else if(Re(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(v=>u(t,n,v,s)-g,d*1e-7,d*1e7,{tol:l,extendInt:n>0?"downX":"upX"})}else Re(n)?n=Ye(d=>u(t,d,o,s)-g,o*1e-7,o*1e7,{tol:l,extendInt:"upX"}):s=Ye(d=>u(t,n,o,d)-g,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 Np(e={}){B(e,wp,"powerPropTest");let{n:t,p1:n,p2:r,power:o}=e,s=e.sigLevel===void 0?.05:e.sigLevel;rn([t,n,r,o,s],"exactly one of 'n', 'p1', 'p2', 'power', and 'sig.level' must be NULL"),Ha(s,Ya),fn(o,Lo,"'power' must be a probability"),fn(n,Lo,"'p1' must be a probability"),fn(r,Lo,"'p2' must be a probability"),fn(t,u=>u>0,"number of observations in each group must be positive"),fn(e.tol,Ls,"'tol' must be a positive finite number");let i=ke(e.alternative,["two.sided","one.sided"]),c=e.strict??!1,a=e.tol??Xl,l=i==="two.sided"?2:1,f=(u,h,p,g)=>{let d=ie(g/l,{lowerTail:!1});if(c&&l===2){let v=(h+p)/2,N=h*(1-h),b=p*(1-p),E=v*(1-v),w=Math.abs(h-p),y=Math.sqrt(N+b);return ae((Math.sqrt(u)*w-d*Math.sqrt(2*E))/y)+ae((Math.sqrt(u)*w+d*Math.sqrt(2*E))/y,{lowerTail:!1})}return ae((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(Re(o))o=f(t,n,r,s);else{let u=o;if(Re(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(Re(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(Re(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 J("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 Gl=1e16,Cs=(e,t)=>e>Gl&&e<1/0&&t>Gl;function Fs(e,t,n,r){let o=ie(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 ae((o*i-s)/c,{lowerTail:!1})}function yp(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 $($(e,-s)*$(t,-i)*$(n,-c)/$(r,-a),s+i+c-a)}function Mp(e={}){B(e,bp,"powerAnovaTest");let{groups:t,n,betweenVar:r,withinVar:o,power:s}=e,i=e.sigLevel===void 0?.05:e.sigLevel;if(rn([t,n,r,o,s,i],"exactly one of 'groups', 'n', 'between.var', 'within.var', 'power', and 'sig.level' must be NULL"),!Re(t)&&!(t>=2))throw new RangeError("number of groups must be at least 2");if(!Re(n)&&!(n>=2))throw new RangeError("number of observations in each group must be at least 2");Ha(i,Ya),fn(s,Lo,"'power' must be a probability"),fn(r,a=>a>=0&&a<1/0,"'betweenVar' must be a nonnegative finite number"),fn(o,Ls,"'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=yp(a-1,l,f,m));let p=a-1,g=(l-1)*a;return h===1/0?1:Cs(p,g)?Fs(p,g,h,u):Tn(Gn(u,p,g,{lowerTail:!1}),p,g,{ncp:h,lowerTail:!1})};if(Re(s))s=c(t,n,r,o,i);else{let a=s;if(Re(t))t=Ye(l=>c(l,n,r,o,i)-a,2,100);else if(Re(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(Re(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 Re(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 Ep=["correct","p","rescaleP"],xp=["confLevel"],Rp=["alternative","rowScores","colScores"];function Jt(e){let t=[0,0];for(let n of e)t=pe(t,[n,0]);return t}function Xa(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 Qa=e=>e.length>0&&typeof e[0]!="number",Za=(e,t)=>Number.isNaN(e)?NaN:bt(e,t/2,2,!1,!1);function Ql(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 Co(e,t){return e.length>0&&Array.isArray(e[0])?e.map((n,r)=>n.map((o,s)=>Ql(o,t[r][s]))):e.map((n,r)=>Ql(n,t[r]))}var Tp=["none","williams","yates"],Jl="Log likelihood ratio (G-test)";function Ap(e,t={}){B(t,Ep,"gTest");let n=ke(t.correct,Tp);if(typeof e!="object"||e===null||typeof e.length!="number")throw new TypeError("'x' must be an array of counts or of rows");if(Qa(e)){let o=Xa(e);return Zl(o.flat()),o.length===1?J("gTest",Ga(o[0],n,t)):o[0].length===1?J("gTest",Ga(o.map(s=>s[0]),n,t)):J("gTest",Sp(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 Zl(r),J("gTest",Ga(r,n,t))}function Zl(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 ef(e,t){return e<1/0?0:Math.ceil(Math.log2(t))+1}function tf(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=pe(n,ll(o,s))}return n[0]+n[1]}function Sp(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=Jt(e.flat()),s=ef(o[0],n*r),i=s===0?e:e.map(b=>b.map(E=>$(E,-s))),c=s===0?o:Jt(i.flat()),a=i.map(b=>Jt(b)),l=i[0].map((b,E)=>Jt(i.map(w=>w[E]))),f=a.map(b=>l.map(E=>Ce(b,ot(E,c)))),m=i;if(t==="yates"){let[[b,E],[w,y]]=i,x=b*y-E*w>0?-.5:.5,M=$(.5,-s),R=x<0?-M:M;m=[[b+R,E-R],[w-R,y+R]]}let u=tf(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 b=c[0]+c[1],E=a.reduce((y,x)=>y+1/(x[0]+x[1]),0),w=l.reduce((y,x)=>y+1/(x[0]+x[1]),0);p=1+$((b*E-1)*(b*w-1)/(6*b*(n-1)*(r-1)),-s)}let g=$(2*u,s)/p,d=(n-1)*(r-1),v=f.map(b=>b.map(E=>$(E[0]+E[1],s)));return{method:`${Jl} test of independence ${t==="none"?"without correction":t==="williams"?"with Williams' correction":"with Yates' correction"}`,statistic:g,statisticName:"G",parameter:d,parameterName:"X-squared df",pValue:Za(g,d),observed:s===0?m:m.map(b=>b.map(E=>$(E,s))),expected:v,ftResiduals:Co(e,v),warnings:h}}function Ga(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(b=>typeof b!="number"||Number.isNaN(b)))throw new RangeError("'p' must not contain missing values");if(s.some(b=>b<0))throw new RangeError("probabilities must be non-negative");if(s.some(b=>b===1/0))throw new RangeError("probabilities must be finite");let i=Jt(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=Jt(e),a=ef(c[0],r),l=a===0?e:e.map(b=>$(b,-a)),f=a===0?c:Jt(l),m=s.map(b=>o?ot(xt(f,b),i):xt(f,b)),u=m.map(b=>b[0]+b[1]),h=l;if(t==="yates"){let b=$(.5,-a),E=l[0]-u[0];E>$(.25,-a)?h=[l[0]-b,l[1]+b]:-E>$(.25,-a)&&(h=[l[0]+b,l[1]-b])}let p=tf(h,m);if(!o&&!Number.isNaN(p)){let b=f[0]+f[1];p-=b*(i[0]-1+i[1])}let g=Number.isNaN(p)?["NaNs produced"]:[],d=t==="williams"?1+(r+1)/(6*$(f[0]+f[1],a)):1,v=$(2*p,a)/d,N=u.map(b=>$(b,a));return{method:`${Jl} goodness of fit test`,statistic:v,statisticName:"G",parameter:r-1,parameterName:"X-squared df",pValue:Za(v,r-1),observed:a===0?h:h.map(b=>$(b,a)),expected:N,ftResiduals:Co(e,N),warnings:g}}function kp(e){if(typeof e!="object"||e===null||!Qa(e))throw new RangeError("'x' must be square with at least two rows and columns");let t=Xa(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 qp(e){let t=kp(e),n=Jt(t.flat())[0],r=n>2**400||n>0&&n<2**-400?Math.round(Math.log2(n)):0,o=r===0?t:t.map(w=>w.map(y=>$(y,-r))),s=o.length,i=o.map(w=>Jt(w)),c=o.map((w,y)=>Jt(o.map(x=>x[y]))),a=o.map((w,y)=>o[y].some((x,M)=>M!==y&&x>0)||o.some((x,M)=>M!==y&&x[y]>0)).flatMap((w,y)=>w?[y]:[]),l=a.length;if(l<2)throw new RangeError("Too many equal marginals, cannot compute");let f=a.map((w,y)=>y),m=w=>f[w]===w?w:f[w]=m(f[w]);for(let w=0;w<l;w++)for(let y=w+1;y<l;y++)o[a[w]][a[y]]+o[a[y]][a[w]]>0&&(f[m(w)]=m(y));if(a.some((w,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(w=>{let y=pe(i[w],[-c[w][0],-c[w][1]]);return y[0]+y[1]}),p=a.slice(0,u).map(w=>a.slice(0,u).map(y=>{if(w!==y)return-(o[w][y]+o[y][w]);let x=0;for(let M=0;M<s;M++)M!==w&&(x+=o[w][M]+o[M][w]);return x})),g=p.map(w=>w.map(()=>0)),d=[];for(let w=0;w<u;w++){let y=p[w][w];for(let x=0;x<w;x++)y-=g[w][x]*g[w][x]*d[x];d.push(y);for(let x=w+1;x<u;x++){let M=p[x][w];for(let R=0;R<w;R++)M-=g[x][R]*g[w][R]*d[R];g[x][w]=M/y}}let v=0,N=[];for(let w=0;w<u;w++){let y=h[w];for(let x=0;x<w;x++)y-=g[w][x]*N[x];N.push(y),v+=y*y/d[w]}let b=Jt(a.flatMap(w=>a.map(y=>t[w][y]))),E=l-1;return v=$(v,r),J("stuartMaxwellTest",{method:"Stuart-Maxwell test",statistic:v,statisticName:"chi-squared",parameter:E,parameterName:"df",pValue:Za(v,E),N:b[0]+b[1],warnings:[]})}function Ip(e,t={}){B(t,xp,"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=nf(e),o=Jt(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(w=>w.map(y=>$(y,-s))),{pc:c,pd:a}=_p(i),l=[0,0],f=[0,0];i.forEach((w,y)=>w.forEach((x,M)=>{l=pe(l,xt(c[y][M],x)),f=pe(f,xt(a[y][M],x))})),l=[l[0]/2,l[1]/2],f=[f[0]/2,f[1]/2];let m=pe(l,f),u=ot(De(l,f),m)[0],h=Ce(m,m),p=i.map((w,y)=>w.map((x,M)=>2*ot(De(Ce(f,c[y][M]),Ce(l,a[y][M])),h)[0])),g=Math.max(...p.flat().map(Math.abs)),d=g>0&&g<1/0?Math.floor(Math.log2(g)):0,v=0;i.forEach((w,y)=>w.forEach((x,M)=>{let R=$(p[y][M],-d);v+=x*R*R}));let N=$(Math.sqrt(v),d-s/2),b=(1-n)/2,E=b<1e-8?ie(b,{lowerTail:!1}):ie(1-b);return J("goodmanKruskalGamma",{estimate:u,confInt:[Math.max(-1,u-E*N),Math.min(1,u+E*N)],confLevel:n,ase:N,warnings:[]})}function nf(e){if(typeof e!="object"||e===null||!Qa(e))throw new TypeError("'table' must be an array of rows");let t=Xa(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 _p(e){let t=e.length,n=e[0].length,r=[0,0],o=e.map(p=>{let g=[],d=r;for(let v=0;v<n;v++)g.push(d),d=pe(d,[p[v],0]);return g}),s=e.map(p=>{let g=new Array(n),d=r;for(let v=n-1;v>=0;v--)g[v]=d,d=pe(d,[p[v],0]);return g}),i=p=>{let g=[],d=new Array(n).fill(r);for(let v=0;v<t;v++)g.push(d),d=d.map((N,b)=>pe(N,p[v][b]));return g},c=p=>{let g=new Array(t),d=new Array(n).fill(r);for(let v=t-1;v>=0;v--)g[v]=d,d=d.map((N,b)=>pe(N,p[v][b]));return g},a=i(o),l=c(s),f=i(s),m=c(o),u=a.map((p,g)=>p.map((d,v)=>pe(d,l[g][v]))),h=f.map((p,g)=>p.map((d,v)=>pe(d,m[g][v])));return{pc:u,pd:h}}function Lp(e,t={}){B(t,Rp,"lblTest");let n=Je(t.alternative),r=nf(e),o=r.length,s=r[0].length,i=(R,T,A)=>{if(R===void 0)return Array.from({length:T},(C,_)=>_+1);let k=Array.from(R);if(k.length!==T)throw new RangeError(`'${A}' must have one score for each ${A==="rowScores"?"row":"column"}`);if(k.some(C=>typeof C!="number"||!Number.isFinite(C)))throw new RangeError(`'${A}' must be finite numbers`);return k},c=i(t.rowScores,o,"rowScores"),a=i(t.colScores,s,"colScores"),f=Jt(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=>$(T,-f))),u=m.map(R=>Jt(R)).map(R=>R[0]+R[1]),h=m[0].map((R,T)=>Jt(m.map(A=>A[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 g=u.reduce((R,T,A)=>R+T*c[A],0)/p,d=h.reduce((R,T,A)=>R+T*a[A],0)/p,v=c.map(R=>R-g),N=a.map(R=>R-d),b=u.reduce((R,T,A)=>R+T*v[A]*v[A],0),E=h.reduce((R,T,A)=>R+T*N[A]*N[A],0),w=0;if(m.forEach((R,T)=>R.forEach((A,k)=>w+=A*v[T]*N[k])),!(b>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=w/Math.sqrt(b)/Math.sqrt(E),x=f===0?Math.sqrt(p-1)*y:$(Math.sqrt(p-$(1,-f))*y,f/2),M=n==="less"?ae(x):n==="greater"?ae(x,{lowerTail:!1}):2*ae(-Math.abs(x));return J("lblTest",{method:"Linear-by-Linear Association Test",alternative:n,statistic:x,statisticName:"Z",pValue:M,warnings:[]})}var rf=["correct","p","rescaleP","simulatePValue","B","nMinus1"];function wn(e){let t=[0,0],n=0;for(let r of e)t=pe(t,[r,0]),n+=r;return Number.isFinite(n)?t[0]:n}function Cp(e,t,n){return ct(t)&&B(t,rf,"chisqTest"),B(n,rf,"chisqTest"),Array.isArray(t)?J("chisqTest",Fp(e,t,n??{})):J("chisqTest",Op(e,t??{}))}var af="Chi-squared approximation may be incorrect";function Fp(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(An),c=[...new Set(s)].sort(An);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])]++),{...uf(f,n),rowLevels:i,colLevels:c}}function Op(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?Ja(r[0],t):o===1?Ja(r.map(s=>s[0]),t):uf(r,t)}return Ja(Array.from(e),t)}function cf(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 lf(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 ff(e,t){return Number.isNaN(e)?NaN:bt(e,t/2,2,!1,!1)}function mf(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 Ja(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(g=>!(g>=0&&g<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=wn(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(g=>g<0))throw new RangeError("probabilities must be non-negative");if(o.some(g=>g===1/0))throw new RangeError("probabilities must be finite");if(t.rescaleP??!1){let g=wn(o),d=g<1/0?0:Math.ceil(Math.log2(n)),v=d===0?o:o.map(b=>$(b,-d)),N=d===0?g:wn(v);o=v.map(b=>b/N)}else if(Math.abs(wn(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(g=>$(g,-s)),c=s===0?r:wn(i),a=o.map(g=>c*g),l=s===0?a:a.map(g=>$(g,s));mf(l);let f=o.map((g,d)=>a[d]*(1-g)),m=$(wn(i.map((g,d)=>Vt(g-a[d],a[d]))),s),u="Chi-squared test for given probabilities",h=n-1,p;if(t.simulatePValue??!1){let g=cf(t);if(!Number.isInteger(r))throw new RangeError("the simulation needs counts summing to an integer");u+=To(g),h=NaN,p=lf(Dp(r,o,l,g),m,g)}else p=ff(m,h);return{method:u,statistic:m,statisticName:"X-squared",parameter:h,parameterName:"df",pValue:p,observed:e,expected:l,residuals:i.map((g,d)=>$((g-a[d])/Math.sqrt(a[d]),s/2)),stdres:i.map((g,d)=>$((g-a[d])/Math.sqrt(f[d]),s/2)),ftResiduals:Co(e,l),warnings:Number.isFinite(h)&&l.some(g=>g<5)?[af]:[]}}function Dp(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=Sl(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 Pp(e,t,n,r){let o=e.length,s=t.length,i=e.reduce((f,m)=>f+m,0);if(i>vs)throw new RangeError("the simulation needs a total count of at most 16777216 (2^24)");let c=Ns(i),a=new Int32Array(o*s),l=new Float64Array(Math.trunc(r));for(let f=0;f<l.length;f++){ys(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 uf(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(b=>!(b>=0&&b<1/0)))throw new RangeError("all entries of 'x' must be non-negative and finite");let c=wn(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(b=>wn(b)),l=e[0].map((b,E)=>wn(e.map(w=>w[E]))),f=a.map(b=>l.map(E=>gs(b,E,c)));mf(f.flat());let m=e.map((b,E)=>b.map((w,y)=>w-f[E][y])),u="Pearson's Chi-squared test",h,p=(o-1)*(s-1),g,d=(t.simulatePValue??!1)&&a.every(b=>b>0)&&l.every(b=>b>0),v=!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(w=>w.some(y=>!Number.isInteger(y))))throw new RangeError("the simulation needs integer counts");let b=cf(t);u+=To(b),h=wn(m.flatMap((w,y)=>w.map((x,M)=>Vt(x,f[y][M])))),p=NaN;let E=Pp(a.map(w=>Math.trunc(w)),l.map(w=>Math.trunc(w)),f,b);g=lf(E,h,b)}else if(v>0)u+=" with Yates' continuity correction",h=wn(m.flatMap((b,E)=>b.map((w,y)=>Vt(Math.abs(w)-v,f[E][y]))));else if(h=wn(m.flatMap((b,E)=>b.map((w,y)=>Vt(w,f[E][y])))),n){u="Pearson's 'N-1' Chi-squared test";let b=h*(c-1);h=Number.isFinite(b)?b/c:h*((c-1)/c)}return{method:u,statistic:h,statisticName:"X-squared",parameter:p,parameterName:"df",pValue:g??ff(h,p),observed:e,expected:f,residuals:m.map((b,E)=>b.map((w,y)=>w/Math.sqrt(f[E][y]))),stdres:Vp(e,c,a,l,f,m),ftResiduals:Co(e,f),warnings:Number.isFinite(p)&&f.some(b=>b.some(E=>E<5))?[...N,af]:N}}function Vp(e,t,n,r,o,s){if(!Number.isFinite(t))return s.map((p,g)=>p.map((d,v)=>d/Math.sqrt(o[g][v]*(1-n[g]/t)*(1-r[v]/t))));let i=e.map(p=>of(p)),c=e[0].map((p,g)=>of(e.map(d=>d[g]))),a=zp(i),l=sf(i),f=sf(c),m=l.map(p=>ot(p,a)),u=c.map(p=>ot(p,a)),h=f.map(p=>ot(p,a));return e.map((p,g)=>p.map((d,v)=>{let N=Ce(i[g],u[v]),b=De([d,0],N),E=b[0]+b[1],w=Ce(Ce(N,m[g]),h[v]),y=w[0]+w[1];if(y>=$p||E===0)return E/Math.sqrt(y);let x=N[0]+N[1],M=m[g][0]+m[g][1],R=h[v][0]+h[v][1];return E/Math.sqrt(x)/Math.sqrt(M)/Math.sqrt(R)}))}var $p=2**-1022;function of(e){let t=[0,0];for(let n of e)t=pe(t,[n,0]);return t}function zp(e){let t=[0,0];for(let n of e)t=pe(t,n);return t}function sf(e){let t=e.length,n=new Array(t+1);n[t]=[0,0];for(let s=t-1;s>=0;s--)n[s]=pe(e[s],n[s+1]);let r=[],o=[0,0];for(let s=0;s<t;s++)r.push(pe(o,n[s+1])),o=pe(o,e[s]);return r}var jp=Math.pow(Number.EPSILON,.25);function Ht(e,t,n,r={}){let o={tol:r.tol??jp,maxiter:r.maxIter??1e3};return r.fLower!==void 0&&(o.fLower=r.fLower),r.fUpper!==void 0&&(o.fUpper=r.fUpper),vr(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?Ht(n,r,s,{tol:2**-50,fLower:o,fUpper:i}):Ht(n,s,r,{tol:2**-50,fLower:i,fUpper:o});return Math.exp(c)}var Kp=["alternative","confInt","confLevel","or","simulatePValue","B","midp"];function Do(e,t={}){B(t,Kp,"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(ul));if(s.flat().some(f=>f>2147483647))throw new RangeError("'x' has entries too large to be integer");let i=[],c=Bp(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 J("fisherTest",{...Yp(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 J("fisherTest",{method:a+To(f),alternative:l,pValue:Wp(s,f),warnings:i})}return J("fisherTest",{method:a,alternative:l,pValue:Gp(s),warnings:i})}function Bp(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: ${kl(a)}`:null}function Wp(e,t){let n=e.filter(d=>d.some(v=>v>0)),r=e[0].map((d,v)=>v).filter(d=>e.some(v=>v[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(v=>d[v])),s=o.map(d=>d.reduce((v,N)=>v+N,0)),i=o[0].map((d,v)=>o.reduce((N,b)=>N+b[v],0)),c=s.length,a=i.length,l=[0,0];for(let d of o.flat())l=pe(l,[-ws(d),0]);let f=l[0]/(1+64*Number.EPSILON),m=s.reduce((d,v)=>d+v,0);if(m>vs)throw new RangeError("the simulation needs a total count of at most 16777216 (2^24)");let u=Ns(m),h=new Int32Array(c*a),p=0,g=Math.trunc(t);for(let d=0;d<g;d++){ys(s,i,u,h);let v=0;for(let N=0;N<h.length;N++)v-=u[h[N]];v<=f&&p++}return Math.max(0,Math.min(1,(1+p)/(g+1)))}var hf=1+1e-7,Up=16,Hp=2**24;function Fo(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=pe(r,[e[o],0]));return r[0]}var Gr=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%Up===0||!(s>0&&s<1/0)?(o=vn(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=Fo(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=pe(r,[(this.lo+o)*n[o],0]));return r[0]}tail(t,n,r){if(n===1)return r?an(t-1,this.m,this.n,this.k,!1,!1):an(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?Fo(o,s,o.length-1):Fo(o,0,s)}midTail(t,n,r){if(n===1){let i=.5*vn(t,this.m,this.n,this.k,!1);return(r?an(t,this.m,this.n,this.k,!1,!1):an(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?Fo(o,s+1,o.length-1):Fo(o,0,s-1))+.5*o[s]}};function ti(e,t,n){let r=e.density(n),o=r[t-e.lo]*hf,s=[0,0];for(let i of r)i<=o&&(s=pe(s,[i,0]));return s[0]}function pf(e,t,n,r){let o=Math.max(0,r-n),s=Math.min(r,t),i=g=>vn(g,t,n,r,!0),c=i(e)+Math.log(hf),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=(g,d)=>{let v=g,N=d+1;for(;N-v>1;){let b=v+Math.floor((N-v)/2);i(b)<=c?v=b:N=b}return v},f=(g,d)=>{let v=g-1,N=d+1;for(;N-v>1;){let b=v+Math.floor((N-v)/2);i(b)<=c?N=b:v=b}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:an(m,t,n,r,!0,!1),p=u>s?0:an(u-1,t,n,r,!1,!1);return h+p}function Oo(e,t){return rr(e,t)}function Yp(e,t,n){let r=Je(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>Hp)throw new RangeError(`the exact distribution of the 2 x 2 table has ${m} values, more than 2^24`);let u=new Gr(i+a,c+l,i+c),{lo:h,hi:p}=u,g=t.midp??!1,d;g?(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=ti(u,f,o);let v;if(f===h)v=0;else if(f===p)v=1/0;else{let y=u.mean(1);v=y===f?1:Oo(x=>u.mean(x)-f,y>f)}let N={method:n,alternative:r,pValue:d,estimate:v,estimateNames:"odds ratio",nullValue:o,nullValueName:"odds ratio",warnings:[]};if(!(t.confInt??!0))return N;let b=y=>{if(f===p)return 1/0;if(g){if(f===h&&y>=.5)return 0;let M=u.midTail(f,1,!1);return M===y?1:Oo(R=>u.midTail(f,R,!1)-y,M<y)}let x=u.tail(f,1,!1);return x===y?1:Oo(M=>u.tail(f,M,!1)-y,x<y)},E=y=>{if(f===h)return 0;if(g){if(f===p&&y>=.5)return 1/0;let M=u.midTail(f,1,!0);return M===y?1:Oo(R=>u.midTail(f,R,!0)-y,M>y)}let x=u.tail(f,1,!0);return x===y?1:Oo(M=>u.tail(f,M,!0)-y,x>y)},w;if(r==="less")w=[0,b(1-s)];else if(r==="greater")w=[E(1-s),1/0];else{let y=(1-s)/2;w=[E(y),b(y)]}return{...N,confInt:w,confLevel:s}}var Os=[[0,0]];function Sn(e){for(let t=Os.length;t<=e;t++)Os.push(pe(Os[t-1],il([t,0])));return Os[e]}function Ds(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 ei=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 Gp(e){let t=e.filter(b=>b.some(E=>E>0));if(t.length===0)throw new RangeError("all entries of the table are zero");let n=t[0].map((b,E)=>t.some(w=>w[E]>0));if(t=t.map(b=>b.filter((E,w)=>n[w])),t.length<2||t[0].length<2)return 1;t.length>t[0].length&&(t=t[0].map((b,E)=>t.map(w=>w[E])));let r=t.length,o=t.map(b=>b.reduce((E,w)=>E+w,0)),s=t[0].map((b,E)=>t.reduce((w,y)=>w+y[E],0)).sort((b,E)=>E-b),i=s.length,c=o.reduce((b,E)=>b+E,0),a=al(Sn(c));for(let b of o)a=pe(a,Sn(b));for(let b of s)a=pe(a,Sn(b));let l=[0,0];for(let b of t.flat())l=De(l,Sn(b));let f=pe(l,[Math.log1p(1e-7),0]),m=1e-9*(1+Math.abs(f[0])),[u,h]=f,p=Array.from({length:i},()=>new Map),g=(b,E)=>{let w=E.join(","),y=p[b].get(w);if(y!==void 0)return y;let x=pe(a,Sn(E.reduce((C,_)=>C+_,0)));for(let C of E)x=De(x,Sn(C));for(let C=b;C<i;C++)x=De(x,Sn(s[C]));let M={R:E,KZ:x,max:0,min:0,edges:[],pastHi:[],pastLo:[],count:[],index:new Map};if(p[b].set(w,M),b===i-1){let C=[0,0];for(let _ of E)C=De(C,Sn(_));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 A=new Array(r).fill(0),k=(C,_,I)=>{if(C===r-1){A[C]=_;let D=De(I,Sn(_)),S=E.map((W,Q)=>W-A[Q]).sort((W,Q)=>W-Q),O=g(b+1,S),q=`${S.join(",")}|${D[0]}`,V=R.get(q);V===void 0?R.set(q,{to:O,hi:D[0],lo:D[1],mult:1}):V.mult++;return}let F=Math.max(0,_-T[C+1]),L=Math.min(E[C],_);for(let D=F;D<=L;D++)A[C]=D,k(C+1,_-D,De(I,Sn(D)))};if(k(0,s[b],[0,0]),M.edges=[...R.values()],M.max=yt(M.edges.map(C=>C.hi+C.to.max)),M.min=cn(M.edges.map(C=>C.hi+C.to.min)),b===i-2){let C=M.edges.map(O=>{let q=O.to.edges[0];return{v:pe([O.hi,O.lo],[q.hi,q.lo]),mult:O.mult}});C.sort((O,q)=>O.v[0]-q.v[0]||O.v[1]-q.v[1]);let _=C.length,I=C[_-1].v[0],F=new Float64Array(_),L=new Float64Array(_),D=new Float64Array(_),S=[0,0];C.forEach(({v:O,mult:q},V)=>{F[V]=O[0],L[V]=O[1];let W=De(O,[I,0]);S=pe(S,[q*Math.exp(W[0])*(1+W[1]),0]),D[V]=S[0]}),M.dist={hi:F,lo:L,cum:D,vmax:I,kv:pe(a,[I,0])}}return M},d=g(0,[...o].sort((b,E)=>b-E)),v=new ei,N=(b,E,w,y)=>{if(E+b.max<=u-m){let[k,C]=Ds(b.KZ[0],b.KZ[1],E,w);return v.add(k,C,y),!0}if(E+b.min>u+m)return!0;let x=b.dist;if(x===void 0)return!1;let[M,R]=Ds(u,h,-E,-w),T=0,A=x.hi.length;for(;T<A;){let k=T+A>>>1,C=x.hi[k];C<M||C===M&&x.lo[k]<=R?T=k+1:A=k}if(T>0){let[k,C]=Ds(x.kv[0],x.kv[1],E,w);v.add(k,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 b=0;b<i-2;b++)for(let E of p[b].values()){let{pastHi:w,pastLo:y,count:x}=E;if(x.length!==0){for(let{to:M,hi:R,lo:T,mult:A}of E.edges)for(let k=0;k<x.length;k++){let[C,_]=Ds(w[k],y[k],R,T),I=x[k]*A;if(N(M,C,_,I))continue;let F=M.index.get(C);F===void 0?(M.index.set(C,M.count.length),M.pastHi.push(C),M.pastLo.push(_),M.count.push(I)):M.count[F]+=I}E.pastHi=E.pastLo=E.count=[],E.index.clear()}}return Math.min(1,v.value())}function Ps(e){let t=e.every(n=>Number.isInteger(n))?1:2;return[t,e.map(n=>n*t).sort((n,r)=>n-r)]}function ri(e){return[...e].sort((t,n)=>t-n).join(",")}function ni(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 oi(e){let[,t]=Ps(e),n=0,r=0;for(let o of t)r+=n+=o;return{steps:r,size:n+1}}function si(e){let[t,n]=Ps(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 ni(t,0,o)}function ai(e,t){let[,n]=Ps(e),r=n.length,o=Math.min(t,r-t),s=df(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 df(e){let t=[0];for(let n of e)t.push(t[t.length-1]+n);return t}function ii(e,t){let[n,r]=Ps(e),o=r.length,s=Math.min(t,o-t),i=df(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 g=Math.min(h,s);g>=Math.max(1,s-(o-h));g--){let d=c[g],v=c[g-1],N=g/h,b=(h-g)/h,E=i[g],w=i[h]-i[h-g],y=i[g-1]+p,x=Math.max(E,y);for(let M=w;M>=x;M--)d[M-E]=b*d[M-E]+N*v[M-y];for(let M=x-1;M>=E;M--)d[M-E]=b*d[M-E]}}let a=c[s],l=t*(t+1)/2;if(s===t)return ni(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 ni(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 Mr(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 gf=["alternative","mu","paired","exact","correct","confInt","confLevel","tolRoot"];function Po(e,t,n={}){if(typeof e!="string"&&ct(t)&&B(t,gf,"wilcoxTest"),B(n,gf,"wilcoxTest"),typeof e=="string"){if(n.paired!==void 0)throw new Error("cannot use 'paired' in formula method");let g=Es(e,t,!0);return g.kind==="one"?Po(g.x,n):Po(g.x,g.y,g.kind==="paired"?{...n,paired:!0}:n)}let r;t!==void 0&&!ArrayBuffer.isView(t)&&!Array.isArray(t)?n=t:r=t;let o=Je(n.alternative),s=n.mu??0,i=n.paired??!1,c=n.exact??null,a=Qp(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[g,d]=Jn(Array.from(e),Array.from(r));u=Dt(g.map((v,N)=>v-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 At};return J("wilcoxTest",h===void 0?nd(u,c,i,p):fd(u,h,c,p))}function Qp(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 Vs(e,t,n){return n?t==="two.sided"?Math.sign(e)*.5:t==="greater"?.5:-.5:0}var vf=10*Number.EPSILON;function Nf(e,t,n=0){let r=a=>Math.min(1,Math.max(a,0)),o=()=>n===0?ae(e):r(ae(e)-n*Zt(e)),s=()=>n===0?ae(e,{lowerTail:!1}):r(ae(e,{lowerTail:!1})+n*Zt(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 Zp(e){let t=e*e;return[e*(t-3),e*(t*(t-10)+15),e*(t*(t*(t-21)+105)-105)]}function Jp(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 yf(e,o,s,n)}function ed(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 yf(e,i,c,r)}function yf(e,t,n,r){let[o,s,i]=Zp(e),c=t*o;return r>=2&&(c+=n*s),r>=3&&(c+=t*t/2*i),c}function fi(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 mi=(e,t)=>e-t,ci=5e9,td=40;function li(e){return new RangeError(`wilcoxTest: the exact conditional ${e} would take too long or too much memory: use exact: false`)}function $s(e,t){if(e.size>16777216||e.steps>ci)throw li(t);return e.steps}function Mf(e){let t=e.length,n=vt(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 bn(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 nd(e,t,n,r){let{alternative:o,mu:s}=r,i=e.map(w=>w-s);i.some((w,y)=>!Number.isFinite(w)&&Number.isFinite(e[y]))&&(i=e.map(w=>w/2-s/2));let a=i.some(w=>w===0),l=t??i.length<50,f=(w,y,x)=>({method:w,alternative:o,statistic:y,statisticName:"V",pValue:x,nullValue:s,nullValueName:n?"location shift":"location",warnings:r.warnings.list});if(l){let w=vt(i.map(Math.abs)),y=bn(w).length>0,x=0;for(let C=0;C<i.length;C++)i[C]>0&&(x+=w[C]);let M="Wilcoxon signed rank exact test";if(!y&&!a){let C=i.length,_=C*(C+1)/2,I;if(o==="less")I=Br(x,C,!0,!1);else if(o==="greater")I=Br(x-1,C,!1,!1);else{let L=x>_/2?Br(x-1,C,!1,!1):Br(x,C,!0,!1);I=Math.min(1,2*L)}let F=f(M,x,I);return r.confInt?{...F,...rd(e,r)}:F}let R=w.filter((C,_)=>i[_]!==0);$s(oi(R),"distribution");let T=si(R),A;if(o==="less")A=or(T,x);else if(o==="greater")A=Mr(T,x-.25);else{let C=0;for(let _ of R)C+=_/2;A=Math.min(1,2*(x>C?Mr(T,x-.25):or(T,x)))}let k=f(M,x,A);return r.confInt?{...k,...sd(e,r)}:k}let m=a?i.filter(w=>w!==0):i,u=m.length;if(u===0)throw new RangeError("no non-zero differences for the signed rank test");let[h,p,g]=Mf(m),v=bn(vt(m.map(Math.abs))).length>0||a?Math.min(r.correct,0):r.correct,N=(p-Vs(p,o,v>=0))/g,b=Nf(N,o,v>=1?Jp(N,u,v):0),E=f(`Wilcoxon signed rank test${r.correct>=0?" with continuity correction":""}`,h,b);return r.confInt?{...E,...ad(e,r)}:E}function ui(e,t,n){return e==="less"?[-1/0,n]:e==="greater"?[t,1/0]:[t,n]}function wf(e,t,n){let r={confInt:ui(e,NaN,NaN),confLevel:0,estimate:t};return n!==void 0&&(r.estimateNames=n),r}function Ef(e){if(!(e>0&&e<1/0))throw new RangeError("tolRoot must be a positive finite number")}function xf(e){let t=e.length,n=[];for(let r=0;r<t;r++)for(let o=r;o<t;o++){let s=gn(e[r],e[o]);Number.isNaN(s)||n.push(s)}return n.sort(mi)}function Rf(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+vf&&l++;let f=fi(e);if(l===0)return{confInt:[-1/0,1/0],confLevel:1,estimate:f,estimateNames:o};let m=t(l-1);return{confInt:ui(s,e[l-1],e[c-l]),confLevel:1-(s==="two.sided"?2*m:m),estimate:f,estimateNames:o}}function rd(e,t){let n=e.length;return Rf(xf(e),r=>Br(r,n,!0,!1),r=>vl(r,n,!0,!1),t,"(pseudo)median")}function Tf(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 Af(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)+vf,g=f!=="less",d=f!=="greater",v=I=>gn(e[I-1],e[I]),N=Math.min(u-1,Math.max(1,Math.ceil(i.mean+ie(Math.min(p,1))*i.sd+.1*i.sd+3))),b=8,E=I=>I===0?e[0]-1:v(I),w=I=>I===0?e[u-1]+1:v(u-I),y=[new Map,new Map];{let I=new Map,F=0,L=td*c;for(let[D,S]of[[0,E],[1,w]])if(!(D===0?!g:!d))for(let O=0;O<=N;O++){if(F+=L,F>ci)throw li("interval");let q;try{q=t(S(O))}catch{break}if(y[D].set(O,[q.stat,q.key]),I.has(q.key)){I.delete(q.key),I.set(q.key,!0);continue}if(F+=r(q.scores),F>ci)throw li("interval");I.set(q.key,!0),I.size>b&&I.delete(I.keys().next().value)}}let x=new Map,M=(I,F)=>{let L=I===0?E(F):w(F),D=y[I].get(F),S;if(D===void 0){let W=t(L);D=[W.stat,W.key],S=W.scores}let[O,q]=D,V=x.get(q);return V!==void 0?x.delete(q):(V=n(S??t(L).scores),x.size>=b&&x.delete(x.keys().next().value)),x.set(q,V),[O,V]},R=I=>{let[F,L]=M(0,I);return o(L,F)},T=I=>{let[F,L]=M(1,I);return s(L,F)},A=-1/0,k=0;if(g){let I=R(0);if(!(I>p)){let F=I,L=1;for(;L<=u-1;L++){let D=R(L);if(D>p)break;F=D}A=e[L-1],k=F}}let C=1/0,_=0;if(d){let I=T(0);if(!(I>p)){let F=I,L=u-1;for(;L>=1;L--){let D=T(u-L);if(D>p)break;F=D}C=e[L],_=F}}return{confInt:[A,C],confLevel:1-(k+_),estimate:fi(e),estimateNames:l}}function od(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 sd(e,t){let n=e.length,r=s=>{let i=od(e,s),{ranks:c,key:a}=Tf(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 Af(xf(e),r,si,s=>$s(oi(s),"interval"),(s,i)=>Mr(s,i-.25),or,{mean:o/2,sd:Math.sqrt(n*(n+1)*(2*n+1)/24)},n,t,"(pseudo)median")}function ad(e,t){let{alternative:n,confLevel:r,tolRoot:o}=t,s=t.correct>=0,i=e.length;if(i===0)return wf(n,NaN);let c=cn(e),a=yt(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"),wf(n,gn(c,a),"midrange");let l=(b,E)=>{let[,w,y]=Mf(e.map(x=>x-b).filter(x=>x!==0));return(w-Vs(w,n,E))/y},f=l(c,s),m=l(a,s),u=b=>-ie(b),h=b=>n==="greater"?!(f<u(b)):n==="less"?!(m>ie(b/2)):!(f<u(b/2))&&!(m>ie(b/2)),p=1-r,g=!1;for(;!h(p)&&p<1;)p*=2,g=!0;g&&t.warnings.add("requested conf.level not achievable"),Ef(o);let d=b=>Ht(E=>l(E,s)-b,c,a,{tol:o,fLower:f-b,fUpper:m-b}),v;if(p>=1){let b=fi([...e].sort(mi));v=ui(n,b,b)}else n==="greater"?v=[d(u(p)),1/0]:n==="less"?v=[-1/0,d(ie(p))]:v=[d(u(p/2)),d(ie(p/2))];let N=Ht(b=>l(b,!1),c,a,{tol:o,fLower:l(c,!1),fUpper:l(a,!1)});return{confInt:v,confLevel:g?1-Math.min(1,p):r,estimate:N,estimateNames:"(pseudo)median"}}function Sf(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 bn(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 id(e,t){let[n,r,o]=Sf(vt([...e,...t]),e.length);return[n,r,o]}function cd(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 ld(e,t,n){return kf(e,t,n).ranks}function kf(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=vt([...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:ri(i)}}if(Number.isFinite(n)&&r.some((o,s)=>!Number.isFinite(o)&&Number.isFinite(e[s]))){let o=cd(e.map(s=>s/2-n/2),t);return{ranks:o,key:ri(o)}}return Tf([...r,...t])}function fd(e,t,n,r){let{alternative:o,mu:s}=r,i=e.length,c=t.length,a=n??(i<50&&c<50),l=ld(e,t,s),[f,m,u,h]=Sf(l,i),p,g;if(a)if(g="Wilcoxon rank sum exact test",!h)p=bf(()=>o==="less"?Wr(f,i,c,!0,!1):o==="greater"?Wr(f-1,i,c,!1,!1):Math.min(2*Wr(f,i,c,!0,!1),2*Wr(f-1,i,c,!1,!1),1));else{$s(ai(l,i),"distribution");let N=ii(l,i);o==="less"?p=or(N,f):o==="greater"?p=Mr(N,f-.25):p=Math.min(2*or(N,f),2*Mr(N,f-.25),1)}else{g=`Wilcoxon rank sum test${r.correct>=0?" with continuity correction":""}`;let N=h?Math.min(r.correct,0):r.correct,b=l.every(w=>w===l[0]),E=(m-Vs(m,o,N>=0))/u;p=b?1:Nf(E,o,N>=1?ed(E,i,c,N):0)}let d={method:g,alternative:o,statistic:f,statisticName:"W",pValue:p,nullValue:s,nullValueName:"location shift",warnings:r.warnings.list};if(!r.confInt)return d;let v=a?h?ud(e,t,r):bf(()=>md(e,t,r)):hd(e,t,r);return{...d,...v}}function bf(e){try{return e()}catch(t){throw t instanceof RangeError&&t.message.startsWith("wilcox:")?new RangeError(`${t.message}: use exact: false`):t}}function qf(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(mi)}function md(e,t,n){let r=e.length,o=t.length;return Rf(qf(e,t,!1),s=>Wr(s,r,o,!0,!1),s=>yl(s,r,o,!0,!1),n,"difference in location")}function ud(e,t,n){let r=e.length,o=t.length,s=i=>{let{ranks:c,key:a}=kf(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 Af(qf(e,t,!0),s,i=>ii(i,r),i=>$s(ai(i,r),"interval"),(i,c)=>Mr(i,c-.25),or,{mean:r*o/2,sd:Math.sqrt(r*o*(r+o+1)/12)},r+o,n,"difference in location")}function hd(e,t,n){let{alternative:r,confLevel:o,tolRoot:s}=n,i=n.correct>=0,c=cn(e)-yt(t),a=yt(e)-cn(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[,g,d]=id(e.map(v=>v-h),t);return(g-Vs(g,r,p))/d},f=(h,p)=>{let g=l(c,p)-h,d=l(a,p)-h;if(Number.isNaN(g)||Number.isNaN(d))throw new Error("cannot compute the confidence interval because all observations are tied");return g<=0?c:d>=0?a:(Ef(s),Ht(v=>l(v,p)-h,c,a,{tol:s,fLower:g,fUpper:d}))},m=1-o,u;return r==="greater"?u=[f(-ie(m),i),1/0]:r==="less"?u=[-1/0,f(ie(m),i)]:u=[f(-ie(m/2),i),f(ie(m/2),i)],{confInt:u,confLevel:o,estimate:f(0,!1),estimateNames:"difference in location"}}var pd=["method","n"],If=["holm","hochberg","hommel","bonferroni","BH","BY","fdr","none"];function dd(e){if(If.includes(e))return e;let t=If.filter(n=>n.startsWith(e));if(e.length===0||t.length!==1)throw new RangeError(`invalid method "${e}"`);return t[0]}function Xr(e,t={}){B(t,pd,"pAdjust");let n=typeof t=="string"?{method:t}:t,r=dd(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=Lf(a,r,c),f=o.slice();return s.forEach((m,u)=>f[m]=l[u]),f}function zs(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 Lf(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=zs(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=zs(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++)dn(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=zs(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?Lf(e,"hochberg",n):gd(e,n)}}var _f=2**16;function gd(e,t){if(!Number.isInteger(t))throw new RangeError("n must be an integer for the hommel method");if(t>_f)throw new RangeError(`n must be at most ${_f} for the hommel method (O(n^2) work)`);let n=e.length,r=[...e,...new Array(t-n).fill(1)],o=zs(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 Cf=["p","alternative","confLevel"],wd=1+1e-7;function hi(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)+pn(e,t,n,!1)}function bd(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=pn(e,t,n,!1)*wd,s=a=>pn(a,t,n,!1)<=o;if(e<r){let a=fs(s,Math.ceil(r),t),l=a>t?0:hi(a,t,n);return Math.min(1,Dn(e,t,n,!0,!1)+l)}let i=ms(s,0,Math.floor(r)),c=i<0?0:Dn(i,t,n,!0,!1);return Math.min(1,hi(e,t,n)+c)}function vd(e,t,n={}){B(t,Cf,"binomTest"),B(n,Cf,"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),!Nn(e[0])||!Nn(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(!Nn(e))throw new RangeError("'x' must be nonnegative and integer");if(r=Math.round(e),!(Nn(t)&&Math.round(t)>=r&&t>0))throw new RangeError("'n' must be a positive integer >= 'x'");o=Math.round(t)}return pi(r,o,n)}function pi(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=Je(n.alternative),s=n.confLevel??.95;_t(s);let i;o==="less"?i=Dn(e,t,r,!0,!1):o==="greater"?i=hi(e,t,r):i=bd(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 Nd=["T","r","alternative","confLevel"],yd=1+1e-7;function Md(e,t){if(t===0)return e===0?1:0;if(e===t)return 1;let n=us(e,t,!1)*yd,r=i=>us(i,t,!1)<=n,o=Math.floor(t);if(e<t){let i=fs(r,Math.max(e+1,o),1/0);return Math.min(1,Kr(e,t,!0,!1)+di(i,t))}let s=ms(r,0,Math.min(e-1,o));return Math.min(1,Kr(s,t,!0,!1)+di(e,t))}var Vo=2**-1022;function di(e,t){return e-(e-1)===1?Kr(e-1,t,!1,!1):Kr(e,t,!1,!1)+us(e,t,!1)}function Er(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 Ed(e,t,n,r){let o=n+t*r,s=e*t*n/o;if(Number.isFinite(s)&&Number.isFinite(o)&&o>=Vo&&(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]=Er(e),[f,m]=Er(t),[u,h]=Er(n),[p,g]=Er(r),d=f*p,v=h-m-g,N=v>=0?u/(u+$(d,-v)):u/($(u,v)+d);return $(a*f*N,l+m+Math.min(v,0))}function xd(e,t,n,r){let o=e/n,s=t/r,i=o/s;return t===0||Number.isFinite(o)&&Number.isFinite(s)&&s>=Vo&&(o===0||o>=Vo)?i:e===0?0:e/t*(r/n)}function Rd(e,t={}){B(t,Nd,"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(Nn))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(v=>Number.isFinite(v)&&v>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=Je(t.alternative),l=t.confLevel??.95;if(o===2?_t(l):ln(l),o===2){let[v,N]=r,[b,E]=i,w=v+N;if(w===1/0)throw new RangeError("'x' must be finite, nonnegative, and integer");let y=c*b,x=y+E,M=Number.isFinite(x)&&x>=Vo&&(y===0||y>=Vo)?y/x:1/(1+E/b/c),R=pi(Math.round(v),Math.round(v)+Math.round(N),{p:M,alternative:a,confLevel:l}),T=E/b,A=k=>{if(k===0)return 0;let C=k/(1-k);if(Number.isFinite(T)||C===1/0)return C*T;let[_,I]=Er(C),[F,L]=Er(b),[D,S]=Er(E);return $(_*D/F,I+S-L)};return{method:"Comparison of Poisson rates",alternative:a,statistic:v,statisticName:"count1",parameter:Ed(w,c,b,E),parameterName:"expected count1",pValue:R.pValue,confInt:[A(R.confInt[0]),A(R.confInt[1])],confLevel:l,estimate:xd(v,N,b,E),estimateNames:"rate ratio",nullValue:c,nullValueName:"rate ratio",warnings:[]}}let f=r[0],m=i[0],u=c*m,h;a==="less"?h=Kr(f,u,!0,!1):a==="greater"?h=di(f,u):h=Md(f,u);let p=v=>f===0?0:ls(v,f,1,!0,!1),g=v=>v<1e-8?ls(v,f+1,1,!1,!1):ls(1-v,f+1,1,!0,!1),d;if(a==="less")d=[0,g(1-l)];else if(a==="greater")d=[p(1-l),1/0];else{let v=(1-l)/2;d=[p(v),g(v)]}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 Ff=["p","alternative","confLevel","correct"],Td="Chi-squared approximation may be incorrect";function gi(e,t,n={}){ct(t)&&B(t,Ff,"propTest"),B(n,Ff,"propTest");let r,o,s=n;if(typeof e!="number"&&e.length>0&&Array.isArray(e[0])){let A=e;if(A.some(k=>k.length!==2))throw new RangeError("'x' must have 2 columns");r=A.map(k=>k[0]),o=A.map(k=>k[0]+k[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 A=0;A<i;A++)!Number.isNaN(r[A])&&!Number.isNaN(o[A])&&c.push(A);let a=c.length;if(a<1)throw new RangeError("not enough data");if(r=c.map(A=>r[A]),o=c.map(A=>o[A]),o.some(A=>!(A>0)))throw new RangeError("elements of 'n' must be positive");if(o.some(A=>A===1/0))throw new RangeError("elements of 'n' must be finite");if(r.some(A=>!(A>=0)))throw new RangeError("elements of 'x' must be nonnegative");if(r.some((A,k)=>A>o[k]))throw new RangeError("elements of 'x' must not be greater than those of 'n'");let l;if(s.p!==void 0){let A=typeof s.p=="number"?[s.p]:Array.from(s.p);if(A.length!==i)throw new RangeError("'p' must have the same length as 'x' and 'n'");if(l=c.map(k=>A[k]),l.some(k=>!(k>0&&k<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=Je(s.alternative);(a>2||a===2&&f)&&(u="two.sided");let h=r.map((A,k)=>A/o[k]),p=a===2?h[0]-h[1]:0,g=a===2?1/o[0]+1/o[1]:0,d=(s.correct??!0)&&a<=2?.5:0,v=!1;if(d>0&&a===1){let A=Math.abs(r[0]-o[0]*l[0]);A<=d&&([d,v]=[A,!0])}else if(d>0&&a===2&&!f){let A=Math.abs(p)/g;A<=d&&([d,v]=[A,!0])}let N=Ad(r,o),b=o.some((A,k)=>{let C=l?.[k]??N;return A*C<5||A*(1-C)<5}),E=0;if(!v)for(let A=0;A<a;A++){let k=l?.[A]??N,C=o[A]*k,_=o[A]*(1-k);E+=Vt(Math.abs(r[A]-C)-d,C),E+=Vt(Math.abs(o[A]-r[A]-_)-d,_)}let w=l!==void 0?a:a-1,y;if(u==="two.sided")y=bt(E,w/2,2,!1,!1);else{let A=Math.sign(a===1?h[0]-l[0]:p);y=ae(A*Math.sqrt(E),{lowerTail:u==="less"})}let x;if(a===1||a===2&&!f){let A=(1-m)/2,k=u==="two.sided"&&A<1e-8?ie(A,{lowerTail:!1}):ie(u==="two.sided"?(1+m)/2:m);if(a===1){let C=o[0],_=k*k,I=(O,q)=>(O+_/(2*C)+q*k*Math.sqrt(O*(1-O)/C+_/(4*C*C)))/(1+_/C),F=h[0]+d/C,L=h[0]-d/C,D=Math.min(1,Math.max(0,F>=1?1:I(F,1))),S=Math.min(1,Math.max(0,L<=0?0:I(L,-1)));x=u==="two.sided"?[S,D]:u==="greater"?[S,1]:[0,D]}else{let C=Math.sqrt(h.reduce((L,D,S)=>L+D*(1-D)/o[S],0)),_=k*C+d*g,I=Math.max(-1,p-_),F=Math.min(1,p+_);x=u==="two.sided"?[I,F]:u==="greater"?[I,1]:[-1,F]}}let M=a===1?"p":c.map(A=>`prop ${A+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:w,parameterName:"df",pValue:y,estimate:a===1?h[0]:h,estimateNames:M,warnings:b?[Td]:[]};return l!==void 0&&(T.nullValue=a===1?l[0]:l,T.nullValueName=M),x!==void 0&&(T.confInt=x,T.confLevel=m),J("propTest",T)}function Ad(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+$(s,-r),0)/t.reduce((o,s)=>o+$(s,-r),0)}var Sd=["method","confLevel","sides"],kd=["wilson","wald","wilsoncc","agresti-coull","jeffreys","clopper-pearson","midp"],qd=["two.sided","left","right"];function $o(e,t){let n=ke(t,qd),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{...wi(s,o),sides:n}}function wi(e,t=1-e){let n=e/2<1e-8,r=n?ie(e/2,{lowerTail:!1}):ie(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 Qr(e,t){if(typeof e!="number")throw new TypeError(`'${t}' must be a number`);if(!Nn(e))throw new RangeError(`'${t}' must be a nonnegative integer`);return Math.round(e)+0}function Bn(e,t,n,r){let o=Qr(e,n),s=Qr(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 Ks(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 js(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 Of(e,t,n,r){let o=n/2,s=r?f=>{let m=Math.exp(f);return .5*pn(e,t,m,!1)+Dn(e-1,t,m,!0,!1)-o}:f=>{let m=Math.exp(f);return .5*pn(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:Ht(g=>i*s(g),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(Ht(f=>i*s(f),c,0,{tol:2**-60,fLower:a,fUpper:l}))}function bi(e,t,n={}){B(n,Sd,"binomCI");let r=ke(n.method,kd),o=$o(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]=Ks(s,i,a);break;case"wilsoncc":{let h=a*a,p=2*(i+h),g=2*s+h-1,d=a*Math.sqrt(h-2-1/i+4*l*(i*f+1));m=l===0?0:g-d<g/2?(2*s-1)/(2*i)*(2*s-1)/(g+d):(g-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,g=(s+h/2)/p,d=a*Math.sqrt(g*(1-g)/p);[m,u]=[g-d,g+d];break}case"jeffreys":m=s===0?0:js(c,s+.5,i-s+.5,!1),u=s===i?1:js(c,s+.5,i-s+.5,!0);break;case"clopper-pearson":m=s===0?0:js(c,s,i-s+1,!1),u=s===i?1:js(c,s+1,i-s,!0);break;case"midp":m=s===0?0:s===i?Math.exp(Math.log(c)/i):Of(s,i,c,!1),u=s===i?1:s===0?-Math.expm1(Math.log(c)/i):Of(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),J("binomCI",{estimate:l,confInt:[m,u],confLevel:n.confLevel??.95,method:r,sides:o.sides,warnings:[]})}function zo(e){return e===1/0?1:e/(1+e)}function jo(e,t){return e===0?0:t>0?e*e/t:1/0}function Id(e,t,n,r){return e/t+n/r>1}function _d(e,t,n,r){return Ws(e,t,n,r,0)}function yi(e,t,n,r,o){if(Id(e,t,n,r)){let[p,g,d,v]=yi(t-e,t,r-n,r,-o);return[g,p,v,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=v=>{let N=0;return i>0&&(N+=i/v),i<c&&(N-=(c-i)/(1-v)),a>0&&(N+=a/(v+f)),a<l&&(N-=(l-a)/(m-v)),N},g=v=>{let N=0;return i>0&&(N-=i/(v*v)),i<c&&(N-=(c-i)/((1-v)*(1-v))),a>0&&(N-=a/((v+f)*(v+f))),a<l&&(N-=(l-a)/((m-v)*(m-v))),N};if(p(0)<=0)u=0;else if(p(m)>=0)u=m;else{let v=0,N=m,b=Ld(e,t,n,r,o);u=s?b:b-o,u>v&&u<N||(u=N/2);for(let E=0;E<2e3;E++){let w=p(u);if(w===0)break;w>0?v=u:N=u;let y=u-w/g(u);if(y>v&&y<N||(y=v===0?N/2:N>2*v?Math.sqrt(v*N):v+(N-v)/2),!(y>v&&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 Ld(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 g=(Math.PI+Math.acos(Math.max(-1,Math.min(1,h/(p*p*p)))))/3;return 2*p*Math.cos(g)-u}function Mi(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]=yi(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 Cd(e,t,n,r,o,s,i){let c=De(De(ft([e,0],t),ft([n,0],r)),[o,0]),[a,l,f,m]=yi(e,t,n,r,o),u=pe(ft(jt(a,l),t),ft(jt(f,m),r));return s==="mn"&&(u=ft(xt(u,t+r),t+r-1)),De(Ce(c,c),xt(u,i))[0]}function Ws(e,t,n,r,o){return De(De(ft([e,0],t),ft([n,0],r)),[o,0])[0]}function Fd(e,t,n,r,o,s){return jo(Ws(e,t,n,r,o),Mi(e,t,n,r,o,s))}var Bs=new Float64Array(1),vi=new BigInt64Array(Bs.buffer);function Df(e){return Bs[0]=Math.abs(e),e<0?-vi[0]:vi[0]}function sr(e){return vi[0]=e<0n?-e:e,e<0n?-Bs[0]:Bs[0]}function Ni(e,t,n,r,o){let s=e(t);if(s===0||Number.isNaN(s))return t;let i=s<0===o,c=Df(i?r:n),a=Df(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 Vf(e,t,n,r,o,s){let i=_d(e,t,n,r);if(o===0)return[i,i];let c=zo(o),a=p=>(p===i?0:zo(Fd(e,t,n,r,p,s)))-c,l=(p,g,d,v)=>{if(d===0)return p;if(v===0)return g;let N=Ht(a,p,g,{tol:1e-300,fLower:d,fUpper:v});return s==="wald"?N:Ni(b=>Cd(e,t,n,r,b,s,o),N,p,g,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 Pf(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?Ht(e,r,i,{tol:2**-60,fLower:o,fUpper:c}):Ht(e,i,r,{tol:2**-60,fLower:c,fUpper:o});return Math.exp(a)}r=i,o=c,s*=2}}function Od(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 Dd(e,t,n,r,o){let[s,i]=Od(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=xt(x,o);return[M[0],De([1,0],M)[0],x[0],De([1,0],x)[0]]},p=e+n,g=c<2?1:-1,d=y=>{let[,x,M,R]=h(y),T=0,A=0;return p>0&&(T+=M>0?p/M:1/0,A-=p/(M*M)),e<t&&(T-=x>0?o*(t-e)/x:1/0,A-=o*(t-e)/x*(o/x)),n<r&&(T-=R>0?(r-n)/R:1/0,A-=(r-n)/(R*R)),[g*T,A*f]},v;if(!(m>u))v=u;else if(!(d(u)[0]>0))v=u;else if(!(d(m)[0]<0))v=m;else{let y=u,x=m;v=a,v>y&&v<x||(v=y+(x-y)/2);for(let M=0;M<2e3;M++){let[R,T]=d(v);if(R===0)break;R>0?y=v:x=v;let A=v-R/T;if(A>y&&A<x||(A=y<=0?x/2:x>2*y?Math.sqrt(y*x):y+(x-y)/2),!(A>y&&A<x))break;let k=Math.abs(A-v);if(v=A,k<=2**-53*v)break}}let[N,b,E,w]=h(v);return[Math.min(1,Math.max(0,N)),Math.min(1,Math.max(0,b)),Math.min(1,Math.max(0,E)),Math.min(1,Math.max(0,w))]}function Pd(e,t,n,r,o){let[s,i,c,a]=Dd(e,t,n,r,o),l=(f,m,u,h)=>u>.5?m*h-(m-f):f-m*u;return jo(l(e,t,s,i),t*s*i)+jo(l(n,r,c,a),r*c*a)}function $f(e,t,n,r,o){let s=e/t/(n/r);return jf(s,o,i=>Pd(e,t,n,r,i))}function Vd(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 $d(e,t,n,r,o){let[s,i]=Vd(e,t,n,r,o),c=e-t*s;return jo(c,t*s*(1-s))+jo(c,r*i*(1-i))}function zf(e,t,n,r,o){let s=e*(r-n)/((t-e)*n);return jf(s,o,i=>$d(e,t,n,r,i))}function jf(e,t,n){if(t===0)return[e,e];let r=zo(t),o=l=>n(l)-t,s=l=>l>0&&l<e?Ni(o,l,Number.MIN_VALUE,e,!1):l,i=l=>l>e&&l<1/0?Ni(o,l,e,Number.MAX_VALUE,!0):l;if(e===0||e===1/0){let l=u=>zo(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:zo(n(Math.exp(l))))-r;return[s(Pf(a,c,-1)),i(Pf(a,c,1))]}var zd=["method","confLevel","sides"],jd=["method","confLevel","sides"],Kd=["method","confLevel"],Bd=["ac","wald","score","mn","mee"];function Ei(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,g=i*Math.sqrt(u*(1-u)/(t+2)+h*(1-h)/(r+2));[f,m]=[Math.max(-1,p-g),Math.min(1,p+g)];break}case"score":{let[u,h]=Ks(e,t,i),[p,g]=Ks(n,r,i);f=l-i*Math.sqrt(u*(1-u)/t+g*(1-g)/r),m=l+i*Math.sqrt(h*(1-h)/t+p*(1-p)/r);break}case"mn":case"mee":[f,m]=Vf(e,t,n,r,s.chisq,o);break}return[Math.min(f,m),Math.max(f,m)]}function Wd(e,t,n,r,o={}){B(o,zd,"binomDiffCI");let s=ke(o.method,Bd),i=$o(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]=Ei(c,a,l,f,s,i);return i.sides==="left"?h=1:i.sides==="right"&&(u=-1),J("binomDiffCI",{estimate:m,confInt:[u,h],confLevel:o.confLevel??.95,method:s,sides:i.sides,warnings:[]})}var Ud=["katz.log","koopman"];function Us(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 Hd(e,t,n,r,o={}){B(o,jd,"binomRatioCI");let s=ke(o.method,Ud),i=$o(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,Us(.5,a,l,f,m)[1]]:l===0?[h,p]=[Us(c,a,.5,f,m)[0],1/0]:c===a&&l===f?[h,p]=Us(a-.5,a,f-.5,f,m):[h,p]=Us(c,a,l,f,m):[h,p]=$f(c,a,l,f,i.chisq),i.sides==="left"?p=1/0:i.sides==="right"&&(h=0),J("binomRatioCI",{estimate:u,confInt:[h,p],confLevel:o.confLevel??.95,method:s,sides:i.sides,warnings:[]})}var Yd=["midp","fisher","wald","score"],Gd={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"},Xd=2**23,Qd=2**20;function Kf(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=pe(r,[e[o],0]));return r[0]}function Zd(e,t={}){B(t,Kd,"oddsRatio");let n=ke(t.method,Yd);if(e.length!==2||e.some(A=>A.length!==2))throw new TypeError("'table' must be a 2 x 2 table");let r=t.confLevel??.95,{alpha:o,kappa:s,chisq:i}=$o(r,void 0),c=e.map((A,k)=>Array.from(A,(C,_)=>Qr(C,`table[${k}][${_}]`))),[[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),g=p-h+1;if(n==="midp"||n==="fisher"){if(g>Xd)throw new RangeError(`the exact distribution of the 2 x 2 table has ${g} 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,v,N;try{d=an(a-1,a+f,l+m,a+l,!0,!1)+.5*vn(a,a+f,l+m,a+l,!1),v=an(a,a+f,l+m,a+l,!1,!1)+.5*vn(a,a+f,l+m,a+l,!1),N=g<=Qd?ti(new Gr(a+f,l+m,a+l),a,1):pf(a,a+f,l+m,a+l)}catch(A){throw A instanceof RangeError?new RangeError(`the exact p-values of the table are beyond reach: ${A.message}`):A}let b=2*Math.min(d,v),E=[a+l,f+m],w=[a+f,l+m],y=0,x=!1;for(let A=0;A<2;A++)for(let k=0;k<2;k++){let C=E[k]*w[A]/u;C<5&&(x=!0);let _=Math.abs(c[k][A]-C);y+=_*_/C}let M=bt(y,.5,2,!1,!1),R,T;switch(n){case"wald":{R=a*m/(l*f);let A=s*Math.sqrt(1/a+1/l+1/f+1/m);T=[Math.exp(Math.log(R)-A),Math.exp(Math.log(R)+A)];break}case"fisher":{let A=Ze(()=>Do(c,{confLevel:r}));R=A.estimate,T=A.confInt;break}case"score":{R=a*m/(l*f),T=(a===0||m===0)&&(l===0||f===0)?[0,1/0]:zf(a,a+l,f,f+m,i);break}case"midp":{let A=new Gr(a+f,l+m,a+l),k=F=>{if(F===1)return[d,v];let L=A.density(F),D=a-h,S=.5*L[D];return[Kf(L,0,D-1)+S,Kf(L,D+1,L.length-1)+S]},C=(F,L)=>{let D=F(1);return D===0?1:rr(F,L?D>0:D<0)};R=a===h?0:a===p?1/0:C(F=>k(F)[0]-.5,!1);let _=a===h?0:C(F=>k(F)[1]-o/2,!0),I=a===p?1/0:C(F=>k(F)[0]-o/2,!1);T=[_,I];break}}return J("oddsRatio",{estimate:R,confInt:T,confLevel:r,method:Gd[n],pValue:{midp:b,fisher:N,chiSquare:M},warnings:x?["Chi-squared approximation may be incorrect"]:[]})}var Jd=["epsilon","paired","varEqual","confLevel","alpha"],e0=["margin","method","alpha"];function Bf(e,t){if(!(typeof e=="number"&&e>0&&e<.5))throw new RangeError(`'${t}' must be a single number in (0, 0.5)`)}function t0(e,t,n={}){B(n,Jd,"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)Bf(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=Ze(()=>yn(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=gt((r-Math.abs(m))/l,f,!1,!1),h=s<1e-8?wt(s,f,!1,!1):wt(o,f,!0,!1),p=[m-h*l,m+h*l];return J("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 n0=["mn","mee","wald"];function r0(e,t,n,r,o={}){B(o,e0,"binomDiffEquivalenceTest");let s=ke(o.method,n0),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;Bf(c,"alpha");let[a,l]=Bn(e,t,"x1","n1"),[f,m]=Bn(n,r,"x2","n2"),u=a/l-f/m,h=E=>Ws(a,l,f,m,E)/Math.sqrt(Mi(a,l,f,m,E,s)),p=h(-i),g=h(i),d=[ae(p,{lowerTail:!1}),ae(g)],v=Math.max(d[0],d[1]),N=1-2*c,b=Ei(a,l,f,m,s,wi(2*c,N));return J("binomDiffEquivalenceTest",{method:s,estimate:u,margin:i,alpha:c,statistic:[p,g],statisticName:"z",pValues:d,pValue:v,result:d[0]<c&&d[1]<c?"rejected":"not rejected",confInt:b,confLevel:N,warnings:[]})}var Yt=new WeakMap;function Ys(e){let t=(n,r)=>n.weights===void 0?Array.from(r):r.filter((o,s)=>n.weights[s]!==0);return Ie(e.flatMap(n=>[t(n,n.fittedValues),t(n,n.residuals)])).k}function Gs(e,t){return e.map(n=>{if(n.rss>=o0&&n.rss<1/0)return $(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=$(o,-t);r+=i*c*c}),r})}var o0=2**-1022;function Ri(e,t){return e.y.flatMap((n,r)=>(e.weights?.[r]??1)===0?[]:[[e.weights?.[r]??1,$(n,-t)]])}function Xs(e){let t=Math.floor(Math.log2(e.reduce((n,[r])=>Math.max(n,r),0)));return t-(t%2+2)%2}function xr(e,t,n=0){let r=i=>e.reduce((c,[a,l])=>c+$(a,-i)*l*l,0),o=n,s=r(o);return Number.isFinite(s)||(o=Xs(e),s=r(o)),!($(t,-o)>=1e-24*s)}function Wf(e){let t=Ys([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+=$(e.effects[s],-t)**2}return{terms:r,rdf:e.dfResidual,rss:Gs([e],t)[0],k:t}}var Uf=(e,t,n)=>ht(e,t,n,!1,!1),Ti="essentially perfect fit for \u20181\u2019: summary may be unreliable";function Ai(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 Si=new WeakMap;function s0(e){let t=Wf(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*$(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 xi(e){let t=s0(e),{names:n,rdf:r,k:o,perfect:s}=t,{rows:i,rss:c}=t;if(r>0){let g=Ri(e,o);xr(g,c)&&(c=0,i=i.map(d=>xr(g,d.ss)?{...d,ss:0}:d))}let a=c/r,l=g=>$(g,2*o),f=i.map(({df:g,ss:d})=>{let v=d/g/a;return[g,l(d),l(d/g),v,Uf(v,g,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:g,ss:d})=>[g,d]);return p.push([r,c]),Yt.set(h,{colNames:["Df","Sum Sq"],rowNames:u,table:p}),h}function a0(e){let t=e.map(f=>f.dfResidual),n=Ys(e),r=Gs(e,n),o=f=>$(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*$(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,g]=u>0?[e[m-1],e[m]]:[e[m],e[m-1]],d=N=>N.filter((b,E)=>(p.weights?.[E]??1)!==0),v=u===0||!Ss(d(p.x),d(g.x))?NaN:h/u/i;return v<0&&(v=NaN),[t[m],o(r[m]),u,o(h),v,Number.isNaN(v)?NaN:Uf(v,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 Hs(...e){let t=[];for(let n of e)for(let r of n??[])t.includes(r)||t.push(r);return t}function ki(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=xi(e),a={...c,warnings:Hs(e.warnings,c.warnings)},l=Yt.get(c);return l!==void 0&&Yt.set(a,l),J("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?xi(e):a0(o);return i.warnings=Hs(...n.map(c=>c.warnings),s,i.warnings),J("anova",i,...n)}function qi(e){if(typeof e=="object"&&e!==null&&e.multistratum===!0){let i=e,c=Hs(i.warnings,Si.get(i)),a={strata:i.strata.filter(f=>f.stratum!=="Error: (Intercept)").map(f=>({...f})),warnings:c},l=Yt.get(i);return Array.isArray(l)&&Yt.set(a,l.filter((f,m)=>i.strata[m].stratum!=="Error: (Intercept)")),J("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=xi(t),r=Wf(t),o=Ai(t.fittedValues.map(i=>$(i,-r.k)),r.rss,r.rdf),s=Hs(t.warnings,n.warnings,o?[Ti]:[]);if(t.dfResidual>0){let i={...n,warnings:s},c=Yt.get(n);return c!==void 0&&Yt.set(i,c),J("aovSummary",i,e)}return J("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 i0=["pooledSd","mu","paired","ci","alternative","adjust"],c0=["pooledSd","mu","paired","ci","alternative"],l0=["generalized","partial","ci","alternative"],f0=["partial","ci","alternative"],m0=["partial","ci","alternative"];function Li(e){if(e!==null&&!(e>0&&e<1))throw new RangeError("ci must be a single numeric value between (0, 1)")}var u0="Missing values detected. NAs dropped.",h0=`'y' is numeric but has only 2 unique values.
1
+ /*! rStats.js 1.14.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 nn,b as zn,c as nr,d as Nr,e as ks,f as Pa}from"./chunks/chunk-YLUF4WTL.js";import{a as vn,c as an,h as Br,i as vl,m as Nl,n as Wr,o as yl,s as bs,t as Ml,u as xo,v as vs,w as Ns,x as ys}from"./chunks/chunk-BCXYAILN.js";import{b as Sl}from"./chunks/chunk-MXOBCQQP.js";import{a as Wa,e as Ua}from"./chunks/chunk-CKUBQW2A.js";import{a as Es,b as tr,c as Ao,d as _l,e as Ll,f as xs,g as Je,h as ln,i as Nn,j as _t,k as Dl,l as fp,n as Is,o as Io}from"./chunks/chunk-MCEZRRQO.js";import{a as _s}from"./chunks/chunk-D25AVVVI.js";import"./chunks/chunk-XXH5OIUO.js";import"./chunks/chunk-ZWSHIUBZ.js";import{a as wl,d as ws}from"./chunks/chunk-4O6XLQBN.js";import{b as Ut,c as Hl}from"./chunks/chunk-3746VXPM.js";import{d as rt}from"./chunks/chunk-SEKOIW43.js";import{a as qs}from"./chunks/chunk-3DA7CMFN.js";import{a as So,b as Va,c as $a,d as Bl,e as ko,f as Yr,g as Kn,h as ja,i as qo,j as Wl,k as Ka,l as mp,m as Ba,n as Ul,o as up,p as hp,q as pp}from"./chunks/chunk-AONDWNMF.js";import{b as Rt,c as On}from"./chunks/chunk-NA7EDZ4Q.js";import{a as Cl,c as Fl,d as Ol,e as vr,g as _o}from"./chunks/chunk-7FGVMDJE.js";import{b as Oa}from"./chunks/chunk-H5GBKP57.js";import{a as ct,b as Rs,c as Ts}from"./chunks/chunk-QLKZ2OFQ.js";import{a as jn,b as Hr}from"./chunks/chunk-AQHG7BEZ.js";import{a as zl,b as jl,c as Ss,d as za}from"./chunks/chunk-KITNZUYC.js";import{d as ht,e as hn,g as Tn,h as Gn,j as Xn}from"./chunks/chunk-A5DZG7FH.js";import{d as fs,e as ms,g as us,h as Kr,m as pn,n as Dn}from"./chunks/chunk-F6KKW5YN.js";import{a as Kl}from"./chunks/chunk-EWNJT27V.js";import{c as Pl,h as Vl,j as Lt,m as Da,n as $l,o as As,p as Ur}from"./chunks/chunk-I2OTBVRK.js";import{a as er,b as ql,g as Ms}from"./chunks/chunk-2426YL2X.js";import"./chunks/chunk-Y7VD43TO.js";import{a as Fa,b as gt,c as wt,d as bl,e as Bt,f as Wt}from"./chunks/chunk-MJHFGGPU.js";import{i as bt,l as ls,t as pl}from"./chunks/chunk-EDRBKZ3C.js";import{a as An,c as Il}from"./chunks/chunk-DZQWGBTW.js";import{b as El,c as xl,d as Rl,e as Tl,f as Al,i as Ro,l as lp,n as J,o as Ze,p as At}from"./chunks/chunk-6VY3XNAF.js";import{a as To,b as $n,c as kl,d as Pt,e as Nt}from"./chunks/chunk-PHDTVTKV.js";import{a as yt,b as gn,c as cn}from"./chunks/chunk-3XUHCWLU.js";import"./chunks/chunk-62Y36UKH.js";import"./chunks/chunk-VWCCPBBG.js";import{b as ml,c as ul}from"./chunks/chunk-JCGCJT7T.js";import{h as cl,i as ll,k as fl,t as hl}from"./chunks/chunk-6HUTVWPO.js";import{f as Ia,r as Zt,s as ae,t as ie}from"./chunks/chunk-ZJJJDLRL.js";import{A as Vt,B as gs,C as Ie,D as Ca,c as _a,d as Eo,e as Pn,f as dn,g as Vn,h as $,i as Ve,j as Qn,k as it,l as at,m as Zn,n as dl,p as hs,q as Tt,r as ps,s as gl,t as ds,u as La,v as Pe,w as vt,x as bn,y as Dt,z as Jn}from"./chunks/chunk-B6NPCYFO.js";import{b as Ot,c as jt,d as pe,e as al,f as De,g as Ce,h as xt,i as ot,j as ft,k as il,l as B,m as Kt}from"./chunks/chunk-TGXUU5NB.js";var Yl=["alternative","mu","paired","varEqual","confLevel"];function yn(e,t,n={}){if(typeof e!="string"&&ct(t)&&B(t,Yl,"tTest"),B(n,Yl,"tTest"),typeof e=="string")return dp(e,t,n);let r;t!==void 0&&!ArrayBuffer.isView(t)&&!Array.isArray(t)?n=t:r=t;let o=Je(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(ln(a),i&&r===void 0)throw new TypeError("y is required when paired is true");let l,f,m;if(i){let[k,C]=Jn(Array.from(e),Array.from(r));({d:l,k:m}=ps(k,C))}else{let k=Ie(r===void 0?[Dt(Array.from(e))]:[Dt(Array.from(e)),Dt(Array.from(r))]);m=k.k,l=k.groups[0],f=k.groups[1]}let u=$(s,-m),h,p,g,d,v,N,b,E=l.length,w=f!==void 0&&c;if(E<1||!w&&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,g=Math.sqrt(x/E),g===0||g<10*Number.EPSILON*Math.abs(y))throw new Error("data are essentially constant");h=(y-u)/g,N=i?"Paired t-test":"One Sample t-test",d=y,v=i?"mean difference":"mean of x",b=i?"mean difference":"mean"}else{let k=f.length;if(k<1||!c&&k<2)throw new Error("not enough 'y' observations");if(c&&E+k<3)throw new Error("not enough observations");let C=it(f),_=k>1?at(f):NaN;if(N=`${c?"":"Welch"} Two Sample t-test`,d=[y,C],v=["mean of x","mean of y"],b="difference in means",c){p=E+k-2;let I=0;E>1&&(I+=(E-1)*x),k>1&&(I+=(k-1)*_),I/=p,g=Math.sqrt(I*(1/E+1/k))}else{let I=x/E,F=_/k;g=Math.sqrt(Math.sqrt(I)**2+Math.sqrt(F)**2),p=ds([I,F],[E-1,k-1])}if(g===0||g<10*Number.EPSILON*Math.max(Math.abs(y),Math.abs(C)))throw new Error("data are essentially constant");h=(y-C-u)/g}let M=typeof d=="number"?d:d[0]-d[1],R=(k,C)=>{let _=u+(h+C*k)*g;return Number.isFinite(_)&&Math.max(Math.abs(u),Math.abs(h*g))<=16*(Math.abs(M)+k*g)?_:M+C*k*g},T,A;if(o==="less")T=gt(h,p,!0,!1),A=[-1/0,R(wt(a,p,!0,!1),1)];else if(o==="greater")T=gt(h,p,!1,!1),A=[R(wt(a,p,!0,!1),-1),1/0];else{T=2*gt(-Math.abs(h),p,!0,!1);let k=1-a,C=k/2<1e-8?wt(k/2,p,!1,!1):wt(1-k/2,p,!0,!1);A=[R(C,-1),R(C,1)]}return A=[$(A[0],m),$(A[1],m)],d=typeof d=="number"?$(d,m):d.map(k=>$(k,m)),g=$(g,m),{method:N,alternative:o,statistic:h,statisticName:"t",parameter:p,parameterName:"df",pValue:T,confInt:A,confLevel:a,estimate:d,estimateNames:v,nullValue:s,nullValueName:b,stdErr:g,stdError:g,warnings:[]}}function dp(e,t,n){if(n.paired!==void 0)throw new Error("cannot use 'paired' in formula method");let r=Es(e,t,!0);if(r.kind==="one")return yn(r.x,n);if(r.kind==="paired")return yn(r.x,r.y,{...n,paired:!0});let[o,s]=r.levels;return{...yn(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 gp=new Set(["n","delta","sd","sigLevel","power","type","alternative","strict","tol"]),wp=["n","p1","p2","sigLevel","power","alternative","strict","tol"],bp=["groups","n","betweenVar","withinVar","sigLevel","power"],Xl=Number.EPSILON**.25;function ke(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 Re=e=>e==null;function fn(e,t,n){if(!Re(e)&&!t(e))throw new RangeError(n)}var Lo=e=>e>=0&&e<=1,Ls=e=>e>0&&e<1/0;function rn(e,t){if(e.filter(Re).length!==1)throw new RangeError(t)}function Ye(e,t,n,r={}){return Ol(e,t,n,r).root}var yr=(e,t,n,r)=>Bt(e,t,{ncp:n,lowerTail:r});function Ha(e,t){if(e!=null&&!(typeof e=="number"&&e>=0&&e<=1))throw new RangeError(t)}var Ya="'sig.level' must be numeric in [0, 1]";function vp(e={}){B(e,gp,"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;rn([t,n,o,r,s],"exactly one of 'n', 'delta', 'sd', 'power', and 'sig.level' must be NULL"),Ha(s,Ya),fn(r,Lo,"'power' must be a probability"),fn(o,Ls,"'sd' must be a positive finite number"),fn(t,g=>g>=2,"number of observations in each group must be at least 2"),fn(n,g=>!Number.isNaN(g),"'delta' must not be NaN"),fn(e.tol,Ls,"'tol' must be a positive finite number");let i=ke(e.type,["two.sample","one.sample","paired"]),c=ke(e.alternative,["two.sided","one.sided"]),a=e.strict??!1,l=e.tol??Xl,f=i==="two.sample"?2:1,m=c==="two.sided"?2:1;m===2&&!Re(n)&&(n=Math.abs(n));let u=(g,d,v,N)=>{let b=Math.max(1e-7,g-1)*f,E=Wt(N/m,b,{lowerTail:!1}),w=Math.sqrt(g/f)*d/v;Number.isFinite(w)||(w=Math.sqrt(g/f)*(d/v));let y=yr(E,b,w,!1);return a&&m===2?y+yr(-E,b,w,!0):y};if(Re(r))r=u(t,n,o,s);else{let g=r;if(Re(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)-g,2,1e7,{tol:l,extendInt:"upX"})}else if(Re(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(v=>u(t,n,v,s)-g,d*1e-7,d*1e7,{tol:l,extendInt:n>0?"downX":"upX"})}else Re(n)?n=Ye(d=>u(t,d,o,s)-g,o*1e-7,o*1e7,{tol:l,extendInt:"upX"}):s=Ye(d=>u(t,n,o,d)-g,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 Np(e={}){B(e,wp,"powerPropTest");let{n:t,p1:n,p2:r,power:o}=e,s=e.sigLevel===void 0?.05:e.sigLevel;rn([t,n,r,o,s],"exactly one of 'n', 'p1', 'p2', 'power', and 'sig.level' must be NULL"),Ha(s,Ya),fn(o,Lo,"'power' must be a probability"),fn(n,Lo,"'p1' must be a probability"),fn(r,Lo,"'p2' must be a probability"),fn(t,u=>u>0,"number of observations in each group must be positive"),fn(e.tol,Ls,"'tol' must be a positive finite number");let i=ke(e.alternative,["two.sided","one.sided"]),c=e.strict??!1,a=e.tol??Xl,l=i==="two.sided"?2:1,f=(u,h,p,g)=>{let d=ie(g/l,{lowerTail:!1});if(c&&l===2){let v=(h+p)/2,N=h*(1-h),b=p*(1-p),E=v*(1-v),w=Math.abs(h-p),y=Math.sqrt(N+b);return ae((Math.sqrt(u)*w-d*Math.sqrt(2*E))/y)+ae((Math.sqrt(u)*w+d*Math.sqrt(2*E))/y,{lowerTail:!1})}return ae((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(Re(o))o=f(t,n,r,s);else{let u=o;if(Re(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(Re(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(Re(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 J("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 Gl=1e16,Cs=(e,t)=>e>Gl&&e<1/0&&t>Gl;function Fs(e,t,n,r){let o=ie(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 ae((o*i-s)/c,{lowerTail:!1})}function yp(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 $($(e,-s)*$(t,-i)*$(n,-c)/$(r,-a),s+i+c-a)}function Mp(e={}){B(e,bp,"powerAnovaTest");let{groups:t,n,betweenVar:r,withinVar:o,power:s}=e,i=e.sigLevel===void 0?.05:e.sigLevel;if(rn([t,n,r,o,s,i],"exactly one of 'groups', 'n', 'between.var', 'within.var', 'power', and 'sig.level' must be NULL"),!Re(t)&&!(t>=2))throw new RangeError("number of groups must be at least 2");if(!Re(n)&&!(n>=2))throw new RangeError("number of observations in each group must be at least 2");Ha(i,Ya),fn(s,Lo,"'power' must be a probability"),fn(r,a=>a>=0&&a<1/0,"'betweenVar' must be a nonnegative finite number"),fn(o,Ls,"'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=yp(a-1,l,f,m));let p=a-1,g=(l-1)*a;return h===1/0?1:Cs(p,g)?Fs(p,g,h,u):Tn(Gn(u,p,g,{lowerTail:!1}),p,g,{ncp:h,lowerTail:!1})};if(Re(s))s=c(t,n,r,o,i);else{let a=s;if(Re(t))t=Ye(l=>c(l,n,r,o,i)-a,2,100);else if(Re(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(Re(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 Re(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 Ep=["correct","p","rescaleP"],xp=["confLevel"],Rp=["alternative","rowScores","colScores"];function Jt(e){let t=[0,0];for(let n of e)t=pe(t,[n,0]);return t}function Xa(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 Qa=e=>e.length>0&&typeof e[0]!="number",Za=(e,t)=>Number.isNaN(e)?NaN:bt(e,t/2,2,!1,!1);function Ql(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 Co(e,t){return e.length>0&&Array.isArray(e[0])?e.map((n,r)=>n.map((o,s)=>Ql(o,t[r][s]))):e.map((n,r)=>Ql(n,t[r]))}var Tp=["none","williams","yates"],Jl="Log likelihood ratio (G-test)";function Ap(e,t={}){B(t,Ep,"gTest");let n=ke(t.correct,Tp);if(typeof e!="object"||e===null||typeof e.length!="number")throw new TypeError("'x' must be an array of counts or of rows");if(Qa(e)){let o=Xa(e);return Zl(o.flat()),o.length===1?J("gTest",Ga(o[0],n,t)):o[0].length===1?J("gTest",Ga(o.map(s=>s[0]),n,t)):J("gTest",Sp(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 Zl(r),J("gTest",Ga(r,n,t))}function Zl(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 ef(e,t){return e<1/0?0:Math.ceil(Math.log2(t))+1}function tf(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=pe(n,ll(o,s))}return n[0]+n[1]}function Sp(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=Jt(e.flat()),s=ef(o[0],n*r),i=s===0?e:e.map(b=>b.map(E=>$(E,-s))),c=s===0?o:Jt(i.flat()),a=i.map(b=>Jt(b)),l=i[0].map((b,E)=>Jt(i.map(w=>w[E]))),f=a.map(b=>l.map(E=>Ce(b,ot(E,c)))),m=i;if(t==="yates"){let[[b,E],[w,y]]=i,x=b*y-E*w>0?-.5:.5,M=$(.5,-s),R=x<0?-M:M;m=[[b+R,E-R],[w-R,y+R]]}let u=tf(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 b=c[0]+c[1],E=a.reduce((y,x)=>y+1/(x[0]+x[1]),0),w=l.reduce((y,x)=>y+1/(x[0]+x[1]),0);p=1+$((b*E-1)*(b*w-1)/(6*b*(n-1)*(r-1)),-s)}let g=$(2*u,s)/p,d=(n-1)*(r-1),v=f.map(b=>b.map(E=>$(E[0]+E[1],s)));return{method:`${Jl} test of independence ${t==="none"?"without correction":t==="williams"?"with Williams' correction":"with Yates' correction"}`,statistic:g,statisticName:"G",parameter:d,parameterName:"X-squared df",pValue:Za(g,d),observed:s===0?m:m.map(b=>b.map(E=>$(E,s))),expected:v,ftResiduals:Co(e,v),warnings:h}}function Ga(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(b=>typeof b!="number"||Number.isNaN(b)))throw new RangeError("'p' must not contain missing values");if(s.some(b=>b<0))throw new RangeError("probabilities must be non-negative");if(s.some(b=>b===1/0))throw new RangeError("probabilities must be finite");let i=Jt(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=Jt(e),a=ef(c[0],r),l=a===0?e:e.map(b=>$(b,-a)),f=a===0?c:Jt(l),m=s.map(b=>o?ot(xt(f,b),i):xt(f,b)),u=m.map(b=>b[0]+b[1]),h=l;if(t==="yates"){let b=$(.5,-a),E=l[0]-u[0];E>$(.25,-a)?h=[l[0]-b,l[1]+b]:-E>$(.25,-a)&&(h=[l[0]+b,l[1]-b])}let p=tf(h,m);if(!o&&!Number.isNaN(p)){let b=f[0]+f[1];p-=b*(i[0]-1+i[1])}let g=Number.isNaN(p)?["NaNs produced"]:[],d=t==="williams"?1+(r+1)/(6*$(f[0]+f[1],a)):1,v=$(2*p,a)/d,N=u.map(b=>$(b,a));return{method:`${Jl} goodness of fit test`,statistic:v,statisticName:"G",parameter:r-1,parameterName:"X-squared df",pValue:Za(v,r-1),observed:a===0?h:h.map(b=>$(b,a)),expected:N,ftResiduals:Co(e,N),warnings:g}}function kp(e){if(typeof e!="object"||e===null||!Qa(e))throw new RangeError("'x' must be square with at least two rows and columns");let t=Xa(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 qp(e){let t=kp(e),n=Jt(t.flat())[0],r=n>2**400||n>0&&n<2**-400?Math.round(Math.log2(n)):0,o=r===0?t:t.map(w=>w.map(y=>$(y,-r))),s=o.length,i=o.map(w=>Jt(w)),c=o.map((w,y)=>Jt(o.map(x=>x[y]))),a=o.map((w,y)=>o[y].some((x,M)=>M!==y&&x>0)||o.some((x,M)=>M!==y&&x[y]>0)).flatMap((w,y)=>w?[y]:[]),l=a.length;if(l<2)throw new RangeError("Too many equal marginals, cannot compute");let f=a.map((w,y)=>y),m=w=>f[w]===w?w:f[w]=m(f[w]);for(let w=0;w<l;w++)for(let y=w+1;y<l;y++)o[a[w]][a[y]]+o[a[y]][a[w]]>0&&(f[m(w)]=m(y));if(a.some((w,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(w=>{let y=pe(i[w],[-c[w][0],-c[w][1]]);return y[0]+y[1]}),p=a.slice(0,u).map(w=>a.slice(0,u).map(y=>{if(w!==y)return-(o[w][y]+o[y][w]);let x=0;for(let M=0;M<s;M++)M!==w&&(x+=o[w][M]+o[M][w]);return x})),g=p.map(w=>w.map(()=>0)),d=[];for(let w=0;w<u;w++){let y=p[w][w];for(let x=0;x<w;x++)y-=g[w][x]*g[w][x]*d[x];d.push(y);for(let x=w+1;x<u;x++){let M=p[x][w];for(let R=0;R<w;R++)M-=g[x][R]*g[w][R]*d[R];g[x][w]=M/y}}let v=0,N=[];for(let w=0;w<u;w++){let y=h[w];for(let x=0;x<w;x++)y-=g[w][x]*N[x];N.push(y),v+=y*y/d[w]}let b=Jt(a.flatMap(w=>a.map(y=>t[w][y]))),E=l-1;return v=$(v,r),J("stuartMaxwellTest",{method:"Stuart-Maxwell test",statistic:v,statisticName:"chi-squared",parameter:E,parameterName:"df",pValue:Za(v,E),N:b[0]+b[1],warnings:[]})}function Ip(e,t={}){B(t,xp,"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=nf(e),o=Jt(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(w=>w.map(y=>$(y,-s))),{pc:c,pd:a}=_p(i),l=[0,0],f=[0,0];i.forEach((w,y)=>w.forEach((x,M)=>{l=pe(l,xt(c[y][M],x)),f=pe(f,xt(a[y][M],x))})),l=[l[0]/2,l[1]/2],f=[f[0]/2,f[1]/2];let m=pe(l,f),u=ot(De(l,f),m)[0],h=Ce(m,m),p=i.map((w,y)=>w.map((x,M)=>2*ot(De(Ce(f,c[y][M]),Ce(l,a[y][M])),h)[0])),g=Math.max(...p.flat().map(Math.abs)),d=g>0&&g<1/0?Math.floor(Math.log2(g)):0,v=0;i.forEach((w,y)=>w.forEach((x,M)=>{let R=$(p[y][M],-d);v+=x*R*R}));let N=$(Math.sqrt(v),d-s/2),b=(1-n)/2,E=b<1e-8?ie(b,{lowerTail:!1}):ie(1-b);return J("goodmanKruskalGamma",{estimate:u,confInt:[Math.max(-1,u-E*N),Math.min(1,u+E*N)],confLevel:n,ase:N,warnings:[]})}function nf(e){if(typeof e!="object"||e===null||!Qa(e))throw new TypeError("'table' must be an array of rows");let t=Xa(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 _p(e){let t=e.length,n=e[0].length,r=[0,0],o=e.map(p=>{let g=[],d=r;for(let v=0;v<n;v++)g.push(d),d=pe(d,[p[v],0]);return g}),s=e.map(p=>{let g=new Array(n),d=r;for(let v=n-1;v>=0;v--)g[v]=d,d=pe(d,[p[v],0]);return g}),i=p=>{let g=[],d=new Array(n).fill(r);for(let v=0;v<t;v++)g.push(d),d=d.map((N,b)=>pe(N,p[v][b]));return g},c=p=>{let g=new Array(t),d=new Array(n).fill(r);for(let v=t-1;v>=0;v--)g[v]=d,d=d.map((N,b)=>pe(N,p[v][b]));return g},a=i(o),l=c(s),f=i(s),m=c(o),u=a.map((p,g)=>p.map((d,v)=>pe(d,l[g][v]))),h=f.map((p,g)=>p.map((d,v)=>pe(d,m[g][v])));return{pc:u,pd:h}}function Lp(e,t={}){B(t,Rp,"lblTest");let n=Je(t.alternative),r=nf(e),o=r.length,s=r[0].length,i=(R,T,A)=>{if(R===void 0)return Array.from({length:T},(C,_)=>_+1);let k=Array.from(R);if(k.length!==T)throw new RangeError(`'${A}' must have one score for each ${A==="rowScores"?"row":"column"}`);if(k.some(C=>typeof C!="number"||!Number.isFinite(C)))throw new RangeError(`'${A}' must be finite numbers`);return k},c=i(t.rowScores,o,"rowScores"),a=i(t.colScores,s,"colScores"),f=Jt(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=>$(T,-f))),u=m.map(R=>Jt(R)).map(R=>R[0]+R[1]),h=m[0].map((R,T)=>Jt(m.map(A=>A[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 g=u.reduce((R,T,A)=>R+T*c[A],0)/p,d=h.reduce((R,T,A)=>R+T*a[A],0)/p,v=c.map(R=>R-g),N=a.map(R=>R-d),b=u.reduce((R,T,A)=>R+T*v[A]*v[A],0),E=h.reduce((R,T,A)=>R+T*N[A]*N[A],0),w=0;if(m.forEach((R,T)=>R.forEach((A,k)=>w+=A*v[T]*N[k])),!(b>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=w/Math.sqrt(b)/Math.sqrt(E),x=f===0?Math.sqrt(p-1)*y:$(Math.sqrt(p-$(1,-f))*y,f/2),M=n==="less"?ae(x):n==="greater"?ae(x,{lowerTail:!1}):2*ae(-Math.abs(x));return J("lblTest",{method:"Linear-by-Linear Association Test",alternative:n,statistic:x,statisticName:"Z",pValue:M,warnings:[]})}var rf=["correct","p","rescaleP","simulatePValue","B","nMinus1"];function wn(e){let t=[0,0],n=0;for(let r of e)t=pe(t,[r,0]),n+=r;return Number.isFinite(n)?t[0]:n}function Cp(e,t,n){return ct(t)&&B(t,rf,"chisqTest"),B(n,rf,"chisqTest"),Array.isArray(t)?J("chisqTest",Fp(e,t,n??{})):J("chisqTest",Op(e,t??{}))}var af="Chi-squared approximation may be incorrect";function Fp(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(An),c=[...new Set(s)].sort(An);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])]++),{...uf(f,n),rowLevels:i,colLevels:c}}function Op(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?Ja(r[0],t):o===1?Ja(r.map(s=>s[0]),t):uf(r,t)}return Ja(Array.from(e),t)}function cf(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 lf(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 ff(e,t){return Number.isNaN(e)?NaN:bt(e,t/2,2,!1,!1)}function mf(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 Ja(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(g=>!(g>=0&&g<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=wn(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(g=>g<0))throw new RangeError("probabilities must be non-negative");if(o.some(g=>g===1/0))throw new RangeError("probabilities must be finite");if(t.rescaleP??!1){let g=wn(o),d=g<1/0?0:Math.ceil(Math.log2(n)),v=d===0?o:o.map(b=>$(b,-d)),N=d===0?g:wn(v);o=v.map(b=>b/N)}else if(Math.abs(wn(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(g=>$(g,-s)),c=s===0?r:wn(i),a=o.map(g=>c*g),l=s===0?a:a.map(g=>$(g,s));mf(l);let f=o.map((g,d)=>a[d]*(1-g)),m=$(wn(i.map((g,d)=>Vt(g-a[d],a[d]))),s),u="Chi-squared test for given probabilities",h=n-1,p;if(t.simulatePValue??!1){let g=cf(t);if(!Number.isInteger(r))throw new RangeError("the simulation needs counts summing to an integer");u+=To(g),h=NaN,p=lf(Dp(r,o,l,g),m,g)}else p=ff(m,h);return{method:u,statistic:m,statisticName:"X-squared",parameter:h,parameterName:"df",pValue:p,observed:e,expected:l,residuals:i.map((g,d)=>$((g-a[d])/Math.sqrt(a[d]),s/2)),stdres:i.map((g,d)=>$((g-a[d])/Math.sqrt(f[d]),s/2)),ftResiduals:Co(e,l),warnings:Number.isFinite(h)&&l.some(g=>g<5)?[af]:[]}}function Dp(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=Sl(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 Pp(e,t,n,r){let o=e.length,s=t.length,i=e.reduce((f,m)=>f+m,0);if(i>vs)throw new RangeError("the simulation needs a total count of at most 16777216 (2^24)");let c=Ns(i),a=new Int32Array(o*s),l=new Float64Array(Math.trunc(r));for(let f=0;f<l.length;f++){ys(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 uf(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(b=>!(b>=0&&b<1/0)))throw new RangeError("all entries of 'x' must be non-negative and finite");let c=wn(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(b=>wn(b)),l=e[0].map((b,E)=>wn(e.map(w=>w[E]))),f=a.map(b=>l.map(E=>gs(b,E,c)));mf(f.flat());let m=e.map((b,E)=>b.map((w,y)=>w-f[E][y])),u="Pearson's Chi-squared test",h,p=(o-1)*(s-1),g,d=(t.simulatePValue??!1)&&a.every(b=>b>0)&&l.every(b=>b>0),v=!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(w=>w.some(y=>!Number.isInteger(y))))throw new RangeError("the simulation needs integer counts");let b=cf(t);u+=To(b),h=wn(m.flatMap((w,y)=>w.map((x,M)=>Vt(x,f[y][M])))),p=NaN;let E=Pp(a.map(w=>Math.trunc(w)),l.map(w=>Math.trunc(w)),f,b);g=lf(E,h,b)}else if(v>0)u+=" with Yates' continuity correction",h=wn(m.flatMap((b,E)=>b.map((w,y)=>Vt(Math.abs(w)-v,f[E][y]))));else if(h=wn(m.flatMap((b,E)=>b.map((w,y)=>Vt(w,f[E][y])))),n){u="Pearson's 'N-1' Chi-squared test";let b=h*(c-1);h=Number.isFinite(b)?b/c:h*((c-1)/c)}return{method:u,statistic:h,statisticName:"X-squared",parameter:p,parameterName:"df",pValue:g??ff(h,p),observed:e,expected:f,residuals:m.map((b,E)=>b.map((w,y)=>w/Math.sqrt(f[E][y]))),stdres:Vp(e,c,a,l,f,m),ftResiduals:Co(e,f),warnings:Number.isFinite(p)&&f.some(b=>b.some(E=>E<5))?[...N,af]:N}}function Vp(e,t,n,r,o,s){if(!Number.isFinite(t))return s.map((p,g)=>p.map((d,v)=>d/Math.sqrt(o[g][v]*(1-n[g]/t)*(1-r[v]/t))));let i=e.map(p=>of(p)),c=e[0].map((p,g)=>of(e.map(d=>d[g]))),a=zp(i),l=sf(i),f=sf(c),m=l.map(p=>ot(p,a)),u=c.map(p=>ot(p,a)),h=f.map(p=>ot(p,a));return e.map((p,g)=>p.map((d,v)=>{let N=Ce(i[g],u[v]),b=De([d,0],N),E=b[0]+b[1],w=Ce(Ce(N,m[g]),h[v]),y=w[0]+w[1];if(y>=$p||E===0)return E/Math.sqrt(y);let x=N[0]+N[1],M=m[g][0]+m[g][1],R=h[v][0]+h[v][1];return E/Math.sqrt(x)/Math.sqrt(M)/Math.sqrt(R)}))}var $p=2**-1022;function of(e){let t=[0,0];for(let n of e)t=pe(t,[n,0]);return t}function zp(e){let t=[0,0];for(let n of e)t=pe(t,n);return t}function sf(e){let t=e.length,n=new Array(t+1);n[t]=[0,0];for(let s=t-1;s>=0;s--)n[s]=pe(e[s],n[s+1]);let r=[],o=[0,0];for(let s=0;s<t;s++)r.push(pe(o,n[s+1])),o=pe(o,e[s]);return r}var jp=Math.pow(Number.EPSILON,.25);function Ht(e,t,n,r={}){let o={tol:r.tol??jp,maxiter:r.maxIter??1e3};return r.fLower!==void 0&&(o.fLower=r.fLower),r.fUpper!==void 0&&(o.fUpper=r.fUpper),vr(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?Ht(n,r,s,{tol:2**-50,fLower:o,fUpper:i}):Ht(n,s,r,{tol:2**-50,fLower:i,fUpper:o});return Math.exp(c)}var Kp=["alternative","confInt","confLevel","or","simulatePValue","B","midp"];function Do(e,t={}){B(t,Kp,"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(ul));if(s.flat().some(f=>f>2147483647))throw new RangeError("'x' has entries too large to be integer");let i=[],c=Bp(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 J("fisherTest",{...Yp(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 J("fisherTest",{method:a+To(f),alternative:l,pValue:Wp(s,f),warnings:i})}return J("fisherTest",{method:a,alternative:l,pValue:Gp(s),warnings:i})}function Bp(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: ${kl(a)}`:null}function Wp(e,t){let n=e.filter(d=>d.some(v=>v>0)),r=e[0].map((d,v)=>v).filter(d=>e.some(v=>v[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(v=>d[v])),s=o.map(d=>d.reduce((v,N)=>v+N,0)),i=o[0].map((d,v)=>o.reduce((N,b)=>N+b[v],0)),c=s.length,a=i.length,l=[0,0];for(let d of o.flat())l=pe(l,[-ws(d),0]);let f=l[0]/(1+64*Number.EPSILON),m=s.reduce((d,v)=>d+v,0);if(m>vs)throw new RangeError("the simulation needs a total count of at most 16777216 (2^24)");let u=Ns(m),h=new Int32Array(c*a),p=0,g=Math.trunc(t);for(let d=0;d<g;d++){ys(s,i,u,h);let v=0;for(let N=0;N<h.length;N++)v-=u[h[N]];v<=f&&p++}return Math.max(0,Math.min(1,(1+p)/(g+1)))}var hf=1+1e-7,Up=16,Hp=2**24;function Fo(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=pe(r,[e[o],0]));return r[0]}var Gr=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%Up===0||!(s>0&&s<1/0)?(o=vn(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=Fo(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=pe(r,[(this.lo+o)*n[o],0]));return r[0]}tail(t,n,r){if(n===1)return r?an(t-1,this.m,this.n,this.k,!1,!1):an(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?Fo(o,s,o.length-1):Fo(o,0,s)}midTail(t,n,r){if(n===1){let i=.5*vn(t,this.m,this.n,this.k,!1);return(r?an(t,this.m,this.n,this.k,!1,!1):an(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?Fo(o,s+1,o.length-1):Fo(o,0,s-1))+.5*o[s]}};function ti(e,t,n){let r=e.density(n),o=r[t-e.lo]*hf,s=[0,0];for(let i of r)i<=o&&(s=pe(s,[i,0]));return s[0]}function pf(e,t,n,r){let o=Math.max(0,r-n),s=Math.min(r,t),i=g=>vn(g,t,n,r,!0),c=i(e)+Math.log(hf),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=(g,d)=>{let v=g,N=d+1;for(;N-v>1;){let b=v+Math.floor((N-v)/2);i(b)<=c?v=b:N=b}return v},f=(g,d)=>{let v=g-1,N=d+1;for(;N-v>1;){let b=v+Math.floor((N-v)/2);i(b)<=c?N=b:v=b}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:an(m,t,n,r,!0,!1),p=u>s?0:an(u-1,t,n,r,!1,!1);return h+p}function Oo(e,t){return rr(e,t)}function Yp(e,t,n){let r=Je(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>Hp)throw new RangeError(`the exact distribution of the 2 x 2 table has ${m} values, more than 2^24`);let u=new Gr(i+a,c+l,i+c),{lo:h,hi:p}=u,g=t.midp??!1,d;g?(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=ti(u,f,o);let v;if(f===h)v=0;else if(f===p)v=1/0;else{let y=u.mean(1);v=y===f?1:Oo(x=>u.mean(x)-f,y>f)}let N={method:n,alternative:r,pValue:d,estimate:v,estimateNames:"odds ratio",nullValue:o,nullValueName:"odds ratio",warnings:[]};if(!(t.confInt??!0))return N;let b=y=>{if(f===p)return 1/0;if(g){if(f===h&&y>=.5)return 0;let M=u.midTail(f,1,!1);return M===y?1:Oo(R=>u.midTail(f,R,!1)-y,M<y)}let x=u.tail(f,1,!1);return x===y?1:Oo(M=>u.tail(f,M,!1)-y,x<y)},E=y=>{if(f===h)return 0;if(g){if(f===p&&y>=.5)return 1/0;let M=u.midTail(f,1,!0);return M===y?1:Oo(R=>u.midTail(f,R,!0)-y,M>y)}let x=u.tail(f,1,!0);return x===y?1:Oo(M=>u.tail(f,M,!0)-y,x>y)},w;if(r==="less")w=[0,b(1-s)];else if(r==="greater")w=[E(1-s),1/0];else{let y=(1-s)/2;w=[E(y),b(y)]}return{...N,confInt:w,confLevel:s}}var Os=[[0,0]];function Sn(e){for(let t=Os.length;t<=e;t++)Os.push(pe(Os[t-1],il([t,0])));return Os[e]}function Ds(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 ei=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 Gp(e){let t=e.filter(b=>b.some(E=>E>0));if(t.length===0)throw new RangeError("all entries of the table are zero");let n=t[0].map((b,E)=>t.some(w=>w[E]>0));if(t=t.map(b=>b.filter((E,w)=>n[w])),t.length<2||t[0].length<2)return 1;t.length>t[0].length&&(t=t[0].map((b,E)=>t.map(w=>w[E])));let r=t.length,o=t.map(b=>b.reduce((E,w)=>E+w,0)),s=t[0].map((b,E)=>t.reduce((w,y)=>w+y[E],0)).sort((b,E)=>E-b),i=s.length,c=o.reduce((b,E)=>b+E,0),a=al(Sn(c));for(let b of o)a=pe(a,Sn(b));for(let b of s)a=pe(a,Sn(b));let l=[0,0];for(let b of t.flat())l=De(l,Sn(b));let f=pe(l,[Math.log1p(1e-7),0]),m=1e-9*(1+Math.abs(f[0])),[u,h]=f,p=Array.from({length:i},()=>new Map),g=(b,E)=>{let w=E.join(","),y=p[b].get(w);if(y!==void 0)return y;let x=pe(a,Sn(E.reduce((C,_)=>C+_,0)));for(let C of E)x=De(x,Sn(C));for(let C=b;C<i;C++)x=De(x,Sn(s[C]));let M={R:E,KZ:x,max:0,min:0,edges:[],pastHi:[],pastLo:[],count:[],index:new Map};if(p[b].set(w,M),b===i-1){let C=[0,0];for(let _ of E)C=De(C,Sn(_));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 A=new Array(r).fill(0),k=(C,_,I)=>{if(C===r-1){A[C]=_;let D=De(I,Sn(_)),S=E.map((W,Q)=>W-A[Q]).sort((W,Q)=>W-Q),O=g(b+1,S),q=`${S.join(",")}|${D[0]}`,V=R.get(q);V===void 0?R.set(q,{to:O,hi:D[0],lo:D[1],mult:1}):V.mult++;return}let F=Math.max(0,_-T[C+1]),L=Math.min(E[C],_);for(let D=F;D<=L;D++)A[C]=D,k(C+1,_-D,De(I,Sn(D)))};if(k(0,s[b],[0,0]),M.edges=[...R.values()],M.max=yt(M.edges.map(C=>C.hi+C.to.max)),M.min=cn(M.edges.map(C=>C.hi+C.to.min)),b===i-2){let C=M.edges.map(O=>{let q=O.to.edges[0];return{v:pe([O.hi,O.lo],[q.hi,q.lo]),mult:O.mult}});C.sort((O,q)=>O.v[0]-q.v[0]||O.v[1]-q.v[1]);let _=C.length,I=C[_-1].v[0],F=new Float64Array(_),L=new Float64Array(_),D=new Float64Array(_),S=[0,0];C.forEach(({v:O,mult:q},V)=>{F[V]=O[0],L[V]=O[1];let W=De(O,[I,0]);S=pe(S,[q*Math.exp(W[0])*(1+W[1]),0]),D[V]=S[0]}),M.dist={hi:F,lo:L,cum:D,vmax:I,kv:pe(a,[I,0])}}return M},d=g(0,[...o].sort((b,E)=>b-E)),v=new ei,N=(b,E,w,y)=>{if(E+b.max<=u-m){let[k,C]=Ds(b.KZ[0],b.KZ[1],E,w);return v.add(k,C,y),!0}if(E+b.min>u+m)return!0;let x=b.dist;if(x===void 0)return!1;let[M,R]=Ds(u,h,-E,-w),T=0,A=x.hi.length;for(;T<A;){let k=T+A>>>1,C=x.hi[k];C<M||C===M&&x.lo[k]<=R?T=k+1:A=k}if(T>0){let[k,C]=Ds(x.kv[0],x.kv[1],E,w);v.add(k,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 b=0;b<i-2;b++)for(let E of p[b].values()){let{pastHi:w,pastLo:y,count:x}=E;if(x.length!==0){for(let{to:M,hi:R,lo:T,mult:A}of E.edges)for(let k=0;k<x.length;k++){let[C,_]=Ds(w[k],y[k],R,T),I=x[k]*A;if(N(M,C,_,I))continue;let F=M.index.get(C);F===void 0?(M.index.set(C,M.count.length),M.pastHi.push(C),M.pastLo.push(_),M.count.push(I)):M.count[F]+=I}E.pastHi=E.pastLo=E.count=[],E.index.clear()}}return Math.min(1,v.value())}function Ps(e){let t=e.every(n=>Number.isInteger(n))?1:2;return[t,e.map(n=>n*t).sort((n,r)=>n-r)]}function ri(e){return[...e].sort((t,n)=>t-n).join(",")}function ni(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 oi(e){let[,t]=Ps(e),n=0,r=0;for(let o of t)r+=n+=o;return{steps:r,size:n+1}}function si(e){let[t,n]=Ps(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 ni(t,0,o)}function ai(e,t){let[,n]=Ps(e),r=n.length,o=Math.min(t,r-t),s=df(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 df(e){let t=[0];for(let n of e)t.push(t[t.length-1]+n);return t}function ii(e,t){let[n,r]=Ps(e),o=r.length,s=Math.min(t,o-t),i=df(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 g=Math.min(h,s);g>=Math.max(1,s-(o-h));g--){let d=c[g],v=c[g-1],N=g/h,b=(h-g)/h,E=i[g],w=i[h]-i[h-g],y=i[g-1]+p,x=Math.max(E,y);for(let M=w;M>=x;M--)d[M-E]=b*d[M-E]+N*v[M-y];for(let M=x-1;M>=E;M--)d[M-E]=b*d[M-E]}}let a=c[s],l=t*(t+1)/2;if(s===t)return ni(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 ni(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 Mr(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 gf=["alternative","mu","paired","exact","correct","confInt","confLevel","tolRoot"];function Po(e,t,n={}){if(typeof e!="string"&&ct(t)&&B(t,gf,"wilcoxTest"),B(n,gf,"wilcoxTest"),typeof e=="string"){if(n.paired!==void 0)throw new Error("cannot use 'paired' in formula method");let g=Es(e,t,!0);return g.kind==="one"?Po(g.x,n):Po(g.x,g.y,g.kind==="paired"?{...n,paired:!0}:n)}let r;t!==void 0&&!ArrayBuffer.isView(t)&&!Array.isArray(t)?n=t:r=t;let o=Je(n.alternative),s=n.mu??0,i=n.paired??!1,c=n.exact??null,a=Qp(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[g,d]=Jn(Array.from(e),Array.from(r));u=Dt(g.map((v,N)=>v-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 At};return J("wilcoxTest",h===void 0?nd(u,c,i,p):fd(u,h,c,p))}function Qp(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 Vs(e,t,n){return n?t==="two.sided"?Math.sign(e)*.5:t==="greater"?.5:-.5:0}var vf=10*Number.EPSILON;function Nf(e,t,n=0){let r=a=>Math.min(1,Math.max(a,0)),o=()=>n===0?ae(e):r(ae(e)-n*Zt(e)),s=()=>n===0?ae(e,{lowerTail:!1}):r(ae(e,{lowerTail:!1})+n*Zt(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 Zp(e){let t=e*e;return[e*(t-3),e*(t*(t-10)+15),e*(t*(t*(t-21)+105)-105)]}function Jp(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 yf(e,o,s,n)}function ed(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 yf(e,i,c,r)}function yf(e,t,n,r){let[o,s,i]=Zp(e),c=t*o;return r>=2&&(c+=n*s),r>=3&&(c+=t*t/2*i),c}function fi(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 mi=(e,t)=>e-t,ci=5e9,td=40;function li(e){return new RangeError(`wilcoxTest: the exact conditional ${e} would take too long or too much memory: use exact: false`)}function $s(e,t){if(e.size>16777216||e.steps>ci)throw li(t);return e.steps}function Mf(e){let t=e.length,n=vt(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 bn(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 nd(e,t,n,r){let{alternative:o,mu:s}=r,i=e.map(w=>w-s);i.some((w,y)=>!Number.isFinite(w)&&Number.isFinite(e[y]))&&(i=e.map(w=>w/2-s/2));let a=i.some(w=>w===0),l=t??i.length<50,f=(w,y,x)=>({method:w,alternative:o,statistic:y,statisticName:"V",pValue:x,nullValue:s,nullValueName:n?"location shift":"location",warnings:r.warnings.list});if(l){let w=vt(i.map(Math.abs)),y=bn(w).length>0,x=0;for(let C=0;C<i.length;C++)i[C]>0&&(x+=w[C]);let M="Wilcoxon signed rank exact test";if(!y&&!a){let C=i.length,_=C*(C+1)/2,I;if(o==="less")I=Br(x,C,!0,!1);else if(o==="greater")I=Br(x-1,C,!1,!1);else{let L=x>_/2?Br(x-1,C,!1,!1):Br(x,C,!0,!1);I=Math.min(1,2*L)}let F=f(M,x,I);return r.confInt?{...F,...rd(e,r)}:F}let R=w.filter((C,_)=>i[_]!==0);$s(oi(R),"distribution");let T=si(R),A;if(o==="less")A=or(T,x);else if(o==="greater")A=Mr(T,x-.25);else{let C=0;for(let _ of R)C+=_/2;A=Math.min(1,2*(x>C?Mr(T,x-.25):or(T,x)))}let k=f(M,x,A);return r.confInt?{...k,...sd(e,r)}:k}let m=a?i.filter(w=>w!==0):i,u=m.length;if(u===0)throw new RangeError("no non-zero differences for the signed rank test");let[h,p,g]=Mf(m),v=bn(vt(m.map(Math.abs))).length>0||a?Math.min(r.correct,0):r.correct,N=(p-Vs(p,o,v>=0))/g,b=Nf(N,o,v>=1?Jp(N,u,v):0),E=f(`Wilcoxon signed rank test${r.correct>=0?" with continuity correction":""}`,h,b);return r.confInt?{...E,...ad(e,r)}:E}function ui(e,t,n){return e==="less"?[-1/0,n]:e==="greater"?[t,1/0]:[t,n]}function wf(e,t,n){let r={confInt:ui(e,NaN,NaN),confLevel:0,estimate:t};return n!==void 0&&(r.estimateNames=n),r}function Ef(e){if(!(e>0&&e<1/0))throw new RangeError("tolRoot must be a positive finite number")}function xf(e){let t=e.length,n=[];for(let r=0;r<t;r++)for(let o=r;o<t;o++){let s=gn(e[r],e[o]);Number.isNaN(s)||n.push(s)}return n.sort(mi)}function Rf(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+vf&&l++;let f=fi(e);if(l===0)return{confInt:[-1/0,1/0],confLevel:1,estimate:f,estimateNames:o};let m=t(l-1);return{confInt:ui(s,e[l-1],e[c-l]),confLevel:1-(s==="two.sided"?2*m:m),estimate:f,estimateNames:o}}function rd(e,t){let n=e.length;return Rf(xf(e),r=>Br(r,n,!0,!1),r=>vl(r,n,!0,!1),t,"(pseudo)median")}function Tf(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 Af(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)+vf,g=f!=="less",d=f!=="greater",v=I=>gn(e[I-1],e[I]),N=Math.min(u-1,Math.max(1,Math.ceil(i.mean+ie(Math.min(p,1))*i.sd+.1*i.sd+3))),b=8,E=I=>I===0?e[0]-1:v(I),w=I=>I===0?e[u-1]+1:v(u-I),y=[new Map,new Map];{let I=new Map,F=0,L=td*c;for(let[D,S]of[[0,E],[1,w]])if(!(D===0?!g:!d))for(let O=0;O<=N;O++){if(F+=L,F>ci)throw li("interval");let q;try{q=t(S(O))}catch{break}if(y[D].set(O,[q.stat,q.key]),I.has(q.key)){I.delete(q.key),I.set(q.key,!0);continue}if(F+=r(q.scores),F>ci)throw li("interval");I.set(q.key,!0),I.size>b&&I.delete(I.keys().next().value)}}let x=new Map,M=(I,F)=>{let L=I===0?E(F):w(F),D=y[I].get(F),S;if(D===void 0){let W=t(L);D=[W.stat,W.key],S=W.scores}let[O,q]=D,V=x.get(q);return V!==void 0?x.delete(q):(V=n(S??t(L).scores),x.size>=b&&x.delete(x.keys().next().value)),x.set(q,V),[O,V]},R=I=>{let[F,L]=M(0,I);return o(L,F)},T=I=>{let[F,L]=M(1,I);return s(L,F)},A=-1/0,k=0;if(g){let I=R(0);if(!(I>p)){let F=I,L=1;for(;L<=u-1;L++){let D=R(L);if(D>p)break;F=D}A=e[L-1],k=F}}let C=1/0,_=0;if(d){let I=T(0);if(!(I>p)){let F=I,L=u-1;for(;L>=1;L--){let D=T(u-L);if(D>p)break;F=D}C=e[L],_=F}}return{confInt:[A,C],confLevel:1-(k+_),estimate:fi(e),estimateNames:l}}function od(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 sd(e,t){let n=e.length,r=s=>{let i=od(e,s),{ranks:c,key:a}=Tf(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 Af(xf(e),r,si,s=>$s(oi(s),"interval"),(s,i)=>Mr(s,i-.25),or,{mean:o/2,sd:Math.sqrt(n*(n+1)*(2*n+1)/24)},n,t,"(pseudo)median")}function ad(e,t){let{alternative:n,confLevel:r,tolRoot:o}=t,s=t.correct>=0,i=e.length;if(i===0)return wf(n,NaN);let c=cn(e),a=yt(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"),wf(n,gn(c,a),"midrange");let l=(b,E)=>{let[,w,y]=Mf(e.map(x=>x-b).filter(x=>x!==0));return(w-Vs(w,n,E))/y},f=l(c,s),m=l(a,s),u=b=>-ie(b),h=b=>n==="greater"?!(f<u(b)):n==="less"?!(m>ie(b/2)):!(f<u(b/2))&&!(m>ie(b/2)),p=1-r,g=!1;for(;!h(p)&&p<1;)p*=2,g=!0;g&&t.warnings.add("requested conf.level not achievable"),Ef(o);let d=b=>Ht(E=>l(E,s)-b,c,a,{tol:o,fLower:f-b,fUpper:m-b}),v;if(p>=1){let b=fi([...e].sort(mi));v=ui(n,b,b)}else n==="greater"?v=[d(u(p)),1/0]:n==="less"?v=[-1/0,d(ie(p))]:v=[d(u(p/2)),d(ie(p/2))];let N=Ht(b=>l(b,!1),c,a,{tol:o,fLower:l(c,!1),fUpper:l(a,!1)});return{confInt:v,confLevel:g?1-Math.min(1,p):r,estimate:N,estimateNames:"(pseudo)median"}}function Sf(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 bn(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 id(e,t){let[n,r,o]=Sf(vt([...e,...t]),e.length);return[n,r,o]}function cd(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 ld(e,t,n){return kf(e,t,n).ranks}function kf(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=vt([...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:ri(i)}}if(Number.isFinite(n)&&r.some((o,s)=>!Number.isFinite(o)&&Number.isFinite(e[s]))){let o=cd(e.map(s=>s/2-n/2),t);return{ranks:o,key:ri(o)}}return Tf([...r,...t])}function fd(e,t,n,r){let{alternative:o,mu:s}=r,i=e.length,c=t.length,a=n??(i<50&&c<50),l=ld(e,t,s),[f,m,u,h]=Sf(l,i),p,g;if(a)if(g="Wilcoxon rank sum exact test",!h)p=bf(()=>o==="less"?Wr(f,i,c,!0,!1):o==="greater"?Wr(f-1,i,c,!1,!1):Math.min(2*Wr(f,i,c,!0,!1),2*Wr(f-1,i,c,!1,!1),1));else{$s(ai(l,i),"distribution");let N=ii(l,i);o==="less"?p=or(N,f):o==="greater"?p=Mr(N,f-.25):p=Math.min(2*or(N,f),2*Mr(N,f-.25),1)}else{g=`Wilcoxon rank sum test${r.correct>=0?" with continuity correction":""}`;let N=h?Math.min(r.correct,0):r.correct,b=l.every(w=>w===l[0]),E=(m-Vs(m,o,N>=0))/u;p=b?1:Nf(E,o,N>=1?ed(E,i,c,N):0)}let d={method:g,alternative:o,statistic:f,statisticName:"W",pValue:p,nullValue:s,nullValueName:"location shift",warnings:r.warnings.list};if(!r.confInt)return d;let v=a?h?ud(e,t,r):bf(()=>md(e,t,r)):hd(e,t,r);return{...d,...v}}function bf(e){try{return e()}catch(t){throw t instanceof RangeError&&t.message.startsWith("wilcox:")?new RangeError(`${t.message}: use exact: false`):t}}function qf(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(mi)}function md(e,t,n){let r=e.length,o=t.length;return Rf(qf(e,t,!1),s=>Wr(s,r,o,!0,!1),s=>yl(s,r,o,!0,!1),n,"difference in location")}function ud(e,t,n){let r=e.length,o=t.length,s=i=>{let{ranks:c,key:a}=kf(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 Af(qf(e,t,!0),s,i=>ii(i,r),i=>$s(ai(i,r),"interval"),(i,c)=>Mr(i,c-.25),or,{mean:r*o/2,sd:Math.sqrt(r*o*(r+o+1)/12)},r+o,n,"difference in location")}function hd(e,t,n){let{alternative:r,confLevel:o,tolRoot:s}=n,i=n.correct>=0,c=cn(e)-yt(t),a=yt(e)-cn(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[,g,d]=id(e.map(v=>v-h),t);return(g-Vs(g,r,p))/d},f=(h,p)=>{let g=l(c,p)-h,d=l(a,p)-h;if(Number.isNaN(g)||Number.isNaN(d))throw new Error("cannot compute the confidence interval because all observations are tied");return g<=0?c:d>=0?a:(Ef(s),Ht(v=>l(v,p)-h,c,a,{tol:s,fLower:g,fUpper:d}))},m=1-o,u;return r==="greater"?u=[f(-ie(m),i),1/0]:r==="less"?u=[-1/0,f(ie(m),i)]:u=[f(-ie(m/2),i),f(ie(m/2),i)],{confInt:u,confLevel:o,estimate:f(0,!1),estimateNames:"difference in location"}}var pd=["method","n"],If=["holm","hochberg","hommel","bonferroni","BH","BY","fdr","none"];function dd(e){if(If.includes(e))return e;let t=If.filter(n=>n.startsWith(e));if(e.length===0||t.length!==1)throw new RangeError(`invalid method "${e}"`);return t[0]}function Xr(e,t={}){B(t,pd,"pAdjust");let n=typeof t=="string"?{method:t}:t,r=dd(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=Lf(a,r,c),f=o.slice();return s.forEach((m,u)=>f[m]=l[u]),f}function zs(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 Lf(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=zs(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=zs(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++)dn(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=zs(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?Lf(e,"hochberg",n):gd(e,n)}}var _f=2**16;function gd(e,t){if(!Number.isInteger(t))throw new RangeError("n must be an integer for the hommel method");if(t>_f)throw new RangeError(`n must be at most ${_f} for the hommel method (O(n^2) work)`);let n=e.length,r=[...e,...new Array(t-n).fill(1)],o=zs(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 Cf=["p","alternative","confLevel"],wd=1+1e-7;function hi(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)+pn(e,t,n,!1)}function bd(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=pn(e,t,n,!1)*wd,s=a=>pn(a,t,n,!1)<=o;if(e<r){let a=fs(s,Math.ceil(r),t),l=a>t?0:hi(a,t,n);return Math.min(1,Dn(e,t,n,!0,!1)+l)}let i=ms(s,0,Math.floor(r)),c=i<0?0:Dn(i,t,n,!0,!1);return Math.min(1,hi(e,t,n)+c)}function vd(e,t,n={}){B(t,Cf,"binomTest"),B(n,Cf,"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),!Nn(e[0])||!Nn(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(!Nn(e))throw new RangeError("'x' must be nonnegative and integer");if(r=Math.round(e),!(Nn(t)&&Math.round(t)>=r&&t>0))throw new RangeError("'n' must be a positive integer >= 'x'");o=Math.round(t)}return pi(r,o,n)}function pi(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=Je(n.alternative),s=n.confLevel??.95;_t(s);let i;o==="less"?i=Dn(e,t,r,!0,!1):o==="greater"?i=hi(e,t,r):i=bd(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 Nd=["T","r","alternative","confLevel"],yd=1+1e-7;function Md(e,t){if(t===0)return e===0?1:0;if(e===t)return 1;let n=us(e,t,!1)*yd,r=i=>us(i,t,!1)<=n,o=Math.floor(t);if(e<t){let i=fs(r,Math.max(e+1,o),1/0);return Math.min(1,Kr(e,t,!0,!1)+di(i,t))}let s=ms(r,0,Math.min(e-1,o));return Math.min(1,Kr(s,t,!0,!1)+di(e,t))}var Vo=2**-1022;function di(e,t){return e-(e-1)===1?Kr(e-1,t,!1,!1):Kr(e,t,!1,!1)+us(e,t,!1)}function Er(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 Ed(e,t,n,r){let o=n+t*r,s=e*t*n/o;if(Number.isFinite(s)&&Number.isFinite(o)&&o>=Vo&&(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]=Er(e),[f,m]=Er(t),[u,h]=Er(n),[p,g]=Er(r),d=f*p,v=h-m-g,N=v>=0?u/(u+$(d,-v)):u/($(u,v)+d);return $(a*f*N,l+m+Math.min(v,0))}function xd(e,t,n,r){let o=e/n,s=t/r,i=o/s;return t===0||Number.isFinite(o)&&Number.isFinite(s)&&s>=Vo&&(o===0||o>=Vo)?i:e===0?0:e/t*(r/n)}function Rd(e,t={}){B(t,Nd,"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(Nn))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(v=>Number.isFinite(v)&&v>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=Je(t.alternative),l=t.confLevel??.95;if(o===2?_t(l):ln(l),o===2){let[v,N]=r,[b,E]=i,w=v+N;if(w===1/0)throw new RangeError("'x' must be finite, nonnegative, and integer");let y=c*b,x=y+E,M=Number.isFinite(x)&&x>=Vo&&(y===0||y>=Vo)?y/x:1/(1+E/b/c),R=pi(Math.round(v),Math.round(v)+Math.round(N),{p:M,alternative:a,confLevel:l}),T=E/b,A=k=>{if(k===0)return 0;let C=k/(1-k);if(Number.isFinite(T)||C===1/0)return C*T;let[_,I]=Er(C),[F,L]=Er(b),[D,S]=Er(E);return $(_*D/F,I+S-L)};return{method:"Comparison of Poisson rates",alternative:a,statistic:v,statisticName:"count1",parameter:Ed(w,c,b,E),parameterName:"expected count1",pValue:R.pValue,confInt:[A(R.confInt[0]),A(R.confInt[1])],confLevel:l,estimate:xd(v,N,b,E),estimateNames:"rate ratio",nullValue:c,nullValueName:"rate ratio",warnings:[]}}let f=r[0],m=i[0],u=c*m,h;a==="less"?h=Kr(f,u,!0,!1):a==="greater"?h=di(f,u):h=Md(f,u);let p=v=>f===0?0:ls(v,f,1,!0,!1),g=v=>v<1e-8?ls(v,f+1,1,!1,!1):ls(1-v,f+1,1,!0,!1),d;if(a==="less")d=[0,g(1-l)];else if(a==="greater")d=[p(1-l),1/0];else{let v=(1-l)/2;d=[p(v),g(v)]}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 Ff=["p","alternative","confLevel","correct"],Td="Chi-squared approximation may be incorrect";function gi(e,t,n={}){ct(t)&&B(t,Ff,"propTest"),B(n,Ff,"propTest");let r,o,s=n;if(typeof e!="number"&&e.length>0&&Array.isArray(e[0])){let A=e;if(A.some(k=>k.length!==2))throw new RangeError("'x' must have 2 columns");r=A.map(k=>k[0]),o=A.map(k=>k[0]+k[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 A=0;A<i;A++)!Number.isNaN(r[A])&&!Number.isNaN(o[A])&&c.push(A);let a=c.length;if(a<1)throw new RangeError("not enough data");if(r=c.map(A=>r[A]),o=c.map(A=>o[A]),o.some(A=>!(A>0)))throw new RangeError("elements of 'n' must be positive");if(o.some(A=>A===1/0))throw new RangeError("elements of 'n' must be finite");if(r.some(A=>!(A>=0)))throw new RangeError("elements of 'x' must be nonnegative");if(r.some((A,k)=>A>o[k]))throw new RangeError("elements of 'x' must not be greater than those of 'n'");let l;if(s.p!==void 0){let A=typeof s.p=="number"?[s.p]:Array.from(s.p);if(A.length!==i)throw new RangeError("'p' must have the same length as 'x' and 'n'");if(l=c.map(k=>A[k]),l.some(k=>!(k>0&&k<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=Je(s.alternative);(a>2||a===2&&f)&&(u="two.sided");let h=r.map((A,k)=>A/o[k]),p=a===2?h[0]-h[1]:0,g=a===2?1/o[0]+1/o[1]:0,d=(s.correct??!0)&&a<=2?.5:0,v=!1;if(d>0&&a===1){let A=Math.abs(r[0]-o[0]*l[0]);A<=d&&([d,v]=[A,!0])}else if(d>0&&a===2&&!f){let A=Math.abs(p)/g;A<=d&&([d,v]=[A,!0])}let N=Ad(r,o),b=o.some((A,k)=>{let C=l?.[k]??N;return A*C<5||A*(1-C)<5}),E=0;if(!v)for(let A=0;A<a;A++){let k=l?.[A]??N,C=o[A]*k,_=o[A]*(1-k);E+=Vt(Math.abs(r[A]-C)-d,C),E+=Vt(Math.abs(o[A]-r[A]-_)-d,_)}let w=l!==void 0?a:a-1,y;if(u==="two.sided")y=bt(E,w/2,2,!1,!1);else{let A=Math.sign(a===1?h[0]-l[0]:p);y=ae(A*Math.sqrt(E),{lowerTail:u==="less"})}let x;if(a===1||a===2&&!f){let A=(1-m)/2,k=u==="two.sided"&&A<1e-8?ie(A,{lowerTail:!1}):ie(u==="two.sided"?(1+m)/2:m);if(a===1){let C=o[0],_=k*k,I=(O,q)=>(O+_/(2*C)+q*k*Math.sqrt(O*(1-O)/C+_/(4*C*C)))/(1+_/C),F=h[0]+d/C,L=h[0]-d/C,D=Math.min(1,Math.max(0,F>=1?1:I(F,1))),S=Math.min(1,Math.max(0,L<=0?0:I(L,-1)));x=u==="two.sided"?[S,D]:u==="greater"?[S,1]:[0,D]}else{let C=Math.sqrt(h.reduce((L,D,S)=>L+D*(1-D)/o[S],0)),_=k*C+d*g,I=Math.max(-1,p-_),F=Math.min(1,p+_);x=u==="two.sided"?[I,F]:u==="greater"?[I,1]:[-1,F]}}let M=a===1?"p":c.map(A=>`prop ${A+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:w,parameterName:"df",pValue:y,estimate:a===1?h[0]:h,estimateNames:M,warnings:b?[Td]:[]};return l!==void 0&&(T.nullValue=a===1?l[0]:l,T.nullValueName=M),x!==void 0&&(T.confInt=x,T.confLevel=m),J("propTest",T)}function Ad(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+$(s,-r),0)/t.reduce((o,s)=>o+$(s,-r),0)}var Sd=["method","confLevel","sides"],kd=["wilson","wald","wilsoncc","agresti-coull","jeffreys","clopper-pearson","midp"],qd=["two.sided","left","right"];function $o(e,t){let n=ke(t,qd),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{...wi(s,o),sides:n}}function wi(e,t=1-e){let n=e/2<1e-8,r=n?ie(e/2,{lowerTail:!1}):ie(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 Qr(e,t){if(typeof e!="number")throw new TypeError(`'${t}' must be a number`);if(!Nn(e))throw new RangeError(`'${t}' must be a nonnegative integer`);return Math.round(e)+0}function Bn(e,t,n,r){let o=Qr(e,n),s=Qr(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 Ks(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 js(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 Of(e,t,n,r){let o=n/2,s=r?f=>{let m=Math.exp(f);return .5*pn(e,t,m,!1)+Dn(e-1,t,m,!0,!1)-o}:f=>{let m=Math.exp(f);return .5*pn(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:Ht(g=>i*s(g),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(Ht(f=>i*s(f),c,0,{tol:2**-60,fLower:a,fUpper:l}))}function bi(e,t,n={}){B(n,Sd,"binomCI");let r=ke(n.method,kd),o=$o(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]=Ks(s,i,a);break;case"wilsoncc":{let h=a*a,p=2*(i+h),g=2*s+h-1,d=a*Math.sqrt(h-2-1/i+4*l*(i*f+1));m=l===0?0:g-d<g/2?(2*s-1)/(2*i)*(2*s-1)/(g+d):(g-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,g=(s+h/2)/p,d=a*Math.sqrt(g*(1-g)/p);[m,u]=[g-d,g+d];break}case"jeffreys":m=s===0?0:js(c,s+.5,i-s+.5,!1),u=s===i?1:js(c,s+.5,i-s+.5,!0);break;case"clopper-pearson":m=s===0?0:js(c,s,i-s+1,!1),u=s===i?1:js(c,s+1,i-s,!0);break;case"midp":m=s===0?0:s===i?Math.exp(Math.log(c)/i):Of(s,i,c,!1),u=s===i?1:s===0?-Math.expm1(Math.log(c)/i):Of(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),J("binomCI",{estimate:l,confInt:[m,u],confLevel:n.confLevel??.95,method:r,sides:o.sides,warnings:[]})}function zo(e){return e===1/0?1:e/(1+e)}function jo(e,t){return e===0?0:t>0?e*e/t:1/0}function Id(e,t,n,r){return e/t+n/r>1}function _d(e,t,n,r){return Ws(e,t,n,r,0)}function yi(e,t,n,r,o){if(Id(e,t,n,r)){let[p,g,d,v]=yi(t-e,t,r-n,r,-o);return[g,p,v,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=v=>{let N=0;return i>0&&(N+=i/v),i<c&&(N-=(c-i)/(1-v)),a>0&&(N+=a/(v+f)),a<l&&(N-=(l-a)/(m-v)),N},g=v=>{let N=0;return i>0&&(N-=i/(v*v)),i<c&&(N-=(c-i)/((1-v)*(1-v))),a>0&&(N-=a/((v+f)*(v+f))),a<l&&(N-=(l-a)/((m-v)*(m-v))),N};if(p(0)<=0)u=0;else if(p(m)>=0)u=m;else{let v=0,N=m,b=Ld(e,t,n,r,o);u=s?b:b-o,u>v&&u<N||(u=N/2);for(let E=0;E<2e3;E++){let w=p(u);if(w===0)break;w>0?v=u:N=u;let y=u-w/g(u);if(y>v&&y<N||(y=v===0?N/2:N>2*v?Math.sqrt(v*N):v+(N-v)/2),!(y>v&&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 Ld(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 g=(Math.PI+Math.acos(Math.max(-1,Math.min(1,h/(p*p*p)))))/3;return 2*p*Math.cos(g)-u}function Mi(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]=yi(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 Cd(e,t,n,r,o,s,i){let c=De(De(ft([e,0],t),ft([n,0],r)),[o,0]),[a,l,f,m]=yi(e,t,n,r,o),u=pe(ft(jt(a,l),t),ft(jt(f,m),r));return s==="mn"&&(u=ft(xt(u,t+r),t+r-1)),De(Ce(c,c),xt(u,i))[0]}function Ws(e,t,n,r,o){return De(De(ft([e,0],t),ft([n,0],r)),[o,0])[0]}function Fd(e,t,n,r,o,s){return jo(Ws(e,t,n,r,o),Mi(e,t,n,r,o,s))}var Bs=new Float64Array(1),vi=new BigInt64Array(Bs.buffer);function Df(e){return Bs[0]=Math.abs(e),e<0?-vi[0]:vi[0]}function sr(e){return vi[0]=e<0n?-e:e,e<0n?-Bs[0]:Bs[0]}function Ni(e,t,n,r,o){let s=e(t);if(s===0||Number.isNaN(s))return t;let i=s<0===o,c=Df(i?r:n),a=Df(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 Vf(e,t,n,r,o,s){let i=_d(e,t,n,r);if(o===0)return[i,i];let c=zo(o),a=p=>(p===i?0:zo(Fd(e,t,n,r,p,s)))-c,l=(p,g,d,v)=>{if(d===0)return p;if(v===0)return g;let N=Ht(a,p,g,{tol:1e-300,fLower:d,fUpper:v});return s==="wald"?N:Ni(b=>Cd(e,t,n,r,b,s,o),N,p,g,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 Pf(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?Ht(e,r,i,{tol:2**-60,fLower:o,fUpper:c}):Ht(e,i,r,{tol:2**-60,fLower:c,fUpper:o});return Math.exp(a)}r=i,o=c,s*=2}}function Od(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 Dd(e,t,n,r,o){let[s,i]=Od(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=xt(x,o);return[M[0],De([1,0],M)[0],x[0],De([1,0],x)[0]]},p=e+n,g=c<2?1:-1,d=y=>{let[,x,M,R]=h(y),T=0,A=0;return p>0&&(T+=M>0?p/M:1/0,A-=p/(M*M)),e<t&&(T-=x>0?o*(t-e)/x:1/0,A-=o*(t-e)/x*(o/x)),n<r&&(T-=R>0?(r-n)/R:1/0,A-=(r-n)/(R*R)),[g*T,A*f]},v;if(!(m>u))v=u;else if(!(d(u)[0]>0))v=u;else if(!(d(m)[0]<0))v=m;else{let y=u,x=m;v=a,v>y&&v<x||(v=y+(x-y)/2);for(let M=0;M<2e3;M++){let[R,T]=d(v);if(R===0)break;R>0?y=v:x=v;let A=v-R/T;if(A>y&&A<x||(A=y<=0?x/2:x>2*y?Math.sqrt(y*x):y+(x-y)/2),!(A>y&&A<x))break;let k=Math.abs(A-v);if(v=A,k<=2**-53*v)break}}let[N,b,E,w]=h(v);return[Math.min(1,Math.max(0,N)),Math.min(1,Math.max(0,b)),Math.min(1,Math.max(0,E)),Math.min(1,Math.max(0,w))]}function Pd(e,t,n,r,o){let[s,i,c,a]=Dd(e,t,n,r,o),l=(f,m,u,h)=>u>.5?m*h-(m-f):f-m*u;return jo(l(e,t,s,i),t*s*i)+jo(l(n,r,c,a),r*c*a)}function $f(e,t,n,r,o){let s=e/t/(n/r);return jf(s,o,i=>Pd(e,t,n,r,i))}function Vd(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 $d(e,t,n,r,o){let[s,i]=Vd(e,t,n,r,o),c=e-t*s;return jo(c,t*s*(1-s))+jo(c,r*i*(1-i))}function zf(e,t,n,r,o){let s=e*(r-n)/((t-e)*n);return jf(s,o,i=>$d(e,t,n,r,i))}function jf(e,t,n){if(t===0)return[e,e];let r=zo(t),o=l=>n(l)-t,s=l=>l>0&&l<e?Ni(o,l,Number.MIN_VALUE,e,!1):l,i=l=>l>e&&l<1/0?Ni(o,l,e,Number.MAX_VALUE,!0):l;if(e===0||e===1/0){let l=u=>zo(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:zo(n(Math.exp(l))))-r;return[s(Pf(a,c,-1)),i(Pf(a,c,1))]}var zd=["method","confLevel","sides"],jd=["method","confLevel","sides"],Kd=["method","confLevel"],Bd=["ac","wald","score","mn","mee"];function Ei(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,g=i*Math.sqrt(u*(1-u)/(t+2)+h*(1-h)/(r+2));[f,m]=[Math.max(-1,p-g),Math.min(1,p+g)];break}case"score":{let[u,h]=Ks(e,t,i),[p,g]=Ks(n,r,i);f=l-i*Math.sqrt(u*(1-u)/t+g*(1-g)/r),m=l+i*Math.sqrt(h*(1-h)/t+p*(1-p)/r);break}case"mn":case"mee":[f,m]=Vf(e,t,n,r,s.chisq,o);break}return[Math.min(f,m),Math.max(f,m)]}function Wd(e,t,n,r,o={}){B(o,zd,"binomDiffCI");let s=ke(o.method,Bd),i=$o(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]=Ei(c,a,l,f,s,i);return i.sides==="left"?h=1:i.sides==="right"&&(u=-1),J("binomDiffCI",{estimate:m,confInt:[u,h],confLevel:o.confLevel??.95,method:s,sides:i.sides,warnings:[]})}var Ud=["katz.log","koopman"];function Us(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 Hd(e,t,n,r,o={}){B(o,jd,"binomRatioCI");let s=ke(o.method,Ud),i=$o(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,Us(.5,a,l,f,m)[1]]:l===0?[h,p]=[Us(c,a,.5,f,m)[0],1/0]:c===a&&l===f?[h,p]=Us(a-.5,a,f-.5,f,m):[h,p]=Us(c,a,l,f,m):[h,p]=$f(c,a,l,f,i.chisq),i.sides==="left"?p=1/0:i.sides==="right"&&(h=0),J("binomRatioCI",{estimate:u,confInt:[h,p],confLevel:o.confLevel??.95,method:s,sides:i.sides,warnings:[]})}var Yd=["midp","fisher","wald","score"],Gd={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"},Xd=2**23,Qd=2**20;function Kf(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=pe(r,[e[o],0]));return r[0]}function Zd(e,t={}){B(t,Kd,"oddsRatio");let n=ke(t.method,Yd);if(e.length!==2||e.some(A=>A.length!==2))throw new TypeError("'table' must be a 2 x 2 table");let r=t.confLevel??.95,{alpha:o,kappa:s,chisq:i}=$o(r,void 0),c=e.map((A,k)=>Array.from(A,(C,_)=>Qr(C,`table[${k}][${_}]`))),[[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),g=p-h+1;if(n==="midp"||n==="fisher"){if(g>Xd)throw new RangeError(`the exact distribution of the 2 x 2 table has ${g} 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,v,N;try{d=an(a-1,a+f,l+m,a+l,!0,!1)+.5*vn(a,a+f,l+m,a+l,!1),v=an(a,a+f,l+m,a+l,!1,!1)+.5*vn(a,a+f,l+m,a+l,!1),N=g<=Qd?ti(new Gr(a+f,l+m,a+l),a,1):pf(a,a+f,l+m,a+l)}catch(A){throw A instanceof RangeError?new RangeError(`the exact p-values of the table are beyond reach: ${A.message}`):A}let b=2*Math.min(d,v),E=[a+l,f+m],w=[a+f,l+m],y=0,x=!1;for(let A=0;A<2;A++)for(let k=0;k<2;k++){let C=E[k]*w[A]/u;C<5&&(x=!0);let _=Math.abs(c[k][A]-C);y+=_*_/C}let M=bt(y,.5,2,!1,!1),R,T;switch(n){case"wald":{R=a*m/(l*f);let A=s*Math.sqrt(1/a+1/l+1/f+1/m);T=[Math.exp(Math.log(R)-A),Math.exp(Math.log(R)+A)];break}case"fisher":{let A=Ze(()=>Do(c,{confLevel:r}));R=A.estimate,T=A.confInt;break}case"score":{R=a*m/(l*f),T=(a===0||m===0)&&(l===0||f===0)?[0,1/0]:zf(a,a+l,f,f+m,i);break}case"midp":{let A=new Gr(a+f,l+m,a+l),k=F=>{if(F===1)return[d,v];let L=A.density(F),D=a-h,S=.5*L[D];return[Kf(L,0,D-1)+S,Kf(L,D+1,L.length-1)+S]},C=(F,L)=>{let D=F(1);return D===0?1:rr(F,L?D>0:D<0)};R=a===h?0:a===p?1/0:C(F=>k(F)[0]-.5,!1);let _=a===h?0:C(F=>k(F)[1]-o/2,!0),I=a===p?1/0:C(F=>k(F)[0]-o/2,!1);T=[_,I];break}}return J("oddsRatio",{estimate:R,confInt:T,confLevel:r,method:Gd[n],pValue:{midp:b,fisher:N,chiSquare:M},warnings:x?["Chi-squared approximation may be incorrect"]:[]})}var Jd=["epsilon","paired","varEqual","confLevel","alpha"],e0=["margin","method","alpha"];function Bf(e,t){if(!(typeof e=="number"&&e>0&&e<.5))throw new RangeError(`'${t}' must be a single number in (0, 0.5)`)}function t0(e,t,n={}){B(n,Jd,"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)Bf(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=Ze(()=>yn(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=gt((r-Math.abs(m))/l,f,!1,!1),h=s<1e-8?wt(s,f,!1,!1):wt(o,f,!0,!1),p=[m-h*l,m+h*l];return J("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 n0=["mn","mee","wald"];function r0(e,t,n,r,o={}){B(o,e0,"binomDiffEquivalenceTest");let s=ke(o.method,n0),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;Bf(c,"alpha");let[a,l]=Bn(e,t,"x1","n1"),[f,m]=Bn(n,r,"x2","n2"),u=a/l-f/m,h=E=>Ws(a,l,f,m,E)/Math.sqrt(Mi(a,l,f,m,E,s)),p=h(-i),g=h(i),d=[ae(p,{lowerTail:!1}),ae(g)],v=Math.max(d[0],d[1]),N=1-2*c,b=Ei(a,l,f,m,s,wi(2*c,N));return J("binomDiffEquivalenceTest",{method:s,estimate:u,margin:i,alpha:c,statistic:[p,g],statisticName:"z",pValues:d,pValue:v,result:d[0]<c&&d[1]<c?"rejected":"not rejected",confInt:b,confLevel:N,warnings:[]})}var Yt=new WeakMap;function Ys(e){let t=(n,r)=>n.weights===void 0?Array.from(r):r.filter((o,s)=>n.weights[s]!==0);return Ie(e.flatMap(n=>[t(n,n.fittedValues),t(n,n.residuals)])).k}function Gs(e,t){return e.map(n=>{if(n.rss>=o0&&n.rss<1/0)return $(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=$(o,-t);r+=i*c*c}),r})}var o0=2**-1022;function Ri(e,t){return e.y.flatMap((n,r)=>(e.weights?.[r]??1)===0?[]:[[e.weights?.[r]??1,$(n,-t)]])}function Xs(e){let t=Math.floor(Math.log2(e.reduce((n,[r])=>Math.max(n,r),0)));return t-(t%2+2)%2}function xr(e,t,n=0){let r=i=>e.reduce((c,[a,l])=>c+$(a,-i)*l*l,0),o=n,s=r(o);return Number.isFinite(s)||(o=Xs(e),s=r(o)),!($(t,-o)>=1e-24*s)}function Wf(e){let t=Ys([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+=$(e.effects[s],-t)**2}return{terms:r,rdf:e.dfResidual,rss:Gs([e],t)[0],k:t}}var Uf=(e,t,n)=>ht(e,t,n,!1,!1),Ti="essentially perfect fit for \u20181\u2019: summary may be unreliable";function Ai(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 Si=new WeakMap;function s0(e){let t=Wf(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*$(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 xi(e){let t=s0(e),{names:n,rdf:r,k:o,perfect:s}=t,{rows:i,rss:c}=t;if(r>0){let g=Ri(e,o);xr(g,c)&&(c=0,i=i.map(d=>xr(g,d.ss)?{...d,ss:0}:d))}let a=c/r,l=g=>$(g,2*o),f=i.map(({df:g,ss:d})=>{let v=d/g/a;return[g,l(d),l(d/g),v,Uf(v,g,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:g,ss:d})=>[g,d]);return p.push([r,c]),Yt.set(h,{colNames:["Df","Sum Sq"],rowNames:u,table:p}),h}function a0(e){let t=e.map(f=>f.dfResidual),n=Ys(e),r=Gs(e,n),o=f=>$(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*$(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,g]=u>0?[e[m-1],e[m]]:[e[m],e[m-1]],d=N=>N.filter((b,E)=>(p.weights?.[E]??1)!==0),v=u===0||!Ss(d(p.x),d(g.x))?NaN:h/u/i;return v<0&&(v=NaN),[t[m],o(r[m]),u,o(h),v,Number.isNaN(v)?NaN:Uf(v,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 Hs(...e){let t=[];for(let n of e)for(let r of n??[])t.includes(r)||t.push(r);return t}function ki(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=xi(e),a={...c,warnings:Hs(e.warnings,c.warnings)},l=Yt.get(c);return l!==void 0&&Yt.set(a,l),J("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?xi(e):a0(o);return i.warnings=Hs(...n.map(c=>c.warnings),s,i.warnings),J("anova",i,...n)}function qi(e){if(typeof e=="object"&&e!==null&&e.multistratum===!0){let i=e,c=Hs(i.warnings,Si.get(i)),a={strata:i.strata.filter(f=>f.stratum!=="Error: (Intercept)").map(f=>({...f})),warnings:c},l=Yt.get(i);return Array.isArray(l)&&Yt.set(a,l.filter((f,m)=>i.strata[m].stratum!=="Error: (Intercept)")),J("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=xi(t),r=Wf(t),o=Ai(t.fittedValues.map(i=>$(i,-r.k)),r.rss,r.rdf),s=Hs(t.warnings,n.warnings,o?[Ti]:[]);if(t.dfResidual>0){let i={...n,warnings:s},c=Yt.get(n);return c!==void 0&&Yt.set(i,c),J("aovSummary",i,e)}return J("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 i0=["pooledSd","mu","paired","ci","alternative","adjust"],c0=["pooledSd","mu","paired","ci","alternative"],l0=["generalized","partial","ci","alternative"],f0=["partial","ci","alternative"],m0=["partial","ci","alternative"];function Li(e){if(e!==null&&!(e>0&&e<1))throw new RangeError("ci must be a single numeric value between (0, 1)")}var u0="Missing values detected. NAs dropped.",h0=`'y' is numeric but has only 2 unique values.
3
3
  If this is a grouping variable, convert it to a factor.`,Xf={tol:0,maxiter:5e3};function Rr(e){return e===1/0?1:Math.exp(hl(.5,(e-1)/2)-.5*Math.log(e/2))}function Hf(e,t,n){let r=e-1<e+1?1:Math.abs(e)*Number.EPSILON;return vr(o=>Bt(e,t,{ncp:o})-n,e-r,e+r,{...Xf,extendInt:"downX"}).root}function Qf(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;Li(c);let a=ke(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(h0),(l.some(Number.isNaN)||(f?.some(Number.isNaN)??!1))&&m.push(u0);let u=A=>A===1/0||A===-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,g,d,v,N;if(f===void 0||i){let A;if(f===void 0)({groups:[A],k:N}=Ie([Dt(l)]));else{let[C,_]=Jn(l,f);({d:A,k:N}=ps(C,_))}let k=A.length;if(k<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(A),p=Math.sqrt(at(A)),g=p/Math.sqrt(k),d=1/k,v=k-1}else{let A=Tt([Dt(l),Dt(f)]);N=A.k;let[k,C]=A.n;if(k<2||C<2)throw new RangeError("not enough observations: at least 2 are needed in each group");h=A.mean[0]-A.mean[1];let _=Math.sqrt(A.variance[0]),I=Math.sqrt(A.variance[1]);o?(p=Math.sqrt(((k-1)*_**2+(C-1)*I**2)/(k+C-2)),g=p*Math.sqrt(1/k+1/C),d=1/k+1/C,v=k+C-2):(p=Math.sqrt((_**2+I**2)/2),d=2*(C*_**2+k*I**2)/(k*C*(_**2+I**2)),g=Math.sqrt(Math.sqrt(_**2/k)**2+Math.sqrt(I**2/C)**2),v=ds([_**2/k,I**2/C],[k-1,C-1]))}let b=r?Rr(v):1,E=$(s,-N),w=(h-E)/p*b,y=r?"Hedges_g":"Cohens_d";if(c===null)return{name:y,value:w,warnings:m};let x=a==="two.sided"?c:2*c-1,M=(h-E)/g,R=Hf(M,v,(1+x)/2)*Math.sqrt(d)*b,T=Hf(M,v,(1-x)/2)*Math.sqrt(d)*b;return a==="less"&&(R=-1/0),a==="greater"&&(T=1/0),{name:y,value:w,ci:c,ciLow:R,ciHigh:T,warnings:m}}function p0(e,t,n={}){return B(n,i0,"cohensD"),J("cohensD",Qf(e,t,n,n.adjust??!1))}function d0(e,t,n={}){return B(n,c0,"hedgesG"),J("hedgesG",Qf(e,t,n,!0))}var g0=e=>Array.isArray(e.effects),Ii=e=>e.multistratum===!0,Zf=e=>Array.isArray(e.strata),Jf=e=>{let t=e;return typeof t.univariate=="object"&&typeof t.anova=="object"},_i=e=>({test:t=>t.split(":").includes(e)});function w0(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 b0(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(v=>_i(v).test(m)),g=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]};g.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 em(e,t){let n=Array.isArray(e)?e:[],r=n.filter(s=>!t.some(i=>_i(s).test(i.name)));if(r.length>0)throw new RangeError(`Observed variable not in data: ${r.join(", ")}`);let o=s=>n.some(i=>_i(i).test(s));return{isObserved:o,ssObserved:t.reduce((s,i)=>o(i.name)?s+i.ss:s,0)}}function Yf(e,t,n,r){let o=i=>Tn(e,t,n,{ncp:i})-r,s=o(0);return Number.isNaN(s)?NaN:s>0?e===1/0?1/0:vr(o,0,Math.max(10,e*t*4),{...Xf,extendInt:"downX"}).root:0}function tm(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=Yf(c,t,n,(1+s)/2),l=Yf(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 v0(e){let t=Ie([e.map(n=>n.ss)]).k;if(t!==0)for(let n of e)n.ss=$(n.ss,-t)}var Gf="needs a table of anova() or Anova(), a fit from lm() or aov() with a formula, its aovSummary(), or a result of aovEz()";function Ci(e,t,n){if(typeof e!="object"||e===null)throw new TypeError(Gf);if(Ii(e)||Zf(e)||Jf(e))return N0(e,t,n);let r=g0(e)?Ze(()=>ki(e)):e;if(!Array.isArray(r.colNames)||!Array.isArray(r.table))throw new TypeError(Gf);let o=Yt.get(r),s=o!==void 0&&!Array.isArray(o)?o:r,i=n.ci===void 0?.95:n.ci;Li(i);let c=ke(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)");v0(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,g=p/h,d=f.reduce((R,T)=>R+T.ss,0),v=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:w,ssObserved:y}=em(N,u),x=R=>{let{ss:T,df:A}=R;if(t==="Eta2")return E==="partial"?T/(T+p):E==="plain"?T/d:T/(T+p+y-(w(R.name)?T:0));if(t==="Omega2"){let C=E==="partial"?(T-A*g)/(T+(v-A)*g):(T-A*g)/(d+g);return Math.max(0,C)}let k=E==="partial"?(T-A*g)/(T+p):(T-A*g)/d;return Math.max(0,k)},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)]:tm(x(R),R.df,h,i,c)),warnings:Array.isArray(r.warnings)?[...r.warnings]:[]}}function N0(e,t,n){let r=n.ci===void 0?.95:n.ci;Li(r);let o=ke(n.alternative??"greater",["two.sided","less","greater"]),s=Jf(e),i,c,a;if(s){let w=Yt.get(e),y=b0(w!==void 0&&!Array.isArray(w)?{...e,univariate:w}:e);({strata:i,subject:c}=y),a=y.order.map(x=>({term:x,stratum:y.strata.find(M=>M.terms.includes(x))}))}else{let w=Ii(e)?Ze(()=>qi(e)):e,y=Yt.get(w);({strata:i,subject:c}=w0(Array.isArray(y)?{...w,strata:y.map((x,M)=>({...w.strata[M],...x}))}:w)),a=i.flatMap(x=>x.terms.map(M=>({term:M,stratum:x})))}let l=Ie([[...i.flatMap(w=>w.terms.map(y=>y.ss)),...i.map(w=>w.sse)]]).k;for(let w of i){w.sse=$(w.sse,-l);for(let y of w.terms)y.ss=$(y.ss,-l)}let f=i.flatMap(w=>w.terms),m=i.reduce((w,y)=>w+y.sse,0),u=f.reduce((w,y)=>w+y.ss,0)+m,h=c.sse/c.dfe,p=t==="Eta2"?n.generalized??!1:!1,g=p!==!1?"generalized":n.partial??!0?"partial":"plain",{isObserved:d,ssObserved:v}=em(p,f),N=(w,y)=>{let{ss:x,df:M}=w,R=y.sse/y.dfe;if(t==="Eta2")return g==="partial"?x/(x+y.sse):g==="plain"?x/u:x/(x+m+v-(d(w.name)?x:0));if(t==="Omega2"){let A=y===c?0:1,k=g==="partial"?(x-M*R)/(x+A*y.sse+c.sse+h):(x-M*R)/(u+h);return Math.max(0,k)}let T=g==="partial"?(x-M*R)/(x+y.sse):(x-M*R)/u;return Math.max(0,T)},b=`${t}${g==="plain"?"":`_${g}`}`,E={colNames:r===null?[b]:[b,"CI","CI_low","CI_high"],rowNames:a.map(w=>w.term.name),table:a.map(({term:w,stratum:y})=>{let x=N(w,y);return r===null?[x]:tm(x,w.df,y.dfe,r,o)}),warnings:[]};return s||(E.groups=a.map(w=>w.stratum.name),E.warnings=[...Ii(e)||Zf(e)?e.warnings:[]]),E}function y0(e,t={}){return B(t,l0,"etaSquared"),J("etaSquared",Ci(e,"Eta2",t),e)}function M0(e,t={}){return B(t,f0,"omegaSquared"),J("omegaSquared",Ci(e,"Omega2",t),e)}function E0(e,t={}){return B(t,m0,"epsilonSquared"),J("epsilonSquared",Ci(e,"Epsilon2",t),e)}var x0=new Set(["eqb","lowEqbound","highEqbound","hypothesis","paired","varEqual","alpha","mu","eqboundType","biasCorrection","rmCorrection","glass","smdCi"]),R0=["m1","sd1","n1","m2","sd2","n2","r12"],T0=new Set(["eqb","lowEqbound","highEqbound","hypothesis","paired","varEqual","alpha","mu","eqboundType","biasCorrection","rmCorrection","glass","smdCi"]),A0="Equivalence interval does not include zero.",S0="Warning: setting bound type to SMD produces biased results!",k0="MET test may have higher error rates than a nil two-tailed test. Consider wider equivalence bounds.";function nm(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 vr(s=>Bt(e,t,{ncp:s,lowerTail:!r})-n,e-o,e+o,{tol:0,maxiter:5e3,extendInt:r?"upX":"downX"}).root}function q0(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,g,d,v,N=t.biasCorrection;if(e.design==="two"){let{n1:M,n2:R,s1:T,s2:A}=e,k=T*T,C=A*A,_=2*M*R/(M+R);if(t.glass!==void 0){let I=t.glass==="glass1",[F,L,D,S]=I?[T,M,A,R]:[A,R,T,M];c=F,a=L-1,i=N?"Glass's delta(g)":"Glass's delta(d)",l=N?Rr(a):1,f=l*o/c,p=1/R+1/M;let O=D/F;m=Math.sqrt(O*O/(S-1)+1/(L-1)+f*f/(2*(L-1))),u=o/(F*Math.sqrt(1/L+O*O/S)),h=M+R-2,g=Math.sqrt(2/_)}else if(t.varEqual)c=Math.sqrt(((M-1)*k+(R-1)*C)/(M+R-2)),a=M+R-2,i=N?"Hedges's g":"Cohen's d",l=N?Rr(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,g=Math.sqrt(2*(R*k+M*C)/(M*R*(k+C)));else{c=Math.sqrt((k+C)/2),a=(M-1)*(R-1)*(k+C)**2/((R-1)*k*k+(M-1)*C*C),i=N?"Hedges's g(av)":"Cohen's d(av)",l=N?Rr(a):1,f=l*o/c,p=2*(R*k+M*C)/(M*R*(k+C));let I=c**4;m=Math.sqrt(f*f*(k*k/(M-1)+C*C/(R-1))/(8*I)+(k/(M-1)+C/(R-1))/(c*c)),u=o/Math.sqrt(k/M+C/R),h=a,g=Math.sqrt(2/_)}d=a,v=(I,F)=>Math.sqrt(d/(d-2)*(2/_)*(1+I*I*_/2)-I*I/(F*F))}else if(e.design==="paired"){let{n:M,s1:R,s2:T,sdif:A,oneMinusR:k}=e,C=t.glass!==void 0?"":t.rmCorrection?"(rm)":"(z)";if(t.glass!==void 0?c=t.glass==="glass1"?R:T:c=t.rmCorrection?A/Math.sqrt(2*k):A,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?Rr(r==="goulet"?d:a):1,f=l*o/c,p=1/M,u=o*Math.sqrt(M)/c,h=a,g=Math.sqrt(2*k/M),v=(_,I)=>{let F=r==="goulet"?d:a,L=Math.sqrt(F/(F-2)*(2*k/M)*(1+_*_*M/(2*k))-_*_/(I*I));return t.rmCorrection?L:L*Math.sqrt(2*k)},t.glass!==void 0){let _=A/c;m=Math.sqrt(_*_/(M-1)+f*f/(2*(M-1)))}else!t.rmCorrection&&r==="nct"?m=Math.sqrt(1/M+s(l,f,a)):m=v(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?Rr(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,g=1/Math.sqrt(M),v=(T,A)=>Math.sqrt(a/(a-2)*(1/M)*(1+T*T*M)-T*T/(A*A))}let b,E,w,y=a;if(r==="nct"){w=m,y=h;let M=nm(u,h,n,!0),R=nm(u,h,n,!1);b=M*Math.sqrt(p)*l,E=R*Math.sqrt(p)*l}else if(r==="goulet"){w=v(f,l),y=d;let M=f/g;b=Wt(n,d,{ncp:M})*g,E=Wt(n,d,{ncp:M,lowerTail:!1})*g}else{w=m;let M=r==="t"?wt(n,a,!1,!1):ie(n,{lowerTail:!1});b=f-M*w,E=f+M*w}let x=e.delta<0;return{label:i,denom:c,estimate:x?-f:f,se:w,confInt:x?[-E,-b]:[b,E],df:y,J:l}}function rm(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 om(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 sm(e){let t=ke(e.hypothesis,["EQU","MET"]),n=ke(e.eqboundType,["raw","SMD"])==="SMD",r=ke(e.smdCi,["nct","goulet","t","z"]),o=e.glass===void 0?void 0:ke(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 am(e,t,n,r,o,s,i,c,a,l,f,m){let{alpha:u,hypothesis:h}=l,p=q0(c,l),g=$(p.denom,a),d=l.smdBounds?[f[0]*g,f[1]*g]:f,v=D=>{if(D===0)return[0,n];let S=Math.floor(Math.log2(Math.abs(D))),O=Ve(D,-S);return l.smdBounds?[O*p.denom,S+a]:[O,S]},N=l.smdBounds?f:[f[0]/g,f[1]/g];l.smdBounds&&m.push(S0),d[0]<=0&&d[1]>=0||m.push(A0);let b=D=>$(D/r,n-o),E=D=>{let[S,O]=v(D),q=Math.max(n,O);return Ve((Ve(t,n-q)-Ve(S,O-q))/r,q-o)},w=b(t),y=E(f[0]),x=E(f[1]),M=$(r,o),R=h==="EQU",T={t:w,se:M,df:s,pValue:2*gt(-Math.abs(w),s,!0,!1)},A={t:y,se:M,df:s,pValue:gt(y,s,!R,!1)},k={t:x,se:M,df:s,pValue:gt(x,s,R,!1)},C=R?Math.max(A.pValue,k.pValue):Math.min(A.pValue,k.pValue);!R&&d[0]<=0&&d[1]>=0&&C<=T.pValue&&m.push(k0);let _=wt(u,s,!1,!1),I=Math.max(n,o),[F,L]=[$(t,n-I),$(r,o-I)];return J(e,{tTest:T,lower:A,upper:k,pValue:C,bounds:{raw:d,smd:N},raw:{estimate:$(t,n),se:M,confInt:[$(F-_*L,I),$(F+_*L,I)]},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 I0(e,t,n={}){B(n,x0,"tTOST");let r=sm(n),o=om(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");rm(e,"x"),a||rm(t,"y");let l=Ze(()=>a?yn(e,{mu:c}):yn(e,t,{paired:s,varEqual:i,mu:c})),f=l.parameter,m,u,h,p;if(a){let g=Ie([Dt(Array.from(e))]);u=g.k;let d=g.groups[0],v=at(d);h=it(d)-$(c,-u),p=Math.sqrt(v/d.length),m={design:"one",n:d.length,delta:h,s:Math.sqrt(v)}}else if(s){let[g,d]=Jn(Array.from(e),Array.from(t)),v=Ie([g,d]);u=v.k;let[N,b]=v.groups,E=N.map((T,A)=>T-b[A]),w=Math.sqrt(at(N)),y=Math.sqrt(at(b)),x=at(E),M=Math.sqrt(x),R=(M-(w-y))*(M+(w-y))/(2*w*y);h=it(E)-$(c,-u),p=Math.sqrt(x/E.length),m={design:"paired",n:E.length,delta:it(N)-it(b)-$(c,-u),s1:w,s2:y,sdif:M,oneMinusR:R}}else{let g=Ie([Dt(Array.from(e)),Dt(Array.from(t))]);u=g.k;let[d,v]=g.groups;if(d.length<2||v.length<2)throw new RangeError("not enough observations: at least 2 are needed in each sample");let[N,b,E,w]=[d.length,v.length,at(d),at(v)];h=it(d)-it(v)-$(c,-u),p=Math.sqrt(i?((N-1)*E+(b-1)*w)/(N+b-2)*(1/N+1/b):Math.sqrt(E/N)**2+Math.sqrt(w/b)**2),m={design:"two",n1:N,n2:b,delta:h,s1:Math.sqrt(E),s2:Math.sqrt(w)}}return am("tTOST",h,u,p,u,f,l.method,m,u,r,o,[])}function _0(e,t={}){B(e,R0,"tsumTOST"),B(t,T0,"tsumTOST");let n=sm(t),r=om(t),{m1:o,sd1:s,n1:i,m2:c,sd2:a,n2:l,r12:f}=e,m=(I,F)=>{if(typeof I!="number"||!Number.isFinite(I))throw new RangeError(`'${F}' must be a finite number`);return I},u=(I,F)=>{let L=m(I,F);if(!(Number.isInteger(L)&&L>=2))throw new RangeError(`'${F}' must be an integer of at least 2`);return L},h=(I,F)=>{let L=m(I,F);if(!(L>0))throw new RangeError(`'${F}' must be positive`);return L};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 g=[],{groups:d,k:v}=Ie([[o,...p?[]:[c],n.mu]]),{groups:N,k:b}=Ie([[s,...p?[]:[a]]]),[E,w=NaN]=p?[d[0][0]]:[d[0][0],d[0][1]],y=d[0][d[0].length-1],[x,M=NaN]=N[0],R=I=>$(I,v-b),T,A,k,C,_;if(p)T=E-y,A=x/Math.sqrt(i),k=i-1,C="One Sample t-test",_={design:"one",n:i,delta:R(T),s:x};else if(n.paired){let I=m(f,"r12");if(!(I>-1&&I<1))throw new RangeError("'r12' must be in (-1, 1)");let F=i;i!==l&&(g.push("Unequal number of pairs; using smallest n"),F=Math.min(i,l));let L=Math.sqrt(x*x+M*M-2*I*x*M);if(!(L>0))throw new RangeError("the standard deviation of the differences is 0");T=E-w-y,A=L/Math.sqrt(F),k=F-1,C="Paired t-test",_={design:"paired",n:F,delta:R(T),s1:x,s2:M,sdif:L,oneMinusR:1-I}}else{let I=l,F=x*x,L=M*M;if(T=E-w-y,n.varEqual){let D=((i-1)*F+(I-1)*L)/(i+I-2);A=Math.sqrt(D*(1/i+1/I)),k=i+I-2,C=" Two Sample t-test"}else{let D=F/i,S=L/I;A=Math.sqrt(D+S),k=(D+S)**2/(D*D/(i-1)+S*S/(I-1)),C="Welch Two Sample t-test"}_={design:"two",n1:i,n2:I,delta:R(T),s1:x,s2:M}}return am("tsumTOST",T,v,A,b,k,C,_,b,n,r,g)}var L0=["alternative","tsmethod"],C0=["square","central"],F0=1e3,O0=32,D0=.9,im=1+1e-7,P0=2**24,V0=34e7;function $0(e,t={}){if(B(t,L0,"boschlooTest"),e.length!==2||e.some(y=>y.length!==2))throw new TypeError("'table' must be a 2 x 2 table");let n=Je(t.alternative),r=ke(t.tsmethod,C0),o=e.map((y,x)=>Array.from(y,(M,R)=>Qr(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)>P0)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(F0,Math.ceil(O0*Math.sqrt(m))),h=y=>Math.min(y+1,40*Math.sqrt(y));if(u*(h(l)+h(f)+4)>V0)throw new RangeError(`Boschloo's test of these group sizes (${l} and ${f}) would take too long`);let p=s/l-c/f,g;n!=="two.sided"?g=n:r==="square"?g="square":g=p>0?"greater":"less";let d=s+c,v;g==="less"?v=an(s,l,f,d,!0,!1):g==="greater"?v=an(s-1,l,f,d,!1,!1):v=Ze(()=>Do(o)).pValue;let N=z0(l,f,s,c,g),{value:b,at:E}=j0(N,l,f,u),w=n==="two.sided"&&r==="central"?Math.min(1,2*b):b;return J("boschlooTest",{method:"Boschloo's Exact Test",alternative:n,statistic:v,statisticName:"test statistic",pValue:w,estimate:p,estimateNames:"difference in proportion",nullValue:0,nullValueName:"difference in proportion",nuisanceParameter:E,warnings:[]})}function z0(e,t,n,r,o){let s=e+t,i=new Float64Array(s+1);for(let v=2;v<=s;v++)i[v]=ws(v);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 v=0;v<=s;v++){let N=Math.max(0,v-t),E=Math.min(v,e)-N+1,w=-1/0;for(let M=0;M<E;M++){let R=N+M,T=-i[R]-i[e-R]-i[v-R]-i[t-v+R];f[M]=T,T>w&&(w=T)}let y=0;for(let M=0;M<E;M++)f[M]=Math.exp(f[M]-w),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+v-N-M]=m[M];else if(o==="greater")for(let M=0;M<E;M++)a[(N+M)*c+v-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]*im,A=-1,k=M;for(;k-A>1;){let F=A+k>>1;f[F]<=T?A=F:k=F}let C=M,_=E;for(;_-C>1;){let F=C+_>>1;f[F]<=T?_=F:C=F}let I=f[M]<=T?1:(A>=0?m[A]:0)+(_<E?u[_]:0);a[(N+R)*c+v-N-R]=Math.min(1,I)}}}let h=a[n*c+r]*im,p=new Int32Array(e+2),g=[],d=[];for(let v=0;v<=e;v++){p[v]=g.length;let N=0;for(;N<=t;){if(a[v*c+N]<=h){let b=N;for(;N+1<=t&&a[v*c+N+1]<=h;)N++;g.push(b),d.push(N)}N++}}return p[e+1]=g.length,{starts:p,from:Int32Array.from(g),to:Int32Array.from(d)}}function cm(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]=pn(r,e,t,!1);let o=Ot(1,-t),s=ot([t,0],o),i=ot(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 j0(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=b=>{let[E,w]=cm(t,b,c),[y,x]=cm(n,b,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<=w;T++){let A=c[T];if(A===0)continue;let k=0;for(let C=o[T];C<o[T+1];C++){let _=s[C],I=i[C];if(!(I<y||_>x))if(_===0)k+=l[Math.min(I,x)];else if(I===n)k+=f[Math.max(_,y)];else for(let F=Math.max(_,y);F<=Math.min(I,x);F++)k+=a[F]}R+=A*k}return Math.min(1,R)},u=Math.PI/2,h=b=>b<=0?0:b>=u?1:Math.sin(b)**2,p=new Float64Array(r+2),g=0;for(let b=0;b<=r+1;b++)p[b]=m(h(u*b/(r+1))),p[b]>p[g]&&(g=b);let d=p[g],v=h(u*g/(r+1));if(d>=1)return{value:1,at:v};let N=D0*d;for(let b=0;b<=r+1;b++){let E=p[b];if(E<N||b>0&&!(E>p[b-1])||b<=r&&!(E>=p[b+1]))continue;let w=u*Math.max(0,b-1)/(r+1),y=u*Math.min(r+1,b+1)/(r+1),M=Cl(A=>-m(h(A)),w,y,1e-14).x,R=m(h(M)),T=1e-4/Math.sqrt(2*(t+n));for(let A=0;A<2&&(M-T>w&&M+T<y);A++,T*=.01){let k=m(h(M-T)),C=m(h(M+T)),_=k-2*R+C;if(!(_<0))break;let I=T*(k-C)/(2*_);if(!(Math.abs(I)<T))break;let F=m(h(M+I));F>R*(1+2**-50)&&(M+=I,R=F)}R>d&&(d=R,v=h(M))}return{value:d,at:v}}var lm=["correct"];function K0(e,t,n={}){ct(t)&&B(t,lm,"mcnemarTest"),B(n,lm,"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=B0(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=fm(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+=fm(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:bt(i,a/2,2,!1,!1),warnings:[]}}function fm(e,t,n){let r=t+n;return r<1/0?Vt(e,r):Vt($(e,-520),$(t,-1040)+$(n,-1040))}function B0(e,t){if(e.length!==t.length)throw new RangeError("'x' and 'y' must have the same length");let n=[],r=nn(e),o=nn(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 W0=["ratio","alternative","confLevel"],Fi=2**-1022;function mm(e,t,n={}){if(B(n,W0,"varTest"),typeof e=="string"){let b=tr(e,t);return mm(b.x,b.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=Je(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]=Tt([r,o]).variance,u=0;if(!(f>=Fi&&m>=Fi)){let b=Tt([r]),E=Tt([o]);f=b.variance[0],m=E.variance[0],u=2*(b.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,g=ht(p,a,l,!0,!1),d=ht(p,a,l,!1,!1),v,N;if(i==="less")v=g,N=[0,h/hn(1-c,a,l,!0,!1)];else if(i==="greater")v=d,N=[h/hn(c,a,l,!0,!1),1/0];else{v=Math.min(1,2*Math.min(g,d));let b=(1-c)/2,E=b<1e-8?hn(b,a,l,!1,!1):hn(1-b,a,l,!0,!1);N=[h/E,h/hn(b,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:v,confInt:N,confLevel:c,estimate:h,estimateNames:"ratio of variances",nullValue:s,nullValueName:"ratio of variances",warnings:[]}}function um(e,t){if(typeof e=="string"){let p=Ao(e,t);return um(_l(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=Tt(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<Fi)){let p=o.map(E=>{let w=Tt([E]);return{w:w.variance[0],e:w.k}}),g=Math.max(...p.map(E=>E.e)),d=p.map(E=>Math.log(E.w)+2*(E.e-g)*Math.LN2),v=d.map((E,w)=>Math.log(s[w])+E),N=Math.max(...v),b=N+Math.log(v.reduce((E,w)=>E+Math.exp(w-N),0))-Math.log(c);m=c*b-d.reduce((E,w,y)=>E+s[y]*w,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:bt(u,h/2,2,!1,!1),warnings:[]}}var U0=["alternative"],Oi=["alternative"];function Di(e,t,n,r){return{method:e,statistic:n,statisticName:t,parameter:r,parameterName:"df",pValue:bt(n,r/2,2,!1,!1),warnings:[]}}function hm(e){let t=0;for(let n of bn(e))t+=n*n*n-n;return t}function Qs(e,t){if(typeof e=="string"){let l=Ao(e,t);return Qs(l.values,l.group.codes)}let n=Nr(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=vt(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-hm(o)/(s*s*s-s)),Di("Kruskal-Wallis rank sum test","Kruskal-Wallis chi-squared",c,r-1)}function Pi(e,t){if(typeof e=="string")return Pi(xs(e,t));let n=Pa(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=vt(l);for(let m=0;m<o;m++)s[m]+=f[m];i+=hm(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 Di("Friedman rank sum test","Friedman chi-squared",a,o-1)}function H0(e,t={},n,r={}){typeof e!="string"&&ct(t)&&B(t,Oi,"pageTest"),ct(n)&&B(n,Oi,"pageTest"),B(r,Oi,"pageTest");let o,s;if(typeof e=="string")o=xs(e,t),s=n??{};else if(Array.isArray(n)){let g=Array.from(e),d=t,v=n;if(g.length!==d.length||g.length!==v.length)throw new RangeError("'x', 'groups' and 'blocks' must have the same length");if(!g.every(Number.isFinite))throw new RangeError("missing or non-finite values are not allowed");let N=nn(d),b=nn(v);if(N.some(M=>typeof M=="number"&&Number.isNaN(M))||b.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),w=Array.from(new Set(b)).sort(zn);if(E.length<2||w.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(w.map((M,R)=>[M,R]));o=w.map(()=>new Array(E.length).fill(NaN));for(let M=0;M<g.length;M++){let R=x.get(b[M]),T=y.get(N[M]);if(!Number.isNaN(o[R][T]))throw new RangeError("not an unreplicated complete block design");o[R][T]=g[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(g=>Array.from(g)),s=t;let i=o.length,c=o[0]?.length??0;if(i<1||c<2||o.some(g=>g.length!==c))throw new RangeError("'y' must be a non-empty matrix with at least two columns");if(o.some(g=>g.some(d=>!Number.isFinite(d))))throw new RangeError("missing or non-finite values are not allowed in a block");let a=Je(s.alternative),l=new Array(c).fill(0);for(let g of o){let d=vt(g);for(let v=0;v<c;v++)l[v]+=d[v]}let f=0;for(let g=0;g<c;g++)f+=(g+1)*l[g];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"?ae(h):a==="greater"?ae(h,{lowerTail:!1}):2*ae(-Math.abs(h));return{method:"Page's ordered aligned rank sum test",alternative:a,statistic:h,statisticName:"z",pValue:p,estimate:f,warnings:[]}}function Y0(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{...Pi(e),method:"Cochran's Q test",statisticName:"Q"}}function pm(e,t){if(typeof e=="string")return pm(xs(e,t));let n=Pa(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(g=>yt(g)-cn(g));s.some(g=>g===1/0)&&n.every(g=>g.every(Number.isFinite))&&(s=Ie(n).groups.map(g=>yt(g)-cn(g)));let i=vt(s),c=new Array(o).fill(0),a=0;n.forEach((g,d)=>{let v=vt(g);for(let N=0;N<o;N++){let b=i[d]*(v[N]-(o+1)/2);a+=b*b,c[N]+=b}});let l=0;for(let g of c)l+=g*g;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 g=0;for(let d=2;d<=o;d++)g+=Math.log(d);h=Math.exp((1-r)*g)}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 dm(e,t){if(typeof e=="string"){let h=Ao(e,t);return dm(h.values,h.group.codes)}let n=Nr(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 g=ks(p);return p.map(d=>Math.abs(d-g))}),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(Ie(n).groups));let i=s.length,a=vt(s).map(h=>ie((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 g=0;g<h.length;g++)p+=a[u+g];u+=h.length,m+=h.length*(p/h.length-f)**2}return m/=at(a),Di("Fligner-Killeen test of homogeneity of variances","Fligner-Killeen:med chi-squared",m,r-1)}function gm(e,t,n={}){if(B(n,U0,"moodTest"),typeof e=="string"){let E=tr(e,t);return gm(E.x,E.y,n)}let r=Array.from(e).filter(Number.isFinite),o=Array.from(t).filter(Number.isFinite),s=Je(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,w)=>w).sort((E,w)=>l[E]-l[w]),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 w=0;for(let y=u;y<=E;y++)w+=(y+1-(a+1)/2)**2;w/=E-u+1;for(let y=u;y<=E;y++)m[f[y]]=w;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 g=0;for(let E of m)g+=(E-p)**2;let d=i*p,v=i*c/(a*(a-1))*g,N=(h-d)/Math.sqrt(v),b=s==="less"?ae(N):s==="greater"?ae(N,{lowerTail:!1}):2*ae(-Math.abs(N));return{method:"Mood two-sample test of scale",alternative:s,statistic:N,statisticName:"Z",pValue:b,warnings:[]}}function Tr(e,t){let n=0;for(let r=e.length-1;r>=0;r--)n=n*t+e[r];return n}var G0=[0,.221157,-.147981,-2.07119,4.434685,-2.706056],X0=[0,.042981,-.293762,-1.752461,5.682633,-3.582633],Q0=[.544,-.39978,.025054,-6714e-7],Z0=[1.3822,-.77857,.062767,-.0020322],J0=[-1.5861,-.31082,-.083751,.0038915],eg=[-.4803,-.082676,.0030302],tg=[-2.273,.459];function ng(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]=-ie((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+Tr(G0,i),a=new Float64Array(n);a[0]=c;let l,f;if(e>5){let m=r[1]/s+Tr(X0,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 wm(e){let t=Ie([e]).groups[0],n=t.length,r=ng(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 rg=1.90985931710274,og=1.0471975511966;function bm(e,t){if(Number.isNaN(e)||!(t>=3))return NaN;if(t===3){let i=rg*(Math.asin(Math.sqrt(1-e))-og);return Math.max(i,0)}let n=Math.log(e);if(t<=11){let i=Tr(tg,t);if(n>=i)return 1e-99;let c=Tr(Q0,t),a=Math.exp(Tr(Z0,t));return ae(-Math.log(i-n),{mean:c,sd:a,lowerTail:!1})}let r=Math.log(t),o=Tr(J0,r),s=Math.exp(Tr(eg,r));return ae(n,{mean:o,sd:s,lowerTail:!1})}var Mm=Math.log(2*Math.PI),vm=2e-14,Nm=500;function sg(e){return Math.exp(-.5*e*e-.5*Mm)}function ag(e){return ae(e,{logP:!0})}function Em(e,t=-1/0,n=!1){let r=t===-1/0?Oa(e,0,2,0,vm,Nm):Oa(e,t,1,0,vm,Nm),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 ym(e,t,n){let r=t.map(Math.sqrt),o=t.map(i=>Math.sqrt(1-i));return Em(i=>{let c=0;for(let a=0;a<r.length;a++){let l=(r[a]*i+e)/o[a];if(!n){c+=ag(l);continue}let f=(r[a]*i-e)/o[a],m=ae(f)+ae(l,{lowerTail:!1});m<.5?c+=Math.log1p(-m):c+=Math.log(Math.max(0,l+f<0?ae(l)-ae(f):ae(f,{lowerTail:!1})-ae(l,{lowerTail:!1})))}return sg(i)*-Math.expm1(c)})}function Ar(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,ym(e,t,r));let o=n/2,s=Math.log(2)+.5*Math.log(o)-.5*Mm-cl(o);return Math.min(1,Em(c=>{let a=s-o*(Math.expm1(2*c)-2*c);return a<-745?0:Math.exp(a)*ym(e*Math.exp(c),t,r)},-1/0,!0))}var ig=["pAdjustMethod"],cg=["pAdjustMethod","alternative"],lg=["alternative"],fg=["pAdjustMethod","alternative"],mg=["method","altp"],ug=["alternative"],hg=["welch"],xm="Ties are present. z-quantiles were corrected for ties.";function kn(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 Vi(e){let t=e.groups.flat(),n=vt(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 bn(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 pg(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=Xr(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 Ko(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 $i(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 Zs=e=>ae(e,{lowerTail:!1}),zi=(e,t,n)=>bs(Math.abs(e),t,n,{lowerTail:!1});function ji(e){let t=Tt(e.groups),n=0;for(let r of t.n)r>1&&(n+=r-1);return Bo(n,t.ssWithin),{m:t.mean,s2:t.ssWithin/n,df:e.N-e.k,k:t.k}}function Bo(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 dg(e,t,n={}){B(n,ig,"kwAllPairsDunnTest");let r=n.pAdjustMethod??"holm",o=kn(e,t),{R:s,C:i}=Vi(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=pg(a.map(f=>f.map(m=>2*Zs(m))),r);return J("kwAllPairsDunnTest",Ko(o,"Dunn's all-pairs test",a,l,r,i!==0?[xm]:[]))}function gg(e,t,n={}){B(n,cg,"kwManyOneDunnTest");let r=Je(n.alternative),o=n.pAdjustMethod??"single-step",s=kn(e,t),{R:i,C:c}=Vi(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=Rm(f,m,r,o);return J("kwManyOneDunnTest",$i(s,"Dunn's many-to-one test",f,u,o,c!==0?[xm]:[]))}function Rm(e,t,n,r){return r==="single-step"?e.map(o=>n==="two.sided"?Ar(o,t,1/0,!0):Ar(n==="greater"?o:-o,t,1/0,!1)):Xr(e.map(o=>n==="two.sided"?2*Zs(Math.abs(o)):Zs(n==="greater"?o:-o)),r)}function wg(e,t,n={}){B(n,lg,"dunnettTest");let r=Je(n.alternative),o=kn(e,t),{m:s,s2:i,df:c}=ji(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*Ar(Math.abs(h),m,c,!1)):Ar(r==="greater"?h:-h,m,c,!1));return J("dunnettTest",$i(o,`Dunnett's-test for multiple
4
4
  comparisons with one control`,f,u,"single-step"))}function bg(e,t,n={}){B(n,fg,"manyOneUTest");let r=Je(n.alternative),o=n.pAdjustMethod??"single-step",s=kn(e,t);if(o==="single-step"&&s.k<3)throw new RangeError(`for 'p.adjust.method = single-step' at least
5
5