@numero-uno-jpn/rstats 1.17.0 → 1.18.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 (91) hide show
  1. package/CHANGELOG.md +25 -0
  2. package/README.ja.md +6 -6
  3. package/README.md +6 -6
  4. package/dist/causal/mediationProcedures.js +18 -1
  5. package/dist/causal/moderation.js +11 -2
  6. package/dist/causal.js +2 -2
  7. package/dist/chunks/{chunk-NPWWGIFG.js → chunk-2JB7F646.js} +2 -2
  8. package/dist/chunks/{chunk-JG4TWDQ4.js → chunk-3JFRDXN7.js} +2 -2
  9. package/dist/chunks/chunk-53WQDB3C.js +2 -0
  10. package/dist/chunks/{chunk-QCPGV7XH.js → chunk-5CTWYFMB.js} +2 -2
  11. package/dist/chunks/chunk-6I23C2YJ.js +2 -0
  12. package/dist/chunks/{chunk-6MLDKHTY.js → chunk-CTZUYKVL.js} +2 -2
  13. package/dist/chunks/{chunk-GOYTZ5RB.js → chunk-EC77KAAM.js} +2 -2
  14. package/dist/chunks/{chunk-VZSQAGL2.js → chunk-FLFNMSHI.js} +2 -2
  15. package/dist/chunks/{chunk-XEJX5HBJ.js → chunk-GEHMND3C.js} +2 -2
  16. package/dist/chunks/{chunk-KY53MB3J.js → chunk-GIK34P23.js} +2 -2
  17. package/dist/chunks/chunk-H6Z7JVSD.js +2 -0
  18. package/dist/chunks/{chunk-YMI33F7W.js → chunk-HG4LJBNV.js} +2 -2
  19. package/dist/chunks/chunk-HGAEVPVS.js +2 -0
  20. package/dist/chunks/{chunk-EIUK6TKF.js → chunk-HMLYEFQN.js} +2 -2
  21. package/dist/chunks/chunk-HRRCC2BD.js +2 -0
  22. package/dist/chunks/chunk-IBHMM2SL.js +2 -0
  23. package/dist/chunks/chunk-ICDW3I2F.js +2 -0
  24. package/dist/chunks/chunk-IKMW63BE.js +2 -0
  25. package/dist/chunks/{chunk-O4D43LWM.js → chunk-IOJVFYHW.js} +2 -2
  26. package/dist/chunks/chunk-IW4S634O.js +2 -0
  27. package/dist/chunks/{chunk-GIIURG5X.js → chunk-JF4PXFJE.js} +2 -2
  28. package/dist/chunks/chunk-JPWYGTUN.js +2 -0
  29. package/dist/chunks/{chunk-I2KJMF4G.js → chunk-KJMLQHCY.js} +1 -1
  30. package/dist/chunks/chunk-KVJBCT3D.js +2 -0
  31. package/dist/chunks/chunk-LCIX76FR.js +2 -0
  32. package/dist/chunks/chunk-LY5XGFPU.js +2 -0
  33. package/dist/chunks/{chunk-NCDPZSI2.js → chunk-MKZH65DK.js} +2 -2
  34. package/dist/chunks/{chunk-GTSK4IN4.js → chunk-MVPHISOV.js} +1 -1
  35. package/dist/chunks/chunk-MXZ64QME.js +2 -0
  36. package/dist/chunks/{chunk-NCRL33PQ.js → chunk-N7BUE54L.js} +2 -2
  37. package/dist/chunks/{chunk-L77QFL3C.js → chunk-NDZVVNBB.js} +2 -2
  38. package/dist/chunks/{chunk-EB45CDFA.js → chunk-NOTJ45G3.js} +2 -2
  39. package/dist/chunks/{chunk-ZRKXHVTJ.js → chunk-QUD3374N.js} +2 -2
  40. package/dist/chunks/{chunk-XR7Y4ZV6.js → chunk-RXYZQ3UX.js} +2 -2
  41. package/dist/chunks/{chunk-V54MBEJM.js → chunk-SCFFHEDZ.js} +2 -2
  42. package/dist/chunks/{chunk-RNGDVSTS.js → chunk-V6FMGGUV.js} +2 -2
  43. package/dist/chunks/{chunk-TTOJDLIQ.js → chunk-VATAUY7N.js} +2 -2
  44. package/dist/chunks/chunk-W5M3R4NH.js +2 -0
  45. package/dist/chunks/{chunk-DD2GG4EC.js → chunk-WYVUCQO3.js} +1 -1
  46. package/dist/chunks/{chunk-JHABMYNH.js → chunk-XQCHC3SH.js} +1 -1
  47. package/dist/chunks/chunk-XUMVTGMM.js +2 -0
  48. package/dist/chunks/{chunk-72I7GDWL.js → chunk-YXMHWS5R.js} +2 -2
  49. package/dist/chunks/{chunk-LKKUUREA.js → chunk-Z33Q545U.js} +2 -2
  50. package/dist/descriptive/qcc.d.ts +320 -0
  51. package/dist/descriptive/qcc.js +524 -0
  52. package/dist/descriptive/qccTools.d.ts +301 -0
  53. package/dist/descriptive/qccTools.js +390 -0
  54. package/dist/descriptive.js +2 -2
  55. package/dist/distributions.js +2 -2
  56. package/dist/entries/descriptive.d.ts +2 -0
  57. package/dist/entries/descriptive.js +2 -0
  58. package/dist/entries/regression.d.ts +1 -0
  59. package/dist/entries/regression.js +2 -0
  60. package/dist/linalg.js +2 -2
  61. package/dist/mixed.js +2 -2
  62. package/dist/models/betareg.js +14 -2
  63. package/dist/models/bradleyterry.d.ts +188 -0
  64. package/dist/models/bradleyterry.js +533 -0
  65. package/dist/models/survreg.d.ts +5 -0
  66. package/dist/models/survreg.js +23 -3
  67. package/dist/models/tobit.js +16 -5
  68. package/dist/multivariate.js +2 -2
  69. package/dist/numerics.js +2 -2
  70. package/dist/regression.js +3 -3
  71. package/dist/rstats.js +17 -17
  72. package/dist/special.js +2 -2
  73. package/dist/tests.js +2 -2
  74. package/dist/timeseries.js +2 -2
  75. package/package.json +1 -1
  76. package/dist/chunks/chunk-23QCIPET.js +0 -2
  77. package/dist/chunks/chunk-3UEYA6VG.js +0 -2
  78. package/dist/chunks/chunk-3Y4DNK4O.js +0 -2
  79. package/dist/chunks/chunk-4BGGWQWS.js +0 -2
  80. package/dist/chunks/chunk-5UFVACSV.js +0 -2
  81. package/dist/chunks/chunk-5W2OZAHQ.js +0 -2
  82. package/dist/chunks/chunk-BWDXJL7T.js +0 -2
  83. package/dist/chunks/chunk-GLABB425.js +0 -2
  84. package/dist/chunks/chunk-JV3POOET.js +0 -2
  85. package/dist/chunks/chunk-K3YSXDTU.js +0 -2
  86. package/dist/chunks/chunk-LY2UZGUL.js +0 -2
  87. package/dist/chunks/chunk-PSQ6MLJM.js +0 -2
  88. package/dist/chunks/chunk-RM5D7Q7W.js +0 -2
  89. package/dist/chunks/chunk-SA2E5GVO.js +0 -2
  90. package/dist/chunks/chunk-T5ZALLTJ.js +0 -2
  91. package/dist/chunks/chunk-XPYZOG5H.js +0 -2
package/dist/tests.js CHANGED
@@ -1,5 +1,5 @@
1
- /*! rStats.js 1.17.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 en,b as Vn,c as Zn,d as dr,e as ws,f as va}from"./chunks/chunk-QCPGV7XH.js";import{a as xe,b as qe,c as bn,d as it,e as No,f as bh,g as vh,h as wa,i as ba,j as Nh,k as il,l as vs,m as Jn,n as Ns,o as fl,p as wr,q as ml,r as Mh,s as Eh,t as Ma,u as Ea,v as pl,w as dl,x as gl,y as xa,z as kh}from"./chunks/chunk-EIUK6TKF.js";import{a as gn,c as on,h as $r,i as jc,m as Kc,n as zr,o as Wc,s as as,t as Yc,u as go,v as is,w as cs,x as ls}from"./chunks/chunk-T5ZALLTJ.js";import{b as Qc}from"./chunks/chunk-GOYTZ5RB.js";import{a as Ra,e as Ta}from"./chunks/chunk-L77QFL3C.js";import{a as ms,b as Qn,c as vo,d as el,e as tl,f as us,g as Ge,h as an,i as wn,j as It,k as ol,l as yh,n as Ms,o as Mo}from"./chunks/chunk-NCDPZSI2.js";import{a as Es}from"./chunks/chunk-TTOJDLIQ.js";import"./chunks/chunk-V54MBEJM.js";import"./chunks/chunk-ZRKXHVTJ.js";import{a as zc,d as ss}from"./chunks/chunk-3UEYA6VG.js";import{b as un,c as hl}from"./chunks/chunk-3Y4DNK4O.js";import{a as qh,b as Ih,c as _h}from"./chunks/chunk-PSQ6MLJM.js";import{f as ll,g as br,h as er,i as ys,k as yo,l as ul,m as ya,n as xh,o as Rh,q as Th,r as Sh,s as Ah}from"./chunks/chunk-O4D43LWM.js";import{b as Mt,c as Cn}from"./chunks/chunk-5W2OZAHQ.js";import{b as Eo}from"./chunks/chunk-VZSQAGL2.js";import{a as nl,c as rl,e as pr}from"./chunks/chunk-LKKUUREA.js";import{b as ga}from"./chunks/chunk-DD2GG4EC.js";import{a as at,b as hs,c as ps}from"./chunks/chunk-6MLDKHTY.js";import{c as gs,d as Na}from"./chunks/chunk-EB45CDFA.js";import{a as gr,b as Kr}from"./chunks/chunk-XR7Y4ZV6.js";import{d as Zo,e as Jo,g as es,h as Pr,m as fn,n as On}from"./chunks/chunk-4BGGWQWS.js";import{d as wt,e as ln,g as xn,h as Vr,k as Yn}from"./chunks/chunk-SA2E5GVO.js";import{d as rt,h as cl}from"./chunks/chunk-XPYZOG5H.js";import{c as sl,j as jt,m as al,n as ds,t as bs}from"./chunks/chunk-K3YSXDTU.js";import{a as Xn,b as Jc,g as jr}from"./chunks/chunk-KY53MB3J.js";import{a as da,b as vt,c as xt,e as dn,f as rn}from"./chunks/chunk-BWDXJL7T.js";import{i as gt,l as Qo,t as Pc}from"./chunks/chunk-23QCIPET.js";import{a as Rn,c as fs}from"./chunks/chunk-RM5D7Q7W.js";import{b as Bc,c as Uc,d as Gc,e as Hc,f as Xc,i as wo,l as wh,n as H,o as je,p as mt}from"./chunks/chunk-NCRL33PQ.js";import{a as bo,b as Pn,c as Zc,d as Ot,e as Rt}from"./chunks/chunk-JHABMYNH.js";import{a as At,b as Hn,c as sn}from"./chunks/chunk-I2KJMF4G.js";import"./chunks/chunk-72I7GDWL.js";import{b as Oc,c as Fc}from"./chunks/chunk-5UFVACSV.js";import{h as _c,i as Lc,k as Cc,t as Dc}from"./chunks/chunk-LY2UZGUL.js";import{f as ha,r as Xt,s as ie,t as oe}from"./chunks/chunk-XEJX5HBJ.js";import{A as Dt,B as os,C as Re,e as Fn,f as mn,g as Dn,h as $,i as et,j as Bn,k as st,l as ot,m as Un,n as Vc,p as ts,q as Et,r as ns,s as $c,t as rs,u as pa,v as Ie,w as bt,x as pn,y as Ct,z as Gn}from"./chunks/chunk-YMI33F7W.js";import{b as $t,c as Ht,d as ue,e as qc,f as Ce,g as _e,h as zt,i as lt,j as ft,k as Ic,l as V,m as Ft}from"./chunks/chunk-GTSK4IN4.js";var wl=["alternative","mu","paired","varEqual","confLevel"];function vn(e,t,n={}){if(typeof e!="string"&&at(t)&&V(t,wl,"tTest"),V(n,wl,"tTest"),typeof e=="string")return Lh(e,t,n);let r;t!==void 0&&!ArrayBuffer.isView(t)&&!Array.isArray(t)?n=t:r=t;let o=Ge(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(an(a),i&&r===void 0)throw new TypeError("y is required when paired is true");let l,f,m;if(i){let[k,L]=Gn(Array.from(e),Array.from(r));({d:l,k:m}=ns(k,L))}else{let k=Re(r===void 0?[Ct(Array.from(e))]:[Ct(Array.from(e)),Ct(Array.from(r))]);m=k.k,l=k.groups[0],f=k.groups[1]}let u=$(s,-m),h,p,d,g,w,N,v,E=l.length,b=f!==void 0&&c;if(E<1||!b&&E<2)throw new Error("not enough 'x' observations");let M=st(l),x=E>1?ot(l):NaN;if(f===void 0){if(p=E-1,d=Math.sqrt(x/E),d===0||d<10*Number.EPSILON*Math.abs(M))throw new Error("data are essentially constant");h=(M-u)/d,N=i?"Paired t-test":"One Sample t-test",g=M,w=i?"mean difference":"mean of x",v=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 L=st(f),_=k>1?ot(f):NaN;if(N=`${c?"":"Welch"} Two Sample t-test`,g=[M,L],w=["mean of x","mean of y"],v="difference in means",c){p=E+k-2;let I=0;E>1&&(I+=(E-1)*x),k>1&&(I+=(k-1)*_),I/=p,d=Math.sqrt(I*(1/E+1/k))}else{let I=x/E,F=_/k;d=Math.sqrt(Math.sqrt(I)**2+Math.sqrt(F)**2),p=rs([I,F],[E-1,k-1])}if(d===0||d<10*Number.EPSILON*Math.max(Math.abs(M),Math.abs(L)))throw new Error("data are essentially constant");h=(M-L-u)/d}let y=typeof g=="number"?g:g[0]-g[1],R=(k,L)=>{let _=u+(h+L*k)*d;return Number.isFinite(_)&&Math.max(Math.abs(u),Math.abs(h*d))<=16*(Math.abs(y)+k*d)?_:y+L*k*d},T,S;if(o==="less")T=vt(h,p,!0,!1),S=[-1/0,R(xt(a,p,!0,!1),1)];else if(o==="greater")T=vt(h,p,!1,!1),S=[R(xt(a,p,!0,!1),-1),1/0];else{T=2*vt(-Math.abs(h),p,!0,!1);let k=1-a,L=k/2<1e-8?xt(k/2,p,!1,!1):xt(1-k/2,p,!0,!1);S=[R(L,-1),R(L,1)]}return S=[$(S[0],m),$(S[1],m)],g=typeof g=="number"?$(g,m):g.map(k=>$(k,m)),d=$(d,m),{method:N,alternative:o,statistic:h,statisticName:"t",parameter:p,parameterName:"df",pValue:T,confInt:S,confLevel:a,estimate:g,estimateNames:w,nullValue:s,nullValueName:v,stdErr:d,stdError:d,warnings:[]}}function Lh(e,t,n){if(n.paired!==void 0)throw new Error("cannot use 'paired' in formula method");let r=ms(e,t,!0);if(r.kind==="one")return vn(r.x,n);if(r.kind==="paired")return vn(r.x,r.y,{...n,paired:!0});let[o,s]=r.levels;return{...vn(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 Ch=["correct","p","rescaleP"],Oh=["confLevel"],Fh=["alternative","rowScores","colScores"];function Qt(e){let t=[0,0];for(let n of e)t=ue(t,[n,0]);return t}function Aa(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 ka=e=>e.length>0&&typeof e[0]!="number",qa=(e,t)=>Number.isNaN(e)?NaN:gt(e,t/2,2,!1,!1);function bl(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 xo(e,t){return e.length>0&&Array.isArray(e[0])?e.map((n,r)=>n.map((o,s)=>bl(o,t[r][s]))):e.map((n,r)=>bl(n,t[r]))}var Dh=["none","williams","yates"],Nl="Log likelihood ratio (G-test)";function Ph(e,t={}){V(t,Ch,"gTest");let n=xe(t.correct,Dh);if(typeof e!="object"||e===null||typeof e.length!="number")throw new TypeError("'x' must be an array of counts or of rows");if(ka(e)){let o=Aa(e);return vl(o.flat()),o.length===1?H("gTest",Sa(o[0],n,t)):o[0].length===1?H("gTest",Sa(o.map(s=>s[0]),n,t)):H("gTest",Vh(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 vl(r),H("gTest",Sa(r,n,t))}function vl(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 yl(e,t){return e<1/0?0:Math.ceil(Math.log2(t))+1}function Ml(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=ue(n,Lc(o,s))}return n[0]+n[1]}function Vh(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=Qt(e.flat()),s=yl(o[0],n*r),i=s===0?e:e.map(v=>v.map(E=>$(E,-s))),c=s===0?o:Qt(i.flat()),a=i.map(v=>Qt(v)),l=i[0].map((v,E)=>Qt(i.map(b=>b[E]))),f=a.map(v=>l.map(E=>_e(v,lt(E,c)))),m=i;if(t==="yates"){let[[v,E],[b,M]]=i,x=v*M-E*b>0?-.5:.5,y=$(.5,-s),R=x<0?-y:y;m=[[v+R,E-R],[b-R,M+R]]}let u=Ml(m.flat(),f.flat()),h=Number.isNaN(u)?["NaNs produced"]:[],p=1;if(t==="williams"){if(a.some(M=>M[0]===0)||l.some(M=>M[0]===0))throw new RangeError("Williams' correction is not defined for a table with an empty row or column");let v=c[0]+c[1],E=a.reduce((M,x)=>M+1/(x[0]+x[1]),0),b=l.reduce((M,x)=>M+1/(x[0]+x[1]),0);p=1+$((v*E-1)*(v*b-1)/(6*v*(n-1)*(r-1)),-s)}let d=$(2*u,s)/p,g=(n-1)*(r-1),w=f.map(v=>v.map(E=>$(E[0]+E[1],s)));return{method:`${Nl} test of independence ${t==="none"?"without correction":t==="williams"?"with Williams' correction":"with Yates' correction"}`,statistic:d,statisticName:"G",parameter:g,parameterName:"X-squared df",pValue:qa(d,g),observed:s===0?m:m.map(v=>v.map(E=>$(E,s))),expected:w,ftResiduals:xo(e,w),warnings:h}}function Sa(e,t,n){let r=e.length;if(r<2)throw new RangeError("'x' must have at least 2 elements");if(t==="yates"&&r!==2)throw new RangeError("Yates' correction requires 2 data values");let o=n.rescaleP??!1;if(typeof o!="boolean")throw new TypeError("'rescaleP' must be a boolean");let s=n.p===void 0?e.map(()=>1/r):Array.from(n.p);if(s.length!==r)throw new RangeError("'x' and 'p' must have the same number of elements");if(s.some(v=>typeof v!="number"||Number.isNaN(v)))throw new RangeError("'p' must not contain missing values");if(s.some(v=>v<0))throw new RangeError("probabilities must be non-negative");if(s.some(v=>v===1/0))throw new RangeError("probabilities must be finite");let i=Qt(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=Qt(e),a=yl(c[0],r),l=a===0?e:e.map(v=>$(v,-a)),f=a===0?c:Qt(l),m=s.map(v=>o?lt(zt(f,v),i):zt(f,v)),u=m.map(v=>v[0]+v[1]),h=l;if(t==="yates"){let v=$(.5,-a),E=l[0]-u[0];E>$(.25,-a)?h=[l[0]-v,l[1]+v]:-E>$(.25,-a)&&(h=[l[0]+v,l[1]-v])}let p=Ml(h,m);if(!o&&!Number.isNaN(p)){let v=f[0]+f[1];p-=v*(i[0]-1+i[1])}let d=Number.isNaN(p)?["NaNs produced"]:[],g=t==="williams"?1+(r+1)/(6*$(f[0]+f[1],a)):1,w=$(2*p,a)/g,N=u.map(v=>$(v,a));return{method:`${Nl} goodness of fit test`,statistic:w,statisticName:"G",parameter:r-1,parameterName:"X-squared df",pValue:qa(w,r-1),observed:a===0?h:h.map(v=>$(v,a)),expected:N,ftResiduals:xo(e,N),warnings:d}}function $h(e){if(typeof e!="object"||e===null||!ka(e))throw new RangeError("'x' must be square with at least two rows and columns");let t=Aa(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 zh(e){let t=$h(e),n=Qt(t.flat())[0],r=n>2**400||n>0&&n<2**-400?Math.round(Math.log2(n)):0,o=r===0?t:t.map(b=>b.map(M=>$(M,-r))),s=o.length,i=o.map(b=>Qt(b)),c=o.map((b,M)=>Qt(o.map(x=>x[M]))),a=o.map((b,M)=>o[M].some((x,y)=>y!==M&&x>0)||o.some((x,y)=>y!==M&&x[M]>0)).flatMap((b,M)=>b?[M]:[]),l=a.length;if(l<2)throw new RangeError("Too many equal marginals, cannot compute");let f=a.map((b,M)=>M),m=b=>f[b]===b?b:f[b]=m(f[b]);for(let b=0;b<l;b++)for(let M=b+1;M<l;M++)o[a[b]][a[M]]+o[a[M]][a[b]]>0&&(f[m(b)]=m(M));if(a.some((b,M)=>m(M)!==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(b=>{let M=ue(i[b],[-c[b][0],-c[b][1]]);return M[0]+M[1]}),p=a.slice(0,u).map(b=>a.slice(0,u).map(M=>{if(b!==M)return-(o[b][M]+o[M][b]);let x=0;for(let y=0;y<s;y++)y!==b&&(x+=o[b][y]+o[y][b]);return x})),d=p.map(b=>b.map(()=>0)),g=[];for(let b=0;b<u;b++){let M=p[b][b];for(let x=0;x<b;x++)M-=d[b][x]*d[b][x]*g[x];g.push(M);for(let x=b+1;x<u;x++){let y=p[x][b];for(let R=0;R<b;R++)y-=d[x][R]*d[b][R]*g[R];d[x][b]=y/M}}let w=0,N=[];for(let b=0;b<u;b++){let M=h[b];for(let x=0;x<b;x++)M-=d[b][x]*N[x];N.push(M),w+=M*M/g[b]}let v=Qt(a.flatMap(b=>a.map(M=>t[b][M]))),E=l-1;return w=$(w,r),H("stuartMaxwellTest",{method:"Stuart-Maxwell test",statistic:w,statisticName:"chi-squared",parameter:E,parameterName:"df",pValue:qa(w,E),N:v[0]+v[1],warnings:[]})}function jh(e,t={}){V(t,Oh,"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=El(e),o=Qt(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(b=>b.map(M=>$(M,-s))),{pc:c,pd:a}=Kh(i),l=[0,0],f=[0,0];i.forEach((b,M)=>b.forEach((x,y)=>{l=ue(l,zt(c[M][y],x)),f=ue(f,zt(a[M][y],x))})),l=[l[0]/2,l[1]/2],f=[f[0]/2,f[1]/2];let m=ue(l,f),u=lt(Ce(l,f),m)[0],h=_e(m,m),p=i.map((b,M)=>b.map((x,y)=>2*lt(Ce(_e(f,c[M][y]),_e(l,a[M][y])),h)[0])),d=Math.max(...p.flat().map(Math.abs)),g=d>0&&d<1/0?Math.floor(Math.log2(d)):0,w=0;i.forEach((b,M)=>b.forEach((x,y)=>{let R=$(p[M][y],-g);w+=x*R*R}));let N=$(Math.sqrt(w),g-s/2),v=(1-n)/2,E=v<1e-8?oe(v,{lowerTail:!1}):oe(1-v);return H("goodmanKruskalGamma",{estimate:u,confInt:[Math.max(-1,u-E*N),Math.min(1,u+E*N)],confLevel:n,ase:N,warnings:[]})}function El(e){if(typeof e!="object"||e===null||!ka(e))throw new TypeError("'table' must be an array of rows");let t=Aa(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 Kh(e){let t=e.length,n=e[0].length,r=[0,0],o=e.map(p=>{let d=[],g=r;for(let w=0;w<n;w++)d.push(g),g=ue(g,[p[w],0]);return d}),s=e.map(p=>{let d=new Array(n),g=r;for(let w=n-1;w>=0;w--)d[w]=g,g=ue(g,[p[w],0]);return d}),i=p=>{let d=[],g=new Array(n).fill(r);for(let w=0;w<t;w++)d.push(g),g=g.map((N,v)=>ue(N,p[w][v]));return d},c=p=>{let d=new Array(t),g=new Array(n).fill(r);for(let w=t-1;w>=0;w--)d[w]=g,g=g.map((N,v)=>ue(N,p[w][v]));return d},a=i(o),l=c(s),f=i(s),m=c(o),u=a.map((p,d)=>p.map((g,w)=>ue(g,l[d][w]))),h=f.map((p,d)=>p.map((g,w)=>ue(g,m[d][w])));return{pc:u,pd:h}}function Wh(e,t={}){V(t,Fh,"lblTest");let n=Ge(t.alternative),r=El(e),o=r.length,s=r[0].length,i=(R,T,S)=>{if(R===void 0)return Array.from({length:T},(L,_)=>_+1);let k=Array.from(R);if(k.length!==T)throw new RangeError(`'${S}' must have one score for each ${S==="rowScores"?"row":"column"}`);if(k.some(L=>typeof L!="number"||!Number.isFinite(L)))throw new RangeError(`'${S}' must be finite numbers`);return k},c=i(t.rowScores,o,"rowScores"),a=i(t.colScores,s,"colScores"),f=Qt(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=>Qt(R)).map(R=>R[0]+R[1]),h=m[0].map((R,T)=>Qt(m.map(S=>S[T]))).map(R=>R[0]+R[1]),p=u.reduce((R,T)=>R+T,0);if(!(f>0||p>1))throw new RangeError("the table must have a total above 1");let d=u.reduce((R,T,S)=>R+T*c[S],0)/p,g=h.reduce((R,T,S)=>R+T*a[S],0)/p,w=c.map(R=>R-d),N=a.map(R=>R-g),v=u.reduce((R,T,S)=>R+T*w[S]*w[S],0),E=h.reduce((R,T,S)=>R+T*N[S]*N[S],0),b=0;if(m.forEach((R,T)=>R.forEach((S,k)=>b+=S*w[T]*N[k])),!(v>0&&E>0))throw new RangeError("the row and column scores must vary over the observations (the variance of the statistic is 0)");let M=b/Math.sqrt(v)/Math.sqrt(E),x=f===0?Math.sqrt(p-1)*M:$(Math.sqrt(p-$(1,-f))*M,f/2),y=n==="less"?ie(x):n==="greater"?ie(x,{lowerTail:!1}):2*ie(-Math.abs(x));return H("lblTest",{method:"Linear-by-Linear Association Test",alternative:n,statistic:x,statisticName:"Z",pValue:y,warnings:[]})}var xl=["correct","p","rescaleP","simulatePValue","B","nMinus1"];function hn(e){let t=[0,0],n=0;for(let r of e)t=ue(t,[r,0]),n+=r;return Number.isFinite(n)?t[0]:n}function Yh(e,t,n){return at(t)&&V(t,xl,"chisqTest"),V(n,xl,"chisqTest"),Array.isArray(t)?H("chisqTest",Bh(e,t,n??{})):H("chisqTest",Uh(e,t??{}))}var Sl="Chi-squared approximation may be incorrect";function Bh(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(Rn),c=[...new Set(s)].sort(Rn);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])]++),{..._l(f,n),rowLevels:i,colLevels:c}}function Uh(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?Ia(r[0],t):o===1?Ia(r.map(s=>s[0]),t):_l(r,t)}return Ia(Array.from(e),t)}function Al(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 kl(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 ql(e,t){return Number.isNaN(e)?NaN:gt(e,t/2,2,!1,!1)}function Il(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 Ia(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(d=>!(d>=0&&d<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=hn(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(d=>d<0))throw new RangeError("probabilities must be non-negative");if(o.some(d=>d===1/0))throw new RangeError("probabilities must be finite");if(t.rescaleP??!1){let d=hn(o),g=d<1/0?0:Math.ceil(Math.log2(n)),w=g===0?o:o.map(v=>$(v,-g)),N=g===0?d:hn(w);o=w.map(v=>v/N)}else if(Math.abs(hn(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(d=>$(d,-s)),c=s===0?r:hn(i),a=o.map(d=>c*d),l=s===0?a:a.map(d=>$(d,s));Il(l);let f=o.map((d,g)=>a[g]*(1-d)),m=$(hn(i.map((d,g)=>Dt(d-a[g],a[g]))),s),u="Chi-squared test for given probabilities",h=n-1,p;if(t.simulatePValue??!1){let d=Al(t);if(!Number.isInteger(r))throw new RangeError("the simulation needs counts summing to an integer");u+=bo(d),h=NaN,p=kl(Gh(r,o,l,d),m,d)}else p=ql(m,h);return{method:u,statistic:m,statisticName:"X-squared",parameter:h,parameterName:"df",pValue:p,observed:e,expected:l,residuals:i.map((d,g)=>$((d-a[g])/Math.sqrt(a[g]),s/2)),stdres:i.map((d,g)=>$((d-a[g])/Math.sqrt(f[g]),s/2)),ftResiduals:xo(e,l),warnings:Number.isFinite(h)&&l.some(d=>d<5)?[Sl]:[]}}function Gh(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=Qc(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 Hh(e,t,n,r){let o=e.length,s=t.length,i=e.reduce((f,m)=>f+m,0);if(i>is)throw new RangeError("the simulation needs a total count of at most 16777216 (2^24)");let c=cs(i),a=new Int32Array(o*s),l=new Float64Array(Math.trunc(r));for(let f=0;f<l.length;f++){ls(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 _l(e,t){let n=t.nMinus1??!1;if(typeof n!="boolean")throw new TypeError("'nMinus1' must be a boolean");if(n&&t.correct===!0)throw new RangeError("'nMinus1' uses no continuity correction: leave 'correct' out or set it to false");if(n&&(t.simulatePValue??!1))throw new RangeError("'nMinus1' cannot be combined with 'simulatePValue'");let r=!n&&(t.correct??!0),o=e.length,s=e[0].length,i=e.flat();if(i.some(v=>!(v>=0&&v<1/0)))throw new RangeError("all entries of 'x' must be non-negative and finite");let c=hn(i);if(!(c>0))throw new RangeError("at least one entry of 'x' must be positive");if(n&&!(c>1))throw new RangeError("'nMinus1' needs a table with a total above 1");let a=e.map(v=>hn(v)),l=e[0].map((v,E)=>hn(e.map(b=>b[E]))),f=a.map(v=>l.map(E=>os(v,E,c)));Il(f.flat());let m=e.map((v,E)=>v.map((b,M)=>b-f[E][M])),u="Pearson's Chi-squared test",h,p=(o-1)*(s-1),d,g=(t.simulatePValue??!1)&&a.every(v=>v>0)&&l.every(v=>v>0),w=!g&&r&&o===2&&s===2?Math.min(.5,...m.flat().map(Math.abs)):0,N=[];if((t.simulatePValue??!1)&&!g&&N.push("cannot compute simulated p-value with zero marginals"),g){if(e.some(b=>b.some(M=>!Number.isInteger(M))))throw new RangeError("the simulation needs integer counts");let v=Al(t);u+=bo(v),h=hn(m.flatMap((b,M)=>b.map((x,y)=>Dt(x,f[M][y])))),p=NaN;let E=Hh(a.map(b=>Math.trunc(b)),l.map(b=>Math.trunc(b)),f,v);d=kl(E,h,v)}else if(w>0)u+=" with Yates' continuity correction",h=hn(m.flatMap((v,E)=>v.map((b,M)=>Dt(Math.abs(b)-w,f[E][M]))));else if(h=hn(m.flatMap((v,E)=>v.map((b,M)=>Dt(b,f[E][M])))),n){u="Pearson's 'N-1' Chi-squared test";let v=h*(c-1);h=Number.isFinite(v)?v/c:h*((c-1)/c)}return{method:u,statistic:h,statisticName:"X-squared",parameter:p,parameterName:"df",pValue:d??ql(h,p),observed:e,expected:f,residuals:m.map((v,E)=>v.map((b,M)=>b/Math.sqrt(f[E][M]))),stdres:Xh(e,c,a,l,f,m),ftResiduals:xo(e,f),warnings:Number.isFinite(p)&&f.some(v=>v.some(E=>E<5))?[...N,Sl]:N}}function Xh(e,t,n,r,o,s){if(!Number.isFinite(t))return s.map((p,d)=>p.map((g,w)=>g/Math.sqrt(o[d][w]*(1-n[d]/t)*(1-r[w]/t))));let i=e.map(p=>Rl(p)),c=e[0].map((p,d)=>Rl(e.map(g=>g[d]))),a=Zh(i),l=Tl(i),f=Tl(c),m=l.map(p=>lt(p,a)),u=c.map(p=>lt(p,a)),h=f.map(p=>lt(p,a));return e.map((p,d)=>p.map((g,w)=>{let N=_e(i[d],u[w]),v=Ce([g,0],N),E=v[0]+v[1],b=_e(_e(N,m[d]),h[w]),M=b[0]+b[1];if(M>=Qh||E===0)return E/Math.sqrt(M);let x=N[0]+N[1],y=m[d][0]+m[d][1],R=h[w][0]+h[w][1];return E/Math.sqrt(x)/Math.sqrt(y)/Math.sqrt(R)}))}var Qh=2**-1022;function Rl(e){let t=[0,0];for(let n of e)t=ue(t,[n,0]);return t}function Zh(e){let t=[0,0];for(let n of e)t=ue(t,n);return t}function Tl(e){let t=e.length,n=new Array(t+1);n[t]=[0,0];for(let s=t-1;s>=0;s--)n[s]=ue(e[s],n[s+1]);let r=[],o=[0,0];for(let s=0;s<t;s++)r.push(ue(o,n[s+1])),o=ue(o,e[s]);return r}var Jh=Math.pow(Number.EPSILON,.25);function Kt(e,t,n,r={}){let o={tol:r.tol??Jh,maxiter:r.maxIter??1e3};return r.fLower!==void 0&&(o.fLower=r.fLower),r.fUpper!==void 0&&(o.fUpper=r.fUpper),pr(e,t,n,o).root}function tr(e,t){let n=a=>e(Math.exp(a)),r=0,o=n(0),s=t?-1:1,i=n(s);for(;i!==0&&Math.sign(i)===Math.sign(o);){if(Math.abs(s)>745)return t?0:1/0;r=s,o=i,s*=2,i=n(s)}if(i===0)return Math.exp(s);let c=r<s?Kt(n,r,s,{tol:2**-50,fLower:o,fUpper:i}):Kt(n,s,r,{tol:2**-50,fLower:i,fUpper:o});return Math.exp(c)}var ep=["alternative","confInt","confLevel","or","simulatePValue","B","midp"];function So(e,t={}){V(t,ep,"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(Fc));if(s.flat().some(f=>f>2147483647))throw new RangeError("'x' has entries too large to be integer");let i=[],c=tp(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 H("fisherTest",{...sp(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 H("fisherTest",{method:a+bo(f),alternative:l,pValue:np(s,f),warnings:i})}return H("fisherTest",{method:a,alternative:l,pValue:ap(s),warnings:i})}function tp(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: ${Zc(a)}`:null}function np(e,t){let n=e.filter(g=>g.some(w=>w>0)),r=e[0].map((g,w)=>w).filter(g=>e.some(w=>w[g]>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(g=>r.map(w=>g[w])),s=o.map(g=>g.reduce((w,N)=>w+N,0)),i=o[0].map((g,w)=>o.reduce((N,v)=>N+v[w],0)),c=s.length,a=i.length,l=[0,0];for(let g of o.flat())l=ue(l,[-ss(g),0]);let f=l[0]/(1+64*Number.EPSILON),m=s.reduce((g,w)=>g+w,0);if(m>is)throw new RangeError("the simulation needs a total count of at most 16777216 (2^24)");let u=cs(m),h=new Int32Array(c*a),p=0,d=Math.trunc(t);for(let g=0;g<d;g++){ls(s,i,u,h);let w=0;for(let N=0;N<h.length;N++)w-=u[h[N]];w<=f&&p++}return Math.max(0,Math.min(1,(1+p)/(d+1)))}var Ll=1+1e-7,rp=16,op=2**24;function Ro(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=ue(r,[e[o],0]));return r[0]}var Wr=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%rp===0||!(s>0&&s<1/0)?(o=gn(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=Ro(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=ue(r,[(this.lo+o)*n[o],0]));return r[0]}tail(t,n,r){if(n===1)return r?on(t-1,this.m,this.n,this.k,!1,!1):on(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?Ro(o,s,o.length-1):Ro(o,0,s)}midTail(t,n,r){if(n===1){let i=.5*gn(t,this.m,this.n,this.k,!1);return(r?on(t,this.m,this.n,this.k,!1,!1):on(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?Ro(o,s+1,o.length-1):Ro(o,0,s-1))+.5*o[s]}};function La(e,t,n){let r=e.density(n),o=r[t-e.lo]*Ll,s=[0,0];for(let i of r)i<=o&&(s=ue(s,[i,0]));return s[0]}function Cl(e,t,n,r){let o=Math.max(0,r-n),s=Math.min(r,t),i=d=>gn(d,t,n,r,!0),c=i(e)+Math.log(Ll),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=(d,g)=>{let w=d,N=g+1;for(;N-w>1;){let v=w+Math.floor((N-w)/2);i(v)<=c?w=v:N=v}return w},f=(d,g)=>{let w=d-1,N=g+1;for(;N-w>1;){let v=w+Math.floor((N-w)/2);i(v)<=c?N=v:w=v}return N},m,u;e<=a?(m=l(e,a),u=f(a+1,s)):(u=f(a,e),m=a-1<o||i(o)>c?o-1:l(o,a-1));let h=m<o?0:on(m,t,n,r,!0,!1),p=u>s?0:on(u-1,t,n,r,!1,!1);return h+p}function To(e,t){return tr(e,t)}function sp(e,t,n){let r=Ge(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>op)throw new RangeError(`the exact distribution of the 2 x 2 table has ${m} values, more than 2^24`);let u=new Wr(i+a,c+l,i+c),{lo:h,hi:p}=u,d=t.midp??!1,g;d?(r==="less"?g=u.midTail(f,o,!1):r==="greater"?g=u.midTail(f,o,!0):g=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"?g=u.tail(f,o,!1):r==="greater"?g=u.tail(f,o,!0):o===0?g=f===h?1:0:o===1/0?g=f===p?1:0:g=La(u,f,o);let w;if(f===h)w=0;else if(f===p)w=1/0;else{let M=u.mean(1);w=M===f?1:To(x=>u.mean(x)-f,M>f)}let N={method:n,alternative:r,pValue:g,estimate:w,estimateNames:"odds ratio",nullValue:o,nullValueName:"odds ratio",warnings:[]};if(!(t.confInt??!0))return N;let v=M=>{if(f===p)return 1/0;if(d){if(f===h&&M>=.5)return 0;let y=u.midTail(f,1,!1);return y===M?1:To(R=>u.midTail(f,R,!1)-M,y<M)}let x=u.tail(f,1,!1);return x===M?1:To(y=>u.tail(f,y,!1)-M,x<M)},E=M=>{if(f===h)return 0;if(d){if(f===p&&M>=.5)return 1/0;let y=u.midTail(f,1,!0);return y===M?1:To(R=>u.midTail(f,R,!0)-M,y>M)}let x=u.tail(f,1,!0);return x===M?1:To(y=>u.tail(f,y,!0)-M,x>M)},b;if(r==="less")b=[0,v(1-s)];else if(r==="greater")b=[E(1-s),1/0];else{let M=(1-s)/2;b=[E(M),v(M)]}return{...N,confInt:b,confLevel:s}}var xs=[[0,0]];function Tn(e){for(let t=xs.length;t<=e;t++)xs.push(ue(xs[t-1],Ic([t,0])));return xs[e]}function Rs(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 _a=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 ap(e){let t=e.filter(v=>v.some(E=>E>0));if(t.length===0)throw new RangeError("all entries of the table are zero");let n=t[0].map((v,E)=>t.some(b=>b[E]>0));if(t=t.map(v=>v.filter((E,b)=>n[b])),t.length<2||t[0].length<2)return 1;t.length>t[0].length&&(t=t[0].map((v,E)=>t.map(b=>b[E])));let r=t.length,o=t.map(v=>v.reduce((E,b)=>E+b,0)),s=t[0].map((v,E)=>t.reduce((b,M)=>b+M[E],0)).sort((v,E)=>E-v),i=s.length,c=o.reduce((v,E)=>v+E,0),a=qc(Tn(c));for(let v of o)a=ue(a,Tn(v));for(let v of s)a=ue(a,Tn(v));let l=[0,0];for(let v of t.flat())l=Ce(l,Tn(v));let f=ue(l,[Math.log1p(1e-7),0]),m=1e-9*(1+Math.abs(f[0])),[u,h]=f,p=Array.from({length:i},()=>new Map),d=(v,E)=>{let b=E.join(","),M=p[v].get(b);if(M!==void 0)return M;let x=ue(a,Tn(E.reduce((L,_)=>L+_,0)));for(let L of E)x=Ce(x,Tn(L));for(let L=v;L<i;L++)x=Ce(x,Tn(s[L]));let y={R:E,KZ:x,max:0,min:0,edges:[],pastHi:[],pastLo:[],count:[],index:new Map};if(p[v].set(b,y),v===i-1){let L=[0,0];for(let _ of E)L=Ce(L,Tn(_));return y.max=y.min=L[0],y.edges.push({to:y,hi:L[0],lo:L[1],mult:1}),y}let R=new Map,T=new Array(r+1).fill(0);for(let L=r-1;L>=0;L--)T[L]=T[L+1]+E[L];let S=new Array(r).fill(0),k=(L,_,I)=>{if(L===r-1){S[L]=_;let D=Ce(I,Tn(_)),A=E.map((B,J)=>B-S[J]).sort((B,J)=>B-J),O=d(v+1,A),q=`${A.join(",")}|${D[0]}`,P=R.get(q);P===void 0?R.set(q,{to:O,hi:D[0],lo:D[1],mult:1}):P.mult++;return}let F=Math.max(0,_-T[L+1]),C=Math.min(E[L],_);for(let D=F;D<=C;D++)S[L]=D,k(L+1,_-D,Ce(I,Tn(D)))};if(k(0,s[v],[0,0]),y.edges=[...R.values()],y.max=At(y.edges.map(L=>L.hi+L.to.max)),y.min=sn(y.edges.map(L=>L.hi+L.to.min)),v===i-2){let L=y.edges.map(O=>{let q=O.to.edges[0];return{v:ue([O.hi,O.lo],[q.hi,q.lo]),mult:O.mult}});L.sort((O,q)=>O.v[0]-q.v[0]||O.v[1]-q.v[1]);let _=L.length,I=L[_-1].v[0],F=new Float64Array(_),C=new Float64Array(_),D=new Float64Array(_),A=[0,0];L.forEach(({v:O,mult:q},P)=>{F[P]=O[0],C[P]=O[1];let B=Ce(O,[I,0]);A=ue(A,[q*Math.exp(B[0])*(1+B[1]),0]),D[P]=A[0]}),y.dist={hi:F,lo:C,cum:D,vmax:I,kv:ue(a,[I,0])}}return y},g=d(0,[...o].sort((v,E)=>v-E)),w=new _a,N=(v,E,b,M)=>{if(E+v.max<=u-m){let[k,L]=Rs(v.KZ[0],v.KZ[1],E,b);return w.add(k,L,M),!0}if(E+v.min>u+m)return!0;let x=v.dist;if(x===void 0)return!1;let[y,R]=Rs(u,h,-E,-b),T=0,S=x.hi.length;for(;T<S;){let k=T+S>>>1,L=x.hi[k];L<y||L===y&&x.lo[k]<=R?T=k+1:S=k}if(T>0){let[k,L]=Rs(x.kv[0],x.kv[1],E,b);w.add(k,L,M*x.cum[T-1])}return!0};N(g,0,0,1)||(g.pastHi.push(0),g.pastLo.push(0),g.count.push(1));for(let v=0;v<i-2;v++)for(let E of p[v].values()){let{pastHi:b,pastLo:M,count:x}=E;if(x.length!==0){for(let{to:y,hi:R,lo:T,mult:S}of E.edges)for(let k=0;k<x.length;k++){let[L,_]=Rs(b[k],M[k],R,T),I=x[k]*S;if(N(y,L,_,I))continue;let F=y.index.get(L);F===void 0?(y.index.set(L,y.count.length),y.pastHi.push(L),y.pastLo.push(_),y.count.push(I)):y.count[F]+=I}E.pastHi=E.pastLo=E.count=[],E.index.clear()}}return Math.min(1,w.value())}function Ts(e){let t=e.every(n=>Number.isInteger(n))?1:2;return[t,e.map(n=>n*t).sort((n,r)=>n-r)]}function Oa(e){return[...e].sort((t,n)=>t-n).join(",")}function Ca(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 Fa(e){let[,t]=Ts(e),n=0,r=0;for(let o of t)r+=n+=o;return{steps:r,size:n+1}}function Da(e){let[t,n]=Ts(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 Ca(t,0,o)}function Pa(e,t){let[,n]=Ts(e),r=n.length,o=Math.min(t,r-t),s=Ol(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 Ol(e){let t=[0];for(let n of e)t.push(t[t.length-1]+n);return t}function Va(e,t){let[n,r]=Ts(e),o=r.length,s=Math.min(t,o-t),i=Ol(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 d=Math.min(h,s);d>=Math.max(1,s-(o-h));d--){let g=c[d],w=c[d-1],N=d/h,v=(h-d)/h,E=i[d],b=i[h]-i[h-d],M=i[d-1]+p,x=Math.max(E,M);for(let y=b;y>=x;y--)g[y-E]=v*g[y-E]+N*w[y-M];for(let y=x-1;y>=E;y--)g[y-E]=v*g[y-E]}}let a=c[s],l=t*(t+1)/2;if(s===t)return Ca(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 Ca(n,(i[o]-u)/n-l,m)}function nr(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 vr(e,t){let n=nr(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 Fl=["alternative","mu","paired","exact","correct","confInt","confLevel","tolRoot"];function Ao(e,t,n={}){if(typeof e!="string"&&at(t)&&V(t,Fl,"wilcoxTest"),V(n,Fl,"wilcoxTest"),typeof e=="string"){if(n.paired!==void 0)throw new Error("cannot use 'paired' in formula method");let d=ms(e,t,!0);return d.kind==="one"?Ao(d.x,n):Ao(d.x,d.y,d.kind==="paired"?{...n,paired:!0}:n)}let r;t!==void 0&&!ArrayBuffer.isView(t)&&!Array.isArray(t)?n=t:r=t;let o=Ge(n.alternative),s=n.mu??0,i=n.paired??!1,c=n.exact??null,a=cp(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[d,g]=Gn(Array.from(e),Array.from(r));u=Ct(d.map((w,N)=>w-g[N]))}else u=Ct(Array.from(e)),r!==void 0&&(h=Ct(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 mt};return H("wilcoxTest",h===void 0?hp(u,c,i,p):yp(u,h,c,p))}function cp(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 Ss(e,t,n){return n?t==="two.sided"?Math.sign(e)*.5:t==="greater"?.5:-.5:0}var Vl=10*Number.EPSILON;function $l(e,t,n=0){let r=a=>Math.min(1,Math.max(a,0)),o=()=>n===0?ie(e):r(ie(e)-n*Xt(e)),s=()=>n===0?ie(e,{lowerTail:!1}):r(ie(e,{lowerTail:!1})+n*Xt(e));if(t==="less")return o();if(t==="greater")return s();let i=o();if(i<=.5)return 2*i;let c=s();return c>=1/64?2*(1-i):2*c}function lp(e){let t=e*e;return[e*(t-3),e*(t*(t-10)+15),e*(t*(t*(t-21)+105)-105)]}function fp(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 zl(e,o,s,n)}function mp(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 zl(e,i,c,r)}function zl(e,t,n,r){let[o,s,i]=lp(e),c=t*o;return r>=2&&(c+=n*s),r>=3&&(c+=t*t/2*i),c}function ja(e){let t=e.length;if(t===0)return NaN;let n=Math.floor(t/2);return t%2===1?e[n]:st([e[n-1],e[n]])}var Ka=(e,t)=>e-t,$a=5e9,up=40;function za(e){return new RangeError(`wilcoxTest: the exact conditional ${e} would take too long or too much memory: use exact: false`)}function As(e,t){if(e.size>16777216||e.steps>$a)throw za(t);return e.steps}function jl(e){let t=e.length,n=bt(e.map(Math.abs)),r=0;for(let i=0;i<t;i++)e[i]>0&&(r+=n[i]);let o=0;for(let i of pn(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 hp(e,t,n,r){let{alternative:o,mu:s}=r,i=e.map(b=>b-s);i.some((b,M)=>!Number.isFinite(b)&&Number.isFinite(e[M]))&&(i=e.map(b=>b/2-s/2));let a=i.some(b=>b===0),l=t??i.length<50,f=(b,M,x)=>({method:b,alternative:o,statistic:M,statisticName:"V",pValue:x,nullValue:s,nullValueName:n?"location shift":"location",warnings:r.warnings.list});if(l){let b=bt(i.map(Math.abs)),M=pn(b).length>0,x=0;for(let L=0;L<i.length;L++)i[L]>0&&(x+=b[L]);let y="Wilcoxon signed rank exact test";if(!M&&!a){let L=i.length,_=L*(L+1)/2,I;if(o==="less")I=$r(x,L,!0,!1);else if(o==="greater")I=$r(x-1,L,!1,!1);else{let C=x>_/2?$r(x-1,L,!1,!1):$r(x,L,!0,!1);I=Math.min(1,2*C)}let F=f(y,x,I);return r.confInt?{...F,...pp(e,r)}:F}let R=b.filter((L,_)=>i[_]!==0);As(Fa(R),"distribution");let T=Da(R),S;if(o==="less")S=nr(T,x);else if(o==="greater")S=vr(T,x-.25);else{let L=0;for(let _ of R)L+=_/2;S=Math.min(1,2*(x>L?vr(T,x-.25):nr(T,x)))}let k=f(y,x,S);return r.confInt?{...k,...gp(e,r)}:k}let m=a?i.filter(b=>b!==0):i,u=m.length;if(u===0)throw new RangeError("no non-zero differences for the signed rank test");let[h,p,d]=jl(m),w=pn(bt(m.map(Math.abs))).length>0||a?Math.min(r.correct,0):r.correct,N=(p-Ss(p,o,w>=0))/d,v=$l(N,o,w>=1?fp(N,u,w):0),E=f(`Wilcoxon signed rank test${r.correct>=0?" with continuity correction":""}`,h,v);return r.confInt?{...E,...wp(e,r)}:E}function Wa(e,t,n){return e==="less"?[-1/0,n]:e==="greater"?[t,1/0]:[t,n]}function Dl(e,t,n){let r={confInt:Wa(e,NaN,NaN),confLevel:0,estimate:t};return n!==void 0&&(r.estimateNames=n),r}function Kl(e){if(!(e>0&&e<1/0))throw new RangeError("tolRoot must be a positive finite number")}function Wl(e){let t=e.length,n=[];for(let r=0;r<t;r++)for(let o=r;o<t;o++){let s=Hn(e[r],e[o]);Number.isNaN(s)||n.push(s)}return n.sort(Ka)}function Yl(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+Vl&&l++;let f=ja(e);if(l===0)return{confInt:[-1/0,1/0],confLevel:1,estimate:f,estimateNames:o};let m=t(l-1);return{confInt:Wa(s,e[l-1],e[c-l]),confLevel:1-(s==="two.sided"?2*m:m),estimate:f,estimateNames:o}}function pp(e,t){let n=e.length;return Yl(Wl(e),r=>$r(r,n,!0,!1),r=>jc(r,n,!0,!1),t,"(pseudo)median")}function Bl(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 Ul(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)+Vl,d=f!=="less",g=f!=="greater",w=I=>Hn(e[I-1],e[I]),N=Math.min(u-1,Math.max(1,Math.ceil(i.mean+oe(Math.min(p,1))*i.sd+.1*i.sd+3))),v=8,E=I=>I===0?e[0]-1:w(I),b=I=>I===0?e[u-1]+1:w(u-I),M=[new Map,new Map];{let I=new Map,F=0,C=up*c;for(let[D,A]of[[0,E],[1,b]])if(!(D===0?!d:!g))for(let O=0;O<=N;O++){if(F+=C,F>$a)throw za("interval");let q;try{q=t(A(O))}catch{break}if(M[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>$a)throw za("interval");I.set(q.key,!0),I.size>v&&I.delete(I.keys().next().value)}}let x=new Map,y=(I,F)=>{let C=I===0?E(F):b(F),D=M[I].get(F),A;if(D===void 0){let B=t(C);D=[B.stat,B.key],A=B.scores}let[O,q]=D,P=x.get(q);return P!==void 0?x.delete(q):(P=n(A??t(C).scores),x.size>=v&&x.delete(x.keys().next().value)),x.set(q,P),[O,P]},R=I=>{let[F,C]=y(0,I);return o(C,F)},T=I=>{let[F,C]=y(1,I);return s(C,F)},S=-1/0,k=0;if(d){let I=R(0);if(!(I>p)){let F=I,C=1;for(;C<=u-1;C++){let D=R(C);if(D>p)break;F=D}S=e[C-1],k=F}}let L=1/0,_=0;if(g){let I=T(0);if(!(I>p)){let F=I,C=u-1;for(;C>=1;C--){let D=T(u-C);if(D>p)break;F=D}L=e[C],_=F}}return{confInt:[S,L],confLevel:1-(k+_),estimate:ja(e),estimateNames:l}}function dp(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 gp(e,t){let n=e.length,r=s=>{let i=dp(e,s),{ranks:c,key:a}=Bl(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 Ul(Wl(e),r,Da,s=>As(Fa(s),"interval"),(s,i)=>vr(s,i-.25),nr,{mean:o/2,sd:Math.sqrt(n*(n+1)*(2*n+1)/24)},n,t,"(pseudo)median")}function wp(e,t){let{alternative:n,confLevel:r,tolRoot:o}=t,s=t.correct>=0,i=e.length;if(i===0)return Dl(n,NaN);let c=sn(e),a=At(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"),Dl(n,Hn(c,a),"midrange");let l=(v,E)=>{let[,b,M]=jl(e.map(x=>x-v).filter(x=>x!==0));return(b-Ss(b,n,E))/M},f=l(c,s),m=l(a,s),u=v=>-oe(v),h=v=>n==="greater"?!(f<u(v)):n==="less"?!(m>oe(v/2)):!(f<u(v/2))&&!(m>oe(v/2)),p=1-r,d=!1;for(;!h(p)&&p<1;)p*=2,d=!0;d&&t.warnings.add("requested conf.level not achievable"),Kl(o);let g=v=>Kt(E=>l(E,s)-v,c,a,{tol:o,fLower:f-v,fUpper:m-v}),w;if(p>=1){let v=ja([...e].sort(Ka));w=Wa(n,v,v)}else n==="greater"?w=[g(u(p)),1/0]:n==="less"?w=[-1/0,g(oe(p))]:w=[g(u(p/2)),g(oe(p/2))];let N=Kt(v=>l(v,!1),c,a,{tol:o,fLower:l(c,!1),fUpper:l(a,!1)});return{confInt:w,confLevel:d?1-Math.min(1,p):r,estimate:N,estimateNames:"(pseudo)median"}}function Gl(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 pn(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 bp(e,t){let[n,r,o]=Gl(bt([...e,...t]),e.length);return[n,r,o]}function vp(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 Np(e,t,n){return Hl(e,t,n).ranks}function Hl(e,t,n){let r=e.map(o=>o-n);if(r.some(o=>Number.isNaN(o))){let o=r.map((a,l)=>[a,l]).filter(([a])=>!Number.isNaN(a)),s=bt([...o.map(([a])=>a),...t]),i=new Array(e.length+t.length);o.forEach(([,a],l)=>i[a]=s[l]),t.forEach((a,l)=>i[e.length+l]=s[o.length+l]);let c=o.length+t.length;return r.forEach((a,l)=>{Number.isNaN(a)&&(i[l]=++c)}),{ranks:i,key:Oa(i)}}if(Number.isFinite(n)&&r.some((o,s)=>!Number.isFinite(o)&&Number.isFinite(e[s]))){let o=vp(e.map(s=>s/2-n/2),t);return{ranks:o,key:Oa(o)}}return Bl([...r,...t])}function yp(e,t,n,r){let{alternative:o,mu:s}=r,i=e.length,c=t.length,a=n??(i<50&&c<50),l=Np(e,t,s),[f,m,u,h]=Gl(l,i),p,d;if(a)if(d="Wilcoxon rank sum exact test",!h)p=Pl(()=>o==="less"?zr(f,i,c,!0,!1):o==="greater"?zr(f-1,i,c,!1,!1):Math.min(2*zr(f,i,c,!0,!1),2*zr(f-1,i,c,!1,!1),1));else{As(Pa(l,i),"distribution");let N=Va(l,i);o==="less"?p=nr(N,f):o==="greater"?p=vr(N,f-.25):p=Math.min(2*nr(N,f),2*vr(N,f-.25),1)}else{d=`Wilcoxon rank sum test${r.correct>=0?" with continuity correction":""}`;let N=h?Math.min(r.correct,0):r.correct,v=l.every(b=>b===l[0]),E=(m-Ss(m,o,N>=0))/u;p=v?1:$l(E,o,N>=1?mp(E,i,c,N):0)}let g={method:d,alternative:o,statistic:f,statisticName:"W",pValue:p,nullValue:s,nullValueName:"location shift",warnings:r.warnings.list};if(!r.confInt)return g;let w=a?h?Ep(e,t,r):Pl(()=>Mp(e,t,r)):xp(e,t,r);return{...g,...w}}function Pl(e){try{return e()}catch(t){throw t instanceof RangeError&&t.message.startsWith("wilcox:")?new RangeError(`${t.message}: use exact: false`):t}}function Xl(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(Ka)}function Mp(e,t,n){let r=e.length,o=t.length;return Yl(Xl(e,t,!1),s=>zr(s,r,o,!0,!1),s=>Wc(s,r,o,!0,!1),n,"difference in location")}function Ep(e,t,n){let r=e.length,o=t.length,s=i=>{let{ranks:c,key:a}=Hl(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 Ul(Xl(e,t,!0),s,i=>Va(i,r),i=>As(Pa(i,r),"interval"),(i,c)=>vr(i,c-.25),nr,{mean:r*o/2,sd:Math.sqrt(r*o*(r+o+1)/12)},r+o,n,"difference in location")}function xp(e,t,n){let{alternative:r,confLevel:o,tolRoot:s}=n,i=n.correct>=0,c=sn(e)-At(t),a=At(e)-sn(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[,d,g]=bp(e.map(w=>w-h),t);return(d-Ss(d,r,p))/g},f=(h,p)=>{let d=l(c,p)-h,g=l(a,p)-h;if(Number.isNaN(d)||Number.isNaN(g))throw new Error("cannot compute the confidence interval because all observations are tied");return d<=0?c:g>=0?a:(Kl(s),Kt(w=>l(w,p)-h,c,a,{tol:s,fLower:d,fUpper:g}))},m=1-o,u;return r==="greater"?u=[f(-oe(m),i),1/0]:r==="less"?u=[-1/0,f(oe(m),i)]:u=[f(-oe(m/2),i),f(oe(m/2),i)],{confInt:u,confLevel:o,estimate:f(0,!1),estimateNames:"difference in location"}}var Rp=["method","n"],Ql=["holm","hochberg","hommel","bonferroni","BH","BY","fdr","none"];function Tp(e){if(Ql.includes(e))return e;let t=Ql.filter(n=>n.startsWith(e));if(e.length===0||t.length!==1)throw new RangeError(`invalid method "${e}"`);return t[0]}function Yr(e,t={}){V(t,Rp,"pAdjust");let n=typeof t=="string"?{method:t}:t,r=Tp(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=Jl(a,r,c),f=o.slice();return s.forEach((m,u)=>f[m]=l[u]),f}function ks(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 Jl(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=ks(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=ks(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=Fn(),u=Math.floor(n);if(u<=1e6){for(let h=1;h<=u;h++)mn(m,1/h,u);s=Dn(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=ks(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?Jl(e,"hochberg",n):Sp(e,n)}}var Zl=2**16;function Sp(e,t){if(!Number.isInteger(t))throw new RangeError("n must be an integer for the hommel method");if(t>Zl)throw new RangeError(`n must be at most ${Zl} for the hommel method (O(n^2) work)`);let n=e.length,r=[...e,...new Array(t-n).fill(1)],o=ks(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 ef=["p","alternative","confLevel"],Ap=1+1e-7;function Ya(e,t,n){let r=e-1;return e===0||e-r===1&&$t(t,-r)[1]===0?On(r,t,n,!1,!1):On(e,t,n,!1,!1)+fn(e,t,n,!1)}function kp(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=fn(e,t,n,!1)*Ap,s=a=>fn(a,t,n,!1)<=o;if(e<r){let a=Zo(s,Math.ceil(r),t),l=a>t?0:Ya(a,t,n);return Math.min(1,On(e,t,n,!0,!1)+l)}let i=Jo(s,0,Math.floor(r)),c=i<0?0:On(i,t,n,!0,!1);return Math.min(1,Ya(e,t,n)+c)}function qp(e,t,n={}){V(t,ef,"binomTest"),V(n,ef,"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),!wn(e[0])||!wn(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(!wn(e))throw new RangeError("'x' must be nonnegative and integer");if(r=Math.round(e),!(wn(t)&&Math.round(t)>=r&&t>0))throw new RangeError("'n' must be a positive integer >= 'x'");o=Math.round(t)}return Ba(r,o,n)}function Ba(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=Ge(n.alternative),s=n.confLevel??.95;It(s);let i;o==="less"?i=On(e,t,r,!0,!1):o==="greater"?i=Ya(e,t,r):i=kp(e,t,r);let c=f=>e===0?0:Yn(f,e,t-e+1,!0,!1),a=f=>e===t?1:f<1e-8?Yn(f,e+1,t-e,!1,!1):Yn(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 Ip=["T","r","alternative","confLevel"],_p=1+1e-7;function Lp(e,t){if(t===0)return e===0?1:0;if(e===t)return 1;let n=es(e,t,!1)*_p,r=i=>es(i,t,!1)<=n,o=Math.floor(t);if(e<t){let i=Zo(r,Math.max(e+1,o),1/0);return Math.min(1,Pr(e,t,!0,!1)+Ua(i,t))}let s=Jo(r,0,Math.min(e-1,o));return Math.min(1,Pr(s,t,!0,!1)+Ua(e,t))}var ko=2**-1022;function Ua(e,t){return e-(e-1)===1?Pr(e-1,t,!1,!1):Pr(e,t,!1,!1)+es(e,t,!1)}function Nr(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 Cp(e,t,n,r){let o=n+t*r,s=e*t*n/o;if(Number.isFinite(s)&&Number.isFinite(o)&&o>=ko&&(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]=Nr(e),[f,m]=Nr(t),[u,h]=Nr(n),[p,d]=Nr(r),g=f*p,w=h-m-d,N=w>=0?u/(u+$(g,-w)):u/($(u,w)+g);return $(a*f*N,l+m+Math.min(w,0))}function Op(e,t,n,r){let o=e/n,s=t/r,i=o/s;return t===0||Number.isFinite(o)&&Number.isFinite(s)&&s>=ko&&(o===0||o>=ko)?i:e===0?0:e/t*(r/n)}function Fp(e,t={}){V(t,Ip,"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(wn))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(w=>Number.isFinite(w)&&w>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=Ge(t.alternative),l=t.confLevel??.95;if(o===2?It(l):an(l),o===2){let[w,N]=r,[v,E]=i,b=w+N;if(b===1/0)throw new RangeError("'x' must be finite, nonnegative, and integer");let M=c*v,x=M+E,y=Number.isFinite(x)&&x>=ko&&(M===0||M>=ko)?M/x:1/(1+E/v/c),R=Ba(Math.round(w),Math.round(w)+Math.round(N),{p:y,alternative:a,confLevel:l}),T=E/v,S=k=>{if(k===0)return 0;let L=k/(1-k);if(Number.isFinite(T)||L===1/0)return L*T;let[_,I]=Nr(L),[F,C]=Nr(v),[D,A]=Nr(E);return $(_*D/F,I+A-C)};return{method:"Comparison of Poisson rates",alternative:a,statistic:w,statisticName:"count1",parameter:Cp(b,c,v,E),parameterName:"expected count1",pValue:R.pValue,confInt:[S(R.confInt[0]),S(R.confInt[1])],confLevel:l,estimate:Op(w,N,v,E),estimateNames:"rate ratio",nullValue:c,nullValueName:"rate ratio",warnings:[]}}let f=r[0],m=i[0],u=c*m,h;a==="less"?h=Pr(f,u,!0,!1):a==="greater"?h=Ua(f,u):h=Lp(f,u);let p=w=>f===0?0:Qo(w,f,1,!0,!1),d=w=>w<1e-8?Qo(w,f+1,1,!1,!1):Qo(1-w,f+1,1,!0,!1),g;if(a==="less")g=[0,d(1-l)];else if(a==="greater")g=[p(1-l),1/0];else{let w=(1-l)/2;g=[p(w),d(w)]}return{method:"Exact Poisson test",alternative:a,statistic:f,statisticName:"number of events",parameter:m,parameterName:"time base",pValue:h,confInt:[g[0]/m,g[1]/m],confLevel:l,estimate:f/m,estimateNames:"event rate",nullValue:c,nullValueName:"event rate",warnings:[]}}var tf=["p","alternative","confLevel","correct"],Dp="Chi-squared approximation may be incorrect";function Ga(e,t,n={}){at(t)&&V(t,tf,"propTest"),V(n,tf,"propTest");let r,o,s=n;if(typeof e!="number"&&e.length>0&&Array.isArray(e[0])){let S=e;if(S.some(k=>k.length!==2))throw new RangeError("'x' must have 2 columns");r=S.map(k=>k[0]),o=S.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 S=0;S<i;S++)!Number.isNaN(r[S])&&!Number.isNaN(o[S])&&c.push(S);let a=c.length;if(a<1)throw new RangeError("not enough data");if(r=c.map(S=>r[S]),o=c.map(S=>o[S]),o.some(S=>!(S>0)))throw new RangeError("elements of 'n' must be positive");if(o.some(S=>S===1/0))throw new RangeError("elements of 'n' must be finite");if(r.some(S=>!(S>=0)))throw new RangeError("elements of 'x' must be nonnegative");if(r.some((S,k)=>S>o[k]))throw new RangeError("elements of 'x' must not be greater than those of 'n'");let l;if(s.p!==void 0){let S=typeof s.p=="number"?[s.p]:Array.from(s.p);if(S.length!==i)throw new RangeError("'p' must have the same length as 'x' and 'n'");if(l=c.map(k=>S[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;It(m);let u=Ge(s.alternative);(a>2||a===2&&f)&&(u="two.sided");let h=r.map((S,k)=>S/o[k]),p=a===2?h[0]-h[1]:0,d=a===2?1/o[0]+1/o[1]:0,g=(s.correct??!0)&&a<=2?.5:0,w=!1;if(g>0&&a===1){let S=Math.abs(r[0]-o[0]*l[0]);S<=g&&([g,w]=[S,!0])}else if(g>0&&a===2&&!f){let S=Math.abs(p)/d;S<=g&&([g,w]=[S,!0])}let N=Pp(r,o),v=o.some((S,k)=>{let L=l?.[k]??N;return S*L<5||S*(1-L)<5}),E=0;if(!w)for(let S=0;S<a;S++){let k=l?.[S]??N,L=o[S]*k,_=o[S]*(1-k);E+=Dt(Math.abs(r[S]-L)-g,L),E+=Dt(Math.abs(o[S]-r[S]-_)-g,_)}let b=l!==void 0?a:a-1,M;if(u==="two.sided")M=gt(E,b/2,2,!1,!1);else{let S=Math.sign(a===1?h[0]-l[0]:p);M=ie(S*Math.sqrt(E),{lowerTail:u==="less"})}let x;if(a===1||a===2&&!f){let S=(1-m)/2,k=u==="two.sided"&&S<1e-8?oe(S,{lowerTail:!1}):oe(u==="two.sided"?(1+m)/2:m);if(a===1){let L=o[0],_=k*k,I=(O,q)=>(O+_/(2*L)+q*k*Math.sqrt(O*(1-O)/L+_/(4*L*L)))/(1+_/L),F=h[0]+g/L,C=h[0]-g/L,D=Math.min(1,Math.max(0,F>=1?1:I(F,1))),A=Math.min(1,Math.max(0,C<=0?0:I(C,-1)));x=u==="two.sided"?[A,D]:u==="greater"?[A,1]:[0,D]}else{let L=Math.sqrt(h.reduce((C,D,A)=>C+D*(1-D)/o[A],0)),_=k*L+g*d,I=Math.max(-1,p-_),F=Math.min(1,p+_);x=u==="two.sided"?[I,F]:u==="greater"?[I,1]:[-1,F]}}let y=a===1?"p":c.map(S=>`prop ${S+1}`),T={method:`${a===1?"1-sample proportions test":`${a}-sample test for ${f?"given proportions":"equality of proportions"}`} ${g!==0?"with":"without"} continuity correction`,alternative:u,statistic:E,statisticName:"X-squared",parameter:b,parameterName:"df",pValue:M,estimate:a===1?h[0]:h,estimateNames:y,warnings:v?[Dp]:[]};return l!==void 0&&(T.nullValue=a===1?l[0]:l,T.nullValueName=y),x!==void 0&&(T.confInt=x,T.confLevel=m),H("propTest",T)}function Pp(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 Vp=["method","confLevel","sides"],$p=["wilson","wald","wilsoncc","agresti-coull","jeffreys","clopper-pearson","midp"],zp=["two.sided","left","right"];function qo(e,t){let n=xe(t,zp),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{...Ha(s,o),sides:n}}function Ha(e,t=1-e){let n=e/2<1e-8,r=n?oe(e/2,{lowerTail:!1}):oe(1-e/2),o=n?Cn(e,1,{lowerTail:!1}):Cn(t,1);return{sides:"two.sided",level:t,alpha:e,kappa:r,chisq:o}}function Br(e,t){if(typeof e!="number")throw new TypeError(`'${t}' must be a number`);if(!wn(e))throw new RangeError(`'${t}' must be a nonnegative integer`);return Math.round(e)+0}function $n(e,t,n,r){let o=Br(e,n),s=Br(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 Is(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 qs(e,t,n,r){return r?e/2<1e-8?Yn(e/2,t,n,!1,!1):Yn(1-e/2,t,n,!0,!1):Yn(e/2,t,n,!0,!1)}function nf(e,t,n,r){let o=n/2,s=r?f=>{let m=Math.exp(f);return .5*fn(e,t,m,!1)+On(e-1,t,m,!0,!1)-o}:f=>{let m=Math.exp(f);return .5*fn(e,t,m,!1)+On(e,t,m,!1,!1)-o},i=r?-1:1,c=Math.log(e/t),a=i*s(c);if(a===0)return e/t;if(a>0){let f=c,m=a,u=1;for(;;){let h=c-u;if(h<-745)return 0;let p=i*s(h);if(p<=0)return Math.exp(p===0?h:Kt(d=>i*s(d),h,f,{tol:2**-60,fLower:p,fUpper:m}));f=h,m=p,u*=2}}let l=i*s(0);return l<=0?1:Math.exp(Kt(f=>i*s(f),c,0,{tol:2**-60,fLower:a,fUpper:l}))}function Xa(e,t,n={}){V(n,Vp,"binomCI");let r=xe(n.method,$p),o=qo(n.confLevel,n.sides),[s,i]=$n(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]=Is(s,i,a);break;case"wilsoncc":{let h=a*a,p=2*(i+h),d=2*s+h-1,g=a*Math.sqrt(h-2-1/i+4*l*(i*f+1));m=l===0?0:d-g<d/2?(2*s-1)/(2*i)*(2*s-1)/(d+g):(d-g)/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,d=(s+h/2)/p,g=a*Math.sqrt(d*(1-d)/p);[m,u]=[d-g,d+g];break}case"jeffreys":m=s===0?0:qs(c,s+.5,i-s+.5,!1),u=s===i?1:qs(c,s+.5,i-s+.5,!0);break;case"clopper-pearson":m=s===0?0:qs(c,s,i-s+1,!1),u=s===i?1:qs(c,s+1,i-s,!0);break;case"midp":m=s===0?0:s===i?Math.exp(Math.log(c)/i):nf(s,i,c,!1),u=s===i?1:s===0?-Math.expm1(Math.log(c)/i):nf(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),H("binomCI",{estimate:l,confInt:[m,u],confLevel:n.confLevel??.95,method:r,sides:o.sides,warnings:[]})}function Io(e){return e===1/0?1:e/(1+e)}function _o(e,t){return e===0?0:t>0?e*e/t:1/0}function jp(e,t,n,r){return e/t+n/r>1}function Kp(e,t,n,r){return Ls(e,t,n,r,0)}function Ja(e,t,n,r,o){if(jp(e,t,n,r)){let[p,d,g,w]=Ja(t-e,t,r-n,r,-o);return[d,p,w,g]}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=w=>{let N=0;return i>0&&(N+=i/w),i<c&&(N-=(c-i)/(1-w)),a>0&&(N+=a/(w+f)),a<l&&(N-=(l-a)/(m-w)),N},d=w=>{let N=0;return i>0&&(N-=i/(w*w)),i<c&&(N-=(c-i)/((1-w)*(1-w))),a>0&&(N-=a/((w+f)*(w+f))),a<l&&(N-=(l-a)/((m-w)*(m-w))),N};if(p(0)<=0)u=0;else if(p(m)>=0)u=m;else{let w=0,N=m,v=Wp(e,t,n,r,o);u=s?v:v-o,u>w&&u<N||(u=N/2);for(let E=0;E<2e3;E++){let b=p(u);if(b===0)break;b>0?w=u:N=u;let M=u-b/d(u);if(M>w&&M<N||(M=w===0?N/2:N>2*w?Math.sqrt(w*N):w+(N-w)/2),!(M>w&&M<N))break;let x=Math.abs(M-u);if(u=M,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 Wp(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 d=(Math.PI+Math.acos(Math.max(-1,Math.min(1,h/(p*p*p)))))/3;return 2*p*Math.cos(d)-u}function ei(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]=Ja(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 Yp(e,t,n,r,o,s,i){let c=Ce(Ce(ft([e,0],t),ft([n,0],r)),[o,0]),[a,l,f,m]=Ja(e,t,n,r,o),u=ue(ft(Ht(a,l),t),ft(Ht(f,m),r));return s==="mn"&&(u=ft(zt(u,t+r),t+r-1)),Ce(_e(c,c),zt(u,i))[0]}function Ls(e,t,n,r,o){return Ce(Ce(ft([e,0],t),ft([n,0],r)),[o,0])[0]}function Bp(e,t,n,r,o,s){return _o(Ls(e,t,n,r,o),ei(e,t,n,r,o,s))}var _s=new Float64Array(1),Qa=new BigInt64Array(_s.buffer);function rf(e){return _s[0]=Math.abs(e),e<0?-Qa[0]:Qa[0]}function rr(e){return Qa[0]=e<0n?-e:e,e<0n?-_s[0]:_s[0]}function Za(e,t,n,r,o){let s=e(t);if(s===0||Number.isNaN(s))return t;let i=s<0===o,c=rf(i?r:n),a=rf(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(rr(f)),m===0)return rr(f);if(m<0!=l<0||f===c)break;if(a=f,l=m,u*=2n,u>1n<<40n)return rr(a)}if(m<0==l<0||Number.isNaN(m))return rr(a);for(;f-a>1n||a-f>1n;){let h=(a+f)/2n,p=e(rr(h));if(p===0)return rr(h);p<0==l<0?(a=h,l=p):(f=h,m=p)}return Math.abs(m)<Math.abs(l)?rr(f):rr(a)}function sf(e,t,n,r,o,s){let i=Kp(e,t,n,r);if(o===0)return[i,i];let c=Io(o),a=p=>(p===i?0:Io(Bp(e,t,n,r,p,s)))-c,l=(p,d,g,w)=>{if(g===0)return p;if(w===0)return d;let N=Kt(a,p,d,{tol:1e-300,fLower:g,fUpper:w});return s==="wald"?N:Za(v=>Yp(e,t,n,r,v,s,o),N,p,d,g<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 of(e,t,n){let r=t,o=e(t);if(o>=0)return Math.exp(t);let s=1;for(;;){let i=t+n*s;if(Math.abs(i)>745)return n<0?0:1/0;let c=e(i);if(c>=0){if(c===0)return Math.exp(i);let a=n>0?Kt(e,r,i,{tol:2**-60,fLower:o,fUpper:c}):Kt(e,i,r,{tol:2**-60,fLower:c,fUpper:o});return Math.exp(a)}r=i,o=c,s*=2}}function Up(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 Gp(e,t,n,r,o){let[s,i]=Up(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=M=>c===0?[M,0]:c===2?$t(1,-M):ft($t(1,-M),o),f=c===3?1/o:1,m=c===0?Math.min(1,1/o):1,u=c===2?o>1?ft($t(o,-1),o)[0]:0:c===3&&o<1?$t(1,-o)[0]:0,h=M=>{let x=l(M),y=zt(x,o);return[y[0],Ce([1,0],y)[0],x[0],Ce([1,0],x)[0]]},p=e+n,d=c<2?1:-1,g=M=>{let[,x,y,R]=h(M),T=0,S=0;return p>0&&(T+=y>0?p/y:1/0,S-=p/(y*y)),e<t&&(T-=x>0?o*(t-e)/x:1/0,S-=o*(t-e)/x*(o/x)),n<r&&(T-=R>0?(r-n)/R:1/0,S-=(r-n)/(R*R)),[d*T,S*f]},w;if(!(m>u))w=u;else if(!(g(u)[0]>0))w=u;else if(!(g(m)[0]<0))w=m;else{let M=u,x=m;w=a,w>M&&w<x||(w=M+(x-M)/2);for(let y=0;y<2e3;y++){let[R,T]=g(w);if(R===0)break;R>0?M=w:x=w;let S=w-R/T;if(S>M&&S<x||(S=M<=0?x/2:x>2*M?Math.sqrt(M*x):M+(x-M)/2),!(S>M&&S<x))break;let k=Math.abs(S-w);if(w=S,k<=2**-53*w)break}}let[N,v,E,b]=h(w);return[Math.min(1,Math.max(0,N)),Math.min(1,Math.max(0,v)),Math.min(1,Math.max(0,E)),Math.min(1,Math.max(0,b))]}function Hp(e,t,n,r,o){let[s,i,c,a]=Gp(e,t,n,r,o),l=(f,m,u,h)=>u>.5?m*h-(m-f):f-m*u;return _o(l(e,t,s,i),t*s*i)+_o(l(n,r,c,a),r*c*a)}function af(e,t,n,r,o){let s=e/t/(n/r);return lf(s,o,i=>Hp(e,t,n,r,i))}function Xp(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 Qp(e,t,n,r,o){let[s,i]=Xp(e,t,n,r,o),c=e-t*s;return _o(c,t*s*(1-s))+_o(c,r*i*(1-i))}function cf(e,t,n,r,o){let s=e*(r-n)/((t-e)*n);return lf(s,o,i=>Qp(e,t,n,r,i))}function lf(e,t,n){if(t===0)return[e,e];let r=Io(t),o=l=>n(l)-t,s=l=>l>0&&l<e?Za(o,l,Number.MIN_VALUE,e,!1):l,i=l=>l>e&&l<1/0?Za(o,l,e,Number.MAX_VALUE,!0):l;if(e===0||e===1/0){let l=u=>Io(n(u))-r,f=l(1),m=f===0?1:tr(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:Io(n(Math.exp(l))))-r;return[s(of(a,c,-1)),i(of(a,c,1))]}var Zp=["method","confLevel","sides"],Jp=["method","confLevel","sides"],ed=["method","confLevel"],td=["ac","wald","score","mn","mee"];function ti(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,d=i*Math.sqrt(u*(1-u)/(t+2)+h*(1-h)/(r+2));[f,m]=[Math.max(-1,p-d),Math.min(1,p+d)];break}case"score":{let[u,h]=Is(e,t,i),[p,d]=Is(n,r,i);f=l-i*Math.sqrt(u*(1-u)/t+d*(1-d)/r),m=l+i*Math.sqrt(h*(1-h)/t+p*(1-p)/r);break}case"mn":case"mee":[f,m]=sf(e,t,n,r,s.chisq,o);break}return[Math.min(f,m),Math.max(f,m)]}function nd(e,t,n,r,o={}){V(o,Zp,"binomDiffCI");let s=xe(o.method,td),i=qo(o.confLevel,o.sides),[c,a]=$n(e,t,"x1","n1"),[l,f]=$n(n,r,"x2","n2"),m=c/a-l/f,[u,h]=ti(c,a,l,f,s,i);return i.sides==="left"?h=1:i.sides==="right"&&(u=-1),H("binomDiffCI",{estimate:m,confInt:[u,h],confLevel:o.confLevel??.95,method:s,sides:i.sides,warnings:[]})}var rd=["katz.log","koopman"];function Cs(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 od(e,t,n,r,o={}){V(o,Jp,"binomRatioCI");let s=xe(o.method,rd),i=qo(o.confLevel,o.sides),[c,a]=$n(e,t,"x1","n1"),[l,f]=$n(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,Cs(.5,a,l,f,m)[1]]:l===0?[h,p]=[Cs(c,a,.5,f,m)[0],1/0]:c===a&&l===f?[h,p]=Cs(a-.5,a,f-.5,f,m):[h,p]=Cs(c,a,l,f,m):[h,p]=af(c,a,l,f,i.chisq),i.sides==="left"?p=1/0:i.sides==="right"&&(h=0),H("binomRatioCI",{estimate:u,confInt:[h,p],confLevel:o.confLevel??.95,method:s,sides:i.sides,warnings:[]})}var sd=["midp","fisher","wald","score"],ad={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"},id=2**23,cd=2**20;function ff(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=ue(r,[e[o],0]));return r[0]}function ld(e,t={}){V(t,ed,"oddsRatio");let n=xe(t.method,sd);if(e.length!==2||e.some(S=>S.length!==2))throw new TypeError("'table' must be a 2 x 2 table");let r=t.confLevel??.95,{alpha:o,kappa:s,chisq:i}=qo(r,void 0),c=e.map((S,k)=>Array.from(S,(L,_)=>Br(L,`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),d=p-h+1;if(n==="midp"||n==="fisher"){if(d>id)throw new RangeError(`the exact distribution of the 2 x 2 table has ${d} 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 g,w,N;try{g=on(a-1,a+f,l+m,a+l,!0,!1)+.5*gn(a,a+f,l+m,a+l,!1),w=on(a,a+f,l+m,a+l,!1,!1)+.5*gn(a,a+f,l+m,a+l,!1),N=d<=cd?La(new Wr(a+f,l+m,a+l),a,1):Cl(a,a+f,l+m,a+l)}catch(S){throw S instanceof RangeError?new RangeError(`the exact p-values of the table are beyond reach: ${S.message}`):S}let v=2*Math.min(g,w),E=[a+l,f+m],b=[a+f,l+m],M=0,x=!1;for(let S=0;S<2;S++)for(let k=0;k<2;k++){let L=E[k]*b[S]/u;L<5&&(x=!0);let _=Math.abs(c[k][S]-L);M+=_*_/L}let y=gt(M,.5,2,!1,!1),R,T;switch(n){case"wald":{R=a*m/(l*f);let S=s*Math.sqrt(1/a+1/l+1/f+1/m);T=[Math.exp(Math.log(R)-S),Math.exp(Math.log(R)+S)];break}case"fisher":{let S=je(()=>So(c,{confLevel:r}));R=S.estimate,T=S.confInt;break}case"score":{R=a*m/(l*f),T=(a===0||m===0)&&(l===0||f===0)?[0,1/0]:cf(a,a+l,f,f+m,i);break}case"midp":{let S=new Wr(a+f,l+m,a+l),k=F=>{if(F===1)return[g,w];let C=S.density(F),D=a-h,A=.5*C[D];return[ff(C,0,D-1)+A,ff(C,D+1,C.length-1)+A]},L=(F,C)=>{let D=F(1);return D===0?1:tr(F,C?D>0:D<0)};R=a===h?0:a===p?1/0:L(F=>k(F)[0]-.5,!1);let _=a===h?0:L(F=>k(F)[1]-o/2,!0),I=a===p?1/0:L(F=>k(F)[0]-o/2,!1);T=[_,I];break}}return H("oddsRatio",{estimate:R,confInt:T,confLevel:r,method:ad[n],pValue:{midp:v,fisher:N,chiSquare:y},warnings:x?["Chi-squared approximation may be incorrect"]:[]})}var fd=["epsilon","paired","varEqual","confLevel","alpha"],md=["margin","method","alpha"];function mf(e,t){if(!(typeof e=="number"&&e>0&&e<.5))throw new RangeError(`'${t}' must be a single number in (0, 0.5)`)}function ud(e,t,n={}){V(n,fd,"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)mf(n.alpha,"alpha"),o=1-n.alpha,s=n.alpha;else{if(o=n.confLevel??.95,It(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=je(()=>vn(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=vt((r-Math.abs(m))/l,f,!1,!1),h=s<1e-8?xt(s,f,!1,!1):xt(o,f,!0,!1),p=[m-h*l,m+h*l];return H("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 hd=["mn","mee","wald"];function pd(e,t,n,r,o={}){V(o,md,"binomDiffEquivalenceTest");let s=xe(o.method,hd),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;mf(c,"alpha");let[a,l]=$n(e,t,"x1","n1"),[f,m]=$n(n,r,"x2","n2"),u=a/l-f/m,h=E=>Ls(a,l,f,m,E)/Math.sqrt(ei(a,l,f,m,E,s)),p=h(-i),d=h(i),g=[ie(p,{lowerTail:!1}),ie(d)],w=Math.max(g[0],g[1]),N=1-2*c,v=ti(a,l,f,m,s,Ha(2*c,N));return H("binomDiffEquivalenceTest",{method:s,estimate:u,margin:i,alpha:c,statistic:[p,d],statisticName:"z",pValues:g,pValue:w,result:g[0]<c&&g[1]<c?"rejected":"not rejected",confInt:v,confLevel:N,warnings:[]})}var Wt=new WeakMap;function Fs(e){let t=(n,r)=>n.weights===void 0?Array.from(r):r.filter((o,s)=>n.weights[s]!==0);return Re(e.flatMap(n=>[t(n,n.fittedValues),t(n,n.residuals)])).k}function Ds(e,t){return e.map(n=>{if(n.rss>=dd&&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 dd=2**-1022;function ri(e,t){return e.y.flatMap((n,r)=>(e.weights?.[r]??1)===0?[]:[[e.weights?.[r]??1,$(n,-t)]])}function Ps(e){let t=Math.floor(Math.log2(e.reduce((n,[r])=>Math.max(n,r),0)));return t-(t%2+2)%2}function yr(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=Ps(e),s=r(o)),!($(t,-o)>=1e-24*s)}function uf(e){let t=Fs([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:Ds([e],t)[0],k:t}}var hf=(e,t,n)=>wt(e,t,n,!1,!1),oi="essentially perfect fit for \u20181\u2019: summary may be unreliable";function si(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 ai=new WeakMap;function gd(e){let t=uf(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 ni(e){let t=gd(e),{names:n,rdf:r,k:o,perfect:s}=t,{rows:i,rss:c}=t;if(r>0){let d=ri(e,o);yr(d,c)&&(c=0,i=i.map(g=>yr(d,g.ss)?{...g,ss:0}:g))}let a=c/r,l=d=>$(d,2*o),f=i.map(({df:d,ss:g})=>{let w=g/d/a;return[d,l(g),l(g/d),w,hf(w,d,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:d,ss:g})=>[d,g]);return p.push([r,c]),Wt.set(h,{colNames:["Df","Sum Sq"],rowNames:u,table:p}),h}function wd(e){let t=e.map(f=>f.dfResidual),n=Fs(e),r=Ds(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,d]=u>0?[e[m-1],e[m]]:[e[m],e[m-1]],g=N=>N.filter((v,E)=>(p.weights?.[E]??1)!==0),w=u===0||!gs(g(p.x),g(d.x))?NaN:h/u/i;return w<0&&(w=NaN),[t[m],o(r[m]),u,o(h),w,Number.isNaN(w)?NaN:hf(w,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 Os(...e){let t=[];for(let n of e)for(let r of n??[])t.includes(r)||t.push(r);return t}function ii(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=ni(e),a={...c,warnings:Os(e.warnings,c.warnings)},l=Wt.get(c);return l!==void 0&&Wt.set(a,l),H("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?ni(e):wd(o);return i.warnings=Os(...n.map(c=>c.warnings),s,i.warnings),H("anova",i,...n)}function ci(e){if(typeof e=="object"&&e!==null&&e.multistratum===!0){let i=e,c=Os(i.warnings,ai.get(i)),a={strata:i.strata.filter(f=>f.stratum!=="Error: (Intercept)").map(f=>({...f})),warnings:c},l=Wt.get(i);return Array.isArray(l)&&Wt.set(a,l.filter((f,m)=>i.strata[m].stratum!=="Error: (Intercept)")),H("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=ni(t),r=uf(t),o=si(t.fittedValues.map(i=>$(i,-r.k)),r.rss,r.rdf),s=Os(t.warnings,n.warnings,o?[oi]:[]);if(t.dfResidual>0){let i={...n,warnings:s},c=Wt.get(n);return c!==void 0&&Wt.set(i,c),H("aovSummary",i,e)}return H("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 bd=["pooledSd","mu","paired","ci","alternative","adjust"],vd=["pooledSd","mu","paired","ci","alternative"],Nd=["generalized","partial","ci","alternative"],yd=["partial","ci","alternative"],Md=["partial","ci","alternative"];function mi(e){if(e!==null&&!(e>0&&e<1))throw new RangeError("ci must be a single numeric value between (0, 1)")}var Ed="Missing values detected. NAs dropped.",xd=`'y' is numeric but has only 2 unique values.
1
+ /*! rStats.js 1.18.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 en,b as Vn,c as Zn,d as dr,e as ws,f as va}from"./chunks/chunk-5CTWYFMB.js";import{a as xe,b as qe,c as bn,d as it,e as No,f as bh,g as vh,h as wa,i as ba,j as Nh,k as il,l as vs,m as Jn,n as Ns,o as fl,p as wr,q as ml,r as Mh,s as Eh,t as Ma,u as Ea,v as pl,w as dl,x as gl,y as xa,z as kh}from"./chunks/chunk-HMLYEFQN.js";import{a as gn,c as on,h as $r,i as jc,m as Kc,n as zr,o as Wc,s as go,t as is,u as cs,v as ls}from"./chunks/chunk-6I23C2YJ.js";import{b as Qc}from"./chunks/chunk-EC77KAAM.js";import{a as Ra,e as Ta}from"./chunks/chunk-NDZVVNBB.js";import{a as ms,b as Qn,c as vo,d as el,e as tl,f as us,g as Ge,h as an,i as wn,j as It,k as ol,l as yh,n as Ms,o as Mo}from"./chunks/chunk-MKZH65DK.js";import{a as Es}from"./chunks/chunk-VATAUY7N.js";import{a as as,b as Yc}from"./chunks/chunk-HGAEVPVS.js";import"./chunks/chunk-SCFFHEDZ.js";import"./chunks/chunk-QUD3374N.js";import{a as zc,d as ss}from"./chunks/chunk-ICDW3I2F.js";import{b as qh,c as Ih,d as _h}from"./chunks/chunk-HRRCC2BD.js";import{f as ll,g as br,h as er,i as ys,k as yo,l as ul,m as ya,n as xh,o as Rh,q as Th,r as Sh,s as Ah}from"./chunks/chunk-IOJVFYHW.js";import{b as Mt,c as Cn}from"./chunks/chunk-LCIX76FR.js";import{b as Eo}from"./chunks/chunk-FLFNMSHI.js";import{a as nl,c as rl,e as pr}from"./chunks/chunk-Z33Q545U.js";import{b as ga}from"./chunks/chunk-WYVUCQO3.js";import{a as at,b as hs,c as ps}from"./chunks/chunk-CTZUYKVL.js";import{c as gs,d as Na}from"./chunks/chunk-NOTJ45G3.js";import{a as gr,b as Kr}from"./chunks/chunk-RXYZQ3UX.js";import"./chunks/chunk-H6Z7JVSD.js";import{d as rt}from"./chunks/chunk-JPWYGTUN.js";import{a as jt,d as al,e as ds,k as bs,o as un,p as hl}from"./chunks/chunk-IBHMM2SL.js";import{a as Xn,b as Jc,g as jr}from"./chunks/chunk-GIK34P23.js";import{d as Zo,e as Jo,g as es,h as Pr,m as fn,n as On,s as da,t as vt,u as xt,w as dn,x as rn}from"./chunks/chunk-IW4S634O.js";import{A as xn,B as Vr,E as Yn,i as gt,l as Qo,u as Pc,x as wt,y as ln}from"./chunks/chunk-LY5XGFPU.js";import{a as cl}from"./chunks/chunk-IKMW63BE.js";import{a as Rn,c as fs,f as sl}from"./chunks/chunk-MXZ64QME.js";import{b as Bc,c as Uc,d as Gc,e as Hc,f as Xc,i as wo,l as wh,n as H,o as je,p as mt}from"./chunks/chunk-N7BUE54L.js";import{a as bo,b as Pn,c as Zc,d as Ot,e as Rt}from"./chunks/chunk-XQCHC3SH.js";import{a as At,b as Hn,c as sn}from"./chunks/chunk-KJMLQHCY.js";import"./chunks/chunk-YXMHWS5R.js";import{h as _c,i as Lc,k as Cc,u as Oc,v as Fc,z as Dc}from"./chunks/chunk-53WQDB3C.js";import{f as ha,r as Xt,s as ie,t as oe}from"./chunks/chunk-GEHMND3C.js";import{A as Dt,B as os,C as Re,e as Fn,f as mn,g as Dn,h as $,i as et,j as Bn,k as st,l as ot,m as Un,n as Vc,p as ts,q as Et,r as ns,s as $c,t as rs,u as pa,v as Ie,w as bt,x as pn,y as Ct,z as Gn}from"./chunks/chunk-HG4LJBNV.js";import{b as $t,c as Ht,d as ue,e as qc,f as Ce,g as _e,h as zt,i as lt,j as ft,k as Ic,l as V,m as Ft}from"./chunks/chunk-MVPHISOV.js";var wl=["alternative","mu","paired","varEqual","confLevel"];function vn(e,t,n={}){if(typeof e!="string"&&at(t)&&V(t,wl,"tTest"),V(n,wl,"tTest"),typeof e=="string")return Lh(e,t,n);let r;t!==void 0&&!ArrayBuffer.isView(t)&&!Array.isArray(t)?n=t:r=t;let o=Ge(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(an(a),i&&r===void 0)throw new TypeError("y is required when paired is true");let l,f,m;if(i){let[k,L]=Gn(Array.from(e),Array.from(r));({d:l,k:m}=ns(k,L))}else{let k=Re(r===void 0?[Ct(Array.from(e))]:[Ct(Array.from(e)),Ct(Array.from(r))]);m=k.k,l=k.groups[0],f=k.groups[1]}let u=$(s,-m),h,p,d,g,w,N,v,E=l.length,b=f!==void 0&&c;if(E<1||!b&&E<2)throw new Error("not enough 'x' observations");let M=st(l),x=E>1?ot(l):NaN;if(f===void 0){if(p=E-1,d=Math.sqrt(x/E),d===0||d<10*Number.EPSILON*Math.abs(M))throw new Error("data are essentially constant");h=(M-u)/d,N=i?"Paired t-test":"One Sample t-test",g=M,w=i?"mean difference":"mean of x",v=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 L=st(f),_=k>1?ot(f):NaN;if(N=`${c?"":"Welch"} Two Sample t-test`,g=[M,L],w=["mean of x","mean of y"],v="difference in means",c){p=E+k-2;let I=0;E>1&&(I+=(E-1)*x),k>1&&(I+=(k-1)*_),I/=p,d=Math.sqrt(I*(1/E+1/k))}else{let I=x/E,F=_/k;d=Math.sqrt(Math.sqrt(I)**2+Math.sqrt(F)**2),p=rs([I,F],[E-1,k-1])}if(d===0||d<10*Number.EPSILON*Math.max(Math.abs(M),Math.abs(L)))throw new Error("data are essentially constant");h=(M-L-u)/d}let y=typeof g=="number"?g:g[0]-g[1],R=(k,L)=>{let _=u+(h+L*k)*d;return Number.isFinite(_)&&Math.max(Math.abs(u),Math.abs(h*d))<=16*(Math.abs(y)+k*d)?_:y+L*k*d},T,S;if(o==="less")T=vt(h,p,!0,!1),S=[-1/0,R(xt(a,p,!0,!1),1)];else if(o==="greater")T=vt(h,p,!1,!1),S=[R(xt(a,p,!0,!1),-1),1/0];else{T=2*vt(-Math.abs(h),p,!0,!1);let k=1-a,L=k/2<1e-8?xt(k/2,p,!1,!1):xt(1-k/2,p,!0,!1);S=[R(L,-1),R(L,1)]}return S=[$(S[0],m),$(S[1],m)],g=typeof g=="number"?$(g,m):g.map(k=>$(k,m)),d=$(d,m),{method:N,alternative:o,statistic:h,statisticName:"t",parameter:p,parameterName:"df",pValue:T,confInt:S,confLevel:a,estimate:g,estimateNames:w,nullValue:s,nullValueName:v,stdErr:d,stdError:d,warnings:[]}}function Lh(e,t,n){if(n.paired!==void 0)throw new Error("cannot use 'paired' in formula method");let r=ms(e,t,!0);if(r.kind==="one")return vn(r.x,n);if(r.kind==="paired")return vn(r.x,r.y,{...n,paired:!0});let[o,s]=r.levels;return{...vn(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 Ch=["correct","p","rescaleP"],Oh=["confLevel"],Fh=["alternative","rowScores","colScores"];function Qt(e){let t=[0,0];for(let n of e)t=ue(t,[n,0]);return t}function Aa(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 ka=e=>e.length>0&&typeof e[0]!="number",qa=(e,t)=>Number.isNaN(e)?NaN:gt(e,t/2,2,!1,!1);function bl(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 xo(e,t){return e.length>0&&Array.isArray(e[0])?e.map((n,r)=>n.map((o,s)=>bl(o,t[r][s]))):e.map((n,r)=>bl(n,t[r]))}var Dh=["none","williams","yates"],Nl="Log likelihood ratio (G-test)";function Ph(e,t={}){V(t,Ch,"gTest");let n=xe(t.correct,Dh);if(typeof e!="object"||e===null||typeof e.length!="number")throw new TypeError("'x' must be an array of counts or of rows");if(ka(e)){let o=Aa(e);return vl(o.flat()),o.length===1?H("gTest",Sa(o[0],n,t)):o[0].length===1?H("gTest",Sa(o.map(s=>s[0]),n,t)):H("gTest",Vh(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 vl(r),H("gTest",Sa(r,n,t))}function vl(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 yl(e,t){return e<1/0?0:Math.ceil(Math.log2(t))+1}function Ml(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=ue(n,Lc(o,s))}return n[0]+n[1]}function Vh(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=Qt(e.flat()),s=yl(o[0],n*r),i=s===0?e:e.map(v=>v.map(E=>$(E,-s))),c=s===0?o:Qt(i.flat()),a=i.map(v=>Qt(v)),l=i[0].map((v,E)=>Qt(i.map(b=>b[E]))),f=a.map(v=>l.map(E=>_e(v,lt(E,c)))),m=i;if(t==="yates"){let[[v,E],[b,M]]=i,x=v*M-E*b>0?-.5:.5,y=$(.5,-s),R=x<0?-y:y;m=[[v+R,E-R],[b-R,M+R]]}let u=Ml(m.flat(),f.flat()),h=Number.isNaN(u)?["NaNs produced"]:[],p=1;if(t==="williams"){if(a.some(M=>M[0]===0)||l.some(M=>M[0]===0))throw new RangeError("Williams' correction is not defined for a table with an empty row or column");let v=c[0]+c[1],E=a.reduce((M,x)=>M+1/(x[0]+x[1]),0),b=l.reduce((M,x)=>M+1/(x[0]+x[1]),0);p=1+$((v*E-1)*(v*b-1)/(6*v*(n-1)*(r-1)),-s)}let d=$(2*u,s)/p,g=(n-1)*(r-1),w=f.map(v=>v.map(E=>$(E[0]+E[1],s)));return{method:`${Nl} test of independence ${t==="none"?"without correction":t==="williams"?"with Williams' correction":"with Yates' correction"}`,statistic:d,statisticName:"G",parameter:g,parameterName:"X-squared df",pValue:qa(d,g),observed:s===0?m:m.map(v=>v.map(E=>$(E,s))),expected:w,ftResiduals:xo(e,w),warnings:h}}function Sa(e,t,n){let r=e.length;if(r<2)throw new RangeError("'x' must have at least 2 elements");if(t==="yates"&&r!==2)throw new RangeError("Yates' correction requires 2 data values");let o=n.rescaleP??!1;if(typeof o!="boolean")throw new TypeError("'rescaleP' must be a boolean");let s=n.p===void 0?e.map(()=>1/r):Array.from(n.p);if(s.length!==r)throw new RangeError("'x' and 'p' must have the same number of elements");if(s.some(v=>typeof v!="number"||Number.isNaN(v)))throw new RangeError("'p' must not contain missing values");if(s.some(v=>v<0))throw new RangeError("probabilities must be non-negative");if(s.some(v=>v===1/0))throw new RangeError("probabilities must be finite");let i=Qt(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=Qt(e),a=yl(c[0],r),l=a===0?e:e.map(v=>$(v,-a)),f=a===0?c:Qt(l),m=s.map(v=>o?lt(zt(f,v),i):zt(f,v)),u=m.map(v=>v[0]+v[1]),h=l;if(t==="yates"){let v=$(.5,-a),E=l[0]-u[0];E>$(.25,-a)?h=[l[0]-v,l[1]+v]:-E>$(.25,-a)&&(h=[l[0]+v,l[1]-v])}let p=Ml(h,m);if(!o&&!Number.isNaN(p)){let v=f[0]+f[1];p-=v*(i[0]-1+i[1])}let d=Number.isNaN(p)?["NaNs produced"]:[],g=t==="williams"?1+(r+1)/(6*$(f[0]+f[1],a)):1,w=$(2*p,a)/g,N=u.map(v=>$(v,a));return{method:`${Nl} goodness of fit test`,statistic:w,statisticName:"G",parameter:r-1,parameterName:"X-squared df",pValue:qa(w,r-1),observed:a===0?h:h.map(v=>$(v,a)),expected:N,ftResiduals:xo(e,N),warnings:d}}function $h(e){if(typeof e!="object"||e===null||!ka(e))throw new RangeError("'x' must be square with at least two rows and columns");let t=Aa(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 zh(e){let t=$h(e),n=Qt(t.flat())[0],r=n>2**400||n>0&&n<2**-400?Math.round(Math.log2(n)):0,o=r===0?t:t.map(b=>b.map(M=>$(M,-r))),s=o.length,i=o.map(b=>Qt(b)),c=o.map((b,M)=>Qt(o.map(x=>x[M]))),a=o.map((b,M)=>o[M].some((x,y)=>y!==M&&x>0)||o.some((x,y)=>y!==M&&x[M]>0)).flatMap((b,M)=>b?[M]:[]),l=a.length;if(l<2)throw new RangeError("Too many equal marginals, cannot compute");let f=a.map((b,M)=>M),m=b=>f[b]===b?b:f[b]=m(f[b]);for(let b=0;b<l;b++)for(let M=b+1;M<l;M++)o[a[b]][a[M]]+o[a[M]][a[b]]>0&&(f[m(b)]=m(M));if(a.some((b,M)=>m(M)!==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(b=>{let M=ue(i[b],[-c[b][0],-c[b][1]]);return M[0]+M[1]}),p=a.slice(0,u).map(b=>a.slice(0,u).map(M=>{if(b!==M)return-(o[b][M]+o[M][b]);let x=0;for(let y=0;y<s;y++)y!==b&&(x+=o[b][y]+o[y][b]);return x})),d=p.map(b=>b.map(()=>0)),g=[];for(let b=0;b<u;b++){let M=p[b][b];for(let x=0;x<b;x++)M-=d[b][x]*d[b][x]*g[x];g.push(M);for(let x=b+1;x<u;x++){let y=p[x][b];for(let R=0;R<b;R++)y-=d[x][R]*d[b][R]*g[R];d[x][b]=y/M}}let w=0,N=[];for(let b=0;b<u;b++){let M=h[b];for(let x=0;x<b;x++)M-=d[b][x]*N[x];N.push(M),w+=M*M/g[b]}let v=Qt(a.flatMap(b=>a.map(M=>t[b][M]))),E=l-1;return w=$(w,r),H("stuartMaxwellTest",{method:"Stuart-Maxwell test",statistic:w,statisticName:"chi-squared",parameter:E,parameterName:"df",pValue:qa(w,E),N:v[0]+v[1],warnings:[]})}function jh(e,t={}){V(t,Oh,"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=El(e),o=Qt(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(b=>b.map(M=>$(M,-s))),{pc:c,pd:a}=Kh(i),l=[0,0],f=[0,0];i.forEach((b,M)=>b.forEach((x,y)=>{l=ue(l,zt(c[M][y],x)),f=ue(f,zt(a[M][y],x))})),l=[l[0]/2,l[1]/2],f=[f[0]/2,f[1]/2];let m=ue(l,f),u=lt(Ce(l,f),m)[0],h=_e(m,m),p=i.map((b,M)=>b.map((x,y)=>2*lt(Ce(_e(f,c[M][y]),_e(l,a[M][y])),h)[0])),d=Math.max(...p.flat().map(Math.abs)),g=d>0&&d<1/0?Math.floor(Math.log2(d)):0,w=0;i.forEach((b,M)=>b.forEach((x,y)=>{let R=$(p[M][y],-g);w+=x*R*R}));let N=$(Math.sqrt(w),g-s/2),v=(1-n)/2,E=v<1e-8?oe(v,{lowerTail:!1}):oe(1-v);return H("goodmanKruskalGamma",{estimate:u,confInt:[Math.max(-1,u-E*N),Math.min(1,u+E*N)],confLevel:n,ase:N,warnings:[]})}function El(e){if(typeof e!="object"||e===null||!ka(e))throw new TypeError("'table' must be an array of rows");let t=Aa(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 Kh(e){let t=e.length,n=e[0].length,r=[0,0],o=e.map(p=>{let d=[],g=r;for(let w=0;w<n;w++)d.push(g),g=ue(g,[p[w],0]);return d}),s=e.map(p=>{let d=new Array(n),g=r;for(let w=n-1;w>=0;w--)d[w]=g,g=ue(g,[p[w],0]);return d}),i=p=>{let d=[],g=new Array(n).fill(r);for(let w=0;w<t;w++)d.push(g),g=g.map((N,v)=>ue(N,p[w][v]));return d},c=p=>{let d=new Array(t),g=new Array(n).fill(r);for(let w=t-1;w>=0;w--)d[w]=g,g=g.map((N,v)=>ue(N,p[w][v]));return d},a=i(o),l=c(s),f=i(s),m=c(o),u=a.map((p,d)=>p.map((g,w)=>ue(g,l[d][w]))),h=f.map((p,d)=>p.map((g,w)=>ue(g,m[d][w])));return{pc:u,pd:h}}function Wh(e,t={}){V(t,Fh,"lblTest");let n=Ge(t.alternative),r=El(e),o=r.length,s=r[0].length,i=(R,T,S)=>{if(R===void 0)return Array.from({length:T},(L,_)=>_+1);let k=Array.from(R);if(k.length!==T)throw new RangeError(`'${S}' must have one score for each ${S==="rowScores"?"row":"column"}`);if(k.some(L=>typeof L!="number"||!Number.isFinite(L)))throw new RangeError(`'${S}' must be finite numbers`);return k},c=i(t.rowScores,o,"rowScores"),a=i(t.colScores,s,"colScores"),f=Qt(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=>Qt(R)).map(R=>R[0]+R[1]),h=m[0].map((R,T)=>Qt(m.map(S=>S[T]))).map(R=>R[0]+R[1]),p=u.reduce((R,T)=>R+T,0);if(!(f>0||p>1))throw new RangeError("the table must have a total above 1");let d=u.reduce((R,T,S)=>R+T*c[S],0)/p,g=h.reduce((R,T,S)=>R+T*a[S],0)/p,w=c.map(R=>R-d),N=a.map(R=>R-g),v=u.reduce((R,T,S)=>R+T*w[S]*w[S],0),E=h.reduce((R,T,S)=>R+T*N[S]*N[S],0),b=0;if(m.forEach((R,T)=>R.forEach((S,k)=>b+=S*w[T]*N[k])),!(v>0&&E>0))throw new RangeError("the row and column scores must vary over the observations (the variance of the statistic is 0)");let M=b/Math.sqrt(v)/Math.sqrt(E),x=f===0?Math.sqrt(p-1)*M:$(Math.sqrt(p-$(1,-f))*M,f/2),y=n==="less"?ie(x):n==="greater"?ie(x,{lowerTail:!1}):2*ie(-Math.abs(x));return H("lblTest",{method:"Linear-by-Linear Association Test",alternative:n,statistic:x,statisticName:"Z",pValue:y,warnings:[]})}var xl=["correct","p","rescaleP","simulatePValue","B","nMinus1"];function hn(e){let t=[0,0],n=0;for(let r of e)t=ue(t,[r,0]),n+=r;return Number.isFinite(n)?t[0]:n}function Yh(e,t,n){return at(t)&&V(t,xl,"chisqTest"),V(n,xl,"chisqTest"),Array.isArray(t)?H("chisqTest",Bh(e,t,n??{})):H("chisqTest",Uh(e,t??{}))}var Sl="Chi-squared approximation may be incorrect";function Bh(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(Rn),c=[...new Set(s)].sort(Rn);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])]++),{..._l(f,n),rowLevels:i,colLevels:c}}function Uh(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?Ia(r[0],t):o===1?Ia(r.map(s=>s[0]),t):_l(r,t)}return Ia(Array.from(e),t)}function Al(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 kl(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 ql(e,t){return Number.isNaN(e)?NaN:gt(e,t/2,2,!1,!1)}function Il(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 Ia(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(d=>!(d>=0&&d<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=hn(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(d=>d<0))throw new RangeError("probabilities must be non-negative");if(o.some(d=>d===1/0))throw new RangeError("probabilities must be finite");if(t.rescaleP??!1){let d=hn(o),g=d<1/0?0:Math.ceil(Math.log2(n)),w=g===0?o:o.map(v=>$(v,-g)),N=g===0?d:hn(w);o=w.map(v=>v/N)}else if(Math.abs(hn(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(d=>$(d,-s)),c=s===0?r:hn(i),a=o.map(d=>c*d),l=s===0?a:a.map(d=>$(d,s));Il(l);let f=o.map((d,g)=>a[g]*(1-d)),m=$(hn(i.map((d,g)=>Dt(d-a[g],a[g]))),s),u="Chi-squared test for given probabilities",h=n-1,p;if(t.simulatePValue??!1){let d=Al(t);if(!Number.isInteger(r))throw new RangeError("the simulation needs counts summing to an integer");u+=bo(d),h=NaN,p=kl(Gh(r,o,l,d),m,d)}else p=ql(m,h);return{method:u,statistic:m,statisticName:"X-squared",parameter:h,parameterName:"df",pValue:p,observed:e,expected:l,residuals:i.map((d,g)=>$((d-a[g])/Math.sqrt(a[g]),s/2)),stdres:i.map((d,g)=>$((d-a[g])/Math.sqrt(f[g]),s/2)),ftResiduals:xo(e,l),warnings:Number.isFinite(h)&&l.some(d=>d<5)?[Sl]:[]}}function Gh(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=Qc(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 Hh(e,t,n,r){let o=e.length,s=t.length,i=e.reduce((f,m)=>f+m,0);if(i>is)throw new RangeError("the simulation needs a total count of at most 16777216 (2^24)");let c=cs(i),a=new Int32Array(o*s),l=new Float64Array(Math.trunc(r));for(let f=0;f<l.length;f++){ls(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 _l(e,t){let n=t.nMinus1??!1;if(typeof n!="boolean")throw new TypeError("'nMinus1' must be a boolean");if(n&&t.correct===!0)throw new RangeError("'nMinus1' uses no continuity correction: leave 'correct' out or set it to false");if(n&&(t.simulatePValue??!1))throw new RangeError("'nMinus1' cannot be combined with 'simulatePValue'");let r=!n&&(t.correct??!0),o=e.length,s=e[0].length,i=e.flat();if(i.some(v=>!(v>=0&&v<1/0)))throw new RangeError("all entries of 'x' must be non-negative and finite");let c=hn(i);if(!(c>0))throw new RangeError("at least one entry of 'x' must be positive");if(n&&!(c>1))throw new RangeError("'nMinus1' needs a table with a total above 1");let a=e.map(v=>hn(v)),l=e[0].map((v,E)=>hn(e.map(b=>b[E]))),f=a.map(v=>l.map(E=>os(v,E,c)));Il(f.flat());let m=e.map((v,E)=>v.map((b,M)=>b-f[E][M])),u="Pearson's Chi-squared test",h,p=(o-1)*(s-1),d,g=(t.simulatePValue??!1)&&a.every(v=>v>0)&&l.every(v=>v>0),w=!g&&r&&o===2&&s===2?Math.min(.5,...m.flat().map(Math.abs)):0,N=[];if((t.simulatePValue??!1)&&!g&&N.push("cannot compute simulated p-value with zero marginals"),g){if(e.some(b=>b.some(M=>!Number.isInteger(M))))throw new RangeError("the simulation needs integer counts");let v=Al(t);u+=bo(v),h=hn(m.flatMap((b,M)=>b.map((x,y)=>Dt(x,f[M][y])))),p=NaN;let E=Hh(a.map(b=>Math.trunc(b)),l.map(b=>Math.trunc(b)),f,v);d=kl(E,h,v)}else if(w>0)u+=" with Yates' continuity correction",h=hn(m.flatMap((v,E)=>v.map((b,M)=>Dt(Math.abs(b)-w,f[E][M]))));else if(h=hn(m.flatMap((v,E)=>v.map((b,M)=>Dt(b,f[E][M])))),n){u="Pearson's 'N-1' Chi-squared test";let v=h*(c-1);h=Number.isFinite(v)?v/c:h*((c-1)/c)}return{method:u,statistic:h,statisticName:"X-squared",parameter:p,parameterName:"df",pValue:d??ql(h,p),observed:e,expected:f,residuals:m.map((v,E)=>v.map((b,M)=>b/Math.sqrt(f[E][M]))),stdres:Xh(e,c,a,l,f,m),ftResiduals:xo(e,f),warnings:Number.isFinite(p)&&f.some(v=>v.some(E=>E<5))?[...N,Sl]:N}}function Xh(e,t,n,r,o,s){if(!Number.isFinite(t))return s.map((p,d)=>p.map((g,w)=>g/Math.sqrt(o[d][w]*(1-n[d]/t)*(1-r[w]/t))));let i=e.map(p=>Rl(p)),c=e[0].map((p,d)=>Rl(e.map(g=>g[d]))),a=Zh(i),l=Tl(i),f=Tl(c),m=l.map(p=>lt(p,a)),u=c.map(p=>lt(p,a)),h=f.map(p=>lt(p,a));return e.map((p,d)=>p.map((g,w)=>{let N=_e(i[d],u[w]),v=Ce([g,0],N),E=v[0]+v[1],b=_e(_e(N,m[d]),h[w]),M=b[0]+b[1];if(M>=Qh||E===0)return E/Math.sqrt(M);let x=N[0]+N[1],y=m[d][0]+m[d][1],R=h[w][0]+h[w][1];return E/Math.sqrt(x)/Math.sqrt(y)/Math.sqrt(R)}))}var Qh=2**-1022;function Rl(e){let t=[0,0];for(let n of e)t=ue(t,[n,0]);return t}function Zh(e){let t=[0,0];for(let n of e)t=ue(t,n);return t}function Tl(e){let t=e.length,n=new Array(t+1);n[t]=[0,0];for(let s=t-1;s>=0;s--)n[s]=ue(e[s],n[s+1]);let r=[],o=[0,0];for(let s=0;s<t;s++)r.push(ue(o,n[s+1])),o=ue(o,e[s]);return r}var Jh=Math.pow(Number.EPSILON,.25);function Kt(e,t,n,r={}){let o={tol:r.tol??Jh,maxiter:r.maxIter??1e3};return r.fLower!==void 0&&(o.fLower=r.fLower),r.fUpper!==void 0&&(o.fUpper=r.fUpper),pr(e,t,n,o).root}function tr(e,t){let n=a=>e(Math.exp(a)),r=0,o=n(0),s=t?-1:1,i=n(s);for(;i!==0&&Math.sign(i)===Math.sign(o);){if(Math.abs(s)>745)return t?0:1/0;r=s,o=i,s*=2,i=n(s)}if(i===0)return Math.exp(s);let c=r<s?Kt(n,r,s,{tol:2**-50,fLower:o,fUpper:i}):Kt(n,s,r,{tol:2**-50,fLower:i,fUpper:o});return Math.exp(c)}var ep=["alternative","confInt","confLevel","or","simulatePValue","B","midp"];function So(e,t={}){V(t,ep,"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(Fc));if(s.flat().some(f=>f>2147483647))throw new RangeError("'x' has entries too large to be integer");let i=[],c=tp(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 H("fisherTest",{...sp(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 H("fisherTest",{method:a+bo(f),alternative:l,pValue:np(s,f),warnings:i})}return H("fisherTest",{method:a,alternative:l,pValue:ap(s),warnings:i})}function tp(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: ${Zc(a)}`:null}function np(e,t){let n=e.filter(g=>g.some(w=>w>0)),r=e[0].map((g,w)=>w).filter(g=>e.some(w=>w[g]>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(g=>r.map(w=>g[w])),s=o.map(g=>g.reduce((w,N)=>w+N,0)),i=o[0].map((g,w)=>o.reduce((N,v)=>N+v[w],0)),c=s.length,a=i.length,l=[0,0];for(let g of o.flat())l=ue(l,[-ss(g),0]);let f=l[0]/(1+64*Number.EPSILON),m=s.reduce((g,w)=>g+w,0);if(m>is)throw new RangeError("the simulation needs a total count of at most 16777216 (2^24)");let u=cs(m),h=new Int32Array(c*a),p=0,d=Math.trunc(t);for(let g=0;g<d;g++){ls(s,i,u,h);let w=0;for(let N=0;N<h.length;N++)w-=u[h[N]];w<=f&&p++}return Math.max(0,Math.min(1,(1+p)/(d+1)))}var Ll=1+1e-7,rp=16,op=2**24;function Ro(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=ue(r,[e[o],0]));return r[0]}var Wr=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%rp===0||!(s>0&&s<1/0)?(o=gn(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=Ro(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=ue(r,[(this.lo+o)*n[o],0]));return r[0]}tail(t,n,r){if(n===1)return r?on(t-1,this.m,this.n,this.k,!1,!1):on(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?Ro(o,s,o.length-1):Ro(o,0,s)}midTail(t,n,r){if(n===1){let i=.5*gn(t,this.m,this.n,this.k,!1);return(r?on(t,this.m,this.n,this.k,!1,!1):on(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?Ro(o,s+1,o.length-1):Ro(o,0,s-1))+.5*o[s]}};function La(e,t,n){let r=e.density(n),o=r[t-e.lo]*Ll,s=[0,0];for(let i of r)i<=o&&(s=ue(s,[i,0]));return s[0]}function Cl(e,t,n,r){let o=Math.max(0,r-n),s=Math.min(r,t),i=d=>gn(d,t,n,r,!0),c=i(e)+Math.log(Ll),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=(d,g)=>{let w=d,N=g+1;for(;N-w>1;){let v=w+Math.floor((N-w)/2);i(v)<=c?w=v:N=v}return w},f=(d,g)=>{let w=d-1,N=g+1;for(;N-w>1;){let v=w+Math.floor((N-w)/2);i(v)<=c?N=v:w=v}return N},m,u;e<=a?(m=l(e,a),u=f(a+1,s)):(u=f(a,e),m=a-1<o||i(o)>c?o-1:l(o,a-1));let h=m<o?0:on(m,t,n,r,!0,!1),p=u>s?0:on(u-1,t,n,r,!1,!1);return h+p}function To(e,t){return tr(e,t)}function sp(e,t,n){let r=Ge(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>op)throw new RangeError(`the exact distribution of the 2 x 2 table has ${m} values, more than 2^24`);let u=new Wr(i+a,c+l,i+c),{lo:h,hi:p}=u,d=t.midp??!1,g;d?(r==="less"?g=u.midTail(f,o,!1):r==="greater"?g=u.midTail(f,o,!0):g=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"?g=u.tail(f,o,!1):r==="greater"?g=u.tail(f,o,!0):o===0?g=f===h?1:0:o===1/0?g=f===p?1:0:g=La(u,f,o);let w;if(f===h)w=0;else if(f===p)w=1/0;else{let M=u.mean(1);w=M===f?1:To(x=>u.mean(x)-f,M>f)}let N={method:n,alternative:r,pValue:g,estimate:w,estimateNames:"odds ratio",nullValue:o,nullValueName:"odds ratio",warnings:[]};if(!(t.confInt??!0))return N;let v=M=>{if(f===p)return 1/0;if(d){if(f===h&&M>=.5)return 0;let y=u.midTail(f,1,!1);return y===M?1:To(R=>u.midTail(f,R,!1)-M,y<M)}let x=u.tail(f,1,!1);return x===M?1:To(y=>u.tail(f,y,!1)-M,x<M)},E=M=>{if(f===h)return 0;if(d){if(f===p&&M>=.5)return 1/0;let y=u.midTail(f,1,!0);return y===M?1:To(R=>u.midTail(f,R,!0)-M,y>M)}let x=u.tail(f,1,!0);return x===M?1:To(y=>u.tail(f,y,!0)-M,x>M)},b;if(r==="less")b=[0,v(1-s)];else if(r==="greater")b=[E(1-s),1/0];else{let M=(1-s)/2;b=[E(M),v(M)]}return{...N,confInt:b,confLevel:s}}var xs=[[0,0]];function Tn(e){for(let t=xs.length;t<=e;t++)xs.push(ue(xs[t-1],Ic([t,0])));return xs[e]}function Rs(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 _a=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 ap(e){let t=e.filter(v=>v.some(E=>E>0));if(t.length===0)throw new RangeError("all entries of the table are zero");let n=t[0].map((v,E)=>t.some(b=>b[E]>0));if(t=t.map(v=>v.filter((E,b)=>n[b])),t.length<2||t[0].length<2)return 1;t.length>t[0].length&&(t=t[0].map((v,E)=>t.map(b=>b[E])));let r=t.length,o=t.map(v=>v.reduce((E,b)=>E+b,0)),s=t[0].map((v,E)=>t.reduce((b,M)=>b+M[E],0)).sort((v,E)=>E-v),i=s.length,c=o.reduce((v,E)=>v+E,0),a=qc(Tn(c));for(let v of o)a=ue(a,Tn(v));for(let v of s)a=ue(a,Tn(v));let l=[0,0];for(let v of t.flat())l=Ce(l,Tn(v));let f=ue(l,[Math.log1p(1e-7),0]),m=1e-9*(1+Math.abs(f[0])),[u,h]=f,p=Array.from({length:i},()=>new Map),d=(v,E)=>{let b=E.join(","),M=p[v].get(b);if(M!==void 0)return M;let x=ue(a,Tn(E.reduce((L,_)=>L+_,0)));for(let L of E)x=Ce(x,Tn(L));for(let L=v;L<i;L++)x=Ce(x,Tn(s[L]));let y={R:E,KZ:x,max:0,min:0,edges:[],pastHi:[],pastLo:[],count:[],index:new Map};if(p[v].set(b,y),v===i-1){let L=[0,0];for(let _ of E)L=Ce(L,Tn(_));return y.max=y.min=L[0],y.edges.push({to:y,hi:L[0],lo:L[1],mult:1}),y}let R=new Map,T=new Array(r+1).fill(0);for(let L=r-1;L>=0;L--)T[L]=T[L+1]+E[L];let S=new Array(r).fill(0),k=(L,_,I)=>{if(L===r-1){S[L]=_;let D=Ce(I,Tn(_)),A=E.map((B,J)=>B-S[J]).sort((B,J)=>B-J),O=d(v+1,A),q=`${A.join(",")}|${D[0]}`,P=R.get(q);P===void 0?R.set(q,{to:O,hi:D[0],lo:D[1],mult:1}):P.mult++;return}let F=Math.max(0,_-T[L+1]),C=Math.min(E[L],_);for(let D=F;D<=C;D++)S[L]=D,k(L+1,_-D,Ce(I,Tn(D)))};if(k(0,s[v],[0,0]),y.edges=[...R.values()],y.max=At(y.edges.map(L=>L.hi+L.to.max)),y.min=sn(y.edges.map(L=>L.hi+L.to.min)),v===i-2){let L=y.edges.map(O=>{let q=O.to.edges[0];return{v:ue([O.hi,O.lo],[q.hi,q.lo]),mult:O.mult}});L.sort((O,q)=>O.v[0]-q.v[0]||O.v[1]-q.v[1]);let _=L.length,I=L[_-1].v[0],F=new Float64Array(_),C=new Float64Array(_),D=new Float64Array(_),A=[0,0];L.forEach(({v:O,mult:q},P)=>{F[P]=O[0],C[P]=O[1];let B=Ce(O,[I,0]);A=ue(A,[q*Math.exp(B[0])*(1+B[1]),0]),D[P]=A[0]}),y.dist={hi:F,lo:C,cum:D,vmax:I,kv:ue(a,[I,0])}}return y},g=d(0,[...o].sort((v,E)=>v-E)),w=new _a,N=(v,E,b,M)=>{if(E+v.max<=u-m){let[k,L]=Rs(v.KZ[0],v.KZ[1],E,b);return w.add(k,L,M),!0}if(E+v.min>u+m)return!0;let x=v.dist;if(x===void 0)return!1;let[y,R]=Rs(u,h,-E,-b),T=0,S=x.hi.length;for(;T<S;){let k=T+S>>>1,L=x.hi[k];L<y||L===y&&x.lo[k]<=R?T=k+1:S=k}if(T>0){let[k,L]=Rs(x.kv[0],x.kv[1],E,b);w.add(k,L,M*x.cum[T-1])}return!0};N(g,0,0,1)||(g.pastHi.push(0),g.pastLo.push(0),g.count.push(1));for(let v=0;v<i-2;v++)for(let E of p[v].values()){let{pastHi:b,pastLo:M,count:x}=E;if(x.length!==0){for(let{to:y,hi:R,lo:T,mult:S}of E.edges)for(let k=0;k<x.length;k++){let[L,_]=Rs(b[k],M[k],R,T),I=x[k]*S;if(N(y,L,_,I))continue;let F=y.index.get(L);F===void 0?(y.index.set(L,y.count.length),y.pastHi.push(L),y.pastLo.push(_),y.count.push(I)):y.count[F]+=I}E.pastHi=E.pastLo=E.count=[],E.index.clear()}}return Math.min(1,w.value())}function Ts(e){let t=e.every(n=>Number.isInteger(n))?1:2;return[t,e.map(n=>n*t).sort((n,r)=>n-r)]}function Oa(e){return[...e].sort((t,n)=>t-n).join(",")}function Ca(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 Fa(e){let[,t]=Ts(e),n=0,r=0;for(let o of t)r+=n+=o;return{steps:r,size:n+1}}function Da(e){let[t,n]=Ts(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 Ca(t,0,o)}function Pa(e,t){let[,n]=Ts(e),r=n.length,o=Math.min(t,r-t),s=Ol(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 Ol(e){let t=[0];for(let n of e)t.push(t[t.length-1]+n);return t}function Va(e,t){let[n,r]=Ts(e),o=r.length,s=Math.min(t,o-t),i=Ol(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 d=Math.min(h,s);d>=Math.max(1,s-(o-h));d--){let g=c[d],w=c[d-1],N=d/h,v=(h-d)/h,E=i[d],b=i[h]-i[h-d],M=i[d-1]+p,x=Math.max(E,M);for(let y=b;y>=x;y--)g[y-E]=v*g[y-E]+N*w[y-M];for(let y=x-1;y>=E;y--)g[y-E]=v*g[y-E]}}let a=c[s],l=t*(t+1)/2;if(s===t)return Ca(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 Ca(n,(i[o]-u)/n-l,m)}function nr(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 vr(e,t){let n=nr(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 Fl=["alternative","mu","paired","exact","correct","confInt","confLevel","tolRoot"];function Ao(e,t,n={}){if(typeof e!="string"&&at(t)&&V(t,Fl,"wilcoxTest"),V(n,Fl,"wilcoxTest"),typeof e=="string"){if(n.paired!==void 0)throw new Error("cannot use 'paired' in formula method");let d=ms(e,t,!0);return d.kind==="one"?Ao(d.x,n):Ao(d.x,d.y,d.kind==="paired"?{...n,paired:!0}:n)}let r;t!==void 0&&!ArrayBuffer.isView(t)&&!Array.isArray(t)?n=t:r=t;let o=Ge(n.alternative),s=n.mu??0,i=n.paired??!1,c=n.exact??null,a=cp(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[d,g]=Gn(Array.from(e),Array.from(r));u=Ct(d.map((w,N)=>w-g[N]))}else u=Ct(Array.from(e)),r!==void 0&&(h=Ct(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 mt};return H("wilcoxTest",h===void 0?hp(u,c,i,p):yp(u,h,c,p))}function cp(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 Ss(e,t,n){return n?t==="two.sided"?Math.sign(e)*.5:t==="greater"?.5:-.5:0}var Vl=10*Number.EPSILON;function $l(e,t,n=0){let r=a=>Math.min(1,Math.max(a,0)),o=()=>n===0?ie(e):r(ie(e)-n*Xt(e)),s=()=>n===0?ie(e,{lowerTail:!1}):r(ie(e,{lowerTail:!1})+n*Xt(e));if(t==="less")return o();if(t==="greater")return s();let i=o();if(i<=.5)return 2*i;let c=s();return c>=1/64?2*(1-i):2*c}function lp(e){let t=e*e;return[e*(t-3),e*(t*(t-10)+15),e*(t*(t*(t-21)+105)-105)]}function fp(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 zl(e,o,s,n)}function mp(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 zl(e,i,c,r)}function zl(e,t,n,r){let[o,s,i]=lp(e),c=t*o;return r>=2&&(c+=n*s),r>=3&&(c+=t*t/2*i),c}function ja(e){let t=e.length;if(t===0)return NaN;let n=Math.floor(t/2);return t%2===1?e[n]:st([e[n-1],e[n]])}var Ka=(e,t)=>e-t,$a=5e9,up=40;function za(e){return new RangeError(`wilcoxTest: the exact conditional ${e} would take too long or too much memory: use exact: false`)}function As(e,t){if(e.size>16777216||e.steps>$a)throw za(t);return e.steps}function jl(e){let t=e.length,n=bt(e.map(Math.abs)),r=0;for(let i=0;i<t;i++)e[i]>0&&(r+=n[i]);let o=0;for(let i of pn(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 hp(e,t,n,r){let{alternative:o,mu:s}=r,i=e.map(b=>b-s);i.some((b,M)=>!Number.isFinite(b)&&Number.isFinite(e[M]))&&(i=e.map(b=>b/2-s/2));let a=i.some(b=>b===0),l=t??i.length<50,f=(b,M,x)=>({method:b,alternative:o,statistic:M,statisticName:"V",pValue:x,nullValue:s,nullValueName:n?"location shift":"location",warnings:r.warnings.list});if(l){let b=bt(i.map(Math.abs)),M=pn(b).length>0,x=0;for(let L=0;L<i.length;L++)i[L]>0&&(x+=b[L]);let y="Wilcoxon signed rank exact test";if(!M&&!a){let L=i.length,_=L*(L+1)/2,I;if(o==="less")I=$r(x,L,!0,!1);else if(o==="greater")I=$r(x-1,L,!1,!1);else{let C=x>_/2?$r(x-1,L,!1,!1):$r(x,L,!0,!1);I=Math.min(1,2*C)}let F=f(y,x,I);return r.confInt?{...F,...pp(e,r)}:F}let R=b.filter((L,_)=>i[_]!==0);As(Fa(R),"distribution");let T=Da(R),S;if(o==="less")S=nr(T,x);else if(o==="greater")S=vr(T,x-.25);else{let L=0;for(let _ of R)L+=_/2;S=Math.min(1,2*(x>L?vr(T,x-.25):nr(T,x)))}let k=f(y,x,S);return r.confInt?{...k,...gp(e,r)}:k}let m=a?i.filter(b=>b!==0):i,u=m.length;if(u===0)throw new RangeError("no non-zero differences for the signed rank test");let[h,p,d]=jl(m),w=pn(bt(m.map(Math.abs))).length>0||a?Math.min(r.correct,0):r.correct,N=(p-Ss(p,o,w>=0))/d,v=$l(N,o,w>=1?fp(N,u,w):0),E=f(`Wilcoxon signed rank test${r.correct>=0?" with continuity correction":""}`,h,v);return r.confInt?{...E,...wp(e,r)}:E}function Wa(e,t,n){return e==="less"?[-1/0,n]:e==="greater"?[t,1/0]:[t,n]}function Dl(e,t,n){let r={confInt:Wa(e,NaN,NaN),confLevel:0,estimate:t};return n!==void 0&&(r.estimateNames=n),r}function Kl(e){if(!(e>0&&e<1/0))throw new RangeError("tolRoot must be a positive finite number")}function Wl(e){let t=e.length,n=[];for(let r=0;r<t;r++)for(let o=r;o<t;o++){let s=Hn(e[r],e[o]);Number.isNaN(s)||n.push(s)}return n.sort(Ka)}function Yl(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+Vl&&l++;let f=ja(e);if(l===0)return{confInt:[-1/0,1/0],confLevel:1,estimate:f,estimateNames:o};let m=t(l-1);return{confInt:Wa(s,e[l-1],e[c-l]),confLevel:1-(s==="two.sided"?2*m:m),estimate:f,estimateNames:o}}function pp(e,t){let n=e.length;return Yl(Wl(e),r=>$r(r,n,!0,!1),r=>jc(r,n,!0,!1),t,"(pseudo)median")}function Bl(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 Ul(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)+Vl,d=f!=="less",g=f!=="greater",w=I=>Hn(e[I-1],e[I]),N=Math.min(u-1,Math.max(1,Math.ceil(i.mean+oe(Math.min(p,1))*i.sd+.1*i.sd+3))),v=8,E=I=>I===0?e[0]-1:w(I),b=I=>I===0?e[u-1]+1:w(u-I),M=[new Map,new Map];{let I=new Map,F=0,C=up*c;for(let[D,A]of[[0,E],[1,b]])if(!(D===0?!d:!g))for(let O=0;O<=N;O++){if(F+=C,F>$a)throw za("interval");let q;try{q=t(A(O))}catch{break}if(M[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>$a)throw za("interval");I.set(q.key,!0),I.size>v&&I.delete(I.keys().next().value)}}let x=new Map,y=(I,F)=>{let C=I===0?E(F):b(F),D=M[I].get(F),A;if(D===void 0){let B=t(C);D=[B.stat,B.key],A=B.scores}let[O,q]=D,P=x.get(q);return P!==void 0?x.delete(q):(P=n(A??t(C).scores),x.size>=v&&x.delete(x.keys().next().value)),x.set(q,P),[O,P]},R=I=>{let[F,C]=y(0,I);return o(C,F)},T=I=>{let[F,C]=y(1,I);return s(C,F)},S=-1/0,k=0;if(d){let I=R(0);if(!(I>p)){let F=I,C=1;for(;C<=u-1;C++){let D=R(C);if(D>p)break;F=D}S=e[C-1],k=F}}let L=1/0,_=0;if(g){let I=T(0);if(!(I>p)){let F=I,C=u-1;for(;C>=1;C--){let D=T(u-C);if(D>p)break;F=D}L=e[C],_=F}}return{confInt:[S,L],confLevel:1-(k+_),estimate:ja(e),estimateNames:l}}function dp(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 gp(e,t){let n=e.length,r=s=>{let i=dp(e,s),{ranks:c,key:a}=Bl(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 Ul(Wl(e),r,Da,s=>As(Fa(s),"interval"),(s,i)=>vr(s,i-.25),nr,{mean:o/2,sd:Math.sqrt(n*(n+1)*(2*n+1)/24)},n,t,"(pseudo)median")}function wp(e,t){let{alternative:n,confLevel:r,tolRoot:o}=t,s=t.correct>=0,i=e.length;if(i===0)return Dl(n,NaN);let c=sn(e),a=At(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"),Dl(n,Hn(c,a),"midrange");let l=(v,E)=>{let[,b,M]=jl(e.map(x=>x-v).filter(x=>x!==0));return(b-Ss(b,n,E))/M},f=l(c,s),m=l(a,s),u=v=>-oe(v),h=v=>n==="greater"?!(f<u(v)):n==="less"?!(m>oe(v/2)):!(f<u(v/2))&&!(m>oe(v/2)),p=1-r,d=!1;for(;!h(p)&&p<1;)p*=2,d=!0;d&&t.warnings.add("requested conf.level not achievable"),Kl(o);let g=v=>Kt(E=>l(E,s)-v,c,a,{tol:o,fLower:f-v,fUpper:m-v}),w;if(p>=1){let v=ja([...e].sort(Ka));w=Wa(n,v,v)}else n==="greater"?w=[g(u(p)),1/0]:n==="less"?w=[-1/0,g(oe(p))]:w=[g(u(p/2)),g(oe(p/2))];let N=Kt(v=>l(v,!1),c,a,{tol:o,fLower:l(c,!1),fUpper:l(a,!1)});return{confInt:w,confLevel:d?1-Math.min(1,p):r,estimate:N,estimateNames:"(pseudo)median"}}function Gl(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 pn(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 bp(e,t){let[n,r,o]=Gl(bt([...e,...t]),e.length);return[n,r,o]}function vp(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 Np(e,t,n){return Hl(e,t,n).ranks}function Hl(e,t,n){let r=e.map(o=>o-n);if(r.some(o=>Number.isNaN(o))){let o=r.map((a,l)=>[a,l]).filter(([a])=>!Number.isNaN(a)),s=bt([...o.map(([a])=>a),...t]),i=new Array(e.length+t.length);o.forEach(([,a],l)=>i[a]=s[l]),t.forEach((a,l)=>i[e.length+l]=s[o.length+l]);let c=o.length+t.length;return r.forEach((a,l)=>{Number.isNaN(a)&&(i[l]=++c)}),{ranks:i,key:Oa(i)}}if(Number.isFinite(n)&&r.some((o,s)=>!Number.isFinite(o)&&Number.isFinite(e[s]))){let o=vp(e.map(s=>s/2-n/2),t);return{ranks:o,key:Oa(o)}}return Bl([...r,...t])}function yp(e,t,n,r){let{alternative:o,mu:s}=r,i=e.length,c=t.length,a=n??(i<50&&c<50),l=Np(e,t,s),[f,m,u,h]=Gl(l,i),p,d;if(a)if(d="Wilcoxon rank sum exact test",!h)p=Pl(()=>o==="less"?zr(f,i,c,!0,!1):o==="greater"?zr(f-1,i,c,!1,!1):Math.min(2*zr(f,i,c,!0,!1),2*zr(f-1,i,c,!1,!1),1));else{As(Pa(l,i),"distribution");let N=Va(l,i);o==="less"?p=nr(N,f):o==="greater"?p=vr(N,f-.25):p=Math.min(2*nr(N,f),2*vr(N,f-.25),1)}else{d=`Wilcoxon rank sum test${r.correct>=0?" with continuity correction":""}`;let N=h?Math.min(r.correct,0):r.correct,v=l.every(b=>b===l[0]),E=(m-Ss(m,o,N>=0))/u;p=v?1:$l(E,o,N>=1?mp(E,i,c,N):0)}let g={method:d,alternative:o,statistic:f,statisticName:"W",pValue:p,nullValue:s,nullValueName:"location shift",warnings:r.warnings.list};if(!r.confInt)return g;let w=a?h?Ep(e,t,r):Pl(()=>Mp(e,t,r)):xp(e,t,r);return{...g,...w}}function Pl(e){try{return e()}catch(t){throw t instanceof RangeError&&t.message.startsWith("wilcox:")?new RangeError(`${t.message}: use exact: false`):t}}function Xl(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(Ka)}function Mp(e,t,n){let r=e.length,o=t.length;return Yl(Xl(e,t,!1),s=>zr(s,r,o,!0,!1),s=>Wc(s,r,o,!0,!1),n,"difference in location")}function Ep(e,t,n){let r=e.length,o=t.length,s=i=>{let{ranks:c,key:a}=Hl(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 Ul(Xl(e,t,!0),s,i=>Va(i,r),i=>As(Pa(i,r),"interval"),(i,c)=>vr(i,c-.25),nr,{mean:r*o/2,sd:Math.sqrt(r*o*(r+o+1)/12)},r+o,n,"difference in location")}function xp(e,t,n){let{alternative:r,confLevel:o,tolRoot:s}=n,i=n.correct>=0,c=sn(e)-At(t),a=At(e)-sn(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[,d,g]=bp(e.map(w=>w-h),t);return(d-Ss(d,r,p))/g},f=(h,p)=>{let d=l(c,p)-h,g=l(a,p)-h;if(Number.isNaN(d)||Number.isNaN(g))throw new Error("cannot compute the confidence interval because all observations are tied");return d<=0?c:g>=0?a:(Kl(s),Kt(w=>l(w,p)-h,c,a,{tol:s,fLower:d,fUpper:g}))},m=1-o,u;return r==="greater"?u=[f(-oe(m),i),1/0]:r==="less"?u=[-1/0,f(oe(m),i)]:u=[f(-oe(m/2),i),f(oe(m/2),i)],{confInt:u,confLevel:o,estimate:f(0,!1),estimateNames:"difference in location"}}var Rp=["method","n"],Ql=["holm","hochberg","hommel","bonferroni","BH","BY","fdr","none"];function Tp(e){if(Ql.includes(e))return e;let t=Ql.filter(n=>n.startsWith(e));if(e.length===0||t.length!==1)throw new RangeError(`invalid method "${e}"`);return t[0]}function Yr(e,t={}){V(t,Rp,"pAdjust");let n=typeof t=="string"?{method:t}:t,r=Tp(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=Jl(a,r,c),f=o.slice();return s.forEach((m,u)=>f[m]=l[u]),f}function ks(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 Jl(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=ks(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=ks(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=Fn(),u=Math.floor(n);if(u<=1e6){for(let h=1;h<=u;h++)mn(m,1/h,u);s=Dn(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=ks(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?Jl(e,"hochberg",n):Sp(e,n)}}var Zl=2**16;function Sp(e,t){if(!Number.isInteger(t))throw new RangeError("n must be an integer for the hommel method");if(t>Zl)throw new RangeError(`n must be at most ${Zl} for the hommel method (O(n^2) work)`);let n=e.length,r=[...e,...new Array(t-n).fill(1)],o=ks(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 ef=["p","alternative","confLevel"],Ap=1+1e-7;function Ya(e,t,n){let r=e-1;return e===0||e-r===1&&$t(t,-r)[1]===0?On(r,t,n,!1,!1):On(e,t,n,!1,!1)+fn(e,t,n,!1)}function kp(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=fn(e,t,n,!1)*Ap,s=a=>fn(a,t,n,!1)<=o;if(e<r){let a=Zo(s,Math.ceil(r),t),l=a>t?0:Ya(a,t,n);return Math.min(1,On(e,t,n,!0,!1)+l)}let i=Jo(s,0,Math.floor(r)),c=i<0?0:On(i,t,n,!0,!1);return Math.min(1,Ya(e,t,n)+c)}function qp(e,t,n={}){V(t,ef,"binomTest"),V(n,ef,"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),!wn(e[0])||!wn(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(!wn(e))throw new RangeError("'x' must be nonnegative and integer");if(r=Math.round(e),!(wn(t)&&Math.round(t)>=r&&t>0))throw new RangeError("'n' must be a positive integer >= 'x'");o=Math.round(t)}return Ba(r,o,n)}function Ba(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=Ge(n.alternative),s=n.confLevel??.95;It(s);let i;o==="less"?i=On(e,t,r,!0,!1):o==="greater"?i=Ya(e,t,r):i=kp(e,t,r);let c=f=>e===0?0:Yn(f,e,t-e+1,!0,!1),a=f=>e===t?1:f<1e-8?Yn(f,e+1,t-e,!1,!1):Yn(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 Ip=["T","r","alternative","confLevel"],_p=1+1e-7;function Lp(e,t){if(t===0)return e===0?1:0;if(e===t)return 1;let n=es(e,t,!1)*_p,r=i=>es(i,t,!1)<=n,o=Math.floor(t);if(e<t){let i=Zo(r,Math.max(e+1,o),1/0);return Math.min(1,Pr(e,t,!0,!1)+Ua(i,t))}let s=Jo(r,0,Math.min(e-1,o));return Math.min(1,Pr(s,t,!0,!1)+Ua(e,t))}var ko=2**-1022;function Ua(e,t){return e-(e-1)===1?Pr(e-1,t,!1,!1):Pr(e,t,!1,!1)+es(e,t,!1)}function Nr(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 Cp(e,t,n,r){let o=n+t*r,s=e*t*n/o;if(Number.isFinite(s)&&Number.isFinite(o)&&o>=ko&&(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]=Nr(e),[f,m]=Nr(t),[u,h]=Nr(n),[p,d]=Nr(r),g=f*p,w=h-m-d,N=w>=0?u/(u+$(g,-w)):u/($(u,w)+g);return $(a*f*N,l+m+Math.min(w,0))}function Op(e,t,n,r){let o=e/n,s=t/r,i=o/s;return t===0||Number.isFinite(o)&&Number.isFinite(s)&&s>=ko&&(o===0||o>=ko)?i:e===0?0:e/t*(r/n)}function Fp(e,t={}){V(t,Ip,"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(wn))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(w=>Number.isFinite(w)&&w>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=Ge(t.alternative),l=t.confLevel??.95;if(o===2?It(l):an(l),o===2){let[w,N]=r,[v,E]=i,b=w+N;if(b===1/0)throw new RangeError("'x' must be finite, nonnegative, and integer");let M=c*v,x=M+E,y=Number.isFinite(x)&&x>=ko&&(M===0||M>=ko)?M/x:1/(1+E/v/c),R=Ba(Math.round(w),Math.round(w)+Math.round(N),{p:y,alternative:a,confLevel:l}),T=E/v,S=k=>{if(k===0)return 0;let L=k/(1-k);if(Number.isFinite(T)||L===1/0)return L*T;let[_,I]=Nr(L),[F,C]=Nr(v),[D,A]=Nr(E);return $(_*D/F,I+A-C)};return{method:"Comparison of Poisson rates",alternative:a,statistic:w,statisticName:"count1",parameter:Cp(b,c,v,E),parameterName:"expected count1",pValue:R.pValue,confInt:[S(R.confInt[0]),S(R.confInt[1])],confLevel:l,estimate:Op(w,N,v,E),estimateNames:"rate ratio",nullValue:c,nullValueName:"rate ratio",warnings:[]}}let f=r[0],m=i[0],u=c*m,h;a==="less"?h=Pr(f,u,!0,!1):a==="greater"?h=Ua(f,u):h=Lp(f,u);let p=w=>f===0?0:Qo(w,f,1,!0,!1),d=w=>w<1e-8?Qo(w,f+1,1,!1,!1):Qo(1-w,f+1,1,!0,!1),g;if(a==="less")g=[0,d(1-l)];else if(a==="greater")g=[p(1-l),1/0];else{let w=(1-l)/2;g=[p(w),d(w)]}return{method:"Exact Poisson test",alternative:a,statistic:f,statisticName:"number of events",parameter:m,parameterName:"time base",pValue:h,confInt:[g[0]/m,g[1]/m],confLevel:l,estimate:f/m,estimateNames:"event rate",nullValue:c,nullValueName:"event rate",warnings:[]}}var tf=["p","alternative","confLevel","correct"],Dp="Chi-squared approximation may be incorrect";function Ga(e,t,n={}){at(t)&&V(t,tf,"propTest"),V(n,tf,"propTest");let r,o,s=n;if(typeof e!="number"&&e.length>0&&Array.isArray(e[0])){let S=e;if(S.some(k=>k.length!==2))throw new RangeError("'x' must have 2 columns");r=S.map(k=>k[0]),o=S.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 S=0;S<i;S++)!Number.isNaN(r[S])&&!Number.isNaN(o[S])&&c.push(S);let a=c.length;if(a<1)throw new RangeError("not enough data");if(r=c.map(S=>r[S]),o=c.map(S=>o[S]),o.some(S=>!(S>0)))throw new RangeError("elements of 'n' must be positive");if(o.some(S=>S===1/0))throw new RangeError("elements of 'n' must be finite");if(r.some(S=>!(S>=0)))throw new RangeError("elements of 'x' must be nonnegative");if(r.some((S,k)=>S>o[k]))throw new RangeError("elements of 'x' must not be greater than those of 'n'");let l;if(s.p!==void 0){let S=typeof s.p=="number"?[s.p]:Array.from(s.p);if(S.length!==i)throw new RangeError("'p' must have the same length as 'x' and 'n'");if(l=c.map(k=>S[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;It(m);let u=Ge(s.alternative);(a>2||a===2&&f)&&(u="two.sided");let h=r.map((S,k)=>S/o[k]),p=a===2?h[0]-h[1]:0,d=a===2?1/o[0]+1/o[1]:0,g=(s.correct??!0)&&a<=2?.5:0,w=!1;if(g>0&&a===1){let S=Math.abs(r[0]-o[0]*l[0]);S<=g&&([g,w]=[S,!0])}else if(g>0&&a===2&&!f){let S=Math.abs(p)/d;S<=g&&([g,w]=[S,!0])}let N=Pp(r,o),v=o.some((S,k)=>{let L=l?.[k]??N;return S*L<5||S*(1-L)<5}),E=0;if(!w)for(let S=0;S<a;S++){let k=l?.[S]??N,L=o[S]*k,_=o[S]*(1-k);E+=Dt(Math.abs(r[S]-L)-g,L),E+=Dt(Math.abs(o[S]-r[S]-_)-g,_)}let b=l!==void 0?a:a-1,M;if(u==="two.sided")M=gt(E,b/2,2,!1,!1);else{let S=Math.sign(a===1?h[0]-l[0]:p);M=ie(S*Math.sqrt(E),{lowerTail:u==="less"})}let x;if(a===1||a===2&&!f){let S=(1-m)/2,k=u==="two.sided"&&S<1e-8?oe(S,{lowerTail:!1}):oe(u==="two.sided"?(1+m)/2:m);if(a===1){let L=o[0],_=k*k,I=(O,q)=>(O+_/(2*L)+q*k*Math.sqrt(O*(1-O)/L+_/(4*L*L)))/(1+_/L),F=h[0]+g/L,C=h[0]-g/L,D=Math.min(1,Math.max(0,F>=1?1:I(F,1))),A=Math.min(1,Math.max(0,C<=0?0:I(C,-1)));x=u==="two.sided"?[A,D]:u==="greater"?[A,1]:[0,D]}else{let L=Math.sqrt(h.reduce((C,D,A)=>C+D*(1-D)/o[A],0)),_=k*L+g*d,I=Math.max(-1,p-_),F=Math.min(1,p+_);x=u==="two.sided"?[I,F]:u==="greater"?[I,1]:[-1,F]}}let y=a===1?"p":c.map(S=>`prop ${S+1}`),T={method:`${a===1?"1-sample proportions test":`${a}-sample test for ${f?"given proportions":"equality of proportions"}`} ${g!==0?"with":"without"} continuity correction`,alternative:u,statistic:E,statisticName:"X-squared",parameter:b,parameterName:"df",pValue:M,estimate:a===1?h[0]:h,estimateNames:y,warnings:v?[Dp]:[]};return l!==void 0&&(T.nullValue=a===1?l[0]:l,T.nullValueName=y),x!==void 0&&(T.confInt=x,T.confLevel=m),H("propTest",T)}function Pp(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 Vp=["method","confLevel","sides"],$p=["wilson","wald","wilsoncc","agresti-coull","jeffreys","clopper-pearson","midp"],zp=["two.sided","left","right"];function qo(e,t){let n=xe(t,zp),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{...Ha(s,o),sides:n}}function Ha(e,t=1-e){let n=e/2<1e-8,r=n?oe(e/2,{lowerTail:!1}):oe(1-e/2),o=n?Cn(e,1,{lowerTail:!1}):Cn(t,1);return{sides:"two.sided",level:t,alpha:e,kappa:r,chisq:o}}function Br(e,t){if(typeof e!="number")throw new TypeError(`'${t}' must be a number`);if(!wn(e))throw new RangeError(`'${t}' must be a nonnegative integer`);return Math.round(e)+0}function $n(e,t,n,r){let o=Br(e,n),s=Br(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 Is(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 qs(e,t,n,r){return r?e/2<1e-8?Yn(e/2,t,n,!1,!1):Yn(1-e/2,t,n,!0,!1):Yn(e/2,t,n,!0,!1)}function nf(e,t,n,r){let o=n/2,s=r?f=>{let m=Math.exp(f);return .5*fn(e,t,m,!1)+On(e-1,t,m,!0,!1)-o}:f=>{let m=Math.exp(f);return .5*fn(e,t,m,!1)+On(e,t,m,!1,!1)-o},i=r?-1:1,c=Math.log(e/t),a=i*s(c);if(a===0)return e/t;if(a>0){let f=c,m=a,u=1;for(;;){let h=c-u;if(h<-745)return 0;let p=i*s(h);if(p<=0)return Math.exp(p===0?h:Kt(d=>i*s(d),h,f,{tol:2**-60,fLower:p,fUpper:m}));f=h,m=p,u*=2}}let l=i*s(0);return l<=0?1:Math.exp(Kt(f=>i*s(f),c,0,{tol:2**-60,fLower:a,fUpper:l}))}function Xa(e,t,n={}){V(n,Vp,"binomCI");let r=xe(n.method,$p),o=qo(n.confLevel,n.sides),[s,i]=$n(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]=Is(s,i,a);break;case"wilsoncc":{let h=a*a,p=2*(i+h),d=2*s+h-1,g=a*Math.sqrt(h-2-1/i+4*l*(i*f+1));m=l===0?0:d-g<d/2?(2*s-1)/(2*i)*(2*s-1)/(d+g):(d-g)/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,d=(s+h/2)/p,g=a*Math.sqrt(d*(1-d)/p);[m,u]=[d-g,d+g];break}case"jeffreys":m=s===0?0:qs(c,s+.5,i-s+.5,!1),u=s===i?1:qs(c,s+.5,i-s+.5,!0);break;case"clopper-pearson":m=s===0?0:qs(c,s,i-s+1,!1),u=s===i?1:qs(c,s+1,i-s,!0);break;case"midp":m=s===0?0:s===i?Math.exp(Math.log(c)/i):nf(s,i,c,!1),u=s===i?1:s===0?-Math.expm1(Math.log(c)/i):nf(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),H("binomCI",{estimate:l,confInt:[m,u],confLevel:n.confLevel??.95,method:r,sides:o.sides,warnings:[]})}function Io(e){return e===1/0?1:e/(1+e)}function _o(e,t){return e===0?0:t>0?e*e/t:1/0}function jp(e,t,n,r){return e/t+n/r>1}function Kp(e,t,n,r){return Ls(e,t,n,r,0)}function Ja(e,t,n,r,o){if(jp(e,t,n,r)){let[p,d,g,w]=Ja(t-e,t,r-n,r,-o);return[d,p,w,g]}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=w=>{let N=0;return i>0&&(N+=i/w),i<c&&(N-=(c-i)/(1-w)),a>0&&(N+=a/(w+f)),a<l&&(N-=(l-a)/(m-w)),N},d=w=>{let N=0;return i>0&&(N-=i/(w*w)),i<c&&(N-=(c-i)/((1-w)*(1-w))),a>0&&(N-=a/((w+f)*(w+f))),a<l&&(N-=(l-a)/((m-w)*(m-w))),N};if(p(0)<=0)u=0;else if(p(m)>=0)u=m;else{let w=0,N=m,v=Wp(e,t,n,r,o);u=s?v:v-o,u>w&&u<N||(u=N/2);for(let E=0;E<2e3;E++){let b=p(u);if(b===0)break;b>0?w=u:N=u;let M=u-b/d(u);if(M>w&&M<N||(M=w===0?N/2:N>2*w?Math.sqrt(w*N):w+(N-w)/2),!(M>w&&M<N))break;let x=Math.abs(M-u);if(u=M,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 Wp(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 d=(Math.PI+Math.acos(Math.max(-1,Math.min(1,h/(p*p*p)))))/3;return 2*p*Math.cos(d)-u}function ei(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]=Ja(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 Yp(e,t,n,r,o,s,i){let c=Ce(Ce(ft([e,0],t),ft([n,0],r)),[o,0]),[a,l,f,m]=Ja(e,t,n,r,o),u=ue(ft(Ht(a,l),t),ft(Ht(f,m),r));return s==="mn"&&(u=ft(zt(u,t+r),t+r-1)),Ce(_e(c,c),zt(u,i))[0]}function Ls(e,t,n,r,o){return Ce(Ce(ft([e,0],t),ft([n,0],r)),[o,0])[0]}function Bp(e,t,n,r,o,s){return _o(Ls(e,t,n,r,o),ei(e,t,n,r,o,s))}var _s=new Float64Array(1),Qa=new BigInt64Array(_s.buffer);function rf(e){return _s[0]=Math.abs(e),e<0?-Qa[0]:Qa[0]}function rr(e){return Qa[0]=e<0n?-e:e,e<0n?-_s[0]:_s[0]}function Za(e,t,n,r,o){let s=e(t);if(s===0||Number.isNaN(s))return t;let i=s<0===o,c=rf(i?r:n),a=rf(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(rr(f)),m===0)return rr(f);if(m<0!=l<0||f===c)break;if(a=f,l=m,u*=2n,u>1n<<40n)return rr(a)}if(m<0==l<0||Number.isNaN(m))return rr(a);for(;f-a>1n||a-f>1n;){let h=(a+f)/2n,p=e(rr(h));if(p===0)return rr(h);p<0==l<0?(a=h,l=p):(f=h,m=p)}return Math.abs(m)<Math.abs(l)?rr(f):rr(a)}function sf(e,t,n,r,o,s){let i=Kp(e,t,n,r);if(o===0)return[i,i];let c=Io(o),a=p=>(p===i?0:Io(Bp(e,t,n,r,p,s)))-c,l=(p,d,g,w)=>{if(g===0)return p;if(w===0)return d;let N=Kt(a,p,d,{tol:1e-300,fLower:g,fUpper:w});return s==="wald"?N:Za(v=>Yp(e,t,n,r,v,s,o),N,p,d,g<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 of(e,t,n){let r=t,o=e(t);if(o>=0)return Math.exp(t);let s=1;for(;;){let i=t+n*s;if(Math.abs(i)>745)return n<0?0:1/0;let c=e(i);if(c>=0){if(c===0)return Math.exp(i);let a=n>0?Kt(e,r,i,{tol:2**-60,fLower:o,fUpper:c}):Kt(e,i,r,{tol:2**-60,fLower:c,fUpper:o});return Math.exp(a)}r=i,o=c,s*=2}}function Up(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 Gp(e,t,n,r,o){let[s,i]=Up(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=M=>c===0?[M,0]:c===2?$t(1,-M):ft($t(1,-M),o),f=c===3?1/o:1,m=c===0?Math.min(1,1/o):1,u=c===2?o>1?ft($t(o,-1),o)[0]:0:c===3&&o<1?$t(1,-o)[0]:0,h=M=>{let x=l(M),y=zt(x,o);return[y[0],Ce([1,0],y)[0],x[0],Ce([1,0],x)[0]]},p=e+n,d=c<2?1:-1,g=M=>{let[,x,y,R]=h(M),T=0,S=0;return p>0&&(T+=y>0?p/y:1/0,S-=p/(y*y)),e<t&&(T-=x>0?o*(t-e)/x:1/0,S-=o*(t-e)/x*(o/x)),n<r&&(T-=R>0?(r-n)/R:1/0,S-=(r-n)/(R*R)),[d*T,S*f]},w;if(!(m>u))w=u;else if(!(g(u)[0]>0))w=u;else if(!(g(m)[0]<0))w=m;else{let M=u,x=m;w=a,w>M&&w<x||(w=M+(x-M)/2);for(let y=0;y<2e3;y++){let[R,T]=g(w);if(R===0)break;R>0?M=w:x=w;let S=w-R/T;if(S>M&&S<x||(S=M<=0?x/2:x>2*M?Math.sqrt(M*x):M+(x-M)/2),!(S>M&&S<x))break;let k=Math.abs(S-w);if(w=S,k<=2**-53*w)break}}let[N,v,E,b]=h(w);return[Math.min(1,Math.max(0,N)),Math.min(1,Math.max(0,v)),Math.min(1,Math.max(0,E)),Math.min(1,Math.max(0,b))]}function Hp(e,t,n,r,o){let[s,i,c,a]=Gp(e,t,n,r,o),l=(f,m,u,h)=>u>.5?m*h-(m-f):f-m*u;return _o(l(e,t,s,i),t*s*i)+_o(l(n,r,c,a),r*c*a)}function af(e,t,n,r,o){let s=e/t/(n/r);return lf(s,o,i=>Hp(e,t,n,r,i))}function Xp(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 Qp(e,t,n,r,o){let[s,i]=Xp(e,t,n,r,o),c=e-t*s;return _o(c,t*s*(1-s))+_o(c,r*i*(1-i))}function cf(e,t,n,r,o){let s=e*(r-n)/((t-e)*n);return lf(s,o,i=>Qp(e,t,n,r,i))}function lf(e,t,n){if(t===0)return[e,e];let r=Io(t),o=l=>n(l)-t,s=l=>l>0&&l<e?Za(o,l,Number.MIN_VALUE,e,!1):l,i=l=>l>e&&l<1/0?Za(o,l,e,Number.MAX_VALUE,!0):l;if(e===0||e===1/0){let l=u=>Io(n(u))-r,f=l(1),m=f===0?1:tr(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:Io(n(Math.exp(l))))-r;return[s(of(a,c,-1)),i(of(a,c,1))]}var Zp=["method","confLevel","sides"],Jp=["method","confLevel","sides"],ed=["method","confLevel"],td=["ac","wald","score","mn","mee"];function ti(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,d=i*Math.sqrt(u*(1-u)/(t+2)+h*(1-h)/(r+2));[f,m]=[Math.max(-1,p-d),Math.min(1,p+d)];break}case"score":{let[u,h]=Is(e,t,i),[p,d]=Is(n,r,i);f=l-i*Math.sqrt(u*(1-u)/t+d*(1-d)/r),m=l+i*Math.sqrt(h*(1-h)/t+p*(1-p)/r);break}case"mn":case"mee":[f,m]=sf(e,t,n,r,s.chisq,o);break}return[Math.min(f,m),Math.max(f,m)]}function nd(e,t,n,r,o={}){V(o,Zp,"binomDiffCI");let s=xe(o.method,td),i=qo(o.confLevel,o.sides),[c,a]=$n(e,t,"x1","n1"),[l,f]=$n(n,r,"x2","n2"),m=c/a-l/f,[u,h]=ti(c,a,l,f,s,i);return i.sides==="left"?h=1:i.sides==="right"&&(u=-1),H("binomDiffCI",{estimate:m,confInt:[u,h],confLevel:o.confLevel??.95,method:s,sides:i.sides,warnings:[]})}var rd=["katz.log","koopman"];function Cs(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 od(e,t,n,r,o={}){V(o,Jp,"binomRatioCI");let s=xe(o.method,rd),i=qo(o.confLevel,o.sides),[c,a]=$n(e,t,"x1","n1"),[l,f]=$n(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,Cs(.5,a,l,f,m)[1]]:l===0?[h,p]=[Cs(c,a,.5,f,m)[0],1/0]:c===a&&l===f?[h,p]=Cs(a-.5,a,f-.5,f,m):[h,p]=Cs(c,a,l,f,m):[h,p]=af(c,a,l,f,i.chisq),i.sides==="left"?p=1/0:i.sides==="right"&&(h=0),H("binomRatioCI",{estimate:u,confInt:[h,p],confLevel:o.confLevel??.95,method:s,sides:i.sides,warnings:[]})}var sd=["midp","fisher","wald","score"],ad={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"},id=2**23,cd=2**20;function ff(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=ue(r,[e[o],0]));return r[0]}function ld(e,t={}){V(t,ed,"oddsRatio");let n=xe(t.method,sd);if(e.length!==2||e.some(S=>S.length!==2))throw new TypeError("'table' must be a 2 x 2 table");let r=t.confLevel??.95,{alpha:o,kappa:s,chisq:i}=qo(r,void 0),c=e.map((S,k)=>Array.from(S,(L,_)=>Br(L,`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),d=p-h+1;if(n==="midp"||n==="fisher"){if(d>id)throw new RangeError(`the exact distribution of the 2 x 2 table has ${d} 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 g,w,N;try{g=on(a-1,a+f,l+m,a+l,!0,!1)+.5*gn(a,a+f,l+m,a+l,!1),w=on(a,a+f,l+m,a+l,!1,!1)+.5*gn(a,a+f,l+m,a+l,!1),N=d<=cd?La(new Wr(a+f,l+m,a+l),a,1):Cl(a,a+f,l+m,a+l)}catch(S){throw S instanceof RangeError?new RangeError(`the exact p-values of the table are beyond reach: ${S.message}`):S}let v=2*Math.min(g,w),E=[a+l,f+m],b=[a+f,l+m],M=0,x=!1;for(let S=0;S<2;S++)for(let k=0;k<2;k++){let L=E[k]*b[S]/u;L<5&&(x=!0);let _=Math.abs(c[k][S]-L);M+=_*_/L}let y=gt(M,.5,2,!1,!1),R,T;switch(n){case"wald":{R=a*m/(l*f);let S=s*Math.sqrt(1/a+1/l+1/f+1/m);T=[Math.exp(Math.log(R)-S),Math.exp(Math.log(R)+S)];break}case"fisher":{let S=je(()=>So(c,{confLevel:r}));R=S.estimate,T=S.confInt;break}case"score":{R=a*m/(l*f),T=(a===0||m===0)&&(l===0||f===0)?[0,1/0]:cf(a,a+l,f,f+m,i);break}case"midp":{let S=new Wr(a+f,l+m,a+l),k=F=>{if(F===1)return[g,w];let C=S.density(F),D=a-h,A=.5*C[D];return[ff(C,0,D-1)+A,ff(C,D+1,C.length-1)+A]},L=(F,C)=>{let D=F(1);return D===0?1:tr(F,C?D>0:D<0)};R=a===h?0:a===p?1/0:L(F=>k(F)[0]-.5,!1);let _=a===h?0:L(F=>k(F)[1]-o/2,!0),I=a===p?1/0:L(F=>k(F)[0]-o/2,!1);T=[_,I];break}}return H("oddsRatio",{estimate:R,confInt:T,confLevel:r,method:ad[n],pValue:{midp:v,fisher:N,chiSquare:y},warnings:x?["Chi-squared approximation may be incorrect"]:[]})}var fd=["epsilon","paired","varEqual","confLevel","alpha"],md=["margin","method","alpha"];function mf(e,t){if(!(typeof e=="number"&&e>0&&e<.5))throw new RangeError(`'${t}' must be a single number in (0, 0.5)`)}function ud(e,t,n={}){V(n,fd,"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)mf(n.alpha,"alpha"),o=1-n.alpha,s=n.alpha;else{if(o=n.confLevel??.95,It(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=je(()=>vn(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=vt((r-Math.abs(m))/l,f,!1,!1),h=s<1e-8?xt(s,f,!1,!1):xt(o,f,!0,!1),p=[m-h*l,m+h*l];return H("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 hd=["mn","mee","wald"];function pd(e,t,n,r,o={}){V(o,md,"binomDiffEquivalenceTest");let s=xe(o.method,hd),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;mf(c,"alpha");let[a,l]=$n(e,t,"x1","n1"),[f,m]=$n(n,r,"x2","n2"),u=a/l-f/m,h=E=>Ls(a,l,f,m,E)/Math.sqrt(ei(a,l,f,m,E,s)),p=h(-i),d=h(i),g=[ie(p,{lowerTail:!1}),ie(d)],w=Math.max(g[0],g[1]),N=1-2*c,v=ti(a,l,f,m,s,Ha(2*c,N));return H("binomDiffEquivalenceTest",{method:s,estimate:u,margin:i,alpha:c,statistic:[p,d],statisticName:"z",pValues:g,pValue:w,result:g[0]<c&&g[1]<c?"rejected":"not rejected",confInt:v,confLevel:N,warnings:[]})}var Wt=new WeakMap;function Fs(e){let t=(n,r)=>n.weights===void 0?Array.from(r):r.filter((o,s)=>n.weights[s]!==0);return Re(e.flatMap(n=>[t(n,n.fittedValues),t(n,n.residuals)])).k}function Ds(e,t){return e.map(n=>{if(n.rss>=dd&&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 dd=2**-1022;function ri(e,t){return e.y.flatMap((n,r)=>(e.weights?.[r]??1)===0?[]:[[e.weights?.[r]??1,$(n,-t)]])}function Ps(e){let t=Math.floor(Math.log2(e.reduce((n,[r])=>Math.max(n,r),0)));return t-(t%2+2)%2}function yr(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=Ps(e),s=r(o)),!($(t,-o)>=1e-24*s)}function uf(e){let t=Fs([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:Ds([e],t)[0],k:t}}var hf=(e,t,n)=>wt(e,t,n,!1,!1),oi="essentially perfect fit for \u20181\u2019: summary may be unreliable";function si(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 ai=new WeakMap;function gd(e){let t=uf(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 ni(e){let t=gd(e),{names:n,rdf:r,k:o,perfect:s}=t,{rows:i,rss:c}=t;if(r>0){let d=ri(e,o);yr(d,c)&&(c=0,i=i.map(g=>yr(d,g.ss)?{...g,ss:0}:g))}let a=c/r,l=d=>$(d,2*o),f=i.map(({df:d,ss:g})=>{let w=g/d/a;return[d,l(g),l(g/d),w,hf(w,d,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:d,ss:g})=>[d,g]);return p.push([r,c]),Wt.set(h,{colNames:["Df","Sum Sq"],rowNames:u,table:p}),h}function wd(e){let t=e.map(f=>f.dfResidual),n=Fs(e),r=Ds(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,d]=u>0?[e[m-1],e[m]]:[e[m],e[m-1]],g=N=>N.filter((v,E)=>(p.weights?.[E]??1)!==0),w=u===0||!gs(g(p.x),g(d.x))?NaN:h/u/i;return w<0&&(w=NaN),[t[m],o(r[m]),u,o(h),w,Number.isNaN(w)?NaN:hf(w,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 Os(...e){let t=[];for(let n of e)for(let r of n??[])t.includes(r)||t.push(r);return t}function ii(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=ni(e),a={...c,warnings:Os(e.warnings,c.warnings)},l=Wt.get(c);return l!==void 0&&Wt.set(a,l),H("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?ni(e):wd(o);return i.warnings=Os(...n.map(c=>c.warnings),s,i.warnings),H("anova",i,...n)}function ci(e){if(typeof e=="object"&&e!==null&&e.multistratum===!0){let i=e,c=Os(i.warnings,ai.get(i)),a={strata:i.strata.filter(f=>f.stratum!=="Error: (Intercept)").map(f=>({...f})),warnings:c},l=Wt.get(i);return Array.isArray(l)&&Wt.set(a,l.filter((f,m)=>i.strata[m].stratum!=="Error: (Intercept)")),H("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=ni(t),r=uf(t),o=si(t.fittedValues.map(i=>$(i,-r.k)),r.rss,r.rdf),s=Os(t.warnings,n.warnings,o?[oi]:[]);if(t.dfResidual>0){let i={...n,warnings:s},c=Wt.get(n);return c!==void 0&&Wt.set(i,c),H("aovSummary",i,e)}return H("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 bd=["pooledSd","mu","paired","ci","alternative","adjust"],vd=["pooledSd","mu","paired","ci","alternative"],Nd=["generalized","partial","ci","alternative"],yd=["partial","ci","alternative"],Md=["partial","ci","alternative"];function mi(e){if(e!==null&&!(e>0&&e<1))throw new RangeError("ci must be a single numeric value between (0, 1)")}var Ed="Missing values detected. NAs dropped.",xd=`'y' is numeric but has only 2 unique values.
3
3
  If this is a grouping variable, convert it to a factor.`,wf={tol:0,maxiter:5e3};function Mr(e){return e===1/0?1:Math.exp(Dc(.5,(e-1)/2)-.5*Math.log(e/2))}function pf(e,t,n){let r=e-1<e+1?1:Math.abs(e)*Number.EPSILON;return pr(o=>dn(e,t,{ncp:o})-n,e-r,e+r,{...wf,extendInt:"downX"}).root}function bf(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;mi(c);let a=xe(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(Ct(f)).size===2&&m.push(xd),(l.some(Number.isNaN)||(f?.some(Number.isNaN)??!1))&&m.push(Ed);let u=S=>S===1/0||S===-1/0;if(l.some(u)||(f?.some(u)??!1))throw new RangeError("the observations must be finite (NaN is dropped as missing)");let h,p,d,g,w,N;if(f===void 0||i){let S;if(f===void 0)({groups:[S],k:N}=Re([Ct(l)]));else{let[L,_]=Gn(l,f);({d:S,k:N}=ns(L,_))}let k=S.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=st(S),p=Math.sqrt(ot(S)),d=p/Math.sqrt(k),g=1/k,w=k-1}else{let S=Et([Ct(l),Ct(f)]);N=S.k;let[k,L]=S.n;if(k<2||L<2)throw new RangeError("not enough observations: at least 2 are needed in each group");h=S.mean[0]-S.mean[1];let _=Math.sqrt(S.variance[0]),I=Math.sqrt(S.variance[1]);o?(p=Math.sqrt(((k-1)*_**2+(L-1)*I**2)/(k+L-2)),d=p*Math.sqrt(1/k+1/L),g=1/k+1/L,w=k+L-2):(p=Math.sqrt((_**2+I**2)/2),g=2*(L*_**2+k*I**2)/(k*L*(_**2+I**2)),d=Math.sqrt(Math.sqrt(_**2/k)**2+Math.sqrt(I**2/L)**2),w=rs([_**2/k,I**2/L],[k-1,L-1]))}let v=r?Mr(w):1,E=$(s,-N),b=(h-E)/p*v,M=r?"Hedges_g":"Cohens_d";if(c===null)return{name:M,value:b,warnings:m};let x=a==="two.sided"?c:2*c-1,y=(h-E)/d,R=pf(y,w,(1+x)/2)*Math.sqrt(g)*v,T=pf(y,w,(1-x)/2)*Math.sqrt(g)*v;return a==="less"&&(R=-1/0),a==="greater"&&(T=1/0),{name:M,value:b,ci:c,ciLow:R,ciHigh:T,warnings:m}}function Rd(e,t,n={}){return V(n,bd,"cohensD"),H("cohensD",bf(e,t,n,n.adjust??!1))}function Td(e,t,n={}){return V(n,vd,"hedgesG"),H("hedgesG",bf(e,t,n,!0))}var Sd=e=>Array.isArray(e.effects),li=e=>e.multistratum===!0,vf=e=>Array.isArray(e.strata),Nf=e=>{let t=e;return typeof t.univariate=="object"&&typeof t.anova=="object"},fi=e=>({test:t=>t.split(":").includes(e)});function Ad(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 kd(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(w=>fi(w).test(m)),d=o(p.length===0?"Subject":`Subject:${p.join(":")}`,h[l],h[f]);if(m==="(Intercept)")return;let g={name:m,df:h[a],ss:h[n]};d.terms.push(g),s.set(m,g)})}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 yf(e,t){let n=Array.isArray(e)?e:[],r=n.filter(s=>!t.some(i=>fi(s).test(i.name)));if(r.length>0)throw new RangeError(`Observed variable not in data: ${r.join(", ")}`);let o=s=>n.some(i=>fi(i).test(s));return{isObserved:o,ssObserved:t.reduce((s,i)=>o(i.name)?s+i.ss:s,0)}}function df(e,t,n,r){let o=i=>xn(e,t,n,{ncp:i})-r,s=o(0);return Number.isNaN(s)?NaN:s>0?e===1/0?1/0:pr(o,0,Math.max(10,e*t*4),{...wf,extendInt:"downX"}).root:0}function Mf(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=df(c,t,n,(1+s)/2),l=df(c,t,n,(1-s)/2);c<=Vr((1-s)/2,t,n)&&(l=0),c<=Vr((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 qd(e){let t=Re([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 ui(e,t,n){if(typeof e!="object"||e===null)throw new TypeError(gf);if(li(e)||vf(e)||Nf(e))return Id(e,t,n);let r=Sd(e)?je(()=>ii(e)):e;if(!Array.isArray(r.colNames)||!Array.isArray(r.table))throw new TypeError(gf);let o=Wt.get(r),s=o!==void 0&&!Array.isArray(o)?o:r,i=n.ci===void 0?.95:n.ci;mi(i);let c=xe(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)");qd(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,d=p/h,g=f.reduce((R,T)=>R+T.ss,0),w=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:b,ssObserved:M}=yf(N,u),x=R=>{let{ss:T,df:S}=R;if(t==="Eta2")return E==="partial"?T/(T+p):E==="plain"?T/g:T/(T+p+M-(b(R.name)?T:0));if(t==="Omega2"){let L=E==="partial"?(T-S*d)/(T+(w-S)*d):(T-S*d)/(g+d);return Math.max(0,L)}let k=E==="partial"?(T-S*d)/(T+p):(T-S*d)/g;return Math.max(0,k)},y=`${t}${E==="plain"?"":`_${E}`}`;return{colNames:i===null?[y]:[y,"CI","CI_low","CI_high"],rowNames:u.map(R=>R.name),table:u.map(R=>i===null?[x(R)]:Mf(x(R),R.df,h,i,c)),warnings:Array.isArray(r.warnings)?[...r.warnings]:[]}}function Id(e,t,n){let r=n.ci===void 0?.95:n.ci;mi(r);let o=xe(n.alternative??"greater",["two.sided","less","greater"]),s=Nf(e),i,c,a;if(s){let b=Wt.get(e),M=kd(b!==void 0&&!Array.isArray(b)?{...e,univariate:b}:e);({strata:i,subject:c}=M),a=M.order.map(x=>({term:x,stratum:M.strata.find(y=>y.terms.includes(x))}))}else{let b=li(e)?je(()=>ci(e)):e,M=Wt.get(b);({strata:i,subject:c}=Ad(Array.isArray(M)?{...b,strata:M.map((x,y)=>({...b.strata[y],...x}))}:b)),a=i.flatMap(x=>x.terms.map(y=>({term:y,stratum:x})))}let l=Re([[...i.flatMap(b=>b.terms.map(M=>M.ss)),...i.map(b=>b.sse)]]).k;for(let b of i){b.sse=$(b.sse,-l);for(let M of b.terms)M.ss=$(M.ss,-l)}let f=i.flatMap(b=>b.terms),m=i.reduce((b,M)=>b+M.sse,0),u=f.reduce((b,M)=>b+M.ss,0)+m,h=c.sse/c.dfe,p=t==="Eta2"?n.generalized??!1:!1,d=p!==!1?"generalized":n.partial??!0?"partial":"plain",{isObserved:g,ssObserved:w}=yf(p,f),N=(b,M)=>{let{ss:x,df:y}=b,R=M.sse/M.dfe;if(t==="Eta2")return d==="partial"?x/(x+M.sse):d==="plain"?x/u:x/(x+m+w-(g(b.name)?x:0));if(t==="Omega2"){let S=M===c?0:1,k=d==="partial"?(x-y*R)/(x+S*M.sse+c.sse+h):(x-y*R)/(u+h);return Math.max(0,k)}let T=d==="partial"?(x-y*R)/(x+M.sse):(x-y*R)/u;return Math.max(0,T)},v=`${t}${d==="plain"?"":`_${d}`}`,E={colNames:r===null?[v]:[v,"CI","CI_low","CI_high"],rowNames:a.map(b=>b.term.name),table:a.map(({term:b,stratum:M})=>{let x=N(b,M);return r===null?[x]:Mf(x,b.df,M.dfe,r,o)}),warnings:[]};return s||(E.groups=a.map(b=>b.stratum.name),E.warnings=[...li(e)||vf(e)?e.warnings:[]]),E}function _d(e,t={}){return V(t,Nd,"etaSquared"),H("etaSquared",ui(e,"Eta2",t),e)}function Ld(e,t={}){return V(t,yd,"omegaSquared"),H("omegaSquared",ui(e,"Omega2",t),e)}function Cd(e,t={}){return V(t,Md,"epsilonSquared"),H("epsilonSquared",ui(e,"Epsilon2",t),e)}var Od=new Set(["eqb","lowEqbound","highEqbound","hypothesis","paired","varEqual","alpha","mu","eqboundType","biasCorrection","rmCorrection","glass","smdCi"]),Fd=["m1","sd1","n1","m2","sd2","n2","r12"],Dd=new Set(["eqb","lowEqbound","highEqbound","hypothesis","paired","varEqual","alpha","mu","eqboundType","biasCorrection","rmCorrection","glass","smdCi"]),Pd="Equivalence interval does not include zero.",Vd="Warning: setting bound type to SMD produces biased results!",$d="MET test may have higher error rates than a nil two-tailed test. Consider wider equivalence bounds.";function Ef(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 pr(s=>dn(e,t,{ncp:s,lowerTail:!r})-n,e-o,e+o,{tol:0,maxiter:5e3,extendInt:r?"upX":"downX"}).root}function zd(e,t){let{alpha:n,smdCi:r}=t,o=Math.abs(e.delta),s=(y,R,T)=>(1-(T-2)/(T*y*y))*R*R,i,c,a,l,f,m,u,h,p,d,g,w,N=t.biasCorrection;if(e.design==="two"){let{n1:y,n2:R,s1:T,s2:S}=e,k=T*T,L=S*S,_=2*y*R/(y+R);if(t.glass!==void 0){let I=t.glass==="glass1",[F,C,D,A]=I?[T,y,S,R]:[S,R,T,y];c=F,a=C-1,i=N?"Glass's delta(g)":"Glass's delta(d)",l=N?Mr(a):1,f=l*o/c,p=1/R+1/y;let O=D/F;m=Math.sqrt(O*O/(A-1)+1/(C-1)+f*f/(2*(C-1))),u=o/(F*Math.sqrt(1/C+O*O/A)),h=y+R-2,d=Math.sqrt(2/_)}else if(t.varEqual)c=Math.sqrt(((y-1)*k+(R-1)*L)/(y+R-2)),a=y+R-2,i=N?"Hedges's g":"Cohen's d",l=N?Mr(a):1,f=l*o/c,p=1/y+1/R,m=Math.sqrt(1/y+1/R+s(l,f,a)),u=o/(c*Math.sqrt(p)),h=a,d=Math.sqrt(2*(R*k+y*L)/(y*R*(k+L)));else{c=Math.sqrt((k+L)/2),a=(y-1)*(R-1)*(k+L)**2/((R-1)*k*k+(y-1)*L*L),i=N?"Hedges's g(av)":"Cohen's d(av)",l=N?Mr(a):1,f=l*o/c,p=2*(R*k+y*L)/(y*R*(k+L));let I=c**4;m=Math.sqrt(f*f*(k*k/(y-1)+L*L/(R-1))/(8*I)+(k/(y-1)+L/(R-1))/(c*c)),u=o/Math.sqrt(k/y+L/R),h=a,d=Math.sqrt(2/_)}g=a,w=(I,F)=>Math.sqrt(g/(g-2)*(2/_)*(1+I*I*_/2)-I*I/(F*F))}else if(e.design==="paired"){let{n:y,s1:R,s2:T,sdif:S,oneMinusR:k}=e,L=t.glass!==void 0?"":t.rmCorrection?"(rm)":"(z)";if(t.glass!==void 0?c=t.glass==="glass1"?R:T:c=t.rmCorrection?S/Math.sqrt(2*k):S,i=t.glass!==void 0?N?"Glass's delta(g)":"Glass's delta(d)":N?`Hedges's g${L}`:`Cohen's d${L}`,a=y-1,g=2*y-2,l=N?Mr(r==="goulet"?g:a):1,f=l*o/c,p=1/y,u=o*Math.sqrt(y)/c,h=a,d=Math.sqrt(2*k/y),w=(_,I)=>{let F=r==="goulet"?g:a,C=Math.sqrt(F/(F-2)*(2*k/y)*(1+_*_*y/(2*k))-_*_/(I*I));return t.rmCorrection?C:C*Math.sqrt(2*k)},t.glass!==void 0){let _=S/c;m=Math.sqrt(_*_/(y-1)+f*f/(2*(y-1)))}else!t.rmCorrection&&r==="nct"?m=Math.sqrt(1/y+s(l,f,a)):m=w(f,l)}else{let{n:y,s:R}=e;c=R,a=y-1,g=a,i=N?"Hedges's g":"Cohen's d",l=N?Mr(a):1,f=l*o/c,p=1/y,m=Math.sqrt(1/y+f*f/(2*y)),u=o*Math.sqrt(y)/R,h=a,d=1/Math.sqrt(y),w=(T,S)=>Math.sqrt(a/(a-2)*(1/y)*(1+T*T*y)-T*T/(S*S))}let v,E,b,M=a;if(r==="nct"){b=m,M=h;let y=Ef(u,h,n,!0),R=Ef(u,h,n,!1);v=y*Math.sqrt(p)*l,E=R*Math.sqrt(p)*l}else if(r==="goulet"){b=w(f,l),M=g;let y=f/d;v=rn(n,g,{ncp:y})*d,E=rn(n,g,{ncp:y,lowerTail:!1})*d}else{b=m;let y=r==="t"?xt(n,a,!1,!1):oe(n,{lowerTail:!1});v=f-y*b,E=f+y*b}let x=e.delta<0;return{label:i,denom:c,estimate:x?-f:f,se:b,confInt:x?[-E,-v]:[v,E],df:M,J:l}}function xf(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 Rf(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 Tf(e){let t=xe(e.hypothesis,["EQU","MET"]),n=xe(e.eqboundType,["raw","SMD"])==="SMD",r=xe(e.smdCi,["nct","goulet","t","z"]),o=e.glass===void 0?void 0:xe(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 Sf(e,t,n,r,o,s,i,c,a,l,f,m){let{alpha:u,hypothesis:h}=l,p=zd(c,l),d=$(p.denom,a),g=l.smdBounds?[f[0]*d,f[1]*d]:f,w=D=>{if(D===0)return[0,n];let A=Math.floor(Math.log2(Math.abs(D))),O=et(D,-A);return l.smdBounds?[O*p.denom,A+a]:[O,A]},N=l.smdBounds?f:[f[0]/d,f[1]/d];l.smdBounds&&m.push(Vd),g[0]<=0&&g[1]>=0||m.push(Pd);let v=D=>$(D/r,n-o),E=D=>{let[A,O]=w(D),q=Math.max(n,O);return et((et(t,n-q)-et(A,O-q))/r,q-o)},b=v(t),M=E(f[0]),x=E(f[1]),y=$(r,o),R=h==="EQU",T={t:b,se:y,df:s,pValue:2*vt(-Math.abs(b),s,!0,!1)},S={t:M,se:y,df:s,pValue:vt(M,s,!R,!1)},k={t:x,se:y,df:s,pValue:vt(x,s,R,!1)},L=R?Math.max(S.pValue,k.pValue):Math.min(S.pValue,k.pValue);!R&&g[0]<=0&&g[1]>=0&&L<=T.pValue&&m.push($d);let _=xt(u,s,!1,!1),I=Math.max(n,o),[F,C]=[$(t,n-I),$(r,o-I)];return H(e,{tTest:T,lower:S,upper:k,pValue:L,bounds:{raw:g,smd:N},raw:{estimate:$(t,n),se:y,confInt:[$(F-_*C,I),$(F+_*C,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 jd(e,t,n={}){V(n,Od,"tTOST");let r=Tf(n),o=Rf(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");xf(e,"x"),a||xf(t,"y");let l=je(()=>a?vn(e,{mu:c}):vn(e,t,{paired:s,varEqual:i,mu:c})),f=l.parameter,m,u,h,p;if(a){let d=Re([Ct(Array.from(e))]);u=d.k;let g=d.groups[0],w=ot(g);h=st(g)-$(c,-u),p=Math.sqrt(w/g.length),m={design:"one",n:g.length,delta:h,s:Math.sqrt(w)}}else if(s){let[d,g]=Gn(Array.from(e),Array.from(t)),w=Re([d,g]);u=w.k;let[N,v]=w.groups,E=N.map((T,S)=>T-v[S]),b=Math.sqrt(ot(N)),M=Math.sqrt(ot(v)),x=ot(E),y=Math.sqrt(x),R=(y-(b-M))*(y+(b-M))/(2*b*M);h=st(E)-$(c,-u),p=Math.sqrt(x/E.length),m={design:"paired",n:E.length,delta:st(N)-st(v)-$(c,-u),s1:b,s2:M,sdif:y,oneMinusR:R}}else{let d=Re([Ct(Array.from(e)),Ct(Array.from(t))]);u=d.k;let[g,w]=d.groups;if(g.length<2||w.length<2)throw new RangeError("not enough observations: at least 2 are needed in each sample");let[N,v,E,b]=[g.length,w.length,ot(g),ot(w)];h=st(g)-st(w)-$(c,-u),p=Math.sqrt(i?((N-1)*E+(v-1)*b)/(N+v-2)*(1/N+1/v):Math.sqrt(E/N)**2+Math.sqrt(b/v)**2),m={design:"two",n1:N,n2:v,delta:h,s1:Math.sqrt(E),s2:Math.sqrt(b)}}return Sf("tTOST",h,u,p,u,f,l.method,m,u,r,o,[])}function Kd(e,t={}){V(e,Fd,"tsumTOST"),V(t,Dd,"tsumTOST");let n=Tf(t),r=Rf(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 C=m(I,F);if(!(Number.isInteger(C)&&C>=2))throw new RangeError(`'${F}' must be an integer of at least 2`);return C},h=(I,F)=>{let C=m(I,F);if(!(C>0))throw new RangeError(`'${F}' must be positive`);return C};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 d=[],{groups:g,k:w}=Re([[o,...p?[]:[c],n.mu]]),{groups:N,k:v}=Re([[s,...p?[]:[a]]]),[E,b=NaN]=p?[g[0][0]]:[g[0][0],g[0][1]],M=g[0][g[0].length-1],[x,y=NaN]=N[0],R=I=>$(I,w-v),T,S,k,L,_;if(p)T=E-M,S=x/Math.sqrt(i),k=i-1,L="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&&(d.push("Unequal number of pairs; using smallest n"),F=Math.min(i,l));let C=Math.sqrt(x*x+y*y-2*I*x*y);if(!(C>0))throw new RangeError("the standard deviation of the differences is 0");T=E-b-M,S=C/Math.sqrt(F),k=F-1,L="Paired t-test",_={design:"paired",n:F,delta:R(T),s1:x,s2:y,sdif:C,oneMinusR:1-I}}else{let I=l,F=x*x,C=y*y;if(T=E-b-M,n.varEqual){let D=((i-1)*F+(I-1)*C)/(i+I-2);S=Math.sqrt(D*(1/i+1/I)),k=i+I-2,L=" Two Sample t-test"}else{let D=F/i,A=C/I;S=Math.sqrt(D+A),k=(D+A)**2/(D*D/(i-1)+A*A/(I-1)),L="Welch Two Sample t-test"}_={design:"two",n1:i,n2:I,delta:R(T),s1:x,s2:y}}return Sf("tsumTOST",T,w,S,v,k,L,_,v,n,r,d)}var Wd=["alternative","tsmethod"],Yd=["square","central"],Bd=1e3,Ud=32,Gd=.9,Af=1+1e-7,Hd=2**24,Xd=34e7;function Qd(e,t={}){if(V(t,Wd,"boschlooTest"),e.length!==2||e.some(M=>M.length!==2))throw new TypeError("'table' must be a 2 x 2 table");let n=Ge(t.alternative),r=xe(t.tsmethod,Yd),o=e.map((M,x)=>Array.from(M,(y,R)=>Br(y,`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)>Hd)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(Bd,Math.ceil(Ud*Math.sqrt(m))),h=M=>Math.min(M+1,40*Math.sqrt(M));if(u*(h(l)+h(f)+4)>Xd)throw new RangeError(`Boschloo's test of these group sizes (${l} and ${f}) would take too long`);let p=s/l-c/f,d;n!=="two.sided"?d=n:r==="square"?d="square":d=p>0?"greater":"less";let g=s+c,w;d==="less"?w=on(s,l,f,g,!0,!1):d==="greater"?w=on(s-1,l,f,g,!1,!1):w=je(()=>So(o)).pValue;let N=Zd(l,f,s,c,d),{value:v,at:E}=Jd(N,l,f,u),b=n==="two.sided"&&r==="central"?Math.min(1,2*v):v;return H("boschlooTest",{method:"Boschloo's Exact Test",alternative:n,statistic:w,statisticName:"test statistic",pValue:b,estimate:p,estimateNames:"difference in proportion",nullValue:0,nullValueName:"difference in proportion",nuisanceParameter:E,warnings:[]})}function Zd(e,t,n,r,o){let s=e+t,i=new Float64Array(s+1);for(let w=2;w<=s;w++)i[w]=ss(w);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 w=0;w<=s;w++){let N=Math.max(0,w-t),E=Math.min(w,e)-N+1,b=-1/0;for(let y=0;y<E;y++){let R=N+y,T=-i[R]-i[e-R]-i[w-R]-i[t-w+R];f[y]=T,T>b&&(b=T)}let M=0;for(let y=0;y<E;y++)f[y]=Math.exp(f[y]-b),M+=f[y];for(let y=0;y<E;y++)f[y]=f[y]/M;let x=0;for(let y=0;y<E;y++)m[y]=x+=f[y];x=0;for(let y=E-1;y>=0;y--)u[y]=x+=f[y];if(o==="less")for(let y=0;y<E;y++)a[(N+y)*c+w-N-y]=m[y];else if(o==="greater")for(let y=0;y<E;y++)a[(N+y)*c+w-N-y]=u[y];else{let y=0;for(let R=1;R<E;R++)f[R]>f[y]&&(y=R);for(let R=0;R<E;R++){let T=f[R]*Af,S=-1,k=y;for(;k-S>1;){let F=S+k>>1;f[F]<=T?S=F:k=F}let L=y,_=E;for(;_-L>1;){let F=L+_>>1;f[F]<=T?_=F:L=F}let I=f[y]<=T?1:(S>=0?m[S]:0)+(_<E?u[_]:0);a[(N+R)*c+w-N-R]=Math.min(1,I)}}}let h=a[n*c+r]*Af,p=new Int32Array(e+2),d=[],g=[];for(let w=0;w<=e;w++){p[w]=d.length;let N=0;for(;N<=t;){if(a[w*c+N]<=h){let v=N;for(;N+1<=t&&a[w*c+N+1]<=h;)N++;d.push(v),g.push(N)}N++}}return p[e+1]=d.length,{starts:p,from:Int32Array.from(d),to:Int32Array.from(g)}}function kf(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]=fn(r,e,t,!1);let o=$t(1,-t),s=lt([t,0],o),i=lt(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 Jd(e,t,n,r){let{starts:o,from:s,to:i}=e,c=new Float64Array(t+1),a=new Float64Array(n+1),l=new Float64Array(n+1),f=new Float64Array(n+1),m=v=>{let[E,b]=kf(t,v,c),[M,x]=kf(n,v,a),y=0;for(let T=M;T<=x;T++)l[T]=y+=a[T];y=0;for(let T=x;T>=M;T--)f[T]=y+=a[T];let R=0;for(let T=E;T<=b;T++){let S=c[T];if(S===0)continue;let k=0;for(let L=o[T];L<o[T+1];L++){let _=s[L],I=i[L];if(!(I<M||_>x))if(_===0)k+=l[Math.min(I,x)];else if(I===n)k+=f[Math.max(_,M)];else for(let F=Math.max(_,M);F<=Math.min(I,x);F++)k+=a[F]}R+=S*k}return Math.min(1,R)},u=Math.PI/2,h=v=>v<=0?0:v>=u?1:Math.sin(v)**2,p=new Float64Array(r+2),d=0;for(let v=0;v<=r+1;v++)p[v]=m(h(u*v/(r+1))),p[v]>p[d]&&(d=v);let g=p[d],w=h(u*d/(r+1));if(g>=1)return{value:1,at:w};let N=Gd*g;for(let v=0;v<=r+1;v++){let E=p[v];if(E<N||v>0&&!(E>p[v-1])||v<=r&&!(E>=p[v+1]))continue;let b=u*Math.max(0,v-1)/(r+1),M=u*Math.min(r+1,v+1)/(r+1),y=nl(S=>-m(h(S)),b,M,1e-14).x,R=m(h(y)),T=1e-4/Math.sqrt(2*(t+n));for(let S=0;S<2&&(y-T>b&&y+T<M);S++,T*=.01){let k=m(h(y-T)),L=m(h(y+T)),_=k-2*R+L;if(!(_<0))break;let I=T*(k-L)/(2*_);if(!(Math.abs(I)<T))break;let F=m(h(y+I));F>R*(1+2**-50)&&(y+=I,R=F)}R>g&&(g=R,w=h(y))}return{value:g,at:w}}var qf=["correct"];function e0(e,t,n={}){at(t)&&V(t,qf,"mcnemarTest"),V(n,qf,"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=t0(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=If(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+=If(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:gt(i,a/2,2,!1,!1),warnings:[]}}function If(e,t,n){let r=t+n;return r<1/0?Dt(e,r):Dt($(e,-520),$(t,-1040)+$(n,-1040))}function t0(e,t){if(e.length!==t.length)throw new RangeError("'x' and 'y' must have the same length");let n=[],r=en(e),o=en(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(Vn),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 n0=["ratio","alternative","confLevel"],hi=2**-1022;function _f(e,t,n={}){if(V(n,n0,"varTest"),typeof e=="string"){let v=Qn(e,t);return _f(v.x,v.y,n)}let r=Array.from(e).filter(Number.isFinite),o=Array.from(t).filter(Number.isFinite),s=n.ratio??1;if(!(Number.isFinite(s)&&s>0))throw new RangeError("'ratio' must be a single positive number");let i=Ge(n.alternative),c=n.confLevel??.95;It(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]=Et([r,o]).variance,u=0;if(!(f>=hi&&m>=hi)){let v=Et([r]),E=Et([o]);f=v.variance[0],m=E.variance[0],u=2*(v.k-E.k)}if(f===0&&m===0)throw new RangeError("both samples have zero variance");let h=u===0?f/m:et(f/m,u),p=h/s,d=wt(p,a,l,!0,!1),g=wt(p,a,l,!1,!1),w,N;if(i==="less")w=d,N=[0,h/ln(1-c,a,l,!0,!1)];else if(i==="greater")w=g,N=[h/ln(c,a,l,!0,!1),1/0];else{w=Math.min(1,2*Math.min(d,g));let v=(1-c)/2,E=v<1e-8?ln(v,a,l,!1,!1):ln(1-v,a,l,!0,!1);N=[h/E,h/ln(v,a,l,!0,!1)]}return{method:"F test to compare two variances",alternative:i,statistic:p,statisticName:"F",parameter:[a,l],parameterName:["num df","denom df"],pValue:w,confInt:N,confLevel:c,estimate:h,estimateNames:"ratio of variances",nullValue:s,nullValueName:"ratio of variances",warnings:[]}}function Lf(e,t){if(typeof e=="string"){let p=vo(e,t);return Lf(el(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=Et(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<hi)){let p=o.map(E=>{let b=Et([E]);return{w:b.variance[0],e:b.k}}),d=Math.max(...p.map(E=>E.e)),g=p.map(E=>Math.log(E.w)+2*(E.e-d)*Math.LN2),w=g.map((E,b)=>Math.log(s[b])+E),N=Math.max(...w),v=N+Math.log(w.reduce((E,b)=>E+Math.exp(b-N),0))-Math.log(c);m=c*v-g.reduce((E,b,M)=>E+s[M]*b,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:gt(u,h/2,2,!1,!1),warnings:[]}}var r0=["alternative"],pi=["alternative"];function di(e,t,n,r){return{method:e,statistic:n,statisticName:t,parameter:r,parameterName:"df",pValue:gt(n,r/2,2,!1,!1),warnings:[]}}function Cf(e){let t=0;for(let n of pn(e))t+=n*n*n-n;return t}function Vs(e,t){if(typeof e=="string"){let l=vo(e,t);return Vs(l.values,l.group.codes)}let n=dr(e,t,"error"),r=n.length;if(r<2)throw new RangeError("all observations are in the same group");let o=n.flat(),s=o.length,i=bt(o),c=0,a=0;for(let l of n){let f=0;for(let m=0;m<l.length;m++)f+=i[a+m];a+=l.length,c+=f*f/l.length}return c=(12*c/(s*(s+1))-3*(s+1))/(1-Cf(o)/(s*s*s-s)),di("Kruskal-Wallis rank sum test","Kruskal-Wallis chi-squared",c,r-1)}function gi(e,t){if(typeof e=="string")return gi(us(e,t));let n=va(e),r=n.length,o=e[0]?.length??0;if(r<2||o<2)throw new RangeError("'y' must have at least two complete rows and two columns");let s=new Array(o).fill(0),i=0;for(let l of n){let f=bt(l);for(let m=0;m<o;m++)s[m]+=f[m];i+=Cf(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 o0(e,t={},n,r={}){typeof e!="string"&&at(t)&&V(t,pi,"pageTest"),at(n)&&V(n,pi,"pageTest"),V(r,pi,"pageTest");let o,s;if(typeof e=="string")o=us(e,t),s=n??{};else if(Array.isArray(n)){let d=Array.from(e),g=t,w=n;if(d.length!==g.length||d.length!==w.length)throw new RangeError("'x', 'groups' and 'blocks' must have the same length");if(!d.every(Number.isFinite))throw new RangeError("missing or non-finite values are not allowed");let N=en(g),v=en(w);if(N.some(y=>typeof y=="number"&&Number.isNaN(y))||v.some(y=>typeof y=="number"&&Number.isNaN(y)))throw new RangeError("missing treatment or block labels are not allowed");let E=Array.from(new Set(N)).sort(Vn),b=Array.from(new Set(v)).sort(Vn);if(E.length<2||b.length<1)throw new RangeError("at least two treatments and one block are required");let M=new Map(E.map((y,R)=>[y,R])),x=new Map(b.map((y,R)=>[y,R]));o=b.map(()=>new Array(E.length).fill(NaN));for(let y=0;y<d.length;y++){let R=x.get(v[y]),T=M.get(N[y]);if(!Number.isNaN(o[R][T]))throw new RangeError("not an unreplicated complete block design");o[R][T]=d[y]}if(o.some(y=>y.some(R=>Number.isNaN(R))))throw new RangeError("not an unreplicated complete block design");s=r}else o=e.map(d=>Array.from(d)),s=t;let i=o.length,c=o[0]?.length??0;if(i<1||c<2||o.some(d=>d.length!==c))throw new RangeError("'y' must be a non-empty matrix with at least two columns");if(o.some(d=>d.some(g=>!Number.isFinite(g))))throw new RangeError("missing or non-finite values are not allowed in a block");let a=Ge(s.alternative),l=new Array(c).fill(0);for(let d of o){let g=bt(d);for(let w=0;w<c;w++)l[w]+=g[w]}let f=0;for(let d=0;d<c;d++)f+=(d+1)*l[d];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"?ie(h):a==="greater"?ie(h,{lowerTail:!1}):2*ie(-Math.abs(h));return{method:"Page's ordered aligned rank sum test",alternative:a,statistic:h,statisticName:"z",pValue:p,estimate:f,warnings:[]}}function s0(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{...gi(e),method:"Cochran's Q test",statisticName:"Q"}}function Of(e,t){if(typeof e=="string")return Of(us(e,t));let n=va(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(d=>At(d)-sn(d));s.some(d=>d===1/0)&&n.every(d=>d.every(Number.isFinite))&&(s=Re(n).groups.map(d=>At(d)-sn(d)));let i=bt(s),c=new Array(o).fill(0),a=0;n.forEach((d,g)=>{let w=bt(d);for(let N=0;N<o;N++){let v=i[g]*(w[N]-(o+1)/2);a+=v*v,c[N]+=v}});let l=0;for(let d of c)l+=d*d;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 d=0;for(let g=2;g<=o;g++)d+=Math.log(g);h=Math.exp((1-r)*d)}else u=(r-1)*l/(a-l),h=wt(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 Ff(e,t){if(typeof e=="string"){let h=vo(e,t);return Ff(h.values,h.group.codes)}let n=dr(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 d=ws(p);return p.map(g=>Math.abs(g-d))}),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(Re(n).groups));let i=s.length,a=bt(s).map(h=>oe((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 d=0;d<h.length;d++)p+=a[u+d];u+=h.length,m+=h.length*(p/h.length-f)**2}return m/=ot(a),di("Fligner-Killeen test of homogeneity of variances","Fligner-Killeen:med chi-squared",m,r-1)}function Df(e,t,n={}){if(V(n,r0,"moodTest"),typeof e=="string"){let E=Qn(e,t);return Df(E.x,E.y,n)}let r=Array.from(e).filter(Number.isFinite),o=Array.from(t).filter(Number.isFinite),s=Ge(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,b)=>b).sort((E,b)=>l[E]-l[b]),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 b=0;for(let M=u;M<=E;M++)b+=(M+1-(a+1)/2)**2;b/=E-u+1;for(let M=u;M<=E;M++)m[f[M]]=b;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 d=0;for(let E of m)d+=(E-p)**2;let g=i*p,w=i*c/(a*(a-1))*d,N=(h-g)/Math.sqrt(w),v=s==="less"?ie(N):s==="greater"?ie(N,{lowerTail:!1}):2*ie(-Math.abs(N));return{method:"Mood two-sample test of scale",alternative:s,statistic:N,statisticName:"Z",pValue:v,warnings:[]}}function Er(e,t){let n=0;for(let r=e.length-1;r>=0;r--)n=n*t+e[r];return n}var a0=[0,.221157,-.147981,-2.07119,4.434685,-2.706056],i0=[0,.042981,-.293762,-1.752461,5.682633,-3.582633],c0=[.544,-.39978,.025054,-6714e-7],l0=[1.3822,-.77857,.062767,-.0020322],f0=[-1.5861,-.31082,-.083751,.0038915],m0=[-.4803,-.082676,.0030302],u0=[-2.273,.459];function h0(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]=-oe((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+Er(a0,i),a=new Float64Array(n);a[0]=c;let l,f;if(e>5){let m=r[1]/s+Er(i0,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 Pf(e){let t=Re([e]).groups[0],n=t.length,r=h0(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 p0=1.90985931710274,d0=1.0471975511966;function Vf(e,t){if(Number.isNaN(e)||!(t>=3))return NaN;if(t===3){let i=p0*(Math.asin(Math.sqrt(1-e))-d0);return Math.max(i,0)}let n=Math.log(e);if(t<=11){let i=Er(u0,t);if(n>=i)return 1e-99;let c=Er(c0,t),a=Math.exp(Er(l0,t));return ie(-Math.log(i-n),{mean:c,sd:a,lowerTail:!1})}let r=Math.log(t),o=Er(f0,r),s=Math.exp(Er(m0,r));return ie(n,{mean:o,sd:s,lowerTail:!1})}var Kf=Math.log(2*Math.PI),$f=2e-14,zf=500;function g0(e){return Math.exp(-.5*e*e-.5*Kf)}function w0(e){return ie(e,{logP:!0})}function Wf(e,t=-1/0,n=!1){let r=t===-1/0?ga(e,0,2,0,$f,zf):ga(e,t,1,0,$f,zf),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 jf(e,t,n){let r=t.map(Math.sqrt),o=t.map(i=>Math.sqrt(1-i));return Wf(i=>{let c=0;for(let a=0;a<r.length;a++){let l=(r[a]*i+e)/o[a];if(!n){c+=w0(l);continue}let f=(r[a]*i-e)/o[a],m=ie(f)+ie(l,{lowerTail:!1});m<.5?c+=Math.log1p(-m):c+=Math.log(Math.max(0,l+f<0?ie(l)-ie(f):ie(f,{lowerTail:!1})-ie(l,{lowerTail:!1})))}return g0(i)*-Math.expm1(c)})}function xr(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,jf(e,t,r));let o=n/2,s=Math.log(2)+.5*Math.log(o)-.5*Kf-_c(o);return Math.min(1,Wf(c=>{let a=s-o*(Math.expm1(2*c)-2*c);return a<-745?0:Math.exp(a)*jf(e*Math.exp(c),t,r)},-1/0,!0))}var b0=["pAdjustMethod"],v0=["pAdjustMethod","alternative"],N0=["alternative"],y0=["pAdjustMethod","alternative"],M0=["method","altp"],E0=["alternative"],x0=["welch"],Yf="Ties are present. z-quantiles were corrected for ties.";function Sn(e,t){let{labels:n,groups:r}=Zn(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 wi(e){let t=e.groups.flat(),n=bt(t),r=[],o=0;for(let i of e.n){let c=0;for(let a=0;a<i;a++)c+=n[o+a];o+=i,r.push(c/i)}let s=0;for(let i of pn(t))s+=i*i*i-i;return{R:r,C:s/(12*(e.N-1))}}function Rr(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 R0(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=Yr(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 Lo(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 bi(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 $s=e=>ie(e,{lowerTail:!1}),vi=(e,t,n)=>as(Math.abs(e),t,n,{lowerTail:!1});function Ni(e){let t=Et(e.groups),n=0;for(let r of t.n)r>1&&(n+=r-1);return Co(n,t.ssWithin),{m:t.mean,s2:t.ssWithin/n,df:e.N-e.k,k:t.k}}function Co(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 T0(e,t,n={}){V(n,b0,"kwAllPairsDunnTest");let r=n.pAdjustMethod??"holm",o=Sn(e,t),{R:s,C:i}=wi(o),c=o.N*(o.N+1)/12-i,a=Rr(o.k,(f,m)=>Math.abs(s[f]-s[m])/Math.sqrt(c*(1/o.n[f]+1/o.n[m]))),l=R0(a.map(f=>f.map(m=>2*$s(m))),r);return H("kwAllPairsDunnTest",Lo(o,"Dunn's all-pairs test",a,l,r,i!==0?[Yf]:[]))}function S0(e,t,n={}){V(n,v0,"kwManyOneDunnTest");let r=Ge(n.alternative),o=n.pAdjustMethod??"single-step",s=Sn(e,t),{R:i,C:c}=wi(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=Bf(f,m,r,o);return H("kwManyOneDunnTest",bi(s,"Dunn's many-to-one test",f,u,o,c!==0?[Yf]:[]))}function Bf(e,t,n,r){return r==="single-step"?e.map(o=>n==="two.sided"?xr(o,t,1/0,!0):xr(n==="greater"?o:-o,t,1/0,!1)):Yr(e.map(o=>n==="two.sided"?2*$s(Math.abs(o)):$s(n==="greater"?o:-o)),r)}function A0(e,t,n={}){V(n,N0,"dunnettTest");let r=Ge(n.alternative),o=Sn(e,t),{m:s,s2:i,df:c}=Ni(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*xr(Math.abs(h),m,c,!1)):xr(r==="greater"?h:-h,m,c,!1));return H("dunnettTest",bi(o,`Dunnett's-test for multiple
4
4
  comparisons with one control`,f,u,"single-step"))}function k0(e,t,n={}){V(n,y0,"manyOneUTest");let r=Ge(n.alternative),o=n.pAdjustMethod??"single-step",s=Sn(e,t);if(o==="single-step"&&s.k<3)throw new RangeError(`for 'p.adjust.method = single-step' at least
5
5
 
@@ -1,2 +1,2 @@
1
- /*! rStats.js 1.17.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{t as k}from"./chunks/chunk-XEJX5HBJ.js";import{l as v}from"./chunks/chunk-GTSK4IN4.js";var L=["type","lagMax","demean","naAction","ci"],z=["lagMax","naAction"],B=["type","lagMax","demean","naAction","ci"];function S(r){if(!Array.isArray(r))throw new TypeError("'x' must be numeric");if(r.length===0)throw new RangeError("'ts' object must have one or more observations");if(Array.isArray(r[0])){let t=r;if(t.some(e=>!Array.isArray(e)||e.length!==1))throw new RangeError("multivariate time series are not supported");if(t.some(e=>typeof e[0]!="number"))throw new TypeError("'x' must be numeric");return t.map(e=>e[0])}let n=r;if(n.some(t=>typeof t!="number"))throw new TypeError("'x' must be numeric");return n}function R(r,n){if(n!=="fail"&&n!=="pass")throw new RangeError("'naAction' must be 'fail' or 'pass'");if(n==="fail"&&r.some(Number.isNaN))throw new RangeError("missing values in object")}function x(r){let n=r.filter(a=>!Number.isNaN(a));if(n.length===0)return NaN;if(n.some(a=>!Number.isFinite(a))){let a=0;for(let o of n)a+=o;return a/n.length}let t=n[0],e=0,s=0;for(let a of n){let o=a-t-s,i=e+o;s=i-e-o,e=i}return t+e/n.length}function T(r,n,t){let e=n===void 0?Math.floor(10*Math.log10(r)):n;if(!Number.isFinite(e)||e<t)throw new RangeError(t===1?"'lag.max' must be at least 1":"'lag.max' must be at least 0");return Math.min(Math.floor(e),r-1)}function q(r,n){if(!n)return Array.from(r);let t=r.filter(i=>!Number.isNaN(i));if(t.length===0)return r.map(()=>NaN);let e=t[0];if(!Number.isFinite(e)||t.some(i=>!Number.isFinite(i))){let i=x(r);return r.map(c=>c-i)}let s=r.map(i=>Number.isNaN(i)?NaN:i-e),a=x(s),o=e+a;return r.map(i=>Number.isNaN(i)?NaN:i-o)}function Y(r){let n=0;for(let e of r)Number.isFinite(e)&&(n=Math.max(n,Math.abs(e)));return n===0?1:2**Math.max(-1074,Math.min(1023,Math.floor(Math.log2(n))))}function C(r,n){if(r===0||!Number.isFinite(r))return r;let t=r,e=n;for(;e>1023;){if(t*=2**1023,!Number.isFinite(t))return t;e-=1023}for(;e<-1022;){if(t*=2**-1022,t===0)return 0;e+=1022}return t*2**e}function K(r,n,t){if(r===0||!Number.isFinite(r))return r;let e=Math.floor(Math.log2(Math.abs(r))),s=r/2**e;return C(s,Math.round(Math.log2(n))+Math.round(Math.log2(t))+e)}function j(r,n){let t=0,e=0,s=0,a=!1;for(let o of r)Number.isFinite(o)&&(t=Math.max(t,Math.abs(o)),a||(e=o,a=!0),s=Math.max(s,Math.abs(o-e)));return t>2**450||t>0&&t<2**-450?!0:n&&t>2**26*Math.max(1,s)}function E(r,n,t,e,s,a,o){let i=r.length,c=0,b=0,M=0,N=t>=0?0:-t,p=t>=0?i-t:i;for(let u=N;u<p;u++){let m=r[u+t],h=n[u];if(Number.isNaN(m)||Number.isNaN(h))continue;let d=(a?m-e:m)*(a?h-s:h)-b,f=c+d;b=f-c-d,c=f,M++}let l=o?M+Math.abs(t):i;return l?c/l:NaN}function F(r,n,t,e){let s=r.length,a=0,o=0,i=0,c=t>=0?0:-t,b=t>=0?s-t:s;for(let N=c;N<b;N++){let p=r[N+t],l=n[N];if(Number.isNaN(p)||Number.isNaN(l))continue;let m=p*l-o,h=a+m;o=h-a-m,a=h,i++}let M=e?i+Math.abs(t):s;return M?a/M:NaN}function _(r){if(r.some(e=>!Number.isFinite(e)))return Array.from({length:Math.max(0,r.length-1)},()=>NaN);let n=[],t=[];for(let e=1;e<r.length;e++){let s=r[e],a=1;for(let c=1;c<e;c++)s-=t[c-1]*r[e-c],a-=t[c-1]*r[c];if(a===0){for(n.push(NaN);n.length<r.length-1;)n.push(NaN);break}let o=s/a,i=new Array(e);for(let c=1;c<e;c++)i[c-1]=t[c-1]-o*t[e-c-1];i[e-1]=o,t.splice(0,t.length,...i),n.push(o)}return n}function D(r,n,t,e){let s=t.naAction??"fail";if(R(r,s),R(n,s),r.length!==n.length)throw new RangeError("time series must have the same length");let a=r.length,o=t.demean??!0,i=j(r,o)||e&&j(n,o),c=T(a,t.lagMax,e||t.type!=="partial"?0:1),b;if(i){let p=q(r,o),l=e?q(n,o):p,u=Y(p),m=e?Y(l):u,h=p.map(f=>f/u),w=e?l.map(f=>f/m):h,d=[];for(let f=0;f<=c;f++)d.push(F(h,w,f,s==="pass"));if(t.type==="covariance")b=d.map(f=>K(f,u,m));else if(a===1&&t.type==="correlation")b=[1];else{let f=d.map(g=>g/d[0]);b=t.type==="partial"?_(f):f}}else{let p=x(r),l=e?x(n):p,u=[];for(let m=0;m<=c;m++)u.push(E(r,n,m,p,l,o,s==="pass"));if(t.type==="covariance")b=u;else if(a===1&&t.type==="correlation")b=[1];else{let m=u.map(h=>h/u[0]);b=t.type==="partial"?_(m):m}}let M=t.type==="partial"?Array.from({length:c},(p,l)=>l+1):Array.from({length:c+1},(p,l)=>l),N=t.ci??.95;if(!Number.isFinite(N)||N<=0||N>=1)throw new RangeError("'ci' must be between 0 and 1");return{lag:M,acf:b,type:t.type,nUsed:a,clim:k((1+N)/2)/Math.sqrt(a)}}function O(r,n={}){v(n,L,"acf");let t=S(r),e=n.type??"correlation";if(e!=="correlation"&&e!=="covariance"&&e!=="partial")throw new RangeError("'type' must be 'correlation', 'covariance' or 'partial'");return D(t,t,{...n,type:e},!1)}function G(r,n={}){return v(n,z,"pacf"),O(r,{...n,type:"partial"})}function H(r,n,t={}){v(t,B,"ccf");let e=S(r),s=S(n),a=t.type??"correlation";if(a!=="correlation"&&a!=="covariance")throw new RangeError("'type' must be 'correlation' or 'covariance'");let o=t.naAction??"fail";if(R(e,o),R(s,o),e.length!==s.length)throw new RangeError("time series must have the same length");let i=e.length,c=t.demean??!0,b=j(e,c)||j(s,c),M=t.lagMax??Math.max(0,Math.floor(10*Math.log10(i/2))),N=T(i,M,0),p=[],l=[];if(b){let m=q(e,c),h=q(s,c),w=Y(m),d=Y(h),f=m.map(y=>y/w),g=h.map(y=>y/d),A=F(f,f,0,o==="pass"),P=F(g,g,0,o==="pass"),U=a==="correlation"?Math.sqrt(A)*Math.sqrt(P):1;for(let y=-N;y<=N;y++){p.push(y);let X=F(f,g,y,o==="pass");l.push(a==="correlation"?X/U:K(X,w,d))}}else{let m=x(e),h=x(s),w=E(e,e,0,m,m,c,o==="pass"),d=E(s,s,0,h,h,c,o==="pass"),f=a==="correlation"?Math.sqrt(w)*Math.sqrt(d):1;for(let g=-N;g<=N;g++){p.push(g);let A=E(e,s,g,m,h,c,o==="pass");l.push(a==="correlation"?A/f:A)}}let u=t.ci??.95;if(!Number.isFinite(u)||u<=0||u>=1)throw new RangeError("'ci' must be between 0 and 1");return{lag:p,acf:l,type:a,nUsed:i,clim:k((1+u)/2)/Math.sqrt(i)}}export{O as acf,H as ccf,G as pacf};
1
+ /*! rStats.js 1.18.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{t as k}from"./chunks/chunk-GEHMND3C.js";import{l as v}from"./chunks/chunk-MVPHISOV.js";var L=["type","lagMax","demean","naAction","ci"],z=["lagMax","naAction"],B=["type","lagMax","demean","naAction","ci"];function S(r){if(!Array.isArray(r))throw new TypeError("'x' must be numeric");if(r.length===0)throw new RangeError("'ts' object must have one or more observations");if(Array.isArray(r[0])){let t=r;if(t.some(e=>!Array.isArray(e)||e.length!==1))throw new RangeError("multivariate time series are not supported");if(t.some(e=>typeof e[0]!="number"))throw new TypeError("'x' must be numeric");return t.map(e=>e[0])}let n=r;if(n.some(t=>typeof t!="number"))throw new TypeError("'x' must be numeric");return n}function R(r,n){if(n!=="fail"&&n!=="pass")throw new RangeError("'naAction' must be 'fail' or 'pass'");if(n==="fail"&&r.some(Number.isNaN))throw new RangeError("missing values in object")}function x(r){let n=r.filter(a=>!Number.isNaN(a));if(n.length===0)return NaN;if(n.some(a=>!Number.isFinite(a))){let a=0;for(let o of n)a+=o;return a/n.length}let t=n[0],e=0,s=0;for(let a of n){let o=a-t-s,i=e+o;s=i-e-o,e=i}return t+e/n.length}function T(r,n,t){let e=n===void 0?Math.floor(10*Math.log10(r)):n;if(!Number.isFinite(e)||e<t)throw new RangeError(t===1?"'lag.max' must be at least 1":"'lag.max' must be at least 0");return Math.min(Math.floor(e),r-1)}function q(r,n){if(!n)return Array.from(r);let t=r.filter(i=>!Number.isNaN(i));if(t.length===0)return r.map(()=>NaN);let e=t[0];if(!Number.isFinite(e)||t.some(i=>!Number.isFinite(i))){let i=x(r);return r.map(c=>c-i)}let s=r.map(i=>Number.isNaN(i)?NaN:i-e),a=x(s),o=e+a;return r.map(i=>Number.isNaN(i)?NaN:i-o)}function Y(r){let n=0;for(let e of r)Number.isFinite(e)&&(n=Math.max(n,Math.abs(e)));return n===0?1:2**Math.max(-1074,Math.min(1023,Math.floor(Math.log2(n))))}function C(r,n){if(r===0||!Number.isFinite(r))return r;let t=r,e=n;for(;e>1023;){if(t*=2**1023,!Number.isFinite(t))return t;e-=1023}for(;e<-1022;){if(t*=2**-1022,t===0)return 0;e+=1022}return t*2**e}function K(r,n,t){if(r===0||!Number.isFinite(r))return r;let e=Math.floor(Math.log2(Math.abs(r))),s=r/2**e;return C(s,Math.round(Math.log2(n))+Math.round(Math.log2(t))+e)}function j(r,n){let t=0,e=0,s=0,a=!1;for(let o of r)Number.isFinite(o)&&(t=Math.max(t,Math.abs(o)),a||(e=o,a=!0),s=Math.max(s,Math.abs(o-e)));return t>2**450||t>0&&t<2**-450?!0:n&&t>2**26*Math.max(1,s)}function E(r,n,t,e,s,a,o){let i=r.length,c=0,b=0,M=0,N=t>=0?0:-t,p=t>=0?i-t:i;for(let u=N;u<p;u++){let m=r[u+t],h=n[u];if(Number.isNaN(m)||Number.isNaN(h))continue;let d=(a?m-e:m)*(a?h-s:h)-b,f=c+d;b=f-c-d,c=f,M++}let l=o?M+Math.abs(t):i;return l?c/l:NaN}function F(r,n,t,e){let s=r.length,a=0,o=0,i=0,c=t>=0?0:-t,b=t>=0?s-t:s;for(let N=c;N<b;N++){let p=r[N+t],l=n[N];if(Number.isNaN(p)||Number.isNaN(l))continue;let m=p*l-o,h=a+m;o=h-a-m,a=h,i++}let M=e?i+Math.abs(t):s;return M?a/M:NaN}function _(r){if(r.some(e=>!Number.isFinite(e)))return Array.from({length:Math.max(0,r.length-1)},()=>NaN);let n=[],t=[];for(let e=1;e<r.length;e++){let s=r[e],a=1;for(let c=1;c<e;c++)s-=t[c-1]*r[e-c],a-=t[c-1]*r[c];if(a===0){for(n.push(NaN);n.length<r.length-1;)n.push(NaN);break}let o=s/a,i=new Array(e);for(let c=1;c<e;c++)i[c-1]=t[c-1]-o*t[e-c-1];i[e-1]=o,t.splice(0,t.length,...i),n.push(o)}return n}function D(r,n,t,e){let s=t.naAction??"fail";if(R(r,s),R(n,s),r.length!==n.length)throw new RangeError("time series must have the same length");let a=r.length,o=t.demean??!0,i=j(r,o)||e&&j(n,o),c=T(a,t.lagMax,e||t.type!=="partial"?0:1),b;if(i){let p=q(r,o),l=e?q(n,o):p,u=Y(p),m=e?Y(l):u,h=p.map(f=>f/u),w=e?l.map(f=>f/m):h,d=[];for(let f=0;f<=c;f++)d.push(F(h,w,f,s==="pass"));if(t.type==="covariance")b=d.map(f=>K(f,u,m));else if(a===1&&t.type==="correlation")b=[1];else{let f=d.map(g=>g/d[0]);b=t.type==="partial"?_(f):f}}else{let p=x(r),l=e?x(n):p,u=[];for(let m=0;m<=c;m++)u.push(E(r,n,m,p,l,o,s==="pass"));if(t.type==="covariance")b=u;else if(a===1&&t.type==="correlation")b=[1];else{let m=u.map(h=>h/u[0]);b=t.type==="partial"?_(m):m}}let M=t.type==="partial"?Array.from({length:c},(p,l)=>l+1):Array.from({length:c+1},(p,l)=>l),N=t.ci??.95;if(!Number.isFinite(N)||N<=0||N>=1)throw new RangeError("'ci' must be between 0 and 1");return{lag:M,acf:b,type:t.type,nUsed:a,clim:k((1+N)/2)/Math.sqrt(a)}}function O(r,n={}){v(n,L,"acf");let t=S(r),e=n.type??"correlation";if(e!=="correlation"&&e!=="covariance"&&e!=="partial")throw new RangeError("'type' must be 'correlation', 'covariance' or 'partial'");return D(t,t,{...n,type:e},!1)}function G(r,n={}){return v(n,z,"pacf"),O(r,{...n,type:"partial"})}function H(r,n,t={}){v(t,B,"ccf");let e=S(r),s=S(n),a=t.type??"correlation";if(a!=="correlation"&&a!=="covariance")throw new RangeError("'type' must be 'correlation' or 'covariance'");let o=t.naAction??"fail";if(R(e,o),R(s,o),e.length!==s.length)throw new RangeError("time series must have the same length");let i=e.length,c=t.demean??!0,b=j(e,c)||j(s,c),M=t.lagMax??Math.max(0,Math.floor(10*Math.log10(i/2))),N=T(i,M,0),p=[],l=[];if(b){let m=q(e,c),h=q(s,c),w=Y(m),d=Y(h),f=m.map(y=>y/w),g=h.map(y=>y/d),A=F(f,f,0,o==="pass"),P=F(g,g,0,o==="pass"),U=a==="correlation"?Math.sqrt(A)*Math.sqrt(P):1;for(let y=-N;y<=N;y++){p.push(y);let X=F(f,g,y,o==="pass");l.push(a==="correlation"?X/U:K(X,w,d))}}else{let m=x(e),h=x(s),w=E(e,e,0,m,m,c,o==="pass"),d=E(s,s,0,h,h,c,o==="pass"),f=a==="correlation"?Math.sqrt(w)*Math.sqrt(d):1;for(let g=-N;g<=N;g++){p.push(g);let A=E(e,s,g,m,h,c,o==="pass");l.push(a==="correlation"?A/f:A)}}let u=t.ci??.95;if(!Number.isFinite(u)||u<=0||u>=1)throw new RangeError("'ci' must be between 0 and 1");return{lag:p,acf:l,type:a,nUsed:i,clim:k((1+u)/2)/Math.sqrt(i)}}export{O as acf,H as ccf,G as pacf};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@numero-uno-jpn/rstats",
3
- "version": "1.17.0",
3
+ "version": "1.18.0",
4
4
  "description": "R-compatible statistical functions for the browser: distributions, random numbers that match set.seed(), hypothesis tests and linear algebra, tested against R.",
5
5
  "keywords": [
6
6
  "statistics",