@numero-uno-jpn/rstats 1.0.0 → 1.2.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 (79) hide show
  1. package/CHANGELOG.md +101 -0
  2. package/README.ja.md +12 -11
  3. package/README.md +12 -11
  4. package/THIRD_PARTY_NOTICES.md +3 -2
  5. package/dist/chunks/{chunk-UJ66VQIG.js → chunk-2NNBUYHJ.js} +2 -2
  6. package/dist/chunks/{chunk-YHNMBYSW.js → chunk-47IIPUFN.js} +2 -2
  7. package/dist/chunks/{chunk-WIBKS73G.js → chunk-4AOTAT3O.js} +2 -2
  8. package/dist/chunks/{chunk-4F2IALD4.js → chunk-5SGTWSCB.js} +3 -3
  9. package/dist/chunks/{chunk-F7YGX7PN.js → chunk-6FZHTD5G.js} +2 -2
  10. package/dist/chunks/{chunk-NSWENS3S.js → chunk-73LNRWU3.js} +1 -1
  11. package/dist/chunks/{chunk-BQSIBJVX.js → chunk-7INBXTVV.js} +2 -2
  12. package/dist/chunks/chunk-7WG7X2UI.js +2 -0
  13. package/dist/chunks/{chunk-H7I32ZZE.js → chunk-A66M3ZEA.js} +2 -2
  14. package/dist/chunks/{chunk-BWKHYRM6.js → chunk-BI7NYJWP.js} +1 -1
  15. package/dist/chunks/{chunk-QZK3CCBA.js → chunk-BKV3FINA.js} +2 -2
  16. package/dist/chunks/{chunk-QQX46TCI.js → chunk-CQB4BJBR.js} +2 -2
  17. package/dist/chunks/{chunk-3W5NFFAA.js → chunk-EEU7D4RU.js} +1 -1
  18. package/dist/chunks/{chunk-DCAWMF4K.js → chunk-F3EQLL4F.js} +2 -2
  19. package/dist/chunks/{chunk-IEOD5GWC.js → chunk-HBHMEPGY.js} +2 -2
  20. package/dist/chunks/{chunk-4OLDGML4.js → chunk-IAWXF73N.js} +2 -2
  21. package/dist/chunks/{chunk-CKV4JWW2.js → chunk-IMPRIQE5.js} +1 -1
  22. package/dist/chunks/chunk-JWBSEL3V.js +2 -0
  23. package/dist/chunks/{chunk-LROMWFAK.js → chunk-L23RSC2N.js} +2 -2
  24. package/dist/chunks/{chunk-UKNDX4BA.js → chunk-NSU6U3ET.js} +2 -2
  25. package/dist/chunks/{chunk-WQWZ7FND.js → chunk-ORE5ZRCY.js} +2 -2
  26. package/dist/chunks/{chunk-YG5S5AVP.js → chunk-RRO4LMCB.js} +2 -2
  27. package/dist/chunks/{chunk-6XJBCRBV.js → chunk-T5DRIITM.js} +2 -2
  28. package/dist/chunks/{chunk-63OPBEYO.js → chunk-TNETWBF5.js} +1 -1
  29. package/dist/chunks/{chunk-O2SQMGE3.js → chunk-VERBXW6E.js} +2 -2
  30. package/dist/descriptive.js +2 -2
  31. package/dist/distributions/signrank.js +2 -2
  32. package/dist/distributions.js +2 -2
  33. package/dist/entries/tests.d.ts +6 -1
  34. package/dist/entries/tests.js +5 -0
  35. package/dist/htest/ansariTest.js +32 -34
  36. package/dist/htest/binomCI.d.ts +116 -0
  37. package/dist/htest/binomCI.js +252 -0
  38. package/dist/htest/boschlooTest.d.ts +65 -0
  39. package/dist/htest/boschlooTest.js +394 -0
  40. package/dist/htest/chisqTest.d.ts +19 -0
  41. package/dist/htest/chisqTest.js +30 -2
  42. package/dist/htest/cor.d.ts +2 -2
  43. package/dist/htest/cor.js +2 -1
  44. package/dist/htest/core/scoreDistribution.d.ts +39 -0
  45. package/dist/htest/core/scoreDistribution.js +151 -0
  46. package/dist/htest/core/twoProportions.d.ts +48 -0
  47. package/dist/htest/core/twoProportions.js +476 -0
  48. package/dist/htest/equivalence.d.ts +172 -0
  49. package/dist/htest/equivalence.js +189 -0
  50. package/dist/htest/fisherTest.d.ts +49 -0
  51. package/dist/htest/fisherTest.js +136 -13
  52. package/dist/htest/tableTests.d.ts +216 -0
  53. package/dist/htest/tableTests.js +650 -0
  54. package/dist/htest/twoByTwo.d.ts +203 -0
  55. package/dist/htest/twoByTwo.js +385 -0
  56. package/dist/htest/wilcoxTest.d.ts +23 -8
  57. package/dist/htest/wilcoxTest.js +521 -199
  58. package/dist/linalg/chol.d.ts +3 -3
  59. package/dist/linalg/chol.js +10 -2
  60. package/dist/linalg/qr.js +39 -15
  61. package/dist/linalg.js +2 -2
  62. package/dist/mixed.js +2 -2
  63. package/dist/models/anovaSequential.d.ts +16 -1
  64. package/dist/models/anovaSequential.js +40 -5
  65. package/dist/models/aovStrata.js +21 -4
  66. package/dist/models/glm.d.ts +15 -4
  67. package/dist/models/glm.js +13 -6
  68. package/dist/models/survival.d.ts +4 -1
  69. package/dist/models/survival.js +7 -1
  70. package/dist/numerics.js +2 -2
  71. package/dist/rstats.js +16 -16
  72. package/dist/special/igam.js +140 -23
  73. package/dist/special/loader.js +8 -0
  74. package/dist/special.js +2 -2
  75. package/dist/tests.js +13 -13
  76. package/llms.txt +9 -1
  77. package/package.json +1 -1
  78. package/dist/chunks/chunk-KICBQGFN.js +0 -2
  79. package/dist/chunks/chunk-WWVMVZUL.js +0 -2
package/dist/tests.js CHANGED
@@ -1,19 +1,19 @@
1
- /*! rStats.js 1.0.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{B as kr,C as qr,D as _r,b as St,c as Xs,d as hn,f as xo,k as dn,l as aa,p as ia,q as gn,r as ca,v as Ar,w as la,x as Yn,z as fa}from"./chunks/chunk-WIBKS73G.js";import{a as Lr,b as Dt,c as Xn,d as ha,e as da,f as To,g as fe,h as Ct,i as Qt,j as Be,k as ba,l as nl,n as Cr,o as Qn}from"./chunks/chunk-YHNMBYSW.js";import"./chunks/chunk-DCAWMF4K.js";import{b as ga,c as wa,d as Bn,f as Zn}from"./chunks/chunk-4F2IALD4.js";import{b as Ro}from"./chunks/chunk-3W5NFFAA.js";import"./chunks/chunk-UKNDX4BA.js";import{a as Ce,b as bn,c as Nn}from"./chunks/chunk-BQSIBJVX.js";import{a as oa,d as sa}from"./chunks/chunk-6XJBCRBV.js";import{a as Zt,b as Mn,c as Fr,d as Pr,e as Dr,f as En,g as xa,h as Ra}from"./chunks/chunk-IEOD5GWC.js";import{A as un,g as vr,h as yr,j as Mr,k as mn,p as we,q as At,s as Gt,t as Pt,v as Er,z as Ht}from"./chunks/chunk-H7I32ZZE.js";import{C as Ma,E as Ea,H as ct,I as Ta,c as Na,h as va,j as ve,l as ya,m as vn,n as yn,t as Or,y as Ve}from"./chunks/chunk-KICBQGFN.js";import{a as Ir,b as ua,g as pa}from"./chunks/chunk-LROMWFAK.js";import{A as Sr,B as Xt,i as xe,l as Nr,x as Ke,y as gt}from"./chunks/chunk-YG5S5AVP.js";import{a as it}from"./chunks/chunk-4OLDGML4.js";import{a as tl,c as j,d as Re,e as Te,f as Gn}from"./chunks/chunk-NSWENS3S.js";import{a as Hn,b as wn,c as ma,d as Fe,e as Pe}from"./chunks/chunk-BWKHYRM6.js";import{a as ge,b as Bt,c as Je}from"./chunks/chunk-CKV4JWW2.js";import"./chunks/chunk-WQWZ7FND.js";import{b as Gs,c as Hs}from"./chunks/chunk-UJ66VQIG.js";import{A as Us,D as Ys,M as Bs,q as at,r as Y,s as Z}from"./chunks/chunk-WWVMVZUL.js";import{A as ea,C as ta,D as _e,E as xr,F as na,G as Rr,H as ra,I as se,J as Ne,K as ut,L as Ge,M as pn,N as Oe,O as Tr,P as ee,Q as Eo,b as Rt,c as Yt,d as Q,e as js,f as de,g as pe,h as st,i as Ae,j as Tt,k as Ks,l as C,m as Ft,p as jn,q as Kn,r as Qs,s as Zs,t as Js,u as _,v as De,w as Un,x as ke,y as qe}from"./chunks/chunk-63OPBEYO.js";var Sa=["alternative","mu","paired","varEqual","confLevel"];function xn(e,t,n={}){if(typeof e!="string"&&Ce(t)&&C(t,Sa,"tTest"),C(n,Sa,"tTest"),typeof e=="string")return rl(e,t,n);let r;t!==void 0&&!ArrayBuffer.isView(t)&&!Array.isArray(t)?n=t:r=t;let o=fe(n.alternative),s=n.mu??0,c=n.paired??!1,i=n.varEqual??!1,a=n.confLevel??.95;if(Number.isNaN(s))throw new RangeError("mu must be a number");if(Ct(a),c&&r===void 0)throw new TypeError("y is required when paired is true");let l,f,m;if(c){let[A,k]=pn(Array.from(e),Array.from(r));({d:l,k:m}=xr(A,k))}else{let A=ee(r===void 0?[Ge(Array.from(e))]:[Ge(Array.from(e)),Ge(Array.from(r))]);m=A.k,l=A.groups[0],f=A.groups[1]}let u=_(s,-m),p,d,g,w,y,v,N,b=l.length,h=f!==void 0&&i;if(b<1||!h&&b<2)throw new Error("not enough 'x' observations");let E=ke(l),x=b>1?qe(l):NaN;if(f===void 0){if(d=b-1,g=Math.sqrt(x/b),g===0||g<10*Number.EPSILON*Math.abs(E))throw new Error("data are essentially constant");p=(E-u)/g,v=c?"Paired t-test":"One Sample t-test",w=E,y=c?"mean difference":"mean of x",N=c?"mean difference":"mean"}else{let A=f.length;if(A<1||!i&&A<2)throw new Error("not enough 'y' observations");if(i&&b+A<3)throw new Error("not enough observations");let k=ke(f),q=A>1?qe(f):NaN;if(v=`${i?"":"Welch"} Two Sample t-test`,w=[E,k],y=["mean of x","mean of y"],N="difference in means",i){d=b+A-2;let I=0;b>1&&(I+=(b-1)*x),A>1&&(I+=(A-1)*q),I/=d,g=Math.sqrt(I*(1/b+1/A))}else{let I=x/b,z=q/A;g=Math.sqrt(Math.sqrt(I)**2+Math.sqrt(z)**2),d=Rr([I,z],[b-1,A-1])}if(g===0||g<10*Number.EPSILON*Math.max(Math.abs(E),Math.abs(k)))throw new Error("data are essentially constant");p=(E-k-u)/g}let M=typeof w=="number"?w:w[0]-w[1],R=(A,k)=>{let q=u+(p+k*A)*g;return Number.isFinite(q)&&Math.max(Math.abs(u),Math.abs(p*g))<=16*(Math.abs(M)+A*g)?q:M+k*A*g},T,S;if(o==="less")T=Ke(p,d,!0,!1),S=[-1/0,R(gt(a,d,!0,!1),1)];else if(o==="greater")T=Ke(p,d,!1,!1),S=[R(gt(a,d,!0,!1),-1),1/0];else{T=2*Ke(-Math.abs(p),d,!0,!1);let A=1-a,k=A/2<1e-8?gt(A/2,d,!1,!1):gt(1-A/2,d,!0,!1);S=[R(k,-1),R(k,1)]}return S=[_(S[0],m),_(S[1],m)],w=typeof w=="number"?_(w,m):w.map(A=>_(A,m)),g=_(g,m),{method:v,alternative:o,statistic:p,statisticName:"t",parameter:d,parameterName:"df",pValue:T,confInt:S,confLevel:a,estimate:w,estimateNames:y,nullValue:s,nullValueName:N,stdErr:g,stdError:g,warnings:[]}}function rl(e,t,n){if(n.paired!==void 0)throw new Error("cannot use 'paired' in formula method");let r=Lr(e,t,!0);if(r.kind==="one")return xn(r.x,n);if(r.kind==="paired")return xn(r.x,r.y,{...n,paired:!0});let[o,s]=r.levels;return{...xn(r.x,r.y,n),estimateNames:[`mean in group ${o}`,`mean in group ${s}`],nullValueName:`difference in means between group ${o} and group ${s}`}}var Aa=["correct","p","rescaleP","simulatePValue","B"];function lt(e){let t=[0,0],n=0;for(let r of e)t=Q(t,[r,0]),n+=r;return Number.isFinite(n)?t[0]:n}function ol(e,t,n){return Ce(t)&&C(t,Aa,"chisqTest"),C(n,Aa,"chisqTest"),Array.isArray(t)?j("chisqTest",sl(e,t,n??{})):j("chisqTest",al(e,t??{}))}var _a="Chi-squared approximation may be incorrect";function sl(e,t,n){if(e.length!==t.length)throw new RangeError("'x' and 'y' must have the same length");let r=u=>u==null;for(let u of[...e,...t])if(!r(u)&&typeof u!="string")throw new TypeError("factor values must be strings (or null for missing values)");let o=[],s=[];e.forEach((u,p)=>{let d=t[p];!r(u)&&!r(d)&&(o.push(u),s.push(d))});let c=[...new Set(o)].sort(it),i=[...new Set(s)].sort(it);if(c.length<2||i.length<2)throw new RangeError("'x' and 'y' must have at least 2 levels");let a=new Map(c.map((u,p)=>[u,p])),l=new Map(i.map((u,p)=>[u,p])),f=c.map(()=>i.map(()=>0));return o.forEach((u,p)=>f[a.get(u)][l.get(s[p])]++),{...Pa(f,n),rowLevels:c,colLevels:i}}function al(e,t){if(e.length>0&&typeof e[0]!="number"){let r=e.map(s=>Array.from(s)),o=r[0].length;if(r.some(s=>s.length!==o))throw new TypeError("all rows of the matrix must have the same length");return r.length===1?So(r[0],t):o===1?So(r.map(s=>s[0]),t):Pa(r,t)}return So(Array.from(e),t)}function Ia(e){let t=e.B??2e3;if(!(Number.isInteger(t)&&t>=1&&t<2**31))throw new RangeError("'B' must be a positive integer");return t}function La(e,t,n){let r=(1-64*Number.EPSILON)*t,o=0;for(let s=0;s<e.length;s++)e[s]>=r&&o++;return(1+o)/(Math.trunc(n)+1)}function Oa(e,t){return Number.isNaN(e)?NaN:xe(e,t/2,2,!1,!1)}function Fa(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 So(e,t){let n=e.length;if(e.some(g=>!(g>=0&&g<1/0)))throw new RangeError("all entries of 'x' must be non-negative and finite");if(n<2)throw new TypeError("'x' must have at least 2 elements");let r=lt(e);if(!(r>0))throw new RangeError("at least one entry of 'x' must be positive");let o=t.p===void 0?e.map(()=>1/n):Array.from(t.p);if(o.length!==n)throw new TypeError("'x' and 'p' must have the same number of elements");if(o.some(Number.isNaN))throw new RangeError("p contains missing values");if(o.some(g=>g<0))throw new RangeError("probabilities must be non-negative");if(o.some(g=>g===1/0))throw new RangeError("probabilities must be finite");if(t.rescaleP??!1){let g=lt(o),w=g<1/0?0:Math.ceil(Math.log2(n)),y=w===0?o:o.map(N=>_(N,-w)),v=w===0?g:lt(y);o=y.map(N=>N/v)}else if(Math.abs(lt(o)-1)>Math.sqrt(Number.EPSILON))throw new RangeError("probabilities must sum to 1");let s=r<1/0?0:2*Math.ceil(Math.log2(n)/2),c=s===0?e:e.map(g=>_(g,-s)),i=s===0?r:lt(c),a=o.map(g=>i*g),l=s===0?a:a.map(g=>_(g,s));Fa(l);let f=o.map((g,w)=>a[w]*(1-g)),m=_(lt(c.map((g,w)=>Oe(g-a[w],a[w]))),s),u="Chi-squared test for given probabilities",p=n-1,d;if(t.simulatePValue??!1){let g=Ia(t);if(!Number.isInteger(r))throw new RangeError("the simulation needs counts summing to an integer");u+=Hn(g),p=NaN,d=La(il(r,o,l,g),m,g)}else d=Oa(m,p);return{method:u,statistic:m,statisticName:"X-squared",parameter:p,parameterName:"df",pValue:d,observed:e,expected:l,residuals:c.map((g,w)=>_((g-a[w])/Math.sqrt(a[w]),s/2)),stdres:c.map((g,w)=>_((g-a[w])/Math.sqrt(f[w]),s/2)),warnings:Number.isFinite(p)&&l.some(g=>g<5)?[_a]:[]}}function il(e,t,n,r){let o=t.length;if(r*e>2**27)throw new RangeError("the simulation needs B * sum(x) <= 2^27 (134217728) draws");let s=fa(o,t);if(r*e===0)return new Float64Array(0);let c=new Float64Array(r),i=new Float64Array(o);for(let a=0;a<r;a++){i.fill(0);for(let f=0;f<e;f++)i[s()]++;let l=0;for(let f=0;f<o;f++)l+=(i[f]-n[f])**2/n[f];c[a]=l}return c}function cl(e,t,n,r){let o=e.length,s=t.length,c=e.reduce((f,m)=>f+m,0);if(c>kr)throw new RangeError("the simulation needs a total count of at most 16777216 (2^24)");let i=qr(c),a=new Int32Array(o*s),l=new Float64Array(Math.trunc(r));for(let f=0;f<l.length;f++){_r(e,t,i,a);let m=0;for(let u=0;u<s;u++)for(let p=0;p<o;p++){let d=n[p][u];m+=(a[p+u*o]-d)**2/d}l[f]=m}return l}function Pa(e,t){let n=t.correct??!0,r=e.length,o=e[0].length,s=e.flat();if(s.some(v=>!(v>=0&&v<1/0)))throw new RangeError("all entries of 'x' must be non-negative and finite");let c=lt(s);if(!(c>0))throw new RangeError("at least one entry of 'x' must be positive");let i=e.map(v=>lt(v)),a=e[0].map((v,N)=>lt(e.map(b=>b[N]))),l=i.map(v=>a.map(N=>Tr(v,N,c)));Fa(l.flat());let f=e.map((v,N)=>v.map((b,h)=>b-l[N][h])),m="Pearson's Chi-squared test",u,p=(r-1)*(o-1),d,g=(t.simulatePValue??!1)&&i.every(v=>v>0)&&a.every(v=>v>0),w=!g&&n&&r===2&&o===2?Math.min(.5,...f.flat().map(Math.abs)):0,y=[];if((t.simulatePValue??!1)&&!g&&y.push("cannot compute simulated p-value with zero marginals"),g){if(e.some(b=>b.some(h=>!Number.isInteger(h))))throw new RangeError("the simulation needs integer counts");let v=Ia(t);m+=Hn(v),u=lt(f.flatMap((b,h)=>b.map((E,x)=>Oe(E,l[h][x])))),p=NaN;let N=cl(i.map(b=>Math.trunc(b)),a.map(b=>Math.trunc(b)),l,v);d=La(N,u,v)}else w>0?(m+=" with Yates' continuity correction",u=lt(f.flatMap((v,N)=>v.map((b,h)=>Oe(Math.abs(b)-w,l[N][h]))))):u=lt(f.flatMap((v,N)=>v.map((b,h)=>Oe(b,l[N][h]))));return{method:m,statistic:u,statisticName:"X-squared",parameter:p,parameterName:"df",pValue:d??Oa(u,p),observed:e,expected:l,residuals:f.map((v,N)=>v.map((b,h)=>b/Math.sqrt(l[N][h]))),stdres:ll(e,c,i,a,l,f),warnings:Number.isFinite(p)&&l.some(v=>v.some(N=>N<5))?[...y,_a]:y}}function ll(e,t,n,r,o,s){if(!Number.isFinite(t))return s.map((d,g)=>d.map((w,y)=>w/Math.sqrt(o[g][y]*(1-n[g]/t)*(1-r[y]/t))));let c=e.map(d=>ka(d)),i=e[0].map((d,g)=>ka(e.map(w=>w[g]))),a=ml(c),l=qa(c),f=qa(i),m=l.map(d=>Ae(d,a)),u=i.map(d=>Ae(d,a)),p=f.map(d=>Ae(d,a));return e.map((d,g)=>d.map((w,y)=>{let v=pe(c[g],u[y]),N=de([w,0],v),b=N[0]+N[1],h=pe(pe(v,m[g]),p[y]),E=h[0]+h[1];if(E>=fl||b===0)return b/Math.sqrt(E);let x=v[0]+v[1],M=m[g][0]+m[g][1],R=p[y][0]+p[y][1];return b/Math.sqrt(x)/Math.sqrt(M)/Math.sqrt(R)}))}var fl=2**-1022;function ka(e){let t=[0,0];for(let n of e)t=Q(t,[n,0]);return t}function ml(e){let t=[0,0];for(let n of e)t=Q(t,n);return t}function qa(e){let t=e.length,n=new Array(t+1);n[t]=[0,0];for(let s=t-1;s>=0;s--)n[s]=Q(e[s],n[s+1]);let r=[],o=[0,0];for(let s=0;s<t;s++)r.push(Q(o,n[s+1])),o=Q(o,e[s]);return r}var ul=Math.pow(Number.EPSILON,.25);function wt(e,t,n,r={}){let o={tol:r.tol??ul,maxiter:r.maxIter??1e3};return r.fLower!==void 0&&(o.fLower=r.fLower),r.fUpper!==void 0&&(o.fUpper=r.fUpper),Bn(e,t,n,o).root}function Vr(e,t){let n=a=>e(Math.exp(a)),r=0,o=n(0),s=t?-1:1,c=n(s);for(;c!==0&&Math.sign(c)===Math.sign(o);){if(Math.abs(s)>745)return t?0:1/0;r=s,o=c,s*=2,c=n(s)}if(c===0)return Math.exp(s);let i=r<s?wt(n,r,s,{tol:2**-50,fLower:o,fUpper:c}):wt(n,s,r,{tol:2**-50,fLower:c,fUpper:o});return Math.exp(i)}var pl=["alternative","confInt","confLevel","or","simulatePValue","B"];function hl(e,t={}){C(t,pl,"fisherTest");let n=e.length,r=n===0?0:e[0].length;if(e.some(f=>f.length!==r))throw new TypeError("all rows of the matrix must have the same length");if(n<2||r<2)throw new TypeError("'x' must have at least 2 rows and columns");let o=e.map(f=>Array.from(f));if(o.flat().some(f=>Number.isNaN(f)||f<0))throw new RangeError("all entries of 'x' must be non-negative and finite");if(o.flat().some(f=>f===1/0))throw new RangeError("'x' has entries too large to be integer");let s=o.map(f=>f.map(Hs));if(s.flat().some(f=>f>2147483647))throw new RangeError("'x' has entries too large to be integer");let c=[],i=dl(o,s);i!==null&&c.push(`'x' has been rounded to integer: ${i}`);let a="Fisher's Exact Test for Count Data";if(n===2&&r===2)return j("fisherTest",{...vl(s,t,a),warnings:c});let l=t.alternative??"two.sided";if(t.simulatePValue??!1){let f=t.B??2e3;if(!(Number.isInteger(f)&&f>=1&&f<2**31))throw new RangeError("'B' must be a positive integer");return j("fisherTest",{method:a+Hn(f),alternative:l,pValue:gl(s,f),warnings:c})}return j("fisherTest",{method:a,alternative:l,pValue:yl(s),warnings:c})}function dl(e,t){let n=Math.sqrt(Number.EPSILON),r=[],o=[];for(let l=0;l<e[0].length;l++)for(let f=0;f<e.length;f++)e[f][l]!==t[f][l]&&(r.push(e[f][l]),o.push(t[f][l]));let s=r.length;if(s===0)return null;let c=0;for(let l of r)c+=Math.abs(l)/s;let i="relative";Number.isFinite(c)&&c>n||(c=1,i="absolute");let a=0;for(let l=0;l<s;l++)a+=Math.abs(r[l]-o[l])/(s*c);return Number.isNaN(a)||a>n?`Mean ${i} difference: ${ma(a)}`:null}function gl(e,t){let n=e.filter(w=>w.some(y=>y>0)),r=e[0].map((w,y)=>y).filter(w=>e.some(y=>y[w]>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(w=>r.map(y=>w[y])),s=o.map(w=>w.reduce((y,v)=>y+v,0)),c=o[0].map((w,y)=>o.reduce((v,N)=>v+N[y],0)),i=s.length,a=c.length,l=[0,0];for(let w of o.flat())l=Q(l,[-sa(w),0]);let f=l[0]/(1+64*Number.EPSILON),m=s.reduce((w,y)=>w+y,0);if(m>kr)throw new RangeError("the simulation needs a total count of at most 16777216 (2^24)");let u=qr(m),p=new Int32Array(i*a),d=0,g=Math.trunc(t);for(let w=0;w<g;w++){_r(s,c,u,p);let y=0;for(let v=0;v<p.length;v++)y-=u[p[v]];y<=f&&d++}return Math.max(0,Math.min(1,(1+d)/(g+1)))}var wl=1+1e-7,bl=16,Nl=2**24;function Ao(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=Q(r,[e[o],0]));return r[0]}var qo=class{m;n;k;lo;hi;logCentral;constructor(t,n,r){this.m=t,this.n=n,this.k=r,this.lo=Math.max(0,r-n),this.hi=Math.min(r,t),this.logCentral=new Float64Array(this.hi-this.lo+1);let o=0,s=1;for(let c=this.lo;c<=this.hi;c++){let i=c-this.lo;i%bl===0||!(s>0&&s<1/0)?(o=hn(c,t,n,r,!0),s=1,this.logCentral[i]=o):this.logCentral[i]=o+Math.log(s),s*=(t-c)*(r-c)/((c+1)*(n-r+c+1))}}density(t){let n=this.logCentral.length,r=Math.log(t),o=new Float64Array(n),s=-1/0;for(let i=0;i<n;i++){let a=this.logCentral[i]+(this.lo+i)*r;o[i]=a,a>s&&(s=a)}for(let i=0;i<n;i++){let a=o[i]-s;o[i]=a<-746?0:Math.exp(a)}let c=Ao(o,0,n-1);for(let i=0;i<n;i++)o[i]!==0&&(o[i]=o[i]/c);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=Q(r,[(this.lo+o)*n[o],0]));return r[0]}tail(t,n,r){if(n===1)return r?xo(t-1,this.m,this.n,this.k,!1,!1):xo(t,this.m,this.n,this.k,!0,!1);if(n===0||n===1/0){let c=n===0?this.lo:this.hi;return(r?t<=c:t>=c)?1:0}let o=this.density(n),s=t-this.lo;return r?Ao(o,s,o.length-1):Ao(o,0,s)}};function ko(e,t){return Vr(e,t)}function vl(e,t,n){let r=fe(t.alternative),o=t.or??1;if(!(o>=0))throw new RangeError("'or' must be a single number between 0 and Inf");let s=t.confLevel??.95;if(!(s>0&&s<1))throw new RangeError("confLevel must be a single number between 0 and 1");let[[c,i],[a,l]]=e,f=c,m=Math.min(c,l)+Math.min(i,a)+1;if(m>Nl)throw new RangeError(`the exact distribution of the 2 x 2 table has ${m} values, more than 2^24`);let u=new qo(c+a,i+l,c+i),{lo:p,hi:d}=u,g;if(r==="less")g=u.tail(f,o,!1);else if(r==="greater")g=u.tail(f,o,!0);else if(o===0)g=f===p?1:0;else if(o===1/0)g=f===d?1:0;else{let h=u.density(o),E=h[f-p]*wl,x=[0,0];for(let M of h)M<=E&&(x=Q(x,[M,0]));g=x[0]}let w;if(f===p)w=0;else if(f===d)w=1/0;else{let h=u.mean(1);w=h===f?1:ko(E=>u.mean(E)-f,h>f)}let y={method:n,alternative:r,pValue:g,estimate:w,estimateNames:"odds ratio",nullValue:o,nullValueName:"odds ratio",warnings:[]};if(!(t.confInt??!0))return y;let v=h=>{if(f===d)return 1/0;let E=u.tail(f,1,!1);return E===h?1:ko(x=>u.tail(f,x,!1)-h,E<h)},N=h=>{if(f===p)return 0;let E=u.tail(f,1,!0);return E===h?1:ko(x=>u.tail(f,x,!0)-h,E>h)},b;if(r==="less")b=[0,v(1-s)];else if(r==="greater")b=[N(1-s),1/0];else{let h=(1-s)/2;b=[N(h),v(h)]}return{...y,confInt:b,confLevel:s}}var zr=[[0,0]];function bt(e){for(let t=zr.length;t<=e;t++)zr.push(Q(zr[t-1],Ks([t,0])));return zr[e]}function $r(e,t,n,r){let o=e+n,s=o-e,c=e-(o-s)+(n-s)+t+r,i=o+c;return[i,c-(i-o)]}var _o=class{scale=-1/0;hi=0;lo=0;add(t,n,r){if(t>this.scale){let a=Number.isFinite(this.scale)?Math.exp(this.scale-t):0;this.hi*=a,this.lo*=a,this.scale=t}let o=r*Math.exp(t-this.scale)*(1+n),s=this.hi+o,c=s-this.hi,i=this.hi-(s-c)+(o-c)+this.lo;this.hi=s+i,this.lo=i-(this.hi-s)}value(){return this.scale===-1/0?0:this.hi*Math.exp(this.scale)}};function yl(e){let t=e.filter(N=>N.some(b=>b>0));if(t.length===0)throw new RangeError("all entries of the table are zero");let n=t[0].map((N,b)=>t.some(h=>h[b]>0));if(t=t.map(N=>N.filter((b,h)=>n[h])),t.length<2||t[0].length<2)return 1;t.length>t[0].length&&(t=t[0].map((N,b)=>t.map(h=>h[b])));let r=t.length,o=t.map(N=>N.reduce((b,h)=>b+h,0)),s=t[0].map((N,b)=>t.reduce((h,E)=>h+E[b],0)).sort((N,b)=>b-N),c=s.length,i=o.reduce((N,b)=>N+b,0),a=js(bt(i));for(let N of o)a=Q(a,bt(N));for(let N of s)a=Q(a,bt(N));let l=[0,0];for(let N of t.flat())l=de(l,bt(N));let f=Q(l,[Math.log1p(1e-7),0]),m=1e-9*(1+Math.abs(f[0])),[u,p]=f,d=Array.from({length:c},()=>new Map),g=(N,b)=>{let h=b.join(","),E=d[N].get(h);if(E!==void 0)return E;let x=Q(a,bt(b.reduce((k,q)=>k+q,0)));for(let k of b)x=de(x,bt(k));for(let k=N;k<c;k++)x=de(x,bt(s[k]));let M={R:b,KZ:x,max:0,min:0,edges:[],pastHi:[],pastLo:[],count:[],index:new Map};if(d[N].set(h,M),N===c-1){let k=[0,0];for(let q of b)k=de(k,bt(q));return M.max=M.min=k[0],M.edges.push({to:M,hi:k[0],lo:k[1],mult:1}),M}let R=new Map,T=new Array(r+1).fill(0);for(let k=r-1;k>=0;k--)T[k]=T[k+1]+b[k];let S=new Array(r).fill(0),A=(k,q,I)=>{if(k===r-1){S[k]=q;let V=de(I,bt(q)),W=b.map((X,te)=>X-S[te]).sort((X,te)=>X-te),F=g(N+1,W),L=`${W.join(",")}|${V[0]}`,$=R.get(L);$===void 0?R.set(L,{to:F,hi:V[0],lo:V[1],mult:1}):$.mult++;return}let z=Math.max(0,q-T[k+1]),P=Math.min(b[k],q);for(let V=z;V<=P;V++)S[k]=V,A(k+1,q-V,de(I,bt(V)))};if(A(0,s[N],[0,0]),M.edges=[...R.values()],M.max=ge(M.edges.map(k=>k.hi+k.to.max)),M.min=Je(M.edges.map(k=>k.hi+k.to.min)),N===c-2){let k=M.edges.map(F=>{let L=F.to.edges[0];return{v:Q([F.hi,F.lo],[L.hi,L.lo]),mult:F.mult}});k.sort((F,L)=>F.v[0]-L.v[0]||F.v[1]-L.v[1]);let q=k.length,I=k[q-1].v[0],z=new Float64Array(q),P=new Float64Array(q),V=new Float64Array(q),W=[0,0];k.forEach(({v:F,mult:L},$)=>{z[$]=F[0],P[$]=F[1];let X=de(F,[I,0]);W=Q(W,[L*Math.exp(X[0])*(1+X[1]),0]),V[$]=W[0]}),M.dist={hi:z,lo:P,cum:V,vmax:I,kv:Q(a,[I,0])}}return M},w=g(0,[...o].sort((N,b)=>N-b)),y=new _o,v=(N,b,h,E)=>{if(b+N.max<=u-m){let[A,k]=$r(N.KZ[0],N.KZ[1],b,h);return y.add(A,k,E),!0}if(b+N.min>u+m)return!0;let x=N.dist;if(x===void 0)return!1;let[M,R]=$r(u,p,-b,-h),T=0,S=x.hi.length;for(;T<S;){let A=T+S>>>1,k=x.hi[A];k<M||k===M&&x.lo[A]<=R?T=A+1:S=A}if(T>0){let[A,k]=$r(x.kv[0],x.kv[1],b,h);y.add(A,k,E*x.cum[T-1])}return!0};v(w,0,0,1)||(w.pastHi.push(0),w.pastLo.push(0),w.count.push(1));for(let N=0;N<c-2;N++)for(let b of d[N].values()){let{pastHi:h,pastLo:E,count:x}=b;if(x.length!==0){for(let{to:M,hi:R,lo:T,mult:S}of b.edges)for(let A=0;A<x.length;A++){let[k,q]=$r(h[A],E[A],R,T),I=x[A]*S;if(v(M,k,q,I))continue;let z=M.index.get(k);z===void 0?(M.index.set(k,M.count.length),M.pastHi.push(k),M.pastLo.push(q),M.count.push(I)):M.count[z]+=I}b.pastHi=b.pastLo=b.count=[],b.index.clear()}}return Math.min(1,y.value())}var Da=["alternative","mu","paired","exact","correct","confInt","confLevel","tolRoot"];function Jn(e,t,n={}){if(typeof e!="string"&&Ce(t)&&C(t,Da,"wilcoxTest"),C(n,Da,"wilcoxTest"),typeof e=="string"){if(n.paired!==void 0)throw new Error("cannot use 'paired' in formula method");let g=Lr(e,t,!0);return g.kind==="one"?Jn(g.x,n):Jn(g.x,g.y,g.kind==="paired"?{...n,paired:!0}:n)}let r;t!==void 0&&!ArrayBuffer.isView(t)&&!Array.isArray(t)?n=t:r=t;let o=fe(n.alternative),s=n.mu??0,c=n.paired??!1,i=n.exact??null,a=n.correct??!0,l=n.confInt??!1,f=n.confLevel??.95,m=n.tolRoot??1e-4;if(!Number.isFinite(s))throw new RangeError("mu must be a single finite number");if(l&&!(f>0&&f<1))throw new RangeError("confLevel must be a number strictly between 0 and 1");if(c&&r===void 0)throw new TypeError("y is required when paired is true");let u,p;if(c){let[g,w]=pn(Array.from(e),Array.from(r));u=Ge(g.map((y,v)=>y-w[v]))}else u=Ge(Array.from(e)),r!==void 0&&(p=Ge(Array.from(r)));if(u.length<1)throw new Error("not enough (non-missing) 'x' observations");if(p!==void 0&&p.length<1)throw new Error("not enough (non-missing) 'y' observations");let d={alternative:o,mu:s,correct:a,confInt:l,confLevel:f,tolRoot:m,warnings:new Te};return j("wilcoxTest",p===void 0?El(u,i,c,d):Sl(u,p,i,d))}function Wr(e,t,n){return n?t==="two.sided"?Math.sign(e)*.5:t==="greater"?.5:-.5:0}function za(e,t){return t==="less"?Y(e):t==="greater"?Y(e,{lowerTail:!1}):2*Math.min(Y(e),Y(e,{lowerTail:!1}))}function Ml(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 Io(e){let t=e.length;if(t===0)return NaN;let n=Math.floor(t/2);return t%2===1?e[n]:ke([e[n-1],e[n]])}var Lo=(e,t)=>e-t;function $a(e){let t=e.length,n=Ne(e.map(Math.abs)),r=0;for(let c=0;c<t;c++)e[c]>0&&(r+=n[c]);let o=0;for(let c of ut(n))o+=c*c*c-c;let s=Math.sqrt(t*(t+1)*(2*t+1)/24-o/48);return[r,r-t*(t+1)/4,s]}function El(e,t,n,r){let{alternative:o,mu:s,correct:c}=r,i=e.map(M=>M-s),a=i.some((M,R)=>!Number.isFinite(M)&&Number.isFinite(e[R]));a&&(i=e.map(M=>M/2-s/2));let l=i.some(M=>M===0);l&&(i=i.filter(M=>M!==0));let f=i.length;if(f===0)throw new RangeError("no non-zero differences for the signed rank test");let m=t??f<50,u=Ne(i.map(Math.abs)),p=ut(u).length>0,[d,g,w]=$a(i),y,v,N=m&&!p&&!l;if(N){v="Wilcoxon signed rank exact test";let M=f*(f+1)/2;if(o==="less")y=dn(d,f,!0,!1);else if(o==="greater")y=dn(d-1,f,!1,!1);else{let R=d>M/2?dn(d-1,f,!1,!1):dn(d,f,!0,!1);y=Math.min(1,2*R)}}else v=`Wilcoxon signed rank test${c?" with continuity correction":""}`,y=za((g-Wr(g,o,c))/w,o);let b={method:v,alternative:o,statistic:d,statisticName:"V",pValue:y,nullValue:s,nullValueName:n?"location shift":"location",warnings:r.warnings.list},h=()=>{m&&p&&(r.warnings.add("cannot compute exact p-value with ties"),r.confInt&&r.warnings.add("cannot compute exact confidence interval with ties")),m&&l&&(r.warnings.add("cannot compute exact p-value with zeroes"),r.confInt&&r.warnings.add("cannot compute exact confidence interval with zeroes"))};if(!r.confInt)return h(),b;let E=a?e.filter(M=>M!==s):i.map(M=>M+s);if(!a){let M=e.filter(T=>T!==s),R=0;for(let T of M)R=Math.max(R,Math.abs(T));E.some((T,S)=>Math.abs(T-M[S])>2**-26*R)&&(E=M)}let x=N?xl(E,r):Rl(E,r);return h(),{...b,...x}}function jr(e,t,n){return e==="less"?[-1/0,n]:e==="greater"?[t,1/0]:[t,n]}function Ca(e,t,n){let r={confInt:jr(e,NaN,NaN),confLevel:0,estimate:t};return n!==void 0&&(r.estimateNames=n),r}function Wa(e,t,n){let r=e(t==="two.sided"?n/2:n);return r===0?1:r}function ja(e){if(!(e>0&&e<1/0))throw new RangeError("tolRoot must be a positive finite number")}function xl(e,t){let{alternative:n,confLevel:r}=t,o=e.length,s=[];for(let u=0;u<o;u++)for(let p=u;p<o;p++)s.push(Bt(e[u],e[p]));s.sort(Lo);let c=s.length,i=1-r,a=Wa(u=>aa(u,o,!0,!1),n,i),l=dn(a-1,o,!0,!1),f=n==="two.sided"?2*l:l,m=f-i>i/2;return m&&t.warnings.add("requested conf.level not achievable"),{confInt:jr(n,s[a-1],s[c-a]),confLevel:m?1-Ml(f):r,estimate:Io(s),estimateNames:"(pseudo)median"}}function Rl(e,t){let{alternative:n,correct:r,confLevel:o,tolRoot:s}=t,c=e.length;if(c===0)return Ca(n,NaN);let i=Je(e),a=ge(e);if(!Number.isFinite(i)||!Number.isFinite(a)||c===1||i===a)return Number.isFinite(i)&&i===a&&t.warnings.add("cannot compute confidence interval when all observations are zero or tied"),Ca(n,Bt(i,a),"midrange");let l=(N,b)=>{let[,h,E]=$a(e.map(x=>x-N).filter(x=>x!==0));return(h-Wr(h,n,b))/E},f=l(i,r),m=l(a,r),u=N=>-Z(N),p=N=>n==="greater"?!(f<u(N)):n==="less"?!(m>Z(N/2)):!(f<u(N/2))&&!(m>Z(N/2)),d=1-o,g=!1;for(;!p(d)&&d<1;)d*=2,g=!0;g&&t.warnings.add("requested conf.level not achievable"),ja(s);let w=N=>wt(b=>l(b,r)-N,i,a,{tol:s,fLower:f-N,fUpper:m-N}),y;if(d>=1){let N=Io([...e].sort(Lo));y=jr(n,N,N)}else n==="greater"?y=[w(u(d)),1/0]:n==="less"?y=[-1/0,w(Z(d))]:y=[w(u(d/2)),w(Z(d/2))];let v=wt(N=>l(N,!1),i,a,{tol:s,fLower:l(i,!1),fUpper:l(a,!1)});return{confInt:y,confLevel:g?1-Math.min(1,d):o,estimate:v,estimateNames:"(pseudo)median"}}function Ka(e,t){let n=e.length,r=t.length,o=Ne([...e,...t]),s=0;for(let f=0;f<n;f++)s+=o[f];let c=s-n*(n+1)/2,i=0;for(let f of ut(o))i+=f*f*f-f;let a=n+r,l=Math.sqrt(n*r/12*(a+1-i/(a*(a-1))));return[c,c-n*r/2,l]}function Tl(e,t){let n=e.length,r=t.length,o=n+r,s=(p,d)=>{let g=p===0||!Number.isFinite(p)||!Number.isFinite(d)?d:d/2;return p<g?-1:p>g?1:0},c=(p,d)=>p<n&&d<n?e[p]<e[d]?-1:e[p]>e[d]?1:0:p>=n&&d>=n?t[p-n]<t[d-n]?-1:t[p-n]>t[d-n]?1:0:p<n?s(e[p],t[d-n]):-s(e[d],t[p-n]),i=Array.from({length:o},(p,d)=>d);i.sort((p,d)=>c(p,d)||p-d);let a=0,l=0,f=0;for(;f<o;){let p=f;for(;p+1<o&&c(i[p+1],i[f])===0;)p++;let d=(f+p+2)/2;for(let w=f;w<=p;w++)i[w]<n&&(a+=d);let g=p-f+1;l+=g*g*g-g,f=p+1}let m=a-n*(n+1)/2,u=Math.sqrt(n*r/12*(o+1-l/(o*(o-1))));return[m,m-n*r/2,u,l>0]}function Sl(e,t,n,r){let{alternative:o,mu:s,correct:c}=r,i=e.length,a=t.length,l=e.map(h=>h-s),f=n??(i<50&&a<50),m=l.some((h,E)=>!Number.isFinite(h)&&Number.isFinite(e[E])),u,p,d,g;m?(l=e.map(h=>h/2-s/2),[p,d,g,u]=Tl(l,t)):(u=ut([...l,...t]).length>0,[p,d,g]=Ka(l,t));let w,y,v=f&&!u;v?(y="Wilcoxon rank sum exact test",w=Va(()=>{if(o==="less")return gn(p,i,a,!0,!1);if(o==="greater")return gn(p-1,i,a,!1,!1);let h=p>i*a/2?gn(p-1,i,a,!1,!1):gn(p,i,a,!0,!1);return Math.min(1,2*h)})):(y=`Wilcoxon rank sum test${c?" with continuity correction":""}`,w=!m&&l.every(E=>E===t[0])&&t.every(E=>E===t[0])?1:za((d-Wr(d,o,c))/g,o));let N={method:y,alternative:o,statistic:p,statisticName:"W",pValue:w,nullValue:s,nullValueName:"location shift",warnings:r.warnings.list},b=r.confInt?v?Va(()=>Al(e,t,r)):kl(e,t,r):{};return f&&u&&(r.warnings.add("cannot compute exact p-value with ties"),r.confInt&&r.warnings.add("cannot compute exact confidence intervals with ties")),{...N,...b}}function Va(e){try{return e()}catch(t){throw t instanceof RangeError&&t.message.startsWith("wilcox:")?new RangeError(`${t.message}: use exact: false`):t}}function Al(e,t,n){let{alternative:r,confLevel:o}=n,s=e.length,c=t.length,i=[];for(let d of e)for(let g of t)i.push(d-g);i.sort(Lo);let a=i.length,l=1-o,f=Wa(d=>ca(d,s,c,!0,!1),r,l),m=gn(f-1,s,c,!0,!1),u=r==="two.sided"?2*m:m,p=u-l>l/2;return p&&n.warnings.add("Requested conf.level not achievable"),{confInt:jr(r,i[f-1],i[a-f]),confLevel:p?1-u:o,estimate:Io(i),estimateNames:"difference in location"}}function kl(e,t,n){let{alternative:r,correct:o,confLevel:s,tolRoot:c}=n,i=Je(e)-ge(t),a=ge(e)-Je(t);if(!Number.isFinite(i)||!Number.isFinite(a))throw new Error("cannot compute the confidence interval because the differences are not finite");let l=(p,d)=>{let[,g,w]=Ka(e.map(y=>y-p),t);return(g-Wr(g,r,d))/w},f=(p,d)=>{let g=l(i,d)-p,w=l(a,d)-p;if(Number.isNaN(g)||Number.isNaN(w))throw new Error("cannot compute the confidence interval because all observations are tied");return g<=0?i:w>=0?a:(ja(c),wt(y=>l(y,d)-p,i,a,{tol:c,fLower:g,fUpper:w}))},m=1-s,u;return r==="greater"?u=[f(-Z(m),o),1/0]:r==="less"?u=[-1/0,f(Z(m),o)]:u=[f(-Z(m/2),o),f(Z(m/2),o)],{confInt:u,confLevel:s,estimate:f(0,!1),estimateNames:"difference in location"}}var ql=["method","n"],Ua=["holm","hochberg","hommel","bonferroni","BH","BY","fdr","none"];function _l(e){if(Ua.includes(e))return e;let t=Ua.filter(n=>n.startsWith(e));if(e.length===0||t.length!==1)throw new RangeError(`invalid method "${e}"`);return t[0]}function Rn(e,t={}){C(t,ql,"pAdjust");let n=typeof t=="string"?{method:t}:t,r=_l(n.method??"holm"),o=Array.from(e),s=[];for(let m=0;m<o.length;m++)Number.isNaN(o[m])||s.push(m);let c=s.length,i=n.n??c;if(!(i>=c))throw new RangeError("n must be at least the number of non-NaN p-values");if(i===1/0)throw new RangeError("n must be finite");if(i<=1||c===0)return o;let a=s.map(m=>o[m]),l=Ga(a,r,i),f=o.slice();return s.forEach((m,u)=>f[m]=l[u]),f}function Kr(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 Ga(e,t,n){let r=e.length,o=new Array(r);switch(t){case"none":return e.slice();case"bonferroni":return e.map(s=>Math.min(1,n*s));case"holm":{let s=Kr(e,!1),c=-1/0;return s.forEach((i,a)=>{c=Math.max(c,(n-a)*e[i]),o[i]=Math.min(1,c)}),o}case"hochberg":{let s=Kr(e,!0),c=1/0;return s.forEach((i,a)=>{let l=r-a;c=Math.min(c,(n-l+1)*e[i]),o[i]=Math.min(1,c)}),o}case"BH":case"fdr":case"BY":{let s=1;if(t==="BY"){let m=Qs(),u=Math.floor(n);if(u<=1e6){for(let p=1;p<=u;p++)Zs(m,1/p,u);s=Js(m)}else{let p=u*u;s=Math.log(u)+.5772156649015329+1/(2*u)-1/(12*p)+1/(120*p*p)}}let c=s*n,i=Number.isFinite(c)?1:1024,a=Number.isFinite(c)?c:s*(n/1024),l=Kr(e,!0),f=1/0;return l.forEach((m,u)=>{let p=r-u;f=Math.min(f,a/p*e[m]*i),o[m]=Math.min(1,f)}),o}case"hommel":return n===2?Ga(e,"hochberg",n):Il(e,n)}}var Ya=2**16;function Il(e,t){if(!Number.isInteger(t))throw new RangeError("n must be an integer for the hommel method");if(t>Ya)throw new RangeError(`n must be at most ${Ya} for the hommel method (O(n^2) work)`);let n=e.length,r=[...e,...new Array(t-n).fill(1)],o=Kr(r,!1),s=o.map(a=>r[a]),c=s.slice();for(let a=t;a>=2;a--){let l=1/0;for(let f=1;f<=a;f++)l=Math.min(l,a*s[t-a+f-1]/f);for(let f=t-a;f<t;f++)c[f]=Math.max(c[f],l);for(let f=0;f<t-a;f++)c[f]=Math.max(c[f],Math.min(a*s[f],l))}let i=new Array(n);return o.forEach((a,l)=>{a<n&&(i[a]=c[l])}),i}var Ha=["p","alternative","confLevel"],Ll=1+1e-7;function Oo(e,t,n){let r=e-1;return e===0||e-r===1&&Rt(t,-r)[1]===0?un(r,t,n,!1,!1):un(e,t,n,!1,!1)+Ht(e,t,n,!1)}function Ol(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=Ht(e,t,n,!1)*Ll,s=a=>Ht(a,t,n,!1)<=o;if(e<r){let a=vr(s,Math.ceil(r),t),l=a>t?0:Oo(a,t,n);return Math.min(1,un(e,t,n,!0,!1)+l)}let c=yr(s,0,Math.floor(r)),i=c<0?0:un(c,t,n,!0,!1);return Math.min(1,Oo(e,t,n)+i)}function Fl(e,t,n={}){C(t,Ha,"binomTest"),C(n,Ha,"binomTest");let r,o;if(typeof e!="number"){if(e.length!==2)throw new RangeError("incorrect length of 'x'");if(t!==void 0&&typeof t!="number"&&(n=t),!Qt(e[0])||!Qt(e[1]))throw new RangeError("'x' must be nonnegative and integer");if(r=Math.round(e[0]),o=Math.round(e[0])+Math.round(e[1]),o===0||o===1/0)throw new RangeError("'n' must be a positive integer >= 'x'")}else{if(typeof t!="number")throw new TypeError("'n' is required when 'x' is a single number");if(!Qt(e))throw new RangeError("'x' must be nonnegative and integer");if(r=Math.round(e),!(Qt(t)&&Math.round(t)>=r&&t>0))throw new RangeError("'n' must be a positive integer >= 'x'");o=Math.round(t)}return Fo(r,o,n)}function Fo(e,t,n={}){let r=n.p??.5;if(!(r>=0&&r<=1))throw new RangeError("'p' must be a single number between 0 and 1");let o=fe(n.alternative),s=n.confLevel??.95;Be(s);let c;o==="less"?c=un(e,t,r,!0,!1):o==="greater"?c=Oo(e,t,r):c=Ol(e,t,r);let i=f=>e===0?0:Er(f,e,t-e+1,!0,!1),a=f=>e===t?1:f<1e-8?Er(f,e+1,t-e,!1,!1):Er(1-f,e+1,t-e,!0,!1),l;if(o==="less")l=[0,a(1-s)];else if(o==="greater")l=[i(1-s),1];else{let f=(1-s)/2;l=[i(f),a(f)]}return{method:"Exact binomial test",alternative:o,statistic:e,statisticName:"number of successes",parameter:t,parameterName:"number of trials",pValue:c,confInt:l,confLevel:s,estimate:e/t,estimateNames:"probability of success",nullValue:r,nullValueName:"probability of success",warnings:[]}}var Pl=["T","r","alternative","confLevel"],Dl=1+1e-7;function Cl(e,t){if(t===0)return e===0?1:0;if(e===t)return 1;let n=Mr(e,t,!1)*Dl,r=c=>Mr(c,t,!1)<=n,o=Math.floor(t);if(e<t){let c=vr(r,Math.max(e+1,o),1/0);return Math.min(1,mn(e,t,!0,!1)+Po(c,t))}let s=yr(r,0,Math.min(e-1,o));return Math.min(1,mn(s,t,!0,!1)+Po(e,t))}var er=2**-1022;function Po(e,t){return e-(e-1)===1?mn(e-1,t,!1,!1):mn(e,t,!1,!1)+Mr(e,t,!1)}function Jt(e){let t=e<9332636185032189e-317?600:0,n=Math.floor(Math.log2(e*2**t));return[e*2**t*2**-n,n-t]}function Vl(e,t,n,r){let o=n+t*r,s=e*t*n/o;if(Number.isFinite(s)&&Number.isFinite(o)&&o>=er&&(s!==0||e===0||t===0))return s;let c=e*t,i=1+t*(r/n);if(e===0||t===0)return 0;if(Number.isFinite(c)&&Number.isFinite(i))return c/i;let[a,l]=Jt(e),[f,m]=Jt(t),[u,p]=Jt(n),[d,g]=Jt(r),w=f*d,y=p-m-g,v=y>=0?u/(u+_(w,-y)):u/(_(u,y)+w);return _(a*f*v,l+m+Math.min(y,0))}function zl(e,t,n,r){let o=e/n,s=t/r,c=o/s;return t===0||Number.isFinite(o)&&Number.isFinite(s)&&s>=er&&(o===0||o>=er)?c:e===0?0:e/t*(r/n)}function $l(e,t={}){C(t,Pl,"poissonTest");let n=typeof e=="number"?[e]:Array.from(e);if(n.length<1||n.length>2)throw new RangeError("incorrect length of 'x'");if(!n.every(Qt))throw new RangeError("'x' must be finite, nonnegative, and integer");let r=n.map(Math.round),o=r.length,s=t.T??1,c=typeof s=="number"?new Array(o).fill(s):Array.from(s);if(c.length!==o)throw new RangeError("'x' and 'T' have incompatible length");if(!c.every(y=>Number.isFinite(y)&&y>0))throw new RangeError("'T' must be positive");let i=t.r??1;if(!(Number.isFinite(i)&&i>=0))throw new RangeError("'r' must be a single positive number");let a=fe(t.alternative),l=t.confLevel??.95;if(o===2?Be(l):Ct(l),o===2){let[y,v]=r,[N,b]=c,h=y+v;if(h===1/0)throw new RangeError("'x' must be finite, nonnegative, and integer");let E=i*N,x=E+b,M=Number.isFinite(x)&&x>=er&&(E===0||E>=er)?E/x:1/(1+b/N/i),R=Fo(Math.round(y),Math.round(y)+Math.round(v),{p:M,alternative:a,confLevel:l}),T=b/N,S=A=>{if(A===0)return 0;let k=A/(1-A);if(Number.isFinite(T)||k===1/0)return k*T;let[q,I]=Jt(k),[z,P]=Jt(N),[V,W]=Jt(b);return _(q*V/z,I+W-P)};return{method:"Comparison of Poisson rates",alternative:a,statistic:y,statisticName:"count1",parameter:Vl(h,i,N,b),parameterName:"expected count1",pValue:R.pValue,confInt:[S(R.confInt[0]),S(R.confInt[1])],confLevel:l,estimate:zl(y,v,N,b),estimateNames:"rate ratio",nullValue:i,nullValueName:"rate ratio",warnings:[]}}let f=r[0],m=c[0],u=i*m,p;a==="less"?p=mn(f,u,!0,!1):a==="greater"?p=Po(f,u):p=Cl(f,u);let d=y=>f===0?0:Nr(y,f,1,!0,!1),g=y=>y<1e-8?Nr(y,f+1,1,!1,!1):Nr(1-y,f+1,1,!0,!1),w;if(a==="less")w=[0,g(1-l)];else if(a==="greater")w=[d(1-l),1/0];else{let y=(1-l)/2;w=[d(y),g(y)]}return{method:"Exact Poisson test",alternative:a,statistic:f,statisticName:"number of events",parameter:m,parameterName:"time base",pValue:p,confInt:[w[0]/m,w[1]/m],confLevel:l,estimate:f/m,estimateNames:"event rate",nullValue:i,nullValueName:"event rate",warnings:[]}}var Xa=["p","alternative","confLevel","correct"],Wl="Chi-squared approximation may be incorrect";function Do(e,t,n={}){Ce(t)&&C(t,Xa,"propTest"),C(n,Xa,"propTest");let r,o,s=n;if(typeof e!="number"&&e.length>0&&Array.isArray(e[0])){let S=e;if(S.some(A=>A.length!==2))throw new RangeError("'x' must have 2 columns");r=S.map(A=>A[0]),o=S.map(A=>A[0]+A[1]),t!==void 0&&typeof t=="object"&&!Array.isArray(t)&&(s=t)}else{if(t===void 0||typeof t=="object"&&!Array.isArray(t))throw new RangeError("'n' must be given");r=typeof e=="number"?[e]:Array.from(e),o=typeof t=="number"?[t]:Array.from(t)}if(r.length!==o.length)throw new RangeError("'x' and 'n' must have the same length");let c=r.length,i=[];for(let S=0;S<c;S++)!Number.isNaN(r[S])&&!Number.isNaN(o[S])&&i.push(S);let a=i.length;if(a<1)throw new RangeError("not enough data");if(r=i.map(S=>r[S]),o=i.map(S=>o[S]),o.some(S=>!(S>0)))throw new RangeError("elements of 'n' must be positive");if(o.some(S=>S===1/0))throw new RangeError("elements of 'n' must be finite");if(r.some(S=>!(S>=0)))throw new RangeError("elements of 'x' must be nonnegative");if(r.some((S,A)=>S>o[A]))throw new RangeError("elements of 'x' must not be greater than those of 'n'");let l;if(s.p!==void 0){let S=typeof s.p=="number"?[s.p]:Array.from(s.p);if(S.length!==c)throw new RangeError("'p' must have the same length as 'x' and 'n'");if(l=i.map(A=>S[A]),l.some(A=>!(A>0&&A<1)))throw new RangeError("elements of 'p' must be in (0,1)")}let f=l!==void 0;a===1&&l===void 0&&(l=[.5]);let m=s.confLevel??.95;Be(m);let u=fe(s.alternative);(a>2||a===2&&f)&&(u="two.sided");let p=r.map((S,A)=>S/o[A]),d=a===2?p[0]-p[1]:0,g=a===2?1/o[0]+1/o[1]:0,w=(s.correct??!0)&&a<=2?.5:0,y=!1;if(w>0&&a===1){let S=Math.abs(r[0]-o[0]*l[0]);S<=w&&([w,y]=[S,!0])}else if(w>0&&a===2&&!f){let S=Math.abs(d)/g;S<=w&&([w,y]=[S,!0])}let v=jl(r,o),N=o.some((S,A)=>{let k=l?.[A]??v;return S*k<5||S*(1-k)<5}),b=0;if(!y)for(let S=0;S<a;S++){let A=l?.[S]??v,k=o[S]*A,q=o[S]*(1-A);b+=Oe(Math.abs(r[S]-k)-w,k),b+=Oe(Math.abs(o[S]-r[S]-q)-w,q)}let h=l!==void 0?a:a-1,E;if(u==="two.sided")E=xe(b,h/2,2,!1,!1);else{let S=Math.sign(a===1?p[0]-l[0]:d);E=Y(S*Math.sqrt(b),{lowerTail:u==="less"})}let x;if(a===1||a===2&&!f){let S=(1-m)/2,A=u==="two.sided"&&S<1e-8?Z(S,{lowerTail:!1}):Z(u==="two.sided"?(1+m)/2:m);if(a===1){let k=o[0],q=A*A,I=(F,L)=>(F+q/(2*k)+L*A*Math.sqrt(F*(1-F)/k+q/(4*k*k)))/(1+q/k),z=p[0]+w/k,P=p[0]-w/k,V=Math.min(1,Math.max(0,z>=1?1:I(z,1))),W=Math.min(1,Math.max(0,P<=0?0:I(P,-1)));x=u==="two.sided"?[W,V]:u==="greater"?[W,1]:[0,V]}else{let k=Math.sqrt(p.reduce((P,V,W)=>P+V*(1-V)/o[W],0)),q=A*k+w*g,I=Math.max(-1,d-q),z=Math.min(1,d+q);x=u==="two.sided"?[I,z]:u==="greater"?[I,1]:[-1,z]}}let M=a===1?"p":i.map(S=>`prop ${S+1}`),T={method:`${a===1?"1-sample proportions test":`${a}-sample test for ${f?"given proportions":"equality of proportions"}`} ${w!==0?"with":"without"} continuity correction`,alternative:u,statistic:b,statisticName:"X-squared",parameter:h,parameterName:"df",pValue:E,estimate:a===1?p[0]:p,estimateNames:M,warnings:N?[Wl]:[]};return l!==void 0&&(T.nullValue=a===1?l[0]:l,T.nullValueName=M),x!==void 0&&(T.confInt=x,T.confLevel=m),j("propTest",T)}function jl(e,t){let n=t.reduce((o,s)=>o+s,0);if(n<1/0)return e.reduce((o,s)=>o+s,0)/n;let r=Math.ceil(Math.log2(t.length));return e.reduce((o,s)=>o+_(s,-r),0)/t.reduce((o,s)=>o+_(s,-r),0)}function Tn(e){let t=Fe(e);return t.some(n=>typeof n=="string")?t.map(n=>typeof n=="number"&&!Number.isNaN(n)?Pe(n):n):t}function tr(e,t){return typeof e=="number"&&typeof t=="number"?e-t:it(String(e),String(t))}function Vt(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 a=i.map(m=>Array.from(m).filter(u=>!Number.isNaN(u)));if(n==="error"&&a.some(m=>m.length===0))throw new RangeError("all groups must contain data");let l=[],f=[];return a.forEach((m,u)=>{m.length!==0&&(l.push(String(u+1)),f.push(m))}),{labels:l,groups:f}}let r=Array.from(e);if(r.length!==t.length)throw new RangeError("'x' and 'g' must have the same length");let o=Tn(t),s=new Map;for(let i=0;i<r.length;i++){let a=r[i],l=o[i];if(Number.isNaN(a)||typeof l=="number"&&Number.isNaN(l))continue;let f=s.get(l);f===void 0&&s.set(l,f=[]),f.push(a)}let c=Array.from(s.keys()).sort(tr);return{labels:c.map(Pe),groups:c.map(i=>s.get(i))}}function en(e,t,n="drop"){return Vt(e,t,n).groups}function Ur(e){let t=[...e].sort((r,o)=>r-o),n=t.length;return n%2===1?t[(n-1)/2]:Bt(t[n/2-1],t[n/2])}function Co(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))}var Ba=["correct"];function Kl(e,t,n={}){Ce(t)&&C(t,Ba,"mcnemarTest"),C(n,Ba,"mcnemarTest");let r;if(e.length>0&&Array.isArray(e[0])){t!==void 0&&!Array.isArray(t)&&(n=t),r=e.map(f=>Array.from(f));let l=r.length;if(l<2||r.some(f=>f.length!==l))throw new RangeError("'x' must be square with at least two rows and columns");if(r.some(f=>f.some(m=>!Number.isFinite(m)||m<0)))throw new RangeError("all entries of 'x' must be nonnegative and finite")}else{if(!Array.isArray(t))throw new TypeError("'y' is required when 'x' is not a table");r=Ul(e,t)}let o=n.correct??!0,s=r.length,c,i=o&&s===2&&r[0][1]!==r[1][0];if(i){let l=r[0][1],f=r[1][0];c=Qa(Math.max(0,Math.abs(l-f)-1),l,f)}else{c=0;let l=0;for(let f=0;f<s;f++)for(let m=f+1;m<s;m++){let u=r[f][m],p=r[m][f];u+p!==0&&(c+=Qa(u-p,u,p),l++)}if(l===0)throw new RangeError("the table has no discordant pairs")}let a=s*(s-1)/2;return{method:i?"McNemar's Chi-squared test with continuity correction":"McNemar's Chi-squared test",statistic:c,statisticName:"McNemar's chi-squared",parameter:a,parameterName:"df",pValue:xe(c,a/2,2,!1,!1),warnings:[]}}function Qa(e,t,n){let r=t+n;return r<1/0?Oe(e,r):Oe(_(e,-520),_(t,-1040)+_(n,-1040))}function Ul(e,t){if(e.length!==t.length)throw new RangeError("'x' and 'y' must have the same length");let n=[],r=Tn(e),o=Tn(t);for(let m=0;m<e.length;m++){let u=r[m],p=o[m];typeof u=="number"&&Number.isNaN(u)||typeof p=="number"&&Number.isNaN(p)||n.push([u,p])}let s=m=>Array.from(new Set(m)).sort(tr),c=s(n.map(m=>m[0])),i=s(n.map(m=>m[1]));if(c.length<2||c.length!==i.length)throw new RangeError("'x' and 'y' must have the same number of levels (minimum 2)");let a=new Map(c.map((m,u)=>[m,u])),l=new Map(i.map((m,u)=>[m,u])),f=c.map(()=>new Array(c.length).fill(0));for(let[m,u]of n)f[a.get(m)][l.get(u)]++;return f}var Yl=["ratio","alternative","confLevel"],Vo=2**-1022;function Za(e,t,n={}){if(C(n,Yl,"varTest"),typeof e=="string"){let N=Dt(e,t);return Za(N.x,N.y,n)}let r=Array.from(e).filter(Number.isFinite),o=Array.from(t).filter(Number.isFinite),s=n.ratio??1;if(!(Number.isFinite(s)&&s>0))throw new RangeError("'ratio' must be a single positive number");let c=fe(n.alternative),i=n.confLevel??.95;Be(i);let a=r.length-1,l=o.length-1;if(a<1)throw new Error("not enough 'x' observations");if(l<1)throw new Error("not enough 'y' observations");let[f,m]=_e([r,o]).variance,u=0;if(!(f>=Vo&&m>=Vo)){let N=_e([r]),b=_e([o]);f=N.variance[0],m=b.variance[0],u=2*(N.k-b.k)}if(f===0&&m===0)throw new RangeError("both samples have zero variance");let p=u===0?f/m:De(f/m,u),d=p/s,g=we(d,a,l,!0,!1),w=we(d,a,l,!1,!1),y,v;if(c==="less")y=g,v=[0,p/At(1-i,a,l,!0,!1)];else if(c==="greater")y=w,v=[p/At(i,a,l,!0,!1),1/0];else{y=Math.min(1,2*Math.min(g,w));let N=(1-i)/2,b=N<1e-8?At(N,a,l,!1,!1):At(1-N,a,l,!0,!1);v=[p/b,p/At(N,a,l,!0,!1)]}return{method:"F test to compare two variances",alternative:c,statistic:d,statisticName:"F",parameter:[a,l],parameterName:["num df","denom df"],pValue:y,confInt:v,confLevel:i,estimate:p,estimateNames:"ratio of variances",nullValue:s,nullValueName:"ratio of variances",warnings:[]}}function Ja(e,t){if(typeof e=="string"){let d=Xn(e,t);return Ja(ha(d.values,d.group))}let n=e,r=n.length;if(r<2)throw new RangeError("'x' must be a list with at least 2 elements");let o=n.map(d=>Array.from(d).filter(Number.isFinite));if(o.some(d=>d.length<2))throw new RangeError("there must be at least 2 observations in each group");let s=o.map(d=>d.length-1),c=_e(o).variance,i=0,a=0,l=0,f=0;for(let d=0;d<r;d++)i+=s[d],a+=s[d]*c[d],l+=s[d]*Math.log(c[d]),f+=1/s[d];a/=i;let m=i*Math.log(a)-l;if(c.some(d=>d<Vo)){let d=o.map(b=>{let h=_e([b]);return{w:h.variance[0],e:h.k}}),g=Math.max(...d.map(b=>b.e)),w=d.map(b=>Math.log(b.w)+2*(b.e-g)*Math.LN2),y=w.map((b,h)=>Math.log(s[h])+b),v=Math.max(...y),N=v+Math.log(y.reduce((b,h)=>b+Math.exp(h-v),0))-Math.log(i);m=i*N-w.reduce((b,h,E)=>b+s[E]*h,0)}let u=m/(1+(f-1/i)/(3*(r-1))),p=r-1;return{method:"Bartlett test of homogeneity of variances",statistic:u,statisticName:"Bartlett's K-squared",parameter:p,parameterName:"df",pValue:xe(u,p/2,2,!1,!1),warnings:[]}}var Gl=["alternative"];function zo(e,t,n,r){return{method:e,statistic:n,statisticName:t,parameter:r,parameterName:"df",pValue:xe(n,r/2,2,!1,!1),warnings:[]}}function ei(e){let t=0;for(let n of ut(e))t+=n*n*n-n;return t}function Yr(e,t){if(typeof e=="string"){let l=Xn(e,t);return Yr(l.values,l.group.codes)}let n=en(e,t,"error"),r=n.length;if(r<2)throw new RangeError("all observations are in the same group");let o=n.flat(),s=o.length,c=Ne(o),i=0,a=0;for(let l of n){let f=0;for(let m=0;m<l.length;m++)f+=c[a+m];a+=l.length,i+=f*f/l.length}return i=(12*i/(s*(s+1))-3*(s+1))/(1-ei(o)/(s*s*s-s)),zo("Kruskal-Wallis rank sum test","Kruskal-Wallis chi-squared",i,r-1)}function $o(e,t){if(typeof e=="string")return $o(To(e,t));let n=Co(e),r=n.length,o=e[0]?.length??0;if(r<2||o<2)throw new RangeError("'y' must have at least two complete rows and two columns");let s=new Array(o).fill(0),c=0;for(let l of n){let f=Ne(l);for(let m=0;m<o;m++)s[m]+=f[m];c+=ei(l)}let i=0;for(let l of s)i+=(l-r*(o+1)/2)**2;let a=12*i/(r*o*(o+1)-c/(o-1));return zo("Friedman rank sum test","Friedman chi-squared",a,o-1)}function Hl(e){if(e.some(t=>t.some(n=>!Number.isNaN(n)&&n!==0&&n!==1)))throw new RangeError("the responses must be 0 or 1 (or NaN)");return{...$o(e),method:"Cochran's Q test",statisticName:"Q"}}function ti(e,t){if(typeof e=="string")return ti(To(e,t));let n=Co(e),r=n.length,o=e[0]?.length??0;if(n.length<2||o<2)throw new RangeError("'y' must have at least two complete rows and two columns");let s=n.map(g=>ge(g)-Je(g));s.some(g=>g===1/0)&&n.every(g=>g.every(Number.isFinite))&&(s=ee(n).groups.map(g=>ge(g)-Je(g)));let c=Ne(s),i=new Array(o).fill(0),a=0;n.forEach((g,w)=>{let y=Ne(g);for(let v=0;v<o;v++){let N=c[w]*(y[v]-(o+1)/2);a+=N*N,i[v]+=N}});let l=0;for(let g of i)l+=g*g;l/=r;let f=o-1,m=(r-1)*(o-1),u,p,d=[f,m];if(a===l){u=NaN,d=[NaN,NaN];let g=0;for(let w=2;w<=o;w++)g+=Math.log(w);p=Math.exp((1-r)*g)}else u=(r-1)*l/(a-l),p=we(u,f,m,!1,!1);return{method:"Quade test",statistic:u,statisticName:"Quade F",parameter:d,parameterName:["num df","denom df"],pValue:p,warnings:[]}}function ni(e,t){if(typeof e=="string"){let p=Xn(e,t);return ni(p.values,p.group.codes)}let n=en(e,t,"error"),r=n.length;if(r<2)throw new RangeError("all observations are in the same group");let o=p=>p.flatMap(d=>{let g=Ur(d);return d.map(w=>Math.abs(w-g))}),s=o(n);if(s.some(Number.isNaN))throw new RangeError("a group has an infinite median: its deviations are undefined");s.some(p=>p===1/0)&&n.every(p=>p.every(Number.isFinite))&&(s=o(ee(n).groups));let c=s.length,a=Ne(s).map(p=>Z((1+p/(c+1))/2)),l=0;for(let p of a)l+=p;let f=l/c,m=0,u=0;for(let p of n){let d=0;for(let g=0;g<p.length;g++)d+=a[u+g];u+=p.length,m+=p.length*(d/p.length-f)**2}return m/=qe(a),zo("Fligner-Killeen test of homogeneity of variances","Fligner-Killeen:med chi-squared",m,r-1)}function ri(e,t,n={}){if(C(n,Gl,"moodTest"),typeof e=="string"){let b=Dt(e,t);return ri(b.x,b.y,n)}let r=Array.from(e).filter(Number.isFinite),o=Array.from(t).filter(Number.isFinite),s=fe(n.alternative),c=r.length,i=o.length,a=c+i;if(a<3)throw new RangeError("not enough observations");if(c===0||i===0)throw new RangeError("not enough observations in each sample");let l=r.concat(o),f=Array.from({length:a},(b,h)=>h).sort((b,h)=>l[b]-l[h]),m=new Array(a),u=0;for(;u<a;){let b=u;for(;b+1<a&&l[f[b+1]]===l[f[u]];)b++;let h=0;for(let E=u;E<=b;E++)h+=(E+1-(a+1)/2)**2;h/=b-u+1;for(let E=u;E<=b;E++)m[f[E]]=h;u=b+1}let p=0;for(let b=0;b<c;b++)p+=m[b];let d=0;for(let b of m)d+=b;d/=a;let g=0;for(let b of m)g+=(b-d)**2;let w=c*d,y=c*i/(a*(a-1))*g,v=(p-w)/Math.sqrt(y),N=s==="less"?Y(v):s==="greater"?Y(v,{lowerTail:!1}):2*Y(-Math.abs(v));return{method:"Mood two-sample test of scale",alternative:s,statistic:v,statisticName:"Z",pValue:N,warnings:[]}}function tn(e,t){let n=0;for(let r=e.length-1;r>=0;r--)n=n*t+e[r];return n}var Xl=[0,.221157,-.147981,-2.07119,4.434685,-2.706056],Bl=[0,.042981,-.293762,-1.752461,5.682633,-3.582633],Ql=[.544,-.39978,.025054,-6714e-7],Zl=[1.3822,-.77857,.062767,-.0020322],Jl=[-1.5861,-.31082,-.083751,.0038915],ef=[-.4803,-.082676,.0030302],tf=[-2.273,.459];function nf(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]=-Z((m+1-.375)/(e+.25)),o+=r[m]*r[m];o*=2;let s=Math.sqrt(o),c=1/Math.sqrt(e),i=r[0]/s+tn(Xl,c),a=new Float64Array(n);a[0]=i;let l,f;if(e>5){let m=r[1]/s+tn(Bl,c);a[1]=m,l=2,f=Math.sqrt((o-2*r[0]*r[0]-2*r[1]*r[1])/(1-2*i*i-2*m*m))}else l=1,f=Math.sqrt((o-2*r[0]*r[0])/(1-2*i*i));for(let m=l;m<n;m++)a[m]=r[m]/f;for(let m=0;m<n;m++)t[e-1-m]=a[m],t[m]=-a[m];return t}function oi(e){let t=ee([e]).groups[0],n=t.length,r=nf(n),o=t[n-1]-t[0],s=0;for(let f of t)s+=f/o;s/=n;let c=0,i=0,a=0;for(let f=0;f<n;f++){let m=t[f]/o-s;c+=r[f]*r[f],i+=m*m,a+=r[f]*m}let l=Math.sqrt(c*i);return(l-a)*(l+a)/(c*i)}var rf=1.90985931710274,of=1.0471975511966;function si(e,t){if(Number.isNaN(e)||!(t>=3))return NaN;if(t===3){let c=rf*(Math.asin(Math.sqrt(1-e))-of);return Math.max(c,0)}let n=Math.log(e);if(t<=11){let c=tn(tf,t);if(n>=c)return 1e-99;let i=tn(Ql,t),a=Math.exp(tn(Zl,t));return Y(-Math.log(c-n),{mean:i,sd:a,lowerTail:!1})}let r=Math.log(t),o=tn(Jl,r),s=Math.exp(tn(ef,r));return Y(n,{mean:o,sd:s,lowerTail:!1})}var li=Math.log(2*Math.PI),ai=2e-14,ii=500;function sf(e){return Math.exp(-.5*e*e-.5*li)}function af(e){return Y(e,{logP:!0})}function fi(e,t=-1/0,n=!1){let r=t===-1/0?Ro(e,0,2,0,ai,ii):Ro(e,t,1,0,ai,ii),o=r.ier===1||r.ier===3||r.ier===5;return n&&o&&!(r.abserr<=1e-6*Math.abs(r.result))?NaN:r.result}function ci(e,t,n){let r=t.map(Math.sqrt),o=t.map(c=>Math.sqrt(1-c));return fi(c=>{let i=0;for(let a=0;a<r.length;a++){let l=(r[a]*c+e)/o[a];if(!n){i+=af(l);continue}let f=(r[a]*c-e)/o[a],m=Y(f)+Y(l,{lowerTail:!1});m<.5?i+=Math.log1p(-m):i+=Math.log(Math.max(0,l+f<0?Y(l)-Y(f):Y(f,{lowerTail:!1})-Y(l,{lowerTail:!1})))}return sf(c)*-Math.expm1(i)})}function nn(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,ci(e,t,r));let o=n/2,s=Math.log(2)+.5*Math.log(o)-.5*li-Us(o);return Math.min(1,fi(i=>{let a=s-o*(Math.expm1(2*i)-2*i);return a<-745?0:Math.exp(a)*ci(e*Math.exp(i),t,r)},-1/0,!0))}var cf=["pAdjustMethod"],lf=["pAdjustMethod","alternative"],ff=["alternative"],mf=["pAdjustMethod","alternative"],uf=["method","altp"],pf=["alternative"],mi="Ties are present. z-quantiles were corrected for ties.";function kt(e,t){let{labels:n,groups:r}=Vt(e,t),o=r.length;if(o<2)throw new RangeError("there must be at least two groups");let s=r.map(c=>c.length);return{labels:n,groups:r,n:s,N:s.reduce((c,i)=>c+i,0),k:o}}function Wo(e){let t=e.groups.flat(),n=Ne(t),r=[],o=0;for(let c of e.n){let i=0;for(let a=0;a<c;a++)i+=n[o+a];o+=c,r.push(i/c)}let s=0;for(let c of ut(t))s+=c*c*c-c;return{R:r,C:s/(12*(e.N-1))}}function nr(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 hf(e,t){let n=e.length+1,r=[];for(let i=0;i<n-1;i++)for(let a=i+1;a<n;a++)r.push(e[a-1][i]);let o=Rn(r,t),s=0,c=e.map(i=>i.slice());for(let i=0;i<n-1;i++)for(let a=i+1;a<n;a++)c[a-1][i]=o[s++];return c}function Hr(e,t,n,r,o,s=[]){return{method:t,statistic:n,pValue:r,rowNames:e.labels.slice(1),colNames:e.labels.slice(0,-1),pAdjustMethod:o,warnings:s}}function jo(e,t,n,r,o,s=[]){return{method:t,statistic:n.map(c=>[c]),pValue:r.map(c=>[c]),rowNames:e.labels.slice(1),colNames:e.labels.slice(0,1),pAdjustMethod:o,warnings:s}}var Gr=e=>Y(e,{lowerTail:!1}),Ko=(e,t,n)=>Ar(Math.abs(e),t,n,{lowerTail:!1});function Uo(e){let t=_e(e.groups),n=0;for(let r of t.n)r>1&&(n+=r-1);return rr(n,t.ssWithin),{m:t.mean,s2:t.ssWithin/n,df:e.N-e.k,k:t.k}}function rr(e,t){if(e===0)throw new RangeError("no residual degrees of freedom");if(t===0)throw new RangeError("the pooled within-group variance is 0")}function df(e,t,n={}){C(n,cf,"kwAllPairsDunnTest");let r=n.pAdjustMethod??"holm",o=kt(e,t),{R:s,C:c}=Wo(o),i=o.N*(o.N+1)/12-c,a=nr(o.k,(f,m)=>Math.abs(s[f]-s[m])/Math.sqrt(i*(1/o.n[f]+1/o.n[m]))),l=hf(a.map(f=>f.map(m=>2*Gr(m))),r);return j("kwAllPairsDunnTest",Hr(o,"Dunn's all-pairs test",a,l,r,c!==0?[mi]:[]))}function gf(e,t,n={}){C(n,lf,"kwManyOneDunnTest");let r=fe(n.alternative),o=n.pAdjustMethod??"single-step",s=kt(e,t),{R:c,C:i}=Wo(s),a=s.N*(s.N+1)/12-i,l=s.n.slice(1).map((p,d)=>d+1),f=l.map(p=>(c[p]-c[0])/Math.sqrt(a*(1/s.n[0]+1/s.n[p]))),m=l.map(p=>s.n[p]/(s.n[p]+s.n[0])),u=ui(f,m,r,o);return j("kwManyOneDunnTest",jo(s,"Dunn's many-to-one test",f,u,o,i!==0?[mi]:[]))}function ui(e,t,n,r){return r==="single-step"?e.map(o=>n==="two.sided"?nn(o,t,1/0,!0):nn(n==="greater"?o:-o,t,1/0,!1)):Rn(e.map(o=>n==="two.sided"?2*Gr(Math.abs(o)):Gr(n==="greater"?o:-o)),r)}function wf(e,t,n={}){C(n,ff,"dunnettTest");let r=fe(n.alternative),o=kt(e,t),{m:s,s2:c,df:i}=Uo(o),a=Math.sqrt(c),l=o.n.slice(1).map((p,d)=>d+1),f=l.map(p=>(s[p]-s[0])/(a*Math.sqrt(1/o.n[p]+1/o.n[0]))),m=l.map(p=>o.n[p]/(o.n[p]+o.n[0])),u=f.map(p=>r==="two.sided"?Math.min(1,2*nn(Math.abs(p),m,i,!1)):nn(r==="greater"?p:-p,m,i,!1));return j("dunnettTest",jo(o,`Dunnett's-test for multiple
3
- comparisons with one control`,f,u,"single-step"))}function bf(e,t,n={}){C(n,mf,"manyOneUTest");let r=fe(n.alternative),o=n.pAdjustMethod??"single-step",s=kt(e,t);if(o==="single-step"&&s.k<3)throw new RangeError(`for 'p.adjust.method = single-step' at least
1
+ /*! rStats.js 1.2.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{B as Yr,C as Hr,D as Xr,b as _t,c as Jn,d as Nt,f as at,k as xn,l as Ya,p as Ha,q as Tn,r as Xa,v as Gr,w as Ba,x as rr,z as Qa}from"./chunks/chunk-4AOTAT3O.js";import{a as Qr,b as Yt,c as ar,d as ti,e as ni,f as Xo,g as le,h as Ht,i as Ft,j as Xe,k as ai,l as Nf,n as no,o as cr}from"./chunks/chunk-47IIPUFN.js";import"./chunks/chunk-F3EQLL4F.js";import{a as ri,c as oi,d as si,e as ir,g as lr}from"./chunks/chunk-5SGTWSCB.js";import{b as Ho}from"./chunks/chunk-EEU7D4RU.js";import"./chunks/chunk-NSU6U3ET.js";import{a as ze,b as Sn,c as kn}from"./chunks/chunk-7INBXTVV.js";import{a as Ga,d as jr}from"./chunks/chunk-T5DRIITM.js";import{a as ln,b as In,c as Jr,d as eo,e as to,f as _n,g as ui,h as hi}from"./chunks/chunk-HBHMEPGY.js";import{A as Ot,g as Pr,h as Vr,j as $r,k as yn,p as Me,q as Lt,s as an,t as jt,v as Ut,z as ht}from"./chunks/chunk-A66M3ZEA.js";import{C as fi,E as mi,H as dt,I as pi,c as ii,h as ci,j as Ee,l as li,m as An,n as qn,t as Zr,y as Ke}from"./chunks/chunk-JWBSEL3V.js";import{a as Br,b as Ja,g as ei}from"./chunks/chunk-L23RSC2N.js";import{A as Ur,B as cn,i as Ne,l as Dr,x as De,y as st}from"./chunks/chunk-RRO4LMCB.js";import{a as pt}from"./chunks/chunk-IAWXF73N.js";import{a as vf,c as z,d as we,e as ke,f as or}from"./chunks/chunk-73LNRWU3.js";import{a as sr,b as Rn,c as Za,d as Pe,e as Ve}from"./chunks/chunk-BI7NYJWP.js";import{a as ve,b as Gt,c as it}from"./chunks/chunk-IMPRIQE5.js";import"./chunks/chunk-ORE5ZRCY.js";import{b as Ca,c as Da}from"./chunks/chunk-2NNBUYHJ.js";import{A as La,B as Oa,D as Fa,M as Pa,q as Ze,r as j,s as Z}from"./chunks/chunk-7WG7X2UI.js";import{A as Ka,C as Wa,D as Le,E as zr,F as ja,G as Kr,H as Ua,I as se,J as ye,K as vt,L as Je,M as En,N as Ce,O as Wr,P as ee,Q as Yo,b as He,c as xt,d as G,e as Ia,f as ie,g as ce,h as Se,i as ge,j as Ie,k as _a,l as C,m as Wt,p as er,q as tr,r as Va,s as $a,t as za,u as _,v as $e,w as nr,x as qe,y as _e}from"./chunks/chunk-TNETWBF5.js";var di=["alternative","mu","paired","varEqual","confLevel"];function Xt(e,t,n={}){if(typeof e!="string"&&ze(t)&&C(t,di,"tTest"),C(n,di,"tTest"),typeof e=="string")return Mf(e,t,n);let r;t!==void 0&&!ArrayBuffer.isView(t)&&!Array.isArray(t)?n=t:r=t;let o=le(n.alternative),s=n.mu??0,i=n.paired??!1,c=n.varEqual??!1,a=n.confLevel??.95;if(Number.isNaN(s))throw new RangeError("mu must be a number");if(Ht(a),i&&r===void 0)throw new TypeError("y is required when paired is true");let l,f,m;if(i){let[k,A]=En(Array.from(e),Array.from(r));({d:l,k:m}=zr(k,A))}else{let k=ee(r===void 0?[Je(Array.from(e))]:[Je(Array.from(e)),Je(Array.from(r))]);m=k.k,l=k.groups[0],f=k.groups[1]}let u=_(s,-m),h,p,g,w,b,N,v,y=l.length,d=f!==void 0&&c;if(y<1||!d&&y<2)throw new Error("not enough 'x' observations");let M=qe(l),x=y>1?_e(l):NaN;if(f===void 0){if(p=y-1,g=Math.sqrt(x/y),g===0||g<10*Number.EPSILON*Math.abs(M))throw new Error("data are essentially constant");h=(M-u)/g,N=i?"Paired t-test":"One Sample t-test",w=M,b=i?"mean difference":"mean of x",v=i?"mean difference":"mean"}else{let k=f.length;if(k<1||!c&&k<2)throw new Error("not enough 'y' observations");if(c&&y+k<3)throw new Error("not enough observations");let A=qe(f),q=k>1?_e(f):NaN;if(N=`${c?"":"Welch"} Two Sample t-test`,w=[M,A],b=["mean of x","mean of y"],v="difference in means",c){p=y+k-2;let I=0;y>1&&(I+=(y-1)*x),k>1&&(I+=(k-1)*q),I/=p,g=Math.sqrt(I*(1/y+1/k))}else{let I=x/y,L=q/k;g=Math.sqrt(Math.sqrt(I)**2+Math.sqrt(L)**2),p=Kr([I,L],[y-1,k-1])}if(g===0||g<10*Number.EPSILON*Math.max(Math.abs(M),Math.abs(A)))throw new Error("data are essentially constant");h=(M-A-u)/g}let E=typeof w=="number"?w:w[0]-w[1],T=(k,A)=>{let q=u+(h+A*k)*g;return Number.isFinite(q)&&Math.max(Math.abs(u),Math.abs(h*g))<=16*(Math.abs(E)+k*g)?q:E+A*k*g},R,S;if(o==="less")R=De(h,p,!0,!1),S=[-1/0,T(st(a,p,!0,!1),1)];else if(o==="greater")R=De(h,p,!1,!1),S=[T(st(a,p,!0,!1),-1),1/0];else{R=2*De(-Math.abs(h),p,!0,!1);let k=1-a,A=k/2<1e-8?st(k/2,p,!1,!1):st(1-k/2,p,!0,!1);S=[T(A,-1),T(A,1)]}return S=[_(S[0],m),_(S[1],m)],w=typeof w=="number"?_(w,m):w.map(k=>_(k,m)),g=_(g,m),{method:N,alternative:o,statistic:h,statisticName:"t",parameter:p,parameterName:"df",pValue:R,confInt:S,confLevel:a,estimate:w,estimateNames:b,nullValue:s,nullValueName:v,stdErr:g,stdError:g,warnings:[]}}function Mf(e,t,n){if(n.paired!==void 0)throw new Error("cannot use 'paired' in formula method");let r=Qr(e,t,!0);if(r.kind==="one")return Xt(r.x,n);if(r.kind==="paired")return Xt(r.x,r.y,{...n,paired:!0});let[o,s]=r.levels;return{...Xt(r.x,r.y,n),estimateNames:[`mean in group ${o}`,`mean in group ${s}`],nullValueName:`difference in means between group ${o} and group ${s}`}}var yf=new Set(["n","delta","sd","sigLevel","power","type","alternative","strict","tol"]),Ef=["n","p1","p2","sigLevel","power","alternative","strict","tol"],xf=["groups","n","betweenVar","withinVar","sigLevel","power"],wi=Number.EPSILON**.25;function ae(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 H=e=>e==null;function ct(e,t,n){if(!H(e)&&!t(e))throw new RangeError(n)}var fr=e=>e>=0&&e<=1,ro=e=>e>0&&e<1/0;function gt(e,t){if(e.filter(H).length!==1)throw new RangeError(t)}function oe(e,t,n,r={}){return si(e,t,n,r).root}var fn=(e,t,n,r)=>Ur(e,t,{ncp:n,lowerTail:r});function Bo(e,t){if(e!=null&&!(typeof e=="number"&&e>=0&&e<=1))throw new RangeError(t)}var Qo="'sig.level' must be numeric in [0, 1]";function Tf(e={}){C(e,yf,"powerTTest");let{n:t,delta:n,power:r}=e,o=e.sd===void 0?1:e.sd,s=e.sigLevel===void 0?.05:e.sigLevel;gt([t,n,o,r,s],"exactly one of 'n', 'delta', 'sd', 'power', and 'sig.level' must be NULL"),Bo(s,Qo),ct(r,fr,"'power' must be a probability"),ct(o,ro,"'sd' must be a positive finite number"),ct(t,g=>g>=2,"number of observations in each group must be at least 2"),ct(n,g=>!Number.isNaN(g),"'delta' must not be NaN"),ct(e.tol,ro,"'tol' must be a positive finite number");let i=ae(e.type,["two.sample","one.sample","paired"]),c=ae(e.alternative,["two.sided","one.sided"]),a=e.strict??!1,l=e.tol??wi,f=i==="two.sample"?2:1,m=c==="two.sided"?2:1;m===2&&!H(n)&&(n=Math.abs(n));let u=(g,w,b,N)=>{let v=Math.max(1e-7,g-1)*f,y=cn(N/m,v,{lowerTail:!1}),d=Math.sqrt(g/f)*w/b;Number.isFinite(d)||(d=Math.sqrt(g/f)*(w/b));let M=fn(y,v,d,!1);return a&&m===2?M+fn(-y,v,d,!0):M};if(H(r))r=u(t,n,o,s);else{let g=r;if(H(t)){if(n===0)throw new RangeError("'n' cannot be determined when 'delta' is 0: the power does not depend on it");t=oe(w=>u(w,n,o,s)-g,2,1e7,{tol:l,extendInt:"upX"})}else if(H(o)){if(n===0)throw new RangeError("'sd' cannot be determined when 'delta' is 0: the power does not depend on it");let w=Math.abs(n);o=oe(b=>u(t,n,b,s)-g,w*1e-7,w*1e7,{tol:l,extendInt:n>0?"downX":"upX"})}else H(n)?n=oe(w=>u(t,w,o,s)-g,o*1e-7,o*1e7,{tol:l,extendInt:"upX"}):s=oe(w=>u(t,n,o,w)-g,1e-10,1-1e-10,{tol:l,extendInt:"yes"})}let h=i==="paired"?"n is number of *pairs*, sd is std.dev. of *differences* within pairs":i==="two.sample"?"n is number in *each* group":void 0,p={"two.sample":"Two-sample","one.sample":"One-sample",paired:"Paired"}[i]+" t test power calculation";return{n:t,delta:n,sd:o,sigLevel:s,power:r,alternative:c,...h===void 0?{}:{note:h},method:p,warnings:[]}}function Rf(e={}){C(e,Ef,"powerPropTest");let{n:t,p1:n,p2:r,power:o}=e,s=e.sigLevel===void 0?.05:e.sigLevel;gt([t,n,r,o,s],"exactly one of 'n', 'p1', 'p2', 'power', and 'sig.level' must be NULL"),Bo(s,Qo),ct(o,fr,"'power' must be a probability"),ct(n,fr,"'p1' must be a probability"),ct(r,fr,"'p2' must be a probability"),ct(t,u=>u>0,"number of observations in each group must be positive"),ct(e.tol,ro,"'tol' must be a positive finite number");let i=ae(e.alternative,["two.sided","one.sided"]),c=e.strict??!1,a=e.tol??wi,l=i==="two.sided"?2:1,f=(u,h,p,g)=>{let w=Z(g/l,{lowerTail:!1});if(c&&l===2){let b=(h+p)/2,N=h*(1-h),v=p*(1-p),y=b*(1-b),d=Math.abs(h-p),M=Math.sqrt(N+v);return j((Math.sqrt(u)*d-w*Math.sqrt(2*y))/M)+j((Math.sqrt(u)*d+w*Math.sqrt(2*y))/M,{lowerTail:!1})}return j((Math.sqrt(u)*Math.abs(h-p)-w*Math.sqrt((h+p)*(1-(h+p)/2)))/Math.sqrt(h*(1-h)+p*(1-p)))},m=[];if(H(o))o=f(t,n,r,s);else{let u=o;if(H(t)){if(n===r)throw new RangeError("'n' cannot be determined when 'p1' equals 'p2': the power does not depend on it");t=oe(h=>f(h,n,r,s)-u,1,1e7,{tol:a,extendInt:"upX"})}else if(H(n)){if(r===0)throw new RangeError("'p1' cannot be determined when 'p2' is 0: no p1 in [0, p2] differs from p2");n=oe(h=>f(t,h,r,s)-u,0,r,{tol:a,extendInt:"yes"}),n<0&&m.push("No p1 in [0, p2] can be found to achieve the desired power")}else if(H(r)){if(n===1)throw new RangeError("'p2' cannot be determined when 'p1' is 1: no p2 in [p1, 1] differs from p1");r=oe(h=>f(t,n,h,s)-u,n,1,{tol:a,extendInt:"yes"}),r>1&&m.push("No p2 in [p1, 1] can be found to achieve the desired power")}else s=oe(h=>f(t,n,r,h)-u,1e-10,1-1e-10,{tol:a,extendInt:"upX"}),(s<0||s>1)&&m.push("No significance level [0, 1] can be found to achieve the desired power")}return z("powerPropTest",{n:t,p1:n,p2:r,sigLevel:s,power:o,alternative:i,note:"n is number in *each* group",method:"Two-sample comparison of proportions power calculation",warnings:m})}var gi=1e16,oo=(e,t)=>e>gi&&e<1/0&&t>gi;function so(e,t,n,r){let o=Z(r,{lowerTail:!1}),s=n/e,i=Math.sqrt(2/e+2/t),c=Math.hypot(Math.sqrt(2*(1+2*s)/e),Math.sqrt(2/t)*(1+s));return j((o*i-s)/c,{lowerTail:!1})}function Sf(e,t,n,r){if(n===0)return 0;let o=l=>Math.floor(Math.log2(l)),[s,i,c,a]=[o(e),o(t),o(n),o(r)];return _(_(e,-s)*_(t,-i)*_(n,-c)/_(r,-a),s+i+c-a)}function kf(e={}){C(e,xf,"powerAnovaTest");let{groups:t,n,betweenVar:r,withinVar:o,power:s}=e,i=e.sigLevel===void 0?.05:e.sigLevel;if(gt([t,n,r,o,s,i],"exactly one of 'groups', 'n', 'between.var', 'within.var', 'power', and 'sig.level' must be NULL"),!H(t)&&!(t>=2))throw new RangeError("number of groups must be at least 2");if(!H(n)&&!(n>=2))throw new RangeError("number of observations in each group must be at least 2");Bo(i,Qo),ct(s,fr,"'power' must be a probability"),ct(r,a=>a>=0&&a<1/0,"'betweenVar' must be a nonnegative finite number"),ct(o,ro,"'withinVar' must be a positive finite number");let c=(a,l,f,m,u)=>{let h=(a-1)*l*f/m;h<1/0||(h=(a-1)*l*(f/m)),!(h<1/0)&&Number.isFinite(a)&&Number.isFinite(l)&&(h=Sf(a-1,l,f,m));let p=a-1,g=(l-1)*a;return h===1/0?1:oo(p,g)?so(p,g,h,u):an(jt(u,p,g,{lowerTail:!1}),p,g,{ncp:h,lowerTail:!1})};if(H(s))s=c(t,n,r,o,i);else{let a=s;if(H(t))t=oe(l=>c(l,n,r,o,i)-a,2,100);else if(H(n)){if(r===0)throw new RangeError("'n' cannot be determined when 'betweenVar' is 0: the power does not depend on it");n=oe(l=>c(t,l,r,o,i)-a,2,1e5)}else if(H(o)){if(r===0)throw new RangeError("'withinVar' cannot be determined when 'betweenVar' is 0: the power does not depend on it");o=oe(l=>c(t,n,r,l,i)-a,r*1e-7,r*1e7)}else H(r)?r=oe(l=>c(t,n,l,o,i)-a,o*1e-7,o*1e7):i=oe(l=>c(t,n,r,o,l)-a,1e-10,1-1e-10)}return{groups:t,n,betweenVar:r,withinVar:o,sigLevel:i,power:s,note:"n is number in each group",method:"Balanced one-way analysis of variance power calculation",warnings:[]}}var Af=["correct","p","rescaleP"],qf=["confLevel"],If=["alternative","rowScores","colScores"];function et(e){let t=[0,0];for(let n of e)t=G(t,[n,0]);return t}function Jo(e){let t=e.map(r=>{if(typeof r!="object"||r===null||typeof r.length!="number")throw new TypeError("'x' must be an array of numbers or an array of rows");return Array.from(r)}),n=t[0].length;if(t.some(r=>r.length!==n))throw new TypeError("all rows of the matrix must have the same length");if(t.flat().some(r=>typeof r!="number"))throw new TypeError("the entries of 'x' must be numbers");return t}var es=e=>e.length>0&&typeof e[0]!="number",ts=(e,t)=>Number.isNaN(e)?NaN:Ne(e,t/2,2,!1,!1);function bi(e,t){if(!(e>=0&&t>=0&&e<1/0&&t<1/0))return NaN;let n=Math.sqrt(e),r=Math.sqrt(e+1),o=n*r+t,s=(e-t)*(e+t);if(Number.isFinite(s)){let c=o===0?0:(s+e)/o;return(2*(e-t)+2*c)/(n+r+Math.sqrt(4*t+1))}let i=(e-t)*((e+t)/o)+e/o;return(.5*(e-t)+.5*i)/((n+r+2*Math.sqrt(t+.25))/4)}function mr(e,t){return e.length>0&&Array.isArray(e[0])?e.map((n,r)=>n.map((o,s)=>bi(o,t[r][s]))):e.map((n,r)=>bi(n,t[r]))}var _f=["none","williams","yates"],Ni="Log likelihood ratio (G-test)";function Lf(e,t={}){C(t,Af,"gTest");let n=ae(t.correct,_f);if(typeof e!="object"||e===null||typeof e.length!="number")throw new TypeError("'x' must be an array of counts or of rows");if(es(e)){let o=Jo(e);return vi(o.flat()),o.length===1?z("gTest",Zo(o[0],n,t)):o[0].length===1?z("gTest",Zo(o.map(s=>s[0]),n,t)):z("gTest",Of(o,n))}let r=Array.from(e);if(r.some(o=>typeof o!="number"))throw new TypeError("the entries of 'x' must be numbers");return vi(r),z("gTest",Zo(r,n,t))}function vi(e){if(e.some(t=>!(t>=0&&t<1/0)))throw new RangeError("all entries of 'x' must be nonnegative and finite");if(!e.some(t=>t>0))throw new RangeError("at least one entry of 'x' must be positive")}function Mi(e,t){return e<1/0?0:Math.ceil(Math.log2(t))+1}function yi(e,t){if(e.some(r=>r<0))return NaN;let n=[0,0];for(let r=0;r<e.length;r++){let o=e[r],s=t[r];if(o>0&&s[0]===0)return 1/0;n=G(n,Oa(o,s))}return n[0]+n[1]}function Of(e,t){let n=e.length,r=e[0].length;if(t==="yates"&&(n!==2||r!==2))throw new RangeError("Yates' correction requires a 2 x 2 matrix");let o=et(e.flat()),s=Mi(o[0],n*r),i=s===0?e:e.map(v=>v.map(y=>_(y,-s))),c=s===0?o:et(i.flat()),a=i.map(v=>et(v)),l=i[0].map((v,y)=>et(i.map(d=>d[y]))),f=a.map(v=>l.map(y=>ce(v,ge(y,c)))),m=i;if(t==="yates"){let[[v,y],[d,M]]=i,x=v*M-y*d>0?-.5:.5,E=_(.5,-s),T=x<0?-E:E;m=[[v+T,y-T],[d-T,M+T]]}let u=yi(m.flat(),f.flat()),h=Number.isNaN(u)?["NaNs produced"]:[],p=1;if(t==="williams"){if(a.some(M=>M[0]===0)||l.some(M=>M[0]===0))throw new RangeError("Williams' correction is not defined for a table with an empty row or column");let v=c[0]+c[1],y=a.reduce((M,x)=>M+1/(x[0]+x[1]),0),d=l.reduce((M,x)=>M+1/(x[0]+x[1]),0);p=1+_((v*y-1)*(v*d-1)/(6*v*(n-1)*(r-1)),-s)}let g=_(2*u,s)/p,w=(n-1)*(r-1),b=f.map(v=>v.map(y=>_(y[0]+y[1],s)));return{method:`${Ni} test of independence ${t==="none"?"without correction":t==="williams"?"with Williams' correction":"with Yates' correction"}`,statistic:g,statisticName:"G",parameter:w,parameterName:"X-squared df",pValue:ts(g,w),observed:s===0?m:m.map(v=>v.map(y=>_(y,s))),expected:b,ftResiduals:mr(e,b),warnings:h}}function Zo(e,t,n){let r=e.length;if(r<2)throw new RangeError("'x' must have at least 2 elements");if(t==="yates"&&r!==2)throw new RangeError("Yates' correction requires 2 data values");let o=n.rescaleP??!1;if(typeof o!="boolean")throw new TypeError("'rescaleP' must be a boolean");let s=n.p===void 0?e.map(()=>1/r):Array.from(n.p);if(s.length!==r)throw new RangeError("'x' and 'p' must have the same number of elements");if(s.some(v=>typeof v!="number"||Number.isNaN(v)))throw new RangeError("'p' must not contain missing values");if(s.some(v=>v<0))throw new RangeError("probabilities must be non-negative");if(s.some(v=>v===1/0))throw new RangeError("probabilities must be finite");let i=et(s);if(o){if(!(i[0]>0&&i[0]<1/0))throw new RangeError("the probabilities to rescale must have a positive finite sum")}else if(Math.abs(i[0]-1+i[1])>Math.sqrt(Number.EPSILON))throw new RangeError("probabilities must sum to 1");let c=et(e),a=Mi(c[0],r),l=a===0?e:e.map(v=>_(v,-a)),f=a===0?c:et(l),m=s.map(v=>o?ge(Se(f,v),i):Se(f,v)),u=m.map(v=>v[0]+v[1]),h=l;if(t==="yates"){let v=_(.5,-a),y=l[0]-u[0];y>_(.25,-a)?h=[l[0]-v,l[1]+v]:-y>_(.25,-a)&&(h=[l[0]+v,l[1]-v])}let p=yi(h,m);if(!o&&!Number.isNaN(p)){let v=f[0]+f[1];p-=v*(i[0]-1+i[1])}let g=Number.isNaN(p)?["NaNs produced"]:[],w=t==="williams"?1+(r+1)/(6*_(f[0]+f[1],a)):1,b=_(2*p,a)/w,N=u.map(v=>_(v,a));return{method:`${Ni} goodness of fit test`,statistic:b,statisticName:"G",parameter:r-1,parameterName:"X-squared df",pValue:ts(b,r-1),observed:a===0?h:h.map(v=>_(v,a)),expected:N,ftResiduals:mr(e,N),warnings:g}}function Ff(e){if(typeof e!="object"||e===null||!es(e))throw new RangeError("'x' must be square with at least two rows and columns");let t=Jo(e),n=t.length;if(n<2||t[0].length!==n)throw new RangeError("'x' must be square with at least two rows and columns");if(t.flat().some(r=>!(r>=0&&r<1/0)))throw new RangeError("all entries of 'x' must be nonnegative and finite");return t}function Cf(e){let t=Ff(e),n=et(t.flat())[0],r=n>2**400||n>0&&n<2**-400?Math.round(Math.log2(n)):0,o=r===0?t:t.map(d=>d.map(M=>_(M,-r))),s=o.length,i=o.map(d=>et(d)),c=o.map((d,M)=>et(o.map(x=>x[M]))),a=o.map((d,M)=>o[M].some((x,E)=>E!==M&&x>0)||o.some((x,E)=>E!==M&&x[M]>0)).flatMap((d,M)=>d?[M]:[]),l=a.length;if(l<2)throw new RangeError("Too many equal marginals, cannot compute");let f=a.map((d,M)=>M),m=d=>f[d]===d?d:f[d]=m(f[d]);for(let d=0;d<l;d++)for(let M=d+1;M<l;M++)o[a[d]][a[M]]+o[a[M]][a[d]]>0&&(f[m(d)]=m(M));if(a.some((d,M)=>m(M)!==m(0)))throw new RangeError("the categories fall into groups without counts between them: the covariance matrix is singular");let u=l-1,h=a.slice(0,u).map(d=>{let M=G(i[d],[-c[d][0],-c[d][1]]);return M[0]+M[1]}),p=a.slice(0,u).map(d=>a.slice(0,u).map(M=>{if(d!==M)return-(o[d][M]+o[M][d]);let x=0;for(let E=0;E<s;E++)E!==d&&(x+=o[d][E]+o[E][d]);return x})),g=p.map(d=>d.map(()=>0)),w=[];for(let d=0;d<u;d++){let M=p[d][d];for(let x=0;x<d;x++)M-=g[d][x]*g[d][x]*w[x];w.push(M);for(let x=d+1;x<u;x++){let E=p[x][d];for(let T=0;T<d;T++)E-=g[x][T]*g[d][T]*w[T];g[x][d]=E/M}}let b=0,N=[];for(let d=0;d<u;d++){let M=h[d];for(let x=0;x<d;x++)M-=g[d][x]*N[x];N.push(M),b+=M*M/w[d]}let v=et(a.flatMap(d=>a.map(M=>t[d][M]))),y=l-1;return b=_(b,r),z("stuartMaxwellTest",{method:"Stuart-Maxwell test",statistic:b,statisticName:"chi-squared",parameter:y,parameterName:"df",pValue:ts(b,y),N:v[0]+v[1],warnings:[]})}function Df(e,t={}){C(t,qf,"goodmanKruskalGamma");let n=t.confLevel??.95;if(!(typeof n=="number"&&n>0&&n<1))throw new RangeError("'confLevel' must be a single number in (0, 1)");let r=Ei(e),o=et(r.flat())[0],s=0;(o>2**250||o>0&&o<2**-250)&&(s=2*Math.round((Math.log2(o)-250)/2));let i=s===0?r:r.map(d=>d.map(M=>_(M,-s))),{pc:c,pd:a}=Pf(i),l=[0,0],f=[0,0];i.forEach((d,M)=>d.forEach((x,E)=>{l=G(l,Se(c[M][E],x)),f=G(f,Se(a[M][E],x))})),l=[l[0]/2,l[1]/2],f=[f[0]/2,f[1]/2];let m=G(l,f),u=ge(ie(l,f),m)[0],h=ce(m,m),p=i.map((d,M)=>d.map((x,E)=>2*ge(ie(ce(f,c[M][E]),ce(l,a[M][E])),h)[0])),g=Math.max(...p.flat().map(Math.abs)),w=g>0&&g<1/0?Math.floor(Math.log2(g)):0,b=0;i.forEach((d,M)=>d.forEach((x,E)=>{let T=_(p[M][E],-w);b+=x*T*T}));let N=_(Math.sqrt(b),w-s/2),v=(1-n)/2,y=v<1e-8?Z(v,{lowerTail:!1}):Z(1-v);return z("goodmanKruskalGamma",{estimate:u,confInt:[Math.max(-1,u-y*N),Math.min(1,u+y*N)],confLevel:n,ase:N,warnings:[]})}function Ei(e){if(typeof e!="object"||e===null||!es(e))throw new TypeError("'table' must be an array of rows");let t=Jo(e);if(t[0].length===0)throw new RangeError("'table' must have at least one column");if(t.flat().some(n=>!(n>=0&&n<1/0)))throw new RangeError("all entries of 'table' must be nonnegative and finite");return t}function Pf(e){let t=e.length,n=e[0].length,r=[0,0],o=e.map(p=>{let g=[],w=r;for(let b=0;b<n;b++)g.push(w),w=G(w,[p[b],0]);return g}),s=e.map(p=>{let g=new Array(n),w=r;for(let b=n-1;b>=0;b--)g[b]=w,w=G(w,[p[b],0]);return g}),i=p=>{let g=[],w=new Array(n).fill(r);for(let b=0;b<t;b++)g.push(w),w=w.map((N,v)=>G(N,p[b][v]));return g},c=p=>{let g=new Array(t),w=new Array(n).fill(r);for(let b=t-1;b>=0;b--)g[b]=w,w=w.map((N,v)=>G(N,p[b][v]));return g},a=i(o),l=c(s),f=i(s),m=c(o),u=a.map((p,g)=>p.map((w,b)=>G(w,l[g][b]))),h=f.map((p,g)=>p.map((w,b)=>G(w,m[g][b])));return{pc:u,pd:h}}function Vf(e,t={}){C(t,If,"lblTest");let n=le(t.alternative),r=Ei(e),o=r.length,s=r[0].length,i=(T,R,S)=>{if(T===void 0)return Array.from({length:R},(A,q)=>q+1);let k=Array.from(T);if(k.length!==R)throw new RangeError(`'${S}' must have one score for each ${S==="rowScores"?"row":"column"}`);if(k.some(A=>typeof A!="number"||!Number.isFinite(A)))throw new RangeError(`'${S}' must be finite numbers`);return k},c=i(t.rowScores,o,"rowScores"),a=i(t.colScores,s,"colScores"),f=et(r.flat())[0]>2**1e3?2*Math.ceil((Math.log2(Math.max(...r.flat()))-990)/2):0,m=f===0?r:r.map(T=>T.map(R=>_(R,-f))),u=m.map(T=>et(T)).map(T=>T[0]+T[1]),h=m[0].map((T,R)=>et(m.map(S=>S[R]))).map(T=>T[0]+T[1]),p=u.reduce((T,R)=>T+R,0);if(!(f>0||p>1))throw new RangeError("the table must have a total above 1");let g=u.reduce((T,R,S)=>T+R*c[S],0)/p,w=h.reduce((T,R,S)=>T+R*a[S],0)/p,b=c.map(T=>T-g),N=a.map(T=>T-w),v=u.reduce((T,R,S)=>T+R*b[S]*b[S],0),y=h.reduce((T,R,S)=>T+R*N[S]*N[S],0),d=0;if(m.forEach((T,R)=>T.forEach((S,k)=>d+=S*b[R]*N[k])),!(v>0&&y>0))throw new RangeError("the row and column scores must vary over the observations (the variance of the statistic is 0)");let M=d/Math.sqrt(v)/Math.sqrt(y),x=f===0?Math.sqrt(p-1)*M:_(Math.sqrt(p-_(1,-f))*M,f/2),E=n==="less"?j(x):n==="greater"?j(x,{lowerTail:!1}):2*j(-Math.abs(x));return z("lblTest",{method:"Linear-by-Linear Association Test",alternative:n,statistic:x,statisticName:"Z",pValue:E,warnings:[]})}var xi=["correct","p","rescaleP","simulatePValue","B","nMinus1"];function wt(e){let t=[0,0],n=0;for(let r of e)t=G(t,[r,0]),n+=r;return Number.isFinite(n)?t[0]:n}function $f(e,t,n){return ze(t)&&C(t,xi,"chisqTest"),C(n,xi,"chisqTest"),Array.isArray(t)?z("chisqTest",zf(e,t,n??{})):z("chisqTest",Kf(e,t??{}))}var Si="Chi-squared approximation may be incorrect";function zf(e,t,n){if(e.length!==t.length)throw new RangeError("'x' and 'y' must have the same length");let r=u=>u==null;for(let u of[...e,...t])if(!r(u)&&typeof u!="string")throw new TypeError("factor values must be strings (or null for missing values)");let o=[],s=[];e.forEach((u,h)=>{let p=t[h];!r(u)&&!r(p)&&(o.push(u),s.push(p))});let i=[...new Set(o)].sort(pt),c=[...new Set(s)].sort(pt);if(i.length<2||c.length<2)throw new RangeError("'x' and 'y' must have at least 2 levels");let a=new Map(i.map((u,h)=>[u,h])),l=new Map(c.map((u,h)=>[u,h])),f=i.map(()=>c.map(()=>0));return o.forEach((u,h)=>f[a.get(u)][l.get(s[h])]++),{..._i(f,n),rowLevels:i,colLevels:c}}function Kf(e,t){if(e.length>0&&typeof e[0]!="number"){let r=e.map(s=>Array.from(s)),o=r[0].length;if(r.some(s=>s.length!==o))throw new TypeError("all rows of the matrix must have the same length");return r.length===1?ns(r[0],t):o===1?ns(r.map(s=>s[0]),t):_i(r,t)}return ns(Array.from(e),t)}function ki(e){let t=e.B??2e3;if(!(Number.isInteger(t)&&t>=1&&t<2**31))throw new RangeError("'B' must be a positive integer");return t}function Ai(e,t,n){let r=(1-64*Number.EPSILON)*t,o=0;for(let s=0;s<e.length;s++)e[s]>=r&&o++;return(1+o)/(Math.trunc(n)+1)}function qi(e,t){return Number.isNaN(e)?NaN:Ne(e,t/2,2,!1,!1)}function Ii(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 ns(e,t){if(t.nMinus1??!1)throw new RangeError("'nMinus1' requires a contingency table with at least two rows and columns");let n=e.length;if(e.some(g=>!(g>=0&&g<1/0)))throw new RangeError("all entries of 'x' must be non-negative and finite");if(n<2)throw new TypeError("'x' must have at least 2 elements");let r=wt(e);if(!(r>0))throw new RangeError("at least one entry of 'x' must be positive");let o=t.p===void 0?e.map(()=>1/n):Array.from(t.p);if(o.length!==n)throw new TypeError("'x' and 'p' must have the same number of elements");if(o.some(Number.isNaN))throw new RangeError("p contains missing values");if(o.some(g=>g<0))throw new RangeError("probabilities must be non-negative");if(o.some(g=>g===1/0))throw new RangeError("probabilities must be finite");if(t.rescaleP??!1){let g=wt(o),w=g<1/0?0:Math.ceil(Math.log2(n)),b=w===0?o:o.map(v=>_(v,-w)),N=w===0?g:wt(b);o=b.map(v=>v/N)}else if(Math.abs(wt(o)-1)>Math.sqrt(Number.EPSILON))throw new RangeError("probabilities must sum to 1");let s=r<1/0?0:2*Math.ceil(Math.log2(n)/2),i=s===0?e:e.map(g=>_(g,-s)),c=s===0?r:wt(i),a=o.map(g=>c*g),l=s===0?a:a.map(g=>_(g,s));Ii(l);let f=o.map((g,w)=>a[w]*(1-g)),m=_(wt(i.map((g,w)=>Ce(g-a[w],a[w]))),s),u="Chi-squared test for given probabilities",h=n-1,p;if(t.simulatePValue??!1){let g=ki(t);if(!Number.isInteger(r))throw new RangeError("the simulation needs counts summing to an integer");u+=sr(g),h=NaN,p=Ai(Wf(r,o,l,g),m,g)}else p=qi(m,h);return{method:u,statistic:m,statisticName:"X-squared",parameter:h,parameterName:"df",pValue:p,observed:e,expected:l,residuals:i.map((g,w)=>_((g-a[w])/Math.sqrt(a[w]),s/2)),stdres:i.map((g,w)=>_((g-a[w])/Math.sqrt(f[w]),s/2)),ftResiduals:mr(e,l),warnings:Number.isFinite(h)&&l.some(g=>g<5)?[Si]:[]}}function Wf(e,t,n,r){let o=t.length;if(r*e>2**27)throw new RangeError("the simulation needs B * sum(x) <= 2^27 (134217728) draws");let s=Qa(o,t);if(r*e===0)return new Float64Array(0);let i=new Float64Array(r),c=new Float64Array(o);for(let a=0;a<r;a++){c.fill(0);for(let f=0;f<e;f++)c[s()]++;let l=0;for(let f=0;f<o;f++)l+=(c[f]-n[f])**2/n[f];i[a]=l}return i}function jf(e,t,n,r){let o=e.length,s=t.length,i=e.reduce((f,m)=>f+m,0);if(i>Yr)throw new RangeError("the simulation needs a total count of at most 16777216 (2^24)");let c=Hr(i),a=new Int32Array(o*s),l=new Float64Array(Math.trunc(r));for(let f=0;f<l.length;f++){Xr(e,t,c,a);let m=0;for(let u=0;u<s;u++)for(let h=0;h<o;h++){let p=n[h][u];m+=(a[h+u*o]-p)**2/p}l[f]=m}return l}function _i(e,t){let n=t.nMinus1??!1;if(typeof n!="boolean")throw new TypeError("'nMinus1' must be a boolean");if(n&&t.correct===!0)throw new RangeError("'nMinus1' uses no continuity correction: leave 'correct' out or set it to false");if(n&&(t.simulatePValue??!1))throw new RangeError("'nMinus1' cannot be combined with 'simulatePValue'");let r=!n&&(t.correct??!0),o=e.length,s=e[0].length,i=e.flat();if(i.some(v=>!(v>=0&&v<1/0)))throw new RangeError("all entries of 'x' must be non-negative and finite");let c=wt(i);if(!(c>0))throw new RangeError("at least one entry of 'x' must be positive");if(n&&!(c>1))throw new RangeError("'nMinus1' needs a table with a total above 1");let a=e.map(v=>wt(v)),l=e[0].map((v,y)=>wt(e.map(d=>d[y]))),f=a.map(v=>l.map(y=>Wr(v,y,c)));Ii(f.flat());let m=e.map((v,y)=>v.map((d,M)=>d-f[y][M])),u="Pearson's Chi-squared test",h,p=(o-1)*(s-1),g,w=(t.simulatePValue??!1)&&a.every(v=>v>0)&&l.every(v=>v>0),b=!w&&r&&o===2&&s===2?Math.min(.5,...m.flat().map(Math.abs)):0,N=[];if((t.simulatePValue??!1)&&!w&&N.push("cannot compute simulated p-value with zero marginals"),w){if(e.some(d=>d.some(M=>!Number.isInteger(M))))throw new RangeError("the simulation needs integer counts");let v=ki(t);u+=sr(v),h=wt(m.flatMap((d,M)=>d.map((x,E)=>Ce(x,f[M][E])))),p=NaN;let y=jf(a.map(d=>Math.trunc(d)),l.map(d=>Math.trunc(d)),f,v);g=Ai(y,h,v)}else if(b>0)u+=" with Yates' continuity correction",h=wt(m.flatMap((v,y)=>v.map((d,M)=>Ce(Math.abs(d)-b,f[y][M]))));else if(h=wt(m.flatMap((v,y)=>v.map((d,M)=>Ce(d,f[y][M])))),n){u="Pearson's 'N-1' Chi-squared test";let v=h*(c-1);h=Number.isFinite(v)?v/c:h*((c-1)/c)}return{method:u,statistic:h,statisticName:"X-squared",parameter:p,parameterName:"df",pValue:g??qi(h,p),observed:e,expected:f,residuals:m.map((v,y)=>v.map((d,M)=>d/Math.sqrt(f[y][M]))),stdres:Uf(e,c,a,l,f,m),ftResiduals:mr(e,f),warnings:Number.isFinite(p)&&f.some(v=>v.some(y=>y<5))?[...N,Si]:N}}function Uf(e,t,n,r,o,s){if(!Number.isFinite(t))return s.map((p,g)=>p.map((w,b)=>w/Math.sqrt(o[g][b]*(1-n[g]/t)*(1-r[b]/t))));let i=e.map(p=>Ti(p)),c=e[0].map((p,g)=>Ti(e.map(w=>w[g]))),a=Yf(i),l=Ri(i),f=Ri(c),m=l.map(p=>ge(p,a)),u=c.map(p=>ge(p,a)),h=f.map(p=>ge(p,a));return e.map((p,g)=>p.map((w,b)=>{let N=ce(i[g],u[b]),v=ie([w,0],N),y=v[0]+v[1],d=ce(ce(N,m[g]),h[b]),M=d[0]+d[1];if(M>=Gf||y===0)return y/Math.sqrt(M);let x=N[0]+N[1],E=m[g][0]+m[g][1],T=h[b][0]+h[b][1];return y/Math.sqrt(x)/Math.sqrt(E)/Math.sqrt(T)}))}var Gf=2**-1022;function Ti(e){let t=[0,0];for(let n of e)t=G(t,[n,0]);return t}function Yf(e){let t=[0,0];for(let n of e)t=G(t,n);return t}function Ri(e){let t=e.length,n=new Array(t+1);n[t]=[0,0];for(let s=t-1;s>=0;s--)n[s]=G(e[s],n[s+1]);let r=[],o=[0,0];for(let s=0;s<t;s++)r.push(G(o,n[s+1])),o=G(o,e[s]);return r}var Hf=Math.pow(Number.EPSILON,.25);function We(e,t,n,r={}){let o={tol:r.tol??Hf,maxiter:r.maxIter??1e3};return r.fLower!==void 0&&(o.fLower=r.fLower),r.fUpper!==void 0&&(o.fUpper=r.fUpper),ir(e,t,n,o).root}function Bt(e,t){let n=a=>e(Math.exp(a)),r=0,o=n(0),s=t?-1:1,i=n(s);for(;i!==0&&Math.sign(i)===Math.sign(o);){if(Math.abs(s)>745)return t?0:1/0;r=s,o=i,s*=2,i=n(s)}if(i===0)return Math.exp(s);let c=r<s?We(n,r,s,{tol:2**-50,fLower:o,fUpper:i}):We(n,s,r,{tol:2**-50,fLower:i,fUpper:o});return Math.exp(c)}var Xf=["alternative","confInt","confLevel","or","simulatePValue","B","midp"];function pr(e,t={}){C(t,Xf,"fisherTest");let n=e.length,r=n===0?0:e[0].length;if(e.some(f=>f.length!==r))throw new TypeError("all rows of the matrix must have the same length");if(n<2||r<2)throw new TypeError("'x' must have at least 2 rows and columns");let o=e.map(f=>Array.from(f));if(o.flat().some(f=>Number.isNaN(f)||f<0))throw new RangeError("all entries of 'x' must be non-negative and finite");if(o.flat().some(f=>f===1/0))throw new RangeError("'x' has entries too large to be integer");let s=o.map(f=>f.map(Da));if(s.flat().some(f=>f>2147483647))throw new RangeError("'x' has entries too large to be integer");let i=[],c=Bf(o,s);c!==null&&i.push(`'x' has been rounded to integer: ${c}`);let a="Fisher's Exact Test for Count Data";if(t.midp!==void 0&&typeof t.midp!="boolean")throw new TypeError("'midp' must be true or false");if((t.midp??!1)&&!(n===2&&r===2))throw new RangeError("the mid-p version ('midp') needs a 2 x 2 table");if(n===2&&r===2)return z("fisherTest",{...em(s,t,a),warnings:i});let l=t.alternative??"two.sided";if(t.simulatePValue??!1){let f=t.B??2e3;if(!(Number.isInteger(f)&&f>=1&&f<2**31))throw new RangeError("'B' must be a positive integer");return z("fisherTest",{method:a+sr(f),alternative:l,pValue:Qf(s,f),warnings:i})}return z("fisherTest",{method:a,alternative:l,pValue:tm(s),warnings:i})}function Bf(e,t){let n=Math.sqrt(Number.EPSILON),r=[],o=[];for(let l=0;l<e[0].length;l++)for(let f=0;f<e.length;f++)e[f][l]!==t[f][l]&&(r.push(e[f][l]),o.push(t[f][l]));let s=r.length;if(s===0)return null;let i=0;for(let l of r)i+=Math.abs(l)/s;let c="relative";Number.isFinite(i)&&i>n||(i=1,c="absolute");let a=0;for(let l=0;l<s;l++)a+=Math.abs(r[l]-o[l])/(s*i);return Number.isNaN(a)||a>n?`Mean ${c} difference: ${Za(a)}`:null}function Qf(e,t){let n=e.filter(w=>w.some(b=>b>0)),r=e[0].map((w,b)=>b).filter(w=>e.some(b=>b[w]>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(w=>r.map(b=>w[b])),s=o.map(w=>w.reduce((b,N)=>b+N,0)),i=o[0].map((w,b)=>o.reduce((N,v)=>N+v[b],0)),c=s.length,a=i.length,l=[0,0];for(let w of o.flat())l=G(l,[-jr(w),0]);let f=l[0]/(1+64*Number.EPSILON),m=s.reduce((w,b)=>w+b,0);if(m>Yr)throw new RangeError("the simulation needs a total count of at most 16777216 (2^24)");let u=Hr(m),h=new Int32Array(c*a),p=0,g=Math.trunc(t);for(let w=0;w<g;w++){Xr(s,i,u,h);let b=0;for(let N=0;N<h.length;N++)b-=u[h[N]];b<=f&&p++}return Math.max(0,Math.min(1,(1+p)/(g+1)))}var Li=1+1e-7,Zf=16,Jf=2**24;function ur(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=G(r,[e[o],0]));return r[0]}var Ln=class{m;n;k;lo;hi;logCentral;constructor(t,n,r){this.m=t,this.n=n,this.k=r,this.lo=Math.max(0,r-n),this.hi=Math.min(r,t),this.logCentral=new Float64Array(this.hi-this.lo+1);let o=0,s=1;for(let i=this.lo;i<=this.hi;i++){let c=i-this.lo;c%Zf===0||!(s>0&&s<1/0)?(o=Nt(i,t,n,r,!0),s=1,this.logCentral[c]=o):this.logCentral[c]=o+Math.log(s),s*=(t-i)*(r-i)/((i+1)*(n-r+i+1))}}density(t){let n=this.logCentral.length,r=Math.log(t),o=new Float64Array(n),s=-1/0;for(let c=0;c<n;c++){let a=this.logCentral[c]+(this.lo+c)*r;o[c]=a,a>s&&(s=a)}for(let c=0;c<n;c++){let a=o[c]-s;o[c]=a<-746?0:Math.exp(a)}let i=ur(o,0,n-1);for(let c=0;c<n;c++)o[c]!==0&&(o[c]=o[c]/i);return o}mean(t){if(t===0)return this.lo;if(t===1/0)return this.hi;let n=this.density(t),r=[0,0];for(let o=0;o<n.length;o++)n[o]!==0&&(r=G(r,[(this.lo+o)*n[o],0]));return r[0]}tail(t,n,r){if(n===1)return r?at(t-1,this.m,this.n,this.k,!1,!1):at(t,this.m,this.n,this.k,!0,!1);if(n===0||n===1/0){let i=n===0?this.lo:this.hi;return(r?t<=i:t>=i)?1:0}let o=this.density(n),s=t-this.lo;return r?ur(o,s,o.length-1):ur(o,0,s)}midTail(t,n,r){if(n===1){let i=.5*Nt(t,this.m,this.n,this.k,!1);return(r?at(t,this.m,this.n,this.k,!1,!1):at(t-1,this.m,this.n,this.k,!0,!1))+i}if(n===0||n===1/0){let i=n===0?this.lo:this.hi;return t===i?.5:(r?t<i:t>i)?1:0}let o=this.density(n),s=t-this.lo;return(r?ur(o,s+1,o.length-1):ur(o,0,s-1))+.5*o[s]}};function os(e,t,n){let r=e.density(n),o=r[t-e.lo]*Li,s=[0,0];for(let i of r)i<=o&&(s=G(s,[i,0]));return s[0]}function Oi(e,t,n,r){let o=Math.max(0,r-n),s=Math.min(r,t),i=g=>Nt(g,t,n,r,!0),c=i(e)+Math.log(Li),a=Math.min(s,Math.max(o,Math.floor((r+1)*(t+1)/(t+n+2))));for(;a<s&&i(a+1)>i(a);)a++;for(;a>o&&i(a-1)>i(a);)a--;let l=(g,w)=>{let b=g,N=w+1;for(;N-b>1;){let v=b+Math.floor((N-b)/2);i(v)<=c?b=v:N=v}return b},f=(g,w)=>{let b=g-1,N=w+1;for(;N-b>1;){let v=b+Math.floor((N-b)/2);i(v)<=c?N=v:b=v}return N},m,u;e<=a?(m=l(e,a),u=f(a+1,s)):(u=f(a,e),m=a-1<o||i(o)>c?o-1:l(o,a-1));let h=m<o?0:at(m,t,n,r,!0,!1),p=u>s?0:at(u-1,t,n,r,!1,!1);return h+p}function hr(e,t){return Bt(e,t)}function em(e,t,n){let r=le(t.alternative),o=t.or??1;if(!(o>=0))throw new RangeError("'or' must be a single number between 0 and Inf");let s=t.confLevel??.95;if(!(s>0&&s<1))throw new RangeError("confLevel must be a single number between 0 and 1");let[[i,c],[a,l]]=e,f=i,m=Math.min(i,l)+Math.min(c,a)+1;if(m>Jf)throw new RangeError(`the exact distribution of the 2 x 2 table has ${m} values, more than 2^24`);let u=new Ln(i+a,c+l,i+c),{lo:h,hi:p}=u,g=t.midp??!1,w;g?(r==="less"?w=u.midTail(f,o,!1):r==="greater"?w=u.midTail(f,o,!0):w=Math.min(1,2*Math.min(u.midTail(f,o,!1),u.midTail(f,o,!0))),n=r==="two.sided"?"Central Fisher's Exact Test (mid-p version)":"One-sided Fisher's Exact Test (mid-p version)"):r==="less"?w=u.tail(f,o,!1):r==="greater"?w=u.tail(f,o,!0):o===0?w=f===h?1:0:o===1/0?w=f===p?1:0:w=os(u,f,o);let b;if(f===h)b=0;else if(f===p)b=1/0;else{let M=u.mean(1);b=M===f?1:hr(x=>u.mean(x)-f,M>f)}let N={method:n,alternative:r,pValue:w,estimate:b,estimateNames:"odds ratio",nullValue:o,nullValueName:"odds ratio",warnings:[]};if(!(t.confInt??!0))return N;let v=M=>{if(f===p)return 1/0;if(g){if(f===h&&M>=.5)return 0;let E=u.midTail(f,1,!1);return E===M?1:hr(T=>u.midTail(f,T,!1)-M,E<M)}let x=u.tail(f,1,!1);return x===M?1:hr(E=>u.tail(f,E,!1)-M,x<M)},y=M=>{if(f===h)return 0;if(g){if(f===p&&M>=.5)return 1/0;let E=u.midTail(f,1,!0);return E===M?1:hr(T=>u.midTail(f,T,!0)-M,E>M)}let x=u.tail(f,1,!0);return x===M?1:hr(E=>u.tail(f,E,!0)-M,x>M)},d;if(r==="less")d=[0,v(1-s)];else if(r==="greater")d=[y(1-s),1/0];else{let M=(1-s)/2;d=[y(M),v(M)]}return{...N,confInt:d,confLevel:s}}var ao=[[0,0]];function Tt(e){for(let t=ao.length;t<=e;t++)ao.push(G(ao[t-1],_a([t,0])));return ao[e]}function io(e,t,n,r){let o=e+n,s=o-e,i=e-(o-s)+(n-s)+t+r,c=o+i;return[c,i-(c-o)]}var rs=class{scale=-1/0;hi=0;lo=0;add(t,n,r){if(t>this.scale){let a=Number.isFinite(this.scale)?Math.exp(this.scale-t):0;this.hi*=a,this.lo*=a,this.scale=t}let o=r*Math.exp(t-this.scale)*(1+n),s=this.hi+o,i=s-this.hi,c=this.hi-(s-i)+(o-i)+this.lo;this.hi=s+c,this.lo=c-(this.hi-s)}value(){return this.scale===-1/0?0:this.hi*Math.exp(this.scale)}};function tm(e){let t=e.filter(v=>v.some(y=>y>0));if(t.length===0)throw new RangeError("all entries of the table are zero");let n=t[0].map((v,y)=>t.some(d=>d[y]>0));if(t=t.map(v=>v.filter((y,d)=>n[d])),t.length<2||t[0].length<2)return 1;t.length>t[0].length&&(t=t[0].map((v,y)=>t.map(d=>d[y])));let r=t.length,o=t.map(v=>v.reduce((y,d)=>y+d,0)),s=t[0].map((v,y)=>t.reduce((d,M)=>d+M[y],0)).sort((v,y)=>y-v),i=s.length,c=o.reduce((v,y)=>v+y,0),a=Ia(Tt(c));for(let v of o)a=G(a,Tt(v));for(let v of s)a=G(a,Tt(v));let l=[0,0];for(let v of t.flat())l=ie(l,Tt(v));let f=G(l,[Math.log1p(1e-7),0]),m=1e-9*(1+Math.abs(f[0])),[u,h]=f,p=Array.from({length:i},()=>new Map),g=(v,y)=>{let d=y.join(","),M=p[v].get(d);if(M!==void 0)return M;let x=G(a,Tt(y.reduce((A,q)=>A+q,0)));for(let A of y)x=ie(x,Tt(A));for(let A=v;A<i;A++)x=ie(x,Tt(s[A]));let E={R:y,KZ:x,max:0,min:0,edges:[],pastHi:[],pastLo:[],count:[],index:new Map};if(p[v].set(d,E),v===i-1){let A=[0,0];for(let q of y)A=ie(A,Tt(q));return E.max=E.min=A[0],E.edges.push({to:E,hi:A[0],lo:A[1],mult:1}),E}let T=new Map,R=new Array(r+1).fill(0);for(let A=r-1;A>=0;A--)R[A]=R[A+1]+y[A];let S=new Array(r).fill(0),k=(A,q,I)=>{if(A===r-1){S[A]=q;let P=ie(I,Tt(q)),W=y.map((X,te)=>X-S[te]).sort((X,te)=>X-te),D=g(v+1,W),F=`${W.join(",")}|${P[0]}`,K=T.get(F);K===void 0?T.set(F,{to:D,hi:P[0],lo:P[1],mult:1}):K.mult++;return}let L=Math.max(0,q-R[A+1]),O=Math.min(y[A],q);for(let P=L;P<=O;P++)S[A]=P,k(A+1,q-P,ie(I,Tt(P)))};if(k(0,s[v],[0,0]),E.edges=[...T.values()],E.max=ve(E.edges.map(A=>A.hi+A.to.max)),E.min=it(E.edges.map(A=>A.hi+A.to.min)),v===i-2){let A=E.edges.map(D=>{let F=D.to.edges[0];return{v:G([D.hi,D.lo],[F.hi,F.lo]),mult:D.mult}});A.sort((D,F)=>D.v[0]-F.v[0]||D.v[1]-F.v[1]);let q=A.length,I=A[q-1].v[0],L=new Float64Array(q),O=new Float64Array(q),P=new Float64Array(q),W=[0,0];A.forEach(({v:D,mult:F},K)=>{L[K]=D[0],O[K]=D[1];let X=ie(D,[I,0]);W=G(W,[F*Math.exp(X[0])*(1+X[1]),0]),P[K]=W[0]}),E.dist={hi:L,lo:O,cum:P,vmax:I,kv:G(a,[I,0])}}return E},w=g(0,[...o].sort((v,y)=>v-y)),b=new rs,N=(v,y,d,M)=>{if(y+v.max<=u-m){let[k,A]=io(v.KZ[0],v.KZ[1],y,d);return b.add(k,A,M),!0}if(y+v.min>u+m)return!0;let x=v.dist;if(x===void 0)return!1;let[E,T]=io(u,h,-y,-d),R=0,S=x.hi.length;for(;R<S;){let k=R+S>>>1,A=x.hi[k];A<E||A===E&&x.lo[k]<=T?R=k+1:S=k}if(R>0){let[k,A]=io(x.kv[0],x.kv[1],y,d);b.add(k,A,M*x.cum[R-1])}return!0};N(w,0,0,1)||(w.pastHi.push(0),w.pastLo.push(0),w.count.push(1));for(let v=0;v<i-2;v++)for(let y of p[v].values()){let{pastHi:d,pastLo:M,count:x}=y;if(x.length!==0){for(let{to:E,hi:T,lo:R,mult:S}of y.edges)for(let k=0;k<x.length;k++){let[A,q]=io(d[k],M[k],T,R),I=x[k]*S;if(N(E,A,q,I))continue;let L=E.index.get(A);L===void 0?(E.index.set(A,E.count.length),E.pastHi.push(A),E.pastLo.push(q),E.count.push(I)):E.count[L]+=I}y.pastHi=y.pastLo=y.count=[],y.index.clear()}}return Math.min(1,b.value())}function co(e){let t=e.every(n=>Number.isInteger(n))?1:2;return[t,e.map(n=>n*t).sort((n,r)=>n-r)]}function as(e){return[...e].sort((t,n)=>t-n).join(",")}function ss(e,t,n){let r=n.length,o=new Float64Array(r),s=new Float64Array(r),i=0;for(let c=0;c<r;c++)o[c]=i+=n[c];i=0;for(let c=r-1;c>=0;c--)s[c]=i+=n[c];return{f:e,offset:t,lower:o,upper:s}}function is(e){let[,t]=co(e),n=0,r=0;for(let o of t)r+=n+=o;return{steps:r,size:n+1}}function cs(e){let[t,n]=co(e),r=0;for(let i of n)r+=i;let o=new Float64Array(r+1);o[0]=1;let s=0;for(let i of n){s+=i;for(let c=s;c>=i;c--)o[c]=.5*(o[c]+o[c-i]);for(let c=i-1;c>=0;c--)o[c]=.5*o[c]}return ss(t,0,o)}function ls(e,t){let[,n]=co(e),r=n.length,o=Math.min(t,r-t),s=Fi(n),i=0;for(let c=1;c<=r;c++)for(let a=Math.max(1,o-(r-c));a<=Math.min(c,o);a++)i+=s[c]-s[c-a]-s[a]+1;return{steps:i,size:s[r]-s[r-o]-s[o]+1}}function Fi(e){let t=[0];for(let n of e)t.push(t[t.length-1]+n);return t}function fs(e,t){let[n,r]=co(e),o=r.length,s=Math.min(t,o-t),i=Fi(r),c=[];for(let h=0;h<=s;h++)c.push(new Float64Array(i[o]-i[o-h]-i[h]+1));c[0][0]=1;for(let h=1;h<=o;h++){let p=r[h-1];for(let g=Math.min(h,s);g>=Math.max(1,s-(o-h));g--){let w=c[g],b=c[g-1],N=g/h,v=(h-g)/h,y=i[g],d=i[h]-i[h-g],M=i[g-1]+p,x=Math.max(y,M);for(let E=d;E>=x;E--)w[E-y]=v*w[E-y]+N*b[E-M];for(let E=x-1;E>=y;E--)w[E-y]=v*w[E-y]}}let a=c[s],l=t*(t+1)/2;if(s===t)return ss(n,i[s]/n-l,a);let f=a.length,m=new Float64Array(f);for(let h=0;h<f;h++)m[h]=a[f-1-h];let u=i[o]-i[o-s];return ss(n,(i[o]-u)/n-l,m)}function Qt(e,t){let n=Math.ceil(e.f*(t-e.offset)+e.f*1e-8)-1;return n<0?0:n>=e.lower.length?1:Math.min(1,e.lower[n])}function mn(e,t){let n=Qt(e,t);if(Math.abs(n-1)>=1/64)return 1-n;let r=Math.ceil(e.f*(t-e.offset)+e.f*1e-8);return r>=e.upper.length?0:Math.min(1,e.upper[Math.max(0,r)])}var Ci=["alternative","mu","paired","exact","correct","confInt","confLevel","tolRoot"];function dr(e,t,n={}){if(typeof e!="string"&&ze(t)&&C(t,Ci,"wilcoxTest"),C(n,Ci,"wilcoxTest"),typeof e=="string"){if(n.paired!==void 0)throw new Error("cannot use 'paired' in formula method");let g=Qr(e,t,!0);return g.kind==="one"?dr(g.x,n):dr(g.x,g.y,g.kind==="paired"?{...n,paired:!0}:n)}let r;t!==void 0&&!ArrayBuffer.isView(t)&&!Array.isArray(t)?n=t:r=t;let o=le(n.alternative),s=n.mu??0,i=n.paired??!1,c=n.exact??null,a=rm(n.correct??!0),l=n.confInt??!1,f=n.confLevel??.95,m=n.tolRoot??1e-4;if(!Number.isFinite(s))throw new RangeError("mu must be a single finite number");if(l&&!(f>0&&f<1))throw new RangeError("confLevel must be a number strictly between 0 and 1");if(i&&r===void 0)throw new TypeError("y is required when paired is true");let u,h;if(i){let[g,w]=En(Array.from(e),Array.from(r));u=Je(g.map((b,N)=>b-w[N]))}else u=Je(Array.from(e)),r!==void 0&&(h=Je(Array.from(r)));if(u.length<1)throw new Error("not enough (non-missing) 'x' observations");if(h!==void 0&&h.length<1)throw new Error("not enough (non-missing) 'y' observations");let p={alternative:o,mu:s,correct:a,confInt:l,confLevel:f,tolRoot:m,warnings:new ke};return z("wilcoxTest",h===void 0?cm(u,c,i,p):gm(u,h,c,p))}function rm(e){if(typeof e=="boolean")return e?0:-1;if(!(Number.isInteger(e)&&e>=0&&e<=3))throw new RangeError("'correct' must be an integer between 0 and 3");return e}function lo(e,t,n){return n?t==="two.sided"?Math.sign(e)*.5:t==="greater"?.5:-.5:0}var Vi=10*Number.EPSILON;function $i(e,t,n=0){let r=a=>Math.min(1,Math.max(a,0)),o=()=>n===0?j(e):r(j(e)-n*Ze(e)),s=()=>n===0?j(e,{lowerTail:!1}):r(j(e,{lowerTail:!1})+n*Ze(e));if(t==="less")return o();if(t==="greater")return s();let i=o();if(i<=.5)return 2*i;let c=s();return c>=1/64?2*(1-i):2*c}function om(e){let t=e*e;return[e*(t-3),e*(t*(t-10)+15),e*(t*(t*(t-21)+105)-105)]}function sm(e,t,n){let r=t*(t+1)*(2*t+1),o=-(3*t*t+3*t-1)/(10*r),s=4*(3*t**4+6*t**3-3*t+1)/(35*r*r);return zi(e,o,s,n)}function am(e,t,n,r){let o=t+n,s=t*n,i=-(o*(o+1)-s)/(20*s*(o+1)),c=(2*(t**4+n**4)+4*s*(t*t+n*n)+6*s*s+4*(t**3+n**3)+(7*s+o-1)*o)/(210*s*s*(o+1)**2);return zi(e,i,c,r)}function zi(e,t,n,r){let[o,s,i]=om(e),c=t*o;return r>=2&&(c+=n*s),r>=3&&(c+=t*t/2*i),c}function hs(e){let t=e.length;if(t===0)return NaN;let n=Math.floor(t/2);return t%2===1?e[n]:qe([e[n-1],e[n]])}var ps=(e,t)=>e-t,ms=5e9,im=40;function us(e){return new RangeError(`wilcoxTest: the exact conditional ${e} would take too long or too much memory: use exact: false`)}function fo(e,t){if(e.size>16777216||e.steps>ms)throw us(t);return e.steps}function Ki(e){let t=e.length,n=ye(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 vt(n))o+=i*i*i-i;let s=Math.sqrt(t*(t+1)*(2*t+1)/24-o/48);return[r,r-t*(t+1)/4,s]}function cm(e,t,n,r){let{alternative:o,mu:s}=r,i=e.map(d=>d-s);i.some((d,M)=>!Number.isFinite(d)&&Number.isFinite(e[M]))&&(i=e.map(d=>d/2-s/2));let a=i.some(d=>d===0),l=t??i.length<50,f=(d,M,x)=>({method:d,alternative:o,statistic:M,statisticName:"V",pValue:x,nullValue:s,nullValueName:n?"location shift":"location",warnings:r.warnings.list});if(l){let d=ye(i.map(Math.abs)),M=vt(d).length>0,x=0;for(let A=0;A<i.length;A++)i[A]>0&&(x+=d[A]);let E="Wilcoxon signed rank exact test";if(!M&&!a){let A=i.length,q=A*(A+1)/2,I;if(o==="less")I=xn(x,A,!0,!1);else if(o==="greater")I=xn(x-1,A,!1,!1);else{let O=x>q/2?xn(x-1,A,!1,!1):xn(x,A,!0,!1);I=Math.min(1,2*O)}let L=f(E,x,I);return r.confInt?{...L,...lm(e,r)}:L}let T=d.filter((A,q)=>i[q]!==0);fo(is(T),"distribution");let R=cs(T),S;if(o==="less")S=Qt(R,x);else if(o==="greater")S=mn(R,x-.25);else{let A=0;for(let q of T)A+=q/2;S=Math.min(1,2*(x>A?mn(R,x-.25):Qt(R,x)))}let k=f(E,x,S);return r.confInt?{...k,...mm(e,r)}:k}let m=a?i.filter(d=>d!==0):i,u=m.length;if(u===0)throw new RangeError("no non-zero differences for the signed rank test");let[h,p,g]=Ki(m),b=vt(ye(m.map(Math.abs))).length>0||a?Math.min(r.correct,0):r.correct,N=(p-lo(p,o,b>=0))/g,v=$i(N,o,b>=1?sm(N,u,b):0),y=f(`Wilcoxon signed rank test${r.correct>=0?" with continuity correction":""}`,h,v);return r.confInt?{...y,...um(e,r)}:y}function ds(e,t,n){return e==="less"?[-1/0,n]:e==="greater"?[t,1/0]:[t,n]}function Di(e,t,n){let r={confInt:ds(e,NaN,NaN),confLevel:0,estimate:t};return n!==void 0&&(r.estimateNames=n),r}function Wi(e){if(!(e>0&&e<1/0))throw new RangeError("tolRoot must be a positive finite number")}function ji(e){let t=e.length,n=[];for(let r=0;r<t;r++)for(let o=r;o<t;o++){let s=Gt(e[r],e[o]);Number.isNaN(s)||n.push(s)}return n.sort(ps)}function Ui(e,t,n,r,o){let{alternative:s,confLevel:i}=r,c=e.length,a=s==="two.sided"?(1-i)/2:1-i,l=n(a);t(l)<=a+Vi&&l++;let f=hs(e);if(l===0)return{confInt:[-1/0,1/0],confLevel:1,estimate:f,estimateNames:o};let m=t(l-1);return{confInt:ds(s,e[l-1],e[c-l]),confLevel:1-(s==="two.sided"?2*m:m),estimate:f,estimateNames:o}}function lm(e,t){let n=e.length;return Ui(ji(e),r=>xn(r,n,!0,!1),r=>Ya(r,n,!0,!1),t,"(pseudo)median")}function Gi(e){let t=e.length,n=Float64Array.from(e).sort(),r=new Array(t);for(let s=0;s<t;s++){let i=e[s],c=0,a=t;for(;c<a;){let f=c+a>>>1;n[f]<i?c=f+1:a=f}let l=c;for(a=t;l<a;){let f=l+a>>>1;n[f]<=i?l=f+1:a=f}r[s]=(c+1+l)/2}let o="";for(let s=0;s<t;){let i=s;for(;i+1<t&&n[i+1]===n[s];)i++;i>s&&(o+=`${s}:${i-s+1},`),s=i+1}return{ranks:r,key:o}}function Yi(e,t,n,r,o,s,i,c,a,l){let{alternative:f,confLevel:m}=a,u=e.length,p=(f==="two.sided"?(1-m)/2:1-m)+Vi,g=f!=="less",w=f!=="greater",b=I=>Gt(e[I-1],e[I]),N=Math.min(u-1,Math.max(1,Math.ceil(i.mean+Z(Math.min(p,1))*i.sd+.1*i.sd+3))),v=8,y=I=>I===0?e[0]-1:b(I),d=I=>I===0?e[u-1]+1:b(u-I),M=[new Map,new Map];{let I=new Map,L=0,O=im*c;for(let[P,W]of[[0,y],[1,d]])if(!(P===0?!g:!w))for(let D=0;D<=N;D++){if(L+=O,L>ms)throw us("interval");let F;try{F=t(W(D))}catch{break}if(M[P].set(D,[F.stat,F.key]),I.has(F.key)){I.delete(F.key),I.set(F.key,!0);continue}if(L+=r(F.scores),L>ms)throw us("interval");I.set(F.key,!0),I.size>v&&I.delete(I.keys().next().value)}}let x=new Map,E=(I,L)=>{let O=I===0?y(L):d(L),P=M[I].get(L),W;if(P===void 0){let X=t(O);P=[X.stat,X.key],W=X.scores}let[D,F]=P,K=x.get(F);return K!==void 0?x.delete(F):(K=n(W??t(O).scores),x.size>=v&&x.delete(x.keys().next().value)),x.set(F,K),[D,K]},T=I=>{let[L,O]=E(0,I);return o(O,L)},R=I=>{let[L,O]=E(1,I);return s(O,L)},S=-1/0,k=0;if(g){let I=T(0);if(!(I>p)){let L=I,O=1;for(;O<=u-1;O++){let P=T(O);if(P>p)break;L=P}S=e[O-1],k=L}}let A=1/0,q=0;if(w){let I=R(0);if(!(I>p)){let L=I,O=u-1;for(;O>=1;O--){let P=R(u-O);if(P>p)break;L=P}A=e[O],q=L}}return{confInt:[S,A],confLevel:1-(k+q),estimate:hs(e),estimateNames:l}}function fm(e,t){let n=e.map(r=>r-t);if(n.some(r=>Number.isNaN(r)))throw new Error("cannot compute the exact confidence interval: the shifted data have undefined values (infinite observations)");return Number.isFinite(t)&&n.some((r,o)=>!Number.isFinite(r)&&Number.isFinite(e[o]))&&(n=e.map(r=>r/2-t/2)),n}function mm(e,t){let n=e.length,r=s=>{let i=fm(e,s),{ranks:c,key:a}=Gi(i.map(Math.abs)),l=0;for(let f=0;f<n;f++)i[f]>0&&(l+=c[f]);return{stat:l,scores:c,key:a}},o=n*(n+1)/2;return Yi(ji(e),r,cs,s=>fo(is(s),"interval"),(s,i)=>mn(s,i-.25),Qt,{mean:o/2,sd:Math.sqrt(n*(n+1)*(2*n+1)/24)},n,t,"(pseudo)median")}function um(e,t){let{alternative:n,confLevel:r,tolRoot:o}=t,s=t.correct>=0,i=e.length;if(i===0)return Di(n,NaN);let c=it(e),a=ve(e);if(!Number.isFinite(c)||!Number.isFinite(a)||i===1||c===a)return Number.isFinite(c)&&c===a&&t.warnings.add("cannot compute confidence interval when all observations are zero or tied"),Di(n,Gt(c,a),"midrange");let l=(v,y)=>{let[,d,M]=Ki(e.map(x=>x-v).filter(x=>x!==0));return(d-lo(d,n,y))/M},f=l(c,s),m=l(a,s),u=v=>-Z(v),h=v=>n==="greater"?!(f<u(v)):n==="less"?!(m>Z(v/2)):!(f<u(v/2))&&!(m>Z(v/2)),p=1-r,g=!1;for(;!h(p)&&p<1;)p*=2,g=!0;g&&t.warnings.add("requested conf.level not achievable"),Wi(o);let w=v=>We(y=>l(y,s)-v,c,a,{tol:o,fLower:f-v,fUpper:m-v}),b;if(p>=1){let v=hs([...e].sort(ps));b=ds(n,v,v)}else n==="greater"?b=[w(u(p)),1/0]:n==="less"?b=[-1/0,w(Z(p))]:b=[w(u(p/2)),w(Z(p/2))];let N=We(v=>l(v,!1),c,a,{tol:o,fLower:l(c,!1),fUpper:l(a,!1)});return{confInt:b,confLevel:g?1-Math.min(1,p):r,estimate:N,estimateNames:"(pseudo)median"}}function Hi(e,t){let n=e.length,r=n-t,o=0;for(let a=0;a<t;a++)o+=e[a];let s=o-t*(t+1)/2,i=0;for(let a of vt(e))i+=a*a*a-a;let c=Math.sqrt(t*r/12*(n+1-i/(n*(n-1))));return[s,s-t*r/2,c,i>0]}function hm(e,t){let[n,r,o]=Hi(ye([...e,...t]),e.length);return[n,r,o]}function pm(e,t){let n=e.length,r=n+t.length,o=(l,f)=>{let m=l===0||!Number.isFinite(l)||!Number.isFinite(f)?f:f/2;return l<m?-1:l>m?1:0},s=(l,f)=>l<n&&f<n?e[l]<e[f]?-1:e[l]>e[f]?1:0:l>=n&&f>=n?t[l-n]<t[f-n]?-1:t[l-n]>t[f-n]?1:0:l<n?o(e[l],t[f-n]):-o(e[f],t[l-n]),i=Array.from({length:r},(l,f)=>f);i.sort((l,f)=>s(l,f)||l-f);let c=new Array(r),a=0;for(;a<r;){let l=a;for(;l+1<r&&s(i[l+1],i[a])===0;)l++;let f=(a+l+2)/2;for(let m=a;m<=l;m++)c[i[m]]=f;a=l+1}return c}function dm(e,t,n){return Xi(e,t,n).ranks}function Xi(e,t,n){let r=e.map(o=>o-n);if(r.some(o=>Number.isNaN(o))){let o=r.map((a,l)=>[a,l]).filter(([a])=>!Number.isNaN(a)),s=ye([...o.map(([a])=>a),...t]),i=new Array(e.length+t.length);o.forEach(([,a],l)=>i[a]=s[l]),t.forEach((a,l)=>i[e.length+l]=s[o.length+l]);let c=o.length+t.length;return r.forEach((a,l)=>{Number.isNaN(a)&&(i[l]=++c)}),{ranks:i,key:as(i)}}if(Number.isFinite(n)&&r.some((o,s)=>!Number.isFinite(o)&&Number.isFinite(e[s]))){let o=pm(e.map(s=>s/2-n/2),t);return{ranks:o,key:as(o)}}return Gi([...r,...t])}function gm(e,t,n,r){let{alternative:o,mu:s}=r,i=e.length,c=t.length,a=n??(i<50&&c<50),l=dm(e,t,s),[f,m,u,h]=Hi(l,i),p,g;if(a)if(g="Wilcoxon rank sum exact test",!h)p=Pi(()=>o==="less"?Tn(f,i,c,!0,!1):o==="greater"?Tn(f-1,i,c,!1,!1):Math.min(2*Tn(f,i,c,!0,!1),2*Tn(f-1,i,c,!1,!1),1));else{fo(ls(l,i),"distribution");let N=fs(l,i);o==="less"?p=Qt(N,f):o==="greater"?p=mn(N,f-.25):p=Math.min(2*Qt(N,f),2*mn(N,f-.25),1)}else{g=`Wilcoxon rank sum test${r.correct>=0?" with continuity correction":""}`;let N=h?Math.min(r.correct,0):r.correct,v=l.every(d=>d===l[0]),y=(m-lo(m,o,N>=0))/u;p=v?1:$i(y,o,N>=1?am(y,i,c,N):0)}let w={method:g,alternative:o,statistic:f,statisticName:"W",pValue:p,nullValue:s,nullValueName:"location shift",warnings:r.warnings.list};if(!r.confInt)return w;let b=a?h?bm(e,t,r):Pi(()=>wm(e,t,r)):vm(e,t,r);return{...w,...b}}function Pi(e){try{return e()}catch(t){throw t instanceof RangeError&&t.message.startsWith("wilcox:")?new RangeError(`${t.message}: use exact: false`):t}}function Bi(e,t,n){let r=[];for(let o of e)for(let s of t){let i=o-s;n&&Number.isNaN(i)||r.push(i)}return r.sort(ps)}function wm(e,t,n){let r=e.length,o=t.length;return Ui(Bi(e,t,!1),s=>Tn(s,r,o,!0,!1),s=>Xa(s,r,o,!0,!1),n,"difference in location")}function bm(e,t,n){let r=e.length,o=t.length,s=i=>{let{ranks:c,key:a}=Xi(e,t,i),l=0;for(let f=0;f<r;f++)l+=c[f];return{stat:l-r*(r+1)/2,scores:c,key:a}};return Yi(Bi(e,t,!0),s,i=>fs(i,r),i=>fo(ls(i,r),"interval"),(i,c)=>mn(i,c-.25),Qt,{mean:r*o/2,sd:Math.sqrt(r*o*(r+o+1)/12)},r+o,n,"difference in location")}function vm(e,t,n){let{alternative:r,confLevel:o,tolRoot:s}=n,i=n.correct>=0,c=it(e)-ve(t),a=ve(e)-it(t);if(!Number.isFinite(c)||!Number.isFinite(a))throw new Error("cannot compute the confidence interval because the differences are not finite");let l=(h,p)=>{let[,g,w]=hm(e.map(b=>b-h),t);return(g-lo(g,r,p))/w},f=(h,p)=>{let g=l(c,p)-h,w=l(a,p)-h;if(Number.isNaN(g)||Number.isNaN(w))throw new Error("cannot compute the confidence interval because all observations are tied");return g<=0?c:w>=0?a:(Wi(s),We(b=>l(b,p)-h,c,a,{tol:s,fLower:g,fUpper:w}))},m=1-o,u;return r==="greater"?u=[f(-Z(m),i),1/0]:r==="less"?u=[-1/0,f(Z(m),i)]:u=[f(-Z(m/2),i),f(Z(m/2),i)],{confInt:u,confLevel:o,estimate:f(0,!1),estimateNames:"difference in location"}}var Nm=["method","n"],Qi=["holm","hochberg","hommel","bonferroni","BH","BY","fdr","none"];function Mm(e){if(Qi.includes(e))return e;let t=Qi.filter(n=>n.startsWith(e));if(e.length===0||t.length!==1)throw new RangeError(`invalid method "${e}"`);return t[0]}function On(e,t={}){C(t,Nm,"pAdjust");let n=typeof t=="string"?{method:t}:t,r=Mm(n.method??"holm"),o=Array.from(e),s=[];for(let m=0;m<o.length;m++)Number.isNaN(o[m])||s.push(m);let i=s.length,c=n.n??i;if(!(c>=i))throw new RangeError("n must be at least the number of non-NaN p-values");if(c===1/0)throw new RangeError("n must be finite");if(c<=1||i===0)return o;let a=s.map(m=>o[m]),l=Ji(a,r,c),f=o.slice();return s.forEach((m,u)=>f[m]=l[u]),f}function mo(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 Ji(e,t,n){let r=e.length,o=new Array(r);switch(t){case"none":return e.slice();case"bonferroni":return e.map(s=>Math.min(1,n*s));case"holm":{let s=mo(e,!1),i=-1/0;return s.forEach((c,a)=>{i=Math.max(i,(n-a)*e[c]),o[c]=Math.min(1,i)}),o}case"hochberg":{let s=mo(e,!0),i=1/0;return s.forEach((c,a)=>{let l=r-a;i=Math.min(i,(n-l+1)*e[c]),o[c]=Math.min(1,i)}),o}case"BH":case"fdr":case"BY":{let s=1;if(t==="BY"){let m=Va(),u=Math.floor(n);if(u<=1e6){for(let h=1;h<=u;h++)$a(m,1/h,u);s=za(m)}else{let h=u*u;s=Math.log(u)+.5772156649015329+1/(2*u)-1/(12*h)+1/(120*h*h)}}let i=s*n,c=Number.isFinite(i)?1:1024,a=Number.isFinite(i)?i:s*(n/1024),l=mo(e,!0),f=1/0;return l.forEach((m,u)=>{let h=r-u;f=Math.min(f,a/h*e[m]*c),o[m]=Math.min(1,f)}),o}case"hommel":return n===2?Ji(e,"hochberg",n):ym(e,n)}}var Zi=2**16;function ym(e,t){if(!Number.isInteger(t))throw new RangeError("n must be an integer for the hommel method");if(t>Zi)throw new RangeError(`n must be at most ${Zi} for the hommel method (O(n^2) work)`);let n=e.length,r=[...e,...new Array(t-n).fill(1)],o=mo(r,!1),s=o.map(a=>r[a]),i=s.slice();for(let a=t;a>=2;a--){let l=1/0;for(let f=1;f<=a;f++)l=Math.min(l,a*s[t-a+f-1]/f);for(let f=t-a;f<t;f++)i[f]=Math.max(i[f],l);for(let f=0;f<t-a;f++)i[f]=Math.max(i[f],Math.min(a*s[f],l))}let c=new Array(n);return o.forEach((a,l)=>{a<n&&(c[a]=i[l])}),c}var ec=["p","alternative","confLevel"],Em=1+1e-7;function gs(e,t,n){let r=e-1;return e===0||e-r===1&&He(t,-r)[1]===0?Ot(r,t,n,!1,!1):Ot(e,t,n,!1,!1)+ht(e,t,n,!1)}function xm(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=ht(e,t,n,!1)*Em,s=a=>ht(a,t,n,!1)<=o;if(e<r){let a=Pr(s,Math.ceil(r),t),l=a>t?0:gs(a,t,n);return Math.min(1,Ot(e,t,n,!0,!1)+l)}let i=Vr(s,0,Math.floor(r)),c=i<0?0:Ot(i,t,n,!0,!1);return Math.min(1,gs(e,t,n)+c)}function Tm(e,t,n={}){C(t,ec,"binomTest"),C(n,ec,"binomTest");let r,o;if(typeof e!="number"){if(e.length!==2)throw new RangeError("incorrect length of 'x'");if(t!==void 0&&typeof t!="number"&&(n=t),!Ft(e[0])||!Ft(e[1]))throw new RangeError("'x' must be nonnegative and integer");if(r=Math.round(e[0]),o=Math.round(e[0])+Math.round(e[1]),o===0||o===1/0)throw new RangeError("'n' must be a positive integer >= 'x'")}else{if(typeof t!="number")throw new TypeError("'n' is required when 'x' is a single number");if(!Ft(e))throw new RangeError("'x' must be nonnegative and integer");if(r=Math.round(e),!(Ft(t)&&Math.round(t)>=r&&t>0))throw new RangeError("'n' must be a positive integer >= 'x'");o=Math.round(t)}return ws(r,o,n)}function ws(e,t,n={}){let r=n.p??.5;if(!(r>=0&&r<=1))throw new RangeError("'p' must be a single number between 0 and 1");let o=le(n.alternative),s=n.confLevel??.95;Xe(s);let i;o==="less"?i=Ot(e,t,r,!0,!1):o==="greater"?i=gs(e,t,r):i=xm(e,t,r);let c=f=>e===0?0:Ut(f,e,t-e+1,!0,!1),a=f=>e===t?1:f<1e-8?Ut(f,e+1,t-e,!1,!1):Ut(1-f,e+1,t-e,!0,!1),l;if(o==="less")l=[0,a(1-s)];else if(o==="greater")l=[c(1-s),1];else{let f=(1-s)/2;l=[c(f),a(f)]}return{method:"Exact binomial test",alternative:o,statistic:e,statisticName:"number of successes",parameter:t,parameterName:"number of trials",pValue:i,confInt:l,confLevel:s,estimate:e/t,estimateNames:"probability of success",nullValue:r,nullValueName:"probability of success",warnings:[]}}var Rm=["T","r","alternative","confLevel"],Sm=1+1e-7;function km(e,t){if(t===0)return e===0?1:0;if(e===t)return 1;let n=$r(e,t,!1)*Sm,r=i=>$r(i,t,!1)<=n,o=Math.floor(t);if(e<t){let i=Pr(r,Math.max(e+1,o),1/0);return Math.min(1,yn(e,t,!0,!1)+bs(i,t))}let s=Vr(r,0,Math.min(e-1,o));return Math.min(1,yn(s,t,!0,!1)+bs(e,t))}var gr=2**-1022;function bs(e,t){return e-(e-1)===1?yn(e-1,t,!1,!1):yn(e,t,!1,!1)+$r(e,t,!1)}function un(e){let t=e<9332636185032189e-317?600:0,n=Math.floor(Math.log2(e*2**t));return[e*2**t*2**-n,n-t]}function Am(e,t,n,r){let o=n+t*r,s=e*t*n/o;if(Number.isFinite(s)&&Number.isFinite(o)&&o>=gr&&(s!==0||e===0||t===0))return s;let i=e*t,c=1+t*(r/n);if(e===0||t===0)return 0;if(Number.isFinite(i)&&Number.isFinite(c))return i/c;let[a,l]=un(e),[f,m]=un(t),[u,h]=un(n),[p,g]=un(r),w=f*p,b=h-m-g,N=b>=0?u/(u+_(w,-b)):u/(_(u,b)+w);return _(a*f*N,l+m+Math.min(b,0))}function qm(e,t,n,r){let o=e/n,s=t/r,i=o/s;return t===0||Number.isFinite(o)&&Number.isFinite(s)&&s>=gr&&(o===0||o>=gr)?i:e===0?0:e/t*(r/n)}function Im(e,t={}){C(t,Rm,"poissonTest");let n=typeof e=="number"?[e]:Array.from(e);if(n.length<1||n.length>2)throw new RangeError("incorrect length of 'x'");if(!n.every(Ft))throw new RangeError("'x' must be finite, nonnegative, and integer");let r=n.map(Math.round),o=r.length,s=t.T??1,i=typeof s=="number"?new Array(o).fill(s):Array.from(s);if(i.length!==o)throw new RangeError("'x' and 'T' have incompatible length");if(!i.every(b=>Number.isFinite(b)&&b>0))throw new RangeError("'T' must be positive");let c=t.r??1;if(!(Number.isFinite(c)&&c>=0))throw new RangeError("'r' must be a single positive number");let a=le(t.alternative),l=t.confLevel??.95;if(o===2?Xe(l):Ht(l),o===2){let[b,N]=r,[v,y]=i,d=b+N;if(d===1/0)throw new RangeError("'x' must be finite, nonnegative, and integer");let M=c*v,x=M+y,E=Number.isFinite(x)&&x>=gr&&(M===0||M>=gr)?M/x:1/(1+y/v/c),T=ws(Math.round(b),Math.round(b)+Math.round(N),{p:E,alternative:a,confLevel:l}),R=y/v,S=k=>{if(k===0)return 0;let A=k/(1-k);if(Number.isFinite(R)||A===1/0)return A*R;let[q,I]=un(A),[L,O]=un(v),[P,W]=un(y);return _(q*P/L,I+W-O)};return{method:"Comparison of Poisson rates",alternative:a,statistic:b,statisticName:"count1",parameter:Am(d,c,v,y),parameterName:"expected count1",pValue:T.pValue,confInt:[S(T.confInt[0]),S(T.confInt[1])],confLevel:l,estimate:qm(b,N,v,y),estimateNames:"rate ratio",nullValue:c,nullValueName:"rate ratio",warnings:[]}}let f=r[0],m=i[0],u=c*m,h;a==="less"?h=yn(f,u,!0,!1):a==="greater"?h=bs(f,u):h=km(f,u);let p=b=>f===0?0:Dr(b,f,1,!0,!1),g=b=>b<1e-8?Dr(b,f+1,1,!1,!1):Dr(1-b,f+1,1,!0,!1),w;if(a==="less")w=[0,g(1-l)];else if(a==="greater")w=[p(1-l),1/0];else{let b=(1-l)/2;w=[p(b),g(b)]}return{method:"Exact Poisson test",alternative:a,statistic:f,statisticName:"number of events",parameter:m,parameterName:"time base",pValue:h,confInt:[w[0]/m,w[1]/m],confLevel:l,estimate:f/m,estimateNames:"event rate",nullValue:c,nullValueName:"event rate",warnings:[]}}var tc=["p","alternative","confLevel","correct"],_m="Chi-squared approximation may be incorrect";function vs(e,t,n={}){ze(t)&&C(t,tc,"propTest"),C(n,tc,"propTest");let r,o,s=n;if(typeof e!="number"&&e.length>0&&Array.isArray(e[0])){let S=e;if(S.some(k=>k.length!==2))throw new RangeError("'x' must have 2 columns");r=S.map(k=>k[0]),o=S.map(k=>k[0]+k[1]),t!==void 0&&typeof t=="object"&&!Array.isArray(t)&&(s=t)}else{if(t===void 0||typeof t=="object"&&!Array.isArray(t))throw new RangeError("'n' must be given");r=typeof e=="number"?[e]:Array.from(e),o=typeof t=="number"?[t]:Array.from(t)}if(r.length!==o.length)throw new RangeError("'x' and 'n' must have the same length");let i=r.length,c=[];for(let S=0;S<i;S++)!Number.isNaN(r[S])&&!Number.isNaN(o[S])&&c.push(S);let a=c.length;if(a<1)throw new RangeError("not enough data");if(r=c.map(S=>r[S]),o=c.map(S=>o[S]),o.some(S=>!(S>0)))throw new RangeError("elements of 'n' must be positive");if(o.some(S=>S===1/0))throw new RangeError("elements of 'n' must be finite");if(r.some(S=>!(S>=0)))throw new RangeError("elements of 'x' must be nonnegative");if(r.some((S,k)=>S>o[k]))throw new RangeError("elements of 'x' must not be greater than those of 'n'");let l;if(s.p!==void 0){let S=typeof s.p=="number"?[s.p]:Array.from(s.p);if(S.length!==i)throw new RangeError("'p' must have the same length as 'x' and 'n'");if(l=c.map(k=>S[k]),l.some(k=>!(k>0&&k<1)))throw new RangeError("elements of 'p' must be in (0,1)")}let f=l!==void 0;a===1&&l===void 0&&(l=[.5]);let m=s.confLevel??.95;Xe(m);let u=le(s.alternative);(a>2||a===2&&f)&&(u="two.sided");let h=r.map((S,k)=>S/o[k]),p=a===2?h[0]-h[1]:0,g=a===2?1/o[0]+1/o[1]:0,w=(s.correct??!0)&&a<=2?.5:0,b=!1;if(w>0&&a===1){let S=Math.abs(r[0]-o[0]*l[0]);S<=w&&([w,b]=[S,!0])}else if(w>0&&a===2&&!f){let S=Math.abs(p)/g;S<=w&&([w,b]=[S,!0])}let N=Lm(r,o),v=o.some((S,k)=>{let A=l?.[k]??N;return S*A<5||S*(1-A)<5}),y=0;if(!b)for(let S=0;S<a;S++){let k=l?.[S]??N,A=o[S]*k,q=o[S]*(1-k);y+=Ce(Math.abs(r[S]-A)-w,A),y+=Ce(Math.abs(o[S]-r[S]-q)-w,q)}let d=l!==void 0?a:a-1,M;if(u==="two.sided")M=Ne(y,d/2,2,!1,!1);else{let S=Math.sign(a===1?h[0]-l[0]:p);M=j(S*Math.sqrt(y),{lowerTail:u==="less"})}let x;if(a===1||a===2&&!f){let S=(1-m)/2,k=u==="two.sided"&&S<1e-8?Z(S,{lowerTail:!1}):Z(u==="two.sided"?(1+m)/2:m);if(a===1){let A=o[0],q=k*k,I=(D,F)=>(D+q/(2*A)+F*k*Math.sqrt(D*(1-D)/A+q/(4*A*A)))/(1+q/A),L=h[0]+w/A,O=h[0]-w/A,P=Math.min(1,Math.max(0,L>=1?1:I(L,1))),W=Math.min(1,Math.max(0,O<=0?0:I(O,-1)));x=u==="two.sided"?[W,P]:u==="greater"?[W,1]:[0,P]}else{let A=Math.sqrt(h.reduce((O,P,W)=>O+P*(1-P)/o[W],0)),q=k*A+w*g,I=Math.max(-1,p-q),L=Math.min(1,p+q);x=u==="two.sided"?[I,L]:u==="greater"?[I,1]:[-1,L]}}let E=a===1?"p":c.map(S=>`prop ${S+1}`),R={method:`${a===1?"1-sample proportions test":`${a}-sample test for ${f?"given proportions":"equality of proportions"}`} ${w!==0?"with":"without"} continuity correction`,alternative:u,statistic:y,statisticName:"X-squared",parameter:d,parameterName:"df",pValue:M,estimate:a===1?h[0]:h,estimateNames:E,warnings:v?[_m]:[]};return l!==void 0&&(R.nullValue=a===1?l[0]:l,R.nullValueName=E),x!==void 0&&(R.confInt=x,R.confLevel=m),z("propTest",R)}function Lm(e,t){let n=t.reduce((o,s)=>o+s,0);if(n<1/0)return e.reduce((o,s)=>o+s,0)/n;let r=Math.ceil(Math.log2(t.length));return e.reduce((o,s)=>o+_(s,-r),0)/t.reduce((o,s)=>o+_(s,-r),0)}var Om=["method","confLevel","sides"],Fm=["wilson","wald","wilsoncc","agresti-coull","jeffreys","clopper-pearson","midp"],Cm=["two.sided","left","right"];function wr(e,t){let n=ae(t,Cm),r=e??.95;if(!(typeof r=="number"&&r>0&&r<1))throw new RangeError("'confLevel' must be a single number in (0, 1)");let o=n==="two.sided"?r:1-2*(1-r);if(!(o>=0))throw new RangeError("a one-sided 'confLevel' must be at least 0.5");let s=n!=="two.sided"&&1-r<1e-8?2*(1-r):1-o;return{...Ns(s,o),sides:n}}function Ns(e,t=1-e){let n=e/2<1e-8,r=n?Z(e/2,{lowerTail:!1}):Z(1-e/2),o=n?Jn(e,1,{lowerTail:!1}):Jn(t,1);return{sides:"two.sided",level:t,alpha:e,kappa:r,chisq:o}}function Fn(e,t){if(typeof e!="number")throw new TypeError(`'${t}' must be a number`);if(!Ft(e))throw new RangeError(`'${t}' must be a nonnegative integer`);return Math.round(e)+0}function Ct(e,t,n,r){let o=Fn(e,n),s=Fn(t,r);if(s===0)throw new RangeError(`'${r}' must be positive`);if(s>2**53)throw new RangeError(`'${r}' must be at most 2^53`);if(o>s)throw new RangeError(`'${n}' must not be greater than '${r}'`);return[o,s]}function ho(e,t,n){let r=e/t,o=1-r,s=n*n,i=(e+s/2)/(t+s),c=n*Math.sqrt(t)/(t+s)*Math.sqrt(r*o+s/(4*t)),a=i+c,l=i-c;return e===0?l=0:l<i/2&&(l=r*(e/(t+s))/a),[l,e===t?1:a]}function uo(e,t,n,r){return r?e/2<1e-8?Ut(e/2,t,n,!1,!1):Ut(1-e/2,t,n,!0,!1):Ut(e/2,t,n,!0,!1)}function nc(e,t,n,r){let o=n/2,s=r?f=>{let m=Math.exp(f);return .5*ht(e,t,m,!1)+Ot(e-1,t,m,!0,!1)-o}:f=>{let m=Math.exp(f);return .5*ht(e,t,m,!1)+Ot(e,t,m,!1,!1)-o},i=r?-1:1,c=Math.log(e/t),a=i*s(c);if(a===0)return e/t;if(a>0){let f=c,m=a,u=1;for(;;){let h=c-u;if(h<-745)return 0;let p=i*s(h);if(p<=0)return Math.exp(p===0?h:We(g=>i*s(g),h,f,{tol:2**-60,fLower:p,fUpper:m}));f=h,m=p,u*=2}}let l=i*s(0);return l<=0?1:Math.exp(We(f=>i*s(f),c,0,{tol:2**-60,fLower:a,fUpper:l}))}function Dm(e,t,n={}){C(n,Om,"binomCI");let r=ae(n.method,Fm),o=wr(n.confLevel,n.sides),[s,i]=Ct(e,t,"x","n"),{alpha:c,kappa:a}=o,l=s/i,f=1-l,m,u;switch(r){case"wald":{let h=a*Math.sqrt(l*f/i);[m,u]=[l-h,l+h];break}case"wilson":[m,u]=ho(s,i,a);break;case"wilsoncc":{let h=a*a,p=2*(i+h),g=2*s+h-1,w=a*Math.sqrt(h-2-1/i+4*l*(i*f+1));m=l===0?0:g-w<g/2?(2*s-1)/(2*i)*(2*s-1)/(g+w):(g-w)/p,u=l===1?1:(2*s+h+1+a*Math.sqrt(h+2-1/i+4*l*(i*f-1)))/p;break}case"agresti-coull":{let h=a*a,p=i+h,g=(s+h/2)/p,w=a*Math.sqrt(g*(1-g)/p);[m,u]=[g-w,g+w];break}case"jeffreys":m=s===0?0:uo(c,s+.5,i-s+.5,!1),u=s===i?1:uo(c,s+.5,i-s+.5,!0);break;case"clopper-pearson":m=s===0?0:uo(c,s,i-s+1,!1),u=s===i?1:uo(c,s+1,i-s,!0);break;case"midp":m=s===0?0:s===i?Math.exp(Math.log(c)/i):nc(s,i,c,!1),u=s===i?1:s===0?-Math.expm1(Math.log(c)/i):nc(s,i,c,!0);break}return m=Math.max(0,m),u=Math.min(1,u),o.sides==="left"?u=1:o.sides==="right"&&(m=0),z("binomCI",{estimate:l,confInt:[m,u],confLevel:n.confLevel??.95,method:r,sides:o.sides,warnings:[]})}function br(e){return e===1/0?1:e/(1+e)}function vr(e,t){return e===0?0:t>0?e*e/t:1/0}function Pm(e,t,n,r){return e/t+n/r>1}function Vm(e,t,n,r){return go(e,t,n,r,0)}function Es(e,t,n,r,o){if(Pm(e,t,n,r)){let[p,g,w,b]=Es(t-e,t,r-n,r,-o);return[g,p,b,w]}let s=o<=0,[i,c,a,l]=s?[e,t,n,r]:[n,r,e,t],f=Math.abs(o),m=1-f,u=0;if(m>0){let p=b=>{let N=0;return i>0&&(N+=i/b),i<c&&(N-=(c-i)/(1-b)),a>0&&(N+=a/(b+f)),a<l&&(N-=(l-a)/(m-b)),N},g=b=>{let N=0;return i>0&&(N-=i/(b*b)),i<c&&(N-=(c-i)/((1-b)*(1-b))),a>0&&(N-=a/((b+f)*(b+f))),a<l&&(N-=(l-a)/((m-b)*(m-b))),N};if(p(0)<=0)u=0;else if(p(m)>=0)u=m;else{let b=0,N=m,v=$m(e,t,n,r,o);u=s?v:v-o,u>b&&u<N||(u=N/2);for(let y=0;y<2e3;y++){let d=p(u);if(d===0)break;d>0?b=u:N=u;let M=u-d/g(u);if(M>b&&M<N||(M=b===0?N/2:N>2*b?Math.sqrt(b*N):b+(N-b)/2),!(M>b&&M<N))break;let x=Math.abs(M-u);if(u=M,x<=2**-53*u)break}}}let h=[u,1-u,Math.min(1,u+f),Math.max(0,m-u)];return s?h:[h[2],h[3],h[0],h[1]]}function $m(e,t,n,r,o){let s=e/t,i=n/r,c=r/t,a=1+c,l=-(1+c+s+c*i+o*(c+2)),f=o*o+o*(2*s+c+1)+s+c*i,m=-s*o*(1+o),u=l/(3*a),h=u*u*u-l*f/(6*a*a)+m/(2*a),p=Math.sign(h)*Math.sqrt(Math.max(0,u*u-f/(3*a)));if(p===0)return-u;let g=(Math.PI+Math.acos(Math.max(-1,Math.min(1,h/(p*p*p)))))/3;return 2*p*Math.cos(g)-u}function xs(e,t,n,r,o,s){if(s==="wald")return e/t*((t-e)/t)/t+n/r*((r-n)/r)/r;let[i,c,a,l]=Es(e,t,n,r,o),f=i*c/t+a*l/r;if(s==="mee")return f;let m=t+r;return f*m/(m-1)}function zm(e,t,n,r,o,s,i){let c=ie(ie(Ie([e,0],t),Ie([n,0],r)),[o,0]),[a,l,f,m]=Es(e,t,n,r,o),u=G(Ie(xt(a,l),t),Ie(xt(f,m),r));return s==="mn"&&(u=Ie(Se(u,t+r),t+r-1)),ie(ce(c,c),Se(u,i))[0]}function go(e,t,n,r,o){return ie(ie(Ie([e,0],t),Ie([n,0],r)),[o,0])[0]}function Km(e,t,n,r,o,s){return vr(go(e,t,n,r,o),xs(e,t,n,r,o,s))}var po=new Float64Array(1),Ms=new BigInt64Array(po.buffer);function rc(e){return po[0]=Math.abs(e),e<0?-Ms[0]:Ms[0]}function Zt(e){return Ms[0]=e<0n?-e:e,e<0n?-po[0]:po[0]}function ys(e,t,n,r,o){let s=e(t);if(s===0||Number.isNaN(s))return t;let i=s<0===o,c=rc(i?r:n),a=rc(t),l=s,f=a,m=s,u=1n;for(;;){if(f=i?a+u:a-u,(i?f>=c:f<=c)&&(f=c),m=e(Zt(f)),m===0)return Zt(f);if(m<0!=l<0||f===c)break;if(a=f,l=m,u*=2n,u>1n<<40n)return Zt(a)}if(m<0==l<0||Number.isNaN(m))return Zt(a);for(;f-a>1n||a-f>1n;){let h=(a+f)/2n,p=e(Zt(h));if(p===0)return Zt(h);p<0==l<0?(a=h,l=p):(f=h,m=p)}return Math.abs(m)<Math.abs(l)?Zt(f):Zt(a)}function sc(e,t,n,r,o,s){let i=Vm(e,t,n,r);if(o===0)return[i,i];let c=br(o),a=p=>(p===i?0:br(Km(e,t,n,r,p,s)))-c,l=(p,g,w,b)=>{if(w===0)return p;if(b===0)return g;let N=We(a,p,g,{tol:1e-300,fLower:w,fUpper:b});return s==="wald"?N:ys(v=>zm(e,t,n,r,v,s,o),N,p,g,w<0)},f=a(-1),m=a(1),u=i<=-1||f<=0?-1:l(-1,i,f,-c),h=i>=1||m<=0?1:l(i,1,-c,m);return[u,h]}function oc(e,t,n){let r=t,o=e(t);if(o>=0)return Math.exp(t);let s=1;for(;;){let i=t+n*s;if(Math.abs(i)>745)return n<0?0:1/0;let c=e(i);if(c>=0){if(c===0)return Math.exp(i);let a=n>0?We(e,r,i,{tol:2**-60,fLower:o,fUpper:c}):We(e,i,r,{tol:2**-60,fLower:c,fUpper:o});return Math.exp(a)}r=i,o=c,s*=2}}function Wm(e,t,n,r,o){let s=t+r,i=o*(t+n)+e+r,c=o*(e+n),a=Math.sqrt(Math.max(0,i*i-4*s*c)),l=Math.min(1,o,c===0?0:2*c/(i+a));return[l,Math.min(1,l/o)]}function jm(e,t,n,r,o){let[s,i]=Wm(e,t,n,r,o);if(!(o>0&&o<1/0))return[s,1-s,i,1-i];let c=s<=.5&&i<=.5?0:o*(1-i)<=1-s?2:3,a=c===0?i:c===2?1-i:1-s,l=M=>c===0?[M,0]:c===2?He(1,-M):Ie(He(1,-M),o),f=c===3?1/o:1,m=c===0?Math.min(1,1/o):1,u=c===2?o>1?Ie(He(o,-1),o)[0]:0:c===3&&o<1?He(1,-o)[0]:0,h=M=>{let x=l(M),E=Se(x,o);return[E[0],ie([1,0],E)[0],x[0],ie([1,0],x)[0]]},p=e+n,g=c<2?1:-1,w=M=>{let[,x,E,T]=h(M),R=0,S=0;return p>0&&(R+=E>0?p/E:1/0,S-=p/(E*E)),e<t&&(R-=x>0?o*(t-e)/x:1/0,S-=o*(t-e)/x*(o/x)),n<r&&(R-=T>0?(r-n)/T:1/0,S-=(r-n)/(T*T)),[g*R,S*f]},b;if(!(m>u))b=u;else if(!(w(u)[0]>0))b=u;else if(!(w(m)[0]<0))b=m;else{let M=u,x=m;b=a,b>M&&b<x||(b=M+(x-M)/2);for(let E=0;E<2e3;E++){let[T,R]=w(b);if(T===0)break;T>0?M=b:x=b;let S=b-T/R;if(S>M&&S<x||(S=M<=0?x/2:x>2*M?Math.sqrt(M*x):M+(x-M)/2),!(S>M&&S<x))break;let k=Math.abs(S-b);if(b=S,k<=2**-53*b)break}}let[N,v,y,d]=h(b);return[Math.min(1,Math.max(0,N)),Math.min(1,Math.max(0,v)),Math.min(1,Math.max(0,y)),Math.min(1,Math.max(0,d))]}function Um(e,t,n,r,o){let[s,i,c,a]=jm(e,t,n,r,o),l=(f,m,u,h)=>u>.5?m*h-(m-f):f-m*u;return vr(l(e,t,s,i),t*s*i)+vr(l(n,r,c,a),r*c*a)}function ac(e,t,n,r,o){let s=e/t/(n/r);return cc(s,o,i=>Um(e,t,n,r,i))}function Gm(e,t,n,r,o){let s=e+n,i=r*(o-1),c=t*o+r-s*(o-1),a=s===0?0:Math.min(1,2*s/(c+Math.sqrt(Math.max(0,c*c+4*i*s))));return[o===1/0?a>0?1:0:Math.min(1,a*o/(1+a*(o-1))),a]}function Ym(e,t,n,r,o){let[s,i]=Gm(e,t,n,r,o),c=e-t*s;return vr(c,t*s*(1-s))+vr(c,r*i*(1-i))}function ic(e,t,n,r,o){let s=e*(r-n)/((t-e)*n);return cc(s,o,i=>Ym(e,t,n,r,i))}function cc(e,t,n){if(t===0)return[e,e];let r=br(t),o=l=>n(l)-t,s=l=>l>0&&l<e?ys(o,l,Number.MIN_VALUE,e,!1):l,i=l=>l>e&&l<1/0?ys(o,l,e,Number.MAX_VALUE,!0):l;if(e===0||e===1/0){let l=u=>br(n(u))-r,f=l(1),m=f===0?1:Bt(l,e===0?f>0:f<0);return e===0?[0,i(m)]:[s(m),1/0]}let c=Math.log(e),a=l=>(l===c?0:br(n(Math.exp(l))))-r;return[s(oc(a,c,-1)),i(oc(a,c,1))]}var Hm=["method","confLevel","sides"],Xm=["method","confLevel","sides"],Bm=["method","confLevel"],Qm=["ac","wald","score","mn","mee"];function Ts(e,t,n,r,o,s){let{kappa:i}=s,c=e/t,a=n/r,l=c-a,f,m;switch(o){case"wald":{let u=i*Math.sqrt(c*(1-c)/t+a*(1-a)/r);[f,m]=[Math.max(-1,l-u),Math.min(1,l+u)];break}case"ac":{let u=(e+1)/(t+2),h=(n+1)/(r+2),p=u-h,g=i*Math.sqrt(u*(1-u)/(t+2)+h*(1-h)/(r+2));[f,m]=[Math.max(-1,p-g),Math.min(1,p+g)];break}case"score":{let[u,h]=ho(e,t,i),[p,g]=ho(n,r,i);f=l-i*Math.sqrt(u*(1-u)/t+g*(1-g)/r),m=l+i*Math.sqrt(h*(1-h)/t+p*(1-p)/r);break}case"mn":case"mee":[f,m]=sc(e,t,n,r,s.chisq,o);break}return[Math.min(f,m),Math.max(f,m)]}function Zm(e,t,n,r,o={}){C(o,Hm,"binomDiffCI");let s=ae(o.method,Qm),i=wr(o.confLevel,o.sides),[c,a]=Ct(e,t,"x1","n1"),[l,f]=Ct(n,r,"x2","n2"),m=c/a-l/f,[u,h]=Ts(c,a,l,f,s,i);return i.sides==="left"?h=1:i.sides==="right"&&(u=-1),z("binomDiffCI",{estimate:m,confInt:[u,h],confLevel:o.confLevel??.95,method:s,sides:i.sides,warnings:[]})}var Jm=["katz.log","koopman"];function wo(e,t,n,r,o){let s=e/t/(n/r),i=o*Math.sqrt(1/e-1/t+1/n-1/r);return[s*Math.exp(-i),s*Math.exp(i)]}function eu(e,t,n,r,o={}){C(o,Xm,"binomRatioCI");let s=ae(o.method,Jm),i=wr(o.confLevel,o.sides),[c,a]=Ct(e,t,"x1","n1"),[l,f]=Ct(n,r,"x2","n2"),m=i.kappa,u=c===0?0:c/a/(l/f),h,p;return c===0&&l===0?[h,p]=[0,1/0]:s==="katz.log"?c===0?[h,p]=[0,wo(.5,a,l,f,m)[1]]:l===0?[h,p]=[wo(c,a,.5,f,m)[0],1/0]:c===a&&l===f?[h,p]=wo(a-.5,a,f-.5,f,m):[h,p]=wo(c,a,l,f,m):[h,p]=ac(c,a,l,f,i.chisq),i.sides==="left"?p=1/0:i.sides==="right"&&(h=0),z("binomRatioCI",{estimate:u,confInt:[h,p],confLevel:o.confLevel??.95,method:s,sides:i.sides,warnings:[]})}var tu=["midp","fisher","wald","score"],nu={midp:"median-unbiased estimate & mid-p exact CI",fisher:"Conditional MLE & exact CI from 'fisher.test'",wald:"Unconditional MLE & normal approximation (Wald) CI",score:"Unconditional MLE & score CI"},ru=2**23,ou=2**20;function lc(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=G(r,[e[o],0]));return r[0]}function su(e,t={}){C(t,Bm,"oddsRatio");let n=ae(t.method,tu);if(e.length!==2||e.some(S=>S.length!==2))throw new TypeError("'table' must be a 2 x 2 table");let r=t.confLevel??.95,{alpha:o,kappa:s,chisq:i}=wr(r,void 0),c=e.map((S,k)=>Array.from(S,(A,q)=>Fn(A,`table[${k}][${q}]`))),[[a,l],[f,m]]=c,u=a+l+f+m;if(u===0)throw new RangeError("all entries of the table are zero");if(!(u<=2**53))throw new RangeError("the total of the table must be at most 2^53");let h=Math.max(0,a-m),p=a+Math.min(l,f),g=p-h+1;if(n==="midp"||n==="fisher"){if(g>ru)throw new RangeError(`the exact distribution of the 2 x 2 table has ${g} values, more than 2^23: use method "wald" or "score"`);if(n==="fisher"&&Math.max(a,l,f,m)>2147483647)throw new RangeError(`'table' has entries too large for the conditional MLE (above 2^31 - 1): use method "wald" or "score"`)}let w,b,N;try{w=at(a-1,a+f,l+m,a+l,!0,!1)+.5*Nt(a,a+f,l+m,a+l,!1),b=at(a,a+f,l+m,a+l,!1,!1)+.5*Nt(a,a+f,l+m,a+l,!1),N=g<=ou?os(new Ln(a+f,l+m,a+l),a,1):Oi(a,a+f,l+m,a+l)}catch(S){throw S instanceof RangeError?new RangeError(`the exact p-values of the table are beyond reach: ${S.message}`):S}let v=2*Math.min(w,b),y=[a+l,f+m],d=[a+f,l+m],M=0,x=!1;for(let S=0;S<2;S++)for(let k=0;k<2;k++){let A=y[k]*d[S]/u;A<5&&(x=!0);let q=Math.abs(c[k][S]-A);M+=q*q/A}let E=Ne(M,.5,2,!1,!1),T,R;switch(n){case"wald":{T=a*m/(l*f);let S=s*Math.sqrt(1/a+1/l+1/f+1/m);R=[Math.exp(Math.log(T)-S),Math.exp(Math.log(T)+S)];break}case"fisher":{let S=we(()=>pr(c,{confLevel:r}));T=S.estimate,R=S.confInt;break}case"score":{T=a*m/(l*f),R=(a===0||m===0)&&(l===0||f===0)?[0,1/0]:ic(a,a+l,f,f+m,i);break}case"midp":{let S=new Ln(a+f,l+m,a+l),k=L=>{if(L===1)return[w,b];let O=S.density(L),P=a-h,W=.5*O[P];return[lc(O,0,P-1)+W,lc(O,P+1,O.length-1)+W]},A=(L,O)=>{let P=L(1);return P===0?1:Bt(L,O?P>0:P<0)};T=a===h?0:a===p?1/0:A(L=>k(L)[0]-.5,!1);let q=a===h?0:A(L=>k(L)[1]-o/2,!0),I=a===p?1/0:A(L=>k(L)[0]-o/2,!1);R=[q,I];break}}return z("oddsRatio",{estimate:T,confInt:R,confLevel:r,method:nu[n],pValue:{midp:v,fisher:N,chiSquare:E},warnings:x?["Chi-squared approximation may be incorrect"]:[]})}var au=["epsilon","paired","varEqual","confLevel","alpha"],iu=["margin","method","alpha"];function fc(e,t){if(!(typeof e=="number"&&e>0&&e<.5))throw new RangeError(`'${t}' must be a single number in (0, 0.5)`)}function cu(e,t,n={}){C(n,au,"tost");let r=n.epsilon??1;if(!(typeof r=="number"&&r>0&&r<1/0))throw new RangeError("'epsilon' must be a positive number");let o,s;if(n.alpha!==void 0)fc(n.alpha,"alpha"),o=1-n.alpha,s=n.alpha;else{if(o=n.confLevel??.95,Xe(o),!(o>.5))throw new RangeError("'confLevel' must be greater than 0.5");s=1-o}if(t===void 0)throw new TypeError("'y' is required");let i=n.paired??!1,c=n.varEqual??!1,a=we(()=>Xt(e,t,{paired:i,varEqual:c})),l=a.stdErr,f=a.parameter,m=typeof a.estimate=="number"?a.estimate:a.estimate[0]-a.estimate[1],u=De((r-Math.abs(m))/l,f,!1,!1),h=s<1e-8?st(s,f,!1,!1):st(o,f,!0,!1),p=[m-h*l,m+h*l];return z("tost",{method:a.method.replace("t-test","TOST"),parameter:f,parameterName:"df",pValue:u,result:p[0]>-r&&p[1]<r?"rejected":"not rejected",confInt:p,confLevel:o,estimate:a.estimate,estimateNames:i?"mean of the differences":a.estimateNames,epsilon:r,seDiff:l,warnings:[]})}var lu=["mn","mee","wald"];function fu(e,t,n,r,o={}){C(o,iu,"binomDiffEquivalenceTest");let s=ae(o.method,lu),i=o.margin;if(i===void 0)throw new TypeError("'margin' is required");if(!(typeof i=="number"&&i>0&&i<1))throw new RangeError("'margin' must be a single number in (0, 1)");let c=o.alpha??.05;fc(c,"alpha");let[a,l]=Ct(e,t,"x1","n1"),[f,m]=Ct(n,r,"x2","n2"),u=a/l-f/m,h=y=>go(a,l,f,m,y)/Math.sqrt(xs(a,l,f,m,y,s)),p=h(-i),g=h(i),w=[j(p,{lowerTail:!1}),j(g)],b=Math.max(w[0],w[1]),N=1-2*c,v=Ts(a,l,f,m,s,Ns(2*c,N));return z("binomDiffEquivalenceTest",{method:s,estimate:u,margin:i,alpha:c,statistic:[p,g],statisticName:"z",pValues:w,pValue:b,result:w[0]<c&&w[1]<c?"rejected":"not rejected",confInt:v,confLevel:N,warnings:[]})}var mu=["alternative","tsmethod"],uu=["square","central"],hu=1e3,pu=32,du=.9,mc=1+1e-7,gu=2**24,wu=34e7;function bu(e,t={}){if(C(t,mu,"boschlooTest"),e.length!==2||e.some(M=>M.length!==2))throw new TypeError("'table' must be a 2 x 2 table");let n=le(t.alternative),r=ae(t.tsmethod,uu),o=e.map((M,x)=>Array.from(M,(E,T)=>Fn(E,`table[${x}][${T}]`))),[[s,i],[c,a]]=o,l=s+i,f=c+a;if(l===0||f===0)throw new RangeError("each row (group) of 'table' must have at least one observation");if((l+1)*(f+1)>gu)throw new RangeError(`Boschloo's test of these group sizes has ${(l+1)*(f+1)} possible tables, more than 2^24`);let m=l+f,u=Math.max(hu,Math.ceil(pu*Math.sqrt(m))),h=M=>Math.min(M+1,40*Math.sqrt(M));if(u*(h(l)+h(f)+4)>wu)throw new RangeError(`Boschloo's test of these group sizes (${l} and ${f}) would take too long`);let p=s/l-c/f,g;n!=="two.sided"?g=n:r==="square"?g="square":g=p>0?"greater":"less";let w=s+c,b;g==="less"?b=at(s,l,f,w,!0,!1):g==="greater"?b=at(s-1,l,f,w,!1,!1):b=we(()=>pr(o)).pValue;let N=vu(l,f,s,c,g),{value:v,at:y}=Nu(N,l,f,u),d=n==="two.sided"&&r==="central"?Math.min(1,2*v):v;return z("boschlooTest",{method:"Boschloo's Exact Test",alternative:n,statistic:b,statisticName:"test statistic",pValue:d,estimate:p,estimateNames:"difference in proportion",nullValue:0,nullValueName:"difference in proportion",nuisanceParameter:y,warnings:[]})}function vu(e,t,n,r,o){let s=e+t,i=new Float64Array(s+1);for(let b=2;b<=s;b++)i[b]=jr(b);let c=t+1,a=new Float64Array((e+1)*c),l=Math.min(e,t)+1,f=new Float64Array(l),m=new Float64Array(l),u=new Float64Array(l);for(let b=0;b<=s;b++){let N=Math.max(0,b-t),y=Math.min(b,e)-N+1,d=-1/0;for(let E=0;E<y;E++){let T=N+E,R=-i[T]-i[e-T]-i[b-T]-i[t-b+T];f[E]=R,R>d&&(d=R)}let M=0;for(let E=0;E<y;E++)f[E]=Math.exp(f[E]-d),M+=f[E];for(let E=0;E<y;E++)f[E]=f[E]/M;let x=0;for(let E=0;E<y;E++)m[E]=x+=f[E];x=0;for(let E=y-1;E>=0;E--)u[E]=x+=f[E];if(o==="less")for(let E=0;E<y;E++)a[(N+E)*c+b-N-E]=m[E];else if(o==="greater")for(let E=0;E<y;E++)a[(N+E)*c+b-N-E]=u[E];else{let E=0;for(let T=1;T<y;T++)f[T]>f[E]&&(E=T);for(let T=0;T<y;T++){let R=f[T]*mc,S=-1,k=E;for(;k-S>1;){let L=S+k>>1;f[L]<=R?S=L:k=L}let A=E,q=y;for(;q-A>1;){let L=A+q>>1;f[L]<=R?q=L:A=L}let I=f[E]<=R?1:(S>=0?m[S]:0)+(q<y?u[q]:0);a[(N+T)*c+b-N-T]=Math.min(1,I)}}}let h=a[n*c+r]*mc,p=new Int32Array(e+2),g=[],w=[];for(let b=0;b<=e;b++){p[b]=g.length;let N=0;for(;N<=t;){if(a[b*c+N]<=h){let v=N;for(;N+1<=t&&a[b*c+N+1]<=h;)N++;g.push(v),w.push(N)}N++}}return p[e+1]=g.length,{starts:p,from:Int32Array.from(g),to:Int32Array.from(w)}}function uc(e,t,n){if(t===0||t===1){let h=t===0?0:e;return n[h]=1,[h,h]}let r=Math.min(e,Math.floor((e+1)*t));n[r]=ht(r,e,t,!1);let o=He(1,-t),s=ge([t,0],o),i=ge(o,[t,0]),c=s[0],a=i[0],l=r;for(let h=r+1;h<=e;h++){let p=n[h-1]*((e-h+1)/h*c);if(p===0)break;n[h]=p,l=h}let f=r;for(let h=r-1;h>=0;h--){let p=n[h+1]*((h+1)/(e-h)*a);if(p===0)break;n[h]=p,f=h}let m=s[1]/c,u=i[1]/a;for(let h=r+1;h<=l;h++)n[h]=n[h]*(1+(h-r)*m);for(let h=f;h<r;h++)n[h]=n[h]*(1+(r-h)*u);return[f,l]}function Nu(e,t,n,r){let{starts:o,from:s,to:i}=e,c=new Float64Array(t+1),a=new Float64Array(n+1),l=new Float64Array(n+1),f=new Float64Array(n+1),m=v=>{let[y,d]=uc(t,v,c),[M,x]=uc(n,v,a),E=0;for(let R=M;R<=x;R++)l[R]=E+=a[R];E=0;for(let R=x;R>=M;R--)f[R]=E+=a[R];let T=0;for(let R=y;R<=d;R++){let S=c[R];if(S===0)continue;let k=0;for(let A=o[R];A<o[R+1];A++){let q=s[A],I=i[A];if(!(I<M||q>x))if(q===0)k+=l[Math.min(I,x)];else if(I===n)k+=f[Math.max(q,M)];else for(let L=Math.max(q,M);L<=Math.min(I,x);L++)k+=a[L]}T+=S*k}return Math.min(1,T)},u=Math.PI/2,h=v=>v<=0?0:v>=u?1:Math.sin(v)**2,p=new Float64Array(r+2),g=0;for(let v=0;v<=r+1;v++)p[v]=m(h(u*v/(r+1))),p[v]>p[g]&&(g=v);let w=p[g],b=h(u*g/(r+1));if(w>=1)return{value:1,at:b};let N=du*w;for(let v=0;v<=r+1;v++){let y=p[v];if(y<N||v>0&&!(y>p[v-1])||v<=r&&!(y>=p[v+1]))continue;let d=u*Math.max(0,v-1)/(r+1),M=u*Math.min(r+1,v+1)/(r+1),E=ri(S=>-m(h(S)),d,M,1e-14).x,T=m(h(E)),R=1e-4/Math.sqrt(2*(t+n));for(let S=0;S<2&&(E-R>d&&E+R<M);S++,R*=.01){let k=m(h(E-R)),A=m(h(E+R)),q=k-2*T+A;if(!(q<0))break;let I=R*(k-A)/(2*q);if(!(Math.abs(I)<R))break;let L=m(h(E+I));L>T*(1+2**-50)&&(E+=I,T=L)}T>w&&(w=T,b=h(E))}return{value:w,at:b}}function Cn(e){let t=Pe(e);return t.some(n=>typeof n=="string")?t.map(n=>typeof n=="number"&&!Number.isNaN(n)?Ve(n):n):t}function Nr(e,t){return typeof e=="number"&&typeof t=="number"?e-t:pt(String(e),String(t))}function Jt(e,t,n="drop"){if(t===void 0){let c=e;if(c.length>0&&typeof c[0]=="number")throw new TypeError("'g' is required when 'x' is a single sample");let a=c.map(m=>Array.from(m).filter(u=>!Number.isNaN(u)));if(n==="error"&&a.some(m=>m.length===0))throw new RangeError("all groups must contain data");let l=[],f=[];return a.forEach((m,u)=>{m.length!==0&&(l.push(String(u+1)),f.push(m))}),{labels:l,groups:f}}let r=Array.from(e);if(r.length!==t.length)throw new RangeError("'x' and 'g' must have the same length");let o=Cn(t),s=new Map;for(let c=0;c<r.length;c++){let a=r[c],l=o[c];if(Number.isNaN(a)||typeof l=="number"&&Number.isNaN(l))continue;let f=s.get(l);f===void 0&&s.set(l,f=[]),f.push(a)}let i=Array.from(s.keys()).sort(Nr);return{labels:i.map(Ve),groups:i.map(c=>s.get(c))}}function hn(e,t,n="drop"){return Jt(e,t,n).groups}function bo(e){let t=[...e].sort((r,o)=>r-o),n=t.length;return n%2===1?t[(n-1)/2]:Gt(t[n/2-1],t[n/2])}function Rs(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))}var hc=["correct"];function Mu(e,t,n={}){ze(t)&&C(t,hc,"mcnemarTest"),C(n,hc,"mcnemarTest");let r;if(e.length>0&&Array.isArray(e[0])){t!==void 0&&!Array.isArray(t)&&(n=t),r=e.map(f=>Array.from(f));let l=r.length;if(l<2||r.some(f=>f.length!==l))throw new RangeError("'x' must be square with at least two rows and columns");if(r.some(f=>f.some(m=>!Number.isFinite(m)||m<0)))throw new RangeError("all entries of 'x' must be nonnegative and finite")}else{if(!Array.isArray(t))throw new TypeError("'y' is required when 'x' is not a table");r=yu(e,t)}let o=n.correct??!0,s=r.length,i,c=o&&s===2&&r[0][1]!==r[1][0];if(c){let l=r[0][1],f=r[1][0];i=pc(Math.max(0,Math.abs(l-f)-1),l,f)}else{i=0;let l=0;for(let f=0;f<s;f++)for(let m=f+1;m<s;m++){let u=r[f][m],h=r[m][f];u+h!==0&&(i+=pc(u-h,u,h),l++)}if(l===0)throw new RangeError("the table has no discordant pairs")}let a=s*(s-1)/2;return{method:c?"McNemar's Chi-squared test with continuity correction":"McNemar's Chi-squared test",statistic:i,statisticName:"McNemar's chi-squared",parameter:a,parameterName:"df",pValue:Ne(i,a/2,2,!1,!1),warnings:[]}}function pc(e,t,n){let r=t+n;return r<1/0?Ce(e,r):Ce(_(e,-520),_(t,-1040)+_(n,-1040))}function yu(e,t){if(e.length!==t.length)throw new RangeError("'x' and 'y' must have the same length");let n=[],r=Cn(e),o=Cn(t);for(let m=0;m<e.length;m++){let u=r[m],h=o[m];typeof u=="number"&&Number.isNaN(u)||typeof h=="number"&&Number.isNaN(h)||n.push([u,h])}let s=m=>Array.from(new Set(m)).sort(Nr),i=s(n.map(m=>m[0])),c=s(n.map(m=>m[1]));if(i.length<2||i.length!==c.length)throw new RangeError("'x' and 'y' must have the same number of levels (minimum 2)");let a=new Map(i.map((m,u)=>[m,u])),l=new Map(c.map((m,u)=>[m,u])),f=i.map(()=>new Array(i.length).fill(0));for(let[m,u]of n)f[a.get(m)][l.get(u)]++;return f}var Eu=["ratio","alternative","confLevel"],Ss=2**-1022;function dc(e,t,n={}){if(C(n,Eu,"varTest"),typeof e=="string"){let v=Yt(e,t);return dc(v.x,v.y,n)}let r=Array.from(e).filter(Number.isFinite),o=Array.from(t).filter(Number.isFinite),s=n.ratio??1;if(!(Number.isFinite(s)&&s>0))throw new RangeError("'ratio' must be a single positive number");let i=le(n.alternative),c=n.confLevel??.95;Xe(c);let a=r.length-1,l=o.length-1;if(a<1)throw new Error("not enough 'x' observations");if(l<1)throw new Error("not enough 'y' observations");let[f,m]=Le([r,o]).variance,u=0;if(!(f>=Ss&&m>=Ss)){let v=Le([r]),y=Le([o]);f=v.variance[0],m=y.variance[0],u=2*(v.k-y.k)}if(f===0&&m===0)throw new RangeError("both samples have zero variance");let h=u===0?f/m:$e(f/m,u),p=h/s,g=Me(p,a,l,!0,!1),w=Me(p,a,l,!1,!1),b,N;if(i==="less")b=g,N=[0,h/Lt(1-c,a,l,!0,!1)];else if(i==="greater")b=w,N=[h/Lt(c,a,l,!0,!1),1/0];else{b=Math.min(1,2*Math.min(g,w));let v=(1-c)/2,y=v<1e-8?Lt(v,a,l,!1,!1):Lt(1-v,a,l,!0,!1);N=[h/y,h/Lt(v,a,l,!0,!1)]}return{method:"F test to compare two variances",alternative:i,statistic:p,statisticName:"F",parameter:[a,l],parameterName:["num df","denom df"],pValue:b,confInt:N,confLevel:c,estimate:h,estimateNames:"ratio of variances",nullValue:s,nullValueName:"ratio of variances",warnings:[]}}function gc(e,t){if(typeof e=="string"){let p=ar(e,t);return gc(ti(p.values,p.group))}let n=e,r=n.length;if(r<2)throw new RangeError("'x' must be a list with at least 2 elements");let o=n.map(p=>Array.from(p).filter(Number.isFinite));if(o.some(p=>p.length<2))throw new RangeError("there must be at least 2 observations in each group");let s=o.map(p=>p.length-1),i=Le(o).variance,c=0,a=0,l=0,f=0;for(let p=0;p<r;p++)c+=s[p],a+=s[p]*i[p],l+=s[p]*Math.log(i[p]),f+=1/s[p];a/=c;let m=c*Math.log(a)-l;if(i.some(p=>p<Ss)){let p=o.map(y=>{let d=Le([y]);return{w:d.variance[0],e:d.k}}),g=Math.max(...p.map(y=>y.e)),w=p.map(y=>Math.log(y.w)+2*(y.e-g)*Math.LN2),b=w.map((y,d)=>Math.log(s[d])+y),N=Math.max(...b),v=N+Math.log(b.reduce((y,d)=>y+Math.exp(d-N),0))-Math.log(c);m=c*v-w.reduce((y,d,M)=>y+s[M]*d,0)}let u=m/(1+(f-1/c)/(3*(r-1))),h=r-1;return{method:"Bartlett test of homogeneity of variances",statistic:u,statisticName:"Bartlett's K-squared",parameter:h,parameterName:"df",pValue:Ne(u,h/2,2,!1,!1),warnings:[]}}var xu=["alternative"];function ks(e,t,n,r){return{method:e,statistic:n,statisticName:t,parameter:r,parameterName:"df",pValue:Ne(n,r/2,2,!1,!1),warnings:[]}}function wc(e){let t=0;for(let n of vt(e))t+=n*n*n-n;return t}function vo(e,t){if(typeof e=="string"){let l=ar(e,t);return vo(l.values,l.group.codes)}let n=hn(e,t,"error"),r=n.length;if(r<2)throw new RangeError("all observations are in the same group");let o=n.flat(),s=o.length,i=ye(o),c=0,a=0;for(let l of n){let f=0;for(let m=0;m<l.length;m++)f+=i[a+m];a+=l.length,c+=f*f/l.length}return c=(12*c/(s*(s+1))-3*(s+1))/(1-wc(o)/(s*s*s-s)),ks("Kruskal-Wallis rank sum test","Kruskal-Wallis chi-squared",c,r-1)}function As(e,t){if(typeof e=="string")return As(Xo(e,t));let n=Rs(e),r=n.length,o=e[0]?.length??0;if(r<2||o<2)throw new RangeError("'y' must have at least two complete rows and two columns");let s=new Array(o).fill(0),i=0;for(let l of n){let f=ye(l);for(let m=0;m<o;m++)s[m]+=f[m];i+=wc(l)}let c=0;for(let l of s)c+=(l-r*(o+1)/2)**2;let a=12*c/(r*o*(o+1)-i/(o-1));return ks("Friedman rank sum test","Friedman chi-squared",a,o-1)}function Tu(e){if(e.some(t=>t.some(n=>!Number.isNaN(n)&&n!==0&&n!==1)))throw new RangeError("the responses must be 0 or 1 (or NaN)");return{...As(e),method:"Cochran's Q test",statisticName:"Q"}}function bc(e,t){if(typeof e=="string")return bc(Xo(e,t));let n=Rs(e),r=n.length,o=e[0]?.length??0;if(n.length<2||o<2)throw new RangeError("'y' must have at least two complete rows and two columns");let s=n.map(g=>ve(g)-it(g));s.some(g=>g===1/0)&&n.every(g=>g.every(Number.isFinite))&&(s=ee(n).groups.map(g=>ve(g)-it(g)));let i=ye(s),c=new Array(o).fill(0),a=0;n.forEach((g,w)=>{let b=ye(g);for(let N=0;N<o;N++){let v=i[w]*(b[N]-(o+1)/2);a+=v*v,c[N]+=v}});let l=0;for(let g of c)l+=g*g;l/=r;let f=o-1,m=(r-1)*(o-1),u,h,p=[f,m];if(a===l){u=NaN,p=[NaN,NaN];let g=0;for(let w=2;w<=o;w++)g+=Math.log(w);h=Math.exp((1-r)*g)}else u=(r-1)*l/(a-l),h=Me(u,f,m,!1,!1);return{method:"Quade test",statistic:u,statisticName:"Quade F",parameter:p,parameterName:["num df","denom df"],pValue:h,warnings:[]}}function vc(e,t){if(typeof e=="string"){let h=ar(e,t);return vc(h.values,h.group.codes)}let n=hn(e,t,"error"),r=n.length;if(r<2)throw new RangeError("all observations are in the same group");let o=h=>h.flatMap(p=>{let g=bo(p);return p.map(w=>Math.abs(w-g))}),s=o(n);if(s.some(Number.isNaN))throw new RangeError("a group has an infinite median: its deviations are undefined");s.some(h=>h===1/0)&&n.every(h=>h.every(Number.isFinite))&&(s=o(ee(n).groups));let i=s.length,a=ye(s).map(h=>Z((1+h/(i+1))/2)),l=0;for(let h of a)l+=h;let f=l/i,m=0,u=0;for(let h of n){let p=0;for(let g=0;g<h.length;g++)p+=a[u+g];u+=h.length,m+=h.length*(p/h.length-f)**2}return m/=_e(a),ks("Fligner-Killeen test of homogeneity of variances","Fligner-Killeen:med chi-squared",m,r-1)}function Nc(e,t,n={}){if(C(n,xu,"moodTest"),typeof e=="string"){let y=Yt(e,t);return Nc(y.x,y.y,n)}let r=Array.from(e).filter(Number.isFinite),o=Array.from(t).filter(Number.isFinite),s=le(n.alternative),i=r.length,c=o.length,a=i+c;if(a<3)throw new RangeError("not enough observations");if(i===0||c===0)throw new RangeError("not enough observations in each sample");let l=r.concat(o),f=Array.from({length:a},(y,d)=>d).sort((y,d)=>l[y]-l[d]),m=new Array(a),u=0;for(;u<a;){let y=u;for(;y+1<a&&l[f[y+1]]===l[f[u]];)y++;let d=0;for(let M=u;M<=y;M++)d+=(M+1-(a+1)/2)**2;d/=y-u+1;for(let M=u;M<=y;M++)m[f[M]]=d;u=y+1}let h=0;for(let y=0;y<i;y++)h+=m[y];let p=0;for(let y of m)p+=y;p/=a;let g=0;for(let y of m)g+=(y-p)**2;let w=i*p,b=i*c/(a*(a-1))*g,N=(h-w)/Math.sqrt(b),v=s==="less"?j(N):s==="greater"?j(N,{lowerTail:!1}):2*j(-Math.abs(N));return{method:"Mood two-sample test of scale",alternative:s,statistic:N,statisticName:"Z",pValue:v,warnings:[]}}function pn(e,t){let n=0;for(let r=e.length-1;r>=0;r--)n=n*t+e[r];return n}var Ru=[0,.221157,-.147981,-2.07119,4.434685,-2.706056],Su=[0,.042981,-.293762,-1.752461,5.682633,-3.582633],ku=[.544,-.39978,.025054,-6714e-7],Au=[1.3822,-.77857,.062767,-.0020322],qu=[-1.5861,-.31082,-.083751,.0038915],Iu=[-.4803,-.082676,.0030302],_u=[-2.273,.459];function Lu(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]=-Z((m+1-.375)/(e+.25)),o+=r[m]*r[m];o*=2;let s=Math.sqrt(o),i=1/Math.sqrt(e),c=r[0]/s+pn(Ru,i),a=new Float64Array(n);a[0]=c;let l,f;if(e>5){let m=r[1]/s+pn(Su,i);a[1]=m,l=2,f=Math.sqrt((o-2*r[0]*r[0]-2*r[1]*r[1])/(1-2*c*c-2*m*m))}else l=1,f=Math.sqrt((o-2*r[0]*r[0])/(1-2*c*c));for(let m=l;m<n;m++)a[m]=r[m]/f;for(let m=0;m<n;m++)t[e-1-m]=a[m],t[m]=-a[m];return t}function Mc(e){let t=ee([e]).groups[0],n=t.length,r=Lu(n),o=t[n-1]-t[0],s=0;for(let f of t)s+=f/o;s/=n;let i=0,c=0,a=0;for(let f=0;f<n;f++){let m=t[f]/o-s;i+=r[f]*r[f],c+=m*m,a+=r[f]*m}let l=Math.sqrt(i*c);return(l-a)*(l+a)/(i*c)}var Ou=1.90985931710274,Fu=1.0471975511966;function yc(e,t){if(Number.isNaN(e)||!(t>=3))return NaN;if(t===3){let i=Ou*(Math.asin(Math.sqrt(1-e))-Fu);return Math.max(i,0)}let n=Math.log(e);if(t<=11){let i=pn(_u,t);if(n>=i)return 1e-99;let c=pn(ku,t),a=Math.exp(pn(Au,t));return j(-Math.log(i-n),{mean:c,sd:a,lowerTail:!1})}let r=Math.log(t),o=pn(qu,r),s=Math.exp(pn(Iu,r));return j(n,{mean:o,sd:s,lowerTail:!1})}var Rc=Math.log(2*Math.PI),Ec=2e-14,xc=500;function Cu(e){return Math.exp(-.5*e*e-.5*Rc)}function Du(e){return j(e,{logP:!0})}function Sc(e,t=-1/0,n=!1){let r=t===-1/0?Ho(e,0,2,0,Ec,xc):Ho(e,t,1,0,Ec,xc),o=r.ier===1||r.ier===3||r.ier===5;return n&&o&&!(r.abserr<=1e-6*Math.abs(r.result))?NaN:r.result}function Tc(e,t,n){let r=t.map(Math.sqrt),o=t.map(i=>Math.sqrt(1-i));return Sc(i=>{let c=0;for(let a=0;a<r.length;a++){let l=(r[a]*i+e)/o[a];if(!n){c+=Du(l);continue}let f=(r[a]*i-e)/o[a],m=j(f)+j(l,{lowerTail:!1});m<.5?c+=Math.log1p(-m):c+=Math.log(Math.max(0,l+f<0?j(l)-j(f):j(f,{lowerTail:!1})-j(l,{lowerTail:!1})))}return Cu(i)*-Math.expm1(c)})}function dn(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,Tc(e,t,r));let o=n/2,s=Math.log(2)+.5*Math.log(o)-.5*Rc-La(o);return Math.min(1,Sc(c=>{let a=s-o*(Math.expm1(2*c)-2*c);return a<-745?0:Math.exp(a)*Tc(e*Math.exp(c),t,r)},-1/0,!0))}var Pu=["pAdjustMethod"],Vu=["pAdjustMethod","alternative"],$u=["alternative"],zu=["pAdjustMethod","alternative"],Ku=["method","altp"],Wu=["alternative"],kc="Ties are present. z-quantiles were corrected for ties.";function Dt(e,t){let{labels:n,groups:r}=Jt(e,t),o=r.length;if(o<2)throw new RangeError("there must be at least two groups");let s=r.map(i=>i.length);return{labels:n,groups:r,n:s,N:s.reduce((i,c)=>i+c,0),k:o}}function qs(e){let t=e.groups.flat(),n=ye(t),r=[],o=0;for(let i of e.n){let c=0;for(let a=0;a<i;a++)c+=n[o+a];o+=i,r.push(c/i)}let s=0;for(let i of vt(t))s+=i*i*i-i;return{R:r,C:s/(12*(e.N-1))}}function Mr(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 ju(e,t){let n=e.length+1,r=[];for(let c=0;c<n-1;c++)for(let a=c+1;a<n;a++)r.push(e[a-1][c]);let o=On(r,t),s=0,i=e.map(c=>c.slice());for(let c=0;c<n-1;c++)for(let a=c+1;a<n;a++)i[a-1][c]=o[s++];return i}function Mo(e,t,n,r,o,s=[]){return{method:t,statistic:n,pValue:r,rowNames:e.labels.slice(1),colNames:e.labels.slice(0,-1),pAdjustMethod:o,warnings:s}}function Is(e,t,n,r,o,s=[]){return{method:t,statistic:n.map(i=>[i]),pValue:r.map(i=>[i]),rowNames:e.labels.slice(1),colNames:e.labels.slice(0,1),pAdjustMethod:o,warnings:s}}var No=e=>j(e,{lowerTail:!1}),_s=(e,t,n)=>Gr(Math.abs(e),t,n,{lowerTail:!1});function Ls(e){let t=Le(e.groups),n=0;for(let r of t.n)r>1&&(n+=r-1);return yr(n,t.ssWithin),{m:t.mean,s2:t.ssWithin/n,df:e.N-e.k,k:t.k}}function yr(e,t){if(e===0)throw new RangeError("no residual degrees of freedom");if(t===0)throw new RangeError("the pooled within-group variance is 0")}function Uu(e,t,n={}){C(n,Pu,"kwAllPairsDunnTest");let r=n.pAdjustMethod??"holm",o=Dt(e,t),{R:s,C:i}=qs(o),c=o.N*(o.N+1)/12-i,a=Mr(o.k,(f,m)=>Math.abs(s[f]-s[m])/Math.sqrt(c*(1/o.n[f]+1/o.n[m]))),l=ju(a.map(f=>f.map(m=>2*No(m))),r);return z("kwAllPairsDunnTest",Mo(o,"Dunn's all-pairs test",a,l,r,i!==0?[kc]:[]))}function Gu(e,t,n={}){C(n,Vu,"kwManyOneDunnTest");let r=le(n.alternative),o=n.pAdjustMethod??"single-step",s=Dt(e,t),{R:i,C:c}=qs(s),a=s.N*(s.N+1)/12-c,l=s.n.slice(1).map((h,p)=>p+1),f=l.map(h=>(i[h]-i[0])/Math.sqrt(a*(1/s.n[0]+1/s.n[h]))),m=l.map(h=>s.n[h]/(s.n[h]+s.n[0])),u=Ac(f,m,r,o);return z("kwManyOneDunnTest",Is(s,"Dunn's many-to-one test",f,u,o,c!==0?[kc]:[]))}function Ac(e,t,n,r){return r==="single-step"?e.map(o=>n==="two.sided"?dn(o,t,1/0,!0):dn(n==="greater"?o:-o,t,1/0,!1)):On(e.map(o=>n==="two.sided"?2*No(Math.abs(o)):No(n==="greater"?o:-o)),r)}function Yu(e,t,n={}){C(n,$u,"dunnettTest");let r=le(n.alternative),o=Dt(e,t),{m:s,s2:i,df:c}=Ls(o),a=Math.sqrt(i),l=o.n.slice(1).map((h,p)=>p+1),f=l.map(h=>(s[h]-s[0])/(a*Math.sqrt(1/o.n[h]+1/o.n[0]))),m=l.map(h=>o.n[h]/(o.n[h]+o.n[0])),u=f.map(h=>r==="two.sided"?Math.min(1,2*dn(Math.abs(h),m,c,!1)):dn(r==="greater"?h:-h,m,c,!1));return z("dunnettTest",Is(o,`Dunnett's-test for multiple
3
+ comparisons with one control`,f,u,"single-step"))}function Hu(e,t,n={}){C(n,zu,"manyOneUTest");let r=le(n.alternative),o=n.pAdjustMethod??"single-step",s=Dt(e,t);if(o==="single-step"&&s.k<3)throw new RangeError(`for 'p.adjust.method = single-step' at least
4
4
 
5
- k = 3 groups are required.`);let c=s.n[0],i=s.n.slice(1).map((m,u)=>u+1),a=i.map(m=>{let u=s.n[m],p=[...s.groups[0],...s.groups[m]],d=Ne(p),g=0;for(let x=0;x<c;x++)g+=d[x];let w=c+u,y=w*(w+1)/2-g,v=c*u+c*(c+1)/2-g,N=c*u+u*(u+1)/2-y,b=0;for(let x of ut(p))b+=x*x*x-x;let h=c*u/(w*(w-1))*((w*w*w-w)/12-b/12),E=(Math.min(v,N)-c*u/2)/Math.sqrt(h);return g<y?-E:E}),l=i.map(m=>s.n[m]/(s.n[m]+c)),f=ui(a,l,r,o);return j("manyOneUTest",jo(s,`Wilcoxon, Mann, Whittney U-test
6
- for multiple comparisons with one control`,a,f,o))}function Nf(e,t){let n=kt(e,t),r=nr(n.k,(s,c)=>{let i=n.n[s],a=n.n[c],l=[...n.groups[s],...n.groups[c]],f=Ne(l),m=0;for(let y=0;y<i;y++)m+=f[y];let u=m-i*(i+1)/2,p=Math.min(u,i*a-u),d=i+a,g=0;for(let y of ut(l))g+=y*y*y-y;let w=i*a/(d*(d-1))*((d*d*d-d)/12-g/12);return Math.SQRT2*(p-i*a/2)/Math.sqrt(w)}),o=r.map(s=>s.map(c=>Number.isNaN(c)?NaN:Ko(c,n.k,1/0)));return j("dscfAllPairsTest",Hr(n,"Dwass-Steele-Critchlow-Fligner all-pairs test",r,o,"single-step"))}function vf(e,t){let n=kt(e,t),r=_e(n.groups),o=r.mean,s=r.variance.map((a,l)=>a/n.n[l]),c=nr(n.k,(a,l)=>(o[a]-o[l])/Math.sqrt(s[a]+s[l])*Math.SQRT2),i=nr(n.k,(a,l)=>{let f=ra([r.variance[a],r.variance[l]],[n.n[a],n.n[l]]);return Ko(c[a-1][l],n.k,f)});return j("gamesHowellTest",Hr(n,"Games-Howell test",c,i,"none"))}function yf(e,t){let n=kt(e,t),{m:r,s2:o,df:s}=Uo(n);if(s<2)throw new RangeError("the studentized range needs at least 2 residual degrees of freedom");let c=nr(n.k,(a,l)=>(r[a]-r[l])/Math.sqrt(o*(1/n.n[a]+1/n.n[l])/2)),i=c.map(a=>a.map(l=>Number.isNaN(l)?NaN:Ko(l,n.k,s)));return j("tukeyTest",Hr(n,"Tukey's test",c,i,"single-step"))}function Mf(e,t,n={}){C(n,uf,"dunnTest");let r=n.method??"none",o=n.altp??!1,s=kt(e,t),c=Yr(e,t).statistic,{R:i,C:a}=Wo(s),l=s.N*(s.N+1)/12-a,f=[],m=[];for(let d=1;d<s.k;d++)for(let g=0;g<d;g++)f.push((i[g]-i[d])/Math.sqrt(l*(1/s.n[g]+1/s.n[d]))),m.push(`${s.labels[g]} - ${s.labels[d]}`);let u=f.map(d=>(o?2:1)*Gr(Math.abs(d))),p=Ef(u,r);return{chi2:c,Z:f,P:u,PAdjusted:p,zValue:f,pValue:u,pAdjusted:p,comparisons:m,warnings:[]}}function Ef(e,t){let n=e.length,r=(s,c)=>Math.min(1,-Math.expm1(c*Math.log1p(-s))),o=s=>{let c=e.map((a,l)=>l).sort((a,l)=>(s?e[l]-e[a]:e[a]-e[l])||a-l),i=new Array(n);return c.forEach((a,l)=>i[a]=l>0&&e[a]===e[c[l-1]]?i[c[l-1]]:l+1),i};switch(t){case"none":return e.slice();case"bonferroni":return e.map(s=>Math.min(1,n*s));case"sidak":return e.map(s=>r(s,n));case"holm":{let s=o(!1);return e.map((c,i)=>Math.min(1,(n+1-s[i])*c))}case"hs":{let s=o(!1);return e.map((c,i)=>r(c,n+1-s[i]))}case"hochberg":{let s=o(!0);return e.map((c,i)=>Math.min(1,s[i]*c))}case"bh":{let s=o(!0);return e.map((c,i)=>Math.min(1,n/(n+1-s[i])*c))}case"by":{let s=o(!0),c=0;for(let i=1;i<=n;i++)c+=1/i;return e.map((i,a)=>Math.min(1,n/(n+1-s[a])*c*i))}default:throw new RangeError(`invalid method "${String(t)}"`)}}function xf(e,t,n={}){C(n,pf,"glhtDunnett");let r=fe(n.alternative),o=kt(e,t),{m:s,s2:c,df:i,k:a}=Uo(o),l=Math.sqrt(c),f=o.n.slice(1).map((v,N)=>N+1),m=f.map(v=>s[v]-s[0]),u=f.map(v=>l*Math.sqrt(1/o.n[v]+1/o.n[0])),p=m.map((v,N)=>v/u[N]),d=m.map(v=>_(v,a)),g=u.map(v=>_(v,a)),w=f.map(v=>o.n[v]/(o.n[v]+o.n[0])),y=p.map(v=>r==="two.sided"?nn(v,w,i,!0):nn(r==="greater"?v:-v,w,i,!1));return{names:f.map(v=>`${o.labels[v]} - ${o.labels[0]}`),estimate:d,se:g,t:p,p:y,stdError:g,tValue:p,pValue:y,df:i,warnings:[]}}var pi=["varEqual"],Rf=2**-1022;function di(e,t,n={}){if(typeof e!="string"&&Ce(t)&&C(t,pi,"onewayTest"),C(n,pi,"onewayTest"),typeof e=="string"){let w=da(e,t);return di(w.values,w.groups,n)}let r;t!==void 0&&!Array.isArray(t)?n=t:r=t;let o=en(e,r),s=_e(o),c=s.groups,i=n.varEqual??!1,a=c.length;if(a<2)throw new RangeError("not enough groups");let l=c.map(w=>w.length);if(l.some(w=>w<2))throw new RangeError("not enough observations");let f=s.mean,m=s.variance,u=l.reduce((w,y)=>w+y,0),p,d,g;if(i){let w=s.grandMean,y=0,v=0;for(let N=0;N<a;N++)y+=l[N]*(f[N]-w)**2,v+=(l[N]-1)*m[N];d=u-a,rr(d,s.ssWithin),p=y/(a-1)/(v/d)}else if(m.some(w=>!(w>=Rf))&&(g=Sf(o))!==void 0){let w=g.map((M,R)=>hi(l[R]/M.variance,-2*M.k)),y=Math.max(...w.map(M=>M.e)),v=w.map(M=>De(M.m,M.e-y)),N=v.reduce((M,R)=>M+R,0),b=0;for(let M=0;M<a;M++){let R=v.reduce((T,S,A)=>A===M?T:T+S,0);b+=(R/N)**2/(l[M]-1)}b/=a*a-1;let h=[];for(let M=0;M<a;M++)for(let R=M+1;R<a;R++){let[T,S]=g[M].k>=g[R].k?[g[M],g[R]]:[g[R],g[M]],A=T.mean-De(S.mean,S.k-T.k);if(A===0)continue;let k=hi(A,T.k);h.push({m:w[M].m*w[R].m*k.m*k.m/N,e:w[M].e+w[R].e+2*k.e-y})}let E=Math.max(...h.map(M=>M.e)),x=h.reduce((M,R)=>M+De(R.m,R.e-E),0);p=(h.length===0?0:De(x,E))/((a-1)*(1+2*(a-2)*b)),d=1/(3*b)}else if(m.some(w=>w===0)){let w=m.flatMap((b,h)=>b===0?[h]:[]);if(w.length>1)throw new RangeError("Welch's test is not defined with two or more constant groups");let y=w[0],v=0,N=0;for(let b=0;b<a;b++)b!==y&&(v+=1/(l[b]-1),N+=l[b]/m[b]*(f[b]-f[y])**2);v/=a*a-1,p=N/((a-1)*(1+2*(a-2)*v)),d=1/(3*v)}else{let w=l.map((h,E)=>h/m[E]),y=w.reduce((h,E)=>h+E,0),v=0,N=0;for(let h=0;h<a;h++)v+=(1-w[h]/y)**2/(l[h]-1),N+=w[h]*f[h];v/=a*a-1,N/=y;let b=0;for(let h=0;h<a;h++)b+=w[h]*(f[h]-N)**2;p=b/((a-1)*(1+2*(a-2)*v)),d=1/(3*v)}return{method:i?"One-way analysis of means":"One-way analysis of means (not assuming equal variances)",statistic:p,statisticName:"F",parameter:[a-1,d],parameterName:["num df","denom df"],pValue:we(p,a-1,d,!1,!1),warnings:[]}}function Tf(e){let t=Array.from(e).filter(o=>!Number.isNaN(o)).sort((o,s)=>o-s),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=oi(t);return{method:"Shapiro-Wilk normality test",statistic:1-r,statisticName:"W",pValue:si(r,n),warnings:[]}}function Sf(e){let t=[];for(let n of e){let r=_e([n]),o=r.variance[0];if(!(o>0))return;t.push({mean:r.mean[0],variance:o,k:r.k})}return t}function hi(e,t){let n=Math.floor(Math.log2(Math.abs(e))),r=De(e,-n);return Math.abs(r)>=2?(r/=2,n++):Math.abs(r)<1&&(r*=2,n--),{m:r,e:t+n}}function Yo(e,t,n){let r=[0,0];for(let o=Math.max(0,t);o<Math.min(n,e.length);o++)r=Q(r,[e[o],0]);return r[0]}function Af([[e,t],[n,r]]){let o=e+n,s=t+r,c=e+t,i=Math.max(0,c-s),a=Math.min(c,o),l=new Float64Array(a-i+1);for(let f=i;f<=a;f++)l[f-i]=hn(f,o,s,c,!1);return{lo:i,d:l}}function kf(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 s=0;s<t.length;s++)n[r+s]+=o*t[s]}return n}var Xr=class{lo;hi;logNull;constructor(t){let n=0,r=new Float64Array([1]);for(let o of t){let s=Af(o);n+=s.lo,r=kf(r,s.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),s=-1/0;for(let i=0;i<n;i++){let a=this.logNull[i]+(this.lo+i)*o;r[i]=a,a>s&&(s=a)}for(let i=0;i<n;i++)r[i]=Math.exp(r[i]-s);let c=Yo(r,0,n);for(let i=0;i<n;i++)r[i]=r[i]/c;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=Q(r,[(this.lo+o)*n[o],0]);return r[0]}cdf(t,n,r){let o=this.density(n),s=t-this.lo;return r?Yo(o,s,o.length):Yo(o,0,s+1)}};function Go(e,t){return Vr(e,t)}function gi(e,t){if(t===e.lo)return 0;if(t===e.hi)return 1/0;let n=e.mean(1);return n===t?1:Go(r=>e.mean(r)-t,n>t)}function Ho(e,t,n){if(t===e.hi)return 1/0;let r=e.cdf(t,1,!1);return r===n?1:Go(o=>n-e.cdf(t,o,!1),r<n)}function Xo(e,t,n){if(t===e.lo)return 0;let r=e.cdf(t,1,!0);return r===n?1:Go(o=>e.cdf(t,o,!0)-n,r>n)}var qf=["alternative","correct","exact","confLevel"];function _f(e,t={}){C(t,qf,"mantelhaenTest");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 s=e.map(Bo);if(s.some(f=>f<2))throw new RangeError("sample size in each stratum must be > 1");let c=fe(t.alternative),i=t.correct??!0,a=t.exact??!1,l=t.confLevel??.95;if(Be(l),r===2&&o===2){if(a&&e.some(m=>m.some(u=>u.some(p=>!Number.isInteger(p)))))throw new RangeError("the exact test needs integer counts");if(a&&s.reduce((m,u)=>m+u,0)>Number.MAX_SAFE_INTEGER)throw new RangeError("the exact test needs counts with a total below 2^53: use exact: false");let f=e.map(m=>[[m[0][0],m[0][1]],[m[1][0],m[1][1]]]);return a?Of(f,c,l):If(f,s,c,i,l)}return Ff(e,s,r,o)}function Bo(e){return e.reduce((t,n)=>t+n.reduce((r,o)=>r+o,0),0)}function Ni(e){let t=0,n=0;for(let o of e)for(let s of o)for(let c of s)t=Math.max(t,c),n++;let r=Math.floor(Math.log2(t))+1+Math.ceil(Math.log2(8*n))-1020;return r<=0?0:r+r%2}function vi(e,t){return e.map(n=>n.map(r=>r.map(o=>_(o,-t))))}function wi(e,t,n){let r=0,o=0,s=0,c=0,i=0,a=0,l=0,f=0;return e.forEach(([[m,u],[p,d]],g)=>{let w=t[g],y=m+u,v=p+d,N=m+p,b=u+d;r+=m;let h=y*N/w;o+=Number.isFinite(h)?h:y/w*N;let E=y*v*N*b/(w*w*(w-n));s+=Number.isFinite(E)?E:y/w*(v/w)*(N/(w-n))*b;let x=(m+d)/w,M=(u+p)/w,R=m*d/w,T=u*p/w,S=Number.isFinite(R)?R:m/w*d,A=Number.isFinite(T)?T:u/w*p;c+=S,i+=A,a+=x*S,l+=x*A+M*S,f+=M*A}),{sa:r,se:o,sv:s,sR:c,sS:i,sPR:a,sPSQR:l,sQS:f}}function If(e,t,n,r,o){let s=wi(e,t,1),c=0;if(!t.every(Number.isFinite)||!Object.values(s).every(Number.isFinite)){c=Ni(e);let x=vi(e,c);s=wi(x,x.map(Bo),_(1,-c))}let{sa:i,se:a,sv:l,sR:f,sS:m,sPR:u,sPSQR:p,sQS:d}=s,g=i-a,w=_(.5,-c),y=r&&Math.abs(g)>=w?w:0,v=_(Oe(Math.abs(g)-y,l),c),N=n==="two.sided"?xe(v,.5,2,!1,!1):Y(Math.sign(g)*Math.sqrt(v),{lowerTail:n==="less"}),b=f/m,h=_(Math.sqrt(u/(2*f*f)+p/(2*f*m)+d/(2*m*m)),-c/2),E;if(n==="less")E=[0,b*Math.exp(Z(o)*h)];else if(n==="greater")E=[b*Math.exp(Z(o,{lowerTail:!1})*h),1/0];else{let x=(1-o)/2,M=x<1e-8?Z(x,{lowerTail:!1}):Z((1+o)/2);E=[b*Math.exp(-M*h),b*Math.exp(M*h)]}return{method:`Mantel-Haenszel chi-squared test ${y!==0?"with":"without"} continuity correction`,alternative:n,statistic:v,statisticName:"Mantel-Haenszel X-squared",parameter:1,parameterName:"df",pValue:N,confInt:E,confLevel:o,estimate:b,estimateNames:"common odds ratio",nullValue:1,nullValueName:"common odds ratio",warnings:[]}}var Lf=2**24;function Of(e,t,n){let r=e.reduce((a,[[l,f],[m,u]])=>a+Math.min(l,u)+Math.min(f,m),1);if(r>Lf)throw new RangeError(`the exact distribution of S has ${r} values, more than 2^24: use exact: false`);let o=new Xr(e),s=e.reduce((a,l)=>a+l[0][0],0),c;if(t==="two.sided"){let a=o.density(1),l=a[s-o.lo]*(1+1e-7),f=[0,0];for(let m of a)m<=l&&(f=Q(f,[m,0]));c=Math.min(1,f[0])}else c=o.cdf(s,1,t==="greater");let i;if(t==="less")i=[0,Ho(o,s,1-n)];else if(t==="greater")i=[Xo(o,s,1-n),1/0];else{let a=(1-n)/2;i=[Xo(o,s,a),Ho(o,s,a)]}return{method:"Exact conditional test of independence in 2 x 2 x k tables",alternative:t,statistic:s,statisticName:"S",pValue:c,confInt:i,confLevel:n,estimate:gi(o,s),estimateNames:"common odds ratio",nullValue:1,nullValueName:"common odds ratio",warnings:[]}}function Ff(e,t,n,r){let{diff:o,V:s}=bi(e,t,n,r,1),c=0;if(!t.every(Number.isFinite)||!o.every(Number.isFinite)||!s.every(f=>f.every(Number.isFinite))){c=Ni(e);let f=vi(e,c);({diff:o,V:s}=bi(f,f.map(Bo),n,r,_(1,-c)))}let i=o.length,a=Ve(s,o),l=0;for(let f=0;f<i;f++)l+=o[f]*a[f];return l=_(l,c),{method:"Cochran-Mantel-Haenszel test",statistic:l,statisticName:"Cochran-Mantel-Haenszel M^2",parameter:i,parameterName:"df",pValue:xe(l,i/2,2,!1,!1),warnings:[]}}var Pf=2**-1022;function bi(e,t,n,r,o){let s=(n-1)*(r-1),c=new Array(s).fill(0),i=Array.from({length:s},()=>new Array(s).fill(0));return e.forEach((a,l)=>{let f=t[l],m=a.map(d=>d.reduce((g,w)=>g+w,0)),u=Array.from({length:r},(d,g)=>a.reduce((w,y)=>w+y[g],0)),p=1/(f*f*(f-o));for(let d=0;d<r-1;d++)for(let g=0;g<n-1;g++){let w=g+(n-1)*d,y=m[g]*u[d]/f;c[w]+=a[g][d]-(Number.isFinite(y)?y:Tr(m[g],u[d],f));for(let v=0;v<r-1;v++)for(let N=0;N<n-1;N++){let b=(g===N?f*m[g]:0)-m[g]*m[N],h=(d===v?f*u[d]:0)-u[d]*u[v],E=p*b*h;i[w][N+(n-1)*v]+=Number.isFinite(E)&&p>=Pf?E:m[g]*((g===N?1:0)-m[N]/f)*(((d===v?1:0)-u[v]/f)*(u[d]/(f-o)))}}}),{diff:c,V:i}}var yi=new Map,Df=2**24,Cf=2**32;function Ei(e,t){let n=e+t,r=(e+1)*(Math.floor((n+1)**2/4)+1);if(2*r>Df||n*r>Cf)throw new RangeError(`the exact distribution for m = ${e} and n = ${t} is too large to compute: use exact: false`)}function Vf(e,t){let n=`${e},${t}`,r=yi.get(n);if(r!==void 0)return r;let o=e+t,s=[];for(let u=1;u<=o;u++)s.push(Math.min(u,o+1-u));let c=s.reduce((u,p)=>u+p,0),i=Array.from({length:e+1},()=>new Float64Array(c+1));i[0][0]=1;for(let u=0;u<o;u++){let p=o-u,d=s[u],g=Array.from({length:e+1},()=>new Float64Array(c+1));for(let w=0;w<=e;w++){let y=i[w],v=e-w;if(v>p)continue;let N=v/p;for(let b=0;b<=c;b++){let h=y[b];h!==0&&(v>0&&(g[w+1][b+d]+=h*N),v<p&&(g[w][b]+=h*(1-N)))}}i=g}let a=i[e],l=0;for(;l<a.length&&a[l]===0;)l++;let f=a.length-1;for(;f>l&&a[f]===0;)f--;let m={min:l,probs:a.slice(l,f+1)};return yi.set(n,m),m}function Qo(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:s}=Vf(t,n),c=Gs(e)-o,i=0;if(r)for(let a=0;a<=Math.min(c,s.length-1);a++)i+=s[a];else for(let a=Math.max(c+1,0);a<s.length;a++)i+=s[a];return Math.min(i,1)}var Mi=new Map;function zf(e,t){let n=`${e},${t}`,r=Mi.get(n);if(r!==void 0)return r;let o=e+t,s=0;for(let m=1;m<=o;m++)s+=Math.min(m,o+1-m);let c=Array.from({length:e+1},()=>new Float64Array(s+1));c[0][0]=1;for(let m=0;m<o;m++){let u=Math.min(m+1,o-m),p=Array.from({length:e+1},()=>new Float64Array(s+1));for(let d=0;d<=e;d++){if(e-d>o-m)continue;let g=c[d];for(let w=0;w<=s;w++){let y=g[w];y!==0&&(d<e&&(p[d+1][w+u]+=y),e-d<o-m&&(p[d][w]+=y))}}c=p}let i=c[e],a=0;for(;i[a]===0;)a++;let l=i.length-1;for(;i[l]===0;)l--;let f={min:a,counts:i.slice(a,l+1)};return Mi.set(n,f),f}function Br(e,t,n){let{min:r,counts:o}=zf(t,n);if(e<=0)return r;if(e>=1)return r+o.length-1;let s=oa(t+n,t),c=0;for(let i=0;i<o.length;i++)if(c+=o[i]/s,c>=e)return r+i;return r+o.length-1}var $f=["alternative","exact","confInt","confLevel"];function Si(e,t,n={}){if(C(n,$f,"ansariTest"),typeof e=="string"){let h=Dt(e,t);return Si(h.x,h.y,n)}let r=Array.from(e).filter(h=>!Number.isNaN(h)),o=Array.from(t).filter(h=>!Number.isNaN(h)),s=r.length,c=o.length;if(s<1)throw new RangeError("not enough 'x' observations");if(c<1)throw new RangeError("not enough 'y' observations");let i=fe(n.alternative),a=n.confInt??!1,l=n.confLevel??.95;a&&Be(l);let f=s+c,m=Ne(r.concat(o)),u=m.map(h=>Math.min(h,f-h+1)),p=0;for(let h=0;h<s;h++)p+=u[h];let d=new Set(m).size<f,g=n.exact??(s<50&&c<50&&!d),w=n.exact??(s<50&&c<50),y=new Te,v,N;if(g&&!d){Ei(s,c);let h=x=>Qo(x-1,s,c,!1),E=x=>Qo(x,s,c,!0);if(i==="two.sided"){let x=Math.floor((s+1)**2/4)+Math.floor(s*c/2)/2;v=Math.min(1,2*(p>x?h(p):E(p)))}else i==="less"?v=h(p):v=E(p);a&&(N=Uf(r,o,i,l,y))}else{let h=0;for(let T of u)h+=T*T;let E=f%2===0,x=E?s*(f+2)/4:s*(f+1)**2/(4*f),M=E?s*c*(16*h-f*(f+2)**2)/(16*f*(f-1)):s*c*(16*f*h-(f+1)**4)/(16*f*f*(f-1)),R=(p-x)/Math.sqrt(M);v=i==="two.sided"?2*Math.min(Y(R),Y(R,{lowerTail:!1})):i==="less"?Y(R,{lowerTail:!1}):Y(R),a&&(N=Yf(r,o,i,l,w?new Te:y)),w&&d&&(y.add("cannot compute exact p-value with ties"),a&&y.add("cannot compute exact confidence intervals with ties"))}let b={method:"Ansari-Bradley test",alternative:i,statistic:p,statisticName:"AB",pValue:v,warnings:y.list};return N!==void 0&&(b.confInt=N.confInt,b.confLevel=l,b.estimate=N.estimate,b.estimateNames="ratio of scales"),b.nullValue=1,b.nullValueName="ratio of scales",j("ansariTest",b)}function Ai(e,t,n){let r=e.length,o=r+t.length,s=e.map(f=>f/n),i=n>0&&n<1/0&&s.some((f,m)=>Number.isFinite(e[m])&&(!Number.isFinite(f)||e[m]!==0&&Math.abs(f)<Wf))?jf(e.map(f=>xi(f,n)).concat(t.map(f=>xi(f,1)))):Kf(s.concat(t)),a=i.map(f=>Math.min(f,o-f+1)),l=0;for(let f=0;f<r;f++)l+=a[f];return{ab:l,a,ties:new Set(i).size<o}}var Wf=2**-1022;function xi(e,t){if(e===0)return[0,0,0];let n=e>0?1:-1;if(!Number.isFinite(e))return[n,1/0,1];let[r,o]=Ri(Math.abs(e)),[s,c]=Ri(t),i=o/c,a=r-s;return i<1&&(i*=2,a-=1),[n,a,i]}function Ri(e){let t=Math.floor(Math.log2(e)),n=_(e,-t);return n>=2?(n/=2,t+=1):n<1&&(n*=2,t-=1),[t,n]}function Ti(e,t){if(e[0]!==t[0])return e[0]-t[0];let n=e[1]!==t[1]?e[1]-t[1]:e[2]-t[2];return e[0]*n}function jf(e){let t=e.map((r,o)=>o).sort((r,o)=>Ti(e[r],e[o])),n=new Array(e.length);for(let r=0;r<t.length;){let o=r+1;for(;o<t.length&&Ti(e[t[r]],e[t[o]])===0;)o++;for(let s=r;s<o;s++)n[t[s]]=(r+1+o)/2;r=o}return n}function Kf(e){let t=e.filter(s=>!Number.isNaN(s));if(t.length===e.length)return Ne(e);let n=Ne(t),r=0,o=t.length;return e.map(s=>Number.isNaN(s)?++o:n[r++])}function Qr(e,t,n){return e==="two.sided"?n(t):e==="greater"?[n(2*t)[0],1/0]:[0,n(2*t)[1]]}function Uf(e,t,n,r,o){let s=e.length,c=t.length,i=[];for(let y of e)for(let v of t)y/v>=0&&i.push(y/v);i.sort((y,v)=>y-v);let a=i.length;if(a===0)return o.add("cannot compute confidence set, returning NA"),{confInt:Qr(n,1-r,()=>[NaN,NaN]),estimate:NaN};let l=i.map((y,v)=>Ai(e,t,y+(v<a-1?(i[v+1]-y)/2:y*1.01)).ab),f=(y,v,N)=>{let b=1/0;for(let E of l)(v?E-y>0:E-y>=0)&&E-y<b&&(b=E-y);let h=N?-1/0:1/0;return l.forEach((E,x)=>{E-y===b&&(h=N?Math.max(h,i[x]):Math.min(h,i[x]))}),h},m=y=>{let v=Br(y/2,s,c),N=Br(1-y/2,s,c);if(!l.some(E=>E-v>=0)||!l.some(E=>E-N>0))return o.add("samples differ in location: cannot compute confidence set, returning NA"),[NaN,NaN];let b=f(v,!1,!1),h=f(N,!0,!0);return b>h?[f(N,!1,!1),f(v,!0,!0)]:[b,h]},u=Qr(n,1-r,m),p=Br(.5,s,c),d=-1/0,g=1/0;l.forEach((y,v)=>{y-p<=0?d=Math.max(d,i[v]):g=Math.min(g,i[v])});let w=d===-1/0||g===1/0?NaN:(g+d)/2;return w===1/0&&(w=g/2+d/2),{confInt:u,estimate:w}}function Yf(e,t,n,r,o){let s=e.length,c=t.length,i=s+c,a=i%2===0,l=(N,b)=>{let{ab:h,a:E,ties:x}=Ai(e,t,N),M=a?h-s*(i+2)/4:h-s*(i+1)**2/(4*i),R;if(!x)R=Math.sqrt(a?s*c*(i+2)*(i-2)/(48*(i-1)):s*c*(i+1)*(3+i*i)/(48*i*i));else{let T=0;for(let S of E)T+=S*S;R=Math.sqrt(a?s*c*(16*T-i*(i+2)**2)/(16*i*(i-1)):s*c*(16*i*T-(i+1)**4)/(16*i*i*(i-1)))}return M/R-b},f=[],m=(N,b)=>{N.length===0||b.length===0||f.push(Je(N)/ge(b),ge(N)/Je(b))};if(m(e.filter(N=>N>0),t.filter(N=>N>0)),m(e.filter(N=>N<=0),t.filter(N=>N<0)),f.some(N=>!Number.isFinite(N))){o.add("cannot compute asymptotic confidence set or estimator");return}if(f.length===0)return o.add("samples differ in location: cannot compute confidence set, returning NA"),{confInt:Qr(n,1-r,()=>[NaN,NaN]),estimate:NaN};let u=Je(f),p=ge(f),d=N=>wt(b=>l(b,N),u,p,{tol:1e-4}),g=N=>{let b=Z(N/2),h=Z(N/2,{lowerTail:!1});if(l(u,b)>0||l(p,h)<0)return o.add("samples differ in location: cannot compute confidence set, returning NA"),[NaN,NaN];let E=d(b),x=d(h);return E<=x?[E,x]:[x,E]},w=Qr(n,1-r,g),y=l(u,0)>0||l(p,0)<0;y&&o.add("cannot compute estimate, returning NA");let v=y?NaN:d(0);return{confInt:w,estimate:v}}var Gf=["alternative","confLevel"];function Hf(e,t,n={}){C(n,Gf,"brunnerMunzelTest");let r=Array.from(e).filter(M=>!Number.isNaN(M)),o=Array.from(t).filter(M=>!Number.isNaN(M)),s=r.length,c=o.length;if(s<2)throw new RangeError("not enough 'x' observations");if(c<2)throw new RangeError("not enough 'y' observations");let i=fe(n.alternative),a=n.confLevel??.95;Ct(a);let l=Ne(r.concat(o)),f=l.slice(0,s),m=l.slice(s),u=M=>M.reduce((R,T)=>R+T,0)/M.length,p=u(f),d=u(m),g=(M,R,T)=>{let S=(M.length+1)/2,A=0;for(let k=0;k<M.length;k++)A+=(M[k]-R[k]-T+S)**2;return A/(M.length-1)},w=g(f,Ne(r),p),y=g(m,Ne(o),d),v=(d-(c+1)/2)/s,N=s*w+c*y,b=s*c*(d-p)/(s+c)/Math.sqrt(N),h=N**2/((s*w)**2/(s-1)+(c*y)**2/(c-1)),E;i==="greater"?E=Ke(b,h,!0,!1):i==="less"?E=Ke(b,h,!1,!1):E=2*Ke(-Math.abs(b),h,!0,!1);let x=gt((1-a)/2,h,!1,!1)*Math.sqrt(w/(s*c**2)+y/(c*s**2));return{method:"Brunner-Munzel Test",alternative:i,statistic:b,statisticName:"Brunner-Munzel Test Statistic",parameter:h,parameterName:"df",pValue:E,confInt:[v-x,v+x],confLevel:a,estimate:v,estimateNames:"P(X<Y)+.5*P(X=Y)",warnings:[]}}var Xf=[1.2337005501361697,7831619385924639e-32];function Bf(e,t,n){let r=(f,m)=>{let u=new Float64Array(t*t);for(let p=0;p<t;p++)for(let d=0;d<t;d++){let g=f[p*t+d];if(g!==0)for(let w=0;w<t;w++)u[p*t+w]+=g*m[d*t+w]}return u},o=f=>{let m=0;for(let d of f)m=Math.max(m,Math.abs(d));if(m===0||!Number.isFinite(m))return 0;let u=Math.floor(Math.log2(m));if(Math.abs(u)<500)return 0;let p=2**-u;for(let d=0;d<f.length;d++)f[d]*=p;return u},s,c=0,i=e.slice(),a=0,l=n;for(;l>0;)l&1&&(s=s===void 0?i.slice():r(s,i),c+=a,c+=o(s)),l>>=1,l>0&&(i=r(i,i),a*=2,a+=o(i));return[s,c]}var Qf=2**33;function qi(e,t){if(Number.isNaN(t)||!(e>=1)||e!==Math.floor(e)||t<=1/(2*e)||t>=1||e*t<1)return;let n=2*Math.floor(e*t)+1,r=e.toString(2),o=Math.max(1,r.length-1+(r.split("1").length-2));if(n**3*o>Qf)throw new RangeError(`the exact distribution of D for n = ${e} needs powers of a ${n} x ${n} matrix, too much work: use exact: false`)}function _i(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]=Yt(e,t),u=2*f-1+2*m,p=1,d=0;for(let g=1;g<=e;g++)p*=g/e*u,p<2**-500&&(p*=2**500,d-=500);for(;d<-1e3;)p*=2**-1e3,d+=1e3;return p*2**d}let n=Math.floor(e*t)+1,r=2*n-1,o=n-e*t,s=new Float64Array(r*r);for(let f=0;f<r;f++)for(let m=0;m<r;m++)s[f*r+m]=f-m+1>=0?1:0;for(let f=0;f<r;f++)s[f*r]-=Math.pow(o,f+1),s[(r-1)*r+f]-=Math.pow(o,r-f);2*o-1>0&&(s[(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 p=1;for(let d=2;d<=u;d++)p*=d;s[f*r+m]/=p}}let[c,i]=Bf(s,r,e),a=c[(n-1)*r+n-1],l=i;for(let f=1;f<=e;f++)a=a*f/e,Math.abs(a)<2**-500&&(a*=2**500,l-=500);for(;l>1e3;)a*=2**1e3,l-=1e3;for(;l<-1e3;)a*=2**-1e3,l+=1e3;return a*2**l}function Zo(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=Ae(Xf,Yt(e,e)),s=o[0]+o[1],c=Math.exp(-o[0])*(1-o[1]),i=0;for(let l=2;l<100;l++){let f=Math.exp(-(2*l-1)*(2*l-1)*s);if(i+=f,f<=1e-17*(i+c))break}if(c+i===0)return t?0:1;let a=Math.sqrt(2*Math.PI)/e*(c+i);return t?a:1-a}let n=0;for(let o=1;o<100;o++){let s=Math.exp(-2*o*o*e*e);if(n+=o%2===1?s:-s,s<=1e-17*Math.abs(n))break}let r=2*n;return t?1-r:r}function Jo(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]=Yt(e,t),o=Math.floor(e-n-r),s=0;for(let c=0;c<=o;c++){let i=Q(Rt(e-c,-n),[-r,0]);if(i[0]<=0)break;let a=Q(Rt(c,n),[r,0]),l=Ae(a,[e,0]),f=Ae(i,[e,0]);s+=Ys(c,e-c,l,f,!1)/(l[0]+l[1])}return Math.min(t*s,1)}var ki=2**28;function Ii(e,t){let[n,r]=Yt(e,t),o=Math.floor(n);o===n&&r<0&&(o-=1);let s=[];for(let p=0;p<=e;p++){let d=(p-n-r)/e;d>0&&d<1&&s.push({t:d,up:!0,i:p});let g=(p+n+r)/e;g>0&&g<1&&s.push({t:g,up:!1,i:p})}if(s.length*(2*o+1)*Math.min(e+1,100)>ki)return NaN;s.sort((p,d)=>p.t-d.t);let c=o+1,i=new Float64Array(e+1),a=new Float64Array(e+1);i[0]=1;let l=0,f=0,m=0,u=0;for(let p of s){let d=(p.t-f)/(1-f);if(d>0){let g=d/(1-d);a.fill(0);for(let w=l;w<c;w++){let y=i[w];if(y===0)continue;let v=e-w,N=Math.min(v,Math.floor((v+1)*d)),b=c-w,h=Ht(N,v,d,!1);for(let E=N;E<=v&&h!==0;E++)E>N&&(h=h*(v-E+1)*g/E),E>=b?m+=y*h:a[w+E]+=y*h,u++;h=N>0?Ht(N-1,v,d,!1):0;for(let E=N-1;E>=0&&h!==0;E--)E<N-1&&(h=h*(E+1)/((v-E)*g)),E>=b?m+=y*h:a[w+E]+=y*h,u++}if(u>2*ki)return NaN;[i,a]=[a,i]}if(f=p.t,p.up)c=p.i+1;else{for(let g=l;g<=p.i;g++)m+=i[g],i[g]=0;l=Math.max(l,p.i+1)}}return Math.min(m,1)}function Li(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 s=t+n,c;if(r.z!==void 0){if(r.z.length!==s)throw new RangeError("z must have length m + n");let u=[...r.z].sort((p,d)=>p-d);c=new Array(s+1).fill(!1);for(let p=1;p<s;p++)c[p]=u[p]!==u[p-1]}let i=e*t*n*(1-1e-10),a=(u,p)=>{let d=u*n-p*t;return(o==="two.sided"?Math.abs(d):o==="greater"?d:-d)>=i},l=new Float64Array(n+1),f=new Float64Array(n+1),m=0;for(let u=0;u<=t;u++){for(let p=0;p<=n;p++){if(u===0&&p===0){f[0]=1;continue}let d=u+p,g=(u>0?u/d*l[p]:0)+(p>0?p/d*f[p-1]:0);(c===void 0?d<s:c[d])&&a(u,p)?(g>0&&(m+=g*hn(u,t,n,d,!1)),f[p]=0):f[p]=g}[l,f]=[f,l],f.fill(0)}return Math.min(m,1)}var Zf=["alternative","exact"];function Oi(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 Fi(e,t,n={}){if(C(n,Zf,"ksTest"),typeof e=="string"){let v=Dt(e,t);return Fi(v.x,v.y,n)}let r=Array.from(e).filter(v=>!Number.isNaN(v)).sort((v,N)=>v-N),o=r.length;if(o<1)throw new RangeError("not enough 'x' data");let s=fe(n.alternative),c=s==="two.sided"?"D":s==="greater"?"D^+":"D^-";if(typeof t=="function"){let v=-1/0,N=-1/0;for(let M=0;M<o;M++){let R=t(r[M]);if(!(R>=0&&R<=1))throw new RangeError(`the distribution function returned ${R} at ${r[M]}, not a value in [0, 1]`);v=Math.max(v,(M+1)/o-R),N=Math.max(N,R-M/o)}let b=s==="two.sided"?Math.max(v,N):s==="greater"?v:N,h=new Set(r).size<o,E=n.exact??(o<100&&!h),x;if(E&&s==="two.sided"&&b<.5&&qi(o,b),s==="two.sided")if(!E)x=Zo(Math.sqrt(o)*b,!1);else{let M=2*Jo(o,b);if(b>=.5)x=Math.min(1,M);else{let R=M<.01?Ii(o,b):NaN;x=Number.isNaN(R)?1-_i(o,b):R}}else x=E?Jo(o,b):Math.exp(-2*o*b*b);return j("ksTest",{method:`${E?"Exact":"Asymptotic"} one-sample Kolmogorov-Smirnov test`,alternative:Oi(s,!0),exact:E,statistic:b,statisticName:c,pValue:Math.min(1,Math.max(0,x)),warnings:h?["ties should not be present for the one-sample Kolmogorov-Smirnov test"]:[]})}let i=Array.from(t).filter(v=>!Number.isNaN(v)),a=i.length;if(a<1)throw new RangeError("not enough 'y' data");let l=r.map(v=>[v,0]).concat(i.map(v=>[v,1]));l.sort((v,N)=>v[0]-N[0]);let f=0,m=0,u=0,p=0;for(let v=0;v<l.length;v++){if(l[v][1]===0?f++:m++,v+1<l.length&&l[v+1][0]===l[v][0])continue;let N=f/o-m/a;u=Math.max(u,N),p=Math.max(p,-N)}let d=s==="two.sided"?Math.max(u,p):s==="greater"?u:p,g=new Set(l.map(v=>v[0])).size<l.length,w=n.exact??o*a<1e4,y;if(w)y=Li(d,o,a,g?{alternative:s,z:l.map(v=>v[0])}:{alternative:s});else{let v=o*a/(o+a);y=s==="two.sided"?Zo(Math.sqrt(v)*d,!1):Math.exp(-2*v*d*d)}return j("ksTest",{method:`${w?"Exact":"Asymptotic"} two-sample Kolmogorov-Smirnov test`,alternative:Oi(s,!1),exact:w,statistic:d,statisticName:c,pValue:Math.min(1,Math.max(0,y)),warnings:g&&!w?["p-value will be approximate in the presence of ties"]:[]})}var Jf=["poolSd","paired","pAdjustMethod","alternative"],em=["mu","paired","exact","correct","confLevel","tolRoot","pAdjustMethod","alternative"],tm=["pAdjustMethod","correct"];function Pi(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,s=Tn(t);for(let i=0;i<r.length;i++){let a=s[i];if(typeof a=="number"&&Number.isNaN(a)||n&&Number.isNaN(r[i]))continue;let l=o.get(a);l===void 0&&o.set(a,l=[]),l.push(r[i])}let c=Array.from(o.keys()).sort(tr);return{levels:c,groups:c.map(i=>o.get(i))}}function es(e,t,n){let r=[],o=[];for(let i=0;i<e-1;i++)for(let a=i+1;a<e;a++)r.push(t(a,i)),o.push([a-1,i]);let s=Rn(r,n),c=Array.from({length:e-1},()=>new Array(e-1).fill(NaN));return o.forEach(([i,a],l)=>c[i][a]=s[l]),c}function ts(e,t,n,r,o=[]){return{method:e,pValue:n,rowNames:t.slice(1).map(Pe),colNames:t.slice(0,-1).map(Pe),pAdjustMethod:r,warnings:o}}function nm(e,t,n={}){C(n,Jf,"pairwiseTTest");let r=n.paired??!1,o=n.poolSd??!r;if(r&&o)throw new RangeError("pooling of SD is incompatible with paired tests");let s=fe(n.alternative),c=n.pAdjustMethod??"holm",{levels:i,groups:a}=Pi(e,t,!r),l=i.length;if(l<2)throw new RangeError("there must be at least two groups");let f,m;if(o){m="t tests with pooled SD";let u=a.map(y=>y.length),p=_e(a),d=p.mean,g=0;for(let y of u)y>1&&(g+=y-1);rr(g,p.ssWithin);let w=Math.sqrt(p.ssWithin/g);f=(y,v)=>{let N=(d[y]-d[v])/(w*Math.sqrt(1/u[y]+1/u[v]));return s==="less"?Ke(N,g,!0,!1):s==="greater"?Ke(N,g,!1,!1):2*Ke(-Math.abs(N),g,!0,!1)}}else m=r?"paired t tests":"t tests with non-pooled SD",f=(u,p)=>Re(()=>xn(a[u],a[p],{paired:r,alternative:s})).pValue;return ts(m,i,es(l,f,c),c)}function rm(e,t,n={}){C(n,em,"pairwiseWilcoxTest");let{pAdjustMethod:r,alternative:o,...s}=n,c=r??"holm",{levels:i,groups:a}=Pi(e,t,!(s.paired??!1)),l=i.length;if(l<2)throw new RangeError("there must be at least two groups");let f="",m=new Te,u=es(l,(p,d)=>{let g=Re(()=>Jn(a[p],a[d],{...s,...o===void 0?{}:{alternative:o}}));f=g.method;for(let w of g.warnings)m.add(w);return g.pValue},c);return j("pairwiseWilcoxTest",ts(f,i,u,c,m.list))}function om(e,t,n={}){if(C(n,tm,"pairwisePropTest"),e.length!==t.length)throw new RangeError("'x' and 'n' must have the same length");let r=[],o=[];e.forEach((m,u)=>{Number.isNaN(m)||Number.isNaN(t[u])||(r.push(m),o.push(t[u]))});let s=r.length;if(s<2)throw new RangeError("too few groups");let c=n.pAdjustMethod??"holm",i=n.correct??!0,a=new Te,l=es(s,(m,u)=>{let p=Re(()=>Do([r[m],r[u]],[o[m],o[u]],{correct:i}));for(let d of p.warnings)a.add(d);return p.pValue},c),f=Array.from({length:s},(m,u)=>u+1);return j("pairwisePropTest",ts("Pairwise comparison of proportions",f,l,c,a.list))}var or=[0,0];function Zr(e){if(!(e[0]>0))return[Math.sqrt(e[0]),0];let t=Math.sqrt(e[0]),n=de(e,Yt(t,t));return Rt(t,n[0]/(2*t))}function Jr(e,t){return Array.from({length:t},(n,r)=>e===void 0?[1,0]:Zr([e[r],0]))}function Sn(e,t){return qt(e,t,[]).fit}function qt(e,t,n){let r=t.length,o=Math.min(e.length,r),s=e.map(f=>f.slice()),c=t.slice(),i=n.map(f=>f.slice()),a=[];for(let f=0;f<o;f++){let m=s[f],u=or;for(let y=f;y<r;y++)u=Q(u,pe(m[y],m[y]));if(u[0]===0){a.push(or);continue}let p=Zr(u),d=m[f][0]<0?[-p[0],-p[1]]:p;m[f]=Q(m[f],d);let g=pe(d,m[f]),w=y=>{let v=or;for(let b=f;b<r;b++)v=Q(v,pe(m[b],y[b]));let N=Ae(v,g);for(let b=f;b<r;b++)y[b]=de(y[b],pe(N,m[b]))};for(let y=f+1;y<e.length;y++)w(s[y]);w(c);for(let y of i)w(y);a.push([-d[0],-d[1]])}let l=new Array(o).fill(or);for(let f=o-1;f>=0;f--){let m=c[f];for(let u=f+1;u<o;u++)m=de(m,pe(s[u][f],l[u]));l[f]=Ae(m,a[f])}return{fit:{qty:c,coef:l},qtv:i}}function An(e,t=0,n=e.length){let r=or;for(let o=t;o<n;o++)r=Q(r,pe(e[o],e[o]));return r}var ze=new WeakMap;function eo(e){let t=(n,r)=>n.weights===void 0?Array.from(r):r.filter((o,s)=>n.weights[s]!==0);return ee(e.flatMap(n=>[t(n,n.fittedValues),t(n,n.residuals)])).k}function to(e,t){return e.map(n=>{if(n.rss>=sm&&n.rss<1/0)return _(n.rss,-2*t);let r=0;return n.residuals.forEach((o,s)=>{let c=n.weights?.[s]??1;if(c===0)return;let i=_(o,-t);r+=c*i*i}),r})}var sm=2**-1022;function os(e,t){return e.y.flatMap((n,r)=>(e.weights?.[r]??1)===0?[]:[[e.weights?.[r]??1,_(n,-t)]])}function no(e){let t=Math.floor(Math.log2(e.reduce((n,[r])=>Math.max(n,r),0)));return t-(t%2+2)%2}function rn(e,t,n=0){let r=c=>e.reduce((i,[a,l])=>i+_(a,-c)*l*l,0),o=n,s=r(o);return Number.isFinite(s)||(o=no(e),s=r(o)),!(_(t,-o)>=1e-24*s)}function am(e){let t=eo([e]),{pivot:n}=e.qr,r=[],o=new Map;for(let s=0;s<e.rank;s++){let c=e.assign[n[s]],i=o.get(c);i===void 0&&(i={term:c,df:0,ss:0},o.set(c,i),r.push(i)),i.df++,i.ss+=_(e.effects[s],-t)**2}return{terms:r,rdf:e.dfResidual,rss:to([e],t)[0],k:t}}var Di=(e,t,n)=>we(e,t,n,!1,!1);function im(e){let t=am(e),n=t.terms.filter(o=>o.term!==0),r=0;return e.fittedValues.forEach((o,s)=>{let c=e.weights?.[s]??1;c!==0&&(r+=c*_(o,-t.k)**2)}),{names:n.map(o=>e.termLabels[o.term-1]),rows:n,rdf:t.rdf,rss:t.rss,k:t.k,perfect:t.rss<1e-10*r}}function ns(e){let t=im(e),{names:n,rdf:r,k:o,perfect:s}=t,{rows:c,rss:i}=t;if(r>0){let g=os(e,o);rn(g,i)&&(i=0,c=c.map(w=>rn(g,w.ss)?{...w,ss:0}:w))}let a=i/r,l=g=>_(g,2*o),f=c.map(({df:g,ss:w})=>{let y=w/g/a;return[g,l(w),l(w/g),y,Di(y,g,r)]});f.push([r,l(i),r===0?NaN:l(a),NaN,NaN]);let m=["Df","Sum Sq","Mean Sq","F value","Pr(>F)"],u=[...n,"Residuals"],p={colNames:m,rowNames:u,table:f,warnings:s?["ANOVA F-tests on an essentially perfect fit are unreliable"]:[]},d=c.map(({df:g,ss:w})=>[g,w]);return d.push([r,i]),ze.set(p,{colNames:["Df","Sum Sq"],rowNames:u,table:d}),p}function cm(e){let t=e.map(f=>f.dfResidual),n=eo(e),r=to(e,n),o=f=>_(f,2*n),s=0;t.forEach((f,m)=>{f<t[s]&&(s=m)});let c=r[s]/t[s],i=0;e[s].fittedValues.forEach((f,m)=>{let u=e[s].weights?.[m]??1;u!==0&&(i+=u*_(f,-n)**2)});let a=r[s]<1e-10*i,l=e.map((f,m)=>{if(m===0)return[t[0],o(r[0]),NaN,NaN,NaN,NaN];let u=t[m-1]-t[m],p=r[m-1]-r[m],[d,g]=u>0?[e[m-1],e[m]]:[e[m],e[m-1]],w=v=>v.filter((N,b)=>(d.weights?.[b]??1)!==0),y=u===0||!Dr(w(d.x),w(g.x))?NaN:p/u/c;return y<0&&(y=NaN),[t[m],o(r[m]),u,o(p),y,Number.isNaN(y)?NaN:Di(y,Math.abs(u),t[s])]});return{colNames:["Res.Df","RSS","Df","Sum of Sq","F","Pr(>F)"],rowNames:e.map((f,m)=>String(m+1)),table:l,warnings:a?["ANOVA F-tests on an essentially perfect fit are unreliable"]:[]}}function rs(...e){let t=[];for(let n of e)for(let r of n??[])t.includes(r)||t.push(r);return t}function ss(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){let i=ns(e),a={...i,warnings:rs(e.warnings,i.warnings)},l=ze.get(i);return l!==void 0&&ze.set(a,l),j("anova",a,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=(i,a)=>i===a||i!==void 0&&a!==void 0&&i.length===a.length&&i.every((l,f)=>Object.is(l,a[f]));if(n.some(i=>!r(i.rows,e.rows)))throw new Error("models were not all fitted to the same rows of the data");let o=n.filter(i=>i.response===e.response);if(o.some(i=>!r(i.y,e.y)))throw new Error("models were not all fitted to the same response values");if(o.some(i=>!r(i.weights,e.weights)))throw new Error("models were not all fitted with the same weights");let s=[];if(o.length<n.length){let i=[...new Set(n.filter(a=>a.response!==e.response).map(a=>a.response))];s.push(`models with response ${i.map(a=>`\u2018${a}\u2019`).join(", ")} removed because response differs from model 1`)}let c=o.length===1?ns(e):cm(o);return c.warnings=rs(...n.map(i=>i.warnings),s,c.warnings),j("anova",c,...n)}function as(e){if(typeof e=="object"&&e!==null&&e.multistratum===!0){let r=e,o={strata:r.strata.filter(c=>c.stratum!=="Error: (Intercept)").map(c=>({...c})),warnings:[...r.warnings]},s=ze.get(r);return Array.isArray(s)&&ze.set(o,s.filter((c,i)=>r.strata[i].stratum!=="Error: (Intercept)")),j("aovSummary",o,e)}if(typeof e!="object"||e===null||!Array.isArray(e.effects))throw new TypeError("aovSummary() needs a fit from aov() with a formula");let t=ns(e),n=rs(e.warnings,t.warnings);if(e.dfResidual>0){let r={...t,warnings:n},o=ze.get(t);return o!==void 0&&ze.set(r,o),j("aovSummary",r,e)}return j("aovSummary",{colNames:t.colNames.slice(0,3),rowNames:t.rowNames.slice(0,-1),table:t.table.slice(0,-1).map(r=>r.slice(0,3)),warnings:n},e)}var lm=1e-14;function fm(e,t,n,r,o,s,c,i){let a=b=>_(b,2*c),l=t.length,f=n.flatMap((b,h)=>An(b)[0]>lm*s[h]?[h]:[]),m=0,u=[],p=t.slice();if(f.length>0){let b=ve(Array.from({length:l},(h,E)=>f.map(x=>n[x][E][0])));m=b.rank,u=b.pivot.slice(0,m).map(h=>f[h]),p=Sn(u.map(h=>n[h]),t).qty}let d=[];u.forEach((b,h)=>{let E=r[b],x=d.find(M=>M.term===E);x===void 0&&d.push(x={term:E,df:0,ss:[0,0]}),x.df++,x.ss=Q(x.ss,pe(p[h],p[h]))});let g=l-m,w=An(p,m)[0],y=d.filter(b=>b.term!==0);g>0&&rn(i,w)&&(w=0,y=y.map(b=>rn(i,b.ss[0])?{...b,ss:[0,0]}:b));let v=y.map(b=>o[b.term-1]),N=b=>{if(g>0){let h=w/g,E=y.map(({df:x,ss:M})=>{let R=M[0]/x/h;return[x,b(M[0]),b(M[0]/x),R,we(R,x,g,!1,!1)]});return E.push([g,b(w),b(h),NaN,NaN]),{stratum:`Error: ${e}`,colNames:["Df","Sum Sq","Mean Sq","F value","Pr(>F)"],rowNames:[...v,"Residuals"],table:E}}return{stratum:`Error: ${e}`,colNames:["Df","Sum Sq","Mean Sq"],rowNames:v,table:y.map(({df:h,ss:E})=>[h,b(E[0]),b(E[0]/h)])}};return[N(a),N(b=>b)]}function Ci(e,t,n,r,o,s){if(s.weights!==void 0)throw new Error("weights are not supported in a multistratum aov() fit");let c=n??{type:"num",value:1,text:"1"},i=pa({response:t.response,rhs:c},Object.keys(o)).variables,a=Zt(t,o,{...s,dropUnusedLevels:!0,contrastVariables:i},c),l=a.y;if(l===void 0)throw new TypeError("the formula has no response");if(l.some(I=>!Number.isFinite(I)))throw new RangeError("NA/NaN/Inf in 'y'");let f=Zt({response:t.response,rhs:r},o,{...s.levels===void 0?{}:{levels:s.levels},allContrasts:"contr.helmert",intercept:a.terms.intercept,extraVariables:a.terms.variables,dropUnusedLevels:!0}),m=l.length,u=[...a.warnings,...f.warnings.filter(I=>!a.warnings.includes(I))],p=f.colNames.length,d=f.colNames.map((I,z)=>ee([f.x.map(P=>P[z])]).groups[0]),g=f.x.map((I,z)=>d.map(P=>P[z])),w=p>0?ve(g):void 0,y=w?.rank??0;y<p&&u.push("Error() model is singular");let v=w?.pivot.slice(0,y)??[],N=I=>I.map(z=>[z,0]),b=a.colNames.map((I,z)=>N(ee([a.x.map(P=>P[z])]).groups[0])),{groups:h,k:E}=ee([l]),{fit:x,qtv:M}=qt(v.map(I=>N(d[I])),N(h[0]),b),R=h[0].map(I=>[1,I]),T=b.map(I=>An(I)[0]),S=[];v.forEach((I,z)=>{let P=f.assign[I],V=P===0?"(Intercept)":f.terms.termLabels[P-1],W=S.find(F=>F.name===V);W===void 0&&S.push(W={name:V,idx:[]}),W.idx.push(z)}),m>y&&S.push({name:"Within",idx:Array.from({length:m-y},(I,z)=>y+z)});let A=S.map(I=>fm(I.name,I.idx.map(z=>x.qty[z]),M.map(z=>I.idx.map(P=>z[P])),a.assign,a.terms.termLabels,T,E,R)),k=A.map(I=>I[0]),q={formula:e,multistratum:!0,strata:k,rows:a.frame.rows,warnings:u};return ze.set(q,A.map(I=>I[1])),q}function ro(e){let t=e.length,n=new Array(t).fill(0);for(let o=0;o<t;o++){let s=0;for(let c=0;c<=o;c++)s=Math.max(s,Math.abs(e[c][o]));s>0&&Number.isFinite(s)&&(s<2**-400||s>2**400)&&(n[o]=Math.floor(Math.log2(s)))}let r=n.every(o=>o===0)?e.map(o=>o.slice()):e.map(o=>o.map((s,c)=>_(s,-n[c])));return{inv:Ea(r),e:n}}var mm=["weights","contrasts","levels","intercept"],um=["level"],pm=["interval","level"],is=2**-1022;function cs(e,t,n){let r=t.y;if(r===void 0)throw new TypeError("the formula has no response");let o=Wi(t.x,r,t.frame.weights,t.terms.intercept,!0),s=o.result,c=[...t.warnings];if(!n){let a=o.yScale??0,l=o.fitRows.map(p=>_(s.fittedValues[p],-a)),f=l.reduce((p,d)=>p+d,0)/l.length,m=l.length>1?l.reduce((p,d)=>p+(d-f)**2,0)/(l.length-1):NaN,u=(o.rssScaled??o.rss)/s.dfResidual;Number.isFinite(u)&&u<1e-30*(f*f+m)&&c.push("essentially perfect fit: summary may be unreliable")}let i={...s,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:c};return t.frame.weights!==void 0&&(i.weights=t.frame.weights),i}function ls(e,t,n={}){return C(n,mm,"lm"),typeof e=="string"?j("lm",cs(e,Zt(Ir(e),t,{...n,dropUnusedLevels:!0}),!1)):hm(e,t,n)}function hm(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]),s=Array.from(t);if(o.some(m=>m.length!==o[0].length))throw new TypeError("all rows of the matrix must have the same length");if(o.length!==s.length)throw new RangeError("variable lengths differ (found for 'x')");let c=n.weights===void 0?void 0:Array.from(n.weights);if(c!==void 0&&c.length!==s.length)throw new RangeError("variable lengths differ (found for '(weights)')");let i=s.map((m,u)=>!Number.isNaN(m)&&o[u].every(p=>!Number.isNaN(p))&&(c===void 0||!Number.isNaN(c[u]))),a=c?.filter((m,u)=>i[u]);if(a?.some(m=>m<0))throw new RangeError("missing or negative weights not allowed");let l=o.filter((m,u)=>i[u]).map(m=>r?[1,...m]:m),f=s.filter((m,u)=>i[u]);return Wi(l,f,a,r).result}function dm(e,t,n,r,o,s,c){let i=s.rank,a=s.pivot.slice(0,i),l=n?.map(h=>_(h,-r)),f=Jr(l===void 0?void 0:o.map(h=>l[h]),o.length).map((h,E)=>{if(n===void 0||l===void 0)return h;let x=n[o[E]];if(x===0||l[o[E]]>=is)return h;let M=Zr([x,0]);return[_(M[0],-r/2),_(M[1],-r/2)]}),m=a.map(h=>{let E=o.map((M,R)=>st(f[R],e[M][h])),x=va(E.map(M=>M[0]));return{c:x===0?E:E.map(M=>[_(M[0],-x),_(M[1],-x)]),e:x}}),{qty:u,coef:p}=Sn(m.map(({c:h})=>h),o.map((h,E)=>st(f[E],t[h]))),d=p.map((h,E)=>m[E].e===0?h:[_(h[0],-m[E].e),_(h[1],-m[E].e)]),g=e[0]?.map(()=>NaN)??[];a.forEach((h,E)=>g[h]=d[E][0]);let w=e.map(h=>{let E=[0,0];return a.forEach((x,M)=>E=Q(E,st(d[M],h[x]))),E}),y=An(u,i)[0],v=o.map(h=>l?.[h]??1),N=[0,0];if(c){let h=[0,0],E=[0,0];o.forEach((x,M)=>{h=Q(h,[v[M],0]),E=Q(E,st(w[x],v[M]))}),N=Ae(E,h)}let b=[0,0];return o.forEach((h,E)=>{let x=de(w[h],N);b=Q(b,st(pe(x,x),v[E]))}),{coefficients:g,fittedValues:w.map(h=>h[0]),residuals:w.map((h,E)=>de([t[E],0],h)[0]),effects:u.map(h=>h[0]),rss:y,mss:b[0]}}function Wi(e,t,n,r,o=!1){let s=ee([t.filter((l,f)=>n===void 0||n[f]!==0)]).k;if(s===0)return Vi(zi(e,t,n,r,o),e,t,n);let c=zi(e,t.map(l=>_(l,-s)),n,r,o),i=c.result,a=l=>_(l,s);return i.coefficients=i.coefficients.map(a),i.residuals=i.residuals.map(a),i.fittedValues=i.fittedValues.map(a),i.sigma=a(i.sigma),i.coefTable.estimate=i.coefTable.estimate.map(a),i.coefTable.stdError=i.coefTable.stdError.map(a),Vi({...c,effects:c.effects.map(a),rss:_(c.rss,2*s),yScale:s,rssScaled:c.rss},e,t,n)}function Vi(e,t,n,r){if(r===void 0)return e;let o=e.result,s=o.coefficients.flatMap((c,i)=>Number.isNaN(c)?[]:[i]);return r.forEach((c,i)=>{if(c!==0)return;let a=!1;if(!Number.isFinite(o.fittedValues[i])){let l=t[i],f=s.reduce((m,u)=>m+l[u]*o.coefficients[u],0);o.fittedValues[i]=jn(f,0)?Kn(s.map(m=>l[m]),s.map(m=>o.coefficients[m]),f):f,a=!0}(a||!Number.isFinite(o.residuals[i]))&&(o.residuals[i]=n[i]-o.fittedValues[i])}),e}var ji=new WeakMap;function gm(e){return e.qr.every(t=>t.every(Number.isFinite))&&e.qraux.every(Number.isFinite)}function zi(e,t,n,r,o){if(t.length===0)throw new RangeError("0 (non-NA) cases");let s=t.map((B,J)=>n===void 0||n[J]!==0),c=e.filter((B,J)=>s[J]),i=t.filter((B,J)=>s[J]),a=n?.filter((B,J)=>s[J]),l=i.length;if(l===0)throw new RangeError("all weights are zero");if(c.some(B=>B.some(J=>!Number.isFinite(J))))throw new RangeError("NA/NaN/Inf in 'x'");if(i.some(B=>!Number.isFinite(B)))throw new RangeError("NA/NaN/Inf in 'y'");if(a?.some(B=>!Number.isFinite(B)))throw new RangeError("non-finite weights not allowed");let f=t.flatMap((B,J)=>s[J]?[J]:[]),m=a===void 0?1:ge(a),u=Number.isFinite(m)?2*Math.floor(Math.floor(Math.log2(m))/2):0,p=0,d=a?.map(Math.sqrt),g=d===void 0?c:c.map((B,J)=>B.map(D=>D*d[J])),w=ve(g);a!==void 0&&Number.isFinite(m)&&u+2>0&&!gm(w)&&(p=u+2,d=a.map(B=>_(Math.sqrt(B),-p/2)),g=c.map((B,J)=>B.map(D=>D*d[J])),w=ve(g),ji.set(w,p));let y=d===void 0?i:i.map((B,J)=>B*d[J]),v=w.rank,N=B=>_(B,-u),b=o?dm(e,t,n,u,f,w,r):void 0,h=b?.coefficients??vn(w,y),E=b?.residuals??[],x=b?.fittedValues??[],M=l-v,R=a?.map(N)??new Array(l).fill(1),T=b?.rss??0;if(b===void 0){let B=yn(w,y),J=d===void 0?B:B.map((U,H)=>U/d[H]),D=0;t.forEach((U,H)=>{if(s[H])E.push(J[D]),x.push(U-J[D]),D++;else{let me=e[H].reduce((Le,He,Xe)=>Number.isNaN(h[Xe])?Le:Le+He*h[Xe],0);x.push(me),E.push(U-me)}});for(let U=0;U<l;U++)T+=R[U]*J[U]**2}let S=x.filter((B,J)=>s[J]),A=0;if(a!==void 0&&a.some(B=>B!==0&&_(B,-u)<is)){let B=f.flatMap((J,D)=>{let U=E[J];return U===0?[]:[{w:a[D],r:U,a:Math.floor(Math.log2(Math.abs(U)))}]});if(B.length>0&&B.every(J=>Number.isFinite(J.r))){let J=Math.max(...B.map(H=>Math.log2(H.w)+2*H.a)),D=2*Math.floor(Math.floor(J)/2),U=0;for(let H of B)U+=_(H.w,2*H.a-D)*_(H.r,-H.a)**2;T=U,A=D-u}}let k=T/M,q=_(Math.sqrt(k),(u+A)/2),I=p===0?0:(p-u)/2,z=w.qr.slice(0,v).map(B=>B.slice(0,v).map(J=>p===0?_(J,-u/2):J)),{inv:P,e:V}=v>0?ro(z):{inv:[],e:[]},F=w.pivot.slice(0,v).map((B,J)=>[B,J]).sort((B,J)=>B[0]-J[0]),L=F.map(([B])=>h[B]),$=F.map(([,B])=>_(Math.sqrt(P[B][B]*k),A/2-V[B]-I)),X=L.map((B,J)=>B/$[J]),te=X.map(B=>2*Ke(-Math.abs(B),M,!0,!1)),ne={coefficients:h,residuals:E,fittedValues:x,rank:v,dfResidual:M,coefTable:{estimate:L,stdError:$,tValue:X,pValue:te},sigma:q,rSquared:0,adjRSquared:0,intercept:r,qr:w},ae=r?1:0,Me=v-ae,Ie={result:ne,fitRows:f,effects:(b?.effects??[]).map(B=>_(B,u/2)),rss:_(T,u+A)};if(Me===0)return Ie;let $e=0;if(b!==void 0)$e=b.mss;else if(r){let B=R.reduce((D,U)=>D+U,0),J=0;for(let D=0;D<l;D++)J+=R[D]*S[D]/B;for(let D=0;D<l;D++)$e+=R[D]*(S[D]-J)**2}else for(let B=0;B<l;B++)$e+=R[B]*S[B]**2;ne.rSquared=$e/($e+_(T,A)),ne.adjRSquared=1-(1-ne.rSquared)*((l-ae)/M);let pt=_($e/Me/k,-A);return ne.fStatistic={value:pt,numdf:Me,dendf:M},ne.fPValue=we(pt,Me,M,!1,!1),Ie}function Ki(e,t){let n=(1-e)/2;return n<1e-8?gt(n,t,!1,!1):gt((1+e)/2,t,!0,!1)}function wm(e,t={}){C(t,um,"confint");let n=t.level??.95;if(!(n>0&&n<1))throw new RangeError("'level' must be between 0 and 1");let r=Ki(n,e.dfResidual),{estimate:o,stdError:s}=e.coefTable,c=e.coefficients.map(()=>NaN),i=e.coefficients.map(()=>NaN),a=0;return e.coefficients.forEach((l,f)=>{Number.isNaN(l)||(c[f]=o[a]-r*s[a],i[f]=o[a]+r*s[a],a++)}),{lower:c,upper:i}}function $i(e,t,n){if(t===0||!Number.isFinite(t))return e*t;let r=Math.floor(Math.log2(e)),o=Math.floor(Math.log2(Math.abs(t)));return _(_(e,-r)*_(t,-o),r+o+n)}function bm(e,t,n={}){C(n,pm,"predict");let o=(t.length>0&&typeof t[0]!="number"?t.map(N=>Array.from(N)):Array.from(t,N=>[N])).map(N=>e.intercept?[1,...N]:N);if(o.some(N=>N.length!==e.coefficients.length))throw new RangeError("newdata must have one column per predictor of the fit");let s=e.rank,c=e.qr.pivot.slice(0,s),i=e.qr.qr.slice(0,s).map(N=>N.slice(0,s)),a=-(ji.get(e.qr)??0)/2,l=e.coefficients.flatMap((N,b)=>Number.isNaN(N)?[]:[b]),f=o.map(N=>{let b=N.reduce((h,E,x)=>Number.isNaN(e.coefficients[x])?h:h+E*e.coefficients[x],0);return jn(b,0)?Kn(l.map(h=>N[h]),l.map(h=>e.coefficients[h]),b):b}),m=N=>{let b=new Array(s).fill(0);for(let h=0;h<s;h++){let E=N[h];for(let x=0;x<h;x++)E-=i[x][h]*b[x];b[h]=E/i[h][h]}return b},u=o.map(N=>{let b=c.map(x=>N[x]),h=Eo(m(b));if(h>=is&&h<1/0||!b.every(Number.isFinite)||b.every(x=>x===0))return a===0||!(h>0&&h<1/0)?h*e.sigma:$i(h,e.sigma,a);let E=Math.floor(Math.log2(ge(b.map(Math.abs))));for(let x of[0,600]){let M=Eo(m(b.map(R=>_(R,-(E+x)))));if(M<1/0)return M===0?M*e.sigma:$i(M,e.sigma,E+x+a)}return h*e.sigma}),p=n.interval??"none";if(p!=="none"&&p!=="confidence"&&p!=="prediction")throw new RangeError(`invalid 'interval': ${String(p)}`);let d=Pr(e.qr,e.rank,o),g=d.length>0?{nonEstimable:d,warnings:[Fr]}:{};if(p==="none")return j("predict",{fit:f,seFit:u,...g},e);let w=n.level??.95;if(!(w>0&&w<1))throw new RangeError("'level' must be between 0 and 1");let y=Ki(w,e.dfResidual),v=u.map(N=>y*(p==="prediction"?Math.hypot(N,e.sigma):N));return j("predict",{fit:f,seFit:u,lwr:f.map((N,b)=>N-v[b]),upr:f.map((N,b)=>N+v[b]),...g},e)}var Nm=["weights","contrasts","levels"],Ui=["confLevel"],Yi=["sigmaX","sigmaY","mu","alternative","confLevel"];function vm(e,t,n={}){return C(n,Nm,"aov"),typeof e=="string"?j("aov",Gi(e,t,n)):j("aov",ym(e,t))}function Gi(e,t,n){let r=Ir(e,!0),{treatment:o,errors:s}=ua(r.rhs);if(s.length>1)throw new Error(`there are ${s.length} Error terms: only 1 is allowed`);return s.length===1?Ci(e,r,o,s[0],t,n):cs(e,Zt(r,t,{...n,dropUnusedLevels:!0},o),!0)}function ym(e,t){let{labels:n,groups:r}=Vt(e,t);if(r.length<2)throw new RangeError("contrasts can be applied only to factors with 2 or more levels");let o=r.flat(),s=r.flatMap((c,i)=>c.map(()=>n[i]));return Gi("y ~ g",{y:o,g:s},{levels:{g:n}})}function Hi(e){let t=e.length;if(t<2)throw new RangeError("contrasts can be applied only to factors with 2 or more levels");let n=e.reduce((u,p)=>u+p.length,0),{ssBetween:r,ssWithin:o,k:s}=_e(e),c=t-1,i=n-t,a=r/c,l=o/i,f=a/l,m=u=>_(u,2*s);return{df:[c,i],sumSq:[m(r),m(o)],meanSq:[m(a),m(l)],fValue:f,pValue:i>0?we(f,c,i,!1,!1):NaN}}function Mm(e,t,n={}){Ce(t)&&C(t,Ui,"TukeyHSD"),C(n,Ui,"TukeyHSD");let r;t!==void 0&&!Array.isArray(t)?n=t:r=t;let{labels:o,groups:s}=Vt(e,r),c=s.length;if(c<2)throw new RangeError("contrasts can be applied only to factors with 2 or more levels");let i=n.confLevel??.95;Ct(i);let l=s.reduce((y,v)=>y+v.length,0)-c;if(l===0)throw new RangeError("no residual degrees of freedom");if(l<2)throw new RangeError("the studentized range needs at least 2 residual degrees of freedom");let{ssWithin:f,mean:m,k:u}=_e(s),p=f/l,d=la(i,c,l),g=[],w={names:[],i:[],j:[],diff:[],lwr:[],upr:[],pAdj:g,pAdjusted:g,warnings:[]};for(let y=0;y<c-1;y++)for(let v=y+1;v<c;v++){let N=m[v]-m[y],b=Math.sqrt(p/2*(1/s[v].length+1/s[y].length));w.names.push(`${o[v]}-${o[y]}`),w.i.push(v+1),w.j.push(y+1),w.diff.push(_(N,u)),w.lwr.push(_(N-d*b,u)),w.upr.push(_(N+d*b,u)),w.pAdj.push(Ar(Math.abs(N)/b,c,l,{lowerTail:!1}))}return w}function Em(e,t,n={}){Ce(t)&&C(t,Yi,"zTest"),C(n,Yi,"zTest");let r;t!==void 0&&!ArrayBuffer.isView(t)&&!Array.isArray(t)?n=t:r=t;let{sigmaX:o,sigmaY:s}=n,c=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&&s===void 0)throw new RangeError("sigmaX and sigmaY are both required for two samples");for(let T of[o,s])if(T!==void 0&&!(T>0&&T<1/0))throw new RangeError("sigmaX and sigmaY must be positive finite numbers");if(Number.isNaN(c))throw new RangeError("mu must be a number");let i=fe(n.alternative),a=n.confLevel??.95;if(!(a>=0&&a<=1))throw new RangeError("confLevel must be between 0 and 1");let l=Array.from(e).filter(T=>!Number.isNaN(T)),f=r===void 0?void 0:Array.from(r).filter(T=>!Number.isNaN(T));if(l.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=ee([l,f??[],[o,s??0]]),u=m.k,[p,d,[g,w]]=m.groups,y=Un(p)[0],v=f===void 0?NaN:Un(d)[0],N=f===void 0?g/Math.sqrt(l.length):Math.sqrt(g**2/l.length+w**2/f.length),b=f===void 0?y:y-v,h=(b-_(c,-u))/N,E,x;if(i==="less")E=Y(h),x=[-1/0,b+Z(a)*N];else if(i==="greater")E=Y(h,{lowerTail:!1}),x=[b-Z(a)*N,1/0];else{E=2*Y(-Math.abs(h));let T=(1-a)/2,S=(T<1e-8?Z(T,{lowerTail:!1}):Z(1-(1-a)/2))*N;x=[b-S,b+S]}x=[_(x[0],u),_(x[1],u)];let M=f!==void 0,R=(T,S)=>{if(!(u>0&&T.some((k,q)=>k!==0&&Math.abs(S[q])<2**-1022)))return _(Un(S)[0],u);let A=Math.max(ee([T]).k,0);return _(Un(A===0?T:T.map(k=>_(k,-A)))[0],A)};return{method:M?"Two-sample z-Test":"One-sample z-Test",alternative:i,statistic:h,statisticName:"z",pValue:E,confInt:x,confLevel:a,estimate:M?[R(l,p),R(f,d)]:R(l,p),estimateNames:M?["mean of x","mean of y"]:"mean of x",nullValue:c,nullValueName:M?"difference in means":"mean",warnings:[]}}var xm=["family","link","weights","offset","start","epsilon","maxit","intercept"],Rm=["family","link","variance"],Xi=["power"],Tm=["dispersion"],Sm=["type"],Am=["dispersion"],km=["dispersion"],qm=["newdata","newoffset","type","dispersion"],kn=2**-52;function _m(e){let t=e.map(n=>`"${n}"`);return t.length===1?t[0]:`c(${t.join(", ")})`}function sr(e,t,n){return e.map((r,o)=>{let s=0;for(let c=0;c<r.length;c++)s+=r[c]*t[c];return s+n[o]})}function zt(e,t,n,r){let o=0;for(let s=0;s<t.length;s++)r[s]!==0&&(o+=e.devContribution(t[s],n[s],r[s]));return o}var Zi=2**-1022,oo=e=>e===0||Math.abs(e)>=Zi&&Math.abs(e)<1/0;function Bi(e,t,n,r){let o=e.variance(r),s=n**2,c=Math.sqrt(t*s/o);if(c>0&&c<1/0&&oo(s)&&s!==0&&oo(o)&&o!==0&&oo(t*s))return c;if(e.muPower!==void 0&&e.varPower!==void 0&&r>0){let{c:i,a}=e.muPower;return Math.sqrt(t)*Math.abs(i)*r**((2*a-e.varPower)/2)}return c}function fs(e,t,n,r){let o=(t-n)/r;if(Math.abs(r)>=Zi&&Math.abs(r)<1/0)return o;if(e.muPower!==void 0&&n>0){let{c:s,a:c}=e.muPower,i=n**(c/2);return(t-n)/s/i/i}return o}function Im(e,t,n,r){let o=e.variance(n),s=(t-n)*Math.sqrt(r)/Math.sqrt(o);if(Number.isFinite(s)&&oo(o)&&o!==0||e.varPower===void 0||!(n>0))return s;let c=n**(e.varPower/4);return(t-n)/c*Math.sqrt(r)/c}var Lm=.1;function Ji(e,t){let{x:n,p:r,y:o,w:s,offset:c,fam:i,epsilon:a,maxit:l}=e,f=o.length;if(o.some((m,u)=>s[u]>0&&!Number.isFinite(m)))throw new RangeError("NA/NaN/Inf in 'y'");if(n.some((m,u)=>s[u]>0&&m.some(p=>!Number.isFinite(p))))throw new RangeError("NA/NaN/Inf in 'x'");return Gn(t,()=>{let m=(k,q)=>k.every(I=>i.etaOk(I))&&q.every(I=>i.muOk(I));if(r===0){let k=c.slice();if(!k.every(P=>i.etaOk(P)))throw new Error("invalid linear predictor values in empty model");let q=k.map(P=>i.linkinv(P));if(!q.every(P=>i.muOk(P)))throw new Error("invalid fitted means in empty model");let I=k.map(P=>i.muEta(P)),z=q.map((P,V)=>Bi(i,s[V],I[V],P)**2);return{coefficients:[],eta:k,mu:q,deviance:zt(i,o,q,s),iter:0,converged:!0,boundary:!0,rank:0,qr:void 0,used:s.map(P=>P>0),workingWeights:z,workingResiduals:o.map((P,V)=>fs(i,P,q[V],I[V]))}}let u,p;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 ${_m(e.columnNames)}`);p=e.start.slice(),u=sr(n,p,c)}else e.etastart!==void 0?u=e.etastart.slice():u=e.mustart.map(k=>i.link(k));let d=u.map(k=>i.linkinv(k));if(!m(u,d))throw new Error("cannot find valid starting values: please specify some");let g=zt(i,o,d,s),w=g,y=!1,v=!1,N=0,b=new Array(r).fill(0),h,E=s.map(()=>!1),x=[],M=[],R=Math.min(1e-7,a/1e3);for(N=1;N<=l;N++){let k=u.map(F=>i.muEta(F));for(let F=0;F<f;F++){if(!(s[F]>0))continue;let L=i.variance(d[F]);if(Number.isNaN(L))throw new Error("NAs in V(mu)");if(L===0&&!(i.varPower!==void 0&&d[F]>0))throw new Error("0s in V(mu)");if(Number.isNaN(k[F]))throw new Error("NAs in d(mu)/d(eta)")}E=s.map((F,L)=>F>0&&k[L]!==0);let q=[];for(let F=0;F<f;F++)E[F]&&q.push(F);if(q.length===0){t.add(`no observations informative at iteration ${N}`),y=!1;break}x=new Array(f).fill(0);let I=[],z=[];for(let F of q){let L=Bi(i,s[F],k[F],d[F]);x[F]=L,I.push((u[F]-c[F]+fs(i,o[F],d[F],k[F]))*L),z.push(n[F].map($=>$*L))}h=ve(z,{tol:R});let P=vn(h,I);if(P.some((F,L)=>h.pivot.indexOf(L)<h.rank&&!Number.isFinite(F))){t.add(`non-finite coefficients at iteration ${N}`),y=!1;break}b=P.map(F=>Number.isNaN(F)?0:F),v=!1;let V=u;if(u=sr(n,b,c),d=u.map(F=>i.linkinv(F)),w=zt(i,o,d,s),!Number.isFinite(w)){if(p===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 F=0;for(;!Number.isFinite(w);){if(F>=l)throw new Error("inner loop 1; cannot correct step size");F++,b=b.map((L,$)=>(L+p[$])/2),u=sr(n,b,c),d=u.map(L=>i.linkinv(L)),w=zt(i,o,d,s)}v=!0}if(!m(u,d)){if(p===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 F=0;for(;!m(u,d);){if(F>=l)throw new Error("inner loop 2; cannot correct step size");F++,b=b.map((L,$)=>(L+p[$])/2),u=sr(n,b,c),d=u.map(L=>i.linkinv(L))}v=!0,w=zt(i,o,d,s)}let W=Math.max(a,1e-10);if(p!==void 0&&N>1&&(w-g)/(.1+Math.abs(w))>=W){t.add("step size truncated due to increasing deviance");let F=0;for(;(w-g)/(.1+Math.abs(w))>=W&&F<l;)F++,b=b.map((L,$)=>(L+p[$])/2),u=sr(n,b,c),d=u.map(L=>i.linkinv(L)),w=zt(i,o,d,s);if((w-g)/(.1+Math.abs(w))>=W){t.add("inner loop 3; cannot correct step size"),y=!1;break}}if(e.untilCoefficients){let F=ge(b.map(Math.abs));if(p!==void 0&&b.every((L,$)=>Math.abs(L-p[$])<=8*kn*F)){y=!0;break}if(M.some(L=>L.every(($,X)=>$===b[X]))){y=!0;break}M.push(b.slice()),M.length>16&&M.shift()}else if(Math.abs(w-g)/(Math.abs(w)+.1)<a){y=!0;let F=0;for(let L=0;L<f;L++)E[L]&&(F=Math.max(F,Math.abs(u[L]-V[L])));F>Lm&&t.add("glm.fit: the linear predictor was still changing at convergence: the data may be separated (no finite maximum likelihood estimates)");break}g=w,p=b.slice()}N>l&&(N=l);let T=r,S=b.slice();if(h!==void 0){T=h.rank;for(let k=h.rank;k<r;k++)S[h.pivot[k]]=NaN}let A=u.map(k=>i.muEta(k));return{coefficients:S,eta:u,mu:d,deviance:w,iter:N,converged:y,boundary:v,rank:T,qr:h,used:E,workingWeights:x.map(k=>k*k),workingResiduals:o.map((k,q)=>fs(i,k,d[q],A[q]))}})}function ec(e){return e.length>0&&typeof e[0]!="number"?e.map(t=>Array.from(t)):Array.from(e,t=>[t])}function tc(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*kn||o<10*kn)&&n.add("glm.fit: fitted probabilities numerically 0 or 1 occurred"),t.family==="poisson"&&e.mu.some(o=>o<10*kn)&&n.add("glm.fit: fitted rates numerically 0 occurred"))}function qn(e,t,n,r={}){let o=En(e.familySpec),s="start"in r?r.start:e.start,c=xa(o,e.yGiven,e.priorGiven,s!==void 0||r.etastart!==void 0,n);if(!c.weights.some(f=>f>0))throw new RangeError("all weights are zero");let i=r.extraOffset===void 0?e.offset:e.offset.map((f,m)=>f+r.extraOffset[m]),a=e.x.map(f=>t.map(m=>f[m])),l=Ji({x:a,p:t.length,y:c.y,w:c.weights,offset:i,fam:o,epsilon:r.epsilon??e.epsilon,maxit:r.maxit??e.maxit,columnNames:t.map(f=>e.columnNames[f]),start:s,etastart:r.etastart,mustart:c.mustart,untilCoefficients:r.untilCoefficients},n);return tc(l,o,n,t.length===0),{fit:l,fam:o,y:c.y,w:c.weights,size:c.size,offset:i}}function Om(e,t,n={}){C(n,xm,"glm"),C(Ft(n,"family"),Rm,"glm","family"),C(Ft(n,"family.link"),Xi,"glm","family.link"),C(Ft(n,"link"),Xi,"glm","link");let r=n.intercept??!0,o=ec(e);if(o.some(M=>M.length!==o[0].length))throw new TypeError("all rows of x must have the same length");let s=t.length>0&&typeof t[0]!="number",c=s?t.map(M=>Array.from(M)):Array.from(t),i=c.length;if(o.length!==i)throw new RangeError("variable lengths differ (found for 'x1')");let a=n.weights===void 0?void 0:Array.from(n.weights);if(a!==void 0&&a.length!==i)throw new RangeError("variable lengths differ (found for '(weights)')");let l=n.offset===void 0?void 0:Array.from(n.offset);if(l!==void 0&&l.length!==i)throw new RangeError("variable lengths differ (found for '(offset)')");let f=o.map((M,R)=>{let T=c[R];return!(Array.isArray(T)?T.some(A=>Number.isNaN(A)):Number.isNaN(T))&&M.every(A=>!Number.isNaN(A))&&!(a!==void 0&&Number.isNaN(a[R]))&&!(l!==void 0&&Number.isNaN(l[R]))}),m=o.filter((M,R)=>f[R]).map(M=>r?[1,...M]:M),u=c.filter((M,R)=>f[R]),p=u.length;if(p===0)throw new RangeError("0 (non-NA) cases");let d=a===void 0?new Array(p).fill(1):a.filter((M,R)=>f[R]);if(d.some(M=>M<0))throw new RangeError("negative weights not allowed");if(d.some(M=>!Number.isFinite(M)))throw new RangeError("non-finite weights not allowed");let g=l===void 0?new Array(p).fill(0):l.filter((M,R)=>f[R]);if(g.some(M=>!Number.isFinite(M)))throw new RangeError("the offset must be finite");let w=o.length>0?o[0].length:0,y=[...r?["(Intercept)"]:[],...Array.from({length:w},(M,R)=>`x${R+1}`)],v=y.length,N=typeof n.family=="object"?{...n.family}:{family:n.family??"gaussian"};n.link!==void 0&&(N.link=n.link);let b={x:m,columnNames:y,yGiven:u,priorGiven:d,offset:g,hasOffset:l!==void 0,intercept:r,familySpec:N,epsilon:n.epsilon??1e-8,maxit:Math.floor(n.maxit??25),start:n.start===void 0?void 0:Array.from(n.start)};if(!(b.epsilon>0&&b.epsilon<1/0))throw new RangeError("value of 'epsilon' must be > 0 and finite");if(!(b.maxit>0&&b.maxit<1/0))throw new RangeError("maximum number of iterations must be > 0");let h=!s&&Fm(N)?ee([u]).k:0;if(h===0)return j("glm",Qi(b));let E=M=>_(M,-h),x=Qi({...b,yGiven:u.map(E),offset:g.map(E),start:b.start?.map(E)});return j("glm",Pm(x,h,{...b,yScale:h}))}function Qi(e){let{x:t,offset:n,intercept:r}=e,o=t.length,s=e.columnNames.length,c=new Te,i=Array.from({length:s},(w,y)=>y),{fit:a,fam:l,y:f,w:m,size:u}=qn(e,i,c),p;if(r&&e.hasOffset){let w=new Te,y=Ji({x:t.map(()=>[1]),p:1,y:f,w:m,offset:n,fam:l,epsilon:e.epsilon,maxit:e.maxit,columnNames:["(Intercept)"],mustart:a.mu},w);tc(y,l,w,!1),w.list.forEach(v=>c.add(v)),p=y.deviance,y.converged||c.add("fitting to calculate the null deviance did not converge -- increase 'maxit'?")}else if(r){let w=Math.floor(Math.log2(ge(m))),y=0,v=0;for(let b=0;b<o;b++){if(!(m[b]>0))continue;let h=_(m[b],-w);y+=h,v+=h*f[b]}let N=v/y;if(!Number.isFinite(N)){let b=0;for(let x=0;x<o;x++)m[x]>0&&(b=Math.max(b,Math.abs(f[x])));let h=Math.floor(Math.log2(b)),E=0;for(let x=0;x<o;x++)m[x]>0&&(E+=_(m[x],-w)*_(f[x],-h));N=_(E/y,h)}p=Gn(c,()=>zt(l,f,f.map(()=>N),m))}else p=Gn(c,()=>zt(l,f,n.map(w=>l.linkinv(w)),m));let d=o-m.filter(w=>w===0).length,g=Ra(l,f,a.mu,m,u,a.deviance,c)+2*a.rank;return nc(a.used,{coefficients:a.coefficients,fittedValues:a.mu,linearPredictors:a.eta,deviance:a.deviance,nullDeviance:p,aic:g,rank:a.rank,dfResidual:d-a.rank,dfNull:d-(r?1:0),iter:a.iter,converged:a.converged,boundary:a.boundary,y:f,priorWeights:m,weights:a.workingWeights,residuals:a.workingResiduals,family:l.family,link:l.name,warnings:c.list,qr:a.qr,model:e})}function Fm(e){return(e.link??"identity")!=="identity"?!1:e.family==="gaussian"||e.family==="quasi"&&(e.variance??"constant")==="constant"}function Pm(e,t,n){let r=s=>_(s,t),o={...e,coefficients:e.coefficients.map(r),fittedValues:e.fittedValues.map(r),linearPredictors:e.linearPredictors.map(r),deviance:_(e.deviance,2*t),nullDeviance:_(e.nullDeviance,2*t),aic:e.aic+2*e.priorWeights.filter(s=>s>0).length*t*Math.LN2,y:e.y.map(r),residuals:e.residuals.map(r),model:n};return Object.defineProperty(o,"scaled",{value:e,enumerable:!1}),nc(ms.get(e),o)}var ms=new WeakMap;function nc(e,t){return e!==void 0&&ms.set(t,e),t}function Nt(e){let t=e.scaled;return t===void 0?{fit:e,k:0}:{fit:t,k:e.model.yScale??0}}function ar(e){return En(e.model.familySpec)}function rc(e,t){let n=Nt(e),r=ir(n.fit,t);return ar(e).fixedDispersion?r:_(r,2*n.k)}function ir(e,t){if(ar(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===1/0?Dm(e):n/e.dfResidual}function Dm(e){let t=[];for(let s=0;s<e.weights.length;s++)e.weights[s]>0&&t.push(s);if(!t.every(s=>Number.isFinite(e.weights[s])&&Number.isFinite(e.residuals[s])))return 1/0;let n=ee([t.map(s=>e.residuals[s])]),r=ee([t.map((s,c)=>Math.sqrt(e.weights[s])*Math.abs(n.groups[0][c]))]),o=0;for(let s of r.groups[0])o+=s*s;return _(o/e.dfResidual,2*(n.k+r.k))}function Cm(e){return e.qr===void 0||e.rank===0?{inv:[],e:[]}:ro(e.qr.qr.slice(0,e.rank).map(t=>t.slice(0,e.rank)))}function Vm(e){if(!(e>=0&&e<1/0))throw new RangeError("'dispersion' must be a non-negative finite number");return e}function _n(e,t){let n=_(Vm(e),-2*t);if(n===0?e===0:n>=2**-1022&&n<1/0)return{m:n,s:0};let r=Math.floor(Math.floor(Math.log2(e))/2);return{m:_(e,-2*r),s:r-t}}function us(e,t={}){C(t,Tm,"glmSummary");let{fit:n,k:r}=Nt(e),o=new Te,s=ar(n),c=t.dispersion===void 0&&!s.fixedDispersion,i=t.dispersion===void 0?void 0:_n(t.dispersion,r),a=i===void 0?ir(n,o):i.m,l=i?.s??0,{inv:f,e:m}=Cm(n),u=f.map((h,E)=>h.map((x,M)=>_(x,-m[E]-m[M]))),d=(n.qr===void 0?[]:n.qr.pivot.slice(0,n.rank)).map(h=>n.coefficients[h]),g=f.map((h,E)=>_(Math.sqrt(h[E]*a),-m[E])),w=l===0?d.map((h,E)=>h/g[E]):d.map((h,E)=>De(h/Math.sqrt(f[E][E]*a),m[E]-l)),y=!c,v=w.map(h=>y?2*Y(-Math.abs(h)):n.dfResidual>0?2*Ke(-Math.abs(h),n.dfResidual,!0,!1):NaN);c&&n.dfResidual===0&&(g.fill(NaN),w.fill(NaN));let N=h=>_(h,r),b=h=>_(h,2*r);return j("glmSummary",{dispersion:l===0?b(a):t.dispersion,estimate:d.map(N),stdError:l===0?g.map(N):f.map((h,E)=>De(Math.sqrt(h[E]*a),l+r-m[E])),statistic:w,pValue:v,statisticName:y?"z value":"t value",covUnscaled:u,covScaled:l===0?u.map(h=>h.map(E=>b(E*a))):f.map((h,E)=>h.map((x,M)=>De(x*a,2*(l+r)-m[E]-m[M]))),dfResidual:n.dfResidual,warnings:o.list},e)}function zm(e,...t){C(Nn(t),Sm,"glmResiduals");let n=bn(["type"],t).type??"deviance",r=Nt(e);return ps(r.fit,n).map(o=>_(o,r.k))}function ps(e,t){let n=ar(e),{y:r,fittedValues:o,priorWeights:s}=e;switch(t){case"response":return r.map((c,i)=>c-o[i]);case"working":return e.residuals.slice();case"pearson":return r.map((c,i)=>Im(n,c,o[i],s[i]));case"deviance":return r.map((c,i)=>s[i]===0?0:Math.sign(c-o[i])*Math.sqrt(Math.max(n.devContribution(c,o[i],s[i]),0)));default:throw new RangeError(`unknown type of residuals "${String(t)}"`)}}function hs(e){let t=[],n=e.qr,r=[];if(n!==void 0&&e.rank>0){let c=n.qr.length,i=Array.from({length:c},(l,f)=>Array.from({length:e.rank},(m,u)=>f===u?1:0));r=ya(n,i).map(l=>l.reduce((f,m)=>f+m*m,0))}let o=0,s=ms.get(e)??e.weights.map(c=>c>0);return e.priorWeights.forEach((c,i)=>{let a=s[i]?r[o++]??0:0;c>0&&t.push(a>1-10*kn?1:a)}),t}function $m(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 Wm(e,t={}){C(t,Am,"glmRstandard");let{fit:n,k:r}=Nt(e),o=hs(n),{m:s,s:c}=t.dispersion===void 0?{m:ir(n),s:0}:_n(t.dispersion,r),i=ps(n,"deviance").filter((l,f)=>n.priorWeights[f]>0),a=n.rank>0?100*kn*$m(i.map(Math.abs)):0;return i.map((l,f)=>{let m=De((Math.abs(l)<a?0:l)/Math.sqrt(s*(1-o[f])),-c);return Number.isFinite(m)?m:NaN})}function jm(e,t={}){C(t,km,"glmCooksDistance");let{fit:n,k:r}=Nt(e),o=hs(n),{m:s,s:c}=t.dispersion===void 0?{m:ir(n),s:0}:_n(t.dispersion,r);return ps(n,"pearson").filter((a,l)=>n.priorWeights[l]>0).map((a,l)=>{let f=o[l]/(s*n.rank),m=(a/(1-o[l]))**2*f;return(!Number.isFinite(m)||f<2**-1022)&&(m=Km(a,o[l],s,n.rank)),c!==0&&(m=De(m,-2*c)),Number.isFinite(m)?m:NaN})}function Km(e,t,n,r){if(!Number.isFinite(e)||!(n>0&&Number.isFinite(n)))return NaN;let o=e===0?0:Math.floor(Math.log2(Math.abs(e))),s=Math.floor(Math.log2(n)),c=_(e,-o)/(1-t);return _(c*c*(t/(_(n,-s)*r)),2*o-s)}function Um(e,t={}){C(t,qm,"glmPredict");let{fit:n,k:r}=Nt(e),o=v=>_(v,r),s=ar(n),c,i;if(t.newdata===void 0)c=n.model.x,i=n.linearPredictors.slice();else{if(c=ec(t.newdata).map(b=>n.model.intercept?[1,...b]:b),c.some(b=>b.length!==n.coefficients.length))throw new RangeError("newdata must have one column per predictor of the fit");let v=t.newoffset===void 0?c.map(()=>0):Array.from(t.newoffset,b=>_(b,-r));if(v.length!==c.length)throw new RangeError("newoffset must have one value per row of newdata");let N=n.coefficients.flatMap((b,h)=>Number.isNaN(b)?[]:[h]);i=c.map((b,h)=>{let E=b.reduce((M,R,T)=>Number.isNaN(n.coefficients[T])?M:M+R*n.coefficients[T],0);return(jn(E,0)?Kn(N.map(M=>b[M]),N.map(M=>n.coefficients[M]),E):E)+v[h]})}let{m:a,s:l}=t.dispersion===void 0?{m:ir(n),s:0}:_n(t.dispersion,r),f=v=>_(v,r+l),m=n.rank,u=n.qr===void 0?[]:n.qr.pivot.slice(0,m),p=n.qr?.qr,d=c.map(v=>{let N=new Array(m).fill(0),b=0;for(let h=0;h<m;h++){let E=v[u[h]];for(let x=0;x<h;x++)E-=p[x][h]*N[x];N[h]=E/p[h][h],b+=N[h]*N[h]}if(!(b>=2**-1022&&b<1/0)){let h=N.reduce((E,x)=>Math.max(E,Math.abs(x)),0);if(h>0&&Number.isFinite(h)){let E=Math.sqrt(N.reduce((M,R)=>M+(R/h)**2,0)),x=h*E*Math.sqrt(a);return Number.isFinite(x)?x:h*(E*Math.sqrt(a))}}return Math.sqrt(b)*Math.sqrt(a)}),g=t.type??"link";if(g!=="link"&&g!=="response")throw new RangeError(`invalid 'type': ${String(g)}`);let w=t.newdata===void 0?[]:Pr(n.qr,m,c),y=w.length>0?{nonEstimable:w,warnings:[Fr]}:{};return g==="link"?j("glmPredict",{fit:i.map(o),seFit:d.map(f),...y},e):j("glmPredict",Gn(new Te,()=>({fit:i.map(v=>o(s.linkinv(v))),seFit:d.map((v,N)=>f(v*Math.abs(s.muEta(i[N])))),...y})),e)}var Ym=["parm","level","test"],oc={epsilon:1e-300,maxit:100,untilCoefficients:!0};function ds(e,t,n,r,o=1e-11){let s=[];for(let g=0;g<t.length;g++)n[g]>0&&s.push(g);let c=0,i=0;for(let g of s)c+=n[g],i+=n[g]*t[g];if(r&&!(Number.isFinite(c)&&Number.isFinite(i))){let g=0;for(let w of s)g=Math.max(g,n[w]);if(Number.isFinite(g)){let w=g>1?Math.floor(Math.log2(g)):0;c=0,i=0;for(let y of s){let v=_(n[y],-w);c+=v,i+=v*t[y]}}}let a=r?i/c:0,l=0;for(let g of s)l+=n[g]*(t[g]-a)**2;if((e.length>0?e[0].length:0)===0||s.length===0)return 0;let m=s.map(g=>Math.sqrt(n[g])),u=ve(s.map((g,w)=>e[g].map(y=>y*m[w])),{tol:o}),p=yn(u,s.map((g,w)=>t[g]*m[w])),d=0;for(let g of p)d+=g*g;if(!Number.isFinite(l)){let g=s.map((y,v)=>m[v]*(t[y]-a)),w=0;for(let y of g)w=Math.max(w,Math.abs(y));for(let y of p)w=Math.max(w,Math.abs(y));if(Number.isFinite(w)){let y=w>1?Math.floor(Math.log2(w)):0,v=0;for(let b of g)v+=_(b,-y)**2;let N=0;for(let b of p)N+=_(b,-y)**2;return _(_(v-N,y),y)}}return l-d}function Gm(e,t={}){C(t,Ym,"glmConfint");let{fit:n,k:r}=Nt(e),o=t.level??.95,s=t.test??"LRT";if(s!=="LRT"&&s!=="Rao")throw new RangeError(`invalid 'test': ${String(s)}`);if(!(o>0&&o<1))throw new RangeError("'level' must be in (0, 1)");let c=n.coefficients.length,i=t.parm??Array.from({length:c},(q,I)=>I);for(let q of i)if(!Number.isInteger(q)||q<0||q>=c)throw new RangeError(`invalid coefficient index ${q}`);let a=n.model,l=n.coefficients.map((q,I)=>I).filter(q=>!Number.isNaN(n.coefficients[q])),f=new Te,m=qn(a,l,f,{...oc,start:a.start===void 0?void 0:l.map(q=>a.start[q])}),u=new Array(c).fill(NaN);if(l.forEach((q,I)=>u[q]=m.fit.coefficients[I]),!m.fit.converged)return{lower:i.map(()=>NaN),upper:i.map(()=>NaN)};let p=!1,d={...n,coefficients:u,weights:m.fit.workingWeights,residuals:m.fit.workingResiduals,qr:m.fit.qr,rank:m.fit.rank},g=Re(()=>us(d)),w=g.dispersion,y=new Array(c).fill(NaN);(m.fit.qr===void 0?[]:m.fit.qr.pivot.slice(0,m.fit.rank)).forEach((q,I)=>y[l[q]]=g.stdError[I]);let N=a.x.map(q=>l.map(I=>q[I])),b=m.fit.eta,h=(q,I)=>{let z=l.filter(W=>W!==q),P=qn(a,z,new Te,{...oc,start:void 0,etastart:b,extraOffset:a.x.map(W=>W[q]*I)}).fit;if(!P.converged)return p=!0,NaN;let V=s==="LRT"?P.deviance-m.fit.deviance:ds(N,P.workingResiduals,P.workingWeights,!1);return Math.sign(I-u[q])*Math.sqrt(Math.max(V/w,0))},E=(1-o)/2,x=Math.sqrt(Math.PI/2)*o*(1+Math.PI/12*o**2+7*Math.PI**2/480*o**4),M=2**-9,R=x<M,T=R?[-x,x]:[Z(E),E<1e-8?Z(E,{lowerTail:!1}):Z((1+o)/2)],S=i.map(()=>NaN),A=i.map(()=>NaN),k=(q,I,z)=>{let P=z===0?-1:1;p=!1;let V=NaN,W=NaN;for(let ae=0;ae<=30;ae++){let Me=u[q]+P*2**ae*y[q],Ie=h(q,Me);if(Math.abs(Ie)>=Math.abs(I)){V=Me,W=Ie;break}}if(Number.isNaN(V)||p)return NaN;let F=ae=>ae===u[q]?-I:h(q,ae)-I,[L,$]=z===0?[V,u[q]]:[u[q],V],[X,te]=z===0?[W-I,-I]:[-I,W-I],ne;try{ne=wt(F,L,$,{tol:1e-14*y[q],maxIter:500,fLower:X,fUpper:te})}catch(ae){if(!p)throw ae;ne=NaN}return p?NaN:ne};return i.forEach((q,I)=>{if(Number.isNaN(u[q]))return;if(!R){S[I]=k(q,T[0],0),A[I]=k(q,T[1],1);return}let z=[-2*M,-M,0,M,2*M],P=[k(q,-2*M,0),k(q,-M,0),u[q],k(q,M,1),k(q,2*M,1)].map(W=>W-u[q]),V=W=>{let F=0;for(let L=0;L<5;L++){let $=1;for(let X=0;X<5;X++)X!==L&&($*=(W-z[X])/(z[L]-z[X]));F+=$*P[L]}return u[q]+F};S[I]=V(T[0]),A[I]=V(T[1])}),{lower:S.map(q=>_(q,r)),upper:A.map(q=>_(q,r))}}var Hm=["test","dispersion"],Xm=(e,t)=>xe(e,t/2,2,!1,!1);function Bm(e,t,n){let{fit:r,w:o}=qn(e,t,n,{start:void 0});return{dfRes:o.filter(c=>c>0).length-r.rank,dev:r.deviance,fit:r,x:e.x.map(c=>t.map(i=>c[i])),intercept:e.intercept}}var gs=(e,t)=>ds(t.x,e.fit.workingResiduals,e.fit.workingWeights,e.intercept);function Qm(e,t={}){C(t,Hm,"glmAnova");let n=Array.isArray(e)?e:[e];if(n.length===0)throw new RangeError("no fits to compare");let r=n.map(Nt),o=r.every(u=>u.k===r[0].k)?r[0].k:0,s=o===0?n:r.map(u=>u.fit),c=t.dispersion===void 0?void 0:_n(t.dispersion,o),i=c?.s??0,a=Zm(s,Array.isArray(e)&&s.length>1,{...t,...c===void 0?{}:{dispersion:c.m}},i);if(o===0&&i===0)return j("glmAnova",a,...n);let l=new Set(["Deviance","Resid. Dev","Rao","Cp"]),f=a.colNames.map(u=>l.has(u)),m=a.table.map(u=>u.map((p,d)=>f[d]?_(p,2*o):p));if(i!==0&&a.colNames.includes("Cp")){let u=a.colNames.indexOf("Cp"),p=a.colNames.indexOf("Resid. Dev"),d=a.colNames.indexOf("Resid. Df"),g=s[0].model.x.length;for(let w of m)w[u]=w[p]+2*(g-w[d])*t.dispersion}return j("glmAnova",{...a,table:m},...n)}function Zm(e,t,n,r=0){let o=new Te,s=e[0],c=En(s.model.familySpec);if(e.some(M=>{let R=En(M.model.familySpec);return R.family!==c.family||R.name!==c.name||R.varianceName!==c.varianceName}))throw new RangeError("models were not all fitted with the same family and link");let i=c.fixedDispersion,a=n.test===void 0?n.dispersion!==void 0||i?"Chisq":"F":n.test;if(a!==!1&&!["Rao","LRT","Chisq","F","Cp"].includes(a))throw new RangeError(`invalid 'test': ${String(a)}`);let l=[],f,m,u=[],p=new Set,d;if(t){let M=e[0].model.x.length;if(e.some(T=>T.model.x.length!==M))throw new RangeError("models were not all fitted to the same size of dataset");let R=(T,S)=>T.length===S.length&&T.every((A,k)=>JSON.stringify(A)===JSON.stringify(S[k]));if(e.some(T=>!R(T.model.yGiven,e[0].model.yGiven)))throw new RangeError("models were not all fitted to the same response");if(e.some(T=>!R(T.model.priorGiven,e[0].model.priorGiven)))throw new RangeError("models were not all fitted with the same prior weights");if(e.forEach((T,S)=>l.push({name:String(S+1),dfRes:T.dfResidual,dev:T.deviance})),f=l.map((T,S)=>S===0?NaN:l[S-1].dfRes-T.dfRes),m=l.map((T,S)=>S===0?NaN:l[S-1].dev-T.dev),e.forEach((T,S)=>{if(S===0||f[S]===0)return;let[A,k]=f[S]>0?[e[S-1],T]:[T,e[S-1]],q=P=>P.filter((V,W)=>A.priorWeights[W]>0&&k.priorWeights[W]>0),I=A.model.offset.map((P,V)=>P-k.model.offset[V]),z=I.some((P,V)=>P!==0&&A.priorWeights[V]>0)?A.model.x.map((P,V)=>[...P,I[V]]):A.model.x;Dr(q(z),q(k.model.x))||p.add(S)}),a==="Rao"){let T=e.map(S=>({dfRes:S.dfResidual,dev:S.deviance,fit:sc(S),x:S.model.x,intercept:S.model.intercept}));u=l.map((S,A)=>A===0?NaN:f[A]<0?-gs(T[A],T[A-1]):gs(T[A-1],T[A]))}d=e.reduce((T,S)=>S.dfResidual<T.dfResidual?S:T)}else{let M=s,R=M.model,T=R.x.length>0?R.x[0].length-(R.intercept?1:0):0,S=R.intercept?[0]:[],A=q=>[...S,...Array.from({length:q},(I,z)=>z+S.length)],k=[];for(let q=0;q<=T;q++)q===T?k.push({dfRes:M.dfResidual,dev:M.deviance,fit:sc(M),x:R.x,intercept:R.intercept}):q>0||a==="Rao"?k.push(Bm(R,A(q),o)):k.push(void 0);l.push({name:"NULL",dfRes:M.dfNull,dev:M.nullDeviance});for(let q=1;q<=T;q++)l.push({name:`x${q}`,dfRes:k[q].dfRes,dev:k[q].dev});f=l.map((q,I)=>I===0?NaN:l[I-1].dfRes-q.dfRes),m=l.map((q,I)=>I===0?NaN:Math.max(l[I-1].dev-q.dev,0)),a==="Rao"&&(u=l.map((q,I)=>I===0?NaN:gs(k[I-1],k[I]))),d=M}e.forEach((M,R)=>{M.converged||o.add(`model ${R+1} has not converged: its deviance is not that of the maximum likelihood fit`)});let g=new Te,w=n.dispersion??rc(d,g);g.list.forEach(M=>o.add(M));let y=i?1/0:d.dfResidual,v=t?["Resid. Df","Resid. Dev","Df","Deviance"]:["Df","Deviance","Resid. Df","Resid. Dev"],N=l.map((M,R)=>t?[M.dfRes,M.dev,f[R],m[R]]:[f[R],m[R],M.dfRes,M.dev]),b=[...v],h=M=>l.map((R,T)=>{if(T===0||f[T]===0||p.has(T))return NaN;let S=De(M[T]/w*Math.sign(f[T]),-2*r);return S<0||Number.isNaN(S)?NaN:Xm(S,Math.abs(f[T]))}),E=[];if(a==="Chisq"||a==="LRT")b.push("Pr(>Chi)"),E=[h(m)];else if(a==="Rao")b.push("Rao","Pr(>Chi)"),E=[u.map((M,R)=>p.has(R)?NaN:M),h(u)];else if(a==="F"){if(i&&o.add(`using F test with a '${c.family}' family is inappropriate`),y!==0){let M=l.map((R,T)=>{let S=De(m[T]/f[T]/w,-2*r);return T===0||f[T]===0||p.has(T)||S<0?NaN:S});b.push("F","Pr(>F)"),E=[M,M.map((R,T)=>Number.isNaN(R)?NaN:we(R,Math.abs(f[T]),y,!1,!1))]}}else if(a==="Cp"){let M=d.model.x.length;b.push("Cp"),E=[l.map(R=>R.dev+2*(M-R.dfRes)*w)]}let x=N.map((M,R)=>[...M,...E.map(T=>T[R])]);return{colNames:b,rowNames:l.map(M=>M.name),table:x,warnings:o.list}}function sc(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}}var Jm=["type"];function ac(e,t,n,r,o=0){let s=[...t,...n];if(s.length===0)return{ss:0,df:0};let c=e.y.flatMap((d,g)=>e.weights===void 0||e.weights[g]!==0?[g]:[]),i=c.map(d=>e.weights===void 0?1:Math.sqrt(e.weights[d])),a=ve(c.map((d,g)=>s.map(w=>e.x[d][w]*i[g]))),l=Jr(e.weights===void 0?void 0:c.map(d=>_(e.weights[d],-o)),c.length),f=a.pivot.slice(0,a.rank),{qty:m}=Sn(f.map(d=>c.map((g,w)=>st(l[w],e.x[g][s[d]]))),c.map((d,g)=>st(l[g],_(e.y[d],-r)))),u=[0,0],p=0;return f.forEach((d,g)=>{d>=t.length&&(u=Q(u,pe(m[g],m[g])),p++)}),{ss:u[0],df:p}}function ws(e,t={}){if(C(t,Jm,"Anova"),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,s=eo([e]),c=to([e],s)[0];if(o===0)throw new Error("residual df = 0");let i=os(e,s),a=E=>{let x=i.map(([T,S])=>[_(T,-E),S]),M=x.reduce((T,[S])=>T+S,0),R=x.reduce((T,[S,A])=>T+S*A,0)/M;return Number.isFinite(M)&&Number.isFinite(R)?x.reduce((T,[S,A])=>T+S*(A-R)**2,0):NaN},l=0,f=a(0);if(Number.isFinite(f)||(l=no(i),f=a(l)),rn(i,c,l)||c===0)throw new Error("residual sum of squares is 0 (within rounding error)");let m=[...e.warnings],u=e.termLabels.length;r===2&&u===0&&e.intercept&&(m.push("the model contains only an intercept: Type III test substituted"),r=3);let p=E=>e.assign.flatMap((x,M)=>E.has(x)?[M]:[]),d=[...new Set(e.assign)],g=[],w=[],y=E=>{g.length=0;let x=[];if(r===3){if(e.coefficients.some(M=>Number.isNaN(M)))throw new Error("there are aliased coefficients in the model");for(let M of d){let R=p(new Set([M])),T=p(new Set(d.filter(S=>S!==M)));g.push(M===0?"(Intercept)":e.termLabels[M-1]),x.push({ss:ac(e,T,R,s,E).ss,df:R.length})}}else{let M=R=>e.factors.flatMap((T,S)=>T[R-1]>0?[S]:[]);for(let R of d){if(R===0)continue;let T=M(R),S=d.filter(q=>q!==0&&q!==R&&T.every(I=>e.factors[I][q-1]>0)),A=p(new Set(d.filter(q=>q!==R&&!S.includes(q)))),k=ac(e,A,p(new Set([R])),s,E);g.push(e.termLabels[R-1]),x.push(k)}}return x};w=y(l),l===0&&e.weights!==void 0&&w.some(E=>!Number.isFinite(E.ss))&&(l=no(i),w=y(l));let v=E=>_(E,2*s+l),N=_(c,-l),b=N/o,h=w.map(({ss:E,df:x})=>{let M=E/x/b;return[v(E),x,M,we(M,x,o,!1,!1)]});return h.push([v(N),o,NaN,NaN]),j("Anova",{colNames:["Sum Sq","Df","F value","Pr(>F)"],rowNames:[...g,"Residuals"],table:h,warnings:m},e)}var so=[0,0];var bs=e=>e.map(t=>t.map(n=>n[0]));function ic(e){let t=e[0]?.length??0;return Array.from({length:t},(n,r)=>e.map(o=>o[r]))}function on(e,t){let n=t[0]?.length??0;return e.map(r=>{let o=new Array(n).fill(so);return r.forEach((s,c)=>{let i=t[c];for(let a=0;a<n;a++)o[a]=Q(o[a],pe(s,i[a]))}),o})}function sn(e){let t=so;return e.forEach((n,r)=>t=Q(t,n[r])),t}function cc(e){let t=e.length,n=e.map(c=>c.slice()),r=Array.from({length:t},(c,i)=>i),o=1,s=!1;for(let c=0;c<t;c++){let i=c;for(let a=c+1;a<t;a++)Math.abs(n[a][c][0])>Math.abs(n[i][c][0])&&(i=a);if(n[i][c][0]===0){s=!0;continue}i!==c&&([n[c],n[i]]=[n[i],n[c]],[r[c],r[i]]=[r[i],r[c]],o=-o);for(let a=c+1;a<t;a++){let l=Ae(n[a][c],n[c][c]);n[a][c]=l;for(let f=c+1;f<t;f++)n[a][f]=de(n[a][f],pe(l,n[c][f]))}}return{a:n,perm:r,sign:o,singular:s}}function lc(e){let{a:t,sign:n,singular:r}=cc(e);if(r)return so;let o=[n,0];return t.forEach((s,c)=>o=pe(o,s[c])),o}function fc(e){let t=e.length,{a:n,perm:r,singular:o}=cc(e);if(o)throw new Error("singular matrix");let s=Array.from({length:t},()=>new Array(t).fill(so));for(let c=0;c<t;c++){let i=r.map(a=>[a===c?1:0,0]);for(let a=0;a<t;a++)for(let l=0;l<a;l++)i[a]=de(i[a],pe(n[a][l],i[l]));for(let a=t-1;a>=0;a--){for(let l=a+1;l<t;l++)i[a]=de(i[a],pe(n[a][l],i[l]));i[a]=Ae(i[a],n[a][a])}for(let a=0;a<t;a++)s[a][c]=i[a]}return s}var eu=["group","idata","M","X"],mc=(e,t)=>xe(e,t/2,2,!1,!1);function Ns(e){let t=e.length,n=Tt(sn(e),t);return lc(e.map(r=>r.map(o=>Ae(o,n))))[0]}function vs(e,t,n,r){let o=1-(2*t*t+t+2)/(6*t*n),s=(t+2)*(t-1)*(t-2)*(2*t**3+6*t*t+3*r+2)/(288*(n*t*o)**2),c=-n*o*Math.log(e),i=t*(t+1)/2-1,a=mc(c,i),l=mc(c,i+4);return a+s*(l-a)}var tu=(e,t)=>Array.from({length:t},(n,r)=>e.map(o=>o[r]));function nu(e,t,n){let r=e.length,o=y=>y.length===0?0:ve(Array.from({length:r},(v,N)=>y.map(b=>b[N]))).rank,s=n.length===0?void 0:ve(Array.from({length:r},(y,v)=>n.map(N=>N[v]))),c=s===void 0?[]:s.pivot.slice(0,s.rank).map(y=>n[y]),i=c.length,a=[...c,...t],l=ve(Array.from({length:r},(y,v)=>a.map(N=>N[v])));if(l.rank>o(t))throw new Error("X does not define a subspace of M");let f=l.pivot.slice(0,l.rank).map(y=>a[y]),m=y=>y.map(v=>[v,0]),u=new Array(r).fill([0,0]),p=qt(f.map(m),u,e.map((y,v)=>e.map(N=>N[v]))).qtv,d=Array.from({length:r},(y,v)=>p.map(N=>N[v])),g=qt(f.map(m),u,d).qtv,w=Array.from({length:l.rank-i},(y,v)=>i+v);return w.map(y=>w.map(v=>g[y][v]))}function ru(e,t={}){C(t,eu,"mauchlyTest");let n=e.map(h=>Array.from(h)),r=n[0]?.length??0;if(r===0||n.some(h=>h.length!==r))throw new TypeError("y must be a matrix with at least one column");let o=t.group;if(o!==void 0&&o.length!==n.length)throw new RangeError("'group' must have one label per row of y");let s=h=>h==null||typeof h=="number"&&Number.isNaN(h),c=n.map((h,E)=>!h.some(s)&&(o===void 0||!s(o[E])));if(n.some((h,E)=>c[E]&&h.some(x=>!Number.isFinite(x))))throw new RangeError("y must not contain infinite values");let i=ee(n.filter((h,E)=>c[E])).groups,a=i.length,l=o?.filter((h,E)=>c[E]).map(Pe)??i.map(()=>""),f=[...new Set(l)].sort(it),m=i.map(h=>h.map(()=>[0,0]));for(let h of f){let E=l.flatMap((x,M)=>x===h?[M]:[]);for(let x=0;x<r;x++){let M=[0,0];for(let T of E)M=Q(M,[i[T][x],0]);let R=Tt(M,E.length);for(let T of E)m[T][x]=de([i[T][x],0],R)}}let u=a-f.length;if(u<=0)throw new RangeError(`no residual degrees of freedom: ${a} complete row(s) for ${f.length} group(s)`);let p=Array.from({length:r},(h,E)=>Array.from({length:r},(x,M)=>{let R=[0,0];for(let T of m)R=Q(R,pe(T[E],T[M]));return R})),d=t.idata??{index:Array.from({length:r},(h,E)=>E+1)},g=h=>{let E=Re(()=>Mn(h,d));if(E.x.length!==r)throw new RangeError("idata must have one row per column of y");return tu(E.x,E.colNames.length)},w=t.M===void 0?Array.from({length:r},(h,E)=>Array.from({length:r},(x,M)=>M===E?1:0)):g(t.M),y=g(t.X??"~0"),v=nu(p,w,y),N=v.length;if(N===0)throw new RangeError("X spans M: the test has no dimensions");if(N===1)throw new RangeError("one tested dimension: sphericity holds trivially and the test has no degrees of freedom");if(u<N)throw new RangeError(`too few residual degrees of freedom for the test: ${u} for ${N} dimension(s)`);let b=Ns(v);return{statistic:b,statisticName:"W",pValue:vs(b,N,u,r),method:"Mauchly's test of sphericity",warnings:[]}}var ou=new Set(["n","delta","sd","sigLevel","power","type","alternative","strict","tol"]),su=["n","p1","p2","sigLevel","power","alternative","strict","tol"],au=["groups","n","betweenVar","withinVar","sigLevel","power"],pc=Number.EPSILON**.25;function be(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 G=e=>e==null;function et(e,t,n){if(!G(e)&&!t(e))throw new RangeError(n)}var cr=e=>e>=0&&e<=1,ao=e=>e>0&&e<1/0;function ft(e,t){if(e.filter(G).length!==1)throw new RangeError(t)}function oe(e,t,n,r={}){return wa(e,t,n,r).root}var an=(e,t,n,r)=>Sr(e,t,{ncp:n,lowerTail:r});function ys(e,t){if(e!=null&&!(typeof e=="number"&&e>=0&&e<=1))throw new RangeError(t)}var Ms="'sig.level' must be numeric in [0, 1]";function iu(e={}){C(e,ou,"powerTTest");let{n:t,delta:n,power:r}=e,o=e.sd===void 0?1:e.sd,s=e.sigLevel===void 0?.05:e.sigLevel;ft([t,n,o,r,s],"exactly one of 'n', 'delta', 'sd', 'power', and 'sig.level' must be NULL"),ys(s,Ms),et(r,cr,"'power' must be a probability"),et(o,ao,"'sd' must be a positive finite number"),et(t,g=>g>=2,"number of observations in each group must be at least 2"),et(n,g=>!Number.isNaN(g),"'delta' must not be NaN"),et(e.tol,ao,"'tol' must be a positive finite number");let c=be(e.type,["two.sample","one.sample","paired"]),i=be(e.alternative,["two.sided","one.sided"]),a=e.strict??!1,l=e.tol??pc,f=c==="two.sample"?2:1,m=i==="two.sided"?2:1;m===2&&!G(n)&&(n=Math.abs(n));let u=(g,w,y,v)=>{let N=Math.max(1e-7,g-1)*f,b=Xt(v/m,N,{lowerTail:!1}),h=Math.sqrt(g/f)*w/y;Number.isFinite(h)||(h=Math.sqrt(g/f)*(w/y));let E=an(b,N,h,!1);return a&&m===2?E+an(-b,N,h,!0):E};if(G(r))r=u(t,n,o,s);else{let g=r;if(G(t)){if(n===0)throw new RangeError("'n' cannot be determined when 'delta' is 0: the power does not depend on it");t=oe(w=>u(w,n,o,s)-g,2,1e7,{tol:l,extendInt:"upX"})}else if(G(o)){if(n===0)throw new RangeError("'sd' cannot be determined when 'delta' is 0: the power does not depend on it");let w=Math.abs(n);o=oe(y=>u(t,n,y,s)-g,w*1e-7,w*1e7,{tol:l,extendInt:n>0?"downX":"upX"})}else G(n)?n=oe(w=>u(t,w,o,s)-g,o*1e-7,o*1e7,{tol:l,extendInt:"upX"}):s=oe(w=>u(t,n,o,w)-g,1e-10,1-1e-10,{tol:l,extendInt:"yes"})}let p=c==="paired"?"n is number of *pairs*, sd is std.dev. of *differences* within pairs":c==="two.sample"?"n is number in *each* group":void 0,d={"two.sample":"Two-sample","one.sample":"One-sample",paired:"Paired"}[c]+" t test power calculation";return{n:t,delta:n,sd:o,sigLevel:s,power:r,alternative:i,...p===void 0?{}:{note:p},method:d,warnings:[]}}function cu(e={}){C(e,su,"powerPropTest");let{n:t,p1:n,p2:r,power:o}=e,s=e.sigLevel===void 0?.05:e.sigLevel;ft([t,n,r,o,s],"exactly one of 'n', 'p1', 'p2', 'power', and 'sig.level' must be NULL"),ys(s,Ms),et(o,cr,"'power' must be a probability"),et(n,cr,"'p1' must be a probability"),et(r,cr,"'p2' must be a probability"),et(t,u=>u>0,"number of observations in each group must be positive"),et(e.tol,ao,"'tol' must be a positive finite number");let c=be(e.alternative,["two.sided","one.sided"]),i=e.strict??!1,a=e.tol??pc,l=c==="two.sided"?2:1,f=(u,p,d,g)=>{let w=Z(g/l,{lowerTail:!1});if(i&&l===2){let y=(p+d)/2,v=p*(1-p),N=d*(1-d),b=y*(1-y),h=Math.abs(p-d),E=Math.sqrt(v+N);return Y((Math.sqrt(u)*h-w*Math.sqrt(2*b))/E)+Y((Math.sqrt(u)*h+w*Math.sqrt(2*b))/E,{lowerTail:!1})}return Y((Math.sqrt(u)*Math.abs(p-d)-w*Math.sqrt((p+d)*(1-(p+d)/2)))/Math.sqrt(p*(1-p)+d*(1-d)))},m=[];if(G(o))o=f(t,n,r,s);else{let u=o;if(G(t)){if(n===r)throw new RangeError("'n' cannot be determined when 'p1' equals 'p2': the power does not depend on it");t=oe(p=>f(p,n,r,s)-u,1,1e7,{tol:a,extendInt:"upX"})}else if(G(n)){if(r===0)throw new RangeError("'p1' cannot be determined when 'p2' is 0: no p1 in [0, p2] differs from p2");n=oe(p=>f(t,p,r,s)-u,0,r,{tol:a,extendInt:"yes"}),n<0&&m.push("No p1 in [0, p2] can be found to achieve the desired power")}else if(G(r)){if(n===1)throw new RangeError("'p2' cannot be determined when 'p1' is 1: no p2 in [p1, 1] differs from p1");r=oe(p=>f(t,n,p,s)-u,n,1,{tol:a,extendInt:"yes"}),r>1&&m.push("No p2 in [p1, 1] can be found to achieve the desired power")}else s=oe(p=>f(t,n,r,p)-u,1e-10,1-1e-10,{tol:a,extendInt:"upX"}),(s<0||s>1)&&m.push("No significance level [0, 1] can be found to achieve the desired power")}return j("powerPropTest",{n:t,p1:n,p2:r,sigLevel:s,power:o,alternative:c,note:"n is number in *each* group",method:"Two-sample comparison of proportions power calculation",warnings:m})}var uc=1e16,io=(e,t)=>e>uc&&e<1/0&&t>uc;function co(e,t,n,r){let o=Z(r,{lowerTail:!1}),s=n/e,c=Math.sqrt(2/e+2/t),i=Math.hypot(Math.sqrt(2*(1+2*s)/e),Math.sqrt(2/t)*(1+s));return Y((o*c-s)/i,{lowerTail:!1})}function lu(e,t,n,r){if(n===0)return 0;let o=l=>Math.floor(Math.log2(l)),[s,c,i,a]=[o(e),o(t),o(n),o(r)];return _(_(e,-s)*_(t,-c)*_(n,-i)/_(r,-a),s+c+i-a)}function fu(e={}){C(e,au,"powerAnovaTest");let{groups:t,n,betweenVar:r,withinVar:o,power:s}=e,c=e.sigLevel===void 0?.05:e.sigLevel;if(ft([t,n,r,o,s,c],"exactly one of 'groups', 'n', 'between.var', 'within.var', 'power', and 'sig.level' must be NULL"),!G(t)&&!(t>=2))throw new RangeError("number of groups must be at least 2");if(!G(n)&&!(n>=2))throw new RangeError("number of observations in each group must be at least 2");ys(c,Ms),et(s,cr,"'power' must be a probability"),et(r,a=>a>=0&&a<1/0,"'betweenVar' must be a nonnegative finite number"),et(o,ao,"'withinVar' must be a positive finite number");let i=(a,l,f,m,u)=>{let p=(a-1)*l*f/m;p<1/0||(p=(a-1)*l*(f/m)),!(p<1/0)&&Number.isFinite(a)&&Number.isFinite(l)&&(p=lu(a-1,l,f,m));let d=a-1,g=(l-1)*a;return p===1/0?1:io(d,g)?co(d,g,p,u):Gt(Pt(u,d,g,{lowerTail:!1}),d,g,{ncp:p,lowerTail:!1})};if(G(s))s=i(t,n,r,o,c);else{let a=s;if(G(t))t=oe(l=>i(l,n,r,o,c)-a,2,100);else if(G(n)){if(r===0)throw new RangeError("'n' cannot be determined when 'betweenVar' is 0: the power does not depend on it");n=oe(l=>i(t,l,r,o,c)-a,2,1e5)}else if(G(o)){if(r===0)throw new RangeError("'withinVar' cannot be determined when 'betweenVar' is 0: the power does not depend on it");o=oe(l=>i(t,n,r,l,c)-a,r*1e-7,r*1e7)}else G(r)?r=oe(l=>i(t,n,l,o,c)-a,o*1e-7,o*1e7):c=oe(l=>i(t,n,r,o,l)-a,1e-10,1-1e-10)}return{groups:t,n,betweenVar:r,withinVar:o,sigLevel:c,power:s,note:"n is number in each group",method:"Balanced one-way analysis of variance power calculation",warnings:[]}}var mu=["n","d","type","sigLevel","power","alternative"],uu=["n1","n2","d","sigLevel","power","alternative"],pu=["h","n","sigLevel","power","alternative"],hu=["h","n1","n2","sigLevel","power","alternative"],du=["h","n","sigLevel","power","alternative"],gu=["d","n","sigLevel","power","alternative"],wu=["n","r","sigLevel","power","alternative"],bu=["w","N","df","sigLevel","power"],Nu=["k","n","f","sigLevel","power"],vu=["u","v","f2","sigLevel","power"],yu=["size"],Mu={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 dc(e="p",...t){C(Nn(t),yu,"cohenES");let n=bn(["size"],t).size??"small",r=be(e,["p","t","r","anov","chisq","f2"]),o=be(n,["small","medium","large"]);return{test:r,size:o,effectSize:Mu[r][["small","medium","large"].indexOf(o)],method:"Conventional effect size from Cohen (1982)"}}function Eu(e,t){if(!lo(e)||!lo(t))throw new RangeError("p1 and p2 must be probabilities in [0, 1]");return 2*Math.asin(Math.sqrt(e))-2*Math.asin(Math.sqrt(t))}function xu(e,t){if(e.length!==t.length)throw new RangeError("'P0' and 'P1' must have the same length");if(![...e,...t].every(lo))throw new RangeError("'P0' and 'P1' must be probabilities in [0, 1]");let n=0;for(let r=0;r<e.length;r++)(e[r]!==0||t[r]!==0)&&(n+=Oe(t[r]-e[r],e[r]));return Math.sqrt(n)}function Ru(e){if(e.length===0||e[0].length===0||e.some(s=>s.length!==e[0].length))throw new RangeError("'P' must be a non-empty matrix (rows of the same length)");if(!e.every(s=>s.every(lo)))throw new RangeError("'P' must contain probabilities in [0, 1]");let t=e.map(s=>s.reduce((c,i)=>c+i,0)),n=e[0]?.length??0,r=Array.from({length:n},(s,c)=>e.reduce((i,a)=>i+a[c],0)),o=0;for(let s=0;s<n;s++)for(let c=0;c<e.length;c++){let i=t[c]*r[s];if(!(i===0&&e[c][s]===0))if(i>=Tu)o+=Oe(e[c][s]-i,i);else{let a=Math.sqrt(t[c]),l=Math.sqrt(r[s]),f=e[c][s]/a/l-a*l;o+=f*f}}return Math.sqrt(o)}var Tu=2**-1022,lo=e=>e>=0&&e<=1,Su="\u2018sig.level\u2019 must be numeric in [0, 1]",Au="\u2018power\u2019 must be numeric in [0, 1]";function $t(e,t){let n=r=>!G(r)&&!(typeof r=="number"&&r>=0&&r<=1);if(n(e))throw new RangeError(Su);if(n(t))throw new RangeError(Au)}function Wt(e,t){if(typeof e=="number"&&Number.isNaN(e))throw new RangeError("the effect size must not be NaN");return typeof e=="string"?dc(t,e).effectSize:e}var lr=["two.sided","less","greater"];function fo(e,t){return e==="two.sided"?t:e==="less"?[-10,5]:[-5,10]}function gc(e,t,n,r){if(r==="less")return an(Xt(n,e),e,t,!0);if(r==="greater")return an(Xt(n,e,{lowerTail:!1}),e,t,!1);let o=Xt(n/2,e,{lowerTail:!1});return an(o,e,t,!1)+an(-o,e,t,!0)}var hc=e=>e<1e-8?Z(e,{lowerTail:!1}):Z(1-e);function wc(e,t,n){return n==="greater"?Y(hc(t)-e,{lowerTail:!1}):n==="less"?Y(Z(t)-e):Y(hc(t/2)-e,{lowerTail:!1})+Y(Z(t/2)-e)}function ku(e){let t=1,n=0;for(let r of e){if(r===0)return 0;let o=Math.floor(Math.log2(Math.abs(r)));t*=_(r,-o),n+=o}return _(t,n)}function qu(e={}){C(e,mu,"pwrTTest");let{n:t,power:n}=e,r=Wt(e.d,"t"),o=e.sigLevel===void 0?.05:e.sigLevel;if(ft([t,r,n,o],"exactly one of n, d, power, and sig.level must be NULL"),$t(o,n),!G(t)&&!(t>=2))throw new RangeError("number of observations in each group must be at least 2");let s=be(e.type,["two.sample","one.sample","paired"]),c=be(e.alternative,lr),i=s==="two.sample"?2:1;c==="two.sided"&&!G(r)&&(r=Math.abs(r));let a=(m,u,p)=>gc((m-1)*i,Math.sqrt(m/i)*u,p,c);if(G(n))n=a(t,r,o);else{let m=n;G(t)?t=oe(u=>a(u,r,o)-m,2+1e-10,1e9):G(r)?r=oe(u=>a(t,u,o)-m,...fo(c,[1e-7,10])):o=oe(u=>a(t,r,u)-m,1e-10,1-1e-10)}let l=s==="paired"?"n is number of *pairs*":s==="two.sample"?"n is number in *each* group":void 0,f={"two.sample":"Two-sample","one.sample":"One-sample",paired:"Paired"}[s]+" t test power calculation";return{n:t,d:r,sigLevel:o,power:n,alternative:c,...l===void 0?{}:{note:l},method:f,warnings:[]}}function _u(e={}){C(e,uu,"pwrT2nTest");let{n1:t,n2:n,power:r}=e,o=Wt(e.d,"t"),s=e.sigLevel===void 0?.05:e.sigLevel;if(ft([t,n,o,r,s],"exactly one of n1, n2, d, power, and sig.level must be NULL"),$t(s,r),!G(t)&&!(t>=2))throw new RangeError("number of observations in the first group must be at least 2");if(!G(n)&&!(n>=2))throw new RangeError("number of observations in the second group must be at least 2");let c=be(e.alternative,lr);c==="two.sided"&&!G(o)&&(o=Math.abs(o));let i=(a,l,f,m)=>gc(a+l-2,f/Math.sqrt(1/a+1/l),m,c);if(G(r))r=i(t,n,o,s);else{let a=r;G(t)?t=oe(l=>i(l,n,o,s)-a,2+1e-10,1e9):G(n)?n=oe(l=>i(t,l,o,s)-a,2+1e-10,1e9):G(o)?o=oe(l=>i(t,n,l,s)-a,...fo(c,[1e-7,10])):s=oe(l=>i(t,n,o,l)-a,1e-10,1-1e-10)}return{n1:t,n2:n,d:o,sigLevel:s,power:r,alternative:c,method:"t test power calculation",warnings:[]}}function Es(e,t,n,r,o,s){let{n:c,power:i}=t,a=Wt(e,n==="h"?"p":"t"),l=t.sigLevel===void 0?.05:t.sigLevel;if(ft([a,c,i,l],`exactly one of ${n}, n, power, and sig.level must be NULL`),$t(l,i),!G(c)&&!(c>=1))throw new RangeError("number of observations in each group must be at least 1");let f=be(t.alternative,lr);f==="two.sided"&&!G(a)&&(a=Math.abs(a));let m=(u,p,d)=>wc(u*r(p),d,f);if(G(i))i=m(a,c,l);else{let u=i;G(a)?a=oe(p=>m(p,c,l)-u,...fo(f,[s,10])):G(c)?c=oe(p=>m(a,p,l)-u,o,1e9):l=oe(p=>m(a,c,p)-u,1e-10,1-1e-10)}return{es:a,n:c,sigLevel:l,power:i,alternative:f}}function Iu(e={}){C(e,pu,"pwr2pTest");let t=Es(e.h,e,"h",n=>Math.sqrt(n/2),2+1e-10,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",warnings:[]}}function Lu(e={}){C(e,du,"pwrPTest");let t=Es(e.h,e,"h",n=>Math.sqrt(n),2+1e-10,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)",warnings:[]}}function Ou(e={}){C(e,gu,"pwrNormTest");let t=Es(e.d,e,"d",n=>Math.sqrt(n),1+1e-10,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",warnings:[]}}function Fu(e={}){C(e,hu,"pwr2p2nTest");let{n1:t,n2:n,power:r}=e,o=Wt(e.h,"p"),s=e.sigLevel===void 0?.05:e.sigLevel;if(ft([o,t,n,r,s],"exactly one of h, n1, n2, power, and sig.level must be NULL"),$t(s,r),!G(t)&&!(t>=2))throw new RangeError("number of observations in the first group must be at least 2");if(!G(n)&&!(n>=2))throw new RangeError("number of observations in the second group must be at least 2");let c=be(e.alternative,lr);c==="two.sided"&&!G(o)&&(o=Math.abs(o));let i=(l,f)=>{let m=l*f;if(Number.isFinite(m)||!Number.isFinite(l)||!Number.isFinite(f))return m/(l+f);let u=Math.min(l,f);return u/(1+u/Math.max(l,f))},a=(l,f,m,u)=>wc(l*Math.sqrt(i(f,m)),u,c);if(G(r))r=a(o,t,n,s);else{let l=r;G(o)?o=oe(f=>a(f,t,n,s)-l,...fo(c,[1e-10,10])):G(t)?t=oe(f=>a(o,f,n,s)-l,2+1e-10,1e9):G(n)?n=oe(f=>a(o,t,f,s)-l,2+1e-10,1e9):s=oe(f=>a(o,t,n,f)-l,1e-10,1-1e-10)}return{h:o,n1:t,n2:n,sigLevel:s,power:r,alternative:c,method:"difference of proportion power calculation for binomial distribution (arcsine transformation)",note:"different sample sizes",warnings:[]}}function Pu(e={}){C(e,wu,"pwrRTest");let{n:t,power:n}=e,r=Wt(e.r,"r"),o=e.sigLevel===void 0?.05:e.sigLevel;if(ft([t,r,n,o],"exactly one of n, r, power, and sig.level must be NULL"),$t(o,n),!G(t)&&!(t>=4))throw new RangeError("number of observations must be at least 4");if(!G(r)&&!(Math.abs(r)<=1))throw new RangeError("'r' must be in [-1, 1]");let s=be(e.alternative,lr);s==="two.sided"&&!G(r)&&(r=Math.abs(r));let c=(a,l,f,m)=>{let u=Xt(m?f/2:f,a-2,{lowerTail:!1}),p=u**2+a-2,d=p===1/0&&Number.isFinite(u)?1/Math.sqrt(1+(a-2)/u/u):Math.sqrt(u**2/p),g=Math.atanh(l)+l/(2*(a-1)),w=Math.atanh(d),y=Y((g-w)*Math.sqrt(a-3));return m?y+Y((-g-w)*Math.sqrt(a-3)):y},i=(a,l,f)=>s==="two.sided"?c(a,l,f,!0):c(a,s==="less"?-l:l,f,!1);if(G(n))n=i(t,r,o),s==="less"&&(r=-r);else{let a=n;if(G(t))t=oe(l=>i(l,r,o)-a,4+1e-10,1e9);else if(G(r)){let[l,f]=s==="two.sided"?[1e-10,.9999999999]:[-.9999999999,.9999999999];r=oe(m=>i(t,m,o)-a,l,f)}else o=oe(l=>i(t,r,l)-a,1e-10,1-1e-10)}return{n:t,r,sigLevel:o,power:n,alternative:s,method:"approximate correlation power calculation (arctangh transformation)",warnings:[]}}function Du(e){C(e,bu,"pwrChisqTest");let{N:t,power:n}=e,{df:r}=e,o=Wt(e.w,"chisq"),s=e.sigLevel===void 0?.05:e.sigLevel;if(ft([o,t,n,s],"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(!(r>0&&r<1/0))throw new RangeError("'df' must be a positive finite number");if($t(s,n),!G(o)&&!(o>=0))throw new RangeError("w must be positive");if(!G(t)&&!(t>=1))throw new RangeError("number of observations must be at least 1");let c=(i,a,l)=>{let f=a*i**2;return f===1/0?1:St(Xs(l,r,{lowerTail:!1}),r,{ncp:f,lowerTail:!1})};if(G(n))n=c(o,t,s);else{let i=n;G(o)?o=oe(a=>c(a,t,s)-i,1e-10,1e9):G(t)?t=oe(a=>c(o,a,s)-i,1+1e-10,1e9):s=oe(a=>c(o,t,a)-i,1e-10,1-1e-10)}return{w:o,N:t,df:r,sigLevel:s,power:n,method:"Chi squared power calculation",note:"N is the number of observations",warnings:[]}}function Cu(e={}){C(e,Nu,"pwrAnovaTest");let{k:t,n,power:r}=e,o=Wt(e.f,"anov"),s=e.sigLevel===void 0?.05:e.sigLevel;if(ft([t,n,o,r,s],"exactly one of k, n, f, power, and sig.level must be NULL"),$t(s,r),!G(o)&&!(o>=0))throw new RangeError("f must be positive");if(!G(t)&&!(t>=2))throw new RangeError("number of groups must be at least 2");if(!G(n)&&!(n>=2))throw new RangeError("number of observations in each group must be at least 2");let c=(i,a,l,f)=>{let m=i-1,u=(a-1)*i,p=i*a*l**2;return!Number.isFinite(p)&&Number.isFinite(i)&&Number.isFinite(a)&&Number.isFinite(l)&&(p=ku([i,a,l,l])),p===1/0?1:io(m,u)?co(m,u,p,f):Gt(Pt(f,m,u,{lowerTail:!1}),m,u,{ncp:p,lowerTail:!1})};if(G(r))r=c(t,n,o,s);else{let i=r;G(t)?t=oe(a=>c(a,n,o,s)-i,2+1e-10,100):G(n)?n=oe(a=>c(t,a,o,s)-i,2+1e-10,1e9):G(o)?o=oe(a=>c(t,n,a,s)-i,1e-7,1e7):s=oe(a=>c(t,n,o,a)-i,1e-10,1-1e-10)}return{k:t,n,f:o,sigLevel:s,power:r,note:"n is number in each group",method:"Balanced one-way analysis of variance power calculation",warnings:[]}}function Vu(e={}){C(e,vu,"pwrF2Test");let{u:t,v:n,power:r}=e,o=Wt(e.f2,"f2"),s=e.sigLevel===void 0?.05:e.sigLevel;if(ft([t,n,o,r,s],"exactly one of u, v, f2, power, and sig.level must be NULL"),$t(s,r),!G(o)&&!(o>=0))throw new RangeError("f2 must be positive");if(!G(t)&&!(t>=1))throw new RangeError("degree of freedom u for numerator must be at least 1");if(!G(n)&&!(n>=1))throw new RangeError("degree of freedom v for denominator must be at least 1");let c=(i,a,l,f)=>{let m=l*(i+a+1);return!Number.isFinite(m)&&Number.isFinite(i)&&Number.isFinite(a)&&Number.isFinite(l)&&(m=2*(l*(i/2+a/2+.5))),m===1/0?1:io(i,a)?co(i,a,m,f):Gt(Pt(f,i,a,{lowerTail:!1}),i,a,{ncp:m,lowerTail:!1})};if(G(r))r=c(t,n,o,s);else{let i=r;G(t)?t=oe(a=>c(a,n,o,s)-i,1+1e-10,100):G(n)?n=oe(a=>c(t,a,o,s)-i,1+1e-10,1e9):G(o)?o=oe(a=>c(t,n,a,s)-i,1e-7,1e7):s=oe(a=>c(t,n,o,a)-i,1e-10,1-1e-10)}return{u:t,v:n,f2:o,sigLevel:s,power:r,method:"Multiple regression power calculation",warnings:[]}}var zu=["pooledSd","mu","paired","ci","alternative","adjust"],$u=["pooledSd","mu","paired","ci","alternative"],Wu=["generalized","partial","ci","alternative"],ju=["partial","ci","alternative"],Ku=["partial","ci","alternative"];function Ts(e){if(e!==null&&!(e>0&&e<1))throw new RangeError("ci must be a single numeric value between (0, 1)")}var Uu="Missing values detected. NAs dropped.",Yu=`'y' is numeric but has only 2 unique values.
7
- If this is a grouping variable, convert it to a factor.`,yc={tol:0,maxiter:5e3};function Gu(e){return e===1/0?1:Math.exp(Bs(.5,(e-1)/2)-.5*Math.log(e/2))}function bc(e,t,n){let r=e-1<e+1?1:Math.abs(e)*Number.EPSILON;return Bn(o=>Sr(e,t,{ncp:o})-n,e-r,e+r,{...yc,extendInt:"downX"}).root}function Mc(e,t,n,r){let o=n.pooledSd??!0,s=n.mu??0,c=n.paired??!1,i=n.ci===void 0?.95:n.ci;Ts(i);let a=be(n.alternative,["two.sided","less","greater"]),l=Array.from(e),f=t===void 0?void 0:Array.from(t),m=[];f!==void 0&&new Set(Ge(f)).size===2&&m.push(Yu),(l.some(Number.isNaN)||(f?.some(Number.isNaN)??!1))&&m.push(Uu);let u=S=>S===1/0||S===-1/0;if(l.some(u)||(f?.some(u)??!1))throw new RangeError("the observations must be finite (NaN is dropped as missing)");let p,d,g,w,y,v;if(f===void 0||c){let S;if(f===void 0)({groups:[S],k:v}=ee([Ge(l)]));else{let[k,q]=pn(l,f);({d:S,k:v}=xr(k,q))}let A=S.length;if(A<2)throw new RangeError(f===void 0?"not enough observations: at least 2 are needed":"not enough complete pairs: at least 2 are needed");p=ke(S),d=Math.sqrt(qe(S)),g=d/Math.sqrt(A),w=1/A,y=A-1}else{let S=_e([Ge(l),Ge(f)]);v=S.k;let[A,k]=S.n;if(A<2||k<2)throw new RangeError("not enough observations: at least 2 are needed in each group");p=S.mean[0]-S.mean[1];let q=Math.sqrt(S.variance[0]),I=Math.sqrt(S.variance[1]);o?(d=Math.sqrt(((A-1)*q**2+(k-1)*I**2)/(A+k-2)),g=d*Math.sqrt(1/A+1/k),w=1/A+1/k,y=A+k-2):(d=Math.sqrt((q**2+I**2)/2),w=2*(k*q**2+A*I**2)/(A*k*(q**2+I**2)),g=Math.sqrt(Math.sqrt(q**2/A)**2+Math.sqrt(I**2/k)**2),y=Rr([q**2/A,I**2/k],[A-1,k-1]))}let N=r?Gu(y):1,b=_(s,-v),h=(p-b)/d*N,E=r?"Hedges_g":"Cohens_d";if(i===null)return{name:E,value:h,warnings:m};let x=a==="two.sided"?i:2*i-1,M=(p-b)/g,R=bc(M,y,(1+x)/2)*Math.sqrt(w)*N,T=bc(M,y,(1-x)/2)*Math.sqrt(w)*N;return a==="less"&&(R=-1/0),a==="greater"&&(T=1/0),{name:E,value:h,ci:i,ciLow:R,ciHigh:T,warnings:m}}function Hu(e,t,n={}){return C(n,zu,"cohensD"),j("cohensD",Mc(e,t,n,n.adjust??!1))}function Xu(e,t,n={}){return C(n,$u,"hedgesG"),j("hedgesG",Mc(e,t,n,!0))}var Bu=e=>Array.isArray(e.effects),xs=e=>e.multistratum===!0,Ec=e=>Array.isArray(e.strata),xc=e=>{let t=e;return typeof t.univariate=="object"&&typeof t.anova=="object"},Rs=e=>({test:t=>t.split(":").includes(e)});function Qu(e){let t=e.strata.filter(o=>o.stratum!=="Error: (Intercept)").map(o=>{let s=o.stratum.replace(/^Error: /,""),c=o.colNames.indexOf("Df"),i=o.colNames.indexOf("Sum Sq"),a=o.rowNames.map((f,m)=>({name:f,df:o.table[m][c],ss:o.table[m][i]})),l=a.find(f=>f.name==="Residuals");if(l===void 0)throw new Error(`the error stratum '${s}' has no residual degrees of freedom`);return{name:s,terms:a.filter(f=>f!==l),sse:l.ss,dfe:l.df}});if(t.length===0)throw new TypeError("the fit has no error strata");let n=t[0].name.split(":")[0],r=t.find(o=>o.name===n);if(r===void 0)throw new Error(`there is no error stratum '${n}'`);return{strata:t,subject:r}}function Zu(e){let t=e.univariate,n=t.colNames.indexOf("Sum Sq"),r=[],o=(a,l,f)=>{let m=r.find(u=>u.name===a);return m===void 0&&r.push(m={name:a,terms:[],sse:l,dfe:f}),m},s=new Map;if(t.colNames.includes("Error SS")){let a=t.colNames.indexOf("num Df"),l=t.colNames.indexOf("Error SS"),f=t.colNames.indexOf("den Df");t.rowNames.forEach((m,u)=>{let p=t.table[u],d=e.within.filter(y=>Rs(y).test(m)),g=o(d.length===0?"Subject":`Subject:${d.join(":")}`,p[l],p[f]);if(m==="(Intercept)")return;let w={name:m,df:p[a],ss:p[n]};g.terms.push(w),s.set(m,w)})}else{let a=t.colNames.indexOf("Df"),l=t.rowNames.indexOf("Residuals"),f=o("Subject",t.table[l][n],t.table[l][a]);t.rowNames.forEach((m,u)=>{if(m==="(Intercept)"||m==="Residuals")return;let p={name:m,df:t.table[u][a],ss:t.table[u][n]};f.terms.push(p),s.set(m,p)})}let c=r.find(a=>a.name==="Subject");if(c===void 0)throw new Error("the result has no subject stratum");let i=e.anova.rowNames.map(a=>{let l=s.get(a);if(l===void 0)throw new Error(`there is no univariate test of '${a}'`);return l});return{strata:r,subject:c,order:i}}function Rc(e,t){let n=Array.isArray(e)?e:[],r=n.filter(s=>!t.some(c=>Rs(s).test(c.name)));if(r.length>0)throw new RangeError(`Observed variable not in data: ${r.join(", ")}`);let o=s=>n.some(c=>Rs(c).test(s));return{isObserved:o,ssObserved:t.reduce((s,c)=>o(c.name)?s+c.ss:s,0)}}function Nc(e,t,n,r){let o=c=>Gt(e,t,n,{ncp:c})-r,s=o(0);return Number.isNaN(s)?NaN:s>0?e===1/0?1/0:Bn(o,0,Math.max(10,e*t*4),{...yc,extendInt:"downX"}).root:0}function Tc(e,t,n,r,o){let s=o==="two.sided"?r:2*r-1,c=Math.max(0,e),i=c/t/((1-c)/n),a=Nc(i,t,n,(1+s)/2),l=Nc(i,t,n,(1-s)/2);i<=Pt((1-s)/2,t,n)&&(l=0),i<=Pt((1+s)/2,t,n)&&(a=0);let f=a===1/0?1:a/(a+n),m=l===1/0?1:l/(l+n);return o==="less"&&(f=0),o==="greater"&&(m=1),[e,r,f,m]}function Ju(e){let t=ee([e.map(n=>n.ss)]).k;if(t!==0)for(let n of e)n.ss=_(n.ss,-t)}var vc="needs a table of anova() or Anova(), a fit from lm() or aov() with a formula, its aovSummary(), or a result of aovEz()";function Ss(e,t,n){if(typeof e!="object"||e===null)throw new TypeError(vc);if(xs(e)||Ec(e)||xc(e))return ep(e,t,n);let r=Bu(e)?Re(()=>ss(e)):e;if(!Array.isArray(r.colNames)||!Array.isArray(r.table))throw new TypeError(vc);let o=ze.get(r),s=o!==void 0&&!Array.isArray(o)?o:r,c=n.ci===void 0?.95:n.ci;Ts(c);let i=be(n.alternative??"greater",["two.sided","less","greater"]),a=s.colNames.indexOf("Df"),l=s.colNames.indexOf("Sum Sq");if(a<0||l<0)throw new TypeError("the table needs the columns 'Df' and 'Sum Sq'");let f=s.rowNames.map((R,T)=>({name:R,df:s.table[T][a],ss:s.table[T][l]})).filter(R=>R.name!=="(Intercept)");Ju(f);let m=f.find(R=>R.name==="Residuals");if(m===void 0)throw new TypeError("the table has no row 'Residuals'");if(!(m.df>0))throw new Error("the model has no residual degrees of freedom: the effect sizes are not defined");let u=f.filter(R=>R!==m),p=m.df,d=m.ss,g=d/p,w=f.reduce((R,T)=>R+T.ss,0),y=f.reduce((R,T)=>R+T.df,0)+1,v=t==="Eta2"?n.generalized??!1:!1,b=u.length===1?"plain":v!==!1?"generalized":n.partial??!0?"partial":"plain",{isObserved:h,ssObserved:E}=Rc(v,u),x=R=>{let{ss:T,df:S}=R;if(t==="Eta2")return b==="partial"?T/(T+d):b==="plain"?T/w:T/(T+d+E-(h(R.name)?T:0));if(t==="Omega2"){let k=b==="partial"?(T-S*g)/(T+(y-S)*g):(T-S*g)/(w+g);return Math.max(0,k)}let A=b==="partial"?(T-S*g)/(T+d):(T-S*g)/w;return Math.max(0,A)},M=`${t}${b==="plain"?"":`_${b}`}`;return{colNames:c===null?[M]:[M,"CI","CI_low","CI_high"],rowNames:u.map(R=>R.name),table:u.map(R=>c===null?[x(R)]:Tc(x(R),R.df,p,c,i)),warnings:Array.isArray(r.warnings)?[...r.warnings]:[]}}function ep(e,t,n){let r=n.ci===void 0?.95:n.ci;Ts(r);let o=be(n.alternative??"greater",["two.sided","less","greater"]),s=xc(e),c,i,a;if(s){let h=ze.get(e),E=Zu(h!==void 0&&!Array.isArray(h)?{...e,univariate:h}:e);({strata:c,subject:i}=E),a=E.order.map(x=>({term:x,stratum:E.strata.find(M=>M.terms.includes(x))}))}else{let h=xs(e)?Re(()=>as(e)):e,E=ze.get(h);({strata:c,subject:i}=Qu(Array.isArray(E)?{...h,strata:E.map((x,M)=>({...h.strata[M],...x}))}:h)),a=c.flatMap(x=>x.terms.map(M=>({term:M,stratum:x})))}let l=ee([[...c.flatMap(h=>h.terms.map(E=>E.ss)),...c.map(h=>h.sse)]]).k;for(let h of c){h.sse=_(h.sse,-l);for(let E of h.terms)E.ss=_(E.ss,-l)}let f=c.flatMap(h=>h.terms),m=c.reduce((h,E)=>h+E.sse,0),u=f.reduce((h,E)=>h+E.ss,0)+m,p=i.sse/i.dfe,d=t==="Eta2"?n.generalized??!1:!1,g=d!==!1?"generalized":n.partial??!0?"partial":"plain",{isObserved:w,ssObserved:y}=Rc(d,f),v=(h,E)=>{let{ss:x,df:M}=h,R=E.sse/E.dfe;if(t==="Eta2")return g==="partial"?x/(x+E.sse):g==="plain"?x/u:x/(x+m+y-(w(h.name)?x:0));if(t==="Omega2"){let S=E===i?0:1,A=g==="partial"?(x-M*R)/(x+S*E.sse+i.sse+p):(x-M*R)/(u+p);return Math.max(0,A)}let T=g==="partial"?(x-M*R)/(x+E.sse):(x-M*R)/u;return Math.max(0,T)},N=`${t}${g==="plain"?"":`_${g}`}`,b={colNames:r===null?[N]:[N,"CI","CI_low","CI_high"],rowNames:a.map(h=>h.term.name),table:a.map(({term:h,stratum:E})=>{let x=v(h,E);return r===null?[x]:Tc(x,h.df,E.dfe,r,o)}),warnings:[]};return s||(b.groups=a.map(h=>h.stratum.name),b.warnings=[...xs(e)||Ec(e)?e.warnings:[]]),b}function tp(e,t={}){return C(t,Wu,"etaSquared"),j("etaSquared",Ss(e,"Eta2",t),e)}function np(e,t={}){return C(t,ju,"omegaSquared"),j("omegaSquared",Ss(e,"Omega2",t),e)}function rp(e,t={}){return C(t,Ku,"epsilonSquared"),j("epsilonSquared",Ss(e,"Epsilon2",t),e)}var op=["id","dv","between","within","type","correction","es","observed"],mo=e=>e===void 0?[]:typeof e=="string"?[e]:[...e],sp=1e-12,Sc=1e-24;function Ac(e,t){let n=Object.prototype.hasOwnProperty.call(e,t)?e[t]:void 0;if(n===void 0)throw new RangeError(`object '${t}' not found`);return Array.from(n)}var fr=e=>typeof e=="boolean"?e?"TRUE":"FALSE":Pe(e);function ap(e){let t=[...new Set(e)];return t.every(n=>typeof n=="number")?t.sort((n,r)=>n-r).map(fr):[...new Set(t.map(fr))].sort(it)}var kc=(e,t)=>t.split(":").includes(e);function qc(e,t,n,r,o){let s=l=>r.some(f=>kc(f,l));for(let l of r)if(!e.some(f=>kc(l,f.name)))throw new RangeError(`Observed variable not in data: ${l}`);let c=[...t.values()].reduce((l,f)=>l+f,0),i=e.reduce((l,f)=>s(f.name)?l+f.ss:l,0),a=["pes","ges"].filter(l=>n.includes(l));return{colNames:["num Df","den Df","MSE","F",...a,"Pr(>F)"],rowNames:e.map(l=>l.name),table:e.map(l=>{let f=a.map(m=>m==="pes"?l.ss/(l.ss+l.errSS):l.ss/(l.ss+c+i-(s(l.name)?l.ss:0)));return[l.numDf,l.denDf,o(l.errSS/l.denDf),l.f,...f,l.p]})}}function ip(e,t){C(t,op,"aovEz");let n=mo(t.between),r=mo(t.within);if(n.length===0&&r.length===0)throw new Error("Either between or within need to be non-NULL!");for(let[D,U]of[["between",n],["within",r]]){let H=U.filter((me,Le)=>U.indexOf(me)!==Le);if(H.length>0)throw new RangeError(`duplicated factor names in '${D}': ${[...new Set(H)].join(", ")}`)}let o=n.filter(D=>r.includes(D));if(o.length>0)throw new RangeError(`factors in both 'between' and 'within': ${o.join(", ")}`);let s=t.type??3;if(s!==2&&s!==3)throw new RangeError(`invalid type (${String(s)}): use 2 or 3`);let c=t.correction??"GG";if(!["GG","HF","none"].includes(c))throw new RangeError(`invalid 'correction': ${String(c)}`);let i=mo(t.es??"ges");for(let D of i)if(D!=="ges"&&D!=="pes")throw new RangeError(`invalid 'es': ${String(D)}`);let a=mo(t.observed),l=[],f=D=>D==null||typeof D=="number"&&Number.isNaN(D),m=Ac(e,t.dv),u=[t.id,...n,...r].map(D=>Ac(e,D));for(let D of u)if(D.length!==m.length)throw new RangeError("the columns must have the same length");let p=m.map((D,U)=>u.every(H=>!f(H[U]))),d=D=>D.filter((U,H)=>p[H]),g=Fe(d(u[0])).map(fr),w=d(m).map(D=>{if(f(D))return NaN;if(typeof D!="number")throw new TypeError(`the response '${t.dv}' must be numeric`);if(!Number.isFinite(D))throw new RangeError(`the response '${t.dv}' must be finite`);return D}),y=n.map((D,U)=>Fe(d(u[1+U]))),v=r.map((D,U)=>Fe(d(u[1+n.length+U]))),N=g.length;for(let D of[w,...y,...v])if(D.length!==N)throw new RangeError("the columns must have the same length");let b=y.map(D=>ap(D)),h=y.map(D=>D.map(fr)),E=v.map(D=>D.map(fr)),x=E.map(D=>[...new Set(D)]);b.forEach((D,U)=>{if(D.length<2)throw new Error(`Factor "${n[U]}" consists of one level only. Remove factor from model?`)}),x.forEach((D,U)=>{if(D.length<2)throw new Error(`Factor "${r[U]}" consists of one level only. Remove factor from model?`)});let M=[...new Set(g)],R=new Map,T=new Set;if(g.forEach((D,U)=>{let H=JSON.stringify(h.map(Le=>Le[U])),me=R.get(D);me===void 0?R.set(D,H):me!==H&&T.add(D)}),T.size>0)throw new Error(`Following ids are in more than one between subjects condition:
8
- ${M.filter(D=>T.has(D)).join(", ")}`);let S=[[]];for(let D of x)S=S.flatMap(U=>D.map(H=>[...U,H]));let A=D=>JSON.stringify(E.map(U=>U[D])),k=new Map(S.map((D,U)=>[JSON.stringify(D),U])),q=new Set(g.map((D,U)=>A(U)));if(S.some(D=>!q.has(JSON.stringify(D))))throw new Error("Empty cells in within-subjects design (i.e., bad data structure).");let I=new Map,z=new Set;g.forEach((D,U)=>{let H=I.get(D);H===void 0&&I.set(D,H=new Set),H.add(A(U)),Number.isNaN(w[U])&&z.add(D)});let P=M.filter(D=>z.has(D)||I.get(D).size<S.length),V=new Set(P),W=ee([w.filter((D,U)=>!V.has(g[U]))]).k,F=W===0?w:w.map(D=>_(D,-W)),L=D=>_(D,2*W),$=S.length,X=new Map;if(g.forEach((D,U)=>{let H=X.get(D);H===void 0&&X.set(D,H={s:S.map(()=>[0,0]),n:S.map(()=>0)});let me=k.get(A(U));H.s[me]=Q(H.s[me],[F[U],0]),H.n[me]++}),[...X.values()].some(D=>D.n.some(U=>U>1))&&l.push("More than one observation per design cell, aggregating data using `fun_aggregate = mean`.\nTo turn off this warning, pass `fun_aggregate = mean` explicitly."),P.length>0){let D=[...P].sort(it),U=D.length>=10?`${D.slice(0,10).join(", ")}, ... [showing first 10 only]`:D.join(", ");l.push(`Missing values for ${P.length} ID(s), which were removed before analysis:
9
- ${U}`)}let te=M.filter(D=>!P.includes(D)),ne=new Map;g.forEach((D,U)=>ne.has(D)||ne.set(D,U));let ae=te.map(D=>{let U=X.get(D);return U.s.map((H,me)=>Tt(H,U.n[me]))}),Me={};n.forEach((D,U)=>Me[D]=te.map(H=>h[U][ne.get(H)]));let Ie=Object.fromEntries(n.map(D=>[D,"contr.sum"])),$e=Object.fromEntries(n.map((D,U)=>[D,b[U]])),pt="Rank deficient model matrix; insufficient data to estimate full model.";if($===1)return j("aovEz",B());return j("aovEz",J());function B(){let D="y";for(;n.includes(D);)D+="_";let U={...Me,[D]:ae.map(je=>je[0][0])},H=Re(()=>ls(`${D} ~ ${n.length>0?n.join(" * "):"1"}`,U,{contrasts:Ie,levels:$e}));if(H.rank<H.coefNames.length)throw new Error(pt);if(H.dfResidual<=0)throw new RangeError(`no residual degrees of freedom: ${ae.length} subject(s) for ${H.rank} parameter(s)`);let me=Re(()=>ws(H,{type:s}));l.push(...me.warnings.filter(je=>!l.includes(je)));let Le=H.dfResidual,He=me.rowNames.flatMap((je,tt)=>{if(je==="(Intercept)"||je==="Residuals")return[];let[yt,_t,cn,It]=me.table[tt];return[{name:je,ss:yt,numDf:_t,errSS:H.rss,denDf:Le,f:cn,p:It}]}),Xe=me.colNames.indexOf("Sum Sq"),We={anova:qc(He,new Map([[0,H.rss]]),i,a,L),univariate:{colNames:me.colNames,rowNames:me.rowNames,table:me.table.map(je=>je.map((tt,yt)=>yt===Xe?L(tt):tt))},within:r,warnings:l};return ze.set(We,{colNames:me.colNames,rowNames:me.rowNames,table:me.table}),We}function J(){let D=te.length,U=ae.reduce((ie,ce)=>ce.reduce((he,Ue)=>he+Ue[0]*Ue[0],ie),0),H=n.length>0?Re(()=>Mn(`~ ${n.join(" * ")}`,Me,{contrasts:Ie,levels:$e})):{colNames:["(Intercept)"],assign:[0],x:te.map(()=>[1]),termLabels:[],factors:[]},me=H.colNames.length;if(D===0||ve(H.x).rank<me)throw new Error(pt);let Le=D-me;if(Le<=0)throw new RangeError(`no residual degrees of freedom: ${D} subject(s) for ${me} between-subject parameter(s)`);let He=[0,...H.termLabels.map((ie,ce)=>ce+1)],Xe=["(Intercept)",...H.termLabels],We=ie=>H.assign.flatMap((ce,he)=>ie.includes(ce)?[he]:[]),je=ie=>H.factors.flatMap((ce,he)=>ce[ie-1]>0?[he]:[]),tt=ie=>H.x.map(ce=>[ce[ie],0]),yt=new Array(D).fill([0,0]),_t=(ie,ce,he)=>{let{qtv:Ue}=qt([...ie,...ce].map(tt),yt,he);return Ue.map(Cn=>Cn.slice(ie.length,ie.length+ce.length))},cn=ie=>{let{qtv:ce}=qt(H.colNames.map((he,Ue)=>tt(Ue)),yt,ie);return ce.map(he=>he.slice(me))},It=Object.fromEntries(r.map((ie,ce)=>[ie,S.map(he=>he[ce])])),Mt=Re(()=>Mn(`~ ${r.join(" * ")}`,It,{contrasts:Object.fromEntries(r.map(ie=>[ie,"contr.sum"])),levels:Object.fromEntries(r.map((ie,ce)=>[ie,x[ce]]))})),dr=[0,...Mt.termLabels.map((ie,ce)=>ce+1)],Pn=["(Intercept)",...Mt.termLabels],gr=ie=>ie.map(ce=>ie.map(he=>{let Ue=[0,0];return ce.forEach((Cn,ln)=>Ue=Q(Ue,pe(Cn,he[ln]))),Ue})),Dn=[],O=[],K=[],re=[],ue=[],le=[],Ee=[],nt=new Map,Lt=!1;for(let ie of dr){let ce=Mt.assign.flatMap((Ye,Se)=>Ye===ie?[Se]:[]),he=ce.length,Ue=ce.map(Ye=>ae.map(Se=>Se.reduce((ht,rt,$n)=>Q(ht,st(rt,Mt.x[$n][Ye])),[0,0]))),Cn=ce.map(Ye=>ce.map(Se=>[Mt.x.reduce((ht,rt)=>ht+rt[Ye]*rt[Se],0),0])),ln=fc(Cn),yo=gr(cn(Ue)),wr=on(yo,ln),Vn=sn(wr)[0],mt=NaN,zn=NaN,Mo=NaN,Ws=NaN,br=!1;if(he>=2){let Ye=Pn[ie],Se=Ma(bs(ln)).map(Et=>Et.map(Ut=>[Ut,0])),ht=bs(on(on(Se,yo),ic(Se))),rt=ct(ht,{symmetric:!0,onlyValues:!0}).values,$n=rt.reduce((Et,Ut)=>Math.max(Et,Ut),0),ot=Math.max($n*sp,U*Sc),Ze=sn(wr),dt=sn(on(wr,wr));$n>U*Sc&&dt[0]>0&&Number.isFinite(dt[0])?(br=!0,mt=Ae(pe(Ze,Ze),st(dt,he))[0],Le-he*mt>0?zn=((Le+1)*he*mt-2)/(he*(Le-he*mt)):l.push(`The Huynh-Feldt epsilon of ${Ye} is undefined (d - p GG <= 0)${c==="HF"?": corrected with GG":""}`)):l.push(`The error SSP matrix of ${Ye} is 0 (or at the rounding level of the data): no sphericity corrections (the p-values are uncorrected)`),br&&rt.every(Et=>Et>ot)?(Mo=Ns(on(ln,yo)),Ws=vs(Mo,he,Le,$)):br&&l.push(`The error SSP matrix of ${Ye} is singular: no Mauchly test (the epsilons are still defined)`)}for(let Ye=0;Ye<He.length;Ye++){let Se=He[Ye],ht;if(s===3)ht=_t(We(He.filter(Ot=>Ot!==Se)),We([Se]),Ue);else if(Se===0)ht=_t([],We([0]),Ue);else{let Ot=je(Se),Wn=He.filter(xt=>xt!==0&&xt!==Se&&Ot.every(el=>H.factors[el][xt-1]>0));ht=_t(We(He.filter(xt=>xt!==Se&&!Wn.includes(xt))),We([Se]),Ue)}let rt=sn(on(gr(ht),ln))[0],ot=(Se===0?1:We([Se]).length)*he,Ze=Le*he,dt=rt/ot/(Vn/Ze),Et=we(dt,ot,Ze,!1,!1),Ut=ie===0?Xe[Ye]:Se===0?Pn[ie]:`${Xe[Ye]}:${Pn[ie]}`;Dn.push([L(rt),ot,L(Vn),Ze,dt,Et]),O.push([rt,ot,Vn,Ze,dt,Et]),K.push(Ut),nt.set(ie,Vn);let fn={name:Ut,ss:rt,numDf:ot,errSS:Vn,denDf:Ze,f:dt,p:Et};if(br){let Ot=Math.min(1,zn),Wn=we(dt,mt*ot,mt*Ze,!1,!1),xt=we(dt,Ot*ot,Ot*Ze,!1,!1);re.push([mt,Wn,zn,xt]),ue.push([Mo,Ws]),le.push(Ut),c==="GG"?fn={...fn,numDf:mt*ot,denDf:mt*Ze,p:Wn}:c==="HF"&&(zn>1&&(Lt=!0),fn=Number.isNaN(zn)?{...fn,numDf:mt*ot,denDf:mt*Ze,p:Wn}:{...fn,numDf:Ot*ot,denDf:Ot*Ze,p:xt})}ie===0&&Se===0||Ee.push(fn)}}Lt&&l.push("HF eps > 1 treated as 1");let Kt={anova:qc(Ee,nt,i,a,L),univariate:{colNames:["Sum Sq","num Df","Error SS","den Df","F value","Pr(>F)"],rowNames:K,table:Dn},within:r,warnings:l};return re.length>0&&(Kt.corrections={colNames:["GG eps","Pr(>F[GG])","HF eps","Pr(>F[HF])"],rowNames:le,table:re},Kt.sphericity={colNames:["Test statistic","p-value"],rowNames:[...le],table:ue}),ze.set(Kt,{colNames:Kt.univariate.colNames,rowNames:K,table:O}),Kt}}function uo(e){let t=Math.sqrt(2220446049250313e-31),n=Array.from(new Set(e.filter(s=>Number.isFinite(s)))).sort((s,c)=>s-c);if(n.length<2)return e.slice();let r=ke(n.map(Math.abs)),o=[n[0]];for(let s=0;s+1<n.length;s++){let c=n[s+1]-n[s];c<=t||c/r<=t||o.push(n[s+1])}return e.map(s=>{if(!Number.isFinite(s))return s;let c=0,i=o.length-1;for(;c<i;){let a=Math.floor((c+i+1)/2);o[a]<=s?c=a:i=a-1}return o[c]})}var cp=["rho"],lp=["group","confType","confInt"],fp=e=>e==null||typeof e=="number"&&Number.isNaN(e);function _c(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 a=0;a<e.length;a++){if(Number.isNaN(e[a])||Number.isNaN(t[a])||n!==void 0&&fp(n[a]))continue;let l=t[a];if(l!==0&&l!==1)throw new RangeError("'status' must be 0 (censored) or 1 (event)");if(e[a]===-1/0||e[a]===1/0&&l===1)throw new RangeError("event times must be finite");r.push(a)}if(r.length===0)throw new RangeError("no observations without missing values");let o=[],s=new Map;n!==void 0&&(n=Fe(n)),n!==void 0&&(o=Array.from(new Set(r.map(a=>n[a]))).sort((a,l)=>typeof a=="number"&&typeof l=="number"?a-l:String(a)<String(l)?-1:String(a)>String(l)?1:0),o.forEach((a,l)=>s.set(a,l)));let c=uo(r.map(a=>e[a])),i=r.map((a,l)=>({time:c[l],event:t[a]===1,group:n===void 0?0:s.get(n[a])}));return i.sort((a,l)=>a.time-l.time),{obs:i,labels:o.map(a=>`group=${Pe(a)}`)}}function mp(e,t,n,r={}){C(r,cp,"survdiff");let{rho:o=0}=r;if(!Number.isFinite(o))throw new RangeError("'rho' must be a finite number");let{obs:s,labels:c}=_c(e,t,n),i=c.length;if(i<2)throw new RangeError("there is only 1 group");let a=new Array(i).fill(0);for(let N of s)a[N.group]++;let l=new Array(i).fill(0),f=new Array(i).fill(0),m=Array.from({length:i},()=>new Array(i).fill(0)),u=a.slice(),p=s.length,d=1;for(let N=0;N<s.length;){let b=s[N].time,h=N,E=new Array(i).fill(0),x=new Array(i).fill(0),M=0;for(;h<s.length&&s[h].time===b;){let R=s[h];x[R.group]++,R.event&&(E[R.group]++,M++),h++}if(M>0){let R=o===0?1:d**o;for(let T=0;T<i;T++)l[T]+=R*E[T],f[T]+=R*M*u[T]/p;if(p>1){let T=R*R*M*(p-M)/(p*(p-1));for(let S=0;S<i;S++)for(let A=0;A<i;A++)m[S][A]+=T*u[S]*((S===A?1:0)-u[A]/p)}d*=1-M/p}for(let R=0;R<i;R++)u[R]-=x[R];p-=h-N,N=h}let g=f.map((N,b)=>N>0?b:-1).filter(N=>N>=0),w=0,y=Math.max(g.length-1,0);if(g.length>=2){let N=g.slice(1),b=N.map(E=>l[E]-f[E]),h=N.map(E=>N.map(x=>m[E][x]));try{let E=Ve(h,b);w=b.reduce((x,M,R)=>x+M*E[R],0)}catch{({chisq:w,df:y}=up(h,b))}}let v=y===0?NaN:St(w,y,{lowerTail:!1});return{n:a,groups:c,obs:l,exp:f,var:m,chisq:w,df:y,pvalue:v,pValue:v,warnings:[]}}function up(e,t){let n=ct(e,{symmetric:!0}),r=n.values,o=r.reduce((i,a)=>Math.max(i,a),0),s=0,c=0;return r.forEach((i,a)=>{if(!(o>0&&i>1e-10*o))return;let l=t.reduce((f,m,u)=>f+m*n.vectors[u][a],0);s+=l*l/i,c++}),{chisq:s,df:c}}function As(e,t){let n=Math.sqrt(Number.EPSILON),r=t.findIndex(s=>s<.5+n);if(r<0)return NaN;let o=t[r];if(Math.abs(o-.5)<n){for(let s=r+1;s<t.length;s++)if(t[s]<o)return Bt(e[r],e[s])}return e[r]}function pp(e,t,n={}){C(n,lp,"survfit");let{group:r,confType:o="log",confInt:s=.95}=n;if(o!=="log"&&o!=="plain"&&o!=="log-log")throw new RangeError(`invalid 'confType': ${String(o)}`);if(!(s>0&&s<1))throw new RangeError("'confInt' must be between 0 and 1");let{obs:c,labels:i}=_c(e,t,r),a=r===void 0?["all"]:i,l=(1-s)/2,f=l<1e-8?Z(l,{lowerTail:!1}):Z(1-(1-s)/2),m=[],u={strata:[],time:[],nRisk:[],nEvent:[],nCensor:[],surv:[],stdErr:m,stdError:m,lower:[],upper:[],median:[],medianRows:a};return a.forEach((p,d)=>{let g=c.filter(h=>h.group===d),w=u.time.length,y=g.length,v=1,N=0;for(let h=0;h<g.length;){let E=g[h].time,x=h,M=0;for(;x<g.length&&g[x].time===E;)g[x].event&&M++,x++;let R=x-h-M;M>0&&(v*=1-M/y,N+=M<y?M/(y*(y-M)):1/0);let T=Math.sqrt(N),S,A;if(o==="log")S=v===0?NaN:v*Math.exp(-f*T),A=v===0?NaN:Math.min(1,v*Math.exp(f*T));else if(o==="plain"){let k=f*T*v;S=Number.isNaN(k)?NaN:Math.max(0,v-k),A=Number.isNaN(k)?NaN:Math.min(1,v+k)}else if(v===0||v===1)S=NaN,A=NaN;else{let k=Math.log(v),q=Math.log(-k);S=Math.exp(-Math.exp(q-f*T/k)),A=Math.exp(-Math.exp(q+f*T/k))}u.strata.push(p),u.time.push(E),u.nRisk.push(y),u.nEvent.push(M),u.nCensor.push(R),u.surv.push(v),u.stdErr.push(v*T),u.lower.push(S),u.upper.push(A),y-=x-h,h=x}let b=u.time.slice(w);u.median.push([As(b,u.surv.slice(w)),As(b,u.lower.slice(w)),As(b,u.upper.slice(w))])}),u}var hp=["ties","weights","strata","init","control","nocenter","confLevel"],dp=["eps","iterMax","tolerChol","tolerInf"],gp=e=>e==null||typeof e=="number"&&Number.isNaN(e),wp=(e,t)=>typeof e=="number"&&typeof t=="number"?e-t:String(e)<String(t)?-1:String(e)>String(t)?1:0,Ic=700;function ks(e,t,n){let{time:r,event:o,weight:s,x:c,stratumEnd:i}=e,a=r.length,l=t.length,f=()=>new Array(l).fill(0),m=()=>Array.from({length:l},f),u=f(),p=m(),d=0,g=0,w=f(),y=m(),v=f(),N=m(),b=0,h=0,E=x=>{let M=Math.exp(h-x);g*=M,b*=M;for(let R=0;R<l;R++){w[R]*=M,v[R]*=M;let T=y[R],S=N[R];for(let A=0;A<=R;A++)T[A]*=M,S[A]*=M}h=x};for(let x=a-1;x>=0;){i[x]&&(h=0,g=0,w=f(),y=m());let M=r[x],R=0,T=0;b=0;let S=x;do{let A=c[S],k=0;for(let z=0;z<l;z++)k+=t[z]*A[z];let q=s[S];Number.isFinite(k)&&(k-h>Ic?E(k):k-h<-Ic&&g===0&&b===0&&(h=k));let I=Math.exp(k-h)*q;if(o[S]){R++,T+=q,b+=I,d+=q*k;for(let z=0;z<l;z++){u[z]+=q*A[z],v[z]+=I*A[z];let P=N[z];for(let V=0;V<=z;V++)P[V]+=I*A[z]*A[V]}}else{g+=I;for(let z=0;z<l;z++){w[z]+=I*A[z];let P=y[z];for(let V=0;V<=z;V++)P[V]+=I*A[z]*A[V]}}S--}while(S>=0&&r[S]===M&&!i[S]);if(R>0){let A=n?R:1,k=n?R:1,q=T/A;for(let I=0;I<A;I++){g+=n?b/k:b,d-=q*(Math.log(g)+h);for(let z=0;z<l;z++){w[z]+=n?v[z]/k:v[z];let P=w[z]/g;u[z]-=q*P;let V=y[z],W=N[z],F=p[z];for(let L=0;L<=z;L++)V[L]+=n?W[L]/k:W[L],F[L]+=q*(V[L]-P*w[L])/g}}v.fill(0);for(let I of N)I.fill(0)}x=S}for(let x=0;x<l;x++)for(let M=0;M<x;M++)p[M][x]=p[x][M];return{loglik:d,score:u,info:p}}function qs(e,t){let n=e.length,r=0,o=!1;for(let a=0;a<n;a++){let l=e[a][a];l>0&&((!o||l>r)&&(r=l),o=!0)}let s=o?r*t:t,c=0,i=!0;for(let a=0;a<n;a++){let l=e[a][a];if(!Number.isFinite(l)||l<s){e[a][a]=0,l<-8*s&&(i=!1);for(let f=a+1;f<n;f++)e[f][a]=0;continue}c++;for(let f=a+1;f<n;f++){let m=e[f][a]/l;e[f][a]=m,e[f][f]-=m*m*l;for(let u=f+1;u<n;u++)e[u][f]-=m*e[u][a]}}return i?c:-c}function _s(e,t){let n=e.length,r=t.slice();for(let o=0;o<n;o++){let s=r[o];for(let c=0;c<o;c++)s-=r[c]*e[o][c];r[o]=s}for(let o=n-1;o>=0;o--){if(e[o][o]===0){r[o]=0;continue}let s=r[o]/e[o][o];for(let c=o+1;c<n;c++)s-=r[c]*e[c][o];r[o]=s}return r}function Is(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 s=n;s<r;s++)e[n][s]+=o*e[r][s]}}for(let n=0;n<t;n++)for(let r=0;r<n;r++)e[n][r]=e[r][n];return e}var po=(e,t)=>({statistic:e,df:t,pValue:St(e,t,{lowerTail:!1})});function bp(e,t){let n=e.length,r=()=>new Array(t).fill(NaN),o=po(0,0),s=new Array(t).fill(0),c=r(),i=r();return{coefficients:r(),var:Array.from({length:t},()=>new Array(t).fill(0)),loglik:[0,0],score:0,wald:0,iter:0,means:Array.from({length:t},(a,l)=>ke(e.map(f=>f[l]))),n,nevent:0,linearPredictors:new Array(n).fill(0),se:s,z:c,p:i,stdError:s,zValue:c,pValue:i,lower:r(),upper:r(),logtest:o,sctest:{...o},waldtest:{...o},rsq:{rsq:0,maxrsq:0},warnings:[]}}function Np(e,t,n,r={}){C(r,hp,"coxph"),C(Ft(r,"control"),dp,"coxph","control");let{ties:o="efron",weights:s,strata:c,init:i,control:a={},nocenter:l=[-1,0,1],confLevel:f=.95}=r,{eps:m=1e-9,iterMax:u=20,tolerChol:p=2**-39}=a,d=a.tolerInf??Math.sqrt(m);if(o!=="efron"&&o!=="breslow")throw new RangeError(`invalid 'ties': ${String(o)}`);if(!(m>0&&m<1/0))throw new RangeError("'eps' must be a positive finite number");if(!(p>0&&p<1/0))throw new RangeError("'tolerChol' must be a positive finite number");if(!(d>0&&d<1/0))throw new RangeError("'tolerInf' must be a positive finite number");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 g=e.length;if(t.length!==g||n.length!==g)throw new RangeError("'time', 'status' and 'x' must have the same length");if(s!==void 0&&s.length!==g)throw new RangeError("'weights' must have the same length as 'time'");if(c!==void 0&&c.length!==g)throw new RangeError("'strata' must have the same length as 'time'");let w=g===0?0:n[0].length;if(w<1)throw new RangeError("'x' must have at least one column");if(n.some(O=>O.length!==w))throw new RangeError("the rows of 'x' must have the same length");if(i!==void 0&&i.length!==w)throw new RangeError("'init' must have one value per column of 'x'");if(i?.some(O=>!Number.isFinite(O)))throw new RangeError("'init' must contain finite values");let y=[];for(let O=0;O<g;O++){if(Number.isNaN(e[O])||Number.isNaN(t[O])||n[O].some(re=>Number.isNaN(re))||s!==void 0&&Number.isNaN(s[O])||c!==void 0&&gp(c[O]))continue;let K=t[O];if(K!==0&&K!==1)throw new RangeError("'status' must be 0 (censored) or 1 (event)");if(e[O]===-1/0||e[O]===1/0&&K===1)throw new RangeError("event times must be finite");y.push(O)}let v=y.length;if(v===0)throw new RangeError("no observations without missing values");if(y.some(O=>n[O].some(K=>!Number.isFinite(K))))throw new RangeError("'x' contains infinite values");let N=y.map(O=>s===void 0?1:s[O]);if(N.some(O=>!(O>0)))throw new RangeError("Invalid weights, must be >0");if(N.some(O=>!Number.isInteger(O)))throw new RangeError("non-integer weights (robust variance) are not supported yet");let b=0;for(let O of y)b+=t[O];if(b===0)return bp(y.map(O=>n[O]),w);let h=uo(y.map(O=>e[O])),E=new Array(v).fill(0);if(c!==void 0){let O=Fe(c),K=Array.from(new Set(y.map(ue=>O[ue]))).sort(wp),re=new Map;K.forEach((ue,le)=>re.set(ue,le)),E=y.map(ue=>re.get(O[ue]))}let x=Array.from({length:v},(O,K)=>K).sort((O,K)=>E[O]-E[K]||h[O]-h[K]||O-K),M=0;for(let O of N)M+=O;let R=x.map(O=>N[O]),T=x.map(O=>n[y[O]].slice()),S=new Array(w).fill(0),A=new Array(w).fill(1),k=new Array(w).fill(0),q=new Set(l);for(let O=0;O<w;O++){if(T.every(le=>q.has(le[O])))continue;let K=T.map(le=>le[O]),re=le=>{let Ee=0;for(let ce=0;ce<v;ce++)Ee+=R[ce]*le[ce];let nt=Ee/M,Lt=0,Kt=le.map((ce,he)=>(Lt+=R[he]*Math.abs(ce-nt),ce-nt)),ie=Lt===0?1:M/Lt;return{m:nt,s:ie,d:Kt,ok:Number.isFinite(nt)&&ie>=2**-1022&&ie<1/0}},ue=re(K);if(!ue.ok){let{groups:le,k:Ee}=ee([K]);ue=re(le[0]),k[O]=Ee}for(let le=0;le<v;le++)T[le][O]=ue.d[le]*ue.s;S[O]=_(ue.m,k[O]),A[O]=ue.s}let I={time:x.map(O=>h[O]),event:x.map(O=>t[y[O]]===1),weight:R,x:T,stratumEnd:x.map((O,K)=>K===v-1||E[x[K+1]]!==E[O])},z=o==="efron",P=i===void 0?new Array(w).fill(0):i.map((O,K)=>_(O/A[K],k[K])),V=ks(I,P,z),W=[V.loglik,V.loglik],F=V.info.map(O=>O.slice()),L=qs(F,p),$=_s(F,V.score),X=0;for(let O=0;O<w;O++)X+=V.score[O]*$[O];let te=0,ne,ae=!1;if(u===0||!Number.isFinite(V.loglik))ne=Is(F);else{let O=V.loglik,K=P.map((le,Ee)=>le+$[Ee]),re=0,ue=!1;for(te=1;te<=u;te++){V=ks(I,K,z),F=V.info.map(Ee=>Ee.slice()),L=qs(F,p);let le=Number.isFinite(V.loglik)&&V.score.every(Number.isFinite)&&V.info.every(Ee=>Ee.every(Number.isFinite));if(le&&(V.loglik===O||Math.abs(1-O/V.loglik)<=m)){ue=!0,W[1]=V.loglik,P=K;break}!le||V.loglik<O?(re++,K=K.map((Ee,nt)=>(Ee+re*P[nt])/(re+1))):(re=0,O=V.loglik,W[1]=O,$=_s(F,V.score),P=K,K=K.map((Ee,nt)=>Ee+$[nt]))}ue?ne=Is(F):(te=u+1,ae=!0,u>1?(V=ks(I,P,z),W[1]=V.loglik,ne=V.info.map((le,Ee)=>le.map((nt,Lt)=>Ee===Lt?le[Ee]>0?1/le[Ee]:0:-0))):ne=Is(F))}let Me=A.map(O=>Math.floor(Math.log2(O))),Ie=A.map((O,K)=>_(O,-Me[K])),$e=Me.map((O,K)=>O-k[K]),pt=P.map((O,K)=>O*Ie[K]),B=V.score.map((O,K)=>O/Ie[K]),J=ne.map((O,K)=>O.map((re,ue)=>re*Ie[K]*Ie[ue])),D=pt.map((O,K)=>_(O,$e[K])),U=J.map((O,K)=>O.map((re,ue)=>_(re,$e[K]+$e[ue]))),H=D.slice();if(u>0&&Math.abs(L)<w)for(let O=0;O<w;O++)ne[O][O]===0&&(H[O]=NaN);let me=0;for(let O=0;O<w;O++)me+=D[O]*S[O];let Le=y.map(O=>{let K=0;for(let re=0;re<w;re++)K+=n[O][re]*D[re];return K-me}),He=new Te;if(u>1){let O=J.map((K,re)=>{let ue=0;for(let le=0;le<w;le++)ue+=B[le]*J[le][re];return Math.abs(ue)});if(ae)He.add("Ran out of iterations and did not converge"),(ge(Le)>500||O.some(K=>!Number.isFinite(K)))&&He.add("one or more coefficients may be infinite");else{let K=[];for(let re=0;re<w;re++){let ue=O[re];(!Number.isFinite(B[re])||_(ue,$e[re])>m&&ue>d*Math.abs(pt[re]))&&K.push(re+1)}K.length>0&&He.add(`Loglik converged before variable ${K.join(",")} ; coefficient may be infinite. `)}}let Xe=H.map((O,K)=>Number.isNaN(O)?-1:K).filter(O=>O>=0),We=Xe.length,je=Xe.map(O=>pt[O]-(i===void 0?0:_(i[O],-$e[O]))),tt=0;if(We===1)tt=je[0]*je[0]/J[Xe[0]][Xe[0]];else if(We>1){let O=Xe.map(re=>Xe.map(ue=>J[re][ue]));qs(O,p);let K=_s(O,je);for(let re=0;re<We;re++)tt+=je[re]*K[re]}let yt=(1-f)/2,_t=yt<1e-8?Z(yt,{lowerTail:!1}):Z((1+f)/2),cn=J.map((O,K)=>Math.sqrt(O[K])),It=cn.map((O,K)=>_(O,$e[K])),Mt=H.map((O,K)=>(Number.isNaN(O)?O:pt[K])/cn[K]),dr=Mt.map(O=>St(O*O,1,{lowerTail:!1})),Pn=H.map((O,K)=>Math.exp(O-_t*It[K])),gr=H.map((O,K)=>Math.exp(O+_t*It[K])),Dn=-2*(W[0]-W[1]);return j("coxph",{coefficients:H,var:U,loglik:W,score:X,wald:tt,iter:te,means:S,n:v,nevent:b,linearPredictors:Le,se:It,z:Mt,p:dr,stdError:It,zValue:Mt,pValue:dr,lower:Pn,upper:gr,logtest:po(Dn,We),sctest:po(X,We),waldtest:po(tt,We),rsq:{rsq:1-Math.exp(-Dn/v),maxrsq:1-Math.exp(2*W[0]/v)},warnings:He.list})}var vp=["center"],yp=2**-1022;function Mp(e,t,n={}){C(n,vp,"leveneTest");let r=be(n.center??"median",["median","mean"]),{groups:o}=Vt(e,t);if(o.length<2)throw new RangeError("contrasts can be applied only to factors with 2 or more levels");let s=l=>{let f=r==="median"?Ur(l):ke(l);return l.map(m=>Math.abs(m-f))},c=ee(o).groups.map(s),i=(l,f)=>l.some(m=>m>0&&m<yp)||l.every(m=>m===0)&&o[f].some(m=>m!==o[f][0]);if(c.some(i)){let l=o.map(u=>{let p=ee([u]);return{d:s(p.groups[0]),k:p.k}}),f=Math.max(...l.map(u=>u.d.some(p=>p>0)?Math.floor(Math.log2(Math.max(...u.d)))+u.k:-1/0)),m=Number.isFinite(f)?f:0;c=l.map(u=>u.d.map(p=>_(p,u.k-m)))}let a=Hi(en(c));return{colNames:["Df","F value","Pr(>F)"],rowNames:["group",""],table:[[a.df[0],a.fValue,a.pValue],[a.df[1],NaN,NaN]],warnings:[]}}var Ep=["score"],xp=["alternative","exact","variant","levels"];function Rp(e,t,...n){C(Nn(n),Ep,"propTrendTest");let{score:r}=bn(["score"],n),o=Array.from(e),s=Array.from(t),c=o.length,i=r===void 0?o.map((v,N)=>N+1):Array.from(r);if(s.length!==c||i.length!==c)throw new RangeError("'x', 'n' and 'score' must have the same length");if(s.some(v=>!(v>0&&v<1/0)))throw new RangeError("elements of 'n' must be positive and finite");if(o.some((v,N)=>!(v>=0&&v<=s[N])))throw new RangeError("elements of 'x' must be between 0 and 'n'");if(o.every(v=>v===0)||o.every((v,N)=>v===s[N]))throw new RangeError("all proportions are 0 or all are 1: the test for trend is not defined");let a=s.reduce((v,N)=>v+N,0),l=o.reduce((v,N)=>v+N,0)/a,f=s.map(v=>v/(l*(1-l))),m=o.map((v,N)=>v/s[N]),u=i.flatMap((v,N)=>Number.isNaN(v)?[]:[N]);if(u.length===0)throw new RangeError("no non-missing scores");if(u.some(v=>!Number.isFinite(i[v])))throw new RangeError("non-missing scores must be finite");if(u.every(v=>i[v]===i[u[0]]))throw new RangeError("at least 2 distinct non-missing scores are needed");let p=i,{sw:d,sxy:g,sxx:w}=Ls(f,m,p,u);if(!(w>=2**-1022&&w<1/0&&Math.abs(g)<1/0)){let v=ee([u.map(N=>i[N])]);v.k!==0&&(p=i.slice(),u.forEach((N,b)=>p[N]=v.groups[0][b]),{sw:d,sxy:g,sxx:w}=Ls(f,m,p,u))}let y=Oe(g,w);if(!(a<1/0)||l>0&&l<1&&!(d<1/0&&Math.abs(g)<1/0&&w<1/0)){let{groups:[v,N],k:b}=ee([o,s]),h=v.reduce((x,M)=>x+M,0)/N.reduce((x,M)=>x+M,0),E=Ls(N,m,p,u);y=_(E.sxy/h*(E.sxy/E.sxx/(1-h)),b)}return{method:"Chi-squared Test for Trend in Proportions",statistic:y,statisticName:"X-squared",parameter:1,parameterName:"df",pValue:xe(y,.5,2,!1,!1),warnings:[]}}function Ls(e,t,n,r){let o=r.reduce((l,f)=>l+e[f],0),s=r.reduce((l,f)=>l+e[f]*n[f],0)/o,c=r.reduce((l,f)=>l+e[f]*t[f],0)/o,i=0,a=0;for(let l of r){let f=n[l]-s;i+=e[l]*f*(t[l]-c),a+=e[l]*f*f}return{sw:o,sxy:i,sxx:a}}var Tp=2**24,Sp=2**32,Ap=`Sample size > 100 or data with ties
10
- p-value based on normal approximation. Specify nperm for permutation p-value`,kp=`Sample data with ties
11
- p-value based on normal approximation. Specify nperm for permutation p-value.`;function qp(e,t,n,r=!1){let o=Array.from(e);if(o.length!==t.length)throw new RangeError("'x' and 'g' must have the same length");let s=f=>typeof f=="number"&&(r?!Number.isFinite(f):Number.isNaN(f)),c=o.flatMap((f,m)=>s(f)||s(t[m])?[]:[m]);n===void 0&&(t=Fe(t));let i;if(n!==void 0)i=n.map(Pe);else{let f=[...new Set(c.map(m=>t[m]))];i=f.every(m=>typeof m=="number")?f.sort((m,u)=>m-u).map(Pe):[...new Set(f.map(Pe))].sort(it)}let a=i.map(()=>[]),l=new Map(i.map((f,m)=>[f,m]));for(let f of c){let m=l.get(Pe(t[f]));if(m===void 0)throw new RangeError(`the group '${Pe(t[f])}' is not among 'levels'`);a[m].push(o[f])}return a}function _p(e,t){let n=new Map;for(let l of t)n.set(l,(n.get(l)??0)+1);let r=[...n.values()],o=t.length,s=(l,f)=>l.reduce((m,u)=>m+f(u),0),c=(o*(o-1)*(2*o+5)-s(e,l=>l*(l-1)*(2*l+5))-s(r,l=>l*(l-1)*(2*l+5)))/72,i=o<3?0:s(e,l=>l*(l-1)*(l-2))*s(r,l=>l*(l-1)*(l-2))/(36*o*(o-1)*(o-2)),a=s(e,l=>l*(l-1))*s(r,l=>l*(l-1))/(8*o*(o-1));return c+i+a}function Ip(e,t,n={}){C(n,xp,"jonckheereTest");let r=be(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 s=qp(e,t,n.levels,o==="clinfun");if(s.filter(v=>v.length>0).length<2)throw new RangeError(o==="clinfun"?"only one group has non-missing data":"all observations are in the same group");let c=s.flat(),i=c.length,a=new Set(c).size<i,l=0,f=[],m=i;s.forEach((v,N)=>{m-=v.length,f.push([v.length,m]);for(let b=N+1;b<s.length;b++)for(let h of v)for(let E of s[b])l+=h<E?1:h===E?.5:0});let u=[],p=!a&&i<=100;o==="DescTools"&&n.exact!==void 0?(p=n.exact,p&&a&&(p=!1,u.push(kp))):p||u.push(Ap);let d,g;if(p){let v=1,N=0;for(let[h,E]of f)h===0||E===0||(N+=v*(h*E+1),v+=h*E);if(v>Tp||N>Sp)throw new RangeError(`the exact distribution of JT has ${v} values and needs ${N} steps, too many: use exact: false`);let b=[1];for(let[h,E]of f){if(h===0||E===0)continue;let x=Array.from({length:h*E+1},(R,T)=>ia(T,h,E,!1)),M=new Array(b.length+x.length-1).fill(0);b.forEach((R,T)=>x.forEach((S,A)=>M[T+A]+=R*S)),b=M}d=0,g=0,b.forEach((h,E)=>{E<=l&&(d+=h),E>=l&&(g+=h)})}else{let v=f.reduce((h,[E,x])=>h+E*x,0)/2,N=a?_p(s.map(h=>h.length),c):f.reduce((h,[E,x])=>h+E*x*(E+x+1)/12,0),b=(l-v)/Math.sqrt(N);d=Y(b),g=1-d,g<1e-8&&(g=Y(b,{lowerTail:!1}))}let y=r==="increasing"?g:r==="decreasing"?d:Math.min(1,2*Math.min(g,d,o==="clinfun"?.5:1));return j("jonckheereTest",{method:"Jonckheere-Terpstra test",alternative:r,statistic:l,statisticName:"JT",pValue:y,warnings:u})}var Lp=["nClasses","adjust"];function mr(e){return Ge(Array.from(e)).sort((t,n)=>t-n)}function Lc(e){if(e.length>0&&!(Number.isFinite(e[0])&&Number.isFinite(e[e.length-1])))throw new RangeError("'x' must be finite")}function ho(e){Lc(e);let t=ta(e);if(t.some(Number.isNaN))throw new RangeError("all 'x' values are identical");return t}function Op(e){let t=mr(e),n=t.length;if(n<8)throw new RangeError("sample size must be greater than 7");let r=ho(t),o=0;for(let a=1;a<=n;a++)o+=(2*a-1)*(Y(r[a-1],{logP:!0})+Y(r[n-a],{lowerTail:!1,logP:!0}));let s=-n-o/n,c=(1+.75/n+2.25/n**2)*s,i;return c<.2?i=1-Math.exp(-13.436+101.14*c-223.73*c**2):c<.34?i=1-Math.exp(-8.318+42.796*c-59.938*c**2):c<.6?i=Math.exp(.9177-4.279*c-1.38*c**2):c<10?i=Math.exp(1.2937-5.709*c+.0186*c**2):i=37e-25,{method:"Anderson-Darling normality test",statistic:s,statisticName:"A",pValue:i,warnings:[]}}function Fp(e){let t=mr(e),n=t.length;if(n<8)throw new RangeError("sample size must be greater than 7");let r=ho(t),o=1/(12*n);r.forEach((a,l)=>{o+=(Y(a)-(2*(l+1)-1)/(2*n))**2});let s=(1+.5/n)*o,c=[],i;return s<.0275?i=1-Math.exp(-13.953+775.5*s-12542.61*s**2):s<.051?i=1-Math.exp(-5.903+179.546*s-1515.29*s**2):s<.092?i=Math.exp(.886-31.62*s+10.897*s**2):s<1.1?i=Math.exp(1.111-34.242*s+12.832*s**2):(i=737e-12,c.push("p-value is smaller than 7.37e-10, cannot be computed more accurately")),j("cvmTest",{method:"Cramer-von Mises normality test",statistic:o,statisticName:"W",pValue:i,warnings:c})}function Pp(e){let t=mr(e),n=t.length;if(n<5)throw new RangeError("sample size must be greater than 4");let r=ho(t).map(f=>Y(f)),o=-1/0,s=-1/0;r.forEach((f,m)=>{o=Math.max(o,(m+1)/n-f),s=Math.max(s,f-m/n)});let c=Math.max(o,s),i=n<=100?c:c*(n/100)**.49,a=n<=100?n:100,l=Math.exp(-7.01256*i**2*(a+2.78019)+2.99587*i*Math.sqrt(a+2.78019)-.122119+.974598/Math.sqrt(a)+1.67997/a);if(l>.1){let f=(Math.sqrt(n)-.01+.85/Math.sqrt(n))*c;f<=.302?l=1:f<=.5?l=2.76773-19.828315*f+80.709644*f**2-138.55152*f**3+81.218052*f**4:f<=.9?l=-4.901232+40.662806*f-97.490286*f**2+94.029866*f**3-32.355711*f**4:f<=1.31?l=6.198765-19.558097*f+23.186922*f**2-12.234627*f**3+2.423045*f**4:l=0}return{method:"Lilliefors (Kolmogorov-Smirnov) normality test",statistic:c,statisticName:"D",pValue:l,warnings:[]}}function Dp(e){let t=mr(e),n=t.length;if(n<5||n>5e3)throw new RangeError("sample size must be between 5 and 5000");if(Lc(t),t[0]===t[n-1])throw new RangeError("all 'x' values are identical");let r=t.map((m,u)=>Z((u+1-3/8)/(n+1/4))),o=ee([t]).groups[0],s=Math.min(ea(o,r)**2/(qe(o)*qe(r)),1),c=Math.log(n),i=Math.log(c),a=-1.2725+1.0521*(i-c),l=1.0308-.26758*(i+2/c),f=Y((Math.log(1-s)-a)/l,{lowerTail:!1});return{method:"Shapiro-Francia normality test",statistic:s,statisticName:"W",pValue:f,warnings:[]}}function Cp(e,t={}){C(t,Lp,"pearsonTest");let n=mr(e),r=n.length,o=t.nClasses??Math.ceil(2*r**(2/5)),s=t.adjust??!0;if(!(Number.isInteger(o)&&o-1-(s?2:0)>0))throw new RangeError("'nClasses' must be an integer leaving positive degrees of freedom");if(o>2147483647)throw new RangeError("attempt to make a table with >= 2^31 elements");let c=[];for(let f of ho(n)){let m=Math.min(o,Math.floor(1+o*Y(f)));m>=1&&c.push(m)}c.sort((f,m)=>f-m);let i=r/o,a=0;for(let f=1,m=0;f<=o;f++){let u=0;for(;m<c.length&&c[m]===f;)u++,m++;a=a+(u-i)**2/i}let l=o-1-(s?2:0);return{method:"Pearson chi-square normality test",statistic:a,statisticName:"P",pValue:xe(a,l/2,2,!1,!1),warnings:[]}}var Vp=["weight"],zp=["exact"],$p=["model","type","unit","r0","confLevel"];function ur(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(s=>s[o]))}function go(e,t){if(e.some(n=>n.some(Number.isNaN)))throw new RangeError(`'${t}' contains missing values`);if(e.some(n=>n.some(r=>!Number.isFinite(r))))throw new RangeError(`'${t}' contains infinite values`)}var Fc=e=>2*Y(-Math.abs(e));function Wp(e,t={}){C(t,Vp,"kappa2");let n=be(t.weight,["unweighted","equal","squared"]);if(e.some(x=>x.length!==2))throw new TypeError("'ratings' must be a matrix with two columns");let r=e.filter(x=>!x.some(Number.isNaN)),o=r.length;if(o===0)throw new RangeError("no complete rating pairs");if(o<2)throw new RangeError("at least 2 complete rating pairs are needed");let s=r.map(x=>wn(x[0])),c=r.map(x=>wn(x[1])),i=[...new Set([...s,...c])].sort((x,M)=>x<M?-1:x>M?1:0),a=i.length,l=new Map(i.map((x,M)=>[x,M])),f=Array.from({length:a},()=>new Array(a).fill(0));for(let x=0;x<o;x++)f[l.get(s[x])][l.get(c[x])]++;let m=x=>n==="unweighted"?x===0?1:0:n==="equal"?1-x/(a-1):1-x*x/((a-1)*(a-1)),u=Array.from({length:a},(x,M)=>Array.from({length:a},(R,T)=>m(Math.abs(M-T)))),p=f.map(x=>se(x)/o),d=Array.from({length:a},(x,M)=>se(f.map(R=>R[M]))/o),g=x=>{let M=[];for(let R=0;R<a;R++)for(let T=0;T<a;T++)M.push(x(R,T));return se(M)},w=g((x,M)=>f[x][M]*u[x][M])/o,y=g((x,M)=>p[x]*d[M]*u[x][M]),v=(w-y)/(1-y),N=Array.from({length:a},(x,M)=>se(d.map((R,T)=>R*u[M][T]))),b=Array.from({length:a},(x,M)=>se(p.map((R,T)=>R*u[T][M]))),h=(g((x,M)=>p[x]*d[M]*(u[x][M]-(N[x]+b[M]))**2)-y*y)/(o*(1-y)**2),E=v/Math.sqrt(h);return{value:v,statistic:E,pValue:Fc(E),warnings:[]}}function jp(e,t={}){C(t,zp,"kappamFleiss");let n=t.exact??!1,r=ur(e,"ratings");go(e,"ratings");let o=e.length,s=r.length;if(o<2||s<2)throw new RangeError("at least 2 subjects and 2 raters are needed");let c=e.flat(),i=Fe(c),a=[...new Set(i)].sort((h,E)=>h-E),l=new Map(a.map((h,E)=>[h,E])),f=new Map(c.map((h,E)=>[h,l.get(i[E])])),m=a.length,u=e.map(h=>{let E=new Array(m).fill(0);for(let x of h)E[f.get(x)]++;return E}),p=se(u.map(h=>(se(h.map(E=>E*E))-s)/(s*(s-1))))/o,d=Array.from({length:m},(h,E)=>se(u.map(x=>x[E]))/(o*s)),g=se(d.map(h=>h*h));if(n){let h=r.map(x=>{let M=new Array(m).fill(0);for(let R of x)M[f.get(R)]++;return M.map(R=>R/o)}),E=Array.from({length:m},(x,M)=>qe(h.map(R=>R[M]))*(s-1)/s);g-=se(E)/(s-1)}let w=(p-g)/(1-g);if(n)return{value:w,warnings:[]};let y=se(d.map(h=>h*(1-h))),v=se(d.map(h=>h*(1-h)*(1-h-h))),N=2/(y*y*o*s*(s-1))*(y*y-v),b=w/Math.sqrt(N);return{value:w,statistic:b,pValue:Fc(b),warnings:[]}}function Kp(e,t={}){C(t,$p,"icc");let n=be(t.model,["oneway","twoway"]),r=be(t.type,["consistency","agreement"]),o=be(t.unit,["single","average"]),s=t.r0??0,c=t.confLevel??.95;Be(c),ur(e,"ratings"),go(e,"ratings");let i=ee(e).groups,a=ur(i,"ratings"),l=i.length,f=a.length;if(l<2||f<2)throw new RangeError("at least 2 subjects and 2 raters are needed");if(!(s>=-1&&s<1))throw new RangeError("'r0' must be in [-1, 1)");let m=1-c,u=qe(i.map(F=>ke(F)))*f,p=qe(a.map(F=>ke(F)))*l,d=ke(i.map(F=>qe(F))),g=qe(i.flat())*(l*f-1),w=g-u*(l-1)-p*(f-1),y=w>2**-20*g?w/((l-1)*(f-1)):Hp(i)/((l-1)*(f-1)),v=(F,L,$)=>we(F,L,$,!1,!1),N=(F,L)=>m/2<1e-8?At(m/2,F,L,!1,!1):At(1-m/2,F,L,!0,!1),b=l-1;if(n==="oneway"||r==="consistency"){let F=n==="oneway"?d:y,L=n==="oneway"?l*(f-1):(l-1)*(f-1),$=u/F,X=$/N(b,L),te=$*N(L,b),[ne,ae,Me]=o==="single"?[(u-F)/(u+(f-1)*F),$*(1-s)/(1+(f-1)*s),Ie=>(Ie-1)/(Ie+f-1)]:[(u-F)/u,$*(1-s),Ie=>1-1/Ie];return{value:ne,Fvalue:ae,fValue:ae,df1:b,df2:L,pValue:v(ae,b,L),lbound:Me(X),ubound:Me(te),warnings:[]}}let h=o==="single"?f:1,E=F=>[h*F/(l*(1-F)),1+h*F*(l-1)/(l*(1-F))],x=(F,L)=>na([F*p,L*y],[f-1,(l-1)*(f-1)]),M=o==="single"?(u-y)/(u+(f-1)*y+f/l*(p-y)):(u-y)/(u+(p-y)/l),[R,T]=E(s),S=u/(R*p+T*y),A=x(R,T),[k,q]=[f*M/(l*(1-M)),1+f*M*(l-1)/(l*(1-M))],I=x(k,q),z=N(b,I),P=N(I,b),[V,W]=o==="single"?[l*(u-z*y)/(z*(f*p+(f*l-f-l)*y)+l*u),l*(P*u-y)/(f*p+(f*l-f-l)*y+l*P*u)]:[l*(u-z*y)/(z*(p-y)+l*u),l*(P*u-y)/(p-y+l*P*u)];return{value:M,Fvalue:S,fValue:S,df1:b,df2:A,pValue:v(S,b,A),lbound:V,ubound:W,warnings:[]}}function Oc(e,t){let n=t.length,r=se(t.flatMap(s=>t.map(c=>e[s][c])));return(1-se(t.map(s=>e[s][s]))/r)*n/(n-1)}function Up(e){let t=ur(e,"x").length;if(go(e,"x"),t<2)throw new RangeError("'x' needs at least two items");if(e.length<2)throw new RangeError("'x' needs at least two subjects");let n=Cr(ee(e).groups),r=Qn(e),o=Array.from({length:t},(i,a)=>a),s=se(r.flat()),c;try{let i=Ve(r);c=o.map(a=>1-1/i[a][a])}catch(i){if(!r.flat().every(Number.isFinite))throw i;let a=ee(e).groups;c=o.map(l=>{let f=a.map(p=>p[l]),m=yn(ve(a.map(p=>[1,...p.filter((d,g)=>g!==l)])),f),u=ke(f);return 1-se(m.map(p=>p*p))/se(f.map(p=>(p-u)**2))})}return j("cronbachAlpha",{rawAlpha:Oc(n,o),stdAlpha:(1-t/s)*t/(t-1),G6:1-(t-se(c))/s,averageR:(s-t)/(t*(t-1)),alphaDrop:o.map(i=>Oc(n,o.filter(a=>a!==i))),warnings:Yp(e,r)})}function Yp(e,t){let n=[],r=t.map((o,s)=>s).filter(o=>e.some(s=>s[o]!==e[0][o]));if(r.length<t.length&&n.push("Item = had no variance and was deleted but still is counted in the score"),r.length>=2){let{vectors:o}=ct(r.map(i=>r.map(a=>t[i][a])),{symmetric:!0}),s=o.map(i=>i[0]),c=se(s)<0?-1:1;s.some(i=>c*i<0)&&n.push(`Some items were negatively correlated with the first principal component and probably
5
+ k = 3 groups are required.`);let i=s.n[0],c=s.n.slice(1).map((m,u)=>u+1),a=c.map(m=>{let u=s.n[m],h=[...s.groups[0],...s.groups[m]],p=ye(h),g=0;for(let x=0;x<i;x++)g+=p[x];let w=i+u,b=w*(w+1)/2-g,N=i*u+i*(i+1)/2-g,v=i*u+u*(u+1)/2-b,y=0;for(let x of vt(h))y+=x*x*x-x;let d=i*u/(w*(w-1))*((w*w*w-w)/12-y/12),M=(Math.min(N,v)-i*u/2)/Math.sqrt(d);return g<b?-M:M}),l=c.map(m=>s.n[m]/(s.n[m]+i)),f=Ac(a,l,r,o);return z("manyOneUTest",Is(s,`Wilcoxon, Mann, Whittney U-test
6
+ for multiple comparisons with one control`,a,f,o))}function Xu(e,t){let n=Dt(e,t),r=Mr(n.k,(s,i)=>{let c=n.n[s],a=n.n[i],l=[...n.groups[s],...n.groups[i]],f=ye(l),m=0;for(let b=0;b<c;b++)m+=f[b];let u=m-c*(c+1)/2,h=Math.min(u,c*a-u),p=c+a,g=0;for(let b of vt(l))g+=b*b*b-b;let w=c*a/(p*(p-1))*((p*p*p-p)/12-g/12);return Math.SQRT2*(h-c*a/2)/Math.sqrt(w)}),o=r.map(s=>s.map(i=>Number.isNaN(i)?NaN:_s(i,n.k,1/0)));return z("dscfAllPairsTest",Mo(n,"Dwass-Steele-Critchlow-Fligner all-pairs test",r,o,"single-step"))}function Bu(e,t){let n=Dt(e,t),r=Le(n.groups),o=r.mean,s=r.variance.map((a,l)=>a/n.n[l]),i=Mr(n.k,(a,l)=>(o[a]-o[l])/Math.sqrt(s[a]+s[l])*Math.SQRT2),c=Mr(n.k,(a,l)=>{let f=Ua([r.variance[a],r.variance[l]],[n.n[a],n.n[l]]);return _s(i[a-1][l],n.k,f)});return z("gamesHowellTest",Mo(n,"Games-Howell test",i,c,"none"))}function Qu(e,t){let n=Dt(e,t),{m:r,s2:o,df:s}=Ls(n);if(s<2)throw new RangeError("the studentized range needs at least 2 residual degrees of freedom");let i=Mr(n.k,(a,l)=>(r[a]-r[l])/Math.sqrt(o*(1/n.n[a]+1/n.n[l])/2)),c=i.map(a=>a.map(l=>Number.isNaN(l)?NaN:_s(l,n.k,s)));return z("tukeyTest",Mo(n,"Tukey's test",i,c,"single-step"))}function Zu(e,t,n={}){C(n,Ku,"dunnTest");let r=n.method??"none",o=n.altp??!1,s=Dt(e,t),i=vo(e,t).statistic,{R:c,C:a}=qs(s),l=s.N*(s.N+1)/12-a,f=[],m=[];for(let p=1;p<s.k;p++)for(let g=0;g<p;g++)f.push((c[g]-c[p])/Math.sqrt(l*(1/s.n[g]+1/s.n[p]))),m.push(`${s.labels[g]} - ${s.labels[p]}`);let u=f.map(p=>(o?2:1)*No(Math.abs(p))),h=Ju(u,r);return{chi2:i,Z:f,P:u,PAdjusted:h,zValue:f,pValue:u,pAdjusted:h,comparisons:m,warnings:[]}}function Ju(e,t){let n=e.length,r=(s,i)=>Math.min(1,-Math.expm1(i*Math.log1p(-s))),o=s=>{let i=e.map((a,l)=>l).sort((a,l)=>(s?e[l]-e[a]:e[a]-e[l])||a-l),c=new Array(n);return i.forEach((a,l)=>c[a]=l>0&&e[a]===e[i[l-1]]?c[i[l-1]]:l+1),c};switch(t){case"none":return e.slice();case"bonferroni":return e.map(s=>Math.min(1,n*s));case"sidak":return e.map(s=>r(s,n));case"holm":{let s=o(!1);return e.map((i,c)=>Math.min(1,(n+1-s[c])*i))}case"hs":{let s=o(!1);return e.map((i,c)=>r(i,n+1-s[c]))}case"hochberg":{let s=o(!0);return e.map((i,c)=>Math.min(1,s[c]*i))}case"bh":{let s=o(!0);return e.map((i,c)=>Math.min(1,n/(n+1-s[c])*i))}case"by":{let s=o(!0),i=0;for(let c=1;c<=n;c++)i+=1/c;return e.map((c,a)=>Math.min(1,n/(n+1-s[a])*i*c))}default:throw new RangeError(`invalid method "${String(t)}"`)}}function eh(e,t,n={}){C(n,Wu,"glhtDunnett");let r=le(n.alternative),o=Dt(e,t),{m:s,s2:i,df:c,k:a}=Ls(o),l=Math.sqrt(i),f=o.n.slice(1).map((N,v)=>v+1),m=f.map(N=>s[N]-s[0]),u=f.map(N=>l*Math.sqrt(1/o.n[N]+1/o.n[0])),h=m.map((N,v)=>N/u[v]),p=m.map(N=>_(N,a)),g=u.map(N=>_(N,a)),w=f.map(N=>o.n[N]/(o.n[N]+o.n[0])),b=h.map(N=>r==="two.sided"?dn(N,w,c,!0):dn(r==="greater"?N:-N,w,c,!1));return{names:f.map(N=>`${o.labels[N]} - ${o.labels[0]}`),estimate:p,se:g,t:h,p:b,stdError:g,tValue:h,pValue:b,df:c,warnings:[]}}var qc=["varEqual"],th=2**-1022;function _c(e,t,n={}){if(typeof e!="string"&&ze(t)&&C(t,qc,"onewayTest"),C(n,qc,"onewayTest"),typeof e=="string"){let w=ni(e,t);return _c(w.values,w.groups,n)}let r;t!==void 0&&!Array.isArray(t)?n=t:r=t;let o=hn(e,r),s=Le(o),i=s.groups,c=n.varEqual??!1,a=i.length;if(a<2)throw new RangeError("not enough groups");let l=i.map(w=>w.length);if(l.some(w=>w<2))throw new RangeError("not enough observations");let f=s.mean,m=s.variance,u=l.reduce((w,b)=>w+b,0),h,p,g;if(c){let w=s.grandMean,b=0,N=0;for(let v=0;v<a;v++)b+=l[v]*(f[v]-w)**2,N+=(l[v]-1)*m[v];p=u-a,yr(p,s.ssWithin),h=b/(a-1)/(N/p)}else if(m.some(w=>!(w>=th))&&(g=rh(o))!==void 0){let w=g.map((E,T)=>Ic(l[T]/E.variance,-2*E.k)),b=Math.max(...w.map(E=>E.e)),N=w.map(E=>$e(E.m,E.e-b)),v=N.reduce((E,T)=>E+T,0),y=0;for(let E=0;E<a;E++){let T=N.reduce((R,S,k)=>k===E?R:R+S,0);y+=(T/v)**2/(l[E]-1)}y/=a*a-1;let d=[];for(let E=0;E<a;E++)for(let T=E+1;T<a;T++){let[R,S]=g[E].k>=g[T].k?[g[E],g[T]]:[g[T],g[E]],k=R.mean-$e(S.mean,S.k-R.k);if(k===0)continue;let A=Ic(k,R.k);d.push({m:w[E].m*w[T].m*A.m*A.m/v,e:w[E].e+w[T].e+2*A.e-b})}let M=Math.max(...d.map(E=>E.e)),x=d.reduce((E,T)=>E+$e(T.m,T.e-M),0);h=(d.length===0?0:$e(x,M))/((a-1)*(1+2*(a-2)*y)),p=1/(3*y)}else if(m.some(w=>w===0)){let w=m.flatMap((y,d)=>y===0?[d]:[]);if(w.length>1)throw new RangeError("Welch's test is not defined with two or more constant groups");let b=w[0],N=0,v=0;for(let y=0;y<a;y++)y!==b&&(N+=1/(l[y]-1),v+=l[y]/m[y]*(f[y]-f[b])**2);N/=a*a-1,h=v/((a-1)*(1+2*(a-2)*N)),p=1/(3*N)}else{let w=l.map((d,M)=>d/m[M]),b=w.reduce((d,M)=>d+M,0),N=0,v=0;for(let d=0;d<a;d++)N+=(1-w[d]/b)**2/(l[d]-1),v+=w[d]*f[d];N/=a*a-1,v/=b;let y=0;for(let d=0;d<a;d++)y+=w[d]*(f[d]-v)**2;h=y/((a-1)*(1+2*(a-2)*N)),p=1/(3*N)}return{method:c?"One-way analysis of means":"One-way analysis of means (not assuming equal variances)",statistic:h,statisticName:"F",parameter:[a-1,p],parameterName:["num df","denom df"],pValue:Me(h,a-1,p,!1,!1),warnings:[]}}function nh(e){let t=Array.from(e).filter(o=>!Number.isNaN(o)).sort((o,s)=>o-s),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=Mc(t);return{method:"Shapiro-Wilk normality test",statistic:1-r,statisticName:"W",pValue:yc(r,n),warnings:[]}}function rh(e){let t=[];for(let n of e){let r=Le([n]),o=r.variance[0];if(!(o>0))return;t.push({mean:r.mean[0],variance:o,k:r.k})}return t}function Ic(e,t){let n=Math.floor(Math.log2(Math.abs(e))),r=$e(e,-n);return Math.abs(r)>=2?(r/=2,n++):Math.abs(r)<1&&(r*=2,n--),{m:r,e:t+n}}function Os(e,t,n){let r=[0,0];for(let o=Math.max(0,t);o<Math.min(n,e.length);o++)r=G(r,[e[o],0]);return r[0]}function oh([[e,t],[n,r]]){let o=e+n,s=t+r,i=e+t,c=Math.max(0,i-s),a=Math.min(i,o),l=new Float64Array(a-c+1);for(let f=c;f<=a;f++)l[f-c]=Nt(f,o,s,i,!1);return{lo:c,d:l}}function sh(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 s=0;s<t.length;s++)n[r+s]+=o*t[s]}return n}var yo=class{lo;hi;logNull;constructor(t){let n=0,r=new Float64Array([1]);for(let o of t){let s=oh(o);n+=s.lo,r=sh(r,s.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),s=-1/0;for(let c=0;c<n;c++){let a=this.logNull[c]+(this.lo+c)*o;r[c]=a,a>s&&(s=a)}for(let c=0;c<n;c++)r[c]=Math.exp(r[c]-s);let i=Os(r,0,n);for(let c=0;c<n;c++)r[c]=r[c]/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=G(r,[(this.lo+o)*n[o],0]);return r[0]}cdf(t,n,r){let o=this.density(n),s=t-this.lo;return r?Os(o,s,o.length):Os(o,0,s+1)}};function Fs(e,t){return Bt(e,t)}function Lc(e,t){if(t===e.lo)return 0;if(t===e.hi)return 1/0;let n=e.mean(1);return n===t?1:Fs(r=>e.mean(r)-t,n>t)}function Cs(e,t,n){if(t===e.hi)return 1/0;let r=e.cdf(t,1,!1);return r===n?1:Fs(o=>n-e.cdf(t,o,!1),r<n)}function Ds(e,t,n){if(t===e.lo)return 0;let r=e.cdf(t,1,!0);return r===n?1:Fs(o=>e.cdf(t,o,!0)-n,r>n)}var ah=["alternative","correct","exact","confLevel"];function ih(e,t={}){C(t,ah,"mantelhaenTest");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 s=e.map(Ps);if(s.some(f=>f<2))throw new RangeError("sample size in each stratum must be > 1");let i=le(t.alternative),c=t.correct??!0,a=t.exact??!1,l=t.confLevel??.95;if(Xe(l),r===2&&o===2){if(a&&e.some(m=>m.some(u=>u.some(h=>!Number.isInteger(h)))))throw new RangeError("the exact test needs integer counts");if(a&&s.reduce((m,u)=>m+u,0)>Number.MAX_SAFE_INTEGER)throw new RangeError("the exact test needs counts with a total below 2^53: use exact: false");let f=e.map(m=>[[m[0][0],m[0][1]],[m[1][0],m[1][1]]]);return a?fh(f,i,l):ch(f,s,i,c,l)}return mh(e,s,r,o)}function Ps(e){return e.reduce((t,n)=>t+n.reduce((r,o)=>r+o,0),0)}function Cc(e){let t=0,n=0;for(let o of e)for(let s of o)for(let i of s)t=Math.max(t,i),n++;let r=Math.floor(Math.log2(t))+1+Math.ceil(Math.log2(8*n))-1020;return r<=0?0:r+r%2}function Dc(e,t){return e.map(n=>n.map(r=>r.map(o=>_(o,-t))))}function Oc(e,t,n){let r=0,o=0,s=0,i=0,c=0,a=0,l=0,f=0;return e.forEach(([[m,u],[h,p]],g)=>{let w=t[g],b=m+u,N=h+p,v=m+h,y=u+p;r+=m;let d=b*v/w;o+=Number.isFinite(d)?d:b/w*v;let M=b*N*v*y/(w*w*(w-n));s+=Number.isFinite(M)?M:b/w*(N/w)*(v/(w-n))*y;let x=(m+p)/w,E=(u+h)/w,T=m*p/w,R=u*h/w,S=Number.isFinite(T)?T:m/w*p,k=Number.isFinite(R)?R:u/w*h;i+=S,c+=k,a+=x*S,l+=x*k+E*S,f+=E*k}),{sa:r,se:o,sv:s,sR:i,sS:c,sPR:a,sPSQR:l,sQS:f}}function ch(e,t,n,r,o){let s=Oc(e,t,1),i=0;if(!t.every(Number.isFinite)||!Object.values(s).every(Number.isFinite)){i=Cc(e);let x=Dc(e,i);s=Oc(x,x.map(Ps),_(1,-i))}let{sa:c,se:a,sv:l,sR:f,sS:m,sPR:u,sPSQR:h,sQS:p}=s,g=c-a,w=_(.5,-i),b=r&&Math.abs(g)>=w?w:0,N=_(Ce(Math.abs(g)-b,l),i),v=n==="two.sided"?Ne(N,.5,2,!1,!1):j(Math.sign(g)*Math.sqrt(N),{lowerTail:n==="less"}),y=f/m,d=_(Math.sqrt(u/(2*f*f)+h/(2*f*m)+p/(2*m*m)),-i/2),M;if(n==="less")M=[0,y*Math.exp(Z(o)*d)];else if(n==="greater")M=[y*Math.exp(Z(o,{lowerTail:!1})*d),1/0];else{let x=(1-o)/2,E=x<1e-8?Z(x,{lowerTail:!1}):Z((1+o)/2);M=[y*Math.exp(-E*d),y*Math.exp(E*d)]}return{method:`Mantel-Haenszel chi-squared test ${b!==0?"with":"without"} continuity correction`,alternative:n,statistic:N,statisticName:"Mantel-Haenszel X-squared",parameter:1,parameterName:"df",pValue:v,confInt:M,confLevel:o,estimate:y,estimateNames:"common odds ratio",nullValue:1,nullValueName:"common odds ratio",warnings:[]}}var lh=2**24;function fh(e,t,n){let r=e.reduce((a,[[l,f],[m,u]])=>a+Math.min(l,u)+Math.min(f,m),1);if(r>lh)throw new RangeError(`the exact distribution of S has ${r} values, more than 2^24: use exact: false`);let o=new yo(e),s=e.reduce((a,l)=>a+l[0][0],0),i;if(t==="two.sided"){let a=o.density(1),l=a[s-o.lo]*(1+1e-7),f=[0,0];for(let m of a)m<=l&&(f=G(f,[m,0]));i=Math.min(1,f[0])}else i=o.cdf(s,1,t==="greater");let c;if(t==="less")c=[0,Cs(o,s,1-n)];else if(t==="greater")c=[Ds(o,s,1-n),1/0];else{let a=(1-n)/2;c=[Ds(o,s,a),Cs(o,s,a)]}return{method:"Exact conditional test of independence in 2 x 2 x k tables",alternative:t,statistic:s,statisticName:"S",pValue:i,confInt:c,confLevel:n,estimate:Lc(o,s),estimateNames:"common odds ratio",nullValue:1,nullValueName:"common odds ratio",warnings:[]}}function mh(e,t,n,r){let{diff:o,V:s}=Fc(e,t,n,r,1),i=0;if(!t.every(Number.isFinite)||!o.every(Number.isFinite)||!s.every(f=>f.every(Number.isFinite))){i=Cc(e);let f=Dc(e,i);({diff:o,V:s}=Fc(f,f.map(Ps),n,r,_(1,-i)))}let c=o.length,a=Ke(s,o),l=0;for(let f=0;f<c;f++)l+=o[f]*a[f];return l=_(l,i),{method:"Cochran-Mantel-Haenszel test",statistic:l,statisticName:"Cochran-Mantel-Haenszel M^2",parameter:c,parameterName:"df",pValue:Ne(l,c/2,2,!1,!1),warnings:[]}}var uh=2**-1022;function Fc(e,t,n,r,o){let s=(n-1)*(r-1),i=new Array(s).fill(0),c=Array.from({length:s},()=>new Array(s).fill(0));return e.forEach((a,l)=>{let f=t[l],m=a.map(p=>p.reduce((g,w)=>g+w,0)),u=Array.from({length:r},(p,g)=>a.reduce((w,b)=>w+b[g],0)),h=1/(f*f*(f-o));for(let p=0;p<r-1;p++)for(let g=0;g<n-1;g++){let w=g+(n-1)*p,b=m[g]*u[p]/f;i[w]+=a[g][p]-(Number.isFinite(b)?b:Wr(m[g],u[p],f));for(let N=0;N<r-1;N++)for(let v=0;v<n-1;v++){let y=(g===v?f*m[g]:0)-m[g]*m[v],d=(p===N?f*u[p]:0)-u[p]*u[N],M=h*y*d;c[w][v+(n-1)*N]+=Number.isFinite(M)&&h>=uh?M:m[g]*((g===v?1:0)-m[v]/f)*(((p===N?1:0)-u[N]/f)*(u[p]/(f-o)))}}}),{diff:i,V:c}}var Pc=new Map,hh=2**24,ph=2**32;function $c(e,t){let n=e+t,r=(e+1)*(Math.floor((n+1)**2/4)+1);if(2*r>hh||n*r>ph)throw new RangeError(`the exact distribution for m = ${e} and n = ${t} is too large to compute: use exact: false`)}function dh(e,t){let n=`${e},${t}`,r=Pc.get(n);if(r!==void 0)return r;let o=e+t,s=[];for(let u=1;u<=o;u++)s.push(Math.min(u,o+1-u));let i=s.reduce((u,h)=>u+h,0),c=Array.from({length:e+1},()=>new Float64Array(i+1));c[0][0]=1;for(let u=0;u<o;u++){let h=o-u,p=s[u],g=Array.from({length:e+1},()=>new Float64Array(i+1));for(let w=0;w<=e;w++){let b=c[w],N=e-w;if(N>h)continue;let v=N/h;for(let y=0;y<=i;y++){let d=b[y];d!==0&&(N>0&&(g[w+1][y+p]+=d*v),N<h&&(g[w][y]+=d*(1-v)))}}c=g}let a=c[e],l=0;for(;l<a.length&&a[l]===0;)l++;let f=a.length-1;for(;f>l&&a[f]===0;)f--;let m={min:l,probs:a.slice(l,f+1)};return Pc.set(n,m),m}function Vs(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:s}=dh(t,n),i=Ca(e)-o,c=0;if(r)for(let a=0;a<=Math.min(i,s.length-1);a++)c+=s[a];else for(let a=Math.max(i+1,0);a<s.length;a++)c+=s[a];return Math.min(c,1)}var Vc=new Map;function gh(e,t){let n=`${e},${t}`,r=Vc.get(n);if(r!==void 0)return r;let o=e+t,s=0;for(let m=1;m<=o;m++)s+=Math.min(m,o+1-m);let i=Array.from({length:e+1},()=>new Float64Array(s+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(s+1));for(let p=0;p<=e;p++){if(e-p>o-m)continue;let g=i[p];for(let w=0;w<=s;w++){let b=g[w];b!==0&&(p<e&&(h[p+1][w+u]+=b),e-p<o-m&&(h[p][w]+=b))}}i=h}let c=i[e],a=0;for(;c[a]===0;)a++;let l=c.length-1;for(;c[l]===0;)l--;let f={min:a,counts:c.slice(a,l+1)};return Vc.set(n,f),f}function Eo(e,t,n){let{min:r,counts:o}=gh(t,n);if(e<=0)return r;if(e>=1)return r+o.length-1;let s=Ga(t+n,t),i=0;for(let c=0;c<o.length;c++)if(i+=o[c]/s,i>=e)return r+c;return r+o.length-1}var wh=["alternative","exact","confInt","confLevel"];function jc(e,t,n={}){if(C(n,wh,"ansariTest"),typeof e=="string"){let d=Yt(e,t);return jc(d.x,d.y,n)}let r=Array.from(e).filter(d=>!Number.isNaN(d)),o=Array.from(t).filter(d=>!Number.isNaN(d)),s=r.length,i=o.length;if(s<1)throw new RangeError("not enough 'x' observations");if(i<1)throw new RangeError("not enough 'y' observations");let c=le(n.alternative),a=n.confInt??!1,l=n.confLevel??.95;a&&Xe(l);let f=s+i,m=ye(r.concat(o)),u=m.map(d=>Math.min(d,f-d+1)),h=0;for(let d=0;d<s;d++)h+=u[d];let p=new Set(m).size<f,g=n.exact??(s<50&&i<50&&!p),w=n.exact??(s<50&&i<50),b=new ke,N,v;if(g&&!p){$c(s,i);let d=x=>Vs(x-1,s,i,!1),M=x=>Vs(x,s,i,!0);if(c==="two.sided"){let x=Math.floor((s+1)**2/4)+Math.floor(s*i/2)/2;N=Math.min(1,2*(h>x?d(h):M(h)))}else c==="less"?N=d(h):N=M(h);a&&(v=Mh(r,o,c,l,b))}else{let d=Uc(h,u,p,s,i);N=c==="two.sided"?2*Math.min(j(d),j(d,{lowerTail:!1})):c==="less"?j(d,{lowerTail:!1}):j(d),a&&(v=yh(r,o,c,l,w?new ke:b)),w&&p&&(b.add("cannot compute exact p-value with ties"),a&&b.add("cannot compute exact confidence interval with ties"))}let y={method:"Ansari-Bradley test",alternative:c,statistic:h,statisticName:"AB",pValue:N,warnings:b.list};return v!==void 0&&(y.confInt=v.confInt,y.confLevel=l,y.estimate=v.estimate,y.estimateNames="ratio of scales"),y.nullValue=1,y.nullValueName="ratio of scales",z("ansariTest",y)}function Uc(e,t,n,r,o){let s=r+o;if(!n){let l=s%2===0,f=l?e-r*(s+2)/4:e-r*(s+1)**2/(4*s),m=Math.sqrt(l?r*o*(s+2)*(s-2)/(48*(s-1)):r*o*(s+1)*(3+s*s)/(48*s*s));return f/m}let i=0;for(let l of t)i+=l;let c=i/s,a=0;for(let l of t)a+=(l-c)**2;return(e-r*c)/Math.sqrt(r*o*(a/(s-1))/s)}function Gc(e,t,n){let r=e.length,o=r+t.length,s=e.map(f=>f/n),c=n>0&&n<1/0&&s.some((f,m)=>Number.isFinite(e[m])&&(!Number.isFinite(f)||e[m]!==0&&Math.abs(f)<bh))?vh(e.map(f=>zc(f,n)).concat(t.map(f=>zc(f,1)))):Nh(s.concat(t)),a=c.map(f=>Math.min(f,o-f+1)),l=0;for(let f=0;f<r;f++)l+=a[f];return{ab:l,a,ties:new Set(c).size<o}}var bh=2**-1022;function zc(e,t){if(e===0)return[0,0,0];let n=e>0?1:-1;if(!Number.isFinite(e))return[n,1/0,1];let[r,o]=Kc(Math.abs(e)),[s,i]=Kc(t),c=o/i,a=r-s;return c<1&&(c*=2,a-=1),[n,a,c]}function Kc(e){let t=Math.floor(Math.log2(e)),n=_(e,-t);return n>=2?(n/=2,t+=1):n<1&&(n*=2,t-=1),[t,n]}function Wc(e,t){if(e[0]!==t[0])return e[0]-t[0];let n=e[1]!==t[1]?e[1]-t[1]:e[2]-t[2];return e[0]*n}function vh(e){let t=e.map((r,o)=>o).sort((r,o)=>Wc(e[r],e[o])),n=new Array(e.length);for(let r=0;r<t.length;){let o=r+1;for(;o<t.length&&Wc(e[t[r]],e[t[o]])===0;)o++;for(let s=r;s<o;s++)n[t[s]]=(r+1+o)/2;r=o}return n}function Nh(e){let t=e.filter(s=>!Number.isNaN(s));if(t.length===e.length)return ye(e);let n=ye(t),r=0,o=t.length;return e.map(s=>Number.isNaN(s)?++o:n[r++])}function xo(e,t,n){return e==="two.sided"?n(t):e==="greater"?[n(2*t)[0],1/0]:[0,n(2*t)[1]]}function Mh(e,t,n,r,o){let s=e.length,i=t.length,c=[];for(let b of e)for(let N of t)b/N>=0&&c.push(b/N);c.sort((b,N)=>b-N);let a=c.length;if(a===0)return o.add("cannot compute confidence set, returning NA"),{confInt:xo(n,1-r,()=>[NaN,NaN]),estimate:NaN};let l=c.map((b,N)=>Gc(e,t,b+(N<a-1?(c[N+1]-b)/2:b*1.01)).ab),f=(b,N,v)=>{let y=1/0;for(let M of l)(N?M-b>0:M-b>=0)&&M-b<y&&(y=M-b);let d=v?-1/0:1/0;return l.forEach((M,x)=>{M-b===y&&(d=v?Math.max(d,c[x]):Math.min(d,c[x]))}),d},m=b=>{let N=Eo(b/2,s,i),v=Eo(1-b/2,s,i);if(!l.some(M=>M-N>=0)||!l.some(M=>M-v>0))return o.add("samples differ in location: cannot compute confidence set, returning NA"),[NaN,NaN];let y=f(N,!1,!1),d=f(v,!0,!0);return y>d?[f(v,!1,!1),f(N,!0,!0)]:[y,d]},u=xo(n,1-r,m),h=Eo(.5,s,i),p=-1/0,g=1/0;l.forEach((b,N)=>{b-h<=0?p=Math.max(p,c[N]):g=Math.min(g,c[N])});let w=p===-1/0||g===1/0?NaN:(g+p)/2;return w===1/0&&(w=g/2+p/2),{confInt:u,estimate:w}}function yh(e,t,n,r,o){let s=e.length,i=t.length,c=(b,N)=>{let{ab:v,a:y,ties:d}=Gc(e,t,b);return Uc(v,y,d,s,i)-N},a=[],l=(b,N)=>{b.length===0||N.length===0||a.push(it(b)/ve(N),ve(b)/it(N))};if(l(e.filter(b=>b>0),t.filter(b=>b>0)),l(e.filter(b=>b<=0),t.filter(b=>b<0)),a.some(b=>!Number.isFinite(b))){o.add("cannot compute asymptotic confidence set or estimator");return}if(a.length===0)return o.add("samples differ in location: cannot compute confidence set, returning NA"),{confInt:xo(n,1-r,()=>[NaN,NaN]),estimate:NaN};let f=it(a),m=ve(a),u=b=>We(N=>c(N,b),f,m,{tol:1e-4}),h=b=>{let N=Z(b/2),v=Z(b/2,{lowerTail:!1});if(c(f,N)>0||c(m,v)<0)return o.add("samples differ in location: cannot compute confidence set, returning NA"),[NaN,NaN];let y=u(N),d=u(v);return y<=d?[y,d]:[d,y]},p=xo(n,1-r,h),g=c(f,0)>0||c(m,0)<0;g&&o.add("cannot compute estimate, returning NA");let w=g?NaN:u(0);return{confInt:p,estimate:w}}var Eh=["alternative","confLevel"];function xh(e,t,n={}){C(n,Eh,"brunnerMunzelTest");let r=Array.from(e).filter(E=>!Number.isNaN(E)),o=Array.from(t).filter(E=>!Number.isNaN(E)),s=r.length,i=o.length;if(s<2)throw new RangeError("not enough 'x' observations");if(i<2)throw new RangeError("not enough 'y' observations");let c=le(n.alternative),a=n.confLevel??.95;Ht(a);let l=ye(r.concat(o)),f=l.slice(0,s),m=l.slice(s),u=E=>E.reduce((T,R)=>T+R,0)/E.length,h=u(f),p=u(m),g=(E,T,R)=>{let S=(E.length+1)/2,k=0;for(let A=0;A<E.length;A++)k+=(E[A]-T[A]-R+S)**2;return k/(E.length-1)},w=g(f,ye(r),h),b=g(m,ye(o),p),N=(p-(i+1)/2)/s,v=s*w+i*b,y=s*i*(p-h)/(s+i)/Math.sqrt(v),d=v**2/((s*w)**2/(s-1)+(i*b)**2/(i-1)),M;c==="greater"?M=De(y,d,!0,!1):c==="less"?M=De(y,d,!1,!1):M=2*De(-Math.abs(y),d,!0,!1);let x=st((1-a)/2,d,!1,!1)*Math.sqrt(w/(s*i**2)+b/(i*s**2));return{method:"Brunner-Munzel Test",alternative:c,statistic:y,statisticName:"Brunner-Munzel Test Statistic",parameter:d,parameterName:"df",pValue:M,confInt:[N-x,N+x],confLevel:a,estimate:N,estimateNames:"P(X<Y)+.5*P(X=Y)",warnings:[]}}var Th=[1.2337005501361697,7831619385924639e-32];function Rh(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 g=f[h*t+p];if(g!==0)for(let w=0;w<t;w++)u[h*t+w]+=g*m[p*t+w]}return u},o=f=>{let m=0;for(let p of f)m=Math.max(m,Math.abs(p));if(m===0||!Number.isFinite(m))return 0;let u=Math.floor(Math.log2(m));if(Math.abs(u)<500)return 0;let h=2**-u;for(let p=0;p<f.length;p++)f[p]*=h;return u},s,i=0,c=e.slice(),a=0,l=n;for(;l>0;)l&1&&(s=s===void 0?c.slice():r(s,c),i+=a,i+=o(s)),l>>=1,l>0&&(c=r(c,c),a*=2,a+=o(c));return[s,i]}var Sh=2**33;function Hc(e,t){if(Number.isNaN(t)||!(e>=1)||e!==Math.floor(e)||t<=1/(2*e)||t>=1||e*t<1)return;let n=2*Math.floor(e*t)+1,r=e.toString(2),o=Math.max(1,r.length-1+(r.split("1").length-2));if(n**3*o>Sh)throw new RangeError(`the exact distribution of D for n = ${e} needs powers of a ${n} x ${n} matrix, too much work: use exact: false`)}function Xc(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 g=1;g<=e;g++)h*=g/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,s=new Float64Array(r*r);for(let f=0;f<r;f++)for(let m=0;m<r;m++)s[f*r+m]=f-m+1>=0?1:0;for(let f=0;f<r;f++)s[f*r]-=Math.pow(o,f+1),s[(r-1)*r+f]-=Math.pow(o,r-f);2*o-1>0&&(s[(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;s[f*r+m]/=h}}let[i,c]=Rh(s,r,e),a=i[(n-1)*r+n-1],l=c;for(let f=1;f<=e;f++)a=a*f/e,Math.abs(a)<2**-500&&(a*=2**500,l-=500);for(;l>1e3;)a*=2**1e3,l-=1e3;for(;l<-1e3;)a*=2**-1e3,l+=1e3;return a*2**l}function $s(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(Th,xt(e,e)),s=o[0]+o[1],i=Math.exp(-o[0])*(1-o[1]),c=0;for(let l=2;l<100;l++){let f=Math.exp(-(2*l-1)*(2*l-1)*s);if(c+=f,f<=1e-17*(c+i))break}if(i+c===0)return t?0:1;let a=Math.sqrt(2*Math.PI)/e*(i+c);return t?a:1-a}let n=0;for(let o=1;o<100;o++){let s=Math.exp(-2*o*o*e*e);if(n+=o%2===1?s:-s,s<=1e-17*Math.abs(n))break}let r=2*n;return t?1-r:r}function zs(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),s=0;for(let i=0;i<=o;i++){let c=G(He(e-i,-n),[-r,0]);if(c[0]<=0)break;let a=G(He(i,n),[r,0]),l=ge(a,[e,0]),f=ge(c,[e,0]);s+=Fa(i,e-i,l,f,!1)/(l[0]+l[1])}return Math.min(t*s,1)}var Yc=2**28;function Bc(e,t){let[n,r]=xt(e,t),o=Math.floor(n);o===n&&r<0&&(o-=1);let s=[];for(let h=0;h<=e;h++){let p=(h-n-r)/e;p>0&&p<1&&s.push({t:p,up:!0,i:h});let g=(h+n+r)/e;g>0&&g<1&&s.push({t:g,up:!1,i:h})}if(s.length*(2*o+1)*Math.min(e+1,100)>Yc)return NaN;s.sort((h,p)=>h.t-p.t);let i=o+1,c=new Float64Array(e+1),a=new Float64Array(e+1);c[0]=1;let l=0,f=0,m=0,u=0;for(let h of s){let p=(h.t-f)/(1-f);if(p>0){let g=p/(1-p);a.fill(0);for(let w=l;w<i;w++){let b=c[w];if(b===0)continue;let N=e-w,v=Math.min(N,Math.floor((N+1)*p)),y=i-w,d=ht(v,N,p,!1);for(let M=v;M<=N&&d!==0;M++)M>v&&(d=d*(N-M+1)*g/M),M>=y?m+=b*d:a[w+M]+=b*d,u++;d=v>0?ht(v-1,N,p,!1):0;for(let M=v-1;M>=0&&d!==0;M--)M<v-1&&(d=d*(M+1)/((N-M)*g)),M>=y?m+=b*d:a[w+M]+=b*d,u++}if(u>2*Yc)return NaN;[c,a]=[a,c]}if(f=h.t,h.up)i=h.i+1;else{for(let g=l;g<=h.i;g++)m+=c[g],c[g]=0;l=Math.max(l,h.i+1)}}return Math.min(m,1)}function Qc(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 s=t+n,i;if(r.z!==void 0){if(r.z.length!==s)throw new RangeError("z must have length m + n");let u=[...r.z].sort((h,p)=>h-p);i=new Array(s+1).fill(!1);for(let h=1;h<s;h++)i[h]=u[h]!==u[h-1]}let c=e*t*n*(1-1e-10),a=(u,h)=>{let p=u*n-h*t;return(o==="two.sided"?Math.abs(p):o==="greater"?p:-p)>=c},l=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,g=(u>0?u/p*l[h]:0)+(h>0?h/p*f[h-1]:0);(i===void 0?p<s:i[p])&&a(u,h)?(g>0&&(m+=g*Nt(u,t,n,p,!1)),f[h]=0):f[h]=g}[l,f]=[f,l],f.fill(0)}return Math.min(m,1)}var kh=["alternative","exact"];function Zc(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 Jc(e,t,n={}){if(C(n,kh,"ksTest"),typeof e=="string"){let N=Yt(e,t);return Jc(N.x,N.y,n)}let r=Array.from(e).filter(N=>!Number.isNaN(N)).sort((N,v)=>N-v),o=r.length;if(o<1)throw new RangeError("not enough 'x' data");let s=le(n.alternative),i=s==="two.sided"?"D":s==="greater"?"D^+":"D^-";if(typeof t=="function"){let N=-1/0,v=-1/0;for(let E=0;E<o;E++){let T=t(r[E]);if(!(T>=0&&T<=1))throw new RangeError(`the distribution function returned ${T} at ${r[E]}, not a value in [0, 1]`);N=Math.max(N,(E+1)/o-T),v=Math.max(v,T-E/o)}let y=s==="two.sided"?Math.max(N,v):s==="greater"?N:v,d=new Set(r).size<o,M=n.exact??(o<100&&!d),x;if(M&&s==="two.sided"&&y<.5&&Hc(o,y),s==="two.sided")if(!M)x=$s(Math.sqrt(o)*y,!1);else{let E=2*zs(o,y);if(y>=.5)x=Math.min(1,E);else{let T=E<.01?Bc(o,y):NaN;x=Number.isNaN(T)?1-Xc(o,y):T}}else x=M?zs(o,y):Math.exp(-2*o*y*y);return z("ksTest",{method:`${M?"Exact":"Asymptotic"} one-sample Kolmogorov-Smirnov test`,alternative:Zc(s,!0),exact:M,statistic:y,statisticName:i,pValue:Math.min(1,Math.max(0,x)),warnings:d?["ties should not be present for the one-sample Kolmogorov-Smirnov test"]:[]})}let c=Array.from(t).filter(N=>!Number.isNaN(N)),a=c.length;if(a<1)throw new RangeError("not enough 'y' data");let l=r.map(N=>[N,0]).concat(c.map(N=>[N,1]));l.sort((N,v)=>N[0]-v[0]);let f=0,m=0,u=0,h=0;for(let N=0;N<l.length;N++){if(l[N][1]===0?f++:m++,N+1<l.length&&l[N+1][0]===l[N][0])continue;let v=f/o-m/a;u=Math.max(u,v),h=Math.max(h,-v)}let p=s==="two.sided"?Math.max(u,h):s==="greater"?u:h,g=new Set(l.map(N=>N[0])).size<l.length,w=n.exact??o*a<1e4,b;if(w)b=Qc(p,o,a,g?{alternative:s,z:l.map(N=>N[0])}:{alternative:s});else{let N=o*a/(o+a);b=s==="two.sided"?$s(Math.sqrt(N)*p,!1):Math.exp(-2*N*p*p)}return z("ksTest",{method:`${w?"Exact":"Asymptotic"} two-sample Kolmogorov-Smirnov test`,alternative:Zc(s,!1),exact:w,statistic:p,statisticName:i,pValue:Math.min(1,Math.max(0,b)),warnings:g&&!w?["p-value will be approximate in the presence of ties"]:[]})}var Ah=["poolSd","paired","pAdjustMethod","alternative"],qh=["mu","paired","exact","correct","confLevel","tolRoot","pAdjustMethod","alternative"],Ih=["pAdjustMethod","correct"];function el(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,s=Cn(t);for(let c=0;c<r.length;c++){let a=s[c];if(typeof a=="number"&&Number.isNaN(a)||n&&Number.isNaN(r[c]))continue;let l=o.get(a);l===void 0&&o.set(a,l=[]),l.push(r[c])}let i=Array.from(o.keys()).sort(Nr);return{levels:i,groups:i.map(c=>o.get(c))}}function Ks(e,t,n){let r=[],o=[];for(let c=0;c<e-1;c++)for(let a=c+1;a<e;a++)r.push(t(a,c)),o.push([a-1,c]);let s=On(r,n),i=Array.from({length:e-1},()=>new Array(e-1).fill(NaN));return o.forEach(([c,a],l)=>i[c][a]=s[l]),i}function Ws(e,t,n,r,o=[]){return{method:e,pValue:n,rowNames:t.slice(1).map(Ve),colNames:t.slice(0,-1).map(Ve),pAdjustMethod:r,warnings:o}}function _h(e,t,n={}){C(n,Ah,"pairwiseTTest");let r=n.paired??!1,o=n.poolSd??!r;if(r&&o)throw new RangeError("pooling of SD is incompatible with paired tests");let s=le(n.alternative),i=n.pAdjustMethod??"holm",{levels:c,groups:a}=el(e,t,!r),l=c.length;if(l<2)throw new RangeError("there must be at least two groups");let f,m;if(o){m="t tests with pooled SD";let u=a.map(b=>b.length),h=Le(a),p=h.mean,g=0;for(let b of u)b>1&&(g+=b-1);yr(g,h.ssWithin);let w=Math.sqrt(h.ssWithin/g);f=(b,N)=>{let v=(p[b]-p[N])/(w*Math.sqrt(1/u[b]+1/u[N]));return s==="less"?De(v,g,!0,!1):s==="greater"?De(v,g,!1,!1):2*De(-Math.abs(v),g,!0,!1)}}else m=r?"paired t tests":"t tests with non-pooled SD",f=(u,h)=>we(()=>Xt(a[u],a[h],{paired:r,alternative:s})).pValue;return Ws(m,c,Ks(l,f,i),i)}function Lh(e,t,n={}){C(n,qh,"pairwiseWilcoxTest");let{pAdjustMethod:r,alternative:o,...s}=n,i=r??"holm",{levels:c,groups:a}=el(e,t,!(s.paired??!1)),l=c.length;if(l<2)throw new RangeError("there must be at least two groups");let f="",m=new ke,u=Ks(l,(h,p)=>{let g=we(()=>dr(a[h],a[p],{...s,...o===void 0?{}:{alternative:o}}));f=g.method;for(let w of g.warnings)m.add(w);return g.pValue},i);return z("pairwiseWilcoxTest",Ws(f,c,u,i,m.list))}function Oh(e,t,n={}){if(C(n,Ih,"pairwisePropTest"),e.length!==t.length)throw new RangeError("'x' and 'n' must have the same length");let r=[],o=[];e.forEach((m,u)=>{Number.isNaN(m)||Number.isNaN(t[u])||(r.push(m),o.push(t[u]))});let s=r.length;if(s<2)throw new RangeError("too few groups");let i=n.pAdjustMethod??"holm",c=n.correct??!0,a=new ke,l=Ks(s,(m,u)=>{let h=we(()=>vs([r[m],r[u]],[o[m],o[u]],{correct:c}));for(let p of h.warnings)a.add(p);return h.pValue},i),f=Array.from({length:s},(m,u)=>u+1);return z("pairwisePropTest",Ws("Pairwise comparison of proportions",f,l,i,a.list))}var Er=[0,0];function To(e){if(!(e[0]>0))return[Math.sqrt(e[0]),0];let t=Math.sqrt(e[0]),n=ie(e,xt(t,t));return He(t,n[0]/(2*t))}function Ro(e,t){return Array.from({length:t},(n,r)=>e===void 0?[1,0]:To([e[r],0]))}function Dn(e,t){return Pt(e,t,[]).fit}function Pt(e,t,n){let r=t.length,o=Math.min(e.length,r),s=e.map(f=>f.slice()),i=t.slice(),c=n.map(f=>f.slice()),a=[];for(let f=0;f<o;f++){let m=s[f],u=Er;for(let b=f;b<r;b++)u=G(u,ce(m[b],m[b]));if(u[0]===0){a.push(Er);continue}let h=To(u),p=m[f][0]<0?[-h[0],-h[1]]:h;m[f]=G(m[f],p);let g=ce(p,m[f]),w=b=>{let N=Er;for(let y=f;y<r;y++)N=G(N,ce(m[y],b[y]));let v=ge(N,g);for(let y=f;y<r;y++)b[y]=ie(b[y],ce(v,m[y]))};for(let b=f+1;b<e.length;b++)w(s[b]);w(i);for(let b of c)w(b);a.push([-p[0],-p[1]])}let l=new Array(o).fill(Er);for(let f=o-1;f>=0;f--){let m=i[f];for(let u=f+1;u<o;u++)m=ie(m,ce(s[u][f],l[u]));l[f]=ge(m,a[f])}return{fit:{qty:i,coef:l},qtv:c}}function Pn(e,t=0,n=e.length){let r=Er;for(let o=t;o<n;o++)r=G(r,ce(e[o],e[o]));return r}var je=new WeakMap;function ko(e){let t=(n,r)=>n.weights===void 0?Array.from(r):r.filter((o,s)=>n.weights[s]!==0);return ee(e.flatMap(n=>[t(n,n.fittedValues),t(n,n.residuals)])).k}function Ao(e,t){return e.map(n=>{if(n.rss>=Fh&&n.rss<1/0)return _(n.rss,-2*t);let r=0;return n.residuals.forEach((o,s)=>{let i=n.weights?.[s]??1;if(i===0)return;let c=_(o,-t);r+=i*c*c}),r})}var Fh=2**-1022;function Us(e,t){return e.y.flatMap((n,r)=>(e.weights?.[r]??1)===0?[]:[[e.weights?.[r]??1,_(n,-t)]])}function qo(e){let t=Math.floor(Math.log2(e.reduce((n,[r])=>Math.max(n,r),0)));return t-(t%2+2)%2}function gn(e,t,n=0){let r=i=>e.reduce((c,[a,l])=>c+_(a,-i)*l*l,0),o=n,s=r(o);return Number.isFinite(s)||(o=qo(e),s=r(o)),!(_(t,-o)>=1e-24*s)}function tl(e){let t=ko([e]),{pivot:n}=e.qr,r=[],o=new Map;for(let s=0;s<e.rank;s++){let i=e.assign[n[s]],c=o.get(i);c===void 0&&(c={term:i,df:0,ss:0},o.set(i,c),r.push(c)),c.df++,c.ss+=_(e.effects[s],-t)**2}return{terms:r,rdf:e.dfResidual,rss:Ao([e],t)[0],k:t}}var nl=(e,t,n)=>Me(e,t,n,!1,!1),Gs="essentially perfect fit for \u20181\u2019: summary may be unreliable";function Ys(e,t,n){if(!(n>0))return!1;let r=e.length,o=0;for(let a of e)o+=a;o/=r;let s=0;for(let a of e)s+=(a-o)**2;let i=r>1?s/(r-1):0,c=t/n;return Number.isFinite(c)&&c<(o*o+i)*1e-30}var Hs=new WeakMap;function Ch(e){let t=tl(e),n=t.terms.filter(o=>o.term!==0),r=0;return e.fittedValues.forEach((o,s)=>{let i=e.weights?.[s]??1;i!==0&&(r+=i*_(o,-t.k)**2)}),{names:n.map(o=>e.termLabels[o.term-1]),rows:n,rdf:t.rdf,rss:t.rss,k:t.k,perfect:t.rss<1e-10*r}}function js(e){let t=Ch(e),{names:n,rdf:r,k:o,perfect:s}=t,{rows:i,rss:c}=t;if(r>0){let g=Us(e,o);gn(g,c)&&(c=0,i=i.map(w=>gn(g,w.ss)?{...w,ss:0}:w))}let a=c/r,l=g=>_(g,2*o),f=i.map(({df:g,ss:w})=>{let b=w/g/a;return[g,l(w),l(w/g),b,nl(b,g,r)]});f.push([r,l(c),r===0?NaN:l(a),NaN,NaN]);let m=["Df","Sum Sq","Mean Sq","F value","Pr(>F)"],u=[...n,"Residuals"],h={colNames:m,rowNames:u,table:f,warnings:s?["ANOVA F-tests on an essentially perfect fit are unreliable"]:[]},p=i.map(({df:g,ss:w})=>[g,w]);return p.push([r,c]),je.set(h,{colNames:["Df","Sum Sq"],rowNames:u,table:p}),h}function Dh(e){let t=e.map(f=>f.dfResidual),n=ko(e),r=Ao(e,n),o=f=>_(f,2*n),s=0;t.forEach((f,m)=>{f<t[s]&&(s=m)});let i=r[s]/t[s],c=0;e[s].fittedValues.forEach((f,m)=>{let u=e[s].weights?.[m]??1;u!==0&&(c+=u*_(f,-n)**2)});let a=r[s]<1e-10*c,l=e.map((f,m)=>{if(m===0)return[t[0],o(r[0]),NaN,NaN,NaN,NaN];let u=t[m-1]-t[m],h=r[m-1]-r[m],[p,g]=u>0?[e[m-1],e[m]]:[e[m],e[m-1]],w=N=>N.filter((v,y)=>(p.weights?.[y]??1)!==0),b=u===0||!to(w(p.x),w(g.x))?NaN:h/u/i;return b<0&&(b=NaN),[t[m],o(r[m]),u,o(h),b,Number.isNaN(b)?NaN:nl(b,Math.abs(u),t[s])]});return{colNames:["Res.Df","RSS","Df","Sum of Sq","F","Pr(>F)"],rowNames:e.map((f,m)=>String(m+1)),table:l,warnings:a?["ANOVA F-tests on an essentially perfect fit are unreliable"]:[]}}function So(...e){let t=[];for(let n of e)for(let r of n??[])t.includes(r)||t.push(r);return t}function Xs(e,...t){let n=[e,...t];for(let c of n)if(typeof c!="object"||c===null||!Array.isArray(c.effects))throw new TypeError("anova() needs fits from lm() or aov() with a formula");if(n.length===1){let c=js(e),a={...c,warnings:So(e.warnings,c.warnings)},l=je.get(c);return l!==void 0&&je.set(a,l),z("anova",a,e)}if(n.some(c=>c.rows.length!==e.rows.length))throw new Error("models were not all fitted to the same size of dataset");let r=(c,a)=>c===a||c!==void 0&&a!==void 0&&c.length===a.length&&c.every((l,f)=>Object.is(l,a[f]));if(n.some(c=>!r(c.rows,e.rows)))throw new Error("models were not all fitted to the same rows of the data");let o=n.filter(c=>c.response===e.response);if(o.some(c=>!r(c.y,e.y)))throw new Error("models were not all fitted to the same response values");if(o.some(c=>!r(c.weights,e.weights)))throw new Error("models were not all fitted with the same weights");let s=[];if(o.length<n.length){let c=[...new Set(n.filter(a=>a.response!==e.response).map(a=>a.response))];s.push(`models with response ${c.map(a=>`\u2018${a}\u2019`).join(", ")} removed because response differs from model 1`)}let i=o.length===1?js(e):Dh(o);return i.warnings=So(...n.map(c=>c.warnings),s,i.warnings),z("anova",i,...n)}function Bs(e){if(typeof e=="object"&&e!==null&&e.multistratum===!0){let i=e,c=So(i.warnings,Hs.get(i)),a={strata:i.strata.filter(f=>f.stratum!=="Error: (Intercept)").map(f=>({...f})),warnings:c},l=je.get(i);return Array.isArray(l)&&je.set(a,l.filter((f,m)=>i.strata[m].stratum!=="Error: (Intercept)")),z("aovSummary",a,e)}if(typeof e!="object"||e===null||!Array.isArray(e.effects))throw new TypeError("aovSummary() needs a fit from aov() with a formula");let t=e,n=js(t),r=tl(t),o=Ys(t.fittedValues.map(i=>_(i,-r.k)),r.rss,r.rdf),s=So(t.warnings,n.warnings,o?[Gs]:[]);if(t.dfResidual>0){let i={...n,warnings:s},c=je.get(n);return c!==void 0&&je.set(i,c),z("aovSummary",i,e)}return z("aovSummary",{colNames:n.colNames.slice(0,3),rowNames:n.rowNames.slice(0,-1),table:n.table.slice(0,-1).map(i=>i.slice(0,3)),warnings:s},e)}var Ph=1e-14;function Vh(e,t,n,r,o,s,i,c){let a=M=>_(M,2*i),l=t.length,f=n.flatMap((M,x)=>Pn(M)[0]>Ph*s[x]?[x]:[]),m=0,u=[],h=t.slice(),p=new Array(l).fill(0);if(f.length>0){let M=Ee(Array.from({length:l},(E,T)=>f.map(R=>n[R][T][0])));m=M.rank,u=M.pivot.slice(0,m).map(E=>f[E]);let x=Dn(u.map(E=>n[E]),t);h=x.qty,u.forEach((E,T)=>{for(let R=0;R<l;R++)p[R]=p[R]+n[E][R][0]*x.coef[T][0]})}let g=[];u.forEach((M,x)=>{let E=r[M],T=g.find(R=>R.term===E);T===void 0&&g.push(T={term:E,df:0,ss:[0,0]}),T.df++,T.ss=G(T.ss,ce(h[x],h[x]))});let w=l-m,b=Pn(h,m)[0],N=Ys(p,b,w),v=g.filter(M=>M.term!==0);w>0&&gn(c,b)&&(b=0,v=v.map(M=>gn(c,M.ss[0])?{...M,ss:[0,0]}:M));let y=v.map(M=>o[M.term-1]),d=M=>{if(w>0){let x=b/w,E=v.map(({df:T,ss:R})=>{let S=R[0]/T/x;return[T,M(R[0]),M(R[0]/T),S,Me(S,T,w,!1,!1)]});return E.push([w,M(b),M(x),NaN,NaN]),{stratum:`Error: ${e}`,colNames:["Df","Sum Sq","Mean Sq","F value","Pr(>F)"],rowNames:[...y,"Residuals"],table:E}}return{stratum:`Error: ${e}`,colNames:["Df","Sum Sq","Mean Sq"],rowNames:y,table:v.map(({df:x,ss:E})=>[x,M(E[0]),M(E[0]/x)])}};return[d(a),d(M=>M),N]}function rl(e,t,n,r,o,s){if(s.weights!==void 0)throw new Error("weights are not supported in a multistratum aov() fit");let i=n??{type:"num",value:1,text:"1"},c=ei({response:t.response,rhs:i},Object.keys(o)).variables,a=ln(t,o,{...s,dropUnusedLevels:!0,contrastVariables:c},i),l=a.y;if(l===void 0)throw new TypeError("the formula has no response");if(l.some(I=>!Number.isFinite(I)))throw new RangeError("NA/NaN/Inf in 'y'");let f=ln({response:t.response,rhs:r},o,{...s.levels===void 0?{}:{levels:s.levels},allContrasts:"contr.helmert",intercept:a.terms.intercept,extraVariables:a.terms.variables,dropUnusedLevels:!0}),m=l.length,u=[...a.warnings,...f.warnings.filter(I=>!a.warnings.includes(I))],h=f.colNames.length,p=f.colNames.map((I,L)=>ee([f.x.map(O=>O[L])]).groups[0]),g=f.x.map((I,L)=>p.map(O=>O[L])),w=h>0?Ee(g):void 0,b=w?.rank??0;b<h&&u.push("Error() model is singular");let N=w?.pivot.slice(0,b)??[],v=I=>I.map(L=>[L,0]),y=a.colNames.map((I,L)=>v(ee([a.x.map(O=>O[L])]).groups[0])),{groups:d,k:M}=ee([l]),{fit:x,qtv:E}=Pt(N.map(I=>v(p[I])),v(d[0]),y),T=d[0].map(I=>[1,I]),R=y.map(I=>Pn(I)[0]),S=[];N.forEach((I,L)=>{let O=f.assign[I],P=O===0?"(Intercept)":f.terms.termLabels[O-1],W=S.find(D=>D.name===P);W===void 0&&S.push(W={name:P,idx:[]}),W.idx.push(L)}),m>b&&S.push({name:"Within",idx:Array.from({length:m-b},(I,L)=>b+L)});let k=S.map(I=>Vh(I.name,I.idx.map(L=>x.qty[L]),E.map(L=>I.idx.map(O=>L[O])),a.assign,a.terms.termLabels,R,M,T)),A=k.map(I=>I[0]),q={formula:e,multistratum:!0,strata:A,rows:a.frame.rows,warnings:u};return je.set(q,k.map(I=>I[1])),k.some((I,L)=>I[2]&&S[L].name!=="(Intercept)")&&Hs.set(q,[Gs]),q}function Io(e){let t=e.length,n=new Array(t).fill(0);for(let o=0;o<t;o++){let s=0;for(let i=0;i<=o;i++)s=Math.max(s,Math.abs(e[i][o]));s>0&&Number.isFinite(s)&&(s<2**-400||s>2**400)&&(n[o]=Math.floor(Math.log2(s)))}let r=n.every(o=>o===0)?e.map(o=>o.slice()):e.map(o=>o.map((s,i)=>_(s,-n[i])));return{inv:mi(r),e:n}}var $h=["weights","contrasts","levels","intercept"],zh=["level"],Kh=["interval","level"],Qs=2**-1022;function Zs(e,t,n){let r=t.y;if(r===void 0)throw new TypeError("the formula has no response");let o=il(t.x,r,t.frame.weights,t.terms.intercept,!0),s=o.result,i=[...t.warnings];if(!n){let a=o.yScale??0,l=o.fitRows.map(h=>_(s.fittedValues[h],-a)),f=l.reduce((h,p)=>h+p,0)/l.length,m=l.length>1?l.reduce((h,p)=>h+(p-f)**2,0)/(l.length-1):NaN,u=(o.rssScaled??o.rss)/s.dfResidual;Number.isFinite(u)&&u<1e-30*(f*f+m)&&i.push("essentially perfect fit: summary may be unreliable")}let c={...s,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&&(c.weights=t.frame.weights),c}function Js(e,t,n={}){return C(n,$h,"lm"),typeof e=="string"?z("lm",Zs(e,ln(Br(e),t,{...n,dropUnusedLevels:!0}),!1)):Wh(e,t,n)}function Wh(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]),s=Array.from(t);if(o.some(m=>m.length!==o[0].length))throw new TypeError("all rows of the matrix must have the same length");if(o.length!==s.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!==s.length)throw new RangeError("variable lengths differ (found for '(weights)')");let c=s.map((m,u)=>!Number.isNaN(m)&&o[u].every(h=>!Number.isNaN(h))&&(i===void 0||!Number.isNaN(i[u]))),a=i?.filter((m,u)=>c[u]);if(a?.some(m=>m<0))throw new RangeError("missing or negative weights not allowed");let l=o.filter((m,u)=>c[u]).map(m=>r?[1,...m]:m),f=s.filter((m,u)=>c[u]);return il(l,f,a,r).result}function jh(e,t,n,r,o,s,i){let c=s.rank,a=s.pivot.slice(0,c),l=n?.map(d=>_(d,-r)),f=Ro(l===void 0?void 0:o.map(d=>l[d]),o.length).map((d,M)=>{if(n===void 0||l===void 0)return d;let x=n[o[M]];if(x===0||l[o[M]]>=Qs)return d;let E=To([x,0]);return[_(E[0],-r/2),_(E[1],-r/2)]}),m=a.map(d=>{let M=o.map((E,T)=>Se(f[T],e[E][d])),x=ci(M.map(E=>E[0]));return{c:x===0?M:M.map(E=>[_(E[0],-x),_(E[1],-x)]),e:x}}),{qty:u,coef:h}=Dn(m.map(({c:d})=>d),o.map((d,M)=>Se(f[M],t[d]))),p=h.map((d,M)=>m[M].e===0?d:[_(d[0],-m[M].e),_(d[1],-m[M].e)]),g=e[0]?.map(()=>NaN)??[];a.forEach((d,M)=>g[d]=p[M][0]);let w=e.map(d=>{let M=[0,0];return a.forEach((x,E)=>M=G(M,Se(p[E],d[x]))),M}),b=Pn(u,c)[0],N=o.map(d=>l?.[d]??1),v=[0,0];if(i){let d=[0,0],M=[0,0];o.forEach((x,E)=>{d=G(d,[N[E],0]),M=G(M,Se(w[x],N[E]))}),v=ge(M,d)}let y=[0,0];return o.forEach((d,M)=>{let x=ie(w[d],v);y=G(y,Se(ce(x,x),N[M]))}),{coefficients:g,fittedValues:w.map(d=>d[0]),residuals:w.map((d,M)=>ie([t[M],0],d)[0]),effects:u.map(d=>d[0]),rss:b,mss:y[0]}}function il(e,t,n,r,o=!1){let s=ee([t.filter((l,f)=>n===void 0||n[f]!==0)]).k;if(s===0)return ol(sl(e,t,n,r,o),e,t,n);let i=sl(e,t.map(l=>_(l,-s)),n,r,o),c=i.result,a=l=>_(l,s);return c.coefficients=c.coefficients.map(a),c.residuals=c.residuals.map(a),c.fittedValues=c.fittedValues.map(a),c.sigma=a(c.sigma),c.coefTable.estimate=c.coefTable.estimate.map(a),c.coefTable.stdError=c.coefTable.stdError.map(a),ol({...i,effects:i.effects.map(a),rss:_(i.rss,2*s),yScale:s,rssScaled:i.rss},e,t,n)}function ol(e,t,n,r){if(r===void 0)return e;let o=e.result,s=o.coefficients.flatMap((i,c)=>Number.isNaN(i)?[]:[c]);return r.forEach((i,c)=>{if(i!==0)return;let a=!1;if(!Number.isFinite(o.fittedValues[c])){let l=t[c],f=s.reduce((m,u)=>m+l[u]*o.coefficients[u],0);o.fittedValues[c]=er(f,0)?tr(s.map(m=>l[m]),s.map(m=>o.coefficients[m]),f):f,a=!0}(a||!Number.isFinite(o.residuals[c]))&&(o.residuals[c]=n[c]-o.fittedValues[c])}),e}var cl=new WeakMap;function Uh(e){return e.qr.every(t=>t.every(Number.isFinite))&&e.qraux.every(Number.isFinite)}function sl(e,t,n,r,o){if(t.length===0)throw new RangeError("0 (non-NA) cases");let s=t.map((Q,J)=>n===void 0||n[J]!==0),i=e.filter((Q,J)=>s[J]),c=t.filter((Q,J)=>s[J]),a=n?.filter((Q,J)=>s[J]),l=c.length;if(l===0)throw new RangeError("all weights are zero");if(i.some(Q=>Q.some(J=>!Number.isFinite(J))))throw new RangeError("NA/NaN/Inf in 'x'");if(c.some(Q=>!Number.isFinite(Q)))throw new RangeError("NA/NaN/Inf in 'y'");if(a?.some(Q=>!Number.isFinite(Q)))throw new RangeError("non-finite weights not allowed");let f=t.flatMap((Q,J)=>s[J]?[J]:[]),m=a===void 0?1:ve(a),u=Number.isFinite(m)?2*Math.floor(Math.floor(Math.log2(m))/2):0,h=0,p=a?.map(Math.sqrt),g=p===void 0?i:i.map((Q,J)=>Q.map($=>$*p[J])),w=Ee(g);a!==void 0&&Number.isFinite(m)&&u+2>0&&!Uh(w)&&(h=u+2,p=a.map(Q=>_(Math.sqrt(Q),-h/2)),g=i.map((Q,J)=>Q.map($=>$*p[J])),w=Ee(g),cl.set(w,h));let b=p===void 0?c:c.map((Q,J)=>Q*p[J]),N=w.rank,v=Q=>_(Q,-u),y=o?jh(e,t,n,u,f,w,r):void 0,d=y?.coefficients??An(w,b),M=y?.residuals??[],x=y?.fittedValues??[],E=l-N,T=a?.map(v)??new Array(l).fill(1),R=y?.rss??0;if(y===void 0){let Q=qn(w,b),J=p===void 0?Q:Q.map((Y,B)=>Y/p[B]),$=0;t.forEach((Y,B)=>{if(s[B])M.push(J[$]),x.push(Y-J[$]),$++;else{let pe=e[B].reduce((Fe,tt,nt)=>Number.isNaN(d[nt])?Fe:Fe+tt*d[nt],0);x.push(pe),M.push(Y-pe)}});for(let Y=0;Y<l;Y++)R+=T[Y]*J[Y]**2}let S=x.filter((Q,J)=>s[J]),k=0;if(a!==void 0&&a.some(Q=>Q!==0&&_(Q,-u)<Qs)){let Q=f.flatMap((J,$)=>{let Y=M[J];return Y===0?[]:[{w:a[$],r:Y,a:Math.floor(Math.log2(Math.abs(Y)))}]});if(Q.length>0&&Q.every(J=>Number.isFinite(J.r))){let J=Math.max(...Q.map(B=>Math.log2(B.w)+2*B.a)),$=2*Math.floor(Math.floor(J)/2),Y=0;for(let B of Q)Y+=_(B.w,2*B.a-$)*_(B.r,-B.a)**2;R=Y,k=$-u}}let A=R/E,q=_(Math.sqrt(A),(u+k)/2),I=h===0?0:(h-u)/2,L=w.qr.slice(0,N).map(Q=>Q.slice(0,N).map(J=>h===0?_(J,-u/2):J)),{inv:O,e:P}=N>0?Io(L):{inv:[],e:[]},D=w.pivot.slice(0,N).map((Q,J)=>[Q,J]).sort((Q,J)=>Q[0]-J[0]),F=D.map(([Q])=>d[Q]),K=D.map(([,Q])=>_(Math.sqrt(O[Q][Q]*A),k/2-P[Q]-I)),X=F.map((Q,J)=>Q/K[J]),te=X.map(Q=>2*De(-Math.abs(Q),E,!0,!1)),ne={coefficients:d,residuals:M,fittedValues:x,rank:N,dfResidual:E,coefTable:{estimate:F,stdError:K,tValue:X,pValue:te},sigma:q,rSquared:0,adjRSquared:0,intercept:r,qr:w},fe=r?1:0,Te=N-fe,Oe={result:ne,fitRows:f,effects:(y?.effects??[]).map(Q=>_(Q,u/2)),rss:_(R,u+k)};if(Te===0)return Oe;let Ue=0;if(y!==void 0)Ue=y.mss;else if(r){let Q=T.reduce(($,Y)=>$+Y,0),J=0;for(let $=0;$<l;$++)J+=T[$]*S[$]/Q;for(let $=0;$<l;$++)Ue+=T[$]*(S[$]-J)**2}else for(let Q=0;Q<l;Q++)Ue+=T[Q]*S[Q]**2;ne.rSquared=Ue/(Ue+_(R,k)),ne.adjRSquared=1-(1-ne.rSquared)*((l-fe)/E);let Mt=_(Ue/Te/A,-k);return ne.fStatistic={value:Mt,numdf:Te,dendf:E},ne.fPValue=Me(Mt,Te,E,!1,!1),Oe}function ll(e,t){let n=(1-e)/2;return n<1e-8?st(n,t,!1,!1):st((1+e)/2,t,!0,!1)}function Gh(e,t={}){C(t,zh,"confint");let n=t.level??.95;if(!(n>0&&n<1))throw new RangeError("'level' must be between 0 and 1");let r=ll(n,e.dfResidual),{estimate:o,stdError:s}=e.coefTable,i=e.coefficients.map(()=>NaN),c=e.coefficients.map(()=>NaN),a=0;return e.coefficients.forEach((l,f)=>{Number.isNaN(l)||(i[f]=o[a]-r*s[a],c[f]=o[a]+r*s[a],a++)}),{lower:i,upper:c}}function al(e,t,n){if(t===0||!Number.isFinite(t))return e*t;let r=Math.floor(Math.log2(e)),o=Math.floor(Math.log2(Math.abs(t)));return _(_(e,-r)*_(t,-o),r+o+n)}function Yh(e,t,n={}){C(n,Kh,"predict");let o=(t.length>0&&typeof t[0]!="number"?t.map(v=>Array.from(v)):Array.from(t,v=>[v])).map(v=>e.intercept?[1,...v]:v);if(o.some(v=>v.length!==e.coefficients.length))throw new RangeError("newdata must have one column per predictor of the fit");let s=e.rank,i=e.qr.pivot.slice(0,s),c=e.qr.qr.slice(0,s).map(v=>v.slice(0,s)),a=-(cl.get(e.qr)??0)/2,l=e.coefficients.flatMap((v,y)=>Number.isNaN(v)?[]:[y]),f=o.map(v=>{let y=v.reduce((d,M,x)=>Number.isNaN(e.coefficients[x])?d:d+M*e.coefficients[x],0);return er(y,0)?tr(l.map(d=>v[d]),l.map(d=>e.coefficients[d]),y):y}),m=v=>{let y=new Array(s).fill(0);for(let d=0;d<s;d++){let M=v[d];for(let x=0;x<d;x++)M-=c[x][d]*y[x];y[d]=M/c[d][d]}return y},u=o.map(v=>{let y=i.map(x=>v[x]),d=Yo(m(y));if(d>=Qs&&d<1/0||!y.every(Number.isFinite)||y.every(x=>x===0))return a===0||!(d>0&&d<1/0)?d*e.sigma:al(d,e.sigma,a);let M=Math.floor(Math.log2(ve(y.map(Math.abs))));for(let x of[0,600]){let E=Yo(m(y.map(T=>_(T,-(M+x)))));if(E<1/0)return E===0?E*e.sigma:al(E,e.sigma,M+x+a)}return d*e.sigma}),h=n.interval??"none";if(h!=="none"&&h!=="confidence"&&h!=="prediction")throw new RangeError(`invalid 'interval': ${String(h)}`);let p=eo(e.qr,e.rank,o),g=p.length>0?{nonEstimable:p,warnings:[Jr]}:{};if(h==="none")return z("predict",{fit:f,seFit:u,...g},e);let w=n.level??.95;if(!(w>0&&w<1))throw new RangeError("'level' must be between 0 and 1");let b=ll(w,e.dfResidual),N=u.map(v=>b*(h==="prediction"?Math.hypot(v,e.sigma):v));return z("predict",{fit:f,seFit:u,lwr:f.map((v,y)=>v-N[y]),upr:f.map((v,y)=>v+N[y]),...g},e)}var Hh=["weights","contrasts","levels"],fl=["confLevel"],ml=["sigmaX","sigmaY","mu","alternative","confLevel"];function Xh(e,t,n={}){return C(n,Hh,"aov"),typeof e=="string"?z("aov",ul(e,t,n)):z("aov",Bh(e,t))}function ul(e,t,n){let r=Br(e,!0),{treatment:o,errors:s}=Ja(r.rhs);if(s.length>1)throw new Error(`there are ${s.length} Error terms: only 1 is allowed`);return s.length===1?rl(e,r,o,s[0],t,n):Zs(e,ln(r,t,{...n,dropUnusedLevels:!0},o),!0)}function Bh(e,t){let{labels:n,groups:r}=Jt(e,t);if(r.length<2)throw new RangeError("contrasts can be applied only to factors with 2 or more levels");let o=r.flat(),s=r.flatMap((i,c)=>i.map(()=>n[c]));return ul("y ~ g",{y:o,g:s},{levels:{g:n}})}function hl(e){let t=e.length;if(t<2)throw new RangeError("contrasts can be applied only to factors with 2 or more levels");let n=e.reduce((u,h)=>u+h.length,0),{ssBetween:r,ssWithin:o,k:s}=Le(e),i=t-1,c=n-t,a=r/i,l=o/c,f=a/l,m=u=>_(u,2*s);return{df:[i,c],sumSq:[m(r),m(o)],meanSq:[m(a),m(l)],fValue:f,pValue:c>0?Me(f,i,c,!1,!1):NaN}}function Qh(e,t,n={}){ze(t)&&C(t,fl,"TukeyHSD"),C(n,fl,"TukeyHSD");let r;t!==void 0&&!Array.isArray(t)?n=t:r=t;let{labels:o,groups:s}=Jt(e,r),i=s.length;if(i<2)throw new RangeError("contrasts can be applied only to factors with 2 or more levels");let c=n.confLevel??.95;Ht(c);let l=s.reduce((b,N)=>b+N.length,0)-i;if(l===0)throw new RangeError("no residual degrees of freedom");if(l<2)throw new RangeError("the studentized range needs at least 2 residual degrees of freedom");let{ssWithin:f,mean:m,k:u}=Le(s),h=f/l,p=Ba(c,i,l),g=[],w={names:[],i:[],j:[],diff:[],lwr:[],upr:[],pAdj:g,pAdjusted:g,warnings:[]};for(let b=0;b<i-1;b++)for(let N=b+1;N<i;N++){let v=m[N]-m[b],y=Math.sqrt(h/2*(1/s[N].length+1/s[b].length));w.names.push(`${o[N]}-${o[b]}`),w.i.push(N+1),w.j.push(b+1),w.diff.push(_(v,u)),w.lwr.push(_(v-p*y,u)),w.upr.push(_(v+p*y,u)),w.pAdj.push(Gr(Math.abs(v)/y,i,l,{lowerTail:!1}))}return w}function Zh(e,t,n={}){ze(t)&&C(t,ml,"zTest"),C(n,ml,"zTest");let r;t!==void 0&&!ArrayBuffer.isView(t)&&!Array.isArray(t)?n=t:r=t;let{sigmaX:o,sigmaY:s}=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&&s===void 0)throw new RangeError("sigmaX and sigmaY are both required for two samples");for(let R of[o,s])if(R!==void 0&&!(R>0&&R<1/0))throw new RangeError("sigmaX and sigmaY must be positive finite numbers");if(Number.isNaN(i))throw new RangeError("mu must be a number");let c=le(n.alternative),a=n.confLevel??.95;if(!(a>=0&&a<=1))throw new RangeError("confLevel must be between 0 and 1");let l=Array.from(e).filter(R=>!Number.isNaN(R)),f=r===void 0?void 0:Array.from(r).filter(R=>!Number.isNaN(R));if(l.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=ee([l,f??[],[o,s??0]]),u=m.k,[h,p,[g,w]]=m.groups,b=nr(h)[0],N=f===void 0?NaN:nr(p)[0],v=f===void 0?g/Math.sqrt(l.length):Math.sqrt(g**2/l.length+w**2/f.length),y=f===void 0?b:b-N,d=(y-_(i,-u))/v,M,x;if(c==="less")M=j(d),x=[-1/0,y+Z(a)*v];else if(c==="greater")M=j(d,{lowerTail:!1}),x=[y-Z(a)*v,1/0];else{M=2*j(-Math.abs(d));let R=(1-a)/2,S=(R<1e-8?Z(R,{lowerTail:!1}):Z(1-(1-a)/2))*v;x=[y-S,y+S]}x=[_(x[0],u),_(x[1],u)];let E=f!==void 0,T=(R,S)=>{if(!(u>0&&R.some((A,q)=>A!==0&&Math.abs(S[q])<2**-1022)))return _(nr(S)[0],u);let k=Math.max(ee([R]).k,0);return _(nr(k===0?R:R.map(A=>_(A,-k)))[0],k)};return{method:E?"Two-sample z-Test":"One-sample z-Test",alternative:c,statistic:d,statisticName:"z",pValue:M,confInt:x,confLevel:a,estimate:E?[T(l,h),T(f,p)]:T(l,h),estimateNames:E?["mean of x","mean of y"]:"mean of x",nullValue:i,nullValueName:E?"difference in means":"mean",warnings:[]}}var Jh=["family","link","weights","offset","start","epsilon","maxit","intercept"],ep=["family","link","variance"],pl=["power"],tp=["dispersion"],np=["type"],rp=["type","dispersion"],op=["dispersion"],sp=["newdata","newoffset","type","dispersion"],Vn=2**-52;function ap(e){let t=e.map(n=>`"${n}"`);return t.length===1?t[0]:`c(${t.join(", ")})`}function xr(e,t,n){return e.map((r,o)=>{let s=0;for(let i=0;i<r.length;i++)s+=r[i]*t[i];return s+n[o]})}function en(e,t,n,r){let o=0;for(let s=0;s<t.length;s++)r[s]!==0&&(o+=e.devContribution(t[s],n[s],r[s]));return o}var wl=2**-1022,_o=e=>e===0||Math.abs(e)>=wl&&Math.abs(e)<1/0;function dl(e,t,n,r){let o=e.variance(r),s=n**2,i=Math.sqrt(t*s/o);if(i>0&&i<1/0&&_o(s)&&s!==0&&_o(o)&&o!==0&&_o(t*s))return i;if(e.muPower!==void 0&&e.varPower!==void 0&&r>0){let{c,a}=e.muPower;return Math.sqrt(t)*Math.abs(c)*r**((2*a-e.varPower)/2)}return i}function ea(e,t,n,r){let o=(t-n)/r;if(Math.abs(r)>=wl&&Math.abs(r)<1/0)return o;if(e.muPower!==void 0&&n>0){let{c:s,a:i}=e.muPower,c=n**(i/2);return(t-n)/s/c/c}return o}function ip(e,t,n,r){let o=e.variance(n),s=(t-n)*Math.sqrt(r)/Math.sqrt(o);if(Number.isFinite(s)&&_o(o)&&o!==0||e.varPower===void 0||!(n>0))return s;let i=n**(e.varPower/4);return(t-n)/i*Math.sqrt(r)/i}var cp=.1;function bl(e,t){let{x:n,p:r,y:o,w:s,offset:i,fam:c,epsilon:a,maxit:l}=e,f=o.length;if(o.some((m,u)=>s[u]>0&&!Number.isFinite(m)))throw new RangeError("NA/NaN/Inf in 'y'");if(n.some((m,u)=>s[u]>0&&m.some(h=>!Number.isFinite(h))))throw new RangeError("NA/NaN/Inf in 'x'");return or(t,()=>{let m=(A,q)=>A.every(I=>c.etaOk(I))&&q.every(I=>c.muOk(I));if(r===0){let A=i.slice();if(!A.every(O=>c.etaOk(O)))throw new Error("invalid linear predictor values in empty model");let q=A.map(O=>c.linkinv(O));if(!q.every(O=>c.muOk(O)))throw new Error("invalid fitted means in empty model");let I=A.map(O=>c.muEta(O)),L=q.map((O,P)=>dl(c,s[P],I[P],O)**2);return{coefficients:[],eta:A,mu:q,deviance:en(c,o,q,s),iter:0,converged:!0,boundary:!0,rank:0,qr:void 0,used:s.map(O=>O>0),workingWeights:L,workingResiduals:o.map((O,P)=>ea(c,O,q[P],I[P]))}}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 ${ap(e.columnNames)}`);h=e.start.slice(),u=xr(n,h,i)}else e.etastart!==void 0?u=e.etastart.slice():u=e.mustart.map(A=>c.link(A));let p=u.map(A=>c.linkinv(A));if(!m(u,p))throw new Error("cannot find valid starting values: please specify some");let g=en(c,o,p,s),w=g,b=!1,N=!1,v=0,y=new Array(r).fill(0),d,M=s.map(()=>!1),x=[],E=[],T=Math.min(1e-7,a/1e3);for(v=1;v<=l;v++){let A=u.map(D=>c.muEta(D));for(let D=0;D<f;D++){if(!(s[D]>0))continue;let F=c.variance(p[D]);if(Number.isNaN(F))throw new Error("NAs in V(mu)");if(F===0&&!(c.varPower!==void 0&&p[D]>0))throw new Error("0s in V(mu)");if(Number.isNaN(A[D]))throw new Error("NAs in d(mu)/d(eta)")}M=s.map((D,F)=>D>0&&A[F]!==0);let q=[];for(let D=0;D<f;D++)M[D]&&q.push(D);if(q.length===0){t.add(`no observations informative at iteration ${v}`),b=!1;break}x=new Array(f).fill(0);let I=[],L=[];for(let D of q){let F=dl(c,s[D],A[D],p[D]);x[D]=F,I.push((u[D]-i[D]+ea(c,o[D],p[D],A[D]))*F),L.push(n[D].map(K=>K*F))}d=Ee(L,{tol:T});let O=An(d,I);if(O.some((D,F)=>d.pivot.indexOf(F)<d.rank&&!Number.isFinite(D))){t.add(`non-finite coefficients at iteration ${v}`),b=!1;break}y=O.map(D=>Number.isNaN(D)?0:D),N=!1;let P=u;if(u=xr(n,y,i),p=u.map(D=>c.linkinv(D)),w=en(c,o,p,s),!Number.isFinite(w)){if(h===void 0)throw new Error("no valid set of coefficients has been found: please supply starting values");t.add("step size truncated due to divergence");let D=0;for(;!Number.isFinite(w);){if(D>=l)throw new Error("inner loop 1; cannot correct step size");D++,y=y.map((F,K)=>(F+h[K])/2),u=xr(n,y,i),p=u.map(F=>c.linkinv(F)),w=en(c,o,p,s)}N=!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 D=0;for(;!m(u,p);){if(D>=l)throw new Error("inner loop 2; cannot correct step size");D++,y=y.map((F,K)=>(F+h[K])/2),u=xr(n,y,i),p=u.map(F=>c.linkinv(F))}N=!0,w=en(c,o,p,s)}let W=Math.max(a,1e-10);if(h!==void 0&&v>1&&(w-g)/(.1+Math.abs(w))>=W){t.add("step size truncated due to increasing deviance");let D=0;for(;(w-g)/(.1+Math.abs(w))>=W&&D<l;)D++,y=y.map((F,K)=>(F+h[K])/2),u=xr(n,y,i),p=u.map(F=>c.linkinv(F)),w=en(c,o,p,s);if((w-g)/(.1+Math.abs(w))>=W){t.add("inner loop 3; cannot correct step size"),b=!1;break}}if(e.untilCoefficients){let D=ve(y.map(Math.abs));if(h!==void 0&&y.every((F,K)=>Math.abs(F-h[K])<=8*Vn*D)){b=!0;break}if(E.some(F=>F.every((K,X)=>K===y[X]))){b=!0;break}E.push(y.slice()),E.length>16&&E.shift()}else if(Math.abs(w-g)/(Math.abs(w)+.1)<a){b=!0;let D=0;for(let F=0;F<f;F++)M[F]&&(D=Math.max(D,Math.abs(u[F]-P[F])));D>cp&&t.add("glm.fit: the linear predictor was still changing at convergence: the data may be separated (no finite maximum likelihood estimates)");break}g=w,h=y.slice()}v>l&&(v=l);let R=r,S=y.slice();if(d!==void 0){R=d.rank;for(let A=d.rank;A<r;A++)S[d.pivot[A]]=NaN}let k=u.map(A=>c.muEta(A));return{coefficients:S,eta:u,mu:p,deviance:w,iter:v,converged:b,boundary:N,rank:R,qr:d,used:M,workingWeights:x.map(A=>A*A),workingResiduals:o.map((A,q)=>ea(c,A,p[q],k[q]))}})}function vl(e){return e.length>0&&typeof e[0]!="number"?e.map(t=>Array.from(t)):Array.from(e,t=>[t])}function Nl(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*Vn||o<10*Vn)&&n.add("glm.fit: fitted probabilities numerically 0 or 1 occurred"),t.family==="poisson"&&e.mu.some(o=>o<10*Vn)&&n.add("glm.fit: fitted rates numerically 0 occurred"))}function $n(e,t,n,r={}){let o=_n(e.familySpec),s="start"in r?r.start:e.start,i=ui(o,e.yGiven,e.priorGiven,s!==void 0||r.etastart!==void 0,n);if(!i.weights.some(f=>f>0))throw new RangeError("all weights are zero");let c=r.extraOffset===void 0?e.offset:e.offset.map((f,m)=>f+r.extraOffset[m]),a=e.x.map(f=>t.map(m=>f[m])),l=bl({x:a,p:t.length,y:i.y,w:i.weights,offset:c,fam:o,epsilon:r.epsilon??e.epsilon,maxit:r.maxit??e.maxit,columnNames:t.map(f=>e.columnNames[f]),start:s,etastart:r.etastart,mustart:i.mustart,untilCoefficients:r.untilCoefficients},n);return Nl(l,o,n,t.length===0),{fit:l,fam:o,y:i.y,w:i.weights,size:i.size,offset:c}}function lp(e,t,n={}){C(n,Jh,"glm"),C(Wt(n,"family"),ep,"glm","family"),C(Wt(n,"family.link"),pl,"glm","family.link"),C(Wt(n,"link"),pl,"glm","link");let r=n.intercept??!0,o=vl(e);if(o.some(E=>E.length!==o[0].length))throw new TypeError("all rows of x must have the same length");let s=t.length>0&&typeof t[0]!="number",i=s?t.map(E=>Array.from(E)):Array.from(t),c=i.length;if(o.length!==c)throw new RangeError("variable lengths differ (found for 'x1')");let a=n.weights===void 0?void 0:Array.from(n.weights);if(a!==void 0&&a.length!==c)throw new RangeError("variable lengths differ (found for '(weights)')");let l=n.offset===void 0?void 0:Array.from(n.offset);if(l!==void 0&&l.length!==c)throw new RangeError("variable lengths differ (found for '(offset)')");let f=o.map((E,T)=>{let R=i[T];return!(Array.isArray(R)?R.some(k=>Number.isNaN(k)):Number.isNaN(R))&&E.every(k=>!Number.isNaN(k))&&!(a!==void 0&&Number.isNaN(a[T]))&&!(l!==void 0&&Number.isNaN(l[T]))}),m=o.filter((E,T)=>f[T]).map(E=>r?[1,...E]:E),u=i.filter((E,T)=>f[T]),h=u.length;if(h===0)throw new RangeError("0 (non-NA) cases");let p=a===void 0?new Array(h).fill(1):a.filter((E,T)=>f[T]);if(p.some(E=>E<0))throw new RangeError("negative weights not allowed");if(p.some(E=>!Number.isFinite(E)))throw new RangeError("non-finite weights not allowed");let g=l===void 0?new Array(h).fill(0):l.filter((E,T)=>f[T]);if(g.some(E=>!Number.isFinite(E)))throw new RangeError("the offset must be finite");let w=o.length>0?o[0].length:0,b=[...r?["(Intercept)"]:[],...Array.from({length:w},(E,T)=>`x${T+1}`)],N=b.length,v=typeof n.family=="object"?{...n.family}:{family:n.family??"gaussian"};n.link!==void 0&&(v.link=n.link);let y={x:m,columnNames:b,yGiven:u,priorGiven:p,offset:g,hasOffset:l!==void 0,intercept:r,familySpec:v,epsilon:n.epsilon??1e-8,maxit:Math.floor(n.maxit??25),start:n.start===void 0?void 0:Array.from(n.start)};if(!(y.epsilon>0&&y.epsilon<1/0))throw new RangeError("value of 'epsilon' must be > 0 and finite");if(!(y.maxit>0&&y.maxit<1/0))throw new RangeError("maximum number of iterations must be > 0");let d=!s&&fp(v)?ee([u]).k:0;if(d===0)return z("glm",gl(y));let M=E=>_(E,-d),x=gl({...y,yGiven:u.map(M),offset:g.map(M),start:y.start?.map(M)});return z("glm",mp(x,d,{...y,yScale:d}))}function gl(e){let{x:t,offset:n,intercept:r}=e,o=t.length,s=e.columnNames.length,i=new ke,c=Array.from({length:s},(w,b)=>b),{fit:a,fam:l,y:f,w:m,size:u}=$n(e,c,i),h;if(r&&e.hasOffset){let w=new ke,b=bl({x:t.map(()=>[1]),p:1,y:f,w:m,offset:n,fam:l,epsilon:e.epsilon,maxit:e.maxit,columnNames:["(Intercept)"],mustart:a.mu},w);Nl(b,l,w,!1),w.list.forEach(N=>i.add(N)),h=b.deviance,b.converged||i.add("fitting to calculate the null deviance did not converge -- increase 'maxit'?")}else if(r){let w=Math.floor(Math.log2(ve(m))),b=0,N=0;for(let y=0;y<o;y++){if(!(m[y]>0))continue;let d=_(m[y],-w);b+=d,N+=d*f[y]}let v=N/b;if(!Number.isFinite(v)){let y=0;for(let x=0;x<o;x++)m[x]>0&&(y=Math.max(y,Math.abs(f[x])));let d=Math.floor(Math.log2(y)),M=0;for(let x=0;x<o;x++)m[x]>0&&(M+=_(m[x],-w)*_(f[x],-d));v=_(M/b,d)}h=or(i,()=>en(l,f,f.map(()=>v),m))}else h=or(i,()=>en(l,f,n.map(w=>l.linkinv(w)),m));let p=o-m.filter(w=>w===0).length,g=hi(l,f,a.mu,m,u,a.deviance,i)+2*a.rank;return Ml(a.used,{coefficients:a.coefficients,fittedValues:a.mu,linearPredictors:a.eta,deviance:a.deviance,nullDeviance:h,aic:g,rank:a.rank,dfResidual:p-a.rank,dfNull:p-(r?1:0),iter:a.iter,converged:a.converged,boundary:a.boundary,y:f,priorWeights:m,weights:a.workingWeights,residuals:a.workingResiduals,family:l.family,link:l.name,warnings:i.list,qr:a.qr,model:e})}function fp(e){return(e.link??"identity")!=="identity"?!1:e.family==="gaussian"||e.family==="quasi"&&(e.variance??"constant")==="constant"}function mp(e,t,n){let r=s=>_(s,t),o={...e,coefficients:e.coefficients.map(r),fittedValues:e.fittedValues.map(r),linearPredictors:e.linearPredictors.map(r),deviance:_(e.deviance,2*t),nullDeviance:_(e.nullDeviance,2*t),aic:e.aic+2*e.priorWeights.filter(s=>s>0).length*t*Math.LN2,y:e.y.map(r),residuals:e.residuals.map(r),model:n};return Object.defineProperty(o,"scaled",{value:e,enumerable:!1}),Ml(ta.get(e),o)}var ta=new WeakMap;function Ml(e,t){return e!==void 0&&ta.set(t,e),t}function Rt(e){let t=e.scaled;return t===void 0?{fit:e,k:0}:{fit:t,k:e.model.yScale??0}}function Tr(e){return _n(e.model.familySpec)}function yl(e,t){let n=Rt(e),r=Rr(n.fit,t);return Tr(e).fixedDispersion?r:_(r,2*n.k)}function Rr(e,t){if(Tr(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===1/0?up(e):n/e.dfResidual}function up(e){let t=[];for(let s=0;s<e.weights.length;s++)e.weights[s]>0&&t.push(s);if(!t.every(s=>Number.isFinite(e.weights[s])&&Number.isFinite(e.residuals[s])))return 1/0;let n=ee([t.map(s=>e.residuals[s])]),r=ee([t.map((s,i)=>Math.sqrt(e.weights[s])*Math.abs(n.groups[0][i]))]),o=0;for(let s of r.groups[0])o+=s*s;return _(o/e.dfResidual,2*(n.k+r.k))}function hp(e){return e.qr===void 0||e.rank===0?{inv:[],e:[]}:Io(e.qr.qr.slice(0,e.rank).map(t=>t.slice(0,e.rank)))}function pp(e){if(!(e>=0&&e<1/0))throw new RangeError("'dispersion' must be a non-negative finite number");return e}function zn(e,t){let n=_(pp(e),-2*t);if(n===0?e===0:n>=2**-1022&&n<1/0)return{m:n,s:0};let r=Math.floor(Math.floor(Math.log2(e))/2);return{m:_(e,-2*r),s:r-t}}function na(e,t={}){C(t,tp,"glmSummary");let{fit:n,k:r}=Rt(e),o=new ke,s=Tr(n),i=t.dispersion===void 0&&!s.fixedDispersion,c=t.dispersion===void 0?void 0:zn(t.dispersion,r),a=c===void 0?Rr(n,o):c.m,l=c?.s??0,{inv:f,e:m}=hp(n),u=f.map((d,M)=>d.map((x,E)=>_(x,-m[M]-m[E]))),p=(n.qr===void 0?[]:n.qr.pivot.slice(0,n.rank)).map(d=>n.coefficients[d]),g=f.map((d,M)=>_(Math.sqrt(d[M]*a),-m[M])),w=l===0?p.map((d,M)=>d/g[M]):p.map((d,M)=>$e(d/Math.sqrt(f[M][M]*a),m[M]-l)),b=!i,N=w.map(d=>b?2*j(-Math.abs(d)):n.dfResidual>0?2*De(-Math.abs(d),n.dfResidual,!0,!1):NaN);i&&n.dfResidual===0&&(g.fill(NaN),w.fill(NaN));let v=d=>_(d,r),y=d=>_(d,2*r);return z("glmSummary",{dispersion:l===0?y(a):t.dispersion,estimate:p.map(v),stdError:l===0?g.map(v):f.map((d,M)=>$e(Math.sqrt(d[M]*a),l+r-m[M])),statistic:w,pValue:N,statisticName:b?"z value":"t value",covUnscaled:u,covScaled:l===0?u.map(d=>d.map(M=>y(M*a))):f.map((d,M)=>d.map((x,E)=>$e(x*a,2*(l+r)-m[M]-m[E]))),dfResidual:n.dfResidual,warnings:o.list},e)}function dp(e,...t){C(kn(t),np,"glmResiduals");let n=Sn(["type"],t).type??"deviance",r=Rt(e);return ra(r.fit,n).map(o=>_(o,r.k))}function ra(e,t){let n=Tr(e),{y:r,fittedValues:o,priorWeights:s}=e;switch(t){case"response":return r.map((i,c)=>i-o[c]);case"working":return e.residuals.slice();case"pearson":return r.map((i,c)=>ip(n,i,o[c],s[c]));case"deviance":return r.map((i,c)=>s[c]===0?0:Math.sign(i-o[c])*Math.sqrt(Math.max(n.devContribution(i,o[c],s[c]),0)));default:throw new RangeError(`unknown type of residuals "${String(t)}"`)}}function oa(e){let t=[],n=e.qr,r=[];if(n!==void 0&&e.rank>0){let i=n.qr.length,c=Array.from({length:i},(l,f)=>Array.from({length:e.rank},(m,u)=>f===u?1:0));r=li(n,c).map(l=>l.reduce((f,m)=>f+m*m,0))}let o=0,s=ta.get(e)??e.weights.map(i=>i>0);return e.priorWeights.forEach((i,c)=>{let a=s[c]?r[o++]??0:0;i>0&&t.push(a>1-10*Vn?1:a)}),t}function gp(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 wp(e,t={}){C(t,rp,"glmRstandard");let n=t.type??"pearson";if(n!=="pearson"&&n!=="deviance")throw new RangeError(`unknown type "${String(n)}" for glmRstandard`);let{fit:r,k:o}=Rt(e),s=oa(r),{m:i,s:c}=t.dispersion===void 0?{m:Rr(r),s:0}:zn(t.dispersion,o),a=Math.sqrt(i),l=ra(r,n).filter((m,u)=>r.priorWeights[u]>0),f=r.rank>0?100*Vn*gp(l.map(Math.abs)):0;return l.map((m,u)=>{let h=$e((Math.abs(m)<f?0:m)/(a*Math.sqrt(1-s[u])),-c);return Number.isFinite(h)?h:NaN})}function bp(e,t={}){C(t,op,"glmCooksDistance");let{fit:n,k:r}=Rt(e),o=oa(n),{m:s,s:i}=t.dispersion===void 0?{m:Rr(n),s:0}:zn(t.dispersion,r);return ra(n,"pearson").filter((a,l)=>n.priorWeights[l]>0).map((a,l)=>{let f=o[l]/(s*n.rank),m=(a/(1-o[l]))**2*f;return(!Number.isFinite(m)||f<2**-1022)&&(m=vp(a,o[l],s,n.rank)),i!==0&&(m=$e(m,-2*i)),Number.isFinite(m)?m:NaN})}function vp(e,t,n,r){if(!Number.isFinite(e)||!(n>0&&Number.isFinite(n)))return NaN;let o=e===0?0:Math.floor(Math.log2(Math.abs(e))),s=Math.floor(Math.log2(n)),i=_(e,-o)/(1-t);return _(i*i*(t/(_(n,-s)*r)),2*o-s)}function Np(e,t={}){C(t,sp,"glmPredict");let{fit:n,k:r}=Rt(e),o=N=>_(N,r),s=Tr(n),i,c;if(t.newdata===void 0)i=n.model.x,c=n.linearPredictors.slice();else{if(i=vl(t.newdata).map(y=>n.model.intercept?[1,...y]:y),i.some(y=>y.length!==n.coefficients.length))throw new RangeError("newdata must have one column per predictor of the fit");let N=t.newoffset===void 0?i.map(()=>0):Array.from(t.newoffset,y=>_(y,-r));if(N.length!==i.length)throw new RangeError("newoffset must have one value per row of newdata");let v=n.coefficients.flatMap((y,d)=>Number.isNaN(y)?[]:[d]);c=i.map((y,d)=>{let M=y.reduce((E,T,R)=>Number.isNaN(n.coefficients[R])?E:E+T*n.coefficients[R],0);return(er(M,0)?tr(v.map(E=>y[E]),v.map(E=>n.coefficients[E]),M):M)+N[d]})}let{m:a,s:l}=t.dispersion===void 0?{m:Rr(n),s:0}:zn(t.dispersion,r),f=N=>_(N,r+l),m=n.rank,u=n.qr===void 0?[]:n.qr.pivot.slice(0,m),h=n.qr?.qr,p=i.map(N=>{let v=new Array(m).fill(0),y=0;for(let d=0;d<m;d++){let M=N[u[d]];for(let x=0;x<d;x++)M-=h[x][d]*v[x];v[d]=M/h[d][d],y+=v[d]*v[d]}if(!(y>=2**-1022&&y<1/0)){let d=v.reduce((M,x)=>Math.max(M,Math.abs(x)),0);if(d>0&&Number.isFinite(d)){let M=Math.sqrt(v.reduce((E,T)=>E+(T/d)**2,0)),x=d*M*Math.sqrt(a);return Number.isFinite(x)?x:d*(M*Math.sqrt(a))}}return Math.sqrt(y)*Math.sqrt(a)}),g=t.type??"link";if(g!=="link"&&g!=="response")throw new RangeError(`invalid 'type': ${String(g)}`);let w=t.newdata===void 0?[]:eo(n.qr,m,i),b=w.length>0?{nonEstimable:w,warnings:[Jr]}:{};return g==="link"?z("glmPredict",{fit:c.map(o),seFit:p.map(f),...b},e):z("glmPredict",or(new ke,()=>({fit:c.map(N=>o(s.linkinv(N))),seFit:p.map((N,v)=>f(N*Math.abs(s.muEta(c[v])))),...b})),e)}var Mp=["parm","level","test"],El={epsilon:1e-300,maxit:100,untilCoefficients:!0};function sa(e,t,n,r,o=1e-11){let s=[];for(let g=0;g<t.length;g++)n[g]>0&&s.push(g);let i=0,c=0;for(let g of s)i+=n[g],c+=n[g]*t[g];if(r&&!(Number.isFinite(i)&&Number.isFinite(c))){let g=0;for(let w of s)g=Math.max(g,n[w]);if(Number.isFinite(g)){let w=g>1?Math.floor(Math.log2(g)):0;i=0,c=0;for(let b of s){let N=_(n[b],-w);i+=N,c+=N*t[b]}}}let a=r?c/i:0,l=0;for(let g of s)l+=n[g]*(t[g]-a)**2;if((e.length>0?e[0].length:0)===0||s.length===0)return 0;let m=s.map(g=>Math.sqrt(n[g])),u=Ee(s.map((g,w)=>e[g].map(b=>b*m[w])),{tol:o}),h=qn(u,s.map((g,w)=>t[g]*m[w])),p=0;for(let g of h)p+=g*g;if(!Number.isFinite(l)){let g=s.map((b,N)=>m[N]*(t[b]-a)),w=0;for(let b of g)w=Math.max(w,Math.abs(b));for(let b of h)w=Math.max(w,Math.abs(b));if(Number.isFinite(w)){let b=w>1?Math.floor(Math.log2(w)):0,N=0;for(let y of g)N+=_(y,-b)**2;let v=0;for(let y of h)v+=_(y,-b)**2;return _(_(N-v,b),b)}}return l-p}function yp(e,t={}){C(t,Mp,"glmConfint");let{fit:n,k:r}=Rt(e),o=t.level??.95,s=t.test??"LRT";if(s!=="LRT"&&s!=="Rao")throw new RangeError(`invalid 'test': ${String(s)}`);if(!(o>0&&o<1))throw new RangeError("'level' must be in (0, 1)");let i=n.coefficients.length,c=t.parm??Array.from({length:i},(q,I)=>I);for(let q of c)if(!Number.isInteger(q)||q<0||q>=i)throw new RangeError(`invalid coefficient index ${q}`);let a=n.model,l=n.coefficients.map((q,I)=>I).filter(q=>!Number.isNaN(n.coefficients[q])),f=new ke,m=$n(a,l,f,{...El,start:a.start===void 0?void 0:l.map(q=>a.start[q])}),u=new Array(i).fill(NaN);if(l.forEach((q,I)=>u[q]=m.fit.coefficients[I]),!m.fit.converged)return{lower:c.map(()=>NaN),upper:c.map(()=>NaN)};let h=!1,p={...n,coefficients:u,weights:m.fit.workingWeights,residuals:m.fit.workingResiduals,qr:m.fit.qr,rank:m.fit.rank},g=we(()=>na(p)),w=g.dispersion,b=new Array(i).fill(NaN);(m.fit.qr===void 0?[]:m.fit.qr.pivot.slice(0,m.fit.rank)).forEach((q,I)=>b[l[q]]=g.stdError[I]);let v=a.x.map(q=>l.map(I=>q[I])),y=m.fit.eta,d=(q,I)=>{let L=l.filter(W=>W!==q),O=$n(a,L,new ke,{...El,start:void 0,etastart:y,extraOffset:a.x.map(W=>W[q]*I)}).fit;if(!O.converged)return h=!0,NaN;let P=s==="LRT"?O.deviance-m.fit.deviance:sa(v,O.workingResiduals,O.workingWeights,!1);return Math.sign(I-u[q])*Math.sqrt(Math.max(P/w,0))},M=(1-o)/2,x=Math.sqrt(Math.PI/2)*o*(1+Math.PI/12*o**2+7*Math.PI**2/480*o**4),E=2**-9,T=x<E,R=T?[-x,x]:[Z(M),M<1e-8?Z(M,{lowerTail:!1}):Z((1+o)/2)],S=c.map(()=>NaN),k=c.map(()=>NaN),A=(q,I,L)=>{let O=L===0?-1:1;h=!1;let P=NaN,W=NaN;for(let fe=0;fe<=30;fe++){let Te=u[q]+O*2**fe*b[q],Oe=d(q,Te);if(Math.abs(Oe)>=Math.abs(I)){P=Te,W=Oe;break}}if(Number.isNaN(P)||h)return NaN;let D=fe=>fe===u[q]?-I:d(q,fe)-I,[F,K]=L===0?[P,u[q]]:[u[q],P],[X,te]=L===0?[W-I,-I]:[-I,W-I],ne;try{ne=We(D,F,K,{tol:1e-14*b[q],maxIter:500,fLower:X,fUpper:te})}catch(fe){if(!h)throw fe;ne=NaN}return h?NaN:ne};return c.forEach((q,I)=>{if(Number.isNaN(u[q]))return;if(!T){S[I]=A(q,R[0],0),k[I]=A(q,R[1],1);return}let L=[-2*E,-E,0,E,2*E],O=[A(q,-2*E,0),A(q,-E,0),u[q],A(q,E,1),A(q,2*E,1)].map(W=>W-u[q]),P=W=>{let D=0;for(let F=0;F<5;F++){let K=1;for(let X=0;X<5;X++)X!==F&&(K*=(W-L[X])/(L[F]-L[X]));D+=K*O[F]}return u[q]+D};S[I]=P(R[0]),k[I]=P(R[1])}),{lower:S.map(q=>_(q,r)),upper:k.map(q=>_(q,r))}}var Ep=["test","dispersion"],xp=(e,t)=>Ne(e,t/2,2,!1,!1);function Tp(e,t,n){let{fit:r,w:o}=$n(e,t,n,{start:void 0});return{dfRes:o.filter(i=>i>0).length-r.rank,dev:r.deviance,fit:r,x:e.x.map(i=>t.map(c=>i[c])),intercept:e.intercept}}var aa=(e,t)=>sa(t.x,e.fit.workingResiduals,e.fit.workingWeights,e.intercept);function Rp(e,t={}){C(t,Ep,"glmAnova");let n=Array.isArray(e)?e:[e];if(n.length===0)throw new RangeError("no fits to compare");let r=n.map(Rt),o=r.every(u=>u.k===r[0].k)?r[0].k:0,s=o===0?n:r.map(u=>u.fit),i=t.dispersion===void 0?void 0:zn(t.dispersion,o),c=i?.s??0,a=Sp(s,Array.isArray(e)&&s.length>1,{...t,...i===void 0?{}:{dispersion:i.m}},c);if(o===0&&c===0)return z("glmAnova",a,...n);let l=new Set(["Deviance","Resid. Dev","Rao","Cp"]),f=a.colNames.map(u=>l.has(u)),m=a.table.map(u=>u.map((h,p)=>f[p]?_(h,2*o):h));if(c!==0&&a.colNames.includes("Cp")){let u=a.colNames.indexOf("Cp"),h=a.colNames.indexOf("Resid. Dev"),p=a.colNames.indexOf("Resid. Df"),g=s[0].model.x.length;for(let w of m)w[u]=w[h]+2*(g-w[p])*t.dispersion}return z("glmAnova",{...a,table:m},...n)}function Sp(e,t,n,r=0){let o=new ke,s=e[0],i=_n(s.model.familySpec);if(e.some(E=>{let T=_n(E.model.familySpec);return T.family!==i.family||T.name!==i.name||T.varianceName!==i.varianceName}))throw new RangeError("models were not all fitted with the same family and link");let c=i.fixedDispersion,a=n.test===void 0?n.dispersion!==void 0||c?"Chisq":"F":n.test;if(a!==!1&&!["Rao","LRT","Chisq","F","Cp"].includes(a))throw new RangeError(`invalid 'test': ${String(a)}`);let l=[],f,m,u=[],h=new Set,p;if(t){let E=e[0].model.x.length;if(e.some(R=>R.model.x.length!==E))throw new RangeError("models were not all fitted to the same size of dataset");let T=(R,S)=>R.length===S.length&&R.every((k,A)=>JSON.stringify(k)===JSON.stringify(S[A]));if(e.some(R=>!T(R.model.yGiven,e[0].model.yGiven)))throw new RangeError("models were not all fitted to the same response");if(e.some(R=>!T(R.model.priorGiven,e[0].model.priorGiven)))throw new RangeError("models were not all fitted with the same prior weights");if(e.forEach((R,S)=>l.push({name:String(S+1),dfRes:R.dfResidual,dev:R.deviance})),f=l.map((R,S)=>S===0?NaN:l[S-1].dfRes-R.dfRes),m=l.map((R,S)=>S===0?NaN:l[S-1].dev-R.dev),e.forEach((R,S)=>{if(S===0||f[S]===0)return;let[k,A]=f[S]>0?[e[S-1],R]:[R,e[S-1]],q=O=>O.filter((P,W)=>k.priorWeights[W]>0&&A.priorWeights[W]>0),I=k.model.offset.map((O,P)=>O-A.model.offset[P]),L=I.some((O,P)=>O!==0&&k.priorWeights[P]>0)?k.model.x.map((O,P)=>[...O,I[P]]):k.model.x;to(q(L),q(A.model.x))||h.add(S)}),a==="Rao"){let R=e.map(S=>({dfRes:S.dfResidual,dev:S.deviance,fit:xl(S),x:S.model.x,intercept:S.model.intercept}));u=l.map((S,k)=>k===0?NaN:f[k]<0?-aa(R[k],R[k-1]):aa(R[k-1],R[k]))}p=e.reduce((R,S)=>S.dfResidual<R.dfResidual?S:R)}else{let E=s,T=E.model,R=T.x.length>0?T.x[0].length-(T.intercept?1:0):0,S=T.intercept?[0]:[],k=q=>[...S,...Array.from({length:q},(I,L)=>L+S.length)],A=[];for(let q=0;q<=R;q++)q===R?A.push({dfRes:E.dfResidual,dev:E.deviance,fit:xl(E),x:T.x,intercept:T.intercept}):q>0||a==="Rao"?A.push(Tp(T,k(q),o)):A.push(void 0);l.push({name:"NULL",dfRes:E.dfNull,dev:E.nullDeviance});for(let q=1;q<=R;q++)l.push({name:`x${q}`,dfRes:A[q].dfRes,dev:A[q].dev});f=l.map((q,I)=>I===0?NaN:l[I-1].dfRes-q.dfRes),m=l.map((q,I)=>I===0?NaN:Math.max(l[I-1].dev-q.dev,0)),a==="Rao"&&(u=l.map((q,I)=>I===0?NaN:aa(A[I-1],A[I]))),p=E}e.forEach((E,T)=>{E.converged||o.add(`model ${T+1} has not converged: its deviance is not that of the maximum likelihood fit`)});let g=new ke,w=n.dispersion??yl(p,g);g.list.forEach(E=>o.add(E));let b=c?1/0:p.dfResidual,N=t?["Resid. Df","Resid. Dev","Df","Deviance"]:["Df","Deviance","Resid. Df","Resid. Dev"],v=l.map((E,T)=>t?[E.dfRes,E.dev,f[T],m[T]]:[f[T],m[T],E.dfRes,E.dev]),y=[...N],d=E=>l.map((T,R)=>{if(R===0||f[R]===0||h.has(R))return NaN;let S=$e(E[R]/w*Math.sign(f[R]),-2*r);return S<0||Number.isNaN(S)?NaN:xp(S,Math.abs(f[R]))}),M=[];if(a==="Chisq"||a==="LRT")y.push("Pr(>Chi)"),M=[d(m)];else if(a==="Rao")y.push("Rao","Pr(>Chi)"),M=[u.map((E,T)=>h.has(T)?NaN:E),d(u)];else if(a==="F"){if(c&&o.add(`using F test with a '${i.family}' family is inappropriate`),b!==0){let E=l.map((T,R)=>{let S=$e(m[R]/f[R]/w,-2*r);return R===0||f[R]===0||h.has(R)||S<0?NaN:S});y.push("F","Pr(>F)"),M=[E,E.map((T,R)=>Number.isNaN(T)?NaN:Me(T,Math.abs(f[R]),b,!1,!1))]}}else if(a==="Cp"){let E=p.model.x.length;y.push("Cp"),M=[l.map(T=>T.dev+2*(E-T.dfRes)*w)]}let x=v.map((E,T)=>[...E,...M.map(R=>R[T])]);return{colNames:y,rowNames:l.map(E=>E.name),table:x,warnings:o.list}}function xl(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}}var kp=["type"];function Tl(e,t,n,r,o=0){let s=[...t,...n];if(s.length===0)return{ss:0,df:0};let i=e.y.flatMap((p,g)=>e.weights===void 0||e.weights[g]!==0?[g]:[]),c=i.map(p=>e.weights===void 0?1:Math.sqrt(e.weights[p])),a=Ee(i.map((p,g)=>s.map(w=>e.x[p][w]*c[g]))),l=Ro(e.weights===void 0?void 0:i.map(p=>_(e.weights[p],-o)),i.length),f=a.pivot.slice(0,a.rank),{qty:m}=Dn(f.map(p=>i.map((g,w)=>Se(l[w],e.x[g][s[p]]))),i.map((p,g)=>Se(l[g],_(e.y[p],-r)))),u=[0,0],h=0;return f.forEach((p,g)=>{p>=t.length&&(u=G(u,ce(m[g],m[g])),h++)}),{ss:u[0],df:h}}function ia(e,t={}){if(C(t,kp,"Anova"),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,s=ko([e]),i=Ao([e],s)[0];if(o===0)throw new Error("residual df = 0");let c=Us(e,s),a=M=>{let x=c.map(([R,S])=>[_(R,-M),S]),E=x.reduce((R,[S])=>R+S,0),T=x.reduce((R,[S,k])=>R+S*k,0)/E;return Number.isFinite(E)&&Number.isFinite(T)?x.reduce((R,[S,k])=>R+S*(k-T)**2,0):NaN},l=0,f=a(0);if(Number.isFinite(f)||(l=qo(c),f=a(l)),gn(c,i,l)||i===0)throw new Error("residual sum of squares is 0 (within rounding error)");let m=[...e.warnings],u=e.termLabels.length;r===2&&u===0&&e.intercept&&(m.push("the model contains only an intercept: Type III test substituted"),r=3);let h=M=>e.assign.flatMap((x,E)=>M.has(x)?[E]:[]),p=[...new Set(e.assign)],g=[],w=[],b=M=>{g.length=0;let x=[];if(r===3){if(e.coefficients.some(E=>Number.isNaN(E)))throw new Error("there are aliased coefficients in the model");for(let E of p){let T=h(new Set([E])),R=h(new Set(p.filter(S=>S!==E)));g.push(E===0?"(Intercept)":e.termLabels[E-1]),x.push({ss:Tl(e,R,T,s,M).ss,df:T.length})}}else{let E=T=>e.factors.flatMap((R,S)=>R[T-1]>0?[S]:[]);for(let T of p){if(T===0)continue;let R=E(T),S=p.filter(q=>q!==0&&q!==T&&R.every(I=>e.factors[I][q-1]>0)),k=h(new Set(p.filter(q=>q!==T&&!S.includes(q)))),A=Tl(e,k,h(new Set([T])),s,M);g.push(e.termLabels[T-1]),x.push(A)}}return x};w=b(l),l===0&&e.weights!==void 0&&w.some(M=>!Number.isFinite(M.ss))&&(l=qo(c),w=b(l));let N=M=>_(M,2*s+l),v=_(i,-l),y=v/o,d=w.map(({ss:M,df:x})=>{let E=M/x/y;return[N(M),x,E,Me(E,x,o,!1,!1)]});return d.push([N(v),o,NaN,NaN]),z("Anova",{colNames:["Sum Sq","Df","F value","Pr(>F)"],rowNames:[...g,"Residuals"],table:d,warnings:m},e)}var Lo=[0,0];var ca=e=>e.map(t=>t.map(n=>n[0]));function Rl(e){let t=e[0]?.length??0;return Array.from({length:t},(n,r)=>e.map(o=>o[r]))}function wn(e,t){let n=t[0]?.length??0;return e.map(r=>{let o=new Array(n).fill(Lo);return r.forEach((s,i)=>{let c=t[i];for(let a=0;a<n;a++)o[a]=G(o[a],ce(s,c[a]))}),o})}function bn(e){let t=Lo;return e.forEach((n,r)=>t=G(t,n[r])),t}function Sl(e){let t=e.length,n=e.map(i=>i.slice()),r=Array.from({length:t},(i,c)=>c),o=1,s=!1;for(let i=0;i<t;i++){let c=i;for(let a=i+1;a<t;a++)Math.abs(n[a][i][0])>Math.abs(n[c][i][0])&&(c=a);if(n[c][i][0]===0){s=!0;continue}c!==i&&([n[i],n[c]]=[n[c],n[i]],[r[i],r[c]]=[r[c],r[i]],o=-o);for(let a=i+1;a<t;a++){let l=ge(n[a][i],n[i][i]);n[a][i]=l;for(let f=i+1;f<t;f++)n[a][f]=ie(n[a][f],ce(l,n[i][f]))}}return{a:n,perm:r,sign:o,singular:s}}function kl(e){let{a:t,sign:n,singular:r}=Sl(e);if(r)return Lo;let o=[n,0];return t.forEach((s,i)=>o=ce(o,s[i])),o}function Al(e){let t=e.length,{a:n,perm:r,singular:o}=Sl(e);if(o)throw new Error("singular matrix");let s=Array.from({length:t},()=>new Array(t).fill(Lo));for(let i=0;i<t;i++){let c=r.map(a=>[a===i?1:0,0]);for(let a=0;a<t;a++)for(let l=0;l<a;l++)c[a]=ie(c[a],ce(n[a][l],c[l]));for(let a=t-1;a>=0;a--){for(let l=a+1;l<t;l++)c[a]=ie(c[a],ce(n[a][l],c[l]));c[a]=ge(c[a],n[a][a])}for(let a=0;a<t;a++)s[a][i]=c[a]}return s}var Ap=["group","idata","M","X"],ql=(e,t)=>Ne(e,t/2,2,!1,!1);function la(e){let t=e.length,n=Ie(bn(e),t);return kl(e.map(r=>r.map(o=>ge(o,n))))[0]}function fa(e,t,n,r){let o=1-(2*t*t+t+2)/(6*t*n),s=(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),c=t*(t+1)/2-1,a=ql(i,c),l=ql(i,c+4);return a+s*(l-a)}var qp=(e,t)=>Array.from({length:t},(n,r)=>e.map(o=>o[r]));function Ip(e,t,n){let r=e.length,o=b=>b.length===0?0:Ee(Array.from({length:r},(N,v)=>b.map(y=>y[v]))).rank,s=n.length===0?void 0:Ee(Array.from({length:r},(b,N)=>n.map(v=>v[N]))),i=s===void 0?[]:s.pivot.slice(0,s.rank).map(b=>n[b]),c=i.length,a=[...i,...t],l=Ee(Array.from({length:r},(b,N)=>a.map(v=>v[N])));if(l.rank>o(t))throw new Error("X does not define a subspace of M");let f=l.pivot.slice(0,l.rank).map(b=>a[b]),m=b=>b.map(N=>[N,0]),u=new Array(r).fill([0,0]),h=Pt(f.map(m),u,e.map((b,N)=>e.map(v=>v[N]))).qtv,p=Array.from({length:r},(b,N)=>h.map(v=>v[N])),g=Pt(f.map(m),u,p).qtv,w=Array.from({length:l.rank-c},(b,N)=>c+N);return w.map(b=>w.map(N=>g[b][N]))}function _p(e,t={}){C(t,Ap,"mauchlyTest");let n=e.map(d=>Array.from(d)),r=n[0]?.length??0;if(r===0||n.some(d=>d.length!==r))throw new TypeError("y must be a matrix with at least one column");let o=t.group;if(o!==void 0&&o.length!==n.length)throw new RangeError("'group' must have one label per row of y");let s=d=>d==null||typeof d=="number"&&Number.isNaN(d),i=n.map((d,M)=>!d.some(s)&&(o===void 0||!s(o[M])));if(n.some((d,M)=>i[M]&&d.some(x=>!Number.isFinite(x))))throw new RangeError("y must not contain infinite values");let c=ee(n.filter((d,M)=>i[M])).groups,a=c.length,l=o?.filter((d,M)=>i[M]).map(Ve)??c.map(()=>""),f=[...new Set(l)].sort(pt),m=c.map(d=>d.map(()=>[0,0]));for(let d of f){let M=l.flatMap((x,E)=>x===d?[E]:[]);for(let x=0;x<r;x++){let E=[0,0];for(let R of M)E=G(E,[c[R][x],0]);let T=Ie(E,M.length);for(let R of M)m[R][x]=ie([c[R][x],0],T)}}let u=a-f.length;if(u<=0)throw new RangeError(`no residual degrees of freedom: ${a} complete row(s) for ${f.length} group(s)`);let h=Array.from({length:r},(d,M)=>Array.from({length:r},(x,E)=>{let T=[0,0];for(let R of m)T=G(T,ce(R[M],R[E]));return T})),p=t.idata??{index:Array.from({length:r},(d,M)=>M+1)},g=d=>{let M=we(()=>In(d,p));if(M.x.length!==r)throw new RangeError("idata must have one row per column of y");return qp(M.x,M.colNames.length)},w=t.M===void 0?Array.from({length:r},(d,M)=>Array.from({length:r},(x,E)=>E===M?1:0)):g(t.M),b=g(t.X??"~0"),N=Ip(h,w,b),v=N.length;if(v===0)throw new RangeError("X spans M: the test has no dimensions");if(v===1)throw new RangeError("one tested dimension: sphericity holds trivially and the test has no degrees of freedom");if(u<v)throw new RangeError(`too few residual degrees of freedom for the test: ${u} for ${v} dimension(s)`);let y=la(N);return{statistic:y,statisticName:"W",pValue:fa(y,v,u,r),method:"Mauchly's test of sphericity",warnings:[]}}var Lp=["n","d","type","sigLevel","power","alternative"],Op=["n1","n2","d","sigLevel","power","alternative"],Fp=["h","n","sigLevel","power","alternative"],Cp=["h","n1","n2","sigLevel","power","alternative"],Dp=["h","n","sigLevel","power","alternative"],Pp=["d","n","sigLevel","power","alternative"],Vp=["n","r","sigLevel","power","alternative"],$p=["w","N","df","sigLevel","power"],zp=["k","n","f","sigLevel","power"],Kp=["u","v","f2","sigLevel","power"],Wp=["size"],jp={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 _l(e="p",...t){C(kn(t),Wp,"cohenES");let n=Sn(["size"],t).size??"small",r=ae(e,["p","t","r","anov","chisq","f2"]),o=ae(n,["small","medium","large"]);return{test:r,size:o,effectSize:jp[r][["small","medium","large"].indexOf(o)],method:"Conventional effect size from Cohen (1982)"}}function Up(e,t){if(!Oo(e)||!Oo(t))throw new RangeError("p1 and p2 must be probabilities in [0, 1]");return 2*Math.asin(Math.sqrt(e))-2*Math.asin(Math.sqrt(t))}function Gp(e,t){if(e.length!==t.length)throw new RangeError("'P0' and 'P1' must have the same length");if(![...e,...t].every(Oo))throw new RangeError("'P0' and 'P1' must be probabilities in [0, 1]");let n=0;for(let r=0;r<e.length;r++)(e[r]!==0||t[r]!==0)&&(n+=Ce(t[r]-e[r],e[r]));return Math.sqrt(n)}function Yp(e){if(e.length===0||e[0].length===0||e.some(s=>s.length!==e[0].length))throw new RangeError("'P' must be a non-empty matrix (rows of the same length)");if(!e.every(s=>s.every(Oo)))throw new RangeError("'P' must contain probabilities in [0, 1]");let t=e.map(s=>s.reduce((i,c)=>i+c,0)),n=e[0]?.length??0,r=Array.from({length:n},(s,i)=>e.reduce((c,a)=>c+a[i],0)),o=0;for(let s=0;s<n;s++)for(let i=0;i<e.length;i++){let c=t[i]*r[s];if(!(c===0&&e[i][s]===0))if(c>=Hp)o+=Ce(e[i][s]-c,c);else{let a=Math.sqrt(t[i]),l=Math.sqrt(r[s]),f=e[i][s]/a/l-a*l;o+=f*f}}return Math.sqrt(o)}var Hp=2**-1022,Oo=e=>e>=0&&e<=1,Xp="\u2018sig.level\u2019 must be numeric in [0, 1]",Bp="\u2018power\u2019 must be numeric in [0, 1]";function tn(e,t){let n=r=>!H(r)&&!(typeof r=="number"&&r>=0&&r<=1);if(n(e))throw new RangeError(Xp);if(n(t))throw new RangeError(Bp)}function nn(e,t){if(typeof e=="number"&&Number.isNaN(e))throw new RangeError("the effect size must not be NaN");return typeof e=="string"?_l(t,e).effectSize:e}var Sr=["two.sided","less","greater"];function Fo(e,t){return e==="two.sided"?t:e==="less"?[-10,5]:[-5,10]}function Ll(e,t,n,r){if(r==="less")return fn(cn(n,e),e,t,!0);if(r==="greater")return fn(cn(n,e,{lowerTail:!1}),e,t,!1);let o=cn(n/2,e,{lowerTail:!1});return fn(o,e,t,!1)+fn(-o,e,t,!0)}var Il=e=>e<1e-8?Z(e,{lowerTail:!1}):Z(1-e);function Ol(e,t,n){return n==="greater"?j(Il(t)-e,{lowerTail:!1}):n==="less"?j(Z(t)-e):j(Il(t/2)-e,{lowerTail:!1})+j(Z(t/2)-e)}function Qp(e){let t=1,n=0;for(let r of e){if(r===0)return 0;let o=Math.floor(Math.log2(Math.abs(r)));t*=_(r,-o),n+=o}return _(t,n)}function Zp(e={}){C(e,Lp,"pwrTTest");let{n:t,power:n}=e,r=nn(e.d,"t"),o=e.sigLevel===void 0?.05:e.sigLevel;if(gt([t,r,n,o],"exactly one of n, d, power, and sig.level must be NULL"),tn(o,n),!H(t)&&!(t>=2))throw new RangeError("number of observations in each group must be at least 2");let s=ae(e.type,["two.sample","one.sample","paired"]),i=ae(e.alternative,Sr),c=s==="two.sample"?2:1;i==="two.sided"&&!H(r)&&(r=Math.abs(r));let a=(m,u,h)=>Ll((m-1)*c,Math.sqrt(m/c)*u,h,i);if(H(n))n=a(t,r,o);else{let m=n;H(t)?t=oe(u=>a(u,r,o)-m,2+1e-10,1e9):H(r)?r=oe(u=>a(t,u,o)-m,...Fo(i,[1e-7,10])):o=oe(u=>a(t,r,u)-m,1e-10,1-1e-10)}let l=s==="paired"?"n is number of *pairs*":s==="two.sample"?"n is number in *each* group":void 0,f={"two.sample":"Two-sample","one.sample":"One-sample",paired:"Paired"}[s]+" t test power calculation";return{n:t,d:r,sigLevel:o,power:n,alternative:i,...l===void 0?{}:{note:l},method:f,warnings:[]}}function Jp(e={}){C(e,Op,"pwrT2nTest");let{n1:t,n2:n,power:r}=e,o=nn(e.d,"t"),s=e.sigLevel===void 0?.05:e.sigLevel;if(gt([t,n,o,r,s],"exactly one of n1, n2, d, power, and sig.level must be NULL"),tn(s,r),!H(t)&&!(t>=2))throw new RangeError("number of observations in the first group must be at least 2");if(!H(n)&&!(n>=2))throw new RangeError("number of observations in the second group must be at least 2");let i=ae(e.alternative,Sr);i==="two.sided"&&!H(o)&&(o=Math.abs(o));let c=(a,l,f,m)=>Ll(a+l-2,f/Math.sqrt(1/a+1/l),m,i);if(H(r))r=c(t,n,o,s);else{let a=r;H(t)?t=oe(l=>c(l,n,o,s)-a,2+1e-10,1e9):H(n)?n=oe(l=>c(t,l,o,s)-a,2+1e-10,1e9):H(o)?o=oe(l=>c(t,n,l,s)-a,...Fo(i,[1e-7,10])):s=oe(l=>c(t,n,o,l)-a,1e-10,1-1e-10)}return{n1:t,n2:n,d:o,sigLevel:s,power:r,alternative:i,method:"t test power calculation",warnings:[]}}function ma(e,t,n,r,o,s){let{n:i,power:c}=t,a=nn(e,n==="h"?"p":"t"),l=t.sigLevel===void 0?.05:t.sigLevel;if(gt([a,i,c,l],`exactly one of ${n}, n, power, and sig.level must be NULL`),tn(l,c),!H(i)&&!(i>=1))throw new RangeError("number of observations in each group must be at least 1");let f=ae(t.alternative,Sr);f==="two.sided"&&!H(a)&&(a=Math.abs(a));let m=(u,h,p)=>Ol(u*r(h),p,f);if(H(c))c=m(a,i,l);else{let u=c;H(a)?a=oe(h=>m(h,i,l)-u,...Fo(f,[s,10])):H(i)?i=oe(h=>m(a,h,l)-u,o,1e9):l=oe(h=>m(a,i,h)-u,1e-10,1-1e-10)}return{es:a,n:i,sigLevel:l,power:c,alternative:f}}function ed(e={}){C(e,Fp,"pwr2pTest");let t=ma(e.h,e,"h",n=>Math.sqrt(n/2),2+1e-10,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",warnings:[]}}function td(e={}){C(e,Dp,"pwrPTest");let t=ma(e.h,e,"h",n=>Math.sqrt(n),2+1e-10,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)",warnings:[]}}function nd(e={}){C(e,Pp,"pwrNormTest");let t=ma(e.d,e,"d",n=>Math.sqrt(n),1+1e-10,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",warnings:[]}}function rd(e={}){C(e,Cp,"pwr2p2nTest");let{n1:t,n2:n,power:r}=e,o=nn(e.h,"p"),s=e.sigLevel===void 0?.05:e.sigLevel;if(gt([o,t,n,r,s],"exactly one of h, n1, n2, power, and sig.level must be NULL"),tn(s,r),!H(t)&&!(t>=2))throw new RangeError("number of observations in the first group must be at least 2");if(!H(n)&&!(n>=2))throw new RangeError("number of observations in the second group must be at least 2");let i=ae(e.alternative,Sr);i==="two.sided"&&!H(o)&&(o=Math.abs(o));let c=(l,f)=>{let m=l*f;if(Number.isFinite(m)||!Number.isFinite(l)||!Number.isFinite(f))return m/(l+f);let u=Math.min(l,f);return u/(1+u/Math.max(l,f))},a=(l,f,m,u)=>Ol(l*Math.sqrt(c(f,m)),u,i);if(H(r))r=a(o,t,n,s);else{let l=r;H(o)?o=oe(f=>a(f,t,n,s)-l,...Fo(i,[1e-10,10])):H(t)?t=oe(f=>a(o,f,n,s)-l,2+1e-10,1e9):H(n)?n=oe(f=>a(o,t,f,s)-l,2+1e-10,1e9):s=oe(f=>a(o,t,n,f)-l,1e-10,1-1e-10)}return{h:o,n1:t,n2:n,sigLevel:s,power:r,alternative:i,method:"difference of proportion power calculation for binomial distribution (arcsine transformation)",note:"different sample sizes",warnings:[]}}function od(e={}){C(e,Vp,"pwrRTest");let{n:t,power:n}=e,r=nn(e.r,"r"),o=e.sigLevel===void 0?.05:e.sigLevel;if(gt([t,r,n,o],"exactly one of n, r, power, and sig.level must be NULL"),tn(o,n),!H(t)&&!(t>=4))throw new RangeError("number of observations must be at least 4");if(!H(r)&&!(Math.abs(r)<=1))throw new RangeError("'r' must be in [-1, 1]");let s=ae(e.alternative,Sr);s==="two.sided"&&!H(r)&&(r=Math.abs(r));let i=(a,l,f,m)=>{let u=cn(m?f/2:f,a-2,{lowerTail:!1}),h=u**2+a-2,p=h===1/0&&Number.isFinite(u)?1/Math.sqrt(1+(a-2)/u/u):Math.sqrt(u**2/h),g=Math.atanh(l)+l/(2*(a-1)),w=Math.atanh(p),b=j((g-w)*Math.sqrt(a-3));return m?b+j((-g-w)*Math.sqrt(a-3)):b},c=(a,l,f)=>s==="two.sided"?i(a,l,f,!0):i(a,s==="less"?-l:l,f,!1);if(H(n))n=c(t,r,o),s==="less"&&(r=-r);else{let a=n;if(H(t))t=oe(l=>c(l,r,o)-a,4+1e-10,1e9);else if(H(r)){let[l,f]=s==="two.sided"?[1e-10,.9999999999]:[-.9999999999,.9999999999];r=oe(m=>c(t,m,o)-a,l,f)}else o=oe(l=>c(t,r,l)-a,1e-10,1-1e-10)}return{n:t,r,sigLevel:o,power:n,alternative:s,method:"approximate correlation power calculation (arctangh transformation)",warnings:[]}}function sd(e){C(e,$p,"pwrChisqTest");let{N:t,power:n}=e,{df:r}=e,o=nn(e.w,"chisq"),s=e.sigLevel===void 0?.05:e.sigLevel;if(gt([o,t,n,s],"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(!(r>0&&r<1/0))throw new RangeError("'df' must be a positive finite number");if(tn(s,n),!H(o)&&!(o>=0))throw new RangeError("w must be positive");if(!H(t)&&!(t>=1))throw new RangeError("number of observations must be at least 1");let i=(c,a,l)=>{let f=a*c**2;return f===1/0?1:_t(Jn(l,r,{lowerTail:!1}),r,{ncp:f,lowerTail:!1})};if(H(n))n=i(o,t,s);else{let c=n;H(o)?o=oe(a=>i(a,t,s)-c,1e-10,1e9):H(t)?t=oe(a=>i(o,a,s)-c,1+1e-10,1e9):s=oe(a=>i(o,t,a)-c,1e-10,1-1e-10)}return{w:o,N:t,df:r,sigLevel:s,power:n,method:"Chi squared power calculation",note:"N is the number of observations",warnings:[]}}function ad(e={}){C(e,zp,"pwrAnovaTest");let{k:t,n,power:r}=e,o=nn(e.f,"anov"),s=e.sigLevel===void 0?.05:e.sigLevel;if(gt([t,n,o,r,s],"exactly one of k, n, f, power, and sig.level must be NULL"),tn(s,r),!H(o)&&!(o>=0))throw new RangeError("f must be positive");if(!H(t)&&!(t>=2))throw new RangeError("number of groups must be at least 2");if(!H(n)&&!(n>=2))throw new RangeError("number of observations in each group must be at least 2");let i=(c,a,l,f)=>{let m=c-1,u=(a-1)*c,h=c*a*l**2;return!Number.isFinite(h)&&Number.isFinite(c)&&Number.isFinite(a)&&Number.isFinite(l)&&(h=Qp([c,a,l,l])),h===1/0?1:oo(m,u)?so(m,u,h,f):an(jt(f,m,u,{lowerTail:!1}),m,u,{ncp:h,lowerTail:!1})};if(H(r))r=i(t,n,o,s);else{let c=r;H(t)?t=oe(a=>i(a,n,o,s)-c,2+1e-10,100):H(n)?n=oe(a=>i(t,a,o,s)-c,2+1e-10,1e9):H(o)?o=oe(a=>i(t,n,a,s)-c,1e-7,1e7):s=oe(a=>i(t,n,o,a)-c,1e-10,1-1e-10)}return{k:t,n,f:o,sigLevel:s,power:r,note:"n is number in each group",method:"Balanced one-way analysis of variance power calculation",warnings:[]}}function id(e={}){C(e,Kp,"pwrF2Test");let{u:t,v:n,power:r}=e,o=nn(e.f2,"f2"),s=e.sigLevel===void 0?.05:e.sigLevel;if(gt([t,n,o,r,s],"exactly one of u, v, f2, power, and sig.level must be NULL"),tn(s,r),!H(o)&&!(o>=0))throw new RangeError("f2 must be positive");if(!H(t)&&!(t>=1))throw new RangeError("degree of freedom u for numerator must be at least 1");if(!H(n)&&!(n>=1))throw new RangeError("degree of freedom v for denominator must be at least 1");let i=(c,a,l,f)=>{let m=l*(c+a+1);return!Number.isFinite(m)&&Number.isFinite(c)&&Number.isFinite(a)&&Number.isFinite(l)&&(m=2*(l*(c/2+a/2+.5))),m===1/0?1:oo(c,a)?so(c,a,m,f):an(jt(f,c,a,{lowerTail:!1}),c,a,{ncp:m,lowerTail:!1})};if(H(r))r=i(t,n,o,s);else{let c=r;H(t)?t=oe(a=>i(a,n,o,s)-c,1+1e-10,100):H(n)?n=oe(a=>i(t,a,o,s)-c,1+1e-10,1e9):H(o)?o=oe(a=>i(t,n,a,s)-c,1e-7,1e7):s=oe(a=>i(t,n,o,a)-c,1e-10,1-1e-10)}return{u:t,v:n,f2:o,sigLevel:s,power:r,method:"Multiple regression power calculation",warnings:[]}}var cd=["pooledSd","mu","paired","ci","alternative","adjust"],ld=["pooledSd","mu","paired","ci","alternative"],fd=["generalized","partial","ci","alternative"],md=["partial","ci","alternative"],ud=["partial","ci","alternative"];function pa(e){if(e!==null&&!(e>0&&e<1))throw new RangeError("ci must be a single numeric value between (0, 1)")}var hd="Missing values detected. NAs dropped.",pd=`'y' is numeric but has only 2 unique values.
7
+ If this is a grouping variable, convert it to a factor.`,Pl={tol:0,maxiter:5e3};function dd(e){return e===1/0?1:Math.exp(Pa(.5,(e-1)/2)-.5*Math.log(e/2))}function Fl(e,t,n){let r=e-1<e+1?1:Math.abs(e)*Number.EPSILON;return ir(o=>Ur(e,t,{ncp:o})-n,e-r,e+r,{...Pl,extendInt:"downX"}).root}function Vl(e,t,n,r){let o=n.pooledSd??!0,s=n.mu??0,i=n.paired??!1,c=n.ci===void 0?.95:n.ci;pa(c);let a=ae(n.alternative,["two.sided","less","greater"]),l=Array.from(e),f=t===void 0?void 0:Array.from(t),m=[];f!==void 0&&new Set(Je(f)).size===2&&m.push(pd),(l.some(Number.isNaN)||(f?.some(Number.isNaN)??!1))&&m.push(hd);let u=S=>S===1/0||S===-1/0;if(l.some(u)||(f?.some(u)??!1))throw new RangeError("the observations must be finite (NaN is dropped as missing)");let h,p,g,w,b,N;if(f===void 0||i){let S;if(f===void 0)({groups:[S],k:N}=ee([Je(l)]));else{let[A,q]=En(l,f);({d:S,k:N}=zr(A,q))}let k=S.length;if(k<2)throw new RangeError(f===void 0?"not enough observations: at least 2 are needed":"not enough complete pairs: at least 2 are needed");h=qe(S),p=Math.sqrt(_e(S)),g=p/Math.sqrt(k),w=1/k,b=k-1}else{let S=Le([Je(l),Je(f)]);N=S.k;let[k,A]=S.n;if(k<2||A<2)throw new RangeError("not enough observations: at least 2 are needed in each group");h=S.mean[0]-S.mean[1];let q=Math.sqrt(S.variance[0]),I=Math.sqrt(S.variance[1]);o?(p=Math.sqrt(((k-1)*q**2+(A-1)*I**2)/(k+A-2)),g=p*Math.sqrt(1/k+1/A),w=1/k+1/A,b=k+A-2):(p=Math.sqrt((q**2+I**2)/2),w=2*(A*q**2+k*I**2)/(k*A*(q**2+I**2)),g=Math.sqrt(Math.sqrt(q**2/k)**2+Math.sqrt(I**2/A)**2),b=Kr([q**2/k,I**2/A],[k-1,A-1]))}let v=r?dd(b):1,y=_(s,-N),d=(h-y)/p*v,M=r?"Hedges_g":"Cohens_d";if(c===null)return{name:M,value:d,warnings:m};let x=a==="two.sided"?c:2*c-1,E=(h-y)/g,T=Fl(E,b,(1+x)/2)*Math.sqrt(w)*v,R=Fl(E,b,(1-x)/2)*Math.sqrt(w)*v;return a==="less"&&(T=-1/0),a==="greater"&&(R=1/0),{name:M,value:d,ci:c,ciLow:T,ciHigh:R,warnings:m}}function gd(e,t,n={}){return C(n,cd,"cohensD"),z("cohensD",Vl(e,t,n,n.adjust??!1))}function wd(e,t,n={}){return C(n,ld,"hedgesG"),z("hedgesG",Vl(e,t,n,!0))}var bd=e=>Array.isArray(e.effects),ua=e=>e.multistratum===!0,$l=e=>Array.isArray(e.strata),zl=e=>{let t=e;return typeof t.univariate=="object"&&typeof t.anova=="object"},ha=e=>({test:t=>t.split(":").includes(e)});function vd(e){let t=e.strata.filter(o=>o.stratum!=="Error: (Intercept)").map(o=>{let s=o.stratum.replace(/^Error: /,""),i=o.colNames.indexOf("Df"),c=o.colNames.indexOf("Sum Sq"),a=o.rowNames.map((f,m)=>({name:f,df:o.table[m][i],ss:o.table[m][c]})),l=a.find(f=>f.name==="Residuals");if(l===void 0)throw new Error(`the error stratum '${s}' has no residual degrees of freedom`);return{name:s,terms:a.filter(f=>f!==l),sse:l.ss,dfe:l.df}});if(t.length===0)throw new TypeError("the fit has no error strata");let n=t[0].name.split(":")[0],r=t.find(o=>o.name===n);if(r===void 0)throw new Error(`there is no error stratum '${n}'`);return{strata:t,subject:r}}function Nd(e){let t=e.univariate,n=t.colNames.indexOf("Sum Sq"),r=[],o=(a,l,f)=>{let m=r.find(u=>u.name===a);return m===void 0&&r.push(m={name:a,terms:[],sse:l,dfe:f}),m},s=new Map;if(t.colNames.includes("Error SS")){let a=t.colNames.indexOf("num Df"),l=t.colNames.indexOf("Error SS"),f=t.colNames.indexOf("den Df");t.rowNames.forEach((m,u)=>{let h=t.table[u],p=e.within.filter(b=>ha(b).test(m)),g=o(p.length===0?"Subject":`Subject:${p.join(":")}`,h[l],h[f]);if(m==="(Intercept)")return;let w={name:m,df:h[a],ss:h[n]};g.terms.push(w),s.set(m,w)})}else{let a=t.colNames.indexOf("Df"),l=t.rowNames.indexOf("Residuals"),f=o("Subject",t.table[l][n],t.table[l][a]);t.rowNames.forEach((m,u)=>{if(m==="(Intercept)"||m==="Residuals")return;let h={name:m,df:t.table[u][a],ss:t.table[u][n]};f.terms.push(h),s.set(m,h)})}let i=r.find(a=>a.name==="Subject");if(i===void 0)throw new Error("the result has no subject stratum");let c=e.anova.rowNames.map(a=>{let l=s.get(a);if(l===void 0)throw new Error(`there is no univariate test of '${a}'`);return l});return{strata:r,subject:i,order:c}}function Kl(e,t){let n=Array.isArray(e)?e:[],r=n.filter(s=>!t.some(i=>ha(s).test(i.name)));if(r.length>0)throw new RangeError(`Observed variable not in data: ${r.join(", ")}`);let o=s=>n.some(i=>ha(i).test(s));return{isObserved:o,ssObserved:t.reduce((s,i)=>o(i.name)?s+i.ss:s,0)}}function Cl(e,t,n,r){let o=i=>an(e,t,n,{ncp:i})-r,s=o(0);return Number.isNaN(s)?NaN:s>0?e===1/0?1/0:ir(o,0,Math.max(10,e*t*4),{...Pl,extendInt:"downX"}).root:0}function Wl(e,t,n,r,o){let s=o==="two.sided"?r:2*r-1,i=Math.max(0,e),c=i/t/((1-i)/n),a=Cl(c,t,n,(1+s)/2),l=Cl(c,t,n,(1-s)/2);c<=jt((1-s)/2,t,n)&&(l=0),c<=jt((1+s)/2,t,n)&&(a=0);let f=a===1/0?1:a/(a+n),m=l===1/0?1:l/(l+n);return o==="less"&&(f=0),o==="greater"&&(m=1),[e,r,f,m]}function Md(e){let t=ee([e.map(n=>n.ss)]).k;if(t!==0)for(let n of e)n.ss=_(n.ss,-t)}var Dl="needs a table of anova() or Anova(), a fit from lm() or aov() with a formula, its aovSummary(), or a result of aovEz()";function da(e,t,n){if(typeof e!="object"||e===null)throw new TypeError(Dl);if(ua(e)||$l(e)||zl(e))return yd(e,t,n);let r=bd(e)?we(()=>Xs(e)):e;if(!Array.isArray(r.colNames)||!Array.isArray(r.table))throw new TypeError(Dl);let o=je.get(r),s=o!==void 0&&!Array.isArray(o)?o:r,i=n.ci===void 0?.95:n.ci;pa(i);let c=ae(n.alternative??"greater",["two.sided","less","greater"]),a=s.colNames.indexOf("Df"),l=s.colNames.indexOf("Sum Sq");if(a<0||l<0)throw new TypeError("the table needs the columns 'Df' and 'Sum Sq'");let f=s.rowNames.map((T,R)=>({name:T,df:s.table[R][a],ss:s.table[R][l]})).filter(T=>T.name!=="(Intercept)");Md(f);let m=f.find(T=>T.name==="Residuals");if(m===void 0)throw new TypeError("the table has no row 'Residuals'");if(!(m.df>0))throw new Error("the model has no residual degrees of freedom: the effect sizes are not defined");let u=f.filter(T=>T!==m),h=m.df,p=m.ss,g=p/h,w=f.reduce((T,R)=>T+R.ss,0),b=f.reduce((T,R)=>T+R.df,0)+1,N=t==="Eta2"?n.generalized??!1:!1,y=u.length===1?"plain":N!==!1?"generalized":n.partial??!0?"partial":"plain",{isObserved:d,ssObserved:M}=Kl(N,u),x=T=>{let{ss:R,df:S}=T;if(t==="Eta2")return y==="partial"?R/(R+p):y==="plain"?R/w:R/(R+p+M-(d(T.name)?R:0));if(t==="Omega2"){let A=y==="partial"?(R-S*g)/(R+(b-S)*g):(R-S*g)/(w+g);return Math.max(0,A)}let k=y==="partial"?(R-S*g)/(R+p):(R-S*g)/w;return Math.max(0,k)},E=`${t}${y==="plain"?"":`_${y}`}`;return{colNames:i===null?[E]:[E,"CI","CI_low","CI_high"],rowNames:u.map(T=>T.name),table:u.map(T=>i===null?[x(T)]:Wl(x(T),T.df,h,i,c)),warnings:Array.isArray(r.warnings)?[...r.warnings]:[]}}function yd(e,t,n){let r=n.ci===void 0?.95:n.ci;pa(r);let o=ae(n.alternative??"greater",["two.sided","less","greater"]),s=zl(e),i,c,a;if(s){let d=je.get(e),M=Nd(d!==void 0&&!Array.isArray(d)?{...e,univariate:d}:e);({strata:i,subject:c}=M),a=M.order.map(x=>({term:x,stratum:M.strata.find(E=>E.terms.includes(x))}))}else{let d=ua(e)?we(()=>Bs(e)):e,M=je.get(d);({strata:i,subject:c}=vd(Array.isArray(M)?{...d,strata:M.map((x,E)=>({...d.strata[E],...x}))}:d)),a=i.flatMap(x=>x.terms.map(E=>({term:E,stratum:x})))}let l=ee([[...i.flatMap(d=>d.terms.map(M=>M.ss)),...i.map(d=>d.sse)]]).k;for(let d of i){d.sse=_(d.sse,-l);for(let M of d.terms)M.ss=_(M.ss,-l)}let f=i.flatMap(d=>d.terms),m=i.reduce((d,M)=>d+M.sse,0),u=f.reduce((d,M)=>d+M.ss,0)+m,h=c.sse/c.dfe,p=t==="Eta2"?n.generalized??!1:!1,g=p!==!1?"generalized":n.partial??!0?"partial":"plain",{isObserved:w,ssObserved:b}=Kl(p,f),N=(d,M)=>{let{ss:x,df:E}=d,T=M.sse/M.dfe;if(t==="Eta2")return g==="partial"?x/(x+M.sse):g==="plain"?x/u:x/(x+m+b-(w(d.name)?x:0));if(t==="Omega2"){let S=M===c?0:1,k=g==="partial"?(x-E*T)/(x+S*M.sse+c.sse+h):(x-E*T)/(u+h);return Math.max(0,k)}let R=g==="partial"?(x-E*T)/(x+M.sse):(x-E*T)/u;return Math.max(0,R)},v=`${t}${g==="plain"?"":`_${g}`}`,y={colNames:r===null?[v]:[v,"CI","CI_low","CI_high"],rowNames:a.map(d=>d.term.name),table:a.map(({term:d,stratum:M})=>{let x=N(d,M);return r===null?[x]:Wl(x,d.df,M.dfe,r,o)}),warnings:[]};return s||(y.groups=a.map(d=>d.stratum.name),y.warnings=[...ua(e)||$l(e)?e.warnings:[]]),y}function Ed(e,t={}){return C(t,fd,"etaSquared"),z("etaSquared",da(e,"Eta2",t),e)}function xd(e,t={}){return C(t,md,"omegaSquared"),z("omegaSquared",da(e,"Omega2",t),e)}function Td(e,t={}){return C(t,ud,"epsilonSquared"),z("epsilonSquared",da(e,"Epsilon2",t),e)}var Rd=["id","dv","between","within","type","correction","es","observed"],Co=e=>e===void 0?[]:typeof e=="string"?[e]:[...e],Sd=1e-12,jl=1e-24;function Ul(e,t){let n=Object.prototype.hasOwnProperty.call(e,t)?e[t]:void 0;if(n===void 0)throw new RangeError(`object '${t}' not found`);return Array.from(n)}var kr=e=>typeof e=="boolean"?e?"TRUE":"FALSE":Ve(e);function kd(e){let t=[...new Set(e)];return t.every(n=>typeof n=="number")?t.sort((n,r)=>n-r).map(kr):[...new Set(t.map(kr))].sort(pt)}var Gl=(e,t)=>t.split(":").includes(e);function Yl(e,t,n,r,o){let s=l=>r.some(f=>Gl(f,l));for(let l of r)if(!e.some(f=>Gl(l,f.name)))throw new RangeError(`Observed variable not in data: ${l}`);let i=[...t.values()].reduce((l,f)=>l+f,0),c=e.reduce((l,f)=>s(f.name)?l+f.ss:l,0),a=["pes","ges"].filter(l=>n.includes(l));return{colNames:["num Df","den Df","MSE","F",...a,"Pr(>F)"],rowNames:e.map(l=>l.name),table:e.map(l=>{let f=a.map(m=>m==="pes"?l.ss/(l.ss+l.errSS):l.ss/(l.ss+i+c-(s(l.name)?l.ss:0)));return[l.numDf,l.denDf,o(l.errSS/l.denDf),l.f,...f,l.p]})}}function Ad(e,t){C(t,Rd,"aovEz");let n=Co(t.between),r=Co(t.within);if(n.length===0&&r.length===0)throw new Error("Either between or within need to be non-NULL!");for(let[$,Y]of[["between",n],["within",r]]){let B=Y.filter((pe,Fe)=>Y.indexOf(pe)!==Fe);if(B.length>0)throw new RangeError(`duplicated factor names in '${$}': ${[...new Set(B)].join(", ")}`)}let o=n.filter($=>r.includes($));if(o.length>0)throw new RangeError(`factors in both 'between' and 'within': ${o.join(", ")}`);let s=t.type??3;if(s!==2&&s!==3)throw new RangeError(`invalid type (${String(s)}): use 2 or 3`);let i=t.correction??"GG";if(!["GG","HF","none"].includes(i))throw new RangeError(`invalid 'correction': ${String(i)}`);let c=Co(t.es??"ges");for(let $ of c)if($!=="ges"&&$!=="pes")throw new RangeError(`invalid 'es': ${String($)}`);let a=Co(t.observed),l=[],f=$=>$==null||typeof $=="number"&&Number.isNaN($),m=Ul(e,t.dv),u=[t.id,...n,...r].map($=>Ul(e,$));for(let $ of u)if($.length!==m.length)throw new RangeError("the columns must have the same length");let h=m.map(($,Y)=>u.every(B=>!f(B[Y]))),p=$=>$.filter((Y,B)=>h[B]),g=Pe(p(u[0])).map(kr),w=p(m).map($=>{if(f($))return NaN;if(typeof $!="number")throw new TypeError(`the response '${t.dv}' must be numeric`);if(!Number.isFinite($))throw new RangeError(`the response '${t.dv}' must be finite`);return $}),b=n.map(($,Y)=>Pe(p(u[1+Y]))),N=r.map(($,Y)=>Pe(p(u[1+n.length+Y]))),v=g.length;for(let $ of[w,...b,...N])if($.length!==v)throw new RangeError("the columns must have the same length");let y=b.map($=>kd($)),d=b.map($=>$.map(kr)),M=N.map($=>$.map(kr)),x=M.map($=>[...new Set($)]);y.forEach(($,Y)=>{if($.length<2)throw new Error(`Factor "${n[Y]}" consists of one level only. Remove factor from model?`)}),x.forEach(($,Y)=>{if($.length<2)throw new Error(`Factor "${r[Y]}" consists of one level only. Remove factor from model?`)});let E=[...new Set(g)],T=new Map,R=new Set;if(g.forEach(($,Y)=>{let B=JSON.stringify(d.map(Fe=>Fe[Y])),pe=T.get($);pe===void 0?T.set($,B):pe!==B&&R.add($)}),R.size>0)throw new Error(`Following ids are in more than one between subjects condition:
8
+ ${E.filter($=>R.has($)).join(", ")}`);let S=[[]];for(let $ of x)S=S.flatMap(Y=>$.map(B=>[...Y,B]));let k=$=>JSON.stringify(M.map(Y=>Y[$])),A=new Map(S.map(($,Y)=>[JSON.stringify($),Y])),q=new Set(g.map(($,Y)=>k(Y)));if(S.some($=>!q.has(JSON.stringify($))))throw new Error("Empty cells in within-subjects design (i.e., bad data structure).");let I=new Map,L=new Set;g.forEach(($,Y)=>{let B=I.get($);B===void 0&&I.set($,B=new Set),B.add(k(Y)),Number.isNaN(w[Y])&&L.add($)});let O=E.filter($=>L.has($)||I.get($).size<S.length),P=new Set(O),W=ee([w.filter(($,Y)=>!P.has(g[Y]))]).k,D=W===0?w:w.map($=>_($,-W)),F=$=>_($,2*W),K=S.length,X=new Map;if(g.forEach(($,Y)=>{let B=X.get($);B===void 0&&X.set($,B={s:S.map(()=>[0,0]),n:S.map(()=>0)});let pe=A.get(k(Y));B.s[pe]=G(B.s[pe],[D[Y],0]),B.n[pe]++}),[...X.values()].some($=>$.n.some(Y=>Y>1))&&l.push("More than one observation per design cell, aggregating data using `fun_aggregate = mean`.\nTo turn off this warning, pass `fun_aggregate = mean` explicitly."),O.length>0){let $=[...O].sort(pt),Y=$.length>=10?`${$.slice(0,10).join(", ")}, ... [showing first 10 only]`:$.join(", ");l.push(`Missing values for ${O.length} ID(s), which were removed before analysis:
9
+ ${Y}`)}let te=E.filter($=>!O.includes($)),ne=new Map;g.forEach(($,Y)=>ne.has($)||ne.set($,Y));let fe=te.map($=>{let Y=X.get($);return Y.s.map((B,pe)=>Ie(B,Y.n[pe]))}),Te={};n.forEach(($,Y)=>Te[$]=te.map(B=>d[Y][ne.get(B)]));let Oe=Object.fromEntries(n.map($=>[$,"contr.sum"])),Ue=Object.fromEntries(n.map(($,Y)=>[$,y[Y]])),Mt="Rank deficient model matrix; insufficient data to estimate full model.";if(K===1)return z("aovEz",Q());return z("aovEz",J());function Q(){let $="y";for(;n.includes($);)$+="_";let Y={...Te,[$]:fe.map(Ye=>Ye[0][0])},B=we(()=>Js(`${$} ~ ${n.length>0?n.join(" * "):"1"}`,Y,{contrasts:Oe,levels:Ue}));if(B.rank<B.coefNames.length)throw new Error(Mt);if(B.dfResidual<=0)throw new RangeError(`no residual degrees of freedom: ${fe.length} subject(s) for ${B.rank} parameter(s)`);let pe=we(()=>ia(B,{type:s}));l.push(...pe.warnings.filter(Ye=>!l.includes(Ye)));let Fe=B.dfResidual,tt=pe.rowNames.flatMap((Ye,lt)=>{if(Ye==="(Intercept)"||Ye==="Residuals")return[];let[kt,Vt,vn,$t]=pe.table[lt];return[{name:Ye,ss:kt,numDf:Vt,errSS:B.rss,denDf:Fe,f:vn,p:$t}]}),nt=pe.colNames.indexOf("Sum Sq"),Ge={anova:Yl(tt,new Map([[0,B.rss]]),c,a,F),univariate:{colNames:pe.colNames,rowNames:pe.rowNames,table:pe.table.map(Ye=>Ye.map((lt,kt)=>kt===nt?F(lt):lt))},within:r,warnings:l};return je.set(Ge,{colNames:pe.colNames,rowNames:pe.rowNames,table:pe.table}),Ge}function J(){let $=te.length,Y=fe.reduce((me,ue)=>ue.reduce((be,Be)=>be+Be[0]*Be[0],me),0),B=n.length>0?we(()=>In(`~ ${n.join(" * ")}`,Te,{contrasts:Oe,levels:Ue})):{colNames:["(Intercept)"],assign:[0],x:te.map(()=>[1]),termLabels:[],factors:[]},pe=B.colNames.length;if($===0||Ee(B.x).rank<pe)throw new Error(Mt);let Fe=$-pe;if(Fe<=0)throw new RangeError(`no residual degrees of freedom: ${$} subject(s) for ${pe} between-subject parameter(s)`);let tt=[0,...B.termLabels.map((me,ue)=>ue+1)],nt=["(Intercept)",...B.termLabels],Ge=me=>B.assign.flatMap((ue,be)=>me.includes(ue)?[be]:[]),Ye=me=>B.factors.flatMap((ue,be)=>ue[me-1]>0?[be]:[]),lt=me=>B.x.map(ue=>[ue[me],0]),kt=new Array($).fill([0,0]),Vt=(me,ue,be)=>{let{qtv:Be}=Pt([...me,...ue].map(lt),kt,be);return Be.map(Hn=>Hn.slice(me.length,me.length+ue.length))},vn=me=>{let{qtv:ue}=Pt(B.colNames.map((be,Be)=>lt(Be)),kt,me);return ue.map(be=>be.slice(pe))},$t=Object.fromEntries(r.map((me,ue)=>[me,S.map(be=>be[ue])])),At=we(()=>In(`~ ${r.join(" * ")}`,$t,{contrasts:Object.fromEntries(r.map(me=>[me,"contr.sum"])),levels:Object.fromEntries(r.map((me,ue)=>[me,x[ue]]))})),Lr=[0,...At.termLabels.map((me,ue)=>ue+1)],Gn=["(Intercept)",...At.termLabels],Or=me=>me.map(ue=>me.map(be=>{let Be=[0,0];return ue.forEach((Hn,Nn)=>Be=G(Be,ce(Hn,be[Nn]))),Be})),Yn=[],V=[],U=[],re=[],de=[],he=[],Re=[],ft=new Map,zt=!1;for(let me of Lr){let ue=At.assign.flatMap((Qe,Ae)=>Qe===me?[Ae]:[]),be=ue.length,Be=ue.map(Qe=>fe.map(Ae=>Ae.reduce((yt,mt,Qn)=>G(yt,Se(mt,At.x[Qn][Qe])),[0,0]))),Hn=ue.map(Qe=>ue.map(Ae=>[At.x.reduce((yt,mt)=>yt+mt[Qe]*mt[Ae],0),0])),Nn=Al(Hn),Uo=Or(vn(Be)),Fr=wn(Uo,Nn),Xn=bn(Fr)[0],bt=NaN,Bn=NaN,Go=NaN,qa=NaN,Cr=!1;if(be>=2){let Qe=Gn[me],Ae=fi(ca(Nn)).map(qt=>qt.map(sn=>[sn,0])),yt=ca(wn(wn(Ae,Uo),Rl(Ae))),mt=dt(yt,{symmetric:!0,onlyValues:!0}).values,Qn=mt.reduce((qt,sn)=>Math.max(qt,sn),0),ut=Math.max(Qn*Sd,Y*jl),ot=bn(Fr),Et=bn(wn(Fr,Fr));Qn>Y*jl&&Et[0]>0&&Number.isFinite(Et[0])?(Cr=!0,bt=ge(ce(ot,ot),Se(Et,be))[0],Fe-be*bt>0?Bn=((Fe+1)*be*bt-2)/(be*(Fe-be*bt)):l.push(`The Huynh-Feldt epsilon of ${Qe} is undefined (d - p GG <= 0)${i==="HF"?": corrected with GG":""}`)):l.push(`The error SSP matrix of ${Qe} is 0 (or at the rounding level of the data): no sphericity corrections (the p-values are uncorrected)`),Cr&&mt.every(qt=>qt>ut)?(Go=la(wn(Nn,Uo)),qa=fa(Go,be,Fe,K)):Cr&&l.push(`The error SSP matrix of ${Qe} is singular: no Mauchly test (the epsilons are still defined)`)}for(let Qe=0;Qe<tt.length;Qe++){let Ae=tt[Qe],yt;if(s===3)yt=Vt(Ge(tt.filter(Kt=>Kt!==Ae)),Ge([Ae]),Be);else if(Ae===0)yt=Vt([],Ge([0]),Be);else{let Kt=Ye(Ae),Zn=tt.filter(It=>It!==0&&It!==Ae&&Kt.every(bf=>B.factors[bf][It-1]>0));yt=Vt(Ge(tt.filter(It=>It!==Ae&&!Zn.includes(It))),Ge([Ae]),Be)}let mt=bn(wn(Or(yt),Nn))[0],ut=(Ae===0?1:Ge([Ae]).length)*be,ot=Fe*be,Et=mt/ut/(Xn/ot),qt=Me(Et,ut,ot,!1,!1),sn=me===0?nt[Qe]:Ae===0?Gn[me]:`${nt[Qe]}:${Gn[me]}`;Yn.push([F(mt),ut,F(Xn),ot,Et,qt]),V.push([mt,ut,Xn,ot,Et,qt]),U.push(sn),ft.set(me,Xn);let Mn={name:sn,ss:mt,numDf:ut,errSS:Xn,denDf:ot,f:Et,p:qt};if(Cr){let Kt=Math.min(1,Bn),Zn=Me(Et,bt*ut,bt*ot,!1,!1),It=Me(Et,Kt*ut,Kt*ot,!1,!1);re.push([bt,Zn,Bn,It]),de.push([Go,qa]),he.push(sn),i==="GG"?Mn={...Mn,numDf:bt*ut,denDf:bt*ot,p:Zn}:i==="HF"&&(Bn>1&&(zt=!0),Mn=Number.isNaN(Bn)?{...Mn,numDf:bt*ut,denDf:bt*ot,p:Zn}:{...Mn,numDf:Kt*ut,denDf:Kt*ot,p:It})}me===0&&Ae===0||Re.push(Mn)}}zt&&l.push("HF eps > 1 treated as 1");let on={anova:Yl(Re,ft,c,a,F),univariate:{colNames:["Sum Sq","num Df","Error SS","den Df","F value","Pr(>F)"],rowNames:U,table:Yn},within:r,warnings:l};return re.length>0&&(on.corrections={colNames:["GG eps","Pr(>F[GG])","HF eps","Pr(>F[HF])"],rowNames:he,table:re},on.sphericity={colNames:["Test statistic","p-value"],rowNames:[...he],table:de}),je.set(on,{colNames:on.univariate.colNames,rowNames:U,table:V}),on}}function Do(e){let t=Math.sqrt(2220446049250313e-31),n=Array.from(new Set(e.filter(s=>Number.isFinite(s)))).sort((s,i)=>s-i);if(n.length<2)return e.slice();let r=qe(n.map(Math.abs)),o=[n[0]];for(let s=0;s+1<n.length;s++){let i=n[s+1]-n[s];i<=t||i/r<=t||o.push(n[s+1])}return e.map(s=>{if(!Number.isFinite(s))return s;let i=0,c=o.length-1;for(;i<c;){let a=Math.floor((i+c+1)/2);o[a]<=s?i=a:c=a-1}return o[i]})}var qd=["rho"],Id=["group","confType","confInt"],_d=e=>e==null||typeof e=="number"&&Number.isNaN(e);function Hl(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 a=0;a<e.length;a++){if(Number.isNaN(e[a])||Number.isNaN(t[a])||n!==void 0&&_d(n[a]))continue;let l=t[a];if(l!==0&&l!==1)throw new RangeError("'status' must be 0 (censored) or 1 (event)");if(e[a]===-1/0||e[a]===1/0&&l===1)throw new RangeError("event times must be finite");r.push(a)}if(r.length===0)throw new RangeError("no observations without missing values");let o=[],s=new Map;n!==void 0&&(n=Pe(n)),n!==void 0&&(o=Array.from(new Set(r.map(a=>n[a]))).sort((a,l)=>typeof a=="number"&&typeof l=="number"?a-l:String(a)<String(l)?-1:String(a)>String(l)?1:0),o.forEach((a,l)=>s.set(a,l)));let i=Do(r.map(a=>e[a])),c=r.map((a,l)=>({time:i[l],event:t[a]===1,group:n===void 0?0:s.get(n[a])}));return c.sort((a,l)=>a.time-l.time),{obs:c,labels:o.map(a=>`group=${Ve(a)}`)}}function Ld(e,t,n,r={}){C(r,qd,"survdiff");let{rho:o=0}=r;if(!Number.isFinite(o))throw new RangeError("'rho' must be a finite number");let{obs:s,labels:i}=Hl(e,t,n),c=i.length;if(c<2)throw new RangeError("there is only 1 group");let a=new Array(c).fill(0);for(let v of s)a[v.group]++;let l=new Array(c).fill(0),f=new Array(c).fill(0),m=Array.from({length:c},()=>new Array(c).fill(0)),u=a.slice(),h=s.length,p=1;for(let v=0;v<s.length;){let y=s[v].time,d=v,M=new Array(c).fill(0),x=new Array(c).fill(0),E=0;for(;d<s.length&&s[d].time===y;){let T=s[d];x[T.group]++,T.event&&(M[T.group]++,E++),d++}if(E>0){let T=o===0?1:p**o;for(let R=0;R<c;R++)l[R]+=T*M[R],f[R]+=T*E*u[R]/h;if(h>1){let R=T*T*E*(h-E)/(h*(h-1));for(let S=0;S<c;S++)for(let k=0;k<c;k++)m[S][k]+=R*u[S]*((S===k?1:0)-u[k]/h)}p*=1-E/h}for(let T=0;T<c;T++)u[T]-=x[T];h-=d-v,v=d}let g=f.map((v,y)=>v>0?y:-1).filter(v=>v>=0),w=0,b=Math.max(g.length-1,0);if(g.length>=2){let v=g.slice(1),y=v.map(M=>l[M]-f[M]),d=v.map(M=>v.map(x=>m[M][x]));try{let M=Ke(d,y);w=y.reduce((x,E,T)=>x+E*M[T],0)}catch{({chisq:w,df:b}=Od(d,y))}}let N=b===0?NaN:_t(w,b,{lowerTail:!1});return{n:a,groups:i,obs:l,exp:f,var:m,chisq:w,df:b,pvalue:N,pValue:N,warnings:[]}}function Od(e,t){let n=dt(e,{symmetric:!0}),r=n.values,o=r.reduce((c,a)=>Math.max(c,a),0),s=0,i=0;return r.forEach((c,a)=>{if(!(o>0&&c>1e-10*o))return;let l=t.reduce((f,m,u)=>f+m*n.vectors[u][a],0);s+=l*l/c,i++}),{chisq:s,df:i}}function ga(e,t){let n=Math.sqrt(Number.EPSILON),r=t.findIndex(s=>s<.5+n);if(r<0)return NaN;let o=t[r];if(Math.abs(o-.5)<n){for(let s=r+1;s<t.length;s++)if(t[s]<o)return Gt(e[r],e[s])}return e[r]}function Fd(e,t,n={}){C(n,Id,"survfit");let{group:r,confType:o="log",confInt:s=.95}=n;if(o!=="log"&&o!=="plain"&&o!=="log-log")throw new RangeError(`invalid 'confType': ${String(o)}`);if(!(s>0&&s<1))throw new RangeError("'confInt' must be between 0 and 1");let{obs:i,labels:c}=Hl(e,t,r),a=r===void 0?["all"]:c,l=(1-s)/2,f=l<1e-8?Z(l,{lowerTail:!1}):Z(1-(1-s)/2),m=[],u={strata:[],time:[],nRisk:[],nEvent:[],nCensor:[],surv:[],stdErr:m,stdError:m,lower:[],upper:[],median:[],medianRows:a};return a.forEach((h,p)=>{let g=i.filter(d=>d.group===p),w=u.time.length,b=g.length,N=1,v=0;for(let d=0;d<g.length;){let M=g[d].time,x=d,E=0;for(;x<g.length&&g[x].time===M;)g[x].event&&E++,x++;let T=x-d-E;E>0&&(N*=1-E/b,v+=E<b?E/(b*(b-E)):1/0);let R=Math.sqrt(v),S,k;if(o==="log")S=N===0?NaN:N*Math.exp(-f*R),k=N===0?NaN:Math.min(1,N*Math.exp(f*R));else if(o==="plain"){let A=f*R*N;S=Number.isNaN(A)?NaN:Math.max(0,N-A),k=Number.isNaN(A)?NaN:Math.min(1,N+A)}else if(N===0)S=NaN,k=NaN;else if(N===1)S=1,k=1;else{let A=Math.log(N),q=Math.log(-A);S=Math.exp(-Math.exp(q-f*R/A)),k=Math.exp(-Math.exp(q+f*R/A))}u.strata.push(h),u.time.push(M),u.nRisk.push(b),u.nEvent.push(E),u.nCensor.push(T),u.surv.push(N),u.stdErr.push(N*R),u.lower.push(S),u.upper.push(k),b-=x-d,d=x}let y=u.time.slice(w);u.median.push([ga(y,u.surv.slice(w)),ga(y,u.lower.slice(w)),ga(y,u.upper.slice(w))])}),u}var Cd=["ties","weights","strata","init","control","nocenter","confLevel"],Dd=["eps","iterMax","tolerChol","tolerInf"],Pd=e=>e==null||typeof e=="number"&&Number.isNaN(e),Vd=(e,t)=>typeof e=="number"&&typeof t=="number"?e-t:String(e)<String(t)?-1:String(e)>String(t)?1:0,Xl=700;function wa(e,t,n){let{time:r,event:o,weight:s,x:i,stratumEnd:c}=e,a=r.length,l=t.length,f=()=>new Array(l).fill(0),m=()=>Array.from({length:l},f),u=f(),h=m(),p=0,g=0,w=f(),b=m(),N=f(),v=m(),y=0,d=0,M=x=>{let E=Math.exp(d-x);g*=E,y*=E;for(let T=0;T<l;T++){w[T]*=E,N[T]*=E;let R=b[T],S=v[T];for(let k=0;k<=T;k++)R[k]*=E,S[k]*=E}d=x};for(let x=a-1;x>=0;){c[x]&&(d=0,g=0,w=f(),b=m());let E=r[x],T=0,R=0;y=0;let S=x;do{let k=i[S],A=0;for(let L=0;L<l;L++)A+=t[L]*k[L];let q=s[S];Number.isFinite(A)&&(A-d>Xl?M(A):A-d<-Xl&&g===0&&y===0&&(d=A));let I=Math.exp(A-d)*q;if(o[S]){T++,R+=q,y+=I,p+=q*A;for(let L=0;L<l;L++){u[L]+=q*k[L],N[L]+=I*k[L];let O=v[L];for(let P=0;P<=L;P++)O[P]+=I*k[L]*k[P]}}else{g+=I;for(let L=0;L<l;L++){w[L]+=I*k[L];let O=b[L];for(let P=0;P<=L;P++)O[P]+=I*k[L]*k[P]}}S--}while(S>=0&&r[S]===E&&!c[S]);if(T>0){let k=n?T:1,A=n?T:1,q=R/k;for(let I=0;I<k;I++){g+=n?y/A:y,p-=q*(Math.log(g)+d);for(let L=0;L<l;L++){w[L]+=n?N[L]/A:N[L];let O=w[L]/g;u[L]-=q*O;let P=b[L],W=v[L],D=h[L];for(let F=0;F<=L;F++)P[F]+=n?W[F]/A:W[F],D[F]+=q*(P[F]-O*w[F])/g}}N.fill(0);for(let I of v)I.fill(0)}x=S}for(let x=0;x<l;x++)for(let E=0;E<x;E++)h[E][x]=h[x][E];return{loglik:p,score:u,info:h}}function ba(e,t){let n=e.length,r=0,o=!1;for(let a=0;a<n;a++){let l=e[a][a];l>0&&((!o||l>r)&&(r=l),o=!0)}let s=o?r*t:t,i=0,c=!0;for(let a=0;a<n;a++){let l=e[a][a];if(!Number.isFinite(l)||l<s){e[a][a]=0,l<-8*s&&(c=!1);for(let f=a+1;f<n;f++)e[f][a]=0;continue}i++;for(let f=a+1;f<n;f++){let m=e[f][a]/l;e[f][a]=m,e[f][f]-=m*m*l;for(let u=f+1;u<n;u++)e[u][f]-=m*e[u][a]}}return c?i:-i}function va(e,t){let n=e.length,r=t.slice();for(let o=0;o<n;o++){let s=r[o];for(let i=0;i<o;i++)s-=r[i]*e[o][i];r[o]=s}for(let o=n-1;o>=0;o--){if(e[o][o]===0){r[o]=0;continue}let s=r[o]/e[o][o];for(let i=o+1;i<n;i++)s-=r[i]*e[i][o];r[o]=s}return r}function Na(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 s=n;s<r;s++)e[n][s]+=o*e[r][s]}}for(let n=0;n<t;n++)for(let r=0;r<n;r++)e[n][r]=e[r][n];return e}var Po=(e,t)=>({statistic:e,df:t,pValue:_t(e,t,{lowerTail:!1})});function $d(e,t){let n=e.length,r=()=>new Array(t).fill(NaN),o=Po(0,0),s=new Array(t).fill(0),i=r(),c=r();return{coefficients:r(),var:Array.from({length:t},()=>new Array(t).fill(0)),loglik:[0,0],score:0,wald:0,iter:0,means:Array.from({length:t},(a,l)=>qe(e.map(f=>f[l]))),n,nevent:0,linearPredictors:new Array(n).fill(0),se:s,z:i,p:c,stdError:s,zValue:i,pValue:c,lower:r(),upper:r(),logtest:o,sctest:{...o},waldtest:{...o},rsq:{rsq:0,maxrsq:0},warnings:[]}}function zd(e,t,n,r={}){C(r,Cd,"coxph"),C(Wt(r,"control"),Dd,"coxph","control");let{ties:o="efron",weights:s,strata:i,init:c,control:a={},nocenter:l=[-1,0,1],confLevel:f=.95}=r,{eps:m=1e-9,iterMax:u=20,tolerChol:h=2**-39}=a,p=a.tolerInf??Math.sqrt(m);if(o!=="efron"&&o!=="breslow")throw new RangeError(`invalid 'ties': ${String(o)}`);if(!(m>0&&m<1/0))throw new RangeError("'eps' must be a positive finite number");if(!(h>0&&h<1/0))throw new RangeError("'tolerChol' must be a positive finite number");if(!(p>0&&p<1/0))throw new RangeError("'tolerInf' must be a positive finite number");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 g=e.length;if(t.length!==g||n.length!==g)throw new RangeError("'time', 'status' and 'x' must have the same length");if(s!==void 0&&s.length!==g)throw new RangeError("'weights' must have the same length as 'time'");if(i!==void 0&&i.length!==g)throw new RangeError("'strata' must have the same length as 'time'");let w=g===0?0:n[0].length;if(w<1)throw new RangeError("'x' must have at least one column");if(n.some(V=>V.length!==w))throw new RangeError("the rows of 'x' must have the same length");if(c!==void 0&&c.length!==w)throw new RangeError("'init' must have one value per column of 'x'");if(c?.some(V=>!Number.isFinite(V)))throw new RangeError("'init' must contain finite values");let b=[];for(let V=0;V<g;V++){if(Number.isNaN(e[V])||Number.isNaN(t[V])||n[V].some(re=>Number.isNaN(re))||s!==void 0&&Number.isNaN(s[V])||i!==void 0&&Pd(i[V]))continue;let U=t[V];if(U!==0&&U!==1)throw new RangeError("'status' must be 0 (censored) or 1 (event)");if(e[V]===-1/0||e[V]===1/0&&U===1)throw new RangeError("event times must be finite");b.push(V)}let N=b.length;if(N===0)throw new RangeError("no observations without missing values");if(b.some(V=>n[V].some(U=>!Number.isFinite(U))))throw new RangeError("'x' contains infinite values");let v=b.map(V=>s===void 0?1:s[V]);if(v.some(V=>!(V>0)))throw new RangeError("Invalid weights, must be >0");if(v.some(V=>!Number.isInteger(V)))throw new RangeError("non-integer weights (robust variance) are not supported yet");let y=0;for(let V of b)y+=t[V];if(y===0)return $d(b.map(V=>n[V]),w);let d=Do(b.map(V=>e[V])),M=new Array(N).fill(0);if(i!==void 0){let V=Pe(i),U=Array.from(new Set(b.map(de=>V[de]))).sort(Vd),re=new Map;U.forEach((de,he)=>re.set(de,he)),M=b.map(de=>re.get(V[de]))}let x=Array.from({length:N},(V,U)=>U).sort((V,U)=>M[V]-M[U]||d[V]-d[U]||V-U),E=0;for(let V of v)E+=V;let T=x.map(V=>v[V]),R=x.map(V=>n[b[V]].slice()),S=new Array(w).fill(0),k=new Array(w).fill(1),A=new Array(w).fill(0),q=new Set(l);for(let V=0;V<w;V++){if(R.every(he=>q.has(he[V])))continue;let U=R.map(he=>he[V]),re=he=>{let Re=0;for(let ue=0;ue<N;ue++)Re+=T[ue]*he[ue];let ft=Re/E,zt=0,on=he.map((ue,be)=>(zt+=T[be]*Math.abs(ue-ft),ue-ft)),me=zt===0?1:E/zt;return{m:ft,s:me,d:on,ok:Number.isFinite(ft)&&me>=2**-1022&&me<1/0}},de=re(U);if(!de.ok){let{groups:he,k:Re}=ee([U]);de=re(he[0]),A[V]=Re}for(let he=0;he<N;he++)R[he][V]=de.d[he]*de.s;S[V]=_(de.m,A[V]),k[V]=de.s}let I={time:x.map(V=>d[V]),event:x.map(V=>t[b[V]]===1),weight:T,x:R,stratumEnd:x.map((V,U)=>U===N-1||M[x[U+1]]!==M[V])},L=o==="efron",O=c===void 0?new Array(w).fill(0):c.map((V,U)=>_(V/k[U],A[U])),P=wa(I,O,L),W=[P.loglik,P.loglik],D=P.info.map(V=>V.slice()),F=ba(D,h),K=va(D,P.score),X=0;for(let V=0;V<w;V++)X+=P.score[V]*K[V];let te=0,ne,fe=!1;if(u===0||!Number.isFinite(P.loglik))ne=Na(D);else{let V=P.loglik,U=O.map((he,Re)=>he+K[Re]),re=0,de=!1;for(te=1;te<=u;te++){P=wa(I,U,L),D=P.info.map(Re=>Re.slice()),F=ba(D,h);let he=Number.isFinite(P.loglik)&&P.score.every(Number.isFinite)&&P.info.every(Re=>Re.every(Number.isFinite));if(he&&(P.loglik===V||Math.abs(1-V/P.loglik)<=m)){de=!0,W[1]=P.loglik,O=U;break}!he||P.loglik<V?(re++,U=U.map((Re,ft)=>(Re+re*O[ft])/(re+1))):(re=0,V=P.loglik,W[1]=V,K=va(D,P.score),O=U,U=U.map((Re,ft)=>Re+K[ft]))}de?ne=Na(D):(te=u+1,fe=!0,u>1?(P=wa(I,O,L),W[1]=P.loglik,ne=P.info.map((he,Re)=>he.map((ft,zt)=>Re===zt?he[Re]>0?1/he[Re]:0:-0))):ne=Na(D))}let Te=k.map(V=>Math.floor(Math.log2(V))),Oe=k.map((V,U)=>_(V,-Te[U])),Ue=Te.map((V,U)=>V-A[U]),Mt=O.map((V,U)=>V*Oe[U]),Q=P.score.map((V,U)=>V/Oe[U]),J=ne.map((V,U)=>V.map((re,de)=>re*Oe[U]*Oe[de])),$=Mt.map((V,U)=>_(V,Ue[U])),Y=J.map((V,U)=>V.map((re,de)=>_(re,Ue[U]+Ue[de]))),B=$.slice();if(u>0&&Math.abs(F)<w)for(let V=0;V<w;V++)ne[V][V]===0&&(B[V]=NaN);let pe=0;for(let V=0;V<w;V++)pe+=$[V]*S[V];let Fe=b.map(V=>{let U=0;for(let re=0;re<w;re++)U+=n[V][re]*$[re];return U-pe}),tt=new ke;if(u>1){let V=J.map((U,re)=>{let de=0;for(let he=0;he<w;he++)de+=Q[he]*J[he][re];return Math.abs(de)});if(fe)tt.add("Ran out of iterations and did not converge"),(ve(Fe)>500||V.some(U=>!Number.isFinite(U)))&&tt.add("one or more coefficients may be infinite");else{let U=[];for(let re=0;re<w;re++){let de=V[re];(!Number.isFinite(Q[re])||_(de,Ue[re])>m&&de>p*Math.abs(Mt[re]))&&U.push(re+1)}U.length>0&&tt.add(`Loglik converged before variable ${U.join(",")} ; coefficient may be infinite. `)}}let nt=B.map((V,U)=>Number.isNaN(V)?-1:U).filter(V=>V>=0),Ge=nt.length,Ye=nt.map(V=>Mt[V]-(c===void 0?0:_(c[V],-Ue[V]))),lt=0;if(Ge===1)lt=Ye[0]*Ye[0]/J[nt[0]][nt[0]];else if(Ge>1){let V=nt.map(re=>nt.map(de=>J[re][de]));ba(V,h);let U=va(V,Ye);for(let re=0;re<Ge;re++)lt+=Ye[re]*U[re]}let kt=(1-f)/2,Vt=kt<1e-8?Z(kt,{lowerTail:!1}):Z((1+f)/2),vn=J.map((V,U)=>Math.sqrt(V[U])),$t=vn.map((V,U)=>_(V,Ue[U])),At=B.map((V,U)=>(Number.isNaN(V)?V:Mt[U])/vn[U]),Lr=At.map(V=>_t(V*V,1,{lowerTail:!1})),Gn=B.map((V,U)=>Math.exp(V-Vt*$t[U])),Or=B.map((V,U)=>Math.exp(V+Vt*$t[U])),Yn=-2*(W[0]-W[1]);return z("coxph",{coefficients:B,var:Y,loglik:W,score:X,wald:lt,iter:te,means:S,n:N,nevent:y,linearPredictors:Fe,se:$t,z:At,p:Lr,stdError:$t,zValue:At,pValue:Lr,lower:Gn,upper:Or,logtest:Po(Yn,Ge),sctest:Po(X,Ge),waldtest:Po(lt,Ge),rsq:{rsq:1-Math.exp(-Yn/N),maxrsq:1-Math.exp(2*W[0]/N)},warnings:tt.list})}var Kd=["center"],Wd=2**-1022;function jd(e,t,n={}){C(n,Kd,"leveneTest");let r=ae(n.center??"median",["median","mean"]),{groups:o}=Jt(e,t);if(o.length<2)throw new RangeError("contrasts can be applied only to factors with 2 or more levels");let s=l=>{let f=r==="median"?bo(l):qe(l);return l.map(m=>Math.abs(m-f))},i=ee(o).groups.map(s),c=(l,f)=>l.some(m=>m>0&&m<Wd)||l.every(m=>m===0)&&o[f].some(m=>m!==o[f][0]);if(i.some(c)){let l=o.map(u=>{let h=ee([u]);return{d:s(h.groups[0]),k:h.k}}),f=Math.max(...l.map(u=>u.d.some(h=>h>0)?Math.floor(Math.log2(Math.max(...u.d)))+u.k:-1/0)),m=Number.isFinite(f)?f:0;i=l.map(u=>u.d.map(h=>_(h,u.k-m)))}let a=hl(hn(i));return{colNames:["Df","F value","Pr(>F)"],rowNames:["group",""],table:[[a.df[0],a.fValue,a.pValue],[a.df[1],NaN,NaN]],warnings:[]}}var Ud=["score"],Gd=["alternative","exact","variant","levels"];function Yd(e,t,...n){C(kn(n),Ud,"propTrendTest");let{score:r}=Sn(["score"],n),o=Array.from(e),s=Array.from(t),i=o.length,c=r===void 0?o.map((N,v)=>v+1):Array.from(r);if(s.length!==i||c.length!==i)throw new RangeError("'x', 'n' and 'score' must have the same length");if(s.some(N=>!(N>0&&N<1/0)))throw new RangeError("elements of 'n' must be positive and finite");if(o.some((N,v)=>!(N>=0&&N<=s[v])))throw new RangeError("elements of 'x' must be between 0 and 'n'");if(o.every(N=>N===0)||o.every((N,v)=>N===s[v]))throw new RangeError("all proportions are 0 or all are 1: the test for trend is not defined");let a=s.reduce((N,v)=>N+v,0),l=o.reduce((N,v)=>N+v,0)/a,f=s.map(N=>N/(l*(1-l))),m=o.map((N,v)=>N/s[v]),u=c.flatMap((N,v)=>Number.isNaN(N)?[]:[v]);if(u.length===0)throw new RangeError("no non-missing scores");if(u.some(N=>!Number.isFinite(c[N])))throw new RangeError("non-missing scores must be finite");if(u.every(N=>c[N]===c[u[0]]))throw new RangeError("at least 2 distinct non-missing scores are needed");let h=c,{sw:p,sxy:g,sxx:w}=Ma(f,m,h,u);if(!(w>=2**-1022&&w<1/0&&Math.abs(g)<1/0)){let N=ee([u.map(v=>c[v])]);N.k!==0&&(h=c.slice(),u.forEach((v,y)=>h[v]=N.groups[0][y]),{sw:p,sxy:g,sxx:w}=Ma(f,m,h,u))}let b=Ce(g,w);if(!(a<1/0)||l>0&&l<1&&!(p<1/0&&Math.abs(g)<1/0&&w<1/0)){let{groups:[N,v],k:y}=ee([o,s]),d=N.reduce((x,E)=>x+E,0)/v.reduce((x,E)=>x+E,0),M=Ma(v,m,h,u);b=_(M.sxy/d*(M.sxy/M.sxx/(1-d)),y)}return{method:"Chi-squared Test for Trend in Proportions",statistic:b,statisticName:"X-squared",parameter:1,parameterName:"df",pValue:Ne(b,.5,2,!1,!1),warnings:[]}}function Ma(e,t,n,r){let o=r.reduce((l,f)=>l+e[f],0),s=r.reduce((l,f)=>l+e[f]*n[f],0)/o,i=r.reduce((l,f)=>l+e[f]*t[f],0)/o,c=0,a=0;for(let l of r){let f=n[l]-s;c+=e[l]*f*(t[l]-i),a+=e[l]*f*f}return{sw:o,sxy:c,sxx:a}}var Hd=2**24,Xd=2**32,Bd=`Sample size > 100 or data with ties
10
+ p-value based on normal approximation. Specify nperm for permutation p-value`,Qd=`Sample data with ties
11
+ p-value based on normal approximation. Specify nperm for permutation p-value.`;function Zd(e,t,n,r=!1){let o=Array.from(e);if(o.length!==t.length)throw new RangeError("'x' and 'g' must have the same length");let s=f=>typeof f=="number"&&(r?!Number.isFinite(f):Number.isNaN(f)),i=o.flatMap((f,m)=>s(f)||s(t[m])?[]:[m]);n===void 0&&(t=Pe(t));let c;if(n!==void 0)c=n.map(Ve);else{let f=[...new Set(i.map(m=>t[m]))];c=f.every(m=>typeof m=="number")?f.sort((m,u)=>m-u).map(Ve):[...new Set(f.map(Ve))].sort(pt)}let a=c.map(()=>[]),l=new Map(c.map((f,m)=>[f,m]));for(let f of i){let m=l.get(Ve(t[f]));if(m===void 0)throw new RangeError(`the group '${Ve(t[f])}' is not among 'levels'`);a[m].push(o[f])}return a}function Jd(e,t){let n=new Map;for(let l of t)n.set(l,(n.get(l)??0)+1);let r=[...n.values()],o=t.length,s=(l,f)=>l.reduce((m,u)=>m+f(u),0),i=(o*(o-1)*(2*o+5)-s(e,l=>l*(l-1)*(2*l+5))-s(r,l=>l*(l-1)*(2*l+5)))/72,c=o<3?0:s(e,l=>l*(l-1)*(l-2))*s(r,l=>l*(l-1)*(l-2))/(36*o*(o-1)*(o-2)),a=s(e,l=>l*(l-1))*s(r,l=>l*(l-1))/(8*o*(o-1));return i+c+a}function e0(e,t,n={}){C(n,Gd,"jonckheereTest");let r=ae(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 s=Zd(e,t,n.levels,o==="clinfun");if(s.filter(N=>N.length>0).length<2)throw new RangeError(o==="clinfun"?"only one group has non-missing data":"all observations are in the same group");let i=s.flat(),c=i.length,a=new Set(i).size<c,l=0,f=[],m=c;s.forEach((N,v)=>{m-=N.length,f.push([N.length,m]);for(let y=v+1;y<s.length;y++)for(let d of N)for(let M of s[y])l+=d<M?1:d===M?.5:0});let u=[],h=!a&&c<=100;o==="DescTools"&&n.exact!==void 0?(h=n.exact,h&&a&&(h=!1,u.push(Qd))):h||u.push(Bd);let p,g;if(h){let N=1,v=0;for(let[d,M]of f)d===0||M===0||(v+=N*(d*M+1),N+=d*M);if(N>Hd||v>Xd)throw new RangeError(`the exact distribution of JT has ${N} values and needs ${v} steps, too many: use exact: false`);let y=[1];for(let[d,M]of f){if(d===0||M===0)continue;let x=Array.from({length:d*M+1},(T,R)=>Ha(R,d,M,!1)),E=new Array(y.length+x.length-1).fill(0);y.forEach((T,R)=>x.forEach((S,k)=>E[R+k]+=T*S)),y=E}p=0,g=0,y.forEach((d,M)=>{M<=l&&(p+=d),M>=l&&(g+=d)})}else{let N=f.reduce((d,[M,x])=>d+M*x,0)/2,v=a?Jd(s.map(d=>d.length),i):f.reduce((d,[M,x])=>d+M*x*(M+x+1)/12,0),y=(l-N)/Math.sqrt(v);p=j(y),g=1-p,g<1e-8&&(g=j(y,{lowerTail:!1}))}let b=r==="increasing"?g:r==="decreasing"?p:Math.min(1,2*Math.min(g,p,o==="clinfun"?.5:1));return z("jonckheereTest",{method:"Jonckheere-Terpstra test",alternative:r,statistic:l,statisticName:"JT",pValue:b,warnings:u})}var t0=["nClasses","adjust"];function Ar(e){return Je(Array.from(e)).sort((t,n)=>t-n)}function Bl(e){if(e.length>0&&!(Number.isFinite(e[0])&&Number.isFinite(e[e.length-1])))throw new RangeError("'x' must be finite")}function Vo(e){Bl(e);let t=Wa(e);if(t.some(Number.isNaN))throw new RangeError("all 'x' values are identical");return t}function n0(e){let t=Ar(e),n=t.length;if(n<8)throw new RangeError("sample size must be greater than 7");let r=Vo(t),o=0;for(let a=1;a<=n;a++)o+=(2*a-1)*(j(r[a-1],{logP:!0})+j(r[n-a],{lowerTail:!1,logP:!0}));let s=-n-o/n,i=(1+.75/n+2.25/n**2)*s,c;return i<.2?c=1-Math.exp(-13.436+101.14*i-223.73*i**2):i<.34?c=1-Math.exp(-8.318+42.796*i-59.938*i**2):i<.6?c=Math.exp(.9177-4.279*i-1.38*i**2):i<10?c=Math.exp(1.2937-5.709*i+.0186*i**2):c=37e-25,{method:"Anderson-Darling normality test",statistic:s,statisticName:"A",pValue:c,warnings:[]}}function r0(e){let t=Ar(e),n=t.length;if(n<8)throw new RangeError("sample size must be greater than 7");let r=Vo(t),o=1/(12*n);r.forEach((a,l)=>{o+=(j(a)-(2*(l+1)-1)/(2*n))**2});let s=(1+.5/n)*o,i=[],c;return s<.0275?c=1-Math.exp(-13.953+775.5*s-12542.61*s**2):s<.051?c=1-Math.exp(-5.903+179.546*s-1515.29*s**2):s<.092?c=Math.exp(.886-31.62*s+10.897*s**2):s<1.1?c=Math.exp(1.111-34.242*s+12.832*s**2):(c=737e-12,i.push("p-value is smaller than 7.37e-10, cannot be computed more accurately")),z("cvmTest",{method:"Cramer-von Mises normality test",statistic:o,statisticName:"W",pValue:c,warnings:i})}function o0(e){let t=Ar(e),n=t.length;if(n<5)throw new RangeError("sample size must be greater than 4");let r=Vo(t).map(f=>j(f)),o=-1/0,s=-1/0;r.forEach((f,m)=>{o=Math.max(o,(m+1)/n-f),s=Math.max(s,f-m/n)});let i=Math.max(o,s),c=n<=100?i:i*(n/100)**.49,a=n<=100?n:100,l=Math.exp(-7.01256*c**2*(a+2.78019)+2.99587*c*Math.sqrt(a+2.78019)-.122119+.974598/Math.sqrt(a)+1.67997/a);if(l>.1){let f=(Math.sqrt(n)-.01+.85/Math.sqrt(n))*i;f<=.302?l=1:f<=.5?l=2.76773-19.828315*f+80.709644*f**2-138.55152*f**3+81.218052*f**4:f<=.9?l=-4.901232+40.662806*f-97.490286*f**2+94.029866*f**3-32.355711*f**4:f<=1.31?l=6.198765-19.558097*f+23.186922*f**2-12.234627*f**3+2.423045*f**4:l=0}return{method:"Lilliefors (Kolmogorov-Smirnov) normality test",statistic:i,statisticName:"D",pValue:l,warnings:[]}}function s0(e){let t=Ar(e),n=t.length;if(n<5||n>5e3)throw new RangeError("sample size must be between 5 and 5000");if(Bl(t),t[0]===t[n-1])throw new RangeError("all 'x' values are identical");let r=t.map((m,u)=>Z((u+1-3/8)/(n+1/4))),o=ee([t]).groups[0],s=Math.min(Ka(o,r)**2/(_e(o)*_e(r)),1),i=Math.log(n),c=Math.log(i),a=-1.2725+1.0521*(c-i),l=1.0308-.26758*(c+2/i),f=j((Math.log(1-s)-a)/l,{lowerTail:!1});return{method:"Shapiro-Francia normality test",statistic:s,statisticName:"W",pValue:f,warnings:[]}}function a0(e,t={}){C(t,t0,"pearsonTest");let n=Ar(e),r=n.length,o=t.nClasses??Math.ceil(2*r**(2/5)),s=t.adjust??!0;if(!(Number.isInteger(o)&&o-1-(s?2:0)>0))throw new RangeError("'nClasses' must be an integer leaving positive degrees of freedom");if(o>2147483647)throw new RangeError("attempt to make a table with >= 2^31 elements");let i=[];for(let f of Vo(n)){let m=Math.min(o,Math.floor(1+o*j(f)));m>=1&&i.push(m)}i.sort((f,m)=>f-m);let c=r/o,a=0;for(let f=1,m=0;f<=o;f++){let u=0;for(;m<i.length&&i[m]===f;)u++,m++;a=a+(u-c)**2/c}let l=o-1-(s?2:0);return{method:"Pearson chi-square normality test",statistic:a,statisticName:"P",pValue:Ne(a,l/2,2,!1,!1),warnings:[]}}var i0=["weight"],c0=["exact"],l0=["model","type","unit","r0","confLevel"];function qr(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(s=>s[o]))}function $o(e,t){if(e.some(n=>n.some(Number.isNaN)))throw new RangeError(`'${t}' contains missing values`);if(e.some(n=>n.some(r=>!Number.isFinite(r))))throw new RangeError(`'${t}' contains infinite values`)}var Zl=e=>2*j(-Math.abs(e));function f0(e,t={}){C(t,i0,"kappa2");let n=ae(t.weight,["unweighted","equal","squared"]);if(e.some(x=>x.length!==2))throw new TypeError("'ratings' must be a matrix with two columns");let r=e.filter(x=>!x.some(Number.isNaN)),o=r.length;if(o===0)throw new RangeError("no complete rating pairs");if(o<2)throw new RangeError("at least 2 complete rating pairs are needed");let s=r.map(x=>Rn(x[0])),i=r.map(x=>Rn(x[1])),c=[...new Set([...s,...i])].sort((x,E)=>x<E?-1:x>E?1:0),a=c.length,l=new Map(c.map((x,E)=>[x,E])),f=Array.from({length:a},()=>new Array(a).fill(0));for(let x=0;x<o;x++)f[l.get(s[x])][l.get(i[x])]++;let m=x=>n==="unweighted"?x===0?1:0:n==="equal"?1-x/(a-1):1-x*x/((a-1)*(a-1)),u=Array.from({length:a},(x,E)=>Array.from({length:a},(T,R)=>m(Math.abs(E-R)))),h=f.map(x=>se(x)/o),p=Array.from({length:a},(x,E)=>se(f.map(T=>T[E]))/o),g=x=>{let E=[];for(let T=0;T<a;T++)for(let R=0;R<a;R++)E.push(x(T,R));return se(E)},w=g((x,E)=>f[x][E]*u[x][E])/o,b=g((x,E)=>h[x]*p[E]*u[x][E]),N=(w-b)/(1-b),v=Array.from({length:a},(x,E)=>se(p.map((T,R)=>T*u[E][R]))),y=Array.from({length:a},(x,E)=>se(h.map((T,R)=>T*u[R][E]))),d=(g((x,E)=>h[x]*p[E]*(u[x][E]-(v[x]+y[E]))**2)-b*b)/(o*(1-b)**2),M=N/Math.sqrt(d);return{value:N,statistic:M,pValue:Zl(M),warnings:[]}}function m0(e,t={}){C(t,c0,"kappamFleiss");let n=t.exact??!1,r=qr(e,"ratings");$o(e,"ratings");let o=e.length,s=r.length;if(o<2||s<2)throw new RangeError("at least 2 subjects and 2 raters are needed");let i=e.flat(),c=Pe(i),a=[...new Set(c)].sort((d,M)=>d-M),l=new Map(a.map((d,M)=>[d,M])),f=new Map(i.map((d,M)=>[d,l.get(c[M])])),m=a.length,u=e.map(d=>{let M=new Array(m).fill(0);for(let x of d)M[f.get(x)]++;return M}),h=se(u.map(d=>(se(d.map(M=>M*M))-s)/(s*(s-1))))/o,p=Array.from({length:m},(d,M)=>se(u.map(x=>x[M]))/(o*s)),g=se(p.map(d=>d*d));if(n){let d=r.map(x=>{let E=new Array(m).fill(0);for(let T of x)E[f.get(T)]++;return E.map(T=>T/o)}),M=Array.from({length:m},(x,E)=>_e(d.map(T=>T[E]))*(s-1)/s);g-=se(M)/(s-1)}let w=(h-g)/(1-g);if(n)return{value:w,warnings:[]};let b=se(p.map(d=>d*(1-d))),N=se(p.map(d=>d*(1-d)*(1-d-d))),v=2/(b*b*o*s*(s-1))*(b*b-N),y=w/Math.sqrt(v);return{value:w,statistic:y,pValue:Zl(y),warnings:[]}}function u0(e,t={}){C(t,l0,"icc");let n=ae(t.model,["oneway","twoway"]),r=ae(t.type,["consistency","agreement"]),o=ae(t.unit,["single","average"]),s=t.r0??0,i=t.confLevel??.95;Xe(i),qr(e,"ratings"),$o(e,"ratings");let c=ee(e).groups,a=qr(c,"ratings"),l=c.length,f=a.length;if(l<2||f<2)throw new RangeError("at least 2 subjects and 2 raters are needed");if(!(s>=-1&&s<1))throw new RangeError("'r0' must be in [-1, 1)");let m=1-i,u=_e(c.map(D=>qe(D)))*f,h=_e(a.map(D=>qe(D)))*l,p=qe(c.map(D=>_e(D))),g=_e(c.flat())*(l*f-1),w=g-u*(l-1)-h*(f-1),b=w>2**-20*g?w/((l-1)*(f-1)):g0(c)/((l-1)*(f-1)),N=(D,F,K)=>Me(D,F,K,!1,!1),v=(D,F)=>m/2<1e-8?Lt(m/2,D,F,!1,!1):Lt(1-m/2,D,F,!0,!1),y=l-1;if(n==="oneway"||r==="consistency"){let D=n==="oneway"?p:b,F=n==="oneway"?l*(f-1):(l-1)*(f-1),K=u/D,X=K/v(y,F),te=K*v(F,y),[ne,fe,Te]=o==="single"?[(u-D)/(u+(f-1)*D),K*(1-s)/(1+(f-1)*s),Oe=>(Oe-1)/(Oe+f-1)]:[(u-D)/u,K*(1-s),Oe=>1-1/Oe];return{value:ne,Fvalue:fe,fValue:fe,df1:y,df2:F,pValue:N(fe,y,F),lbound:Te(X),ubound:Te(te),warnings:[]}}let d=o==="single"?f:1,M=D=>[d*D/(l*(1-D)),1+d*D*(l-1)/(l*(1-D))],x=(D,F)=>ja([D*h,F*b],[f-1,(l-1)*(f-1)]),E=o==="single"?(u-b)/(u+(f-1)*b+f/l*(h-b)):(u-b)/(u+(h-b)/l),[T,R]=M(s),S=u/(T*h+R*b),k=x(T,R),[A,q]=[f*E/(l*(1-E)),1+f*E*(l-1)/(l*(1-E))],I=x(A,q),L=v(y,I),O=v(I,y),[P,W]=o==="single"?[l*(u-L*b)/(L*(f*h+(f*l-f-l)*b)+l*u),l*(O*u-b)/(f*h+(f*l-f-l)*b+l*O*u)]:[l*(u-L*b)/(L*(h-b)+l*u),l*(O*u-b)/(h-b+l*O*u)];return{value:E,Fvalue:S,fValue:S,df1:y,df2:k,pValue:N(S,y,k),lbound:P,ubound:W,warnings:[]}}function Ql(e,t){let n=t.length,r=se(t.flatMap(s=>t.map(i=>e[s][i])));return(1-se(t.map(s=>e[s][s]))/r)*n/(n-1)}function h0(e){let t=qr(e,"x").length;if($o(e,"x"),t<2)throw new RangeError("'x' needs at least two items");if(e.length<2)throw new RangeError("'x' needs at least two subjects");let n=no(ee(e).groups),r=cr(e),o=Array.from({length:t},(c,a)=>a),s=se(r.flat()),i;try{let c=Ke(r);i=o.map(a=>1-1/c[a][a])}catch(c){if(!r.flat().every(Number.isFinite))throw c;let a=ee(e).groups;i=o.map(l=>{let f=a.map(h=>h[l]),m=qn(Ee(a.map(h=>[1,...h.filter((p,g)=>g!==l)])),f),u=qe(f);return 1-se(m.map(h=>h*h))/se(f.map(h=>(h-u)**2))})}return z("cronbachAlpha",{rawAlpha:Ql(n,o),stdAlpha:(1-t/s)*t/(t-1),G6:1-(t-se(i))/s,averageR:(s-t)/(t*(t-1)),alphaDrop:o.map(c=>Ql(n,o.filter(a=>a!==c))),warnings:p0(e,r)})}function p0(e,t){let n=[],r=t.map((o,s)=>s).filter(o=>e.some(s=>s[o]!==e[0][o]));if(r.length<t.length&&n.push("Item = had no variance and was deleted but still is counted in the score"),r.length>=2){let{vectors:o}=dt(r.map(c=>r.map(a=>t[c][a])),{symmetric:!0}),s=o.map(c=>c[0]),i=se(s)<0?-1:1;s.some(c=>i*c<0)&&n.push(`Some items were negatively correlated with the first principal component and probably
12
12
  should be reversed.
13
- To do this, run the function again with the 'check.keys=TRUE' option`)}return n}function Gp(e,t,n){let r=ur(e,"x").length;go(e,"x");for(let a of[t,n])if(!Number.isInteger(a)||a<0||a>=r)throw new RangeError(`column index ${a} out of range (0 to ${r-1})`);if(t===n)throw new RangeError("'i' and 'j' must be different columns");let o=Qn(e),s=[t,n],c=Array.from({length:r},(a,l)=>l).filter(a=>a!==t&&a!==n),i=s.map(a=>s.map(l=>o[a][l]));if(c.length>0){let a=c.map(m=>c.map(u=>o[m][u])),l=c.map(m=>s.map(u=>o[m][u])),f=Ve(a,l);for(let m=0;m<2;m++)for(let u=0;u<2;u++)i[m][u]-=se(c.map((p,d)=>o[s[m]][p]*f[d][u]))}return i[0][1]/Math.sqrt(i[0][0]*i[1][1])}function Hp(e){let t=e.length,n=e[0].length,r=a=>a.reduce((l,f)=>Q(l,[f,0]),[0,0]),o=e.map(a=>Tt(r(a),n)),s=e[0].map((a,l)=>Tt(r(e.map(f=>f[l])),t)),c=Tt(r(e.flat()),t*n),i=[0,0];return e.forEach((a,l)=>a.forEach((f,m)=>{let u=Q(de(de([f,0],o[l]),s[m]),c);i=Q(i,pe(u,u))})),i[0]+i[1]}function wo(e){let t=e.length;if(t===0||e.some(n=>n.length!==t))throw new RangeError("'covmat' must be a square matrix");if(e.some(n=>n.some(r=>!Number.isFinite(r))))throw new RangeError("'covmat' must be finite");if(e.some((n,r)=>!(n[r]>0)))throw new RangeError("the diagonal of 'covmat' must be positive");if(!Ta(Na(e)))throw new RangeError("'covmat' must be symmetric")}var Xp=["covmat","nObs","rotation","scores","control"],Bp=["lower","start"];function ye(e){let t=0,n=0;for(let r of e){let[o,s]=Rt(t,r);t=o,n+=s}return t+n}var Os=(e,t)=>e[0].map((n,r)=>ye(e.map(o=>t(o[r]))));function Cc(e,t){let n=t.map(r=>1/Math.sqrt(r));return e.map((r,o)=>r.map((s,c)=>n[o]*s*n[c]))}function Pc(e,t,n){let r=ct(Cc(e,t),{symmetric:!0}),o=r.values.slice(0,n).map(s=>Math.sqrt(Math.max(s-1,0)));return r.vectors.map((s,c)=>s.slice(0,n).map((i,a)=>Math.sqrt(t[c])*(i*o[a])))}function Dc(e,t){let n=Os(e,i=>i*i),r=n.map((i,a)=>a).sort((i,a)=>n[a]-n[i]||i-a),o=e.map(i=>r.map(a=>i[a])),s=t?.map(i=>r.map(a=>i[a])),c=Os(o,i=>i).map(i=>i<0?-1:1);return o=o.map(i=>i.map((a,l)=>c[l]<0?-a:a)),s=s?.map(i=>i.map((a,l)=>c[l]<0?-a:a)),s===void 0?{L:o}:{L:o,rot:s}}var vt=(e,t)=>e.map(n=>t[0].map((r,o)=>{let s=0;for(let c=0;c<n.length;c++)s+=n[c]*t[c][o];return s})),In=e=>e[0].map((t,n)=>e.map(r=>r[n]));function bo(e){let t=e[0].length,n=e.map(m=>Math.sqrt(ye(m.map(u=>u*u)))),r=e.flatMap((m,u)=>n[u]>0?[u]:[]),o=r.map(m=>e[m].map(u=>u/n[m])),s=o.length,c=Array.from({length:t},(m,u)=>Array.from({length:t},(p,d)=>u===d?1:0));if(s===0)return{loadings:e.map(m=>m.slice()),rotmat:c};let i=In(o),a=0;for(let m=0;m<1e3;m++){let u=vt(o,c),p=u[0].map((v,N)=>{let b=0;for(let h=0;h<s;h++)b+=u[h][N]*u[h][N];return b}),d=u.map(v=>v.map((N,b)=>N**3-N*p[b]/s)),g=vt(i,d),w=Or(g);c=vt(w.u,In(w.v));let y=a;if(a=ye(w.d),a<y*(1+1e-5))break}let l=vt(o,c),f=e.map(m=>m.map(()=>0));return r.forEach((m,u)=>f[m]=l[u].map(p=>p*n[m])),{loadings:f,rotmat:c}}function Qp(e){let t=bo(e),n=t.loadings,r=n.map(i=>i.map(a=>a*Math.abs(a)**3)),o=vn(ve(n),r),s=Ve(vt(In(o),o)).map((i,a)=>i[a]),c=o.map(i=>i.map((a,l)=>a*Math.sqrt(s[l])));return{loadings:vt(n,c),rotmat:vt(t.rotmat,c)}}function Zp(e,t,n={}){C(n,Xp,"factanal"),C(Ft(n,"control"),Bp,"factanal","control");let r=n.scores??"none";if(r!=="none"&&r!=="regression"&&r!=="Bartlett")throw new RangeError("'arg' should be one of \u201Cnone\u201D, \u201Cregression\u201D, \u201CBartlett\u201D");let o,s;if(n.covmat!==void 0){if(r!=="none")throw new RangeError("requested scores without an 'x' matrix");wo(n.covmat),o=n.covmat.map(x=>x.slice()),s=n.nObs??NaN}else{if(e===null)throw new TypeError("neither 'x' nor 'covmat' supplied");o=Cr(e),s=e.length}let c=o.length;if(c<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 a=((c-i)**2-c-i)/2;if(a<0)throw new RangeError(`${i} ${i===1?"factor is":"factors are"} too many for ${c} variables`);let l=o.map((x,M)=>Math.sqrt(x[M])),f=o.map((x,M)=>x.map((R,T)=>R/(l[M]*l[T]))),m=n.control?.lower??.005,u=n.control?.start?.slice()??Ve(f).map((x,M)=>(1-i/(2*c))/x[M]),p=x=>{let M=ct(Cc(f,x),{symmetric:!0,onlyValues:!0}).values.slice(i);return-(ye(M.map(R=>Math.log(R)-R))-i+c)},d=x=>{let M=Pc(f,x,i);return x.map((R,T)=>{let S=0;for(let A=0;A<i;A++)S+=M[T][A]*M[T][A];return(S+R-f[T][T])/(R*R)})},g=Re(()=>Zn(u,p,{gr:d,method:"L-BFGS-B",lower:m,upper:1,control:{fnscale:1,parscale:new Array(c).fill(.01)}}));if(g.convergence!==0)throw new Error("unable to optimize from this starting value");let w=g.par,y=Dc(Pc(f,w,i)).L,v,N=n.rotation??"varimax";if(N!=="varimax"&&N!=="promax"&&N!=="none")throw new RangeError(`unknown rotation '${String(N)}'`);if(N!=="none"&&i>=2){let x=N==="varimax"?bo(y):Qp(y),M=Dc(x.loadings,x.rotmat);y=M.L,v=M.rot}let b=NaN,h=NaN;!Number.isNaN(s)&&a>0&&(b=(s-1-(2*c+5)/6-2*i/3)*g.value,h=St(b,a,{lowerTail:!1}));let E={uniquenesses:w,loadings:y,STATISTIC:b,dof:a,PVAL:h,converged:!0,objective:g.value,counts:g.counts,warnings:[]};return r!=="none"&&(E.scores=Jp(e,f,w,y,r)),v!==void 0&&(E.rotmat=v),E}function Jp(e,t,n,r,o){let s=e.length,c=e[0].map((m,u)=>ye(e.map(p=>p[u]))/s),i=e.map(m=>m.map((u,p)=>u-c[p])),a=Os(i,m=>m*m).map(m=>Math.sqrt(m/(s-1))),l=i.map(m=>m.map((u,p)=>u/a[p]));if(o==="regression")return vt(l,Ve(t,r));let f=r.map((m,u)=>m.map(p=>p/n[u]));return vt(l,In(Ve(vt(In(r),f),In(f))))}var eh=["nfactors","covmat"],Ln=2**-52,th=`
13
+ To do this, run the function again with the 'check.keys=TRUE' option`)}return n}function d0(e,t,n){let r=qr(e,"x").length;$o(e,"x");for(let a of[t,n])if(!Number.isInteger(a)||a<0||a>=r)throw new RangeError(`column index ${a} out of range (0 to ${r-1})`);if(t===n)throw new RangeError("'i' and 'j' must be different columns");let o=cr(e),s=[t,n],i=Array.from({length:r},(a,l)=>l).filter(a=>a!==t&&a!==n),c=s.map(a=>s.map(l=>o[a][l]));if(i.length>0){let a=i.map(m=>i.map(u=>o[m][u])),l=i.map(m=>s.map(u=>o[m][u])),f=Ke(a,l);for(let m=0;m<2;m++)for(let u=0;u<2;u++)c[m][u]-=se(i.map((h,p)=>o[s[m]][h]*f[p][u]))}return c[0][1]/Math.sqrt(c[0][0]*c[1][1])}function g0(e){let t=e.length,n=e[0].length,r=a=>a.reduce((l,f)=>G(l,[f,0]),[0,0]),o=e.map(a=>Ie(r(a),n)),s=e[0].map((a,l)=>Ie(r(e.map(f=>f[l])),t)),i=Ie(r(e.flat()),t*n),c=[0,0];return e.forEach((a,l)=>a.forEach((f,m)=>{let u=G(ie(ie([f,0],o[l]),s[m]),i);c=G(c,ce(u,u))})),c[0]+c[1]}function zo(e){let t=e.length;if(t===0||e.some(n=>n.length!==t))throw new RangeError("'covmat' must be a square matrix");if(e.some(n=>n.some(r=>!Number.isFinite(r))))throw new RangeError("'covmat' must be finite");if(e.some((n,r)=>!(n[r]>0)))throw new RangeError("the diagonal of 'covmat' must be positive");if(!pi(ii(e)))throw new RangeError("'covmat' must be symmetric")}var w0=["covmat","nObs","rotation","scores","control"],b0=["lower","start"];function xe(e){let t=0,n=0;for(let r of e){let[o,s]=He(t,r);t=o,n+=s}return t+n}var ya=(e,t)=>e[0].map((n,r)=>xe(e.map(o=>t(o[r]))));function tf(e,t){let n=t.map(r=>1/Math.sqrt(r));return e.map((r,o)=>r.map((s,i)=>n[o]*s*n[i]))}function Jl(e,t,n){let r=dt(tf(e,t),{symmetric:!0}),o=r.values.slice(0,n).map(s=>Math.sqrt(Math.max(s-1,0)));return r.vectors.map((s,i)=>s.slice(0,n).map((c,a)=>Math.sqrt(t[i])*(c*o[a])))}function ef(e,t){let n=ya(e,c=>c*c),r=n.map((c,a)=>a).sort((c,a)=>n[a]-n[c]||c-a),o=e.map(c=>r.map(a=>c[a])),s=t?.map(c=>r.map(a=>c[a])),i=ya(o,c=>c).map(c=>c<0?-1:1);return o=o.map(c=>c.map((a,l)=>i[l]<0?-a:a)),s=s?.map(c=>c.map((a,l)=>i[l]<0?-a:a)),s===void 0?{L:o}:{L:o,rot:s}}var St=(e,t)=>e.map(n=>t[0].map((r,o)=>{let s=0;for(let i=0;i<n.length;i++)s+=n[i]*t[i][o];return s})),Kn=e=>e[0].map((t,n)=>e.map(r=>r[n]));function Ko(e){let t=e[0].length,n=e.map(m=>Math.sqrt(xe(m.map(u=>u*u)))),r=e.flatMap((m,u)=>n[u]>0?[u]:[]),o=r.map(m=>e[m].map(u=>u/n[m])),s=o.length,i=Array.from({length:t},(m,u)=>Array.from({length:t},(h,p)=>u===p?1:0));if(s===0)return{loadings:e.map(m=>m.slice()),rotmat:i};let c=Kn(o),a=0;for(let m=0;m<1e3;m++){let u=St(o,i),h=u[0].map((N,v)=>{let y=0;for(let d=0;d<s;d++)y+=u[d][v]*u[d][v];return y}),p=u.map(N=>N.map((v,y)=>v**3-v*h[y]/s)),g=St(c,p),w=Zr(g);i=St(w.u,Kn(w.v));let b=a;if(a=xe(w.d),a<b*(1+1e-5))break}let l=St(o,i),f=e.map(m=>m.map(()=>0));return r.forEach((m,u)=>f[m]=l[u].map(h=>h*n[m])),{loadings:f,rotmat:i}}function v0(e){let t=Ko(e),n=t.loadings,r=n.map(c=>c.map(a=>a*Math.abs(a)**3)),o=An(Ee(n),r),s=Ke(St(Kn(o),o)).map((c,a)=>c[a]),i=o.map(c=>c.map((a,l)=>a*Math.sqrt(s[l])));return{loadings:St(n,i),rotmat:St(t.rotmat,i)}}function N0(e,t,n={}){C(n,w0,"factanal"),C(Wt(n,"control"),b0,"factanal","control");let r=n.scores??"none";if(r!=="none"&&r!=="regression"&&r!=="Bartlett")throw new RangeError("'arg' should be one of \u201Cnone\u201D, \u201Cregression\u201D, \u201CBartlett\u201D");let o,s;if(n.covmat!==void 0){if(r!=="none")throw new RangeError("requested scores without an 'x' matrix");zo(n.covmat),o=n.covmat.map(x=>x.slice()),s=n.nObs??NaN}else{if(e===null)throw new TypeError("neither 'x' nor 'covmat' supplied");o=no(e),s=e.length}let i=o.length;if(i<3)throw new RangeError("factor analysis requires at least three variables");let c=t;if(!Number.isInteger(c)||c<1)throw new RangeError("'factors' must be a positive integer");let a=((i-c)**2-i-c)/2;if(a<0)throw new RangeError(`${c} ${c===1?"factor is":"factors are"} too many for ${i} variables`);let l=o.map((x,E)=>Math.sqrt(x[E])),f=o.map((x,E)=>x.map((T,R)=>T/(l[E]*l[R]))),m=n.control?.lower??.005,u=n.control?.start?.slice()??Ke(f).map((x,E)=>(1-c/(2*i))/x[E]),h=x=>{let E=dt(tf(f,x),{symmetric:!0,onlyValues:!0}).values.slice(c);return-(xe(E.map(T=>Math.log(T)-T))-c+i)},p=x=>{let E=Jl(f,x,c);return x.map((T,R)=>{let S=0;for(let k=0;k<c;k++)S+=E[R][k]*E[R][k];return(S+T-f[R][R])/(T*T)})},g=we(()=>lr(u,h,{gr:p,method:"L-BFGS-B",lower:m,upper:1,control:{fnscale:1,parscale:new Array(i).fill(.01)}}));if(g.convergence!==0)throw new Error("unable to optimize from this starting value");let w=g.par,b=ef(Jl(f,w,c)).L,N,v=n.rotation??"varimax";if(v!=="varimax"&&v!=="promax"&&v!=="none")throw new RangeError(`unknown rotation '${String(v)}'`);if(v!=="none"&&c>=2){let x=v==="varimax"?Ko(b):v0(b),E=ef(x.loadings,x.rotmat);b=E.L,N=E.rot}let y=NaN,d=NaN;!Number.isNaN(s)&&a>0&&(y=(s-1-(2*i+5)/6-2*c/3)*g.value,d=_t(y,a,{lowerTail:!1}));let M={uniquenesses:w,loadings:b,STATISTIC:y,dof:a,PVAL:d,converged:!0,objective:g.value,counts:g.counts,warnings:[]};return r!=="none"&&(M.scores=M0(e,f,w,b,r)),N!==void 0&&(M.rotmat=N),M}function M0(e,t,n,r,o){let s=e.length,i=e[0].map((m,u)=>xe(e.map(h=>h[u]))/s),c=e.map(m=>m.map((u,h)=>u-i[h])),a=ya(c,m=>m*m).map(m=>Math.sqrt(m/(s-1))),l=c.map(m=>m.map((u,h)=>u/a[h]));if(o==="regression")return St(l,Ke(t,r));let f=r.map((m,u)=>m.map(h=>h/n[u]));return St(l,Kn(Ke(St(Kn(r),f),Kn(f))))}var y0=["nfactors","covmat"],Wn=2**-52,E0=`
14
14
  Three factors are required for identification -- general factor loadings set to be equal.
15
15
  Proceed with caution.
16
16
  Think about redoing the analysis with alternative values of the 'option' setting.
17
17
 
18
- `,On=(e,t)=>e.map(n=>t[0].map((r,o)=>{let s=0;for(let c=0;c<n.length;c++)s+=n[c]*t[c][o];return s})),pr=e=>e[0].map((t,n)=>e.map(r=>r[n])),hr=(e,t=n=>n)=>e[0].map((n,r)=>ye(e.map(o=>t(o[r])))),zc=(e,t=n=>n)=>e.map(n=>ye(n.map(t))),Fn=(e,t=n=>n)=>ye(e[0].flatMap((n,r)=>e.map(o=>t(o[r])))),No=e=>e<0?-1:1;function Ds(e){let t=e.map((n,r)=>Math.sqrt(n[r]));return e.map((n,r)=>n.map((o,s)=>r===s?1:o/(t[r]*t[s])))}function $c(e,t){let n=Ds(e),{d:r,u:o,v:s}=Or(n),c=r.map((a,l)=>l).filter(a=>r[a]>Math.sqrt(Ln)*r[0]),i=n.map((a,l)=>{let f=0;for(let m of c)f+=s[l][m]*(1/r[m])*o[l][m];return 1-1/f});return i.some(a=>a>1)&&jt(t,"In smc, smcs > 1 were set to 1.0"),i.some(a=>a<0)&&jt(t,"In smc, smcs < 0 were set to .0"),i.map(a=>a>1?1:a<0?0:a)}var jt=(e,t)=>{e.includes(t)||e.push(t)};function Fs(e,t,n,r){let o=e.map((i,a)=>i.map((l,f)=>a===f?t[a]:l)),s=ct(o,{symmetric:!0}),c=s.values.slice(0,n).map(i=>Math.sqrt(r(i)));return s.vectors.map(i=>i.slice(0,n).map((a,l)=>a*c[l]))}var nh=e=>On(e,pr(e));function Vc(e,t,n,r){let o=Ds(e),s=o.length,c=$c(o,r),i=w=>{let y=Fs(o,w.map(b=>1-b),t,b=>b<Ln?100*Ln:b),v=nh(y),N=[];for(let b=0;b<s;b++)for(let h=b+1;h<s;h++)N.push((o[h][b]-v[h][b])**2);return ye(N)},a=w=>{let y=Fs(o,w.map((v,N)=>o[N][N]-v),t,v=>Math.max(v,0));return w.map((v,N)=>{let b=0;for(let h=0;h<t;h++)b+=y[N][h]*y[N][h];return b+v-o[N][N]})},l=w=>{let y=ct(o.map((T,S)=>T.map((A,k)=>S===k?1-w[S]:A)),{symmetric:!0}),v=y.values,N=y.vectors,b=v.map(T=>T>=Ln),h=v.map(T=>T<Ln?100*Ln:T),E=o.map((T,S)=>T.map((A,k)=>{if(S===k)return 0;let q=0;for(let I=0;I<t;I++)q+=h[I]*N[S][I]*N[k][I];return A-q})),x=On(E,N),M=(T,S)=>{let A=0;for(let k=0;k<s;k++)A+=N[k][T]*x[k][S];return A},R=new Array(s).fill(0);for(let T=0;T<t;T++){let S=(A,k)=>{if(k===0)return;let q=k*M(T,A);for(let I=0;I<s;I++)R[I]+=q*N[I][T]*N[I][A]};b[T]&&S(T,1);for(let A=T+1;A<t;A++)S(A,2*(b[T]&&b[A]?1:(h[T]-h[A])/(v[T]-v[A])));for(let A=t;A<s;A++)S(A,2*h[T]/(v[T]-v[A]))}return R},f=.005,m={method:"L-BFGS-B",lower:f,upper:Math.max(ge(c),1)},u=new Array(s).fill(.01),p=Re(()=>Zn(c.map(w=>1-w),i,{gr:a,...m,control:{fnscale:1,parscale:u}}));p.par.some(w=>w===f)&&(p=Re(()=>Zn(p.par,i,{gr:l,...m,control:{fnscale:1,parscale:u,factr:1e3}}))),p.convergence!==0&&jt(n,`minres did not converge (optim convergence code ${p.convergence}): examine the results carefully`);let d=p.par,g=Fs(o,d.map((w,y)=>o[y][y]-w),t,w=>Math.max(w,0));if(g=Ps(g,t),g=g.map(w=>w.map(y=>y===0?1e-15:y)),zc(g,w=>w*w).some(w=>w>1)&&jt(n,"An ultra-Heywood case was detected. Examine the results carefully"),t>1){g=Ps(bo(g).loadings,t);let w=hr(g,v=>v*v),y=w.map((v,N)=>N).sort((v,N)=>w[N]-w[v]||v-N);g=g.map(v=>y.map(N=>v[N]))}else g=Ps(g,t);return g}function Ps(e,t){let n=hr(e).map(No);return t===1?n[0]<0?e.map(r=>r.map(o=>-o)):e:e.map(r=>r.map((o,s)=>n[s]*o))}function rh(e){let t=e[0].length,n=e.map(o=>o.map(s=>s*s)),r=n.map(o=>o.map((s,c)=>{let i=0;for(let a=0;a<t;a++)a!==c&&(i+=o[a]);return i}));return{f:Fn(n.map((o,s)=>o.map((c,i)=>c*r[s][i])))/4,Gq:e.map((o,s)=>o.map((c,i)=>c*r[s][i]))}}function oh(e){let t=e[0].length,n=l=>{let f=Ve(l),m=On(e,pr(f)),{f:u,Gq:p}=rh(m),d=pr(On(On(pr(m),p),f)).map(g=>g.map(w=>-w));return{L:m,f:u,G:d}},r=Array.from({length:t},(l,f)=>Array.from({length:t},(m,u)=>f===u?1:0)),o=1,s=n(r),c,i=[],a=!1;for(let l=0;l<=2e3;l++){let f=hr(r.map((w,y)=>w.map((v,N)=>v*s.G[y][N]))),m=s.G.map((w,y)=>w.map((v,N)=>v-r[y][N]*f[N])),u=Math.sqrt(Fn(m,w=>w*w));if(i.push(s.f),u<1e-5){a=!0;break}if(c!==void 0){let w=c,y=r.map((N,b)=>N.map((h,E)=>h-w.T[b][E])),v=m.map((N,b)=>N.map((h,E)=>h-w.Gp[b][E]));Fn(v,N=>N*N)>0&&(o=Fn(y,N=>N*N)/Math.abs(Fn(y.map((N,b)=>N.map((h,E)=>h*v[b][E])))),o=Math.max(1e-10,Math.min(o,20)))}else o=2*o;let p=ge(i.slice(-10)),d=s,g=r;for(let w=0;w<=10;w++){let y=r.map((N,b)=>N.map((h,E)=>h-o*m[b][E])),v=hr(y,N=>N*N).map(N=>1/Math.sqrt(N));if(g=y.map(N=>N.map((b,h)=>b*v[h])),d=n(g),p-d.f>.5*u*u*o)break;o=o/2}c={T:r,Gp:m},r=g,s=d}return{loadings:s.L,phi:On(pr(r),r),converged:a}}function sh(e,t={}){C(t,eh,"omega");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)wo(t.covmat),r=Ds(t.covmat);else{if(e===null)throw new TypeError("neither 'x' nor 'covmat' supplied");r=Qn(e,void 0,{use:"pairwise.complete.obs"})}let o=r.length;if(o<2)throw new RangeError(`omega needs at least 2 variables (got ${o})`);if(n>o)throw new RangeError(`'nfactors' (${n}) must not exceed the number of variables (${o})`);let s=[],c=[],i=Vc(r,n,s,c),a,l,f;if(n===1)a=i.map(L=>L.slice()),f=[1],jt(c,`Omega_h for 1 factor is not meaningful, just omega_t
19
- `),jt(s,"Omega_h and Omega_asymptotic are not meaningful with one factor");else{let L=oh(i);if(L.converged||jt(s,"oblimin did not converge in 2000 iterations"),a=L.loadings,l=L.phi,n===2){let $=l[0][1],X=Math.sqrt(Math.abs($));f=[X,No($)*X],jt(c,th)}else f=Vc(l,1,s,c).map($=>$[0])}let m=a.map(L=>{let $=0;for(let X=0;X<n;X++)$+=L[X]*f[X];return $}),u=zc(i,L=>L*L),p=u.map((L,$)=>r[$][$]-L),d=a.map((L,$)=>L.map((X,te)=>{if(n===1)return 0;let ne=r[$][$],ae=ne-(ne-X*X)-(X*f[te])**2;return ae<0&&(ae=0),No(X)*Math.sqrt(ae)})),g=m.map((L,$)=>L*L/u[$]),w=m.map((L,$)=>{let X=[L,...d[$]];return ye(X.map(te=>te*te))**2/ye(X.map(te=>te**4))}),y=m.map(No);y.some(L=>L<0)&&(r=r.map((L,$)=>L.map((X,te)=>y[$]*X*y[te])));let v=m.map((L,$)=>y[$]*L),N=d.map((L,$)=>L.map(X=>y[$]*X)),b=v.map((L,$)=>[L,...N[$],u[$],p[$],g[$],w[$]]),h=Fn(r),E=ye(r.map((L,$)=>L[$])),x=ye(v)**2,M=ye(p),R=x/h,T=(h-M)/h,S=x/(h-M),A=(h-E)/h*o/(o-1),k=(h+ye($c(r,c))-E)/h,q=hr(b.map(L=>L.slice(0,n+1)),L=>L*L),I=q[0]/ye(q),z=N.map(L=>{let $=0;for(let X=1;X<n;X++)Math.abs(L[X])>Math.abs(L[$])&&($=X);return $}),P=[],V=[];for(let L=0;L<n;L++){let $=z.map((ae,Me)=>Me).filter(ae=>z[ae]===L),X=ye($.flatMap(ae=>$.map(Me=>r[Me][ae]))),te=ye($.map(ae=>N[ae][L])),ne=ye($.map(ae=>v[ae]));V.push(te*te),P.push([(te*te+ne*ne)/X,ne*ne/X,te*te/X])}let W=[[T,ye(v)**2/h,ye(V)/h],...P],F={omegaH:R,omegaLim:S,alpha:A,omegaTot:T,G6:k,ECV:I,sl:b,slNames:["g",...Array.from({length:n},(L,$)=>`F${$+1}*`),"h2","u2","p2","com"],orthog:i,oblique:a,gloading:f,key:y,omegaGroup:W,warnings:s,messages:c};return l!==void 0&&(F.phi=l),j("omega",F)}var Wc=["ML","stdErr"],ah=["ML","stdErr"],Qe=.9999;function Vs(e){let t=se(e),n=[],r=0;for(let o=0;o<e.length-1;o++)r+=e[o],n.push(Z(r/t));return n}var zs=e=>[...new Set(e)].sort((t,n)=>t-n),jc=(e,t)=>e===1?`1 ${t} with zero marginal removed`:`${e} ${t}s with zero marginals removed`,Kc=e=>Array.isArray(e)||ArrayBuffer.isView(e),Uc=(e,t)=>e>0?Y(e,{lowerTail:!1})-Y(t,{lowerTail:!1}):Y(t)-Y(e);function $s(e){let t=ee([e]).groups[0],n=ke(t),r=t.map(s=>s-n),o=Math.sqrt(qe(r));return r.map(s=>s/o)}function Yc(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 vo=e=>Array.from({length:e},()=>new Array(e).fill(0));function Xc(e,t,n,r,o){e.value+=t*Math.log(n);let s=r.length;for(let c=0;c<s;c++){e.gradient[c]+=t*r[c]/n;for(let i=0;i<s;i++)e.hessian[c][i]+=t*(o[c][i]/n-r[c]*r[i]/(n*n))}}var Cs={r:0,h:0,k:0,rr:0,rh:0,rk:0,hh:0,hk:0,kk:0};function ih(e,t,n){if(e===-1/0||t===-1/0||e===1/0&&t===1/0)return Cs;if(e===1/0)return{...Cs,k:at(t),kk:-t*at(t)};if(t===1/0)return{...Cs,h:at(e),hh:-e*at(e)};let r=(1-n)*(1+n),o=Math.sqrt(r),s=e*e-2*n*e*t+t*t,c=Math.exp(-s/(2*r))/(2*Math.PI*o),i=(t-n*e)/o,a=(e-n*t)/o,l=at(e),f=at(t);return{r:c,h:l*Y(i),k:f*Y(a),rr:c*(n/r+(e*t*r-n*s)/(r*r)),rh:-c*(e-n*t)/r,rk:-c*(t-n*e)/r,hh:-e*l*Y(i)-n/o*l*at(i),hk:c,kk:-t*f*Y(a)-n/o*f*at(a)}}function Gc(e,t,n,r,o){let s=e.length,c=e[0].length,i=o?s+c-1:1,a={value:0,gradient:new Array(i).fill(0),hessian:vo(i)},l=p=>o&&p>=1&&p<=s-1?p:-1,f=p=>o&&p>=1&&p<=c-1?s-1+p:-1,m=t.map(p=>n.map(d=>ih(p,d,r))),u=[];for(let p=0;p<s;p++)for(let d=0;d<c;d++){let g=e[p][d];if(g===0)continue;let w=Yn([t[p],n[d]],[t[p+1],n[d+1]],r),y=new Array(i).fill(0),v=vo(i);for(let[N,b,h]of[[p+1,d+1,1],[p,d+1,-1],[p+1,d,-1],[p,d,1]]){let E=m[N][b],x=l(N),M=f(b);y[0]+=h*E.r,v[0][0]+=h*E.rr,x>=0&&(y[x]+=h*E.h,v[0][x]+=h*E.rh,v[x][0]+=h*E.rh,v[x][x]+=h*E.hh),M>=0&&(y[M]+=h*E.k,v[0][M]+=h*E.rk,v[M][0]+=h*E.rk,v[M][M]+=h*E.kk),x>=0&&M>=0&&(v[x][M]+=h*E.hk,v[M][x]+=h*E.hk)}Xc(a,g,w,y,v),u.push(g*Math.log(w))}return a.value=se(u),a}function ch(e,t,n,r,o){let s=o?n.length+1:1,c={value:0,gradient:new Array(s).fill(0),hessian:vo(s)},i=(1-r)*(1+r),a=Math.sqrt(i),l=i*a,f=new Array(s),m=vo(s),u=[],p=(d,g,w)=>{let y=n[d],v=(y-r*g)/a,N=at(v),b=(r*y-g)/l,h=1/a,E=y/l+3*r*(r*y-g)/(l*i),x=r/l;if(f[0]+=w*N*b,m[0][0]+=w*N*(E-v*b*b),o){let M=d+1;f[M]+=w*N*h;let R=w*N*(x-v*b*h);m[0][M]+=R,m[M][0]+=R,m[M][M]+=w*N*(-v*h*h)}};for(let d=0;d<e.length;d++){let g=e[d],w=t[d];f.fill(0);for(let N of m)N.fill(0);let y=w<n.length?(n[w]-r*g)/a:1/0,v=w>0?(n[w-1]-r*g)/a:-1/0;w<n.length&&p(w,g,1),w>0&&p(w-1,g,-1),Xc(c,1,Uc(v,y),f,m),u.push(Math.log(Uc(v,y)),at(g,{log:!0}))}return c.value=se(u),c}function lh(e,t){let n=new Array(e.gradient.length).fill(0),r=t.map(c=>e.gradient[c]),o=t.map(c=>t.map(i=>-e.hessian[c][i])),s;try{s=Ve(o,r)}catch{s=void 0}return(s===void 0||!s.every(Number.isFinite)||se(s.map((c,i)=>c*r[i]))<=0)&&(s=r.map((c,i)=>c/Math.max(Math.abs(o[i][i]),1))),t.forEach((c,i)=>n[c]=s[i]),n}var Bc=(e,t)=>e>=t-1e-12*Math.abs(t);function Hc(e,t,n){let r=[...t],o=e(r),s=n,c=r.map((i,a)=>a);for(let i=0;i<200;i++){let a=s?c.slice(1):c;if(a.length===0)break;let l=lh(o,a);if(!s&&Math.abs(r[0])===Qe&&l[0]*r[0]>0){s=!0;continue}let f=Math.abs(r[0]+l[0])>Qe?(Math.sign(l[0])*Qe-r[0])/l[0]:1,m=!1;for(let u=0;u<60;u++){let p=r.map((g,w)=>g+f*l[w]);f*l[0]!==0&&Math.abs(p[0])>=Qe*(1-1e-15)&&(p[0]=Math.sign(p[0])*Qe);let d=e(p);if(Bc(d.value,o.value)){let g=ge(p.map((w,y)=>Math.abs(w-r[y])/Math.max(1,Math.abs(r[y]))));r=p,o=d,m=g>1e-12;break}f/=2}if(!m)break}return{theta:r,at:o,bounded:s}}function Qc(e,t){let n=Hc(e,[Math.max(-Qe,Math.min(Qe,t[0])),...t.slice(1)],!1);if(n.bounded||n.theta[0]===0)return n;let r=Hc(e,[Math.sign(n.theta[0])*Qe,...n.theta.slice(1)],!0);return Bc(r.at.value,n.at.value)?r:n}function Zc(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(s=>-s)))}catch{return n()}return r.map((o,s)=>o.map((c,i)=>(c+r[i][s])/2))}var Jc=e=>`inadmissible correlation set to ${e>0?"0.9999":"-0.9999"}`;function fh(e,t,n){Ce(t)&&C(t,Wc,"polychor"),C(n,Wc,"polychor");let r,o;if(Kc(t)){let P=e,V=t;if(P.length!==V.length)throw new RangeError("'x' and 'y' must have the same length");let W=P.map((te,ne)=>!Number.isNaN(te)&&!Number.isNaN(V[ne])),F=Fe(P.filter((te,ne)=>W[ne])),L=Fe(V.filter((te,ne)=>W[ne])),$=new Map(zs(F).map((te,ne)=>[te,ne])),X=new Map(zs(L).map((te,ne)=>[te,ne]));r=Array.from({length:$.size},()=>new Array(X.size).fill(0)),F.forEach((te,ne)=>r[$.get(te)][X.get(L[ne])]++),o=n??{}}else{let P=e;if(!P.every(Kc))throw new TypeError("give a table (array of rows) or two vectors");let V=P[0]?.length??0;if(P.some(W=>W.length!==V))throw new TypeError("the table must have rows of equal length");if(r=P.map(W=>Array.from(W)),r.some(W=>W.some(F=>!(F>=0&&F<1/0))))throw new RangeError("the counts of the table must be nonnegative and finite");o=t??{}}let{ML:s=!1,stdErr:c=!1}=o,i=[],a=0;se(r.map(P=>se(P)))<2**1e3||({groups:r,k:a}=ee(r));let l=r.map(P=>se(P)),f=l.filter(P=>P===0).length;f>0&&(i.push(jc(f,"row")),r=r.filter((P,V)=>l[V]!==0));let m=(r[0]??[]).map((P,V)=>se(r.map(W=>W[V]))),u=m.filter(P=>P===0).length;u>0&&(i.push(jc(u,"column")),r=r.map(P=>P.filter((V,W)=>m[W]!==0)));let p=r.length,d=r[0]?.length??0;if(p<2&&i.push("the table has fewer than 2 rows"),d<2&&i.push("the table has fewer than 2 columns"),p<2||d<2)return j("polychor",{rho:NaN,warnings:i});let g=[-1/0,...Vs(r.map(P=>se(P))),1/0],w=[-1/0,...Vs(r[0].map((P,V)=>se(r.map(W=>W[V])))),1/0],v=ga(P=>{let V=Math.max(-Qe,Math.min(Qe,P)),W=[];for(let F=0;F<p;F++)for(let L=0;L<d;L++){if(r[F][L]===0)continue;let $=Yn([g[F],w[L]],[g[F+1],w[L+1]],V);W.push(r[F][L]*Math.log($))}return-se(W)},-Qe,Qe).optimum;if(!s&&!c)return j("polychor",{rho:v,warnings:i});let N=P=>[-1/0,...P,1/0],b=g.slice(1,p),h=w.slice(1,d),E=P=>s?Gc(r,N(P.slice(1,p)),N(P.slice(p)),P[0],!0):Gc(r,g,w,P[0],!1),{theta:x,at:M,bounded:R}=Qc(E,s?[v,...b,...h]:[v]),T=x[0];if(R&&i.push(Jc(T)),!c)return j("polychor",{rho:T,warnings:i});let S=s?N(x.slice(1,p)):g,A=s?N(x.slice(p)):w,k=se(r.map(P=>se(P))),q=[],I=[];for(let P=0;P<p;P++)for(let V=0;V<d;V++){let W=r[P][V],F=Yn([S[P],A[V]],[S[P+1],A[V+1]],T);if(W===0)q.push(k*F);else if(a===0)q.push(W*Math.log1p(1e-6/W),W*Yc(k*F/W-1));else{let L=_(W,a);I.push(L*Math.log1p(1e-6/L)),q.push(W*Yc(k*F/W-1))}}let z=Zc(M.hessian,R);return j("polychor",{rho:T,warnings:i,var:a===0?z:z.map(P=>P.map(V=>_(V,-a))),chisq:a===0?2*se(q):_(2*se(q),a)+2*se(I),df:p*d-p-d,n:_(k,a),rowCuts:S.slice(1,p),colCuts:A.slice(1,d)})}function mh(e,t,n,r){let o=e.length,s=[Z(.25),Z(.5),Z(.75)],c=(p,d)=>d.filter(g=>g<p).length,i=$s(e).map(p=>c(p,n)),a=$s(t).map(p=>c(p,s)),l=Array.from({length:n.length+1},()=>new Array(4).fill(0));i.forEach((p,d)=>l[p][a[d]]++);let f=[-1/0,...n,1/0],m=[-1/0,...s,1/0],u=[];return l.forEach((p,d)=>p.forEach((g,w)=>{if(g===0)return;let y=Yn([f[d],m[w]],[f[d+1],m[w+1]],r);u.push(g*Math.log((g+1e-6)/(y*o)))})),2*se(u)}function uh(e,t,n={}){if(C(n,ah,"polyserial"),e.length!==t.length)throw new RangeError("'x' and 'y' must have the same length");let{ML:r=!1,stdErr:o=!1}=n,s=e.map((M,R)=>!Number.isNaN(M)&&!Number.isNaN(t[R])),c=e.filter((M,R)=>s[R]),i=Fe(t.filter((M,R)=>s[R])),a=c.length,l=zs(i);if(l.length<2)return j("polyserial",{rho:NaN,warnings:["y has fewer than 2 levels"]});let f=new Map(l.map((M,R)=>[M,R+1])),m=i.map(M=>f.get(M)),u=l.map(()=>0);for(let M of m)u[M-1]++;let p=Vs(u),d=[],g=Math.sqrt((a-1)/a)*Math.sqrt(qe(m))*ba(c,m)[0]/se(p.map(M=>at(M)));if(Math.abs(g)>Qe){let M=Math.sign(g)*Qe;d.push(`initial correlation inadmissible, ${wn(g)}, set to ${wn(M)}`),g=M}if(!r&&!o)return j("polyserial",{rho:g,warnings:d});let w=$s(c),y=m.map(M=>M-1),v=M=>ch(w,y,r?M.slice(1):p,M[0],r),{theta:N,at:b,bounded:h}=Qc(v,r?[g,...p]:[g]);if(h&&d.push(Jc(N[0])),!o)return j("polyserial",{rho:N[0],warnings:d});let E=r?N.slice(1):p,x=l.length;return j("polyserial",{rho:N[0],warnings:d,var:Zc(b.hessian,h),chisq:mh(m,w,E,r?N[0]:g),df:4*x-x-1,n:a,cuts:E})}export{ws as Anova,Mm as TukeyHSD,Op as adTest,ss as anova,Si as ansariTest,vm as aov,ip as aovEz,as as aovSummary,Ja as bartlettTest,Fl as binomTest,Hf as brunnerMunzelTest,ol as chisqTest,Hl as cochranQTest,dc as cohenES,Hu as cohensD,wm as confint,nl as corTest,Np as coxph,Up as cronbachAlpha,Fp as cvmTest,Nf as dscfAllPairsTest,Mf as dunnTest,wf as dunnettTest,rp as epsilonSquared,Eu as esH,xu as esW1,Ru as esW2,tp as etaSquared,Zp as factanal,hl as fisherTest,ni as flignerTest,$o as friedmanTest,vf as gamesHowellTest,xf as glhtDunnett,Om as glm,Qm as glmAnova,Gm as glmConfint,jm as glmCooksDistance,hs as glmHatvalues,Um as glmPredict,zm as glmResiduals,Wm as glmRstandard,us as glmSummary,Xu as hedgesG,Kp as icc,Ip as jonckheereTest,Wp as kappa2,jp as kappamFleiss,Yr as kruskalTest,Fi as ksTest,df as kwAllPairsDunnTest,gf as kwManyOneDunnTest,Mp as leveneTest,Pp as lillieTest,ls as lm,_f as mantelhaenTest,bf as manyOneUTest,ru as mauchlyTest,Kl as mcnemarTest,Mn as modelMatrix,ri as moodTest,sh as omega,np as omegaSquared,di as onewayTest,Rn as pAdjust,om as pairwisePropTest,nm as pairwiseTTest,rm as pairwiseWilcoxTest,Gp as partialR,Cp as pearsonTest,$l as poissonTest,fh as polychor,uh as polyserial,fu as powerAnovaTest,cu as powerPropTest,iu as powerTTest,bm as predict,Do as propTest,Rp as propTrendTest,Fu as pwr2p2nTest,Iu as pwr2pTest,Cu as pwrAnovaTest,Du as pwrChisqTest,Vu as pwrF2Test,Ou as pwrNormTest,Lu as pwrPTest,Pu as pwrRTest,_u as pwrT2nTest,qu as pwrTTest,ti as quadeTest,tl as setWarningHandler,Dp as sfTest,Tf as shapiroTest,mp as survdiff,pp as survfit,xn as tTest,yf as tukeyTest,Za as varTest,Jn as wilcoxTest,Em as zTest};
18
+ `,jn=(e,t)=>e.map(n=>t[0].map((r,o)=>{let s=0;for(let i=0;i<n.length;i++)s+=n[i]*t[i][o];return s})),Ir=e=>e[0].map((t,n)=>e.map(r=>r[n])),_r=(e,t=n=>n)=>e[0].map((n,r)=>xe(e.map(o=>t(o[r])))),rf=(e,t=n=>n)=>e.map(n=>xe(n.map(t))),Un=(e,t=n=>n)=>xe(e[0].flatMap((n,r)=>e.map(o=>t(o[r])))),Wo=e=>e<0?-1:1;function Ta(e){let t=e.map((n,r)=>Math.sqrt(n[r]));return e.map((n,r)=>n.map((o,s)=>r===s?1:o/(t[r]*t[s])))}function of(e,t){let n=Ta(e),{d:r,u:o,v:s}=Zr(n),i=r.map((a,l)=>l).filter(a=>r[a]>Math.sqrt(Wn)*r[0]),c=n.map((a,l)=>{let f=0;for(let m of i)f+=s[l][m]*(1/r[m])*o[l][m];return 1-1/f});return c.some(a=>a>1)&&rn(t,"In smc, smcs > 1 were set to 1.0"),c.some(a=>a<0)&&rn(t,"In smc, smcs < 0 were set to .0"),c.map(a=>a>1?1:a<0?0:a)}var rn=(e,t)=>{e.includes(t)||e.push(t)};function Ea(e,t,n,r){let o=e.map((c,a)=>c.map((l,f)=>a===f?t[a]:l)),s=dt(o,{symmetric:!0}),i=s.values.slice(0,n).map(c=>Math.sqrt(r(c)));return s.vectors.map(c=>c.slice(0,n).map((a,l)=>a*i[l]))}var x0=e=>jn(e,Ir(e));function nf(e,t,n,r){let o=Ta(e),s=o.length,i=of(o,r),c=w=>{let b=Ea(o,w.map(y=>1-y),t,y=>y<Wn?100*Wn:y),N=x0(b),v=[];for(let y=0;y<s;y++)for(let d=y+1;d<s;d++)v.push((o[d][y]-N[d][y])**2);return xe(v)},a=w=>{let b=Ea(o,w.map((N,v)=>o[v][v]-N),t,N=>Math.max(N,0));return w.map((N,v)=>{let y=0;for(let d=0;d<t;d++)y+=b[v][d]*b[v][d];return y+N-o[v][v]})},l=w=>{let b=dt(o.map((R,S)=>R.map((k,A)=>S===A?1-w[S]:k)),{symmetric:!0}),N=b.values,v=b.vectors,y=N.map(R=>R>=Wn),d=N.map(R=>R<Wn?100*Wn:R),M=o.map((R,S)=>R.map((k,A)=>{if(S===A)return 0;let q=0;for(let I=0;I<t;I++)q+=d[I]*v[S][I]*v[A][I];return k-q})),x=jn(M,v),E=(R,S)=>{let k=0;for(let A=0;A<s;A++)k+=v[A][R]*x[A][S];return k},T=new Array(s).fill(0);for(let R=0;R<t;R++){let S=(k,A)=>{if(A===0)return;let q=A*E(R,k);for(let I=0;I<s;I++)T[I]+=q*v[I][R]*v[I][k]};y[R]&&S(R,1);for(let k=R+1;k<t;k++)S(k,2*(y[R]&&y[k]?1:(d[R]-d[k])/(N[R]-N[k])));for(let k=t;k<s;k++)S(k,2*d[R]/(N[R]-N[k]))}return T},f=.005,m={method:"L-BFGS-B",lower:f,upper:Math.max(ve(i),1)},u=new Array(s).fill(.01),h=we(()=>lr(i.map(w=>1-w),c,{gr:a,...m,control:{fnscale:1,parscale:u}}));h.par.some(w=>w===f)&&(h=we(()=>lr(h.par,c,{gr:l,...m,control:{fnscale:1,parscale:u,factr:1e3}}))),h.convergence!==0&&rn(n,`minres did not converge (optim convergence code ${h.convergence}): examine the results carefully`);let p=h.par,g=Ea(o,p.map((w,b)=>o[b][b]-w),t,w=>Math.max(w,0));if(g=xa(g,t),g=g.map(w=>w.map(b=>b===0?1e-15:b)),rf(g,w=>w*w).some(w=>w>1)&&rn(n,"An ultra-Heywood case was detected. Examine the results carefully"),t>1){g=xa(Ko(g).loadings,t);let w=_r(g,N=>N*N),b=w.map((N,v)=>v).sort((N,v)=>w[v]-w[N]||N-v);g=g.map(N=>b.map(v=>N[v]))}else g=xa(g,t);return g}function xa(e,t){let n=_r(e).map(Wo);return t===1?n[0]<0?e.map(r=>r.map(o=>-o)):e:e.map(r=>r.map((o,s)=>n[s]*o))}function T0(e){let t=e[0].length,n=e.map(o=>o.map(s=>s*s)),r=n.map(o=>o.map((s,i)=>{let c=0;for(let a=0;a<t;a++)a!==i&&(c+=o[a]);return c}));return{f:Un(n.map((o,s)=>o.map((i,c)=>i*r[s][c])))/4,Gq:e.map((o,s)=>o.map((i,c)=>i*r[s][c]))}}function R0(e){let t=e[0].length,n=l=>{let f=Ke(l),m=jn(e,Ir(f)),{f:u,Gq:h}=T0(m),p=Ir(jn(jn(Ir(m),h),f)).map(g=>g.map(w=>-w));return{L:m,f:u,G:p}},r=Array.from({length:t},(l,f)=>Array.from({length:t},(m,u)=>f===u?1:0)),o=1,s=n(r),i,c=[],a=!1;for(let l=0;l<=2e3;l++){let f=_r(r.map((w,b)=>w.map((N,v)=>N*s.G[b][v]))),m=s.G.map((w,b)=>w.map((N,v)=>N-r[b][v]*f[v])),u=Math.sqrt(Un(m,w=>w*w));if(c.push(s.f),u<1e-5){a=!0;break}if(i!==void 0){let w=i,b=r.map((v,y)=>v.map((d,M)=>d-w.T[y][M])),N=m.map((v,y)=>v.map((d,M)=>d-w.Gp[y][M]));Un(N,v=>v*v)>0&&(o=Un(b,v=>v*v)/Math.abs(Un(b.map((v,y)=>v.map((d,M)=>d*N[y][M])))),o=Math.max(1e-10,Math.min(o,20)))}else o=2*o;let h=ve(c.slice(-10)),p=s,g=r;for(let w=0;w<=10;w++){let b=r.map((v,y)=>v.map((d,M)=>d-o*m[y][M])),N=_r(b,v=>v*v).map(v=>1/Math.sqrt(v));if(g=b.map(v=>v.map((y,d)=>y*N[d])),p=n(g),h-p.f>.5*u*u*o)break;o=o/2}i={T:r,Gp:m},r=g,s=p}return{loadings:s.L,phi:jn(Ir(r),r),converged:a}}function S0(e,t={}){C(t,y0,"omega");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)zo(t.covmat),r=Ta(t.covmat);else{if(e===null)throw new TypeError("neither 'x' nor 'covmat' supplied");r=cr(e,void 0,{use:"pairwise.complete.obs"})}let o=r.length;if(o<2)throw new RangeError(`omega needs at least 2 variables (got ${o})`);if(n>o)throw new RangeError(`'nfactors' (${n}) must not exceed the number of variables (${o})`);let s=[],i=[],c=nf(r,n,s,i),a,l,f;if(n===1)a=c.map(F=>F.slice()),f=[1],rn(i,`Omega_h for 1 factor is not meaningful, just omega_t
19
+ `),rn(s,"Omega_h and Omega_asymptotic are not meaningful with one factor");else{let F=R0(c);if(F.converged||rn(s,"oblimin did not converge in 2000 iterations"),a=F.loadings,l=F.phi,n===2){let K=l[0][1],X=Math.sqrt(Math.abs(K));f=[X,Wo(K)*X],rn(i,E0)}else f=nf(l,1,s,i).map(K=>K[0])}let m=a.map(F=>{let K=0;for(let X=0;X<n;X++)K+=F[X]*f[X];return K}),u=rf(c,F=>F*F),h=u.map((F,K)=>r[K][K]-F),p=a.map((F,K)=>F.map((X,te)=>{if(n===1)return 0;let ne=r[K][K],fe=ne-(ne-X*X)-(X*f[te])**2;return fe<0&&(fe=0),Wo(X)*Math.sqrt(fe)})),g=m.map((F,K)=>F*F/u[K]),w=m.map((F,K)=>{let X=[F,...p[K]];return xe(X.map(te=>te*te))**2/xe(X.map(te=>te**4))}),b=m.map(Wo);b.some(F=>F<0)&&(r=r.map((F,K)=>F.map((X,te)=>b[K]*X*b[te])));let N=m.map((F,K)=>b[K]*F),v=p.map((F,K)=>F.map(X=>b[K]*X)),y=N.map((F,K)=>[F,...v[K],u[K],h[K],g[K],w[K]]),d=Un(r),M=xe(r.map((F,K)=>F[K])),x=xe(N)**2,E=xe(h),T=x/d,R=(d-E)/d,S=x/(d-E),k=(d-M)/d*o/(o-1),A=(d+xe(of(r,i))-M)/d,q=_r(y.map(F=>F.slice(0,n+1)),F=>F*F),I=q[0]/xe(q),L=v.map(F=>{let K=0;for(let X=1;X<n;X++)Math.abs(F[X])>Math.abs(F[K])&&(K=X);return K}),O=[],P=[];for(let F=0;F<n;F++){let K=L.map((fe,Te)=>Te).filter(fe=>L[fe]===F),X=xe(K.flatMap(fe=>K.map(Te=>r[Te][fe]))),te=xe(K.map(fe=>v[fe][F])),ne=xe(K.map(fe=>N[fe]));P.push(te*te),O.push([(te*te+ne*ne)/X,ne*ne/X,te*te/X])}let W=[[R,xe(N)**2/d,xe(P)/d],...O],D={omegaH:T,omegaLim:S,alpha:k,omegaTot:R,G6:A,ECV:I,sl:y,slNames:["g",...Array.from({length:n},(F,K)=>`F${K+1}*`),"h2","u2","p2","com"],orthog:c,oblique:a,gloading:f,key:b,omegaGroup:W,warnings:s,messages:i};return l!==void 0&&(D.phi=l),z("omega",D)}var sf=["ML","stdErr"],k0=["ML","stdErr"],rt=.9999;function Sa(e){let t=se(e),n=[],r=0;for(let o=0;o<e.length-1;o++)r+=e[o],n.push(Z(r/t));return n}var ka=e=>[...new Set(e)].sort((t,n)=>t-n),af=(e,t)=>e===1?`1 ${t} with zero marginal removed`:`${e} ${t}s with zero marginals removed`,cf=e=>Array.isArray(e)||ArrayBuffer.isView(e),lf=(e,t)=>e>0?j(e,{lowerTail:!1})-j(t,{lowerTail:!1}):j(t)-j(e);function Aa(e){let t=ee([e]).groups[0],n=qe(t),r=t.map(s=>s-n),o=Math.sqrt(_e(r));return r.map(s=>s/o)}function ff(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 jo=e=>Array.from({length:e},()=>new Array(e).fill(0));function hf(e,t,n,r,o){e.value+=t*Math.log(n);let s=r.length;for(let i=0;i<s;i++){e.gradient[i]+=t*r[i]/n;for(let c=0;c<s;c++)e.hessian[i][c]+=t*(o[i][c]/n-r[i]*r[c]/(n*n))}}var Ra={r:0,h:0,k:0,rr:0,rh:0,rk:0,hh:0,hk:0,kk:0};function A0(e,t,n){if(e===-1/0||t===-1/0||e===1/0&&t===1/0)return Ra;if(e===1/0)return{...Ra,k:Ze(t),kk:-t*Ze(t)};if(t===1/0)return{...Ra,h:Ze(e),hh:-e*Ze(e)};let r=(1-n)*(1+n),o=Math.sqrt(r),s=e*e-2*n*e*t+t*t,i=Math.exp(-s/(2*r))/(2*Math.PI*o),c=(t-n*e)/o,a=(e-n*t)/o,l=Ze(e),f=Ze(t);return{r:i,h:l*j(c),k:f*j(a),rr:i*(n/r+(e*t*r-n*s)/(r*r)),rh:-i*(e-n*t)/r,rk:-i*(t-n*e)/r,hh:-e*l*j(c)-n/o*l*Ze(c),hk:i,kk:-t*f*j(a)-n/o*f*Ze(a)}}function mf(e,t,n,r,o){let s=e.length,i=e[0].length,c=o?s+i-1:1,a={value:0,gradient:new Array(c).fill(0),hessian:jo(c)},l=h=>o&&h>=1&&h<=s-1?h:-1,f=h=>o&&h>=1&&h<=i-1?s-1+h:-1,m=t.map(h=>n.map(p=>A0(h,p,r))),u=[];for(let h=0;h<s;h++)for(let p=0;p<i;p++){let g=e[h][p];if(g===0)continue;let w=rr([t[h],n[p]],[t[h+1],n[p+1]],r),b=new Array(c).fill(0),N=jo(c);for(let[v,y,d]of[[h+1,p+1,1],[h,p+1,-1],[h+1,p,-1],[h,p,1]]){let M=m[v][y],x=l(v),E=f(y);b[0]+=d*M.r,N[0][0]+=d*M.rr,x>=0&&(b[x]+=d*M.h,N[0][x]+=d*M.rh,N[x][0]+=d*M.rh,N[x][x]+=d*M.hh),E>=0&&(b[E]+=d*M.k,N[0][E]+=d*M.rk,N[E][0]+=d*M.rk,N[E][E]+=d*M.kk),x>=0&&E>=0&&(N[x][E]+=d*M.hk,N[E][x]+=d*M.hk)}hf(a,g,w,b,N),u.push(g*Math.log(w))}return a.value=se(u),a}function q0(e,t,n,r,o){let s=o?n.length+1:1,i={value:0,gradient:new Array(s).fill(0),hessian:jo(s)},c=(1-r)*(1+r),a=Math.sqrt(c),l=c*a,f=new Array(s),m=jo(s),u=[],h=(p,g,w)=>{let b=n[p],N=(b-r*g)/a,v=Ze(N),y=(r*b-g)/l,d=1/a,M=b/l+3*r*(r*b-g)/(l*c),x=r/l;if(f[0]+=w*v*y,m[0][0]+=w*v*(M-N*y*y),o){let E=p+1;f[E]+=w*v*d;let T=w*v*(x-N*y*d);m[0][E]+=T,m[E][0]+=T,m[E][E]+=w*v*(-N*d*d)}};for(let p=0;p<e.length;p++){let g=e[p],w=t[p];f.fill(0);for(let v of m)v.fill(0);let b=w<n.length?(n[w]-r*g)/a:1/0,N=w>0?(n[w-1]-r*g)/a:-1/0;w<n.length&&h(w,g,1),w>0&&h(w-1,g,-1),hf(i,1,lf(N,b),f,m),u.push(Math.log(lf(N,b)),Ze(g,{log:!0}))}return i.value=se(u),i}function I0(e,t){let n=new Array(e.gradient.length).fill(0),r=t.map(i=>e.gradient[i]),o=t.map(i=>t.map(c=>-e.hessian[i][c])),s;try{s=Ke(o,r)}catch{s=void 0}return(s===void 0||!s.every(Number.isFinite)||se(s.map((i,c)=>i*r[c]))<=0)&&(s=r.map((i,c)=>i/Math.max(Math.abs(o[c][c]),1))),t.forEach((i,c)=>n[i]=s[c]),n}var pf=(e,t)=>e>=t-1e-12*Math.abs(t);function uf(e,t,n){let r=[...t],o=e(r),s=n,i=r.map((c,a)=>a);for(let c=0;c<200;c++){let a=s?i.slice(1):i;if(a.length===0)break;let l=I0(o,a);if(!s&&Math.abs(r[0])===rt&&l[0]*r[0]>0){s=!0;continue}let f=Math.abs(r[0]+l[0])>rt?(Math.sign(l[0])*rt-r[0])/l[0]:1,m=!1;for(let u=0;u<60;u++){let h=r.map((g,w)=>g+f*l[w]);f*l[0]!==0&&Math.abs(h[0])>=rt*(1-1e-15)&&(h[0]=Math.sign(h[0])*rt);let p=e(h);if(pf(p.value,o.value)){let g=ve(h.map((w,b)=>Math.abs(w-r[b])/Math.max(1,Math.abs(r[b]))));r=h,o=p,m=g>1e-12;break}f/=2}if(!m)break}return{theta:r,at:o,bounded:s}}function df(e,t){let n=uf(e,[Math.max(-rt,Math.min(rt,t[0])),...t.slice(1)],!1);if(n.bounded||n.theta[0]===0)return n;let r=uf(e,[Math.sign(n.theta[0])*rt,...n.theta.slice(1)],!0);return pf(r.at.value,n.at.value)?r:n}function gf(e,t){let n=()=>e.map(o=>o.map(()=>1/0));if(t)return n();let r;try{r=Ke(e.map(o=>o.map(s=>-s)))}catch{return n()}return r.map((o,s)=>o.map((i,c)=>(i+r[c][s])/2))}var wf=e=>`inadmissible correlation set to ${e>0?"0.9999":"-0.9999"}`;function _0(e,t,n){ze(t)&&C(t,sf,"polychor"),C(n,sf,"polychor");let r,o;if(cf(t)){let O=e,P=t;if(O.length!==P.length)throw new RangeError("'x' and 'y' must have the same length");let W=O.map((te,ne)=>!Number.isNaN(te)&&!Number.isNaN(P[ne])),D=Pe(O.filter((te,ne)=>W[ne])),F=Pe(P.filter((te,ne)=>W[ne])),K=new Map(ka(D).map((te,ne)=>[te,ne])),X=new Map(ka(F).map((te,ne)=>[te,ne]));r=Array.from({length:K.size},()=>new Array(X.size).fill(0)),D.forEach((te,ne)=>r[K.get(te)][X.get(F[ne])]++),o=n??{}}else{let O=e;if(!O.every(cf))throw new TypeError("give a table (array of rows) or two vectors");let P=O[0]?.length??0;if(O.some(W=>W.length!==P))throw new TypeError("the table must have rows of equal length");if(r=O.map(W=>Array.from(W)),r.some(W=>W.some(D=>!(D>=0&&D<1/0))))throw new RangeError("the counts of the table must be nonnegative and finite");o=t??{}}let{ML:s=!1,stdErr:i=!1}=o,c=[],a=0;se(r.map(O=>se(O)))<2**1e3||({groups:r,k:a}=ee(r));let l=r.map(O=>se(O)),f=l.filter(O=>O===0).length;f>0&&(c.push(af(f,"row")),r=r.filter((O,P)=>l[P]!==0));let m=(r[0]??[]).map((O,P)=>se(r.map(W=>W[P]))),u=m.filter(O=>O===0).length;u>0&&(c.push(af(u,"column")),r=r.map(O=>O.filter((P,W)=>m[W]!==0)));let h=r.length,p=r[0]?.length??0;if(h<2&&c.push("the table has fewer than 2 rows"),p<2&&c.push("the table has fewer than 2 columns"),h<2||p<2)return z("polychor",{rho:NaN,warnings:c});let g=[-1/0,...Sa(r.map(O=>se(O))),1/0],w=[-1/0,...Sa(r[0].map((O,P)=>se(r.map(W=>W[P])))),1/0],N=oi(O=>{let P=Math.max(-rt,Math.min(rt,O)),W=[];for(let D=0;D<h;D++)for(let F=0;F<p;F++){if(r[D][F]===0)continue;let K=rr([g[D],w[F]],[g[D+1],w[F+1]],P);W.push(r[D][F]*Math.log(K))}return-se(W)},-rt,rt).optimum;if(!s&&!i)return z("polychor",{rho:N,warnings:c});let v=O=>[-1/0,...O,1/0],y=g.slice(1,h),d=w.slice(1,p),M=O=>s?mf(r,v(O.slice(1,h)),v(O.slice(h)),O[0],!0):mf(r,g,w,O[0],!1),{theta:x,at:E,bounded:T}=df(M,s?[N,...y,...d]:[N]),R=x[0];if(T&&c.push(wf(R)),!i)return z("polychor",{rho:R,warnings:c});let S=s?v(x.slice(1,h)):g,k=s?v(x.slice(h)):w,A=se(r.map(O=>se(O))),q=[],I=[];for(let O=0;O<h;O++)for(let P=0;P<p;P++){let W=r[O][P],D=rr([S[O],k[P]],[S[O+1],k[P+1]],R);if(W===0)q.push(A*D);else if(a===0)q.push(W*Math.log1p(1e-6/W),W*ff(A*D/W-1));else{let F=_(W,a);I.push(F*Math.log1p(1e-6/F)),q.push(W*ff(A*D/W-1))}}let L=gf(E.hessian,T);return z("polychor",{rho:R,warnings:c,var:a===0?L:L.map(O=>O.map(P=>_(P,-a))),chisq:a===0?2*se(q):_(2*se(q),a)+2*se(I),df:h*p-h-p,n:_(A,a),rowCuts:S.slice(1,h),colCuts:k.slice(1,p)})}function L0(e,t,n,r){let o=e.length,s=[Z(.25),Z(.5),Z(.75)],i=(h,p)=>p.filter(g=>g<h).length,c=Aa(e).map(h=>i(h,n)),a=Aa(t).map(h=>i(h,s)),l=Array.from({length:n.length+1},()=>new Array(4).fill(0));c.forEach((h,p)=>l[h][a[p]]++);let f=[-1/0,...n,1/0],m=[-1/0,...s,1/0],u=[];return l.forEach((h,p)=>h.forEach((g,w)=>{if(g===0)return;let b=rr([f[p],m[w]],[f[p+1],m[w+1]],r);u.push(g*Math.log((g+1e-6)/(b*o)))})),2*se(u)}function O0(e,t,n={}){if(C(n,k0,"polyserial"),e.length!==t.length)throw new RangeError("'x' and 'y' must have the same length");let{ML:r=!1,stdErr:o=!1}=n,s=e.map((E,T)=>!Number.isNaN(E)&&!Number.isNaN(t[T])),i=e.filter((E,T)=>s[T]),c=Pe(t.filter((E,T)=>s[T])),a=i.length,l=ka(c);if(l.length<2)return z("polyserial",{rho:NaN,warnings:["y has fewer than 2 levels"]});let f=new Map(l.map((E,T)=>[E,T+1])),m=c.map(E=>f.get(E)),u=l.map(()=>0);for(let E of m)u[E-1]++;let h=Sa(u),p=[],g=Math.sqrt((a-1)/a)*Math.sqrt(_e(m))*ai(i,m)[0]/se(h.map(E=>Ze(E)));if(Math.abs(g)>rt){let E=Math.sign(g)*rt;p.push(`initial correlation inadmissible, ${Rn(g)}, set to ${Rn(E)}`),g=E}if(!r&&!o)return z("polyserial",{rho:g,warnings:p});let w=Aa(i),b=m.map(E=>E-1),N=E=>q0(w,b,r?E.slice(1):h,E[0],r),{theta:v,at:y,bounded:d}=df(N,r?[g,...h]:[g]);if(d&&p.push(wf(v[0])),!o)return z("polyserial",{rho:v[0],warnings:p});let M=r?v.slice(1):h,x=l.length;return z("polyserial",{rho:v[0],warnings:p,var:gf(y.hessian,d),chisq:L0(m,w,M,r?v[0]:g),df:4*x-x-1,n:a,cuts:M})}export{ia as Anova,Qh as TukeyHSD,n0 as adTest,Xs as anova,jc as ansariTest,Xh as aov,Ad as aovEz,Bs as aovSummary,gc as bartlettTest,Dm as binomCI,Zm as binomDiffCI,fu as binomDiffEquivalenceTest,eu as binomRatioCI,Tm as binomTest,bu as boschlooTest,xh as brunnerMunzelTest,$f as chisqTest,Tu as cochranQTest,_l as cohenES,gd as cohensD,Gh as confint,Nf as corTest,zd as coxph,h0 as cronbachAlpha,r0 as cvmTest,Xu as dscfAllPairsTest,Zu as dunnTest,Yu as dunnettTest,Td as epsilonSquared,Up as esH,Gp as esW1,Yp as esW2,Ed as etaSquared,N0 as factanal,pr as fisherTest,vc as flignerTest,As as friedmanTest,Lf as gTest,Bu as gamesHowellTest,eh as glhtDunnett,lp as glm,Rp as glmAnova,yp as glmConfint,bp as glmCooksDistance,oa as glmHatvalues,Np as glmPredict,dp as glmResiduals,wp as glmRstandard,na as glmSummary,Df as goodmanKruskalGamma,wd as hedgesG,u0 as icc,e0 as jonckheereTest,f0 as kappa2,m0 as kappamFleiss,vo as kruskalTest,Jc as ksTest,Uu as kwAllPairsDunnTest,Gu as kwManyOneDunnTest,Vf as lblTest,jd as leveneTest,o0 as lillieTest,Js as lm,ih as mantelhaenTest,Hu as manyOneUTest,_p as mauchlyTest,Mu as mcnemarTest,In as modelMatrix,Nc as moodTest,su as oddsRatio,S0 as omega,xd as omegaSquared,_c as onewayTest,On as pAdjust,Oh as pairwisePropTest,_h as pairwiseTTest,Lh as pairwiseWilcoxTest,d0 as partialR,a0 as pearsonTest,Im as poissonTest,_0 as polychor,O0 as polyserial,kf as powerAnovaTest,Rf as powerPropTest,Tf as powerTTest,Yh as predict,vs as propTest,Yd as propTrendTest,rd as pwr2p2nTest,ed as pwr2pTest,ad as pwrAnovaTest,sd as pwrChisqTest,id as pwrF2Test,nd as pwrNormTest,td as pwrPTest,od as pwrRTest,Jp as pwrT2nTest,Zp as pwrTTest,bc as quadeTest,vf as setWarningHandler,s0 as sfTest,nh as shapiroTest,Cf as stuartMaxwellTest,Ld as survdiff,Fd as survfit,Xt as tTest,cu as tost,Qu as tukeyTest,dc as varTest,dr as wilcoxTest,Zh as zTest};