@numero-uno-jpn/rstats 0.8.0 → 0.10.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 (122) hide show
  1. package/CHANGELOG.md +88 -0
  2. package/README.ja.md +89 -13
  3. package/README.md +90 -13
  4. package/THIRD_PARTY_NOTICES.md +12 -2
  5. package/dist/chunks/{chunk-OHPHNE3O.js → chunk-2XKG3DX4.js} +1 -1
  6. package/dist/chunks/{chunk-3I6EKGB6.js → chunk-4NM37MGA.js} +2 -2
  7. package/dist/chunks/chunk-7HPVUVZB.js +2 -0
  8. package/dist/chunks/{chunk-I3LAZE6C.js → chunk-AECGWZHD.js} +2 -2
  9. package/dist/chunks/chunk-ENL43FNO.js +2 -0
  10. package/dist/chunks/chunk-EWC6I75B.js +2 -0
  11. package/dist/chunks/chunk-IT354SHV.js +3 -0
  12. package/dist/chunks/{chunk-VIPSPG3Y.js → chunk-LIMZKONM.js} +2 -2
  13. package/dist/chunks/chunk-O6PT7IVU.js +2 -0
  14. package/dist/chunks/{chunk-YGWPU67I.js → chunk-QFULZNHL.js} +1 -1
  15. package/dist/chunks/{chunk-XU4BGFZ6.js → chunk-QJJWGNJP.js} +2 -2
  16. package/dist/chunks/{chunk-32PRRONF.js → chunk-QULPY6YM.js} +1 -1
  17. package/dist/chunks/chunk-XWTZ4RTR.js +2 -0
  18. package/dist/chunks/chunk-ZRIM4R4A.js +2 -0
  19. package/dist/descriptive/factanal.d.ts +56 -0
  20. package/dist/descriptive/factanal.js +186 -0
  21. package/dist/descriptive/omega.d.ts +56 -0
  22. package/dist/descriptive/omega.js +323 -0
  23. package/dist/descriptive.js +2 -2
  24. package/dist/distributions/bvnorm.d.ts +15 -0
  25. package/dist/distributions/bvnorm.js +131 -0
  26. package/dist/distributions/nbeta.js +34 -1
  27. package/dist/distributions/nchisq.d.ts +10 -0
  28. package/dist/distributions/nchisq.js +46 -0
  29. package/dist/distributions.js +2 -2
  30. package/dist/entries/distributions.d.ts +1 -0
  31. package/dist/entries/distributions.js +1 -0
  32. package/dist/entries/linalg.d.ts +2 -0
  33. package/dist/entries/linalg.js +2 -0
  34. package/dist/entries/numerics.d.ts +2 -0
  35. package/dist/entries/numerics.js +1 -0
  36. package/dist/entries/tests.d.ts +25 -1
  37. package/dist/entries/tests.js +22 -1
  38. package/dist/htest/agreement.d.ts +141 -0
  39. package/dist/htest/agreement.js +274 -0
  40. package/dist/htest/aov.d.ts +19 -0
  41. package/dist/htest/aov.js +22 -15
  42. package/dist/htest/core/simulate.d.ts +2 -0
  43. package/dist/htest/core/simulate.js +1 -1
  44. package/dist/htest/dunnett.d.ts +5 -0
  45. package/dist/htest/dunnett.js +89 -0
  46. package/dist/htest/effectSize.d.ts +112 -0
  47. package/dist/htest/effectSize.js +385 -0
  48. package/dist/htest/leveneTest.d.ts +30 -0
  49. package/dist/htest/leveneTest.js +41 -0
  50. package/dist/htest/multipleComparisons.d.ts +147 -0
  51. package/dist/htest/multipleComparisons.js +369 -0
  52. package/dist/htest/normalityTests.d.ts +44 -0
  53. package/dist/htest/normalityTests.js +164 -0
  54. package/dist/htest/polycor.d.ts +117 -0
  55. package/dist/htest/polycor.js +468 -0
  56. package/dist/htest/power.d.ts +132 -0
  57. package/dist/htest/power.js +227 -0
  58. package/dist/htest/pwr.d.ts +263 -0
  59. package/dist/htest/pwr.js +440 -0
  60. package/dist/htest/rankTests.d.ts +7 -0
  61. package/dist/htest/rankTests.js +11 -1
  62. package/dist/htest/trendTests.d.ts +57 -0
  63. package/dist/htest/trendTests.js +167 -0
  64. package/dist/linalg/eigen.d.ts +8 -0
  65. package/dist/linalg/eigen.js +5 -4
  66. package/dist/linalg/expm.d.ts +48 -0
  67. package/dist/linalg/expm.js +472 -0
  68. package/dist/linalg/schur.d.ts +48 -0
  69. package/dist/linalg/schur.js +225 -0
  70. package/dist/linalg.js +2 -2
  71. package/dist/models/anovaSequential.d.ts +58 -0
  72. package/dist/models/anovaSequential.js +138 -0
  73. package/dist/models/aovEz.d.ts +64 -0
  74. package/dist/models/aovEz.js +332 -0
  75. package/dist/models/aovStrata.d.ts +32 -0
  76. package/dist/models/aovStrata.js +104 -0
  77. package/dist/models/carAnova.d.ts +30 -0
  78. package/dist/models/carAnova.js +121 -0
  79. package/dist/models/coxph.d.ts +101 -0
  80. package/dist/models/coxph.js +502 -0
  81. package/dist/models/ddLeastSquares.d.ts +30 -0
  82. package/dist/models/ddLeastSquares.js +90 -0
  83. package/dist/models/ddMatrix.d.ts +10 -0
  84. package/dist/models/ddMatrix.js +91 -0
  85. package/dist/models/formula.d.ts +83 -0
  86. package/dist/models/formula.js +333 -0
  87. package/dist/models/glm.d.ts +226 -0
  88. package/dist/models/glm.js +546 -0
  89. package/dist/models/glmAnova.d.ts +31 -0
  90. package/dist/models/glmAnova.js +146 -0
  91. package/dist/models/glmConfint.d.ts +25 -0
  92. package/dist/models/glmConfint.js +139 -0
  93. package/dist/models/glmFamily.d.ts +66 -0
  94. package/dist/models/glmFamily.js +359 -0
  95. package/dist/models/lm.d.ts +84 -3
  96. package/dist/models/lm.js +141 -44
  97. package/dist/models/mauchly.d.ts +48 -0
  98. package/dist/models/mauchly.js +123 -0
  99. package/dist/models/modelFrame.d.ts +40 -0
  100. package/dist/models/modelFrame.js +131 -0
  101. package/dist/models/modelMatrix.d.ts +88 -0
  102. package/dist/models/modelMatrix.js +170 -0
  103. package/dist/models/survival.d.ts +87 -0
  104. package/dist/models/survival.js +238 -0
  105. package/dist/numerics.js +2 -2
  106. package/dist/optim/lbfgsb.d.ts +20 -0
  107. package/dist/optim/lbfgsb.js +1240 -0
  108. package/dist/optim/optim.d.ts +85 -0
  109. package/dist/optim/optim.js +610 -0
  110. package/dist/rstats.js +19 -3
  111. package/dist/special.js +2 -2
  112. package/dist/tests.js +18 -3
  113. package/llms.txt +102 -0
  114. package/package.json +3 -2
  115. package/dist/chunks/chunk-EJRCDJ76.js +0 -2
  116. package/dist/chunks/chunk-JERGQKZM.js +0 -2
  117. package/dist/chunks/chunk-JVYV4633.js +0 -2
  118. package/dist/chunks/chunk-LXMMKO26.js +0 -2
  119. package/dist/chunks/chunk-MJQLKEO5.js +0 -2
  120. package/dist/chunks/chunk-RZ7SBJEG.js +0 -2
  121. package/dist/chunks/chunk-SPQC445Q.js +0 -2
  122. package/dist/chunks/chunk-U633VFH3.js +0 -2
package/dist/tests.js CHANGED
@@ -1,3 +1,18 @@
1
- /*! rStats.js 0.8.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{$ as qt,B as At,C as it,G as ut,H as Bt,M as ht,N as Me,R as pt,S as ye,W as ve,X as xe,Y as Ee,_ as Rt,c as K,d as Ot,l as _t,m as at,r as G,s as wt,x as $t}from"./chunks/chunk-JERGQKZM.js";import{h as ke,k as Ie,l as Se,t as Le,y as Ve}from"./chunks/chunk-JVYV4633.js";import{a as V,b as kt,c as X,f as cn}from"./chunks/chunk-U633VFH3.js";import{k as W,l as ot}from"./chunks/chunk-LXMMKO26.js";import{c as Te}from"./chunks/chunk-SPQC445Q.js";import"./chunks/chunk-32PRRONF.js";import{a as Nt,b as Z,c as U,e as Ae,f as $,g as lt,h as ct,i as Tt}from"./chunks/chunk-VIPSPG3Y.js";import{a as Re,d as qe}from"./chunks/chunk-XU4BGFZ6.js";import{b as be}from"./chunks/chunk-MJQLKEO5.js";import{g as P,h as F,s as Ne}from"./chunks/chunk-3I6EKGB6.js";import{b as zt,c as vt,d as C,e as ge,f as Q,g as xt,i as Et,k as we}from"./chunks/chunk-OHPHNE3O.js";function Ht(e,t,o={}){let n;t!==void 0&&!ArrayBuffer.isView(t)&&!Array.isArray(t)?o=t:n=t;let r=V(o.alternative),s=o.mu??0,a=o.paired??!1,l=o.varEqual??!1,i=o.confLevel??.95;if(Number.isNaN(s))throw new RangeError("mu must be a number");if(kt(i),a&&n===void 0)throw new TypeError("y is required when paired is true");let f=Array.from(e),c=n===void 0?void 0:Array.from(n);if(a){let[E,R]=Tt(f,c);f=E.map((k,v)=>k-R[v]),c=void 0}else f=ct(f),c!==void 0&&(c=ct(c));let m,u,h,p,M,d,b,w=f.length;if(w<2)throw new Error("not enough 'x' observations");let g=Z(f),N=U(f);if(c===void 0){if(u=w-1,h=Math.sqrt(N/w),h<10*Number.EPSILON*Math.abs(g))throw new Error("data are essentially constant");m=(g-s)/h,d=a?"Paired t-test":"One Sample t-test",p=g,M=a?"mean difference":"mean of x",b=a?"mean difference":"mean"}else{let E=c.length;if(E<1||!l&&E<2)throw new Error("not enough 'y' observations");if(l&&w+E<3)throw new Error("not enough observations");let R=Z(c),k=U(c);if(d=`${l?"":"Welch"} Two Sample t-test`,p=[g,R],M=["mean of x","mean of y"],b="difference in means",l){u=w+E-2;let v=0;w>1&&(v+=(w-1)*N),E>1&&(v+=(E-1)*k),v/=u,h=Math.sqrt(v*(1/w+1/E))}else{let v=N/w,q=k/E;h=Math.sqrt(v+q),u=(h*h)**2/(v*v/(w-1)+q*q/(E-1))}if(h<10*Number.EPSILON*Math.max(Math.abs(g),Math.abs(R)))throw new Error("data are essentially constant");m=(g-R-s)/h}let y,x;if(r==="less")y=W(m,u,!0,!1),x=[-1/0,m+ot(i,u,!0,!1)];else if(r==="greater")y=W(m,u,!1,!1),x=[m-ot(i,u,!0,!1),1/0];else{y=2*W(-Math.abs(m),u,!0,!1);let E=1-i,R=ot(1-E/2,u,!0,!1);x=[m-R,m+R]}return x=[s+x[0]*h,s+x[1]*h],{method:d,alternative:r,statistic:m,statisticName:"t",parameter:u,parameterName:"df",pValue:y,confInt:x,confLevel:i,estimate:p,estimateNames:M,nullValue:s,nullValueName:b,stdErr:h}}function bt(e){return` with simulated p-value
3
- (based on ${fn(e)} replicates)`}function fn(e){if(!Number.isFinite(e)||e===0)return String(e);let t=e.toExponential(14).replace(/\.?0+e/,"e"),[o,n]=t.split("e"),r=Number(n),s=`${o}e${r<0?"-":"+"}${String(Math.abs(r)).padStart(2,"0")}`,a=String(Number(e.toPrecision(15)));return a.length<=s.length?a:s}function J(e){let t=[0,0],o=0;for(let n of e)t=C(t,[n,0]),o+=n;return Number.isFinite(o)?t[0]:o}function mn(e,t,o){return Array.isArray(t)?un(e,t,o??{}):hn(e,t??{})}var Fe=(e,t)=>e<t?-1:e>t?1:0;function un(e,t,o){if(e.length!==t.length)throw new RangeError("'x' and 'y' must have the same length");let n=u=>u==null;for(let u of[...e,...t])if(!n(u)&&typeof u!="string")throw new TypeError("factor values must be strings (or null for missing values)");let r=[],s=[];e.forEach((u,h)=>{let p=t[h];!n(u)&&!n(p)&&(r.push(u),s.push(p))});let a=[...new Set(r)].sort(Fe),l=[...new Set(s)].sort(Fe);if(a.length<2||l.length<2)throw new RangeError("'x' and 'y' must have at least 2 levels");let i=new Map(a.map((u,h)=>[u,h])),f=new Map(l.map((u,h)=>[u,h])),c=a.map(()=>l.map(()=>0));return r.forEach((u,h)=>c[i.get(u)][f.get(s[h])]++),{...Oe(c,o),rowLevels:a,colLevels:l}}function hn(e,t){if(e.length>0&&typeof e[0]!="number"){let n=e.map(s=>Array.from(s)),r=n[0].length;if(n.some(s=>s.length!==r))throw new TypeError("all rows of the matrix must have the same length");return n.length===1?Wt(n[0],t):r===1?Wt(n.map(s=>s[0]),t):Oe(n,t)}return Wt(Array.from(e),t)}function Ce(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 Pe(e,t,o){let n=(1-64*Number.EPSILON)*t,r=0;for(let s=0;s<e.length;s++)e[s]>=n&&r++;return(1+r)/(o+1)}function je(e,t){return Number.isNaN(e)?NaN:K(e,t/2,2,!1,!1)}function ze(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 Wt(e,t){let o=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(o<2)throw new TypeError("'x' must have at least 2 elements");let n=J(e);if(!(n>0))throw new RangeError("at least one entry of 'x' must be positive");let r=t.p===void 0?e.map(()=>1/o):Array.from(t.p);if(r.length!==o)throw new TypeError("'x' and 'p' must have the same number of elements");if(r.some(Number.isNaN))throw new RangeError("p contains missing values");if(r.some(m=>m<0))throw new RangeError("probabilities must be non-negative");if(t.rescaleP??!1){let m=J(r);r=r.map(u=>u/m)}else if(Math.abs(J(r)-1)>Math.sqrt(Number.EPSILON))throw new RangeError("probabilities must sum to 1");let s=r.map(m=>n*m);ze(s);let a=r.map((m,u)=>s[u]*(1-m)),l=J(e.map((m,u)=>(m-s[u])**2/s[u])),i="Chi-squared test for given probabilities",f=o-1,c;if(t.simulatePValue??!1){let m=Ce(t);i+=bt(m),f=NaN,c=Pe(pn(n,r,s,m),l,m)}else c=je(l,f);return{method:i,statistic:l,statisticName:"X-squared",parameter:f,parameterName:"df",pValue:c,observed:e,expected:s,residuals:e.map((m,u)=>(m-s[u])/Math.sqrt(s[u])),stdres:e.map((m,u)=>(m-s[u])/Math.sqrt(a[u]))}}function pn(e,t,o,n){let r=t.length,s=Ee(r,{size:Math.trunc(n*e),replace:!0,prob:t}),a=Math.trunc(e);if(s.length===0)return new Float64Array(0);if(a===0)throw new RangeError("invalid 'nrow' value (too large or NA)");let l=Math.ceil(s.length/a),i=new Float64Array(l),f=new Float64Array(r);for(let c=0;c<l;c++){f.fill(0);for(let u=0;u<a;u++)f[s[(c*a+u)%s.length]]++;let m=0;for(let u=0;u<r;u++)m+=(f[u]-o[u])**2/o[u];i[c]=m}return i}function dn(e,t,o,n){let r=e.length,s=t.length,a=e.reduce((c,m)=>c+m,0),l=Rt(a),i=new Int32Array(r*s),f=new Float64Array(Math.trunc(n));for(let c=0;c<f.length;c++){qt(e,t,l,i);let m=0;for(let u=0;u<s;u++)for(let h=0;h<r;h++){let p=o[h][u];m+=(i[h+u*r]-p)**2/p}f[c]=m}return f}function Oe(e,t){let o=t.correct??!0,n=e.length,r=e[0].length,s=e.flat();if(s.some(w=>!(w>=0&&w<1/0)))throw new RangeError("all entries of 'x' must be non-negative and finite");let a=J(s);if(!(a>0))throw new RangeError("at least one entry of 'x' must be positive");let l=e.map(w=>J(w)),i=e[0].map((w,g)=>J(e.map(N=>N[g]))),f=l.map(w=>i.map(g=>w*g/a));ze(f.flat());let c=e.map((w,g)=>w.map((N,y)=>N-f[g][y])),m="Pearson's Chi-squared test",u,h=(n-1)*(r-1),p,M=(t.simulatePValue??!1)&&l.every(w=>w>0)&&i.every(w=>w>0),d=!M&&o&&n===2&&r===2?Math.min(.5,...c.flat().map(Math.abs)):0;if(M){let w=Ce(t);m+=bt(w),u=J(c.flatMap((N,y)=>N.map((x,E)=>x*x/f[y][E]))),h=NaN;let g=dn(l.map(N=>Math.trunc(N)),i.map(N=>Math.trunc(N)),f,w);p=Pe(g,u,w)}else d>0?(m+=" with Yates' continuity correction",u=J(c.flatMap((w,g)=>w.map((N,y)=>(Math.abs(N)-d)**2/f[g][y])))):u=J(c.flatMap((w,g)=>w.map((N,y)=>N*N/f[g][y])));let b=f.map((w,g)=>w.map((N,y)=>N*(1-l[g]/a)*(1-i[y]/a)));return{method:m,statistic:u,statisticName:"X-squared",parameter:h,parameterName:"df",pValue:p??je(u,h),observed:e,expected:f,residuals:c.map((w,g)=>w.map((N,y)=>N/Math.sqrt(f[g][y]))),stdres:c.map((w,g)=>w.map((N,y)=>N/Math.sqrt(b[g][y])))}}var gn=Math.pow(Number.EPSILON,.25);function Y(e,t,o,n={}){let r={tol:n.tol??gn,maxiter:n.maxIter??1e3};return n.fLower!==void 0&&(r.fLower=n.fLower),n.fUpper!==void 0&&(r.fUpper=n.fUpper),Te(e,t,o,r).root}function wn(e,t={}){let o=e.length,n=o===0?0:e[0].length;if(e.some(i=>i.length!==n))throw new TypeError("all rows of the matrix must have the same length");if(o<2||n<2)throw new TypeError("'x' must have at least 2 rows and columns");let r=e.map(i=>Array.from(i));if(r.flat().some(i=>Number.isNaN(i)||i<0))throw new RangeError("all entries of 'x' must be non-negative and finite");if(r.flat().some(i=>i===1/0))throw new RangeError("'x' has entries too large to be integer");let s=r.map(i=>i.map(be)),a="Fisher's Exact Test for Count Data";if(o===2&&n===2)return yn(s,t,a);let l=t.alternative??"two.sided";if(t.simulatePValue??!1){let i=t.B??2e3;if(!(i>=0&&i<2**31))throw new RangeError("'B' must be a non-negative number");return{method:a+bt(i),alternative:l,pValue:Nn(s,i)}}return{method:a,alternative:l,pValue:vn(s)}}function Nn(e,t){let o=e.filter(M=>M.some(d=>d>0)),n=e[0].map((M,d)=>d).filter(M=>e.some(d=>d[M]>0));if(o.length<=1)throw new RangeError("need 2 or more non-zero row marginals");if(n.length<=1)throw new RangeError("need 2 or more non-zero column marginals");let r=o.map(M=>n.map(d=>M[d])),s=r.map(M=>M.reduce((d,b)=>d+b,0)),a=r[0].map((M,d)=>r.reduce((b,w)=>b+w[d],0)),l=s.length,i=a.length,f=[0,0];for(let M of r.flat())f=C(f,[-qe(M),0]);let c=f[0]/(1+64*Number.EPSILON),m=Rt(s.reduce((M,d)=>M+d,0)),u=new Int32Array(l*i),h=0,p=Math.trunc(t);for(let M=0;M<p;M++){qt(s,a,m,u);let d=0;for(let b=0;b<u.length;b++)d-=m[u[b]];d<=c&&h++}return Math.max(0,Math.min(1,(1+h)/(t+1)))}var bn=1+1e-7,Mn=16;function Dt(e,t,o){let n=[0,0];for(let r=Math.max(0,t);r<=Math.min(o,e.length-1);r++)e[r]!==0&&(n=C(n,[e[r],0]));return n[0]}var Ut=class{m;n;k;lo;hi;logCentral;constructor(t,o,n){this.m=t,this.n=o,this.k=n,this.lo=Math.max(0,n-o),this.hi=Math.min(n,t),this.logCentral=new Float64Array(this.hi-this.lo+1);let r=0,s=1;for(let a=this.lo;a<=this.hi;a++){let l=a-this.lo;l%Mn===0||!(s>0&&s<1/0)?(r=ut(a,t,o,n,!0),s=1,this.logCentral[l]=r):this.logCentral[l]=r+Math.log(s),s*=(t-a)*(n-a)/((a+1)*(o-n+a+1))}}density(t){let o=this.logCentral.length,n=Math.log(t),r=new Float64Array(o),s=-1/0;for(let l=0;l<o;l++){let i=this.logCentral[l]+(this.lo+l)*n;r[l]=i,i>s&&(s=i)}for(let l=0;l<o;l++){let i=r[l]-s;r[l]=i<-746?0:Math.exp(i)}let a=Dt(r,0,o-1);for(let l=0;l<o;l++)r[l]!==0&&(r[l]=r[l]/a);return r}mean(t){if(t===0)return this.lo;if(t===1/0)return this.hi;let o=this.density(t),n=[0,0];for(let r=0;r<o.length;r++)o[r]!==0&&(n=C(n,[(this.lo+r)*o[r],0]));return n[0]}tail(t,o,n){if(o===1)return n?Bt(t-1,this.m,this.n,this.k,!1,!1):Bt(t,this.m,this.n,this.k,!0,!1);if(o===0||o===1/0){let a=o===0?this.lo:this.hi;return(n?t<=a:t>=a)?1:0}let r=this.density(o),s=t-this.lo;return n?Dt(r,s,r.length-1):Dt(r,0,s)}};function Kt(e,t){return t?Y(e,0,1):1/Y(o=>e(1/o),Number.EPSILON,1)}function yn(e,t,o){let n=V(t.alternative),r=t.or??1;if(!(r>=0))throw new RangeError("'or' must be a single number between 0 and Inf");let s=t.confLevel??.95;if(!(s>0&&s<1))throw new RangeError("confLevel must be a single number between 0 and 1");let[[a,l],[i,f]]=e,c=a,m=new Ut(a+i,l+f,a+l),{lo:u,hi:h}=m,p;if(n==="less")p=m.tail(c,r,!1);else if(n==="greater")p=m.tail(c,r,!0);else if(r===0)p=c===u?1:0;else if(r===1/0)p=c===h?1:0;else{let N=m.density(r),y=N[c-u]*bn,x=[0,0];for(let E of N)E<=y&&(x=C(x,[E,0]));p=x[0]}let M;if(c===u)M=0;else if(c===h)M=1/0;else{let N=m.mean(1);M=N===c?1:Kt(y=>m.mean(y)-c,N>c)}let d={method:o,alternative:n,pValue:p,estimate:M,estimateNames:"odds ratio",nullValue:r,nullValueName:"odds ratio"};if(!(t.confInt??!0))return d;let b=N=>{if(c===h)return 1/0;let y=m.tail(c,1,!1);return y===N?1:Kt(x=>m.tail(c,x,!1)-N,y<N)},w=N=>{if(c===u)return 0;let y=m.tail(c,1,!0);return y===N?1:Kt(x=>m.tail(c,x,!0)-N,y>N)},g;if(n==="less")g=[0,b(1-s)];else if(n==="greater")g=[w(1-s),1/0];else{let N=(1-s)/2;g=[w(N),b(N)]}return{...d,confInt:g,confLevel:s}}var It=[[0,0]];function tt(e){for(let t=It.length;t<=e;t++)It.push(C(It[t-1],we([t,0])));return It[e]}function St(e,t,o,n){let r=e+o,s=r-e,a=e-(r-s)+(o-s)+t+n,l=r+a;return[l,a-(l-r)]}var Xt=class{scale=-1/0;hi=0;lo=0;add(t,o,n){if(t>this.scale){let i=Number.isFinite(this.scale)?Math.exp(this.scale-t):0;this.hi*=i,this.lo*=i,this.scale=t}let r=n*Math.exp(t-this.scale)*(1+o),s=this.hi+r,a=s-this.hi,l=this.hi-(s-a)+(r-a)+this.lo;this.hi=s+l,this.lo=l-(this.hi-s)}value(){return this.scale===-1/0?0:this.hi*Math.exp(this.scale)}};function vn(e){let t=e.filter(g=>g.some(N=>N>0));if(t.length===0)throw new RangeError("all entries of the table are zero");let o=t[0].map((g,N)=>t.some(y=>y[N]>0));if(t=t.map(g=>g.filter((N,y)=>o[y])),t.length<2||t[0].length<2)return 1;t.length>t[0].length&&(t=t[0].map((g,N)=>t.map(y=>y[N])));let n=t.length,r=t.map(g=>g.reduce((N,y)=>N+y,0)),s=t[0].map((g,N)=>t.reduce((y,x)=>y+x[N],0)).sort((g,N)=>N-g),a=s.length,l=r.reduce((g,N)=>g+N,0),i=ge(tt(l));for(let g of r)i=C(i,tt(g));for(let g of s)i=C(i,tt(g));let f=[0,0];for(let g of t.flat())f=Q(f,tt(g));let c=C(f,[Math.log1p(1e-7),0]),m=1e-9*(1+Math.abs(c[0])),[u,h]=c,p=Array.from({length:a},()=>new Map),M=(g,N)=>{let y=N.join(","),x=p[g].get(y);if(x!==void 0)return x;let E=C(i,tt(N.reduce((A,L)=>A+L,0)));for(let A of N)E=Q(E,tt(A));for(let A=g;A<a;A++)E=Q(E,tt(s[A]));let R={R:N,KZ:E,max:0,min:0,edges:[],pastHi:[],pastLo:[],count:[],index:new Map};if(p[g].set(y,R),g===a-1){let A=[0,0];for(let L of N)A=Q(A,tt(L));return R.max=R.min=A[0],R.edges.push({to:R,hi:A[0],lo:A[1],mult:1}),R}let k=new Map,v=new Array(n+1).fill(0);for(let A=n-1;A>=0;A--)v[A]=v[A+1]+N[A];let q=new Array(n).fill(0),I=(A,L,j)=>{if(A===n-1){q[A]=L;let O=Q(j,tt(L)),et=N.map((rt,dt)=>rt-q[dt]).sort((rt,dt)=>rt-dt),B=M(g+1,et),D=`${et.join(",")}|${O[0]}`,nt=k.get(D);nt===void 0?k.set(D,{to:B,hi:O[0],lo:O[1],mult:1}):nt.mult++;return}let z=Math.max(0,L-v[A+1]),H=Math.min(N[A],L);for(let O=z;O<=H;O++)q[A]=O,I(A+1,L-O,Q(j,tt(O)))};if(I(0,s[g],[0,0]),R.edges=[...k.values()],R.max=Math.max(...R.edges.map(A=>A.hi+A.to.max)),R.min=Math.min(...R.edges.map(A=>A.hi+A.to.min)),g===a-2){let A=R.edges.map(B=>{let D=B.to.edges[0];return{v:C([B.hi,B.lo],[D.hi,D.lo]),mult:B.mult}});A.sort((B,D)=>B.v[0]-D.v[0]||B.v[1]-D.v[1]);let L=A.length,j=A[L-1].v[0],z=new Float64Array(L),H=new Float64Array(L),O=new Float64Array(L),et=[0,0];A.forEach(({v:B,mult:D},nt)=>{z[nt]=B[0],H[nt]=B[1];let rt=Q(B,[j,0]);et=C(et,[D*Math.exp(rt[0])*(1+rt[1]),0]),O[nt]=et[0]}),R.dist={hi:z,lo:H,cum:O,vmax:j,kv:C(i,[j,0])}}return R},d=M(0,[...r].sort((g,N)=>g-N)),b=new Xt,w=(g,N,y,x)=>{if(N+g.max<=u-m){let[I,A]=St(g.KZ[0],g.KZ[1],N,y);return b.add(I,A,x),!0}if(N+g.min>u+m)return!0;let E=g.dist;if(E===void 0)return!1;let[R,k]=St(u,h,-N,-y),v=0,q=E.hi.length;for(;v<q;){let I=v+q>>>1,A=E.hi[I];A<R||A===R&&E.lo[I]<=k?v=I+1:q=I}if(v>0){let[I,A]=St(E.kv[0],E.kv[1],N,y);b.add(I,A,x*E.cum[v-1])}return!0};w(d,0,0,1)||(d.pastHi.push(0),d.pastLo.push(0),d.count.push(1));for(let g=0;g<a-2;g++)for(let N of p[g].values()){let{pastHi:y,pastLo:x,count:E}=N;if(E.length!==0){for(let{to:R,hi:k,lo:v,mult:q}of N.edges)for(let I=0;I<E.length;I++){let[A,L]=St(y[I],x[I],k,v),j=E[I]*q;if(w(R,A,L,j))continue;let z=R.index.get(A);z===void 0?(R.index.set(A,R.count.length),R.pastHi.push(A),R.pastLo.push(L),R.count.push(j)):R.count[z]+=j}N.pastHi=N.pastLo=N.count=[],N.index.clear()}}return Math.min(1,b.value())}function Yt(e,t,o={}){let n;t!==void 0&&!ArrayBuffer.isView(t)&&!Array.isArray(t)?o=t:n=t;let r=V(o.alternative),s=o.mu??0,a=o.paired??!1,l=o.exact??null,i=o.correct??!0,f=o.confInt??!1,c=o.confLevel??.95,m=o.tolRoot??1e-4;if(!Number.isFinite(s))throw new RangeError("mu must be a single finite number");if(f&&!(c>0&&c<1))throw new RangeError("confLevel must be a number strictly between 0 and 1");if(a&&n===void 0)throw new TypeError("y is required when paired is true");let u,h;if(a){let[M,d]=Tt(Array.from(e),Array.from(n));u=ct(M.map((b,w)=>b-d[w]))}else u=ct(Array.from(e)),n!==void 0&&(h=ct(Array.from(n)));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:r,mu:s,correct:i,confInt:f,confLevel:c,tolRoot:m};return h===void 0?En(u,l,a,p):qn(u,h,l,p)}function Lt(e,t,o){return o?t==="two.sided"?Math.sign(e)*.5:t==="greater"?.5:-.5:0}function $e(e,t){return t==="less"?P(e):t==="greater"?P(e,{lowerTail:!1}):2*Math.min(P(e),P(e,{lowerTail:!1}))}function xn(e){if(!(e>0)||!Number.isFinite(e))return e;let o=1-Math.floor(Math.log10(e)),n=o>=0?e*10**o:e/10**-o,r=Math.round(n);return r-n===.5&&r%2!==0&&(r-=1),o>=0?r/10**o:r*10**-o}function Qt(e){let t=e.length;if(t===0)return NaN;let o=Math.floor(t/2);return t%2===1?e[o]:Z([e[o-1],e[o]])}var Gt=(e,t)=>e-t;function Be(e){let t=e.length,o=$(e.map(Math.abs)),n=0;for(let a=0;a<t;a++)e[a]>0&&(n+=o[a]);let r=0;for(let a of lt(o))r+=a*a*a-a;let s=Math.sqrt(t*(t+1)*(2*t+1)/24-r/48);return[n,n-t*(t+1)/4,s]}function En(e,t,o,n){let{alternative:r,mu:s,correct:a}=n,l=e.map(x=>x-s),i=l.some(x=>x===0);i&&(l=l.filter(x=>x!==0));let f=l.length,c=t??f<50,m=$(l.map(Math.abs)),u=lt(m).length>0,[h,p,M]=Be(l),d,b,w=c&&!u&&!i;if(w){b="Wilcoxon signed rank exact test";let x=f*(f+1)/2;if(r==="less")d=ht(h,f,!0,!1);else if(r==="greater")d=ht(h-1,f,!1,!1);else{let E=h>x/2?ht(h-1,f,!1,!1):ht(h,f,!0,!1);d=Math.min(1,2*E)}}else b=`Wilcoxon signed rank test${a?" with continuity correction":""}`,d=$e((p-Lt(p,r,a))/M,r);let g={method:b,alternative:r,statistic:h,statisticName:"V",pValue:d,nullValue:s,nullValueName:o?"location shift":"location"};if(!n.confInt)return g;let N=l.map(x=>x+s),y=w?An(N,n):Rn(N,n);return{...g,...y}}function Vt(e,t,o){return e==="less"?[-1/0,o]:e==="greater"?[t,1/0]:[t,o]}function _e(e,t,o){let n={confInt:Vt(e,NaN,NaN),confLevel:0,estimate:t};return o!==void 0&&(n.estimateNames=o),n}function He(e,t,o){let n=e(t==="two.sided"?o/2:o);return n===0?1:n}function We(e){if(!(e>0))throw new RangeError("tolRoot must be positive")}function An(e,t){let{alternative:o,confLevel:n}=t,r=e.length,s=[];for(let m=0;m<r;m++)for(let u=m;u<r;u++)s.push((e[m]+e[u])/2);s.sort(Gt);let a=s.length,l=1-n,i=He(m=>Me(m,r,!0,!1),o,l),f=ht(i-1,r,!0,!1),c=o==="two.sided"?2*f:f;return{confInt:Vt(o,s[i-1],s[a-i]),confLevel:c-l>l/2?1-xn(c):n,estimate:Qt(s),estimateNames:"(pseudo)median"}}function Rn(e,t){let{alternative:o,correct:n,confLevel:r,tolRoot:s}=t,a=e.length;if(a===0)return _e(o,NaN);let l=Math.min(...e),i=Math.max(...e);if(!Number.isFinite(l)||!Number.isFinite(i)||a===1||l===i)return _e(o,(l+i)/2,"midrange");let f=(g,N)=>{let[,y,x]=Be(e.map(E=>E-g).filter(E=>E!==0));return(y-Lt(y,o,N))/x},c=f(l,n),m=f(i,n),u=g=>-F(g),h=g=>o==="greater"?!(c<u(g)):o==="less"?!(m>F(g/2)):!(c<u(g/2))&&!(m>F(g/2)),p=1-r,M=!1;for(;!h(p)&&p<1;)p*=2,M=!0;We(s);let d=g=>Y(N=>f(N,n)-g,l,i,{tol:s,fLower:c-g,fUpper:m-g}),b;if(p>=1){let g=Qt([...e].sort(Gt));b=Vt(o,g,g)}else o==="greater"?b=[d(u(p)),1/0]:o==="less"?b=[-1/0,d(F(p))]:b=[d(u(p/2)),d(F(p/2))];let w=Y(g=>f(g,!1),l,i,{tol:s,fLower:f(l,!1),fUpper:f(i,!1)});return{confInt:b,confLevel:M?1-Math.min(1,p):r,estimate:w,estimateNames:"(pseudo)median"}}function De(e,t){let o=e.length,n=t.length,r=$([...e,...t]),s=0;for(let c=0;c<o;c++)s+=r[c];let a=s-o*(o+1)/2,l=0;for(let c of lt(r))l+=c*c*c-c;let i=o+n,f=Math.sqrt(o*n/12*(i+1-l/(i*(i-1))));return[a,a-o*n/2,f]}function qn(e,t,o,n){let{alternative:r,mu:s,correct:a}=n,l=e.length,i=t.length,f=e.map(N=>N-s),c=o??(l<50&&i<50),m=lt([...f,...t]).length>0,[u,h,p]=De(f,t),M,d,b=c&&!m;if(b)if(d="Wilcoxon rank sum exact test",r==="less")M=pt(u,l,i,!0,!1);else if(r==="greater")M=pt(u-1,l,i,!1,!1);else{let N=u>l*i/2?pt(u-1,l,i,!1,!1):pt(u,l,i,!0,!1);M=Math.min(1,2*N)}else d=`Wilcoxon rank sum test${a?" with continuity correction":""}`,M=$e((h-Lt(h,r,a))/p,r);let w={method:d,alternative:r,statistic:u,statisticName:"W",pValue:M,nullValue:s,nullValueName:"location shift"};if(!n.confInt)return w;let g=b?Tn(e,t,n):kn(e,t,n);return{...w,...g}}function Tn(e,t,o){let{alternative:n,confLevel:r}=o,s=e.length,a=t.length,l=[];for(let h of e)for(let p of t)l.push(h-p);l.sort(Gt);let i=l.length,f=1-r,c=He(h=>ye(h,s,a,!0,!1),n,f),m=pt(c-1,s,a,!0,!1),u=n==="two.sided"?2*m:m;return{confInt:Vt(n,l[c-1],l[i-c]),confLevel:u-f>f/2?1-u:r,estimate:Qt(l),estimateNames:"difference in location"}}function kn(e,t,o){let{alternative:n,correct:r,confLevel:s,tolRoot:a}=o,l=Math.min(...e)-Math.max(...t),i=Math.max(...e)-Math.min(...t);if(!Number.isFinite(l)||!Number.isFinite(i))throw new Error("cannot compute the confidence interval because the differences are not finite");let f=(h,p)=>{let[,M,d]=De(e.map(b=>b-h),t);return(M-Lt(M,n,p))/d},c=(h,p)=>{let M=f(l,p)-h,d=f(i,p)-h;if(Number.isNaN(M)||Number.isNaN(d))throw new Error("cannot compute the confidence interval because all observations are tied");return M<=0?l:d>=0?i:(We(a),Y(b=>f(b,p)-h,l,i,{tol:a,fLower:M,fUpper:d}))},m=1-s,u;return n==="greater"?u=[c(-F(m),r),1/0]:n==="less"?u=[-1/0,c(F(m),r)]:u=[c(-F(m/2),r),c(F(m/2),r)],{confInt:u,confLevel:s,estimate:c(0,!1),estimateNames:"difference in location"}}var Ke=["holm","hochberg","hommel","bonferroni","BH","BY","fdr","none"];function In(e){if(Ke.includes(e))return e;let t=Ke.filter(o=>o.startsWith(e));if(e.length===0||t.length!==1)throw new RangeError(`invalid method "${e}"`);return t[0]}function Zt(e,t={}){let o=typeof t=="string"?{method:t}:t,n=In(o.method??"holm"),r=Array.from(e),s=[];for(let m=0;m<r.length;m++)Number.isNaN(r[m])||s.push(m);let a=s.length,l=o.n??a;if(!(l>=a))throw new RangeError("n must be at least the number of non-NaN p-values");if(l<=1)return r;let i=s.map(m=>r[m]),f=Sn(i,n,l),c=r.slice();return s.forEach((m,u)=>c[m]=f[u]),c}function Ft(e,t){let o=e.map((n,r)=>r);return o.sort((n,r)=>(t?e[r]-e[n]:e[n]-e[r])||n-r),o}function Sn(e,t,o){let n=e.length,r=new Array(n);switch(t){case"none":return e.slice();case"bonferroni":return e.map(s=>Math.min(1,o*s));case"holm":{let s=Ft(e,!1),a=-1/0;return s.forEach((l,i)=>{a=Math.max(a,(o-i)*e[l]),r[l]=Math.min(1,a)}),r}case"hochberg":{let s=Ft(e,!0),a=1/0;return s.forEach((l,i)=>{let f=n-i;a=Math.min(a,(o-f+1)*e[l]),r[l]=Math.min(1,a)}),r}case"BH":case"fdr":case"BY":{let s=t==="BY"?Ae(Array.from({length:Math.floor(o)},(i,f)=>1/(f+1))):1,a=Ft(e,!0),l=1/0;return a.forEach((i,f)=>{let c=n-f;l=Math.min(l,s*o/c*e[i]),r[i]=Math.min(1,l)}),r}case"hommel":return Ln(e,o)}}function Ln(e,t){if(!Number.isInteger(t))throw new RangeError("n must be an integer for the hommel method");let o=e.length,n=[...e,...new Array(t-o).fill(1)],r=Ft(n,!1),s=r.map(i=>n[i]),a=s.slice();for(let i=t;i>=2;i--){let f=1/0;for(let c=1;c<=i;c++)f=Math.min(f,i*s[t-i+c-1]/c);for(let c=t-i;c<t;c++)a[c]=Math.max(a[c],f);for(let c=0;c<t-i;c++)a[c]=Math.max(a[c],Math.min(i*s[c],f))}let l=new Array(o);return r.forEach((i,f)=>{i<o&&(l[i]=a[f])}),l}var Vn=1+1e-7;function Ct(e){return Number.isFinite(e)&&e>=0&&Math.abs(e-Math.round(e))<=1e-7*Math.max(1,Math.abs(e))}function Fn(e,t,o){if(o===0)return e===0?1:0;if(o===1)return e===t?1:0;let n=t*o;if(e===n)return 1;let r=At(e,t,o,!1)*Vn,s=0;if(e<n){for(let i=Math.ceil(n);i<=t;i++)At(i,t,o,!1)<=r&&s++;let l=s===0?0:it(t-s,t,o,!1,!1);return Math.min(1,it(e,t,o,!0,!1)+l)}for(let l=0;l<=Math.floor(n);l++)At(l,t,o,!1)<=r&&s++;let a=s===0?0:it(s-1,t,o,!0,!1);return Math.min(1,it(e-1,t,o,!1,!1)+a)}function Jt(e,t,o={}){let n,r;if(typeof e!="number"){if(e.length!==2)throw new RangeError("incorrect length of 'x'");if(t!==void 0&&typeof t!="number"&&(o=t),!Ct(e[0])||!Ct(e[1]))throw new RangeError("'x' must be nonnegative and integer");n=Math.round(e[0]),r=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(!Ct(e))throw new RangeError("'x' must be nonnegative and integer");if(n=Math.round(e),!(Ct(t)&&Math.round(t)>=n&&t>0))throw new RangeError("'n' must be a positive integer >= 'x'");r=Math.round(t)}let s=o.p??.5;if(!(s>=0&&s<=1))throw new RangeError("'p' must be a single number between 0 and 1");let a=V(o.alternative),l=o.confLevel??.95;X(l);let i;a==="less"?i=it(n,r,s,!0,!1):a==="greater"?i=it(n-1,r,s,!1,!1):i=Fn(n,r,s);let f=u=>n===0?0:$t(u,n,r-n+1,!0,!1),c=u=>n===r?1:$t(1-u,n+1,r-n,!0,!1),m;if(a==="less")m=[0,c(1-l)];else if(a==="greater")m=[f(1-l),1];else{let u=(1-l)/2;m=[f(u),c(u)]}return{method:"Exact binomial test",alternative:a,statistic:n,statisticName:"number of successes",parameter:r,parameterName:"number of trials",pValue:i,confInt:m,confLevel:l,estimate:n/r,estimateNames:"probability of success",nullValue:s,nullValueName:"probability of success"}}var Cn=1+1e-7;function Pn(e){return Number.isFinite(e)&&e>=0&&Math.abs(e-Math.round(e))<=1e-7*Math.max(1,Math.abs(e))}function jn(e,t){if(t===0)return e===0?1:0;if(e===t)return 1;let o=_t(e,t,!1)*Cn,n=l=>_t(l,t,!1)<=o,r=Math.floor(t);if(e<t){let l=Math.max(e+1,r),i=l;if(!n(l)){let f=l,c=1;for(i=l+1;!n(i);)f=i,c*=2,i=l+c;for(;i-f>1;){let m=f+Math.floor((i-f)/2);n(m)?i=m:f=m}}return Math.min(1,at(e,t,!0,!1)+at(i-1,t,!1,!1))}let s=Math.min(e-1,r),a;if(!n(0))a=-1;else if(n(s))a=s;else{a=0;let l=s;for(;l-a>1;){let i=a+Math.floor((l-a)/2);n(i)?a=i:l=i}}return Math.min(1,at(a,t,!0,!1)+at(e-1,t,!1,!1))}function zn(e,t={}){let o=typeof e=="number"?[e]:Array.from(e);if(o.length<1||o.length>2)throw new RangeError("incorrect length of 'x'");if(!o.every(Pn))throw new RangeError("'x' must be finite, nonnegative, and integer");let n=o.map(Math.round),r=n.length,s=t.T??1,a=typeof s=="number"?new Array(r).fill(s):Array.from(s);if(a.length!==r)throw new RangeError("'x' and 'T' have incompatible length");if(!a.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 i=V(t.alternative),f=t.confLevel??.95;if(X(f),r===2){let[b,w]=n,[g,N]=a,y=b+w,x=l*g/(l*g+N),E=Jt([b,w],{p:x,alternative:i,confLevel:f}),R=k=>k/(1-k)*(N/g);return{method:"Comparison of Poisson rates",alternative:i,statistic:b,statisticName:"count1",parameter:y*l*g/(g+l*N),parameterName:"expected count1",pValue:E.pValue,confInt:[R(E.confInt[0]),R(E.confInt[1])],confLevel:f,estimate:b/g/(w/N),estimateNames:"rate ratio",nullValue:l,nullValueName:"rate ratio"}}let c=n[0],m=a[0],u=l*m,h;i==="less"?h=at(c,u,!0,!1):i==="greater"?h=at(c-1,u,!1,!1):h=jn(c,u);let p=b=>c===0?0:Ot(b,c,1,!0,!1),M=b=>Ot(1-b,c+1,1,!0,!1),d;if(i==="less")d=[0,M(1-f)];else if(i==="greater")d=[p(1-f),1/0];else{let b=(1-f)/2;d=[p(b),M(b)]}return{method:"Exact Poisson test",alternative:i,statistic:c,statisticName:"number of events",parameter:m,parameterName:"time base",pValue:h,confInt:[d[0]/m,d[1]/m],confLevel:f,estimate:c/m,estimateNames:"event rate",nullValue:l,nullValueName:"event rate"}}function te(e,t,o={}){let n,r,s=o;if(typeof e!="number"&&e.length>0&&Array.isArray(e[0])){let v=e;if(v.some(q=>q.length!==2))throw new RangeError("'x' must have 2 columns");n=v.map(q=>q[0]),r=v.map(q=>q[0]+q[1]),t!==void 0&&typeof t=="object"&&!Array.isArray(t)&&(s=t)}else{if(t===void 0||typeof t=="object"&&!Array.isArray(t))throw new RangeError("'n' must be given");n=typeof e=="number"?[e]:Array.from(e),r=typeof t=="number"?[t]:Array.from(t)}if(n.length!==r.length)throw new RangeError("'x' and 'n' must have the same length");let a=n.length,l=[];for(let v=0;v<a;v++)!Number.isNaN(n[v])&&!Number.isNaN(r[v])&&l.push(v);let i=l.length;if(i<1)throw new RangeError("not enough data");if(n=l.map(v=>n[v]),r=l.map(v=>r[v]),r.some(v=>!(v>0)))throw new RangeError("elements of 'n' must be positive");if(n.some(v=>!(v>=0)))throw new RangeError("elements of 'x' must be nonnegative");if(n.some((v,q)=>v>r[q]))throw new RangeError("elements of 'x' must not be greater than those of 'n'");let f;if(s.p!==void 0){let v=typeof s.p=="number"?[s.p]:Array.from(s.p);if(v.length!==a)throw new RangeError("'p' must have the same length as 'x' and 'n'");if(f=l.map(q=>v[q]),f.some(q=>!(q>0&&q<1)))throw new RangeError("elements of 'p' must be in (0,1)")}let c=f!==void 0;i===1&&f===void 0&&(f=[.5]);let m=s.confLevel??.95;X(m);let u=V(s.alternative);(i>2||i===2&&c)&&(u="two.sided");let h=n.map((v,q)=>v/r[q]),p=i===2?h[0]-h[1]:0,M=i===2?1/r[0]+1/r[1]:0,d=(s.correct??!0)&&i<=2?.5:0,b=!1;if(d>0&&i===1){let v=Math.abs(n[0]-r[0]*f[0]);v<=d&&([d,b]=[v,!0])}else if(d>0&&i===2&&!c){let v=Math.abs(p)/M;v<=d&&([d,b]=[v,!0])}let w=n.reduce((v,q)=>v+q,0)/r.reduce((v,q)=>v+q,0),g=0;if(!b)for(let v=0;v<i;v++){let q=f?.[v]??w,I=r[v]*q,A=r[v]*(1-q);g+=(Math.abs(n[v]-I)-d)**2/I,g+=(Math.abs(r[v]-n[v]-A)-d)**2/A}let N=f!==void 0?i:i-1,y;if(u==="two.sided")y=K(g,N/2,2,!1,!1);else{let v=Math.sign(i===1?h[0]-f[0]:p);y=P(v*Math.sqrt(g),{lowerTail:u==="less"})}let x;if(i===1||i===2&&!c){let v=F(u==="two.sided"?(1+m)/2:m);if(i===1){let q=r[0],I=v*v,A=(O,et)=>(O+I/(2*q)+et*v*Math.sqrt(O*(1-O)/q+I/(4*q*q)))/(1+I/q),L=h[0]+d/q,j=h[0]-d/q,z=Math.min(1,Math.max(0,L>=1?1:A(L,1))),H=Math.min(1,Math.max(0,j<=0?0:A(j,-1)));x=u==="two.sided"?[H,z]:u==="greater"?[H,1]:[0,z]}else{let q=Math.sqrt(h.reduce((j,z,H)=>j+z*(1-z)/r[H],0)),I=v*q+d*M,A=Math.max(-1,p-I),L=Math.min(1,p+I);x=u==="two.sided"?[A,L]:u==="greater"?[A,1]:[-1,L]}}let E=i===1?"p":l.map(v=>`prop ${v+1}`),k={method:`${i===1?"1-sample proportions test":`${i}-sample test for ${c?"given proportions":"equality of proportions"}`} ${d!==0?"with":"without"} continuity correction`,alternative:u,statistic:g,statisticName:"X-squared",parameter:N,parameterName:"df",pValue:y,estimate:i===1?h[0]:h,estimateNames:E};return f!==void 0&&(k.nullValue=i===1?f[0]:f,k.nullValueName=E),x!==void 0&&(k.confInt=x,k.confLevel=m),k}function On(e,t,o={}){let n;if(e.length>0&&Array.isArray(e[0])){t!==void 0&&!Array.isArray(t)&&(o=t),n=e.map(c=>Array.from(c));let f=n.length;if(f<2||n.some(c=>c.length!==f))throw new RangeError("'x' must be square with at least two rows and columns");if(n.some(c=>c.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");n=_n(e,t)}let r=o.correct??!0,s=n.length,a,l=r&&s===2&&n[0][1]!==n[1][0];if(l){let f=n[0][1],c=n[1][0];a=(Math.abs(f-c)-1)**2/(f+c)}else{a=0;for(let f=0;f<s;f++)for(let c=f+1;c<s;c++){let m=n[f][c],u=n[c][f];a+=(m-u)**2/(m+u)}}let i=s*(s-1)/2;return{method:l?"McNemar's Chi-squared test with continuity correction":"McNemar's Chi-squared test",statistic:a,statisticName:"McNemar's chi-squared",parameter:i,parameterName:"df",pValue:K(a,i/2,2,!1,!1)}}function _n(e,t){if(e.length!==t.length)throw new RangeError("'x' and 'y' must have the same length");let o=[];for(let f=0;f<e.length;f++){let c=e[f],m=t[f];typeof c=="number"&&Number.isNaN(c)||typeof m=="number"&&Number.isNaN(m)||o.push([c,m])}let n=f=>Array.from(new Set(f)).sort((c,m)=>typeof c=="number"&&typeof m=="number"?c-m:String(c)<String(m)?-1:String(c)>String(m)?1:0),r=n(o.map(f=>f[0])),s=n(o.map(f=>f[1]));if(r.length<2||r.length!==s.length)throw new RangeError("'x' and 'y' must have the same number of levels (minimum 2)");let a=new Map(r.map((f,c)=>[f,c])),l=new Map(s.map((f,c)=>[f,c])),i=r.map(()=>new Array(r.length).fill(0));for(let[f,c]of o)i[a.get(f)][l.get(c)]++;return i}function $n(e,t,o={}){let n=Array.from(e).filter(Number.isFinite),r=Array.from(t).filter(Number.isFinite),s=o.ratio??1;if(!(Number.isFinite(s)&&s>0))throw new RangeError("'ratio' must be a single positive number");let a=V(o.alternative),l=o.confLevel??.95;X(l);let i=n.length-1,f=r.length-1;if(i<1)throw new Error("not enough 'x' observations");if(f<1)throw new Error("not enough 'y' observations");let c=U(n)/U(r),m=c/s,u=G(m,i,f,!0,!1),h=G(m,i,f,!1,!1),p,M;if(a==="less")p=u,M=[0,c/wt(1-l,i,f,!0,!1)];else if(a==="greater")p=h,M=[c/wt(l,i,f,!0,!1),1/0];else{p=Math.min(1,2*Math.min(u,h));let d=(1-l)/2;M=[c/wt(1-d,i,f,!0,!1),c/wt(d,i,f,!0,!1)]}return{method:"F test to compare two variances",alternative:a,statistic:m,statisticName:"F",parameter:[i,f],parameterName:["num df","denom df"],pValue:p,confInt:M,confLevel:l,estimate:c,estimateNames:"ratio of variances",nullValue:s,nullValueName:"ratio of variances"}}function Bn(e){let t=e.length;if(t<2)throw new RangeError("'x' must be a list with at least 2 elements");let o=[],n=[];for(let c of e){let m=Array.from(c).filter(Number.isFinite);if(m.length<2)throw new RangeError("there must be at least 2 observations in each group");o.push(m.length-1),n.push(U(m))}let r=0,s=0,a=0,l=0;for(let c=0;c<t;c++)r+=o[c],s+=o[c]*n[c],a+=o[c]*Math.log(n[c]),l+=1/o[c];s/=r;let i=(r*Math.log(s)-a)/(1+(l-1/r)/(3*(t-1))),f=t-1;return{method:"Bartlett test of homogeneity of variances",statistic:i,statisticName:"Bartlett's K-squared",parameter:f,parameterName:"df",pValue:K(i,f/2,2,!1,!1)}}function ee(e,t,o="drop"){if(t===void 0){let a=e;if(a.length>0&&typeof a[0]=="number")throw new TypeError("'g' is required when 'x' is a single sample");let l=a.map(c=>Array.from(c).filter(m=>!Number.isNaN(m)));if(o==="error"&&l.some(c=>c.length===0))throw new RangeError("all groups must contain data");let i=[],f=[];return l.forEach((c,m)=>{c.length!==0&&(i.push(String(m+1)),f.push(c))}),{labels:i,groups:f}}let n=Array.from(e);if(n.length!==t.length)throw new RangeError("'x' and 'g' must have the same length");let r=new Map;for(let a=0;a<n.length;a++){let l=n[a],i=t[a];if(Number.isNaN(l)||typeof i=="number"&&Number.isNaN(i))continue;let f=r.get(i);f===void 0&&r.set(i,f=[]),f.push(l)}let s=Array.from(r.keys()).sort((a,l)=>typeof a=="number"&&typeof l=="number"?a-l:String(a)<String(l)?-1:String(a)>String(l)?1:0);return{labels:s.map(String),groups:s.map(a=>r.get(a))}}function ft(e,t,o="drop"){return ee(e,t,o).groups}function Ue(e){let t=[...e].sort((n,r)=>n-r),o=t.length;return o%2===1?t[(o-1)/2]:(t[o/2-1]+t[o/2])/2}function ne(e){if(e.length===0)throw new RangeError("'y' must have at least one row");let t=e[0].length;if(e.some(o=>o.length!==t))throw new RangeError("all rows of 'y' must have the same length");return e.filter(o=>o.every(n=>!Number.isNaN(n))).map(o=>Array.from(o))}function re(e,t,o,n){return{method:e,statistic:o,statisticName:t,parameter:n,parameterName:"df",pValue:K(o,n/2,2,!1,!1)}}function Xe(e){let t=0;for(let o of lt(e))t+=o*o*o-o;return t}function Hn(e,t){let o=ft(e,t,"error"),n=o.length;if(n<2)throw new RangeError("all observations are in the same group");let r=o.flat(),s=r.length,a=$(r),l=0,i=0;for(let f of o){let c=0;for(let m=0;m<f.length;m++)c+=a[i+m];i+=f.length,l+=c*c/f.length}return l=(12*l/(s*(s+1))-3*(s+1))/(1-Xe(r)/(s*s*s-s)),re("Kruskal-Wallis rank sum test","Kruskal-Wallis chi-squared",l,n-1)}function Wn(e){let t=ne(e),o=t.length,n=e[0]?.length??0;if(o<2||n<2)throw new RangeError("'y' must have at least two complete rows and two columns");let r=new Array(n).fill(0),s=0;for(let i of t){let f=$(i);for(let c=0;c<n;c++)r[c]+=f[c];s+=Xe(i)}let a=0;for(let i of r)a+=(i-o*(n+1)/2)**2;let l=12*a/(o*n*(n+1)-s/(n-1));return re("Friedman rank sum test","Friedman chi-squared",l,n-1)}function Dn(e){let t=ne(e),o=e.length,n=e[0]?.length??0;if(t.length<2||n<2)throw new RangeError("'y' must have at least two complete rows and two columns");let r=$(t.map(h=>Math.max(...h)-Math.min(...h))),s=new Array(n).fill(0),a=0;t.forEach((h,p)=>{let M=$(h);for(let d=0;d<n;d++){let b=r[p]*(M[d]-(n+1)/2);a+=b*b,s[d]+=b}});let l=0;for(let h of s)l+=h*h;l/=o;let i=n-1,f=(o-1)*(n-1),c,m,u=[i,f];if(a===l){c=NaN,u=[NaN,NaN];let h=0;for(let p=2;p<=n;p++)h+=Math.log(p);m=Math.exp((1-o)*h)}else c=(o-1)*l/(a-l),m=G(c,i,f,!1,!1);return{method:"Quade test",statistic:c,statisticName:"Quade F",parameter:u,parameterName:["num df","denom df"],pValue:m}}function Kn(e,t){let o=ft(e,t,"error"),n=o.length;if(n<2)throw new RangeError("all observations are in the same group");let r=o.flatMap(u=>{let h=Ue(u);return u.map(p=>Math.abs(p-h))}),s=r.length,l=$(r).map(u=>F((1+u/(s+1))/2)),i=0;for(let u of l)i+=u;let f=i/s,c=0,m=0;for(let u of o){let h=0;for(let p=0;p<u.length;p++)h+=l[m+p];m+=u.length,c+=u.length*(h/u.length-f)**2}return c/=U(l),re("Fligner-Killeen test of homogeneity of variances","Fligner-Killeen:med chi-squared",c,n-1)}function Un(e,t,o={}){let n=Array.from(e).filter(Number.isFinite),r=Array.from(t).filter(Number.isFinite),s=V(o.alternative),a=n.length,l=r.length,i=a+l;if(i<3)throw new RangeError("not enough observations");let f=n.concat(r),c=Array.from({length:i},(N,y)=>y).sort((N,y)=>f[N]-f[y]),m=new Array(i),u=0;for(;u<i;){let N=u;for(;N+1<i&&f[c[N+1]]===f[c[u]];)N++;let y=0;for(let x=u;x<=N;x++)y+=(x+1-(i+1)/2)**2;y/=N-u+1;for(let x=u;x<=N;x++)m[c[x]]=y;u=N+1}let h=0;for(let N=0;N<a;N++)h+=m[N];let p=0;for(let N of m)p+=N;p/=i;let M=0;for(let N of m)M+=(N-p)**2;let d=a*p,b=a*l/(i*(i-1))*M,w=(h-d)/Math.sqrt(b),g=s==="less"?P(w):s==="greater"?P(w,{lowerTail:!1}):2*P(-Math.abs(w));return{method:"Mood two-sample test of scale",alternative:s,statistic:w,statisticName:"Z",pValue:g}}function mt(e,t){let o=0;for(let n=e.length-1;n>=0;n--)o=o*t+e[n];return o}var Xn=[0,.221157,-.147981,-2.07119,4.434685,-2.706056],Yn=[0,.042981,-.293762,-1.752461,5.682633,-3.582633],Qn=[.544,-.39978,.025054,-6714e-7],Gn=[1.3822,-.77857,.062767,-.0020322],Zn=[-1.5861,-.31082,-.083751,.0038915],Jn=[-.4803,-.082676,.0030302],tr=[-2.273,.459];function er(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 o=Math.floor(e/2),n=new Float64Array(o),r=0;for(let m=0;m<o;m++)n[m]=-F((m+1-.375)/(e+.25)),r+=n[m]*n[m];r*=2;let s=Math.sqrt(r),a=1/Math.sqrt(e),l=n[0]/s+mt(Xn,a),i=new Float64Array(o);i[0]=l;let f,c;if(e>5){let m=n[1]/s+mt(Yn,a);i[1]=m,f=2,c=Math.sqrt((r-2*n[0]*n[0]-2*n[1]*n[1])/(1-2*l*l-2*m*m))}else f=1,c=Math.sqrt((r-2*n[0]*n[0])/(1-2*l*l));for(let m=f;m<o;m++)i[m]=n[m]/c;for(let m=0;m<o;m++)t[e-1-m]=i[m],t[m]=-i[m];return t}function Ye(e){let t=e.length,o=er(t),n=e[t-1]-e[0],r=0;for(let f of e)r+=f/n;r/=t;let s=0,a=0,l=0;for(let f=0;f<t;f++){let c=e[f]/n-r;s+=o[f]*o[f],a+=c*c,l+=o[f]*c}let i=Math.sqrt(s*a);return(i-l)*(i+l)/(s*a)}var nr=1.90985931710274,rr=1.0471975511966;function Qe(e,t){if(Number.isNaN(e)||!(t>=3))return NaN;if(t===3){let a=nr*(Math.asin(Math.sqrt(1-e))-rr);return Math.max(a,0)}let o=Math.log(e);if(t<=11){let a=mt(tr,t);if(o>=a)return 1e-99;let l=mt(Qn,t),i=Math.exp(mt(Gn,t));return P(-Math.log(a-o),{mean:l,sd:i,lowerTail:!1})}let n=Math.log(t),r=mt(Zn,n),s=Math.exp(mt(Jn,n));return P(o,{mean:r,sd:s,lowerTail:!1})}function or(e,t,o={}){let n;t!==void 0&&!Array.isArray(t)?o=t:n=t;let r=ft(e,n),s=o.varEqual??!1,a=r.length;if(a<2)throw new RangeError("not enough groups");let l=r.map(h=>h.length);if(l.some(h=>h<2))throw new RangeError("not enough observations");let i=r.map(Z),f=r.map(U),c=l.reduce((h,p)=>h+p,0),m,u;if(s){let h=Z(r.flat()),p=0,M=0;for(let d=0;d<a;d++)p+=l[d]*(i[d]-h)**2,M+=(l[d]-1)*f[d];u=c-a,m=p/(a-1)/(M/u)}else{let h=l.map((w,g)=>w/f[g]),p=h.reduce((w,g)=>w+g,0),M=0,d=0;for(let w=0;w<a;w++)M+=(1-h[w]/p)**2/(l[w]-1),d+=h[w]*i[w];M/=a*a-1,d/=p;let b=0;for(let w=0;w<a;w++)b+=h[w]*(i[w]-d)**2;m=b/((a-1)*(1+2*(a-2)*M)),u=1/(3*M)}return{method:s?"One-way analysis of means":"One-way analysis of means (not assuming equal variances)",statistic:m,statisticName:"F",parameter:[a-1,u],parameterName:["num df","denom df"],pValue:G(m,a-1,u,!1,!1)}}function sr(e){let t=Array.from(e).filter(r=>!Number.isNaN(r)).sort((r,s)=>r-s),o=t.length;if(o<3||o>5e3)throw new RangeError("sample size must be between 3 and 5000");if(!Number.isFinite(t[0])||!Number.isFinite(t[o-1]))throw new RangeError("'x' must be finite");if(t[o-1]===t[0])throw new RangeError("all 'x' values are identical");let n=Ye(t);return{method:"Shapiro-Wilk normality test",statistic:1-n,statisticName:"W",pValue:Qe(n,o)}}function oe(e,t,o){let n=[0,0];for(let r=Math.max(0,t);r<Math.min(o,e.length);r++)n=C(n,[e[r],0]);return n[0]}function ar([[e,t],[o,n]]){let r=e+o,s=t+n,a=e+t,l=Math.max(0,a-s),i=Math.min(a,r),f=new Float64Array(i-l+1);for(let c=l;c<=i;c++)f[c-l]=ut(c,r,s,a,!1);return{lo:l,d:f}}function ir(e,t){let o=new Float64Array(e.length+t.length-1);for(let n=0;n<e.length;n++){let r=e[n];if(r!==0)for(let s=0;s<t.length;s++)o[n+s]+=r*t[s]}return o}var Pt=class{lo;hi;logNull;constructor(t){let o=0,n=new Float64Array([1]);for(let r of t){let s=ar(r);o+=s.lo,n=ir(n,s.d)}this.lo=o,this.hi=o+n.length-1,this.logNull=n.map(Math.log)}density(t){let o=this.logNull.length,n=new Float64Array(o);if(t===0||t===1/0)return n[t===0?0:o-1]=1,n;let r=Math.log(t),s=-1/0;for(let l=0;l<o;l++){let i=this.logNull[l]+(this.lo+l)*r;n[l]=i,i>s&&(s=i)}for(let l=0;l<o;l++)n[l]=Math.exp(n[l]-s);let a=oe(n,0,o);for(let l=0;l<o;l++)n[l]=n[l]/a;return n}mean(t){if(t===0)return this.lo;if(t===1/0)return this.hi;let o=this.density(t),n=[0,0];for(let r=0;r<o.length;r++)n=C(n,[(this.lo+r)*o[r],0]);return n[0]}cdf(t,o,n){let r=this.density(o),s=t-this.lo;return n?oe(r,s,r.length):oe(r,0,s+1)}};function se(e,t){return t?Y(e,0,1):1/Y(o=>e(1/o),Number.EPSILON,1)}function Ge(e,t){if(t===e.lo)return 0;if(t===e.hi)return 1/0;let o=e.mean(1);return o===t?1:se(n=>e.mean(n)-t,o>t)}function ae(e,t,o){if(t===e.hi)return 1/0;let n=e.cdf(t,1,!1);return n===o?1:se(r=>o-e.cdf(t,r,!1),n<o)}function ie(e,t,o){if(t===e.lo)return 0;let n=e.cdf(t,1,!0);return n===o?1:se(r=>e.cdf(t,r,!0)-o,n>o)}function lr(e,t={}){let o=e.length;if(o<1)throw new RangeError("'x' must have at least one stratum");let n=e[0].length,r=e[0][0]?.length??0;if(n<2||r<2||o<2)throw new RangeError("each dimension in table must be >= 2");for(let c of e){if(c.length!==n||c.some(m=>m.length!==r))throw new RangeError("all strata must have the same dimensions");if(c.some(m=>m.some(u=>!Number.isFinite(u)||u<0)))throw new RangeError("all entries of 'x' must be nonnegative and finite")}let s=e.map(c=>c.reduce((m,u)=>m+u.reduce((h,p)=>h+p,0),0));if(s.some(c=>c<2))throw new RangeError("sample size in each stratum must be > 1");let a=V(t.alternative),l=t.correct??!0,i=t.exact??!1,f=t.confLevel??.95;if(X(f),n===2&&r===2){let c=e.map(m=>[[m[0][0],m[0][1]],[m[1][0],m[1][1]]]);return i?fr(c,a,f):cr(c,s,a,l,f)}return mr(e,s,n,r)}function cr(e,t,o,n,r){let s=0,a=0,l=0,i=0,f=0,c=0,m=0,u=0;e.forEach(([[N,y],[x,E]],R)=>{let k=t[R],v=N+y,q=x+E,I=N+x,A=y+E;s+=N,a+=v*I/k,l+=v*q*I*A/(k*k*(k-1));let L=(N+E)/k,j=(y+x)/k,z=N*E/k,H=y*x/k;i+=z,f+=H,c+=L*z,m+=L*H+j*z,u+=j*H});let h=s-a,p=n&&Math.abs(h)>=.5?.5:0,M=(Math.abs(h)-p)**2/l,d=o==="two.sided"?K(M,.5,2,!1,!1):P(Math.sign(h)*Math.sqrt(M),{lowerTail:o==="less"}),b=i/f,w=Math.sqrt(c/(2*i*i)+m/(2*i*f)+u/(2*f*f)),g;if(o==="less")g=[0,b*Math.exp(F(r)*w)];else if(o==="greater")g=[b*Math.exp(F(r,{lowerTail:!1})*w),1/0];else{let N=F((1+r)/2);g=[b*Math.exp(-N*w),b*Math.exp(N*w)]}return{method:`Mantel-Haenszel chi-squared test ${p!==0?"with":"without"} continuity correction`,alternative:o,statistic:M,statisticName:"Mantel-Haenszel X-squared",parameter:1,parameterName:"df",pValue:d,confInt:g,confLevel:r,estimate:b,estimateNames:"common odds ratio",nullValue:1,nullValueName:"common odds ratio"}}function fr(e,t,o){let n=new Pt(e),r=e.reduce((l,i)=>l+i[0][0],0),s;if(t==="two.sided"){let l=n.density(1),i=l[r-n.lo]*(1+1e-7),f=[0,0];for(let c of l)c<=i&&(f=C(f,[c,0]));s=Math.min(1,f[0])}else s=n.cdf(r,1,t==="greater");let a;if(t==="less")a=[0,ae(n,r,1-o)];else if(t==="greater")a=[ie(n,r,1-o),1/0];else{let l=(1-o)/2;a=[ie(n,r,l),ae(n,r,l)]}return{method:"Exact conditional test of independence in 2 x 2 x k tables",alternative:t,statistic:r,statisticName:"S",pValue:s,confInt:a,confLevel:o,estimate:Ge(n,r),estimateNames:"common odds ratio",nullValue:1,nullValueName:"common odds ratio"}}function mr(e,t,o,n){let r=(o-1)*(n-1),s=new Array(r).fill(0),a=Array.from({length:r},()=>new Array(r).fill(0));e.forEach((f,c)=>{let m=t[c],u=f.map(M=>M.reduce((d,b)=>d+b,0)),h=Array.from({length:n},(M,d)=>f.reduce((b,w)=>b+w[d],0)),p=1/(m*m*(m-1));for(let M=0;M<n-1;M++)for(let d=0;d<o-1;d++){let b=d+(o-1)*M;s[b]+=f[d][M]-u[d]*h[M]/m;for(let w=0;w<n-1;w++)for(let g=0;g<o-1;g++){let N=(d===g?m*u[d]:0)-u[d]*u[g],y=(M===w?m*h[M]:0)-h[M]*h[w];a[b][g+(o-1)*w]+=p*N*y}}});let l=Le(a,s),i=0;for(let f=0;f<r;f++)i+=s[f]*l[f];return{method:"Cochran-Mantel-Haenszel test",statistic:i,statisticName:"Cochran-Mantel-Haenszel M^2",parameter:r,parameterName:"df",pValue:K(i,r/2,2,!1,!1)}}var Ze=new Map;function ur(e,t){let o=`${e},${t}`,n=Ze.get(o);if(n!==void 0)return n;let r=e+t,s=[];for(let u=1;u<=r;u++)s.push(Math.min(u,r+1-u));let a=s.reduce((u,h)=>u+h,0),l=Array.from({length:e+1},()=>new Float64Array(a+1));l[0][0]=1;for(let u=0;u<r;u++){let h=r-u,p=s[u],M=Array.from({length:e+1},()=>new Float64Array(a+1));for(let d=0;d<=e;d++){let b=l[d],w=e-d;if(w>h)continue;let g=w/h;for(let N=0;N<=a;N++){let y=b[N];y!==0&&(w>0&&(M[d+1][N+p]+=y*g),w<h&&(M[d][N]+=y*(1-g)))}}l=M}let i=l[e],f=0;for(;f<i.length&&i[f]===0;)f++;let c=i.length-1;for(;c>f&&i[c]===0;)c--;let m={min:f,probs:i.slice(f,c+1)};return Ze.set(o,m),m}function le(e,t,o,n=!0){if(Number.isNaN(e)||!(t>=1)||!(o>=1)||t!==Math.floor(t)||o!==Math.floor(o))return NaN;let{min:r,probs:s}=ur(t,o),a=Math.floor(e+1e-7)-r,l=0;if(n)for(let i=0;i<=Math.min(a,s.length-1);i++)l+=s[i];else for(let i=Math.max(a+1,0);i<s.length;i++)l+=s[i];return Math.min(l,1)}var Je=new Map;function hr(e,t){let o=`${e},${t}`,n=Je.get(o);if(n!==void 0)return n;let r=e+t,s=0;for(let m=1;m<=r;m++)s+=Math.min(m,r+1-m);let a=Array.from({length:e+1},()=>new Float64Array(s+1));a[0][0]=1;for(let m=0;m<r;m++){let u=Math.min(m+1,r-m),h=Array.from({length:e+1},()=>new Float64Array(s+1));for(let p=0;p<=e;p++){if(e-p>r-m)continue;let M=a[p];for(let d=0;d<=s;d++){let b=M[d];b!==0&&(p<e&&(h[p+1][d+u]+=b),e-p<r-m&&(h[p][d]+=b))}}a=h}let l=a[e],i=0;for(;l[i]===0;)i++;let f=l.length-1;for(;l[f]===0;)f--;let c={min:i,counts:l.slice(i,f+1)};return Je.set(o,c),c}function jt(e,t,o){let{min:n,counts:r}=hr(t,o);if(e<=0)return n;if(e>=1)return n+r.length-1;let s=Re(t+o,t),a=0;for(let l=0;l<r.length;l++)if(a+=r[l]/s,a>=e)return n+l;return n+r.length-1}function pr(e,t,o={}){let n=Array.from(e).filter(g=>!Number.isNaN(g)),r=Array.from(t).filter(g=>!Number.isNaN(g)),s=n.length,a=r.length;if(s<1)throw new RangeError("not enough 'x' observations");if(a<1)throw new RangeError("not enough 'y' observations");let l=V(o.alternative),i=o.confInt??!1,f=o.confLevel??.95;i&&X(f);let c=s+a,m=$(n.concat(r)),u=m.map(g=>Math.min(g,c-g+1)),h=0;for(let g=0;g<s;g++)h+=u[g];let p=new Set(m).size<c,M=o.exact??(s<50&&a<50&&!p),d,b;if(M&&!p){let g=y=>le(y-1,s,a,!1),N=y=>le(y,s,a,!0);if(l==="two.sided"){let y=Math.floor((s+1)**2/4)+Math.floor(s*a/2)/2;d=Math.min(1,2*(h>y?g(h):N(h)))}else l==="less"?d=g(h):d=N(h);i&&(b=gr(n,r,l,f))}else{let g=0;for(let R of u)g+=R*R;let N=c%2===0,y=N?s*(c+2)/4:s*(c+1)**2/(4*c),x=N?s*a*(16*g-c*(c+2)**2)/(16*c*(c-1)):s*a*(16*c*g-(c+1)**4)/(16*c*c*(c-1)),E=(h-y)/Math.sqrt(x);d=l==="two.sided"?2*Math.min(P(E),P(E,{lowerTail:!1})):l==="less"?P(E,{lowerTail:!1}):P(E),i&&(b=wr(n,r,l,f))}let w={method:"Ansari-Bradley test",alternative:l,statistic:h,statisticName:"AB",pValue:d};return b!==void 0&&(w.confInt=b.confInt,w.confLevel=f,w.estimate=b.estimate,w.estimateNames="ratio of scales"),w.nullValue=1,w.nullValueName="ratio of scales",w}function tn(e,t,o){let n=e.length,r=n+t.length,s=dr(e.map(i=>i/o).concat(t)),a=s.map(i=>Math.min(i,r-i+1)),l=0;for(let i=0;i<n;i++)l+=a[i];return{ab:l,a,ties:new Set(s).size<r}}function dr(e){let t=e.filter(s=>!Number.isNaN(s));if(t.length===e.length)return $(e);let o=$(t),n=0,r=t.length;return e.map(s=>Number.isNaN(s)?++r:o[n++])}function ce(e,t,o){return e==="two.sided"?o(t):e==="greater"?[o(2*t)[0],1/0]:[0,o(2*t)[1]]}function gr(e,t,o,n){let r=e.length,s=t.length,a=[];for(let d of e)for(let b of t)d/b>=0&&a.push(d/b);a.sort((d,b)=>d-b);let l=a.length;if(l===0)return{confInt:ce(o,1-n,()=>[NaN,NaN]),estimate:NaN};let i=a.map((d,b)=>tn(e,t,d+(b<l-1?(a[b+1]-d)/2:d*1.01)).ab),f=(d,b,w)=>{let g=1/0;for(let y of i)(b?y-d>0:y-d>=0)&&y-d<g&&(g=y-d);let N=w?-1/0:1/0;return i.forEach((y,x)=>{y-d===g&&(N=w?Math.max(N,a[x]):Math.min(N,a[x]))}),N},c=d=>{let b=jt(d/2,r,s),w=jt(1-d/2,r,s);if(!i.some(y=>y-b>=0)||!i.some(y=>y-w>0))return[NaN,NaN];let g=f(b,!1,!1),N=f(w,!0,!0);return g>N?[f(w,!1,!1),f(b,!0,!0)]:[g,N]},m=ce(o,1-n,c),u=jt(.5,r,s),h=-1/0,p=1/0;i.forEach((d,b)=>{d-u<=0?h=Math.max(h,a[b]):p=Math.min(p,a[b])});let M=h===-1/0||p===1/0?NaN:(p+h)/2;return{confInt:m,estimate:M}}function wr(e,t,o,n){let r=e.length,s=t.length,a=r+s,l=a%2===0,i=(b,w)=>{let{ab:g,a:N,ties:y}=tn(e,t,b),x=l?g-r*(a+2)/4:g-r*(a+1)**2/(4*a),E;if(!y)E=Math.sqrt(l?r*s*(a+2)*(a-2)/(48*(a-1)):r*s*(a+1)*(3+a*a)/(48*a*a));else{let R=0;for(let k of N)R+=k*k;E=Math.sqrt(l?r*s*(16*R-a*(a+2)**2)/(16*a*(a-1)):r*s*(16*a*R-(a+1)**4)/(16*a*a*(a-1)))}return x/E-w},f=[],c=(b,w)=>{b.length===0||w.length===0||f.push(Math.min(...b)/Math.max(...w),Math.max(...b)/Math.min(...w))};if(c(e.filter(b=>b>0),t.filter(b=>b>0)),c(e.filter(b=>b<=0),t.filter(b=>b<0)),f.some(b=>!Number.isFinite(b)))return;let m=Math.min(...f),u=Math.max(...f),h=b=>Y(w=>i(w,b),m,u,{tol:1e-4}),p=b=>{let w=F(b/2),g=F(b/2,{lowerTail:!1});if(i(m,w)>0||i(u,g)<0)return[NaN,NaN];let N=h(w),y=h(g);return N<=y?[N,y]:[y,N]},M=ce(o,1-n,p),d=i(m,0)>0||i(u,0)<0?NaN:h(0);return{confInt:M,estimate:d}}function Nr(e,t,o={}){let n=Array.from(e).filter(R=>!Number.isNaN(R)),r=Array.from(t).filter(R=>!Number.isNaN(R)),s=n.length,a=r.length;if(s<1)throw new RangeError("not enough 'x' observations");if(a<1)throw new RangeError("not enough 'y' observations");let l=V(o.alternative),i=o.confLevel??.95;kt(i);let f=$(n.concat(r)),c=f.slice(0,s),m=f.slice(s),u=R=>R.reduce((k,v)=>k+v,0)/R.length,h=u(c),p=u(m),M=(R,k,v)=>{let q=(R.length+1)/2,I=0;for(let A=0;A<R.length;A++)I+=(R[A]-k[A]-v+q)**2;return I/(R.length-1)},d=M(c,$(n),h),b=M(m,$(r),p),w=(p-(a+1)/2)/s,g=s*d+a*b,N=s*a*(p-h)/(s+a)/Math.sqrt(g),y=g**2/((s*d)**2/(s-1)+(a*b)**2/(a-1)),x;l==="greater"?x=W(N,y,!0,!1):l==="less"?x=W(N,y,!1,!1):x=2*W(-Math.abs(N),y,!0,!1);let E=ot((1-i)/2,y,!1,!1)*Math.sqrt(d/(s*a**2)+b/(a*s**2));return{method:"Brunner-Munzel Test",alternative:l,statistic:N,statisticName:"Brunner-Munzel Test Statistic",parameter:y,parameterName:"df",pValue:x,confInt:[w-E,w+E],confLevel:i,estimate:w,estimateNames:"P(X<Y)+.5*P(X=Y)"}}var br=[1.2337005501361697,7831619385924639e-32];function Mr(e,t,o){let n=(c,m)=>{let u=new Float64Array(t*t);for(let h=0;h<t;h++)for(let p=0;p<t;p++){let M=c[h*t+p];if(M!==0)for(let d=0;d<t;d++)u[h*t+d]+=M*m[p*t+d]}return u},r=c=>{let m=0;for(let p of c)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<c.length;p++)c[p]*=h;return u},s,a=0,l=e.slice(),i=0,f=o;for(;f>0;)f&1&&(s=s===void 0?l.slice():n(s,l),a+=i,a+=r(s)),f>>=1,f>0&&(l=n(l,l),i*=2,i+=r(l));return[s,a]}function en(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[c,m]=vt(e,t),u=2*c-1+2*m,h=1,p=0;for(let M=1;M<=e;M++)h*=M/e*u,h<2**-500&&(h*=2**500,p-=500);for(;p<-1e3;)h*=2**-1e3,p+=1e3;return h*2**p}let o=Math.floor(e*t)+1,n=2*o-1,r=o-e*t,s=new Float64Array(n*n);for(let c=0;c<n;c++)for(let m=0;m<n;m++)s[c*n+m]=c-m+1>=0?1:0;for(let c=0;c<n;c++)s[c*n]-=Math.pow(r,c+1),s[(n-1)*n+c]-=Math.pow(r,n-c);2*r-1>0&&(s[(n-1)*n]+=Math.pow(2*r-1,n));for(let c=0;c<n;c++)for(let m=0;m<n;m++){let u=c-m+1;if(u>0){let h=1;for(let p=2;p<=u;p++)h*=p;s[c*n+m]/=h}}let[a,l]=Mr(s,n,e),i=a[(o-1)*n+o-1],f=l;for(let c=1;c<=e;c++)i=i*c/e,Math.abs(i)<2**-500&&(i*=2**500,f-=500);for(;f>1e3;)i*=2**1e3,f-=1e3;for(;f<-1e3;)i*=2**-1e3,f+=1e3;return i*2**f}function fe(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 r=Et(br,vt(e,e)),s=r[0]+r[1],a=Math.exp(-r[0])*(1-r[1]),l=0;for(let f=2;f<100;f++){let c=Math.exp(-(2*f-1)*(2*f-1)*s);if(l+=c,c<=1e-17*(l+a))break}let i=Math.sqrt(2*Math.PI)/e*(a+l);return t?i:1-i}let o=0;for(let r=1;r<100;r++){let s=Math.exp(-2*r*r*e*e);if(o+=r%2===1?s:-s,s<=1e-17*Math.abs(o))break}let n=2*o;return t?1-n:n}function nn(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[o,n]=vt(e,t),r=Math.floor(e-o-n),s=0;for(let a=0;a<=r;a++){let l=C(zt(e-a,-o),[-n,0]);if(l[0]<=0)break;let i=C(zt(a,o),[n,0]),f=Et(i,[e,0]),c=Et(l,[e,0]);s+=Ne(a,e-a,f,c,!1)/(f[0]+f[1])}return Math.min(t*s,1)}function rn(e,t,o,n={}){let r=n.alternative??"two.sided";if(Number.isNaN(e)||!(t>=1)||!(o>=1)||t!==Math.floor(t)||o!==Math.floor(o))return NaN;if(e<=0)return 1;if(e>1)return 0;let s=t+o,a;if(n.z!==void 0){if(n.z.length!==s)throw new RangeError("z must have length m + n");let u=[...n.z].sort((h,p)=>h-p);a=new Array(s+1).fill(!1);for(let h=1;h<s;h++)a[h]=u[h]!==u[h-1]}let l=e*t*o*(1-1e-10),i=(u,h)=>{let p=u*o-h*t;return(r==="two.sided"?Math.abs(p):r==="greater"?p:-p)>=l},f=new Float64Array(o+1),c=new Float64Array(o+1),m=0;for(let u=0;u<=t;u++){for(let h=0;h<=o;h++){if(u===0&&h===0){c[0]=1;continue}let p=u+h,M=(u>0?u/p*f[h]:0)+(h>0?h/p*c[h-1]:0);(a===void 0?p<s:a[p])&&i(u,h)?(M>0&&(m+=M*ut(u,t,o,p,!1)),c[h]=0):c[h]=M}[f,c]=[c,f],c.fill(0)}return Math.min(m,1)}function on(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 yr(e,t,o={}){let n=Array.from(e).filter(w=>!Number.isNaN(w)).sort((w,g)=>w-g),r=n.length;if(r<1)throw new RangeError("not enough 'x' data");let s=V(o.alternative),a=s==="two.sided"?"D":s==="greater"?"D^+":"D^-";if(typeof t=="function"){let w=-1/0,g=-1/0;for(let R=0;R<r;R++){let k=t(n[R]);w=Math.max(w,(R+1)/r-k),g=Math.max(g,k-R/r)}let N=s==="two.sided"?Math.max(w,g):s==="greater"?w:g,y=new Set(n).size<r,x=o.exact??(r<100&&!y),E;return s==="two.sided"?E=x?1-en(r,N):fe(Math.sqrt(r)*N,!1):E=x?nn(r,N):Math.exp(-2*r*N*N),{method:`${x?"Exact":"Asymptotic"} one-sample Kolmogorov-Smirnov test`,alternative:on(s,!0),exact:x,statistic:N,statisticName:a,pValue:Math.min(1,Math.max(0,E))}}let l=Array.from(t).filter(w=>!Number.isNaN(w)),i=l.length;if(i<1)throw new RangeError("not enough 'y' data");let f=n.map(w=>[w,0]).concat(l.map(w=>[w,1]));f.sort((w,g)=>w[0]-g[0]);let c=0,m=0,u=0,h=0;for(let w=0;w<f.length;w++){if(f[w][1]===0?c++:m++,w+1<f.length&&f[w+1][0]===f[w][0])continue;let g=c/r-m/i;u=Math.max(u,g),h=Math.max(h,-g)}let p=s==="two.sided"?Math.max(u,h):s==="greater"?u:h,M=new Set(f.map(w=>w[0])).size<f.length,d=o.exact??r*i<1e4,b;if(d)b=rn(p,r,i,M?{alternative:s,z:f.map(w=>w[0])}:{alternative:s});else{let w=r*i/(r+i);b=s==="two.sided"?fe(Math.sqrt(w)*p,!1):Math.exp(-2*w*p*p)}return{method:`${d?"Exact":"Asymptotic"} two-sample Kolmogorov-Smirnov test`,alternative:on(s,!1),exact:d,statistic:p,statisticName:a,pValue:Math.min(1,Math.max(0,b))}}function sn(e,t,o){let n=Array.from(e);if(n.length!==t.length)throw new RangeError("'x' and 'g' must have the same length");let r=new Map;for(let a=0;a<n.length;a++){let l=t[a];if(typeof l=="number"&&Number.isNaN(l)||o&&Number.isNaN(n[a]))continue;let i=r.get(l);i===void 0&&r.set(l,i=[]),i.push(n[a])}let s=Array.from(r.keys()).sort((a,l)=>typeof a=="number"&&typeof l=="number"?a-l:String(a)<String(l)?-1:String(a)>String(l)?1:0);return{levels:s,groups:s.map(a=>r.get(a))}}function me(e,t,o){let n=[],r=[];for(let l=0;l<e-1;l++)for(let i=l+1;i<e;i++)n.push(t(i,l)),r.push([i-1,l]);let s=Zt(n,o),a=Array.from({length:e-1},()=>new Array(e-1).fill(NaN));return r.forEach(([l,i],f)=>a[l][i]=s[f]),a}function ue(e,t,o,n){return{method:e,pValue:o,rowNames:t.slice(1).map(String),colNames:t.slice(0,-1).map(String),pAdjustMethod:n}}function vr(e,t,o={}){let n=o.paired??!1,r=o.poolSd??!n;if(n&&r)throw new RangeError("pooling of SD is incompatible with paired tests");let s=V(o.alternative),a=o.pAdjustMethod??"holm",{levels:l,groups:i}=sn(e,t,!n),f=l.length;if(f<2)throw new RangeError("there must be at least two groups");let c,m;if(r){m="t tests with pooled SD";let u=i.map(b=>b.length),h=i.map(b=>b.length>0?Z(b):NaN),p=0,M=0;i.forEach(b=>{b.length>1&&(p+=U(b)*(b.length-1),M+=b.length-1)});let d=Math.sqrt(p/M);c=(b,w)=>{let g=(h[b]-h[w])/(d*Math.sqrt(1/u[b]+1/u[w]));return s==="less"?W(g,M,!0,!1):s==="greater"?W(g,M,!1,!1):2*W(-Math.abs(g),M,!0,!1)}}else m=n?"paired t tests":"t tests with non-pooled SD",c=(u,h)=>Ht(i[u],i[h],{paired:n,alternative:s}).pValue;return ue(m,l,me(f,c,a),a)}function xr(e,t,o={}){let{pAdjustMethod:n,alternative:r,...s}=o,a=n??"holm",{levels:l,groups:i}=sn(e,t,!(s.paired??!1)),f=l.length;if(f<2)throw new RangeError("there must be at least two groups");let c="",m=me(f,(u,h)=>{let p=Yt(i[u],i[h],{...s,...r===void 0?{}:{alternative:r}});return c=p.method,p.pValue},a);return ue(c,l,m,a)}function Er(e,t,o={}){if(e.length!==t.length)throw new RangeError("'x' and 'n' must have the same length");let n=[],r=[];e.forEach((c,m)=>{Number.isNaN(c)||Number.isNaN(t[m])||(n.push(c),r.push(t[m]))});let s=n.length;if(s<2)throw new RangeError("too few groups");let a=o.pAdjustMethod??"holm",l=o.correct??!0,i=me(s,(c,m)=>te([n[c],n[m]],[r[c],r[m]],{correct:l}).pValue,a),f=Array.from({length:s},(c,m)=>m+1);return ue("Pairwise comparison of proportions",f,i,a)}function an(e){let t=e.flat(),o=Nt(t),n=[0,0],r=[0,0],s=[];for(let a of e){let l=Nt(a);s.push(l[0]);let i=Q(l,o);n=C(n,xt(xt(i,i),[a.length,0]));for(let f of a){let c=Q([f,0],l);r=C(r,xt(c,c))}}return{ssb:n[0],ssw:r[0],means:s}}function Ar(e,t){let o=ft(e,t),n=o.length;if(n<2)throw new RangeError("contrasts can be applied only to factors with 2 or more levels");let r=o.reduce((u,h)=>u+h.length,0),{ssb:s,ssw:a}=an(o),l=n-1,i=r-n,f=s/l,c=a/i,m=f/c;return{df:[l,i],sumSq:[s,a],meanSq:[f,c],fValue:m,pValue:i>0?G(m,l,i,!1,!1):NaN}}function Rr(e,t,o={}){let n;t!==void 0&&!Array.isArray(t)?o=t:n=t;let{labels:r,groups:s}=ee(e,n),a=s.length;if(a<2)throw new RangeError("contrasts can be applied only to factors with 2 or more levels");let l=o.confLevel??.95,f=s.reduce((M,d)=>M+d.length,0)-a,{ssw:c,means:m}=an(s),u=c/f,h=xe(l,a,f),p={names:[],i:[],j:[],diff:[],lwr:[],upr:[],pAdj:[]};for(let M=0;M<a-1;M++)for(let d=M+1;d<a;d++){let b=m[d]-m[M],w=Math.sqrt(u/2*(1/s[d].length+1/s[M].length));p.names.push(`${r[d]}-${r[M]}`),p.i.push(d+1),p.j.push(M+1),p.diff.push(b),p.lwr.push(b-h*w),p.upr.push(b+h*w),p.pAdj.push(ve(Math.abs(b)/w,a,f,{lowerTail:!1}))}return p}function qr(e,t,o={}){let n;t!==void 0&&!ArrayBuffer.isView(t)&&!Array.isArray(t)?o=t:n=t;let{sigmaX:r,sigmaY:s}=o,a=o.mu??0;if(r===void 0&&n===void 0)throw new RangeError("sigmaX (the known standard deviation of x) is required");if(r===void 0||n!==void 0&&s===void 0)throw new RangeError("sigmaX and sigmaY are both required for two samples");if(Number.isNaN(a))throw new RangeError("mu must be a number");let l=V(o.alternative),i=o.confLevel??.95;if(!(i>=0&&i<=1))throw new RangeError("confLevel must be between 0 and 1");let f=Array.from(e).filter(g=>!Number.isNaN(g)),c=n===void 0?void 0:Array.from(n).filter(g=>!Number.isNaN(g));if(f.length<3)throw new RangeError("x needs at least 3 non-missing values");if(c!==void 0&&c.length<3)throw new RangeError("y needs at least 3 non-missing values");let m=Nt(f)[0],u=c===void 0?NaN:Nt(c)[0],h=c===void 0?r/Math.sqrt(f.length):Math.sqrt(r**2/f.length+s**2/c.length),p=c===void 0?m:m-u,M=(p-a)/h,d,b;if(l==="less")d=P(M),b=[-1/0,p+F(i)*h];else if(l==="greater")d=P(M,{lowerTail:!1}),b=[p-F(i)*h,1/0];else{d=2*P(-Math.abs(M));let g=F(1-(1-i)/2)*h;b=[p-g,p+g]}let w=c!==void 0;return{method:w?"Two-sample z-Test":"One-sample z-Test",alternative:l,statistic:M,statisticName:"z",pValue:d,confInt:b,confLevel:i,estimate:w?[m,u]:m,estimateNames:w?["mean of x","mean of y"]:"mean of x",nullValue:a,nullValueName:w?"difference in means":"mean"}}function Tr(e,t,o={}){let n=o.intercept??!0,r=e.length>0&&typeof e[0]!="number"?e.map(T=>Array.from(T)):Array.from(e,T=>[T]),s=Array.from(t);if(r.length!==s.length)throw new RangeError("variable lengths differ (found for 'x')");let a=o.weights===void 0?void 0:Array.from(o.weights);if(a!==void 0&&a.length!==s.length)throw new RangeError("variable lengths differ (found for '(weights)')");if(a?.some(T=>T<0))throw new RangeError("missing or negative weights not allowed");let l=s.map((T,S)=>!Number.isNaN(T)&&r[S].every(_=>!Number.isNaN(_))&&(a===void 0||!Number.isNaN(a[S]))),i=r.filter((T,S)=>l[S]).map(T=>n?[1,...T]:T),f=s.filter((T,S)=>l[S]),c=a?.filter((T,S)=>l[S]);if(f.length===0)throw new RangeError("0 (non-NA) cases");let m=f.map((T,S)=>c===void 0||c[S]!==0),u=i.filter((T,S)=>m[S]),h=f.filter((T,S)=>m[S]),p=c?.filter((T,S)=>m[S]),M=h.length;if(M===0)throw new RangeError("all weights are zero");let d=p?.map(Math.sqrt),b=d===void 0?u:u.map((T,S)=>T.map(_=>_*d[S])),w=d===void 0?h:h.map((T,S)=>T*d[S]),g=ke(b),N=g.rank,y=Ie(g,w),x=Se(g,w),E=d===void 0?x:x.map((T,S)=>T/d[S]),R=[],k=[],v=0;f.forEach((T,S)=>{if(m[S])R.push(E[v]),k.push(T-E[v]),v++;else{let _=i[S].reduce((yt,ln,de)=>Number.isNaN(y[de])?yt:yt+ln*y[de],0);k.push(_),R.push(T-_)}});let q=k.filter((T,S)=>m[S]),I=M-N,A=p??new Array(M).fill(1),L=0;for(let T=0;T<M;T++)L+=A[T]*E[T]**2;let j=L/I,z=Math.sqrt(j),H=g.qr.slice(0,N).map(T=>T.slice(0,N)),O=N>0?Ve(H):[],B=g.pivot.slice(0,N).map((T,S)=>[T,S]).sort((T,S)=>T[0]-S[0]),D=B.map(([T])=>y[T]),nt=B.map(([,T])=>Math.sqrt(O[T][T]*j)),rt=D.map((T,S)=>T/nt[S]),dt=rt.map(T=>2*W(-Math.abs(T),I,!0,!1)),st={coefficients:y,residuals:R,fittedValues:k,rank:N,dfResidual:I,coefTable:{estimate:D,stdError:nt,tValue:rt,pValue:dt},sigma:z,rSquared:0,adjRSquared:0,intercept:n,qr:g},he=n?1:0,Mt=N-he;if(Mt===0)return st;let gt=0;if(n){let T=A.reduce((_,yt)=>_+yt,0),S=0;for(let _=0;_<M;_++)S+=A[_]*q[_]/T;for(let _=0;_<M;_++)gt+=A[_]*(q[_]-S)**2}else for(let T=0;T<M;T++)gt+=A[T]*q[T]**2;st.rSquared=gt/(gt+L),st.adjRSquared=1-(1-st.rSquared)*((M-he)/I);let pe=gt/Mt/j;return st.fStatistic={value:pe,numdf:Mt,dendf:I},st.fPValue=G(pe,Mt,I,!1,!1),st}function kr(e,t={}){let o=t.level??.95,n=ot((1+o)/2,e.dfResidual,!0,!1),{estimate:r,stdError:s}=e.coefTable,a=e.coefficients.map(()=>NaN),l=e.coefficients.map(()=>NaN),i=0;return e.coefficients.forEach((f,c)=>{Number.isNaN(f)||(a[c]=r[i]-n*s[i],l[c]=r[i]+n*s[i],i++)}),{lower:a,upper:l}}function Ir(e,t,o={}){let r=(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),s=e.rank,a=e.qr.pivot.slice(0,s),l=e.qr.qr.slice(0,s).map(p=>p.slice(0,s)),i=e.sigma**2,f=r.map(p=>p.reduce((M,d,b)=>Number.isNaN(e.coefficients[b])?M:M+d*e.coefficients[b],0)),c=r.map(p=>{let M=new Array(s).fill(0);for(let d=0;d<s;d++){let b=p[a[d]];for(let w=0;w<d;w++)b-=l[w][d]*M[w];M[d]=b/l[d][d]}return Math.sqrt(M.reduce((d,b)=>d+b*b,0)*i)}),m=o.interval??"none";if(m==="none")return{fit:f,seFit:c};let u=ot((1+(o.level??.95))/2,e.dfResidual,!0,!1),h=c.map(p=>u*Math.sqrt(p*p+(m==="prediction"?i:0)));return{fit:f,seFit:c,lwr:f.map((p,M)=>p-h[M]),upr:f.map((p,M)=>p+h[M])}}export{Rr as TukeyHSD,pr as ansariTest,Ar as aov,Bn as bartlettTest,Jt as binomTest,Nr as brunnerMunzelTest,mn as chisqTest,kr as confint,cn as corTest,wn as fisherTest,Kn as flignerTest,Wn as friedmanTest,Hn as kruskalTest,yr as ksTest,Tr as lm,lr as mantelhaenTest,On as mcnemarTest,Un as moodTest,or as onewayTest,Zt as pAdjust,Er as pairwisePropTest,vr as pairwiseTTest,xr as pairwiseWilcoxTest,zn as poissonTest,Ir as predict,te as propTest,Dn as quadeTest,sr as shapiroTest,Ht as tTest,$n as varTest,Yt as wilcoxTest,qr as zTest};
1
+ /*! rStats.js 0.10.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 Et,D as lt,H as Fo,I as _o,J as lr,N as kt,O as cr,T as At,U as Co,Y as Vo,Z as qt,_ as $o,b as Lo,c as me,ca as Nn,d as ar,da as Do,ea as Zt,fa as Po,ha as yn,i as Pe,ia as Mn,j as Io,l as Oo,m as Kt,n as st,s as ae,t as at,v as it,w as Ke,y as ir}from"./chunks/chunk-O6PT7IVU.js";import{A as An,B as mt,h as he,j as Ho,k as Tt,l as En,r as kn,v as ve}from"./chunks/chunk-EWC6I75B.js";import{a as te,b as xn,c as Me,d as Uo,e as Sa,g as qn,h as Jt}from"./chunks/chunk-XWTZ4RTR.js";import{k as we,l as je,n as vn,o as ct}from"./chunks/chunk-ZRIM4R4A.js";import{b as Go,c as ft,e as Rn}from"./chunks/chunk-IT354SHV.js";import{b as fr}from"./chunks/chunk-QULPY6YM.js";import"./chunks/chunk-7HPVUVZB.js";import{a as Yt,b as se,c as re,d as jo,e as ee,f as fe,g as Oe,h as Ae,i as Rt}from"./chunks/chunk-LIMZKONM.js";import{a as zo,d as Wo}from"./chunks/chunk-QJJWGNJP.js";import{b as Qt}from"./chunks/chunk-ENL43FNO.js";import{f as ye,g as P,h as H,n as sr,p as To,s as So}from"./chunks/chunk-4NM37MGA.js";import{b as ot,c as xt,d as B,e as qo,f as le,g as oe,h as Te,i as ge,j as Xt,k as Ro}from"./chunks/chunk-2XKG3DX4.js";function mr(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(xn(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]=Rt(c,f);c=M.map((A,k)=>A-x[k]),f=void 0}else c=Ae(c),f!==void 0&&(f=Ae(f));let m,u,h,p,d,g,v,w=c.length;if(w<2)throw new Error("not enough 'x' observations");let b=se(c),N=re(c);if(f===void 0){if(u=w-1,h=Math.sqrt(N/w),h<10*Number.EPSILON*Math.abs(b))throw new Error("data are essentially constant");m=(b-a)/h,g=i?"Paired t-test":"One Sample t-test",p=b,d=i?"mean difference":"mean of x",v=i?"mean difference":"mean"}else{let M=f.length;if(M<1||!l&&M<2)throw new Error("not enough 'y' observations");if(l&&w+M<3)throw new Error("not enough observations");let x=se(f),A=re(f);if(g=`${l?"":"Welch"} Two Sample t-test`,p=[b,x],d=["mean of x","mean of y"],v="difference in means",l){u=w+M-2;let k=0;w>1&&(k+=(w-1)*N),M>1&&(k+=(M-1)*A),k/=u,h=Math.sqrt(k*(1/w+1/M))}else{let k=N/w,S=A/M;h=Math.sqrt(k+S),u=(h*h)**2/(k*k/(w-1)+S*S/(M-1))}if(h<10*Number.EPSILON*Math.max(Math.abs(b),Math.abs(x)))throw new Error("data are essentially constant");m=(b-x-a)/h}let y,E;if(o==="less")y=we(m,u,!0,!1),E=[-1/0,m+je(s,u,!0,!1)];else if(o==="greater")y=we(m,u,!1,!1),E=[m-je(s,u,!0,!1),1/0];else{y=2*we(-Math.abs(m),u,!0,!1);let M=1-s,x=je(1-M/2,u,!0,!1);E=[m-x,m+x]}return E=[a+E[0]*h,a+E[1]*h],{method:g,alternative:o,statistic:m,statisticName:"t",parameter:u,parameterName:"df",pValue:y,confInt:E,confLevel:s,estimate:p,estimateNames:d,nullValue:a,nullValueName:v,stdErr:h}}function en(e){return` with simulated p-value
3
+ (based on ${ut(e)} replicates)`}function ut(e){if(!Number.isFinite(e)||e===0)return String(e);let t=e.toExponential(14).replace(/\.?0+e/,"e"),[n,r]=t.split("e"),o=Number(r),a=`${n}e${o<0?"-":"+"}${String(Math.abs(o)).padStart(2,"0")}`,i=String(Number(e.toPrecision(15)));return i.length<=a.length?i:a}function Ce(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 La(e,t,n){return Array.isArray(t)?Ia(e,t,n??{}):Oa(e,t??{})}var Bo=(e,t)=>e<t?-1:e>t?1:0;function Ia(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(Bo),l=[...new Set(a)].sort(Bo);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])]++),{...Yo(f,n),rowLevels:i,colLevels:l}}function Oa(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?ur(r[0],t):o===1?ur(r.map(a=>a[0]),t):Yo(r,t)}return ur(Array.from(e),t)}function Xo(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 Qo(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 Ko(e,t){return Number.isNaN(e)?NaN:me(e,t/2,2,!1,!1)}function Zo(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 ur(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=Ce(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=Ce(o);o=o.map(u=>u/m)}else if(Math.abs(Ce(o)-1)>Math.sqrt(Number.EPSILON))throw new RangeError("probabilities must sum to 1");let a=o.map(m=>r*m);Zo(a);let i=o.map((m,u)=>a[u]*(1-m)),l=Ce(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=Xo(t);s+=en(m),c=NaN,f=Qo(Fa(r,o,a,m),l,m)}else f=Ko(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 Fa(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 _a(e,t,n,r){let o=e.length,a=t.length,i=e.reduce((f,m)=>f+m,0),l=yn(i),s=new Int32Array(o*a),c=new Float64Array(Math.trunc(r));for(let f=0;f<c.length;f++){Mn(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 Yo(e,t){let n=t.correct??!0,r=e.length,o=e[0].length,a=e.flat();if(a.some(w=>!(w>=0&&w<1/0)))throw new RangeError("all entries of 'x' must be non-negative and finite");let i=Ce(a);if(!(i>0))throw new RangeError("at least one entry of 'x' must be positive");let l=e.map(w=>Ce(w)),s=e[0].map((w,b)=>Ce(e.map(N=>N[b]))),c=l.map(w=>s.map(b=>w*b/i));Zo(c.flat());let f=e.map((w,b)=>w.map((N,y)=>N-c[b][y])),m="Pearson's Chi-squared test",u,h=(r-1)*(o-1),p,d=(t.simulatePValue??!1)&&l.every(w=>w>0)&&s.every(w=>w>0),g=!d&&n&&r===2&&o===2?Math.min(.5,...f.flat().map(Math.abs)):0;if(d){let w=Xo(t);m+=en(w),u=Ce(f.flatMap((N,y)=>N.map((E,M)=>E*E/c[y][M]))),h=NaN;let b=_a(l.map(N=>Math.trunc(N)),s.map(N=>Math.trunc(N)),c,w);p=Qo(b,u,w)}else g>0?(m+=" with Yates' continuity correction",u=Ce(f.flatMap((w,b)=>w.map((N,y)=>(Math.abs(N)-g)**2/c[b][y])))):u=Ce(f.flatMap((w,b)=>w.map((N,y)=>N*N/c[b][y])));let v=c.map((w,b)=>w.map((N,y)=>N*(1-l[b]/i)*(1-s[y]/i)));return{method:m,statistic:u,statisticName:"X-squared",parameter:h,parameterName:"df",pValue:p??Ko(u,h),observed:e,expected:c,residuals:f.map((w,b)=>w.map((N,y)=>N/Math.sqrt(c[b][y]))),stdres:f.map((w,b)=>w.map((N,y)=>N/Math.sqrt(v[b][y])))}}var Ca=Math.pow(Number.EPSILON,.25);function xe(e,t,n,r={}){let o={tol:r.tol??Ca,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 Va(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 ja(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+en(s),alternative:l,pValue:$a(a,s)}}return{method:i,alternative:l,pValue:za(a)}}function $a(e,t){let n=e.filter(d=>d.some(g=>g>0)),r=e[0].map((d,g)=>g).filter(d=>e.some(g=>g[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(g=>d[g])),a=o.map(d=>d.reduce((g,v)=>g+v,0)),i=o[0].map((d,g)=>o.reduce((v,w)=>v+w[g],0)),l=a.length,s=i.length,c=[0,0];for(let d of o.flat())c=B(c,[-Wo(d),0]);let f=c[0]/(1+64*Number.EPSILON),m=yn(a.reduce((d,g)=>d+g,0)),u=new Int32Array(l*s),h=0,p=Math.trunc(t);for(let d=0;d<p;d++){Mn(a,i,m,u);let g=0;for(let v=0;v<u.length;v++)g-=m[u[v]];g<=f&&h++}return Math.max(0,Math.min(1,(1+h)/(t+1)))}var Da=1+1e-7,Pa=16;function hr(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 dr=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%Pa===0||!(a>0&&a<1/0)?(o=kt(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=hr(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?cr(t-1,this.m,this.n,this.k,!1,!1):cr(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?hr(o,a,o.length-1):hr(o,0,a)}};function pr(e,t){return t?xe(e,0,1):1/xe(n=>e(1/n),Number.EPSILON,1)}function ja(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 dr(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]*Da,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:pr(y=>m.mean(y)-f,N>f)}let g={method:n,alternative:r,pValue:p,estimate:d,estimateNames:"odds ratio",nullValue:o,nullValueName:"odds ratio"};if(!(t.confInt??!0))return g;let v=N=>{if(f===h)return 1/0;let y=m.tail(f,1,!1);return y===N?1:pr(E=>m.tail(f,E,!1)-N,y<N)},w=N=>{if(f===u)return 0;let y=m.tail(f,1,!0);return y===N?1:pr(E=>m.tail(f,E,!0)-N,y>N)},b;if(r==="less")b=[0,v(1-a)];else if(r==="greater")b=[w(1-a),1/0];else{let N=(1-a)/2;b=[w(N),v(N)]}return{...g,confInt:b,confLevel:a}}var Tn=[[0,0]];function Ve(e){for(let t=Tn.length;t<=e;t++)Tn.push(B(Tn[t-1],Ro([t,0])));return Tn[e]}function Sn(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 gr=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 za(e){let t=e.filter(b=>b.some(N=>N>0));if(t.length===0)throw new RangeError("all entries of the table are zero");let n=t[0].map((b,N)=>t.some(y=>y[N]>0));if(t=t.map(b=>b.filter((N,y)=>n[y])),t.length<2||t[0].length<2)return 1;t.length>t[0].length&&(t=t[0].map((b,N)=>t.map(y=>y[N])));let r=t.length,o=t.map(b=>b.reduce((N,y)=>N+y,0)),a=t[0].map((b,N)=>t.reduce((y,E)=>y+E[N],0)).sort((b,N)=>N-b),i=a.length,l=o.reduce((b,N)=>b+N,0),s=qo(Ve(l));for(let b of o)s=B(s,Ve(b));for(let b of a)s=B(s,Ve(b));let c=[0,0];for(let b of t.flat())c=le(c,Ve(b));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=(b,N)=>{let y=N.join(","),E=p[b].get(y);if(E!==void 0)return E;let M=B(s,Ve(N.reduce((q,R)=>q+R,0)));for(let q of N)M=le(M,Ve(q));for(let q=b;q<i;q++)M=le(M,Ve(a[q]));let x={R:N,KZ:M,max:0,min:0,edges:[],pastHi:[],pastLo:[],count:[],index:new Map};if(p[b].set(y,x),b===i-1){let q=[0,0];for(let R of N)q=le(q,Ve(R));return x.max=x.min=q[0],x.edges.push({to:x,hi:q[0],lo:q[1],mult:1}),x}let A=new Map,k=new Array(r+1).fill(0);for(let q=r-1;q>=0;q--)k[q]=k[q+1]+N[q];let S=new Array(r).fill(0),L=(q,R,V)=>{if(q===r-1){S[q]=R;let C=le(V,Ve(R)),z=N.map((D,$)=>D-S[$]).sort((D,$)=>D-$),J=d(b+1,z),T=`${z.join(",")}|${C[0]}`,O=A.get(T);O===void 0?A.set(T,{to:J,hi:C[0],lo:C[1],mult:1}):O.mult++;return}let F=Math.max(0,R-k[q+1]),I=Math.min(N[q],R);for(let C=F;C<=I;C++)S[q]=C,L(q+1,R-C,le(V,Ve(C)))};if(L(0,a[b],[0,0]),x.edges=[...A.values()],x.max=Math.max(...x.edges.map(q=>q.hi+q.to.max)),x.min=Math.min(...x.edges.map(q=>q.hi+q.to.min)),b===i-2){let q=x.edges.map(J=>{let T=J.to.edges[0];return{v:B([J.hi,J.lo],[T.hi,T.lo]),mult:J.mult}});q.sort((J,T)=>J.v[0]-T.v[0]||J.v[1]-T.v[1]);let R=q.length,V=q[R-1].v[0],F=new Float64Array(R),I=new Float64Array(R),C=new Float64Array(R),z=[0,0];q.forEach(({v:J,mult:T},O)=>{F[O]=J[0],I[O]=J[1];let D=le(J,[V,0]);z=B(z,[T*Math.exp(D[0])*(1+D[1]),0]),C[O]=z[0]}),x.dist={hi:F,lo:I,cum:C,vmax:V,kv:B(s,[V,0])}}return x},g=d(0,[...o].sort((b,N)=>b-N)),v=new gr,w=(b,N,y,E)=>{if(N+b.max<=u-m){let[L,q]=Sn(b.KZ[0],b.KZ[1],N,y);return v.add(L,q,E),!0}if(N+b.min>u+m)return!0;let M=b.dist;if(M===void 0)return!1;let[x,A]=Sn(u,h,-N,-y),k=0,S=M.hi.length;for(;k<S;){let L=k+S>>>1,q=M.hi[L];q<x||q===x&&M.lo[L]<=A?k=L+1:S=L}if(k>0){let[L,q]=Sn(M.kv[0],M.kv[1],N,y);v.add(L,q,E*M.cum[k-1])}return!0};w(g,0,0,1)||(g.pastHi.push(0),g.pastLo.push(0),g.count.push(1));for(let b=0;b<i-2;b++)for(let N of p[b].values()){let{pastHi:y,pastLo:E,count:M}=N;if(M.length!==0){for(let{to:x,hi:A,lo:k,mult:S}of N.edges)for(let L=0;L<M.length;L++){let[q,R]=Sn(y[L],E[L],A,k),V=M[L]*S;if(w(x,q,R,V))continue;let F=x.index.get(q);F===void 0?(x.index.set(q,x.count.length),x.pastHi.push(q),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,v.value())}function wr(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.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,g]=Rt(Array.from(e),Array.from(r));u=Ae(d.map((v,w)=>v-g[w]))}else u=Ae(Array.from(e)),r!==void 0&&(h=Ae(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?Ga(u,l,i,p):Ba(u,h,l,p)}function Ln(e,t,n){return n?t==="two.sided"?Math.sign(e)*.5:t==="greater"?.5:-.5:0}function es(e,t){return t==="less"?P(e):t==="greater"?P(e,{lowerTail:!1}):2*Math.min(P(e),P(e,{lowerTail:!1}))}function Wa(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 vr=(e,t)=>e-t;function ts(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 Oe(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 Ga(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=Oe(m).length>0,[h,p,d]=ts(l),g,v,w=f&&!u&&!s;if(w){v="Wilcoxon signed rank exact test";let E=c*(c+1)/2;if(o==="less")g=At(h,c,!0,!1);else if(o==="greater")g=At(h-1,c,!1,!1);else{let M=h>E/2?At(h-1,c,!1,!1):At(h,c,!0,!1);g=Math.min(1,2*M)}}else v=`Wilcoxon signed rank test${i?" with continuity correction":""}`,g=es((p-Ln(p,o,i))/d,o);let b={method:v,alternative:o,statistic:h,statisticName:"V",pValue:g,nullValue:a,nullValueName:n?"location shift":"location"};if(!r.confInt)return b;let N=l.map(E=>E+a),y=w?Ua(N,r):Ha(N,r);return{...b,...y}}function In(e,t,n){return e==="less"?[-1/0,n]:e==="greater"?[t,1/0]:[t,n]}function Jo(e,t,n){let r={confInt:In(e,NaN,NaN),confLevel:0,estimate:t};return n!==void 0&&(r.estimateNames=n),r}function ns(e,t,n){let r=e(t==="two.sided"?n/2:n);return r===0?1:r}function rs(e){if(!(e>0))throw new RangeError("tolRoot must be positive")}function Ua(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(vr);let i=a.length,l=1-r,s=ns(m=>Co(m,o,!0,!1),n,l),c=At(s-1,o,!0,!1),f=n==="two.sided"?2*c:c;return{confInt:In(n,a[s-1],a[i-s]),confLevel:f-l>l/2?1-Wa(f):r,estimate:br(a),estimateNames:"(pseudo)median"}}function Ha(e,t){let{alternative:n,correct:r,confLevel:o,tolRoot:a}=t,i=e.length;if(i===0)return Jo(n,NaN);let l=Math.min(...e),s=Math.max(...e);if(!Number.isFinite(l)||!Number.isFinite(s)||i===1||l===s)return Jo(n,(l+s)/2,"midrange");let c=(b,N)=>{let[,y,E]=ts(e.map(M=>M-b).filter(M=>M!==0));return(y-Ln(y,n,N))/E},f=c(l,r),m=c(s,r),u=b=>-H(b),h=b=>n==="greater"?!(f<u(b)):n==="less"?!(m>H(b/2)):!(f<u(b/2))&&!(m>H(b/2)),p=1-o,d=!1;for(;!h(p)&&p<1;)p*=2,d=!0;rs(a);let g=b=>xe(N=>c(N,r)-b,l,s,{tol:a,fLower:f-b,fUpper:m-b}),v;if(p>=1){let b=br([...e].sort(vr));v=In(n,b,b)}else n==="greater"?v=[g(u(p)),1/0]:n==="less"?v=[-1/0,g(H(p))]:v=[g(u(p/2)),g(H(p/2))];let w=xe(b=>c(b,!1),l,s,{tol:a,fLower:c(l,!1),fUpper:c(s,!1)});return{confInt:v,confLevel:d?1-Math.min(1,p):o,estimate:w,estimateNames:"(pseudo)median"}}function os(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 Oe(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 Ba(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=Oe([...c,...t]).length>0,[u,h,p]=os(c,t),d,g,v=f&&!m;if(v)if(g="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 g=`Wilcoxon rank sum test${i?" with continuity correction":""}`,d=es((h-Ln(h,o,i))/p,o);let w={method:g,alternative:o,statistic:u,statisticName:"W",pValue:d,nullValue:a,nullValueName:"location shift"};if(!r.confInt)return w;let b=v?Xa(e,t,r):Qa(e,t,r);return{...w,...b}}function Xa(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(vr);let s=l.length,c=1-o,f=ns(h=>$o(h,a,i,!0,!1),r,c),m=qt(f-1,a,i,!0,!1),u=r==="two.sided"?2*m:m;return{confInt:In(r,l[f-1],l[s-f]),confLevel:u-c>c/2?1-u:o,estimate:br(l),estimateNames:"difference in location"}}function Qa(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,g]=os(e.map(v=>v-h),t);return(d-Ln(d,r,p))/g},f=(h,p)=>{let d=c(l,p)-h,g=c(s,p)-h;if(Number.isNaN(d)||Number.isNaN(g))throw new Error("cannot compute the confidence interval because all observations are tied");return d<=0?l:g>=0?s:(rs(i),xe(v=>c(v,p)-h,l,s,{tol:i,fLower:d,fUpper:g}))},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 ss=["holm","hochberg","hommel","bonferroni","BH","BY","fdr","none"];function Ka(e){if(ss.includes(e))return e;let t=ss.filter(n=>n.startsWith(e));if(e.length===0||t.length!==1)throw new RangeError(`invalid method "${e}"`);return t[0]}function St(e,t={}){let n=typeof t=="string"?{method:t}:t,r=Ka(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=Za(s,r,l),f=o.slice();return a.forEach((m,u)=>f[m]=c[u]),f}function On(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 Za(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=On(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=On(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=On(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 Ya(e,n)}}function Ya(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=On(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 Ja=1+1e-7;function Fn(e){return Number.isFinite(e)&&e>=0&&Math.abs(e-Math.round(e))<=1e-7*Math.max(1,Math.abs(e))}function ei(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=Et(e,t,n,!1)*Ja,a=0;if(e<r){for(let s=Math.ceil(r);s<=t;s++)Et(s,t,n,!1)<=o&&a++;let l=a===0?0:lt(t-a,t,n,!1,!1);return Math.min(1,lt(e,t,n,!0,!1)+l)}for(let l=0;l<=Math.floor(r);l++)Et(l,t,n,!1)<=o&&a++;let i=a===0?0:lt(a-1,t,n,!0,!1);return Math.min(1,lt(e-1,t,n,!1,!1)+i)}function Nr(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),!Fn(e[0])||!Fn(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(!Fn(e))throw new RangeError("'x' must be nonnegative and integer");if(r=Math.round(e),!(Fn(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=lt(r,o,a,!0,!1):i==="greater"?s=lt(r-1,o,a,!1,!1):s=ei(r,o,a);let c=u=>r===0?0:ir(u,r,o-r+1,!0,!1),f=u=>r===o?1:ir(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 ti=1+1e-7;function ni(e){return Number.isFinite(e)&&e>=0&&Math.abs(e-Math.round(e))<=1e-7*Math.max(1,Math.abs(e))}function ri(e,t){if(t===0)return e===0?1:0;if(e===t)return 1;let n=Kt(e,t,!1)*ti,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,st(e,t,!0,!1)+st(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,st(i,t,!0,!1)+st(e-1,t,!1,!1))}function oi(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(ni))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(v=>Number.isFinite(v)&&v>=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[v,w]=r,[b,N]=i,y=v+w,E=l*b/(l*b+N),M=Nr([v,w],{p:E,alternative:s,confLevel:c}),x=A=>A/(1-A)*(N/b);return{method:"Comparison of Poisson rates",alternative:s,statistic:v,statisticName:"count1",parameter:y*l*b/(b+l*N),parameterName:"expected count1",pValue:M.pValue,confInt:[x(M.confInt[0]),x(M.confInt[1])],confLevel:c,estimate:v/b/(w/N),estimateNames:"rate ratio",nullValue:l,nullValueName:"rate ratio"}}let f=r[0],m=i[0],u=l*m,h;s==="less"?h=st(f,u,!0,!1):s==="greater"?h=st(f-1,u,!1,!1):h=ri(f,u);let p=v=>f===0?0:ar(v,f,1,!0,!1),d=v=>ar(1-v,f+1,1,!0,!1),g;if(s==="less")g=[0,d(1-c)];else if(s==="greater")g=[p(1-c),1/0];else{let v=(1-c)/2;g=[p(v),d(v)]}return{method:"Exact Poisson test",alternative:s,statistic:f,statisticName:"number of events",parameter:m,parameterName:"time base",pValue:h,confInt:[g[0]/m,g[1]/m],confLevel:c,estimate:f/m,estimateNames:"event rate",nullValue:l,nullValueName:"event rate"}}function yr(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,g=(a.correct??!0)&&s<=2?.5:0,v=!1;if(g>0&&s===1){let k=Math.abs(r[0]-o[0]*c[0]);k<=g&&([g,v]=[k,!0])}else if(g>0&&s===2&&!f){let k=Math.abs(p)/d;k<=g&&([g,v]=[k,!0])}let w=r.reduce((k,S)=>k+S,0)/o.reduce((k,S)=>k+S,0),b=0;if(!v)for(let k=0;k<s;k++){let S=c?.[k]??w,L=o[k]*S,q=o[k]*(1-S);b+=(Math.abs(r[k]-L)-g)**2/L,b+=(Math.abs(o[k]-r[k]-q)-g)**2/q}let N=c!==void 0?s:s-1,y;if(u==="two.sided")y=me(b,N/2,2,!1,!1);else{let k=Math.sign(s===1?h[0]-c[0]:p);y=P(k*Math.sqrt(b),{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,q=(C,z)=>(C+L/(2*S)+z*k*Math.sqrt(C*(1-C)/S+L/(4*S*S)))/(1+L/S),R=h[0]+g/S,V=h[0]-g/S,F=Math.min(1,Math.max(0,R>=1?1:q(R,1))),I=Math.min(1,Math.max(0,V<=0?0:q(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+g*d,q=Math.max(-1,p-L),R=Math.min(1,p+L);E=u==="two.sided"?[q,R]:u==="greater"?[q,1]:[-1,R]}}let M=s===1?"p":l.map(k=>`prop ${k+1}`),A={method:`${s===1?"1-sample proportions test":`${s}-sample test for ${f?"given proportions":"equality of proportions"}`} ${g!==0?"with":"without"} continuity correction`,alternative:u,statistic:b,statisticName:"X-squared",parameter:N,parameterName:"df",pValue:y,estimate:s===1?h[0]:h,estimateNames:M};return c!==void 0&&(A.nullValue=s===1?c[0]:c,A.nullValueName=M),E!==void 0&&(A.confInt=E,A.confLevel=m),A}function si(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=ai(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 ai(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 ii(e,t,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/at(1-l,s,c,!0,!1)];else if(i==="greater")p=h,d=[f/at(l,s,c,!0,!1),1/0];else{p=Math.min(1,2*Math.min(u,h));let g=(1-l)/2;d=[f/at(1-g,s,c,!0,!1),f/at(g,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 li(e){let t=e.length;if(t<2)throw new RangeError("'x' must be a list with at least 2 elements");let n=[],r=[];for(let f of e){let m=Array.from(f).filter(Number.isFinite);if(m.length<2)throw new RangeError("there must be at least 2 observations in each group");n.push(m.length-1),r.push(re(m))}let o=0,a=0,i=0,l=0;for(let f=0;f<t;f++)o+=n[f],a+=n[f]*r[f],i+=n[f]*Math.log(r[f]),l+=1/n[f];a/=o;let s=(o*Math.log(a)-i)/(1+(l-1/o)/(3*(t-1))),c=t-1;return{method:"Bartlett test of homogeneity of variances",statistic:s,statisticName:"Bartlett's K-squared",parameter:c,parameterName:"df",pValue:me(s,c/2,2,!1,!1)}}function ht(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 pt(e,t,n="drop"){return ht(e,t,n).groups}function _n(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 Mr(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 xr(e,t,n,r){return{method:e,statistic:n,statisticName:t,parameter:r,parameterName:"df",pValue:me(n,r/2,2,!1,!1)}}function as(e){let t=0;for(let n of Oe(e))t+=n*n*n-n;return t}function Er(e,t){let n=pt(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-as(o)/(a*a*a-a)),xr("Kruskal-Wallis rank sum test","Kruskal-Wallis chi-squared",l,r-1)}function is(e){let t=Mr(e),n=t.length,r=e[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 t){let c=fe(s);for(let f=0;f<r;f++)o[f]+=c[f];a+=as(s)}let i=0;for(let s of o)i+=(s-n*(r+1)/2)**2;let l=12*i/(n*r*(r+1)-a/(r-1));return xr("Friedman rank sum test","Friedman chi-squared",l,r-1)}function ci(e){return{...is(e),method:"Cochran's Q test",statisticName:"Q"}}function fi(e){let t=Mr(e),n=e.length,r=e[0]?.length??0;if(t.length<2||r<2)throw new RangeError("'y' must have at least two complete rows and two columns");let o=fe(t.map(h=>Math.max(...h)-Math.min(...h))),a=new Array(r).fill(0),i=0;t.forEach((h,p)=>{let d=fe(h);for(let g=0;g<r;g++){let v=o[p]*(d[g]-(r+1)/2);i+=v*v,a[g]+=v}});let l=0;for(let h of a)l+=h*h;l/=n;let s=r-1,c=(n-1)*(r-1),f,m,u=[s,c];if(i===l){f=NaN,u=[NaN,NaN];let h=0;for(let p=2;p<=r;p++)h+=Math.log(p);m=Math.exp((1-n)*h)}else f=(n-1)*l/(i-l),m=ae(f,s,c,!1,!1);return{method:"Quade test",statistic:f,statisticName:"Quade F",parameter:u,parameterName:["num df","denom df"],pValue:m}}function mi(e,t){let n=pt(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=_n(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),xr("Fligner-Killeen test of homogeneity of variances","Fligner-Killeen:med chi-squared",f,r-1)}function ui(e,t,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 g=i*p,v=i*l/(s*(s-1))*d,w=(h-g)/Math.sqrt(v),b=a==="less"?P(w):a==="greater"?P(w,{lowerTail:!1}):2*P(-Math.abs(w));return{method:"Mood two-sample test of scale",alternative:a,statistic:w,statisticName:"Z",pValue:b}}function dt(e,t){let n=0;for(let r=e.length-1;r>=0;r--)n=n*t+e[r];return n}var hi=[0,.221157,-.147981,-2.07119,4.434685,-2.706056],pi=[0,.042981,-.293762,-1.752461,5.682633,-3.582633],di=[.544,-.39978,.025054,-6714e-7],gi=[1.3822,-.77857,.062767,-.0020322],wi=[-1.5861,-.31082,-.083751,.0038915],bi=[-.4803,-.082676,.0030302],vi=[-2.273,.459];function Ni(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+dt(hi,i),s=new Float64Array(n);s[0]=l;let c,f;if(e>5){let m=r[1]/a+dt(pi,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 ls(e){let t=e.length,n=Ni(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 yi=1.90985931710274,Mi=1.0471975511966;function cs(e,t){if(Number.isNaN(e)||!(t>=3))return NaN;if(t===3){let i=yi*(Math.asin(Math.sqrt(1-e))-Mi);return Math.max(i,0)}let n=Math.log(e);if(t<=11){let i=dt(vi,t);if(n>=i)return 1e-99;let l=dt(di,t),s=Math.exp(dt(gi,t));return P(-Math.log(i-n),{mean:l,sd:s,lowerTail:!1})}let r=Math.log(t),o=dt(wi,r),a=Math.exp(dt(bi,r));return P(n,{mean:o,sd:a,lowerTail:!1})}function xi(e,t,n={}){let r;t!==void 0&&!Array.isArray(t)?n=t:r=t;let o=pt(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 g=0;g<i;g++)p+=l[g]*(s[g]-h)**2,d+=(l[g]-1)*c[g];u=f-i,m=p/(i-1)/(d/u)}else{let h=l.map((w,b)=>w/c[b]),p=h.reduce((w,b)=>w+b,0),d=0,g=0;for(let w=0;w<i;w++)d+=(1-h[w]/p)**2/(l[w]-1),g+=h[w]*s[w];d/=i*i-1,g/=p;let v=0;for(let w=0;w<i;w++)v+=h[w]*(s[w]-g)**2;m=v/((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 Ei(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=ls(t);return{method:"Shapiro-Wilk normality test",statistic:1-r,statisticName:"W",pValue:cs(r,n)}}function kr(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 ki([[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]=kt(f,o,a,i,!1);return{lo:l,d:c}}function Ai(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 Cn=class{lo;hi;logNull;constructor(t){let n=0,r=new Float64Array([1]);for(let o of t){let a=ki(o);n+=a.lo,r=Ai(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=kr(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?kr(o,a,o.length):kr(o,0,a+1)}};function Ar(e,t){return t?xe(e,0,1):1/xe(n=>e(1/n),Number.EPSILON,1)}function fs(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 qr(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 Rr(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 qi(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?Ti(f,i,c):Ri(f,a,i,l,c)}return Si(e,a,r,o)}function Ri(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 A=t[x],k=N+y,S=E+M,L=N+E,q=y+M;a+=N,i+=k*L/A,l+=k*S*L*q/(A*A*(A-1));let R=(N+M)/A,V=(y+E)/A,F=N*M/A,I=y*E/A;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,g=n==="two.sided"?me(d,.5,2,!1,!1):P(Math.sign(h)*Math.sqrt(d),{lowerTail:n==="less"}),v=s/c,w=Math.sqrt(f/(2*s*s)+m/(2*s*c)+u/(2*c*c)),b;if(n==="less")b=[0,v*Math.exp(H(o)*w)];else if(n==="greater")b=[v*Math.exp(H(o,{lowerTail:!1})*w),1/0];else{let N=H((1+o)/2);b=[v*Math.exp(-N*w),v*Math.exp(N*w)]}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:g,confInt:b,confLevel:o,estimate:v,estimateNames:"common odds ratio",nullValue:1,nullValueName:"common odds ratio"}}function Ti(e,t,n){let r=new Cn(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,qr(r,o,1-n)];else if(t==="greater")i=[Rr(r,o,1-n),1/0];else{let l=(1-n)/2;i=[Rr(r,o,l),qr(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:fs(r,o),estimateNames:"common odds ratio",nullValue:1,nullValueName:"common odds ratio"}}function Si(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((g,v)=>g+v,0)),h=Array.from({length:r},(d,g)=>c.reduce((v,w)=>v+w[g],0)),p=1/(m*m*(m-1));for(let d=0;d<r-1;d++)for(let g=0;g<n-1;g++){let v=g+(n-1)*d;a[v]+=c[g][d]-u[g]*h[d]/m;for(let w=0;w<r-1;w++)for(let b=0;b<n-1;b++){let N=(g===b?m*u[g]:0)-u[g]*u[b],y=(d===w?m*h[d]:0)-h[d]*h[w];i[v][b+(n-1)*w]+=p*N*y}}});let l=ve(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 ms=new Map;function Li(e,t){let n=`${e},${t}`,r=ms.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 g=0;g<=e;g++){let v=l[g],w=e-g;if(w>h)continue;let b=w/h;for(let N=0;N<=i;N++){let y=v[N];y!==0&&(w>0&&(d[g+1][N+p]+=y*b),w<h&&(d[g][N]+=y*(1-b)))}}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 ms.set(n,m),m}function Tr(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}=Li(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 us=new Map;function Ii(e,t){let n=`${e},${t}`,r=us.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 g=0;g<=a;g++){let v=d[g];v!==0&&(p<e&&(h[p+1][g+u]+=v),e-p<o-m&&(h[p][g]+=v))}}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 us.set(n,f),f}function Vn(e,t,n){let{min:r,counts:o}=Ii(t,n);if(e<=0)return r;if(e>=1)return r+o.length-1;let a=zo(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 Oi(e,t,n={}){let r=Array.from(e).filter(b=>!Number.isNaN(b)),o=Array.from(t).filter(b=>!Number.isNaN(b)),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(b=>Math.min(b,f-b+1)),h=0;for(let b=0;b<a;b++)h+=u[b];let p=new Set(m).size<f,d=n.exact??(a<50&&i<50&&!p),g,v;if(d&&!p){let b=y=>Tr(y-1,a,i,!1),N=y=>Tr(y,a,i,!0);if(l==="two.sided"){let y=Math.floor((a+1)**2/4)+Math.floor(a*i/2)/2;g=Math.min(1,2*(h>y?b(h):N(h)))}else l==="less"?g=b(h):g=N(h);s&&(v=_i(r,o,l,c))}else{let b=0;for(let x of u)b+=x*x;let N=f%2===0,y=N?a*(f+2)/4:a*(f+1)**2/(4*f),E=N?a*i*(16*b-f*(f+2)**2)/(16*f*(f-1)):a*i*(16*f*b-(f+1)**4)/(16*f*f*(f-1)),M=(h-y)/Math.sqrt(E);g=l==="two.sided"?2*Math.min(P(M),P(M,{lowerTail:!1})):l==="less"?P(M,{lowerTail:!1}):P(M),s&&(v=Ci(r,o,l,c))}let w={method:"Ansari-Bradley test",alternative:l,statistic:h,statisticName:"AB",pValue:g};return v!==void 0&&(w.confInt=v.confInt,w.confLevel=c,w.estimate=v.estimate,w.estimateNames="ratio of scales"),w.nullValue=1,w.nullValueName="ratio of scales",w}function hs(e,t,n){let r=e.length,o=r+t.length,a=Fi(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 Fi(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 Sr(e,t,n){return e==="two.sided"?n(t):e==="greater"?[n(2*t)[0],1/0]:[0,n(2*t)[1]]}function _i(e,t,n,r){let o=e.length,a=t.length,i=[];for(let g of e)for(let v of t)g/v>=0&&i.push(g/v);i.sort((g,v)=>g-v);let l=i.length;if(l===0)return{confInt:Sr(n,1-r,()=>[NaN,NaN]),estimate:NaN};let s=i.map((g,v)=>hs(e,t,g+(v<l-1?(i[v+1]-g)/2:g*1.01)).ab),c=(g,v,w)=>{let b=1/0;for(let y of s)(v?y-g>0:y-g>=0)&&y-g<b&&(b=y-g);let N=w?-1/0:1/0;return s.forEach((y,E)=>{y-g===b&&(N=w?Math.max(N,i[E]):Math.min(N,i[E]))}),N},f=g=>{let v=Vn(g/2,o,a),w=Vn(1-g/2,o,a);if(!s.some(y=>y-v>=0)||!s.some(y=>y-w>0))return[NaN,NaN];let b=c(v,!1,!1),N=c(w,!0,!0);return b>N?[c(w,!1,!1),c(v,!0,!0)]:[b,N]},m=Sr(n,1-r,f),u=Vn(.5,o,a),h=-1/0,p=1/0;s.forEach((g,v)=>{g-u<=0?h=Math.max(h,i[v]):p=Math.min(p,i[v])});let d=h===-1/0||p===1/0?NaN:(p+h)/2;return{confInt:m,estimate:d}}function Ci(e,t,n,r){let o=e.length,a=t.length,i=o+a,l=i%2===0,s=(v,w)=>{let{ab:b,a:N,ties:y}=hs(e,t,v),E=l?b-o*(i+2)/4:b-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 A of N)x+=A*A;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-w},c=[],f=(v,w)=>{v.length===0||w.length===0||c.push(Math.min(...v)/Math.max(...w),Math.max(...v)/Math.min(...w))};if(f(e.filter(v=>v>0),t.filter(v=>v>0)),f(e.filter(v=>v<=0),t.filter(v=>v<0)),c.some(v=>!Number.isFinite(v)))return;let m=Math.min(...c),u=Math.max(...c),h=v=>xe(w=>s(w,v),m,u,{tol:1e-4}),p=v=>{let w=H(v/2),b=H(v/2,{lowerTail:!1});if(s(m,w)>0||s(u,b)<0)return[NaN,NaN];let N=h(w),y=h(b);return N<=y?[N,y]:[y,N]},d=Sr(n,1-r,p),g=s(m,0)>0||s(u,0)<0?NaN:h(0);return{confInt:d,estimate:g}}function Vi(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;xn(s);let c=fe(r.concat(o)),f=c.slice(0,a),m=c.slice(a),u=x=>x.reduce((A,k)=>A+k,0)/x.length,h=u(f),p=u(m),d=(x,A,k)=>{let S=(x.length+1)/2,L=0;for(let q=0;q<x.length;q++)L+=(x[q]-A[q]-k+S)**2;return L/(x.length-1)},g=d(f,fe(r),h),v=d(m,fe(o),p),w=(p-(i+1)/2)/a,b=a*g+i*v,N=a*i*(p-h)/(a+i)/Math.sqrt(b),y=b**2/((a*g)**2/(a-1)+(i*v)**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=je((1-s)/2,y,!1,!1)*Math.sqrt(g/(a*i**2)+v/(i*a**2));return{method:"Brunner-Munzel Test",alternative:l,statistic:N,statisticName:"Brunner-Munzel Test Statistic",parameter:y,parameterName:"df",pValue:E,confInt:[w-M,w+M],confLevel:s,estimate:w,estimateNames:"P(X<Y)+.5*P(X=Y)"}}var $i=[1.2337005501361697,7831619385924639e-32];function Di(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 g=0;g<t;g++)u[h*t+g]+=d*m[p*t+g]}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 ps(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]=xt(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]=Di(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 Lr(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($i,xt(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 ds(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]=xt(e,t),o=Math.floor(e-n-r),a=0;for(let i=0;i<=o;i++){let l=B(ot(e-i,-n),[-r,0]);if(l[0]<=0)break;let s=B(ot(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 gs(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*kt(u,t,n,p,!1)),f[h]=0):f[h]=d}[c,f]=[f,c],f.fill(0)}return Math.min(m,1)}function ws(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 Pi(e,t,n={}){let r=Array.from(e).filter(w=>!Number.isNaN(w)).sort((w,b)=>w-b),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 w=-1/0,b=-1/0;for(let x=0;x<o;x++){let A=t(r[x]);w=Math.max(w,(x+1)/o-A),b=Math.max(b,A-x/o)}let N=a==="two.sided"?Math.max(w,b):a==="greater"?w:b,y=new Set(r).size<o,E=n.exact??(o<100&&!y),M;return a==="two.sided"?M=E?1-ps(o,N):Lr(Math.sqrt(o)*N,!1):M=E?ds(o,N):Math.exp(-2*o*N*N),{method:`${E?"Exact":"Asymptotic"} one-sample Kolmogorov-Smirnov test`,alternative:ws(a,!0),exact:E,statistic:N,statisticName:i,pValue:Math.min(1,Math.max(0,M))}}let l=Array.from(t).filter(w=>!Number.isNaN(w)),s=l.length;if(s<1)throw new RangeError("not enough 'y' data");let c=r.map(w=>[w,0]).concat(l.map(w=>[w,1]));c.sort((w,b)=>w[0]-b[0]);let f=0,m=0,u=0,h=0;for(let w=0;w<c.length;w++){if(c[w][1]===0?f++:m++,w+1<c.length&&c[w+1][0]===c[w][0])continue;let b=f/o-m/s;u=Math.max(u,b),h=Math.max(h,-b)}let p=a==="two.sided"?Math.max(u,h):a==="greater"?u:h,d=new Set(c.map(w=>w[0])).size<c.length,g=n.exact??o*s<1e4,v;if(g)v=gs(p,o,s,d?{alternative:a,z:c.map(w=>w[0])}:{alternative:a});else{let w=o*s/(o+s);v=a==="two.sided"?Lr(Math.sqrt(w)*p,!1):Math.exp(-2*w*p*p)}return{method:`${g?"Exact":"Asymptotic"} two-sample Kolmogorov-Smirnov test`,alternative:ws(a,!1),exact:g,statistic:p,statisticName:i,pValue:Math.min(1,Math.max(0,v))}}function bs(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 Ir(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=St(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 Or(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 ji(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}=bs(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(v=>v.length),h=s.map(v=>v.length>0?se(v):NaN),p=0,d=0;s.forEach(v=>{v.length>1&&(p+=re(v)*(v.length-1),d+=v.length-1)});let g=Math.sqrt(p/d);f=(v,w)=>{let b=(h[v]-h[w])/(g*Math.sqrt(1/u[v]+1/u[w]));return a==="less"?we(b,d,!0,!1):a==="greater"?we(b,d,!1,!1):2*we(-Math.abs(b),d,!0,!1)}}else m=r?"paired t tests":"t tests with non-pooled SD",f=(u,h)=>mr(s[u],s[h],{paired:r,alternative:a}).pValue;return Or(m,l,Ir(c,f,i),i)}function zi(e,t,n={}){let{pAdjustMethod:r,alternative:o,...a}=n,i=r??"holm",{levels:l,groups:s}=bs(e,t,!(a.paired??!1)),c=l.length;if(c<2)throw new RangeError("there must be at least two groups");let f="",m=Ir(c,(u,h)=>{let p=wr(s[u],s[h],{...a,...o===void 0?{}:{alternative:o}});return f=p.method,p.pValue},i);return Or(f,l,m,i)}function Wi(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=Ir(a,(f,m)=>yr([r[f],r[m]],[o[f],o[m]],{correct:l}).pValue,i),c=Array.from({length:a},(f,m)=>m+1);return Or("Pairwise comparison of proportions",c,s,i)}var Se=e=>new SyntaxError(`invalid formula: ${e}`);function Gi(e){let t=[],n=0;for(;n<e.length;){let r=e[n];if(/\s/.test(r)){n++;continue}if(/[0-9]/.test(r)||r==="."&&/[0-9]/.test(e[n+1]??"")){let o=/^(?:[0-9]+\.?[0-9]*|\.[0-9]+)(?:[eE][+-]?[0-9]+)?/.exec(e.slice(n));t.push({kind:"num",text:o[0]}),n+=o[0].length;continue}if(/[A-Za-z.]/.test(r)){let o=/^[A-Za-z.][A-Za-z0-9._]*/.exec(e.slice(n));t.push({kind:"name",text:o[0]}),n+=o[0].length;continue}if(e.startsWith("%in%",n)){t.push({kind:"op",text:"%in%"}),n+=4;continue}if("~+-*/:^()".includes(r)){t.push({kind:"op",text:r}),n++;continue}throw Se(`unexpected '${r}'`)}return t.push({kind:"end"}),t}var Ui={"+":1,"-":1,"*":2,"/":2,"%in%":3,":":4,"^":6},Hi=5;function It(e,t=!1){if(typeof e!="string")throw new TypeError("the formula must be a string");let n=Gi(e),r=0,o=()=>n[r],a=()=>n[r++],i=(m,u)=>m.kind==="op"&&m.text===u;function l(){let m=a();if(m.kind==="name"){if(i(o(),"(")){if(m.text!=="Error"||!t)throw Se(`function calls are not supported (${m.text})`);a();let u=s(0);if(!i(a(),")"))throw Se("missing ')'");return{type:"Error",arg:u}}if(m.text===".")return{type:"dot"};if(/^\.[0-9]/.test(m.text))throw Se(`unexpected '${m.text}'`);return{type:"var",name:m.text}}if(m.kind==="num")return{type:"num",value:Number(m.text),text:m.text};if(m.kind==="op"&&m.text==="("){let u=s(0);if(!i(a(),")"))throw Se("missing ')'");return{type:"paren",arg:u}}if(m.kind==="op"&&(m.text==="-"||m.text==="+"))return{type:"unary",op:m.text,arg:s(Hi)};throw Se(m.kind==="end"?"unexpected end of input":`unexpected '${m.kind==="op"?m.text:""}'`)}function s(m){let u=l();for(;;){let h=o();if(h.kind!=="op")break;let p=Ui[h.text];if(p===void 0||p<=m)break;a();let d=s(h.text==="^"?p-1:p);u={type:"binary",op:h.text,left:u,right:d}}return u}let c;if(!i(o(),"~")){let m=a();if(m.kind!=="name"||m.text==="."||!i(o(),"~"))throw Se("the response must be a variable name followed by '~'");c=m.text}a();let f=s(0);if(o().kind!=="end")throw Se("unexpected input after the formula");return{response:c,rhs:f}}function vs(e){let t=[],n=a=>{if(a.type==="Error"){t.push(a.arg);return}if(a.type==="binary"&&a.op==="+"){let i=n(a.left),l=n(a.right);return i===void 0?l:l===void 0?i:{...a,left:i,right:l}}if(a.type==="binary"&&a.op==="-"){let i=n(a.left);return i===void 0?{type:"unary",op:"-",arg:a.right}:{...a,left:i}}return a},r=n(e),o=a=>{if(a.type==="Error")throw Se("Error() must be a term of the right-hand side");a.type==="binary"?(o(a.left),o(a.right)):(a.type==="unary"||a.type==="paren")&&o(a.arg)};return r!==void 0&&o(r),t.forEach(o),{treatment:r,errors:t}}function $n(e,t,n){if(e!==void 0)switch(e.type){case"var":n.includes(e.name)||n.push(e.name);return;case"dot":for(let r of t)n.includes(r)||n.push(r);return;case"num":return;case"binary":$n(e.left,t,n),e.op!=="^"&&$n(e.right,t,n);return;case"unary":case"paren":case"Error":$n(e.arg,t,n)}}var Fr=e=>e.join(",");function Lt(e){let t=new Set;return e.filter(n=>{let r=Fr(n);return t.has(r)?!1:(t.add(r),!0)})}var tn=(e,t)=>[...new Set([...e,...t])].sort((n,r)=>n-r);function Bi(e,t,n){let r=!0,o=(i,l)=>{switch(i.type){case"var":return[[t(i.name)]];case"dot":return n.map(s=>[t(s)]);case"num":if(i.value===1)r=!l;else if(i.value===0)r=l;else throw Se(`a number (${i.text}) is not a term`);return[];case"paren":return o(i.arg,l);case"Error":throw Se("Error() must be a term of the right-hand side");case"unary":return i.op==="+"?o(i.arg,l):(o(i.arg,!l),[]);case"binary":{if(i.op==="^"){let u=o(i.left,l),h=i.right;for(;h.type==="paren";)h=h.arg;let p=h.type==="num"?h.value:NaN;if(!Number.isInteger(p)||p<2)throw new Error("invalid power in formula");let d=u;for(let g=1;g<p;g++){let v=[];for(let w of u)for(let b of d)v.push(tn(w,b));d=Lt(v)}return d}if(i.op==="-"){let u=o(i.left,l),h=new Set(o(i.right,!l).map(Fr));return u.filter(p=>!h.has(Fr(p)))}let s=o(i.left,l),c=o(i.right,l),f=()=>{let u=[];for(let h of s)for(let p of c)u.push(tn(h,p));return u},m=u=>u.reduce((h,p)=>tn(h,p),[]);switch(i.op){case"+":return Lt([...s,...c]);case":":return Lt(f());case"*":return Lt([...s,...c,...f()]);case"/":{let u=m(s);return Lt([...s,...c.map(h=>tn(h,u))])}case"%in%":{let u=m(c);return Lt(s.map(h=>tn(h,u)))}}}}throw Se("unsupported expression")};return{terms:e===void 0?[]:o(e,!1),intercept:r}}function Ns(e,t,n=e.rhs){let{response:r}=e,o=t.filter(u=>u!==r),a=r===void 0?[]:[r];$n(e.rhs,o,a);let l=Bi(n,u=>a.indexOf(u),o),s=l.terms.map((u,h)=>[u,h]).sort((u,h)=>u[0].length-h[0].length||u[1]-h[1]).map(([u])=>u),c=[],f=r===void 0?-1:s.findIndex(u=>u.length===1&&u[0]===0);f>=0&&(c.push("the response appeared on the right-hand side and was dropped"),c.push(`problem with term ${f+1} in model.matrix: no columns are assigned`));let m=a.map(()=>s.map(()=>0));return s.forEach((u,h)=>{for(let p of u){let d=u.filter(v=>v!==p),g=d.length===0||s.slice(0,h).some(v=>d.every(w=>v.includes(w)));m[p][h]=g?1:2}}),{variables:a,response:r===void 0?-1:0,terms:s,termLabels:s.map(u=>u.map(h=>a[h]).join(":")),order:s.map(u=>u.length),intercept:l.intercept,factors:m,responseTerm:f,warnings:c}}var nn=[0,0];function ys(e){if(!(e[0]>0))return[Math.sqrt(e[0]),0];let t=Math.sqrt(e[0]),n=le(e,xt(t,t));return ot(t,n[0]/(2*t))}function Dn(e,t){return Array.from({length:t},(n,r)=>e===void 0?[1,0]:ys([e[r],0]))}function Ot(e,t){return ze(e,t,[]).fit}function ze(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=nn;for(let v=f;v<r;v++)u=B(u,oe(m[v],m[v]));if(u[0]===0){s.push(nn);continue}let h=ys(u),p=m[f][0]<0?[-h[0],-h[1]]:h;m[f]=B(m[f],p);let d=oe(p,m[f]),g=v=>{let w=nn;for(let N=f;N<r;N++)w=B(w,oe(m[N],v[N]));let b=ge(w,d);for(let N=f;N<r;N++)v[N]=le(v[N],oe(b,m[N]))};for(let v=f+1;v<e.length;v++)g(a[v]);g(i);for(let v of l)g(v);s.push([-p[0],-p[1]])}let c=new Array(o).fill(nn);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 rn(e,t=0,n=e.length){let r=nn;for(let o=t;o<n;o++)r=B(r,oe(e[o],e[o]));return r}function Ze(e,t){let n=e[Symbol.iterator](),r=t[Symbol.iterator]();for(;;){let o=n.next(),a=r.next();if(o.done)return a.done?0:-1;if(a.done)return 1;let i=o.value.codePointAt(0)-a.value.codePointAt(0);if(i!==0)return i}}function Xi(e,t,n){if(t instanceof Float64Array&&n===void 0){let m=Array.from(t);return{v:{kind:"numeric",values:m},missing:m.map(u=>Number.isNaN(u))}}let r=Array.from(t),o=new Set(r.filter(m=>m!==null).map(m=>typeof m));if(o.size>1)throw new TypeError(`column '${e}' mixes ${[...o].join(" and ")} values`);let a=[...o][0]??"number";if(a!=="number"&&a!=="string"&&a!=="boolean")throw new TypeError(`column '${e}' has values of an unsupported type`);if(a==="number"&&n===void 0){let m=r.map(u=>u===null?NaN:u);return{v:{kind:"numeric",values:m},missing:m.map(u=>Number.isNaN(u))}}let i=m=>m===null||typeof m=="number"&&Number.isNaN(m)?null:typeof m=="boolean"?m?"TRUE":"FALSE":String(m),l=r.map(i),s;n!==void 0?s=Array.from(n,String):a==="boolean"?s=["FALSE","TRUE"]:s=[...new Set(l.filter(m=>m!==null))].sort(Ze);let c=new Map(s.map((m,u)=>[m,u])),f=l.map(m=>m===null?-1:c.get(m)??-1);return{v:{kind:"factor",levels:s,codes:f},missing:f.map(m=>m<0)}}function Ms(e,t,n={}){if(typeof e!="object"||e===null)throw new TypeError("data must be an object of columns");let r=t.map(m=>{let u=Object.prototype.hasOwnProperty.call(e,m)?e[m]:void 0;if(u===void 0)throw new Error(`object '${m}' not found`);if(!Array.isArray(u)&&!(u instanceof Float64Array))throw new TypeError(`column '${m}' must be an array`);return u}),o;if(typeof n.weights=="string"){let m=Object.prototype.hasOwnProperty.call(e,n.weights)?e[n.weights]:void 0;if(m===void 0)throw new Error(`object '${n.weights}' not found`);o=m}else n.weights!==void 0&&(o=Array.from(n.weights));let a=r.length>0?r[0].length:o?.length??Object.values(e)[0]?.length??0;if(t.forEach((m,u)=>{if(r[u].length!==a)throw new RangeError(`variable lengths differ (found for '${m}')`)}),o!==void 0&&o.length!==a)throw new RangeError("variable lengths differ (found for '(weights)')");let i=t.map((m,u)=>Xi(m,r[u],n.levels?.[m])),l;o!==void 0&&(l=Array.from(o,m=>{if(m===null)return NaN;if(typeof m!="number")throw new TypeError("the weights must be numeric");return m}));let s=[];for(let m=0;m<a;m++)i.some(u=>u.missing[m])||l!==void 0&&Number.isNaN(l[m])||s.push(m);let c=i.map(({v:m})=>{if(m.kind==="numeric")return{kind:"numeric",values:s.map(p=>m.values[p])};let u=s.map(p=>m.codes[p]),h=m.levels;if(n.dropUnusedLevels){let p=h.map(()=>!1);for(let g of u)p[g]=!0;let d=new Map;h=h.filter((g,v)=>(p[v]&&d.set(v,d.size),p[v])),u=u.map(g=>d.get(g))}return{kind:"factor",levels:h,codes:u}}),f={rows:s,variables:c};if(l!==void 0&&(f.weights=s.map(m=>l[m]),f.weights.some(m=>m<0)))throw new RangeError("missing or negative weights not allowed");return f}function Qi(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 Ye(e,t,n={},r=e.rhs){if(typeof t!="object"||t===null)throw new TypeError("data must be an object of columns");let o=Ns(e,Object.keys(t),r);n.intercept!==void 0&&(o.intercept=n.intercept);let a=[...o.variables,...(n.extraVariables??[]).filter(w=>!o.variables.includes(w))],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=Ms(t,a,i),s=l.variables,c=l.rows.length,f=o.terms.map((w,b)=>b!==o.responseTerm),m=o.terms.map((w,b)=>w.map(N=>o.factors[N][b]));for(let w=0;w<o.terms.length;w++)if(f[w])for(let b of o.terms[w]){let N=s[b];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 w=0;w<o.terms.length;w++){if(!f[w])continue;let b=o.terms[w];for(let N=0;N<b.length;N++)if(s[b[N]].kind==="factor"){m[w][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=(w,b)=>{let N=s[w],y=o.variables[w];if(N.kind==="numeric")return[{name:y,values:N.values}];let E=b===2?"indicators":n.allContrasts??n.contrasts?.[y]??"contr.treatment",{m:M,names:x}=Qi(N.levels,E);return x.map((A,k)=>({name:y+A,values:N.codes.map(S=>M[S][k])}))};o.terms.forEach((w,b)=>{if(!f[b])return;let N=d(w[0],m[b][0]);for(let y=1;y<w.length;y++){let E=[];for(let M of d(w[y],m[b][y]))for(let x of N)E.push({name:`${x.name}:${M.name}`,values:x.values.map((A,k)=>A*M.values[k])});N=E}for(let y of N)u.push(y.values),h.push(y.name),p.push(b+1)});let g=Array.from({length:c},(w,b)=>u.map(N=>N[b])),v={terms:o,frame:l,x:g,colNames:h,assign:p,warnings:[...o.warnings]};if(o.response>=0){let w=s[o.response];if(w.kind!=="numeric")throw new TypeError("the response must be numeric");v.y=w.values}return v}function Ft(e,t,n={}){let r=Ye(It(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 Ki(e,t,n,r,o){let a=t.length,i=n.flatMap((d,g)=>rn(d)[0]>1e-5?[g]:[]),l=0,s=[],c=t.slice();if(i.length>0){let d=he(Array.from({length:a},(g,v)=>i.map(w=>n[w][v][0])));l=d.rank,s=d.pivot.slice(0,l).map(g=>i[g]),c=Ot(s.map(g=>n[g]),t).qty}let f=[];s.forEach((d,g)=>{let v=r[d],w=f.find(b=>b.term===v);w===void 0&&f.push(w={term:v,df:0,ss:[0,0]}),w.df++,w.ss=B(w.ss,oe(c[g],c[g]))});let m=a-l,u=rn(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,g=h.map(({df:v,ss:w})=>{let b=w[0]/v/d;return[v,w[0],w[0]/v,b,ae(b,v,m,!1,!1)]});return g.push([m,u,d,NaN,NaN]),{stratum:`Error: ${e}`,colNames:["Df","Sum Sq","Mean Sq","F value","Pr(>F)"],rowNames:[...p,"Residuals"],table:g}}return{stratum:`Error: ${e}`,colNames:["Df","Sum Sq","Mean Sq"],rowNames:p,table:h.map(({df:d,ss:g})=>[d,g[0],g[0]/d])}}function xs(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=Ye(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=Ye({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]),g=i.colNames.map((y,E)=>d(i.x.map(M=>M[E]))),{fit:v,qtv:w}=ze(p.map(y=>d(s.x.map(E=>E[y]))),d(l),g),b=[];p.forEach((y,E)=>{let M=s.assign[y],x=M===0?"(Intercept)":s.terms.termLabels[M-1],A=b.find(k=>k.name===x);A===void 0&&b.push(A={name:x,idx:[]}),A.idx.push(E)}),c>h&&b.push({name:"Within",idx:Array.from({length:c-h},(y,E)=>h+E)});let N=b.map(y=>Ki(y.name,y.idx.map(E=>v.qty[E]),w.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=Es(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,Ye(It(e),t,{...n,dropUnusedLevels:!0}),!1):Zi(e,t,n)}function Zi(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 Es(s,c,f,r).result}function Yi(e,t,n,r,o,a){let i=o.rank,l=o.pivot.slice(0,i),s=Dn(n===void 0?void 0:r.map(w=>n[w]),r.length),c=l.map(w=>r.map((b,N)=>Te(s[N],e[b][w]))),{qty:f,coef:m}=Ot(c,r.map((w,b)=>Te(s[b],t[w]))),u=e[0]?.map(()=>NaN)??[];l.forEach((w,b)=>u[w]=m[b][0]);let h=e.map(w=>{let b=[0,0];return l.forEach((N,y)=>b=B(b,Te(m[y],w[N]))),b}),p=rn(f,i)[0],d=r.map(w=>n?.[w]??1),g=[0,0];if(a){let w=[0,0],b=[0,0];r.forEach((N,y)=>{w=B(w,[d[y],0]),b=B(b,Te(h[N],d[y]))}),g=ge(b,w)}let v=[0,0];return r.forEach((w,b)=>{let N=le(h[w],g);v=B(v,Te(oe(N,N),d[b]))}),{coefficients:u,fittedValues:h.map(w=>w[0]),residuals:h.map((w,b)=>le([t[b],0],w)[0]),effects:f.map(w=>w[0]),rss:p,mss:v[0]}}function Es(e,t,n,r,o=!1){if(t.length===0)throw new RangeError("0 (non-NA) cases");let a=t.map(($,G)=>n===void 0||n[G]!==0),i=e.filter(($,G)=>a[G]),l=t.filter(($,G)=>a[G]),s=n?.filter(($,G)=>a[G]),c=l.length;if(c===0)throw new RangeError("all weights are zero");let f=t.flatMap(($,G)=>a[G]?[G]:[]),m=s?.map(Math.sqrt),u=m===void 0?i:i.map(($,G)=>$.map(Q=>Q*m[G])),h=m===void 0?l:l.map(($,G)=>$*m[G]),p=he(u),d=p.rank,g=o?Yi(e,t,n,f,p,r):void 0,v=g?.coefficients??Tt(p,h),w=g?.residuals??[],b=g?.fittedValues??[],N=c-d,y=s??new Array(c).fill(1),E=g?.rss??0;if(g===void 0){let $=En(p,h),G=m===void 0?$:$.map((ne,de)=>ne/m[de]),Q=0;t.forEach((ne,de)=>{if(a[de])w.push(G[Q]),b.push(ne-G[Q]),Q++;else{let be=e[de].reduce((Fe,_e,Ie)=>Number.isNaN(v[Ie])?Fe:Fe+_e*v[Ie],0);b.push(be),w.push(ne-be)}});for(let ne=0;ne<c;ne++)E+=y[ne]*G[ne]**2}let M=b.filter(($,G)=>a[G]),x=E/N,A=Math.sqrt(x),k=p.qr.slice(0,d).map($=>$.slice(0,d)),S=d>0?An(k):[],q=p.pivot.slice(0,d).map(($,G)=>[$,G]).sort(($,G)=>$[0]-G[0]),R=q.map(([$])=>v[$]),V=q.map(([,$])=>Math.sqrt(S[$][$]*x)),F=R.map(($,G)=>$/V[G]),I=F.map($=>2*we(-Math.abs($),N,!0,!1)),C={coefficients:v,residuals:w,fittedValues:b,rank:d,dfResidual:N,coefTable:{estimate:R,stdError:V,tValue:F,pValue:I},sigma:A,rSquared:0,adjRSquared:0,intercept:r,qr:p},z=r?1:0,J=d-z,T={result:C,fitRows:f,effects:g?.effects??[],rss:E};if(J===0)return T;let O=0;if(g!==void 0)O=g.mss;else if(r){let $=y.reduce((Q,ne)=>Q+ne,0),G=0;for(let Q=0;Q<c;Q++)G+=y[Q]*M[Q]/$;for(let Q=0;Q<c;Q++)O+=y[Q]*(M[Q]-G)**2}else for(let $=0;$<c;$++)O+=y[$]*M[$]**2;C.rSquared=O/(O+E),C.adjRSquared=1-(1-C.rSquared)*((c-z)/N);let D=O/J/x;return C.fStatistic={value:D,numdf:J,dendf:N},C.fPValue=ae(D,J,N,!1,!1),T}function Ji(e,t={}){let n=t.level??.95,r=je((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 el(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,g,v)=>Number.isNaN(e.coefficients[v])?d:d+g*e.coefficients[v],0)),f=o.map(p=>{let d=new Array(a).fill(0);for(let g=0;g<a;g++){let v=p[i[g]];for(let w=0;w<g;w++)v-=l[w][g]*d[w];d[g]=v/l[g][g]}return Math.sqrt(d.reduce((g,v)=>g+v*v,0)*s)}),m=n.interval??"none";if(m==="none")return{fit:c,seFit:f};let u=je((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 ks(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"?tl(e,t,n):nl(e,t)}function tl(e,t,n){let r=It(e,!0),{treatment:o,errors:a}=vs(r.rhs);if(a.length>1)throw new Error(`there are ${a.length} Error terms: only 1 is allowed`);return a.length===1?xs(e,r,o,a[0],t,n):_r(e,Ye(r,t,{...n,dropUnusedLevels:!0},o),!0)}function nl(e,t){let n=pt(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}=ks(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 rl(e,t,n={}){let r;t!==void 0&&!Array.isArray(t)?n=t:r=t;let{labels:o,groups:a}=ht(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,g)=>d+g.length,0)-i,{ssw:f,means:m}=ks(a),u=f/c,h=Do(l,i,c),p={names:[],i:[],j:[],diff:[],lwr:[],upr:[],pAdj:[]};for(let d=0;d<i-1;d++)for(let g=d+1;g<i;g++){let v=m[g]-m[d],w=Math.sqrt(u/2*(1/a[g].length+1/a[d].length));p.names.push(`${o[g]}-${o[d]}`),p.i.push(g+1),p.j.push(d+1),p.diff.push(v),p.lwr.push(v-h*w),p.upr.push(v+h*w),p.pAdj.push(Nn(Math.abs(v)/w,i,c,{lowerTail:!1}))}return p}function ol(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(b=>!Number.isNaN(b)),f=r===void 0?void 0:Array.from(r).filter(b=>!Number.isNaN(b));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,g,v;if(l==="less")g=P(d),v=[-1/0,p+H(s)*h];else if(l==="greater")g=P(d,{lowerTail:!1}),v=[p-H(s)*h,1/0];else{g=2*P(-Math.abs(d));let b=H(1-(1-s)/2)*h;v=[p-b,p+b]}let w=f!==void 0;return{method:w?"Two-sample z-Test":"One-sample z-Test",alternative:l,statistic:d,statisticName:"z",pValue:g,confInt:v,confLevel:s,estimate:w?[m,u]:m,estimateNames:w?["mean of x","mean of y"]:"mean of x",nullValue:i,nullValueName:w?"difference in means":"mean"}}var Ee=2**-52,sl={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"]},$r=e=>Number.isFinite(e)&&e>0,_t=()=>!0,Ne=class{list=[];add(t){this.list.includes(t)||this.list.push(t)}},Pn;function jn(e,t){let n=Pn;Pn=e;try{return t()}finally{Pn=n}}function al(e){return e<0&&Pn?.add("NaNs produced"),Math.sqrt(e)}function Dr(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:$r};case"1/mu^2":return{name:e,link:t=>1/(t*t),linkinv:t=>1/al(t),muEta:t=>-1/(2*t**1.5),etaOk:$r};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=-lr(Ee,0,1,!0,!1);return{name:e,link:n=>lr(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 il(e){if(!Number.isFinite(e))throw new RangeError("invalid power for the power link");if(e<=0)return Dr("log");if(e===1)return Dr("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:$r}}function ll(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:As};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:qs};default:throw new RangeError(`'variance' "${String(e)}" is invalid: possible values are "mu(1-mu)", "mu", "mu^2", "mu^3" and "constant"`)}}function As(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 qs(e,t,n){return n*(e-t)**2/(e*t*t)}function on(e,t){let n=typeof e=="string"?{family:e}:e,r=n.family,o=sl[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=il(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=Dr(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:As};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:qs};case"quasi":{let s=n.variance??"constant",c=ll(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
4
+ 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,g)=>m[g]===0?0:d[0]/m[g]),h=n.map((d,g)=>d*m[g]),p=u.map((d,g)=>(m[g]*d+.5)/(m[g]+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 Rs(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]*Et(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 cl(e){let t=e.map(n=>`"${n}"`);return t.length===1?t[0]:`c(${t.join(", ")})`}function zn(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 gt(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 Ts(e,t){let{x:n,p:r,y:o,w:a,offset:i,fam:l,epsilon:s,maxit:c}=e,f=o.length;return jn(t,()=>{let m=(L,q)=>L.every(R=>l.etaOk(R))&&q.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 q=L.map(F=>l.linkinv(F));if(!q.every(F=>l.muOk(F)))throw new Error("invalid fitted means in empty model");let R=L.map(F=>l.muEta(F)),V=q.map((F,I)=>Math.sqrt(a[I]*R[I]**2/l.variance(F))**2);return{coefficients:[],eta:L,mu:q,deviance:gt(l,o,q,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-q[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 ${cl(e.columnNames)}`);h=e.start.slice(),u=zn(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=gt(l,o,p,a),g=d,v=!1,w=!1,b=0,N=new Array(r).fill(0),y,E=a.map(()=>!1),M=[],x=Math.min(1e-7,s/1e3);for(b=1;b<=c;b++){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 q=[];for(let I=0;I<f;I++)E[I]&&q.push(I);if(q.length===0){t.add(`no observations informative at iteration ${b}`),v=!1;break}M=new Array(f).fill(0);let R=[],V=[];for(let I of q){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=Tt(y,R);if(F.some((I,C)=>y.pivot.indexOf(C)<y.rank&&!Number.isFinite(I))){t.add(`non-finite coefficients at iteration ${b}`),v=!1;break}if(N=F.map(I=>Number.isNaN(I)?0:I),w=!1,u=zn(n,N,i),p=u.map(I=>l.linkinv(I)),g=gt(l,o,p,a),!Number.isFinite(g)){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(g);){if(I>=c)throw new Error("inner loop 1; cannot correct step size");I++,N=N.map((C,z)=>(C+h[z])/2),u=zn(n,N,i),p=u.map(C=>l.linkinv(C)),g=gt(l,o,p,a)}w=!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=zn(n,N,i),p=u.map(C=>l.linkinv(C))}w=!0,g=gt(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)){v=!0;break}}else if(Math.abs(g-d)/(Math.abs(g)+.1)<s){v=!0;break}d=g,h=N.slice()}b>c&&(b=c);let A=r,k=N.slice();if(y!==void 0){A=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:g,iter:b,converged:v,boundary:w,rank:A,qr:y,used:E,workingWeights:M.map(L=>L*L),workingResiduals:o.map((L,q)=>(L-p[q])/S[q])}})}function Ss(e){return e.length>0&&typeof e[0]!="number"?e.map(t=>Array.from(t)):Array.from(e,t=>[t])}function Ls(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=on(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=Ts({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 Ls(c,o,n,t.length===0),{fit:c,fam:o,y:i.y,w:i.weights,size:i.size,offset:l}}function fl(e,t,n={}){let r=n.intercept??!0,o=Ss(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]),g=o.length>0?o[0].length:0,v=[...r?["(Intercept)"]:[],...Array.from({length:g},(V,F)=>`x${F+1}`)],w=v.length;if(m.some(V=>V.length!==w))throw new TypeError("all rows of x must have the same length");let b=typeof n.family=="object"?{...n.family}:{family:n.family??"gaussian"};n.link!==void 0&&(b.link=n.link);let N={x:m,columnNames:v,yGiven:u,priorGiven:p,offset:d,hasOffset:c!==void 0,intercept:r,familySpec:b,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:w},(V,F)=>F),{fit:M,fam:x,y:A,w:k,size:S}=Vt(N,E,y),L;if(r&&N.hasOffset){let V=new Ne,F=Ts({x:m.map(()=>[1]),p:1,y:A,w:k,offset:d,fam:x,epsilon:N.epsilon,maxit:N.maxit,columnNames:["(Intercept)"],mustart:M.mu},V);Ls(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]*A[C];let I=F/V;L=gt(x,A,A.map(()=>I),k)}else L=gt(x,A,jn(y,()=>d.map(V=>x.linkinv(V))),k);let q=h-k.filter(V=>V===0).length,R=Rs(x,A,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:q-M.rank,dfNull:q-(r?1:0),iter:M.iter,converged:M.converged,boundary:M.boundary,y:A,priorWeights:k,weights:M.workingWeights,residuals:M.workingResiduals,family:x.family,link:x.name,warnings:y.list,qr:M.qr,model:N}}function Wn(e){return on(e.model.familySpec)}function $t(e,t){if(Wn(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 ml(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 An(t)}function jr(e,t={}){let n=new Ne,r=Wn(e),o=t.dispersion===void 0&&!r.fixedDispersion,a=t.dispersion??$t(e,n),i=ml(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=Wn(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=Ho(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 ul(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 hl(e,t={}){let n=Wr(e),r=t.dispersion??$t(e),o=zr(e,"deviance").filter((i,l)=>e.priorWeights[l]>0),a=100*Ct*ul(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 pl(e,t={}){let n=Wr(e),r=t.dispersion??$t(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 dl(e,t={}){let n=Wn(e),r,o;if(t.newdata===void 0)r=e.model.x,o=e.linearPredictors.slice();else{r=Ss(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??$t(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}:jn(new Ne,()=>({fit:o.map(f=>n.linkinv(f)),seFit:c.map((f,m)=>f*Math.abs(n.muEta(o[m])))}))}var Is={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,g)=>e[d].map(v=>v*m[g])),{tol:o}),h=En(u,a.map((d,g)=>t[d]*m[g])),p=0;for(let d of h)p+=d*d;return c-p}function gl(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,{...Is,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 g=i.x.map(E=>l.map(M=>E[M])),v=c.fit.eta,w=(E,M)=>{let x=l.filter(S=>S!==E),A=Vt(i,x,new Ne,{...Is,start:void 0,etastart:v,extraOffset:i.x.map(S=>S[E]*M)}).fit,k=r==="LRT"?A.deviance-c.fit.deviance:Gr(g,A.workingResiduals,A.workingWeights,!1);return Math.sign(M-f[E])*Math.sqrt(Math.max(k/h,0))},b=[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 A=x===0?-1:1,k=NaN,S=NaN;for(let C=0;C<=30;C++){let z=f[E]+A*2**C*p[E],J=w(E,z);if(Math.abs(J)>=Math.abs(b[x])){k=z,S=J;break}}if(Number.isNaN(k))continue;let L=C=>C===f[E]?-b[x]:w(E,C)-b[x],[q,R]=x===0?[k,f[E]]:[f[E],k],[V,F]=x===0?[S-b[x],-b[x]]:[-b[x],S-b[x]],I=xe(L,q,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 wl=(e,t)=>me(e,t/2,2,!1,!1);function bl(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 vl(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=on(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,A)=>c.push({name:String(A+1),dfRes:x.dfResidual,dev:x.deviance})),f=c.map((x,A)=>A===0?NaN:c[A-1].dfRes-x.dfRes),m=c.map((x,A)=>A===0?NaN:c[A-1].dev-x.dev),s==="Rao"){let x=n.map(A=>({dfRes:A.dfResidual,dev:A.deviance,fit:Os(A),x:A.model.x,intercept:A.model.intercept}));u=c.map((A,k)=>k===0?NaN:f[k]<0?-Ur(x[k],x[k-1]):Ur(x[k-1],x[k]))}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,k=x.intercept?[0]:[],S=q=>[...k,...Array.from({length:q},(R,V)=>V+k.length)],L=[];for(let q=0;q<=A;q++)q===A?L.push({dfRes:M.dfResidual,dev:M.deviance,fit:Os(M),x:x.x,intercept:x.intercept}):q>0||s==="Rao"?L.push(bl(x,S(q),r)):L.push(void 0);c.push({name:"NULL",dfRes:M.dfNull,dev:M.nullDeviance});for(let q=1;q<=A;q++)c.push({name:`x${q}`,dfRes:L[q].dfRes,dev:L[q].dev});f=c.map((q,R)=>R===0?NaN:c[R-1].dfRes-q.dfRes),m=c.map((q,R)=>R===0?NaN:Math.max(c[R-1].dev-q.dev,0)),s==="Rao"&&(u=c.map((q,R)=>R===0?NaN:Ur(L[R-1],L[R]))),h=M}let p=new Ne,d=t.dispersion??$t(h,p);p.list.forEach(M=>r.add(M));let g=l?1/0:h.dfResidual,v=o?["Resid. Df","Resid. Dev","Df","Deviance"]:["Df","Deviance","Resid. Df","Resid. Dev"],w=c.map((M,x)=>o?[M.dfRes,M.dev,f[x],m[x]]:[f[x],m[x],M.dfRes,M.dev]),b=[...v],N=M=>c.map((x,A)=>{if(A===0||f[A]===0)return NaN;let k=M[A]/d*Math.sign(f[A]);return k<0||Number.isNaN(k)?NaN:wl(k,Math.abs(f[A]))}),y=[];if(s==="Chisq"||s==="LRT")b.push("Pr(>Chi)"),y=[N(m)];else if(s==="Rao")b.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`),g!==0){let M=c.map((x,A)=>{let k=m[A]/f[A]/d;return A===0||f[A]===0||k<0?NaN:k});b.push("F","Pr(>F)"),y=[M,M.map((x,A)=>Number.isNaN(x)?NaN:ae(x,Math.abs(f[A]),g,!1,!1))]}}else if(s==="Cp"){let M=h.model.x.length;b.push("Cp"),y=[c.map(x=>x.dev+2*d*(M-x.dfRes))]}let E=w.map((M,x)=>[...M,...y.map(A=>A[x])]);return{colNames:b,rowNames:c.map(M=>M.name),table:E,warnings:r.list}}function Os(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 Nl(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 Fs=(e,t,n)=>ae(e,t,n,!1,!1);function yl(e){let t=Nl(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}=yl(e),i=o/r,l=n.map(({df:s,ss:c})=>{let f=c/s/i;return[s,c,c/s,f,Fs(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 Ml(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:Fs(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):Ml(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 _s(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=Dn(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,g)=>p.has(d)?[g]:[]),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])),g=s(new Set(c.filter(v=>v!==p)));f.push(p===0?"(Intercept)":e.termLabels[p-1]),m.push({ss:_s(e,g,d).ss,df:d.length})}}else{let p=d=>e.factors.flatMap((g,v)=>g[d-1]>0?[v]:[]);for(let d of c){if(d===0)continue;let g=p(d),v=c.filter(N=>N!==0&&N!==d&&g.every(y=>e.factors[y][N-1]>0)),w=s(new Set(c.filter(N=>N!==d&&!v.includes(N)))),b=_s(e,w,s(new Set([d])));f.push(e.termLabels[d-1]),m.push(b)}}let u=a/o,h=m.map(({ss:p,df:d})=>{let g=p/d/u;return[p,d,g,ae(g,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 Gn=[0,0];var Cs=e=>e.map(t=>t.map(n=>n[0]));function sn(e,t){let n=t[0]?.length??0;return e.map(r=>{let o=new Array(n).fill(Gn);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 wt(e){let t=Gn;return e.forEach((n,r)=>t=B(t,n[r])),t}function Vs(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 $s(e){let{a:t,sign:n,singular:r}=Vs(e);if(r)return Gn;let o=[n,0];return t.forEach((a,i)=>o=oe(o,a[i])),o}function Ds(e){let t=e.length,{a:n,perm:r,singular:o}=Vs(e);if(o)throw new Error("singular matrix");let a=Array.from({length:t},()=>new Array(t).fill(Gn));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 Ps=(e,t)=>me(e,t/2,2,!1,!1);function Kr(e){let t=e.length,n=Xt(wt(e),t),r=[1,0];for(let o=0;o<t;o++)r=oe(r,n);return n=ge($s(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=Ps(i,l),c=Ps(i,l+4);return s+a*(c-s)}var xl=(e,t)=>Array.from({length:t},(n,r)=>e.map(o=>o[r]));function El(e,t,n){let r=e.length,o=v=>v.length===0?0:he(Array.from({length:r},(w,b)=>v.map(N=>N[b]))).rank,a=n.length===0?void 0:he(Array.from({length:r},(v,w)=>n.map(b=>b[w]))),i=a===void 0?[]:a.pivot.slice(0,a.rank).map(v=>n[v]),l=i.length,s=[...i,...t],c=he(Array.from({length:r},(v,w)=>s.map(b=>b[w])));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(v=>s[v]),m=v=>v.map(w=>[w,0]),u=new Array(r).fill([0,0]),h=ze(f.map(m),u,e.map((v,w)=>e.map(b=>b[w]))).qtv,p=Array.from({length:r},(v,w)=>h.map(b=>b[w])),d=ze(f.map(m),u,p).qtv,g=Array.from({length:c.rank-l},(v,w)=>l+w);return g.map(v=>g.map(w=>d[v][w]))}function kl(e,t={}){let n=e.map(w=>Array.from(w)),r=n.length,o=n[0]?.length??0;if(o===0||n.some(w=>w.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(Ze),s=n.map(w=>w.map(()=>[0,0]));for(let w of l){let b=i.flatMap((N,y)=>N===w?[y]:[]);for(let N=0;N<o;N++){let y=[0,0];for(let M of b)y=B(y,[n[M][N],0]);let E=Xt(y,b.length);for(let M of b)s[M][N]=le([n[M][N],0],E)}}let c=r-l.length,f=Array.from({length:o},(w,b)=>Array.from({length:o},(N,y)=>{let E=[0,0];for(let M of s)E=B(E,oe(M[b],M[y]));return E})),m=t.idata??{index:Array.from({length:o},(w,b)=>b+1)},u=w=>{let b=Ft(w,m);if(b.x.length!==o)throw new RangeError("idata must have one row per column of y");return xl(b.x,b.colNames.length)},h=t.M===void 0?Array.from({length:o},(w,b)=>Array.from({length:o},(N,y)=>y===b?1:0)):u(t.M),p=u(t.X??"~0"),d=El(f,h,p),g=d.length,v=Kr(d);return{statistic:v,statisticName:"W",pValue:Zr(v,g,c,o),method:"Mauchly's test of sphericity"}}var js=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 Le(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 bt=(e,t,n,r)=>vn(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 Al(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;Le([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??js,f=i==="two.sample"?2:1,m=l==="two.sided"?2:1;m===2&&!j(n)&&(n=Math.abs(n));let u=(d,g,v,w)=>{let b=Math.max(1e-7,d-1)*f,N=ct(w/m,b,{lowerTail:!1}),y=Math.sqrt(d/f)*g/v,E=bt(N,b,y,!1);return s&&m===2?E+bt(-N,b,y,!0):E};if(j(r))r=u(t,n,o,a);else{let d=r;j(t)?t=Y(g=>u(g,n,o,a)-d,2,1e7,{tol:c,extendInt:"upX"}):j(o)?o=Y(g=>u(t,n,g,a)-d,n*1e-7,n*1e7,{tol:c,extendInt:"downX"}):j(n)?n=Y(g=>u(t,g,o,a)-d,o*1e-7,o*1e7,{tol:c,extendInt:"upX"}):a=Y(g=>u(t,n,o,g)-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 ql(e={}){let{n:t,p1:n,p2:r,power:o}=e,a=e.sigLevel===void 0?.05:e.sigLevel;Le([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??js,c=i==="two.sided"?2:1,f=(u,h,p,d)=>{let g=H(d/c,{lowerTail:!1});if(l&&c===2){let v=(h+p)/2,w=h*(1-h),b=p*(1-p),N=v*(1-v),y=Math.abs(h-p),E=Math.sqrt(w+b);return P((Math.sqrt(u)*y-g*Math.sqrt(2*N))/E)+P((Math.sqrt(u)*y+g*Math.sqrt(2*N))/E,{lowerTail:!1})}return P((Math.sqrt(u)*Math.abs(h-p)-g*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 Rl(e={}){let{groups:t,n,betweenVar:r,withinVar:o,power:a}=e,i=e.sigLevel===void 0?.05:e.sigLevel;if(Le([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 it(Ke(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 Tl={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:Tl[n][["small","medium","large"].indexOf(r)],method:"Conventional effect size from Cohen (1982)"}}function Sl(e,t){return 2*Math.asin(Math.sqrt(e))-2*Math.asin(Math.sqrt(t))}function Ll(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 Il(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 Ol="\u2018sig.level\u2019 must be numeric in [0, 1]",Fl="\u2018power\u2019 must be numeric in [0, 1]";function Je(e,t){let n=r=>!j(r)&&!(typeof r=="number"&&r>=0&&r<=1);if(n(e))throw new RangeError(Ol);if(n(t))throw new RangeError(Fl)}function et(e,t){return typeof e=="string"?zs(t,e).effectSize:e}var an=["two.sided","less","greater"];function Un(e,t){return e==="two.sided"?t:e==="less"?[-10,5]:[-5,10]}function Ws(e,t,n,r){if(r==="less")return bt(ct(n,e),e,t,!0);if(r==="greater")return bt(ct(n,e,{lowerTail:!1}),e,t,!1);let o=ct(n/2,e,{lowerTail:!1});return bt(o,e,t,!1)+bt(-o,e,t,!0)}function Gs(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 _l(e={}){let{n:t,power:n}=e,r=et(e.d,"t"),o=e.sigLevel===void 0?.05:e.sigLevel;Le([t,r,n,o],"exactly one of n, d, power, and sig.level must be NULL"),Je(o,n);let a=ie(e.type,["two.sample","one.sample","paired"]),i=ie(e.alternative,an),l=a==="two.sample"?2:1;i==="two.sided"&&!j(r)&&(r=Math.abs(r));let s=(m,u,h)=>Ws((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,...Un(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 Cl(e={}){let{n1:t,n2:n,power:r}=e,o=et(e.d,"t"),a=e.sigLevel===void 0?.05:e.sigLevel;if(Le([t,n,o,r,a],"exactly one of n1, n2, d, power, and sig.level must be NULL"),Je(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,an);i==="two.sided"&&!j(o)&&(o=Math.abs(o));let l=(s,c,f,m)=>Ws(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,...Un(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=et(e,n==="h"?"p":"t"),c=t.sigLevel===void 0?.05:t.sigLevel;if(Le([s,i,l,c],`exactly one of ${n}, n, power, and sig.level must be NULL`),Je(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,an);f==="two.sided"&&!j(s)&&(s=Math.abs(s));let m=(u,h,p)=>Gs(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,...Un(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 Vl(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 Dl(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 Pl(e={}){let{n1:t,n2:n,power:r}=e,o=et(e.h,"p"),a=e.sigLevel===void 0?.05:e.sigLevel;if(Le([o,t,n,r,a],"exactly one of h, n1, n2, power, and sig.level must be NULL"),Je(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,an);i==="two.sided"&&!j(o)&&(o=Math.abs(o));let l=(s,c,f,m)=>Gs(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,...Un(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 jl(e={}){let{n:t,power:n}=e,r=et(e.r,"r"),o=e.sigLevel===void 0?.05:e.sigLevel;if(Le([t,r,n,o],"exactly one of n, r, power, and sig.level must be NULL"),Je(o,n),!j(t)&&t<4)throw new RangeError("number of observations must be at least 4");let a=ie(e.alternative,an);a==="two.sided"&&!j(r)&&(r=Math.abs(r));let i=(s,c,f,m)=>{let u=ct(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),g=P((p-d)*Math.sqrt(s-3));return m?g+P((-p-d)*Math.sqrt(s-3)):g},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 zl(e){let{N:t,power:n}=e,{df:r}=e,o=et(e.w,"chisq"),a=e.sigLevel===void 0?.05:e.sigLevel;if(Le([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(Je(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)=>Pe(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 Wl(e={}){let{k:t,n,power:r}=e,o=et(e.f,"anov"),a=e.sigLevel===void 0?.05:e.sigLevel;if(Le([t,n,o,r,a],"exactly one of k, n, f, power, and sig.level must be NULL"),Je(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 it(Ke(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 Gl(e={}){let{u:t,v:n,power:r}=e,o=et(e.f2,"f2"),a=e.sigLevel===void 0?.05:e.sigLevel;if(Le([t,n,o,r,a],"exactly one of u, v, f2, power, and sig.level must be NULL"),Je(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)=>it(Ke(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 Ul="Missing values detected. NAs dropped.",Xs={tol:0,maxiter:5e3};function Hl(e){return Math.exp(sr(e/2)-Math.log(Math.sqrt(e/2))-sr((e-1)/2))}function Us(e,t,n){return ft(r=>vn(e,t,{ncp:r})-n,e-1,e+1,{...Xs,extendInt:"downX"}).root}function Qs(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(Ul);let u,h,p,d,g;if(f===void 0||i){let x;if(f===void 0)x=Ae(c);else{let[k,S]=Rt(c,f);x=k.map((L,q)=>L-S[q])}let A=x.length;u=se(x),h=Math.sqrt(re(x)),p=h/Math.sqrt(A),d=1/A,g=A-1}else{let x=Ae(c),A=Ae(f),k=x.length,S=A.length;u=se(x)-se(A);let L=Math.sqrt(re(x)),q=Math.sqrt(re(A));o?(h=Math.sqrt(((k-1)*L**2+(S-1)*q**2)/(k+S-2)),p=h*Math.sqrt(1/k+1/S),d=1/k+1/S,g=k+S-2):(h=Math.sqrt((L**2+q**2)/2),d=2*(S*L**2+k*q**2)/(k*S*(L**2+q**2)),p=Math.sqrt(L**2/k+q**2/S),g=p**4/((L**2/k)**2/(k-1)+(q**2/S)**2/(S-1)))}let v=r?Hl(g):1,w=(u-a)/h*v,b=r?"Hedges_g":"Cohens_d";if(l===null)return{name:b,value:w,warnings:m};let N=s==="two.sided"?l:2*l-1,y=(u-a)/p,E=Us(y,g,(1+N)/2)*Math.sqrt(d)*v,M=Us(y,g,(1-N)/2)*Math.sqrt(d)*v;return s==="less"&&(E=-1/0),s==="greater"&&(M=1/0),{name:b,value:w,ci:l,ciLow:E,ciHigh:M,warnings:m}}function Bl(e,t,n={}){return Qs(e,t,n,n.adjust??!1)}function Xl(e,t,n={}){return Qs(e,t,n,!0)}var Ql=e=>Array.isArray(e.effects),Ks=e=>e.multistratum===!0,Kl=e=>Array.isArray(e.strata),Zs=e=>{let t=e;return typeof t.univariate=="object"&&typeof t.anova=="object"},Zl=e=>e.replace(/[.*+?^${}()|[\]\\]/g,"\\$&"),to=e=>new RegExp(`\\b${Zl(e)}\\b`);function Yl(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 Jl(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(v=>to(v).test(m)),d=o(p.length===0?"Subject":`Subject:${p.join(":")}`,h[c],h[f]);if(m==="(Intercept)")return;let g={name:m,df:h[s],ss:h[n]};d.terms.push(g),a.set(m,g)})}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 Ys(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 Hs(e,t,n,r){let o=a=>it(e,t,n,{ncp:a})-r;return o(0)>0?e===1/0?1/0:ft(o,0,Math.max(10,e*t*4),{...Xs,extendInt:"downX"}).root:0}function Js(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=Hs(l,t,n,(1+a)/2),c=Hs(l,t,n,(1-a)/2);l<=Ke((1-a)/2,t,n)&&(c=0),l<=Ke((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 Bs="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(Bs);if(Ks(e)||Kl(e)||Zs(e))return ec(e,t,n);let r=Ql(e)?Br(e):e;if(!Array.isArray(r.colNames)||!Array.isArray(r.table))throw new TypeError(Bs);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,g=t==="Eta2"?n.generalized??!1:!1,w=f.length===1?"plain":g!==!1?"generalized":n.partial??!0?"partial":"plain",{isObserved:b,ssObserved:N}=Ys(g,f),y=M=>{let{ss:x,df:A}=M;if(t==="Eta2")return w==="partial"?x/(x+u):w==="plain"?x/p:x/(x+u+N-(b(M.name)?x:0));if(t==="Omega2"){let S=w==="partial"?(x-A*h)/(x+(d-A)*h):(x-A*h)/(p+h);return Math.max(0,S)}let k=w==="partial"?(x-A*h)/(x+u):(x-A*h)/p;return Math.max(0,k)},E=`${t}${w==="plain"?"":`_${w}`}`;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)]:Js(y(M),M.df,m,o,a)),warnings:[]}}function ec(e,t,n){let r=n.ci===void 0?.95:n.ci,o=ie(n.alternative??"greater",["two.sided","less","greater"]),a=Zs(e),i,l,s;if(a){let N=Jl(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}=Yl(Ks(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:g}=Ys(h,c),v=(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+g-(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 A=p==="partial"?(E-M*x)/(E+y.sse):(E-M*x)/m;return Math.max(0,A)},w=`${t}${p==="plain"?"":`_${p}`}`,b={colNames:r===null?[w]:[w,"CI","CI_low","CI_high"],rowNames:s.map(N=>N.term.name),table:s.map(({term:N,stratum:y})=>{let E=v(N,y);return r===null?[E]:Js(E,N.df,y.dfe,r,o)}),warnings:[]};return a||(b.groups=s.map(N=>N.stratum.name)),b}function tc(e,t={}){return no(e,"Eta2",t)}function nc(e,t={}){return no(e,"Omega2",t)}function rc(e,t={}){return no(e,"Epsilon2",t)}var Hn=e=>e===void 0?[]:typeof e=="string"?[e]:[...e],oc=Math.sqrt(2220446049250313e-31);function Bn(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 ln=e=>typeof e=="boolean"?e?"TRUE":"FALSE":String(e);function sc(e){let t=[...new Set(e)];return t.every(n=>typeof n=="number")?t.sort((n,r)=>n-r).map(ln):[...new Set(t.map(ln))].sort(Ze)}var ea=e=>e.replace(/[.*+?^${}()|[\]\\]/g,"\\$&");function ta(e,t,n,r){let o=s=>r.some(c=>new RegExp(`\\b${ea(c)}\\b`).test(s));for(let s of r)if(!e.some(c=>new RegExp(`\\b${ea(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 ac(e,t){let n=Hn(t.between),r=Hn(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=Hn(t.es??"ges"),l=Hn(t.observed),s=[],c=Bn(e,t.id).map(ln),f=Bn(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=>Bn(e,T)),u=r.map(T=>Bn(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=>sc(T)),d=m.map(T=>T.map(ln)),g=u.map(T=>T.map(ln)),v=g.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 w=[...new Set(c)],b=new Map,N=new Set;if(c.forEach((T,O)=>{let D=JSON.stringify(d.map(G=>G[O])),$=b.get(T);$===void 0?b.set(T,D):$!==D&&N.add(T)}),N.size>0)throw new Error(`Following ids are in more than one between subjects condition:
5
+ ${w.filter(T=>N.has(T)).join(", ")}`);let y=[[]];for(let T of v)y=y.flatMap(O=>T.map(D=>[...O,D]));let E=T=>JSON.stringify(g.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 A=y.length,k=new Map;c.forEach((T,O)=>{let D=k.get(T);D===void 0&&k.set(T,D={s:y.map(()=>[0,0]),n:y.map(()=>0)});let $=M.get(E(O));D.s[$]=B(D.s[$],[f[O],0]),D.n[$]++}),[...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=w.filter(T=>k.get(T).n.some(O=>O===0));if(S.length>0){let T=[...S].sort(Ze),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:
6
+ ${O}`)}let L=w.filter(T=>!S.includes(T)),q=new Map;c.forEach((T,O)=>q.has(T)||q.set(T,O));let R=L.map(T=>{let O=k.get(T);return O.s.map((D,$)=>Xt(D,O.n[$]))}),V={};n.forEach((T,O)=>V[T]=L.map(D=>d[O][q.get(D)]));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(A===1)return z();return J();function z(){let T=n.includes("y")?"y_":"y",O={...V,[T]:R.map(ne=>ne[0][0])},D=Cr(`${T} ~ ${n.length>0?n.join(" * "):"1"}`,O,{contrasts:F,levels:I});if(D.rank<D.coefNames.length)throw new Error(C);let $=Qr(D,{type:o});s.push(...$.warnings.filter(ne=>!s.includes(ne)));let G=D.dfResidual,Q=$.rowNames.flatMap((ne,de)=>{if(ne==="(Intercept)"||ne==="Residuals")return[];let[be,Fe,_e,Ie]=$.table[de];return[{name:ne,ss:be,numDf:Fe,errSS:D.rss,denDf:G,f:_e,p:Ie}]});return{anova:ta(Q,[D.rss],i,l),univariate:{colNames:$.colNames,rowNames:$.rowNames,table:$.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:[]},D=O.colNames.length;if(T===0||he(O.x).rank<D)throw new Error(C);let $=T-D,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]:[]),be=X=>O.x.map(Z=>[Z[X],0]),Fe=new Array(T).fill([0,0]),_e=(X,Z,U)=>{let{qtv:pe}=ze([...X,...Z].map(be),Fe,U);return pe.map(rt=>rt.slice(X.length,X.length+Z.length))},Ie=X=>{let{qtv:Z}=ze(O.colNames.map((U,pe)=>be(pe)),Fe,X);return Z.map(U=>U.slice(D))},We=Object.fromEntries(r.map((X,Z)=>[X,y.map(U=>U[Z])])),qe=Ft(`~ ${r.join(" * ")}`,We,{contrasts:Object.fromEntries(r.map(X=>[X,"contr.sum"])),levels:Object.fromEntries(r.map((X,Z)=>[X,v[Z]]))}),Mt=[0,...qe.termLabels.map((X,Z)=>Z+1)],tt=["(Intercept)",...qe.termLabels],Pt=X=>X.map(Z=>X.map(U=>{let pe=[0,0];return Z.forEach((rt,Ut)=>pe=B(pe,oe(rt,U[Ut]))),pe})),nt=[],jt=[],zt=[],pn=[],Wt=[],Gt=[],_=[],W=!1;for(let X of Mt){let Z=qe.assign.flatMap((Re,ce)=>Re===X?[ce]:[]),U=Z.length,pe=Z.map(Re=>R.map(ce=>ce.reduce((Ue,He,ko)=>B(Ue,Te(He,qe.x[ko][Re])),[0,0]))),rt=Z.map(Re=>Z.map(ce=>[qe.x.reduce((Ue,He)=>Ue+He[Re]*He[ce],0),0])),Ut=Ds(rt),nr=Pt(Ie(pe)),dn=sn(nr,Ut),gn=wt(dn)[0],Ge=NaN,wn=NaN,rr=NaN,xo=NaN,Eo=!1;if(U>=2&&mt(Cs(nr),{symmetric:!0,onlyValues:!0}).values.filter(ce=>ce>=oc).length===U){Eo=!0;let ce=wt(dn);Ge=ge(oe(ce,ce),Te(wt(sn(dn,dn)),U))[0],wn=(($+1)*U*Ge-2)/(U*($-U*Ge)),rr=Kr(sn(Ut,nr)),xo=Zr(rr,U,$,A)}for(let Re=0;Re<G.length;Re++){let ce=G[Re],Ue;if(o===3)Ue=_e(ne(G.filter(Qe=>Qe!==ce)),ne([ce]),pe);else if(ce===0)Ue=_e([],ne([0]),pe);else{let Qe=de(ce),bn=G.filter(De=>De!==0&&De!==ce&&Qe.every(Ta=>O.factors[Ta][De-1]>0));Ue=_e(ne(G.filter(De=>De!==ce&&!bn.includes(De))),ne([ce]),pe)}let He=wt(sn(Pt(Ue),Ut))[0],Be=(ce===0?1:ne([ce]).length)*U,Xe=$*U,Ht=He/Be/(gn/Xe),Ao=ae(Ht,Be,Xe,!1,!1),or=X===0?Q[Re]:ce===0?tt[X]:`${Q[Re]}:${tt[X]}`;nt.push([He,Be,gn,Xe,Ht,Ao]),jt.push(or),_.push(gn);let Bt={name:or,ss:He,numDf:Be,errSS:gn,denDf:Xe,f:Ht,p:Ao};if(Eo){let Qe=Math.min(1,wn),bn=ae(Ht,Ge*Be,Ge*Xe,!1,!1),De=ae(Ht,Qe*Be,Qe*Xe,!1,!1);zt.push([Ge,bn,wn,De]),pn.push([rr,xo]),Wt.push(or),a==="GG"?Bt={...Bt,numDf:Ge*Be,denDf:Ge*Xe,p:bn}:a==="HF"&&(wn>1&&(W=!0),Bt={...Bt,numDf:Qe*Be,denDf:Qe*Xe,p:De})}X===0&&ce===0||Gt.push(Bt)}}W&&s.push("HF eps > 1 treated as 1");let K={anova:ta(Gt,_,i,l),univariate:{colNames:["Sum Sq","num Df","Error SS","den Df","F value","Pr(>F)"],rowNames:jt,table:nt},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:pn}),K}}var sa=Math.log(2*Math.PI),na=2e-14,ra=500;function ic(e){return Math.exp(-.5*e*e-.5*sa)}function lc(e){return P(e,{logP:!0})}function aa(e,t=-1/0){return(t===-1/0?fr(e,0,2,0,na,ra):fr(e,t,1,0,na,ra)).result}function oa(e,t,n){let r=t.map(Math.sqrt),o=t.map(i=>Math.sqrt(1-i));return aa(i=>{let l=0;for(let s=0;s<r.length;s++){let c=(r[s]*i+e)/o[s];if(!n){l+=lc(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 ic(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,oa(e,t,r));let o=n/2,a=Math.log(2)+.5*Math.log(o)-.5*sa-To(o);return Math.min(1,aa(l=>{let s=a-o*(Math.expm1(2*l)-2*l);return s<-745?0:Math.exp(s)*oa(e*Math.exp(l),t,r)}))}var ia="Ties are present. z-quantiles were corrected for ties.";function $e(e,t){let{labels:n,groups:r}=ht(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 Oe(t))a+=i*i*i-i;return{R:r,C:a/(12*(e.N-1))}}function cn(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 cc(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=St(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 Qn(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 Xn=e=>P(e,{lowerTail:!1}),so=(e,t,n)=>Nn(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 fc(e,t,n={}){let r=n.pAdjustMethod??"holm",o=$e(e,t),{R:a,C:i}=ro(o),l=o.N*(o.N+1)/12-i,s=cn(o.k,(f,m)=>Math.abs(a[f]-a[m])/Math.sqrt(l*(1/o.n[f]+1/o.n[m]))),c=cc(s.map(f=>f.map(m=>2*Xn(m))),r);return Qn(o,"Dunn's all-pairs test",s,c,r,i!==0?[ia]:[])}function mc(e,t,n={}){let r=te(n.alternative),o=n.pAdjustMethod??"single-step",a=$e(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=la(f,m,r,o);return oo(a,"Dunn's many-to-one test",f,u,o,l!==0?[ia]:[])}function la(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)):St(e.map(o=>n==="two.sided"?2*Xn(Math.abs(o)):Xn(n==="greater"?o:-o)),r)}function uc(e,t,n={}){let r=te(n.alternative),o=$e(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
7
+ comparisons with one control`,f,u,"single-step")}function hc(e,t,n={}){let r=te(n.alternative),o=n.pAdjustMethod??"single-step",a=$e(e,t);if(o==="single-step"&&a.k<3)throw new RangeError(`for 'p.adjust.method = single-step' at least
8
+
9
+ 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 g=i+u,v=g*(g+1)/2-d,w=i*u+i*(i+1)/2-d,b=i*u+u*(u+1)/2-v,N=0;for(let M of Oe(h))N+=M*M*M-M;let y=i*u/(g*(g-1))*((g*g*g-g)/12-N/12),E=(Math.min(w,b)-i*u/2)/Math.sqrt(y);return d<v?-E:E}),c=l.map(m=>a.n[m]/(a.n[m]+i)),f=la(s,c,r,o);return oo(a,`Wilcoxon, Mann, Whittney U-test
10
+ for multiple comparisons with one control`,s,f,o)}function pc(e,t){let n=$e(e,t),r=cn(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 v=0;v<l;v++)m+=f[v];let u=m-l*(l+1)/2,h=Math.min(u,l*s-u),p=l+s,d=0;for(let v of Oe(c))d+=v*v*v-v;let g=l*s/(p*(p-1))*((p*p*p-p)/12-d/12);return Math.SQRT2*(h-l*s/2)/Math.sqrt(g)}),o=r.map(a=>a.map(i=>Number.isNaN(i)?NaN:so(i,n.k,1/0)));return Qn(n,"Dwass-Steele-Critchlow-Fligner all-pairs test",r,o,"single-step")}function dc(e,t){let n=$e(e,t),r=n.groups.map(se),o=n.groups.map((l,s)=>re(l)/n.n[s]),a=cn(n.k,(l,s)=>(r[l]-r[s])/Math.sqrt(o[l]+o[s])*Math.SQRT2),i=cn(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 Qn(n,"Games-Howell test",a,i,"none")}function gc(e,t){let n=$e(e,t),{m:r,s2:o,df:a}=ao(n),i=cn(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 Qn(n,"Tukey's test",i,l,"single-step")}function wc(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=$e(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 bc(e,t,n={}){let r=n.method??"none",o=n.altp??!1,a=$e(e,t),i=Er(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)*Xn(Math.abs(h)));return{chi2:i,Z:f,P:u,PAdjusted:vc(u,r),comparisons:m}}function vc(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 Nc(e,t,n={}){let r=te(n.alternative),o=$e(e,t),{m:a,s2:i,df:l}=ao(o),s=Math.sqrt(i),c=o.n.slice(1).map((d,g)=>g+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,g)=>d/m[g]),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 yc=e=>e==null||typeof e=="number"&&Number.isNaN(e);function ca(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&&yc(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 Mc(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}=ca(e,t,n),l=i.length,s=new Array(l).fill(0);for(let w of a)s[w.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 w=0;w<a.length;){let b=a[w].time,N=w,y=new Array(l).fill(0),E=new Array(l).fill(0),M=0;for(;N<a.length&&a[N].time===b;){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 A=0;A<l;A++)c[A]+=x*y[A],f[A]+=x*M*u[A]/h;if(h>1){let A=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]+=A*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-w,w=N}let d=f.map((w,b)=>w>0?b:-1).filter(w=>w>=0),g=0;if(d.length>=2){let w=d.slice(1),b=w.map(y=>c[y]-f[y]),N=ve(w.map(y=>w.map(E=>m[y][E])),b);g=b.reduce((y,E,M)=>y+E*N[M],0)}let v=Pe(g,d.length-1,{lowerTail:!1});return{n:s,groups:i,obs:c,exp:f,var:m,chisq:g,pvalue:v}}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 xc(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}=ca(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,g=1,v=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&&(g*=1-M/d,v+=M<d?M/(d*(d-M)):1/0);let A=Math.sqrt(v),k,S;if(o==="log")k=g===0?NaN:g*Math.exp(-c*A),S=g===0?NaN:Math.min(1,g*Math.exp(c*A));else if(o==="plain"){let L=c*A*g;k=Number.isNaN(L)?NaN:Math.max(0,g-L),S=Number.isNaN(L)?NaN:Math.min(1,g+L)}else if(g===0||g===1)k=NaN,S=NaN;else{let L=Math.log(g),q=Math.log(-L);k=Math.exp(-Math.exp(q-c*A/L)),S=Math.exp(-Math.exp(q+c*A/L))}f.strata.push(m),f.time.push(y),f.nRisk.push(d),f.nEvent.push(M),f.nCensor.push(x),f.surv.push(g),f.stdErr.push(g*A),f.lower.push(k),f.upper.push(S),d-=E-N,N=E}let w=f.time.slice(p),b=f.nEvent.slice(p);f.median.push([io(w,b,f.surv.slice(p)),io(w,b,f.lower.slice(p)),io(w,b,f.upper.slice(p))])}),f}var Ec=e=>e==null||typeof e=="number"&&Number.isNaN(e),kc=(e,t)=>typeof e=="number"&&typeof t=="number"?e-t:String(e)<String(t)?-1:String(e)>String(t)?1:0;function Ac(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,g=f(),v=m(),w=f(),b=m();for(let N=s-1;N>=0;){l[N]&&(d=0,g=f(),v=m());let y=r[N],E=0,M=0,x=0,A=N;do{let k=i[A],S=0;for(let R=0;R<c;R++)S+=t[R]*k[R];let L=a[A],q=Math.exp(S)*L;if(o[A]){E++,M+=L,x+=q,p+=L*S;for(let R=0;R<c;R++){u[R]+=L*k[R],w[R]+=q*k[R];let V=b[R];for(let F=0;F<=R;F++)V[F]+=q*k[R]*k[F]}}else{d+=q;for(let R=0;R<c;R++){g[R]+=q*k[R];let V=v[R];for(let F=0;F<=R;F++)V[F]+=q*k[R]*k[F]}}A--}while(A>=0&&r[A]===y&&!l[A]);if(E>0){let k=n?E:1,S=n?E:1,L=M/k;for(let q=0;q<k;q++){d+=n?x/S:x,p-=L*Math.log(d);for(let R=0;R<c;R++){g[R]+=n?w[R]/S:w[R];let V=g[R]/d;u[R]-=L*V;let F=v[R],I=b[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*g[z])/d}}w.fill(0);for(let q of b)q.fill(0)}N=A}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:Pe(e,t,{lowerTail:!1})});function qc(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 g=d===0?0:n[0].length;if(g<1)throw new RangeError("'x' must have at least one column");if(n.some(_=>_.length!==g))throw new RangeError("the rows of 'x' must have the same length");if(l!==void 0&&l.length!==g)throw new RangeError("'init' must have one value per column of 'x'");let v=[];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&&Ec(i[_]))continue;let W=t[_];if(W!==0&&W!==1)throw new RangeError("'status' must be 0 (censored) or 1 (event)");v.push(_)}let w=v.length;if(w===0)throw new RangeError("no observations without missing values");let b=v.map(_=>a===void 0?1:a[_]);if(b.some(_=>!(_>0)))throw new RangeError("Invalid weights, must be >0");if(b.some(_=>!Number.isInteger(_)))throw new RangeError("non-integer weights (robust variance) are not supported yet");let N=0;for(let _ of v)N+=t[_];let y=Ac(v.map(_=>e[_])),E=new Array(w).fill(0);if(i!==void 0){let _=Array.from(new Set(v.map(K=>i[K]))).sort(kc),W=new Map;_.forEach((K,X)=>W.set(K,X)),E=v.map(K=>W.get(i[K]))}let M=Array.from({length:w},(_,W)=>W).sort((_,W)=>E[_]-E[W]||y[_]-y[W]||_-W),x=0;for(let _ of b)x+=_;let A=M.map(_=>b[_]),k=M.map(_=>n[v[_]].slice()),S=new Array(g).fill(0),L=new Array(g).fill(1),q=new Set(c);for(let _=0;_<g;_++){if(k.every(U=>q.has(U[_])))continue;let W=0;for(let U=0;U<w;U++)W+=A[U]*k[U][_];let K=W/x,X=0;for(let U=0;U<w;U++)k[U][_]-=K,X+=A[U]*Math.abs(k[U][_]);let Z=X===0?1:x/X;for(let U=0;U<w;U++)k[U][_]*=Z;S[_]=K,L[_]=Z}let R={time:M.map(_=>y[_]),event:M.map(_=>t[v[_]]===1),weight:A,x:k,stratumEnd:M.map((_,W)=>W===w-1||E[M[W+1]]!==E[_])},V=o==="efron",F=l===void 0?new Array(g).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;_<g;_++)O+=I.score[_]*T[_];let D=0,$,G=!1;if(u===0||!Number.isFinite(I.loglik))$=mo(z);else{let _=I.loglik,W=F.map((Z,U)=>Z+T[U]),K=0,X=!1;for(D=1;D<=u;D++){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?$=mo(z):(D=u+1,G=!0,u>1?(I=lo(R,F,V),C[1]=I.loglik,$=I.info.map((Z,U)=>Z.map((pe,rt)=>U===rt?Z[U]>0?1/Z[U]:0:-0))):$=mo(z))}let Q=F.map((_,W)=>_*L[W]),ne=I.score.map((_,W)=>_/L[W]),de=$.map((_,W)=>_.map((K,X)=>K*L[W]*L[X])),be=Q.slice();if(u>0&&Math.abs(J)<g)for(let _=0;_<g;_++)de[_][_]===0&&(be[_]=NaN);let Fe=0;for(let _=0;_<g;_++)Fe+=Q[_]*S[_];let _e=v.map(_=>{let W=0;for(let K=0;K<g;K++)W+=n[_][K]*Q[K];return W-Fe}),Ie=new Ne;if(u>1){let _=de.map((W,K)=>{let X=0;for(let Z=0;Z<g;Z++)X+=ne[Z]*de[Z][K];return Math.abs(X)});if(G)Ie.add("Ran out of iterations and did not converge"),(Math.max(..._e)>500||_.some(W=>!Number.isFinite(W)))&&Ie.add("one or more coefficients may be infinite");else{let W=[];for(let K=0;K<g;K++){let X=_[K];(!Number.isFinite(ne[K])||X>m&&X>p*Math.abs(Q[K]))&&W.push(K+1)}W.length>0&&Ie.add(`Loglik converged before variable ${W.join(",")} ; coefficient may be infinite. `)}}let We=be.map((_,W)=>Number.isNaN(_)?-1:W).filter(_=>_>=0),qe=We.length,Mt=We.map(_=>be[_]-(l===void 0?0:l[_])),tt=0;if(qe===1)tt=Mt[0]*Mt[0]/de[We[0]][We[0]];else if(qe>1){let _=We.map(K=>We.map(X=>de[K][X]));co(_,h);let W=fo(_,Mt);for(let K=0;K<qe;K++)tt+=Mt[K]*W[K]}let Pt=H((1+f)/2),nt=de.map((_,W)=>Math.sqrt(_[W])),jt=be.map((_,W)=>_/nt[W]),zt=jt.map(_=>Pe(_*_,1,{lowerTail:!1})),pn=be.map((_,W)=>Math.exp(_-Pt*nt[W])),Wt=be.map((_,W)=>Math.exp(_+Pt*nt[W])),Gt=-2*(C[0]-C[1]);return{coefficients:be,var:de,loglik:C,score:O,wald:tt,iter:D,means:S,n:w,nevent:N,linearPredictors:_e,se:nt,z:jt,p:zt,lower:pn,upper:Wt,logtest:uo(Gt,qe),sctest:uo(O,qe),waldtest:uo(tt,qe),rsq:{rsq:1-Math.exp(-Gt/w),maxrsq:1-Math.exp(2*C[0]/w)},warnings:Ie.list}}function Rc(e,t,n={}){let r=ie(n.center??"median",["median","mean"]),{groups:o}=ht(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"?_n(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 Tc(e,t,n){let r=Array.from(e),o=Array.from(t),a=r.length,i=n===void 0?r.map((g,v)=>v+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((g,v)=>g+v,0)/o.reduce((g,v)=>g+v,0),s=o.map(g=>g/(l*(1-l))),c=r.map((g,v)=>g/o[v]),f=s.reduce((g,v)=>g+v,0),m=s.reduce((g,v,w)=>g+v*i[w],0)/f,u=s.reduce((g,v,w)=>g+v*c[w],0)/f,h=0,p=0;s.forEach((g,v)=>{let w=i[v]-m;h+=g*w*(c[v]-u),p+=g*w*w});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 Sc=`Sample size > 100 or data with ties
11
+ p-value based on normal approximation. Specify nperm for permutation p-value`,Lc=`Sample data with ties
12
+ p-value based on normal approximation. Specify nperm for permutation p-value.`;function Ic(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(Ze)}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 Oc(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=Ic(e,t,n.levels),i=a.flat(),l=i.length,s=new Set(i).size<l,c=0,f=[],m=l;a.forEach((w,b)=>{m-=w.length,f.push([w.length,m]);for(let N=b+1;N<a.length;N++)for(let y of w)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(Lc))):h||u.push(Sc);let p,d;if(h){let w=[1];for(let[b,N]of f){if(b===0||N===0)continue;let y=Array.from({length:b*N+1},(M,x)=>Vo(x,b,N,!1)),E=new Array(w.length+y.length-1).fill(0);w.forEach((M,x)=>y.forEach((A,k)=>E[x+k]+=M*A)),w=E}p=0,d=0,w.forEach((b,N)=>{N<=c&&(p+=b),N>=c&&(d+=b)})}else{let w=f.reduce((y,[E,M])=>y+E*M,0)/2,b=f.reduce((y,[E,M])=>y+E*M*(E+M+1)/12,0),N=(c-w)/Math.sqrt(b);p=P(N),d=1-p}let v=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:v,warnings:u}}function fn(e){return Ae(Array.from(e)).sort((t,n)=>t-n)}function Kn(e){let t=se(e),n=Math.sqrt(re(e));return e.map(r=>(r-t)/n)}function Fc(e){let t=fn(e),n=t.length;if(n<8)throw new RangeError("sample size must be greater than 7");let r=Kn(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 _c(e){let t=fn(e),n=t.length;if(n<8)throw new RangeError("sample size must be greater than 7");let r=Kn(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 Cc(e){let t=fn(e),n=t.length;if(n<5)throw new RangeError("sample size must be greater than 4");let r=Kn(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 Vc(e){let t=fn(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=fn(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 Kn(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 Zn(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 Yn(e,t){if(e.some(n=>n.some(Number.isNaN)))throw new RangeError(`'${t}' contains missing values`)}var ma=e=>2*P(-Math.abs(e));function Dc(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=>ut(M[0])),i=r.map(M=>ut(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},(A,k)=>m(Math.abs(x-k)))),h=f.map(M=>ee(M)/o),p=Array.from({length:s},(M,x)=>ee(f.map(A=>A[x]))/o),d=M=>{let x=[];for(let A=0;A<s;A++)for(let k=0;k<s;k++)x.push(M(A,k));return ee(x)},g=d((M,x)=>f[M][x]*u[M][x])/o,v=d((M,x)=>h[M]*p[x]*u[M][x]),w=(g-v)/(1-v),b=Array.from({length:s},(M,x)=>ee(p.map((A,k)=>A*u[x][k]))),N=Array.from({length:s},(M,x)=>ee(h.map((A,k)=>A*u[k][x]))),y=(d((M,x)=>h[M]*p[x]*(u[M][x]-(b[M]+N[x]))**2)-v*v)/(o*(1-v)**2),E=w/Math.sqrt(y);return{value:w,statistic:E,pValue:ma(E)}}function Pc(e,t={}){let n=t.exact??!1,r=Zn(e,"ratings");Yn(e,"ratings");let o=e.length,a=r.length,i=[...new Set(e.flat())].sort((w,b)=>w-b),l=new Map(i.map((w,b)=>[w,b])),s=i.length,c=e.map(w=>{let b=new Array(s).fill(0);for(let N of w)b[l.get(N)]++;return b}),f=ee(c.map(w=>(ee(w.map(b=>b*b))-a)/(a*(a-1))))/o,m=Array.from({length:s},(w,b)=>ee(c.map(N=>N[b]))/(o*a)),u=ee(m.map(w=>w*w));if(n){let w=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)}),b=Array.from({length:s},(N,y)=>re(w.map(E=>E[y]))*(a-1)/a);u-=ee(b)/(a-1)}let h=(f-u)/(1-u);if(n)return{value:h};let p=ee(m.map(w=>w*(1-w))),d=ee(m.map(w=>w*(1-w)*(1-w-w))),g=2/(p*p*o*a*(a-1))*(p*p-d),v=h/Math.sqrt(g);return{value:h,statistic:v,pValue:ma(v)}}function jc(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=Zn(e,"ratings");Yn(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)),g=(C,z,J)=>ae(C,z,J,!1,!1),v=(C,z)=>at(1-f/2,C,z,!0,!1),w=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/v(w,z),O=J*v(z,w),[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:D,Fvalue:$,df1:w,df2:z,pValue:g($,w,z),lbound:G(T),ubound:G(O)}}let b=o==="single"?c:1,N=C=>[b*C/(s*(1-C)),1+b*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),A=m/(M*u+x*d),k=y(M,x),[S,L]=[c*E/(s*(1-E)),1+c*E*(s-1)/(s*(1-E))],q=y(S,L),R=v(w,q),V=v(q,w),[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:A,df1:w,df2:k,pValue:g(A,w,k),lbound:F,ubound:I}}function fa(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 zc(e){let t=Zn(e,"x").length;if(Yn(e,"x"),t<2)throw new RangeError("'x' needs at least two items");let n=qn(e),r=Jt(e),o=Array.from({length:t},(s,c)=>c),a=ee(r.flat()),i=ve(r),l=o.map(s=>1-1/i[s][s]);return{rawAlpha:fa(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=>fa(n,o.filter(c=>c!==s)))}}function Wc(e,t,n){let r=Zn(e,"x").length;Yn(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=Jt(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=ve(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]=ot(t,r);t=o,n+=a}return t+n}var ua=(e,t)=>e[0].map((n,r)=>ue(e.map(o=>t(o[r]))));function da(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 ha(e,t,n){let r=mt(da(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 pa(e,t){let n=ua(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=ua(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 Nt=(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 Jn(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=Nt(o,i),m=f[0].map((g,v)=>{let w=0;for(let b=0;b<n;b++)w+=f[b][v]*f[b][v];return w}),u=f.map(g=>g.map((v,w)=>v**3-v*m[w]/n)),h=Nt(a,u),p=kn(h);i=Nt(p.u,ho(p.v));let d=l;if(l=ue(p.d),l<d*(1+1e-5))break}return{loadings:Nt(o,i).map((c,f)=>c.map(m=>m*r[f])),rotmat:i}}function Gc(e){let t=Jn(e),n=t.loadings,r=n.map(l=>l.map(s=>s*Math.abs(s)**3)),o=Tt(he(n),r),a=ve(Nt(ho(o),o)).map((l,s)=>l[s]),i=o.map(l=>l.map((s,c)=>s*Math.sqrt(a[c])));return{loadings:Nt(n,i),rotmat:Nt(t.rotmat,i)}}function Uc(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=qn(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,A)=>x/(s[M]*s[A]))),f=n.control?.lower??.005,m=n.control?.start?.slice()??ve(c).map((E,M)=>(1-i/(2*a))/E[M]),p=Rn(m,E=>{let M=mt(da(c,E),{symmetric:!0,onlyValues:!0}).values.slice(i);return-(ue(M.map(x=>Math.log(x)-x))-i+a)},{gr:E=>{let M=ha(c,E,i);return E.map((x,A)=>{let k=0;for(let S=0;S<i;S++)k+=M[A][S]*M[A][S];return(k+x-c[A][A])/(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,g=pa(ha(c,d,i)).L,v,w=n.rotation??"varimax";if(w!=="none"&&i>=2){let E=w==="varimax"?Jn(g):Gc(g),M=pa(E.loadings,E.rotmat);g=M.L,v=M.rot}else if(w!=="varimax"&&w!=="promax"&&w!=="none")throw new RangeError(`unknown rotation '${String(w)}'`);let b=NaN,N=NaN;!Number.isNaN(o)&&l>0&&(b=(o-1-(2*a+5)/6-2*i/3)*p.value,N=Pe(b,l,{lowerTail:!1}));let y={uniquenesses:d,loadings:g,STATISTIC:b,dof:l,PVAL:N,converged:!0,objective:p.value,counts:p.counts};return v!==void 0&&(y.rotmat=v),y}var wo=2**-52,Hc=`
13
+ Three factors are required for identification -- general factor loadings set to be equal.
14
+ Proceed with caution.
15
+ Think about redoing the analysis with alternative values of the 'option' setting.
16
+
17
+ `,mn=(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})),un=e=>e[0].map((t,n)=>e.map(r=>r[n])),hn=(e,t=n=>n)=>e[0].map((n,r)=>ue(e.map(o=>t(o[r])))),wa=(e,t=n=>n)=>e.map(n=>ue(n.map(t))),Dt=(e,t=n=>n)=>ue(e[0].flatMap((n,r)=>e.map(o=>t(o[r])))),er=e=>e<0?-1:1;function bo(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 ba(e,t){let n=bo(e),{d:r,u:o,v:a}=kn(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)&&yt(t,"In smc, smcs > 1 were set to 1.0"),l.some(s=>s<0)&&yt(t,"In smc, smcs < 0 were set to .0"),l.map(s=>s>1?1:s<0?0:s)}var yt=(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 Bc=e=>mn(e,un(e));function ga(e,t,n,r){let o=bo(e),a=o.length,i=ba(o,r),l=u=>{let h=po(o,u.map(g=>1-g),t,g=>g<wo?100*wo:g),p=Bc(h),d=[];for(let g=0;g<a;g++)for(let v=g+1;v<a;v++)d.push((o[v][g]-p[v][g])**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 g=0;for(let v=0;v<t;v++)g+=h[d][v]*h[d][v];return g+p-o[d][d]})},f=Rn(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)),wa(m,u=>u*u).some(u=>u>1)&&yt(n,"An ultra-Heywood case was detected. Examine the results carefully"),t>1){m=go(Jn(m).loadings,t);let u=hn(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=hn(e).map(er);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 Xc(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:Dt(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 Qc(e){let t=e[0].length,n=c=>{let f=ve(c),m=mn(e,un(f)),{f:u,Gq:h}=Xc(m),p=un(mn(mn(un(m),h),f)).map(d=>d.map(g=>-g));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=hn(r.map((g,v)=>g.map((w,b)=>w*a.G[v][b]))),m=a.G.map((g,v)=>g.map((w,b)=>w-r[v][b]*f[b])),u=Math.sqrt(Dt(m,g=>g*g));if(l.push(a.f),u<1e-5){s=!0;break}if(i!==void 0){let g=i,v=r.map((b,N)=>b.map((y,E)=>y-g.T[N][E])),w=m.map((b,N)=>b.map((y,E)=>y-g.Gp[N][E]));Dt(w,b=>b*b)>0&&(o=Dt(v,b=>b*b)/Math.abs(Dt(v.map((b,N)=>b.map((y,E)=>y*w[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 g=0;g<=10;g++){let v=r.map((b,N)=>b.map((y,E)=>y-o*m[N][E])),w=hn(v,b=>b*b).map(b=>1/Math.sqrt(b));if(d=v.map(b=>b.map((N,y)=>N*w[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:mn(un(r),r),converged:s}}function Kc(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=bo(t.covmat);else{if(e===null)throw new TypeError("neither 'x' nor 'covmat' supplied");r=Jt(e,void 0,{use:"pairwise.complete.obs"})}let o=r.length,a=[],i=[],l=ga(r,n,a,i),s,c,f;if(n===1)s=l.map(T=>T.slice()),f=[1],yt(i,`Omega_h for 1 factor is not meaningful, just omega_t
18
+ `),yt(a,"Omega_h and Omega_asymptotic are not meaningful with one factor");else{let T=Qc(l);if(T.converged||yt(a,"oblimin did not converge in 2000 iterations"),s=T.loadings,c=T.phi,n===2){let O=c[0][1],D=Math.sqrt(Math.abs(O));f=[D,er(O)*D],yt(i,Hc)}else f=ga(c,1,a,i).map(O=>O[0])}let m=s.map(T=>{let O=0;for(let D=0;D<n;D++)O+=T[D]*f[D];return O}),u=wa(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-D*D)-(D*f[$])**2;return Q<0&&(Q=0),er(D)*Math.sqrt(Q)})),d=m.map((T,O)=>T*T/u[O]),g=m.map((T,O)=>{let D=[T,...p[O]];return ue(D.map($=>$*$))**2/ue(D.map($=>$**4))}),v=m.map(er);v.some(T=>T<0)&&(r=r.map((T,O)=>T.map((D,$)=>v[O]*D*v[$])));let w=m.map((T,O)=>v[O]*T),b=p.map((T,O)=>T.map(D=>v[O]*D)),N=w.map((T,O)=>[T,...b[O],u[O],h[O],d[O],g[O]]),y=Dt(r),E=ue(r.map((T,O)=>T[O])),M=ue(w)**2,x=ue(h),A=M/y,k=(y-x)/y,S=M/(y-x),L=(y-E)/y*o/(o-1),q=(y+ue(ba(r,i))-E)/y,R=hn(N.map(T=>T.slice(0,n+1)),T=>T*T),V=R[0]/ue(R),F=b.map(T=>{let O=0;for(let D=1;D<n;D++)Math.abs(T[D])>Math.abs(T[O])&&(O=D);return O}),I=[],C=[];for(let T=0;T<n;T++){let O=F.map((Q,ne)=>ne).filter(Q=>F[Q]===T),D=ue(O.flatMap(Q=>O.map(ne=>r[ne][Q]))),$=ue(O.map(Q=>b[Q][T])),G=ue(O.map(Q=>w[Q]));C.push($*$),I.push([($*$+G*G)/D,G*G/D,$*$/D])}let z=[[k,ue(w)**2/y,ue(C)/y],...I],J={omegaH:A,omegaLim:S,alpha:L,omegaTot:k,G6:q,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:v,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),va=(e,t)=>e===1?`1 ${t} with zero marginal removed`:`${e} ${t}s with zero marginals removed`,Na=e=>Array.isArray(e)||ArrayBuffer.isView(e),ya=(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 Zc(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 tr=e=>Array.from({length:e},()=>new Array(e).fill(0));function Ea(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 vo={r:0,h:0,k:0,rr:0,rh:0,rk:0,hh:0,hk:0,kk:0};function Yc(e,t,n){if(e===-1/0||t===-1/0||e===1/0&&t===1/0)return vo;if(e===1/0)return{...vo,k:ye(t),kk:-t*ye(t)};if(t===1/0)return{...vo,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 Ma(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:tr(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=>Yc(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 g=Zt([t[h],n[p]],[t[h+1],n[p+1]],r),v=new Array(l).fill(0),w=tr(l);for(let[b,N,y]of[[h+1,p+1,1],[h,p+1,-1],[h+1,p,-1],[h,p,1]]){let E=m[b][N],M=c(b),x=f(N);v[0]+=y*E.r,w[0][0]+=y*E.rr,M>=0&&(v[M]+=y*E.h,w[0][M]+=y*E.rh,w[M][0]+=y*E.rh,w[M][M]+=y*E.hh),x>=0&&(v[x]+=y*E.k,w[0][x]+=y*E.rk,w[x][0]+=y*E.rk,w[x][x]+=y*E.kk),M>=0&&x>=0&&(w[M][x]+=y*E.hk,w[x][M]+=y*E.hk)}Ea(s,d,g,v,w),u.push(d*Math.log(g))}return s.value=ee(u),s}function Jc(e,t,n,r,o){let a=o?n.length+1:1,i={value:0,gradient:new Array(a).fill(0),hessian:tr(a)},l=(1-r)*(1+r),s=Math.sqrt(l),c=l*s,f=new Array(a),m=tr(a),u=[],h=(p,d,g)=>{let v=n[p],w=(v-r*d)/s,b=ye(w),N=(r*v-d)/c,y=1/s,E=v/c+3*r*(r*v-d)/(c*l),M=r/c;if(f[0]+=g*b*N,m[0][0]+=g*b*(E-w*N*N),o){let x=p+1;f[x]+=g*b*y;let A=g*b*(M-w*N*y);m[0][x]+=A,m[x][0]+=A,m[x][x]+=g*b*(-w*y*y)}};for(let p=0;p<e.length;p++){let d=e[p],g=t[p];f.fill(0);for(let b of m)b.fill(0);let v=g<n.length?(n[g]-r*d)/s:1/0,w=g>0?(n[g-1]-r*d)/s:-1/0;g<n.length&&h(g,d,1),g>0&&h(g-1,d,-1),Ea(i,1,ya(w,v),f,m),u.push(Math.log(ya(w,v)),ye(d,{log:!0}))}return i.value=ee(u),i}function ef(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=ve(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 ka=(e,t)=>e>=t-1e-12*Math.abs(t);function xa(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=ef(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,g)=>d+f*c[g]);f*c[0]!==0&&Math.abs(h[0])>=ke*(1-1e-15)&&(h[0]=Math.sign(h[0])*ke);let p=e(h);if(ka(p.value,o.value)){let d=Math.max(...h.map((g,v)=>Math.abs(g-r[v])/Math.max(1,Math.abs(r[v]))));r=h,o=p,m=d>1e-12;break}f/=2}if(!m)break}return{theta:r,at:o,bounded:a}}function Aa(e,t){let n=xa(e,[Math.max(-ke,Math.min(ke,t[0])),...t.slice(1)],!1);if(n.bounded||n.theta[0]===0)return n;let r=xa(e,[Math.sign(n.theta[0])*ke,...n.theta.slice(1)],!0);return ka(r.at.value,n.at.value)?r:n}function qa(e,t){let n=()=>e.map(o=>o.map(()=>1/0));if(t)return n();let r;try{r=ve(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 Ra=e=>`inadmissible correlation set to ${e>0?"0.9999":"-0.9999"}`;function tf(e,t,n){let r,o;if(Na(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(Na))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(va(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(va(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],v=Go(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:v,warnings:l};let w=R=>[-1/0,...R,1/0],b=p.slice(1,u),N=d.slice(1,h),y=R=>a?Ma(r,w(R.slice(1,u)),w(R.slice(u)),R[0],!0):Ma(r,p,d,R[0],!1),{theta:E,at:M,bounded:x}=Aa(y,a?[v,...b,...N]:[v]),A=E[0];if(x&&l.push(Ra(A)),!i)return{rho:A,warnings:l};let k=a?w(E.slice(1,u)):p,S=a?w(E.slice(u)):d,L=ee(r.map(R=>ee(R))),q=[];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]],A);F===0?q.push(L*I):q.push(F*Math.log1p(1e-6/F),F*Zc(L*I/F-1))}return{rho:A,warnings:l,var:qa(M.hessian,x),chisq:2*ee(q),df:u*h-u-h,n:L,rowCuts:k.slice(1,u),colCuts:S.slice(1,h)}}function nf(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,g)=>{if(d===0)return;let v=Zt([f[p],m[g]],[f[p+1],m[g+1]],r);u.push(d*Math.log((d+1e-6)/(v*o)))})),2*ee(u)}function rf(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,A)=>!Number.isNaN(x)&&!Number.isNaN(t[A])),i=e.filter((x,A)=>a[A]),l=t.filter((x,A)=>a[A]),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,A)=>[x,A+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))*Uo(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, ${ut(d)}, set to ${ut(x)}`),d=x}if(!r&&!o)return{rho:d,warnings:p};let g=Mo(i),v=m.map(x=>x-1),w=x=>Jc(g,v,r?x.slice(1):h,x[0],r),{theta:b,at:N,bounded:y}=Aa(w,r?[d,...h]:[d]);if(y&&p.push(Ra(b[0])),!o)return{rho:b[0],warnings:p};let E=r?b.slice(1):h,M=c.length;return{rho:b[0],warnings:p,var:qa(N.hessian,y),chisq:nf(m,g,E,r?b[0]:d),df:4*M-M-1,n:s,cuts:E}}export{Qr as Anova,rl as TukeyHSD,Fc as adTest,Br as anova,Oi as ansariTest,Vr as aov,ac as aovEz,Xr as aovSummary,li as bartlettTest,Nr as binomTest,Vi as brunnerMunzelTest,La as chisqTest,ci as cochranQTest,zs as cohenES,Bl as cohensD,Ji as confint,Sa as corTest,qc as coxph,zc as cronbachAlpha,_c as cvmTest,pc as dscfAllPairsTest,bc as dunnTest,uc as dunnettTest,rc as epsilonSquared,Sl as esH,Ll as esW1,Il as esW2,tc as etaSquared,Uc as factanal,Va as fisherTest,mi as flignerTest,is as friedmanTest,dc as gamesHowellTest,Nc as glhtDunnett,fl as glm,vl as glmAnova,gl as glmConfint,pl as glmCooksDistance,Wr as glmHatvalues,dl as glmPredict,zr as glmResiduals,hl as glmRstandard,jr as glmSummary,Xl as hedgesG,jc as icc,Oc as jonckheereTest,Dc as kappa2,Pc as kappamFleiss,Er as kruskalTest,Pi as ksTest,fc as kwAllPairsDunnTest,mc as kwManyOneDunnTest,Rc as leveneTest,Cc as lillieTest,Cr as lm,qi as mantelhaenTest,hc as manyOneUTest,kl as mauchlyTest,si as mcnemarTest,Ft as modelMatrix,ui as moodTest,Kc as omega,nc as omegaSquared,xi as onewayTest,St as pAdjust,Wi as pairwisePropTest,ji as pairwiseTTest,zi as pairwiseWilcoxTest,Wc as partialR,$c as pearsonTest,oi as poissonTest,tf as polychor,rf as polyserial,Rl as powerAnovaTest,ql as powerPropTest,Al as powerTTest,el as predict,yr as propTest,Tc as propTrendTest,Pl as pwr2p2nTest,Vl as pwr2pTest,Wl as pwrAnovaTest,zl as pwrChisqTest,Gl as pwrF2Test,Dl as pwrNormTest,$l as pwrPTest,jl as pwrRTest,Cl as pwrT2nTest,_l as pwrTTest,fi as quadeTest,Vc as sfTest,Ei as shapiroTest,wc as steelTest,Mc as survdiff,xc as survfit,mr as tTest,gc as tukeyTest,ii as varTest,wr as wilcoxTest,ol as zTest};