@numero-uno-jpn/rstats 0.9.0 → 0.11.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (142) hide show
  1. package/CHANGELOG.md +87 -0
  2. package/README.ja.md +64 -12
  3. package/README.md +63 -11
  4. package/THIRD_PARTY_NOTICES.md +7 -2
  5. package/dist/chunks/chunk-5WDJPHYY.js +2 -0
  6. package/dist/chunks/chunk-7GQMKH3Z.js +2 -0
  7. package/dist/chunks/chunk-AHHS6AC4.js +3 -0
  8. package/dist/chunks/chunk-ALM3VHFP.js +2 -0
  9. package/dist/chunks/{chunk-TOVTN5Y2.js → chunk-C5XTWMGB.js} +1 -1
  10. package/dist/chunks/chunk-CGBNKHNZ.js +2 -0
  11. package/dist/chunks/{chunk-LWUSYIQ4.js → chunk-EZAFN5CD.js} +1 -1
  12. package/dist/chunks/{chunk-H2AP6TCR.js → chunk-EZWBRCJL.js} +1 -1
  13. package/dist/chunks/chunk-F5EDJFEO.js +2 -0
  14. package/dist/chunks/{chunk-FC2TLHZC.js → chunk-JWBO5WVN.js} +2 -2
  15. package/dist/chunks/{chunk-LE7TBHNI.js → chunk-TOGXTCZO.js} +2 -2
  16. package/dist/chunks/chunk-VJ4BDBMU.js +3 -0
  17. package/dist/chunks/chunk-Y5CNQ266.js +2 -0
  18. package/dist/chunks/{chunk-BGVMSDVJ.js → chunk-Y5PYRAYP.js} +2 -2
  19. package/dist/chunks/{chunk-5K4PS7IV.js → chunk-YSWGDOWK.js} +2 -2
  20. package/dist/data/grouping.d.ts +79 -0
  21. package/dist/data/grouping.js +262 -0
  22. package/dist/descriptive/dataFrames.d.ts +110 -0
  23. package/dist/descriptive/dataFrames.js +392 -0
  24. package/dist/descriptive/factanal.d.ts +56 -0
  25. package/dist/descriptive/factanal.js +186 -0
  26. package/dist/descriptive/missing.d.ts +35 -0
  27. package/dist/descriptive/missing.js +50 -0
  28. package/dist/descriptive/omega.d.ts +56 -0
  29. package/dist/descriptive/omega.js +323 -0
  30. package/dist/descriptive/pivot.d.ts +89 -0
  31. package/dist/descriptive/pivot.js +343 -0
  32. package/dist/descriptive.js +2 -2
  33. package/dist/distributions/bvnorm.d.ts +15 -0
  34. package/dist/distributions/bvnorm.js +131 -0
  35. package/dist/distributions/nbeta.js +34 -1
  36. package/dist/distributions/nchisq.d.ts +10 -0
  37. package/dist/distributions/nchisq.js +46 -0
  38. package/dist/distributions.js +2 -2
  39. package/dist/entries/descriptive.d.ts +4 -0
  40. package/dist/entries/descriptive.js +4 -0
  41. package/dist/entries/distributions.d.ts +1 -0
  42. package/dist/entries/distributions.js +1 -0
  43. package/dist/entries/linalg.d.ts +2 -0
  44. package/dist/entries/linalg.js +2 -0
  45. package/dist/entries/numerics.d.ts +2 -0
  46. package/dist/entries/numerics.js +1 -0
  47. package/dist/entries/tests.d.ts +22 -1
  48. package/dist/entries/tests.js +19 -1
  49. package/dist/htest/agreement.d.ts +141 -0
  50. package/dist/htest/agreement.js +274 -0
  51. package/dist/htest/ansariTest.d.ts +12 -0
  52. package/dist/htest/ansariTest.js +5 -17
  53. package/dist/htest/aov.d.ts +19 -0
  54. package/dist/htest/aov.js +22 -15
  55. package/dist/htest/cor.d.ts +12 -0
  56. package/dist/htest/cor.js +9 -27
  57. package/dist/htest/core/simulate.d.ts +2 -0
  58. package/dist/htest/core/simulate.js +1 -1
  59. package/dist/htest/dunnett.d.ts +5 -0
  60. package/dist/htest/dunnett.js +89 -0
  61. package/dist/htest/effectSize.d.ts +112 -0
  62. package/dist/htest/effectSize.js +385 -0
  63. package/dist/htest/ksTest.d.ts +13 -0
  64. package/dist/htest/ksTest.js +5 -8
  65. package/dist/htest/leveneTest.d.ts +30 -0
  66. package/dist/htest/leveneTest.js +41 -0
  67. package/dist/htest/multipleComparisons.d.ts +147 -0
  68. package/dist/htest/multipleComparisons.js +369 -0
  69. package/dist/htest/normalityTests.d.ts +44 -0
  70. package/dist/htest/normalityTests.js +164 -0
  71. package/dist/htest/onewayTest.d.ts +14 -0
  72. package/dist/htest/onewayTest.js +5 -7
  73. package/dist/htest/polycor.d.ts +117 -0
  74. package/dist/htest/polycor.js +468 -0
  75. package/dist/htest/power.d.ts +132 -0
  76. package/dist/htest/power.js +227 -0
  77. package/dist/htest/pwr.d.ts +263 -0
  78. package/dist/htest/pwr.js +440 -0
  79. package/dist/htest/rankTests.d.ts +64 -0
  80. package/dist/htest/rankTests.js +27 -29
  81. package/dist/htest/tTest.d.ts +20 -0
  82. package/dist/htest/tTest.js +19 -24
  83. package/dist/htest/testFormula.d.ts +57 -0
  84. package/dist/htest/testFormula.js +171 -0
  85. package/dist/htest/trendTests.d.ts +57 -0
  86. package/dist/htest/trendTests.js +167 -0
  87. package/dist/htest/varTest.d.ts +23 -0
  88. package/dist/htest/varTest.js +11 -21
  89. package/dist/htest/wilcoxTest.d.ts +14 -0
  90. package/dist/htest/wilcoxTest.js +9 -24
  91. package/dist/linalg/eigen.d.ts +8 -0
  92. package/dist/linalg/eigen.js +5 -4
  93. package/dist/linalg/expm.d.ts +48 -0
  94. package/dist/linalg/expm.js +472 -0
  95. package/dist/linalg/ops.d.ts +13 -2
  96. package/dist/linalg/ops.js +20 -5
  97. package/dist/linalg/schur.d.ts +48 -0
  98. package/dist/linalg/schur.js +225 -0
  99. package/dist/linalg.js +2 -2
  100. package/dist/models/anovaSequential.d.ts +58 -0
  101. package/dist/models/anovaSequential.js +138 -0
  102. package/dist/models/aovEz.d.ts +64 -0
  103. package/dist/models/aovEz.js +332 -0
  104. package/dist/models/aovStrata.d.ts +32 -0
  105. package/dist/models/aovStrata.js +104 -0
  106. package/dist/models/carAnova.d.ts +30 -0
  107. package/dist/models/carAnova.js +121 -0
  108. package/dist/models/coxph.d.ts +101 -0
  109. package/dist/models/coxph.js +502 -0
  110. package/dist/models/ddLeastSquares.d.ts +30 -0
  111. package/dist/models/ddLeastSquares.js +90 -0
  112. package/dist/models/ddMatrix.d.ts +10 -0
  113. package/dist/models/ddMatrix.js +91 -0
  114. package/dist/models/formula.d.ts +83 -0
  115. package/dist/models/formula.js +333 -0
  116. package/dist/models/lm.d.ts +84 -3
  117. package/dist/models/lm.js +141 -44
  118. package/dist/models/mauchly.d.ts +48 -0
  119. package/dist/models/mauchly.js +123 -0
  120. package/dist/models/modelFrame.d.ts +40 -0
  121. package/dist/models/modelFrame.js +131 -0
  122. package/dist/models/modelMatrix.d.ts +88 -0
  123. package/dist/models/modelMatrix.js +170 -0
  124. package/dist/models/survival.d.ts +87 -0
  125. package/dist/models/survival.js +238 -0
  126. package/dist/numerics.js +2 -2
  127. package/dist/optim/lbfgsb.d.ts +20 -0
  128. package/dist/optim/lbfgsb.js +1240 -0
  129. package/dist/optim/optim.d.ts +85 -0
  130. package/dist/optim/optim.js +610 -0
  131. package/dist/rstats.js +19 -4
  132. package/dist/special.js +2 -2
  133. package/dist/tests.js +17 -4
  134. package/llms.txt +114 -0
  135. package/package.json +3 -2
  136. package/dist/chunks/chunk-24U4DKRY.js +0 -2
  137. package/dist/chunks/chunk-IVOBWYLV.js +0 -2
  138. package/dist/chunks/chunk-JLSTEMSN.js +0 -2
  139. package/dist/chunks/chunk-MF4VBCHQ.js +0 -2
  140. package/dist/chunks/chunk-MSAUU6PY.js +0 -2
  141. package/dist/chunks/chunk-YC4JWUKG.js +0 -2
  142. package/dist/chunks/chunk-ZRO446BP.js +0 -2
package/dist/tests.js CHANGED
@@ -1,4 +1,17 @@
1
- /*! rStats.js 0.9.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{C as pe,D as ce,H as Kt,I as Xt,J as st,N as ge,O as at,T as we,U as Qt,Y as Ne,Z as Yt,b as Gt,ba as Zt,c as G,ca as Jt,d as rt,da as en,fa as Pe,ga as ze,l as Ut,m as Ae,n as le,s as Q,t as qe,y as ot}from"./chunks/chunk-24U4DKRY.js";import{h as ve,j as sn,k as We,l as _e,t as an,y as je}from"./chunks/chunk-JLSTEMSN.js";import{a as $,b as De,c as J,f as Hn}from"./chunks/chunk-MSAUU6PY.js";import{k as B,l as ae}from"./chunks/chunk-IVOBWYLV.js";import{c as on}from"./chunks/chunk-ZRO446BP.js";import"./chunks/chunk-LWUSYIQ4.js";import{a as Te,b as te,c as Y,e as tn,f as H,g as fe,h as ue,i as $e}from"./chunks/chunk-BGVMSDVJ.js";import{a as nn,d as rn}from"./chunks/chunk-5K4PS7IV.js";import{b as Re}from"./chunks/chunk-LE7TBHNI.js";import{f as Ht,g as z,h as P,s as Bt}from"./chunks/chunk-FC2TLHZC.js";import{b as nt,c as Ve,d as D,e as _t,f as ee,g as Ce,i as Oe,k as jt}from"./chunks/chunk-H2AP6TCR.js";function it(t,e,n={}){let r;e!==void 0&&!ArrayBuffer.isView(e)&&!Array.isArray(e)?n=e:r=e;let o=$(n.alternative),a=n.mu??0,l=n.paired??!1,c=n.varEqual??!1,s=n.confLevel??.95;if(Number.isNaN(a))throw new RangeError("mu must be a number");if(De(s),l&&r===void 0)throw new TypeError("y is required when paired is true");let f=Array.from(t),i=r===void 0?void 0:Array.from(r);if(l){let[M,x]=$e(f,i);f=M.map((A,E)=>A-x[E]),i=void 0}else f=ue(f),i!==void 0&&(i=ue(i));let u,m,h,d,g,p,b,v=f.length;if(v<2)throw new Error("not enough 'x' observations");let w=te(f),N=Y(f);if(i===void 0){if(m=v-1,h=Math.sqrt(N/v),h<10*Number.EPSILON*Math.abs(w))throw new Error("data are essentially constant");u=(w-a)/h,p=l?"Paired t-test":"One Sample t-test",d=w,g=l?"mean difference":"mean of x",b=l?"mean difference":"mean"}else{let M=i.length;if(M<1||!c&&M<2)throw new Error("not enough 'y' observations");if(c&&v+M<3)throw new Error("not enough observations");let x=te(i),A=Y(i);if(p=`${c?"":"Welch"} Two Sample t-test`,d=[w,x],g=["mean of x","mean of y"],b="difference in means",c){m=v+M-2;let E=0;v>1&&(E+=(v-1)*N),M>1&&(E+=(M-1)*A),E/=m,h=Math.sqrt(E*(1/v+1/M))}else{let E=N/v,I=A/M;h=Math.sqrt(E+I),m=(h*h)**2/(E*E/(v-1)+I*I/(M-1))}if(h<10*Number.EPSILON*Math.max(Math.abs(w),Math.abs(x)))throw new Error("data are essentially constant");u=(w-x-a)/h}let y,k;if(o==="less")y=B(u,m,!0,!1),k=[-1/0,u+ae(s,m,!0,!1)];else if(o==="greater")y=B(u,m,!1,!1),k=[u-ae(s,m,!0,!1),1/0];else{y=2*B(-Math.abs(u),m,!0,!1);let M=1-s,x=ae(1-M/2,m,!0,!1);k=[u-x,u+x]}return k=[a+k[0]*h,a+k[1]*h],{method:p,alternative:o,statistic:u,statisticName:"t",parameter:m,parameterName:"df",pValue:y,confInt:k,confLevel:s,estimate:d,estimateNames:g,nullValue:a,nullValueName:b,stdErr:h}}function Ie(t){return` with simulated p-value
3
- (based on ${Bn(t)} replicates)`}function Bn(t){if(!Number.isFinite(t)||t===0)return String(t);let e=t.toExponential(14).replace(/\.?0+e/,"e"),[n,r]=e.split("e"),o=Number(r),a=`${n}e${o<0?"-":"+"}${String(Math.abs(o)).padStart(2,"0")}`,l=String(Number(t.toPrecision(15)));return l.length<=a.length?l:a}function ne(t){let e=[0,0],n=0;for(let r of t)e=D(e,[r,0]),n+=r;return Number.isFinite(n)?e[0]:n}function Gn(t,e,n){return Array.isArray(e)?Un(t,e,n??{}):Kn(t,e??{})}var ln=(t,e)=>t<e?-1:t>e?1:0;function Un(t,e,n){if(t.length!==e.length)throw new RangeError("'x' and 'y' must have the same length");let r=m=>m==null;for(let m of[...t,...e])if(!r(m)&&typeof m!="string")throw new TypeError("factor values must be strings (or null for missing values)");let o=[],a=[];t.forEach((m,h)=>{let d=e[h];!r(m)&&!r(d)&&(o.push(m),a.push(d))});let l=[...new Set(o)].sort(ln),c=[...new Set(a)].sort(ln);if(l.length<2||c.length<2)throw new RangeError("'x' and 'y' must have at least 2 levels");let s=new Map(l.map((m,h)=>[m,h])),f=new Map(c.map((m,h)=>[m,h])),i=l.map(()=>c.map(()=>0));return o.forEach((m,h)=>i[s.get(m)][f.get(a[h])]++),{...hn(i,n),rowLevels:l,colLevels:c}}function Kn(t,e){if(t.length>0&&typeof t[0]!="number"){let r=t.map(a=>Array.from(a)),o=r[0].length;if(r.some(a=>a.length!==o))throw new TypeError("all rows of the matrix must have the same length");return r.length===1?lt(r[0],e):o===1?lt(r.map(a=>a[0]),e):hn(r,e)}return lt(Array.from(t),e)}function cn(t){let e=t.B??2e3;if(!(e>=0&&e<2**31))throw new RangeError("'B' must be a non-negative number");return e}function fn(t,e,n){let r=(1-64*Number.EPSILON)*e,o=0;for(let a=0;a<t.length;a++)t[a]>=r&&o++;return(1+o)/(n+1)}function un(t,e){return Number.isNaN(t)?NaN:G(t,e/2,2,!1,!1)}function mn(t){if(!t.some(e=>e<5)&&t.some(Number.isNaN))throw new RangeError("missing value where TRUE/FALSE needed (the expected counts contain NaN)")}function lt(t,e){let n=t.length;if(t.some(u=>Number.isNaN(u)||u<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=ne(t);if(!(r>0))throw new RangeError("at least one entry of 'x' must be positive");let o=e.p===void 0?t.map(()=>1/n):Array.from(e.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(u=>u<0))throw new RangeError("probabilities must be non-negative");if(e.rescaleP??!1){let u=ne(o);o=o.map(m=>m/u)}else if(Math.abs(ne(o)-1)>Math.sqrt(Number.EPSILON))throw new RangeError("probabilities must sum to 1");let a=o.map(u=>r*u);mn(a);let l=o.map((u,m)=>a[m]*(1-u)),c=ne(t.map((u,m)=>(u-a[m])**2/a[m])),s="Chi-squared test for given probabilities",f=n-1,i;if(e.simulatePValue??!1){let u=cn(e);s+=Ie(u),f=NaN,i=fn(Xn(r,o,a,u),c,u)}else i=un(c,f);return{method:s,statistic:c,statisticName:"X-squared",parameter:f,parameterName:"df",pValue:i,observed:t,expected:a,residuals:t.map((u,m)=>(u-a[m])/Math.sqrt(a[m])),stdres:t.map((u,m)=>(u-a[m])/Math.sqrt(l[m]))}}function Xn(t,e,n,r){let o=e.length,a=en(o,{size:Math.trunc(r*t),replace:!0,prob:e}),l=Math.trunc(t);if(a.length===0)return new Float64Array(0);if(l===0)throw new RangeError("invalid 'nrow' value (too large or NA)");let c=Math.ceil(a.length/l),s=new Float64Array(c),f=new Float64Array(o);for(let i=0;i<c;i++){f.fill(0);for(let m=0;m<l;m++)f[a[(i*l+m)%a.length]]++;let u=0;for(let m=0;m<o;m++)u+=(f[m]-n[m])**2/n[m];s[i]=u}return s}function Qn(t,e,n,r){let o=t.length,a=e.length,l=t.reduce((i,u)=>i+u,0),c=Pe(l),s=new Int32Array(o*a),f=new Float64Array(Math.trunc(r));for(let i=0;i<f.length;i++){ze(t,e,c,s);let u=0;for(let m=0;m<a;m++)for(let h=0;h<o;h++){let d=n[h][m];u+=(s[h+m*o]-d)**2/d}f[i]=u}return f}function hn(t,e){let n=e.correct??!0,r=t.length,o=t[0].length,a=t.flat();if(a.some(v=>!(v>=0&&v<1/0)))throw new RangeError("all entries of 'x' must be non-negative and finite");let l=ne(a);if(!(l>0))throw new RangeError("at least one entry of 'x' must be positive");let c=t.map(v=>ne(v)),s=t[0].map((v,w)=>ne(t.map(N=>N[w]))),f=c.map(v=>s.map(w=>v*w/l));mn(f.flat());let i=t.map((v,w)=>v.map((N,y)=>N-f[w][y])),u="Pearson's Chi-squared test",m,h=(r-1)*(o-1),d,g=(e.simulatePValue??!1)&&c.every(v=>v>0)&&s.every(v=>v>0),p=!g&&n&&r===2&&o===2?Math.min(.5,...i.flat().map(Math.abs)):0;if(g){let v=cn(e);u+=Ie(v),m=ne(i.flatMap((N,y)=>N.map((k,M)=>k*k/f[y][M]))),h=NaN;let w=Qn(c.map(N=>Math.trunc(N)),s.map(N=>Math.trunc(N)),f,v);d=fn(w,m,v)}else p>0?(u+=" with Yates' continuity correction",m=ne(i.flatMap((v,w)=>v.map((N,y)=>(Math.abs(N)-p)**2/f[w][y])))):m=ne(i.flatMap((v,w)=>v.map((N,y)=>N*N/f[w][y])));let b=f.map((v,w)=>v.map((N,y)=>N*(1-c[w]/l)*(1-s[y]/l)));return{method:u,statistic:m,statisticName:"X-squared",parameter:h,parameterName:"df",pValue:d??un(m,h),observed:t,expected:f,residuals:i.map((v,w)=>v.map((N,y)=>N/Math.sqrt(f[w][y]))),stdres:i.map((v,w)=>v.map((N,y)=>N/Math.sqrt(b[w][y])))}}var Yn=Math.pow(Number.EPSILON,.25);function U(t,e,n,r={}){let o={tol:r.tol??Yn,maxiter:r.maxIter??1e3};return r.fLower!==void 0&&(o.fLower=r.fLower),r.fUpper!==void 0&&(o.fUpper=r.fUpper),on(t,e,n,o).root}function Zn(t,e={}){let n=t.length,r=n===0?0:t[0].length;if(t.some(s=>s.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=t.map(s=>Array.from(s));if(o.flat().some(s=>Number.isNaN(s)||s<0))throw new RangeError("all entries of 'x' must be non-negative and finite");if(o.flat().some(s=>s===1/0))throw new RangeError("'x' has entries too large to be integer");let a=o.map(s=>s.map(Re)),l="Fisher's Exact Test for Count Data";if(n===2&&r===2)return nr(a,e,l);let c=e.alternative??"two.sided";if(e.simulatePValue??!1){let s=e.B??2e3;if(!(s>=0&&s<2**31))throw new RangeError("'B' must be a non-negative number");return{method:l+Ie(s),alternative:c,pValue:Jn(a,s)}}return{method:l,alternative:c,pValue:rr(a)}}function Jn(t,e){let n=t.filter(g=>g.some(p=>p>0)),r=t[0].map((g,p)=>p).filter(g=>t.some(p=>p[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(p=>g[p])),a=o.map(g=>g.reduce((p,b)=>p+b,0)),l=o[0].map((g,p)=>o.reduce((b,v)=>b+v[p],0)),c=a.length,s=l.length,f=[0,0];for(let g of o.flat())f=D(f,[-rn(g),0]);let i=f[0]/(1+64*Number.EPSILON),u=Pe(a.reduce((g,p)=>g+p,0)),m=new Int32Array(c*s),h=0,d=Math.trunc(e);for(let g=0;g<d;g++){ze(a,l,u,m);let p=0;for(let b=0;b<m.length;b++)p-=u[m[b]];p<=i&&h++}return Math.max(0,Math.min(1,(1+h)/(e+1)))}var er=1+1e-7,tr=16;function ct(t,e,n){let r=[0,0];for(let o=Math.max(0,e);o<=Math.min(n,t.length-1);o++)t[o]!==0&&(r=D(r,[t[o],0]));return r[0]}var ut=class{m;n;k;lo;hi;logCentral;constructor(e,n,r){this.m=e,this.n=n,this.k=r,this.lo=Math.max(0,r-n),this.hi=Math.min(r,e),this.logCentral=new Float64Array(this.hi-this.lo+1);let o=0,a=1;for(let l=this.lo;l<=this.hi;l++){let c=l-this.lo;c%tr===0||!(a>0&&a<1/0)?(o=ge(l,e,n,r,!0),a=1,this.logCentral[c]=o):this.logCentral[c]=o+Math.log(a),a*=(e-l)*(r-l)/((l+1)*(n-r+l+1))}}density(e){let n=this.logCentral.length,r=Math.log(e),o=new Float64Array(n),a=-1/0;for(let c=0;c<n;c++){let s=this.logCentral[c]+(this.lo+c)*r;o[c]=s,s>a&&(a=s)}for(let c=0;c<n;c++){let s=o[c]-a;o[c]=s<-746?0:Math.exp(s)}let l=ct(o,0,n-1);for(let c=0;c<n;c++)o[c]!==0&&(o[c]=o[c]/l);return o}mean(e){if(e===0)return this.lo;if(e===1/0)return this.hi;let n=this.density(e),r=[0,0];for(let o=0;o<n.length;o++)n[o]!==0&&(r=D(r,[(this.lo+o)*n[o],0]));return r[0]}tail(e,n,r){if(n===1)return r?at(e-1,this.m,this.n,this.k,!1,!1):at(e,this.m,this.n,this.k,!0,!1);if(n===0||n===1/0){let l=n===0?this.lo:this.hi;return(r?e<=l:e>=l)?1:0}let o=this.density(n),a=e-this.lo;return r?ct(o,a,o.length-1):ct(o,0,a)}};function ft(t,e){return e?U(t,0,1):1/U(n=>t(1/n),Number.EPSILON,1)}function nr(t,e,n){let r=$(e.alternative),o=e.or??1;if(!(o>=0))throw new RangeError("'or' must be a single number between 0 and Inf");let a=e.confLevel??.95;if(!(a>0&&a<1))throw new RangeError("confLevel must be a single number between 0 and 1");let[[l,c],[s,f]]=t,i=l,u=new ut(l+s,c+f,l+c),{lo:m,hi:h}=u,d;if(r==="less")d=u.tail(i,o,!1);else if(r==="greater")d=u.tail(i,o,!0);else if(o===0)d=i===m?1:0;else if(o===1/0)d=i===h?1:0;else{let N=u.density(o),y=N[i-m]*er,k=[0,0];for(let M of N)M<=y&&(k=D(k,[M,0]));d=k[0]}let g;if(i===m)g=0;else if(i===h)g=1/0;else{let N=u.mean(1);g=N===i?1:ft(y=>u.mean(y)-i,N>i)}let p={method:n,alternative:r,pValue:d,estimate:g,estimateNames:"odds ratio",nullValue:o,nullValueName:"odds ratio"};if(!(e.confInt??!0))return p;let b=N=>{if(i===h)return 1/0;let y=u.tail(i,1,!1);return y===N?1:ft(k=>u.tail(i,k,!1)-N,y<N)},v=N=>{if(i===m)return 0;let y=u.tail(i,1,!0);return y===N?1:ft(k=>u.tail(i,k,!0)-N,y>N)},w;if(r==="less")w=[0,b(1-a)];else if(r==="greater")w=[v(1-a),1/0];else{let N=(1-a)/2;w=[v(N),b(N)]}return{...p,confInt:w,confLevel:a}}var He=[[0,0]];function re(t){for(let e=He.length;e<=t;e++)He.push(D(He[e-1],jt([e,0])));return He[t]}function Be(t,e,n,r){let o=t+n,a=o-t,l=t-(o-a)+(n-a)+e+r,c=o+l;return[c,l-(c-o)]}var mt=class{scale=-1/0;hi=0;lo=0;add(e,n,r){if(e>this.scale){let s=Number.isFinite(this.scale)?Math.exp(this.scale-e):0;this.hi*=s,this.lo*=s,this.scale=e}let o=r*Math.exp(e-this.scale)*(1+n),a=this.hi+o,l=a-this.hi,c=this.hi-(a-l)+(o-l)+this.lo;this.hi=a+c,this.lo=c-(this.hi-a)}value(){return this.scale===-1/0?0:this.hi*Math.exp(this.scale)}};function rr(t){let e=t.filter(w=>w.some(N=>N>0));if(e.length===0)throw new RangeError("all entries of the table are zero");let n=e[0].map((w,N)=>e.some(y=>y[N]>0));if(e=e.map(w=>w.filter((N,y)=>n[y])),e.length<2||e[0].length<2)return 1;e.length>e[0].length&&(e=e[0].map((w,N)=>e.map(y=>y[N])));let r=e.length,o=e.map(w=>w.reduce((N,y)=>N+y,0)),a=e[0].map((w,N)=>e.reduce((y,k)=>y+k[N],0)).sort((w,N)=>N-w),l=a.length,c=o.reduce((w,N)=>w+N,0),s=_t(re(c));for(let w of o)s=D(s,re(w));for(let w of a)s=D(s,re(w));let f=[0,0];for(let w of e.flat())f=ee(f,re(w));let i=D(f,[Math.log1p(1e-7),0]),u=1e-9*(1+Math.abs(i[0])),[m,h]=i,d=Array.from({length:l},()=>new Map),g=(w,N)=>{let y=N.join(","),k=d[w].get(y);if(k!==void 0)return k;let M=D(s,re(N.reduce((R,F)=>R+F,0)));for(let R of N)M=ee(M,re(R));for(let R=w;R<l;R++)M=ee(M,re(a[R]));let x={R:N,KZ:M,max:0,min:0,edges:[],pastHi:[],pastLo:[],count:[],index:new Map};if(d[w].set(y,x),w===l-1){let R=[0,0];for(let F of N)R=ee(R,re(F));return x.max=x.min=R[0],x.edges.push({to:x,hi:R[0],lo:R[1],mult:1}),x}let A=new Map,E=new Array(r+1).fill(0);for(let R=r-1;R>=0;R--)E[R]=E[R+1]+N[R];let I=new Array(r).fill(0),q=(R,F,L)=>{if(R===r-1){I[R]=F;let C=ee(L,re(F)),W=N.map((se,Ee)=>se-I[Ee]).sort((se,Ee)=>se-Ee),_=g(w+1,W),X=`${W.join(",")}|${C[0]}`,oe=A.get(X);oe===void 0?A.set(X,{to:_,hi:C[0],lo:C[1],mult:1}):oe.mult++;return}let S=Math.max(0,F-E[R+1]),T=Math.min(N[R],F);for(let C=S;C<=T;C++)I[R]=C,q(R+1,F-C,ee(L,re(C)))};if(q(0,a[w],[0,0]),x.edges=[...A.values()],x.max=Math.max(...x.edges.map(R=>R.hi+R.to.max)),x.min=Math.min(...x.edges.map(R=>R.hi+R.to.min)),w===l-2){let R=x.edges.map(_=>{let X=_.to.edges[0];return{v:D([_.hi,_.lo],[X.hi,X.lo]),mult:_.mult}});R.sort((_,X)=>_.v[0]-X.v[0]||_.v[1]-X.v[1]);let F=R.length,L=R[F-1].v[0],S=new Float64Array(F),T=new Float64Array(F),C=new Float64Array(F),W=[0,0];R.forEach(({v:_,mult:X},oe)=>{S[oe]=_[0],T[oe]=_[1];let se=ee(_,[L,0]);W=D(W,[X*Math.exp(se[0])*(1+se[1]),0]),C[oe]=W[0]}),x.dist={hi:S,lo:T,cum:C,vmax:L,kv:D(s,[L,0])}}return x},p=g(0,[...o].sort((w,N)=>w-N)),b=new mt,v=(w,N,y,k)=>{if(N+w.max<=m-u){let[q,R]=Be(w.KZ[0],w.KZ[1],N,y);return b.add(q,R,k),!0}if(N+w.min>m+u)return!0;let M=w.dist;if(M===void 0)return!1;let[x,A]=Be(m,h,-N,-y),E=0,I=M.hi.length;for(;E<I;){let q=E+I>>>1,R=M.hi[q];R<x||R===x&&M.lo[q]<=A?E=q+1:I=q}if(E>0){let[q,R]=Be(M.kv[0],M.kv[1],N,y);b.add(q,R,k*M.cum[E-1])}return!0};v(p,0,0,1)||(p.pastHi.push(0),p.pastLo.push(0),p.count.push(1));for(let w=0;w<l-2;w++)for(let N of d[w].values()){let{pastHi:y,pastLo:k,count:M}=N;if(M.length!==0){for(let{to:x,hi:A,lo:E,mult:I}of N.edges)for(let q=0;q<M.length;q++){let[R,F]=Be(y[q],k[q],A,E),L=M[q]*I;if(v(x,R,F,L))continue;let S=x.index.get(R);S===void 0?(x.index.set(R,x.count.length),x.pastHi.push(R),x.pastLo.push(F),x.count.push(L)):x.count[S]+=L}N.pastHi=N.pastLo=N.count=[],N.index.clear()}}return Math.min(1,b.value())}function ht(t,e,n={}){let r;e!==void 0&&!ArrayBuffer.isView(e)&&!Array.isArray(e)?n=e:r=e;let o=$(n.alternative),a=n.mu??0,l=n.paired??!1,c=n.exact??null,s=n.correct??!0,f=n.confInt??!1,i=n.confLevel??.95,u=n.tolRoot??1e-4;if(!Number.isFinite(a))throw new RangeError("mu must be a single finite number");if(f&&!(i>0&&i<1))throw new RangeError("confLevel must be a number strictly between 0 and 1");if(l&&r===void 0)throw new TypeError("y is required when paired is true");let m,h;if(l){let[g,p]=$e(Array.from(t),Array.from(r));m=ue(g.map((b,v)=>b-p[v]))}else m=ue(Array.from(t)),r!==void 0&&(h=ue(Array.from(r)));if(m.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 d={alternative:o,mu:a,correct:s,confInt:f,confLevel:i,tolRoot:u};return h===void 0?sr(m,c,l,d):lr(m,h,c,d)}function Ge(t,e,n){return n?e==="two.sided"?Math.sign(t)*.5:e==="greater"?.5:-.5:0}function pn(t,e){return e==="less"?z(t):e==="greater"?z(t,{lowerTail:!1}):2*Math.min(z(t),z(t,{lowerTail:!1}))}function or(t){if(!(t>0)||!Number.isFinite(t))return t;let n=1-Math.floor(Math.log10(t)),r=n>=0?t*10**n:t/10**-n,o=Math.round(r);return o-r===.5&&o%2!==0&&(o-=1),n>=0?o/10**n:o*10**-n}function dt(t){let e=t.length;if(e===0)return NaN;let n=Math.floor(e/2);return e%2===1?t[n]:te([t[n-1],t[n]])}var pt=(t,e)=>t-e;function gn(t){let e=t.length,n=H(t.map(Math.abs)),r=0;for(let l=0;l<e;l++)t[l]>0&&(r+=n[l]);let o=0;for(let l of fe(n))o+=l*l*l-l;let a=Math.sqrt(e*(e+1)*(2*e+1)/24-o/48);return[r,r-e*(e+1)/4,a]}function sr(t,e,n,r){let{alternative:o,mu:a,correct:l}=r,c=t.map(k=>k-a),s=c.some(k=>k===0);s&&(c=c.filter(k=>k!==0));let f=c.length,i=e??f<50,u=H(c.map(Math.abs)),m=fe(u).length>0,[h,d,g]=gn(c),p,b,v=i&&!m&&!s;if(v){b="Wilcoxon signed rank exact test";let k=f*(f+1)/2;if(o==="less")p=we(h,f,!0,!1);else if(o==="greater")p=we(h-1,f,!1,!1);else{let M=h>k/2?we(h-1,f,!1,!1):we(h,f,!0,!1);p=Math.min(1,2*M)}}else b=`Wilcoxon signed rank test${l?" with continuity correction":""}`,p=pn((d-Ge(d,o,l))/g,o);let w={method:b,alternative:o,statistic:h,statisticName:"V",pValue:p,nullValue:a,nullValueName:n?"location shift":"location"};if(!r.confInt)return w;let N=c.map(k=>k+a),y=v?ar(N,r):ir(N,r);return{...w,...y}}function Ue(t,e,n){return t==="less"?[-1/0,n]:t==="greater"?[e,1/0]:[e,n]}function dn(t,e,n){let r={confInt:Ue(t,NaN,NaN),confLevel:0,estimate:e};return n!==void 0&&(r.estimateNames=n),r}function wn(t,e,n){let r=t(e==="two.sided"?n/2:n);return r===0?1:r}function Nn(t){if(!(t>0))throw new RangeError("tolRoot must be positive")}function ar(t,e){let{alternative:n,confLevel:r}=e,o=t.length,a=[];for(let u=0;u<o;u++)for(let m=u;m<o;m++)a.push((t[u]+t[m])/2);a.sort(pt);let l=a.length,c=1-r,s=wn(u=>Qt(u,o,!0,!1),n,c),f=we(s-1,o,!0,!1),i=n==="two.sided"?2*f:f;return{confInt:Ue(n,a[s-1],a[l-s]),confLevel:i-c>c/2?1-or(i):r,estimate:dt(a),estimateNames:"(pseudo)median"}}function ir(t,e){let{alternative:n,correct:r,confLevel:o,tolRoot:a}=e,l=t.length;if(l===0)return dn(n,NaN);let c=Math.min(...t),s=Math.max(...t);if(!Number.isFinite(c)||!Number.isFinite(s)||l===1||c===s)return dn(n,(c+s)/2,"midrange");let f=(w,N)=>{let[,y,k]=gn(t.map(M=>M-w).filter(M=>M!==0));return(y-Ge(y,n,N))/k},i=f(c,r),u=f(s,r),m=w=>-P(w),h=w=>n==="greater"?!(i<m(w)):n==="less"?!(u>P(w/2)):!(i<m(w/2))&&!(u>P(w/2)),d=1-o,g=!1;for(;!h(d)&&d<1;)d*=2,g=!0;Nn(a);let p=w=>U(N=>f(N,r)-w,c,s,{tol:a,fLower:i-w,fUpper:u-w}),b;if(d>=1){let w=dt([...t].sort(pt));b=Ue(n,w,w)}else n==="greater"?b=[p(m(d)),1/0]:n==="less"?b=[-1/0,p(P(d))]:b=[p(m(d/2)),p(P(d/2))];let v=U(w=>f(w,!1),c,s,{tol:a,fLower:f(c,!1),fUpper:f(s,!1)});return{confInt:b,confLevel:g?1-Math.min(1,d):o,estimate:v,estimateNames:"(pseudo)median"}}function vn(t,e){let n=t.length,r=e.length,o=H([...t,...e]),a=0;for(let i=0;i<n;i++)a+=o[i];let l=a-n*(n+1)/2,c=0;for(let i of fe(o))c+=i*i*i-i;let s=n+r,f=Math.sqrt(n*r/12*(s+1-c/(s*(s-1))));return[l,l-n*r/2,f]}function lr(t,e,n,r){let{alternative:o,mu:a,correct:l}=r,c=t.length,s=e.length,f=t.map(N=>N-a),i=n??(c<50&&s<50),u=fe([...f,...e]).length>0,[m,h,d]=vn(f,e),g,p,b=i&&!u;if(b)if(p="Wilcoxon rank sum exact test",o==="less")g=Ne(m,c,s,!0,!1);else if(o==="greater")g=Ne(m-1,c,s,!1,!1);else{let N=m>c*s/2?Ne(m-1,c,s,!1,!1):Ne(m,c,s,!0,!1);g=Math.min(1,2*N)}else p=`Wilcoxon rank sum test${l?" with continuity correction":""}`,g=pn((h-Ge(h,o,l))/d,o);let v={method:p,alternative:o,statistic:m,statisticName:"W",pValue:g,nullValue:a,nullValueName:"location shift"};if(!r.confInt)return v;let w=b?cr(t,e,r):fr(t,e,r);return{...v,...w}}function cr(t,e,n){let{alternative:r,confLevel:o}=n,a=t.length,l=e.length,c=[];for(let h of t)for(let d of e)c.push(h-d);c.sort(pt);let s=c.length,f=1-o,i=wn(h=>Yt(h,a,l,!0,!1),r,f),u=Ne(i-1,a,l,!0,!1),m=r==="two.sided"?2*u:u;return{confInt:Ue(r,c[i-1],c[s-i]),confLevel:m-f>f/2?1-m:o,estimate:dt(c),estimateNames:"difference in location"}}function fr(t,e,n){let{alternative:r,correct:o,confLevel:a,tolRoot:l}=n,c=Math.min(...t)-Math.max(...e),s=Math.max(...t)-Math.min(...e);if(!Number.isFinite(c)||!Number.isFinite(s))throw new Error("cannot compute the confidence interval because the differences are not finite");let f=(h,d)=>{let[,g,p]=vn(t.map(b=>b-h),e);return(g-Ge(g,r,d))/p},i=(h,d)=>{let g=f(c,d)-h,p=f(s,d)-h;if(Number.isNaN(g)||Number.isNaN(p))throw new Error("cannot compute the confidence interval because all observations are tied");return g<=0?c:p>=0?s:(Nn(l),U(b=>f(b,d)-h,c,s,{tol:l,fLower:g,fUpper:p}))},u=1-a,m;return r==="greater"?m=[i(-P(u),o),1/0]:r==="less"?m=[-1/0,i(P(u),o)]:m=[i(-P(u/2),o),i(P(u/2),o)],{confInt:m,confLevel:a,estimate:i(0,!1),estimateNames:"difference in location"}}var bn=["holm","hochberg","hommel","bonferroni","BH","BY","fdr","none"];function ur(t){if(bn.includes(t))return t;let e=bn.filter(n=>n.startsWith(t));if(t.length===0||e.length!==1)throw new RangeError(`invalid method "${t}"`);return e[0]}function gt(t,e={}){let n=typeof e=="string"?{method:e}:e,r=ur(n.method??"holm"),o=Array.from(t),a=[];for(let u=0;u<o.length;u++)Number.isNaN(o[u])||a.push(u);let l=a.length,c=n.n??l;if(!(c>=l))throw new RangeError("n must be at least the number of non-NaN p-values");if(c<=1)return o;let s=a.map(u=>o[u]),f=mr(s,r,c),i=o.slice();return a.forEach((u,m)=>i[u]=f[m]),i}function Ke(t,e){let n=t.map((r,o)=>o);return n.sort((r,o)=>(e?t[o]-t[r]:t[r]-t[o])||r-o),n}function mr(t,e,n){let r=t.length,o=new Array(r);switch(e){case"none":return t.slice();case"bonferroni":return t.map(a=>Math.min(1,n*a));case"holm":{let a=Ke(t,!1),l=-1/0;return a.forEach((c,s)=>{l=Math.max(l,(n-s)*t[c]),o[c]=Math.min(1,l)}),o}case"hochberg":{let a=Ke(t,!0),l=1/0;return a.forEach((c,s)=>{let f=r-s;l=Math.min(l,(n-f+1)*t[c]),o[c]=Math.min(1,l)}),o}case"BH":case"fdr":case"BY":{let a=e==="BY"?tn(Array.from({length:Math.floor(n)},(s,f)=>1/(f+1))):1,l=Ke(t,!0),c=1/0;return l.forEach((s,f)=>{let i=r-f;c=Math.min(c,a*n/i*t[s]),o[s]=Math.min(1,c)}),o}case"hommel":return hr(t,n)}}function hr(t,e){if(!Number.isInteger(e))throw new RangeError("n must be an integer for the hommel method");let n=t.length,r=[...t,...new Array(e-n).fill(1)],o=Ke(r,!1),a=o.map(s=>r[s]),l=a.slice();for(let s=e;s>=2;s--){let f=1/0;for(let i=1;i<=s;i++)f=Math.min(f,s*a[e-s+i-1]/i);for(let i=e-s;i<e;i++)l[i]=Math.max(l[i],f);for(let i=0;i<e-s;i++)l[i]=Math.max(l[i],Math.min(s*a[i],f))}let c=new Array(n);return o.forEach((s,f)=>{s<n&&(c[s]=l[f])}),c}var dr=1+1e-7;function Xe(t){return Number.isFinite(t)&&t>=0&&Math.abs(t-Math.round(t))<=1e-7*Math.max(1,Math.abs(t))}function pr(t,e,n){if(n===0)return t===0?1:0;if(n===1)return t===e?1:0;let r=e*n;if(t===r)return 1;let o=pe(t,e,n,!1)*dr,a=0;if(t<r){for(let s=Math.ceil(r);s<=e;s++)pe(s,e,n,!1)<=o&&a++;let c=a===0?0:ce(e-a,e,n,!1,!1);return Math.min(1,ce(t,e,n,!0,!1)+c)}for(let c=0;c<=Math.floor(r);c++)pe(c,e,n,!1)<=o&&a++;let l=a===0?0:ce(a-1,e,n,!0,!1);return Math.min(1,ce(t-1,e,n,!1,!1)+l)}function wt(t,e,n={}){let r,o;if(typeof t!="number"){if(t.length!==2)throw new RangeError("incorrect length of 'x'");if(e!==void 0&&typeof e!="number"&&(n=e),!Xe(t[0])||!Xe(t[1]))throw new RangeError("'x' must be nonnegative and integer");r=Math.round(t[0]),o=Math.round(t[0])+Math.round(t[1])}else{if(typeof e!="number")throw new TypeError("'n' is required when 'x' is a single number");if(!Xe(t))throw new RangeError("'x' must be nonnegative and integer");if(r=Math.round(t),!(Xe(e)&&Math.round(e)>=r&&e>0))throw new RangeError("'n' must be a positive integer >= 'x'");o=Math.round(e)}let a=n.p??.5;if(!(a>=0&&a<=1))throw new RangeError("'p' must be a single number between 0 and 1");let l=$(n.alternative),c=n.confLevel??.95;J(c);let s;l==="less"?s=ce(r,o,a,!0,!1):l==="greater"?s=ce(r-1,o,a,!1,!1):s=pr(r,o,a);let f=m=>r===0?0:ot(m,r,o-r+1,!0,!1),i=m=>r===o?1:ot(1-m,r+1,o-r,!0,!1),u;if(l==="less")u=[0,i(1-c)];else if(l==="greater")u=[f(1-c),1];else{let m=(1-c)/2;u=[f(m),i(m)]}return{method:"Exact binomial test",alternative:l,statistic:r,statisticName:"number of successes",parameter:o,parameterName:"number of trials",pValue:s,confInt:u,confLevel:c,estimate:r/o,estimateNames:"probability of success",nullValue:a,nullValueName:"probability of success"}}var gr=1+1e-7;function wr(t){return Number.isFinite(t)&&t>=0&&Math.abs(t-Math.round(t))<=1e-7*Math.max(1,Math.abs(t))}function Nr(t,e){if(e===0)return t===0?1:0;if(t===e)return 1;let n=Ae(t,e,!1)*gr,r=c=>Ae(c,e,!1)<=n,o=Math.floor(e);if(t<e){let c=Math.max(t+1,o),s=c;if(!r(c)){let f=c,i=1;for(s=c+1;!r(s);)f=s,i*=2,s=c+i;for(;s-f>1;){let u=f+Math.floor((s-f)/2);r(u)?s=u:f=u}}return Math.min(1,le(t,e,!0,!1)+le(s-1,e,!1,!1))}let a=Math.min(t-1,o),l;if(!r(0))l=-1;else if(r(a))l=a;else{l=0;let c=a;for(;c-l>1;){let s=l+Math.floor((c-l)/2);r(s)?l=s:c=s}}return Math.min(1,le(l,e,!0,!1)+le(t-1,e,!1,!1))}function vr(t,e={}){let n=typeof t=="number"?[t]:Array.from(t);if(n.length<1||n.length>2)throw new RangeError("incorrect length of 'x'");if(!n.every(wr))throw new RangeError("'x' must be finite, nonnegative, and integer");let r=n.map(Math.round),o=r.length,a=e.T??1,l=typeof a=="number"?new Array(o).fill(a):Array.from(a);if(l.length!==o)throw new RangeError("'x' and 'T' have incompatible length");if(!l.every(b=>Number.isFinite(b)&&b>=0))throw new RangeError("'T' must be nonnegative");let c=e.r??1;if(!(Number.isFinite(c)&&c>=0))throw new RangeError("'r' must be a single positive number");let s=$(e.alternative),f=e.confLevel??.95;if(J(f),o===2){let[b,v]=r,[w,N]=l,y=b+v,k=c*w/(c*w+N),M=wt([b,v],{p:k,alternative:s,confLevel:f}),x=A=>A/(1-A)*(N/w);return{method:"Comparison of Poisson rates",alternative:s,statistic:b,statisticName:"count1",parameter:y*c*w/(w+c*N),parameterName:"expected count1",pValue:M.pValue,confInt:[x(M.confInt[0]),x(M.confInt[1])],confLevel:f,estimate:b/w/(v/N),estimateNames:"rate ratio",nullValue:c,nullValueName:"rate ratio"}}let i=r[0],u=l[0],m=c*u,h;s==="less"?h=le(i,m,!0,!1):s==="greater"?h=le(i-1,m,!1,!1):h=Nr(i,m);let d=b=>i===0?0:rt(b,i,1,!0,!1),g=b=>rt(1-b,i+1,1,!0,!1),p;if(s==="less")p=[0,g(1-f)];else if(s==="greater")p=[d(1-f),1/0];else{let b=(1-f)/2;p=[d(b),g(b)]}return{method:"Exact Poisson test",alternative:s,statistic:i,statisticName:"number of events",parameter:u,parameterName:"time base",pValue:h,confInt:[p[0]/u,p[1]/u],confLevel:f,estimate:i/u,estimateNames:"event rate",nullValue:c,nullValueName:"event rate"}}function Nt(t,e,n={}){let r,o,a=n;if(typeof t!="number"&&t.length>0&&Array.isArray(t[0])){let E=t;if(E.some(I=>I.length!==2))throw new RangeError("'x' must have 2 columns");r=E.map(I=>I[0]),o=E.map(I=>I[0]+I[1]),e!==void 0&&typeof e=="object"&&!Array.isArray(e)&&(a=e)}else{if(e===void 0||typeof e=="object"&&!Array.isArray(e))throw new RangeError("'n' must be given");r=typeof t=="number"?[t]:Array.from(t),o=typeof e=="number"?[e]:Array.from(e)}if(r.length!==o.length)throw new RangeError("'x' and 'n' must have the same length");let l=r.length,c=[];for(let E=0;E<l;E++)!Number.isNaN(r[E])&&!Number.isNaN(o[E])&&c.push(E);let s=c.length;if(s<1)throw new RangeError("not enough data");if(r=c.map(E=>r[E]),o=c.map(E=>o[E]),o.some(E=>!(E>0)))throw new RangeError("elements of 'n' must be positive");if(r.some(E=>!(E>=0)))throw new RangeError("elements of 'x' must be nonnegative");if(r.some((E,I)=>E>o[I]))throw new RangeError("elements of 'x' must not be greater than those of 'n'");let f;if(a.p!==void 0){let E=typeof a.p=="number"?[a.p]:Array.from(a.p);if(E.length!==l)throw new RangeError("'p' must have the same length as 'x' and 'n'");if(f=c.map(I=>E[I]),f.some(I=>!(I>0&&I<1)))throw new RangeError("elements of 'p' must be in (0,1)")}let i=f!==void 0;s===1&&f===void 0&&(f=[.5]);let u=a.confLevel??.95;J(u);let m=$(a.alternative);(s>2||s===2&&i)&&(m="two.sided");let h=r.map((E,I)=>E/o[I]),d=s===2?h[0]-h[1]:0,g=s===2?1/o[0]+1/o[1]:0,p=(a.correct??!0)&&s<=2?.5:0,b=!1;if(p>0&&s===1){let E=Math.abs(r[0]-o[0]*f[0]);E<=p&&([p,b]=[E,!0])}else if(p>0&&s===2&&!i){let E=Math.abs(d)/g;E<=p&&([p,b]=[E,!0])}let v=r.reduce((E,I)=>E+I,0)/o.reduce((E,I)=>E+I,0),w=0;if(!b)for(let E=0;E<s;E++){let I=f?.[E]??v,q=o[E]*I,R=o[E]*(1-I);w+=(Math.abs(r[E]-q)-p)**2/q,w+=(Math.abs(o[E]-r[E]-R)-p)**2/R}let N=f!==void 0?s:s-1,y;if(m==="two.sided")y=G(w,N/2,2,!1,!1);else{let E=Math.sign(s===1?h[0]-f[0]:d);y=z(E*Math.sqrt(w),{lowerTail:m==="less"})}let k;if(s===1||s===2&&!i){let E=P(m==="two.sided"?(1+u)/2:u);if(s===1){let I=o[0],q=E*E,R=(C,W)=>(C+q/(2*I)+W*E*Math.sqrt(C*(1-C)/I+q/(4*I*I)))/(1+q/I),F=h[0]+p/I,L=h[0]-p/I,S=Math.min(1,Math.max(0,F>=1?1:R(F,1))),T=Math.min(1,Math.max(0,L<=0?0:R(L,-1)));k=m==="two.sided"?[T,S]:m==="greater"?[T,1]:[0,S]}else{let I=Math.sqrt(h.reduce((L,S,T)=>L+S*(1-S)/o[T],0)),q=E*I+p*g,R=Math.max(-1,d-q),F=Math.min(1,d+q);k=m==="two.sided"?[R,F]:m==="greater"?[R,1]:[-1,F]}}let M=s===1?"p":c.map(E=>`prop ${E+1}`),A={method:`${s===1?"1-sample proportions test":`${s}-sample test for ${i?"given proportions":"equality of proportions"}`} ${p!==0?"with":"without"} continuity correction`,alternative:m,statistic:w,statisticName:"X-squared",parameter:N,parameterName:"df",pValue:y,estimate:s===1?h[0]:h,estimateNames:M};return f!==void 0&&(A.nullValue=s===1?f[0]:f,A.nullValueName=M),k!==void 0&&(A.confInt=k,A.confLevel=u),A}function br(t,e,n={}){let r;if(t.length>0&&Array.isArray(t[0])){e!==void 0&&!Array.isArray(e)&&(n=e),r=t.map(i=>Array.from(i));let f=r.length;if(f<2||r.some(i=>i.length!==f))throw new RangeError("'x' must be square with at least two rows and columns");if(r.some(i=>i.some(u=>!Number.isFinite(u)||u<0)))throw new RangeError("all entries of 'x' must be nonnegative and finite")}else{if(!Array.isArray(e))throw new TypeError("'y' is required when 'x' is not a table");r=yr(t,e)}let o=n.correct??!0,a=r.length,l,c=o&&a===2&&r[0][1]!==r[1][0];if(c){let f=r[0][1],i=r[1][0];l=(Math.abs(f-i)-1)**2/(f+i)}else{l=0;for(let f=0;f<a;f++)for(let i=f+1;i<a;i++){let u=r[f][i],m=r[i][f];l+=(u-m)**2/(u+m)}}let s=a*(a-1)/2;return{method:c?"McNemar's Chi-squared test with continuity correction":"McNemar's Chi-squared test",statistic:l,statisticName:"McNemar's chi-squared",parameter:s,parameterName:"df",pValue:G(l,s/2,2,!1,!1)}}function yr(t,e){if(t.length!==e.length)throw new RangeError("'x' and 'y' must have the same length");let n=[];for(let f=0;f<t.length;f++){let i=t[f],u=e[f];typeof i=="number"&&Number.isNaN(i)||typeof u=="number"&&Number.isNaN(u)||n.push([i,u])}let r=f=>Array.from(new Set(f)).sort((i,u)=>typeof i=="number"&&typeof u=="number"?i-u:String(i)<String(u)?-1:String(i)>String(u)?1:0),o=r(n.map(f=>f[0])),a=r(n.map(f=>f[1]));if(o.length<2||o.length!==a.length)throw new RangeError("'x' and 'y' must have the same number of levels (minimum 2)");let l=new Map(o.map((f,i)=>[f,i])),c=new Map(a.map((f,i)=>[f,i])),s=o.map(()=>new Array(o.length).fill(0));for(let[f,i]of n)s[l.get(f)][c.get(i)]++;return s}function Mr(t,e,n={}){let r=Array.from(t).filter(Number.isFinite),o=Array.from(e).filter(Number.isFinite),a=n.ratio??1;if(!(Number.isFinite(a)&&a>0))throw new RangeError("'ratio' must be a single positive number");let l=$(n.alternative),c=n.confLevel??.95;J(c);let s=r.length-1,f=o.length-1;if(s<1)throw new Error("not enough 'x' observations");if(f<1)throw new Error("not enough 'y' observations");let i=Y(r)/Y(o),u=i/a,m=Q(u,s,f,!0,!1),h=Q(u,s,f,!1,!1),d,g;if(l==="less")d=m,g=[0,i/qe(1-c,s,f,!0,!1)];else if(l==="greater")d=h,g=[i/qe(c,s,f,!0,!1),1/0];else{d=Math.min(1,2*Math.min(m,h));let p=(1-c)/2;g=[i/qe(1-p,s,f,!0,!1),i/qe(p,s,f,!0,!1)]}return{method:"F test to compare two variances",alternative:l,statistic:u,statisticName:"F",parameter:[s,f],parameterName:["num df","denom df"],pValue:d,confInt:g,confLevel:c,estimate:i,estimateNames:"ratio of variances",nullValue:a,nullValueName:"ratio of variances"}}function xr(t){let e=t.length;if(e<2)throw new RangeError("'x' must be a list with at least 2 elements");let n=[],r=[];for(let i of t){let u=Array.from(i).filter(Number.isFinite);if(u.length<2)throw new RangeError("there must be at least 2 observations in each group");n.push(u.length-1),r.push(Y(u))}let o=0,a=0,l=0,c=0;for(let i=0;i<e;i++)o+=n[i],a+=n[i]*r[i],l+=n[i]*Math.log(r[i]),c+=1/n[i];a/=o;let s=(o*Math.log(a)-l)/(1+(c-1/o)/(3*(e-1))),f=e-1;return{method:"Bartlett test of homogeneity of variances",statistic:s,statisticName:"Bartlett's K-squared",parameter:f,parameterName:"df",pValue:G(s,f/2,2,!1,!1)}}function vt(t,e,n="drop"){if(e===void 0){let l=t;if(l.length>0&&typeof l[0]=="number")throw new TypeError("'g' is required when 'x' is a single sample");let c=l.map(i=>Array.from(i).filter(u=>!Number.isNaN(u)));if(n==="error"&&c.some(i=>i.length===0))throw new RangeError("all groups must contain data");let s=[],f=[];return c.forEach((i,u)=>{i.length!==0&&(s.push(String(u+1)),f.push(i))}),{labels:s,groups:f}}let r=Array.from(t);if(r.length!==e.length)throw new RangeError("'x' and 'g' must have the same length");let o=new Map;for(let l=0;l<r.length;l++){let c=r[l],s=e[l];if(Number.isNaN(c)||typeof s=="number"&&Number.isNaN(s))continue;let f=o.get(s);f===void 0&&o.set(s,f=[]),f.push(c)}let a=Array.from(o.keys()).sort((l,c)=>typeof l=="number"&&typeof c=="number"?l-c:String(l)<String(c)?-1:String(l)>String(c)?1:0);return{labels:a.map(String),groups:a.map(l=>o.get(l))}}function me(t,e,n="drop"){return vt(t,e,n).groups}function yn(t){let e=[...t].sort((r,o)=>r-o),n=e.length;return n%2===1?e[(n-1)/2]:(e[n/2-1]+e[n/2])/2}function bt(t){if(t.length===0)throw new RangeError("'y' must have at least one row");let e=t[0].length;if(t.some(n=>n.length!==e))throw new RangeError("all rows of 'y' must have the same length");return t.filter(n=>n.every(r=>!Number.isNaN(r))).map(n=>Array.from(n))}function yt(t,e,n,r){return{method:t,statistic:n,statisticName:e,parameter:r,parameterName:"df",pValue:G(n,r/2,2,!1,!1)}}function Mn(t){let e=0;for(let n of fe(t))e+=n*n*n-n;return e}function Er(t,e){let n=me(t,e,"error"),r=n.length;if(r<2)throw new RangeError("all observations are in the same group");let o=n.flat(),a=o.length,l=H(o),c=0,s=0;for(let f of n){let i=0;for(let u=0;u<f.length;u++)i+=l[s+u];s+=f.length,c+=i*i/f.length}return c=(12*c/(a*(a+1))-3*(a+1))/(1-Mn(o)/(a*a*a-a)),yt("Kruskal-Wallis rank sum test","Kruskal-Wallis chi-squared",c,r-1)}function kr(t){let e=bt(t),n=e.length,r=t[0]?.length??0;if(n<2||r<2)throw new RangeError("'y' must have at least two complete rows and two columns");let o=new Array(r).fill(0),a=0;for(let s of e){let f=H(s);for(let i=0;i<r;i++)o[i]+=f[i];a+=Mn(s)}let l=0;for(let s of o)l+=(s-n*(r+1)/2)**2;let c=12*l/(n*r*(r+1)-a/(r-1));return yt("Friedman rank sum test","Friedman chi-squared",c,r-1)}function Rr(t){let e=bt(t),n=t.length,r=t[0]?.length??0;if(e.length<2||r<2)throw new RangeError("'y' must have at least two complete rows and two columns");let o=H(e.map(h=>Math.max(...h)-Math.min(...h))),a=new Array(r).fill(0),l=0;e.forEach((h,d)=>{let g=H(h);for(let p=0;p<r;p++){let b=o[d]*(g[p]-(r+1)/2);l+=b*b,a[p]+=b}});let c=0;for(let h of a)c+=h*h;c/=n;let s=r-1,f=(n-1)*(r-1),i,u,m=[s,f];if(l===c){i=NaN,m=[NaN,NaN];let h=0;for(let d=2;d<=r;d++)h+=Math.log(d);u=Math.exp((1-n)*h)}else i=(n-1)*c/(l-c),u=Q(i,s,f,!1,!1);return{method:"Quade test",statistic:i,statisticName:"Quade F",parameter:m,parameterName:["num df","denom df"],pValue:u}}function Ar(t,e){let n=me(t,e,"error"),r=n.length;if(r<2)throw new RangeError("all observations are in the same group");let o=n.flatMap(m=>{let h=yn(m);return m.map(d=>Math.abs(d-h))}),a=o.length,c=H(o).map(m=>P((1+m/(a+1))/2)),s=0;for(let m of c)s+=m;let f=s/a,i=0,u=0;for(let m of n){let h=0;for(let d=0;d<m.length;d++)h+=c[u+d];u+=m.length,i+=m.length*(h/m.length-f)**2}return i/=Y(c),yt("Fligner-Killeen test of homogeneity of variances","Fligner-Killeen:med chi-squared",i,r-1)}function qr(t,e,n={}){let r=Array.from(t).filter(Number.isFinite),o=Array.from(e).filter(Number.isFinite),a=$(n.alternative),l=r.length,c=o.length,s=l+c;if(s<3)throw new RangeError("not enough observations");let f=r.concat(o),i=Array.from({length:s},(N,y)=>y).sort((N,y)=>f[N]-f[y]),u=new Array(s),m=0;for(;m<s;){let N=m;for(;N+1<s&&f[i[N+1]]===f[i[m]];)N++;let y=0;for(let k=m;k<=N;k++)y+=(k+1-(s+1)/2)**2;y/=N-m+1;for(let k=m;k<=N;k++)u[i[k]]=y;m=N+1}let h=0;for(let N=0;N<l;N++)h+=u[N];let d=0;for(let N of u)d+=N;d/=s;let g=0;for(let N of u)g+=(N-d)**2;let p=l*d,b=l*c/(s*(s-1))*g,v=(h-p)/Math.sqrt(b),w=a==="less"?z(v):a==="greater"?z(v,{lowerTail:!1}):2*z(-Math.abs(v));return{method:"Mood two-sample test of scale",alternative:a,statistic:v,statisticName:"Z",pValue:w}}function he(t,e){let n=0;for(let r=t.length-1;r>=0;r--)n=n*e+t[r];return n}var Tr=[0,.221157,-.147981,-2.07119,4.434685,-2.706056],Ir=[0,.042981,-.293762,-1.752461,5.682633,-3.582633],Sr=[.544,-.39978,.025054,-6714e-7],Lr=[1.3822,-.77857,.062767,-.0020322],Fr=[-1.5861,-.31082,-.083751,.0038915],Vr=[-.4803,-.082676,.0030302],Cr=[-2.273,.459];function Or(t){if(!(t>=3)||t!==Math.floor(t))throw new RangeError("n must be an integer of at least 3");let e=new Float64Array(t);if(t===3)return e[0]=-Math.SQRT1_2,e[2]=Math.SQRT1_2,e;let n=Math.floor(t/2),r=new Float64Array(n),o=0;for(let u=0;u<n;u++)r[u]=-P((u+1-.375)/(t+.25)),o+=r[u]*r[u];o*=2;let a=Math.sqrt(o),l=1/Math.sqrt(t),c=r[0]/a+he(Tr,l),s=new Float64Array(n);s[0]=c;let f,i;if(t>5){let u=r[1]/a+he(Ir,l);s[1]=u,f=2,i=Math.sqrt((o-2*r[0]*r[0]-2*r[1]*r[1])/(1-2*c*c-2*u*u))}else f=1,i=Math.sqrt((o-2*r[0]*r[0])/(1-2*c*c));for(let u=f;u<n;u++)s[u]=r[u]/i;for(let u=0;u<n;u++)e[t-1-u]=s[u],e[u]=-s[u];return e}function xn(t){let e=t.length,n=Or(e),r=t[e-1]-t[0],o=0;for(let f of t)o+=f/r;o/=e;let a=0,l=0,c=0;for(let f=0;f<e;f++){let i=t[f]/r-o;a+=n[f]*n[f],l+=i*i,c+=n[f]*i}let s=Math.sqrt(a*l);return(s-c)*(s+c)/(a*l)}var Pr=1.90985931710274,zr=1.0471975511966;function En(t,e){if(Number.isNaN(t)||!(e>=3))return NaN;if(e===3){let l=Pr*(Math.asin(Math.sqrt(1-t))-zr);return Math.max(l,0)}let n=Math.log(t);if(e<=11){let l=he(Cr,e);if(n>=l)return 1e-99;let c=he(Sr,e),s=Math.exp(he(Lr,e));return z(-Math.log(l-n),{mean:c,sd:s,lowerTail:!1})}let r=Math.log(e),o=he(Fr,r),a=Math.exp(he(Vr,r));return z(n,{mean:o,sd:a,lowerTail:!1})}function $r(t,e,n={}){let r;e!==void 0&&!Array.isArray(e)?n=e:r=e;let o=me(t,r),a=n.varEqual??!1,l=o.length;if(l<2)throw new RangeError("not enough groups");let c=o.map(h=>h.length);if(c.some(h=>h<2))throw new RangeError("not enough observations");let s=o.map(te),f=o.map(Y),i=c.reduce((h,d)=>h+d,0),u,m;if(a){let h=te(o.flat()),d=0,g=0;for(let p=0;p<l;p++)d+=c[p]*(s[p]-h)**2,g+=(c[p]-1)*f[p];m=i-l,u=d/(l-1)/(g/m)}else{let h=c.map((v,w)=>v/f[w]),d=h.reduce((v,w)=>v+w,0),g=0,p=0;for(let v=0;v<l;v++)g+=(1-h[v]/d)**2/(c[v]-1),p+=h[v]*s[v];g/=l*l-1,p/=d;let b=0;for(let v=0;v<l;v++)b+=h[v]*(s[v]-p)**2;u=b/((l-1)*(1+2*(l-2)*g)),m=1/(3*g)}return{method:a?"One-way analysis of means":"One-way analysis of means (not assuming equal variances)",statistic:u,statisticName:"F",parameter:[l-1,m],parameterName:["num df","denom df"],pValue:Q(u,l-1,m,!1,!1)}}function Dr(t){let e=Array.from(t).filter(o=>!Number.isNaN(o)).sort((o,a)=>o-a),n=e.length;if(n<3||n>5e3)throw new RangeError("sample size must be between 3 and 5000");if(!Number.isFinite(e[0])||!Number.isFinite(e[n-1]))throw new RangeError("'x' must be finite");if(e[n-1]===e[0])throw new RangeError("all 'x' values are identical");let r=xn(e);return{method:"Shapiro-Wilk normality test",statistic:1-r,statisticName:"W",pValue:En(r,n)}}function Mt(t,e,n){let r=[0,0];for(let o=Math.max(0,e);o<Math.min(n,t.length);o++)r=D(r,[t[o],0]);return r[0]}function Wr([[t,e],[n,r]]){let o=t+n,a=e+r,l=t+e,c=Math.max(0,l-a),s=Math.min(l,o),f=new Float64Array(s-c+1);for(let i=c;i<=s;i++)f[i-c]=ge(i,o,a,l,!1);return{lo:c,d:f}}function _r(t,e){let n=new Float64Array(t.length+e.length-1);for(let r=0;r<t.length;r++){let o=t[r];if(o!==0)for(let a=0;a<e.length;a++)n[r+a]+=o*e[a]}return n}var Qe=class{lo;hi;logNull;constructor(e){let n=0,r=new Float64Array([1]);for(let o of e){let a=Wr(o);n+=a.lo,r=_r(r,a.d)}this.lo=n,this.hi=n+r.length-1,this.logNull=r.map(Math.log)}density(e){let n=this.logNull.length,r=new Float64Array(n);if(e===0||e===1/0)return r[e===0?0:n-1]=1,r;let o=Math.log(e),a=-1/0;for(let c=0;c<n;c++){let s=this.logNull[c]+(this.lo+c)*o;r[c]=s,s>a&&(a=s)}for(let c=0;c<n;c++)r[c]=Math.exp(r[c]-a);let l=Mt(r,0,n);for(let c=0;c<n;c++)r[c]=r[c]/l;return r}mean(e){if(e===0)return this.lo;if(e===1/0)return this.hi;let n=this.density(e),r=[0,0];for(let o=0;o<n.length;o++)r=D(r,[(this.lo+o)*n[o],0]);return r[0]}cdf(e,n,r){let o=this.density(n),a=e-this.lo;return r?Mt(o,a,o.length):Mt(o,0,a+1)}};function xt(t,e){return e?U(t,0,1):1/U(n=>t(1/n),Number.EPSILON,1)}function kn(t,e){if(e===t.lo)return 0;if(e===t.hi)return 1/0;let n=t.mean(1);return n===e?1:xt(r=>t.mean(r)-e,n>e)}function Et(t,e,n){if(e===t.hi)return 1/0;let r=t.cdf(e,1,!1);return r===n?1:xt(o=>n-t.cdf(e,o,!1),r<n)}function kt(t,e,n){if(e===t.lo)return 0;let r=t.cdf(e,1,!0);return r===n?1:xt(o=>t.cdf(e,o,!0)-n,r>n)}function jr(t,e={}){let n=t.length;if(n<1)throw new RangeError("'x' must have at least one stratum");let r=t[0].length,o=t[0][0]?.length??0;if(r<2||o<2||n<2)throw new RangeError("each dimension in table must be >= 2");for(let i of t){if(i.length!==r||i.some(u=>u.length!==o))throw new RangeError("all strata must have the same dimensions");if(i.some(u=>u.some(m=>!Number.isFinite(m)||m<0)))throw new RangeError("all entries of 'x' must be nonnegative and finite")}let a=t.map(i=>i.reduce((u,m)=>u+m.reduce((h,d)=>h+d,0),0));if(a.some(i=>i<2))throw new RangeError("sample size in each stratum must be > 1");let l=$(e.alternative),c=e.correct??!0,s=e.exact??!1,f=e.confLevel??.95;if(J(f),r===2&&o===2){let i=t.map(u=>[[u[0][0],u[0][1]],[u[1][0],u[1][1]]]);return s?Br(i,l,f):Hr(i,a,l,c,f)}return Gr(t,a,r,o)}function Hr(t,e,n,r,o){let a=0,l=0,c=0,s=0,f=0,i=0,u=0,m=0;t.forEach(([[N,y],[k,M]],x)=>{let A=e[x],E=N+y,I=k+M,q=N+k,R=y+M;a+=N,l+=E*q/A,c+=E*I*q*R/(A*A*(A-1));let F=(N+M)/A,L=(y+k)/A,S=N*M/A,T=y*k/A;s+=S,f+=T,i+=F*S,u+=F*T+L*S,m+=L*T});let h=a-l,d=r&&Math.abs(h)>=.5?.5:0,g=(Math.abs(h)-d)**2/c,p=n==="two.sided"?G(g,.5,2,!1,!1):z(Math.sign(h)*Math.sqrt(g),{lowerTail:n==="less"}),b=s/f,v=Math.sqrt(i/(2*s*s)+u/(2*s*f)+m/(2*f*f)),w;if(n==="less")w=[0,b*Math.exp(P(o)*v)];else if(n==="greater")w=[b*Math.exp(P(o,{lowerTail:!1})*v),1/0];else{let N=P((1+o)/2);w=[b*Math.exp(-N*v),b*Math.exp(N*v)]}return{method:`Mantel-Haenszel chi-squared test ${d!==0?"with":"without"} continuity correction`,alternative:n,statistic:g,statisticName:"Mantel-Haenszel X-squared",parameter:1,parameterName:"df",pValue:p,confInt:w,confLevel:o,estimate:b,estimateNames:"common odds ratio",nullValue:1,nullValueName:"common odds ratio"}}function Br(t,e,n){let r=new Qe(t),o=t.reduce((c,s)=>c+s[0][0],0),a;if(e==="two.sided"){let c=r.density(1),s=c[o-r.lo]*(1+1e-7),f=[0,0];for(let i of c)i<=s&&(f=D(f,[i,0]));a=Math.min(1,f[0])}else a=r.cdf(o,1,e==="greater");let l;if(e==="less")l=[0,Et(r,o,1-n)];else if(e==="greater")l=[kt(r,o,1-n),1/0];else{let c=(1-n)/2;l=[kt(r,o,c),Et(r,o,c)]}return{method:"Exact conditional test of independence in 2 x 2 x k tables",alternative:e,statistic:o,statisticName:"S",pValue:a,confInt:l,confLevel:n,estimate:kn(r,o),estimateNames:"common odds ratio",nullValue:1,nullValueName:"common odds ratio"}}function Gr(t,e,n,r){let o=(n-1)*(r-1),a=new Array(o).fill(0),l=Array.from({length:o},()=>new Array(o).fill(0));t.forEach((f,i)=>{let u=e[i],m=f.map(g=>g.reduce((p,b)=>p+b,0)),h=Array.from({length:r},(g,p)=>f.reduce((b,v)=>b+v[p],0)),d=1/(u*u*(u-1));for(let g=0;g<r-1;g++)for(let p=0;p<n-1;p++){let b=p+(n-1)*g;a[b]+=f[p][g]-m[p]*h[g]/u;for(let v=0;v<r-1;v++)for(let w=0;w<n-1;w++){let N=(p===w?u*m[p]:0)-m[p]*m[w],y=(g===v?u*h[g]:0)-h[g]*h[v];l[b][w+(n-1)*v]+=d*N*y}}});let c=an(l,a),s=0;for(let f=0;f<o;f++)s+=a[f]*c[f];return{method:"Cochran-Mantel-Haenszel test",statistic:s,statisticName:"Cochran-Mantel-Haenszel M^2",parameter:o,parameterName:"df",pValue:G(s,o/2,2,!1,!1)}}var Rn=new Map;function Ur(t,e){let n=`${t},${e}`,r=Rn.get(n);if(r!==void 0)return r;let o=t+e,a=[];for(let m=1;m<=o;m++)a.push(Math.min(m,o+1-m));let l=a.reduce((m,h)=>m+h,0),c=Array.from({length:t+1},()=>new Float64Array(l+1));c[0][0]=1;for(let m=0;m<o;m++){let h=o-m,d=a[m],g=Array.from({length:t+1},()=>new Float64Array(l+1));for(let p=0;p<=t;p++){let b=c[p],v=t-p;if(v>h)continue;let w=v/h;for(let N=0;N<=l;N++){let y=b[N];y!==0&&(v>0&&(g[p+1][N+d]+=y*w),v<h&&(g[p][N]+=y*(1-w)))}}c=g}let s=c[t],f=0;for(;f<s.length&&s[f]===0;)f++;let i=s.length-1;for(;i>f&&s[i]===0;)i--;let u={min:f,probs:s.slice(f,i+1)};return Rn.set(n,u),u}function Rt(t,e,n,r=!0){if(Number.isNaN(t)||!(e>=1)||!(n>=1)||e!==Math.floor(e)||n!==Math.floor(n))return NaN;let{min:o,probs:a}=Ur(e,n),l=Math.floor(t+1e-7)-o,c=0;if(r)for(let s=0;s<=Math.min(l,a.length-1);s++)c+=a[s];else for(let s=Math.max(l+1,0);s<a.length;s++)c+=a[s];return Math.min(c,1)}var An=new Map;function Kr(t,e){let n=`${t},${e}`,r=An.get(n);if(r!==void 0)return r;let o=t+e,a=0;for(let u=1;u<=o;u++)a+=Math.min(u,o+1-u);let l=Array.from({length:t+1},()=>new Float64Array(a+1));l[0][0]=1;for(let u=0;u<o;u++){let m=Math.min(u+1,o-u),h=Array.from({length:t+1},()=>new Float64Array(a+1));for(let d=0;d<=t;d++){if(t-d>o-u)continue;let g=l[d];for(let p=0;p<=a;p++){let b=g[p];b!==0&&(d<t&&(h[d+1][p+m]+=b),t-d<o-u&&(h[d][p]+=b))}}l=h}let c=l[t],s=0;for(;c[s]===0;)s++;let f=c.length-1;for(;c[f]===0;)f--;let i={min:s,counts:c.slice(s,f+1)};return An.set(n,i),i}function Ye(t,e,n){let{min:r,counts:o}=Kr(e,n);if(t<=0)return r;if(t>=1)return r+o.length-1;let a=nn(e+n,e),l=0;for(let c=0;c<o.length;c++)if(l+=o[c]/a,l>=t)return r+c;return r+o.length-1}function Xr(t,e,n={}){let r=Array.from(t).filter(w=>!Number.isNaN(w)),o=Array.from(e).filter(w=>!Number.isNaN(w)),a=r.length,l=o.length;if(a<1)throw new RangeError("not enough 'x' observations");if(l<1)throw new RangeError("not enough 'y' observations");let c=$(n.alternative),s=n.confInt??!1,f=n.confLevel??.95;s&&J(f);let i=a+l,u=H(r.concat(o)),m=u.map(w=>Math.min(w,i-w+1)),h=0;for(let w=0;w<a;w++)h+=m[w];let d=new Set(u).size<i,g=n.exact??(a<50&&l<50&&!d),p,b;if(g&&!d){let w=y=>Rt(y-1,a,l,!1),N=y=>Rt(y,a,l,!0);if(c==="two.sided"){let y=Math.floor((a+1)**2/4)+Math.floor(a*l/2)/2;p=Math.min(1,2*(h>y?w(h):N(h)))}else c==="less"?p=w(h):p=N(h);s&&(b=Yr(r,o,c,f))}else{let w=0;for(let x of m)w+=x*x;let N=i%2===0,y=N?a*(i+2)/4:a*(i+1)**2/(4*i),k=N?a*l*(16*w-i*(i+2)**2)/(16*i*(i-1)):a*l*(16*i*w-(i+1)**4)/(16*i*i*(i-1)),M=(h-y)/Math.sqrt(k);p=c==="two.sided"?2*Math.min(z(M),z(M,{lowerTail:!1})):c==="less"?z(M,{lowerTail:!1}):z(M),s&&(b=Zr(r,o,c,f))}let v={method:"Ansari-Bradley test",alternative:c,statistic:h,statisticName:"AB",pValue:p};return b!==void 0&&(v.confInt=b.confInt,v.confLevel=f,v.estimate=b.estimate,v.estimateNames="ratio of scales"),v.nullValue=1,v.nullValueName="ratio of scales",v}function qn(t,e,n){let r=t.length,o=r+e.length,a=Qr(t.map(s=>s/n).concat(e)),l=a.map(s=>Math.min(s,o-s+1)),c=0;for(let s=0;s<r;s++)c+=l[s];return{ab:c,a:l,ties:new Set(a).size<o}}function Qr(t){let e=t.filter(a=>!Number.isNaN(a));if(e.length===t.length)return H(t);let n=H(e),r=0,o=e.length;return t.map(a=>Number.isNaN(a)?++o:n[r++])}function At(t,e,n){return t==="two.sided"?n(e):t==="greater"?[n(2*e)[0],1/0]:[0,n(2*e)[1]]}function Yr(t,e,n,r){let o=t.length,a=e.length,l=[];for(let p of t)for(let b of e)p/b>=0&&l.push(p/b);l.sort((p,b)=>p-b);let c=l.length;if(c===0)return{confInt:At(n,1-r,()=>[NaN,NaN]),estimate:NaN};let s=l.map((p,b)=>qn(t,e,p+(b<c-1?(l[b+1]-p)/2:p*1.01)).ab),f=(p,b,v)=>{let w=1/0;for(let y of s)(b?y-p>0:y-p>=0)&&y-p<w&&(w=y-p);let N=v?-1/0:1/0;return s.forEach((y,k)=>{y-p===w&&(N=v?Math.max(N,l[k]):Math.min(N,l[k]))}),N},i=p=>{let b=Ye(p/2,o,a),v=Ye(1-p/2,o,a);if(!s.some(y=>y-b>=0)||!s.some(y=>y-v>0))return[NaN,NaN];let w=f(b,!1,!1),N=f(v,!0,!0);return w>N?[f(v,!1,!1),f(b,!0,!0)]:[w,N]},u=At(n,1-r,i),m=Ye(.5,o,a),h=-1/0,d=1/0;s.forEach((p,b)=>{p-m<=0?h=Math.max(h,l[b]):d=Math.min(d,l[b])});let g=h===-1/0||d===1/0?NaN:(d+h)/2;return{confInt:u,estimate:g}}function Zr(t,e,n,r){let o=t.length,a=e.length,l=o+a,c=l%2===0,s=(b,v)=>{let{ab:w,a:N,ties:y}=qn(t,e,b),k=c?w-o*(l+2)/4:w-o*(l+1)**2/(4*l),M;if(!y)M=Math.sqrt(c?o*a*(l+2)*(l-2)/(48*(l-1)):o*a*(l+1)*(3+l*l)/(48*l*l));else{let x=0;for(let A of N)x+=A*A;M=Math.sqrt(c?o*a*(16*x-l*(l+2)**2)/(16*l*(l-1)):o*a*(16*l*x-(l+1)**4)/(16*l*l*(l-1)))}return k/M-v},f=[],i=(b,v)=>{b.length===0||v.length===0||f.push(Math.min(...b)/Math.max(...v),Math.max(...b)/Math.min(...v))};if(i(t.filter(b=>b>0),e.filter(b=>b>0)),i(t.filter(b=>b<=0),e.filter(b=>b<0)),f.some(b=>!Number.isFinite(b)))return;let u=Math.min(...f),m=Math.max(...f),h=b=>U(v=>s(v,b),u,m,{tol:1e-4}),d=b=>{let v=P(b/2),w=P(b/2,{lowerTail:!1});if(s(u,v)>0||s(m,w)<0)return[NaN,NaN];let N=h(v),y=h(w);return N<=y?[N,y]:[y,N]},g=At(n,1-r,d),p=s(u,0)>0||s(m,0)<0?NaN:h(0);return{confInt:g,estimate:p}}function Jr(t,e,n={}){let r=Array.from(t).filter(x=>!Number.isNaN(x)),o=Array.from(e).filter(x=>!Number.isNaN(x)),a=r.length,l=o.length;if(a<1)throw new RangeError("not enough 'x' observations");if(l<1)throw new RangeError("not enough 'y' observations");let c=$(n.alternative),s=n.confLevel??.95;De(s);let f=H(r.concat(o)),i=f.slice(0,a),u=f.slice(a),m=x=>x.reduce((A,E)=>A+E,0)/x.length,h=m(i),d=m(u),g=(x,A,E)=>{let I=(x.length+1)/2,q=0;for(let R=0;R<x.length;R++)q+=(x[R]-A[R]-E+I)**2;return q/(x.length-1)},p=g(i,H(r),h),b=g(u,H(o),d),v=(d-(l+1)/2)/a,w=a*p+l*b,N=a*l*(d-h)/(a+l)/Math.sqrt(w),y=w**2/((a*p)**2/(a-1)+(l*b)**2/(l-1)),k;c==="greater"?k=B(N,y,!0,!1):c==="less"?k=B(N,y,!1,!1):k=2*B(-Math.abs(N),y,!0,!1);let M=ae((1-s)/2,y,!1,!1)*Math.sqrt(p/(a*l**2)+b/(l*a**2));return{method:"Brunner-Munzel Test",alternative:c,statistic:N,statisticName:"Brunner-Munzel Test Statistic",parameter:y,parameterName:"df",pValue:k,confInt:[v-M,v+M],confLevel:s,estimate:v,estimateNames:"P(X<Y)+.5*P(X=Y)"}}var eo=[1.2337005501361697,7831619385924639e-32];function to(t,e,n){let r=(i,u)=>{let m=new Float64Array(e*e);for(let h=0;h<e;h++)for(let d=0;d<e;d++){let g=i[h*e+d];if(g!==0)for(let p=0;p<e;p++)m[h*e+p]+=g*u[d*e+p]}return m},o=i=>{let u=0;for(let d of i)u=Math.max(u,Math.abs(d));if(u===0||!Number.isFinite(u))return 0;let m=Math.floor(Math.log2(u));if(Math.abs(m)<500)return 0;let h=2**-m;for(let d=0;d<i.length;d++)i[d]*=h;return m},a,l=0,c=t.slice(),s=0,f=n;for(;f>0;)f&1&&(a=a===void 0?c.slice():r(a,c),l+=s,l+=o(a)),f>>=1,f>0&&(c=r(c,c),s*=2,s+=o(c));return[a,l]}function Tn(t,e){if(Number.isNaN(e)||!(t>=1)||t!==Math.floor(t))return NaN;if(e<=1/(2*t))return 0;if(e>=1)return 1;if(t*e<1){let[i,u]=Ve(t,e),m=2*i-1+2*u,h=1,d=0;for(let g=1;g<=t;g++)h*=g/t*m,h<2**-500&&(h*=2**500,d-=500);for(;d<-1e3;)h*=2**-1e3,d+=1e3;return h*2**d}let n=Math.floor(t*e)+1,r=2*n-1,o=n-t*e,a=new Float64Array(r*r);for(let i=0;i<r;i++)for(let u=0;u<r;u++)a[i*r+u]=i-u+1>=0?1:0;for(let i=0;i<r;i++)a[i*r]-=Math.pow(o,i+1),a[(r-1)*r+i]-=Math.pow(o,r-i);2*o-1>0&&(a[(r-1)*r]+=Math.pow(2*o-1,r));for(let i=0;i<r;i++)for(let u=0;u<r;u++){let m=i-u+1;if(m>0){let h=1;for(let d=2;d<=m;d++)h*=d;a[i*r+u]/=h}}let[l,c]=to(a,r,t),s=l[(n-1)*r+n-1],f=c;for(let i=1;i<=t;i++)s=s*i/t,Math.abs(s)<2**-500&&(s*=2**500,f-=500);for(;f>1e3;)s*=2**1e3,f-=1e3;for(;f<-1e3;)s*=2**-1e3,f+=1e3;return s*2**f}function qt(t,e=!0){if(Number.isNaN(t))return NaN;if(t<=0)return e?0:1;if(t===1/0)return e?1:0;if(t<1){let o=Oe(eo,Ve(t,t)),a=o[0]+o[1],l=Math.exp(-o[0])*(1-o[1]),c=0;for(let f=2;f<100;f++){let i=Math.exp(-(2*f-1)*(2*f-1)*a);if(c+=i,i<=1e-17*(c+l))break}let s=Math.sqrt(2*Math.PI)/t*(l+c);return e?s:1-s}let n=0;for(let o=1;o<100;o++){let a=Math.exp(-2*o*o*t*t);if(n+=o%2===1?a:-a,a<=1e-17*Math.abs(n))break}let r=2*n;return e?1-r:r}function In(t,e){if(Number.isNaN(e)||!(t>=1)||t!==Math.floor(t))return NaN;if(e<=0)return 1;if(e>1)return 0;let[n,r]=Ve(t,e),o=Math.floor(t-n-r),a=0;for(let l=0;l<=o;l++){let c=D(nt(t-l,-n),[-r,0]);if(c[0]<=0)break;let s=D(nt(l,n),[r,0]),f=Oe(s,[t,0]),i=Oe(c,[t,0]);a+=Bt(l,t-l,f,i,!1)/(f[0]+f[1])}return Math.min(e*a,1)}function Sn(t,e,n,r={}){let o=r.alternative??"two.sided";if(Number.isNaN(t)||!(e>=1)||!(n>=1)||e!==Math.floor(e)||n!==Math.floor(n))return NaN;if(t<=0)return 1;if(t>1)return 0;let a=e+n,l;if(r.z!==void 0){if(r.z.length!==a)throw new RangeError("z must have length m + n");let m=[...r.z].sort((h,d)=>h-d);l=new Array(a+1).fill(!1);for(let h=1;h<a;h++)l[h]=m[h]!==m[h-1]}let c=t*e*n*(1-1e-10),s=(m,h)=>{let d=m*n-h*e;return(o==="two.sided"?Math.abs(d):o==="greater"?d:-d)>=c},f=new Float64Array(n+1),i=new Float64Array(n+1),u=0;for(let m=0;m<=e;m++){for(let h=0;h<=n;h++){if(m===0&&h===0){i[0]=1;continue}let d=m+h,g=(m>0?m/d*f[h]:0)+(h>0?h/d*i[h-1]:0);(l===void 0?d<a:l[d])&&s(m,h)?(g>0&&(u+=g*ge(m,e,n,d,!1)),i[h]=0):i[h]=g}[f,i]=[i,f],i.fill(0)}return Math.min(u,1)}function Ln(t,e){return t==="two.sided"?"two-sided":`the CDF of x lies ${t==="less"?"below":"above"} ${e?"the null hypothesis":"that of y"}`}function no(t,e,n={}){let r=Array.from(t).filter(v=>!Number.isNaN(v)).sort((v,w)=>v-w),o=r.length;if(o<1)throw new RangeError("not enough 'x' data");let a=$(n.alternative),l=a==="two.sided"?"D":a==="greater"?"D^+":"D^-";if(typeof e=="function"){let v=-1/0,w=-1/0;for(let x=0;x<o;x++){let A=e(r[x]);v=Math.max(v,(x+1)/o-A),w=Math.max(w,A-x/o)}let N=a==="two.sided"?Math.max(v,w):a==="greater"?v:w,y=new Set(r).size<o,k=n.exact??(o<100&&!y),M;return a==="two.sided"?M=k?1-Tn(o,N):qt(Math.sqrt(o)*N,!1):M=k?In(o,N):Math.exp(-2*o*N*N),{method:`${k?"Exact":"Asymptotic"} one-sample Kolmogorov-Smirnov test`,alternative:Ln(a,!0),exact:k,statistic:N,statisticName:l,pValue:Math.min(1,Math.max(0,M))}}let c=Array.from(e).filter(v=>!Number.isNaN(v)),s=c.length;if(s<1)throw new RangeError("not enough 'y' data");let f=r.map(v=>[v,0]).concat(c.map(v=>[v,1]));f.sort((v,w)=>v[0]-w[0]);let i=0,u=0,m=0,h=0;for(let v=0;v<f.length;v++){if(f[v][1]===0?i++:u++,v+1<f.length&&f[v+1][0]===f[v][0])continue;let w=i/o-u/s;m=Math.max(m,w),h=Math.max(h,-w)}let d=a==="two.sided"?Math.max(m,h):a==="greater"?m:h,g=new Set(f.map(v=>v[0])).size<f.length,p=n.exact??o*s<1e4,b;if(p)b=Sn(d,o,s,g?{alternative:a,z:f.map(v=>v[0])}:{alternative:a});else{let v=o*s/(o+s);b=a==="two.sided"?qt(Math.sqrt(v)*d,!1):Math.exp(-2*v*d*d)}return{method:`${p?"Exact":"Asymptotic"} two-sample Kolmogorov-Smirnov test`,alternative:Ln(a,!1),exact:p,statistic:d,statisticName:l,pValue:Math.min(1,Math.max(0,b))}}function Fn(t,e,n){let r=Array.from(t);if(r.length!==e.length)throw new RangeError("'x' and 'g' must have the same length");let o=new Map;for(let l=0;l<r.length;l++){let c=e[l];if(typeof c=="number"&&Number.isNaN(c)||n&&Number.isNaN(r[l]))continue;let s=o.get(c);s===void 0&&o.set(c,s=[]),s.push(r[l])}let a=Array.from(o.keys()).sort((l,c)=>typeof l=="number"&&typeof c=="number"?l-c:String(l)<String(c)?-1:String(l)>String(c)?1:0);return{levels:a,groups:a.map(l=>o.get(l))}}function Tt(t,e,n){let r=[],o=[];for(let c=0;c<t-1;c++)for(let s=c+1;s<t;s++)r.push(e(s,c)),o.push([s-1,c]);let a=gt(r,n),l=Array.from({length:t-1},()=>new Array(t-1).fill(NaN));return o.forEach(([c,s],f)=>l[c][s]=a[f]),l}function It(t,e,n,r){return{method:t,pValue:n,rowNames:e.slice(1).map(String),colNames:e.slice(0,-1).map(String),pAdjustMethod:r}}function ro(t,e,n={}){let r=n.paired??!1,o=n.poolSd??!r;if(r&&o)throw new RangeError("pooling of SD is incompatible with paired tests");let a=$(n.alternative),l=n.pAdjustMethod??"holm",{levels:c,groups:s}=Fn(t,e,!r),f=c.length;if(f<2)throw new RangeError("there must be at least two groups");let i,u;if(o){u="t tests with pooled SD";let m=s.map(b=>b.length),h=s.map(b=>b.length>0?te(b):NaN),d=0,g=0;s.forEach(b=>{b.length>1&&(d+=Y(b)*(b.length-1),g+=b.length-1)});let p=Math.sqrt(d/g);i=(b,v)=>{let w=(h[b]-h[v])/(p*Math.sqrt(1/m[b]+1/m[v]));return a==="less"?B(w,g,!0,!1):a==="greater"?B(w,g,!1,!1):2*B(-Math.abs(w),g,!0,!1)}}else u=r?"paired t tests":"t tests with non-pooled SD",i=(m,h)=>it(s[m],s[h],{paired:r,alternative:a}).pValue;return It(u,c,Tt(f,i,l),l)}function oo(t,e,n={}){let{pAdjustMethod:r,alternative:o,...a}=n,l=r??"holm",{levels:c,groups:s}=Fn(t,e,!(a.paired??!1)),f=c.length;if(f<2)throw new RangeError("there must be at least two groups");let i="",u=Tt(f,(m,h)=>{let d=ht(s[m],s[h],{...a,...o===void 0?{}:{alternative:o}});return i=d.method,d.pValue},l);return It(i,c,u,l)}function so(t,e,n={}){if(t.length!==e.length)throw new RangeError("'x' and 'n' must have the same length");let r=[],o=[];t.forEach((i,u)=>{Number.isNaN(i)||Number.isNaN(e[u])||(r.push(i),o.push(e[u]))});let a=r.length;if(a<2)throw new RangeError("too few groups");let l=n.pAdjustMethod??"holm",c=n.correct??!0,s=Tt(a,(i,u)=>Nt([r[i],r[u]],[o[i],o[u]],{correct:c}).pValue,l),f=Array.from({length:a},(i,u)=>u+1);return It("Pairwise comparison of proportions",f,s,l)}function Vn(t){let e=t.flat(),n=Te(e),r=[0,0],o=[0,0],a=[];for(let l of t){let c=Te(l);a.push(c[0]);let s=ee(c,n);r=D(r,Ce(Ce(s,s),[l.length,0]));for(let f of l){let i=ee([f,0],c);o=D(o,Ce(i,i))}}return{ssb:r[0],ssw:o[0],means:a}}function ao(t,e){let n=me(t,e),r=n.length;if(r<2)throw new RangeError("contrasts can be applied only to factors with 2 or more levels");let o=n.reduce((m,h)=>m+h.length,0),{ssb:a,ssw:l}=Vn(n),c=r-1,s=o-r,f=a/c,i=l/s,u=f/i;return{df:[c,s],sumSq:[a,l],meanSq:[f,i],fValue:u,pValue:s>0?Q(u,c,s,!1,!1):NaN}}function io(t,e,n={}){let r;e!==void 0&&!Array.isArray(e)?n=e:r=e;let{labels:o,groups:a}=vt(t,r),l=a.length;if(l<2)throw new RangeError("contrasts can be applied only to factors with 2 or more levels");let c=n.confLevel??.95,f=a.reduce((g,p)=>g+p.length,0)-l,{ssw:i,means:u}=Vn(a),m=i/f,h=Jt(c,l,f),d={names:[],i:[],j:[],diff:[],lwr:[],upr:[],pAdj:[]};for(let g=0;g<l-1;g++)for(let p=g+1;p<l;p++){let b=u[p]-u[g],v=Math.sqrt(m/2*(1/a[p].length+1/a[g].length));d.names.push(`${o[p]}-${o[g]}`),d.i.push(p+1),d.j.push(g+1),d.diff.push(b),d.lwr.push(b-h*v),d.upr.push(b+h*v),d.pAdj.push(Zt(Math.abs(b)/v,l,f,{lowerTail:!1}))}return d}function lo(t,e,n={}){let r;e!==void 0&&!ArrayBuffer.isView(e)&&!Array.isArray(e)?n=e:r=e;let{sigmaX:o,sigmaY:a}=n,l=n.mu??0;if(o===void 0&&r===void 0)throw new RangeError("sigmaX (the known standard deviation of x) is required");if(o===void 0||r!==void 0&&a===void 0)throw new RangeError("sigmaX and sigmaY are both required for two samples");if(Number.isNaN(l))throw new RangeError("mu must be a number");let c=$(n.alternative),s=n.confLevel??.95;if(!(s>=0&&s<=1))throw new RangeError("confLevel must be between 0 and 1");let f=Array.from(t).filter(w=>!Number.isNaN(w)),i=r===void 0?void 0:Array.from(r).filter(w=>!Number.isNaN(w));if(f.length<3)throw new RangeError("x needs at least 3 non-missing values");if(i!==void 0&&i.length<3)throw new RangeError("y needs at least 3 non-missing values");let u=Te(f)[0],m=i===void 0?NaN:Te(i)[0],h=i===void 0?o/Math.sqrt(f.length):Math.sqrt(o**2/f.length+a**2/i.length),d=i===void 0?u:u-m,g=(d-l)/h,p,b;if(c==="less")p=z(g),b=[-1/0,d+P(s)*h];else if(c==="greater")p=z(g,{lowerTail:!1}),b=[d-P(s)*h,1/0];else{p=2*z(-Math.abs(g));let w=P(1-(1-s)/2)*h;b=[d-w,d+w]}let v=i!==void 0;return{method:v?"Two-sample z-Test":"One-sample z-Test",alternative:c,statistic:g,statisticName:"z",pValue:p,confInt:b,confLevel:s,estimate:v?[u,m]:u,estimateNames:v?["mean of x","mean of y"]:"mean of x",nullValue:l,nullValueName:v?"difference in means":"mean"}}function co(t,e,n={}){let r=n.intercept??!0,o=t.length>0&&typeof t[0]!="number"?t.map(V=>Array.from(V)):Array.from(t,V=>[V]),a=Array.from(e);if(o.length!==a.length)throw new RangeError("variable lengths differ (found for 'x')");let l=n.weights===void 0?void 0:Array.from(n.weights);if(l!==void 0&&l.length!==a.length)throw new RangeError("variable lengths differ (found for '(weights)')");if(l?.some(V=>V<0))throw new RangeError("missing or negative weights not allowed");let c=a.map((V,O)=>!Number.isNaN(V)&&o[O].every(j=>!Number.isNaN(j))&&(l===void 0||!Number.isNaN(l[O]))),s=o.filter((V,O)=>c[O]).map(V=>r?[1,...V]:V),f=a.filter((V,O)=>c[O]),i=l?.filter((V,O)=>c[O]);if(f.length===0)throw new RangeError("0 (non-NA) cases");let u=f.map((V,O)=>i===void 0||i[O]!==0),m=s.filter((V,O)=>u[O]),h=f.filter((V,O)=>u[O]),d=i?.filter((V,O)=>u[O]),g=h.length;if(g===0)throw new RangeError("all weights are zero");let p=d?.map(Math.sqrt),b=p===void 0?m:m.map((V,O)=>V.map(j=>j*p[O])),v=p===void 0?h:h.map((V,O)=>V*p[O]),w=ve(b),N=w.rank,y=We(w,v),k=_e(w,v),M=p===void 0?k:k.map((V,O)=>V/p[O]),x=[],A=[],E=0;f.forEach((V,O)=>{if(u[O])x.push(M[E]),A.push(V-M[E]),E++;else{let j=s[O].reduce((Fe,jn,Wt)=>Number.isNaN(y[Wt])?Fe:Fe+jn*y[Wt],0);A.push(j),x.push(V-j)}});let I=A.filter((V,O)=>u[O]),q=g-N,R=d??new Array(g).fill(1),F=0;for(let V=0;V<g;V++)F+=R[V]*M[V]**2;let L=F/q,S=Math.sqrt(L),T=w.qr.slice(0,N).map(V=>V.slice(0,N)),C=N>0?je(T):[],_=w.pivot.slice(0,N).map((V,O)=>[V,O]).sort((V,O)=>V[0]-O[0]),X=_.map(([V])=>y[V]),oe=_.map(([,V])=>Math.sqrt(C[V][V]*L)),se=X.map((V,O)=>V/oe[O]),Ee=se.map(V=>2*B(-Math.abs(V),q,!0,!1)),ie={coefficients:y,residuals:x,fittedValues:A,rank:N,dfResidual:q,coefTable:{estimate:X,stdError:oe,tValue:se,pValue:Ee},sigma:S,rSquared:0,adjRSquared:0,intercept:r,qr:w},$t=r?1:0,Le=N-$t;if(Le===0)return ie;let ke=0;if(r){let V=R.reduce((j,Fe)=>j+Fe,0),O=0;for(let j=0;j<g;j++)O+=R[j]*I[j]/V;for(let j=0;j<g;j++)ke+=R[j]*(I[j]-O)**2}else for(let V=0;V<g;V++)ke+=R[V]*I[V]**2;ie.rSquared=ke/(ke+F),ie.adjRSquared=1-(1-ie.rSquared)*((g-$t)/q);let Dt=ke/Le/L;return ie.fStatistic={value:Dt,numdf:Le,dendf:q},ie.fPValue=Q(Dt,Le,q,!1,!1),ie}function fo(t,e={}){let n=e.level??.95,r=ae((1+n)/2,t.dfResidual,!0,!1),{estimate:o,stdError:a}=t.coefTable,l=t.coefficients.map(()=>NaN),c=t.coefficients.map(()=>NaN),s=0;return t.coefficients.forEach((f,i)=>{Number.isNaN(f)||(l[i]=o[s]-r*a[s],c[i]=o[s]+r*a[s],s++)}),{lower:l,upper:c}}function uo(t,e,n={}){let o=(e.length>0&&typeof e[0]!="number"?e.map(d=>Array.from(d)):Array.from(e,d=>[d])).map(d=>t.intercept?[1,...d]:d),a=t.rank,l=t.qr.pivot.slice(0,a),c=t.qr.qr.slice(0,a).map(d=>d.slice(0,a)),s=t.sigma**2,f=o.map(d=>d.reduce((g,p,b)=>Number.isNaN(t.coefficients[b])?g:g+p*t.coefficients[b],0)),i=o.map(d=>{let g=new Array(a).fill(0);for(let p=0;p<a;p++){let b=d[l[p]];for(let v=0;v<p;v++)b-=c[v][p]*g[v];g[p]=b/c[p][p]}return Math.sqrt(g.reduce((p,b)=>p+b*b,0)*s)}),u=n.interval??"none";if(u==="none")return{fit:f,seFit:i};let m=ae((1+(n.level??.95))/2,t.dfResidual,!0,!1),h=i.map(d=>m*Math.sqrt(d*d+(u==="prediction"?s:0)));return{fit:f,seFit:i,lwr:f.map((d,g)=>d-h[g]),upr:f.map((d,g)=>d+h[g])}}var K=2**-52,mo={gaussian:["identity","log","inverse"],binomial:["logit","probit","cauchit","log","cloglog"],quasibinomial:["logit","probit","cauchit","log","cloglog"],poisson:["log","identity","sqrt"],quasipoisson:["log","identity","sqrt"],Gamma:["inverse","identity","log"],"inverse.gaussian":["1/mu^2","inverse","identity","log"],quasi:["identity","logit","probit","cloglog","inverse","log","1/mu^2","sqrt"]},St=t=>Number.isFinite(t)&&t>0,be=()=>!0,Z=class{list=[];add(e){this.list.includes(e)||this.list.push(e)}},Ze;function Je(t,e){let n=Ze;Ze=t;try{return e()}finally{Ze=n}}function ho(t){return t<0&&Ze?.add("NaNs produced"),Math.sqrt(t)}function Lt(t){switch(t){case"identity":return{name:t,link:e=>e,linkinv:e=>e,muEta:()=>1,etaOk:be};case"log":return{name:t,link:Math.log,linkinv:e=>Math.max(Math.exp(e),K),muEta:e=>Math.max(Math.exp(e),K),etaOk:be};case"inverse":return{name:t,link:e=>1/e,linkinv:e=>1/e,muEta:e=>-1/(e*e),etaOk:e=>Number.isFinite(e)&&e!==0};case"sqrt":return{name:t,link:Math.sqrt,linkinv:e=>e*e,muEta:e=>2*e,etaOk:St};case"1/mu^2":return{name:t,link:e=>1/(e*e),linkinv:e=>1/ho(e),muEta:e=>-1/(2*e**1.5),etaOk:St};case"logit":return{name:t,link:e=>Math.log(e/(1-e)),linkinv:e=>{let n=e<-30?K:e>30?1/K:Math.exp(e);return n/(1+n)},muEta:e=>{if(Math.abs(e)>30)return K;let n=Math.exp(e);return n/((1+n)*(1+n))},etaOk:be};case"probit":{let e=-P(K);return{name:t,link:n=>P(n),linkinv:n=>z(Math.min(Math.max(n,-e),e)),muEta:n=>Math.max(Ht(n),K),etaOk:be}}case"cauchit":{let e=-st(K,0,1,!0,!1);return{name:t,link:n=>st(n,0,1,!0,!1),linkinv:n=>Xt(Math.min(Math.max(n,-e),e),0,1,!0,!1),muEta:n=>Math.max(Kt(n,0,1,!1),K),etaOk:be}}case"cloglog":return{name:t,link:e=>Math.log(-Math.log(1-e)),linkinv:e=>Math.min(Math.max(-Math.expm1(-Math.exp(e)),K),1-K),muEta:e=>{let n=Math.min(e,700);return Math.max(Math.exp(n)*Math.exp(-Math.exp(n)),K)},etaOk:be};default:throw new RangeError(`unknown link "${String(t)}"`)}}function po(t){if(!Number.isFinite(t))throw new RangeError("invalid power for the power link");if(t<=0)return Lt("log");if(t===1)return Lt("identity");let e=1/t;return{name:`mu^${Math.round(t*1e3)/1e3}`,link:n=>n**t,linkinv:n=>Math.max(n**e,K),muEta:n=>Math.max(e*n**(e-1),K),etaOk:St}}function go(t){switch(t){case"constant":return{variance:()=>1,muOk:()=>!0,dev:(e,n,r)=>r*(e-n)**2};case"mu(1-mu)":return{variance:e=>e*(1-e),muOk:e=>e>0&&e<1,dev:Cn};case"mu":return{variance:e=>e,muOk:e=>e>0,dev:(e,n,r)=>2*r*((e===0?0:e*Math.log(e/n))-(e-n))};case"mu^2":return{variance:e=>e*e,muOk:e=>e>0,dev:(e,n,r)=>Math.max(-2*r*(Math.log(e>0?e/n:1/n)-(e-n)/n),0)};case"mu^3":return{variance:e=>e*e*e,muOk:e=>e>0,dev:On};default:throw new RangeError(`'variance' "${String(t)}" is invalid: possible values are "mu(1-mu)", "mu", "mu^2", "mu^3" and "constant"`)}}function Cn(t,e,n){let r=t===0?0:t*Math.log(t/e),o=t===1?0:(1-t)*Math.log((1-t)/(1-e));return 2*n*(r+o)}function On(t,e,n){return n*(t-e)**2/(t*e*e)}function Se(t,e){let n=typeof t=="string"?{family:t}:t,r=n.family,o=mo[r];if(o===void 0)throw new RangeError(`unknown family "${String(r)}"`);let a=e??n.link??o[0],l;if(typeof a=="object"){if(r!=="quasi")throw new RangeError(`link "power" not available for ${r} family`);l=po(a.power)}else{if(!o.includes(a))throw new RangeError(`link "${a}" not available for ${r} family; available links are ${o.map(s=>`'${s}'`).join(", ")}`);l=Lt(a)}if(n.variance!==void 0&&r!=="quasi")throw new TypeError("'variance' is only used by the quasi family");let c={...l,family:r,varianceName:void 0,fixedDispersion:!1};switch(r){case"gaussian":return{...c,variance:()=>1,muOk:()=>!0,devContribution:(s,f,i)=>i*(s-f)**2};case"binomial":case"quasibinomial":return{...c,fixedDispersion:r==="binomial",variance:s=>s*(1-s),muOk:s=>Number.isFinite(s)&&s>0&&s<1,devContribution:Cn};case"poisson":case"quasipoisson":return{...c,fixedDispersion:r==="poisson",variance:s=>s,muOk:s=>Number.isFinite(s)&&s>0,devContribution:(s,f,i)=>s>0?2*i*(s*Math.log(s/f)-(s-f)):2*i*f};case"Gamma":return{...c,variance:s=>s*s,muOk:s=>Number.isFinite(s)&&s>0,devContribution:(s,f,i)=>-2*i*((s===0?0:Math.log(s/f))-(s-f)/f)};case"inverse.gaussian":return{...c,variance:s=>s*s*s,muOk:()=>!0,devContribution:On};case"quasi":{let s=n.variance??"constant",f=go(s);return{...c,varianceName:s,variance:f.variance,muOk:f.muOk,devContribution:f.dev}}}}function Ft(t,e,n,r,o){let a=t.family,l=a==="binomial"||a==="quasibinomial";if(e.length>0&&Array.isArray(e[0])){let i=e;if(i.every(g=>g.length===1))return Ft(t,i.map(g=>g[0]),n,r,o);if(!l)throw new TypeError("y must be a vector (or, for the binomial families, a two-column matrix)");if(i.some(g=>g.length!==2))throw new TypeError(`for the '${a}' family, y must be a vector of 0 and 1's
4
- or a 2 column matrix where col 1 is no. successes and col 2 is no. failures`);a==="binomial"&&i.some(g=>Math.abs(g[0]-Math.round(g[0]))>.001||Math.abs(g[1]-Math.round(g[1]))>.001)&&o.add("non-integer counts in a binomial glm!");let u=i.map(g=>g[0]+g[1]),m=i.map((g,p)=>u[p]===0?0:g[0]/u[p]),h=n.map((g,p)=>g*u[p]),d=m.map((g,p)=>(u[p]*g+.5)/(u[p]+1));return{y:m,weights:h,mustart:d,size:u}}let s=e.slice(),f=n.slice();switch(a){case"binomial":case"quasibinomial":{for(let u=0;u<s.length;u++)f[u]===0&&(s[u]=0);if(s.some(u=>u<0||u>1))throw new RangeError("y values must be 0 <= y <= 1");let i=s.map((u,m)=>(f[m]*u+.5)/(f[m]+1));return a==="binomial"&&s.some((u,m)=>Math.abs(f[m]*u-Math.round(f[m]*u))>.001)&&o.add("non-integer #successes in a binomial glm!"),{y:s,weights:f,mustart:i,size:s.map(()=>1)}}case"poisson":case"quasipoisson":if(s.some(i=>i<0))throw new RangeError(`negative values not allowed for the '${a==="poisson"?"Poisson":"quasiPoisson"}' family`);return{y:s,weights:f,mustart:s.map(i=>i+.1),size:void 0};case"Gamma":if(s.some(i=>i<=0))throw new RangeError("non-positive values not allowed for the 'Gamma' family");return{y:s,weights:f,mustart:s.slice(),size:void 0};case"inverse.gaussian":if(s.some(i=>i<=0))throw new RangeError("positive values only are allowed for the 'inverse.gaussian' family");return{y:s,weights:f,mustart:s.slice(),size:void 0};case"gaussian":if(!r&&(t.name==="inverse"&&s.some(i=>i===0)||t.name==="log"&&s.some(i=>i<=0)))throw new Error("cannot find valid starting values: please specify some");return{y:s,weights:f,mustart:s.slice(),size:void 0};case"quasi":{let i=t.varianceName,u=i==="constant"?s.slice():i==="mu(1-mu)"?s.map(m=>Math.min(Math.max(m,.001),.999)):s.map(m=>m+(m===0?.1:0));return{y:s,weights:f,mustart:u,size:void 0}}}}function Pn(t,e,n,r,o,a,l){let c=e.length,s=0;switch(t.family){case"gaussian":{let f=0;for(let i of r)f+=Math.log(i);return c*(Math.log(2*Math.PI*a/c)+1)+2-f}case"binomial":{let f=o!==void 0&&o.some(i=>i>1)?o:r;for(let i=0;i<c;i++)f[i]>0&&(s+=r[i]/f[i]*pe(Re(f[i]*e[i]),Re(f[i]),n[i],!0));return-2*s}case"poisson":for(let f=0;f<c;f++)Ut(e[f])&&l.add(`non-integer x = ${e[f].toFixed(6)}`),s+=r[f]*Ae(e[f],n[f],!0);return-2*s;case"Gamma":{let f=0;for(let u of r)f+=u;let i=a/f;for(let u=0;u<c;u++)s+=r[u]*Gt(e[u],1/i,n[u]*i,!0);return-2*s+2}case"inverse.gaussian":{let f=0,i=0;for(let u=0;u<c;u++)f+=r[u],i+=r[u]*Math.log(e[u]);return f*(Math.log(2*Math.PI*a/f)+1)+3*i+2}default:return NaN}}var ye=2**-52;function wo(t){let e=t.map(n=>`"${n}"`);return e.length===1?e[0]:`c(${e.join(", ")})`}function et(t,e,n){return t.map((r,o)=>{let a=0;for(let l=0;l<r.length;l++)a+=r[l]*e[l];return a+n[o]})}function de(t,e,n,r){let o=0;for(let a=0;a<e.length;a++)o+=t.devContribution(e[a],n[a],r[a]);return o}function zn(t,e){let{x:n,p:r,y:o,w:a,offset:l,fam:c,epsilon:s,maxit:f}=t,i=o.length;return Je(e,()=>{let u=(q,R)=>q.every(F=>c.etaOk(F))&&R.every(F=>c.muOk(F));if(r===0){let q=l.slice();if(!q.every(S=>c.etaOk(S)))throw new Error("invalid linear predictor values in empty model");let R=q.map(S=>c.linkinv(S));if(!R.every(S=>c.muOk(S)))throw new Error("invalid fitted means in empty model");let F=q.map(S=>c.muEta(S)),L=R.map((S,T)=>Math.sqrt(a[T]*F[T]**2/c.variance(S))**2);return{coefficients:[],eta:q,mu:R,deviance:de(c,o,R,a),iter:0,converged:!0,boundary:!0,rank:0,qr:void 0,used:a.map(S=>S>0),workingWeights:L,workingResiduals:o.map((S,T)=>(S-R[T])/F[T])}}let m,h;if(t.start!==void 0){if(t.start.length!==r)throw new RangeError(`length of 'start' should equal ${r} and correspond to initial coefs for ${wo(t.columnNames)}`);h=t.start.slice(),m=et(n,h,l)}else t.etastart!==void 0?m=t.etastart.slice():m=t.mustart.map(q=>c.link(q));let d=m.map(q=>c.linkinv(q));if(!u(m,d))throw new Error("cannot find valid starting values: please specify some");let g=de(c,o,d,a),p=g,b=!1,v=!1,w=0,N=new Array(r).fill(0),y,k=a.map(()=>!1),M=[],x=Math.min(1e-7,s/1e3);for(w=1;w<=f;w++){let q=m.map(T=>c.muEta(T));for(let T=0;T<i;T++){if(!(a[T]>0))continue;let C=c.variance(d[T]);if(Number.isNaN(C))throw new Error("NAs in V(mu)");if(C===0)throw new Error("0s in V(mu)");if(Number.isNaN(q[T]))throw new Error("NAs in d(mu)/d(eta)")}k=a.map((T,C)=>T>0&&q[C]!==0);let R=[];for(let T=0;T<i;T++)k[T]&&R.push(T);if(R.length===0){e.add(`no observations informative at iteration ${w}`),b=!1;break}M=new Array(i).fill(0);let F=[],L=[];for(let T of R){let C=Math.sqrt(a[T]*q[T]**2/c.variance(d[T]));M[T]=C,F.push((m[T]-l[T]+(o[T]-d[T])/q[T])*C),L.push(n[T].map(W=>W*C))}y=ve(L,{tol:x});let S=We(y,F);if(S.some((T,C)=>y.pivot.indexOf(C)<y.rank&&!Number.isFinite(T))){e.add(`non-finite coefficients at iteration ${w}`),b=!1;break}if(N=S.map(T=>Number.isNaN(T)?0:T),v=!1,m=et(n,N,l),d=m.map(T=>c.linkinv(T)),p=de(c,o,d,a),!Number.isFinite(p)){if(h===void 0)throw new Error("no valid set of coefficients has been found: please supply starting values");e.add("step size truncated due to divergence");let T=0;for(;!Number.isFinite(p);){if(T>=f)throw new Error("inner loop 1; cannot correct step size");T++,N=N.map((C,W)=>(C+h[W])/2),m=et(n,N,l),d=m.map(C=>c.linkinv(C)),p=de(c,o,d,a)}v=!0}if(!u(m,d)){if(h===void 0)throw new Error("no valid set of coefficients has been found: please supply starting values");e.add("step size truncated: out of bounds");let T=0;for(;!u(m,d);){if(T>=f)throw new Error("inner loop 2; cannot correct step size");T++,N=N.map((C,W)=>(C+h[W])/2),m=et(n,N,l),d=m.map(C=>c.linkinv(C))}v=!0,p=de(c,o,d,a)}if(t.untilCoefficients){let T=Math.max(...N.map(Math.abs));if(h!==void 0&&N.every((C,W)=>Math.abs(C-h[W])<=8*ye*T)){b=!0;break}}else if(Math.abs(p-g)/(Math.abs(p)+.1)<s){b=!0;break}g=p,h=N.slice()}w>f&&(w=f);let A=r,E=N.slice();if(y!==void 0){A=y.rank;for(let q=y.rank;q<r;q++)E[y.pivot[q]]=NaN}let I=m.map(q=>c.muEta(q));return{coefficients:E,eta:m,mu:d,deviance:p,iter:w,converged:b,boundary:v,rank:A,qr:y,used:k,workingWeights:M.map(q=>q*q),workingResiduals:o.map((q,R)=>(q-d[R])/I[R])}})}function $n(t){return t.length>0&&typeof t[0]!="number"?t.map(e=>Array.from(e)):Array.from(t,e=>[e])}function Dn(t,e,n,r){r||(t.converged||n.add("glm.fit: algorithm did not converge"),t.boundary&&n.add("glm.fit: algorithm stopped at boundary value"),e.family==="binomial"&&t.mu.some(o=>o>1-10*ye||o<10*ye)&&n.add("glm.fit: fitted probabilities numerically 0 or 1 occurred"),e.family==="poisson"&&t.mu.some(o=>o<10*ye)&&n.add("glm.fit: fitted rates numerically 0 occurred"))}function Me(t,e,n,r={}){let o=Se(t.familySpec),a="start"in r?r.start:t.start,l=Ft(o,t.yGiven,t.priorGiven,a!==void 0||r.etastart!==void 0,n),c=r.extraOffset===void 0?t.offset:t.offset.map((i,u)=>i+r.extraOffset[u]),s=t.x.map(i=>e.map(u=>i[u])),f=zn({x:s,p:e.length,y:l.y,w:l.weights,offset:c,fam:o,epsilon:r.epsilon??t.epsilon,maxit:r.maxit??t.maxit,columnNames:e.map(i=>t.columnNames[i]),start:a,etastart:r.etastart,mustart:l.mustart,untilCoefficients:r.untilCoefficients},n);return Dn(f,o,n,e.length===0),{fit:f,fam:o,y:l.y,w:l.weights,size:l.size,offset:c}}function No(t,e,n={}){let r=n.intercept??!0,o=$n(t),l=e.length>0&&typeof e[0]!="number"?e.map(L=>Array.from(L)):Array.from(e),c=l.length;if(o.length!==c)throw new RangeError("variable lengths differ (found for 'x1')");let s=n.weights===void 0?void 0:Array.from(n.weights);if(s!==void 0&&s.length!==c)throw new RangeError("variable lengths differ (found for '(weights)')");let f=n.offset===void 0?void 0:Array.from(n.offset);if(f!==void 0&&f.length!==c)throw new RangeError("variable lengths differ (found for '(offset)')");let i=o.map((L,S)=>{let T=l[S];return!(Array.isArray(T)?T.some(W=>Number.isNaN(W)):Number.isNaN(T))&&L.every(W=>!Number.isNaN(W))&&!(s!==void 0&&Number.isNaN(s[S]))&&!(f!==void 0&&Number.isNaN(f[S]))}),u=o.filter((L,S)=>i[S]).map(L=>r?[1,...L]:L),m=l.filter((L,S)=>i[S]),h=m.length;if(h===0)throw new RangeError("0 (non-NA) cases");let d=s===void 0?new Array(h).fill(1):s.filter((L,S)=>i[S]);if(d.some(L=>L<0))throw new RangeError("negative weights not allowed");let g=f===void 0?new Array(h).fill(0):f.filter((L,S)=>i[S]),p=o.length>0?o[0].length:0,b=[...r?["(Intercept)"]:[],...Array.from({length:p},(L,S)=>`x${S+1}`)],v=b.length;if(u.some(L=>L.length!==v))throw new TypeError("all rows of x must have the same length");let w=typeof n.family=="object"?{...n.family}:{family:n.family??"gaussian"};n.link!==void 0&&(w.link=n.link);let N={x:u,columnNames:b,yGiven:m,priorGiven:d,offset:g,hasOffset:f!==void 0,intercept:r,familySpec:w,epsilon:n.epsilon??1e-8,maxit:n.maxit??25,start:n.start===void 0?void 0:Array.from(n.start)};if(!(N.epsilon>0))throw new RangeError("value of 'epsilon' must be > 0");if(!(N.maxit>0))throw new RangeError("maximum number of iterations must be > 0");let y=new Z,k=Array.from({length:v},(L,S)=>S),{fit:M,fam:x,y:A,w:E,size:I}=Me(N,k,y),q;if(r&&N.hasOffset){let L=new Z,S=zn({x:u.map(()=>[1]),p:1,y:A,w:E,offset:g,fam:x,epsilon:N.epsilon,maxit:N.maxit,columnNames:["(Intercept)"],mustart:M.mu},L);Dn(S,x,L,!1),L.list.forEach(T=>y.add(T)),q=S.deviance,S.converged||y.add("fitting to calculate the null deviance did not converge -- increase 'maxit'?")}else if(r){let L=0,S=0;for(let C=0;C<h;C++)L+=E[C],S+=E[C]*A[C];let T=S/L;q=de(x,A,A.map(()=>T),E)}else q=de(x,A,Je(y,()=>g.map(L=>x.linkinv(L))),E);let R=h-E.filter(L=>L===0).length,F=Pn(x,A,M.mu,E,I,M.deviance,y)+2*M.rank;return{coefficients:M.coefficients,fittedValues:M.mu,linearPredictors:M.eta,deviance:M.deviance,nullDeviance:q,aic:F,rank:M.rank,dfResidual:R-M.rank,dfNull:R-(r?1:0),iter:M.iter,converged:M.converged,boundary:M.boundary,y:A,priorWeights:E,weights:M.workingWeights,residuals:M.workingResiduals,family:x.family,link:x.name,warnings:y.list,qr:M.qr,model:N}}function tt(t){return Se(t.model.familySpec)}function xe(t,e){if(tt(t).fixedDispersion)return 1;if(t.dfResidual<=0)return NaN;let n=0;for(let r=0;r<t.weights.length;r++)t.weights[r]>0&&(n+=t.weights[r]*t.residuals[r]**2);return t.weights.some(r=>r===0)&&e?.add("observations with zero weight not used for calculating dispersion"),n/t.dfResidual}function vo(t){if(t.qr===void 0||t.rank===0)return[];let e=t.qr.qr.slice(0,t.rank).map(n=>n.slice(0,t.rank));return je(e)}function Vt(t,e={}){let n=new Z,r=tt(t),o=e.dispersion===void 0&&!r.fixedDispersion,a=e.dispersion??xe(t,n),l=vo(t),s=(t.qr===void 0?[]:t.qr.pivot.slice(0,t.rank)).map(h=>t.coefficients[h]),f=l.map((h,d)=>Math.sqrt(h[d]*a)),i=s.map((h,d)=>h/f[d]),u=!o,m=i.map(h=>u?2*z(-Math.abs(h)):t.dfResidual>0?2*B(-Math.abs(h),t.dfResidual,!0,!1):NaN);return o&&t.dfResidual===0&&(f.fill(NaN),i.fill(NaN)),{dispersion:a,estimate:s,stdError:f,statistic:i,pValue:m,statisticName:r.fixedDispersion?"z value":"t value",covUnscaled:l,covScaled:l.map(h=>h.map(d=>d*a)),dfResidual:t.dfResidual,warnings:n.list}}function Ct(t,e="deviance"){let n=tt(t),{y:r,fittedValues:o,priorWeights:a}=t;switch(e){case"response":return r.map((l,c)=>l-o[c]);case"working":return t.residuals.slice();case"pearson":return r.map((l,c)=>(l-o[c])*Math.sqrt(a[c])/Math.sqrt(n.variance(o[c])));case"deviance":return t.dfResidual<=0?r.map(()=>0):r.map((l,c)=>Math.sign(l-o[c])*Math.sqrt(Math.max(n.devContribution(l,o[c],a[c]),0)));default:throw new RangeError(`unknown type of residuals "${String(e)}"`)}}function Ot(t){let e=[],n=t.qr,r=[];if(n!==void 0&&t.rank>0){let l=n.qr.length,c=Array.from({length:l},(f,i)=>Array.from({length:t.rank},(u,m)=>i===m?1:0));r=sn(n,c).map(f=>f.reduce((i,u)=>i+u*u,0))}let o=0,a=t.weights.map(l=>l>0);return t.priorWeights.forEach((l,c)=>{let s=a[c]?r[o++]??0:0;l>0&&e.push(s>1-10*ye?1:s)}),e}function bo(t){let e=t.slice().sort((r,o)=>r-o),n=e.length>>1;return e.length%2===1?e[n]:(e[n-1]+e[n])/2}function yo(t,e={}){let n=Ot(t),r=e.dispersion??xe(t),o=Ct(t,"deviance").filter((l,c)=>t.priorWeights[c]>0),a=100*ye*bo(o.map(Math.abs));return o.map((l,c)=>{let s=(Math.abs(l)<a?0:l)/Math.sqrt(r*(1-n[c]));return Number.isFinite(s)?s:NaN})}function Mo(t,e={}){let n=Ot(t),r=e.dispersion??xe(t);return Ct(t,"pearson").filter((a,l)=>t.priorWeights[l]>0).map((a,l)=>{let c=(a/(1-n[l]))**2*(n[l]/(r*t.rank));return Number.isFinite(c)?c:NaN})}function xo(t,e={}){let n=tt(t),r,o;if(e.newdata===void 0)r=t.model.x,o=t.linearPredictors.slice();else{r=$n(e.newdata).map(u=>t.model.intercept?[1,...u]:u);let i=e.newoffset===void 0?r.map(()=>0):Array.from(e.newoffset);if(i.length!==r.length)throw new RangeError("newoffset must have one value per row of newdata");o=r.map((u,m)=>u.reduce((h,d,g)=>Number.isNaN(t.coefficients[g])?h:h+d*t.coefficients[g],0)+i[m])}let a=e.dispersion??xe(t),l=t.rank,c=t.qr===void 0?[]:t.qr.pivot.slice(0,l),s=t.qr?.qr,f=r.map(i=>{let u=new Array(l).fill(0),m=0;for(let h=0;h<l;h++){let d=i[c[h]];for(let g=0;g<h;g++)d-=s[g][h]*u[g];u[h]=d/s[h][h],m+=u[h]*u[h]}return Math.sqrt(m)*Math.sqrt(a)});return(e.type??"link")==="link"?{fit:o,seFit:f}:Je(new Z,()=>({fit:o.map(i=>n.linkinv(i)),seFit:f.map((i,u)=>i*Math.abs(n.muEta(o[u])))}))}var Wn={epsilon:1e-300,maxit:100,untilCoefficients:!0};function Pt(t,e,n,r,o=1e-11){let a=[];for(let g=0;g<e.length;g++)n[g]>0&&a.push(g);let l=0,c=0;for(let g of a)l+=n[g],c+=n[g]*e[g];let s=r?c/l:0,f=0;for(let g of a)f+=n[g]*(e[g]-s)**2;if((t.length>0?t[0].length:0)===0||a.length===0)return 0;let u=a.map(g=>Math.sqrt(n[g])),m=ve(a.map((g,p)=>t[g].map(b=>b*u[p])),{tol:o}),h=_e(m,a.map((g,p)=>e[g]*u[p])),d=0;for(let g of h)d+=g*g;return f-d}function Eo(t,e={}){let n=e.level??.95,r=e.test??"LRT";if(!(n>0&&n<1))throw new RangeError("'level' must be in (0, 1)");let o=t.coefficients.length,a=e.parm??Array.from({length:o},(k,M)=>M);for(let k of a)if(!Number.isInteger(k)||k<0||k>=o)throw new RangeError(`invalid coefficient index ${k}`);let l=t.model,c=t.coefficients.map((k,M)=>M).filter(k=>!Number.isNaN(t.coefficients[k])),s=new Z,f=Me(l,c,s,{...Wn,start:l.start===void 0?void 0:c.map(k=>l.start[k])}),i=new Array(o).fill(NaN);c.forEach((k,M)=>i[k]=f.fit.coefficients[M]);let u={...t,coefficients:i,weights:f.fit.workingWeights,residuals:f.fit.workingResiduals,qr:f.fit.qr,rank:f.fit.rank},m=Vt(u),h=m.dispersion,d=new Array(o).fill(NaN);(f.fit.qr===void 0?[]:f.fit.qr.pivot.slice(0,f.fit.rank)).forEach((k,M)=>d[c[k]]=m.stdError[M]);let p=l.x.map(k=>c.map(M=>k[M])),b=f.fit.eta,v=(k,M)=>{let x=c.filter(I=>I!==k),A=Me(l,x,new Z,{...Wn,start:void 0,etastart:b,extraOffset:l.x.map(I=>I[k]*M)}).fit,E=r==="LRT"?A.deviance-f.fit.deviance:Pt(p,A.workingResiduals,A.workingWeights,!1);return Math.sign(M-i[k])*Math.sqrt(Math.max(E/h,0))},w=[P((1-n)/2),P((1+n)/2)],N=a.map(()=>NaN),y=a.map(()=>NaN);return a.forEach((k,M)=>{if(!Number.isNaN(i[k]))for(let x of[0,1]){let A=x===0?-1:1,E=NaN,I=NaN;for(let C=0;C<=30;C++){let W=i[k]+A*2**C*d[k],_=v(k,W);if(Math.abs(_)>=Math.abs(w[x])){E=W,I=_;break}}if(Number.isNaN(E))continue;let q=C=>C===i[k]?-w[x]:v(k,C)-w[x],[R,F]=x===0?[E,i[k]]:[i[k],E],[L,S]=x===0?[I-w[x],-w[x]]:[-w[x],I-w[x]],T=U(q,R,F,{tol:1e-14*d[k],maxIter:500,fLower:L,fUpper:S});x===0?N[M]=T:y[M]=T}}),{lower:N,upper:y}}var ko=(t,e)=>G(t,e/2,2,!1,!1);function Ro(t,e,n){let{fit:r,w:o}=Me(t,e,n);return{dfRes:o.filter(l=>l>0).length-r.rank,dev:r.deviance,fit:r,x:t.x.map(l=>e.map(c=>l[c])),intercept:t.intercept}}var zt=(t,e)=>Pt(e.x,t.fit.workingResiduals,t.fit.workingWeights,e.intercept);function Ao(t,e={}){let n=Array.isArray(t)?t:[t];if(n.length===0)throw new RangeError("no fits to compare");let r=new Z,o=Array.isArray(t)&&n.length>1,a=n[0],l=Se(a.model.familySpec),c=l.fixedDispersion,s=e.test===void 0?e.dispersion!==void 0||c?"Chisq":"F":e.test,f=[],i,u,m=[],h;if(o){let M=n[0].model.x.length;if(n.some(x=>x.model.x.length!==M))throw new RangeError("models were not all fitted to the same size of dataset");if(n.forEach((x,A)=>f.push({name:String(A+1),dfRes:x.dfResidual,dev:x.deviance})),i=f.map((x,A)=>A===0?NaN:f[A-1].dfRes-x.dfRes),u=f.map((x,A)=>A===0?NaN:f[A-1].dev-x.dev),s==="Rao"){let x=n.map(A=>({dfRes:A.dfResidual,dev:A.deviance,fit:_n(A),x:A.model.x,intercept:A.model.intercept}));m=f.map((A,E)=>E===0?NaN:i[E]<0?-zt(x[E],x[E-1]):zt(x[E-1],x[E]))}h=n.reduce((x,A)=>A.dfResidual<x.dfResidual?A:x)}else{let M=a,x=M.model,A=x.x.length>0?x.x[0].length-(x.intercept?1:0):0,E=x.intercept?[0]:[],I=R=>[...E,...Array.from({length:R},(F,L)=>L+E.length)],q=[];for(let R=0;R<=A;R++)R===A?q.push({dfRes:M.dfResidual,dev:M.deviance,fit:_n(M),x:x.x,intercept:x.intercept}):R>0||s==="Rao"?q.push(Ro(x,I(R),r)):q.push(void 0);f.push({name:"NULL",dfRes:M.dfNull,dev:M.nullDeviance});for(let R=1;R<=A;R++)f.push({name:`x${R}`,dfRes:q[R].dfRes,dev:q[R].dev});i=f.map((R,F)=>F===0?NaN:f[F-1].dfRes-R.dfRes),u=f.map((R,F)=>F===0?NaN:Math.max(f[F-1].dev-R.dev,0)),s==="Rao"&&(m=f.map((R,F)=>F===0?NaN:zt(q[F-1],q[F]))),h=M}let d=new Z,g=e.dispersion??xe(h,d);d.list.forEach(M=>r.add(M));let p=c?1/0:h.dfResidual,b=o?["Resid. Df","Resid. Dev","Df","Deviance"]:["Df","Deviance","Resid. Df","Resid. Dev"],v=f.map((M,x)=>o?[M.dfRes,M.dev,i[x],u[x]]:[i[x],u[x],M.dfRes,M.dev]),w=[...b],N=M=>f.map((x,A)=>{if(A===0||i[A]===0)return NaN;let E=M[A]/g*Math.sign(i[A]);return E<0||Number.isNaN(E)?NaN:ko(E,Math.abs(i[A]))}),y=[];if(s==="Chisq"||s==="LRT")w.push("Pr(>Chi)"),y=[N(u)];else if(s==="Rao")w.push("Rao","Pr(>Chi)"),y=[m,N(m)];else if(s==="F"){if(c&&r.add(`using F test with a '${l.family}' family is inappropriate`),p!==0){let M=f.map((x,A)=>{let E=u[A]/i[A]/g;return A===0||i[A]===0||E<0?NaN:E});w.push("F","Pr(>F)"),y=[M,M.map((x,A)=>Number.isNaN(x)?NaN:Q(x,Math.abs(i[A]),p,!1,!1))]}}else if(s==="Cp"){let M=h.model.x.length;w.push("Cp"),y=[f.map(x=>x.dev+2*g*(M-x.dfRes))]}let k=v.map((M,x)=>[...M,...y.map(A=>A[x])]);return{colNames:w,rowNames:f.map(M=>M.name),table:k,warnings:r.list}}function _n(t){return{coefficients:t.coefficients,eta:t.linearPredictors,mu:t.fittedValues,deviance:t.deviance,iter:t.iter,converged:t.converged,boundary:t.boundary,rank:t.rank,qr:t.qr,used:t.weights.map(e=>e>0),workingWeights:t.weights,workingResiduals:t.residuals}}export{io as TukeyHSD,Xr as ansariTest,ao as aov,xr as bartlettTest,wt as binomTest,Jr as brunnerMunzelTest,Gn as chisqTest,fo as confint,Hn as corTest,Zn as fisherTest,Ar as flignerTest,kr as friedmanTest,No as glm,Ao as glmAnova,Eo as glmConfint,Mo as glmCooksDistance,Ot as glmHatvalues,xo as glmPredict,Ct as glmResiduals,yo as glmRstandard,Vt as glmSummary,Er as kruskalTest,no as ksTest,co as lm,jr as mantelhaenTest,br as mcnemarTest,qr as moodTest,$r as onewayTest,gt as pAdjust,so as pairwisePropTest,ro as pairwiseTTest,oo as pairwiseWilcoxTest,vr as poissonTest,uo as predict,Nt as propTest,Rr as quadeTest,Dr as shapiroTest,it as tTest,Mr as varTest,ht as wilcoxTest,lo as zTest};
1
+ /*! rStats.js 0.11.0 | MIT License | includes code ported from SciPy, Boost.Math, statsmodels, LAPACK and others: see THIRD_PARTY_NOTICES.md at https://github.com/numero-uno-jpn/rStats.js */
2
+ import{C as xt,D as it,H as Fo,I as _o,J as fr,N as Et,O as mr,T as kt,U as Co,Y as Vo,Z as qt,_ as Do,b as Lo,c as me,ca as yn,d as lr,da as $o,ea as Zt,fa as Po,ha as Mn,i as $e,ia as xn,j as Io,l as Oo,m as Kt,n as ot,s as ae,t as st,v as at,w as Qe,y as cr}from"./chunks/chunk-F5EDJFEO.js";import{A as Rn,B as mt,h as he,j as Zo,k as St,l as qn,r as An,v as be}from"./chunks/chunk-7GQMKH3Z.js";import{a as Rt,b as zo,c as Wo,d as Jt,e as Tt,f as En,g as Ke,h as en,i as Uo,j as Ho,k as hr,l as te,m as kn,n as Me,o as Ko,p as $a,r as Tn,s as tn}from"./chunks/chunk-AHHS6AC4.js";import{k as we,l as Pe,n as Nn,o as lt}from"./chunks/chunk-Y5CNQ266.js";import{a as ct,b as Go}from"./chunks/chunk-ALM3VHFP.js";import{b as Qo,c as ft,e as Sn}from"./chunks/chunk-VJ4BDBMU.js";import{b as ur}from"./chunks/chunk-EZAFN5CD.js";import"./chunks/chunk-5WDJPHYY.js";import{a as Yt,b as se,c as re,d as jo,e as ee,f as fe,g as Ie,h as qe,i as At}from"./chunks/chunk-Y5PYRAYP.js";import{a as Bo,d as Xo}from"./chunks/chunk-YSWGDOWK.js";import{b as Qt}from"./chunks/chunk-TOGXTCZO.js";import{f as ye,g as P,h as H,n as ir,p as To,s as So}from"./chunks/chunk-JWBO5WVN.js";import{b as rt,c as Mt,d as B,e as Ao,f as le,g as oe,h as Te,i as ge,j as Xt,k as Ro}from"./chunks/chunk-EZWBRCJL.js";function Lt(e,t,n={}){if(typeof e=="string")return Pa(e,t,n);let r;t!==void 0&&!ArrayBuffer.isView(t)&&!Array.isArray(t)?n=t:r=t;let o=te(n.alternative),a=n.mu??0,i=n.paired??!1,l=n.varEqual??!1,s=n.confLevel??.95;if(Number.isNaN(a))throw new RangeError("mu must be a number");if(kn(s),i&&r===void 0)throw new TypeError("y is required when paired is true");let c=Array.from(e),f=r===void 0?void 0:Array.from(r);if(i){let[M,x]=At(c,f);c=M.map((q,k)=>q-x[k]),f=void 0}else c=qe(c),f!==void 0&&(f=qe(f));let m,u,h,p,d,w,b,g=c.length;if(g<2)throw new Error("not enough 'x' observations");let v=se(c),N=re(c);if(f===void 0){if(u=g-1,h=Math.sqrt(N/g),h<10*Number.EPSILON*Math.abs(v))throw new Error("data are essentially constant");m=(v-a)/h,w=i?"Paired t-test":"One Sample t-test",p=v,d=i?"mean difference":"mean of x",b=i?"mean difference":"mean"}else{let M=f.length;if(M<1||!l&&M<2)throw new Error("not enough 'y' observations");if(l&&g+M<3)throw new Error("not enough observations");let x=se(f),q=re(f);if(w=`${l?"":"Welch"} Two Sample t-test`,p=[v,x],d=["mean of x","mean of y"],b="difference in means",l){u=g+M-2;let k=0;g>1&&(k+=(g-1)*N),M>1&&(k+=(M-1)*q),k/=u,h=Math.sqrt(k*(1/g+1/M))}else{let k=N/g,S=q/M;h=Math.sqrt(k+S),u=(h*h)**2/(k*k/(g-1)+S*S/(M-1))}if(h<10*Number.EPSILON*Math.max(Math.abs(v),Math.abs(x)))throw new Error("data are essentially constant");m=(v-x-a)/h}let y,E;if(o==="less")y=we(m,u,!0,!1),E=[-1/0,m+Pe(s,u,!0,!1)];else if(o==="greater")y=we(m,u,!1,!1),E=[m-Pe(s,u,!0,!1),1/0];else{y=2*we(-Math.abs(m),u,!0,!1);let M=1-s,x=Pe(1-M/2,u,!0,!1);E=[m-x,m+x]}return E=[a+E[0]*h,a+E[1]*h],{method:w,alternative:o,statistic:m,statisticName:"t",parameter:u,parameterName:"df",pValue:y,confInt:E,confLevel:s,estimate:p,estimateNames:d,nullValue:a,nullValueName:b,stdErr:h}}function Pa(e,t,n){if(n.paired!==void 0)throw new Error("cannot use 'paired' in formula method");let r=En(e,t,!0);if(r.kind==="one")return Lt(r.x,n);if(r.kind==="paired")return Lt(r.x,r.y,{...n,paired:!0});let[o,a]=r.levels;return{...Lt(r.x,r.y,n),estimateNames:[`mean in group ${o}`,`mean in group ${a}`],nullValueName:`difference in means between group ${o} and group ${a}`}}function _e(e){let t=[0,0],n=0;for(let r of e)t=B(t,[r,0]),n+=r;return Number.isFinite(n)?t[0]:n}function ja(e,t,n){return Array.isArray(t)?za(e,t,n??{}):Wa(e,t??{})}var Yo=(e,t)=>e<t?-1:e>t?1:0;function za(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=[],a=[];e.forEach((u,h)=>{let p=t[h];!r(u)&&!r(p)&&(o.push(u),a.push(p))});let i=[...new Set(o)].sort(Yo),l=[...new Set(a)].sort(Yo);if(i.length<2||l.length<2)throw new RangeError("'x' and 'y' must have at least 2 levels");let s=new Map(i.map((u,h)=>[u,h])),c=new Map(l.map((u,h)=>[u,h])),f=i.map(()=>l.map(()=>0));return o.forEach((u,h)=>f[s.get(u)][c.get(a[h])]++),{...rs(f,n),rowLevels:i,colLevels:l}}function Wa(e,t){if(e.length>0&&typeof e[0]!="number"){let r=e.map(a=>Array.from(a)),o=r[0].length;if(r.some(a=>a.length!==o))throw new TypeError("all rows of the matrix must have the same length");return r.length===1?pr(r[0],t):o===1?pr(r.map(a=>a[0]),t):rs(r,t)}return pr(Array.from(e),t)}function Jo(e){let t=e.B??2e3;if(!(t>=0&&t<2**31))throw new RangeError("'B' must be a non-negative number");return t}function es(e,t,n){let r=(1-64*Number.EPSILON)*t,o=0;for(let a=0;a<e.length;a++)e[a]>=r&&o++;return(1+o)/(n+1)}function ts(e,t){return Number.isNaN(e)?NaN:me(e,t/2,2,!1,!1)}function ns(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 pr(e,t){let n=e.length;if(e.some(m=>Number.isNaN(m)||m<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=_e(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(m=>m<0))throw new RangeError("probabilities must be non-negative");if(t.rescaleP??!1){let m=_e(o);o=o.map(u=>u/m)}else if(Math.abs(_e(o)-1)>Math.sqrt(Number.EPSILON))throw new RangeError("probabilities must sum to 1");let a=o.map(m=>r*m);ns(a);let i=o.map((m,u)=>a[u]*(1-m)),l=_e(e.map((m,u)=>(m-a[u])**2/a[u])),s="Chi-squared test for given probabilities",c=n-1,f;if(t.simulatePValue??!1){let m=Jo(t);s+=Jt(m),c=NaN,f=es(Ga(r,o,a,m),l,m)}else f=ts(l,c);return{method:s,statistic:l,statisticName:"X-squared",parameter:c,parameterName:"df",pValue:f,observed:e,expected:a,residuals:e.map((m,u)=>(m-a[u])/Math.sqrt(a[u])),stdres:e.map((m,u)=>(m-a[u])/Math.sqrt(i[u]))}}function Ga(e,t,n,r){let o=t.length,a=Po(o,{size:Math.trunc(r*e),replace:!0,prob:t}),i=Math.trunc(e);if(a.length===0)return new Float64Array(0);if(i===0)throw new RangeError("invalid 'nrow' value (too large or NA)");let l=Math.ceil(a.length/i),s=new Float64Array(l),c=new Float64Array(o);for(let f=0;f<l;f++){c.fill(0);for(let u=0;u<i;u++)c[a[(f*i+u)%a.length]]++;let m=0;for(let u=0;u<o;u++)m+=(c[u]-n[u])**2/n[u];s[f]=m}return s}function Ua(e,t,n,r){let o=e.length,a=t.length,i=e.reduce((f,m)=>f+m,0),l=Mn(i),s=new Int32Array(o*a),c=new Float64Array(Math.trunc(r));for(let f=0;f<c.length;f++){xn(e,t,l,s);let m=0;for(let u=0;u<a;u++)for(let h=0;h<o;h++){let p=n[h][u];m+=(s[h+u*o]-p)**2/p}c[f]=m}return c}function rs(e,t){let n=t.correct??!0,r=e.length,o=e[0].length,a=e.flat();if(a.some(g=>!(g>=0&&g<1/0)))throw new RangeError("all entries of 'x' must be non-negative and finite");let i=_e(a);if(!(i>0))throw new RangeError("at least one entry of 'x' must be positive");let l=e.map(g=>_e(g)),s=e[0].map((g,v)=>_e(e.map(N=>N[v]))),c=l.map(g=>s.map(v=>g*v/i));ns(c.flat());let f=e.map((g,v)=>g.map((N,y)=>N-c[v][y])),m="Pearson's Chi-squared test",u,h=(r-1)*(o-1),p,d=(t.simulatePValue??!1)&&l.every(g=>g>0)&&s.every(g=>g>0),w=!d&&n&&r===2&&o===2?Math.min(.5,...f.flat().map(Math.abs)):0;if(d){let g=Jo(t);m+=Jt(g),u=_e(f.flatMap((N,y)=>N.map((E,M)=>E*E/c[y][M]))),h=NaN;let v=Ua(l.map(N=>Math.trunc(N)),s.map(N=>Math.trunc(N)),c,g);p=es(v,u,g)}else w>0?(m+=" with Yates' continuity correction",u=_e(f.flatMap((g,v)=>g.map((N,y)=>(Math.abs(N)-w)**2/c[v][y])))):u=_e(f.flatMap((g,v)=>g.map((N,y)=>N*N/c[v][y])));let b=c.map((g,v)=>g.map((N,y)=>N*(1-l[v]/i)*(1-s[y]/i)));return{method:m,statistic:u,statisticName:"X-squared",parameter:h,parameterName:"df",pValue:p??ts(u,h),observed:e,expected:c,residuals:f.map((g,v)=>g.map((N,y)=>N/Math.sqrt(c[v][y]))),stdres:f.map((g,v)=>g.map((N,y)=>N/Math.sqrt(b[v][y])))}}var Ha=Math.pow(Number.EPSILON,.25);function xe(e,t,n,r={}){let o={tol:r.tol??Ha,maxiter:r.maxIter??1e3};return r.fLower!==void 0&&(o.fLower=r.fLower),r.fUpper!==void 0&&(o.fUpper=r.fUpper),ft(e,t,n,o).root}function Ba(e,t={}){let n=e.length,r=n===0?0:e[0].length;if(e.some(s=>s.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(s=>Array.from(s));if(o.flat().some(s=>Number.isNaN(s)||s<0))throw new RangeError("all entries of 'x' must be non-negative and finite");if(o.flat().some(s=>s===1/0))throw new RangeError("'x' has entries too large to be integer");let a=o.map(s=>s.map(Qt)),i="Fisher's Exact Test for Count Data";if(n===2&&r===2)return Za(a,t,i);let l=t.alternative??"two.sided";if(t.simulatePValue??!1){let s=t.B??2e3;if(!(s>=0&&s<2**31))throw new RangeError("'B' must be a non-negative number");return{method:i+Jt(s),alternative:l,pValue:Xa(a,s)}}return{method:i,alternative:l,pValue:Ya(a)}}function Xa(e,t){let n=e.filter(d=>d.some(w=>w>0)),r=e[0].map((d,w)=>w).filter(d=>e.some(w=>w[d]>0));if(n.length<=1)throw new RangeError("need 2 or more non-zero row marginals");if(r.length<=1)throw new RangeError("need 2 or more non-zero column marginals");let o=n.map(d=>r.map(w=>d[w])),a=o.map(d=>d.reduce((w,b)=>w+b,0)),i=o[0].map((d,w)=>o.reduce((b,g)=>b+g[w],0)),l=a.length,s=i.length,c=[0,0];for(let d of o.flat())c=B(c,[-Xo(d),0]);let f=c[0]/(1+64*Number.EPSILON),m=Mn(a.reduce((d,w)=>d+w,0)),u=new Int32Array(l*s),h=0,p=Math.trunc(t);for(let d=0;d<p;d++){xn(a,i,m,u);let w=0;for(let b=0;b<u.length;b++)w-=m[u[b]];w<=f&&h++}return Math.max(0,Math.min(1,(1+h)/(t+1)))}var Qa=1+1e-7,Ka=16;function dr(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=B(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,a=1;for(let i=this.lo;i<=this.hi;i++){let l=i-this.lo;l%Ka===0||!(a>0&&a<1/0)?(o=Et(i,t,n,r,!0),a=1,this.logCentral[l]=o):this.logCentral[l]=o+Math.log(a),a*=(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),a=-1/0;for(let l=0;l<n;l++){let s=this.logCentral[l]+(this.lo+l)*r;o[l]=s,s>a&&(a=s)}for(let l=0;l<n;l++){let s=o[l]-a;o[l]=s<-746?0:Math.exp(s)}let i=dr(o,0,n-1);for(let l=0;l<n;l++)o[l]!==0&&(o[l]=o[l]/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=B(r,[(this.lo+o)*n[o],0]));return r[0]}tail(t,n,r){if(n===1)return r?mr(t-1,this.m,this.n,this.k,!1,!1):mr(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),a=t-this.lo;return r?dr(o,a,o.length-1):dr(o,0,a)}};function gr(e,t){return t?xe(e,0,1):1/xe(n=>e(1/n),Number.EPSILON,1)}function Za(e,t,n){let r=te(t.alternative),o=t.or??1;if(!(o>=0))throw new RangeError("'or' must be a single number between 0 and Inf");let a=t.confLevel??.95;if(!(a>0&&a<1))throw new RangeError("confLevel must be a single number between 0 and 1");let[[i,l],[s,c]]=e,f=i,m=new wr(i+s,l+c,i+l),{lo:u,hi:h}=m,p;if(r==="less")p=m.tail(f,o,!1);else if(r==="greater")p=m.tail(f,o,!0);else if(o===0)p=f===u?1:0;else if(o===1/0)p=f===h?1:0;else{let N=m.density(o),y=N[f-u]*Qa,E=[0,0];for(let M of N)M<=y&&(E=B(E,[M,0]));p=E[0]}let d;if(f===u)d=0;else if(f===h)d=1/0;else{let N=m.mean(1);d=N===f?1:gr(y=>m.mean(y)-f,N>f)}let w={method:n,alternative:r,pValue:p,estimate:d,estimateNames:"odds ratio",nullValue:o,nullValueName:"odds ratio"};if(!(t.confInt??!0))return w;let b=N=>{if(f===h)return 1/0;let y=m.tail(f,1,!1);return y===N?1:gr(E=>m.tail(f,E,!1)-N,y<N)},g=N=>{if(f===u)return 0;let y=m.tail(f,1,!0);return y===N?1:gr(E=>m.tail(f,E,!0)-N,y>N)},v;if(r==="less")v=[0,b(1-a)];else if(r==="greater")v=[g(1-a),1/0];else{let N=(1-a)/2;v=[g(N),b(N)]}return{...w,confInt:v,confLevel:a}}var Ln=[[0,0]];function Ce(e){for(let t=Ln.length;t<=e;t++)Ln.push(B(Ln[t-1],Ro([t,0])));return Ln[e]}function In(e,t,n,r){let o=e+n,a=o-e,i=e-(o-a)+(n-a)+t+r,l=o+i;return[l,i-(l-o)]}var vr=class{scale=-1/0;hi=0;lo=0;add(t,n,r){if(t>this.scale){let s=Number.isFinite(this.scale)?Math.exp(this.scale-t):0;this.hi*=s,this.lo*=s,this.scale=t}let o=r*Math.exp(t-this.scale)*(1+n),a=this.hi+o,i=a-this.hi,l=this.hi-(a-i)+(o-i)+this.lo;this.hi=a+l,this.lo=l-(this.hi-a)}value(){return this.scale===-1/0?0:this.hi*Math.exp(this.scale)}};function Ya(e){let t=e.filter(v=>v.some(N=>N>0));if(t.length===0)throw new RangeError("all entries of the table are zero");let n=t[0].map((v,N)=>t.some(y=>y[N]>0));if(t=t.map(v=>v.filter((N,y)=>n[y])),t.length<2||t[0].length<2)return 1;t.length>t[0].length&&(t=t[0].map((v,N)=>t.map(y=>y[N])));let r=t.length,o=t.map(v=>v.reduce((N,y)=>N+y,0)),a=t[0].map((v,N)=>t.reduce((y,E)=>y+E[N],0)).sort((v,N)=>N-v),i=a.length,l=o.reduce((v,N)=>v+N,0),s=Ao(Ce(l));for(let v of o)s=B(s,Ce(v));for(let v of a)s=B(s,Ce(v));let c=[0,0];for(let v of t.flat())c=le(c,Ce(v));let f=B(c,[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,N)=>{let y=N.join(","),E=p[v].get(y);if(E!==void 0)return E;let M=B(s,Ce(N.reduce((A,R)=>A+R,0)));for(let A of N)M=le(M,Ce(A));for(let A=v;A<i;A++)M=le(M,Ce(a[A]));let x={R:N,KZ:M,max:0,min:0,edges:[],pastHi:[],pastLo:[],count:[],index:new Map};if(p[v].set(y,x),v===i-1){let A=[0,0];for(let R of N)A=le(A,Ce(R));return x.max=x.min=A[0],x.edges.push({to:x,hi:A[0],lo:A[1],mult:1}),x}let q=new Map,k=new Array(r+1).fill(0);for(let A=r-1;A>=0;A--)k[A]=k[A+1]+N[A];let S=new Array(r).fill(0),L=(A,R,V)=>{if(A===r-1){S[A]=R;let C=le(V,Ce(R)),z=N.map(($,D)=>$-S[D]).sort(($,D)=>$-D),J=d(v+1,z),T=`${z.join(",")}|${C[0]}`,O=q.get(T);O===void 0?q.set(T,{to:J,hi:C[0],lo:C[1],mult:1}):O.mult++;return}let F=Math.max(0,R-k[A+1]),I=Math.min(N[A],R);for(let C=F;C<=I;C++)S[A]=C,L(A+1,R-C,le(V,Ce(C)))};if(L(0,a[v],[0,0]),x.edges=[...q.values()],x.max=Math.max(...x.edges.map(A=>A.hi+A.to.max)),x.min=Math.min(...x.edges.map(A=>A.hi+A.to.min)),v===i-2){let A=x.edges.map(J=>{let T=J.to.edges[0];return{v:B([J.hi,J.lo],[T.hi,T.lo]),mult:J.mult}});A.sort((J,T)=>J.v[0]-T.v[0]||J.v[1]-T.v[1]);let R=A.length,V=A[R-1].v[0],F=new Float64Array(R),I=new Float64Array(R),C=new Float64Array(R),z=[0,0];A.forEach(({v:J,mult:T},O)=>{F[O]=J[0],I[O]=J[1];let $=le(J,[V,0]);z=B(z,[T*Math.exp($[0])*(1+$[1]),0]),C[O]=z[0]}),x.dist={hi:F,lo:I,cum:C,vmax:V,kv:B(s,[V,0])}}return x},w=d(0,[...o].sort((v,N)=>v-N)),b=new vr,g=(v,N,y,E)=>{if(N+v.max<=u-m){let[L,A]=In(v.KZ[0],v.KZ[1],N,y);return b.add(L,A,E),!0}if(N+v.min>u+m)return!0;let M=v.dist;if(M===void 0)return!1;let[x,q]=In(u,h,-N,-y),k=0,S=M.hi.length;for(;k<S;){let L=k+S>>>1,A=M.hi[L];A<x||A===x&&M.lo[L]<=q?k=L+1:S=L}if(k>0){let[L,A]=In(M.kv[0],M.kv[1],N,y);b.add(L,A,E*M.cum[k-1])}return!0};g(w,0,0,1)||(w.pastHi.push(0),w.pastLo.push(0),w.count.push(1));for(let v=0;v<i-2;v++)for(let N of p[v].values()){let{pastHi:y,pastLo:E,count:M}=N;if(M.length!==0){for(let{to:x,hi:q,lo:k,mult:S}of N.edges)for(let L=0;L<M.length;L++){let[A,R]=In(y[L],E[L],q,k),V=M[L]*S;if(g(x,A,R,V))continue;let F=x.index.get(A);F===void 0?(x.index.set(A,x.count.length),x.pastHi.push(A),x.pastLo.push(R),x.count.push(V)):x.count[F]+=V}N.pastHi=N.pastLo=N.count=[],N.index.clear()}}return Math.min(1,b.value())}function nn(e,t,n={}){if(typeof e=="string"){if(n.paired!==void 0)throw new Error("cannot use 'paired' in formula method");let d=En(e,t,!0);return d.kind==="one"?nn(d.x,n):nn(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=te(n.alternative),a=n.mu??0,i=n.paired??!1,l=n.exact??null,s=n.correct??!0,c=n.confInt??!1,f=n.confLevel??.95,m=n.tolRoot??1e-4;if(!Number.isFinite(a))throw new RangeError("mu must be a single finite number");if(c&&!(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,w]=At(Array.from(e),Array.from(r));u=qe(d.map((b,g)=>b-w[g]))}else u=qe(Array.from(e)),r!==void 0&&(h=qe(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:a,correct:s,confInt:c,confLevel:f,tolRoot:m};return h===void 0?ei(u,l,i,p):ri(u,h,l,p)}function On(e,t,n){return n?t==="two.sided"?Math.sign(e)*.5:t==="greater"?.5:-.5:0}function ss(e,t){return t==="less"?P(e):t==="greater"?P(e,{lowerTail:!1}):2*Math.min(P(e),P(e,{lowerTail:!1}))}function Ja(e){if(!(e>0)||!Number.isFinite(e))return e;let n=1-Math.floor(Math.log10(e)),r=n>=0?e*10**n:e/10**-n,o=Math.round(r);return o-r===.5&&o%2!==0&&(o-=1),n>=0?o/10**n:o*10**-n}function br(e){let t=e.length;if(t===0)return NaN;let n=Math.floor(t/2);return t%2===1?e[n]:se([e[n-1],e[n]])}var Nr=(e,t)=>e-t;function as(e){let t=e.length,n=fe(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 Ie(n))o+=i*i*i-i;let a=Math.sqrt(t*(t+1)*(2*t+1)/24-o/48);return[r,r-t*(t+1)/4,a]}function ei(e,t,n,r){let{alternative:o,mu:a,correct:i}=r,l=e.map(E=>E-a),s=l.some(E=>E===0);s&&(l=l.filter(E=>E!==0));let c=l.length,f=t??c<50,m=fe(l.map(Math.abs)),u=Ie(m).length>0,[h,p,d]=as(l),w,b,g=f&&!u&&!s;if(g){b="Wilcoxon signed rank exact test";let E=c*(c+1)/2;if(o==="less")w=kt(h,c,!0,!1);else if(o==="greater")w=kt(h-1,c,!1,!1);else{let M=h>E/2?kt(h-1,c,!1,!1):kt(h,c,!0,!1);w=Math.min(1,2*M)}}else b=`Wilcoxon signed rank test${i?" with continuity correction":""}`,w=ss((p-On(p,o,i))/d,o);let v={method:b,alternative:o,statistic:h,statisticName:"V",pValue:w,nullValue:a,nullValueName:n?"location shift":"location"};if(!r.confInt)return v;let N=l.map(E=>E+a),y=g?ti(N,r):ni(N,r);return{...v,...y}}function Fn(e,t,n){return e==="less"?[-1/0,n]:e==="greater"?[t,1/0]:[t,n]}function os(e,t,n){let r={confInt:Fn(e,NaN,NaN),confLevel:0,estimate:t};return n!==void 0&&(r.estimateNames=n),r}function is(e,t,n){let r=e(t==="two.sided"?n/2:n);return r===0?1:r}function ls(e){if(!(e>0))throw new RangeError("tolRoot must be positive")}function ti(e,t){let{alternative:n,confLevel:r}=t,o=e.length,a=[];for(let m=0;m<o;m++)for(let u=m;u<o;u++)a.push((e[m]+e[u])/2);a.sort(Nr);let i=a.length,l=1-r,s=is(m=>Co(m,o,!0,!1),n,l),c=kt(s-1,o,!0,!1),f=n==="two.sided"?2*c:c;return{confInt:Fn(n,a[s-1],a[i-s]),confLevel:f-l>l/2?1-Ja(f):r,estimate:br(a),estimateNames:"(pseudo)median"}}function ni(e,t){let{alternative:n,correct:r,confLevel:o,tolRoot:a}=t,i=e.length;if(i===0)return os(n,NaN);let l=Math.min(...e),s=Math.max(...e);if(!Number.isFinite(l)||!Number.isFinite(s)||i===1||l===s)return os(n,(l+s)/2,"midrange");let c=(v,N)=>{let[,y,E]=as(e.map(M=>M-v).filter(M=>M!==0));return(y-On(y,n,N))/E},f=c(l,r),m=c(s,r),u=v=>-H(v),h=v=>n==="greater"?!(f<u(v)):n==="less"?!(m>H(v/2)):!(f<u(v/2))&&!(m>H(v/2)),p=1-o,d=!1;for(;!h(p)&&p<1;)p*=2,d=!0;ls(a);let w=v=>xe(N=>c(N,r)-v,l,s,{tol:a,fLower:f-v,fUpper:m-v}),b;if(p>=1){let v=br([...e].sort(Nr));b=Fn(n,v,v)}else n==="greater"?b=[w(u(p)),1/0]:n==="less"?b=[-1/0,w(H(p))]:b=[w(u(p/2)),w(H(p/2))];let g=xe(v=>c(v,!1),l,s,{tol:a,fLower:c(l,!1),fUpper:c(s,!1)});return{confInt:b,confLevel:d?1-Math.min(1,p):o,estimate:g,estimateNames:"(pseudo)median"}}function cs(e,t){let n=e.length,r=t.length,o=fe([...e,...t]),a=0;for(let f=0;f<n;f++)a+=o[f];let i=a-n*(n+1)/2,l=0;for(let f of Ie(o))l+=f*f*f-f;let s=n+r,c=Math.sqrt(n*r/12*(s+1-l/(s*(s-1))));return[i,i-n*r/2,c]}function ri(e,t,n,r){let{alternative:o,mu:a,correct:i}=r,l=e.length,s=t.length,c=e.map(N=>N-a),f=n??(l<50&&s<50),m=Ie([...c,...t]).length>0,[u,h,p]=cs(c,t),d,w,b=f&&!m;if(b)if(w="Wilcoxon rank sum exact test",o==="less")d=qt(u,l,s,!0,!1);else if(o==="greater")d=qt(u-1,l,s,!1,!1);else{let N=u>l*s/2?qt(u-1,l,s,!1,!1):qt(u,l,s,!0,!1);d=Math.min(1,2*N)}else w=`Wilcoxon rank sum test${i?" with continuity correction":""}`,d=ss((h-On(h,o,i))/p,o);let g={method:w,alternative:o,statistic:u,statisticName:"W",pValue:d,nullValue:a,nullValueName:"location shift"};if(!r.confInt)return g;let v=b?oi(e,t,r):si(e,t,r);return{...g,...v}}function oi(e,t,n){let{alternative:r,confLevel:o}=n,a=e.length,i=t.length,l=[];for(let h of e)for(let p of t)l.push(h-p);l.sort(Nr);let s=l.length,c=1-o,f=is(h=>Do(h,a,i,!0,!1),r,c),m=qt(f-1,a,i,!0,!1),u=r==="two.sided"?2*m:m;return{confInt:Fn(r,l[f-1],l[s-f]),confLevel:u-c>c/2?1-u:o,estimate:br(l),estimateNames:"difference in location"}}function si(e,t,n){let{alternative:r,correct:o,confLevel:a,tolRoot:i}=n,l=Math.min(...e)-Math.max(...t),s=Math.max(...e)-Math.min(...t);if(!Number.isFinite(l)||!Number.isFinite(s))throw new Error("cannot compute the confidence interval because the differences are not finite");let c=(h,p)=>{let[,d,w]=cs(e.map(b=>b-h),t);return(d-On(d,r,p))/w},f=(h,p)=>{let d=c(l,p)-h,w=c(s,p)-h;if(Number.isNaN(d)||Number.isNaN(w))throw new Error("cannot compute the confidence interval because all observations are tied");return d<=0?l:w>=0?s:(ls(i),xe(b=>c(b,p)-h,l,s,{tol:i,fLower:d,fUpper:w}))},m=1-a,u;return r==="greater"?u=[f(-H(m),o),1/0]:r==="less"?u=[-1/0,f(H(m),o)]:u=[f(-H(m/2),o),f(H(m/2),o)],{confInt:u,confLevel:a,estimate:f(0,!1),estimateNames:"difference in location"}}var fs=["holm","hochberg","hommel","bonferroni","BH","BY","fdr","none"];function ai(e){if(fs.includes(e))return e;let t=fs.filter(n=>n.startsWith(e));if(e.length===0||t.length!==1)throw new RangeError(`invalid method "${e}"`);return t[0]}function It(e,t={}){let n=typeof t=="string"?{method:t}:t,r=ai(n.method??"holm"),o=Array.from(e),a=[];for(let m=0;m<o.length;m++)Number.isNaN(o[m])||a.push(m);let i=a.length,l=n.n??i;if(!(l>=i))throw new RangeError("n must be at least the number of non-NaN p-values");if(l<=1)return o;let s=a.map(m=>o[m]),c=ii(s,r,l),f=o.slice();return a.forEach((m,u)=>f[m]=c[u]),f}function _n(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 ii(e,t,n){let r=e.length,o=new Array(r);switch(t){case"none":return e.slice();case"bonferroni":return e.map(a=>Math.min(1,n*a));case"holm":{let a=_n(e,!1),i=-1/0;return a.forEach((l,s)=>{i=Math.max(i,(n-s)*e[l]),o[l]=Math.min(1,i)}),o}case"hochberg":{let a=_n(e,!0),i=1/0;return a.forEach((l,s)=>{let c=r-s;i=Math.min(i,(n-c+1)*e[l]),o[l]=Math.min(1,i)}),o}case"BH":case"fdr":case"BY":{let a=t==="BY"?ee(Array.from({length:Math.floor(n)},(s,c)=>1/(c+1))):1,i=_n(e,!0),l=1/0;return i.forEach((s,c)=>{let f=r-c;l=Math.min(l,a*n/f*e[s]),o[s]=Math.min(1,l)}),o}case"hommel":return li(e,n)}}function li(e,t){if(!Number.isInteger(t))throw new RangeError("n must be an integer for the hommel method");let n=e.length,r=[...e,...new Array(t-n).fill(1)],o=_n(r,!1),a=o.map(s=>r[s]),i=a.slice();for(let s=t;s>=2;s--){let c=1/0;for(let f=1;f<=s;f++)c=Math.min(c,s*a[t-s+f-1]/f);for(let f=t-s;f<t;f++)i[f]=Math.max(i[f],c);for(let f=0;f<t-s;f++)i[f]=Math.max(i[f],Math.min(s*a[f],c))}let l=new Array(n);return o.forEach((s,c)=>{s<n&&(l[s]=i[c])}),l}var ci=1+1e-7;function Cn(e){return Number.isFinite(e)&&e>=0&&Math.abs(e-Math.round(e))<=1e-7*Math.max(1,Math.abs(e))}function fi(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=xt(e,t,n,!1)*ci,a=0;if(e<r){for(let s=Math.ceil(r);s<=t;s++)xt(s,t,n,!1)<=o&&a++;let l=a===0?0:it(t-a,t,n,!1,!1);return Math.min(1,it(e,t,n,!0,!1)+l)}for(let l=0;l<=Math.floor(r);l++)xt(l,t,n,!1)<=o&&a++;let i=a===0?0:it(a-1,t,n,!0,!1);return Math.min(1,it(e-1,t,n,!1,!1)+i)}function yr(e,t,n={}){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),!Cn(e[0])||!Cn(e[1]))throw new RangeError("'x' must be nonnegative and integer");r=Math.round(e[0]),o=Math.round(e[0])+Math.round(e[1])}else{if(typeof t!="number")throw new TypeError("'n' is required when 'x' is a single number");if(!Cn(e))throw new RangeError("'x' must be nonnegative and integer");if(r=Math.round(e),!(Cn(t)&&Math.round(t)>=r&&t>0))throw new RangeError("'n' must be a positive integer >= 'x'");o=Math.round(t)}let a=n.p??.5;if(!(a>=0&&a<=1))throw new RangeError("'p' must be a single number between 0 and 1");let i=te(n.alternative),l=n.confLevel??.95;Me(l);let s;i==="less"?s=it(r,o,a,!0,!1):i==="greater"?s=it(r-1,o,a,!1,!1):s=fi(r,o,a);let c=u=>r===0?0:cr(u,r,o-r+1,!0,!1),f=u=>r===o?1:cr(1-u,r+1,o-r,!0,!1),m;if(i==="less")m=[0,f(1-l)];else if(i==="greater")m=[c(1-l),1];else{let u=(1-l)/2;m=[c(u),f(u)]}return{method:"Exact binomial test",alternative:i,statistic:r,statisticName:"number of successes",parameter:o,parameterName:"number of trials",pValue:s,confInt:m,confLevel:l,estimate:r/o,estimateNames:"probability of success",nullValue:a,nullValueName:"probability of success"}}var mi=1+1e-7;function ui(e){return Number.isFinite(e)&&e>=0&&Math.abs(e-Math.round(e))<=1e-7*Math.max(1,Math.abs(e))}function hi(e,t){if(t===0)return e===0?1:0;if(e===t)return 1;let n=Kt(e,t,!1)*mi,r=l=>Kt(l,t,!1)<=n,o=Math.floor(t);if(e<t){let l=Math.max(e+1,o),s=l;if(!r(l)){let c=l,f=1;for(s=l+1;!r(s);)c=s,f*=2,s=l+f;for(;s-c>1;){let m=c+Math.floor((s-c)/2);r(m)?s=m:c=m}}return Math.min(1,ot(e,t,!0,!1)+ot(s-1,t,!1,!1))}let a=Math.min(e-1,o),i;if(!r(0))i=-1;else if(r(a))i=a;else{i=0;let l=a;for(;l-i>1;){let s=i+Math.floor((l-i)/2);r(s)?i=s:l=s}}return Math.min(1,ot(i,t,!0,!1)+ot(e-1,t,!1,!1))}function pi(e,t={}){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(ui))throw new RangeError("'x' must be finite, nonnegative, and integer");let r=n.map(Math.round),o=r.length,a=t.T??1,i=typeof a=="number"?new Array(o).fill(a):Array.from(a);if(i.length!==o)throw new RangeError("'x' and 'T' have incompatible length");if(!i.every(b=>Number.isFinite(b)&&b>=0))throw new RangeError("'T' must be nonnegative");let l=t.r??1;if(!(Number.isFinite(l)&&l>=0))throw new RangeError("'r' must be a single positive number");let s=te(t.alternative),c=t.confLevel??.95;if(Me(c),o===2){let[b,g]=r,[v,N]=i,y=b+g,E=l*v/(l*v+N),M=yr([b,g],{p:E,alternative:s,confLevel:c}),x=q=>q/(1-q)*(N/v);return{method:"Comparison of Poisson rates",alternative:s,statistic:b,statisticName:"count1",parameter:y*l*v/(v+l*N),parameterName:"expected count1",pValue:M.pValue,confInt:[x(M.confInt[0]),x(M.confInt[1])],confLevel:c,estimate:b/v/(g/N),estimateNames:"rate ratio",nullValue:l,nullValueName:"rate ratio"}}let f=r[0],m=i[0],u=l*m,h;s==="less"?h=ot(f,u,!0,!1):s==="greater"?h=ot(f-1,u,!1,!1):h=hi(f,u);let p=b=>f===0?0:lr(b,f,1,!0,!1),d=b=>lr(1-b,f+1,1,!0,!1),w;if(s==="less")w=[0,d(1-c)];else if(s==="greater")w=[p(1-c),1/0];else{let b=(1-c)/2;w=[p(b),d(b)]}return{method:"Exact Poisson test",alternative:s,statistic:f,statisticName:"number of events",parameter:m,parameterName:"time base",pValue:h,confInt:[w[0]/m,w[1]/m],confLevel:c,estimate:f/m,estimateNames:"event rate",nullValue:l,nullValueName:"event rate"}}function Mr(e,t,n={}){let r,o,a=n;if(typeof e!="number"&&e.length>0&&Array.isArray(e[0])){let k=e;if(k.some(S=>S.length!==2))throw new RangeError("'x' must have 2 columns");r=k.map(S=>S[0]),o=k.map(S=>S[0]+S[1]),t!==void 0&&typeof t=="object"&&!Array.isArray(t)&&(a=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,l=[];for(let k=0;k<i;k++)!Number.isNaN(r[k])&&!Number.isNaN(o[k])&&l.push(k);let s=l.length;if(s<1)throw new RangeError("not enough data");if(r=l.map(k=>r[k]),o=l.map(k=>o[k]),o.some(k=>!(k>0)))throw new RangeError("elements of 'n' must be positive");if(r.some(k=>!(k>=0)))throw new RangeError("elements of 'x' must be nonnegative");if(r.some((k,S)=>k>o[S]))throw new RangeError("elements of 'x' must not be greater than those of 'n'");let c;if(a.p!==void 0){let k=typeof a.p=="number"?[a.p]:Array.from(a.p);if(k.length!==i)throw new RangeError("'p' must have the same length as 'x' and 'n'");if(c=l.map(S=>k[S]),c.some(S=>!(S>0&&S<1)))throw new RangeError("elements of 'p' must be in (0,1)")}let f=c!==void 0;s===1&&c===void 0&&(c=[.5]);let m=a.confLevel??.95;Me(m);let u=te(a.alternative);(s>2||s===2&&f)&&(u="two.sided");let h=r.map((k,S)=>k/o[S]),p=s===2?h[0]-h[1]:0,d=s===2?1/o[0]+1/o[1]:0,w=(a.correct??!0)&&s<=2?.5:0,b=!1;if(w>0&&s===1){let k=Math.abs(r[0]-o[0]*c[0]);k<=w&&([w,b]=[k,!0])}else if(w>0&&s===2&&!f){let k=Math.abs(p)/d;k<=w&&([w,b]=[k,!0])}let g=r.reduce((k,S)=>k+S,0)/o.reduce((k,S)=>k+S,0),v=0;if(!b)for(let k=0;k<s;k++){let S=c?.[k]??g,L=o[k]*S,A=o[k]*(1-S);v+=(Math.abs(r[k]-L)-w)**2/L,v+=(Math.abs(o[k]-r[k]-A)-w)**2/A}let N=c!==void 0?s:s-1,y;if(u==="two.sided")y=me(v,N/2,2,!1,!1);else{let k=Math.sign(s===1?h[0]-c[0]:p);y=P(k*Math.sqrt(v),{lowerTail:u==="less"})}let E;if(s===1||s===2&&!f){let k=H(u==="two.sided"?(1+m)/2:m);if(s===1){let S=o[0],L=k*k,A=(C,z)=>(C+L/(2*S)+z*k*Math.sqrt(C*(1-C)/S+L/(4*S*S)))/(1+L/S),R=h[0]+w/S,V=h[0]-w/S,F=Math.min(1,Math.max(0,R>=1?1:A(R,1))),I=Math.min(1,Math.max(0,V<=0?0:A(V,-1)));E=u==="two.sided"?[I,F]:u==="greater"?[I,1]:[0,F]}else{let S=Math.sqrt(h.reduce((V,F,I)=>V+F*(1-F)/o[I],0)),L=k*S+w*d,A=Math.max(-1,p-L),R=Math.min(1,p+L);E=u==="two.sided"?[A,R]:u==="greater"?[A,1]:[-1,R]}}let M=s===1?"p":l.map(k=>`prop ${k+1}`),q={method:`${s===1?"1-sample proportions test":`${s}-sample test for ${f?"given proportions":"equality of proportions"}`} ${w!==0?"with":"without"} continuity correction`,alternative:u,statistic:v,statisticName:"X-squared",parameter:N,parameterName:"df",pValue:y,estimate:s===1?h[0]:h,estimateNames:M};return c!==void 0&&(q.nullValue=s===1?c[0]:c,q.nullValueName=M),E!==void 0&&(q.confInt=E,q.confLevel=m),q}function di(e,t,n={}){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 c=r.length;if(c<2||r.some(f=>f.length!==c))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=gi(e,t)}let o=n.correct??!0,a=r.length,i,l=o&&a===2&&r[0][1]!==r[1][0];if(l){let c=r[0][1],f=r[1][0];i=(Math.abs(c-f)-1)**2/(c+f)}else{i=0;for(let c=0;c<a;c++)for(let f=c+1;f<a;f++){let m=r[c][f],u=r[f][c];i+=(m-u)**2/(m+u)}}let s=a*(a-1)/2;return{method:l?"McNemar's Chi-squared test with continuity correction":"McNemar's Chi-squared test",statistic:i,statisticName:"McNemar's chi-squared",parameter:s,parameterName:"df",pValue:me(i,s/2,2,!1,!1)}}function gi(e,t){if(e.length!==t.length)throw new RangeError("'x' and 'y' must have the same length");let n=[];for(let c=0;c<e.length;c++){let f=e[c],m=t[c];typeof f=="number"&&Number.isNaN(f)||typeof m=="number"&&Number.isNaN(m)||n.push([f,m])}let r=c=>Array.from(new Set(c)).sort((f,m)=>typeof f=="number"&&typeof m=="number"?f-m:String(f)<String(m)?-1:String(f)>String(m)?1:0),o=r(n.map(c=>c[0])),a=r(n.map(c=>c[1]));if(o.length<2||o.length!==a.length)throw new RangeError("'x' and 'y' must have the same number of levels (minimum 2)");let i=new Map(o.map((c,f)=>[c,f])),l=new Map(a.map((c,f)=>[c,f])),s=o.map(()=>new Array(o.length).fill(0));for(let[c,f]of n)s[i.get(c)][l.get(f)]++;return s}function ms(e,t,n={}){if(typeof e=="string"){let w=Ke(e,t);return ms(w.x,w.y,n)}let r=Array.from(e).filter(Number.isFinite),o=Array.from(t).filter(Number.isFinite),a=n.ratio??1;if(!(Number.isFinite(a)&&a>0))throw new RangeError("'ratio' must be a single positive number");let i=te(n.alternative),l=n.confLevel??.95;Me(l);let s=r.length-1,c=o.length-1;if(s<1)throw new Error("not enough 'x' observations");if(c<1)throw new Error("not enough 'y' observations");let f=re(r)/re(o),m=f/a,u=ae(m,s,c,!0,!1),h=ae(m,s,c,!1,!1),p,d;if(i==="less")p=u,d=[0,f/st(1-l,s,c,!0,!1)];else if(i==="greater")p=h,d=[f/st(l,s,c,!0,!1),1/0];else{p=Math.min(1,2*Math.min(u,h));let w=(1-l)/2;d=[f/st(1-w,s,c,!0,!1),f/st(w,s,c,!0,!1)]}return{method:"F test to compare two variances",alternative:i,statistic:m,statisticName:"F",parameter:[s,c],parameterName:["num df","denom df"],pValue:p,confInt:d,confLevel:l,estimate:f,estimateNames:"ratio of variances",nullValue:a,nullValueName:"ratio of variances"}}function us(e,t){if(typeof e=="string"){let u=en(e,t);return us(Uo(u.values,u.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=[],a=[];for(let u of n){let h=Array.from(u).filter(Number.isFinite);if(h.length<2)throw new RangeError("there must be at least 2 observations in each group");o.push(h.length-1),a.push(re(h))}let i=0,l=0,s=0,c=0;for(let u=0;u<r;u++)i+=o[u],l+=o[u]*a[u],s+=o[u]*Math.log(a[u]),c+=1/o[u];l/=i;let f=(i*Math.log(l)-s)/(1+(c-1/i)/(3*(r-1))),m=r-1;return{method:"Bartlett test of homogeneity of variances",statistic:f,statisticName:"Bartlett's K-squared",parameter:m,parameterName:"df",pValue:me(f,m/2,2,!1,!1)}}function ut(e,t,n="drop"){if(t===void 0){let i=e;if(i.length>0&&typeof i[0]=="number")throw new TypeError("'g' is required when 'x' is a single sample");let l=i.map(f=>Array.from(f).filter(m=>!Number.isNaN(m)));if(n==="error"&&l.some(f=>f.length===0))throw new RangeError("all groups must contain data");let s=[],c=[];return l.forEach((f,m)=>{f.length!==0&&(s.push(String(m+1)),c.push(f))}),{labels:s,groups:c}}let r=Array.from(e);if(r.length!==t.length)throw new RangeError("'x' and 'g' must have the same length");let o=new Map;for(let i=0;i<r.length;i++){let l=r[i],s=t[i];if(Number.isNaN(l)||typeof s=="number"&&Number.isNaN(s))continue;let c=o.get(s);c===void 0&&o.set(s,c=[]),c.push(l)}let a=Array.from(o.keys()).sort((i,l)=>typeof i=="number"&&typeof l=="number"?i-l:String(i)<String(l)?-1:String(i)>String(l)?1:0);return{labels:a.map(String),groups:a.map(i=>o.get(i))}}function ht(e,t,n="drop"){return ut(e,t,n).groups}function Vn(e){let t=[...e].sort((r,o)=>r-o),n=t.length;return n%2===1?t[(n-1)/2]:(t[n/2-1]+t[n/2])/2}function xr(e){if(e.length===0)throw new RangeError("'y' must have at least one row");let t=e[0].length;if(e.some(n=>n.length!==t))throw new RangeError("all rows of 'y' must have the same length");return e.filter(n=>n.every(r=>!Number.isNaN(r))).map(n=>Array.from(n))}function Er(e,t,n,r){return{method:e,statistic:n,statisticName:t,parameter:r,parameterName:"df",pValue:me(n,r/2,2,!1,!1)}}function hs(e){let t=0;for(let n of Ie(e))t+=n*n*n-n;return t}function Dn(e,t){if(typeof e=="string"){let c=en(e,t);return Dn(c.values,c.group.codes)}let n=ht(e,t,"error"),r=n.length;if(r<2)throw new RangeError("all observations are in the same group");let o=n.flat(),a=o.length,i=fe(o),l=0,s=0;for(let c of n){let f=0;for(let m=0;m<c.length;m++)f+=i[s+m];s+=c.length,l+=f*f/c.length}return l=(12*l/(a*(a+1))-3*(a+1))/(1-hs(o)/(a*a*a-a)),Er("Kruskal-Wallis rank sum test","Kruskal-Wallis chi-squared",l,r-1)}function kr(e,t){if(typeof e=="string")return kr(hr(e,t));let n=xr(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 a=new Array(o).fill(0),i=0;for(let c of n){let f=fe(c);for(let m=0;m<o;m++)a[m]+=f[m];i+=hs(c)}let l=0;for(let c of a)l+=(c-r*(o+1)/2)**2;let s=12*l/(r*o*(o+1)-i/(o-1));return Er("Friedman rank sum test","Friedman chi-squared",s,o-1)}function wi(e){return{...kr(e),method:"Cochran's Q test",statisticName:"Q"}}function ps(e,t){if(typeof e=="string")return ps(hr(e,t));let n=xr(e),r=e.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 a=fe(n.map(p=>Math.max(...p)-Math.min(...p))),i=new Array(o).fill(0),l=0;n.forEach((p,d)=>{let w=fe(p);for(let b=0;b<o;b++){let g=a[d]*(w[b]-(o+1)/2);l+=g*g,i[b]+=g}});let s=0;for(let p of i)s+=p*p;s/=r;let c=o-1,f=(r-1)*(o-1),m,u,h=[c,f];if(l===s){m=NaN,h=[NaN,NaN];let p=0;for(let d=2;d<=o;d++)p+=Math.log(d);u=Math.exp((1-r)*p)}else m=(r-1)*s/(l-s),u=ae(m,c,f,!1,!1);return{method:"Quade test",statistic:m,statisticName:"Quade F",parameter:h,parameterName:["num df","denom df"],pValue:u}}function ds(e,t){if(typeof e=="string"){let u=en(e,t);return ds(u.values,u.group.codes)}let n=ht(e,t,"error"),r=n.length;if(r<2)throw new RangeError("all observations are in the same group");let o=n.flatMap(u=>{let h=Vn(u);return u.map(p=>Math.abs(p-h))}),a=o.length,l=fe(o).map(u=>H((1+u/(a+1))/2)),s=0;for(let u of l)s+=u;let c=s/a,f=0,m=0;for(let u of n){let h=0;for(let p=0;p<u.length;p++)h+=l[m+p];m+=u.length,f+=u.length*(h/u.length-c)**2}return f/=re(l),Er("Fligner-Killeen test of homogeneity of variances","Fligner-Killeen:med chi-squared",f,r-1)}function gs(e,t,n={}){if(typeof e=="string"){let N=Ke(e,t);return gs(N.x,N.y,n)}let r=Array.from(e).filter(Number.isFinite),o=Array.from(t).filter(Number.isFinite),a=te(n.alternative),i=r.length,l=o.length,s=i+l;if(s<3)throw new RangeError("not enough observations");let c=r.concat(o),f=Array.from({length:s},(N,y)=>y).sort((N,y)=>c[N]-c[y]),m=new Array(s),u=0;for(;u<s;){let N=u;for(;N+1<s&&c[f[N+1]]===c[f[u]];)N++;let y=0;for(let E=u;E<=N;E++)y+=(E+1-(s+1)/2)**2;y/=N-u+1;for(let E=u;E<=N;E++)m[f[E]]=y;u=N+1}let h=0;for(let N=0;N<i;N++)h+=m[N];let p=0;for(let N of m)p+=N;p/=s;let d=0;for(let N of m)d+=(N-p)**2;let w=i*p,b=i*l/(s*(s-1))*d,g=(h-w)/Math.sqrt(b),v=a==="less"?P(g):a==="greater"?P(g,{lowerTail:!1}):2*P(-Math.abs(g));return{method:"Mood two-sample test of scale",alternative:a,statistic:g,statisticName:"Z",pValue:v}}function pt(e,t){let n=0;for(let r=e.length-1;r>=0;r--)n=n*t+e[r];return n}var vi=[0,.221157,-.147981,-2.07119,4.434685,-2.706056],bi=[0,.042981,-.293762,-1.752461,5.682633,-3.582633],Ni=[.544,-.39978,.025054,-6714e-7],yi=[1.3822,-.77857,.062767,-.0020322],Mi=[-1.5861,-.31082,-.083751,.0038915],xi=[-.4803,-.082676,.0030302],Ei=[-2.273,.459];function ki(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]=-H((m+1-.375)/(e+.25)),o+=r[m]*r[m];o*=2;let a=Math.sqrt(o),i=1/Math.sqrt(e),l=r[0]/a+pt(vi,i),s=new Float64Array(n);s[0]=l;let c,f;if(e>5){let m=r[1]/a+pt(bi,i);s[1]=m,c=2,f=Math.sqrt((o-2*r[0]*r[0]-2*r[1]*r[1])/(1-2*l*l-2*m*m))}else c=1,f=Math.sqrt((o-2*r[0]*r[0])/(1-2*l*l));for(let m=c;m<n;m++)s[m]=r[m]/f;for(let m=0;m<n;m++)t[e-1-m]=s[m],t[m]=-s[m];return t}function ws(e){let t=e.length,n=ki(t),r=e[t-1]-e[0],o=0;for(let c of e)o+=c/r;o/=t;let a=0,i=0,l=0;for(let c=0;c<t;c++){let f=e[c]/r-o;a+=n[c]*n[c],i+=f*f,l+=n[c]*f}let s=Math.sqrt(a*i);return(s-l)*(s+l)/(a*i)}var qi=1.90985931710274,Ai=1.0471975511966;function vs(e,t){if(Number.isNaN(e)||!(t>=3))return NaN;if(t===3){let i=qi*(Math.asin(Math.sqrt(1-e))-Ai);return Math.max(i,0)}let n=Math.log(e);if(t<=11){let i=pt(Ei,t);if(n>=i)return 1e-99;let l=pt(Ni,t),s=Math.exp(pt(yi,t));return P(-Math.log(i-n),{mean:l,sd:s,lowerTail:!1})}let r=Math.log(t),o=pt(Mi,r),a=Math.exp(pt(xi,r));return P(n,{mean:o,sd:a,lowerTail:!1})}function bs(e,t,n={}){if(typeof e=="string"){let h=Ho(e,t);return bs(h.values,h.groups,n)}let r;t!==void 0&&!Array.isArray(t)?n=t:r=t;let o=ht(e,r),a=n.varEqual??!1,i=o.length;if(i<2)throw new RangeError("not enough groups");let l=o.map(h=>h.length);if(l.some(h=>h<2))throw new RangeError("not enough observations");let s=o.map(se),c=o.map(re),f=l.reduce((h,p)=>h+p,0),m,u;if(a){let h=se(o.flat()),p=0,d=0;for(let w=0;w<i;w++)p+=l[w]*(s[w]-h)**2,d+=(l[w]-1)*c[w];u=f-i,m=p/(i-1)/(d/u)}else{let h=l.map((g,v)=>g/c[v]),p=h.reduce((g,v)=>g+v,0),d=0,w=0;for(let g=0;g<i;g++)d+=(1-h[g]/p)**2/(l[g]-1),w+=h[g]*s[g];d/=i*i-1,w/=p;let b=0;for(let g=0;g<i;g++)b+=h[g]*(s[g]-w)**2;m=b/((i-1)*(1+2*(i-2)*d)),u=1/(3*d)}return{method:a?"One-way analysis of means":"One-way analysis of means (not assuming equal variances)",statistic:m,statisticName:"F",parameter:[i-1,u],parameterName:["num df","denom df"],pValue:ae(m,i-1,u,!1,!1)}}function Ri(e){let t=Array.from(e).filter(o=>!Number.isNaN(o)).sort((o,a)=>o-a),n=t.length;if(n<3||n>5e3)throw new RangeError("sample size must be between 3 and 5000");if(!Number.isFinite(t[0])||!Number.isFinite(t[n-1]))throw new RangeError("'x' must be finite");if(t[n-1]===t[0])throw new RangeError("all 'x' values are identical");let r=ws(t);return{method:"Shapiro-Wilk normality test",statistic:1-r,statisticName:"W",pValue:vs(r,n)}}function qr(e,t,n){let r=[0,0];for(let o=Math.max(0,t);o<Math.min(n,e.length);o++)r=B(r,[e[o],0]);return r[0]}function Ti([[e,t],[n,r]]){let o=e+n,a=t+r,i=e+t,l=Math.max(0,i-a),s=Math.min(i,o),c=new Float64Array(s-l+1);for(let f=l;f<=s;f++)c[f-l]=Et(f,o,a,i,!1);return{lo:l,d:c}}function Si(e,t){let n=new Float64Array(e.length+t.length-1);for(let r=0;r<e.length;r++){let o=e[r];if(o!==0)for(let a=0;a<t.length;a++)n[r+a]+=o*t[a]}return n}var $n=class{lo;hi;logNull;constructor(t){let n=0,r=new Float64Array([1]);for(let o of t){let a=Ti(o);n+=a.lo,r=Si(r,a.d)}this.lo=n,this.hi=n+r.length-1,this.logNull=r.map(Math.log)}density(t){let n=this.logNull.length,r=new Float64Array(n);if(t===0||t===1/0)return r[t===0?0:n-1]=1,r;let o=Math.log(t),a=-1/0;for(let l=0;l<n;l++){let s=this.logNull[l]+(this.lo+l)*o;r[l]=s,s>a&&(a=s)}for(let l=0;l<n;l++)r[l]=Math.exp(r[l]-a);let i=qr(r,0,n);for(let l=0;l<n;l++)r[l]=r[l]/i;return r}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++)r=B(r,[(this.lo+o)*n[o],0]);return r[0]}cdf(t,n,r){let o=this.density(n),a=t-this.lo;return r?qr(o,a,o.length):qr(o,0,a+1)}};function Ar(e,t){return t?xe(e,0,1):1/xe(n=>e(1/n),Number.EPSILON,1)}function Ns(e,t){if(t===e.lo)return 0;if(t===e.hi)return 1/0;let n=e.mean(1);return n===t?1:Ar(r=>e.mean(r)-t,n>t)}function Rr(e,t,n){if(t===e.hi)return 1/0;let r=e.cdf(t,1,!1);return r===n?1:Ar(o=>n-e.cdf(t,o,!1),r<n)}function Tr(e,t,n){if(t===e.lo)return 0;let r=e.cdf(t,1,!0);return r===n?1:Ar(o=>e.cdf(t,o,!0)-n,r>n)}function Li(e,t={}){let n=e.length;if(n<1)throw new RangeError("'x' must have at least one stratum");let r=e[0].length,o=e[0][0]?.length??0;if(r<2||o<2||n<2)throw new RangeError("each dimension in table must be >= 2");for(let f of e){if(f.length!==r||f.some(m=>m.length!==o))throw new RangeError("all strata must have the same dimensions");if(f.some(m=>m.some(u=>!Number.isFinite(u)||u<0)))throw new RangeError("all entries of 'x' must be nonnegative and finite")}let a=e.map(f=>f.reduce((m,u)=>m+u.reduce((h,p)=>h+p,0),0));if(a.some(f=>f<2))throw new RangeError("sample size in each stratum must be > 1");let i=te(t.alternative),l=t.correct??!0,s=t.exact??!1,c=t.confLevel??.95;if(Me(c),r===2&&o===2){let f=e.map(m=>[[m[0][0],m[0][1]],[m[1][0],m[1][1]]]);return s?Oi(f,i,c):Ii(f,a,i,l,c)}return Fi(e,a,r,o)}function Ii(e,t,n,r,o){let a=0,i=0,l=0,s=0,c=0,f=0,m=0,u=0;e.forEach(([[N,y],[E,M]],x)=>{let q=t[x],k=N+y,S=E+M,L=N+E,A=y+M;a+=N,i+=k*L/q,l+=k*S*L*A/(q*q*(q-1));let R=(N+M)/q,V=(y+E)/q,F=N*M/q,I=y*E/q;s+=F,c+=I,f+=R*F,m+=R*I+V*F,u+=V*I});let h=a-i,p=r&&Math.abs(h)>=.5?.5:0,d=(Math.abs(h)-p)**2/l,w=n==="two.sided"?me(d,.5,2,!1,!1):P(Math.sign(h)*Math.sqrt(d),{lowerTail:n==="less"}),b=s/c,g=Math.sqrt(f/(2*s*s)+m/(2*s*c)+u/(2*c*c)),v;if(n==="less")v=[0,b*Math.exp(H(o)*g)];else if(n==="greater")v=[b*Math.exp(H(o,{lowerTail:!1})*g),1/0];else{let N=H((1+o)/2);v=[b*Math.exp(-N*g),b*Math.exp(N*g)]}return{method:`Mantel-Haenszel chi-squared test ${p!==0?"with":"without"} continuity correction`,alternative:n,statistic:d,statisticName:"Mantel-Haenszel X-squared",parameter:1,parameterName:"df",pValue:w,confInt:v,confLevel:o,estimate:b,estimateNames:"common odds ratio",nullValue:1,nullValueName:"common odds ratio"}}function Oi(e,t,n){let r=new $n(e),o=e.reduce((l,s)=>l+s[0][0],0),a;if(t==="two.sided"){let l=r.density(1),s=l[o-r.lo]*(1+1e-7),c=[0,0];for(let f of l)f<=s&&(c=B(c,[f,0]));a=Math.min(1,c[0])}else a=r.cdf(o,1,t==="greater");let i;if(t==="less")i=[0,Rr(r,o,1-n)];else if(t==="greater")i=[Tr(r,o,1-n),1/0];else{let l=(1-n)/2;i=[Tr(r,o,l),Rr(r,o,l)]}return{method:"Exact conditional test of independence in 2 x 2 x k tables",alternative:t,statistic:o,statisticName:"S",pValue:a,confInt:i,confLevel:n,estimate:Ns(r,o),estimateNames:"common odds ratio",nullValue:1,nullValueName:"common odds ratio"}}function Fi(e,t,n,r){let o=(n-1)*(r-1),a=new Array(o).fill(0),i=Array.from({length:o},()=>new Array(o).fill(0));e.forEach((c,f)=>{let m=t[f],u=c.map(d=>d.reduce((w,b)=>w+b,0)),h=Array.from({length:r},(d,w)=>c.reduce((b,g)=>b+g[w],0)),p=1/(m*m*(m-1));for(let d=0;d<r-1;d++)for(let w=0;w<n-1;w++){let b=w+(n-1)*d;a[b]+=c[w][d]-u[w]*h[d]/m;for(let g=0;g<r-1;g++)for(let v=0;v<n-1;v++){let N=(w===v?m*u[w]:0)-u[w]*u[v],y=(d===g?m*h[d]:0)-h[d]*h[g];i[b][v+(n-1)*g]+=p*N*y}}});let l=be(i,a),s=0;for(let c=0;c<o;c++)s+=a[c]*l[c];return{method:"Cochran-Mantel-Haenszel test",statistic:s,statisticName:"Cochran-Mantel-Haenszel M^2",parameter:o,parameterName:"df",pValue:me(s,o/2,2,!1,!1)}}var ys=new Map;function _i(e,t){let n=`${e},${t}`,r=ys.get(n);if(r!==void 0)return r;let o=e+t,a=[];for(let u=1;u<=o;u++)a.push(Math.min(u,o+1-u));let i=a.reduce((u,h)=>u+h,0),l=Array.from({length:e+1},()=>new Float64Array(i+1));l[0][0]=1;for(let u=0;u<o;u++){let h=o-u,p=a[u],d=Array.from({length:e+1},()=>new Float64Array(i+1));for(let w=0;w<=e;w++){let b=l[w],g=e-w;if(g>h)continue;let v=g/h;for(let N=0;N<=i;N++){let y=b[N];y!==0&&(g>0&&(d[w+1][N+p]+=y*v),g<h&&(d[w][N]+=y*(1-v)))}}l=d}let s=l[e],c=0;for(;c<s.length&&s[c]===0;)c++;let f=s.length-1;for(;f>c&&s[f]===0;)f--;let m={min:c,probs:s.slice(c,f+1)};return ys.set(n,m),m}function Sr(e,t,n,r=!0){if(Number.isNaN(e)||!(t>=1)||!(n>=1)||t!==Math.floor(t)||n!==Math.floor(n))return NaN;let{min:o,probs:a}=_i(t,n),i=Math.floor(e+1e-7)-o,l=0;if(r)for(let s=0;s<=Math.min(i,a.length-1);s++)l+=a[s];else for(let s=Math.max(i+1,0);s<a.length;s++)l+=a[s];return Math.min(l,1)}var Ms=new Map;function Ci(e,t){let n=`${e},${t}`,r=Ms.get(n);if(r!==void 0)return r;let o=e+t,a=0;for(let m=1;m<=o;m++)a+=Math.min(m,o+1-m);let i=Array.from({length:e+1},()=>new Float64Array(a+1));i[0][0]=1;for(let m=0;m<o;m++){let u=Math.min(m+1,o-m),h=Array.from({length:e+1},()=>new Float64Array(a+1));for(let p=0;p<=e;p++){if(e-p>o-m)continue;let d=i[p];for(let w=0;w<=a;w++){let b=d[w];b!==0&&(p<e&&(h[p+1][w+u]+=b),e-p<o-m&&(h[p][w]+=b))}}i=h}let l=i[e],s=0;for(;l[s]===0;)s++;let c=l.length-1;for(;l[c]===0;)c--;let f={min:s,counts:l.slice(s,c+1)};return Ms.set(n,f),f}function Pn(e,t,n){let{min:r,counts:o}=Ci(t,n);if(e<=0)return r;if(e>=1)return r+o.length-1;let a=Bo(t+n,t),i=0;for(let l=0;l<o.length;l++)if(i+=o[l]/a,i>=e)return r+l;return r+o.length-1}function xs(e,t,n={}){if(typeof e=="string"){let v=Ke(e,t);return xs(v.x,v.y,n)}let r=Array.from(e).filter(v=>!Number.isNaN(v)),o=Array.from(t).filter(v=>!Number.isNaN(v)),a=r.length,i=o.length;if(a<1)throw new RangeError("not enough 'x' observations");if(i<1)throw new RangeError("not enough 'y' observations");let l=te(n.alternative),s=n.confInt??!1,c=n.confLevel??.95;s&&Me(c);let f=a+i,m=fe(r.concat(o)),u=m.map(v=>Math.min(v,f-v+1)),h=0;for(let v=0;v<a;v++)h+=u[v];let p=new Set(m).size<f,d=n.exact??(a<50&&i<50&&!p),w,b;if(d&&!p){let v=y=>Sr(y-1,a,i,!1),N=y=>Sr(y,a,i,!0);if(l==="two.sided"){let y=Math.floor((a+1)**2/4)+Math.floor(a*i/2)/2;w=Math.min(1,2*(h>y?v(h):N(h)))}else l==="less"?w=v(h):w=N(h);s&&(b=Di(r,o,l,c))}else{let v=0;for(let x of u)v+=x*x;let N=f%2===0,y=N?a*(f+2)/4:a*(f+1)**2/(4*f),E=N?a*i*(16*v-f*(f+2)**2)/(16*f*(f-1)):a*i*(16*f*v-(f+1)**4)/(16*f*f*(f-1)),M=(h-y)/Math.sqrt(E);w=l==="two.sided"?2*Math.min(P(M),P(M,{lowerTail:!1})):l==="less"?P(M,{lowerTail:!1}):P(M),s&&(b=$i(r,o,l,c))}let g={method:"Ansari-Bradley test",alternative:l,statistic:h,statisticName:"AB",pValue:w};return b!==void 0&&(g.confInt=b.confInt,g.confLevel=c,g.estimate=b.estimate,g.estimateNames="ratio of scales"),g.nullValue=1,g.nullValueName="ratio of scales",g}function Es(e,t,n){let r=e.length,o=r+t.length,a=Vi(e.map(s=>s/n).concat(t)),i=a.map(s=>Math.min(s,o-s+1)),l=0;for(let s=0;s<r;s++)l+=i[s];return{ab:l,a:i,ties:new Set(a).size<o}}function Vi(e){let t=e.filter(a=>!Number.isNaN(a));if(t.length===e.length)return fe(e);let n=fe(t),r=0,o=t.length;return e.map(a=>Number.isNaN(a)?++o:n[r++])}function Lr(e,t,n){return e==="two.sided"?n(t):e==="greater"?[n(2*t)[0],1/0]:[0,n(2*t)[1]]}function Di(e,t,n,r){let o=e.length,a=t.length,i=[];for(let w of e)for(let b of t)w/b>=0&&i.push(w/b);i.sort((w,b)=>w-b);let l=i.length;if(l===0)return{confInt:Lr(n,1-r,()=>[NaN,NaN]),estimate:NaN};let s=i.map((w,b)=>Es(e,t,w+(b<l-1?(i[b+1]-w)/2:w*1.01)).ab),c=(w,b,g)=>{let v=1/0;for(let y of s)(b?y-w>0:y-w>=0)&&y-w<v&&(v=y-w);let N=g?-1/0:1/0;return s.forEach((y,E)=>{y-w===v&&(N=g?Math.max(N,i[E]):Math.min(N,i[E]))}),N},f=w=>{let b=Pn(w/2,o,a),g=Pn(1-w/2,o,a);if(!s.some(y=>y-b>=0)||!s.some(y=>y-g>0))return[NaN,NaN];let v=c(b,!1,!1),N=c(g,!0,!0);return v>N?[c(g,!1,!1),c(b,!0,!0)]:[v,N]},m=Lr(n,1-r,f),u=Pn(.5,o,a),h=-1/0,p=1/0;s.forEach((w,b)=>{w-u<=0?h=Math.max(h,i[b]):p=Math.min(p,i[b])});let d=h===-1/0||p===1/0?NaN:(p+h)/2;return{confInt:m,estimate:d}}function $i(e,t,n,r){let o=e.length,a=t.length,i=o+a,l=i%2===0,s=(b,g)=>{let{ab:v,a:N,ties:y}=Es(e,t,b),E=l?v-o*(i+2)/4:v-o*(i+1)**2/(4*i),M;if(!y)M=Math.sqrt(l?o*a*(i+2)*(i-2)/(48*(i-1)):o*a*(i+1)*(3+i*i)/(48*i*i));else{let x=0;for(let q of N)x+=q*q;M=Math.sqrt(l?o*a*(16*x-i*(i+2)**2)/(16*i*(i-1)):o*a*(16*i*x-(i+1)**4)/(16*i*i*(i-1)))}return E/M-g},c=[],f=(b,g)=>{b.length===0||g.length===0||c.push(Math.min(...b)/Math.max(...g),Math.max(...b)/Math.min(...g))};if(f(e.filter(b=>b>0),t.filter(b=>b>0)),f(e.filter(b=>b<=0),t.filter(b=>b<0)),c.some(b=>!Number.isFinite(b)))return;let m=Math.min(...c),u=Math.max(...c),h=b=>xe(g=>s(g,b),m,u,{tol:1e-4}),p=b=>{let g=H(b/2),v=H(b/2,{lowerTail:!1});if(s(m,g)>0||s(u,v)<0)return[NaN,NaN];let N=h(g),y=h(v);return N<=y?[N,y]:[y,N]},d=Lr(n,1-r,p),w=s(m,0)>0||s(u,0)<0?NaN:h(0);return{confInt:d,estimate:w}}function Pi(e,t,n={}){let r=Array.from(e).filter(x=>!Number.isNaN(x)),o=Array.from(t).filter(x=>!Number.isNaN(x)),a=r.length,i=o.length;if(a<1)throw new RangeError("not enough 'x' observations");if(i<1)throw new RangeError("not enough 'y' observations");let l=te(n.alternative),s=n.confLevel??.95;kn(s);let c=fe(r.concat(o)),f=c.slice(0,a),m=c.slice(a),u=x=>x.reduce((q,k)=>q+k,0)/x.length,h=u(f),p=u(m),d=(x,q,k)=>{let S=(x.length+1)/2,L=0;for(let A=0;A<x.length;A++)L+=(x[A]-q[A]-k+S)**2;return L/(x.length-1)},w=d(f,fe(r),h),b=d(m,fe(o),p),g=(p-(i+1)/2)/a,v=a*w+i*b,N=a*i*(p-h)/(a+i)/Math.sqrt(v),y=v**2/((a*w)**2/(a-1)+(i*b)**2/(i-1)),E;l==="greater"?E=we(N,y,!0,!1):l==="less"?E=we(N,y,!1,!1):E=2*we(-Math.abs(N),y,!0,!1);let M=Pe((1-s)/2,y,!1,!1)*Math.sqrt(w/(a*i**2)+b/(i*a**2));return{method:"Brunner-Munzel Test",alternative:l,statistic:N,statisticName:"Brunner-Munzel Test Statistic",parameter:y,parameterName:"df",pValue:E,confInt:[g-M,g+M],confLevel:s,estimate:g,estimateNames:"P(X<Y)+.5*P(X=Y)"}}var ji=[1.2337005501361697,7831619385924639e-32];function zi(e,t,n){let r=(f,m)=>{let u=new Float64Array(t*t);for(let h=0;h<t;h++)for(let p=0;p<t;p++){let d=f[h*t+p];if(d!==0)for(let w=0;w<t;w++)u[h*t+w]+=d*m[p*t+w]}return u},o=f=>{let m=0;for(let p of f)m=Math.max(m,Math.abs(p));if(m===0||!Number.isFinite(m))return 0;let u=Math.floor(Math.log2(m));if(Math.abs(u)<500)return 0;let h=2**-u;for(let p=0;p<f.length;p++)f[p]*=h;return u},a,i=0,l=e.slice(),s=0,c=n;for(;c>0;)c&1&&(a=a===void 0?l.slice():r(a,l),i+=s,i+=o(a)),c>>=1,c>0&&(l=r(l,l),s*=2,s+=o(l));return[a,i]}function ks(e,t){if(Number.isNaN(t)||!(e>=1)||e!==Math.floor(e))return NaN;if(t<=1/(2*e))return 0;if(t>=1)return 1;if(e*t<1){let[f,m]=Mt(e,t),u=2*f-1+2*m,h=1,p=0;for(let d=1;d<=e;d++)h*=d/e*u,h<2**-500&&(h*=2**500,p-=500);for(;p<-1e3;)h*=2**-1e3,p+=1e3;return h*2**p}let n=Math.floor(e*t)+1,r=2*n-1,o=n-e*t,a=new Float64Array(r*r);for(let f=0;f<r;f++)for(let m=0;m<r;m++)a[f*r+m]=f-m+1>=0?1:0;for(let f=0;f<r;f++)a[f*r]-=Math.pow(o,f+1),a[(r-1)*r+f]-=Math.pow(o,r-f);2*o-1>0&&(a[(r-1)*r]+=Math.pow(2*o-1,r));for(let f=0;f<r;f++)for(let m=0;m<r;m++){let u=f-m+1;if(u>0){let h=1;for(let p=2;p<=u;p++)h*=p;a[f*r+m]/=h}}let[i,l]=zi(a,r,e),s=i[(n-1)*r+n-1],c=l;for(let f=1;f<=e;f++)s=s*f/e,Math.abs(s)<2**-500&&(s*=2**500,c-=500);for(;c>1e3;)s*=2**1e3,c-=1e3;for(;c<-1e3;)s*=2**-1e3,c+=1e3;return s*2**c}function Ir(e,t=!0){if(Number.isNaN(e))return NaN;if(e<=0)return t?0:1;if(e===1/0)return t?1:0;if(e<1){let o=ge(ji,Mt(e,e)),a=o[0]+o[1],i=Math.exp(-o[0])*(1-o[1]),l=0;for(let c=2;c<100;c++){let f=Math.exp(-(2*c-1)*(2*c-1)*a);if(l+=f,f<=1e-17*(l+i))break}let s=Math.sqrt(2*Math.PI)/e*(i+l);return t?s:1-s}let n=0;for(let o=1;o<100;o++){let a=Math.exp(-2*o*o*e*e);if(n+=o%2===1?a:-a,a<=1e-17*Math.abs(n))break}let r=2*n;return t?1-r:r}function qs(e,t){if(Number.isNaN(t)||!(e>=1)||e!==Math.floor(e))return NaN;if(t<=0)return 1;if(t>1)return 0;let[n,r]=Mt(e,t),o=Math.floor(e-n-r),a=0;for(let i=0;i<=o;i++){let l=B(rt(e-i,-n),[-r,0]);if(l[0]<=0)break;let s=B(rt(i,n),[r,0]),c=ge(s,[e,0]),f=ge(l,[e,0]);a+=So(i,e-i,c,f,!1)/(c[0]+c[1])}return Math.min(t*a,1)}function As(e,t,n,r={}){let o=r.alternative??"two.sided";if(Number.isNaN(e)||!(t>=1)||!(n>=1)||t!==Math.floor(t)||n!==Math.floor(n))return NaN;if(e<=0)return 1;if(e>1)return 0;let a=t+n,i;if(r.z!==void 0){if(r.z.length!==a)throw new RangeError("z must have length m + n");let u=[...r.z].sort((h,p)=>h-p);i=new Array(a+1).fill(!1);for(let h=1;h<a;h++)i[h]=u[h]!==u[h-1]}let l=e*t*n*(1-1e-10),s=(u,h)=>{let p=u*n-h*t;return(o==="two.sided"?Math.abs(p):o==="greater"?p:-p)>=l},c=new Float64Array(n+1),f=new Float64Array(n+1),m=0;for(let u=0;u<=t;u++){for(let h=0;h<=n;h++){if(u===0&&h===0){f[0]=1;continue}let p=u+h,d=(u>0?u/p*c[h]:0)+(h>0?h/p*f[h-1]:0);(i===void 0?p<a:i[p])&&s(u,h)?(d>0&&(m+=d*Et(u,t,n,p,!1)),f[h]=0):f[h]=d}[c,f]=[f,c],f.fill(0)}return Math.min(m,1)}function Rs(e,t){return e==="two.sided"?"two-sided":`the CDF of x lies ${e==="less"?"below":"above"} ${t?"the null hypothesis":"that of y"}`}function Ts(e,t,n={}){if(typeof e=="string"){let g=Ke(e,t);return Ts(g.x,g.y,n)}let r=Array.from(e).filter(g=>!Number.isNaN(g)).sort((g,v)=>g-v),o=r.length;if(o<1)throw new RangeError("not enough 'x' data");let a=te(n.alternative),i=a==="two.sided"?"D":a==="greater"?"D^+":"D^-";if(typeof t=="function"){let g=-1/0,v=-1/0;for(let x=0;x<o;x++){let q=t(r[x]);g=Math.max(g,(x+1)/o-q),v=Math.max(v,q-x/o)}let N=a==="two.sided"?Math.max(g,v):a==="greater"?g:v,y=new Set(r).size<o,E=n.exact??(o<100&&!y),M;return a==="two.sided"?M=E?1-ks(o,N):Ir(Math.sqrt(o)*N,!1):M=E?qs(o,N):Math.exp(-2*o*N*N),{method:`${E?"Exact":"Asymptotic"} one-sample Kolmogorov-Smirnov test`,alternative:Rs(a,!0),exact:E,statistic:N,statisticName:i,pValue:Math.min(1,Math.max(0,M))}}let l=Array.from(t).filter(g=>!Number.isNaN(g)),s=l.length;if(s<1)throw new RangeError("not enough 'y' data");let c=r.map(g=>[g,0]).concat(l.map(g=>[g,1]));c.sort((g,v)=>g[0]-v[0]);let f=0,m=0,u=0,h=0;for(let g=0;g<c.length;g++){if(c[g][1]===0?f++:m++,g+1<c.length&&c[g+1][0]===c[g][0])continue;let v=f/o-m/s;u=Math.max(u,v),h=Math.max(h,-v)}let p=a==="two.sided"?Math.max(u,h):a==="greater"?u:h,d=new Set(c.map(g=>g[0])).size<c.length,w=n.exact??o*s<1e4,b;if(w)b=As(p,o,s,d?{alternative:a,z:c.map(g=>g[0])}:{alternative:a});else{let g=o*s/(o+s);b=a==="two.sided"?Ir(Math.sqrt(g)*p,!1):Math.exp(-2*g*p*p)}return{method:`${w?"Exact":"Asymptotic"} two-sample Kolmogorov-Smirnov test`,alternative:Rs(a,!1),exact:w,statistic:p,statisticName:i,pValue:Math.min(1,Math.max(0,b))}}function Ss(e,t,n){let r=Array.from(e);if(r.length!==t.length)throw new RangeError("'x' and 'g' must have the same length");let o=new Map;for(let i=0;i<r.length;i++){let l=t[i];if(typeof l=="number"&&Number.isNaN(l)||n&&Number.isNaN(r[i]))continue;let s=o.get(l);s===void 0&&o.set(l,s=[]),s.push(r[i])}let a=Array.from(o.keys()).sort((i,l)=>typeof i=="number"&&typeof l=="number"?i-l:String(i)<String(l)?-1:String(i)>String(l)?1:0);return{levels:a,groups:a.map(i=>o.get(i))}}function Or(e,t,n){let r=[],o=[];for(let l=0;l<e-1;l++)for(let s=l+1;s<e;s++)r.push(t(s,l)),o.push([s-1,l]);let a=It(r,n),i=Array.from({length:e-1},()=>new Array(e-1).fill(NaN));return o.forEach(([l,s],c)=>i[l][s]=a[c]),i}function Fr(e,t,n,r){return{method:e,pValue:n,rowNames:t.slice(1).map(String),colNames:t.slice(0,-1).map(String),pAdjustMethod:r}}function Wi(e,t,n={}){let r=n.paired??!1,o=n.poolSd??!r;if(r&&o)throw new RangeError("pooling of SD is incompatible with paired tests");let a=te(n.alternative),i=n.pAdjustMethod??"holm",{levels:l,groups:s}=Ss(e,t,!r),c=l.length;if(c<2)throw new RangeError("there must be at least two groups");let f,m;if(o){m="t tests with pooled SD";let u=s.map(b=>b.length),h=s.map(b=>b.length>0?se(b):NaN),p=0,d=0;s.forEach(b=>{b.length>1&&(p+=re(b)*(b.length-1),d+=b.length-1)});let w=Math.sqrt(p/d);f=(b,g)=>{let v=(h[b]-h[g])/(w*Math.sqrt(1/u[b]+1/u[g]));return a==="less"?we(v,d,!0,!1):a==="greater"?we(v,d,!1,!1):2*we(-Math.abs(v),d,!0,!1)}}else m=r?"paired t tests":"t tests with non-pooled SD",f=(u,h)=>Lt(s[u],s[h],{paired:r,alternative:a}).pValue;return Fr(m,l,Or(c,f,i),i)}function Gi(e,t,n={}){let{pAdjustMethod:r,alternative:o,...a}=n,i=r??"holm",{levels:l,groups:s}=Ss(e,t,!(a.paired??!1)),c=l.length;if(c<2)throw new RangeError("there must be at least two groups");let f="",m=Or(c,(u,h)=>{let p=nn(s[u],s[h],{...a,...o===void 0?{}:{alternative:o}});return f=p.method,p.pValue},i);return Fr(f,l,m,i)}function Ui(e,t,n={}){if(e.length!==t.length)throw new RangeError("'x' and 'n' must have the same length");let r=[],o=[];e.forEach((f,m)=>{Number.isNaN(f)||Number.isNaN(t[m])||(r.push(f),o.push(t[m]))});let a=r.length;if(a<2)throw new RangeError("too few groups");let i=n.pAdjustMethod??"holm",l=n.correct??!0,s=Or(a,(f,m)=>Mr([r[f],r[m]],[o[f],o[m]],{correct:l}).pValue,i),c=Array.from({length:a},(f,m)=>m+1);return Fr("Pairwise comparison of proportions",c,s,i)}var rn=[0,0];function Ls(e){if(!(e[0]>0))return[Math.sqrt(e[0]),0];let t=Math.sqrt(e[0]),n=le(e,Mt(t,t));return rt(t,n[0]/(2*t))}function jn(e,t){return Array.from({length:t},(n,r)=>e===void 0?[1,0]:Ls([e[r],0]))}function Ot(e,t){return je(e,t,[]).fit}function je(e,t,n){let r=t.length,o=Math.min(e.length,r),a=e.map(f=>f.slice()),i=t.slice(),l=n.map(f=>f.slice()),s=[];for(let f=0;f<o;f++){let m=a[f],u=rn;for(let b=f;b<r;b++)u=B(u,oe(m[b],m[b]));if(u[0]===0){s.push(rn);continue}let h=Ls(u),p=m[f][0]<0?[-h[0],-h[1]]:h;m[f]=B(m[f],p);let d=oe(p,m[f]),w=b=>{let g=rn;for(let N=f;N<r;N++)g=B(g,oe(m[N],b[N]));let v=ge(g,d);for(let N=f;N<r;N++)b[N]=le(b[N],oe(v,m[N]))};for(let b=f+1;b<e.length;b++)w(a[b]);w(i);for(let b of l)w(b);s.push([-p[0],-p[1]])}let c=new Array(o).fill(rn);for(let f=o-1;f>=0;f--){let m=i[f];for(let u=f+1;u<o;u++)m=le(m,oe(a[u][f],c[u]));c[f]=ge(m,s[f])}return{fit:{qty:i,coef:c},qtv:l}}function on(e,t=0,n=e.length){let r=rn;for(let o=t;o<n;o++)r=B(r,oe(e[o],e[o]));return r}function Hi(e,t){let n=e.length,r=e.map(()=>[]),o;switch(t){case"indicators":e.forEach((a,i)=>e.forEach((l,s)=>r[i].push(i===s?1:0))),o=[...e];break;case"contr.treatment":e.forEach((a,i)=>{for(let l=1;l<n;l++)r[i].push(i===l?1:0)}),o=e.slice(1);break;case"contr.sum":e.forEach((a,i)=>{for(let l=0;l<n-1;l++)r[i].push(i===l?1:i===n-1?-1:0)}),o=Array.from({length:n-1},(a,i)=>String(i+1));break;case"contr.helmert":e.forEach((a,i)=>{for(let l=1;l<n;l++)r[i].push(i<l?-1:i===l?l:0)}),o=Array.from({length:n-1},(a,i)=>String(i+1));break;default:throw new RangeError(`unknown contrast '${String(t)}'`)}return{m:r,names:o}}function Ze(e,t,n={},r=e.rhs){if(typeof t!="object"||t===null)throw new TypeError("data must be an object of columns");let o=Wo(e,Object.keys(t),r);n.intercept!==void 0&&(o.intercept=n.intercept);let a=[...o.variables,...(n.extraVariables??[]).filter(g=>!o.variables.includes(g))],i={};n.levels!==void 0&&(i.levels=n.levels),n.weights!==void 0&&(i.weights=n.weights),n.dropUnusedLevels!==void 0&&(i.dropUnusedLevels=n.dropUnusedLevels);let l=Go(t,a,i),s=l.variables,c=l.rows.length,f=o.terms.map((g,v)=>v!==o.responseTerm),m=o.terms.map((g,v)=>g.map(N=>o.factors[N][v]));for(let g=0;g<o.terms.length;g++)if(f[g])for(let v of o.terms[g]){let N=s[v];if(N.kind==="factor"&&N.levels.length<2)throw new RangeError("contrasts can be applied only to factors with 2 or more levels")}if(!o.intercept)e:for(let g=0;g<o.terms.length;g++){if(!f[g])continue;let v=o.terms[g];for(let N=0;N<v.length;N++)if(s[v[N]].kind==="factor"){m[g][N]=2;break e}}let u=[],h=[],p=[];o.intercept&&(u.push(new Array(c).fill(1)),h.push("(Intercept)"),p.push(0));let d=(g,v)=>{let N=s[g],y=o.variables[g];if(N.kind==="numeric")return[{name:y,values:N.values}];let E=v===2?"indicators":n.allContrasts??n.contrasts?.[y]??"contr.treatment",{m:M,names:x}=Hi(N.levels,E);return x.map((q,k)=>({name:y+q,values:N.codes.map(S=>M[S][k])}))};o.terms.forEach((g,v)=>{if(!f[v])return;let N=d(g[0],m[v][0]);for(let y=1;y<g.length;y++){let E=[];for(let M of d(g[y],m[v][y]))for(let x of N)E.push({name:`${x.name}:${M.name}`,values:x.values.map((q,k)=>q*M.values[k])});N=E}for(let y of N)u.push(y.values),h.push(y.name),p.push(v+1)});let w=Array.from({length:c},(g,v)=>u.map(N=>N[v])),b={terms:o,frame:l,x:w,colNames:h,assign:p,warnings:[...o.warnings]};if(o.response>=0){let g=s[o.response];if(g.kind!=="numeric")throw new TypeError("the response must be numeric");b.y=g.values}return b}function Ft(e,t,n={}){let r=Ze(Rt(e),t,n);return{colNames:r.colNames,assign:r.assign,x:r.x,rows:r.frame.rows,termLabels:r.terms.termLabels,order:r.terms.order,intercept:r.terms.intercept,variables:r.terms.variables,factors:r.terms.terms.length>0?r.terms.factors:[],warnings:r.warnings}}function Bi(e,t,n,r,o){let a=t.length,i=n.flatMap((d,w)=>on(d)[0]>1e-5?[w]:[]),l=0,s=[],c=t.slice();if(i.length>0){let d=he(Array.from({length:a},(w,b)=>i.map(g=>n[g][b][0])));l=d.rank,s=d.pivot.slice(0,l).map(w=>i[w]),c=Ot(s.map(w=>n[w]),t).qty}let f=[];s.forEach((d,w)=>{let b=r[d],g=f.find(v=>v.term===b);g===void 0&&f.push(g={term:b,df:0,ss:[0,0]}),g.df++,g.ss=B(g.ss,oe(c[w],c[w]))});let m=a-l,u=on(c,l)[0],h=f.filter(d=>d.term!==0),p=h.map(d=>o[d.term-1]);if(m>0){let d=u/m,w=h.map(({df:b,ss:g})=>{let v=g[0]/b/d;return[b,g[0],g[0]/b,v,ae(v,b,m,!1,!1)]});return w.push([m,u,d,NaN,NaN]),{stratum:`Error: ${e}`,colNames:["Df","Sum Sq","Mean Sq","F value","Pr(>F)"],rowNames:[...p,"Residuals"],table:w}}return{stratum:`Error: ${e}`,colNames:["Df","Sum Sq","Mean Sq"],rowNames:p,table:h.map(({df:d,ss:w})=>[d,w[0],w[0]/d])}}function Is(e,t,n,r,o,a){if(a.weights!==void 0)throw new Error("weights are not supported in a multistratum aov() fit");let i=Ze(t,o,{...a,dropUnusedLevels:!0},n??{type:"num",value:1,text:"1"}),l=i.y;if(l===void 0)throw new TypeError("the formula has no response");let s=Ze({response:t.response,rhs:r},o,{...a.levels===void 0?{}:{levels:a.levels},allContrasts:"contr.helmert",intercept:i.terms.intercept,extraVariables:i.terms.variables,dropUnusedLevels:!0}),c=l.length,f=[...i.warnings,...s.warnings.filter(y=>!i.warnings.includes(y))],m=s.colNames.length,u=m>0?he(s.x):void 0,h=u?.rank??0;h<m&&f.push("Error() model is singular");let p=u?.pivot.slice(0,h)??[],d=y=>y.map(E=>[E,0]),w=i.colNames.map((y,E)=>d(i.x.map(M=>M[E]))),{fit:b,qtv:g}=je(p.map(y=>d(s.x.map(E=>E[y]))),d(l),w),v=[];p.forEach((y,E)=>{let M=s.assign[y],x=M===0?"(Intercept)":s.terms.termLabels[M-1],q=v.find(k=>k.name===x);q===void 0&&v.push(q={name:x,idx:[]}),q.idx.push(E)}),c>h&&v.push({name:"Within",idx:Array.from({length:c-h},(y,E)=>h+E)});let N=v.map(y=>Bi(y.name,y.idx.map(E=>b.qty[E]),g.map(E=>y.idx.map(M=>E[M])),i.assign,i.terms.termLabels));return{formula:e,multistratum:!0,strata:N,rows:i.frame.rows,warnings:f}}function _r(e,t,n){let r=t.y;if(r===void 0)throw new TypeError("the formula has no response");let o=Os(t.x,r,t.frame.weights,t.terms.intercept,!0),a=o.result,i=[...t.warnings];if(!n){let s=o.fitRows.map(u=>a.fittedValues[u]),c=s.reduce((u,h)=>u+h,0)/s.length,f=s.length>1?s.reduce((u,h)=>u+(h-c)**2,0)/(s.length-1):NaN,m=o.rss/a.dfResidual;Number.isFinite(m)&&m<1e-30*(c*c+f)&&i.push("essentially perfect fit: summary may be unreliable")}let l={...a,formula:e,response:t.terms.variables[0],coefNames:t.colNames,assign:t.assign,termLabels:t.terms.termLabels,order:t.terms.order,variables:t.terms.variables,factors:t.terms.factors,rows:t.frame.rows,x:t.x,y:r,effects:o.effects,rss:o.rss,nObs:o.fitRows.length,aov:n,warnings:i};return t.frame.weights!==void 0&&(l.weights=t.frame.weights),l}function Cr(e,t,n={}){return typeof e=="string"?_r(e,Ze(Rt(e),t,{...n,dropUnusedLevels:!0}),!1):Xi(e,t,n)}function Xi(e,t,n){let r=n.intercept??!0,o=e.length>0&&typeof e[0]!="number"?e.map(m=>Array.from(m)):Array.from(e,m=>[m]),a=Array.from(t);if(o.length!==a.length)throw new RangeError("variable lengths differ (found for 'x')");let i=n.weights===void 0?void 0:Array.from(n.weights);if(i!==void 0&&i.length!==a.length)throw new RangeError("variable lengths differ (found for '(weights)')");if(i?.some(m=>m<0))throw new RangeError("missing or negative weights not allowed");let l=a.map((m,u)=>!Number.isNaN(m)&&o[u].every(h=>!Number.isNaN(h))&&(i===void 0||!Number.isNaN(i[u]))),s=o.filter((m,u)=>l[u]).map(m=>r?[1,...m]:m),c=a.filter((m,u)=>l[u]),f=i?.filter((m,u)=>l[u]);return Os(s,c,f,r).result}function Qi(e,t,n,r,o,a){let i=o.rank,l=o.pivot.slice(0,i),s=jn(n===void 0?void 0:r.map(g=>n[g]),r.length),c=l.map(g=>r.map((v,N)=>Te(s[N],e[v][g]))),{qty:f,coef:m}=Ot(c,r.map((g,v)=>Te(s[v],t[g]))),u=e[0]?.map(()=>NaN)??[];l.forEach((g,v)=>u[g]=m[v][0]);let h=e.map(g=>{let v=[0,0];return l.forEach((N,y)=>v=B(v,Te(m[y],g[N]))),v}),p=on(f,i)[0],d=r.map(g=>n?.[g]??1),w=[0,0];if(a){let g=[0,0],v=[0,0];r.forEach((N,y)=>{g=B(g,[d[y],0]),v=B(v,Te(h[N],d[y]))}),w=ge(v,g)}let b=[0,0];return r.forEach((g,v)=>{let N=le(h[g],w);b=B(b,Te(oe(N,N),d[v]))}),{coefficients:u,fittedValues:h.map(g=>g[0]),residuals:h.map((g,v)=>le([t[v],0],g)[0]),effects:f.map(g=>g[0]),rss:p,mss:b[0]}}function Os(e,t,n,r,o=!1){if(t.length===0)throw new RangeError("0 (non-NA) cases");let a=t.map((D,G)=>n===void 0||n[G]!==0),i=e.filter((D,G)=>a[G]),l=t.filter((D,G)=>a[G]),s=n?.filter((D,G)=>a[G]),c=l.length;if(c===0)throw new RangeError("all weights are zero");let f=t.flatMap((D,G)=>a[G]?[G]:[]),m=s?.map(Math.sqrt),u=m===void 0?i:i.map((D,G)=>D.map(Q=>Q*m[G])),h=m===void 0?l:l.map((D,G)=>D*m[G]),p=he(u),d=p.rank,w=o?Qi(e,t,n,f,p,r):void 0,b=w?.coefficients??St(p,h),g=w?.residuals??[],v=w?.fittedValues??[],N=c-d,y=s??new Array(c).fill(1),E=w?.rss??0;if(w===void 0){let D=qn(p,h),G=m===void 0?D:D.map((ne,de)=>ne/m[de]),Q=0;t.forEach((ne,de)=>{if(a[de])g.push(G[Q]),v.push(ne-G[Q]),Q++;else{let ve=e[de].reduce((Oe,Fe,Le)=>Number.isNaN(b[Le])?Oe:Oe+Fe*b[Le],0);v.push(ve),g.push(ne-ve)}});for(let ne=0;ne<c;ne++)E+=y[ne]*G[ne]**2}let M=v.filter((D,G)=>a[G]),x=E/N,q=Math.sqrt(x),k=p.qr.slice(0,d).map(D=>D.slice(0,d)),S=d>0?Rn(k):[],A=p.pivot.slice(0,d).map((D,G)=>[D,G]).sort((D,G)=>D[0]-G[0]),R=A.map(([D])=>b[D]),V=A.map(([,D])=>Math.sqrt(S[D][D]*x)),F=R.map((D,G)=>D/V[G]),I=F.map(D=>2*we(-Math.abs(D),N,!0,!1)),C={coefficients:b,residuals:g,fittedValues:v,rank:d,dfResidual:N,coefTable:{estimate:R,stdError:V,tValue:F,pValue:I},sigma:q,rSquared:0,adjRSquared:0,intercept:r,qr:p},z=r?1:0,J=d-z,T={result:C,fitRows:f,effects:w?.effects??[],rss:E};if(J===0)return T;let O=0;if(w!==void 0)O=w.mss;else if(r){let D=y.reduce((Q,ne)=>Q+ne,0),G=0;for(let Q=0;Q<c;Q++)G+=y[Q]*M[Q]/D;for(let Q=0;Q<c;Q++)O+=y[Q]*(M[Q]-G)**2}else for(let D=0;D<c;D++)O+=y[D]*M[D]**2;C.rSquared=O/(O+E),C.adjRSquared=1-(1-C.rSquared)*((c-z)/N);let $=O/J/x;return C.fStatistic={value:$,numdf:J,dendf:N},C.fPValue=ae($,J,N,!1,!1),T}function Ki(e,t={}){let n=t.level??.95,r=Pe((1+n)/2,e.dfResidual,!0,!1),{estimate:o,stdError:a}=e.coefTable,i=e.coefficients.map(()=>NaN),l=e.coefficients.map(()=>NaN),s=0;return e.coefficients.forEach((c,f)=>{Number.isNaN(c)||(i[f]=o[s]-r*a[s],l[f]=o[s]+r*a[s],s++)}),{lower:i,upper:l}}function Zi(e,t,n={}){let o=(t.length>0&&typeof t[0]!="number"?t.map(p=>Array.from(p)):Array.from(t,p=>[p])).map(p=>e.intercept?[1,...p]:p),a=e.rank,i=e.qr.pivot.slice(0,a),l=e.qr.qr.slice(0,a).map(p=>p.slice(0,a)),s=e.sigma**2,c=o.map(p=>p.reduce((d,w,b)=>Number.isNaN(e.coefficients[b])?d:d+w*e.coefficients[b],0)),f=o.map(p=>{let d=new Array(a).fill(0);for(let w=0;w<a;w++){let b=p[i[w]];for(let g=0;g<w;g++)b-=l[g][w]*d[g];d[w]=b/l[w][w]}return Math.sqrt(d.reduce((w,b)=>w+b*b,0)*s)}),m=n.interval??"none";if(m==="none")return{fit:c,seFit:f};let u=Pe((1+(n.level??.95))/2,e.dfResidual,!0,!1),h=f.map(p=>u*Math.sqrt(p*p+(m==="prediction"?s:0)));return{fit:c,seFit:f,lwr:c.map((p,d)=>p-h[d]),upr:c.map((p,d)=>p+h[d])}}function Fs(e){let t=e.flat(),n=Yt(t),r=[0,0],o=[0,0],a=[];for(let i of e){let l=Yt(i);a.push(l[0]);let s=le(l,n);r=B(r,oe(oe(s,s),[i.length,0]));for(let c of i){let f=le([c,0],l);o=B(o,oe(f,f))}}return{ssb:r[0],ssw:o[0],means:a}}function Vr(e,t,n={}){return typeof e=="string"?Yi(e,t,n):Ji(e,t)}function Yi(e,t,n){let r=Rt(e,!0),{treatment:o,errors:a}=zo(r.rhs);if(a.length>1)throw new Error(`there are ${a.length} Error terms: only 1 is allowed`);return a.length===1?Is(e,r,o,a[0],t,n):_r(e,Ze(r,t,{...n,dropUnusedLevels:!0},o),!0)}function Ji(e,t){let n=ht(e,t),r=n.length;if(r<2)throw new RangeError("contrasts can be applied only to factors with 2 or more levels");let o=n.reduce((u,h)=>u+h.length,0),{ssb:a,ssw:i}=Fs(n),l=r-1,s=o-r,c=a/l,f=i/s,m=c/f;return{df:[l,s],sumSq:[a,i],meanSq:[c,f],fValue:m,pValue:s>0?ae(m,l,s,!1,!1):NaN}}function el(e,t,n={}){let r;t!==void 0&&!Array.isArray(t)?n=t:r=t;let{labels:o,groups:a}=ut(e,r),i=a.length;if(i<2)throw new RangeError("contrasts can be applied only to factors with 2 or more levels");let l=n.confLevel??.95,c=a.reduce((d,w)=>d+w.length,0)-i,{ssw:f,means:m}=Fs(a),u=f/c,h=$o(l,i,c),p={names:[],i:[],j:[],diff:[],lwr:[],upr:[],pAdj:[]};for(let d=0;d<i-1;d++)for(let w=d+1;w<i;w++){let b=m[w]-m[d],g=Math.sqrt(u/2*(1/a[w].length+1/a[d].length));p.names.push(`${o[w]}-${o[d]}`),p.i.push(w+1),p.j.push(d+1),p.diff.push(b),p.lwr.push(b-h*g),p.upr.push(b+h*g),p.pAdj.push(yn(Math.abs(b)/g,i,c,{lowerTail:!1}))}return p}function tl(e,t,n={}){let r;t!==void 0&&!ArrayBuffer.isView(t)&&!Array.isArray(t)?n=t:r=t;let{sigmaX:o,sigmaY:a}=n,i=n.mu??0;if(o===void 0&&r===void 0)throw new RangeError("sigmaX (the known standard deviation of x) is required");if(o===void 0||r!==void 0&&a===void 0)throw new RangeError("sigmaX and sigmaY are both required for two samples");if(Number.isNaN(i))throw new RangeError("mu must be a number");let l=te(n.alternative),s=n.confLevel??.95;if(!(s>=0&&s<=1))throw new RangeError("confLevel must be between 0 and 1");let c=Array.from(e).filter(v=>!Number.isNaN(v)),f=r===void 0?void 0:Array.from(r).filter(v=>!Number.isNaN(v));if(c.length<3)throw new RangeError("x needs at least 3 non-missing values");if(f!==void 0&&f.length<3)throw new RangeError("y needs at least 3 non-missing values");let m=Yt(c)[0],u=f===void 0?NaN:Yt(f)[0],h=f===void 0?o/Math.sqrt(c.length):Math.sqrt(o**2/c.length+a**2/f.length),p=f===void 0?m:m-u,d=(p-i)/h,w,b;if(l==="less")w=P(d),b=[-1/0,p+H(s)*h];else if(l==="greater")w=P(d,{lowerTail:!1}),b=[p-H(s)*h,1/0];else{w=2*P(-Math.abs(d));let v=H(1-(1-s)/2)*h;b=[p-v,p+v]}let g=f!==void 0;return{method:g?"Two-sample z-Test":"One-sample z-Test",alternative:l,statistic:d,statisticName:"z",pValue:w,confInt:b,confLevel:s,estimate:g?[m,u]:m,estimateNames:g?["mean of x","mean of y"]:"mean of x",nullValue:i,nullValueName:g?"difference in means":"mean"}}var Ee=2**-52,nl={gaussian:["identity","log","inverse"],binomial:["logit","probit","cauchit","log","cloglog"],quasibinomial:["logit","probit","cauchit","log","cloglog"],poisson:["log","identity","sqrt"],quasipoisson:["log","identity","sqrt"],Gamma:["inverse","identity","log"],"inverse.gaussian":["1/mu^2","inverse","identity","log"],quasi:["identity","logit","probit","cloglog","inverse","log","1/mu^2","sqrt"]},Dr=e=>Number.isFinite(e)&&e>0,_t=()=>!0,Ne=class{list=[];add(t){this.list.includes(t)||this.list.push(t)}},zn;function Wn(e,t){let n=zn;zn=e;try{return t()}finally{zn=n}}function rl(e){return e<0&&zn?.add("NaNs produced"),Math.sqrt(e)}function $r(e){switch(e){case"identity":return{name:e,link:t=>t,linkinv:t=>t,muEta:()=>1,etaOk:_t};case"log":return{name:e,link:Math.log,linkinv:t=>Math.max(Math.exp(t),Ee),muEta:t=>Math.max(Math.exp(t),Ee),etaOk:_t};case"inverse":return{name:e,link:t=>1/t,linkinv:t=>1/t,muEta:t=>-1/(t*t),etaOk:t=>Number.isFinite(t)&&t!==0};case"sqrt":return{name:e,link:Math.sqrt,linkinv:t=>t*t,muEta:t=>2*t,etaOk:Dr};case"1/mu^2":return{name:e,link:t=>1/(t*t),linkinv:t=>1/rl(t),muEta:t=>-1/(2*t**1.5),etaOk:Dr};case"logit":return{name:e,link:t=>Math.log(t/(1-t)),linkinv:t=>{let n=t<-30?Ee:t>30?1/Ee:Math.exp(t);return n/(1+n)},muEta:t=>{if(Math.abs(t)>30)return Ee;let n=Math.exp(t);return n/((1+n)*(1+n))},etaOk:_t};case"probit":{let t=-H(Ee);return{name:e,link:n=>H(n),linkinv:n=>P(Math.min(Math.max(n,-t),t)),muEta:n=>Math.max(ye(n),Ee),etaOk:_t}}case"cauchit":{let t=-fr(Ee,0,1,!0,!1);return{name:e,link:n=>fr(n,0,1,!0,!1),linkinv:n=>_o(Math.min(Math.max(n,-t),t),0,1,!0,!1),muEta:n=>Math.max(Fo(n,0,1,!1),Ee),etaOk:_t}}case"cloglog":return{name:e,link:t=>Math.log(-Math.log(1-t)),linkinv:t=>Math.min(Math.max(-Math.expm1(-Math.exp(t)),Ee),1-Ee),muEta:t=>{let n=Math.min(t,700);return Math.max(Math.exp(n)*Math.exp(-Math.exp(n)),Ee)},etaOk:_t};default:throw new RangeError(`unknown link "${String(e)}"`)}}function ol(e){if(!Number.isFinite(e))throw new RangeError("invalid power for the power link");if(e<=0)return $r("log");if(e===1)return $r("identity");let t=1/e;return{name:`mu^${Math.round(e*1e3)/1e3}`,link:n=>n**e,linkinv:n=>Math.max(n**t,Ee),muEta:n=>Math.max(t*n**(t-1),Ee),etaOk:Dr}}function sl(e){switch(e){case"constant":return{variance:()=>1,muOk:()=>!0,dev:(t,n,r)=>r*(t-n)**2};case"mu(1-mu)":return{variance:t=>t*(1-t),muOk:t=>t>0&&t<1,dev:_s};case"mu":return{variance:t=>t,muOk:t=>t>0,dev:(t,n,r)=>2*r*((t===0?0:t*Math.log(t/n))-(t-n))};case"mu^2":return{variance:t=>t*t,muOk:t=>t>0,dev:(t,n,r)=>Math.max(-2*r*(Math.log(t>0?t/n:1/n)-(t-n)/n),0)};case"mu^3":return{variance:t=>t*t*t,muOk:t=>t>0,dev:Cs};default:throw new RangeError(`'variance' "${String(e)}" is invalid: possible values are "mu(1-mu)", "mu", "mu^2", "mu^3" and "constant"`)}}function _s(e,t,n){let r=e===0?0:e*Math.log(e/t),o=e===1?0:(1-e)*Math.log((1-e)/(1-t));return 2*n*(r+o)}function Cs(e,t,n){return n*(e-t)**2/(e*t*t)}function sn(e,t){let n=typeof e=="string"?{family:e}:e,r=n.family,o=nl[r];if(o===void 0)throw new RangeError(`unknown family "${String(r)}"`);let a=t??n.link??o[0],i;if(typeof a=="object"){if(r!=="quasi")throw new RangeError(`link "power" not available for ${r} family`);i=ol(a.power)}else{if(!o.includes(a))throw new RangeError(`link "${a}" not available for ${r} family; available links are ${o.map(s=>`'${s}'`).join(", ")}`);i=$r(a)}if(n.variance!==void 0&&r!=="quasi")throw new TypeError("'variance' is only used by the quasi family");let l={...i,family:r,varianceName:void 0,fixedDispersion:!1};switch(r){case"gaussian":return{...l,variance:()=>1,muOk:()=>!0,devContribution:(s,c,f)=>f*(s-c)**2};case"binomial":case"quasibinomial":return{...l,fixedDispersion:r==="binomial",variance:s=>s*(1-s),muOk:s=>Number.isFinite(s)&&s>0&&s<1,devContribution:_s};case"poisson":case"quasipoisson":return{...l,fixedDispersion:r==="poisson",variance:s=>s,muOk:s=>Number.isFinite(s)&&s>0,devContribution:(s,c,f)=>s>0?2*f*(s*Math.log(s/c)-(s-c)):2*f*c};case"Gamma":return{...l,variance:s=>s*s,muOk:s=>Number.isFinite(s)&&s>0,devContribution:(s,c,f)=>-2*f*((s===0?0:Math.log(s/c))-(s-c)/c)};case"inverse.gaussian":return{...l,variance:s=>s*s*s,muOk:()=>!0,devContribution:Cs};case"quasi":{let s=n.variance??"constant",c=sl(s);return{...l,varianceName:s,variance:c.variance,muOk:c.muOk,devContribution:c.dev}}}}function Pr(e,t,n,r,o){let a=e.family,i=a==="binomial"||a==="quasibinomial";if(t.length>0&&Array.isArray(t[0])){let f=t;if(f.every(d=>d.length===1))return Pr(e,f.map(d=>d[0]),n,r,o);if(!i)throw new TypeError("y must be a vector (or, for the binomial families, a two-column matrix)");if(f.some(d=>d.length!==2))throw new TypeError(`for the '${a}' family, y must be a vector of 0 and 1's
3
+ or a 2 column matrix where col 1 is no. successes and col 2 is no. failures`);a==="binomial"&&f.some(d=>Math.abs(d[0]-Math.round(d[0]))>.001||Math.abs(d[1]-Math.round(d[1]))>.001)&&o.add("non-integer counts in a binomial glm!");let m=f.map(d=>d[0]+d[1]),u=f.map((d,w)=>m[w]===0?0:d[0]/m[w]),h=n.map((d,w)=>d*m[w]),p=u.map((d,w)=>(m[w]*d+.5)/(m[w]+1));return{y:u,weights:h,mustart:p,size:m}}let s=t.slice(),c=n.slice();switch(a){case"binomial":case"quasibinomial":{for(let m=0;m<s.length;m++)c[m]===0&&(s[m]=0);if(s.some(m=>m<0||m>1))throw new RangeError("y values must be 0 <= y <= 1");let f=s.map((m,u)=>(c[u]*m+.5)/(c[u]+1));return a==="binomial"&&s.some((m,u)=>Math.abs(c[u]*m-Math.round(c[u]*m))>.001)&&o.add("non-integer #successes in a binomial glm!"),{y:s,weights:c,mustart:f,size:s.map(()=>1)}}case"poisson":case"quasipoisson":if(s.some(f=>f<0))throw new RangeError(`negative values not allowed for the '${a==="poisson"?"Poisson":"quasiPoisson"}' family`);return{y:s,weights:c,mustart:s.map(f=>f+.1),size:void 0};case"Gamma":if(s.some(f=>f<=0))throw new RangeError("non-positive values not allowed for the 'Gamma' family");return{y:s,weights:c,mustart:s.slice(),size:void 0};case"inverse.gaussian":if(s.some(f=>f<=0))throw new RangeError("positive values only are allowed for the 'inverse.gaussian' family");return{y:s,weights:c,mustart:s.slice(),size:void 0};case"gaussian":if(!r&&(e.name==="inverse"&&s.some(f=>f===0)||e.name==="log"&&s.some(f=>f<=0)))throw new Error("cannot find valid starting values: please specify some");return{y:s,weights:c,mustart:s.slice(),size:void 0};case"quasi":{let f=e.varianceName,m=f==="constant"?s.slice():f==="mu(1-mu)"?s.map(u=>Math.min(Math.max(u,.001),.999)):s.map(u=>u+(u===0?.1:0));return{y:s,weights:c,mustart:m,size:void 0}}}}function Vs(e,t,n,r,o,a,i){let l=t.length,s=0;switch(e.family){case"gaussian":{let c=0;for(let f of r)c+=Math.log(f);return l*(Math.log(2*Math.PI*a/l)+1)+2-c}case"binomial":{let c=o!==void 0&&o.some(f=>f>1)?o:r;for(let f=0;f<l;f++)c[f]>0&&(s+=r[f]/c[f]*xt(Qt(c[f]*t[f]),Qt(c[f]),n[f],!0));return-2*s}case"poisson":for(let c=0;c<l;c++)Oo(t[c])&&i.add(`non-integer x = ${t[c].toFixed(6)}`),s+=r[c]*Kt(t[c],n[c],!0);return-2*s;case"Gamma":{let c=0;for(let m of r)c+=m;let f=a/c;for(let m=0;m<l;m++)s+=r[m]*Lo(t[m],1/f,n[m]*f,!0);return-2*s+2}case"inverse.gaussian":{let c=0,f=0;for(let m=0;m<l;m++)c+=r[m],f+=r[m]*Math.log(t[m]);return c*(Math.log(2*Math.PI*a/c)+1)+3*f+2}default:return NaN}}var Ct=2**-52;function al(e){let t=e.map(n=>`"${n}"`);return t.length===1?t[0]:`c(${t.join(", ")})`}function Gn(e,t,n){return e.map((r,o)=>{let a=0;for(let i=0;i<r.length;i++)a+=r[i]*t[i];return a+n[o]})}function dt(e,t,n,r){let o=0;for(let a=0;a<t.length;a++)o+=e.devContribution(t[a],n[a],r[a]);return o}function Ds(e,t){let{x:n,p:r,y:o,w:a,offset:i,fam:l,epsilon:s,maxit:c}=e,f=o.length;return Wn(t,()=>{let m=(L,A)=>L.every(R=>l.etaOk(R))&&A.every(R=>l.muOk(R));if(r===0){let L=i.slice();if(!L.every(F=>l.etaOk(F)))throw new Error("invalid linear predictor values in empty model");let A=L.map(F=>l.linkinv(F));if(!A.every(F=>l.muOk(F)))throw new Error("invalid fitted means in empty model");let R=L.map(F=>l.muEta(F)),V=A.map((F,I)=>Math.sqrt(a[I]*R[I]**2/l.variance(F))**2);return{coefficients:[],eta:L,mu:A,deviance:dt(l,o,A,a),iter:0,converged:!0,boundary:!0,rank:0,qr:void 0,used:a.map(F=>F>0),workingWeights:V,workingResiduals:o.map((F,I)=>(F-A[I])/R[I])}}let u,h;if(e.start!==void 0){if(e.start.length!==r)throw new RangeError(`length of 'start' should equal ${r} and correspond to initial coefs for ${al(e.columnNames)}`);h=e.start.slice(),u=Gn(n,h,i)}else e.etastart!==void 0?u=e.etastart.slice():u=e.mustart.map(L=>l.link(L));let p=u.map(L=>l.linkinv(L));if(!m(u,p))throw new Error("cannot find valid starting values: please specify some");let d=dt(l,o,p,a),w=d,b=!1,g=!1,v=0,N=new Array(r).fill(0),y,E=a.map(()=>!1),M=[],x=Math.min(1e-7,s/1e3);for(v=1;v<=c;v++){let L=u.map(I=>l.muEta(I));for(let I=0;I<f;I++){if(!(a[I]>0))continue;let C=l.variance(p[I]);if(Number.isNaN(C))throw new Error("NAs in V(mu)");if(C===0)throw new Error("0s in V(mu)");if(Number.isNaN(L[I]))throw new Error("NAs in d(mu)/d(eta)")}E=a.map((I,C)=>I>0&&L[C]!==0);let A=[];for(let I=0;I<f;I++)E[I]&&A.push(I);if(A.length===0){t.add(`no observations informative at iteration ${v}`),b=!1;break}M=new Array(f).fill(0);let R=[],V=[];for(let I of A){let C=Math.sqrt(a[I]*L[I]**2/l.variance(p[I]));M[I]=C,R.push((u[I]-i[I]+(o[I]-p[I])/L[I])*C),V.push(n[I].map(z=>z*C))}y=he(V,{tol:x});let F=St(y,R);if(F.some((I,C)=>y.pivot.indexOf(C)<y.rank&&!Number.isFinite(I))){t.add(`non-finite coefficients at iteration ${v}`),b=!1;break}if(N=F.map(I=>Number.isNaN(I)?0:I),g=!1,u=Gn(n,N,i),p=u.map(I=>l.linkinv(I)),w=dt(l,o,p,a),!Number.isFinite(w)){if(h===void 0)throw new Error("no valid set of coefficients has been found: please supply starting values");t.add("step size truncated due to divergence");let I=0;for(;!Number.isFinite(w);){if(I>=c)throw new Error("inner loop 1; cannot correct step size");I++,N=N.map((C,z)=>(C+h[z])/2),u=Gn(n,N,i),p=u.map(C=>l.linkinv(C)),w=dt(l,o,p,a)}g=!0}if(!m(u,p)){if(h===void 0)throw new Error("no valid set of coefficients has been found: please supply starting values");t.add("step size truncated: out of bounds");let I=0;for(;!m(u,p);){if(I>=c)throw new Error("inner loop 2; cannot correct step size");I++,N=N.map((C,z)=>(C+h[z])/2),u=Gn(n,N,i),p=u.map(C=>l.linkinv(C))}g=!0,w=dt(l,o,p,a)}if(e.untilCoefficients){let I=Math.max(...N.map(Math.abs));if(h!==void 0&&N.every((C,z)=>Math.abs(C-h[z])<=8*Ct*I)){b=!0;break}}else if(Math.abs(w-d)/(Math.abs(w)+.1)<s){b=!0;break}d=w,h=N.slice()}v>c&&(v=c);let q=r,k=N.slice();if(y!==void 0){q=y.rank;for(let L=y.rank;L<r;L++)k[y.pivot[L]]=NaN}let S=u.map(L=>l.muEta(L));return{coefficients:k,eta:u,mu:p,deviance:w,iter:v,converged:b,boundary:g,rank:q,qr:y,used:E,workingWeights:M.map(L=>L*L),workingResiduals:o.map((L,A)=>(L-p[A])/S[A])}})}function $s(e){return e.length>0&&typeof e[0]!="number"?e.map(t=>Array.from(t)):Array.from(e,t=>[t])}function Ps(e,t,n,r){r||(e.converged||n.add("glm.fit: algorithm did not converge"),e.boundary&&n.add("glm.fit: algorithm stopped at boundary value"),t.family==="binomial"&&e.mu.some(o=>o>1-10*Ct||o<10*Ct)&&n.add("glm.fit: fitted probabilities numerically 0 or 1 occurred"),t.family==="poisson"&&e.mu.some(o=>o<10*Ct)&&n.add("glm.fit: fitted rates numerically 0 occurred"))}function Vt(e,t,n,r={}){let o=sn(e.familySpec),a="start"in r?r.start:e.start,i=Pr(o,e.yGiven,e.priorGiven,a!==void 0||r.etastart!==void 0,n),l=r.extraOffset===void 0?e.offset:e.offset.map((f,m)=>f+r.extraOffset[m]),s=e.x.map(f=>t.map(m=>f[m])),c=Ds({x:s,p:t.length,y:i.y,w:i.weights,offset:l,fam:o,epsilon:r.epsilon??e.epsilon,maxit:r.maxit??e.maxit,columnNames:t.map(f=>e.columnNames[f]),start:a,etastart:r.etastart,mustart:i.mustart,untilCoefficients:r.untilCoefficients},n);return Ps(c,o,n,t.length===0),{fit:c,fam:o,y:i.y,w:i.weights,size:i.size,offset:l}}function il(e,t,n={}){let r=n.intercept??!0,o=$s(e),i=t.length>0&&typeof t[0]!="number"?t.map(V=>Array.from(V)):Array.from(t),l=i.length;if(o.length!==l)throw new RangeError("variable lengths differ (found for 'x1')");let s=n.weights===void 0?void 0:Array.from(n.weights);if(s!==void 0&&s.length!==l)throw new RangeError("variable lengths differ (found for '(weights)')");let c=n.offset===void 0?void 0:Array.from(n.offset);if(c!==void 0&&c.length!==l)throw new RangeError("variable lengths differ (found for '(offset)')");let f=o.map((V,F)=>{let I=i[F];return!(Array.isArray(I)?I.some(z=>Number.isNaN(z)):Number.isNaN(I))&&V.every(z=>!Number.isNaN(z))&&!(s!==void 0&&Number.isNaN(s[F]))&&!(c!==void 0&&Number.isNaN(c[F]))}),m=o.filter((V,F)=>f[F]).map(V=>r?[1,...V]:V),u=i.filter((V,F)=>f[F]),h=u.length;if(h===0)throw new RangeError("0 (non-NA) cases");let p=s===void 0?new Array(h).fill(1):s.filter((V,F)=>f[F]);if(p.some(V=>V<0))throw new RangeError("negative weights not allowed");let d=c===void 0?new Array(h).fill(0):c.filter((V,F)=>f[F]),w=o.length>0?o[0].length:0,b=[...r?["(Intercept)"]:[],...Array.from({length:w},(V,F)=>`x${F+1}`)],g=b.length;if(m.some(V=>V.length!==g))throw new TypeError("all rows of x must have the same length");let v=typeof n.family=="object"?{...n.family}:{family:n.family??"gaussian"};n.link!==void 0&&(v.link=n.link);let N={x:m,columnNames:b,yGiven:u,priorGiven:p,offset:d,hasOffset:c!==void 0,intercept:r,familySpec:v,epsilon:n.epsilon??1e-8,maxit:n.maxit??25,start:n.start===void 0?void 0:Array.from(n.start)};if(!(N.epsilon>0))throw new RangeError("value of 'epsilon' must be > 0");if(!(N.maxit>0))throw new RangeError("maximum number of iterations must be > 0");let y=new Ne,E=Array.from({length:g},(V,F)=>F),{fit:M,fam:x,y:q,w:k,size:S}=Vt(N,E,y),L;if(r&&N.hasOffset){let V=new Ne,F=Ds({x:m.map(()=>[1]),p:1,y:q,w:k,offset:d,fam:x,epsilon:N.epsilon,maxit:N.maxit,columnNames:["(Intercept)"],mustart:M.mu},V);Ps(F,x,V,!1),V.list.forEach(I=>y.add(I)),L=F.deviance,F.converged||y.add("fitting to calculate the null deviance did not converge -- increase 'maxit'?")}else if(r){let V=0,F=0;for(let C=0;C<h;C++)V+=k[C],F+=k[C]*q[C];let I=F/V;L=dt(x,q,q.map(()=>I),k)}else L=dt(x,q,Wn(y,()=>d.map(V=>x.linkinv(V))),k);let A=h-k.filter(V=>V===0).length,R=Vs(x,q,M.mu,k,S,M.deviance,y)+2*M.rank;return{coefficients:M.coefficients,fittedValues:M.mu,linearPredictors:M.eta,deviance:M.deviance,nullDeviance:L,aic:R,rank:M.rank,dfResidual:A-M.rank,dfNull:A-(r?1:0),iter:M.iter,converged:M.converged,boundary:M.boundary,y:q,priorWeights:k,weights:M.workingWeights,residuals:M.workingResiduals,family:x.family,link:x.name,warnings:y.list,qr:M.qr,model:N}}function Un(e){return sn(e.model.familySpec)}function Dt(e,t){if(Un(e).fixedDispersion)return 1;if(e.dfResidual<=0)return NaN;let n=0;for(let r=0;r<e.weights.length;r++)e.weights[r]>0&&(n+=e.weights[r]*e.residuals[r]**2);return e.weights.some(r=>r===0)&&t?.add("observations with zero weight not used for calculating dispersion"),n/e.dfResidual}function ll(e){if(e.qr===void 0||e.rank===0)return[];let t=e.qr.qr.slice(0,e.rank).map(n=>n.slice(0,e.rank));return Rn(t)}function jr(e,t={}){let n=new Ne,r=Un(e),o=t.dispersion===void 0&&!r.fixedDispersion,a=t.dispersion??Dt(e,n),i=ll(e),s=(e.qr===void 0?[]:e.qr.pivot.slice(0,e.rank)).map(h=>e.coefficients[h]),c=i.map((h,p)=>Math.sqrt(h[p]*a)),f=s.map((h,p)=>h/c[p]),m=!o,u=f.map(h=>m?2*P(-Math.abs(h)):e.dfResidual>0?2*we(-Math.abs(h),e.dfResidual,!0,!1):NaN);return o&&e.dfResidual===0&&(c.fill(NaN),f.fill(NaN)),{dispersion:a,estimate:s,stdError:c,statistic:f,pValue:u,statisticName:r.fixedDispersion?"z value":"t value",covUnscaled:i,covScaled:i.map(h=>h.map(p=>p*a)),dfResidual:e.dfResidual,warnings:n.list}}function zr(e,t="deviance"){let n=Un(e),{y:r,fittedValues:o,priorWeights:a}=e;switch(t){case"response":return r.map((i,l)=>i-o[l]);case"working":return e.residuals.slice();case"pearson":return r.map((i,l)=>(i-o[l])*Math.sqrt(a[l])/Math.sqrt(n.variance(o[l])));case"deviance":return e.dfResidual<=0?r.map(()=>0):r.map((i,l)=>Math.sign(i-o[l])*Math.sqrt(Math.max(n.devContribution(i,o[l],a[l]),0)));default:throw new RangeError(`unknown type of residuals "${String(t)}"`)}}function Wr(e){let t=[],n=e.qr,r=[];if(n!==void 0&&e.rank>0){let i=n.qr.length,l=Array.from({length:i},(c,f)=>Array.from({length:e.rank},(m,u)=>f===u?1:0));r=Zo(n,l).map(c=>c.reduce((f,m)=>f+m*m,0))}let o=0,a=e.weights.map(i=>i>0);return e.priorWeights.forEach((i,l)=>{let s=a[l]?r[o++]??0:0;i>0&&t.push(s>1-10*Ct?1:s)}),t}function cl(e){let t=e.slice().sort((r,o)=>r-o),n=t.length>>1;return t.length%2===1?t[n]:(t[n-1]+t[n])/2}function fl(e,t={}){let n=Wr(e),r=t.dispersion??Dt(e),o=zr(e,"deviance").filter((i,l)=>e.priorWeights[l]>0),a=100*Ct*cl(o.map(Math.abs));return o.map((i,l)=>{let s=(Math.abs(i)<a?0:i)/Math.sqrt(r*(1-n[l]));return Number.isFinite(s)?s:NaN})}function ml(e,t={}){let n=Wr(e),r=t.dispersion??Dt(e);return zr(e,"pearson").filter((a,i)=>e.priorWeights[i]>0).map((a,i)=>{let l=(a/(1-n[i]))**2*(n[i]/(r*e.rank));return Number.isFinite(l)?l:NaN})}function ul(e,t={}){let n=Un(e),r,o;if(t.newdata===void 0)r=e.model.x,o=e.linearPredictors.slice();else{r=$s(t.newdata).map(m=>e.model.intercept?[1,...m]:m);let f=t.newoffset===void 0?r.map(()=>0):Array.from(t.newoffset);if(f.length!==r.length)throw new RangeError("newoffset must have one value per row of newdata");o=r.map((m,u)=>m.reduce((h,p,d)=>Number.isNaN(e.coefficients[d])?h:h+p*e.coefficients[d],0)+f[u])}let a=t.dispersion??Dt(e),i=e.rank,l=e.qr===void 0?[]:e.qr.pivot.slice(0,i),s=e.qr?.qr,c=r.map(f=>{let m=new Array(i).fill(0),u=0;for(let h=0;h<i;h++){let p=f[l[h]];for(let d=0;d<h;d++)p-=s[d][h]*m[d];m[h]=p/s[h][h],u+=m[h]*m[h]}return Math.sqrt(u)*Math.sqrt(a)});return(t.type??"link")==="link"?{fit:o,seFit:c}:Wn(new Ne,()=>({fit:o.map(f=>n.linkinv(f)),seFit:c.map((f,m)=>f*Math.abs(n.muEta(o[m])))}))}var js={epsilon:1e-300,maxit:100,untilCoefficients:!0};function Gr(e,t,n,r,o=1e-11){let a=[];for(let d=0;d<t.length;d++)n[d]>0&&a.push(d);let i=0,l=0;for(let d of a)i+=n[d],l+=n[d]*t[d];let s=r?l/i:0,c=0;for(let d of a)c+=n[d]*(t[d]-s)**2;if((e.length>0?e[0].length:0)===0||a.length===0)return 0;let m=a.map(d=>Math.sqrt(n[d])),u=he(a.map((d,w)=>e[d].map(b=>b*m[w])),{tol:o}),h=qn(u,a.map((d,w)=>t[d]*m[w])),p=0;for(let d of h)p+=d*d;return c-p}function hl(e,t={}){let n=t.level??.95,r=t.test??"LRT";if(!(n>0&&n<1))throw new RangeError("'level' must be in (0, 1)");let o=e.coefficients.length,a=t.parm??Array.from({length:o},(E,M)=>M);for(let E of a)if(!Number.isInteger(E)||E<0||E>=o)throw new RangeError(`invalid coefficient index ${E}`);let i=e.model,l=e.coefficients.map((E,M)=>M).filter(E=>!Number.isNaN(e.coefficients[E])),s=new Ne,c=Vt(i,l,s,{...js,start:i.start===void 0?void 0:l.map(E=>i.start[E])}),f=new Array(o).fill(NaN);l.forEach((E,M)=>f[E]=c.fit.coefficients[M]);let m={...e,coefficients:f,weights:c.fit.workingWeights,residuals:c.fit.workingResiduals,qr:c.fit.qr,rank:c.fit.rank},u=jr(m),h=u.dispersion,p=new Array(o).fill(NaN);(c.fit.qr===void 0?[]:c.fit.qr.pivot.slice(0,c.fit.rank)).forEach((E,M)=>p[l[E]]=u.stdError[M]);let w=i.x.map(E=>l.map(M=>E[M])),b=c.fit.eta,g=(E,M)=>{let x=l.filter(S=>S!==E),q=Vt(i,x,new Ne,{...js,start:void 0,etastart:b,extraOffset:i.x.map(S=>S[E]*M)}).fit,k=r==="LRT"?q.deviance-c.fit.deviance:Gr(w,q.workingResiduals,q.workingWeights,!1);return Math.sign(M-f[E])*Math.sqrt(Math.max(k/h,0))},v=[H((1-n)/2),H((1+n)/2)],N=a.map(()=>NaN),y=a.map(()=>NaN);return a.forEach((E,M)=>{if(!Number.isNaN(f[E]))for(let x of[0,1]){let q=x===0?-1:1,k=NaN,S=NaN;for(let C=0;C<=30;C++){let z=f[E]+q*2**C*p[E],J=g(E,z);if(Math.abs(J)>=Math.abs(v[x])){k=z,S=J;break}}if(Number.isNaN(k))continue;let L=C=>C===f[E]?-v[x]:g(E,C)-v[x],[A,R]=x===0?[k,f[E]]:[f[E],k],[V,F]=x===0?[S-v[x],-v[x]]:[-v[x],S-v[x]],I=xe(L,A,R,{tol:1e-14*p[E],maxIter:500,fLower:V,fUpper:F});x===0?N[M]=I:y[M]=I}}),{lower:N,upper:y}}var pl=(e,t)=>me(e,t/2,2,!1,!1);function dl(e,t,n){let{fit:r,w:o}=Vt(e,t,n);return{dfRes:o.filter(i=>i>0).length-r.rank,dev:r.deviance,fit:r,x:e.x.map(i=>t.map(l=>i[l])),intercept:e.intercept}}var Ur=(e,t)=>Gr(t.x,e.fit.workingResiduals,e.fit.workingWeights,t.intercept);function gl(e,t={}){let n=Array.isArray(e)?e:[e];if(n.length===0)throw new RangeError("no fits to compare");let r=new Ne,o=Array.isArray(e)&&n.length>1,a=n[0],i=sn(a.model.familySpec),l=i.fixedDispersion,s=t.test===void 0?t.dispersion!==void 0||l?"Chisq":"F":t.test,c=[],f,m,u=[],h;if(o){let M=n[0].model.x.length;if(n.some(x=>x.model.x.length!==M))throw new RangeError("models were not all fitted to the same size of dataset");if(n.forEach((x,q)=>c.push({name:String(q+1),dfRes:x.dfResidual,dev:x.deviance})),f=c.map((x,q)=>q===0?NaN:c[q-1].dfRes-x.dfRes),m=c.map((x,q)=>q===0?NaN:c[q-1].dev-x.dev),s==="Rao"){let x=n.map(q=>({dfRes:q.dfResidual,dev:q.deviance,fit:zs(q),x:q.model.x,intercept:q.model.intercept}));u=c.map((q,k)=>k===0?NaN:f[k]<0?-Ur(x[k],x[k-1]):Ur(x[k-1],x[k]))}h=n.reduce((x,q)=>q.dfResidual<x.dfResidual?q:x)}else{let M=a,x=M.model,q=x.x.length>0?x.x[0].length-(x.intercept?1:0):0,k=x.intercept?[0]:[],S=A=>[...k,...Array.from({length:A},(R,V)=>V+k.length)],L=[];for(let A=0;A<=q;A++)A===q?L.push({dfRes:M.dfResidual,dev:M.deviance,fit:zs(M),x:x.x,intercept:x.intercept}):A>0||s==="Rao"?L.push(dl(x,S(A),r)):L.push(void 0);c.push({name:"NULL",dfRes:M.dfNull,dev:M.nullDeviance});for(let A=1;A<=q;A++)c.push({name:`x${A}`,dfRes:L[A].dfRes,dev:L[A].dev});f=c.map((A,R)=>R===0?NaN:c[R-1].dfRes-A.dfRes),m=c.map((A,R)=>R===0?NaN:Math.max(c[R-1].dev-A.dev,0)),s==="Rao"&&(u=c.map((A,R)=>R===0?NaN:Ur(L[R-1],L[R]))),h=M}let p=new Ne,d=t.dispersion??Dt(h,p);p.list.forEach(M=>r.add(M));let w=l?1/0:h.dfResidual,b=o?["Resid. Df","Resid. Dev","Df","Deviance"]:["Df","Deviance","Resid. Df","Resid. Dev"],g=c.map((M,x)=>o?[M.dfRes,M.dev,f[x],m[x]]:[f[x],m[x],M.dfRes,M.dev]),v=[...b],N=M=>c.map((x,q)=>{if(q===0||f[q]===0)return NaN;let k=M[q]/d*Math.sign(f[q]);return k<0||Number.isNaN(k)?NaN:pl(k,Math.abs(f[q]))}),y=[];if(s==="Chisq"||s==="LRT")v.push("Pr(>Chi)"),y=[N(m)];else if(s==="Rao")v.push("Rao","Pr(>Chi)"),y=[u,N(u)];else if(s==="F"){if(l&&r.add(`using F test with a '${i.family}' family is inappropriate`),w!==0){let M=c.map((x,q)=>{let k=m[q]/f[q]/d;return q===0||f[q]===0||k<0?NaN:k});v.push("F","Pr(>F)"),y=[M,M.map((x,q)=>Number.isNaN(x)?NaN:ae(x,Math.abs(f[q]),w,!1,!1))]}}else if(s==="Cp"){let M=h.model.x.length;v.push("Cp"),y=[c.map(x=>x.dev+2*d*(M-x.dfRes))]}let E=g.map((M,x)=>[...M,...y.map(q=>q[x])]);return{colNames:v,rowNames:c.map(M=>M.name),table:E,warnings:r.list}}function zs(e){return{coefficients:e.coefficients,eta:e.linearPredictors,mu:e.fittedValues,deviance:e.deviance,iter:e.iter,converged:e.converged,boundary:e.boundary,rank:e.rank,qr:e.qr,used:e.weights.map(t=>t>0),workingWeights:e.weights,workingResiduals:e.residuals}}function wl(e){let{pivot:t}=e.qr,n=[],r=new Map;for(let o=0;o<e.rank;o++){let a=e.assign[t[o]],i=r.get(a);i===void 0&&(i={term:a,df:0,ss:0},r.set(a,i),n.push(i)),i.df++,i.ss+=e.effects[o]**2}return{terms:n,rdf:e.dfResidual,rss:e.rss}}var Ws=(e,t,n)=>ae(e,t,n,!1,!1);function vl(e){let t=wl(e),n=t.terms.filter(o=>o.term!==0),r=0;return e.fittedValues.forEach((o,a)=>r+=(e.weights?.[a]??1)*o*o),{names:n.map(o=>e.termLabels[o.term-1]),rows:n,rdf:t.rdf,rss:t.rss,perfect:t.rss<1e-10*r}}function Hr(e){let{names:t,rows:n,rdf:r,rss:o,perfect:a}=vl(e),i=o/r,l=n.map(({df:s,ss:c})=>{let f=c/s/i;return[s,c,c/s,f,Ws(f,s,r)]});return l.push([r,o,r===0?NaN:i,NaN,NaN]),{colNames:["Df","Sum Sq","Mean Sq","F value","Pr(>F)"],rowNames:[...t,"Residuals"],table:l,warnings:a?["ANOVA F-tests on an essentially perfect fit are unreliable"]:[]}}function bl(e){let t=e.map(i=>i.dfResidual),n=e.map(i=>i.rss),r=0;t.forEach((i,l)=>{i<t[r]&&(r=l)});let o=n[r]/t[r],a=e.map((i,l)=>{if(l===0)return[t[0],n[0],NaN,NaN,NaN,NaN];let s=t[l-1]-t[l],c=n[l-1]-n[l],f=s===0?NaN:c/s/o;return f<0&&(f=NaN),[t[l],n[l],s,c,f,Number.isNaN(f)?NaN:Ws(f,Math.abs(s),t[r])]});return{colNames:["Res.Df","RSS","Df","Sum of Sq","F","Pr(>F)"],rowNames:e.map((i,l)=>String(l+1)),table:a,warnings:[]}}function Br(e,...t){let n=[e,...t];for(let i of n)if(typeof i!="object"||i===null||!Array.isArray(i.effects))throw new TypeError("anova() needs fits from lm() or aov() with a formula");if(n.length===1)return Hr(e);if(n.some(i=>i.rows.length!==e.rows.length))throw new Error("models were not all fitted to the same size of dataset");let r=n.filter(i=>i.response===e.response),o=[];if(r.length<n.length){let i=[...new Set(n.filter(l=>l.response!==e.response).map(l=>l.response))];o.push(`models with response ${i.map(l=>`\u2018${l}\u2019`).join(", ")} removed because response differs from model 1`)}let a=r.length===1?Hr(e):bl(r);return a.warnings=[...o,...a.warnings],a}function Xr(e){if(typeof e=="object"&&e!==null&&e.multistratum===!0)return{strata:e.strata.filter(r=>r.stratum!=="Error: (Intercept)").map(r=>({...r})),warnings:[]};if(typeof e!="object"||e===null||!Array.isArray(e.effects))throw new TypeError("aovSummary() needs a fit from aov() with a formula");let t=Hr(e);return e.dfResidual>0?{...t,warnings:[]}:{colNames:t.colNames.slice(0,3),rowNames:t.rowNames.slice(0,-1),table:t.table.slice(0,-1).map(n=>n.slice(0,3)),warnings:[]}}function Gs(e,t,n){let r=[...t,...n];if(r.length===0)return{ss:0,df:0};let o=e.y.flatMap((u,h)=>e.weights===void 0||e.weights[h]!==0?[h]:[]),a=o.map(u=>e.weights===void 0?1:Math.sqrt(e.weights[u])),i=he(o.map((u,h)=>r.map(p=>e.x[u][p]*a[h]))),l=jn(e.weights===void 0?void 0:o.map(u=>e.weights[u]),o.length),s=i.pivot.slice(0,i.rank),{qty:c}=Ot(s.map(u=>o.map((h,p)=>Te(l[p],e.x[h][r[u]]))),o.map((u,h)=>Te(l[h],e.y[u]))),f=[0,0],m=0;return s.forEach((u,h)=>{u>=t.length&&(f=B(f,oe(c[h],c[h])),m++)}),{ss:f[0],df:m}}function Qr(e,t={}){if(typeof e!="object"||e===null||!Array.isArray(e.effects))throw new TypeError("Anova() needs a fit from lm() with a formula");let n=t.type??2,r;if(n===2||n==="II")r=2;else if(n===3||n==="III")r=3;else throw new RangeError(`invalid type (${String(n)}): use 2 or 3`);let o=e.dfResidual,a=e.rss;if(o===0)throw new Error("residual df = 0");if(a<Math.sqrt(2220446049250313e-31))throw new Error("residual sum of squares is 0 (within rounding error)");let i=[],l=e.termLabels.length;r===2&&l===0&&e.intercept&&(i.push("the model contains only an intercept: Type III test substituted"),r=3);let s=p=>e.assign.flatMap((d,w)=>p.has(d)?[w]:[]),c=[...new Set(e.assign)],f=[],m=[];if(r===3){if(e.coefficients.some(p=>Number.isNaN(p)))throw new Error("there are aliased coefficients in the model");for(let p of c){let d=s(new Set([p])),w=s(new Set(c.filter(b=>b!==p)));f.push(p===0?"(Intercept)":e.termLabels[p-1]),m.push({ss:Gs(e,w,d).ss,df:d.length})}}else{let p=d=>e.factors.flatMap((w,b)=>w[d-1]>0?[b]:[]);for(let d of c){if(d===0)continue;let w=p(d),b=c.filter(N=>N!==0&&N!==d&&w.every(y=>e.factors[y][N-1]>0)),g=s(new Set(c.filter(N=>N!==d&&!b.includes(N)))),v=Gs(e,g,s(new Set([d])));f.push(e.termLabels[d-1]),m.push(v)}}let u=a/o,h=m.map(({ss:p,df:d})=>{let w=p/d/u;return[p,d,w,ae(w,d,o,!1,!1)]});return h.push([a,o,NaN,NaN]),{colNames:["Sum Sq","Df","F value","Pr(>F)"],rowNames:[...f,"Residuals"],table:h,warnings:i}}var Hn=[0,0];var Us=e=>e.map(t=>t.map(n=>n[0]));function an(e,t){let n=t[0]?.length??0;return e.map(r=>{let o=new Array(n).fill(Hn);return r.forEach((a,i)=>{let l=t[i];for(let s=0;s<n;s++)o[s]=B(o[s],oe(a,l[s]))}),o})}function gt(e){let t=Hn;return e.forEach((n,r)=>t=B(t,n[r])),t}function Hs(e){let t=e.length,n=e.map(i=>i.slice()),r=Array.from({length:t},(i,l)=>l),o=1,a=!1;for(let i=0;i<t;i++){let l=i;for(let s=i+1;s<t;s++)Math.abs(n[s][i][0])>Math.abs(n[l][i][0])&&(l=s);if(n[l][i][0]===0){a=!0;continue}l!==i&&([n[i],n[l]]=[n[l],n[i]],[r[i],r[l]]=[r[l],r[i]],o=-o);for(let s=i+1;s<t;s++){let c=ge(n[s][i],n[i][i]);n[s][i]=c;for(let f=i+1;f<t;f++)n[s][f]=le(n[s][f],oe(c,n[i][f]))}}return{a:n,perm:r,sign:o,singular:a}}function Bs(e){let{a:t,sign:n,singular:r}=Hs(e);if(r)return Hn;let o=[n,0];return t.forEach((a,i)=>o=oe(o,a[i])),o}function Xs(e){let t=e.length,{a:n,perm:r,singular:o}=Hs(e);if(o)throw new Error("singular matrix");let a=Array.from({length:t},()=>new Array(t).fill(Hn));for(let i=0;i<t;i++){let l=r.map(s=>[s===i?1:0,0]);for(let s=0;s<t;s++)for(let c=0;c<s;c++)l[s]=le(l[s],oe(n[s][c],l[c]));for(let s=t-1;s>=0;s--){for(let c=s+1;c<t;c++)l[s]=le(l[s],oe(n[s][c],l[c]));l[s]=ge(l[s],n[s][s])}for(let s=0;s<t;s++)a[s][i]=l[s]}return a}var Qs=(e,t)=>me(e,t/2,2,!1,!1);function Kr(e){let t=e.length,n=Xt(gt(e),t),r=[1,0];for(let o=0;o<t;o++)r=oe(r,n);return n=ge(Bs(e),r),n[0]}function Zr(e,t,n,r){let o=1-(2*t*t+t+2)/(6*t*n),a=(t+2)*(t-1)*(t-2)*(2*t**3+6*t*t+3*r+2)/(288*(n*t*o)**2),i=-n*o*Math.log(e),l=t*(t+1)/2-1,s=Qs(i,l),c=Qs(i,l+4);return s+a*(c-s)}var Nl=(e,t)=>Array.from({length:t},(n,r)=>e.map(o=>o[r]));function yl(e,t,n){let r=e.length,o=b=>b.length===0?0:he(Array.from({length:r},(g,v)=>b.map(N=>N[v]))).rank,a=n.length===0?void 0:he(Array.from({length:r},(b,g)=>n.map(v=>v[g]))),i=a===void 0?[]:a.pivot.slice(0,a.rank).map(b=>n[b]),l=i.length,s=[...i,...t],c=he(Array.from({length:r},(b,g)=>s.map(v=>v[g])));if(c.rank>o(t))throw new Error("X does not define a subspace of M");let f=c.pivot.slice(0,c.rank).map(b=>s[b]),m=b=>b.map(g=>[g,0]),u=new Array(r).fill([0,0]),h=je(f.map(m),u,e.map((b,g)=>e.map(v=>v[g]))).qtv,p=Array.from({length:r},(b,g)=>h.map(v=>v[g])),d=je(f.map(m),u,p).qtv,w=Array.from({length:c.rank-l},(b,g)=>l+g);return w.map(b=>w.map(g=>d[b][g]))}function Ml(e,t={}){let n=e.map(g=>Array.from(g)),r=n.length,o=n[0]?.length??0;if(o===0||n.some(g=>g.length!==o))throw new TypeError("y must be a matrix with at least one column");let a=t.group;if(a!==void 0&&a.length!==r)throw new RangeError("'group' must have one label per row of y");let i=a?.map(String)??n.map(()=>""),l=[...new Set(i)].sort(ct),s=n.map(g=>g.map(()=>[0,0]));for(let g of l){let v=i.flatMap((N,y)=>N===g?[y]:[]);for(let N=0;N<o;N++){let y=[0,0];for(let M of v)y=B(y,[n[M][N],0]);let E=Xt(y,v.length);for(let M of v)s[M][N]=le([n[M][N],0],E)}}let c=r-l.length,f=Array.from({length:o},(g,v)=>Array.from({length:o},(N,y)=>{let E=[0,0];for(let M of s)E=B(E,oe(M[v],M[y]));return E})),m=t.idata??{index:Array.from({length:o},(g,v)=>v+1)},u=g=>{let v=Ft(g,m);if(v.x.length!==o)throw new RangeError("idata must have one row per column of y");return Nl(v.x,v.colNames.length)},h=t.M===void 0?Array.from({length:o},(g,v)=>Array.from({length:o},(N,y)=>y===v?1:0)):u(t.M),p=u(t.X??"~0"),d=yl(f,h,p),w=d.length,b=Kr(d);return{statistic:b,statisticName:"W",pValue:Zr(b,w,c,o),method:"Mauchly's test of sphericity"}}var Ks=Number.EPSILON**.25;function ie(e,t){if(e===void 0)return t[0];let n=t.find(o=>o===e);if(n!==void 0)return n;let r=t.filter(o=>o.startsWith(e));if(e.length===0||r.length!==1)throw new RangeError(`'arg' should be one of ${t.map(o=>`\u201C${o}\u201D`).join(", ")}`);return r[0]}var j=e=>e==null;function Se(e,t){if(e.filter(j).length!==1)throw new RangeError(t)}function Y(e,t,n,r={}){return ft(e,t,n,r).root}var wt=(e,t,n,r)=>Nn(e,t,{ncp:n,lowerTail:r});function Yr(e,t){if(e!=null&&!(typeof e=="number"&&e>=0&&e<=1))throw new RangeError(t)}var Jr="'sig.level' must be numeric in [0, 1]";function xl(e={}){let{n:t,delta:n,power:r}=e,o=e.sd===void 0?1:e.sd,a=e.sigLevel===void 0?.05:e.sigLevel;Se([t,n,o,r,a],"exactly one of 'n', 'delta', 'sd', 'power', and 'sig.level' must be NULL"),Yr(a,Jr);let i=ie(e.type,["two.sample","one.sample","paired"]),l=ie(e.alternative,["two.sided","one.sided"]),s=e.strict??!1,c=e.tol??Ks,f=i==="two.sample"?2:1,m=l==="two.sided"?2:1;m===2&&!j(n)&&(n=Math.abs(n));let u=(d,w,b,g)=>{let v=Math.max(1e-7,d-1)*f,N=lt(g/m,v,{lowerTail:!1}),y=Math.sqrt(d/f)*w/b,E=wt(N,v,y,!1);return s&&m===2?E+wt(-N,v,y,!0):E};if(j(r))r=u(t,n,o,a);else{let d=r;j(t)?t=Y(w=>u(w,n,o,a)-d,2,1e7,{tol:c,extendInt:"upX"}):j(o)?o=Y(w=>u(t,n,w,a)-d,n*1e-7,n*1e7,{tol:c,extendInt:"downX"}):j(n)?n=Y(w=>u(t,w,o,a)-d,o*1e-7,o*1e7,{tol:c,extendInt:"upX"}):a=Y(w=>u(t,n,o,w)-d,1e-10,1-1e-10,{tol:c,extendInt:"yes"})}let h=i==="paired"?"n is number of *pairs*, sd is std.dev. of *differences* within pairs":i==="two.sample"?"n is number in *each* group":void 0,p={"two.sample":"Two-sample","one.sample":"One-sample",paired:"Paired"}[i]+" t test power calculation";return{n:t,delta:n,sd:o,sigLevel:a,power:r,alternative:l,...h===void 0?{}:{note:h},method:p}}function El(e={}){let{n:t,p1:n,p2:r,power:o}=e,a=e.sigLevel===void 0?.05:e.sigLevel;Se([t,n,r,o,a],"exactly one of 'n', 'p1', 'p2', 'power', and 'sig.level' must be NULL"),Yr(a,Jr);let i=ie(e.alternative,["two.sided","one.sided"]),l=e.strict??!1,s=e.tol??Ks,c=i==="two.sided"?2:1,f=(u,h,p,d)=>{let w=H(d/c,{lowerTail:!1});if(l&&c===2){let b=(h+p)/2,g=h*(1-h),v=p*(1-p),N=b*(1-b),y=Math.abs(h-p),E=Math.sqrt(g+v);return P((Math.sqrt(u)*y-w*Math.sqrt(2*N))/E)+P((Math.sqrt(u)*y+w*Math.sqrt(2*N))/E,{lowerTail:!1})}return P((Math.sqrt(u)*Math.abs(h-p)-w*Math.sqrt((h+p)*(1-(h+p)/2)))/Math.sqrt(h*(1-h)+p*(1-p)))},m=[];if(j(o))o=f(t,n,r,a);else{let u=o;j(t)?t=Y(h=>f(h,n,r,a)-u,1,1e7,{tol:s,extendInt:"upX"}):j(n)?(n=Y(h=>f(t,h,r,a)-u,0,r,{tol:s,extendInt:"yes"}),n<0&&m.push("No p1 in [0, p2] can be found to achieve the desired power")):j(r)?(r=Y(h=>f(t,n,h,a)-u,n,1,{tol:s,extendInt:"yes"}),r>1&&m.push("No p2 in [p1, 1] can be found to achieve the desired power")):(a=Y(h=>f(t,n,r,h)-u,1e-10,1-1e-10,{tol:s,extendInt:"upX"}),(a<0||a>1)&&m.push("No significance level [0, 1] can be found to achieve the desired power"))}return{n:t,p1:n,p2:r,sigLevel:a,power:o,alternative:i,note:"n is number in *each* group",method:"Two-sample comparison of proportions power calculation",warnings:m}}function kl(e={}){let{groups:t,n,betweenVar:r,withinVar:o,power:a}=e,i=e.sigLevel===void 0?.05:e.sigLevel;if(Se([t,n,r,o,a,i],"exactly one of 'groups', 'n', 'between.var', 'within.var', 'power', and 'sig.level' must be NULL"),!j(t)&&t<2)throw new RangeError("number of groups must be at least 2");if(!j(n)&&n<2)throw new RangeError("number of observations in each group must be at least 2");Yr(i,Jr);let l=(s,c,f,m,u)=>{let h=(s-1)*c*f/m,p=s-1,d=(c-1)*s;return at(Qe(u,p,d,{lowerTail:!1}),p,d,{ncp:h,lowerTail:!1})};if(j(a))a=l(t,n,r,o,i);else{let s=a;j(t)?t=Y(c=>l(c,n,r,o,i)-s,2,100):j(n)?n=Y(c=>l(t,c,r,o,i)-s,2,1e5):j(o)?o=Y(c=>l(t,n,r,c,i)-s,r*1e-7,r*1e7):j(r)?r=Y(c=>l(t,n,c,o,i)-s,o*1e-7,o*1e7):i=Y(c=>l(t,n,r,o,c)-s,1e-10,1-1e-10)}return{groups:t,n,betweenVar:r,withinVar:o,sigLevel:i,power:a,note:"n is number in each group",method:"Balanced one-way analysis of variance power calculation"}}var ql={p:[.2,.5,.8],t:[.2,.5,.8],r:[.1,.3,.5],anov:[.1,.25,.4],chisq:[.1,.3,.5],f2:[.02,.15,.35]};function Zs(e="p",t="small"){let n=ie(e,["p","t","r","anov","chisq","f2"]),r=ie(t,["small","medium","large"]);return{test:n,size:r,effectSize:ql[n][["small","medium","large"].indexOf(r)],method:"Conventional effect size from Cohen (1982)"}}function Al(e,t){return 2*Math.asin(Math.sqrt(e))-2*Math.asin(Math.sqrt(t))}function Rl(e,t){if(e.length!==t.length)throw new RangeError("'P0' and 'P1' must have the same length");let n=0;for(let r=0;r<e.length;r++)n+=(t[r]-e[r])**2/e[r];return Math.sqrt(n)}function Tl(e){let t=e.map(a=>a.reduce((i,l)=>i+l,0)),n=e[0]?.length??0,r=Array.from({length:n},(a,i)=>e.reduce((l,s)=>l+s[i],0)),o=0;for(let a=0;a<n;a++)for(let i=0;i<e.length;i++){let l=t[i]*r[a];o+=(e[i][a]-l)**2/l}return Math.sqrt(o)}var Sl="\u2018sig.level\u2019 must be numeric in [0, 1]",Ll="\u2018power\u2019 must be numeric in [0, 1]";function Ye(e,t){let n=r=>!j(r)&&!(typeof r=="number"&&r>=0&&r<=1);if(n(e))throw new RangeError(Sl);if(n(t))throw new RangeError(Ll)}function Je(e,t){return typeof e=="string"?Zs(t,e).effectSize:e}var ln=["two.sided","less","greater"];function Bn(e,t){return e==="two.sided"?t:e==="less"?[-10,5]:[-5,10]}function Ys(e,t,n,r){if(r==="less")return wt(lt(n,e),e,t,!0);if(r==="greater")return wt(lt(n,e,{lowerTail:!1}),e,t,!1);let o=lt(n/2,e,{lowerTail:!1});return wt(o,e,t,!1)+wt(-o,e,t,!0)}function Js(e,t,n){return n==="greater"?P(H(1-t)-e,{lowerTail:!1}):n==="less"?P(H(t)-e):P(H(1-t/2)-e,{lowerTail:!1})+P(H(t/2)-e)}function Il(e={}){let{n:t,power:n}=e,r=Je(e.d,"t"),o=e.sigLevel===void 0?.05:e.sigLevel;Se([t,r,n,o],"exactly one of n, d, power, and sig.level must be NULL"),Ye(o,n);let a=ie(e.type,["two.sample","one.sample","paired"]),i=ie(e.alternative,ln),l=a==="two.sample"?2:1;i==="two.sided"&&!j(r)&&(r=Math.abs(r));let s=(m,u,h)=>Ys((m-1)*l,Math.sqrt(m/l)*u,h,i);if(j(n))n=s(t,r,o);else{let m=n;j(t)?t=Y(u=>s(u,r,o)-m,2+1e-10,1e9):j(r)?r=Y(u=>s(t,u,o)-m,...Bn(i,[1e-7,10])):o=Y(u=>s(t,r,u)-m,1e-10,1-1e-10)}let c=a==="paired"?"n is number of *pairs*":a==="two.sample"?"n is number in *each* group":void 0,f={"two.sample":"Two-sample","one.sample":"One-sample",paired:"Paired"}[a]+" t test power calculation";return{n:t,d:r,sigLevel:o,power:n,alternative:i,...c===void 0?{}:{note:c},method:f}}function Ol(e={}){let{n1:t,n2:n,power:r}=e,o=Je(e.d,"t"),a=e.sigLevel===void 0?.05:e.sigLevel;if(Se([t,n,o,r,a],"exactly one of n1, n2, d, power, and sig.level must be NULL"),Ye(a,r),!j(t)&&t<2)throw new RangeError("number of observations in the first group must be at least 2");if(!j(n)&&n<2)throw new RangeError("number of observations in the second group must be at least 2");let i=ie(e.alternative,ln);i==="two.sided"&&!j(o)&&(o=Math.abs(o));let l=(s,c,f,m)=>Ys(s+c-2,f/Math.sqrt(1/s+1/c),m,i);if(j(r))r=l(t,n,o,a);else{let s=r;j(t)?t=Y(c=>l(c,n,o,a)-s,2+1e-10,1e9):j(n)?n=Y(c=>l(t,c,o,a)-s,2+1e-10,1e9):j(o)?o=Y(c=>l(t,n,c,a)-s,...Bn(i,[1e-7,10])):a=Y(c=>l(t,n,o,c)-s,1e-10,1-1e-10)}return{n1:t,n2:n,d:o,sigLevel:a,power:r,alternative:i,method:"t test power calculation"}}function eo(e,t,n,r,o,a){let{n:i,power:l}=t,s=Je(e,n==="h"?"p":"t"),c=t.sigLevel===void 0?.05:t.sigLevel;if(Se([s,i,l,c],`exactly one of ${n}, n, power, and sig.level must be NULL`),Ye(c,l),!j(i)&&i<1)throw new RangeError("number of observations in each group must be at least 1");let f=ie(t.alternative,ln);f==="two.sided"&&!j(s)&&(s=Math.abs(s));let m=(u,h,p)=>Js(u*r(h),p,f);if(j(l))l=m(s,i,c);else{let u=l;j(s)?s=Y(h=>m(h,i,c)-u,...Bn(f,[a,10])):j(i)?i=Y(h=>m(s,h,c)-u,o,1e9):c=Y(h=>m(s,i,h)-u,1e-10,1-1e-10)}return{es:s,n:i,sigLevel:c,power:l,alternative:f}}function Fl(e={}){let t=eo(e.h,e,"h",n=>Math.sqrt(n/2),2.0000000001,1e-10);return{h:t.es,n:t.n,sigLevel:t.sigLevel,power:t.power,alternative:t.alternative,method:"Difference of proportion power calculation for binomial distribution (arcsine transformation)",note:"same sample sizes"}}function _l(e={}){let t=eo(e.h,e,"h",n=>Math.sqrt(n),2.0000000001,1e-10);return{h:t.es,n:t.n,sigLevel:t.sigLevel,power:t.power,alternative:t.alternative,method:"proportion power calculation for binomial distribution (arcsine transformation)"}}function Cl(e={}){let t=eo(e.d,e,"d",n=>Math.sqrt(n),1.0000000001,1e-10);return{d:t.es,n:t.n,sigLevel:t.sigLevel,power:t.power,alternative:t.alternative,method:"Mean power calculation for normal distribution with known variance"}}function Vl(e={}){let{n1:t,n2:n,power:r}=e,o=Je(e.h,"p"),a=e.sigLevel===void 0?.05:e.sigLevel;if(Se([o,t,n,r,a],"exactly one of h, n1, n2, power, and sig.level must be NULL"),Ye(a,r),!j(t)&&t<2)throw new RangeError("number of observations in the first group must be at least 2");if(!j(n)&&n<2)throw new RangeError("number of observations in the second group must be at least 2");let i=ie(e.alternative,ln);i==="two.sided"&&!j(o)&&(o=Math.abs(o));let l=(s,c,f,m)=>Js(s*Math.sqrt(c*f/(c+f)),m,i);if(j(r))r=l(o,t,n,a);else{let s=r;j(o)?o=Y(c=>l(c,t,n,a)-s,...Bn(i,[1e-10,10])):j(t)?t=Y(c=>l(o,c,n,a)-s,2+1e-10,1e9):j(n)?n=Y(c=>l(o,t,c,a)-s,2+1e-10,1e9):a=Y(c=>l(o,t,n,c)-s,1e-10,1-1e-10)}return{h:o,n1:t,n2:n,sigLevel:a,power:r,alternative:i,method:"difference of proportion power calculation for binomial distribution (arcsine transformation)",note:"different sample sizes"}}function Dl(e={}){let{n:t,power:n}=e,r=Je(e.r,"r"),o=e.sigLevel===void 0?.05:e.sigLevel;if(Se([t,r,n,o],"exactly one of n, r, power, and sig.level must be NULL"),Ye(o,n),!j(t)&&t<4)throw new RangeError("number of observations must be at least 4");let a=ie(e.alternative,ln);a==="two.sided"&&!j(r)&&(r=Math.abs(r));let i=(s,c,f,m)=>{let u=lt(m?f/2:f,s-2,{lowerTail:!1}),h=Math.sqrt(u**2/(u**2+s-2)),p=Math.atanh(c)+c/(2*(s-1)),d=Math.atanh(h),w=P((p-d)*Math.sqrt(s-3));return m?w+P((-p-d)*Math.sqrt(s-3)):w},l=(s,c,f)=>a==="two.sided"?i(s,c,f,!0):i(s,a==="less"?-c:c,f,!1);if(j(n))n=l(t,r,o),a==="less"&&(r=-r);else{let s=n;if(j(t))t=Y(c=>l(c,r,o)-s,4+1e-10,1e9);else if(j(r)){let[c,f]=a==="two.sided"?[1e-10,.9999999999]:[-.9999999999,.9999999999];r=Y(m=>l(t,m,o)-s,c,f)}else o=Y(c=>l(t,r,c)-s,1e-10,1-1e-10)}return{n:t,r,sigLevel:o,power:n,alternative:a,method:"approximate correlation power calculation (arctangh transformation)"}}function $l(e){let{N:t,power:n}=e,{df:r}=e,o=Je(e.w,"chisq"),a=e.sigLevel===void 0?.05:e.sigLevel;if(Se([o,t,n,a],"exactly one of w, N, power, and sig.level must be NULL"),typeof r!="number")throw new TypeError('argument "df" is missing, with no default');if(Ye(a,n),!j(o)&&o<0)throw new RangeError("w must be positive");if(!j(t)&&t<1)throw new RangeError("number of observations must be at least 1");let i=(l,s,c)=>$e(Io(c,r,{lowerTail:!1}),r,{ncp:s*l**2,lowerTail:!1});if(j(n))n=i(o,t,a);else{let l=n;j(o)?o=Y(s=>i(s,t,a)-l,1e-10,1e9):j(t)?t=Y(s=>i(o,s,a)-l,1+1e-10,1e9):a=Y(s=>i(o,t,s)-l,1e-10,1-1e-10)}return{w:o,N:t,df:r,sigLevel:a,power:n,method:"Chi squared power calculation",note:"N is the number of observations"}}function Pl(e={}){let{k:t,n,power:r}=e,o=Je(e.f,"anov"),a=e.sigLevel===void 0?.05:e.sigLevel;if(Se([t,n,o,r,a],"exactly one of k, n, f, power, and sig.level must be NULL"),Ye(a,r),!j(o)&&o<0)throw new RangeError("f must be positive");if(!j(t)&&t<2)throw new RangeError("number of groups must be at least 2");if(!j(n)&&n<2)throw new RangeError("number of observations in each group must be at least 2");let i=(l,s,c,f)=>{let m=l-1,u=(s-1)*l;return at(Qe(f,m,u,{lowerTail:!1}),m,u,{ncp:l*s*c**2,lowerTail:!1})};if(j(r))r=i(t,n,o,a);else{let l=r;j(t)?t=Y(s=>i(s,n,o,a)-l,2+1e-10,100):j(n)?n=Y(s=>i(t,s,o,a)-l,2+1e-10,1e9):j(o)?o=Y(s=>i(t,n,s,a)-l,1e-7,1e7):a=Y(s=>i(t,n,o,s)-l,1e-10,1-1e-10)}return{k:t,n,f:o,sigLevel:a,power:r,note:"n is number in each group",method:"Balanced one-way analysis of variance power calculation"}}function jl(e={}){let{u:t,v:n,power:r}=e,o=Je(e.f2,"f2"),a=e.sigLevel===void 0?.05:e.sigLevel;if(Se([t,n,o,r,a],"exactly one of u, v, f2, power, and sig.level must be NULL"),Ye(a,r),!j(o)&&o<0)throw new RangeError("f2 must be positive");if(!j(t)&&t<1)throw new RangeError("degree of freedom u for numerator must be at least 1");if(!j(n)&&n<1)throw new RangeError("degree of freedom v for denominator must be at least 1");let i=(l,s,c,f)=>at(Qe(f,l,s,{lowerTail:!1}),l,s,{ncp:c*(l+s+1),lowerTail:!1});if(j(r))r=i(t,n,o,a);else{let l=r;j(t)?t=Y(s=>i(s,n,o,a)-l,1+1e-10,100):j(n)?n=Y(s=>i(t,s,o,a)-l,1+1e-10,1e9):j(o)?o=Y(s=>i(t,n,s,a)-l,1e-7,1e7):a=Y(s=>i(t,n,o,s)-l,1e-10,1-1e-10)}return{u:t,v:n,f2:o,sigLevel:a,power:r,method:"Multiple regression power calculation"}}var zl="Missing values detected. NAs dropped.",ra={tol:0,maxiter:5e3};function Wl(e){return Math.exp(ir(e/2)-Math.log(Math.sqrt(e/2))-ir((e-1)/2))}function ea(e,t,n){return ft(r=>Nn(e,t,{ncp:r})-n,e-1,e+1,{...ra,extendInt:"downX"}).root}function oa(e,t,n,r){let o=n.pooledSd??!0,a=n.mu??0,i=n.paired??!1,l=n.ci===void 0?.95:n.ci,s=ie(n.alternative,["two.sided","less","greater"]),c=Array.from(e),f=t===void 0?void 0:Array.from(t),m=[];(c.some(Number.isNaN)||(f?.some(Number.isNaN)??!1))&&m.push(zl);let u,h,p,d,w;if(f===void 0||i){let x;if(f===void 0)x=qe(c);else{let[k,S]=At(c,f);x=k.map((L,A)=>L-S[A])}let q=x.length;u=se(x),h=Math.sqrt(re(x)),p=h/Math.sqrt(q),d=1/q,w=q-1}else{let x=qe(c),q=qe(f),k=x.length,S=q.length;u=se(x)-se(q);let L=Math.sqrt(re(x)),A=Math.sqrt(re(q));o?(h=Math.sqrt(((k-1)*L**2+(S-1)*A**2)/(k+S-2)),p=h*Math.sqrt(1/k+1/S),d=1/k+1/S,w=k+S-2):(h=Math.sqrt((L**2+A**2)/2),d=2*(S*L**2+k*A**2)/(k*S*(L**2+A**2)),p=Math.sqrt(L**2/k+A**2/S),w=p**4/((L**2/k)**2/(k-1)+(A**2/S)**2/(S-1)))}let b=r?Wl(w):1,g=(u-a)/h*b,v=r?"Hedges_g":"Cohens_d";if(l===null)return{name:v,value:g,warnings:m};let N=s==="two.sided"?l:2*l-1,y=(u-a)/p,E=ea(y,w,(1+N)/2)*Math.sqrt(d)*b,M=ea(y,w,(1-N)/2)*Math.sqrt(d)*b;return s==="less"&&(E=-1/0),s==="greater"&&(M=1/0),{name:v,value:g,ci:l,ciLow:E,ciHigh:M,warnings:m}}function Gl(e,t,n={}){return oa(e,t,n,n.adjust??!1)}function Ul(e,t,n={}){return oa(e,t,n,!0)}var Hl=e=>Array.isArray(e.effects),sa=e=>e.multistratum===!0,Bl=e=>Array.isArray(e.strata),aa=e=>{let t=e;return typeof t.univariate=="object"&&typeof t.anova=="object"},Xl=e=>e.replace(/[.*+?^${}()|[\]\\]/g,"\\$&"),to=e=>new RegExp(`\\b${Xl(e)}\\b`);function Ql(e){let t=e.strata.filter(o=>o.stratum!=="Error: (Intercept)").map(o=>{let a=o.stratum.replace(/^Error: /,""),i=o.colNames.indexOf("Df"),l=o.colNames.indexOf("Sum Sq"),s=o.rowNames.map((f,m)=>({name:f,df:o.table[m][i],ss:o.table[m][l]})),c=s.find(f=>f.name==="Residuals");if(c===void 0)throw new Error(`the error stratum '${a}' has no residual degrees of freedom`);return{name:a,terms:s.filter(f=>f!==c),sse:c.ss,dfe:c.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 Kl(e){let t=e.univariate,n=t.colNames.indexOf("Sum Sq"),r=[],o=(s,c,f)=>{let m=r.find(u=>u.name===s);return m===void 0&&r.push(m={name:s,terms:[],sse:c,dfe:f}),m},a=new Map;if(t.colNames.includes("Error SS")){let s=t.colNames.indexOf("num Df"),c=t.colNames.indexOf("Error SS"),f=t.colNames.indexOf("den Df");t.rowNames.forEach((m,u)=>{let h=t.table[u],p=e.within.filter(b=>to(b).test(m)),d=o(p.length===0?"Subject":`Subject:${p.join(":")}`,h[c],h[f]);if(m==="(Intercept)")return;let w={name:m,df:h[s],ss:h[n]};d.terms.push(w),a.set(m,w)})}else{let s=t.colNames.indexOf("Df"),c=t.rowNames.indexOf("Residuals"),f=o("Subject",t.table[c][n],t.table[c][s]);t.rowNames.forEach((m,u)=>{if(m==="(Intercept)"||m==="Residuals")return;let h={name:m,df:t.table[u][s],ss:t.table[u][n]};f.terms.push(h),a.set(m,h)})}let i=r.find(s=>s.name==="Subject");if(i===void 0)throw new Error("the result has no subject stratum");let l=e.anova.rowNames.map(s=>{let c=a.get(s);if(c===void 0)throw new Error(`there is no univariate test of '${s}'`);return c});return{strata:r,subject:i,order:l}}function ia(e,t){let n=Array.isArray(e)?e:[],r=n.filter(a=>!t.some(i=>to(a).test(i.name)));if(r.length>0)throw new Error(`Observed variable not in data: ${r.join(", ")}`);let o=a=>n.some(i=>to(i).test(a));return{isObserved:o,ssObserved:t.reduce((a,i)=>o(i.name)?a+i.ss:a,0)}}function ta(e,t,n,r){let o=a=>at(e,t,n,{ncp:a})-r;return o(0)>0?e===1/0?1/0:ft(o,0,Math.max(10,e*t*4),{...ra,extendInt:"downX"}).root:0}function la(e,t,n,r,o){let a=o==="two.sided"?r:2*r-1,i=Math.max(0,e),l=i/t/((1-i)/n),s=ta(l,t,n,(1+a)/2),c=ta(l,t,n,(1-a)/2);l<=Qe((1-a)/2,t,n)&&(c=0),l<=Qe((1+a)/2,t,n)&&(s=0);let f=s===1/0?1:s/(s+n),m=c===1/0?1:c/(c+n);return o==="less"&&(f=0),o==="greater"&&(m=1),[e,r,f,m]}var na="needs a table of anova() or Anova(), a fit from lm() or aov() with a formula, its aovSummary(), or a result of aovEz()";function no(e,t,n){if(typeof e!="object"||e===null)throw new TypeError(na);if(sa(e)||Bl(e)||aa(e))return Zl(e,t,n);let r=Hl(e)?Br(e):e;if(!Array.isArray(r.colNames)||!Array.isArray(r.table))throw new TypeError(na);let o=n.ci===void 0?.95:n.ci,a=ie(n.alternative??"greater",["two.sided","less","greater"]),i=r.colNames.indexOf("Df"),l=r.colNames.indexOf("Sum Sq");if(i<0||l<0)throw new TypeError("the table needs the columns 'Df' and 'Sum Sq'");let s=r.rowNames.map((M,x)=>({name:M,df:r.table[x][i],ss:r.table[x][l]})).filter(M=>M.name!=="(Intercept)"),c=s.find(M=>M.name==="Residuals");if(c===void 0)throw new TypeError("the table has no row 'Residuals'");let f=s.filter(M=>M!==c),m=c.df,u=c.ss,h=u/m,p=s.reduce((M,x)=>M+x.ss,0),d=s.reduce((M,x)=>M+x.df,0)+1,w=t==="Eta2"?n.generalized??!1:!1,g=f.length===1?"plain":w!==!1?"generalized":n.partial??!0?"partial":"plain",{isObserved:v,ssObserved:N}=ia(w,f),y=M=>{let{ss:x,df:q}=M;if(t==="Eta2")return g==="partial"?x/(x+u):g==="plain"?x/p:x/(x+u+N-(v(M.name)?x:0));if(t==="Omega2"){let S=g==="partial"?(x-q*h)/(x+(d-q)*h):(x-q*h)/(p+h);return Math.max(0,S)}let k=g==="partial"?(x-q*h)/(x+u):(x-q*h)/p;return Math.max(0,k)},E=`${t}${g==="plain"?"":`_${g}`}`;return{colNames:o===null?[E]:[E,"CI","CI_low","CI_high"],rowNames:f.map(M=>M.name),table:f.map(M=>o===null?[y(M)]:la(y(M),M.df,m,o,a)),warnings:[]}}function Zl(e,t,n){let r=n.ci===void 0?.95:n.ci,o=ie(n.alternative??"greater",["two.sided","less","greater"]),a=aa(e),i,l,s;if(a){let N=Kl(e);({strata:i,subject:l}=N),s=N.order.map(y=>({term:y,stratum:N.strata.find(E=>E.terms.includes(y))}))}else({strata:i,subject:l}=Ql(sa(e)?Xr(e):e)),s=i.flatMap(N=>N.terms.map(y=>({term:y,stratum:N})));let c=i.flatMap(N=>N.terms),f=i.reduce((N,y)=>N+y.sse,0),m=c.reduce((N,y)=>N+y.ss,0)+f,u=l.sse/l.dfe,h=t==="Eta2"?n.generalized??!1:!1,p=h!==!1?"generalized":n.partial??!0?"partial":"plain",{isObserved:d,ssObserved:w}=ia(h,c),b=(N,y)=>{let{ss:E,df:M}=N,x=y.sse/y.dfe;if(t==="Eta2")return p==="partial"?E/(E+y.sse):p==="plain"?E/m:E/(E+f+w-(d(N.name)?E:0));if(t==="Omega2"){let k=y===l?0:1,S=p==="partial"?(E-M*x)/(E+k*y.sse+l.sse+u):(E-M*x)/(m+u);return Math.max(0,S)}let q=p==="partial"?(E-M*x)/(E+y.sse):(E-M*x)/m;return Math.max(0,q)},g=`${t}${p==="plain"?"":`_${p}`}`,v={colNames:r===null?[g]:[g,"CI","CI_low","CI_high"],rowNames:s.map(N=>N.term.name),table:s.map(({term:N,stratum:y})=>{let E=b(N,y);return r===null?[E]:la(E,N.df,y.dfe,r,o)}),warnings:[]};return a||(v.groups=s.map(N=>N.stratum.name)),v}function Yl(e,t={}){return no(e,"Eta2",t)}function Jl(e,t={}){return no(e,"Omega2",t)}function ec(e,t={}){return no(e,"Epsilon2",t)}var Xn=e=>e===void 0?[]:typeof e=="string"?[e]:[...e],tc=Math.sqrt(2220446049250313e-31);function Qn(e,t){let n=Object.prototype.hasOwnProperty.call(e,t)?e[t]:void 0;if(n===void 0)throw new Error(`object '${t}' not found`);return Array.from(n)}var cn=e=>typeof e=="boolean"?e?"TRUE":"FALSE":String(e);function nc(e){let t=[...new Set(e)];return t.every(n=>typeof n=="number")?t.sort((n,r)=>n-r).map(cn):[...new Set(t.map(cn))].sort(ct)}var ca=e=>e.replace(/[.*+?^${}()|[\]\\]/g,"\\$&");function fa(e,t,n,r){let o=s=>r.some(c=>new RegExp(`\\b${ca(c)}\\b`).test(s));for(let s of r)if(!e.some(c=>new RegExp(`\\b${ca(s)}\\b`).test(c.name)))throw new Error(`Observed variable not in data: ${s}`);let a=[...new Set(t)].reduce((s,c)=>s+c,0),i=e.reduce((s,c)=>o(c.name)?s+c.ss:s,0),l=["pes","ges"].filter(s=>n.includes(s));return{colNames:["num Df","den Df","MSE","F",...l,"Pr(>F)"],rowNames:e.map(s=>s.name),table:e.map(s=>{let c=l.map(f=>f==="pes"?s.ss/(s.ss+s.errSS):s.ss/(s.ss+a+i-(o(s.name)?s.ss:0)));return[s.numDf,s.denDf,s.errSS/s.denDf,s.f,...c,s.p]})}}function rc(e,t){let n=Xn(t.between),r=Xn(t.within);if(n.length===0&&r.length===0)throw new Error("Either between or within need to be non-NULL!");let o=t.type??3;if(o!==2&&o!==3)throw new RangeError(`invalid type (${String(o)}): use 2 or 3`);let a=t.correction??"GG",i=Xn(t.es??"ges"),l=Xn(t.observed),s=[],c=Qn(e,t.id).map(cn),f=Qn(e,t.dv).map(T=>{if(typeof T!="number")throw new TypeError(`the response '${t.dv}' must be numeric`);return T}),m=n.map(T=>Qn(e,T)),u=r.map(T=>Qn(e,T)),h=c.length;for(let T of[f,...m,...u])if(T.length!==h)throw new RangeError("the columns must have the same length");let p=m.map(T=>nc(T)),d=m.map(T=>T.map(cn)),w=u.map(T=>T.map(cn)),b=w.map(T=>[...new Set(T)]);p.forEach((T,O)=>{if(T.length<2)throw new Error(`Factor "${n[O]}" consists of one level only. Remove factor from model?`)});let g=[...new Set(c)],v=new Map,N=new Set;if(c.forEach((T,O)=>{let $=JSON.stringify(d.map(G=>G[O])),D=v.get(T);D===void 0?v.set(T,$):D!==$&&N.add(T)}),N.size>0)throw new Error(`Following ids are in more than one between subjects condition:
4
+ ${g.filter(T=>N.has(T)).join(", ")}`);let y=[[]];for(let T of b)y=y.flatMap(O=>T.map($=>[...O,$]));let E=T=>JSON.stringify(w.map(O=>O[T])),M=new Map(y.map((T,O)=>[JSON.stringify(T),O])),x=new Set(c.map((T,O)=>E(O)));if(y.some(T=>!x.has(JSON.stringify(T))))throw new Error("Empty cells in within-subjects design (i.e., bad data structure).");let q=y.length,k=new Map;c.forEach((T,O)=>{let $=k.get(T);$===void 0&&k.set(T,$={s:y.map(()=>[0,0]),n:y.map(()=>0)});let D=M.get(E(O));$.s[D]=B($.s[D],[f[O],0]),$.n[D]++}),[...k.values()].some(T=>T.n.some(O=>O>1))&&s.push("More than one observation per design cell, aggregating data using `fun_aggregate = mean`.\nTo turn off this warning, pass `fun_aggregate = mean` explicitly.");let S=g.filter(T=>k.get(T).n.some(O=>O===0));if(S.length>0){let T=[...S].sort(ct),O=T.length>=10?`${T.slice(0,10).join(", ")}, ... [showing first 10 only]`:T.join(", ");s.push(`Missing values for ${S.length} ID(s), which were removed before analysis:
5
+ ${O}`)}let L=g.filter(T=>!S.includes(T)),A=new Map;c.forEach((T,O)=>A.has(T)||A.set(T,O));let R=L.map(T=>{let O=k.get(T);return O.s.map(($,D)=>Xt($,O.n[D]))}),V={};n.forEach((T,O)=>V[T]=L.map($=>d[O][A.get($)]));let F=Object.fromEntries(n.map(T=>[T,"contr.sum"])),I=Object.fromEntries(n.map((T,O)=>[T,p[O]])),C="Rank deficient model matrix; insufficient data to estimate full model.";if(q===1)return z();return J();function z(){let T=n.includes("y")?"y_":"y",O={...V,[T]:R.map(ne=>ne[0][0])},$=Cr(`${T} ~ ${n.length>0?n.join(" * "):"1"}`,O,{contrasts:F,levels:I});if($.rank<$.coefNames.length)throw new Error(C);let D=Qr($,{type:o});s.push(...D.warnings.filter(ne=>!s.includes(ne)));let G=$.dfResidual,Q=D.rowNames.flatMap((ne,de)=>{if(ne==="(Intercept)"||ne==="Residuals")return[];let[ve,Oe,Fe,Le]=D.table[de];return[{name:ne,ss:ve,numDf:Oe,errSS:$.rss,denDf:G,f:Fe,p:Le}]});return{anova:fa(Q,[$.rss],i,l),univariate:{colNames:D.colNames,rowNames:D.rowNames,table:D.table},within:r,warnings:s}}function J(){let T=L.length,O=n.length>0?Ft(`~ ${n.join(" * ")}`,V,{contrasts:F,levels:I}):{colNames:["(Intercept)"],assign:[0],x:L.map(()=>[1]),termLabels:[],factors:[]},$=O.colNames.length;if(T===0||he(O.x).rank<$)throw new Error(C);let D=T-$,G=[0,...O.termLabels.map((X,Z)=>Z+1)],Q=["(Intercept)",...O.termLabels],ne=X=>O.assign.flatMap((Z,U)=>X.includes(Z)?[U]:[]),de=X=>O.factors.flatMap((Z,U)=>Z[X-1]>0?[U]:[]),ve=X=>O.x.map(Z=>[Z[X],0]),Oe=new Array(T).fill([0,0]),Fe=(X,Z,U)=>{let{qtv:pe}=je([...X,...Z].map(ve),Oe,U);return pe.map(nt=>nt.slice(X.length,X.length+Z.length))},Le=X=>{let{qtv:Z}=je(O.colNames.map((U,pe)=>ve(pe)),Oe,X);return Z.map(U=>U.slice($))},ze=Object.fromEntries(r.map((X,Z)=>[X,y.map(U=>U[Z])])),Ae=Ft(`~ ${r.join(" * ")}`,ze,{contrasts:Object.fromEntries(r.map(X=>[X,"contr.sum"])),levels:Object.fromEntries(r.map((X,Z)=>[X,b[Z]]))}),yt=[0,...Ae.termLabels.map((X,Z)=>Z+1)],et=["(Intercept)",...Ae.termLabels],Pt=X=>X.map(Z=>X.map(U=>{let pe=[0,0];return Z.forEach((nt,Ut)=>pe=B(pe,oe(nt,U[Ut]))),pe})),tt=[],jt=[],zt=[],dn=[],Wt=[],Gt=[],_=[],W=!1;for(let X of yt){let Z=Ae.assign.flatMap((Re,ce)=>Re===X?[ce]:[]),U=Z.length,pe=Z.map(Re=>R.map(ce=>ce.reduce((Ge,Ue,ko)=>B(Ge,Te(Ue,Ae.x[ko][Re])),[0,0]))),nt=Z.map(Re=>Z.map(ce=>[Ae.x.reduce((Ge,Ue)=>Ge+Ue[Re]*Ue[ce],0),0])),Ut=Xs(nt),or=Pt(Le(pe)),gn=an(or,Ut),wn=gt(gn)[0],We=NaN,vn=NaN,sr=NaN,xo=NaN,Eo=!1;if(U>=2&&mt(Us(or),{symmetric:!0,onlyValues:!0}).values.filter(ce=>ce>=tc).length===U){Eo=!0;let ce=gt(gn);We=ge(oe(ce,ce),Te(gt(an(gn,gn)),U))[0],vn=((D+1)*U*We-2)/(U*(D-U*We)),sr=Kr(an(Ut,or)),xo=Zr(sr,U,D,q)}for(let Re=0;Re<G.length;Re++){let ce=G[Re],Ge;if(o===3)Ge=Fe(ne(G.filter(Xe=>Xe!==ce)),ne([ce]),pe);else if(ce===0)Ge=Fe([],ne([0]),pe);else{let Xe=de(ce),bn=G.filter(De=>De!==0&&De!==ce&&Xe.every(Da=>O.factors[Da][De-1]>0));Ge=Fe(ne(G.filter(De=>De!==ce&&!bn.includes(De))),ne([ce]),pe)}let Ue=gt(an(Pt(Ge),Ut))[0],He=(ce===0?1:ne([ce]).length)*U,Be=D*U,Ht=Ue/He/(wn/Be),qo=ae(Ht,He,Be,!1,!1),ar=X===0?Q[Re]:ce===0?et[X]:`${Q[Re]}:${et[X]}`;tt.push([Ue,He,wn,Be,Ht,qo]),jt.push(ar),_.push(wn);let Bt={name:ar,ss:Ue,numDf:He,errSS:wn,denDf:Be,f:Ht,p:qo};if(Eo){let Xe=Math.min(1,vn),bn=ae(Ht,We*He,We*Be,!1,!1),De=ae(Ht,Xe*He,Xe*Be,!1,!1);zt.push([We,bn,vn,De]),dn.push([sr,xo]),Wt.push(ar),a==="GG"?Bt={...Bt,numDf:We*He,denDf:We*Be,p:bn}:a==="HF"&&(vn>1&&(W=!0),Bt={...Bt,numDf:Xe*He,denDf:Xe*Be,p:De})}X===0&&ce===0||Gt.push(Bt)}}W&&s.push("HF eps > 1 treated as 1");let K={anova:fa(Gt,_,i,l),univariate:{colNames:["Sum Sq","num Df","Error SS","den Df","F value","Pr(>F)"],rowNames:jt,table:tt},within:r,warnings:s};return zt.length>0&&(K.corrections={colNames:["GG eps","Pr(>F[GG])","HF eps","Pr(>F[HF])"],rowNames:Wt,table:zt},K.sphericity={colNames:["Test statistic","p-value"],rowNames:[...Wt],table:dn}),K}}var pa=Math.log(2*Math.PI),ma=2e-14,ua=500;function oc(e){return Math.exp(-.5*e*e-.5*pa)}function sc(e){return P(e,{logP:!0})}function da(e,t=-1/0){return(t===-1/0?ur(e,0,2,0,ma,ua):ur(e,t,1,0,ma,ua)).result}function ha(e,t,n){let r=t.map(Math.sqrt),o=t.map(i=>Math.sqrt(1-i));return da(i=>{let l=0;for(let s=0;s<r.length;s++){let c=(r[s]*i+e)/o[s];if(!n){l+=sc(c);continue}let f=(r[s]*i-e)/o[s],m=P(f)+P(c,{lowerTail:!1});m<.5?l+=Math.log1p(-m):l+=Math.log(c+f<0?P(c)-P(f):P(f,{lowerTail:!1})-P(c,{lowerTail:!1}))}return oc(i)*-Math.expm1(l)})}function vt(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,ha(e,t,r));let o=n/2,a=Math.log(2)+.5*Math.log(o)-.5*pa-To(o);return Math.min(1,da(l=>{let s=a-o*(Math.expm1(2*l)-2*l);return s<-745?0:Math.exp(s)*ha(e*Math.exp(l),t,r)}))}var ga="Ties are present. z-quantiles were corrected for ties.";function Ve(e,t){let{labels:n,groups:r}=ut(e,t),o=r.length;if(o<2)throw new RangeError("there must be at least two groups");let a=r.map(i=>i.length);return{labels:n,groups:r,n:a,N:a.reduce((i,l)=>i+l,0),k:o}}function ro(e){let t=e.groups.flat(),n=fe(t),r=[],o=0;for(let i of e.n){let l=0;for(let s=0;s<i;s++)l+=n[o+s];o+=i,r.push(l/i)}let a=0;for(let i of Ie(t))a+=i*i*i-i;return{R:r,C:a/(12*(e.N-1))}}function fn(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 ac(e,t){let n=e.length+1,r=[];for(let l=0;l<n-1;l++)for(let s=l+1;s<n;s++)r.push(e[s-1][l]);let o=It(r,t),a=0,i=e.map(l=>l.slice());for(let l=0;l<n-1;l++)for(let s=l+1;s<n;s++)i[s-1][l]=o[a++];return i}function Zn(e,t,n,r,o,a=[]){return{method:t,statistic:n,pValue:r,rowNames:e.labels.slice(1),colNames:e.labels.slice(0,-1),pAdjustMethod:o,warnings:a}}function oo(e,t,n,r,o,a=[]){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:a}}var Kn=e=>P(e,{lowerTail:!1}),so=(e,t,n)=>yn(Math.abs(e),t,n,{lowerTail:!1});function ao(e){let t=0,n=0;for(let r of e.groups)r.length<2||(t+=(r.length-1)*re(r),n+=r.length-1);return{m:e.groups.map(se),s2:t/n,df:e.N-e.k}}function ic(e,t,n={}){let r=n.pAdjustMethod??"holm",o=Ve(e,t),{R:a,C:i}=ro(o),l=o.N*(o.N+1)/12-i,s=fn(o.k,(f,m)=>Math.abs(a[f]-a[m])/Math.sqrt(l*(1/o.n[f]+1/o.n[m]))),c=ac(s.map(f=>f.map(m=>2*Kn(m))),r);return Zn(o,"Dunn's all-pairs test",s,c,r,i!==0?[ga]:[])}function lc(e,t,n={}){let r=te(n.alternative),o=n.pAdjustMethod??"single-step",a=Ve(e,t),{R:i,C:l}=ro(a),s=a.N*(a.N+1)/12-l,c=a.n.slice(1).map((h,p)=>p+1),f=c.map(h=>(i[h]-i[0])/Math.sqrt(s*(1/a.n[0]+1/a.n[h]))),m=c.map(h=>a.n[h]/(a.n[h]+a.n[0])),u=wa(f,m,r,o);return oo(a,"Dunn's many-to-one test",f,u,o,l!==0?[ga]:[])}function wa(e,t,n,r){return r==="single-step"?e.map(o=>n==="two.sided"?vt(o,t,1/0,!0):vt(n==="greater"?o:-o,t,1/0,!1)):It(e.map(o=>n==="two.sided"?2*Kn(Math.abs(o)):Kn(n==="greater"?o:-o)),r)}function cc(e,t,n={}){let r=te(n.alternative),o=Ve(e,t),{m:a,s2:i,df:l}=ao(o),s=Math.sqrt(i),c=o.n.slice(1).map((h,p)=>p+1),f=c.map(h=>(a[h]-a[0])/(s*Math.sqrt(1/o.n[h]+1/o.n[0]))),m=c.map(h=>o.n[h]/(o.n[h]+o.n[0])),u=f.map(h=>r==="two.sided"?Math.min(1,2*vt(Math.abs(h),m,l,!1)):vt(r==="greater"?h:-h,m,l,!1));return oo(o,`Dunnett's-test for multiple
6
+ comparisons with one control`,f,u,"single-step")}function fc(e,t,n={}){let r=te(n.alternative),o=n.pAdjustMethod??"single-step",a=Ve(e,t);if(o==="single-step"&&a.k<3)throw new RangeError(`for 'p.adjust.method = single-step' at least
7
+
8
+ k = 3 groups are required.`);let i=a.n[0],l=a.n.slice(1).map((m,u)=>u+1),s=l.map(m=>{let u=a.n[m],h=[...a.groups[0],...a.groups[m]],p=fe(h),d=0;for(let M=0;M<i;M++)d+=p[M];let w=i+u,b=w*(w+1)/2-d,g=i*u+i*(i+1)/2-d,v=i*u+u*(u+1)/2-b,N=0;for(let M of Ie(h))N+=M*M*M-M;let y=i*u/(w*(w-1))*((w*w*w-w)/12-N/12),E=(Math.min(g,v)-i*u/2)/Math.sqrt(y);return d<b?-E:E}),c=l.map(m=>a.n[m]/(a.n[m]+i)),f=wa(s,c,r,o);return oo(a,`Wilcoxon, Mann, Whittney U-test
9
+ for multiple comparisons with one control`,s,f,o)}function mc(e,t){let n=Ve(e,t),r=fn(n.k,(a,i)=>{let l=n.n[a],s=n.n[i],c=[...n.groups[a],...n.groups[i]],f=fe(c),m=0;for(let b=0;b<l;b++)m+=f[b];let u=m-l*(l+1)/2,h=Math.min(u,l*s-u),p=l+s,d=0;for(let b of Ie(c))d+=b*b*b-b;let w=l*s/(p*(p-1))*((p*p*p-p)/12-d/12);return Math.SQRT2*(h-l*s/2)/Math.sqrt(w)}),o=r.map(a=>a.map(i=>Number.isNaN(i)?NaN:so(i,n.k,1/0)));return Zn(n,"Dwass-Steele-Critchlow-Fligner all-pairs test",r,o,"single-step")}function uc(e,t){let n=Ve(e,t),r=n.groups.map(se),o=n.groups.map((l,s)=>re(l)/n.n[s]),a=fn(n.k,(l,s)=>(r[l]-r[s])/Math.sqrt(o[l]+o[s])*Math.SQRT2),i=fn(n.k,(l,s)=>{let c=(o[l]+o[s])**2/(o[l]**2/(n.n[l]-1)+o[s]**2/(n.n[s]-1));return so(a[l-1][s],n.k,c)});return Zn(n,"Games-Howell test",a,i,"none")}function hc(e,t){let n=Ve(e,t),{m:r,s2:o,df:a}=ao(n),i=fn(n.k,(s,c)=>(r[s]-r[c])/Math.sqrt(o*(1/n.n[s]+1/n.n[c])/2)),l=i.map(s=>s.map(c=>Number.isNaN(c)?NaN:so(c,n.k,a)));return Zn(n,"Tukey's test",i,l,"single-step")}function pc(e,t,n={}){if((n.alternative===void 0?"greater":te(n.alternative))==="two.sided")throw new RangeError("'arg' should be one of \u201Cgreater\u201D, \u201Cless\u201D");let o=Ve(e,t);throw o.n.some(a=>a!==o.n[0])?new RangeError("Steel's test is only valid for balanced designs."):new Error("'data' must be of a vector type, was 'NULL'")}function dc(e,t,n={}){let r=n.method??"none",o=n.altp??!1,a=Ve(e,t),i=Dn(e,t).statistic,{R:l,C:s}=ro(a),c=a.N*(a.N+1)/12-s,f=[],m=[];for(let h=1;h<a.k;h++)for(let p=0;p<h;p++)f.push((l[p]-l[h])/Math.sqrt(c*(1/a.n[p]+1/a.n[h]))),m.push(`${a.labels[p]} - ${a.labels[h]}`);let u=f.map(h=>(o?2:1)*Kn(Math.abs(h)));return{chi2:i,Z:f,P:u,PAdjusted:gc(u,r),comparisons:m}}function gc(e,t){let n=e.length,r=(a,i)=>Math.min(1,-Math.expm1(i*Math.log1p(-a))),o=a=>{let i=e.map((s,c)=>c).sort((s,c)=>(a?e[c]-e[s]:e[s]-e[c])||s-c),l=new Array(n);return i.forEach((s,c)=>l[s]=c+1),l};switch(t){case"none":return e.slice();case"bonferroni":return e.map(a=>Math.min(1,n*a));case"sidak":return e.map(a=>r(a,n));case"holm":{let a=o(!1);return e.map((i,l)=>Math.min(1,(n+1-a[l])*i))}case"hs":{let a=o(!1);return e.map((i,l)=>r(i,n+1-a[l]))}case"hochberg":{let a=o(!0);return e.map((i,l)=>Math.min(1,a[l]*i))}case"bh":{let a=o(!0);return e.map((i,l)=>Math.min(1,n/(n+1-a[l])*i))}case"by":{let a=o(!0),i=0;for(let l=1;l<=n;l++)i+=1/l;return e.map((l,s)=>Math.min(1,n/(n+1-a[s])*i*l))}default:throw new RangeError(`invalid method "${String(t)}"`)}}function wc(e,t,n={}){let r=te(n.alternative),o=Ve(e,t),{m:a,s2:i,df:l}=ao(o),s=Math.sqrt(i),c=o.n.slice(1).map((d,w)=>w+1),f=c.map(d=>a[d]-a[0]),m=c.map(d=>s*Math.sqrt(1/o.n[d]+1/o.n[0])),u=f.map((d,w)=>d/m[w]),h=c.map(d=>o.n[d]/(o.n[d]+o.n[0])),p=u.map(d=>r==="two.sided"?vt(d,h,l,!0):vt(r==="greater"?d:-d,h,l,!1));return{names:c.map(d=>`${o.labels[d]} - ${o.labels[0]}`),estimate:f,se:m,t:u,p,df:l}}var vc=e=>e==null||typeof e=="number"&&Number.isNaN(e);function va(e,t,n){if(e.length!==t.length)throw new RangeError("'time' and 'status' must have the same length");if(n!==void 0&&n.length!==e.length)throw new RangeError("'group' must have the same length as 'time'");let r=[];for(let l=0;l<e.length;l++){if(Number.isNaN(e[l])||Number.isNaN(t[l])||n!==void 0&&vc(n[l]))continue;let s=t[l];if(s!==0&&s!==1)throw new RangeError("'status' must be 0 (censored) or 1 (event)");r.push(l)}if(r.length===0)throw new RangeError("no observations without missing values");let o=[],a=new Map;n!==void 0&&(o=Array.from(new Set(r.map(l=>n[l]))).sort((l,s)=>typeof l=="number"&&typeof s=="number"?l-s:String(l)<String(s)?-1:String(l)>String(s)?1:0),o.forEach((l,s)=>a.set(l,s)));let i=r.map(l=>({time:e[l],event:t[l]===1,group:n===void 0?0:a.get(n[l])}));return i.sort((l,s)=>l.time-s.time),{obs:i,labels:o.map(l=>`group=${String(l)}`)}}function bc(e,t,n,r={}){let{rho:o=0}=r;if(!Number.isFinite(o))throw new RangeError("'rho' must be a finite number");let{obs:a,labels:i}=va(e,t,n),l=i.length,s=new Array(l).fill(0);for(let g of a)s[g.group]++;let c=new Array(l).fill(0),f=new Array(l).fill(0),m=Array.from({length:l},()=>new Array(l).fill(0)),u=s.slice(),h=a.length,p=1;for(let g=0;g<a.length;){let v=a[g].time,N=g,y=new Array(l).fill(0),E=new Array(l).fill(0),M=0;for(;N<a.length&&a[N].time===v;){let x=a[N];E[x.group]++,x.event&&(y[x.group]++,M++),N++}if(M>0){let x=o===0?1:p**o;for(let q=0;q<l;q++)c[q]+=x*y[q],f[q]+=x*M*u[q]/h;if(h>1){let q=x*x*M*(h-M)/(h*(h-1));for(let k=0;k<l;k++)for(let S=0;S<l;S++)m[k][S]+=q*u[k]*((k===S?1:0)-u[S]/h)}p*=1-M/h}for(let x=0;x<l;x++)u[x]-=E[x];h-=N-g,g=N}let d=f.map((g,v)=>g>0?v:-1).filter(g=>g>=0),w=0;if(d.length>=2){let g=d.slice(1),v=g.map(y=>c[y]-f[y]),N=be(g.map(y=>g.map(E=>m[y][E])),v);w=v.reduce((y,E,M)=>y+E*N[M],0)}let b=$e(w,d.length-1,{lowerTail:!1});return{n:s,groups:i,obs:c,exp:f,var:m,chisq:w,pvalue:b}}function io(e,t,n){let r=Math.sqrt(Number.EPSILON);for(let o=0;o<n.length;o++){let a=n[o];if(a<.5+r){if(a>.5-r){for(let i=o+1;i<n.length;i++)if(t[i]>0)return(e[o]+e[i])/2;return e[o]}return e[o]}}return NaN}function Nc(e,t,n={}){let{group:r,confType:o="log",confInt:a=.95}=n;if(o!=="log"&&o!=="plain"&&o!=="log-log")throw new RangeError(`invalid 'confType': ${String(o)}`);if(!(a>0&&a<1))throw new RangeError("'confInt' must be between 0 and 1");let{obs:i,labels:l}=va(e,t,r),s=r===void 0?["all"]:l,c=H(1-(1-a)/2),f={strata:[],time:[],nRisk:[],nEvent:[],nCensor:[],surv:[],stdErr:[],lower:[],upper:[],median:[],medianRows:s};return s.forEach((m,u)=>{let h=i.filter(N=>N.group===u),p=f.time.length,d=h.length,w=1,b=0;for(let N=0;N<h.length;){let y=h[N].time,E=N,M=0;for(;E<h.length&&h[E].time===y;)h[E].event&&M++,E++;let x=E-N-M;M>0&&(w*=1-M/d,b+=M<d?M/(d*(d-M)):1/0);let q=Math.sqrt(b),k,S;if(o==="log")k=w===0?NaN:w*Math.exp(-c*q),S=w===0?NaN:Math.min(1,w*Math.exp(c*q));else if(o==="plain"){let L=c*q*w;k=Number.isNaN(L)?NaN:Math.max(0,w-L),S=Number.isNaN(L)?NaN:Math.min(1,w+L)}else if(w===0||w===1)k=NaN,S=NaN;else{let L=Math.log(w),A=Math.log(-L);k=Math.exp(-Math.exp(A-c*q/L)),S=Math.exp(-Math.exp(A+c*q/L))}f.strata.push(m),f.time.push(y),f.nRisk.push(d),f.nEvent.push(M),f.nCensor.push(x),f.surv.push(w),f.stdErr.push(w*q),f.lower.push(k),f.upper.push(S),d-=E-N,N=E}let g=f.time.slice(p),v=f.nEvent.slice(p);f.median.push([io(g,v,f.surv.slice(p)),io(g,v,f.lower.slice(p)),io(g,v,f.upper.slice(p))])}),f}var yc=e=>e==null||typeof e=="number"&&Number.isNaN(e),Mc=(e,t)=>typeof e=="number"&&typeof t=="number"?e-t:String(e)<String(t)?-1:String(e)>String(t)?1:0;function xc(e){let t=Math.sqrt(2220446049250313e-31),n=Array.from(new Set(e.filter(a=>Number.isFinite(a)))).sort((a,i)=>a-i);if(n.length<2)return e.slice();let r=0;for(let a of n)r+=Math.abs(a);r/=n.length;let o=[n[0]];for(let a=0;a+1<n.length;a++){let i=n[a+1]-n[a];i<=t||i/r<=t||o.push(n[a+1])}return e.map(a=>{if(!Number.isFinite(a))return a;let i=0,l=o.length-1;for(;i<l;){let s=i+l+1>>1;o[s]<=a?i=s:l=s-1}return o[i]})}function lo(e,t,n){let{time:r,event:o,weight:a,x:i,stratumEnd:l}=e,s=r.length,c=t.length,f=()=>new Array(c).fill(0),m=()=>Array.from({length:c},f),u=f(),h=m(),p=0,d=0,w=f(),b=m(),g=f(),v=m();for(let N=s-1;N>=0;){l[N]&&(d=0,w=f(),b=m());let y=r[N],E=0,M=0,x=0,q=N;do{let k=i[q],S=0;for(let R=0;R<c;R++)S+=t[R]*k[R];let L=a[q],A=Math.exp(S)*L;if(o[q]){E++,M+=L,x+=A,p+=L*S;for(let R=0;R<c;R++){u[R]+=L*k[R],g[R]+=A*k[R];let V=v[R];for(let F=0;F<=R;F++)V[F]+=A*k[R]*k[F]}}else{d+=A;for(let R=0;R<c;R++){w[R]+=A*k[R];let V=b[R];for(let F=0;F<=R;F++)V[F]+=A*k[R]*k[F]}}q--}while(q>=0&&r[q]===y&&!l[q]);if(E>0){let k=n?E:1,S=n?E:1,L=M/k;for(let A=0;A<k;A++){d+=n?x/S:x,p-=L*Math.log(d);for(let R=0;R<c;R++){w[R]+=n?g[R]/S:g[R];let V=w[R]/d;u[R]-=L*V;let F=b[R],I=v[R],C=h[R];for(let z=0;z<=R;z++)F[z]+=n?I[z]/S:I[z],C[z]+=L*(F[z]-V*w[z])/d}}g.fill(0);for(let A of v)A.fill(0)}N=q}for(let N=0;N<c;N++)for(let y=0;y<N;y++)h[y][N]=h[N][y];return{loglik:p,score:u,info:h}}function co(e,t){let n=e.length,r=0,o=!1;for(let s=0;s<n;s++){let c=e[s][s];c>0&&((!o||c>r)&&(r=c),o=!0)}let a=o?r*t:t,i=0,l=!0;for(let s=0;s<n;s++){let c=e[s][s];if(!Number.isFinite(c)||c<a){e[s][s]=0,c<-8*a&&(l=!1);continue}i++;for(let f=s+1;f<n;f++){let m=e[f][s]/c;e[f][s]=m,e[f][f]-=m*m*c;for(let u=f+1;u<n;u++)e[u][f]-=m*e[u][s]}}return l?i:-i}function fo(e,t){let n=e.length,r=t.slice();for(let o=0;o<n;o++){let a=r[o];for(let i=0;i<o;i++)a-=r[i]*e[o][i];r[o]=a}for(let o=n-1;o>=0;o--){if(e[o][o]===0){r[o]=0;continue}let a=r[o]/e[o][o];for(let i=o+1;i<n;i++)a-=r[i]*e[i][o];r[o]=a}return r}function mo(e){let t=e.length;for(let n=0;n<t;n++)if(e[n][n]>0){e[n][n]=1/e[n][n];for(let r=n+1;r<t;r++){e[r][n]=-e[r][n];for(let o=0;o<n;o++)e[r][o]+=e[r][n]*e[n][o]}}for(let n=0;n<t;n++){if(e[n][n]===0){for(let r=0;r<t;r++)e[n][r]=0,e[r][n]=0;continue}for(let r=n+1;r<t;r++){let o=e[r][n]*e[r][r];e[n][r]=o;for(let a=n;a<r;a++)e[n][a]+=o*e[r][a]}}for(let n=0;n<t;n++)for(let r=0;r<n;r++)e[n][r]=e[r][n];return e}var uo=(e,t)=>({statistic:e,df:t,pValue:$e(e,t,{lowerTail:!1})});function Ec(e,t,n,r={}){let{ties:o="efron",weights:a,strata:i,init:l,control:s={},nocenter:c=[-1,0,1],confLevel:f=.95}=r,{eps:m=1e-9,iterMax:u=20,tolerChol:h=2**-39}=s,p=s.tolerInf??Math.sqrt(m);if(o!=="efron"&&o!=="breslow")throw new RangeError(`invalid 'ties': ${String(o)}`);if(!(m>0))throw new RangeError("'eps' must be positive");if(!(Number.isInteger(u)&&u>=0))throw new RangeError("'iterMax' must be a non-negative integer");if(!(f>0&&f<1))throw new RangeError("'confLevel' must be between 0 and 1");let d=e.length;if(t.length!==d||n.length!==d)throw new RangeError("'time', 'status' and 'x' must have the same length");if(a!==void 0&&a.length!==d)throw new RangeError("'weights' must have the same length as 'time'");if(i!==void 0&&i.length!==d)throw new RangeError("'strata' must have the same length as 'time'");let w=d===0?0:n[0].length;if(w<1)throw new RangeError("'x' must have at least one column");if(n.some(_=>_.length!==w))throw new RangeError("the rows of 'x' must have the same length");if(l!==void 0&&l.length!==w)throw new RangeError("'init' must have one value per column of 'x'");let b=[];for(let _=0;_<d;_++){if(Number.isNaN(e[_])||Number.isNaN(t[_])||n[_].some(K=>Number.isNaN(K))||a!==void 0&&Number.isNaN(a[_])||i!==void 0&&yc(i[_]))continue;let W=t[_];if(W!==0&&W!==1)throw new RangeError("'status' must be 0 (censored) or 1 (event)");b.push(_)}let g=b.length;if(g===0)throw new RangeError("no observations without missing values");let v=b.map(_=>a===void 0?1:a[_]);if(v.some(_=>!(_>0)))throw new RangeError("Invalid weights, must be >0");if(v.some(_=>!Number.isInteger(_)))throw new RangeError("non-integer weights (robust variance) are not supported yet");let N=0;for(let _ of b)N+=t[_];let y=xc(b.map(_=>e[_])),E=new Array(g).fill(0);if(i!==void 0){let _=Array.from(new Set(b.map(K=>i[K]))).sort(Mc),W=new Map;_.forEach((K,X)=>W.set(K,X)),E=b.map(K=>W.get(i[K]))}let M=Array.from({length:g},(_,W)=>W).sort((_,W)=>E[_]-E[W]||y[_]-y[W]||_-W),x=0;for(let _ of v)x+=_;let q=M.map(_=>v[_]),k=M.map(_=>n[b[_]].slice()),S=new Array(w).fill(0),L=new Array(w).fill(1),A=new Set(c);for(let _=0;_<w;_++){if(k.every(U=>A.has(U[_])))continue;let W=0;for(let U=0;U<g;U++)W+=q[U]*k[U][_];let K=W/x,X=0;for(let U=0;U<g;U++)k[U][_]-=K,X+=q[U]*Math.abs(k[U][_]);let Z=X===0?1:x/X;for(let U=0;U<g;U++)k[U][_]*=Z;S[_]=K,L[_]=Z}let R={time:M.map(_=>y[_]),event:M.map(_=>t[b[_]]===1),weight:q,x:k,stratumEnd:M.map((_,W)=>W===g-1||E[M[W+1]]!==E[_])},V=o==="efron",F=l===void 0?new Array(w).fill(0):l.map((_,W)=>_/L[W]),I=lo(R,F,V),C=[I.loglik,I.loglik],z=I.info.map(_=>_.slice()),J=co(z,h),T=fo(z,I.score),O=0;for(let _=0;_<w;_++)O+=I.score[_]*T[_];let $=0,D,G=!1;if(u===0||!Number.isFinite(I.loglik))D=mo(z);else{let _=I.loglik,W=F.map((Z,U)=>Z+T[U]),K=0,X=!1;for($=1;$<=u;$++){I=lo(R,W,V),z=I.info.map(U=>U.slice()),J=co(z,h);let Z=Number.isFinite(I.loglik)&&I.score.every(Number.isFinite)&&I.info.every(U=>U.every(Number.isFinite));if(Z&&Math.abs(1-_/I.loglik)<=m){X=!0,C[1]=I.loglik,F=W;break}!Z||I.loglik<_?(K++,W=W.map((U,pe)=>(U+K*F[pe])/(K+1))):(K=0,_=I.loglik,C[1]=_,T=fo(z,I.score),F=W,W=W.map((U,pe)=>U+T[pe]))}X?D=mo(z):($=u+1,G=!0,u>1?(I=lo(R,F,V),C[1]=I.loglik,D=I.info.map((Z,U)=>Z.map((pe,nt)=>U===nt?Z[U]>0?1/Z[U]:0:-0))):D=mo(z))}let Q=F.map((_,W)=>_*L[W]),ne=I.score.map((_,W)=>_/L[W]),de=D.map((_,W)=>_.map((K,X)=>K*L[W]*L[X])),ve=Q.slice();if(u>0&&Math.abs(J)<w)for(let _=0;_<w;_++)de[_][_]===0&&(ve[_]=NaN);let Oe=0;for(let _=0;_<w;_++)Oe+=Q[_]*S[_];let Fe=b.map(_=>{let W=0;for(let K=0;K<w;K++)W+=n[_][K]*Q[K];return W-Oe}),Le=new Ne;if(u>1){let _=de.map((W,K)=>{let X=0;for(let Z=0;Z<w;Z++)X+=ne[Z]*de[Z][K];return Math.abs(X)});if(G)Le.add("Ran out of iterations and did not converge"),(Math.max(...Fe)>500||_.some(W=>!Number.isFinite(W)))&&Le.add("one or more coefficients may be infinite");else{let W=[];for(let K=0;K<w;K++){let X=_[K];(!Number.isFinite(ne[K])||X>m&&X>p*Math.abs(Q[K]))&&W.push(K+1)}W.length>0&&Le.add(`Loglik converged before variable ${W.join(",")} ; coefficient may be infinite. `)}}let ze=ve.map((_,W)=>Number.isNaN(_)?-1:W).filter(_=>_>=0),Ae=ze.length,yt=ze.map(_=>ve[_]-(l===void 0?0:l[_])),et=0;if(Ae===1)et=yt[0]*yt[0]/de[ze[0]][ze[0]];else if(Ae>1){let _=ze.map(K=>ze.map(X=>de[K][X]));co(_,h);let W=fo(_,yt);for(let K=0;K<Ae;K++)et+=yt[K]*W[K]}let Pt=H((1+f)/2),tt=de.map((_,W)=>Math.sqrt(_[W])),jt=ve.map((_,W)=>_/tt[W]),zt=jt.map(_=>$e(_*_,1,{lowerTail:!1})),dn=ve.map((_,W)=>Math.exp(_-Pt*tt[W])),Wt=ve.map((_,W)=>Math.exp(_+Pt*tt[W])),Gt=-2*(C[0]-C[1]);return{coefficients:ve,var:de,loglik:C,score:O,wald:et,iter:$,means:S,n:g,nevent:N,linearPredictors:Fe,se:tt,z:jt,p:zt,lower:dn,upper:Wt,logtest:uo(Gt,Ae),sctest:uo(O,Ae),waldtest:uo(et,Ae),rsq:{rsq:1-Math.exp(-Gt/g),maxrsq:1-Math.exp(2*C[0]/g)},warnings:Le.list}}function kc(e,t,n={}){let r=ie(n.center??"median",["median","mean"]),{groups:o}=ut(e,t);if(o.length<2)throw new RangeError("contrasts can be applied only to factors with 2 or more levels");let a=o.map(l=>{let s=r==="median"?Vn(l):se(l);return l.map(c=>Math.abs(c-s))}),i=Vr(a);return{colNames:["Df","F value","Pr(>F)"],rowNames:["group",""],table:[[i.df[0],i.fValue,i.pValue],[i.df[1],NaN,NaN]]}}function qc(e,t,n){let r=Array.from(e),o=Array.from(t),a=r.length,i=n===void 0?r.map((w,b)=>b+1):Array.from(n);if(o.length!==a||i.length!==a)throw new RangeError("'x', 'n' and 'score' must have the same length");let l=r.reduce((w,b)=>w+b,0)/o.reduce((w,b)=>w+b,0),s=o.map(w=>w/(l*(1-l))),c=r.map((w,b)=>w/o[b]),f=s.reduce((w,b)=>w+b,0),m=s.reduce((w,b,g)=>w+b*i[g],0)/f,u=s.reduce((w,b,g)=>w+b*c[g],0)/f,h=0,p=0;s.forEach((w,b)=>{let g=i[b]-m;h+=w*g*(c[b]-u),p+=w*g*g});let d=h*h/p;return{method:"Chi-squared Test for Trend in Proportions",statistic:d,statisticName:"X-squared",parameter:1,parameterName:"df",pValue:me(d,.5,2,!1,!1)}}var Ac=`Sample size > 100 or data with ties
10
+ p-value based on normal approximation. Specify nperm for permutation p-value`,Rc=`Sample data with ties
11
+ p-value based on normal approximation. Specify nperm for permutation p-value.`;function Tc(e,t,n){let r=Array.from(e);if(r.length!==t.length)throw new RangeError("'x' and 'g' must have the same length");let o=r.flatMap((s,c)=>Number.isNaN(s)||typeof t[c]=="number"&&Number.isNaN(t[c])?[]:[c]),a;if(n!==void 0)a=n.map(String);else{let s=[...new Set(o.map(c=>t[c]))];a=s.every(c=>typeof c=="number")?s.sort((c,f)=>c-f).map(String):[...new Set(s.map(String))].sort(ct)}let i=a.map(()=>[]),l=new Map(a.map((s,c)=>[s,c]));for(let s of o){let c=l.get(String(t[s]));if(c===void 0)throw new RangeError(`the group '${String(t[s])}' is not among 'levels'`);i[c].push(r[s])}return i}function Sc(e,t,n={}){let r=ie(n.alternative,["two.sided","increasing","decreasing"]),o=n.variant??"DescTools";if(o!=="DescTools"&&o!=="clinfun")throw new RangeError(`invalid variant "${String(o)}"`);if(o==="clinfun"&&n.exact!==void 0)throw new TypeError("clinfun's jonckheere.test has no option 'exact'");let a=Tc(e,t,n.levels),i=a.flat(),l=i.length,s=new Set(i).size<l,c=0,f=[],m=l;a.forEach((g,v)=>{m-=g.length,f.push([g.length,m]);for(let N=v+1;N<a.length;N++)for(let y of g)for(let E of a[N])c+=y<E?1:y===E?.5:0});let u=[],h=!s&&l<=100;o==="DescTools"&&n.exact!==void 0?(h=n.exact,h&&s&&(h=!1,u.push(Rc))):h||u.push(Ac);let p,d;if(h){let g=[1];for(let[v,N]of f){if(v===0||N===0)continue;let y=Array.from({length:v*N+1},(M,x)=>Vo(x,v,N,!1)),E=new Array(g.length+y.length-1).fill(0);g.forEach((M,x)=>y.forEach((q,k)=>E[x+k]+=M*q)),g=E}p=0,d=0,g.forEach((v,N)=>{N<=c&&(p+=v),N>=c&&(d+=v)})}else{let g=f.reduce((y,[E,M])=>y+E*M,0)/2,v=f.reduce((y,[E,M])=>y+E*M*(E+M+1)/12,0),N=(c-g)/Math.sqrt(v);p=P(N),d=1-p}let b=r==="increasing"?d:r==="decreasing"?p:2*Math.min(d,p,o==="clinfun"?.5:1);return{method:"Jonckheere-Terpstra test",alternative:r,statistic:c,statisticName:"JT",pValue:b,warnings:u}}function mn(e){return qe(Array.from(e)).sort((t,n)=>t-n)}function Yn(e){let t=se(e),n=Math.sqrt(re(e));return e.map(r=>(r-t)/n)}function Lc(e){let t=mn(e),n=t.length;if(n<8)throw new RangeError("sample size must be greater than 7");let r=Yn(t),o=0;for(let s=1;s<=n;s++)o+=(2*s-1)*(P(r[s-1],{logP:!0})+P(r[n-s],{lowerTail:!1,logP:!0}));let a=-n-o/n,i=(1+.75/n+2.25/n**2)*a,l;return i<.2?l=1-Math.exp(-13.436+101.14*i-223.73*i**2):i<.34?l=1-Math.exp(-8.318+42.796*i-59.938*i**2):i<.6?l=Math.exp(.9177-4.279*i-1.38*i**2):i<10?l=Math.exp(1.2937-5.709*i+.0186*i**2):l=37e-25,{method:"Anderson-Darling normality test",statistic:a,statisticName:"A",pValue:l}}function Ic(e){let t=mn(e),n=t.length;if(n<8)throw new RangeError("sample size must be greater than 7");let r=Yn(t),o=1/(12*n);r.forEach((s,c)=>{o+=(P(s)-(2*(c+1)-1)/(2*n))**2});let a=(1+.5/n)*o,i=[],l;return a<.0275?l=1-Math.exp(-13.953+775.5*a-12542.61*a**2):a<.051?l=1-Math.exp(-5.903+179.546*a-1515.29*a**2):a<.092?l=Math.exp(.886-31.62*a+10.897*a**2):a<1.1?l=Math.exp(1.111-34.242*a+12.832*a**2):(l=737e-12,i.push("p-value is smaller than 7.37e-10, cannot be computed more accurately")),{method:"Cramer-von Mises normality test",statistic:o,statisticName:"W",pValue:l,warnings:i}}function Oc(e){let t=mn(e),n=t.length;if(n<5)throw new RangeError("sample size must be greater than 4");let r=Yn(t).map(f=>P(f)),o=-1/0,a=-1/0;r.forEach((f,m)=>{o=Math.max(o,(m+1)/n-f),a=Math.max(a,f-m/n)});let i=Math.max(o,a),l=n<=100?i:i*(n/100)**.49,s=n<=100?n:100,c=Math.exp(-7.01256*l**2*(s+2.78019)+2.99587*l*Math.sqrt(s+2.78019)-.122119+.974598/Math.sqrt(s)+1.67997/s);if(c>.1){let f=(Math.sqrt(n)-.01+.85/Math.sqrt(n))*i;f<=.302?c=1:f<=.5?c=2.76773-19.828315*f+80.709644*f**2-138.55152*f**3+81.218052*f**4:f<=.9?c=-4.901232+40.662806*f-97.490286*f**2+94.029866*f**3-32.355711*f**4:f<=1.31?c=6.198765-19.558097*f+23.186922*f**2-12.234627*f**3+2.423045*f**4:c=0}return{method:"Lilliefors (Kolmogorov-Smirnov) normality test",statistic:i,statisticName:"D",pValue:c}}function Fc(e){let t=mn(e),n=t.length;if(n<5||n>5e3)throw new RangeError("sample size must be between 5 and 5000");let r=t.map((f,m)=>H((m+1-3/8)/(n+1/4))),o=jo(t,r)**2/(re(t)*re(r)),a=Math.log(n),i=Math.log(a),l=-1.2725+1.0521*(i-a),s=1.0308-.26758*(i+2/a),c=P((Math.log(1-o)-l)/s,{lowerTail:!1});return{method:"Shapiro-Francia normality test",statistic:o,statisticName:"W",pValue:c}}function _c(e,t={}){let n=mn(e),r=n.length,o=t.nClasses??Math.ceil(2*r**(2/5)),a=t.adjust??!0,i=new Array(o).fill(0);for(let f of Yn(n)){let m=Math.floor(1+o*P(f));m>=1&&m<=o&&i[m-1]++}let l=r/o,s=i.reduce((f,m)=>f+(m-l)**2/l,0),c=o-1-(a?2:0);return{method:"Pearson chi-square normality test",statistic:s,statisticName:"P",pValue:me(s,c/2,2,!1,!1)}}function Jn(e,t){let n=e[0]?.length??0;if(e.some(r=>r.length!==n))throw new TypeError(`'${t}' must be a matrix (rows of equal length)`);return Array.from({length:n},(r,o)=>e.map(a=>a[o]))}function er(e,t){if(e.some(n=>n.some(Number.isNaN)))throw new RangeError(`'${t}' contains missing values`)}var Na=e=>2*P(-Math.abs(e));function Cc(e,t={}){let n=ie(t.weight,["unweighted","equal","squared"]);if(e.some(M=>M.length!==2))throw new TypeError("'ratings' must be a matrix with two columns");let r=e.filter(M=>!M.some(Number.isNaN)),o=r.length,a=r.map(M=>Tt(M[0])),i=r.map(M=>Tt(M[1])),l=[...new Set([...a,...i])].sort((M,x)=>M<x?-1:M>x?1:0),s=l.length,c=new Map(l.map((M,x)=>[M,x])),f=Array.from({length:s},()=>new Array(s).fill(0));for(let M=0;M<o;M++)f[c.get(a[M])][c.get(i[M])]++;let m=M=>n==="unweighted"?M===0?1:0:n==="equal"?1-M/(s-1):1-M*M/((s-1)*(s-1)),u=Array.from({length:s},(M,x)=>Array.from({length:s},(q,k)=>m(Math.abs(x-k)))),h=f.map(M=>ee(M)/o),p=Array.from({length:s},(M,x)=>ee(f.map(q=>q[x]))/o),d=M=>{let x=[];for(let q=0;q<s;q++)for(let k=0;k<s;k++)x.push(M(q,k));return ee(x)},w=d((M,x)=>f[M][x]*u[M][x])/o,b=d((M,x)=>h[M]*p[x]*u[M][x]),g=(w-b)/(1-b),v=Array.from({length:s},(M,x)=>ee(p.map((q,k)=>q*u[x][k]))),N=Array.from({length:s},(M,x)=>ee(h.map((q,k)=>q*u[k][x]))),y=(d((M,x)=>h[M]*p[x]*(u[M][x]-(v[M]+N[x]))**2)-b*b)/(o*(1-b)**2),E=g/Math.sqrt(y);return{value:g,statistic:E,pValue:Na(E)}}function Vc(e,t={}){let n=t.exact??!1,r=Jn(e,"ratings");er(e,"ratings");let o=e.length,a=r.length,i=[...new Set(e.flat())].sort((g,v)=>g-v),l=new Map(i.map((g,v)=>[g,v])),s=i.length,c=e.map(g=>{let v=new Array(s).fill(0);for(let N of g)v[l.get(N)]++;return v}),f=ee(c.map(g=>(ee(g.map(v=>v*v))-a)/(a*(a-1))))/o,m=Array.from({length:s},(g,v)=>ee(c.map(N=>N[v]))/(o*a)),u=ee(m.map(g=>g*g));if(n){let g=r.map(N=>{let y=new Array(s).fill(0);for(let E of N)y[l.get(E)]++;return y.map(E=>E/o)}),v=Array.from({length:s},(N,y)=>re(g.map(E=>E[y]))*(a-1)/a);u-=ee(v)/(a-1)}let h=(f-u)/(1-u);if(n)return{value:h};let p=ee(m.map(g=>g*(1-g))),d=ee(m.map(g=>g*(1-g)*(1-g-g))),w=2/(p*p*o*a*(a-1))*(p*p-d),b=h/Math.sqrt(w);return{value:h,statistic:b,pValue:Na(b)}}function Dc(e,t={}){let n=ie(t.model,["oneway","twoway"]),r=ie(t.type,["consistency","agreement"]),o=ie(t.unit,["single","average"]),a=t.r0??0,i=t.confLevel??.95;Me(i);let l=Jn(e,"ratings");er(e,"ratings");let s=e.length,c=l.length,f=1-i,m=re(e.map(C=>se(C)))*c,u=re(l.map(C=>se(C)))*s,h=se(e.map(C=>re(C))),d=(re(e.flat())*(s*c-1)-m*(s-1)-u*(c-1))/((s-1)*(c-1)),w=(C,z,J)=>ae(C,z,J,!1,!1),b=(C,z)=>st(1-f/2,C,z,!0,!1),g=s-1;if(n==="oneway"||r==="consistency"){let C=n==="oneway"?h:d,z=n==="oneway"?s*(c-1):(s-1)*(c-1),J=m/C,T=J/b(g,z),O=J*b(z,g),[$,D,G]=o==="single"?[(m-C)/(m+(c-1)*C),J*(1-a)/(1+(c-1)*a),Q=>(Q-1)/(Q+c-1)]:[(m-C)/m,J*(1-a),Q=>1-1/Q];return{value:$,Fvalue:D,df1:g,df2:z,pValue:w(D,g,z),lbound:G(T),ubound:G(O)}}let v=o==="single"?c:1,N=C=>[v*C/(s*(1-C)),1+v*C*(s-1)/(s*(1-C))],y=(C,z)=>(C*u+z*d)**2/((C*u)**2/(c-1)+(z*d)**2/((s-1)*(c-1))),E=o==="single"?(m-d)/(m+(c-1)*d+c/s*(u-d)):(m-d)/(m+(u-d)/s),[M,x]=N(a),q=m/(M*u+x*d),k=y(M,x),[S,L]=[c*E/(s*(1-E)),1+c*E*(s-1)/(s*(1-E))],A=y(S,L),R=b(g,A),V=b(A,g),[F,I]=o==="single"?[s*(m-R*d)/(R*(c*u+(c*s-c-s)*d)+s*m),s*(V*m-d)/(c*u+(c*s-c-s)*d+s*V*m)]:[s*(m-R*d)/(R*(u-d)+s*m),s*(V*m-d)/(u-d+s*V*m)];return{value:E,Fvalue:q,df1:g,df2:k,pValue:w(q,g,k),lbound:F,ubound:I}}function ba(e,t){let n=t.length,r=ee(t.flatMap(a=>t.map(i=>e[a][i])));return(1-ee(t.map(a=>e[a][a]))/r)*n/(n-1)}function $c(e){let t=Jn(e,"x").length;if(er(e,"x"),t<2)throw new RangeError("'x' needs at least two items");let n=Tn(e),r=tn(e),o=Array.from({length:t},(s,c)=>c),a=ee(r.flat()),i=be(r),l=o.map(s=>1-1/i[s][s]);return{rawAlpha:ba(n,o),stdAlpha:(1-t/a)*t/(t-1),G6:1-(t-ee(l))/a,averageR:(a-t)/(t*(t-1)),alphaDrop:o.map(s=>ba(n,o.filter(c=>c!==s)))}}function Pc(e,t,n){let r=Jn(e,"x").length;er(e,"x");for(let s of[t,n])if(!Number.isInteger(s)||s<0||s>=r)throw new RangeError(`column index ${s} out of range (0 to ${r-1})`);if(t===n)throw new RangeError("'i' and 'j' must be different columns");let o=tn(e),a=[t,n],i=Array.from({length:r},(s,c)=>c).filter(s=>s!==t&&s!==n),l=a.map(s=>a.map(c=>o[s][c]));if(i.length>0){let s=i.map(m=>i.map(u=>o[m][u])),c=i.map(m=>a.map(u=>o[m][u])),f=be(s,c);for(let m=0;m<2;m++)for(let u=0;u<2;u++)l[m][u]-=ee(i.map((h,p)=>o[a[m]][h]*f[p][u]))}return l[0][1]/Math.sqrt(l[0][0]*l[1][1])}function ue(e){let t=0,n=0;for(let r of e){let[o,a]=rt(t,r);t=o,n+=a}return t+n}var ya=(e,t)=>e[0].map((n,r)=>ue(e.map(o=>t(o[r]))));function Ea(e,t){let n=t.map(r=>1/Math.sqrt(r));return e.map((r,o)=>r.map((a,i)=>n[o]*a*n[i]))}function Ma(e,t,n){let r=mt(Ea(e,t),{symmetric:!0}),o=r.values.slice(0,n).map(a=>Math.sqrt(Math.max(a-1,0)));return r.vectors.map((a,i)=>a.slice(0,n).map((l,s)=>Math.sqrt(t[i])*(l*o[s])))}function xa(e,t){let n=ya(e,l=>l*l),r=n.map((l,s)=>s).sort((l,s)=>n[s]-n[l]||l-s),o=e.map(l=>r.map(s=>l[s])),a=t?.map(l=>r.map(s=>l[s])),i=ya(o,l=>l).map(l=>l<0?-1:1);return o=o.map(l=>l.map((s,c)=>i[c]<0?-s:s)),a=a?.map(l=>l.map((s,c)=>i[c]<0?-s:s)),a===void 0?{L:o}:{L:o,rot:a}}var bt=(e,t)=>e.map(n=>t[0].map((r,o)=>{let a=0;for(let i=0;i<n.length;i++)a+=n[i]*t[i][o];return a})),ho=e=>e[0].map((t,n)=>e.map(r=>r[n]));function tr(e){let t=e[0].length,n=e.length,r=e.map(c=>Math.sqrt(ue(c.map(f=>f*f)))),o=e.map((c,f)=>c.map(m=>m/r[f])),a=ho(o),i=Array.from({length:t},(c,f)=>Array.from({length:t},(m,u)=>f===u?1:0)),l=0;for(let c=0;c<1e3;c++){let f=bt(o,i),m=f[0].map((w,b)=>{let g=0;for(let v=0;v<n;v++)g+=f[v][b]*f[v][b];return g}),u=f.map(w=>w.map((b,g)=>b**3-b*m[g]/n)),h=bt(a,u),p=An(h);i=bt(p.u,ho(p.v));let d=l;if(l=ue(p.d),l<d*(1+1e-5))break}return{loadings:bt(o,i).map((c,f)=>c.map(m=>m*r[f])),rotmat:i}}function jc(e){let t=tr(e),n=t.loadings,r=n.map(l=>l.map(s=>s*Math.abs(s)**3)),o=St(he(n),r),a=be(bt(ho(o),o)).map((l,s)=>l[s]),i=o.map(l=>l.map((s,c)=>s*Math.sqrt(a[c])));return{loadings:bt(n,i),rotmat:bt(t.rotmat,i)}}function zc(e,t,n={}){let r,o;if(n.covmat!==void 0)r=n.covmat.map(E=>E.slice()),o=n.nObs??NaN;else{if(e===null)throw new TypeError("neither 'x' nor 'covmat' supplied");r=Tn(e),o=e.length}let a=r.length;if(a<3)throw new RangeError("factor analysis requires at least three variables");let i=t;if(!Number.isInteger(i)||i<1)throw new RangeError("'factors' must be a positive integer");let l=((a-i)**2-a-i)/2;if(l<0)throw new RangeError(`${i} ${i===1?"factor is":"factors are"} too many for ${a} variables`);let s=r.map((E,M)=>Math.sqrt(E[M])),c=r.map((E,M)=>E.map((x,q)=>x/(s[M]*s[q]))),f=n.control?.lower??.005,m=n.control?.start?.slice()??be(c).map((E,M)=>(1-i/(2*a))/E[M]),p=Sn(m,E=>{let M=mt(Ea(c,E),{symmetric:!0,onlyValues:!0}).values.slice(i);return-(ue(M.map(x=>Math.log(x)-x))-i+a)},{gr:E=>{let M=Ma(c,E,i);return E.map((x,q)=>{let k=0;for(let S=0;S<i;S++)k+=M[q][S]*M[q][S];return(k+x-c[q][q])/(x*x)})},method:"L-BFGS-B",lower:f,upper:1,control:{fnscale:1,parscale:new Array(a).fill(.01)}});if(p.convergence!==0)throw new Error("unable to optimize from this starting value");let d=p.par,w=xa(Ma(c,d,i)).L,b,g=n.rotation??"varimax";if(g!=="none"&&i>=2){let E=g==="varimax"?tr(w):jc(w),M=xa(E.loadings,E.rotmat);w=M.L,b=M.rot}else if(g!=="varimax"&&g!=="promax"&&g!=="none")throw new RangeError(`unknown rotation '${String(g)}'`);let v=NaN,N=NaN;!Number.isNaN(o)&&l>0&&(v=(o-1-(2*a+5)/6-2*i/3)*p.value,N=$e(v,l,{lowerTail:!1}));let y={uniquenesses:d,loadings:w,STATISTIC:v,dof:l,PVAL:N,converged:!0,objective:p.value,counts:p.counts};return b!==void 0&&(y.rotmat=b),y}var wo=2**-52,Wc=`
12
+ Three factors are required for identification -- general factor loadings set to be equal.
13
+ Proceed with caution.
14
+ Think about redoing the analysis with alternative values of the 'option' setting.
15
+
16
+ `,un=(e,t)=>e.map(n=>t[0].map((r,o)=>{let a=0;for(let i=0;i<n.length;i++)a+=n[i]*t[i][o];return a})),hn=e=>e[0].map((t,n)=>e.map(r=>r[n])),pn=(e,t=n=>n)=>e[0].map((n,r)=>ue(e.map(o=>t(o[r])))),qa=(e,t=n=>n)=>e.map(n=>ue(n.map(t))),$t=(e,t=n=>n)=>ue(e[0].flatMap((n,r)=>e.map(o=>t(o[r])))),nr=e=>e<0?-1:1;function vo(e){let t=e.map((n,r)=>Math.sqrt(n[r]));return e.map((n,r)=>n.map((o,a)=>r===a?1:o/(t[r]*t[a])))}function Aa(e,t){let n=vo(e),{d:r,u:o,v:a}=An(n),i=r.map((s,c)=>c).filter(s=>r[s]>Math.sqrt(wo)*r[0]),l=n.map((s,c)=>{let f=0;for(let m of i)f+=a[c][m]*(1/r[m])*o[c][m];return 1-1/f});return l.some(s=>s>1)&&Nt(t,"In smc, smcs > 1 were set to 1.0"),l.some(s=>s<0)&&Nt(t,"In smc, smcs < 0 were set to .0"),l.map(s=>s>1?1:s<0?0:s)}var Nt=(e,t)=>{e.includes(t)||e.push(t)};function po(e,t,n,r){let o=e.map((l,s)=>l.map((c,f)=>s===f?t[s]:c)),a=mt(o,{symmetric:!0}),i=a.values.slice(0,n).map(l=>Math.sqrt(r(l)));return a.vectors.map(l=>l.slice(0,n).map((s,c)=>s*i[c]))}var Gc=e=>un(e,hn(e));function ka(e,t,n,r){let o=vo(e),a=o.length,i=Aa(o,r),l=u=>{let h=po(o,u.map(w=>1-w),t,w=>w<wo?100*wo:w),p=Gc(h),d=[];for(let w=0;w<a;w++)for(let b=w+1;b<a;b++)d.push((o[b][w]-p[b][w])**2);return ue(d)},s=u=>{let h=po(o,u.map((p,d)=>o[d][d]-p),t,p=>Math.max(p,0));return u.map((p,d)=>{let w=0;for(let b=0;b<t;b++)w+=h[d][b]*h[d][b];return w+p-o[d][d]})},f=Sn(i.map(u=>1-u),l,{gr:s,method:"L-BFGS-B",lower:.005,upper:Math.max(Math.max(...i),1),control:{fnscale:1,parscale:new Array(a).fill(.01)}}).par,m=po(o,f.map((u,h)=>o[h][h]-u),t,u=>Math.max(u,0));if(m=go(m,t),m=m.map(u=>u.map(h=>h===0?1e-15:h)),qa(m,u=>u*u).some(u=>u>1)&&Nt(n,"An ultra-Heywood case was detected. Examine the results carefully"),t>1){m=go(tr(m).loadings,t);let u=pn(m,p=>p*p),h=u.map((p,d)=>d).sort((p,d)=>u[d]-u[p]||p-d);m=m.map(p=>h.map(d=>p[d]))}else m=go(m,t);return m}function go(e,t){let n=pn(e).map(nr);return t===1?n[0]<0?e.map(r=>r.map(o=>-o)):e:e.map(r=>r.map((o,a)=>n[a]*o))}function Uc(e){let t=e[0].length,n=e.map(o=>o.map(a=>a*a)),r=n.map(o=>o.map((a,i)=>{let l=0;for(let s=0;s<t;s++)s!==i&&(l+=o[s]);return l}));return{f:$t(n.map((o,a)=>o.map((i,l)=>i*r[a][l])))/4,Gq:e.map((o,a)=>o.map((i,l)=>i*r[a][l]))}}function Hc(e){let t=e[0].length,n=c=>{let f=be(c),m=un(e,hn(f)),{f:u,Gq:h}=Uc(m),p=hn(un(un(hn(m),h),f)).map(d=>d.map(w=>-w));return{L:m,f:u,G:p}},r=Array.from({length:t},(c,f)=>Array.from({length:t},(m,u)=>f===u?1:0)),o=1,a=n(r),i,l=[],s=!1;for(let c=0;c<=2e3;c++){let f=pn(r.map((w,b)=>w.map((g,v)=>g*a.G[b][v]))),m=a.G.map((w,b)=>w.map((g,v)=>g-r[b][v]*f[v])),u=Math.sqrt($t(m,w=>w*w));if(l.push(a.f),u<1e-5){s=!0;break}if(i!==void 0){let w=i,b=r.map((v,N)=>v.map((y,E)=>y-w.T[N][E])),g=m.map((v,N)=>v.map((y,E)=>y-w.Gp[N][E]));$t(g,v=>v*v)>0&&(o=$t(b,v=>v*v)/Math.abs($t(b.map((v,N)=>v.map((y,E)=>y*g[N][E])))),o=Math.max(1e-10,Math.min(o,20)))}else o=2*o;let h=Math.max(...l.slice(-10)),p=a,d=r;for(let w=0;w<=10;w++){let b=r.map((v,N)=>v.map((y,E)=>y-o*m[N][E])),g=pn(b,v=>v*v).map(v=>1/Math.sqrt(v));if(d=b.map(v=>v.map((N,y)=>N*g[y])),p=n(d),h-p.f>.5*u*u*o)break;o=o/2}i={T:r,Gp:m},r=d,a=p}return{loadings:a.L,phi:un(hn(r),r),converged:s}}function Bc(e,t={}){let n=t.nfactors??3;if(!Number.isInteger(n)||n<1)throw new RangeError("'nfactors' must be a positive integer");let r;if(t.covmat!==void 0)r=vo(t.covmat);else{if(e===null)throw new TypeError("neither 'x' nor 'covmat' supplied");r=tn(e,void 0,{use:"pairwise.complete.obs"})}let o=r.length,a=[],i=[],l=ka(r,n,a,i),s,c,f;if(n===1)s=l.map(T=>T.slice()),f=[1],Nt(i,`Omega_h for 1 factor is not meaningful, just omega_t
17
+ `),Nt(a,"Omega_h and Omega_asymptotic are not meaningful with one factor");else{let T=Hc(l);if(T.converged||Nt(a,"oblimin did not converge in 2000 iterations"),s=T.loadings,c=T.phi,n===2){let O=c[0][1],$=Math.sqrt(Math.abs(O));f=[$,nr(O)*$],Nt(i,Wc)}else f=ka(c,1,a,i).map(O=>O[0])}let m=s.map(T=>{let O=0;for(let $=0;$<n;$++)O+=T[$]*f[$];return O}),u=qa(l,T=>T*T),h=u.map((T,O)=>r[O][O]-T),p=s.map((T,O)=>T.map(($,D)=>{if(n===1)return 0;let G=r[O][O],Q=G-(G-$*$)-($*f[D])**2;return Q<0&&(Q=0),nr($)*Math.sqrt(Q)})),d=m.map((T,O)=>T*T/u[O]),w=m.map((T,O)=>{let $=[T,...p[O]];return ue($.map(D=>D*D))**2/ue($.map(D=>D**4))}),b=m.map(nr);b.some(T=>T<0)&&(r=r.map((T,O)=>T.map(($,D)=>b[O]*$*b[D])));let g=m.map((T,O)=>b[O]*T),v=p.map((T,O)=>T.map($=>b[O]*$)),N=g.map((T,O)=>[T,...v[O],u[O],h[O],d[O],w[O]]),y=$t(r),E=ue(r.map((T,O)=>T[O])),M=ue(g)**2,x=ue(h),q=M/y,k=(y-x)/y,S=M/(y-x),L=(y-E)/y*o/(o-1),A=(y+ue(Aa(r,i))-E)/y,R=pn(N.map(T=>T.slice(0,n+1)),T=>T*T),V=R[0]/ue(R),F=v.map(T=>{let O=0;for(let $=1;$<n;$++)Math.abs(T[$])>Math.abs(T[O])&&(O=$);return O}),I=[],C=[];for(let T=0;T<n;T++){let O=F.map((Q,ne)=>ne).filter(Q=>F[Q]===T),$=ue(O.flatMap(Q=>O.map(ne=>r[ne][Q]))),D=ue(O.map(Q=>v[Q][T])),G=ue(O.map(Q=>g[Q]));C.push(D*D),I.push([(D*D+G*G)/$,G*G/$,D*D/$])}let z=[[k,ue(g)**2/y,ue(C)/y],...I],J={omegaH:q,omegaLim:S,alpha:L,omegaTot:k,G6:A,ECV:V,sl:N,slNames:["g",...Array.from({length:n},(T,O)=>`F${O+1}*`),"h2","u2","p2","com"],orthog:l,oblique:s,gloading:f,key:b,omegaGroup:z,warnings:a,messages:i};return c!==void 0&&(J.phi=c),J}var ke=.9999;function No(e){let t=ee(e),n=[],r=0;for(let o=0;o<e.length-1;o++)r+=e[o],n.push(H(r/t));return n}var yo=e=>[...new Set(e)].sort((t,n)=>t-n),Ra=(e,t)=>e===1?`1 ${t} with zero marginal removed`:`${e} ${t}s with zero marginals removed`,Ta=e=>Array.isArray(e)||ArrayBuffer.isView(e),Sa=(e,t)=>e>0?P(e,{lowerTail:!1})-P(t,{lowerTail:!1}):P(t)-P(e);function Mo(e){let t=se(e),n=e.map(o=>o-t),r=Math.sqrt(re(n));return n.map(o=>o/r)}function Xc(e){if(Math.abs(e)>=.1)return e-Math.log1p(e);let t=e,n=0;for(let r=2;r<40;r++){t*=-e;let o=t/r;if(n+=o,Math.abs(o)<=1e-17*Math.abs(n))break}return-n}var rr=e=>Array.from({length:e},()=>new Array(e).fill(0));function Oa(e,t,n,r,o){e.value+=t*Math.log(n);let a=r.length;for(let i=0;i<a;i++){e.gradient[i]+=t*r[i]/n;for(let l=0;l<a;l++)e.hessian[i][l]+=t*(o[i][l]/n-r[i]*r[l]/(n*n))}}var bo={r:0,h:0,k:0,rr:0,rh:0,rk:0,hh:0,hk:0,kk:0};function Qc(e,t,n){if(e===-1/0||t===-1/0||e===1/0&&t===1/0)return bo;if(e===1/0)return{...bo,k:ye(t),kk:-t*ye(t)};if(t===1/0)return{...bo,h:ye(e),hh:-e*ye(e)};let r=(1-n)*(1+n),o=Math.sqrt(r),a=e*e-2*n*e*t+t*t,i=Math.exp(-a/(2*r))/(2*Math.PI*o),l=(t-n*e)/o,s=(e-n*t)/o,c=ye(e),f=ye(t);return{r:i,h:c*P(l),k:f*P(s),rr:i*(n/r+(e*t*r-n*a)/(r*r)),rh:-i*(e-n*t)/r,rk:-i*(t-n*e)/r,hh:-e*c*P(l)-n/o*c*ye(l),hk:i,kk:-t*f*P(s)-n/o*f*ye(s)}}function La(e,t,n,r,o){let a=e.length,i=e[0].length,l=o?a+i-1:1,s={value:0,gradient:new Array(l).fill(0),hessian:rr(l)},c=h=>o&&h>=1&&h<=a-1?h:-1,f=h=>o&&h>=1&&h<=i-1?a-1+h:-1,m=t.map(h=>n.map(p=>Qc(h,p,r))),u=[];for(let h=0;h<a;h++)for(let p=0;p<i;p++){let d=e[h][p];if(d===0)continue;let w=Zt([t[h],n[p]],[t[h+1],n[p+1]],r),b=new Array(l).fill(0),g=rr(l);for(let[v,N,y]of[[h+1,p+1,1],[h,p+1,-1],[h+1,p,-1],[h,p,1]]){let E=m[v][N],M=c(v),x=f(N);b[0]+=y*E.r,g[0][0]+=y*E.rr,M>=0&&(b[M]+=y*E.h,g[0][M]+=y*E.rh,g[M][0]+=y*E.rh,g[M][M]+=y*E.hh),x>=0&&(b[x]+=y*E.k,g[0][x]+=y*E.rk,g[x][0]+=y*E.rk,g[x][x]+=y*E.kk),M>=0&&x>=0&&(g[M][x]+=y*E.hk,g[x][M]+=y*E.hk)}Oa(s,d,w,b,g),u.push(d*Math.log(w))}return s.value=ee(u),s}function Kc(e,t,n,r,o){let a=o?n.length+1:1,i={value:0,gradient:new Array(a).fill(0),hessian:rr(a)},l=(1-r)*(1+r),s=Math.sqrt(l),c=l*s,f=new Array(a),m=rr(a),u=[],h=(p,d,w)=>{let b=n[p],g=(b-r*d)/s,v=ye(g),N=(r*b-d)/c,y=1/s,E=b/c+3*r*(r*b-d)/(c*l),M=r/c;if(f[0]+=w*v*N,m[0][0]+=w*v*(E-g*N*N),o){let x=p+1;f[x]+=w*v*y;let q=w*v*(M-g*N*y);m[0][x]+=q,m[x][0]+=q,m[x][x]+=w*v*(-g*y*y)}};for(let p=0;p<e.length;p++){let d=e[p],w=t[p];f.fill(0);for(let v of m)v.fill(0);let b=w<n.length?(n[w]-r*d)/s:1/0,g=w>0?(n[w-1]-r*d)/s:-1/0;w<n.length&&h(w,d,1),w>0&&h(w-1,d,-1),Oa(i,1,Sa(g,b),f,m),u.push(Math.log(Sa(g,b)),ye(d,{log:!0}))}return i.value=ee(u),i}function Zc(e,t){let n=new Array(e.gradient.length).fill(0),r=t.map(i=>e.gradient[i]),o=t.map(i=>t.map(l=>-e.hessian[i][l])),a;try{a=be(o,r)}catch{a=void 0}return(a===void 0||!a.every(Number.isFinite)||ee(a.map((i,l)=>i*r[l]))<=0)&&(a=r.map((i,l)=>i/Math.max(Math.abs(o[l][l]),1))),t.forEach((i,l)=>n[i]=a[l]),n}var Fa=(e,t)=>e>=t-1e-12*Math.abs(t);function Ia(e,t,n){let r=[...t],o=e(r),a=n,i=r.map((l,s)=>s);for(let l=0;l<200;l++){let s=a?i.slice(1):i;if(s.length===0)break;let c=Zc(o,s);if(!a&&Math.abs(r[0])===ke&&c[0]*r[0]>0){a=!0;continue}let f=Math.abs(r[0]+c[0])>ke?(Math.sign(c[0])*ke-r[0])/c[0]:1,m=!1;for(let u=0;u<60;u++){let h=r.map((d,w)=>d+f*c[w]);f*c[0]!==0&&Math.abs(h[0])>=ke*(1-1e-15)&&(h[0]=Math.sign(h[0])*ke);let p=e(h);if(Fa(p.value,o.value)){let d=Math.max(...h.map((w,b)=>Math.abs(w-r[b])/Math.max(1,Math.abs(r[b]))));r=h,o=p,m=d>1e-12;break}f/=2}if(!m)break}return{theta:r,at:o,bounded:a}}function _a(e,t){let n=Ia(e,[Math.max(-ke,Math.min(ke,t[0])),...t.slice(1)],!1);if(n.bounded||n.theta[0]===0)return n;let r=Ia(e,[Math.sign(n.theta[0])*ke,...n.theta.slice(1)],!0);return Fa(r.at.value,n.at.value)?r:n}function Ca(e,t){let n=()=>e.map(o=>o.map(()=>1/0));if(t)return n();let r;try{r=be(e.map(o=>o.map(a=>-a)))}catch{return n()}return r.map((o,a)=>o.map((i,l)=>(i+r[l][a])/2))}var Va=e=>`inadmissible correlation set to ${e>0?"0.9999":"-0.9999"}`;function Yc(e,t,n){let r,o;if(Ta(t)){let R=e,V=t;if(R.length!==V.length)throw new RangeError("'x' and 'y' must have the same length");let F=R.map((T,O)=>!Number.isNaN(T)&&!Number.isNaN(V[O])),I=R.filter((T,O)=>F[O]),C=V.filter((T,O)=>F[O]),z=new Map(yo(I).map((T,O)=>[T,O])),J=new Map(yo(C).map((T,O)=>[T,O]));r=Array.from({length:z.size},()=>new Array(J.size).fill(0)),I.forEach((T,O)=>r[z.get(T)][J.get(C[O])]++),o=n??{}}else{let R=e;if(!R.every(Ta))throw new TypeError("give a table (array of rows) or two vectors");let V=R[0]?.length??0;if(R.some(F=>F.length!==V))throw new TypeError("the table must have rows of equal length");r=R.map(F=>Array.from(F)),o=t??{}}let{ML:a=!1,stdErr:i=!1}=o,l=[],s=r.map(R=>ee(R)),c=s.filter(R=>R===0).length;c>0&&(l.push(Ra(c,"row")),r=r.filter((R,V)=>s[V]!==0));let f=(r[0]??[]).map((R,V)=>ee(r.map(F=>F[V]))),m=f.filter(R=>R===0).length;m>0&&(l.push(Ra(m,"column")),r=r.map(R=>R.filter((V,F)=>f[F]!==0)));let u=r.length,h=r[0]?.length??0;if(u<2&&l.push("the table has fewer than 2 rows"),h<2&&l.push("the table has fewer than 2 columns"),u<2||h<2)return{rho:NaN,warnings:l};let p=[-1/0,...No(r.map(R=>ee(R))),1/0],d=[-1/0,...No(r[0].map((R,V)=>ee(r.map(F=>F[V])))),1/0],b=Qo(R=>{let V=Math.max(-ke,Math.min(ke,R)),F=[];for(let I=0;I<u;I++)for(let C=0;C<h;C++){if(r[I][C]===0)continue;let z=Zt([p[I],d[C]],[p[I+1],d[C+1]],V);F.push(r[I][C]*Math.log(z))}return-ee(F)},-ke,ke).optimum;if(!a&&!i)return{rho:b,warnings:l};let g=R=>[-1/0,...R,1/0],v=p.slice(1,u),N=d.slice(1,h),y=R=>a?La(r,g(R.slice(1,u)),g(R.slice(u)),R[0],!0):La(r,p,d,R[0],!1),{theta:E,at:M,bounded:x}=_a(y,a?[b,...v,...N]:[b]),q=E[0];if(x&&l.push(Va(q)),!i)return{rho:q,warnings:l};let k=a?g(E.slice(1,u)):p,S=a?g(E.slice(u)):d,L=ee(r.map(R=>ee(R))),A=[];for(let R=0;R<u;R++)for(let V=0;V<h;V++){let F=r[R][V],I=Zt([k[R],S[V]],[k[R+1],S[V+1]],q);F===0?A.push(L*I):A.push(F*Math.log1p(1e-6/F),F*Xc(L*I/F-1))}return{rho:q,warnings:l,var:Ca(M.hessian,x),chisq:2*ee(A),df:u*h-u-h,n:L,rowCuts:k.slice(1,u),colCuts:S.slice(1,h)}}function Jc(e,t,n,r){let o=e.length,a=[H(.25),H(.5),H(.75)],i=(h,p)=>p.filter(d=>d<h).length,l=Mo(e).map(h=>i(h,n)),s=Mo(t).map(h=>i(h,a)),c=Array.from({length:n.length+1},()=>new Array(4).fill(0));l.forEach((h,p)=>c[h][s[p]]++);let f=[-1/0,...n,1/0],m=[-1/0,...a,1/0],u=[];return c.forEach((h,p)=>h.forEach((d,w)=>{if(d===0)return;let b=Zt([f[p],m[w]],[f[p+1],m[w+1]],r);u.push(d*Math.log((d+1e-6)/(b*o)))})),2*ee(u)}function ef(e,t,n={}){if(e.length!==t.length)throw new RangeError("'x' and 'y' must have the same length");let{ML:r=!1,stdErr:o=!1}=n,a=e.map((x,q)=>!Number.isNaN(x)&&!Number.isNaN(t[q])),i=e.filter((x,q)=>a[q]),l=t.filter((x,q)=>a[q]),s=i.length,c=yo(l);if(c.length<2)return{rho:NaN,warnings:["y has fewer than 2 levels"]};let f=new Map(c.map((x,q)=>[x,q+1])),m=l.map(x=>f.get(x)),u=c.map(()=>0);for(let x of m)u[x-1]++;let h=No(u),p=[],d=Math.sqrt((s-1)/s)*Math.sqrt(re(m))*Ko(i,m)[0]/ee(h.map(x=>ye(x)));if(Math.abs(d)>ke){let x=Math.sign(d)*ke;p.push(`initial correlation inadmissible, ${Tt(d)}, set to ${Tt(x)}`),d=x}if(!r&&!o)return{rho:d,warnings:p};let w=Mo(i),b=m.map(x=>x-1),g=x=>Kc(w,b,r?x.slice(1):h,x[0],r),{theta:v,at:N,bounded:y}=_a(g,r?[d,...h]:[d]);if(y&&p.push(Va(v[0])),!o)return{rho:v[0],warnings:p};let E=r?v.slice(1):h,M=c.length;return{rho:v[0],warnings:p,var:Ca(N.hessian,y),chisq:Jc(m,w,E,r?v[0]:d),df:4*M-M-1,n:s,cuts:E}}export{Qr as Anova,el as TukeyHSD,Lc as adTest,Br as anova,xs as ansariTest,Vr as aov,rc as aovEz,Xr as aovSummary,us as bartlettTest,yr as binomTest,Pi as brunnerMunzelTest,ja as chisqTest,wi as cochranQTest,Zs as cohenES,Gl as cohensD,Ki as confint,$a as corTest,Ec as coxph,$c as cronbachAlpha,Ic as cvmTest,mc as dscfAllPairsTest,dc as dunnTest,cc as dunnettTest,ec as epsilonSquared,Al as esH,Rl as esW1,Tl as esW2,Yl as etaSquared,zc as factanal,Ba as fisherTest,ds as flignerTest,kr as friedmanTest,uc as gamesHowellTest,wc as glhtDunnett,il as glm,gl as glmAnova,hl as glmConfint,ml as glmCooksDistance,Wr as glmHatvalues,ul as glmPredict,zr as glmResiduals,fl as glmRstandard,jr as glmSummary,Ul as hedgesG,Dc as icc,Sc as jonckheereTest,Cc as kappa2,Vc as kappamFleiss,Dn as kruskalTest,Ts as ksTest,ic as kwAllPairsDunnTest,lc as kwManyOneDunnTest,kc as leveneTest,Oc as lillieTest,Cr as lm,Li as mantelhaenTest,fc as manyOneUTest,Ml as mauchlyTest,di as mcnemarTest,Ft as modelMatrix,gs as moodTest,Bc as omega,Jl as omegaSquared,bs as onewayTest,It as pAdjust,Ui as pairwisePropTest,Wi as pairwiseTTest,Gi as pairwiseWilcoxTest,Pc as partialR,_c as pearsonTest,pi as poissonTest,Yc as polychor,ef as polyserial,kl as powerAnovaTest,El as powerPropTest,xl as powerTTest,Zi as predict,Mr as propTest,qc as propTrendTest,Vl as pwr2p2nTest,Fl as pwr2pTest,Pl as pwrAnovaTest,$l as pwrChisqTest,jl as pwrF2Test,Cl as pwrNormTest,_l as pwrPTest,Dl as pwrRTest,Ol as pwrT2nTest,Il as pwrTTest,ps as quadeTest,Fc as sfTest,Ri as shapiroTest,pc as steelTest,bc as survdiff,Nc as survfit,Lt as tTest,hc as tukeyTest,ms as varTest,nn as wilcoxTest,tl as zTest};