@numero-uno-jpn/rstats 1.8.0 → 1.10.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (87) hide show
  1. package/CHANGELOG.md +28 -0
  2. package/README.ja.md +7 -6
  3. package/README.md +6 -5
  4. package/dist/chunks/chunk-3QUOB47N.js +2 -0
  5. package/dist/chunks/{chunk-73IRLCTF.js → chunk-5PVS6TF2.js} +2 -2
  6. package/dist/chunks/chunk-6BDTLUNM.js +2 -0
  7. package/dist/chunks/{chunk-YD4U3QZ3.js → chunk-6U2ICRN3.js} +1 -1
  8. package/dist/chunks/chunk-6Y7OKIIM.js +2 -0
  9. package/dist/chunks/chunk-AQ7H4RJH.js +2 -0
  10. package/dist/chunks/{chunk-3DO726WZ.js → chunk-CN5D6ITZ.js} +2 -2
  11. package/dist/chunks/{chunk-V7ECXPWY.js → chunk-DC2FP6ZH.js} +2 -2
  12. package/dist/chunks/{chunk-IJST2ZGT.js → chunk-EDHAQHB7.js} +1 -1
  13. package/dist/chunks/chunk-F232C4SB.js +3 -0
  14. package/dist/chunks/{chunk-7ODUCEGX.js → chunk-F3WMBL5R.js} +2 -2
  15. package/dist/chunks/{chunk-QQHF5T2A.js → chunk-F5K2UXKW.js} +1 -1
  16. package/dist/chunks/{chunk-YT62DNDS.js → chunk-GYLFMAD6.js} +2 -2
  17. package/dist/chunks/{chunk-JXJDCQYE.js → chunk-IFFGJN5L.js} +2 -2
  18. package/dist/chunks/{chunk-TUL6UFLQ.js → chunk-ISNGTKVZ.js} +2 -2
  19. package/dist/chunks/chunk-JPQEOHZG.js +2 -0
  20. package/dist/chunks/{chunk-2ZQ2TSZS.js → chunk-JRMISESC.js} +2 -2
  21. package/dist/chunks/{chunk-LZB43TUL.js → chunk-K25FNCFH.js} +2 -2
  22. package/dist/chunks/{chunk-D3LKPD4H.js → chunk-KGDCQ2RV.js} +2 -2
  23. package/dist/chunks/{chunk-TYMC2D5I.js → chunk-LQWAHXHZ.js} +2 -2
  24. package/dist/chunks/{chunk-TQM6KGKT.js → chunk-MBQJWNPX.js} +2 -2
  25. package/dist/chunks/chunk-MULNGWZY.js +2 -0
  26. package/dist/chunks/{chunk-2X6PE6JF.js → chunk-MVIBJA2I.js} +2 -2
  27. package/dist/chunks/chunk-NAUAW5TO.js +2 -0
  28. package/dist/chunks/{chunk-RWZ6ICYN.js → chunk-NMVFSSEZ.js} +2 -2
  29. package/dist/chunks/{chunk-45HUI3M4.js → chunk-NNJGXB5K.js} +2 -2
  30. package/dist/chunks/{chunk-52RWDNO6.js → chunk-OQFZKQVT.js} +2 -2
  31. package/dist/chunks/chunk-OXYMJNVJ.js +2 -0
  32. package/dist/chunks/chunk-PJ73WRSA.js +2 -0
  33. package/dist/chunks/chunk-R5BXUEH3.js +2 -0
  34. package/dist/chunks/chunk-RPEFV4FR.js +2 -0
  35. package/dist/chunks/chunk-T3XFVWOW.js +2 -0
  36. package/dist/chunks/{chunk-GRTQ7FYQ.js → chunk-Y5M7NEJ5.js} +2 -2
  37. package/dist/chunks/chunk-Y7SXBQZY.js +2 -0
  38. package/dist/chunks/chunk-YCBQQHCB.js +2 -0
  39. package/dist/chunks/{chunk-JIAFWEED.js → chunk-YGKM6PIJ.js} +2 -2
  40. package/dist/chunks/{chunk-DG2RTNIR.js → chunk-ZUHBIFAJ.js} +2 -2
  41. package/dist/descriptive.js +2 -2
  42. package/dist/distributions.js +2 -2
  43. package/dist/entries/multivariate.d.ts +7 -0
  44. package/dist/entries/multivariate.js +8 -0
  45. package/dist/entries/tests.d.ts +3 -1
  46. package/dist/entries/tests.js +3 -1
  47. package/dist/htest/grubbs.d.ts +68 -0
  48. package/dist/htest/grubbs.js +151 -0
  49. package/dist/htest/multipleComparisons.d.ts +21 -0
  50. package/dist/htest/multipleComparisons.js +47 -0
  51. package/dist/htest/rankTests.d.ts +35 -0
  52. package/dist/htest/rankTests.js +89 -1
  53. package/dist/internal/summary.js +8 -2
  54. package/dist/linalg/symmetricLeading.d.ts +8 -0
  55. package/dist/linalg/symmetricLeading.js +231 -0
  56. package/dist/linalg.js +2 -2
  57. package/dist/mixed.js +2 -2
  58. package/dist/multivariate/cancor.d.ts +55 -0
  59. package/dist/multivariate/cancor.js +167 -0
  60. package/dist/multivariate/cmdscale.d.ts +40 -0
  61. package/dist/multivariate/cmdscale.js +171 -0
  62. package/dist/multivariate/dist.d.ts +63 -0
  63. package/dist/multivariate/dist.js +218 -0
  64. package/dist/multivariate/hclust.d.ts +58 -0
  65. package/dist/multivariate/hclust.js +264 -0
  66. package/dist/multivariate/kmeans.d.ts +58 -0
  67. package/dist/multivariate/kmeans.js +392 -0
  68. package/dist/multivariate/lda.d.ts +146 -0
  69. package/dist/multivariate/lda.js +325 -0
  70. package/dist/multivariate/manova.d.ts +106 -0
  71. package/dist/multivariate/manova.js +303 -0
  72. package/dist/multivariate.js +2 -0
  73. package/dist/numerics.js +2 -2
  74. package/dist/regression.js +2 -2
  75. package/dist/rstats.js +16 -16
  76. package/dist/special.js +2 -2
  77. package/dist/tests.js +15 -15
  78. package/llms.txt +4 -1
  79. package/package.json +6 -1
  80. package/dist/chunks/chunk-53OM63EU.js +0 -2
  81. package/dist/chunks/chunk-ANZUXEMH.js +0 -3
  82. package/dist/chunks/chunk-BHQGMZTI.js +0 -2
  83. package/dist/chunks/chunk-HXYH5ZMB.js +0 -2
  84. package/dist/chunks/chunk-HYJ4MIZE.js +0 -2
  85. package/dist/chunks/chunk-MLNWWGGE.js +0 -2
  86. package/dist/chunks/chunk-VB6PVRSN.js +0 -2
  87. package/dist/chunks/chunk-Y4KNH43T.js +0 -2
package/dist/tests.js CHANGED
@@ -1,22 +1,22 @@
1
- /*! rStats.js 1.8.0 | MIT License | includes code ported from SciPy, Boost.Math, statsmodels, LAPACK and others: see THIRD_PARTY_NOTICES.md at https://github.com/numero-uno-jpn/rStats.js */
2
- import{A as ps,a as vn,c as sn,h as Vr,i as ml,m as ul,n as $r,o as hl,s as ms,t as pl,u as wo,w as Nl,y as us,z as hs}from"./chunks/chunk-Y4KNH43T.js";import{a as Va,e as $a}from"./chunks/chunk-GRTQ7FYQ.js";import{a as gs,b as er,c as No,d as xl,e as Rl,f as ka,g as Ze,h as cn,i as Nn,j as _t,k as ql,l as ep,n as Ms,o as xo,p as Es}from"./chunks/chunk-RWZ6ICYN.js";import"./chunks/chunk-2X6PE6JF.js";import{a as ll,d as fs}from"./chunks/chunk-LZB43TUL.js";import{a as ys,f as nt,k as Wt,l as Vl}from"./chunks/chunk-53OM63EU.js";import"./chunks/chunk-7ODUCEGX.js";import{a as yo,b as _a,c as La,d as Ol,e as Mo,f as Fa,g as zn,h as Oa,i as Eo,j as Dl,k as Da,l as tp,m as Pa,n as Pl,o as np,p as rp,q as op}from"./chunks/chunk-BHQGMZTI.js";import{b as It,c as Fn}from"./chunks/chunk-V7ECXPWY.js";import{a as Tl,c as Al,d as Sl,e as wr,g as Ro}from"./chunks/chunk-3DO726WZ.js";import{b as qa}from"./chunks/chunk-QQHF5T2A.js";import{a as wt,b as ws,c as bs}from"./chunks/chunk-45HUI3M4.js";import{a as Ll,b as Cl,c as Ns,d as $n,e as jr,f as Ca}from"./chunks/chunk-ANZUXEMH.js";import{A as On,g as os,h as ss,j as as,k as Pr,p as ht,q as un,s as Tn,t as Yn,v as Gn,z as hn}from"./chunks/chunk-HYJ4MIZE.js";import{c as kl,h as Il,j as Lt,m as Ia,n as _l,o as vs,p as zr,v as Fl}from"./chunks/chunk-VB6PVRSN.js";import{a as Jn,b as Ml,g as ds}from"./chunks/chunk-YT62DNDS.js";import{A as fl,B as on,C as jt,i as bt,l as rs,x as Sa,y as dt,z as gt}from"./chunks/chunk-HXYH5ZMB.js";import{a as dn,c as El}from"./chunks/chunk-JXJDCQYE.js";import{b as dl,c as gl,d as wl,e as bl,f as vl,i as bo,l as Jh,n as Q,o as rt,p as Et}from"./chunks/chunk-TQM6KGKT.js";import{a as vo,b as Vn,c as yl,d as St,e as ft}from"./chunks/chunk-IJST2ZGT.js";import{a as Nt,b as en,c as an}from"./chunks/chunk-YD4U3QZ3.js";import"./chunks/chunk-JIAFWEED.js";import{b as nl,c as rl}from"./chunks/chunk-D3LKPD4H.js";import{A as Jc,B as el,D as tl,M as ol,q as Gt,r as ae,s as le}from"./chunks/chunk-73IRLCTF.js";import{A as sl,C as al,D as Dt,E as is,F as il,G as cs,H as cl,I as De,J as vt,K as bn,L as Pt,M as Zn,N as Vt,O as ls,P as qe,Q as Aa,b as Ot,c as Jt,d as he,e as Qc,f as Oe,g as _e,h as At,i as at,j as lt,k as Zc,l as B,m as Cn,p as Ta,q as go,r as Dn,s as pn,t as Pn,u as V,v as Ve,w as Xn,x as it,y as st,z as Qn}from"./chunks/chunk-2ZQ2TSZS.js";var $l=["alternative","mu","paired","varEqual","confLevel"];function yn(e,t,n={}){if(typeof e!="string"&&wt(t)&&B(t,$l,"tTest"),B(n,$l,"tTest"),typeof e=="string")return sp(e,t,n);let r;t!==void 0&&!ArrayBuffer.isView(t)&&!Array.isArray(t)?n=t:r=t;let o=Ze(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(cn(a),i&&r===void 0)throw new TypeError("y is required when paired is true");let l,f,m;if(i){let[S,C]=Zn(Array.from(e),Array.from(r));({d:l,k:m}=is(S,C))}else{let S=qe(r===void 0?[Pt(Array.from(e))]:[Pt(Array.from(e)),Pt(Array.from(r))]);m=S.k,l=S.groups[0],f=S.groups[1]}let u=V(s,-m),h,p,w,d,b,y,v,M=l.length,g=f!==void 0&&c;if(M<1||!g&&M<2)throw new Error("not enough 'x' observations");let N=it(l),x=M>1?st(l):NaN;if(f===void 0){if(p=M-1,w=Math.sqrt(x/M),w===0||w<10*Number.EPSILON*Math.abs(N))throw new Error("data are essentially constant");h=(N-u)/w,y=i?"Paired t-test":"One Sample t-test",d=N,b=i?"mean difference":"mean of x",v=i?"mean difference":"mean"}else{let S=f.length;if(S<1||!c&&S<2)throw new Error("not enough 'y' observations");if(c&&M+S<3)throw new Error("not enough observations");let C=it(f),L=S>1?st(f):NaN;if(y=`${c?"":"Welch"} Two Sample t-test`,d=[N,C],b=["mean of x","mean of y"],v="difference in means",c){p=M+S-2;let k=0;M>1&&(k+=(M-1)*x),S>1&&(k+=(S-1)*L),k/=p,w=Math.sqrt(k*(1/M+1/S))}else{let k=x/M,F=L/S;w=Math.sqrt(Math.sqrt(k)**2+Math.sqrt(F)**2),p=cs([k,F],[M-1,S-1])}if(w===0||w<10*Number.EPSILON*Math.max(Math.abs(N),Math.abs(C)))throw new Error("data are essentially constant");h=(N-C-u)/w}let E=typeof d=="number"?d:d[0]-d[1],R=(S,C)=>{let L=u+(h+C*S)*w;return Number.isFinite(L)&&Math.max(Math.abs(u),Math.abs(h*w))<=16*(Math.abs(E)+S*w)?L:E+C*S*w},T,A;if(o==="less")T=dt(h,p,!0,!1),A=[-1/0,R(gt(a,p,!0,!1),1)];else if(o==="greater")T=dt(h,p,!1,!1),A=[R(gt(a,p,!0,!1),-1),1/0];else{T=2*dt(-Math.abs(h),p,!0,!1);let S=1-a,C=S/2<1e-8?gt(S/2,p,!1,!1):gt(1-S/2,p,!0,!1);A=[R(C,-1),R(C,1)]}return A=[V(A[0],m),V(A[1],m)],d=typeof d=="number"?V(d,m):d.map(S=>V(S,m)),w=V(w,m),{method:y,alternative:o,statistic:h,statisticName:"t",parameter:p,parameterName:"df",pValue:T,confInt:A,confLevel:a,estimate:d,estimateNames:b,nullValue:s,nullValueName:v,stdErr:w,stdError:w,warnings:[]}}function sp(e,t,n){if(n.paired!==void 0)throw new Error("cannot use 'paired' in formula method");let r=gs(e,t,!0);if(r.kind==="one")return yn(r.x,n);if(r.kind==="paired")return yn(r.x,r.y,{...n,paired:!0});let[o,s]=r.levels;return{...yn(r.x,r.y,n),estimateNames:[`mean in group ${o}`,`mean in group ${s}`],nullValueName:`difference in means between group ${o} and group ${s}`}}var ap=new Set(["n","delta","sd","sigLevel","power","type","alternative","strict","tol"]),ip=["n","p1","p2","sigLevel","power","alternative","strict","tol"],cp=["groups","n","betweenVar","withinVar","sigLevel","power"],jl=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 ye=e=>e==null;function ln(e,t,n){if(!ye(e)&&!t(e))throw new RangeError(n)}var To=e=>e>=0&&e<=1,xs=e=>e>0&&e<1/0;function tn(e,t){if(e.filter(ye).length!==1)throw new RangeError(t)}function Ye(e,t,n,r={}){return Sl(e,t,n,r).root}var br=(e,t,n,r)=>on(e,t,{ncp:n,lowerTail:r});function za(e,t){if(e!=null&&!(typeof e=="number"&&e>=0&&e<=1))throw new RangeError(t)}var ja="'sig.level' must be numeric in [0, 1]";function lp(e={}){B(e,ap,"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;tn([t,n,o,r,s],"exactly one of 'n', 'delta', 'sd', 'power', and 'sig.level' must be NULL"),za(s,ja),ln(r,To,"'power' must be a probability"),ln(o,xs,"'sd' must be a positive finite number"),ln(t,w=>w>=2,"number of observations in each group must be at least 2"),ln(n,w=>!Number.isNaN(w),"'delta' must not be NaN"),ln(e.tol,xs,"'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??jl,f=i==="two.sample"?2:1,m=c==="two.sided"?2:1;m===2&&!ye(n)&&(n=Math.abs(n));let u=(w,d,b,y)=>{let v=Math.max(1e-7,w-1)*f,M=jt(y/m,v,{lowerTail:!1}),g=Math.sqrt(w/f)*d/b;Number.isFinite(g)||(g=Math.sqrt(w/f)*(d/b));let N=br(M,v,g,!1);return a&&m===2?N+br(-M,v,g,!0):N};if(ye(r))r=u(t,n,o,s);else{let w=r;if(ye(t)){if(n===0)throw new RangeError("'n' cannot be determined when 'delta' is 0: the power does not depend on it");t=Ye(d=>u(d,n,o,s)-w,2,1e7,{tol:l,extendInt:"upX"})}else if(ye(o)){if(n===0)throw new RangeError("'sd' cannot be determined when 'delta' is 0: the power does not depend on it");let d=Math.abs(n);o=Ye(b=>u(t,n,b,s)-w,d*1e-7,d*1e7,{tol:l,extendInt:n>0?"downX":"upX"})}else ye(n)?n=Ye(d=>u(t,d,o,s)-w,o*1e-7,o*1e7,{tol:l,extendInt:"upX"}):s=Ye(d=>u(t,n,o,d)-w,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 fp(e={}){B(e,ip,"powerPropTest");let{n:t,p1:n,p2:r,power:o}=e,s=e.sigLevel===void 0?.05:e.sigLevel;tn([t,n,r,o,s],"exactly one of 'n', 'p1', 'p2', 'power', and 'sig.level' must be NULL"),za(s,ja),ln(o,To,"'power' must be a probability"),ln(n,To,"'p1' must be a probability"),ln(r,To,"'p2' must be a probability"),ln(t,u=>u>0,"number of observations in each group must be positive"),ln(e.tol,xs,"'tol' must be a positive finite number");let i=Ae(e.alternative,["two.sided","one.sided"]),c=e.strict??!1,a=e.tol??jl,l=i==="two.sided"?2:1,f=(u,h,p,w)=>{let d=le(w/l,{lowerTail:!1});if(c&&l===2){let b=(h+p)/2,y=h*(1-h),v=p*(1-p),M=b*(1-b),g=Math.abs(h-p),N=Math.sqrt(y+v);return ae((Math.sqrt(u)*g-d*Math.sqrt(2*M))/N)+ae((Math.sqrt(u)*g+d*Math.sqrt(2*M))/N,{lowerTail:!1})}return ae((Math.sqrt(u)*Math.abs(h-p)-d*Math.sqrt((h+p)*(1-(h+p)/2)))/Math.sqrt(h*(1-h)+p*(1-p)))},m=[];if(ye(o))o=f(t,n,r,s);else{let u=o;if(ye(t)){if(n===r)throw new RangeError("'n' cannot be determined when 'p1' equals 'p2': the power does not depend on it");t=Ye(h=>f(h,n,r,s)-u,1,1e7,{tol:a,extendInt:"upX"})}else if(ye(n)){if(r===0)throw new RangeError("'p1' cannot be determined when 'p2' is 0: no p1 in [0, p2] differs from p2");n=Ye(h=>f(t,h,r,s)-u,0,r,{tol:a,extendInt:"yes"}),n<0&&m.push("No p1 in [0, p2] can be found to achieve the desired power")}else if(ye(r)){if(n===1)throw new RangeError("'p2' cannot be determined when 'p1' is 1: no p2 in [p1, 1] differs from p1");r=Ye(h=>f(t,n,h,s)-u,n,1,{tol:a,extendInt:"yes"}),r>1&&m.push("No p2 in [p1, 1] can be found to achieve the desired power")}else s=Ye(h=>f(t,n,r,h)-u,1e-10,1-1e-10,{tol:a,extendInt:"upX"}),(s<0||s>1)&&m.push("No significance level [0, 1] can be found to achieve the desired power")}return Q("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 zl=1e16,Rs=(e,t)=>e>zl&&e<1/0&&t>zl;function Ts(e,t,n,r){let o=le(r,{lowerTail:!1}),s=n/e,i=Math.sqrt(2/e+2/t),c=Math.hypot(Math.sqrt(2*(1+2*s)/e),Math.sqrt(2/t)*(1+s));return ae((o*i-s)/c,{lowerTail:!1})}function mp(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 V(V(e,-s)*V(t,-i)*V(n,-c)/V(r,-a),s+i+c-a)}function up(e={}){B(e,cp,"powerAnovaTest");let{groups:t,n,betweenVar:r,withinVar:o,power:s}=e,i=e.sigLevel===void 0?.05:e.sigLevel;if(tn([t,n,r,o,s,i],"exactly one of 'groups', 'n', 'between.var', 'within.var', 'power', and 'sig.level' must be NULL"),!ye(t)&&!(t>=2))throw new RangeError("number of groups must be at least 2");if(!ye(n)&&!(n>=2))throw new RangeError("number of observations in each group must be at least 2");za(i,ja),ln(s,To,"'power' must be a probability"),ln(r,a=>a>=0&&a<1/0,"'betweenVar' must be a nonnegative finite number"),ln(o,xs,"'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=mp(a-1,l,f,m));let p=a-1,w=(l-1)*a;return h===1/0?1:Rs(p,w)?Ts(p,w,h,u):Tn(Yn(u,p,w,{lowerTail:!1}),p,w,{ncp:h,lowerTail:!1})};if(ye(s))s=c(t,n,r,o,i);else{let a=s;if(ye(t))t=Ye(l=>c(l,n,r,o,i)-a,2,100);else if(ye(n)){if(r===0)throw new RangeError("'n' cannot be determined when 'betweenVar' is 0: the power does not depend on it");n=Ye(l=>c(t,l,r,o,i)-a,2,1e5)}else if(ye(o)){if(r===0)throw new RangeError("'withinVar' cannot be determined when 'betweenVar' is 0: the power does not depend on it");o=Ye(l=>c(t,n,r,l,i)-a,r*1e-7,r*1e7)}else ye(r)?r=Ye(l=>c(t,n,l,o,i)-a,o*1e-7,o*1e7):i=Ye(l=>c(t,n,r,o,l)-a,1e-10,1-1e-10)}return{groups:t,n,betweenVar:r,withinVar:o,sigLevel:i,power:s,note:"n is number in each group",method:"Balanced one-way analysis of variance power calculation",warnings:[]}}var hp=["correct","p","rescaleP"],pp=["confLevel"],dp=["alternative","rowScores","colScores"];function Xt(e){let t=[0,0];for(let n of e)t=he(t,[n,0]);return t}function Ba(e){let t=e.map(r=>{if(typeof r!="object"||r===null||typeof r.length!="number")throw new TypeError("'x' must be an array of numbers or an array of rows");return Array.from(r)}),n=t[0].length;if(t.some(r=>r.length!==n))throw new TypeError("all rows of the matrix must have the same length");if(t.flat().some(r=>typeof r!="number"))throw new TypeError("the entries of 'x' must be numbers");return t}var Ka=e=>e.length>0&&typeof e[0]!="number",Ua=(e,t)=>Number.isNaN(e)?NaN:bt(e,t/2,2,!1,!1);function Wl(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 Ao(e,t){return e.length>0&&Array.isArray(e[0])?e.map((n,r)=>n.map((o,s)=>Wl(o,t[r][s]))):e.map((n,r)=>Wl(n,t[r]))}var gp=["none","williams","yates"],Kl="Log likelihood ratio (G-test)";function wp(e,t={}){B(t,hp,"gTest");let n=Ae(t.correct,gp);if(typeof e!="object"||e===null||typeof e.length!="number")throw new TypeError("'x' must be an array of counts or of rows");if(Ka(e)){let o=Ba(e);return Bl(o.flat()),o.length===1?Q("gTest",Wa(o[0],n,t)):o[0].length===1?Q("gTest",Wa(o.map(s=>s[0]),n,t)):Q("gTest",bp(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 Bl(r),Q("gTest",Wa(r,n,t))}function Bl(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 Ul(e,t){return e<1/0?0:Math.ceil(Math.log2(t))+1}function Hl(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=he(n,el(o,s))}return n[0]+n[1]}function bp(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=Xt(e.flat()),s=Ul(o[0],n*r),i=s===0?e:e.map(v=>v.map(M=>V(M,-s))),c=s===0?o:Xt(i.flat()),a=i.map(v=>Xt(v)),l=i[0].map((v,M)=>Xt(i.map(g=>g[M]))),f=a.map(v=>l.map(M=>_e(v,at(M,c)))),m=i;if(t==="yates"){let[[v,M],[g,N]]=i,x=v*N-M*g>0?-.5:.5,E=V(.5,-s),R=x<0?-E:E;m=[[v+R,M-R],[g-R,N+R]]}let u=Hl(m.flat(),f.flat()),h=Number.isNaN(u)?["NaNs produced"]:[],p=1;if(t==="williams"){if(a.some(N=>N[0]===0)||l.some(N=>N[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],M=a.reduce((N,x)=>N+1/(x[0]+x[1]),0),g=l.reduce((N,x)=>N+1/(x[0]+x[1]),0);p=1+V((v*M-1)*(v*g-1)/(6*v*(n-1)*(r-1)),-s)}let w=V(2*u,s)/p,d=(n-1)*(r-1),b=f.map(v=>v.map(M=>V(M[0]+M[1],s)));return{method:`${Kl} test of independence ${t==="none"?"without correction":t==="williams"?"with Williams' correction":"with Yates' correction"}`,statistic:w,statisticName:"G",parameter:d,parameterName:"X-squared df",pValue:Ua(w,d),observed:s===0?m:m.map(v=>v.map(M=>V(M,s))),expected:b,ftResiduals:Ao(e,b),warnings:h}}function Wa(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=Xt(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=Xt(e),a=Ul(c[0],r),l=a===0?e:e.map(v=>V(v,-a)),f=a===0?c:Xt(l),m=s.map(v=>o?at(At(f,v),i):At(f,v)),u=m.map(v=>v[0]+v[1]),h=l;if(t==="yates"){let v=V(.5,-a),M=l[0]-u[0];M>V(.25,-a)?h=[l[0]-v,l[1]+v]:-M>V(.25,-a)&&(h=[l[0]+v,l[1]-v])}let p=Hl(h,m);if(!o&&!Number.isNaN(p)){let v=f[0]+f[1];p-=v*(i[0]-1+i[1])}let w=Number.isNaN(p)?["NaNs produced"]:[],d=t==="williams"?1+(r+1)/(6*V(f[0]+f[1],a)):1,b=V(2*p,a)/d,y=u.map(v=>V(v,a));return{method:`${Kl} goodness of fit test`,statistic:b,statisticName:"G",parameter:r-1,parameterName:"X-squared df",pValue:Ua(b,r-1),observed:a===0?h:h.map(v=>V(v,a)),expected:y,ftResiduals:Ao(e,y),warnings:w}}function vp(e){if(typeof e!="object"||e===null||!Ka(e))throw new RangeError("'x' must be square with at least two rows and columns");let t=Ba(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 Np(e){let t=vp(e),n=Xt(t.flat())[0],r=n>2**400||n>0&&n<2**-400?Math.round(Math.log2(n)):0,o=r===0?t:t.map(g=>g.map(N=>V(N,-r))),s=o.length,i=o.map(g=>Xt(g)),c=o.map((g,N)=>Xt(o.map(x=>x[N]))),a=o.map((g,N)=>o[N].some((x,E)=>E!==N&&x>0)||o.some((x,E)=>E!==N&&x[N]>0)).flatMap((g,N)=>g?[N]:[]),l=a.length;if(l<2)throw new RangeError("Too many equal marginals, cannot compute");let f=a.map((g,N)=>N),m=g=>f[g]===g?g:f[g]=m(f[g]);for(let g=0;g<l;g++)for(let N=g+1;N<l;N++)o[a[g]][a[N]]+o[a[N]][a[g]]>0&&(f[m(g)]=m(N));if(a.some((g,N)=>m(N)!==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(g=>{let N=he(i[g],[-c[g][0],-c[g][1]]);return N[0]+N[1]}),p=a.slice(0,u).map(g=>a.slice(0,u).map(N=>{if(g!==N)return-(o[g][N]+o[N][g]);let x=0;for(let E=0;E<s;E++)E!==g&&(x+=o[g][E]+o[E][g]);return x})),w=p.map(g=>g.map(()=>0)),d=[];for(let g=0;g<u;g++){let N=p[g][g];for(let x=0;x<g;x++)N-=w[g][x]*w[g][x]*d[x];d.push(N);for(let x=g+1;x<u;x++){let E=p[x][g];for(let R=0;R<g;R++)E-=w[x][R]*w[g][R]*d[R];w[x][g]=E/N}}let b=0,y=[];for(let g=0;g<u;g++){let N=h[g];for(let x=0;x<g;x++)N-=w[g][x]*y[x];y.push(N),b+=N*N/d[g]}let v=Xt(a.flatMap(g=>a.map(N=>t[g][N]))),M=l-1;return b=V(b,r),Q("stuartMaxwellTest",{method:"Stuart-Maxwell test",statistic:b,statisticName:"chi-squared",parameter:M,parameterName:"df",pValue:Ua(b,M),N:v[0]+v[1],warnings:[]})}function yp(e,t={}){B(t,pp,"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=Yl(e),o=Xt(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(g=>g.map(N=>V(N,-s))),{pc:c,pd:a}=Mp(i),l=[0,0],f=[0,0];i.forEach((g,N)=>g.forEach((x,E)=>{l=he(l,At(c[N][E],x)),f=he(f,At(a[N][E],x))})),l=[l[0]/2,l[1]/2],f=[f[0]/2,f[1]/2];let m=he(l,f),u=at(Oe(l,f),m)[0],h=_e(m,m),p=i.map((g,N)=>g.map((x,E)=>2*at(Oe(_e(f,c[N][E]),_e(l,a[N][E])),h)[0])),w=Math.max(...p.flat().map(Math.abs)),d=w>0&&w<1/0?Math.floor(Math.log2(w)):0,b=0;i.forEach((g,N)=>g.forEach((x,E)=>{let R=V(p[N][E],-d);b+=x*R*R}));let y=V(Math.sqrt(b),d-s/2),v=(1-n)/2,M=v<1e-8?le(v,{lowerTail:!1}):le(1-v);return Q("goodmanKruskalGamma",{estimate:u,confInt:[Math.max(-1,u-M*y),Math.min(1,u+M*y)],confLevel:n,ase:y,warnings:[]})}function Yl(e){if(typeof e!="object"||e===null||!Ka(e))throw new TypeError("'table' must be an array of rows");let t=Ba(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 Mp(e){let t=e.length,n=e[0].length,r=[0,0],o=e.map(p=>{let w=[],d=r;for(let b=0;b<n;b++)w.push(d),d=he(d,[p[b],0]);return w}),s=e.map(p=>{let w=new Array(n),d=r;for(let b=n-1;b>=0;b--)w[b]=d,d=he(d,[p[b],0]);return w}),i=p=>{let w=[],d=new Array(n).fill(r);for(let b=0;b<t;b++)w.push(d),d=d.map((y,v)=>he(y,p[b][v]));return w},c=p=>{let w=new Array(t),d=new Array(n).fill(r);for(let b=t-1;b>=0;b--)w[b]=d,d=d.map((y,v)=>he(y,p[b][v]));return w},a=i(o),l=c(s),f=i(s),m=c(o),u=a.map((p,w)=>p.map((d,b)=>he(d,l[w][b]))),h=f.map((p,w)=>p.map((d,b)=>he(d,m[w][b])));return{pc:u,pd:h}}function Ep(e,t={}){B(t,dp,"lblTest");let n=Ze(t.alternative),r=Yl(e),o=r.length,s=r[0].length,i=(R,T,A)=>{if(R===void 0)return Array.from({length:T},(C,L)=>L+1);let S=Array.from(R);if(S.length!==T)throw new RangeError(`'${A}' must have one score for each ${A==="rowScores"?"row":"column"}`);if(S.some(C=>typeof C!="number"||!Number.isFinite(C)))throw new RangeError(`'${A}' must be finite numbers`);return S},c=i(t.rowScores,o,"rowScores"),a=i(t.colScores,s,"colScores"),f=Xt(r.flat())[0]>2**1e3?2*Math.ceil((Math.log2(Math.max(...r.flat()))-990)/2):0,m=f===0?r:r.map(R=>R.map(T=>V(T,-f))),u=m.map(R=>Xt(R)).map(R=>R[0]+R[1]),h=m[0].map((R,T)=>Xt(m.map(A=>A[T]))).map(R=>R[0]+R[1]),p=u.reduce((R,T)=>R+T,0);if(!(f>0||p>1))throw new RangeError("the table must have a total above 1");let w=u.reduce((R,T,A)=>R+T*c[A],0)/p,d=h.reduce((R,T,A)=>R+T*a[A],0)/p,b=c.map(R=>R-w),y=a.map(R=>R-d),v=u.reduce((R,T,A)=>R+T*b[A]*b[A],0),M=h.reduce((R,T,A)=>R+T*y[A]*y[A],0),g=0;if(m.forEach((R,T)=>R.forEach((A,S)=>g+=A*b[T]*y[S])),!(v>0&&M>0))throw new RangeError("the row and column scores must vary over the observations (the variance of the statistic is 0)");let N=g/Math.sqrt(v)/Math.sqrt(M),x=f===0?Math.sqrt(p-1)*N:V(Math.sqrt(p-V(1,-f))*N,f/2),E=n==="less"?ae(x):n==="greater"?ae(x,{lowerTail:!1}):2*ae(-Math.abs(x));return Q("lblTest",{method:"Linear-by-Linear Association Test",alternative:n,statistic:x,statisticName:"Z",pValue:E,warnings:[]})}var Gl=["correct","p","rescaleP","simulatePValue","B","nMinus1"];function gn(e){let t=[0,0],n=0;for(let r of e)t=he(t,[r,0]),n+=r;return Number.isFinite(n)?t[0]:n}function xp(e,t,n){return wt(t)&&B(t,Gl,"chisqTest"),B(n,Gl,"chisqTest"),Array.isArray(t)?Q("chisqTest",Rp(e,t,n??{})):Q("chisqTest",Tp(e,t??{}))}var Zl="Chi-squared approximation may be incorrect";function Rp(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(dn),c=[...new Set(s)].sort(dn);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])]++),{...rf(f,n),rowLevels:i,colLevels:c}}function Tp(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?Ha(r[0],t):o===1?Ha(r.map(s=>s[0]),t):rf(r,t)}return Ha(Array.from(e),t)}function Jl(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 ef(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 tf(e,t){return Number.isNaN(e)?NaN:bt(e,t/2,2,!1,!1)}function nf(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 Ha(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(w=>!(w>=0&&w<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=gn(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(w=>w<0))throw new RangeError("probabilities must be non-negative");if(o.some(w=>w===1/0))throw new RangeError("probabilities must be finite");if(t.rescaleP??!1){let w=gn(o),d=w<1/0?0:Math.ceil(Math.log2(n)),b=d===0?o:o.map(v=>V(v,-d)),y=d===0?w:gn(b);o=b.map(v=>v/y)}else if(Math.abs(gn(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(w=>V(w,-s)),c=s===0?r:gn(i),a=o.map(w=>c*w),l=s===0?a:a.map(w=>V(w,s));nf(l);let f=o.map((w,d)=>a[d]*(1-w)),m=V(gn(i.map((w,d)=>Vt(w-a[d],a[d]))),s),u="Chi-squared test for given probabilities",h=n-1,p;if(t.simulatePValue??!1){let w=Jl(t);if(!Number.isInteger(r))throw new RangeError("the simulation needs counts summing to an integer");u+=vo(w),h=NaN,p=ef(Ap(r,o,l,w),m,w)}else p=tf(m,h);return{method:u,statistic:m,statisticName:"X-squared",parameter:h,parameterName:"df",pValue:p,observed:e,expected:l,residuals:i.map((w,d)=>V((w-a[d])/Math.sqrt(a[d]),s/2)),stdres:i.map((w,d)=>V((w-a[d])/Math.sqrt(f[d]),s/2)),ftResiduals:Ao(e,l),warnings:Number.isFinite(h)&&l.some(w=>w<5)?[Zl]:[]}}function Ap(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=Nl(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 Sp(e,t,n,r){let o=e.length,s=t.length,i=e.reduce((f,m)=>f+m,0);if(i>us)throw new RangeError("the simulation needs a total count of at most 16777216 (2^24)");let c=hs(i),a=new Int32Array(o*s),l=new Float64Array(Math.trunc(r));for(let f=0;f<l.length;f++){ps(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 rf(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=gn(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=>gn(v)),l=e[0].map((v,M)=>gn(e.map(g=>g[M]))),f=a.map(v=>l.map(M=>ls(v,M,c)));nf(f.flat());let m=e.map((v,M)=>v.map((g,N)=>g-f[M][N])),u="Pearson's Chi-squared test",h,p=(o-1)*(s-1),w,d=(t.simulatePValue??!1)&&a.every(v=>v>0)&&l.every(v=>v>0),b=!d&&r&&o===2&&s===2?Math.min(.5,...m.flat().map(Math.abs)):0,y=[];if((t.simulatePValue??!1)&&!d&&y.push("cannot compute simulated p-value with zero marginals"),d){if(e.some(g=>g.some(N=>!Number.isInteger(N))))throw new RangeError("the simulation needs integer counts");let v=Jl(t);u+=vo(v),h=gn(m.flatMap((g,N)=>g.map((x,E)=>Vt(x,f[N][E])))),p=NaN;let M=Sp(a.map(g=>Math.trunc(g)),l.map(g=>Math.trunc(g)),f,v);w=ef(M,h,v)}else if(b>0)u+=" with Yates' continuity correction",h=gn(m.flatMap((v,M)=>v.map((g,N)=>Vt(Math.abs(g)-b,f[M][N]))));else if(h=gn(m.flatMap((v,M)=>v.map((g,N)=>Vt(g,f[M][N])))),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:w??tf(h,p),observed:e,expected:f,residuals:m.map((v,M)=>v.map((g,N)=>g/Math.sqrt(f[M][N]))),stdres:qp(e,c,a,l,f,m),ftResiduals:Ao(e,f),warnings:Number.isFinite(p)&&f.some(v=>v.some(M=>M<5))?[...y,Zl]:y}}function qp(e,t,n,r,o,s){if(!Number.isFinite(t))return s.map((p,w)=>p.map((d,b)=>d/Math.sqrt(o[w][b]*(1-n[w]/t)*(1-r[b]/t))));let i=e.map(p=>Xl(p)),c=e[0].map((p,w)=>Xl(e.map(d=>d[w]))),a=Ip(i),l=Ql(i),f=Ql(c),m=l.map(p=>at(p,a)),u=c.map(p=>at(p,a)),h=f.map(p=>at(p,a));return e.map((p,w)=>p.map((d,b)=>{let y=_e(i[w],u[b]),v=Oe([d,0],y),M=v[0]+v[1],g=_e(_e(y,m[w]),h[b]),N=g[0]+g[1];if(N>=kp||M===0)return M/Math.sqrt(N);let x=y[0]+y[1],E=m[w][0]+m[w][1],R=h[b][0]+h[b][1];return M/Math.sqrt(x)/Math.sqrt(E)/Math.sqrt(R)}))}var kp=2**-1022;function Xl(e){let t=[0,0];for(let n of e)t=he(t,[n,0]);return t}function Ip(e){let t=[0,0];for(let n of e)t=he(t,n);return t}function Ql(e){let t=e.length,n=new Array(t+1);n[t]=[0,0];for(let s=t-1;s>=0;s--)n[s]=he(e[s],n[s+1]);let r=[],o=[0,0];for(let s=0;s<t;s++)r.push(he(o,n[s+1])),o=he(o,e[s]);return r}var _p=Math.pow(Number.EPSILON,.25);function Bt(e,t,n,r={}){let o={tol:r.tol??_p,maxiter:r.maxIter??1e3};return r.fLower!==void 0&&(o.fLower=r.fLower),r.fUpper!==void 0&&(o.fUpper=r.fUpper),wr(e,t,n,o).root}function tr(e,t){let n=a=>e(Math.exp(a)),r=0,o=n(0),s=t?-1:1,i=n(s);for(;i!==0&&Math.sign(i)===Math.sign(o);){if(Math.abs(s)>745)return t?0:1/0;r=s,o=i,s*=2,i=n(s)}if(i===0)return Math.exp(s);let c=r<s?Bt(n,r,s,{tol:2**-50,fLower:o,fUpper:i}):Bt(n,s,r,{tol:2**-50,fLower:i,fUpper:o});return Math.exp(c)}var Lp=["alternative","confInt","confLevel","or","simulatePValue","B","midp"];function ko(e,t={}){B(t,Lp,"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(rl));if(s.flat().some(f=>f>2147483647))throw new RangeError("'x' has entries too large to be integer");let i=[],c=Cp(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 Q("fisherTest",{...Pp(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 Q("fisherTest",{method:a+vo(f),alternative:l,pValue:Fp(s,f),warnings:i})}return Q("fisherTest",{method:a,alternative:l,pValue:Vp(s),warnings:i})}function Cp(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: ${yl(a)}`:null}function Fp(e,t){let n=e.filter(d=>d.some(b=>b>0)),r=e[0].map((d,b)=>b).filter(d=>e.some(b=>b[d]>0));if(n.length<=1)throw new RangeError("need 2 or more non-zero row marginals");if(r.length<=1)throw new RangeError("need 2 or more non-zero column marginals");let o=n.map(d=>r.map(b=>d[b])),s=o.map(d=>d.reduce((b,y)=>b+y,0)),i=o[0].map((d,b)=>o.reduce((y,v)=>y+v[b],0)),c=s.length,a=i.length,l=[0,0];for(let d of o.flat())l=he(l,[-fs(d),0]);let f=l[0]/(1+64*Number.EPSILON),m=s.reduce((d,b)=>d+b,0);if(m>us)throw new RangeError("the simulation needs a total count of at most 16777216 (2^24)");let u=hs(m),h=new Int32Array(c*a),p=0,w=Math.trunc(t);for(let d=0;d<w;d++){ps(s,i,u,h);let b=0;for(let y=0;y<h.length;y++)b-=u[h[y]];b<=f&&p++}return Math.max(0,Math.min(1,(1+p)/(w+1)))}var of=1+1e-7,Op=16,Dp=2**24;function So(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=he(r,[e[o],0]));return r[0]}var Wr=class{m;n;k;lo;hi;logCentral;constructor(t,n,r){this.m=t,this.n=n,this.k=r,this.lo=Math.max(0,r-n),this.hi=Math.min(r,t),this.logCentral=new Float64Array(this.hi-this.lo+1);let o=0,s=1;for(let i=this.lo;i<=this.hi;i++){let c=i-this.lo;c%Op===0||!(s>0&&s<1/0)?(o=vn(i,t,n,r,!0),s=1,this.logCentral[c]=o):this.logCentral[c]=o+Math.log(s),s*=(t-i)*(r-i)/((i+1)*(n-r+i+1))}}density(t){let n=this.logCentral.length,r=Math.log(t),o=new Float64Array(n),s=-1/0;for(let c=0;c<n;c++){let a=this.logCentral[c]+(this.lo+c)*r;o[c]=a,a>s&&(s=a)}for(let c=0;c<n;c++){let a=o[c]-s;o[c]=a<-746?0:Math.exp(a)}let i=So(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=he(r,[(this.lo+o)*n[o],0]));return r[0]}tail(t,n,r){if(n===1)return r?sn(t-1,this.m,this.n,this.k,!1,!1):sn(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?So(o,s,o.length-1):So(o,0,s)}midTail(t,n,r){if(n===1){let i=.5*vn(t,this.m,this.n,this.k,!1);return(r?sn(t,this.m,this.n,this.k,!1,!1):sn(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?So(o,s+1,o.length-1):So(o,0,s-1))+.5*o[s]}};function Ga(e,t,n){let r=e.density(n),o=r[t-e.lo]*of,s=[0,0];for(let i of r)i<=o&&(s=he(s,[i,0]));return s[0]}function sf(e,t,n,r){let o=Math.max(0,r-n),s=Math.min(r,t),i=w=>vn(w,t,n,r,!0),c=i(e)+Math.log(of),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=(w,d)=>{let b=w,y=d+1;for(;y-b>1;){let v=b+Math.floor((y-b)/2);i(v)<=c?b=v:y=v}return b},f=(w,d)=>{let b=w-1,y=d+1;for(;y-b>1;){let v=b+Math.floor((y-b)/2);i(v)<=c?y=v:b=v}return y},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:sn(m,t,n,r,!0,!1),p=u>s?0:sn(u-1,t,n,r,!1,!1);return h+p}function qo(e,t){return tr(e,t)}function Pp(e,t,n){let r=Ze(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>Dp)throw new RangeError(`the exact distribution of the 2 x 2 table has ${m} values, more than 2^24`);let u=new Wr(i+a,c+l,i+c),{lo:h,hi:p}=u,w=t.midp??!1,d;w?(r==="less"?d=u.midTail(f,o,!1):r==="greater"?d=u.midTail(f,o,!0):d=Math.min(1,2*Math.min(u.midTail(f,o,!1),u.midTail(f,o,!0))),n=r==="two.sided"?"Central Fisher's Exact Test (mid-p version)":"One-sided Fisher's Exact Test (mid-p version)"):r==="less"?d=u.tail(f,o,!1):r==="greater"?d=u.tail(f,o,!0):o===0?d=f===h?1:0:o===1/0?d=f===p?1:0:d=Ga(u,f,o);let b;if(f===h)b=0;else if(f===p)b=1/0;else{let N=u.mean(1);b=N===f?1:qo(x=>u.mean(x)-f,N>f)}let y={method:n,alternative:r,pValue:d,estimate:b,estimateNames:"odds ratio",nullValue:o,nullValueName:"odds ratio",warnings:[]};if(!(t.confInt??!0))return y;let v=N=>{if(f===p)return 1/0;if(w){if(f===h&&N>=.5)return 0;let E=u.midTail(f,1,!1);return E===N?1:qo(R=>u.midTail(f,R,!1)-N,E<N)}let x=u.tail(f,1,!1);return x===N?1:qo(E=>u.tail(f,E,!1)-N,x<N)},M=N=>{if(f===h)return 0;if(w){if(f===p&&N>=.5)return 1/0;let E=u.midTail(f,1,!0);return E===N?1:qo(R=>u.midTail(f,R,!0)-N,E>N)}let x=u.tail(f,1,!0);return x===N?1:qo(E=>u.tail(f,E,!0)-N,x>N)},g;if(r==="less")g=[0,v(1-s)];else if(r==="greater")g=[M(1-s),1/0];else{let N=(1-s)/2;g=[M(N),v(N)]}return{...y,confInt:g,confLevel:s}}var As=[[0,0]];function An(e){for(let t=As.length;t<=e;t++)As.push(he(As[t-1],Zc([t,0])));return As[e]}function Ss(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 Ya=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 Vp(e){let t=e.filter(v=>v.some(M=>M>0));if(t.length===0)throw new RangeError("all entries of the table are zero");let n=t[0].map((v,M)=>t.some(g=>g[M]>0));if(t=t.map(v=>v.filter((M,g)=>n[g])),t.length<2||t[0].length<2)return 1;t.length>t[0].length&&(t=t[0].map((v,M)=>t.map(g=>g[M])));let r=t.length,o=t.map(v=>v.reduce((M,g)=>M+g,0)),s=t[0].map((v,M)=>t.reduce((g,N)=>g+N[M],0)).sort((v,M)=>M-v),i=s.length,c=o.reduce((v,M)=>v+M,0),a=Qc(An(c));for(let v of o)a=he(a,An(v));for(let v of s)a=he(a,An(v));let l=[0,0];for(let v of t.flat())l=Oe(l,An(v));let f=he(l,[Math.log1p(1e-7),0]),m=1e-9*(1+Math.abs(f[0])),[u,h]=f,p=Array.from({length:i},()=>new Map),w=(v,M)=>{let g=M.join(","),N=p[v].get(g);if(N!==void 0)return N;let x=he(a,An(M.reduce((C,L)=>C+L,0)));for(let C of M)x=Oe(x,An(C));for(let C=v;C<i;C++)x=Oe(x,An(s[C]));let E={R:M,KZ:x,max:0,min:0,edges:[],pastHi:[],pastLo:[],count:[],index:new Map};if(p[v].set(g,E),v===i-1){let C=[0,0];for(let L of M)C=Oe(C,An(L));return E.max=E.min=C[0],E.edges.push({to:E,hi:C[0],lo:C[1],mult:1}),E}let R=new Map,T=new Array(r+1).fill(0);for(let C=r-1;C>=0;C--)T[C]=T[C+1]+M[C];let A=new Array(r).fill(0),S=(C,L,k)=>{if(C===r-1){A[C]=L;let O=Oe(k,An(L)),q=M.map((K,X)=>K-A[X]).sort((K,X)=>K-X),$=w(v+1,q),_=`${q.join(",")}|${O[0]}`,P=R.get(_);P===void 0?R.set(_,{to:$,hi:O[0],lo:O[1],mult:1}):P.mult++;return}let F=Math.max(0,L-T[C+1]),I=Math.min(M[C],L);for(let O=F;O<=I;O++)A[C]=O,S(C+1,L-O,Oe(k,An(O)))};if(S(0,s[v],[0,0]),E.edges=[...R.values()],E.max=Nt(E.edges.map(C=>C.hi+C.to.max)),E.min=an(E.edges.map(C=>C.hi+C.to.min)),v===i-2){let C=E.edges.map($=>{let _=$.to.edges[0];return{v:he([$.hi,$.lo],[_.hi,_.lo]),mult:$.mult}});C.sort(($,_)=>$.v[0]-_.v[0]||$.v[1]-_.v[1]);let L=C.length,k=C[L-1].v[0],F=new Float64Array(L),I=new Float64Array(L),O=new Float64Array(L),q=[0,0];C.forEach(({v:$,mult:_},P)=>{F[P]=$[0],I[P]=$[1];let K=Oe($,[k,0]);q=he(q,[_*Math.exp(K[0])*(1+K[1]),0]),O[P]=q[0]}),E.dist={hi:F,lo:I,cum:O,vmax:k,kv:he(a,[k,0])}}return E},d=w(0,[...o].sort((v,M)=>v-M)),b=new Ya,y=(v,M,g,N)=>{if(M+v.max<=u-m){let[S,C]=Ss(v.KZ[0],v.KZ[1],M,g);return b.add(S,C,N),!0}if(M+v.min>u+m)return!0;let x=v.dist;if(x===void 0)return!1;let[E,R]=Ss(u,h,-M,-g),T=0,A=x.hi.length;for(;T<A;){let S=T+A>>>1,C=x.hi[S];C<E||C===E&&x.lo[S]<=R?T=S+1:A=S}if(T>0){let[S,C]=Ss(x.kv[0],x.kv[1],M,g);b.add(S,C,N*x.cum[T-1])}return!0};y(d,0,0,1)||(d.pastHi.push(0),d.pastLo.push(0),d.count.push(1));for(let v=0;v<i-2;v++)for(let M of p[v].values()){let{pastHi:g,pastLo:N,count:x}=M;if(x.length!==0){for(let{to:E,hi:R,lo:T,mult:A}of M.edges)for(let S=0;S<x.length;S++){let[C,L]=Ss(g[S],N[S],R,T),k=x[S]*A;if(y(E,C,L,k))continue;let F=E.index.get(C);F===void 0?(E.index.set(C,E.count.length),E.pastHi.push(C),E.pastLo.push(L),E.count.push(k)):E.count[F]+=k}M.pastHi=M.pastLo=M.count=[],M.index.clear()}}return Math.min(1,b.value())}function qs(e){let t=e.every(n=>Number.isInteger(n))?1:2;return[t,e.map(n=>n*t).sort((n,r)=>n-r)]}function Qa(e){return[...e].sort((t,n)=>t-n).join(",")}function Xa(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 Za(e){let[,t]=qs(e),n=0,r=0;for(let o of t)r+=n+=o;return{steps:r,size:n+1}}function Ja(e){let[t,n]=qs(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 Xa(t,0,o)}function ei(e,t){let[,n]=qs(e),r=n.length,o=Math.min(t,r-t),s=af(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 af(e){let t=[0];for(let n of e)t.push(t[t.length-1]+n);return t}function ti(e,t){let[n,r]=qs(e),o=r.length,s=Math.min(t,o-t),i=af(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 w=Math.min(h,s);w>=Math.max(1,s-(o-h));w--){let d=c[w],b=c[w-1],y=w/h,v=(h-w)/h,M=i[w],g=i[h]-i[h-w],N=i[w-1]+p,x=Math.max(M,N);for(let E=g;E>=x;E--)d[E-M]=v*d[E-M]+y*b[E-N];for(let E=x-1;E>=M;E--)d[E-M]=v*d[E-M]}}let a=c[s],l=t*(t+1)/2;if(s===t)return Xa(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 Xa(n,(i[o]-u)/n-l,m)}function nr(e,t){let n=Math.ceil(e.f*(t-e.offset)+e.f*1e-8)-1;return n<0?0:n>=e.lower.length?1:Math.min(1,e.lower[n])}function vr(e,t){let n=nr(e,t);if(Math.abs(n-1)>=1/64)return 1-n;let r=Math.ceil(e.f*(t-e.offset)+e.f*1e-8);return r>=e.upper.length?0:Math.min(1,e.upper[Math.max(0,r)])}var cf=["alternative","mu","paired","exact","correct","confInt","confLevel","tolRoot"];function Io(e,t,n={}){if(typeof e!="string"&&wt(t)&&B(t,cf,"wilcoxTest"),B(n,cf,"wilcoxTest"),typeof e=="string"){if(n.paired!==void 0)throw new Error("cannot use 'paired' in formula method");let w=gs(e,t,!0);return w.kind==="one"?Io(w.x,n):Io(w.x,w.y,w.kind==="paired"?{...n,paired:!0}:n)}let r;t!==void 0&&!ArrayBuffer.isView(t)&&!Array.isArray(t)?n=t:r=t;let o=Ze(n.alternative),s=n.mu??0,i=n.paired??!1,c=n.exact??null,a=zp(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[w,d]=Zn(Array.from(e),Array.from(r));u=Pt(w.map((b,y)=>b-d[y]))}else u=Pt(Array.from(e)),r!==void 0&&(h=Pt(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 Et};return Q("wilcoxTest",h===void 0?Up(u,c,i,p):ed(u,h,c,p))}function zp(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 ks(e,t,n){return n?t==="two.sided"?Math.sign(e)*.5:t==="greater"?.5:-.5:0}var mf=10*Number.EPSILON;function uf(e,t,n=0){let r=a=>Math.min(1,Math.max(a,0)),o=()=>n===0?ae(e):r(ae(e)-n*Gt(e)),s=()=>n===0?ae(e,{lowerTail:!1}):r(ae(e,{lowerTail:!1})+n*Gt(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 jp(e){let t=e*e;return[e*(t-3),e*(t*(t-10)+15),e*(t*(t*(t-21)+105)-105)]}function Wp(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 hf(e,o,s,n)}function Bp(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 hf(e,i,c,r)}function hf(e,t,n,r){let[o,s,i]=jp(e),c=t*o;return r>=2&&(c+=n*s),r>=3&&(c+=t*t/2*i),c}function oi(e){let t=e.length;if(t===0)return NaN;let n=Math.floor(t/2);return t%2===1?e[n]:it([e[n-1],e[n]])}var si=(e,t)=>e-t,ni=5e9,Kp=40;function ri(e){return new RangeError(`wilcoxTest: the exact conditional ${e} would take too long or too much memory: use exact: false`)}function Is(e,t){if(e.size>16777216||e.steps>ni)throw ri(t);return e.steps}function pf(e){let t=e.length,n=vt(e.map(Math.abs)),r=0;for(let i=0;i<t;i++)e[i]>0&&(r+=n[i]);let o=0;for(let i of bn(n))o+=i*i*i-i;let s=Math.sqrt(t*(t+1)*(2*t+1)/24-o/48);return[r,r-t*(t+1)/4,s]}function Up(e,t,n,r){let{alternative:o,mu:s}=r,i=e.map(g=>g-s);i.some((g,N)=>!Number.isFinite(g)&&Number.isFinite(e[N]))&&(i=e.map(g=>g/2-s/2));let a=i.some(g=>g===0),l=t??i.length<50,f=(g,N,x)=>({method:g,alternative:o,statistic:N,statisticName:"V",pValue:x,nullValue:s,nullValueName:n?"location shift":"location",warnings:r.warnings.list});if(l){let g=vt(i.map(Math.abs)),N=bn(g).length>0,x=0;for(let C=0;C<i.length;C++)i[C]>0&&(x+=g[C]);let E="Wilcoxon signed rank exact test";if(!N&&!a){let C=i.length,L=C*(C+1)/2,k;if(o==="less")k=Vr(x,C,!0,!1);else if(o==="greater")k=Vr(x-1,C,!1,!1);else{let I=x>L/2?Vr(x-1,C,!1,!1):Vr(x,C,!0,!1);k=Math.min(1,2*I)}let F=f(E,x,k);return r.confInt?{...F,...Hp(e,r)}:F}let R=g.filter((C,L)=>i[L]!==0);Is(Za(R),"distribution");let T=Ja(R),A;if(o==="less")A=nr(T,x);else if(o==="greater")A=vr(T,x-.25);else{let C=0;for(let L of R)C+=L/2;A=Math.min(1,2*(x>C?vr(T,x-.25):nr(T,x)))}let S=f(E,x,A);return r.confInt?{...S,...Gp(e,r)}:S}let m=a?i.filter(g=>g!==0):i,u=m.length;if(u===0)throw new RangeError("no non-zero differences for the signed rank test");let[h,p,w]=pf(m),b=bn(vt(m.map(Math.abs))).length>0||a?Math.min(r.correct,0):r.correct,y=(p-ks(p,o,b>=0))/w,v=uf(y,o,b>=1?Wp(y,u,b):0),M=f(`Wilcoxon signed rank test${r.correct>=0?" with continuity correction":""}`,h,v);return r.confInt?{...M,...Xp(e,r)}:M}function ai(e,t,n){return e==="less"?[-1/0,n]:e==="greater"?[t,1/0]:[t,n]}function lf(e,t,n){let r={confInt:ai(e,NaN,NaN),confLevel:0,estimate:t};return n!==void 0&&(r.estimateNames=n),r}function df(e){if(!(e>0&&e<1/0))throw new RangeError("tolRoot must be a positive finite number")}function gf(e){let t=e.length,n=[];for(let r=0;r<t;r++)for(let o=r;o<t;o++){let s=en(e[r],e[o]);Number.isNaN(s)||n.push(s)}return n.sort(si)}function wf(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+mf&&l++;let f=oi(e);if(l===0)return{confInt:[-1/0,1/0],confLevel:1,estimate:f,estimateNames:o};let m=t(l-1);return{confInt:ai(s,e[l-1],e[c-l]),confLevel:1-(s==="two.sided"?2*m:m),estimate:f,estimateNames:o}}function Hp(e,t){let n=e.length;return wf(gf(e),r=>Vr(r,n,!0,!1),r=>ml(r,n,!0,!1),t,"(pseudo)median")}function bf(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 vf(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)+mf,w=f!=="less",d=f!=="greater",b=k=>en(e[k-1],e[k]),y=Math.min(u-1,Math.max(1,Math.ceil(i.mean+le(Math.min(p,1))*i.sd+.1*i.sd+3))),v=8,M=k=>k===0?e[0]-1:b(k),g=k=>k===0?e[u-1]+1:b(u-k),N=[new Map,new Map];{let k=new Map,F=0,I=Kp*c;for(let[O,q]of[[0,M],[1,g]])if(!(O===0?!w:!d))for(let $=0;$<=y;$++){if(F+=I,F>ni)throw ri("interval");let _;try{_=t(q($))}catch{break}if(N[O].set($,[_.stat,_.key]),k.has(_.key)){k.delete(_.key),k.set(_.key,!0);continue}if(F+=r(_.scores),F>ni)throw ri("interval");k.set(_.key,!0),k.size>v&&k.delete(k.keys().next().value)}}let x=new Map,E=(k,F)=>{let I=k===0?M(F):g(F),O=N[k].get(F),q;if(O===void 0){let K=t(I);O=[K.stat,K.key],q=K.scores}let[$,_]=O,P=x.get(_);return P!==void 0?x.delete(_):(P=n(q??t(I).scores),x.size>=v&&x.delete(x.keys().next().value)),x.set(_,P),[$,P]},R=k=>{let[F,I]=E(0,k);return o(I,F)},T=k=>{let[F,I]=E(1,k);return s(I,F)},A=-1/0,S=0;if(w){let k=R(0);if(!(k>p)){let F=k,I=1;for(;I<=u-1;I++){let O=R(I);if(O>p)break;F=O}A=e[I-1],S=F}}let C=1/0,L=0;if(d){let k=T(0);if(!(k>p)){let F=k,I=u-1;for(;I>=1;I--){let O=T(u-I);if(O>p)break;F=O}C=e[I],L=F}}return{confInt:[A,C],confLevel:1-(S+L),estimate:oi(e),estimateNames:l}}function Yp(e,t){let n=e.map(r=>r-t);if(n.some(r=>Number.isNaN(r)))throw new Error("cannot compute the exact confidence interval: the shifted data have undefined values (infinite observations)");return Number.isFinite(t)&&n.some((r,o)=>!Number.isFinite(r)&&Number.isFinite(e[o]))&&(n=e.map(r=>r/2-t/2)),n}function Gp(e,t){let n=e.length,r=s=>{let i=Yp(e,s),{ranks:c,key:a}=bf(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 vf(gf(e),r,Ja,s=>Is(Za(s),"interval"),(s,i)=>vr(s,i-.25),nr,{mean:o/2,sd:Math.sqrt(n*(n+1)*(2*n+1)/24)},n,t,"(pseudo)median")}function Xp(e,t){let{alternative:n,confLevel:r,tolRoot:o}=t,s=t.correct>=0,i=e.length;if(i===0)return lf(n,NaN);let c=an(e),a=Nt(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"),lf(n,en(c,a),"midrange");let l=(v,M)=>{let[,g,N]=pf(e.map(x=>x-v).filter(x=>x!==0));return(g-ks(g,n,M))/N},f=l(c,s),m=l(a,s),u=v=>-le(v),h=v=>n==="greater"?!(f<u(v)):n==="less"?!(m>le(v/2)):!(f<u(v/2))&&!(m>le(v/2)),p=1-r,w=!1;for(;!h(p)&&p<1;)p*=2,w=!0;w&&t.warnings.add("requested conf.level not achievable"),df(o);let d=v=>Bt(M=>l(M,s)-v,c,a,{tol:o,fLower:f-v,fUpper:m-v}),b;if(p>=1){let v=oi([...e].sort(si));b=ai(n,v,v)}else n==="greater"?b=[d(u(p)),1/0]:n==="less"?b=[-1/0,d(le(p))]:b=[d(u(p/2)),d(le(p/2))];let y=Bt(v=>l(v,!1),c,a,{tol:o,fLower:l(c,!1),fUpper:l(a,!1)});return{confInt:b,confLevel:w?1-Math.min(1,p):r,estimate:y,estimateNames:"(pseudo)median"}}function Nf(e,t){let n=e.length,r=n-t,o=0;for(let a=0;a<t;a++)o+=e[a];let s=o-t*(t+1)/2,i=0;for(let a of bn(e))i+=a*a*a-a;let c=Math.sqrt(t*r/12*(n+1-i/(n*(n-1))));return[s,s-t*r/2,c,i>0]}function Qp(e,t){let[n,r,o]=Nf(vt([...e,...t]),e.length);return[n,r,o]}function Zp(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 Jp(e,t,n){return yf(e,t,n).ranks}function yf(e,t,n){let r=e.map(o=>o-n);if(r.some(o=>Number.isNaN(o))){let o=r.map((a,l)=>[a,l]).filter(([a])=>!Number.isNaN(a)),s=vt([...o.map(([a])=>a),...t]),i=new Array(e.length+t.length);o.forEach(([,a],l)=>i[a]=s[l]),t.forEach((a,l)=>i[e.length+l]=s[o.length+l]);let c=o.length+t.length;return r.forEach((a,l)=>{Number.isNaN(a)&&(i[l]=++c)}),{ranks:i,key:Qa(i)}}if(Number.isFinite(n)&&r.some((o,s)=>!Number.isFinite(o)&&Number.isFinite(e[s]))){let o=Zp(e.map(s=>s/2-n/2),t);return{ranks:o,key:Qa(o)}}return bf([...r,...t])}function ed(e,t,n,r){let{alternative:o,mu:s}=r,i=e.length,c=t.length,a=n??(i<50&&c<50),l=Jp(e,t,s),[f,m,u,h]=Nf(l,i),p,w;if(a)if(w="Wilcoxon rank sum exact test",!h)p=ff(()=>o==="less"?$r(f,i,c,!0,!1):o==="greater"?$r(f-1,i,c,!1,!1):Math.min(2*$r(f,i,c,!0,!1),2*$r(f-1,i,c,!1,!1),1));else{Is(ei(l,i),"distribution");let y=ti(l,i);o==="less"?p=nr(y,f):o==="greater"?p=vr(y,f-.25):p=Math.min(2*nr(y,f),2*vr(y,f-.25),1)}else{w=`Wilcoxon rank sum test${r.correct>=0?" with continuity correction":""}`;let y=h?Math.min(r.correct,0):r.correct,v=l.every(g=>g===l[0]),M=(m-ks(m,o,y>=0))/u;p=v?1:uf(M,o,y>=1?Bp(M,i,c,y):0)}let d={method:w,alternative:o,statistic:f,statisticName:"W",pValue:p,nullValue:s,nullValueName:"location shift",warnings:r.warnings.list};if(!r.confInt)return d;let b=a?h?nd(e,t,r):ff(()=>td(e,t,r)):rd(e,t,r);return{...d,...b}}function ff(e){try{return e()}catch(t){throw t instanceof RangeError&&t.message.startsWith("wilcox:")?new RangeError(`${t.message}: use exact: false`):t}}function Mf(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(si)}function td(e,t,n){let r=e.length,o=t.length;return wf(Mf(e,t,!1),s=>$r(s,r,o,!0,!1),s=>hl(s,r,o,!0,!1),n,"difference in location")}function nd(e,t,n){let r=e.length,o=t.length,s=i=>{let{ranks:c,key:a}=yf(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 vf(Mf(e,t,!0),s,i=>ti(i,r),i=>Is(ei(i,r),"interval"),(i,c)=>vr(i,c-.25),nr,{mean:r*o/2,sd:Math.sqrt(r*o*(r+o+1)/12)},r+o,n,"difference in location")}function rd(e,t,n){let{alternative:r,confLevel:o,tolRoot:s}=n,i=n.correct>=0,c=an(e)-Nt(t),a=Nt(e)-an(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[,w,d]=Qp(e.map(b=>b-h),t);return(w-ks(w,r,p))/d},f=(h,p)=>{let w=l(c,p)-h,d=l(a,p)-h;if(Number.isNaN(w)||Number.isNaN(d))throw new Error("cannot compute the confidence interval because all observations are tied");return w<=0?c:d>=0?a:(df(s),Bt(b=>l(b,p)-h,c,a,{tol:s,fLower:w,fUpper:d}))},m=1-o,u;return r==="greater"?u=[f(-le(m),i),1/0]:r==="less"?u=[-1/0,f(le(m),i)]:u=[f(-le(m/2),i),f(le(m/2),i)],{confInt:u,confLevel:o,estimate:f(0,!1),estimateNames:"difference in location"}}var od=["method","n"],Ef=["holm","hochberg","hommel","bonferroni","BH","BY","fdr","none"];function sd(e){if(Ef.includes(e))return e;let t=Ef.filter(n=>n.startsWith(e));if(e.length===0||t.length!==1)throw new RangeError(`invalid method "${e}"`);return t[0]}function Br(e,t={}){B(t,od,"pAdjust");let n=typeof t=="string"?{method:t}:t,r=sd(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=Rf(a,r,c),f=o.slice();return s.forEach((m,u)=>f[m]=l[u]),f}function _s(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 Rf(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=_s(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=_s(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=Dn(),u=Math.floor(n);if(u<=1e6){for(let h=1;h<=u;h++)pn(m,1/h,u);s=Pn(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=_s(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?Rf(e,"hochberg",n):ad(e,n)}}var xf=2**16;function ad(e,t){if(!Number.isInteger(t))throw new RangeError("n must be an integer for the hommel method");if(t>xf)throw new RangeError(`n must be at most ${xf} for the hommel method (O(n^2) work)`);let n=e.length,r=[...e,...new Array(t-n).fill(1)],o=_s(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 Tf=["p","alternative","confLevel"],id=1+1e-7;function ii(e,t,n){let r=e-1;return e===0||e-r===1&&Ot(t,-r)[1]===0?On(r,t,n,!1,!1):On(e,t,n,!1,!1)+hn(e,t,n,!1)}function cd(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=hn(e,t,n,!1)*id,s=a=>hn(a,t,n,!1)<=o;if(e<r){let a=os(s,Math.ceil(r),t),l=a>t?0:ii(a,t,n);return Math.min(1,On(e,t,n,!0,!1)+l)}let i=ss(s,0,Math.floor(r)),c=i<0?0:On(i,t,n,!0,!1);return Math.min(1,ii(e,t,n)+c)}function ld(e,t,n={}){B(t,Tf,"binomTest"),B(n,Tf,"binomTest");let r,o;if(typeof e!="number"){if(e.length!==2)throw new RangeError("incorrect length of 'x'");if(t!==void 0&&typeof t!="number"&&(n=t),!Nn(e[0])||!Nn(e[1]))throw new RangeError("'x' must be nonnegative and integer");if(r=Math.round(e[0]),o=Math.round(e[0])+Math.round(e[1]),o===0||o===1/0)throw new RangeError("'n' must be a positive integer >= 'x'")}else{if(typeof t!="number")throw new TypeError("'n' is required when 'x' is a single number");if(!Nn(e))throw new RangeError("'x' must be nonnegative and integer");if(r=Math.round(e),!(Nn(t)&&Math.round(t)>=r&&t>0))throw new RangeError("'n' must be a positive integer >= 'x'");o=Math.round(t)}return ci(r,o,n)}function ci(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=Ze(n.alternative),s=n.confLevel??.95;_t(s);let i;o==="less"?i=On(e,t,r,!0,!1):o==="greater"?i=ii(e,t,r):i=cd(e,t,r);let c=f=>e===0?0:Gn(f,e,t-e+1,!0,!1),a=f=>e===t?1:f<1e-8?Gn(f,e+1,t-e,!1,!1):Gn(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 fd=["T","r","alternative","confLevel"],md=1+1e-7;function ud(e,t){if(t===0)return e===0?1:0;if(e===t)return 1;let n=as(e,t,!1)*md,r=i=>as(i,t,!1)<=n,o=Math.floor(t);if(e<t){let i=os(r,Math.max(e+1,o),1/0);return Math.min(1,Pr(e,t,!0,!1)+li(i,t))}let s=ss(r,0,Math.min(e-1,o));return Math.min(1,Pr(s,t,!0,!1)+li(e,t))}var _o=2**-1022;function li(e,t){return e-(e-1)===1?Pr(e-1,t,!1,!1):Pr(e,t,!1,!1)+as(e,t,!1)}function Nr(e){let t=e<9332636185032189e-317?600:0,n=Math.floor(Math.log2(e*2**t));return[e*2**t*2**-n,n-t]}function hd(e,t,n,r){let o=n+t*r,s=e*t*n/o;if(Number.isFinite(s)&&Number.isFinite(o)&&o>=_o&&(s!==0||e===0||t===0))return s;let i=e*t,c=1+t*(r/n);if(e===0||t===0)return 0;if(Number.isFinite(i)&&Number.isFinite(c))return i/c;let[a,l]=Nr(e),[f,m]=Nr(t),[u,h]=Nr(n),[p,w]=Nr(r),d=f*p,b=h-m-w,y=b>=0?u/(u+V(d,-b)):u/(V(u,b)+d);return V(a*f*y,l+m+Math.min(b,0))}function pd(e,t,n,r){let o=e/n,s=t/r,i=o/s;return t===0||Number.isFinite(o)&&Number.isFinite(s)&&s>=_o&&(o===0||o>=_o)?i:e===0?0:e/t*(r/n)}function dd(e,t={}){B(t,fd,"poissonTest");let n=typeof e=="number"?[e]:Array.from(e);if(n.length<1||n.length>2)throw new RangeError("incorrect length of 'x'");if(!n.every(Nn))throw new RangeError("'x' must be finite, nonnegative, and integer");let r=n.map(Math.round),o=r.length,s=t.T??1,i=typeof s=="number"?new Array(o).fill(s):Array.from(s);if(i.length!==o)throw new RangeError("'x' and 'T' have incompatible length");if(!i.every(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=Ze(t.alternative),l=t.confLevel??.95;if(o===2?_t(l):cn(l),o===2){let[b,y]=r,[v,M]=i,g=b+y;if(g===1/0)throw new RangeError("'x' must be finite, nonnegative, and integer");let N=c*v,x=N+M,E=Number.isFinite(x)&&x>=_o&&(N===0||N>=_o)?N/x:1/(1+M/v/c),R=ci(Math.round(b),Math.round(b)+Math.round(y),{p:E,alternative:a,confLevel:l}),T=M/v,A=S=>{if(S===0)return 0;let C=S/(1-S);if(Number.isFinite(T)||C===1/0)return C*T;let[L,k]=Nr(C),[F,I]=Nr(v),[O,q]=Nr(M);return V(L*O/F,k+q-I)};return{method:"Comparison of Poisson rates",alternative:a,statistic:b,statisticName:"count1",parameter:hd(g,c,v,M),parameterName:"expected count1",pValue:R.pValue,confInt:[A(R.confInt[0]),A(R.confInt[1])],confLevel:l,estimate:pd(b,y,v,M),estimateNames:"rate ratio",nullValue:c,nullValueName:"rate ratio",warnings:[]}}let f=r[0],m=i[0],u=c*m,h;a==="less"?h=Pr(f,u,!0,!1):a==="greater"?h=li(f,u):h=ud(f,u);let p=b=>f===0?0:rs(b,f,1,!0,!1),w=b=>b<1e-8?rs(b,f+1,1,!1,!1):rs(1-b,f+1,1,!0,!1),d;if(a==="less")d=[0,w(1-l)];else if(a==="greater")d=[p(1-l),1/0];else{let b=(1-l)/2;d=[p(b),w(b)]}return{method:"Exact Poisson test",alternative:a,statistic:f,statisticName:"number of events",parameter:m,parameterName:"time base",pValue:h,confInt:[d[0]/m,d[1]/m],confLevel:l,estimate:f/m,estimateNames:"event rate",nullValue:c,nullValueName:"event rate",warnings:[]}}var Af=["p","alternative","confLevel","correct"],gd="Chi-squared approximation may be incorrect";function fi(e,t,n={}){wt(t)&&B(t,Af,"propTest"),B(n,Af,"propTest");let r,o,s=n;if(typeof e!="number"&&e.length>0&&Array.isArray(e[0])){let A=e;if(A.some(S=>S.length!==2))throw new RangeError("'x' must have 2 columns");r=A.map(S=>S[0]),o=A.map(S=>S[0]+S[1]),t!==void 0&&typeof t=="object"&&!Array.isArray(t)&&(s=t)}else{if(t===void 0||typeof t=="object"&&!Array.isArray(t))throw new RangeError("'n' must be given");r=typeof e=="number"?[e]:Array.from(e),o=typeof t=="number"?[t]:Array.from(t)}if(r.length!==o.length)throw new RangeError("'x' and 'n' must have the same length");let i=r.length,c=[];for(let A=0;A<i;A++)!Number.isNaN(r[A])&&!Number.isNaN(o[A])&&c.push(A);let a=c.length;if(a<1)throw new RangeError("not enough data");if(r=c.map(A=>r[A]),o=c.map(A=>o[A]),o.some(A=>!(A>0)))throw new RangeError("elements of 'n' must be positive");if(o.some(A=>A===1/0))throw new RangeError("elements of 'n' must be finite");if(r.some(A=>!(A>=0)))throw new RangeError("elements of 'x' must be nonnegative");if(r.some((A,S)=>A>o[S]))throw new RangeError("elements of 'x' must not be greater than those of 'n'");let l;if(s.p!==void 0){let A=typeof s.p=="number"?[s.p]:Array.from(s.p);if(A.length!==i)throw new RangeError("'p' must have the same length as 'x' and 'n'");if(l=c.map(S=>A[S]),l.some(S=>!(S>0&&S<1)))throw new RangeError("elements of 'p' must be in (0,1)")}let f=l!==void 0;a===1&&l===void 0&&(l=[.5]);let m=s.confLevel??.95;_t(m);let u=Ze(s.alternative);(a>2||a===2&&f)&&(u="two.sided");let h=r.map((A,S)=>A/o[S]),p=a===2?h[0]-h[1]:0,w=a===2?1/o[0]+1/o[1]:0,d=(s.correct??!0)&&a<=2?.5:0,b=!1;if(d>0&&a===1){let A=Math.abs(r[0]-o[0]*l[0]);A<=d&&([d,b]=[A,!0])}else if(d>0&&a===2&&!f){let A=Math.abs(p)/w;A<=d&&([d,b]=[A,!0])}let y=wd(r,o),v=o.some((A,S)=>{let C=l?.[S]??y;return A*C<5||A*(1-C)<5}),M=0;if(!b)for(let A=0;A<a;A++){let S=l?.[A]??y,C=o[A]*S,L=o[A]*(1-S);M+=Vt(Math.abs(r[A]-C)-d,C),M+=Vt(Math.abs(o[A]-r[A]-L)-d,L)}let g=l!==void 0?a:a-1,N;if(u==="two.sided")N=bt(M,g/2,2,!1,!1);else{let A=Math.sign(a===1?h[0]-l[0]:p);N=ae(A*Math.sqrt(M),{lowerTail:u==="less"})}let x;if(a===1||a===2&&!f){let A=(1-m)/2,S=u==="two.sided"&&A<1e-8?le(A,{lowerTail:!1}):le(u==="two.sided"?(1+m)/2:m);if(a===1){let C=o[0],L=S*S,k=($,_)=>($+L/(2*C)+_*S*Math.sqrt($*(1-$)/C+L/(4*C*C)))/(1+L/C),F=h[0]+d/C,I=h[0]-d/C,O=Math.min(1,Math.max(0,F>=1?1:k(F,1))),q=Math.min(1,Math.max(0,I<=0?0:k(I,-1)));x=u==="two.sided"?[q,O]:u==="greater"?[q,1]:[0,O]}else{let C=Math.sqrt(h.reduce((I,O,q)=>I+O*(1-O)/o[q],0)),L=S*C+d*w,k=Math.max(-1,p-L),F=Math.min(1,p+L);x=u==="two.sided"?[k,F]:u==="greater"?[k,1]:[-1,F]}}let E=a===1?"p":c.map(A=>`prop ${A+1}`),T={method:`${a===1?"1-sample proportions test":`${a}-sample test for ${f?"given proportions":"equality of proportions"}`} ${d!==0?"with":"without"} continuity correction`,alternative:u,statistic:M,statisticName:"X-squared",parameter:g,parameterName:"df",pValue:N,estimate:a===1?h[0]:h,estimateNames:E,warnings:v?[gd]:[]};return l!==void 0&&(T.nullValue=a===1?l[0]:l,T.nullValueName=E),x!==void 0&&(T.confInt=x,T.confLevel=m),Q("propTest",T)}function wd(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+V(s,-r),0)/t.reduce((o,s)=>o+V(s,-r),0)}var bd=["method","confLevel","sides"],vd=["wilson","wald","wilsoncc","agresti-coull","jeffreys","clopper-pearson","midp"],Nd=["two.sided","left","right"];function Lo(e,t){let n=Ae(t,Nd),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{...mi(s,o),sides:n}}function mi(e,t=1-e){let n=e/2<1e-8,r=n?le(e/2,{lowerTail:!1}):le(1-e/2),o=n?Fn(e,1,{lowerTail:!1}):Fn(t,1);return{sides:"two.sided",level:t,alpha:e,kappa:r,chisq:o}}function Kr(e,t){if(typeof e!="number")throw new TypeError(`'${t}' must be a number`);if(!Nn(e))throw new RangeError(`'${t}' must be a nonnegative integer`);return Math.round(e)+0}function jn(e,t,n,r){let o=Kr(e,n),s=Kr(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 Cs(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 Ls(e,t,n,r){return r?e/2<1e-8?Gn(e/2,t,n,!1,!1):Gn(1-e/2,t,n,!0,!1):Gn(e/2,t,n,!0,!1)}function Sf(e,t,n,r){let o=n/2,s=r?f=>{let m=Math.exp(f);return .5*hn(e,t,m,!1)+On(e-1,t,m,!0,!1)-o}:f=>{let m=Math.exp(f);return .5*hn(e,t,m,!1)+On(e,t,m,!1,!1)-o},i=r?-1:1,c=Math.log(e/t),a=i*s(c);if(a===0)return e/t;if(a>0){let f=c,m=a,u=1;for(;;){let h=c-u;if(h<-745)return 0;let p=i*s(h);if(p<=0)return Math.exp(p===0?h:Bt(w=>i*s(w),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(Bt(f=>i*s(f),c,0,{tol:2**-60,fLower:a,fUpper:l}))}function ui(e,t,n={}){B(n,bd,"binomCI");let r=Ae(n.method,vd),o=Lo(n.confLevel,n.sides),[s,i]=jn(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]=Cs(s,i,a);break;case"wilsoncc":{let h=a*a,p=2*(i+h),w=2*s+h-1,d=a*Math.sqrt(h-2-1/i+4*l*(i*f+1));m=l===0?0:w-d<w/2?(2*s-1)/(2*i)*(2*s-1)/(w+d):(w-d)/p,u=l===1?1:(2*s+h+1+a*Math.sqrt(h+2-1/i+4*l*(i*f-1)))/p;break}case"agresti-coull":{let h=a*a,p=i+h,w=(s+h/2)/p,d=a*Math.sqrt(w*(1-w)/p);[m,u]=[w-d,w+d];break}case"jeffreys":m=s===0?0:Ls(c,s+.5,i-s+.5,!1),u=s===i?1:Ls(c,s+.5,i-s+.5,!0);break;case"clopper-pearson":m=s===0?0:Ls(c,s,i-s+1,!1),u=s===i?1:Ls(c,s+1,i-s,!0);break;case"midp":m=s===0?0:s===i?Math.exp(Math.log(c)/i):Sf(s,i,c,!1),u=s===i?1:s===0?-Math.expm1(Math.log(c)/i):Sf(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),Q("binomCI",{estimate:l,confInt:[m,u],confLevel:n.confLevel??.95,method:r,sides:o.sides,warnings:[]})}function Co(e){return e===1/0?1:e/(1+e)}function Fo(e,t){return e===0?0:t>0?e*e/t:1/0}function yd(e,t,n,r){return e/t+n/r>1}function Md(e,t,n,r){return Os(e,t,n,r,0)}function di(e,t,n,r,o){if(yd(e,t,n,r)){let[p,w,d,b]=di(t-e,t,r-n,r,-o);return[w,p,b,d]}let s=o<=0,[i,c,a,l]=s?[e,t,n,r]:[n,r,e,t],f=Math.abs(o),m=1-f,u=0;if(m>0){let p=b=>{let y=0;return i>0&&(y+=i/b),i<c&&(y-=(c-i)/(1-b)),a>0&&(y+=a/(b+f)),a<l&&(y-=(l-a)/(m-b)),y},w=b=>{let y=0;return i>0&&(y-=i/(b*b)),i<c&&(y-=(c-i)/((1-b)*(1-b))),a>0&&(y-=a/((b+f)*(b+f))),a<l&&(y-=(l-a)/((m-b)*(m-b))),y};if(p(0)<=0)u=0;else if(p(m)>=0)u=m;else{let b=0,y=m,v=Ed(e,t,n,r,o);u=s?v:v-o,u>b&&u<y||(u=y/2);for(let M=0;M<2e3;M++){let g=p(u);if(g===0)break;g>0?b=u:y=u;let N=u-g/w(u);if(N>b&&N<y||(N=b===0?y/2:y>2*b?Math.sqrt(b*y):b+(y-b)/2),!(N>b&&N<y))break;let x=Math.abs(N-u);if(u=N,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 Ed(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 w=(Math.PI+Math.acos(Math.max(-1,Math.min(1,h/(p*p*p)))))/3;return 2*p*Math.cos(w)-u}function gi(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]=di(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 xd(e,t,n,r,o,s,i){let c=Oe(Oe(lt([e,0],t),lt([n,0],r)),[o,0]),[a,l,f,m]=di(e,t,n,r,o),u=he(lt(Jt(a,l),t),lt(Jt(f,m),r));return s==="mn"&&(u=lt(At(u,t+r),t+r-1)),Oe(_e(c,c),At(u,i))[0]}function Os(e,t,n,r,o){return Oe(Oe(lt([e,0],t),lt([n,0],r)),[o,0])[0]}function Rd(e,t,n,r,o,s){return Fo(Os(e,t,n,r,o),gi(e,t,n,r,o,s))}var Fs=new Float64Array(1),hi=new BigInt64Array(Fs.buffer);function qf(e){return Fs[0]=Math.abs(e),e<0?-hi[0]:hi[0]}function rr(e){return hi[0]=e<0n?-e:e,e<0n?-Fs[0]:Fs[0]}function pi(e,t,n,r,o){let s=e(t);if(s===0||Number.isNaN(s))return t;let i=s<0===o,c=qf(i?r:n),a=qf(t),l=s,f=a,m=s,u=1n;for(;;){if(f=i?a+u:a-u,(i?f>=c:f<=c)&&(f=c),m=e(rr(f)),m===0)return rr(f);if(m<0!=l<0||f===c)break;if(a=f,l=m,u*=2n,u>1n<<40n)return rr(a)}if(m<0==l<0||Number.isNaN(m))return rr(a);for(;f-a>1n||a-f>1n;){let h=(a+f)/2n,p=e(rr(h));if(p===0)return rr(h);p<0==l<0?(a=h,l=p):(f=h,m=p)}return Math.abs(m)<Math.abs(l)?rr(f):rr(a)}function If(e,t,n,r,o,s){let i=Md(e,t,n,r);if(o===0)return[i,i];let c=Co(o),a=p=>(p===i?0:Co(Rd(e,t,n,r,p,s)))-c,l=(p,w,d,b)=>{if(d===0)return p;if(b===0)return w;let y=Bt(a,p,w,{tol:1e-300,fLower:d,fUpper:b});return s==="wald"?y:pi(v=>xd(e,t,n,r,v,s,o),y,p,w,d<0)},f=a(-1),m=a(1),u=i<=-1||f<=0?-1:l(-1,i,f,-c),h=i>=1||m<=0?1:l(i,1,-c,m);return[u,h]}function kf(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?Bt(e,r,i,{tol:2**-60,fLower:o,fUpper:c}):Bt(e,i,r,{tol:2**-60,fLower:c,fUpper:o});return Math.exp(a)}r=i,o=c,s*=2}}function Td(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 Ad(e,t,n,r,o){let[s,i]=Td(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=N=>c===0?[N,0]:c===2?Ot(1,-N):lt(Ot(1,-N),o),f=c===3?1/o:1,m=c===0?Math.min(1,1/o):1,u=c===2?o>1?lt(Ot(o,-1),o)[0]:0:c===3&&o<1?Ot(1,-o)[0]:0,h=N=>{let x=l(N),E=At(x,o);return[E[0],Oe([1,0],E)[0],x[0],Oe([1,0],x)[0]]},p=e+n,w=c<2?1:-1,d=N=>{let[,x,E,R]=h(N),T=0,A=0;return p>0&&(T+=E>0?p/E:1/0,A-=p/(E*E)),e<t&&(T-=x>0?o*(t-e)/x:1/0,A-=o*(t-e)/x*(o/x)),n<r&&(T-=R>0?(r-n)/R:1/0,A-=(r-n)/(R*R)),[w*T,A*f]},b;if(!(m>u))b=u;else if(!(d(u)[0]>0))b=u;else if(!(d(m)[0]<0))b=m;else{let N=u,x=m;b=a,b>N&&b<x||(b=N+(x-N)/2);for(let E=0;E<2e3;E++){let[R,T]=d(b);if(R===0)break;R>0?N=b:x=b;let A=b-R/T;if(A>N&&A<x||(A=N<=0?x/2:x>2*N?Math.sqrt(N*x):N+(x-N)/2),!(A>N&&A<x))break;let S=Math.abs(A-b);if(b=A,S<=2**-53*b)break}}let[y,v,M,g]=h(b);return[Math.min(1,Math.max(0,y)),Math.min(1,Math.max(0,v)),Math.min(1,Math.max(0,M)),Math.min(1,Math.max(0,g))]}function Sd(e,t,n,r,o){let[s,i,c,a]=Ad(e,t,n,r,o),l=(f,m,u,h)=>u>.5?m*h-(m-f):f-m*u;return Fo(l(e,t,s,i),t*s*i)+Fo(l(n,r,c,a),r*c*a)}function _f(e,t,n,r,o){let s=e/t/(n/r);return Cf(s,o,i=>Sd(e,t,n,r,i))}function qd(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 kd(e,t,n,r,o){let[s,i]=qd(e,t,n,r,o),c=e-t*s;return Fo(c,t*s*(1-s))+Fo(c,r*i*(1-i))}function Lf(e,t,n,r,o){let s=e*(r-n)/((t-e)*n);return Cf(s,o,i=>kd(e,t,n,r,i))}function Cf(e,t,n){if(t===0)return[e,e];let r=Co(t),o=l=>n(l)-t,s=l=>l>0&&l<e?pi(o,l,Number.MIN_VALUE,e,!1):l,i=l=>l>e&&l<1/0?pi(o,l,e,Number.MAX_VALUE,!0):l;if(e===0||e===1/0){let l=u=>Co(n(u))-r,f=l(1),m=f===0?1:tr(l,e===0?f>0:f<0);return e===0?[0,i(m)]:[s(m),1/0]}let c=Math.log(e),a=l=>(l===c?0:Co(n(Math.exp(l))))-r;return[s(kf(a,c,-1)),i(kf(a,c,1))]}var Id=["method","confLevel","sides"],_d=["method","confLevel","sides"],Ld=["method","confLevel"],Cd=["ac","wald","score","mn","mee"];function wi(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,w=i*Math.sqrt(u*(1-u)/(t+2)+h*(1-h)/(r+2));[f,m]=[Math.max(-1,p-w),Math.min(1,p+w)];break}case"score":{let[u,h]=Cs(e,t,i),[p,w]=Cs(n,r,i);f=l-i*Math.sqrt(u*(1-u)/t+w*(1-w)/r),m=l+i*Math.sqrt(h*(1-h)/t+p*(1-p)/r);break}case"mn":case"mee":[f,m]=If(e,t,n,r,s.chisq,o);break}return[Math.min(f,m),Math.max(f,m)]}function Fd(e,t,n,r,o={}){B(o,Id,"binomDiffCI");let s=Ae(o.method,Cd),i=Lo(o.confLevel,o.sides),[c,a]=jn(e,t,"x1","n1"),[l,f]=jn(n,r,"x2","n2"),m=c/a-l/f,[u,h]=wi(c,a,l,f,s,i);return i.sides==="left"?h=1:i.sides==="right"&&(u=-1),Q("binomDiffCI",{estimate:m,confInt:[u,h],confLevel:o.confLevel??.95,method:s,sides:i.sides,warnings:[]})}var Od=["katz.log","koopman"];function Ds(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 Dd(e,t,n,r,o={}){B(o,_d,"binomRatioCI");let s=Ae(o.method,Od),i=Lo(o.confLevel,o.sides),[c,a]=jn(e,t,"x1","n1"),[l,f]=jn(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,Ds(.5,a,l,f,m)[1]]:l===0?[h,p]=[Ds(c,a,.5,f,m)[0],1/0]:c===a&&l===f?[h,p]=Ds(a-.5,a,f-.5,f,m):[h,p]=Ds(c,a,l,f,m):[h,p]=_f(c,a,l,f,i.chisq),i.sides==="left"?p=1/0:i.sides==="right"&&(h=0),Q("binomRatioCI",{estimate:u,confInt:[h,p],confLevel:o.confLevel??.95,method:s,sides:i.sides,warnings:[]})}var Pd=["midp","fisher","wald","score"],Vd={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"},$d=2**23,zd=2**20;function Ff(e,t,n){let r=[0,0];for(let o=Math.max(0,t);o<=Math.min(n,e.length-1);o++)e[o]!==0&&(r=he(r,[e[o],0]));return r[0]}function jd(e,t={}){B(t,Ld,"oddsRatio");let n=Ae(t.method,Pd);if(e.length!==2||e.some(A=>A.length!==2))throw new TypeError("'table' must be a 2 x 2 table");let r=t.confLevel??.95,{alpha:o,kappa:s,chisq:i}=Lo(r,void 0),c=e.map((A,S)=>Array.from(A,(C,L)=>Kr(C,`table[${S}][${L}]`))),[[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),w=p-h+1;if(n==="midp"||n==="fisher"){if(w>$d)throw new RangeError(`the exact distribution of the 2 x 2 table has ${w} values, more than 2^23: use method "wald" or "score"`);if(n==="fisher"&&Math.max(a,l,f,m)>2147483647)throw new RangeError(`'table' has entries too large for the conditional MLE (above 2^31 - 1): use method "wald" or "score"`)}let d,b,y;try{d=sn(a-1,a+f,l+m,a+l,!0,!1)+.5*vn(a,a+f,l+m,a+l,!1),b=sn(a,a+f,l+m,a+l,!1,!1)+.5*vn(a,a+f,l+m,a+l,!1),y=w<=zd?Ga(new Wr(a+f,l+m,a+l),a,1):sf(a,a+f,l+m,a+l)}catch(A){throw A instanceof RangeError?new RangeError(`the exact p-values of the table are beyond reach: ${A.message}`):A}let v=2*Math.min(d,b),M=[a+l,f+m],g=[a+f,l+m],N=0,x=!1;for(let A=0;A<2;A++)for(let S=0;S<2;S++){let C=M[S]*g[A]/u;C<5&&(x=!0);let L=Math.abs(c[S][A]-C);N+=L*L/C}let E=bt(N,.5,2,!1,!1),R,T;switch(n){case"wald":{R=a*m/(l*f);let A=s*Math.sqrt(1/a+1/l+1/f+1/m);T=[Math.exp(Math.log(R)-A),Math.exp(Math.log(R)+A)];break}case"fisher":{let A=rt(()=>ko(c,{confLevel:r}));R=A.estimate,T=A.confInt;break}case"score":{R=a*m/(l*f),T=(a===0||m===0)&&(l===0||f===0)?[0,1/0]:Lf(a,a+l,f,f+m,i);break}case"midp":{let A=new Wr(a+f,l+m,a+l),S=F=>{if(F===1)return[d,b];let I=A.density(F),O=a-h,q=.5*I[O];return[Ff(I,0,O-1)+q,Ff(I,O+1,I.length-1)+q]},C=(F,I)=>{let O=F(1);return O===0?1:tr(F,I?O>0:O<0)};R=a===h?0:a===p?1/0:C(F=>S(F)[0]-.5,!1);let L=a===h?0:C(F=>S(F)[1]-o/2,!0),k=a===p?1/0:C(F=>S(F)[0]-o/2,!1);T=[L,k];break}}return Q("oddsRatio",{estimate:R,confInt:T,confLevel:r,method:Vd[n],pValue:{midp:v,fisher:y,chiSquare:E},warnings:x?["Chi-squared approximation may be incorrect"]:[]})}var Wd=["epsilon","paired","varEqual","confLevel","alpha"],Bd=["margin","method","alpha"];function Of(e,t){if(!(typeof e=="number"&&e>0&&e<.5))throw new RangeError(`'${t}' must be a single number in (0, 0.5)`)}function Kd(e,t,n={}){B(n,Wd,"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)Of(n.alpha,"alpha"),o=1-n.alpha,s=n.alpha;else{if(o=n.confLevel??.95,_t(o),!(o>.5))throw new RangeError("'confLevel' must be greater than 0.5");s=1-o}if(t===void 0)throw new TypeError("'y' is required");let i=n.paired??!1,c=n.varEqual??!1,a=rt(()=>yn(e,t,{paired:i,varEqual:c})),l=a.stdErr,f=a.parameter,m=typeof a.estimate=="number"?a.estimate:a.estimate[0]-a.estimate[1],u=dt((r-Math.abs(m))/l,f,!1,!1),h=s<1e-8?gt(s,f,!1,!1):gt(o,f,!0,!1),p=[m-h*l,m+h*l];return Q("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 Ud=["mn","mee","wald"];function Hd(e,t,n,r,o={}){B(o,Bd,"binomDiffEquivalenceTest");let s=Ae(o.method,Ud),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;Of(c,"alpha");let[a,l]=jn(e,t,"x1","n1"),[f,m]=jn(n,r,"x2","n2"),u=a/l-f/m,h=M=>Os(a,l,f,m,M)/Math.sqrt(gi(a,l,f,m,M,s)),p=h(-i),w=h(i),d=[ae(p,{lowerTail:!1}),ae(w)],b=Math.max(d[0],d[1]),y=1-2*c,v=wi(a,l,f,m,s,mi(2*c,y));return Q("binomDiffEquivalenceTest",{method:s,estimate:u,margin:i,alpha:c,statistic:[p,w],statisticName:"z",pValues:d,pValue:b,result:d[0]<c&&d[1]<c?"rejected":"not rejected",confInt:v,confLevel:y,warnings:[]})}var Kt=new WeakMap;function Vs(e){let t=(n,r)=>n.weights===void 0?Array.from(r):r.filter((o,s)=>n.weights[s]!==0);return qe(e.flatMap(n=>[t(n,n.fittedValues),t(n,n.residuals)])).k}function $s(e,t){return e.map(n=>{if(n.rss>=Yd&&n.rss<1/0)return V(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=V(o,-t);r+=i*c*c}),r})}var Yd=2**-1022;function vi(e,t){return e.y.flatMap((n,r)=>(e.weights?.[r]??1)===0?[]:[[e.weights?.[r]??1,V(n,-t)]])}function zs(e){let t=Math.floor(Math.log2(e.reduce((n,[r])=>Math.max(n,r),0)));return t-(t%2+2)%2}function yr(e,t,n=0){let r=i=>e.reduce((c,[a,l])=>c+V(a,-i)*l*l,0),o=n,s=r(o);return Number.isFinite(s)||(o=zs(e),s=r(o)),!(V(t,-o)>=1e-24*s)}function Df(e){let t=Vs([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+=V(e.effects[s],-t)**2}return{terms:r,rdf:e.dfResidual,rss:$s([e],t)[0],k:t}}var Pf=(e,t,n)=>ht(e,t,n,!1,!1),Ni="essentially perfect fit for \u20181\u2019: summary may be unreliable";function yi(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 Mi=new WeakMap;function Gd(e){let t=Df(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*V(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 bi(e){let t=Gd(e),{names:n,rdf:r,k:o,perfect:s}=t,{rows:i,rss:c}=t;if(r>0){let w=vi(e,o);yr(w,c)&&(c=0,i=i.map(d=>yr(w,d.ss)?{...d,ss:0}:d))}let a=c/r,l=w=>V(w,2*o),f=i.map(({df:w,ss:d})=>{let b=d/w/a;return[w,l(d),l(d/w),b,Pf(b,w,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:w,ss:d})=>[w,d]);return p.push([r,c]),Kt.set(h,{colNames:["Df","Sum Sq"],rowNames:u,table:p}),h}function Xd(e){let t=e.map(f=>f.dfResidual),n=Vs(e),r=$s(e,n),o=f=>V(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*V(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,w]=u>0?[e[m-1],e[m]]:[e[m],e[m-1]],d=y=>y.filter((v,M)=>(p.weights?.[M]??1)!==0),b=u===0||!Ns(d(p.x),d(w.x))?NaN:h/u/i;return b<0&&(b=NaN),[t[m],o(r[m]),u,o(h),b,Number.isNaN(b)?NaN:Pf(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 Ps(...e){let t=[];for(let n of e)for(let r of n??[])t.includes(r)||t.push(r);return t}function Ei(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=bi(e),a={...c,warnings:Ps(e.warnings,c.warnings)},l=Kt.get(c);return l!==void 0&&Kt.set(a,l),Q("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?bi(e):Xd(o);return i.warnings=Ps(...n.map(c=>c.warnings),s,i.warnings),Q("anova",i,...n)}function xi(e){if(typeof e=="object"&&e!==null&&e.multistratum===!0){let i=e,c=Ps(i.warnings,Mi.get(i)),a={strata:i.strata.filter(f=>f.stratum!=="Error: (Intercept)").map(f=>({...f})),warnings:c},l=Kt.get(i);return Array.isArray(l)&&Kt.set(a,l.filter((f,m)=>i.strata[m].stratum!=="Error: (Intercept)")),Q("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=bi(t),r=Df(t),o=yi(t.fittedValues.map(i=>V(i,-r.k)),r.rss,r.rdf),s=Ps(t.warnings,n.warnings,o?[Ni]:[]);if(t.dfResidual>0){let i={...n,warnings:s},c=Kt.get(n);return c!==void 0&&Kt.set(i,c),Q("aovSummary",i,e)}return Q("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 Qd=["pooledSd","mu","paired","ci","alternative","adjust"],Zd=["pooledSd","mu","paired","ci","alternative"],Jd=["generalized","partial","ci","alternative"],e0=["partial","ci","alternative"],t0=["partial","ci","alternative"];function Ai(e){if(e!==null&&!(e>0&&e<1))throw new RangeError("ci must be a single numeric value between (0, 1)")}var n0="Missing values detected. NAs dropped.",r0=`'y' is numeric but has only 2 unique values.
3
- If this is a grouping variable, convert it to a factor.`,jf={tol:0,maxiter:5e3};function Mr(e){return e===1/0?1:Math.exp(ol(.5,(e-1)/2)-.5*Math.log(e/2))}function Vf(e,t,n){let r=e-1<e+1?1:Math.abs(e)*Number.EPSILON;return wr(o=>on(e,t,{ncp:o})-n,e-r,e+r,{...jf,extendInt:"downX"}).root}function Wf(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;Ai(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(Pt(f)).size===2&&m.push(r0),(l.some(Number.isNaN)||(f?.some(Number.isNaN)??!1))&&m.push(n0);let u=A=>A===1/0||A===-1/0;if(l.some(u)||(f?.some(u)??!1))throw new RangeError("the observations must be finite (NaN is dropped as missing)");let h,p,w,d,b,y;if(f===void 0||i){let A;if(f===void 0)({groups:[A],k:y}=qe([Pt(l)]));else{let[C,L]=Zn(l,f);({d:A,k:y}=is(C,L))}let S=A.length;if(S<2)throw new RangeError(f===void 0?"not enough observations: at least 2 are needed":"not enough complete pairs: at least 2 are needed");h=it(A),p=Math.sqrt(st(A)),w=p/Math.sqrt(S),d=1/S,b=S-1}else{let A=Dt([Pt(l),Pt(f)]);y=A.k;let[S,C]=A.n;if(S<2||C<2)throw new RangeError("not enough observations: at least 2 are needed in each group");h=A.mean[0]-A.mean[1];let L=Math.sqrt(A.variance[0]),k=Math.sqrt(A.variance[1]);o?(p=Math.sqrt(((S-1)*L**2+(C-1)*k**2)/(S+C-2)),w=p*Math.sqrt(1/S+1/C),d=1/S+1/C,b=S+C-2):(p=Math.sqrt((L**2+k**2)/2),d=2*(C*L**2+S*k**2)/(S*C*(L**2+k**2)),w=Math.sqrt(Math.sqrt(L**2/S)**2+Math.sqrt(k**2/C)**2),b=cs([L**2/S,k**2/C],[S-1,C-1]))}let v=r?Mr(b):1,M=V(s,-y),g=(h-M)/p*v,N=r?"Hedges_g":"Cohens_d";if(c===null)return{name:N,value:g,warnings:m};let x=a==="two.sided"?c:2*c-1,E=(h-M)/w,R=Vf(E,b,(1+x)/2)*Math.sqrt(d)*v,T=Vf(E,b,(1-x)/2)*Math.sqrt(d)*v;return a==="less"&&(R=-1/0),a==="greater"&&(T=1/0),{name:N,value:g,ci:c,ciLow:R,ciHigh:T,warnings:m}}function o0(e,t,n={}){return B(n,Qd,"cohensD"),Q("cohensD",Wf(e,t,n,n.adjust??!1))}function s0(e,t,n={}){return B(n,Zd,"hedgesG"),Q("hedgesG",Wf(e,t,n,!0))}var a0=e=>Array.isArray(e.effects),Ri=e=>e.multistratum===!0,Bf=e=>Array.isArray(e.strata),Kf=e=>{let t=e;return typeof t.univariate=="object"&&typeof t.anova=="object"},Ti=e=>({test:t=>t.split(":").includes(e)});function i0(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 c0(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=>Ti(b).test(m)),w=o(p.length===0?"Subject":`Subject:${p.join(":")}`,h[l],h[f]);if(m==="(Intercept)")return;let d={name:m,df:h[a],ss:h[n]};w.terms.push(d),s.set(m,d)})}else{let a=t.colNames.indexOf("Df"),l=t.rowNames.indexOf("Residuals"),f=o("Subject",t.table[l][n],t.table[l][a]);t.rowNames.forEach((m,u)=>{if(m==="(Intercept)"||m==="Residuals")return;let h={name:m,df:t.table[u][a],ss:t.table[u][n]};f.terms.push(h),s.set(m,h)})}let i=r.find(a=>a.name==="Subject");if(i===void 0)throw new Error("the result has no subject stratum");let c=e.anova.rowNames.map(a=>{let l=s.get(a);if(l===void 0)throw new Error(`there is no univariate test of '${a}'`);return l});return{strata:r,subject:i,order:c}}function Uf(e,t){let n=Array.isArray(e)?e:[],r=n.filter(s=>!t.some(i=>Ti(s).test(i.name)));if(r.length>0)throw new RangeError(`Observed variable not in data: ${r.join(", ")}`);let o=s=>n.some(i=>Ti(i).test(s));return{isObserved:o,ssObserved:t.reduce((s,i)=>o(i.name)?s+i.ss:s,0)}}function $f(e,t,n,r){let o=i=>Tn(e,t,n,{ncp:i})-r,s=o(0);return Number.isNaN(s)?NaN:s>0?e===1/0?1/0:wr(o,0,Math.max(10,e*t*4),{...jf,extendInt:"downX"}).root:0}function Hf(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=$f(c,t,n,(1+s)/2),l=$f(c,t,n,(1-s)/2);c<=Yn((1-s)/2,t,n)&&(l=0),c<=Yn((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 l0(e){let t=qe([e.map(n=>n.ss)]).k;if(t!==0)for(let n of e)n.ss=V(n.ss,-t)}var zf="needs a table of anova() or Anova(), a fit from lm() or aov() with a formula, its aovSummary(), or a result of aovEz()";function Si(e,t,n){if(typeof e!="object"||e===null)throw new TypeError(zf);if(Ri(e)||Bf(e)||Kf(e))return f0(e,t,n);let r=a0(e)?rt(()=>Ei(e)):e;if(!Array.isArray(r.colNames)||!Array.isArray(r.table))throw new TypeError(zf);let o=Kt.get(r),s=o!==void 0&&!Array.isArray(o)?o:r,i=n.ci===void 0?.95:n.ci;Ai(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((R,T)=>({name:R,df:s.table[T][a],ss:s.table[T][l]})).filter(R=>R.name!=="(Intercept)");l0(f);let m=f.find(R=>R.name==="Residuals");if(m===void 0)throw new TypeError("the table has no row 'Residuals'");if(!(m.df>0))throw new Error("the model has no residual degrees of freedom: the effect sizes are not defined");let u=f.filter(R=>R!==m),h=m.df,p=m.ss,w=p/h,d=f.reduce((R,T)=>R+T.ss,0),b=f.reduce((R,T)=>R+T.df,0)+1,y=t==="Eta2"?n.generalized??!1:!1,M=u.length===1?"plain":y!==!1?"generalized":n.partial??!0?"partial":"plain",{isObserved:g,ssObserved:N}=Uf(y,u),x=R=>{let{ss:T,df:A}=R;if(t==="Eta2")return M==="partial"?T/(T+p):M==="plain"?T/d:T/(T+p+N-(g(R.name)?T:0));if(t==="Omega2"){let C=M==="partial"?(T-A*w)/(T+(b-A)*w):(T-A*w)/(d+w);return Math.max(0,C)}let S=M==="partial"?(T-A*w)/(T+p):(T-A*w)/d;return Math.max(0,S)},E=`${t}${M==="plain"?"":`_${M}`}`;return{colNames:i===null?[E]:[E,"CI","CI_low","CI_high"],rowNames:u.map(R=>R.name),table:u.map(R=>i===null?[x(R)]:Hf(x(R),R.df,h,i,c)),warnings:Array.isArray(r.warnings)?[...r.warnings]:[]}}function f0(e,t,n){let r=n.ci===void 0?.95:n.ci;Ai(r);let o=Ae(n.alternative??"greater",["two.sided","less","greater"]),s=Kf(e),i,c,a;if(s){let g=Kt.get(e),N=c0(g!==void 0&&!Array.isArray(g)?{...e,univariate:g}:e);({strata:i,subject:c}=N),a=N.order.map(x=>({term:x,stratum:N.strata.find(E=>E.terms.includes(x))}))}else{let g=Ri(e)?rt(()=>xi(e)):e,N=Kt.get(g);({strata:i,subject:c}=i0(Array.isArray(N)?{...g,strata:N.map((x,E)=>({...g.strata[E],...x}))}:g)),a=i.flatMap(x=>x.terms.map(E=>({term:E,stratum:x})))}let l=qe([[...i.flatMap(g=>g.terms.map(N=>N.ss)),...i.map(g=>g.sse)]]).k;for(let g of i){g.sse=V(g.sse,-l);for(let N of g.terms)N.ss=V(N.ss,-l)}let f=i.flatMap(g=>g.terms),m=i.reduce((g,N)=>g+N.sse,0),u=f.reduce((g,N)=>g+N.ss,0)+m,h=c.sse/c.dfe,p=t==="Eta2"?n.generalized??!1:!1,w=p!==!1?"generalized":n.partial??!0?"partial":"plain",{isObserved:d,ssObserved:b}=Uf(p,f),y=(g,N)=>{let{ss:x,df:E}=g,R=N.sse/N.dfe;if(t==="Eta2")return w==="partial"?x/(x+N.sse):w==="plain"?x/u:x/(x+m+b-(d(g.name)?x:0));if(t==="Omega2"){let A=N===c?0:1,S=w==="partial"?(x-E*R)/(x+A*N.sse+c.sse+h):(x-E*R)/(u+h);return Math.max(0,S)}let T=w==="partial"?(x-E*R)/(x+N.sse):(x-E*R)/u;return Math.max(0,T)},v=`${t}${w==="plain"?"":`_${w}`}`,M={colNames:r===null?[v]:[v,"CI","CI_low","CI_high"],rowNames:a.map(g=>g.term.name),table:a.map(({term:g,stratum:N})=>{let x=y(g,N);return r===null?[x]:Hf(x,g.df,N.dfe,r,o)}),warnings:[]};return s||(M.groups=a.map(g=>g.stratum.name),M.warnings=[...Ri(e)||Bf(e)?e.warnings:[]]),M}function m0(e,t={}){return B(t,Jd,"etaSquared"),Q("etaSquared",Si(e,"Eta2",t),e)}function u0(e,t={}){return B(t,e0,"omegaSquared"),Q("omegaSquared",Si(e,"Omega2",t),e)}function h0(e,t={}){return B(t,t0,"epsilonSquared"),Q("epsilonSquared",Si(e,"Epsilon2",t),e)}var p0=new Set(["eqb","lowEqbound","highEqbound","hypothesis","paired","varEqual","alpha","mu","eqboundType","biasCorrection","rmCorrection","glass","smdCi"]),d0=["m1","sd1","n1","m2","sd2","n2","r12"],g0=new Set(["eqb","lowEqbound","highEqbound","hypothesis","paired","varEqual","alpha","mu","eqboundType","biasCorrection","rmCorrection","glass","smdCi"]),w0="Equivalence interval does not include zero.",b0="Warning: setting bound type to SMD produces biased results!",v0="MET test may have higher error rates than a nil two-tailed test. Consider wider equivalence bounds.";function Yf(e,t,n,r){if(e===1/0||e===-1/0)return e;let o=e-1<e+1?1:Math.abs(e)*Number.EPSILON;return wr(s=>on(e,t,{ncp:s,lowerTail:!r})-n,e-o,e+o,{tol:0,maxiter:5e3,extendInt:r?"upX":"downX"}).root}function N0(e,t){let{alpha:n,smdCi:r}=t,o=Math.abs(e.delta),s=(E,R,T)=>(1-(T-2)/(T*E*E))*R*R,i,c,a,l,f,m,u,h,p,w,d,b,y=t.biasCorrection;if(e.design==="two"){let{n1:E,n2:R,s1:T,s2:A}=e,S=T*T,C=A*A,L=2*E*R/(E+R);if(t.glass!==void 0){let k=t.glass==="glass1",[F,I,O,q]=k?[T,E,A,R]:[A,R,T,E];c=F,a=I-1,i=y?"Glass's delta(g)":"Glass's delta(d)",l=y?Mr(a):1,f=l*o/c,p=1/R+1/E;let $=O/F;m=Math.sqrt($*$/(q-1)+1/(I-1)+f*f/(2*(I-1))),u=o/(F*Math.sqrt(1/I+$*$/q)),h=E+R-2,w=Math.sqrt(2/L)}else if(t.varEqual)c=Math.sqrt(((E-1)*S+(R-1)*C)/(E+R-2)),a=E+R-2,i=y?"Hedges's g":"Cohen's d",l=y?Mr(a):1,f=l*o/c,p=1/E+1/R,m=Math.sqrt(1/E+1/R+s(l,f,a)),u=o/(c*Math.sqrt(p)),h=a,w=Math.sqrt(2*(R*S+E*C)/(E*R*(S+C)));else{c=Math.sqrt((S+C)/2),a=(E-1)*(R-1)*(S+C)**2/((R-1)*S*S+(E-1)*C*C),i=y?"Hedges's g(av)":"Cohen's d(av)",l=y?Mr(a):1,f=l*o/c,p=2*(R*S+E*C)/(E*R*(S+C));let k=c**4;m=Math.sqrt(f*f*(S*S/(E-1)+C*C/(R-1))/(8*k)+(S/(E-1)+C/(R-1))/(c*c)),u=o/Math.sqrt(S/E+C/R),h=a,w=Math.sqrt(2/L)}d=a,b=(k,F)=>Math.sqrt(d/(d-2)*(2/L)*(1+k*k*L/2)-k*k/(F*F))}else if(e.design==="paired"){let{n:E,s1:R,s2:T,sdif:A,oneMinusR:S}=e,C=t.glass!==void 0?"":t.rmCorrection?"(rm)":"(z)";if(t.glass!==void 0?c=t.glass==="glass1"?R:T:c=t.rmCorrection?A/Math.sqrt(2*S):A,i=t.glass!==void 0?y?"Glass's delta(g)":"Glass's delta(d)":y?`Hedges's g${C}`:`Cohen's d${C}`,a=E-1,d=2*E-2,l=y?Mr(r==="goulet"?d:a):1,f=l*o/c,p=1/E,u=o*Math.sqrt(E)/c,h=a,w=Math.sqrt(2*S/E),b=(L,k)=>{let F=r==="goulet"?d:a,I=Math.sqrt(F/(F-2)*(2*S/E)*(1+L*L*E/(2*S))-L*L/(k*k));return t.rmCorrection?I:I*Math.sqrt(2*S)},t.glass!==void 0){let L=A/c;m=Math.sqrt(L*L/(E-1)+f*f/(2*(E-1)))}else!t.rmCorrection&&r==="nct"?m=Math.sqrt(1/E+s(l,f,a)):m=b(f,l)}else{let{n:E,s:R}=e;c=R,a=E-1,d=a,i=y?"Hedges's g":"Cohen's d",l=y?Mr(a):1,f=l*o/c,p=1/E,m=Math.sqrt(1/E+f*f/(2*E)),u=o*Math.sqrt(E)/R,h=a,w=1/Math.sqrt(E),b=(T,A)=>Math.sqrt(a/(a-2)*(1/E)*(1+T*T*E)-T*T/(A*A))}let v,M,g,N=a;if(r==="nct"){g=m,N=h;let E=Yf(u,h,n,!0),R=Yf(u,h,n,!1);v=E*Math.sqrt(p)*l,M=R*Math.sqrt(p)*l}else if(r==="goulet"){g=b(f,l),N=d;let E=f/w;v=jt(n,d,{ncp:E})*w,M=jt(n,d,{ncp:E,lowerTail:!1})*w}else{g=m;let E=r==="t"?gt(n,a,!1,!1):le(n,{lowerTail:!1});v=f-E*g,M=f+E*g}let x=e.delta<0;return{label:i,denom:c,estimate:x?-f:f,se:g,confInt:x?[-M,-v]:[v,M],df:N,J:l}}function Gf(e,t){if(!(Array.isArray(e)||ArrayBuffer.isView(e)))throw new TypeError(`'${t}' must be an array of numbers`);let n=e;for(let r=0;r<n.length;r++){let o=n[r];if(typeof o!="number")throw new TypeError(`'${t}' must be numeric`);if(o===1/0||o===-1/0)throw new RangeError(`'${t}' must not contain infinite values`)}}function Xf(e){let{eqb:t}=e,n;if(t!==void 0){let r=typeof t=="number"?[t]:Array.from(t);if(!r.every(o=>typeof o=="number"))throw new TypeError("'eqb' must be a number or an array of 1 or 2 numbers");if(r.length===1)n=[-Math.abs(r[0]),Math.abs(r[0])];else if(r.length===2)n=[Math.min(r[0],r[1]),Math.max(r[0],r[1])];else throw new RangeError("'eqb' must be a number or an array of 1 or 2 numbers")}else{if(e.lowEqbound===void 0||e.highEqbound===void 0)throw new TypeError("the equivalence bounds are required: 'eqb', or 'lowEqbound' and 'highEqbound'");n=[e.lowEqbound,e.highEqbound]}if(!n.every(r=>typeof r=="number"&&Number.isFinite(r)))throw new RangeError("the equivalence bounds must be finite numbers");if(n[0]>n[1])throw new RangeError("'lowEqbound' must not be greater than 'highEqbound'");return n}function Qf(e){let t=Ae(e.hypothesis,["EQU","MET"]),n=Ae(e.eqboundType,["raw","SMD"])==="SMD",r=Ae(e.smdCi,["nct","goulet","t","z"]),o=e.glass===void 0?void 0:Ae(e.glass,["glass1","glass2"]),s=e.alpha??.05;if(!(typeof s=="number"&&s>0&&s<.5))throw new RangeError("'alpha' must be a single number in (0, 0.5)");let i=e.mu??0;if(!(typeof i=="number"&&Number.isFinite(i)))throw new RangeError("'mu' must be a finite number");let c=(a,l,f)=>{if(a!==void 0&&typeof a!="boolean")throw new TypeError(`'${l}' must be true or false`);return a??f};return{hypothesis:t,smdBounds:n,smdCi:r,glass:o,alpha:s,mu:i,paired:c(e.paired,"paired",!1),varEqual:c(e.varEqual,"varEqual",!1),biasCorrection:c(e.biasCorrection,"biasCorrection",!0),rmCorrection:c(e.rmCorrection,"rmCorrection",!1)}}function Zf(e,t,n,r,o,s,i,c,a,l,f,m){let{alpha:u,hypothesis:h}=l,p=N0(c,l),w=V(p.denom,a),d=l.smdBounds?[f[0]*w,f[1]*w]:f,b=O=>{if(O===0)return[0,n];let q=Math.floor(Math.log2(Math.abs(O))),$=Ve(O,-q);return l.smdBounds?[$*p.denom,q+a]:[$,q]},y=l.smdBounds?f:[f[0]/w,f[1]/w];l.smdBounds&&m.push(b0),d[0]<=0&&d[1]>=0||m.push(w0);let v=O=>V(O/r,n-o),M=O=>{let[q,$]=b(O),_=Math.max(n,$);return Ve((Ve(t,n-_)-Ve(q,$-_))/r,_-o)},g=v(t),N=M(f[0]),x=M(f[1]),E=V(r,o),R=h==="EQU",T={t:g,se:E,df:s,pValue:2*dt(-Math.abs(g),s,!0,!1)},A={t:N,se:E,df:s,pValue:dt(N,s,!R,!1)},S={t:x,se:E,df:s,pValue:dt(x,s,R,!1)},C=R?Math.max(A.pValue,S.pValue):Math.min(A.pValue,S.pValue);!R&&d[0]<=0&&d[1]>=0&&C<=T.pValue&&m.push(v0);let L=gt(u,s,!1,!1),k=Math.max(n,o),[F,I]=[V(t,n-k),V(r,o-k)];return Q(e,{tTest:T,lower:A,upper:S,pValue:C,bounds:{raw:d,smd:y},raw:{estimate:V(t,n),se:E,confInt:[V(F-L*I,k),V(F+L*I,k)]},smd:{label:p.label,estimate:p.estimate,se:p.se,confInt:p.confInt,df:p.df,J:p.J},confLevel:1-2*u,method:i,hypothesis:h,warnings:m})}function y0(e,t,n={}){B(n,p0,"tTOST");let r=Qf(n),o=Xf(n),{paired:s,varEqual:i,mu:c}=r,a=t===void 0;if(s&&a)throw new TypeError("'y' is required when paired is true");Gf(e,"x"),a||Gf(t,"y");let l=rt(()=>a?yn(e,{mu:c}):yn(e,t,{paired:s,varEqual:i,mu:c})),f=l.parameter,m,u,h,p;if(a){let w=qe([Pt(Array.from(e))]);u=w.k;let d=w.groups[0],b=st(d);h=it(d)-V(c,-u),p=Math.sqrt(b/d.length),m={design:"one",n:d.length,delta:h,s:Math.sqrt(b)}}else if(s){let[w,d]=Zn(Array.from(e),Array.from(t)),b=qe([w,d]);u=b.k;let[y,v]=b.groups,M=y.map((T,A)=>T-v[A]),g=Math.sqrt(st(y)),N=Math.sqrt(st(v)),x=st(M),E=Math.sqrt(x),R=(E-(g-N))*(E+(g-N))/(2*g*N);h=it(M)-V(c,-u),p=Math.sqrt(x/M.length),m={design:"paired",n:M.length,delta:it(y)-it(v)-V(c,-u),s1:g,s2:N,sdif:E,oneMinusR:R}}else{let w=qe([Pt(Array.from(e)),Pt(Array.from(t))]);u=w.k;let[d,b]=w.groups;if(d.length<2||b.length<2)throw new RangeError("not enough observations: at least 2 are needed in each sample");let[y,v,M,g]=[d.length,b.length,st(d),st(b)];h=it(d)-it(b)-V(c,-u),p=Math.sqrt(i?((y-1)*M+(v-1)*g)/(y+v-2)*(1/y+1/v):Math.sqrt(M/y)**2+Math.sqrt(g/v)**2),m={design:"two",n1:y,n2:v,delta:h,s1:Math.sqrt(M),s2:Math.sqrt(g)}}return Zf("tTOST",h,u,p,u,f,l.method,m,u,r,o,[])}function M0(e,t={}){B(e,d0,"tsumTOST"),B(t,g0,"tsumTOST");let n=Qf(t),r=Xf(t),{m1:o,sd1:s,n1:i,m2:c,sd2:a,n2:l,r12:f}=e,m=(k,F)=>{if(typeof k!="number"||!Number.isFinite(k))throw new RangeError(`'${F}' must be a finite number`);return k},u=(k,F)=>{let I=m(k,F);if(!(Number.isInteger(I)&&I>=2))throw new RangeError(`'${F}' must be an integer of at least 2`);return I},h=(k,F)=>{let I=m(k,F);if(!(I>0))throw new RangeError(`'${F}' must be positive`);return I};m(o,"m1"),h(s,"sd1"),u(i,"n1");let p=c===void 0&&a===void 0&&l===void 0;if(n.paired&&p)throw new TypeError("'m2', 'sd2' and 'n2' are required when paired is true");p||(m(c,"m2"),h(a,"sd2"),u(l,"n2"));let w=[],{groups:d,k:b}=qe([[o,...p?[]:[c],n.mu]]),{groups:y,k:v}=qe([[s,...p?[]:[a]]]),[M,g=NaN]=p?[d[0][0]]:[d[0][0],d[0][1]],N=d[0][d[0].length-1],[x,E=NaN]=y[0],R=k=>V(k,b-v),T,A,S,C,L;if(p)T=M-N,A=x/Math.sqrt(i),S=i-1,C="One Sample t-test",L={design:"one",n:i,delta:R(T),s:x};else if(n.paired){let k=m(f,"r12");if(!(k>-1&&k<1))throw new RangeError("'r12' must be in (-1, 1)");let F=i;i!==l&&(w.push("Unequal number of pairs; using smallest n"),F=Math.min(i,l));let I=Math.sqrt(x*x+E*E-2*k*x*E);if(!(I>0))throw new RangeError("the standard deviation of the differences is 0");T=M-g-N,A=I/Math.sqrt(F),S=F-1,C="Paired t-test",L={design:"paired",n:F,delta:R(T),s1:x,s2:E,sdif:I,oneMinusR:1-k}}else{let k=l,F=x*x,I=E*E;if(T=M-g-N,n.varEqual){let O=((i-1)*F+(k-1)*I)/(i+k-2);A=Math.sqrt(O*(1/i+1/k)),S=i+k-2,C=" Two Sample t-test"}else{let O=F/i,q=I/k;A=Math.sqrt(O+q),S=(O+q)**2/(O*O/(i-1)+q*q/(k-1)),C="Welch Two Sample t-test"}L={design:"two",n1:i,n2:k,delta:R(T),s1:x,s2:E}}return Zf("tsumTOST",T,b,A,v,S,C,L,v,n,r,w)}var E0=["alternative","tsmethod"],x0=["square","central"],R0=1e3,T0=32,A0=.9,Jf=1+1e-7,S0=2**24,q0=34e7;function k0(e,t={}){if(B(t,E0,"boschlooTest"),e.length!==2||e.some(N=>N.length!==2))throw new TypeError("'table' must be a 2 x 2 table");let n=Ze(t.alternative),r=Ae(t.tsmethod,x0),o=e.map((N,x)=>Array.from(N,(E,R)=>Kr(E,`table[${x}][${R}]`))),[[s,i],[c,a]]=o,l=s+i,f=c+a;if(l===0||f===0)throw new RangeError("each row (group) of 'table' must have at least one observation");if((l+1)*(f+1)>S0)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(R0,Math.ceil(T0*Math.sqrt(m))),h=N=>Math.min(N+1,40*Math.sqrt(N));if(u*(h(l)+h(f)+4)>q0)throw new RangeError(`Boschloo's test of these group sizes (${l} and ${f}) would take too long`);let p=s/l-c/f,w;n!=="two.sided"?w=n:r==="square"?w="square":w=p>0?"greater":"less";let d=s+c,b;w==="less"?b=sn(s,l,f,d,!0,!1):w==="greater"?b=sn(s-1,l,f,d,!1,!1):b=rt(()=>ko(o)).pValue;let y=I0(l,f,s,c,w),{value:v,at:M}=_0(y,l,f,u),g=n==="two.sided"&&r==="central"?Math.min(1,2*v):v;return Q("boschlooTest",{method:"Boschloo's Exact Test",alternative:n,statistic:b,statisticName:"test statistic",pValue:g,estimate:p,estimateNames:"difference in proportion",nullValue:0,nullValueName:"difference in proportion",nuisanceParameter:M,warnings:[]})}function I0(e,t,n,r,o){let s=e+t,i=new Float64Array(s+1);for(let b=2;b<=s;b++)i[b]=fs(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 y=Math.max(0,b-t),M=Math.min(b,e)-y+1,g=-1/0;for(let E=0;E<M;E++){let R=y+E,T=-i[R]-i[e-R]-i[b-R]-i[t-b+R];f[E]=T,T>g&&(g=T)}let N=0;for(let E=0;E<M;E++)f[E]=Math.exp(f[E]-g),N+=f[E];for(let E=0;E<M;E++)f[E]=f[E]/N;let x=0;for(let E=0;E<M;E++)m[E]=x+=f[E];x=0;for(let E=M-1;E>=0;E--)u[E]=x+=f[E];if(o==="less")for(let E=0;E<M;E++)a[(y+E)*c+b-y-E]=m[E];else if(o==="greater")for(let E=0;E<M;E++)a[(y+E)*c+b-y-E]=u[E];else{let E=0;for(let R=1;R<M;R++)f[R]>f[E]&&(E=R);for(let R=0;R<M;R++){let T=f[R]*Jf,A=-1,S=E;for(;S-A>1;){let F=A+S>>1;f[F]<=T?A=F:S=F}let C=E,L=M;for(;L-C>1;){let F=C+L>>1;f[F]<=T?L=F:C=F}let k=f[E]<=T?1:(A>=0?m[A]:0)+(L<M?u[L]:0);a[(y+R)*c+b-y-R]=Math.min(1,k)}}}let h=a[n*c+r]*Jf,p=new Int32Array(e+2),w=[],d=[];for(let b=0;b<=e;b++){p[b]=w.length;let y=0;for(;y<=t;){if(a[b*c+y]<=h){let v=y;for(;y+1<=t&&a[b*c+y+1]<=h;)y++;w.push(v),d.push(y)}y++}}return p[e+1]=w.length,{starts:p,from:Int32Array.from(w),to:Int32Array.from(d)}}function em(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]=hn(r,e,t,!1);let o=Ot(1,-t),s=at([t,0],o),i=at(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 _0(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[M,g]=em(t,v,c),[N,x]=em(n,v,a),E=0;for(let T=N;T<=x;T++)l[T]=E+=a[T];E=0;for(let T=x;T>=N;T--)f[T]=E+=a[T];let R=0;for(let T=M;T<=g;T++){let A=c[T];if(A===0)continue;let S=0;for(let C=o[T];C<o[T+1];C++){let L=s[C],k=i[C];if(!(k<N||L>x))if(L===0)S+=l[Math.min(k,x)];else if(k===n)S+=f[Math.max(L,N)];else for(let F=Math.max(L,N);F<=Math.min(k,x);F++)S+=a[F]}R+=A*S}return Math.min(1,R)},u=Math.PI/2,h=v=>v<=0?0:v>=u?1:Math.sin(v)**2,p=new Float64Array(r+2),w=0;for(let v=0;v<=r+1;v++)p[v]=m(h(u*v/(r+1))),p[v]>p[w]&&(w=v);let d=p[w],b=h(u*w/(r+1));if(d>=1)return{value:1,at:b};let y=A0*d;for(let v=0;v<=r+1;v++){let M=p[v];if(M<y||v>0&&!(M>p[v-1])||v<=r&&!(M>=p[v+1]))continue;let g=u*Math.max(0,v-1)/(r+1),N=u*Math.min(r+1,v+1)/(r+1),E=Tl(A=>-m(h(A)),g,N,1e-14).x,R=m(h(E)),T=1e-4/Math.sqrt(2*(t+n));for(let A=0;A<2&&(E-T>g&&E+T<N);A++,T*=.01){let S=m(h(E-T)),C=m(h(E+T)),L=S-2*R+C;if(!(L<0))break;let k=T*(S-C)/(2*L);if(!(Math.abs(k)<T))break;let F=m(h(E+k));F>R*(1+2**-50)&&(E+=k,R=F)}R>d&&(d=R,b=h(E))}return{value:d,at:b}}function wn(e){let t=St(e);return t.some(n=>typeof n=="string")?t.map(n=>typeof n=="number"&&!Number.isNaN(n)?ft(n):n):t}function Er(e,t){return typeof e=="number"&&typeof t=="number"?e-t:dn(String(e),String(t))}function or(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=wn(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(Er);return{labels:i.map(ft),groups:i.map(c=>s.get(c))}}function xr(e,t,n="drop"){return or(e,t,n).groups}function js(e){let t=[...e].sort((r,o)=>r-o),n=t.length;return n%2===1?t[(n-1)/2]:en(t[n/2-1],t[n/2])}function qi(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 tm=["correct"];function L0(e,t,n={}){wt(t)&&B(t,tm,"mcnemarTest"),B(n,tm,"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=C0(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=nm(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+=nm(u-h,u,h),l++)}if(l===0)throw new RangeError("the table has no discordant pairs")}let a=s*(s-1)/2;return{method:c?"McNemar's Chi-squared test with continuity correction":"McNemar's Chi-squared test",statistic:i,statisticName:"McNemar's chi-squared",parameter:a,parameterName:"df",pValue:bt(i,a/2,2,!1,!1),warnings:[]}}function nm(e,t,n){let r=t+n;return r<1/0?Vt(e,r):Vt(V(e,-520),V(t,-1040)+V(n,-1040))}function C0(e,t){if(e.length!==t.length)throw new RangeError("'x' and 'y' must have the same length");let n=[],r=wn(e),o=wn(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(Er),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 F0=["ratio","alternative","confLevel"],ki=2**-1022;function rm(e,t,n={}){if(B(n,F0,"varTest"),typeof e=="string"){let v=er(e,t);return rm(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=Ze(n.alternative),c=n.confLevel??.95;_t(c);let a=r.length-1,l=o.length-1;if(a<1)throw new Error("not enough 'x' observations");if(l<1)throw new Error("not enough 'y' observations");let[f,m]=Dt([r,o]).variance,u=0;if(!(f>=ki&&m>=ki)){let v=Dt([r]),M=Dt([o]);f=v.variance[0],m=M.variance[0],u=2*(v.k-M.k)}if(f===0&&m===0)throw new RangeError("both samples have zero variance");let h=u===0?f/m:Ve(f/m,u),p=h/s,w=ht(p,a,l,!0,!1),d=ht(p,a,l,!1,!1),b,y;if(i==="less")b=w,y=[0,h/un(1-c,a,l,!0,!1)];else if(i==="greater")b=d,y=[h/un(c,a,l,!0,!1),1/0];else{b=Math.min(1,2*Math.min(w,d));let v=(1-c)/2,M=v<1e-8?un(v,a,l,!1,!1):un(1-v,a,l,!0,!1);y=[h/M,h/un(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:y,confLevel:c,estimate:h,estimateNames:"ratio of variances",nullValue:s,nullValueName:"ratio of variances",warnings:[]}}function om(e,t){if(typeof e=="string"){let p=No(e,t);return om(xl(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=Dt(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<ki)){let p=o.map(M=>{let g=Dt([M]);return{w:g.variance[0],e:g.k}}),w=Math.max(...p.map(M=>M.e)),d=p.map(M=>Math.log(M.w)+2*(M.e-w)*Math.LN2),b=d.map((M,g)=>Math.log(s[g])+M),y=Math.max(...b),v=y+Math.log(b.reduce((M,g)=>M+Math.exp(g-y),0))-Math.log(c);m=c*v-d.reduce((M,g,N)=>M+s[N]*g,0)}let u=m/(1+(f-1/c)/(3*(r-1))),h=r-1;return{method:"Bartlett test of homogeneity of variances",statistic:u,statisticName:"Bartlett's K-squared",parameter:h,parameterName:"df",pValue:bt(u,h/2,2,!1,!1),warnings:[]}}var O0=["alternative"];function Ii(e,t,n,r){return{method:e,statistic:n,statisticName:t,parameter:r,parameterName:"df",pValue:bt(n,r/2,2,!1,!1),warnings:[]}}function sm(e){let t=0;for(let n of bn(e))t+=n*n*n-n;return t}function Ws(e,t){if(typeof e=="string"){let l=No(e,t);return Ws(l.values,l.group.codes)}let n=xr(e,t,"error"),r=n.length;if(r<2)throw new RangeError("all observations are in the same group");let o=n.flat(),s=o.length,i=vt(o),c=0,a=0;for(let l of n){let f=0;for(let m=0;m<l.length;m++)f+=i[a+m];a+=l.length,c+=f*f/l.length}return c=(12*c/(s*(s+1))-3*(s+1))/(1-sm(o)/(s*s*s-s)),Ii("Kruskal-Wallis rank sum test","Kruskal-Wallis chi-squared",c,r-1)}function _i(e,t){if(typeof e=="string")return _i(ka(e,t));let n=qi(e),r=n.length,o=e[0]?.length??0;if(r<2||o<2)throw new RangeError("'y' must have at least two complete rows and two columns");let s=new Array(o).fill(0),i=0;for(let l of n){let f=vt(l);for(let m=0;m<o;m++)s[m]+=f[m];i+=sm(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 Ii("Friedman rank sum test","Friedman chi-squared",a,o-1)}function D0(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{..._i(e),method:"Cochran's Q test",statisticName:"Q"}}function am(e,t){if(typeof e=="string")return am(ka(e,t));let n=qi(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(w=>Nt(w)-an(w));s.some(w=>w===1/0)&&n.every(w=>w.every(Number.isFinite))&&(s=qe(n).groups.map(w=>Nt(w)-an(w)));let i=vt(s),c=new Array(o).fill(0),a=0;n.forEach((w,d)=>{let b=vt(w);for(let y=0;y<o;y++){let v=i[d]*(b[y]-(o+1)/2);a+=v*v,c[y]+=v}});let l=0;for(let w of c)l+=w*w;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 w=0;for(let d=2;d<=o;d++)w+=Math.log(d);h=Math.exp((1-r)*w)}else u=(r-1)*l/(a-l),h=ht(u,f,m,!1,!1);return{method:"Quade test",statistic:u,statisticName:"Quade F",parameter:p,parameterName:["num df","denom df"],pValue:h,warnings:[]}}function im(e,t){if(typeof e=="string"){let h=No(e,t);return im(h.values,h.group.codes)}let n=xr(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 w=js(p);return p.map(d=>Math.abs(d-w))}),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(qe(n).groups));let i=s.length,a=vt(s).map(h=>le((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 w=0;w<h.length;w++)p+=a[u+w];u+=h.length,m+=h.length*(p/h.length-f)**2}return m/=st(a),Ii("Fligner-Killeen test of homogeneity of variances","Fligner-Killeen:med chi-squared",m,r-1)}function cm(e,t,n={}){if(B(n,O0,"moodTest"),typeof e=="string"){let M=er(e,t);return cm(M.x,M.y,n)}let r=Array.from(e).filter(Number.isFinite),o=Array.from(t).filter(Number.isFinite),s=Ze(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},(M,g)=>g).sort((M,g)=>l[M]-l[g]),m=new Array(a),u=0;for(;u<a;){let M=u;for(;M+1<a&&l[f[M+1]]===l[f[u]];)M++;let g=0;for(let N=u;N<=M;N++)g+=(N+1-(a+1)/2)**2;g/=M-u+1;for(let N=u;N<=M;N++)m[f[N]]=g;u=M+1}let h=0;for(let M=0;M<i;M++)h+=m[M];let p=0;for(let M of m)p+=M;p/=a;let w=0;for(let M of m)w+=(M-p)**2;let d=i*p,b=i*c/(a*(a-1))*w,y=(h-d)/Math.sqrt(b),v=s==="less"?ae(y):s==="greater"?ae(y,{lowerTail:!1}):2*ae(-Math.abs(y));return{method:"Mood two-sample test of scale",alternative:s,statistic:y,statisticName:"Z",pValue:v,warnings:[]}}function Rr(e,t){let n=0;for(let r=e.length-1;r>=0;r--)n=n*t+e[r];return n}var P0=[0,.221157,-.147981,-2.07119,4.434685,-2.706056],V0=[0,.042981,-.293762,-1.752461,5.682633,-3.582633],$0=[.544,-.39978,.025054,-6714e-7],z0=[1.3822,-.77857,.062767,-.0020322],j0=[-1.5861,-.31082,-.083751,.0038915],W0=[-.4803,-.082676,.0030302],B0=[-2.273,.459];function K0(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]=-le((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+Rr(P0,i),a=new Float64Array(n);a[0]=c;let l,f;if(e>5){let m=r[1]/s+Rr(V0,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 lm(e){let t=qe([e]).groups[0],n=t.length,r=K0(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 U0=1.90985931710274,H0=1.0471975511966;function fm(e,t){if(Number.isNaN(e)||!(t>=3))return NaN;if(t===3){let i=U0*(Math.asin(Math.sqrt(1-e))-H0);return Math.max(i,0)}let n=Math.log(e);if(t<=11){let i=Rr(B0,t);if(n>=i)return 1e-99;let c=Rr($0,t),a=Math.exp(Rr(z0,t));return ae(-Math.log(i-n),{mean:c,sd:a,lowerTail:!1})}let r=Math.log(t),o=Rr(j0,r),s=Math.exp(Rr(W0,r));return ae(n,{mean:o,sd:s,lowerTail:!1})}var pm=Math.log(2*Math.PI),mm=2e-14,um=500;function Y0(e){return Math.exp(-.5*e*e-.5*pm)}function G0(e){return ae(e,{logP:!0})}function dm(e,t=-1/0,n=!1){let r=t===-1/0?qa(e,0,2,0,mm,um):qa(e,t,1,0,mm,um),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 hm(e,t,n){let r=t.map(Math.sqrt),o=t.map(i=>Math.sqrt(1-i));return dm(i=>{let c=0;for(let a=0;a<r.length;a++){let l=(r[a]*i+e)/o[a];if(!n){c+=G0(l);continue}let f=(r[a]*i-e)/o[a],m=ae(f)+ae(l,{lowerTail:!1});m<.5?c+=Math.log1p(-m):c+=Math.log(Math.max(0,l+f<0?ae(l)-ae(f):ae(f,{lowerTail:!1})-ae(l,{lowerTail:!1})))}return Y0(i)*-Math.expm1(c)})}function Tr(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,hm(e,t,r));let o=n/2,s=Math.log(2)+.5*Math.log(o)-.5*pm-Jc(o);return Math.min(1,dm(c=>{let a=s-o*(Math.expm1(2*c)-2*c);return a<-745?0:Math.exp(a)*hm(e*Math.exp(c),t,r)},-1/0,!0))}var X0=["pAdjustMethod"],Q0=["pAdjustMethod","alternative"],Z0=["alternative"],J0=["pAdjustMethod","alternative"],eg=["method","altp"],tg=["alternative"],gm="Ties are present. z-quantiles were corrected for ties.";function Wn(e,t){let{labels:n,groups:r}=or(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 Li(e){let t=e.groups.flat(),n=vt(t),r=[],o=0;for(let i of e.n){let c=0;for(let a=0;a<i;a++)c+=n[o+a];o+=i,r.push(c/i)}let s=0;for(let i of bn(t))s+=i*i*i-i;return{R:r,C:s/(12*(e.N-1))}}function Oo(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 ng(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=Br(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 Ks(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 Ci(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 Bs=e=>ae(e,{lowerTail:!1}),Fi=(e,t,n)=>ms(Math.abs(e),t,n,{lowerTail:!1});function Oi(e){let t=Dt(e.groups),n=0;for(let r of t.n)r>1&&(n+=r-1);return Do(n,t.ssWithin),{m:t.mean,s2:t.ssWithin/n,df:e.N-e.k,k:t.k}}function Do(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 rg(e,t,n={}){B(n,X0,"kwAllPairsDunnTest");let r=n.pAdjustMethod??"holm",o=Wn(e,t),{R:s,C:i}=Li(o),c=o.N*(o.N+1)/12-i,a=Oo(o.k,(f,m)=>Math.abs(s[f]-s[m])/Math.sqrt(c*(1/o.n[f]+1/o.n[m]))),l=ng(a.map(f=>f.map(m=>2*Bs(m))),r);return Q("kwAllPairsDunnTest",Ks(o,"Dunn's all-pairs test",a,l,r,i!==0?[gm]:[]))}function og(e,t,n={}){B(n,Q0,"kwManyOneDunnTest");let r=Ze(n.alternative),o=n.pAdjustMethod??"single-step",s=Wn(e,t),{R:i,C:c}=Li(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=wm(f,m,r,o);return Q("kwManyOneDunnTest",Ci(s,"Dunn's many-to-one test",f,u,o,c!==0?[gm]:[]))}function wm(e,t,n,r){return r==="single-step"?e.map(o=>n==="two.sided"?Tr(o,t,1/0,!0):Tr(n==="greater"?o:-o,t,1/0,!1)):Br(e.map(o=>n==="two.sided"?2*Bs(Math.abs(o)):Bs(n==="greater"?o:-o)),r)}function sg(e,t,n={}){B(n,Z0,"dunnettTest");let r=Ze(n.alternative),o=Wn(e,t),{m:s,s2:i,df:c}=Oi(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*Tr(Math.abs(h),m,c,!1)):Tr(r==="greater"?h:-h,m,c,!1));return Q("dunnettTest",Ci(o,`Dunnett's-test for multiple
4
- comparisons with one control`,f,u,"single-step"))}function ag(e,t,n={}){B(n,J0,"manyOneUTest");let r=Ze(n.alternative),o=n.pAdjustMethod??"single-step",s=Wn(e,t);if(o==="single-step"&&s.k<3)throw new RangeError(`for 'p.adjust.method = single-step' at least
1
+ /*! rStats.js 1.10.0 | MIT License | includes code ported from SciPy, Boost.Math, statsmodels, LAPACK and others: see THIRD_PARTY_NOTICES.md at https://github.com/numero-uno-jpn/rStats.js */
2
+ import{a as tn,b as zn,c as nr,d as vr,e as xs,f as Fa}from"./chunks/chunk-Y7SXBQZY.js";import{a as bn,c as sn,h as $r,i as dl,m as gl,n as zr,o as wl,s as hs,t as bl,u as bo,v as ps,w as ds,x as gs}from"./chunks/chunk-NAUAW5TO.js";import{b as xl}from"./chunks/chunk-PJ73WRSA.js";import{a as Wa,e as Ba}from"./chunks/chunk-Y5M7NEJ5.js";import{a as bs,b as tr,c as yo,d as Al,e as ql,f as vs,g as Ze,h as cn,i as vn,j as _t,k as Ll,l as sp,n as Ts,o as Ro,p as Ss}from"./chunks/chunk-NMVFSSEZ.js";import"./chunks/chunk-MVIBJA2I.js";import{a as hl,d as us}from"./chunks/chunk-K25FNCFH.js";import{d as nt}from"./chunks/chunk-JPQEOHZG.js";import{a as Rs,e as Bt,f as Wl}from"./chunks/chunk-MULNGWZY.js";import"./chunks/chunk-F3WMBL5R.js";import{a as Mo,b as Oa,c as Da,d as $l,e as Eo,f as Va,g as Wn,h as $a,i as xo,j as zl,k as za,l as ap,m as ja,n as jl,o as ip,p as cp,q as lp}from"./chunks/chunk-R5BXUEH3.js";import{b as It,c as On}from"./chunks/chunk-DC2FP6ZH.js";import{a as kl,c as Il,d as _l,e as br,g as To}from"./chunks/chunk-CN5D6ITZ.js";import{b as La}from"./chunks/chunk-F5K2UXKW.js";import{a as ct,b as Ns,c as ys}from"./chunks/chunk-NNJGXB5K.js";import{a as Dl,b as Pl,c as Es,d as Pa}from"./chunks/chunk-F232C4SB.js";import{a as jn,b as Wr}from"./chunks/chunk-6BDTLUNM.js";import{d as ss,e as as,g as is,h as Vr,m as hn,n as Dn}from"./chunks/chunk-3QUOB47N.js";import{d as ht,e as un,g as Rn,h as Gn,j as Xn}from"./chunks/chunk-YCBQQHCB.js";import{a as Vl}from"./chunks/chunk-6Y7OKIIM.js";import{c as Cl,h as Fl,j as Lt,m as Ca,n as Ol,o as Ms,p as jr}from"./chunks/chunk-AQ7H4RJH.js";import{a as er,b as Tl,g as ws}from"./chunks/chunk-GYLFMAD6.js";import{a as _a,b as dt,c as gt,d as pl,e as Qt,f as Wt}from"./chunks/chunk-RPEFV4FR.js";import{i as wt,l as os,t as fl}from"./chunks/chunk-OXYMJNVJ.js";import{a as Tn,c as Sl}from"./chunks/chunk-IFFGJN5L.js";import{b as vl,c as Nl,d as yl,e as Ml,f as El,i as vo,l as op,n as Q,o as ot,p as Rt}from"./chunks/chunk-MBQJWNPX.js";import{a as No,b as $n,c as Rl,d as Pt,e as vt}from"./chunks/chunk-EDHAQHB7.js";import{a as Nt,b as dn,c as an}from"./chunks/chunk-6U2ICRN3.js";import"./chunks/chunk-YGKM6PIJ.js";import{b as il,c as cl}from"./chunks/chunk-KGDCQ2RV.js";import{A as ol,B as sl,D as al,M as ll,e as Aa,q as Xt,r as se,s as le}from"./chunks/chunk-5PVS6TF2.js";import{A as ml,C as cs,D as xt,E as ls,F as ul,G as fs,H as ka,I as De,J as bt,K as wn,L as Dt,M as Jn,N as Vt,O as ms,P as qe,Q as Ia,b as Ot,c as jt,d as he,e as nl,f as Le,g as _e,h as Et,i as rt,j as ft,k as rl,l as B,m as Fn,p as qa,q as wo,r as Pn,s as pn,t as Vn,u as V,v as Ve,w as Qn,x as it,y as at,z as Zn}from"./chunks/chunk-JRMISESC.js";var Bl=["alternative","mu","paired","varEqual","confLevel"];function Nn(e,t,n={}){if(typeof e!="string"&&ct(t)&&B(t,Bl,"tTest"),B(n,Bl,"tTest"),typeof e=="string")return fp(e,t,n);let r;t!==void 0&&!ArrayBuffer.isView(t)&&!Array.isArray(t)?n=t:r=t;let o=Ze(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(cn(a),i&&r===void 0)throw new TypeError("y is required when paired is true");let l,f,m;if(i){let[A,C]=Jn(Array.from(e),Array.from(r));({d:l,k:m}=ls(A,C))}else{let A=qe(r===void 0?[Dt(Array.from(e))]:[Dt(Array.from(e)),Dt(Array.from(r))]);m=A.k,l=A.groups[0],f=A.groups[1]}let u=V(s,-m),h,p,w,d,b,N,v,E=l.length,g=f!==void 0&&c;if(E<1||!g&&E<2)throw new Error("not enough 'x' observations");let y=it(l),x=E>1?at(l):NaN;if(f===void 0){if(p=E-1,w=Math.sqrt(x/E),w===0||w<10*Number.EPSILON*Math.abs(y))throw new Error("data are essentially constant");h=(y-u)/w,N=i?"Paired t-test":"One Sample t-test",d=y,b=i?"mean difference":"mean of x",v=i?"mean difference":"mean"}else{let A=f.length;if(A<1||!c&&A<2)throw new Error("not enough 'y' observations");if(c&&E+A<3)throw new Error("not enough observations");let C=it(f),L=A>1?at(f):NaN;if(N=`${c?"":"Welch"} Two Sample t-test`,d=[y,C],b=["mean of x","mean of y"],v="difference in means",c){p=E+A-2;let k=0;E>1&&(k+=(E-1)*x),A>1&&(k+=(A-1)*L),k/=p,w=Math.sqrt(k*(1/E+1/A))}else{let k=x/E,F=L/A;w=Math.sqrt(Math.sqrt(k)**2+Math.sqrt(F)**2),p=fs([k,F],[E-1,A-1])}if(w===0||w<10*Number.EPSILON*Math.max(Math.abs(y),Math.abs(C)))throw new Error("data are essentially constant");h=(y-C-u)/w}let M=typeof d=="number"?d:d[0]-d[1],R=(A,C)=>{let L=u+(h+C*A)*w;return Number.isFinite(L)&&Math.max(Math.abs(u),Math.abs(h*w))<=16*(Math.abs(M)+A*w)?L:M+C*A*w},T,S;if(o==="less")T=dt(h,p,!0,!1),S=[-1/0,R(gt(a,p,!0,!1),1)];else if(o==="greater")T=dt(h,p,!1,!1),S=[R(gt(a,p,!0,!1),-1),1/0];else{T=2*dt(-Math.abs(h),p,!0,!1);let A=1-a,C=A/2<1e-8?gt(A/2,p,!1,!1):gt(1-A/2,p,!0,!1);S=[R(C,-1),R(C,1)]}return S=[V(S[0],m),V(S[1],m)],d=typeof d=="number"?V(d,m):d.map(A=>V(A,m)),w=V(w,m),{method:N,alternative:o,statistic:h,statisticName:"t",parameter:p,parameterName:"df",pValue:T,confInt:S,confLevel:a,estimate:d,estimateNames:b,nullValue:s,nullValueName:v,stdErr:w,stdError:w,warnings:[]}}function fp(e,t,n){if(n.paired!==void 0)throw new Error("cannot use 'paired' in formula method");let r=bs(e,t,!0);if(r.kind==="one")return Nn(r.x,n);if(r.kind==="paired")return Nn(r.x,r.y,{...n,paired:!0});let[o,s]=r.levels;return{...Nn(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 mp=new Set(["n","delta","sd","sigLevel","power","type","alternative","strict","tol"]),up=["n","p1","p2","sigLevel","power","alternative","strict","tol"],hp=["groups","n","betweenVar","withinVar","sigLevel","power"],Ul=Number.EPSILON**.25;function Se(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 ye=e=>e==null;function ln(e,t,n){if(!ye(e)&&!t(e))throw new RangeError(n)}var So=e=>e>=0&&e<=1,As=e=>e>0&&e<1/0;function nn(e,t){if(e.filter(ye).length!==1)throw new RangeError(t)}function Ye(e,t,n,r={}){return _l(e,t,n,r).root}var Nr=(e,t,n,r)=>Qt(e,t,{ncp:n,lowerTail:r});function Ka(e,t){if(e!=null&&!(typeof e=="number"&&e>=0&&e<=1))throw new RangeError(t)}var Ua="'sig.level' must be numeric in [0, 1]";function pp(e={}){B(e,mp,"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;nn([t,n,o,r,s],"exactly one of 'n', 'delta', 'sd', 'power', and 'sig.level' must be NULL"),Ka(s,Ua),ln(r,So,"'power' must be a probability"),ln(o,As,"'sd' must be a positive finite number"),ln(t,w=>w>=2,"number of observations in each group must be at least 2"),ln(n,w=>!Number.isNaN(w),"'delta' must not be NaN"),ln(e.tol,As,"'tol' must be a positive finite number");let i=Se(e.type,["two.sample","one.sample","paired"]),c=Se(e.alternative,["two.sided","one.sided"]),a=e.strict??!1,l=e.tol??Ul,f=i==="two.sample"?2:1,m=c==="two.sided"?2:1;m===2&&!ye(n)&&(n=Math.abs(n));let u=(w,d,b,N)=>{let v=Math.max(1e-7,w-1)*f,E=Wt(N/m,v,{lowerTail:!1}),g=Math.sqrt(w/f)*d/b;Number.isFinite(g)||(g=Math.sqrt(w/f)*(d/b));let y=Nr(E,v,g,!1);return a&&m===2?y+Nr(-E,v,g,!0):y};if(ye(r))r=u(t,n,o,s);else{let w=r;if(ye(t)){if(n===0)throw new RangeError("'n' cannot be determined when 'delta' is 0: the power does not depend on it");t=Ye(d=>u(d,n,o,s)-w,2,1e7,{tol:l,extendInt:"upX"})}else if(ye(o)){if(n===0)throw new RangeError("'sd' cannot be determined when 'delta' is 0: the power does not depend on it");let d=Math.abs(n);o=Ye(b=>u(t,n,b,s)-w,d*1e-7,d*1e7,{tol:l,extendInt:n>0?"downX":"upX"})}else ye(n)?n=Ye(d=>u(t,d,o,s)-w,o*1e-7,o*1e7,{tol:l,extendInt:"upX"}):s=Ye(d=>u(t,n,o,d)-w,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 dp(e={}){B(e,up,"powerPropTest");let{n:t,p1:n,p2:r,power:o}=e,s=e.sigLevel===void 0?.05:e.sigLevel;nn([t,n,r,o,s],"exactly one of 'n', 'p1', 'p2', 'power', and 'sig.level' must be NULL"),Ka(s,Ua),ln(o,So,"'power' must be a probability"),ln(n,So,"'p1' must be a probability"),ln(r,So,"'p2' must be a probability"),ln(t,u=>u>0,"number of observations in each group must be positive"),ln(e.tol,As,"'tol' must be a positive finite number");let i=Se(e.alternative,["two.sided","one.sided"]),c=e.strict??!1,a=e.tol??Ul,l=i==="two.sided"?2:1,f=(u,h,p,w)=>{let d=le(w/l,{lowerTail:!1});if(c&&l===2){let b=(h+p)/2,N=h*(1-h),v=p*(1-p),E=b*(1-b),g=Math.abs(h-p),y=Math.sqrt(N+v);return se((Math.sqrt(u)*g-d*Math.sqrt(2*E))/y)+se((Math.sqrt(u)*g+d*Math.sqrt(2*E))/y,{lowerTail:!1})}return se((Math.sqrt(u)*Math.abs(h-p)-d*Math.sqrt((h+p)*(1-(h+p)/2)))/Math.sqrt(h*(1-h)+p*(1-p)))},m=[];if(ye(o))o=f(t,n,r,s);else{let u=o;if(ye(t)){if(n===r)throw new RangeError("'n' cannot be determined when 'p1' equals 'p2': the power does not depend on it");t=Ye(h=>f(h,n,r,s)-u,1,1e7,{tol:a,extendInt:"upX"})}else if(ye(n)){if(r===0)throw new RangeError("'p1' cannot be determined when 'p2' is 0: no p1 in [0, p2] differs from p2");n=Ye(h=>f(t,h,r,s)-u,0,r,{tol:a,extendInt:"yes"}),n<0&&m.push("No p1 in [0, p2] can be found to achieve the desired power")}else if(ye(r)){if(n===1)throw new RangeError("'p2' cannot be determined when 'p1' is 1: no p2 in [p1, 1] differs from p1");r=Ye(h=>f(t,n,h,s)-u,n,1,{tol:a,extendInt:"yes"}),r>1&&m.push("No p2 in [p1, 1] can be found to achieve the desired power")}else s=Ye(h=>f(t,n,r,h)-u,1e-10,1-1e-10,{tol:a,extendInt:"upX"}),(s<0||s>1)&&m.push("No significance level [0, 1] can be found to achieve the desired power")}return Q("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 Kl=1e16,qs=(e,t)=>e>Kl&&e<1/0&&t>Kl;function ks(e,t,n,r){let o=le(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 se((o*i-s)/c,{lowerTail:!1})}function gp(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 V(V(e,-s)*V(t,-i)*V(n,-c)/V(r,-a),s+i+c-a)}function wp(e={}){B(e,hp,"powerAnovaTest");let{groups:t,n,betweenVar:r,withinVar:o,power:s}=e,i=e.sigLevel===void 0?.05:e.sigLevel;if(nn([t,n,r,o,s,i],"exactly one of 'groups', 'n', 'between.var', 'within.var', 'power', and 'sig.level' must be NULL"),!ye(t)&&!(t>=2))throw new RangeError("number of groups must be at least 2");if(!ye(n)&&!(n>=2))throw new RangeError("number of observations in each group must be at least 2");Ka(i,Ua),ln(s,So,"'power' must be a probability"),ln(r,a=>a>=0&&a<1/0,"'betweenVar' must be a nonnegative finite number"),ln(o,As,"'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=gp(a-1,l,f,m));let p=a-1,w=(l-1)*a;return h===1/0?1:qs(p,w)?ks(p,w,h,u):Rn(Gn(u,p,w,{lowerTail:!1}),p,w,{ncp:h,lowerTail:!1})};if(ye(s))s=c(t,n,r,o,i);else{let a=s;if(ye(t))t=Ye(l=>c(l,n,r,o,i)-a,2,100);else if(ye(n)){if(r===0)throw new RangeError("'n' cannot be determined when 'betweenVar' is 0: the power does not depend on it");n=Ye(l=>c(t,l,r,o,i)-a,2,1e5)}else if(ye(o)){if(r===0)throw new RangeError("'withinVar' cannot be determined when 'betweenVar' is 0: the power does not depend on it");o=Ye(l=>c(t,n,r,l,i)-a,r*1e-7,r*1e7)}else ye(r)?r=Ye(l=>c(t,n,l,o,i)-a,o*1e-7,o*1e7):i=Ye(l=>c(t,n,r,o,l)-a,1e-10,1-1e-10)}return{groups:t,n,betweenVar:r,withinVar:o,sigLevel:i,power:s,note:"n is number in each group",method:"Balanced one-way analysis of variance power calculation",warnings:[]}}var bp=["correct","p","rescaleP"],vp=["confLevel"],Np=["alternative","rowScores","colScores"];function Zt(e){let t=[0,0];for(let n of e)t=he(t,[n,0]);return t}function Ya(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 Ga=e=>e.length>0&&typeof e[0]!="number",Xa=(e,t)=>Number.isNaN(e)?NaN:wt(e,t/2,2,!1,!1);function Hl(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 Ao(e,t){return e.length>0&&Array.isArray(e[0])?e.map((n,r)=>n.map((o,s)=>Hl(o,t[r][s]))):e.map((n,r)=>Hl(n,t[r]))}var yp=["none","williams","yates"],Gl="Log likelihood ratio (G-test)";function Mp(e,t={}){B(t,bp,"gTest");let n=Se(t.correct,yp);if(typeof e!="object"||e===null||typeof e.length!="number")throw new TypeError("'x' must be an array of counts or of rows");if(Ga(e)){let o=Ya(e);return Yl(o.flat()),o.length===1?Q("gTest",Ha(o[0],n,t)):o[0].length===1?Q("gTest",Ha(o.map(s=>s[0]),n,t)):Q("gTest",Ep(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 Yl(r),Q("gTest",Ha(r,n,t))}function Yl(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 Xl(e,t){return e<1/0?0:Math.ceil(Math.log2(t))+1}function Ql(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=he(n,sl(o,s))}return n[0]+n[1]}function Ep(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=Zt(e.flat()),s=Xl(o[0],n*r),i=s===0?e:e.map(v=>v.map(E=>V(E,-s))),c=s===0?o:Zt(i.flat()),a=i.map(v=>Zt(v)),l=i[0].map((v,E)=>Zt(i.map(g=>g[E]))),f=a.map(v=>l.map(E=>_e(v,rt(E,c)))),m=i;if(t==="yates"){let[[v,E],[g,y]]=i,x=v*y-E*g>0?-.5:.5,M=V(.5,-s),R=x<0?-M:M;m=[[v+R,E-R],[g-R,y+R]]}let u=Ql(m.flat(),f.flat()),h=Number.isNaN(u)?["NaNs produced"]:[],p=1;if(t==="williams"){if(a.some(y=>y[0]===0)||l.some(y=>y[0]===0))throw new RangeError("Williams' correction is not defined for a table with an empty row or column");let v=c[0]+c[1],E=a.reduce((y,x)=>y+1/(x[0]+x[1]),0),g=l.reduce((y,x)=>y+1/(x[0]+x[1]),0);p=1+V((v*E-1)*(v*g-1)/(6*v*(n-1)*(r-1)),-s)}let w=V(2*u,s)/p,d=(n-1)*(r-1),b=f.map(v=>v.map(E=>V(E[0]+E[1],s)));return{method:`${Gl} test of independence ${t==="none"?"without correction":t==="williams"?"with Williams' correction":"with Yates' correction"}`,statistic:w,statisticName:"G",parameter:d,parameterName:"X-squared df",pValue:Xa(w,d),observed:s===0?m:m.map(v=>v.map(E=>V(E,s))),expected:b,ftResiduals:Ao(e,b),warnings:h}}function Ha(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=Zt(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=Zt(e),a=Xl(c[0],r),l=a===0?e:e.map(v=>V(v,-a)),f=a===0?c:Zt(l),m=s.map(v=>o?rt(Et(f,v),i):Et(f,v)),u=m.map(v=>v[0]+v[1]),h=l;if(t==="yates"){let v=V(.5,-a),E=l[0]-u[0];E>V(.25,-a)?h=[l[0]-v,l[1]+v]:-E>V(.25,-a)&&(h=[l[0]+v,l[1]-v])}let p=Ql(h,m);if(!o&&!Number.isNaN(p)){let v=f[0]+f[1];p-=v*(i[0]-1+i[1])}let w=Number.isNaN(p)?["NaNs produced"]:[],d=t==="williams"?1+(r+1)/(6*V(f[0]+f[1],a)):1,b=V(2*p,a)/d,N=u.map(v=>V(v,a));return{method:`${Gl} goodness of fit test`,statistic:b,statisticName:"G",parameter:r-1,parameterName:"X-squared df",pValue:Xa(b,r-1),observed:a===0?h:h.map(v=>V(v,a)),expected:N,ftResiduals:Ao(e,N),warnings:w}}function xp(e){if(typeof e!="object"||e===null||!Ga(e))throw new RangeError("'x' must be square with at least two rows and columns");let t=Ya(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 Rp(e){let t=xp(e),n=Zt(t.flat())[0],r=n>2**400||n>0&&n<2**-400?Math.round(Math.log2(n)):0,o=r===0?t:t.map(g=>g.map(y=>V(y,-r))),s=o.length,i=o.map(g=>Zt(g)),c=o.map((g,y)=>Zt(o.map(x=>x[y]))),a=o.map((g,y)=>o[y].some((x,M)=>M!==y&&x>0)||o.some((x,M)=>M!==y&&x[y]>0)).flatMap((g,y)=>g?[y]:[]),l=a.length;if(l<2)throw new RangeError("Too many equal marginals, cannot compute");let f=a.map((g,y)=>y),m=g=>f[g]===g?g:f[g]=m(f[g]);for(let g=0;g<l;g++)for(let y=g+1;y<l;y++)o[a[g]][a[y]]+o[a[y]][a[g]]>0&&(f[m(g)]=m(y));if(a.some((g,y)=>m(y)!==m(0)))throw new RangeError("the categories fall into groups without counts between them: the covariance matrix is singular");let u=l-1,h=a.slice(0,u).map(g=>{let y=he(i[g],[-c[g][0],-c[g][1]]);return y[0]+y[1]}),p=a.slice(0,u).map(g=>a.slice(0,u).map(y=>{if(g!==y)return-(o[g][y]+o[y][g]);let x=0;for(let M=0;M<s;M++)M!==g&&(x+=o[g][M]+o[M][g]);return x})),w=p.map(g=>g.map(()=>0)),d=[];for(let g=0;g<u;g++){let y=p[g][g];for(let x=0;x<g;x++)y-=w[g][x]*w[g][x]*d[x];d.push(y);for(let x=g+1;x<u;x++){let M=p[x][g];for(let R=0;R<g;R++)M-=w[x][R]*w[g][R]*d[R];w[x][g]=M/y}}let b=0,N=[];for(let g=0;g<u;g++){let y=h[g];for(let x=0;x<g;x++)y-=w[g][x]*N[x];N.push(y),b+=y*y/d[g]}let v=Zt(a.flatMap(g=>a.map(y=>t[g][y]))),E=l-1;return b=V(b,r),Q("stuartMaxwellTest",{method:"Stuart-Maxwell test",statistic:b,statisticName:"chi-squared",parameter:E,parameterName:"df",pValue:Xa(b,E),N:v[0]+v[1],warnings:[]})}function Tp(e,t={}){B(t,vp,"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=Zl(e),o=Zt(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(g=>g.map(y=>V(y,-s))),{pc:c,pd:a}=Sp(i),l=[0,0],f=[0,0];i.forEach((g,y)=>g.forEach((x,M)=>{l=he(l,Et(c[y][M],x)),f=he(f,Et(a[y][M],x))})),l=[l[0]/2,l[1]/2],f=[f[0]/2,f[1]/2];let m=he(l,f),u=rt(Le(l,f),m)[0],h=_e(m,m),p=i.map((g,y)=>g.map((x,M)=>2*rt(Le(_e(f,c[y][M]),_e(l,a[y][M])),h)[0])),w=Math.max(...p.flat().map(Math.abs)),d=w>0&&w<1/0?Math.floor(Math.log2(w)):0,b=0;i.forEach((g,y)=>g.forEach((x,M)=>{let R=V(p[y][M],-d);b+=x*R*R}));let N=V(Math.sqrt(b),d-s/2),v=(1-n)/2,E=v<1e-8?le(v,{lowerTail:!1}):le(1-v);return Q("goodmanKruskalGamma",{estimate:u,confInt:[Math.max(-1,u-E*N),Math.min(1,u+E*N)],confLevel:n,ase:N,warnings:[]})}function Zl(e){if(typeof e!="object"||e===null||!Ga(e))throw new TypeError("'table' must be an array of rows");let t=Ya(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 Sp(e){let t=e.length,n=e[0].length,r=[0,0],o=e.map(p=>{let w=[],d=r;for(let b=0;b<n;b++)w.push(d),d=he(d,[p[b],0]);return w}),s=e.map(p=>{let w=new Array(n),d=r;for(let b=n-1;b>=0;b--)w[b]=d,d=he(d,[p[b],0]);return w}),i=p=>{let w=[],d=new Array(n).fill(r);for(let b=0;b<t;b++)w.push(d),d=d.map((N,v)=>he(N,p[b][v]));return w},c=p=>{let w=new Array(t),d=new Array(n).fill(r);for(let b=t-1;b>=0;b--)w[b]=d,d=d.map((N,v)=>he(N,p[b][v]));return w},a=i(o),l=c(s),f=i(s),m=c(o),u=a.map((p,w)=>p.map((d,b)=>he(d,l[w][b]))),h=f.map((p,w)=>p.map((d,b)=>he(d,m[w][b])));return{pc:u,pd:h}}function Ap(e,t={}){B(t,Np,"lblTest");let n=Ze(t.alternative),r=Zl(e),o=r.length,s=r[0].length,i=(R,T,S)=>{if(R===void 0)return Array.from({length:T},(C,L)=>L+1);let A=Array.from(R);if(A.length!==T)throw new RangeError(`'${S}' must have one score for each ${S==="rowScores"?"row":"column"}`);if(A.some(C=>typeof C!="number"||!Number.isFinite(C)))throw new RangeError(`'${S}' must be finite numbers`);return A},c=i(t.rowScores,o,"rowScores"),a=i(t.colScores,s,"colScores"),f=Zt(r.flat())[0]>2**1e3?2*Math.ceil((Math.log2(Math.max(...r.flat()))-990)/2):0,m=f===0?r:r.map(R=>R.map(T=>V(T,-f))),u=m.map(R=>Zt(R)).map(R=>R[0]+R[1]),h=m[0].map((R,T)=>Zt(m.map(S=>S[T]))).map(R=>R[0]+R[1]),p=u.reduce((R,T)=>R+T,0);if(!(f>0||p>1))throw new RangeError("the table must have a total above 1");let w=u.reduce((R,T,S)=>R+T*c[S],0)/p,d=h.reduce((R,T,S)=>R+T*a[S],0)/p,b=c.map(R=>R-w),N=a.map(R=>R-d),v=u.reduce((R,T,S)=>R+T*b[S]*b[S],0),E=h.reduce((R,T,S)=>R+T*N[S]*N[S],0),g=0;if(m.forEach((R,T)=>R.forEach((S,A)=>g+=S*b[T]*N[A])),!(v>0&&E>0))throw new RangeError("the row and column scores must vary over the observations (the variance of the statistic is 0)");let y=g/Math.sqrt(v)/Math.sqrt(E),x=f===0?Math.sqrt(p-1)*y:V(Math.sqrt(p-V(1,-f))*y,f/2),M=n==="less"?se(x):n==="greater"?se(x,{lowerTail:!1}):2*se(-Math.abs(x));return Q("lblTest",{method:"Linear-by-Linear Association Test",alternative:n,statistic:x,statisticName:"Z",pValue:M,warnings:[]})}var Jl=["correct","p","rescaleP","simulatePValue","B","nMinus1"];function gn(e){let t=[0,0],n=0;for(let r of e)t=he(t,[r,0]),n+=r;return Number.isFinite(n)?t[0]:n}function qp(e,t,n){return ct(t)&&B(t,Jl,"chisqTest"),B(n,Jl,"chisqTest"),Array.isArray(t)?Q("chisqTest",kp(e,t,n??{})):Q("chisqTest",Ip(e,t??{}))}var nf="Chi-squared approximation may be incorrect";function kp(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(Tn),c=[...new Set(s)].sort(Tn);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])]++),{...cf(f,n),rowLevels:i,colLevels:c}}function Ip(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?Qa(r[0],t):o===1?Qa(r.map(s=>s[0]),t):cf(r,t)}return Qa(Array.from(e),t)}function rf(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 of(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 sf(e,t){return Number.isNaN(e)?NaN:wt(e,t/2,2,!1,!1)}function af(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 Qa(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(w=>!(w>=0&&w<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=gn(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(w=>w<0))throw new RangeError("probabilities must be non-negative");if(o.some(w=>w===1/0))throw new RangeError("probabilities must be finite");if(t.rescaleP??!1){let w=gn(o),d=w<1/0?0:Math.ceil(Math.log2(n)),b=d===0?o:o.map(v=>V(v,-d)),N=d===0?w:gn(b);o=b.map(v=>v/N)}else if(Math.abs(gn(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(w=>V(w,-s)),c=s===0?r:gn(i),a=o.map(w=>c*w),l=s===0?a:a.map(w=>V(w,s));af(l);let f=o.map((w,d)=>a[d]*(1-w)),m=V(gn(i.map((w,d)=>Vt(w-a[d],a[d]))),s),u="Chi-squared test for given probabilities",h=n-1,p;if(t.simulatePValue??!1){let w=rf(t);if(!Number.isInteger(r))throw new RangeError("the simulation needs counts summing to an integer");u+=No(w),h=NaN,p=of(_p(r,o,l,w),m,w)}else p=sf(m,h);return{method:u,statistic:m,statisticName:"X-squared",parameter:h,parameterName:"df",pValue:p,observed:e,expected:l,residuals:i.map((w,d)=>V((w-a[d])/Math.sqrt(a[d]),s/2)),stdres:i.map((w,d)=>V((w-a[d])/Math.sqrt(f[d]),s/2)),ftResiduals:Ao(e,l),warnings:Number.isFinite(h)&&l.some(w=>w<5)?[nf]:[]}}function _p(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=xl(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 Lp(e,t,n,r){let o=e.length,s=t.length,i=e.reduce((f,m)=>f+m,0);if(i>ps)throw new RangeError("the simulation needs a total count of at most 16777216 (2^24)");let c=ds(i),a=new Int32Array(o*s),l=new Float64Array(Math.trunc(r));for(let f=0;f<l.length;f++){gs(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 cf(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=gn(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=>gn(v)),l=e[0].map((v,E)=>gn(e.map(g=>g[E]))),f=a.map(v=>l.map(E=>ms(v,E,c)));af(f.flat());let m=e.map((v,E)=>v.map((g,y)=>g-f[E][y])),u="Pearson's Chi-squared test",h,p=(o-1)*(s-1),w,d=(t.simulatePValue??!1)&&a.every(v=>v>0)&&l.every(v=>v>0),b=!d&&r&&o===2&&s===2?Math.min(.5,...m.flat().map(Math.abs)):0,N=[];if((t.simulatePValue??!1)&&!d&&N.push("cannot compute simulated p-value with zero marginals"),d){if(e.some(g=>g.some(y=>!Number.isInteger(y))))throw new RangeError("the simulation needs integer counts");let v=rf(t);u+=No(v),h=gn(m.flatMap((g,y)=>g.map((x,M)=>Vt(x,f[y][M])))),p=NaN;let E=Lp(a.map(g=>Math.trunc(g)),l.map(g=>Math.trunc(g)),f,v);w=of(E,h,v)}else if(b>0)u+=" with Yates' continuity correction",h=gn(m.flatMap((v,E)=>v.map((g,y)=>Vt(Math.abs(g)-b,f[E][y]))));else if(h=gn(m.flatMap((v,E)=>v.map((g,y)=>Vt(g,f[E][y])))),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:w??sf(h,p),observed:e,expected:f,residuals:m.map((v,E)=>v.map((g,y)=>g/Math.sqrt(f[E][y]))),stdres:Cp(e,c,a,l,f,m),ftResiduals:Ao(e,f),warnings:Number.isFinite(p)&&f.some(v=>v.some(E=>E<5))?[...N,nf]:N}}function Cp(e,t,n,r,o,s){if(!Number.isFinite(t))return s.map((p,w)=>p.map((d,b)=>d/Math.sqrt(o[w][b]*(1-n[w]/t)*(1-r[b]/t))));let i=e.map(p=>ef(p)),c=e[0].map((p,w)=>ef(e.map(d=>d[w]))),a=Op(i),l=tf(i),f=tf(c),m=l.map(p=>rt(p,a)),u=c.map(p=>rt(p,a)),h=f.map(p=>rt(p,a));return e.map((p,w)=>p.map((d,b)=>{let N=_e(i[w],u[b]),v=Le([d,0],N),E=v[0]+v[1],g=_e(_e(N,m[w]),h[b]),y=g[0]+g[1];if(y>=Fp||E===0)return E/Math.sqrt(y);let x=N[0]+N[1],M=m[w][0]+m[w][1],R=h[b][0]+h[b][1];return E/Math.sqrt(x)/Math.sqrt(M)/Math.sqrt(R)}))}var Fp=2**-1022;function ef(e){let t=[0,0];for(let n of e)t=he(t,[n,0]);return t}function Op(e){let t=[0,0];for(let n of e)t=he(t,n);return t}function tf(e){let t=e.length,n=new Array(t+1);n[t]=[0,0];for(let s=t-1;s>=0;s--)n[s]=he(e[s],n[s+1]);let r=[],o=[0,0];for(let s=0;s<t;s++)r.push(he(o,n[s+1])),o=he(o,e[s]);return r}var Dp=Math.pow(Number.EPSILON,.25);function Kt(e,t,n,r={}){let o={tol:r.tol??Dp,maxiter:r.maxIter??1e3};return r.fLower!==void 0&&(o.fLower=r.fLower),r.fUpper!==void 0&&(o.fUpper=r.fUpper),br(e,t,n,o).root}function rr(e,t){let n=a=>e(Math.exp(a)),r=0,o=n(0),s=t?-1:1,i=n(s);for(;i!==0&&Math.sign(i)===Math.sign(o);){if(Math.abs(s)>745)return t?0:1/0;r=s,o=i,s*=2,i=n(s)}if(i===0)return Math.exp(s);let c=r<s?Kt(n,r,s,{tol:2**-50,fLower:o,fUpper:i}):Kt(n,s,r,{tol:2**-50,fLower:i,fUpper:o});return Math.exp(c)}var Pp=["alternative","confInt","confLevel","or","simulatePValue","B","midp"];function Io(e,t={}){B(t,Pp,"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(cl));if(s.flat().some(f=>f>2147483647))throw new RangeError("'x' has entries too large to be integer");let i=[],c=Vp(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 Q("fisherTest",{...Wp(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 Q("fisherTest",{method:a+No(f),alternative:l,pValue:$p(s,f),warnings:i})}return Q("fisherTest",{method:a,alternative:l,pValue:Bp(s),warnings:i})}function Vp(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: ${Rl(a)}`:null}function $p(e,t){let n=e.filter(d=>d.some(b=>b>0)),r=e[0].map((d,b)=>b).filter(d=>e.some(b=>b[d]>0));if(n.length<=1)throw new RangeError("need 2 or more non-zero row marginals");if(r.length<=1)throw new RangeError("need 2 or more non-zero column marginals");let o=n.map(d=>r.map(b=>d[b])),s=o.map(d=>d.reduce((b,N)=>b+N,0)),i=o[0].map((d,b)=>o.reduce((N,v)=>N+v[b],0)),c=s.length,a=i.length,l=[0,0];for(let d of o.flat())l=he(l,[-us(d),0]);let f=l[0]/(1+64*Number.EPSILON),m=s.reduce((d,b)=>d+b,0);if(m>ps)throw new RangeError("the simulation needs a total count of at most 16777216 (2^24)");let u=ds(m),h=new Int32Array(c*a),p=0,w=Math.trunc(t);for(let d=0;d<w;d++){gs(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)/(w+1)))}var lf=1+1e-7,zp=16,jp=2**24;function qo(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=he(r,[e[o],0]));return r[0]}var Br=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%zp===0||!(s>0&&s<1/0)?(o=bn(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=qo(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=he(r,[(this.lo+o)*n[o],0]));return r[0]}tail(t,n,r){if(n===1)return r?sn(t-1,this.m,this.n,this.k,!1,!1):sn(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?qo(o,s,o.length-1):qo(o,0,s)}midTail(t,n,r){if(n===1){let i=.5*bn(t,this.m,this.n,this.k,!1);return(r?sn(t,this.m,this.n,this.k,!1,!1):sn(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?qo(o,s+1,o.length-1):qo(o,0,s-1))+.5*o[s]}};function Ja(e,t,n){let r=e.density(n),o=r[t-e.lo]*lf,s=[0,0];for(let i of r)i<=o&&(s=he(s,[i,0]));return s[0]}function ff(e,t,n,r){let o=Math.max(0,r-n),s=Math.min(r,t),i=w=>bn(w,t,n,r,!0),c=i(e)+Math.log(lf),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=(w,d)=>{let b=w,N=d+1;for(;N-b>1;){let v=b+Math.floor((N-b)/2);i(v)<=c?b=v:N=v}return b},f=(w,d)=>{let b=w-1,N=d+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:sn(m,t,n,r,!0,!1),p=u>s?0:sn(u-1,t,n,r,!1,!1);return h+p}function ko(e,t){return rr(e,t)}function Wp(e,t,n){let r=Ze(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>jp)throw new RangeError(`the exact distribution of the 2 x 2 table has ${m} values, more than 2^24`);let u=new Br(i+a,c+l,i+c),{lo:h,hi:p}=u,w=t.midp??!1,d;w?(r==="less"?d=u.midTail(f,o,!1):r==="greater"?d=u.midTail(f,o,!0):d=Math.min(1,2*Math.min(u.midTail(f,o,!1),u.midTail(f,o,!0))),n=r==="two.sided"?"Central Fisher's Exact Test (mid-p version)":"One-sided Fisher's Exact Test (mid-p version)"):r==="less"?d=u.tail(f,o,!1):r==="greater"?d=u.tail(f,o,!0):o===0?d=f===h?1:0:o===1/0?d=f===p?1:0:d=Ja(u,f,o);let b;if(f===h)b=0;else if(f===p)b=1/0;else{let y=u.mean(1);b=y===f?1:ko(x=>u.mean(x)-f,y>f)}let N={method:n,alternative:r,pValue:d,estimate:b,estimateNames:"odds ratio",nullValue:o,nullValueName:"odds ratio",warnings:[]};if(!(t.confInt??!0))return N;let v=y=>{if(f===p)return 1/0;if(w){if(f===h&&y>=.5)return 0;let M=u.midTail(f,1,!1);return M===y?1:ko(R=>u.midTail(f,R,!1)-y,M<y)}let x=u.tail(f,1,!1);return x===y?1:ko(M=>u.tail(f,M,!1)-y,x<y)},E=y=>{if(f===h)return 0;if(w){if(f===p&&y>=.5)return 1/0;let M=u.midTail(f,1,!0);return M===y?1:ko(R=>u.midTail(f,R,!0)-y,M>y)}let x=u.tail(f,1,!0);return x===y?1:ko(M=>u.tail(f,M,!0)-y,x>y)},g;if(r==="less")g=[0,v(1-s)];else if(r==="greater")g=[E(1-s),1/0];else{let y=(1-s)/2;g=[E(y),v(y)]}return{...N,confInt:g,confLevel:s}}var Is=[[0,0]];function Sn(e){for(let t=Is.length;t<=e;t++)Is.push(he(Is[t-1],rl([t,0])));return Is[e]}function _s(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 Za=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 Bp(e){let t=e.filter(v=>v.some(E=>E>0));if(t.length===0)throw new RangeError("all entries of the table are zero");let n=t[0].map((v,E)=>t.some(g=>g[E]>0));if(t=t.map(v=>v.filter((E,g)=>n[g])),t.length<2||t[0].length<2)return 1;t.length>t[0].length&&(t=t[0].map((v,E)=>t.map(g=>g[E])));let r=t.length,o=t.map(v=>v.reduce((E,g)=>E+g,0)),s=t[0].map((v,E)=>t.reduce((g,y)=>g+y[E],0)).sort((v,E)=>E-v),i=s.length,c=o.reduce((v,E)=>v+E,0),a=nl(Sn(c));for(let v of o)a=he(a,Sn(v));for(let v of s)a=he(a,Sn(v));let l=[0,0];for(let v of t.flat())l=Le(l,Sn(v));let f=he(l,[Math.log1p(1e-7),0]),m=1e-9*(1+Math.abs(f[0])),[u,h]=f,p=Array.from({length:i},()=>new Map),w=(v,E)=>{let g=E.join(","),y=p[v].get(g);if(y!==void 0)return y;let x=he(a,Sn(E.reduce((C,L)=>C+L,0)));for(let C of E)x=Le(x,Sn(C));for(let C=v;C<i;C++)x=Le(x,Sn(s[C]));let M={R:E,KZ:x,max:0,min:0,edges:[],pastHi:[],pastLo:[],count:[],index:new Map};if(p[v].set(g,M),v===i-1){let C=[0,0];for(let L of E)C=Le(C,Sn(L));return M.max=M.min=C[0],M.edges.push({to:M,hi:C[0],lo:C[1],mult:1}),M}let R=new Map,T=new Array(r+1).fill(0);for(let C=r-1;C>=0;C--)T[C]=T[C+1]+E[C];let S=new Array(r).fill(0),A=(C,L,k)=>{if(C===r-1){S[C]=L;let O=Le(k,Sn(L)),q=E.map((K,X)=>K-S[X]).sort((K,X)=>K-X),$=w(v+1,q),_=`${q.join(",")}|${O[0]}`,P=R.get(_);P===void 0?R.set(_,{to:$,hi:O[0],lo:O[1],mult:1}):P.mult++;return}let F=Math.max(0,L-T[C+1]),I=Math.min(E[C],L);for(let O=F;O<=I;O++)S[C]=O,A(C+1,L-O,Le(k,Sn(O)))};if(A(0,s[v],[0,0]),M.edges=[...R.values()],M.max=Nt(M.edges.map(C=>C.hi+C.to.max)),M.min=an(M.edges.map(C=>C.hi+C.to.min)),v===i-2){let C=M.edges.map($=>{let _=$.to.edges[0];return{v:he([$.hi,$.lo],[_.hi,_.lo]),mult:$.mult}});C.sort(($,_)=>$.v[0]-_.v[0]||$.v[1]-_.v[1]);let L=C.length,k=C[L-1].v[0],F=new Float64Array(L),I=new Float64Array(L),O=new Float64Array(L),q=[0,0];C.forEach(({v:$,mult:_},P)=>{F[P]=$[0],I[P]=$[1];let K=Le($,[k,0]);q=he(q,[_*Math.exp(K[0])*(1+K[1]),0]),O[P]=q[0]}),M.dist={hi:F,lo:I,cum:O,vmax:k,kv:he(a,[k,0])}}return M},d=w(0,[...o].sort((v,E)=>v-E)),b=new Za,N=(v,E,g,y)=>{if(E+v.max<=u-m){let[A,C]=_s(v.KZ[0],v.KZ[1],E,g);return b.add(A,C,y),!0}if(E+v.min>u+m)return!0;let x=v.dist;if(x===void 0)return!1;let[M,R]=_s(u,h,-E,-g),T=0,S=x.hi.length;for(;T<S;){let A=T+S>>>1,C=x.hi[A];C<M||C===M&&x.lo[A]<=R?T=A+1:S=A}if(T>0){let[A,C]=_s(x.kv[0],x.kv[1],E,g);b.add(A,C,y*x.cum[T-1])}return!0};N(d,0,0,1)||(d.pastHi.push(0),d.pastLo.push(0),d.count.push(1));for(let v=0;v<i-2;v++)for(let E of p[v].values()){let{pastHi:g,pastLo:y,count:x}=E;if(x.length!==0){for(let{to:M,hi:R,lo:T,mult:S}of E.edges)for(let A=0;A<x.length;A++){let[C,L]=_s(g[A],y[A],R,T),k=x[A]*S;if(N(M,C,L,k))continue;let F=M.index.get(C);F===void 0?(M.index.set(C,M.count.length),M.pastHi.push(C),M.pastLo.push(L),M.count.push(k)):M.count[F]+=k}E.pastHi=E.pastLo=E.count=[],E.index.clear()}}return Math.min(1,b.value())}function Ls(e){let t=e.every(n=>Number.isInteger(n))?1:2;return[t,e.map(n=>n*t).sort((n,r)=>n-r)]}function ti(e){return[...e].sort((t,n)=>t-n).join(",")}function ei(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 ni(e){let[,t]=Ls(e),n=0,r=0;for(let o of t)r+=n+=o;return{steps:r,size:n+1}}function ri(e){let[t,n]=Ls(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 ei(t,0,o)}function oi(e,t){let[,n]=Ls(e),r=n.length,o=Math.min(t,r-t),s=mf(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 mf(e){let t=[0];for(let n of e)t.push(t[t.length-1]+n);return t}function si(e,t){let[n,r]=Ls(e),o=r.length,s=Math.min(t,o-t),i=mf(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 w=Math.min(h,s);w>=Math.max(1,s-(o-h));w--){let d=c[w],b=c[w-1],N=w/h,v=(h-w)/h,E=i[w],g=i[h]-i[h-w],y=i[w-1]+p,x=Math.max(E,y);for(let M=g;M>=x;M--)d[M-E]=v*d[M-E]+N*b[M-y];for(let M=x-1;M>=E;M--)d[M-E]=v*d[M-E]}}let a=c[s],l=t*(t+1)/2;if(s===t)return ei(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 ei(n,(i[o]-u)/n-l,m)}function or(e,t){let n=Math.ceil(e.f*(t-e.offset)+e.f*1e-8)-1;return n<0?0:n>=e.lower.length?1:Math.min(1,e.lower[n])}function yr(e,t){let n=or(e,t);if(Math.abs(n-1)>=1/64)return 1-n;let r=Math.ceil(e.f*(t-e.offset)+e.f*1e-8);return r>=e.upper.length?0:Math.min(1,e.upper[Math.max(0,r)])}var uf=["alternative","mu","paired","exact","correct","confInt","confLevel","tolRoot"];function _o(e,t,n={}){if(typeof e!="string"&&ct(t)&&B(t,uf,"wilcoxTest"),B(n,uf,"wilcoxTest"),typeof e=="string"){if(n.paired!==void 0)throw new Error("cannot use 'paired' in formula method");let w=bs(e,t,!0);return w.kind==="one"?_o(w.x,n):_o(w.x,w.y,w.kind==="paired"?{...n,paired:!0}:n)}let r;t!==void 0&&!ArrayBuffer.isView(t)&&!Array.isArray(t)?n=t:r=t;let o=Ze(n.alternative),s=n.mu??0,i=n.paired??!1,c=n.exact??null,a=Up(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[w,d]=Jn(Array.from(e),Array.from(r));u=Dt(w.map((b,N)=>b-d[N]))}else u=Dt(Array.from(e)),r!==void 0&&(h=Dt(Array.from(r)));if(u.length<1)throw new Error("not enough (non-missing) 'x' observations");if(h!==void 0&&h.length<1)throw new Error("not enough (non-missing) 'y' observations");let p={alternative:o,mu:s,correct:a,confInt:l,confLevel:f,tolRoot:m,warnings:new Rt};return Q("wilcoxTest",h===void 0?Qp(u,c,i,p):sd(u,h,c,p))}function Up(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 Cs(e,t,n){return n?t==="two.sided"?Math.sign(e)*.5:t==="greater"?.5:-.5:0}var df=10*Number.EPSILON;function gf(e,t,n=0){let r=a=>Math.min(1,Math.max(a,0)),o=()=>n===0?se(e):r(se(e)-n*Xt(e)),s=()=>n===0?se(e,{lowerTail:!1}):r(se(e,{lowerTail:!1})+n*Xt(e));if(t==="less")return o();if(t==="greater")return s();let i=o();if(i<=.5)return 2*i;let c=s();return c>=1/64?2*(1-i):2*c}function Hp(e){let t=e*e;return[e*(t-3),e*(t*(t-10)+15),e*(t*(t*(t-21)+105)-105)]}function Yp(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 wf(e,o,s,n)}function Gp(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 wf(e,i,c,r)}function wf(e,t,n,r){let[o,s,i]=Hp(e),c=t*o;return r>=2&&(c+=n*s),r>=3&&(c+=t*t/2*i),c}function ci(e){let t=e.length;if(t===0)return NaN;let n=Math.floor(t/2);return t%2===1?e[n]:it([e[n-1],e[n]])}var li=(e,t)=>e-t,ai=5e9,Xp=40;function ii(e){return new RangeError(`wilcoxTest: the exact conditional ${e} would take too long or too much memory: use exact: false`)}function Fs(e,t){if(e.size>16777216||e.steps>ai)throw ii(t);return e.steps}function bf(e){let t=e.length,n=bt(e.map(Math.abs)),r=0;for(let i=0;i<t;i++)e[i]>0&&(r+=n[i]);let o=0;for(let i of wn(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 Qp(e,t,n,r){let{alternative:o,mu:s}=r,i=e.map(g=>g-s);i.some((g,y)=>!Number.isFinite(g)&&Number.isFinite(e[y]))&&(i=e.map(g=>g/2-s/2));let a=i.some(g=>g===0),l=t??i.length<50,f=(g,y,x)=>({method:g,alternative:o,statistic:y,statisticName:"V",pValue:x,nullValue:s,nullValueName:n?"location shift":"location",warnings:r.warnings.list});if(l){let g=bt(i.map(Math.abs)),y=wn(g).length>0,x=0;for(let C=0;C<i.length;C++)i[C]>0&&(x+=g[C]);let M="Wilcoxon signed rank exact test";if(!y&&!a){let C=i.length,L=C*(C+1)/2,k;if(o==="less")k=$r(x,C,!0,!1);else if(o==="greater")k=$r(x-1,C,!1,!1);else{let I=x>L/2?$r(x-1,C,!1,!1):$r(x,C,!0,!1);k=Math.min(1,2*I)}let F=f(M,x,k);return r.confInt?{...F,...Zp(e,r)}:F}let R=g.filter((C,L)=>i[L]!==0);Fs(ni(R),"distribution");let T=ri(R),S;if(o==="less")S=or(T,x);else if(o==="greater")S=yr(T,x-.25);else{let C=0;for(let L of R)C+=L/2;S=Math.min(1,2*(x>C?yr(T,x-.25):or(T,x)))}let A=f(M,x,S);return r.confInt?{...A,...ed(e,r)}:A}let m=a?i.filter(g=>g!==0):i,u=m.length;if(u===0)throw new RangeError("no non-zero differences for the signed rank test");let[h,p,w]=bf(m),b=wn(bt(m.map(Math.abs))).length>0||a?Math.min(r.correct,0):r.correct,N=(p-Cs(p,o,b>=0))/w,v=gf(N,o,b>=1?Yp(N,u,b):0),E=f(`Wilcoxon signed rank test${r.correct>=0?" with continuity correction":""}`,h,v);return r.confInt?{...E,...td(e,r)}:E}function fi(e,t,n){return e==="less"?[-1/0,n]:e==="greater"?[t,1/0]:[t,n]}function hf(e,t,n){let r={confInt:fi(e,NaN,NaN),confLevel:0,estimate:t};return n!==void 0&&(r.estimateNames=n),r}function vf(e){if(!(e>0&&e<1/0))throw new RangeError("tolRoot must be a positive finite number")}function Nf(e){let t=e.length,n=[];for(let r=0;r<t;r++)for(let o=r;o<t;o++){let s=dn(e[r],e[o]);Number.isNaN(s)||n.push(s)}return n.sort(li)}function yf(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+df&&l++;let f=ci(e);if(l===0)return{confInt:[-1/0,1/0],confLevel:1,estimate:f,estimateNames:o};let m=t(l-1);return{confInt:fi(s,e[l-1],e[c-l]),confLevel:1-(s==="two.sided"?2*m:m),estimate:f,estimateNames:o}}function Zp(e,t){let n=e.length;return yf(Nf(e),r=>$r(r,n,!0,!1),r=>dl(r,n,!0,!1),t,"(pseudo)median")}function Mf(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 Ef(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)+df,w=f!=="less",d=f!=="greater",b=k=>dn(e[k-1],e[k]),N=Math.min(u-1,Math.max(1,Math.ceil(i.mean+le(Math.min(p,1))*i.sd+.1*i.sd+3))),v=8,E=k=>k===0?e[0]-1:b(k),g=k=>k===0?e[u-1]+1:b(u-k),y=[new Map,new Map];{let k=new Map,F=0,I=Xp*c;for(let[O,q]of[[0,E],[1,g]])if(!(O===0?!w:!d))for(let $=0;$<=N;$++){if(F+=I,F>ai)throw ii("interval");let _;try{_=t(q($))}catch{break}if(y[O].set($,[_.stat,_.key]),k.has(_.key)){k.delete(_.key),k.set(_.key,!0);continue}if(F+=r(_.scores),F>ai)throw ii("interval");k.set(_.key,!0),k.size>v&&k.delete(k.keys().next().value)}}let x=new Map,M=(k,F)=>{let I=k===0?E(F):g(F),O=y[k].get(F),q;if(O===void 0){let K=t(I);O=[K.stat,K.key],q=K.scores}let[$,_]=O,P=x.get(_);return P!==void 0?x.delete(_):(P=n(q??t(I).scores),x.size>=v&&x.delete(x.keys().next().value)),x.set(_,P),[$,P]},R=k=>{let[F,I]=M(0,k);return o(I,F)},T=k=>{let[F,I]=M(1,k);return s(I,F)},S=-1/0,A=0;if(w){let k=R(0);if(!(k>p)){let F=k,I=1;for(;I<=u-1;I++){let O=R(I);if(O>p)break;F=O}S=e[I-1],A=F}}let C=1/0,L=0;if(d){let k=T(0);if(!(k>p)){let F=k,I=u-1;for(;I>=1;I--){let O=T(u-I);if(O>p)break;F=O}C=e[I],L=F}}return{confInt:[S,C],confLevel:1-(A+L),estimate:ci(e),estimateNames:l}}function Jp(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 ed(e,t){let n=e.length,r=s=>{let i=Jp(e,s),{ranks:c,key:a}=Mf(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 Ef(Nf(e),r,ri,s=>Fs(ni(s),"interval"),(s,i)=>yr(s,i-.25),or,{mean:o/2,sd:Math.sqrt(n*(n+1)*(2*n+1)/24)},n,t,"(pseudo)median")}function td(e,t){let{alternative:n,confLevel:r,tolRoot:o}=t,s=t.correct>=0,i=e.length;if(i===0)return hf(n,NaN);let c=an(e),a=Nt(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"),hf(n,dn(c,a),"midrange");let l=(v,E)=>{let[,g,y]=bf(e.map(x=>x-v).filter(x=>x!==0));return(g-Cs(g,n,E))/y},f=l(c,s),m=l(a,s),u=v=>-le(v),h=v=>n==="greater"?!(f<u(v)):n==="less"?!(m>le(v/2)):!(f<u(v/2))&&!(m>le(v/2)),p=1-r,w=!1;for(;!h(p)&&p<1;)p*=2,w=!0;w&&t.warnings.add("requested conf.level not achievable"),vf(o);let d=v=>Kt(E=>l(E,s)-v,c,a,{tol:o,fLower:f-v,fUpper:m-v}),b;if(p>=1){let v=ci([...e].sort(li));b=fi(n,v,v)}else n==="greater"?b=[d(u(p)),1/0]:n==="less"?b=[-1/0,d(le(p))]:b=[d(u(p/2)),d(le(p/2))];let N=Kt(v=>l(v,!1),c,a,{tol:o,fLower:l(c,!1),fUpper:l(a,!1)});return{confInt:b,confLevel:w?1-Math.min(1,p):r,estimate:N,estimateNames:"(pseudo)median"}}function xf(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 wn(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 nd(e,t){let[n,r,o]=xf(bt([...e,...t]),e.length);return[n,r,o]}function rd(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 od(e,t,n){return Rf(e,t,n).ranks}function Rf(e,t,n){let r=e.map(o=>o-n);if(r.some(o=>Number.isNaN(o))){let o=r.map((a,l)=>[a,l]).filter(([a])=>!Number.isNaN(a)),s=bt([...o.map(([a])=>a),...t]),i=new Array(e.length+t.length);o.forEach(([,a],l)=>i[a]=s[l]),t.forEach((a,l)=>i[e.length+l]=s[o.length+l]);let c=o.length+t.length;return r.forEach((a,l)=>{Number.isNaN(a)&&(i[l]=++c)}),{ranks:i,key:ti(i)}}if(Number.isFinite(n)&&r.some((o,s)=>!Number.isFinite(o)&&Number.isFinite(e[s]))){let o=rd(e.map(s=>s/2-n/2),t);return{ranks:o,key:ti(o)}}return Mf([...r,...t])}function sd(e,t,n,r){let{alternative:o,mu:s}=r,i=e.length,c=t.length,a=n??(i<50&&c<50),l=od(e,t,s),[f,m,u,h]=xf(l,i),p,w;if(a)if(w="Wilcoxon rank sum exact test",!h)p=pf(()=>o==="less"?zr(f,i,c,!0,!1):o==="greater"?zr(f-1,i,c,!1,!1):Math.min(2*zr(f,i,c,!0,!1),2*zr(f-1,i,c,!1,!1),1));else{Fs(oi(l,i),"distribution");let N=si(l,i);o==="less"?p=or(N,f):o==="greater"?p=yr(N,f-.25):p=Math.min(2*or(N,f),2*yr(N,f-.25),1)}else{w=`Wilcoxon rank sum test${r.correct>=0?" with continuity correction":""}`;let N=h?Math.min(r.correct,0):r.correct,v=l.every(g=>g===l[0]),E=(m-Cs(m,o,N>=0))/u;p=v?1:gf(E,o,N>=1?Gp(E,i,c,N):0)}let d={method:w,alternative:o,statistic:f,statisticName:"W",pValue:p,nullValue:s,nullValueName:"location shift",warnings:r.warnings.list};if(!r.confInt)return d;let b=a?h?id(e,t,r):pf(()=>ad(e,t,r)):cd(e,t,r);return{...d,...b}}function pf(e){try{return e()}catch(t){throw t instanceof RangeError&&t.message.startsWith("wilcox:")?new RangeError(`${t.message}: use exact: false`):t}}function Tf(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(li)}function ad(e,t,n){let r=e.length,o=t.length;return yf(Tf(e,t,!1),s=>zr(s,r,o,!0,!1),s=>wl(s,r,o,!0,!1),n,"difference in location")}function id(e,t,n){let r=e.length,o=t.length,s=i=>{let{ranks:c,key:a}=Rf(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 Ef(Tf(e,t,!0),s,i=>si(i,r),i=>Fs(oi(i,r),"interval"),(i,c)=>yr(i,c-.25),or,{mean:r*o/2,sd:Math.sqrt(r*o*(r+o+1)/12)},r+o,n,"difference in location")}function cd(e,t,n){let{alternative:r,confLevel:o,tolRoot:s}=n,i=n.correct>=0,c=an(e)-Nt(t),a=Nt(e)-an(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[,w,d]=nd(e.map(b=>b-h),t);return(w-Cs(w,r,p))/d},f=(h,p)=>{let w=l(c,p)-h,d=l(a,p)-h;if(Number.isNaN(w)||Number.isNaN(d))throw new Error("cannot compute the confidence interval because all observations are tied");return w<=0?c:d>=0?a:(vf(s),Kt(b=>l(b,p)-h,c,a,{tol:s,fLower:w,fUpper:d}))},m=1-o,u;return r==="greater"?u=[f(-le(m),i),1/0]:r==="less"?u=[-1/0,f(le(m),i)]:u=[f(-le(m/2),i),f(le(m/2),i)],{confInt:u,confLevel:o,estimate:f(0,!1),estimateNames:"difference in location"}}var ld=["method","n"],Sf=["holm","hochberg","hommel","bonferroni","BH","BY","fdr","none"];function fd(e){if(Sf.includes(e))return e;let t=Sf.filter(n=>n.startsWith(e));if(e.length===0||t.length!==1)throw new RangeError(`invalid method "${e}"`);return t[0]}function Kr(e,t={}){B(t,ld,"pAdjust");let n=typeof t=="string"?{method:t}:t,r=fd(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=qf(a,r,c),f=o.slice();return s.forEach((m,u)=>f[m]=l[u]),f}function Os(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 qf(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=Os(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=Os(e,!0),i=1/0;return s.forEach((c,a)=>{let l=r-a;i=Math.min(i,(n-l+1)*e[c]),o[c]=Math.min(1,i)}),o}case"BH":case"fdr":case"BY":{let s=1;if(t==="BY"){let m=Pn(),u=Math.floor(n);if(u<=1e6){for(let h=1;h<=u;h++)pn(m,1/h,u);s=Vn(m)}else{let h=u*u;s=Math.log(u)+.5772156649015329+1/(2*u)-1/(12*h)+1/(120*h*h)}}let i=s*n,c=Number.isFinite(i)?1:1024,a=Number.isFinite(i)?i:s*(n/1024),l=Os(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?qf(e,"hochberg",n):md(e,n)}}var Af=2**16;function md(e,t){if(!Number.isInteger(t))throw new RangeError("n must be an integer for the hommel method");if(t>Af)throw new RangeError(`n must be at most ${Af} for the hommel method (O(n^2) work)`);let n=e.length,r=[...e,...new Array(t-n).fill(1)],o=Os(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 kf=["p","alternative","confLevel"],ud=1+1e-7;function mi(e,t,n){let r=e-1;return e===0||e-r===1&&Ot(t,-r)[1]===0?Dn(r,t,n,!1,!1):Dn(e,t,n,!1,!1)+hn(e,t,n,!1)}function hd(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=hn(e,t,n,!1)*ud,s=a=>hn(a,t,n,!1)<=o;if(e<r){let a=ss(s,Math.ceil(r),t),l=a>t?0:mi(a,t,n);return Math.min(1,Dn(e,t,n,!0,!1)+l)}let i=as(s,0,Math.floor(r)),c=i<0?0:Dn(i,t,n,!0,!1);return Math.min(1,mi(e,t,n)+c)}function pd(e,t,n={}){B(t,kf,"binomTest"),B(n,kf,"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),!vn(e[0])||!vn(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(!vn(e))throw new RangeError("'x' must be nonnegative and integer");if(r=Math.round(e),!(vn(t)&&Math.round(t)>=r&&t>0))throw new RangeError("'n' must be a positive integer >= 'x'");o=Math.round(t)}return ui(r,o,n)}function ui(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=Ze(n.alternative),s=n.confLevel??.95;_t(s);let i;o==="less"?i=Dn(e,t,r,!0,!1):o==="greater"?i=mi(e,t,r):i=hd(e,t,r);let c=f=>e===0?0:Xn(f,e,t-e+1,!0,!1),a=f=>e===t?1:f<1e-8?Xn(f,e+1,t-e,!1,!1):Xn(1-f,e+1,t-e,!0,!1),l;if(o==="less")l=[0,a(1-s)];else if(o==="greater")l=[c(1-s),1];else{let f=(1-s)/2;l=[c(f),a(f)]}return{method:"Exact binomial test",alternative:o,statistic:e,statisticName:"number of successes",parameter:t,parameterName:"number of trials",pValue:i,confInt:l,confLevel:s,estimate:e/t,estimateNames:"probability of success",nullValue:r,nullValueName:"probability of success",warnings:[]}}var dd=["T","r","alternative","confLevel"],gd=1+1e-7;function wd(e,t){if(t===0)return e===0?1:0;if(e===t)return 1;let n=is(e,t,!1)*gd,r=i=>is(i,t,!1)<=n,o=Math.floor(t);if(e<t){let i=ss(r,Math.max(e+1,o),1/0);return Math.min(1,Vr(e,t,!0,!1)+hi(i,t))}let s=as(r,0,Math.min(e-1,o));return Math.min(1,Vr(s,t,!0,!1)+hi(e,t))}var Lo=2**-1022;function hi(e,t){return e-(e-1)===1?Vr(e-1,t,!1,!1):Vr(e,t,!1,!1)+is(e,t,!1)}function Mr(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 bd(e,t,n,r){let o=n+t*r,s=e*t*n/o;if(Number.isFinite(s)&&Number.isFinite(o)&&o>=Lo&&(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]=Mr(e),[f,m]=Mr(t),[u,h]=Mr(n),[p,w]=Mr(r),d=f*p,b=h-m-w,N=b>=0?u/(u+V(d,-b)):u/(V(u,b)+d);return V(a*f*N,l+m+Math.min(b,0))}function vd(e,t,n,r){let o=e/n,s=t/r,i=o/s;return t===0||Number.isFinite(o)&&Number.isFinite(s)&&s>=Lo&&(o===0||o>=Lo)?i:e===0?0:e/t*(r/n)}function Nd(e,t={}){B(t,dd,"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(vn))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=Ze(t.alternative),l=t.confLevel??.95;if(o===2?_t(l):cn(l),o===2){let[b,N]=r,[v,E]=i,g=b+N;if(g===1/0)throw new RangeError("'x' must be finite, nonnegative, and integer");let y=c*v,x=y+E,M=Number.isFinite(x)&&x>=Lo&&(y===0||y>=Lo)?y/x:1/(1+E/v/c),R=ui(Math.round(b),Math.round(b)+Math.round(N),{p:M,alternative:a,confLevel:l}),T=E/v,S=A=>{if(A===0)return 0;let C=A/(1-A);if(Number.isFinite(T)||C===1/0)return C*T;let[L,k]=Mr(C),[F,I]=Mr(v),[O,q]=Mr(E);return V(L*O/F,k+q-I)};return{method:"Comparison of Poisson rates",alternative:a,statistic:b,statisticName:"count1",parameter:bd(g,c,v,E),parameterName:"expected count1",pValue:R.pValue,confInt:[S(R.confInt[0]),S(R.confInt[1])],confLevel:l,estimate:vd(b,N,v,E),estimateNames:"rate ratio",nullValue:c,nullValueName:"rate ratio",warnings:[]}}let f=r[0],m=i[0],u=c*m,h;a==="less"?h=Vr(f,u,!0,!1):a==="greater"?h=hi(f,u):h=wd(f,u);let p=b=>f===0?0:os(b,f,1,!0,!1),w=b=>b<1e-8?os(b,f+1,1,!1,!1):os(1-b,f+1,1,!0,!1),d;if(a==="less")d=[0,w(1-l)];else if(a==="greater")d=[p(1-l),1/0];else{let b=(1-l)/2;d=[p(b),w(b)]}return{method:"Exact Poisson test",alternative:a,statistic:f,statisticName:"number of events",parameter:m,parameterName:"time base",pValue:h,confInt:[d[0]/m,d[1]/m],confLevel:l,estimate:f/m,estimateNames:"event rate",nullValue:c,nullValueName:"event rate",warnings:[]}}var If=["p","alternative","confLevel","correct"],yd="Chi-squared approximation may be incorrect";function pi(e,t,n={}){ct(t)&&B(t,If,"propTest"),B(n,If,"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 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,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!==i)throw new RangeError("'p' must have the same length as 'x' and 'n'");if(l=c.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;_t(m);let u=Ze(s.alternative);(a>2||a===2&&f)&&(u="two.sided");let h=r.map((S,A)=>S/o[A]),p=a===2?h[0]-h[1]:0,w=a===2?1/o[0]+1/o[1]:0,d=(s.correct??!0)&&a<=2?.5:0,b=!1;if(d>0&&a===1){let S=Math.abs(r[0]-o[0]*l[0]);S<=d&&([d,b]=[S,!0])}else if(d>0&&a===2&&!f){let S=Math.abs(p)/w;S<=d&&([d,b]=[S,!0])}let N=Md(r,o),v=o.some((S,A)=>{let C=l?.[A]??N;return S*C<5||S*(1-C)<5}),E=0;if(!b)for(let S=0;S<a;S++){let A=l?.[S]??N,C=o[S]*A,L=o[S]*(1-A);E+=Vt(Math.abs(r[S]-C)-d,C),E+=Vt(Math.abs(o[S]-r[S]-L)-d,L)}let g=l!==void 0?a:a-1,y;if(u==="two.sided")y=wt(E,g/2,2,!1,!1);else{let S=Math.sign(a===1?h[0]-l[0]:p);y=se(S*Math.sqrt(E),{lowerTail:u==="less"})}let x;if(a===1||a===2&&!f){let S=(1-m)/2,A=u==="two.sided"&&S<1e-8?le(S,{lowerTail:!1}):le(u==="two.sided"?(1+m)/2:m);if(a===1){let C=o[0],L=A*A,k=($,_)=>($+L/(2*C)+_*A*Math.sqrt($*(1-$)/C+L/(4*C*C)))/(1+L/C),F=h[0]+d/C,I=h[0]-d/C,O=Math.min(1,Math.max(0,F>=1?1:k(F,1))),q=Math.min(1,Math.max(0,I<=0?0:k(I,-1)));x=u==="two.sided"?[q,O]:u==="greater"?[q,1]:[0,O]}else{let C=Math.sqrt(h.reduce((I,O,q)=>I+O*(1-O)/o[q],0)),L=A*C+d*w,k=Math.max(-1,p-L),F=Math.min(1,p+L);x=u==="two.sided"?[k,F]:u==="greater"?[k,1]:[-1,F]}}let M=a===1?"p":c.map(S=>`prop ${S+1}`),T={method:`${a===1?"1-sample proportions test":`${a}-sample test for ${f?"given proportions":"equality of proportions"}`} ${d!==0?"with":"without"} continuity correction`,alternative:u,statistic:E,statisticName:"X-squared",parameter:g,parameterName:"df",pValue:y,estimate:a===1?h[0]:h,estimateNames:M,warnings:v?[yd]:[]};return l!==void 0&&(T.nullValue=a===1?l[0]:l,T.nullValueName=M),x!==void 0&&(T.confInt=x,T.confLevel=m),Q("propTest",T)}function Md(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+V(s,-r),0)/t.reduce((o,s)=>o+V(s,-r),0)}var Ed=["method","confLevel","sides"],xd=["wilson","wald","wilsoncc","agresti-coull","jeffreys","clopper-pearson","midp"],Rd=["two.sided","left","right"];function Co(e,t){let n=Se(t,Rd),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{...di(s,o),sides:n}}function di(e,t=1-e){let n=e/2<1e-8,r=n?le(e/2,{lowerTail:!1}):le(1-e/2),o=n?On(e,1,{lowerTail:!1}):On(t,1);return{sides:"two.sided",level:t,alpha:e,kappa:r,chisq:o}}function Ur(e,t){if(typeof e!="number")throw new TypeError(`'${t}' must be a number`);if(!vn(e))throw new RangeError(`'${t}' must be a nonnegative integer`);return Math.round(e)+0}function Bn(e,t,n,r){let o=Ur(e,n),s=Ur(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 Ps(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 Ds(e,t,n,r){return r?e/2<1e-8?Xn(e/2,t,n,!1,!1):Xn(1-e/2,t,n,!0,!1):Xn(e/2,t,n,!0,!1)}function _f(e,t,n,r){let o=n/2,s=r?f=>{let m=Math.exp(f);return .5*hn(e,t,m,!1)+Dn(e-1,t,m,!0,!1)-o}:f=>{let m=Math.exp(f);return .5*hn(e,t,m,!1)+Dn(e,t,m,!1,!1)-o},i=r?-1:1,c=Math.log(e/t),a=i*s(c);if(a===0)return e/t;if(a>0){let f=c,m=a,u=1;for(;;){let h=c-u;if(h<-745)return 0;let p=i*s(h);if(p<=0)return Math.exp(p===0?h:Kt(w=>i*s(w),h,f,{tol:2**-60,fLower:p,fUpper:m}));f=h,m=p,u*=2}}let l=i*s(0);return l<=0?1:Math.exp(Kt(f=>i*s(f),c,0,{tol:2**-60,fLower:a,fUpper:l}))}function gi(e,t,n={}){B(n,Ed,"binomCI");let r=Se(n.method,xd),o=Co(n.confLevel,n.sides),[s,i]=Bn(e,t,"x","n"),{alpha:c,kappa:a}=o,l=s/i,f=1-l,m,u;switch(r){case"wald":{let h=a*Math.sqrt(l*f/i);[m,u]=[l-h,l+h];break}case"wilson":[m,u]=Ps(s,i,a);break;case"wilsoncc":{let h=a*a,p=2*(i+h),w=2*s+h-1,d=a*Math.sqrt(h-2-1/i+4*l*(i*f+1));m=l===0?0:w-d<w/2?(2*s-1)/(2*i)*(2*s-1)/(w+d):(w-d)/p,u=l===1?1:(2*s+h+1+a*Math.sqrt(h+2-1/i+4*l*(i*f-1)))/p;break}case"agresti-coull":{let h=a*a,p=i+h,w=(s+h/2)/p,d=a*Math.sqrt(w*(1-w)/p);[m,u]=[w-d,w+d];break}case"jeffreys":m=s===0?0:Ds(c,s+.5,i-s+.5,!1),u=s===i?1:Ds(c,s+.5,i-s+.5,!0);break;case"clopper-pearson":m=s===0?0:Ds(c,s,i-s+1,!1),u=s===i?1:Ds(c,s+1,i-s,!0);break;case"midp":m=s===0?0:s===i?Math.exp(Math.log(c)/i):_f(s,i,c,!1),u=s===i?1:s===0?-Math.expm1(Math.log(c)/i):_f(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),Q("binomCI",{estimate:l,confInt:[m,u],confLevel:n.confLevel??.95,method:r,sides:o.sides,warnings:[]})}function Fo(e){return e===1/0?1:e/(1+e)}function Oo(e,t){return e===0?0:t>0?e*e/t:1/0}function Td(e,t,n,r){return e/t+n/r>1}function Sd(e,t,n,r){return $s(e,t,n,r,0)}function vi(e,t,n,r,o){if(Td(e,t,n,r)){let[p,w,d,b]=vi(t-e,t,r-n,r,-o);return[w,p,b,d]}let s=o<=0,[i,c,a,l]=s?[e,t,n,r]:[n,r,e,t],f=Math.abs(o),m=1-f,u=0;if(m>0){let p=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},w=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=Ad(e,t,n,r,o);u=s?v:v-o,u>b&&u<N||(u=N/2);for(let E=0;E<2e3;E++){let g=p(u);if(g===0)break;g>0?b=u:N=u;let y=u-g/w(u);if(y>b&&y<N||(y=b===0?N/2:N>2*b?Math.sqrt(b*N):b+(N-b)/2),!(y>b&&y<N))break;let x=Math.abs(y-u);if(u=y,x<=2**-53*u)break}}}let h=[u,1-u,Math.min(1,u+f),Math.max(0,m-u)];return s?h:[h[2],h[3],h[0],h[1]]}function Ad(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 w=(Math.PI+Math.acos(Math.max(-1,Math.min(1,h/(p*p*p)))))/3;return 2*p*Math.cos(w)-u}function Ni(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]=vi(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 qd(e,t,n,r,o,s,i){let c=Le(Le(ft([e,0],t),ft([n,0],r)),[o,0]),[a,l,f,m]=vi(e,t,n,r,o),u=he(ft(jt(a,l),t),ft(jt(f,m),r));return s==="mn"&&(u=ft(Et(u,t+r),t+r-1)),Le(_e(c,c),Et(u,i))[0]}function $s(e,t,n,r,o){return Le(Le(ft([e,0],t),ft([n,0],r)),[o,0])[0]}function kd(e,t,n,r,o,s){return Oo($s(e,t,n,r,o),Ni(e,t,n,r,o,s))}var Vs=new Float64Array(1),wi=new BigInt64Array(Vs.buffer);function Lf(e){return Vs[0]=Math.abs(e),e<0?-wi[0]:wi[0]}function sr(e){return wi[0]=e<0n?-e:e,e<0n?-Vs[0]:Vs[0]}function bi(e,t,n,r,o){let s=e(t);if(s===0||Number.isNaN(s))return t;let i=s<0===o,c=Lf(i?r:n),a=Lf(t),l=s,f=a,m=s,u=1n;for(;;){if(f=i?a+u:a-u,(i?f>=c:f<=c)&&(f=c),m=e(sr(f)),m===0)return sr(f);if(m<0!=l<0||f===c)break;if(a=f,l=m,u*=2n,u>1n<<40n)return sr(a)}if(m<0==l<0||Number.isNaN(m))return sr(a);for(;f-a>1n||a-f>1n;){let h=(a+f)/2n,p=e(sr(h));if(p===0)return sr(h);p<0==l<0?(a=h,l=p):(f=h,m=p)}return Math.abs(m)<Math.abs(l)?sr(f):sr(a)}function Ff(e,t,n,r,o,s){let i=Sd(e,t,n,r);if(o===0)return[i,i];let c=Fo(o),a=p=>(p===i?0:Fo(kd(e,t,n,r,p,s)))-c,l=(p,w,d,b)=>{if(d===0)return p;if(b===0)return w;let N=Kt(a,p,w,{tol:1e-300,fLower:d,fUpper:b});return s==="wald"?N:bi(v=>qd(e,t,n,r,v,s,o),N,p,w,d<0)},f=a(-1),m=a(1),u=i<=-1||f<=0?-1:l(-1,i,f,-c),h=i>=1||m<=0?1:l(i,1,-c,m);return[u,h]}function Cf(e,t,n){let r=t,o=e(t);if(o>=0)return Math.exp(t);let s=1;for(;;){let i=t+n*s;if(Math.abs(i)>745)return n<0?0:1/0;let c=e(i);if(c>=0){if(c===0)return Math.exp(i);let a=n>0?Kt(e,r,i,{tol:2**-60,fLower:o,fUpper:c}):Kt(e,i,r,{tol:2**-60,fLower:c,fUpper:o});return Math.exp(a)}r=i,o=c,s*=2}}function Id(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 _d(e,t,n,r,o){let[s,i]=Id(e,t,n,r,o);if(!(o>0&&o<1/0))return[s,1-s,i,1-i];let c=s<=.5&&i<=.5?0:o*(1-i)<=1-s?2:3,a=c===0?i:c===2?1-i:1-s,l=y=>c===0?[y,0]:c===2?Ot(1,-y):ft(Ot(1,-y),o),f=c===3?1/o:1,m=c===0?Math.min(1,1/o):1,u=c===2?o>1?ft(Ot(o,-1),o)[0]:0:c===3&&o<1?Ot(1,-o)[0]:0,h=y=>{let x=l(y),M=Et(x,o);return[M[0],Le([1,0],M)[0],x[0],Le([1,0],x)[0]]},p=e+n,w=c<2?1:-1,d=y=>{let[,x,M,R]=h(y),T=0,S=0;return p>0&&(T+=M>0?p/M:1/0,S-=p/(M*M)),e<t&&(T-=x>0?o*(t-e)/x:1/0,S-=o*(t-e)/x*(o/x)),n<r&&(T-=R>0?(r-n)/R:1/0,S-=(r-n)/(R*R)),[w*T,S*f]},b;if(!(m>u))b=u;else if(!(d(u)[0]>0))b=u;else if(!(d(m)[0]<0))b=m;else{let y=u,x=m;b=a,b>y&&b<x||(b=y+(x-y)/2);for(let M=0;M<2e3;M++){let[R,T]=d(b);if(R===0)break;R>0?y=b:x=b;let S=b-R/T;if(S>y&&S<x||(S=y<=0?x/2:x>2*y?Math.sqrt(y*x):y+(x-y)/2),!(S>y&&S<x))break;let A=Math.abs(S-b);if(b=S,A<=2**-53*b)break}}let[N,v,E,g]=h(b);return[Math.min(1,Math.max(0,N)),Math.min(1,Math.max(0,v)),Math.min(1,Math.max(0,E)),Math.min(1,Math.max(0,g))]}function Ld(e,t,n,r,o){let[s,i,c,a]=_d(e,t,n,r,o),l=(f,m,u,h)=>u>.5?m*h-(m-f):f-m*u;return Oo(l(e,t,s,i),t*s*i)+Oo(l(n,r,c,a),r*c*a)}function Of(e,t,n,r,o){let s=e/t/(n/r);return Pf(s,o,i=>Ld(e,t,n,r,i))}function Cd(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 Fd(e,t,n,r,o){let[s,i]=Cd(e,t,n,r,o),c=e-t*s;return Oo(c,t*s*(1-s))+Oo(c,r*i*(1-i))}function Df(e,t,n,r,o){let s=e*(r-n)/((t-e)*n);return Pf(s,o,i=>Fd(e,t,n,r,i))}function Pf(e,t,n){if(t===0)return[e,e];let r=Fo(t),o=l=>n(l)-t,s=l=>l>0&&l<e?bi(o,l,Number.MIN_VALUE,e,!1):l,i=l=>l>e&&l<1/0?bi(o,l,e,Number.MAX_VALUE,!0):l;if(e===0||e===1/0){let l=u=>Fo(n(u))-r,f=l(1),m=f===0?1:rr(l,e===0?f>0:f<0);return e===0?[0,i(m)]:[s(m),1/0]}let c=Math.log(e),a=l=>(l===c?0:Fo(n(Math.exp(l))))-r;return[s(Cf(a,c,-1)),i(Cf(a,c,1))]}var Od=["method","confLevel","sides"],Dd=["method","confLevel","sides"],Pd=["method","confLevel"],Vd=["ac","wald","score","mn","mee"];function yi(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,w=i*Math.sqrt(u*(1-u)/(t+2)+h*(1-h)/(r+2));[f,m]=[Math.max(-1,p-w),Math.min(1,p+w)];break}case"score":{let[u,h]=Ps(e,t,i),[p,w]=Ps(n,r,i);f=l-i*Math.sqrt(u*(1-u)/t+w*(1-w)/r),m=l+i*Math.sqrt(h*(1-h)/t+p*(1-p)/r);break}case"mn":case"mee":[f,m]=Ff(e,t,n,r,s.chisq,o);break}return[Math.min(f,m),Math.max(f,m)]}function $d(e,t,n,r,o={}){B(o,Od,"binomDiffCI");let s=Se(o.method,Vd),i=Co(o.confLevel,o.sides),[c,a]=Bn(e,t,"x1","n1"),[l,f]=Bn(n,r,"x2","n2"),m=c/a-l/f,[u,h]=yi(c,a,l,f,s,i);return i.sides==="left"?h=1:i.sides==="right"&&(u=-1),Q("binomDiffCI",{estimate:m,confInt:[u,h],confLevel:o.confLevel??.95,method:s,sides:i.sides,warnings:[]})}var zd=["katz.log","koopman"];function zs(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 jd(e,t,n,r,o={}){B(o,Dd,"binomRatioCI");let s=Se(o.method,zd),i=Co(o.confLevel,o.sides),[c,a]=Bn(e,t,"x1","n1"),[l,f]=Bn(n,r,"x2","n2"),m=i.kappa,u=c===0?0:c/a/(l/f),h,p;return c===0&&l===0?[h,p]=[0,1/0]:s==="katz.log"?c===0?[h,p]=[0,zs(.5,a,l,f,m)[1]]:l===0?[h,p]=[zs(c,a,.5,f,m)[0],1/0]:c===a&&l===f?[h,p]=zs(a-.5,a,f-.5,f,m):[h,p]=zs(c,a,l,f,m):[h,p]=Of(c,a,l,f,i.chisq),i.sides==="left"?p=1/0:i.sides==="right"&&(h=0),Q("binomRatioCI",{estimate:u,confInt:[h,p],confLevel:o.confLevel??.95,method:s,sides:i.sides,warnings:[]})}var Wd=["midp","fisher","wald","score"],Bd={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"},Kd=2**23,Ud=2**20;function Vf(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=he(r,[e[o],0]));return r[0]}function Hd(e,t={}){B(t,Pd,"oddsRatio");let n=Se(t.method,Wd);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}=Co(r,void 0),c=e.map((S,A)=>Array.from(S,(C,L)=>Ur(C,`table[${A}][${L}]`))),[[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),w=p-h+1;if(n==="midp"||n==="fisher"){if(w>Kd)throw new RangeError(`the exact distribution of the 2 x 2 table has ${w} values, more than 2^23: use method "wald" or "score"`);if(n==="fisher"&&Math.max(a,l,f,m)>2147483647)throw new RangeError(`'table' has entries too large for the conditional MLE (above 2^31 - 1): use method "wald" or "score"`)}let d,b,N;try{d=sn(a-1,a+f,l+m,a+l,!0,!1)+.5*bn(a,a+f,l+m,a+l,!1),b=sn(a,a+f,l+m,a+l,!1,!1)+.5*bn(a,a+f,l+m,a+l,!1),N=w<=Ud?Ja(new Br(a+f,l+m,a+l),a,1):ff(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(d,b),E=[a+l,f+m],g=[a+f,l+m],y=0,x=!1;for(let S=0;S<2;S++)for(let A=0;A<2;A++){let C=E[A]*g[S]/u;C<5&&(x=!0);let L=Math.abs(c[A][S]-C);y+=L*L/C}let M=wt(y,.5,2,!1,!1),R,T;switch(n){case"wald":{R=a*m/(l*f);let S=s*Math.sqrt(1/a+1/l+1/f+1/m);T=[Math.exp(Math.log(R)-S),Math.exp(Math.log(R)+S)];break}case"fisher":{let S=ot(()=>Io(c,{confLevel:r}));R=S.estimate,T=S.confInt;break}case"score":{R=a*m/(l*f),T=(a===0||m===0)&&(l===0||f===0)?[0,1/0]:Df(a,a+l,f,f+m,i);break}case"midp":{let S=new Br(a+f,l+m,a+l),A=F=>{if(F===1)return[d,b];let I=S.density(F),O=a-h,q=.5*I[O];return[Vf(I,0,O-1)+q,Vf(I,O+1,I.length-1)+q]},C=(F,I)=>{let O=F(1);return O===0?1:rr(F,I?O>0:O<0)};R=a===h?0:a===p?1/0:C(F=>A(F)[0]-.5,!1);let L=a===h?0:C(F=>A(F)[1]-o/2,!0),k=a===p?1/0:C(F=>A(F)[0]-o/2,!1);T=[L,k];break}}return Q("oddsRatio",{estimate:R,confInt:T,confLevel:r,method:Bd[n],pValue:{midp:v,fisher:N,chiSquare:M},warnings:x?["Chi-squared approximation may be incorrect"]:[]})}var Yd=["epsilon","paired","varEqual","confLevel","alpha"],Gd=["margin","method","alpha"];function $f(e,t){if(!(typeof e=="number"&&e>0&&e<.5))throw new RangeError(`'${t}' must be a single number in (0, 0.5)`)}function Xd(e,t,n={}){B(n,Yd,"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)$f(n.alpha,"alpha"),o=1-n.alpha,s=n.alpha;else{if(o=n.confLevel??.95,_t(o),!(o>.5))throw new RangeError("'confLevel' must be greater than 0.5");s=1-o}if(t===void 0)throw new TypeError("'y' is required");let i=n.paired??!1,c=n.varEqual??!1,a=ot(()=>Nn(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=dt((r-Math.abs(m))/l,f,!1,!1),h=s<1e-8?gt(s,f,!1,!1):gt(o,f,!0,!1),p=[m-h*l,m+h*l];return Q("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 Qd=["mn","mee","wald"];function Zd(e,t,n,r,o={}){B(o,Gd,"binomDiffEquivalenceTest");let s=Se(o.method,Qd),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;$f(c,"alpha");let[a,l]=Bn(e,t,"x1","n1"),[f,m]=Bn(n,r,"x2","n2"),u=a/l-f/m,h=E=>$s(a,l,f,m,E)/Math.sqrt(Ni(a,l,f,m,E,s)),p=h(-i),w=h(i),d=[se(p,{lowerTail:!1}),se(w)],b=Math.max(d[0],d[1]),N=1-2*c,v=yi(a,l,f,m,s,di(2*c,N));return Q("binomDiffEquivalenceTest",{method:s,estimate:u,margin:i,alpha:c,statistic:[p,w],statisticName:"z",pValues:d,pValue:b,result:d[0]<c&&d[1]<c?"rejected":"not rejected",confInt:v,confLevel:N,warnings:[]})}var Ut=new WeakMap;function Ws(e){let t=(n,r)=>n.weights===void 0?Array.from(r):r.filter((o,s)=>n.weights[s]!==0);return qe(e.flatMap(n=>[t(n,n.fittedValues),t(n,n.residuals)])).k}function Bs(e,t){return e.map(n=>{if(n.rss>=Jd&&n.rss<1/0)return V(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=V(o,-t);r+=i*c*c}),r})}var Jd=2**-1022;function Ei(e,t){return e.y.flatMap((n,r)=>(e.weights?.[r]??1)===0?[]:[[e.weights?.[r]??1,V(n,-t)]])}function Ks(e){let t=Math.floor(Math.log2(e.reduce((n,[r])=>Math.max(n,r),0)));return t-(t%2+2)%2}function Er(e,t,n=0){let r=i=>e.reduce((c,[a,l])=>c+V(a,-i)*l*l,0),o=n,s=r(o);return Number.isFinite(s)||(o=Ks(e),s=r(o)),!(V(t,-o)>=1e-24*s)}function zf(e){let t=Ws([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+=V(e.effects[s],-t)**2}return{terms:r,rdf:e.dfResidual,rss:Bs([e],t)[0],k:t}}var jf=(e,t,n)=>ht(e,t,n,!1,!1),xi="essentially perfect fit for \u20181\u2019: summary may be unreliable";function Ri(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 Ti=new WeakMap;function e0(e){let t=zf(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*V(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 Mi(e){let t=e0(e),{names:n,rdf:r,k:o,perfect:s}=t,{rows:i,rss:c}=t;if(r>0){let w=Ei(e,o);Er(w,c)&&(c=0,i=i.map(d=>Er(w,d.ss)?{...d,ss:0}:d))}let a=c/r,l=w=>V(w,2*o),f=i.map(({df:w,ss:d})=>{let b=d/w/a;return[w,l(d),l(d/w),b,jf(b,w,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:w,ss:d})=>[w,d]);return p.push([r,c]),Ut.set(h,{colNames:["Df","Sum Sq"],rowNames:u,table:p}),h}function t0(e){let t=e.map(f=>f.dfResidual),n=Ws(e),r=Bs(e,n),o=f=>V(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*V(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,w]=u>0?[e[m-1],e[m]]:[e[m],e[m-1]],d=N=>N.filter((v,E)=>(p.weights?.[E]??1)!==0),b=u===0||!Es(d(p.x),d(w.x))?NaN:h/u/i;return b<0&&(b=NaN),[t[m],o(r[m]),u,o(h),b,Number.isNaN(b)?NaN:jf(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 js(...e){let t=[];for(let n of e)for(let r of n??[])t.includes(r)||t.push(r);return t}function Si(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=Mi(e),a={...c,warnings:js(e.warnings,c.warnings)},l=Ut.get(c);return l!==void 0&&Ut.set(a,l),Q("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?Mi(e):t0(o);return i.warnings=js(...n.map(c=>c.warnings),s,i.warnings),Q("anova",i,...n)}function Ai(e){if(typeof e=="object"&&e!==null&&e.multistratum===!0){let i=e,c=js(i.warnings,Ti.get(i)),a={strata:i.strata.filter(f=>f.stratum!=="Error: (Intercept)").map(f=>({...f})),warnings:c},l=Ut.get(i);return Array.isArray(l)&&Ut.set(a,l.filter((f,m)=>i.strata[m].stratum!=="Error: (Intercept)")),Q("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=Mi(t),r=zf(t),o=Ri(t.fittedValues.map(i=>V(i,-r.k)),r.rss,r.rdf),s=js(t.warnings,n.warnings,o?[xi]:[]);if(t.dfResidual>0){let i={...n,warnings:s},c=Ut.get(n);return c!==void 0&&Ut.set(i,c),Q("aovSummary",i,e)}return Q("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 n0=["pooledSd","mu","paired","ci","alternative","adjust"],r0=["pooledSd","mu","paired","ci","alternative"],o0=["generalized","partial","ci","alternative"],s0=["partial","ci","alternative"],a0=["partial","ci","alternative"];function Ii(e){if(e!==null&&!(e>0&&e<1))throw new RangeError("ci must be a single numeric value between (0, 1)")}var i0="Missing values detected. NAs dropped.",c0=`'y' is numeric but has only 2 unique values.
3
+ If this is a grouping variable, convert it to a factor.`,Uf={tol:0,maxiter:5e3};function xr(e){return e===1/0?1:Math.exp(ll(.5,(e-1)/2)-.5*Math.log(e/2))}function Wf(e,t,n){let r=e-1<e+1?1:Math.abs(e)*Number.EPSILON;return br(o=>Qt(e,t,{ncp:o})-n,e-r,e+r,{...Uf,extendInt:"downX"}).root}function Hf(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;Ii(c);let a=Se(n.alternative,["two.sided","less","greater"]),l=Array.from(e),f=t===void 0?void 0:Array.from(t),m=[];f!==void 0&&new Set(Dt(f)).size===2&&m.push(c0),(l.some(Number.isNaN)||(f?.some(Number.isNaN)??!1))&&m.push(i0);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,w,d,b,N;if(f===void 0||i){let S;if(f===void 0)({groups:[S],k:N}=qe([Dt(l)]));else{let[C,L]=Jn(l,f);({d:S,k:N}=ls(C,L))}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");h=it(S),p=Math.sqrt(at(S)),w=p/Math.sqrt(A),d=1/A,b=A-1}else{let S=xt([Dt(l),Dt(f)]);N=S.k;let[A,C]=S.n;if(A<2||C<2)throw new RangeError("not enough observations: at least 2 are needed in each group");h=S.mean[0]-S.mean[1];let L=Math.sqrt(S.variance[0]),k=Math.sqrt(S.variance[1]);o?(p=Math.sqrt(((A-1)*L**2+(C-1)*k**2)/(A+C-2)),w=p*Math.sqrt(1/A+1/C),d=1/A+1/C,b=A+C-2):(p=Math.sqrt((L**2+k**2)/2),d=2*(C*L**2+A*k**2)/(A*C*(L**2+k**2)),w=Math.sqrt(Math.sqrt(L**2/A)**2+Math.sqrt(k**2/C)**2),b=fs([L**2/A,k**2/C],[A-1,C-1]))}let v=r?xr(b):1,E=V(s,-N),g=(h-E)/p*v,y=r?"Hedges_g":"Cohens_d";if(c===null)return{name:y,value:g,warnings:m};let x=a==="two.sided"?c:2*c-1,M=(h-E)/w,R=Wf(M,b,(1+x)/2)*Math.sqrt(d)*v,T=Wf(M,b,(1-x)/2)*Math.sqrt(d)*v;return a==="less"&&(R=-1/0),a==="greater"&&(T=1/0),{name:y,value:g,ci:c,ciLow:R,ciHigh:T,warnings:m}}function l0(e,t,n={}){return B(n,n0,"cohensD"),Q("cohensD",Hf(e,t,n,n.adjust??!1))}function f0(e,t,n={}){return B(n,r0,"hedgesG"),Q("hedgesG",Hf(e,t,n,!0))}var m0=e=>Array.isArray(e.effects),qi=e=>e.multistratum===!0,Yf=e=>Array.isArray(e.strata),Gf=e=>{let t=e;return typeof t.univariate=="object"&&typeof t.anova=="object"},ki=e=>({test:t=>t.split(":").includes(e)});function u0(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 h0(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=>ki(b).test(m)),w=o(p.length===0?"Subject":`Subject:${p.join(":")}`,h[l],h[f]);if(m==="(Intercept)")return;let d={name:m,df:h[a],ss:h[n]};w.terms.push(d),s.set(m,d)})}else{let a=t.colNames.indexOf("Df"),l=t.rowNames.indexOf("Residuals"),f=o("Subject",t.table[l][n],t.table[l][a]);t.rowNames.forEach((m,u)=>{if(m==="(Intercept)"||m==="Residuals")return;let h={name:m,df:t.table[u][a],ss:t.table[u][n]};f.terms.push(h),s.set(m,h)})}let i=r.find(a=>a.name==="Subject");if(i===void 0)throw new Error("the result has no subject stratum");let c=e.anova.rowNames.map(a=>{let l=s.get(a);if(l===void 0)throw new Error(`there is no univariate test of '${a}'`);return l});return{strata:r,subject:i,order:c}}function Xf(e,t){let n=Array.isArray(e)?e:[],r=n.filter(s=>!t.some(i=>ki(s).test(i.name)));if(r.length>0)throw new RangeError(`Observed variable not in data: ${r.join(", ")}`);let o=s=>n.some(i=>ki(i).test(s));return{isObserved:o,ssObserved:t.reduce((s,i)=>o(i.name)?s+i.ss:s,0)}}function Bf(e,t,n,r){let o=i=>Rn(e,t,n,{ncp:i})-r,s=o(0);return Number.isNaN(s)?NaN:s>0?e===1/0?1/0:br(o,0,Math.max(10,e*t*4),{...Uf,extendInt:"downX"}).root:0}function Qf(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=Bf(c,t,n,(1+s)/2),l=Bf(c,t,n,(1-s)/2);c<=Gn((1-s)/2,t,n)&&(l=0),c<=Gn((1+s)/2,t,n)&&(a=0);let f=a===1/0?1:a/(a+n),m=l===1/0?1:l/(l+n);return o==="less"&&(f=0),o==="greater"&&(m=1),[e,r,f,m]}function p0(e){let t=qe([e.map(n=>n.ss)]).k;if(t!==0)for(let n of e)n.ss=V(n.ss,-t)}var Kf="needs a table of anova() or Anova(), a fit from lm() or aov() with a formula, its aovSummary(), or a result of aovEz()";function _i(e,t,n){if(typeof e!="object"||e===null)throw new TypeError(Kf);if(qi(e)||Yf(e)||Gf(e))return d0(e,t,n);let r=m0(e)?ot(()=>Si(e)):e;if(!Array.isArray(r.colNames)||!Array.isArray(r.table))throw new TypeError(Kf);let o=Ut.get(r),s=o!==void 0&&!Array.isArray(o)?o:r,i=n.ci===void 0?.95:n.ci;Ii(i);let c=Se(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)");p0(f);let m=f.find(R=>R.name==="Residuals");if(m===void 0)throw new TypeError("the table has no row 'Residuals'");if(!(m.df>0))throw new Error("the model has no residual degrees of freedom: the effect sizes are not defined");let u=f.filter(R=>R!==m),h=m.df,p=m.ss,w=p/h,d=f.reduce((R,T)=>R+T.ss,0),b=f.reduce((R,T)=>R+T.df,0)+1,N=t==="Eta2"?n.generalized??!1:!1,E=u.length===1?"plain":N!==!1?"generalized":n.partial??!0?"partial":"plain",{isObserved:g,ssObserved:y}=Xf(N,u),x=R=>{let{ss:T,df:S}=R;if(t==="Eta2")return E==="partial"?T/(T+p):E==="plain"?T/d:T/(T+p+y-(g(R.name)?T:0));if(t==="Omega2"){let C=E==="partial"?(T-S*w)/(T+(b-S)*w):(T-S*w)/(d+w);return Math.max(0,C)}let A=E==="partial"?(T-S*w)/(T+p):(T-S*w)/d;return Math.max(0,A)},M=`${t}${E==="plain"?"":`_${E}`}`;return{colNames:i===null?[M]:[M,"CI","CI_low","CI_high"],rowNames:u.map(R=>R.name),table:u.map(R=>i===null?[x(R)]:Qf(x(R),R.df,h,i,c)),warnings:Array.isArray(r.warnings)?[...r.warnings]:[]}}function d0(e,t,n){let r=n.ci===void 0?.95:n.ci;Ii(r);let o=Se(n.alternative??"greater",["two.sided","less","greater"]),s=Gf(e),i,c,a;if(s){let g=Ut.get(e),y=h0(g!==void 0&&!Array.isArray(g)?{...e,univariate:g}:e);({strata:i,subject:c}=y),a=y.order.map(x=>({term:x,stratum:y.strata.find(M=>M.terms.includes(x))}))}else{let g=qi(e)?ot(()=>Ai(e)):e,y=Ut.get(g);({strata:i,subject:c}=u0(Array.isArray(y)?{...g,strata:y.map((x,M)=>({...g.strata[M],...x}))}:g)),a=i.flatMap(x=>x.terms.map(M=>({term:M,stratum:x})))}let l=qe([[...i.flatMap(g=>g.terms.map(y=>y.ss)),...i.map(g=>g.sse)]]).k;for(let g of i){g.sse=V(g.sse,-l);for(let y of g.terms)y.ss=V(y.ss,-l)}let f=i.flatMap(g=>g.terms),m=i.reduce((g,y)=>g+y.sse,0),u=f.reduce((g,y)=>g+y.ss,0)+m,h=c.sse/c.dfe,p=t==="Eta2"?n.generalized??!1:!1,w=p!==!1?"generalized":n.partial??!0?"partial":"plain",{isObserved:d,ssObserved:b}=Xf(p,f),N=(g,y)=>{let{ss:x,df:M}=g,R=y.sse/y.dfe;if(t==="Eta2")return w==="partial"?x/(x+y.sse):w==="plain"?x/u:x/(x+m+b-(d(g.name)?x:0));if(t==="Omega2"){let S=y===c?0:1,A=w==="partial"?(x-M*R)/(x+S*y.sse+c.sse+h):(x-M*R)/(u+h);return Math.max(0,A)}let T=w==="partial"?(x-M*R)/(x+y.sse):(x-M*R)/u;return Math.max(0,T)},v=`${t}${w==="plain"?"":`_${w}`}`,E={colNames:r===null?[v]:[v,"CI","CI_low","CI_high"],rowNames:a.map(g=>g.term.name),table:a.map(({term:g,stratum:y})=>{let x=N(g,y);return r===null?[x]:Qf(x,g.df,y.dfe,r,o)}),warnings:[]};return s||(E.groups=a.map(g=>g.stratum.name),E.warnings=[...qi(e)||Yf(e)?e.warnings:[]]),E}function g0(e,t={}){return B(t,o0,"etaSquared"),Q("etaSquared",_i(e,"Eta2",t),e)}function w0(e,t={}){return B(t,s0,"omegaSquared"),Q("omegaSquared",_i(e,"Omega2",t),e)}function b0(e,t={}){return B(t,a0,"epsilonSquared"),Q("epsilonSquared",_i(e,"Epsilon2",t),e)}var v0=new Set(["eqb","lowEqbound","highEqbound","hypothesis","paired","varEqual","alpha","mu","eqboundType","biasCorrection","rmCorrection","glass","smdCi"]),N0=["m1","sd1","n1","m2","sd2","n2","r12"],y0=new Set(["eqb","lowEqbound","highEqbound","hypothesis","paired","varEqual","alpha","mu","eqboundType","biasCorrection","rmCorrection","glass","smdCi"]),M0="Equivalence interval does not include zero.",E0="Warning: setting bound type to SMD produces biased results!",x0="MET test may have higher error rates than a nil two-tailed test. Consider wider equivalence bounds.";function Zf(e,t,n,r){if(e===1/0||e===-1/0)return e;let o=e-1<e+1?1:Math.abs(e)*Number.EPSILON;return br(s=>Qt(e,t,{ncp:s,lowerTail:!r})-n,e-o,e+o,{tol:0,maxiter:5e3,extendInt:r?"upX":"downX"}).root}function R0(e,t){let{alpha:n,smdCi:r}=t,o=Math.abs(e.delta),s=(M,R,T)=>(1-(T-2)/(T*M*M))*R*R,i,c,a,l,f,m,u,h,p,w,d,b,N=t.biasCorrection;if(e.design==="two"){let{n1:M,n2:R,s1:T,s2:S}=e,A=T*T,C=S*S,L=2*M*R/(M+R);if(t.glass!==void 0){let k=t.glass==="glass1",[F,I,O,q]=k?[T,M,S,R]:[S,R,T,M];c=F,a=I-1,i=N?"Glass's delta(g)":"Glass's delta(d)",l=N?xr(a):1,f=l*o/c,p=1/R+1/M;let $=O/F;m=Math.sqrt($*$/(q-1)+1/(I-1)+f*f/(2*(I-1))),u=o/(F*Math.sqrt(1/I+$*$/q)),h=M+R-2,w=Math.sqrt(2/L)}else if(t.varEqual)c=Math.sqrt(((M-1)*A+(R-1)*C)/(M+R-2)),a=M+R-2,i=N?"Hedges's g":"Cohen's d",l=N?xr(a):1,f=l*o/c,p=1/M+1/R,m=Math.sqrt(1/M+1/R+s(l,f,a)),u=o/(c*Math.sqrt(p)),h=a,w=Math.sqrt(2*(R*A+M*C)/(M*R*(A+C)));else{c=Math.sqrt((A+C)/2),a=(M-1)*(R-1)*(A+C)**2/((R-1)*A*A+(M-1)*C*C),i=N?"Hedges's g(av)":"Cohen's d(av)",l=N?xr(a):1,f=l*o/c,p=2*(R*A+M*C)/(M*R*(A+C));let k=c**4;m=Math.sqrt(f*f*(A*A/(M-1)+C*C/(R-1))/(8*k)+(A/(M-1)+C/(R-1))/(c*c)),u=o/Math.sqrt(A/M+C/R),h=a,w=Math.sqrt(2/L)}d=a,b=(k,F)=>Math.sqrt(d/(d-2)*(2/L)*(1+k*k*L/2)-k*k/(F*F))}else if(e.design==="paired"){let{n:M,s1:R,s2:T,sdif:S,oneMinusR:A}=e,C=t.glass!==void 0?"":t.rmCorrection?"(rm)":"(z)";if(t.glass!==void 0?c=t.glass==="glass1"?R:T:c=t.rmCorrection?S/Math.sqrt(2*A):S,i=t.glass!==void 0?N?"Glass's delta(g)":"Glass's delta(d)":N?`Hedges's g${C}`:`Cohen's d${C}`,a=M-1,d=2*M-2,l=N?xr(r==="goulet"?d:a):1,f=l*o/c,p=1/M,u=o*Math.sqrt(M)/c,h=a,w=Math.sqrt(2*A/M),b=(L,k)=>{let F=r==="goulet"?d:a,I=Math.sqrt(F/(F-2)*(2*A/M)*(1+L*L*M/(2*A))-L*L/(k*k));return t.rmCorrection?I:I*Math.sqrt(2*A)},t.glass!==void 0){let L=S/c;m=Math.sqrt(L*L/(M-1)+f*f/(2*(M-1)))}else!t.rmCorrection&&r==="nct"?m=Math.sqrt(1/M+s(l,f,a)):m=b(f,l)}else{let{n:M,s:R}=e;c=R,a=M-1,d=a,i=N?"Hedges's g":"Cohen's d",l=N?xr(a):1,f=l*o/c,p=1/M,m=Math.sqrt(1/M+f*f/(2*M)),u=o*Math.sqrt(M)/R,h=a,w=1/Math.sqrt(M),b=(T,S)=>Math.sqrt(a/(a-2)*(1/M)*(1+T*T*M)-T*T/(S*S))}let v,E,g,y=a;if(r==="nct"){g=m,y=h;let M=Zf(u,h,n,!0),R=Zf(u,h,n,!1);v=M*Math.sqrt(p)*l,E=R*Math.sqrt(p)*l}else if(r==="goulet"){g=b(f,l),y=d;let M=f/w;v=Wt(n,d,{ncp:M})*w,E=Wt(n,d,{ncp:M,lowerTail:!1})*w}else{g=m;let M=r==="t"?gt(n,a,!1,!1):le(n,{lowerTail:!1});v=f-M*g,E=f+M*g}let x=e.delta<0;return{label:i,denom:c,estimate:x?-f:f,se:g,confInt:x?[-E,-v]:[v,E],df:y,J:l}}function Jf(e,t){if(!(Array.isArray(e)||ArrayBuffer.isView(e)))throw new TypeError(`'${t}' must be an array of numbers`);let n=e;for(let r=0;r<n.length;r++){let o=n[r];if(typeof o!="number")throw new TypeError(`'${t}' must be numeric`);if(o===1/0||o===-1/0)throw new RangeError(`'${t}' must not contain infinite values`)}}function em(e){let{eqb:t}=e,n;if(t!==void 0){let r=typeof t=="number"?[t]:Array.from(t);if(!r.every(o=>typeof o=="number"))throw new TypeError("'eqb' must be a number or an array of 1 or 2 numbers");if(r.length===1)n=[-Math.abs(r[0]),Math.abs(r[0])];else if(r.length===2)n=[Math.min(r[0],r[1]),Math.max(r[0],r[1])];else throw new RangeError("'eqb' must be a number or an array of 1 or 2 numbers")}else{if(e.lowEqbound===void 0||e.highEqbound===void 0)throw new TypeError("the equivalence bounds are required: 'eqb', or 'lowEqbound' and 'highEqbound'");n=[e.lowEqbound,e.highEqbound]}if(!n.every(r=>typeof r=="number"&&Number.isFinite(r)))throw new RangeError("the equivalence bounds must be finite numbers");if(n[0]>n[1])throw new RangeError("'lowEqbound' must not be greater than 'highEqbound'");return n}function tm(e){let t=Se(e.hypothesis,["EQU","MET"]),n=Se(e.eqboundType,["raw","SMD"])==="SMD",r=Se(e.smdCi,["nct","goulet","t","z"]),o=e.glass===void 0?void 0:Se(e.glass,["glass1","glass2"]),s=e.alpha??.05;if(!(typeof s=="number"&&s>0&&s<.5))throw new RangeError("'alpha' must be a single number in (0, 0.5)");let i=e.mu??0;if(!(typeof i=="number"&&Number.isFinite(i)))throw new RangeError("'mu' must be a finite number");let c=(a,l,f)=>{if(a!==void 0&&typeof a!="boolean")throw new TypeError(`'${l}' must be true or false`);return a??f};return{hypothesis:t,smdBounds:n,smdCi:r,glass:o,alpha:s,mu:i,paired:c(e.paired,"paired",!1),varEqual:c(e.varEqual,"varEqual",!1),biasCorrection:c(e.biasCorrection,"biasCorrection",!0),rmCorrection:c(e.rmCorrection,"rmCorrection",!1)}}function nm(e,t,n,r,o,s,i,c,a,l,f,m){let{alpha:u,hypothesis:h}=l,p=R0(c,l),w=V(p.denom,a),d=l.smdBounds?[f[0]*w,f[1]*w]:f,b=O=>{if(O===0)return[0,n];let q=Math.floor(Math.log2(Math.abs(O))),$=Ve(O,-q);return l.smdBounds?[$*p.denom,q+a]:[$,q]},N=l.smdBounds?f:[f[0]/w,f[1]/w];l.smdBounds&&m.push(E0),d[0]<=0&&d[1]>=0||m.push(M0);let v=O=>V(O/r,n-o),E=O=>{let[q,$]=b(O),_=Math.max(n,$);return Ve((Ve(t,n-_)-Ve(q,$-_))/r,_-o)},g=v(t),y=E(f[0]),x=E(f[1]),M=V(r,o),R=h==="EQU",T={t:g,se:M,df:s,pValue:2*dt(-Math.abs(g),s,!0,!1)},S={t:y,se:M,df:s,pValue:dt(y,s,!R,!1)},A={t:x,se:M,df:s,pValue:dt(x,s,R,!1)},C=R?Math.max(S.pValue,A.pValue):Math.min(S.pValue,A.pValue);!R&&d[0]<=0&&d[1]>=0&&C<=T.pValue&&m.push(x0);let L=gt(u,s,!1,!1),k=Math.max(n,o),[F,I]=[V(t,n-k),V(r,o-k)];return Q(e,{tTest:T,lower:S,upper:A,pValue:C,bounds:{raw:d,smd:N},raw:{estimate:V(t,n),se:M,confInt:[V(F-L*I,k),V(F+L*I,k)]},smd:{label:p.label,estimate:p.estimate,se:p.se,confInt:p.confInt,df:p.df,J:p.J},confLevel:1-2*u,method:i,hypothesis:h,warnings:m})}function T0(e,t,n={}){B(n,v0,"tTOST");let r=tm(n),o=em(n),{paired:s,varEqual:i,mu:c}=r,a=t===void 0;if(s&&a)throw new TypeError("'y' is required when paired is true");Jf(e,"x"),a||Jf(t,"y");let l=ot(()=>a?Nn(e,{mu:c}):Nn(e,t,{paired:s,varEqual:i,mu:c})),f=l.parameter,m,u,h,p;if(a){let w=qe([Dt(Array.from(e))]);u=w.k;let d=w.groups[0],b=at(d);h=it(d)-V(c,-u),p=Math.sqrt(b/d.length),m={design:"one",n:d.length,delta:h,s:Math.sqrt(b)}}else if(s){let[w,d]=Jn(Array.from(e),Array.from(t)),b=qe([w,d]);u=b.k;let[N,v]=b.groups,E=N.map((T,S)=>T-v[S]),g=Math.sqrt(at(N)),y=Math.sqrt(at(v)),x=at(E),M=Math.sqrt(x),R=(M-(g-y))*(M+(g-y))/(2*g*y);h=it(E)-V(c,-u),p=Math.sqrt(x/E.length),m={design:"paired",n:E.length,delta:it(N)-it(v)-V(c,-u),s1:g,s2:y,sdif:M,oneMinusR:R}}else{let w=qe([Dt(Array.from(e)),Dt(Array.from(t))]);u=w.k;let[d,b]=w.groups;if(d.length<2||b.length<2)throw new RangeError("not enough observations: at least 2 are needed in each sample");let[N,v,E,g]=[d.length,b.length,at(d),at(b)];h=it(d)-it(b)-V(c,-u),p=Math.sqrt(i?((N-1)*E+(v-1)*g)/(N+v-2)*(1/N+1/v):Math.sqrt(E/N)**2+Math.sqrt(g/v)**2),m={design:"two",n1:N,n2:v,delta:h,s1:Math.sqrt(E),s2:Math.sqrt(g)}}return nm("tTOST",h,u,p,u,f,l.method,m,u,r,o,[])}function S0(e,t={}){B(e,N0,"tsumTOST"),B(t,y0,"tsumTOST");let n=tm(t),r=em(t),{m1:o,sd1:s,n1:i,m2:c,sd2:a,n2:l,r12:f}=e,m=(k,F)=>{if(typeof k!="number"||!Number.isFinite(k))throw new RangeError(`'${F}' must be a finite number`);return k},u=(k,F)=>{let I=m(k,F);if(!(Number.isInteger(I)&&I>=2))throw new RangeError(`'${F}' must be an integer of at least 2`);return I},h=(k,F)=>{let I=m(k,F);if(!(I>0))throw new RangeError(`'${F}' must be positive`);return I};m(o,"m1"),h(s,"sd1"),u(i,"n1");let p=c===void 0&&a===void 0&&l===void 0;if(n.paired&&p)throw new TypeError("'m2', 'sd2' and 'n2' are required when paired is true");p||(m(c,"m2"),h(a,"sd2"),u(l,"n2"));let w=[],{groups:d,k:b}=qe([[o,...p?[]:[c],n.mu]]),{groups:N,k:v}=qe([[s,...p?[]:[a]]]),[E,g=NaN]=p?[d[0][0]]:[d[0][0],d[0][1]],y=d[0][d[0].length-1],[x,M=NaN]=N[0],R=k=>V(k,b-v),T,S,A,C,L;if(p)T=E-y,S=x/Math.sqrt(i),A=i-1,C="One Sample t-test",L={design:"one",n:i,delta:R(T),s:x};else if(n.paired){let k=m(f,"r12");if(!(k>-1&&k<1))throw new RangeError("'r12' must be in (-1, 1)");let F=i;i!==l&&(w.push("Unequal number of pairs; using smallest n"),F=Math.min(i,l));let I=Math.sqrt(x*x+M*M-2*k*x*M);if(!(I>0))throw new RangeError("the standard deviation of the differences is 0");T=E-g-y,S=I/Math.sqrt(F),A=F-1,C="Paired t-test",L={design:"paired",n:F,delta:R(T),s1:x,s2:M,sdif:I,oneMinusR:1-k}}else{let k=l,F=x*x,I=M*M;if(T=E-g-y,n.varEqual){let O=((i-1)*F+(k-1)*I)/(i+k-2);S=Math.sqrt(O*(1/i+1/k)),A=i+k-2,C=" Two Sample t-test"}else{let O=F/i,q=I/k;S=Math.sqrt(O+q),A=(O+q)**2/(O*O/(i-1)+q*q/(k-1)),C="Welch Two Sample t-test"}L={design:"two",n1:i,n2:k,delta:R(T),s1:x,s2:M}}return nm("tsumTOST",T,b,S,v,A,C,L,v,n,r,w)}var A0=["alternative","tsmethod"],q0=["square","central"],k0=1e3,I0=32,_0=.9,rm=1+1e-7,L0=2**24,C0=34e7;function F0(e,t={}){if(B(t,A0,"boschlooTest"),e.length!==2||e.some(y=>y.length!==2))throw new TypeError("'table' must be a 2 x 2 table");let n=Ze(t.alternative),r=Se(t.tsmethod,q0),o=e.map((y,x)=>Array.from(y,(M,R)=>Ur(M,`table[${x}][${R}]`))),[[s,i],[c,a]]=o,l=s+i,f=c+a;if(l===0||f===0)throw new RangeError("each row (group) of 'table' must have at least one observation");if((l+1)*(f+1)>L0)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(k0,Math.ceil(I0*Math.sqrt(m))),h=y=>Math.min(y+1,40*Math.sqrt(y));if(u*(h(l)+h(f)+4)>C0)throw new RangeError(`Boschloo's test of these group sizes (${l} and ${f}) would take too long`);let p=s/l-c/f,w;n!=="two.sided"?w=n:r==="square"?w="square":w=p>0?"greater":"less";let d=s+c,b;w==="less"?b=sn(s,l,f,d,!0,!1):w==="greater"?b=sn(s-1,l,f,d,!1,!1):b=ot(()=>Io(o)).pValue;let N=O0(l,f,s,c,w),{value:v,at:E}=D0(N,l,f,u),g=n==="two.sided"&&r==="central"?Math.min(1,2*v):v;return Q("boschlooTest",{method:"Boschloo's Exact Test",alternative:n,statistic:b,statisticName:"test statistic",pValue:g,estimate:p,estimateNames:"difference in proportion",nullValue:0,nullValueName:"difference in proportion",nuisanceParameter:E,warnings:[]})}function O0(e,t,n,r,o){let s=e+t,i=new Float64Array(s+1);for(let b=2;b<=s;b++)i[b]=us(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),E=Math.min(b,e)-N+1,g=-1/0;for(let M=0;M<E;M++){let R=N+M,T=-i[R]-i[e-R]-i[b-R]-i[t-b+R];f[M]=T,T>g&&(g=T)}let y=0;for(let M=0;M<E;M++)f[M]=Math.exp(f[M]-g),y+=f[M];for(let M=0;M<E;M++)f[M]=f[M]/y;let x=0;for(let M=0;M<E;M++)m[M]=x+=f[M];x=0;for(let M=E-1;M>=0;M--)u[M]=x+=f[M];if(o==="less")for(let M=0;M<E;M++)a[(N+M)*c+b-N-M]=m[M];else if(o==="greater")for(let M=0;M<E;M++)a[(N+M)*c+b-N-M]=u[M];else{let M=0;for(let R=1;R<E;R++)f[R]>f[M]&&(M=R);for(let R=0;R<E;R++){let T=f[R]*rm,S=-1,A=M;for(;A-S>1;){let F=S+A>>1;f[F]<=T?S=F:A=F}let C=M,L=E;for(;L-C>1;){let F=C+L>>1;f[F]<=T?L=F:C=F}let k=f[M]<=T?1:(S>=0?m[S]:0)+(L<E?u[L]:0);a[(N+R)*c+b-N-R]=Math.min(1,k)}}}let h=a[n*c+r]*rm,p=new Int32Array(e+2),w=[],d=[];for(let b=0;b<=e;b++){p[b]=w.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++;w.push(v),d.push(N)}N++}}return p[e+1]=w.length,{starts:p,from:Int32Array.from(w),to:Int32Array.from(d)}}function om(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]=hn(r,e,t,!1);let o=Ot(1,-t),s=rt([t,0],o),i=rt(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 D0(e,t,n,r){let{starts:o,from:s,to:i}=e,c=new Float64Array(t+1),a=new Float64Array(n+1),l=new Float64Array(n+1),f=new Float64Array(n+1),m=v=>{let[E,g]=om(t,v,c),[y,x]=om(n,v,a),M=0;for(let T=y;T<=x;T++)l[T]=M+=a[T];M=0;for(let T=x;T>=y;T--)f[T]=M+=a[T];let R=0;for(let T=E;T<=g;T++){let S=c[T];if(S===0)continue;let A=0;for(let C=o[T];C<o[T+1];C++){let L=s[C],k=i[C];if(!(k<y||L>x))if(L===0)A+=l[Math.min(k,x)];else if(k===n)A+=f[Math.max(L,y)];else for(let F=Math.max(L,y);F<=Math.min(k,x);F++)A+=a[F]}R+=S*A}return Math.min(1,R)},u=Math.PI/2,h=v=>v<=0?0:v>=u?1:Math.sin(v)**2,p=new Float64Array(r+2),w=0;for(let v=0;v<=r+1;v++)p[v]=m(h(u*v/(r+1))),p[v]>p[w]&&(w=v);let d=p[w],b=h(u*w/(r+1));if(d>=1)return{value:1,at:b};let N=_0*d;for(let v=0;v<=r+1;v++){let E=p[v];if(E<N||v>0&&!(E>p[v-1])||v<=r&&!(E>=p[v+1]))continue;let g=u*Math.max(0,v-1)/(r+1),y=u*Math.min(r+1,v+1)/(r+1),M=kl(S=>-m(h(S)),g,y,1e-14).x,R=m(h(M)),T=1e-4/Math.sqrt(2*(t+n));for(let S=0;S<2&&(M-T>g&&M+T<y);S++,T*=.01){let A=m(h(M-T)),C=m(h(M+T)),L=A-2*R+C;if(!(L<0))break;let k=T*(A-C)/(2*L);if(!(Math.abs(k)<T))break;let F=m(h(M+k));F>R*(1+2**-50)&&(M+=k,R=F)}R>d&&(d=R,b=h(M))}return{value:d,at:b}}var sm=["correct"];function P0(e,t,n={}){ct(t)&&B(t,sm,"mcnemarTest"),B(n,sm,"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=V0(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=am(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+=am(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:wt(i,a/2,2,!1,!1),warnings:[]}}function am(e,t,n){let r=t+n;return r<1/0?Vt(e,r):Vt(V(e,-520),V(t,-1040)+V(n,-1040))}function V0(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],h=o[m];typeof u=="number"&&Number.isNaN(u)||typeof h=="number"&&Number.isNaN(h)||n.push([u,h])}let s=m=>Array.from(new Set(m)).sort(zn),i=s(n.map(m=>m[0])),c=s(n.map(m=>m[1]));if(i.length<2||i.length!==c.length)throw new RangeError("'x' and 'y' must have the same number of levels (minimum 2)");let a=new Map(i.map((m,u)=>[m,u])),l=new Map(c.map((m,u)=>[m,u])),f=i.map(()=>new Array(i.length).fill(0));for(let[m,u]of n)f[a.get(m)][l.get(u)]++;return f}var $0=["ratio","alternative","confLevel"],Li=2**-1022;function im(e,t,n={}){if(B(n,$0,"varTest"),typeof e=="string"){let v=tr(e,t);return im(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=Ze(n.alternative),c=n.confLevel??.95;_t(c);let a=r.length-1,l=o.length-1;if(a<1)throw new Error("not enough 'x' observations");if(l<1)throw new Error("not enough 'y' observations");let[f,m]=xt([r,o]).variance,u=0;if(!(f>=Li&&m>=Li)){let v=xt([r]),E=xt([o]);f=v.variance[0],m=E.variance[0],u=2*(v.k-E.k)}if(f===0&&m===0)throw new RangeError("both samples have zero variance");let h=u===0?f/m:Ve(f/m,u),p=h/s,w=ht(p,a,l,!0,!1),d=ht(p,a,l,!1,!1),b,N;if(i==="less")b=w,N=[0,h/un(1-c,a,l,!0,!1)];else if(i==="greater")b=d,N=[h/un(c,a,l,!0,!1),1/0];else{b=Math.min(1,2*Math.min(w,d));let v=(1-c)/2,E=v<1e-8?un(v,a,l,!1,!1):un(1-v,a,l,!0,!1);N=[h/E,h/un(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 cm(e,t){if(typeof e=="string"){let p=yo(e,t);return cm(Al(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=xt(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<Li)){let p=o.map(E=>{let g=xt([E]);return{w:g.variance[0],e:g.k}}),w=Math.max(...p.map(E=>E.e)),d=p.map(E=>Math.log(E.w)+2*(E.e-w)*Math.LN2),b=d.map((E,g)=>Math.log(s[g])+E),N=Math.max(...b),v=N+Math.log(b.reduce((E,g)=>E+Math.exp(g-N),0))-Math.log(c);m=c*v-d.reduce((E,g,y)=>E+s[y]*g,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:wt(u,h/2,2,!1,!1),warnings:[]}}var z0=["alternative"],Ci=["alternative"];function Fi(e,t,n,r){return{method:e,statistic:n,statisticName:t,parameter:r,parameterName:"df",pValue:wt(n,r/2,2,!1,!1),warnings:[]}}function lm(e){let t=0;for(let n of wn(e))t+=n*n*n-n;return t}function Us(e,t){if(typeof e=="string"){let l=yo(e,t);return Us(l.values,l.group.codes)}let n=vr(e,t,"error"),r=n.length;if(r<2)throw new RangeError("all observations are in the same group");let o=n.flat(),s=o.length,i=bt(o),c=0,a=0;for(let l of n){let f=0;for(let m=0;m<l.length;m++)f+=i[a+m];a+=l.length,c+=f*f/l.length}return c=(12*c/(s*(s+1))-3*(s+1))/(1-lm(o)/(s*s*s-s)),Fi("Kruskal-Wallis rank sum test","Kruskal-Wallis chi-squared",c,r-1)}function Oi(e,t){if(typeof e=="string")return Oi(vs(e,t));let n=Fa(e),r=n.length,o=e[0]?.length??0;if(r<2||o<2)throw new RangeError("'y' must have at least two complete rows and two columns");let s=new Array(o).fill(0),i=0;for(let l of n){let f=bt(l);for(let m=0;m<o;m++)s[m]+=f[m];i+=lm(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 Fi("Friedman rank sum test","Friedman chi-squared",a,o-1)}function j0(e,t={},n,r={}){typeof e!="string"&&ct(t)&&B(t,Ci,"pageTest"),ct(n)&&B(n,Ci,"pageTest"),B(r,Ci,"pageTest");let o,s;if(typeof e=="string")o=vs(e,t),s=n??{};else if(Array.isArray(n)){let w=Array.from(e),d=t,b=n;if(w.length!==d.length||w.length!==b.length)throw new RangeError("'x', 'groups' and 'blocks' must have the same length");if(!w.every(Number.isFinite))throw new RangeError("missing or non-finite values are not allowed");let N=tn(d),v=tn(b);if(N.some(M=>typeof M=="number"&&Number.isNaN(M))||v.some(M=>typeof M=="number"&&Number.isNaN(M)))throw new RangeError("missing treatment or block labels are not allowed");let E=Array.from(new Set(N)).sort(zn),g=Array.from(new Set(v)).sort(zn);if(E.length<2||g.length<1)throw new RangeError("at least two treatments and one block are required");let y=new Map(E.map((M,R)=>[M,R])),x=new Map(g.map((M,R)=>[M,R]));o=g.map(()=>new Array(E.length).fill(NaN));for(let M=0;M<w.length;M++){let R=x.get(v[M]),T=y.get(N[M]);if(!Number.isNaN(o[R][T]))throw new RangeError("not an unreplicated complete block design");o[R][T]=w[M]}if(o.some(M=>M.some(R=>Number.isNaN(R))))throw new RangeError("not an unreplicated complete block design");s=r}else o=e.map(w=>Array.from(w)),s=t;let i=o.length,c=o[0]?.length??0;if(i<1||c<2||o.some(w=>w.length!==c))throw new RangeError("'y' must be a non-empty matrix with at least two columns");if(o.some(w=>w.some(d=>!Number.isFinite(d))))throw new RangeError("missing or non-finite values are not allowed in a block");let a=Ze(s.alternative),l=new Array(c).fill(0);for(let w of o){let d=bt(w);for(let b=0;b<c;b++)l[b]+=d[b]}let f=0;for(let w=0;w<c;w++)f+=(w+1)*l[w];let m=i*c*(c+1)**2/4,u=i*c**2*(c-1)*(c+1)**2/144,h=(f-m-.5)/Math.sqrt(u),p=a==="less"?se(h):a==="greater"?se(h,{lowerTail:!1}):2*se(-Math.abs(h));return{method:"Page's ordered aligned rank sum test",alternative:a,statistic:h,statisticName:"z",pValue:p,estimate:f,warnings:[]}}function W0(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{...Oi(e),method:"Cochran's Q test",statisticName:"Q"}}function fm(e,t){if(typeof e=="string")return fm(vs(e,t));let n=Fa(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(w=>Nt(w)-an(w));s.some(w=>w===1/0)&&n.every(w=>w.every(Number.isFinite))&&(s=qe(n).groups.map(w=>Nt(w)-an(w)));let i=bt(s),c=new Array(o).fill(0),a=0;n.forEach((w,d)=>{let b=bt(w);for(let N=0;N<o;N++){let v=i[d]*(b[N]-(o+1)/2);a+=v*v,c[N]+=v}});let l=0;for(let w of c)l+=w*w;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 w=0;for(let d=2;d<=o;d++)w+=Math.log(d);h=Math.exp((1-r)*w)}else u=(r-1)*l/(a-l),h=ht(u,f,m,!1,!1);return{method:"Quade test",statistic:u,statisticName:"Quade F",parameter:p,parameterName:["num df","denom df"],pValue:h,warnings:[]}}function mm(e,t){if(typeof e=="string"){let h=yo(e,t);return mm(h.values,h.group.codes)}let n=vr(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 w=xs(p);return p.map(d=>Math.abs(d-w))}),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(qe(n).groups));let i=s.length,a=bt(s).map(h=>le((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 w=0;w<h.length;w++)p+=a[u+w];u+=h.length,m+=h.length*(p/h.length-f)**2}return m/=at(a),Fi("Fligner-Killeen test of homogeneity of variances","Fligner-Killeen:med chi-squared",m,r-1)}function um(e,t,n={}){if(B(n,z0,"moodTest"),typeof e=="string"){let E=tr(e,t);return um(E.x,E.y,n)}let r=Array.from(e).filter(Number.isFinite),o=Array.from(t).filter(Number.isFinite),s=Ze(n.alternative),i=r.length,c=o.length,a=i+c;if(a<3)throw new RangeError("not enough observations");if(i===0||c===0)throw new RangeError("not enough observations in each sample");let l=r.concat(o),f=Array.from({length:a},(E,g)=>g).sort((E,g)=>l[E]-l[g]),m=new Array(a),u=0;for(;u<a;){let E=u;for(;E+1<a&&l[f[E+1]]===l[f[u]];)E++;let g=0;for(let y=u;y<=E;y++)g+=(y+1-(a+1)/2)**2;g/=E-u+1;for(let y=u;y<=E;y++)m[f[y]]=g;u=E+1}let h=0;for(let E=0;E<i;E++)h+=m[E];let p=0;for(let E of m)p+=E;p/=a;let w=0;for(let E of m)w+=(E-p)**2;let d=i*p,b=i*c/(a*(a-1))*w,N=(h-d)/Math.sqrt(b),v=s==="less"?se(N):s==="greater"?se(N,{lowerTail:!1}):2*se(-Math.abs(N));return{method:"Mood two-sample test of scale",alternative:s,statistic:N,statisticName:"Z",pValue:v,warnings:[]}}function Rr(e,t){let n=0;for(let r=e.length-1;r>=0;r--)n=n*t+e[r];return n}var B0=[0,.221157,-.147981,-2.07119,4.434685,-2.706056],K0=[0,.042981,-.293762,-1.752461,5.682633,-3.582633],U0=[.544,-.39978,.025054,-6714e-7],H0=[1.3822,-.77857,.062767,-.0020322],Y0=[-1.5861,-.31082,-.083751,.0038915],G0=[-.4803,-.082676,.0030302],X0=[-2.273,.459];function Q0(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]=-le((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+Rr(B0,i),a=new Float64Array(n);a[0]=c;let l,f;if(e>5){let m=r[1]/s+Rr(K0,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 hm(e){let t=qe([e]).groups[0],n=t.length,r=Q0(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 Z0=1.90985931710274,J0=1.0471975511966;function pm(e,t){if(Number.isNaN(e)||!(t>=3))return NaN;if(t===3){let i=Z0*(Math.asin(Math.sqrt(1-e))-J0);return Math.max(i,0)}let n=Math.log(e);if(t<=11){let i=Rr(X0,t);if(n>=i)return 1e-99;let c=Rr(U0,t),a=Math.exp(Rr(H0,t));return se(-Math.log(i-n),{mean:c,sd:a,lowerTail:!1})}let r=Math.log(t),o=Rr(Y0,r),s=Math.exp(Rr(G0,r));return se(n,{mean:o,sd:s,lowerTail:!1})}var bm=Math.log(2*Math.PI),dm=2e-14,gm=500;function eg(e){return Math.exp(-.5*e*e-.5*bm)}function tg(e){return se(e,{logP:!0})}function vm(e,t=-1/0,n=!1){let r=t===-1/0?La(e,0,2,0,dm,gm):La(e,t,1,0,dm,gm),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 wm(e,t,n){let r=t.map(Math.sqrt),o=t.map(i=>Math.sqrt(1-i));return vm(i=>{let c=0;for(let a=0;a<r.length;a++){let l=(r[a]*i+e)/o[a];if(!n){c+=tg(l);continue}let f=(r[a]*i-e)/o[a],m=se(f)+se(l,{lowerTail:!1});m<.5?c+=Math.log1p(-m):c+=Math.log(Math.max(0,l+f<0?se(l)-se(f):se(f,{lowerTail:!1})-se(l,{lowerTail:!1})))}return eg(i)*-Math.expm1(c)})}function Tr(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,wm(e,t,r));let o=n/2,s=Math.log(2)+.5*Math.log(o)-.5*bm-ol(o);return Math.min(1,vm(c=>{let a=s-o*(Math.expm1(2*c)-2*c);return a<-745?0:Math.exp(a)*wm(e*Math.exp(c),t,r)},-1/0,!0))}var ng=["pAdjustMethod"],rg=["pAdjustMethod","alternative"],og=["alternative"],sg=["pAdjustMethod","alternative"],ag=["method","altp"],ig=["alternative"],cg=["welch"],Nm="Ties are present. z-quantiles were corrected for ties.";function An(e,t){let{labels:n,groups:r}=nr(e,t),o=r.length;if(o<2)throw new RangeError("there must be at least two groups");let s=r.map(i=>i.length);return{labels:n,groups:r,n:s,N:s.reduce((i,c)=>i+c,0),k:o}}function Di(e){let t=e.groups.flat(),n=bt(t),r=[],o=0;for(let i of e.n){let c=0;for(let a=0;a<i;a++)c+=n[o+a];o+=i,r.push(c/i)}let s=0;for(let i of wn(t))s+=i*i*i-i;return{R:r,C:s/(12*(e.N-1))}}function Sr(e,t){let n=Array.from({length:e-1},()=>new Array(e-1).fill(NaN));for(let r=0;r<e-1;r++)for(let o=r+1;o<e;o++)n[o-1][r]=t(o,r);return n}function lg(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=Kr(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 Do(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 Pi(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 Hs=e=>se(e,{lowerTail:!1}),Vi=(e,t,n)=>hs(Math.abs(e),t,n,{lowerTail:!1});function $i(e){let t=xt(e.groups),n=0;for(let r of t.n)r>1&&(n+=r-1);return Po(n,t.ssWithin),{m:t.mean,s2:t.ssWithin/n,df:e.N-e.k,k:t.k}}function Po(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 fg(e,t,n={}){B(n,ng,"kwAllPairsDunnTest");let r=n.pAdjustMethod??"holm",o=An(e,t),{R:s,C:i}=Di(o),c=o.N*(o.N+1)/12-i,a=Sr(o.k,(f,m)=>Math.abs(s[f]-s[m])/Math.sqrt(c*(1/o.n[f]+1/o.n[m]))),l=lg(a.map(f=>f.map(m=>2*Hs(m))),r);return Q("kwAllPairsDunnTest",Do(o,"Dunn's all-pairs test",a,l,r,i!==0?[Nm]:[]))}function mg(e,t,n={}){B(n,rg,"kwManyOneDunnTest");let r=Ze(n.alternative),o=n.pAdjustMethod??"single-step",s=An(e,t),{R:i,C:c}=Di(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=ym(f,m,r,o);return Q("kwManyOneDunnTest",Pi(s,"Dunn's many-to-one test",f,u,o,c!==0?[Nm]:[]))}function ym(e,t,n,r){return r==="single-step"?e.map(o=>n==="two.sided"?Tr(o,t,1/0,!0):Tr(n==="greater"?o:-o,t,1/0,!1)):Kr(e.map(o=>n==="two.sided"?2*Hs(Math.abs(o)):Hs(n==="greater"?o:-o)),r)}function ug(e,t,n={}){B(n,og,"dunnettTest");let r=Ze(n.alternative),o=An(e,t),{m:s,s2:i,df:c}=$i(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*Tr(Math.abs(h),m,c,!1)):Tr(r==="greater"?h:-h,m,c,!1));return Q("dunnettTest",Pi(o,`Dunnett's-test for multiple
4
+ comparisons with one control`,f,u,"single-step"))}function hg(e,t,n={}){B(n,sg,"manyOneUTest");let r=Ze(n.alternative),o=n.pAdjustMethod??"single-step",s=An(e,t);if(o==="single-step"&&s.k<3)throw new RangeError(`for 'p.adjust.method = single-step' at least
5
5
 
6
- 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=vt(h),w=0;for(let x=0;x<i;x++)w+=p[x];let d=i+u,b=d*(d+1)/2-w,y=i*u+i*(i+1)/2-w,v=i*u+u*(u+1)/2-b,M=0;for(let x of bn(h))M+=x*x*x-x;let g=i*u/(d*(d-1))*((d*d*d-d)/12-M/12),N=(Math.min(y,v)-i*u/2)/Math.sqrt(g);return w<b?-N:N}),l=c.map(m=>s.n[m]/(s.n[m]+i)),f=wm(a,l,r,o);return Q("manyOneUTest",Ci(s,`Wilcoxon, Mann, Whittney U-test
7
- for multiple comparisons with one control`,a,f,o))}function ig(e,t){let n=Wn(e,t),r=Oo(n.k,(s,i)=>{let c=n.n[s],a=n.n[i],l=[...n.groups[s],...n.groups[i]],f=vt(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,w=0;for(let b of bn(l))w+=b*b*b-b;let d=c*a/(p*(p-1))*((p*p*p-p)/12-w/12);return Math.SQRT2*(h-c*a/2)/Math.sqrt(d)}),o=r.map(s=>s.map(i=>Number.isNaN(i)?NaN:Fi(i,n.k,1/0)));return Q("dscfAllPairsTest",Ks(n,"Dwass-Steele-Critchlow-Fligner all-pairs test",r,o,"single-step"))}function cg(e,t){let n=Wn(e,t),r=Dt(n.groups),o=r.mean,s=r.variance.map((a,l)=>a/n.n[l]),i=Oo(n.k,(a,l)=>(o[a]-o[l])/Math.sqrt(s[a]+s[l])*Math.SQRT2),c=Oo(n.k,(a,l)=>{let f=cl([r.variance[a],r.variance[l]],[n.n[a],n.n[l]]);return Fi(i[a-1][l],n.k,f)});return Q("gamesHowellTest",Ks(n,"Games-Howell test",i,c,"none"))}function lg(e,t){let n=Wn(e,t),{m:r,s2:o,df:s}=Oi(n);if(s<2)throw new RangeError("the studentized range needs at least 2 residual degrees of freedom");let i=Oo(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:Fi(l,n.k,s)));return Q("tukeyTest",Ks(n,"Tukey's test",i,c,"single-step"))}function fg(e,t,n={}){B(n,eg,"dunnTest");let r=n.method??"none",o=n.altp??!1,s=Wn(e,t),i=Ws(e,t).statistic,{R:c,C:a}=Li(s),l=s.N*(s.N+1)/12-a,f=[],m=[];for(let p=1;p<s.k;p++)for(let w=0;w<p;w++)f.push((c[w]-c[p])/Math.sqrt(l*(1/s.n[w]+1/s.n[p]))),m.push(`${s.labels[w]} - ${s.labels[p]}`);let u=f.map(p=>(o?2:1)*Bs(Math.abs(p))),h=mg(u,r);return{chi2:i,Z:f,P:u,PAdjusted:h,zValue:f,pValue:u,pAdjusted:h,comparisons:m,warnings:[]}}function mg(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 ug(e,t,n={}){B(n,tg,"glhtDunnett");let r=Ze(n.alternative),o=Wn(e,t),{m:s,s2:i,df:c,k:a}=Oi(o),l=Math.sqrt(i),f=o.n.slice(1).map((y,v)=>v+1),m=f.map(y=>s[y]-s[0]),u=f.map(y=>l*Math.sqrt(1/o.n[y]+1/o.n[0])),h=m.map((y,v)=>y/u[v]),p=m.map(y=>V(y,a)),w=u.map(y=>V(y,a)),d=f.map(y=>o.n[y]/(o.n[y]+o.n[0])),b=h.map(y=>r==="two.sided"?Tr(y,d,c,!0):Tr(r==="greater"?y:-y,d,c,!1));return{names:f.map(y=>`${o.labels[y]} - ${o.labels[0]}`),estimate:p,se:w,t:h,p:b,stdError:w,tValue:h,pValue:b,df:c,warnings:[]}}var bm=["varEqual"],hg=2**-1022;function Nm(e,t,n={}){if(typeof e!="string"&&wt(t)&&B(t,bm,"onewayTest"),B(n,bm,"onewayTest"),typeof e=="string"){let d=Rl(e,t);return Nm(d.values,d.groups,n)}let r;t!==void 0&&!Array.isArray(t)?n=t:r=t;let o=xr(e,r),s=Dt(o),i=s.groups,c=n.varEqual??!1,a=i.length;if(a<2)throw new RangeError("not enough groups");let l=i.map(d=>d.length);if(l.some(d=>d<2))throw new RangeError("not enough observations");let f=s.mean,m=s.variance,u=l.reduce((d,b)=>d+b,0),h,p,w;if(c){let d=s.grandMean,b=0,y=0;for(let v=0;v<a;v++)b+=l[v]*(f[v]-d)**2,y+=(l[v]-1)*m[v];p=u-a,Do(p,s.ssWithin),h=b/(a-1)/(y/p)}else if(m.some(d=>!(d>=hg))&&(w=dg(o))!==void 0){let d=w.map((E,R)=>vm(l[R]/E.variance,-2*E.k)),b=Math.max(...d.map(E=>E.e)),y=d.map(E=>Ve(E.m,E.e-b)),v=y.reduce((E,R)=>E+R,0),M=0;for(let E=0;E<a;E++){let R=y.reduce((T,A,S)=>S===E?T:T+A,0);M+=(R/v)**2/(l[E]-1)}M/=a*a-1;let g=[];for(let E=0;E<a;E++)for(let R=E+1;R<a;R++){let[T,A]=w[E].k>=w[R].k?[w[E],w[R]]:[w[R],w[E]],S=T.mean-Ve(A.mean,A.k-T.k);if(S===0)continue;let C=vm(S,T.k);g.push({m:d[E].m*d[R].m*C.m*C.m/v,e:d[E].e+d[R].e+2*C.e-b})}let N=Math.max(...g.map(E=>E.e)),x=g.reduce((E,R)=>E+Ve(R.m,R.e-N),0);h=(g.length===0?0:Ve(x,N))/((a-1)*(1+2*(a-2)*M)),p=1/(3*M)}else if(m.some(d=>d===0)){let d=m.flatMap((M,g)=>M===0?[g]:[]);if(d.length>1)throw new RangeError("Welch's test is not defined with two or more constant groups");let b=d[0],y=0,v=0;for(let M=0;M<a;M++)M!==b&&(y+=1/(l[M]-1),v+=l[M]/m[M]*(f[M]-f[b])**2);y/=a*a-1,h=v/((a-1)*(1+2*(a-2)*y)),p=1/(3*y)}else{let d=l.map((g,N)=>g/m[N]),b=d.reduce((g,N)=>g+N,0),y=0,v=0;for(let g=0;g<a;g++)y+=(1-d[g]/b)**2/(l[g]-1),v+=d[g]*f[g];y/=a*a-1,v/=b;let M=0;for(let g=0;g<a;g++)M+=d[g]*(f[g]-v)**2;h=M/((a-1)*(1+2*(a-2)*y)),p=1/(3*y)}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:ht(h,a-1,p,!1,!1),warnings:[]}}function pg(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=lm(t);return{method:"Shapiro-Wilk normality test",statistic:1-r,statisticName:"W",pValue:fm(r,n),warnings:[]}}function dg(e){let t=[];for(let n of e){let r=Dt([n]),o=r.variance[0];if(!(o>0))return;t.push({mean:r.mean[0],variance:o,k:r.k})}return t}function vm(e,t){let n=Math.floor(Math.log2(Math.abs(e))),r=Ve(e,-n);return Math.abs(r)>=2?(r/=2,n++):Math.abs(r)<1&&(r*=2,n--),{m:r,e:t+n}}function Di(e,t,n){let r=[0,0];for(let o=Math.max(0,t);o<Math.min(n,e.length);o++)r=he(r,[e[o],0]);return r[0]}function gg([[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]=vn(f,o,s,i,!1);return{lo:c,d:l}}function wg(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 Us=class{lo;hi;logNull;constructor(t){let n=0,r=new Float64Array([1]);for(let o of t){let s=gg(o);n+=s.lo,r=wg(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=Di(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=he(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?Di(o,s,o.length):Di(o,0,s+1)}};function Pi(e,t){return tr(e,t)}function ym(e,t){if(t===e.lo)return 0;if(t===e.hi)return 1/0;let n=e.mean(1);return n===t?1:Pi(r=>e.mean(r)-t,n>t)}function Vi(e,t,n){if(t===e.hi)return 1/0;let r=e.cdf(t,1,!1);return r===n?1:Pi(o=>n-e.cdf(t,o,!1),r<n)}function $i(e,t,n){if(t===e.lo)return 0;let r=e.cdf(t,1,!0);return r===n?1:Pi(o=>e.cdf(t,o,!0)-n,r>n)}var bg=["alternative","correct","exact","confLevel"];function vg(e,t={}){B(t,bg,"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(zi);if(s.some(f=>f<2))throw new RangeError("sample size in each stratum must be > 1");let i=Ze(t.alternative),c=t.correct??!0,a=t.exact??!1,l=t.confLevel??.95;if(_t(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?Mg(f,i,l):Ng(f,s,i,c,l)}return Eg(e,s,r,o)}function zi(e){return e.reduce((t,n)=>t+n.reduce((r,o)=>r+o,0),0)}function xm(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 Rm(e,t){return e.map(n=>n.map(r=>r.map(o=>V(o,-t))))}function Mm(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]],w)=>{let d=t[w],b=m+u,y=h+p,v=m+h,M=u+p;r+=m;let g=b*v/d;o+=Number.isFinite(g)?g:b/d*v;let N=b*y*v*M/(d*d*(d-n));s+=Number.isFinite(N)?N:b/d*(y/d)*(v/(d-n))*M;let x=(m+p)/d,E=(u+h)/d,R=m*p/d,T=u*h/d,A=Number.isFinite(R)?R:m/d*p,S=Number.isFinite(T)?T:u/d*h;i+=A,c+=S,a+=x*A,l+=x*S+E*A,f+=E*S}),{sa:r,se:o,sv:s,sR:i,sS:c,sPR:a,sPSQR:l,sQS:f}}function Ng(e,t,n,r,o){let s=Mm(e,t,1),i=0;if(!t.every(Number.isFinite)||!Object.values(s).every(Number.isFinite)){i=xm(e);let x=Rm(e,i);s=Mm(x,x.map(zi),V(1,-i))}let{sa:c,se:a,sv:l,sR:f,sS:m,sPR:u,sPSQR:h,sQS:p}=s,w=c-a,d=V(.5,-i),b=r&&Math.abs(w)>=d?d:0,y=V(Vt(Math.abs(w)-b,l),i),v=n==="two.sided"?bt(y,.5,2,!1,!1):ae(Math.sign(w)*Math.sqrt(y),{lowerTail:n==="less"}),M=f/m,g=V(Math.sqrt(u/(2*f*f)+h/(2*f*m)+p/(2*m*m)),-i/2),N;if(n==="less")N=[0,M*Math.exp(le(o)*g)];else if(n==="greater")N=[M*Math.exp(le(o,{lowerTail:!1})*g),1/0];else{let x=(1-o)/2,E=x<1e-8?le(x,{lowerTail:!1}):le((1+o)/2);N=[M*Math.exp(-E*g),M*Math.exp(E*g)]}return{method:`Mantel-Haenszel chi-squared test ${b!==0?"with":"without"} continuity correction`,alternative:n,statistic:y,statisticName:"Mantel-Haenszel X-squared",parameter:1,parameterName:"df",pValue:v,confInt:N,confLevel:o,estimate:M,estimateNames:"common odds ratio",nullValue:1,nullValueName:"common odds ratio",warnings:[]}}var yg=2**24;function Mg(e,t,n){let r=e.reduce((a,[[l,f],[m,u]])=>a+Math.min(l,u)+Math.min(f,m),1);if(r>yg)throw new RangeError(`the exact distribution of S has ${r} values, more than 2^24: use exact: false`);let o=new Us(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=he(f,[m,0]));i=Math.min(1,f[0])}else i=o.cdf(s,1,t==="greater");let c;if(t==="less")c=[0,Vi(o,s,1-n)];else if(t==="greater")c=[$i(o,s,1-n),1/0];else{let a=(1-n)/2;c=[$i(o,s,a),Vi(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:ym(o,s),estimateNames:"common odds ratio",nullValue:1,nullValueName:"common odds ratio",warnings:[]}}function Eg(e,t,n,r){let{diff:o,V:s}=Em(e,t,n,r,1),i=0;if(!t.every(Number.isFinite)||!o.every(Number.isFinite)||!s.every(f=>f.every(Number.isFinite))){i=xm(e);let f=Rm(e,i);({diff:o,V:s}=Em(f,f.map(zi),n,r,V(1,-i)))}let c=o.length,a=nt(s,o),l=0;for(let f=0;f<c;f++)l+=o[f]*a[f];return l=V(l,i),{method:"Cochran-Mantel-Haenszel test",statistic:l,statisticName:"Cochran-Mantel-Haenszel M^2",parameter:c,parameterName:"df",pValue:bt(l,c/2,2,!1,!1),warnings:[]}}var xg=2**-1022;function Em(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((w,d)=>w+d,0)),u=Array.from({length:r},(p,w)=>a.reduce((d,b)=>d+b[w],0)),h=1/(f*f*(f-o));for(let p=0;p<r-1;p++)for(let w=0;w<n-1;w++){let d=w+(n-1)*p,b=m[w]*u[p]/f;i[d]+=a[w][p]-(Number.isFinite(b)?b:ls(m[w],u[p],f));for(let y=0;y<r-1;y++)for(let v=0;v<n-1;v++){let M=(w===v?f*m[w]:0)-m[w]*m[v],g=(p===y?f*u[p]:0)-u[p]*u[y],N=h*M*g;c[d][v+(n-1)*y]+=Number.isFinite(N)&&h>=xg?N:m[w]*((w===v?1:0)-m[v]/f)*(((p===y?1:0)-u[y]/f)*(u[p]/(f-o)))}}}),{diff:i,V:c}}var Tm=new Map,Rg=2**24,Tg=2**32;function Sm(e,t){let n=e+t,r=(e+1)*(Math.floor((n+1)**2/4)+1);if(2*r>Rg||n*r>Tg)throw new RangeError(`the exact distribution for m = ${e} and n = ${t} is too large to compute: use exact: false`)}function Ag(e,t){let n=`${e},${t}`,r=Tm.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],w=Array.from({length:e+1},()=>new Float64Array(i+1));for(let d=0;d<=e;d++){let b=c[d],y=e-d;if(y>h)continue;let v=y/h;for(let M=0;M<=i;M++){let g=b[M];g!==0&&(y>0&&(w[d+1][M+p]+=g*v),y<h&&(w[d][M]+=g*(1-v)))}}c=w}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 Tm.set(n,m),m}function ji(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}=Ag(t,n),i=nl(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 Am=new Map;function Sg(e,t){let n=`${e},${t}`,r=Am.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 w=i[p];for(let d=0;d<=s;d++){let b=w[d];b!==0&&(p<e&&(h[p+1][d+u]+=b),e-p<o-m&&(h[p][d]+=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 Am.set(n,f),f}function Hs(e,t,n){let{min:r,counts:o}=Sg(t,n);if(e<=0)return r;if(e>=1)return r+o.length-1;let s=ll(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 qg=["alternative","exact","confInt","confLevel"];function _m(e,t,n={}){if(B(n,qg,"ansariTest"),typeof e=="string"){let g=er(e,t);return _m(g.x,g.y,n)}let r=Array.from(e).filter(g=>!Number.isNaN(g)),o=Array.from(t).filter(g=>!Number.isNaN(g)),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=Ze(n.alternative),a=n.confInt??!1,l=n.confLevel??.95;a&&_t(l);let f=s+i,m=vt(r.concat(o)),u=m.map(g=>Math.min(g,f-g+1)),h=0;for(let g=0;g<s;g++)h+=u[g];let p=new Set(m).size<f,w=n.exact??(s<50&&i<50&&!p),d=n.exact??(s<50&&i<50),b=new Et,y,v;if(w&&!p){Sm(s,i);let g=x=>ji(x-1,s,i,!1),N=x=>ji(x,s,i,!0);if(c==="two.sided"){let x=Math.floor((s+1)**2/4)+Math.floor(s*i/2)/2;y=Math.min(1,2*(h>x?g(h):N(h)))}else c==="less"?y=g(h):y=N(h);a&&(v=Lg(r,o,c,l,b))}else{let g=Lm(h,u,p,s,i);y=c==="two.sided"?2*Math.min(ae(g),ae(g,{lowerTail:!1})):c==="less"?ae(g,{lowerTail:!1}):ae(g),a&&(v=Cg(r,o,c,l,d?new Et:b)),d&&p&&(b.add("cannot compute exact p-value with ties"),a&&b.add("cannot compute exact confidence interval with ties"))}let M={method:"Ansari-Bradley test",alternative:c,statistic:h,statisticName:"AB",pValue:y,warnings:b.list};return v!==void 0&&(M.confInt=v.confInt,M.confLevel=l,M.estimate=v.estimate,M.estimateNames="ratio of scales"),M.nullValue=1,M.nullValueName="ratio of scales",Q("ansariTest",M)}function Lm(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 Cm(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)<kg))?Ig(e.map(f=>qm(f,n)).concat(t.map(f=>qm(f,1)))):_g(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 kg=2**-1022;function qm(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]=km(Math.abs(e)),[s,i]=km(t),c=o/i,a=r-s;return c<1&&(c*=2,a-=1),[n,a,c]}function km(e){let t=Math.floor(Math.log2(e)),n=V(e,-t);return n>=2?(n/=2,t+=1):n<1&&(n*=2,t-=1),[t,n]}function Im(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 Ig(e){let t=e.map((r,o)=>o).sort((r,o)=>Im(e[r],e[o])),n=new Array(e.length);for(let r=0;r<t.length;){let o=r+1;for(;o<t.length&&Im(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 _g(e){let t=e.filter(s=>!Number.isNaN(s));if(t.length===e.length)return vt(e);let n=vt(t),r=0,o=t.length;return e.map(s=>Number.isNaN(s)?++o:n[r++])}function Ys(e,t,n){return e==="two.sided"?n(t):e==="greater"?[n(2*t)[0],1/0]:[0,n(2*t)[1]]}function Lg(e,t,n,r,o){let s=e.length,i=t.length,c=[];for(let b of e)for(let y of t)b/y>=0&&c.push(b/y);c.sort((b,y)=>b-y);let a=c.length;if(a===0)return o.add("cannot compute confidence set, returning NA"),{confInt:Ys(n,1-r,()=>[NaN,NaN]),estimate:NaN};let l=c.map((b,y)=>Cm(e,t,b+(y<a-1?(c[y+1]-b)/2:b*1.01)).ab),f=(b,y,v)=>{let M=1/0;for(let N of l)(y?N-b>0:N-b>=0)&&N-b<M&&(M=N-b);let g=v?-1/0:1/0;return l.forEach((N,x)=>{N-b===M&&(g=v?Math.max(g,c[x]):Math.min(g,c[x]))}),g},m=b=>{let y=Hs(b/2,s,i),v=Hs(1-b/2,s,i);if(!l.some(N=>N-y>=0)||!l.some(N=>N-v>0))return o.add("samples differ in location: cannot compute confidence set, returning NA"),[NaN,NaN];let M=f(y,!1,!1),g=f(v,!0,!0);return M>g?[f(v,!1,!1),f(y,!0,!0)]:[M,g]},u=Ys(n,1-r,m),h=Hs(.5,s,i),p=-1/0,w=1/0;l.forEach((b,y)=>{b-h<=0?p=Math.max(p,c[y]):w=Math.min(w,c[y])});let d=p===-1/0||w===1/0?NaN:(w+p)/2;return d===1/0&&(d=w/2+p/2),{confInt:u,estimate:d}}function Cg(e,t,n,r,o){let s=e.length,i=t.length,c=(b,y)=>{let{ab:v,a:M,ties:g}=Cm(e,t,b);return Lm(v,M,g,s,i)-y},a=[],l=(b,y)=>{b.length===0||y.length===0||a.push(an(b)/Nt(y),Nt(b)/an(y))};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:Ys(n,1-r,()=>[NaN,NaN]),estimate:NaN};let f=an(a),m=Nt(a),u=b=>Bt(y=>c(y,b),f,m,{tol:1e-4}),h=b=>{let y=le(b/2),v=le(b/2,{lowerTail:!1});if(c(f,y)>0||c(m,v)<0)return o.add("samples differ in location: cannot compute confidence set, returning NA"),[NaN,NaN];let M=u(y),g=u(v);return M<=g?[M,g]:[g,M]},p=Ys(n,1-r,h),w=c(f,0)>0||c(m,0)<0;w&&o.add("cannot compute estimate, returning NA");let d=w?NaN:u(0);return{confInt:p,estimate:d}}var Fg=["alternative","confLevel"];function Og(e,t,n={}){B(n,Fg,"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=Ze(n.alternative),a=n.confLevel??.95;cn(a);let l=vt(r.concat(o)),f=l.slice(0,s),m=l.slice(s),u=E=>E.reduce((R,T)=>R+T,0)/E.length,h=u(f),p=u(m),w=(E,R,T)=>{let A=(E.length+1)/2,S=0;for(let C=0;C<E.length;C++)S+=(E[C]-R[C]-T+A)**2;return S/(E.length-1)},d=w(f,vt(r),h),b=w(m,vt(o),p),y=(p-(i+1)/2)/s,v=s*d+i*b,M=s*i*(p-h)/(s+i)/Math.sqrt(v),g=v**2/((s*d)**2/(s-1)+(i*b)**2/(i-1)),N;c==="greater"?N=dt(M,g,!0,!1):c==="less"?N=dt(M,g,!1,!1):N=2*dt(-Math.abs(M),g,!0,!1);let x=gt((1-a)/2,g,!1,!1)*Math.sqrt(d/(s*i**2)+b/(i*s**2));return{method:"Brunner-Munzel Test",alternative:c,statistic:M,statisticName:"Brunner-Munzel Test Statistic",parameter:g,parameterName:"df",pValue:N,confInt:[y-x,y+x],confLevel:a,estimate:y,estimateNames:"P(X<Y)+.5*P(X=Y)",warnings:[]}}var Dg=[1.2337005501361697,7831619385924639e-32];function Pg(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 w=f[h*t+p];if(w!==0)for(let d=0;d<t;d++)u[h*t+d]+=w*m[p*t+d]}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 Vg=2**33;function Om(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>Vg)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 Dm(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]=Jt(e,t),u=2*f-1+2*m,h=1,p=0;for(let w=1;w<=e;w++)h*=w/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]=Pg(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 Wi(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=at(Dg,Jt(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 Bi(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]=Jt(e,t),o=Math.floor(e-n-r),s=0;for(let i=0;i<=o;i++){let c=he(Ot(e-i,-n),[-r,0]);if(c[0]<=0)break;let a=he(Ot(i,n),[r,0]),l=at(a,[e,0]),f=at(c,[e,0]);s+=tl(i,e-i,l,f,!1)/(l[0]+l[1])}return Math.min(t*s,1)}var Fm=2**28;function Pm(e,t){let[n,r]=Jt(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 w=(h+n+r)/e;w>0&&w<1&&s.push({t:w,up:!1,i:h})}if(s.length*(2*o+1)*Math.min(e+1,100)>Fm)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 w=p/(1-p);a.fill(0);for(let d=l;d<i;d++){let b=c[d];if(b===0)continue;let y=e-d,v=Math.min(y,Math.floor((y+1)*p)),M=i-d,g=hn(v,y,p,!1);for(let N=v;N<=y&&g!==0;N++)N>v&&(g=g*(y-N+1)*w/N),N>=M?m+=b*g:a[d+N]+=b*g,u++;g=v>0?hn(v-1,y,p,!1):0;for(let N=v-1;N>=0&&g!==0;N--)N<v-1&&(g=g*(N+1)/((y-N)*w)),N>=M?m+=b*g:a[d+N]+=b*g,u++}if(u>2*Fm)return NaN;[c,a]=[a,c]}if(f=h.t,h.up)i=h.i+1;else{for(let w=l;w<=h.i;w++)m+=c[w],c[w]=0;l=Math.max(l,h.i+1)}}return Math.min(m,1)}function Vm(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,w=(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)?(w>0&&(m+=w*vn(u,t,n,p,!1)),f[h]=0):f[h]=w}[l,f]=[f,l],f.fill(0)}return Math.min(m,1)}var $g=["alternative","exact"];function $m(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 zm(e,t,n={}){if(B(n,$g,"ksTest"),typeof e=="string"){let y=er(e,t);return zm(y.x,y.y,n)}let r=Array.from(e).filter(y=>!Number.isNaN(y)).sort((y,v)=>y-v),o=r.length;if(o<1)throw new RangeError("not enough 'x' data");let s=Ze(n.alternative),i=s==="two.sided"?"D":s==="greater"?"D^+":"D^-";if(typeof t=="function"){let y=-1/0,v=-1/0;for(let E=0;E<o;E++){let R=t(r[E]);if(!(R>=0&&R<=1))throw new RangeError(`the distribution function returned ${R} at ${r[E]}, not a value in [0, 1]`);y=Math.max(y,(E+1)/o-R),v=Math.max(v,R-E/o)}let M=s==="two.sided"?Math.max(y,v):s==="greater"?y:v,g=new Set(r).size<o,N=n.exact??(o<100&&!g),x;if(N&&s==="two.sided"&&M<.5&&Om(o,M),s==="two.sided")if(!N)x=Wi(Math.sqrt(o)*M,!1);else{let E=2*Bi(o,M);if(M>=.5)x=Math.min(1,E);else{let R=E<.01?Pm(o,M):NaN;x=Number.isNaN(R)?1-Dm(o,M):R}}else x=N?Bi(o,M):Math.exp(-2*o*M*M);return Q("ksTest",{method:`${N?"Exact":"Asymptotic"} one-sample Kolmogorov-Smirnov test`,alternative:$m(s,!0),exact:N,statistic:M,statisticName:i,pValue:Math.min(1,Math.max(0,x)),warnings:g?["ties should not be present for the one-sample Kolmogorov-Smirnov test"]:[]})}let c=Array.from(t).filter(y=>!Number.isNaN(y)),a=c.length;if(a<1)throw new RangeError("not enough 'y' data");let l=r.map(y=>[y,0]).concat(c.map(y=>[y,1]));l.sort((y,v)=>y[0]-v[0]);let f=0,m=0,u=0,h=0;for(let y=0;y<l.length;y++){if(l[y][1]===0?f++:m++,y+1<l.length&&l[y+1][0]===l[y][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,w=new Set(l.map(y=>y[0])).size<l.length,d=n.exact??o*a<1e4,b;if(d)b=Vm(p,o,a,w?{alternative:s,z:l.map(y=>y[0])}:{alternative:s});else{let y=o*a/(o+a);b=s==="two.sided"?Wi(Math.sqrt(y)*p,!1):Math.exp(-2*y*p*p)}return Q("ksTest",{method:`${d?"Exact":"Asymptotic"} two-sample Kolmogorov-Smirnov test`,alternative:$m(s,!1),exact:d,statistic:p,statisticName:i,pValue:Math.min(1,Math.max(0,b)),warnings:w&&!d?["p-value will be approximate in the presence of ties"]:[]})}var zg=["poolSd","paired","pAdjustMethod","alternative"],jg=["mu","paired","exact","correct","confLevel","tolRoot","pAdjustMethod","alternative"],Wg=["pAdjustMethod","correct"];function jm(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=wn(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(Er);return{levels:i,groups:i.map(c=>o.get(c))}}function Ki(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=Br(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 Ui(e,t,n,r,o=[]){return{method:e,pValue:n,rowNames:t.slice(1).map(ft),colNames:t.slice(0,-1).map(ft),pAdjustMethod:r,warnings:o}}function Bg(e,t,n={}){B(n,zg,"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=Ze(n.alternative),i=n.pAdjustMethod??"holm",{levels:c,groups:a}=jm(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=Dt(a),p=h.mean,w=0;for(let b of u)b>1&&(w+=b-1);Do(w,h.ssWithin);let d=Math.sqrt(h.ssWithin/w);f=(b,y)=>{let v=(p[b]-p[y])/(d*Math.sqrt(1/u[b]+1/u[y]));return s==="less"?dt(v,w,!0,!1):s==="greater"?dt(v,w,!1,!1):2*dt(-Math.abs(v),w,!0,!1)}}else m=r?"paired t tests":"t tests with non-pooled SD",f=(u,h)=>rt(()=>yn(a[u],a[h],{paired:r,alternative:s})).pValue;return Ui(m,c,Ki(l,f,i),i)}function Kg(e,t,n={}){B(n,jg,"pairwiseWilcoxTest");let{pAdjustMethod:r,alternative:o,...s}=n,i=r??"holm",{levels:c,groups:a}=jm(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 Et,u=Ki(l,(h,p)=>{let w=rt(()=>Io(a[h],a[p],{...s,...o===void 0?{}:{alternative:o}}));f=w.method;for(let d of w.warnings)m.add(d);return w.pValue},i);return Q("pairwiseWilcoxTest",Ui(f,c,u,i,m.list))}function Ug(e,t,n={}){if(B(n,Wg,"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 Et,l=Ki(s,(m,u)=>{let h=rt(()=>fi([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 Q("pairwisePropTest",Ui("Pairwise comparison of proportions",f,l,i,a.list))}var Po=[0,0];function Gs(e){if(!(e[0]>0))return[Math.sqrt(e[0]),0];let t=Math.sqrt(e[0]),n=Oe(e,Jt(t,t));return Ot(t,n[0]/(2*t))}function Xs(e,t){return Array.from({length:t},(n,r)=>e===void 0?[1,0]:Gs([e[r],0]))}function Ur(e,t){return Bn(e,t,[]).fit}function Bn(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=Po;for(let b=f;b<r;b++)u=he(u,_e(m[b],m[b]));if(u[0]===0){a.push(Po);continue}let h=Gs(u),p=m[f][0]<0?[-h[0],-h[1]]:h;m[f]=he(m[f],p);let w=_e(p,m[f]),d=b=>{let y=Po;for(let M=f;M<r;M++)y=he(y,_e(m[M],b[M]));let v=at(y,w);for(let M=f;M<r;M++)b[M]=Oe(b[M],_e(v,m[M]))};for(let b=f+1;b<e.length;b++)d(s[b]);d(i);for(let b of c)d(b);a.push([-p[0],-p[1]])}let l=new Array(o).fill(Po);for(let f=o-1;f>=0;f--){let m=i[f];for(let u=f+1;u<o;u++)m=Oe(m,_e(s[u][f],l[u]));l[f]=at(m,a[f])}return{fit:{qty:i,coef:l},qtv:c}}function Hr(e,t=0,n=e.length){let r=Po;for(let o=t;o<n;o++)r=he(r,_e(e[o],e[o]));return r}var Hg=1e-14;function Yg(e,t,n,r,o,s,i,c){let a=N=>V(N,2*i),l=t.length,f=n.flatMap((N,x)=>Hr(N)[0]>Hg*s[x]?[x]:[]),m=0,u=[],h=t.slice(),p=new Array(l).fill(0);if(f.length>0){let N=Lt(Array.from({length:l},(E,R)=>f.map(T=>n[T][R][0])));m=N.rank,u=N.pivot.slice(0,m).map(E=>f[E]);let x=Ur(u.map(E=>n[E]),t);h=x.qty,u.forEach((E,R)=>{for(let T=0;T<l;T++)p[T]=p[T]+n[E][T][0]*x.coef[R][0]})}let w=[];u.forEach((N,x)=>{let E=r[N],R=w.find(T=>T.term===E);R===void 0&&w.push(R={term:E,df:0,ss:[0,0]}),R.df++,R.ss=he(R.ss,_e(h[x],h[x]))});let d=l-m,b=Hr(h,m)[0],y=yi(p,b,d),v=w.filter(N=>N.term!==0);d>0&&yr(c,b)&&(b=0,v=v.map(N=>yr(c,N.ss[0])?{...N,ss:[0,0]}:N));let M=v.map(N=>o[N.term-1]),g=N=>{if(d>0){let x=b/d,E=v.map(({df:R,ss:T})=>{let A=T[0]/R/x;return[R,N(T[0]),N(T[0]/R),A,ht(A,R,d,!1,!1)]});return E.push([d,N(b),N(x),NaN,NaN]),{stratum:`Error: ${e}`,colNames:["Df","Sum Sq","Mean Sq","F value","Pr(>F)"],rowNames:[...M,"Residuals"],table:E}}return{stratum:`Error: ${e}`,colNames:["Df","Sum Sq","Mean Sq"],rowNames:M,table:v.map(({df:x,ss:E})=>[x,N(E[0]),N(E[0]/x)])}};return[g(a),g(N=>N),y]}function Wm(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=ds({response:t.response,rhs:i},Object.keys(o)).variables,a=$n(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(k=>!Number.isFinite(k)))throw new RangeError("NA/NaN/Inf in 'y'");let f=$n({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(k=>!a.warnings.includes(k))],h=f.colNames.length,p=f.colNames.map((k,F)=>qe([f.x.map(I=>I[F])]).groups[0]),w=f.x.map((k,F)=>p.map(I=>I[F])),d=h>0?Lt(w):void 0,b=d?.rank??0;b<h&&u.push("Error() model is singular");let y=d?.pivot.slice(0,b)??[],v=k=>k.map(F=>[F,0]),M=a.colNames.map((k,F)=>v(qe([a.x.map(I=>I[F])]).groups[0])),{groups:g,k:N}=qe([l]),{fit:x,qtv:E}=Bn(y.map(k=>v(p[k])),v(g[0]),M),R=g[0].map(k=>[1,k]),T=M.map(k=>Hr(k)[0]),A=[];y.forEach((k,F)=>{let I=f.assign[k],O=I===0?"(Intercept)":f.terms.termLabels[I-1],q=A.find($=>$.name===O);q===void 0&&A.push(q={name:O,idx:[]}),q.idx.push(F)}),m>b&&A.push({name:"Within",idx:Array.from({length:m-b},(k,F)=>b+F)});let S=A.map(k=>Yg(k.name,k.idx.map(F=>x.qty[F]),E.map(F=>k.idx.map(I=>F[I])),a.assign,a.terms.termLabels,T,N,R)),C=S.map(k=>k[0]),L={formula:e,multistratum:!0,strata:C,rows:a.frame.rows,warnings:u};return Kt.set(L,S.map(k=>k[1])),S.some((k,F)=>k[2]&&A[F].name!=="(Intercept)")&&Mi.set(L,[Ni]),L}var Gg=["weights","contrasts","levels","intercept"],Xg=["level"],Qg=["interval","level"],Hi=2**-1022;function Yi(e,t,n){let r=t.y;if(r===void 0)throw new TypeError("the formula has no response");let o=Hm(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=>V(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 Vo(e,t,n={}){if(B(n,Gg,"lm"),typeof e=="string"){let r=$n(Jn(e),t,{...n,dropUnusedLevels:!0});return Q("lm",_a(Yi(e,r,!1),Ol(t,r.terms.variables)))}return Zg(e,t,n)}function Zg(e,t,n){let r=n.intercept??!0,o=e.length>0&&typeof e[0]!="number"?e.map(u=>Array.from(u)):Array.from(e,u=>[u]),s=Array.from(t);if(o.some(u=>u.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((u,h)=>!Number.isNaN(u)&&o[h].every(p=>!Number.isNaN(p))&&(i===void 0||!Number.isNaN(i[h]))),a=i?.filter((u,h)=>c[h]);if(a?.some(u=>u<0))throw new RangeError("missing or negative weights not allowed");let l=o.filter((u,h)=>c[h]).map(u=>r?[1,...u]:u),f=s.filter((u,h)=>c[h]),m=Hm(l,f,a,r).result;return m.rows=c.flatMap((u,h)=>u?[h]:[]),_a(m,s.length)}function Jg(e,t,n,r,o,s,i){let c=s.rank,a=s.pivot.slice(0,c),l=n?.map(g=>V(g,-r)),f=Xs(l===void 0?void 0:o.map(g=>l[g]),o.length).map((g,N)=>{if(n===void 0||l===void 0)return g;let x=n[o[N]];if(x===0||l[o[N]]>=Hi)return g;let E=Gs([x,0]);return[V(E[0],-r/2),V(E[1],-r/2)]}),m=a.map(g=>{let N=o.map((E,R)=>At(f[R],e[E][g])),x=Il(N.map(E=>E[0]));return{c:x===0?N:N.map(E=>[V(E[0],-x),V(E[1],-x)]),e:x}}),{qty:u,coef:h}=Ur(m.map(({c:g})=>g),o.map((g,N)=>At(f[N],t[g]))),p=h.map((g,N)=>m[N].e===0?g:[V(g[0],-m[N].e),V(g[1],-m[N].e)]),w=e[0]?.map(()=>NaN)??[];a.forEach((g,N)=>w[g]=p[N][0]);let d=e.map(g=>{let N=[0,0];return a.forEach((x,E)=>N=he(N,At(p[E],g[x]))),N}),b=Hr(u,c)[0],y=o.map(g=>l?.[g]??1),v=[0,0];if(i){let g=[0,0],N=[0,0];o.forEach((x,E)=>{g=he(g,[y[E],0]),N=he(N,At(d[x],y[E]))}),v=at(N,g)}let M=[0,0];return o.forEach((g,N)=>{let x=Oe(d[g],v);M=he(M,At(_e(x,x),y[N]))}),{coefficients:w,fittedValues:d.map(g=>g[0]),residuals:d.map((g,N)=>Oe([t[N],0],g)[0]),effects:u.map(g=>g[0]),rss:b,mss:M[0]}}function Hm(e,t,n,r,o=!1){let s=qe([t.filter((l,f)=>n===void 0||n[f]!==0)]).k;if(s===0)return Bm(Km(e,t,n,r,o),e,t,n);let i=Km(e,t.map(l=>V(l,-s)),n,r,o),c=i.result,a=l=>V(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),Bm({...i,effects:i.effects.map(a),rss:V(i.rss,2*s),yScale:s,rssScaled:i.rss},e,t,n)}function Bm(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]=Ta(f,0)?go(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 Gi=new WeakMap;function Xi(e){return Gi.get(e)??0}function ew(e){return e.qr.every(t=>t.every(Number.isFinite))&&e.qraux.every(Number.isFinite)}function Km(e,t,n,r,o){if(t.length===0)throw new RangeError("0 (non-NA) cases");let s=t.map((D,Y)=>n===void 0||n[Y]!==0),i=e.filter((D,Y)=>s[Y]),c=t.filter((D,Y)=>s[Y]),a=n?.filter((D,Y)=>s[Y]),l=c.length;if(l===0)throw new RangeError("all weights are zero");if(i.some(D=>D.some(Y=>!Number.isFinite(Y))))throw new RangeError("NA/NaN/Inf in 'x'");if(c.some(D=>!Number.isFinite(D)))throw new RangeError("NA/NaN/Inf in 'y'");if(a?.some(D=>!Number.isFinite(D)))throw new RangeError("non-finite weights not allowed");let f=t.flatMap((D,Y)=>s[Y]?[Y]:[]),m=a===void 0?1:Nt(a),u=Number.isFinite(m)?2*Math.floor(Math.floor(Math.log2(m))/2):0,h=0,p=a?.map(Math.sqrt),w=p===void 0?i:i.map((D,Y)=>D.map(j=>j*p[Y])),d=Lt(w);a!==void 0&&Number.isFinite(m)&&u+2>0&&!ew(d)&&(h=u+2,p=a.map(D=>V(Math.sqrt(D),-h/2)),w=i.map((D,Y)=>D.map(j=>j*p[Y])),d=Lt(w),Gi.set(d,h));let b=p===void 0?c:c.map((D,Y)=>D*p[Y]),y=d.rank,v=D=>V(D,-u),M=o?Jg(e,t,n,u,f,d,r):void 0,g=M?.coefficients??vs(d,b),N=M?.residuals??[],x=M?.fittedValues??[],E=l-y,R=a?.map(v)??new Array(l).fill(1),T=M?.rss??0;if(M===void 0){let D=zr(d,b),Y=p===void 0?D:D.map((U,W)=>U/p[W]),j=0;t.forEach((U,W)=>{if(s[W])N.push(Y[j]),x.push(U-Y[j]),j++;else{let te=e[W].reduce((fe,Re,Le)=>Number.isNaN(g[Le])?fe:fe+Re*g[Le],0);x.push(te),N.push(U-te)}});for(let U=0;U<l;U++)T+=R[U]*Y[U]**2}let A=x.filter((D,Y)=>s[Y]),S=0;if(a!==void 0&&a.some(D=>D!==0&&V(D,-u)<Hi)){let D=f.flatMap((Y,j)=>{let U=N[Y];return U===0?[]:[{w:a[j],r:U,a:Math.floor(Math.log2(Math.abs(U)))}]});if(D.length>0&&D.every(Y=>Number.isFinite(Y.r))){let Y=Math.max(...D.map(W=>Math.log2(W.w)+2*W.a)),j=2*Math.floor(Math.floor(Y)/2),U=0;for(let W of D)U+=V(W.w,2*W.a-j)*V(W.r,-W.a)**2;T=U,S=j-u}}let C=T/E,L=V(Math.sqrt(C),(u+S)/2),k=h===0?0:(h-u)/2,F=d.qr.slice(0,y).map(D=>D.slice(0,y).map(Y=>h===0?V(Y,-u/2):Y)),{inv:I,e:O}=y>0?yo(F):{inv:[],e:[]},$=d.pivot.slice(0,y).map((D,Y)=>[D,Y]).sort((D,Y)=>D[0]-Y[0]),_=$.map(([D])=>g[D]),P=$.map(([,D])=>V(Math.sqrt(I[D][D]*C),S/2-O[D]-k)),K=_.map((D,Y)=>D/P[Y]),X=K.map(D=>2*dt(-Math.abs(D),E,!0,!1)),re={coefficients:g,residuals:N,fittedValues:x,rank:y,dfResidual:E,coefTable:{estimate:_,stdError:P,tValue:K,pValue:X},sigma:L,rSquared:0,adjRSquared:0,intercept:r,qr:d,x:e,rows:e.map((D,Y)=>Y)};n!==void 0&&(re.weights=n);let z=r?1:0,J=y-z,Z={result:re,fitRows:f,effects:(M?.effects??[]).map(D=>V(D,u/2)),rss:V(T,u+S)};if(J===0)return Z;let Se=0;if(M!==void 0)Se=M.mss;else if(r){let D=R.reduce((j,U)=>j+U,0),Y=0;for(let j=0;j<l;j++)Y+=R[j]*A[j]/D;for(let j=0;j<l;j++)Se+=R[j]*(A[j]-Y)**2}else for(let D=0;D<l;D++)Se+=R[D]*A[D]**2;re.rSquared=Se/(Se+V(T,S)),re.adjRSquared=1-(1-re.rSquared)*((l-z)/E);let ne=V(Se/J/C,-S);return re.fStatistic={value:ne,numdf:J,dendf:E},re.fPValue=ht(ne,J,E,!1,!1),Z}function Ym(e,t){let n=(1-e)/2;return n<1e-8?gt(n,t,!1,!1):gt((1+e)/2,t,!0,!1)}function tw(e,t={}){B(t,Xg,"confint");let n=t.level??.95;if(!(n>0&&n<1))throw new RangeError("'level' must be between 0 and 1");let r=Ym(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 Um(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 V(V(e,-r)*V(t,-o),r+o+n)}function nw(e,t,n={}){B(n,Qg,"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=-(Gi.get(e.qr)??0)/2,l=e.coefficients.flatMap((v,M)=>Number.isNaN(v)?[]:[M]),f=o.map(v=>{let M=v.reduce((g,N,x)=>Number.isNaN(e.coefficients[x])?g:g+N*e.coefficients[x],0);return Ta(M,0)?go(l.map(g=>v[g]),l.map(g=>e.coefficients[g]),M):M}),m=v=>{let M=new Array(s).fill(0);for(let g=0;g<s;g++){let N=v[g];for(let x=0;x<g;x++)N-=c[x][g]*M[x];M[g]=N/c[g][g]}return M},u=o.map(v=>{let M=i.map(x=>v[x]),g=Aa(m(M));if(g>=Hi&&g<1/0||!M.every(Number.isFinite)||M.every(x=>x===0))return a===0||!(g>0&&g<1/0)?g*e.sigma:Um(g,e.sigma,a);let N=Math.floor(Math.log2(Nt(M.map(Math.abs))));for(let x of[0,600]){let E=Aa(m(M.map(R=>V(R,-(N+x)))));if(E<1/0)return E===0?E*e.sigma:Um(E,e.sigma,N+x+a)}return g*e.sigma}),h=n.interval??"none";if(h!=="none"&&h!=="confidence"&&h!=="prediction")throw new RangeError(`invalid 'interval': ${String(h)}`);let p=Cl(e.qr,e.rank,o),w=p.length>0?{nonEstimable:p,warnings:[Ll]}:{};if(h==="none")return Q("predict",{fit:f,seFit:u,...w},e);let d=n.level??.95;if(!(d>0&&d<1))throw new RangeError("'level' must be between 0 and 1");let b=Ym(d,e.dfResidual),y=u.map(v=>b*(h==="prediction"?Math.hypot(v,e.sigma):v));return Q("predict",{fit:f,seFit:u,lwr:f.map((v,M)=>v-y[M]),upr:f.map((v,M)=>v+y[M]),...w},e)}var rw=["weights","contrasts","levels"],Gm=["confLevel"],Xm=["sigmaX","sigmaY","mu","alternative","confLevel"];function ow(e,t,n={}){return B(n,rw,"aov"),typeof e=="string"?Q("aov",Qm(e,t,n)):Q("aov",sw(e,t))}function Qm(e,t,n){let r=Jn(e,!0),{treatment:o,errors:s}=Ml(r.rhs);if(s.length>1)throw new Error(`there are ${s.length} Error terms: only 1 is allowed`);return s.length===1?Wm(e,r,o,s[0],t,n):Yi(e,$n(r,t,{...n,dropUnusedLevels:!0},o),!0)}function sw(e,t){let{labels:n,groups:r}=or(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 Qm("y ~ g",{y:o,g:s},{levels:{g:n}})}function Zm(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}=Dt(e),i=t-1,c=n-t,a=r/i,l=o/c,f=a/l,m=u=>V(u,2*s);return{df:[i,c],sumSq:[m(r),m(o)],meanSq:[m(a),m(l)],fValue:f,pValue:c>0?ht(f,i,c,!1,!1):NaN}}function aw(e,t,n={}){wt(t)&&B(t,Gm,"TukeyHSD"),B(n,Gm,"TukeyHSD");let r;t!==void 0&&!Array.isArray(t)?n=t:r=t;let{labels:o,groups:s}=or(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;cn(c);let l=s.reduce((b,y)=>b+y.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}=Dt(s),h=f/l,p=pl(c,i,l),w=[],d={names:[],i:[],j:[],diff:[],lwr:[],upr:[],pAdj:w,pAdjusted:w,warnings:[]};for(let b=0;b<i-1;b++)for(let y=b+1;y<i;y++){let v=m[y]-m[b],M=Math.sqrt(h/2*(1/s[y].length+1/s[b].length));d.names.push(`${o[y]}-${o[b]}`),d.i.push(y+1),d.j.push(b+1),d.diff.push(V(v,u)),d.lwr.push(V(v-p*M,u)),d.upr.push(V(v+p*M,u)),d.pAdj.push(ms(Math.abs(v)/M,i,l,{lowerTail:!1}))}return d}function iw(e,t,n={}){wt(t)&&B(t,Xm,"zTest"),B(n,Xm,"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 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(i))throw new RangeError("mu must be a number");let c=Ze(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=qe([l,f??[],[o,s??0]]),u=m.k,[h,p,[w,d]]=m.groups,b=Xn(h)[0],y=f===void 0?NaN:Xn(p)[0],v=f===void 0?w/Math.sqrt(l.length):Math.sqrt(w**2/l.length+d**2/f.length),M=f===void 0?b:b-y,g=(M-V(i,-u))/v,N,x;if(c==="less")N=ae(g),x=[-1/0,M+le(a)*v];else if(c==="greater")N=ae(g,{lowerTail:!1}),x=[M-le(a)*v,1/0];else{N=2*ae(-Math.abs(g));let T=(1-a)/2,A=(T<1e-8?le(T,{lowerTail:!1}):le(1-(1-a)/2))*v;x=[M-A,M+A]}x=[V(x[0],u),V(x[1],u)];let E=f!==void 0,R=(T,A)=>{if(!(u>0&&T.some((C,L)=>C!==0&&Math.abs(A[L])<2**-1022)))return V(Xn(A)[0],u);let S=Math.max(qe([T]).k,0);return V(Xn(S===0?T:T.map(C=>V(C,-S)))[0],S)};return{method:E?"Two-sample z-Test":"One-sample z-Test",alternative:c,statistic:g,statisticName:"z",pValue:N,confInt:x,confLevel:a,estimate:E?[R(l,h),R(f,p)]:R(l,h),estimateNames:E?["mean of x","mean of y"]:"mean of x",nullValue:i,nullValueName:E?"difference in means":"mean",warnings:[]}}var cw=["parm","level","test"],Jm={epsilon:1e-300,maxit:100,untilCoefficients:!0};function Qi(e,t,n,r,o=1e-11){let s=[];for(let w=0;w<t.length;w++)n[w]>0&&s.push(w);let i=0,c=0;for(let w of s)i+=n[w],c+=n[w]*t[w];if(r&&!(Number.isFinite(i)&&Number.isFinite(c))){let w=0;for(let d of s)w=Math.max(w,n[d]);if(Number.isFinite(w)){let d=w>1?Math.floor(Math.log2(w)):0;i=0,c=0;for(let b of s){let y=V(n[b],-d);i+=y,c+=y*t[b]}}}let a=r?c/i:0,l=0;for(let w of s)l+=n[w]*(t[w]-a)**2;if((e.length>0?e[0].length:0)===0||s.length===0)return 0;let m=s.map(w=>Math.sqrt(n[w])),u=Lt(s.map((w,d)=>e[w].map(b=>b*m[d])),{tol:o}),h=zr(u,s.map((w,d)=>t[w]*m[d])),p=0;for(let w of h)p+=w*w;if(!Number.isFinite(l)){let w=s.map((b,y)=>m[y]*(t[b]-a)),d=0;for(let b of w)d=Math.max(d,Math.abs(b));for(let b of h)d=Math.max(d,Math.abs(b));if(Number.isFinite(d)){let b=d>1?Math.floor(Math.log2(d)):0,y=0;for(let M of w)y+=V(M,-b)**2;let v=0;for(let M of h)v+=V(M,-b)**2;return V(V(y-v,b),b)}}return l-p}function lw(e,t={}){B(t,cw,"glmConfint");let{fit:n,k:r}=zn(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},(L,k)=>k);for(let L of c)if(!Number.isInteger(L)||L<0||L>=i)throw new RangeError(`invalid coefficient index ${L}`);let a=n.model,l=n.coefficients.map((L,k)=>k).filter(L=>!Number.isNaN(n.coefficients[L])),f=new Et,m=Mo(a,l,f,{...Jm,start:a.start===void 0?void 0:l.map(L=>a.start[L])}),u=new Array(i).fill(NaN);if(l.forEach((L,k)=>u[L]=m.fit.coefficients[k]),!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},w=rt(()=>Da(p)),d=w.dispersion,b=new Array(i).fill(NaN);(m.fit.qr===void 0?[]:m.fit.qr.pivot.slice(0,m.fit.rank)).forEach((L,k)=>b[l[L]]=w.stdError[k]);let v=a.x.map(L=>l.map(k=>L[k])),M=m.fit.eta,g=(L,k)=>{let F=l.filter(q=>q!==L),I=Mo(a,F,new Et,{...Jm,start:void 0,etastart:M,extraOffset:a.x.map(q=>q[L]*k)}).fit;if(!I.converged)return h=!0,NaN;let O=s==="LRT"?I.deviance-m.fit.deviance:Qi(v,I.workingResiduals,I.workingWeights,!1);return Math.sign(k-u[L])*Math.sqrt(Math.max(O/d,0))},N=(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,R=x<E,T=R?[-x,x]:[le(N),N<1e-8?le(N,{lowerTail:!1}):le((1+o)/2)],A=c.map(()=>NaN),S=c.map(()=>NaN),C=(L,k,F)=>{let I=F===0?-1:1;h=!1;let O=NaN,q=NaN;for(let z=0;z<=30;z++){let J=u[L]+I*2**z*b[L],Z=g(L,J);if(Math.abs(Z)>=Math.abs(k)){O=J,q=Z;break}}if(Number.isNaN(O)||h)return NaN;let $=z=>z===u[L]?-k:g(L,z)-k,[_,P]=F===0?[O,u[L]]:[u[L],O],[K,X]=F===0?[q-k,-k]:[-k,q-k],re;try{re=Bt($,_,P,{tol:1e-14*b[L],maxIter:500,fLower:K,fUpper:X})}catch(z){if(!h)throw z;re=NaN}return h?NaN:re};return c.forEach((L,k)=>{if(Number.isNaN(u[L]))return;if(!R){A[k]=C(L,T[0],0),S[k]=C(L,T[1],1);return}let F=[-2*E,-E,0,E,2*E],I=[C(L,-2*E,0),C(L,-E,0),u[L],C(L,E,1),C(L,2*E,1)].map(q=>q-u[L]),O=q=>{let $=0;for(let _=0;_<5;_++){let P=1;for(let K=0;K<5;K++)K!==_&&(P*=(q-F[K])/(F[_]-F[K]));$+=P*I[_]}return u[L]+$};A[k]=O(T[0]),S[k]=O(T[1])}),{lower:A.map(L=>V(L,r)),upper:S.map(L=>V(L,r))}}var fw=["type","omega","sandwich"],mw=["cluster","type","cadjust","multi0","fix","sandwich"],Qs=2**-52;function ec(e,t){if(t===0)return e;let n=nc.get(e)??{mant:e,exp:e.map(()=>0)};return Ar(n.mant,n.exp.map(r=>r+t))}var Zs=e=>Math.floor(Math.log2(Math.abs(e)));function fn(e){if(e===0||!Number.isFinite(e))return[e,0];let t=Zs(e);return[V(e,-t),t]}var $o=(e,t)=>[e[0]*t[0],e[1]+t[1]];function sr(e,t=!1){let n=-1/0;for(let[o,s]of e)o!==0&&Number.isFinite(o)&&(n=Math.max(n,s+Zs(o)));let r=n===-1/0?0:n;return t&&(r=2*Math.floor(r/2)),{v:e.map(([o,s])=>Ve(o,s-r)),s:r}}var ar=(e,t)=>Math.abs(t)<=2046?V(e,t):Ve(e,t),nc=new WeakMap;function Ar(e,t){let n=e.map((r,o)=>r.map((s,i)=>ar(s,t[o]+t[i])));return nc.set(n,{mant:e,exp:t}),n}function rc(e,t,n){let r=nc.get(e),o=r?.mant[t]?.[t];if(!(r===void 0||o===void 0||!Object.is(ar(o,2*r.exp[t]),n)))return ar(Math.sqrt(o),r.exp[t])}function ir(e){return typeof e=="object"&&e!==null&&"family"in e&&"model"in e}function Js(e){if(!(typeof e=="object"&&e!==null&&Array.isArray(e.coefficients)&&"qr"in e&&(ir(e)||"sigma"in e&&"x"in e&&"rows"in e)))throw new TypeError("'fit' must be the result of lm or glm")}function ru(e,t){return e.pivot.slice(0,t).map((n,r)=>[n,r]).sort((n,r)=>n[0]-r[0])}function ou(e,t){let{inv:n,e:r}=yo(e.qr.slice(0,t).map(s=>s.slice(0,t))),o=ru(e,t);return{inv:o.map(([,s])=>o.map(([,i])=>n[s][i])),e:o.map(([,s])=>r[s]),cols:o.map(([s])=>s)}}function eu(e,t,n){let{inv:r,e:o,cols:s}=ou(e,t);return{A:r.map((i,c)=>i.map((a,l)=>V(a,-o[c]-o[l]-n))),cols:s}}function tu(e,t){let n=e.qr.slice(0,t).map(i=>i.slice(0,t)),r=n.map((i,c)=>{let a=0;for(let l=0;l<=c;l++)a=Math.max(a,Math.abs(n[l][c]));return a>0&&Number.isFinite(a)?Zs(a):0}),{inv:o}=yo(n.map(i=>i.map((c,a)=>V(c,-r[a])))),s=ru(e,t);return{B:s.map(([,i])=>s.map(([,c])=>o[i][c])),e:s.map(([,i])=>r[i])}}function su(e,t){let n=e.qr.length;return _l(e,Array.from({length:n},(r,o)=>Array.from({length:t},(s,i)=>o===i?1:0)))}function uw(e,t){let n=e.qr.length;return t===0?new Array(n).fill(0):su(e,t).map(o=>{let s=o.reduce((i,c)=>i+c*c,0);return s>1-10*Qs?1:s})}function nu(e,t,n,r){return n.map((o,s)=>{if(o>=1)return 0;let i=r(s);if(o<=.5||e===void 0||i<0)return 1-o;let c=e.qr.length,a=new Array(c).fill(0);a[i]=1;let l=Ia(e,a),f=0;for(let m=t;m<c;m++)f+=l[m]*l[m];return f})}function ea(e){if(ir(e))return e.model.columnNames;let t=e.coefNames;if(t!==void 0)return t;let n=e.coefficients.length;return e.intercept?["(Intercept)",...Array.from({length:n-1},(r,o)=>`x${o+1}`)]:Array.from({length:n},(r,o)=>`x${o+1}`)}function oc(e){let t=ea(e);if(e.qr===void 0||e.rank===0)return{vcov:[],names:[]};let{inv:n,e:r,cols:o}=ou(e.qr,e.rank),s=ir(e)?0:Xi(e.qr),i,c;if(ir(e)){let l=zn(e),f=Oa(e).fixedDispersion?1:Eo(l.fit),m=f===0||!Number.isFinite(f)?0:2*Math.floor(Zs(f)/2);i=V(f,-m),c=m+2*l.k}else{let[l,f]=fn(e.sigma);i=l*l,c=2*f}let a=n.map(l=>l.map(f=>f*i));return{vcov:Ar(a,r.map(l=>(c-s)/2-l)),names:o.map(l=>t[l])}}function au(e,t=!1){let n=ea(e);if(ir(e)){let w=t?zn(e):{fit:e,k:0},d=w.fit,b=Oa(d),y=d.priorWeights,v=y.map(I=>I>0),M=y.flatMap((I,O)=>I>0?[O]:[]),{A:g,cols:N}=d.qr===void 0||d.rank===0?{A:[],cols:[]}:eu(d.qr,d.rank,0),x=M.map(I=>N.map(O=>d.model.x[I][O])),E=M.map(I=>d.weights[I]),R=M.map(I=>d.weights[I]*d.residuals[I]),T=b.family==="binomial"||b.family==="poisson",A=T?1:R.reduce((I,O)=>I+O*O,0)/E.reduce((I,O)=>I+O,0),S=d.model.rows,C=Pa(d),L=Pl(d),k=()=>{let I=zn(d),O=M.map(z=>I.fit.residuals[z]),q=E.map(Math.sqrt),{B:$,e:_}=d.qr===void 0||d.rank===0?{B:[],e:[]}:tu(d.qr,d.rank),P=x.map((z,J)=>z.map((Z,Se)=>V(q[J]*Z,-_[Se]))),{v:K,s:X}=sr(q.map((z,J)=>z===0?[0,0]:$o(fn(z),fn(O[J])))),re=()=>{let z=[1,0];if(!T){let J=sr(E.map((Se,ne)=>Se===0?[0,0]:$o(fn(Se),fn(O[ne])))),Z=sr(E.map(fn));z=[J.v.reduce((Se,ne)=>Se+ne*ne,0)/Z.v.reduce((Se,ne)=>Se+ne,0),2*J.s-Z.s+2*I.k]}return{T:x.map(J=>J.map((Z,Se)=>ar(Z,-_[Se]))),factor:[z[0]*z[0],2*z[1]]}};return{T:P,B:$,e:_,u:K,s:X+I.k,omegaPart:re}},F;if(Sr(A)&&R.every(Number.isFinite))F=R.map(I=>I/A);else{let I=zn(d),O=sr(E.map((q,$)=>q===0?[0,0]:$o(fn(q),fn(I.fit.residuals[M[$]]))));if(T)F=O.v.map(q=>ar(q,O.s+I.k));else{let q=sr(E.map(fn)),$=O.v.reduce((_,P)=>_+P*P,0)/q.v.reduce((_,P)=>_+P,0);F=O.v.map(_=>ar(_/$,q.s-O.s-I.k))}}return{glm:!0,names:N.map(I=>n[I]),X:x,W:E,res:F,phiS:A,A:g,h:C,omh:nu(d.qr,d.rank,C,I=>L[I]),qr:d.qr,rank:d.rank,qrRow:I=>L[I],inputRows:M.map(I=>S[I]),nInput:La(e,S),nonZero:v,dispersion:()=>Eo(d),scaled:k,k:w.k}}let r=e,o=r.weights,s=r.residuals.map((w,d)=>o===void 0||o[d]!==0),i=s.flatMap((w,d)=>w?[d]:[]),c=Xi(r.qr),{A:a,cols:l}=r.rank===0?{A:[],cols:[]}:eu(r.qr,r.rank,c),f=i.map(w=>l.map(d=>r.x[w][d])),m=i.map(w=>o?.[w]??1),u=i.map((w,d)=>m[d]*r.residuals[w]),h=uw(r.qr,r.rank),p=()=>{let w=m.map(N=>c===0?Math.sqrt(N):V(Math.sqrt(N),-c/2)),{B:d,e:b}=r.rank===0?{B:[],e:[]}:tu(r.qr,r.rank),y=f.map((N,x)=>N.map((E,R)=>V(w[x]*E,-b[R]))),{v,s:M}=sr(w.map((N,x)=>$o(fn(N),fn(r.residuals[i[x]]))));return{T:y,B:d,e:b,u:v,s:M,omegaPart:()=>({T:f.map(N=>N.map((x,E)=>ar(x,-b[E]))),factor:[1,-2*c]})}};return{glm:!1,names:l.map(w=>n[w]),X:f,W:m,res:u,phiS:1,A:a,h,omh:nu(r.qr,r.rank,h,w=>w),qr:r.qr,rank:r.rank,qrRow:w=>w,inputRows:i.map(w=>r.rows[w]),nInput:La(r,r.rows),nonZero:s,dispersion:()=>r.sigma*r.sigma,scaled:p,k:0}}var sc=2**-969,Sr=e=>Math.abs(e)>=sc&&Math.abs(e)<1/0;function iu(e,t,n,r){if(!(e.phiS>=sc&&e.phiS<1/0)||!Sr(e.X.length*e.phiS*e.phiS))return!1;for(let o=0;o<n.length;o++)if(!Sr(e.A[o][o])||!r&&(!Sr(t[o][o])||!Sr(n[o][o]))||!n[o].every(Number.isFinite))return!1;return!0}function zo(e,t,n){let r=e.length,s=e.map(i=>Array.from({length:r},(c,a)=>i.reduce((l,f,m)=>l+f*t[m][a],0))).map(i=>Array.from({length:r},(c,a)=>n*i.reduce((l,f,m)=>l+f*e[m][a],0)));for(let i=0;i<r;i++)for(let c=0;c<i;c++)s[i][c]=s[c][i];return s}function Yr(e,t,n){let r=e[0]?.length??0,o=Array.from({length:r},()=>new Array(r).fill(0));e.forEach((s,i)=>{let c=t[i];for(let a=0;a<r;a++)for(let l=a;l<r;l++)o[a][l]+=c*s[a]*s[l]});for(let s=0;s<r;s++)for(let i=s;i<r;i++)o[s][i]=o[i][s]=o[s][i]/n;return o}function Zi(e){let t=e.slice(0,10).map(n=>String(n+1));return e.length>10?`${t.join(", ")}, ...`:t.join(", ")}var hw=["HC3","const","HC","HC0","HC1","HC2","HC4","HC4m","HC5"];function pw(e,t={}){B(t,fw,"vcovHC"),Js(e);let n=Ae(t.type,hw),r=n==="HC"?"HC0":n,o=t.sandwich??!0;if(typeof o!="boolean")throw new TypeError("'sandwich' must be true or false");let s=au(e,t.omega===void 0),i=s.X.length,c=s.names.length,a=[],{res:l,h:f,omh:m}=s,u,h;if(t.omega!==void 0){let w;if(typeof t.omega=="function"?w=t.omega(l.slice(),f.slice(),i-c):(w=t.omega,w.length!==i&&w.length===s.nonZero.length&&(w=w.filter((d,b)=>s.nonZero[b]))),!Array.isArray(w)&&!ArrayBuffer.isView(w))throw new TypeError("'omega' must give an array of numbers");if(w.length!==i)throw new RangeError(`'omega' must have one value per observation (${i})`);if(u=Array.from(w,Number),u.some(d=>!Number.isFinite(d)))throw new RangeError("'omega' must be finite")}else if(r==="const"){let w=s.dispersion();u=s.W.map(d=>w*d/(s.phiS*s.phiS))}else{let w=f.flatMap((y,v)=>y>1-Math.sqrt(Qs)?[s.inputRows[v]]:[]);if(w.length>0&&a.push(r==="HC0"||r==="HC1"?`${r} covariances become (close to) singular if hat values are (close to) 1 as for observation(s) ${Zi(w)}`:`${r} covariances are numerically unstable for hat values close to 1 (and undefined if exactly 1) as for observation(s) ${Zi(w)}`),r!=="HC0"&&r!=="HC1"){let y=f.flatMap((v,M)=>v>=1?[s.inputRows[M]]:[]);if(y.length>0)throw new RangeError(`${r} covariances are undefined for hat values of 1 as for observation(s) ${Zi(y)}`)}if(r==="HC1"&&i<=c)throw new RangeError("HC1 covariances need more observations than coefficients");let d=Math.round(f.reduce((y,v)=>y+v,0)),b=f.reduce((y,v)=>Math.max(y,v),0);h=y=>{switch(r){case"HC0":return y;case"HC1":return y.map(v=>v*i/(i-c));case"HC2":return y.map((v,M)=>v/m[M]);case"HC3":return y.map((v,M)=>v/m[M]**2);case"HC4":return y.map((v,M)=>v/m[M]**Math.min(4,i*f[M]/d));case"HC4m":return y.map((v,M)=>v/m[M]**(Math.min(1,i*f[M]/d)+Math.min(1.5,i*f[M]/d)));default:return y.map((v,M)=>v/Math.sqrt(m[M]**Math.min(i*f[M]/d,Math.max(4,i*.7*b/d))))}},u=h(l.map(y=>y*y))}let p;if(o&&r==="const"&&t.omega===void 0)p=oc(e).vcov;else if(o){let w=Yr(s.X,u,i);if(p=zo(s.A,w,i*s.phiS*s.phiS),!iu(s,w,p,(h===void 0?u:l).every(d=>d===0))){let d=s.scaled();if(h!==void 0){let b=zo(d.B,Yr(d.T,h(d.u.map(y=>y*y)),i),i);p=Ar(b,d.e.map(y=>d.s-y))}else{let{T:b,factor:y}=d.omegaPart(),v=sr(u.map(M=>$o(fn(M),y)),!0);p=Ar(zo(d.B,Yr(b,v.v,i),i),d.e.map(M=>v.s/2-M))}}p=ec(p,s.k)}else{let w=Yr(s.X,u,i);if(fu(w,(h===void 0?u:l).every(d=>d===0)))p=w;else{let d=s.scaled();if(h!==void 0){let{mant:b,exp:y}=mu(d,Yr(d.T,h(d.u.map(v=>v*v)),i));p=Ar(b,y)}else{let b=sr(u.map(fn),!0);p=Ar(Yr(d.omegaPart().T,b.v,i),d.e.map(y=>b.s/2+y))}}p=ec(p,-s.k)}return Q("vcovHC",{vcov:p,names:s.names,warnings:a},e)}var dw=["HC","HC0","HC1","HC2","HC3"];function gw(e){if(e==null||typeof e=="number"&&Number.isNaN(e))throw new RangeError("cannot handle NAs in 'cluster': either refit the model without the NA observations in 'cluster' or impute the NAs");if(typeof e=="boolean")return e?"TRUE":"FALSE";if(typeof e=="number")return ft(e);if(typeof e=="string")return e;throw new TypeError("cluster labels must be numbers, strings or booleans")}function ww(e){let t=new Map;return{g:e.map(r=>{let o=t.get(r);return o===void 0&&(o=t.size,t.set(r,o)),o}),G:t.size}}function bw(e){let t=[],n=(r,o,s)=>{if(s.length===r){t.push(s.slice());return}for(let i=o;i<e;i++)s.push(i),n(r,i+1,s),s.pop()};for(let r=1;r<=e;r++)n(r,0,[]);return t}var cu=Qs**(1/1.3);function lu(e,t){let{values:n,vectors:r}=Wt(e,{symmetric:!0});if(n.some(i=>!(i>=cu)))return;let o=r,s=n.map(i=>i**t);return e.map((i,c)=>e.map((a,l)=>o[c].reduce((f,m,u)=>f+m*s[u]*o[l][u],0)))}function vw(e){return{A:e.A,xs:t=>e.X[t].map(n=>n*Math.sqrt(e.W[t])),rs:t=>{let n=Math.sqrt(e.W[t]);return n===0?0:e.res[t]/n},single:(t,n,r)=>e.X[t].map(o=>n*e.res[t]*r*o),psi:t=>e.X[t].map(n=>n*e.res[t])}}function Nw(e){return{A:e.B,xs:t=>e.T[t],rs:t=>e.u[t],single:(t,n,r)=>e.T[t].map(o=>n*e.u[t]*r*o),psi:t=>e.T[t].map(n=>n*e.u[t])}}function yw(e,t,n,r,o){let s=e.names.length,i=Array.from({length:r},()=>[]);n.forEach((f,m)=>i[f].push(m));let c=Math.sqrt((r-1)/r),a=r===n.length,l;return i.map((f,m)=>{if(f.length===1){let y=f[0];if(a?e.h[y]>=1:!(e.omh[y]>=cu))throw Ji(m);return t.single(y,c,e.omh[y]**o)}let u=f.map(t.xs),h=f.map(t.rs);if(e.qr!==void 0&&e.rank>0){l??=su(e.qr,e.rank);let y=Mw(e,l,f,h,o);if(y===null)throw Ji(m);if(y!==void 0)return Array.from({length:s},(v,M)=>c*u.reduce((g,N,x)=>g+N[M]*y[x],0))}let p=u.map(y=>Array.from({length:s},(v,M)=>y.reduce((g,N,x)=>g+N*t.A[x][M],0))),w=u.map((y,v)=>u.map((M,g)=>(v===g?1:0)-p[v].reduce((N,x,E)=>N+x*M[E],0)));for(let y=0;y<w.length;y++)for(let v=0;v<y;v++)w[y][v]=w[v][y]=(w[y][v]+w[v][y])/2;let d=lu(w,o);if(d===void 0)throw Ji(m);let b=d.map(y=>y.reduce((v,M,g)=>v+M*h[g],0));return Array.from({length:s},(y,v)=>c*u.reduce((M,g,N)=>M+g[v]*b[N],0))})}function Mw(e,t,n,r,o){let s=e.qr,i=e.rank,c=new Array(i).fill(0),a=n.map(g=>{let N=e.qrRow(g);return N<0?c:t[N]}),l=Array.from({length:i},(g,N)=>Array.from({length:i},(x,E)=>a.reduce((R,T)=>R+T[N]*T[E],0))),{values:f,vectors:m}=Wt(l,{symmetric:!0});if(!f.some(g=>g>.5))return;let u=m,h=r.slice(),p=[];f.forEach((g,N)=>{let x=a.map(E=>E.reduce((R,T,A)=>R+T*u[A][N],0));if(g>.5)p.push(x.map(E=>E/Math.sqrt(g)));else if(g>0){let R=Math.expm1(o*Math.log1p(-g))/g*x.reduce((T,A,S)=>T+A*r[S],0);x.forEach((T,A)=>h[A]+=R*T)}});let w=s.qr.length,d=p.map(g=>{let N=new Array(w).fill(0);return n.forEach((x,E)=>{let R=e.qrRow(x);R>=0&&(N[R]=g[E])}),Ia(s,N).slice(i)}),b=d.map(g=>d.map(N=>g.reduce((x,E,R)=>x+E*N[R],0))),y=lu(b,o);if(y===void 0)return null;let v=p.map(g=>g.reduce((N,x,E)=>N+x*r[E],0)),M=y.map((g,N)=>g.reduce((x,E,R)=>x+E*v[R],0)-v[N]);return p.forEach((g,N)=>g.forEach((x,E)=>h[E]+=M[N]*x)),h}function Ji(e){return new RangeError(`clustered HC2/HC3 covariances are undefined: the block I - H_gg of cluster ${e+1} (in order of appearance) is singular, as with cluster fixed effects`)}function Ew(e,t){let n=Array.from({length:t},()=>new Array(t).fill(0));for(let r of e)for(let o=0;o<t;o++)for(let s=0;s<t;s++)n[o][s]+=r[o]*r[s];return n}function tc(e){if(e.length===0)return e;let{values:t,vectors:n}=Wt(e,{symmetric:!0});if(t.every(s=>s>=0))return e;let r=n,o=t.map(s=>Math.max(s,0));return e.map((s,i)=>e.map((c,a)=>r[i].reduce((l,f,m)=>l+f*o[m]*r[a][m],0)))}function xw(e,t){let n=e.length;if(n===0)return{mant:e,exp:t};let r=e.map((a,l)=>a[l]!==0?Math.sqrt(Math.abs(a[l])):1),o=e.map((a,l)=>a.map((f,m)=>f/r[l]/r[m])),{values:s}=Wt(o,{symmetric:!0}),i=s.reduce((a,l)=>Math.max(a,Math.abs(l)),0);if(s.every(a=>a>=-16*n*Qs*i))return{mant:e,exp:t};let c=Math.max(...t);return{mant:tc(e.map((a,l)=>a.map((f,m)=>ar(f,t[l]+t[m]-2*c)))),exp:t.map(()=>c)}}function fu(e,t){return t||e.every((n,r)=>Sr(n[r])&&n.every(Number.isFinite))}function mu(e,t){let[n,r]=e.omegaPart().factor;return{mant:t.map(o=>o.map(s=>s/n)),exp:e.e.map(o=>e.s+o-r/2)}}function Rw(e,t={}){B(t,mw,"vcovCL"),Js(e);let n=au(e,!0),r=n.X.length,o=n.names.length,s=t.type===void 0?n.glm?"HC0":"HC1":Ae(t.type,dw),i=s==="HC"?"HC0":s,c=t.cadjust??!0,a=t.multi0??!1,l=t.fix??!1,f=t.sandwich??!0;for(let[v,M]of[["cadjust",c],["multi0",a],["fix",l],["sandwich",f]])if(typeof M!="boolean")throw new TypeError(`'${v}' must be true or false`);let m,u=t.cluster;if(u===void 0)m=[n.inputRows.map(String)];else{let v=N=>Array.isArray(N)||ArrayBuffer.isView(N);if(!v(u))throw new TypeError("'cluster' must be an array of labels or an array of such arrays");let g=u.length>0&&Array.from(u).every(v)?Array.from(u):[u];if(g.length===0)throw new RangeError("'cluster' has no dimension");m=g.map(N=>{if(N.length!==n.nInput)throw new RangeError("number of observations in 'cluster' and 'estfun()' do not match");return n.inputRows.map(x=>gw(N[x]))})}if(i==="HC1"&&r<=o)throw new RangeError("HC1 covariances need more observations than coefficients");let h=m.length,p=bw(h).map((v,M)=>{let g=a&&h>1&&M===2**h-2,N=g?n.inputRows.map(String):n.inputRows.map((T,A)=>v.length===1?m[v[0]][A]:JSON.stringify(v.map(S=>m[S][A]))),{g:x,G:E}=ww(N);if(E<2)throw new RangeError("at least two clusters are needed");let R=g?i==="HC1"?(r-o)/(r-1):1:c?E/(E-1):1;return{g:x,G:E,c:R,sign:v.length%2===1?1:-1}}),w=v=>{let M=Array.from({length:o},()=>new Array(o).fill(0));for(let{g,G:N,c:x,sign:E}of p){let R;i==="HC2"||i==="HC3"?R=yw(n,v,g,N,i==="HC2"?-.5:-1):N===r?R=n.X.map((A,S)=>v.psi(S)):(R=Array.from({length:N},()=>new Array(o).fill(0)),n.X.forEach((A,S)=>{let C=R[g[S]],L=v.psi(S);for(let k=0;k<o;k++)C[k]+=L[k]}));let T=Ew(R,o);M=M.map((A,S)=>A.map((C,L)=>C+E*x*T[S][L]/r))}return i==="HC1"&&(M=M.map(g=>g.map(N=>N*(r-1)/(r-o)))),M},d=n.res.every(v=>v===0),b=n.phiS>=sc&&n.phiS<1/0&&n.A.every((v,M)=>Sr(v[M]))&&n.res.every(Number.isFinite),y;if(b){let v=w(vw(n));if(!f)fu(v,d)&&(y=l?tc(v):v);else{let M=zo(n.A,v,r*n.phiS*n.phiS);iu(n,v,M,d)&&(y=l?tc(M):M)}}if(y===void 0){let v=n.scaled(),M=w(Nw(v)),{mant:g,exp:N}=f?{mant:zo(v.B,M,r),exp:v.e.map(E=>v.s-E)}:mu(v,M),x=l?xw(g,N):{mant:g,exp:N};y=Ar(x.mant,x.exp)}return y=ec(y,f?n.k:-n.k),Q("vcovCL",{vcov:y,names:n.names,warnings:[]},e)}var Tw=["vcov","df"],Aw=["parm","level","vcov","df"];function uu(e){return typeof e=="object"&&e!==null&&!Array.isArray(e)&&Array.isArray(e.vcov)&&Array.isArray(e.names)}function hu(e){if(!Array.isArray(e)||e.some(n=>!Array.isArray(n)&&!ArrayBuffer.isView(n)))throw new TypeError("'vcov' must be a matrix (an array of rows)");let t=e.map(n=>Array.from(n,Number));if(t.some(n=>n.length!==t.length))throw new RangeError("'vcov' must be a square matrix");return t}var pu=e=>new Set(e).size===e.length;function du(e,t){Js(e);let n=e.coefficients,r=ea(e),o=n.length,s=n.map(m=>Number.isNaN(m)),i,c,a=[],l;if(t===void 0){let m=oc(e),u=new Map(m.names.map((p,w)=>[p,w])),h=r.map((p,w)=>s[w]?void 0:u.get(p));i=h.map(p=>h.map(w=>p===void 0||w===void 0?NaN:m.vcov[p][w])),c=r,l=h.map(p=>p===void 0?NaN:rc(m.vcov,p,m.vcov[p][p])??Math.sqrt(m.vcov[p][p]))}else{let m=typeof t=="function"?rt(()=>t(e)):t,u=uu(m)?m.vcov:m;if(uu(m)){if(i=hu(m.vcov),c=m.names.slice(),c.length!==i.length)throw new RangeError("'vcov' must have one name per row");a=[...m.warnings??[]]}else i=hu(m);l=i.map((h,p)=>{let w=h[p],d=typeof u=="object"&&u!==null?rc(u,p,w):void 0;if(d!==void 0&&!Number.isNaN(d))return d;if(!Number.isFinite(w))throw new RangeError(`the variance of coefficient ${p+1} in 'vcov' is not a finite number`);if(w<0)throw new RangeError(`the variance of coefficient ${p+1} in 'vcov' is negative`);return Math.sqrt(w)})}if(c!==void 0&&pu(c)&&pu(r)){let m=new Map(c.map((h,p)=>[h,p])),u=r.flatMap((h,p)=>m.has(h)?[p]:[]);return{est:u.map(h=>n[h]),se:u.map(h=>l[m.get(r[h])]),names:u.map(h=>r[h]),warnings:a}}let f;if(i.length===o)f=n.map((m,u)=>u);else if(i.length===e.rank)f=n.flatMap((m,u)=>s[u]?[]:[u]);else{let m=e.rank===o?`${o} x ${o}`:`${o} x ${o} or ${e.rank} x ${e.rank}`;throw new RangeError(`'vcov' must be ${m}, one row per coefficient`)}return{est:f.map(m=>n[m]),se:l,names:f.map(m=>r[m]),warnings:a}}function gu(e,t){if(t===void 0)return ir(e)?1/0:e.dfResidual;if(typeof t!="number")throw new TypeError("'df' must be a number");if(Number.isNaN(t))throw new RangeError("'df' must not be NaN");return t}var wu=e=>Number.isFinite(e)&&e>0;function Sw(e){return ir(e)?e.priorWeights.filter(t=>t!==0).length:e.rank+e.dfResidual}function qw(e,t={}){B(t,Tw,"coeftest");let{est:n,se:r,names:o,warnings:s}=du(e,t.vcov),i=gu(e,t.df),c=wu(i),a=n.map((f,m)=>f/r[m]),l=a.map(f=>Number.isNaN(f)?NaN:c?2*dt(-Math.abs(f),i,!0,!1):2*ae(-Math.abs(f)));return Q("coeftest",{estimate:n,stdError:r,statistic:a,pValue:l,names:o,statisticName:c?"t value":"z value",df:i,method:c?"t test of coefficients":"z test of coefficients",nobs:Sw(e),warnings:s},e,typeof t.vcov=="function"?void 0:t.vcov)}function kw(e){let t=[100*e,100*(1-e)],n=t.map(o=>{if(o===0||!Number.isFinite(o))return 0;let s=Math.floor(Math.log10(Math.abs(o))),i=o.toFixed(Math.max(0,2-s));return(i.includes(".")?i.replace(/0+$/,"").split(".")[1]:"").length}),r=Math.max(...n);return[`${t[0].toFixed(r)} %`,`${t[1].toFixed(r)} %`]}function Iw(e,t={}){B(t,Aw,"coefci");let n=t.level??.95;if(typeof n!="number")throw new TypeError("'level' must be a number");if(!(n>=0&&n<=1))throw new RangeError("'level' must be between 0 and 1");let r=du(e,t.vcov),o=gu(e,t.df),s=r.est.map((m,u)=>u),i=t.parm;if(i!==void 0){let m=Array.isArray(i)?i:[i];if(m.every(u=>typeof u=="string")){let u=new Set(m);s=s.filter(h=>u.has(r.names[h]))}else if(m.every(u=>typeof u=="number"))s=m.map(u=>{if(!Number.isInteger(u)||u<0)throw new RangeError("'parm' must hold 0-based indices or names");if(u>=r.est.length)throw new RangeError("subscript out of bounds");return u});else throw new TypeError("'parm' must hold 0-based indices or names")}let c=(1-n)/2,[a,l]=wu(o)?[gt(c,o,!0,!1),c<1e-8?gt(c,o,!1,!1):gt(1-c,o,!0,!1)]:[le(c),c<1e-8?le(c,{lowerTail:!1}):le(1-c)],f=(m,u,h)=>m+h*u;return Q("coefci",{lower:s.map(m=>f(r.est[m],r.se[m],a)),upper:s.map(m=>f(r.est[m],r.se[m],l)),names:s.map(m=>r.names[m]),colNames:kw(c),warnings:r.warnings},e,typeof t.vcov=="function"?void 0:t.vcov)}var _w=["test","dispersion"],Lw=(e,t)=>bt(e,t/2,2,!1,!1);function Cw(e,t,n){let{fit:r,w:o}=Mo(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 ac=(e,t)=>Qi(t.x,e.fit.workingResiduals,e.fit.workingWeights,e.intercept);function Fw(e,t={}){B(t,_w,"glmAnova");let n=Array.isArray(e)?e:[e];if(n.length===0)throw new RangeError("no fits to compare");let r=n.map(zn),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:Dl(t.dispersion,o),c=i?.s??0,a=Ow(s,Array.isArray(e)&&s.length>1,{...t,...i===void 0?{}:{dispersion:i.m}},c);if(o===0&&c===0)return Q("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]?V(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"),w=s[0].model.x.length;for(let d of m)d[u]=d[h]+2*(w-d[p])*t.dispersion}return Q("glmAnova",{...a,table:m},...n)}function Ow(e,t,n,r=0){let o=new Et,s=e[0],i=Ca(s.model.familySpec);if(e.some(E=>{let R=Ca(E.model.familySpec);return R.family!==i.family||R.name!==i.name||R.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(T=>T.model.x.length!==E))throw new RangeError("models were not all fitted to the same size of dataset");let R=(T,A)=>T.length===A.length&&T.every((S,C)=>JSON.stringify(S)===JSON.stringify(A[C]));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,A)=>l.push({name:String(A+1),dfRes:T.dfResidual,dev:T.deviance})),f=l.map((T,A)=>A===0?NaN:l[A-1].dfRes-T.dfRes),m=l.map((T,A)=>A===0?NaN:l[A-1].dev-T.dev),e.forEach((T,A)=>{if(A===0||f[A]===0)return;let[S,C]=f[A]>0?[e[A-1],T]:[T,e[A-1]],L=I=>I.filter((O,q)=>S.priorWeights[q]>0&&C.priorWeights[q]>0),k=S.model.offset.map((I,O)=>I-C.model.offset[O]),F=k.some((I,O)=>I!==0&&S.priorWeights[O]>0)?S.model.x.map((I,O)=>[...I,k[O]]):S.model.x;Ns(L(F),L(C.model.x))||h.add(A)}),a==="Rao"){let T=e.map(A=>({dfRes:A.dfResidual,dev:A.deviance,fit:bu(A),x:A.model.x,intercept:A.model.intercept}));u=l.map((A,S)=>S===0?NaN:f[S]<0?-ac(T[S],T[S-1]):ac(T[S-1],T[S]))}p=e.reduce((T,A)=>A.dfResidual<T.dfResidual?A:T)}else{let E=s,R=E.model,T=R.x.length>0?R.x[0].length-(R.intercept?1:0):0,A=R.intercept?[0]:[],S=L=>[...A,...Array.from({length:L},(k,F)=>F+A.length)],C=[];for(let L=0;L<=T;L++)L===T?C.push({dfRes:E.dfResidual,dev:E.deviance,fit:bu(E),x:R.x,intercept:R.intercept}):L>0||a==="Rao"?C.push(Cw(R,S(L),o)):C.push(void 0);l.push({name:"NULL",dfRes:E.dfNull,dev:E.nullDeviance});for(let L=1;L<=T;L++)l.push({name:`x${L}`,dfRes:C[L].dfRes,dev:C[L].dev});f=l.map((L,k)=>k===0?NaN:l[k-1].dfRes-L.dfRes),m=l.map((L,k)=>k===0?NaN:Math.max(l[k-1].dev-L.dev,0)),a==="Rao"&&(u=l.map((L,k)=>k===0?NaN:ac(C[k-1],C[k]))),p=E}e.forEach((E,R)=>{E.converged||o.add(`model ${R+1} has not converged: its deviance is not that of the maximum likelihood fit`)});let w=new Et,d=n.dispersion??Eo(p,w);w.list.forEach(E=>o.add(E));let b=c?1/0:p.dfResidual,y=t?["Resid. Df","Resid. Dev","Df","Deviance"]:["Df","Deviance","Resid. Df","Resid. Dev"],v=l.map((E,R)=>t?[E.dfRes,E.dev,f[R],m[R]]:[f[R],m[R],E.dfRes,E.dev]),M=[...y],g=E=>l.map((R,T)=>{if(T===0||f[T]===0||h.has(T))return NaN;let A=Ve(E[T]/d*Math.sign(f[T]),-2*r);return A<0||Number.isNaN(A)?NaN:Lw(A,Math.abs(f[T]))}),N=[];if(a==="Chisq"||a==="LRT")M.push("Pr(>Chi)"),N=[g(m)];else if(a==="Rao")M.push("Rao","Pr(>Chi)"),N=[u.map((E,R)=>h.has(R)?NaN:E),g(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((R,T)=>{let A=Ve(m[T]/f[T]/d,-2*r);return T===0||f[T]===0||h.has(T)||A<0?NaN:A});M.push("F","Pr(>F)"),N=[E,E.map((R,T)=>Number.isNaN(R)?NaN:ht(R,Math.abs(f[T]),b,!1,!1))]}}else if(a==="Cp"){let E=p.model.x.length;M.push("Cp"),N=[l.map(R=>R.dev+2*(E-R.dfRes)*d)]}let x=v.map((E,R)=>[...E,...N.map(T=>T[R])]);return{colNames:M,rowNames:l.map(E=>E.name),table:x,warnings:o.list}}function bu(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 Dw=["type"];function vu(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,w)=>e.weights===void 0||e.weights[w]!==0?[w]:[]),c=i.map(p=>e.weights===void 0?1:Math.sqrt(e.weights[p])),a=Lt(i.map((p,w)=>s.map(d=>e.x[p][d]*c[w]))),l=Xs(e.weights===void 0?void 0:i.map(p=>V(e.weights[p],-o)),i.length),f=a.pivot.slice(0,a.rank),{qty:m}=Ur(f.map(p=>i.map((w,d)=>At(l[d],e.x[w][s[p]]))),i.map((p,w)=>At(l[w],V(e.y[p],-r)))),u=[0,0],h=0;return f.forEach((p,w)=>{p>=t.length&&(u=he(u,_e(m[w],m[w])),h++)}),{ss:u[0],df:h}}function ic(e,t={}){if(B(t,Dw,"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=Vs([e]),i=$s([e],s)[0];if(o===0)throw new Error("residual df = 0");let c=vi(e,s),a=N=>{let x=c.map(([T,A])=>[V(T,-N),A]),E=x.reduce((T,[A])=>T+A,0),R=x.reduce((T,[A,S])=>T+A*S,0)/E;return Number.isFinite(E)&&Number.isFinite(R)?x.reduce((T,[A,S])=>T+A*(S-R)**2,0):NaN},l=0,f=a(0);if(Number.isFinite(f)||(l=zs(c),f=a(l)),yr(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=N=>e.assign.flatMap((x,E)=>N.has(x)?[E]:[]),p=[...new Set(e.assign)],w=[],d=[],b=N=>{w.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 R=h(new Set([E])),T=h(new Set(p.filter(A=>A!==E)));w.push(E===0?"(Intercept)":e.termLabels[E-1]),x.push({ss:vu(e,T,R,s,N).ss,df:R.length})}}else{let E=R=>e.factors.flatMap((T,A)=>T[R-1]>0?[A]:[]);for(let R of p){if(R===0)continue;let T=E(R),A=p.filter(L=>L!==0&&L!==R&&T.every(k=>e.factors[k][L-1]>0)),S=h(new Set(p.filter(L=>L!==R&&!A.includes(L)))),C=vu(e,S,h(new Set([R])),s,N);w.push(e.termLabels[R-1]),x.push(C)}}return x};d=b(l),l===0&&e.weights!==void 0&&d.some(N=>!Number.isFinite(N.ss))&&(l=zs(c),d=b(l));let y=N=>V(N,2*s+l),v=V(i,-l),M=v/o,g=d.map(({ss:N,df:x})=>{let E=N/x/M;return[y(N),x,E,ht(E,x,o,!1,!1)]});return g.push([y(v),o,NaN,NaN]),Q("Anova",{colNames:["Sum Sq","Df","F value","Pr(>F)"],rowNames:[...w,"Residuals"],table:g,warnings:m},e)}var ta=[0,0];var cc=e=>e.map(t=>t.map(n=>n[0]));function Nu(e){let t=e[0]?.length??0;return Array.from({length:t},(n,r)=>e.map(o=>o[r]))}function qr(e,t){let n=t[0]?.length??0;return e.map(r=>{let o=new Array(n).fill(ta);return r.forEach((s,i)=>{let c=t[i];for(let a=0;a<n;a++)o[a]=he(o[a],_e(s,c[a]))}),o})}function kr(e){let t=ta;return e.forEach((n,r)=>t=he(t,n[r])),t}function yu(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=at(n[a][i],n[i][i]);n[a][i]=l;for(let f=i+1;f<t;f++)n[a][f]=Oe(n[a][f],_e(l,n[i][f]))}}return{a:n,perm:r,sign:o,singular:s}}function Mu(e){let{a:t,sign:n,singular:r}=yu(e);if(r)return ta;let o=[n,0];return t.forEach((s,i)=>o=_e(o,s[i])),o}function Eu(e){let t=e.length,{a:n,perm:r,singular:o}=yu(e);if(o)throw new Error("singular matrix");let s=Array.from({length:t},()=>new Array(t).fill(ta));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]=Oe(c[a],_e(n[a][l],c[l]));for(let a=t-1;a>=0;a--){for(let l=a+1;l<t;l++)c[a]=Oe(c[a],_e(n[a][l],c[l]));c[a]=at(c[a],n[a][a])}for(let a=0;a<t;a++)s[a][i]=c[a]}return s}var Pw=["group","idata","M","X"],xu=(e,t)=>bt(e,t/2,2,!1,!1);function lc(e){let t=e.length,n=lt(kr(e),t);return Mu(e.map(r=>r.map(o=>at(o,n))))[0]}function fc(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=xu(i,c),l=xu(i,c+4);return a+s*(l-a)}var Vw=(e,t)=>Array.from({length:t},(n,r)=>e.map(o=>o[r]));function $w(e,t,n){let r=e.length,o=b=>b.length===0?0:Lt(Array.from({length:r},(y,v)=>b.map(M=>M[v]))).rank,s=n.length===0?void 0:Lt(Array.from({length:r},(b,y)=>n.map(v=>v[y]))),i=s===void 0?[]:s.pivot.slice(0,s.rank).map(b=>n[b]),c=i.length,a=[...i,...t],l=Lt(Array.from({length:r},(b,y)=>a.map(v=>v[y])));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(y=>[y,0]),u=new Array(r).fill([0,0]),h=Bn(f.map(m),u,e.map((b,y)=>e.map(v=>v[y]))).qtv,p=Array.from({length:r},(b,y)=>h.map(v=>v[y])),w=Bn(f.map(m),u,p).qtv,d=Array.from({length:l.rank-c},(b,y)=>c+y);return d.map(b=>d.map(y=>w[b][y]))}function zw(e,t={}){B(t,Pw,"mauchlyTest");let n=e.map(g=>Array.from(g)),r=n[0]?.length??0;if(r===0||n.some(g=>g.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=g=>g==null||typeof g=="number"&&Number.isNaN(g),i=n.map((g,N)=>!g.some(s)&&(o===void 0||!s(o[N])));if(n.some((g,N)=>i[N]&&g.some(x=>!Number.isFinite(x))))throw new RangeError("y must not contain infinite values");let c=qe(n.filter((g,N)=>i[N])).groups,a=c.length,l=o?.filter((g,N)=>i[N]).map(ft)??c.map(()=>""),f=[...new Set(l)].sort(dn),m=c.map(g=>g.map(()=>[0,0]));for(let g of f){let N=l.flatMap((x,E)=>x===g?[E]:[]);for(let x=0;x<r;x++){let E=[0,0];for(let T of N)E=he(E,[c[T][x],0]);let R=lt(E,N.length);for(let T of N)m[T][x]=Oe([c[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 h=Array.from({length:r},(g,N)=>Array.from({length:r},(x,E)=>{let R=[0,0];for(let T of m)R=he(R,_e(T[N],T[E]));return R})),p=t.idata??{index:Array.from({length:r},(g,N)=>N+1)},w=g=>{let N=rt(()=>jr(g,p));if(N.x.length!==r)throw new RangeError("idata must have one row per column of y");return Vw(N.x,N.colNames.length)},d=t.M===void 0?Array.from({length:r},(g,N)=>Array.from({length:r},(x,E)=>E===N?1:0)):w(t.M),b=w(t.X??"~0"),y=$w(h,d,b),v=y.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 M=lc(y);return{statistic:M,statisticName:"W",pValue:fc(M,v,u,r),method:"Mauchly's test of sphericity",warnings:[]}}var jw=["n","d","type","sigLevel","power","alternative"],Ww=["n1","n2","d","sigLevel","power","alternative"],Bw=["h","n","sigLevel","power","alternative"],Kw=["h","n1","n2","sigLevel","power","alternative"],Uw=["h","n","sigLevel","power","alternative"],Hw=["d","n","sigLevel","power","alternative"],Yw=["n","r","sigLevel","power","alternative"],Gw=["w","N","df","sigLevel","power"],Xw=["k","n","f","sigLevel","power"],Qw=["u","v","f2","sigLevel","power"],Zw=["size"],Jw={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 Tu(e="p",...t){B(bs(t),Zw,"cohenES");let n=ws(["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:Jw[r][["small","medium","large"].indexOf(o)],method:"Conventional effect size from Cohen (1982)"}}function e1(e,t){if(!na(e)||!na(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 t1(e,t){if(e.length!==t.length)throw new RangeError("'P0' and 'P1' must have the same length");if(![...e,...t].every(na))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+=Vt(t[r]-e[r],e[r]));return Math.sqrt(n)}function n1(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(na)))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>=r1)o+=Vt(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 r1=2**-1022,na=e=>e>=0&&e<=1,o1="\u2018sig.level\u2019 must be numeric in [0, 1]",s1="\u2018power\u2019 must be numeric in [0, 1]";function cr(e,t){let n=r=>!ye(r)&&!(typeof r=="number"&&r>=0&&r<=1);if(n(e))throw new RangeError(o1);if(n(t))throw new RangeError(s1)}function lr(e,t){if(typeof e=="number"&&Number.isNaN(e))throw new RangeError("the effect size must not be NaN");return typeof e=="string"?Tu(t,e).effectSize:e}var jo=["two.sided","less","greater"];function ra(e,t){return e==="two.sided"?t:e==="less"?[-10,5]:[-5,10]}function Au(e,t,n,r){if(r==="less")return br(jt(n,e),e,t,!0);if(r==="greater")return br(jt(n,e,{lowerTail:!1}),e,t,!1);let o=jt(n/2,e,{lowerTail:!1});return br(o,e,t,!1)+br(-o,e,t,!0)}var Ru=e=>e<1e-8?le(e,{lowerTail:!1}):le(1-e);function Su(e,t,n){return n==="greater"?ae(Ru(t)-e,{lowerTail:!1}):n==="less"?ae(le(t)-e):ae(Ru(t/2)-e,{lowerTail:!1})+ae(le(t/2)-e)}function a1(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*=V(r,-o),n+=o}return V(t,n)}function i1(e={}){B(e,jw,"pwrTTest");let{n:t,power:n}=e,r=lr(e.d,"t"),o=e.sigLevel===void 0?.05:e.sigLevel;if(tn([t,r,n,o],"exactly one of n, d, power, and sig.level must be NULL"),cr(o,n),!ye(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,jo),c=s==="two.sample"?2:1;i==="two.sided"&&!ye(r)&&(r=Math.abs(r));let a=(m,u,h)=>Au((m-1)*c,Math.sqrt(m/c)*u,h,i);if(ye(n))n=a(t,r,o);else{let m=n;ye(t)?t=Ye(u=>a(u,r,o)-m,2+1e-10,1e9):ye(r)?r=Ye(u=>a(t,u,o)-m,...ra(i,[1e-7,10])):o=Ye(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 c1(e={}){B(e,Ww,"pwrT2nTest");let{n1:t,n2:n,power:r}=e,o=lr(e.d,"t"),s=e.sigLevel===void 0?.05:e.sigLevel;if(tn([t,n,o,r,s],"exactly one of n1, n2, d, power, and sig.level must be NULL"),cr(s,r),!ye(t)&&!(t>=2))throw new RangeError("number of observations in the first group must be at least 2");if(!ye(n)&&!(n>=2))throw new RangeError("number of observations in the second group must be at least 2");let i=Ae(e.alternative,jo);i==="two.sided"&&!ye(o)&&(o=Math.abs(o));let c=(a,l,f,m)=>Au(a+l-2,f/Math.sqrt(1/a+1/l),m,i);if(ye(r))r=c(t,n,o,s);else{let a=r;ye(t)?t=Ye(l=>c(l,n,o,s)-a,2+1e-10,1e9):ye(n)?n=Ye(l=>c(t,l,o,s)-a,2+1e-10,1e9):ye(o)?o=Ye(l=>c(t,n,l,s)-a,...ra(i,[1e-7,10])):s=Ye(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 mc(e,t,n,r,o,s){let{n:i,power:c}=t,a=lr(e,n==="h"?"p":"t"),l=t.sigLevel===void 0?.05:t.sigLevel;if(tn([a,i,c,l],`exactly one of ${n}, n, power, and sig.level must be NULL`),cr(l,c),!ye(i)&&!(i>=1))throw new RangeError("number of observations in each group must be at least 1");let f=Ae(t.alternative,jo);f==="two.sided"&&!ye(a)&&(a=Math.abs(a));let m=(u,h,p)=>Su(u*r(h),p,f);if(ye(c))c=m(a,i,l);else{let u=c;ye(a)?a=Ye(h=>m(h,i,l)-u,...ra(f,[s,10])):ye(i)?i=Ye(h=>m(a,h,l)-u,o,1e9):l=Ye(h=>m(a,i,h)-u,1e-10,1-1e-10)}return{es:a,n:i,sigLevel:l,power:c,alternative:f}}function l1(e={}){B(e,Bw,"pwr2pTest");let t=mc(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 f1(e={}){B(e,Uw,"pwrPTest");let t=mc(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 m1(e={}){B(e,Hw,"pwrNormTest");let t=mc(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 u1(e={}){B(e,Kw,"pwr2p2nTest");let{n1:t,n2:n,power:r}=e,o=lr(e.h,"p"),s=e.sigLevel===void 0?.05:e.sigLevel;if(tn([o,t,n,r,s],"exactly one of h, n1, n2, power, and sig.level must be NULL"),cr(s,r),!ye(t)&&!(t>=2))throw new RangeError("number of observations in the first group must be at least 2");if(!ye(n)&&!(n>=2))throw new RangeError("number of observations in the second group must be at least 2");let i=Ae(e.alternative,jo);i==="two.sided"&&!ye(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)=>Su(l*Math.sqrt(c(f,m)),u,i);if(ye(r))r=a(o,t,n,s);else{let l=r;ye(o)?o=Ye(f=>a(f,t,n,s)-l,...ra(i,[1e-10,10])):ye(t)?t=Ye(f=>a(o,f,n,s)-l,2+1e-10,1e9):ye(n)?n=Ye(f=>a(o,t,f,s)-l,2+1e-10,1e9):s=Ye(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 h1(e={}){B(e,Yw,"pwrRTest");let{n:t,power:n}=e,r=lr(e.r,"r"),o=e.sigLevel===void 0?.05:e.sigLevel;if(tn([t,r,n,o],"exactly one of n, r, power, and sig.level must be NULL"),cr(o,n),!ye(t)&&!(t>=4))throw new RangeError("number of observations must be at least 4");if(!ye(r)&&!(Math.abs(r)<=1))throw new RangeError("'r' must be in [-1, 1]");let s=Ae(e.alternative,jo);s==="two.sided"&&!ye(r)&&(r=Math.abs(r));let i=(a,l,f,m)=>{let u=jt(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),w=Math.atanh(l)+l/(2*(a-1)),d=Math.atanh(p),b=ae((w-d)*Math.sqrt(a-3));return m?b+ae((-w-d)*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(ye(n))n=c(t,r,o),s==="less"&&(r=-r);else{let a=n;if(ye(t))t=Ye(l=>c(l,r,o)-a,4+1e-10,1e9);else if(ye(r)){let[l,f]=s==="two.sided"?[1e-10,.9999999999]:[-.9999999999,.9999999999];r=Ye(m=>c(t,m,o)-a,l,f)}else o=Ye(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 p1(e){B(e,Gw,"pwrChisqTest");let{N:t,power:n}=e,{df:r}=e,o=lr(e.w,"chisq"),s=e.sigLevel===void 0?.05:e.sigLevel;if(tn([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(cr(s,n),!ye(o)&&!(o>=0))throw new RangeError("w must be positive");if(!ye(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:It(Fn(l,r,{lowerTail:!1}),r,{ncp:f,lowerTail:!1})};if(ye(n))n=i(o,t,s);else{let c=n;ye(o)?o=Ye(a=>i(a,t,s)-c,1e-10,1e9):ye(t)?t=Ye(a=>i(o,a,s)-c,1+1e-10,1e9):s=Ye(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 d1(e={}){B(e,Xw,"pwrAnovaTest");let{k:t,n,power:r}=e,o=lr(e.f,"anov"),s=e.sigLevel===void 0?.05:e.sigLevel;if(tn([t,n,o,r,s],"exactly one of k, n, f, power, and sig.level must be NULL"),cr(s,r),!ye(o)&&!(o>=0))throw new RangeError("f must be positive");if(!ye(t)&&!(t>=2))throw new RangeError("number of groups must be at least 2");if(!ye(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=a1([c,a,l,l])),h===1/0?1:Rs(m,u)?Ts(m,u,h,f):Tn(Yn(f,m,u,{lowerTail:!1}),m,u,{ncp:h,lowerTail:!1})};if(ye(r))r=i(t,n,o,s);else{let c=r;ye(t)?t=Ye(a=>i(a,n,o,s)-c,2+1e-10,100):ye(n)?n=Ye(a=>i(t,a,o,s)-c,2+1e-10,1e9):ye(o)?o=Ye(a=>i(t,n,a,s)-c,1e-7,1e7):s=Ye(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 g1(e={}){B(e,Qw,"pwrF2Test");let{u:t,v:n,power:r}=e,o=lr(e.f2,"f2"),s=e.sigLevel===void 0?.05:e.sigLevel;if(tn([t,n,o,r,s],"exactly one of u, v, f2, power, and sig.level must be NULL"),cr(s,r),!ye(o)&&!(o>=0))throw new RangeError("f2 must be positive");if(!ye(t)&&!(t>=1))throw new RangeError("degree of freedom u for numerator must be at least 1");if(!ye(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:Rs(c,a)?Ts(c,a,m,f):Tn(Yn(f,c,a,{lowerTail:!1}),c,a,{ncp:m,lowerTail:!1})};if(ye(r))r=i(t,n,o,s);else{let c=r;ye(t)?t=Ye(a=>i(a,n,o,s)-c,1+1e-10,100):ye(n)?n=Ye(a=>i(t,a,o,s)-c,1+1e-10,1e9):ye(o)?o=Ye(a=>i(t,n,a,s)-c,1e-7,1e7):s=Ye(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 w1=["id","dv","between","within","type","correction","es","observed"],oa=e=>e===void 0?[]:typeof e=="string"?[e]:[...e],b1=1e-12,qu=1e-24;function ku(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 Wo=e=>typeof e=="boolean"?e?"TRUE":"FALSE":ft(e);function v1(e){let t=[...new Set(e)];return t.every(n=>typeof n=="number")?t.sort((n,r)=>n-r).map(Wo):[...new Set(t.map(Wo))].sort(dn)}var Iu=(e,t)=>t.split(":").includes(e);function _u(e,t,n,r,o){let s=l=>r.some(f=>Iu(f,l));for(let l of r)if(!e.some(f=>Iu(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 N1(e,t){B(t,w1,"aovEz");let n=oa(t.between),r=oa(t.within);if(n.length===0&&r.length===0)throw new Error("Either between or within need to be non-NULL!");for(let[j,U]of[["between",n],["within",r]]){let W=U.filter((te,fe)=>U.indexOf(te)!==fe);if(W.length>0)throw new RangeError(`duplicated factor names in '${j}': ${[...new Set(W)].join(", ")}`)}let o=n.filter(j=>r.includes(j));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=oa(t.es??"ges");for(let j of c)if(j!=="ges"&&j!=="pes")throw new RangeError(`invalid 'es': ${String(j)}`);let a=oa(t.observed),l=[],f=j=>j==null||typeof j=="number"&&Number.isNaN(j),m=ku(e,t.dv),u=[t.id,...n,...r].map(j=>ku(e,j));for(let j of u)if(j.length!==m.length)throw new RangeError("the columns must have the same length");let h=m.map((j,U)=>u.every(W=>!f(W[U]))),p=j=>j.filter((U,W)=>h[W]),w=St(p(u[0])).map(Wo),d=p(m).map(j=>{if(f(j))return NaN;if(typeof j!="number")throw new TypeError(`the response '${t.dv}' must be numeric`);if(!Number.isFinite(j))throw new RangeError(`the response '${t.dv}' must be finite`);return j}),b=n.map((j,U)=>St(p(u[1+U]))),y=r.map((j,U)=>St(p(u[1+n.length+U]))),v=w.length;for(let j of[d,...b,...y])if(j.length!==v)throw new RangeError("the columns must have the same length");let M=b.map(j=>v1(j)),g=b.map(j=>j.map(Wo)),N=y.map(j=>j.map(Wo)),x=N.map(j=>[...new Set(j)]);M.forEach((j,U)=>{if(j.length<2)throw new Error(`Factor "${n[U]}" consists of one level only. Remove factor from model?`)}),x.forEach((j,U)=>{if(j.length<2)throw new Error(`Factor "${r[U]}" consists of one level only. Remove factor from model?`)});let E=[...new Set(w)],R=new Map,T=new Set;if(w.forEach((j,U)=>{let W=JSON.stringify(g.map(fe=>fe[U])),te=R.get(j);te===void 0?R.set(j,W):te!==W&&T.add(j)}),T.size>0)throw new Error(`Following ids are in more than one between subjects condition:
8
- ${E.filter(j=>T.has(j)).join(", ")}`);let A=[[]];for(let j of x)A=A.flatMap(U=>j.map(W=>[...U,W]));let S=j=>JSON.stringify(N.map(U=>U[j])),C=new Map(A.map((j,U)=>[JSON.stringify(j),U])),L=new Set(w.map((j,U)=>S(U)));if(A.some(j=>!L.has(JSON.stringify(j))))throw new Error("Empty cells in within-subjects design (i.e., bad data structure).");let k=new Map,F=new Set;w.forEach((j,U)=>{let W=k.get(j);W===void 0&&k.set(j,W=new Set),W.add(S(U)),Number.isNaN(d[U])&&F.add(j)});let I=E.filter(j=>F.has(j)||k.get(j).size<A.length),O=new Set(I),q=qe([d.filter((j,U)=>!O.has(w[U]))]).k,$=q===0?d:d.map(j=>V(j,-q)),_=j=>V(j,2*q),P=A.length,K=new Map;if(w.forEach((j,U)=>{let W=K.get(j);W===void 0&&K.set(j,W={s:A.map(()=>[0,0]),n:A.map(()=>0)});let te=C.get(S(U));W.s[te]=he(W.s[te],[$[U],0]),W.n[te]++}),[...K.values()].some(j=>j.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."),I.length>0){let j=[...I].sort(dn),U=j.length>=10?`${j.slice(0,10).join(", ")}, ... [showing first 10 only]`:j.join(", ");l.push(`Missing values for ${I.length} ID(s), which were removed before analysis:
9
- ${U}`)}let X=E.filter(j=>!I.includes(j)),re=new Map;w.forEach((j,U)=>re.has(j)||re.set(j,U));let z=X.map(j=>{let U=K.get(j);return U.s.map((W,te)=>lt(W,U.n[te]))}),J={};n.forEach((j,U)=>J[j]=X.map(W=>g[U][re.get(W)]));let Z=Object.fromEntries(n.map(j=>[j,"contr.sum"])),Se=Object.fromEntries(n.map((j,U)=>[j,M[U]])),ne="Rank deficient model matrix; insufficient data to estimate full model.";if(P===1)return Q("aovEz",D());return Q("aovEz",Y());function D(){let j="y";for(;n.includes(j);)j+="_";let U={...J,[j]:z.map(me=>me[0][0])},W=rt(()=>Vo(`${j} ~ ${n.length>0?n.join(" * "):"1"}`,U,{contrasts:Z,levels:Se}));if(W.rank<W.coefNames.length)throw new Error(ne);if(W.dfResidual<=0)throw new RangeError(`no residual degrees of freedom: ${z.length} subject(s) for ${W.rank} parameter(s)`);let te=rt(()=>ic(W,{type:s}));l.push(...te.warnings.filter(me=>!l.includes(me)));let fe=W.dfResidual,Re=te.rowNames.flatMap((me,ke)=>{if(me==="(Intercept)"||me==="Residuals")return[];let[de,Ce,xe,Pe]=te.table[ke];return[{name:me,ss:de,numDf:Ce,errSS:W.rss,denDf:fe,f:xe,p:Pe}]}),Le=te.colNames.indexOf("Sum Sq"),be={anova:_u(Re,new Map([[0,W.rss]]),c,a,_),univariate:{colNames:te.colNames,rowNames:te.rowNames,table:te.table.map(me=>me.map((ke,de)=>de===Le?_(ke):ke))},within:r,warnings:l};return Kt.set(be,{colNames:te.colNames,rowNames:te.rowNames,table:te.table}),be}function Y(){let j=X.length,U=z.reduce((Me,Ne)=>Ne.reduce((Ee,ee)=>Ee+ee[0]*ee[0],Me),0),W=n.length>0?rt(()=>jr(`~ ${n.join(" * ")}`,J,{contrasts:Z,levels:Se})):{colNames:["(Intercept)"],assign:[0],x:X.map(()=>[1]),termLabels:[],factors:[]},te=W.colNames.length;if(j===0||Lt(W.x).rank<te)throw new Error(ne);let fe=j-te;if(fe<=0)throw new RangeError(`no residual degrees of freedom: ${j} subject(s) for ${te} between-subject parameter(s)`);let Re=[0,...W.termLabels.map((Me,Ne)=>Ne+1)],Le=["(Intercept)",...W.termLabels],be=Me=>W.assign.flatMap((Ne,Ee)=>Me.includes(Ne)?[Ee]:[]),me=Me=>W.factors.flatMap((Ne,Ee)=>Ne[Me-1]>0?[Ee]:[]),ke=Me=>W.x.map(Ne=>[Ne[Me],0]),de=new Array(j).fill([0,0]),Ce=(Me,Ne,Ee)=>{let{qtv:ee}=Bn([...Me,...Ne].map(ke),de,Ee);return ee.map(ve=>ve.slice(Me.length,Me.length+Ne.length))},xe=Me=>{let{qtv:Ne}=Bn(W.colNames.map((Ee,ee)=>ke(ee)),de,Me);return Ne.map(Ee=>Ee.slice(te))},Pe=Object.fromEntries(r.map((Me,Ne)=>[Me,A.map(Ee=>Ee[Ne])])),Fe=rt(()=>jr(`~ ${r.join(" * ")}`,Pe,{contrasts:Object.fromEntries(r.map(Me=>[Me,"contr.sum"])),levels:Object.fromEntries(r.map((Me,Ne)=>[Me,x[Ne]]))})),Ke=[0,...Fe.termLabels.map((Me,Ne)=>Ne+1)],ze=["(Intercept)",...Fe.termLabels],et=Me=>Me.map(Ne=>Me.map(Ee=>{let ee=[0,0];return Ne.forEach((ve,ie)=>ee=he(ee,_e(ve,Ee[ie]))),ee})),Ue=[],Xe=[],pe=[],oe=[],we=[],ge=[],ce=[],tt=new Map,G=!1;for(let Me of Ke){let Ne=Fe.assign.flatMap((je,We)=>je===Me?[We]:[]),Ee=Ne.length,ee=Ne.map(je=>z.map(We=>We.reduce((xt,Rt,Rn)=>he(xt,At(Rt,Fe.x[Rn][je])),[0,0]))),ve=Ne.map(je=>Ne.map(We=>[Fe.x.reduce((xt,Rt)=>xt+Rt[je]*Rt[We],0),0])),ie=Eu(ve),Te=et(xe(ee)),Be=qr(Te,ie),$e=kr(Be)[0],ot=NaN,mt=NaN,Ct=NaN,Ut=NaN,Hn=!1;if(Ee>=2){let je=ze[Me],We=Fl(cc(ie)).map(Ht=>Ht.map(rn=>[rn,0])),xt=cc(qr(qr(We,Te),Nu(We))),Rt=Wt(xt,{symmetric:!0,onlyValues:!0}).values,Rn=Rt.reduce((Ht,rn)=>Math.max(Ht,rn),0),$t=Math.max(Rn*b1,U*qu),kt=kr(Be),ut=kr(qr(Be,Be));Rn>U*qu&&ut[0]>0&&Number.isFinite(ut[0])?(Hn=!0,ot=at(_e(kt,kt),At(ut,Ee))[0],fe-Ee*ot>0?mt=((fe+1)*Ee*ot-2)/(Ee*(fe-Ee*ot)):l.push(`The Huynh-Feldt epsilon of ${je} is undefined (d - p GG <= 0)${i==="HF"?": corrected with GG":""}`)):l.push(`The error SSP matrix of ${je} is 0 (or at the rounding level of the data): no sphericity corrections (the p-values are uncorrected)`),Hn&&Rt.every(Ht=>Ht>$t)?(Ct=lc(qr(ie,Te)),Ut=fc(Ct,Ee,fe,P)):Hn&&l.push(`The error SSP matrix of ${je} is singular: no Mauchly test (the epsilons are still defined)`)}for(let je=0;je<Re.length;je++){let We=Re[je],xt;if(s===3)xt=Ce(be(Re.filter(Ft=>Ft!==We)),be([We]),ee);else if(We===0)xt=Ce([],be([0]),ee);else{let Ft=me(We),Zt=Re.filter(H=>H!==0&&H!==We&&Ft.every(se=>W.factors[se][H-1]>0));xt=Ce(be(Re.filter(H=>H!==We&&!Zt.includes(H))),be([We]),ee)}let Rt=kr(qr(et(xt),ie))[0],$t=(We===0?1:be([We]).length)*Ee,kt=fe*Ee,ut=Rt/$t/($e/kt),Ht=ht(ut,$t,kt,!1,!1),rn=Me===0?Le[je]:We===0?ze[Me]:`${Le[je]}:${ze[Me]}`;Ue.push([_(Rt),$t,_($e),kt,ut,Ht]),Xe.push([Rt,$t,$e,kt,ut,Ht]),pe.push(rn),tt.set(Me,$e);let Qt={name:rn,ss:Rt,numDf:$t,errSS:$e,denDf:kt,f:ut,p:Ht};if(Hn){let Ft=Math.min(1,mt),Zt=ht(ut,ot*$t,ot*kt,!1,!1),H=ht(ut,Ft*$t,Ft*kt,!1,!1);oe.push([ot,Zt,mt,H]),we.push([Ct,Ut]),ge.push(rn),i==="GG"?Qt={...Qt,numDf:ot*$t,denDf:ot*kt,p:Zt}:i==="HF"&&(mt>1&&(G=!0),Qt=Number.isNaN(mt)?{...Qt,numDf:ot*$t,denDf:ot*kt,p:Zt}:{...Qt,numDf:Ft*$t,denDf:Ft*kt,p:H})}Me===0&&We===0||ce.push(Qt)}}G&&l.push("HF eps > 1 treated as 1");let ue={anova:_u(ce,tt,c,a,_),univariate:{colNames:["Sum Sq","num Df","Error SS","den Df","F value","Pr(>F)"],rowNames:pe,table:Ue},within:r,warnings:l};return oe.length>0&&(ue.corrections={colNames:["GG eps","Pr(>F[GG])","HF eps","Pr(>F[HF])"],rowNames:ge,table:oe},ue.sphericity={colNames:["Test statistic","p-value"],rowNames:[...ge],table:we}),Kt.set(ue,{colNames:ue.univariate.colNames,rowNames:pe,table:Xe}),ue}}function Gr(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=it(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 y1=["rho"],M1=new Set(["group","start","id","weights","ctype","stype","robust","rmean","confType","confInt"]),hc=e=>e==null||typeof e=="number"&&Number.isNaN(e);function E1(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&&hc(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=St(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=Gr(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=${ft(a)}`)}}function x1(e,t,n,r={}){B(r,y1,"survdiff");let{rho:o=0}=r;if(!Number.isFinite(o))throw new RangeError("'rho' must be a finite number");let{obs:s,labels:i}=E1(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 M=s[v].time,g=v,N=new Array(c).fill(0),x=new Array(c).fill(0),E=0;for(;g<s.length&&s[g].time===M;){let R=s[g];x[R.group]++,R.event&&(N[R.group]++,E++),g++}if(E>0){let R=o===0?1:p**o;for(let T=0;T<c;T++)l[T]+=R*N[T],f[T]+=R*E*u[T]/h;if(h>1){let T=R*R*E*(h-E)/(h*(h-1));for(let A=0;A<c;A++)for(let S=0;S<c;S++)m[A][S]+=T*u[A]*((A===S?1:0)-u[S]/h)}p*=1-E/h}for(let R=0;R<c;R++)u[R]-=x[R];h-=g-v,v=g}let w=f.map((v,M)=>v>0?M:-1).filter(v=>v>=0),d=0,b=Math.max(w.length-1,0);if(w.length>=2){let v=w.slice(1),M=v.map(N=>l[N]-f[N]),g=v.map(N=>v.map(x=>m[N][x]));try{let N=nt(g,M);d=M.reduce((x,E,R)=>x+E*N[R],0)}catch{({chisq:d,df:b}=R1(g,M))}}let y=b===0?NaN:It(d,b,{lowerTail:!1});return{n:a,groups:i,obs:l,exp:f,var:m,chisq:d,df:b,pvalue:y,pValue:y,warnings:[]}}function R1(e,t){let n=Wt(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 uc(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 en(e[r],e[s])}return e[r]}function fr(e,t,n={}){B(n,M1,"survfit");let{group:r,start:o,id:s,weights:i,ctype:c=1,stype:a=1,robust:l=!1,rmean:f="common",confType:m="log",confInt:u=.95}=n;if(typeof m!="string")throw new TypeError("confType must be a string");let h=["log","log-log","plain","none","logit","arcsin"],p=typeof m=="string"?h.filter(_=>_.startsWith(m)):[],w=h.includes(m)?m:p.length===1?p[0]:m;if(typeof c!="number")throw new TypeError("ctype must be a number");if(typeof a!="number")throw new TypeError("stype must be a number");if(c!==1&&c!==2)throw new RangeError("ctype must be 1 or 2");if(a!==1&&a!==2)throw new RangeError("stype must be 1 or 2");if(!h.some(_=>_===w))throw new RangeError("'arg' should be one of \u201Clog\u201D, \u201Clog-log\u201D, \u201Cplain\u201D, \u201Cnone\u201D, \u201Clogit\u201D, \u201Carcsin\u201D");if(typeof u!="number")throw new TypeError("confInt must be a number");if(!(u>0&&u<1))throw new RangeError("confidence intervals must be between 0 and 1");if(e.length!==t.length)throw new RangeError("'time' and 'status' must have the same length");if(r!==void 0&&r.length!==e.length)throw new RangeError("'group' must have the same length as 'time'");if(o!==void 0&&o.length!==e.length)throw new RangeError("'start' must have the same length as 'time'");if(s!==void 0&&s.length!==e.length)throw new RangeError("'id' must have the same length as 'time'");if(s!==void 0&&o===void 0)throw new RangeError("'id' requires 'start'");if(i!==void 0&&!Array.isArray(i))throw new TypeError("'weights' must be an array");if(i!==void 0&&i.length!==e.length)throw new RangeError("'weights' must have the same length as 'time'");if(typeof l!="boolean")throw new TypeError("'robust' must be a boolean");if(typeof f!="number"&&typeof f!="string")throw new TypeError("rmean must be a number or string");if(typeof f=="number"&&!Number.isFinite(f))throw new RangeError("'rmean' must be finite");if(typeof f!="number"&&f!=="none"&&f!=="common"&&f!=="individual")throw new RangeError("'arg' should be one of \u201Cnone\u201D, \u201Ccommon\u201D, \u201Cindividual\u201D");let d=[];for(let _=0;_<e.length;_++){let P=i?.[_];if(i&&P!=null&&typeof P!="number")throw new TypeError("weights must contain numbers");if(i&&(P==null||Number.isNaN(P)))continue;let K=P??1;if(!Number.isFinite(K))throw new RangeError("weights must be finite");if(K<0)throw new RangeError("weights must be non-negative");if(!Number.isSafeInteger(K))throw new RangeError("weights must be non-negative safe integers");if(e[_]!=null&&typeof e[_]!="number")throw new TypeError("'time' must contain numbers");if(t[_]!=null&&typeof t[_]!="number")throw new TypeError("'status' must contain numbers");if(o!==void 0&&o[_]!=null&&typeof o[_]!="number")throw new TypeError("'start' must contain numbers");if(!(e[_]==null||t[_]==null||Number.isNaN(e[_])||Number.isNaN(t[_])||o!==void 0&&(o[_]==null||Number.isNaN(o[_]))||r!==void 0&&hc(r[_])||s!==void 0&&hc(s[_]))){if(o!==void 0&&!(o[_]<e[_]))throw new RangeError("stop time must be > start time");if(t[_]!==0&&t[_]!==1)throw new RangeError("'status' must be 0 (censored) or 1 (event)");if(e[_]===-1/0||e[_]===1/0&&t[_]===1)throw new RangeError("event times must be finite");d.push(_)}}if(d.length===0)throw new RangeError("no observations without missing values");let b=r===void 0?[]:St(r),y=s===void 0?[]:Array.from(s),v=r===void 0?["all"]:Array.from(new Set(d.map(_=>b[_]))).sort((_,P)=>typeof _=="number"&&typeof P=="number"?_-P:String(_)<String(P)?-1:String(_)>String(P)?1:0).map(_=>`group=${ft(_)}`),M=new Map;r!==void 0&&Array.from(new Set(d.map(_=>b[_]))).sort((_,P)=>typeof _=="number"&&typeof P=="number"?_-P:String(_)<String(P)?-1:String(_)>String(P)?1:0).forEach((_,P)=>M.set(_,P));let g=new Map;s!==void 0&&Array.from(new Set(d.map(_=>y[_]))).forEach((_,P)=>g.set(_,P));let N=d.flatMap(_=>o===void 0?[e[_]]:[o[_],e[_]]),x=Gr(N),E=new Map,R=new Map;d.forEach((_,P)=>{if(o===void 0)R.set(_,x[P]);else if(E.set(_,x[2*P]),R.set(_,x[2*P+1]),!(E.get(_)<R.get(_)))throw new Error("aeqSurv exception, an interval has effective length 0")});let T=d.map(_=>({time:R.get(_),start:o===void 0?-1/0:E.get(_),event:t[_]===1,group:r===void 0?0:M.get(b[_]),id:s===void 0?_:g.get(y[_]),weight:i?.[_]??1})).sort((_,P)=>_.time-P.time),A=s===void 0?void 0:new Map;if(A)for(let _ of T){let P=A.get(_.id)??[];P.push(_),A.set(_.id,P)}let S=v,C=T.map(_=>_.time),L=Math.min(...C),k=Math.min(0,L);if(typeof f=="number"&&f<=k)throw new RangeError("'rmean' must be greater than the integration origin");let F=(1-u)/2,I=F<1e-8?le(F,{lowerTail:!1}):le(1-F),O=[],q={strata:[],time:[],nRisk:[],nEvent:[],nCensor:[],surv:[],stdErr:O,stdError:O,lower:[],upper:[],cumhaz:[],stdChaz:[],logse:!l||c===2,n:[],records:[],nMax:[],nStart:[],events:[],rmean:[],seRmean:[],median:[],medianRows:S,table:{colNames:[],rowNames:S,table:[]},warnings:[]};i!==void 0&&(q.counts={nRisk:[],nEvent:[],nCensor:[]});let $=[];for(let _=0;_<S.length;_++){let P=T.filter(pe=>pe.group===_);if(!P.some(pe=>pe.weight>0))throw new Error("a group has no positive weight");let K=q.time.length,X=P.length,re=Math.max(...P.map(pe=>pe.time)),z=0,J=0,Z=1,Se=0,ne=0,D=0;if(o===void 0){let pe=0;for(let oe of P)if(pe+=oe.weight,!Number.isSafeInteger(pe))throw new RangeError("total case weight per group must be a safe integer");z=pe,J=P.length}let Y=[],j=[],U=[],W=s===void 0?void 0:new Map;if(W)for(let pe of P){let oe=W.get(pe.id)??[];oe.push(pe),W.set(pe.id,oe)}for(let pe=0;pe<P.length;){let oe=P[pe].time,we=pe,ge=0,ce=0,tt=0,G=J;for(;we<P.length&&P[we].time===oe;)we++;let ue=P.slice(pe,we);if(o!==void 0){let je=P.filter(We=>We.start<oe&&We.time>=oe);if(z=je.reduce((We,xt)=>We+xt.weight,0),J=je.length,G=J,!Number.isSafeInteger(z))throw new RangeError("total case weight at risk must be a safe integer")}let Me=ue.filter(je=>je.event),Ne=ue.filter(je=>!je.event&&(A===void 0||!(A.get(je.id)??[]).some(We=>We.start===oe&&We.time>oe)));for(let je of Me)ge+=je.weight,ce++;for(let je of Ne)tt+=je.weight;let Ee=Me.length,ee=Ne.length;if(s!==void 0&&Me.length===0&&Ne.length===0){pe=we;continue}let ve=0,ie=0;if(ge>0){if(c===1)ve=ge/z,ie=ge/(z*z);else for(let je=0;je<ce;je++){let We=ge/ce,xt=z-je*We;ve+=We/xt,ie+=We/(xt*xt)}Se+=ve,D+=ie,a===1?(Z*=1-ge/z,ne+=ge<z?ge/(z*(z-ge)):1/0):Z=Math.exp(-Se),U.push({t:oe,d:ge,n:z,e:ce,S:Z})}let Te=Math.sqrt(a===1?ne:D),Be=0,$e=0;if(l){let je=0,We=0,xt=W===void 0?P:Array.from(W.values(),Rt=>Rt[0]);for(let Rt of xt){let Rn=W?.get(Rt.id)??[Rt],$t=0,kt=0;for(let ut of U){if(ut.t>oe)break;let Ht=Rn.some(Ft=>Ft.start<ut.t&&Ft.time>=ut.t),rn=Rn.some(Ft=>Ft.time===ut.t&&Ft.event),Qt=ut.d/ut.n;if(c===1)kt+=(Number(rn)-Number(Ht)*Qt)/ut.n;else{let Ft=0,Zt=0;for(let H=0;H<ut.e;H++){let se=ut.d/ut.e,Ie=ut.n-H*se;Ft+=(Ie+H*se)/(ut.e*Ie*Ie),Zt-=se/(Ie*Ie)}kt+=Ft*Number(rn)+Zt*Number(Ht)}a===1&&Qt<1&&($t-=Z*(Number(rn)-Number(Ht)*Qt)/((1-Qt)*ut.n))}je+=(Rt.weight*$t)**2,We+=(Rt.weight*kt)**2}Be=Math.sqrt(je),$e=Math.sqrt(We)}let ot=l?c===2?a===1?Be:$e:a===1?Z===0?NaN:Be/Z:$e:Te,mt=l?c===2?Z*(a===1?Be:$e):a===1?Be:Z*$e:Z*Te,Ct=NaN,Ut=NaN;if(w!=="none")if(w==="log")Ct=Z===0?mt===0?0:NaN:Z*Math.exp(-I*ot),Ut=Z===0?mt===0?0:NaN:Math.min(1,Z*Math.exp(I*ot));else if(w==="log-log")if(Z===0||Z===1)Ct=mt===0?Z:NaN,Ut=Ct;else{let je=Math.log(-Math.log(Z));Ct=Math.exp(-Math.exp(je-I*ot/Math.log(Z))),Ut=Math.exp(-Math.exp(je+I*ot/Math.log(Z)))}else if(w==="plain")Z===0&&mt===1/0?(Ct=NaN,Ut=NaN):(Ct=Math.max(0,Z-I*mt),Ut=Math.min(1,Z+I*mt));else if(w==="logit")if(Z===0||Z===1)Ct=mt===0?Z:NaN,Ut=Ct;else{let je=Math.log(Z/(1-Z)),We=I*mt/(Z*(1-Z));Ct=1-1/(1+Math.exp(je-We)),Ut=1-1/(1+Math.exp(je+We))}else if(Z>0&&Z<1){let je=Math.asin(Math.sqrt(Z)),We=I*mt/(2*Math.sqrt(Z*(1-Z)));Ct=Math.sin(Math.max(0,je-We))**2,Ut=Math.sin(Math.min(Math.PI/2,je+We))**2}else Ct=NaN,Ut=NaN;let Hn=z;q.strata.push(S[_]),q.time.push(oe),q.nRisk.push(Hn),q.nEvent.push(ge),q.nCensor.push(tt),q.surv.push(Z),q.cumhaz.push(Se),q.stdChaz.push(l?$e:Math.sqrt(D)),q.stdErr.push(l?c===2?Z*(a===1?Be:$e):a===1?Be:Z*$e:Z*Te),q.lower.push(Ct),q.upper.push(Ut),q.counts&&(q.counts.nRisk.push(G),q.counts.nEvent.push(Ee),q.counts.nCensor.push(ee)),o===void 0&&(z-=ge+tt,J-=we-pe),pe=we}let te=q.time.slice(K),fe=q.surv.slice(K),Re=q.lower.slice(K),Le=q.upper.slice(K);q.median.push([uc(te,fe),w==="none"?NaN:uc(te,Re),w==="none"?NaN:uc(te,Le)]),q.n.push(X),q.records.push(X);let be=q.nRisk.slice(K);q.nMax.push(Math.max(...be)),q.nStart.push(be[0]??0),q.events.push(P.reduce((pe,oe)=>pe+(oe.event?oe.weight:0),0));let me=f==="none"?NaN:f==="common"?Math.max(...C):f==="individual"?re:f;if(f!=="none"&&!Number.isFinite(me))throw new RangeError("'rmean' must be finite");let ke=P.filter(pe=>Number.isFinite(pe.time)).map(pe=>Math.abs(pe.time)),de=Number.isFinite(me)?Math.max(Math.abs(me),Math.abs(k),...ke):Math.max(Math.abs(k),...ke),Ce=de===0?0:Math.max(-1074,Math.min(1023,Math.floor(Math.log2(de)))),xe=de===0?1:2**Ce,Pe=Number.isFinite(me)&&!Number.isFinite(me-k),Fe=pe=>{let oe=1;for(let we of U)if(we.t<=pe)oe=we.S;else break;return oe},Ke=pe=>{let oe=1;for(let we of U)if(we.t/xe<=pe)oe=we.S;else break;return oe},ze=Dn();if(Pe){let pe=Array.from(new Set([k/xe,...U.map(oe=>oe.t/xe).filter(oe=>oe>k/xe&&oe<me/xe),me/xe])).sort((oe,we)=>oe-we);for(let oe=0;oe<pe.length-1;oe++){let we=pe[oe],ge=pe[oe+1];pn(ze,Ke(we)*(ge-we),pe.length+1)}}else{let pe=Array.from(new Set([k,...U.map(oe=>oe.t).filter(oe=>oe>k&&oe<me),me])).sort((oe,we)=>oe-we);for(let oe=0;oe<pe.length-1;oe++){let we=pe[oe],ge=pe[oe+1];pn(ze,Fe(we)*(ge-we),pe.length+1)}}let et=Pn(ze)*(Pe?xe:1),Ue=Dn();if(Number.isFinite(me))for(let pe of U){if(pe.t>me||pe.n<=pe.d)continue;let oe=Dn();if(Pe){let ge=Array.from(new Set([pe.t/xe,...U.map(ce=>ce.t/xe).filter(ce=>ce>pe.t/xe&&ce<me/xe),me/xe])).sort((ce,tt)=>ce-tt);for(let ce=0;ce<ge.length-1;ce++)pn(oe,Ke(ge[ce])*(ge[ce+1]-ge[ce]),ge.length+1)}else{let ge=Array.from(new Set([pe.t,...U.map(ce=>ce.t).filter(ce=>ce>pe.t&&ce<me),me])).sort((ce,tt)=>ce-tt);for(let ce=0;ce<ge.length-1;ce++)pn(oe,Fe(ge[ce])*((ge[ce+1]-ge[ce])/xe),ge.length+1)}let we=Pn(oe);pn(Ue,we*we*pe.d/(pe.n*(pe.n-pe.d)),U.length+1)}let Xe=Pn(Ue);q.rmean.push(f==="none"?NaN:et),q.seRmean.push(f==="none"?NaN:Math.sqrt(Xe)*xe),$.push(f==="none"?w==="none"?[X,q.nMax[_],q.nStart[_],q.events[_],q.median[_][0]]:[X,q.nMax[_],q.nStart[_],q.events[_],q.median[_][0],q.median[_][1],q.median[_][2]]:w==="none"?[X,q.nMax[_],q.nStart[_],q.events[_],et,Math.sqrt(Xe)*xe,q.median[_][0]]:[X,q.nMax[_],q.nStart[_],q.events[_],et,Math.sqrt(Xe)*xe,q.median[_][0],q.median[_][1],q.median[_][2]])}return q.table={colNames:[...f==="none"?["records","n.max","n.start","events","median"]:["records","n.max","n.start","events","rmean","se(rmean)","median"],...w==="none"?[]:[`${u}LCL`,`${u}UCL`]],rowNames:S,table:$},Q("survfit",q)}var T1=["tau","covariates","covariateNames","confLevel"];function Bo(e){return 2*ae(-Math.abs(e))}function Fu(e,t,n){return[e-n*t,e+n*t]}function Lu(e,t,n){return{estimate:e,se:t,confInt:Fu(e,t,n)}}function A1(e,t,n,r){return{estimate:e,confInt:Fu(e,t,n),pValue:Bo(r)}}function Cu(e,t){let n=fr(e.map(l=>l.time),e.map(l=>l.status),{rmean:t}),r=n.rmean[0],o=n.seRmean[0],s=0,i=1,c=Dn();for(let l=0;l<n.time.length&&n.time[l]<t;l++){let f=n.time[l];pn(c,(1-i)*(f-s),n.time.length+1),i=n.surv[l],s=f}pn(c,(1-i)*(t-s),n.time.length+1);let a=Pn(c);return{rmst:{estimate:r,se:o},rmtl:{estimate:a,se:o}}}function pc(e,t,n,r){if(n==="difference"){let l=e.estimate-t.estimate,f=Math.hypot(e.se,t.se);return A1(l,f,r,l/f)}let o=e.estimate,s=t.estimate;if(o===0||s===0)return{estimate:o===0?s===0?NaN:0:1/0,confInt:[NaN,NaN],pValue:NaN};if(!(o>0&&s>0))return{estimate:NaN,confInt:[NaN,NaN],pValue:NaN};let i=o/s,c=Math.log(i),a=Math.hypot(e.se/o,t.se/s);return{estimate:i,confInt:[Math.exp(c-r*a),Math.exp(c+r*a)],pValue:Bo(c/a)}}function S1(e){let t=new Array(e.length).fill(0);for(let n of[0,1]){let r=e.flatMap((s,i)=>s.arm===n?[i]:[]),o=fr(r.map(s=>e[s].time),r.map(s=>e[s].status===0?1:0),{rmean:"none"});for(let s of r){let i=e[s];if(i.status===0)continue;let c=1;for(let a=0;a<o.time.length&&o.time[a]<=i.time;a++)c=o.surv[a];if(!(c>0))throw new Error("the censoring survival probability is zero");t[s]=1/c}}return t}function dc(e,t,n,r,o,s){let i=e.map(L=>[L.arm,...L.x]),c=i[0].map((L,k)=>{let F=i.reduce((O,q)=>Math.max(O,Math.abs(q[k])),0);if(F===0)return 1;let I=Math.floor(Math.log2(F));return I>500||I<-500?2**I:1}),a=i.map(L=>L.map((k,F)=>k/c[F])),l=e.map((L,k)=>[1,...a[k]]),f=[1,...c],m=S1(e),u=r?Fa(a,n,{family:"poisson",weights:m}):Vo(a,n,{weights:m});if(r&&!u.converged)throw new Error("the adjusted RMST Poisson model did not converge");let p=u.coefficients.map((L,k)=>L/f[k]),w=u.fittedValues;if(p.some(L=>!Number.isFinite(L))||w.some(L=>!Number.isFinite(L)))throw new Error("the adjusted RMST model is singular or has non-finite fitted values");let d=l.map((L,k)=>L.map(F=>m[k]*F*(n[k]-w[k]))),b=d.map(L=>L.slice());for(let L of[0,1]){let k=e.flatMap((I,O)=>I.arm===L?[O]:[]),F=fr(k.map(I=>e[I].time),k.map(I=>e[I].status===0?1:0),{rmean:"none"});for(let I=0;I<F.time.length;I++){let O=F.time[I],q=F.nEvent[I];if(!(O<t)||q===0)continue;let $=F.nRisk[I];if(!($>0))continue;let _=new Array(l[0].length).fill(0);for(let K=0;K<_.length;K++){let X=Dn();for(let re of k)e[re].time>=O&&pn(X,d[re][K],k.length);_[K]=Pn(X)}let P=q/$;for(let K of k){let X=e[K],re=X.time===O&&X.status===0,z=X.time>=O;for(let J=0;J<_.length;J++)b[K][J]+=_[J]/$*(Number(re)-(z?P:0))}}}let y=l.map((L,k)=>L.map(F=>F*Math.sqrt(r?w[k]:1))),v=Va(y),M=Va(b),g=nt(v),x=$a($a(g,M),g).map((L,k)=>L.map((F,I)=>F/f[k]/f[I])),E=x.map((L,k)=>Math.sqrt(Math.max(0,L[k]))),R=p.map((L,k)=>L/E[k]),T=R.map(Bo),A=p.map((L,k)=>L-s*E[k]),S=p.map((L,k)=>L+s*E[k]),C=r?[A.map(Math.exp),S.map(Math.exp)]:[A,S];return{coef:p,cov:x,table:{names:["intercept","arm",...o],estimate:p,se:E,z:R,pValue:T,confInt:C}}}function q1(e,t,n,r={}){if(B(r,T1,"rmst2"),e.length!==t.length||e.length!==n.length)throw new RangeError("'time', 'status' and 'arm' must have the same length");if(r.tau!==void 0&&typeof r.tau!="number")throw new TypeError("'tau' must be a number");if(r.confLevel!==void 0&&typeof r.confLevel!="number")throw new TypeError("'confLevel' must be a number");if(r.covariateNames!==void 0&&!Array.isArray(r.covariateNames))throw new TypeError("'covariateNames' must be an array of strings");let o=r.confLevel??.95;if(!(o>0&&o<1))throw new RangeError("'confLevel' must be between 0 and 1");let s=e.reduce((z,J)=>Number.isFinite(J)?Math.max(z,Math.abs(J)):z,0),i=s>2**500?2**Math.floor(Math.log2(s)):1,a=e.every(z=>!Number.isFinite(z)||z===0||z/i!==0)&&(r.tau===void 0||r.tau===0||r.tau/i!==0)?i:1,l=z=>z/a,f=Array.from(t),m=new Set;for(let z=0;z<f.length;z++){let J=n[z]===!0?1:n[z]===!1?0:n[z];(J===0||J===1)&&f[z]===2&&m.add(J)}let u=r.covariates?.[0],h=r.covariates!==void 0&&typeof u=="number";if(r.covariates!==void 0&&r.covariates.length>0&&!h&&!Array.isArray(u))throw new TypeError("'covariates' must be a numeric vector or a matrix of numeric rows");let p=r.covariates===void 0?void 0:h?r.covariates.map(z=>[z]):r.covariates.map(z=>Array.from(z));if(p&&p.length!==e.length)throw new RangeError("'covariates' must have one row per observation");if(p&&p.some(z=>z.length!==p[0].length))throw new RangeError("'covariates' must have the same number of columns in every row");if(p&&p.some(z=>z.some(J=>typeof J!="number")))throw new TypeError("'covariates' must contain numeric values");if(p&&p.some(z=>z.some(J=>!Number.isFinite(J)&&!Number.isNaN(J))))throw new RangeError("'covariates' must be finite");let w=[],d=[],b=[],y=[];for(let z=0;z<e.length;z++){if(e[z]==null||Number.isNaN(e[z])||f[z]==null||Number.isNaN(f[z])||n[z]==null||Number.isNaN(n[z]))throw new Error("missing value where TRUE/FALSE needed");if(typeof e[z]!="number")throw new TypeError("'time' must contain numbers");if(!Number.isFinite(e[z]))throw new RangeError("'time' must be finite");if(typeof n[z]!="number"&&typeof n[z]!="boolean")throw new TypeError("'arm' must contain numbers or booleans");let J=n[z]===!0?1:n[z]===!1?0:n[z];if(J!==0&&J!==1){b.push(l(e[z]));continue}if(typeof f[z]!="number")throw new TypeError("'status' must contain numbers");let Z=f[z],Se=Z===1?1:0,ne=p?p[z]:[];if(ne.some(Y=>!Number.isFinite(Y)))continue;d.push({time:l(e[z]),status:Se,arm:J,x:ne,index:z});let D=Z;if(m.has(J))if(Z===2)D=1;else if(Z===1)D=0;else{y.includes("Invalid status value, converted to NA")||y.push("Invalid status value, converted to NA");continue}else if(D!==0&&D!==1){y.includes("Invalid status value, converted to NA")||y.push("Invalid status value, converted to NA");continue}w.push({time:l(e[z]),status:D,arm:J,x:ne,index:z})}if(d.some(z=>z.time<0))throw new Error("negative follow-up times are not supported");if(!w.some(z=>z.arm===0)||!w.some(z=>z.arm===1))throw new Error("both arms 0 and 1 are required");let v=[0,1].map(z=>Math.max(...d.filter(J=>J.arm===z).map(J=>J.time))),M=[0,1].map(z=>{let J=v[z];return d.some(Z=>Z.arm===z&&Z.time===J&&Z.status===0)}),g=M.some(Boolean)?Math.min(...v.filter((z,J)=>M[J])):Math.max(...v),N=r.tau===void 0,x=r.tau===void 0?g:l(r.tau),E=r.tau===void 0?x*a:r.tau;if(b.some(z=>z<=x))throw new RangeError("'arm' must contain only 0 and 1 through tau");if(!(x>0))throw new RangeError("'tau' must be greater than zero");if(x>g)throw new RangeError(`'tau' must be at most ${g*a}: the minimum of the largest times allowed by both arms`);if(x<Math.min(...d.map(z=>z.time)))throw new RangeError("'tau' must not be below the first observed time");let R=(1-o)/2,T=R<1e-8?le(R,{lowerTail:!1}):le(1-R),A=w.filter(z=>z.arm===1),S=w.filter(z=>z.arm===0),C=Cu(A,x),L=Cu(S,x),k=z=>({estimate:z.estimate*a,se:z.se*a}),F={rmst:k(C.rmst),rmtl:k(C.rmtl)},I={rmst:k(L.rmst),rmtl:k(L.rmtl)},O=Lu(F.rmst.estimate,F.rmst.se,T),q=Lu(I.rmst.estimate,I.rmst.se,T),$={estimate:F.rmtl.estimate,se:F.rmtl.se,confInt:[E-O.confInt[1],E-O.confInt[0]]},_={estimate:I.rmtl.estimate,se:I.rmtl.se,confInt:[E-q.confInt[1],E-q.confInt[0]]},P=pc(F.rmst,I.rmst,"difference",T),K=pc(F.rmst,I.rmst,"ratio",T),X=pc(F.rmtl,I.rmtl,"rmtlRatio",T),re={tau:E,tauDefault:N,confLevel:o,arm1:{rmst:O,rmtl:$},arm0:{rmst:q,rmtl:_},difference:P,ratio:K,rmtlRatio:X,warnings:y};if(p){let z=r.covariateNames===void 0?h?["covariates"]:Array.from({length:p[0].length},()=>""):Array.from(r.covariateNames);if(z.some(be=>typeof be!="string"))throw new TypeError("'covariateNames' must contain strings");if(z.length!==p[0].length)throw new RangeError("'covariateNames' must have one name per covariate column");let J=w.map(be=>({...be,time:Math.min(be.time,x),status:be.time>=x?1:be.status})),Z=J.map(be=>be.time),Se=J.map(be=>x-be.time),ne=dc(J,x,Z,!1,z,T),D=!(F.rmst.estimate>0&&I.rmst.estimate>0),Y=!(F.rmtl.estimate>0&&I.rmtl.estimate>0),j=D?void 0:dc(J,x,Z,!0,z,T),U=Y?void 0:dc(J,x,Se,!0,z,T),W=(be,me)=>{let ke=be.coef[1],de=Math.sqrt(Math.max(0,be.cov[1][1]));return me?{estimate:Math.exp(ke),confInt:[Math.exp(ke-T*de),Math.exp(ke+T*de)],pValue:Bo(ke/de)}:{estimate:ke*a,confInt:[(ke-T*de)*a,(ke+T*de)*a],pValue:Bo(ke/de)}},te=be=>({...be,estimate:be.estimate.map(me=>me*a),se:be.se.map(me=>me*a),confInt:[be.confInt[0].map(me=>me*a),be.confInt[1].map(me=>me*a)]}),fe=be=>({...be,estimate:be.estimate.map((me,ke)=>ke===0?me+Math.log(a):me),confInt:[be.confInt[0].map((me,ke)=>ke===0?me+Math.log(a):me),be.confInt[1].map((me,ke)=>ke===0?me+Math.log(a):me)]}),Re={names:["intercept","arm",...z],estimate:Array(ne.coef.length).fill(NaN),se:Array(ne.coef.length).fill(NaN),z:Array(ne.coef.length).fill(NaN),pValue:Array(ne.coef.length).fill(NaN),confInt:[Array(ne.coef.length).fill(NaN),Array(ne.coef.length).fill(NaN)]},Le={estimate:NaN,confInt:[NaN,NaN],pValue:NaN};re.adjusted={difference:W(ne,!1),ratio:D?Le:W(j,!0),rmtlRatio:Y?Le:W(U,!0)},re.coefficients={difference:te(ne.table),ratio:j?fe(j.table):Re,rmtlRatio:U?fe(U.table):Re},delete re.arm1,delete re.arm0}return Q("rmst2",re)}var k1=new Set(["start","id","ties","weights","strata","init","control","nocenter","confLevel","robust","cluster"]),I1=["eps","iterMax","tolerChol","tolerInf","timefix"],gc=e=>e==null||typeof e=="number"&&Number.isNaN(e),_1=(e,t)=>typeof e=="number"&&typeof t=="number"?e-t:String(e)<String(t)?-1:String(e)>String(t)?1:0,L1=2**64,C1=2**-1022,Ou=700;function sa(e,t,n){let{time:r,event:o,weight:s,x:i,stratumEnd:c,logUnit:a,start:l}=e,f=r.length,m=t.length,u=()=>new Array(m).fill(0),h=()=>Array.from({length:m},u),p=u(),w=h(),d=0,b=0,y=u(),v=h(),M=u(),g=h(),N=0,x=0,E=R=>{let T=Math.exp(x-R);b*=T,N*=T;for(let A=0;A<m;A++){y[A]*=T,M[A]*=T;let S=v[A],C=g[A];for(let L=0;L<=A;L++)S[L]*=T,C[L]*=T}x=R};for(let R=f-1;R>=0;){c[R]&&(x=0,b=0,y=u(),v=h());let T=r[R],A=!1;if(l!==void 0){let k=R;for(;k>0&&r[k-1]===T&&!c[k-1];)k--;for(let F=k;F<=R;F++)o[F]&&(A=!0)}if(l!==void 0&&A){let k=R;for(;k>0&&!c[k-1];)k--;let F=R;for(;!c[F];)F++;let I=q=>{let $=0;for(let _=0;_<m;_++)$+=t[_]*i[q][_];return $},O=-1/0;for(let q=k;q<=F;q++)l[q]<T&&r[q]>=T&&(O=Math.max(O,I(q)));x=Number.isFinite(O)?O:0,b=0,y=u(),v=h();for(let q=k;q<=F;q++){if(!(l[q]<T&&r[q]>=T)||r[q]===T&&o[q])continue;let $=Math.exp(I(q)-x)*s[q];b+=$;for(let _=0;_<m;_++){y[_]+=$*i[q][_];for(let P=0;P<=_;P++)v[_][P]+=$*i[q][_]*i[q][P]}}}let S=0,C=0;N=0;let L=R;do{let k=i[L],F=0;for(let q=0;q<m;q++)F+=t[q]*k[q];let I=s[L];Number.isFinite(F)&&(F-x>Ou?E(F):F-x<-Ou&&b===0&&N===0&&(x=F));let O=Math.exp(F-x)*I;if(o[L]){S++,C+=I,N+=O,d+=I*F;for(let q=0;q<m;q++){p[q]+=I*k[q],M[q]+=O*k[q];let $=g[q];for(let _=0;_<=q;_++)$[_]+=O*k[q]*k[_]}}else if(l===void 0){b+=O;for(let q=0;q<m;q++){y[q]+=O*k[q];let $=v[q];for(let _=0;_<=q;_++)$[_]+=O*k[q]*k[_]}}L--}while(L>=0&&r[L]===T&&!c[L]);if(S>0){let k=n?S:1,F=n?S:1,I=C/k;for(let O=0;O<k;O++){b+=n?N/F:N,d-=I*(Math.log(b)+x+a);for(let q=0;q<m;q++){y[q]+=n?M[q]/F:M[q];let $=y[q]/b;p[q]-=I*$;let _=v[q],P=g[q],K=w[q];for(let X=0;X<=q;X++){_[X]+=n?P[X]/F:P[X];let re=_[X]-$*y[X],z=I*re;K[X]+=z<1/0&&z>-1/0&&(Math.abs(z)>=C1||re===0)||!Number.isFinite(I)?z/b:I*(re/b)}}}M.fill(0);for(let O of g)O.fill(0)}R=L}for(let R=0;R<m;R++)for(let T=0;T<R;T++)w[T][R]=w[R][T];return{loglik:d,score:p,info:w}}function Du(e,t,n){let{time:r,event:o,x:s,stratumEnd:i}=e,c=qe([e.weight]).groups[0],a=r.length,l=t.length,f=Array.from({length:a},()=>new Array(l).fill(0)),m=u=>{let h=0;for(let p=0;p<l;p++)h+=t[p]*s[u][p];return h};for(let u=0;u<a;){let h=u;for(;!i[h];)h++;let p=[];for(let R=u;R<=h;R++)p.push(m(R));let w=[];for(let R=u;R<=h;R++)(R===u||r[R]!==r[R-1])&&w.push(R);let d=R=>R+1<w.length?w[R+1]:h+1,b=new Array(w.length),y=new Array(w.length),v=new Array(w.length),M=-1/0,g=0,N=new Array(l).fill(0);for(let R=w.length-1;R>=0;R--){let T=Number.isFinite(M)?M:0;for(let S=w[R];S<d(R);S++)M=Math.max(M,p[S-u]);let A=Number.isFinite(M)?M:0;if(R<w.length-1&&A!==T){let S=Math.exp(T-A);g*=S;for(let C=0;C<l;C++)N[C]*=S}for(let S=w[R];S<d(R);S++){let C=c[S]*Math.exp(p[S-u]-A);g+=C;for(let L=0;L<l;L++)N[L]+=C*s[S][L]}b[R]=g,y[R]=N.slice(),v[R]=A}let x=0,E=new Array(l).fill(0);for(let R=0;R<w.length;R++){let T=v[R];if(R>0&&T!==v[R-1]){let q=Math.exp(T-v[R-1]);x*=q;for(let $=0;$<l;$++)E[$]*=q}let A=w[R],S=d(R),C=[];for(let q=A;q<S;q++)C.push(Math.exp(p[q-u]-T));let L=0,k=0,F=0,I=new Array(l).fill(0);for(let q=A;q<S;q++){if(!o[q])continue;let $=c[q]*C[q-A];L++,k+=c[q],F+=$;for(let _=0;_<l;_++)I[_]+=$*s[q][_]}for(let q=A;q<S;q++){let $=C[q-A],_=s[q],P=f[q];for(let K=0;K<l;K++)P[K]=-$*(_[K]*x-E[K])}if(L===0)continue;let O=n?L:1;for(let q=0;q<O;q++){let $=q/O,_=b[R]-$*F,P=y[R].map((X,re)=>(X-$*I[re])/_),K=k/O/_;for(let X=A;X<S;X++){let re=C[X-A],z=s[X],J=f[X];if(o[X])for(let Z=0;Z<l;Z++){let Se=z[Z]-P[Z];J[Z]+=Se/O-(1-$)*re*Se*K}else for(let Z=0;Z<l;Z++)J[Z]-=re*(z[Z]-P[Z])*K}x+=K;for(let X=0;X<l;X++)E[X]+=K*P[X]}}u=h+1}return f}function wc(e,t,n){let r=e.start,{time:o,event:s,x:i,stratumEnd:c}=e,a=qe([e.weight]).groups[0],l=o.length,f=t.length,m=Array.from({length:l},()=>new Array(f).fill(0)),u=o.map((h,p)=>{let w=0;for(let d=0;d<f;d++)w+=t[d]*i[p][d];return w});for(let h=0;h<l;){let p=h;for(;!c[p];)p++;for(let w=h;w<=p;){let d=o[w],b=w;for(;b<p&&o[b+1]===d;)b++;let y=[],v=[],M=-1/0;for(let g=w;g<=b;g++)s[g]&&v.push(g);if(v.length>0)for(let g=h;g<=p;g++)r[g]<d&&o[g]>=d&&(y.push(g),M=Math.max(M,u[g]));if(v.length>0&&y.length>0){Number.isFinite(M)||(M=0);let g=0,N=new Array(f).fill(0),x=0,E=new Array(f).fill(0),R=0;for(let A of y){let S=Math.exp(u[A]-M),C=a[A]*S;g+=C;for(let L=0;L<f;L++)N[L]+=C*i[A][L];if(s[A]&&o[A]===d){R+=a[A],x+=C;for(let L=0;L<f;L++)E[L]+=C*i[A][L]}}let T=n?v.length:1;for(let A=0;A<T;A++){let S=A/T,C=g-S*x,L=R/T/C,k=N.map((F,I)=>(F-S*E[I])/C);for(let F of y){let I=Math.exp(u[F]-M),O=s[F]&&o[F]===d;for(let q=0;q<f;q++){let $=i[F][q]-k[q];O?m[F][q]+=$/T-(1-S)*I*$*L:m[F][q]-=I*$*L}}}}w=b+1}h=p+1}return m}function Ko(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 aa(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 ia(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 Uo=(e,t)=>({statistic:e,df:t,pValue:It(e,t,{lowerTail:!1})});function F1(e,t,n){let r=e.length,o=()=>new Array(t).fill(NaN),s=Uo(0,0),i=new Array(t).fill(0),c=o(),a=o();return{coefficients:o(),var:Array.from({length:t},()=>new Array(t).fill(0)),loglik:[0,0],score:0,wald:0,iter:0,means:Array.from({length:t},(l,f)=>it(e.map(m=>m[f]))),n:r,nevent:0,linearPredictors:new Array(r).fill(0),se:i,z:c,p:a,stdError:i,zValue:c,pValue:a,lower:o(),upper:o(),logtest:s,sctest:{...s},waldtest:{...s},rsq:{rsq:0,maxrsq:0},usedRobust:!1,warnings:n}}function O1(e,t,n,r={}){return B(r,k1,"coxph"),B(Cn(r,"control"),I1,"coxph","control"),Q("coxph",D1(e,t,n,r).result)}function D1(e,t,n,r={},o=!1){let{ties:s="efron",weights:i,strata:c,init:a,control:l={},nocenter:f=[-1,0,1],confLevel:m=.95,robust:u,cluster:h,start:p,id:w}=r,{eps:d=1e-9,iterMax:b=20,tolerChol:y=2**-39}=l,v=l.tolerInf??Math.sqrt(d);if(l.timefix!==void 0&&typeof l.timefix!="boolean")throw new TypeError("'timefix' must be a boolean");let M=l.timefix??!0;if(s!=="efron"&&s!=="breslow")throw new RangeError(`invalid 'ties': ${String(s)}`);if(!(d>0&&d<1/0))throw new RangeError("'eps' must be a positive finite number");if(!(y>0&&y<1/0))throw new RangeError("'tolerChol' must be a positive finite number");if(!(v>0&&v<1/0))throw new RangeError("'tolerInf' must be a positive finite number");if(!(Number.isInteger(b)&&b>=0))throw new RangeError("'iterMax' must be a non-negative integer");if(!(m>0&&m<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(i!==void 0&&i.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'");if(h!==void 0&&h.length!==g)throw new RangeError("'cluster' must have the same length as 'time'");if(p!==void 0&&p.length!==g)throw new RangeError("'start' must have the same length as 'time'");if(w!==void 0&&w.length!==g)throw new RangeError("'id' must have the same length as 'time'");if(u!==void 0&&typeof u!="boolean")throw new TypeError("'robust' must be true or false");let N=g===0?0:n[0].length;if(N<1)throw new RangeError("'x' must have at least one column");if(n.some(H=>H.length!==N))throw new RangeError("the rows of 'x' must have the same length");if(a!==void 0&&a.length!==N)throw new RangeError("'init' must have one value per column of 'x'");if(a?.some(H=>!Number.isFinite(H)))throw new RangeError("'init' must contain finite values");let x=new Et,E=[];for(let H=0;H<g;H++){if(Number.isNaN(e[H])||Number.isNaN(t[H])||n[H].some(Ie=>Number.isNaN(Ie))||p!==void 0&&Number.isNaN(p[H])||i!==void 0&&Number.isNaN(i[H])||c!==void 0&&gc(c[H])||h!==void 0&&gc(h[H])||w!==void 0&&gc(w[H]))continue;if(p!==void 0&&!(p[H]<e[H]))throw new RangeError("stop time must be > start time");let se=t[H];if(se!==0&&se!==1){if(p===void 0)throw new RangeError("'status' must be 0 (censored) or 1 (event)");x.add("Invalid status value, converted to NA");continue}if(e[H]===-1/0||e[H]===1/0&&se===1)throw new RangeError("event times must be finite");E.push(H)}let R=E.length;if(R===0)throw new RangeError(p===void 0?"no observations without missing values":"No (non-missing) observations");if(E.some(H=>n[H].some(se=>!Number.isFinite(se))))throw new RangeError("'x' contains infinite values");let T=E.map(H=>i===void 0?1:i[H]);if(T.some(H=>!(H>0)))throw new RangeError("Invalid weights, must be >0");if(T.some(H=>H===1/0))throw new RangeError("'weights' must be finite");let A=w===void 0?void 0:St(w),S=new Map;if(A!==void 0)for(let H of E){if(t[H]!==1)continue;let se=A[H];S.set(se,(S.get(se)??0)+1)}let C=p!==void 0&&[...S.values()].some(H=>H>1),L=u??(h!==void 0||T.some(H=>!Number.isInteger(H))||C);if(p!==void 0&&u===!0&&h===void 0&&w===void 0)throw new RangeError("one of cluster or id is needed");h!==void 0&&!L&&x.add("cluster specified with robust=FALSE, cluster ignored");let k=0;for(let H of E)k+=t[H];let F=E.map(H=>e[H]),I=p===void 0?void 0:E.map(H=>p[H]),O,q;if(I===void 0)O=M?Gr(F):F;else if(!M)O=F,q=I;else{let H=Gr([...I,...F]);if(q=H.slice(0,R),O=H.slice(R),q.some((se,Ie)=>se>=O[Ie]))throw new Error("aeqSurv exception, an interval has effective length 0")}if(k===0){let H=F1(E.map(se=>n[se]),N,x.list);return A!==void 0&&(H.nId=new Set(E.map(se=>A[se])).size),{result:H}}let $=new Array(R).fill(0),_,P=[];if(c!==void 0){let H=St(c);P=Array.from(new Set(E.map(Ie=>H[Ie]))).sort(_1);let se=new Map;P.forEach((Ie,Ge)=>se.set(Ie,Ge)),_=E.map(Ie=>H[Ie]),$=E.map(Ie=>se.get(H[Ie]))}let K=Array.from({length:R},(H,se)=>se).sort((H,se)=>$[H]-$[se]||O[H]-O[se]||H-se),X=K.map(H=>T[H]),{groups:re,k:z}=Nt(X)>=L1?qe([X]):{groups:[X],k:0},J=re[0],Z=0;for(let H of T)Z+=z===0?H:V(H,-z);let Se=K.map(H=>n[E[H]].slice()),ne=new Array(N).fill(0),D=new Array(N).fill(1),Y=new Array(N).fill(0),j=new Set(f);for(let H=0;H<N;H++){if(Se.every(He=>j.has(He[H])))continue;let se=Se.map(He=>He[H]),Ie=He=>{let zt=0;for(let Ln=0;Ln<R;Ln++)zt+=(p===void 0?J[Ln]:1)*He[Ln];let _n=p===void 0?Z:R,dr=zt/_n,Or=0,xa=He.map((Ln,ns)=>(Or+=(p===void 0?J[ns]:1)*Math.abs(Ln-dr),Ln-dr)),Dr=Or===0?1:_n/Or;return{m:dr,s:Dr,d:xa,ok:Number.isFinite(dr)&&Dr>=2**-1022&&Dr<1/0}},Ge=Ie(se);if(!Ge.ok){let{groups:He,k:zt}=qe([se]);Ge=Ie(He[0]),Y[H]=zt}for(let He=0;He<R;He++)Se[He][H]=Ge.d[He]*Ge.s;ne[H]=V(Ge.m,Y[H]),D[H]=Ge.s}let U={time:K.map(H=>O[H]),...q===void 0?{}:{start:K.map(H=>q[H])},event:K.map(H=>t[E[H]]===1),weight:J,x:Se,stratumEnd:K.map((H,se)=>se===R-1||$[K[se+1]]!==$[H]),logUnit:z*Math.LN2},W=s==="efron",te=a===void 0?new Array(N).fill(0):a.map((H,se)=>V(H/D[se],Y[se])),fe=sa(U,te,W),Re=[fe.loglik,fe.loglik],Le=fe.info.map(H=>H.slice()),be=Ko(Le,y),me=aa(Le,fe.score),ke=0;for(let H=0;H<N;H++)ke+=fe.score[H]*me[H];let de=0,Ce,xe=!1,Pe=b===0;if(b===0||!Number.isFinite(fe.loglik))Ce=ia(Le);else{let H=fe.loglik,se=te.map((Ge,He)=>Ge+me[He]),Ie=0;for(Pe=!1,de=1;de<=b;de++){fe=sa(U,se,W),Le=fe.info.map(He=>He.slice()),be=Ko(Le,y);let Ge=Number.isFinite(fe.loglik)&&fe.score.every(Number.isFinite)&&fe.info.every(He=>He.every(Number.isFinite));if(Ge&&(fe.loglik===H||Math.abs(1-H/fe.loglik)<=d)){Pe=!0,Re[1]=fe.loglik,te=se;break}!Ge||fe.loglik<H?(Ie++,se=se.map((He,zt)=>(He+Ie*te[zt])/(Ie+1))):(Ie=0,H=fe.loglik,Re[1]=H,me=aa(Le,fe.score),te=se,se=se.map((He,zt)=>He+me[zt]))}Pe?Ce=ia(Le):(de=p===void 0?b+1:b,xe=!0,p!==void 0?(fe=sa(U,te,W),Re[1]=fe.loglik,Le=fe.info.map(Ge=>Ge.slice()),be=Ko(Le,y),Ce=ia(Le)):b>1?(fe=sa(U,te,W),Re[1]=fe.loglik,Ce=fe.info.map((Ge,He)=>Ge.map((zt,_n)=>He===_n?Ge[He]>0?1/Ge[He]:0:-0))):Ce=ia(Le))}let Fe=D.map(H=>Math.floor(Math.log2(H))),Ke=D.map((H,se)=>V(H,-Fe[se])),ze=Fe.map((H,se)=>H-Y[se]),et=te.map((H,se)=>H*Ke[se]),Ue=fe.score.map((H,se)=>H/Ke[se]),Xe=Ce.map((H,se)=>H.map((Ie,Ge)=>Ie*Ke[se]*Ke[Ge])),pe=et.map((H,se)=>V(H,ze[se])),oe=Xe.map((H,se)=>H.map((Ie,Ge)=>V(Ie,ze[se]+ze[Ge]-z))),we=pe.slice();if(b>0&&Math.abs(be)<N)for(let H=0;H<N;H++)Ce[H][H]===0&&(we[H]=NaN);let ge=p===void 0?pe:pe.map((H,se)=>Number.isNaN(we[se])?0:H),ce=0;for(let H=0;H<N;H++)ce+=ge[H]*ne[H];let tt=E.map(H=>{let se=0;for(let Ie=0;Ie<N;Ie++)se+=n[H][Ie]*ge[Ie];return se-ce});if(b>1){let H=Xe.map((se,Ie)=>{let Ge=0;for(let He=0;He<N;He++)Ge+=Ue[He]*Xe[He][Ie];return Math.abs(Ge)});if(xe)(p===void 0||b>1)&&x.add("Ran out of iterations and did not converge"),p===void 0&&(Nt(tt)>500||H.some(se=>!Number.isFinite(se)))&&x.add("one or more coefficients may be infinite");else if(p===void 0){let se=[];for(let Ie=0;Ie<N;Ie++){let Ge=H[Ie];(!Number.isFinite(Ue[Ie])||V(Ge,ze[Ie])>d&&Ge>v*Math.abs(et[Ie]))&&se.push(Ie+1)}se.length>0&&x.add("Loglik converged before variable "+se.join(",")+" ; coefficient may be infinite. ")}}let G=we.map((H,se)=>Number.isNaN(H)?-1:se).filter(H=>H>=0),ue=G.length,Me,Ne=0,Ee=0,ee;if(L&&ue>0){let H=K.map((ct,Qe)=>Qe),se=R,Ie=h??(p===void 0?void 0:w);if(Ie!==void 0){let ct=St(Ie),Qe=new Map;H=K.map(pt=>{let yt=ct[E[pt]],Mt=Qe.get(yt);return Mt===void 0&&(Mt=Qe.size,Qe.set(yt,Mt)),Mt}),se=Qe.size}if(se<2)throw new RangeError(p===void 0?"at least two clusters are needed for the robust variance":"(subscript) logical subscript too long");let Ge=ct=>ct.map(Qe=>Qe.map((pt,yt)=>pt/Ke[yt])),He=Ge(p===void 0?Du(U,te,W):wc(U,te,W)),zt=He.map(ct=>Xe.map((Qe,pt)=>{let yt=0;for(let Mt=0;Mt<N;Mt++)yt+=ct[Mt]*Xe[Mt][pt];return yt})),_n;if(z===0)_n=zt.map((ct,Qe)=>ct.map(pt=>J[Qe]*pt));else{let ct=pt=>pt===0||!Number.isFinite(pt)?0:Math.floor(Math.log2(Math.abs(pt))),Qe=-1/0;zt.forEach((pt,yt)=>pt.forEach(Mt=>Mt!==0&&Number.isFinite(Mt)&&(Qe=Math.max(Qe,ct(J[yt])+ct(Mt))))),Ne=Qe===-1/0?0:-Qe,_n=zt.map((pt,yt)=>{let Mt=ct(J[yt]);return pt.map(gr=>{let Xc=ct(gr);return V(V(J[yt],-Mt)*V(gr,-Xc),Mt+Xc+Ne)})})}let dr=ct=>{let Qe=Array.from({length:se},()=>new Array(N).fill(0));return ct.forEach((pt,yt)=>{let Mt=Qe[H[yt]];for(let gr=0;gr<N;gr++)Mt[gr]+=pt[gr]}),Qe},Or=ct=>{let Qe=Array.from({length:N},()=>new Array(N).fill(0));for(let pt of ct)for(let yt=0;yt<N;yt++)for(let Mt=0;Mt<N;Mt++)Qe[yt][Mt]+=pt[yt]*pt[Mt];return Qe};Me=Or(dr(_n));let xa=qe([X]).groups[0],Dr=a===void 0?new Array(N).fill(0):a.map((ct,Qe)=>V(ct/D[Qe],Y[Qe])),Ln=p===void 0?Du(U,Dr,W):wc(U,Dr,W),ns=dr(Ge(Ln).map((ct,Qe)=>ct.map(pt=>xa[Qe]*pt))),po=new Array(N).fill(0);for(let ct of ns)for(let Qe=0;Qe<N;Qe++)po[Qe]+=ct[Qe];let Ra=Or(ns);if(N===1)Ee=po[0]*po[0]/Ra[0][0];else{Ko(Ra,y);let ct=aa(Ra,po);for(let Qe=0;Qe<N;Qe++)Ee+=po[Qe]*ct[Qe]}let Yc=new Array(R),Gc=new Array(R);K.forEach((ct,Qe)=>{Yc[ct]=He[Qe].map((pt,yt)=>V(pt,-ze[yt])),Gc[ct]=_n[Qe].map((pt,yt)=>V(pt,ze[yt]-Ne))}),ee={scoreResiduals:Yc,dfbeta:Gc}}let ve=Me!==void 0,ie=Me??Xe,Te=Me===void 0?-z:-2*Ne,Be=G.map(H=>et[H]-(a===void 0?0:V(a[H],-ze[H]))),$e=0;if(ue===1)$e=Be[0]*Be[0]/ie[G[0]][G[0]];else if(ue>1){let H=G.map(Ie=>G.map(Ge=>ie[Ie][Ge]));Ko(H,y);let se=aa(H,Be);for(let Ie=0;Ie<ue;Ie++)$e+=Be[Ie]*se[Ie]}$e=V($e,-Te);let ot=(1-m)/2,mt=ot<1e-8?le(ot,{lowerTail:!1}):le((1+m)/2),Ct=(H,se)=>{let Ie=Math.floor(se/2),Ge=H.map((He,zt)=>Math.sqrt(V(He[zt],se-2*Ie)));return{unit:Ge,h:Ie,se:Ge.map((He,zt)=>V(He,ze[zt]+Ie))}},Ut=Ct(Xe,-z).se,{unit:Hn,h:je,se:We}=Ct(ie,Te),xt=we.map((H,se)=>V((Number.isNaN(H)?H:et[se])/Hn[se],-je)),Rt=xt.map(H=>It(H*H,1,{lowerTail:!1})),Rn=we.map((H,se)=>Math.exp(H-mt*We[se])),$t=we.map((H,se)=>Math.exp(H+mt*We[se])),kt=V(-2*(Re[0]-Re[1]),z),ut=[V(Re[0],z),V(Re[1],z)],Ht=V(ke,z),rn=Me?.map((H,se)=>H.map((Ie,Ge)=>V(Ie,ze[se]+ze[Ge]-2*Ne))),Qt={coefficients:we,var:rn??oe,loglik:ut,score:Ht,wald:$e,iter:de,means:ne,n:R,nevent:k,...A===void 0?{}:{nId:new Set(E.map(H=>A[H])).size},linearPredictors:tt,se:Ut,z:xt,p:Rt,stdError:Ut,zValue:xt,pValue:Rt,lower:Rn,upper:$t,logtest:Uo(kt,ue),sctest:Uo(Ht,ue),waldtest:Uo($e,ue),rsq:{rsq:1-Math.exp(-kt/R),maxrsq:1-Math.exp(2*ut[0]/R)},usedRobust:ve,...ve?{naiveVar:oe,robustSe:We,rscore:Ee,robscore:Uo(Ee,ue)}:{},warnings:x.list},Ft=fe.score.map((H,se)=>V(H/D[se],Y[se]+z));Object.defineProperty(Qt,"coxZphData",{enumerable:!1,value:{time:O.slice(),...q===void 0?{}:{start:q.slice()},status:E.map(H=>t[H]===1),weights:T.slice(),..._===void 0?{}:{strata:_.slice()},x:E.map(H=>n[H].slice()),sortedToInput:K.slice(),sortedTime:U.time.slice(),...U.start===void 0?{}:{sortedStart:U.start.slice()},sortedEvent:U.event.slice(),sortedWeight:U.weight.slice(),sortedX:U.x.map(H=>H.slice()),stratumIndex:K.map(H=>$[H]),stratumEnd:U.stratumEnd.slice(),betaScaled:te.slice(),inverseCovariateScale:Ke.map((H,se)=>V(1/H,-ze[se])),weightExp:z,ties:s}});let Zt=ee?.scoreResiduals;if(p!==void 0&&o&&Zt===void 0){let H=wc(U,te,W);Zt=new Array(R),K.forEach((se,Ie)=>{Zt[se]=H[Ie].map((Ge,He)=>V(Ge/Ke[He],-ze[He]))})}return{result:Qt,...ee,...Zt===void 0?{}:{scoreResiduals:Zt},first:Ft,converged:Pe}}var Pu=["transform","terms","collapseTerms","global"],Kn=e=>new Array(e).fill(0),mr=e=>Array.from({length:e},()=>Kn(e));function P1(e){let t=Array.from({length:e.length},(r,o)=>o).sort((r,o)=>e[r]-e[o]||r-o),n=new Array(e.length);for(let r=0;r<t.length;){let o=r+1;for(;o<t.length&&e[t[o]]===e[t[r]];)o++;let s=(r+1+o)/2;for(let i=r;i<o;i++)n[t[i]]=s;r=o}return n}function Vu(e,t){let n=0,r=e.length;for(;n<r;){let o=Math.floor((n+r)/2);e[o]<t?n=o+1:r=o}return n}function V1(e){let t=e.time.length,n=new Array(t),r=new Map;for(let a=0;a<t;a++){let l=e.time[a],f=r.get(l)??{rows:[],deaths:0};f.rows.push(a),e.status[a]&&f.deaths++,r.set(l,f)}let o=[...r.keys()].sort((a,l)=>a-l),s=e.time.slice().sort((a,l)=>a-l),i=(e.start??new Array(t).fill(-1/0)).slice().sort((a,l)=>a-l),c=1;for(let a of o){let l=r.get(a);for(let m of l.rows)n[m]=1-c;let f=Vu(i,a)-Vu(s,a);l.deaths>0&&(c*=1-l.deaths/f)}return n}function $1(e,t,n,r){let o;if(typeof t=="function"){let s=t(e.time.slice());if(!Array.isArray(s)||s.length!==e.time.length)throw new RangeError("transform function must return one value per observation");o=Array.from(s)}else if(t==="km")o=V1(e);else if(t==="rank")o=P1(e.time);else if(t==="identity")o=e.time.slice();else if(t==="log")e.time.some(s=>s<0)&&r.push("NaNs produced"),o=e.time.map(Math.log);else throw new RangeError("Unrecognized transform");for(let s of n)if(!Number.isFinite(o[s]))throw new RangeError("transform must be finite at every event time");return o}function z1(e,t={}){B(t,Pu,"coxZph"),B(t,Pu,"coxZph");let n=t.transform??"km",r=t.global??!0,o=t.collapseTerms??!0;if(typeof r!="boolean")throw new TypeError("'global' must be a boolean");if(typeof o!="boolean")throw new TypeError("'collapseTerms' must be a boolean");if(t.terms!==void 0&&t.terms!==!1&&!Array.isArray(t.terms))throw new TypeError("'terms' must be an array of column groups or false");if(e.nevent===0)throw new Error("the proportional-hazards test requires events");let s=e.coxZphData;if(s===void 0)throw new TypeError("coxZph requires a CoxphResult returned by coxph");let i=e.coefficients.map((D,Y)=>Number.isNaN(D)?-1:Y).filter(D=>D>=0),c=i.length;if(c===0)throw new Error("the proportional-hazards test has no estimable coefficients");let a=[];for(let D=0;D<s.sortedTime.length;){let Y=D;for(;!s.stratumEnd[Y];)Y++;for(let j=D;j<=Y;){let U=s.sortedTime[j],W=j;for(;W<Y&&s.sortedTime[W+1]===U;)W++;let te=[];for(let fe=j;fe<=W;fe++)s.sortedEvent[fe]&&te.push(fe);s.start!==void 0&&te.reverse();for(let fe of te)a.push(s.sortedToInput[fe]);j=W+1}D=Y+1}if(a.length===0)throw new Error("the proportional-hazards test requires events");let l=a.map(D=>s.time[D]);if(new Set(l).size<2)throw new Error("the test needs events at two or more distinct times");let f=[...e.warnings],m=$1(s,n,a,f),u=m[a[0]];if(a.every(D=>m[D]===u))throw new Error("the transform is constant at the event times");let h=a.reduce((D,Y)=>Math.max(D,Math.abs(m[Y])),0),p=h===0?0:Math.floor(Math.log2(h)),w=m.map(D=>Number.isFinite(D)?V(D,-p):D),d=a.reduce((D,Y)=>D+w[Y],0)/a.length,b=w.map(D=>D-d);if(a.every(D=>b[D]===0))throw new Error("the transform is constant at the event times");let y=i.map(D=>s.betaScaled[D]),v=mr(c),M=mr(c),g=mr(c),N=Kn(c),x=[];for(let D=0;D<s.sortedTime.length;){let Y=D;for(;!s.stratumEnd[Y];)Y++;for(let j=D;j<=Y;){let U=s.sortedTime[j],W=j;for(;W<Y&&s.sortedTime[W+1]===U;)W++;let te=[];for(let fe=j;fe<=W;fe++)s.sortedEvent[fe]&&te.push(fe);if(te.length>0){let fe=[],Re=-1/0;for(let oe=D;oe<=Y;oe++){if(!((s.sortedStart===void 0||s.sortedStart[oe]<U)&&s.sortedTime[oe]>=U))continue;fe.push(oe);let ge=0;for(let ce=0;ce<c;ce++)ge+=y[ce]*s.sortedX[oe][i[ce]];Re=Math.max(Re,ge)}if(fe.length===0||!Number.isFinite(Re))throw new Error("empty Cox proportional-hazards risk set");let Le=Kn(c),be=mr(c),me=Kn(c),ke=mr(c),de=0,Ce=0;for(let oe of fe){let we=0;for(let ce=0;ce<c;ce++)we+=y[ce]*s.sortedX[oe][i[ce]];let ge=s.sortedWeight[oe]*Math.exp(we-Re);de+=ge;for(let ce=0;ce<c;ce++){let tt=s.sortedX[oe][i[ce]];Le[ce]+=ge*tt;for(let G=0;G<c;G++)be[ce][G]+=ge*tt*s.sortedX[oe][i[G]]}if(s.sortedEvent[oe]&&s.sortedTime[oe]===U){Ce+=ge;for(let ce=0;ce<c;ce++){let tt=s.sortedX[oe][i[ce]];me[ce]+=ge*tt;for(let G=0;G<c;G++)ke[ce][G]+=ge*tt*s.sortedX[oe][i[G]]}}}let xe=0,Pe=Kn(c);for(let oe of te){let we=s.sortedWeight[oe];xe+=we;for(let ge=0;ge<c;ge++)Pe[ge]+=we*s.sortedX[oe][i[ge]]}let Fe=te.length,Ke=s.ties==="efron"&&Fe>1?Fe:1,ze=Kn(c),et=mr(c);for(let oe=0;oe<Ke;oe++){let we=oe/Ke,ge=de-we*Ce,ce=Le.map((G,ue)=>(G-we*me[ue])/ge);for(let G=0;G<c;G++)ze[G]+=ce[G]/Ke;let tt=xe/Ke;for(let G=0;G<c;G++)for(let ue=0;ue<c;ue++)et[G][ue]+=tt*((be[G][ue]-we*ke[G][ue])/ge-ce[G]*ce[ue])}let Ue=Pe.map((oe,we)=>oe-xe*ze[we]),Xe=s.sortedToInput[te[0]],pe=b[Xe];for(let oe=0;oe<c;oe++){N[oe]+=pe*Ue[oe];for(let we=0;we<c;we++)v[oe][we]+=et[oe][we],M[oe][we]+=pe*et[oe][we],g[oe][we]+=pe*pe*et[oe][we]}s.start!==void 0&&te.reverse();for(let oe of te){let we=s.sortedToInput[oe],ge=i.map(ce=>s.sortedX[oe][ce]);for(let ce=0;ce<c;ce++)ge[ce]-=ze[ce];x.push({input:we,time:U,stratum:s.stratumIndex[oe],g:m[we],residual:ge})}}j=W+1}D=Y+1}let E;try{E=nt(v,M)}catch{throw new Error("the proportional-hazards information is singular")}let R=mr(c);for(let D=0;D<c;D++)for(let Y=0;Y<c;Y++){let j=0;for(let U=0;U<c;U++)j+=M[U][D]*E[U][Y];R[D][Y]=g[D][Y]-j}for(let D=0;D<c;D++)for(let Y=0;Y<D;Y++){let j=(R[D][Y]+R[Y][D])/2;R[D][Y]=j,R[Y][D]=j}let T=t.terms===!1||t.terms===void 0?i.map(D=>[D]):t.terms,A=new Set,S=[],C=[];if(T.forEach((D,Y)=>{if(!Array.isArray(D)||D.length===0)throw new RangeError("each terms group must be a non-empty array");let j=[];for(let U of D){if(!Number.isInteger(U)||U<0||U>=e.coefficients.length)throw new RangeError("terms column indices must be zero-based and within x");if(A.has(U))throw new RangeError("terms groups must not overlap");A.add(U),i.includes(U)&&j.push(U)}j.length>0&&(S.push(j),C.push(Y))}),i.forEach(D=>{A.has(D)||(S.push([D]),C.push(T.length))}),S.length===0)throw new Error("the proportional-hazards test has no estimable terms");let L=[];S.forEach((D,Y)=>{if(o&&D.length>1)L.push({col:-1,group:Y,collapsed:!0});else for(let j of D)L.push({col:j,group:Y,collapsed:!1})});let k=L.length,F=Array.from({length:c},()=>Kn(k)),I=[],O=[];L.forEach((D,Y)=>{let j=S[D.group],U=C[D.group];if(D.collapsed){for(let W of j){let te=i.indexOf(W);F[te][Y]=y[te]}I.push(1),O.push(`g${U+1}`)}else{let W=i.indexOf(D.col);F[W][Y]=s.inverseCovariateScale[D.col],I.push(e.coefficients[D.col]),O.push(j.length>1?`g${U+1}x${D.col+1}`:`x${D.col+1}`)}});let q=Array.from({length:k},(D,Y)=>{let j=0;for(let U=0;U<c;U++)j=Math.max(j,Math.abs(F[U][Y]));if(!(j>0&&Number.isFinite(j)))throw new Error("the proportional-hazards residual variance is singular");return Math.floor(Math.log2(j))}),$=F.map(D=>D.map((Y,j)=>V(Y,-q[j]))),_=Array.from({length:c},()=>Kn(k));for(let D=0;D<c;D++)for(let Y=0;Y<k;Y++)for(let j=0;j<c;j++)_[D][Y]+=v[D][j]*$[j][Y];let P=mr(k);for(let D=0;D<k;D++)for(let Y=0;Y<k;Y++)for(let j=0;j<c;j++)P[D][Y]+=$[j][D]*_[j][Y];for(let D=0;D<k;D++)for(let Y=0;Y<D;Y++){let j=(P[D][Y]+P[Y][D])/2;P[D][Y]=j,P[Y][D]=j}let K;try{K=nt(P)}catch{throw new Error("the proportional-hazards residual variance is singular")}for(let D=0;D<k;D++)for(let Y=0;Y<D;Y++){let j=(K[D][Y]+K[Y][D])/2;K[D][Y]=j,K[Y][D]=j}let X=K.map((D,Y)=>D.map((j,U)=>a.length*Ve(j,-q[Y]-q[U]-s.weightExp))),re=x.map(D=>{let Y=Kn(k);for(let j=0;j<k;j++)for(let U=0;U<c;U++)Y[j]+=$[U][j]*D.residual[U];return I.map((j,U)=>{let W=0;for(let te=0;te<k;te++)W+=Y[te]*K[te][U];return j+a.length*Ve(W,-q[U]-s.weightExp)})}),z=[],J=[],Z=[],Se=L.map(D=>D.collapsed?S[D.group].map(Y=>i.indexOf(Y)):[i.indexOf(D.col)]);for(let D of Se){let Y=D.map(te=>D.map(fe=>R[te][fe])),j=D.map(te=>N[te]),U;try{U=nt(Y,j)}catch{throw new Error("the proportional-hazards term information is singular")}let W=V(Math.max(0,j.reduce((te,fe,Re)=>te+fe*U[Re],0)),s.weightExp);z.push(W),J.push(D.length),Z.push(It(W,D.length,{lowerTail:!1}))}if(r){let D;try{D=nt(R,N)}catch{throw new Error("the global proportional-hazards information is singular")}let Y=V(Math.max(0,N.reduce((j,U,W)=>j+U*D[W],0)),s.weightExp);z.push(Y),J.push(c),Z.push(It(Y,c,{lowerTail:!1})),O.push("GLOBAL")}let ne={rowNames:O,chisq:z,df:J,p:Z,pValue:Z,x:x.map(D=>D.g),time:x.map(D=>D.time),...s.strata===void 0?{}:{strata:x.map(D=>ft(s.strata[D.input]))},y:re,var:X,transform:n,warnings:f};return Q("coxZph",ne,e)}var j1=["center"],W1=2**-1022;function B1(e,t,n={}){B(n,j1,"leveneTest");let r=Ae(n.center??"median",["median","mean"]),{groups:o}=or(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"?js(l):it(l);return l.map(m=>Math.abs(m-f))},i=qe(o).groups.map(s),c=(l,f)=>l.some(m=>m>0&&m<W1)||l.every(m=>m===0)&&o[f].some(m=>m!==o[f][0]);if(i.some(c)){let l=o.map(u=>{let h=qe([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=>V(h,u.k-m)))}let a=Zm(xr(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 K1=["score"],U1=["alternative","exact","variant","levels"];function H1(e,t,...n){B(bs(n),K1,"propTrendTest");let{score:r}=ws(["score"],n),o=Array.from(e),s=Array.from(t),i=o.length,c=r===void 0?o.map((y,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(y=>!(y>0&&y<1/0)))throw new RangeError("elements of 'n' must be positive and finite");if(o.some((y,v)=>!(y>=0&&y<=s[v])))throw new RangeError("elements of 'x' must be between 0 and 'n'");if(o.every(y=>y===0)||o.every((y,v)=>y===s[v]))throw new RangeError("all proportions are 0 or all are 1: the test for trend is not defined");let a=s.reduce((y,v)=>y+v,0),l=o.reduce((y,v)=>y+v,0)/a,f=s.map(y=>y/(l*(1-l))),m=o.map((y,v)=>y/s[v]),u=c.flatMap((y,v)=>Number.isNaN(y)?[]:[v]);if(u.length===0)throw new RangeError("no non-missing scores");if(u.some(y=>!Number.isFinite(c[y])))throw new RangeError("non-missing scores must be finite");if(u.every(y=>c[y]===c[u[0]]))throw new RangeError("at least 2 distinct non-missing scores are needed");let h=c,{sw:p,sxy:w,sxx:d}=bc(f,m,h,u);if(!(d>=2**-1022&&d<1/0&&Math.abs(w)<1/0)){let y=qe([u.map(v=>c[v])]);y.k!==0&&(h=c.slice(),u.forEach((v,M)=>h[v]=y.groups[0][M]),{sw:p,sxy:w,sxx:d}=bc(f,m,h,u))}let b=Vt(w,d);if(!(a<1/0)||l>0&&l<1&&!(p<1/0&&Math.abs(w)<1/0&&d<1/0)){let{groups:[y,v],k:M}=qe([o,s]),g=y.reduce((x,E)=>x+E,0)/v.reduce((x,E)=>x+E,0),N=bc(v,m,h,u);b=V(N.sxy/g*(N.sxy/N.sxx/(1-g)),M)}return{method:"Chi-squared Test for Trend in Proportions",statistic:b,statisticName:"X-squared",parameter:1,parameterName:"df",pValue:bt(b,.5,2,!1,!1),warnings:[]}}function bc(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 Y1=2**24,G1=2**32,X1=`Sample size > 100 or data with ties
10
- p-value based on normal approximation. Specify nperm for permutation p-value`,Q1=`Sample data with ties
11
- p-value based on normal approximation. Specify nperm for permutation p-value.`;function Z1(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=St(t));let c;if(n!==void 0)c=n.map(ft);else{let f=[...new Set(i.map(m=>t[m]))];c=f.every(m=>typeof m=="number")?f.sort((m,u)=>m-u).map(ft):[...new Set(f.map(ft))].sort(dn)}let a=c.map(()=>[]),l=new Map(c.map((f,m)=>[f,m]));for(let f of i){let m=l.get(ft(t[f]));if(m===void 0)throw new RangeError(`the group '${ft(t[f])}' is not among 'levels'`);a[m].push(o[f])}return a}function J1(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 eb(e,t,n={}){B(n,U1,"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=Z1(e,t,n.levels,o==="clinfun");if(s.filter(y=>y.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((y,v)=>{m-=y.length,f.push([y.length,m]);for(let M=v+1;M<s.length;M++)for(let g of y)for(let N of s[M])l+=g<N?1:g===N?.5:0});let u=[],h=!a&&c<=100;o==="DescTools"&&n.exact!==void 0?(h=n.exact,h&&a&&(h=!1,u.push(Q1))):h||u.push(X1);let p,w;if(h){let y=1,v=0;for(let[g,N]of f)g===0||N===0||(v+=y*(g*N+1),y+=g*N);if(y>Y1||v>G1)throw new RangeError(`the exact distribution of JT has ${y} values and needs ${v} steps, too many: use exact: false`);let M=[1];for(let[g,N]of f){if(g===0||N===0)continue;let x=Array.from({length:g*N+1},(R,T)=>ul(T,g,N,!1)),E=new Array(M.length+x.length-1).fill(0);M.forEach((R,T)=>x.forEach((A,S)=>E[T+S]+=R*A)),M=E}p=0,w=0,M.forEach((g,N)=>{N<=l&&(p+=g),N>=l&&(w+=g)})}else{let y=f.reduce((g,[N,x])=>g+N*x,0)/2,v=a?J1(s.map(g=>g.length),i):f.reduce((g,[N,x])=>g+N*x*(N+x+1)/12,0),M=(l-y)/Math.sqrt(v);p=ae(M),w=1-p,w<1e-8&&(w=ae(M,{lowerTail:!1}))}let b=r==="increasing"?w:r==="decreasing"?p:Math.min(1,2*Math.min(w,p,o==="clinfun"?.5:1));return Q("jonckheereTest",{method:"Jonckheere-Terpstra test",alternative:r,statistic:l,statisticName:"JT",pValue:b,warnings:u})}var tb=["nClasses","adjust"];function Ho(e){return Pt(Array.from(e)).sort((t,n)=>t-n)}function $u(e){if(e.length>0&&!(Number.isFinite(e[0])&&Number.isFinite(e[e.length-1])))throw new RangeError("'x' must be finite")}function ca(e){$u(e);let t=al(e);if(t.some(Number.isNaN))throw new RangeError("all 'x' values are identical");return t}function nb(e){let t=Ho(e),n=t.length;if(n<8)throw new RangeError("sample size must be greater than 7");let r=ca(t),o=0;for(let a=1;a<=n;a++)o+=(2*a-1)*(ae(r[a-1],{logP:!0})+ae(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 rb(e){let t=Ho(e),n=t.length;if(n<8)throw new RangeError("sample size must be greater than 7");let r=ca(t),o=1/(12*n);r.forEach((a,l)=>{o+=(ae(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")),Q("cvmTest",{method:"Cramer-von Mises normality test",statistic:o,statisticName:"W",pValue:c,warnings:i})}function ob(e){let t=Ho(e),n=t.length;if(n<5)throw new RangeError("sample size must be greater than 4");let r=ca(t).map(f=>ae(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 sb(e){let t=Ho(e),n=t.length;if(n<5||n>5e3)throw new RangeError("sample size must be between 5 and 5000");if($u(t),t[0]===t[n-1])throw new RangeError("all 'x' values are identical");let r=t.map((m,u)=>le((u+1-3/8)/(n+1/4))),o=qe([t]).groups[0],s=Math.min(sl(o,r)**2/(st(o)*st(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=ae((Math.log(1-s)-a)/l,{lowerTail:!1});return{method:"Shapiro-Francia normality test",statistic:s,statisticName:"W",pValue:f,warnings:[]}}function ab(e,t={}){B(t,tb,"pearsonTest");let n=Ho(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 ca(n)){let m=Math.min(o,Math.floor(1+o*ae(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:bt(a,l/2,2,!1,!1),warnings:[]}}var ib=["weight"],cb=["exact"],lb=["model","type","unit","r0","confLevel"];function Yo(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 la(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 ju=e=>2*ae(-Math.abs(e));function fb(e,t={}){B(t,ib,"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=>Vn(x[0])),i=r.map(x=>Vn(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},(R,T)=>m(Math.abs(E-T)))),h=f.map(x=>De(x)/o),p=Array.from({length:a},(x,E)=>De(f.map(R=>R[E]))/o),w=x=>{let E=[];for(let R=0;R<a;R++)for(let T=0;T<a;T++)E.push(x(R,T));return De(E)},d=w((x,E)=>f[x][E]*u[x][E])/o,b=w((x,E)=>h[x]*p[E]*u[x][E]),y=(d-b)/(1-b),v=Array.from({length:a},(x,E)=>De(p.map((R,T)=>R*u[E][T]))),M=Array.from({length:a},(x,E)=>De(h.map((R,T)=>R*u[T][E]))),g=(w((x,E)=>h[x]*p[E]*(u[x][E]-(v[x]+M[E]))**2)-b*b)/(o*(1-b)**2),N=y/Math.sqrt(g);return{value:y,statistic:N,pValue:ju(N),warnings:[]}}function mb(e,t={}){B(t,cb,"kappamFleiss");let n=t.exact??!1,r=Yo(e,"ratings");la(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=St(i),a=[...new Set(c)].sort((g,N)=>g-N),l=new Map(a.map((g,N)=>[g,N])),f=new Map(i.map((g,N)=>[g,l.get(c[N])])),m=a.length,u=e.map(g=>{let N=new Array(m).fill(0);for(let x of g)N[f.get(x)]++;return N}),h=De(u.map(g=>(De(g.map(N=>N*N))-s)/(s*(s-1))))/o,p=Array.from({length:m},(g,N)=>De(u.map(x=>x[N]))/(o*s)),w=De(p.map(g=>g*g));if(n){let g=r.map(x=>{let E=new Array(m).fill(0);for(let R of x)E[f.get(R)]++;return E.map(R=>R/o)}),N=Array.from({length:m},(x,E)=>st(g.map(R=>R[E]))*(s-1)/s);w-=De(N)/(s-1)}let d=(h-w)/(1-w);if(n)return{value:d,warnings:[]};let b=De(p.map(g=>g*(1-g))),y=De(p.map(g=>g*(1-g)*(1-g-g))),v=2/(b*b*o*s*(s-1))*(b*b-y),M=d/Math.sqrt(v);return{value:d,statistic:M,pValue:ju(M),warnings:[]}}function ub(e,t={}){B(t,lb,"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;_t(i),Yo(e,"ratings"),la(e,"ratings");let c=qe(e).groups,a=Yo(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=st(c.map($=>it($)))*f,h=st(a.map($=>it($)))*l,p=it(c.map($=>st($))),w=st(c.flat())*(l*f-1),d=w-u*(l-1)-h*(f-1),b=d>2**-20*w?d/((l-1)*(f-1)):gb(c)/((l-1)*(f-1)),y=($,_,P)=>ht($,_,P,!1,!1),v=($,_)=>m/2<1e-8?un(m/2,$,_,!1,!1):un(1-m/2,$,_,!0,!1),M=l-1;if(n==="oneway"||r==="consistency"){let $=n==="oneway"?p:b,_=n==="oneway"?l*(f-1):(l-1)*(f-1),P=u/$,K=P/v(M,_),X=P*v(_,M),[re,z,J]=o==="single"?[(u-$)/(u+(f-1)*$),P*(1-s)/(1+(f-1)*s),Z=>(Z-1)/(Z+f-1)]:[(u-$)/u,P*(1-s),Z=>1-1/Z];return{value:re,Fvalue:z,fValue:z,df1:M,df2:_,pValue:y(z,M,_),lbound:J(K),ubound:J(X),warnings:[]}}let g=o==="single"?f:1,N=$=>[g*$/(l*(1-$)),1+g*$*(l-1)/(l*(1-$))],x=($,_)=>il([$*h,_*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),[R,T]=N(s),A=u/(R*h+T*b),S=x(R,T),[C,L]=[f*E/(l*(1-E)),1+f*E*(l-1)/(l*(1-E))],k=x(C,L),F=v(M,k),I=v(k,M),[O,q]=o==="single"?[l*(u-F*b)/(F*(f*h+(f*l-f-l)*b)+l*u),l*(I*u-b)/(f*h+(f*l-f-l)*b+l*I*u)]:[l*(u-F*b)/(F*(h-b)+l*u),l*(I*u-b)/(h-b+l*I*u)];return{value:E,Fvalue:A,fValue:A,df1:M,df2:S,pValue:y(A,M,S),lbound:O,ubound:q,warnings:[]}}function zu(e,t){let n=t.length,r=De(t.flatMap(s=>t.map(i=>e[s][i])));return(1-De(t.map(s=>e[s][s]))/r)*n/(n-1)}function hb(e){let t=Yo(e,"x").length;if(la(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=Ms(qe(e).groups),r=xo(e),o=Array.from({length:t},(c,a)=>a),s=De(r.flat()),i;try{let c=nt(r);i=o.map(a=>1-1/c[a][a])}catch(c){if(!r.flat().every(Number.isFinite))throw c;let a=qe(e).groups;i=o.map(l=>{let f=a.map(h=>h[l]),m=zr(Lt(a.map(h=>[1,...h.filter((p,w)=>w!==l)])),f),u=it(f);return 1-De(m.map(h=>h*h))/De(f.map(h=>(h-u)**2))})}return Q("cronbachAlpha",{rawAlpha:zu(n,o),stdAlpha:(1-t/s)*t/(t-1),G6:1-(t-De(i))/s,averageR:(s-t)/(t*(t-1)),alphaDrop:o.map(c=>zu(n,o.filter(a=>a!==c))),warnings:pb(e,r)})}function pb(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}=Wt(r.map(c=>r.map(a=>t[c][a])),{symmetric:!0}),s=o.map(c=>c[0]),i=De(s)<0?-1:1;s.some(c=>i*c<0)&&n.push(`Some items were negatively correlated with the first principal component and probably
6
+ 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=bt(h),w=0;for(let x=0;x<i;x++)w+=p[x];let d=i+u,b=d*(d+1)/2-w,N=i*u+i*(i+1)/2-w,v=i*u+u*(u+1)/2-b,E=0;for(let x of wn(h))E+=x*x*x-x;let g=i*u/(d*(d-1))*((d*d*d-d)/12-E/12),y=(Math.min(N,v)-i*u/2)/Math.sqrt(g);return w<b?-y:y}),l=c.map(m=>s.n[m]/(s.n[m]+i)),f=ym(a,l,r,o);return Q("manyOneUTest",Pi(s,`Wilcoxon, Mann, Whittney U-test
7
+ for multiple comparisons with one control`,a,f,o))}function pg(e,t){let n=An(e,t),r=Sr(n.k,(s,i)=>{let c=n.n[s],a=n.n[i],l=[...n.groups[s],...n.groups[i]],f=bt(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,w=0;for(let b of wn(l))w+=b*b*b-b;let d=c*a/(p*(p-1))*((p*p*p-p)/12-w/12);return Math.SQRT2*(h-c*a/2)/Math.sqrt(d)}),o=r.map(s=>s.map(i=>Number.isNaN(i)?NaN:Vi(i,n.k,1/0)));return Q("dscfAllPairsTest",Do(n,"Dwass-Steele-Critchlow-Fligner all-pairs test",r,o,"single-step"))}function dg(e,t,n={}){B(n,cg,"tamhaneT2Test");let r=(()=>{if(t!==void 0)return An(e,t);let f=nr(e),m=f.groups.length;if(m<2)throw new RangeError("there must be at least two groups");let u=f.groups.map(h=>h.length);return{labels:f.labels,groups:f.groups,n:u,N:u.reduce((h,p)=>h+p,0),k:m}})(),o=xt(r.groups),s=xt(o.groups),i=n.welch??!0,c=r.k*(r.k-1)/2,a=Sr(r.k,(f,m)=>{let u=s.variance[f]/r.n[f]+s.variance[m]/r.n[m];return(s.mean[f]-s.mean[m])/Math.sqrt(u)}),l=Sr(r.k,(f,m)=>{let u=a[f-1][m];if(Number.isNaN(u))return NaN;let h=i?ka([s.variance[f],s.variance[m]],[r.n[f],r.n[m]]):r.n[f]+r.n[m]-2,p=2*Qt(Math.abs(u),h,{lowerTail:!1});return p>=1?1:-Math.expm1(c*Math.log1p(-p))});return Q("tamhaneT2Test",Do(r,i?"Tamhane's T2-test for unequal variances":"Tamhane's T2'-test for unequal variances",a,l,i?"T2 (Sidak)":"T2' (Sidak)"))}function gg(e,t){let n=An(e,t),r=xt(n.groups),o=r.mean,s=r.variance.map((a,l)=>a/n.n[l]),i=Sr(n.k,(a,l)=>(o[a]-o[l])/Math.sqrt(s[a]+s[l])*Math.SQRT2),c=Sr(n.k,(a,l)=>{let f=ka([r.variance[a],r.variance[l]],[n.n[a],n.n[l]]);return Vi(i[a-1][l],n.k,f)});return Q("gamesHowellTest",Do(n,"Games-Howell test",i,c,"none"))}function wg(e,t){let n=An(e,t),{m:r,s2:o,df:s}=$i(n);if(s<2)throw new RangeError("the studentized range needs at least 2 residual degrees of freedom");let i=Sr(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:Vi(l,n.k,s)));return Q("tukeyTest",Do(n,"Tukey's test",i,c,"single-step"))}function bg(e,t,n={}){B(n,ag,"dunnTest");let r=n.method??"none",o=n.altp??!1,s=An(e,t),i=Us(e,t).statistic,{R:c,C:a}=Di(s),l=s.N*(s.N+1)/12-a,f=[],m=[];for(let p=1;p<s.k;p++)for(let w=0;w<p;w++)f.push((c[w]-c[p])/Math.sqrt(l*(1/s.n[w]+1/s.n[p]))),m.push(`${s.labels[w]} - ${s.labels[p]}`);let u=f.map(p=>(o?2:1)*Hs(Math.abs(p))),h=vg(u,r);return{chi2:i,Z:f,P:u,PAdjusted:h,zValue:f,pValue:u,pAdjusted:h,comparisons:m,warnings:[]}}function vg(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 Ng(e,t,n={}){B(n,ig,"glhtDunnett");let r=Ze(n.alternative),o=An(e,t),{m:s,s2:i,df:c,k:a}=$i(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=>V(N,a)),w=u.map(N=>V(N,a)),d=f.map(N=>o.n[N]/(o.n[N]+o.n[0])),b=h.map(N=>r==="two.sided"?Tr(N,d,c,!0):Tr(r==="greater"?N:-N,d,c,!1));return{names:f.map(N=>`${o.labels[N]} - ${o.labels[0]}`),estimate:p,se:w,t:h,p:b,stdError:w,tValue:h,pValue:b,df:c,warnings:[]}}var Mm=["varEqual"],yg=2**-1022;function xm(e,t,n={}){if(typeof e!="string"&&ct(t)&&B(t,Mm,"onewayTest"),B(n,Mm,"onewayTest"),typeof e=="string"){let d=ql(e,t);return xm(d.values,d.groups,n)}let r;t!==void 0&&!Array.isArray(t)?n=t:r=t;let o=vr(e,r),s=xt(o),i=s.groups,c=n.varEqual??!1,a=i.length;if(a<2)throw new RangeError("not enough groups");let l=i.map(d=>d.length);if(l.some(d=>d<2))throw new RangeError("not enough observations");let f=s.mean,m=s.variance,u=l.reduce((d,b)=>d+b,0),h,p,w;if(c){let d=s.grandMean,b=0,N=0;for(let v=0;v<a;v++)b+=l[v]*(f[v]-d)**2,N+=(l[v]-1)*m[v];p=u-a,Po(p,s.ssWithin),h=b/(a-1)/(N/p)}else if(m.some(d=>!(d>=yg))&&(w=Eg(o))!==void 0){let d=w.map((M,R)=>Em(l[R]/M.variance,-2*M.k)),b=Math.max(...d.map(M=>M.e)),N=d.map(M=>Ve(M.m,M.e-b)),v=N.reduce((M,R)=>M+R,0),E=0;for(let M=0;M<a;M++){let R=N.reduce((T,S,A)=>A===M?T:T+S,0);E+=(R/v)**2/(l[M]-1)}E/=a*a-1;let g=[];for(let M=0;M<a;M++)for(let R=M+1;R<a;R++){let[T,S]=w[M].k>=w[R].k?[w[M],w[R]]:[w[R],w[M]],A=T.mean-Ve(S.mean,S.k-T.k);if(A===0)continue;let C=Em(A,T.k);g.push({m:d[M].m*d[R].m*C.m*C.m/v,e:d[M].e+d[R].e+2*C.e-b})}let y=Math.max(...g.map(M=>M.e)),x=g.reduce((M,R)=>M+Ve(R.m,R.e-y),0);h=(g.length===0?0:Ve(x,y))/((a-1)*(1+2*(a-2)*E)),p=1/(3*E)}else if(m.some(d=>d===0)){let d=m.flatMap((E,g)=>E===0?[g]:[]);if(d.length>1)throw new RangeError("Welch's test is not defined with two or more constant groups");let b=d[0],N=0,v=0;for(let E=0;E<a;E++)E!==b&&(N+=1/(l[E]-1),v+=l[E]/m[E]*(f[E]-f[b])**2);N/=a*a-1,h=v/((a-1)*(1+2*(a-2)*N)),p=1/(3*N)}else{let d=l.map((g,y)=>g/m[y]),b=d.reduce((g,y)=>g+y,0),N=0,v=0;for(let g=0;g<a;g++)N+=(1-d[g]/b)**2/(l[g]-1),v+=d[g]*f[g];N/=a*a-1,v/=b;let E=0;for(let g=0;g<a;g++)E+=d[g]*(f[g]-v)**2;h=E/((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:ht(h,a-1,p,!1,!1),warnings:[]}}function Mg(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=hm(t);return{method:"Shapiro-Wilk normality test",statistic:1-r,statisticName:"W",pValue:pm(r,n),warnings:[]}}function Eg(e){let t=[];for(let n of e){let r=xt([n]),o=r.variance[0];if(!(o>0))return;t.push({mean:r.mean[0],variance:o,k:r.k})}return t}function Em(e,t){let n=Math.floor(Math.log2(Math.abs(e))),r=Ve(e,-n);return Math.abs(r)>=2?(r/=2,n++):Math.abs(r)<1&&(r*=2,n--),{m:r,e:t+n}}function zi(e,t,n){let r=[0,0];for(let o=Math.max(0,t);o<Math.min(n,e.length);o++)r=he(r,[e[o],0]);return r[0]}function xg([[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]=bn(f,o,s,i,!1);return{lo:c,d:l}}function Rg(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 Ys=class{lo;hi;logNull;constructor(t){let n=0,r=new Float64Array([1]);for(let o of t){let s=xg(o);n+=s.lo,r=Rg(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=zi(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=he(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?zi(o,s,o.length):zi(o,0,s+1)}};function ji(e,t){return rr(e,t)}function Rm(e,t){if(t===e.lo)return 0;if(t===e.hi)return 1/0;let n=e.mean(1);return n===t?1:ji(r=>e.mean(r)-t,n>t)}function Wi(e,t,n){if(t===e.hi)return 1/0;let r=e.cdf(t,1,!1);return r===n?1:ji(o=>n-e.cdf(t,o,!1),r<n)}function Bi(e,t,n){if(t===e.lo)return 0;let r=e.cdf(t,1,!0);return r===n?1:ji(o=>e.cdf(t,o,!0)-n,r>n)}var Tg=["alternative","correct","exact","confLevel"];function Sg(e,t={}){B(t,Tg,"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(Ki);if(s.some(f=>f<2))throw new RangeError("sample size in each stratum must be > 1");let i=Ze(t.alternative),c=t.correct??!0,a=t.exact??!1,l=t.confLevel??.95;if(_t(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?kg(f,i,l):Ag(f,s,i,c,l)}return Ig(e,s,r,o)}function Ki(e){return e.reduce((t,n)=>t+n.reduce((r,o)=>r+o,0),0)}function Am(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 qm(e,t){return e.map(n=>n.map(r=>r.map(o=>V(o,-t))))}function Tm(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]],w)=>{let d=t[w],b=m+u,N=h+p,v=m+h,E=u+p;r+=m;let g=b*v/d;o+=Number.isFinite(g)?g:b/d*v;let y=b*N*v*E/(d*d*(d-n));s+=Number.isFinite(y)?y:b/d*(N/d)*(v/(d-n))*E;let x=(m+p)/d,M=(u+h)/d,R=m*p/d,T=u*h/d,S=Number.isFinite(R)?R:m/d*p,A=Number.isFinite(T)?T:u/d*h;i+=S,c+=A,a+=x*S,l+=x*A+M*S,f+=M*A}),{sa:r,se:o,sv:s,sR:i,sS:c,sPR:a,sPSQR:l,sQS:f}}function Ag(e,t,n,r,o){let s=Tm(e,t,1),i=0;if(!t.every(Number.isFinite)||!Object.values(s).every(Number.isFinite)){i=Am(e);let x=qm(e,i);s=Tm(x,x.map(Ki),V(1,-i))}let{sa:c,se:a,sv:l,sR:f,sS:m,sPR:u,sPSQR:h,sQS:p}=s,w=c-a,d=V(.5,-i),b=r&&Math.abs(w)>=d?d:0,N=V(Vt(Math.abs(w)-b,l),i),v=n==="two.sided"?wt(N,.5,2,!1,!1):se(Math.sign(w)*Math.sqrt(N),{lowerTail:n==="less"}),E=f/m,g=V(Math.sqrt(u/(2*f*f)+h/(2*f*m)+p/(2*m*m)),-i/2),y;if(n==="less")y=[0,E*Math.exp(le(o)*g)];else if(n==="greater")y=[E*Math.exp(le(o,{lowerTail:!1})*g),1/0];else{let x=(1-o)/2,M=x<1e-8?le(x,{lowerTail:!1}):le((1+o)/2);y=[E*Math.exp(-M*g),E*Math.exp(M*g)]}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:y,confLevel:o,estimate:E,estimateNames:"common odds ratio",nullValue:1,nullValueName:"common odds ratio",warnings:[]}}var qg=2**24;function kg(e,t,n){let r=e.reduce((a,[[l,f],[m,u]])=>a+Math.min(l,u)+Math.min(f,m),1);if(r>qg)throw new RangeError(`the exact distribution of S has ${r} values, more than 2^24: use exact: false`);let o=new Ys(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=he(f,[m,0]));i=Math.min(1,f[0])}else i=o.cdf(s,1,t==="greater");let c;if(t==="less")c=[0,Wi(o,s,1-n)];else if(t==="greater")c=[Bi(o,s,1-n),1/0];else{let a=(1-n)/2;c=[Bi(o,s,a),Wi(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:Rm(o,s),estimateNames:"common odds ratio",nullValue:1,nullValueName:"common odds ratio",warnings:[]}}function Ig(e,t,n,r){let{diff:o,V:s}=Sm(e,t,n,r,1),i=0;if(!t.every(Number.isFinite)||!o.every(Number.isFinite)||!s.every(f=>f.every(Number.isFinite))){i=Am(e);let f=qm(e,i);({diff:o,V:s}=Sm(f,f.map(Ki),n,r,V(1,-i)))}let c=o.length,a=nt(s,o),l=0;for(let f=0;f<c;f++)l+=o[f]*a[f];return l=V(l,i),{method:"Cochran-Mantel-Haenszel test",statistic:l,statisticName:"Cochran-Mantel-Haenszel M^2",parameter:c,parameterName:"df",pValue:wt(l,c/2,2,!1,!1),warnings:[]}}var _g=2**-1022;function Sm(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((w,d)=>w+d,0)),u=Array.from({length:r},(p,w)=>a.reduce((d,b)=>d+b[w],0)),h=1/(f*f*(f-o));for(let p=0;p<r-1;p++)for(let w=0;w<n-1;w++){let d=w+(n-1)*p,b=m[w]*u[p]/f;i[d]+=a[w][p]-(Number.isFinite(b)?b:ms(m[w],u[p],f));for(let N=0;N<r-1;N++)for(let v=0;v<n-1;v++){let E=(w===v?f*m[w]:0)-m[w]*m[v],g=(p===N?f*u[p]:0)-u[p]*u[N],y=h*E*g;c[d][v+(n-1)*N]+=Number.isFinite(y)&&h>=_g?y:m[w]*((w===v?1:0)-m[v]/f)*(((p===N?1:0)-u[N]/f)*(u[p]/(f-o)))}}}),{diff:i,V:c}}var km=new Map,Lg=2**24,Cg=2**32;function _m(e,t){let n=e+t,r=(e+1)*(Math.floor((n+1)**2/4)+1);if(2*r>Lg||n*r>Cg)throw new RangeError(`the exact distribution for m = ${e} and n = ${t} is too large to compute: use exact: false`)}function Fg(e,t){let n=`${e},${t}`,r=km.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],w=Array.from({length:e+1},()=>new Float64Array(i+1));for(let d=0;d<=e;d++){let b=c[d],N=e-d;if(N>h)continue;let v=N/h;for(let E=0;E<=i;E++){let g=b[E];g!==0&&(N>0&&(w[d+1][E+p]+=g*v),N<h&&(w[d][E]+=g*(1-v)))}}c=w}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 km.set(n,m),m}function Ui(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}=Fg(t,n),i=il(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 Im=new Map;function Og(e,t){let n=`${e},${t}`,r=Im.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 w=i[p];for(let d=0;d<=s;d++){let b=w[d];b!==0&&(p<e&&(h[p+1][d+u]+=b),e-p<o-m&&(h[p][d]+=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 Im.set(n,f),f}function Gs(e,t,n){let{min:r,counts:o}=Og(t,n);if(e<=0)return r;if(e>=1)return r+o.length-1;let s=hl(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 Dg=["alternative","exact","confInt","confLevel"];function Om(e,t,n={}){if(B(n,Dg,"ansariTest"),typeof e=="string"){let g=tr(e,t);return Om(g.x,g.y,n)}let r=Array.from(e).filter(g=>!Number.isNaN(g)),o=Array.from(t).filter(g=>!Number.isNaN(g)),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=Ze(n.alternative),a=n.confInt??!1,l=n.confLevel??.95;a&&_t(l);let f=s+i,m=bt(r.concat(o)),u=m.map(g=>Math.min(g,f-g+1)),h=0;for(let g=0;g<s;g++)h+=u[g];let p=new Set(m).size<f,w=n.exact??(s<50&&i<50&&!p),d=n.exact??(s<50&&i<50),b=new Rt,N,v;if(w&&!p){_m(s,i);let g=x=>Ui(x-1,s,i,!1),y=x=>Ui(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?g(h):y(h)))}else c==="less"?N=g(h):N=y(h);a&&(v=zg(r,o,c,l,b))}else{let g=Dm(h,u,p,s,i);N=c==="two.sided"?2*Math.min(se(g),se(g,{lowerTail:!1})):c==="less"?se(g,{lowerTail:!1}):se(g),a&&(v=jg(r,o,c,l,d?new Rt:b)),d&&p&&(b.add("cannot compute exact p-value with ties"),a&&b.add("cannot compute exact confidence interval with ties"))}let E={method:"Ansari-Bradley test",alternative:c,statistic:h,statisticName:"AB",pValue:N,warnings:b.list};return v!==void 0&&(E.confInt=v.confInt,E.confLevel=l,E.estimate=v.estimate,E.estimateNames="ratio of scales"),E.nullValue=1,E.nullValueName="ratio of scales",Q("ansariTest",E)}function Dm(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 Pm(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)<Pg))?Vg(e.map(f=>Lm(f,n)).concat(t.map(f=>Lm(f,1)))):$g(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 Pg=2**-1022;function Lm(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]=Cm(Math.abs(e)),[s,i]=Cm(t),c=o/i,a=r-s;return c<1&&(c*=2,a-=1),[n,a,c]}function Cm(e){let t=Math.floor(Math.log2(e)),n=V(e,-t);return n>=2?(n/=2,t+=1):n<1&&(n*=2,t-=1),[t,n]}function Fm(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 Vg(e){let t=e.map((r,o)=>o).sort((r,o)=>Fm(e[r],e[o])),n=new Array(e.length);for(let r=0;r<t.length;){let o=r+1;for(;o<t.length&&Fm(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 $g(e){let t=e.filter(s=>!Number.isNaN(s));if(t.length===e.length)return bt(e);let n=bt(t),r=0,o=t.length;return e.map(s=>Number.isNaN(s)?++o:n[r++])}function Xs(e,t,n){return e==="two.sided"?n(t):e==="greater"?[n(2*t)[0],1/0]:[0,n(2*t)[1]]}function zg(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:Xs(n,1-r,()=>[NaN,NaN]),estimate:NaN};let l=c.map((b,N)=>Pm(e,t,b+(N<a-1?(c[N+1]-b)/2:b*1.01)).ab),f=(b,N,v)=>{let E=1/0;for(let y of l)(N?y-b>0:y-b>=0)&&y-b<E&&(E=y-b);let g=v?-1/0:1/0;return l.forEach((y,x)=>{y-b===E&&(g=v?Math.max(g,c[x]):Math.min(g,c[x]))}),g},m=b=>{let N=Gs(b/2,s,i),v=Gs(1-b/2,s,i);if(!l.some(y=>y-N>=0)||!l.some(y=>y-v>0))return o.add("samples differ in location: cannot compute confidence set, returning NA"),[NaN,NaN];let E=f(N,!1,!1),g=f(v,!0,!0);return E>g?[f(v,!1,!1),f(N,!0,!0)]:[E,g]},u=Xs(n,1-r,m),h=Gs(.5,s,i),p=-1/0,w=1/0;l.forEach((b,N)=>{b-h<=0?p=Math.max(p,c[N]):w=Math.min(w,c[N])});let d=p===-1/0||w===1/0?NaN:(w+p)/2;return d===1/0&&(d=w/2+p/2),{confInt:u,estimate:d}}function jg(e,t,n,r,o){let s=e.length,i=t.length,c=(b,N)=>{let{ab:v,a:E,ties:g}=Pm(e,t,b);return Dm(v,E,g,s,i)-N},a=[],l=(b,N)=>{b.length===0||N.length===0||a.push(an(b)/Nt(N),Nt(b)/an(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:Xs(n,1-r,()=>[NaN,NaN]),estimate:NaN};let f=an(a),m=Nt(a),u=b=>Kt(N=>c(N,b),f,m,{tol:1e-4}),h=b=>{let N=le(b/2),v=le(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 E=u(N),g=u(v);return E<=g?[E,g]:[g,E]},p=Xs(n,1-r,h),w=c(f,0)>0||c(m,0)<0;w&&o.add("cannot compute estimate, returning NA");let d=w?NaN:u(0);return{confInt:p,estimate:d}}var Wg=["alternative","confLevel"];function Bg(e,t,n={}){B(n,Wg,"brunnerMunzelTest");let r=Array.from(e).filter(M=>!Number.isNaN(M)),o=Array.from(t).filter(M=>!Number.isNaN(M)),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=Ze(n.alternative),a=n.confLevel??.95;cn(a);let l=bt(r.concat(o)),f=l.slice(0,s),m=l.slice(s),u=M=>M.reduce((R,T)=>R+T,0)/M.length,h=u(f),p=u(m),w=(M,R,T)=>{let S=(M.length+1)/2,A=0;for(let C=0;C<M.length;C++)A+=(M[C]-R[C]-T+S)**2;return A/(M.length-1)},d=w(f,bt(r),h),b=w(m,bt(o),p),N=(p-(i+1)/2)/s,v=s*d+i*b,E=s*i*(p-h)/(s+i)/Math.sqrt(v),g=v**2/((s*d)**2/(s-1)+(i*b)**2/(i-1)),y;c==="greater"?y=dt(E,g,!0,!1):c==="less"?y=dt(E,g,!1,!1):y=2*dt(-Math.abs(E),g,!0,!1);let x=gt((1-a)/2,g,!1,!1)*Math.sqrt(d/(s*i**2)+b/(i*s**2));return{method:"Brunner-Munzel Test",alternative:c,statistic:E,statisticName:"Brunner-Munzel Test Statistic",parameter:g,parameterName:"df",pValue:y,confInt:[N-x,N+x],confLevel:a,estimate:N,estimateNames:"P(X<Y)+.5*P(X=Y)",warnings:[]}}var Kg=[1.2337005501361697,7831619385924639e-32];function Ug(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 w=f[h*t+p];if(w!==0)for(let d=0;d<t;d++)u[h*t+d]+=w*m[p*t+d]}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 Hg=2**33;function $m(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>Hg)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 zm(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]=jt(e,t),u=2*f-1+2*m,h=1,p=0;for(let w=1;w<=e;w++)h*=w/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]=Ug(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 Hi(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=rt(Kg,jt(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 Yi(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]=jt(e,t),o=Math.floor(e-n-r),s=0;for(let i=0;i<=o;i++){let c=he(Ot(e-i,-n),[-r,0]);if(c[0]<=0)break;let a=he(Ot(i,n),[r,0]),l=rt(a,[e,0]),f=rt(c,[e,0]);s+=al(i,e-i,l,f,!1)/(l[0]+l[1])}return Math.min(t*s,1)}var Vm=2**28;function jm(e,t){let[n,r]=jt(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 w=(h+n+r)/e;w>0&&w<1&&s.push({t:w,up:!1,i:h})}if(s.length*(2*o+1)*Math.min(e+1,100)>Vm)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 w=p/(1-p);a.fill(0);for(let d=l;d<i;d++){let b=c[d];if(b===0)continue;let N=e-d,v=Math.min(N,Math.floor((N+1)*p)),E=i-d,g=hn(v,N,p,!1);for(let y=v;y<=N&&g!==0;y++)y>v&&(g=g*(N-y+1)*w/y),y>=E?m+=b*g:a[d+y]+=b*g,u++;g=v>0?hn(v-1,N,p,!1):0;for(let y=v-1;y>=0&&g!==0;y--)y<v-1&&(g=g*(y+1)/((N-y)*w)),y>=E?m+=b*g:a[d+y]+=b*g,u++}if(u>2*Vm)return NaN;[c,a]=[a,c]}if(f=h.t,h.up)i=h.i+1;else{for(let w=l;w<=h.i;w++)m+=c[w],c[w]=0;l=Math.max(l,h.i+1)}}return Math.min(m,1)}function Wm(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,w=(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)?(w>0&&(m+=w*bn(u,t,n,p,!1)),f[h]=0):f[h]=w}[l,f]=[f,l],f.fill(0)}return Math.min(m,1)}var Yg=["alternative","exact"];function Bm(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 Km(e,t,n={}){if(B(n,Yg,"ksTest"),typeof e=="string"){let N=tr(e,t);return Km(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=Ze(n.alternative),i=s==="two.sided"?"D":s==="greater"?"D^+":"D^-";if(typeof t=="function"){let N=-1/0,v=-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]`);N=Math.max(N,(M+1)/o-R),v=Math.max(v,R-M/o)}let E=s==="two.sided"?Math.max(N,v):s==="greater"?N:v,g=new Set(r).size<o,y=n.exact??(o<100&&!g),x;if(y&&s==="two.sided"&&E<.5&&$m(o,E),s==="two.sided")if(!y)x=Hi(Math.sqrt(o)*E,!1);else{let M=2*Yi(o,E);if(E>=.5)x=Math.min(1,M);else{let R=M<.01?jm(o,E):NaN;x=Number.isNaN(R)?1-zm(o,E):R}}else x=y?Yi(o,E):Math.exp(-2*o*E*E);return Q("ksTest",{method:`${y?"Exact":"Asymptotic"} one-sample Kolmogorov-Smirnov test`,alternative:Bm(s,!0),exact:y,statistic:E,statisticName:i,pValue:Math.min(1,Math.max(0,x)),warnings:g?["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,w=new Set(l.map(N=>N[0])).size<l.length,d=n.exact??o*a<1e4,b;if(d)b=Wm(p,o,a,w?{alternative:s,z:l.map(N=>N[0])}:{alternative:s});else{let N=o*a/(o+a);b=s==="two.sided"?Hi(Math.sqrt(N)*p,!1):Math.exp(-2*N*p*p)}return Q("ksTest",{method:`${d?"Exact":"Asymptotic"} two-sample Kolmogorov-Smirnov test`,alternative:Bm(s,!1),exact:d,statistic:p,statisticName:i,pValue:Math.min(1,Math.max(0,b)),warnings:w&&!d?["p-value will be approximate in the presence of ties"]:[]})}var Gg=["poolSd","paired","pAdjustMethod","alternative"],Xg=["mu","paired","exact","correct","confLevel","tolRoot","pAdjustMethod","alternative"],Qg=["pAdjustMethod","correct"];function Um(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 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(zn);return{levels:i,groups:i.map(c=>o.get(c))}}function Gi(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=Kr(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 Xi(e,t,n,r,o=[]){return{method:e,pValue:n,rowNames:t.slice(1).map(vt),colNames:t.slice(0,-1).map(vt),pAdjustMethod:r,warnings:o}}function Zg(e,t,n={}){B(n,Gg,"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=Ze(n.alternative),i=n.pAdjustMethod??"holm",{levels:c,groups:a}=Um(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=xt(a),p=h.mean,w=0;for(let b of u)b>1&&(w+=b-1);Po(w,h.ssWithin);let d=Math.sqrt(h.ssWithin/w);f=(b,N)=>{let v=(p[b]-p[N])/(d*Math.sqrt(1/u[b]+1/u[N]));return s==="less"?dt(v,w,!0,!1):s==="greater"?dt(v,w,!1,!1):2*dt(-Math.abs(v),w,!0,!1)}}else m=r?"paired t tests":"t tests with non-pooled SD",f=(u,h)=>ot(()=>Nn(a[u],a[h],{paired:r,alternative:s})).pValue;return Xi(m,c,Gi(l,f,i),i)}function Jg(e,t,n={}){B(n,Xg,"pairwiseWilcoxTest");let{pAdjustMethod:r,alternative:o,...s}=n,i=r??"holm",{levels:c,groups:a}=Um(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 Rt,u=Gi(l,(h,p)=>{let w=ot(()=>_o(a[h],a[p],{...s,...o===void 0?{}:{alternative:o}}));f=w.method;for(let d of w.warnings)m.add(d);return w.pValue},i);return Q("pairwiseWilcoxTest",Xi(f,c,u,i,m.list))}function ew(e,t,n={}){if(B(n,Qg,"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 Rt,l=Gi(s,(m,u)=>{let h=ot(()=>pi([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 Q("pairwisePropTest",Xi("Pairwise comparison of proportions",f,l,i,a.list))}var Vo=[0,0];function Qs(e){if(!(e[0]>0))return[Math.sqrt(e[0]),0];let t=Math.sqrt(e[0]),n=Le(e,jt(t,t));return Ot(t,n[0]/(2*t))}function Zs(e,t){return Array.from({length:t},(n,r)=>e===void 0?[1,0]:Qs([e[r],0]))}function Hr(e,t){return Kn(e,t,[]).fit}function Kn(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=Vo;for(let b=f;b<r;b++)u=he(u,_e(m[b],m[b]));if(u[0]===0){a.push(Vo);continue}let h=Qs(u),p=m[f][0]<0?[-h[0],-h[1]]:h;m[f]=he(m[f],p);let w=_e(p,m[f]),d=b=>{let N=Vo;for(let E=f;E<r;E++)N=he(N,_e(m[E],b[E]));let v=rt(N,w);for(let E=f;E<r;E++)b[E]=Le(b[E],_e(v,m[E]))};for(let b=f+1;b<e.length;b++)d(s[b]);d(i);for(let b of c)d(b);a.push([-p[0],-p[1]])}let l=new Array(o).fill(Vo);for(let f=o-1;f>=0;f--){let m=i[f];for(let u=f+1;u<o;u++)m=Le(m,_e(s[u][f],l[u]));l[f]=rt(m,a[f])}return{fit:{qty:i,coef:l},qtv:c}}function Yr(e,t=0,n=e.length){let r=Vo;for(let o=t;o<n;o++)r=he(r,_e(e[o],e[o]));return r}var tw=1e-14;function nw(e,t,n,r,o,s,i,c){let a=y=>V(y,2*i),l=t.length,f=n.flatMap((y,x)=>Yr(y)[0]>tw*s[x]?[x]:[]),m=0,u=[],h=t.slice(),p=new Array(l).fill(0);if(f.length>0){let y=Lt(Array.from({length:l},(M,R)=>f.map(T=>n[T][R][0])));m=y.rank,u=y.pivot.slice(0,m).map(M=>f[M]);let x=Hr(u.map(M=>n[M]),t);h=x.qty,u.forEach((M,R)=>{for(let T=0;T<l;T++)p[T]=p[T]+n[M][T][0]*x.coef[R][0]})}let w=[];u.forEach((y,x)=>{let M=r[y],R=w.find(T=>T.term===M);R===void 0&&w.push(R={term:M,df:0,ss:[0,0]}),R.df++,R.ss=he(R.ss,_e(h[x],h[x]))});let d=l-m,b=Yr(h,m)[0],N=Ri(p,b,d),v=w.filter(y=>y.term!==0);d>0&&Er(c,b)&&(b=0,v=v.map(y=>Er(c,y.ss[0])?{...y,ss:[0,0]}:y));let E=v.map(y=>o[y.term-1]),g=y=>{if(d>0){let x=b/d,M=v.map(({df:R,ss:T})=>{let S=T[0]/R/x;return[R,y(T[0]),y(T[0]/R),S,ht(S,R,d,!1,!1)]});return M.push([d,y(b),y(x),NaN,NaN]),{stratum:`Error: ${e}`,colNames:["Df","Sum Sq","Mean Sq","F value","Pr(>F)"],rowNames:[...E,"Residuals"],table:M}}return{stratum:`Error: ${e}`,colNames:["Df","Sum Sq","Mean Sq"],rowNames:E,table:v.map(({df:x,ss:M})=>[x,y(M[0]),y(M[0]/x)])}};return[g(a),g(y=>y),N]}function Hm(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=ws({response:t.response,rhs:i},Object.keys(o)).variables,a=jn(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(k=>!Number.isFinite(k)))throw new RangeError("NA/NaN/Inf in 'y'");let f=jn({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(k=>!a.warnings.includes(k))],h=f.colNames.length,p=f.colNames.map((k,F)=>qe([f.x.map(I=>I[F])]).groups[0]),w=f.x.map((k,F)=>p.map(I=>I[F])),d=h>0?Lt(w):void 0,b=d?.rank??0;b<h&&u.push("Error() model is singular");let N=d?.pivot.slice(0,b)??[],v=k=>k.map(F=>[F,0]),E=a.colNames.map((k,F)=>v(qe([a.x.map(I=>I[F])]).groups[0])),{groups:g,k:y}=qe([l]),{fit:x,qtv:M}=Kn(N.map(k=>v(p[k])),v(g[0]),E),R=g[0].map(k=>[1,k]),T=E.map(k=>Yr(k)[0]),S=[];N.forEach((k,F)=>{let I=f.assign[k],O=I===0?"(Intercept)":f.terms.termLabels[I-1],q=S.find($=>$.name===O);q===void 0&&S.push(q={name:O,idx:[]}),q.idx.push(F)}),m>b&&S.push({name:"Within",idx:Array.from({length:m-b},(k,F)=>b+F)});let A=S.map(k=>nw(k.name,k.idx.map(F=>x.qty[F]),M.map(F=>k.idx.map(I=>F[I])),a.assign,a.terms.termLabels,T,y,R)),C=A.map(k=>k[0]),L={formula:e,multistratum:!0,strata:C,rows:a.frame.rows,warnings:u};return Ut.set(L,A.map(k=>k[1])),A.some((k,F)=>k[2]&&S[F].name!=="(Intercept)")&&Ti.set(L,[xi]),L}var rw=["weights","contrasts","levels","intercept"],ow=["level"],sw=["interval","level"],Qi=2**-1022;function Zi(e,t,n){let r=t.y;if(r===void 0)throw new TypeError("the formula has no response");let o=Qm(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=>V(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 $o(e,t,n={}){if(B(n,rw,"lm"),typeof e=="string"){let r=jn(er(e),t,{...n,dropUnusedLevels:!0});return Q("lm",Oa(Zi(e,r,!1),$l(t,r.terms.variables)))}return aw(e,t,n)}function aw(e,t,n){let r=n.intercept??!0,o=e.length>0&&typeof e[0]!="number"?e.map(u=>Array.from(u)):Array.from(e,u=>[u]),s=Array.from(t);if(o.some(u=>u.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((u,h)=>!Number.isNaN(u)&&o[h].every(p=>!Number.isNaN(p))&&(i===void 0||!Number.isNaN(i[h]))),a=i?.filter((u,h)=>c[h]);if(a?.some(u=>u<0))throw new RangeError("missing or negative weights not allowed");let l=o.filter((u,h)=>c[h]).map(u=>r?[1,...u]:u),f=s.filter((u,h)=>c[h]),m=Qm(l,f,a,r).result;return m.rows=c.flatMap((u,h)=>u?[h]:[]),Oa(m,s.length)}function iw(e,t,n,r,o,s,i){let c=s.rank,a=s.pivot.slice(0,c),l=n?.map(g=>V(g,-r)),f=Zs(l===void 0?void 0:o.map(g=>l[g]),o.length).map((g,y)=>{if(n===void 0||l===void 0)return g;let x=n[o[y]];if(x===0||l[o[y]]>=Qi)return g;let M=Qs([x,0]);return[V(M[0],-r/2),V(M[1],-r/2)]}),m=a.map(g=>{let y=o.map((M,R)=>Et(f[R],e[M][g])),x=Fl(y.map(M=>M[0]));return{c:x===0?y:y.map(M=>[V(M[0],-x),V(M[1],-x)]),e:x}}),{qty:u,coef:h}=Hr(m.map(({c:g})=>g),o.map((g,y)=>Et(f[y],t[g]))),p=h.map((g,y)=>m[y].e===0?g:[V(g[0],-m[y].e),V(g[1],-m[y].e)]),w=e[0]?.map(()=>NaN)??[];a.forEach((g,y)=>w[g]=p[y][0]);let d=e.map(g=>{let y=[0,0];return a.forEach((x,M)=>y=he(y,Et(p[M],g[x]))),y}),b=Yr(u,c)[0],N=o.map(g=>l?.[g]??1),v=[0,0];if(i){let g=[0,0],y=[0,0];o.forEach((x,M)=>{g=he(g,[N[M],0]),y=he(y,Et(d[x],N[M]))}),v=rt(y,g)}let E=[0,0];return o.forEach((g,y)=>{let x=Le(d[g],v);E=he(E,Et(_e(x,x),N[y]))}),{coefficients:w,fittedValues:d.map(g=>g[0]),residuals:d.map((g,y)=>Le([t[y],0],g)[0]),effects:u.map(g=>g[0]),rss:b,mss:E[0]}}function Qm(e,t,n,r,o=!1){let s=qe([t.filter((l,f)=>n===void 0||n[f]!==0)]).k;if(s===0)return Ym(Gm(e,t,n,r,o),e,t,n);let i=Gm(e,t.map(l=>V(l,-s)),n,r,o),c=i.result,a=l=>V(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),Ym({...i,effects:i.effects.map(a),rss:V(i.rss,2*s),yScale:s,rssScaled:i.rss},e,t,n)}function Ym(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]=qa(f,0)?wo(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 Ji=new WeakMap;function ec(e){return Ji.get(e)??0}function cw(e){return e.qr.every(t=>t.every(Number.isFinite))&&e.qraux.every(Number.isFinite)}function Gm(e,t,n,r,o){if(t.length===0)throw new RangeError("0 (non-NA) cases");let s=t.map((D,Y)=>n===void 0||n[Y]!==0),i=e.filter((D,Y)=>s[Y]),c=t.filter((D,Y)=>s[Y]),a=n?.filter((D,Y)=>s[Y]),l=c.length;if(l===0)throw new RangeError("all weights are zero");if(i.some(D=>D.some(Y=>!Number.isFinite(Y))))throw new RangeError("NA/NaN/Inf in 'x'");if(c.some(D=>!Number.isFinite(D)))throw new RangeError("NA/NaN/Inf in 'y'");if(a?.some(D=>!Number.isFinite(D)))throw new RangeError("non-finite weights not allowed");let f=t.flatMap((D,Y)=>s[Y]?[Y]:[]),m=a===void 0?1:Nt(a),u=Number.isFinite(m)?2*Math.floor(Math.floor(Math.log2(m))/2):0,h=0,p=a?.map(Math.sqrt),w=p===void 0?i:i.map((D,Y)=>D.map(j=>j*p[Y])),d=Lt(w);a!==void 0&&Number.isFinite(m)&&u+2>0&&!cw(d)&&(h=u+2,p=a.map(D=>V(Math.sqrt(D),-h/2)),w=i.map((D,Y)=>D.map(j=>j*p[Y])),d=Lt(w),Ji.set(d,h));let b=p===void 0?c:c.map((D,Y)=>D*p[Y]),N=d.rank,v=D=>V(D,-u),E=o?iw(e,t,n,u,f,d,r):void 0,g=E?.coefficients??Ms(d,b),y=E?.residuals??[],x=E?.fittedValues??[],M=l-N,R=a?.map(v)??new Array(l).fill(1),T=E?.rss??0;if(E===void 0){let D=jr(d,b),Y=p===void 0?D:D.map((U,W)=>U/p[W]),j=0;t.forEach((U,W)=>{if(s[W])y.push(Y[j]),x.push(U-Y[j]),j++;else{let te=e[W].reduce((fe,Re,Ce)=>Number.isNaN(g[Ce])?fe:fe+Re*g[Ce],0);x.push(te),y.push(U-te)}});for(let U=0;U<l;U++)T+=R[U]*Y[U]**2}let S=x.filter((D,Y)=>s[Y]),A=0;if(a!==void 0&&a.some(D=>D!==0&&V(D,-u)<Qi)){let D=f.flatMap((Y,j)=>{let U=y[Y];return U===0?[]:[{w:a[j],r:U,a:Math.floor(Math.log2(Math.abs(U)))}]});if(D.length>0&&D.every(Y=>Number.isFinite(Y.r))){let Y=Math.max(...D.map(W=>Math.log2(W.w)+2*W.a)),j=2*Math.floor(Math.floor(Y)/2),U=0;for(let W of D)U+=V(W.w,2*W.a-j)*V(W.r,-W.a)**2;T=U,A=j-u}}let C=T/M,L=V(Math.sqrt(C),(u+A)/2),k=h===0?0:(h-u)/2,F=d.qr.slice(0,N).map(D=>D.slice(0,N).map(Y=>h===0?V(Y,-u/2):Y)),{inv:I,e:O}=N>0?Mo(F):{inv:[],e:[]},$=d.pivot.slice(0,N).map((D,Y)=>[D,Y]).sort((D,Y)=>D[0]-Y[0]),_=$.map(([D])=>g[D]),P=$.map(([,D])=>V(Math.sqrt(I[D][D]*C),A/2-O[D]-k)),K=_.map((D,Y)=>D/P[Y]),X=K.map(D=>2*dt(-Math.abs(D),M,!0,!1)),re={coefficients:g,residuals:y,fittedValues:x,rank:N,dfResidual:M,coefTable:{estimate:_,stdError:P,tValue:K,pValue:X},sigma:L,rSquared:0,adjRSquared:0,intercept:r,qr:d,x:e,rows:e.map((D,Y)=>Y)};n!==void 0&&(re.weights=n);let z=r?1:0,J=N-z,Z={result:re,fitRows:f,effects:(E?.effects??[]).map(D=>V(D,u/2)),rss:V(T,u+A)};if(J===0)return Z;let Ae=0;if(E!==void 0)Ae=E.mss;else if(r){let D=R.reduce((j,U)=>j+U,0),Y=0;for(let j=0;j<l;j++)Y+=R[j]*S[j]/D;for(let j=0;j<l;j++)Ae+=R[j]*(S[j]-Y)**2}else for(let D=0;D<l;D++)Ae+=R[D]*S[D]**2;re.rSquared=Ae/(Ae+V(T,A)),re.adjRSquared=1-(1-re.rSquared)*((l-z)/M);let ne=V(Ae/J/C,-A);return re.fStatistic={value:ne,numdf:J,dendf:M},re.fPValue=ht(ne,J,M,!1,!1),Z}function Zm(e,t){let n=(1-e)/2;return n<1e-8?gt(n,t,!1,!1):gt((1+e)/2,t,!0,!1)}function lw(e,t={}){B(t,ow,"confint");let n=t.level??.95;if(!(n>0&&n<1))throw new RangeError("'level' must be between 0 and 1");let r=Zm(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 Xm(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 V(V(e,-r)*V(t,-o),r+o+n)}function fw(e,t,n={}){B(n,sw,"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=-(Ji.get(e.qr)??0)/2,l=e.coefficients.flatMap((v,E)=>Number.isNaN(v)?[]:[E]),f=o.map(v=>{let E=v.reduce((g,y,x)=>Number.isNaN(e.coefficients[x])?g:g+y*e.coefficients[x],0);return qa(E,0)?wo(l.map(g=>v[g]),l.map(g=>e.coefficients[g]),E):E}),m=v=>{let E=new Array(s).fill(0);for(let g=0;g<s;g++){let y=v[g];for(let x=0;x<g;x++)y-=c[x][g]*E[x];E[g]=y/c[g][g]}return E},u=o.map(v=>{let E=i.map(x=>v[x]),g=Ia(m(E));if(g>=Qi&&g<1/0||!E.every(Number.isFinite)||E.every(x=>x===0))return a===0||!(g>0&&g<1/0)?g*e.sigma:Xm(g,e.sigma,a);let y=Math.floor(Math.log2(Nt(E.map(Math.abs))));for(let x of[0,600]){let M=Ia(m(E.map(R=>V(R,-(y+x)))));if(M<1/0)return M===0?M*e.sigma:Xm(M,e.sigma,y+x+a)}return g*e.sigma}),h=n.interval??"none";if(h!=="none"&&h!=="confidence"&&h!=="prediction")throw new RangeError(`invalid 'interval': ${String(h)}`);let p=Pl(e.qr,e.rank,o),w=p.length>0?{nonEstimable:p,warnings:[Dl]}:{};if(h==="none")return Q("predict",{fit:f,seFit:u,...w},e);let d=n.level??.95;if(!(d>0&&d<1))throw new RangeError("'level' must be between 0 and 1");let b=Zm(d,e.dfResidual),N=u.map(v=>b*(h==="prediction"?Math.hypot(v,e.sigma):v));return Q("predict",{fit:f,seFit:u,lwr:f.map((v,E)=>v-N[E]),upr:f.map((v,E)=>v+N[E]),...w},e)}var mw=["weights","contrasts","levels"],Jm=["confLevel"],eu=["sigmaX","sigmaY","mu","alternative","confLevel"];function uw(e,t,n={}){return B(n,mw,"aov"),typeof e=="string"?Q("aov",tu(e,t,n)):Q("aov",hw(e,t))}function tu(e,t,n){let r=er(e,!0),{treatment:o,errors:s}=Tl(r.rhs);if(s.length>1)throw new Error(`there are ${s.length} Error terms: only 1 is allowed`);return s.length===1?Hm(e,r,o,s[0],t,n):Zi(e,jn(r,t,{...n,dropUnusedLevels:!0},o),!0)}function hw(e,t){let{labels:n,groups:r}=nr(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 tu("y ~ g",{y:o,g:s},{levels:{g:n}})}function nu(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}=xt(e),i=t-1,c=n-t,a=r/i,l=o/c,f=a/l,m=u=>V(u,2*s);return{df:[i,c],sumSq:[m(r),m(o)],meanSq:[m(a),m(l)],fValue:f,pValue:c>0?ht(f,i,c,!1,!1):NaN}}function pw(e,t,n={}){ct(t)&&B(t,Jm,"TukeyHSD"),B(n,Jm,"TukeyHSD");let r;t!==void 0&&!Array.isArray(t)?n=t:r=t;let{labels:o,groups:s}=nr(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;cn(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}=xt(s),h=f/l,p=bl(c,i,l),w=[],d={names:[],i:[],j:[],diff:[],lwr:[],upr:[],pAdj:w,pAdjusted:w,warnings:[]};for(let b=0;b<i-1;b++)for(let N=b+1;N<i;N++){let v=m[N]-m[b],E=Math.sqrt(h/2*(1/s[N].length+1/s[b].length));d.names.push(`${o[N]}-${o[b]}`),d.i.push(N+1),d.j.push(b+1),d.diff.push(V(v,u)),d.lwr.push(V(v-p*E,u)),d.upr.push(V(v+p*E,u)),d.pAdj.push(hs(Math.abs(v)/E,i,l,{lowerTail:!1}))}return d}function dw(e,t,n={}){ct(t)&&B(t,eu,"zTest"),B(n,eu,"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 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(i))throw new RangeError("mu must be a number");let c=Ze(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=qe([l,f??[],[o,s??0]]),u=m.k,[h,p,[w,d]]=m.groups,b=Qn(h)[0],N=f===void 0?NaN:Qn(p)[0],v=f===void 0?w/Math.sqrt(l.length):Math.sqrt(w**2/l.length+d**2/f.length),E=f===void 0?b:b-N,g=(E-V(i,-u))/v,y,x;if(c==="less")y=se(g),x=[-1/0,E+le(a)*v];else if(c==="greater")y=se(g,{lowerTail:!1}),x=[E-le(a)*v,1/0];else{y=2*se(-Math.abs(g));let T=(1-a)/2,S=(T<1e-8?le(T,{lowerTail:!1}):le(1-(1-a)/2))*v;x=[E-S,E+S]}x=[V(x[0],u),V(x[1],u)];let M=f!==void 0,R=(T,S)=>{if(!(u>0&&T.some((C,L)=>C!==0&&Math.abs(S[L])<2**-1022)))return V(Qn(S)[0],u);let A=Math.max(qe([T]).k,0);return V(Qn(A===0?T:T.map(C=>V(C,-A)))[0],A)};return{method:M?"Two-sample z-Test":"One-sample z-Test",alternative:c,statistic:g,statisticName:"z",pValue:y,confInt:x,confLevel:a,estimate:M?[R(l,h),R(f,p)]:R(l,h),estimateNames:M?["mean of x","mean of y"]:"mean of x",nullValue:i,nullValueName:M?"difference in means":"mean",warnings:[]}}var gw=["parm","level","test"],ru={epsilon:1e-300,maxit:100,untilCoefficients:!0};function tc(e,t,n,r,o=1e-11){let s=[];for(let w=0;w<t.length;w++)n[w]>0&&s.push(w);let i=0,c=0;for(let w of s)i+=n[w],c+=n[w]*t[w];if(r&&!(Number.isFinite(i)&&Number.isFinite(c))){let w=0;for(let d of s)w=Math.max(w,n[d]);if(Number.isFinite(w)){let d=w>1?Math.floor(Math.log2(w)):0;i=0,c=0;for(let b of s){let N=V(n[b],-d);i+=N,c+=N*t[b]}}}let a=r?c/i:0,l=0;for(let w of s)l+=n[w]*(t[w]-a)**2;if((e.length>0?e[0].length:0)===0||s.length===0)return 0;let m=s.map(w=>Math.sqrt(n[w])),u=Lt(s.map((w,d)=>e[w].map(b=>b*m[d])),{tol:o}),h=jr(u,s.map((w,d)=>t[w]*m[d])),p=0;for(let w of h)p+=w*w;if(!Number.isFinite(l)){let w=s.map((b,N)=>m[N]*(t[b]-a)),d=0;for(let b of w)d=Math.max(d,Math.abs(b));for(let b of h)d=Math.max(d,Math.abs(b));if(Number.isFinite(d)){let b=d>1?Math.floor(Math.log2(d)):0,N=0;for(let E of w)N+=V(E,-b)**2;let v=0;for(let E of h)v+=V(E,-b)**2;return V(V(N-v,b),b)}}return l-p}function ww(e,t={}){B(t,gw,"glmConfint");let{fit:n,k:r}=Wn(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},(L,k)=>k);for(let L of c)if(!Number.isInteger(L)||L<0||L>=i)throw new RangeError(`invalid coefficient index ${L}`);let a=n.model,l=n.coefficients.map((L,k)=>k).filter(L=>!Number.isNaN(n.coefficients[L])),f=new Rt,m=Eo(a,l,f,{...ru,start:a.start===void 0?void 0:l.map(L=>a.start[L])}),u=new Array(i).fill(NaN);if(l.forEach((L,k)=>u[L]=m.fit.coefficients[k]),!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},w=ot(()=>za(p)),d=w.dispersion,b=new Array(i).fill(NaN);(m.fit.qr===void 0?[]:m.fit.qr.pivot.slice(0,m.fit.rank)).forEach((L,k)=>b[l[L]]=w.stdError[k]);let v=a.x.map(L=>l.map(k=>L[k])),E=m.fit.eta,g=(L,k)=>{let F=l.filter(q=>q!==L),I=Eo(a,F,new Rt,{...ru,start:void 0,etastart:E,extraOffset:a.x.map(q=>q[L]*k)}).fit;if(!I.converged)return h=!0,NaN;let O=s==="LRT"?I.deviance-m.fit.deviance:tc(v,I.workingResiduals,I.workingWeights,!1);return Math.sign(k-u[L])*Math.sqrt(Math.max(O/d,0))},y=(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]:[le(y),y<1e-8?le(y,{lowerTail:!1}):le((1+o)/2)],S=c.map(()=>NaN),A=c.map(()=>NaN),C=(L,k,F)=>{let I=F===0?-1:1;h=!1;let O=NaN,q=NaN;for(let z=0;z<=30;z++){let J=u[L]+I*2**z*b[L],Z=g(L,J);if(Math.abs(Z)>=Math.abs(k)){O=J,q=Z;break}}if(Number.isNaN(O)||h)return NaN;let $=z=>z===u[L]?-k:g(L,z)-k,[_,P]=F===0?[O,u[L]]:[u[L],O],[K,X]=F===0?[q-k,-k]:[-k,q-k],re;try{re=Kt($,_,P,{tol:1e-14*b[L],maxIter:500,fLower:K,fUpper:X})}catch(z){if(!h)throw z;re=NaN}return h?NaN:re};return c.forEach((L,k)=>{if(Number.isNaN(u[L]))return;if(!R){S[k]=C(L,T[0],0),A[k]=C(L,T[1],1);return}let F=[-2*M,-M,0,M,2*M],I=[C(L,-2*M,0),C(L,-M,0),u[L],C(L,M,1),C(L,2*M,1)].map(q=>q-u[L]),O=q=>{let $=0;for(let _=0;_<5;_++){let P=1;for(let K=0;K<5;K++)K!==_&&(P*=(q-F[K])/(F[_]-F[K]));$+=P*I[_]}return u[L]+$};S[k]=O(T[0]),A[k]=O(T[1])}),{lower:S.map(L=>V(L,r)),upper:A.map(L=>V(L,r))}}var bw=["type","omega","sandwich"],vw=["cluster","type","cadjust","multi0","fix","sandwich"],Js=2**-52;function oc(e,t){if(t===0)return e;let n=ac.get(e)??{mant:e,exp:e.map(()=>0)};return Ar(n.mant,n.exp.map(r=>r+t))}var ea=e=>Math.floor(Math.log2(Math.abs(e)));function fn(e){if(e===0||!Number.isFinite(e))return[e,0];let t=ea(e);return[V(e,-t),t]}var zo=(e,t)=>[e[0]*t[0],e[1]+t[1]];function ar(e,t=!1){let n=-1/0;for(let[o,s]of e)o!==0&&Number.isFinite(o)&&(n=Math.max(n,s+ea(o)));let r=n===-1/0?0:n;return t&&(r=2*Math.floor(r/2)),{v:e.map(([o,s])=>Ve(o,s-r)),s:r}}var ir=(e,t)=>Math.abs(t)<=2046?V(e,t):Ve(e,t),ac=new WeakMap;function Ar(e,t){let n=e.map((r,o)=>r.map((s,i)=>ir(s,t[o]+t[i])));return ac.set(n,{mant:e,exp:t}),n}function ic(e,t,n){let r=ac.get(e),o=r?.mant[t]?.[t];if(!(r===void 0||o===void 0||!Object.is(ir(o,2*r.exp[t]),n)))return ir(Math.sqrt(o),r.exp[t])}function cr(e){return typeof e=="object"&&e!==null&&"family"in e&&"model"in e}function ta(e){if(!(typeof e=="object"&&e!==null&&Array.isArray(e.coefficients)&&"qr"in e&&(cr(e)||"sigma"in e&&"x"in e&&"rows"in e)))throw new TypeError("'fit' must be the result of lm or glm")}function iu(e,t){return e.pivot.slice(0,t).map((n,r)=>[n,r]).sort((n,r)=>n[0]-r[0])}function cu(e,t){let{inv:n,e:r}=Mo(e.qr.slice(0,t).map(s=>s.slice(0,t))),o=iu(e,t);return{inv:o.map(([,s])=>o.map(([,i])=>n[s][i])),e:o.map(([,s])=>r[s]),cols:o.map(([s])=>s)}}function ou(e,t,n){let{inv:r,e:o,cols:s}=cu(e,t);return{A:r.map((i,c)=>i.map((a,l)=>V(a,-o[c]-o[l]-n))),cols:s}}function su(e,t){let n=e.qr.slice(0,t).map(i=>i.slice(0,t)),r=n.map((i,c)=>{let a=0;for(let l=0;l<=c;l++)a=Math.max(a,Math.abs(n[l][c]));return a>0&&Number.isFinite(a)?ea(a):0}),{inv:o}=Mo(n.map(i=>i.map((c,a)=>V(c,-r[a])))),s=iu(e,t);return{B:s.map(([,i])=>s.map(([,c])=>o[i][c])),e:s.map(([,i])=>r[i])}}function lu(e,t){let n=e.qr.length;return Ol(e,Array.from({length:n},(r,o)=>Array.from({length:t},(s,i)=>o===i?1:0)))}function Nw(e,t){let n=e.qr.length;return t===0?new Array(n).fill(0):lu(e,t).map(o=>{let s=o.reduce((i,c)=>i+c*c,0);return s>1-10*Js?1:s})}function au(e,t,n,r){return n.map((o,s)=>{if(o>=1)return 0;let i=r(s);if(o<=.5||e===void 0||i<0)return 1-o;let c=e.qr.length,a=new Array(c).fill(0);a[i]=1;let l=Ca(e,a),f=0;for(let m=t;m<c;m++)f+=l[m]*l[m];return f})}function na(e){if(cr(e))return e.model.columnNames;let t=e.coefNames;if(t!==void 0)return t;let n=e.coefficients.length;return e.intercept?["(Intercept)",...Array.from({length:n-1},(r,o)=>`x${o+1}`)]:Array.from({length:n},(r,o)=>`x${o+1}`)}function cc(e){let t=na(e);if(e.qr===void 0||e.rank===0)return{vcov:[],names:[]};let{inv:n,e:r,cols:o}=cu(e.qr,e.rank),s=cr(e)?0:ec(e.qr),i,c;if(cr(e)){let l=Wn(e),f=$a(e).fixedDispersion?1:xo(l.fit),m=f===0||!Number.isFinite(f)?0:2*Math.floor(ea(f)/2);i=V(f,-m),c=m+2*l.k}else{let[l,f]=fn(e.sigma);i=l*l,c=2*f}let a=n.map(l=>l.map(f=>f*i));return{vcov:Ar(a,r.map(l=>(c-s)/2-l)),names:o.map(l=>t[l])}}function fu(e,t=!1){let n=na(e);if(cr(e)){let w=t?Wn(e):{fit:e,k:0},d=w.fit,b=$a(d),N=d.priorWeights,v=N.map(I=>I>0),E=N.flatMap((I,O)=>I>0?[O]:[]),{A:g,cols:y}=d.qr===void 0||d.rank===0?{A:[],cols:[]}:ou(d.qr,d.rank,0),x=E.map(I=>y.map(O=>d.model.x[I][O])),M=E.map(I=>d.weights[I]),R=E.map(I=>d.weights[I]*d.residuals[I]),T=b.family==="binomial"||b.family==="poisson",S=T?1:R.reduce((I,O)=>I+O*O,0)/M.reduce((I,O)=>I+O,0),A=d.model.rows,C=ja(d),L=jl(d),k=()=>{let I=Wn(d),O=E.map(z=>I.fit.residuals[z]),q=M.map(Math.sqrt),{B:$,e:_}=d.qr===void 0||d.rank===0?{B:[],e:[]}:su(d.qr,d.rank),P=x.map((z,J)=>z.map((Z,Ae)=>V(q[J]*Z,-_[Ae]))),{v:K,s:X}=ar(q.map((z,J)=>z===0?[0,0]:zo(fn(z),fn(O[J])))),re=()=>{let z=[1,0];if(!T){let J=ar(M.map((Ae,ne)=>Ae===0?[0,0]:zo(fn(Ae),fn(O[ne])))),Z=ar(M.map(fn));z=[J.v.reduce((Ae,ne)=>Ae+ne*ne,0)/Z.v.reduce((Ae,ne)=>Ae+ne,0),2*J.s-Z.s+2*I.k]}return{T:x.map(J=>J.map((Z,Ae)=>ir(Z,-_[Ae]))),factor:[z[0]*z[0],2*z[1]]}};return{T:P,B:$,e:_,u:K,s:X+I.k,omegaPart:re}},F;if(qr(S)&&R.every(Number.isFinite))F=R.map(I=>I/S);else{let I=Wn(d),O=ar(M.map((q,$)=>q===0?[0,0]:zo(fn(q),fn(I.fit.residuals[E[$]]))));if(T)F=O.v.map(q=>ir(q,O.s+I.k));else{let q=ar(M.map(fn)),$=O.v.reduce((_,P)=>_+P*P,0)/q.v.reduce((_,P)=>_+P,0);F=O.v.map(_=>ir(_/$,q.s-O.s-I.k))}}return{glm:!0,names:y.map(I=>n[I]),X:x,W:M,res:F,phiS:S,A:g,h:C,omh:au(d.qr,d.rank,C,I=>L[I]),qr:d.qr,rank:d.rank,qrRow:I=>L[I],inputRows:E.map(I=>A[I]),nInput:Da(e,A),nonZero:v,dispersion:()=>xo(d),scaled:k,k:w.k}}let r=e,o=r.weights,s=r.residuals.map((w,d)=>o===void 0||o[d]!==0),i=s.flatMap((w,d)=>w?[d]:[]),c=ec(r.qr),{A:a,cols:l}=r.rank===0?{A:[],cols:[]}:ou(r.qr,r.rank,c),f=i.map(w=>l.map(d=>r.x[w][d])),m=i.map(w=>o?.[w]??1),u=i.map((w,d)=>m[d]*r.residuals[w]),h=Nw(r.qr,r.rank),p=()=>{let w=m.map(y=>c===0?Math.sqrt(y):V(Math.sqrt(y),-c/2)),{B:d,e:b}=r.rank===0?{B:[],e:[]}:su(r.qr,r.rank),N=f.map((y,x)=>y.map((M,R)=>V(w[x]*M,-b[R]))),{v,s:E}=ar(w.map((y,x)=>zo(fn(y),fn(r.residuals[i[x]]))));return{T:N,B:d,e:b,u:v,s:E,omegaPart:()=>({T:f.map(y=>y.map((x,M)=>ir(x,-b[M]))),factor:[1,-2*c]})}};return{glm:!1,names:l.map(w=>n[w]),X:f,W:m,res:u,phiS:1,A:a,h,omh:au(r.qr,r.rank,h,w=>w),qr:r.qr,rank:r.rank,qrRow:w=>w,inputRows:i.map(w=>r.rows[w]),nInput:Da(r,r.rows),nonZero:s,dispersion:()=>r.sigma*r.sigma,scaled:p,k:0}}var lc=2**-969,qr=e=>Math.abs(e)>=lc&&Math.abs(e)<1/0;function mu(e,t,n,r){if(!(e.phiS>=lc&&e.phiS<1/0)||!qr(e.X.length*e.phiS*e.phiS))return!1;for(let o=0;o<n.length;o++)if(!qr(e.A[o][o])||!r&&(!qr(t[o][o])||!qr(n[o][o]))||!n[o].every(Number.isFinite))return!1;return!0}function jo(e,t,n){let r=e.length,s=e.map(i=>Array.from({length:r},(c,a)=>i.reduce((l,f,m)=>l+f*t[m][a],0))).map(i=>Array.from({length:r},(c,a)=>n*i.reduce((l,f,m)=>l+f*e[m][a],0)));for(let i=0;i<r;i++)for(let c=0;c<i;c++)s[i][c]=s[c][i];return s}function Gr(e,t,n){let r=e[0]?.length??0,o=Array.from({length:r},()=>new Array(r).fill(0));e.forEach((s,i)=>{let c=t[i];for(let a=0;a<r;a++)for(let l=a;l<r;l++)o[a][l]+=c*s[a]*s[l]});for(let s=0;s<r;s++)for(let i=s;i<r;i++)o[s][i]=o[i][s]=o[s][i]/n;return o}function nc(e){let t=e.slice(0,10).map(n=>String(n+1));return e.length>10?`${t.join(", ")}, ...`:t.join(", ")}var yw=["HC3","const","HC","HC0","HC1","HC2","HC4","HC4m","HC5"];function Mw(e,t={}){B(t,bw,"vcovHC"),ta(e);let n=Se(t.type,yw),r=n==="HC"?"HC0":n,o=t.sandwich??!0;if(typeof o!="boolean")throw new TypeError("'sandwich' must be true or false");let s=fu(e,t.omega===void 0),i=s.X.length,c=s.names.length,a=[],{res:l,h:f,omh:m}=s,u,h;if(t.omega!==void 0){let w;if(typeof t.omega=="function"?w=t.omega(l.slice(),f.slice(),i-c):(w=t.omega,w.length!==i&&w.length===s.nonZero.length&&(w=w.filter((d,b)=>s.nonZero[b]))),!Array.isArray(w)&&!ArrayBuffer.isView(w))throw new TypeError("'omega' must give an array of numbers");if(w.length!==i)throw new RangeError(`'omega' must have one value per observation (${i})`);if(u=Array.from(w,Number),u.some(d=>!Number.isFinite(d)))throw new RangeError("'omega' must be finite")}else if(r==="const"){let w=s.dispersion();u=s.W.map(d=>w*d/(s.phiS*s.phiS))}else{let w=f.flatMap((N,v)=>N>1-Math.sqrt(Js)?[s.inputRows[v]]:[]);if(w.length>0&&a.push(r==="HC0"||r==="HC1"?`${r} covariances become (close to) singular if hat values are (close to) 1 as for observation(s) ${nc(w)}`:`${r} covariances are numerically unstable for hat values close to 1 (and undefined if exactly 1) as for observation(s) ${nc(w)}`),r!=="HC0"&&r!=="HC1"){let N=f.flatMap((v,E)=>v>=1?[s.inputRows[E]]:[]);if(N.length>0)throw new RangeError(`${r} covariances are undefined for hat values of 1 as for observation(s) ${nc(N)}`)}if(r==="HC1"&&i<=c)throw new RangeError("HC1 covariances need more observations than coefficients");let d=Math.round(f.reduce((N,v)=>N+v,0)),b=f.reduce((N,v)=>Math.max(N,v),0);h=N=>{switch(r){case"HC0":return N;case"HC1":return N.map(v=>v*i/(i-c));case"HC2":return N.map((v,E)=>v/m[E]);case"HC3":return N.map((v,E)=>v/m[E]**2);case"HC4":return N.map((v,E)=>v/m[E]**Math.min(4,i*f[E]/d));case"HC4m":return N.map((v,E)=>v/m[E]**(Math.min(1,i*f[E]/d)+Math.min(1.5,i*f[E]/d)));default:return N.map((v,E)=>v/Math.sqrt(m[E]**Math.min(i*f[E]/d,Math.max(4,i*.7*b/d))))}},u=h(l.map(N=>N*N))}let p;if(o&&r==="const"&&t.omega===void 0)p=cc(e).vcov;else if(o){let w=Gr(s.X,u,i);if(p=jo(s.A,w,i*s.phiS*s.phiS),!mu(s,w,p,(h===void 0?u:l).every(d=>d===0))){let d=s.scaled();if(h!==void 0){let b=jo(d.B,Gr(d.T,h(d.u.map(N=>N*N)),i),i);p=Ar(b,d.e.map(N=>d.s-N))}else{let{T:b,factor:N}=d.omegaPart(),v=ar(u.map(E=>zo(fn(E),N)),!0);p=Ar(jo(d.B,Gr(b,v.v,i),i),d.e.map(E=>v.s/2-E))}}p=oc(p,s.k)}else{let w=Gr(s.X,u,i);if(pu(w,(h===void 0?u:l).every(d=>d===0)))p=w;else{let d=s.scaled();if(h!==void 0){let{mant:b,exp:N}=du(d,Gr(d.T,h(d.u.map(v=>v*v)),i));p=Ar(b,N)}else{let b=ar(u.map(fn),!0);p=Ar(Gr(d.omegaPart().T,b.v,i),d.e.map(N=>b.s/2+N))}}p=oc(p,-s.k)}return Q("vcovHC",{vcov:p,names:s.names,warnings:a},e)}var Ew=["HC","HC0","HC1","HC2","HC3"];function xw(e){if(e==null||typeof e=="number"&&Number.isNaN(e))throw new RangeError("cannot handle NAs in 'cluster': either refit the model without the NA observations in 'cluster' or impute the NAs");if(typeof e=="boolean")return e?"TRUE":"FALSE";if(typeof e=="number")return vt(e);if(typeof e=="string")return e;throw new TypeError("cluster labels must be numbers, strings or booleans")}function Rw(e){let t=new Map;return{g:e.map(r=>{let o=t.get(r);return o===void 0&&(o=t.size,t.set(r,o)),o}),G:t.size}}function Tw(e){let t=[],n=(r,o,s)=>{if(s.length===r){t.push(s.slice());return}for(let i=o;i<e;i++)s.push(i),n(r,i+1,s),s.pop()};for(let r=1;r<=e;r++)n(r,0,[]);return t}var uu=Js**(1/1.3);function hu(e,t){let{values:n,vectors:r}=Bt(e,{symmetric:!0});if(n.some(i=>!(i>=uu)))return;let o=r,s=n.map(i=>i**t);return e.map((i,c)=>e.map((a,l)=>o[c].reduce((f,m,u)=>f+m*s[u]*o[l][u],0)))}function Sw(e){return{A:e.A,xs:t=>e.X[t].map(n=>n*Math.sqrt(e.W[t])),rs:t=>{let n=Math.sqrt(e.W[t]);return n===0?0:e.res[t]/n},single:(t,n,r)=>e.X[t].map(o=>n*e.res[t]*r*o),psi:t=>e.X[t].map(n=>n*e.res[t])}}function Aw(e){return{A:e.B,xs:t=>e.T[t],rs:t=>e.u[t],single:(t,n,r)=>e.T[t].map(o=>n*e.u[t]*r*o),psi:t=>e.T[t].map(n=>n*e.u[t])}}function qw(e,t,n,r,o){let s=e.names.length,i=Array.from({length:r},()=>[]);n.forEach((f,m)=>i[f].push(m));let c=Math.sqrt((r-1)/r),a=r===n.length,l;return i.map((f,m)=>{if(f.length===1){let N=f[0];if(a?e.h[N]>=1:!(e.omh[N]>=uu))throw rc(m);return t.single(N,c,e.omh[N]**o)}let u=f.map(t.xs),h=f.map(t.rs);if(e.qr!==void 0&&e.rank>0){l??=lu(e.qr,e.rank);let N=kw(e,l,f,h,o);if(N===null)throw rc(m);if(N!==void 0)return Array.from({length:s},(v,E)=>c*u.reduce((g,y,x)=>g+y[E]*N[x],0))}let p=u.map(N=>Array.from({length:s},(v,E)=>N.reduce((g,y,x)=>g+y*t.A[x][E],0))),w=u.map((N,v)=>u.map((E,g)=>(v===g?1:0)-p[v].reduce((y,x,M)=>y+x*E[M],0)));for(let N=0;N<w.length;N++)for(let v=0;v<N;v++)w[N][v]=w[v][N]=(w[N][v]+w[v][N])/2;let d=hu(w,o);if(d===void 0)throw rc(m);let b=d.map(N=>N.reduce((v,E,g)=>v+E*h[g],0));return Array.from({length:s},(N,v)=>c*u.reduce((E,g,y)=>E+g[v]*b[y],0))})}function kw(e,t,n,r,o){let s=e.qr,i=e.rank,c=new Array(i).fill(0),a=n.map(g=>{let y=e.qrRow(g);return y<0?c:t[y]}),l=Array.from({length:i},(g,y)=>Array.from({length:i},(x,M)=>a.reduce((R,T)=>R+T[y]*T[M],0))),{values:f,vectors:m}=Bt(l,{symmetric:!0});if(!f.some(g=>g>.5))return;let u=m,h=r.slice(),p=[];f.forEach((g,y)=>{let x=a.map(M=>M.reduce((R,T,S)=>R+T*u[S][y],0));if(g>.5)p.push(x.map(M=>M/Math.sqrt(g)));else if(g>0){let R=Math.expm1(o*Math.log1p(-g))/g*x.reduce((T,S,A)=>T+S*r[A],0);x.forEach((T,S)=>h[S]+=R*T)}});let w=s.qr.length,d=p.map(g=>{let y=new Array(w).fill(0);return n.forEach((x,M)=>{let R=e.qrRow(x);R>=0&&(y[R]=g[M])}),Ca(s,y).slice(i)}),b=d.map(g=>d.map(y=>g.reduce((x,M,R)=>x+M*y[R],0))),N=hu(b,o);if(N===void 0)return null;let v=p.map(g=>g.reduce((y,x,M)=>y+x*r[M],0)),E=N.map((g,y)=>g.reduce((x,M,R)=>x+M*v[R],0)-v[y]);return p.forEach((g,y)=>g.forEach((x,M)=>h[M]+=E[y]*x)),h}function rc(e){return new RangeError(`clustered HC2/HC3 covariances are undefined: the block I - H_gg of cluster ${e+1} (in order of appearance) is singular, as with cluster fixed effects`)}function Iw(e,t){let n=Array.from({length:t},()=>new Array(t).fill(0));for(let r of e)for(let o=0;o<t;o++)for(let s=0;s<t;s++)n[o][s]+=r[o]*r[s];return n}function sc(e){if(e.length===0)return e;let{values:t,vectors:n}=Bt(e,{symmetric:!0});if(t.every(s=>s>=0))return e;let r=n,o=t.map(s=>Math.max(s,0));return e.map((s,i)=>e.map((c,a)=>r[i].reduce((l,f,m)=>l+f*o[m]*r[a][m],0)))}function _w(e,t){let n=e.length;if(n===0)return{mant:e,exp:t};let r=e.map((a,l)=>a[l]!==0?Math.sqrt(Math.abs(a[l])):1),o=e.map((a,l)=>a.map((f,m)=>f/r[l]/r[m])),{values:s}=Bt(o,{symmetric:!0}),i=s.reduce((a,l)=>Math.max(a,Math.abs(l)),0);if(s.every(a=>a>=-16*n*Js*i))return{mant:e,exp:t};let c=Math.max(...t);return{mant:sc(e.map((a,l)=>a.map((f,m)=>ir(f,t[l]+t[m]-2*c)))),exp:t.map(()=>c)}}function pu(e,t){return t||e.every((n,r)=>qr(n[r])&&n.every(Number.isFinite))}function du(e,t){let[n,r]=e.omegaPart().factor;return{mant:t.map(o=>o.map(s=>s/n)),exp:e.e.map(o=>e.s+o-r/2)}}function Lw(e,t={}){B(t,vw,"vcovCL"),ta(e);let n=fu(e,!0),r=n.X.length,o=n.names.length,s=t.type===void 0?n.glm?"HC0":"HC1":Se(t.type,Ew),i=s==="HC"?"HC0":s,c=t.cadjust??!0,a=t.multi0??!1,l=t.fix??!1,f=t.sandwich??!0;for(let[v,E]of[["cadjust",c],["multi0",a],["fix",l],["sandwich",f]])if(typeof E!="boolean")throw new TypeError(`'${v}' must be true or false`);let m,u=t.cluster;if(u===void 0)m=[n.inputRows.map(String)];else{let v=y=>Array.isArray(y)||ArrayBuffer.isView(y);if(!v(u))throw new TypeError("'cluster' must be an array of labels or an array of such arrays");let g=u.length>0&&Array.from(u).every(v)?Array.from(u):[u];if(g.length===0)throw new RangeError("'cluster' has no dimension");m=g.map(y=>{if(y.length!==n.nInput)throw new RangeError("number of observations in 'cluster' and 'estfun()' do not match");return n.inputRows.map(x=>xw(y[x]))})}if(i==="HC1"&&r<=o)throw new RangeError("HC1 covariances need more observations than coefficients");let h=m.length,p=Tw(h).map((v,E)=>{let g=a&&h>1&&E===2**h-2,y=g?n.inputRows.map(String):n.inputRows.map((T,S)=>v.length===1?m[v[0]][S]:JSON.stringify(v.map(A=>m[A][S]))),{g:x,G:M}=Rw(y);if(M<2)throw new RangeError("at least two clusters are needed");let R=g?i==="HC1"?(r-o)/(r-1):1:c?M/(M-1):1;return{g:x,G:M,c:R,sign:v.length%2===1?1:-1}}),w=v=>{let E=Array.from({length:o},()=>new Array(o).fill(0));for(let{g,G:y,c:x,sign:M}of p){let R;i==="HC2"||i==="HC3"?R=qw(n,v,g,y,i==="HC2"?-.5:-1):y===r?R=n.X.map((S,A)=>v.psi(A)):(R=Array.from({length:y},()=>new Array(o).fill(0)),n.X.forEach((S,A)=>{let C=R[g[A]],L=v.psi(A);for(let k=0;k<o;k++)C[k]+=L[k]}));let T=Iw(R,o);E=E.map((S,A)=>S.map((C,L)=>C+M*x*T[A][L]/r))}return i==="HC1"&&(E=E.map(g=>g.map(y=>y*(r-1)/(r-o)))),E},d=n.res.every(v=>v===0),b=n.phiS>=lc&&n.phiS<1/0&&n.A.every((v,E)=>qr(v[E]))&&n.res.every(Number.isFinite),N;if(b){let v=w(Sw(n));if(!f)pu(v,d)&&(N=l?sc(v):v);else{let E=jo(n.A,v,r*n.phiS*n.phiS);mu(n,v,E,d)&&(N=l?sc(E):E)}}if(N===void 0){let v=n.scaled(),E=w(Aw(v)),{mant:g,exp:y}=f?{mant:jo(v.B,E,r),exp:v.e.map(M=>v.s-M)}:du(v,E),x=l?_w(g,y):{mant:g,exp:y};N=Ar(x.mant,x.exp)}return N=oc(N,f?n.k:-n.k),Q("vcovCL",{vcov:N,names:n.names,warnings:[]},e)}var Cw=["vcov","df"],Fw=["parm","level","vcov","df"];function gu(e){return typeof e=="object"&&e!==null&&!Array.isArray(e)&&Array.isArray(e.vcov)&&Array.isArray(e.names)}function wu(e){if(!Array.isArray(e)||e.some(n=>!Array.isArray(n)&&!ArrayBuffer.isView(n)))throw new TypeError("'vcov' must be a matrix (an array of rows)");let t=e.map(n=>Array.from(n,Number));if(t.some(n=>n.length!==t.length))throw new RangeError("'vcov' must be a square matrix");return t}var bu=e=>new Set(e).size===e.length;function vu(e,t){ta(e);let n=e.coefficients,r=na(e),o=n.length,s=n.map(m=>Number.isNaN(m)),i,c,a=[],l;if(t===void 0){let m=cc(e),u=new Map(m.names.map((p,w)=>[p,w])),h=r.map((p,w)=>s[w]?void 0:u.get(p));i=h.map(p=>h.map(w=>p===void 0||w===void 0?NaN:m.vcov[p][w])),c=r,l=h.map(p=>p===void 0?NaN:ic(m.vcov,p,m.vcov[p][p])??Math.sqrt(m.vcov[p][p]))}else{let m=typeof t=="function"?ot(()=>t(e)):t,u=gu(m)?m.vcov:m;if(gu(m)){if(i=wu(m.vcov),c=m.names.slice(),c.length!==i.length)throw new RangeError("'vcov' must have one name per row");a=[...m.warnings??[]]}else i=wu(m);l=i.map((h,p)=>{let w=h[p],d=typeof u=="object"&&u!==null?ic(u,p,w):void 0;if(d!==void 0&&!Number.isNaN(d))return d;if(!Number.isFinite(w))throw new RangeError(`the variance of coefficient ${p+1} in 'vcov' is not a finite number`);if(w<0)throw new RangeError(`the variance of coefficient ${p+1} in 'vcov' is negative`);return Math.sqrt(w)})}if(c!==void 0&&bu(c)&&bu(r)){let m=new Map(c.map((h,p)=>[h,p])),u=r.flatMap((h,p)=>m.has(h)?[p]:[]);return{est:u.map(h=>n[h]),se:u.map(h=>l[m.get(r[h])]),names:u.map(h=>r[h]),warnings:a}}let f;if(i.length===o)f=n.map((m,u)=>u);else if(i.length===e.rank)f=n.flatMap((m,u)=>s[u]?[]:[u]);else{let m=e.rank===o?`${o} x ${o}`:`${o} x ${o} or ${e.rank} x ${e.rank}`;throw new RangeError(`'vcov' must be ${m}, one row per coefficient`)}return{est:f.map(m=>n[m]),se:l,names:f.map(m=>r[m]),warnings:a}}function Nu(e,t){if(t===void 0)return cr(e)?1/0:e.dfResidual;if(typeof t!="number")throw new TypeError("'df' must be a number");if(Number.isNaN(t))throw new RangeError("'df' must not be NaN");return t}var yu=e=>Number.isFinite(e)&&e>0;function Ow(e){return cr(e)?e.priorWeights.filter(t=>t!==0).length:e.rank+e.dfResidual}function Dw(e,t={}){B(t,Cw,"coeftest");let{est:n,se:r,names:o,warnings:s}=vu(e,t.vcov),i=Nu(e,t.df),c=yu(i),a=n.map((f,m)=>f/r[m]),l=a.map(f=>Number.isNaN(f)?NaN:c?2*dt(-Math.abs(f),i,!0,!1):2*se(-Math.abs(f)));return Q("coeftest",{estimate:n,stdError:r,statistic:a,pValue:l,names:o,statisticName:c?"t value":"z value",df:i,method:c?"t test of coefficients":"z test of coefficients",nobs:Ow(e),warnings:s},e,typeof t.vcov=="function"?void 0:t.vcov)}function Pw(e){let t=[100*e,100*(1-e)],n=t.map(o=>{if(o===0||!Number.isFinite(o))return 0;let s=Math.floor(Math.log10(Math.abs(o))),i=o.toFixed(Math.max(0,2-s));return(i.includes(".")?i.replace(/0+$/,"").split(".")[1]:"").length}),r=Math.max(...n);return[`${t[0].toFixed(r)} %`,`${t[1].toFixed(r)} %`]}function Vw(e,t={}){B(t,Fw,"coefci");let n=t.level??.95;if(typeof n!="number")throw new TypeError("'level' must be a number");if(!(n>=0&&n<=1))throw new RangeError("'level' must be between 0 and 1");let r=vu(e,t.vcov),o=Nu(e,t.df),s=r.est.map((m,u)=>u),i=t.parm;if(i!==void 0){let m=Array.isArray(i)?i:[i];if(m.every(u=>typeof u=="string")){let u=new Set(m);s=s.filter(h=>u.has(r.names[h]))}else if(m.every(u=>typeof u=="number"))s=m.map(u=>{if(!Number.isInteger(u)||u<0)throw new RangeError("'parm' must hold 0-based indices or names");if(u>=r.est.length)throw new RangeError("subscript out of bounds");return u});else throw new TypeError("'parm' must hold 0-based indices or names")}let c=(1-n)/2,[a,l]=yu(o)?[gt(c,o,!0,!1),c<1e-8?gt(c,o,!1,!1):gt(1-c,o,!0,!1)]:[le(c),c<1e-8?le(c,{lowerTail:!1}):le(1-c)],f=(m,u,h)=>m+h*u;return Q("coefci",{lower:s.map(m=>f(r.est[m],r.se[m],a)),upper:s.map(m=>f(r.est[m],r.se[m],l)),names:s.map(m=>r.names[m]),colNames:Pw(c),warnings:r.warnings},e,typeof t.vcov=="function"?void 0:t.vcov)}var $w=["test","dispersion"],zw=(e,t)=>wt(e,t/2,2,!1,!1);function jw(e,t,n){let{fit:r,w:o}=Eo(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 fc=(e,t)=>tc(t.x,e.fit.workingResiduals,e.fit.workingWeights,e.intercept);function Ww(e,t={}){B(t,$w,"glmAnova");let n=Array.isArray(e)?e:[e];if(n.length===0)throw new RangeError("no fits to compare");let r=n.map(Wn),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:zl(t.dispersion,o),c=i?.s??0,a=Bw(s,Array.isArray(e)&&s.length>1,{...t,...i===void 0?{}:{dispersion:i.m}},c);if(o===0&&c===0)return Q("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]?V(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"),w=s[0].model.x.length;for(let d of m)d[u]=d[h]+2*(w-d[p])*t.dispersion}return Q("glmAnova",{...a,table:m},...n)}function Bw(e,t,n,r=0){let o=new Rt,s=e[0],i=Pa(s.model.familySpec);if(e.some(M=>{let R=Pa(M.model.familySpec);return R.family!==i.family||R.name!==i.name||R.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 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,C)=>JSON.stringify(A)===JSON.stringify(S[C]));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,C]=f[S]>0?[e[S-1],T]:[T,e[S-1]],L=I=>I.filter((O,q)=>A.priorWeights[q]>0&&C.priorWeights[q]>0),k=A.model.offset.map((I,O)=>I-C.model.offset[O]),F=k.some((I,O)=>I!==0&&A.priorWeights[O]>0)?A.model.x.map((I,O)=>[...I,k[O]]):A.model.x;Es(L(F),L(C.model.x))||h.add(S)}),a==="Rao"){let T=e.map(S=>({dfRes:S.dfResidual,dev:S.deviance,fit:Mu(S),x:S.model.x,intercept:S.model.intercept}));u=l.map((S,A)=>A===0?NaN:f[A]<0?-fc(T[A],T[A-1]):fc(T[A-1],T[A]))}p=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=L=>[...S,...Array.from({length:L},(k,F)=>F+S.length)],C=[];for(let L=0;L<=T;L++)L===T?C.push({dfRes:M.dfResidual,dev:M.deviance,fit:Mu(M),x:R.x,intercept:R.intercept}):L>0||a==="Rao"?C.push(jw(R,A(L),o)):C.push(void 0);l.push({name:"NULL",dfRes:M.dfNull,dev:M.nullDeviance});for(let L=1;L<=T;L++)l.push({name:`x${L}`,dfRes:C[L].dfRes,dev:C[L].dev});f=l.map((L,k)=>k===0?NaN:l[k-1].dfRes-L.dfRes),m=l.map((L,k)=>k===0?NaN:Math.max(l[k-1].dev-L.dev,0)),a==="Rao"&&(u=l.map((L,k)=>k===0?NaN:fc(C[k-1],C[k]))),p=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 w=new Rt,d=n.dispersion??xo(p,w);w.list.forEach(M=>o.add(M));let b=c?1/0:p.dfResidual,N=t?["Resid. Df","Resid. Dev","Df","Deviance"]:["Df","Deviance","Resid. Df","Resid. Dev"],v=l.map((M,R)=>t?[M.dfRes,M.dev,f[R],m[R]]:[f[R],m[R],M.dfRes,M.dev]),E=[...N],g=M=>l.map((R,T)=>{if(T===0||f[T]===0||h.has(T))return NaN;let S=Ve(M[T]/d*Math.sign(f[T]),-2*r);return S<0||Number.isNaN(S)?NaN:zw(S,Math.abs(f[T]))}),y=[];if(a==="Chisq"||a==="LRT")E.push("Pr(>Chi)"),y=[g(m)];else if(a==="Rao")E.push("Rao","Pr(>Chi)"),y=[u.map((M,R)=>h.has(R)?NaN:M),g(u)];else if(a==="F"){if(c&&o.add(`using F test with a '${i.family}' family is inappropriate`),b!==0){let M=l.map((R,T)=>{let S=Ve(m[T]/f[T]/d,-2*r);return T===0||f[T]===0||h.has(T)||S<0?NaN:S});E.push("F","Pr(>F)"),y=[M,M.map((R,T)=>Number.isNaN(R)?NaN:ht(R,Math.abs(f[T]),b,!1,!1))]}}else if(a==="Cp"){let M=p.model.x.length;E.push("Cp"),y=[l.map(R=>R.dev+2*(M-R.dfRes)*d)]}let x=v.map((M,R)=>[...M,...y.map(T=>T[R])]);return{colNames:E,rowNames:l.map(M=>M.name),table:x,warnings:o.list}}function Mu(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 Kw=["type"];function Eu(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,w)=>e.weights===void 0||e.weights[w]!==0?[w]:[]),c=i.map(p=>e.weights===void 0?1:Math.sqrt(e.weights[p])),a=Lt(i.map((p,w)=>s.map(d=>e.x[p][d]*c[w]))),l=Zs(e.weights===void 0?void 0:i.map(p=>V(e.weights[p],-o)),i.length),f=a.pivot.slice(0,a.rank),{qty:m}=Hr(f.map(p=>i.map((w,d)=>Et(l[d],e.x[w][s[p]]))),i.map((p,w)=>Et(l[w],V(e.y[p],-r)))),u=[0,0],h=0;return f.forEach((p,w)=>{p>=t.length&&(u=he(u,_e(m[w],m[w])),h++)}),{ss:u[0],df:h}}function mc(e,t={}){if(B(t,Kw,"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=Ws([e]),i=Bs([e],s)[0];if(o===0)throw new Error("residual df = 0");let c=Ei(e,s),a=y=>{let x=c.map(([T,S])=>[V(T,-y),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=Ks(c),f=a(l)),Er(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=y=>e.assign.flatMap((x,M)=>y.has(x)?[M]:[]),p=[...new Set(e.assign)],w=[],d=[],b=y=>{w.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 p){let R=h(new Set([M])),T=h(new Set(p.filter(S=>S!==M)));w.push(M===0?"(Intercept)":e.termLabels[M-1]),x.push({ss:Eu(e,T,R,s,y).ss,df:R.length})}}else{let M=R=>e.factors.flatMap((T,S)=>T[R-1]>0?[S]:[]);for(let R of p){if(R===0)continue;let T=M(R),S=p.filter(L=>L!==0&&L!==R&&T.every(k=>e.factors[k][L-1]>0)),A=h(new Set(p.filter(L=>L!==R&&!S.includes(L)))),C=Eu(e,A,h(new Set([R])),s,y);w.push(e.termLabels[R-1]),x.push(C)}}return x};d=b(l),l===0&&e.weights!==void 0&&d.some(y=>!Number.isFinite(y.ss))&&(l=Ks(c),d=b(l));let N=y=>V(y,2*s+l),v=V(i,-l),E=v/o,g=d.map(({ss:y,df:x})=>{let M=y/x/E;return[N(y),x,M,ht(M,x,o,!1,!1)]});return g.push([N(v),o,NaN,NaN]),Q("Anova",{colNames:["Sum Sq","Df","F value","Pr(>F)"],rowNames:[...w,"Residuals"],table:g,warnings:m},e)}var ra=[0,0];var uc=e=>e.map(t=>t.map(n=>n[0]));function xu(e){let t=e[0]?.length??0;return Array.from({length:t},(n,r)=>e.map(o=>o[r]))}function kr(e,t){let n=t[0]?.length??0;return e.map(r=>{let o=new Array(n).fill(ra);return r.forEach((s,i)=>{let c=t[i];for(let a=0;a<n;a++)o[a]=he(o[a],_e(s,c[a]))}),o})}function Ir(e){let t=ra;return e.forEach((n,r)=>t=he(t,n[r])),t}function Ru(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=rt(n[a][i],n[i][i]);n[a][i]=l;for(let f=i+1;f<t;f++)n[a][f]=Le(n[a][f],_e(l,n[i][f]))}}return{a:n,perm:r,sign:o,singular:s}}function Tu(e){let{a:t,sign:n,singular:r}=Ru(e);if(r)return ra;let o=[n,0];return t.forEach((s,i)=>o=_e(o,s[i])),o}function Su(e){let t=e.length,{a:n,perm:r,singular:o}=Ru(e);if(o)throw new Error("singular matrix");let s=Array.from({length:t},()=>new Array(t).fill(ra));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]=Le(c[a],_e(n[a][l],c[l]));for(let a=t-1;a>=0;a--){for(let l=a+1;l<t;l++)c[a]=Le(c[a],_e(n[a][l],c[l]));c[a]=rt(c[a],n[a][a])}for(let a=0;a<t;a++)s[a][i]=c[a]}return s}var Uw=["group","idata","M","X"],Au=(e,t)=>wt(e,t/2,2,!1,!1);function hc(e){let t=e.length,n=ft(Ir(e),t);return Tu(e.map(r=>r.map(o=>rt(o,n))))[0]}function pc(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=Au(i,c),l=Au(i,c+4);return a+s*(l-a)}var Hw=(e,t)=>Array.from({length:t},(n,r)=>e.map(o=>o[r]));function Yw(e,t,n){let r=e.length,o=b=>b.length===0?0:Lt(Array.from({length:r},(N,v)=>b.map(E=>E[v]))).rank,s=n.length===0?void 0:Lt(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=Lt(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=Kn(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])),w=Kn(f.map(m),u,p).qtv,d=Array.from({length:l.rank-c},(b,N)=>c+N);return d.map(b=>d.map(N=>w[b][N]))}function Gw(e,t={}){B(t,Uw,"mauchlyTest");let n=e.map(g=>Array.from(g)),r=n[0]?.length??0;if(r===0||n.some(g=>g.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=g=>g==null||typeof g=="number"&&Number.isNaN(g),i=n.map((g,y)=>!g.some(s)&&(o===void 0||!s(o[y])));if(n.some((g,y)=>i[y]&&g.some(x=>!Number.isFinite(x))))throw new RangeError("y must not contain infinite values");let c=qe(n.filter((g,y)=>i[y])).groups,a=c.length,l=o?.filter((g,y)=>i[y]).map(vt)??c.map(()=>""),f=[...new Set(l)].sort(Tn),m=c.map(g=>g.map(()=>[0,0]));for(let g of f){let y=l.flatMap((x,M)=>x===g?[M]:[]);for(let x=0;x<r;x++){let M=[0,0];for(let T of y)M=he(M,[c[T][x],0]);let R=ft(M,y.length);for(let T of y)m[T][x]=Le([c[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 h=Array.from({length:r},(g,y)=>Array.from({length:r},(x,M)=>{let R=[0,0];for(let T of m)R=he(R,_e(T[y],T[M]));return R})),p=t.idata??{index:Array.from({length:r},(g,y)=>y+1)},w=g=>{let y=ot(()=>Wr(g,p));if(y.x.length!==r)throw new RangeError("idata must have one row per column of y");return Hw(y.x,y.colNames.length)},d=t.M===void 0?Array.from({length:r},(g,y)=>Array.from({length:r},(x,M)=>M===y?1:0)):w(t.M),b=w(t.X??"~0"),N=Yw(h,d,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 E=hc(N);return{statistic:E,statisticName:"W",pValue:pc(E,v,u,r),method:"Mauchly's test of sphericity",warnings:[]}}var Xw=["n","d","type","sigLevel","power","alternative"],Qw=["n1","n2","d","sigLevel","power","alternative"],Zw=["h","n","sigLevel","power","alternative"],Jw=["h","n1","n2","sigLevel","power","alternative"],e1=["h","n","sigLevel","power","alternative"],t1=["d","n","sigLevel","power","alternative"],n1=["n","r","sigLevel","power","alternative"],r1=["w","N","df","sigLevel","power"],o1=["k","n","f","sigLevel","power"],s1=["u","v","f2","sigLevel","power"],a1=["size"],i1={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 ku(e="p",...t){B(ys(t),a1,"cohenES");let n=Ns(["size"],t).size??"small",r=Se(e,["p","t","r","anov","chisq","f2"]),o=Se(n,["small","medium","large"]);return{test:r,size:o,effectSize:i1[r][["small","medium","large"].indexOf(o)],method:"Conventional effect size from Cohen (1982)"}}function c1(e,t){if(!oa(e)||!oa(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 l1(e,t){if(e.length!==t.length)throw new RangeError("'P0' and 'P1' must have the same length");if(![...e,...t].every(oa))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+=Vt(t[r]-e[r],e[r]));return Math.sqrt(n)}function f1(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(oa)))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>=m1)o+=Vt(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 m1=2**-1022,oa=e=>e>=0&&e<=1,u1="\u2018sig.level\u2019 must be numeric in [0, 1]",h1="\u2018power\u2019 must be numeric in [0, 1]";function lr(e,t){let n=r=>!ye(r)&&!(typeof r=="number"&&r>=0&&r<=1);if(n(e))throw new RangeError(u1);if(n(t))throw new RangeError(h1)}function fr(e,t){if(typeof e=="number"&&Number.isNaN(e))throw new RangeError("the effect size must not be NaN");return typeof e=="string"?ku(t,e).effectSize:e}var Wo=["two.sided","less","greater"];function sa(e,t){return e==="two.sided"?t:e==="less"?[-10,5]:[-5,10]}function Iu(e,t,n,r){if(r==="less")return Nr(Wt(n,e),e,t,!0);if(r==="greater")return Nr(Wt(n,e,{lowerTail:!1}),e,t,!1);let o=Wt(n/2,e,{lowerTail:!1});return Nr(o,e,t,!1)+Nr(-o,e,t,!0)}var qu=e=>e<1e-8?le(e,{lowerTail:!1}):le(1-e);function _u(e,t,n){return n==="greater"?se(qu(t)-e,{lowerTail:!1}):n==="less"?se(le(t)-e):se(qu(t/2)-e,{lowerTail:!1})+se(le(t/2)-e)}function p1(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*=V(r,-o),n+=o}return V(t,n)}function d1(e={}){B(e,Xw,"pwrTTest");let{n:t,power:n}=e,r=fr(e.d,"t"),o=e.sigLevel===void 0?.05:e.sigLevel;if(nn([t,r,n,o],"exactly one of n, d, power, and sig.level must be NULL"),lr(o,n),!ye(t)&&!(t>=2))throw new RangeError("number of observations in each group must be at least 2");let s=Se(e.type,["two.sample","one.sample","paired"]),i=Se(e.alternative,Wo),c=s==="two.sample"?2:1;i==="two.sided"&&!ye(r)&&(r=Math.abs(r));let a=(m,u,h)=>Iu((m-1)*c,Math.sqrt(m/c)*u,h,i);if(ye(n))n=a(t,r,o);else{let m=n;ye(t)?t=Ye(u=>a(u,r,o)-m,2+1e-10,1e9):ye(r)?r=Ye(u=>a(t,u,o)-m,...sa(i,[1e-7,10])):o=Ye(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 g1(e={}){B(e,Qw,"pwrT2nTest");let{n1:t,n2:n,power:r}=e,o=fr(e.d,"t"),s=e.sigLevel===void 0?.05:e.sigLevel;if(nn([t,n,o,r,s],"exactly one of n1, n2, d, power, and sig.level must be NULL"),lr(s,r),!ye(t)&&!(t>=2))throw new RangeError("number of observations in the first group must be at least 2");if(!ye(n)&&!(n>=2))throw new RangeError("number of observations in the second group must be at least 2");let i=Se(e.alternative,Wo);i==="two.sided"&&!ye(o)&&(o=Math.abs(o));let c=(a,l,f,m)=>Iu(a+l-2,f/Math.sqrt(1/a+1/l),m,i);if(ye(r))r=c(t,n,o,s);else{let a=r;ye(t)?t=Ye(l=>c(l,n,o,s)-a,2+1e-10,1e9):ye(n)?n=Ye(l=>c(t,l,o,s)-a,2+1e-10,1e9):ye(o)?o=Ye(l=>c(t,n,l,s)-a,...sa(i,[1e-7,10])):s=Ye(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 dc(e,t,n,r,o,s){let{n:i,power:c}=t,a=fr(e,n==="h"?"p":"t"),l=t.sigLevel===void 0?.05:t.sigLevel;if(nn([a,i,c,l],`exactly one of ${n}, n, power, and sig.level must be NULL`),lr(l,c),!ye(i)&&!(i>=1))throw new RangeError("number of observations in each group must be at least 1");let f=Se(t.alternative,Wo);f==="two.sided"&&!ye(a)&&(a=Math.abs(a));let m=(u,h,p)=>_u(u*r(h),p,f);if(ye(c))c=m(a,i,l);else{let u=c;ye(a)?a=Ye(h=>m(h,i,l)-u,...sa(f,[s,10])):ye(i)?i=Ye(h=>m(a,h,l)-u,o,1e9):l=Ye(h=>m(a,i,h)-u,1e-10,1-1e-10)}return{es:a,n:i,sigLevel:l,power:c,alternative:f}}function w1(e={}){B(e,Zw,"pwr2pTest");let t=dc(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 b1(e={}){B(e,e1,"pwrPTest");let t=dc(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 v1(e={}){B(e,t1,"pwrNormTest");let t=dc(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 N1(e={}){B(e,Jw,"pwr2p2nTest");let{n1:t,n2:n,power:r}=e,o=fr(e.h,"p"),s=e.sigLevel===void 0?.05:e.sigLevel;if(nn([o,t,n,r,s],"exactly one of h, n1, n2, power, and sig.level must be NULL"),lr(s,r),!ye(t)&&!(t>=2))throw new RangeError("number of observations in the first group must be at least 2");if(!ye(n)&&!(n>=2))throw new RangeError("number of observations in the second group must be at least 2");let i=Se(e.alternative,Wo);i==="two.sided"&&!ye(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)=>_u(l*Math.sqrt(c(f,m)),u,i);if(ye(r))r=a(o,t,n,s);else{let l=r;ye(o)?o=Ye(f=>a(f,t,n,s)-l,...sa(i,[1e-10,10])):ye(t)?t=Ye(f=>a(o,f,n,s)-l,2+1e-10,1e9):ye(n)?n=Ye(f=>a(o,t,f,s)-l,2+1e-10,1e9):s=Ye(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 y1(e={}){B(e,n1,"pwrRTest");let{n:t,power:n}=e,r=fr(e.r,"r"),o=e.sigLevel===void 0?.05:e.sigLevel;if(nn([t,r,n,o],"exactly one of n, r, power, and sig.level must be NULL"),lr(o,n),!ye(t)&&!(t>=4))throw new RangeError("number of observations must be at least 4");if(!ye(r)&&!(Math.abs(r)<=1))throw new RangeError("'r' must be in [-1, 1]");let s=Se(e.alternative,Wo);s==="two.sided"&&!ye(r)&&(r=Math.abs(r));let i=(a,l,f,m)=>{let u=Wt(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),w=Math.atanh(l)+l/(2*(a-1)),d=Math.atanh(p),b=se((w-d)*Math.sqrt(a-3));return m?b+se((-w-d)*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(ye(n))n=c(t,r,o),s==="less"&&(r=-r);else{let a=n;if(ye(t))t=Ye(l=>c(l,r,o)-a,4+1e-10,1e9);else if(ye(r)){let[l,f]=s==="two.sided"?[1e-10,.9999999999]:[-.9999999999,.9999999999];r=Ye(m=>c(t,m,o)-a,l,f)}else o=Ye(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 M1(e){B(e,r1,"pwrChisqTest");let{N:t,power:n}=e,{df:r}=e,o=fr(e.w,"chisq"),s=e.sigLevel===void 0?.05:e.sigLevel;if(nn([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(lr(s,n),!ye(o)&&!(o>=0))throw new RangeError("w must be positive");if(!ye(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:It(On(l,r,{lowerTail:!1}),r,{ncp:f,lowerTail:!1})};if(ye(n))n=i(o,t,s);else{let c=n;ye(o)?o=Ye(a=>i(a,t,s)-c,1e-10,1e9):ye(t)?t=Ye(a=>i(o,a,s)-c,1+1e-10,1e9):s=Ye(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 E1(e={}){B(e,o1,"pwrAnovaTest");let{k:t,n,power:r}=e,o=fr(e.f,"anov"),s=e.sigLevel===void 0?.05:e.sigLevel;if(nn([t,n,o,r,s],"exactly one of k, n, f, power, and sig.level must be NULL"),lr(s,r),!ye(o)&&!(o>=0))throw new RangeError("f must be positive");if(!ye(t)&&!(t>=2))throw new RangeError("number of groups must be at least 2");if(!ye(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=p1([c,a,l,l])),h===1/0?1:qs(m,u)?ks(m,u,h,f):Rn(Gn(f,m,u,{lowerTail:!1}),m,u,{ncp:h,lowerTail:!1})};if(ye(r))r=i(t,n,o,s);else{let c=r;ye(t)?t=Ye(a=>i(a,n,o,s)-c,2+1e-10,100):ye(n)?n=Ye(a=>i(t,a,o,s)-c,2+1e-10,1e9):ye(o)?o=Ye(a=>i(t,n,a,s)-c,1e-7,1e7):s=Ye(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 x1(e={}){B(e,s1,"pwrF2Test");let{u:t,v:n,power:r}=e,o=fr(e.f2,"f2"),s=e.sigLevel===void 0?.05:e.sigLevel;if(nn([t,n,o,r,s],"exactly one of u, v, f2, power, and sig.level must be NULL"),lr(s,r),!ye(o)&&!(o>=0))throw new RangeError("f2 must be positive");if(!ye(t)&&!(t>=1))throw new RangeError("degree of freedom u for numerator must be at least 1");if(!ye(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:qs(c,a)?ks(c,a,m,f):Rn(Gn(f,c,a,{lowerTail:!1}),c,a,{ncp:m,lowerTail:!1})};if(ye(r))r=i(t,n,o,s);else{let c=r;ye(t)?t=Ye(a=>i(a,n,o,s)-c,1+1e-10,100):ye(n)?n=Ye(a=>i(t,a,o,s)-c,1+1e-10,1e9):ye(o)?o=Ye(a=>i(t,n,a,s)-c,1e-7,1e7):s=Ye(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 R1=["id","dv","between","within","type","correction","es","observed"],aa=e=>e===void 0?[]:typeof e=="string"?[e]:[...e],T1=1e-12,Lu=1e-24;function Cu(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 Bo=e=>typeof e=="boolean"?e?"TRUE":"FALSE":vt(e);function S1(e){let t=[...new Set(e)];return t.every(n=>typeof n=="number")?t.sort((n,r)=>n-r).map(Bo):[...new Set(t.map(Bo))].sort(Tn)}var Fu=(e,t)=>t.split(":").includes(e);function Ou(e,t,n,r,o){let s=l=>r.some(f=>Fu(f,l));for(let l of r)if(!e.some(f=>Fu(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 A1(e,t){B(t,R1,"aovEz");let n=aa(t.between),r=aa(t.within);if(n.length===0&&r.length===0)throw new Error("Either between or within need to be non-NULL!");for(let[j,U]of[["between",n],["within",r]]){let W=U.filter((te,fe)=>U.indexOf(te)!==fe);if(W.length>0)throw new RangeError(`duplicated factor names in '${j}': ${[...new Set(W)].join(", ")}`)}let o=n.filter(j=>r.includes(j));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=aa(t.es??"ges");for(let j of c)if(j!=="ges"&&j!=="pes")throw new RangeError(`invalid 'es': ${String(j)}`);let a=aa(t.observed),l=[],f=j=>j==null||typeof j=="number"&&Number.isNaN(j),m=Cu(e,t.dv),u=[t.id,...n,...r].map(j=>Cu(e,j));for(let j of u)if(j.length!==m.length)throw new RangeError("the columns must have the same length");let h=m.map((j,U)=>u.every(W=>!f(W[U]))),p=j=>j.filter((U,W)=>h[W]),w=Pt(p(u[0])).map(Bo),d=p(m).map(j=>{if(f(j))return NaN;if(typeof j!="number")throw new TypeError(`the response '${t.dv}' must be numeric`);if(!Number.isFinite(j))throw new RangeError(`the response '${t.dv}' must be finite`);return j}),b=n.map((j,U)=>Pt(p(u[1+U]))),N=r.map((j,U)=>Pt(p(u[1+n.length+U]))),v=w.length;for(let j of[d,...b,...N])if(j.length!==v)throw new RangeError("the columns must have the same length");let E=b.map(j=>S1(j)),g=b.map(j=>j.map(Bo)),y=N.map(j=>j.map(Bo)),x=y.map(j=>[...new Set(j)]);E.forEach((j,U)=>{if(j.length<2)throw new Error(`Factor "${n[U]}" consists of one level only. Remove factor from model?`)}),x.forEach((j,U)=>{if(j.length<2)throw new Error(`Factor "${r[U]}" consists of one level only. Remove factor from model?`)});let M=[...new Set(w)],R=new Map,T=new Set;if(w.forEach((j,U)=>{let W=JSON.stringify(g.map(fe=>fe[U])),te=R.get(j);te===void 0?R.set(j,W):te!==W&&T.add(j)}),T.size>0)throw new Error(`Following ids are in more than one between subjects condition:
8
+ ${M.filter(j=>T.has(j)).join(", ")}`);let S=[[]];for(let j of x)S=S.flatMap(U=>j.map(W=>[...U,W]));let A=j=>JSON.stringify(y.map(U=>U[j])),C=new Map(S.map((j,U)=>[JSON.stringify(j),U])),L=new Set(w.map((j,U)=>A(U)));if(S.some(j=>!L.has(JSON.stringify(j))))throw new Error("Empty cells in within-subjects design (i.e., bad data structure).");let k=new Map,F=new Set;w.forEach((j,U)=>{let W=k.get(j);W===void 0&&k.set(j,W=new Set),W.add(A(U)),Number.isNaN(d[U])&&F.add(j)});let I=M.filter(j=>F.has(j)||k.get(j).size<S.length),O=new Set(I),q=qe([d.filter((j,U)=>!O.has(w[U]))]).k,$=q===0?d:d.map(j=>V(j,-q)),_=j=>V(j,2*q),P=S.length,K=new Map;if(w.forEach((j,U)=>{let W=K.get(j);W===void 0&&K.set(j,W={s:S.map(()=>[0,0]),n:S.map(()=>0)});let te=C.get(A(U));W.s[te]=he(W.s[te],[$[U],0]),W.n[te]++}),[...K.values()].some(j=>j.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."),I.length>0){let j=[...I].sort(Tn),U=j.length>=10?`${j.slice(0,10).join(", ")}, ... [showing first 10 only]`:j.join(", ");l.push(`Missing values for ${I.length} ID(s), which were removed before analysis:
9
+ ${U}`)}let X=M.filter(j=>!I.includes(j)),re=new Map;w.forEach((j,U)=>re.has(j)||re.set(j,U));let z=X.map(j=>{let U=K.get(j);return U.s.map((W,te)=>ft(W,U.n[te]))}),J={};n.forEach((j,U)=>J[j]=X.map(W=>g[U][re.get(W)]));let Z=Object.fromEntries(n.map(j=>[j,"contr.sum"])),Ae=Object.fromEntries(n.map((j,U)=>[j,E[U]])),ne="Rank deficient model matrix; insufficient data to estimate full model.";if(P===1)return Q("aovEz",D());return Q("aovEz",Y());function D(){let j="y";for(;n.includes(j);)j+="_";let U={...J,[j]:z.map(me=>me[0][0])},W=ot(()=>$o(`${j} ~ ${n.length>0?n.join(" * "):"1"}`,U,{contrasts:Z,levels:Ae}));if(W.rank<W.coefNames.length)throw new Error(ne);if(W.dfResidual<=0)throw new RangeError(`no residual degrees of freedom: ${z.length} subject(s) for ${W.rank} parameter(s)`);let te=ot(()=>mc(W,{type:s}));l.push(...te.warnings.filter(me=>!l.includes(me)));let fe=W.dfResidual,Re=te.rowNames.flatMap((me,ke)=>{if(me==="(Intercept)"||me==="Residuals")return[];let[de,Fe,xe,Pe]=te.table[ke];return[{name:me,ss:de,numDf:Fe,errSS:W.rss,denDf:fe,f:xe,p:Pe}]}),Ce=te.colNames.indexOf("Sum Sq"),be={anova:Ou(Re,new Map([[0,W.rss]]),c,a,_),univariate:{colNames:te.colNames,rowNames:te.rowNames,table:te.table.map(me=>me.map((ke,de)=>de===Ce?_(ke):ke))},within:r,warnings:l};return Ut.set(be,{colNames:te.colNames,rowNames:te.rowNames,table:te.table}),be}function Y(){let j=X.length,U=z.reduce((Me,Ne)=>Ne.reduce((Ee,ee)=>Ee+ee[0]*ee[0],Me),0),W=n.length>0?ot(()=>Wr(`~ ${n.join(" * ")}`,J,{contrasts:Z,levels:Ae})):{colNames:["(Intercept)"],assign:[0],x:X.map(()=>[1]),termLabels:[],factors:[]},te=W.colNames.length;if(j===0||Lt(W.x).rank<te)throw new Error(ne);let fe=j-te;if(fe<=0)throw new RangeError(`no residual degrees of freedom: ${j} subject(s) for ${te} between-subject parameter(s)`);let Re=[0,...W.termLabels.map((Me,Ne)=>Ne+1)],Ce=["(Intercept)",...W.termLabels],be=Me=>W.assign.flatMap((Ne,Ee)=>Me.includes(Ne)?[Ee]:[]),me=Me=>W.factors.flatMap((Ne,Ee)=>Ne[Me-1]>0?[Ee]:[]),ke=Me=>W.x.map(Ne=>[Ne[Me],0]),de=new Array(j).fill([0,0]),Fe=(Me,Ne,Ee)=>{let{qtv:ee}=Kn([...Me,...Ne].map(ke),de,Ee);return ee.map(ve=>ve.slice(Me.length,Me.length+Ne.length))},xe=Me=>{let{qtv:Ne}=Kn(W.colNames.map((Ee,ee)=>ke(ee)),de,Me);return Ne.map(Ee=>Ee.slice(te))},Pe=Object.fromEntries(r.map((Me,Ne)=>[Me,S.map(Ee=>Ee[Ne])])),Oe=ot(()=>Wr(`~ ${r.join(" * ")}`,Pe,{contrasts:Object.fromEntries(r.map(Me=>[Me,"contr.sum"])),levels:Object.fromEntries(r.map((Me,Ne)=>[Me,x[Ne]]))})),Ke=[0,...Oe.termLabels.map((Me,Ne)=>Ne+1)],ze=["(Intercept)",...Oe.termLabels],et=Me=>Me.map(Ne=>Me.map(Ee=>{let ee=[0,0];return Ne.forEach((ve,ie)=>ee=he(ee,_e(ve,Ee[ie]))),ee})),Ue=[],Xe=[],pe=[],oe=[],we=[],ge=[],ce=[],tt=new Map,G=!1;for(let Me of Ke){let Ne=Oe.assign.flatMap((je,We)=>je===Me?[We]:[]),Ee=Ne.length,ee=Ne.map(je=>z.map(We=>We.reduce((Tt,St,xn)=>he(Tt,Et(St,Oe.x[xn][je])),[0,0]))),ve=Ne.map(je=>Ne.map(We=>[Oe.x.reduce((Tt,St)=>Tt+St[je]*St[We],0),0])),ie=Su(ve),Te=et(xe(ee)),Be=kr(Te,ie),$e=Ir(Be)[0],st=NaN,mt=NaN,Ct=NaN,Ht=NaN,Yn=!1;if(Ee>=2){let je=ze[Me],We=Vl(uc(ie)).map(Yt=>Yt.map(on=>[on,0])),Tt=uc(kr(kr(We,Te),xu(We))),St=Bt(Tt,{symmetric:!0,onlyValues:!0}).values,xn=St.reduce((Yt,on)=>Math.max(Yt,on),0),$t=Math.max(xn*T1,U*Lu),kt=Ir(Be),ut=Ir(kr(Be,Be));xn>U*Lu&&ut[0]>0&&Number.isFinite(ut[0])?(Yn=!0,st=rt(_e(kt,kt),Et(ut,Ee))[0],fe-Ee*st>0?mt=((fe+1)*Ee*st-2)/(Ee*(fe-Ee*st)):l.push(`The Huynh-Feldt epsilon of ${je} is undefined (d - p GG <= 0)${i==="HF"?": corrected with GG":""}`)):l.push(`The error SSP matrix of ${je} is 0 (or at the rounding level of the data): no sphericity corrections (the p-values are uncorrected)`),Yn&&St.every(Yt=>Yt>$t)?(Ct=hc(kr(ie,Te)),Ht=pc(Ct,Ee,fe,P)):Yn&&l.push(`The error SSP matrix of ${je} is singular: no Mauchly test (the epsilons are still defined)`)}for(let je=0;je<Re.length;je++){let We=Re[je],Tt;if(s===3)Tt=Fe(be(Re.filter(Ft=>Ft!==We)),be([We]),ee);else if(We===0)Tt=Fe([],be([0]),ee);else{let Ft=me(We),en=Re.filter(H=>H!==0&&H!==We&&Ft.every(ae=>W.factors[ae][H-1]>0));Tt=Fe(be(Re.filter(H=>H!==We&&!en.includes(H))),be([We]),ee)}let St=Ir(kr(et(Tt),ie))[0],$t=(We===0?1:be([We]).length)*Ee,kt=fe*Ee,ut=St/$t/($e/kt),Yt=ht(ut,$t,kt,!1,!1),on=Me===0?Ce[je]:We===0?ze[Me]:`${Ce[je]}:${ze[Me]}`;Ue.push([_(St),$t,_($e),kt,ut,Yt]),Xe.push([St,$t,$e,kt,ut,Yt]),pe.push(on),tt.set(Me,$e);let Jt={name:on,ss:St,numDf:$t,errSS:$e,denDf:kt,f:ut,p:Yt};if(Yn){let Ft=Math.min(1,mt),en=ht(ut,st*$t,st*kt,!1,!1),H=ht(ut,Ft*$t,Ft*kt,!1,!1);oe.push([st,en,mt,H]),we.push([Ct,Ht]),ge.push(on),i==="GG"?Jt={...Jt,numDf:st*$t,denDf:st*kt,p:en}:i==="HF"&&(mt>1&&(G=!0),Jt=Number.isNaN(mt)?{...Jt,numDf:st*$t,denDf:st*kt,p:en}:{...Jt,numDf:Ft*$t,denDf:Ft*kt,p:H})}Me===0&&We===0||ce.push(Jt)}}G&&l.push("HF eps > 1 treated as 1");let ue={anova:Ou(ce,tt,c,a,_),univariate:{colNames:["Sum Sq","num Df","Error SS","den Df","F value","Pr(>F)"],rowNames:pe,table:Ue},within:r,warnings:l};return oe.length>0&&(ue.corrections={colNames:["GG eps","Pr(>F[GG])","HF eps","Pr(>F[HF])"],rowNames:ge,table:oe},ue.sphericity={colNames:["Test statistic","p-value"],rowNames:[...ge],table:we}),Ut.set(ue,{colNames:ue.univariate.colNames,rowNames:pe,table:Xe}),ue}}function Xr(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=it(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 q1=["rho"],k1=new Set(["group","start","id","weights","ctype","stype","robust","rmean","confType","confInt"]),wc=e=>e==null||typeof e=="number"&&Number.isNaN(e);function I1(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&&wc(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=Pt(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=Xr(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=${vt(a)}`)}}function _1(e,t,n,r={}){B(r,q1,"survdiff");let{rho:o=0}=r;if(!Number.isFinite(o))throw new RangeError("'rho' must be a finite number");let{obs:s,labels:i}=I1(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 E=s[v].time,g=v,y=new Array(c).fill(0),x=new Array(c).fill(0),M=0;for(;g<s.length&&s[g].time===E;){let R=s[g];x[R.group]++,R.event&&(y[R.group]++,M++),g++}if(M>0){let R=o===0?1:p**o;for(let T=0;T<c;T++)l[T]+=R*y[T],f[T]+=R*M*u[T]/h;if(h>1){let T=R*R*M*(h-M)/(h*(h-1));for(let S=0;S<c;S++)for(let A=0;A<c;A++)m[S][A]+=T*u[S]*((S===A?1:0)-u[A]/h)}p*=1-M/h}for(let R=0;R<c;R++)u[R]-=x[R];h-=g-v,v=g}let w=f.map((v,E)=>v>0?E:-1).filter(v=>v>=0),d=0,b=Math.max(w.length-1,0);if(w.length>=2){let v=w.slice(1),E=v.map(y=>l[y]-f[y]),g=v.map(y=>v.map(x=>m[y][x]));try{let y=nt(g,E);d=E.reduce((x,M,R)=>x+M*y[R],0)}catch{({chisq:d,df:b}=L1(g,E))}}let N=b===0?NaN:It(d,b,{lowerTail:!1});return{n:a,groups:i,obs:l,exp:f,var:m,chisq:d,df:b,pvalue:N,pValue:N,warnings:[]}}function L1(e,t){let n=Bt(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 gc(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 dn(e[r],e[s])}return e[r]}function mr(e,t,n={}){B(n,k1,"survfit");let{group:r,start:o,id:s,weights:i,ctype:c=1,stype:a=1,robust:l=!1,rmean:f="common",confType:m="log",confInt:u=.95}=n;if(typeof m!="string")throw new TypeError("confType must be a string");let h=["log","log-log","plain","none","logit","arcsin"],p=typeof m=="string"?h.filter(_=>_.startsWith(m)):[],w=h.includes(m)?m:p.length===1?p[0]:m;if(typeof c!="number")throw new TypeError("ctype must be a number");if(typeof a!="number")throw new TypeError("stype must be a number");if(c!==1&&c!==2)throw new RangeError("ctype must be 1 or 2");if(a!==1&&a!==2)throw new RangeError("stype must be 1 or 2");if(!h.some(_=>_===w))throw new RangeError("'arg' should be one of \u201Clog\u201D, \u201Clog-log\u201D, \u201Cplain\u201D, \u201Cnone\u201D, \u201Clogit\u201D, \u201Carcsin\u201D");if(typeof u!="number")throw new TypeError("confInt must be a number");if(!(u>0&&u<1))throw new RangeError("confidence intervals must be between 0 and 1");if(e.length!==t.length)throw new RangeError("'time' and 'status' must have the same length");if(r!==void 0&&r.length!==e.length)throw new RangeError("'group' must have the same length as 'time'");if(o!==void 0&&o.length!==e.length)throw new RangeError("'start' must have the same length as 'time'");if(s!==void 0&&s.length!==e.length)throw new RangeError("'id' must have the same length as 'time'");if(s!==void 0&&o===void 0)throw new RangeError("'id' requires 'start'");if(i!==void 0&&!Array.isArray(i))throw new TypeError("'weights' must be an array");if(i!==void 0&&i.length!==e.length)throw new RangeError("'weights' must have the same length as 'time'");if(typeof l!="boolean")throw new TypeError("'robust' must be a boolean");if(typeof f!="number"&&typeof f!="string")throw new TypeError("rmean must be a number or string");if(typeof f=="number"&&!Number.isFinite(f))throw new RangeError("'rmean' must be finite");if(typeof f!="number"&&f!=="none"&&f!=="common"&&f!=="individual")throw new RangeError("'arg' should be one of \u201Cnone\u201D, \u201Ccommon\u201D, \u201Cindividual\u201D");let d=[];for(let _=0;_<e.length;_++){let P=i?.[_];if(i&&P!=null&&typeof P!="number")throw new TypeError("weights must contain numbers");if(i&&(P==null||Number.isNaN(P)))continue;let K=P??1;if(!Number.isFinite(K))throw new RangeError("weights must be finite");if(K<0)throw new RangeError("weights must be non-negative");if(!Number.isSafeInteger(K))throw new RangeError("weights must be non-negative safe integers");if(e[_]!=null&&typeof e[_]!="number")throw new TypeError("'time' must contain numbers");if(t[_]!=null&&typeof t[_]!="number")throw new TypeError("'status' must contain numbers");if(o!==void 0&&o[_]!=null&&typeof o[_]!="number")throw new TypeError("'start' must contain numbers");if(!(e[_]==null||t[_]==null||Number.isNaN(e[_])||Number.isNaN(t[_])||o!==void 0&&(o[_]==null||Number.isNaN(o[_]))||r!==void 0&&wc(r[_])||s!==void 0&&wc(s[_]))){if(o!==void 0&&!(o[_]<e[_]))throw new RangeError("stop time must be > start time");if(t[_]!==0&&t[_]!==1)throw new RangeError("'status' must be 0 (censored) or 1 (event)");if(e[_]===-1/0||e[_]===1/0&&t[_]===1)throw new RangeError("event times must be finite");d.push(_)}}if(d.length===0)throw new RangeError("no observations without missing values");let b=r===void 0?[]:Pt(r),N=s===void 0?[]:Array.from(s),v=r===void 0?["all"]:Array.from(new Set(d.map(_=>b[_]))).sort((_,P)=>typeof _=="number"&&typeof P=="number"?_-P:String(_)<String(P)?-1:String(_)>String(P)?1:0).map(_=>`group=${vt(_)}`),E=new Map;r!==void 0&&Array.from(new Set(d.map(_=>b[_]))).sort((_,P)=>typeof _=="number"&&typeof P=="number"?_-P:String(_)<String(P)?-1:String(_)>String(P)?1:0).forEach((_,P)=>E.set(_,P));let g=new Map;s!==void 0&&Array.from(new Set(d.map(_=>N[_]))).forEach((_,P)=>g.set(_,P));let y=d.flatMap(_=>o===void 0?[e[_]]:[o[_],e[_]]),x=Xr(y),M=new Map,R=new Map;d.forEach((_,P)=>{if(o===void 0)R.set(_,x[P]);else if(M.set(_,x[2*P]),R.set(_,x[2*P+1]),!(M.get(_)<R.get(_)))throw new Error("aeqSurv exception, an interval has effective length 0")});let T=d.map(_=>({time:R.get(_),start:o===void 0?-1/0:M.get(_),event:t[_]===1,group:r===void 0?0:E.get(b[_]),id:s===void 0?_:g.get(N[_]),weight:i?.[_]??1})).sort((_,P)=>_.time-P.time),S=s===void 0?void 0:new Map;if(S)for(let _ of T){let P=S.get(_.id)??[];P.push(_),S.set(_.id,P)}let A=v,C=T.map(_=>_.time),L=Math.min(...C),k=Math.min(0,L);if(typeof f=="number"&&f<=k)throw new RangeError("'rmean' must be greater than the integration origin");let F=(1-u)/2,I=F<1e-8?le(F,{lowerTail:!1}):le(1-F),O=[],q={strata:[],time:[],nRisk:[],nEvent:[],nCensor:[],surv:[],stdErr:O,stdError:O,lower:[],upper:[],cumhaz:[],stdChaz:[],logse:!l||c===2,n:[],records:[],nMax:[],nStart:[],events:[],rmean:[],seRmean:[],median:[],medianRows:A,table:{colNames:[],rowNames:A,table:[]},warnings:[]};i!==void 0&&(q.counts={nRisk:[],nEvent:[],nCensor:[]});let $=[];for(let _=0;_<A.length;_++){let P=T.filter(pe=>pe.group===_);if(!P.some(pe=>pe.weight>0))throw new Error("a group has no positive weight");let K=q.time.length,X=P.length,re=Math.max(...P.map(pe=>pe.time)),z=0,J=0,Z=1,Ae=0,ne=0,D=0;if(o===void 0){let pe=0;for(let oe of P)if(pe+=oe.weight,!Number.isSafeInteger(pe))throw new RangeError("total case weight per group must be a safe integer");z=pe,J=P.length}let Y=[],j=[],U=[],W=s===void 0?void 0:new Map;if(W)for(let pe of P){let oe=W.get(pe.id)??[];oe.push(pe),W.set(pe.id,oe)}for(let pe=0;pe<P.length;){let oe=P[pe].time,we=pe,ge=0,ce=0,tt=0,G=J;for(;we<P.length&&P[we].time===oe;)we++;let ue=P.slice(pe,we);if(o!==void 0){let je=P.filter(We=>We.start<oe&&We.time>=oe);if(z=je.reduce((We,Tt)=>We+Tt.weight,0),J=je.length,G=J,!Number.isSafeInteger(z))throw new RangeError("total case weight at risk must be a safe integer")}let Me=ue.filter(je=>je.event),Ne=ue.filter(je=>!je.event&&(S===void 0||!(S.get(je.id)??[]).some(We=>We.start===oe&&We.time>oe)));for(let je of Me)ge+=je.weight,ce++;for(let je of Ne)tt+=je.weight;let Ee=Me.length,ee=Ne.length;if(s!==void 0&&Me.length===0&&Ne.length===0){pe=we;continue}let ve=0,ie=0;if(ge>0){if(c===1)ve=ge/z,ie=ge/(z*z);else for(let je=0;je<ce;je++){let We=ge/ce,Tt=z-je*We;ve+=We/Tt,ie+=We/(Tt*Tt)}Ae+=ve,D+=ie,a===1?(Z*=1-ge/z,ne+=ge<z?ge/(z*(z-ge)):1/0):Z=Math.exp(-Ae),U.push({t:oe,d:ge,n:z,e:ce,S:Z})}let Te=Math.sqrt(a===1?ne:D),Be=0,$e=0;if(l){let je=0,We=0,Tt=W===void 0?P:Array.from(W.values(),St=>St[0]);for(let St of Tt){let xn=W?.get(St.id)??[St],$t=0,kt=0;for(let ut of U){if(ut.t>oe)break;let Yt=xn.some(Ft=>Ft.start<ut.t&&Ft.time>=ut.t),on=xn.some(Ft=>Ft.time===ut.t&&Ft.event),Jt=ut.d/ut.n;if(c===1)kt+=(Number(on)-Number(Yt)*Jt)/ut.n;else{let Ft=0,en=0;for(let H=0;H<ut.e;H++){let ae=ut.d/ut.e,Ie=ut.n-H*ae;Ft+=(Ie+H*ae)/(ut.e*Ie*Ie),en-=ae/(Ie*Ie)}kt+=Ft*Number(on)+en*Number(Yt)}a===1&&Jt<1&&($t-=Z*(Number(on)-Number(Yt)*Jt)/((1-Jt)*ut.n))}je+=(St.weight*$t)**2,We+=(St.weight*kt)**2}Be=Math.sqrt(je),$e=Math.sqrt(We)}let st=l?c===2?a===1?Be:$e:a===1?Z===0?NaN:Be/Z:$e:Te,mt=l?c===2?Z*(a===1?Be:$e):a===1?Be:Z*$e:Z*Te,Ct=NaN,Ht=NaN;if(w!=="none")if(w==="log")Ct=Z===0?mt===0?0:NaN:Z*Math.exp(-I*st),Ht=Z===0?mt===0?0:NaN:Math.min(1,Z*Math.exp(I*st));else if(w==="log-log")if(Z===0||Z===1)Ct=mt===0?Z:NaN,Ht=Ct;else{let je=Math.log(-Math.log(Z));Ct=Math.exp(-Math.exp(je-I*st/Math.log(Z))),Ht=Math.exp(-Math.exp(je+I*st/Math.log(Z)))}else if(w==="plain")Z===0&&mt===1/0?(Ct=NaN,Ht=NaN):(Ct=Math.max(0,Z-I*mt),Ht=Math.min(1,Z+I*mt));else if(w==="logit")if(Z===0||Z===1)Ct=mt===0?Z:NaN,Ht=Ct;else{let je=Math.log(Z/(1-Z)),We=I*mt/(Z*(1-Z));Ct=1-1/(1+Math.exp(je-We)),Ht=1-1/(1+Math.exp(je+We))}else if(Z>0&&Z<1){let je=Math.asin(Math.sqrt(Z)),We=I*mt/(2*Math.sqrt(Z*(1-Z)));Ct=Math.sin(Math.max(0,je-We))**2,Ht=Math.sin(Math.min(Math.PI/2,je+We))**2}else Ct=NaN,Ht=NaN;let Yn=z;q.strata.push(A[_]),q.time.push(oe),q.nRisk.push(Yn),q.nEvent.push(ge),q.nCensor.push(tt),q.surv.push(Z),q.cumhaz.push(Ae),q.stdChaz.push(l?$e:Math.sqrt(D)),q.stdErr.push(l?c===2?Z*(a===1?Be:$e):a===1?Be:Z*$e:Z*Te),q.lower.push(Ct),q.upper.push(Ht),q.counts&&(q.counts.nRisk.push(G),q.counts.nEvent.push(Ee),q.counts.nCensor.push(ee)),o===void 0&&(z-=ge+tt,J-=we-pe),pe=we}let te=q.time.slice(K),fe=q.surv.slice(K),Re=q.lower.slice(K),Ce=q.upper.slice(K);q.median.push([gc(te,fe),w==="none"?NaN:gc(te,Re),w==="none"?NaN:gc(te,Ce)]),q.n.push(X),q.records.push(X);let be=q.nRisk.slice(K);q.nMax.push(Math.max(...be)),q.nStart.push(be[0]??0),q.events.push(P.reduce((pe,oe)=>pe+(oe.event?oe.weight:0),0));let me=f==="none"?NaN:f==="common"?Math.max(...C):f==="individual"?re:f;if(f!=="none"&&!Number.isFinite(me))throw new RangeError("'rmean' must be finite");let ke=P.filter(pe=>Number.isFinite(pe.time)).map(pe=>Math.abs(pe.time)),de=Number.isFinite(me)?Math.max(Math.abs(me),Math.abs(k),...ke):Math.max(Math.abs(k),...ke),Fe=de===0?0:Math.max(-1074,Math.min(1023,Math.floor(Math.log2(de)))),xe=de===0?1:2**Fe,Pe=Number.isFinite(me)&&!Number.isFinite(me-k),Oe=pe=>{let oe=1;for(let we of U)if(we.t<=pe)oe=we.S;else break;return oe},Ke=pe=>{let oe=1;for(let we of U)if(we.t/xe<=pe)oe=we.S;else break;return oe},ze=Pn();if(Pe){let pe=Array.from(new Set([k/xe,...U.map(oe=>oe.t/xe).filter(oe=>oe>k/xe&&oe<me/xe),me/xe])).sort((oe,we)=>oe-we);for(let oe=0;oe<pe.length-1;oe++){let we=pe[oe],ge=pe[oe+1];pn(ze,Ke(we)*(ge-we),pe.length+1)}}else{let pe=Array.from(new Set([k,...U.map(oe=>oe.t).filter(oe=>oe>k&&oe<me),me])).sort((oe,we)=>oe-we);for(let oe=0;oe<pe.length-1;oe++){let we=pe[oe],ge=pe[oe+1];pn(ze,Oe(we)*(ge-we),pe.length+1)}}let et=Vn(ze)*(Pe?xe:1),Ue=Pn();if(Number.isFinite(me))for(let pe of U){if(pe.t>me||pe.n<=pe.d)continue;let oe=Pn();if(Pe){let ge=Array.from(new Set([pe.t/xe,...U.map(ce=>ce.t/xe).filter(ce=>ce>pe.t/xe&&ce<me/xe),me/xe])).sort((ce,tt)=>ce-tt);for(let ce=0;ce<ge.length-1;ce++)pn(oe,Ke(ge[ce])*(ge[ce+1]-ge[ce]),ge.length+1)}else{let ge=Array.from(new Set([pe.t,...U.map(ce=>ce.t).filter(ce=>ce>pe.t&&ce<me),me])).sort((ce,tt)=>ce-tt);for(let ce=0;ce<ge.length-1;ce++)pn(oe,Oe(ge[ce])*((ge[ce+1]-ge[ce])/xe),ge.length+1)}let we=Vn(oe);pn(Ue,we*we*pe.d/(pe.n*(pe.n-pe.d)),U.length+1)}let Xe=Vn(Ue);q.rmean.push(f==="none"?NaN:et),q.seRmean.push(f==="none"?NaN:Math.sqrt(Xe)*xe),$.push(f==="none"?w==="none"?[X,q.nMax[_],q.nStart[_],q.events[_],q.median[_][0]]:[X,q.nMax[_],q.nStart[_],q.events[_],q.median[_][0],q.median[_][1],q.median[_][2]]:w==="none"?[X,q.nMax[_],q.nStart[_],q.events[_],et,Math.sqrt(Xe)*xe,q.median[_][0]]:[X,q.nMax[_],q.nStart[_],q.events[_],et,Math.sqrt(Xe)*xe,q.median[_][0],q.median[_][1],q.median[_][2]])}return q.table={colNames:[...f==="none"?["records","n.max","n.start","events","median"]:["records","n.max","n.start","events","rmean","se(rmean)","median"],...w==="none"?[]:[`${u}LCL`,`${u}UCL`]],rowNames:A,table:$},Q("survfit",q)}var C1=["tau","covariates","covariateNames","confLevel"];function Ko(e){return 2*se(-Math.abs(e))}function Vu(e,t,n){return[e-n*t,e+n*t]}function Du(e,t,n){return{estimate:e,se:t,confInt:Vu(e,t,n)}}function F1(e,t,n,r){return{estimate:e,confInt:Vu(e,t,n),pValue:Ko(r)}}function Pu(e,t){let n=mr(e.map(l=>l.time),e.map(l=>l.status),{rmean:t}),r=n.rmean[0],o=n.seRmean[0],s=0,i=1,c=Pn();for(let l=0;l<n.time.length&&n.time[l]<t;l++){let f=n.time[l];pn(c,(1-i)*(f-s),n.time.length+1),i=n.surv[l],s=f}pn(c,(1-i)*(t-s),n.time.length+1);let a=Vn(c);return{rmst:{estimate:r,se:o},rmtl:{estimate:a,se:o}}}function bc(e,t,n,r){if(n==="difference"){let l=e.estimate-t.estimate,f=Math.hypot(e.se,t.se);return F1(l,f,r,l/f)}let o=e.estimate,s=t.estimate;if(o===0||s===0)return{estimate:o===0?s===0?NaN:0:1/0,confInt:[NaN,NaN],pValue:NaN};if(!(o>0&&s>0))return{estimate:NaN,confInt:[NaN,NaN],pValue:NaN};let i=o/s,c=Math.log(i),a=Math.hypot(e.se/o,t.se/s);return{estimate:i,confInt:[Math.exp(c-r*a),Math.exp(c+r*a)],pValue:Ko(c/a)}}function O1(e){let t=new Array(e.length).fill(0);for(let n of[0,1]){let r=e.flatMap((s,i)=>s.arm===n?[i]:[]),o=mr(r.map(s=>e[s].time),r.map(s=>e[s].status===0?1:0),{rmean:"none"});for(let s of r){let i=e[s];if(i.status===0)continue;let c=1;for(let a=0;a<o.time.length&&o.time[a]<=i.time;a++)c=o.surv[a];if(!(c>0))throw new Error("the censoring survival probability is zero");t[s]=1/c}}return t}function vc(e,t,n,r,o,s){let i=e.map(L=>[L.arm,...L.x]),c=i[0].map((L,k)=>{let F=i.reduce((O,q)=>Math.max(O,Math.abs(q[k])),0);if(F===0)return 1;let I=Math.floor(Math.log2(F));return I>500||I<-500?2**I:1}),a=i.map(L=>L.map((k,F)=>k/c[F])),l=e.map((L,k)=>[1,...a[k]]),f=[1,...c],m=O1(e),u=r?Va(a,n,{family:"poisson",weights:m}):$o(a,n,{weights:m});if(r&&!u.converged)throw new Error("the adjusted RMST Poisson model did not converge");let p=u.coefficients.map((L,k)=>L/f[k]),w=u.fittedValues;if(p.some(L=>!Number.isFinite(L))||w.some(L=>!Number.isFinite(L)))throw new Error("the adjusted RMST model is singular or has non-finite fitted values");let d=l.map((L,k)=>L.map(F=>m[k]*F*(n[k]-w[k]))),b=d.map(L=>L.slice());for(let L of[0,1]){let k=e.flatMap((I,O)=>I.arm===L?[O]:[]),F=mr(k.map(I=>e[I].time),k.map(I=>e[I].status===0?1:0),{rmean:"none"});for(let I=0;I<F.time.length;I++){let O=F.time[I],q=F.nEvent[I];if(!(O<t)||q===0)continue;let $=F.nRisk[I];if(!($>0))continue;let _=new Array(l[0].length).fill(0);for(let K=0;K<_.length;K++){let X=Pn();for(let re of k)e[re].time>=O&&pn(X,d[re][K],k.length);_[K]=Vn(X)}let P=q/$;for(let K of k){let X=e[K],re=X.time===O&&X.status===0,z=X.time>=O;for(let J=0;J<_.length;J++)b[K][J]+=_[J]/$*(Number(re)-(z?P:0))}}}let N=l.map((L,k)=>L.map(F=>F*Math.sqrt(r?w[k]:1))),v=Wa(N),E=Wa(b),g=nt(v),x=Ba(Ba(g,E),g).map((L,k)=>L.map((F,I)=>F/f[k]/f[I])),M=x.map((L,k)=>Math.sqrt(Math.max(0,L[k]))),R=p.map((L,k)=>L/M[k]),T=R.map(Ko),S=p.map((L,k)=>L-s*M[k]),A=p.map((L,k)=>L+s*M[k]),C=r?[S.map(Math.exp),A.map(Math.exp)]:[S,A];return{coef:p,cov:x,table:{names:["intercept","arm",...o],estimate:p,se:M,z:R,pValue:T,confInt:C}}}function D1(e,t,n,r={}){if(B(r,C1,"rmst2"),e.length!==t.length||e.length!==n.length)throw new RangeError("'time', 'status' and 'arm' must have the same length");if(r.tau!==void 0&&typeof r.tau!="number")throw new TypeError("'tau' must be a number");if(r.confLevel!==void 0&&typeof r.confLevel!="number")throw new TypeError("'confLevel' must be a number");if(r.covariateNames!==void 0&&!Array.isArray(r.covariateNames))throw new TypeError("'covariateNames' must be an array of strings");let o=r.confLevel??.95;if(!(o>0&&o<1))throw new RangeError("'confLevel' must be between 0 and 1");let s=e.reduce((z,J)=>Number.isFinite(J)?Math.max(z,Math.abs(J)):z,0),i=s>2**500?2**Math.floor(Math.log2(s)):1,a=e.every(z=>!Number.isFinite(z)||z===0||z/i!==0)&&(r.tau===void 0||r.tau===0||r.tau/i!==0)?i:1,l=z=>z/a,f=Array.from(t),m=new Set;for(let z=0;z<f.length;z++){let J=n[z]===!0?1:n[z]===!1?0:n[z];(J===0||J===1)&&f[z]===2&&m.add(J)}let u=r.covariates?.[0],h=r.covariates!==void 0&&typeof u=="number";if(r.covariates!==void 0&&r.covariates.length>0&&!h&&!Array.isArray(u))throw new TypeError("'covariates' must be a numeric vector or a matrix of numeric rows");let p=r.covariates===void 0?void 0:h?r.covariates.map(z=>[z]):r.covariates.map(z=>Array.from(z));if(p&&p.length!==e.length)throw new RangeError("'covariates' must have one row per observation");if(p&&p.some(z=>z.length!==p[0].length))throw new RangeError("'covariates' must have the same number of columns in every row");if(p&&p.some(z=>z.some(J=>typeof J!="number")))throw new TypeError("'covariates' must contain numeric values");if(p&&p.some(z=>z.some(J=>!Number.isFinite(J)&&!Number.isNaN(J))))throw new RangeError("'covariates' must be finite");let w=[],d=[],b=[],N=[];for(let z=0;z<e.length;z++){if(e[z]==null||Number.isNaN(e[z])||f[z]==null||Number.isNaN(f[z])||n[z]==null||Number.isNaN(n[z]))throw new Error("missing value where TRUE/FALSE needed");if(typeof e[z]!="number")throw new TypeError("'time' must contain numbers");if(!Number.isFinite(e[z]))throw new RangeError("'time' must be finite");if(typeof n[z]!="number"&&typeof n[z]!="boolean")throw new TypeError("'arm' must contain numbers or booleans");let J=n[z]===!0?1:n[z]===!1?0:n[z];if(J!==0&&J!==1){b.push(l(e[z]));continue}if(typeof f[z]!="number")throw new TypeError("'status' must contain numbers");let Z=f[z],Ae=Z===1?1:0,ne=p?p[z]:[];if(ne.some(Y=>!Number.isFinite(Y)))continue;d.push({time:l(e[z]),status:Ae,arm:J,x:ne,index:z});let D=Z;if(m.has(J))if(Z===2)D=1;else if(Z===1)D=0;else{N.includes("Invalid status value, converted to NA")||N.push("Invalid status value, converted to NA");continue}else if(D!==0&&D!==1){N.includes("Invalid status value, converted to NA")||N.push("Invalid status value, converted to NA");continue}w.push({time:l(e[z]),status:D,arm:J,x:ne,index:z})}if(d.some(z=>z.time<0))throw new Error("negative follow-up times are not supported");if(!w.some(z=>z.arm===0)||!w.some(z=>z.arm===1))throw new Error("both arms 0 and 1 are required");let v=[0,1].map(z=>Math.max(...d.filter(J=>J.arm===z).map(J=>J.time))),E=[0,1].map(z=>{let J=v[z];return d.some(Z=>Z.arm===z&&Z.time===J&&Z.status===0)}),g=E.some(Boolean)?Math.min(...v.filter((z,J)=>E[J])):Math.max(...v),y=r.tau===void 0,x=r.tau===void 0?g:l(r.tau),M=r.tau===void 0?x*a:r.tau;if(b.some(z=>z<=x))throw new RangeError("'arm' must contain only 0 and 1 through tau");if(!(x>0))throw new RangeError("'tau' must be greater than zero");if(x>g)throw new RangeError(`'tau' must be at most ${g*a}: the minimum of the largest times allowed by both arms`);if(x<Math.min(...d.map(z=>z.time)))throw new RangeError("'tau' must not be below the first observed time");let R=(1-o)/2,T=R<1e-8?le(R,{lowerTail:!1}):le(1-R),S=w.filter(z=>z.arm===1),A=w.filter(z=>z.arm===0),C=Pu(S,x),L=Pu(A,x),k=z=>({estimate:z.estimate*a,se:z.se*a}),F={rmst:k(C.rmst),rmtl:k(C.rmtl)},I={rmst:k(L.rmst),rmtl:k(L.rmtl)},O=Du(F.rmst.estimate,F.rmst.se,T),q=Du(I.rmst.estimate,I.rmst.se,T),$={estimate:F.rmtl.estimate,se:F.rmtl.se,confInt:[M-O.confInt[1],M-O.confInt[0]]},_={estimate:I.rmtl.estimate,se:I.rmtl.se,confInt:[M-q.confInt[1],M-q.confInt[0]]},P=bc(F.rmst,I.rmst,"difference",T),K=bc(F.rmst,I.rmst,"ratio",T),X=bc(F.rmtl,I.rmtl,"rmtlRatio",T),re={tau:M,tauDefault:y,confLevel:o,arm1:{rmst:O,rmtl:$},arm0:{rmst:q,rmtl:_},difference:P,ratio:K,rmtlRatio:X,warnings:N};if(p){let z=r.covariateNames===void 0?h?["covariates"]:Array.from({length:p[0].length},()=>""):Array.from(r.covariateNames);if(z.some(be=>typeof be!="string"))throw new TypeError("'covariateNames' must contain strings");if(z.length!==p[0].length)throw new RangeError("'covariateNames' must have one name per covariate column");let J=w.map(be=>({...be,time:Math.min(be.time,x),status:be.time>=x?1:be.status})),Z=J.map(be=>be.time),Ae=J.map(be=>x-be.time),ne=vc(J,x,Z,!1,z,T),D=!(F.rmst.estimate>0&&I.rmst.estimate>0),Y=!(F.rmtl.estimate>0&&I.rmtl.estimate>0),j=D?void 0:vc(J,x,Z,!0,z,T),U=Y?void 0:vc(J,x,Ae,!0,z,T),W=(be,me)=>{let ke=be.coef[1],de=Math.sqrt(Math.max(0,be.cov[1][1]));return me?{estimate:Math.exp(ke),confInt:[Math.exp(ke-T*de),Math.exp(ke+T*de)],pValue:Ko(ke/de)}:{estimate:ke*a,confInt:[(ke-T*de)*a,(ke+T*de)*a],pValue:Ko(ke/de)}},te=be=>({...be,estimate:be.estimate.map(me=>me*a),se:be.se.map(me=>me*a),confInt:[be.confInt[0].map(me=>me*a),be.confInt[1].map(me=>me*a)]}),fe=be=>({...be,estimate:be.estimate.map((me,ke)=>ke===0?me+Math.log(a):me),confInt:[be.confInt[0].map((me,ke)=>ke===0?me+Math.log(a):me),be.confInt[1].map((me,ke)=>ke===0?me+Math.log(a):me)]}),Re={names:["intercept","arm",...z],estimate:Array(ne.coef.length).fill(NaN),se:Array(ne.coef.length).fill(NaN),z:Array(ne.coef.length).fill(NaN),pValue:Array(ne.coef.length).fill(NaN),confInt:[Array(ne.coef.length).fill(NaN),Array(ne.coef.length).fill(NaN)]},Ce={estimate:NaN,confInt:[NaN,NaN],pValue:NaN};re.adjusted={difference:W(ne,!1),ratio:D?Ce:W(j,!0),rmtlRatio:Y?Ce:W(U,!0)},re.coefficients={difference:te(ne.table),ratio:j?fe(j.table):Re,rmtlRatio:U?fe(U.table):Re},delete re.arm1,delete re.arm0}return Q("rmst2",re)}var P1=new Set(["start","id","ties","weights","strata","init","control","nocenter","confLevel","robust","cluster"]),V1=["eps","iterMax","tolerChol","tolerInf","timefix"],Nc=e=>e==null||typeof e=="number"&&Number.isNaN(e),$1=(e,t)=>typeof e=="number"&&typeof t=="number"?e-t:String(e)<String(t)?-1:String(e)>String(t)?1:0,z1=2**64,j1=2**-1022,$u=700;function ia(e,t,n){let{time:r,event:o,weight:s,x:i,stratumEnd:c,logUnit:a,start:l}=e,f=r.length,m=t.length,u=()=>new Array(m).fill(0),h=()=>Array.from({length:m},u),p=u(),w=h(),d=0,b=0,N=u(),v=h(),E=u(),g=h(),y=0,x=0,M=R=>{let T=Math.exp(x-R);b*=T,y*=T;for(let S=0;S<m;S++){N[S]*=T,E[S]*=T;let A=v[S],C=g[S];for(let L=0;L<=S;L++)A[L]*=T,C[L]*=T}x=R};for(let R=f-1;R>=0;){c[R]&&(x=0,b=0,N=u(),v=h());let T=r[R],S=!1;if(l!==void 0){let k=R;for(;k>0&&r[k-1]===T&&!c[k-1];)k--;for(let F=k;F<=R;F++)o[F]&&(S=!0)}if(l!==void 0&&S){let k=R;for(;k>0&&!c[k-1];)k--;let F=R;for(;!c[F];)F++;let I=q=>{let $=0;for(let _=0;_<m;_++)$+=t[_]*i[q][_];return $},O=-1/0;for(let q=k;q<=F;q++)l[q]<T&&r[q]>=T&&(O=Math.max(O,I(q)));x=Number.isFinite(O)?O:0,b=0,N=u(),v=h();for(let q=k;q<=F;q++){if(!(l[q]<T&&r[q]>=T)||r[q]===T&&o[q])continue;let $=Math.exp(I(q)-x)*s[q];b+=$;for(let _=0;_<m;_++){N[_]+=$*i[q][_];for(let P=0;P<=_;P++)v[_][P]+=$*i[q][_]*i[q][P]}}}let A=0,C=0;y=0;let L=R;do{let k=i[L],F=0;for(let q=0;q<m;q++)F+=t[q]*k[q];let I=s[L];Number.isFinite(F)&&(F-x>$u?M(F):F-x<-$u&&b===0&&y===0&&(x=F));let O=Math.exp(F-x)*I;if(o[L]){A++,C+=I,y+=O,d+=I*F;for(let q=0;q<m;q++){p[q]+=I*k[q],E[q]+=O*k[q];let $=g[q];for(let _=0;_<=q;_++)$[_]+=O*k[q]*k[_]}}else if(l===void 0){b+=O;for(let q=0;q<m;q++){N[q]+=O*k[q];let $=v[q];for(let _=0;_<=q;_++)$[_]+=O*k[q]*k[_]}}L--}while(L>=0&&r[L]===T&&!c[L]);if(A>0){let k=n?A:1,F=n?A:1,I=C/k;for(let O=0;O<k;O++){b+=n?y/F:y,d-=I*(Math.log(b)+x+a);for(let q=0;q<m;q++){N[q]+=n?E[q]/F:E[q];let $=N[q]/b;p[q]-=I*$;let _=v[q],P=g[q],K=w[q];for(let X=0;X<=q;X++){_[X]+=n?P[X]/F:P[X];let re=_[X]-$*N[X],z=I*re;K[X]+=z<1/0&&z>-1/0&&(Math.abs(z)>=j1||re===0)||!Number.isFinite(I)?z/b:I*(re/b)}}}E.fill(0);for(let O of g)O.fill(0)}R=L}for(let R=0;R<m;R++)for(let T=0;T<R;T++)w[T][R]=w[R][T];return{loglik:d,score:p,info:w}}function zu(e,t,n){let{time:r,event:o,x:s,stratumEnd:i}=e,c=qe([e.weight]).groups[0],a=r.length,l=t.length,f=Array.from({length:a},()=>new Array(l).fill(0)),m=u=>{let h=0;for(let p=0;p<l;p++)h+=t[p]*s[u][p];return h};for(let u=0;u<a;){let h=u;for(;!i[h];)h++;let p=[];for(let R=u;R<=h;R++)p.push(m(R));let w=[];for(let R=u;R<=h;R++)(R===u||r[R]!==r[R-1])&&w.push(R);let d=R=>R+1<w.length?w[R+1]:h+1,b=new Array(w.length),N=new Array(w.length),v=new Array(w.length),E=-1/0,g=0,y=new Array(l).fill(0);for(let R=w.length-1;R>=0;R--){let T=Number.isFinite(E)?E:0;for(let A=w[R];A<d(R);A++)E=Math.max(E,p[A-u]);let S=Number.isFinite(E)?E:0;if(R<w.length-1&&S!==T){let A=Math.exp(T-S);g*=A;for(let C=0;C<l;C++)y[C]*=A}for(let A=w[R];A<d(R);A++){let C=c[A]*Math.exp(p[A-u]-S);g+=C;for(let L=0;L<l;L++)y[L]+=C*s[A][L]}b[R]=g,N[R]=y.slice(),v[R]=S}let x=0,M=new Array(l).fill(0);for(let R=0;R<w.length;R++){let T=v[R];if(R>0&&T!==v[R-1]){let q=Math.exp(T-v[R-1]);x*=q;for(let $=0;$<l;$++)M[$]*=q}let S=w[R],A=d(R),C=[];for(let q=S;q<A;q++)C.push(Math.exp(p[q-u]-T));let L=0,k=0,F=0,I=new Array(l).fill(0);for(let q=S;q<A;q++){if(!o[q])continue;let $=c[q]*C[q-S];L++,k+=c[q],F+=$;for(let _=0;_<l;_++)I[_]+=$*s[q][_]}for(let q=S;q<A;q++){let $=C[q-S],_=s[q],P=f[q];for(let K=0;K<l;K++)P[K]=-$*(_[K]*x-M[K])}if(L===0)continue;let O=n?L:1;for(let q=0;q<O;q++){let $=q/O,_=b[R]-$*F,P=N[R].map((X,re)=>(X-$*I[re])/_),K=k/O/_;for(let X=S;X<A;X++){let re=C[X-S],z=s[X],J=f[X];if(o[X])for(let Z=0;Z<l;Z++){let Ae=z[Z]-P[Z];J[Z]+=Ae/O-(1-$)*re*Ae*K}else for(let Z=0;Z<l;Z++)J[Z]-=re*(z[Z]-P[Z])*K}x+=K;for(let X=0;X<l;X++)M[X]+=K*P[X]}}u=h+1}return f}function yc(e,t,n){let r=e.start,{time:o,event:s,x:i,stratumEnd:c}=e,a=qe([e.weight]).groups[0],l=o.length,f=t.length,m=Array.from({length:l},()=>new Array(f).fill(0)),u=o.map((h,p)=>{let w=0;for(let d=0;d<f;d++)w+=t[d]*i[p][d];return w});for(let h=0;h<l;){let p=h;for(;!c[p];)p++;for(let w=h;w<=p;){let d=o[w],b=w;for(;b<p&&o[b+1]===d;)b++;let N=[],v=[],E=-1/0;for(let g=w;g<=b;g++)s[g]&&v.push(g);if(v.length>0)for(let g=h;g<=p;g++)r[g]<d&&o[g]>=d&&(N.push(g),E=Math.max(E,u[g]));if(v.length>0&&N.length>0){Number.isFinite(E)||(E=0);let g=0,y=new Array(f).fill(0),x=0,M=new Array(f).fill(0),R=0;for(let S of N){let A=Math.exp(u[S]-E),C=a[S]*A;g+=C;for(let L=0;L<f;L++)y[L]+=C*i[S][L];if(s[S]&&o[S]===d){R+=a[S],x+=C;for(let L=0;L<f;L++)M[L]+=C*i[S][L]}}let T=n?v.length:1;for(let S=0;S<T;S++){let A=S/T,C=g-A*x,L=R/T/C,k=y.map((F,I)=>(F-A*M[I])/C);for(let F of N){let I=Math.exp(u[F]-E),O=s[F]&&o[F]===d;for(let q=0;q<f;q++){let $=i[F][q]-k[q];O?m[F][q]+=$/T-(1-A)*I*$*L:m[F][q]-=I*$*L}}}}w=b+1}h=p+1}return m}function Uo(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 ca(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 la(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 Ho=(e,t)=>({statistic:e,df:t,pValue:It(e,t,{lowerTail:!1})});function W1(e,t,n){let r=e.length,o=()=>new Array(t).fill(NaN),s=Ho(0,0),i=new Array(t).fill(0),c=o(),a=o();return{coefficients:o(),var:Array.from({length:t},()=>new Array(t).fill(0)),loglik:[0,0],score:0,wald:0,iter:0,means:Array.from({length:t},(l,f)=>it(e.map(m=>m[f]))),n:r,nevent:0,linearPredictors:new Array(r).fill(0),se:i,z:c,p:a,stdError:i,zValue:c,pValue:a,lower:o(),upper:o(),logtest:s,sctest:{...s},waldtest:{...s},rsq:{rsq:0,maxrsq:0},usedRobust:!1,warnings:n}}function B1(e,t,n,r={}){return B(r,P1,"coxph"),B(Fn(r,"control"),V1,"coxph","control"),Q("coxph",K1(e,t,n,r).result)}function K1(e,t,n,r={},o=!1){let{ties:s="efron",weights:i,strata:c,init:a,control:l={},nocenter:f=[-1,0,1],confLevel:m=.95,robust:u,cluster:h,start:p,id:w}=r,{eps:d=1e-9,iterMax:b=20,tolerChol:N=2**-39}=l,v=l.tolerInf??Math.sqrt(d);if(l.timefix!==void 0&&typeof l.timefix!="boolean")throw new TypeError("'timefix' must be a boolean");let E=l.timefix??!0;if(s!=="efron"&&s!=="breslow")throw new RangeError(`invalid 'ties': ${String(s)}`);if(!(d>0&&d<1/0))throw new RangeError("'eps' must be a positive finite number");if(!(N>0&&N<1/0))throw new RangeError("'tolerChol' must be a positive finite number");if(!(v>0&&v<1/0))throw new RangeError("'tolerInf' must be a positive finite number");if(!(Number.isInteger(b)&&b>=0))throw new RangeError("'iterMax' must be a non-negative integer");if(!(m>0&&m<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(i!==void 0&&i.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'");if(h!==void 0&&h.length!==g)throw new RangeError("'cluster' must have the same length as 'time'");if(p!==void 0&&p.length!==g)throw new RangeError("'start' must have the same length as 'time'");if(w!==void 0&&w.length!==g)throw new RangeError("'id' must have the same length as 'time'");if(u!==void 0&&typeof u!="boolean")throw new TypeError("'robust' must be true or false");let y=g===0?0:n[0].length;if(y<1)throw new RangeError("'x' must have at least one column");if(n.some(H=>H.length!==y))throw new RangeError("the rows of 'x' must have the same length");if(a!==void 0&&a.length!==y)throw new RangeError("'init' must have one value per column of 'x'");if(a?.some(H=>!Number.isFinite(H)))throw new RangeError("'init' must contain finite values");let x=new Rt,M=[];for(let H=0;H<g;H++){if(Number.isNaN(e[H])||Number.isNaN(t[H])||n[H].some(Ie=>Number.isNaN(Ie))||p!==void 0&&Number.isNaN(p[H])||i!==void 0&&Number.isNaN(i[H])||c!==void 0&&Nc(c[H])||h!==void 0&&Nc(h[H])||w!==void 0&&Nc(w[H]))continue;if(p!==void 0&&!(p[H]<e[H]))throw new RangeError("stop time must be > start time");let ae=t[H];if(ae!==0&&ae!==1){if(p===void 0)throw new RangeError("'status' must be 0 (censored) or 1 (event)");x.add("Invalid status value, converted to NA");continue}if(e[H]===-1/0||e[H]===1/0&&ae===1)throw new RangeError("event times must be finite");M.push(H)}let R=M.length;if(R===0)throw new RangeError(p===void 0?"no observations without missing values":"No (non-missing) observations");if(M.some(H=>n[H].some(ae=>!Number.isFinite(ae))))throw new RangeError("'x' contains infinite values");let T=M.map(H=>i===void 0?1:i[H]);if(T.some(H=>!(H>0)))throw new RangeError("Invalid weights, must be >0");if(T.some(H=>H===1/0))throw new RangeError("'weights' must be finite");let S=w===void 0?void 0:Pt(w),A=new Map;if(S!==void 0)for(let H of M){if(t[H]!==1)continue;let ae=S[H];A.set(ae,(A.get(ae)??0)+1)}let C=p!==void 0&&[...A.values()].some(H=>H>1),L=u??(h!==void 0||T.some(H=>!Number.isInteger(H))||C);if(p!==void 0&&u===!0&&h===void 0&&w===void 0)throw new RangeError("one of cluster or id is needed");h!==void 0&&!L&&x.add("cluster specified with robust=FALSE, cluster ignored");let k=0;for(let H of M)k+=t[H];let F=M.map(H=>e[H]),I=p===void 0?void 0:M.map(H=>p[H]),O,q;if(I===void 0)O=E?Xr(F):F;else if(!E)O=F,q=I;else{let H=Xr([...I,...F]);if(q=H.slice(0,R),O=H.slice(R),q.some((ae,Ie)=>ae>=O[Ie]))throw new Error("aeqSurv exception, an interval has effective length 0")}if(k===0){let H=W1(M.map(ae=>n[ae]),y,x.list);return S!==void 0&&(H.nId=new Set(M.map(ae=>S[ae])).size),{result:H}}let $=new Array(R).fill(0),_,P=[];if(c!==void 0){let H=Pt(c);P=Array.from(new Set(M.map(Ie=>H[Ie]))).sort($1);let ae=new Map;P.forEach((Ie,Ge)=>ae.set(Ie,Ge)),_=M.map(Ie=>H[Ie]),$=M.map(Ie=>ae.get(H[Ie]))}let K=Array.from({length:R},(H,ae)=>ae).sort((H,ae)=>$[H]-$[ae]||O[H]-O[ae]||H-ae),X=K.map(H=>T[H]),{groups:re,k:z}=Nt(X)>=z1?qe([X]):{groups:[X],k:0},J=re[0],Z=0;for(let H of T)Z+=z===0?H:V(H,-z);let Ae=K.map(H=>n[M[H]].slice()),ne=new Array(y).fill(0),D=new Array(y).fill(1),Y=new Array(y).fill(0),j=new Set(f);for(let H=0;H<y;H++){if(Ae.every(He=>j.has(He[H])))continue;let ae=Ae.map(He=>He[H]),Ie=He=>{let zt=0;for(let Cn=0;Cn<R;Cn++)zt+=(p===void 0?J[Cn]:1)*He[Cn];let Ln=p===void 0?Z:R,gr=zt/Ln,Dr=0,Ta=He.map((Cn,rs)=>(Dr+=(p===void 0?J[rs]:1)*Math.abs(Cn-gr),Cn-gr)),Pr=Dr===0?1:Ln/Dr;return{m:gr,s:Pr,d:Ta,ok:Number.isFinite(gr)&&Pr>=2**-1022&&Pr<1/0}},Ge=Ie(ae);if(!Ge.ok){let{groups:He,k:zt}=qe([ae]);Ge=Ie(He[0]),Y[H]=zt}for(let He=0;He<R;He++)Ae[He][H]=Ge.d[He]*Ge.s;ne[H]=V(Ge.m,Y[H]),D[H]=Ge.s}let U={time:K.map(H=>O[H]),...q===void 0?{}:{start:K.map(H=>q[H])},event:K.map(H=>t[M[H]]===1),weight:J,x:Ae,stratumEnd:K.map((H,ae)=>ae===R-1||$[K[ae+1]]!==$[H]),logUnit:z*Math.LN2},W=s==="efron",te=a===void 0?new Array(y).fill(0):a.map((H,ae)=>V(H/D[ae],Y[ae])),fe=ia(U,te,W),Re=[fe.loglik,fe.loglik],Ce=fe.info.map(H=>H.slice()),be=Uo(Ce,N),me=ca(Ce,fe.score),ke=0;for(let H=0;H<y;H++)ke+=fe.score[H]*me[H];let de=0,Fe,xe=!1,Pe=b===0;if(b===0||!Number.isFinite(fe.loglik))Fe=la(Ce);else{let H=fe.loglik,ae=te.map((Ge,He)=>Ge+me[He]),Ie=0;for(Pe=!1,de=1;de<=b;de++){fe=ia(U,ae,W),Ce=fe.info.map(He=>He.slice()),be=Uo(Ce,N);let Ge=Number.isFinite(fe.loglik)&&fe.score.every(Number.isFinite)&&fe.info.every(He=>He.every(Number.isFinite));if(Ge&&(fe.loglik===H||Math.abs(1-H/fe.loglik)<=d)){Pe=!0,Re[1]=fe.loglik,te=ae;break}!Ge||fe.loglik<H?(Ie++,ae=ae.map((He,zt)=>(He+Ie*te[zt])/(Ie+1))):(Ie=0,H=fe.loglik,Re[1]=H,me=ca(Ce,fe.score),te=ae,ae=ae.map((He,zt)=>He+me[zt]))}Pe?Fe=la(Ce):(de=p===void 0?b+1:b,xe=!0,p!==void 0?(fe=ia(U,te,W),Re[1]=fe.loglik,Ce=fe.info.map(Ge=>Ge.slice()),be=Uo(Ce,N),Fe=la(Ce)):b>1?(fe=ia(U,te,W),Re[1]=fe.loglik,Fe=fe.info.map((Ge,He)=>Ge.map((zt,Ln)=>He===Ln?Ge[He]>0?1/Ge[He]:0:-0))):Fe=la(Ce))}let Oe=D.map(H=>Math.floor(Math.log2(H))),Ke=D.map((H,ae)=>V(H,-Oe[ae])),ze=Oe.map((H,ae)=>H-Y[ae]),et=te.map((H,ae)=>H*Ke[ae]),Ue=fe.score.map((H,ae)=>H/Ke[ae]),Xe=Fe.map((H,ae)=>H.map((Ie,Ge)=>Ie*Ke[ae]*Ke[Ge])),pe=et.map((H,ae)=>V(H,ze[ae])),oe=Xe.map((H,ae)=>H.map((Ie,Ge)=>V(Ie,ze[ae]+ze[Ge]-z))),we=pe.slice();if(b>0&&Math.abs(be)<y)for(let H=0;H<y;H++)Fe[H][H]===0&&(we[H]=NaN);let ge=p===void 0?pe:pe.map((H,ae)=>Number.isNaN(we[ae])?0:H),ce=0;for(let H=0;H<y;H++)ce+=ge[H]*ne[H];let tt=M.map(H=>{let ae=0;for(let Ie=0;Ie<y;Ie++)ae+=n[H][Ie]*ge[Ie];return ae-ce});if(b>1){let H=Xe.map((ae,Ie)=>{let Ge=0;for(let He=0;He<y;He++)Ge+=Ue[He]*Xe[He][Ie];return Math.abs(Ge)});if(xe)(p===void 0||b>1)&&x.add("Ran out of iterations and did not converge"),p===void 0&&(Nt(tt)>500||H.some(ae=>!Number.isFinite(ae)))&&x.add("one or more coefficients may be infinite");else if(p===void 0){let ae=[];for(let Ie=0;Ie<y;Ie++){let Ge=H[Ie];(!Number.isFinite(Ue[Ie])||V(Ge,ze[Ie])>d&&Ge>v*Math.abs(et[Ie]))&&ae.push(Ie+1)}ae.length>0&&x.add("Loglik converged before variable "+ae.join(",")+" ; coefficient may be infinite. ")}}let G=we.map((H,ae)=>Number.isNaN(H)?-1:ae).filter(H=>H>=0),ue=G.length,Me,Ne=0,Ee=0,ee;if(L&&ue>0){let H=K.map((lt,Qe)=>Qe),ae=R,Ie=h??(p===void 0?void 0:w);if(Ie!==void 0){let lt=Pt(Ie),Qe=new Map;H=K.map(pt=>{let yt=lt[M[pt]],Mt=Qe.get(yt);return Mt===void 0&&(Mt=Qe.size,Qe.set(yt,Mt)),Mt}),ae=Qe.size}if(ae<2)throw new RangeError(p===void 0?"at least two clusters are needed for the robust variance":"(subscript) logical subscript too long");let Ge=lt=>lt.map(Qe=>Qe.map((pt,yt)=>pt/Ke[yt])),He=Ge(p===void 0?zu(U,te,W):yc(U,te,W)),zt=He.map(lt=>Xe.map((Qe,pt)=>{let yt=0;for(let Mt=0;Mt<y;Mt++)yt+=lt[Mt]*Xe[Mt][pt];return yt})),Ln;if(z===0)Ln=zt.map((lt,Qe)=>lt.map(pt=>J[Qe]*pt));else{let lt=pt=>pt===0||!Number.isFinite(pt)?0:Math.floor(Math.log2(Math.abs(pt))),Qe=-1/0;zt.forEach((pt,yt)=>pt.forEach(Mt=>Mt!==0&&Number.isFinite(Mt)&&(Qe=Math.max(Qe,lt(J[yt])+lt(Mt))))),Ne=Qe===-1/0?0:-Qe,Ln=zt.map((pt,yt)=>{let Mt=lt(J[yt]);return pt.map(wr=>{let tl=lt(wr);return V(V(J[yt],-Mt)*V(wr,-tl),Mt+tl+Ne)})})}let gr=lt=>{let Qe=Array.from({length:ae},()=>new Array(y).fill(0));return lt.forEach((pt,yt)=>{let Mt=Qe[H[yt]];for(let wr=0;wr<y;wr++)Mt[wr]+=pt[wr]}),Qe},Dr=lt=>{let Qe=Array.from({length:y},()=>new Array(y).fill(0));for(let pt of lt)for(let yt=0;yt<y;yt++)for(let Mt=0;Mt<y;Mt++)Qe[yt][Mt]+=pt[yt]*pt[Mt];return Qe};Me=Dr(gr(Ln));let Ta=qe([X]).groups[0],Pr=a===void 0?new Array(y).fill(0):a.map((lt,Qe)=>V(lt/D[Qe],Y[Qe])),Cn=p===void 0?zu(U,Pr,W):yc(U,Pr,W),rs=gr(Ge(Cn).map((lt,Qe)=>lt.map(pt=>Ta[Qe]*pt))),go=new Array(y).fill(0);for(let lt of rs)for(let Qe=0;Qe<y;Qe++)go[Qe]+=lt[Qe];let Sa=Dr(rs);if(y===1)Ee=go[0]*go[0]/Sa[0][0];else{Uo(Sa,N);let lt=ca(Sa,go);for(let Qe=0;Qe<y;Qe++)Ee+=go[Qe]*lt[Qe]}let Jc=new Array(R),el=new Array(R);K.forEach((lt,Qe)=>{Jc[lt]=He[Qe].map((pt,yt)=>V(pt,-ze[yt])),el[lt]=Ln[Qe].map((pt,yt)=>V(pt,ze[yt]-Ne))}),ee={scoreResiduals:Jc,dfbeta:el}}let ve=Me!==void 0,ie=Me??Xe,Te=Me===void 0?-z:-2*Ne,Be=G.map(H=>et[H]-(a===void 0?0:V(a[H],-ze[H]))),$e=0;if(ue===1)$e=Be[0]*Be[0]/ie[G[0]][G[0]];else if(ue>1){let H=G.map(Ie=>G.map(Ge=>ie[Ie][Ge]));Uo(H,N);let ae=ca(H,Be);for(let Ie=0;Ie<ue;Ie++)$e+=Be[Ie]*ae[Ie]}$e=V($e,-Te);let st=(1-m)/2,mt=st<1e-8?le(st,{lowerTail:!1}):le((1+m)/2),Ct=(H,ae)=>{let Ie=Math.floor(ae/2),Ge=H.map((He,zt)=>Math.sqrt(V(He[zt],ae-2*Ie)));return{unit:Ge,h:Ie,se:Ge.map((He,zt)=>V(He,ze[zt]+Ie))}},Ht=Ct(Xe,-z).se,{unit:Yn,h:je,se:We}=Ct(ie,Te),Tt=we.map((H,ae)=>V((Number.isNaN(H)?H:et[ae])/Yn[ae],-je)),St=Tt.map(H=>It(H*H,1,{lowerTail:!1})),xn=we.map((H,ae)=>Math.exp(H-mt*We[ae])),$t=we.map((H,ae)=>Math.exp(H+mt*We[ae])),kt=V(-2*(Re[0]-Re[1]),z),ut=[V(Re[0],z),V(Re[1],z)],Yt=V(ke,z),on=Me?.map((H,ae)=>H.map((Ie,Ge)=>V(Ie,ze[ae]+ze[Ge]-2*Ne))),Jt={coefficients:we,var:on??oe,loglik:ut,score:Yt,wald:$e,iter:de,means:ne,n:R,nevent:k,...S===void 0?{}:{nId:new Set(M.map(H=>S[H])).size},linearPredictors:tt,se:Ht,z:Tt,p:St,stdError:Ht,zValue:Tt,pValue:St,lower:xn,upper:$t,logtest:Ho(kt,ue),sctest:Ho(Yt,ue),waldtest:Ho($e,ue),rsq:{rsq:1-Math.exp(-kt/R),maxrsq:1-Math.exp(2*ut[0]/R)},usedRobust:ve,...ve?{naiveVar:oe,robustSe:We,rscore:Ee,robscore:Ho(Ee,ue)}:{},warnings:x.list},Ft=fe.score.map((H,ae)=>V(H/D[ae],Y[ae]+z));Object.defineProperty(Jt,"coxZphData",{enumerable:!1,value:{time:O.slice(),...q===void 0?{}:{start:q.slice()},status:M.map(H=>t[H]===1),weights:T.slice(),..._===void 0?{}:{strata:_.slice()},x:M.map(H=>n[H].slice()),sortedToInput:K.slice(),sortedTime:U.time.slice(),...U.start===void 0?{}:{sortedStart:U.start.slice()},sortedEvent:U.event.slice(),sortedWeight:U.weight.slice(),sortedX:U.x.map(H=>H.slice()),stratumIndex:K.map(H=>$[H]),stratumEnd:U.stratumEnd.slice(),betaScaled:te.slice(),inverseCovariateScale:Ke.map((H,ae)=>V(1/H,-ze[ae])),weightExp:z,ties:s}});let en=ee?.scoreResiduals;if(p!==void 0&&o&&en===void 0){let H=yc(U,te,W);en=new Array(R),K.forEach((ae,Ie)=>{en[ae]=H[Ie].map((Ge,He)=>V(Ge/Ke[He],-ze[He]))})}return{result:Jt,...ee,...en===void 0?{}:{scoreResiduals:en},first:Ft,converged:Pe}}var ju=["transform","terms","collapseTerms","global"],Un=e=>new Array(e).fill(0),ur=e=>Array.from({length:e},()=>Un(e));function U1(e){let t=Array.from({length:e.length},(r,o)=>o).sort((r,o)=>e[r]-e[o]||r-o),n=new Array(e.length);for(let r=0;r<t.length;){let o=r+1;for(;o<t.length&&e[t[o]]===e[t[r]];)o++;let s=(r+1+o)/2;for(let i=r;i<o;i++)n[t[i]]=s;r=o}return n}function Wu(e,t){let n=0,r=e.length;for(;n<r;){let o=Math.floor((n+r)/2);e[o]<t?n=o+1:r=o}return n}function H1(e){let t=e.time.length,n=new Array(t),r=new Map;for(let a=0;a<t;a++){let l=e.time[a],f=r.get(l)??{rows:[],deaths:0};f.rows.push(a),e.status[a]&&f.deaths++,r.set(l,f)}let o=[...r.keys()].sort((a,l)=>a-l),s=e.time.slice().sort((a,l)=>a-l),i=(e.start??new Array(t).fill(-1/0)).slice().sort((a,l)=>a-l),c=1;for(let a of o){let l=r.get(a);for(let m of l.rows)n[m]=1-c;let f=Wu(i,a)-Wu(s,a);l.deaths>0&&(c*=1-l.deaths/f)}return n}function Y1(e,t,n,r){let o;if(typeof t=="function"){let s=t(e.time.slice());if(!Array.isArray(s)||s.length!==e.time.length)throw new RangeError("transform function must return one value per observation");o=Array.from(s)}else if(t==="km")o=H1(e);else if(t==="rank")o=U1(e.time);else if(t==="identity")o=e.time.slice();else if(t==="log")e.time.some(s=>s<0)&&r.push("NaNs produced"),o=e.time.map(Math.log);else throw new RangeError("Unrecognized transform");for(let s of n)if(!Number.isFinite(o[s]))throw new RangeError("transform must be finite at every event time");return o}function G1(e,t={}){B(t,ju,"coxZph"),B(t,ju,"coxZph");let n=t.transform??"km",r=t.global??!0,o=t.collapseTerms??!0;if(typeof r!="boolean")throw new TypeError("'global' must be a boolean");if(typeof o!="boolean")throw new TypeError("'collapseTerms' must be a boolean");if(t.terms!==void 0&&t.terms!==!1&&!Array.isArray(t.terms))throw new TypeError("'terms' must be an array of column groups or false");if(e.nevent===0)throw new Error("the proportional-hazards test requires events");let s=e.coxZphData;if(s===void 0)throw new TypeError("coxZph requires a CoxphResult returned by coxph");let i=e.coefficients.map((D,Y)=>Number.isNaN(D)?-1:Y).filter(D=>D>=0),c=i.length;if(c===0)throw new Error("the proportional-hazards test has no estimable coefficients");let a=[];for(let D=0;D<s.sortedTime.length;){let Y=D;for(;!s.stratumEnd[Y];)Y++;for(let j=D;j<=Y;){let U=s.sortedTime[j],W=j;for(;W<Y&&s.sortedTime[W+1]===U;)W++;let te=[];for(let fe=j;fe<=W;fe++)s.sortedEvent[fe]&&te.push(fe);s.start!==void 0&&te.reverse();for(let fe of te)a.push(s.sortedToInput[fe]);j=W+1}D=Y+1}if(a.length===0)throw new Error("the proportional-hazards test requires events");let l=a.map(D=>s.time[D]);if(new Set(l).size<2)throw new Error("the test needs events at two or more distinct times");let f=[...e.warnings],m=Y1(s,n,a,f),u=m[a[0]];if(a.every(D=>m[D]===u))throw new Error("the transform is constant at the event times");let h=a.reduce((D,Y)=>Math.max(D,Math.abs(m[Y])),0),p=h===0?0:Math.floor(Math.log2(h)),w=m.map(D=>Number.isFinite(D)?V(D,-p):D),d=a.reduce((D,Y)=>D+w[Y],0)/a.length,b=w.map(D=>D-d);if(a.every(D=>b[D]===0))throw new Error("the transform is constant at the event times");let N=i.map(D=>s.betaScaled[D]),v=ur(c),E=ur(c),g=ur(c),y=Un(c),x=[];for(let D=0;D<s.sortedTime.length;){let Y=D;for(;!s.stratumEnd[Y];)Y++;for(let j=D;j<=Y;){let U=s.sortedTime[j],W=j;for(;W<Y&&s.sortedTime[W+1]===U;)W++;let te=[];for(let fe=j;fe<=W;fe++)s.sortedEvent[fe]&&te.push(fe);if(te.length>0){let fe=[],Re=-1/0;for(let oe=D;oe<=Y;oe++){if(!((s.sortedStart===void 0||s.sortedStart[oe]<U)&&s.sortedTime[oe]>=U))continue;fe.push(oe);let ge=0;for(let ce=0;ce<c;ce++)ge+=N[ce]*s.sortedX[oe][i[ce]];Re=Math.max(Re,ge)}if(fe.length===0||!Number.isFinite(Re))throw new Error("empty Cox proportional-hazards risk set");let Ce=Un(c),be=ur(c),me=Un(c),ke=ur(c),de=0,Fe=0;for(let oe of fe){let we=0;for(let ce=0;ce<c;ce++)we+=N[ce]*s.sortedX[oe][i[ce]];let ge=s.sortedWeight[oe]*Math.exp(we-Re);de+=ge;for(let ce=0;ce<c;ce++){let tt=s.sortedX[oe][i[ce]];Ce[ce]+=ge*tt;for(let G=0;G<c;G++)be[ce][G]+=ge*tt*s.sortedX[oe][i[G]]}if(s.sortedEvent[oe]&&s.sortedTime[oe]===U){Fe+=ge;for(let ce=0;ce<c;ce++){let tt=s.sortedX[oe][i[ce]];me[ce]+=ge*tt;for(let G=0;G<c;G++)ke[ce][G]+=ge*tt*s.sortedX[oe][i[G]]}}}let xe=0,Pe=Un(c);for(let oe of te){let we=s.sortedWeight[oe];xe+=we;for(let ge=0;ge<c;ge++)Pe[ge]+=we*s.sortedX[oe][i[ge]]}let Oe=te.length,Ke=s.ties==="efron"&&Oe>1?Oe:1,ze=Un(c),et=ur(c);for(let oe=0;oe<Ke;oe++){let we=oe/Ke,ge=de-we*Fe,ce=Ce.map((G,ue)=>(G-we*me[ue])/ge);for(let G=0;G<c;G++)ze[G]+=ce[G]/Ke;let tt=xe/Ke;for(let G=0;G<c;G++)for(let ue=0;ue<c;ue++)et[G][ue]+=tt*((be[G][ue]-we*ke[G][ue])/ge-ce[G]*ce[ue])}let Ue=Pe.map((oe,we)=>oe-xe*ze[we]),Xe=s.sortedToInput[te[0]],pe=b[Xe];for(let oe=0;oe<c;oe++){y[oe]+=pe*Ue[oe];for(let we=0;we<c;we++)v[oe][we]+=et[oe][we],E[oe][we]+=pe*et[oe][we],g[oe][we]+=pe*pe*et[oe][we]}s.start!==void 0&&te.reverse();for(let oe of te){let we=s.sortedToInput[oe],ge=i.map(ce=>s.sortedX[oe][ce]);for(let ce=0;ce<c;ce++)ge[ce]-=ze[ce];x.push({input:we,time:U,stratum:s.stratumIndex[oe],g:m[we],residual:ge})}}j=W+1}D=Y+1}let M;try{M=nt(v,E)}catch{throw new Error("the proportional-hazards information is singular")}let R=ur(c);for(let D=0;D<c;D++)for(let Y=0;Y<c;Y++){let j=0;for(let U=0;U<c;U++)j+=E[U][D]*M[U][Y];R[D][Y]=g[D][Y]-j}for(let D=0;D<c;D++)for(let Y=0;Y<D;Y++){let j=(R[D][Y]+R[Y][D])/2;R[D][Y]=j,R[Y][D]=j}let T=t.terms===!1||t.terms===void 0?i.map(D=>[D]):t.terms,S=new Set,A=[],C=[];if(T.forEach((D,Y)=>{if(!Array.isArray(D)||D.length===0)throw new RangeError("each terms group must be a non-empty array");let j=[];for(let U of D){if(!Number.isInteger(U)||U<0||U>=e.coefficients.length)throw new RangeError("terms column indices must be zero-based and within x");if(S.has(U))throw new RangeError("terms groups must not overlap");S.add(U),i.includes(U)&&j.push(U)}j.length>0&&(A.push(j),C.push(Y))}),i.forEach(D=>{S.has(D)||(A.push([D]),C.push(T.length))}),A.length===0)throw new Error("the proportional-hazards test has no estimable terms");let L=[];A.forEach((D,Y)=>{if(o&&D.length>1)L.push({col:-1,group:Y,collapsed:!0});else for(let j of D)L.push({col:j,group:Y,collapsed:!1})});let k=L.length,F=Array.from({length:c},()=>Un(k)),I=[],O=[];L.forEach((D,Y)=>{let j=A[D.group],U=C[D.group];if(D.collapsed){for(let W of j){let te=i.indexOf(W);F[te][Y]=N[te]}I.push(1),O.push(`g${U+1}`)}else{let W=i.indexOf(D.col);F[W][Y]=s.inverseCovariateScale[D.col],I.push(e.coefficients[D.col]),O.push(j.length>1?`g${U+1}x${D.col+1}`:`x${D.col+1}`)}});let q=Array.from({length:k},(D,Y)=>{let j=0;for(let U=0;U<c;U++)j=Math.max(j,Math.abs(F[U][Y]));if(!(j>0&&Number.isFinite(j)))throw new Error("the proportional-hazards residual variance is singular");return Math.floor(Math.log2(j))}),$=F.map(D=>D.map((Y,j)=>V(Y,-q[j]))),_=Array.from({length:c},()=>Un(k));for(let D=0;D<c;D++)for(let Y=0;Y<k;Y++)for(let j=0;j<c;j++)_[D][Y]+=v[D][j]*$[j][Y];let P=ur(k);for(let D=0;D<k;D++)for(let Y=0;Y<k;Y++)for(let j=0;j<c;j++)P[D][Y]+=$[j][D]*_[j][Y];for(let D=0;D<k;D++)for(let Y=0;Y<D;Y++){let j=(P[D][Y]+P[Y][D])/2;P[D][Y]=j,P[Y][D]=j}let K;try{K=nt(P)}catch{throw new Error("the proportional-hazards residual variance is singular")}for(let D=0;D<k;D++)for(let Y=0;Y<D;Y++){let j=(K[D][Y]+K[Y][D])/2;K[D][Y]=j,K[Y][D]=j}let X=K.map((D,Y)=>D.map((j,U)=>a.length*Ve(j,-q[Y]-q[U]-s.weightExp))),re=x.map(D=>{let Y=Un(k);for(let j=0;j<k;j++)for(let U=0;U<c;U++)Y[j]+=$[U][j]*D.residual[U];return I.map((j,U)=>{let W=0;for(let te=0;te<k;te++)W+=Y[te]*K[te][U];return j+a.length*Ve(W,-q[U]-s.weightExp)})}),z=[],J=[],Z=[],Ae=L.map(D=>D.collapsed?A[D.group].map(Y=>i.indexOf(Y)):[i.indexOf(D.col)]);for(let D of Ae){let Y=D.map(te=>D.map(fe=>R[te][fe])),j=D.map(te=>y[te]),U;try{U=nt(Y,j)}catch{throw new Error("the proportional-hazards term information is singular")}let W=V(Math.max(0,j.reduce((te,fe,Re)=>te+fe*U[Re],0)),s.weightExp);z.push(W),J.push(D.length),Z.push(It(W,D.length,{lowerTail:!1}))}if(r){let D;try{D=nt(R,y)}catch{throw new Error("the global proportional-hazards information is singular")}let Y=V(Math.max(0,y.reduce((j,U,W)=>j+U*D[W],0)),s.weightExp);z.push(Y),J.push(c),Z.push(It(Y,c,{lowerTail:!1})),O.push("GLOBAL")}let ne={rowNames:O,chisq:z,df:J,p:Z,pValue:Z,x:x.map(D=>D.g),time:x.map(D=>D.time),...s.strata===void 0?{}:{strata:x.map(D=>vt(s.strata[D.input]))},y:re,var:X,transform:n,warnings:f};return Q("coxZph",ne,e)}var X1=["center"],Q1=2**-1022;function Z1(e,t,n={}){B(n,X1,"leveneTest");let r=Se(n.center??"median",["median","mean"]),{groups:o}=nr(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"?xs(l):it(l);return l.map(m=>Math.abs(m-f))},i=qe(o).groups.map(s),c=(l,f)=>l.some(m=>m>0&&m<Q1)||l.every(m=>m===0)&&o[f].some(m=>m!==o[f][0]);if(i.some(c)){let l=o.map(u=>{let h=qe([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=>V(h,u.k-m)))}let a=nu(vr(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 J1=["score"],eb=["alternative","exact","variant","levels"];function tb(e,t,...n){B(ys(n),J1,"propTrendTest");let{score:r}=Ns(["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:w,sxx:d}=Mc(f,m,h,u);if(!(d>=2**-1022&&d<1/0&&Math.abs(w)<1/0)){let N=qe([u.map(v=>c[v])]);N.k!==0&&(h=c.slice(),u.forEach((v,E)=>h[v]=N.groups[0][E]),{sw:p,sxy:w,sxx:d}=Mc(f,m,h,u))}let b=Vt(w,d);if(!(a<1/0)||l>0&&l<1&&!(p<1/0&&Math.abs(w)<1/0&&d<1/0)){let{groups:[N,v],k:E}=qe([o,s]),g=N.reduce((x,M)=>x+M,0)/v.reduce((x,M)=>x+M,0),y=Mc(v,m,h,u);b=V(y.sxy/g*(y.sxy/y.sxx/(1-g)),E)}return{method:"Chi-squared Test for Trend in Proportions",statistic:b,statisticName:"X-squared",parameter:1,parameterName:"df",pValue:wt(b,.5,2,!1,!1),warnings:[]}}function Mc(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 nb=2**24,rb=2**32,ob=`Sample size > 100 or data with ties
10
+ p-value based on normal approximation. Specify nperm for permutation p-value`,sb=`Sample data with ties
11
+ p-value based on normal approximation. Specify nperm for permutation p-value.`;function ab(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=Pt(t));let c;if(n!==void 0)c=n.map(vt);else{let f=[...new Set(i.map(m=>t[m]))];c=f.every(m=>typeof m=="number")?f.sort((m,u)=>m-u).map(vt):[...new Set(f.map(vt))].sort(Tn)}let a=c.map(()=>[]),l=new Map(c.map((f,m)=>[f,m]));for(let f of i){let m=l.get(vt(t[f]));if(m===void 0)throw new RangeError(`the group '${vt(t[f])}' is not among 'levels'`);a[m].push(o[f])}return a}function ib(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 cb(e,t,n={}){B(n,eb,"jonckheereTest");let r=Se(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=ab(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 E=v+1;E<s.length;E++)for(let g of N)for(let y of s[E])l+=g<y?1:g===y?.5:0});let u=[],h=!a&&c<=100;o==="DescTools"&&n.exact!==void 0?(h=n.exact,h&&a&&(h=!1,u.push(sb))):h||u.push(ob);let p,w;if(h){let N=1,v=0;for(let[g,y]of f)g===0||y===0||(v+=N*(g*y+1),N+=g*y);if(N>nb||v>rb)throw new RangeError(`the exact distribution of JT has ${N} values and needs ${v} steps, too many: use exact: false`);let E=[1];for(let[g,y]of f){if(g===0||y===0)continue;let x=Array.from({length:g*y+1},(R,T)=>gl(T,g,y,!1)),M=new Array(E.length+x.length-1).fill(0);E.forEach((R,T)=>x.forEach((S,A)=>M[T+A]+=R*S)),E=M}p=0,w=0,E.forEach((g,y)=>{y<=l&&(p+=g),y>=l&&(w+=g)})}else{let N=f.reduce((g,[y,x])=>g+y*x,0)/2,v=a?ib(s.map(g=>g.length),i):f.reduce((g,[y,x])=>g+y*x*(y+x+1)/12,0),E=(l-N)/Math.sqrt(v);p=se(E),w=1-p,w<1e-8&&(w=se(E,{lowerTail:!1}))}let b=r==="increasing"?w:r==="decreasing"?p:Math.min(1,2*Math.min(w,p,o==="clinfun"?.5:1));return Q("jonckheereTest",{method:"Jonckheere-Terpstra test",alternative:r,statistic:l,statisticName:"JT",pValue:b,warnings:u})}var lb=["nClasses","adjust"];function Yo(e){return Dt(Array.from(e)).sort((t,n)=>t-n)}function Bu(e){if(e.length>0&&!(Number.isFinite(e[0])&&Number.isFinite(e[e.length-1])))throw new RangeError("'x' must be finite")}function fa(e){Bu(e);let t=cs(e);if(t.some(Number.isNaN))throw new RangeError("all 'x' values are identical");return t}function fb(e){let t=Yo(e),n=t.length;if(n<8)throw new RangeError("sample size must be greater than 7");let r=fa(t),o=0;for(let a=1;a<=n;a++)o+=(2*a-1)*(se(r[a-1],{logP:!0})+se(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 mb(e){let t=Yo(e),n=t.length;if(n<8)throw new RangeError("sample size must be greater than 7");let r=fa(t),o=1/(12*n);r.forEach((a,l)=>{o+=(se(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")),Q("cvmTest",{method:"Cramer-von Mises normality test",statistic:o,statisticName:"W",pValue:c,warnings:i})}function ub(e){let t=Yo(e),n=t.length;if(n<5)throw new RangeError("sample size must be greater than 4");let r=fa(t).map(f=>se(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 hb(e){let t=Yo(e),n=t.length;if(n<5||n>5e3)throw new RangeError("sample size must be between 5 and 5000");if(Bu(t),t[0]===t[n-1])throw new RangeError("all 'x' values are identical");let r=t.map((m,u)=>le((u+1-3/8)/(n+1/4))),o=qe([t]).groups[0],s=Math.min(ml(o,r)**2/(at(o)*at(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=se((Math.log(1-s)-a)/l,{lowerTail:!1});return{method:"Shapiro-Francia normality test",statistic:s,statisticName:"W",pValue:f,warnings:[]}}function pb(e,t={}){B(t,lb,"pearsonTest");let n=Yo(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 fa(n)){let m=Math.min(o,Math.floor(1+o*se(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:wt(a,l/2,2,!1,!1),warnings:[]}}var db=["weight"],gb=["exact"],wb=["model","type","unit","r0","confLevel"];function Go(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 ma(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 Uu=e=>2*se(-Math.abs(e));function bb(e,t={}){B(t,db,"kappa2");let n=Se(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=>$n(x[0])),i=r.map(x=>$n(x[1])),c=[...new Set([...s,...i])].sort((x,M)=>x<M?-1:x>M?1:0),a=c.length,l=new Map(c.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(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,M)=>Array.from({length:a},(R,T)=>m(Math.abs(M-T)))),h=f.map(x=>De(x)/o),p=Array.from({length:a},(x,M)=>De(f.map(R=>R[M]))/o),w=x=>{let M=[];for(let R=0;R<a;R++)for(let T=0;T<a;T++)M.push(x(R,T));return De(M)},d=w((x,M)=>f[x][M]*u[x][M])/o,b=w((x,M)=>h[x]*p[M]*u[x][M]),N=(d-b)/(1-b),v=Array.from({length:a},(x,M)=>De(p.map((R,T)=>R*u[M][T]))),E=Array.from({length:a},(x,M)=>De(h.map((R,T)=>R*u[T][M]))),g=(w((x,M)=>h[x]*p[M]*(u[x][M]-(v[x]+E[M]))**2)-b*b)/(o*(1-b)**2),y=N/Math.sqrt(g);return{value:N,statistic:y,pValue:Uu(y),warnings:[]}}function vb(e,t={}){B(t,gb,"kappamFleiss");let n=t.exact??!1,r=Go(e,"ratings");ma(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=Pt(i),a=[...new Set(c)].sort((g,y)=>g-y),l=new Map(a.map((g,y)=>[g,y])),f=new Map(i.map((g,y)=>[g,l.get(c[y])])),m=a.length,u=e.map(g=>{let y=new Array(m).fill(0);for(let x of g)y[f.get(x)]++;return y}),h=De(u.map(g=>(De(g.map(y=>y*y))-s)/(s*(s-1))))/o,p=Array.from({length:m},(g,y)=>De(u.map(x=>x[y]))/(o*s)),w=De(p.map(g=>g*g));if(n){let g=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)}),y=Array.from({length:m},(x,M)=>at(g.map(R=>R[M]))*(s-1)/s);w-=De(y)/(s-1)}let d=(h-w)/(1-w);if(n)return{value:d,warnings:[]};let b=De(p.map(g=>g*(1-g))),N=De(p.map(g=>g*(1-g)*(1-g-g))),v=2/(b*b*o*s*(s-1))*(b*b-N),E=d/Math.sqrt(v);return{value:d,statistic:E,pValue:Uu(E),warnings:[]}}function Nb(e,t={}){B(t,wb,"icc");let n=Se(t.model,["oneway","twoway"]),r=Se(t.type,["consistency","agreement"]),o=Se(t.unit,["single","average"]),s=t.r0??0,i=t.confLevel??.95;_t(i),Go(e,"ratings"),ma(e,"ratings");let c=qe(e).groups,a=Go(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=at(c.map($=>it($)))*f,h=at(a.map($=>it($)))*l,p=it(c.map($=>at($))),w=at(c.flat())*(l*f-1),d=w-u*(l-1)-h*(f-1),b=d>2**-20*w?d/((l-1)*(f-1)):xb(c)/((l-1)*(f-1)),N=($,_,P)=>ht($,_,P,!1,!1),v=($,_)=>m/2<1e-8?un(m/2,$,_,!1,!1):un(1-m/2,$,_,!0,!1),E=l-1;if(n==="oneway"||r==="consistency"){let $=n==="oneway"?p:b,_=n==="oneway"?l*(f-1):(l-1)*(f-1),P=u/$,K=P/v(E,_),X=P*v(_,E),[re,z,J]=o==="single"?[(u-$)/(u+(f-1)*$),P*(1-s)/(1+(f-1)*s),Z=>(Z-1)/(Z+f-1)]:[(u-$)/u,P*(1-s),Z=>1-1/Z];return{value:re,Fvalue:z,fValue:z,df1:E,df2:_,pValue:N(z,E,_),lbound:J(K),ubound:J(X),warnings:[]}}let g=o==="single"?f:1,y=$=>[g*$/(l*(1-$)),1+g*$*(l-1)/(l*(1-$))],x=($,_)=>ul([$*h,_*b],[f-1,(l-1)*(f-1)]),M=o==="single"?(u-b)/(u+(f-1)*b+f/l*(h-b)):(u-b)/(u+(h-b)/l),[R,T]=y(s),S=u/(R*h+T*b),A=x(R,T),[C,L]=[f*M/(l*(1-M)),1+f*M*(l-1)/(l*(1-M))],k=x(C,L),F=v(E,k),I=v(k,E),[O,q]=o==="single"?[l*(u-F*b)/(F*(f*h+(f*l-f-l)*b)+l*u),l*(I*u-b)/(f*h+(f*l-f-l)*b+l*I*u)]:[l*(u-F*b)/(F*(h-b)+l*u),l*(I*u-b)/(h-b+l*I*u)];return{value:M,Fvalue:S,fValue:S,df1:E,df2:A,pValue:N(S,E,A),lbound:O,ubound:q,warnings:[]}}function Ku(e,t){let n=t.length,r=De(t.flatMap(s=>t.map(i=>e[s][i])));return(1-De(t.map(s=>e[s][s]))/r)*n/(n-1)}function yb(e){let t=Go(e,"x").length;if(ma(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=Ts(qe(e).groups),r=Ro(e),o=Array.from({length:t},(c,a)=>a),s=De(r.flat()),i;try{let c=nt(r);i=o.map(a=>1-1/c[a][a])}catch(c){if(!r.flat().every(Number.isFinite))throw c;let a=qe(e).groups;i=o.map(l=>{let f=a.map(h=>h[l]),m=jr(Lt(a.map(h=>[1,...h.filter((p,w)=>w!==l)])),f),u=it(f);return 1-De(m.map(h=>h*h))/De(f.map(h=>(h-u)**2))})}return Q("cronbachAlpha",{rawAlpha:Ku(n,o),stdAlpha:(1-t/s)*t/(t-1),G6:1-(t-De(i))/s,averageR:(s-t)/(t*(t-1)),alphaDrop:o.map(c=>Ku(n,o.filter(a=>a!==c))),warnings:Mb(e,r)})}function Mb(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}=Bt(r.map(c=>r.map(a=>t[c][a])),{symmetric:!0}),s=o.map(c=>c[0]),i=De(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 db(e,t,n){let r=Yo(e,"x").length;la(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=xo(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=nt(a,l);for(let m=0;m<2;m++)for(let u=0;u<2;u++)c[m][u]-=De(i.map((h,p)=>o[s[m]][h]*f[p][u]))}return c[0][1]/Math.sqrt(c[0][0]*c[1][1])}function gb(e){let t=e.length,n=e[0].length,r=a=>a.reduce((l,f)=>he(l,[f,0]),[0,0]),o=e.map(a=>lt(r(a),n)),s=e[0].map((a,l)=>lt(r(e.map(f=>f[l])),t)),i=lt(r(e.flat()),t*n),c=[0,0];return e.forEach((a,l)=>a.forEach((f,m)=>{let u=he(Oe(Oe([f,0],o[l]),s[m]),i);c=he(c,_e(u,u))})),c[0]+c[1]}var wb=["controls","cases"],Wu=["levels","direction","naRm"],bb=["partialAuc","focus","correct"],vb=["bestMethod","bestWeights"],Gu=new WeakMap;function ma(e){return Gu.get(e)}var Nb=["auto","<",">"];function vc(e){if(e==null)return null;if(typeof e=="number")return Number.isNaN(e)?null:Vn(e);if(typeof e=="boolean")return e?"TRUE":"FALSE";if(typeof e=="string")return e;throw new TypeError("the response must hold numbers, strings or booleans")}function yb(e){let t=e.filter(s=>vc(s)!==null),n=new Set(t.map(s=>typeof s));if(n.size>1)throw new TypeError("the response mixes values of different types");let r=[...n][0],o;return r==="number"?o=[...new Set(t)].sort((s,i)=>s-i).map(Vn):r==="boolean"?o=[...new Set(t)].sort((s,i)=>Number(s)-Number(i)).map(s=>s?"TRUE":"FALSE"):o=[...new Set(t)].sort((s,i)=>s<i?-1:s>i?1:0),[...new Set(o)]}function Xo(e){return Float64Array.from(e).sort()}function Nc(e,t){let n=Xo(e),r=Xo(t),o=[],s=[],i=[],c=0,a=0;for(;c<n.length||a<r.length;){let l=a>=r.length||c<n.length&&n[c]<=r[a]?n[c]:r[a];for(;c<n.length&&n[c]===l;)c++;for(;a<r.length&&r[a]===l;)a++;o.push(l),s.push(c),i.push(a)}return{u:o,ctrlLE:s,caseLE:i}}function yc(e,t,n){let r=0;for(let o=0;o<e.u.length;o++){let s=o>0?e.ctrlLE[o-1]:0,i=e.caseLE[o]-(o>0?e.caseLE[o-1]:0),c=e.ctrlLE[o]-s,a=n==="<"?s:t-e.ctrlLE[o];r+=i*(2*a+c)}return r}function Mc(e,t,n){let r=e.length,o=t.length;return yc(Nc(e,t),r,n)/(2*o*r)}function Ec(e,t,n){return yc(Nc(e,t),e.length,n)}var Bu=e=>e===0n?0:e.toString(2).length;function Zr(e,t){if(t<0n&&(e=-e,t=-t),e===0n)return 0;let n=e<0n,r=n?-e:e,o=55-(Bu(r)-Bu(t)),s=o>=0?r<<BigInt(o):r,i=o>=0?t:t<<BigInt(-o),c=s/i;c*i!==s&&(c|=1n);let a=Ve(Number(c),-o);return n?-a:a}function Mb(e,t,n){let r=(e+t)/2;return Math.abs(r)<=1e100||(r=e/2+t/2),(r===e||r===t)&&(r=n==="<"?t:e),r}function Eb(e,t,n){let r=Nc(e,t),o=e.length,s=t.length,i=r.u.length,c=[],a=[],l=[],f=m=>{let u=m===0?-1/0:m===i?1/0:Mb(r.u[m-1],r.u[m],n),h=m===0?0:r.caseLE[m-1],p=m===0?0:r.ctrlLE[m-1];c.push(u),a.push((n==="<"?s-h:h)/s),l.push((n==="<"?p:o-p)/o)};if(n==="<")for(let m=0;m<=i;m++)f(m);else for(let m=i;m>=0;m--)f(m);return{thresholds:c,sensitivities:a,specificities:l,auc:yc(r,o,n)/(2*s*o)}}function xb(e,t){let n=r=>{let o=Xo(r),s=o.length>>1;return o.length%2===1?o[s]:en(o[s-1],o[s])};return n(e)<=n(t)?"<":">"}function Xu(e,t,n,r,o,s){if(e.length===0)throw new Error("No control observation.");if(t.length===0)throw new Error("No case observation.");if(e.some(l=>!Number.isFinite(l))||t.some(l=>!Number.isFinite(l)))throw new RangeError("infinite values in the predictor: cannot build a valid ROC curve");let i=n==="auto"?xb(e,t):n,a={...Eb(e,t,i),direction:i,levels:r,controls:e,cases:t,warnings:s};return Gu.set(a,o),a}function Qu(e){if(e===void 0)return"auto";if(!Nb.includes(e))throw new RangeError("'arg' should be one of \u201Cauto\u201D, \u201C<\u201D, \u201C>\u201D");return e}function Xr(e,t,n){let r=Qu(n.direction),o=new Et,s=e.map(vc),i;if(n.levels===void 0){if(i=yb(e),i.length>2&&(o.add("'response' has more than two levels. Consider setting 'levels' explicitly or using 'multiclass.roc' instead"),i=i.slice(0,2)),i.length<2)throw new RangeError("'response' must have two levels")}else{if(!Array.isArray(n.levels)||n.levels.length!==2)throw new RangeError("'levels' argument must have length 2");i=n.levels.map(p=>{let w=vc(p);if(w===null)throw new RangeError("'levels' must not be missing");return w})}if(t.some(p=>typeof p!="number"))throw new TypeError("Predictor must be numeric.");if(s.length!==t.length)throw new TypeError("Response and predictor must be vectors of the same length.");let c=s.map((p,w)=>p===null||Number.isNaN(t[w]));if(n.naRm===!1&&c.some(p=>p))throw new RangeError("missing values in the response or the predictor with naRm false");let a=[],l=[],f=[],m=[],u=[];s.forEach((p,w)=>{if(c[w])return;let d=t[w];if(p===i[0])a.push(d);else if(p===i[1])l.push(d);else return;f.push(d),m.push(p===i[1]),u.push(w)});let h={labels:s,predictor:[...t],values:f,isCase:m,kept:u};return Xu(a,l,r,[i[0],i[1]],h,o.list)}function Rb(e,t){let n=ds(Jn(e),Object.keys(t));if(n.response!==0||n.variables.length!==2||n.terms.length!==1)throw new TypeError("the formula must be of the form response ~ predictor");let r=El(t,n.variables),[o,s]=r.variables;if(s.kind!=="numeric")throw new TypeError("Predictor must be numeric.");return{response:o.kind==="numeric"?o.values:o.codes.map(c=>o.levels[c]),predictor:s.values}}function Tb(e){return typeof e=="object"&&e!==null&&!Array.isArray(e)&&!ArrayBuffer.isView(e)}function Ab(e,t,n={}){if(wt(e)&&B(e,wb,"roc"),typeof e!="string"&&wt(t)&&B(t,Wu,"roc"),B(n,Wu,"roc"),typeof e=="string"){let r=Rb(e,t);return Q("roc",Xr(r.response,r.predictor,n))}if(Tb(e)){let r=t??{};if(r.levels!==void 0)throw new RangeError("'levels' cannot be given with controls and cases");let o=e;if(o.controls===void 0||o.cases===void 0)throw new TypeError("'controls' and 'cases' must both be given");let s=Qu(r.direction),i=Array.from(o.controls),c=Array.from(o.cases);if([...i,...c].some(u=>typeof u!="number"))throw new TypeError("Predictor must be numeric.");if(r.naRm===!1&&[...i,...c].some(Number.isNaN))throw new RangeError("missing values in the controls or the cases with naRm false");let a=i.filter(u=>!Number.isNaN(u)),l=c.filter(u=>!Number.isNaN(u)),f=[...a,...l],m={labels:null,predictor:f,values:f,isCase:f.map((u,h)=>h>=a.length),kept:f.map((u,h)=>h)};return Q("roc",Xu(a,l,s,["0","1"],m,[]))}return Q("roc",Xr(Array.from(e),Array.from(t),n))}var fa=(e,t,n,r,o)=>t[r]+(n-e[r])/(e[o]-e[r])*(t[o]-t[r]);function Sb(e,t,n,r){let o=[];if(e.forEach((a,l)=>{a>=n&&a<=r&&o.push(l)}),o.length===0){let a=e.findIndex(u=>u>=r),l=a-1,f=fa(e,t,r,a,l),m=fa(e,t,n,a,l);return(f+m)/2*(r-n)}let s=0;for(let a=1;a<o.length;a++){let l=o[a-1],f=o[a];s+=(e[f]-e[l])*(t[l]+t[f])/2}let i=o[o.length-1];if(e[i]!==r){let a=e.findIndex(m=>m>=r),l=a-1,f=fa(e,t,r,a,l);s+=(r-e[l])*(t[l]+f)/2}let c=o[0];if(e[c]!==n){let a=e.findIndex(m=>m>n)-1,l=a+1,f=fa(e,t,n,l,a);s+=(e[l]-n)*(t[l]+f)/2}return s}var ur=(e,t)=>[e[0]*t[1]+t[0]*e[1],e[1]*t[1]],Ir=(e,t)=>[e[0]*t[1]-t[0]*e[1],e[1]*t[1]],Sn=(e,t)=>[e[0]*t[0],e[1]*t[1]],Ku=(e,t)=>t[0]<0n?[-e[0]*t[1],-e[1]*t[0]]:[e[0]*t[1],e[1]*t[0]],Go=(e,t)=>{let n=e[0]*t[1]-t[0]*e[1];return n<0n?-1:n>0n?1:0};function Uu(e,t){if(!(t>=1))return;let n=e.map(r=>Math.round(r*t));return n.every((r,o)=>r/t===e[o])?n:void 0}function Hu(e,t,n,r,o,s){let i=Uu(e,n),c=Uu(t,r);if(i!==void 0&&c!==void 0)return Yu(i.map(BigInt),c.map(BigInt),BigInt(n),BigInt(r),o,s);if(!(e.length>=2&&e.length===t.length&&e[0]===0&&e[e.length-1]===1&&e.every((h,p)=>h>=0&&h<=1&&(p===0||h>=e[p-1]))&&t.every(h=>h>=0&&h<=1)))throw new RangeError("the curve's sensitivities and specificities must be numbers in [0, 1] forming a ROC curve (use a curve from roc)");let l=e.map(Qr),f=t.map(Qr),m=0n;for(let[,h]of[...l,...f])m=h>m?h:m;let u=h=>h[0]*(m/h[1]);return Yu(l.map(u),f.map(u),m,m,o,s)}function Yu(e,t,n,r,o,s){let i=n*r,c=d=>[t[d]*n+e[d]*r-i,i],a=d=>[e[d],n],l=Qr(o),f=Qr(s),m=(d,b,y)=>ur(c(b),Sn(Ku(Ir(d,a(b)),Ir(a(y),a(b))),Ir(c(y),c(b)))),u=[];e.forEach((d,b)=>{Go(a(b),l)>=0&&Go(a(b),f)<=0&&u.push(b)});let h;if(u.length===0){let d=e.findIndex((y,v)=>Go(a(v),f)>=0),b=d-1;h=Sn(ur(m(f,b,d),m(l,b,d)),Sn(Ir(f,l),[1n,2n]))}else{let d=0n;for(let v=1;v<u.length;v++){let M=u[v-1],g=u[v];d+=(e[g]-e[M])*(c(M)[0]+c(g)[0])}h=[d,2n*n*i];let b=u[u.length-1];if(Go(a(b),f)!==0){let v=b+1;h=ur(h,Sn(Ir(f,a(b)),Sn(ur(c(b),m(f,b,v)),[1n,2n])))}let y=u[0];if(Go(a(y),l)!==0){let v=y-1;h=ur(h,Sn(Ir(a(y),l),Sn(ur(c(y),m(l,y,v)),[1n,2n])))}}if(h[0]<0n)throw new Error("Partial AUC correction not defined for ROC curves below the diagonal.");let p=Sn(Sn(Ir(f,l),ur(f,l)),[1n,2n]),w=Ku(ur(p,h),Sn(p,[2n,1n]));return Zr(w[0],w[1])}function qb(e,t={}){B(t,bb,"auc");let n=Ae(t.focus,["specificity","sensitivity"]),r=t.correct??!1;if(t.partialAuc===void 0)return Q("auc",{auc:e.auc,partialAuc:null,focus:n,correct:r,warnings:[]});let o=t.partialAuc;if(!Array.isArray(o)||o.length!==2||o.some(p=>typeof p!="number"))throw new TypeError("partialAuc must be a numeric vector of length 2");let s=Math.max(o[0],o[1]),i=Math.min(o[0],o[1]);if(!(i>=0&&s<=1))throw new RangeError("the limits of partialAuc must be within [0, 1]");if(!(i<s))throw new RangeError("the limits of partialAuc must differ");let c=e.sensitivities,a=e.specificities,[l,f]=n==="specificity"?[a,c]:[[...c].reverse(),[...a].reverse()],m,u=e.controls.length,h=e.cases.length;return r?m=n==="specificity"?Hu(l,f,u,h,i,s):Hu(l,f,h,u,i,s):m=Sb(l,f,i,s),Q("auc",{auc:m,partialAuc:[s,i],focus:n,correct:r,warnings:[]})}function Qr(e){if(e===0)return[0n,1n];let t=0,n=e;for(;!Number.isInteger(n);)n*=2,t++;return[BigInt(n),1n<<BigInt(t)]}function kb(e,t){let n=Xo(e.controls),r=Xo(e.cases),o=(i,c,a)=>{let l=0,f=i.length;for(;l<f;){let m=l+f>>1;(a?i[m]<=c:i[m]<c)?l=m+1:f=m}return l},s=e.direction==="<";return{tp:t.map(i=>s?r.length-o(r,i,!1):o(r,i,!0)),tn:t.map(i=>s?o(n,i,!1):n.length-o(n,i,!0))}}function Ib(e,t="all",n={}){B(n,vb,"coords");let r=Ae(n.bestMethod,["youden","closest.topleft","topleft"]),o=n.bestWeights??[1,.5];if(!Array.isArray(o)||o.length!==2||o.some(d=>typeof d!="number"))throw new TypeError("'best.weights' must be a numeric vector of length 2");let[s,i]=o;if(!(i>0&&i<1))throw new RangeError("prevalence ('best.weights[2]') must be in the interval ]0,1[.");if(!(s>0&&s<1/0))throw new RangeError("the cost ('bestWeights[0]') must be a positive finite number");let c=(1-i)/(s*i),a,l=null;if(typeof t=="string"&&(l=Ae(t,["all","best"])),l!==null)a=[...e.thresholds];else{if(a=typeof t=="number"?[t]:Array.from(t),a.length===0)throw new RangeError("Numeric 'x' has length 0");if(a.some(d=>typeof d!="number"||Number.isNaN(d)))throw new RangeError("the thresholds must be numbers (not NaN)")}let f=e.cases.length,m=e.controls.length,{tp:u,tn:h}=kb(e,a);if(l==="best"){let[d,b]=Qr(s),[y,v]=Qr(i),M=(v-y)*b,g=d*y,N=BigInt(f),x=BigInt(m),E=u.map((A,S)=>{if(r==="youden")return BigInt(A)*x*g+M*BigInt(h[S])*N;let C=BigInt(f-A),L=BigInt(m-h[S]);return-(C*C*x*x*g+M*L*L*N*N)}),R=E.reduce((A,S)=>S>A?S:A),T=E.map((A,S)=>A===R?S:-1).filter(A=>A>=0);a=T.map(A=>a[A]),u=T.map(A=>u[A]),h=T.map(A=>h[A])}let p=u.map(d=>f-d),w=h.map(d=>m-d);return Q("coords",{threshold:a,sensitivity:u.map(d=>d/f),specificity:h.map(d=>d/m),accuracy:u.map((d,b)=>(d+h[b])/(f+m)),tn:h,tp:u,fn:p,fp:w,npv:h.map((d,b)=>d/(d+p[b])),ppv:u.map((d,b)=>d/(d+w[b])),fpr:w.map(d=>d/m),lrPos:u.map((d,b)=>d*m/(f*w[b])),lrNeg:p.map((d,b)=>d*m/(f*h[b])),youden:u.map((d,b)=>d/f+c*(h[b]/m)),closestTopleft:p.map((d,b)=>(d/f)**2+c*(w[b]/m)**2),warnings:[]})}var _b=["confLevel","method","bootN","bootStratified"],Zu=["method","alternative","paired","confLevel","bootN","bootStratified","levels","direction"];function pa(e){let t=Float64Array.from(e.controls).sort(),n=Float64Array.from(e.cases).sort(),r=t.length,o=n.length,s=(l,f)=>{let m=0,u=l.length;for(;m<u;){let p=m+u>>1;l[p]<f?m=p+1:u=p}let h=m;for(;h<l.length&&l[h]===f;)h++;return[m,h-m]},i=e.direction==="<",c=e.cases.map(l=>{let[f,m]=s(t,l);return 2*(i?f:r-f-m)+m}),a=e.controls.map(l=>{let[f,m]=s(n,l);return 2*(i?o-f-m:f)+m});return{k10:c,k01:a}}function Ac(e,t,n,r){return st(e)/(4*n*n)/r+st(t)/(4*r*r)/n}function Ju(e){let{k10:t,k01:n}=pa(e);return Ac(t,n,e.controls.length,e.cases.length)}function ua(e,t,n,r,o,s){let i=BigInt(Ec(e,t,n)),c=BigInt(Ec(r,o,s)),a=BigInt(e.length)*BigInt(t.length),l=BigInt(r.length)*BigInt(o.length);return Zr(i*l-c*a,2n*a*l)}var Rc=e=>e.sensitivities.some((t,n)=>t===1&&e.specificities[n]===1);function eo(e,t){if(e.controls.length<2||e.cases.length<2)throw new Error(`${t} needs at least 2 controls and 2 cases`)}function th(e){let t=(1-e)/2;return t<1e-8?le(t,{lowerTail:!1}):le(1-t)}function nh(e){if(!(Number.isInteger(e)&&e>=1&&e<=2**24))throw new RangeError("bootN must be an integer from 1 to 2^24")}function to(e){let t=new Array(e.length);for(let n=0;n<e.length;n++)t[n]=e[bo(e.length)];return t}function Tc(e){let t=ma(e);if(t!==void 0)return{values:t.values,isCase:t.isCase};let n=[...e.controls,...e.cases];return{values:n,isCase:n.map((r,o)=>o>=e.controls.length)}}function Qo(e,t,n){let r=[],o=[];for(let s of n)(t[s]?o:r).push(e[s]);if(!(r.length===0||o.length===0))return{controls:r,cases:o}}function Lb(e,t,n,r){let o=Qo(e,t,n);return o===void 0?NaN:Mc(o.controls,o.cases,r)}function Jr(e){let t=new Array(e);for(let n=0;n<e;n++)t[n]=bo(e);return t}function Cb(e,t={}){B(t,_b,"ciAuc");let n=t.confLevel??.95;cn(n);let r=Ae(t.method,["delong","bootstrap"]),o=new Et;Rc(e)&&o.add("ci.auc() of a ROC curve with AUC == 1 is always 1-1 and can be misleading.");let s=1-n;if(r==="delong"){eo(e,"DeLong's interval");let{k10:m,k01:u}=pa(e),h=e.auc,p=Ac(m,u,e.controls.length,e.cases.length),w=Math.sqrt(p),d=v=>Math.min(1,Math.max(0,v)),b=s/2,y=n===1?[0,1]:w===0?[h,h]:[d(h+le(b)*w),d(h+th(n)*w)];return Q("ciAuc",{estimate:h,confInt:y,confLevel:n,method:r,variance:p,warnings:o.list})}let i=t.bootN??2e3;nh(i);let c=t.bootStratified??!0,a=[],l=0;if(c){eo(e,"a stratified bootstrap");for(let m=0;m<i;m++){let u=to(e.controls),h=to(e.cases);a.push(Mc(u,h,e.direction))}}else{let{values:m,isCase:u}=Tc(e);for(let h=0;h<i;h++){let p=Lb(m,u,Jr(m.length),e.direction);Number.isNaN(p)?l++:a.push(p)}}if(l>0&&o.add("NA value(s) produced during bootstrap were ignored."),a.length===0)throw new Error("no bootstrap replicate had both controls and cases");let f=Es(a,[s/2,1-s/2]);return Q("ciAuc",{estimate:e.auc,confInt:[f[0],f[1]],confLevel:n,method:r,variance:a.length>1?st(a):NaN,warnings:o.list})}var eh=e=>typeof e=="object"&&e!==null&&!Array.isArray(e)&&"thresholds"in e&&"sensitivities"in e,ha=(e,t)=>e.length===t.length&&e.every((n,r)=>n===t[r]);function Fb(e,t){let n=ma(e),r=ma(t);if(n?.labels==null||r?.labels==null||!ha(e.levels,t.levels))return;let o=c=>c.kept.map(a=>c.labels[a]);if(ha(o(n),o(r))&&ha(n.isCase,r.isCase))return[e,t];if(!ha(n.labels,r.labels))return;let s=n.labels.map((c,a)=>a).filter(c=>n.labels[c]!==null&&!Number.isNaN(n.predictor[c])&&!Number.isNaN(r.predictor[c])),i=(c,a)=>Xr(s.map(l=>a.labels[l]),s.map(l=>a.predictor[l]),{levels:c.levels,direction:c.direction});return[i(e,n),i(t,r)]}function xc(e,t){return t==="two.sided"?2*ae(-Math.abs(e)):t==="greater"?ae(e,{lowerTail:!1}):ae(e)}function Ob(e,t,n,r){wt(n)&&B(n,Zu,"rocTest"),B(r,Zu,"rocTest");let o,s=new Et,i,c;if(eh(e)){if(!eh(t))throw new TypeError("roc2 must be a curve from roc()");if(o=n??{},o.levels!==void 0||o.direction!==void 0)throw new RangeError("'levels' and 'direction' apply only to a response and two predictors");i=e,c=t}else{o=r??{};let g=Array.from(e),N=Array.from(t),x=Array.from(n);if(g.length!==N.length||g.length!==x.length)throw new TypeError("the response and the predictors must have the same length");let E=g.map((A,S)=>S).filter(A=>{let S=g[A];return S!=null&&!(typeof S=="number"&&Number.isNaN(S))&&!Number.isNaN(N[A])&&!Number.isNaN(x[A])}),R=E.map(A=>g[A]),T={levels:o.levels,direction:o.direction};i=Xr(R,E.map(A=>N[A]),T),c=Xr(R,E.map(A=>x[A]),T);for(let A of i.warnings)s.add(A)}let a=Ze(o.alternative),l=Ae(o.method,["delong","bootstrap"]),f=o.confLevel??.95;cn(f),Rc(i)&&Rc(c)&&s.add("roc.test() of two ROC curves with AUC == 1 has always p.value = 1 and can be misleading.");let m=Fb(i,c),u;if(o.paired===!0){if(m===void 0)throw new RangeError("The paired ROC test cannot be applied to unpaired curves.");u=!0}else o.paired===!1?(m!==void 0&&s.add("The ROC curves seem to be paired. Consider performing a paired roc.test."),u=!1):u=m!==void 0;u&&([i,c]=m);let h={alternative:a,estimate:[i.auc,c.auc],estimateNames:["AUC of roc1","AUC of roc2"],nullValue:0,nullValueName:"difference in AUC",roc1:i,roc2:c},p=ua(i.controls,i.cases,i.direction,c.controls,c.cases,c.direction);if(l==="delong"){if(i.direction!==c.direction&&s.add("DeLong's test should not be applied to ROC curves with a different direction."),eo(i,"DeLong's test"),eo(c,"DeLong's test"),u){let C=pa(i),L=pa(c),k=C.k10.map(($,_)=>$-L.k10[_]),F=C.k01.map(($,_)=>$-L.k01[_]),I=Math.sqrt(Ac(k,F,i.controls.length,i.cases.length)),O=p===0&&I===0?0:p/I,q=I===0?0:th(f)*I;return Q("rocTest",{method:"DeLong's test for two correlated ROC curves",statistic:O,statisticName:"Z",pValue:xc(O,a),confInt:[p-q,p+q],confLevel:f,...h,warnings:s.list})}let g=Ju(i),N=Ju(c),x=i.controls.length+i.cases.length,E=c.controls.length+c.cases.length,R=g+N,T,A,S;return R===0?(T=p===0?0:p>0?1/0:-1/0,A=NaN,S=xc(T,a)):(T=p/Math.sqrt(R),A=R*R/(g*g/(x-1)+N*N/(E-1)),S=a==="two.sided"?2*dt(-Math.abs(T),A,!0,!1):a==="greater"?dt(T,A,!1,!1):dt(T,A,!0,!1)),Q("rocTest",{method:"DeLong's test for two ROC curves",statistic:T,statisticName:"D",parameter:A,parameterName:"df",pValue:S,...h,warnings:s.list})}let w=o.bootN??2e3;nh(w);let d=o.bootStratified??!0,b=[],y=0;if(d){eo(i,"a stratified bootstrap"),eo(c,"a stratified bootstrap");for(let g=0;g<w;g++)if(u){let N=Jr(i.controls.length),x=Jr(i.cases.length);b.push(ua(N.map(E=>i.controls[E]),x.map(E=>i.cases[E]),i.direction,N.map(E=>c.controls[E]),x.map(E=>c.cases[E]),c.direction))}else{let N=to(i.controls),x=to(i.cases),E=to(c.controls),R=to(c.cases);b.push(ua(N,x,i.direction,E,R,c.direction))}}else{let g=Tc(i),N=Tc(c);for(let x=0;x<w;x++){let E,R;if(u){let T=Jr(g.values.length);E=Qo(g.values,g.isCase,T),R=Qo(N.values,N.isCase,T)}else E=Qo(g.values,g.isCase,Jr(g.values.length)),R=Qo(N.values,N.isCase,Jr(N.values.length));E===void 0||R===void 0?y++:b.push(ua(E.controls,E.cases,i.direction,R.controls,R.cases,c.direction))}}if(y>0&&s.add(`${y} NA value(s) produced during bootstrap were ignored.`),b.length<2)throw new Error("fewer than 2 bootstrap replicates had both controls and cases");let v=Math.sqrt(st(b)),M=p===0&&v===0?0:p/v;return Q("rocTest",{method:u?"Bootstrap test for two correlated ROC curves":"Bootstrap test for two ROC curves",statistic:M,statisticName:"D",parameter:w,parameterName:"boot.n",pValue:xc(M,a),bootStratified:d,...h,warnings:s.list})}var Db=["method","confLevel","prevalence"];function Pb(e,t={}){B(t,Db,"diagnosticTests");let n=Ae(t.method,["exact","wilson","jeffreys"]),r=t.confLevel??.95;if(_t(r),!Array.isArray(e)||e.length!==2||e.some(g=>!(Array.isArray(g)||ArrayBuffer.isView(g))||g.length!==2))throw new TypeError("'table' must be a 2 x 2 table (an array of two rows of two counts)");let o=[e[0][0],e[0][1],e[1][0],e[1][1]];if(o.some(g=>typeof g!="number"))throw new TypeError("the counts must be numbers");if(!o.every(Nn))throw new RangeError("the counts must be nonnegative integers");let[s,i,c,a]=o.map(Math.round),l=s+c,f=i+a,m=s+i,u=c+a,h=l+f;if(l===0||f===0||m===0||u===0)throw new RangeError("every row and column total of the table must be positive");let p=(g,N)=>{let x=rt(()=>ui(g,N,{method:n==="exact"?"clopper-pearson":n,confLevel:r}));return{estimate:x.estimate,confInt:x.confInt}},w=1-r,d=w/2<1e-8?le(w/2,{lowerTail:!1}):le(1-w/2),b=(g,N)=>{let x=d*Math.sqrt(N);return{estimate:g,confInt:[Math.exp(Math.log(g)-x),Math.exp(Math.log(g)+x)]}},y=s/l,v=a/f,M={sensitivity:p(s,l),specificity:p(a,f),ppv:p(s,m),npv:p(a,u),lrPos:b(i===0?y/0:Zr(BigInt(s)*BigInt(f),BigInt(l)*BigInt(i)),c/(s*l)+a/(i*f)),lrNeg:b(a===0?c/l/v:Zr(BigInt(c)*BigInt(f),BigInt(l)*BigInt(a)),s/(c*l)+i/(a*f)),accuracy:p(s+a,h),diagnosticOR:b(s*a/(i*c),1/s+1/i+1/c+1/a),prevalence:p(l,h),apparentPrevalence:p(m,h),method:n,confLevel:r,warnings:[]};if(t.prevalence!==void 0){let g=t.prevalence;if(!(typeof g=="number"&&g>0&&g<1))throw new RangeError("'prevalence' must be a number in (0, 1)");let N=g<2**-500?2**600:1,x=y*(g*N),E=i/f*((1-g)*N),R=v*(1-g),T=c/l*g;M.ppvAtPrevalence=x/(x+E),M.npvAtPrevalence=R/(R+T)}return Q("diagnosticTests",M)}var Vb=["two","mode","confLevel","ciMethod"],$b=["package","exact"],qn=(e,t)=>t===0?0:e*t;function zb(e,t,n={}){if(B(n,Vb,"blandAltman"),e.length!==t.length)throw new TypeError("'x' and 'y' must have the same length");let r=n.two??1.96;if(!(typeof r=="number"&&r>0&&r<1/0))throw new RangeError("'two' must be a positive number");let o=n.mode??1;if(o!==1&&o!==2)throw new RangeError("'mode' must be 1 or 2");let s=n.confLevel??.95;cn(s);let i=Ae(n.ciMethod,$b),c=[],a=[];for(let I=0;I<e.length;I++){let O=e[I],q=t[I];if(typeof O!="number"||typeof q!="number")throw new TypeError("'x' and 'y' must be numeric");if(!(Number.isNaN(O)||Number.isNaN(q))){if(!Number.isFinite(O)||!Number.isFinite(q))throw new RangeError("'x' and 'y' must not contain infinite values");c.push(O),a.push(q)}}let l=c.length;if(l<2)throw new RangeError("fewer than 2 complete pairs");let[f,m]=o===1?[c,a]:[a,c],u=c.map((I,O)=>{let q=(I+a[O])/2;return Number.isFinite(q)?q:I/2+a[O]/2}),h=f.map((I,O)=>I-m[O]),p=!h.every(Number.isFinite),{groups:w,k:d}=qe([p?f.map((I,O)=>I/2-m[O]/2):h]),b=w[0],y=d+(p?1:0),v=it(b),M=Qn(b),g=r*M,N=v-g,x=v+g,E=l-1,R=(1-s)/2,T=gt(R,E,!0,!1),A=R<1e-8?gt(R,E,!1,!1):gt((s+1)/2,E,!0,!1),S=[v+qn(T,M)/Math.sqrt(l),v+qn(A,M)/Math.sqrt(l)],C,L;if(i==="package"){let I=3*M*M/l,O=I>=2**-1022||I===0?Math.sqrt(I):M*Math.sqrt(3/l);C=[N+qn(T,O),N+qn(A,O)],L=[x+qn(T,O),x+qn(A,O)]}else{let I=r*Math.sqrt(l),O=Sa(R,E,I,!0,!1)/Math.sqrt(l),q=Sa(R,E,I,!1,!1)/Math.sqrt(l);C=[v-qn(q,M),v-qn(O,M)],L=[v+qn(O,M),v+qn(q,M)]}let k=I=>V(I,y),F=I=>[k(I[0]),k(I[1])];return Q("blandAltman",{means:u,diffs:h,n:l,meanDiff:k(v),lowerLimit:k(N),upperLimit:k(x),criticalDiff:k(g),meanDiffConfInt:F(S),lowerLimitConfInt:F(C),upperLimitConfInt:F(L),confLevel:s,two:r,mode:o,ciMethod:i,warnings:[]})}var jb=new Set(["methodReg","methodCi","methodBootstrapCi","errorRatio","confLevel","slopeMeasure","nsamples","nnested","rngSeed","iterMax","threshold","naRm"]),Wb=["PaBa","Deming","WDeming"],Bb=["analytical","jackknife","bootstrap","nestedbootstrap"],Kb=["quantile","Student","BCa","tBoot"],rh=5793,Ub=2**27;function kc(e,t){let n=e-t;return Math.abs(n)<1e-12*(Math.abs(e)+Math.abs(t))/2?0:n}function Hb(e,t){let n=Xn(e),r=Xn(t),o=[0,0],s=[0,0],i=[0,0];for(let c=0;c<e.length;c++){let a=Oe([e[c],0],n),l=Oe([t[c],0],r);o=he(o,_e(a,a)),s=he(s,_e(l,l)),i=he(i,_e(a,l))}return{u:o,q:s,p:i,mx:n,my:r}}function ih(e,t,n,r){if(n===0)return NaN;if(r<=1){let i=r*t-e,c=Math.sqrt(i*i+4*r*n*n);return i>=0?(i+c)/(2*r*n):2*n/(c-i)}let o=t-e/r,s=Math.sqrt(o*o+4*n*n/r);return o>=0?(o+s)/(2*n):2*n/r/(s-o)}function Ic(e,t,n){let r=e.length,{u:o,q:s,p:i,mx:c,my:a}=Hb(e,t),l=ih(o[0],s[0],i[0],n),f=Oe(a,_e([l,0],c))[0],m=_e(o,s),u=i[0]*i[0]/(o[0]*s[0]),h=Oe(m,_e(i,i))[0]/(m[0]+m[1]);Math.abs(h)<1e-12*(1+u)/2&&(h=0);let p=Math.sqrt(l*l*h/u/(r-2)),w=[0,0];for(let b of e)w=he(w,_e([b,0],[b,0]));let d=Math.sqrt(p*p*lt(w,r)[0]);return{b0:f,b1:l,se0:d,se1:p,xmean:c[0]}}function oh(e,t,n,r,o,s,i,c,a){let l=e.length,f=Ic(e,t,n),m=f.b0,u=f.b1,h=NaN,p=new Array(l),w=s;for(let g=0;g<s;g++){let N=1+n*u*u,x=1/0;for(let q=0;q<l;q++){let $=t[q]-(m+u*e[q]),_=e[q]+n*u*$/N,P=t[q]-$/N;p[q]=r>1?_/r+P:_+r*P,x=Math.min(x,Math.abs(p[q]))}let E=[0,0],R=[0,0],T=[0,0];for(let q=0;q<l;q++){let $=x/p[q],_=[$*$,0];E=he(E,_),R=he(R,_e(_,[e[q],0])),T=he(T,_e(_,[t[q],0]))}let A=lt(R,E[0]+E[1]),S=lt(T,E[0]+E[1]),C=[0,0],L=[0,0],k=[0,0];for(let q=0;q<l;q++){let $=lt(Oe([e[q],0],A),p[q]),_=lt(Oe([t[q],0],S),p[q]);C=he(C,_e($,$)),L=he(L,_e(_,_)),k=he(k,_e($,_))}let F=ih(C[0],L[0],k[0],n),I=Oe(S,_e([F,0],A))[0];h=A[0];let O=V(Math.abs(u-F),a-c)<i&&V(Math.abs(m-I),a)<i;if(m=I,u=F,O){w=g+1;break}}let[d,b]=Sc(o),[y,v]=Sc(1+o),M=p.map(g=>{if(g===0)return 1/0;let[N,x]=Sc(Math.abs(g));return r>1?Ve((y/(d*N))**2,2*(v-b-x-a)):Ve((y/N)**2,2*(v-x-c))});return{b0:m,b1:u,xmean:h,weights:M,converged:w<s}}function Sc(e){let t=Math.floor(Math.log2(e));return[Ve(e,-t),t]}var oo=e=>Math.atan(e),ro=e=>Number.isFinite(e)?e:Math.tan(oo(e));function Yb(e,t){let n=e.length,r=0;for(let m=0;m<n;m++)for(let u=m+1;u<n;u++)r+=Math.sign(e[u]-e[m])*Math.sign(t[u]-t[m]);let o=r>=0,s=new Float64Array(n*(n-1)/2),i=0,c=0,a=0,l=Math.PI/4;for(let m=0;m<n;m++)for(let u=m+1;u<n;u++){let h=kc(e[u],e[m]),p=kc(t[u],t[m]),w;if(h!==0)w=p/h;else if(p!==0)w=o?1/0:-1/0;else continue;s[i++]=w;let d=oo(w);o?d===-l?c++:d<-l&&a++:d===l?c++:d>l&&a++}return{sorted:s.subarray(0,i).sort(),offset:o?2*a+c:-(2*a+c),e:c}}function Gb(e,t,n){let r=e.length;if(t%2!==0){let c=(t+1)/2;return c>=1&&c<=r?ro(e[c-1]):NaN}let o=t/2;if(o<1||o+1>r)return NaN;let s=e[o-1],i=e[o];return n?(ro(s)+ro(i))/2:Math.tan((oo(s)+oo(i))/2)}function Xb(e){let t=Float64Array.from(e).sort(),n=t.length>>1;return t.length%2===1?t[n]:(t[n-1]+t[n])/2}function _c(e,t,n){return Number.isFinite(n)?Xb(e.map((r,o)=>kc(t[o],n*r))):-n}function sh(e,t,n){let r=Yb(e,t),o=Gb(r.sorted,r.sorted.length+r.offset,n);return{b0:Number.isNaN(o)?NaN:_c(e,t,o),b1:o,slopes:r}}function Qb(e,t,n,r,o){let s=e.length,{sorted:i,offset:c,e:a}=n,l=i.length,f=Math.round(r*Math.sqrt(s*(s-1)*(2*s+5)/18)),m=(p,w)=>{let d=Math.trunc((p+1)/2);if(w?d<2*a+1:d>l)return NaN;if(p%2===0){if(d+1>l)return NaN;let b=i[d-1],y=i[d];return o?(ro(b)+ro(y))/2:Math.tan((oo(b)+oo(y))/2)}return ro(i[d-1])},u=m(l-f+c,!0),h=m(l+f+c,!1);return Number.isNaN(u)||Number.isNaN(h)?{slope:[-1/0,1/0],intercept:[-1/0,1/0],defined:!1}:{slope:[u,h],intercept:[_c(e,t,h),_c(e,t,u)],defined:!0}}function Zb(e,t,n,r){let o=e.length,s=e.map((p,w)=>t[w]-n-r*p),i=s.filter(p=>p>0).length,c=s.filter(p=>p<0).length,a=Math.sqrt(c/i),l=-Math.sqrt(i/c),f=e.map((p,w)=>(t[w]+p/r-n)/Math.sqrt(1+1/(r*r))),m=Array.from({length:o},(p,w)=>w).sort((p,w)=>f[p]-f[w]||p-w),u=0,h=0;for(let p of m){let w=s[p];u+=w>0?a:w<0?l:0,h=Math.max(h,Math.abs(u))}return{nPos:i,nNeg:c,maxCusum:h}}var da=e=>e.every(t=>t===e[0]);function qc(e,t){if(!(typeof e=="number"&&Number.isInteger(e)&&e>=1))throw new RangeError(`'${t}' must be a positive integer`);return e}var no=(e,t)=>Es(e,t),ah=e=>Qn(e.filter(t=>!Number.isNaN(t)));function Jb(e,t,n={}){B(n,jb,"mcreg");let r=Ae(n.methodReg,Wb),o=Ae(n.methodCi??(r==="WDeming"?"jackknife":"analytical"),Bb),s=o==="bootstrap"||o==="nestedbootstrap",i=Ae(n.methodBootstrapCi,Kb),c=n.confLevel??.95;if(!(typeof c=="number"&&c>0&&c<1))throw new RangeError("'confLevel' must be a single number in (0, 1)");let l=Ae(n.slopeMeasure,["radian","tangent"])==="tangent",f=r!=="PaBa",m=f?n.errorRatio??1:1;if(f&&!(typeof m=="number"&&m>0&&m<1/0))throw new RangeError("'errorRatio' must be a positive finite number");let u=qc(n.nsamples??999,"nsamples"),h=qc(n.nnested??25,"nnested"),p=qc(n.iterMax??30,"iterMax"),w=n.threshold??1e-6;if(!(typeof w=="number"&&w>=0&&w<1/0))throw new RangeError("'threshold' must be a nonnegative number");if(r==="WDeming"&&o==="analytical")throw new RangeError('there is no analytical interval for weighted Deming regression (use methodCi "jackknife" or a bootstrap)');if(o==="bootstrap"&&i==="tBoot"&&r!=="Deming")throw new RangeError(`the bootstrap-t interval of ${r} needs methodCi "nestedbootstrap" (there is no analytical standard error)`);if(e.length!==t.length)throw new TypeError("'x' and 'y' must have the same length");let d=[],b=[];for(let ne=0;ne<e.length;ne++){let D=e[ne],Y=t[ne];if(typeof D!="number"||typeof Y!="number")throw new TypeError("'x' and 'y' must be numeric");if(Number.isNaN(D)||Number.isNaN(Y)){if(n.naRm===!0)continue;throw new RangeError("'x' or 'y' has missing values (use naRm: true to drop the incomplete pairs)")}if(!Number.isFinite(D)||!Number.isFinite(Y))throw new RangeError("'x' and 'y' must not contain infinite values");d.push(D),b.push(Y)}let y=d.length;if(y<=2)throw new RangeError("mcreg needs at least 3 complete pairs");if(da(d)||da(b))throw new RangeError("'x' and 'y' must not be constant");if(r==="PaBa"){if(d.some(D=>D<0)||b.some(D=>D<0))throw new RangeError("Passing-Bablok regression needs nonnegative values");if(y>rh)throw new RangeError(`Passing-Bablok regression is limited to ${rh} points (2^24 pairwise slopes)`);if((1+(o==="jackknife"||s&&i==="BCa"?y:0)+(s?u*(1+(o==="nestedbootstrap"?h:0)):0))*(y*(y-1)/2)>Ub)throw new RangeError("the Passing-Bablok fits would take more than 2^27 pairwise slopes in all (reduce nsamples or nnested)")}if(r==="WDeming"&&(d.some(ne=>ne<=0)||b.some(ne=>ne<=0)))throw new RangeError("weighted Deming regression needs positive values");let v=qe([d,b]),[M,g]=f?[qe([d]).k,qe([b]).k]:[v.k,v.k],N=f?d.map(ne=>V(ne,-M)):v.groups[0],x=f?b.map(ne=>V(ne,-g)):v.groups[1],E=Math.min(Math.max(Ve(m,2*(g-M)),Number.MIN_VALUE),2**1e3),R=Ve(m,g-M),T=new Set,A=`no konvergence after ${p} iterations`,S=(ne,D)=>{if(r==="Deming")return Ic(ne,D,E);if(r==="WDeming"){let Y=oh(ne,D,E,R,m,p,w,M,g);return Y.converged||T.add(A),Y}return sh(ne,D,l)},C,L=NaN,k,F,I;if(r==="Deming"){let ne=Ic(N,x,E);C=ne,L=V(ne.xmean,M),F=[ne.se0,ne.se1]}else if(r==="WDeming"){let ne=oh(N,x,E,R,m,p,w,M,g);ne.converged||T.add(A),C=ne,L=V(ne.xmean,M),k=ne.weights}else{let ne=sh(N,x,l);C=ne,I=ne.slopes}if(!Number.isFinite(C.b1))throw new RangeError(r==="PaBa"?"the Passing-Bablok slope is not defined for these data":"the covariance of x and y is 0: the Deming slope is not defined");let O=1-c,q=gt(O/2,y-2,!1,!1),$=le(O/2,{lowerTail:!1}),_=[C.b0,C.b1],P=[NaN,NaN],K=[[NaN,NaN],[NaN,NaN]],X,re,z,J=()=>{let ne=[],D=[];for(let Y=0;Y<y;Y++){let j=S(N.filter((U,W)=>W!==Y),x.filter((U,W)=>W!==Y));ne.push(j.b0),D.push(j.b1)}X=[ne,D],re=[(y-1)*Qn(ne)/Math.sqrt(y),(y-1)*Qn(D)/Math.sqrt(y)]};if(o==="analytical")if(r==="Deming")for(let ne of[0,1])P[ne]=F[ne],K[ne]=[_[ne]-q*P[ne],_[ne]+q*P[ne]];else{let ne=Qb(N,x,I,$,l);ne.defined||T.add("the Passing-Bablok interval is not defined for so few points: its limits are -Inf and Inf"),K[0]=ne.intercept,K[1]=ne.slope}else if(o==="jackknife"){J();for(let ne of[0,1])P[ne]=re[ne],K[ne]=[_[ne]-q*P[ne],_[ne]+q*P[ne]]}else{if(i==="BCa"&&(J(),X.some(de=>de.some(Number.isNaN))))throw new RangeError("Too many identical data points!");let ne=r==="Deming"?F:r==="WDeming"&&re!==void 0?re:[NaN,NaN],D=o==="nestedbootstrap",Y=[],j=[],U=[],W=[],te=0,fe=()=>Array.from({length:y},()=>bo(y)),Re=de=>{let Ce=de.map(Pe=>N[Pe]),xe=de.map(Pe=>x[Pe]);if(da(Ce)||da(xe)){if(r==="PaBa")return T.add("Resampling PaBa regression: x or y coordinates of all data points in subsample identical - regression coefficients undetermined!"),{b0:NaN,b1:NaN};T.add("Resampling Deming regression: all x coordinates in subsample identical - regression coefficients undetermined!")}return S(Ce,xe)},Le=()=>{for(let de=0;de<u;de++){let Ce=fe(),xe=Re(Ce),Pe;if(D){let Fe=[],Ke=[];for(let ze=0;ze<h;ze++){let et=Re(fe().map(Ue=>Ce[Ue]));Fe.push(et.b0),Ke.push(et.b1)}Pe=[ah(Fe),ah(Ke)]}else r==="Deming"?Pe=[xe.se0,xe.se1]:Pe=ne;if(Number.isNaN(xe.b0)||Number.isNaN(xe.b1)){te++;continue}Y.push(xe.b0),j.push(xe.b1),U.push(Pe[0]),W.push(Pe[1])}};if(n.rngSeed!==void 0){let de=dl()?bl():void 0;try{wl({kind:"Mersenne-Twister"}),gl(n.rngSeed),Le()}finally{de!==void 0&&vl(de)}}else Le();if(te>.05*u)throw new RangeError("There were too many resamples with undetermined regression coefficients!");let be=Y.length,me=[Y,j],ke=[U,W];for(let de of[0,1]){let Ce=me[de],xe=_[de];if(i==="quantile")K[de]=[no(Ce,O/2),no(Ce,1-O/2)];else if(i==="Student")P[de]=Qn(Ce),K[de]=[xe-q*P[de],xe+q*P[de]];else if(i==="BCa"){let Pe=Ce.filter(ce=>ce<xe).length;Ce.every(ce=>ce===xe)?Pe=be/2:Pe===0?Pe=.5:Pe===be&&(Pe=be-.5);let Fe=le(Pe/be),Ke=X[de],ze=Ke.reduce((ce,tt)=>ce+tt,0)/Ke.length,et=0,Ue=0;for(let ce of Ke){let tt=ze-ce;et+=tt*tt,Ue+=tt*tt*tt}let Xe=et===0?0:Ue/(6*et**1.5),pe=-$,oe=$,we=ae(Fe-(pe-Fe)/(1+Xe*(pe-Fe))),ge=ae(Fe-(oe-Fe)/(1+Xe*(oe-Fe)));K[de]=[no(Ce,ge),no(Ce,we)]}else{let Pe=Ce.map((Ke,ze)=>(Ke-xe)/ke[de][ze]),Fe=Number.isNaN(ne[de])?Qn(Ce):ne[de];P[de]=Fe,K[de]=[xe-no(Pe,1-O/2)*Fe,xe-no(Pe,O/2)*Fe]}}z={intercept:Y.map(de=>V(de,g)),slope:j.map(de=>V(de,g-M)),sigmaIntercept:U.map(de=>V(de,g)),sigmaSlope:W.map(de=>V(de,g-M)),globalSigma:[V(ne[0],g),V(ne[1],g-M)],nsamples:be}}let Z=ne=>{let D=Y=>V(Y,ne===0?g:g-M);return{estimate:D(_[ne]),se:D(P[ne]),confInt:[D(K[ne][0]),D(K[ne][1])]}},Se={methodReg:r,methodCi:o,...s?{methodBootstrapCi:i}:{},confLevel:c,errorRatio:m,n:y,intercept:Z(0),slope:Z(1),xmean:L,...k===void 0?{}:{weights:k},...X===void 0?{}:{jackknife:{intercept:X[0].map(ne=>V(ne,g)),slope:X[1].map(ne=>V(ne,g-M))}},...z===void 0?{}:{bootstrap:z},...r==="PaBa"?{cusum:Zb(N,x,C.b0,C.b1)}:{},warnings:[...T]};return Q("mcreg",Se)}var ev=["alpha","confLevel","lmer"],tv={ICC1:"Single_raters_absolute",ICC2:"Single_random_raters",ICC3:"Single_fixed_raters",ICC1k:"Average_raters_absolute",ICC2k:"Average_random_raters",ICC3k:"Average_fixed_raters"};function ga(e,t){let n=e-t,r=n-e;return[n,e-(n-r)+(-t-r)]}function nv(e){let t=e[0];return e.every(n=>n.every((r,o)=>{let[s,i]=ga(r,n[0]),[c,a]=ga(t[o],t[0]);return s===c&&i===a}))}var ch=(e,t)=>e.every((n,r)=>n===t[r]);function _r(e){let t=[0,0];for(let n of e)t=he(t,n);return lt(t,e.length)}var so=(e,t)=>({v:lt(e.v,t),e:e.e});function ao(...e){let t=e.filter(r=>r.v[0]!==0),n=t.length===0?0:Math.max(...t.map(r=>r.e));return e.map(r=>({at:Ve(r.v[0],r.e-n),E:n}))}function lh(e,t){if(t.v[0]===0)return e;if(e.v[0]===0)return t;let n=Math.max(e.e,t.e),r=o=>[Ve(o.v[0],o.e-n),Ve(o.v[1],o.e-n)];return{v:he(r(e),r(t)),e:n}}var fh=(e,t)=>Ve(e.v[0]/t.v[0],e.e-t.e),rv=(e,t)=>{let[n,r]=ao(e,t);return n.at<r.at},ov=(e,t)=>{let[n,r]=ao(e,t);return n.at<=r.at};function mh(e,t,n){let r=[0,0];for(let o of e){let s=Oe(o,t);r=he(r,_e(s,s))}return _e(r,[n,0])}function sv(e,t={}){B(t,ev,"ICC");let n=t.lmer??!0;if(typeof n!="boolean")throw new TypeError("'lmer' must be true or false");let r,o;if(t.alpha!==void 0){if(r=t.alpha,!(typeof r=="number"&&r>0&&r<1))throw new RangeError("'alpha' must be a single number in (0, 1)");o=1-r}else o=t.confLevel??.95,_t(o),r=1-o;if(!Array.isArray(e)||e.length===0||!e.every(ee=>Array.isArray(ee)))throw new TypeError("'x' must be a matrix (an array of rows)");let s=e[0].length;if(e.some(ee=>ee.length!==s))throw new TypeError("'x' must be a matrix (rows of equal length)");if(e.some(ee=>ee.some(ve=>typeof ve!="number")))throw new TypeError("'x' must contain numbers");if(e.some(ee=>ee.some(Number.isNaN)))throw new RangeError("'x' contains missing ratings (only complete data are supported)");if(e.some(ee=>ee.some(ve=>!Number.isFinite(ve))))throw new RangeError("'x' contains infinite ratings");let i=e.length;if(i<2||s<2)throw new RangeError("at least 2 subjects and 2 raters are needed");let{groups:c}=qe(e);if(c.every(ee=>ch(ee,c[0])))throw new RangeError(c[0].every(ee=>ee===c[0][0])?"all ratings are equal":"every subject has the same ratings: the coefficients are undefined");let a=Array.from({length:s},(ee,ve)=>c.map(ie=>ie[ve])),l=0;for(let ee of e)for(let ve of ee)l=Math.max(l,Math.abs(ve));let f=l>2**1020?3:0,m=f>0?e.map(ee=>ee.map(ve=>ve/8)):e,u=m.map(ee=>ee.map((ve,ie)=>ga(ve,m[0][ie]))),h=m.map(ee=>ee.map(ve=>ga(ve,ee[0]))),p=h.map(ee=>ee.map((ve,ie)=>Oe(ve,h[0][ie]))),w=ee=>{let ve=0;for(let ie of ee)for(let Te of ie)ve=Math.max(ve,Math.abs(Te[0]));return ve===0?0:Math.floor(Math.log2(ve))},d=(ee,ve)=>ee.map(ie=>ie.map(Te=>[V(Te[0],-ve),V(Te[1],-ve)])),[b,y,v]=[w(u),w(h),w(p)],[M,g,N]=[d(u,b),d(h,y),d(p,v)],x=ee=>Array.from({length:s},(ve,ie)=>ee.map(Te=>Te[ie])),E=mh(M.map(_r),_r(M.flat()),s),R=a.every(ee=>ch(ee,a[0]))?[0,0]:mh(x(g).map(_r),_r(g.flat()),i),T=[0,0];if(!nv(c)){let ee=N.map(_r),ve=x(N).map(_r),ie=_r(N.flat());N.forEach((Te,Be)=>Te.forEach(($e,ot)=>{let mt=he(Oe(Oe($e,ee[Be]),ve[ot]),ie);T=he(T,_e(mt,mt))}))}let A=(ee,ve)=>({v:ee,e:2*(ve+f)}),S=A(E,b),C=A(R,y),L=A(T,v),k=i-1,F=s-1,I=(i-1)*(s-1),O=so(S,k),q=so(C,F),$=so(L,I),_=so(lh(C,L),F+I),P=[];if(n){let ee={B:{ss:S,df:k,ms:O,pooled:!1},J:{ss:C,df:F,ms:q,pooled:!1}},ve=L,ie=I;for(let Be of ov(O,q)?["B","J"]:["J","B"]){let $e=ee[Be];rv($e.ms,so(ve,ie))&&(ve=lh(ve,$e.ss),ie+=$e.df,$e.pooled=!0)}let Te=so(ve,ie);if(ee.B.pooled&&(O=Te),ee.J.pooled&&(q=Te),$=Te,ee.B.pooled){let[Be,$e]=ao($,q);_={v:[Be.at+($e.at-Be.at)/i,0],e:Be.E}}}else T[0]===0&&P.push("essentially perfect fit for \u20181\u2019: summary may be unreliable");let K=(ee,ve)=>r/2<1e-8?un(r/2,ee,ve,!1,!1):un(1-r/2,ee,ve,!0,!1),X=(ee,ve,ie)=>ht(ee,ve,ie,!1,!1),re=ee=>ee===1/0?1:(ee-1)/(ee+s-1),z=ee=>1-1/ee,J=i*(s-1),Z=fh(O,_),Se=fh(O,$),[ne,D]=[Z/K(k,J),Z*K(J,k)],[Y,j]=[Se/K(k,I),Se*K(I,k)],[U,W]=ao(O,_).map(ee=>ee.at),[te,fe]=ao(O,$).map(ee=>ee.at),[Re,Le,be]=ao(O,q,$).map(ee=>ee.at),me=(U-W)/(U+(s-1)*W),ke=(Re-be)/(Re+(s-1)*be+s*(Le-be)/i),de=(te-fe)/(te+(s-1)*fe),Ce=s*ke*Le,xe=(i*(1+(s-1)*ke)-s*ke)*be,Pe=Math.max(Math.abs(Ce),Math.abs(xe)),Fe=Pe>0&&Number.isFinite(Pe)?Math.floor(Math.log2(Pe)):0,Ke=V(Ce,-Fe),ze=V(xe,-Fe),et=(i-1)*Ke*Ke+ze*ze,Ue=et===0?s-1:(s-1)*(i-1)*(Ke+ze)**2/et,Xe=K(k,Ue),pe=K(Ue,k),oe=s*Le+(s*i-s-i)*be,we=(ee,ve)=>ee>.5?1-(i*be+oe)/ve:ee,ge=we(i*(Re/Xe-be)/(oe+i*Re/Xe),oe+i*Re/Xe),ce=pe>1?we(i*(Re-be/pe)/(oe/pe+i*Re),oe+i*pe*Re):we(i*(pe*Re-be)/(oe+i*pe*Re),oe+i*pe*Re),tt=ee=>ee*s/(1+ee*(s-1)),G=X(Z,k,J),ue=X(Se,k,I),Me=(ee,ve,ie,Te,Be,$e,ot)=>({type:ee,name:tv[ee],ICC:ve,F:ie,df1:k,df2:Te,pValue:Be,lower:$e,upper:ot}),Ne=[Me("ICC1",me,Z,J,G,re(ne),re(D)),Me("ICC2",ke,Se,I,ue,ge,ce),Me("ICC3",de,Se,I,ue,re(Y),re(j)),Me("ICC1k",(U-W)/U,Z,J,G,z(ne),z(D)),Me("ICC2k",(Re-be)/(Re+(Le-be)/i),Se,I,ue,tt(ge),tt(ce)),Me("ICC3k",(te-fe)/te,Se,I,ue,z(Y),z(j))],Ee=ee=>Ve(ee.v[0],ee.e);return Q("ICC",{results:Ne,MSB:Ee(O),MSJ:Ee(q),MSE:Ee($),MSW:Ee(_),n:i,k:s,confLevel:o,lmer:n,warnings:P})}function wa(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(!Vl(kl(e)))throw new RangeError("'covmat' must be symmetric")}var av=["covmat","nObs","rotation","scores","control"],iv=["lower","start"];function Tt(e){let t=0,n=0;for(let r of e){let[o,s]=Ot(t,r);t=o,n+=s}return t+n}var Lc=(e,t)=>e[0].map((n,r)=>Tt(e.map(o=>t(o[r]))));function ph(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 uh(e,t,n){let r=Wt(ph(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 hh(e,t){let n=Lc(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=Lc(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 kn=(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})),io=e=>e[0].map((t,n)=>e.map(r=>r[n]));function ba(e){let t=e[0].length,n=e.map(m=>Math.sqrt(Tt(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=io(o),a=0;for(let m=0;m<1e3;m++){let u=kn(o,i),h=u[0].map((y,v)=>{let M=0;for(let g=0;g<s;g++)M+=u[g][v]*u[g][v];return M}),p=u.map(y=>y.map((v,M)=>v**3-v*h[M]/s)),w=kn(c,p),d=ys(w);i=kn(d.u,io(d.v));let b=a;if(a=Tt(d.d),a<b*(1+1e-5))break}let l=kn(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 cv(e){let t=ba(e),n=t.loadings,r=n.map(c=>c.map(a=>a*Math.abs(a)**3)),o=vs(Lt(n),r),s=nt(kn(io(o),o)).map((c,a)=>c[a]),i=o.map(c=>c.map((a,l)=>a*Math.sqrt(s[l])));return{loadings:kn(n,i),rotmat:kn(t.rotmat,i)}}function lv(e,t,n={}){B(n,av,"factanal"),B(Cn(n,"control"),iv,"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");wa(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=Ms(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((R,T)=>R/(l[E]*l[T]))),m=n.control?.lower??.005,u=n.control?.start?.slice()??nt(f).map((x,E)=>(1-c/(2*i))/x[E]),h=x=>{let E=Wt(ph(f,x),{symmetric:!0,onlyValues:!0}).values.slice(c);return-(Tt(E.map(R=>Math.log(R)-R))-c+i)},p=x=>{let E=uh(f,x,c);return x.map((R,T)=>{let A=0;for(let S=0;S<c;S++)A+=E[T][S]*E[T][S];return(A+R-f[T][T])/(R*R)})},w=rt(()=>Ro(u,h,{gr:p,method:"L-BFGS-B",lower:m,upper:1,control:{fnscale:1,parscale:new Array(i).fill(.01)}}));if(w.convergence!==0)throw new Error("unable to optimize from this starting value");let d=w.par,b=hh(uh(f,d,c)).L,y,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"?ba(b):cv(b),E=hh(x.loadings,x.rotmat);b=E.L,y=E.rot}let M=NaN,g=NaN;!Number.isNaN(s)&&a>0&&(M=(s-1-(2*i+5)/6-2*c/3)*w.value,g=It(M,a,{lowerTail:!1}));let N={uniquenesses:d,loadings:b,STATISTIC:M,dof:a,PVAL:g,converged:!0,objective:w.value,counts:w.counts,warnings:[]};return r!=="none"&&(N.scores=fv(e,f,d,b,r)),y!==void 0&&(N.rotmat=y),N}function fv(e,t,n,r,o){let s=e.length,i=e[0].map((m,u)=>Tt(e.map(h=>h[u]))/s),c=e.map(m=>m.map((u,h)=>u-i[h])),a=Lc(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 kn(l,nt(t,r));let f=r.map((m,u)=>m.map(h=>h/n[u]));return kn(l,io(nt(kn(io(r),f),io(f))))}var mv=["nfactors","covmat"],co=2**-52,uv=`
13
+ To do this, run the function again with the 'check.keys=TRUE' option`)}return n}function Eb(e,t,n){let r=Go(e,"x").length;ma(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=Ro(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=nt(a,l);for(let m=0;m<2;m++)for(let u=0;u<2;u++)c[m][u]-=De(i.map((h,p)=>o[s[m]][h]*f[p][u]))}return c[0][1]/Math.sqrt(c[0][0]*c[1][1])}function xb(e){let t=e.length,n=e[0].length,r=a=>a.reduce((l,f)=>he(l,[f,0]),[0,0]),o=e.map(a=>ft(r(a),n)),s=e[0].map((a,l)=>ft(r(e.map(f=>f[l])),t)),i=ft(r(e.flat()),t*n),c=[0,0];return e.forEach((a,l)=>a.forEach((f,m)=>{let u=he(Le(Le([f,0],o[l]),s[m]),i);c=he(c,_e(u,u))})),c[0]+c[1]}var Rb=["controls","cases"],Hu=["levels","direction","naRm"],Tb=["partialAuc","focus","correct"],Sb=["bestMethod","bestWeights"],Ju=new WeakMap;function ha(e){return Ju.get(e)}var Ab=["auto","<",">"];function Ec(e){if(e==null)return null;if(typeof e=="number")return Number.isNaN(e)?null:$n(e);if(typeof e=="boolean")return e?"TRUE":"FALSE";if(typeof e=="string")return e;throw new TypeError("the response must hold numbers, strings or booleans")}function qb(e){let t=e.filter(s=>Ec(s)!==null),n=new Set(t.map(s=>typeof s));if(n.size>1)throw new TypeError("the response mixes values of different types");let r=[...n][0],o;return r==="number"?o=[...new Set(t)].sort((s,i)=>s-i).map($n):r==="boolean"?o=[...new Set(t)].sort((s,i)=>Number(s)-Number(i)).map(s=>s?"TRUE":"FALSE"):o=[...new Set(t)].sort((s,i)=>s<i?-1:s>i?1:0),[...new Set(o)]}function Qo(e){return Float64Array.from(e).sort()}function xc(e,t){let n=Qo(e),r=Qo(t),o=[],s=[],i=[],c=0,a=0;for(;c<n.length||a<r.length;){let l=a>=r.length||c<n.length&&n[c]<=r[a]?n[c]:r[a];for(;c<n.length&&n[c]===l;)c++;for(;a<r.length&&r[a]===l;)a++;o.push(l),s.push(c),i.push(a)}return{u:o,ctrlLE:s,caseLE:i}}function Rc(e,t,n){let r=0;for(let o=0;o<e.u.length;o++){let s=o>0?e.ctrlLE[o-1]:0,i=e.caseLE[o]-(o>0?e.caseLE[o-1]:0),c=e.ctrlLE[o]-s,a=n==="<"?s:t-e.ctrlLE[o];r+=i*(2*a+c)}return r}function Tc(e,t,n){let r=e.length,o=t.length;return Rc(xc(e,t),r,n)/(2*o*r)}function Sc(e,t,n){return Rc(xc(e,t),e.length,n)}var Yu=e=>e===0n?0:e.toString(2).length;function Jr(e,t){if(t<0n&&(e=-e,t=-t),e===0n)return 0;let n=e<0n,r=n?-e:e,o=55-(Yu(r)-Yu(t)),s=o>=0?r<<BigInt(o):r,i=o>=0?t:t<<BigInt(-o),c=s/i;c*i!==s&&(c|=1n);let a=Ve(Number(c),-o);return n?-a:a}function kb(e,t,n){let r=(e+t)/2;return Math.abs(r)<=1e100||(r=e/2+t/2),(r===e||r===t)&&(r=n==="<"?t:e),r}function Ib(e,t,n){let r=xc(e,t),o=e.length,s=t.length,i=r.u.length,c=[],a=[],l=[],f=m=>{let u=m===0?-1/0:m===i?1/0:kb(r.u[m-1],r.u[m],n),h=m===0?0:r.caseLE[m-1],p=m===0?0:r.ctrlLE[m-1];c.push(u),a.push((n==="<"?s-h:h)/s),l.push((n==="<"?p:o-p)/o)};if(n==="<")for(let m=0;m<=i;m++)f(m);else for(let m=i;m>=0;m--)f(m);return{thresholds:c,sensitivities:a,specificities:l,auc:Rc(r,o,n)/(2*s*o)}}function _b(e,t){let n=r=>{let o=Qo(r),s=o.length>>1;return o.length%2===1?o[s]:dn(o[s-1],o[s])};return n(e)<=n(t)?"<":">"}function eh(e,t,n,r,o,s){if(e.length===0)throw new Error("No control observation.");if(t.length===0)throw new Error("No case observation.");if(e.some(l=>!Number.isFinite(l))||t.some(l=>!Number.isFinite(l)))throw new RangeError("infinite values in the predictor: cannot build a valid ROC curve");let i=n==="auto"?_b(e,t):n,a={...Ib(e,t,i),direction:i,levels:r,controls:e,cases:t,warnings:s};return Ju.set(a,o),a}function th(e){if(e===void 0)return"auto";if(!Ab.includes(e))throw new RangeError("'arg' should be one of \u201Cauto\u201D, \u201C<\u201D, \u201C>\u201D");return e}function Qr(e,t,n){let r=th(n.direction),o=new Rt,s=e.map(Ec),i;if(n.levels===void 0){if(i=qb(e),i.length>2&&(o.add("'response' has more than two levels. Consider setting 'levels' explicitly or using 'multiclass.roc' instead"),i=i.slice(0,2)),i.length<2)throw new RangeError("'response' must have two levels")}else{if(!Array.isArray(n.levels)||n.levels.length!==2)throw new RangeError("'levels' argument must have length 2");i=n.levels.map(p=>{let w=Ec(p);if(w===null)throw new RangeError("'levels' must not be missing");return w})}if(t.some(p=>typeof p!="number"))throw new TypeError("Predictor must be numeric.");if(s.length!==t.length)throw new TypeError("Response and predictor must be vectors of the same length.");let c=s.map((p,w)=>p===null||Number.isNaN(t[w]));if(n.naRm===!1&&c.some(p=>p))throw new RangeError("missing values in the response or the predictor with naRm false");let a=[],l=[],f=[],m=[],u=[];s.forEach((p,w)=>{if(c[w])return;let d=t[w];if(p===i[0])a.push(d);else if(p===i[1])l.push(d);else return;f.push(d),m.push(p===i[1]),u.push(w)});let h={labels:s,predictor:[...t],values:f,isCase:m,kept:u};return eh(a,l,r,[i[0],i[1]],h,o.list)}function Lb(e,t){let n=ws(er(e),Object.keys(t));if(n.response!==0||n.variables.length!==2||n.terms.length!==1)throw new TypeError("the formula must be of the form response ~ predictor");let r=Sl(t,n.variables),[o,s]=r.variables;if(s.kind!=="numeric")throw new TypeError("Predictor must be numeric.");return{response:o.kind==="numeric"?o.values:o.codes.map(c=>o.levels[c]),predictor:s.values}}function Cb(e){return typeof e=="object"&&e!==null&&!Array.isArray(e)&&!ArrayBuffer.isView(e)}function Fb(e,t,n={}){if(ct(e)&&B(e,Rb,"roc"),typeof e!="string"&&ct(t)&&B(t,Hu,"roc"),B(n,Hu,"roc"),typeof e=="string"){let r=Lb(e,t);return Q("roc",Qr(r.response,r.predictor,n))}if(Cb(e)){let r=t??{};if(r.levels!==void 0)throw new RangeError("'levels' cannot be given with controls and cases");let o=e;if(o.controls===void 0||o.cases===void 0)throw new TypeError("'controls' and 'cases' must both be given");let s=th(r.direction),i=Array.from(o.controls),c=Array.from(o.cases);if([...i,...c].some(u=>typeof u!="number"))throw new TypeError("Predictor must be numeric.");if(r.naRm===!1&&[...i,...c].some(Number.isNaN))throw new RangeError("missing values in the controls or the cases with naRm false");let a=i.filter(u=>!Number.isNaN(u)),l=c.filter(u=>!Number.isNaN(u)),f=[...a,...l],m={labels:null,predictor:f,values:f,isCase:f.map((u,h)=>h>=a.length),kept:f.map((u,h)=>h)};return Q("roc",eh(a,l,s,["0","1"],m,[]))}return Q("roc",Qr(Array.from(e),Array.from(t),n))}var ua=(e,t,n,r,o)=>t[r]+(n-e[r])/(e[o]-e[r])*(t[o]-t[r]);function Ob(e,t,n,r){let o=[];if(e.forEach((a,l)=>{a>=n&&a<=r&&o.push(l)}),o.length===0){let a=e.findIndex(u=>u>=r),l=a-1,f=ua(e,t,r,a,l),m=ua(e,t,n,a,l);return(f+m)/2*(r-n)}let s=0;for(let a=1;a<o.length;a++){let l=o[a-1],f=o[a];s+=(e[f]-e[l])*(t[l]+t[f])/2}let i=o[o.length-1];if(e[i]!==r){let a=e.findIndex(m=>m>=r),l=a-1,f=ua(e,t,r,a,l);s+=(r-e[l])*(t[l]+f)/2}let c=o[0];if(e[c]!==n){let a=e.findIndex(m=>m>n)-1,l=a+1,f=ua(e,t,n,l,a);s+=(e[l]-n)*(t[l]+f)/2}return s}var hr=(e,t)=>[e[0]*t[1]+t[0]*e[1],e[1]*t[1]],_r=(e,t)=>[e[0]*t[1]-t[0]*e[1],e[1]*t[1]],qn=(e,t)=>[e[0]*t[0],e[1]*t[1]],Gu=(e,t)=>t[0]<0n?[-e[0]*t[1],-e[1]*t[0]]:[e[0]*t[1],e[1]*t[0]],Xo=(e,t)=>{let n=e[0]*t[1]-t[0]*e[1];return n<0n?-1:n>0n?1:0};function Xu(e,t){if(!(t>=1))return;let n=e.map(r=>Math.round(r*t));return n.every((r,o)=>r/t===e[o])?n:void 0}function Qu(e,t,n,r,o,s){let i=Xu(e,n),c=Xu(t,r);if(i!==void 0&&c!==void 0)return Zu(i.map(BigInt),c.map(BigInt),BigInt(n),BigInt(r),o,s);if(!(e.length>=2&&e.length===t.length&&e[0]===0&&e[e.length-1]===1&&e.every((h,p)=>h>=0&&h<=1&&(p===0||h>=e[p-1]))&&t.every(h=>h>=0&&h<=1)))throw new RangeError("the curve's sensitivities and specificities must be numbers in [0, 1] forming a ROC curve (use a curve from roc)");let l=e.map(Zr),f=t.map(Zr),m=0n;for(let[,h]of[...l,...f])m=h>m?h:m;let u=h=>h[0]*(m/h[1]);return Zu(l.map(u),f.map(u),m,m,o,s)}function Zu(e,t,n,r,o,s){let i=n*r,c=d=>[t[d]*n+e[d]*r-i,i],a=d=>[e[d],n],l=Zr(o),f=Zr(s),m=(d,b,N)=>hr(c(b),qn(Gu(_r(d,a(b)),_r(a(N),a(b))),_r(c(N),c(b)))),u=[];e.forEach((d,b)=>{Xo(a(b),l)>=0&&Xo(a(b),f)<=0&&u.push(b)});let h;if(u.length===0){let d=e.findIndex((N,v)=>Xo(a(v),f)>=0),b=d-1;h=qn(hr(m(f,b,d),m(l,b,d)),qn(_r(f,l),[1n,2n]))}else{let d=0n;for(let v=1;v<u.length;v++){let E=u[v-1],g=u[v];d+=(e[g]-e[E])*(c(E)[0]+c(g)[0])}h=[d,2n*n*i];let b=u[u.length-1];if(Xo(a(b),f)!==0){let v=b+1;h=hr(h,qn(_r(f,a(b)),qn(hr(c(b),m(f,b,v)),[1n,2n])))}let N=u[0];if(Xo(a(N),l)!==0){let v=N-1;h=hr(h,qn(_r(a(N),l),qn(hr(c(N),m(l,N,v)),[1n,2n])))}}if(h[0]<0n)throw new Error("Partial AUC correction not defined for ROC curves below the diagonal.");let p=qn(qn(_r(f,l),hr(f,l)),[1n,2n]),w=Gu(hr(p,h),qn(p,[2n,1n]));return Jr(w[0],w[1])}function Db(e,t={}){B(t,Tb,"auc");let n=Se(t.focus,["specificity","sensitivity"]),r=t.correct??!1;if(t.partialAuc===void 0)return Q("auc",{auc:e.auc,partialAuc:null,focus:n,correct:r,warnings:[]});let o=t.partialAuc;if(!Array.isArray(o)||o.length!==2||o.some(p=>typeof p!="number"))throw new TypeError("partialAuc must be a numeric vector of length 2");let s=Math.max(o[0],o[1]),i=Math.min(o[0],o[1]);if(!(i>=0&&s<=1))throw new RangeError("the limits of partialAuc must be within [0, 1]");if(!(i<s))throw new RangeError("the limits of partialAuc must differ");let c=e.sensitivities,a=e.specificities,[l,f]=n==="specificity"?[a,c]:[[...c].reverse(),[...a].reverse()],m,u=e.controls.length,h=e.cases.length;return r?m=n==="specificity"?Qu(l,f,u,h,i,s):Qu(l,f,h,u,i,s):m=Ob(l,f,i,s),Q("auc",{auc:m,partialAuc:[s,i],focus:n,correct:r,warnings:[]})}function Zr(e){if(e===0)return[0n,1n];let t=0,n=e;for(;!Number.isInteger(n);)n*=2,t++;return[BigInt(n),1n<<BigInt(t)]}function Pb(e,t){let n=Qo(e.controls),r=Qo(e.cases),o=(i,c,a)=>{let l=0,f=i.length;for(;l<f;){let m=l+f>>1;(a?i[m]<=c:i[m]<c)?l=m+1:f=m}return l},s=e.direction==="<";return{tp:t.map(i=>s?r.length-o(r,i,!1):o(r,i,!0)),tn:t.map(i=>s?o(n,i,!1):n.length-o(n,i,!0))}}function Vb(e,t="all",n={}){B(n,Sb,"coords");let r=Se(n.bestMethod,["youden","closest.topleft","topleft"]),o=n.bestWeights??[1,.5];if(!Array.isArray(o)||o.length!==2||o.some(d=>typeof d!="number"))throw new TypeError("'best.weights' must be a numeric vector of length 2");let[s,i]=o;if(!(i>0&&i<1))throw new RangeError("prevalence ('best.weights[2]') must be in the interval ]0,1[.");if(!(s>0&&s<1/0))throw new RangeError("the cost ('bestWeights[0]') must be a positive finite number");let c=(1-i)/(s*i),a,l=null;if(typeof t=="string"&&(l=Se(t,["all","best"])),l!==null)a=[...e.thresholds];else{if(a=typeof t=="number"?[t]:Array.from(t),a.length===0)throw new RangeError("Numeric 'x' has length 0");if(a.some(d=>typeof d!="number"||Number.isNaN(d)))throw new RangeError("the thresholds must be numbers (not NaN)")}let f=e.cases.length,m=e.controls.length,{tp:u,tn:h}=Pb(e,a);if(l==="best"){let[d,b]=Zr(s),[N,v]=Zr(i),E=(v-N)*b,g=d*N,y=BigInt(f),x=BigInt(m),M=u.map((S,A)=>{if(r==="youden")return BigInt(S)*x*g+E*BigInt(h[A])*y;let C=BigInt(f-S),L=BigInt(m-h[A]);return-(C*C*x*x*g+E*L*L*y*y)}),R=M.reduce((S,A)=>A>S?A:S),T=M.map((S,A)=>S===R?A:-1).filter(S=>S>=0);a=T.map(S=>a[S]),u=T.map(S=>u[S]),h=T.map(S=>h[S])}let p=u.map(d=>f-d),w=h.map(d=>m-d);return Q("coords",{threshold:a,sensitivity:u.map(d=>d/f),specificity:h.map(d=>d/m),accuracy:u.map((d,b)=>(d+h[b])/(f+m)),tn:h,tp:u,fn:p,fp:w,npv:h.map((d,b)=>d/(d+p[b])),ppv:u.map((d,b)=>d/(d+w[b])),fpr:w.map(d=>d/m),lrPos:u.map((d,b)=>d*m/(f*w[b])),lrNeg:p.map((d,b)=>d*m/(f*h[b])),youden:u.map((d,b)=>d/f+c*(h[b]/m)),closestTopleft:p.map((d,b)=>(d/f)**2+c*(w[b]/m)**2),warnings:[]})}var $b=["confLevel","method","bootN","bootStratified"],nh=["method","alternative","paired","confLevel","bootN","bootStratified","levels","direction"];function ga(e){let t=Float64Array.from(e.controls).sort(),n=Float64Array.from(e.cases).sort(),r=t.length,o=n.length,s=(l,f)=>{let m=0,u=l.length;for(;m<u;){let p=m+u>>1;l[p]<f?m=p+1:u=p}let h=m;for(;h<l.length&&l[h]===f;)h++;return[m,h-m]},i=e.direction==="<",c=e.cases.map(l=>{let[f,m]=s(t,l);return 2*(i?f:r-f-m)+m}),a=e.controls.map(l=>{let[f,m]=s(n,l);return 2*(i?o-f-m:f)+m});return{k10:c,k01:a}}function Ic(e,t,n,r){return at(e)/(4*n*n)/r+at(t)/(4*r*r)/n}function rh(e){let{k10:t,k01:n}=ga(e);return Ic(t,n,e.controls.length,e.cases.length)}function pa(e,t,n,r,o,s){let i=BigInt(Sc(e,t,n)),c=BigInt(Sc(r,o,s)),a=BigInt(e.length)*BigInt(t.length),l=BigInt(r.length)*BigInt(o.length);return Jr(i*l-c*a,2n*a*l)}var qc=e=>e.sensitivities.some((t,n)=>t===1&&e.specificities[n]===1);function to(e,t){if(e.controls.length<2||e.cases.length<2)throw new Error(`${t} needs at least 2 controls and 2 cases`)}function sh(e){let t=(1-e)/2;return t<1e-8?le(t,{lowerTail:!1}):le(1-t)}function ah(e){if(!(Number.isInteger(e)&&e>=1&&e<=2**24))throw new RangeError("bootN must be an integer from 1 to 2^24")}function no(e){let t=new Array(e.length);for(let n=0;n<e.length;n++)t[n]=e[vo(e.length)];return t}function kc(e){let t=ha(e);if(t!==void 0)return{values:t.values,isCase:t.isCase};let n=[...e.controls,...e.cases];return{values:n,isCase:n.map((r,o)=>o>=e.controls.length)}}function Zo(e,t,n){let r=[],o=[];for(let s of n)(t[s]?o:r).push(e[s]);if(!(r.length===0||o.length===0))return{controls:r,cases:o}}function zb(e,t,n,r){let o=Zo(e,t,n);return o===void 0?NaN:Tc(o.controls,o.cases,r)}function eo(e){let t=new Array(e);for(let n=0;n<e;n++)t[n]=vo(e);return t}function jb(e,t={}){B(t,$b,"ciAuc");let n=t.confLevel??.95;cn(n);let r=Se(t.method,["delong","bootstrap"]),o=new Rt;qc(e)&&o.add("ci.auc() of a ROC curve with AUC == 1 is always 1-1 and can be misleading.");let s=1-n;if(r==="delong"){to(e,"DeLong's interval");let{k10:m,k01:u}=ga(e),h=e.auc,p=Ic(m,u,e.controls.length,e.cases.length),w=Math.sqrt(p),d=v=>Math.min(1,Math.max(0,v)),b=s/2,N=n===1?[0,1]:w===0?[h,h]:[d(h+le(b)*w),d(h+sh(n)*w)];return Q("ciAuc",{estimate:h,confInt:N,confLevel:n,method:r,variance:p,warnings:o.list})}let i=t.bootN??2e3;ah(i);let c=t.bootStratified??!0,a=[],l=0;if(c){to(e,"a stratified bootstrap");for(let m=0;m<i;m++){let u=no(e.controls),h=no(e.cases);a.push(Tc(u,h,e.direction))}}else{let{values:m,isCase:u}=kc(e);for(let h=0;h<i;h++){let p=zb(m,u,eo(m.length),e.direction);Number.isNaN(p)?l++:a.push(p)}}if(l>0&&o.add("NA value(s) produced during bootstrap were ignored."),a.length===0)throw new Error("no bootstrap replicate had both controls and cases");let f=Ss(a,[s/2,1-s/2]);return Q("ciAuc",{estimate:e.auc,confInt:[f[0],f[1]],confLevel:n,method:r,variance:a.length>1?at(a):NaN,warnings:o.list})}var oh=e=>typeof e=="object"&&e!==null&&!Array.isArray(e)&&"thresholds"in e&&"sensitivities"in e,da=(e,t)=>e.length===t.length&&e.every((n,r)=>n===t[r]);function Wb(e,t){let n=ha(e),r=ha(t);if(n?.labels==null||r?.labels==null||!da(e.levels,t.levels))return;let o=c=>c.kept.map(a=>c.labels[a]);if(da(o(n),o(r))&&da(n.isCase,r.isCase))return[e,t];if(!da(n.labels,r.labels))return;let s=n.labels.map((c,a)=>a).filter(c=>n.labels[c]!==null&&!Number.isNaN(n.predictor[c])&&!Number.isNaN(r.predictor[c])),i=(c,a)=>Qr(s.map(l=>a.labels[l]),s.map(l=>a.predictor[l]),{levels:c.levels,direction:c.direction});return[i(e,n),i(t,r)]}function Ac(e,t){return t==="two.sided"?2*se(-Math.abs(e)):t==="greater"?se(e,{lowerTail:!1}):se(e)}function Bb(e,t,n,r){ct(n)&&B(n,nh,"rocTest"),B(r,nh,"rocTest");let o,s=new Rt,i,c;if(oh(e)){if(!oh(t))throw new TypeError("roc2 must be a curve from roc()");if(o=n??{},o.levels!==void 0||o.direction!==void 0)throw new RangeError("'levels' and 'direction' apply only to a response and two predictors");i=e,c=t}else{o=r??{};let g=Array.from(e),y=Array.from(t),x=Array.from(n);if(g.length!==y.length||g.length!==x.length)throw new TypeError("the response and the predictors must have the same length");let M=g.map((S,A)=>A).filter(S=>{let A=g[S];return A!=null&&!(typeof A=="number"&&Number.isNaN(A))&&!Number.isNaN(y[S])&&!Number.isNaN(x[S])}),R=M.map(S=>g[S]),T={levels:o.levels,direction:o.direction};i=Qr(R,M.map(S=>y[S]),T),c=Qr(R,M.map(S=>x[S]),T);for(let S of i.warnings)s.add(S)}let a=Ze(o.alternative),l=Se(o.method,["delong","bootstrap"]),f=o.confLevel??.95;cn(f),qc(i)&&qc(c)&&s.add("roc.test() of two ROC curves with AUC == 1 has always p.value = 1 and can be misleading.");let m=Wb(i,c),u;if(o.paired===!0){if(m===void 0)throw new RangeError("The paired ROC test cannot be applied to unpaired curves.");u=!0}else o.paired===!1?(m!==void 0&&s.add("The ROC curves seem to be paired. Consider performing a paired roc.test."),u=!1):u=m!==void 0;u&&([i,c]=m);let h={alternative:a,estimate:[i.auc,c.auc],estimateNames:["AUC of roc1","AUC of roc2"],nullValue:0,nullValueName:"difference in AUC",roc1:i,roc2:c},p=pa(i.controls,i.cases,i.direction,c.controls,c.cases,c.direction);if(l==="delong"){if(i.direction!==c.direction&&s.add("DeLong's test should not be applied to ROC curves with a different direction."),to(i,"DeLong's test"),to(c,"DeLong's test"),u){let C=ga(i),L=ga(c),k=C.k10.map(($,_)=>$-L.k10[_]),F=C.k01.map(($,_)=>$-L.k01[_]),I=Math.sqrt(Ic(k,F,i.controls.length,i.cases.length)),O=p===0&&I===0?0:p/I,q=I===0?0:sh(f)*I;return Q("rocTest",{method:"DeLong's test for two correlated ROC curves",statistic:O,statisticName:"Z",pValue:Ac(O,a),confInt:[p-q,p+q],confLevel:f,...h,warnings:s.list})}let g=rh(i),y=rh(c),x=i.controls.length+i.cases.length,M=c.controls.length+c.cases.length,R=g+y,T,S,A;return R===0?(T=p===0?0:p>0?1/0:-1/0,S=NaN,A=Ac(T,a)):(T=p/Math.sqrt(R),S=R*R/(g*g/(x-1)+y*y/(M-1)),A=a==="two.sided"?2*dt(-Math.abs(T),S,!0,!1):a==="greater"?dt(T,S,!1,!1):dt(T,S,!0,!1)),Q("rocTest",{method:"DeLong's test for two ROC curves",statistic:T,statisticName:"D",parameter:S,parameterName:"df",pValue:A,...h,warnings:s.list})}let w=o.bootN??2e3;ah(w);let d=o.bootStratified??!0,b=[],N=0;if(d){to(i,"a stratified bootstrap"),to(c,"a stratified bootstrap");for(let g=0;g<w;g++)if(u){let y=eo(i.controls.length),x=eo(i.cases.length);b.push(pa(y.map(M=>i.controls[M]),x.map(M=>i.cases[M]),i.direction,y.map(M=>c.controls[M]),x.map(M=>c.cases[M]),c.direction))}else{let y=no(i.controls),x=no(i.cases),M=no(c.controls),R=no(c.cases);b.push(pa(y,x,i.direction,M,R,c.direction))}}else{let g=kc(i),y=kc(c);for(let x=0;x<w;x++){let M,R;if(u){let T=eo(g.values.length);M=Zo(g.values,g.isCase,T),R=Zo(y.values,y.isCase,T)}else M=Zo(g.values,g.isCase,eo(g.values.length)),R=Zo(y.values,y.isCase,eo(y.values.length));M===void 0||R===void 0?N++:b.push(pa(M.controls,M.cases,i.direction,R.controls,R.cases,c.direction))}}if(N>0&&s.add(`${N} NA value(s) produced during bootstrap were ignored.`),b.length<2)throw new Error("fewer than 2 bootstrap replicates had both controls and cases");let v=Math.sqrt(at(b)),E=p===0&&v===0?0:p/v;return Q("rocTest",{method:u?"Bootstrap test for two correlated ROC curves":"Bootstrap test for two ROC curves",statistic:E,statisticName:"D",parameter:w,parameterName:"boot.n",pValue:Ac(E,a),bootStratified:d,...h,warnings:s.list})}var Kb=["method","confLevel","prevalence"];function Ub(e,t={}){B(t,Kb,"diagnosticTests");let n=Se(t.method,["exact","wilson","jeffreys"]),r=t.confLevel??.95;if(_t(r),!Array.isArray(e)||e.length!==2||e.some(g=>!(Array.isArray(g)||ArrayBuffer.isView(g))||g.length!==2))throw new TypeError("'table' must be a 2 x 2 table (an array of two rows of two counts)");let o=[e[0][0],e[0][1],e[1][0],e[1][1]];if(o.some(g=>typeof g!="number"))throw new TypeError("the counts must be numbers");if(!o.every(vn))throw new RangeError("the counts must be nonnegative integers");let[s,i,c,a]=o.map(Math.round),l=s+c,f=i+a,m=s+i,u=c+a,h=l+f;if(l===0||f===0||m===0||u===0)throw new RangeError("every row and column total of the table must be positive");let p=(g,y)=>{let x=ot(()=>gi(g,y,{method:n==="exact"?"clopper-pearson":n,confLevel:r}));return{estimate:x.estimate,confInt:x.confInt}},w=1-r,d=w/2<1e-8?le(w/2,{lowerTail:!1}):le(1-w/2),b=(g,y)=>{let x=d*Math.sqrt(y);return{estimate:g,confInt:[Math.exp(Math.log(g)-x),Math.exp(Math.log(g)+x)]}},N=s/l,v=a/f,E={sensitivity:p(s,l),specificity:p(a,f),ppv:p(s,m),npv:p(a,u),lrPos:b(i===0?N/0:Jr(BigInt(s)*BigInt(f),BigInt(l)*BigInt(i)),c/(s*l)+a/(i*f)),lrNeg:b(a===0?c/l/v:Jr(BigInt(c)*BigInt(f),BigInt(l)*BigInt(a)),s/(c*l)+i/(a*f)),accuracy:p(s+a,h),diagnosticOR:b(s*a/(i*c),1/s+1/i+1/c+1/a),prevalence:p(l,h),apparentPrevalence:p(m,h),method:n,confLevel:r,warnings:[]};if(t.prevalence!==void 0){let g=t.prevalence;if(!(typeof g=="number"&&g>0&&g<1))throw new RangeError("'prevalence' must be a number in (0, 1)");let y=g<2**-500?2**600:1,x=N*(g*y),M=i/f*((1-g)*y),R=v*(1-g),T=c/l*g;E.ppvAtPrevalence=x/(x+M),E.npvAtPrevalence=R/(R+T)}return Q("diagnosticTests",E)}var Hb=["two","mode","confLevel","ciMethod"],Yb=["package","exact"],kn=(e,t)=>t===0?0:e*t;function Gb(e,t,n={}){if(B(n,Hb,"blandAltman"),e.length!==t.length)throw new TypeError("'x' and 'y' must have the same length");let r=n.two??1.96;if(!(typeof r=="number"&&r>0&&r<1/0))throw new RangeError("'two' must be a positive number");let o=n.mode??1;if(o!==1&&o!==2)throw new RangeError("'mode' must be 1 or 2");let s=n.confLevel??.95;cn(s);let i=Se(n.ciMethod,Yb),c=[],a=[];for(let I=0;I<e.length;I++){let O=e[I],q=t[I];if(typeof O!="number"||typeof q!="number")throw new TypeError("'x' and 'y' must be numeric");if(!(Number.isNaN(O)||Number.isNaN(q))){if(!Number.isFinite(O)||!Number.isFinite(q))throw new RangeError("'x' and 'y' must not contain infinite values");c.push(O),a.push(q)}}let l=c.length;if(l<2)throw new RangeError("fewer than 2 complete pairs");let[f,m]=o===1?[c,a]:[a,c],u=c.map((I,O)=>{let q=(I+a[O])/2;return Number.isFinite(q)?q:I/2+a[O]/2}),h=f.map((I,O)=>I-m[O]),p=!h.every(Number.isFinite),{groups:w,k:d}=qe([p?f.map((I,O)=>I/2-m[O]/2):h]),b=w[0],N=d+(p?1:0),v=it(b),E=Zn(b),g=r*E,y=v-g,x=v+g,M=l-1,R=(1-s)/2,T=gt(R,M,!0,!1),S=R<1e-8?gt(R,M,!1,!1):gt((s+1)/2,M,!0,!1),A=[v+kn(T,E)/Math.sqrt(l),v+kn(S,E)/Math.sqrt(l)],C,L;if(i==="package"){let I=3*E*E/l,O=I>=2**-1022||I===0?Math.sqrt(I):E*Math.sqrt(3/l);C=[y+kn(T,O),y+kn(S,O)],L=[x+kn(T,O),x+kn(S,O)]}else{let I=r*Math.sqrt(l),O=_a(R,M,I,!0,!1)/Math.sqrt(l),q=_a(R,M,I,!1,!1)/Math.sqrt(l);C=[v-kn(q,E),v-kn(O,E)],L=[v+kn(O,E),v+kn(q,E)]}let k=I=>V(I,N),F=I=>[k(I[0]),k(I[1])];return Q("blandAltman",{means:u,diffs:h,n:l,meanDiff:k(v),lowerLimit:k(y),upperLimit:k(x),criticalDiff:k(g),meanDiffConfInt:F(A),lowerLimitConfInt:F(C),upperLimitConfInt:F(L),confLevel:s,two:r,mode:o,ciMethod:i,warnings:[]})}var Xb=new Set(["methodReg","methodCi","methodBootstrapCi","errorRatio","confLevel","slopeMeasure","nsamples","nnested","rngSeed","iterMax","threshold","naRm"]),Qb=["PaBa","Deming","WDeming"],Zb=["analytical","jackknife","bootstrap","nestedbootstrap"],Jb=["quantile","Student","BCa","tBoot"],ih=5793,ev=2**27;function Cc(e,t){let n=e-t;return Math.abs(n)<1e-12*(Math.abs(e)+Math.abs(t))/2?0:n}function tv(e,t){let n=Qn(e),r=Qn(t),o=[0,0],s=[0,0],i=[0,0];for(let c=0;c<e.length;c++){let a=Le([e[c],0],n),l=Le([t[c],0],r);o=he(o,_e(a,a)),s=he(s,_e(l,l)),i=he(i,_e(a,l))}return{u:o,q:s,p:i,mx:n,my:r}}function mh(e,t,n,r){if(n===0)return NaN;if(r<=1){let i=r*t-e,c=Math.sqrt(i*i+4*r*n*n);return i>=0?(i+c)/(2*r*n):2*n/(c-i)}let o=t-e/r,s=Math.sqrt(o*o+4*n*n/r);return o>=0?(o+s)/(2*n):2*n/r/(s-o)}function Fc(e,t,n){let r=e.length,{u:o,q:s,p:i,mx:c,my:a}=tv(e,t),l=mh(o[0],s[0],i[0],n),f=Le(a,_e([l,0],c))[0],m=_e(o,s),u=i[0]*i[0]/(o[0]*s[0]),h=Le(m,_e(i,i))[0]/(m[0]+m[1]);Math.abs(h)<1e-12*(1+u)/2&&(h=0);let p=Math.sqrt(l*l*h/u/(r-2)),w=[0,0];for(let b of e)w=he(w,_e([b,0],[b,0]));let d=Math.sqrt(p*p*ft(w,r)[0]);return{b0:f,b1:l,se0:d,se1:p,xmean:c[0]}}function ch(e,t,n,r,o,s,i,c,a){let l=e.length,f=Fc(e,t,n),m=f.b0,u=f.b1,h=NaN,p=new Array(l),w=s;for(let g=0;g<s;g++){let y=1+n*u*u,x=1/0;for(let q=0;q<l;q++){let $=t[q]-(m+u*e[q]),_=e[q]+n*u*$/y,P=t[q]-$/y;p[q]=r>1?_/r+P:_+r*P,x=Math.min(x,Math.abs(p[q]))}let M=[0,0],R=[0,0],T=[0,0];for(let q=0;q<l;q++){let $=x/p[q],_=[$*$,0];M=he(M,_),R=he(R,_e(_,[e[q],0])),T=he(T,_e(_,[t[q],0]))}let S=ft(R,M[0]+M[1]),A=ft(T,M[0]+M[1]),C=[0,0],L=[0,0],k=[0,0];for(let q=0;q<l;q++){let $=ft(Le([e[q],0],S),p[q]),_=ft(Le([t[q],0],A),p[q]);C=he(C,_e($,$)),L=he(L,_e(_,_)),k=he(k,_e($,_))}let F=mh(C[0],L[0],k[0],n),I=Le(A,_e([F,0],S))[0];h=S[0];let O=V(Math.abs(u-F),a-c)<i&&V(Math.abs(m-I),a)<i;if(m=I,u=F,O){w=g+1;break}}let[d,b]=_c(o),[N,v]=_c(1+o),E=p.map(g=>{if(g===0)return 1/0;let[y,x]=_c(Math.abs(g));return r>1?Ve((N/(d*y))**2,2*(v-b-x-a)):Ve((N/y)**2,2*(v-x-c))});return{b0:m,b1:u,xmean:h,weights:E,converged:w<s}}function _c(e){let t=Math.floor(Math.log2(e));return[Ve(e,-t),t]}var so=e=>Math.atan(e),oo=e=>Number.isFinite(e)?e:Math.tan(so(e));function nv(e,t){let n=e.length,r=0;for(let m=0;m<n;m++)for(let u=m+1;u<n;u++)r+=Math.sign(e[u]-e[m])*Math.sign(t[u]-t[m]);let o=r>=0,s=new Float64Array(n*(n-1)/2),i=0,c=0,a=0,l=Math.PI/4;for(let m=0;m<n;m++)for(let u=m+1;u<n;u++){let h=Cc(e[u],e[m]),p=Cc(t[u],t[m]),w;if(h!==0)w=p/h;else if(p!==0)w=o?1/0:-1/0;else continue;s[i++]=w;let d=so(w);o?d===-l?c++:d<-l&&a++:d===l?c++:d>l&&a++}return{sorted:s.subarray(0,i).sort(),offset:o?2*a+c:-(2*a+c),e:c}}function rv(e,t,n){let r=e.length;if(t%2!==0){let c=(t+1)/2;return c>=1&&c<=r?oo(e[c-1]):NaN}let o=t/2;if(o<1||o+1>r)return NaN;let s=e[o-1],i=e[o];return n?(oo(s)+oo(i))/2:Math.tan((so(s)+so(i))/2)}function ov(e){let t=Float64Array.from(e).sort(),n=t.length>>1;return t.length%2===1?t[n]:(t[n-1]+t[n])/2}function Oc(e,t,n){return Number.isFinite(n)?ov(e.map((r,o)=>Cc(t[o],n*r))):-n}function lh(e,t,n){let r=nv(e,t),o=rv(r.sorted,r.sorted.length+r.offset,n);return{b0:Number.isNaN(o)?NaN:Oc(e,t,o),b1:o,slopes:r}}function sv(e,t,n,r,o){let s=e.length,{sorted:i,offset:c,e:a}=n,l=i.length,f=Math.round(r*Math.sqrt(s*(s-1)*(2*s+5)/18)),m=(p,w)=>{let d=Math.trunc((p+1)/2);if(w?d<2*a+1:d>l)return NaN;if(p%2===0){if(d+1>l)return NaN;let b=i[d-1],N=i[d];return o?(oo(b)+oo(N))/2:Math.tan((so(b)+so(N))/2)}return oo(i[d-1])},u=m(l-f+c,!0),h=m(l+f+c,!1);return Number.isNaN(u)||Number.isNaN(h)?{slope:[-1/0,1/0],intercept:[-1/0,1/0],defined:!1}:{slope:[u,h],intercept:[Oc(e,t,h),Oc(e,t,u)],defined:!0}}function av(e,t,n,r){let o=e.length,s=e.map((p,w)=>t[w]-n-r*p),i=s.filter(p=>p>0).length,c=s.filter(p=>p<0).length,a=Math.sqrt(c/i),l=-Math.sqrt(i/c),f=e.map((p,w)=>(t[w]+p/r-n)/Math.sqrt(1+1/(r*r))),m=Array.from({length:o},(p,w)=>w).sort((p,w)=>f[p]-f[w]||p-w),u=0,h=0;for(let p of m){let w=s[p];u+=w>0?a:w<0?l:0,h=Math.max(h,Math.abs(u))}return{nPos:i,nNeg:c,maxCusum:h}}var wa=e=>e.every(t=>t===e[0]);function Lc(e,t){if(!(typeof e=="number"&&Number.isInteger(e)&&e>=1))throw new RangeError(`'${t}' must be a positive integer`);return e}var ro=(e,t)=>Ss(e,t),fh=e=>Zn(e.filter(t=>!Number.isNaN(t)));function iv(e,t,n={}){B(n,Xb,"mcreg");let r=Se(n.methodReg,Qb),o=Se(n.methodCi??(r==="WDeming"?"jackknife":"analytical"),Zb),s=o==="bootstrap"||o==="nestedbootstrap",i=Se(n.methodBootstrapCi,Jb),c=n.confLevel??.95;if(!(typeof c=="number"&&c>0&&c<1))throw new RangeError("'confLevel' must be a single number in (0, 1)");let l=Se(n.slopeMeasure,["radian","tangent"])==="tangent",f=r!=="PaBa",m=f?n.errorRatio??1:1;if(f&&!(typeof m=="number"&&m>0&&m<1/0))throw new RangeError("'errorRatio' must be a positive finite number");let u=Lc(n.nsamples??999,"nsamples"),h=Lc(n.nnested??25,"nnested"),p=Lc(n.iterMax??30,"iterMax"),w=n.threshold??1e-6;if(!(typeof w=="number"&&w>=0&&w<1/0))throw new RangeError("'threshold' must be a nonnegative number");if(r==="WDeming"&&o==="analytical")throw new RangeError('there is no analytical interval for weighted Deming regression (use methodCi "jackknife" or a bootstrap)');if(o==="bootstrap"&&i==="tBoot"&&r!=="Deming")throw new RangeError(`the bootstrap-t interval of ${r} needs methodCi "nestedbootstrap" (there is no analytical standard error)`);if(e.length!==t.length)throw new TypeError("'x' and 'y' must have the same length");let d=[],b=[];for(let ne=0;ne<e.length;ne++){let D=e[ne],Y=t[ne];if(typeof D!="number"||typeof Y!="number")throw new TypeError("'x' and 'y' must be numeric");if(Number.isNaN(D)||Number.isNaN(Y)){if(n.naRm===!0)continue;throw new RangeError("'x' or 'y' has missing values (use naRm: true to drop the incomplete pairs)")}if(!Number.isFinite(D)||!Number.isFinite(Y))throw new RangeError("'x' and 'y' must not contain infinite values");d.push(D),b.push(Y)}let N=d.length;if(N<=2)throw new RangeError("mcreg needs at least 3 complete pairs");if(wa(d)||wa(b))throw new RangeError("'x' and 'y' must not be constant");if(r==="PaBa"){if(d.some(D=>D<0)||b.some(D=>D<0))throw new RangeError("Passing-Bablok regression needs nonnegative values");if(N>ih)throw new RangeError(`Passing-Bablok regression is limited to ${ih} points (2^24 pairwise slopes)`);if((1+(o==="jackknife"||s&&i==="BCa"?N:0)+(s?u*(1+(o==="nestedbootstrap"?h:0)):0))*(N*(N-1)/2)>ev)throw new RangeError("the Passing-Bablok fits would take more than 2^27 pairwise slopes in all (reduce nsamples or nnested)")}if(r==="WDeming"&&(d.some(ne=>ne<=0)||b.some(ne=>ne<=0)))throw new RangeError("weighted Deming regression needs positive values");let v=qe([d,b]),[E,g]=f?[qe([d]).k,qe([b]).k]:[v.k,v.k],y=f?d.map(ne=>V(ne,-E)):v.groups[0],x=f?b.map(ne=>V(ne,-g)):v.groups[1],M=Math.min(Math.max(Ve(m,2*(g-E)),Number.MIN_VALUE),2**1e3),R=Ve(m,g-E),T=new Set,S=`no konvergence after ${p} iterations`,A=(ne,D)=>{if(r==="Deming")return Fc(ne,D,M);if(r==="WDeming"){let Y=ch(ne,D,M,R,m,p,w,E,g);return Y.converged||T.add(S),Y}return lh(ne,D,l)},C,L=NaN,k,F,I;if(r==="Deming"){let ne=Fc(y,x,M);C=ne,L=V(ne.xmean,E),F=[ne.se0,ne.se1]}else if(r==="WDeming"){let ne=ch(y,x,M,R,m,p,w,E,g);ne.converged||T.add(S),C=ne,L=V(ne.xmean,E),k=ne.weights}else{let ne=lh(y,x,l);C=ne,I=ne.slopes}if(!Number.isFinite(C.b1))throw new RangeError(r==="PaBa"?"the Passing-Bablok slope is not defined for these data":"the covariance of x and y is 0: the Deming slope is not defined");let O=1-c,q=gt(O/2,N-2,!1,!1),$=le(O/2,{lowerTail:!1}),_=[C.b0,C.b1],P=[NaN,NaN],K=[[NaN,NaN],[NaN,NaN]],X,re,z,J=()=>{let ne=[],D=[];for(let Y=0;Y<N;Y++){let j=A(y.filter((U,W)=>W!==Y),x.filter((U,W)=>W!==Y));ne.push(j.b0),D.push(j.b1)}X=[ne,D],re=[(N-1)*Zn(ne)/Math.sqrt(N),(N-1)*Zn(D)/Math.sqrt(N)]};if(o==="analytical")if(r==="Deming")for(let ne of[0,1])P[ne]=F[ne],K[ne]=[_[ne]-q*P[ne],_[ne]+q*P[ne]];else{let ne=sv(y,x,I,$,l);ne.defined||T.add("the Passing-Bablok interval is not defined for so few points: its limits are -Inf and Inf"),K[0]=ne.intercept,K[1]=ne.slope}else if(o==="jackknife"){J();for(let ne of[0,1])P[ne]=re[ne],K[ne]=[_[ne]-q*P[ne],_[ne]+q*P[ne]]}else{if(i==="BCa"&&(J(),X.some(de=>de.some(Number.isNaN))))throw new RangeError("Too many identical data points!");let ne=r==="Deming"?F:r==="WDeming"&&re!==void 0?re:[NaN,NaN],D=o==="nestedbootstrap",Y=[],j=[],U=[],W=[],te=0,fe=()=>Array.from({length:N},()=>vo(N)),Re=de=>{let Fe=de.map(Pe=>y[Pe]),xe=de.map(Pe=>x[Pe]);if(wa(Fe)||wa(xe)){if(r==="PaBa")return T.add("Resampling PaBa regression: x or y coordinates of all data points in subsample identical - regression coefficients undetermined!"),{b0:NaN,b1:NaN};T.add("Resampling Deming regression: all x coordinates in subsample identical - regression coefficients undetermined!")}return A(Fe,xe)},Ce=()=>{for(let de=0;de<u;de++){let Fe=fe(),xe=Re(Fe),Pe;if(D){let Oe=[],Ke=[];for(let ze=0;ze<h;ze++){let et=Re(fe().map(Ue=>Fe[Ue]));Oe.push(et.b0),Ke.push(et.b1)}Pe=[fh(Oe),fh(Ke)]}else r==="Deming"?Pe=[xe.se0,xe.se1]:Pe=ne;if(Number.isNaN(xe.b0)||Number.isNaN(xe.b1)){te++;continue}Y.push(xe.b0),j.push(xe.b1),U.push(Pe[0]),W.push(Pe[1])}};if(n.rngSeed!==void 0){let de=vl()?Ml():void 0;try{yl({kind:"Mersenne-Twister"}),Nl(n.rngSeed),Ce()}finally{de!==void 0&&El(de)}}else Ce();if(te>.05*u)throw new RangeError("There were too many resamples with undetermined regression coefficients!");let be=Y.length,me=[Y,j],ke=[U,W];for(let de of[0,1]){let Fe=me[de],xe=_[de];if(i==="quantile")K[de]=[ro(Fe,O/2),ro(Fe,1-O/2)];else if(i==="Student")P[de]=Zn(Fe),K[de]=[xe-q*P[de],xe+q*P[de]];else if(i==="BCa"){let Pe=Fe.filter(ce=>ce<xe).length;Fe.every(ce=>ce===xe)?Pe=be/2:Pe===0?Pe=.5:Pe===be&&(Pe=be-.5);let Oe=le(Pe/be),Ke=X[de],ze=Ke.reduce((ce,tt)=>ce+tt,0)/Ke.length,et=0,Ue=0;for(let ce of Ke){let tt=ze-ce;et+=tt*tt,Ue+=tt*tt*tt}let Xe=et===0?0:Ue/(6*et**1.5),pe=-$,oe=$,we=se(Oe-(pe-Oe)/(1+Xe*(pe-Oe))),ge=se(Oe-(oe-Oe)/(1+Xe*(oe-Oe)));K[de]=[ro(Fe,ge),ro(Fe,we)]}else{let Pe=Fe.map((Ke,ze)=>(Ke-xe)/ke[de][ze]),Oe=Number.isNaN(ne[de])?Zn(Fe):ne[de];P[de]=Oe,K[de]=[xe-ro(Pe,1-O/2)*Oe,xe-ro(Pe,O/2)*Oe]}}z={intercept:Y.map(de=>V(de,g)),slope:j.map(de=>V(de,g-E)),sigmaIntercept:U.map(de=>V(de,g)),sigmaSlope:W.map(de=>V(de,g-E)),globalSigma:[V(ne[0],g),V(ne[1],g-E)],nsamples:be}}let Z=ne=>{let D=Y=>V(Y,ne===0?g:g-E);return{estimate:D(_[ne]),se:D(P[ne]),confInt:[D(K[ne][0]),D(K[ne][1])]}},Ae={methodReg:r,methodCi:o,...s?{methodBootstrapCi:i}:{},confLevel:c,errorRatio:m,n:N,intercept:Z(0),slope:Z(1),xmean:L,...k===void 0?{}:{weights:k},...X===void 0?{}:{jackknife:{intercept:X[0].map(ne=>V(ne,g)),slope:X[1].map(ne=>V(ne,g-E))}},...z===void 0?{}:{bootstrap:z},...r==="PaBa"?{cusum:av(y,x,C.b0,C.b1)}:{},warnings:[...T]};return Q("mcreg",Ae)}var cv=["alpha","confLevel","lmer"],lv={ICC1:"Single_raters_absolute",ICC2:"Single_random_raters",ICC3:"Single_fixed_raters",ICC1k:"Average_raters_absolute",ICC2k:"Average_random_raters",ICC3k:"Average_fixed_raters"};function ba(e,t){let n=e-t,r=n-e;return[n,e-(n-r)+(-t-r)]}function fv(e){let t=e[0];return e.every(n=>n.every((r,o)=>{let[s,i]=ba(r,n[0]),[c,a]=ba(t[o],t[0]);return s===c&&i===a}))}var uh=(e,t)=>e.every((n,r)=>n===t[r]);function Lr(e){let t=[0,0];for(let n of e)t=he(t,n);return ft(t,e.length)}var ao=(e,t)=>({v:ft(e.v,t),e:e.e});function io(...e){let t=e.filter(r=>r.v[0]!==0),n=t.length===0?0:Math.max(...t.map(r=>r.e));return e.map(r=>({at:Ve(r.v[0],r.e-n),E:n}))}function hh(e,t){if(t.v[0]===0)return e;if(e.v[0]===0)return t;let n=Math.max(e.e,t.e),r=o=>[Ve(o.v[0],o.e-n),Ve(o.v[1],o.e-n)];return{v:he(r(e),r(t)),e:n}}var ph=(e,t)=>Ve(e.v[0]/t.v[0],e.e-t.e),mv=(e,t)=>{let[n,r]=io(e,t);return n.at<r.at},uv=(e,t)=>{let[n,r]=io(e,t);return n.at<=r.at};function dh(e,t,n){let r=[0,0];for(let o of e){let s=Le(o,t);r=he(r,_e(s,s))}return _e(r,[n,0])}function hv(e,t={}){B(t,cv,"ICC");let n=t.lmer??!0;if(typeof n!="boolean")throw new TypeError("'lmer' must be true or false");let r,o;if(t.alpha!==void 0){if(r=t.alpha,!(typeof r=="number"&&r>0&&r<1))throw new RangeError("'alpha' must be a single number in (0, 1)");o=1-r}else o=t.confLevel??.95,_t(o),r=1-o;if(!Array.isArray(e)||e.length===0||!e.every(ee=>Array.isArray(ee)))throw new TypeError("'x' must be a matrix (an array of rows)");let s=e[0].length;if(e.some(ee=>ee.length!==s))throw new TypeError("'x' must be a matrix (rows of equal length)");if(e.some(ee=>ee.some(ve=>typeof ve!="number")))throw new TypeError("'x' must contain numbers");if(e.some(ee=>ee.some(Number.isNaN)))throw new RangeError("'x' contains missing ratings (only complete data are supported)");if(e.some(ee=>ee.some(ve=>!Number.isFinite(ve))))throw new RangeError("'x' contains infinite ratings");let i=e.length;if(i<2||s<2)throw new RangeError("at least 2 subjects and 2 raters are needed");let{groups:c}=qe(e);if(c.every(ee=>uh(ee,c[0])))throw new RangeError(c[0].every(ee=>ee===c[0][0])?"all ratings are equal":"every subject has the same ratings: the coefficients are undefined");let a=Array.from({length:s},(ee,ve)=>c.map(ie=>ie[ve])),l=0;for(let ee of e)for(let ve of ee)l=Math.max(l,Math.abs(ve));let f=l>2**1020?3:0,m=f>0?e.map(ee=>ee.map(ve=>ve/8)):e,u=m.map(ee=>ee.map((ve,ie)=>ba(ve,m[0][ie]))),h=m.map(ee=>ee.map(ve=>ba(ve,ee[0]))),p=h.map(ee=>ee.map((ve,ie)=>Le(ve,h[0][ie]))),w=ee=>{let ve=0;for(let ie of ee)for(let Te of ie)ve=Math.max(ve,Math.abs(Te[0]));return ve===0?0:Math.floor(Math.log2(ve))},d=(ee,ve)=>ee.map(ie=>ie.map(Te=>[V(Te[0],-ve),V(Te[1],-ve)])),[b,N,v]=[w(u),w(h),w(p)],[E,g,y]=[d(u,b),d(h,N),d(p,v)],x=ee=>Array.from({length:s},(ve,ie)=>ee.map(Te=>Te[ie])),M=dh(E.map(Lr),Lr(E.flat()),s),R=a.every(ee=>uh(ee,a[0]))?[0,0]:dh(x(g).map(Lr),Lr(g.flat()),i),T=[0,0];if(!fv(c)){let ee=y.map(Lr),ve=x(y).map(Lr),ie=Lr(y.flat());y.forEach((Te,Be)=>Te.forEach(($e,st)=>{let mt=he(Le(Le($e,ee[Be]),ve[st]),ie);T=he(T,_e(mt,mt))}))}let S=(ee,ve)=>({v:ee,e:2*(ve+f)}),A=S(M,b),C=S(R,N),L=S(T,v),k=i-1,F=s-1,I=(i-1)*(s-1),O=ao(A,k),q=ao(C,F),$=ao(L,I),_=ao(hh(C,L),F+I),P=[];if(n){let ee={B:{ss:A,df:k,ms:O,pooled:!1},J:{ss:C,df:F,ms:q,pooled:!1}},ve=L,ie=I;for(let Be of uv(O,q)?["B","J"]:["J","B"]){let $e=ee[Be];mv($e.ms,ao(ve,ie))&&(ve=hh(ve,$e.ss),ie+=$e.df,$e.pooled=!0)}let Te=ao(ve,ie);if(ee.B.pooled&&(O=Te),ee.J.pooled&&(q=Te),$=Te,ee.B.pooled){let[Be,$e]=io($,q);_={v:[Be.at+($e.at-Be.at)/i,0],e:Be.E}}}else T[0]===0&&P.push("essentially perfect fit for \u20181\u2019: summary may be unreliable");let K=(ee,ve)=>r/2<1e-8?un(r/2,ee,ve,!1,!1):un(1-r/2,ee,ve,!0,!1),X=(ee,ve,ie)=>ht(ee,ve,ie,!1,!1),re=ee=>ee===1/0?1:(ee-1)/(ee+s-1),z=ee=>1-1/ee,J=i*(s-1),Z=ph(O,_),Ae=ph(O,$),[ne,D]=[Z/K(k,J),Z*K(J,k)],[Y,j]=[Ae/K(k,I),Ae*K(I,k)],[U,W]=io(O,_).map(ee=>ee.at),[te,fe]=io(O,$).map(ee=>ee.at),[Re,Ce,be]=io(O,q,$).map(ee=>ee.at),me=(U-W)/(U+(s-1)*W),ke=(Re-be)/(Re+(s-1)*be+s*(Ce-be)/i),de=(te-fe)/(te+(s-1)*fe),Fe=s*ke*Ce,xe=(i*(1+(s-1)*ke)-s*ke)*be,Pe=Math.max(Math.abs(Fe),Math.abs(xe)),Oe=Pe>0&&Number.isFinite(Pe)?Math.floor(Math.log2(Pe)):0,Ke=V(Fe,-Oe),ze=V(xe,-Oe),et=(i-1)*Ke*Ke+ze*ze,Ue=et===0?s-1:(s-1)*(i-1)*(Ke+ze)**2/et,Xe=K(k,Ue),pe=K(Ue,k),oe=s*Ce+(s*i-s-i)*be,we=(ee,ve)=>ee>.5?1-(i*be+oe)/ve:ee,ge=we(i*(Re/Xe-be)/(oe+i*Re/Xe),oe+i*Re/Xe),ce=pe>1?we(i*(Re-be/pe)/(oe/pe+i*Re),oe+i*pe*Re):we(i*(pe*Re-be)/(oe+i*pe*Re),oe+i*pe*Re),tt=ee=>ee*s/(1+ee*(s-1)),G=X(Z,k,J),ue=X(Ae,k,I),Me=(ee,ve,ie,Te,Be,$e,st)=>({type:ee,name:lv[ee],ICC:ve,F:ie,df1:k,df2:Te,pValue:Be,lower:$e,upper:st}),Ne=[Me("ICC1",me,Z,J,G,re(ne),re(D)),Me("ICC2",ke,Ae,I,ue,ge,ce),Me("ICC3",de,Ae,I,ue,re(Y),re(j)),Me("ICC1k",(U-W)/U,Z,J,G,z(ne),z(D)),Me("ICC2k",(Re-be)/(Re+(Ce-be)/i),Ae,I,ue,tt(ge),tt(ce)),Me("ICC3k",(te-fe)/te,Ae,I,ue,z(Y),z(j))],Ee=ee=>Ve(ee.v[0],ee.e);return Q("ICC",{results:Ne,MSB:Ee(O),MSJ:Ee(q),MSE:Ee($),MSW:Ee(_),n:i,k:s,confLevel:o,lmer:n,warnings:P})}function va(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(!Wl(Cl(e)))throw new RangeError("'covmat' must be symmetric")}var pv=["covmat","nObs","rotation","scores","control"],dv=["lower","start"];function At(e){let t=0,n=0;for(let r of e){let[o,s]=Ot(t,r);t=o,n+=s}return t+n}var Dc=(e,t)=>e[0].map((n,r)=>At(e.map(o=>t(o[r]))));function bh(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 gh(e,t,n){let r=Bt(bh(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 wh(e,t){let n=Dc(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=Dc(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 In=(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})),co=e=>e[0].map((t,n)=>e.map(r=>r[n]));function Na(e){let t=e[0].length,n=e.map(m=>Math.sqrt(At(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=co(o),a=0;for(let m=0;m<1e3;m++){let u=In(o,i),h=u[0].map((N,v)=>{let E=0;for(let g=0;g<s;g++)E+=u[g][v]*u[g][v];return E}),p=u.map(N=>N.map((v,E)=>v**3-v*h[E]/s)),w=In(c,p),d=Rs(w);i=In(d.u,co(d.v));let b=a;if(a=At(d.d),a<b*(1+1e-5))break}let l=In(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 gv(e){let t=Na(e),n=t.loadings,r=n.map(c=>c.map(a=>a*Math.abs(a)**3)),o=Ms(Lt(n),r),s=nt(In(co(o),o)).map((c,a)=>c[a]),i=o.map(c=>c.map((a,l)=>a*Math.sqrt(s[l])));return{loadings:In(n,i),rotmat:In(t.rotmat,i)}}function wv(e,t,n={}){B(n,pv,"factanal"),B(Fn(n,"control"),dv,"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");va(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=Ts(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,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()??nt(f).map((x,M)=>(1-c/(2*i))/x[M]),h=x=>{let M=Bt(bh(f,x),{symmetric:!0,onlyValues:!0}).values.slice(c);return-(At(M.map(R=>Math.log(R)-R))-c+i)},p=x=>{let M=gh(f,x,c);return x.map((R,T)=>{let S=0;for(let A=0;A<c;A++)S+=M[T][A]*M[T][A];return(S+R-f[T][T])/(R*R)})},w=ot(()=>To(u,h,{gr:p,method:"L-BFGS-B",lower:m,upper:1,control:{fnscale:1,parscale:new Array(i).fill(.01)}}));if(w.convergence!==0)throw new Error("unable to optimize from this starting value");let d=w.par,b=wh(gh(f,d,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"?Na(b):gv(b),M=wh(x.loadings,x.rotmat);b=M.L,N=M.rot}let E=NaN,g=NaN;!Number.isNaN(s)&&a>0&&(E=(s-1-(2*i+5)/6-2*c/3)*w.value,g=It(E,a,{lowerTail:!1}));let y={uniquenesses:d,loadings:b,STATISTIC:E,dof:a,PVAL:g,converged:!0,objective:w.value,counts:w.counts,warnings:[]};return r!=="none"&&(y.scores=bv(e,f,d,b,r)),N!==void 0&&(y.rotmat=N),y}function bv(e,t,n,r,o){let s=e.length,i=e[0].map((m,u)=>At(e.map(h=>h[u]))/s),c=e.map(m=>m.map((u,h)=>u-i[h])),a=Dc(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 In(l,nt(t,r));let f=r.map((m,u)=>m.map(h=>h/n[u]));return In(l,co(nt(In(co(r),f),co(f))))}var vv=["nfactors","covmat"],lo=2**-52,Nv=`
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
- `,lo=(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})),Zo=e=>e[0].map((t,n)=>e.map(r=>r[n])),Jo=(e,t=n=>n)=>e[0].map((n,r)=>Tt(e.map(o=>t(o[r])))),gh=(e,t=n=>n)=>e.map(n=>Tt(n.map(t))),fo=(e,t=n=>n)=>Tt(e[0].flatMap((n,r)=>e.map(o=>t(o[r])))),va=e=>e<0?-1:1;function Oc(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 wh(e,t){let n=Oc(e),{d:r,u:o,v:s}=ys(n),i=r.map((a,l)=>l).filter(a=>r[a]>Math.sqrt(co)*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)&&hr(t,"In smc, smcs > 1 were set to 1.0"),c.some(a=>a<0)&&hr(t,"In smc, smcs < 0 were set to .0"),c.map(a=>a>1?1:a<0?0:a)}var hr=(e,t)=>{e.includes(t)||e.push(t)};function Cc(e,t,n,r){let o=e.map((c,a)=>c.map((l,f)=>a===f?t[a]:l)),s=Wt(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 hv=e=>lo(e,Zo(e));function dh(e,t,n,r){let o=Oc(e),s=o.length,i=wh(o,r),c=d=>{let b=Cc(o,d.map(M=>1-M),t,M=>M<co?100*co:M),y=hv(b),v=[];for(let M=0;M<s;M++)for(let g=M+1;g<s;g++)v.push((o[g][M]-y[g][M])**2);return Tt(v)},a=d=>{let b=Cc(o,d.map((y,v)=>o[v][v]-y),t,y=>Math.max(y,0));return d.map((y,v)=>{let M=0;for(let g=0;g<t;g++)M+=b[v][g]*b[v][g];return M+y-o[v][v]})},l=d=>{let b=Wt(o.map((T,A)=>T.map((S,C)=>A===C?1-d[A]:S)),{symmetric:!0}),y=b.values,v=b.vectors,M=y.map(T=>T>=co),g=y.map(T=>T<co?100*co:T),N=o.map((T,A)=>T.map((S,C)=>{if(A===C)return 0;let L=0;for(let k=0;k<t;k++)L+=g[k]*v[A][k]*v[C][k];return S-L})),x=lo(N,v),E=(T,A)=>{let S=0;for(let C=0;C<s;C++)S+=v[C][T]*x[C][A];return S},R=new Array(s).fill(0);for(let T=0;T<t;T++){let A=(S,C)=>{if(C===0)return;let L=C*E(T,S);for(let k=0;k<s;k++)R[k]+=L*v[k][T]*v[k][S]};M[T]&&A(T,1);for(let S=T+1;S<t;S++)A(S,2*(M[T]&&M[S]?1:(g[T]-g[S])/(y[T]-y[S])));for(let S=t;S<s;S++)A(S,2*g[T]/(y[T]-y[S]))}return R},f=.005,m={method:"L-BFGS-B",lower:f,upper:Math.max(Nt(i),1)},u=new Array(s).fill(.01),h=rt(()=>Ro(i.map(d=>1-d),c,{gr:a,...m,control:{fnscale:1,parscale:u}}));h.par.some(d=>d===f)&&(h=rt(()=>Ro(h.par,c,{gr:l,...m,control:{fnscale:1,parscale:u,factr:1e3}}))),h.convergence!==0&&hr(n,`minres did not converge (optim convergence code ${h.convergence}): examine the results carefully`);let p=h.par,w=Cc(o,p.map((d,b)=>o[b][b]-d),t,d=>Math.max(d,0));if(w=Fc(w,t),w=w.map(d=>d.map(b=>b===0?1e-15:b)),gh(w,d=>d*d).some(d=>d>1)&&hr(n,"An ultra-Heywood case was detected. Examine the results carefully"),t>1){w=Fc(ba(w).loadings,t);let d=Jo(w,y=>y*y),b=d.map((y,v)=>v).sort((y,v)=>d[v]-d[y]||y-v);w=w.map(y=>b.map(v=>y[v]))}else w=Fc(w,t);return w}function Fc(e,t){let n=Jo(e).map(va);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 pv(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:fo(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 dv(e){let t=e[0].length,n=l=>{let f=nt(l),m=lo(e,Zo(f)),{f:u,Gq:h}=pv(m),p=Zo(lo(lo(Zo(m),h),f)).map(w=>w.map(d=>-d));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=Jo(r.map((d,b)=>d.map((y,v)=>y*s.G[b][v]))),m=s.G.map((d,b)=>d.map((y,v)=>y-r[b][v]*f[v])),u=Math.sqrt(fo(m,d=>d*d));if(c.push(s.f),u<1e-5){a=!0;break}if(i!==void 0){let d=i,b=r.map((v,M)=>v.map((g,N)=>g-d.T[M][N])),y=m.map((v,M)=>v.map((g,N)=>g-d.Gp[M][N]));fo(y,v=>v*v)>0&&(o=fo(b,v=>v*v)/Math.abs(fo(b.map((v,M)=>v.map((g,N)=>g*y[M][N])))),o=Math.max(1e-10,Math.min(o,20)))}else o=2*o;let h=Nt(c.slice(-10)),p=s,w=r;for(let d=0;d<=10;d++){let b=r.map((v,M)=>v.map((g,N)=>g-o*m[M][N])),y=Jo(b,v=>v*v).map(v=>1/Math.sqrt(v));if(w=b.map(v=>v.map((M,g)=>M*y[g])),p=n(w),h-p.f>.5*u*u*o)break;o=o/2}i={T:r,Gp:m},r=w,s=p}return{loadings:s.L,phi:lo(Zo(r),r),converged:a}}function gv(e,t={}){B(t,mv,"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)wa(t.covmat),r=Oc(t.covmat);else{if(e===null)throw new TypeError("neither 'x' nor 'covmat' supplied");r=xo(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=dh(r,n,s,i),a,l,f;if(n===1)a=c.map(_=>_.slice()),f=[1],hr(i,`Omega_h for 1 factor is not meaningful, just omega_t
19
- `),hr(s,"Omega_h and Omega_asymptotic are not meaningful with one factor");else{let _=dv(c);if(_.converged||hr(s,"oblimin did not converge in 2000 iterations"),a=_.loadings,l=_.phi,n===2){let P=l[0][1],K=Math.sqrt(Math.abs(P));f=[K,va(P)*K],hr(i,uv)}else f=dh(l,1,s,i).map(P=>P[0])}let m=a.map(_=>{let P=0;for(let K=0;K<n;K++)P+=_[K]*f[K];return P}),u=gh(c,_=>_*_),h=u.map((_,P)=>r[P][P]-_),p=a.map((_,P)=>_.map((K,X)=>{if(n===1)return 0;let re=r[P][P],z=re-(re-K*K)-(K*f[X])**2;return z<0&&(z=0),va(K)*Math.sqrt(z)})),w=m.map((_,P)=>_*_/u[P]),d=m.map((_,P)=>{let K=[_,...p[P]];return Tt(K.map(X=>X*X))**2/Tt(K.map(X=>X**4))}),b=m.map(va);b.some(_=>_<0)&&(r=r.map((_,P)=>_.map((K,X)=>b[P]*K*b[X])));let y=m.map((_,P)=>b[P]*_),v=p.map((_,P)=>_.map(K=>b[P]*K)),M=y.map((_,P)=>[_,...v[P],u[P],h[P],w[P],d[P]]),g=fo(r),N=Tt(r.map((_,P)=>_[P])),x=Tt(y)**2,E=Tt(h),R=x/g,T=(g-E)/g,A=x/(g-E),S=(g-N)/g*o/(o-1),C=(g+Tt(wh(r,i))-N)/g,L=Jo(M.map(_=>_.slice(0,n+1)),_=>_*_),k=L[0]/Tt(L),F=v.map(_=>{let P=0;for(let K=1;K<n;K++)Math.abs(_[K])>Math.abs(_[P])&&(P=K);return P}),I=[],O=[];for(let _=0;_<n;_++){let P=F.map((z,J)=>J).filter(z=>F[z]===_),K=Tt(P.flatMap(z=>P.map(J=>r[J][z]))),X=Tt(P.map(z=>v[z][_])),re=Tt(P.map(z=>y[z]));O.push(X*X),I.push([(X*X+re*re)/K,re*re/K,X*X/K])}let q=[[T,Tt(y)**2/g,Tt(O)/g],...I],$={omegaH:R,omegaLim:A,alpha:S,omegaTot:T,G6:C,ECV:k,sl:M,slNames:["g",...Array.from({length:n},(_,P)=>`F${P+1}*`),"h2","u2","p2","com"],orthog:c,oblique:a,gloading:f,key:b,omegaGroup:q,warnings:s,messages:i};return l!==void 0&&($.phi=l),Q("omega",$)}var bh=["ML","stdErr"],wv=["ML","stdErr"],nn=.9999;function Pc(e){let t=De(e),n=[],r=0;for(let o=0;o<e.length-1;o++)r+=e[o],n.push(le(r/t));return n}var Vc=e=>[...new Set(e)].sort((t,n)=>t-n),vh=(e,t)=>e===1?`1 ${t} with zero marginal removed`:`${e} ${t}s with zero marginals removed`,Nh=e=>Array.isArray(e)||ArrayBuffer.isView(e),yh=(e,t)=>e>0?ae(e,{lowerTail:!1})-ae(t,{lowerTail:!1}):ae(t)-ae(e);function $c(e){let t=qe([e]).groups[0],n=it(t),r=t.map(s=>s-n),o=Math.sqrt(st(r));return r.map(s=>s/o)}function Mh(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 Na=e=>Array.from({length:e},()=>new Array(e).fill(0));function Rh(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 Dc={r:0,h:0,k:0,rr:0,rh:0,rk:0,hh:0,hk:0,kk:0};function bv(e,t,n){if(e===-1/0||t===-1/0||e===1/0&&t===1/0)return Dc;if(e===1/0)return{...Dc,k:Gt(t),kk:-t*Gt(t)};if(t===1/0)return{...Dc,h:Gt(e),hh:-e*Gt(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=Gt(e),f=Gt(t);return{r:i,h:l*ae(c),k:f*ae(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*ae(c)-n/o*l*Gt(c),hk:i,kk:-t*f*ae(a)-n/o*f*Gt(a)}}function Eh(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:Na(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=>bv(h,p,r))),u=[];for(let h=0;h<s;h++)for(let p=0;p<i;p++){let w=e[h][p];if(w===0)continue;let d=wo([t[h],n[p]],[t[h+1],n[p+1]],r),b=new Array(c).fill(0),y=Na(c);for(let[v,M,g]of[[h+1,p+1,1],[h,p+1,-1],[h+1,p,-1],[h,p,1]]){let N=m[v][M],x=l(v),E=f(M);b[0]+=g*N.r,y[0][0]+=g*N.rr,x>=0&&(b[x]+=g*N.h,y[0][x]+=g*N.rh,y[x][0]+=g*N.rh,y[x][x]+=g*N.hh),E>=0&&(b[E]+=g*N.k,y[0][E]+=g*N.rk,y[E][0]+=g*N.rk,y[E][E]+=g*N.kk),x>=0&&E>=0&&(y[x][E]+=g*N.hk,y[E][x]+=g*N.hk)}Rh(a,w,d,b,y),u.push(w*Math.log(d))}return a.value=De(u),a}function vv(e,t,n,r,o){let s=o?n.length+1:1,i={value:0,gradient:new Array(s).fill(0),hessian:Na(s)},c=(1-r)*(1+r),a=Math.sqrt(c),l=c*a,f=new Array(s),m=Na(s),u=[],h=(p,w,d)=>{let b=n[p],y=(b-r*w)/a,v=Gt(y),M=(r*b-w)/l,g=1/a,N=b/l+3*r*(r*b-w)/(l*c),x=r/l;if(f[0]+=d*v*M,m[0][0]+=d*v*(N-y*M*M),o){let E=p+1;f[E]+=d*v*g;let R=d*v*(x-y*M*g);m[0][E]+=R,m[E][0]+=R,m[E][E]+=d*v*(-y*g*g)}};for(let p=0;p<e.length;p++){let w=e[p],d=t[p];f.fill(0);for(let v of m)v.fill(0);let b=d<n.length?(n[d]-r*w)/a:1/0,y=d>0?(n[d-1]-r*w)/a:-1/0;d<n.length&&h(d,w,1),d>0&&h(d-1,w,-1),Rh(i,1,yh(y,b),f,m),u.push(Math.log(yh(y,b)),Gt(w,{log:!0}))}return i.value=De(u),i}function Nv(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=nt(o,r)}catch{s=void 0}return(s===void 0||!s.every(Number.isFinite)||De(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 Th=(e,t)=>e>=t-1e-12*Math.abs(t);function xh(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=Nv(o,a);if(!s&&Math.abs(r[0])===nn&&l[0]*r[0]>0){s=!0;continue}let f=Math.abs(r[0]+l[0])>nn?(Math.sign(l[0])*nn-r[0])/l[0]:1,m=!1;for(let u=0;u<60;u++){let h=r.map((w,d)=>w+f*l[d]);f*l[0]!==0&&Math.abs(h[0])>=nn*(1-1e-15)&&(h[0]=Math.sign(h[0])*nn);let p=e(h);if(Th(p.value,o.value)){let w=Nt(h.map((d,b)=>Math.abs(d-r[b])/Math.max(1,Math.abs(r[b]))));r=h,o=p,m=w>1e-12;break}f/=2}if(!m)break}return{theta:r,at:o,bounded:s}}function Ah(e,t){let n=xh(e,[Math.max(-nn,Math.min(nn,t[0])),...t.slice(1)],!1);if(n.bounded||n.theta[0]===0)return n;let r=xh(e,[Math.sign(n.theta[0])*nn,...n.theta.slice(1)],!0);return Th(r.at.value,n.at.value)?r:n}function Sh(e,t){let n=()=>e.map(o=>o.map(()=>1/0));if(t)return n();let r;try{r=nt(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 qh=e=>`inadmissible correlation set to ${e>0?"0.9999":"-0.9999"}`;function yv(e,t,n){wt(t)&&B(t,bh,"polychor"),B(n,bh,"polychor");let r,o;if(Nh(t)){let I=e,O=t;if(I.length!==O.length)throw new RangeError("'x' and 'y' must have the same length");let q=I.map((X,re)=>!Number.isNaN(X)&&!Number.isNaN(O[re])),$=St(I.filter((X,re)=>q[re])),_=St(O.filter((X,re)=>q[re])),P=new Map(Vc($).map((X,re)=>[X,re])),K=new Map(Vc(_).map((X,re)=>[X,re]));r=Array.from({length:P.size},()=>new Array(K.size).fill(0)),$.forEach((X,re)=>r[P.get(X)][K.get(_[re])]++),o=n??{}}else{let I=e;if(!I.every(Nh))throw new TypeError("give a table (array of rows) or two vectors");let O=I[0]?.length??0;if(I.some(q=>q.length!==O))throw new TypeError("the table must have rows of equal length");if(r=I.map(q=>Array.from(q)),r.some(q=>q.some($=>!($>=0&&$<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;De(r.map(I=>De(I)))<2**1e3||({groups:r,k:a}=qe(r));let l=r.map(I=>De(I)),f=l.filter(I=>I===0).length;f>0&&(c.push(vh(f,"row")),r=r.filter((I,O)=>l[O]!==0));let m=(r[0]??[]).map((I,O)=>De(r.map(q=>q[O]))),u=m.filter(I=>I===0).length;u>0&&(c.push(vh(u,"column")),r=r.map(I=>I.filter((O,q)=>m[q]!==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 Q("polychor",{rho:NaN,warnings:c});let w=[-1/0,...Pc(r.map(I=>De(I))),1/0],d=[-1/0,...Pc(r[0].map((I,O)=>De(r.map(q=>q[O])))),1/0],y=Al(I=>{let O=Math.max(-nn,Math.min(nn,I)),q=[];for(let $=0;$<h;$++)for(let _=0;_<p;_++){if(r[$][_]===0)continue;let P=wo([w[$],d[_]],[w[$+1],d[_+1]],O);q.push(r[$][_]*Math.log(P))}return-De(q)},-nn,nn).optimum;if(!s&&!i)return Q("polychor",{rho:y,warnings:c});let v=I=>[-1/0,...I,1/0],M=w.slice(1,h),g=d.slice(1,p),N=I=>s?Eh(r,v(I.slice(1,h)),v(I.slice(h)),I[0],!0):Eh(r,w,d,I[0],!1),{theta:x,at:E,bounded:R}=Ah(N,s?[y,...M,...g]:[y]),T=x[0];if(R&&c.push(qh(T)),!i)return Q("polychor",{rho:T,warnings:c});let A=s?v(x.slice(1,h)):w,S=s?v(x.slice(h)):d,C=De(r.map(I=>De(I))),L=[],k=[];for(let I=0;I<h;I++)for(let O=0;O<p;O++){let q=r[I][O],$=wo([A[I],S[O]],[A[I+1],S[O+1]],T);if(q===0)L.push(C*$);else if(a===0)L.push(q*Math.log1p(1e-6/q),q*Mh(C*$/q-1));else{let _=V(q,a);k.push(_*Math.log1p(1e-6/_)),L.push(q*Mh(C*$/q-1))}}let F=Sh(E.hessian,R);return Q("polychor",{rho:T,warnings:c,var:a===0?F:F.map(I=>I.map(O=>V(O,-a))),chisq:a===0?2*De(L):V(2*De(L),a)+2*De(k),df:h*p-h-p,n:V(C,a),rowCuts:A.slice(1,h),colCuts:S.slice(1,p)})}function Mv(e,t,n,r){let o=e.length,s=[le(.25),le(.5),le(.75)],i=(h,p)=>p.filter(w=>w<h).length,c=$c(e).map(h=>i(h,n)),a=$c(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((w,d)=>{if(w===0)return;let b=wo([f[p],m[d]],[f[p+1],m[d+1]],r);u.push(w*Math.log((w+1e-6)/(b*o)))})),2*De(u)}function Ev(e,t,n={}){if(B(n,wv,"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,R)=>!Number.isNaN(E)&&!Number.isNaN(t[R])),i=e.filter((E,R)=>s[R]),c=St(t.filter((E,R)=>s[R])),a=i.length,l=Vc(c);if(l.length<2)return Q("polyserial",{rho:NaN,warnings:["y has fewer than 2 levels"]});let f=new Map(l.map((E,R)=>[E,R+1])),m=c.map(E=>f.get(E)),u=l.map(()=>0);for(let E of m)u[E-1]++;let h=Pc(u),p=[],w=Math.sqrt((a-1)/a)*Math.sqrt(st(m))*ql(i,m)[0]/De(h.map(E=>Gt(E)));if(Math.abs(w)>nn){let E=Math.sign(w)*nn;p.push(`initial correlation inadmissible, ${Vn(w)}, set to ${Vn(E)}`),w=E}if(!r&&!o)return Q("polyserial",{rho:w,warnings:p});let d=$c(i),b=m.map(E=>E-1),y=E=>vv(d,b,r?E.slice(1):h,E[0],r),{theta:v,at:M,bounded:g}=Ah(y,r?[w,...h]:[w]);if(g&&p.push(qh(v[0])),!o)return Q("polyserial",{rho:v[0],warnings:p});let N=r?v.slice(1):h,x=l.length;return Q("polyserial",{rho:v[0],warnings:p,var:Sh(M.hessian,g),chisq:Mv(m,d,N,r?v[0]:w),df:4*x-x-1,n:a,cuts:N})}var xv=["group","strata","rho","cencode"],Rv=new Set(["cov2","tf","failcode","cencode","gtol","maxiter","init","variance","confLevel","names"]);function ya(e){return e==null||typeof e=="number"&&Number.isNaN(e)}function zc(e){let t=wn(e),n=Array.from(new Set(t)).sort(Er);return{labels:n,index:new Map(n.map((r,o)=>[r,o]))}}function Tv(e,t,n){if(e.length!==t.length)throw new RangeError("'time' and 'status' must have the same length");if(n.group!==void 0&&n.group.length!==e.length)throw new RangeError("'group' must have the same length as 'time'");if(n.strata!==void 0&&n.strata.length!==e.length)throw new RangeError("'strata' must have the same length as 'time'")}function Av(e,t,n={}){B(n,xv,"cuminc"),Tv(e,t,n);let{group:r,strata:o,rho:s=0,cencode:i=0}=n,c=[],a=new Et;for(let g=0;g<e.length;g++)if(!(Number.isNaN(e[g])||ya(t[g])||r!==void 0&&ya(r[g])||o!==void 0&&ya(o[g]))){if(!Number.isFinite(e[g]))throw new RangeError("'time' must contain only finite values");if(e[g]<0)throw new RangeError("'time' must be non-negative");c.push(g)}let l=e.length-c.length;if(l>0&&a.add(`${l} cases omitted due to missing values`),c.length===0)throw new RangeError("no complete observations");let f=r===void 0?[1]:wn(c.map(g=>r[g])),m=o===void 0?[1]:wn(c.map(g=>o[g])),u=wn([...c.map(g=>t[g]),i]),h=zc(f);if(h.labels.length>=2&&!Number.isFinite(s))throw new RangeError("'rho' must be finite when Gray's test is requested");let p=zc(m),w=zc(u),d=u[u.length-1],b=w.labels.filter(g=>g!==d);if(b.length===0)throw new Error("there are no events of any cause");let y=c.map((g,N)=>({time:e[g],status:u[N],group:r===void 0?0:h.index.get(f[N]),strata:o===void 0?0:p.index.get(m[N]),source:g})).sort((g,N)=>g.time-N.time),v=[];for(let g of b){let N=ft(g);for(let x=0;x<h.labels.length;x++){let E=y.filter(q=>q.group===x),R=E.reduce((q,$)=>Math.max(q,$.time),-1/0),T=R>0&&R<1?2**Math.min(1023,Math.ceil(-Math.log2(R))):1,A=fr(E.map(q=>q.time*T),E.map(q=>q.status===d?0:1)),S=[0],C=[0],L=[0],k=[],F=1;for(let q=0;q<A.time.length;q++){let $=A.nEvent[q];if($===0)continue;let _=A.time[q]/T,P=A.nRisk[q],K=E.filter(X=>X.time===_&&X.status===g).length;k.push({t:_,Y:P,d:$,dk:K,sb:F,sa:A.surv[q]}),F=A.surv[q]}let I=0,O=0;for(let q=0;q<k.length;q++){let $=k[q],_=I,P=O;I+=$.sb*$.dk/$.Y;let K=0;for(let X=0;X<=q;X++){let re=k[X],z=re.Y,J=re.d,Z=re.dk,Se=z===J?0:(J*(z-J)+2*Z*(J-Z))/((z-1)*(z-J)**2),ne=z===1?Z:Z*(z-Z)/(z*z*(z-1)),D=z===J||z===1?0:Z*(z-Z)/(z*(z-J)*(z-1)),Y=0;for(let U=0;U<=X;U++){let W=k[U];Y+=W.sb*W.dk/W.Y}let j=I-Y;K+=j*j*Se+re.sb*re.sb*ne-2*j*re.sb*D}O=Math.max(0,K),$.dk>0&&(S.push($.t,$.t),C.push(_,I),L.push(P,O))}S.push(R),C.push(I),L.push(O),v.push({name:`${ft(h.labels[x])} ${N}`,group:ft(h.labels[x]),cause:N,time:S,est:C,var:L})}}let M=h.labels.length>=2?Sv(y,h.labels,b,p.labels.length,s,d):void 0;return Q("cuminc",{curves:v,...M===void 0?{}:{tests:M},warnings:a.list})}function Sv(e,t,n,r,o,s){let i=[];for(let c of n){let a=t.length,l=new Array(a).fill(0),f=Array.from({length:a},()=>new Array(a).fill(0));for(let w=0;w<r;w++){let d=e.filter(g=>g.strata===w),b=Array.from(new Set(d.filter(g=>g.status!==s).map(g=>g.time))).sort((g,N)=>g-N),y=new Array(a).fill(1),v=new Array(a).fill(0),M=[];for(let g of b){let N=new Array(a).fill(0),x=new Array(a).fill(0),E=new Array(a).fill(0);for(let q of d)q.time>=g&&N[q.group]++;for(let q of d)q.time===g&&q.status!==s&&(q.status===c?x[q.group]++:E[q.group]++);let R=y.slice(),T=v.slice(),A=N.map((q,$)=>q===0?0:q/R[$]),S=N.map((q,$)=>q===0?0:q*(1-T[$])/R[$]),C=x.reduce((q,$)=>q+$,0),L=A.reduce((q,$)=>q+$,0),k=L===0?0:C/L,F=M.length===0?1:M[M.length-1].Gafter,I=F-k,O=S.reduce((q,$)=>q+$,0);for(let q=0;q<a;q++){let $=F**o;O>0&&(l[q]+=$*(x[q]-S[q]*C/O)),N[q]>0?(y[q]=y[q]*(1-(x[q]+E[q])/N[q]),v[q]+=R[q]*x[q]/N[q]):y[q]=0}M.push({time:g,Y:N,d1:x,d2:E,Sbefore:R,Safter:y.slice(),Fbefore:T,h:A,R:S,dF0:k,Gbefore:F,Gafter:I})}for(let g=0;g<a;g++)for(let N=0;N<a;N++){let x=0;for(let E=0;E<a;E++)for(let R=0;R<M.length;R++){let T=M[R],A=T.h.reduce((J,Z)=>J+Z,0);if(!(A>0)||!(T.h[E]>0))continue;let S=T.Gbefore**o,C=S*T.h[g]*((g===E?1:0)-T.h[E]/A),L=S*T.h[N]*((N===E?1:0)-T.h[E]/A),k=0,F=0;for(let J=R+1;J<M.length;J++){let Z=M[J],Se=Z.h.reduce((U,W)=>U+W,0);if(!(Se>0))continue;let ne=Z.Gbefore**o,D=ne*Z.h[g]*((g===E?1:0)-Z.h[E]/Se),Y=ne*Z.h[N]*((N===E?1:0)-Z.h[E]/Se),j=Z.Gbefore===0?0:Z.dF0/Z.Gbefore;k+=D*j,F+=Y*j}let I=T.Safter[E]===0?0:T.Gafter/T.Safter[E],O=C+(1-I)*k,q=L+(1-I)*F,$=-I*k,_=-I*F,P=T.Y[E]===0?0:T.Sbefore[E]*T.d2[E]/T.Y[E],K=(O*q*T.dF0+$*_*P)/T.h[E],X=T.d1.reduce((J,Z)=>J+Z,0),re=0;if(X>1&&T.Y[E]>0&&T.Sbefore[E]>0){let J=1/T.Sbefore[E];A>J&&(re=X*(X-1)/A*(T.Y[E]*J*J/(A-J))*(O*q/(T.h[E]*T.h[E])))}let z=0;T.d2[E]>1&&T.Y[E]>1&&(z=(T.d2[E]-1)/(T.Y[E]-1)*(P/T.h[E])*$*_),x+=K-re-z}f[g][N]+=x}}let m=a-1,u=NaN,h=NaN,p=f.slice(0,m).map(w=>w.slice(0,m));try{let w=nt(p,l.slice(0,m),{tol:1e-12});u=l.slice(0,m).reduce((d,b,y)=>d+b*w[y],0),h=It(u,m,{lowerTail:!1})}catch{u=NaN,h=NaN}i.push({cause:ft(c),stat:u,df:m,pValue:h})}return i}function qv(e,t){let n=Array.from(new Set(t.filter(s=>!Number.isNaN(s)))).sort((s,i)=>s-i),r=[],o=[];for(let s of e.curves){let i=[],c=[];for(let a of n){let l=-1;for(let f=0;f<s.time.length;f++)s.time[f]<=a&&(l=f);a<0||l<0||a>s.time[s.time.length-1]?(i.push(NaN),c.push(NaN)):(i.push(s.est[l]),c.push(s.var[l]))}r.push(i),o.push(c)}return{curveNames:e.curves.map(s=>s.name),times:n,est:r,var:o}}function In(e,t=e){return Array.from({length:e},()=>new Array(t).fill(0))}function kv(e,t,n,r){for(let o=0;o<t.length;o++)for(let s=0;s<n.length;s++)e[o][s]+=r*t[o]*n[s]}function jc(e,t){return De(e.map((n,r)=>n*t[r]))}function Iv(e){return e.every(t=>t.every(Number.isFinite))}function _v(e,t,n,r={}){B(r,Rv,"crr");let{cov2:o,tf:s,failcode:i=1,cencode:c=0,gtol:a=1e-6,maxiter:l=10,init:f,variance:m=!0,confLevel:u=.95,names:h}=r;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("'cov1' must have one row per observation");if(o!==void 0&&o.length!==e.length)throw new RangeError("'cov2' must have one row per observation");if(o!==void 0&&s===void 0)throw new TypeError("'tf' is required when 'cov2' is supplied");if(!(a>0)||!Number.isFinite(a))throw new RangeError("'gtol' must be finite and positive");if(!Number.isInteger(l)||l<0)throw new RangeError("'maxiter' must be a non-negative integer");if(!(u>0&&u<1))throw new RangeError("'confLevel' must be between 0 and 1");let p=n?.[0]?.length??0,w=o?.[0]?.length??0,d=p+w;if(d===0)throw new RangeError("at least one covariate is required");if(n!==void 0&&n.some(W=>W.length!==p))throw new RangeError("rows of 'cov1' must have equal length");if(o!==void 0&&o.some(W=>W.length!==w))throw new RangeError("rows of 'cov2' must have equal length");if(f!==void 0&&f.length!==d)throw new RangeError("'init' must have one value per coefficient");if(h!==void 0&&h.length!==d)throw new RangeError("'names' must have one name per coefficient");let b=[];for(let W=0;W<e.length;W++)if(!(Number.isNaN(e[W])||ya(t[W])||n!==void 0&&n[W].some(Number.isNaN)||o!==void 0&&o[W].some(Number.isNaN))){if(!Number.isFinite(e[W]))throw new RangeError("'time' must contain only finite values");if(n!==void 0&&!n[W].every(Number.isFinite)||o!==void 0&&!o[W].every(Number.isFinite))throw new RangeError("covariates must be finite");b.push(W)}let y=e.length-b.length;if(b.length===0)throw new RangeError("no complete observations");let v=b.map(W=>e[W]),M=wn([...b.map(W=>t[W]),c,i]),g=M.slice(0,b.length),N=M[M.length-2],x=M[M.length-1],E=b.map(W=>n===void 0?[]:Array.from(n[W])),R=b.map(W=>o===void 0?[]:Array.from(o[W])),T=Array.from(new Set(v.filter((W,te)=>g[te]===x))).sort((W,te)=>W-te);if(T.length===0)throw new RangeError("no observations of 'failcode'");let A=w===0?[[0]]:s(T).map(W=>Array.from(W));if(w>0&&(A.length!==T.length||A.some(W=>W.length!==w)||!Iv(A)))throw new RangeError("'tf' must return a finite matrix with one row per unique failure time and one column per cov2 column");let S=T.map((W,te)=>v.map((fe,Re)=>[...E[Re],...R[Re].map((Le,be)=>Le*(A[te][be]??0))])),C=v.reduce((W,te)=>Math.max(W,Math.abs(te)),0),L=C>0&&C<1?2**Math.min(1023,Math.ceil(-Math.log2(C))):1,k=fr(v.map(W=>W*L),g.map(W=>W===N?1:0)),F=W=>{let te=W*L,fe=1;for(let Re=0;Re<k.time.length&&!(k.time[Re]>=te);Re++)fe=k.surv[Re];return fe},I=T.map(W=>v.map((te,fe)=>{if(te>=W)return 1;if(g[fe]===N||g[fe]===x)return 0;let Re=F(te);return Re===0?0:F(W)/Re}));for(let W=0;W<T.length;W++)for(let te=0;te<v.length;te++)v[te]===T[W]&&g[te]===x&&(I[W][te]=1);let O=W=>{let te=Array.from({length:d},()=>[]),fe=Array.from({length:d},()=>Array.from({length:d},()=>[])),Re=[],Le=In(T.length,d),be=[],me=[],ke=[];for(let Fe=0;Fe<T.length;Fe++){let Ke=T[Fe],ze=v.map((ie,Te)=>ie===Ke&&g[Te]===x?Te:-1).filter(ie=>ie>=0),et=ze.length,Ue=S[Fe],Xe=Ue.map(ie=>jc(W,ie)),pe=I[Fe].slice();for(let ie of ze)pe[ie]=1;let oe=Math.max(...Xe.map((ie,Te)=>pe[Te]>0?ie:-1/0));if(oe===-1/0)throw new Error("empty Fine-Gray risk set");let we=oe<500&&oe>-500,ge=we?Xe.map((ie,Te)=>pe[Te]*Math.exp(ie)):Xe.map((ie,Te)=>pe[Te]*Math.exp(ie-oe)),ce=De(ge),tt=Array.from({length:d},(ie,Te)=>De(ge.map((Be,$e)=>Be*Ue[$e][Te])));if(!(ce>0))throw new Error("empty Fine-Gray risk set");let G=tt.map(ie=>ie/ce),Me=Array.from({length:d},(ie,Te)=>{let Be=Ue[0][Te],$e=0,ot=0;for(let mt of Ue)$e=Math.max($e,Math.abs(mt[Te]-Be)),ot=Math.max(ot,Math.abs(mt[Te]));return $e>0&&ot/$e>1e6}).some(Boolean),Ne=Me?Array.from({length:d},(ie,Te)=>Ue.map(Be=>Be[Te]-Ue[0][Te])):[],Ee=Me?Ne.map(ie=>De(ie.map((Te,Be)=>ge[Be]*Te))/ce):[],ee=Me?void 0:Array.from({length:d},(ie,Te)=>Array.from({length:d},(Be,$e)=>De(ge.map((ot,mt)=>ot*Ue[mt][Te]*Ue[mt][$e]))));me.push(G),ke.push(ge.map(ie=>ie/ce));let ve=we?et/ce:et*Math.exp(-oe)/ce;be.push(ve),Re.push(De(ze.map(ie=>jc(W,Ue[ie])))-et*(we?Math.log(ce):oe+Math.log(ce)));for(let ie=0;ie<d;ie++){let Be=De(ze.map($e=>Ue[$e][ie]))-et*G[ie];te[ie].push(Be),Le[Fe][ie]=Be}for(let ie=0;ie<d;ie++)for(let Te=0;Te<d;Te++){let Be=Me?De(ge.map(($e,ot)=>$e*(Ne[ie][ot]-Ee[ie])*(Ne[Te][ot]-Ee[Te])))/ce:ee[ie][Te]/ce-G[ie]*G[Te];fe[ie][Te].push(et*Be)}}let de=In(v.length,d);for(let Fe=0;Fe<T.length;Fe++){let Ke=S[Fe],ze=ke[Fe],et=me[Fe],Ue=T[Fe];for(let Xe=0;Xe<v.length;Xe++){let pe=v[Xe]===Ue&&g[Xe]===x?1:0,oe=v.filter((ge,ce)=>ge===Ue&&g[ce]===x).length,we=pe-ze[Xe]*oe;for(let ge=0;ge<d;ge++)de[Xe][ge]+=(Ke[Xe][ge]-et[ge])*we}}let Ce=te.map(De),xe=fe.map(Fe=>Fe.map(De));return{loglik:De(Re),score:Ce,inf:xe,bfitj:be,res:Le,eta:de,means:me,riskFactors:ke,zByTime:S}},q=f===void 0?new Array(d).fill(0):Array.from(f);if(!q.every(Number.isFinite))throw new RangeError("'init' must contain finite values");let $=q.slice(),_=O($),P=!1,K=0,X=(W,te)=>Math.max(...W.score.map((fe,Re)=>Math.abs(fe)*Math.max(Math.abs(te[Re]),1)))<a*Math.max(Math.abs(W.loglik),1);for(;;){if(X(_,$)){P=!0;break}if(K>=l)break;let W=nt(_.inf,_.score,{tol:0});$=$.map((te,fe)=>te+W[fe]),K++,_=O($)}let re=nt(_.inf),z=In(d);if(m){let W=In(d),te=v.length,fe=Array.from(new Set(v)).sort((me,ke)=>me-ke),Re=fe.map(me=>{let ke=v.filter(Ce=>Ce>=me).length,de=v.filter((Ce,xe)=>Ce===me&&g[xe]===N).length;return ke===0?0:de/ke}),Le=In(te,d);for(let me=0;me<fe.length;me++){let ke=fe[me],de=new Array(d).fill(0);for(let xe=0;xe<te;xe++)if(!(!(v[xe]<ke)||g[xe]===N||g[xe]===x))for(let Pe=0;Pe<T.length;Pe++){if(T[Pe]<ke)continue;let Fe=_.riskFactors[Pe][xe],Ke=v.filter((Ue,Xe)=>Ue===T[Pe]&&g[Xe]===x).length,ze=_.zByTime[Pe][xe],et=_.means[Pe];for(let Ue=0;Ue<d;Ue++)de[Ue]+=(ze[Ue]-et[Ue])*Fe*Ke}let Ce=v.filter(xe=>xe>=ke).length;if(Ce!==0)for(let xe=0;xe<te;xe++){let Pe=v[xe]===ke&&g[xe]===N?1:0,Fe=v[xe]>=ke?Re[me]:0;for(let Ke=0;Ke<d;Ke++)Le[xe][Ke]+=de[Ke]/Ce*(Pe-Fe)}}for(let me=0;me<te;me++)kv(W,_.eta[me].map((ke,de)=>ke+Le[me][de]),_.eta[me].map((ke,de)=>ke+Le[me][de]),1);z=In(d);let be=kh(re,W);z=kh(be,re)}else z=In(d).map(W=>W.map(()=>NaN));let J=m?z.map((W,te)=>Math.sqrt(W[te])):void 0,Z=J===void 0?void 0:$.map((W,te)=>W/J[te]),Se=Z===void 0?void 0:Z.map(W=>2*ae(-Math.abs(W))),ne=m?le((1+u)/2):void 0,D=ne===void 0?void 0:$.map((W,te)=>Math.exp(W-ne*J[te])),Y=ne===void 0?void 0:$.map((W,te)=>Math.exp(W+ne*J[te])),j=m?$.map(Math.exp):void 0,U=h===void 0?[...Array.from({length:p},(W,te)=>`cov1${te+1}`),...Array.from({length:w},(W,te)=>`cov2${te+1}*tf${te+1}`)]:Array.from(h);return Q("crr",{coefficients:$,names:U,var:z,...J===void 0?{}:{se:J,z:Z,pValue:Se,lower:D,upper:Y,expCoef:j},loglik:_.loglik,loglikNull:O(new Array(d).fill(0)).loglik,logtest:{statistic:2*(_.loglik-O(new Array(d).fill(0)).loglik),df:d},score:_.score,inf:m?_.inf:In(d).map(W=>W.map(()=>NaN)),invinf:m?re:In(d).map(W=>W.map(()=>NaN)),converged:P,iterations:K,n:b.length,nMissing:y,uftime:T,bfitj:_.bfitj,res:m?_.res:null,tfs:A,warnings:[],covariateDimensions:[p,w]})}function kh(e,t){let n=In(e.length,t[0]?.length??0);for(let r=0;r<e.length;r++)for(let o=0;o<(t[0]?.length??0);o++)for(let s=0;s<t.length;s++)n[r][o]+=e[r][s]*t[s][o];return n}function Lv(e,t,n){let[r,o]=e.covariateDimensions;if(r>0&&t===void 0)throw new TypeError("'cov1' is required by this fit");if(o>0&&n===void 0)throw new TypeError("'cov2' is required by this fit");if(r>0&&t.some(a=>a.length!==r))throw new RangeError("'cov1' has the wrong number of columns");if(o>0&&n.some(a=>a.length!==o))throw new RangeError("'cov2' has the wrong number of columns");if(r>0&&o>0&&t.length!==n.length)throw new RangeError("'cov1' and 'cov2' must have the same number of rows");let s=r>0?t.length:n.length,i=o===0?e.uftime.map(()=>[0]):e.tfs;return e.uftime.map((a,l)=>{let f=[a];for(let m=0;m<s;m++){let u=0;for(let h=0;h<=l;h++){let p=[...r===0?[]:t[m],...o===0?[]:n[m].map((v,M)=>v*i[h][M])],w=e.bfitj[h],d=jc(e.coefficients,p),b=w*Math.exp(d),y=Number.isFinite(b)&&(b!==0||w===0)?b:w===0?0:Math.exp(Math.log(w)+d);u+=y}f.push(-Math.expm1(-u))}return f})}var Cv=new Set(["type","time2","dist","weights","offset","strata","scale","init","df","control","intercept","confLevel"]),Fv=["maxiter","relTolerance","tolerChol"],Ov=["type","p","newX","newStrata","newOffset","seFit"],Dv=["type","weighted"],Bc=new WeakMap,Pv=Math.log(2*Math.PI),Vv=e=>Number(e),$v=e=>e<-Math.LN2?Math.log1p(-Math.exp(e)):Math.log(-Math.expm1(e));function Dh(e,t){return t===-1/0?e:e<t?NaN:e+$v(t-e)}function zv(e,t){if(e===t)return 0;let n=Math.max(e,t),r=Math.min(e,t),o=n+Math.log(-Math.expm1(r-n));return(e>t?1:-1)*Math.exp(o)}function Ih(e,t){return t===0?0:Number.isFinite(t)?Math.sign(t)*Math.exp(e+Math.log(Math.abs(t))):Math.exp(e)*t}function jv(e,t,n){let r=t-e;return Number.isFinite(r)?r/n:t/n-e/n}function Ph(e,t,n){let r=t-e;if(Number.isFinite(r))return Math.log(r)-Math.log(n);let o=Math.max(Math.abs(e),Math.abs(t));return Math.log(o)+Math.log(t/o-e/o)-Math.log(n)}function Wv(e,t,n){let r=Ph(e,t,n),o=r>Math.log(Number.MAX_VALUE)?1/0:Math.exp(r);if(o<1e-8){let s=t-e;return en(e,t)+s*(o/24)}return o>50?t-n*r-n*Math.log1p(-Math.exp(-o)):e-n*(r-Math.log(Math.expm1(o)))}function Bv(e,t,n){let r=Ph(e,t,n);if(r<Math.log(1e-8))return r-1;let o=r>Math.log(Number.MAX_VALUE)?1/0:Math.exp(r);if(!Number.isFinite(o))return 0;let s=o>50?o+Math.log1p(-Math.exp(-o)):Math.log(Math.expm1(o)),i=r-s,c=o>50?r-Math.log1p(-Math.exp(-o)):i+o;return Dh(-Math.exp(i),-Math.exp(c))}function Kv(e){return e<-36?e:-Math.log1p(Math.exp(-e))}function Uv(e){return e>36?-e:-Math.log1p(Math.exp(e))}function _h(e,t,n){if(e===-1/0)return-1/0;if(e===1/0)return 0;if(t==="extreme"||t==="weibull"||t==="exponential"||t==="rayleigh"){let r=Math.exp(e);return r===0?e:Math.log(-Math.expm1(-r))}return t==="logistic"||t==="loglogistic"?Kv(e):t==="gaussian"||t==="loggaussian"||t==="lognormal"?ae(e,{logP:!0}):on(e,n,{logP:!0})}function Lh(e,t,n){return e===-1/0?0:e===1/0?-1/0:t==="extreme"||t==="weibull"||t==="exponential"||t==="rayleigh"?-Math.exp(e):t==="logistic"||t==="loglogistic"?Uv(e):t==="gaussian"||t==="loggaussian"||t==="lognormal"?ae(e,{lowerTail:!1,logP:!0}):on(e,n,{lowerTail:!1,logP:!0})}function Ma(e,t,n){return t==="extreme"||t==="weibull"||t==="exponential"||t==="rayleigh"?e===1/0?-1/0:e-Math.exp(e):t==="logistic"||t==="loglogistic"?-Math.abs(e)-2*Math.log1p(Math.exp(-Math.abs(e))):t==="gaussian"||t==="loggaussian"||t==="lognormal"?-.5*(e*e+Pv):fl(e,n,{log:!0})}function Wc(e,t,n){if(t==="extreme"||t==="weibull"||t==="exponential"||t==="rayleigh"){let o=Math.exp(e);return[1-o,o*o-3*o+1]}if(t==="logistic"||t==="loglogistic"){let o=Math.exp(-Math.abs(e));return[-Math.tanh(e/2),1-6*o/((1+o)*(1+o))]}if(t==="gaussian"||t==="loggaussian"||t==="lognormal")return[-e,e*e-1];let r=n+e*e;return[-(n+1)*e/r,(n+1)*((n+2)*e*e-n)/(r*r)]}function Hv(e,t,n){if(t==="extreme"||t==="weibull"||t==="exponential"||t==="rayleigh")return-Math.exp(e);if(t==="logistic"||t==="loglogistic"){let r=Math.exp(-Math.abs(e));return-2*r/((1+r)*(1+r))}return t==="gaussian"||t==="loggaussian"||t==="lognormal"?-1:-(n+1)*(n-e*e)/((n+e*e)*(n+e*e))}function Ea(e,t,n,r,o,s,i){let c=e===-1/0?-1/0:(e-r)/o,a=t===1/0?1/0:(t-r)/o;if(n===1){let[L]=Wc(a,s,i),k=Ma(a,s,i),F=Hv(a,s,i);return{g:k-Math.log(o),e:-L/o,ee:F/(o*o),t:-a*L-1,tt:a*L+a*a*F,et:(L+a*F)/o}}let l=_h(a,s,i),f=_h(c,s,i),m=Lh(c,s,i),u=Lh(a,s,i),h=n===0?m:n===2?l:Dh(c>0?m:l,c>0?u:f),w=c===-1/0?0:Math.exp(n===0&&(s==="extreme"||s==="weibull"||s==="exponential"||s==="rayleigh")?c:Ma(c,s,i)-h),d=a===1/0?0:Math.exp(Ma(a,s,i)-h),[b]=Wc(c,s,i),[y]=Wc(a,s,i),v=w===0||c===-1/0?0:w*b,M=d===0||a===1/0?0:d*y,g=c===-1/0||w===0?0:c*w,N=a===1/0||d===0?0:a*d,x=c===-1/0||v===0?0:c*c*v,E=a===1/0||M===0?0:a*a*M,R=-(d-w)/o,T=(M-v)/(o*o)-R*R,A=-(N-g),S=E-x-A*(1+A),C=((a===1/0||M===0?0:a*M)-(c===-1/0||v===0?0:c*v))/o-R*(1+A);return{g:h,e:R,ee:T,t:A,tt:S,et:C}}function Yv(e){let t=e.length,n=e.map((r,o)=>[...r,...r.map((s,i)=>o===i?1:0)]);for(let r=0;r<t;r++){let o=r;for(let i=r+1;i<t;i++)Math.abs(n[i][r])>Math.abs(n[o][r])&&(o=i);if(!Number.isFinite(n[o][r])||Math.abs(n[o][r])<=1e-13*Math.max(Number.MIN_VALUE,...e.map((i,c)=>Math.abs(i[c]??0))))throw new Error("singular information matrix");[n[r],n[o]]=[n[o],n[r]];let s=n[r][r];for(let i=0;i<2*t;i++)n[r][i]/=s;for(let i=0;i<t;i++)if(i!==r){let c=n[i][r];for(let a=0;a<2*t;a++)n[i][a]-=c*n[r][a]}}return n.map(r=>r.slice(t))}function Vh(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 $h(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 Ch(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}function es(e,t){return e.map(n=>pr(n,t))}function pr(e,t){let n=e.reduce((r,o,s)=>r+o*t[s],0);return go(e,t,n)}function zh(e,t=1e-10){let n=t>0?Math.min(65536,1/Math.sqrt(t)):65536,r=Array.from({length:e[0].length},(o,s)=>{let i=e.map(a=>a[s]),c=0;for(let a of i)c=Math.max(c,Math.abs(a));if(c>n||c>0&&c<1/n){let a=qe([i]);return{values:a.groups[0],exponent:a.k}}return{values:i,exponent:0}});return{values:e.map((o,s)=>r.map(i=>i.values[s])),exponent:r.map(o=>o.exponent)}}function Fh(e,t,n){return e<=0?-1/0:e>=1?1/0:t==="extreme"||t==="weibull"||t==="exponential"||t==="rayleigh"?Math.log(-Math.log1p(-e)):t==="logistic"||t==="loglogistic"?Math.log(e/(1-e)):t==="gaussian"||t==="loggaussian"||t==="lognormal"?le(e):jt(e,n)}function Oh(e,t,n,r,o,s,i,c,a,l,f,m,u,h,p,w=1e-10){let d=e[0].length,b=d+(m===void 0?a:0),y=zh(e,w),v=y.values,M=y.exponent,g=u.map((_,P)=>P<d?Ve(_,M[P]):_),N=(_,P)=>{let K=0,X=Array(b).fill(0),re=Array.from({length:b},()=>Array(b).fill(0)),z=Array.from({length:b},()=>Array(b).fill(0));for(let J=0;J<t.length;J++){let Z=c[J],Se=pr(v[J],_.slice(0,d))+i[J],ne=m??Math.exp(_[d+Z]),D=Ea(n[J],r[J],o[J],Se,ne,l,f),Y=s[J];K+=Y*D.g;let j=Array(b).fill(0);for(let U=0;U<d;U++)j[U]=v[J][U]*D.e;m===void 0&&(j[d+Z]=D.t);for(let U=0;U<b;U++){X[U]+=Y*j[U];for(let W=0;W<b;W++)z[U][W]+=Y*j[U]*j[W]}if(P){for(let U=0;U<d;U++){for(let W=0;W<d;W++)re[U][W]+=Y*v[J][U]*v[J][W]*D.ee;m===void 0&&(re[U][d+Z]+=Y*v[J][U]*D.et)}if(m===void 0){for(let U=0;U<d;U++)re[d+Z][U]+=Y*v[J][U]*D.et;re[d+Z][d+Z]+=Y*D.tt}}}return{ll:K,u:X,H:re,B:z}},x=_=>{let P=_.H.map(X=>X.map(re=>-re)),K=Vh(P,w);return{factor:P,rank:K}},E=_=>{let P=N(_,!0),K=x(P),X=K.rank>=0?$h(K.factor,P.u):es(Yv(P.B),P.u);return{e:P,delta:X}},R=N(g,!0),T=R.ll,A=0,S=!1,C=!1,L;try{L=E(g)}catch{L={e:R,delta:Array(b).fill(0)}}let k=g.map((_,P)=>_+L.delta[P]);if(h===0){let _=x(R),P=Ch(_.factor),K=P.map(re=>re.slice()),X=P.map((re,z)=>re[z]===0?NaN:g[z]/Math.sqrt(Math.max(0,re[z])));return{b:g.map((re,z)=>z<d?Ve(re,-M[z]):re),ll:R.ll,iter:A,V:P.map((re,z)=>re.map((J,Z)=>Ve(J,(z<d?-M[z]:0)+(Z<d?-M[Z]:0)))),VScaled:K,xExponent:M,score:R.u.map((re,z)=>z<d?Ve(re,M[z]):re),z:X,warn:S}}for(let _=1;_<=h;_++){let P=N(k,!0);if(Number.isFinite(P.ll)&&Math.abs(1-T/P.ll)<=p){g.splice(0,g.length,...k),R=P,T=P.ll,A=_,C=!0;break}if(!Number.isFinite(P.ll)||P.ll<T){let K=k.slice();k=g.map((X,re)=>X+(K[re]-X)/3);for(let X=0;X<a;X++)m===void 0&&(k[d+X]=Math.max(k[d+X],g[d+X]-1.1));A=_;continue}if(g.splice(0,g.length,...k),R=P,T=P.ll,A=_,_<h)try{let K=E(g);k=g.map((X,re)=>X+K.delta[re])}catch{k=g.slice()}else try{let K=E(g),X=g.map((z,J)=>z+K.delta[J]),re=N(X,!1);Number.isFinite(re.ll)&&re.ll>=T&&g.splice(0,g.length,...X)}catch{}}A===h&&h>=2&&!C&&(S=!0);let F=N(g,!0),I=x(F),O=Ch(I.factor),q=O.map(_=>_.slice()),$=O.map((_,P)=>_[P]===0?NaN:g[P]/Math.sqrt(Math.max(0,_[P])));return{b:g.map((_,P)=>P<d?Ve(_,-M[P]):_),ll:T,iter:A,V:O.map((_,P)=>_.map((K,X)=>Ve(K,(P<d?-M[P]:0)+(X<d?-M[X]:0)))),VScaled:q,xExponent:M,score:F.u.map((_,P)=>P<d?Ve(_,M[P]):_),z:$,warn:S}}function Gv(e,t,n=[],r={}){B(r,Cv,"survreg"),B(Cn(r,"control"),Fv,"survreg","control");let o=r.type??"right",s=r.dist??"weibull",i=r.df??4,c=r.intercept??!0,a=e.length,l=t===void 0?Array(a).fill(1):Array.from(t,Number);if(l.length!==a||n.length!==0&&n.length!==a)throw new RangeError("variable lengths differ");if(o==="counting")throw new Error("start-stop type Surv objects are not supported");if(!["right","left","interval","interval2"].includes(o))throw new TypeError("invalid survival type");if(!["extreme","logistic","gaussian","weibull","exponential","rayleigh","loggaussian","lognormal","loglogistic","t"].includes(s))throw new RangeError("'arg' should be one of \u201Cextreme\u201D, \u201Clogistic\u201D, \u201Cgaussian\u201D, \u201Cweibull\u201D, \u201Cexponential\u201D, \u201Crayleigh\u201D, \u201Cloggaussian\u201D, \u201Clognormal\u201D, \u201Cloglogistic\u201D, \u201Ct\u201D");if((o==="interval"||o==="interval2")&&r.time2===void 0)throw new TypeError("time2 is required for interval data");if(r.time2!==void 0&&r.time2.length!==a)throw new RangeError("variable lengths differ (found for 'time2')");if(r.scale!==void 0&&(!Number.isFinite(r.scale)||r.scale<0))throw new RangeError("Invalid scale value");if(s==="t"&&(!(i>=3)||!Number.isFinite(i)))throw new RangeError("Degrees of freedom must be >=3");if(s!=="t"&&r.df!==void 0)throw new RangeError("Extreme value distribution has no optional parameters");let f=["weibull","exponential","rayleigh","loggaussian","lognormal","loglogistic"].includes(s),m=r.scale!==void 0&&r.scale>0?r.scale:void 0,u=s==="exponential"||s==="rayleigh"?s==="exponential"?1:.5:m,h=r.time2,p=r.weights??Array(a).fill(1),w=r.offset??Array(a).fill(0),d=r.strata??Array(a).fill("1");for(let[G,ue]of[[p,"weights"],[w,"offset"],[d,"strata"]])if(G.length!==a)throw new RangeError(`variable lengths differ (found for '${ue}')`);let b=[],y=[],v=[],M=[],g=[],N=[],x=[],E=[],R=[],T=(o==="right"||o==="left")&&l.some(G=>Number(G)===2),A=G=>{R.includes(G)||R.push(G)},S=[...new Set(d.map(String))].sort(),C=d.map(G=>S.indexOf(String(G))),L=[];if(u!==void 0&&S.length>1)throw new Error("The scale argument is not valid with multiple strata");for(let G=0;G<a;G++){let ue=n.length===0?[]:Array.from(n[G]);if((o==="interval2"?Number.isNaN(e[G])&&Number.isNaN(h[G]):o==="interval"&&Vv(l[G])===3?Number.isNaN(e[G])||Number.isNaN(h[G]):Number.isNaN(e[G]))||o!=="interval2"&&Number.isNaN(l[G])||Number.isNaN(p[G])||Number.isNaN(w[G])||Number.isNaN(d[G])||ue.some(Number.isNaN))continue;if(!(p[G]>0))throw new RangeError("Invalid weights, must be >0");if(ue.some(ve=>!Number.isFinite(ve)))throw new Error("data contains an infinite predictor");let Ne=e[G],Ee=e[G],ee=Number(l[G]);if(o==="right")if(T){if(ee!==1&&ee!==2){A("Invalid status value, converted to NA");continue}ee===1&&(Ee=1/0),ee=ee===2?1:0}else{if(ee!==0&&ee!==1){A("Invalid status value, converted to NA");continue}ee===0&&(Ee=1/0),ee=ee===1?1:0}else if(o==="left")if(T){if(ee!==1&&ee!==2){A("Invalid status value, converted to NA");continue}ee===1&&(Ne=-1/0),ee=ee===2?1:2}else{if(ee!==0&&ee!==1){A("Invalid status value, converted to NA");continue}ee===0&&(Ne=-1/0),ee=ee===1?1:2}else if(o==="interval"){if(Ee=h[G],ee===0)Ne=e[G],Ee=1/0;else if(ee===1)Ee=Ne;else if(ee===2)Ee=e[G],Ne=-1/0;else if(ee!==3){A("Invalid status value, converted to NA");continue}if(ee===3&&Ne>Ee){A("Invalid interval: start > stop, NA created");continue}if(ee===3&&Ne===Ee)throw new Error("initial iteration failed (use starting estimates?)")}else if(o==="interval2"){let ve=e[G],ie=h[G];if(Number.isNaN(ve)&&Number.isNaN(ie))continue;if(ve>ie){A("Invalid interval: start > stop, NA created");continue}ve===ie?(Ne=ve,Ee=ie,ee=1):!Number.isFinite(ve)||ve===0?(Ne=-1/0,Ee=ie,ee=2):Number.isFinite(ie)?(Ne=ve,Ee=ie,ee=3):(Ne=ve,Ee=1/0,ee=0)}if(f){if((Number.isFinite(Ne)&&Ne<0||Number.isFinite(Ee)&&Ee<0)&&A("NaNs produced"),Ne===1/0||Ee===-1/0||Number.isFinite(Ne)&&Ne<=0||Number.isFinite(Ee)&&Ee<=0)throw new Error("Invalid survival times for this distribution");Number.isFinite(Ne)&&(Ne=Math.log(Ne)),Number.isFinite(Ee)&&(Ee=Math.log(Ee))}b.push(G),y.push(ee===1?Ee:Number.isFinite(Ne)&&Number.isFinite(Ee)?en(Ne,Ee):Number.isFinite(Ne)?Ne:Ee),v.push(Ne),M.push(Ee),g.push(ee),N.push(p[G]),x.push(w[G]),E.push(ue),L.push(C[G])}if(b.length===0)throw new Error("initial iteration failed (use starting estimates?)");if(!g.some(G=>G===1||G===3))throw new Error("initial iteration failed (use starting estimates?)");if(y.some(G=>!Number.isFinite(G)))throw new Error("initial iteration failed (use starting estimates?)");if(E.some(G=>G.length!==E[0].length))throw new TypeError("all rows of the matrix must have the same length");let k=E[0].length,F=k+(c?1:0),I=E.map(G=>c?[1,...G]:G),O=S.length,q=y.map((G,ue)=>G-x[ue]),$=N.reduce((G,ue)=>G+ue,0),_=q.reduce((G,ue,Me)=>G+ue*N[Me],0)/$,P=q.reduce((G,ue,Me)=>G+N[Me]*(ue-_)**2,0)/$,K=Math.max(...q)-Math.min(...q),X=s==="extreme"||s==="weibull"||s==="exponential"||s==="rayleigh"?P/1.64:s==="logistic"||s==="loglogistic"?P/3.2:s==="t"?P*(i-2)/i:P,re=u??2*Math.sqrt(X);if(u===void 0&&(!(re>0)||!(K>0)))throw new Error("initial iteration failed (use starting estimates?)");let z=F+(u===void 0?O:0),J=Array(z).fill(0);if(c&&(J[0]=_),u===void 0)for(let G=0;G<O;G++)J[F+G]=Math.log(re);let Z=r.control??{},Se=Z.maxiter??30,ne=Z.relTolerance??1e-9;if(!Number.isInteger(Se)||Se<0||!Number.isFinite(ne)||ne<0)throw new RangeError("invalid control");if(Z.tolerChol!==void 0&&(!Number.isFinite(Z.tolerChol)||Z.tolerChol<0))throw new RangeError("invalid control");let D=I.map(()=>[1]),Y=I.every(G=>G.length===1&&G[0]===1);if(r.init!==void 0&&r.init.length!==F&&r.init.length!==z)throw new Error("Wrong length for initial parameters");let U=y.map((G,ue)=>Ea(v[ue],M[ue],g[ue],G,u??re,s,i)).map((G,ue)=>{let Me=-N[ue]*G.ee;return Me>0&&Number.isFinite(Me)?{w:Me,r:y[ue]-G.e/G.ee-x[ue]}:{w:0,r:0}}),W=U.reduce((G,ue)=>G+ue.w,0);if(!(W>0))throw new Error("initial iteration failed (use starting estimates?)");let te=U.reduce((G,ue)=>G+ue.w*ue.r,0)/W,fe=[r.init!==void 0&&Y?r.init[0]:te,...u===void 0?Array(O).fill(Math.log(re)):[]];if(Y&&r.init!==void 0&&r.init.length===z)for(let G=0;G<O;G++)fe[1+G]=r.init[1+G];let Re=Y?Se:Math.max(Se,1e3),Le=Oh(D,y,v,M,g,N,x,L,O,s,i,u,fe,Re,ne,Z.tolerChol??1e-10),be=J.slice();if(u===void 0)for(let G=0;G<O;G++)be[F+G]=Le.b[1+G];if(!Y&&r.init!==void 0){for(let G=0;G<F;G++)be[G]=r.init[G];if(r.init.length===z)for(let G=F;G<z;G++)be[G]=r.init[G]}else if(!Y&&F>0){let G=E.map((Ee,ee)=>{let ve=u??Math.exp(Le.b[1+L[ee]]),ie=Ea(v[ee],M[ee],g[ee],y[ee],ve,s,i),Te=-N[ee]*ie.ee;return Te>0&&Number.isFinite(Te)?{w:Te,r:y[ee]-ie.e/ie.ee-x[ee]}:{w:0,r:0}}),ue=zh(I,Z.tolerChol??1e-10),Me=Array.from({length:F},(Ee,ee)=>Array.from({length:F},(ve,ie)=>ue.values.reduce((Te,Be,$e)=>Te+G[$e].w*Be[ee]*Be[ie],0))),Ne=Array.from({length:F},(Ee,ee)=>ue.values.reduce((ve,ie,Te)=>ve+G[Te].w*ie[ee]*G[Te].r,0));try{let Ee=Me.map(ie=>ie.slice()),ve=Vh(Ee,Z.tolerChol??1e-10)>=0?$h(Ee,Ne):es(nt(Me),Ne);be.splice(0,F,...ve.map((ie,Te)=>Ve(ie,-ue.exponent[Te])))}catch{}}let me=Y?Le:Oh(I,y,v,M,g,N,x,L,O,s,i,u,be,Se,ne,Z.tolerChol??1e-10),ke=f?b.reduce((G,ue,Me)=>G+(g[Me]===1?N[Me]*Math.log(e[ue]):0),0)*-1:0,de=me.V.map(G=>G.slice()),Ce=me.b.slice(0,F).map((G,ue)=>Number.isNaN(me.z[ue])?NaN:G),xe=Ce.map((G,ue)=>(de[ue]?.[ue]??0)===0?0:Math.sqrt(Math.max(0,de[ue]?.[ue]??0))),Fe=Ce.map((G,ue)=>me.z[ue]).map(G=>2*ae(-Math.abs(G))),Ke=u!==void 0?[u]:Array.from({length:O},(G,ue)=>Math.exp(me.b[F+ue])),ze=Array.from({length:F},(G,ue)=>I.reduce((Me,Ne)=>Me+Ne[ue],0)/I.length),et=I.map((G,ue)=>pr(G,me.b.slice(0,F))+x[ue]),Ue=2*(me.ll+ke-(Le.ll+ke)),Xe=F+(u===void 0?O:0)-(1+(u===void 0?O:0)),pe=[...Ce.map((G,ue)=>ue===0&&c?"(Intercept)":`x${ue+(c?0:1)}`),...u===void 0?O===1?["Log(scale)"]:S:[]],oe=[...Ce,...u===void 0?me.b.slice(F):[]],we=oe.map((G,ue)=>(de[ue]?.[ue]??0)===0?0:Math.sqrt(Math.max(0,de[ue]?.[ue]??0))),ge=oe.map((G,ue)=>me.z[ue]),ce=ge.map(G=>2*ae(-Math.abs(G))),tt={coefficients:Ce,coefNames:pe.slice(0,F),scale:Ke,table:{value:oe,se:we,z:ge,p:ce},strataLevels:S.map(G=>d.find(ue=>String(ue)===G)),hasOffset:r.offset!==void 0,icoef:u===void 0||b.length===1?Le.b.slice():[Le.b[0],Math.log(u)],var:de,loglik:[Le.ll+ke,me.ll+ke],iter:me.iter,df:F+(u===void 0?O:0),idf:1+(u===void 0?O:0),dfResidual:b.length-(F+(u===void 0?O:0)),n:b.length,linearPredictors:et,means:ze,score:me.score.slice(),tableRowNames:pe,value:oe,se:we,z:ge,p:ce,chi:Ue,chiDf:Xe,chiP:It(Ue,Xe,{lowerTail:!1}),dist:s,distributionDf:i,parms:i,intercept:c,x:I,offset:x,rows:b,weights:N,strata:L,y,lower:v,upper:M,status:g,type:o,warnings:[...R,...me.warn||Le.warn?["Ran out of iterations and did not converge"]:[],...s==="exponential"&&r.scale!==void 0&&r.scale>0?["Exponential has a fixed scale, user specified value ignored"]:[]]};return Bc.set(tt,{V:me.VScaled,xExponent:me.xExponent}),Q("survreg",tt)}function Xv(e,t={}){B(t,Ov,"survregPredict");let n=t.type??"lp",r=t.newX===void 0?e.x:t.newX.map(h=>e.intercept?[1,...h]:Array.from(h)),o=e.hasOffset?t.newOffset??(t.newX===void 0?e.offset:Array(r.length).fill(0)):Array(r.length).fill(0);if(o.length!==r.length)throw new RangeError("variable lengths differ (found for 'newOffset')");let s=e.strataLevels.map(String),i=t.newStrata?.map(h=>s.indexOf(String(h)))??(t.newX===void 0?e.strata.slice():r.map(()=>0));if(e.strataLevels.length>1&&t.newStrata===void 0&&t.newX!==void 0)throw new Error("object 's' not found");if(t.newStrata!==void 0&&t.newStrata.length!==r.length)throw new RangeError("variable lengths differ (found for 'newStrata')");if(t.newX!==void 0&&r.some(h=>h.length!==e.coefficients.length))throw new RangeError("newX has the wrong number of columns");if(i.some(h=>h<0))throw new Error("unknown stratum in newStrata");if(!["lp","linear","link","response","quantile","uquantile"].includes(n))throw new RangeError("invalid prediction type");let c=t.p===void 0?[.1,.9]:typeof t.p=="number"?[t.p]:Array.from(t.p),a=["weibull","exponential","rayleigh","loggaussian","lognormal","loglogistic"].includes(e.dist),l=r.map((h,p)=>{if(h.some(Number.isNaN))return n==="quantile"||n==="uquantile"?c.map(()=>NaN):NaN;let w=t.newX===void 0&&t.newOffset===void 0?e.linearPredictors[p]:pr(h,e.coefficients)+o[p],d=e.scale.length===1?e.scale[0]:e.scale[i[p]];return n==="lp"||n==="linear"||n==="link"?w:n==="response"?a?Math.exp(w):w:c.map(b=>{let y=w+d*Fh(b,e.dist,e.distributionDf);return n==="uquantile"?y:a?Math.exp(y):y})}),f=(n==="quantile"||n==="uquantile")&&c.length===1?l.map(h=>h[0]):l;if(!t.seFit)return f;let m=r.map((h,p)=>{if(h.some(Number.isNaN))return n==="quantile"||n==="uquantile"?c.map(()=>NaN):NaN;let w=t.newX===void 0&&t.newOffset===void 0?e.linearPredictors[p]:pr(h,e.coefficients)+o[p],d=e.scale.length===1?e.scale[0]:e.scale[i[p]],b=Bc.get(e),y=b?.V??e.var,v=M=>{let g=Array(y.length).fill(0);for(let N=0;N<h.length;N++)g[N]=b?Ve(h[N],-b.xExponent[N]):h[N];return y.length>h.length&&(g[h.length+i[p]]=M*d),Math.sqrt(Math.max(0,pr(g,es(y,g))))};return n==="lp"||n==="linear"||n==="link"?v(0):n==="response"?a?Ih(w,v(0)):v(0):c.map(M=>{let g=Fh(M,e.dist,e.parms),N=v(g),x=w+d*g;return n==="uquantile"?N:a?Ih(x,N):N})}),u=(n==="quantile"||n==="uquantile")&&c.length===1?m.map(h=>h[0]):m;return{fit:f,seFit:u,warnings:[]}}function Qv(e,t={}){B(t,Dv,"survregResiduals");let n=t.type??"response",r=e.distributionDf,o=e.coefficients.length,s=e.var.length,i=t.weighted??!1;if(!["response","deviance","working","dfbeta","dfbetas","ldcase","ldresp","ldshape","matrix"].includes(n))throw new RangeError("invalid residual type");let c=Bc.get(e),a=c?.V??e.var,l=e.x.map((f,m)=>{let u=e.linearPredictors[m],h=e.scale.length===1?e.scale[0]:e.scale[e.strata[m]],p=Ea(e.lower[m],e.upper[m],e.status[m],u,h,e.dist,r),w=i?e.weights[m]:1,d=f.map((R,T)=>c?Ve(R,-c.xExponent[T]):R),b=Array(s).fill(0);for(let R=0;R<o;R++)b[R]=d[R]*p.e;s>o&&(b[o+e.strata[m]]=p.t);let y=es(a,b),v=y.map((R,T)=>c&&T<o?Ve(R,-c.xExponent[T]):R),M=pr(b,y);if(n==="matrix")return[p.g,p.e,p.ee,p.t,p.tt,p.et].map(R=>R*w);if(n==="working")return-p.e/p.ee*w;if(n==="dfbeta")return v.map(R=>R*w);if(n==="dfbetas")return y.map((R,T)=>R/Math.sqrt(a[T]?.[T]??NaN)*w);if(n==="ldcase")return M*w;if(n==="ldresp"||n==="ldshape"){let R=Array(s).fill(0);for(let T=0;T<o;T++)R[T]=n==="ldresp"?h*(-d[T]*p.ee):d[T]*p.et;return s>o&&(R[o+e.strata[m]]=n==="ldresp"?-h*p.et:p.tt),pr(R,es(a,R))*w}let g=Ma(0,e.dist,r)-Math.log(h),N=e.status[m]===3?en(e.lower[m],e.upper[m]):e.y[m];e.status[m]===3&&(e.dist==="extreme"||e.dist==="weibull"||e.dist==="exponential"||e.dist==="rayleigh")&&(N=Wv(e.lower[m],e.upper[m],h));let x=e.status[m]===0?1:e.status[m]===2?-1:Math.sign(N-u);if(n==="deviance"){let R=e.status[m]===1?g:0;if(e.status[m]===3){let T=jv(e.lower[m],e.upper[m],h);e.dist==="extreme"||e.dist==="weibull"||e.dist==="exponential"||e.dist==="rayleigh"?R=Bv(e.lower[m],e.upper[m],h):e.dist==="logistic"||e.dist==="loglogistic"?R=Math.log(Math.tanh(T/4)):e.dist==="gaussian"||e.dist==="loggaussian"||e.dist==="lognormal"?R=Math.log(2*ae(T/2)-1):e.dist==="t"&&(R=Math.log(2*on(T/2,e.distributionDf)-1))}return x*Math.sqrt(Math.max(0,2*(R-p.g)))*w}let E=N-u;return(["weibull","exponential","rayleigh","loggaussian","lognormal","loglogistic"].includes(e.dist)?zv(N,u):E)*w});return l}var Zv=["n","power","r","type","nfractional","alpha","sidedTest"],Jv=["n","power","r","type","nfractional","alpha","sidedTest"],eN=["n","power","r","type","nfractional","alpha","sidedTest"],tN=["n","power","r","type","nfractional","alpha","sidedTest"],nN=["n","power","r","type","nfractional","alpha","sidedTest"],rN=["n","power","r","type","nfractional","alpha","sidedTest"],oN=["sigLevel","alternative","strict","nullOddsRatio","errbound","tsmethod"],sN=["alpha"],aN=["alpha"],iN=["alpha"],cN=["alpha"],lN=["alpha"],fN=["nmax"],Cr=.05,mn=e=>le(e),Un=e=>ae(e),Lr=e=>Number.isFinite(e),qt=e=>Lr(e)&&e>=0&&e<=1,Mn=e=>Lr(e)&&e>0;function Je(e,t,n,r){if(typeof e!="number")throw new TypeError(`'${t}' must be numeric`);if(!n(e))throw new RangeError(r)}function Fr(e){Je(e,"alpha",qt,"'alpha' must be a probability in [0, 1]")}function mN(e,t){return[e*t/(1+t),e/(1+t)]}function uo(e,t,n,r,o,s,i={}){let{n:c,power:a}=i,l=i.r??1,f=i.alpha??Cr,m=i.nfractional??!1,u=e==="equivalence"?Ae(i.type,["equivalence","equality"]):"equivalence",h=i.sidedTest??1;if(e==="equivalence"&&u!=="equivalence"&&u!=="equality")throw new RangeError("'type' should be one of \u201Cequivalence\u201D, \u201Cequality\u201D");if(Je(n,"treat",Lr,"'treat' must be finite"),Je(r,"control",Lr,"'control' must be finite"),t==="binary"?(Je(n,"treat",qt,"'treat' must be a probability in [0, 1]"),Je(r,"control",qt,"'control' must be a probability in [0, 1]")):Je(o,"sigma",Mn,"'sigma' must be positive and finite"),Je(l,"r",Mn,"'r' must be positive and finite"),Fr(f),(e!=="equivalence"||u!=="equality")&&Je(s,"delta",C=>Lr(C)&&C>=0,"'delta' must be nonnegative and finite"),e==="superiority"&&h!==1&&h!==2)throw new RangeError("'sidedTest' must be 1 or 2");if(typeof m!="boolean")throw new TypeError("'nfractional' must be logical");tn([c,a],"exactly one of 'n' and 'power' must be NULL"),ye(a)||Je(a,"power",C=>qt(C)&&C>0&&C<1,"'power' must be strictly between 0 and 1"),c!=null&&Je(c,"n",C=>Lr(C)&&C>0,"'n' must be positive and finite");let p=n-r,w=t==="binary"?n*(1-n):o*o,d=t==="binary"?r*(1-r):o*o,b=e==="equivalence"&&u==="equality",y=b?void 0:s,v=e==="noninferiority"?p+y:e==="superiority"?p-y:b?p:y-Math.abs(p);if(e==="noninferiority"&&v<=0)throw new RangeError("Target power is not reachable under the specified hypotheses");let M=e==="superiority"?le(f/h,{lowerTail:!1}):b?le(f/2,{lowerTail:!1}):le(f,{lowerTail:!1}),g=a??.8,N=b||e!=="equivalence"?le(1-g,{lowerTail:!1}):le((1-g)/2,{lowerTail:!1}),x,E,R,T,A;if(ye(c))v===0?(R=1/0,T=1/0):(T=(M+N)**2*(w/l+d)/(v*v),R=l*T),m?(x=R,E=T):(E=Math.ceil(T),x=Math.ceil(e==="equivalence"&&t==="continuous"||e==="superiority"?l*E:R)),A=a;else{[R,T]=mN(c,l),x=m?R:Math.ceil(R),E=m?T:Math.ceil(T);let C=Math.sqrt(w/x+d/E);if(e==="equivalence"&&!b)A=Math.max(0,Un((s-p)/C-mn(1-f))+Un((s+p)/C-mn(1-f))-1);else if(b)A=Un(Math.abs(p)/C-mn(1-f/2))+Un(-Math.abs(p)/C-mn(1-f/2));else if(e==="noninferiority")A=Un((p+s)/C-mn(1-f));else{let L=Math.abs(p-s)/C;A=Un(L-M)+Un(-L-M)}}return{nTotal:x+E,nTreat:x,nControl:E,nTotalUnrounded:R+T,nTreatUnrounded:R,nControlUnrounded:T,power:A,...y===void 0?{}:{delta:y},warnings:[]}}function uN(e,t,n,r={}){return B(r,Zv,"epiSsequb"),Q("epiSsequb",uo("equivalence","binary",e,t,void 0,n??void 0,r))}function hN(e,t,n,r,o={}){B(o,Jv,"epiSsequc");let s=uo("equivalence","continuous",e,t,n,r??void 0,o);return Q("epiSsequc",s)}function pN(e,t,n,r={}){return B(r,eN,"epiSsninfb"),Q("epiSsninfb",uo("noninferiority","binary",e,t,void 0,n,r))}function dN(e,t,n,r,o={}){return B(o,tN,"epiSsninfc"),Q("epiSsninfc",uo("noninferiority","continuous",e,t,n,r,o))}function gN(e,t,n,r={}){return B(r,nN,"epiSssupb"),Q("epiSssupb",uo("superiority","binary",e,t,void 0,n,r))}function wN(e,t,n,r,o={}){return B(o,rN,"epiSssupc"),Q("epiSssupc",uo("superiority","continuous",e,t,n,r,o))}function mo(e,t){let n=new Array(e+1).fill(0);if(t===0)return n[0]=1,n;if(t===1)return n[e]=1,n;if(t>.5)return mo(e,1-t).reverse();let r=Math.floor((e+1)*t);n[r]=Math.exp(e*Math.log1p(-t)+r*Math.log(t/(1-t))+Array.from({length:r},(o,s)=>Math.log(e-s)-Math.log(s+1)).reduce((o,s)=>o+s,0));for(let o=r;o>0;o--)n[o-1]=n[o]*(o/(e-o+1))*((1-t)/t);for(let o=r;o<e;o++)n[o+1]=n[o]*((e-o)/(o+1))*(t/(1-t));return n}function bN(e,t,n,r={}){B(r,oN,"powerPaired2x2");let o=r.sigLevel??Cr,s=Ae(r.alternative,["two.sided","one.sided"]),i=r.strict??!1,c=r.nullOddsRatio??1,a=r.errbound??1e-6,l=Ae(r.tsmethod,["central","minlike","blaker"]);if(Je(e,"pb",qt,"'pb' must be in [0, 1]"),Je(t,"pc",qt,"'pc' must be in [0, 1]"),Je(n,"npairs",N=>Number.isSafeInteger(N)&&N>=0&&N<=1e5,"'npairs' must be a nonnegative safe integer"),Fr(o),Je(c,"nullOddsRatio",Mn,"'nullOddsRatio' must be positive and finite"),Je(a,"errbound",N=>Lr(N)&&N>=0,"'errbound' must be nonnegative and finite"),e+t>1)throw new RangeError("probabilities must be finite, non-negative and not all 0");if(s!=="two.sided"&&s!=="one.sided")throw new RangeError("'alternative' should be one of \u201Ctwo.sided\u201D, \u201Cone.sided\u201D");if(!["central","minlike","blaker"].includes(l))throw new RangeError("'tsmethod' should be one of \u201Ccentral\u201D, \u201Cminlike\u201D, \u201Cblaker\u201D");let f=e+t,m=f===0?.5:e/f,u=c/(1+c),h=e>=t?1:-1,p=s==="two.sided"&&i&&l!=="blaker",w=s==="one.sided",d=mo(n,f),b=0,y=n;if(a>0){let N=0;for(;b<=n&&(N+=d[b])<=a/2;)b++;for(N=0;y>=0&&(N+=d[y])<=a/2;)y--}let v=0;for(let N=b;N<=y;N++){let x=d[N];if(x===0)continue;let E=mo(N,m),R=mo(N,u),T=0,A=0,S=new Array(N+1),C=new Array(N+1);for(let L=0;L<=N;L++)T+=R[L],S[L]=T;for(let L=N;L>=0;L--)A+=R[L],C[L]=A;for(let L=0;L<=N;L++){let k=S[L],F=C[L],I,O=u===.5&&o===1;w?I=O||(h>0?F<=o:k<=o):p?I=O||k<=o/2||F<=o/2:I=h>0?O?L>Math.floor(N/2):F<=o/2:O?L<Math.ceil(N/2):k<=o/2,I&&(v+=x*E[L])}}let M=`errbound= ${a===0?"0":a>=.001?String(a):`${a.toExponential(0).replace(/e([+-]?)(\\d+)$/,(N,x,E)=>`e${x||"+"}${E.padStart(2,"0")}`)}`}${e+t===1?" no non-informative pairs: pa+pd=0":""}`,g="Power for McNemar's Exact Test"+(p?", including power to reject in the wrong direction":"")+(l==="central"&&r.tsmethod!==void 0?`, using (tsmethod=${l})`:l==="minlike"?`, using (tsmethod=${l})`:l==="blaker"?`, using (tsmethod=${l})`:"");return Q("powerPaired2x2",{power:v,npairs:n,pb:e,pc:t,sigLevel:o,alternative:s,nullOddsRatio:c,note:M,method:g,warnings:[]})}function vN(e,t,n,r,o,s={}){B(s,aN,"powerCT");let i=s.alpha??Cr;Fr(i);for(let[l,f]of[[e,"nE"],[t,"nC"]])Je(l,f,Mn,`'${f}' must be positive and finite`);for(let[l,f]of[[n,"pE"],[r,"pC"]])Je(l,f,qt,`'${f}' must be in [0, 1]`);Je(o,"RR",Mn,"'RR' must be positive and finite");let c=e/t,a=e*n+t*r;return Un(Math.sqrt(c*a)*Math.abs(o-1)/(c*o+1)-mn(1-i/2))}function NN(e,t,n,r,o,s={}){B(s,sN,"ssizeCT");let i=s.alpha??Cr;Fr(i),Je(e,"power",f=>qt(f)&&f>0&&f<1,"'power' must be strictly between 0 and 1"),Je(t,"k",Mn,"'k' must be positive and finite"),Je(n,"pE",qt,"'pE' must be in [0, 1]"),Je(r,"pC",qt,"'pC' must be in [0, 1]"),Je(o,"RR",Mn,"'RR' must be positive and finite");let c=mn(1-i/2)+mn(e),a=1/t*((t*o+1)/(o-1))**2*(c*c),l=t*n+r;return{nE:Math.ceil(a*t/l),nC:Math.ceil(a/l)}}function yN(e,t,n,r,o,s={}){B(s,cN,"powerEpi");let i=s.alpha??Cr;Fr(i),Je(e,"n",Mn,"'n' must be positive and finite"),Je(t,"theta",Mn,"'theta' must be positive and finite"),Je(n,"p",qt,"'p' must be in [0, 1]"),Je(r,"psi",qt,"'psi' must be in [0, 1]"),Je(o,"rho2",qt,"'rho2' must be in [0, 1]");let c=e*Math.log(t)**2*n*(1-n)*r*(1-o);return Un(Math.sqrt(c)-mn(1-i/2))}function MN(e,t,n,r,o,s={}){B(s,iN,"ssizeEpi");let i=s.alpha??Cr;Fr(i),Je(e,"power",qt,"'power' must be in [0, 1]"),Je(t,"theta",Mn,"'theta' must be positive and finite"),Je(n,"p",qt,"'p' must be in [0, 1]"),Je(r,"psi",qt,"'psi' must be in [0, 1]"),Je(o,"rho2",qt,"'rho2' must be in [0, 1]");let c=Math.log(t)**2*n*(1-n)*r*(1-o);return Math.ceil((mn(1-i/2)+mn(e))**2/c)}function EN(e,t,n,r,o={}){B(o,lN,"numDEpi");let s=o.alpha??Cr;Fr(s),Je(e,"power",c=>qt(c)&&c>0&&c<1,"'power' must be strictly between 0 and 1"),Je(t,"theta",Mn,"'theta' must be positive and finite"),Je(n,"p",qt,"'p' must be in [0, 1]"),Je(r,"rho2",qt,"'rho2' must be in [0, 1]");let i=Math.log(t)**2*n*(1-n)*(1-r);return Math.ceil((mn(1-s/2)+mn(e))**2/i)}function jh(e){let t=new Array(e.length),n=0,r=0;for(let o=0;o<e.length;o++){let s=e[o]-r,i=n+s;r=i-n-s,n=i,t[o]=n}return t}function xN(e){let t=new Array(e.length),n=0,r=0;for(let o=e.length-1;o>=0;o--){let s=e[o]-r,i=n+s;r=i-n-s,n=i,t[o]=n}return t}function Wh(e,t,n,r,o,s){let i=0,c=0;c=r.slice(0,e+1).reduce((a,l)=>a+l,0);for(let a=e+1;a<n.length;a++){let l=t-a,f=l<0?1:l+1>=o.length?0:o[l+1],m=l<0?0:s[Math.min(l,s.length-1)];i+=n[a]*f,c+=r[a]*m}return[i,c]}function RN(e,t,n,r,o={}){B(o,fN,"ph2simon");let s=o.nmax??100;for(let[M,g]of[[e,"pu"],[t,"pa"],[n,"ep1"],[r,"ep2"]])Je(M,g,qt,`'${g}' must be in [0, 1]`);Je(s,"nmax",M=>Number.isInteger(M)&&M>=1&&M<=1e3,"nmax cannot exceed 1000");let i=Array.from({length:s+1},(M,g)=>mo(g,e)),c=Array.from({length:s+1},(M,g)=>mo(g,t)),a=i.map(jh),l=c.map(jh),f=i.map(xN),m=[];for(let M=2;M<=s;M++){let g;for(let N=1;N<M&&(g===void 0||N<g.enP0);N++){let x=M-N,E=i[N],R=c[N],T=l[x],A=a[N];for(let S=0;S<N;S++){let C=A[S],L=N+x*(1-C),k=S,F=M-1,I=-1;for(;k<=F;){let O=Math.floor((k+F)/2),[,q]=Wh(S,O,E,R,f[x],T);q<=r?(I=O,k=O+1):F=O-1}if(I>=0){let[O]=Wh(S,I,E,R,f[x],T);O>n&&(I=-1)}I>=0&&(g===void 0||L<g.enP0)&&(g={r1:S,n1:N,r:I,n:M,enP0:L,petP0:C})}}g&&m.push(g)}if(m.length===0)throw new Error(` No feasible solution found.
20
- Increase maximum sample size. Current nmax value = ${s}.`);let u=m.filter((M,g)=>!m.some((N,x)=>x!==g&&N.n<=M.n&&N.enP0<=M.enP0&&(N.n<M.n||N.enP0<M.enP0))),h=[],p=(M,g)=>(M.enP0-g.enP0)/(g.n-M.n+(M.enP0-g.enP0));for(let M of u)for(h.push(M);h.length>=3;){let g=h[h.length-3],N=h[h.length-2],x=h[h.length-1];if(p(g,N)>p(N,x))break;h.splice(h.length-2,1)}let w=h.map((M,g)=>{let N=g===h.length-1?0:p(M,h[g+1]),x=g===0?1:p(h[g-1],M);return{...M,qLo:N,qHi:x}}),d=w[0],b=w.reduce((M,g)=>g.enP0<M.enP0?g:M),y=w,v=[];return b.n>=s-1&&v.push(` Optimal sample size too close to nmax.
18
+ `,fo=(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})),Jo=e=>e[0].map((t,n)=>e.map(r=>r[n])),es=(e,t=n=>n)=>e[0].map((n,r)=>At(e.map(o=>t(o[r])))),Nh=(e,t=n=>n)=>e.map(n=>At(n.map(t))),mo=(e,t=n=>n)=>At(e[0].flatMap((n,r)=>e.map(o=>t(o[r])))),ya=e=>e<0?-1:1;function $c(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 yh(e,t){let n=$c(e),{d:r,u:o,v:s}=Rs(n),i=r.map((a,l)=>l).filter(a=>r[a]>Math.sqrt(lo)*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)&&pr(t,"In smc, smcs > 1 were set to 1.0"),c.some(a=>a<0)&&pr(t,"In smc, smcs < 0 were set to .0"),c.map(a=>a>1?1:a<0?0:a)}var pr=(e,t)=>{e.includes(t)||e.push(t)};function Pc(e,t,n,r){let o=e.map((c,a)=>c.map((l,f)=>a===f?t[a]:l)),s=Bt(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 yv=e=>fo(e,Jo(e));function vh(e,t,n,r){let o=$c(e),s=o.length,i=yh(o,r),c=d=>{let b=Pc(o,d.map(E=>1-E),t,E=>E<lo?100*lo:E),N=yv(b),v=[];for(let E=0;E<s;E++)for(let g=E+1;g<s;g++)v.push((o[g][E]-N[g][E])**2);return At(v)},a=d=>{let b=Pc(o,d.map((N,v)=>o[v][v]-N),t,N=>Math.max(N,0));return d.map((N,v)=>{let E=0;for(let g=0;g<t;g++)E+=b[v][g]*b[v][g];return E+N-o[v][v]})},l=d=>{let b=Bt(o.map((T,S)=>T.map((A,C)=>S===C?1-d[S]:A)),{symmetric:!0}),N=b.values,v=b.vectors,E=N.map(T=>T>=lo),g=N.map(T=>T<lo?100*lo:T),y=o.map((T,S)=>T.map((A,C)=>{if(S===C)return 0;let L=0;for(let k=0;k<t;k++)L+=g[k]*v[S][k]*v[C][k];return A-L})),x=fo(y,v),M=(T,S)=>{let A=0;for(let C=0;C<s;C++)A+=v[C][T]*x[C][S];return A},R=new Array(s).fill(0);for(let T=0;T<t;T++){let S=(A,C)=>{if(C===0)return;let L=C*M(T,A);for(let k=0;k<s;k++)R[k]+=L*v[k][T]*v[k][A]};E[T]&&S(T,1);for(let A=T+1;A<t;A++)S(A,2*(E[T]&&E[A]?1:(g[T]-g[A])/(N[T]-N[A])));for(let A=t;A<s;A++)S(A,2*g[T]/(N[T]-N[A]))}return R},f=.005,m={method:"L-BFGS-B",lower:f,upper:Math.max(Nt(i),1)},u=new Array(s).fill(.01),h=ot(()=>To(i.map(d=>1-d),c,{gr:a,...m,control:{fnscale:1,parscale:u}}));h.par.some(d=>d===f)&&(h=ot(()=>To(h.par,c,{gr:l,...m,control:{fnscale:1,parscale:u,factr:1e3}}))),h.convergence!==0&&pr(n,`minres did not converge (optim convergence code ${h.convergence}): examine the results carefully`);let p=h.par,w=Pc(o,p.map((d,b)=>o[b][b]-d),t,d=>Math.max(d,0));if(w=Vc(w,t),w=w.map(d=>d.map(b=>b===0?1e-15:b)),Nh(w,d=>d*d).some(d=>d>1)&&pr(n,"An ultra-Heywood case was detected. Examine the results carefully"),t>1){w=Vc(Na(w).loadings,t);let d=es(w,N=>N*N),b=d.map((N,v)=>v).sort((N,v)=>d[v]-d[N]||N-v);w=w.map(N=>b.map(v=>N[v]))}else w=Vc(w,t);return w}function Vc(e,t){let n=es(e).map(ya);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 Mv(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:mo(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 Ev(e){let t=e[0].length,n=l=>{let f=nt(l),m=fo(e,Jo(f)),{f:u,Gq:h}=Mv(m),p=Jo(fo(fo(Jo(m),h),f)).map(w=>w.map(d=>-d));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=es(r.map((d,b)=>d.map((N,v)=>N*s.G[b][v]))),m=s.G.map((d,b)=>d.map((N,v)=>N-r[b][v]*f[v])),u=Math.sqrt(mo(m,d=>d*d));if(c.push(s.f),u<1e-5){a=!0;break}if(i!==void 0){let d=i,b=r.map((v,E)=>v.map((g,y)=>g-d.T[E][y])),N=m.map((v,E)=>v.map((g,y)=>g-d.Gp[E][y]));mo(N,v=>v*v)>0&&(o=mo(b,v=>v*v)/Math.abs(mo(b.map((v,E)=>v.map((g,y)=>g*N[E][y])))),o=Math.max(1e-10,Math.min(o,20)))}else o=2*o;let h=Nt(c.slice(-10)),p=s,w=r;for(let d=0;d<=10;d++){let b=r.map((v,E)=>v.map((g,y)=>g-o*m[E][y])),N=es(b,v=>v*v).map(v=>1/Math.sqrt(v));if(w=b.map(v=>v.map((E,g)=>E*N[g])),p=n(w),h-p.f>.5*u*u*o)break;o=o/2}i={T:r,Gp:m},r=w,s=p}return{loadings:s.L,phi:fo(Jo(r),r),converged:a}}function xv(e,t={}){B(t,vv,"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)va(t.covmat),r=$c(t.covmat);else{if(e===null)throw new TypeError("neither 'x' nor 'covmat' supplied");r=Ro(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=vh(r,n,s,i),a,l,f;if(n===1)a=c.map(_=>_.slice()),f=[1],pr(i,`Omega_h for 1 factor is not meaningful, just omega_t
19
+ `),pr(s,"Omega_h and Omega_asymptotic are not meaningful with one factor");else{let _=Ev(c);if(_.converged||pr(s,"oblimin did not converge in 2000 iterations"),a=_.loadings,l=_.phi,n===2){let P=l[0][1],K=Math.sqrt(Math.abs(P));f=[K,ya(P)*K],pr(i,Nv)}else f=vh(l,1,s,i).map(P=>P[0])}let m=a.map(_=>{let P=0;for(let K=0;K<n;K++)P+=_[K]*f[K];return P}),u=Nh(c,_=>_*_),h=u.map((_,P)=>r[P][P]-_),p=a.map((_,P)=>_.map((K,X)=>{if(n===1)return 0;let re=r[P][P],z=re-(re-K*K)-(K*f[X])**2;return z<0&&(z=0),ya(K)*Math.sqrt(z)})),w=m.map((_,P)=>_*_/u[P]),d=m.map((_,P)=>{let K=[_,...p[P]];return At(K.map(X=>X*X))**2/At(K.map(X=>X**4))}),b=m.map(ya);b.some(_=>_<0)&&(r=r.map((_,P)=>_.map((K,X)=>b[P]*K*b[X])));let N=m.map((_,P)=>b[P]*_),v=p.map((_,P)=>_.map(K=>b[P]*K)),E=N.map((_,P)=>[_,...v[P],u[P],h[P],w[P],d[P]]),g=mo(r),y=At(r.map((_,P)=>_[P])),x=At(N)**2,M=At(h),R=x/g,T=(g-M)/g,S=x/(g-M),A=(g-y)/g*o/(o-1),C=(g+At(yh(r,i))-y)/g,L=es(E.map(_=>_.slice(0,n+1)),_=>_*_),k=L[0]/At(L),F=v.map(_=>{let P=0;for(let K=1;K<n;K++)Math.abs(_[K])>Math.abs(_[P])&&(P=K);return P}),I=[],O=[];for(let _=0;_<n;_++){let P=F.map((z,J)=>J).filter(z=>F[z]===_),K=At(P.flatMap(z=>P.map(J=>r[J][z]))),X=At(P.map(z=>v[z][_])),re=At(P.map(z=>N[z]));O.push(X*X),I.push([(X*X+re*re)/K,re*re/K,X*X/K])}let q=[[T,At(N)**2/g,At(O)/g],...I],$={omegaH:R,omegaLim:S,alpha:A,omegaTot:T,G6:C,ECV:k,sl:E,slNames:["g",...Array.from({length:n},(_,P)=>`F${P+1}*`),"h2","u2","p2","com"],orthog:c,oblique:a,gloading:f,key:b,omegaGroup:q,warnings:s,messages:i};return l!==void 0&&($.phi=l),Q("omega",$)}var Mh=["ML","stdErr"],Rv=["ML","stdErr"],rn=.9999;function jc(e){let t=De(e),n=[],r=0;for(let o=0;o<e.length-1;o++)r+=e[o],n.push(le(r/t));return n}var Wc=e=>[...new Set(e)].sort((t,n)=>t-n),Eh=(e,t)=>e===1?`1 ${t} with zero marginal removed`:`${e} ${t}s with zero marginals removed`,xh=e=>Array.isArray(e)||ArrayBuffer.isView(e),Rh=(e,t)=>e>0?se(e,{lowerTail:!1})-se(t,{lowerTail:!1}):se(t)-se(e);function Bc(e){let t=qe([e]).groups[0],n=it(t),r=t.map(s=>s-n),o=Math.sqrt(at(r));return r.map(s=>s/o)}function Th(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 Ma=e=>Array.from({length:e},()=>new Array(e).fill(0));function qh(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 zc={r:0,h:0,k:0,rr:0,rh:0,rk:0,hh:0,hk:0,kk:0};function Tv(e,t,n){if(e===-1/0||t===-1/0||e===1/0&&t===1/0)return zc;if(e===1/0)return{...zc,k:Xt(t),kk:-t*Xt(t)};if(t===1/0)return{...zc,h:Xt(e),hh:-e*Xt(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=Xt(e),f=Xt(t);return{r:i,h:l*se(c),k:f*se(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*se(c)-n/o*l*Xt(c),hk:i,kk:-t*f*se(a)-n/o*f*Xt(a)}}function Sh(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:Ma(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=>Tv(h,p,r))),u=[];for(let h=0;h<s;h++)for(let p=0;p<i;p++){let w=e[h][p];if(w===0)continue;let d=bo([t[h],n[p]],[t[h+1],n[p+1]],r),b=new Array(c).fill(0),N=Ma(c);for(let[v,E,g]of[[h+1,p+1,1],[h,p+1,-1],[h+1,p,-1],[h,p,1]]){let y=m[v][E],x=l(v),M=f(E);b[0]+=g*y.r,N[0][0]+=g*y.rr,x>=0&&(b[x]+=g*y.h,N[0][x]+=g*y.rh,N[x][0]+=g*y.rh,N[x][x]+=g*y.hh),M>=0&&(b[M]+=g*y.k,N[0][M]+=g*y.rk,N[M][0]+=g*y.rk,N[M][M]+=g*y.kk),x>=0&&M>=0&&(N[x][M]+=g*y.hk,N[M][x]+=g*y.hk)}qh(a,w,d,b,N),u.push(w*Math.log(d))}return a.value=De(u),a}function Sv(e,t,n,r,o){let s=o?n.length+1:1,i={value:0,gradient:new Array(s).fill(0),hessian:Ma(s)},c=(1-r)*(1+r),a=Math.sqrt(c),l=c*a,f=new Array(s),m=Ma(s),u=[],h=(p,w,d)=>{let b=n[p],N=(b-r*w)/a,v=Xt(N),E=(r*b-w)/l,g=1/a,y=b/l+3*r*(r*b-w)/(l*c),x=r/l;if(f[0]+=d*v*E,m[0][0]+=d*v*(y-N*E*E),o){let M=p+1;f[M]+=d*v*g;let R=d*v*(x-N*E*g);m[0][M]+=R,m[M][0]+=R,m[M][M]+=d*v*(-N*g*g)}};for(let p=0;p<e.length;p++){let w=e[p],d=t[p];f.fill(0);for(let v of m)v.fill(0);let b=d<n.length?(n[d]-r*w)/a:1/0,N=d>0?(n[d-1]-r*w)/a:-1/0;d<n.length&&h(d,w,1),d>0&&h(d-1,w,-1),qh(i,1,Rh(N,b),f,m),u.push(Math.log(Rh(N,b)),Xt(w,{log:!0}))}return i.value=De(u),i}function Av(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=nt(o,r)}catch{s=void 0}return(s===void 0||!s.every(Number.isFinite)||De(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 kh=(e,t)=>e>=t-1e-12*Math.abs(t);function Ah(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=Av(o,a);if(!s&&Math.abs(r[0])===rn&&l[0]*r[0]>0){s=!0;continue}let f=Math.abs(r[0]+l[0])>rn?(Math.sign(l[0])*rn-r[0])/l[0]:1,m=!1;for(let u=0;u<60;u++){let h=r.map((w,d)=>w+f*l[d]);f*l[0]!==0&&Math.abs(h[0])>=rn*(1-1e-15)&&(h[0]=Math.sign(h[0])*rn);let p=e(h);if(kh(p.value,o.value)){let w=Nt(h.map((d,b)=>Math.abs(d-r[b])/Math.max(1,Math.abs(r[b]))));r=h,o=p,m=w>1e-12;break}f/=2}if(!m)break}return{theta:r,at:o,bounded:s}}function Ih(e,t){let n=Ah(e,[Math.max(-rn,Math.min(rn,t[0])),...t.slice(1)],!1);if(n.bounded||n.theta[0]===0)return n;let r=Ah(e,[Math.sign(n.theta[0])*rn,...n.theta.slice(1)],!0);return kh(r.at.value,n.at.value)?r:n}function _h(e,t){let n=()=>e.map(o=>o.map(()=>1/0));if(t)return n();let r;try{r=nt(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 Lh=e=>`inadmissible correlation set to ${e>0?"0.9999":"-0.9999"}`;function qv(e,t,n){ct(t)&&B(t,Mh,"polychor"),B(n,Mh,"polychor");let r,o;if(xh(t)){let I=e,O=t;if(I.length!==O.length)throw new RangeError("'x' and 'y' must have the same length");let q=I.map((X,re)=>!Number.isNaN(X)&&!Number.isNaN(O[re])),$=Pt(I.filter((X,re)=>q[re])),_=Pt(O.filter((X,re)=>q[re])),P=new Map(Wc($).map((X,re)=>[X,re])),K=new Map(Wc(_).map((X,re)=>[X,re]));r=Array.from({length:P.size},()=>new Array(K.size).fill(0)),$.forEach((X,re)=>r[P.get(X)][K.get(_[re])]++),o=n??{}}else{let I=e;if(!I.every(xh))throw new TypeError("give a table (array of rows) or two vectors");let O=I[0]?.length??0;if(I.some(q=>q.length!==O))throw new TypeError("the table must have rows of equal length");if(r=I.map(q=>Array.from(q)),r.some(q=>q.some($=>!($>=0&&$<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;De(r.map(I=>De(I)))<2**1e3||({groups:r,k:a}=qe(r));let l=r.map(I=>De(I)),f=l.filter(I=>I===0).length;f>0&&(c.push(Eh(f,"row")),r=r.filter((I,O)=>l[O]!==0));let m=(r[0]??[]).map((I,O)=>De(r.map(q=>q[O]))),u=m.filter(I=>I===0).length;u>0&&(c.push(Eh(u,"column")),r=r.map(I=>I.filter((O,q)=>m[q]!==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 Q("polychor",{rho:NaN,warnings:c});let w=[-1/0,...jc(r.map(I=>De(I))),1/0],d=[-1/0,...jc(r[0].map((I,O)=>De(r.map(q=>q[O])))),1/0],N=Il(I=>{let O=Math.max(-rn,Math.min(rn,I)),q=[];for(let $=0;$<h;$++)for(let _=0;_<p;_++){if(r[$][_]===0)continue;let P=bo([w[$],d[_]],[w[$+1],d[_+1]],O);q.push(r[$][_]*Math.log(P))}return-De(q)},-rn,rn).optimum;if(!s&&!i)return Q("polychor",{rho:N,warnings:c});let v=I=>[-1/0,...I,1/0],E=w.slice(1,h),g=d.slice(1,p),y=I=>s?Sh(r,v(I.slice(1,h)),v(I.slice(h)),I[0],!0):Sh(r,w,d,I[0],!1),{theta:x,at:M,bounded:R}=Ih(y,s?[N,...E,...g]:[N]),T=x[0];if(R&&c.push(Lh(T)),!i)return Q("polychor",{rho:T,warnings:c});let S=s?v(x.slice(1,h)):w,A=s?v(x.slice(h)):d,C=De(r.map(I=>De(I))),L=[],k=[];for(let I=0;I<h;I++)for(let O=0;O<p;O++){let q=r[I][O],$=bo([S[I],A[O]],[S[I+1],A[O+1]],T);if(q===0)L.push(C*$);else if(a===0)L.push(q*Math.log1p(1e-6/q),q*Th(C*$/q-1));else{let _=V(q,a);k.push(_*Math.log1p(1e-6/_)),L.push(q*Th(C*$/q-1))}}let F=_h(M.hessian,R);return Q("polychor",{rho:T,warnings:c,var:a===0?F:F.map(I=>I.map(O=>V(O,-a))),chisq:a===0?2*De(L):V(2*De(L),a)+2*De(k),df:h*p-h-p,n:V(C,a),rowCuts:S.slice(1,h),colCuts:A.slice(1,p)})}function kv(e,t,n,r){let o=e.length,s=[le(.25),le(.5),le(.75)],i=(h,p)=>p.filter(w=>w<h).length,c=Bc(e).map(h=>i(h,n)),a=Bc(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((w,d)=>{if(w===0)return;let b=bo([f[p],m[d]],[f[p+1],m[d+1]],r);u.push(w*Math.log((w+1e-6)/(b*o)))})),2*De(u)}function Iv(e,t,n={}){if(B(n,Rv,"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])),i=e.filter((M,R)=>s[R]),c=Pt(t.filter((M,R)=>s[R])),a=i.length,l=Wc(c);if(l.length<2)return Q("polyserial",{rho:NaN,warnings:["y has fewer than 2 levels"]});let f=new Map(l.map((M,R)=>[M,R+1])),m=c.map(M=>f.get(M)),u=l.map(()=>0);for(let M of m)u[M-1]++;let h=jc(u),p=[],w=Math.sqrt((a-1)/a)*Math.sqrt(at(m))*Ll(i,m)[0]/De(h.map(M=>Xt(M)));if(Math.abs(w)>rn){let M=Math.sign(w)*rn;p.push(`initial correlation inadmissible, ${$n(w)}, set to ${$n(M)}`),w=M}if(!r&&!o)return Q("polyserial",{rho:w,warnings:p});let d=Bc(i),b=m.map(M=>M-1),N=M=>Sv(d,b,r?M.slice(1):h,M[0],r),{theta:v,at:E,bounded:g}=Ih(N,r?[w,...h]:[w]);if(g&&p.push(Lh(v[0])),!o)return Q("polyserial",{rho:v[0],warnings:p});let y=r?v.slice(1):h,x=l.length;return Q("polyserial",{rho:v[0],warnings:p,var:_h(E.hessian,g),chisq:kv(m,d,y,r?v[0]:w),df:4*x-x-1,n:a,cuts:y})}var _v=["group","strata","rho","cencode"],Lv=new Set(["cov2","tf","failcode","cencode","gtol","maxiter","init","variance","confLevel","names"]);function Ea(e){return e==null||typeof e=="number"&&Number.isNaN(e)}function Kc(e){let t=tn(e),n=Array.from(new Set(t)).sort(zn);return{labels:n,index:new Map(n.map((r,o)=>[r,o]))}}function Cv(e,t,n){if(e.length!==t.length)throw new RangeError("'time' and 'status' must have the same length");if(n.group!==void 0&&n.group.length!==e.length)throw new RangeError("'group' must have the same length as 'time'");if(n.strata!==void 0&&n.strata.length!==e.length)throw new RangeError("'strata' must have the same length as 'time'")}function Fv(e,t,n={}){B(n,_v,"cuminc"),Cv(e,t,n);let{group:r,strata:o,rho:s=0,cencode:i=0}=n,c=[],a=new Rt;for(let g=0;g<e.length;g++)if(!(Number.isNaN(e[g])||Ea(t[g])||r!==void 0&&Ea(r[g])||o!==void 0&&Ea(o[g]))){if(!Number.isFinite(e[g]))throw new RangeError("'time' must contain only finite values");if(e[g]<0)throw new RangeError("'time' must be non-negative");c.push(g)}let l=e.length-c.length;if(l>0&&a.add(`${l} cases omitted due to missing values`),c.length===0)throw new RangeError("no complete observations");let f=r===void 0?[1]:tn(c.map(g=>r[g])),m=o===void 0?[1]:tn(c.map(g=>o[g])),u=tn([...c.map(g=>t[g]),i]),h=Kc(f);if(h.labels.length>=2&&!Number.isFinite(s))throw new RangeError("'rho' must be finite when Gray's test is requested");let p=Kc(m),w=Kc(u),d=u[u.length-1],b=w.labels.filter(g=>g!==d);if(b.length===0)throw new Error("there are no events of any cause");let N=c.map((g,y)=>({time:e[g],status:u[y],group:r===void 0?0:h.index.get(f[y]),strata:o===void 0?0:p.index.get(m[y]),source:g})).sort((g,y)=>g.time-y.time),v=[];for(let g of b){let y=vt(g);for(let x=0;x<h.labels.length;x++){let M=N.filter(q=>q.group===x),R=M.reduce((q,$)=>Math.max(q,$.time),-1/0),T=R>0&&R<1?2**Math.min(1023,Math.ceil(-Math.log2(R))):1,S=mr(M.map(q=>q.time*T),M.map(q=>q.status===d?0:1)),A=[0],C=[0],L=[0],k=[],F=1;for(let q=0;q<S.time.length;q++){let $=S.nEvent[q];if($===0)continue;let _=S.time[q]/T,P=S.nRisk[q],K=M.filter(X=>X.time===_&&X.status===g).length;k.push({t:_,Y:P,d:$,dk:K,sb:F,sa:S.surv[q]}),F=S.surv[q]}let I=0,O=0;for(let q=0;q<k.length;q++){let $=k[q],_=I,P=O;I+=$.sb*$.dk/$.Y;let K=0;for(let X=0;X<=q;X++){let re=k[X],z=re.Y,J=re.d,Z=re.dk,Ae=z===J?0:(J*(z-J)+2*Z*(J-Z))/((z-1)*(z-J)**2),ne=z===1?Z:Z*(z-Z)/(z*z*(z-1)),D=z===J||z===1?0:Z*(z-Z)/(z*(z-J)*(z-1)),Y=0;for(let U=0;U<=X;U++){let W=k[U];Y+=W.sb*W.dk/W.Y}let j=I-Y;K+=j*j*Ae+re.sb*re.sb*ne-2*j*re.sb*D}O=Math.max(0,K),$.dk>0&&(A.push($.t,$.t),C.push(_,I),L.push(P,O))}A.push(R),C.push(I),L.push(O),v.push({name:`${vt(h.labels[x])} ${y}`,group:vt(h.labels[x]),cause:y,time:A,est:C,var:L})}}let E=h.labels.length>=2?Ov(N,h.labels,b,p.labels.length,s,d):void 0;return Q("cuminc",{curves:v,...E===void 0?{}:{tests:E},warnings:a.list})}function Ov(e,t,n,r,o,s){let i=[];for(let c of n){let a=t.length,l=new Array(a).fill(0),f=Array.from({length:a},()=>new Array(a).fill(0));for(let w=0;w<r;w++){let d=e.filter(g=>g.strata===w),b=Array.from(new Set(d.filter(g=>g.status!==s).map(g=>g.time))).sort((g,y)=>g-y),N=new Array(a).fill(1),v=new Array(a).fill(0),E=[];for(let g of b){let y=new Array(a).fill(0),x=new Array(a).fill(0),M=new Array(a).fill(0);for(let q of d)q.time>=g&&y[q.group]++;for(let q of d)q.time===g&&q.status!==s&&(q.status===c?x[q.group]++:M[q.group]++);let R=N.slice(),T=v.slice(),S=y.map((q,$)=>q===0?0:q/R[$]),A=y.map((q,$)=>q===0?0:q*(1-T[$])/R[$]),C=x.reduce((q,$)=>q+$,0),L=S.reduce((q,$)=>q+$,0),k=L===0?0:C/L,F=E.length===0?1:E[E.length-1].Gafter,I=F-k,O=A.reduce((q,$)=>q+$,0);for(let q=0;q<a;q++){let $=F**o;O>0&&(l[q]+=$*(x[q]-A[q]*C/O)),y[q]>0?(N[q]=N[q]*(1-(x[q]+M[q])/y[q]),v[q]+=R[q]*x[q]/y[q]):N[q]=0}E.push({time:g,Y:y,d1:x,d2:M,Sbefore:R,Safter:N.slice(),Fbefore:T,h:S,R:A,dF0:k,Gbefore:F,Gafter:I})}for(let g=0;g<a;g++)for(let y=0;y<a;y++){let x=0;for(let M=0;M<a;M++)for(let R=0;R<E.length;R++){let T=E[R],S=T.h.reduce((J,Z)=>J+Z,0);if(!(S>0)||!(T.h[M]>0))continue;let A=T.Gbefore**o,C=A*T.h[g]*((g===M?1:0)-T.h[M]/S),L=A*T.h[y]*((y===M?1:0)-T.h[M]/S),k=0,F=0;for(let J=R+1;J<E.length;J++){let Z=E[J],Ae=Z.h.reduce((U,W)=>U+W,0);if(!(Ae>0))continue;let ne=Z.Gbefore**o,D=ne*Z.h[g]*((g===M?1:0)-Z.h[M]/Ae),Y=ne*Z.h[y]*((y===M?1:0)-Z.h[M]/Ae),j=Z.Gbefore===0?0:Z.dF0/Z.Gbefore;k+=D*j,F+=Y*j}let I=T.Safter[M]===0?0:T.Gafter/T.Safter[M],O=C+(1-I)*k,q=L+(1-I)*F,$=-I*k,_=-I*F,P=T.Y[M]===0?0:T.Sbefore[M]*T.d2[M]/T.Y[M],K=(O*q*T.dF0+$*_*P)/T.h[M],X=T.d1.reduce((J,Z)=>J+Z,0),re=0;if(X>1&&T.Y[M]>0&&T.Sbefore[M]>0){let J=1/T.Sbefore[M];S>J&&(re=X*(X-1)/S*(T.Y[M]*J*J/(S-J))*(O*q/(T.h[M]*T.h[M])))}let z=0;T.d2[M]>1&&T.Y[M]>1&&(z=(T.d2[M]-1)/(T.Y[M]-1)*(P/T.h[M])*$*_),x+=K-re-z}f[g][y]+=x}}let m=a-1,u=NaN,h=NaN,p=f.slice(0,m).map(w=>w.slice(0,m));try{let w=nt(p,l.slice(0,m),{tol:1e-12});u=l.slice(0,m).reduce((d,b,N)=>d+b*w[N],0),h=It(u,m,{lowerTail:!1})}catch{u=NaN,h=NaN}i.push({cause:vt(c),stat:u,df:m,pValue:h})}return i}function Dv(e,t){let n=Array.from(new Set(t.filter(s=>!Number.isNaN(s)))).sort((s,i)=>s-i),r=[],o=[];for(let s of e.curves){let i=[],c=[];for(let a of n){let l=-1;for(let f=0;f<s.time.length;f++)s.time[f]<=a&&(l=f);a<0||l<0||a>s.time[s.time.length-1]?(i.push(NaN),c.push(NaN)):(i.push(s.est[l]),c.push(s.var[l]))}r.push(i),o.push(c)}return{curveNames:e.curves.map(s=>s.name),times:n,est:r,var:o}}function _n(e,t=e){return Array.from({length:e},()=>new Array(t).fill(0))}function Pv(e,t,n,r){for(let o=0;o<t.length;o++)for(let s=0;s<n.length;s++)e[o][s]+=r*t[o]*n[s]}function Uc(e,t){return De(e.map((n,r)=>n*t[r]))}function Vv(e){return e.every(t=>t.every(Number.isFinite))}function $v(e,t,n,r={}){B(r,Lv,"crr");let{cov2:o,tf:s,failcode:i=1,cencode:c=0,gtol:a=1e-6,maxiter:l=10,init:f,variance:m=!0,confLevel:u=.95,names:h}=r;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("'cov1' must have one row per observation");if(o!==void 0&&o.length!==e.length)throw new RangeError("'cov2' must have one row per observation");if(o!==void 0&&s===void 0)throw new TypeError("'tf' is required when 'cov2' is supplied");if(!(a>0)||!Number.isFinite(a))throw new RangeError("'gtol' must be finite and positive");if(!Number.isInteger(l)||l<0)throw new RangeError("'maxiter' must be a non-negative integer");if(!(u>0&&u<1))throw new RangeError("'confLevel' must be between 0 and 1");let p=n?.[0]?.length??0,w=o?.[0]?.length??0,d=p+w;if(d===0)throw new RangeError("at least one covariate is required");if(n!==void 0&&n.some(W=>W.length!==p))throw new RangeError("rows of 'cov1' must have equal length");if(o!==void 0&&o.some(W=>W.length!==w))throw new RangeError("rows of 'cov2' must have equal length");if(f!==void 0&&f.length!==d)throw new RangeError("'init' must have one value per coefficient");if(h!==void 0&&h.length!==d)throw new RangeError("'names' must have one name per coefficient");let b=[];for(let W=0;W<e.length;W++)if(!(Number.isNaN(e[W])||Ea(t[W])||n!==void 0&&n[W].some(Number.isNaN)||o!==void 0&&o[W].some(Number.isNaN))){if(!Number.isFinite(e[W]))throw new RangeError("'time' must contain only finite values");if(n!==void 0&&!n[W].every(Number.isFinite)||o!==void 0&&!o[W].every(Number.isFinite))throw new RangeError("covariates must be finite");b.push(W)}let N=e.length-b.length;if(b.length===0)throw new RangeError("no complete observations");let v=b.map(W=>e[W]),E=tn([...b.map(W=>t[W]),c,i]),g=E.slice(0,b.length),y=E[E.length-2],x=E[E.length-1],M=b.map(W=>n===void 0?[]:Array.from(n[W])),R=b.map(W=>o===void 0?[]:Array.from(o[W])),T=Array.from(new Set(v.filter((W,te)=>g[te]===x))).sort((W,te)=>W-te);if(T.length===0)throw new RangeError("no observations of 'failcode'");let S=w===0?[[0]]:s(T).map(W=>Array.from(W));if(w>0&&(S.length!==T.length||S.some(W=>W.length!==w)||!Vv(S)))throw new RangeError("'tf' must return a finite matrix with one row per unique failure time and one column per cov2 column");let A=T.map((W,te)=>v.map((fe,Re)=>[...M[Re],...R[Re].map((Ce,be)=>Ce*(S[te][be]??0))])),C=v.reduce((W,te)=>Math.max(W,Math.abs(te)),0),L=C>0&&C<1?2**Math.min(1023,Math.ceil(-Math.log2(C))):1,k=mr(v.map(W=>W*L),g.map(W=>W===y?1:0)),F=W=>{let te=W*L,fe=1;for(let Re=0;Re<k.time.length&&!(k.time[Re]>=te);Re++)fe=k.surv[Re];return fe},I=T.map(W=>v.map((te,fe)=>{if(te>=W)return 1;if(g[fe]===y||g[fe]===x)return 0;let Re=F(te);return Re===0?0:F(W)/Re}));for(let W=0;W<T.length;W++)for(let te=0;te<v.length;te++)v[te]===T[W]&&g[te]===x&&(I[W][te]=1);let O=W=>{let te=Array.from({length:d},()=>[]),fe=Array.from({length:d},()=>Array.from({length:d},()=>[])),Re=[],Ce=_n(T.length,d),be=[],me=[],ke=[];for(let Oe=0;Oe<T.length;Oe++){let Ke=T[Oe],ze=v.map((ie,Te)=>ie===Ke&&g[Te]===x?Te:-1).filter(ie=>ie>=0),et=ze.length,Ue=A[Oe],Xe=Ue.map(ie=>Uc(W,ie)),pe=I[Oe].slice();for(let ie of ze)pe[ie]=1;let oe=Math.max(...Xe.map((ie,Te)=>pe[Te]>0?ie:-1/0));if(oe===-1/0)throw new Error("empty Fine-Gray risk set");let we=oe<500&&oe>-500,ge=we?Xe.map((ie,Te)=>pe[Te]*Math.exp(ie)):Xe.map((ie,Te)=>pe[Te]*Math.exp(ie-oe)),ce=De(ge),tt=Array.from({length:d},(ie,Te)=>De(ge.map((Be,$e)=>Be*Ue[$e][Te])));if(!(ce>0))throw new Error("empty Fine-Gray risk set");let G=tt.map(ie=>ie/ce),Me=Array.from({length:d},(ie,Te)=>{let Be=Ue[0][Te],$e=0,st=0;for(let mt of Ue)$e=Math.max($e,Math.abs(mt[Te]-Be)),st=Math.max(st,Math.abs(mt[Te]));return $e>0&&st/$e>1e6}).some(Boolean),Ne=Me?Array.from({length:d},(ie,Te)=>Ue.map(Be=>Be[Te]-Ue[0][Te])):[],Ee=Me?Ne.map(ie=>De(ie.map((Te,Be)=>ge[Be]*Te))/ce):[],ee=Me?void 0:Array.from({length:d},(ie,Te)=>Array.from({length:d},(Be,$e)=>De(ge.map((st,mt)=>st*Ue[mt][Te]*Ue[mt][$e]))));me.push(G),ke.push(ge.map(ie=>ie/ce));let ve=we?et/ce:et*Math.exp(-oe)/ce;be.push(ve),Re.push(De(ze.map(ie=>Uc(W,Ue[ie])))-et*(we?Math.log(ce):oe+Math.log(ce)));for(let ie=0;ie<d;ie++){let Be=De(ze.map($e=>Ue[$e][ie]))-et*G[ie];te[ie].push(Be),Ce[Oe][ie]=Be}for(let ie=0;ie<d;ie++)for(let Te=0;Te<d;Te++){let Be=Me?De(ge.map(($e,st)=>$e*(Ne[ie][st]-Ee[ie])*(Ne[Te][st]-Ee[Te])))/ce:ee[ie][Te]/ce-G[ie]*G[Te];fe[ie][Te].push(et*Be)}}let de=_n(v.length,d);for(let Oe=0;Oe<T.length;Oe++){let Ke=A[Oe],ze=ke[Oe],et=me[Oe],Ue=T[Oe];for(let Xe=0;Xe<v.length;Xe++){let pe=v[Xe]===Ue&&g[Xe]===x?1:0,oe=v.filter((ge,ce)=>ge===Ue&&g[ce]===x).length,we=pe-ze[Xe]*oe;for(let ge=0;ge<d;ge++)de[Xe][ge]+=(Ke[Xe][ge]-et[ge])*we}}let Fe=te.map(De),xe=fe.map(Oe=>Oe.map(De));return{loglik:De(Re),score:Fe,inf:xe,bfitj:be,res:Ce,eta:de,means:me,riskFactors:ke,zByTime:A}},q=f===void 0?new Array(d).fill(0):Array.from(f);if(!q.every(Number.isFinite))throw new RangeError("'init' must contain finite values");let $=q.slice(),_=O($),P=!1,K=0,X=(W,te)=>Math.max(...W.score.map((fe,Re)=>Math.abs(fe)*Math.max(Math.abs(te[Re]),1)))<a*Math.max(Math.abs(W.loglik),1);for(;;){if(X(_,$)){P=!0;break}if(K>=l)break;let W=nt(_.inf,_.score,{tol:0});$=$.map((te,fe)=>te+W[fe]),K++,_=O($)}let re=nt(_.inf),z=_n(d);if(m){let W=_n(d),te=v.length,fe=Array.from(new Set(v)).sort((me,ke)=>me-ke),Re=fe.map(me=>{let ke=v.filter(Fe=>Fe>=me).length,de=v.filter((Fe,xe)=>Fe===me&&g[xe]===y).length;return ke===0?0:de/ke}),Ce=_n(te,d);for(let me=0;me<fe.length;me++){let ke=fe[me],de=new Array(d).fill(0);for(let xe=0;xe<te;xe++)if(!(!(v[xe]<ke)||g[xe]===y||g[xe]===x))for(let Pe=0;Pe<T.length;Pe++){if(T[Pe]<ke)continue;let Oe=_.riskFactors[Pe][xe],Ke=v.filter((Ue,Xe)=>Ue===T[Pe]&&g[Xe]===x).length,ze=_.zByTime[Pe][xe],et=_.means[Pe];for(let Ue=0;Ue<d;Ue++)de[Ue]+=(ze[Ue]-et[Ue])*Oe*Ke}let Fe=v.filter(xe=>xe>=ke).length;if(Fe!==0)for(let xe=0;xe<te;xe++){let Pe=v[xe]===ke&&g[xe]===y?1:0,Oe=v[xe]>=ke?Re[me]:0;for(let Ke=0;Ke<d;Ke++)Ce[xe][Ke]+=de[Ke]/Fe*(Pe-Oe)}}for(let me=0;me<te;me++)Pv(W,_.eta[me].map((ke,de)=>ke+Ce[me][de]),_.eta[me].map((ke,de)=>ke+Ce[me][de]),1);z=_n(d);let be=Ch(re,W);z=Ch(be,re)}else z=_n(d).map(W=>W.map(()=>NaN));let J=m?z.map((W,te)=>Math.sqrt(W[te])):void 0,Z=J===void 0?void 0:$.map((W,te)=>W/J[te]),Ae=Z===void 0?void 0:Z.map(W=>2*se(-Math.abs(W))),ne=m?le((1+u)/2):void 0,D=ne===void 0?void 0:$.map((W,te)=>Math.exp(W-ne*J[te])),Y=ne===void 0?void 0:$.map((W,te)=>Math.exp(W+ne*J[te])),j=m?$.map(Math.exp):void 0,U=h===void 0?[...Array.from({length:p},(W,te)=>`cov1${te+1}`),...Array.from({length:w},(W,te)=>`cov2${te+1}*tf${te+1}`)]:Array.from(h);return Q("crr",{coefficients:$,names:U,var:z,...J===void 0?{}:{se:J,z:Z,pValue:Ae,lower:D,upper:Y,expCoef:j},loglik:_.loglik,loglikNull:O(new Array(d).fill(0)).loglik,logtest:{statistic:2*(_.loglik-O(new Array(d).fill(0)).loglik),df:d},score:_.score,inf:m?_.inf:_n(d).map(W=>W.map(()=>NaN)),invinf:m?re:_n(d).map(W=>W.map(()=>NaN)),converged:P,iterations:K,n:b.length,nMissing:N,uftime:T,bfitj:_.bfitj,res:m?_.res:null,tfs:S,warnings:[],covariateDimensions:[p,w]})}function Ch(e,t){let n=_n(e.length,t[0]?.length??0);for(let r=0;r<e.length;r++)for(let o=0;o<(t[0]?.length??0);o++)for(let s=0;s<t.length;s++)n[r][o]+=e[r][s]*t[s][o];return n}function zv(e,t,n){let[r,o]=e.covariateDimensions;if(r>0&&t===void 0)throw new TypeError("'cov1' is required by this fit");if(o>0&&n===void 0)throw new TypeError("'cov2' is required by this fit");if(r>0&&t.some(a=>a.length!==r))throw new RangeError("'cov1' has the wrong number of columns");if(o>0&&n.some(a=>a.length!==o))throw new RangeError("'cov2' has the wrong number of columns");if(r>0&&o>0&&t.length!==n.length)throw new RangeError("'cov1' and 'cov2' must have the same number of rows");let s=r>0?t.length:n.length,i=o===0?e.uftime.map(()=>[0]):e.tfs;return e.uftime.map((a,l)=>{let f=[a];for(let m=0;m<s;m++){let u=0;for(let h=0;h<=l;h++){let p=[...r===0?[]:t[m],...o===0?[]:n[m].map((v,E)=>v*i[h][E])],w=e.bfitj[h],d=Uc(e.coefficients,p),b=w*Math.exp(d),N=Number.isFinite(b)&&(b!==0||w===0)?b:w===0?0:Math.exp(Math.log(w)+d);u+=N}f.push(-Math.expm1(-u))}return f})}var jv=new Set(["type","time2","dist","weights","offset","strata","scale","init","df","control","intercept","confLevel"]),Wv=["maxiter","relTolerance","tolerChol"],Bv=["type","p","newX","newStrata","newOffset","seFit"],Kv=["type","weighted"],Yc=new WeakMap,Uv=Math.log(2*Math.PI),Hv=e=>Number(e),Yv=e=>e<-Math.LN2?Math.log1p(-Math.exp(e)):Math.log(-Math.expm1(e));function zh(e,t){return t===-1/0?e:e<t?NaN:e+Yv(t-e)}function Gv(e,t){if(e===t)return 0;let n=Math.max(e,t),r=Math.min(e,t),o=n+Math.log(-Math.expm1(r-n));return(e>t?1:-1)*Math.exp(o)}function Fh(e,t){return t===0?0:Number.isFinite(t)?Math.sign(t)*Math.exp(e+Math.log(Math.abs(t))):Math.exp(e)*t}function Xv(e,t,n){let r=t-e;return Number.isFinite(r)?r/n:t/n-e/n}function jh(e,t,n){let r=t-e;if(Number.isFinite(r))return Math.log(r)-Math.log(n);let o=Math.max(Math.abs(e),Math.abs(t));return Math.log(o)+Math.log(t/o-e/o)-Math.log(n)}function Qv(e,t,n){let r=jh(e,t,n),o=r>Math.log(Number.MAX_VALUE)?1/0:Math.exp(r);if(o<1e-8){let s=t-e;return dn(e,t)+s*(o/24)}return o>50?t-n*r-n*Math.log1p(-Math.exp(-o)):e-n*(r-Math.log(Math.expm1(o)))}function Zv(e,t,n){let r=jh(e,t,n);if(r<Math.log(1e-8))return r-1;let o=r>Math.log(Number.MAX_VALUE)?1/0:Math.exp(r);if(!Number.isFinite(o))return 0;let s=o>50?o+Math.log1p(-Math.exp(-o)):Math.log(Math.expm1(o)),i=r-s,c=o>50?r-Math.log1p(-Math.exp(-o)):i+o;return zh(-Math.exp(i),-Math.exp(c))}function Jv(e){return e<-36?e:-Math.log1p(Math.exp(-e))}function eN(e){return e>36?-e:-Math.log1p(Math.exp(e))}function Oh(e,t,n){if(e===-1/0)return-1/0;if(e===1/0)return 0;if(t==="extreme"||t==="weibull"||t==="exponential"||t==="rayleigh"){let r=Math.exp(e);return r===0?e:Math.log(-Math.expm1(-r))}return t==="logistic"||t==="loglogistic"?Jv(e):t==="gaussian"||t==="loggaussian"||t==="lognormal"?se(e,{logP:!0}):Qt(e,n,{logP:!0})}function Dh(e,t,n){return e===-1/0?0:e===1/0?-1/0:t==="extreme"||t==="weibull"||t==="exponential"||t==="rayleigh"?-Math.exp(e):t==="logistic"||t==="loglogistic"?eN(e):t==="gaussian"||t==="loggaussian"||t==="lognormal"?se(e,{lowerTail:!1,logP:!0}):Qt(e,n,{lowerTail:!1,logP:!0})}function xa(e,t,n){return t==="extreme"||t==="weibull"||t==="exponential"||t==="rayleigh"?e===1/0?-1/0:e-Math.exp(e):t==="logistic"||t==="loglogistic"?-Math.abs(e)-2*Math.log1p(Math.exp(-Math.abs(e))):t==="gaussian"||t==="loggaussian"||t==="lognormal"?-.5*(e*e+Uv):pl(e,n,{log:!0})}function Hc(e,t,n){if(t==="extreme"||t==="weibull"||t==="exponential"||t==="rayleigh"){let o=Math.exp(e);return[1-o,o*o-3*o+1]}if(t==="logistic"||t==="loglogistic"){let o=Math.exp(-Math.abs(e));return[-Math.tanh(e/2),1-6*o/((1+o)*(1+o))]}if(t==="gaussian"||t==="loggaussian"||t==="lognormal")return[-e,e*e-1];let r=n+e*e;return[-(n+1)*e/r,(n+1)*((n+2)*e*e-n)/(r*r)]}function tN(e,t,n){if(t==="extreme"||t==="weibull"||t==="exponential"||t==="rayleigh")return-Math.exp(e);if(t==="logistic"||t==="loglogistic"){let r=Math.exp(-Math.abs(e));return-2*r/((1+r)*(1+r))}return t==="gaussian"||t==="loggaussian"||t==="lognormal"?-1:-(n+1)*(n-e*e)/((n+e*e)*(n+e*e))}function Ra(e,t,n,r,o,s,i){let c=e===-1/0?-1/0:(e-r)/o,a=t===1/0?1/0:(t-r)/o;if(n===1){let[L]=Hc(a,s,i),k=xa(a,s,i),F=tN(a,s,i);return{g:k-Math.log(o),e:-L/o,ee:F/(o*o),t:-a*L-1,tt:a*L+a*a*F,et:(L+a*F)/o}}let l=Oh(a,s,i),f=Oh(c,s,i),m=Dh(c,s,i),u=Dh(a,s,i),h=n===0?m:n===2?l:zh(c>0?m:l,c>0?u:f),w=c===-1/0?0:Math.exp(n===0&&(s==="extreme"||s==="weibull"||s==="exponential"||s==="rayleigh")?c:xa(c,s,i)-h),d=a===1/0?0:Math.exp(xa(a,s,i)-h),[b]=Hc(c,s,i),[N]=Hc(a,s,i),v=w===0||c===-1/0?0:w*b,E=d===0||a===1/0?0:d*N,g=c===-1/0||w===0?0:c*w,y=a===1/0||d===0?0:a*d,x=c===-1/0||v===0?0:c*c*v,M=a===1/0||E===0?0:a*a*E,R=-(d-w)/o,T=(E-v)/(o*o)-R*R,S=-(y-g),A=M-x-S*(1+S),C=((a===1/0||E===0?0:a*E)-(c===-1/0||v===0?0:c*v))/o-R*(1+S);return{g:h,e:R,ee:T,t:S,tt:A,et:C}}function nN(e){let t=e.length,n=e.map((r,o)=>[...r,...r.map((s,i)=>o===i?1:0)]);for(let r=0;r<t;r++){let o=r;for(let i=r+1;i<t;i++)Math.abs(n[i][r])>Math.abs(n[o][r])&&(o=i);if(!Number.isFinite(n[o][r])||Math.abs(n[o][r])<=1e-13*Math.max(Number.MIN_VALUE,...e.map((i,c)=>Math.abs(i[c]??0))))throw new Error("singular information matrix");[n[r],n[o]]=[n[o],n[r]];let s=n[r][r];for(let i=0;i<2*t;i++)n[r][i]/=s;for(let i=0;i<t;i++)if(i!==r){let c=n[i][r];for(let a=0;a<2*t;a++)n[i][a]-=c*n[r][a]}}return n.map(r=>r.slice(t))}function Wh(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 Bh(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 Ph(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}function ts(e,t){return e.map(n=>dr(n,t))}function dr(e,t){let n=e.reduce((r,o,s)=>r+o*t[s],0);return wo(e,t,n)}function Kh(e,t=1e-10){let n=t>0?Math.min(65536,1/Math.sqrt(t)):65536,r=Array.from({length:e[0].length},(o,s)=>{let i=e.map(a=>a[s]),c=0;for(let a of i)c=Math.max(c,Math.abs(a));if(c>n||c>0&&c<1/n){let a=qe([i]);return{values:a.groups[0],exponent:a.k}}return{values:i,exponent:0}});return{values:e.map((o,s)=>r.map(i=>i.values[s])),exponent:r.map(o=>o.exponent)}}function Vh(e,t,n){return e<=0?-1/0:e>=1?1/0:t==="extreme"||t==="weibull"||t==="exponential"||t==="rayleigh"?Math.log(-Math.log1p(-e)):t==="logistic"||t==="loglogistic"?Math.log(e/(1-e)):t==="gaussian"||t==="loggaussian"||t==="lognormal"?le(e):Wt(e,n)}function $h(e,t,n,r,o,s,i,c,a,l,f,m,u,h,p,w=1e-10){let d=e[0].length,b=d+(m===void 0?a:0),N=Kh(e,w),v=N.values,E=N.exponent,g=u.map((_,P)=>P<d?Ve(_,E[P]):_),y=(_,P)=>{let K=0,X=Array(b).fill(0),re=Array.from({length:b},()=>Array(b).fill(0)),z=Array.from({length:b},()=>Array(b).fill(0));for(let J=0;J<t.length;J++){let Z=c[J],Ae=dr(v[J],_.slice(0,d))+i[J],ne=m??Math.exp(_[d+Z]),D=Ra(n[J],r[J],o[J],Ae,ne,l,f),Y=s[J];K+=Y*D.g;let j=Array(b).fill(0);for(let U=0;U<d;U++)j[U]=v[J][U]*D.e;m===void 0&&(j[d+Z]=D.t);for(let U=0;U<b;U++){X[U]+=Y*j[U];for(let W=0;W<b;W++)z[U][W]+=Y*j[U]*j[W]}if(P){for(let U=0;U<d;U++){for(let W=0;W<d;W++)re[U][W]+=Y*v[J][U]*v[J][W]*D.ee;m===void 0&&(re[U][d+Z]+=Y*v[J][U]*D.et)}if(m===void 0){for(let U=0;U<d;U++)re[d+Z][U]+=Y*v[J][U]*D.et;re[d+Z][d+Z]+=Y*D.tt}}}return{ll:K,u:X,H:re,B:z}},x=_=>{let P=_.H.map(X=>X.map(re=>-re)),K=Wh(P,w);return{factor:P,rank:K}},M=_=>{let P=y(_,!0),K=x(P),X=K.rank>=0?Bh(K.factor,P.u):ts(nN(P.B),P.u);return{e:P,delta:X}},R=y(g,!0),T=R.ll,S=0,A=!1,C=!1,L;try{L=M(g)}catch{L={e:R,delta:Array(b).fill(0)}}let k=g.map((_,P)=>_+L.delta[P]);if(h===0){let _=x(R),P=Ph(_.factor),K=P.map(re=>re.slice()),X=P.map((re,z)=>re[z]===0?NaN:g[z]/Math.sqrt(Math.max(0,re[z])));return{b:g.map((re,z)=>z<d?Ve(re,-E[z]):re),ll:R.ll,iter:S,V:P.map((re,z)=>re.map((J,Z)=>Ve(J,(z<d?-E[z]:0)+(Z<d?-E[Z]:0)))),VScaled:K,xExponent:E,score:R.u.map((re,z)=>z<d?Ve(re,E[z]):re),z:X,warn:A}}for(let _=1;_<=h;_++){let P=y(k,!0);if(Number.isFinite(P.ll)&&Math.abs(1-T/P.ll)<=p){g.splice(0,g.length,...k),R=P,T=P.ll,S=_,C=!0;break}if(!Number.isFinite(P.ll)||P.ll<T){let K=k.slice();k=g.map((X,re)=>X+(K[re]-X)/3);for(let X=0;X<a;X++)m===void 0&&(k[d+X]=Math.max(k[d+X],g[d+X]-1.1));S=_;continue}if(g.splice(0,g.length,...k),R=P,T=P.ll,S=_,_<h)try{let K=M(g);k=g.map((X,re)=>X+K.delta[re])}catch{k=g.slice()}else try{let K=M(g),X=g.map((z,J)=>z+K.delta[J]),re=y(X,!1);Number.isFinite(re.ll)&&re.ll>=T&&g.splice(0,g.length,...X)}catch{}}S===h&&h>=2&&!C&&(A=!0);let F=y(g,!0),I=x(F),O=Ph(I.factor),q=O.map(_=>_.slice()),$=O.map((_,P)=>_[P]===0?NaN:g[P]/Math.sqrt(Math.max(0,_[P])));return{b:g.map((_,P)=>P<d?Ve(_,-E[P]):_),ll:T,iter:S,V:O.map((_,P)=>_.map((K,X)=>Ve(K,(P<d?-E[P]:0)+(X<d?-E[X]:0)))),VScaled:q,xExponent:E,score:F.u.map((_,P)=>P<d?Ve(_,E[P]):_),z:$,warn:A}}function rN(e,t,n=[],r={}){B(r,jv,"survreg"),B(Fn(r,"control"),Wv,"survreg","control");let o=r.type??"right",s=r.dist??"weibull",i=r.df??4,c=r.intercept??!0,a=e.length,l=t===void 0?Array(a).fill(1):Array.from(t,Number);if(l.length!==a||n.length!==0&&n.length!==a)throw new RangeError("variable lengths differ");if(o==="counting")throw new Error("start-stop type Surv objects are not supported");if(!["right","left","interval","interval2"].includes(o))throw new TypeError("invalid survival type");if(!["extreme","logistic","gaussian","weibull","exponential","rayleigh","loggaussian","lognormal","loglogistic","t"].includes(s))throw new RangeError("'arg' should be one of \u201Cextreme\u201D, \u201Clogistic\u201D, \u201Cgaussian\u201D, \u201Cweibull\u201D, \u201Cexponential\u201D, \u201Crayleigh\u201D, \u201Cloggaussian\u201D, \u201Clognormal\u201D, \u201Cloglogistic\u201D, \u201Ct\u201D");if((o==="interval"||o==="interval2")&&r.time2===void 0)throw new TypeError("time2 is required for interval data");if(r.time2!==void 0&&r.time2.length!==a)throw new RangeError("variable lengths differ (found for 'time2')");if(r.scale!==void 0&&(!Number.isFinite(r.scale)||r.scale<0))throw new RangeError("Invalid scale value");if(s==="t"&&(!(i>=3)||!Number.isFinite(i)))throw new RangeError("Degrees of freedom must be >=3");if(s!=="t"&&r.df!==void 0)throw new RangeError("Extreme value distribution has no optional parameters");let f=["weibull","exponential","rayleigh","loggaussian","lognormal","loglogistic"].includes(s),m=r.scale!==void 0&&r.scale>0?r.scale:void 0,u=s==="exponential"||s==="rayleigh"?s==="exponential"?1:.5:m,h=r.time2,p=r.weights??Array(a).fill(1),w=r.offset??Array(a).fill(0),d=r.strata??Array(a).fill("1");for(let[G,ue]of[[p,"weights"],[w,"offset"],[d,"strata"]])if(G.length!==a)throw new RangeError(`variable lengths differ (found for '${ue}')`);let b=[],N=[],v=[],E=[],g=[],y=[],x=[],M=[],R=[],T=(o==="right"||o==="left")&&l.some(G=>Number(G)===2),S=G=>{R.includes(G)||R.push(G)},A=[...new Set(d.map(String))].sort(),C=d.map(G=>A.indexOf(String(G))),L=[];if(u!==void 0&&A.length>1)throw new Error("The scale argument is not valid with multiple strata");for(let G=0;G<a;G++){let ue=n.length===0?[]:Array.from(n[G]);if((o==="interval2"?Number.isNaN(e[G])&&Number.isNaN(h[G]):o==="interval"&&Hv(l[G])===3?Number.isNaN(e[G])||Number.isNaN(h[G]):Number.isNaN(e[G]))||o!=="interval2"&&Number.isNaN(l[G])||Number.isNaN(p[G])||Number.isNaN(w[G])||Number.isNaN(d[G])||ue.some(Number.isNaN))continue;if(!(p[G]>0))throw new RangeError("Invalid weights, must be >0");if(ue.some(ve=>!Number.isFinite(ve)))throw new Error("data contains an infinite predictor");let Ne=e[G],Ee=e[G],ee=Number(l[G]);if(o==="right")if(T){if(ee!==1&&ee!==2){S("Invalid status value, converted to NA");continue}ee===1&&(Ee=1/0),ee=ee===2?1:0}else{if(ee!==0&&ee!==1){S("Invalid status value, converted to NA");continue}ee===0&&(Ee=1/0),ee=ee===1?1:0}else if(o==="left")if(T){if(ee!==1&&ee!==2){S("Invalid status value, converted to NA");continue}ee===1&&(Ne=-1/0),ee=ee===2?1:2}else{if(ee!==0&&ee!==1){S("Invalid status value, converted to NA");continue}ee===0&&(Ne=-1/0),ee=ee===1?1:2}else if(o==="interval"){if(Ee=h[G],ee===0)Ne=e[G],Ee=1/0;else if(ee===1)Ee=Ne;else if(ee===2)Ee=e[G],Ne=-1/0;else if(ee!==3){S("Invalid status value, converted to NA");continue}if(ee===3&&Ne>Ee){S("Invalid interval: start > stop, NA created");continue}if(ee===3&&Ne===Ee)throw new Error("initial iteration failed (use starting estimates?)")}else if(o==="interval2"){let ve=e[G],ie=h[G];if(Number.isNaN(ve)&&Number.isNaN(ie))continue;if(ve>ie){S("Invalid interval: start > stop, NA created");continue}ve===ie?(Ne=ve,Ee=ie,ee=1):!Number.isFinite(ve)||ve===0?(Ne=-1/0,Ee=ie,ee=2):Number.isFinite(ie)?(Ne=ve,Ee=ie,ee=3):(Ne=ve,Ee=1/0,ee=0)}if(f){if((Number.isFinite(Ne)&&Ne<0||Number.isFinite(Ee)&&Ee<0)&&S("NaNs produced"),Ne===1/0||Ee===-1/0||Number.isFinite(Ne)&&Ne<=0||Number.isFinite(Ee)&&Ee<=0)throw new Error("Invalid survival times for this distribution");Number.isFinite(Ne)&&(Ne=Math.log(Ne)),Number.isFinite(Ee)&&(Ee=Math.log(Ee))}b.push(G),N.push(ee===1?Ee:Number.isFinite(Ne)&&Number.isFinite(Ee)?dn(Ne,Ee):Number.isFinite(Ne)?Ne:Ee),v.push(Ne),E.push(Ee),g.push(ee),y.push(p[G]),x.push(w[G]),M.push(ue),L.push(C[G])}if(b.length===0)throw new Error("initial iteration failed (use starting estimates?)");if(!g.some(G=>G===1||G===3))throw new Error("initial iteration failed (use starting estimates?)");if(N.some(G=>!Number.isFinite(G)))throw new Error("initial iteration failed (use starting estimates?)");if(M.some(G=>G.length!==M[0].length))throw new TypeError("all rows of the matrix must have the same length");let k=M[0].length,F=k+(c?1:0),I=M.map(G=>c?[1,...G]:G),O=A.length,q=N.map((G,ue)=>G-x[ue]),$=y.reduce((G,ue)=>G+ue,0),_=q.reduce((G,ue,Me)=>G+ue*y[Me],0)/$,P=q.reduce((G,ue,Me)=>G+y[Me]*(ue-_)**2,0)/$,K=Math.max(...q)-Math.min(...q),X=s==="extreme"||s==="weibull"||s==="exponential"||s==="rayleigh"?P/1.64:s==="logistic"||s==="loglogistic"?P/3.2:s==="t"?P*(i-2)/i:P,re=u??2*Math.sqrt(X);if(u===void 0&&(!(re>0)||!(K>0)))throw new Error("initial iteration failed (use starting estimates?)");let z=F+(u===void 0?O:0),J=Array(z).fill(0);if(c&&(J[0]=_),u===void 0)for(let G=0;G<O;G++)J[F+G]=Math.log(re);let Z=r.control??{},Ae=Z.maxiter??30,ne=Z.relTolerance??1e-9;if(!Number.isInteger(Ae)||Ae<0||!Number.isFinite(ne)||ne<0)throw new RangeError("invalid control");if(Z.tolerChol!==void 0&&(!Number.isFinite(Z.tolerChol)||Z.tolerChol<0))throw new RangeError("invalid control");let D=I.map(()=>[1]),Y=I.every(G=>G.length===1&&G[0]===1);if(r.init!==void 0&&r.init.length!==F&&r.init.length!==z)throw new Error("Wrong length for initial parameters");let U=N.map((G,ue)=>Ra(v[ue],E[ue],g[ue],G,u??re,s,i)).map((G,ue)=>{let Me=-y[ue]*G.ee;return Me>0&&Number.isFinite(Me)?{w:Me,r:N[ue]-G.e/G.ee-x[ue]}:{w:0,r:0}}),W=U.reduce((G,ue)=>G+ue.w,0);if(!(W>0))throw new Error("initial iteration failed (use starting estimates?)");let te=U.reduce((G,ue)=>G+ue.w*ue.r,0)/W,fe=[r.init!==void 0&&Y?r.init[0]:te,...u===void 0?Array(O).fill(Math.log(re)):[]];if(Y&&r.init!==void 0&&r.init.length===z)for(let G=0;G<O;G++)fe[1+G]=r.init[1+G];let Re=Y?Ae:Math.max(Ae,1e3),Ce=$h(D,N,v,E,g,y,x,L,O,s,i,u,fe,Re,ne,Z.tolerChol??1e-10),be=J.slice();if(u===void 0)for(let G=0;G<O;G++)be[F+G]=Ce.b[1+G];if(!Y&&r.init!==void 0){for(let G=0;G<F;G++)be[G]=r.init[G];if(r.init.length===z)for(let G=F;G<z;G++)be[G]=r.init[G]}else if(!Y&&F>0){let G=M.map((Ee,ee)=>{let ve=u??Math.exp(Ce.b[1+L[ee]]),ie=Ra(v[ee],E[ee],g[ee],N[ee],ve,s,i),Te=-y[ee]*ie.ee;return Te>0&&Number.isFinite(Te)?{w:Te,r:N[ee]-ie.e/ie.ee-x[ee]}:{w:0,r:0}}),ue=Kh(I,Z.tolerChol??1e-10),Me=Array.from({length:F},(Ee,ee)=>Array.from({length:F},(ve,ie)=>ue.values.reduce((Te,Be,$e)=>Te+G[$e].w*Be[ee]*Be[ie],0))),Ne=Array.from({length:F},(Ee,ee)=>ue.values.reduce((ve,ie,Te)=>ve+G[Te].w*ie[ee]*G[Te].r,0));try{let Ee=Me.map(ie=>ie.slice()),ve=Wh(Ee,Z.tolerChol??1e-10)>=0?Bh(Ee,Ne):ts(nt(Me),Ne);be.splice(0,F,...ve.map((ie,Te)=>Ve(ie,-ue.exponent[Te])))}catch{}}let me=Y?Ce:$h(I,N,v,E,g,y,x,L,O,s,i,u,be,Ae,ne,Z.tolerChol??1e-10),ke=f?b.reduce((G,ue,Me)=>G+(g[Me]===1?y[Me]*Math.log(e[ue]):0),0)*-1:0,de=me.V.map(G=>G.slice()),Fe=me.b.slice(0,F).map((G,ue)=>Number.isNaN(me.z[ue])?NaN:G),xe=Fe.map((G,ue)=>(de[ue]?.[ue]??0)===0?0:Math.sqrt(Math.max(0,de[ue]?.[ue]??0))),Oe=Fe.map((G,ue)=>me.z[ue]).map(G=>2*se(-Math.abs(G))),Ke=u!==void 0?[u]:Array.from({length:O},(G,ue)=>Math.exp(me.b[F+ue])),ze=Array.from({length:F},(G,ue)=>I.reduce((Me,Ne)=>Me+Ne[ue],0)/I.length),et=I.map((G,ue)=>dr(G,me.b.slice(0,F))+x[ue]),Ue=2*(me.ll+ke-(Ce.ll+ke)),Xe=F+(u===void 0?O:0)-(1+(u===void 0?O:0)),pe=[...Fe.map((G,ue)=>ue===0&&c?"(Intercept)":`x${ue+(c?0:1)}`),...u===void 0?O===1?["Log(scale)"]:A:[]],oe=[...Fe,...u===void 0?me.b.slice(F):[]],we=oe.map((G,ue)=>(de[ue]?.[ue]??0)===0?0:Math.sqrt(Math.max(0,de[ue]?.[ue]??0))),ge=oe.map((G,ue)=>me.z[ue]),ce=ge.map(G=>2*se(-Math.abs(G))),tt={coefficients:Fe,coefNames:pe.slice(0,F),scale:Ke,table:{value:oe,se:we,z:ge,p:ce},strataLevels:A.map(G=>d.find(ue=>String(ue)===G)),hasOffset:r.offset!==void 0,icoef:u===void 0||b.length===1?Ce.b.slice():[Ce.b[0],Math.log(u)],var:de,loglik:[Ce.ll+ke,me.ll+ke],iter:me.iter,df:F+(u===void 0?O:0),idf:1+(u===void 0?O:0),dfResidual:b.length-(F+(u===void 0?O:0)),n:b.length,linearPredictors:et,means:ze,score:me.score.slice(),tableRowNames:pe,value:oe,se:we,z:ge,p:ce,chi:Ue,chiDf:Xe,chiP:It(Ue,Xe,{lowerTail:!1}),dist:s,distributionDf:i,parms:i,intercept:c,x:I,offset:x,rows:b,weights:y,strata:L,y:N,lower:v,upper:E,status:g,type:o,warnings:[...R,...me.warn||Ce.warn?["Ran out of iterations and did not converge"]:[],...s==="exponential"&&r.scale!==void 0&&r.scale>0?["Exponential has a fixed scale, user specified value ignored"]:[]]};return Yc.set(tt,{V:me.VScaled,xExponent:me.xExponent}),Q("survreg",tt)}function oN(e,t={}){B(t,Bv,"survregPredict");let n=t.type??"lp",r=t.newX===void 0?e.x:t.newX.map(h=>e.intercept?[1,...h]:Array.from(h)),o=e.hasOffset?t.newOffset??(t.newX===void 0?e.offset:Array(r.length).fill(0)):Array(r.length).fill(0);if(o.length!==r.length)throw new RangeError("variable lengths differ (found for 'newOffset')");let s=e.strataLevels.map(String),i=t.newStrata?.map(h=>s.indexOf(String(h)))??(t.newX===void 0?e.strata.slice():r.map(()=>0));if(e.strataLevels.length>1&&t.newStrata===void 0&&t.newX!==void 0)throw new Error("object 's' not found");if(t.newStrata!==void 0&&t.newStrata.length!==r.length)throw new RangeError("variable lengths differ (found for 'newStrata')");if(t.newX!==void 0&&r.some(h=>h.length!==e.coefficients.length))throw new RangeError("newX has the wrong number of columns");if(i.some(h=>h<0))throw new Error("unknown stratum in newStrata");if(!["lp","linear","link","response","quantile","uquantile"].includes(n))throw new RangeError("invalid prediction type");let c=t.p===void 0?[.1,.9]:typeof t.p=="number"?[t.p]:Array.from(t.p),a=["weibull","exponential","rayleigh","loggaussian","lognormal","loglogistic"].includes(e.dist),l=r.map((h,p)=>{if(h.some(Number.isNaN))return n==="quantile"||n==="uquantile"?c.map(()=>NaN):NaN;let w=t.newX===void 0&&t.newOffset===void 0?e.linearPredictors[p]:dr(h,e.coefficients)+o[p],d=e.scale.length===1?e.scale[0]:e.scale[i[p]];return n==="lp"||n==="linear"||n==="link"?w:n==="response"?a?Math.exp(w):w:c.map(b=>{let N=w+d*Vh(b,e.dist,e.distributionDf);return n==="uquantile"?N:a?Math.exp(N):N})}),f=(n==="quantile"||n==="uquantile")&&c.length===1?l.map(h=>h[0]):l;if(!t.seFit)return f;let m=r.map((h,p)=>{if(h.some(Number.isNaN))return n==="quantile"||n==="uquantile"?c.map(()=>NaN):NaN;let w=t.newX===void 0&&t.newOffset===void 0?e.linearPredictors[p]:dr(h,e.coefficients)+o[p],d=e.scale.length===1?e.scale[0]:e.scale[i[p]],b=Yc.get(e),N=b?.V??e.var,v=E=>{let g=Array(N.length).fill(0);for(let y=0;y<h.length;y++)g[y]=b?Ve(h[y],-b.xExponent[y]):h[y];return N.length>h.length&&(g[h.length+i[p]]=E*d),Math.sqrt(Math.max(0,dr(g,ts(N,g))))};return n==="lp"||n==="linear"||n==="link"?v(0):n==="response"?a?Fh(w,v(0)):v(0):c.map(E=>{let g=Vh(E,e.dist,e.parms),y=v(g),x=w+d*g;return n==="uquantile"?y:a?Fh(x,y):y})}),u=(n==="quantile"||n==="uquantile")&&c.length===1?m.map(h=>h[0]):m;return{fit:f,seFit:u,warnings:[]}}function sN(e,t={}){B(t,Kv,"survregResiduals");let n=t.type??"response",r=e.distributionDf,o=e.coefficients.length,s=e.var.length,i=t.weighted??!1;if(!["response","deviance","working","dfbeta","dfbetas","ldcase","ldresp","ldshape","matrix"].includes(n))throw new RangeError("invalid residual type");let c=Yc.get(e),a=c?.V??e.var,l=e.x.map((f,m)=>{let u=e.linearPredictors[m],h=e.scale.length===1?e.scale[0]:e.scale[e.strata[m]],p=Ra(e.lower[m],e.upper[m],e.status[m],u,h,e.dist,r),w=i?e.weights[m]:1,d=f.map((R,T)=>c?Ve(R,-c.xExponent[T]):R),b=Array(s).fill(0);for(let R=0;R<o;R++)b[R]=d[R]*p.e;s>o&&(b[o+e.strata[m]]=p.t);let N=ts(a,b),v=N.map((R,T)=>c&&T<o?Ve(R,-c.xExponent[T]):R),E=dr(b,N);if(n==="matrix")return[p.g,p.e,p.ee,p.t,p.tt,p.et].map(R=>R*w);if(n==="working")return-p.e/p.ee*w;if(n==="dfbeta")return v.map(R=>R*w);if(n==="dfbetas")return N.map((R,T)=>R/Math.sqrt(a[T]?.[T]??NaN)*w);if(n==="ldcase")return E*w;if(n==="ldresp"||n==="ldshape"){let R=Array(s).fill(0);for(let T=0;T<o;T++)R[T]=n==="ldresp"?h*(-d[T]*p.ee):d[T]*p.et;return s>o&&(R[o+e.strata[m]]=n==="ldresp"?-h*p.et:p.tt),dr(R,ts(a,R))*w}let g=xa(0,e.dist,r)-Math.log(h),y=e.status[m]===3?dn(e.lower[m],e.upper[m]):e.y[m];e.status[m]===3&&(e.dist==="extreme"||e.dist==="weibull"||e.dist==="exponential"||e.dist==="rayleigh")&&(y=Qv(e.lower[m],e.upper[m],h));let x=e.status[m]===0?1:e.status[m]===2?-1:Math.sign(y-u);if(n==="deviance"){let R=e.status[m]===1?g:0;if(e.status[m]===3){let T=Xv(e.lower[m],e.upper[m],h);e.dist==="extreme"||e.dist==="weibull"||e.dist==="exponential"||e.dist==="rayleigh"?R=Zv(e.lower[m],e.upper[m],h):e.dist==="logistic"||e.dist==="loglogistic"?R=Math.log(Math.tanh(T/4)):e.dist==="gaussian"||e.dist==="loggaussian"||e.dist==="lognormal"?R=Math.log(2*se(T/2)-1):e.dist==="t"&&(R=Math.log(2*Qt(T/2,e.distributionDf)-1))}return x*Math.sqrt(Math.max(0,2*(R-p.g)))*w}let M=y-u;return(["weibull","exponential","rayleigh","loggaussian","lognormal","loglogistic"].includes(e.dist)?Gv(y,u):M)*w});return l}var aN=["n","power","r","type","nfractional","alpha","sidedTest"],iN=["n","power","r","type","nfractional","alpha","sidedTest"],cN=["n","power","r","type","nfractional","alpha","sidedTest"],lN=["n","power","r","type","nfractional","alpha","sidedTest"],fN=["n","power","r","type","nfractional","alpha","sidedTest"],mN=["n","power","r","type","nfractional","alpha","sidedTest"],uN=["sigLevel","alternative","strict","nullOddsRatio","errbound","tsmethod"],hN=["alpha"],pN=["alpha"],dN=["alpha"],gN=["alpha"],wN=["alpha"],bN=["nmax"],Fr=.05,mn=e=>le(e),Hn=e=>se(e),Cr=e=>Number.isFinite(e),qt=e=>Cr(e)&&e>=0&&e<=1,yn=e=>Cr(e)&&e>0;function Je(e,t,n,r){if(typeof e!="number")throw new TypeError(`'${t}' must be numeric`);if(!n(e))throw new RangeError(r)}function Or(e){Je(e,"alpha",qt,"'alpha' must be a probability in [0, 1]")}function vN(e,t){return[e*t/(1+t),e/(1+t)]}function ho(e,t,n,r,o,s,i={}){let{n:c,power:a}=i,l=i.r??1,f=i.alpha??Fr,m=i.nfractional??!1,u=e==="equivalence"?Se(i.type,["equivalence","equality"]):"equivalence",h=i.sidedTest??1;if(e==="equivalence"&&u!=="equivalence"&&u!=="equality")throw new RangeError("'type' should be one of \u201Cequivalence\u201D, \u201Cequality\u201D");if(Je(n,"treat",Cr,"'treat' must be finite"),Je(r,"control",Cr,"'control' must be finite"),t==="binary"?(Je(n,"treat",qt,"'treat' must be a probability in [0, 1]"),Je(r,"control",qt,"'control' must be a probability in [0, 1]")):Je(o,"sigma",yn,"'sigma' must be positive and finite"),Je(l,"r",yn,"'r' must be positive and finite"),Or(f),(e!=="equivalence"||u!=="equality")&&Je(s,"delta",C=>Cr(C)&&C>=0,"'delta' must be nonnegative and finite"),e==="superiority"&&h!==1&&h!==2)throw new RangeError("'sidedTest' must be 1 or 2");if(typeof m!="boolean")throw new TypeError("'nfractional' must be logical");nn([c,a],"exactly one of 'n' and 'power' must be NULL"),ye(a)||Je(a,"power",C=>qt(C)&&C>0&&C<1,"'power' must be strictly between 0 and 1"),c!=null&&Je(c,"n",C=>Cr(C)&&C>0,"'n' must be positive and finite");let p=n-r,w=t==="binary"?n*(1-n):o*o,d=t==="binary"?r*(1-r):o*o,b=e==="equivalence"&&u==="equality",N=b?void 0:s,v=e==="noninferiority"?p+N:e==="superiority"?p-N:b?p:N-Math.abs(p);if(e==="noninferiority"&&v<=0)throw new RangeError("Target power is not reachable under the specified hypotheses");let E=e==="superiority"?le(f/h,{lowerTail:!1}):b?le(f/2,{lowerTail:!1}):le(f,{lowerTail:!1}),g=a??.8,y=b||e!=="equivalence"?le(1-g,{lowerTail:!1}):le((1-g)/2,{lowerTail:!1}),x,M,R,T,S;if(ye(c))v===0?(R=1/0,T=1/0):(T=(E+y)**2*(w/l+d)/(v*v),R=l*T),m?(x=R,M=T):(M=Math.ceil(T),x=Math.ceil(e==="equivalence"&&t==="continuous"||e==="superiority"?l*M:R)),S=a;else{[R,T]=vN(c,l),x=m?R:Math.ceil(R),M=m?T:Math.ceil(T);let C=Math.sqrt(w/x+d/M);if(e==="equivalence"&&!b)S=Math.max(0,Hn((s-p)/C-mn(1-f))+Hn((s+p)/C-mn(1-f))-1);else if(b)S=Hn(Math.abs(p)/C-mn(1-f/2))+Hn(-Math.abs(p)/C-mn(1-f/2));else if(e==="noninferiority")S=Hn((p+s)/C-mn(1-f));else{let L=Math.abs(p-s)/C;S=Hn(L-E)+Hn(-L-E)}}return{nTotal:x+M,nTreat:x,nControl:M,nTotalUnrounded:R+T,nTreatUnrounded:R,nControlUnrounded:T,power:S,...N===void 0?{}:{delta:N},warnings:[]}}function NN(e,t,n,r={}){return B(r,aN,"epiSsequb"),Q("epiSsequb",ho("equivalence","binary",e,t,void 0,n??void 0,r))}function yN(e,t,n,r,o={}){B(o,iN,"epiSsequc");let s=ho("equivalence","continuous",e,t,n,r??void 0,o);return Q("epiSsequc",s)}function MN(e,t,n,r={}){return B(r,cN,"epiSsninfb"),Q("epiSsninfb",ho("noninferiority","binary",e,t,void 0,n,r))}function EN(e,t,n,r,o={}){return B(o,lN,"epiSsninfc"),Q("epiSsninfc",ho("noninferiority","continuous",e,t,n,r,o))}function xN(e,t,n,r={}){return B(r,fN,"epiSssupb"),Q("epiSssupb",ho("superiority","binary",e,t,void 0,n,r))}function RN(e,t,n,r,o={}){return B(o,mN,"epiSssupc"),Q("epiSssupc",ho("superiority","continuous",e,t,n,r,o))}function uo(e,t){let n=new Array(e+1).fill(0);if(t===0)return n[0]=1,n;if(t===1)return n[e]=1,n;if(t>.5)return uo(e,1-t).reverse();let r=Math.floor((e+1)*t);n[r]=Math.exp(e*Math.log1p(-t)+r*Math.log(t/(1-t))+Array.from({length:r},(o,s)=>Math.log(e-s)-Math.log(s+1)).reduce((o,s)=>o+s,0));for(let o=r;o>0;o--)n[o-1]=n[o]*(o/(e-o+1))*((1-t)/t);for(let o=r;o<e;o++)n[o+1]=n[o]*((e-o)/(o+1))*(t/(1-t));return n}function TN(e,t,n,r={}){B(r,uN,"powerPaired2x2");let o=r.sigLevel??Fr,s=Se(r.alternative,["two.sided","one.sided"]),i=r.strict??!1,c=r.nullOddsRatio??1,a=r.errbound??1e-6,l=Se(r.tsmethod,["central","minlike","blaker"]);if(Je(e,"pb",qt,"'pb' must be in [0, 1]"),Je(t,"pc",qt,"'pc' must be in [0, 1]"),Je(n,"npairs",y=>Number.isSafeInteger(y)&&y>=0&&y<=1e5,"'npairs' must be a nonnegative safe integer"),Or(o),Je(c,"nullOddsRatio",yn,"'nullOddsRatio' must be positive and finite"),Je(a,"errbound",y=>Cr(y)&&y>=0,"'errbound' must be nonnegative and finite"),e+t>1)throw new RangeError("probabilities must be finite, non-negative and not all 0");if(s!=="two.sided"&&s!=="one.sided")throw new RangeError("'alternative' should be one of \u201Ctwo.sided\u201D, \u201Cone.sided\u201D");if(!["central","minlike","blaker"].includes(l))throw new RangeError("'tsmethod' should be one of \u201Ccentral\u201D, \u201Cminlike\u201D, \u201Cblaker\u201D");let f=e+t,m=f===0?.5:e/f,u=c/(1+c),h=e>=t?1:-1,p=s==="two.sided"&&i&&l!=="blaker",w=s==="one.sided",d=uo(n,f),b=0,N=n;if(a>0){let y=0;for(;b<=n&&(y+=d[b])<=a/2;)b++;for(y=0;N>=0&&(y+=d[N])<=a/2;)N--}let v=0;for(let y=b;y<=N;y++){let x=d[y];if(x===0)continue;let M=uo(y,m),R=uo(y,u),T=0,S=0,A=new Array(y+1),C=new Array(y+1);for(let L=0;L<=y;L++)T+=R[L],A[L]=T;for(let L=y;L>=0;L--)S+=R[L],C[L]=S;for(let L=0;L<=y;L++){let k=A[L],F=C[L],I,O=u===.5&&o===1;w?I=O||(h>0?F<=o:k<=o):p?I=O||k<=o/2||F<=o/2:I=h>0?O?L>Math.floor(y/2):F<=o/2:O?L<Math.ceil(y/2):k<=o/2,I&&(v+=x*M[L])}}let E=`errbound= ${a===0?"0":a>=.001?String(a):`${a.toExponential(0).replace(/e([+-]?)(\\d+)$/,(y,x,M)=>`e${x||"+"}${M.padStart(2,"0")}`)}`}${e+t===1?" no non-informative pairs: pa+pd=0":""}`,g="Power for McNemar's Exact Test"+(p?", including power to reject in the wrong direction":"")+(l==="central"&&r.tsmethod!==void 0?`, using (tsmethod=${l})`:l==="minlike"?`, using (tsmethod=${l})`:l==="blaker"?`, using (tsmethod=${l})`:"");return Q("powerPaired2x2",{power:v,npairs:n,pb:e,pc:t,sigLevel:o,alternative:s,nullOddsRatio:c,note:E,method:g,warnings:[]})}function SN(e,t,n,r,o,s={}){B(s,pN,"powerCT");let i=s.alpha??Fr;Or(i);for(let[l,f]of[[e,"nE"],[t,"nC"]])Je(l,f,yn,`'${f}' must be positive and finite`);for(let[l,f]of[[n,"pE"],[r,"pC"]])Je(l,f,qt,`'${f}' must be in [0, 1]`);Je(o,"RR",yn,"'RR' must be positive and finite");let c=e/t,a=e*n+t*r;return Hn(Math.sqrt(c*a)*Math.abs(o-1)/(c*o+1)-mn(1-i/2))}function AN(e,t,n,r,o,s={}){B(s,hN,"ssizeCT");let i=s.alpha??Fr;Or(i),Je(e,"power",f=>qt(f)&&f>0&&f<1,"'power' must be strictly between 0 and 1"),Je(t,"k",yn,"'k' must be positive and finite"),Je(n,"pE",qt,"'pE' must be in [0, 1]"),Je(r,"pC",qt,"'pC' must be in [0, 1]"),Je(o,"RR",yn,"'RR' must be positive and finite");let c=mn(1-i/2)+mn(e),a=1/t*((t*o+1)/(o-1))**2*(c*c),l=t*n+r;return{nE:Math.ceil(a*t/l),nC:Math.ceil(a/l)}}function qN(e,t,n,r,o,s={}){B(s,gN,"powerEpi");let i=s.alpha??Fr;Or(i),Je(e,"n",yn,"'n' must be positive and finite"),Je(t,"theta",yn,"'theta' must be positive and finite"),Je(n,"p",qt,"'p' must be in [0, 1]"),Je(r,"psi",qt,"'psi' must be in [0, 1]"),Je(o,"rho2",qt,"'rho2' must be in [0, 1]");let c=e*Math.log(t)**2*n*(1-n)*r*(1-o);return Hn(Math.sqrt(c)-mn(1-i/2))}function kN(e,t,n,r,o,s={}){B(s,dN,"ssizeEpi");let i=s.alpha??Fr;Or(i),Je(e,"power",qt,"'power' must be in [0, 1]"),Je(t,"theta",yn,"'theta' must be positive and finite"),Je(n,"p",qt,"'p' must be in [0, 1]"),Je(r,"psi",qt,"'psi' must be in [0, 1]"),Je(o,"rho2",qt,"'rho2' must be in [0, 1]");let c=Math.log(t)**2*n*(1-n)*r*(1-o);return Math.ceil((mn(1-i/2)+mn(e))**2/c)}function IN(e,t,n,r,o={}){B(o,wN,"numDEpi");let s=o.alpha??Fr;Or(s),Je(e,"power",c=>qt(c)&&c>0&&c<1,"'power' must be strictly between 0 and 1"),Je(t,"theta",yn,"'theta' must be positive and finite"),Je(n,"p",qt,"'p' must be in [0, 1]"),Je(r,"rho2",qt,"'rho2' must be in [0, 1]");let i=Math.log(t)**2*n*(1-n)*(1-r);return Math.ceil((mn(1-s/2)+mn(e))**2/i)}function Uh(e){let t=new Array(e.length),n=0,r=0;for(let o=0;o<e.length;o++){let s=e[o]-r,i=n+s;r=i-n-s,n=i,t[o]=n}return t}function _N(e){let t=new Array(e.length),n=0,r=0;for(let o=e.length-1;o>=0;o--){let s=e[o]-r,i=n+s;r=i-n-s,n=i,t[o]=n}return t}function Hh(e,t,n,r,o,s){let i=0,c=0;c=r.slice(0,e+1).reduce((a,l)=>a+l,0);for(let a=e+1;a<n.length;a++){let l=t-a,f=l<0?1:l+1>=o.length?0:o[l+1],m=l<0?0:s[Math.min(l,s.length-1)];i+=n[a]*f,c+=r[a]*m}return[i,c]}function LN(e,t,n,r,o={}){B(o,bN,"ph2simon");let s=o.nmax??100;for(let[E,g]of[[e,"pu"],[t,"pa"],[n,"ep1"],[r,"ep2"]])Je(E,g,qt,`'${g}' must be in [0, 1]`);Je(s,"nmax",E=>Number.isInteger(E)&&E>=1&&E<=1e3,"nmax cannot exceed 1000");let i=Array.from({length:s+1},(E,g)=>uo(g,e)),c=Array.from({length:s+1},(E,g)=>uo(g,t)),a=i.map(Uh),l=c.map(Uh),f=i.map(_N),m=[];for(let E=2;E<=s;E++){let g;for(let y=1;y<E&&(g===void 0||y<g.enP0);y++){let x=E-y,M=i[y],R=c[y],T=l[x],S=a[y];for(let A=0;A<y;A++){let C=S[A],L=y+x*(1-C),k=A,F=E-1,I=-1;for(;k<=F;){let O=Math.floor((k+F)/2),[,q]=Hh(A,O,M,R,f[x],T);q<=r?(I=O,k=O+1):F=O-1}if(I>=0){let[O]=Hh(A,I,M,R,f[x],T);O>n&&(I=-1)}I>=0&&(g===void 0||L<g.enP0)&&(g={r1:A,n1:y,r:I,n:E,enP0:L,petP0:C})}}g&&m.push(g)}if(m.length===0)throw new Error(` No feasible solution found.
20
+ Increase maximum sample size. Current nmax value = ${s}.`);let u=m.filter((E,g)=>!m.some((y,x)=>x!==g&&y.n<=E.n&&y.enP0<=E.enP0&&(y.n<E.n||y.enP0<E.enP0))),h=[],p=(E,g)=>(E.enP0-g.enP0)/(g.n-E.n+(E.enP0-g.enP0));for(let E of u)for(h.push(E);h.length>=3;){let g=h[h.length-3],y=h[h.length-2],x=h[h.length-1];if(p(g,y)>p(y,x))break;h.splice(h.length-2,1)}let w=h.map((E,g)=>{let y=g===h.length-1?0:p(E,h[g+1]),x=g===0?1:p(h[g-1],E);return{...E,qLo:y,qHi:x}}),d=w[0],b=w.reduce((E,g)=>g.enP0<E.enP0?g:E),N=w,v=[];return b.n>=s-1&&v.push(` Optimal sample size too close to nmax.
21
21
  Try increasing nmax (current value = ${s})
22
- `),Q("ph2simon",{out:m,minimax:d,optimal:b,admissible:y,pu:e,pa:t,alpha:n,beta:r,nmax:s,warnings:v})}var TN=new Set(["ai","bi","ci","di","m1i","sd1i","n1i","m2i","sd2i","n2i","ri","ni","add","to","drop00"]),Bh=["method","mods","data","intercept","test","level","control"],Kh=["tau2Init","threshold","maxiter","stepadj","tol","tau2Max"],AN=["type","level"],SN=["newmods","predtype","level"],Uh=["model","predictor","retFit","level"],En=e=>e.length===1?e[0]:e,xn=(e,t)=>e.reduce((n,r,o)=>n+r*t[o],0),ts=(e,t)=>e.map(n=>xn(n,t)),Xh=e=>Array.from({length:e},(t,n)=>Array.from({length:e},(r,o)=>n===o?1:0)),ho=(e,t,n,r=1e-14)=>{let o=e(t),s=e(n);if(o===0)return t;if(s===0)return n;if(!(o*s<0))return NaN;for(let i=0;i<180;i++){let c=t+(n-t)/2,a=e(c);if(!Number.isFinite(a)){n=c;continue}if(Math.abs(n-t)<=r*Math.max(1,Math.abs(c))||a===0)return c;o*a<=0?(n=c,s=a):(t=c,o=a)}return t+(n-t)/2},Hh=(e,t)=>{let n=Math.max(t,1e-8),r=e(n);for(let o=0;o<80&&r>0;o++)n*=2,r=e(n);return r<=0?ho(e,0,n):NaN};function Yt(e,t,n,r){let o=e.length,s=n[0].length,i=t.map(F=>F+r),c=i.reduce((F,I)=>Math.min(F,I),1/0),a=i.map(F=>c/F),l=a.reduce((F,I)=>F+I,0),f=Array.from({length:s},(F,I)=>Array.from({length:s},(O,q)=>n.reduce(($,_,P)=>$+a[P]*_[I]*_[q],0))),m=Array.from({length:s},(F,I)=>n.reduce((O,q,$)=>O+a[$]*q[I]*e[$],0)),u;try{u=nt(f,Xh(s))}catch{throw new RangeError("Model matrix is not full rank.")}let h=ts(u,m),p=ts(n,h),w=e.map((F,I)=>F-p[I]),d=e.every(F=>F===e[0])?0:w.reduce((F,I,O)=>F+a[O]*I*I,0),b=d/c,y=Array.from({length:o},(F,I)=>Array.from({length:o},(O,q)=>(I===q?a[I]:0)-a[I]*xn(n[I],ts(u,n[q]).map($=>$*a[q])))),v=y.reduce((F,I,O)=>F+I[O],0),M=v/c,g=y.reduce((F,I,O)=>F+I.reduce((q,$,_)=>q+$*y[_][O],0),0),N=g/c/c,x=y.map(F=>xn(F,e)),E=.5*(xn(x,x)-c*v),R=.5*(w.reduce((F,I,O)=>F+I*I*a[O]*a[O],0)-c*a.reduce((F,I)=>F+I,0)),T=a.reduce((F,I)=>F+I*I,0),A=.5*T,S=f.length===1?Math.log(f[0][0]):Math.log(Math.abs(qN(f))),C=-.5*(i.reduce((F,I)=>F+Math.log(I),0)+S-s*Math.log(c)+b),L=u.map(F=>F.map(I=>I*c)),k=u.map((F,I)=>Math.sqrt(Math.max(0,F[I]))*Math.sqrt(c));return{beta:h,cov:L,se:k,fitted:p,residuals:w,qe:b,qeScaled:d,weights:a,minVariance:c,P:y,traceP:M,tracePNorm:v,traceP2:N,traceP2Norm:g,score:E,mlScore:R,mlInfo:A,logLik:C,sumWeightsNorm:l,sumWeightSquaredNorm:T}}function qN(e){let t=e.length,n=e.map(o=>o.slice()),r=1;for(let o=0;o<t;o++){let s=o;for(let c=o+1;c<t;c++)Math.abs(n[c][o])>Math.abs(n[s][o])&&(s=c);if(n[s][o]===0)return 0;s!==o&&([n[s],n[o]]=[n[o],n[s]],r=-r);let i=n[o][o];r*=i;for(let c=o+1;c<t;c++){let a=n[c][o]/i;for(let l=o+1;l<t;l++)n[c][l]-=a*n[o][l]}}return r}function Kc(e,t){return e.reduce((n,r,o)=>n+r.reduce((s,i,c)=>s+i*t[c]*e[c][o]*t[o],0),0)}function kN(e,t,n,r,o,s,i){let c=e.length,a=c-n[0].length,l=t.map(m=>m+o);if(r==="EE"||r==="FE")return NaN;if(r==="REML")return s.traceP2Norm>0?s.minVariance*Math.sqrt(2/s.traceP2Norm):NaN;if(r==="ML")return s.sumWeightSquaredNorm>0?s.minVariance*Math.sqrt(2/s.sumWeightSquaredNorm):NaN;if(r==="DL"||r==="HS"||r==="HSk"){let m=Math.sqrt(2*Kc(i.P,l)),u=r==="DL"?i.tracePNorm:r==="HS"?i.sumWeightsNorm:i.sumWeightsNorm*(c-1)/c;return u>0?m/u:NaN}if(r==="HE"){let m=Yt(e,Array(c).fill(1),n,0);return a>0?Math.sqrt(2*Kc(m.P,l))/a:NaN}if(r==="SJ"){let m=Yt(e,Array(c).fill(1),n,0),u=m.residuals.reduce((p,w)=>p+w*w,0)/c;if(!(u>0)||a<=0)return NaN;let h=Yt(e,t,n,u);return u/a*Math.sqrt(2*Kc(h.P,l))/h.minVariance}let f=s.residuals.reduce((m,u,h)=>m+s.weights[h]*s.weights[h]*u*u,0);return f>0&&a>0?Math.sqrt(2*a)*s.minVariance*s.minVariance/f:NaN}function Yh(e,t,n,r,o){let s=e.length,i=n[0].length,c=s-i,a=Yt(e,t,n,0);if(r==="EE"||r==="FE")return 0;if(r==="DL")return Math.max(0,(a.qeScaled-c*a.minVariance)/a.tracePNorm);if(r==="HS")return Math.max(0,(a.qeScaled-s*a.minVariance)/a.sumWeightsNorm);if(r==="HSk")return Math.max(0,(s/(s-1)*a.qeScaled-s*a.minVariance)/a.sumWeightsNorm);let l=Yt(e,Array(s).fill(1),n,0),f=l.residuals.reduce((b,y)=>b+y*y,0),m=Math.max(0,(f-t.reduce((b,y,v)=>b+l.P[v][v]*y,0))/Math.max(1,c));if(r==="HE")return m;if(r==="SJ"){let b=f/s,y=Yt(e,t,n,b),v=t.map(M=>b/(M+b));return Math.max(0,v.reduce((M,g,N)=>M+g*y.residuals[N]*y.residuals[N],0)/Math.max(1,c))}let u=b=>Yt(e,t,n,b).qe-c;if(r==="EB"){let b=o.tau2Max??Math.max(100,10*f/s);return Math.max(0,ho(u,0,b,1e-14))}if(r==="PM"){let b=o.tau2Max??Math.max(100,10*f/s);for(;u(b)>0&&b<Number.MAX_VALUE/4;)b*=2;let y=o.tol??1e-14;return Math.max(0,ho(u,0,b,y))}let h=Math.max(0,o.tau2Init??m),p=o.maxiter??100,w=o.threshold??1e-5,d=o.stepadj??1;if(p<=1)return h;for(let b=0;b<p;b++){let y=Yt(e,t,n,h),v=r==="REML"?y.score:y.mlScore,M=r==="REML"?.5*y.traceP2Norm:y.mlInfo;if(!(M>0)||!Number.isFinite(M))return h;let g=Math.max(0,h+d*v/M);if(!Number.isFinite(g))return h;if(Math.abs(g-h)<w)return g;h=g}return h}function Gh(e){if(e.length===0||e[0].length===0)return[];let t=[],n=[];for(let r=0;r<e[0].length;r++){let o=e.map(a=>a[r]),s=Math.sqrt(xn(o,o)),i=o.slice();for(let a of n){let l=xn(i,a);i=i.map((f,m)=>f-l*a[m])}let c=Math.sqrt(xn(i,i));s>0&&c>1e-10*s&&(t.push(r),n.push(i.map(a=>a/c)))}return t}function IN(e,t,n,r){let o,s=[],i=!1,c=0;if(typeof t=="string"){if(r===void 0)throw new TypeError("data is required when mods is a formula");let f=t.includes("~")?t:`~ ${t}`,m=$n(Jn(f),r,{intercept:n});if(o=m.x,s=m.colNames.map(h=>h==="(Intercept)"?"intrcpt":h),m.frame.rows.length!==e)throw new RangeError("variable lengths differ between yi and mods");if(o[0].length>e)throw new Error("Number of parameters to be estimated is larger than the number of observations.");let u=Gh(o);return{x:o.map(h=>u.map(p=>h[p])),names:u.map(h=>s[h]??""),warnings:u.length<o[0].length?["Redundant predictors dropped from the model."]:[]}}else if(t===void 0)o=Array.from({length:e},()=>[]);else if(Array.isArray(t))if(t.length===0)o=Array.from({length:e},()=>[]);else if(typeof t[0]=="number"){let f=Array.from(t,Number);if(f.length!==e)throw new RangeError("variable lengths differ (found for 'mods')");o=f.map(m=>[m]),c=1}else{let f=t;if(f.length===e)o=Array.from(f,m=>Array.from(m,Number)),c=o[0]?.length??0;else if(f.every(m=>m.length===e))c=f.length,o=Array.from({length:e},(m,u)=>f.map(h=>Number(h[u])));else throw new RangeError("variable lengths differ (found for 'mods')")}else{let f=t;if(f.rows!==void 0){let m=f.rows[0];if(m!=null&&typeof m=="object"&&!Array.isArray(m)){let u=typeof f.colNames=="string"?[f.colNames]:f.colNames===void 0||f.colNames===null?Object.keys(m):Array.from(f.colNames);o=f.rows.map(h=>u.map(p=>Number(h[p]))),s=u.map(h=>h==="(Intercept)"?"intrcpt":h),i=!0,c=u.length}else o=f.rows.map(u=>Array.from(u,Number)),c=o[0]?.length??0}else if(f.columns!==void 0)c=f.columns.length,o=Array.from({length:e},(m,u)=>f.columns.map(h=>Number(h[u])));else throw new TypeError("mods matrix must have rows or columns");if(o.length!==e)throw new RangeError("variable lengths differ (found for 'mods')");f.colNames!==void 0&&f.colNames!==null&&(s=(typeof f.colNames=="string"?[f.colNames]:Array.from(f.colNames)).map(m=>m==="(Intercept)"?"intrcpt":m),i=!0)}if(o.some(f=>f.length!==o[0].length))throw new TypeError("all rows of the moderator matrix must have the same length");if(i||(s=Array.from({length:c},(f,m)=>c>1?`.${m+1}`:"")),n&&(o=o.map(f=>[1,...f]),s=["intrcpt",...s]),o[0].length>e)throw new Error("Number of parameters to be estimated is larger than the number of observations.");let a=Gh(o);if(a.length<o[0].length&&!i&&c>0){for(let f=0;f<a.length;f++)if(a[f]!==0||!n){s[a[f]]="";break}}return{x:o.map(f=>a.map(m=>f[m])),names:a.map(f=>s[f]??""),warnings:a.length<o[0].length?["Redundant predictors dropped from the model."]:[]}}function Hc(e){let t=[676.5203681218851,-1259.1392167224028,771.3234287776531,-176.6150291621406,12.507343278686905,-.13857109526572012,9984369578019572e-21,15056327351493116e-23];if(e<.5)return Math.log(Math.PI)-Math.log(Math.sin(Math.PI*e))-Hc(1-e);let n=e-1,r=.9999999999998099;for(let s=0;s<t.length;s++)r+=t[s]/(n+s+1);let o=n+7.5;return .9189385332046727+(n+.5)*Math.log(o)-o+Math.log(r)}function Qh(e,t,n,r){let o=Math.max(e,t,n,r);return o===0?0:2**Math.floor(Math.log2(o))}function Uc(e,t,n,r,o){let s=Qh(e,t,n,r);if(s===0)return NaN;let i=e/s,c=t/s,a=n/s,l=r/s,f=Oe(Jt(i,l),Jt(c,a)),m=Ot(i,c),u=Ot(a,l),h=o==="RR"?_e([a,0],m):o==="OR"?Jt(c,a):_e(m,u),p=at(f,h);return p[0]+p[1]}function _N(e){let t=e/2;if(t>1e3){let n=1/t,r=n*n,o=r*n,s=r*r,i=s*n,c=o*o,a=c*n,l=s*s,f=-(3/8)*n-1/8*r-3/64*o-1/64*s-3/640*i-1/384*c-33/14336*a-1/2048*l;return Math.exp(f)}return Math.exp(Hc(t)-.5*Math.log(t)-Hc(t-.5))}function LN(e,t){B(t,TN,"escalc");let n=[],r=f=>t[f]===void 0?void 0:Array.from(t[f]),s={RR:["ai","bi","ci","di"],OR:["ai","bi","ci","di"],RD:["ai","bi","ci","di"],MD:["m1i","sd1i","n1i","m2i","sd2i","n2i"],SMD:["m1i","sd1i","n1i","m2i","sd2i","n2i"],ZCOR:["ri","ni"]}[e].map(f=>r(f)),i=s.slice(0,4).some(f=>f.some(m=>m===0));if(s.some(f=>f===void 0))throw new TypeError(`missing required arguments for measure ${e}`);let c=s[0].length;if(s.some(f=>f.length!==c))throw new RangeError("variable lengths differ");let a=[],l=[];for(let f=0;f<c;f++){let m=NaN,u=NaN;if(e==="ZCOR"){let h=s[0][f],p=s[1][f];if(Math.abs(h)>1)throw new RangeError("One or more correlations are > 1 or < -1.");p>3?(m=Math.atanh(h),u=1/(p-3)):(n.push("Cannot estimate the sampling variance when ni <= 3."),m=Math.atanh(h),u=NaN)}else if(e==="MD"||e==="SMD"){let h=s[0][f],p=s[1][f],w=s[2][f],d=s[3][f],b=s[4][f],y=s[5][f];if(!(w>1&&y>1&&p>=0&&b>=0))throw new RangeError("invalid means, standard deviations, or sample sizes");if(e==="MD")m=h-d,u=p*p/w+b*b/y;else{let v=w+y-2,M=((w-1)*p*p+(y-1)*b*b)/v,g=(h-d)/Math.sqrt(M);m=_N(v)*g,u=(w+y)/(w*y)+m*m/(2*(w+y))}}else{let h=s[0][f],p=s[1][f],w=s[2][f],d=s[3][f],b=h,y=p,v=w,M=d,g=t.add??.5,N=t.to??"only0",x=[b,y,v,M].some(I=>I===0),E=N==="all"||N==="only0"&&x||N==="if0all"&&i;if(e!=="RD"&&E&&(b+=g,y+=g,v+=g,M+=g),e==="RD"&&E&&(b+=g,y+=g,v+=g,M+=g),t.drop00&&(h===0&&w===0||p===0&&d===0)){a.push(NaN),l.push(NaN);continue}let R=Qh(b,y,v,M),T=b/R,A=y/R,S=v/R,C=M/R,L=T+A,k=S+C,F=1/R;e==="RR"?(m=Math.log1p(Uc(b,y,v,M,"RR")),u=F*(A/(T*L)+C/(S*k))):e==="OR"?(m=Math.log1p(Uc(b,y,v,M,"OR")),u=F*(1/T+1/A+1/S+1/C)):(m=Uc(b,y,v,M,"RD"),u=F*(T*A/(L*L*L)+S*C/(k*k*k)))}a.push(m),l.push(u)}return Q("escalc",{yi:a,vi:l,measure:e,warnings:n})}function Zh(e,t,n={}){wt(t)&&B(t,Bh,"rma"),wt(t)&&B(Cn(t,"control"),Kh,"rma","control"),B(n,Bh,"rma"),B(Cn(n,"control"),Kh,"rma","control");let r=typeof e=="object"&&"yi"in e,o=Array.from(r?e.yi:e),s=(r?t:n)??{},i=Array.from(r?e.vi:t??[]);if(o.length===0||i.length!==o.length)throw new RangeError("yi and vi must have the same positive length");if(o.some(K=>!Number.isFinite(K)))throw new Error("NA/NaN/Inf in foreign function call (arg 1)");if(i.some(K=>!Number.isFinite(K)))throw new Error("NA/NaN/Inf in foreign function call (arg 1)");if(s.method==="DL"&&i.some(K=>K<=0))throw new Error("DL estimator cannot be used when there are non-positive sampling variances in the data.");if(i.some(K=>K<0))throw new RangeError("sampling variances must be non-negative");if(i.some(K=>K===0)&&!["EE","FE"].includes(s.method??"REML"))throw new RangeError("random-effects estimators require positive sampling variances");let c=s.method??"REML",a=s.test??"z",l=s.level??95,f=s.intercept??!0,m=IN(o.length,s.mods,f,s.data),u=m.x,h=u[0].length,p=o.length,w=p-h,d=m.names[0]==="intrcpt"||!f&&u.every(K=>K[0]===1);if(h===0)throw new RangeError("the model must contain at least one coefficient");if(p<h)throw new Error("Number of parameters to be estimated is larger than the number of observations.");if(!(l>0&&l<100))throw new RangeError("level must be between 0 and 100");if(!["EE","FE","DL","HE","HS","HSk","SJ","ML","REML","EB","PM"].includes(c))throw new RangeError(`unknown method '${c}'`);if(!["z","t","knha"].includes(a))throw new RangeError(`unknown test '${a}'`);if(c==="DL"&&!Number.isFinite(Yt(o,i,u,0).qe))throw new Error("NA/NaN/Inf in foreign function call (arg 1)");let b=Yt(o,i,u,0),y=Yh(o,i,u,c,s.control??{}),v=Yt(o,i,u,y),M=s.mods===void 0?y:Yh(o,i,Array.from({length:p},()=>[1]),c,s.control??{}),g=a==="knha"&&w>0?Math.max(1,v.qe/w):1,N=v.cov.map(K=>K.map(X=>X*g)),x=v.se.map(K=>K*Math.sqrt(g)),E=v.beta.map((K,X)=>K/x[X]),R=a==="z"?le(1-(1-l/100)/2):jt(1-(1-l/100)/2,w),T=E.map(K=>2*(a==="z"?ae(-Math.abs(K)):on(-Math.abs(K),w))),A=v.beta.map((K,X)=>K-R*x[X]),S=v.beta.map((K,X)=>K+R*x[X]),C=d?1:0,L=h===1?1:Math.max(1,h-C),k=0,F=1;if(h===1)k=E[0]*E[0],F=a==="z"?It(k,1,{lowerTail:!1}):Tn(k,1,w,{lowerTail:!1});else{let K=v.beta.slice(C),X=N.slice(C).map(re=>re.slice(C));try{k=xn(K,nt(X,K))}catch{k=NaN}F=a==="z"?It(k,L,{lowerTail:!1}):Tn(k/L,L,w,{lowerTail:!1})}let I=b.tracePNorm,O=w>0?w*b.minVariance/I:0,q=c==="EE"||c==="FE"?b.qe>0?100*Math.max(0,(b.qe-w)/b.qe):0:y===0?0:100*y/(y+O),$=c==="EE"||c==="FE"?w>0?Math.max(1,b.qe/w):1:y===0?1:(y+O)/O,_=s.mods===void 0||h===1?null:M>0?100*Math.max(0,(M-y)/M):0,P=kN(o,i,u,c,y,v,b);return Q("rma",{beta:{dim:[h,1],rowNames:h===1?m.names[0]??"intrcpt":m.names,colNames:null,rows:v.beta.map(K=>[K])},se:En(x),zval:En(E),pval:En(T),"ci.lb":En(A),"ci.ub":En(S),tau2:y,"se.tau2":P,QE:b.qe,QEp:w===0?1:It(b.qe,w,{lowerTail:!1}),QM:k,QMp:F,R2:_,p:h,m:L,I2:q,H2:$,k:p,yi:o,vi:i,mods:u,intercept:d,method:c,test:a,level:l,warnings:m.warnings,covariance:N,tau2Null:M})}function CN(e,t={}){B(t,AN,"confintRma");let n=t.type??"QP",r=t.level??e.level,o=1-r/100;if(n!=="QP"&&n!=="PL")throw new RangeError("type must be 'QP' or 'PL'");let s=Yt(e.yi,e.vi,e.mods,e.tau2),i=e.k-e.mods[0].length,c=0,a=NaN;if(n==="QP"){let d=Yt(e.yi,e.vi,e.mods,0),b=Fn(1-o/2,i),y=Fn(o/2,i),v=d.qe,M=g=>Yt(e.yi,e.vi,e.mods,g).qe;c=v<=b?0:Hh(g=>M(g)-b,Math.max(e.tau2,1e-8)),a=v<=y?NaN:Hh(g=>M(g)-y,Math.max(e.tau2,1e-8))}else{let d=Fn(r/100,1),b=v=>Yt(e.yi,e.vi,e.mods,v).logLik,y=v=>2*(s.logLik-b(v))-d;if(e.tau2>0){c=y(0)<=0?0:ho(y,0,e.tau2);let v=Math.max(e.tau2*2,1e-8),M=y(v);for(let g=0;g<80&&M<0;g++)v*=2,M=y(v);a=M>=0?ho(y,e.tau2,v):NaN}else{let v=1e-8,M=y(v);for(let g=0;g<80&&M<0;g++)v*=2,M=y(v);a=M>=0?ho(y,0,v):NaN}}let l=Yt(e.yi,e.vi,e.mods,0),f=i>0?i*l.minVariance/l.tracePNorm:0,m=d=>[d,Math.sqrt(d),100*d/(d+f),(d+f)/f],u=m(e.tau2),h=m(c),p=m(a),w=["tau^2","tau","I^2(%)","H^2"];return{random:{dim:[4,3],rowNames:w,colNames:["estimate","ci.lb","ci.ub"],rows:w.map((d,b)=>({estimate:u[b],"ci.lb":h[b],"ci.ub":p[b]}))},digits:{est:"4",se:"4",test:"4",pval:"4",ci:"4",var:"4",sevar:"4",fit:"4",het:"4"},"ci.null":!1,"lb.sign":"","ub.sign":"","tau2.min":"0"}}function FN(e,t={}){B(t,SN,"predictRma");let n=t.level??e.level,r=1-n/100,o=e.mods[0].length,s;if(t.newmods===void 0)s=o-(e.intercept?1:0)===0?[e.intercept?[1]:[]]:e.mods.map(h=>h.slice());else{let u=t.newmods;if(u.length>0&&typeof u[0]=="number"?s=Array.from(u,h=>e.intercept?[1,h]:[h]):s=Array.from(u,h=>Array.from(h)),s.some(h=>h.length===o-(e.intercept?1:0))&&(s=s.map(h=>e.intercept?[1,...h]:h)),s.some(h=>h.length!==o))throw new RangeError("newmods has the wrong number of columns")}let i=e.beta.rows.map(u=>u[0]),c=s.map(u=>xn(u,i)),a=s.map(u=>Math.sqrt(Math.max(0,xn(u,ts(e.covariance,u))))),l=e.test==="z"?le(1-r/2):jt(1-r/2,e.k-o),f=t.predtype==="riley"?jt(1-r/2,e.k-o-1):l,m=s.map((u,h)=>Math.sqrt(a[h]**2+e.tau2));return{pred:En(c),se:En(a),"ci.lb":En(c.map((u,h)=>u-l*a[h])),"ci.ub":En(c.map((u,h)=>u+l*a[h])),"pi.lb":En(c.map((u,h)=>u-f*m[h])),"pi.ub":En(c.map((u,h)=>u+f*m[h]))}}function ON(e,t,n={}){wt(t)&&B(t,Uh,"regtest"),B(n,Uh,"regtest");let r=typeof e=="object"&&"beta"in e,o=(r?t:n)??{},s=o.model??"rma",i=o.predictor??"sei",c=o.level??95,a=Array.from(r?e.yi:e),l=Array.from(r?e.vi:t??[]);if(a.length!==l.length||a.length<2)throw new RangeError("yi and vi must have equal lengths and at least two studies");let f=l.map(g=>Math.sqrt(g)),m=i==="sei"?f:i==="vi"?l:l.map((g,N)=>1/(N+1)),u,h,p,w,d,b;if(s==="lm"){let g=m.map((T,A)=>[1,1/T]),N=a.map((T,A)=>T/f[A]),x=DN(g,N);u=x.beta[1];let E=Math.sqrt(x.cov[0][0]*x.sigma2),R=Math.sqrt(x.cov[1][1]*x.sigma2);p=x.beta[0]/E,h=R,w=a.length-2,d=2*on(-Math.abs(p),w)}else{b=Zh(a,l,{method:"REML",mods:m,intercept:!0,test:"z"}),u=b.beta.rows[0][0],h=Array.isArray(b.se)?b.se[0]:b.se;let g=Array.isArray(b.zval)?b.zval[1]:NaN,N=Array.isArray(b.pval)?b.pval[1]:NaN;p=g,w=NaN,d=N}let y=s==="lm"?jt(1-(1-c/100)/2,w):le(1-(1-c/100)/2),v=[];s==="lm"&&(!Number.isFinite(p)||!Number.isFinite(d)||!Number.isFinite(h))&&v.push("NaNs produced");let M={model:s,predictor:i,zval:p,pval:d,dfs:w,est:u,"ci.lb":u-y*h,"ci.ub":u+y*h,warnings:v};return Q("regtest",o.retFit&&b!==void 0?{...M,fit:b}:M)}function DN(e,t){let n=Array.from({length:2},(c,a)=>Array.from({length:2},(l,f)=>e.reduce((m,u,h)=>m+u[a]*u[f],0))),r=nt(n,Xh(2)),o=ts(r,Array.from({length:2},(c,a)=>e.reduce((l,f,m)=>l+f[a]*t[m],0))),s=t.map((c,a)=>c-xn(e[a],o)),i=t.length>2?s.reduce((c,a)=>c+a*a,0)/(t.length-2):NaN;return{beta:o,cov:r,sigma2:i}}export{ic as Anova,sv as ICC,aw as TukeyHSD,nb as adTest,Ei as anova,_m as ansariTest,ow as aov,N1 as aovEz,xi as aovSummary,qb as auc,om as bartlettTest,ui as binomCI,Fd as binomDiffCI,Hd as binomDiffEquivalenceTest,Dd as binomRatioCI,ld as binomTest,zb as blandAltman,k0 as boschlooTest,Og as brunnerMunzelTest,xp as chisqTest,Cb as ciAuc,D0 as cochranQTest,Iw as coefci,qw as coeftest,Tu as cohenES,o0 as cohensD,tw as confint,CN as confintRma,Ib as coords,ep as corTest,z1 as coxZph,O1 as coxph,hb as cronbachAlpha,_v as crr,Av as cuminc,rb as cvmTest,Pb as diagnosticTests,ig as dscfAllPairsTest,fg as dunnTest,sg as dunnettTest,uN as epiSsequb,hN as epiSsequc,pN as epiSsninfb,dN as epiSsninfc,gN as epiSssupb,wN as epiSssupc,h0 as epsilonSquared,e1 as esH,t1 as esW1,n1 as esW2,LN as escalc,m0 as etaSquared,lv as factanal,ko as fisherTest,im as flignerTest,_i as friedmanTest,wp as gTest,cg as gamesHowellTest,ug as glhtDunnett,Fa as glm,Fw as glmAnova,lw as glmConfint,rp as glmCooksDistance,Pa as glmHatvalues,op as glmPredict,tp as glmResiduals,np as glmRstandard,Da as glmSummary,yp as goodmanKruskalGamma,s0 as hedgesG,ub as icc,eb as jonckheereTest,fb as kappa2,mb as kappamFleiss,Ws as kruskalTest,zm as ksTest,rg as kwAllPairsDunnTest,og as kwManyOneDunnTest,Ep as lblTest,B1 as leveneTest,ob as lillieTest,Vo as lm,vg as mantelhaenTest,ag as manyOneUTest,zw as mauchlyTest,L0 as mcnemarTest,Jb as mcreg,jr as modelMatrix,cm as moodTest,EN as numDEpi,jd as oddsRatio,gv as omega,u0 as omegaSquared,Nm as onewayTest,Br as pAdjust,Ug as pairwisePropTest,Bg as pairwiseTTest,Kg as pairwiseWilcoxTest,db as partialR,ab as pearsonTest,RN as ph2simon,dd as poissonTest,yv as polychor,Ev as polyserial,up as powerAnovaTest,vN as powerCT,yN as powerEpi,bN as powerPaired2x2,fp as powerPropTest,lp as powerTTest,nw as predict,Lv as predictCrr,FN as predictRma,fi as propTest,H1 as propTrendTest,u1 as pwr2p2nTest,l1 as pwr2pTest,d1 as pwrAnovaTest,p1 as pwrChisqTest,g1 as pwrF2Test,m1 as pwrNormTest,f1 as pwrPTest,h1 as pwrRTest,c1 as pwrT2nTest,i1 as pwrTTest,am as quadeTest,ON as regtest,Zh as rma,q1 as rmst2,Ab as roc,Ob as rocTest,Jh as setWarningHandler,sb as sfTest,pg as shapiroTest,NN as ssizeCT,MN as ssizeEpi,Np as stuartMaxwellTest,x1 as survdiff,fr as survfit,Gv as survreg,Xv as survregPredict,Qv as survregResiduals,y0 as tTOST,yn as tTest,qv as timepoints,Kd as tost,M0 as tsumTOST,lg as tukeyTest,rm as varTest,Rw as vcovCL,pw as vcovHC,Io as wilcoxTest,iw as zTest};
22
+ `),Q("ph2simon",{out:m,minimax:d,optimal:b,admissible:N,pu:e,pa:t,alpha:n,beta:r,nmax:s,warnings:v})}var CN=new Set(["ai","bi","ci","di","m1i","sd1i","n1i","m2i","sd2i","n2i","ri","ni","add","to","drop00"]),Yh=["method","mods","data","intercept","test","level","control"],Gh=["tau2Init","threshold","maxiter","stepadj","tol","tau2Max"],FN=["type","level"],ON=["newmods","predtype","level"],Xh=["model","predictor","retFit","level"],Mn=e=>e.length===1?e[0]:e,En=(e,t)=>e.reduce((n,r,o)=>n+r*t[o],0),ns=(e,t)=>e.map(n=>En(n,t)),ep=e=>Array.from({length:e},(t,n)=>Array.from({length:e},(r,o)=>n===o?1:0)),po=(e,t,n,r=1e-14)=>{let o=e(t),s=e(n);if(o===0)return t;if(s===0)return n;if(!(o*s<0))return NaN;for(let i=0;i<180;i++){let c=t+(n-t)/2,a=e(c);if(!Number.isFinite(a)){n=c;continue}if(Math.abs(n-t)<=r*Math.max(1,Math.abs(c))||a===0)return c;o*a<=0?(n=c,s=a):(t=c,o=a)}return t+(n-t)/2},Qh=(e,t)=>{let n=Math.max(t,1e-8),r=e(n);for(let o=0;o<80&&r>0;o++)n*=2,r=e(n);return r<=0?po(e,0,n):NaN};function Gt(e,t,n,r){let o=e.length,s=n[0].length,i=t.map(F=>F+r),c=i.reduce((F,I)=>Math.min(F,I),1/0),a=i.map(F=>c/F),l=a.reduce((F,I)=>F+I,0),f=Array.from({length:s},(F,I)=>Array.from({length:s},(O,q)=>n.reduce(($,_,P)=>$+a[P]*_[I]*_[q],0))),m=Array.from({length:s},(F,I)=>n.reduce((O,q,$)=>O+a[$]*q[I]*e[$],0)),u;try{u=nt(f,ep(s))}catch{throw new RangeError("Model matrix is not full rank.")}let h=ns(u,m),p=ns(n,h),w=e.map((F,I)=>F-p[I]),d=e.every(F=>F===e[0])?0:w.reduce((F,I,O)=>F+a[O]*I*I,0),b=d/c,N=Array.from({length:o},(F,I)=>Array.from({length:o},(O,q)=>(I===q?a[I]:0)-a[I]*En(n[I],ns(u,n[q]).map($=>$*a[q])))),v=N.reduce((F,I,O)=>F+I[O],0),E=v/c,g=N.reduce((F,I,O)=>F+I.reduce((q,$,_)=>q+$*N[_][O],0),0),y=g/c/c,x=N.map(F=>En(F,e)),M=.5*(En(x,x)-c*v),R=.5*(w.reduce((F,I,O)=>F+I*I*a[O]*a[O],0)-c*a.reduce((F,I)=>F+I,0)),T=a.reduce((F,I)=>F+I*I,0),S=.5*T,A=f.length===1?Math.log(f[0][0]):Math.log(Math.abs(DN(f))),C=-.5*(i.reduce((F,I)=>F+Math.log(I),0)+A-s*Math.log(c)+b),L=u.map(F=>F.map(I=>I*c)),k=u.map((F,I)=>Math.sqrt(Math.max(0,F[I]))*Math.sqrt(c));return{beta:h,cov:L,se:k,fitted:p,residuals:w,qe:b,qeScaled:d,weights:a,minVariance:c,P:N,traceP:E,tracePNorm:v,traceP2:y,traceP2Norm:g,score:M,mlScore:R,mlInfo:S,logLik:C,sumWeightsNorm:l,sumWeightSquaredNorm:T}}function DN(e){let t=e.length,n=e.map(o=>o.slice()),r=1;for(let o=0;o<t;o++){let s=o;for(let c=o+1;c<t;c++)Math.abs(n[c][o])>Math.abs(n[s][o])&&(s=c);if(n[s][o]===0)return 0;s!==o&&([n[s],n[o]]=[n[o],n[s]],r=-r);let i=n[o][o];r*=i;for(let c=o+1;c<t;c++){let a=n[c][o]/i;for(let l=o+1;l<t;l++)n[c][l]-=a*n[o][l]}}return r}function Gc(e,t){return e.reduce((n,r,o)=>n+r.reduce((s,i,c)=>s+i*t[c]*e[c][o]*t[o],0),0)}function PN(e,t,n,r,o,s,i){let c=e.length,a=c-n[0].length,l=t.map(m=>m+o);if(r==="EE"||r==="FE")return NaN;if(r==="REML")return s.traceP2Norm>0?s.minVariance*Math.sqrt(2/s.traceP2Norm):NaN;if(r==="ML")return s.sumWeightSquaredNorm>0?s.minVariance*Math.sqrt(2/s.sumWeightSquaredNorm):NaN;if(r==="DL"||r==="HS"||r==="HSk"){let m=Math.sqrt(2*Gc(i.P,l)),u=r==="DL"?i.tracePNorm:r==="HS"?i.sumWeightsNorm:i.sumWeightsNorm*(c-1)/c;return u>0?m/u:NaN}if(r==="HE"){let m=Gt(e,Array(c).fill(1),n,0);return a>0?Math.sqrt(2*Gc(m.P,l))/a:NaN}if(r==="SJ"){let m=Gt(e,Array(c).fill(1),n,0),u=m.residuals.reduce((p,w)=>p+w*w,0)/c;if(!(u>0)||a<=0)return NaN;let h=Gt(e,t,n,u);return u/a*Math.sqrt(2*Gc(h.P,l))/h.minVariance}let f=s.residuals.reduce((m,u,h)=>m+s.weights[h]*s.weights[h]*u*u,0);return f>0&&a>0?Math.sqrt(2*a)*s.minVariance*s.minVariance/f:NaN}function Zh(e,t,n,r,o){let s=e.length,i=n[0].length,c=s-i,a=Gt(e,t,n,0);if(r==="EE"||r==="FE")return 0;if(r==="DL")return Math.max(0,(a.qeScaled-c*a.minVariance)/a.tracePNorm);if(r==="HS")return Math.max(0,(a.qeScaled-s*a.minVariance)/a.sumWeightsNorm);if(r==="HSk")return Math.max(0,(s/(s-1)*a.qeScaled-s*a.minVariance)/a.sumWeightsNorm);let l=Gt(e,Array(s).fill(1),n,0),f=l.residuals.reduce((b,N)=>b+N*N,0),m=Math.max(0,(f-t.reduce((b,N,v)=>b+l.P[v][v]*N,0))/Math.max(1,c));if(r==="HE")return m;if(r==="SJ"){let b=f/s,N=Gt(e,t,n,b),v=t.map(E=>b/(E+b));return Math.max(0,v.reduce((E,g,y)=>E+g*N.residuals[y]*N.residuals[y],0)/Math.max(1,c))}let u=b=>Gt(e,t,n,b).qe-c;if(r==="EB"){let b=o.tau2Max??Math.max(100,10*f/s);return Math.max(0,po(u,0,b,1e-14))}if(r==="PM"){let b=o.tau2Max??Math.max(100,10*f/s);for(;u(b)>0&&b<Number.MAX_VALUE/4;)b*=2;let N=o.tol??1e-14;return Math.max(0,po(u,0,b,N))}let h=Math.max(0,o.tau2Init??m),p=o.maxiter??100,w=o.threshold??1e-5,d=o.stepadj??1;if(p<=1)return h;for(let b=0;b<p;b++){let N=Gt(e,t,n,h),v=r==="REML"?N.score:N.mlScore,E=r==="REML"?.5*N.traceP2Norm:N.mlInfo;if(!(E>0)||!Number.isFinite(E))return h;let g=Math.max(0,h+d*v/E);if(!Number.isFinite(g))return h;if(Math.abs(g-h)<w)return g;h=g}return h}function Jh(e){if(e.length===0||e[0].length===0)return[];let t=[],n=[];for(let r=0;r<e[0].length;r++){let o=e.map(a=>a[r]),s=Math.sqrt(En(o,o)),i=o.slice();for(let a of n){let l=En(i,a);i=i.map((f,m)=>f-l*a[m])}let c=Math.sqrt(En(i,i));s>0&&c>1e-10*s&&(t.push(r),n.push(i.map(a=>a/c)))}return t}function VN(e,t,n,r){let o,s=[],i=!1,c=0;if(typeof t=="string"){if(r===void 0)throw new TypeError("data is required when mods is a formula");let f=t.includes("~")?t:`~ ${t}`,m=jn(er(f),r,{intercept:n});if(o=m.x,s=m.colNames.map(h=>h==="(Intercept)"?"intrcpt":h),m.frame.rows.length!==e)throw new RangeError("variable lengths differ between yi and mods");if(o[0].length>e)throw new Error("Number of parameters to be estimated is larger than the number of observations.");let u=Jh(o);return{x:o.map(h=>u.map(p=>h[p])),names:u.map(h=>s[h]??""),warnings:u.length<o[0].length?["Redundant predictors dropped from the model."]:[]}}else if(t===void 0)o=Array.from({length:e},()=>[]);else if(Array.isArray(t))if(t.length===0)o=Array.from({length:e},()=>[]);else if(typeof t[0]=="number"){let f=Array.from(t,Number);if(f.length!==e)throw new RangeError("variable lengths differ (found for 'mods')");o=f.map(m=>[m]),c=1}else{let f=t;if(f.length===e)o=Array.from(f,m=>Array.from(m,Number)),c=o[0]?.length??0;else if(f.every(m=>m.length===e))c=f.length,o=Array.from({length:e},(m,u)=>f.map(h=>Number(h[u])));else throw new RangeError("variable lengths differ (found for 'mods')")}else{let f=t;if(f.rows!==void 0){let m=f.rows[0];if(m!=null&&typeof m=="object"&&!Array.isArray(m)){let u=typeof f.colNames=="string"?[f.colNames]:f.colNames===void 0||f.colNames===null?Object.keys(m):Array.from(f.colNames);o=f.rows.map(h=>u.map(p=>Number(h[p]))),s=u.map(h=>h==="(Intercept)"?"intrcpt":h),i=!0,c=u.length}else o=f.rows.map(u=>Array.from(u,Number)),c=o[0]?.length??0}else if(f.columns!==void 0)c=f.columns.length,o=Array.from({length:e},(m,u)=>f.columns.map(h=>Number(h[u])));else throw new TypeError("mods matrix must have rows or columns");if(o.length!==e)throw new RangeError("variable lengths differ (found for 'mods')");f.colNames!==void 0&&f.colNames!==null&&(s=(typeof f.colNames=="string"?[f.colNames]:Array.from(f.colNames)).map(m=>m==="(Intercept)"?"intrcpt":m),i=!0)}if(o.some(f=>f.length!==o[0].length))throw new TypeError("all rows of the moderator matrix must have the same length");if(i||(s=Array.from({length:c},(f,m)=>c>1?`.${m+1}`:"")),n&&(o=o.map(f=>[1,...f]),s=["intrcpt",...s]),o[0].length>e)throw new Error("Number of parameters to be estimated is larger than the number of observations.");let a=Jh(o);if(a.length<o[0].length&&!i&&c>0){for(let f=0;f<a.length;f++)if(a[f]!==0||!n){s[a[f]]="";break}}return{x:o.map(f=>a.map(m=>f[m])),names:a.map(f=>s[f]??""),warnings:a.length<o[0].length?["Redundant predictors dropped from the model."]:[]}}function Qc(e){let t=[676.5203681218851,-1259.1392167224028,771.3234287776531,-176.6150291621406,12.507343278686905,-.13857109526572012,9984369578019572e-21,15056327351493116e-23];if(e<.5)return Math.log(Math.PI)-Math.log(Math.sin(Math.PI*e))-Qc(1-e);let n=e-1,r=.9999999999998099;for(let s=0;s<t.length;s++)r+=t[s]/(n+s+1);let o=n+7.5;return .9189385332046727+(n+.5)*Math.log(o)-o+Math.log(r)}function tp(e,t,n,r){let o=Math.max(e,t,n,r);return o===0?0:2**Math.floor(Math.log2(o))}function Xc(e,t,n,r,o){let s=tp(e,t,n,r);if(s===0)return NaN;let i=e/s,c=t/s,a=n/s,l=r/s,f=Le(jt(i,l),jt(c,a)),m=Ot(i,c),u=Ot(a,l),h=o==="RR"?_e([a,0],m):o==="OR"?jt(c,a):_e(m,u),p=rt(f,h);return p[0]+p[1]}function $N(e){let t=e/2;if(t>1e3){let n=1/t,r=n*n,o=r*n,s=r*r,i=s*n,c=o*o,a=c*n,l=s*s,f=-(3/8)*n-1/8*r-3/64*o-1/64*s-3/640*i-1/384*c-33/14336*a-1/2048*l;return Math.exp(f)}return Math.exp(Qc(t)-.5*Math.log(t)-Qc(t-.5))}function zN(e,t){B(t,CN,"escalc");let n=[],r=f=>t[f]===void 0?void 0:Array.from(t[f]),s={RR:["ai","bi","ci","di"],OR:["ai","bi","ci","di"],RD:["ai","bi","ci","di"],MD:["m1i","sd1i","n1i","m2i","sd2i","n2i"],SMD:["m1i","sd1i","n1i","m2i","sd2i","n2i"],ZCOR:["ri","ni"]}[e].map(f=>r(f)),i=s.slice(0,4).some(f=>f.some(m=>m===0));if(s.some(f=>f===void 0))throw new TypeError(`missing required arguments for measure ${e}`);let c=s[0].length;if(s.some(f=>f.length!==c))throw new RangeError("variable lengths differ");let a=[],l=[];for(let f=0;f<c;f++){let m=NaN,u=NaN;if(e==="ZCOR"){let h=s[0][f],p=s[1][f];if(Math.abs(h)>1)throw new RangeError("One or more correlations are > 1 or < -1.");p>3?(m=Math.atanh(h),u=1/(p-3)):(n.push("Cannot estimate the sampling variance when ni <= 3."),m=Math.atanh(h),u=NaN)}else if(e==="MD"||e==="SMD"){let h=s[0][f],p=s[1][f],w=s[2][f],d=s[3][f],b=s[4][f],N=s[5][f];if(!(w>1&&N>1&&p>=0&&b>=0))throw new RangeError("invalid means, standard deviations, or sample sizes");if(e==="MD")m=h-d,u=p*p/w+b*b/N;else{let v=w+N-2,E=((w-1)*p*p+(N-1)*b*b)/v,g=(h-d)/Math.sqrt(E);m=$N(v)*g,u=(w+N)/(w*N)+m*m/(2*(w+N))}}else{let h=s[0][f],p=s[1][f],w=s[2][f],d=s[3][f],b=h,N=p,v=w,E=d,g=t.add??.5,y=t.to??"only0",x=[b,N,v,E].some(I=>I===0),M=y==="all"||y==="only0"&&x||y==="if0all"&&i;if(e!=="RD"&&M&&(b+=g,N+=g,v+=g,E+=g),e==="RD"&&M&&(b+=g,N+=g,v+=g,E+=g),t.drop00&&(h===0&&w===0||p===0&&d===0)){a.push(NaN),l.push(NaN);continue}let R=tp(b,N,v,E),T=b/R,S=N/R,A=v/R,C=E/R,L=T+S,k=A+C,F=1/R;e==="RR"?(m=Math.log1p(Xc(b,N,v,E,"RR")),u=F*(S/(T*L)+C/(A*k))):e==="OR"?(m=Math.log1p(Xc(b,N,v,E,"OR")),u=F*(1/T+1/S+1/A+1/C)):(m=Xc(b,N,v,E,"RD"),u=F*(T*S/(L*L*L)+A*C/(k*k*k)))}a.push(m),l.push(u)}return Q("escalc",{yi:a,vi:l,measure:e,warnings:n})}function np(e,t,n={}){ct(t)&&B(t,Yh,"rma"),ct(t)&&B(Fn(t,"control"),Gh,"rma","control"),B(n,Yh,"rma"),B(Fn(n,"control"),Gh,"rma","control");let r=typeof e=="object"&&"yi"in e,o=Array.from(r?e.yi:e),s=(r?t:n)??{},i=Array.from(r?e.vi:t??[]);if(o.length===0||i.length!==o.length)throw new RangeError("yi and vi must have the same positive length");if(o.some(K=>!Number.isFinite(K)))throw new Error("NA/NaN/Inf in foreign function call (arg 1)");if(i.some(K=>!Number.isFinite(K)))throw new Error("NA/NaN/Inf in foreign function call (arg 1)");if(s.method==="DL"&&i.some(K=>K<=0))throw new Error("DL estimator cannot be used when there are non-positive sampling variances in the data.");if(i.some(K=>K<0))throw new RangeError("sampling variances must be non-negative");if(i.some(K=>K===0)&&!["EE","FE"].includes(s.method??"REML"))throw new RangeError("random-effects estimators require positive sampling variances");let c=s.method??"REML",a=s.test??"z",l=s.level??95,f=s.intercept??!0,m=VN(o.length,s.mods,f,s.data),u=m.x,h=u[0].length,p=o.length,w=p-h,d=m.names[0]==="intrcpt"||!f&&u.every(K=>K[0]===1);if(h===0)throw new RangeError("the model must contain at least one coefficient");if(p<h)throw new Error("Number of parameters to be estimated is larger than the number of observations.");if(!(l>0&&l<100))throw new RangeError("level must be between 0 and 100");if(!["EE","FE","DL","HE","HS","HSk","SJ","ML","REML","EB","PM"].includes(c))throw new RangeError(`unknown method '${c}'`);if(!["z","t","knha"].includes(a))throw new RangeError(`unknown test '${a}'`);if(c==="DL"&&!Number.isFinite(Gt(o,i,u,0).qe))throw new Error("NA/NaN/Inf in foreign function call (arg 1)");let b=Gt(o,i,u,0),N=Zh(o,i,u,c,s.control??{}),v=Gt(o,i,u,N),E=s.mods===void 0?N:Zh(o,i,Array.from({length:p},()=>[1]),c,s.control??{}),g=a==="knha"&&w>0?Math.max(1,v.qe/w):1,y=v.cov.map(K=>K.map(X=>X*g)),x=v.se.map(K=>K*Math.sqrt(g)),M=v.beta.map((K,X)=>K/x[X]),R=a==="z"?le(1-(1-l/100)/2):Wt(1-(1-l/100)/2,w),T=M.map(K=>2*(a==="z"?se(-Math.abs(K)):Qt(-Math.abs(K),w))),S=v.beta.map((K,X)=>K-R*x[X]),A=v.beta.map((K,X)=>K+R*x[X]),C=d?1:0,L=h===1?1:Math.max(1,h-C),k=0,F=1;if(h===1)k=M[0]*M[0],F=a==="z"?It(k,1,{lowerTail:!1}):Rn(k,1,w,{lowerTail:!1});else{let K=v.beta.slice(C),X=y.slice(C).map(re=>re.slice(C));try{k=En(K,nt(X,K))}catch{k=NaN}F=a==="z"?It(k,L,{lowerTail:!1}):Rn(k/L,L,w,{lowerTail:!1})}let I=b.tracePNorm,O=w>0?w*b.minVariance/I:0,q=c==="EE"||c==="FE"?b.qe>0?100*Math.max(0,(b.qe-w)/b.qe):0:N===0?0:100*N/(N+O),$=c==="EE"||c==="FE"?w>0?Math.max(1,b.qe/w):1:N===0?1:(N+O)/O,_=s.mods===void 0||h===1?null:E>0?100*Math.max(0,(E-N)/E):0,P=PN(o,i,u,c,N,v,b);return Q("rma",{beta:{dim:[h,1],rowNames:h===1?m.names[0]??"intrcpt":m.names,colNames:null,rows:v.beta.map(K=>[K])},se:Mn(x),zval:Mn(M),pval:Mn(T),"ci.lb":Mn(S),"ci.ub":Mn(A),tau2:N,"se.tau2":P,QE:b.qe,QEp:w===0?1:It(b.qe,w,{lowerTail:!1}),QM:k,QMp:F,R2:_,p:h,m:L,I2:q,H2:$,k:p,yi:o,vi:i,mods:u,intercept:d,method:c,test:a,level:l,warnings:m.warnings,covariance:y,tau2Null:E})}function jN(e,t={}){B(t,FN,"confintRma");let n=t.type??"QP",r=t.level??e.level,o=1-r/100;if(n!=="QP"&&n!=="PL")throw new RangeError("type must be 'QP' or 'PL'");let s=Gt(e.yi,e.vi,e.mods,e.tau2),i=e.k-e.mods[0].length,c=0,a=NaN;if(n==="QP"){let d=Gt(e.yi,e.vi,e.mods,0),b=On(1-o/2,i),N=On(o/2,i),v=d.qe,E=g=>Gt(e.yi,e.vi,e.mods,g).qe;c=v<=b?0:Qh(g=>E(g)-b,Math.max(e.tau2,1e-8)),a=v<=N?NaN:Qh(g=>E(g)-N,Math.max(e.tau2,1e-8))}else{let d=On(r/100,1),b=v=>Gt(e.yi,e.vi,e.mods,v).logLik,N=v=>2*(s.logLik-b(v))-d;if(e.tau2>0){c=N(0)<=0?0:po(N,0,e.tau2);let v=Math.max(e.tau2*2,1e-8),E=N(v);for(let g=0;g<80&&E<0;g++)v*=2,E=N(v);a=E>=0?po(N,e.tau2,v):NaN}else{let v=1e-8,E=N(v);for(let g=0;g<80&&E<0;g++)v*=2,E=N(v);a=E>=0?po(N,0,v):NaN}}let l=Gt(e.yi,e.vi,e.mods,0),f=i>0?i*l.minVariance/l.tracePNorm:0,m=d=>[d,Math.sqrt(d),100*d/(d+f),(d+f)/f],u=m(e.tau2),h=m(c),p=m(a),w=["tau^2","tau","I^2(%)","H^2"];return{random:{dim:[4,3],rowNames:w,colNames:["estimate","ci.lb","ci.ub"],rows:w.map((d,b)=>({estimate:u[b],"ci.lb":h[b],"ci.ub":p[b]}))},digits:{est:"4",se:"4",test:"4",pval:"4",ci:"4",var:"4",sevar:"4",fit:"4",het:"4"},"ci.null":!1,"lb.sign":"","ub.sign":"","tau2.min":"0"}}function WN(e,t={}){B(t,ON,"predictRma");let n=t.level??e.level,r=1-n/100,o=e.mods[0].length,s;if(t.newmods===void 0)s=o-(e.intercept?1:0)===0?[e.intercept?[1]:[]]:e.mods.map(h=>h.slice());else{let u=t.newmods;if(u.length>0&&typeof u[0]=="number"?s=Array.from(u,h=>e.intercept?[1,h]:[h]):s=Array.from(u,h=>Array.from(h)),s.some(h=>h.length===o-(e.intercept?1:0))&&(s=s.map(h=>e.intercept?[1,...h]:h)),s.some(h=>h.length!==o))throw new RangeError("newmods has the wrong number of columns")}let i=e.beta.rows.map(u=>u[0]),c=s.map(u=>En(u,i)),a=s.map(u=>Math.sqrt(Math.max(0,En(u,ns(e.covariance,u))))),l=e.test==="z"?le(1-r/2):Wt(1-r/2,e.k-o),f=t.predtype==="riley"?Wt(1-r/2,e.k-o-1):l,m=s.map((u,h)=>Math.sqrt(a[h]**2+e.tau2));return{pred:Mn(c),se:Mn(a),"ci.lb":Mn(c.map((u,h)=>u-l*a[h])),"ci.ub":Mn(c.map((u,h)=>u+l*a[h])),"pi.lb":Mn(c.map((u,h)=>u-f*m[h])),"pi.ub":Mn(c.map((u,h)=>u+f*m[h]))}}function BN(e,t,n={}){ct(t)&&B(t,Xh,"regtest"),B(n,Xh,"regtest");let r=typeof e=="object"&&"beta"in e,o=(r?t:n)??{},s=o.model??"rma",i=o.predictor??"sei",c=o.level??95,a=Array.from(r?e.yi:e),l=Array.from(r?e.vi:t??[]);if(a.length!==l.length||a.length<2)throw new RangeError("yi and vi must have equal lengths and at least two studies");let f=l.map(g=>Math.sqrt(g)),m=i==="sei"?f:i==="vi"?l:l.map((g,y)=>1/(y+1)),u,h,p,w,d,b;if(s==="lm"){let g=m.map((T,S)=>[1,1/T]),y=a.map((T,S)=>T/f[S]),x=KN(g,y);u=x.beta[1];let M=Math.sqrt(x.cov[0][0]*x.sigma2),R=Math.sqrt(x.cov[1][1]*x.sigma2);p=x.beta[0]/M,h=R,w=a.length-2,d=2*Qt(-Math.abs(p),w)}else{b=np(a,l,{method:"REML",mods:m,intercept:!0,test:"z"}),u=b.beta.rows[0][0],h=Array.isArray(b.se)?b.se[0]:b.se;let g=Array.isArray(b.zval)?b.zval[1]:NaN,y=Array.isArray(b.pval)?b.pval[1]:NaN;p=g,w=NaN,d=y}let N=s==="lm"?Wt(1-(1-c/100)/2,w):le(1-(1-c/100)/2),v=[];s==="lm"&&(!Number.isFinite(p)||!Number.isFinite(d)||!Number.isFinite(h))&&v.push("NaNs produced");let E={model:s,predictor:i,zval:p,pval:d,dfs:w,est:u,"ci.lb":u-N*h,"ci.ub":u+N*h,warnings:v};return Q("regtest",o.retFit&&b!==void 0?{...E,fit:b}:E)}function KN(e,t){let n=Array.from({length:2},(c,a)=>Array.from({length:2},(l,f)=>e.reduce((m,u,h)=>m+u[a]*u[f],0))),r=nt(n,ep(2)),o=ns(r,Array.from({length:2},(c,a)=>e.reduce((l,f,m)=>l+f[a]*t[m],0))),s=t.map((c,a)=>c-En(e[a],o)),i=t.length>2?s.reduce((c,a)=>c+a*a,0)/(t.length-2):NaN;return{beta:o,cov:r,sigma2:i}}var UN=["type","opposite","twoSided"];function Zc(e,t){if(Number.isNaN(e)||Number.isNaN(t))return NaN;if(!Number.isSafeInteger(t)||t<3)return NaN;if(e<=0)return 1;if(e>=(t-1)/Math.sqrt(t))return 0;let n=t-1,r=jt(n,n),o=Et(jt(e,e),t),s=Le(r,o);if(s[0]+s[1]<=0)return 0;let i=rt(s,r),c=rt(o,r),a=fl((t-2)/2,.5,i,c,!0,!1);return Math.min(1,t*a/2)}function rp(e,t){let n=Zc(e,t);return Number.isNaN(n)?NaN:1-n}function HN(e,t,n=10){if(n!==10)throw new RangeError(`Grubbs type ${n} is not supported in rStats.js 1.9.0 (type 10 only)`);return Aa(e,r=>rp(r,t))}function YN(e){let t=0,n=(e-1)/Math.sqrt(e);for(let r=0;r<80;r++){let o=t+(n-t)/2;if(o===t||o===n)break;Zc(o,e)>=1?t=o:n=o}return n}function GN(e,t){if(Number.isNaN(e)||Number.isNaN(t))return NaN;if(!Number.isSafeInteger(t)||t<3||e<0||e>1)return NaN;if(e===0)return YN(t);if(e===1)return(t-1)/Math.sqrt(t);let n=0,r=(t-1)/Math.sqrt(t);for(let o=0;o<80;o++){let s=n+(r-n)/2;rp(s,t)<e?n=s:r=s}return n+(r-n)/2}function XN(e,t,n=10){if(n!==10)throw new RangeError(`Grubbs type ${n} is not supported in rStats.js 1.9.0 (type 10 only)`);return Aa(e,r=>GN(r,t))}function QN(e,t={}){B(t,UN,"grubbsTest");let n=t.type??10;if(n!==10)throw new RangeError(`Grubbs type ${n} is not supported in rStats.js 1.9.0 (type 10 only)`);let r=Array.from(e).filter(N=>!Number.isNaN(N));if(r.some(N=>!Number.isFinite(N)))throw new RangeError("x must contain finite values");let o=r.length;if(o<2)throw new RangeError("sample size must be at least 2");let s=t.opposite??!1,i=t.twoSided??!1,c=r[0];for(let N=1;N<r.length;N++)(s?r[N]<c:r[N]>c)&&(c=r[N]);let a=cs(r),l=r.indexOf(c),f=Math.abs(a[l]),m=r.slice();m.splice(l,1);let u=xt([r,m]),h=(o-2)*u.variance[1]/((o-1)*u.variance[0]),p=Zc(f,o),w=1-p,d=Number.isNaN(f)?NaN:Math.min(1,i?2*Math.min(w,p):p);return{method:"Grubbs test for one outlier",statistic:[f,h],statisticName:["G","U"],pValue:d,alternative:`${s?"lowest":"highest"} value ${String(c)} is an outlier`,warnings:o===2?["NaNs produced"]:[]}}export{mc as Anova,hv as ICC,pw as TukeyHSD,fb as adTest,Si as anova,Om as ansariTest,uw as aov,A1 as aovEz,Ai as aovSummary,Db as auc,cm as bartlettTest,gi as binomCI,$d as binomDiffCI,Zd as binomDiffEquivalenceTest,jd as binomRatioCI,pd as binomTest,Gb as blandAltman,F0 as boschlooTest,Bg as brunnerMunzelTest,qp as chisqTest,jb as ciAuc,W0 as cochranQTest,Vw as coefci,Dw as coeftest,ku as cohenES,l0 as cohensD,lw as confint,jN as confintRma,Vb as coords,sp as corTest,G1 as coxZph,B1 as coxph,yb as cronbachAlpha,$v as crr,Fv as cuminc,mb as cvmTest,Ub as diagnosticTests,pg as dscfAllPairsTest,bg as dunnTest,ug as dunnettTest,NN as epiSsequb,yN as epiSsequc,MN as epiSsninfb,EN as epiSsninfc,xN as epiSssupb,RN as epiSssupc,b0 as epsilonSquared,c1 as esH,l1 as esW1,f1 as esW2,zN as escalc,g0 as etaSquared,wv as factanal,Io as fisherTest,mm as flignerTest,Oi as friedmanTest,Mp as gTest,gg as gamesHowellTest,Ng as glhtDunnett,Va as glm,Ww as glmAnova,ww as glmConfint,cp as glmCooksDistance,ja as glmHatvalues,lp as glmPredict,ap as glmResiduals,ip as glmRstandard,za as glmSummary,Tp as goodmanKruskalGamma,QN as grubbsTest,f0 as hedgesG,Nb as icc,cb as jonckheereTest,bb as kappa2,vb as kappamFleiss,Us as kruskalTest,Km as ksTest,fg as kwAllPairsDunnTest,mg as kwManyOneDunnTest,Ap as lblTest,Z1 as leveneTest,ub as lillieTest,$o as lm,Sg as mantelhaenTest,hg as manyOneUTest,Gw as mauchlyTest,P0 as mcnemarTest,iv as mcreg,Wr as modelMatrix,um as moodTest,IN as numDEpi,Hd as oddsRatio,xv as omega,w0 as omegaSquared,xm as onewayTest,Kr as pAdjust,j0 as pageTest,ew as pairwisePropTest,Zg as pairwiseTTest,Jg as pairwiseWilcoxTest,Eb as partialR,pb as pearsonTest,HN as pgrubbs,LN as ph2simon,Nd as poissonTest,qv as polychor,Iv as polyserial,wp as powerAnovaTest,SN as powerCT,qN as powerEpi,TN as powerPaired2x2,dp as powerPropTest,pp as powerTTest,fw as predict,zv as predictCrr,WN as predictRma,pi as propTest,tb as propTrendTest,N1 as pwr2p2nTest,w1 as pwr2pTest,E1 as pwrAnovaTest,M1 as pwrChisqTest,x1 as pwrF2Test,v1 as pwrNormTest,b1 as pwrPTest,y1 as pwrRTest,g1 as pwrT2nTest,d1 as pwrTTest,XN as qgrubbs,fm as quadeTest,BN as regtest,np as rma,D1 as rmst2,Fb as roc,Bb as rocTest,op as setWarningHandler,hb as sfTest,Mg as shapiroTest,AN as ssizeCT,kN as ssizeEpi,Rp as stuartMaxwellTest,_1 as survdiff,mr as survfit,rN as survreg,oN as survregPredict,sN as survregResiduals,T0 as tTOST,Nn as tTest,dg as tamhaneT2Test,Dv as timepoints,Xd as tost,S0 as tsumTOST,wg as tukeyTest,im as varTest,Lw as vcovCL,Mw as vcovHC,_o as wilcoxTest,dw as zTest};