@numero-uno-jpn/rstats 1.12.0 → 1.14.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 (84) hide show
  1. package/CHANGELOG.md +18 -0
  2. package/README.ja.md +6 -6
  3. package/README.md +6 -6
  4. package/dist/causal/balance.d.ts +30 -18
  5. package/dist/causal/balance.js +74 -26
  6. package/dist/causal/weightit.d.ts +159 -0
  7. package/dist/causal/weightit.js +491 -0
  8. package/dist/causal.js +2 -2
  9. package/dist/chunks/{chunk-3QOW56EJ.js → chunk-2426YL2X.js} +2 -2
  10. package/dist/chunks/{chunk-JG26HC2H.js → chunk-3746VXPM.js} +2 -2
  11. package/dist/chunks/{chunk-XQSYV3IZ.js → chunk-3DA7CMFN.js} +2 -2
  12. package/dist/chunks/{chunk-O6OMXX5U.js → chunk-3XUHCWLU.js} +1 -1
  13. package/dist/chunks/chunk-4O6XLQBN.js +2 -0
  14. package/dist/chunks/{chunk-CKBUE4FV.js → chunk-62Y36UKH.js} +2 -2
  15. package/dist/chunks/{chunk-NGDBJR3K.js → chunk-6HUTVWPO.js} +2 -2
  16. package/dist/chunks/chunk-6VY3XNAF.js +2 -0
  17. package/dist/chunks/{chunk-7K2LMKZR.js → chunk-7BBIQY4W.js} +2 -2
  18. package/dist/chunks/chunk-7FGVMDJE.js +3 -0
  19. package/dist/chunks/{chunk-2P42OXYW.js → chunk-7VMGP2U7.js} +2 -2
  20. package/dist/chunks/{chunk-YKCZF4ES.js → chunk-A5DZG7FH.js} +2 -2
  21. package/dist/chunks/{chunk-7FXBOYIH.js → chunk-AONDWNMF.js} +2 -2
  22. package/dist/chunks/{chunk-SKQHW2GD.js → chunk-AQHG7BEZ.js} +2 -2
  23. package/dist/chunks/{chunk-NAACMR7H.js → chunk-B6NPCYFO.js} +2 -2
  24. package/dist/chunks/{chunk-WUNTM7PJ.js → chunk-BCXYAILN.js} +2 -2
  25. package/dist/chunks/{chunk-QT32BMYA.js → chunk-CKUBQW2A.js} +2 -2
  26. package/dist/chunks/chunk-D25AVVVI.js +2 -0
  27. package/dist/chunks/{chunk-CHKBDZC4.js → chunk-DZQWGBTW.js} +2 -2
  28. package/dist/chunks/{chunk-HF3QOIZX.js → chunk-EDRBKZ3C.js} +2 -2
  29. package/dist/chunks/{chunk-SZR5HAMB.js → chunk-EWNJT27V.js} +2 -2
  30. package/dist/chunks/{chunk-3QSYPBUM.js → chunk-F6KKW5YN.js} +2 -2
  31. package/dist/chunks/{chunk-T5TE5KCP.js → chunk-FPJ2CWLK.js} +2 -2
  32. package/dist/chunks/{chunk-WSDLT4AR.js → chunk-H5GBKP57.js} +1 -1
  33. package/dist/chunks/{chunk-4B7NEJCE.js → chunk-I2OTBVRK.js} +2 -2
  34. package/dist/chunks/{chunk-ZK7FW5UX.js → chunk-IXFWR3LH.js} +2 -2
  35. package/dist/chunks/chunk-JCGCJT7T.js +2 -0
  36. package/dist/chunks/{chunk-L54WIMTZ.js → chunk-KITNZUYC.js} +2 -2
  37. package/dist/chunks/chunk-MCEZRRQO.js +2 -0
  38. package/dist/chunks/{chunk-UPFMYDF7.js → chunk-MJHFGGPU.js} +2 -2
  39. package/dist/chunks/{chunk-4GRV66HW.js → chunk-MXOBCQQP.js} +2 -2
  40. package/dist/chunks/{chunk-WACTNIKS.js → chunk-NA7EDZ4Q.js} +2 -2
  41. package/dist/chunks/{chunk-SLMS3FQD.js → chunk-PHDTVTKV.js} +1 -1
  42. package/dist/chunks/{chunk-ZHAT6244.js → chunk-QLKZ2OFQ.js} +2 -2
  43. package/dist/chunks/{chunk-MAFZ5FGL.js → chunk-SDIIGAY5.js} +2 -2
  44. package/dist/chunks/{chunk-4U65I4J2.js → chunk-SEKOIW43.js} +2 -2
  45. package/dist/chunks/{chunk-52KGBGFL.js → chunk-TGXUU5NB.js} +1 -1
  46. package/dist/chunks/chunk-VWCCPBBG.js +2 -0
  47. package/dist/chunks/chunk-XXH5OIUO.js +2 -0
  48. package/dist/chunks/{chunk-MJ7IY7AF.js → chunk-Y7VD43TO.js} +2 -2
  49. package/dist/chunks/{chunk-5AR4V7MW.js → chunk-YLUF4WTL.js} +2 -2
  50. package/dist/chunks/{chunk-ULQXNAI6.js → chunk-ZJJJDLRL.js} +2 -2
  51. package/dist/chunks/chunk-ZWSHIUBZ.js +2 -0
  52. package/dist/descriptive.js +2 -2
  53. package/dist/distributions.js +2 -2
  54. package/dist/entries/causal.d.ts +1 -0
  55. package/dist/entries/causal.js +1 -0
  56. package/dist/entries/regression.d.ts +1 -0
  57. package/dist/entries/regression.js +2 -0
  58. package/dist/entries/tests.d.ts +2 -0
  59. package/dist/entries/tests.js +2 -0
  60. package/dist/linalg.js +2 -2
  61. package/dist/mixed.js +2 -2
  62. package/dist/models/multinom.d.ts +250 -0
  63. package/dist/models/multinom.js +520 -0
  64. package/dist/models/survfitCoxph.d.ts +95 -0
  65. package/dist/models/survfitCoxph.js +223 -0
  66. package/dist/models/survival.d.ts +9 -2
  67. package/dist/models/survival.js +15 -11
  68. package/dist/models/svyglm.d.ts +77 -0
  69. package/dist/models/svyglm.js +102 -0
  70. package/dist/multivariate.js +2 -2
  71. package/dist/numerics.js +2 -2
  72. package/dist/regression.js +3 -3
  73. package/dist/rstats.js +17 -17
  74. package/dist/special.js +2 -2
  75. package/dist/tests.js +15 -15
  76. package/dist/timeseries.js +2 -2
  77. package/package.json +1 -1
  78. package/dist/chunks/chunk-7UI4PW32.js +0 -2
  79. package/dist/chunks/chunk-CMTCPLIA.js +0 -2
  80. package/dist/chunks/chunk-DWVTVOQS.js +0 -2
  81. package/dist/chunks/chunk-FFMMKXMK.js +0 -2
  82. package/dist/chunks/chunk-MUYGPZ37.js +0 -3
  83. package/dist/chunks/chunk-NQT7ZWL6.js +0 -2
  84. package/dist/chunks/chunk-PYFASERO.js +0 -2
package/dist/tests.js CHANGED
@@ -1,22 +1,22 @@
1
- /*! rStats.js 1.12.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 Ts,f as Fa}from"./chunks/chunk-5AR4V7MW.js";import{a as vn,c as sn,h as jr,i as bl,m as vl,n as Wr,o as Nl,s as ds,t as yl,u as yo,v as gs,w as ws,x as bs}from"./chunks/chunk-WUNTM7PJ.js";import{b as Sl}from"./chunks/chunk-4GRV66HW.js";import{a as Wa,e as Ba}from"./chunks/chunk-QT32BMYA.js";import{a as Ns,b as tr,c as xo,d as Il,e as _l,f as ys,g as Ze,h as cn,i as Nn,j as _t,k as Ol,l as cp,n as As,o as Ao,p as ks}from"./chunks/chunk-NQT7ZWL6.js";import"./chunks/chunk-CMTCPLIA.js";import{a as gl,d as ps}from"./chunks/chunk-PYFASERO.js";import{b as Bt,c as Ul}from"./chunks/chunk-JG26HC2H.js";import{d as nt}from"./chunks/chunk-4U65I4J2.js";import{a as Ss}from"./chunks/chunk-XQSYV3IZ.js";import"./chunks/chunk-MJ7IY7AF.js";import{a as Ro,b as Oa,c as Da,d as Wl,e as To,f as Va,g as Wn,h as $a,i as So,j as Bl,k as za,l as lp,m as ja,n as Kl,o as fp,p as mp,q as up}from"./chunks/chunk-7FXBOYIH.js";import{b as xt,c as On}from"./chunks/chunk-WACTNIKS.js";import{a as Ll,c as Cl,d as Fl,e as br,g as ko}from"./chunks/chunk-MUYGPZ37.js";import{b as La}from"./chunks/chunk-WSDLT4AR.js";import{a as ct,b as Ms,c as Es}from"./chunks/chunk-ZHAT6244.js";import{a as $l,b as zl,c as Rs,d as Pa}from"./chunks/chunk-L54WIMTZ.js";import{a as jn,b as Kr}from"./chunks/chunk-SKQHW2GD.js";import{d as is,e as cs,g as ls,h as zr,m as hn,n as Dn}from"./chunks/chunk-3QSYPBUM.js";import{d as ht,e as un,g as Tn,h as Gn,j as Xn}from"./chunks/chunk-YKCZF4ES.js";import{a as jl}from"./chunks/chunk-SZR5HAMB.js";import{c as Dl,h as Pl,j as Lt,m as Ca,n as Vl,o as xs,p as Br}from"./chunks/chunk-4B7NEJCE.js";import{a as er,b as kl,g as vs}from"./chunks/chunk-3QOW56EJ.js";import{a as _a,b as dt,c as gt,d as wl,e as Qt,f as Wt}from"./chunks/chunk-UPFMYDF7.js";import{i as wt,l as as,t as hl}from"./chunks/chunk-HF3QOIZX.js";import{a as Sn,c as ql}from"./chunks/chunk-CHKBDZC4.js";import{a as ip,c as Z,d as ot,e as Tt}from"./chunks/chunk-7UI4PW32.js";import{b as Ml,c as El,d as xl,e as Rl,f as Tl,i as Mo}from"./chunks/chunk-DWVTVOQS.js";import{a as Eo,b as $n,c as Al,d as Pt,e as vt}from"./chunks/chunk-SLMS3FQD.js";import{a as Nt,b as dn,c as an}from"./chunks/chunk-O6OMXX5U.js";import"./chunks/chunk-CKBUE4FV.js";import{b as fl,c as ml}from"./chunks/chunk-FFMMKXMK.js";import{h as il,i as cl,k as ll,t as ul}from"./chunks/chunk-NGDBJR3K.js";import{f as Aa,r as Xt,s as se,t as ie}from"./chunks/chunk-ULQXNAI6.js";import{A as Vt,B as hs,C as ke,D as Ia,c as ka,d as No,e as Pn,f as pn,g as Vn,h as $,i as Ve,j as Qn,k as it,l as at,m as Zn,n as pl,p as fs,q as Rt,r as ms,s as dl,t as us,u as qa,v as De,w as bt,x as bn,y as Dt,z as Jn}from"./chunks/chunk-NAACMR7H.js";import{b as Ot,c as jt,d as he,e as sl,f as Le,g as _e,h as Et,i as rt,j as ft,k as al,l as B,m as wn}from"./chunks/chunk-52KGBGFL.js";var Hl=["alternative","mu","paired","varEqual","confLevel"];function yn(e,t,n={}){if(typeof e!="string"&&ct(t)&&B(t,Hl,"tTest"),B(n,Hl,"tTest"),typeof e=="string")return hp(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}=ms(A,C))}else{let A=ke(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=$(s,-m),h,p,w,d,v,N,b,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,v=i?"mean difference":"mean of x",b=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),_=A>1?at(f):NaN;if(N=`${c?"":"Welch"} Two Sample t-test`,d=[y,C],v=["mean of x","mean of y"],b="difference in means",c){p=E+A-2;let q=0;E>1&&(q+=(E-1)*x),A>1&&(q+=(A-1)*_),q/=p,w=Math.sqrt(q*(1/E+1/A))}else{let q=x/E,F=_/A;w=Math.sqrt(Math.sqrt(q)**2+Math.sqrt(F)**2),p=us([q,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 _=u+(h+C*A)*w;return Number.isFinite(_)&&Math.max(Math.abs(u),Math.abs(h*w))<=16*(Math.abs(M)+A*w)?_: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=[$(S[0],m),$(S[1],m)],d=typeof d=="number"?$(d,m):d.map(A=>$(A,m)),w=$(w,m),{method:N,alternative:o,statistic:h,statisticName:"t",parameter:p,parameterName:"df",pValue:T,confInt:S,confLevel:a,estimate:d,estimateNames:v,nullValue:s,nullValueName:b,stdErr:w,stdError:w,warnings:[]}}function hp(e,t,n){if(n.paired!==void 0)throw new Error("cannot use 'paired' in formula method");let r=Ns(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 pp=new Set(["n","delta","sd","sigLevel","power","type","alternative","strict","tol"]),dp=["n","p1","p2","sigLevel","power","alternative","strict","tol"],gp=["groups","n","betweenVar","withinVar","sigLevel","power"],Gl=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 qo=e=>e>=0&&e<=1,qs=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 Fl(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 wp(e={}){B(e,pp,"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,qo,"'power' must be a probability"),ln(o,qs,"'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,qs,"'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??Gl,f=i==="two.sample"?2:1,m=c==="two.sided"?2:1;m===2&&!ye(n)&&(n=Math.abs(n));let u=(w,d,v,N)=>{let b=Math.max(1e-7,w-1)*f,E=Wt(N/m,b,{lowerTail:!1}),g=Math.sqrt(w/f)*d/v;Number.isFinite(g)||(g=Math.sqrt(w/f)*(d/v));let y=Nr(E,b,g,!1);return a&&m===2?y+Nr(-E,b,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(v=>u(t,n,v,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 bp(e={}){B(e,dp,"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,qo,"'power' must be a probability"),ln(n,qo,"'p1' must be a probability"),ln(r,qo,"'p2' must be a probability"),ln(t,u=>u>0,"number of observations in each group must be positive"),ln(e.tol,qs,"'tol' must be a positive finite number");let i=Ae(e.alternative,["two.sided","one.sided"]),c=e.strict??!1,a=e.tol??Gl,l=i==="two.sided"?2:1,f=(u,h,p,w)=>{let d=ie(w/l,{lowerTail:!1});if(c&&l===2){let v=(h+p)/2,N=h*(1-h),b=p*(1-p),E=v*(1-v),g=Math.abs(h-p),y=Math.sqrt(N+b);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 Z("powerPropTest",{n:t,p1:n,p2:r,sigLevel:s,power:o,alternative:i,note:"n is number in *each* group",method:"Two-sample comparison of proportions power calculation",warnings:m})}var Yl=1e16,Is=(e,t)=>e>Yl&&e<1/0&&t>Yl;function _s(e,t,n,r){let o=ie(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 vp(e,t,n,r){if(n===0)return 0;let o=l=>Math.floor(Math.log2(l)),[s,i,c,a]=[o(e),o(t),o(n),o(r)];return $($(e,-s)*$(t,-i)*$(n,-c)/$(r,-a),s+i+c-a)}function Np(e={}){B(e,gp,"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,qo,"'power' must be a probability"),ln(r,a=>a>=0&&a<1/0,"'betweenVar' must be a nonnegative finite number"),ln(o,qs,"'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=vp(a-1,l,f,m));let p=a-1,w=(l-1)*a;return h===1/0?1:Is(p,w)?_s(p,w,h,u):Tn(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 yp=["correct","p","rescaleP"],Mp=["confLevel"],Ep=["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 Xl(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 Io(e,t){return e.length>0&&Array.isArray(e[0])?e.map((n,r)=>n.map((o,s)=>Xl(o,t[r][s]))):e.map((n,r)=>Xl(n,t[r]))}var xp=["none","williams","yates"],Zl="Log likelihood ratio (G-test)";function Rp(e,t={}){B(t,yp,"gTest");let n=Ae(t.correct,xp);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 Ql(o.flat()),o.length===1?Z("gTest",Ha(o[0],n,t)):o[0].length===1?Z("gTest",Ha(o.map(s=>s[0]),n,t)):Z("gTest",Tp(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 Ql(r),Z("gTest",Ha(r,n,t))}function Ql(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 Jl(e,t){return e<1/0?0:Math.ceil(Math.log2(t))+1}function ef(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,cl(o,s))}return n[0]+n[1]}function Tp(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=Jl(o[0],n*r),i=s===0?e:e.map(b=>b.map(E=>$(E,-s))),c=s===0?o:Zt(i.flat()),a=i.map(b=>Zt(b)),l=i[0].map((b,E)=>Zt(i.map(g=>g[E]))),f=a.map(b=>l.map(E=>_e(b,rt(E,c)))),m=i;if(t==="yates"){let[[b,E],[g,y]]=i,x=b*y-E*g>0?-.5:.5,M=$(.5,-s),R=x<0?-M:M;m=[[b+R,E-R],[g-R,y+R]]}let u=ef(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 b=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+$((b*E-1)*(b*g-1)/(6*b*(n-1)*(r-1)),-s)}let w=$(2*u,s)/p,d=(n-1)*(r-1),v=f.map(b=>b.map(E=>$(E[0]+E[1],s)));return{method:`${Zl} 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(b=>b.map(E=>$(E,s))),expected:v,ftResiduals:Io(e,v),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(b=>typeof b!="number"||Number.isNaN(b)))throw new RangeError("'p' must not contain missing values");if(s.some(b=>b<0))throw new RangeError("probabilities must be non-negative");if(s.some(b=>b===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=Jl(c[0],r),l=a===0?e:e.map(b=>$(b,-a)),f=a===0?c:Zt(l),m=s.map(b=>o?rt(Et(f,b),i):Et(f,b)),u=m.map(b=>b[0]+b[1]),h=l;if(t==="yates"){let b=$(.5,-a),E=l[0]-u[0];E>$(.25,-a)?h=[l[0]-b,l[1]+b]:-E>$(.25,-a)&&(h=[l[0]+b,l[1]-b])}let p=ef(h,m);if(!o&&!Number.isNaN(p)){let b=f[0]+f[1];p-=b*(i[0]-1+i[1])}let w=Number.isNaN(p)?["NaNs produced"]:[],d=t==="williams"?1+(r+1)/(6*$(f[0]+f[1],a)):1,v=$(2*p,a)/d,N=u.map(b=>$(b,a));return{method:`${Zl} goodness of fit test`,statistic:v,statisticName:"G",parameter:r-1,parameterName:"X-squared df",pValue:Xa(v,r-1),observed:a===0?h:h.map(b=>$(b,a)),expected:N,ftResiduals:Io(e,N),warnings:w}}function Sp(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 Ap(e){let t=Sp(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=>$(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 v=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),v+=y*y/d[g]}let b=Zt(a.flatMap(g=>a.map(y=>t[g][y]))),E=l-1;return v=$(v,r),Z("stuartMaxwellTest",{method:"Stuart-Maxwell test",statistic:v,statisticName:"chi-squared",parameter:E,parameterName:"df",pValue:Xa(v,E),N:b[0]+b[1],warnings:[]})}function kp(e,t={}){B(t,Mp,"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=tf(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=>$(y,-s))),{pc:c,pd:a}=qp(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,v=0;i.forEach((g,y)=>g.forEach((x,M)=>{let R=$(p[y][M],-d);v+=x*R*R}));let N=$(Math.sqrt(v),d-s/2),b=(1-n)/2,E=b<1e-8?ie(b,{lowerTail:!1}):ie(1-b);return Z("goodmanKruskalGamma",{estimate:u,confInt:[Math.max(-1,u-E*N),Math.min(1,u+E*N)],confLevel:n,ase:N,warnings:[]})}function tf(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 qp(e){let t=e.length,n=e[0].length,r=[0,0],o=e.map(p=>{let w=[],d=r;for(let v=0;v<n;v++)w.push(d),d=he(d,[p[v],0]);return w}),s=e.map(p=>{let w=new Array(n),d=r;for(let v=n-1;v>=0;v--)w[v]=d,d=he(d,[p[v],0]);return w}),i=p=>{let w=[],d=new Array(n).fill(r);for(let v=0;v<t;v++)w.push(d),d=d.map((N,b)=>he(N,p[v][b]));return w},c=p=>{let w=new Array(t),d=new Array(n).fill(r);for(let v=t-1;v>=0;v--)w[v]=d,d=d.map((N,b)=>he(N,p[v][b]));return w},a=i(o),l=c(s),f=i(s),m=c(o),u=a.map((p,w)=>p.map((d,v)=>he(d,l[w][v]))),h=f.map((p,w)=>p.map((d,v)=>he(d,m[w][v])));return{pc:u,pd:h}}function Ip(e,t={}){B(t,Ep,"lblTest");let n=Ze(t.alternative),r=tf(e),o=r.length,s=r[0].length,i=(R,T,S)=>{if(R===void 0)return Array.from({length:T},(C,_)=>_+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=>$(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,v=c.map(R=>R-w),N=a.map(R=>R-d),b=u.reduce((R,T,S)=>R+T*v[S]*v[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*v[T]*N[A])),!(b>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(b)/Math.sqrt(E),x=f===0?Math.sqrt(p-1)*y:$(Math.sqrt(p-$(1,-f))*y,f/2),M=n==="less"?se(x):n==="greater"?se(x,{lowerTail:!1}):2*se(-Math.abs(x));return Z("lblTest",{method:"Linear-by-Linear Association Test",alternative:n,statistic:x,statisticName:"Z",pValue:M,warnings:[]})}var nf=["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 _p(e,t,n){return ct(t)&&B(t,nf,"chisqTest"),B(n,nf,"chisqTest"),Array.isArray(t)?Z("chisqTest",Lp(e,t,n??{})):Z("chisqTest",Cp(e,t??{}))}var sf="Chi-squared approximation may be incorrect";function Lp(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(Sn),c=[...new Set(s)].sort(Sn);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])]++),{...mf(f,n),rowLevels:i,colLevels:c}}function Cp(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):mf(r,t)}return Qa(Array.from(e),t)}function af(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 cf(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 lf(e,t){return Number.isNaN(e)?NaN:wt(e,t/2,2,!1,!1)}function ff(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)),v=d===0?o:o.map(b=>$(b,-d)),N=d===0?w:gn(v);o=v.map(b=>b/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=>$(w,-s)),c=s===0?r:gn(i),a=o.map(w=>c*w),l=s===0?a:a.map(w=>$(w,s));ff(l);let f=o.map((w,d)=>a[d]*(1-w)),m=$(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=af(t);if(!Number.isInteger(r))throw new RangeError("the simulation needs counts summing to an integer");u+=Eo(w),h=NaN,p=cf(Fp(r,o,l,w),m,w)}else p=lf(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)=>$((w-a[d])/Math.sqrt(a[d]),s/2)),stdres:i.map((w,d)=>$((w-a[d])/Math.sqrt(f[d]),s/2)),ftResiduals:Io(e,l),warnings:Number.isFinite(h)&&l.some(w=>w<5)?[sf]:[]}}function Fp(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=Sl(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 Op(e,t,n,r){let o=e.length,s=t.length,i=e.reduce((f,m)=>f+m,0);if(i>gs)throw new RangeError("the simulation needs a total count of at most 16777216 (2^24)");let c=ws(i),a=new Int32Array(o*s),l=new Float64Array(Math.trunc(r));for(let f=0;f<l.length;f++){bs(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 mf(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(b=>!(b>=0&&b<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(b=>gn(b)),l=e[0].map((b,E)=>gn(e.map(g=>g[E]))),f=a.map(b=>l.map(E=>hs(b,E,c)));ff(f.flat());let m=e.map((b,E)=>b.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(b=>b>0)&&l.every(b=>b>0),v=!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 b=af(t);u+=Eo(b),h=gn(m.flatMap((g,y)=>g.map((x,M)=>Vt(x,f[y][M])))),p=NaN;let E=Op(a.map(g=>Math.trunc(g)),l.map(g=>Math.trunc(g)),f,b);w=cf(E,h,b)}else if(v>0)u+=" with Yates' continuity correction",h=gn(m.flatMap((b,E)=>b.map((g,y)=>Vt(Math.abs(g)-v,f[E][y]))));else if(h=gn(m.flatMap((b,E)=>b.map((g,y)=>Vt(g,f[E][y])))),n){u="Pearson's 'N-1' Chi-squared test";let b=h*(c-1);h=Number.isFinite(b)?b/c:h*((c-1)/c)}return{method:u,statistic:h,statisticName:"X-squared",parameter:p,parameterName:"df",pValue:w??lf(h,p),observed:e,expected:f,residuals:m.map((b,E)=>b.map((g,y)=>g/Math.sqrt(f[E][y]))),stdres:Dp(e,c,a,l,f,m),ftResiduals:Io(e,f),warnings:Number.isFinite(p)&&f.some(b=>b.some(E=>E<5))?[...N,sf]:N}}function Dp(e,t,n,r,o,s){if(!Number.isFinite(t))return s.map((p,w)=>p.map((d,v)=>d/Math.sqrt(o[w][v]*(1-n[w]/t)*(1-r[v]/t))));let i=e.map(p=>rf(p)),c=e[0].map((p,w)=>rf(e.map(d=>d[w]))),a=Vp(i),l=of(i),f=of(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,v)=>{let N=_e(i[w],u[v]),b=Le([d,0],N),E=b[0]+b[1],g=_e(_e(N,m[w]),h[v]),y=g[0]+g[1];if(y>=Pp||E===0)return E/Math.sqrt(y);let x=N[0]+N[1],M=m[w][0]+m[w][1],R=h[v][0]+h[v][1];return E/Math.sqrt(x)/Math.sqrt(M)/Math.sqrt(R)}))}var Pp=2**-1022;function rf(e){let t=[0,0];for(let n of e)t=he(t,[n,0]);return t}function Vp(e){let t=[0,0];for(let n of e)t=he(t,n);return t}function of(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 Kt(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),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 zp=["alternative","confInt","confLevel","or","simulatePValue","B","midp"];function Co(e,t={}){B(t,zp,"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(ml));if(s.flat().some(f=>f>2147483647))throw new RangeError("'x' has entries too large to be integer");let i=[],c=jp(o,s);c!==null&&i.push(`'x' has been rounded to integer: ${c}`);let a="Fisher's Exact Test for Count Data";if(t.midp!==void 0&&typeof t.midp!="boolean")throw new TypeError("'midp' must be true or false");if((t.midp??!1)&&!(n===2&&r===2))throw new RangeError("the mid-p version ('midp') needs a 2 x 2 table");if(n===2&&r===2)return Z("fisherTest",{...Up(s,t,a),warnings:i});let l=t.alternative??"two.sided";if(t.simulatePValue??!1){let f=t.B??2e3;if(!(Number.isInteger(f)&&f>=1&&f<2**31))throw new RangeError("'B' must be a positive integer");return Z("fisherTest",{method:a+Eo(f),alternative:l,pValue:Wp(s,f),warnings:i})}return Z("fisherTest",{method:a,alternative:l,pValue:Hp(s),warnings:i})}function jp(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: ${Al(a)}`:null}function Wp(e,t){let n=e.filter(d=>d.some(v=>v>0)),r=e[0].map((d,v)=>v).filter(d=>e.some(v=>v[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(v=>d[v])),s=o.map(d=>d.reduce((v,N)=>v+N,0)),i=o[0].map((d,v)=>o.reduce((N,b)=>N+b[v],0)),c=s.length,a=i.length,l=[0,0];for(let d of o.flat())l=he(l,[-ps(d),0]);let f=l[0]/(1+64*Number.EPSILON),m=s.reduce((d,v)=>d+v,0);if(m>gs)throw new RangeError("the simulation needs a total count of at most 16777216 (2^24)");let u=ws(m),h=new Int32Array(c*a),p=0,w=Math.trunc(t);for(let d=0;d<w;d++){bs(s,i,u,h);let v=0;for(let N=0;N<h.length;N++)v-=u[h[N]];v<=f&&p++}return Math.max(0,Math.min(1,(1+p)/(w+1)))}var uf=1+1e-7,Bp=16,Kp=2**24;function _o(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 Ur=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%Bp===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=_o(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?_o(o,s,o.length-1):_o(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?_o(o,s+1,o.length-1):_o(o,0,s-1))+.5*o[s]}};function Ja(e,t,n){let r=e.density(n),o=r[t-e.lo]*uf,s=[0,0];for(let i of r)i<=o&&(s=he(s,[i,0]));return s[0]}function hf(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(uf),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 v=w,N=d+1;for(;N-v>1;){let b=v+Math.floor((N-v)/2);i(b)<=c?v=b:N=b}return v},f=(w,d)=>{let v=w-1,N=d+1;for(;N-v>1;){let b=v+Math.floor((N-v)/2);i(b)<=c?N=b:v=b}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 Lo(e,t){return rr(e,t)}function Up(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>Kp)throw new RangeError(`the exact distribution of the 2 x 2 table has ${m} values, more than 2^24`);let u=new Ur(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 v;if(f===h)v=0;else if(f===p)v=1/0;else{let y=u.mean(1);v=y===f?1:Lo(x=>u.mean(x)-f,y>f)}let N={method:n,alternative:r,pValue:d,estimate:v,estimateNames:"odds ratio",nullValue:o,nullValueName:"odds ratio",warnings:[]};if(!(t.confInt??!0))return N;let b=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:Lo(R=>u.midTail(f,R,!1)-y,M<y)}let x=u.tail(f,1,!1);return x===y?1:Lo(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:Lo(R=>u.midTail(f,R,!0)-y,M>y)}let x=u.tail(f,1,!0);return x===y?1:Lo(M=>u.tail(f,M,!0)-y,x>y)},g;if(r==="less")g=[0,b(1-s)];else if(r==="greater")g=[E(1-s),1/0];else{let y=(1-s)/2;g=[E(y),b(y)]}return{...N,confInt:g,confLevel:s}}var Ls=[[0,0]];function An(e){for(let t=Ls.length;t<=e;t++)Ls.push(he(Ls[t-1],al([t,0])));return Ls[e]}function Cs(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 Hp(e){let t=e.filter(b=>b.some(E=>E>0));if(t.length===0)throw new RangeError("all entries of the table are zero");let n=t[0].map((b,E)=>t.some(g=>g[E]>0));if(t=t.map(b=>b.filter((E,g)=>n[g])),t.length<2||t[0].length<2)return 1;t.length>t[0].length&&(t=t[0].map((b,E)=>t.map(g=>g[E])));let r=t.length,o=t.map(b=>b.reduce((E,g)=>E+g,0)),s=t[0].map((b,E)=>t.reduce((g,y)=>g+y[E],0)).sort((b,E)=>E-b),i=s.length,c=o.reduce((b,E)=>b+E,0),a=sl(An(c));for(let b of o)a=he(a,An(b));for(let b of s)a=he(a,An(b));let l=[0,0];for(let b of t.flat())l=Le(l,An(b));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=(b,E)=>{let g=E.join(","),y=p[b].get(g);if(y!==void 0)return y;let x=he(a,An(E.reduce((C,_)=>C+_,0)));for(let C of E)x=Le(x,An(C));for(let C=b;C<i;C++)x=Le(x,An(s[C]));let M={R:E,KZ:x,max:0,min:0,edges:[],pastHi:[],pastLo:[],count:[],index:new Map};if(p[b].set(g,M),b===i-1){let C=[0,0];for(let _ of E)C=Le(C,An(_));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,_,q)=>{if(C===r-1){S[C]=_;let O=Le(q,An(_)),I=E.map((K,X)=>K-S[X]).sort((K,X)=>K-X),D=w(b+1,I),k=`${I.join(",")}|${O[0]}`,V=R.get(k);V===void 0?R.set(k,{to:D,hi:O[0],lo:O[1],mult:1}):V.mult++;return}let F=Math.max(0,_-T[C+1]),L=Math.min(E[C],_);for(let O=F;O<=L;O++)S[C]=O,A(C+1,_-O,Le(q,An(O)))};if(A(0,s[b],[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)),b===i-2){let C=M.edges.map(D=>{let k=D.to.edges[0];return{v:he([D.hi,D.lo],[k.hi,k.lo]),mult:D.mult}});C.sort((D,k)=>D.v[0]-k.v[0]||D.v[1]-k.v[1]);let _=C.length,q=C[_-1].v[0],F=new Float64Array(_),L=new Float64Array(_),O=new Float64Array(_),I=[0,0];C.forEach(({v:D,mult:k},V)=>{F[V]=D[0],L[V]=D[1];let K=Le(D,[q,0]);I=he(I,[k*Math.exp(K[0])*(1+K[1]),0]),O[V]=I[0]}),M.dist={hi:F,lo:L,cum:O,vmax:q,kv:he(a,[q,0])}}return M},d=w(0,[...o].sort((b,E)=>b-E)),v=new Za,N=(b,E,g,y)=>{if(E+b.max<=u-m){let[A,C]=Cs(b.KZ[0],b.KZ[1],E,g);return v.add(A,C,y),!0}if(E+b.min>u+m)return!0;let x=b.dist;if(x===void 0)return!1;let[M,R]=Cs(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]=Cs(x.kv[0],x.kv[1],E,g);v.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 b=0;b<i-2;b++)for(let E of p[b].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,_]=Cs(g[A],y[A],R,T),q=x[A]*S;if(N(M,C,_,q))continue;let F=M.index.get(C);F===void 0?(M.index.set(C,M.count.length),M.pastHi.push(C),M.pastLo.push(_),M.count.push(q)):M.count[F]+=q}E.pastHi=E.pastLo=E.count=[],E.index.clear()}}return Math.min(1,v.value())}function Fs(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]=Fs(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]=Fs(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]=Fs(e),r=n.length,o=Math.min(t,r-t),s=pf(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 pf(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]=Fs(e),o=r.length,s=Math.min(t,o-t),i=pf(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],v=c[w-1],N=w/h,b=(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]=b*d[M-E]+N*v[M-y];for(let M=x-1;M>=E;M--)d[M-E]=b*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 df=["alternative","mu","paired","exact","correct","confInt","confLevel","tolRoot"];function Fo(e,t,n={}){if(typeof e!="string"&&ct(t)&&B(t,df,"wilcoxTest"),B(n,df,"wilcoxTest"),typeof e=="string"){if(n.paired!==void 0)throw new Error("cannot use 'paired' in formula method");let w=Ns(e,t,!0);return w.kind==="one"?Fo(w.x,n):Fo(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=Gp(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((v,N)=>v-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 Tt};return Z("wilcoxTest",h===void 0?ed(u,c,i,p):cd(u,h,c,p))}function Gp(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 Os(e,t,n){return n?t==="two.sided"?Math.sign(e)*.5:t==="greater"?.5:-.5:0}var bf=10*Number.EPSILON;function vf(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 Xp(e){let t=e*e;return[e*(t-3),e*(t*(t-10)+15),e*(t*(t*(t-21)+105)-105)]}function Qp(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 Nf(e,o,s,n)}function Zp(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 Nf(e,i,c,r)}function Nf(e,t,n,r){let[o,s,i]=Xp(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,Jp=40;function ii(e){return new RangeError(`wilcoxTest: the exact conditional ${e} would take too long or too much memory: use exact: false`)}function Ds(e,t){if(e.size>16777216||e.steps>ai)throw ii(t);return e.steps}function yf(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 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 ed(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=bn(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,_=C*(C+1)/2,q;if(o==="less")q=jr(x,C,!0,!1);else if(o==="greater")q=jr(x-1,C,!1,!1);else{let L=x>_/2?jr(x-1,C,!1,!1):jr(x,C,!0,!1);q=Math.min(1,2*L)}let F=f(M,x,q);return r.confInt?{...F,...td(e,r)}:F}let R=g.filter((C,_)=>i[_]!==0);Ds(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 _ of R)C+=_/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,...rd(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]=yf(m),v=bn(bt(m.map(Math.abs))).length>0||a?Math.min(r.correct,0):r.correct,N=(p-Os(p,o,v>=0))/w,b=vf(N,o,v>=1?Qp(N,u,v):0),E=f(`Wilcoxon signed rank test${r.correct>=0?" with continuity correction":""}`,h,b);return r.confInt?{...E,...od(e,r)}:E}function fi(e,t,n){return e==="less"?[-1/0,n]:e==="greater"?[t,1/0]:[t,n]}function gf(e,t,n){let r={confInt:fi(e,NaN,NaN),confLevel:0,estimate:t};return n!==void 0&&(r.estimateNames=n),r}function Mf(e){if(!(e>0&&e<1/0))throw new RangeError("tolRoot must be a positive finite number")}function Ef(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 xf(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+bf&&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 td(e,t){let n=e.length;return xf(Ef(e),r=>jr(r,n,!0,!1),r=>bl(r,n,!0,!1),t,"(pseudo)median")}function Rf(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 Tf(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)+bf,w=f!=="less",d=f!=="greater",v=q=>dn(e[q-1],e[q]),N=Math.min(u-1,Math.max(1,Math.ceil(i.mean+ie(Math.min(p,1))*i.sd+.1*i.sd+3))),b=8,E=q=>q===0?e[0]-1:v(q),g=q=>q===0?e[u-1]+1:v(u-q),y=[new Map,new Map];{let q=new Map,F=0,L=Jp*c;for(let[O,I]of[[0,E],[1,g]])if(!(O===0?!w:!d))for(let D=0;D<=N;D++){if(F+=L,F>ai)throw ii("interval");let k;try{k=t(I(D))}catch{break}if(y[O].set(D,[k.stat,k.key]),q.has(k.key)){q.delete(k.key),q.set(k.key,!0);continue}if(F+=r(k.scores),F>ai)throw ii("interval");q.set(k.key,!0),q.size>b&&q.delete(q.keys().next().value)}}let x=new Map,M=(q,F)=>{let L=q===0?E(F):g(F),O=y[q].get(F),I;if(O===void 0){let K=t(L);O=[K.stat,K.key],I=K.scores}let[D,k]=O,V=x.get(k);return V!==void 0?x.delete(k):(V=n(I??t(L).scores),x.size>=b&&x.delete(x.keys().next().value)),x.set(k,V),[D,V]},R=q=>{let[F,L]=M(0,q);return o(L,F)},T=q=>{let[F,L]=M(1,q);return s(L,F)},S=-1/0,A=0;if(w){let q=R(0);if(!(q>p)){let F=q,L=1;for(;L<=u-1;L++){let O=R(L);if(O>p)break;F=O}S=e[L-1],A=F}}let C=1/0,_=0;if(d){let q=T(0);if(!(q>p)){let F=q,L=u-1;for(;L>=1;L--){let O=T(u-L);if(O>p)break;F=O}C=e[L],_=F}}return{confInt:[S,C],confLevel:1-(A+_),estimate:ci(e),estimateNames:l}}function nd(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 rd(e,t){let n=e.length,r=s=>{let i=nd(e,s),{ranks:c,key:a}=Rf(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 Tf(Ef(e),r,ri,s=>Ds(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 od(e,t){let{alternative:n,confLevel:r,tolRoot:o}=t,s=t.correct>=0,i=e.length;if(i===0)return gf(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"),gf(n,dn(c,a),"midrange");let l=(b,E)=>{let[,g,y]=yf(e.map(x=>x-b).filter(x=>x!==0));return(g-Os(g,n,E))/y},f=l(c,s),m=l(a,s),u=b=>-ie(b),h=b=>n==="greater"?!(f<u(b)):n==="less"?!(m>ie(b/2)):!(f<u(b/2))&&!(m>ie(b/2)),p=1-r,w=!1;for(;!h(p)&&p<1;)p*=2,w=!0;w&&t.warnings.add("requested conf.level not achievable"),Mf(o);let d=b=>Kt(E=>l(E,s)-b,c,a,{tol:o,fLower:f-b,fUpper:m-b}),v;if(p>=1){let b=ci([...e].sort(li));v=fi(n,b,b)}else n==="greater"?v=[d(u(p)),1/0]:n==="less"?v=[-1/0,d(ie(p))]:v=[d(u(p/2)),d(ie(p/2))];let N=Kt(b=>l(b,!1),c,a,{tol:o,fLower:l(c,!1),fUpper:l(a,!1)});return{confInt:v,confLevel:w?1-Math.min(1,p):r,estimate:N,estimateNames:"(pseudo)median"}}function Sf(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 sd(e,t){let[n,r,o]=Sf(bt([...e,...t]),e.length);return[n,r,o]}function ad(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 id(e,t,n){return Af(e,t,n).ranks}function Af(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=ad(e.map(s=>s/2-n/2),t);return{ranks:o,key:ti(o)}}return Rf([...r,...t])}function cd(e,t,n,r){let{alternative:o,mu:s}=r,i=e.length,c=t.length,a=n??(i<50&&c<50),l=id(e,t,s),[f,m,u,h]=Sf(l,i),p,w;if(a)if(w="Wilcoxon rank sum exact test",!h)p=wf(()=>o==="less"?Wr(f,i,c,!0,!1):o==="greater"?Wr(f-1,i,c,!1,!1):Math.min(2*Wr(f,i,c,!0,!1),2*Wr(f-1,i,c,!1,!1),1));else{Ds(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,b=l.every(g=>g===l[0]),E=(m-Os(m,o,N>=0))/u;p=b?1:vf(E,o,N>=1?Zp(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 v=a?h?fd(e,t,r):wf(()=>ld(e,t,r)):md(e,t,r);return{...d,...v}}function wf(e){try{return e()}catch(t){throw t instanceof RangeError&&t.message.startsWith("wilcox:")?new RangeError(`${t.message}: use exact: false`):t}}function kf(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 ld(e,t,n){let r=e.length,o=t.length;return xf(kf(e,t,!1),s=>Wr(s,r,o,!0,!1),s=>Nl(s,r,o,!0,!1),n,"difference in location")}function fd(e,t,n){let r=e.length,o=t.length,s=i=>{let{ranks:c,key:a}=Af(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 Tf(kf(e,t,!0),s,i=>si(i,r),i=>Ds(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 md(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]=sd(e.map(v=>v-h),t);return(w-Os(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:(Mf(s),Kt(v=>l(v,p)-h,c,a,{tol:s,fLower:w,fUpper:d}))},m=1-o,u;return r==="greater"?u=[f(-ie(m),i),1/0]:r==="less"?u=[-1/0,f(ie(m),i)]:u=[f(-ie(m/2),i),f(ie(m/2),i)],{confInt:u,confLevel:o,estimate:f(0,!1),estimateNames:"difference in location"}}var ud=["method","n"],qf=["holm","hochberg","hommel","bonferroni","BH","BY","fdr","none"];function hd(e){if(qf.includes(e))return e;let t=qf.filter(n=>n.startsWith(e));if(e.length===0||t.length!==1)throw new RangeError(`invalid method "${e}"`);return t[0]}function Hr(e,t={}){B(t,ud,"pAdjust");let n=typeof t=="string"?{method:t}:t,r=hd(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=_f(a,r,c),f=o.slice();return s.forEach((m,u)=>f[m]=l[u]),f}function Ps(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 _f(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=Ps(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=Ps(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=Ps(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?_f(e,"hochberg",n):pd(e,n)}}var If=2**16;function pd(e,t){if(!Number.isInteger(t))throw new RangeError("n must be an integer for the hommel method");if(t>If)throw new RangeError(`n must be at most ${If} for the hommel method (O(n^2) work)`);let n=e.length,r=[...e,...new Array(t-n).fill(1)],o=Ps(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 Lf=["p","alternative","confLevel"],dd=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 gd(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)*dd,s=a=>hn(a,t,n,!1)<=o;if(e<r){let a=is(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=cs(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 wd(e,t,n={}){B(t,Lf,"binomTest"),B(n,Lf,"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 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=gd(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 bd=["T","r","alternative","confLevel"],vd=1+1e-7;function Nd(e,t){if(t===0)return e===0?1:0;if(e===t)return 1;let n=ls(e,t,!1)*vd,r=i=>ls(i,t,!1)<=n,o=Math.floor(t);if(e<t){let i=is(r,Math.max(e+1,o),1/0);return Math.min(1,zr(e,t,!0,!1)+hi(i,t))}let s=cs(r,0,Math.min(e-1,o));return Math.min(1,zr(s,t,!0,!1)+hi(e,t))}var Oo=2**-1022;function hi(e,t){return e-(e-1)===1?zr(e-1,t,!1,!1):zr(e,t,!1,!1)+ls(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 yd(e,t,n,r){let o=n+t*r,s=e*t*n/o;if(Number.isFinite(s)&&Number.isFinite(o)&&o>=Oo&&(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,v=h-m-w,N=v>=0?u/(u+$(d,-v)):u/($(u,v)+d);return $(a*f*N,l+m+Math.min(v,0))}function Md(e,t,n,r){let o=e/n,s=t/r,i=o/s;return t===0||Number.isFinite(o)&&Number.isFinite(s)&&s>=Oo&&(o===0||o>=Oo)?i:e===0?0:e/t*(r/n)}function Ed(e,t={}){B(t,bd,"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(v=>Number.isFinite(v)&&v>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[v,N]=r,[b,E]=i,g=v+N;if(g===1/0)throw new RangeError("'x' must be finite, nonnegative, and integer");let y=c*b,x=y+E,M=Number.isFinite(x)&&x>=Oo&&(y===0||y>=Oo)?y/x:1/(1+E/b/c),R=ui(Math.round(v),Math.round(v)+Math.round(N),{p:M,alternative:a,confLevel:l}),T=E/b,S=A=>{if(A===0)return 0;let C=A/(1-A);if(Number.isFinite(T)||C===1/0)return C*T;let[_,q]=Mr(C),[F,L]=Mr(b),[O,I]=Mr(E);return $(_*O/F,q+I-L)};return{method:"Comparison of Poisson rates",alternative:a,statistic:v,statisticName:"count1",parameter:yd(g,c,b,E),parameterName:"expected count1",pValue:R.pValue,confInt:[S(R.confInt[0]),S(R.confInt[1])],confLevel:l,estimate:Md(v,N,b,E),estimateNames:"rate ratio",nullValue:c,nullValueName:"rate ratio",warnings:[]}}let f=r[0],m=i[0],u=c*m,h;a==="less"?h=zr(f,u,!0,!1):a==="greater"?h=hi(f,u):h=Nd(f,u);let p=v=>f===0?0:as(v,f,1,!0,!1),w=v=>v<1e-8?as(v,f+1,1,!1,!1):as(1-v,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 v=(1-l)/2;d=[p(v),w(v)]}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 Cf=["p","alternative","confLevel","correct"],xd="Chi-squared approximation may be incorrect";function pi(e,t,n={}){ct(t)&&B(t,Cf,"propTest"),B(n,Cf,"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,v=!1;if(d>0&&a===1){let S=Math.abs(r[0]-o[0]*l[0]);S<=d&&([d,v]=[S,!0])}else if(d>0&&a===2&&!f){let S=Math.abs(p)/w;S<=d&&([d,v]=[S,!0])}let N=Rd(r,o),b=o.some((S,A)=>{let C=l?.[A]??N;return S*C<5||S*(1-C)<5}),E=0;if(!v)for(let S=0;S<a;S++){let A=l?.[S]??N,C=o[S]*A,_=o[S]*(1-A);E+=Vt(Math.abs(r[S]-C)-d,C),E+=Vt(Math.abs(o[S]-r[S]-_)-d,_)}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?ie(S,{lowerTail:!1}):ie(u==="two.sided"?(1+m)/2:m);if(a===1){let C=o[0],_=A*A,q=(D,k)=>(D+_/(2*C)+k*A*Math.sqrt(D*(1-D)/C+_/(4*C*C)))/(1+_/C),F=h[0]+d/C,L=h[0]-d/C,O=Math.min(1,Math.max(0,F>=1?1:q(F,1))),I=Math.min(1,Math.max(0,L<=0?0:q(L,-1)));x=u==="two.sided"?[I,O]:u==="greater"?[I,1]:[0,O]}else{let C=Math.sqrt(h.reduce((L,O,I)=>L+O*(1-O)/o[I],0)),_=A*C+d*w,q=Math.max(-1,p-_),F=Math.min(1,p+_);x=u==="two.sided"?[q,F]:u==="greater"?[q,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:b?[xd]:[]};return l!==void 0&&(T.nullValue=a===1?l[0]:l,T.nullValueName=M),x!==void 0&&(T.confInt=x,T.confLevel=m),Z("propTest",T)}function Rd(e,t){let n=t.reduce((o,s)=>o+s,0);if(n<1/0)return e.reduce((o,s)=>o+s,0)/n;let r=Math.ceil(Math.log2(t.length));return e.reduce((o,s)=>o+$(s,-r),0)/t.reduce((o,s)=>o+$(s,-r),0)}var Td=["method","confLevel","sides"],Sd=["wilson","wald","wilsoncc","agresti-coull","jeffreys","clopper-pearson","midp"],Ad=["two.sided","left","right"];function Do(e,t){let n=Ae(t,Ad),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?ie(e/2,{lowerTail:!1}):ie(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 Yr(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 Bn(e,t,n,r){let o=Yr(e,n),s=Yr(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 $s(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 Vs(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 Ff(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,Td,"binomCI");let r=Ae(n.method,Sd),o=Do(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]=$s(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:Vs(c,s+.5,i-s+.5,!1),u=s===i?1:Vs(c,s+.5,i-s+.5,!0);break;case"clopper-pearson":m=s===0?0:Vs(c,s,i-s+1,!1),u=s===i?1:Vs(c,s+1,i-s,!0);break;case"midp":m=s===0?0:s===i?Math.exp(Math.log(c)/i):Ff(s,i,c,!1),u=s===i?1:s===0?-Math.expm1(Math.log(c)/i):Ff(s,i,c,!0);break}return m=Math.max(0,m),u=Math.min(1,u),o.sides==="left"?u=1:o.sides==="right"&&(m=0),Z("binomCI",{estimate:l,confInt:[m,u],confLevel:n.confLevel??.95,method:r,sides:o.sides,warnings:[]})}function Po(e){return e===1/0?1:e/(1+e)}function Vo(e,t){return e===0?0:t>0?e*e/t:1/0}function kd(e,t,n,r){return e/t+n/r>1}function qd(e,t,n,r){return js(e,t,n,r,0)}function vi(e,t,n,r,o){if(kd(e,t,n,r)){let[p,w,d,v]=vi(t-e,t,r-n,r,-o);return[w,p,v,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=v=>{let N=0;return i>0&&(N+=i/v),i<c&&(N-=(c-i)/(1-v)),a>0&&(N+=a/(v+f)),a<l&&(N-=(l-a)/(m-v)),N},w=v=>{let N=0;return i>0&&(N-=i/(v*v)),i<c&&(N-=(c-i)/((1-v)*(1-v))),a>0&&(N-=a/((v+f)*(v+f))),a<l&&(N-=(l-a)/((m-v)*(m-v))),N};if(p(0)<=0)u=0;else if(p(m)>=0)u=m;else{let v=0,N=m,b=Id(e,t,n,r,o);u=s?b:b-o,u>v&&u<N||(u=N/2);for(let E=0;E<2e3;E++){let g=p(u);if(g===0)break;g>0?v=u:N=u;let y=u-g/w(u);if(y>v&&y<N||(y=v===0?N/2:N>2*v?Math.sqrt(v*N):v+(N-v)/2),!(y>v&&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 Id(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 _d(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 js(e,t,n,r,o){return Le(Le(ft([e,0],t),ft([n,0],r)),[o,0])[0]}function Ld(e,t,n,r,o,s){return Vo(js(e,t,n,r,o),Ni(e,t,n,r,o,s))}var zs=new Float64Array(1),wi=new BigInt64Array(zs.buffer);function Of(e){return zs[0]=Math.abs(e),e<0?-wi[0]:wi[0]}function sr(e){return wi[0]=e<0n?-e:e,e<0n?-zs[0]:zs[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=Of(i?r:n),a=Of(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 Pf(e,t,n,r,o,s){let i=qd(e,t,n,r);if(o===0)return[i,i];let c=Po(o),a=p=>(p===i?0:Po(Ld(e,t,n,r,p,s)))-c,l=(p,w,d,v)=>{if(d===0)return p;if(v===0)return w;let N=Kt(a,p,w,{tol:1e-300,fLower:d,fUpper:v});return s==="wald"?N:bi(b=>_d(e,t,n,r,b,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 Df(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 Cd(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 Fd(e,t,n,r,o){let[s,i]=Cd(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]},v;if(!(m>u))v=u;else if(!(d(u)[0]>0))v=u;else if(!(d(m)[0]<0))v=m;else{let y=u,x=m;v=a,v>y&&v<x||(v=y+(x-y)/2);for(let M=0;M<2e3;M++){let[R,T]=d(v);if(R===0)break;R>0?y=v:x=v;let S=v-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-v);if(v=S,A<=2**-53*v)break}}let[N,b,E,g]=h(v);return[Math.min(1,Math.max(0,N)),Math.min(1,Math.max(0,b)),Math.min(1,Math.max(0,E)),Math.min(1,Math.max(0,g))]}function Od(e,t,n,r,o){let[s,i,c,a]=Fd(e,t,n,r,o),l=(f,m,u,h)=>u>.5?m*h-(m-f):f-m*u;return Vo(l(e,t,s,i),t*s*i)+Vo(l(n,r,c,a),r*c*a)}function Vf(e,t,n,r,o){let s=e/t/(n/r);return zf(s,o,i=>Od(e,t,n,r,i))}function Dd(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 Pd(e,t,n,r,o){let[s,i]=Dd(e,t,n,r,o),c=e-t*s;return Vo(c,t*s*(1-s))+Vo(c,r*i*(1-i))}function $f(e,t,n,r,o){let s=e*(r-n)/((t-e)*n);return zf(s,o,i=>Pd(e,t,n,r,i))}function zf(e,t,n){if(t===0)return[e,e];let r=Po(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=>Po(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:Po(n(Math.exp(l))))-r;return[s(Df(a,c,-1)),i(Df(a,c,1))]}var Vd=["method","confLevel","sides"],$d=["method","confLevel","sides"],zd=["method","confLevel"],jd=["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]=$s(e,t,i),[p,w]=$s(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]=Pf(e,t,n,r,s.chisq,o);break}return[Math.min(f,m),Math.max(f,m)]}function Wd(e,t,n,r,o={}){B(o,Vd,"binomDiffCI");let s=Ae(o.method,jd),i=Do(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),Z("binomDiffCI",{estimate:m,confInt:[u,h],confLevel:o.confLevel??.95,method:s,sides:i.sides,warnings:[]})}var Bd=["katz.log","koopman"];function Ws(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 Kd(e,t,n,r,o={}){B(o,$d,"binomRatioCI");let s=Ae(o.method,Bd),i=Do(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,Ws(.5,a,l,f,m)[1]]:l===0?[h,p]=[Ws(c,a,.5,f,m)[0],1/0]:c===a&&l===f?[h,p]=Ws(a-.5,a,f-.5,f,m):[h,p]=Ws(c,a,l,f,m):[h,p]=Vf(c,a,l,f,i.chisq),i.sides==="left"?p=1/0:i.sides==="right"&&(h=0),Z("binomRatioCI",{estimate:u,confInt:[h,p],confLevel:o.confLevel??.95,method:s,sides:i.sides,warnings:[]})}var Ud=["midp","fisher","wald","score"],Hd={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"},Yd=2**23,Gd=2**20;function jf(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 Xd(e,t={}){B(t,zd,"oddsRatio");let n=Ae(t.method,Ud);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}=Do(r,void 0),c=e.map((S,A)=>Array.from(S,(C,_)=>Yr(C,`table[${A}][${_}]`))),[[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>Yd)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,v,N;try{d=sn(a-1,a+f,l+m,a+l,!0,!1)+.5*vn(a,a+f,l+m,a+l,!1),v=sn(a,a+f,l+m,a+l,!1,!1)+.5*vn(a,a+f,l+m,a+l,!1),N=w<=Gd?Ja(new Ur(a+f,l+m,a+l),a,1):hf(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 b=2*Math.min(d,v),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 _=Math.abs(c[A][S]-C);y+=_*_/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(()=>Co(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]:$f(a,a+l,f,f+m,i);break}case"midp":{let S=new Ur(a+f,l+m,a+l),A=F=>{if(F===1)return[d,v];let L=S.density(F),O=a-h,I=.5*L[O];return[jf(L,0,O-1)+I,jf(L,O+1,L.length-1)+I]},C=(F,L)=>{let O=F(1);return O===0?1:rr(F,L?O>0:O<0)};R=a===h?0:a===p?1/0:C(F=>A(F)[0]-.5,!1);let _=a===h?0:C(F=>A(F)[1]-o/2,!0),q=a===p?1/0:C(F=>A(F)[0]-o/2,!1);T=[_,q];break}}return Z("oddsRatio",{estimate:R,confInt:T,confLevel:r,method:Hd[n],pValue:{midp:b,fisher:N,chiSquare:M},warnings:x?["Chi-squared approximation may be incorrect"]:[]})}var Qd=["epsilon","paired","varEqual","confLevel","alpha"],Zd=["margin","method","alpha"];function Wf(e,t){if(!(typeof e=="number"&&e>0&&e<.5))throw new RangeError(`'${t}' must be a single number in (0, 0.5)`)}function Jd(e,t,n={}){B(n,Qd,"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)Wf(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(()=>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 Z("tost",{method:a.method.replace("t-test","TOST"),parameter:f,parameterName:"df",pValue:u,result:p[0]>-r&&p[1]<r?"rejected":"not rejected",confInt:p,confLevel:o,estimate:a.estimate,estimateNames:i?"mean of the differences":a.estimateNames,epsilon:r,seDiff:l,warnings:[]})}var e0=["mn","mee","wald"];function t0(e,t,n,r,o={}){B(o,Zd,"binomDiffEquivalenceTest");let s=Ae(o.method,e0),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;Wf(c,"alpha");let[a,l]=Bn(e,t,"x1","n1"),[f,m]=Bn(n,r,"x2","n2"),u=a/l-f/m,h=E=>js(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)],v=Math.max(d[0],d[1]),N=1-2*c,b=yi(a,l,f,m,s,di(2*c,N));return Z("binomDiffEquivalenceTest",{method:s,estimate:u,margin:i,alpha:c,statistic:[p,w],statisticName:"z",pValues:d,pValue:v,result:d[0]<c&&d[1]<c?"rejected":"not rejected",confInt:b,confLevel:N,warnings:[]})}var Ut=new WeakMap;function Ks(e){let t=(n,r)=>n.weights===void 0?Array.from(r):r.filter((o,s)=>n.weights[s]!==0);return ke(e.flatMap(n=>[t(n,n.fittedValues),t(n,n.residuals)])).k}function Us(e,t){return e.map(n=>{if(n.rss>=n0&&n.rss<1/0)return $(n.rss,-2*t);let r=0;return n.residuals.forEach((o,s)=>{let i=n.weights?.[s]??1;if(i===0)return;let c=$(o,-t);r+=i*c*c}),r})}var n0=2**-1022;function Ei(e,t){return e.y.flatMap((n,r)=>(e.weights?.[r]??1)===0?[]:[[e.weights?.[r]??1,$(n,-t)]])}function Hs(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+$(a,-i)*l*l,0),o=n,s=r(o);return Number.isFinite(s)||(o=Hs(e),s=r(o)),!($(t,-o)>=1e-24*s)}function Bf(e){let t=Ks([e]),{pivot:n}=e.qr,r=[],o=new Map;for(let s=0;s<e.rank;s++){let i=e.assign[n[s]],c=o.get(i);c===void 0&&(c={term:i,df:0,ss:0},o.set(i,c),r.push(c)),c.df++,c.ss+=$(e.effects[s],-t)**2}return{terms:r,rdf:e.dfResidual,rss:Us([e],t)[0],k:t}}var Kf=(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 r0(e){let t=Bf(e),n=t.terms.filter(o=>o.term!==0),r=0;return e.fittedValues.forEach((o,s)=>{let i=e.weights?.[s]??1;i!==0&&(r+=i*$(o,-t.k)**2)}),{names:n.map(o=>e.termLabels[o.term-1]),rows:n,rdf:t.rdf,rss:t.rss,k:t.k,perfect:t.rss<1e-10*r}}function Mi(e){let t=r0(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=>$(w,2*o),f=i.map(({df:w,ss:d})=>{let v=d/w/a;return[w,l(d),l(d/w),v,Kf(v,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 o0(e){let t=e.map(f=>f.dfResidual),n=Ks(e),r=Us(e,n),o=f=>$(f,2*n),s=0;t.forEach((f,m)=>{f<t[s]&&(s=m)});let i=r[s]/t[s],c=0;e[s].fittedValues.forEach((f,m)=>{let u=e[s].weights?.[m]??1;u!==0&&(c+=u*$(f,-n)**2)});let a=r[s]<1e-10*c,l=e.map((f,m)=>{if(m===0)return[t[0],o(r[0]),NaN,NaN,NaN,NaN];let u=t[m-1]-t[m],h=r[m-1]-r[m],[p,w]=u>0?[e[m-1],e[m]]:[e[m],e[m-1]],d=N=>N.filter((b,E)=>(p.weights?.[E]??1)!==0),v=u===0||!Rs(d(p.x),d(w.x))?NaN:h/u/i;return v<0&&(v=NaN),[t[m],o(r[m]),u,o(h),v,Number.isNaN(v)?NaN:Kf(v,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 Bs(...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:Bs(e.warnings,c.warnings)},l=Ut.get(c);return l!==void 0&&Ut.set(a,l),Z("anova",a,e)}if(n.some(c=>c.rows.length!==e.rows.length))throw new Error("models were not all fitted to the same size of dataset");let r=(c,a)=>c===a||c!==void 0&&a!==void 0&&c.length===a.length&&c.every((l,f)=>Object.is(l,a[f]));if(n.some(c=>!r(c.rows,e.rows)))throw new Error("models were not all fitted to the same rows of the data");let o=n.filter(c=>c.response===e.response);if(o.some(c=>!r(c.y,e.y)))throw new Error("models were not all fitted to the same response values");if(o.some(c=>!r(c.weights,e.weights)))throw new Error("models were not all fitted with the same weights");let s=[];if(o.length<n.length){let c=[...new Set(n.filter(a=>a.response!==e.response).map(a=>a.response))];s.push(`models with response ${c.map(a=>`\u2018${a}\u2019`).join(", ")} removed because response differs from model 1`)}let i=o.length===1?Mi(e):o0(o);return i.warnings=Bs(...n.map(c=>c.warnings),s,i.warnings),Z("anova",i,...n)}function Ai(e){if(typeof e=="object"&&e!==null&&e.multistratum===!0){let i=e,c=Bs(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)")),Z("aovSummary",a,e)}if(typeof e!="object"||e===null||!Array.isArray(e.effects))throw new TypeError("aovSummary() needs a fit from aov() with a formula");let t=e,n=Mi(t),r=Bf(t),o=Ri(t.fittedValues.map(i=>$(i,-r.k)),r.rss,r.rdf),s=Bs(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),Z("aovSummary",i,e)}return Z("aovSummary",{colNames:n.colNames.slice(0,3),rowNames:n.rowNames.slice(0,-1),table:n.table.slice(0,-1).map(i=>i.slice(0,3)),warnings:s},e)}var s0=["pooledSd","mu","paired","ci","alternative","adjust"],a0=["pooledSd","mu","paired","ci","alternative"],i0=["generalized","partial","ci","alternative"],c0=["partial","ci","alternative"],l0=["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 f0="Missing values detected. NAs dropped.",m0=`'y' is numeric but has only 2 unique values.
3
- If this is a grouping variable, convert it to a factor.`,Gf={tol:0,maxiter:5e3};function xr(e){return e===1/0?1:Math.exp(ul(.5,(e-1)/2)-.5*Math.log(e/2))}function Uf(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,{...Gf,extendInt:"downX"}).root}function Xf(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=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(Dt(f)).size===2&&m.push(m0),(l.some(Number.isNaN)||(f?.some(Number.isNaN)??!1))&&m.push(f0);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,v,N;if(f===void 0||i){let S;if(f===void 0)({groups:[S],k:N}=ke([Dt(l)]));else{let[C,_]=Jn(l,f);({d:S,k:N}=ms(C,_))}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,v=A-1}else{let S=Rt([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 _=Math.sqrt(S.variance[0]),q=Math.sqrt(S.variance[1]);o?(p=Math.sqrt(((A-1)*_**2+(C-1)*q**2)/(A+C-2)),w=p*Math.sqrt(1/A+1/C),d=1/A+1/C,v=A+C-2):(p=Math.sqrt((_**2+q**2)/2),d=2*(C*_**2+A*q**2)/(A*C*(_**2+q**2)),w=Math.sqrt(Math.sqrt(_**2/A)**2+Math.sqrt(q**2/C)**2),v=us([_**2/A,q**2/C],[A-1,C-1]))}let b=r?xr(v):1,E=$(s,-N),g=(h-E)/p*b,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=Uf(M,v,(1+x)/2)*Math.sqrt(d)*b,T=Uf(M,v,(1-x)/2)*Math.sqrt(d)*b;return a==="less"&&(R=-1/0),a==="greater"&&(T=1/0),{name:y,value:g,ci:c,ciLow:R,ciHigh:T,warnings:m}}function u0(e,t,n={}){return B(n,s0,"cohensD"),Z("cohensD",Xf(e,t,n,n.adjust??!1))}function h0(e,t,n={}){return B(n,a0,"hedgesG"),Z("hedgesG",Xf(e,t,n,!0))}var p0=e=>Array.isArray(e.effects),ki=e=>e.multistratum===!0,Qf=e=>Array.isArray(e.strata),Zf=e=>{let t=e;return typeof t.univariate=="object"&&typeof t.anova=="object"},qi=e=>({test:t=>t.split(":").includes(e)});function d0(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 g0(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(v=>qi(v).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 Jf(e,t){let n=Array.isArray(e)?e:[],r=n.filter(s=>!t.some(i=>qi(s).test(i.name)));if(r.length>0)throw new RangeError(`Observed variable not in data: ${r.join(", ")}`);let o=s=>n.some(i=>qi(i).test(s));return{isObserved:o,ssObserved:t.reduce((s,i)=>o(i.name)?s+i.ss:s,0)}}function Hf(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:br(o,0,Math.max(10,e*t*4),{...Gf,extendInt:"downX"}).root:0}function em(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=Hf(c,t,n,(1+s)/2),l=Hf(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 w0(e){let t=ke([e.map(n=>n.ss)]).k;if(t!==0)for(let n of e)n.ss=$(n.ss,-t)}var Yf="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(Yf);if(ki(e)||Qf(e)||Zf(e))return b0(e,t,n);let r=p0(e)?ot(()=>Si(e)):e;if(!Array.isArray(r.colNames)||!Array.isArray(r.table))throw new TypeError(Yf);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=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)");w0(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),v=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}=Jf(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+(v-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)]:em(x(R),R.df,h,i,c)),warnings:Array.isArray(r.warnings)?[...r.warnings]:[]}}function b0(e,t,n){let r=n.ci===void 0?.95:n.ci;Ii(r);let o=Ae(n.alternative??"greater",["two.sided","less","greater"]),s=Zf(e),i,c,a;if(s){let g=Ut.get(e),y=g0(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=ki(e)?ot(()=>Ai(e)):e,y=Ut.get(g);({strata:i,subject:c}=d0(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=ke([[...i.flatMap(g=>g.terms.map(y=>y.ss)),...i.map(g=>g.sse)]]).k;for(let g of i){g.sse=$(g.sse,-l);for(let y of g.terms)y.ss=$(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:v}=Jf(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+v-(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)},b=`${t}${w==="plain"?"":`_${w}`}`,E={colNames:r===null?[b]:[b,"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]:em(x,g.df,y.dfe,r,o)}),warnings:[]};return s||(E.groups=a.map(g=>g.stratum.name),E.warnings=[...ki(e)||Qf(e)?e.warnings:[]]),E}function v0(e,t={}){return B(t,i0,"etaSquared"),Z("etaSquared",_i(e,"Eta2",t),e)}function N0(e,t={}){return B(t,c0,"omegaSquared"),Z("omegaSquared",_i(e,"Omega2",t),e)}function y0(e,t={}){return B(t,l0,"epsilonSquared"),Z("epsilonSquared",_i(e,"Epsilon2",t),e)}var M0=new Set(["eqb","lowEqbound","highEqbound","hypothesis","paired","varEqual","alpha","mu","eqboundType","biasCorrection","rmCorrection","glass","smdCi"]),E0=["m1","sd1","n1","m2","sd2","n2","r12"],x0=new Set(["eqb","lowEqbound","highEqbound","hypothesis","paired","varEqual","alpha","mu","eqboundType","biasCorrection","rmCorrection","glass","smdCi"]),R0="Equivalence interval does not include zero.",T0="Warning: setting bound type to SMD produces biased results!",S0="MET test may have higher error rates than a nil two-tailed test. Consider wider equivalence bounds.";function tm(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 A0(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,v,N=t.biasCorrection;if(e.design==="two"){let{n1:M,n2:R,s1:T,s2:S}=e,A=T*T,C=S*S,_=2*M*R/(M+R);if(t.glass!==void 0){let q=t.glass==="glass1",[F,L,O,I]=q?[T,M,S,R]:[S,R,T,M];c=F,a=L-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 D=O/F;m=Math.sqrt(D*D/(I-1)+1/(L-1)+f*f/(2*(L-1))),u=o/(F*Math.sqrt(1/L+D*D/I)),h=M+R-2,w=Math.sqrt(2/_)}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 q=c**4;m=Math.sqrt(f*f*(A*A/(M-1)+C*C/(R-1))/(8*q)+(A/(M-1)+C/(R-1))/(c*c)),u=o/Math.sqrt(A/M+C/R),h=a,w=Math.sqrt(2/_)}d=a,v=(q,F)=>Math.sqrt(d/(d-2)*(2/_)*(1+q*q*_/2)-q*q/(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),v=(_,q)=>{let F=r==="goulet"?d:a,L=Math.sqrt(F/(F-2)*(2*A/M)*(1+_*_*M/(2*A))-_*_/(q*q));return t.rmCorrection?L:L*Math.sqrt(2*A)},t.glass!==void 0){let _=S/c;m=Math.sqrt(_*_/(M-1)+f*f/(2*(M-1)))}else!t.rmCorrection&&r==="nct"?m=Math.sqrt(1/M+s(l,f,a)):m=v(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),v=(T,S)=>Math.sqrt(a/(a-2)*(1/M)*(1+T*T*M)-T*T/(S*S))}let b,E,g,y=a;if(r==="nct"){g=m,y=h;let M=tm(u,h,n,!0),R=tm(u,h,n,!1);b=M*Math.sqrt(p)*l,E=R*Math.sqrt(p)*l}else if(r==="goulet"){g=v(f,l),y=d;let M=f/w;b=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):ie(n,{lowerTail:!1});b=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,-b]:[b,E],df:y,J:l}}function nm(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 rm(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 om(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 sm(e,t,n,r,o,s,i,c,a,l,f,m){let{alpha:u,hypothesis:h}=l,p=A0(c,l),w=$(p.denom,a),d=l.smdBounds?[f[0]*w,f[1]*w]:f,v=O=>{if(O===0)return[0,n];let I=Math.floor(Math.log2(Math.abs(O))),D=Ve(O,-I);return l.smdBounds?[D*p.denom,I+a]:[D,I]},N=l.smdBounds?f:[f[0]/w,f[1]/w];l.smdBounds&&m.push(T0),d[0]<=0&&d[1]>=0||m.push(R0);let b=O=>$(O/r,n-o),E=O=>{let[I,D]=v(O),k=Math.max(n,D);return Ve((Ve(t,n-k)-Ve(I,D-k))/r,k-o)},g=b(t),y=E(f[0]),x=E(f[1]),M=$(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(S0);let _=gt(u,s,!1,!1),q=Math.max(n,o),[F,L]=[$(t,n-q),$(r,o-q)];return Z(e,{tTest:T,lower:S,upper:A,pValue:C,bounds:{raw:d,smd:N},raw:{estimate:$(t,n),se:M,confInt:[$(F-_*L,q),$(F+_*L,q)]},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 k0(e,t,n={}){B(n,M0,"tTOST");let r=om(n),o=rm(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");nm(e,"x"),a||nm(t,"y");let l=ot(()=>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=ke([Dt(Array.from(e))]);u=w.k;let d=w.groups[0],v=at(d);h=it(d)-$(c,-u),p=Math.sqrt(v/d.length),m={design:"one",n:d.length,delta:h,s:Math.sqrt(v)}}else if(s){let[w,d]=Jn(Array.from(e),Array.from(t)),v=ke([w,d]);u=v.k;let[N,b]=v.groups,E=N.map((T,S)=>T-b[S]),g=Math.sqrt(at(N)),y=Math.sqrt(at(b)),x=at(E),M=Math.sqrt(x),R=(M-(g-y))*(M+(g-y))/(2*g*y);h=it(E)-$(c,-u),p=Math.sqrt(x/E.length),m={design:"paired",n:E.length,delta:it(N)-it(b)-$(c,-u),s1:g,s2:y,sdif:M,oneMinusR:R}}else{let w=ke([Dt(Array.from(e)),Dt(Array.from(t))]);u=w.k;let[d,v]=w.groups;if(d.length<2||v.length<2)throw new RangeError("not enough observations: at least 2 are needed in each sample");let[N,b,E,g]=[d.length,v.length,at(d),at(v)];h=it(d)-it(v)-$(c,-u),p=Math.sqrt(i?((N-1)*E+(b-1)*g)/(N+b-2)*(1/N+1/b):Math.sqrt(E/N)**2+Math.sqrt(g/b)**2),m={design:"two",n1:N,n2:b,delta:h,s1:Math.sqrt(E),s2:Math.sqrt(g)}}return sm("tTOST",h,u,p,u,f,l.method,m,u,r,o,[])}function q0(e,t={}){B(e,E0,"tsumTOST"),B(t,x0,"tsumTOST");let n=om(t),r=rm(t),{m1:o,sd1:s,n1:i,m2:c,sd2:a,n2:l,r12:f}=e,m=(q,F)=>{if(typeof q!="number"||!Number.isFinite(q))throw new RangeError(`'${F}' must be a finite number`);return q},u=(q,F)=>{let L=m(q,F);if(!(Number.isInteger(L)&&L>=2))throw new RangeError(`'${F}' must be an integer of at least 2`);return L},h=(q,F)=>{let L=m(q,F);if(!(L>0))throw new RangeError(`'${F}' must be positive`);return L};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:v}=ke([[o,...p?[]:[c],n.mu]]),{groups:N,k:b}=ke([[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=q=>$(q,v-b),T,S,A,C,_;if(p)T=E-y,S=x/Math.sqrt(i),A=i-1,C="One Sample t-test",_={design:"one",n:i,delta:R(T),s:x};else if(n.paired){let q=m(f,"r12");if(!(q>-1&&q<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 L=Math.sqrt(x*x+M*M-2*q*x*M);if(!(L>0))throw new RangeError("the standard deviation of the differences is 0");T=E-g-y,S=L/Math.sqrt(F),A=F-1,C="Paired t-test",_={design:"paired",n:F,delta:R(T),s1:x,s2:M,sdif:L,oneMinusR:1-q}}else{let q=l,F=x*x,L=M*M;if(T=E-g-y,n.varEqual){let O=((i-1)*F+(q-1)*L)/(i+q-2);S=Math.sqrt(O*(1/i+1/q)),A=i+q-2,C=" Two Sample t-test"}else{let O=F/i,I=L/q;S=Math.sqrt(O+I),A=(O+I)**2/(O*O/(i-1)+I*I/(q-1)),C="Welch Two Sample t-test"}_={design:"two",n1:i,n2:q,delta:R(T),s1:x,s2:M}}return sm("tsumTOST",T,v,S,b,A,C,_,b,n,r,w)}var I0=["alternative","tsmethod"],_0=["square","central"],L0=1e3,C0=32,F0=.9,am=1+1e-7,O0=2**24,D0=34e7;function P0(e,t={}){if(B(t,I0,"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=Ae(t.tsmethod,_0),o=e.map((y,x)=>Array.from(y,(M,R)=>Yr(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)>O0)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(L0,Math.ceil(C0*Math.sqrt(m))),h=y=>Math.min(y+1,40*Math.sqrt(y));if(u*(h(l)+h(f)+4)>D0)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,v;w==="less"?v=sn(s,l,f,d,!0,!1):w==="greater"?v=sn(s-1,l,f,d,!1,!1):v=ot(()=>Co(o)).pValue;let N=V0(l,f,s,c,w),{value:b,at:E}=$0(N,l,f,u),g=n==="two.sided"&&r==="central"?Math.min(1,2*b):b;return Z("boschlooTest",{method:"Boschloo's Exact Test",alternative:n,statistic:v,statisticName:"test statistic",pValue:g,estimate:p,estimateNames:"difference in proportion",nullValue:0,nullValueName:"difference in proportion",nuisanceParameter:E,warnings:[]})}function V0(e,t,n,r,o){let s=e+t,i=new Float64Array(s+1);for(let v=2;v<=s;v++)i[v]=ps(v);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 v=0;v<=s;v++){let N=Math.max(0,v-t),E=Math.min(v,e)-N+1,g=-1/0;for(let M=0;M<E;M++){let R=N+M,T=-i[R]-i[e-R]-i[v-R]-i[t-v+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+v-N-M]=m[M];else if(o==="greater")for(let M=0;M<E;M++)a[(N+M)*c+v-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]*am,S=-1,A=M;for(;A-S>1;){let F=S+A>>1;f[F]<=T?S=F:A=F}let C=M,_=E;for(;_-C>1;){let F=C+_>>1;f[F]<=T?_=F:C=F}let q=f[M]<=T?1:(S>=0?m[S]:0)+(_<E?u[_]:0);a[(N+R)*c+v-N-R]=Math.min(1,q)}}}let h=a[n*c+r]*am,p=new Int32Array(e+2),w=[],d=[];for(let v=0;v<=e;v++){p[v]=w.length;let N=0;for(;N<=t;){if(a[v*c+N]<=h){let b=N;for(;N+1<=t&&a[v*c+N+1]<=h;)N++;w.push(b),d.push(N)}N++}}return p[e+1]=w.length,{starts:p,from:Int32Array.from(w),to:Int32Array.from(d)}}function im(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 $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=b=>{let[E,g]=im(t,b,c),[y,x]=im(n,b,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 _=s[C],q=i[C];if(!(q<y||_>x))if(_===0)A+=l[Math.min(q,x)];else if(q===n)A+=f[Math.max(_,y)];else for(let F=Math.max(_,y);F<=Math.min(q,x);F++)A+=a[F]}R+=S*A}return Math.min(1,R)},u=Math.PI/2,h=b=>b<=0?0:b>=u?1:Math.sin(b)**2,p=new Float64Array(r+2),w=0;for(let b=0;b<=r+1;b++)p[b]=m(h(u*b/(r+1))),p[b]>p[w]&&(w=b);let d=p[w],v=h(u*w/(r+1));if(d>=1)return{value:1,at:v};let N=F0*d;for(let b=0;b<=r+1;b++){let E=p[b];if(E<N||b>0&&!(E>p[b-1])||b<=r&&!(E>=p[b+1]))continue;let g=u*Math.max(0,b-1)/(r+1),y=u*Math.min(r+1,b+1)/(r+1),M=Ll(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)),_=A-2*R+C;if(!(_<0))break;let q=T*(A-C)/(2*_);if(!(Math.abs(q)<T))break;let F=m(h(M+q));F>R*(1+2**-50)&&(M+=q,R=F)}R>d&&(d=R,v=h(M))}return{value:d,at:v}}var cm=["correct"];function z0(e,t,n={}){ct(t)&&B(t,cm,"mcnemarTest"),B(n,cm,"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=j0(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=lm(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+=lm(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 lm(e,t,n){let r=t+n;return r<1/0?Vt(e,r):Vt($(e,-520),$(t,-1040)+$(n,-1040))}function j0(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 W0=["ratio","alternative","confLevel"],Li=2**-1022;function fm(e,t,n={}){if(B(n,W0,"varTest"),typeof e=="string"){let b=tr(e,t);return fm(b.x,b.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]=Rt([r,o]).variance,u=0;if(!(f>=Li&&m>=Li)){let b=Rt([r]),E=Rt([o]);f=b.variance[0],m=E.variance[0],u=2*(b.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),v,N;if(i==="less")v=w,N=[0,h/un(1-c,a,l,!0,!1)];else if(i==="greater")v=d,N=[h/un(c,a,l,!0,!1),1/0];else{v=Math.min(1,2*Math.min(w,d));let b=(1-c)/2,E=b<1e-8?un(b,a,l,!1,!1):un(1-b,a,l,!0,!1);N=[h/E,h/un(b,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:v,confInt:N,confLevel:c,estimate:h,estimateNames:"ratio of variances",nullValue:s,nullValueName:"ratio of variances",warnings:[]}}function mm(e,t){if(typeof e=="string"){let p=xo(e,t);return mm(Il(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=Rt(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=Rt([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),v=d.map((E,g)=>Math.log(s[g])+E),N=Math.max(...v),b=N+Math.log(v.reduce((E,g)=>E+Math.exp(g-N),0))-Math.log(c);m=c*b-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 B0=["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 um(e){let t=0;for(let n of bn(e))t+=n*n*n-n;return t}function Ys(e,t){if(typeof e=="string"){let l=xo(e,t);return Ys(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-um(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(ys(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+=um(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 K0(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=ys(e,t),s=n??{};else if(Array.isArray(n)){let w=Array.from(e),d=t,v=n;if(w.length!==d.length||w.length!==v.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),b=tn(v);if(N.some(M=>typeof M=="number"&&Number.isNaN(M))||b.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(b)).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(b[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 v=0;v<c;v++)l[v]+=d[v]}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 U0(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 hm(e,t){if(typeof e=="string")return hm(ys(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=ke(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 v=bt(w);for(let N=0;N<o;N++){let b=i[d]*(v[N]-(o+1)/2);a+=b*b,c[N]+=b}});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 pm(e,t){if(typeof e=="string"){let h=xo(e,t);return pm(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=Ts(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(ke(n).groups));let i=s.length,a=bt(s).map(h=>ie((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 dm(e,t,n={}){if(B(n,B0,"moodTest"),typeof e=="string"){let E=tr(e,t);return dm(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,v=i*c/(a*(a-1))*w,N=(h-d)/Math.sqrt(v),b=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:b,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 H0=[0,.221157,-.147981,-2.07119,4.434685,-2.706056],Y0=[0,.042981,-.293762,-1.752461,5.682633,-3.582633],G0=[.544,-.39978,.025054,-6714e-7],X0=[1.3822,-.77857,.062767,-.0020322],Q0=[-1.5861,-.31082,-.083751,.0038915],Z0=[-.4803,-.082676,.0030302],J0=[-2.273,.459];function eg(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]=-ie((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(H0,i),a=new Float64Array(n);a[0]=c;let l,f;if(e>5){let m=r[1]/s+Rr(Y0,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 gm(e){let t=ke([e]).groups[0],n=t.length,r=eg(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 tg=1.90985931710274,ng=1.0471975511966;function wm(e,t){if(Number.isNaN(e)||!(t>=3))return NaN;if(t===3){let i=tg*(Math.asin(Math.sqrt(1-e))-ng);return Math.max(i,0)}let n=Math.log(e);if(t<=11){let i=Rr(J0,t);if(n>=i)return 1e-99;let c=Rr(G0,t),a=Math.exp(Rr(X0,t));return se(-Math.log(i-n),{mean:c,sd:a,lowerTail:!1})}let r=Math.log(t),o=Rr(Q0,r),s=Math.exp(Rr(Z0,r));return se(n,{mean:o,sd:s,lowerTail:!1})}var ym=Math.log(2*Math.PI),bm=2e-14,vm=500;function rg(e){return Math.exp(-.5*e*e-.5*ym)}function og(e){return se(e,{logP:!0})}function Mm(e,t=-1/0,n=!1){let r=t===-1/0?La(e,0,2,0,bm,vm):La(e,t,1,0,bm,vm),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 Nm(e,t,n){let r=t.map(Math.sqrt),o=t.map(i=>Math.sqrt(1-i));return Mm(i=>{let c=0;for(let a=0;a<r.length;a++){let l=(r[a]*i+e)/o[a];if(!n){c+=og(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 rg(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,Nm(e,t,r));let o=n/2,s=Math.log(2)+.5*Math.log(o)-.5*ym-il(o);return Math.min(1,Mm(c=>{let a=s-o*(Math.expm1(2*c)-2*c);return a<-745?0:Math.exp(a)*Nm(e*Math.exp(c),t,r)},-1/0,!0))}var sg=["pAdjustMethod"],ag=["pAdjustMethod","alternative"],ig=["alternative"],cg=["pAdjustMethod","alternative"],lg=["method","altp"],fg=["alternative"],mg=["welch"],Em="Ties are present. z-quantiles were corrected for ties.";function kn(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 bn(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 ug(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=Hr(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 $o(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 Gs=e=>se(e,{lowerTail:!1}),Vi=(e,t,n)=>ds(Math.abs(e),t,n,{lowerTail:!1});function $i(e){let t=Rt(e.groups),n=0;for(let r of t.n)r>1&&(n+=r-1);return zo(n,t.ssWithin),{m:t.mean,s2:t.ssWithin/n,df:e.N-e.k,k:t.k}}function zo(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 hg(e,t,n={}){B(n,sg,"kwAllPairsDunnTest");let r=n.pAdjustMethod??"holm",o=kn(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=ug(a.map(f=>f.map(m=>2*Gs(m))),r);return Z("kwAllPairsDunnTest",$o(o,"Dunn's all-pairs test",a,l,r,i!==0?[Em]:[]))}function pg(e,t,n={}){B(n,ag,"kwManyOneDunnTest");let r=Ze(n.alternative),o=n.pAdjustMethod??"single-step",s=kn(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=xm(f,m,r,o);return Z("kwManyOneDunnTest",Pi(s,"Dunn's many-to-one test",f,u,o,c!==0?[Em]:[]))}function xm(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)):Hr(e.map(o=>n==="two.sided"?2*Gs(Math.abs(o)):Gs(n==="greater"?o:-o)),r)}function dg(e,t,n={}){B(n,ig,"dunnettTest");let r=Ze(n.alternative),o=kn(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 Z("dunnettTest",Pi(o,`Dunnett's-test for multiple
4
- comparisons with one control`,f,u,"single-step"))}function gg(e,t,n={}){B(n,cg,"manyOneUTest");let r=Ze(n.alternative),o=n.pAdjustMethod??"single-step",s=kn(e,t);if(o==="single-step"&&s.k<3)throw new RangeError(`for 'p.adjust.method = single-step' at least
1
+ /*! rStats.js 1.14.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 nn,b as zn,c as nr,d as Nr,e as ks,f as Pa}from"./chunks/chunk-YLUF4WTL.js";import{a as vn,c as an,h as Br,i as vl,m as Nl,n as Wr,o as yl,s as bs,t as Ml,u as xo,v as vs,w as Ns,x as ys}from"./chunks/chunk-BCXYAILN.js";import{b as Sl}from"./chunks/chunk-MXOBCQQP.js";import{a as Wa,e as Ua}from"./chunks/chunk-CKUBQW2A.js";import{a as Es,b as tr,c as Ao,d as _l,e as Ll,f as xs,g as Je,h as ln,i as Nn,j as _t,k as Dl,l as fp,n as Is,o as Io}from"./chunks/chunk-MCEZRRQO.js";import{a as _s}from"./chunks/chunk-D25AVVVI.js";import"./chunks/chunk-XXH5OIUO.js";import"./chunks/chunk-ZWSHIUBZ.js";import{a as wl,d as ws}from"./chunks/chunk-4O6XLQBN.js";import{b as Ut,c as Hl}from"./chunks/chunk-3746VXPM.js";import{d as rt}from"./chunks/chunk-SEKOIW43.js";import{a as qs}from"./chunks/chunk-3DA7CMFN.js";import{a as So,b as Va,c as $a,d as Bl,e as ko,f as Yr,g as Kn,h as ja,i as qo,j as Wl,k as Ka,l as mp,m as Ba,n as Ul,o as up,p as hp,q as pp}from"./chunks/chunk-AONDWNMF.js";import{b as Rt,c as On}from"./chunks/chunk-NA7EDZ4Q.js";import{a as Cl,c as Fl,d as Ol,e as vr,g as _o}from"./chunks/chunk-7FGVMDJE.js";import{b as Oa}from"./chunks/chunk-H5GBKP57.js";import{a as ct,b as Rs,c as Ts}from"./chunks/chunk-QLKZ2OFQ.js";import{a as jn,b as Hr}from"./chunks/chunk-AQHG7BEZ.js";import{a as zl,b as jl,c as Ss,d as za}from"./chunks/chunk-KITNZUYC.js";import{d as ht,e as hn,g as Tn,h as Gn,j as Xn}from"./chunks/chunk-A5DZG7FH.js";import{d as fs,e as ms,g as us,h as Kr,m as pn,n as Dn}from"./chunks/chunk-F6KKW5YN.js";import{a as Kl}from"./chunks/chunk-EWNJT27V.js";import{c as Pl,h as Vl,j as Lt,m as Da,n as $l,o as As,p as Ur}from"./chunks/chunk-I2OTBVRK.js";import{a as er,b as ql,g as Ms}from"./chunks/chunk-2426YL2X.js";import"./chunks/chunk-Y7VD43TO.js";import{a as Fa,b as gt,c as wt,d as bl,e as Bt,f as Wt}from"./chunks/chunk-MJHFGGPU.js";import{i as bt,l as ls,t as pl}from"./chunks/chunk-EDRBKZ3C.js";import{a as An,c as Il}from"./chunks/chunk-DZQWGBTW.js";import{b as El,c as xl,d as Rl,e as Tl,f as Al,i as Ro,l as lp,n as J,o as Ze,p as At}from"./chunks/chunk-6VY3XNAF.js";import{a as To,b as $n,c as kl,d as Pt,e as Nt}from"./chunks/chunk-PHDTVTKV.js";import{a as yt,b as gn,c as cn}from"./chunks/chunk-3XUHCWLU.js";import"./chunks/chunk-62Y36UKH.js";import"./chunks/chunk-VWCCPBBG.js";import{b as ml,c as ul}from"./chunks/chunk-JCGCJT7T.js";import{h as cl,i as ll,k as fl,t as hl}from"./chunks/chunk-6HUTVWPO.js";import{f as Ia,r as Zt,s as ae,t as ie}from"./chunks/chunk-ZJJJDLRL.js";import{A as Vt,B as gs,C as Ie,D as Ca,c as _a,d as Eo,e as Pn,f as dn,g as Vn,h as $,i as Ve,j as Qn,k as it,l as at,m as Zn,n as dl,p as hs,q as Tt,r as ps,s as gl,t as ds,u as La,v as Pe,w as vt,x as bn,y as Dt,z as Jn}from"./chunks/chunk-B6NPCYFO.js";import{b as Ot,c as jt,d as pe,e as al,f as De,g as Ce,h as xt,i as ot,j as ft,k as il,l as B,m as Kt}from"./chunks/chunk-TGXUU5NB.js";var Yl=["alternative","mu","paired","varEqual","confLevel"];function yn(e,t,n={}){if(typeof e!="string"&&ct(t)&&B(t,Yl,"tTest"),B(n,Yl,"tTest"),typeof e=="string")return dp(e,t,n);let r;t!==void 0&&!ArrayBuffer.isView(t)&&!Array.isArray(t)?n=t:r=t;let o=Je(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(ln(a),i&&r===void 0)throw new TypeError("y is required when paired is true");let l,f,m;if(i){let[k,C]=Jn(Array.from(e),Array.from(r));({d:l,k:m}=ps(k,C))}else{let k=Ie(r===void 0?[Dt(Array.from(e))]:[Dt(Array.from(e)),Dt(Array.from(r))]);m=k.k,l=k.groups[0],f=k.groups[1]}let u=$(s,-m),h,p,g,d,v,N,b,E=l.length,w=f!==void 0&&c;if(E<1||!w&&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,g=Math.sqrt(x/E),g===0||g<10*Number.EPSILON*Math.abs(y))throw new Error("data are essentially constant");h=(y-u)/g,N=i?"Paired t-test":"One Sample t-test",d=y,v=i?"mean difference":"mean of x",b=i?"mean difference":"mean"}else{let k=f.length;if(k<1||!c&&k<2)throw new Error("not enough 'y' observations");if(c&&E+k<3)throw new Error("not enough observations");let C=it(f),_=k>1?at(f):NaN;if(N=`${c?"":"Welch"} Two Sample t-test`,d=[y,C],v=["mean of x","mean of y"],b="difference in means",c){p=E+k-2;let I=0;E>1&&(I+=(E-1)*x),k>1&&(I+=(k-1)*_),I/=p,g=Math.sqrt(I*(1/E+1/k))}else{let I=x/E,F=_/k;g=Math.sqrt(Math.sqrt(I)**2+Math.sqrt(F)**2),p=ds([I,F],[E-1,k-1])}if(g===0||g<10*Number.EPSILON*Math.max(Math.abs(y),Math.abs(C)))throw new Error("data are essentially constant");h=(y-C-u)/g}let M=typeof d=="number"?d:d[0]-d[1],R=(k,C)=>{let _=u+(h+C*k)*g;return Number.isFinite(_)&&Math.max(Math.abs(u),Math.abs(h*g))<=16*(Math.abs(M)+k*g)?_:M+C*k*g},T,A;if(o==="less")T=gt(h,p,!0,!1),A=[-1/0,R(wt(a,p,!0,!1),1)];else if(o==="greater")T=gt(h,p,!1,!1),A=[R(wt(a,p,!0,!1),-1),1/0];else{T=2*gt(-Math.abs(h),p,!0,!1);let k=1-a,C=k/2<1e-8?wt(k/2,p,!1,!1):wt(1-k/2,p,!0,!1);A=[R(C,-1),R(C,1)]}return A=[$(A[0],m),$(A[1],m)],d=typeof d=="number"?$(d,m):d.map(k=>$(k,m)),g=$(g,m),{method:N,alternative:o,statistic:h,statisticName:"t",parameter:p,parameterName:"df",pValue:T,confInt:A,confLevel:a,estimate:d,estimateNames:v,nullValue:s,nullValueName:b,stdErr:g,stdError:g,warnings:[]}}function dp(e,t,n){if(n.paired!==void 0)throw new Error("cannot use 'paired' in formula method");let r=Es(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 gp=new Set(["n","delta","sd","sigLevel","power","type","alternative","strict","tol"]),wp=["n","p1","p2","sigLevel","power","alternative","strict","tol"],bp=["groups","n","betweenVar","withinVar","sigLevel","power"],Xl=Number.EPSILON**.25;function ke(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 Re=e=>e==null;function fn(e,t,n){if(!Re(e)&&!t(e))throw new RangeError(n)}var Lo=e=>e>=0&&e<=1,Ls=e=>e>0&&e<1/0;function rn(e,t){if(e.filter(Re).length!==1)throw new RangeError(t)}function Ye(e,t,n,r={}){return Ol(e,t,n,r).root}var yr=(e,t,n,r)=>Bt(e,t,{ncp:n,lowerTail:r});function Ha(e,t){if(e!=null&&!(typeof e=="number"&&e>=0&&e<=1))throw new RangeError(t)}var Ya="'sig.level' must be numeric in [0, 1]";function vp(e={}){B(e,gp,"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;rn([t,n,o,r,s],"exactly one of 'n', 'delta', 'sd', 'power', and 'sig.level' must be NULL"),Ha(s,Ya),fn(r,Lo,"'power' must be a probability"),fn(o,Ls,"'sd' must be a positive finite number"),fn(t,g=>g>=2,"number of observations in each group must be at least 2"),fn(n,g=>!Number.isNaN(g),"'delta' must not be NaN"),fn(e.tol,Ls,"'tol' must be a positive finite number");let i=ke(e.type,["two.sample","one.sample","paired"]),c=ke(e.alternative,["two.sided","one.sided"]),a=e.strict??!1,l=e.tol??Xl,f=i==="two.sample"?2:1,m=c==="two.sided"?2:1;m===2&&!Re(n)&&(n=Math.abs(n));let u=(g,d,v,N)=>{let b=Math.max(1e-7,g-1)*f,E=Wt(N/m,b,{lowerTail:!1}),w=Math.sqrt(g/f)*d/v;Number.isFinite(w)||(w=Math.sqrt(g/f)*(d/v));let y=yr(E,b,w,!1);return a&&m===2?y+yr(-E,b,w,!0):y};if(Re(r))r=u(t,n,o,s);else{let g=r;if(Re(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)-g,2,1e7,{tol:l,extendInt:"upX"})}else if(Re(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(v=>u(t,n,v,s)-g,d*1e-7,d*1e7,{tol:l,extendInt:n>0?"downX":"upX"})}else Re(n)?n=Ye(d=>u(t,d,o,s)-g,o*1e-7,o*1e7,{tol:l,extendInt:"upX"}):s=Ye(d=>u(t,n,o,d)-g,1e-10,1-1e-10,{tol:l,extendInt:"yes"})}let h=i==="paired"?"n is number of *pairs*, sd is std.dev. of *differences* within pairs":i==="two.sample"?"n is number in *each* group":void 0,p={"two.sample":"Two-sample","one.sample":"One-sample",paired:"Paired"}[i]+" t test power calculation";return{n:t,delta:n,sd:o,sigLevel:s,power:r,alternative:c,...h===void 0?{}:{note:h},method:p,warnings:[]}}function Np(e={}){B(e,wp,"powerPropTest");let{n:t,p1:n,p2:r,power:o}=e,s=e.sigLevel===void 0?.05:e.sigLevel;rn([t,n,r,o,s],"exactly one of 'n', 'p1', 'p2', 'power', and 'sig.level' must be NULL"),Ha(s,Ya),fn(o,Lo,"'power' must be a probability"),fn(n,Lo,"'p1' must be a probability"),fn(r,Lo,"'p2' must be a probability"),fn(t,u=>u>0,"number of observations in each group must be positive"),fn(e.tol,Ls,"'tol' must be a positive finite number");let i=ke(e.alternative,["two.sided","one.sided"]),c=e.strict??!1,a=e.tol??Xl,l=i==="two.sided"?2:1,f=(u,h,p,g)=>{let d=ie(g/l,{lowerTail:!1});if(c&&l===2){let v=(h+p)/2,N=h*(1-h),b=p*(1-p),E=v*(1-v),w=Math.abs(h-p),y=Math.sqrt(N+b);return ae((Math.sqrt(u)*w-d*Math.sqrt(2*E))/y)+ae((Math.sqrt(u)*w+d*Math.sqrt(2*E))/y,{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(Re(o))o=f(t,n,r,s);else{let u=o;if(Re(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(Re(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(Re(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 J("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 Gl=1e16,Cs=(e,t)=>e>Gl&&e<1/0&&t>Gl;function Fs(e,t,n,r){let o=ie(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 yp(e,t,n,r){if(n===0)return 0;let o=l=>Math.floor(Math.log2(l)),[s,i,c,a]=[o(e),o(t),o(n),o(r)];return $($(e,-s)*$(t,-i)*$(n,-c)/$(r,-a),s+i+c-a)}function Mp(e={}){B(e,bp,"powerAnovaTest");let{groups:t,n,betweenVar:r,withinVar:o,power:s}=e,i=e.sigLevel===void 0?.05:e.sigLevel;if(rn([t,n,r,o,s,i],"exactly one of 'groups', 'n', 'between.var', 'within.var', 'power', and 'sig.level' must be NULL"),!Re(t)&&!(t>=2))throw new RangeError("number of groups must be at least 2");if(!Re(n)&&!(n>=2))throw new RangeError("number of observations in each group must be at least 2");Ha(i,Ya),fn(s,Lo,"'power' must be a probability"),fn(r,a=>a>=0&&a<1/0,"'betweenVar' must be a nonnegative finite number"),fn(o,Ls,"'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=yp(a-1,l,f,m));let p=a-1,g=(l-1)*a;return h===1/0?1:Cs(p,g)?Fs(p,g,h,u):Tn(Gn(u,p,g,{lowerTail:!1}),p,g,{ncp:h,lowerTail:!1})};if(Re(s))s=c(t,n,r,o,i);else{let a=s;if(Re(t))t=Ye(l=>c(l,n,r,o,i)-a,2,100);else if(Re(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(Re(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 Re(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 Ep=["correct","p","rescaleP"],xp=["confLevel"],Rp=["alternative","rowScores","colScores"];function Jt(e){let t=[0,0];for(let n of e)t=pe(t,[n,0]);return t}function Xa(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 Qa=e=>e.length>0&&typeof e[0]!="number",Za=(e,t)=>Number.isNaN(e)?NaN:bt(e,t/2,2,!1,!1);function Ql(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 Co(e,t){return e.length>0&&Array.isArray(e[0])?e.map((n,r)=>n.map((o,s)=>Ql(o,t[r][s]))):e.map((n,r)=>Ql(n,t[r]))}var Tp=["none","williams","yates"],Jl="Log likelihood ratio (G-test)";function Ap(e,t={}){B(t,Ep,"gTest");let n=ke(t.correct,Tp);if(typeof e!="object"||e===null||typeof e.length!="number")throw new TypeError("'x' must be an array of counts or of rows");if(Qa(e)){let o=Xa(e);return Zl(o.flat()),o.length===1?J("gTest",Ga(o[0],n,t)):o[0].length===1?J("gTest",Ga(o.map(s=>s[0]),n,t)):J("gTest",Sp(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 Zl(r),J("gTest",Ga(r,n,t))}function Zl(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 ef(e,t){return e<1/0?0:Math.ceil(Math.log2(t))+1}function tf(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=pe(n,ll(o,s))}return n[0]+n[1]}function Sp(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=Jt(e.flat()),s=ef(o[0],n*r),i=s===0?e:e.map(b=>b.map(E=>$(E,-s))),c=s===0?o:Jt(i.flat()),a=i.map(b=>Jt(b)),l=i[0].map((b,E)=>Jt(i.map(w=>w[E]))),f=a.map(b=>l.map(E=>Ce(b,ot(E,c)))),m=i;if(t==="yates"){let[[b,E],[w,y]]=i,x=b*y-E*w>0?-.5:.5,M=$(.5,-s),R=x<0?-M:M;m=[[b+R,E-R],[w-R,y+R]]}let u=tf(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 b=c[0]+c[1],E=a.reduce((y,x)=>y+1/(x[0]+x[1]),0),w=l.reduce((y,x)=>y+1/(x[0]+x[1]),0);p=1+$((b*E-1)*(b*w-1)/(6*b*(n-1)*(r-1)),-s)}let g=$(2*u,s)/p,d=(n-1)*(r-1),v=f.map(b=>b.map(E=>$(E[0]+E[1],s)));return{method:`${Jl} test of independence ${t==="none"?"without correction":t==="williams"?"with Williams' correction":"with Yates' correction"}`,statistic:g,statisticName:"G",parameter:d,parameterName:"X-squared df",pValue:Za(g,d),observed:s===0?m:m.map(b=>b.map(E=>$(E,s))),expected:v,ftResiduals:Co(e,v),warnings:h}}function Ga(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(b=>typeof b!="number"||Number.isNaN(b)))throw new RangeError("'p' must not contain missing values");if(s.some(b=>b<0))throw new RangeError("probabilities must be non-negative");if(s.some(b=>b===1/0))throw new RangeError("probabilities must be finite");let i=Jt(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=Jt(e),a=ef(c[0],r),l=a===0?e:e.map(b=>$(b,-a)),f=a===0?c:Jt(l),m=s.map(b=>o?ot(xt(f,b),i):xt(f,b)),u=m.map(b=>b[0]+b[1]),h=l;if(t==="yates"){let b=$(.5,-a),E=l[0]-u[0];E>$(.25,-a)?h=[l[0]-b,l[1]+b]:-E>$(.25,-a)&&(h=[l[0]+b,l[1]-b])}let p=tf(h,m);if(!o&&!Number.isNaN(p)){let b=f[0]+f[1];p-=b*(i[0]-1+i[1])}let g=Number.isNaN(p)?["NaNs produced"]:[],d=t==="williams"?1+(r+1)/(6*$(f[0]+f[1],a)):1,v=$(2*p,a)/d,N=u.map(b=>$(b,a));return{method:`${Jl} goodness of fit test`,statistic:v,statisticName:"G",parameter:r-1,parameterName:"X-squared df",pValue:Za(v,r-1),observed:a===0?h:h.map(b=>$(b,a)),expected:N,ftResiduals:Co(e,N),warnings:g}}function kp(e){if(typeof e!="object"||e===null||!Qa(e))throw new RangeError("'x' must be square with at least two rows and columns");let t=Xa(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 qp(e){let t=kp(e),n=Jt(t.flat())[0],r=n>2**400||n>0&&n<2**-400?Math.round(Math.log2(n)):0,o=r===0?t:t.map(w=>w.map(y=>$(y,-r))),s=o.length,i=o.map(w=>Jt(w)),c=o.map((w,y)=>Jt(o.map(x=>x[y]))),a=o.map((w,y)=>o[y].some((x,M)=>M!==y&&x>0)||o.some((x,M)=>M!==y&&x[y]>0)).flatMap((w,y)=>w?[y]:[]),l=a.length;if(l<2)throw new RangeError("Too many equal marginals, cannot compute");let f=a.map((w,y)=>y),m=w=>f[w]===w?w:f[w]=m(f[w]);for(let w=0;w<l;w++)for(let y=w+1;y<l;y++)o[a[w]][a[y]]+o[a[y]][a[w]]>0&&(f[m(w)]=m(y));if(a.some((w,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(w=>{let y=pe(i[w],[-c[w][0],-c[w][1]]);return y[0]+y[1]}),p=a.slice(0,u).map(w=>a.slice(0,u).map(y=>{if(w!==y)return-(o[w][y]+o[y][w]);let x=0;for(let M=0;M<s;M++)M!==w&&(x+=o[w][M]+o[M][w]);return x})),g=p.map(w=>w.map(()=>0)),d=[];for(let w=0;w<u;w++){let y=p[w][w];for(let x=0;x<w;x++)y-=g[w][x]*g[w][x]*d[x];d.push(y);for(let x=w+1;x<u;x++){let M=p[x][w];for(let R=0;R<w;R++)M-=g[x][R]*g[w][R]*d[R];g[x][w]=M/y}}let v=0,N=[];for(let w=0;w<u;w++){let y=h[w];for(let x=0;x<w;x++)y-=g[w][x]*N[x];N.push(y),v+=y*y/d[w]}let b=Jt(a.flatMap(w=>a.map(y=>t[w][y]))),E=l-1;return v=$(v,r),J("stuartMaxwellTest",{method:"Stuart-Maxwell test",statistic:v,statisticName:"chi-squared",parameter:E,parameterName:"df",pValue:Za(v,E),N:b[0]+b[1],warnings:[]})}function Ip(e,t={}){B(t,xp,"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=nf(e),o=Jt(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(w=>w.map(y=>$(y,-s))),{pc:c,pd:a}=_p(i),l=[0,0],f=[0,0];i.forEach((w,y)=>w.forEach((x,M)=>{l=pe(l,xt(c[y][M],x)),f=pe(f,xt(a[y][M],x))})),l=[l[0]/2,l[1]/2],f=[f[0]/2,f[1]/2];let m=pe(l,f),u=ot(De(l,f),m)[0],h=Ce(m,m),p=i.map((w,y)=>w.map((x,M)=>2*ot(De(Ce(f,c[y][M]),Ce(l,a[y][M])),h)[0])),g=Math.max(...p.flat().map(Math.abs)),d=g>0&&g<1/0?Math.floor(Math.log2(g)):0,v=0;i.forEach((w,y)=>w.forEach((x,M)=>{let R=$(p[y][M],-d);v+=x*R*R}));let N=$(Math.sqrt(v),d-s/2),b=(1-n)/2,E=b<1e-8?ie(b,{lowerTail:!1}):ie(1-b);return J("goodmanKruskalGamma",{estimate:u,confInt:[Math.max(-1,u-E*N),Math.min(1,u+E*N)],confLevel:n,ase:N,warnings:[]})}function nf(e){if(typeof e!="object"||e===null||!Qa(e))throw new TypeError("'table' must be an array of rows");let t=Xa(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 _p(e){let t=e.length,n=e[0].length,r=[0,0],o=e.map(p=>{let g=[],d=r;for(let v=0;v<n;v++)g.push(d),d=pe(d,[p[v],0]);return g}),s=e.map(p=>{let g=new Array(n),d=r;for(let v=n-1;v>=0;v--)g[v]=d,d=pe(d,[p[v],0]);return g}),i=p=>{let g=[],d=new Array(n).fill(r);for(let v=0;v<t;v++)g.push(d),d=d.map((N,b)=>pe(N,p[v][b]));return g},c=p=>{let g=new Array(t),d=new Array(n).fill(r);for(let v=t-1;v>=0;v--)g[v]=d,d=d.map((N,b)=>pe(N,p[v][b]));return g},a=i(o),l=c(s),f=i(s),m=c(o),u=a.map((p,g)=>p.map((d,v)=>pe(d,l[g][v]))),h=f.map((p,g)=>p.map((d,v)=>pe(d,m[g][v])));return{pc:u,pd:h}}function Lp(e,t={}){B(t,Rp,"lblTest");let n=Je(t.alternative),r=nf(e),o=r.length,s=r[0].length,i=(R,T,A)=>{if(R===void 0)return Array.from({length:T},(C,_)=>_+1);let k=Array.from(R);if(k.length!==T)throw new RangeError(`'${A}' must have one score for each ${A==="rowScores"?"row":"column"}`);if(k.some(C=>typeof C!="number"||!Number.isFinite(C)))throw new RangeError(`'${A}' must be finite numbers`);return k},c=i(t.rowScores,o,"rowScores"),a=i(t.colScores,s,"colScores"),f=Jt(r.flat())[0]>2**1e3?2*Math.ceil((Math.log2(Math.max(...r.flat()))-990)/2):0,m=f===0?r:r.map(R=>R.map(T=>$(T,-f))),u=m.map(R=>Jt(R)).map(R=>R[0]+R[1]),h=m[0].map((R,T)=>Jt(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 g=u.reduce((R,T,A)=>R+T*c[A],0)/p,d=h.reduce((R,T,A)=>R+T*a[A],0)/p,v=c.map(R=>R-g),N=a.map(R=>R-d),b=u.reduce((R,T,A)=>R+T*v[A]*v[A],0),E=h.reduce((R,T,A)=>R+T*N[A]*N[A],0),w=0;if(m.forEach((R,T)=>R.forEach((A,k)=>w+=A*v[T]*N[k])),!(b>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=w/Math.sqrt(b)/Math.sqrt(E),x=f===0?Math.sqrt(p-1)*y:$(Math.sqrt(p-$(1,-f))*y,f/2),M=n==="less"?ae(x):n==="greater"?ae(x,{lowerTail:!1}):2*ae(-Math.abs(x));return J("lblTest",{method:"Linear-by-Linear Association Test",alternative:n,statistic:x,statisticName:"Z",pValue:M,warnings:[]})}var rf=["correct","p","rescaleP","simulatePValue","B","nMinus1"];function wn(e){let t=[0,0],n=0;for(let r of e)t=pe(t,[r,0]),n+=r;return Number.isFinite(n)?t[0]:n}function Cp(e,t,n){return ct(t)&&B(t,rf,"chisqTest"),B(n,rf,"chisqTest"),Array.isArray(t)?J("chisqTest",Fp(e,t,n??{})):J("chisqTest",Op(e,t??{}))}var af="Chi-squared approximation may be incorrect";function Fp(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(An),c=[...new Set(s)].sort(An);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])]++),{...uf(f,n),rowLevels:i,colLevels:c}}function Op(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?Ja(r[0],t):o===1?Ja(r.map(s=>s[0]),t):uf(r,t)}return Ja(Array.from(e),t)}function cf(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 lf(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 ff(e,t){return Number.isNaN(e)?NaN:bt(e,t/2,2,!1,!1)}function mf(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 Ja(e,t){if(t.nMinus1??!1)throw new RangeError("'nMinus1' requires a contingency table with at least two rows and columns");let n=e.length;if(e.some(g=>!(g>=0&&g<1/0)))throw new RangeError("all entries of 'x' must be non-negative and finite");if(n<2)throw new TypeError("'x' must have at least 2 elements");let r=wn(e);if(!(r>0))throw new RangeError("at least one entry of 'x' must be positive");let o=t.p===void 0?e.map(()=>1/n):Array.from(t.p);if(o.length!==n)throw new TypeError("'x' and 'p' must have the same number of elements");if(o.some(Number.isNaN))throw new RangeError("p contains missing values");if(o.some(g=>g<0))throw new RangeError("probabilities must be non-negative");if(o.some(g=>g===1/0))throw new RangeError("probabilities must be finite");if(t.rescaleP??!1){let g=wn(o),d=g<1/0?0:Math.ceil(Math.log2(n)),v=d===0?o:o.map(b=>$(b,-d)),N=d===0?g:wn(v);o=v.map(b=>b/N)}else if(Math.abs(wn(o)-1)>Math.sqrt(Number.EPSILON))throw new RangeError("probabilities must sum to 1");let s=r<1/0?0:2*Math.ceil(Math.log2(n)/2),i=s===0?e:e.map(g=>$(g,-s)),c=s===0?r:wn(i),a=o.map(g=>c*g),l=s===0?a:a.map(g=>$(g,s));mf(l);let f=o.map((g,d)=>a[d]*(1-g)),m=$(wn(i.map((g,d)=>Vt(g-a[d],a[d]))),s),u="Chi-squared test for given probabilities",h=n-1,p;if(t.simulatePValue??!1){let g=cf(t);if(!Number.isInteger(r))throw new RangeError("the simulation needs counts summing to an integer");u+=To(g),h=NaN,p=lf(Dp(r,o,l,g),m,g)}else p=ff(m,h);return{method:u,statistic:m,statisticName:"X-squared",parameter:h,parameterName:"df",pValue:p,observed:e,expected:l,residuals:i.map((g,d)=>$((g-a[d])/Math.sqrt(a[d]),s/2)),stdres:i.map((g,d)=>$((g-a[d])/Math.sqrt(f[d]),s/2)),ftResiduals:Co(e,l),warnings:Number.isFinite(h)&&l.some(g=>g<5)?[af]:[]}}function Dp(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=Sl(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 Pp(e,t,n,r){let o=e.length,s=t.length,i=e.reduce((f,m)=>f+m,0);if(i>vs)throw new RangeError("the simulation needs a total count of at most 16777216 (2^24)");let c=Ns(i),a=new Int32Array(o*s),l=new Float64Array(Math.trunc(r));for(let f=0;f<l.length;f++){ys(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 uf(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(b=>!(b>=0&&b<1/0)))throw new RangeError("all entries of 'x' must be non-negative and finite");let c=wn(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(b=>wn(b)),l=e[0].map((b,E)=>wn(e.map(w=>w[E]))),f=a.map(b=>l.map(E=>gs(b,E,c)));mf(f.flat());let m=e.map((b,E)=>b.map((w,y)=>w-f[E][y])),u="Pearson's Chi-squared test",h,p=(o-1)*(s-1),g,d=(t.simulatePValue??!1)&&a.every(b=>b>0)&&l.every(b=>b>0),v=!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(w=>w.some(y=>!Number.isInteger(y))))throw new RangeError("the simulation needs integer counts");let b=cf(t);u+=To(b),h=wn(m.flatMap((w,y)=>w.map((x,M)=>Vt(x,f[y][M])))),p=NaN;let E=Pp(a.map(w=>Math.trunc(w)),l.map(w=>Math.trunc(w)),f,b);g=lf(E,h,b)}else if(v>0)u+=" with Yates' continuity correction",h=wn(m.flatMap((b,E)=>b.map((w,y)=>Vt(Math.abs(w)-v,f[E][y]))));else if(h=wn(m.flatMap((b,E)=>b.map((w,y)=>Vt(w,f[E][y])))),n){u="Pearson's 'N-1' Chi-squared test";let b=h*(c-1);h=Number.isFinite(b)?b/c:h*((c-1)/c)}return{method:u,statistic:h,statisticName:"X-squared",parameter:p,parameterName:"df",pValue:g??ff(h,p),observed:e,expected:f,residuals:m.map((b,E)=>b.map((w,y)=>w/Math.sqrt(f[E][y]))),stdres:Vp(e,c,a,l,f,m),ftResiduals:Co(e,f),warnings:Number.isFinite(p)&&f.some(b=>b.some(E=>E<5))?[...N,af]:N}}function Vp(e,t,n,r,o,s){if(!Number.isFinite(t))return s.map((p,g)=>p.map((d,v)=>d/Math.sqrt(o[g][v]*(1-n[g]/t)*(1-r[v]/t))));let i=e.map(p=>of(p)),c=e[0].map((p,g)=>of(e.map(d=>d[g]))),a=zp(i),l=sf(i),f=sf(c),m=l.map(p=>ot(p,a)),u=c.map(p=>ot(p,a)),h=f.map(p=>ot(p,a));return e.map((p,g)=>p.map((d,v)=>{let N=Ce(i[g],u[v]),b=De([d,0],N),E=b[0]+b[1],w=Ce(Ce(N,m[g]),h[v]),y=w[0]+w[1];if(y>=$p||E===0)return E/Math.sqrt(y);let x=N[0]+N[1],M=m[g][0]+m[g][1],R=h[v][0]+h[v][1];return E/Math.sqrt(x)/Math.sqrt(M)/Math.sqrt(R)}))}var $p=2**-1022;function of(e){let t=[0,0];for(let n of e)t=pe(t,[n,0]);return t}function zp(e){let t=[0,0];for(let n of e)t=pe(t,n);return t}function sf(e){let t=e.length,n=new Array(t+1);n[t]=[0,0];for(let s=t-1;s>=0;s--)n[s]=pe(e[s],n[s+1]);let r=[],o=[0,0];for(let s=0;s<t;s++)r.push(pe(o,n[s+1])),o=pe(o,e[s]);return r}var jp=Math.pow(Number.EPSILON,.25);function Ht(e,t,n,r={}){let o={tol:r.tol??jp,maxiter:r.maxIter??1e3};return r.fLower!==void 0&&(o.fLower=r.fLower),r.fUpper!==void 0&&(o.fUpper=r.fUpper),vr(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?Ht(n,r,s,{tol:2**-50,fLower:o,fUpper:i}):Ht(n,s,r,{tol:2**-50,fLower:i,fUpper:o});return Math.exp(c)}var Kp=["alternative","confInt","confLevel","or","simulatePValue","B","midp"];function Do(e,t={}){B(t,Kp,"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(ul));if(s.flat().some(f=>f>2147483647))throw new RangeError("'x' has entries too large to be integer");let i=[],c=Bp(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 J("fisherTest",{...Yp(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 J("fisherTest",{method:a+To(f),alternative:l,pValue:Wp(s,f),warnings:i})}return J("fisherTest",{method:a,alternative:l,pValue:Gp(s),warnings:i})}function Bp(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: ${kl(a)}`:null}function Wp(e,t){let n=e.filter(d=>d.some(v=>v>0)),r=e[0].map((d,v)=>v).filter(d=>e.some(v=>v[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(v=>d[v])),s=o.map(d=>d.reduce((v,N)=>v+N,0)),i=o[0].map((d,v)=>o.reduce((N,b)=>N+b[v],0)),c=s.length,a=i.length,l=[0,0];for(let d of o.flat())l=pe(l,[-ws(d),0]);let f=l[0]/(1+64*Number.EPSILON),m=s.reduce((d,v)=>d+v,0);if(m>vs)throw new RangeError("the simulation needs a total count of at most 16777216 (2^24)");let u=Ns(m),h=new Int32Array(c*a),p=0,g=Math.trunc(t);for(let d=0;d<g;d++){ys(s,i,u,h);let v=0;for(let N=0;N<h.length;N++)v-=u[h[N]];v<=f&&p++}return Math.max(0,Math.min(1,(1+p)/(g+1)))}var hf=1+1e-7,Up=16,Hp=2**24;function Fo(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=pe(r,[e[o],0]));return r[0]}var Gr=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%Up===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=Fo(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=pe(r,[(this.lo+o)*n[o],0]));return r[0]}tail(t,n,r){if(n===1)return r?an(t-1,this.m,this.n,this.k,!1,!1):an(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?Fo(o,s,o.length-1):Fo(o,0,s)}midTail(t,n,r){if(n===1){let i=.5*vn(t,this.m,this.n,this.k,!1);return(r?an(t,this.m,this.n,this.k,!1,!1):an(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?Fo(o,s+1,o.length-1):Fo(o,0,s-1))+.5*o[s]}};function ti(e,t,n){let r=e.density(n),o=r[t-e.lo]*hf,s=[0,0];for(let i of r)i<=o&&(s=pe(s,[i,0]));return s[0]}function pf(e,t,n,r){let o=Math.max(0,r-n),s=Math.min(r,t),i=g=>vn(g,t,n,r,!0),c=i(e)+Math.log(hf),a=Math.min(s,Math.max(o,Math.floor((r+1)*(t+1)/(t+n+2))));for(;a<s&&i(a+1)>i(a);)a++;for(;a>o&&i(a-1)>i(a);)a--;let l=(g,d)=>{let v=g,N=d+1;for(;N-v>1;){let b=v+Math.floor((N-v)/2);i(b)<=c?v=b:N=b}return v},f=(g,d)=>{let v=g-1,N=d+1;for(;N-v>1;){let b=v+Math.floor((N-v)/2);i(b)<=c?N=b:v=b}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:an(m,t,n,r,!0,!1),p=u>s?0:an(u-1,t,n,r,!1,!1);return h+p}function Oo(e,t){return rr(e,t)}function Yp(e,t,n){let r=Je(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>Hp)throw new RangeError(`the exact distribution of the 2 x 2 table has ${m} values, more than 2^24`);let u=new Gr(i+a,c+l,i+c),{lo:h,hi:p}=u,g=t.midp??!1,d;g?(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=ti(u,f,o);let v;if(f===h)v=0;else if(f===p)v=1/0;else{let y=u.mean(1);v=y===f?1:Oo(x=>u.mean(x)-f,y>f)}let N={method:n,alternative:r,pValue:d,estimate:v,estimateNames:"odds ratio",nullValue:o,nullValueName:"odds ratio",warnings:[]};if(!(t.confInt??!0))return N;let b=y=>{if(f===p)return 1/0;if(g){if(f===h&&y>=.5)return 0;let M=u.midTail(f,1,!1);return M===y?1:Oo(R=>u.midTail(f,R,!1)-y,M<y)}let x=u.tail(f,1,!1);return x===y?1:Oo(M=>u.tail(f,M,!1)-y,x<y)},E=y=>{if(f===h)return 0;if(g){if(f===p&&y>=.5)return 1/0;let M=u.midTail(f,1,!0);return M===y?1:Oo(R=>u.midTail(f,R,!0)-y,M>y)}let x=u.tail(f,1,!0);return x===y?1:Oo(M=>u.tail(f,M,!0)-y,x>y)},w;if(r==="less")w=[0,b(1-s)];else if(r==="greater")w=[E(1-s),1/0];else{let y=(1-s)/2;w=[E(y),b(y)]}return{...N,confInt:w,confLevel:s}}var Os=[[0,0]];function Sn(e){for(let t=Os.length;t<=e;t++)Os.push(pe(Os[t-1],il([t,0])));return Os[e]}function Ds(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 ei=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 Gp(e){let t=e.filter(b=>b.some(E=>E>0));if(t.length===0)throw new RangeError("all entries of the table are zero");let n=t[0].map((b,E)=>t.some(w=>w[E]>0));if(t=t.map(b=>b.filter((E,w)=>n[w])),t.length<2||t[0].length<2)return 1;t.length>t[0].length&&(t=t[0].map((b,E)=>t.map(w=>w[E])));let r=t.length,o=t.map(b=>b.reduce((E,w)=>E+w,0)),s=t[0].map((b,E)=>t.reduce((w,y)=>w+y[E],0)).sort((b,E)=>E-b),i=s.length,c=o.reduce((b,E)=>b+E,0),a=al(Sn(c));for(let b of o)a=pe(a,Sn(b));for(let b of s)a=pe(a,Sn(b));let l=[0,0];for(let b of t.flat())l=De(l,Sn(b));let f=pe(l,[Math.log1p(1e-7),0]),m=1e-9*(1+Math.abs(f[0])),[u,h]=f,p=Array.from({length:i},()=>new Map),g=(b,E)=>{let w=E.join(","),y=p[b].get(w);if(y!==void 0)return y;let x=pe(a,Sn(E.reduce((C,_)=>C+_,0)));for(let C of E)x=De(x,Sn(C));for(let C=b;C<i;C++)x=De(x,Sn(s[C]));let M={R:E,KZ:x,max:0,min:0,edges:[],pastHi:[],pastLo:[],count:[],index:new Map};if(p[b].set(w,M),b===i-1){let C=[0,0];for(let _ of E)C=De(C,Sn(_));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 A=new Array(r).fill(0),k=(C,_,I)=>{if(C===r-1){A[C]=_;let D=De(I,Sn(_)),S=E.map((W,Q)=>W-A[Q]).sort((W,Q)=>W-Q),O=g(b+1,S),q=`${S.join(",")}|${D[0]}`,V=R.get(q);V===void 0?R.set(q,{to:O,hi:D[0],lo:D[1],mult:1}):V.mult++;return}let F=Math.max(0,_-T[C+1]),L=Math.min(E[C],_);for(let D=F;D<=L;D++)A[C]=D,k(C+1,_-D,De(I,Sn(D)))};if(k(0,s[b],[0,0]),M.edges=[...R.values()],M.max=yt(M.edges.map(C=>C.hi+C.to.max)),M.min=cn(M.edges.map(C=>C.hi+C.to.min)),b===i-2){let C=M.edges.map(O=>{let q=O.to.edges[0];return{v:pe([O.hi,O.lo],[q.hi,q.lo]),mult:O.mult}});C.sort((O,q)=>O.v[0]-q.v[0]||O.v[1]-q.v[1]);let _=C.length,I=C[_-1].v[0],F=new Float64Array(_),L=new Float64Array(_),D=new Float64Array(_),S=[0,0];C.forEach(({v:O,mult:q},V)=>{F[V]=O[0],L[V]=O[1];let W=De(O,[I,0]);S=pe(S,[q*Math.exp(W[0])*(1+W[1]),0]),D[V]=S[0]}),M.dist={hi:F,lo:L,cum:D,vmax:I,kv:pe(a,[I,0])}}return M},d=g(0,[...o].sort((b,E)=>b-E)),v=new ei,N=(b,E,w,y)=>{if(E+b.max<=u-m){let[k,C]=Ds(b.KZ[0],b.KZ[1],E,w);return v.add(k,C,y),!0}if(E+b.min>u+m)return!0;let x=b.dist;if(x===void 0)return!1;let[M,R]=Ds(u,h,-E,-w),T=0,A=x.hi.length;for(;T<A;){let k=T+A>>>1,C=x.hi[k];C<M||C===M&&x.lo[k]<=R?T=k+1:A=k}if(T>0){let[k,C]=Ds(x.kv[0],x.kv[1],E,w);v.add(k,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 b=0;b<i-2;b++)for(let E of p[b].values()){let{pastHi:w,pastLo:y,count:x}=E;if(x.length!==0){for(let{to:M,hi:R,lo:T,mult:A}of E.edges)for(let k=0;k<x.length;k++){let[C,_]=Ds(w[k],y[k],R,T),I=x[k]*A;if(N(M,C,_,I))continue;let F=M.index.get(C);F===void 0?(M.index.set(C,M.count.length),M.pastHi.push(C),M.pastLo.push(_),M.count.push(I)):M.count[F]+=I}E.pastHi=E.pastLo=E.count=[],E.index.clear()}}return Math.min(1,v.value())}function Ps(e){let t=e.every(n=>Number.isInteger(n))?1:2;return[t,e.map(n=>n*t).sort((n,r)=>n-r)]}function ri(e){return[...e].sort((t,n)=>t-n).join(",")}function ni(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 oi(e){let[,t]=Ps(e),n=0,r=0;for(let o of t)r+=n+=o;return{steps:r,size:n+1}}function si(e){let[t,n]=Ps(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 ni(t,0,o)}function ai(e,t){let[,n]=Ps(e),r=n.length,o=Math.min(t,r-t),s=df(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 df(e){let t=[0];for(let n of e)t.push(t[t.length-1]+n);return t}function ii(e,t){let[n,r]=Ps(e),o=r.length,s=Math.min(t,o-t),i=df(r),c=[];for(let h=0;h<=s;h++)c.push(new Float64Array(i[o]-i[o-h]-i[h]+1));c[0][0]=1;for(let h=1;h<=o;h++){let p=r[h-1];for(let g=Math.min(h,s);g>=Math.max(1,s-(o-h));g--){let d=c[g],v=c[g-1],N=g/h,b=(h-g)/h,E=i[g],w=i[h]-i[h-g],y=i[g-1]+p,x=Math.max(E,y);for(let M=w;M>=x;M--)d[M-E]=b*d[M-E]+N*v[M-y];for(let M=x-1;M>=E;M--)d[M-E]=b*d[M-E]}}let a=c[s],l=t*(t+1)/2;if(s===t)return ni(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 ni(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 Mr(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 gf=["alternative","mu","paired","exact","correct","confInt","confLevel","tolRoot"];function Po(e,t,n={}){if(typeof e!="string"&&ct(t)&&B(t,gf,"wilcoxTest"),B(n,gf,"wilcoxTest"),typeof e=="string"){if(n.paired!==void 0)throw new Error("cannot use 'paired' in formula method");let g=Es(e,t,!0);return g.kind==="one"?Po(g.x,n):Po(g.x,g.y,g.kind==="paired"?{...n,paired:!0}:n)}let r;t!==void 0&&!ArrayBuffer.isView(t)&&!Array.isArray(t)?n=t:r=t;let o=Je(n.alternative),s=n.mu??0,i=n.paired??!1,c=n.exact??null,a=Qp(n.correct??!0),l=n.confInt??!1,f=n.confLevel??.95,m=n.tolRoot??1e-4;if(!Number.isFinite(s))throw new RangeError("mu must be a single finite number");if(l&&!(f>0&&f<1))throw new RangeError("confLevel must be a number strictly between 0 and 1");if(i&&r===void 0)throw new TypeError("y is required when paired is true");let u,h;if(i){let[g,d]=Jn(Array.from(e),Array.from(r));u=Dt(g.map((v,N)=>v-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 At};return J("wilcoxTest",h===void 0?nd(u,c,i,p):fd(u,h,c,p))}function Qp(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 Vs(e,t,n){return n?t==="two.sided"?Math.sign(e)*.5:t==="greater"?.5:-.5:0}var vf=10*Number.EPSILON;function Nf(e,t,n=0){let r=a=>Math.min(1,Math.max(a,0)),o=()=>n===0?ae(e):r(ae(e)-n*Zt(e)),s=()=>n===0?ae(e,{lowerTail:!1}):r(ae(e,{lowerTail:!1})+n*Zt(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 Zp(e){let t=e*e;return[e*(t-3),e*(t*(t-10)+15),e*(t*(t*(t-21)+105)-105)]}function Jp(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 yf(e,o,s,n)}function ed(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 yf(e,i,c,r)}function yf(e,t,n,r){let[o,s,i]=Zp(e),c=t*o;return r>=2&&(c+=n*s),r>=3&&(c+=t*t/2*i),c}function fi(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 mi=(e,t)=>e-t,ci=5e9,td=40;function li(e){return new RangeError(`wilcoxTest: the exact conditional ${e} would take too long or too much memory: use exact: false`)}function $s(e,t){if(e.size>16777216||e.steps>ci)throw li(t);return e.steps}function Mf(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 nd(e,t,n,r){let{alternative:o,mu:s}=r,i=e.map(w=>w-s);i.some((w,y)=>!Number.isFinite(w)&&Number.isFinite(e[y]))&&(i=e.map(w=>w/2-s/2));let a=i.some(w=>w===0),l=t??i.length<50,f=(w,y,x)=>({method:w,alternative:o,statistic:y,statisticName:"V",pValue:x,nullValue:s,nullValueName:n?"location shift":"location",warnings:r.warnings.list});if(l){let w=vt(i.map(Math.abs)),y=bn(w).length>0,x=0;for(let C=0;C<i.length;C++)i[C]>0&&(x+=w[C]);let M="Wilcoxon signed rank exact test";if(!y&&!a){let C=i.length,_=C*(C+1)/2,I;if(o==="less")I=Br(x,C,!0,!1);else if(o==="greater")I=Br(x-1,C,!1,!1);else{let L=x>_/2?Br(x-1,C,!1,!1):Br(x,C,!0,!1);I=Math.min(1,2*L)}let F=f(M,x,I);return r.confInt?{...F,...rd(e,r)}:F}let R=w.filter((C,_)=>i[_]!==0);$s(oi(R),"distribution");let T=si(R),A;if(o==="less")A=or(T,x);else if(o==="greater")A=Mr(T,x-.25);else{let C=0;for(let _ of R)C+=_/2;A=Math.min(1,2*(x>C?Mr(T,x-.25):or(T,x)))}let k=f(M,x,A);return r.confInt?{...k,...sd(e,r)}:k}let m=a?i.filter(w=>w!==0):i,u=m.length;if(u===0)throw new RangeError("no non-zero differences for the signed rank test");let[h,p,g]=Mf(m),v=bn(vt(m.map(Math.abs))).length>0||a?Math.min(r.correct,0):r.correct,N=(p-Vs(p,o,v>=0))/g,b=Nf(N,o,v>=1?Jp(N,u,v):0),E=f(`Wilcoxon signed rank test${r.correct>=0?" with continuity correction":""}`,h,b);return r.confInt?{...E,...ad(e,r)}:E}function ui(e,t,n){return e==="less"?[-1/0,n]:e==="greater"?[t,1/0]:[t,n]}function wf(e,t,n){let r={confInt:ui(e,NaN,NaN),confLevel:0,estimate:t};return n!==void 0&&(r.estimateNames=n),r}function Ef(e){if(!(e>0&&e<1/0))throw new RangeError("tolRoot must be a positive finite number")}function xf(e){let t=e.length,n=[];for(let r=0;r<t;r++)for(let o=r;o<t;o++){let s=gn(e[r],e[o]);Number.isNaN(s)||n.push(s)}return n.sort(mi)}function Rf(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+vf&&l++;let f=fi(e);if(l===0)return{confInt:[-1/0,1/0],confLevel:1,estimate:f,estimateNames:o};let m=t(l-1);return{confInt:ui(s,e[l-1],e[c-l]),confLevel:1-(s==="two.sided"?2*m:m),estimate:f,estimateNames:o}}function rd(e,t){let n=e.length;return Rf(xf(e),r=>Br(r,n,!0,!1),r=>vl(r,n,!0,!1),t,"(pseudo)median")}function Tf(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 Af(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)+vf,g=f!=="less",d=f!=="greater",v=I=>gn(e[I-1],e[I]),N=Math.min(u-1,Math.max(1,Math.ceil(i.mean+ie(Math.min(p,1))*i.sd+.1*i.sd+3))),b=8,E=I=>I===0?e[0]-1:v(I),w=I=>I===0?e[u-1]+1:v(u-I),y=[new Map,new Map];{let I=new Map,F=0,L=td*c;for(let[D,S]of[[0,E],[1,w]])if(!(D===0?!g:!d))for(let O=0;O<=N;O++){if(F+=L,F>ci)throw li("interval");let q;try{q=t(S(O))}catch{break}if(y[D].set(O,[q.stat,q.key]),I.has(q.key)){I.delete(q.key),I.set(q.key,!0);continue}if(F+=r(q.scores),F>ci)throw li("interval");I.set(q.key,!0),I.size>b&&I.delete(I.keys().next().value)}}let x=new Map,M=(I,F)=>{let L=I===0?E(F):w(F),D=y[I].get(F),S;if(D===void 0){let W=t(L);D=[W.stat,W.key],S=W.scores}let[O,q]=D,V=x.get(q);return V!==void 0?x.delete(q):(V=n(S??t(L).scores),x.size>=b&&x.delete(x.keys().next().value)),x.set(q,V),[O,V]},R=I=>{let[F,L]=M(0,I);return o(L,F)},T=I=>{let[F,L]=M(1,I);return s(L,F)},A=-1/0,k=0;if(g){let I=R(0);if(!(I>p)){let F=I,L=1;for(;L<=u-1;L++){let D=R(L);if(D>p)break;F=D}A=e[L-1],k=F}}let C=1/0,_=0;if(d){let I=T(0);if(!(I>p)){let F=I,L=u-1;for(;L>=1;L--){let D=T(u-L);if(D>p)break;F=D}C=e[L],_=F}}return{confInt:[A,C],confLevel:1-(k+_),estimate:fi(e),estimateNames:l}}function od(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 sd(e,t){let n=e.length,r=s=>{let i=od(e,s),{ranks:c,key:a}=Tf(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 Af(xf(e),r,si,s=>$s(oi(s),"interval"),(s,i)=>Mr(s,i-.25),or,{mean:o/2,sd:Math.sqrt(n*(n+1)*(2*n+1)/24)},n,t,"(pseudo)median")}function ad(e,t){let{alternative:n,confLevel:r,tolRoot:o}=t,s=t.correct>=0,i=e.length;if(i===0)return wf(n,NaN);let c=cn(e),a=yt(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"),wf(n,gn(c,a),"midrange");let l=(b,E)=>{let[,w,y]=Mf(e.map(x=>x-b).filter(x=>x!==0));return(w-Vs(w,n,E))/y},f=l(c,s),m=l(a,s),u=b=>-ie(b),h=b=>n==="greater"?!(f<u(b)):n==="less"?!(m>ie(b/2)):!(f<u(b/2))&&!(m>ie(b/2)),p=1-r,g=!1;for(;!h(p)&&p<1;)p*=2,g=!0;g&&t.warnings.add("requested conf.level not achievable"),Ef(o);let d=b=>Ht(E=>l(E,s)-b,c,a,{tol:o,fLower:f-b,fUpper:m-b}),v;if(p>=1){let b=fi([...e].sort(mi));v=ui(n,b,b)}else n==="greater"?v=[d(u(p)),1/0]:n==="less"?v=[-1/0,d(ie(p))]:v=[d(u(p/2)),d(ie(p/2))];let N=Ht(b=>l(b,!1),c,a,{tol:o,fLower:l(c,!1),fUpper:l(a,!1)});return{confInt:v,confLevel:g?1-Math.min(1,p):r,estimate:N,estimateNames:"(pseudo)median"}}function Sf(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 id(e,t){let[n,r,o]=Sf(vt([...e,...t]),e.length);return[n,r,o]}function cd(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 ld(e,t,n){return kf(e,t,n).ranks}function kf(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:ri(i)}}if(Number.isFinite(n)&&r.some((o,s)=>!Number.isFinite(o)&&Number.isFinite(e[s]))){let o=cd(e.map(s=>s/2-n/2),t);return{ranks:o,key:ri(o)}}return Tf([...r,...t])}function fd(e,t,n,r){let{alternative:o,mu:s}=r,i=e.length,c=t.length,a=n??(i<50&&c<50),l=ld(e,t,s),[f,m,u,h]=Sf(l,i),p,g;if(a)if(g="Wilcoxon rank sum exact test",!h)p=bf(()=>o==="less"?Wr(f,i,c,!0,!1):o==="greater"?Wr(f-1,i,c,!1,!1):Math.min(2*Wr(f,i,c,!0,!1),2*Wr(f-1,i,c,!1,!1),1));else{$s(ai(l,i),"distribution");let N=ii(l,i);o==="less"?p=or(N,f):o==="greater"?p=Mr(N,f-.25):p=Math.min(2*or(N,f),2*Mr(N,f-.25),1)}else{g=`Wilcoxon rank sum test${r.correct>=0?" with continuity correction":""}`;let N=h?Math.min(r.correct,0):r.correct,b=l.every(w=>w===l[0]),E=(m-Vs(m,o,N>=0))/u;p=b?1:Nf(E,o,N>=1?ed(E,i,c,N):0)}let d={method:g,alternative:o,statistic:f,statisticName:"W",pValue:p,nullValue:s,nullValueName:"location shift",warnings:r.warnings.list};if(!r.confInt)return d;let v=a?h?ud(e,t,r):bf(()=>md(e,t,r)):hd(e,t,r);return{...d,...v}}function bf(e){try{return e()}catch(t){throw t instanceof RangeError&&t.message.startsWith("wilcox:")?new RangeError(`${t.message}: use exact: false`):t}}function qf(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(mi)}function md(e,t,n){let r=e.length,o=t.length;return Rf(qf(e,t,!1),s=>Wr(s,r,o,!0,!1),s=>yl(s,r,o,!0,!1),n,"difference in location")}function ud(e,t,n){let r=e.length,o=t.length,s=i=>{let{ranks:c,key:a}=kf(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 Af(qf(e,t,!0),s,i=>ii(i,r),i=>$s(ai(i,r),"interval"),(i,c)=>Mr(i,c-.25),or,{mean:r*o/2,sd:Math.sqrt(r*o*(r+o+1)/12)},r+o,n,"difference in location")}function hd(e,t,n){let{alternative:r,confLevel:o,tolRoot:s}=n,i=n.correct>=0,c=cn(e)-yt(t),a=yt(e)-cn(t);if(!Number.isFinite(c)||!Number.isFinite(a))throw new Error("cannot compute the confidence interval because the differences are not finite");let l=(h,p)=>{let[,g,d]=id(e.map(v=>v-h),t);return(g-Vs(g,r,p))/d},f=(h,p)=>{let g=l(c,p)-h,d=l(a,p)-h;if(Number.isNaN(g)||Number.isNaN(d))throw new Error("cannot compute the confidence interval because all observations are tied");return g<=0?c:d>=0?a:(Ef(s),Ht(v=>l(v,p)-h,c,a,{tol:s,fLower:g,fUpper:d}))},m=1-o,u;return r==="greater"?u=[f(-ie(m),i),1/0]:r==="less"?u=[-1/0,f(ie(m),i)]:u=[f(-ie(m/2),i),f(ie(m/2),i)],{confInt:u,confLevel:o,estimate:f(0,!1),estimateNames:"difference in location"}}var pd=["method","n"],If=["holm","hochberg","hommel","bonferroni","BH","BY","fdr","none"];function dd(e){if(If.includes(e))return e;let t=If.filter(n=>n.startsWith(e));if(e.length===0||t.length!==1)throw new RangeError(`invalid method "${e}"`);return t[0]}function Xr(e,t={}){B(t,pd,"pAdjust");let n=typeof t=="string"?{method:t}:t,r=dd(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=Lf(a,r,c),f=o.slice();return s.forEach((m,u)=>f[m]=l[u]),f}function zs(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 Lf(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=zs(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=zs(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++)dn(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=zs(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?Lf(e,"hochberg",n):gd(e,n)}}var _f=2**16;function gd(e,t){if(!Number.isInteger(t))throw new RangeError("n must be an integer for the hommel method");if(t>_f)throw new RangeError(`n must be at most ${_f} for the hommel method (O(n^2) work)`);let n=e.length,r=[...e,...new Array(t-n).fill(1)],o=zs(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 Cf=["p","alternative","confLevel"],wd=1+1e-7;function hi(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)+pn(e,t,n,!1)}function bd(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=pn(e,t,n,!1)*wd,s=a=>pn(a,t,n,!1)<=o;if(e<r){let a=fs(s,Math.ceil(r),t),l=a>t?0:hi(a,t,n);return Math.min(1,Dn(e,t,n,!0,!1)+l)}let i=ms(s,0,Math.floor(r)),c=i<0?0:Dn(i,t,n,!0,!1);return Math.min(1,hi(e,t,n)+c)}function vd(e,t,n={}){B(t,Cf,"binomTest"),B(n,Cf,"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 pi(r,o,n)}function pi(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=Je(n.alternative),s=n.confLevel??.95;_t(s);let i;o==="less"?i=Dn(e,t,r,!0,!1):o==="greater"?i=hi(e,t,r):i=bd(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 Nd=["T","r","alternative","confLevel"],yd=1+1e-7;function Md(e,t){if(t===0)return e===0?1:0;if(e===t)return 1;let n=us(e,t,!1)*yd,r=i=>us(i,t,!1)<=n,o=Math.floor(t);if(e<t){let i=fs(r,Math.max(e+1,o),1/0);return Math.min(1,Kr(e,t,!0,!1)+di(i,t))}let s=ms(r,0,Math.min(e-1,o));return Math.min(1,Kr(s,t,!0,!1)+di(e,t))}var Vo=2**-1022;function di(e,t){return e-(e-1)===1?Kr(e-1,t,!1,!1):Kr(e,t,!1,!1)+us(e,t,!1)}function Er(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 Ed(e,t,n,r){let o=n+t*r,s=e*t*n/o;if(Number.isFinite(s)&&Number.isFinite(o)&&o>=Vo&&(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]=Er(e),[f,m]=Er(t),[u,h]=Er(n),[p,g]=Er(r),d=f*p,v=h-m-g,N=v>=0?u/(u+$(d,-v)):u/($(u,v)+d);return $(a*f*N,l+m+Math.min(v,0))}function xd(e,t,n,r){let o=e/n,s=t/r,i=o/s;return t===0||Number.isFinite(o)&&Number.isFinite(s)&&s>=Vo&&(o===0||o>=Vo)?i:e===0?0:e/t*(r/n)}function Rd(e,t={}){B(t,Nd,"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(v=>Number.isFinite(v)&&v>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=Je(t.alternative),l=t.confLevel??.95;if(o===2?_t(l):ln(l),o===2){let[v,N]=r,[b,E]=i,w=v+N;if(w===1/0)throw new RangeError("'x' must be finite, nonnegative, and integer");let y=c*b,x=y+E,M=Number.isFinite(x)&&x>=Vo&&(y===0||y>=Vo)?y/x:1/(1+E/b/c),R=pi(Math.round(v),Math.round(v)+Math.round(N),{p:M,alternative:a,confLevel:l}),T=E/b,A=k=>{if(k===0)return 0;let C=k/(1-k);if(Number.isFinite(T)||C===1/0)return C*T;let[_,I]=Er(C),[F,L]=Er(b),[D,S]=Er(E);return $(_*D/F,I+S-L)};return{method:"Comparison of Poisson rates",alternative:a,statistic:v,statisticName:"count1",parameter:Ed(w,c,b,E),parameterName:"expected count1",pValue:R.pValue,confInt:[A(R.confInt[0]),A(R.confInt[1])],confLevel:l,estimate:xd(v,N,b,E),estimateNames:"rate ratio",nullValue:c,nullValueName:"rate ratio",warnings:[]}}let f=r[0],m=i[0],u=c*m,h;a==="less"?h=Kr(f,u,!0,!1):a==="greater"?h=di(f,u):h=Md(f,u);let p=v=>f===0?0:ls(v,f,1,!0,!1),g=v=>v<1e-8?ls(v,f+1,1,!1,!1):ls(1-v,f+1,1,!0,!1),d;if(a==="less")d=[0,g(1-l)];else if(a==="greater")d=[p(1-l),1/0];else{let v=(1-l)/2;d=[p(v),g(v)]}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 Ff=["p","alternative","confLevel","correct"],Td="Chi-squared approximation may be incorrect";function gi(e,t,n={}){ct(t)&&B(t,Ff,"propTest"),B(n,Ff,"propTest");let r,o,s=n;if(typeof e!="number"&&e.length>0&&Array.isArray(e[0])){let A=e;if(A.some(k=>k.length!==2))throw new RangeError("'x' must have 2 columns");r=A.map(k=>k[0]),o=A.map(k=>k[0]+k[1]),t!==void 0&&typeof t=="object"&&!Array.isArray(t)&&(s=t)}else{if(t===void 0||typeof t=="object"&&!Array.isArray(t))throw new RangeError("'n' must be given");r=typeof e=="number"?[e]:Array.from(e),o=typeof t=="number"?[t]:Array.from(t)}if(r.length!==o.length)throw new RangeError("'x' and 'n' must have the same length");let i=r.length,c=[];for(let 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,k)=>A>o[k]))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(k=>A[k]),l.some(k=>!(k>0&&k<1)))throw new RangeError("elements of 'p' must be in (0,1)")}let f=l!==void 0;a===1&&l===void 0&&(l=[.5]);let m=s.confLevel??.95;_t(m);let u=Je(s.alternative);(a>2||a===2&&f)&&(u="two.sided");let h=r.map((A,k)=>A/o[k]),p=a===2?h[0]-h[1]:0,g=a===2?1/o[0]+1/o[1]:0,d=(s.correct??!0)&&a<=2?.5:0,v=!1;if(d>0&&a===1){let A=Math.abs(r[0]-o[0]*l[0]);A<=d&&([d,v]=[A,!0])}else if(d>0&&a===2&&!f){let A=Math.abs(p)/g;A<=d&&([d,v]=[A,!0])}let N=Ad(r,o),b=o.some((A,k)=>{let C=l?.[k]??N;return A*C<5||A*(1-C)<5}),E=0;if(!v)for(let A=0;A<a;A++){let k=l?.[A]??N,C=o[A]*k,_=o[A]*(1-k);E+=Vt(Math.abs(r[A]-C)-d,C),E+=Vt(Math.abs(o[A]-r[A]-_)-d,_)}let w=l!==void 0?a:a-1,y;if(u==="two.sided")y=bt(E,w/2,2,!1,!1);else{let A=Math.sign(a===1?h[0]-l[0]:p);y=ae(A*Math.sqrt(E),{lowerTail:u==="less"})}let x;if(a===1||a===2&&!f){let A=(1-m)/2,k=u==="two.sided"&&A<1e-8?ie(A,{lowerTail:!1}):ie(u==="two.sided"?(1+m)/2:m);if(a===1){let C=o[0],_=k*k,I=(O,q)=>(O+_/(2*C)+q*k*Math.sqrt(O*(1-O)/C+_/(4*C*C)))/(1+_/C),F=h[0]+d/C,L=h[0]-d/C,D=Math.min(1,Math.max(0,F>=1?1:I(F,1))),S=Math.min(1,Math.max(0,L<=0?0:I(L,-1)));x=u==="two.sided"?[S,D]:u==="greater"?[S,1]:[0,D]}else{let C=Math.sqrt(h.reduce((L,D,S)=>L+D*(1-D)/o[S],0)),_=k*C+d*g,I=Math.max(-1,p-_),F=Math.min(1,p+_);x=u==="two.sided"?[I,F]:u==="greater"?[I,1]:[-1,F]}}let M=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:E,statisticName:"X-squared",parameter:w,parameterName:"df",pValue:y,estimate:a===1?h[0]:h,estimateNames:M,warnings:b?[Td]:[]};return l!==void 0&&(T.nullValue=a===1?l[0]:l,T.nullValueName=M),x!==void 0&&(T.confInt=x,T.confLevel=m),J("propTest",T)}function Ad(e,t){let n=t.reduce((o,s)=>o+s,0);if(n<1/0)return e.reduce((o,s)=>o+s,0)/n;let r=Math.ceil(Math.log2(t.length));return e.reduce((o,s)=>o+$(s,-r),0)/t.reduce((o,s)=>o+$(s,-r),0)}var Sd=["method","confLevel","sides"],kd=["wilson","wald","wilsoncc","agresti-coull","jeffreys","clopper-pearson","midp"],qd=["two.sided","left","right"];function $o(e,t){let n=ke(t,qd),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{...wi(s,o),sides:n}}function wi(e,t=1-e){let n=e/2<1e-8,r=n?ie(e/2,{lowerTail:!1}):ie(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 Qr(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 Bn(e,t,n,r){let o=Qr(e,n),s=Qr(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 Ks(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 js(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 Of(e,t,n,r){let o=n/2,s=r?f=>{let m=Math.exp(f);return .5*pn(e,t,m,!1)+Dn(e-1,t,m,!0,!1)-o}:f=>{let m=Math.exp(f);return .5*pn(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:Ht(g=>i*s(g),h,f,{tol:2**-60,fLower:p,fUpper:m}));f=h,m=p,u*=2}}let l=i*s(0);return l<=0?1:Math.exp(Ht(f=>i*s(f),c,0,{tol:2**-60,fLower:a,fUpper:l}))}function bi(e,t,n={}){B(n,Sd,"binomCI");let r=ke(n.method,kd),o=$o(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]=Ks(s,i,a);break;case"wilsoncc":{let h=a*a,p=2*(i+h),g=2*s+h-1,d=a*Math.sqrt(h-2-1/i+4*l*(i*f+1));m=l===0?0:g-d<g/2?(2*s-1)/(2*i)*(2*s-1)/(g+d):(g-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,g=(s+h/2)/p,d=a*Math.sqrt(g*(1-g)/p);[m,u]=[g-d,g+d];break}case"jeffreys":m=s===0?0:js(c,s+.5,i-s+.5,!1),u=s===i?1:js(c,s+.5,i-s+.5,!0);break;case"clopper-pearson":m=s===0?0:js(c,s,i-s+1,!1),u=s===i?1:js(c,s+1,i-s,!0);break;case"midp":m=s===0?0:s===i?Math.exp(Math.log(c)/i):Of(s,i,c,!1),u=s===i?1:s===0?-Math.expm1(Math.log(c)/i):Of(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),J("binomCI",{estimate:l,confInt:[m,u],confLevel:n.confLevel??.95,method:r,sides:o.sides,warnings:[]})}function zo(e){return e===1/0?1:e/(1+e)}function jo(e,t){return e===0?0:t>0?e*e/t:1/0}function Id(e,t,n,r){return e/t+n/r>1}function _d(e,t,n,r){return Ws(e,t,n,r,0)}function yi(e,t,n,r,o){if(Id(e,t,n,r)){let[p,g,d,v]=yi(t-e,t,r-n,r,-o);return[g,p,v,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=v=>{let N=0;return i>0&&(N+=i/v),i<c&&(N-=(c-i)/(1-v)),a>0&&(N+=a/(v+f)),a<l&&(N-=(l-a)/(m-v)),N},g=v=>{let N=0;return i>0&&(N-=i/(v*v)),i<c&&(N-=(c-i)/((1-v)*(1-v))),a>0&&(N-=a/((v+f)*(v+f))),a<l&&(N-=(l-a)/((m-v)*(m-v))),N};if(p(0)<=0)u=0;else if(p(m)>=0)u=m;else{let v=0,N=m,b=Ld(e,t,n,r,o);u=s?b:b-o,u>v&&u<N||(u=N/2);for(let E=0;E<2e3;E++){let w=p(u);if(w===0)break;w>0?v=u:N=u;let y=u-w/g(u);if(y>v&&y<N||(y=v===0?N/2:N>2*v?Math.sqrt(v*N):v+(N-v)/2),!(y>v&&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 Ld(e,t,n,r,o){let s=e/t,i=n/r,c=r/t,a=1+c,l=-(1+c+s+c*i+o*(c+2)),f=o*o+o*(2*s+c+1)+s+c*i,m=-s*o*(1+o),u=l/(3*a),h=u*u*u-l*f/(6*a*a)+m/(2*a),p=Math.sign(h)*Math.sqrt(Math.max(0,u*u-f/(3*a)));if(p===0)return-u;let g=(Math.PI+Math.acos(Math.max(-1,Math.min(1,h/(p*p*p)))))/3;return 2*p*Math.cos(g)-u}function Mi(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]=yi(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 Cd(e,t,n,r,o,s,i){let c=De(De(ft([e,0],t),ft([n,0],r)),[o,0]),[a,l,f,m]=yi(e,t,n,r,o),u=pe(ft(jt(a,l),t),ft(jt(f,m),r));return s==="mn"&&(u=ft(xt(u,t+r),t+r-1)),De(Ce(c,c),xt(u,i))[0]}function Ws(e,t,n,r,o){return De(De(ft([e,0],t),ft([n,0],r)),[o,0])[0]}function Fd(e,t,n,r,o,s){return jo(Ws(e,t,n,r,o),Mi(e,t,n,r,o,s))}var Bs=new Float64Array(1),vi=new BigInt64Array(Bs.buffer);function Df(e){return Bs[0]=Math.abs(e),e<0?-vi[0]:vi[0]}function sr(e){return vi[0]=e<0n?-e:e,e<0n?-Bs[0]:Bs[0]}function Ni(e,t,n,r,o){let s=e(t);if(s===0||Number.isNaN(s))return t;let i=s<0===o,c=Df(i?r:n),a=Df(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 Vf(e,t,n,r,o,s){let i=_d(e,t,n,r);if(o===0)return[i,i];let c=zo(o),a=p=>(p===i?0:zo(Fd(e,t,n,r,p,s)))-c,l=(p,g,d,v)=>{if(d===0)return p;if(v===0)return g;let N=Ht(a,p,g,{tol:1e-300,fLower:d,fUpper:v});return s==="wald"?N:Ni(b=>Cd(e,t,n,r,b,s,o),N,p,g,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 Pf(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?Ht(e,r,i,{tol:2**-60,fLower:o,fUpper:c}):Ht(e,i,r,{tol:2**-60,fLower:c,fUpper:o});return Math.exp(a)}r=i,o=c,s*=2}}function Od(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 Dd(e,t,n,r,o){let[s,i]=Od(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=xt(x,o);return[M[0],De([1,0],M)[0],x[0],De([1,0],x)[0]]},p=e+n,g=c<2?1:-1,d=y=>{let[,x,M,R]=h(y),T=0,A=0;return p>0&&(T+=M>0?p/M:1/0,A-=p/(M*M)),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)),[g*T,A*f]},v;if(!(m>u))v=u;else if(!(d(u)[0]>0))v=u;else if(!(d(m)[0]<0))v=m;else{let y=u,x=m;v=a,v>y&&v<x||(v=y+(x-y)/2);for(let M=0;M<2e3;M++){let[R,T]=d(v);if(R===0)break;R>0?y=v:x=v;let A=v-R/T;if(A>y&&A<x||(A=y<=0?x/2:x>2*y?Math.sqrt(y*x):y+(x-y)/2),!(A>y&&A<x))break;let k=Math.abs(A-v);if(v=A,k<=2**-53*v)break}}let[N,b,E,w]=h(v);return[Math.min(1,Math.max(0,N)),Math.min(1,Math.max(0,b)),Math.min(1,Math.max(0,E)),Math.min(1,Math.max(0,w))]}function Pd(e,t,n,r,o){let[s,i,c,a]=Dd(e,t,n,r,o),l=(f,m,u,h)=>u>.5?m*h-(m-f):f-m*u;return jo(l(e,t,s,i),t*s*i)+jo(l(n,r,c,a),r*c*a)}function $f(e,t,n,r,o){let s=e/t/(n/r);return jf(s,o,i=>Pd(e,t,n,r,i))}function Vd(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 $d(e,t,n,r,o){let[s,i]=Vd(e,t,n,r,o),c=e-t*s;return jo(c,t*s*(1-s))+jo(c,r*i*(1-i))}function zf(e,t,n,r,o){let s=e*(r-n)/((t-e)*n);return jf(s,o,i=>$d(e,t,n,r,i))}function jf(e,t,n){if(t===0)return[e,e];let r=zo(t),o=l=>n(l)-t,s=l=>l>0&&l<e?Ni(o,l,Number.MIN_VALUE,e,!1):l,i=l=>l>e&&l<1/0?Ni(o,l,e,Number.MAX_VALUE,!0):l;if(e===0||e===1/0){let l=u=>zo(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:zo(n(Math.exp(l))))-r;return[s(Pf(a,c,-1)),i(Pf(a,c,1))]}var zd=["method","confLevel","sides"],jd=["method","confLevel","sides"],Kd=["method","confLevel"],Bd=["ac","wald","score","mn","mee"];function Ei(e,t,n,r,o,s){let{kappa:i}=s,c=e/t,a=n/r,l=c-a,f,m;switch(o){case"wald":{let u=i*Math.sqrt(c*(1-c)/t+a*(1-a)/r);[f,m]=[Math.max(-1,l-u),Math.min(1,l+u)];break}case"ac":{let u=(e+1)/(t+2),h=(n+1)/(r+2),p=u-h,g=i*Math.sqrt(u*(1-u)/(t+2)+h*(1-h)/(r+2));[f,m]=[Math.max(-1,p-g),Math.min(1,p+g)];break}case"score":{let[u,h]=Ks(e,t,i),[p,g]=Ks(n,r,i);f=l-i*Math.sqrt(u*(1-u)/t+g*(1-g)/r),m=l+i*Math.sqrt(h*(1-h)/t+p*(1-p)/r);break}case"mn":case"mee":[f,m]=Vf(e,t,n,r,s.chisq,o);break}return[Math.min(f,m),Math.max(f,m)]}function Wd(e,t,n,r,o={}){B(o,zd,"binomDiffCI");let s=ke(o.method,Bd),i=$o(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]=Ei(c,a,l,f,s,i);return i.sides==="left"?h=1:i.sides==="right"&&(u=-1),J("binomDiffCI",{estimate:m,confInt:[u,h],confLevel:o.confLevel??.95,method:s,sides:i.sides,warnings:[]})}var Ud=["katz.log","koopman"];function Us(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 Hd(e,t,n,r,o={}){B(o,jd,"binomRatioCI");let s=ke(o.method,Ud),i=$o(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,Us(.5,a,l,f,m)[1]]:l===0?[h,p]=[Us(c,a,.5,f,m)[0],1/0]:c===a&&l===f?[h,p]=Us(a-.5,a,f-.5,f,m):[h,p]=Us(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),J("binomRatioCI",{estimate:u,confInt:[h,p],confLevel:o.confLevel??.95,method:s,sides:i.sides,warnings:[]})}var Yd=["midp","fisher","wald","score"],Gd={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"},Xd=2**23,Qd=2**20;function Kf(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=pe(r,[e[o],0]));return r[0]}function Zd(e,t={}){B(t,Kd,"oddsRatio");let n=ke(t.method,Yd);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}=$o(r,void 0),c=e.map((A,k)=>Array.from(A,(C,_)=>Qr(C,`table[${k}][${_}]`))),[[a,l],[f,m]]=c,u=a+l+f+m;if(u===0)throw new RangeError("all entries of the table are zero");if(!(u<=2**53))throw new RangeError("the total of the table must be at most 2^53");let h=Math.max(0,a-m),p=a+Math.min(l,f),g=p-h+1;if(n==="midp"||n==="fisher"){if(g>Xd)throw new RangeError(`the exact distribution of the 2 x 2 table has ${g} values, more than 2^23: use method "wald" or "score"`);if(n==="fisher"&&Math.max(a,l,f,m)>2147483647)throw new RangeError(`'table' has entries too large for the conditional MLE (above 2^31 - 1): use method "wald" or "score"`)}let d,v,N;try{d=an(a-1,a+f,l+m,a+l,!0,!1)+.5*vn(a,a+f,l+m,a+l,!1),v=an(a,a+f,l+m,a+l,!1,!1)+.5*vn(a,a+f,l+m,a+l,!1),N=g<=Qd?ti(new Gr(a+f,l+m,a+l),a,1):pf(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 b=2*Math.min(d,v),E=[a+l,f+m],w=[a+f,l+m],y=0,x=!1;for(let A=0;A<2;A++)for(let k=0;k<2;k++){let C=E[k]*w[A]/u;C<5&&(x=!0);let _=Math.abs(c[k][A]-C);y+=_*_/C}let M=bt(y,.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=Ze(()=>Do(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]:zf(a,a+l,f,f+m,i);break}case"midp":{let A=new Gr(a+f,l+m,a+l),k=F=>{if(F===1)return[d,v];let L=A.density(F),D=a-h,S=.5*L[D];return[Kf(L,0,D-1)+S,Kf(L,D+1,L.length-1)+S]},C=(F,L)=>{let D=F(1);return D===0?1:rr(F,L?D>0:D<0)};R=a===h?0:a===p?1/0:C(F=>k(F)[0]-.5,!1);let _=a===h?0:C(F=>k(F)[1]-o/2,!0),I=a===p?1/0:C(F=>k(F)[0]-o/2,!1);T=[_,I];break}}return J("oddsRatio",{estimate:R,confInt:T,confLevel:r,method:Gd[n],pValue:{midp:b,fisher:N,chiSquare:M},warnings:x?["Chi-squared approximation may be incorrect"]:[]})}var Jd=["epsilon","paired","varEqual","confLevel","alpha"],e0=["margin","method","alpha"];function Bf(e,t){if(!(typeof e=="number"&&e>0&&e<.5))throw new RangeError(`'${t}' must be a single number in (0, 0.5)`)}function t0(e,t,n={}){B(n,Jd,"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)Bf(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=Ze(()=>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=gt((r-Math.abs(m))/l,f,!1,!1),h=s<1e-8?wt(s,f,!1,!1):wt(o,f,!0,!1),p=[m-h*l,m+h*l];return J("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 n0=["mn","mee","wald"];function r0(e,t,n,r,o={}){B(o,e0,"binomDiffEquivalenceTest");let s=ke(o.method,n0),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;Bf(c,"alpha");let[a,l]=Bn(e,t,"x1","n1"),[f,m]=Bn(n,r,"x2","n2"),u=a/l-f/m,h=E=>Ws(a,l,f,m,E)/Math.sqrt(Mi(a,l,f,m,E,s)),p=h(-i),g=h(i),d=[ae(p,{lowerTail:!1}),ae(g)],v=Math.max(d[0],d[1]),N=1-2*c,b=Ei(a,l,f,m,s,wi(2*c,N));return J("binomDiffEquivalenceTest",{method:s,estimate:u,margin:i,alpha:c,statistic:[p,g],statisticName:"z",pValues:d,pValue:v,result:d[0]<c&&d[1]<c?"rejected":"not rejected",confInt:b,confLevel:N,warnings:[]})}var Yt=new WeakMap;function Ys(e){let t=(n,r)=>n.weights===void 0?Array.from(r):r.filter((o,s)=>n.weights[s]!==0);return Ie(e.flatMap(n=>[t(n,n.fittedValues),t(n,n.residuals)])).k}function Gs(e,t){return e.map(n=>{if(n.rss>=o0&&n.rss<1/0)return $(n.rss,-2*t);let r=0;return n.residuals.forEach((o,s)=>{let i=n.weights?.[s]??1;if(i===0)return;let c=$(o,-t);r+=i*c*c}),r})}var o0=2**-1022;function Ri(e,t){return e.y.flatMap((n,r)=>(e.weights?.[r]??1)===0?[]:[[e.weights?.[r]??1,$(n,-t)]])}function Xs(e){let t=Math.floor(Math.log2(e.reduce((n,[r])=>Math.max(n,r),0)));return t-(t%2+2)%2}function xr(e,t,n=0){let r=i=>e.reduce((c,[a,l])=>c+$(a,-i)*l*l,0),o=n,s=r(o);return Number.isFinite(s)||(o=Xs(e),s=r(o)),!($(t,-o)>=1e-24*s)}function Wf(e){let t=Ys([e]),{pivot:n}=e.qr,r=[],o=new Map;for(let s=0;s<e.rank;s++){let i=e.assign[n[s]],c=o.get(i);c===void 0&&(c={term:i,df:0,ss:0},o.set(i,c),r.push(c)),c.df++,c.ss+=$(e.effects[s],-t)**2}return{terms:r,rdf:e.dfResidual,rss:Gs([e],t)[0],k:t}}var Uf=(e,t,n)=>ht(e,t,n,!1,!1),Ti="essentially perfect fit for \u20181\u2019: summary may be unreliable";function Ai(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 Si=new WeakMap;function s0(e){let t=Wf(e),n=t.terms.filter(o=>o.term!==0),r=0;return e.fittedValues.forEach((o,s)=>{let i=e.weights?.[s]??1;i!==0&&(r+=i*$(o,-t.k)**2)}),{names:n.map(o=>e.termLabels[o.term-1]),rows:n,rdf:t.rdf,rss:t.rss,k:t.k,perfect:t.rss<1e-10*r}}function xi(e){let t=s0(e),{names:n,rdf:r,k:o,perfect:s}=t,{rows:i,rss:c}=t;if(r>0){let g=Ri(e,o);xr(g,c)&&(c=0,i=i.map(d=>xr(g,d.ss)?{...d,ss:0}:d))}let a=c/r,l=g=>$(g,2*o),f=i.map(({df:g,ss:d})=>{let v=d/g/a;return[g,l(d),l(d/g),v,Uf(v,g,r)]});f.push([r,l(c),r===0?NaN:l(a),NaN,NaN]);let m=["Df","Sum Sq","Mean Sq","F value","Pr(>F)"],u=[...n,"Residuals"],h={colNames:m,rowNames:u,table:f,warnings:s?["ANOVA F-tests on an essentially perfect fit are unreliable"]:[]},p=i.map(({df:g,ss:d})=>[g,d]);return p.push([r,c]),Yt.set(h,{colNames:["Df","Sum Sq"],rowNames:u,table:p}),h}function a0(e){let t=e.map(f=>f.dfResidual),n=Ys(e),r=Gs(e,n),o=f=>$(f,2*n),s=0;t.forEach((f,m)=>{f<t[s]&&(s=m)});let i=r[s]/t[s],c=0;e[s].fittedValues.forEach((f,m)=>{let u=e[s].weights?.[m]??1;u!==0&&(c+=u*$(f,-n)**2)});let a=r[s]<1e-10*c,l=e.map((f,m)=>{if(m===0)return[t[0],o(r[0]),NaN,NaN,NaN,NaN];let u=t[m-1]-t[m],h=r[m-1]-r[m],[p,g]=u>0?[e[m-1],e[m]]:[e[m],e[m-1]],d=N=>N.filter((b,E)=>(p.weights?.[E]??1)!==0),v=u===0||!Ss(d(p.x),d(g.x))?NaN:h/u/i;return v<0&&(v=NaN),[t[m],o(r[m]),u,o(h),v,Number.isNaN(v)?NaN:Uf(v,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 Hs(...e){let t=[];for(let n of e)for(let r of n??[])t.includes(r)||t.push(r);return t}function ki(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=xi(e),a={...c,warnings:Hs(e.warnings,c.warnings)},l=Yt.get(c);return l!==void 0&&Yt.set(a,l),J("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?xi(e):a0(o);return i.warnings=Hs(...n.map(c=>c.warnings),s,i.warnings),J("anova",i,...n)}function qi(e){if(typeof e=="object"&&e!==null&&e.multistratum===!0){let i=e,c=Hs(i.warnings,Si.get(i)),a={strata:i.strata.filter(f=>f.stratum!=="Error: (Intercept)").map(f=>({...f})),warnings:c},l=Yt.get(i);return Array.isArray(l)&&Yt.set(a,l.filter((f,m)=>i.strata[m].stratum!=="Error: (Intercept)")),J("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=xi(t),r=Wf(t),o=Ai(t.fittedValues.map(i=>$(i,-r.k)),r.rss,r.rdf),s=Hs(t.warnings,n.warnings,o?[Ti]:[]);if(t.dfResidual>0){let i={...n,warnings:s},c=Yt.get(n);return c!==void 0&&Yt.set(i,c),J("aovSummary",i,e)}return J("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 i0=["pooledSd","mu","paired","ci","alternative","adjust"],c0=["pooledSd","mu","paired","ci","alternative"],l0=["generalized","partial","ci","alternative"],f0=["partial","ci","alternative"],m0=["partial","ci","alternative"];function Li(e){if(e!==null&&!(e>0&&e<1))throw new RangeError("ci must be a single numeric value between (0, 1)")}var u0="Missing values detected. NAs dropped.",h0=`'y' is numeric but has only 2 unique values.
3
+ If this is a grouping variable, convert it to a factor.`,Xf={tol:0,maxiter:5e3};function Rr(e){return e===1/0?1:Math.exp(hl(.5,(e-1)/2)-.5*Math.log(e/2))}function Hf(e,t,n){let r=e-1<e+1?1:Math.abs(e)*Number.EPSILON;return vr(o=>Bt(e,t,{ncp:o})-n,e-r,e+r,{...Xf,extendInt:"downX"}).root}function Qf(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;Li(c);let a=ke(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(h0),(l.some(Number.isNaN)||(f?.some(Number.isNaN)??!1))&&m.push(u0);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,g,d,v,N;if(f===void 0||i){let A;if(f===void 0)({groups:[A],k:N}=Ie([Dt(l)]));else{let[C,_]=Jn(l,f);({d:A,k:N}=ps(C,_))}let k=A.length;if(k<2)throw new RangeError(f===void 0?"not enough observations: at least 2 are needed":"not enough complete pairs: at least 2 are needed");h=it(A),p=Math.sqrt(at(A)),g=p/Math.sqrt(k),d=1/k,v=k-1}else{let A=Tt([Dt(l),Dt(f)]);N=A.k;let[k,C]=A.n;if(k<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 _=Math.sqrt(A.variance[0]),I=Math.sqrt(A.variance[1]);o?(p=Math.sqrt(((k-1)*_**2+(C-1)*I**2)/(k+C-2)),g=p*Math.sqrt(1/k+1/C),d=1/k+1/C,v=k+C-2):(p=Math.sqrt((_**2+I**2)/2),d=2*(C*_**2+k*I**2)/(k*C*(_**2+I**2)),g=Math.sqrt(Math.sqrt(_**2/k)**2+Math.sqrt(I**2/C)**2),v=ds([_**2/k,I**2/C],[k-1,C-1]))}let b=r?Rr(v):1,E=$(s,-N),w=(h-E)/p*b,y=r?"Hedges_g":"Cohens_d";if(c===null)return{name:y,value:w,warnings:m};let x=a==="two.sided"?c:2*c-1,M=(h-E)/g,R=Hf(M,v,(1+x)/2)*Math.sqrt(d)*b,T=Hf(M,v,(1-x)/2)*Math.sqrt(d)*b;return a==="less"&&(R=-1/0),a==="greater"&&(T=1/0),{name:y,value:w,ci:c,ciLow:R,ciHigh:T,warnings:m}}function p0(e,t,n={}){return B(n,i0,"cohensD"),J("cohensD",Qf(e,t,n,n.adjust??!1))}function d0(e,t,n={}){return B(n,c0,"hedgesG"),J("hedgesG",Qf(e,t,n,!0))}var g0=e=>Array.isArray(e.effects),Ii=e=>e.multistratum===!0,Zf=e=>Array.isArray(e.strata),Jf=e=>{let t=e;return typeof t.univariate=="object"&&typeof t.anova=="object"},_i=e=>({test:t=>t.split(":").includes(e)});function w0(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 b0(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(v=>_i(v).test(m)),g=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]};g.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 em(e,t){let n=Array.isArray(e)?e:[],r=n.filter(s=>!t.some(i=>_i(s).test(i.name)));if(r.length>0)throw new RangeError(`Observed variable not in data: ${r.join(", ")}`);let o=s=>n.some(i=>_i(i).test(s));return{isObserved:o,ssObserved:t.reduce((s,i)=>o(i.name)?s+i.ss:s,0)}}function Yf(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:vr(o,0,Math.max(10,e*t*4),{...Xf,extendInt:"downX"}).root:0}function tm(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=Yf(c,t,n,(1+s)/2),l=Yf(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 v0(e){let t=Ie([e.map(n=>n.ss)]).k;if(t!==0)for(let n of e)n.ss=$(n.ss,-t)}var Gf="needs a table of anova() or Anova(), a fit from lm() or aov() with a formula, its aovSummary(), or a result of aovEz()";function Ci(e,t,n){if(typeof e!="object"||e===null)throw new TypeError(Gf);if(Ii(e)||Zf(e)||Jf(e))return N0(e,t,n);let r=g0(e)?Ze(()=>ki(e)):e;if(!Array.isArray(r.colNames)||!Array.isArray(r.table))throw new TypeError(Gf);let o=Yt.get(r),s=o!==void 0&&!Array.isArray(o)?o:r,i=n.ci===void 0?.95:n.ci;Li(i);let c=ke(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)");v0(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,g=p/h,d=f.reduce((R,T)=>R+T.ss,0),v=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:w,ssObserved:y}=em(N,u),x=R=>{let{ss:T,df:A}=R;if(t==="Eta2")return E==="partial"?T/(T+p):E==="plain"?T/d:T/(T+p+y-(w(R.name)?T:0));if(t==="Omega2"){let C=E==="partial"?(T-A*g)/(T+(v-A)*g):(T-A*g)/(d+g);return Math.max(0,C)}let k=E==="partial"?(T-A*g)/(T+p):(T-A*g)/d;return Math.max(0,k)},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)]:tm(x(R),R.df,h,i,c)),warnings:Array.isArray(r.warnings)?[...r.warnings]:[]}}function N0(e,t,n){let r=n.ci===void 0?.95:n.ci;Li(r);let o=ke(n.alternative??"greater",["two.sided","less","greater"]),s=Jf(e),i,c,a;if(s){let w=Yt.get(e),y=b0(w!==void 0&&!Array.isArray(w)?{...e,univariate:w}:e);({strata:i,subject:c}=y),a=y.order.map(x=>({term:x,stratum:y.strata.find(M=>M.terms.includes(x))}))}else{let w=Ii(e)?Ze(()=>qi(e)):e,y=Yt.get(w);({strata:i,subject:c}=w0(Array.isArray(y)?{...w,strata:y.map((x,M)=>({...w.strata[M],...x}))}:w)),a=i.flatMap(x=>x.terms.map(M=>({term:M,stratum:x})))}let l=Ie([[...i.flatMap(w=>w.terms.map(y=>y.ss)),...i.map(w=>w.sse)]]).k;for(let w of i){w.sse=$(w.sse,-l);for(let y of w.terms)y.ss=$(y.ss,-l)}let f=i.flatMap(w=>w.terms),m=i.reduce((w,y)=>w+y.sse,0),u=f.reduce((w,y)=>w+y.ss,0)+m,h=c.sse/c.dfe,p=t==="Eta2"?n.generalized??!1:!1,g=p!==!1?"generalized":n.partial??!0?"partial":"plain",{isObserved:d,ssObserved:v}=em(p,f),N=(w,y)=>{let{ss:x,df:M}=w,R=y.sse/y.dfe;if(t==="Eta2")return g==="partial"?x/(x+y.sse):g==="plain"?x/u:x/(x+m+v-(d(w.name)?x:0));if(t==="Omega2"){let A=y===c?0:1,k=g==="partial"?(x-M*R)/(x+A*y.sse+c.sse+h):(x-M*R)/(u+h);return Math.max(0,k)}let T=g==="partial"?(x-M*R)/(x+y.sse):(x-M*R)/u;return Math.max(0,T)},b=`${t}${g==="plain"?"":`_${g}`}`,E={colNames:r===null?[b]:[b,"CI","CI_low","CI_high"],rowNames:a.map(w=>w.term.name),table:a.map(({term:w,stratum:y})=>{let x=N(w,y);return r===null?[x]:tm(x,w.df,y.dfe,r,o)}),warnings:[]};return s||(E.groups=a.map(w=>w.stratum.name),E.warnings=[...Ii(e)||Zf(e)?e.warnings:[]]),E}function y0(e,t={}){return B(t,l0,"etaSquared"),J("etaSquared",Ci(e,"Eta2",t),e)}function M0(e,t={}){return B(t,f0,"omegaSquared"),J("omegaSquared",Ci(e,"Omega2",t),e)}function E0(e,t={}){return B(t,m0,"epsilonSquared"),J("epsilonSquared",Ci(e,"Epsilon2",t),e)}var x0=new Set(["eqb","lowEqbound","highEqbound","hypothesis","paired","varEqual","alpha","mu","eqboundType","biasCorrection","rmCorrection","glass","smdCi"]),R0=["m1","sd1","n1","m2","sd2","n2","r12"],T0=new Set(["eqb","lowEqbound","highEqbound","hypothesis","paired","varEqual","alpha","mu","eqboundType","biasCorrection","rmCorrection","glass","smdCi"]),A0="Equivalence interval does not include zero.",S0="Warning: setting bound type to SMD produces biased results!",k0="MET test may have higher error rates than a nil two-tailed test. Consider wider equivalence bounds.";function nm(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 vr(s=>Bt(e,t,{ncp:s,lowerTail:!r})-n,e-o,e+o,{tol:0,maxiter:5e3,extendInt:r?"upX":"downX"}).root}function q0(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,g,d,v,N=t.biasCorrection;if(e.design==="two"){let{n1:M,n2:R,s1:T,s2:A}=e,k=T*T,C=A*A,_=2*M*R/(M+R);if(t.glass!==void 0){let I=t.glass==="glass1",[F,L,D,S]=I?[T,M,A,R]:[A,R,T,M];c=F,a=L-1,i=N?"Glass's delta(g)":"Glass's delta(d)",l=N?Rr(a):1,f=l*o/c,p=1/R+1/M;let O=D/F;m=Math.sqrt(O*O/(S-1)+1/(L-1)+f*f/(2*(L-1))),u=o/(F*Math.sqrt(1/L+O*O/S)),h=M+R-2,g=Math.sqrt(2/_)}else if(t.varEqual)c=Math.sqrt(((M-1)*k+(R-1)*C)/(M+R-2)),a=M+R-2,i=N?"Hedges's g":"Cohen's d",l=N?Rr(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,g=Math.sqrt(2*(R*k+M*C)/(M*R*(k+C)));else{c=Math.sqrt((k+C)/2),a=(M-1)*(R-1)*(k+C)**2/((R-1)*k*k+(M-1)*C*C),i=N?"Hedges's g(av)":"Cohen's d(av)",l=N?Rr(a):1,f=l*o/c,p=2*(R*k+M*C)/(M*R*(k+C));let I=c**4;m=Math.sqrt(f*f*(k*k/(M-1)+C*C/(R-1))/(8*I)+(k/(M-1)+C/(R-1))/(c*c)),u=o/Math.sqrt(k/M+C/R),h=a,g=Math.sqrt(2/_)}d=a,v=(I,F)=>Math.sqrt(d/(d-2)*(2/_)*(1+I*I*_/2)-I*I/(F*F))}else if(e.design==="paired"){let{n:M,s1:R,s2:T,sdif:A,oneMinusR:k}=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*k):A,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?Rr(r==="goulet"?d:a):1,f=l*o/c,p=1/M,u=o*Math.sqrt(M)/c,h=a,g=Math.sqrt(2*k/M),v=(_,I)=>{let F=r==="goulet"?d:a,L=Math.sqrt(F/(F-2)*(2*k/M)*(1+_*_*M/(2*k))-_*_/(I*I));return t.rmCorrection?L:L*Math.sqrt(2*k)},t.glass!==void 0){let _=A/c;m=Math.sqrt(_*_/(M-1)+f*f/(2*(M-1)))}else!t.rmCorrection&&r==="nct"?m=Math.sqrt(1/M+s(l,f,a)):m=v(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?Rr(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,g=1/Math.sqrt(M),v=(T,A)=>Math.sqrt(a/(a-2)*(1/M)*(1+T*T*M)-T*T/(A*A))}let b,E,w,y=a;if(r==="nct"){w=m,y=h;let M=nm(u,h,n,!0),R=nm(u,h,n,!1);b=M*Math.sqrt(p)*l,E=R*Math.sqrt(p)*l}else if(r==="goulet"){w=v(f,l),y=d;let M=f/g;b=Wt(n,d,{ncp:M})*g,E=Wt(n,d,{ncp:M,lowerTail:!1})*g}else{w=m;let M=r==="t"?wt(n,a,!1,!1):ie(n,{lowerTail:!1});b=f-M*w,E=f+M*w}let x=e.delta<0;return{label:i,denom:c,estimate:x?-f:f,se:w,confInt:x?[-E,-b]:[b,E],df:y,J:l}}function rm(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 om(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 sm(e){let t=ke(e.hypothesis,["EQU","MET"]),n=ke(e.eqboundType,["raw","SMD"])==="SMD",r=ke(e.smdCi,["nct","goulet","t","z"]),o=e.glass===void 0?void 0:ke(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 am(e,t,n,r,o,s,i,c,a,l,f,m){let{alpha:u,hypothesis:h}=l,p=q0(c,l),g=$(p.denom,a),d=l.smdBounds?[f[0]*g,f[1]*g]:f,v=D=>{if(D===0)return[0,n];let S=Math.floor(Math.log2(Math.abs(D))),O=Ve(D,-S);return l.smdBounds?[O*p.denom,S+a]:[O,S]},N=l.smdBounds?f:[f[0]/g,f[1]/g];l.smdBounds&&m.push(S0),d[0]<=0&&d[1]>=0||m.push(A0);let b=D=>$(D/r,n-o),E=D=>{let[S,O]=v(D),q=Math.max(n,O);return Ve((Ve(t,n-q)-Ve(S,O-q))/r,q-o)},w=b(t),y=E(f[0]),x=E(f[1]),M=$(r,o),R=h==="EQU",T={t:w,se:M,df:s,pValue:2*gt(-Math.abs(w),s,!0,!1)},A={t:y,se:M,df:s,pValue:gt(y,s,!R,!1)},k={t:x,se:M,df:s,pValue:gt(x,s,R,!1)},C=R?Math.max(A.pValue,k.pValue):Math.min(A.pValue,k.pValue);!R&&d[0]<=0&&d[1]>=0&&C<=T.pValue&&m.push(k0);let _=wt(u,s,!1,!1),I=Math.max(n,o),[F,L]=[$(t,n-I),$(r,o-I)];return J(e,{tTest:T,lower:A,upper:k,pValue:C,bounds:{raw:d,smd:N},raw:{estimate:$(t,n),se:M,confInt:[$(F-_*L,I),$(F+_*L,I)]},smd:{label:p.label,estimate:p.estimate,se:p.se,confInt:p.confInt,df:p.df,J:p.J},confLevel:1-2*u,method:i,hypothesis:h,warnings:m})}function I0(e,t,n={}){B(n,x0,"tTOST");let r=sm(n),o=om(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");rm(e,"x"),a||rm(t,"y");let l=Ze(()=>a?yn(e,{mu:c}):yn(e,t,{paired:s,varEqual:i,mu:c})),f=l.parameter,m,u,h,p;if(a){let g=Ie([Dt(Array.from(e))]);u=g.k;let d=g.groups[0],v=at(d);h=it(d)-$(c,-u),p=Math.sqrt(v/d.length),m={design:"one",n:d.length,delta:h,s:Math.sqrt(v)}}else if(s){let[g,d]=Jn(Array.from(e),Array.from(t)),v=Ie([g,d]);u=v.k;let[N,b]=v.groups,E=N.map((T,A)=>T-b[A]),w=Math.sqrt(at(N)),y=Math.sqrt(at(b)),x=at(E),M=Math.sqrt(x),R=(M-(w-y))*(M+(w-y))/(2*w*y);h=it(E)-$(c,-u),p=Math.sqrt(x/E.length),m={design:"paired",n:E.length,delta:it(N)-it(b)-$(c,-u),s1:w,s2:y,sdif:M,oneMinusR:R}}else{let g=Ie([Dt(Array.from(e)),Dt(Array.from(t))]);u=g.k;let[d,v]=g.groups;if(d.length<2||v.length<2)throw new RangeError("not enough observations: at least 2 are needed in each sample");let[N,b,E,w]=[d.length,v.length,at(d),at(v)];h=it(d)-it(v)-$(c,-u),p=Math.sqrt(i?((N-1)*E+(b-1)*w)/(N+b-2)*(1/N+1/b):Math.sqrt(E/N)**2+Math.sqrt(w/b)**2),m={design:"two",n1:N,n2:b,delta:h,s1:Math.sqrt(E),s2:Math.sqrt(w)}}return am("tTOST",h,u,p,u,f,l.method,m,u,r,o,[])}function _0(e,t={}){B(e,R0,"tsumTOST"),B(t,T0,"tsumTOST");let n=sm(t),r=om(t),{m1:o,sd1:s,n1:i,m2:c,sd2:a,n2:l,r12:f}=e,m=(I,F)=>{if(typeof I!="number"||!Number.isFinite(I))throw new RangeError(`'${F}' must be a finite number`);return I},u=(I,F)=>{let L=m(I,F);if(!(Number.isInteger(L)&&L>=2))throw new RangeError(`'${F}' must be an integer of at least 2`);return L},h=(I,F)=>{let L=m(I,F);if(!(L>0))throw new RangeError(`'${F}' must be positive`);return L};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 g=[],{groups:d,k:v}=Ie([[o,...p?[]:[c],n.mu]]),{groups:N,k:b}=Ie([[s,...p?[]:[a]]]),[E,w=NaN]=p?[d[0][0]]:[d[0][0],d[0][1]],y=d[0][d[0].length-1],[x,M=NaN]=N[0],R=I=>$(I,v-b),T,A,k,C,_;if(p)T=E-y,A=x/Math.sqrt(i),k=i-1,C="One Sample t-test",_={design:"one",n:i,delta:R(T),s:x};else if(n.paired){let I=m(f,"r12");if(!(I>-1&&I<1))throw new RangeError("'r12' must be in (-1, 1)");let F=i;i!==l&&(g.push("Unequal number of pairs; using smallest n"),F=Math.min(i,l));let L=Math.sqrt(x*x+M*M-2*I*x*M);if(!(L>0))throw new RangeError("the standard deviation of the differences is 0");T=E-w-y,A=L/Math.sqrt(F),k=F-1,C="Paired t-test",_={design:"paired",n:F,delta:R(T),s1:x,s2:M,sdif:L,oneMinusR:1-I}}else{let I=l,F=x*x,L=M*M;if(T=E-w-y,n.varEqual){let D=((i-1)*F+(I-1)*L)/(i+I-2);A=Math.sqrt(D*(1/i+1/I)),k=i+I-2,C=" Two Sample t-test"}else{let D=F/i,S=L/I;A=Math.sqrt(D+S),k=(D+S)**2/(D*D/(i-1)+S*S/(I-1)),C="Welch Two Sample t-test"}_={design:"two",n1:i,n2:I,delta:R(T),s1:x,s2:M}}return am("tsumTOST",T,v,A,b,k,C,_,b,n,r,g)}var L0=["alternative","tsmethod"],C0=["square","central"],F0=1e3,O0=32,D0=.9,im=1+1e-7,P0=2**24,V0=34e7;function $0(e,t={}){if(B(t,L0,"boschlooTest"),e.length!==2||e.some(y=>y.length!==2))throw new TypeError("'table' must be a 2 x 2 table");let n=Je(t.alternative),r=ke(t.tsmethod,C0),o=e.map((y,x)=>Array.from(y,(M,R)=>Qr(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)>P0)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(F0,Math.ceil(O0*Math.sqrt(m))),h=y=>Math.min(y+1,40*Math.sqrt(y));if(u*(h(l)+h(f)+4)>V0)throw new RangeError(`Boschloo's test of these group sizes (${l} and ${f}) would take too long`);let p=s/l-c/f,g;n!=="two.sided"?g=n:r==="square"?g="square":g=p>0?"greater":"less";let d=s+c,v;g==="less"?v=an(s,l,f,d,!0,!1):g==="greater"?v=an(s-1,l,f,d,!1,!1):v=Ze(()=>Do(o)).pValue;let N=z0(l,f,s,c,g),{value:b,at:E}=j0(N,l,f,u),w=n==="two.sided"&&r==="central"?Math.min(1,2*b):b;return J("boschlooTest",{method:"Boschloo's Exact Test",alternative:n,statistic:v,statisticName:"test statistic",pValue:w,estimate:p,estimateNames:"difference in proportion",nullValue:0,nullValueName:"difference in proportion",nuisanceParameter:E,warnings:[]})}function z0(e,t,n,r,o){let s=e+t,i=new Float64Array(s+1);for(let v=2;v<=s;v++)i[v]=ws(v);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 v=0;v<=s;v++){let N=Math.max(0,v-t),E=Math.min(v,e)-N+1,w=-1/0;for(let M=0;M<E;M++){let R=N+M,T=-i[R]-i[e-R]-i[v-R]-i[t-v+R];f[M]=T,T>w&&(w=T)}let y=0;for(let M=0;M<E;M++)f[M]=Math.exp(f[M]-w),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+v-N-M]=m[M];else if(o==="greater")for(let M=0;M<E;M++)a[(N+M)*c+v-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]*im,A=-1,k=M;for(;k-A>1;){let F=A+k>>1;f[F]<=T?A=F:k=F}let C=M,_=E;for(;_-C>1;){let F=C+_>>1;f[F]<=T?_=F:C=F}let I=f[M]<=T?1:(A>=0?m[A]:0)+(_<E?u[_]:0);a[(N+R)*c+v-N-R]=Math.min(1,I)}}}let h=a[n*c+r]*im,p=new Int32Array(e+2),g=[],d=[];for(let v=0;v<=e;v++){p[v]=g.length;let N=0;for(;N<=t;){if(a[v*c+N]<=h){let b=N;for(;N+1<=t&&a[v*c+N+1]<=h;)N++;g.push(b),d.push(N)}N++}}return p[e+1]=g.length,{starts:p,from:Int32Array.from(g),to:Int32Array.from(d)}}function cm(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]=pn(r,e,t,!1);let o=Ot(1,-t),s=ot([t,0],o),i=ot(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 j0(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=b=>{let[E,w]=cm(t,b,c),[y,x]=cm(n,b,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<=w;T++){let A=c[T];if(A===0)continue;let k=0;for(let C=o[T];C<o[T+1];C++){let _=s[C],I=i[C];if(!(I<y||_>x))if(_===0)k+=l[Math.min(I,x)];else if(I===n)k+=f[Math.max(_,y)];else for(let F=Math.max(_,y);F<=Math.min(I,x);F++)k+=a[F]}R+=A*k}return Math.min(1,R)},u=Math.PI/2,h=b=>b<=0?0:b>=u?1:Math.sin(b)**2,p=new Float64Array(r+2),g=0;for(let b=0;b<=r+1;b++)p[b]=m(h(u*b/(r+1))),p[b]>p[g]&&(g=b);let d=p[g],v=h(u*g/(r+1));if(d>=1)return{value:1,at:v};let N=D0*d;for(let b=0;b<=r+1;b++){let E=p[b];if(E<N||b>0&&!(E>p[b-1])||b<=r&&!(E>=p[b+1]))continue;let w=u*Math.max(0,b-1)/(r+1),y=u*Math.min(r+1,b+1)/(r+1),M=Cl(A=>-m(h(A)),w,y,1e-14).x,R=m(h(M)),T=1e-4/Math.sqrt(2*(t+n));for(let A=0;A<2&&(M-T>w&&M+T<y);A++,T*=.01){let k=m(h(M-T)),C=m(h(M+T)),_=k-2*R+C;if(!(_<0))break;let I=T*(k-C)/(2*_);if(!(Math.abs(I)<T))break;let F=m(h(M+I));F>R*(1+2**-50)&&(M+=I,R=F)}R>d&&(d=R,v=h(M))}return{value:d,at:v}}var lm=["correct"];function K0(e,t,n={}){ct(t)&&B(t,lm,"mcnemarTest"),B(n,lm,"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=B0(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=fm(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+=fm(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 fm(e,t,n){let r=t+n;return r<1/0?Vt(e,r):Vt($(e,-520),$(t,-1040)+$(n,-1040))}function B0(e,t){if(e.length!==t.length)throw new RangeError("'x' and 'y' must have the same length");let n=[],r=nn(e),o=nn(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 W0=["ratio","alternative","confLevel"],Fi=2**-1022;function mm(e,t,n={}){if(B(n,W0,"varTest"),typeof e=="string"){let b=tr(e,t);return mm(b.x,b.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=Je(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]=Tt([r,o]).variance,u=0;if(!(f>=Fi&&m>=Fi)){let b=Tt([r]),E=Tt([o]);f=b.variance[0],m=E.variance[0],u=2*(b.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,g=ht(p,a,l,!0,!1),d=ht(p,a,l,!1,!1),v,N;if(i==="less")v=g,N=[0,h/hn(1-c,a,l,!0,!1)];else if(i==="greater")v=d,N=[h/hn(c,a,l,!0,!1),1/0];else{v=Math.min(1,2*Math.min(g,d));let b=(1-c)/2,E=b<1e-8?hn(b,a,l,!1,!1):hn(1-b,a,l,!0,!1);N=[h/E,h/hn(b,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:v,confInt:N,confLevel:c,estimate:h,estimateNames:"ratio of variances",nullValue:s,nullValueName:"ratio of variances",warnings:[]}}function um(e,t){if(typeof e=="string"){let p=Ao(e,t);return um(_l(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=Tt(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<Fi)){let p=o.map(E=>{let w=Tt([E]);return{w:w.variance[0],e:w.k}}),g=Math.max(...p.map(E=>E.e)),d=p.map(E=>Math.log(E.w)+2*(E.e-g)*Math.LN2),v=d.map((E,w)=>Math.log(s[w])+E),N=Math.max(...v),b=N+Math.log(v.reduce((E,w)=>E+Math.exp(w-N),0))-Math.log(c);m=c*b-d.reduce((E,w,y)=>E+s[y]*w,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 U0=["alternative"],Oi=["alternative"];function Di(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 hm(e){let t=0;for(let n of bn(e))t+=n*n*n-n;return t}function Qs(e,t){if(typeof e=="string"){let l=Ao(e,t);return Qs(l.values,l.group.codes)}let n=Nr(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-hm(o)/(s*s*s-s)),Di("Kruskal-Wallis rank sum test","Kruskal-Wallis chi-squared",c,r-1)}function Pi(e,t){if(typeof e=="string")return Pi(xs(e,t));let n=Pa(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+=hm(l)}let c=0;for(let l of s)c+=(l-r*(o+1)/2)**2;let a=12*c/(r*o*(o+1)-i/(o-1));return Di("Friedman rank sum test","Friedman chi-squared",a,o-1)}function H0(e,t={},n,r={}){typeof e!="string"&&ct(t)&&B(t,Oi,"pageTest"),ct(n)&&B(n,Oi,"pageTest"),B(r,Oi,"pageTest");let o,s;if(typeof e=="string")o=xs(e,t),s=n??{};else if(Array.isArray(n)){let g=Array.from(e),d=t,v=n;if(g.length!==d.length||g.length!==v.length)throw new RangeError("'x', 'groups' and 'blocks' must have the same length");if(!g.every(Number.isFinite))throw new RangeError("missing or non-finite values are not allowed");let N=nn(d),b=nn(v);if(N.some(M=>typeof M=="number"&&Number.isNaN(M))||b.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),w=Array.from(new Set(b)).sort(zn);if(E.length<2||w.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(w.map((M,R)=>[M,R]));o=w.map(()=>new Array(E.length).fill(NaN));for(let M=0;M<g.length;M++){let R=x.get(b[M]),T=y.get(N[M]);if(!Number.isNaN(o[R][T]))throw new RangeError("not an unreplicated complete block design");o[R][T]=g[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(g=>Array.from(g)),s=t;let i=o.length,c=o[0]?.length??0;if(i<1||c<2||o.some(g=>g.length!==c))throw new RangeError("'y' must be a non-empty matrix with at least two columns");if(o.some(g=>g.some(d=>!Number.isFinite(d))))throw new RangeError("missing or non-finite values are not allowed in a block");let a=Je(s.alternative),l=new Array(c).fill(0);for(let g of o){let d=vt(g);for(let v=0;v<c;v++)l[v]+=d[v]}let f=0;for(let g=0;g<c;g++)f+=(g+1)*l[g];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"?ae(h):a==="greater"?ae(h,{lowerTail:!1}):2*ae(-Math.abs(h));return{method:"Page's ordered aligned rank sum test",alternative:a,statistic:h,statisticName:"z",pValue:p,estimate:f,warnings:[]}}function Y0(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{...Pi(e),method:"Cochran's Q test",statisticName:"Q"}}function pm(e,t){if(typeof e=="string")return pm(xs(e,t));let n=Pa(e),r=n.length,o=e[0]?.length??0;if(n.length<2||o<2)throw new RangeError("'y' must have at least two complete rows and two columns");let s=n.map(g=>yt(g)-cn(g));s.some(g=>g===1/0)&&n.every(g=>g.every(Number.isFinite))&&(s=Ie(n).groups.map(g=>yt(g)-cn(g)));let i=vt(s),c=new Array(o).fill(0),a=0;n.forEach((g,d)=>{let v=vt(g);for(let N=0;N<o;N++){let b=i[d]*(v[N]-(o+1)/2);a+=b*b,c[N]+=b}});let l=0;for(let g of c)l+=g*g;l/=r;let f=o-1,m=(r-1)*(o-1),u,h,p=[f,m];if(a===l){u=NaN,p=[NaN,NaN];let g=0;for(let d=2;d<=o;d++)g+=Math.log(d);h=Math.exp((1-r)*g)}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 dm(e,t){if(typeof e=="string"){let h=Ao(e,t);return dm(h.values,h.group.codes)}let n=Nr(e,t,"error"),r=n.length;if(r<2)throw new RangeError("all observations are in the same group");let o=h=>h.flatMap(p=>{let g=ks(p);return p.map(d=>Math.abs(d-g))}),s=o(n);if(s.some(Number.isNaN))throw new RangeError("a group has an infinite median: its deviations are undefined");s.some(h=>h===1/0)&&n.every(h=>h.every(Number.isFinite))&&(s=o(Ie(n).groups));let i=s.length,a=vt(s).map(h=>ie((1+h/(i+1))/2)),l=0;for(let h of a)l+=h;let f=l/i,m=0,u=0;for(let h of n){let p=0;for(let g=0;g<h.length;g++)p+=a[u+g];u+=h.length,m+=h.length*(p/h.length-f)**2}return m/=at(a),Di("Fligner-Killeen test of homogeneity of variances","Fligner-Killeen:med chi-squared",m,r-1)}function gm(e,t,n={}){if(B(n,U0,"moodTest"),typeof e=="string"){let E=tr(e,t);return gm(E.x,E.y,n)}let r=Array.from(e).filter(Number.isFinite),o=Array.from(t).filter(Number.isFinite),s=Je(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,w)=>w).sort((E,w)=>l[E]-l[w]),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 w=0;for(let y=u;y<=E;y++)w+=(y+1-(a+1)/2)**2;w/=E-u+1;for(let y=u;y<=E;y++)m[f[y]]=w;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 g=0;for(let E of m)g+=(E-p)**2;let d=i*p,v=i*c/(a*(a-1))*g,N=(h-d)/Math.sqrt(v),b=s==="less"?ae(N):s==="greater"?ae(N,{lowerTail:!1}):2*ae(-Math.abs(N));return{method:"Mood two-sample test of scale",alternative:s,statistic:N,statisticName:"Z",pValue:b,warnings:[]}}function Tr(e,t){let n=0;for(let r=e.length-1;r>=0;r--)n=n*t+e[r];return n}var G0=[0,.221157,-.147981,-2.07119,4.434685,-2.706056],X0=[0,.042981,-.293762,-1.752461,5.682633,-3.582633],Q0=[.544,-.39978,.025054,-6714e-7],Z0=[1.3822,-.77857,.062767,-.0020322],J0=[-1.5861,-.31082,-.083751,.0038915],eg=[-.4803,-.082676,.0030302],tg=[-2.273,.459];function ng(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]=-ie((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+Tr(G0,i),a=new Float64Array(n);a[0]=c;let l,f;if(e>5){let m=r[1]/s+Tr(X0,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 wm(e){let t=Ie([e]).groups[0],n=t.length,r=ng(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 rg=1.90985931710274,og=1.0471975511966;function bm(e,t){if(Number.isNaN(e)||!(t>=3))return NaN;if(t===3){let i=rg*(Math.asin(Math.sqrt(1-e))-og);return Math.max(i,0)}let n=Math.log(e);if(t<=11){let i=Tr(tg,t);if(n>=i)return 1e-99;let c=Tr(Q0,t),a=Math.exp(Tr(Z0,t));return ae(-Math.log(i-n),{mean:c,sd:a,lowerTail:!1})}let r=Math.log(t),o=Tr(J0,r),s=Math.exp(Tr(eg,r));return ae(n,{mean:o,sd:s,lowerTail:!1})}var Mm=Math.log(2*Math.PI),vm=2e-14,Nm=500;function sg(e){return Math.exp(-.5*e*e-.5*Mm)}function ag(e){return ae(e,{logP:!0})}function Em(e,t=-1/0,n=!1){let r=t===-1/0?Oa(e,0,2,0,vm,Nm):Oa(e,t,1,0,vm,Nm),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 ym(e,t,n){let r=t.map(Math.sqrt),o=t.map(i=>Math.sqrt(1-i));return Em(i=>{let c=0;for(let a=0;a<r.length;a++){let l=(r[a]*i+e)/o[a];if(!n){c+=ag(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 sg(i)*-Math.expm1(c)})}function Ar(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,ym(e,t,r));let o=n/2,s=Math.log(2)+.5*Math.log(o)-.5*Mm-cl(o);return Math.min(1,Em(c=>{let a=s-o*(Math.expm1(2*c)-2*c);return a<-745?0:Math.exp(a)*ym(e*Math.exp(c),t,r)},-1/0,!0))}var ig=["pAdjustMethod"],cg=["pAdjustMethod","alternative"],lg=["alternative"],fg=["pAdjustMethod","alternative"],mg=["method","altp"],ug=["alternative"],hg=["welch"],xm="Ties are present. z-quantiles were corrected for ties.";function kn(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 Vi(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 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 pg(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=Xr(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 Ko(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 $i(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 Zs=e=>ae(e,{lowerTail:!1}),zi=(e,t,n)=>bs(Math.abs(e),t,n,{lowerTail:!1});function ji(e){let t=Tt(e.groups),n=0;for(let r of t.n)r>1&&(n+=r-1);return Bo(n,t.ssWithin),{m:t.mean,s2:t.ssWithin/n,df:e.N-e.k,k:t.k}}function Bo(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 dg(e,t,n={}){B(n,ig,"kwAllPairsDunnTest");let r=n.pAdjustMethod??"holm",o=kn(e,t),{R:s,C:i}=Vi(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=pg(a.map(f=>f.map(m=>2*Zs(m))),r);return J("kwAllPairsDunnTest",Ko(o,"Dunn's all-pairs test",a,l,r,i!==0?[xm]:[]))}function gg(e,t,n={}){B(n,cg,"kwManyOneDunnTest");let r=Je(n.alternative),o=n.pAdjustMethod??"single-step",s=kn(e,t),{R:i,C:c}=Vi(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=Rm(f,m,r,o);return J("kwManyOneDunnTest",$i(s,"Dunn's many-to-one test",f,u,o,c!==0?[xm]:[]))}function Rm(e,t,n,r){return r==="single-step"?e.map(o=>n==="two.sided"?Ar(o,t,1/0,!0):Ar(n==="greater"?o:-o,t,1/0,!1)):Xr(e.map(o=>n==="two.sided"?2*Zs(Math.abs(o)):Zs(n==="greater"?o:-o)),r)}function wg(e,t,n={}){B(n,lg,"dunnettTest");let r=Je(n.alternative),o=kn(e,t),{m:s,s2:i,df:c}=ji(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*Ar(Math.abs(h),m,c,!1)):Ar(r==="greater"?h:-h,m,c,!1));return J("dunnettTest",$i(o,`Dunnett's-test for multiple
4
+ comparisons with one control`,f,u,"single-step"))}function bg(e,t,n={}){B(n,fg,"manyOneUTest");let r=Je(n.alternative),o=n.pAdjustMethod??"single-step",s=kn(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=bt(h),w=0;for(let x=0;x<i;x++)w+=p[x];let d=i+u,v=d*(d+1)/2-w,N=i*u+i*(i+1)/2-w,b=i*u+u*(u+1)/2-v,E=0;for(let x of bn(h))E+=x*x*x-x;let g=i*u/(d*(d-1))*((d*d*d-d)/12-E/12),y=(Math.min(N,b)-i*u/2)/Math.sqrt(g);return w<v?-y:y}),l=c.map(m=>s.n[m]/(s.n[m]+i)),f=xm(a,l,r,o);return Z("manyOneUTest",Pi(s,`Wilcoxon, Mann, Whittney U-test
7
- for multiple comparisons with one control`,a,f,o))}function wg(e,t){let n=kn(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 v=0;v<c;v++)m+=f[v];let u=m-c*(c+1)/2,h=Math.min(u,c*a-u),p=c+a,w=0;for(let v of bn(l))w+=v*v*v-v;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 Z("dscfAllPairsTest",$o(n,"Dwass-Steele-Critchlow-Fligner all-pairs test",r,o,"single-step"))}function bg(e,t,n={}){B(n,mg,"tamhaneT2Test");let r=(()=>{if(t!==void 0)return kn(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=Rt(r.groups),s=Rt(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?qa([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 Z("tamhaneT2Test",$o(r,i?"Tamhane's T2-test for unequal variances":"Tamhane's T2'-test for unequal variances",a,l,i?"T2 (Sidak)":"T2' (Sidak)"))}function vg(e,t){let n=kn(e,t),r=Rt(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=qa([r.variance[a],r.variance[l]],[n.n[a],n.n[l]]);return Vi(i[a-1][l],n.k,f)});return Z("gamesHowellTest",$o(n,"Games-Howell test",i,c,"none"))}function Ng(e,t){let n=kn(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 Z("tukeyTest",$o(n,"Tukey's test",i,c,"single-step"))}function yg(e,t,n={}){B(n,lg,"dunnTest");let r=n.method??"none",o=n.altp??!1,s=kn(e,t),i=Ys(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)*Gs(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 Eg(e,t,n={}){B(n,fg,"glhtDunnett");let r=Ze(n.alternative),o=kn(e,t),{m:s,s2:i,df:c,k:a}=$i(o),l=Math.sqrt(i),f=o.n.slice(1).map((N,b)=>b+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,b)=>N/u[b]),p=m.map(N=>$(N,a)),w=u.map(N=>$(N,a)),d=f.map(N=>o.n[N]/(o.n[N]+o.n[0])),v=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:v,stdError:w,tValue:h,pValue:v,df:c,warnings:[]}}var Rm=["varEqual"],xg=2**-1022;function Sm(e,t,n={}){if(typeof e!="string"&&ct(t)&&B(t,Rm,"onewayTest"),B(n,Rm,"onewayTest"),typeof e=="string"){let d=_l(e,t);return Sm(d.values,d.groups,n)}let r;t!==void 0&&!Array.isArray(t)?n=t:r=t;let o=vr(e,r),s=Rt(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,v)=>d+v,0),h,p,w;if(c){let d=s.grandMean,v=0,N=0;for(let b=0;b<a;b++)v+=l[b]*(f[b]-d)**2,N+=(l[b]-1)*m[b];p=u-a,zo(p,s.ssWithin),h=v/(a-1)/(N/p)}else if(m.some(d=>!(d>=xg))&&(w=Tg(o))!==void 0){let d=w.map((M,R)=>Tm(l[R]/M.variance,-2*M.k)),v=Math.max(...d.map(M=>M.e)),N=d.map(M=>Ve(M.m,M.e-v)),b=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/b)**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=Tm(A,T.k);g.push({m:d[M].m*d[R].m*C.m*C.m/b,e:d[M].e+d[R].e+2*C.e-v})}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 v=d[0],N=0,b=0;for(let E=0;E<a;E++)E!==v&&(N+=1/(l[E]-1),b+=l[E]/m[E]*(f[E]-f[v])**2);N/=a*a-1,h=b/((a-1)*(1+2*(a-2)*N)),p=1/(3*N)}else{let d=l.map((g,y)=>g/m[y]),v=d.reduce((g,y)=>g+y,0),N=0,b=0;for(let g=0;g<a;g++)N+=(1-d[g]/v)**2/(l[g]-1),b+=d[g]*f[g];N/=a*a-1,b/=v;let E=0;for(let g=0;g<a;g++)E+=d[g]*(f[g]-b)**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 Rg(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=gm(t);return{method:"Shapiro-Wilk normality test",statistic:1-r,statisticName:"W",pValue:wm(r,n),warnings:[]}}function Tg(e){let t=[];for(let n of e){let r=Rt([n]),o=r.variance[0];if(!(o>0))return;t.push({mean:r.mean[0],variance:o,k:r.k})}return t}function Tm(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 Sg([[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 Ag(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 Xs=class{lo;hi;logNull;constructor(t){let n=0,r=new Float64Array([1]);for(let o of t){let s=Sg(o);n+=s.lo,r=Ag(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 Am(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 kg=["alternative","correct","exact","confLevel"];function qg(e,t={}){B(t,kg,"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?Lg(f,i,l):Ig(f,s,i,c,l)}return Cg(e,s,r,o)}function Ki(e){return e.reduce((t,n)=>t+n.reduce((r,o)=>r+o,0),0)}function Im(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 _m(e,t){return e.map(n=>n.map(r=>r.map(o=>$(o,-t))))}function km(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],v=m+u,N=h+p,b=m+h,E=u+p;r+=m;let g=v*b/d;o+=Number.isFinite(g)?g:v/d*b;let y=v*N*b*E/(d*d*(d-n));s+=Number.isFinite(y)?y:v/d*(N/d)*(b/(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 Ig(e,t,n,r,o){let s=km(e,t,1),i=0;if(!t.every(Number.isFinite)||!Object.values(s).every(Number.isFinite)){i=Im(e);let x=_m(e,i);s=km(x,x.map(Ki),$(1,-i))}let{sa:c,se:a,sv:l,sR:f,sS:m,sPR:u,sPSQR:h,sQS:p}=s,w=c-a,d=$(.5,-i),v=r&&Math.abs(w)>=d?d:0,N=$(Vt(Math.abs(w)-v,l),i),b=n==="two.sided"?wt(N,.5,2,!1,!1):se(Math.sign(w)*Math.sqrt(N),{lowerTail:n==="less"}),E=f/m,g=$(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(ie(o)*g)];else if(n==="greater")y=[E*Math.exp(ie(o,{lowerTail:!1})*g),1/0];else{let x=(1-o)/2,M=x<1e-8?ie(x,{lowerTail:!1}):ie((1+o)/2);y=[E*Math.exp(-M*g),E*Math.exp(M*g)]}return{method:`Mantel-Haenszel chi-squared test ${v!==0?"with":"without"} continuity correction`,alternative:n,statistic:N,statisticName:"Mantel-Haenszel X-squared",parameter:1,parameterName:"df",pValue:b,confInt:y,confLevel:o,estimate:E,estimateNames:"common odds ratio",nullValue:1,nullValueName:"common odds ratio",warnings:[]}}var _g=2**24;function Lg(e,t,n){let r=e.reduce((a,[[l,f],[m,u]])=>a+Math.min(l,u)+Math.min(f,m),1);if(r>_g)throw new RangeError(`the exact distribution of S has ${r} values, more than 2^24: use exact: false`);let o=new Xs(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:Am(o,s),estimateNames:"common odds ratio",nullValue:1,nullValueName:"common odds ratio",warnings:[]}}function Cg(e,t,n,r){let{diff:o,V:s}=qm(e,t,n,r,1),i=0;if(!t.every(Number.isFinite)||!o.every(Number.isFinite)||!s.every(f=>f.every(Number.isFinite))){i=Im(e);let f=_m(e,i);({diff:o,V:s}=qm(f,f.map(Ki),n,r,$(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=$(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 Fg=2**-1022;function qm(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,v)=>d+v[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,v=m[w]*u[p]/f;i[d]+=a[w][p]-(Number.isFinite(v)?v:hs(m[w],u[p],f));for(let N=0;N<r-1;N++)for(let b=0;b<n-1;b++){let E=(w===b?f*m[w]:0)-m[w]*m[b],g=(p===N?f*u[p]:0)-u[p]*u[N],y=h*E*g;c[d][b+(n-1)*N]+=Number.isFinite(y)&&h>=Fg?y:m[w]*((w===b?1:0)-m[b]/f)*(((p===N?1:0)-u[N]/f)*(u[p]/(f-o)))}}}),{diff:i,V:c}}var Lm=new Map,Og=2**24,Dg=2**32;function Fm(e,t){let n=e+t,r=(e+1)*(Math.floor((n+1)**2/4)+1);if(2*r>Og||n*r>Dg)throw new RangeError(`the exact distribution for m = ${e} and n = ${t} is too large to compute: use exact: false`)}function Pg(e,t){let n=`${e},${t}`,r=Lm.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 v=c[d],N=e-d;if(N>h)continue;let b=N/h;for(let E=0;E<=i;E++){let g=v[E];g!==0&&(N>0&&(w[d+1][E+p]+=g*b),N<h&&(w[d][E]+=g*(1-b)))}}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 Lm.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}=Pg(t,n),i=fl(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 Cm=new Map;function Vg(e,t){let n=`${e},${t}`,r=Cm.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 v=w[d];v!==0&&(p<e&&(h[p+1][d+u]+=v),e-p<o-m&&(h[p][d]+=v))}}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 Cm.set(n,f),f}function Qs(e,t,n){let{min:r,counts:o}=Vg(t,n);if(e<=0)return r;if(e>=1)return r+o.length-1;let s=gl(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 $g=["alternative","exact","confInt","confLevel"];function Vm(e,t,n={}){if(B(n,$g,"ansariTest"),typeof e=="string"){let g=tr(e,t);return Vm(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),v=new Tt,N,b;if(w&&!p){Fm(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&&(b=Bg(r,o,c,l,v))}else{let g=$m(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&&(b=Kg(r,o,c,l,d?new Tt:v)),d&&p&&(v.add("cannot compute exact p-value with ties"),a&&v.add("cannot compute exact confidence interval with ties"))}let E={method:"Ansari-Bradley test",alternative:c,statistic:h,statisticName:"AB",pValue:N,warnings:v.list};return b!==void 0&&(E.confInt=b.confInt,E.confLevel=l,E.estimate=b.estimate,E.estimateNames="ratio of scales"),E.nullValue=1,E.nullValueName="ratio of scales",Z("ansariTest",E)}function $m(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 zm(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)<zg))?jg(e.map(f=>Om(f,n)).concat(t.map(f=>Om(f,1)))):Wg(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 zg=2**-1022;function Om(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]=Dm(Math.abs(e)),[s,i]=Dm(t),c=o/i,a=r-s;return c<1&&(c*=2,a-=1),[n,a,c]}function Dm(e){let t=Math.floor(Math.log2(e)),n=$(e,-t);return n>=2?(n/=2,t+=1):n<1&&(n*=2,t-=1),[t,n]}function Pm(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 jg(e){let t=e.map((r,o)=>o).sort((r,o)=>Pm(e[r],e[o])),n=new Array(e.length);for(let r=0;r<t.length;){let o=r+1;for(;o<t.length&&Pm(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 Wg(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 Zs(e,t,n){return e==="two.sided"?n(t):e==="greater"?[n(2*t)[0],1/0]:[0,n(2*t)[1]]}function Bg(e,t,n,r,o){let s=e.length,i=t.length,c=[];for(let v of e)for(let N of t)v/N>=0&&c.push(v/N);c.sort((v,N)=>v-N);let a=c.length;if(a===0)return o.add("cannot compute confidence set, returning NA"),{confInt:Zs(n,1-r,()=>[NaN,NaN]),estimate:NaN};let l=c.map((v,N)=>zm(e,t,v+(N<a-1?(c[N+1]-v)/2:v*1.01)).ab),f=(v,N,b)=>{let E=1/0;for(let y of l)(N?y-v>0:y-v>=0)&&y-v<E&&(E=y-v);let g=b?-1/0:1/0;return l.forEach((y,x)=>{y-v===E&&(g=b?Math.max(g,c[x]):Math.min(g,c[x]))}),g},m=v=>{let N=Qs(v/2,s,i),b=Qs(1-v/2,s,i);if(!l.some(y=>y-N>=0)||!l.some(y=>y-b>0))return o.add("samples differ in location: cannot compute confidence set, returning NA"),[NaN,NaN];let E=f(N,!1,!1),g=f(b,!0,!0);return E>g?[f(b,!1,!1),f(N,!0,!0)]:[E,g]},u=Zs(n,1-r,m),h=Qs(.5,s,i),p=-1/0,w=1/0;l.forEach((v,N)=>{v-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 Kg(e,t,n,r,o){let s=e.length,i=t.length,c=(v,N)=>{let{ab:b,a:E,ties:g}=zm(e,t,v);return $m(b,E,g,s,i)-N},a=[],l=(v,N)=>{v.length===0||N.length===0||a.push(an(v)/Nt(N),Nt(v)/an(N))};if(l(e.filter(v=>v>0),t.filter(v=>v>0)),l(e.filter(v=>v<=0),t.filter(v=>v<0)),a.some(v=>!Number.isFinite(v))){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:Zs(n,1-r,()=>[NaN,NaN]),estimate:NaN};let f=an(a),m=Nt(a),u=v=>Kt(N=>c(N,v),f,m,{tol:1e-4}),h=v=>{let N=ie(v/2),b=ie(v/2,{lowerTail:!1});if(c(f,N)>0||c(m,b)<0)return o.add("samples differ in location: cannot compute confidence set, returning NA"),[NaN,NaN];let E=u(N),g=u(b);return E<=g?[E,g]:[g,E]},p=Zs(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 Ug=["alternative","confLevel"];function Hg(e,t,n={}){B(n,Ug,"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),v=w(m,bt(o),p),N=(p-(i+1)/2)/s,b=s*d+i*v,E=s*i*(p-h)/(s+i)/Math.sqrt(b),g=b**2/((s*d)**2/(s-1)+(i*v)**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)+v/(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 Yg=[1.2337005501361697,7831619385924639e-32];function Gg(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 Xg=2**33;function Wm(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>Xg)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 Bm(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]=Gg(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(Yg,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+=ll(i,e-i,l,f,!1)/(l[0]+l[1])}return Math.min(t*s,1)}var jm=2**28;function Km(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)>jm)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 v=c[d];if(v===0)continue;let N=e-d,b=Math.min(N,Math.floor((N+1)*p)),E=i-d,g=hn(b,N,p,!1);for(let y=b;y<=N&&g!==0;y++)y>b&&(g=g*(N-y+1)*w/y),y>=E?m+=v*g:a[d+y]+=v*g,u++;g=b>0?hn(b-1,N,p,!1):0;for(let y=b-1;y>=0&&g!==0;y--)y<b-1&&(g=g*(y+1)/((N-y)*w)),y>=E?m+=v*g:a[d+y]+=v*g,u++}if(u>2*jm)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 Um(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 Qg=["alternative","exact"];function Hm(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 Ym(e,t,n={}){if(B(n,Qg,"ksTest"),typeof e=="string"){let N=tr(e,t);return Ym(N.x,N.y,n)}let r=Array.from(e).filter(N=>!Number.isNaN(N)).sort((N,b)=>N-b),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,b=-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),b=Math.max(b,R-M/o)}let E=s==="two.sided"?Math.max(N,b):s==="greater"?N:b,g=new Set(r).size<o,y=n.exact??(o<100&&!g),x;if(y&&s==="two.sided"&&E<.5&&Wm(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?Km(o,E):NaN;x=Number.isNaN(R)?1-Bm(o,E):R}}else x=y?Yi(o,E):Math.exp(-2*o*E*E);return Z("ksTest",{method:`${y?"Exact":"Asymptotic"} one-sample Kolmogorov-Smirnov test`,alternative:Hm(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,b)=>N[0]-b[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 b=f/o-m/a;u=Math.max(u,b),h=Math.max(h,-b)}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,v;if(d)v=Um(p,o,a,w?{alternative:s,z:l.map(N=>N[0])}:{alternative:s});else{let N=o*a/(o+a);v=s==="two.sided"?Hi(Math.sqrt(N)*p,!1):Math.exp(-2*N*p*p)}return Z("ksTest",{method:`${d?"Exact":"Asymptotic"} two-sample Kolmogorov-Smirnov test`,alternative:Hm(s,!1),exact:d,statistic:p,statisticName:i,pValue:Math.min(1,Math.max(0,v)),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"],ew=["pAdjustMethod","correct"];function Gm(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=Hr(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 tw(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}=Gm(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(v=>v.length),h=Rt(a),p=h.mean,w=0;for(let v of u)v>1&&(w+=v-1);zo(w,h.ssWithin);let d=Math.sqrt(h.ssWithin/w);f=(v,N)=>{let b=(p[v]-p[N])/(d*Math.sqrt(1/u[v]+1/u[N]));return s==="less"?dt(b,w,!0,!1):s==="greater"?dt(b,w,!1,!1):2*dt(-Math.abs(b),w,!0,!1)}}else m=r?"paired t tests":"t tests with non-pooled SD",f=(u,h)=>ot(()=>yn(a[u],a[h],{paired:r,alternative:s})).pValue;return Xi(m,c,Gi(l,f,i),i)}function nw(e,t,n={}){B(n,Jg,"pairwiseWilcoxTest");let{pAdjustMethod:r,alternative:o,...s}=n,i=r??"holm",{levels:c,groups:a}=Gm(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 Tt,u=Gi(l,(h,p)=>{let w=ot(()=>Fo(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 Z("pairwiseWilcoxTest",Xi(f,c,u,i,m.list))}function rw(e,t,n={}){if(B(n,ew,"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 Tt,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 Z("pairwisePropTest",Xi("Pairwise comparison of proportions",f,l,i,a.list))}var jo=[0,0];function Js(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 ea(e,t){return Array.from({length:t},(n,r)=>e===void 0?[1,0]:Js([e[r],0]))}function Gr(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=jo;for(let v=f;v<r;v++)u=he(u,_e(m[v],m[v]));if(u[0]===0){a.push(jo);continue}let h=Js(u),p=m[f][0]<0?[-h[0],-h[1]]:h;m[f]=he(m[f],p);let w=_e(p,m[f]),d=v=>{let N=jo;for(let E=f;E<r;E++)N=he(N,_e(m[E],v[E]));let b=rt(N,w);for(let E=f;E<r;E++)v[E]=Le(v[E],_e(b,m[E]))};for(let v=f+1;v<e.length;v++)d(s[v]);d(i);for(let v of c)d(v);a.push([-p[0],-p[1]])}let l=new Array(o).fill(jo);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 Xr(e,t=0,n=e.length){let r=jo;for(let o=t;o<n;o++)r=he(r,_e(e[o],e[o]));return r}var ow=1e-14;function sw(e,t,n,r,o,s,i,c){let a=y=>$(y,2*i),l=t.length,f=n.flatMap((y,x)=>Xr(y)[0]>ow*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=Gr(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,v=Xr(h,m)[0],N=Ri(p,v,d),b=w.filter(y=>y.term!==0);d>0&&Er(c,v)&&(v=0,b=b.map(y=>Er(c,y.ss[0])?{...y,ss:[0,0]}:y));let E=b.map(y=>o[y.term-1]),g=y=>{if(d>0){let x=v/d,M=b.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(v),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:b.map(({df:x,ss:M})=>[x,y(M[0]),y(M[0]/x)])}};return[g(a),g(y=>y),N]}function Xm(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=vs({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(q=>!Number.isFinite(q)))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(q=>!a.warnings.includes(q))],h=f.colNames.length,p=f.colNames.map((q,F)=>ke([f.x.map(L=>L[F])]).groups[0]),w=f.x.map((q,F)=>p.map(L=>L[F])),d=h>0?Lt(w):void 0,v=d?.rank??0;v<h&&u.push("Error() model is singular");let N=d?.pivot.slice(0,v)??[],b=q=>q.map(F=>[F,0]),E=a.colNames.map((q,F)=>b(ke([a.x.map(L=>L[F])]).groups[0])),{groups:g,k:y}=ke([l]),{fit:x,qtv:M}=Kn(N.map(q=>b(p[q])),b(g[0]),E),R=g[0].map(q=>[1,q]),T=E.map(q=>Xr(q)[0]),S=[];N.forEach((q,F)=>{let L=f.assign[q],O=L===0?"(Intercept)":f.terms.termLabels[L-1],I=S.find(D=>D.name===O);I===void 0&&S.push(I={name:O,idx:[]}),I.idx.push(F)}),m>v&&S.push({name:"Within",idx:Array.from({length:m-v},(q,F)=>v+F)});let A=S.map(q=>sw(q.name,q.idx.map(F=>x.qty[F]),M.map(F=>q.idx.map(L=>F[L])),a.assign,a.terms.termLabels,T,y,R)),C=A.map(q=>q[0]),_={formula:e,multistratum:!0,strata:C,rows:a.frame.rows,warnings:u};return Ut.set(_,A.map(q=>q[1])),A.some((q,F)=>q[2]&&S[F].name!=="(Intercept)")&&Ti.set(_,[xi]),_}var aw=["weights","contrasts","levels","intercept"],iw=["level"],cw=["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=eu(t.x,r,t.frame.weights,t.terms.intercept,!0),s=o.result,i=[...t.warnings];if(!n){let a=o.yScale??0,l=o.fitRows.map(h=>$(s.fittedValues[h],-a)),f=l.reduce((h,p)=>h+p,0)/l.length,m=l.length>1?l.reduce((h,p)=>h+(p-f)**2,0)/(l.length-1):NaN,u=(o.rssScaled??o.rss)/s.dfResidual;Number.isFinite(u)&&u<1e-30*(f*f+m)&&i.push("essentially perfect fit: summary may be unreliable")}let c={...s,formula:e,response:t.terms.variables[0],coefNames:t.colNames,assign:t.assign,termLabels:t.terms.termLabels,order:t.terms.order,variables:t.terms.variables,factors:t.terms.factors,rows:t.frame.rows,x:t.x,y:r,effects:o.effects,rss:o.rss,nObs:o.fitRows.length,aov:n,warnings:i};return t.frame.weights!==void 0&&(c.weights=t.frame.weights),c}function Wo(e,t,n={}){if(B(n,aw,"lm"),typeof e=="string"){let r=jn(er(e),t,{...n,dropUnusedLevels:!0});return Z("lm",Oa(Zi(e,r,!1),Wl(t,r.terms.variables)))}return lw(e,t,n)}function lw(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=eu(l,f,a,r).result;return m.rows=c.flatMap((u,h)=>u?[h]:[]),Oa(m,s.length)}function fw(e,t,n,r,o,s,i){let c=s.rank,a=s.pivot.slice(0,c),l=n?.map(g=>$(g,-r)),f=ea(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=Js([x,0]);return[$(M[0],-r/2),$(M[1],-r/2)]}),m=a.map(g=>{let y=o.map((M,R)=>Et(f[R],e[M][g])),x=Pl(y.map(M=>M[0]));return{c:x===0?y:y.map(M=>[$(M[0],-x),$(M[1],-x)]),e:x}}),{qty:u,coef:h}=Gr(m.map(({c:g})=>g),o.map((g,y)=>Et(f[y],t[g]))),p=h.map((g,y)=>m[y].e===0?g:[$(g[0],-m[y].e),$(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}),v=Xr(u,c)[0],N=o.map(g=>l?.[g]??1),b=[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]))}),b=rt(y,g)}let E=[0,0];return o.forEach((g,y)=>{let x=Le(d[g],b);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:v,mss:E[0]}}function eu(e,t,n,r,o=!1){let s=ke([t.filter((l,f)=>n===void 0||n[f]!==0)]).k;if(s===0)return Qm(Zm(e,t,n,r,o),e,t,n);let i=Zm(e,t.map(l=>$(l,-s)),n,r,o),c=i.result,a=l=>$(l,s);return c.coefficients=c.coefficients.map(a),c.residuals=c.residuals.map(a),c.fittedValues=c.fittedValues.map(a),c.sigma=a(c.sigma),c.coefTable.estimate=c.coefTable.estimate.map(a),c.coefTable.stdError=c.coefTable.stdError.map(a),Qm({...i,effects:i.effects.map(a),rss:$(i.rss,2*s),yScale:s,rssScaled:i.rss},e,t,n)}function Qm(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]=ka(f,0)?No(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 mw(e){return e.qr.every(t=>t.every(Number.isFinite))&&e.qraux.every(Number.isFinite)}function Zm(e,t,n,r,o){if(t.length===0)throw new RangeError("0 (non-NA) cases");let s=t.map((P,Y)=>n===void 0||n[Y]!==0),i=e.filter((P,Y)=>s[Y]),c=t.filter((P,Y)=>s[Y]),a=n?.filter((P,Y)=>s[Y]),l=c.length;if(l===0)throw new RangeError("all weights are zero");if(i.some(P=>P.some(Y=>!Number.isFinite(Y))))throw new RangeError("NA/NaN/Inf in 'x'");if(c.some(P=>!Number.isFinite(P)))throw new RangeError("NA/NaN/Inf in 'y'");if(a?.some(P=>!Number.isFinite(P)))throw new RangeError("non-finite weights not allowed");let f=t.flatMap((P,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((P,Y)=>P.map(j=>j*p[Y])),d=Lt(w);a!==void 0&&Number.isFinite(m)&&u+2>0&&!mw(d)&&(h=u+2,p=a.map(P=>$(Math.sqrt(P),-h/2)),w=i.map((P,Y)=>P.map(j=>j*p[Y])),d=Lt(w),Ji.set(d,h));let v=p===void 0?c:c.map((P,Y)=>P*p[Y]),N=d.rank,b=P=>$(P,-u),E=o?fw(e,t,n,u,f,d,r):void 0,g=E?.coefficients??xs(d,v),y=E?.residuals??[],x=E?.fittedValues??[],M=l-N,R=a?.map(b)??new Array(l).fill(1),T=E?.rss??0;if(E===void 0){let P=Br(d,v),Y=p===void 0?P:P.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 ne=e[W].reduce((fe,Te,Ce)=>Number.isNaN(g[Ce])?fe:fe+Te*g[Ce],0);x.push(ne),y.push(U-ne)}});for(let U=0;U<l;U++)T+=R[U]*Y[U]**2}let S=x.filter((P,Y)=>s[Y]),A=0;if(a!==void 0&&a.some(P=>P!==0&&$(P,-u)<Qi)){let P=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(P.length>0&&P.every(Y=>Number.isFinite(Y.r))){let Y=Math.max(...P.map(W=>Math.log2(W.w)+2*W.a)),j=2*Math.floor(Math.floor(Y)/2),U=0;for(let W of P)U+=$(W.w,2*W.a-j)*$(W.r,-W.a)**2;T=U,A=j-u}}let C=T/M,_=$(Math.sqrt(C),(u+A)/2),q=h===0?0:(h-u)/2,F=d.qr.slice(0,N).map(P=>P.slice(0,N).map(Y=>h===0?$(Y,-u/2):Y)),{inv:L,e:O}=N>0?Ro(F):{inv:[],e:[]},D=d.pivot.slice(0,N).map((P,Y)=>[P,Y]).sort((P,Y)=>P[0]-Y[0]),k=D.map(([P])=>g[P]),V=D.map(([,P])=>$(Math.sqrt(L[P][P]*C),A/2-O[P]-q)),K=k.map((P,Y)=>P/V[Y]),X=K.map(P=>2*dt(-Math.abs(P),M,!0,!1)),re={coefficients:g,residuals:y,fittedValues:x,rank:N,dfResidual:M,coefTable:{estimate:k,stdError:V,tValue:K,pValue:X},sigma:_,rSquared:0,adjRSquared:0,intercept:r,qr:d,x:e,rows:e.map((P,Y)=>Y)};n!==void 0&&(re.weights=n);let z=r?1:0,J=N-z,Q={result:re,fitRows:f,effects:(E?.effects??[]).map(P=>$(P,u/2)),rss:$(T,u+A)};if(J===0)return Q;let Re=0;if(E!==void 0)Re=E.mss;else if(r){let P=R.reduce((j,U)=>j+U,0),Y=0;for(let j=0;j<l;j++)Y+=R[j]*S[j]/P;for(let j=0;j<l;j++)Re+=R[j]*(S[j]-Y)**2}else for(let P=0;P<l;P++)Re+=R[P]*S[P]**2;re.rSquared=Re/(Re+$(T,A)),re.adjRSquared=1-(1-re.rSquared)*((l-z)/M);let te=$(Re/J/C,-A);return re.fStatistic={value:te,numdf:J,dendf:M},re.fPValue=ht(te,J,M,!1,!1),Q}function tu(e,t){let n=(1-e)/2;return n<1e-8?gt(n,t,!1,!1):gt((1+e)/2,t,!0,!1)}function uw(e,t={}){B(t,iw,"confint");let n=t.level??.95;if(!(n>0&&n<1))throw new RangeError("'level' must be between 0 and 1");let r=tu(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 Jm(e,t,n){if(t===0||!Number.isFinite(t))return e*t;let r=Math.floor(Math.log2(e)),o=Math.floor(Math.log2(Math.abs(t)));return $($(e,-r)*$(t,-o),r+o+n)}function hw(e,t,n={}){B(n,cw,"predict");let o=(t.length>0&&typeof t[0]!="number"?t.map(b=>Array.from(b)):Array.from(t,b=>[b])).map(b=>e.intercept?[1,...b]:b);if(o.some(b=>b.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(b=>b.slice(0,s)),a=-(Ji.get(e.qr)??0)/2,l=e.coefficients.flatMap((b,E)=>Number.isNaN(b)?[]:[E]),f=o.map(b=>{let E=b.reduce((g,y,x)=>Number.isNaN(e.coefficients[x])?g:g+y*e.coefficients[x],0);return ka(E,0)?No(l.map(g=>b[g]),l.map(g=>e.coefficients[g]),E):E}),m=b=>{let E=new Array(s).fill(0);for(let g=0;g<s;g++){let y=b[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(b=>{let E=i.map(x=>b[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:Jm(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=>$(R,-(y+x)))));if(M<1/0)return M===0?M*e.sigma:Jm(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=zl(e.qr,e.rank,o),w=p.length>0?{nonEstimable:p,warnings:[$l]}:{};if(h==="none")return Z("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 v=tu(d,e.dfResidual),N=u.map(b=>v*(h==="prediction"?Math.hypot(b,e.sigma):b));return Z("predict",{fit:f,seFit:u,lwr:f.map((b,E)=>b-N[E]),upr:f.map((b,E)=>b+N[E]),...w},e)}var pw=["weights","contrasts","levels"],nu=["confLevel"],ru=["sigmaX","sigmaY","mu","alternative","confLevel"];function dw(e,t,n={}){return B(n,pw,"aov"),typeof e=="string"?Z("aov",ou(e,t,n)):Z("aov",gw(e,t))}function ou(e,t,n){let r=er(e,!0),{treatment:o,errors:s}=kl(r.rhs);if(s.length>1)throw new Error(`there are ${s.length} Error terms: only 1 is allowed`);return s.length===1?Xm(e,r,o,s[0],t,n):Zi(e,jn(r,t,{...n,dropUnusedLevels:!0},o),!0)}function gw(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 ou("y ~ g",{y:o,g:s},{levels:{g:n}})}function su(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}=Rt(e),i=t-1,c=n-t,a=r/i,l=o/c,f=a/l,m=u=>$(u,2*s);return{df:[i,c],sumSq:[m(r),m(o)],meanSq:[m(a),m(l)],fValue:f,pValue:c>0?ht(f,i,c,!1,!1):NaN}}function ww(e,t,n={}){ct(t)&&B(t,nu,"TukeyHSD"),B(n,nu,"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((v,N)=>v+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}=Rt(s),h=f/l,p=yl(c,i,l),w=[],d={names:[],i:[],j:[],diff:[],lwr:[],upr:[],pAdj:w,pAdjusted:w,warnings:[]};for(let v=0;v<i-1;v++)for(let N=v+1;N<i;N++){let b=m[N]-m[v],E=Math.sqrt(h/2*(1/s[N].length+1/s[v].length));d.names.push(`${o[N]}-${o[v]}`),d.i.push(N+1),d.j.push(v+1),d.diff.push($(b,u)),d.lwr.push($(b-p*E,u)),d.upr.push($(b+p*E,u)),d.pAdj.push(ds(Math.abs(b)/E,i,l,{lowerTail:!1}))}return d}function bw(e,t,n={}){ct(t)&&B(t,ru,"zTest"),B(n,ru,"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=ke([l,f??[],[o,s??0]]),u=m.k,[h,p,[w,d]]=m.groups,v=Qn(h)[0],N=f===void 0?NaN:Qn(p)[0],b=f===void 0?w/Math.sqrt(l.length):Math.sqrt(w**2/l.length+d**2/f.length),E=f===void 0?v:v-N,g=(E-$(i,-u))/b,y,x;if(c==="less")y=se(g),x=[-1/0,E+ie(a)*b];else if(c==="greater")y=se(g,{lowerTail:!1}),x=[E-ie(a)*b,1/0];else{y=2*se(-Math.abs(g));let T=(1-a)/2,S=(T<1e-8?ie(T,{lowerTail:!1}):ie(1-(1-a)/2))*b;x=[E-S,E+S]}x=[$(x[0],u),$(x[1],u)];let M=f!==void 0,R=(T,S)=>{if(!(u>0&&T.some((C,_)=>C!==0&&Math.abs(S[_])<2**-1022)))return $(Qn(S)[0],u);let A=Math.max(ke([T]).k,0);return $(Qn(A===0?T:T.map(C=>$(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 vw=["parm","level","test"],au={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 v of s){let N=$(n[v],-d);i+=N,c+=N*t[v]}}}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(v=>v*m[d])),{tol:o}),h=Br(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((v,N)=>m[N]*(t[v]-a)),d=0;for(let v of w)d=Math.max(d,Math.abs(v));for(let v of h)d=Math.max(d,Math.abs(v));if(Number.isFinite(d)){let v=d>1?Math.floor(Math.log2(d)):0,N=0;for(let E of w)N+=$(E,-v)**2;let b=0;for(let E of h)b+=$(E,-v)**2;return $($(N-b,v),v)}}return l-p}function Nw(e,t={}){B(t,vw,"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},(_,q)=>q);for(let _ of c)if(!Number.isInteger(_)||_<0||_>=i)throw new RangeError(`invalid coefficient index ${_}`);let a=n.model,l=n.coefficients.map((_,q)=>q).filter(_=>!Number.isNaN(n.coefficients[_])),f=new Tt,m=To(a,l,f,{...au,start:a.start===void 0?void 0:l.map(_=>a.start[_])}),u=new Array(i).fill(NaN);if(l.forEach((_,q)=>u[_]=m.fit.coefficients[q]),!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,v=new Array(i).fill(NaN);(m.fit.qr===void 0?[]:m.fit.qr.pivot.slice(0,m.fit.rank)).forEach((_,q)=>v[l[_]]=w.stdError[q]);let b=a.x.map(_=>l.map(q=>_[q])),E=m.fit.eta,g=(_,q)=>{let F=l.filter(I=>I!==_),L=To(a,F,new Tt,{...au,start:void 0,etastart:E,extraOffset:a.x.map(I=>I[_]*q)}).fit;if(!L.converged)return h=!0,NaN;let O=s==="LRT"?L.deviance-m.fit.deviance:tc(b,L.workingResiduals,L.workingWeights,!1);return Math.sign(q-u[_])*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]:[ie(y),y<1e-8?ie(y,{lowerTail:!1}):ie((1+o)/2)],S=c.map(()=>NaN),A=c.map(()=>NaN),C=(_,q,F)=>{let L=F===0?-1:1;h=!1;let O=NaN,I=NaN;for(let z=0;z<=30;z++){let J=u[_]+L*2**z*v[_],Q=g(_,J);if(Math.abs(Q)>=Math.abs(q)){O=J,I=Q;break}}if(Number.isNaN(O)||h)return NaN;let D=z=>z===u[_]?-q:g(_,z)-q,[k,V]=F===0?[O,u[_]]:[u[_],O],[K,X]=F===0?[I-q,-q]:[-q,I-q],re;try{re=Kt(D,k,V,{tol:1e-14*v[_],maxIter:500,fLower:K,fUpper:X})}catch(z){if(!h)throw z;re=NaN}return h?NaN:re};return c.forEach((_,q)=>{if(Number.isNaN(u[_]))return;if(!R){S[q]=C(_,T[0],0),A[q]=C(_,T[1],1);return}let F=[-2*M,-M,0,M,2*M],L=[C(_,-2*M,0),C(_,-M,0),u[_],C(_,M,1),C(_,2*M,1)].map(I=>I-u[_]),O=I=>{let D=0;for(let k=0;k<5;k++){let V=1;for(let K=0;K<5;K++)K!==k&&(V*=(I-F[K])/(F[k]-F[K]));D+=V*L[k]}return u[_]+D};S[q]=O(T[0]),A[q]=O(T[1])}),{lower:S.map(_=>$(_,r)),upper:A.map(_=>$(_,r))}}var yw=["type","omega","sandwich"],Mw=["cluster","type","cadjust","multi0","fix","sandwich"],ta=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 na=e=>Math.floor(Math.log2(Math.abs(e)));function fn(e){if(e===0||!Number.isFinite(e))return[e,0];let t=na(e);return[$(e,-t),t]}var Bo=(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+na(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?$(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 ra(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 fu(e,t){return e.pivot.slice(0,t).map((n,r)=>[n,r]).sort((n,r)=>n[0]-r[0])}function mu(e,t){let{inv:n,e:r}=Ro(e.qr.slice(0,t).map(s=>s.slice(0,t))),o=fu(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 iu(e,t,n){let{inv:r,e:o,cols:s}=mu(e,t);return{A:r.map((i,c)=>i.map((a,l)=>$(a,-o[c]-o[l]-n))),cols:s}}function cu(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)?na(a):0}),{inv:o}=Ro(n.map(i=>i.map((c,a)=>$(c,-r[a])))),s=fu(e,t);return{B:s.map(([,i])=>s.map(([,c])=>o[i][c])),e:s.map(([,i])=>r[i])}}function uu(e,t){let n=e.qr.length;return Vl(e,Array.from({length:n},(r,o)=>Array.from({length:t},(s,i)=>o===i?1:0)))}function Ew(e,t){let n=e.qr.length;return t===0?new Array(n).fill(0):uu(e,t).map(o=>{let s=o.reduce((i,c)=>i+c*c,0);return s>1-10*ta?1:s})}function lu(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 oa(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=oa(e);if(e.qr===void 0||e.rank===0)return{vcov:[],names:[]};let{inv:n,e:r,cols:o}=mu(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:So(l.fit),m=f===0||!Number.isFinite(f)?0:2*Math.floor(na(f)/2);i=$(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 hu(e,t=!1){let n=oa(e);if(cr(e)){let w=t?Wn(e):{fit:e,k:0},d=w.fit,v=$a(d),N=d.priorWeights,b=N.map(L=>L>0),E=N.flatMap((L,O)=>L>0?[O]:[]),{A:g,cols:y}=d.qr===void 0||d.rank===0?{A:[],cols:[]}:iu(d.qr,d.rank,0),x=E.map(L=>y.map(O=>d.model.x[L][O])),M=E.map(L=>d.weights[L]),R=E.map(L=>d.weights[L]*d.residuals[L]),T=v.family==="binomial"||v.family==="poisson",S=T?1:R.reduce((L,O)=>L+O*O,0)/M.reduce((L,O)=>L+O,0),A=d.model.rows,C=ja(d),_=Kl(d),q=()=>{let L=Wn(d),O=E.map(z=>L.fit.residuals[z]),I=M.map(Math.sqrt),{B:D,e:k}=d.qr===void 0||d.rank===0?{B:[],e:[]}:cu(d.qr,d.rank),V=x.map((z,J)=>z.map((Q,Re)=>$(I[J]*Q,-k[Re]))),{v:K,s:X}=ar(I.map((z,J)=>z===0?[0,0]:Bo(fn(z),fn(O[J])))),re=()=>{let z=[1,0];if(!T){let J=ar(M.map((Re,te)=>Re===0?[0,0]:Bo(fn(Re),fn(O[te])))),Q=ar(M.map(fn));z=[J.v.reduce((Re,te)=>Re+te*te,0)/Q.v.reduce((Re,te)=>Re+te,0),2*J.s-Q.s+2*L.k]}return{T:x.map(J=>J.map((Q,Re)=>ir(Q,-k[Re]))),factor:[z[0]*z[0],2*z[1]]}};return{T:V,B:D,e:k,u:K,s:X+L.k,omegaPart:re}},F;if(kr(S)&&R.every(Number.isFinite))F=R.map(L=>L/S);else{let L=Wn(d),O=ar(M.map((I,D)=>I===0?[0,0]:Bo(fn(I),fn(L.fit.residuals[E[D]]))));if(T)F=O.v.map(I=>ir(I,O.s+L.k));else{let I=ar(M.map(fn)),D=O.v.reduce((k,V)=>k+V*V,0)/I.v.reduce((k,V)=>k+V,0);F=O.v.map(k=>ir(k/D,I.s-O.s-L.k))}}return{glm:!0,names:y.map(L=>n[L]),X:x,W:M,res:F,phiS:S,A:g,h:C,omh:lu(d.qr,d.rank,C,L=>_[L]),qr:d.qr,rank:d.rank,qrRow:L=>_[L],inputRows:E.map(L=>A[L]),nInput:Da(e,A),nonZero:b,dispersion:()=>So(d),scaled:q,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:[]}:iu(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=Ew(r.qr,r.rank),p=()=>{let w=m.map(y=>c===0?Math.sqrt(y):$(Math.sqrt(y),-c/2)),{B:d,e:v}=r.rank===0?{B:[],e:[]}:cu(r.qr,r.rank),N=f.map((y,x)=>y.map((M,R)=>$(w[x]*M,-v[R]))),{v:b,s:E}=ar(w.map((y,x)=>Bo(fn(y),fn(r.residuals[i[x]]))));return{T:N,B:d,e:v,u:b,s:E,omegaPart:()=>({T:f.map(y=>y.map((x,M)=>ir(x,-v[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:lu(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,kr=e=>Math.abs(e)>=lc&&Math.abs(e)<1/0;function pu(e,t,n,r){if(!(e.phiS>=lc&&e.phiS<1/0)||!kr(e.X.length*e.phiS*e.phiS))return!1;for(let o=0;o<n.length;o++)if(!kr(e.A[o][o])||!r&&(!kr(t[o][o])||!kr(n[o][o]))||!n[o].every(Number.isFinite))return!1;return!0}function Ko(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 Qr(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 xw=["HC3","const","HC","HC0","HC1","HC2","HC4","HC4m","HC5"];function Rw(e,t={}){B(t,yw,"vcovHC"),ra(e);let n=Ae(t.type,xw),r=n==="HC"?"HC0":n,o=t.sandwich??!0;if(typeof o!="boolean")throw new TypeError("'sandwich' must be true or false");let s=hu(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,v)=>s.nonZero[v]))),!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,b)=>N>1-Math.sqrt(ta)?[s.inputRows[b]]:[]);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((b,E)=>b>=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,b)=>N+b,0)),v=f.reduce((N,b)=>Math.max(N,b),0);h=N=>{switch(r){case"HC0":return N;case"HC1":return N.map(b=>b*i/(i-c));case"HC2":return N.map((b,E)=>b/m[E]);case"HC3":return N.map((b,E)=>b/m[E]**2);case"HC4":return N.map((b,E)=>b/m[E]**Math.min(4,i*f[E]/d));case"HC4m":return N.map((b,E)=>b/m[E]**(Math.min(1,i*f[E]/d)+Math.min(1.5,i*f[E]/d)));default:return N.map((b,E)=>b/Math.sqrt(m[E]**Math.min(i*f[E]/d,Math.max(4,i*.7*v/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=Qr(s.X,u,i);if(p=Ko(s.A,w,i*s.phiS*s.phiS),!pu(s,w,p,(h===void 0?u:l).every(d=>d===0))){let d=s.scaled();if(h!==void 0){let v=Ko(d.B,Qr(d.T,h(d.u.map(N=>N*N)),i),i);p=Ar(v,d.e.map(N=>d.s-N))}else{let{T:v,factor:N}=d.omegaPart(),b=ar(u.map(E=>Bo(fn(E),N)),!0);p=Ar(Ko(d.B,Qr(v,b.v,i),i),d.e.map(E=>b.s/2-E))}}p=oc(p,s.k)}else{let w=Qr(s.X,u,i);if(wu(w,(h===void 0?u:l).every(d=>d===0)))p=w;else{let d=s.scaled();if(h!==void 0){let{mant:v,exp:N}=bu(d,Qr(d.T,h(d.u.map(b=>b*b)),i));p=Ar(v,N)}else{let v=ar(u.map(fn),!0);p=Ar(Qr(d.omegaPart().T,v.v,i),d.e.map(N=>v.s/2+N))}}p=oc(p,-s.k)}return Z("vcovHC",{vcov:p,names:s.names,warnings:a},e)}var Tw=["HC","HC0","HC1","HC2","HC3"];function Sw(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 Aw(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 kw(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 du=ta**(1/1.3);function gu(e,t){let{values:n,vectors:r}=Bt(e,{symmetric:!0});if(n.some(i=>!(i>=du)))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 qw(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 Iw(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 _w(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]>=du))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??=uu(e.qr,e.rank);let N=Lw(e,l,f,h,o);if(N===null)throw rc(m);if(N!==void 0)return Array.from({length:s},(b,E)=>c*u.reduce((g,y,x)=>g+y[E]*N[x],0))}let p=u.map(N=>Array.from({length:s},(b,E)=>N.reduce((g,y,x)=>g+y*t.A[x][E],0))),w=u.map((N,b)=>u.map((E,g)=>(b===g?1:0)-p[b].reduce((y,x,M)=>y+x*E[M],0)));for(let N=0;N<w.length;N++)for(let b=0;b<N;b++)w[N][b]=w[b][N]=(w[N][b]+w[b][N])/2;let d=gu(w,o);if(d===void 0)throw rc(m);let v=d.map(N=>N.reduce((b,E,g)=>b+E*h[g],0));return Array.from({length:s},(N,b)=>c*u.reduce((E,g,y)=>E+g[b]*v[y],0))})}function Lw(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)}),v=d.map(g=>d.map(y=>g.reduce((x,M,R)=>x+M*y[R],0))),N=gu(v,o);if(N===void 0)return null;let b=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*b[R],0)-b[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 Cw(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 Fw(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*ta*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 wu(e,t){return t||e.every((n,r)=>kr(n[r])&&n.every(Number.isFinite))}function bu(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 Ow(e,t={}){B(t,Mw,"vcovCL"),ra(e);let n=hu(e,!0),r=n.X.length,o=n.names.length,s=t.type===void 0?n.glm?"HC0":"HC1":Ae(t.type,Tw),i=s==="HC"?"HC0":s,c=t.cadjust??!0,a=t.multi0??!1,l=t.fix??!1,f=t.sandwich??!0;for(let[b,E]of[["cadjust",c],["multi0",a],["fix",l],["sandwich",f]])if(typeof E!="boolean")throw new TypeError(`'${b}' must be true or false`);let m,u=t.cluster;if(u===void 0)m=[n.inputRows.map(String)];else{let b=y=>Array.isArray(y)||ArrayBuffer.isView(y);if(!b(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(b)?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=>Sw(y[x]))})}if(i==="HC1"&&r<=o)throw new RangeError("HC1 covariances need more observations than coefficients");let h=m.length,p=kw(h).map((b,E)=>{let g=a&&h>1&&E===2**h-2,y=g?n.inputRows.map(String):n.inputRows.map((T,S)=>b.length===1?m[b[0]][S]:JSON.stringify(b.map(A=>m[A][S]))),{g:x,G:M}=Aw(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:b.length%2===1?1:-1}}),w=b=>{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=_w(n,b,g,y,i==="HC2"?-.5:-1):y===r?R=n.X.map((S,A)=>b.psi(A)):(R=Array.from({length:y},()=>new Array(o).fill(0)),n.X.forEach((S,A)=>{let C=R[g[A]],_=b.psi(A);for(let q=0;q<o;q++)C[q]+=_[q]}));let T=Cw(R,o);E=E.map((S,A)=>S.map((C,_)=>C+M*x*T[A][_]/r))}return i==="HC1"&&(E=E.map(g=>g.map(y=>y*(r-1)/(r-o)))),E},d=n.res.every(b=>b===0),v=n.phiS>=lc&&n.phiS<1/0&&n.A.every((b,E)=>kr(b[E]))&&n.res.every(Number.isFinite),N;if(v){let b=w(qw(n));if(!f)wu(b,d)&&(N=l?sc(b):b);else{let E=Ko(n.A,b,r*n.phiS*n.phiS);pu(n,b,E,d)&&(N=l?sc(E):E)}}if(N===void 0){let b=n.scaled(),E=w(Iw(b)),{mant:g,exp:y}=f?{mant:Ko(b.B,E,r),exp:b.e.map(M=>b.s-M)}:bu(b,E),x=l?Fw(g,y):{mant:g,exp:y};N=Ar(x.mant,x.exp)}return N=oc(N,f?n.k:-n.k),Z("vcovCL",{vcov:N,names:n.names,warnings:[]},e)}var Dw=["vcov","df"],Pw=["parm","level","vcov","df"];function vu(e){return typeof e=="object"&&e!==null&&!Array.isArray(e)&&Array.isArray(e.vcov)&&Array.isArray(e.names)}function Nu(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 yu=e=>new Set(e).size===e.length;function Mu(e,t){ra(e);let n=e.coefficients,r=oa(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=vu(m)?m.vcov:m;if(vu(m)){if(i=Nu(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=Nu(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&&yu(c)&&yu(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 Eu(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 xu=e=>Number.isFinite(e)&&e>0;function Vw(e){return cr(e)?e.priorWeights.filter(t=>t!==0).length:e.rank+e.dfResidual}function $w(e,t={}){B(t,Dw,"coeftest");let{est:n,se:r,names:o,warnings:s}=Mu(e,t.vcov),i=Eu(e,t.df),c=xu(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 Z("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:Vw(e),warnings:s},e,typeof t.vcov=="function"?void 0:t.vcov)}function zw(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 jw(e,t={}){B(t,Pw,"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=Mu(e,t.vcov),o=Eu(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]=xu(o)?[gt(c,o,!0,!1),c<1e-8?gt(c,o,!1,!1):gt(1-c,o,!0,!1)]:[ie(c),c<1e-8?ie(c,{lowerTail:!1}):ie(1-c)],f=(m,u,h)=>m+h*u;return Z("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:zw(c),warnings:r.warnings},e,typeof t.vcov=="function"?void 0:t.vcov)}var Ww=["test","dispersion"],Bw=(e,t)=>wt(e,t/2,2,!1,!1);function Kw(e,t,n){let{fit:r,w:o}=To(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 Uw(e,t={}){B(t,Ww,"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:Bl(t.dispersion,o),c=i?.s??0,a=Hw(s,Array.isArray(e)&&s.length>1,{...t,...i===void 0?{}:{dispersion:i.m}},c);if(o===0&&c===0)return Z("glmAnova",a,...n);let l=new Set(["Deviance","Resid. Dev","Rao","Cp"]),f=a.colNames.map(u=>l.has(u)),m=a.table.map(u=>u.map((h,p)=>f[p]?$(h,2*o):h));if(c!==0&&a.colNames.includes("Cp")){let u=a.colNames.indexOf("Cp"),h=a.colNames.indexOf("Resid. Dev"),p=a.colNames.indexOf("Resid. Df"),w=s[0].model.x.length;for(let d of m)d[u]=d[h]+2*(w-d[p])*t.dispersion}return Z("glmAnova",{...a,table:m},...n)}function Hw(e,t,n,r=0){let o=new Tt,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=>L.filter((O,I)=>A.priorWeights[I]>0&&C.priorWeights[I]>0),q=A.model.offset.map((L,O)=>L-C.model.offset[O]),F=q.some((L,O)=>L!==0&&A.priorWeights[O]>0)?A.model.x.map((L,O)=>[...L,q[O]]):A.model.x;Rs(_(F),_(C.model.x))||h.add(S)}),a==="Rao"){let T=e.map(S=>({dfRes:S.dfResidual,dev:S.deviance,fit:Ru(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=_=>[...S,...Array.from({length:_},(q,F)=>F+S.length)],C=[];for(let _=0;_<=T;_++)_===T?C.push({dfRes:M.dfResidual,dev:M.deviance,fit:Ru(M),x:R.x,intercept:R.intercept}):_>0||a==="Rao"?C.push(Kw(R,A(_),o)):C.push(void 0);l.push({name:"NULL",dfRes:M.dfNull,dev:M.nullDeviance});for(let _=1;_<=T;_++)l.push({name:`x${_}`,dfRes:C[_].dfRes,dev:C[_].dev});f=l.map((_,q)=>q===0?NaN:l[q-1].dfRes-_.dfRes),m=l.map((_,q)=>q===0?NaN:Math.max(l[q-1].dev-_.dev,0)),a==="Rao"&&(u=l.map((_,q)=>q===0?NaN:fc(C[q-1],C[q]))),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 Tt,d=n.dispersion??So(p,w);w.list.forEach(M=>o.add(M));let v=c?1/0:p.dfResidual,N=t?["Resid. Df","Resid. Dev","Df","Deviance"]:["Df","Deviance","Resid. Df","Resid. Dev"],b=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:Bw(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`),v!==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]),v,!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=b.map((M,R)=>[...M,...y.map(T=>T[R])]);return{colNames:E,rowNames:l.map(M=>M.name),table:x,warnings:o.list}}function Ru(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 Yw=["type"];function Tu(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=ea(e.weights===void 0?void 0:i.map(p=>$(e.weights[p],-o)),i.length),f=a.pivot.slice(0,a.rank),{qty:m}=Gr(f.map(p=>i.map((w,d)=>Et(l[d],e.x[w][s[p]]))),i.map((p,w)=>Et(l[w],$(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,Yw,"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=Ks([e]),i=Us([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])=>[$(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=Hs(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=[],v=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:Tu(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(_=>_!==0&&_!==R&&T.every(q=>e.factors[q][_-1]>0)),A=h(new Set(p.filter(_=>_!==R&&!S.includes(_)))),C=Tu(e,A,h(new Set([R])),s,y);w.push(e.termLabels[R-1]),x.push(C)}}return x};d=v(l),l===0&&e.weights!==void 0&&d.some(y=>!Number.isFinite(y.ss))&&(l=Hs(c),d=v(l));let N=y=>$(y,2*s+l),b=$(i,-l),E=b/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(b),o,NaN,NaN]),Z("Anova",{colNames:["Sum Sq","Df","F value","Pr(>F)"],rowNames:[...w,"Residuals"],table:g,warnings:m},e)}var sa=[0,0];var uc=e=>e.map(t=>t.map(n=>n[0]));function Su(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(sa);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=sa;return e.forEach((n,r)=>t=he(t,n[r])),t}function Au(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 ku(e){let{a:t,sign:n,singular:r}=Au(e);if(r)return sa;let o=[n,0];return t.forEach((s,i)=>o=_e(o,s[i])),o}function qu(e){let t=e.length,{a:n,perm:r,singular:o}=Au(e);if(o)throw new Error("singular matrix");let s=Array.from({length:t},()=>new Array(t).fill(sa));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 Gw=["group","idata","M","X"],Iu=(e,t)=>wt(e,t/2,2,!1,!1);function hc(e){let t=e.length,n=ft(Ir(e),t);return ku(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=Iu(i,c),l=Iu(i,c+4);return a+s*(l-a)}var Xw=(e,t)=>Array.from({length:t},(n,r)=>e.map(o=>o[r]));function Qw(e,t,n){let r=e.length,o=v=>v.length===0?0:Lt(Array.from({length:r},(N,b)=>v.map(E=>E[b]))).rank,s=n.length===0?void 0:Lt(Array.from({length:r},(v,N)=>n.map(b=>b[N]))),i=s===void 0?[]:s.pivot.slice(0,s.rank).map(v=>n[v]),c=i.length,a=[...i,...t],l=Lt(Array.from({length:r},(v,N)=>a.map(b=>b[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(v=>a[v]),m=v=>v.map(N=>[N,0]),u=new Array(r).fill([0,0]),h=Kn(f.map(m),u,e.map((v,N)=>e.map(b=>b[N]))).qtv,p=Array.from({length:r},(v,N)=>h.map(b=>b[N])),w=Kn(f.map(m),u,p).qtv,d=Array.from({length:l.rank-c},(v,N)=>c+N);return d.map(v=>d.map(N=>w[v][N]))}function Zw(e,t={}){B(t,Gw,"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=ke(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(Sn),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(()=>Kr(g,p));if(y.x.length!==r)throw new RangeError("idata must have one row per column of y");return Xw(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),v=w(t.X??"~0"),N=Qw(h,d,v),b=N.length;if(b===0)throw new RangeError("X spans M: the test has no dimensions");if(b===1)throw new RangeError("one tested dimension: sphericity holds trivially and the test has no degrees of freedom");if(u<b)throw new RangeError(`too few residual degrees of freedom for the test: ${u} for ${b} dimension(s)`);let E=hc(N);return{statistic:E,statisticName:"W",pValue:pc(E,b,u,r),method:"Mauchly's test of sphericity",warnings:[]}}var Jw=["n","d","type","sigLevel","power","alternative"],e1=["n1","n2","d","sigLevel","power","alternative"],t1=["h","n","sigLevel","power","alternative"],n1=["h","n1","n2","sigLevel","power","alternative"],r1=["h","n","sigLevel","power","alternative"],o1=["d","n","sigLevel","power","alternative"],s1=["n","r","sigLevel","power","alternative"],a1=["w","N","df","sigLevel","power"],i1=["k","n","f","sigLevel","power"],c1=["u","v","f2","sigLevel","power"],l1=["size"],f1={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 Lu(e="p",...t){B(Es(t),l1,"cohenES");let n=Ms(["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:f1[r][["small","medium","large"].indexOf(o)],method:"Conventional effect size from Cohen (1982)"}}function m1(e,t){if(!aa(e)||!aa(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 u1(e,t){if(e.length!==t.length)throw new RangeError("'P0' and 'P1' must have the same length");if(![...e,...t].every(aa))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 h1(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(aa)))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>=p1)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 p1=2**-1022,aa=e=>e>=0&&e<=1,d1="\u2018sig.level\u2019 must be numeric in [0, 1]",g1="\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(d1);if(n(t))throw new RangeError(g1)}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"?Lu(t,e).effectSize:e}var Uo=["two.sided","less","greater"];function ia(e,t){return e==="two.sided"?t:e==="less"?[-10,5]:[-5,10]}function Cu(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 _u=e=>e<1e-8?ie(e,{lowerTail:!1}):ie(1-e);function Fu(e,t,n){return n==="greater"?se(_u(t)-e,{lowerTail:!1}):n==="less"?se(ie(t)-e):se(_u(t/2)-e,{lowerTail:!1})+se(ie(t/2)-e)}function w1(e){let t=1,n=0;for(let r of e){if(r===0)return 0;let o=Math.floor(Math.log2(Math.abs(r)));t*=$(r,-o),n+=o}return $(t,n)}function b1(e={}){B(e,Jw,"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=Ae(e.type,["two.sample","one.sample","paired"]),i=Ae(e.alternative,Uo),c=s==="two.sample"?2:1;i==="two.sided"&&!ye(r)&&(r=Math.abs(r));let a=(m,u,h)=>Cu((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,...ia(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 v1(e={}){B(e,e1,"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=Ae(e.alternative,Uo);i==="two.sided"&&!ye(o)&&(o=Math.abs(o));let c=(a,l,f,m)=>Cu(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,...ia(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=Ae(t.alternative,Uo);f==="two.sided"&&!ye(a)&&(a=Math.abs(a));let m=(u,h,p)=>Fu(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,...ia(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 N1(e={}){B(e,t1,"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 y1(e={}){B(e,r1,"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 M1(e={}){B(e,o1,"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 E1(e={}){B(e,n1,"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=Ae(e.alternative,Uo);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)=>Fu(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,...ia(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 x1(e={}){B(e,s1,"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=Ae(e.alternative,Uo);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),v=se((w-d)*Math.sqrt(a-3));return m?v+se((-w-d)*Math.sqrt(a-3)):v},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 R1(e){B(e,a1,"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:xt(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 T1(e={}){B(e,i1,"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=w1([c,a,l,l])),h===1/0?1:Is(m,u)?_s(m,u,h,f):Tn(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 S1(e={}){B(e,c1,"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:Is(c,a)?_s(c,a,m,f):Tn(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 A1=["id","dv","between","within","type","correction","es","observed"],ca=e=>e===void 0?[]:typeof e=="string"?[e]:[...e],k1=1e-12,Ou=1e-24;function Du(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 Ho=e=>typeof e=="boolean"?e?"TRUE":"FALSE":vt(e);function q1(e){let t=[...new Set(e)];return t.every(n=>typeof n=="number")?t.sort((n,r)=>n-r).map(Ho):[...new Set(t.map(Ho))].sort(Sn)}var Pu=(e,t)=>t.split(":").includes(e);function Vu(e,t,n,r,o){let s=l=>r.some(f=>Pu(f,l));for(let l of r)if(!e.some(f=>Pu(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 I1(e,t){B(t,A1,"aovEz");let n=ca(t.between),r=ca(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((ne,fe)=>U.indexOf(ne)!==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=ca(t.es??"ges");for(let j of c)if(j!=="ges"&&j!=="pes")throw new RangeError(`invalid 'es': ${String(j)}`);let a=ca(t.observed),l=[],f=j=>j==null||typeof j=="number"&&Number.isNaN(j),m=Du(e,t.dv),u=[t.id,...n,...r].map(j=>Du(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(Ho),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}),v=n.map((j,U)=>Pt(p(u[1+U]))),N=r.map((j,U)=>Pt(p(u[1+n.length+U]))),b=w.length;for(let j of[d,...v,...N])if(j.length!==b)throw new RangeError("the columns must have the same length");let E=v.map(j=>q1(j)),g=v.map(j=>j.map(Ho)),y=N.map(j=>j.map(Ho)),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])),ne=R.get(j);ne===void 0?R.set(j,W):ne!==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])),_=new Set(w.map((j,U)=>A(U)));if(S.some(j=>!_.has(JSON.stringify(j))))throw new Error("Empty cells in within-subjects design (i.e., bad data structure).");let q=new Map,F=new Set;w.forEach((j,U)=>{let W=q.get(j);W===void 0&&q.set(j,W=new Set),W.add(A(U)),Number.isNaN(d[U])&&F.add(j)});let L=M.filter(j=>F.has(j)||q.get(j).size<S.length),O=new Set(L),I=ke([d.filter((j,U)=>!O.has(w[U]))]).k,D=I===0?d:d.map(j=>$(j,-I)),k=j=>$(j,2*I),V=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 ne=C.get(A(U));W.s[ne]=he(W.s[ne],[D[U],0]),W.n[ne]++}),[...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."),L.length>0){let j=[...L].sort(Sn),U=j.length>=10?`${j.slice(0,10).join(", ")}, ... [showing first 10 only]`:j.join(", ");l.push(`Missing values for ${L.length} ID(s), which were removed before analysis:
9
- ${U}`)}let X=M.filter(j=>!L.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,ne)=>ft(W,U.n[ne]))}),J={};n.forEach((j,U)=>J[j]=X.map(W=>g[U][re.get(W)]));let Q=Object.fromEntries(n.map(j=>[j,"contr.sum"])),Re=Object.fromEntries(n.map((j,U)=>[j,E[U]])),te="Rank deficient model matrix; insufficient data to estimate full model.";if(V===1)return Z("aovEz",P());return Z("aovEz",Y());function P(){let j="y";for(;n.includes(j);)j+="_";let U={...J,[j]:z.map(me=>me[0][0])},W=ot(()=>Wo(`${j} ~ ${n.length>0?n.join(" * "):"1"}`,U,{contrasts:Q,levels:Re}));if(W.rank<W.coefNames.length)throw new Error(te);if(W.dfResidual<=0)throw new RangeError(`no residual degrees of freedom: ${z.length} subject(s) for ${W.rank} parameter(s)`);let ne=ot(()=>mc(W,{type:s}));l.push(...ne.warnings.filter(me=>!l.includes(me)));let fe=W.dfResidual,Te=ne.rowNames.flatMap((me,qe)=>{if(me==="(Intercept)"||me==="Residuals")return[];let[de,Fe,xe,Pe]=ne.table[qe];return[{name:me,ss:de,numDf:Fe,errSS:W.rss,denDf:fe,f:xe,p:Pe}]}),Ce=ne.colNames.indexOf("Sum Sq"),be={anova:Vu(Te,new Map([[0,W.rss]]),c,a,k),univariate:{colNames:ne.colNames,rowNames:ne.rowNames,table:ne.table.map(me=>me.map((qe,de)=>de===Ce?k(qe):qe))},within:r,warnings:l};return Ut.set(be,{colNames:ne.colNames,rowNames:ne.rowNames,table:ne.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(()=>Kr(`~ ${n.join(" * ")}`,J,{contrasts:Q,levels:Re})):{colNames:["(Intercept)"],assign:[0],x:X.map(()=>[1]),termLabels:[],factors:[]},ne=W.colNames.length;if(j===0||Lt(W.x).rank<ne)throw new Error(te);let fe=j-ne;if(fe<=0)throw new RangeError(`no residual degrees of freedom: ${j} subject(s) for ${ne} between-subject parameter(s)`);let Te=[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]:[]),qe=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(qe),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)=>qe(ee)),de,Me);return Ne.map(Ee=>Ee.slice(ne))},Pe=Object.fromEntries(r.map((Me,Ne)=>[Me,S.map(Ee=>Ee[Ne])])),Oe=ot(()=>Kr(`~ ${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,ce)=>ee=he(ee,_e(ve,Ee[ce]))),ee})),Ue=[],Xe=[],pe=[],oe=[],we=[],ge=[],le=[],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((St,At,Rn)=>he(St,Et(At,Oe.x[Rn][je])),[0,0]))),ve=Ne.map(je=>Ne.map(We=>[Oe.x.reduce((St,At)=>St+At[je]*At[We],0),0])),ce=qu(ve),Se=et(xe(ee)),Be=qr(Se,ce),$e=Ir(Be)[0],st=NaN,mt=NaN,Ct=NaN,Ht=NaN,Yn=!1;if(Ee>=2){let je=ze[Me],We=jl(uc(ce)).map(Yt=>Yt.map(on=>[on,0])),St=uc(qr(qr(We,Se),Su(We))),At=Bt(St,{symmetric:!0,onlyValues:!0}).values,Rn=At.reduce((Yt,on)=>Math.max(Yt,on),0),$t=Math.max(Rn*k1,U*Ou),It=Ir(Be),ut=Ir(qr(Be,Be));Rn>U*Ou&&ut[0]>0&&Number.isFinite(ut[0])?(Yn=!0,st=rt(_e(It,It),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&&At.every(Yt=>Yt>$t)?(Ct=hc(qr(ce,Se)),Ht=pc(Ct,Ee,fe,V)):Yn&&l.push(`The error SSP matrix of ${je} is singular: no Mauchly test (the epsilons are still defined)`)}for(let je=0;je<Te.length;je++){let We=Te[je],St;if(s===3)St=Fe(be(Te.filter(Ft=>Ft!==We)),be([We]),ee);else if(We===0)St=Fe([],be([0]),ee);else{let Ft=me(We),en=Te.filter(H=>H!==0&&H!==We&&Ft.every(ae=>W.factors[ae][H-1]>0));St=Fe(be(Te.filter(H=>H!==We&&!en.includes(H))),be([We]),ee)}let At=Ir(qr(et(St),ce))[0],$t=(We===0?1:be([We]).length)*Ee,It=fe*Ee,ut=At/$t/($e/It),Yt=ht(ut,$t,It,!1,!1),on=Me===0?Ce[je]:We===0?ze[Me]:`${Ce[je]}:${ze[Me]}`;Ue.push([k(At),$t,k($e),It,ut,Yt]),Xe.push([At,$t,$e,It,ut,Yt]),pe.push(on),tt.set(Me,$e);let Jt={name:on,ss:At,numDf:$t,errSS:$e,denDf:It,f:ut,p:Yt};if(Yn){let Ft=Math.min(1,mt),en=ht(ut,st*$t,st*It,!1,!1),H=ht(ut,Ft*$t,Ft*It,!1,!1);oe.push([st,en,mt,H]),we.push([Ct,Ht]),ge.push(on),i==="GG"?Jt={...Jt,numDf:st*$t,denDf:st*It,p:en}:i==="HF"&&(mt>1&&(G=!0),Jt=Number.isNaN(mt)?{...Jt,numDf:st*$t,denDf:st*It,p:en}:{...Jt,numDf:Ft*$t,denDf:Ft*It,p:H})}Me===0&&We===0||le.push(Jt)}}G&&l.push("HF eps > 1 treated as 1");let ue={anova:Vu(le,tt,c,a,k),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 Zr(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 _1=["rho"],L1=new Set(["group","start","id","weights","ctype","stype","robust","rmean","confType","confInt"]),wc=e=>e==null||typeof e=="number"&&Number.isNaN(e);function C1(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=Zr(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 F1(e,t,n,r={}){B(r,_1,"survdiff");let{rho:o=0}=r;if(!Number.isFinite(o))throw new RangeError("'rho' must be a finite number");let{obs:s,labels:i}=C1(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 b of s)a[b.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 b=0;b<s.length;){let E=s[b].time,g=b,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-b,b=g}let w=f.map((b,E)=>b>0?E:-1).filter(b=>b>=0),d=0,v=Math.max(w.length-1,0);if(w.length>=2){let b=w.slice(1),E=b.map(y=>l[y]-f[y]),g=b.map(y=>b.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:v}=O1(g,E))}}let N=v===0?NaN:xt(d,v,{lowerTail:!1});return{n:a,groups:i,obs:l,exp:f,var:m,chisq:d,df:v,pvalue:N,pValue:N,warnings:[]}}function O1(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,L1,"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(k=>k.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(k=>k===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 k=0;k<e.length;k++){let V=i?.[k];if(i&&V!=null&&typeof V!="number")throw new TypeError("weights must contain numbers");if(i&&(V==null||Number.isNaN(V)))continue;let K=V??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[k]!=null&&typeof e[k]!="number")throw new TypeError("'time' must contain numbers");if(t[k]!=null&&typeof t[k]!="number")throw new TypeError("'status' must contain numbers");if(o!==void 0&&o[k]!=null&&typeof o[k]!="number")throw new TypeError("'start' must contain numbers");if(!(e[k]==null||t[k]==null||Number.isNaN(e[k])||Number.isNaN(t[k])||o!==void 0&&(o[k]==null||Number.isNaN(o[k]))||r!==void 0&&wc(r[k])||s!==void 0&&wc(s[k]))){if(o!==void 0&&!(o[k]<e[k]))throw new RangeError("stop time must be > start time");if(t[k]!==0&&t[k]!==1)throw new RangeError("'status' must be 0 (censored) or 1 (event)");if(e[k]===-1/0||e[k]===1/0&&t[k]===1)throw new RangeError("event times must be finite");d.push(k)}}if(d.length===0)throw new RangeError("no observations without missing values");let v=r===void 0?[]:Pt(r),N=s===void 0?[]:Array.from(s),b=r===void 0?["all"]:Array.from(new Set(d.map(k=>v[k]))).sort((k,V)=>typeof k=="number"&&typeof V=="number"?k-V:String(k)<String(V)?-1:String(k)>String(V)?1:0).map(k=>`group=${vt(k)}`),E=new Map;r!==void 0&&Array.from(new Set(d.map(k=>v[k]))).sort((k,V)=>typeof k=="number"&&typeof V=="number"?k-V:String(k)<String(V)?-1:String(k)>String(V)?1:0).forEach((k,V)=>E.set(k,V));let g=new Map;s!==void 0&&Array.from(new Set(d.map(k=>N[k]))).forEach((k,V)=>g.set(k,V));let y=d.flatMap(k=>o===void 0?[e[k]]:[o[k],e[k]]),x=Zr(y),M=new Map,R=new Map;d.forEach((k,V)=>{if(o===void 0)R.set(k,x[V]);else if(M.set(k,x[2*V]),R.set(k,x[2*V+1]),!(M.get(k)<R.get(k)))throw new Error("aeqSurv exception, an interval has effective length 0")});let T=d.map(k=>({time:R.get(k),start:o===void 0?-1/0:M.get(k),event:t[k]===1,group:r===void 0?0:E.get(v[k]),id:s===void 0?k:g.get(N[k]),weight:i?.[k]??1})).sort((k,V)=>k.time-V.time),S=s===void 0?void 0:new Map;if(S)for(let k of T){let V=S.get(k.id)??[];V.push(k),S.set(k.id,V)}let A=b,C=T.map(k=>k.time),_=Math.min(...C),q=Math.min(0,_);if(typeof f=="number"&&f<=q)throw new RangeError("'rmean' must be greater than the integration origin");let F=(1-u)/2,L=F<1e-8?ie(F,{lowerTail:!1}):ie(1-F),O=[],I={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&&(I.counts={nRisk:[],nEvent:[],nCensor:[]});let D=[];for(let k=0;k<A.length;k++){let V=T.filter(pe=>pe.group===k);if(!V.some(pe=>pe.weight>0))throw new Error("a group has no positive weight");let K=I.time.length,X=V.length,re=Math.max(...V.map(pe=>pe.time)),z=0,J=0,Q=1,Re=0,te=0,P=0;if(o===void 0){let pe=0;for(let oe of V)if(pe+=oe.weight,!Number.isSafeInteger(pe))throw new RangeError("total case weight per group must be a safe integer");z=pe,J=V.length}let Y=[],j=[],U=[],W=s===void 0?void 0:new Map;if(W)for(let pe of V){let oe=W.get(pe.id)??[];oe.push(pe),W.set(pe.id,oe)}for(let pe=0;pe<V.length;){let oe=V[pe].time,we=pe,ge=0,le=0,tt=0,G=J;for(;we<V.length&&V[we].time===oe;)we++;let ue=V.slice(pe,we);if(o!==void 0){let je=V.filter(We=>We.start<oe&&We.time>=oe);if(z=je.reduce((We,St)=>We+St.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,le++;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,ce=0;if(ge>0){if(c===1)ve=ge/z,ce=ge/(z*z);else for(let je=0;je<le;je++){let We=ge/le,St=z-je*We;ve+=We/St,ce+=We/(St*St)}Re+=ve,P+=ce,a===1?(Q*=1-ge/z,te+=ge<z?ge/(z*(z-ge)):1/0):Q=Math.exp(-Re),U.push({t:oe,d:ge,n:z,e:le,S:Q})}let Se=Math.sqrt(a===1?te:P),Be=0,$e=0;if(l){let je=0,We=0,St=W===void 0?V:Array.from(W.values(),At=>At[0]);for(let At of St){let Rn=W?.get(At.id)??[At],$t=0,It=0;for(let ut of U){if(ut.t>oe)break;let Yt=Rn.some(Ft=>Ft.start<ut.t&&Ft.time>=ut.t),on=Rn.some(Ft=>Ft.time===ut.t&&Ft.event),Jt=ut.d/ut.n;if(c===1)It+=(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)}It+=Ft*Number(on)+en*Number(Yt)}a===1&&Jt<1&&($t-=Q*(Number(on)-Number(Yt)*Jt)/((1-Jt)*ut.n))}je+=(At.weight*$t)**2,We+=(At.weight*It)**2}Be=Math.sqrt(je),$e=Math.sqrt(We)}let st=l?c===2?a===1?Be:$e:a===1?Q===0?NaN:Be/Q:$e:Se,mt=l?c===2?Q*(a===1?Be:$e):a===1?Be:Q*$e:Q*Se,Ct=NaN,Ht=NaN;if(w!=="none")if(w==="log")Ct=Q===0?mt===0?0:NaN:Q*Math.exp(-L*st),Ht=Q===0?mt===0?0:NaN:Math.min(1,Q*Math.exp(L*st));else if(w==="log-log")if(Q===0||Q===1)Ct=mt===0?Q:NaN,Ht=Ct;else{let je=Math.log(-Math.log(Q));Ct=Math.exp(-Math.exp(je-L*st/Math.log(Q))),Ht=Math.exp(-Math.exp(je+L*st/Math.log(Q)))}else if(w==="plain")Q===0&&mt===1/0?(Ct=NaN,Ht=NaN):(Ct=Math.max(0,Q-L*mt),Ht=Math.min(1,Q+L*mt));else if(w==="logit")if(Q===0||Q===1)Ct=mt===0?Q:NaN,Ht=Ct;else{let je=Math.log(Q/(1-Q)),We=L*mt/(Q*(1-Q));Ct=1-1/(1+Math.exp(je-We)),Ht=1-1/(1+Math.exp(je+We))}else if(Q>0&&Q<1){let je=Math.asin(Math.sqrt(Q)),We=L*mt/(2*Math.sqrt(Q*(1-Q)));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;I.strata.push(A[k]),I.time.push(oe),I.nRisk.push(Yn),I.nEvent.push(ge),I.nCensor.push(tt),I.surv.push(Q),I.cumhaz.push(Re),I.stdChaz.push(l?$e:Math.sqrt(P)),I.stdErr.push(l?c===2?Q*(a===1?Be:$e):a===1?Be:Q*$e:Q*Se),I.lower.push(Ct),I.upper.push(Ht),I.counts&&(I.counts.nRisk.push(G),I.counts.nEvent.push(Ee),I.counts.nCensor.push(ee)),o===void 0&&(z-=ge+tt,J-=we-pe),pe=we}let ne=I.time.slice(K),fe=I.surv.slice(K),Te=I.lower.slice(K),Ce=I.upper.slice(K);I.median.push([gc(ne,fe),w==="none"?NaN:gc(ne,Te),w==="none"?NaN:gc(ne,Ce)]),I.n.push(X),I.records.push(X);let be=I.nRisk.slice(K);I.nMax.push(Math.max(...be)),I.nStart.push(be[0]??0),I.events.push(V.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 qe=V.filter(pe=>Number.isFinite(pe.time)).map(pe=>Math.abs(pe.time)),de=Number.isFinite(me)?Math.max(Math.abs(me),Math.abs(q),...qe):Math.max(Math.abs(q),...qe),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-q),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([q/xe,...U.map(oe=>oe.t/xe).filter(oe=>oe>q/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([q,...U.map(oe=>oe.t).filter(oe=>oe>q&&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(le=>le.t/xe).filter(le=>le>pe.t/xe&&le<me/xe),me/xe])).sort((le,tt)=>le-tt);for(let le=0;le<ge.length-1;le++)pn(oe,Ke(ge[le])*(ge[le+1]-ge[le]),ge.length+1)}else{let ge=Array.from(new Set([pe.t,...U.map(le=>le.t).filter(le=>le>pe.t&&le<me),me])).sort((le,tt)=>le-tt);for(let le=0;le<ge.length-1;le++)pn(oe,Oe(ge[le])*((ge[le+1]-ge[le])/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);I.rmean.push(f==="none"?NaN:et),I.seRmean.push(f==="none"?NaN:Math.sqrt(Xe)*xe),D.push(f==="none"?w==="none"?[X,I.nMax[k],I.nStart[k],I.events[k],I.median[k][0]]:[X,I.nMax[k],I.nStart[k],I.events[k],I.median[k][0],I.median[k][1],I.median[k][2]]:w==="none"?[X,I.nMax[k],I.nStart[k],I.events[k],et,Math.sqrt(Xe)*xe,I.median[k][0]]:[X,I.nMax[k],I.nStart[k],I.events[k],et,Math.sqrt(Xe)*xe,I.median[k][0],I.median[k][1],I.median[k][2]])}return I.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:D},Z("survfit",I)}var D1=["tau","covariates","covariateNames","confLevel"];function Yo(e){return 2*se(-Math.abs(e))}function ju(e,t,n){return[e-n*t,e+n*t]}function $u(e,t,n){return{estimate:e,se:t,confInt:ju(e,t,n)}}function P1(e,t,n,r){return{estimate:e,confInt:ju(e,t,n),pValue:Yo(r)}}function zu(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 P1(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:Yo(c/a)}}function V1(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(_=>[_.arm,..._.x]),c=i[0].map((_,q)=>{let F=i.reduce((O,I)=>Math.max(O,Math.abs(I[q])),0);if(F===0)return 1;let L=Math.floor(Math.log2(F));return L>500||L<-500?2**L:1}),a=i.map(_=>_.map((q,F)=>q/c[F])),l=e.map((_,q)=>[1,...a[q]]),f=[1,...c],m=V1(e),u=r?Va(a,n,{family:"poisson",weights:m}):Wo(a,n,{weights:m});if(r&&!u.converged)throw new Error("the adjusted RMST Poisson model did not converge");let p=u.coefficients.map((_,q)=>_/f[q]),w=u.fittedValues;if(p.some(_=>!Number.isFinite(_))||w.some(_=>!Number.isFinite(_)))throw new Error("the adjusted RMST model is singular or has non-finite fitted values");let d=l.map((_,q)=>_.map(F=>m[q]*F*(n[q]-w[q]))),v=d.map(_=>_.slice());for(let _ of[0,1]){let q=e.flatMap((L,O)=>L.arm===_?[O]:[]),F=mr(q.map(L=>e[L].time),q.map(L=>e[L].status===0?1:0),{rmean:"none"});for(let L=0;L<F.time.length;L++){let O=F.time[L],I=F.nEvent[L];if(!(O<t)||I===0)continue;let D=F.nRisk[L];if(!(D>0))continue;let k=new Array(l[0].length).fill(0);for(let K=0;K<k.length;K++){let X=Pn();for(let re of q)e[re].time>=O&&pn(X,d[re][K],q.length);k[K]=Vn(X)}let V=I/D;for(let K of q){let X=e[K],re=X.time===O&&X.status===0,z=X.time>=O;for(let J=0;J<k.length;J++)v[K][J]+=k[J]/D*(Number(re)-(z?V:0))}}}let N=l.map((_,q)=>_.map(F=>F*Math.sqrt(r?w[q]:1))),b=Wa(N),E=Wa(v),g=nt(b),x=Ba(Ba(g,E),g).map((_,q)=>_.map((F,L)=>F/f[q]/f[L])),M=x.map((_,q)=>Math.sqrt(Math.max(0,_[q]))),R=p.map((_,q)=>_/M[q]),T=R.map(Yo),S=p.map((_,q)=>_-s*M[q]),A=p.map((_,q)=>_+s*M[q]),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 $1(e,t,n,r={}){if(B(r,D1,"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=[],v=[],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){v.push(l(e[z]));continue}if(typeof f[z]!="number")throw new TypeError("'status' must contain numbers");let Q=f[z],Re=Q===1?1:0,te=p?p[z]:[];if(te.some(Y=>!Number.isFinite(Y)))continue;d.push({time:l(e[z]),status:Re,arm:J,x:te,index:z});let P=Q;if(m.has(J))if(Q===2)P=1;else if(Q===1)P=0;else{N.includes("Invalid status value, converted to NA")||N.push("Invalid status value, converted to NA");continue}else if(P!==0&&P!==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:P,arm:J,x:te,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 b=[0,1].map(z=>Math.max(...d.filter(J=>J.arm===z).map(J=>J.time))),E=[0,1].map(z=>{let J=b[z];return d.some(Q=>Q.arm===z&&Q.time===J&&Q.status===0)}),g=E.some(Boolean)?Math.min(...b.filter((z,J)=>E[J])):Math.max(...b),y=r.tau===void 0,x=r.tau===void 0?g:l(r.tau),M=r.tau===void 0?x*a:r.tau;if(v.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?ie(R,{lowerTail:!1}):ie(1-R),S=w.filter(z=>z.arm===1),A=w.filter(z=>z.arm===0),C=zu(S,x),_=zu(A,x),q=z=>({estimate:z.estimate*a,se:z.se*a}),F={rmst:q(C.rmst),rmtl:q(C.rmtl)},L={rmst:q(_.rmst),rmtl:q(_.rmtl)},O=$u(F.rmst.estimate,F.rmst.se,T),I=$u(L.rmst.estimate,L.rmst.se,T),D={estimate:F.rmtl.estimate,se:F.rmtl.se,confInt:[M-O.confInt[1],M-O.confInt[0]]},k={estimate:L.rmtl.estimate,se:L.rmtl.se,confInt:[M-I.confInt[1],M-I.confInt[0]]},V=bc(F.rmst,L.rmst,"difference",T),K=bc(F.rmst,L.rmst,"ratio",T),X=bc(F.rmtl,L.rmtl,"rmtlRatio",T),re={tau:M,tauDefault:y,confLevel:o,arm1:{rmst:O,rmtl:D},arm0:{rmst:I,rmtl:k},difference:V,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})),Q=J.map(be=>be.time),Re=J.map(be=>x-be.time),te=vc(J,x,Q,!1,z,T),P=!(F.rmst.estimate>0&&L.rmst.estimate>0),Y=!(F.rmtl.estimate>0&&L.rmtl.estimate>0),j=P?void 0:vc(J,x,Q,!0,z,T),U=Y?void 0:vc(J,x,Re,!0,z,T),W=(be,me)=>{let qe=be.coef[1],de=Math.sqrt(Math.max(0,be.cov[1][1]));return me?{estimate:Math.exp(qe),confInt:[Math.exp(qe-T*de),Math.exp(qe+T*de)],pValue:Yo(qe/de)}:{estimate:qe*a,confInt:[(qe-T*de)*a,(qe+T*de)*a],pValue:Yo(qe/de)}},ne=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,qe)=>qe===0?me+Math.log(a):me),confInt:[be.confInt[0].map((me,qe)=>qe===0?me+Math.log(a):me),be.confInt[1].map((me,qe)=>qe===0?me+Math.log(a):me)]}),Te={names:["intercept","arm",...z],estimate:Array(te.coef.length).fill(NaN),se:Array(te.coef.length).fill(NaN),z:Array(te.coef.length).fill(NaN),pValue:Array(te.coef.length).fill(NaN),confInt:[Array(te.coef.length).fill(NaN),Array(te.coef.length).fill(NaN)]},Ce={estimate:NaN,confInt:[NaN,NaN],pValue:NaN};re.adjusted={difference:W(te,!1),ratio:P?Ce:W(j,!0),rmtlRatio:Y?Ce:W(U,!0)},re.coefficients={difference:ne(te.table),ratio:j?fe(j.table):Te,rmtlRatio:U?fe(U.table):Te},delete re.arm1,delete re.arm0}return Z("rmst2",re)}var z1=new Set(["start","id","ties","weights","strata","init","control","nocenter","confLevel","robust","cluster"]),j1=["eps","iterMax","tolerChol","tolerInf","timefix"],Nc=e=>e==null||typeof e=="number"&&Number.isNaN(e),W1=(e,t)=>typeof e=="number"&&typeof t=="number"?e-t:String(e)<String(t)?-1:String(e)>String(t)?1:0,B1=2**64,K1=2**-1022,Wu=700;function la(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,v=0,N=u(),b=h(),E=u(),g=h(),y=0,x=0,M=R=>{let T=Math.exp(x-R);v*=T,y*=T;for(let S=0;S<m;S++){N[S]*=T,E[S]*=T;let A=b[S],C=g[S];for(let _=0;_<=S;_++)A[_]*=T,C[_]*=T}x=R};for(let R=f-1;R>=0;){c[R]&&(x=0,v=0,N=u(),b=h());let T=r[R],S=!1;if(l!==void 0){let q=R;for(;q>0&&r[q-1]===T&&!c[q-1];)q--;for(let F=q;F<=R;F++)o[F]&&(S=!0)}if(l!==void 0&&S){let q=R;for(;q>0&&!c[q-1];)q--;let F=R;for(;!c[F];)F++;let L=I=>{let D=0;for(let k=0;k<m;k++)D+=t[k]*i[I][k];return D},O=-1/0;for(let I=q;I<=F;I++)l[I]<T&&r[I]>=T&&(O=Math.max(O,L(I)));x=Number.isFinite(O)?O:0,v=0,N=u(),b=h();for(let I=q;I<=F;I++){if(!(l[I]<T&&r[I]>=T)||r[I]===T&&o[I])continue;let D=Math.exp(L(I)-x)*s[I];v+=D;for(let k=0;k<m;k++){N[k]+=D*i[I][k];for(let V=0;V<=k;V++)b[k][V]+=D*i[I][k]*i[I][V]}}}let A=0,C=0;y=0;let _=R;do{let q=i[_],F=0;for(let I=0;I<m;I++)F+=t[I]*q[I];let L=s[_];Number.isFinite(F)&&(F-x>Wu?M(F):F-x<-Wu&&v===0&&y===0&&(x=F));let O=Math.exp(F-x)*L;if(o[_]){A++,C+=L,y+=O,d+=L*F;for(let I=0;I<m;I++){p[I]+=L*q[I],E[I]+=O*q[I];let D=g[I];for(let k=0;k<=I;k++)D[k]+=O*q[I]*q[k]}}else if(l===void 0){v+=O;for(let I=0;I<m;I++){N[I]+=O*q[I];let D=b[I];for(let k=0;k<=I;k++)D[k]+=O*q[I]*q[k]}}_--}while(_>=0&&r[_]===T&&!c[_]);if(A>0){let q=n?A:1,F=n?A:1,L=C/q;for(let O=0;O<q;O++){v+=n?y/F:y,d-=L*(Math.log(v)+x+a);for(let I=0;I<m;I++){N[I]+=n?E[I]/F:E[I];let D=N[I]/v;p[I]-=L*D;let k=b[I],V=g[I],K=w[I];for(let X=0;X<=I;X++){k[X]+=n?V[X]/F:V[X];let re=k[X]-D*N[X],z=L*re;K[X]+=z<1/0&&z>-1/0&&(Math.abs(z)>=K1||re===0)||!Number.isFinite(L)?z/v:L*(re/v)}}}E.fill(0);for(let O of g)O.fill(0)}R=_}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 Bu(e,t,n){let{time:r,event:o,x:s,stratumEnd:i}=e,c=ke([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,v=new Array(w.length),N=new Array(w.length),b=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 _=0;_<l;_++)y[_]+=C*s[A][_]}v[R]=g,N[R]=y.slice(),b[R]=S}let x=0,M=new Array(l).fill(0);for(let R=0;R<w.length;R++){let T=b[R];if(R>0&&T!==b[R-1]){let I=Math.exp(T-b[R-1]);x*=I;for(let D=0;D<l;D++)M[D]*=I}let S=w[R],A=d(R),C=[];for(let I=S;I<A;I++)C.push(Math.exp(p[I-u]-T));let _=0,q=0,F=0,L=new Array(l).fill(0);for(let I=S;I<A;I++){if(!o[I])continue;let D=c[I]*C[I-S];_++,q+=c[I],F+=D;for(let k=0;k<l;k++)L[k]+=D*s[I][k]}for(let I=S;I<A;I++){let D=C[I-S],k=s[I],V=f[I];for(let K=0;K<l;K++)V[K]=-D*(k[K]*x-M[K])}if(_===0)continue;let O=n?_:1;for(let I=0;I<O;I++){let D=I/O,k=v[R]-D*F,V=N[R].map((X,re)=>(X-D*L[re])/k),K=q/O/k;for(let X=S;X<A;X++){let re=C[X-S],z=s[X],J=f[X];if(o[X])for(let Q=0;Q<l;Q++){let Re=z[Q]-V[Q];J[Q]+=Re/O-(1-D)*re*Re*K}else for(let Q=0;Q<l;Q++)J[Q]-=re*(z[Q]-V[Q])*K}x+=K;for(let X=0;X<l;X++)M[X]+=K*V[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=ke([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],v=w;for(;v<p&&o[v+1]===d;)v++;let N=[],b=[],E=-1/0;for(let g=w;g<=v;g++)s[g]&&b.push(g);if(b.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(b.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 _=0;_<f;_++)y[_]+=C*i[S][_];if(s[S]&&o[S]===d){R+=a[S],x+=C;for(let _=0;_<f;_++)M[_]+=C*i[S][_]}}let T=n?b.length:1;for(let S=0;S<T;S++){let A=S/T,C=g-A*x,_=R/T/C,q=y.map((F,L)=>(F-A*M[L])/C);for(let F of N){let L=Math.exp(u[F]-E),O=s[F]&&o[F]===d;for(let I=0;I<f;I++){let D=i[F][I]-q[I];O?m[F][I]+=D/T-(1-A)*L*D*_:m[F][I]-=L*D*_}}}}w=v+1}h=p+1}return m}function _r(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 Lr(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 Jr(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 Go=(e,t)=>({statistic:e,df:t,pValue:xt(e,t,{lowerTail:!1})});function U1(e,t,n){let r=e.length,o=()=>new Array(t).fill(NaN),s=Go(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 Mc(e,t,n,r={}){return B(r,z1,"coxph"),B(wn(r,"control"),j1,"coxph","control"),Z("coxph",H1(e,t,n,r).result)}function H1(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:v=20,tolerChol:N=2**-39}=l,b=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(!(b>0&&b<1/0))throw new RangeError("'tolerInf' must be a positive finite number");if(!(Number.isInteger(v)&&v>=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 Tt,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),_=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&&!_&&x.add("cluster specified with robust=FALSE, cluster ignored");let q=0;for(let H of M)q+=t[H];let F=M.map(H=>e[H]),L=p===void 0?void 0:M.map(H=>p[H]),O,I;if(L===void 0)O=E?Zr(F):F;else if(!E)O=F,I=L;else{let H=Zr([...L,...F]);if(I=H.slice(0,R),O=H.slice(R),I.some((ae,Ie)=>ae>=O[Ie]))throw new Error("aeqSurv exception, an interval has effective length 0")}if(q===0){let H=U1(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 D=new Array(R).fill(0),k,V=[];if(c!==void 0){let H=Pt(c);V=Array.from(new Set(M.map(Ie=>H[Ie]))).sort(W1);let ae=new Map;V.forEach((Ie,Ge)=>ae.set(Ie,Ge)),k=M.map(Ie=>H[Ie]),D=M.map(Ie=>ae.get(H[Ie]))}let K=Array.from({length:R},(H,ae)=>ae).sort((H,ae)=>D[H]-D[ae]||O[H]-O[ae]||H-ae),X=K.map(H=>T[H]),{groups:re,k:z}=Nt(X)>=B1?ke([X]):{groups:[X],k:0},J=re[0],Q=0;for(let H of T)Q+=z===0?H:$(H,-z);let Re=K.map(H=>n[M[H]].slice()),te=new Array(y).fill(0),P=new Array(y).fill(1),Y=new Array(y).fill(0),j=new Set(f);for(let H=0;H<y;H++){if(Re.every(He=>j.has(He[H])))continue;let ae=Re.map(He=>He[H]),Ie=He=>{let zt=0;for(let Fn=0;Fn<R;Fn++)zt+=(p===void 0?J[Fn]:1)*He[Fn];let Cn=p===void 0?Q:R,gr=zt/Cn,Vr=0,Ta=He.map((Fn,ss)=>(Vr+=(p===void 0?J[ss]:1)*Math.abs(Fn-gr),Fn-gr)),$r=Vr===0?1:Cn/Vr;return{m:gr,s:$r,d:Ta,ok:Number.isFinite(gr)&&$r>=2**-1022&&$r<1/0}},Ge=Ie(ae);if(!Ge.ok){let{groups:He,k:zt}=ke([ae]);Ge=Ie(He[0]),Y[H]=zt}for(let He=0;He<R;He++)Re[He][H]=Ge.d[He]*Ge.s;te[H]=$(Ge.m,Y[H]),P[H]=Ge.s}let U={time:K.map(H=>O[H]),...I===void 0?{}:{start:K.map(H=>I[H])},event:K.map(H=>t[M[H]]===1),weight:J,x:Re,stratumEnd:K.map((H,ae)=>ae===R-1||D[K[ae+1]]!==D[H]),logUnit:z*Math.LN2},W=s==="efron",ne=a===void 0?new Array(y).fill(0):a.map((H,ae)=>$(H/P[ae],Y[ae])),fe=la(U,ne,W),Te=[fe.loglik,fe.loglik],Ce=fe.info.map(H=>H.slice()),be=_r(Ce,N),me=Lr(Ce,fe.score),qe=0;for(let H=0;H<y;H++)qe+=fe.score[H]*me[H];let de=0,Fe,xe=!1,Pe=v===0;if(v===0||!Number.isFinite(fe.loglik))Fe=Jr(Ce);else{let H=fe.loglik,ae=ne.map((Ge,He)=>Ge+me[He]),Ie=0;for(Pe=!1,de=1;de<=v;de++){fe=la(U,ae,W),Ce=fe.info.map(He=>He.slice()),be=_r(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,Te[1]=fe.loglik,ne=ae;break}!Ge||fe.loglik<H?(Ie++,ae=ae.map((He,zt)=>(He+Ie*ne[zt])/(Ie+1))):(Ie=0,H=fe.loglik,Te[1]=H,me=Lr(Ce,fe.score),ne=ae,ae=ae.map((He,zt)=>He+me[zt]))}Pe?Fe=Jr(Ce):(de=p===void 0?v+1:v,xe=!0,p!==void 0?(fe=la(U,ne,W),Te[1]=fe.loglik,Ce=fe.info.map(Ge=>Ge.slice()),be=_r(Ce,N),Fe=Jr(Ce)):v>1?(fe=la(U,ne,W),Te[1]=fe.loglik,Fe=fe.info.map((Ge,He)=>Ge.map((zt,Cn)=>He===Cn?Ge[He]>0?1/Ge[He]:0:-0))):Fe=Jr(Ce))}let Oe=P.map(H=>Math.floor(Math.log2(H))),Ke=P.map((H,ae)=>$(H,-Oe[ae])),ze=Oe.map((H,ae)=>H-Y[ae]),et=ne.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)=>$(H,ze[ae])),oe=Xe.map((H,ae)=>H.map((Ie,Ge)=>$(Ie,ze[ae]+ze[Ge]-z))),we=pe.slice();if(v>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),le=0;for(let H=0;H<y;H++)le+=ge[H]*te[H];let tt=M.map(H=>{let ae=0;for(let Ie=0;Ie<y;Ie++)ae+=n[H][Ie]*ge[Ie];return ae-le});if(v>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||v>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])||$(Ge,ze[Ie])>d&&Ge>b*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(_&&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?Bu(U,ne,W):yc(U,ne,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})),Cn;if(z===0)Cn=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,Cn=zt.map((pt,yt)=>{let Mt=lt(J[yt]);return pt.map(wr=>{let ol=lt(wr);return $($(J[yt],-Mt)*$(wr,-ol),Mt+ol+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},Vr=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=Vr(gr(Cn));let Ta=ke([X]).groups[0],$r=a===void 0?new Array(y).fill(0):a.map((lt,Qe)=>$(lt/P[Qe],Y[Qe])),Fn=p===void 0?Bu(U,$r,W):yc(U,$r,W),ss=gr(Ge(Fn).map((lt,Qe)=>lt.map(pt=>Ta[Qe]*pt))),vo=new Array(y).fill(0);for(let lt of ss)for(let Qe=0;Qe<y;Qe++)vo[Qe]+=lt[Qe];let Sa=Vr(ss);if(y===1)Ee=vo[0]*vo[0]/Sa[0][0];else{_r(Sa,N);let lt=Lr(Sa,vo);for(let Qe=0;Qe<y;Qe++)Ee+=vo[Qe]*lt[Qe]}let nl=new Array(R),rl=new Array(R);K.forEach((lt,Qe)=>{nl[lt]=He[Qe].map((pt,yt)=>$(pt,-ze[yt])),rl[lt]=Cn[Qe].map((pt,yt)=>$(pt,ze[yt]-Ne))}),ee={scoreResiduals:nl,dfbeta:rl}}let ve=Me!==void 0,ce=Me??Xe,Se=Me===void 0?-z:-2*Ne,Be=G.map(H=>et[H]-(a===void 0?0:$(a[H],-ze[H]))),$e=0;if(ue===1)$e=Be[0]*Be[0]/ce[G[0]][G[0]];else if(ue>1){let H=G.map(Ie=>G.map(Ge=>ce[Ie][Ge]));_r(H,N);let ae=Lr(H,Be);for(let Ie=0;Ie<ue;Ie++)$e+=Be[Ie]*ae[Ie]}$e=$($e,-Se);let st=(1-m)/2,mt=st<1e-8?ie(st,{lowerTail:!1}):ie((1+m)/2),Ct=(H,ae)=>{let Ie=Math.floor(ae/2),Ge=H.map((He,zt)=>Math.sqrt($(He[zt],ae-2*Ie)));return{unit:Ge,h:Ie,se:Ge.map((He,zt)=>$(He,ze[zt]+Ie))}},Ht=Ct(Xe,-z).se,{unit:Yn,h:je,se:We}=Ct(ce,Se),St=we.map((H,ae)=>$((Number.isNaN(H)?H:et[ae])/Yn[ae],-je)),At=St.map(H=>xt(H*H,1,{lowerTail:!1})),Rn=we.map((H,ae)=>Math.exp(H-mt*We[ae])),$t=we.map((H,ae)=>Math.exp(H+mt*We[ae])),It=$(-2*(Te[0]-Te[1]),z),ut=[$(Te[0],z),$(Te[1],z)],Yt=$(qe,z),on=Me?.map((H,ae)=>H.map((Ie,Ge)=>$(Ie,ze[ae]+ze[Ge]-2*Ne))),Jt={coefficients:we,var:on??oe,loglik:ut,score:Yt,wald:$e,iter:de,means:te,n:R,nevent:q,...S===void 0?{}:{nId:new Set(M.map(H=>S[H])).size},linearPredictors:tt,se:Ht,z:St,p:At,stdError:Ht,zValue:St,pValue:At,lower:Rn,upper:$t,logtest:Go(It,ue),sctest:Go(Yt,ue),waldtest:Go($e,ue),rsq:{rsq:1-Math.exp(-It/R),maxrsq:1-Math.exp(2*ut[0]/R)},usedRobust:ve,...ve?{naiveVar:oe,robustSe:We,rscore:Ee,robscore:Go(Ee,ue)}:{},warnings:x.list},Ft=fe.score.map((H,ae)=>$(H/P[ae],Y[ae]+z));Object.defineProperty(Jt,"coxZphData",{enumerable:!1,value:{time:O.slice(),...I===void 0?{}:{start:I.slice()},status:M.map(H=>t[H]===1),weights:T.slice(),...k===void 0?{}:{strata:k.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=>D[H]),stratumEnd:U.stratumEnd.slice(),betaScaled:ne.slice(),inverseCovariateScale:Ke.map((H,ae)=>$(1/H,-ze[ae])),weightExp:z,ties:s}});let en=ee?.scoreResiduals;if(p!==void 0&&o&&en===void 0){let H=yc(U,ne,W);en=new Array(R),K.forEach((ae,Ie)=>{en[ae]=H[Ie].map((Ge,He)=>$(Ge/Ke[He],-ze[He]))})}return{result:Jt,...ee,...en===void 0?{}:{scoreResiduals:en},first:Ft,converged:Pe}}var Y1=["method","confLevel","control"],G1=["eps","iterMax"];function Ec(e,t){let n=t.length,r=0,o=new Array(n).fill(0),s=Array.from({length:n},()=>new Array(n).fill(0));for(let{x:i,cases:c}of e){let a=i.length;if(c===0||c===a)continue;let l=i.map(d=>d.reduce((v,N,b)=>v+N*t[b],0)),f=Math.max(...l),m=new Array(c+1).fill(0),u=new Array(c+1).fill(0),h=Array.from({length:c+1},()=>new Array(n).fill(0)),p=Array.from({length:c+1},()=>Array.from({length:n},()=>new Array(n).fill(0)));m[0]=1;for(let d=0;d<a;d++){let v=i[d],N=Math.exp(l[d]-f);for(let b=Math.min(c,d+1);b>=1;b--){let E=m[b]===0?u[b-1]:Math.max(u[b],u[b-1]),g=m[b]===0?0:2**(u[b]-E),y=N*2**(u[b-1]-E),x=m[b-1],M=h[b-1],R=p[b-1],T=h[b],S=p[b];m[b]=m[b]*g+y*x;for(let A=0;A<n;A++){let C=T[A];T[A]=C*g+y*(v[A]*x+M[A]);for(let _=0;_<n;_++)S[A][_]=S[A][_]*g+y*(v[A]*v[_]*x+v[A]*M[_]+v[_]*M[A]+R[A][_])}if(u[b]=E,m[b]>2**256||m[b]>0&&m[b]<2**-256){let A=Math.floor(Math.log2(m[b])),C=2**-A;m[b]=m[b]*C;for(let _=0;_<n;_++){T[_]=T[_]*C;for(let q=0;q<n;q++)S[_][q]=S[_][q]*C}u[b]=E+A}}}let w=0;for(let d=0;d<c;d++)w+=l[d]-f;r+=w-(Math.log(m[c])+u[c]*Math.LN2);for(let d=0;d<n;d++){let v=0;for(let b=0;b<c;b++)v+=i[b][d];let N=h[c][d]/m[c];o[d]+=v-N;for(let b=0;b<n;b++)s[d][b]+=p[c][d][b]/m[c]-N*(h[c][b]/m[c])}}return{loglik:r,score:o,info:s}}function xc(e,t){return{statistic:e,df:t,pValue:xt(e,t,{lowerTail:!1})}}function X1(e,t,n,r={}){B(r,Y1,"clogit"),B(wn(r,"control"),G1,"clogit","control");let{method:o="exact",confLevel:s=.95,control:i={}}=r,{eps:c=1e-9,iterMax:a=20}=i,l=e.length;if(t.length!==l||n.length!==l)throw new RangeError("'y', 'x' and 'strata' must have the same length");if(o!=="exact"&&o!=="approximate"&&o!=="breslow")throw new RangeError(`invalid 'method': ${String(o)}`);e.forEach((D,k)=>{if(D!==0&&D!==1)throw new RangeError(`'y' must be 0 or 1 (element ${k+1})`)});let f=t[0]?.length??0;if(f===0||t.some(D=>D.length!==f||D.some(k=>!Number.isFinite(k))))throw new RangeError("'x' must have the same positive number of finite covariates in every row");let m=ie((1+s)/2),u=(D,k,V,K,X,re,z,J)=>{let Q=k.map((te,P)=>Math.sqrt(te[P])),Re=D.map((te,P)=>te/Q[P]);return{coefficients:D,var:k,loglik:V,score:K,wald:X,iter:re,n:l,nevent:e.reduce((te,P)=>te+P,0),method:z,se:Q,z:Re,p:Re.map(te=>xt(te*te,1,{lowerTail:!1})),lower:D.map((te,P)=>Math.exp(te-m*Q[P])),upper:D.map((te,P)=>Math.exp(te+m*Q[P])),logtest:xc(2*(V[1]-V[0]),f),sctest:xc(K,f),waldtest:xc(X,f),warnings:J}};if(o!=="exact"){let D=Mc(new Array(l).fill(1),e,t,{ties:"breslow",strata:n,confLevel:s,control:{eps:c,iterMax:a}});return u(D.coefficients,D.var,D.loglik,D.score,D.wald,D.iter,"breslow",D.warnings)}let h=Array.from({length:f},(D,k)=>t.reduce((V,K)=>V+K[k],0)/l),p=new Map;for(let D=0;D<l;D++){let k=p.get(n[D]);k===void 0&&(k={cases:[],controls:[]},p.set(n[D],k)),(e[D]===1?k.cases:k.controls).push(t[D].map((V,K)=>V-h[K]))}let w=[...p.values()].map(D=>({x:[...D.cases,...D.controls],cases:D.cases.length})),d=2**-39,v=D=>{let k=D.map(V=>V.slice());return{f:k,rank:_r(k,d)}},N=new Array(f).fill(0),b=Ec(w,N),E=[b.loglik,b.loglik],{f:g}=v(b.info),y=Lr(g,b.score),x=b.score.reduce((D,k,V)=>D+k*y[V],0),M=[],R=[N],T=b.loglik,S=N.map((D,k)=>D+y[k]),A=0,C=!1,_=0;for(_=1;_<=a;_++){b=Ec(w,S);let D=Number.isFinite(b.loglik)&&b.score.every(Number.isFinite);if(D&&Math.abs(1-T/b.loglik)<=c){C=!0,N=S;break}!D||b.loglik<T?(A++,S=S.map((k,V)=>(k+A*N[V])/(A+1)),R.push(N)):(A=0,T=b.loglik,{f:g}=v(b.info),y=Lr(g,b.score),N=S,R.push(N),S=S.map((k,V)=>k+y[V]))}C||(_=a,N=R[Math.max(1,a-1)]??N,M.push("Ran out of iterations and did not converge")),b=Ec(w,N),E[1]=b.loglik;let q=v(b.info),F=Jr(q.f),L=N.map((D,k)=>q.rank<f&&F[k][k]===0?NaN:D),O=L.map((D,k)=>Number.isNaN(D)?-1:k).filter(D=>D>=0),I=0;for(let D of O)for(let k of O)I+=N[D]*b.info[D][k]*N[k];return u(L,F,E,x,I,_,"exact",M)}var Ku=["transform","terms","collapseTerms","global"],Un=e=>new Array(e).fill(0),ur=e=>Array.from({length:e},()=>Un(e));function Q1(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 Uu(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 Z1(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=Uu(i,a)-Uu(s,a);l.deaths>0&&(c*=1-l.deaths/f)}return n}function J1(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=Z1(e);else if(t==="rank")o=Q1(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 eb(e,t={}){B(t,Ku,"coxZph"),B(t,Ku,"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((P,Y)=>Number.isNaN(P)?-1:Y).filter(P=>P>=0),c=i.length;if(c===0)throw new Error("the proportional-hazards test has no estimable coefficients");let a=[];for(let P=0;P<s.sortedTime.length;){let Y=P;for(;!s.stratumEnd[Y];)Y++;for(let j=P;j<=Y;){let U=s.sortedTime[j],W=j;for(;W<Y&&s.sortedTime[W+1]===U;)W++;let ne=[];for(let fe=j;fe<=W;fe++)s.sortedEvent[fe]&&ne.push(fe);s.start!==void 0&&ne.reverse();for(let fe of ne)a.push(s.sortedToInput[fe]);j=W+1}P=Y+1}if(a.length===0)throw new Error("the proportional-hazards test requires events");let l=a.map(P=>s.time[P]);if(new Set(l).size<2)throw new Error("the test needs events at two or more distinct times");let f=[...e.warnings],m=J1(s,n,a,f),u=m[a[0]];if(a.every(P=>m[P]===u))throw new Error("the transform is constant at the event times");let h=a.reduce((P,Y)=>Math.max(P,Math.abs(m[Y])),0),p=h===0?0:Math.floor(Math.log2(h)),w=m.map(P=>Number.isFinite(P)?$(P,-p):P),d=a.reduce((P,Y)=>P+w[Y],0)/a.length,v=w.map(P=>P-d);if(a.every(P=>v[P]===0))throw new Error("the transform is constant at the event times");let N=i.map(P=>s.betaScaled[P]),b=ur(c),E=ur(c),g=ur(c),y=Un(c),x=[];for(let P=0;P<s.sortedTime.length;){let Y=P;for(;!s.stratumEnd[Y];)Y++;for(let j=P;j<=Y;){let U=s.sortedTime[j],W=j;for(;W<Y&&s.sortedTime[W+1]===U;)W++;let ne=[];for(let fe=j;fe<=W;fe++)s.sortedEvent[fe]&&ne.push(fe);if(ne.length>0){let fe=[],Te=-1/0;for(let oe=P;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 le=0;le<c;le++)ge+=N[le]*s.sortedX[oe][i[le]];Te=Math.max(Te,ge)}if(fe.length===0||!Number.isFinite(Te))throw new Error("empty Cox proportional-hazards risk set");let Ce=Un(c),be=ur(c),me=Un(c),qe=ur(c),de=0,Fe=0;for(let oe of fe){let we=0;for(let le=0;le<c;le++)we+=N[le]*s.sortedX[oe][i[le]];let ge=s.sortedWeight[oe]*Math.exp(we-Te);de+=ge;for(let le=0;le<c;le++){let tt=s.sortedX[oe][i[le]];Ce[le]+=ge*tt;for(let G=0;G<c;G++)be[le][G]+=ge*tt*s.sortedX[oe][i[G]]}if(s.sortedEvent[oe]&&s.sortedTime[oe]===U){Fe+=ge;for(let le=0;le<c;le++){let tt=s.sortedX[oe][i[le]];me[le]+=ge*tt;for(let G=0;G<c;G++)qe[le][G]+=ge*tt*s.sortedX[oe][i[G]]}}}let xe=0,Pe=Un(c);for(let oe of ne){let we=s.sortedWeight[oe];xe+=we;for(let ge=0;ge<c;ge++)Pe[ge]+=we*s.sortedX[oe][i[ge]]}let Oe=ne.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,le=Ce.map((G,ue)=>(G-we*me[ue])/ge);for(let G=0;G<c;G++)ze[G]+=le[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*qe[G][ue])/ge-le[G]*le[ue])}let Ue=Pe.map((oe,we)=>oe-xe*ze[we]),Xe=s.sortedToInput[ne[0]],pe=v[Xe];for(let oe=0;oe<c;oe++){y[oe]+=pe*Ue[oe];for(let we=0;we<c;we++)b[oe][we]+=et[oe][we],E[oe][we]+=pe*et[oe][we],g[oe][we]+=pe*pe*et[oe][we]}s.start!==void 0&&ne.reverse();for(let oe of ne){let we=s.sortedToInput[oe],ge=i.map(le=>s.sortedX[oe][le]);for(let le=0;le<c;le++)ge[le]-=ze[le];x.push({input:we,time:U,stratum:s.stratumIndex[oe],g:m[we],residual:ge})}}j=W+1}P=Y+1}let M;try{M=nt(b,E)}catch{throw new Error("the proportional-hazards information is singular")}let R=ur(c);for(let P=0;P<c;P++)for(let Y=0;Y<c;Y++){let j=0;for(let U=0;U<c;U++)j+=E[U][P]*M[U][Y];R[P][Y]=g[P][Y]-j}for(let P=0;P<c;P++)for(let Y=0;Y<P;Y++){let j=(R[P][Y]+R[Y][P])/2;R[P][Y]=j,R[Y][P]=j}let T=t.terms===!1||t.terms===void 0?i.map(P=>[P]):t.terms,S=new Set,A=[],C=[];if(T.forEach((P,Y)=>{if(!Array.isArray(P)||P.length===0)throw new RangeError("each terms group must be a non-empty array");let j=[];for(let U of P){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(P=>{S.has(P)||(A.push([P]),C.push(T.length))}),A.length===0)throw new Error("the proportional-hazards test has no estimable terms");let _=[];A.forEach((P,Y)=>{if(o&&P.length>1)_.push({col:-1,group:Y,collapsed:!0});else for(let j of P)_.push({col:j,group:Y,collapsed:!1})});let q=_.length,F=Array.from({length:c},()=>Un(q)),L=[],O=[];_.forEach((P,Y)=>{let j=A[P.group],U=C[P.group];if(P.collapsed){for(let W of j){let ne=i.indexOf(W);F[ne][Y]=N[ne]}L.push(1),O.push(`g${U+1}`)}else{let W=i.indexOf(P.col);F[W][Y]=s.inverseCovariateScale[P.col],L.push(e.coefficients[P.col]),O.push(j.length>1?`g${U+1}x${P.col+1}`:`x${P.col+1}`)}});let I=Array.from({length:q},(P,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))}),D=F.map(P=>P.map((Y,j)=>$(Y,-I[j]))),k=Array.from({length:c},()=>Un(q));for(let P=0;P<c;P++)for(let Y=0;Y<q;Y++)for(let j=0;j<c;j++)k[P][Y]+=b[P][j]*D[j][Y];let V=ur(q);for(let P=0;P<q;P++)for(let Y=0;Y<q;Y++)for(let j=0;j<c;j++)V[P][Y]+=D[j][P]*k[j][Y];for(let P=0;P<q;P++)for(let Y=0;Y<P;Y++){let j=(V[P][Y]+V[Y][P])/2;V[P][Y]=j,V[Y][P]=j}let K;try{K=nt(V)}catch{throw new Error("the proportional-hazards residual variance is singular")}for(let P=0;P<q;P++)for(let Y=0;Y<P;Y++){let j=(K[P][Y]+K[Y][P])/2;K[P][Y]=j,K[Y][P]=j}let X=K.map((P,Y)=>P.map((j,U)=>a.length*Ve(j,-I[Y]-I[U]-s.weightExp))),re=x.map(P=>{let Y=Un(q);for(let j=0;j<q;j++)for(let U=0;U<c;U++)Y[j]+=D[U][j]*P.residual[U];return L.map((j,U)=>{let W=0;for(let ne=0;ne<q;ne++)W+=Y[ne]*K[ne][U];return j+a.length*Ve(W,-I[U]-s.weightExp)})}),z=[],J=[],Q=[],Re=_.map(P=>P.collapsed?A[P.group].map(Y=>i.indexOf(Y)):[i.indexOf(P.col)]);for(let P of Re){let Y=P.map(ne=>P.map(fe=>R[ne][fe])),j=P.map(ne=>y[ne]),U;try{U=nt(Y,j)}catch{throw new Error("the proportional-hazards term information is singular")}let W=$(Math.max(0,j.reduce((ne,fe,Te)=>ne+fe*U[Te],0)),s.weightExp);z.push(W),J.push(P.length),Q.push(xt(W,P.length,{lowerTail:!1}))}if(r){let P;try{P=nt(R,y)}catch{throw new Error("the global proportional-hazards information is singular")}let Y=$(Math.max(0,y.reduce((j,U,W)=>j+U*P[W],0)),s.weightExp);z.push(Y),J.push(c),Q.push(xt(Y,c,{lowerTail:!1})),O.push("GLOBAL")}let te={rowNames:O,chisq:z,df:J,p:Q,pValue:Q,x:x.map(P=>P.g),time:x.map(P=>P.time),...s.strata===void 0?{}:{strata:x.map(P=>vt(s.strata[P.input]))},y:re,var:X,transform:n,warnings:f};return Z("coxZph",te,e)}var tb=["center"],nb=2**-1022;function rb(e,t,n={}){B(n,tb,"leveneTest");let r=Ae(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"?Ts(l):it(l);return l.map(m=>Math.abs(m-f))},i=ke(o).groups.map(s),c=(l,f)=>l.some(m=>m>0&&m<nb)||l.every(m=>m===0)&&o[f].some(m=>m!==o[f][0]);if(i.some(c)){let l=o.map(u=>{let h=ke([u]);return{d:s(h.groups[0]),k:h.k}}),f=Math.max(...l.map(u=>u.d.some(h=>h>0)?Math.floor(Math.log2(Math.max(...u.d)))+u.k:-1/0)),m=Number.isFinite(f)?f:0;i=l.map(u=>u.d.map(h=>$(h,u.k-m)))}let a=su(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 ob=["score"],sb=["alternative","exact","variant","levels"];function ab(e,t,...n){B(Es(n),ob,"propTrendTest");let{score:r}=Ms(["score"],n),o=Array.from(e),s=Array.from(t),i=o.length,c=r===void 0?o.map((N,b)=>b+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,b)=>!(N>=0&&N<=s[b])))throw new RangeError("elements of 'x' must be between 0 and 'n'");if(o.every(N=>N===0)||o.every((N,b)=>N===s[b]))throw new RangeError("all proportions are 0 or all are 1: the test for trend is not defined");let a=s.reduce((N,b)=>N+b,0),l=o.reduce((N,b)=>N+b,0)/a,f=s.map(N=>N/(l*(1-l))),m=o.map((N,b)=>N/s[b]),u=c.flatMap((N,b)=>Number.isNaN(N)?[]:[b]);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}=Rc(f,m,h,u);if(!(d>=2**-1022&&d<1/0&&Math.abs(w)<1/0)){let N=ke([u.map(b=>c[b])]);N.k!==0&&(h=c.slice(),u.forEach((b,E)=>h[b]=N.groups[0][E]),{sw:p,sxy:w,sxx:d}=Rc(f,m,h,u))}let v=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,b],k:E}=ke([o,s]),g=N.reduce((x,M)=>x+M,0)/b.reduce((x,M)=>x+M,0),y=Rc(b,m,h,u);v=$(y.sxy/g*(y.sxy/y.sxx/(1-g)),E)}return{method:"Chi-squared Test for Trend in Proportions",statistic:v,statisticName:"X-squared",parameter:1,parameterName:"df",pValue:wt(v,.5,2,!1,!1),warnings:[]}}function Rc(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 ib=2**24,cb=2**32,lb=`Sample size > 100 or data with ties
10
- p-value based on normal approximation. Specify nperm for permutation p-value`,fb=`Sample data with ties
11
- p-value based on normal approximation. Specify nperm for permutation p-value.`;function mb(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(Sn)}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 ub(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 hb(e,t,n={}){B(n,sb,"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=mb(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,b)=>{m-=N.length,f.push([N.length,m]);for(let E=b+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(fb))):h||u.push(lb);let p,w;if(h){let N=1,b=0;for(let[g,y]of f)g===0||y===0||(b+=N*(g*y+1),N+=g*y);if(N>ib||b>cb)throw new RangeError(`the exact distribution of JT has ${N} values and needs ${b} 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)=>vl(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,b=a?ub(s.map(g=>g.length),i):f.reduce((g,[y,x])=>g+y*x*(y+x+1)/12,0),E=(l-N)/Math.sqrt(b);p=se(E),w=1-p,w<1e-8&&(w=se(E,{lowerTail:!1}))}let v=r==="increasing"?w:r==="decreasing"?p:Math.min(1,2*Math.min(w,p,o==="clinfun"?.5:1));return Z("jonckheereTest",{method:"Jonckheere-Terpstra test",alternative:r,statistic:l,statisticName:"JT",pValue:v,warnings:u})}var pb=["nClasses","adjust"];function Xo(e){return Dt(Array.from(e)).sort((t,n)=>t-n)}function Hu(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){Hu(e);let t=fs(e);if(t.some(Number.isNaN))throw new RangeError("all 'x' values are identical");return t}function db(e){let t=Xo(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 gb(e){let t=Xo(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")),Z("cvmTest",{method:"Cramer-von Mises normality test",statistic:o,statisticName:"W",pValue:c,warnings:i})}function wb(e){let t=Xo(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 bb(e){let t=Xo(e),n=t.length;if(n<5||n>5e3)throw new RangeError("sample size must be between 5 and 5000");if(Hu(t),t[0]===t[n-1])throw new RangeError("all 'x' values are identical");let r=t.map((m,u)=>ie((u+1-3/8)/(n+1/4))),o=ke([t]).groups[0],s=Math.min(pl(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 vb(e,t={}){B(t,pb,"pearsonTest");let n=Xo(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 Nb=["weight"],yb=["exact"],Mb=["model","type","unit","r0","confLevel"];function Qo(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 Gu=e=>2*se(-Math.abs(e));function Eb(e,t={}){B(t,Nb,"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=>$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,v=w((x,M)=>h[x]*p[M]*u[x][M]),N=(d-v)/(1-v),b=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]-(b[x]+E[M]))**2)-v*v)/(o*(1-v)**2),y=N/Math.sqrt(g);return{value:N,statistic:y,pValue:Gu(y),warnings:[]}}function xb(e,t={}){B(t,yb,"kappamFleiss");let n=t.exact??!1,r=Qo(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 v=De(p.map(g=>g*(1-g))),N=De(p.map(g=>g*(1-g)*(1-g-g))),b=2/(v*v*o*s*(s-1))*(v*v-N),E=d/Math.sqrt(b);return{value:d,statistic:E,pValue:Gu(E),warnings:[]}}function Rb(e,t={}){B(t,Mb,"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),Qo(e,"ratings"),ma(e,"ratings");let c=ke(e).groups,a=Qo(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(D=>it(D)))*f,h=at(a.map(D=>it(D)))*l,p=it(c.map(D=>at(D))),w=at(c.flat())*(l*f-1),d=w-u*(l-1)-h*(f-1),v=d>2**-20*w?d/((l-1)*(f-1)):kb(c)/((l-1)*(f-1)),N=(D,k,V)=>ht(D,k,V,!1,!1),b=(D,k)=>m/2<1e-8?un(m/2,D,k,!1,!1):un(1-m/2,D,k,!0,!1),E=l-1;if(n==="oneway"||r==="consistency"){let D=n==="oneway"?p:v,k=n==="oneway"?l*(f-1):(l-1)*(f-1),V=u/D,K=V/b(E,k),X=V*b(k,E),[re,z,J]=o==="single"?[(u-D)/(u+(f-1)*D),V*(1-s)/(1+(f-1)*s),Q=>(Q-1)/(Q+f-1)]:[(u-D)/u,V*(1-s),Q=>1-1/Q];return{value:re,Fvalue:z,fValue:z,df1:E,df2:k,pValue:N(z,E,k),lbound:J(K),ubound:J(X),warnings:[]}}let g=o==="single"?f:1,y=D=>[g*D/(l*(1-D)),1+g*D*(l-1)/(l*(1-D))],x=(D,k)=>dl([D*h,k*v],[f-1,(l-1)*(f-1)]),M=o==="single"?(u-v)/(u+(f-1)*v+f/l*(h-v)):(u-v)/(u+(h-v)/l),[R,T]=y(s),S=u/(R*h+T*v),A=x(R,T),[C,_]=[f*M/(l*(1-M)),1+f*M*(l-1)/(l*(1-M))],q=x(C,_),F=b(E,q),L=b(q,E),[O,I]=o==="single"?[l*(u-F*v)/(F*(f*h+(f*l-f-l)*v)+l*u),l*(L*u-v)/(f*h+(f*l-f-l)*v+l*L*u)]:[l*(u-F*v)/(F*(h-v)+l*u),l*(L*u-v)/(h-v+l*L*u)];return{value:M,Fvalue:S,fValue:S,df1:E,df2:A,pValue:N(S,E,A),lbound:O,ubound:I,warnings:[]}}function Yu(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 Tb(e){let t=Qo(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=As(ke(e).groups),r=Ao(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=ke(e).groups;i=o.map(l=>{let f=a.map(h=>h[l]),m=Br(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 Z("cronbachAlpha",{rawAlpha:Yu(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=>Yu(n,o.filter(a=>a!==c))),warnings:Sb(e,r)})}function Sb(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
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),g=0;for(let x=0;x<i;x++)g+=p[x];let d=i+u,v=d*(d+1)/2-g,N=i*u+i*(i+1)/2-g,b=i*u+u*(u+1)/2-v,E=0;for(let x of bn(h))E+=x*x*x-x;let w=i*u/(d*(d-1))*((d*d*d-d)/12-E/12),y=(Math.min(N,b)-i*u/2)/Math.sqrt(w);return g<v?-y:y}),l=c.map(m=>s.n[m]/(s.n[m]+i)),f=Rm(a,l,r,o);return J("manyOneUTest",$i(s,`Wilcoxon, Mann, Whittney U-test
7
+ for multiple comparisons with one control`,a,f,o))}function vg(e,t){let n=kn(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=vt(l),m=0;for(let v=0;v<c;v++)m+=f[v];let u=m-c*(c+1)/2,h=Math.min(u,c*a-u),p=c+a,g=0;for(let v of bn(l))g+=v*v*v-v;let d=c*a/(p*(p-1))*((p*p*p-p)/12-g/12);return Math.SQRT2*(h-c*a/2)/Math.sqrt(d)}),o=r.map(s=>s.map(i=>Number.isNaN(i)?NaN:zi(i,n.k,1/0)));return J("dscfAllPairsTest",Ko(n,"Dwass-Steele-Critchlow-Fligner all-pairs test",r,o,"single-step"))}function Ng(e,t,n={}){B(n,hg,"tamhaneT2Test");let r=(()=>{if(t!==void 0)return kn(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=Tt(r.groups),s=Tt(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?La([s.variance[f],s.variance[m]],[r.n[f],r.n[m]]):r.n[f]+r.n[m]-2,p=2*Bt(Math.abs(u),h,{lowerTail:!1});return p>=1?1:-Math.expm1(c*Math.log1p(-p))});return J("tamhaneT2Test",Ko(r,i?"Tamhane's T2-test for unequal variances":"Tamhane's T2'-test for unequal variances",a,l,i?"T2 (Sidak)":"T2' (Sidak)"))}function yg(e,t){let n=kn(e,t),r=Tt(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=La([r.variance[a],r.variance[l]],[n.n[a],n.n[l]]);return zi(i[a-1][l],n.k,f)});return J("gamesHowellTest",Ko(n,"Games-Howell test",i,c,"none"))}function Mg(e,t){let n=kn(e,t),{m:r,s2:o,df:s}=ji(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:zi(l,n.k,s)));return J("tukeyTest",Ko(n,"Tukey's test",i,c,"single-step"))}function Eg(e,t,n={}){B(n,mg,"dunnTest");let r=n.method??"none",o=n.altp??!1,s=kn(e,t),i=Qs(e,t).statistic,{R:c,C:a}=Vi(s),l=s.N*(s.N+1)/12-a,f=[],m=[];for(let p=1;p<s.k;p++)for(let g=0;g<p;g++)f.push((c[g]-c[p])/Math.sqrt(l*(1/s.n[g]+1/s.n[p]))),m.push(`${s.labels[g]} - ${s.labels[p]}`);let u=f.map(p=>(o?2:1)*Zs(Math.abs(p))),h=xg(u,r);return{chi2:i,Z:f,P:u,PAdjusted:h,zValue:f,pValue:u,pAdjusted:h,comparisons:m,warnings:[]}}function xg(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 Rg(e,t,n={}){B(n,ug,"glhtDunnett");let r=Je(n.alternative),o=kn(e,t),{m:s,s2:i,df:c,k:a}=ji(o),l=Math.sqrt(i),f=o.n.slice(1).map((N,b)=>b+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,b)=>N/u[b]),p=m.map(N=>$(N,a)),g=u.map(N=>$(N,a)),d=f.map(N=>o.n[N]/(o.n[N]+o.n[0])),v=h.map(N=>r==="two.sided"?Ar(N,d,c,!0):Ar(r==="greater"?N:-N,d,c,!1));return{names:f.map(N=>`${o.labels[N]} - ${o.labels[0]}`),estimate:p,se:g,t:h,p:v,stdError:g,tValue:h,pValue:v,df:c,warnings:[]}}var Tm=["varEqual"],Tg=2**-1022;function Sm(e,t,n={}){if(typeof e!="string"&&ct(t)&&B(t,Tm,"onewayTest"),B(n,Tm,"onewayTest"),typeof e=="string"){let d=Ll(e,t);return Sm(d.values,d.groups,n)}let r;t!==void 0&&!Array.isArray(t)?n=t:r=t;let o=Nr(e,r),s=Tt(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,v)=>d+v,0),h,p,g;if(c){let d=s.grandMean,v=0,N=0;for(let b=0;b<a;b++)v+=l[b]*(f[b]-d)**2,N+=(l[b]-1)*m[b];p=u-a,Bo(p,s.ssWithin),h=v/(a-1)/(N/p)}else if(m.some(d=>!(d>=Tg))&&(g=Sg(o))!==void 0){let d=g.map((M,R)=>Am(l[R]/M.variance,-2*M.k)),v=Math.max(...d.map(M=>M.e)),N=d.map(M=>Ve(M.m,M.e-v)),b=N.reduce((M,R)=>M+R,0),E=0;for(let M=0;M<a;M++){let R=N.reduce((T,A,k)=>k===M?T:T+A,0);E+=(R/b)**2/(l[M]-1)}E/=a*a-1;let w=[];for(let M=0;M<a;M++)for(let R=M+1;R<a;R++){let[T,A]=g[M].k>=g[R].k?[g[M],g[R]]:[g[R],g[M]],k=T.mean-Ve(A.mean,A.k-T.k);if(k===0)continue;let C=Am(k,T.k);w.push({m:d[M].m*d[R].m*C.m*C.m/b,e:d[M].e+d[R].e+2*C.e-v})}let y=Math.max(...w.map(M=>M.e)),x=w.reduce((M,R)=>M+Ve(R.m,R.e-y),0);h=(w.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,w)=>E===0?[w]:[]);if(d.length>1)throw new RangeError("Welch's test is not defined with two or more constant groups");let v=d[0],N=0,b=0;for(let E=0;E<a;E++)E!==v&&(N+=1/(l[E]-1),b+=l[E]/m[E]*(f[E]-f[v])**2);N/=a*a-1,h=b/((a-1)*(1+2*(a-2)*N)),p=1/(3*N)}else{let d=l.map((w,y)=>w/m[y]),v=d.reduce((w,y)=>w+y,0),N=0,b=0;for(let w=0;w<a;w++)N+=(1-d[w]/v)**2/(l[w]-1),b+=d[w]*f[w];N/=a*a-1,b/=v;let E=0;for(let w=0;w<a;w++)E+=d[w]*(f[w]-b)**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 Ag(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=wm(t);return{method:"Shapiro-Wilk normality test",statistic:1-r,statisticName:"W",pValue:bm(r,n),warnings:[]}}function Sg(e){let t=[];for(let n of e){let r=Tt([n]),o=r.variance[0];if(!(o>0))return;t.push({mean:r.mean[0],variance:o,k:r.k})}return t}function Am(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 Ki(e,t,n){let r=[0,0];for(let o=Math.max(0,t);o<Math.min(n,e.length);o++)r=pe(r,[e[o],0]);return r[0]}function kg([[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 qg(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 Js=class{lo;hi;logNull;constructor(t){let n=0,r=new Float64Array([1]);for(let o of t){let s=kg(o);n+=s.lo,r=qg(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=Ki(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=pe(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?Ki(o,s,o.length):Ki(o,0,s+1)}};function Bi(e,t){return rr(e,t)}function km(e,t){if(t===e.lo)return 0;if(t===e.hi)return 1/0;let n=e.mean(1);return n===t?1:Bi(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:Bi(o=>n-e.cdf(t,o,!1),r<n)}function Ui(e,t,n){if(t===e.lo)return 0;let r=e.cdf(t,1,!0);return r===n?1:Bi(o=>e.cdf(t,o,!0)-n,r>n)}var Ig=["alternative","correct","exact","confLevel"];function _g(e,t={}){B(t,Ig,"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(Hi);if(s.some(f=>f<2))throw new RangeError("sample size in each stratum must be > 1");let i=Je(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?Fg(f,i,l):Lg(f,s,i,c,l)}return Og(e,s,r,o)}function Hi(e){return e.reduce((t,n)=>t+n.reduce((r,o)=>r+o,0),0)}function _m(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 Lm(e,t){return e.map(n=>n.map(r=>r.map(o=>$(o,-t))))}function qm(e,t,n){let r=0,o=0,s=0,i=0,c=0,a=0,l=0,f=0;return e.forEach(([[m,u],[h,p]],g)=>{let d=t[g],v=m+u,N=h+p,b=m+h,E=u+p;r+=m;let w=v*b/d;o+=Number.isFinite(w)?w:v/d*b;let y=v*N*b*E/(d*d*(d-n));s+=Number.isFinite(y)?y:v/d*(N/d)*(b/(d-n))*E;let x=(m+p)/d,M=(u+h)/d,R=m*p/d,T=u*h/d,A=Number.isFinite(R)?R:m/d*p,k=Number.isFinite(T)?T:u/d*h;i+=A,c+=k,a+=x*A,l+=x*k+M*A,f+=M*k}),{sa:r,se:o,sv:s,sR:i,sS:c,sPR:a,sPSQR:l,sQS:f}}function Lg(e,t,n,r,o){let s=qm(e,t,1),i=0;if(!t.every(Number.isFinite)||!Object.values(s).every(Number.isFinite)){i=_m(e);let x=Lm(e,i);s=qm(x,x.map(Hi),$(1,-i))}let{sa:c,se:a,sv:l,sR:f,sS:m,sPR:u,sPSQR:h,sQS:p}=s,g=c-a,d=$(.5,-i),v=r&&Math.abs(g)>=d?d:0,N=$(Vt(Math.abs(g)-v,l),i),b=n==="two.sided"?bt(N,.5,2,!1,!1):ae(Math.sign(g)*Math.sqrt(N),{lowerTail:n==="less"}),E=f/m,w=$(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(ie(o)*w)];else if(n==="greater")y=[E*Math.exp(ie(o,{lowerTail:!1})*w),1/0];else{let x=(1-o)/2,M=x<1e-8?ie(x,{lowerTail:!1}):ie((1+o)/2);y=[E*Math.exp(-M*w),E*Math.exp(M*w)]}return{method:`Mantel-Haenszel chi-squared test ${v!==0?"with":"without"} continuity correction`,alternative:n,statistic:N,statisticName:"Mantel-Haenszel X-squared",parameter:1,parameterName:"df",pValue:b,confInt:y,confLevel:o,estimate:E,estimateNames:"common odds ratio",nullValue:1,nullValueName:"common odds ratio",warnings:[]}}var Cg=2**24;function Fg(e,t,n){let r=e.reduce((a,[[l,f],[m,u]])=>a+Math.min(l,u)+Math.min(f,m),1);if(r>Cg)throw new RangeError(`the exact distribution of S has ${r} values, more than 2^24: use exact: false`);let o=new Js(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=pe(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=[Ui(o,s,1-n),1/0];else{let a=(1-n)/2;c=[Ui(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:km(o,s),estimateNames:"common odds ratio",nullValue:1,nullValueName:"common odds ratio",warnings:[]}}function Og(e,t,n,r){let{diff:o,V:s}=Im(e,t,n,r,1),i=0;if(!t.every(Number.isFinite)||!o.every(Number.isFinite)||!s.every(f=>f.every(Number.isFinite))){i=_m(e);let f=Lm(e,i);({diff:o,V:s}=Im(f,f.map(Hi),n,r,$(1,-i)))}let c=o.length,a=rt(s,o),l=0;for(let f=0;f<c;f++)l+=o[f]*a[f];return l=$(l,i),{method:"Cochran-Mantel-Haenszel test",statistic:l,statisticName:"Cochran-Mantel-Haenszel M^2",parameter:c,parameterName:"df",pValue:bt(l,c/2,2,!1,!1),warnings:[]}}var Dg=2**-1022;function Im(e,t,n,r,o){let s=(n-1)*(r-1),i=new Array(s).fill(0),c=Array.from({length:s},()=>new Array(s).fill(0));return e.forEach((a,l)=>{let f=t[l],m=a.map(p=>p.reduce((g,d)=>g+d,0)),u=Array.from({length:r},(p,g)=>a.reduce((d,v)=>d+v[g],0)),h=1/(f*f*(f-o));for(let p=0;p<r-1;p++)for(let g=0;g<n-1;g++){let d=g+(n-1)*p,v=m[g]*u[p]/f;i[d]+=a[g][p]-(Number.isFinite(v)?v:gs(m[g],u[p],f));for(let N=0;N<r-1;N++)for(let b=0;b<n-1;b++){let E=(g===b?f*m[g]:0)-m[g]*m[b],w=(p===N?f*u[p]:0)-u[p]*u[N],y=h*E*w;c[d][b+(n-1)*N]+=Number.isFinite(y)&&h>=Dg?y:m[g]*((g===b?1:0)-m[b]/f)*(((p===N?1:0)-u[N]/f)*(u[p]/(f-o)))}}}),{diff:i,V:c}}var Cm=new Map,Pg=2**24,Vg=2**32;function Om(e,t){let n=e+t,r=(e+1)*(Math.floor((n+1)**2/4)+1);if(2*r>Pg||n*r>Vg)throw new RangeError(`the exact distribution for m = ${e} and n = ${t} is too large to compute: use exact: false`)}function $g(e,t){let n=`${e},${t}`,r=Cm.get(n);if(r!==void 0)return r;let o=e+t,s=[];for(let u=1;u<=o;u++)s.push(Math.min(u,o+1-u));let i=s.reduce((u,h)=>u+h,0),c=Array.from({length:e+1},()=>new Float64Array(i+1));c[0][0]=1;for(let u=0;u<o;u++){let h=o-u,p=s[u],g=Array.from({length:e+1},()=>new Float64Array(i+1));for(let d=0;d<=e;d++){let v=c[d],N=e-d;if(N>h)continue;let b=N/h;for(let E=0;E<=i;E++){let w=v[E];w!==0&&(N>0&&(g[d+1][E+p]+=w*b),N<h&&(g[d][E]+=w*(1-b)))}}c=g}let a=c[e],l=0;for(;l<a.length&&a[l]===0;)l++;let f=a.length-1;for(;f>l&&a[f]===0;)f--;let m={min:l,probs:a.slice(l,f+1)};return Cm.set(n,m),m}function Yi(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}=$g(t,n),i=ml(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 Fm=new Map;function zg(e,t){let n=`${e},${t}`,r=Fm.get(n);if(r!==void 0)return r;let o=e+t,s=0;for(let m=1;m<=o;m++)s+=Math.min(m,o+1-m);let i=Array.from({length:e+1},()=>new Float64Array(s+1));i[0][0]=1;for(let m=0;m<o;m++){let u=Math.min(m+1,o-m),h=Array.from({length:e+1},()=>new Float64Array(s+1));for(let p=0;p<=e;p++){if(e-p>o-m)continue;let g=i[p];for(let d=0;d<=s;d++){let v=g[d];v!==0&&(p<e&&(h[p+1][d+u]+=v),e-p<o-m&&(h[p][d]+=v))}}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 Fm.set(n,f),f}function ea(e,t,n){let{min:r,counts:o}=zg(t,n);if(e<=0)return r;if(e>=1)return r+o.length-1;let s=wl(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 jg=["alternative","exact","confInt","confLevel"];function $m(e,t,n={}){if(B(n,jg,"ansariTest"),typeof e=="string"){let w=tr(e,t);return $m(w.x,w.y,n)}let r=Array.from(e).filter(w=>!Number.isNaN(w)),o=Array.from(t).filter(w=>!Number.isNaN(w)),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=Je(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(w=>Math.min(w,f-w+1)),h=0;for(let w=0;w<s;w++)h+=u[w];let p=new Set(m).size<f,g=n.exact??(s<50&&i<50&&!p),d=n.exact??(s<50&&i<50),v=new At,N,b;if(g&&!p){Om(s,i);let w=x=>Yi(x-1,s,i,!1),y=x=>Yi(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?w(h):y(h)))}else c==="less"?N=w(h):N=y(h);a&&(b=Ug(r,o,c,l,v))}else{let w=zm(h,u,p,s,i);N=c==="two.sided"?2*Math.min(ae(w),ae(w,{lowerTail:!1})):c==="less"?ae(w,{lowerTail:!1}):ae(w),a&&(b=Hg(r,o,c,l,d?new At:v)),d&&p&&(v.add("cannot compute exact p-value with ties"),a&&v.add("cannot compute exact confidence interval with ties"))}let E={method:"Ansari-Bradley test",alternative:c,statistic:h,statisticName:"AB",pValue:N,warnings:v.list};return b!==void 0&&(E.confInt=b.confInt,E.confLevel=l,E.estimate=b.estimate,E.estimateNames="ratio of scales"),E.nullValue=1,E.nullValueName="ratio of scales",J("ansariTest",E)}function zm(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 jm(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))?Bg(e.map(f=>Dm(f,n)).concat(t.map(f=>Dm(f,1)))):Wg(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 Dm(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]=Pm(Math.abs(e)),[s,i]=Pm(t),c=o/i,a=r-s;return c<1&&(c*=2,a-=1),[n,a,c]}function Pm(e){let t=Math.floor(Math.log2(e)),n=$(e,-t);return n>=2?(n/=2,t+=1):n<1&&(n*=2,t-=1),[t,n]}function Vm(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 Bg(e){let t=e.map((r,o)=>o).sort((r,o)=>Vm(e[r],e[o])),n=new Array(e.length);for(let r=0;r<t.length;){let o=r+1;for(;o<t.length&&Vm(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 Wg(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 ta(e,t,n){return e==="two.sided"?n(t):e==="greater"?[n(2*t)[0],1/0]:[0,n(2*t)[1]]}function Ug(e,t,n,r,o){let s=e.length,i=t.length,c=[];for(let v of e)for(let N of t)v/N>=0&&c.push(v/N);c.sort((v,N)=>v-N);let a=c.length;if(a===0)return o.add("cannot compute confidence set, returning NA"),{confInt:ta(n,1-r,()=>[NaN,NaN]),estimate:NaN};let l=c.map((v,N)=>jm(e,t,v+(N<a-1?(c[N+1]-v)/2:v*1.01)).ab),f=(v,N,b)=>{let E=1/0;for(let y of l)(N?y-v>0:y-v>=0)&&y-v<E&&(E=y-v);let w=b?-1/0:1/0;return l.forEach((y,x)=>{y-v===E&&(w=b?Math.max(w,c[x]):Math.min(w,c[x]))}),w},m=v=>{let N=ea(v/2,s,i),b=ea(1-v/2,s,i);if(!l.some(y=>y-N>=0)||!l.some(y=>y-b>0))return o.add("samples differ in location: cannot compute confidence set, returning NA"),[NaN,NaN];let E=f(N,!1,!1),w=f(b,!0,!0);return E>w?[f(b,!1,!1),f(N,!0,!0)]:[E,w]},u=ta(n,1-r,m),h=ea(.5,s,i),p=-1/0,g=1/0;l.forEach((v,N)=>{v-h<=0?p=Math.max(p,c[N]):g=Math.min(g,c[N])});let d=p===-1/0||g===1/0?NaN:(g+p)/2;return d===1/0&&(d=g/2+p/2),{confInt:u,estimate:d}}function Hg(e,t,n,r,o){let s=e.length,i=t.length,c=(v,N)=>{let{ab:b,a:E,ties:w}=jm(e,t,v);return zm(b,E,w,s,i)-N},a=[],l=(v,N)=>{v.length===0||N.length===0||a.push(cn(v)/yt(N),yt(v)/cn(N))};if(l(e.filter(v=>v>0),t.filter(v=>v>0)),l(e.filter(v=>v<=0),t.filter(v=>v<0)),a.some(v=>!Number.isFinite(v))){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:ta(n,1-r,()=>[NaN,NaN]),estimate:NaN};let f=cn(a),m=yt(a),u=v=>Ht(N=>c(N,v),f,m,{tol:1e-4}),h=v=>{let N=ie(v/2),b=ie(v/2,{lowerTail:!1});if(c(f,N)>0||c(m,b)<0)return o.add("samples differ in location: cannot compute confidence set, returning NA"),[NaN,NaN];let E=u(N),w=u(b);return E<=w?[E,w]:[w,E]},p=ta(n,1-r,h),g=c(f,0)>0||c(m,0)<0;g&&o.add("cannot compute estimate, returning NA");let d=g?NaN:u(0);return{confInt:p,estimate:d}}var Yg=["alternative","confLevel"];function Gg(e,t,n={}){B(n,Yg,"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=Je(n.alternative),a=n.confLevel??.95;ln(a);let l=vt(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),g=(M,R,T)=>{let A=(M.length+1)/2,k=0;for(let C=0;C<M.length;C++)k+=(M[C]-R[C]-T+A)**2;return k/(M.length-1)},d=g(f,vt(r),h),v=g(m,vt(o),p),N=(p-(i+1)/2)/s,b=s*d+i*v,E=s*i*(p-h)/(s+i)/Math.sqrt(b),w=b**2/((s*d)**2/(s-1)+(i*v)**2/(i-1)),y;c==="greater"?y=gt(E,w,!0,!1):c==="less"?y=gt(E,w,!1,!1):y=2*gt(-Math.abs(E),w,!0,!1);let x=wt((1-a)/2,w,!1,!1)*Math.sqrt(d/(s*i**2)+v/(i*s**2));return{method:"Brunner-Munzel Test",alternative:c,statistic:E,statisticName:"Brunner-Munzel Test Statistic",parameter:w,parameterName:"df",pValue:y,confInt:[N-x,N+x],confLevel:a,estimate:N,estimateNames:"P(X<Y)+.5*P(X=Y)",warnings:[]}}var Xg=[1.2337005501361697,7831619385924639e-32];function Qg(e,t,n){let r=(f,m)=>{let u=new Float64Array(t*t);for(let h=0;h<t;h++)for(let p=0;p<t;p++){let g=f[h*t+p];if(g!==0)for(let d=0;d<t;d++)u[h*t+d]+=g*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 Zg=2**33;function Bm(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>Zg)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 Wm(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 g=1;g<=e;g++)h*=g/e*u,h<2**-500&&(h*=2**500,p-=500);for(;p<-1e3;)h*=2**-1e3,p+=1e3;return h*2**p}let n=Math.floor(e*t)+1,r=2*n-1,o=n-e*t,s=new Float64Array(r*r);for(let f=0;f<r;f++)for(let m=0;m<r;m++)s[f*r+m]=f-m+1>=0?1:0;for(let f=0;f<r;f++)s[f*r]-=Math.pow(o,f+1),s[(r-1)*r+f]-=Math.pow(o,r-f);2*o-1>0&&(s[(r-1)*r]+=Math.pow(2*o-1,r));for(let f=0;f<r;f++)for(let m=0;m<r;m++){let u=f-m+1;if(u>0){let h=1;for(let p=2;p<=u;p++)h*=p;s[f*r+m]/=h}}let[i,c]=Qg(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 Gi(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=ot(Xg,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 Xi(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=pe(Ot(e-i,-n),[-r,0]);if(c[0]<=0)break;let a=pe(Ot(i,n),[r,0]),l=ot(a,[e,0]),f=ot(c,[e,0]);s+=fl(i,e-i,l,f,!1)/(l[0]+l[1])}return Math.min(t*s,1)}var Km=2**28;function Um(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 g=(h+n+r)/e;g>0&&g<1&&s.push({t:g,up:!1,i:h})}if(s.length*(2*o+1)*Math.min(e+1,100)>Km)return NaN;s.sort((h,p)=>h.t-p.t);let i=o+1,c=new Float64Array(e+1),a=new Float64Array(e+1);c[0]=1;let l=0,f=0,m=0,u=0;for(let h of s){let p=(h.t-f)/(1-f);if(p>0){let g=p/(1-p);a.fill(0);for(let d=l;d<i;d++){let v=c[d];if(v===0)continue;let N=e-d,b=Math.min(N,Math.floor((N+1)*p)),E=i-d,w=pn(b,N,p,!1);for(let y=b;y<=N&&w!==0;y++)y>b&&(w=w*(N-y+1)*g/y),y>=E?m+=v*w:a[d+y]+=v*w,u++;w=b>0?pn(b-1,N,p,!1):0;for(let y=b-1;y>=0&&w!==0;y--)y<b-1&&(w=w*(y+1)/((N-y)*g)),y>=E?m+=v*w:a[d+y]+=v*w,u++}if(u>2*Km)return NaN;[c,a]=[a,c]}if(f=h.t,h.up)i=h.i+1;else{for(let g=l;g<=h.i;g++)m+=c[g],c[g]=0;l=Math.max(l,h.i+1)}}return Math.min(m,1)}function Hm(e,t,n,r={}){let o=r.alternative??"two.sided";if(Number.isNaN(e)||!(t>=1)||!(n>=1)||t!==Math.floor(t)||n!==Math.floor(n))return NaN;if(e<=0)return 1;if(e>1)return 0;let s=t+n,i;if(r.z!==void 0){if(r.z.length!==s)throw new RangeError("z must have length m + n");let u=[...r.z].sort((h,p)=>h-p);i=new Array(s+1).fill(!1);for(let h=1;h<s;h++)i[h]=u[h]!==u[h-1]}let c=e*t*n*(1-1e-10),a=(u,h)=>{let p=u*n-h*t;return(o==="two.sided"?Math.abs(p):o==="greater"?p:-p)>=c},l=new Float64Array(n+1),f=new Float64Array(n+1),m=0;for(let u=0;u<=t;u++){for(let h=0;h<=n;h++){if(u===0&&h===0){f[0]=1;continue}let p=u+h,g=(u>0?u/p*l[h]:0)+(h>0?h/p*f[h-1]:0);(i===void 0?p<s:i[p])&&a(u,h)?(g>0&&(m+=g*vn(u,t,n,p,!1)),f[h]=0):f[h]=g}[l,f]=[f,l],f.fill(0)}return Math.min(m,1)}var Jg=["alternative","exact"];function Ym(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 Gm(e,t,n={}){if(B(n,Jg,"ksTest"),typeof e=="string"){let N=tr(e,t);return Gm(N.x,N.y,n)}let r=Array.from(e).filter(N=>!Number.isNaN(N)).sort((N,b)=>N-b),o=r.length;if(o<1)throw new RangeError("not enough 'x' data");let s=Je(n.alternative),i=s==="two.sided"?"D":s==="greater"?"D^+":"D^-";if(typeof t=="function"){let N=-1/0,b=-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),b=Math.max(b,R-M/o)}let E=s==="two.sided"?Math.max(N,b):s==="greater"?N:b,w=new Set(r).size<o,y=n.exact??(o<100&&!w),x;if(y&&s==="two.sided"&&E<.5&&Bm(o,E),s==="two.sided")if(!y)x=Gi(Math.sqrt(o)*E,!1);else{let M=2*Xi(o,E);if(E>=.5)x=Math.min(1,M);else{let R=M<.01?Um(o,E):NaN;x=Number.isNaN(R)?1-Wm(o,E):R}}else x=y?Xi(o,E):Math.exp(-2*o*E*E);return J("ksTest",{method:`${y?"Exact":"Asymptotic"} one-sample Kolmogorov-Smirnov test`,alternative:Ym(s,!0),exact:y,statistic:E,statisticName:i,pValue:Math.min(1,Math.max(0,x)),warnings:w?["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,b)=>N[0]-b[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 b=f/o-m/a;u=Math.max(u,b),h=Math.max(h,-b)}let p=s==="two.sided"?Math.max(u,h):s==="greater"?u:h,g=new Set(l.map(N=>N[0])).size<l.length,d=n.exact??o*a<1e4,v;if(d)v=Hm(p,o,a,g?{alternative:s,z:l.map(N=>N[0])}:{alternative:s});else{let N=o*a/(o+a);v=s==="two.sided"?Gi(Math.sqrt(N)*p,!1):Math.exp(-2*N*p*p)}return J("ksTest",{method:`${d?"Exact":"Asymptotic"} two-sample Kolmogorov-Smirnov test`,alternative:Ym(s,!1),exact:d,statistic:p,statisticName:i,pValue:Math.min(1,Math.max(0,v)),warnings:g&&!d?["p-value will be approximate in the presence of ties"]:[]})}var ew=["poolSd","paired","pAdjustMethod","alternative"],tw=["mu","paired","exact","correct","confLevel","tolRoot","pAdjustMethod","alternative"],nw=["pAdjustMethod","correct"];function Xm(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=nn(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 Qi(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=Xr(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 Zi(e,t,n,r,o=[]){return{method:e,pValue:n,rowNames:t.slice(1).map(Nt),colNames:t.slice(0,-1).map(Nt),pAdjustMethod:r,warnings:o}}function rw(e,t,n={}){B(n,ew,"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=Je(n.alternative),i=n.pAdjustMethod??"holm",{levels:c,groups:a}=Xm(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(v=>v.length),h=Tt(a),p=h.mean,g=0;for(let v of u)v>1&&(g+=v-1);Bo(g,h.ssWithin);let d=Math.sqrt(h.ssWithin/g);f=(v,N)=>{let b=(p[v]-p[N])/(d*Math.sqrt(1/u[v]+1/u[N]));return s==="less"?gt(b,g,!0,!1):s==="greater"?gt(b,g,!1,!1):2*gt(-Math.abs(b),g,!0,!1)}}else m=r?"paired t tests":"t tests with non-pooled SD",f=(u,h)=>Ze(()=>yn(a[u],a[h],{paired:r,alternative:s})).pValue;return Zi(m,c,Qi(l,f,i),i)}function ow(e,t,n={}){B(n,tw,"pairwiseWilcoxTest");let{pAdjustMethod:r,alternative:o,...s}=n,i=r??"holm",{levels:c,groups:a}=Xm(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 At,u=Qi(l,(h,p)=>{let g=Ze(()=>Po(a[h],a[p],{...s,...o===void 0?{}:{alternative:o}}));f=g.method;for(let d of g.warnings)m.add(d);return g.pValue},i);return J("pairwiseWilcoxTest",Zi(f,c,u,i,m.list))}function sw(e,t,n={}){if(B(n,nw,"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 At,l=Qi(s,(m,u)=>{let h=Ze(()=>gi([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 J("pairwisePropTest",Zi("Pairwise comparison of proportions",f,l,i,a.list))}var Wo=[0,0];function na(e){if(!(e[0]>0))return[Math.sqrt(e[0]),0];let t=Math.sqrt(e[0]),n=De(e,jt(t,t));return Ot(t,n[0]/(2*t))}function ra(e,t){return Array.from({length:t},(n,r)=>e===void 0?[1,0]:na([e[r],0]))}function Zr(e,t){return Wn(e,t,[]).fit}function Wn(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=Wo;for(let v=f;v<r;v++)u=pe(u,Ce(m[v],m[v]));if(u[0]===0){a.push(Wo);continue}let h=na(u),p=m[f][0]<0?[-h[0],-h[1]]:h;m[f]=pe(m[f],p);let g=Ce(p,m[f]),d=v=>{let N=Wo;for(let E=f;E<r;E++)N=pe(N,Ce(m[E],v[E]));let b=ot(N,g);for(let E=f;E<r;E++)v[E]=De(v[E],Ce(b,m[E]))};for(let v=f+1;v<e.length;v++)d(s[v]);d(i);for(let v of c)d(v);a.push([-p[0],-p[1]])}let l=new Array(o).fill(Wo);for(let f=o-1;f>=0;f--){let m=i[f];for(let u=f+1;u<o;u++)m=De(m,Ce(s[u][f],l[u]));l[f]=ot(m,a[f])}return{fit:{qty:i,coef:l},qtv:c}}function Jr(e,t=0,n=e.length){let r=Wo;for(let o=t;o<n;o++)r=pe(r,Ce(e[o],e[o]));return r}var aw=1e-14;function iw(e,t,n,r,o,s,i,c){let a=y=>$(y,2*i),l=t.length,f=n.flatMap((y,x)=>Jr(y)[0]>aw*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=Zr(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 g=[];u.forEach((y,x)=>{let M=r[y],R=g.find(T=>T.term===M);R===void 0&&g.push(R={term:M,df:0,ss:[0,0]}),R.df++,R.ss=pe(R.ss,Ce(h[x],h[x]))});let d=l-m,v=Jr(h,m)[0],N=Ai(p,v,d),b=g.filter(y=>y.term!==0);d>0&&xr(c,v)&&(v=0,b=b.map(y=>xr(c,y.ss[0])?{...y,ss:[0,0]}:y));let E=b.map(y=>o[y.term-1]),w=y=>{if(d>0){let x=v/d,M=b.map(({df:R,ss:T})=>{let A=T[0]/R/x;return[R,y(T[0]),y(T[0]/R),A,ht(A,R,d,!1,!1)]});return M.push([d,y(v),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:b.map(({df:x,ss:M})=>[x,y(M[0]),y(M[0]/x)])}};return[w(a),w(y=>y),N]}function Qm(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=Ms({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(I=>!Number.isFinite(I)))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(I=>!a.warnings.includes(I))],h=f.colNames.length,p=f.colNames.map((I,F)=>Ie([f.x.map(L=>L[F])]).groups[0]),g=f.x.map((I,F)=>p.map(L=>L[F])),d=h>0?Lt(g):void 0,v=d?.rank??0;v<h&&u.push("Error() model is singular");let N=d?.pivot.slice(0,v)??[],b=I=>I.map(F=>[F,0]),E=a.colNames.map((I,F)=>b(Ie([a.x.map(L=>L[F])]).groups[0])),{groups:w,k:y}=Ie([l]),{fit:x,qtv:M}=Wn(N.map(I=>b(p[I])),b(w[0]),E),R=w[0].map(I=>[1,I]),T=E.map(I=>Jr(I)[0]),A=[];N.forEach((I,F)=>{let L=f.assign[I],D=L===0?"(Intercept)":f.terms.termLabels[L-1],S=A.find(O=>O.name===D);S===void 0&&A.push(S={name:D,idx:[]}),S.idx.push(F)}),m>v&&A.push({name:"Within",idx:Array.from({length:m-v},(I,F)=>v+F)});let k=A.map(I=>iw(I.name,I.idx.map(F=>x.qty[F]),M.map(F=>I.idx.map(L=>F[L])),a.assign,a.terms.termLabels,T,y,R)),C=k.map(I=>I[0]),_={formula:e,multistratum:!0,strata:C,rows:a.frame.rows,warnings:u};return Yt.set(_,k.map(I=>I[1])),k.some((I,F)=>I[2]&&A[F].name!=="(Intercept)")&&Si.set(_,[Ti]),_}var cw=["weights","contrasts","levels","intercept"],lw=["level"],fw=["interval","level"],Ji=2**-1022;function ec(e,t,n){let r=t.y;if(r===void 0)throw new TypeError("the formula has no response");let o=tu(t.x,r,t.frame.weights,t.terms.intercept,!0),s=o.result,i=[...t.warnings];if(!n){let a=o.yScale??0,l=o.fitRows.map(h=>$(s.fittedValues[h],-a)),f=l.reduce((h,p)=>h+p,0)/l.length,m=l.length>1?l.reduce((h,p)=>h+(p-f)**2,0)/(l.length-1):NaN,u=(o.rssScaled??o.rss)/s.dfResidual;Number.isFinite(u)&&u<1e-30*(f*f+m)&&i.push("essentially perfect fit: summary may be unreliable")}let c={...s,formula:e,response:t.terms.variables[0],coefNames:t.colNames,assign:t.assign,termLabels:t.terms.termLabels,order:t.terms.order,variables:t.terms.variables,factors:t.terms.factors,rows:t.frame.rows,x:t.x,y:r,effects:o.effects,rss:o.rss,nObs:o.fitRows.length,aov:n,warnings:i};return t.frame.weights!==void 0&&(c.weights=t.frame.weights),c}function Uo(e,t,n={}){if(B(n,cw,"lm"),typeof e=="string"){let r=jn(er(e),t,{...n,dropUnusedLevels:!0});return J("lm",Va(ec(e,r,!1),Bl(t,r.terms.variables)))}return mw(e,t,n)}function mw(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=tu(l,f,a,r).result;return m.rows=c.flatMap((u,h)=>u?[h]:[]),Va(m,s.length)}function uw(e,t,n,r,o,s,i){let c=s.rank,a=s.pivot.slice(0,c),l=n?.map(w=>$(w,-r)),f=ra(l===void 0?void 0:o.map(w=>l[w]),o.length).map((w,y)=>{if(n===void 0||l===void 0)return w;let x=n[o[y]];if(x===0||l[o[y]]>=Ji)return w;let M=na([x,0]);return[$(M[0],-r/2),$(M[1],-r/2)]}),m=a.map(w=>{let y=o.map((M,R)=>xt(f[R],e[M][w])),x=Vl(y.map(M=>M[0]));return{c:x===0?y:y.map(M=>[$(M[0],-x),$(M[1],-x)]),e:x}}),{qty:u,coef:h}=Zr(m.map(({c:w})=>w),o.map((w,y)=>xt(f[y],t[w]))),p=h.map((w,y)=>m[y].e===0?w:[$(w[0],-m[y].e),$(w[1],-m[y].e)]),g=e[0]?.map(()=>NaN)??[];a.forEach((w,y)=>g[w]=p[y][0]);let d=e.map(w=>{let y=[0,0];return a.forEach((x,M)=>y=pe(y,xt(p[M],w[x]))),y}),v=Jr(u,c)[0],N=o.map(w=>l?.[w]??1),b=[0,0];if(i){let w=[0,0],y=[0,0];o.forEach((x,M)=>{w=pe(w,[N[M],0]),y=pe(y,xt(d[x],N[M]))}),b=ot(y,w)}let E=[0,0];return o.forEach((w,y)=>{let x=De(d[w],b);E=pe(E,xt(Ce(x,x),N[y]))}),{coefficients:g,fittedValues:d.map(w=>w[0]),residuals:d.map((w,y)=>De([t[y],0],w)[0]),effects:u.map(w=>w[0]),rss:v,mss:E[0]}}function tu(e,t,n,r,o=!1){let s=Ie([t.filter((l,f)=>n===void 0||n[f]!==0)]).k;if(s===0)return Zm(Jm(e,t,n,r,o),e,t,n);let i=Jm(e,t.map(l=>$(l,-s)),n,r,o),c=i.result,a=l=>$(l,s);return c.coefficients=c.coefficients.map(a),c.residuals=c.residuals.map(a),c.fittedValues=c.fittedValues.map(a),c.sigma=a(c.sigma),c.coefTable.estimate=c.coefTable.estimate.map(a),c.coefTable.stdError=c.coefTable.stdError.map(a),Zm({...i,effects:i.effects.map(a),rss:$(i.rss,2*s),yScale:s,rssScaled:i.rss},e,t,n)}function Zm(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]=_a(f,0)?Eo(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 tc=new WeakMap;function nc(e){return tc.get(e)??0}function hw(e){return e.qr.every(t=>t.every(Number.isFinite))&&e.qraux.every(Number.isFinite)}function Jm(e,t,n,r,o){if(t.length===0)throw new RangeError("0 (non-NA) cases");let s=t.map((P,Y)=>n===void 0||n[Y]!==0),i=e.filter((P,Y)=>s[Y]),c=t.filter((P,Y)=>s[Y]),a=n?.filter((P,Y)=>s[Y]),l=c.length;if(l===0)throw new RangeError("all weights are zero");if(i.some(P=>P.some(Y=>!Number.isFinite(Y))))throw new RangeError("NA/NaN/Inf in 'x'");if(c.some(P=>!Number.isFinite(P)))throw new RangeError("NA/NaN/Inf in 'y'");if(a?.some(P=>!Number.isFinite(P)))throw new RangeError("non-finite weights not allowed");let f=t.flatMap((P,Y)=>s[Y]?[Y]:[]),m=a===void 0?1:yt(a),u=Number.isFinite(m)?2*Math.floor(Math.floor(Math.log2(m))/2):0,h=0,p=a?.map(Math.sqrt),g=p===void 0?i:i.map((P,Y)=>P.map(z=>z*p[Y])),d=Lt(g);a!==void 0&&Number.isFinite(m)&&u+2>0&&!hw(d)&&(h=u+2,p=a.map(P=>$(Math.sqrt(P),-h/2)),g=i.map((P,Y)=>P.map(z=>z*p[Y])),d=Lt(g),tc.set(d,h));let v=p===void 0?c:c.map((P,Y)=>P*p[Y]),N=d.rank,b=P=>$(P,-u),E=o?uw(e,t,n,u,f,d,r):void 0,w=E?.coefficients??As(d,v),y=E?.residuals??[],x=E?.fittedValues??[],M=l-N,R=a?.map(b)??new Array(l).fill(1),T=E?.rss??0;if(E===void 0){let P=Ur(d,v),Y=p===void 0?P:P.map((H,K)=>H/p[K]),z=0;t.forEach((H,K)=>{if(s[K])y.push(Y[z]),x.push(H-Y[z]),z++;else{let te=e[K].reduce((se,Me,Ne)=>Number.isNaN(w[Ne])?se:se+Me*w[Ne],0);x.push(te),y.push(H-te)}});for(let H=0;H<l;H++)T+=R[H]*Y[H]**2}let A=x.filter((P,Y)=>s[Y]),k=0;if(a!==void 0&&a.some(P=>P!==0&&$(P,-u)<Ji)){let P=f.flatMap((Y,z)=>{let H=y[Y];return H===0?[]:[{w:a[z],r:H,a:Math.floor(Math.log2(Math.abs(H)))}]});if(P.length>0&&P.every(Y=>Number.isFinite(Y.r))){let Y=Math.max(...P.map(K=>Math.log2(K.w)+2*K.a)),z=2*Math.floor(Math.floor(Y)/2),H=0;for(let K of P)H+=$(K.w,2*K.a-z)*$(K.r,-K.a)**2;T=H,k=z-u}}let C=T/M,_=$(Math.sqrt(C),(u+k)/2),I=h===0?0:(h-u)/2,F=d.qr.slice(0,N).map(P=>P.slice(0,N).map(Y=>h===0?$(Y,-u/2):Y)),{inv:L,e:D}=N>0?So(F):{inv:[],e:[]},O=d.pivot.slice(0,N).map((P,Y)=>[P,Y]).sort((P,Y)=>P[0]-Y[0]),q=O.map(([P])=>w[P]),V=O.map(([,P])=>$(Math.sqrt(L[P][P]*C),k/2-D[P]-I)),W=q.map((P,Y)=>P/V[Y]),Q=W.map(P=>2*gt(-Math.abs(P),M,!0,!1)),re={coefficients:w,residuals:y,fittedValues:x,rank:N,dfResidual:M,coefTable:{estimate:q,stdError:V,tValue:W,pValue:Q},sigma:_,rSquared:0,adjRSquared:0,intercept:r,qr:d,x:e,rows:e.map((P,Y)=>Y)};n!==void 0&&(re.weights=n);let j=r?1:0,ee=N-j,U={result:re,fitRows:f,effects:(E?.effects??[]).map(P=>$(P,u/2)),rss:$(T,u+k)};if(ee===0)return U;let ue=0;if(E!==void 0)ue=E.mss;else if(r){let P=R.reduce((z,H)=>z+H,0),Y=0;for(let z=0;z<l;z++)Y+=R[z]*A[z]/P;for(let z=0;z<l;z++)ue+=R[z]*(A[z]-Y)**2}else for(let P=0;P<l;P++)ue+=R[P]*A[P]**2;re.rSquared=ue/(ue+$(T,k)),re.adjRSquared=1-(1-re.rSquared)*((l-j)/M);let Z=$(ue/ee/C,-k);return re.fStatistic={value:Z,numdf:ee,dendf:M},re.fPValue=ht(Z,ee,M,!1,!1),U}function nu(e,t){let n=(1-e)/2;return n<1e-8?wt(n,t,!1,!1):wt((1+e)/2,t,!0,!1)}function pw(e,t={}){B(t,lw,"confint");let n=t.level??.95;if(!(n>0&&n<1))throw new RangeError("'level' must be between 0 and 1");let r=nu(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 eu(e,t,n){if(t===0||!Number.isFinite(t))return e*t;let r=Math.floor(Math.log2(e)),o=Math.floor(Math.log2(Math.abs(t)));return $($(e,-r)*$(t,-o),r+o+n)}function dw(e,t,n={}){B(n,fw,"predict");let o=(t.length>0&&typeof t[0]!="number"?t.map(b=>Array.from(b)):Array.from(t,b=>[b])).map(b=>e.intercept?[1,...b]:b);if(o.some(b=>b.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(b=>b.slice(0,s)),a=-(tc.get(e.qr)??0)/2,l=e.coefficients.flatMap((b,E)=>Number.isNaN(b)?[]:[E]),f=o.map(b=>{let E=b.reduce((w,y,x)=>Number.isNaN(e.coefficients[x])?w:w+y*e.coefficients[x],0);return _a(E,0)?Eo(l.map(w=>b[w]),l.map(w=>e.coefficients[w]),E):E}),m=b=>{let E=new Array(s).fill(0);for(let w=0;w<s;w++){let y=b[w];for(let x=0;x<w;x++)y-=c[x][w]*E[x];E[w]=y/c[w][w]}return E},u=o.map(b=>{let E=i.map(x=>b[x]),w=Ca(m(E));if(w>=Ji&&w<1/0||!E.every(Number.isFinite)||E.every(x=>x===0))return a===0||!(w>0&&w<1/0)?w*e.sigma:eu(w,e.sigma,a);let y=Math.floor(Math.log2(yt(E.map(Math.abs))));for(let x of[0,600]){let M=Ca(m(E.map(R=>$(R,-(y+x)))));if(M<1/0)return M===0?M*e.sigma:eu(M,e.sigma,y+x+a)}return w*e.sigma}),h=n.interval??"none";if(h!=="none"&&h!=="confidence"&&h!=="prediction")throw new RangeError(`invalid 'interval': ${String(h)}`);let p=jl(e.qr,e.rank,o),g=p.length>0?{nonEstimable:p,warnings:[zl]}:{};if(h==="none")return J("predict",{fit:f,seFit:u,...g},e);let d=n.level??.95;if(!(d>0&&d<1))throw new RangeError("'level' must be between 0 and 1");let v=nu(d,e.dfResidual),N=u.map(b=>v*(h==="prediction"?Math.hypot(b,e.sigma):b));return J("predict",{fit:f,seFit:u,lwr:f.map((b,E)=>b-N[E]),upr:f.map((b,E)=>b+N[E]),...g},e)}var gw=["weights","contrasts","levels"],ru=["confLevel"],ou=["sigmaX","sigmaY","mu","alternative","confLevel"];function ww(e,t,n={}){return B(n,gw,"aov"),typeof e=="string"?J("aov",su(e,t,n)):J("aov",bw(e,t))}function su(e,t,n){let r=er(e,!0),{treatment:o,errors:s}=ql(r.rhs);if(s.length>1)throw new Error(`there are ${s.length} Error terms: only 1 is allowed`);return s.length===1?Qm(e,r,o,s[0],t,n):ec(e,jn(r,t,{...n,dropUnusedLevels:!0},o),!0)}function bw(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 su("y ~ g",{y:o,g:s},{levels:{g:n}})}function au(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}=Tt(e),i=t-1,c=n-t,a=r/i,l=o/c,f=a/l,m=u=>$(u,2*s);return{df:[i,c],sumSq:[m(r),m(o)],meanSq:[m(a),m(l)],fValue:f,pValue:c>0?ht(f,i,c,!1,!1):NaN}}function vw(e,t,n={}){ct(t)&&B(t,ru,"TukeyHSD"),B(n,ru,"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;ln(c);let l=s.reduce((v,N)=>v+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}=Tt(s),h=f/l,p=Ml(c,i,l),g=[],d={names:[],i:[],j:[],diff:[],lwr:[],upr:[],pAdj:g,pAdjusted:g,warnings:[]};for(let v=0;v<i-1;v++)for(let N=v+1;N<i;N++){let b=m[N]-m[v],E=Math.sqrt(h/2*(1/s[N].length+1/s[v].length));d.names.push(`${o[N]}-${o[v]}`),d.i.push(N+1),d.j.push(v+1),d.diff.push($(b,u)),d.lwr.push($(b-p*E,u)),d.upr.push($(b+p*E,u)),d.pAdj.push(bs(Math.abs(b)/E,i,l,{lowerTail:!1}))}return d}function Nw(e,t,n={}){ct(t)&&B(t,ou,"zTest"),B(n,ou,"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=Je(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=Ie([l,f??[],[o,s??0]]),u=m.k,[h,p,[g,d]]=m.groups,v=Qn(h)[0],N=f===void 0?NaN:Qn(p)[0],b=f===void 0?g/Math.sqrt(l.length):Math.sqrt(g**2/l.length+d**2/f.length),E=f===void 0?v:v-N,w=(E-$(i,-u))/b,y,x;if(c==="less")y=ae(w),x=[-1/0,E+ie(a)*b];else if(c==="greater")y=ae(w,{lowerTail:!1}),x=[E-ie(a)*b,1/0];else{y=2*ae(-Math.abs(w));let T=(1-a)/2,A=(T<1e-8?ie(T,{lowerTail:!1}):ie(1-(1-a)/2))*b;x=[E-A,E+A]}x=[$(x[0],u),$(x[1],u)];let M=f!==void 0,R=(T,A)=>{if(!(u>0&&T.some((C,_)=>C!==0&&Math.abs(A[_])<2**-1022)))return $(Qn(A)[0],u);let k=Math.max(Ie([T]).k,0);return $(Qn(k===0?T:T.map(C=>$(C,-k)))[0],k)};return{method:M?"Two-sample z-Test":"One-sample z-Test",alternative:c,statistic:w,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 yw=["parm","level","test"],iu={epsilon:1e-300,maxit:100,untilCoefficients:!0};function rc(e,t,n,r,o=1e-11){let s=[];for(let g=0;g<t.length;g++)n[g]>0&&s.push(g);let i=0,c=0;for(let g of s)i+=n[g],c+=n[g]*t[g];if(r&&!(Number.isFinite(i)&&Number.isFinite(c))){let g=0;for(let d of s)g=Math.max(g,n[d]);if(Number.isFinite(g)){let d=g>1?Math.floor(Math.log2(g)):0;i=0,c=0;for(let v of s){let N=$(n[v],-d);i+=N,c+=N*t[v]}}}let a=r?c/i:0,l=0;for(let g of s)l+=n[g]*(t[g]-a)**2;if((e.length>0?e[0].length:0)===0||s.length===0)return 0;let m=s.map(g=>Math.sqrt(n[g])),u=Lt(s.map((g,d)=>e[g].map(v=>v*m[d])),{tol:o}),h=Ur(u,s.map((g,d)=>t[g]*m[d])),p=0;for(let g of h)p+=g*g;if(!Number.isFinite(l)){let g=s.map((v,N)=>m[N]*(t[v]-a)),d=0;for(let v of g)d=Math.max(d,Math.abs(v));for(let v of h)d=Math.max(d,Math.abs(v));if(Number.isFinite(d)){let v=d>1?Math.floor(Math.log2(d)):0,N=0;for(let E of g)N+=$(E,-v)**2;let b=0;for(let E of h)b+=$(E,-v)**2;return $($(N-b,v),v)}}return l-p}function Mw(e,t={}){B(t,yw,"glmConfint");let{fit:n,k:r}=Kn(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},(_,I)=>I);for(let _ of c)if(!Number.isInteger(_)||_<0||_>=i)throw new RangeError(`invalid coefficient index ${_}`);let a=n.model,l=n.coefficients.map((_,I)=>I).filter(_=>!Number.isNaN(n.coefficients[_])),f=new At,m=ko(a,l,f,{...iu,start:a.start===void 0?void 0:l.map(_=>a.start[_])}),u=new Array(i).fill(NaN);if(l.forEach((_,I)=>u[_]=m.fit.coefficients[I]),!m.fit.converged)return{lower:c.map(()=>NaN),upper:c.map(()=>NaN)};let h=!1,p={...n,coefficients:u,weights:m.fit.workingWeights,residuals:m.fit.workingResiduals,qr:m.fit.qr,rank:m.fit.rank},g=Ze(()=>Ka(p)),d=g.dispersion,v=new Array(i).fill(NaN);(m.fit.qr===void 0?[]:m.fit.qr.pivot.slice(0,m.fit.rank)).forEach((_,I)=>v[l[_]]=g.stdError[I]);let b=a.x.map(_=>l.map(I=>_[I])),E=m.fit.eta,w=(_,I)=>{let F=l.filter(S=>S!==_),L=ko(a,F,new At,{...iu,start:void 0,etastart:E,extraOffset:a.x.map(S=>S[_]*I)}).fit;if(!L.converged)return h=!0,NaN;let D=s==="LRT"?L.deviance-m.fit.deviance:rc(b,L.workingResiduals,L.workingWeights,!1);return Math.sign(I-u[_])*Math.sqrt(Math.max(D/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]:[ie(y),y<1e-8?ie(y,{lowerTail:!1}):ie((1+o)/2)],A=c.map(()=>NaN),k=c.map(()=>NaN),C=(_,I,F)=>{let L=F===0?-1:1;h=!1;let D=NaN,S=NaN;for(let j=0;j<=30;j++){let ee=u[_]+L*2**j*v[_],U=w(_,ee);if(Math.abs(U)>=Math.abs(I)){D=ee,S=U;break}}if(Number.isNaN(D)||h)return NaN;let O=j=>j===u[_]?-I:w(_,j)-I,[q,V]=F===0?[D,u[_]]:[u[_],D],[W,Q]=F===0?[S-I,-I]:[-I,S-I],re;try{re=Ht(O,q,V,{tol:1e-14*v[_],maxIter:500,fLower:W,fUpper:Q})}catch(j){if(!h)throw j;re=NaN}return h?NaN:re};return c.forEach((_,I)=>{if(Number.isNaN(u[_]))return;if(!R){A[I]=C(_,T[0],0),k[I]=C(_,T[1],1);return}let F=[-2*M,-M,0,M,2*M],L=[C(_,-2*M,0),C(_,-M,0),u[_],C(_,M,1),C(_,2*M,1)].map(S=>S-u[_]),D=S=>{let O=0;for(let q=0;q<5;q++){let V=1;for(let W=0;W<5;W++)W!==q&&(V*=(S-F[W])/(F[q]-F[W]));O+=V*L[q]}return u[_]+O};A[I]=D(T[0]),k[I]=D(T[1])}),{lower:A.map(_=>$(_,r)),upper:k.map(_=>$(_,r))}}var Ew=["type","omega","sandwich"],xw=["cluster","type","cadjust","multi0","fix","sandwich"],oa=2**-52;function ac(e,t){if(t===0)return e;let n=cc.get(e)??{mant:e,exp:e.map(()=>0)};return kr(n.mant,n.exp.map(r=>r+t))}var sa=e=>Math.floor(Math.log2(Math.abs(e)));function mn(e){if(e===0||!Number.isFinite(e))return[e,0];let t=sa(e);return[$(e,-t),t]}var Ho=(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+sa(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?$(e,t):Ve(e,t),cc=new WeakMap;function kr(e,t){let n=e.map((r,o)=>r.map((s,i)=>ir(s,t[o]+t[i])));return cc.set(n,{mant:e,exp:t}),n}function lc(e,t,n){let r=cc.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 aa(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 mu(e,t){return e.pivot.slice(0,t).map((n,r)=>[n,r]).sort((n,r)=>n[0]-r[0])}function uu(e,t){let{inv:n,e:r}=So(e.qr.slice(0,t).map(s=>s.slice(0,t))),o=mu(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 cu(e,t,n){let{inv:r,e:o,cols:s}=uu(e,t);return{A:r.map((i,c)=>i.map((a,l)=>$(a,-o[c]-o[l]-n))),cols:s}}function lu(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)?sa(a):0}),{inv:o}=So(n.map(i=>i.map((c,a)=>$(c,-r[a])))),s=mu(e,t);return{B:s.map(([,i])=>s.map(([,c])=>o[i][c])),e:s.map(([,i])=>r[i])}}function hu(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 Rw(e,t){let n=e.qr.length;return t===0?new Array(n).fill(0):hu(e,t).map(o=>{let s=o.reduce((i,c)=>i+c*c,0);return s>1-10*oa?1:s})}function fu(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=Da(e,a),f=0;for(let m=t;m<c;m++)f+=l[m]*l[m];return f})}function ia(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 fc(e){let t=ia(e);if(e.qr===void 0||e.rank===0)return{vcov:[],names:[]};let{inv:n,e:r,cols:o}=uu(e.qr,e.rank),s=cr(e)?0:nc(e.qr),i,c;if(cr(e)){let l=Kn(e),f=ja(e).fixedDispersion?1:qo(l.fit),m=f===0||!Number.isFinite(f)?0:2*Math.floor(sa(f)/2);i=$(f,-m),c=m+2*l.k}else{let[l,f]=mn(e.sigma);i=l*l,c=2*f}let a=n.map(l=>l.map(f=>f*i));return{vcov:kr(a,r.map(l=>(c-s)/2-l)),names:o.map(l=>t[l])}}function pu(e,t=!1){let n=ia(e);if(cr(e)){let g=t?Kn(e):{fit:e,k:0},d=g.fit,v=ja(d),N=d.priorWeights,b=N.map(L=>L>0),E=N.flatMap((L,D)=>L>0?[D]:[]),{A:w,cols:y}=d.qr===void 0||d.rank===0?{A:[],cols:[]}:cu(d.qr,d.rank,0),x=E.map(L=>y.map(D=>d.model.x[L][D])),M=E.map(L=>d.weights[L]),R=E.map(L=>d.weights[L]*d.residuals[L]),T=v.family==="binomial"||v.family==="poisson",A=T?1:R.reduce((L,D)=>L+D*D,0)/M.reduce((L,D)=>L+D,0),k=d.model.rows,C=Ba(d),_=Ul(d),I=()=>{let L=Kn(d),D=E.map(j=>L.fit.residuals[j]),S=M.map(Math.sqrt),{B:O,e:q}=d.qr===void 0||d.rank===0?{B:[],e:[]}:lu(d.qr,d.rank),V=x.map((j,ee)=>j.map((U,ue)=>$(S[ee]*U,-q[ue]))),{v:W,s:Q}=ar(S.map((j,ee)=>j===0?[0,0]:Ho(mn(j),mn(D[ee])))),re=()=>{let j=[1,0];if(!T){let ee=ar(M.map((ue,Z)=>ue===0?[0,0]:Ho(mn(ue),mn(D[Z])))),U=ar(M.map(mn));j=[ee.v.reduce((ue,Z)=>ue+Z*Z,0)/U.v.reduce((ue,Z)=>ue+Z,0),2*ee.s-U.s+2*L.k]}return{T:x.map(ee=>ee.map((U,ue)=>ir(U,-q[ue]))),factor:[j[0]*j[0],2*j[1]]}};return{T:V,B:O,e:q,u:W,s:Q+L.k,omegaPart:re}},F;if(qr(A)&&R.every(Number.isFinite))F=R.map(L=>L/A);else{let L=Kn(d),D=ar(M.map((S,O)=>S===0?[0,0]:Ho(mn(S),mn(L.fit.residuals[E[O]]))));if(T)F=D.v.map(S=>ir(S,D.s+L.k));else{let S=ar(M.map(mn)),O=D.v.reduce((q,V)=>q+V*V,0)/S.v.reduce((q,V)=>q+V,0);F=D.v.map(q=>ir(q/O,S.s-D.s-L.k))}}return{glm:!0,names:y.map(L=>n[L]),X:x,W:M,res:F,phiS:A,A:w,h:C,omh:fu(d.qr,d.rank,C,L=>_[L]),qr:d.qr,rank:d.rank,qrRow:L=>_[L],inputRows:E.map(L=>k[L]),nInput:$a(e,k),nonZero:b,dispersion:()=>qo(d),scaled:I,k:g.k}}let r=e,o=r.weights,s=r.residuals.map((g,d)=>o===void 0||o[d]!==0),i=s.flatMap((g,d)=>g?[d]:[]),c=nc(r.qr),{A:a,cols:l}=r.rank===0?{A:[],cols:[]}:cu(r.qr,r.rank,c),f=i.map(g=>l.map(d=>r.x[g][d])),m=i.map(g=>o?.[g]??1),u=i.map((g,d)=>m[d]*r.residuals[g]),h=Rw(r.qr,r.rank),p=()=>{let g=m.map(y=>c===0?Math.sqrt(y):$(Math.sqrt(y),-c/2)),{B:d,e:v}=r.rank===0?{B:[],e:[]}:lu(r.qr,r.rank),N=f.map((y,x)=>y.map((M,R)=>$(g[x]*M,-v[R]))),{v:b,s:E}=ar(g.map((y,x)=>Ho(mn(y),mn(r.residuals[i[x]]))));return{T:N,B:d,e:v,u:b,s:E,omegaPart:()=>({T:f.map(y=>y.map((x,M)=>ir(x,-v[M]))),factor:[1,-2*c]})}};return{glm:!1,names:l.map(g=>n[g]),X:f,W:m,res:u,phiS:1,A:a,h,omh:fu(r.qr,r.rank,h,g=>g),qr:r.qr,rank:r.rank,qrRow:g=>g,inputRows:i.map(g=>r.rows[g]),nInput:$a(r,r.rows),nonZero:s,dispersion:()=>r.sigma*r.sigma,scaled:p,k:0}}var mc=2**-969,qr=e=>Math.abs(e)>=mc&&Math.abs(e)<1/0;function du(e,t,n,r){if(!(e.phiS>=mc&&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 Yo(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 eo(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 oc(e){let t=e.slice(0,10).map(n=>String(n+1));return e.length>10?`${t.join(", ")}, ...`:t.join(", ")}var Tw=["HC3","const","HC","HC0","HC1","HC2","HC4","HC4m","HC5"];function uc(e,t={}){B(t,Ew,"vcovHC"),aa(e);let n=ke(t.type,Tw),r=n==="HC"?"HC0":n,o=t.sandwich??!0;if(typeof o!="boolean")throw new TypeError("'sandwich' must be true or false");let s=pu(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 g;if(typeof t.omega=="function"?g=t.omega(l.slice(),f.slice(),i-c):(g=t.omega,g.length!==i&&g.length===s.nonZero.length&&(g=g.filter((d,v)=>s.nonZero[v]))),!Array.isArray(g)&&!ArrayBuffer.isView(g))throw new TypeError("'omega' must give an array of numbers");if(g.length!==i)throw new RangeError(`'omega' must have one value per observation (${i})`);if(u=Array.from(g,Number),u.some(d=>!Number.isFinite(d)))throw new RangeError("'omega' must be finite")}else if(r==="const"){let g=s.dispersion();u=s.W.map(d=>g*d/(s.phiS*s.phiS))}else{let g=f.flatMap((N,b)=>N>1-Math.sqrt(oa)?[s.inputRows[b]]:[]);if(g.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) ${oc(g)}`:`${r} covariances are numerically unstable for hat values close to 1 (and undefined if exactly 1) as for observation(s) ${oc(g)}`),r!=="HC0"&&r!=="HC1"){let N=f.flatMap((b,E)=>b>=1?[s.inputRows[E]]:[]);if(N.length>0)throw new RangeError(`${r} covariances are undefined for hat values of 1 as for observation(s) ${oc(N)}`)}if(r==="HC1"&&i<=c)throw new RangeError("HC1 covariances need more observations than coefficients");let d=Math.round(f.reduce((N,b)=>N+b,0)),v=f.reduce((N,b)=>Math.max(N,b),0);h=N=>{switch(r){case"HC0":return N;case"HC1":return N.map(b=>b*i/(i-c));case"HC2":return N.map((b,E)=>b/m[E]);case"HC3":return N.map((b,E)=>b/m[E]**2);case"HC4":return N.map((b,E)=>b/m[E]**Math.min(4,i*f[E]/d));case"HC4m":return N.map((b,E)=>b/m[E]**(Math.min(1,i*f[E]/d)+Math.min(1.5,i*f[E]/d)));default:return N.map((b,E)=>b/Math.sqrt(m[E]**Math.min(i*f[E]/d,Math.max(4,i*.7*v/d))))}},u=h(l.map(N=>N*N))}let p;if(o&&r==="const"&&t.omega===void 0)p=fc(e).vcov;else if(o){let g=eo(s.X,u,i);if(p=Yo(s.A,g,i*s.phiS*s.phiS),!du(s,g,p,(h===void 0?u:l).every(d=>d===0))){let d=s.scaled();if(h!==void 0){let v=Yo(d.B,eo(d.T,h(d.u.map(N=>N*N)),i),i);p=kr(v,d.e.map(N=>d.s-N))}else{let{T:v,factor:N}=d.omegaPart(),b=ar(u.map(E=>Ho(mn(E),N)),!0);p=kr(Yo(d.B,eo(v,b.v,i),i),d.e.map(E=>b.s/2-E))}}p=ac(p,s.k)}else{let g=eo(s.X,u,i);if(bu(g,(h===void 0?u:l).every(d=>d===0)))p=g;else{let d=s.scaled();if(h!==void 0){let{mant:v,exp:N}=vu(d,eo(d.T,h(d.u.map(b=>b*b)),i));p=kr(v,N)}else{let v=ar(u.map(mn),!0);p=kr(eo(d.omegaPart().T,v.v,i),d.e.map(N=>v.s/2+N))}}p=ac(p,-s.k)}return J("vcovHC",{vcov:p,names:s.names,warnings:a},e)}var Aw=["HC","HC0","HC1","HC2","HC3"];function Sw(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 Nt(e);if(typeof e=="string")return e;throw new TypeError("cluster labels must be numbers, strings or booleans")}function kw(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 qw(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 gu=oa**(1/1.3);function wu(e,t){let{values:n,vectors:r}=Ut(e,{symmetric:!0});if(n.some(i=>!(i>=gu)))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 Iw(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 _w(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 Lw(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]>=gu))throw sc(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??=hu(e.qr,e.rank);let N=Cw(e,l,f,h,o);if(N===null)throw sc(m);if(N!==void 0)return Array.from({length:s},(b,E)=>c*u.reduce((w,y,x)=>w+y[E]*N[x],0))}let p=u.map(N=>Array.from({length:s},(b,E)=>N.reduce((w,y,x)=>w+y*t.A[x][E],0))),g=u.map((N,b)=>u.map((E,w)=>(b===w?1:0)-p[b].reduce((y,x,M)=>y+x*E[M],0)));for(let N=0;N<g.length;N++)for(let b=0;b<N;b++)g[N][b]=g[b][N]=(g[N][b]+g[b][N])/2;let d=wu(g,o);if(d===void 0)throw sc(m);let v=d.map(N=>N.reduce((b,E,w)=>b+E*h[w],0));return Array.from({length:s},(N,b)=>c*u.reduce((E,w,y)=>E+w[b]*v[y],0))})}function Cw(e,t,n,r,o){let s=e.qr,i=e.rank,c=new Array(i).fill(0),a=n.map(w=>{let y=e.qrRow(w);return y<0?c:t[y]}),l=Array.from({length:i},(w,y)=>Array.from({length:i},(x,M)=>a.reduce((R,T)=>R+T[y]*T[M],0))),{values:f,vectors:m}=Ut(l,{symmetric:!0});if(!f.some(w=>w>.5))return;let u=m,h=r.slice(),p=[];f.forEach((w,y)=>{let x=a.map(M=>M.reduce((R,T,A)=>R+T*u[A][y],0));if(w>.5)p.push(x.map(M=>M/Math.sqrt(w)));else if(w>0){let R=Math.expm1(o*Math.log1p(-w))/w*x.reduce((T,A,k)=>T+A*r[k],0);x.forEach((T,A)=>h[A]+=R*T)}});let g=s.qr.length,d=p.map(w=>{let y=new Array(g).fill(0);return n.forEach((x,M)=>{let R=e.qrRow(x);R>=0&&(y[R]=w[M])}),Da(s,y).slice(i)}),v=d.map(w=>d.map(y=>w.reduce((x,M,R)=>x+M*y[R],0))),N=wu(v,o);if(N===void 0)return null;let b=p.map(w=>w.reduce((y,x,M)=>y+x*r[M],0)),E=N.map((w,y)=>w.reduce((x,M,R)=>x+M*b[R],0)-b[y]);return p.forEach((w,y)=>w.forEach((x,M)=>h[M]+=E[y]*x)),h}function sc(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 Fw(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 ic(e){if(e.length===0)return e;let{values:t,vectors:n}=Ut(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 Ow(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}=Ut(o,{symmetric:!0}),i=s.reduce((a,l)=>Math.max(a,Math.abs(l)),0);if(s.every(a=>a>=-16*n*oa*i))return{mant:e,exp:t};let c=Math.max(...t);return{mant:ic(e.map((a,l)=>a.map((f,m)=>ir(f,t[l]+t[m]-2*c)))),exp:t.map(()=>c)}}function bu(e,t){return t||e.every((n,r)=>qr(n[r])&&n.every(Number.isFinite))}function vu(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 Dw(e,t={}){B(t,xw,"vcovCL"),aa(e);let n=pu(e,!0),r=n.X.length,o=n.names.length,s=t.type===void 0?n.glm?"HC0":"HC1":ke(t.type,Aw),i=s==="HC"?"HC0":s,c=t.cadjust??!0,a=t.multi0??!1,l=t.fix??!1,f=t.sandwich??!0;for(let[b,E]of[["cadjust",c],["multi0",a],["fix",l],["sandwich",f]])if(typeof E!="boolean")throw new TypeError(`'${b}' must be true or false`);let m,u=t.cluster;if(u===void 0)m=[n.inputRows.map(String)];else{let b=y=>Array.isArray(y)||ArrayBuffer.isView(y);if(!b(u))throw new TypeError("'cluster' must be an array of labels or an array of such arrays");let w=u.length>0&&Array.from(u).every(b)?Array.from(u):[u];if(w.length===0)throw new RangeError("'cluster' has no dimension");m=w.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=>Sw(y[x]))})}if(i==="HC1"&&r<=o)throw new RangeError("HC1 covariances need more observations than coefficients");let h=m.length,p=qw(h).map((b,E)=>{let w=a&&h>1&&E===2**h-2,y=w?n.inputRows.map(String):n.inputRows.map((T,A)=>b.length===1?m[b[0]][A]:JSON.stringify(b.map(k=>m[k][A]))),{g:x,G:M}=kw(y);if(M<2)throw new RangeError("at least two clusters are needed");let R=w?i==="HC1"?(r-o)/(r-1):1:c?M/(M-1):1;return{g:x,G:M,c:R,sign:b.length%2===1?1:-1}}),g=b=>{let E=Array.from({length:o},()=>new Array(o).fill(0));for(let{g:w,G:y,c:x,sign:M}of p){let R;i==="HC2"||i==="HC3"?R=Lw(n,b,w,y,i==="HC2"?-.5:-1):y===r?R=n.X.map((A,k)=>b.psi(k)):(R=Array.from({length:y},()=>new Array(o).fill(0)),n.X.forEach((A,k)=>{let C=R[w[k]],_=b.psi(k);for(let I=0;I<o;I++)C[I]+=_[I]}));let T=Fw(R,o);E=E.map((A,k)=>A.map((C,_)=>C+M*x*T[k][_]/r))}return i==="HC1"&&(E=E.map(w=>w.map(y=>y*(r-1)/(r-o)))),E},d=n.res.every(b=>b===0),v=n.phiS>=mc&&n.phiS<1/0&&n.A.every((b,E)=>qr(b[E]))&&n.res.every(Number.isFinite),N;if(v){let b=g(Iw(n));if(!f)bu(b,d)&&(N=l?ic(b):b);else{let E=Yo(n.A,b,r*n.phiS*n.phiS);du(n,b,E,d)&&(N=l?ic(E):E)}}if(N===void 0){let b=n.scaled(),E=g(_w(b)),{mant:w,exp:y}=f?{mant:Yo(b.B,E,r),exp:b.e.map(M=>b.s-M)}:vu(b,E),x=l?Ow(w,y):{mant:w,exp:y};N=kr(x.mant,x.exp)}return N=ac(N,f?n.k:-n.k),J("vcovCL",{vcov:N,names:n.names,warnings:[]},e)}var Pw=["weights","family","link","epsilon","maxit"],Vw=["family","link","variance"],Nu=["power"];function $w(e,t,n){B(n,Pw,"svyglm"),B(Kt(n,"family"),Vw,"svyglm","family"),B(Kt(n,"family.link"),Nu,"svyglm","family.link"),B(Kt(n,"link"),Nu,"svyglm","link");let{weights:r,family:o,link:s,epsilon:i,maxit:c}=n;if(r===void 0)throw new RangeError("'weights' must be given");let a=Array.from(r);if(a.length!==t.length)throw new RangeError("'weights' must have the length of 'y'");if(a.some(S=>!(S>0)||!Number.isFinite(S)))throw new RangeError("'weights' must be positive and finite");if(o==="quasi"||typeof o=="object"&&o!==null&&o.family==="quasi")throw new RangeError("the quasi family is not available in svyglm (its variance function is not known here)");let l=a.length,f=a.reduce((S,O)=>S+O,0)/l,m=a.map(S=>S/f),u=Ze(()=>Yr(e,t,{weights:m,...o===void 0?{}:{family:o},...s===void 0?{}:{link:s},...i===void 0?{}:{epsilon:i},...c===void 0?{}:{maxit:c}})),h=Array.isArray(e[0])||ArrayBuffer.isView(e[0])?e.map(S=>Array.from(S)):Array.from(e,S=>[S]),p=S=>{let O=0;for(let q=0;q<S.length;q++)Number.isFinite(S[q])&&Math.abs(S[q])>O&&(O=Math.abs(S[q]));return O>0?Math.floor(Math.log2(O)):0},g=(h[0]??[]).map((S,O)=>p(h.map(q=>q[O]))),N=(typeof o=="object"&&o!==null?o.family:o??"gaussian")==="gaussian"&&(s===void 0||s==="identity")&&(typeof o!="object"||o===null||o.link===void 0||o.link==="identity")?p(t):0,b=g.every(S=>S===0)&&N===0?u:Ze(()=>Yr(h.map(S=>S.map((O,q)=>$(O,-g[q]))),Array.from(t,S=>$(S,-N)),{weights:m,...o===void 0?{}:{family:o},...s===void 0?{}:{link:s},...i===void 0?{}:{epsilon:i},...c===void 0?{}:{maxit:c}})),E=Ze(()=>uc(b,{type:"HC0"})).vcov,w=[...u.coefficients.length>g.length?[N]:[],...g.map(S=>N-S)],y=E.map((S,O)=>S.map((q,V)=>$(q*l/(l-1),w[O]+w[V]))),x=u.coefficients.length,M=l-x,R=E.map((S,O)=>$(Math.sqrt(S[O]*l/(l-1)),w[O])),T=b.coefficients.map((S,O)=>S/Math.sqrt(E[O][O]*l/(l-1))),A=T.map(S=>2*Bt(-Math.abs(S),M)),k=b.fittedValues,C=b.y,_=u.family==="binomial"||u.family==="quasibinomial"?S=>S*(1-S):u.family==="poisson"||u.family==="quasipoisson"?S=>S:u.family==="Gamma"?S=>S*S:u.family==="inverse.gaussian"?S=>S*S*S:()=>1,I=k.map((S,O)=>(C[O]-S)/Math.sqrt(_(S))),F=I.reduce((S,O,q)=>S+m[q]*O,0)/l,L=0;for(let S=0;S<l;S++)L+=m[S]*(I[S]-F)**2;let D=$(L/l*(l/(l-1)),2*N);return J("svyglm",{names:u.model.columnNames.slice(),coefficients:u.coefficients.slice(),se:R,tValue:T,pValue:A,vcov:y,dfResidual:M,dispersion:D,deviance:u.deviance,nullDeviance:u.nullDeviance,fittedValues:u.fittedValues.slice(),linearPredictors:u.linearPredictors.slice(),iter:u.iter,converged:u.converged,glm:u,warnings:u.warnings.slice()})}function lr(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 zw=new Set(["start","id","ties","weights","strata","init","control","nocenter","confLevel","robust","cluster"]),jw=["eps","iterMax","tolerChol","tolerInf","timefix"],hc=e=>e==null||typeof e=="number"&&Number.isNaN(e),Kw=(e,t)=>typeof e=="number"&&typeof t=="number"?e-t:String(e)<String(t)?-1:String(e)>String(t)?1:0,Bw=2**64,Ww=2**-1022,yu=700;function ca(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(),g=h(),d=0,v=0,N=u(),b=h(),E=u(),w=h(),y=0,x=0,M=R=>{let T=Math.exp(x-R);v*=T,y*=T;for(let A=0;A<m;A++){N[A]*=T,E[A]*=T;let k=b[A],C=w[A];for(let _=0;_<=A;_++)k[_]*=T,C[_]*=T}x=R};for(let R=f-1;R>=0;){c[R]&&(x=0,v=0,N=u(),b=h());let T=r[R],A=!1;if(l!==void 0){let I=R;for(;I>0&&r[I-1]===T&&!c[I-1];)I--;for(let F=I;F<=R;F++)o[F]&&(A=!0)}if(l!==void 0&&A){let I=R;for(;I>0&&!c[I-1];)I--;let F=R;for(;!c[F];)F++;let L=S=>{let O=0;for(let q=0;q<m;q++)O+=t[q]*i[S][q];return O},D=-1/0;for(let S=I;S<=F;S++)l[S]<T&&r[S]>=T&&(D=Math.max(D,L(S)));x=Number.isFinite(D)?D:0,v=0,N=u(),b=h();for(let S=I;S<=F;S++){if(!(l[S]<T&&r[S]>=T)||r[S]===T&&o[S])continue;let O=Math.exp(L(S)-x)*s[S];v+=O;for(let q=0;q<m;q++){N[q]+=O*i[S][q];for(let V=0;V<=q;V++)b[q][V]+=O*i[S][q]*i[S][V]}}}let k=0,C=0;y=0;let _=R;do{let I=i[_],F=0;for(let S=0;S<m;S++)F+=t[S]*I[S];let L=s[_];Number.isFinite(F)&&(F-x>yu?M(F):F-x<-yu&&v===0&&y===0&&(x=F));let D=Math.exp(F-x)*L;if(o[_]){k++,C+=L,y+=D,d+=L*F;for(let S=0;S<m;S++){p[S]+=L*I[S],E[S]+=D*I[S];let O=w[S];for(let q=0;q<=S;q++)O[q]+=D*I[S]*I[q]}}else if(l===void 0){v+=D;for(let S=0;S<m;S++){N[S]+=D*I[S];let O=b[S];for(let q=0;q<=S;q++)O[q]+=D*I[S]*I[q]}}_--}while(_>=0&&r[_]===T&&!c[_]);if(k>0){let I=n?k:1,F=n?k:1,L=C/I;for(let D=0;D<I;D++){v+=n?y/F:y,d-=L*(Math.log(v)+x+a);for(let S=0;S<m;S++){N[S]+=n?E[S]/F:E[S];let O=N[S]/v;p[S]-=L*O;let q=b[S],V=w[S],W=g[S];for(let Q=0;Q<=S;Q++){q[Q]+=n?V[Q]/F:V[Q];let re=q[Q]-O*N[Q],j=L*re;W[Q]+=j<1/0&&j>-1/0&&(Math.abs(j)>=Ww||re===0)||!Number.isFinite(L)?j/v:L*(re/v)}}}E.fill(0);for(let D of w)D.fill(0)}R=_}for(let R=0;R<m;R++)for(let T=0;T<R;T++)g[T][R]=g[R][T];return{loglik:d,score:p,info:g}}function Mu(e,t,n){let{time:r,event:o,x:s,stratumEnd:i}=e,c=Ie([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 g=[];for(let R=u;R<=h;R++)(R===u||r[R]!==r[R-1])&&g.push(R);let d=R=>R+1<g.length?g[R+1]:h+1,v=new Array(g.length),N=new Array(g.length),b=new Array(g.length),E=-1/0,w=0,y=new Array(l).fill(0);for(let R=g.length-1;R>=0;R--){let T=Number.isFinite(E)?E:0;for(let k=g[R];k<d(R);k++)E=Math.max(E,p[k-u]);let A=Number.isFinite(E)?E:0;if(R<g.length-1&&A!==T){let k=Math.exp(T-A);w*=k;for(let C=0;C<l;C++)y[C]*=k}for(let k=g[R];k<d(R);k++){let C=c[k]*Math.exp(p[k-u]-A);w+=C;for(let _=0;_<l;_++)y[_]+=C*s[k][_]}v[R]=w,N[R]=y.slice(),b[R]=A}let x=0,M=new Array(l).fill(0);for(let R=0;R<g.length;R++){let T=b[R];if(R>0&&T!==b[R-1]){let S=Math.exp(T-b[R-1]);x*=S;for(let O=0;O<l;O++)M[O]*=S}let A=g[R],k=d(R),C=[];for(let S=A;S<k;S++)C.push(Math.exp(p[S-u]-T));let _=0,I=0,F=0,L=new Array(l).fill(0);for(let S=A;S<k;S++){if(!o[S])continue;let O=c[S]*C[S-A];_++,I+=c[S],F+=O;for(let q=0;q<l;q++)L[q]+=O*s[S][q]}for(let S=A;S<k;S++){let O=C[S-A],q=s[S],V=f[S];for(let W=0;W<l;W++)V[W]=-O*(q[W]*x-M[W])}if(_===0)continue;let D=n?_:1;for(let S=0;S<D;S++){let O=S/D,q=v[R]-O*F,V=N[R].map((Q,re)=>(Q-O*L[re])/q),W=I/D/q;for(let Q=A;Q<k;Q++){let re=C[Q-A],j=s[Q],ee=f[Q];if(o[Q])for(let U=0;U<l;U++){let ue=j[U]-V[U];ee[U]+=ue/D-(1-O)*re*ue*W}else for(let U=0;U<l;U++)ee[U]-=re*(j[U]-V[U])*W}x+=W;for(let Q=0;Q<l;Q++)M[Q]+=W*V[Q]}}u=h+1}return f}function pc(e,t,n){let r=e.start,{time:o,event:s,x:i,stratumEnd:c}=e,a=Ie([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 g=0;for(let d=0;d<f;d++)g+=t[d]*i[p][d];return g});for(let h=0;h<l;){let p=h;for(;!c[p];)p++;for(let g=h;g<=p;){let d=o[g],v=g;for(;v<p&&o[v+1]===d;)v++;let N=[],b=[],E=-1/0;for(let w=g;w<=v;w++)s[w]&&b.push(w);if(b.length>0)for(let w=h;w<=p;w++)r[w]<d&&o[w]>=d&&(N.push(w),E=Math.max(E,u[w]));if(b.length>0&&N.length>0){Number.isFinite(E)||(E=0);let w=0,y=new Array(f).fill(0),x=0,M=new Array(f).fill(0),R=0;for(let A of N){let k=Math.exp(u[A]-E),C=a[A]*k;w+=C;for(let _=0;_<f;_++)y[_]+=C*i[A][_];if(s[A]&&o[A]===d){R+=a[A],x+=C;for(let _=0;_<f;_++)M[_]+=C*i[A][_]}}let T=n?b.length:1;for(let A=0;A<T;A++){let k=A/T,C=w-k*x,_=R/T/C,I=y.map((F,L)=>(F-k*M[L])/C);for(let F of N){let L=Math.exp(u[F]-E),D=s[F]&&o[F]===d;for(let S=0;S<f;S++){let O=i[F][S]-I[S];D?m[F][S]+=O/T-(1-k)*L*O*_:m[F][S]-=L*O*_}}}}g=v+1}h=p+1}return m}function Ir(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 _r(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 to(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 Go=(e,t)=>({statistic:e,df:t,pValue:Rt(e,t,{lowerTail:!1})});function Uw(e,t,n){let r=e.length,o=()=>new Array(t).fill(NaN),s=Go(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 no(e,t,n,r={}){return B(r,zw,"coxph"),B(Kt(r,"control"),jw,"coxph","control"),J("coxph",Hw(e,t,n,r).result)}function Hw(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:g}=r,{eps:d=1e-9,iterMax:v=20,tolerChol:N=2**-39}=l,b=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(!(b>0&&b<1/0))throw new RangeError("'tolerInf' must be a positive finite number");if(!(Number.isInteger(v)&&v>=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 w=e.length;if(t.length!==w||n.length!==w)throw new RangeError("'time', 'status' and 'x' must have the same length");if(i!==void 0&&i.length!==w)throw new RangeError("'weights' must have the same length as 'time'");if(c!==void 0&&c.length!==w)throw new RangeError("'strata' must have the same length as 'time'");if(h!==void 0&&h.length!==w)throw new RangeError("'cluster' must have the same length as 'time'");if(p!==void 0&&p.length!==w)throw new RangeError("'start' must have the same length as 'time'");if(g!==void 0&&g.length!==w)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=w===0?0:n[0].length;if(y<1)throw new RangeError("'x' must have at least one column");if(n.some(G=>G.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(G=>!Number.isFinite(G)))throw new RangeError("'init' must contain finite values");let x=new At,M=[];for(let G=0;G<w;G++){if(Number.isNaN(e[G])||Number.isNaN(t[G])||n[G].some(Le=>Number.isNaN(Le))||p!==void 0&&Number.isNaN(p[G])||i!==void 0&&Number.isNaN(i[G])||c!==void 0&&hc(c[G])||h!==void 0&&hc(h[G])||g!==void 0&&hc(g[G]))continue;if(p!==void 0&&!(p[G]<e[G]))throw new RangeError("stop time must be > start time");let le=t[G];if(le!==0&&le!==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[G]===-1/0||e[G]===1/0&&le===1)throw new RangeError("event times must be finite");M.push(G)}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(G=>n[G].some(le=>!Number.isFinite(le))))throw new RangeError("'x' contains infinite values");let T=M.map(G=>i===void 0?1:i[G]);if(T.some(G=>!(G>0)))throw new RangeError("Invalid weights, must be >0");if(T.some(G=>G===1/0))throw new RangeError("'weights' must be finite");let A=g===void 0?void 0:Pt(g),k=new Map;if(A!==void 0)for(let G of M){if(t[G]!==1)continue;let le=A[G];k.set(le,(k.get(le)??0)+1)}let C=p!==void 0&&[...k.values()].some(G=>G>1),_=u??(h!==void 0||T.some(G=>!Number.isInteger(G))||C);if(p!==void 0&&u===!0&&h===void 0&&g===void 0)throw new RangeError("one of cluster or id is needed");h!==void 0&&!_&&x.add("cluster specified with robust=FALSE, cluster ignored");let I=0;for(let G of M)I+=t[G];let F=M.map(G=>e[G]),L=p===void 0?void 0:M.map(G=>p[G]),D,S;if(L===void 0)D=E?lr(F):F;else if(!E)D=F,S=L;else{let G=lr([...L,...F]);if(S=G.slice(0,R),D=G.slice(R),S.some((le,Le)=>le>=D[Le]))throw new Error("aeqSurv exception, an interval has effective length 0")}if(I===0){let G=Uw(M.map(le=>n[le]),y,x.list);return A!==void 0&&(G.nId=new Set(M.map(le=>A[le])).size),{result:G}}let O=new Array(R).fill(0),q,V=[];if(c!==void 0){let G=Pt(c);V=Array.from(new Set(M.map(Le=>G[Le]))).sort(Kw);let le=new Map;V.forEach((Le,Ge)=>le.set(Le,Ge)),q=M.map(Le=>G[Le]),O=M.map(Le=>le.get(G[Le]))}let W=Array.from({length:R},(G,le)=>le).sort((G,le)=>O[G]-O[le]||D[G]-D[le]||G-le),Q=W.map(G=>T[G]),{groups:re,k:j}=yt(Q)>=Bw?Ie([Q]):{groups:[Q],k:0},ee=re[0],U=0;for(let G of T)U+=j===0?G:$(G,-j);let ue=W.map(G=>n[M[G]].slice()),Z=new Array(y).fill(0),P=new Array(y).fill(1),Y=new Array(y).fill(0),z=new Set(f);for(let G=0;G<y;G++){if(ue.every(He=>z.has(He[G])))continue;let le=ue.map(He=>He[G]),Le=He=>{let zt=0;for(let Fn=0;Fn<R;Fn++)zt+=(p===void 0?ee[Fn]:1)*He[Fn];let Cn=p===void 0?U:R,wr=zt/Cn,zr=0,ka=He.map((Fn,cs)=>(zr+=(p===void 0?ee[cs]:1)*Math.abs(Fn-wr),Fn-wr)),jr=zr===0?1:Cn/zr;return{m:wr,s:jr,d:ka,ok:Number.isFinite(wr)&&jr>=2**-1022&&jr<1/0}},Ge=Le(le);if(!Ge.ok){let{groups:He,k:zt}=Ie([le]);Ge=Le(He[0]),Y[G]=zt}for(let He=0;He<R;He++)ue[He][G]=Ge.d[He]*Ge.s;Z[G]=$(Ge.m,Y[G]),P[G]=Ge.s}let H={time:W.map(G=>D[G]),...S===void 0?{}:{start:W.map(G=>S[G])},event:W.map(G=>t[M[G]]===1),weight:ee,x:ue,stratumEnd:W.map((G,le)=>le===R-1||O[W[le+1]]!==O[G]),logUnit:j*Math.LN2},K=s==="efron",te=a===void 0?new Array(y).fill(0):a.map((G,le)=>$(G/P[le],Y[le])),se=ca(H,te,K),Me=[se.loglik,se.loglik],Ne=se.info.map(G=>G.slice()),de=Ir(Ne,N),ce=_r(Ne,se.score),qe=0;for(let G=0;G<y;G++)qe+=se.score[G]*ce[G];let we=0,Oe,xe=!1,_e=v===0;if(v===0||!Number.isFinite(se.loglik))Oe=to(Ne);else{let G=se.loglik,le=te.map((Ge,He)=>Ge+ce[He]),Le=0;for(_e=!1,we=1;we<=v;we++){se=ca(H,le,K),Ne=se.info.map(He=>He.slice()),de=Ir(Ne,N);let Ge=Number.isFinite(se.loglik)&&se.score.every(Number.isFinite)&&se.info.every(He=>He.every(Number.isFinite));if(Ge&&(se.loglik===G||Math.abs(1-G/se.loglik)<=d)){_e=!0,Me[1]=se.loglik,te=le;break}!Ge||se.loglik<G?(Le++,le=le.map((He,zt)=>(He+Le*te[zt])/(Le+1))):(Le=0,G=se.loglik,Me[1]=G,ce=_r(Ne,se.score),te=le,le=le.map((He,zt)=>He+ce[zt]))}_e?Oe=to(Ne):(we=p===void 0?v+1:v,xe=!0,p!==void 0?(se=ca(H,te,K),Me[1]=se.loglik,Ne=se.info.map(Ge=>Ge.slice()),de=Ir(Ne,N),Oe=to(Ne)):v>1?(se=ca(H,te,K),Me[1]=se.loglik,Oe=se.info.map((Ge,He)=>Ge.map((zt,Cn)=>He===Cn?Ge[He]>0?1/Ge[He]:0:-0))):Oe=to(Ne))}let Fe=P.map(G=>Math.floor(Math.log2(G))),We=P.map((G,le)=>$(G,-Fe[le])),ze=Fe.map((G,le)=>G-Y[le]),tt=te.map((G,le)=>G*We[le]),Ue=se.score.map((G,le)=>G/We[le]),Xe=Oe.map((G,le)=>G.map((Le,Ge)=>Le*We[le]*We[Ge])),ge=tt.map((G,le)=>$(G,ze[le])),oe=Xe.map((G,le)=>G.map((Le,Ge)=>$(Le,ze[le]+ze[Ge]-j))),ve=ge.slice();if(v>0&&Math.abs(de)<y)for(let G=0;G<y;G++)Oe[G][G]===0&&(ve[G]=NaN);let be=p===void 0?ge:ge.map((G,le)=>Number.isNaN(ve[le])?0:G),fe=0;for(let G=0;G<y;G++)fe+=be[G]*Z[G];let nt=M.map(G=>{let le=0;for(let Le=0;Le<y;Le++)le+=n[G][Le]*be[Le];return le-fe});if(v>1){let G=Xe.map((le,Le)=>{let Ge=0;for(let He=0;He<y;He++)Ge+=Ue[He]*Xe[He][Le];return Math.abs(Ge)});if(xe)(p===void 0||v>1)&&x.add("Ran out of iterations and did not converge"),p===void 0&&(yt(nt)>500||G.some(le=>!Number.isFinite(le)))&&x.add("one or more coefficients may be infinite");else if(p===void 0){let le=[];for(let Le=0;Le<y;Le++){let Ge=G[Le];(!Number.isFinite(Ue[Le])||$(Ge,ze[Le])>d&&Ge>b*Math.abs(tt[Le]))&&le.push(Le+1)}le.length>0&&x.add("Loglik converged before variable "+le.join(",")+" ; coefficient may be infinite. ")}}let X=ve.map((G,le)=>Number.isNaN(G)?-1:le).filter(G=>G>=0),he=X.length,Te,Ee=0,Ae=0,ne;if(_&&he>0){let G=W.map((lt,Qe)=>Qe),le=R,Le=h??(p===void 0?void 0:g);if(Le!==void 0){let lt=Pt(Le),Qe=new Map;G=W.map(dt=>{let Mt=lt[M[dt]],Et=Qe.get(Mt);return Et===void 0&&(Et=Qe.size,Qe.set(Mt,Et)),Et}),le=Qe.size}if(le<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((dt,Mt)=>dt/We[Mt])),He=Ge(p===void 0?Mu(H,te,K):pc(H,te,K)),zt=He.map(lt=>Xe.map((Qe,dt)=>{let Mt=0;for(let Et=0;Et<y;Et++)Mt+=lt[Et]*Xe[Et][dt];return Mt})),Cn;if(j===0)Cn=zt.map((lt,Qe)=>lt.map(dt=>ee[Qe]*dt));else{let lt=dt=>dt===0||!Number.isFinite(dt)?0:Math.floor(Math.log2(Math.abs(dt))),Qe=-1/0;zt.forEach((dt,Mt)=>dt.forEach(Et=>Et!==0&&Number.isFinite(Et)&&(Qe=Math.max(Qe,lt(ee[Mt])+lt(Et))))),Ee=Qe===-1/0?0:-Qe,Cn=zt.map((dt,Mt)=>{let Et=lt(ee[Mt]);return dt.map(br=>{let sl=lt(br);return $($(ee[Mt],-Et)*$(br,-sl),Et+sl+Ee)})})}let wr=lt=>{let Qe=Array.from({length:le},()=>new Array(y).fill(0));return lt.forEach((dt,Mt)=>{let Et=Qe[G[Mt]];for(let br=0;br<y;br++)Et[br]+=dt[br]}),Qe},zr=lt=>{let Qe=Array.from({length:y},()=>new Array(y).fill(0));for(let dt of lt)for(let Mt=0;Mt<y;Mt++)for(let Et=0;Et<y;Et++)Qe[Mt][Et]+=dt[Mt]*dt[Et];return Qe};Te=zr(wr(Cn));let ka=Ie([Q]).groups[0],jr=a===void 0?new Array(y).fill(0):a.map((lt,Qe)=>$(lt/P[Qe],Y[Qe])),Fn=p===void 0?Mu(H,jr,K):pc(H,jr,K),cs=wr(Ge(Fn).map((lt,Qe)=>lt.map(dt=>ka[Qe]*dt))),Mo=new Array(y).fill(0);for(let lt of cs)for(let Qe=0;Qe<y;Qe++)Mo[Qe]+=lt[Qe];let qa=zr(cs);if(y===1)Ae=Mo[0]*Mo[0]/qa[0][0];else{Ir(qa,N);let lt=_r(qa,Mo);for(let Qe=0;Qe<y;Qe++)Ae+=Mo[Qe]*lt[Qe]}let rl=new Array(R),ol=new Array(R);W.forEach((lt,Qe)=>{rl[lt]=He[Qe].map((dt,Mt)=>$(dt,-ze[Mt])),ol[lt]=Cn[Qe].map((dt,Mt)=>$(dt,ze[Mt]-Ee))}),ne={scoreResiduals:rl,dfbeta:ol}}let ye=Te!==void 0,me=Te??Xe,Se=Te===void 0?-j:-2*Ee,Ke=X.map(G=>tt[G]-(a===void 0?0:$(a[G],-ze[G]))),$e=0;if(he===1)$e=Ke[0]*Ke[0]/me[X[0]][X[0]];else if(he>1){let G=X.map(Le=>X.map(Ge=>me[Le][Ge]));Ir(G,N);let le=_r(G,Ke);for(let Le=0;Le<he;Le++)$e+=Ke[Le]*le[Le]}$e=$($e,-Se);let st=(1-m)/2,mt=st<1e-8?ie(st,{lowerTail:!1}):ie((1+m)/2),Ct=(G,le)=>{let Le=Math.floor(le/2),Ge=G.map((He,zt)=>Math.sqrt($(He[zt],le-2*Le)));return{unit:Ge,h:Le,se:Ge.map((He,zt)=>$(He,ze[zt]+Le))}},Gt=Ct(Xe,-j).se,{unit:Yn,h:Be,se:je}=Ct(me,Se),St=ve.map((G,le)=>$((Number.isNaN(G)?G:tt[le])/Yn[le],-Be)),pt=St.map(G=>Rt(G*G,1,{lowerTail:!1})),Rn=ve.map((G,le)=>Math.exp(G-mt*je[le])),$t=ve.map((G,le)=>Math.exp(G+mt*je[le])),It=$(-2*(Me[0]-Me[1]),j),ut=[$(Me[0],j),$(Me[1],j)],Xt=$(qe,j),sn=Te?.map((G,le)=>G.map((Le,Ge)=>$(Le,ze[le]+ze[Ge]-2*Ee))),en={coefficients:ve,var:sn??oe,loglik:ut,score:Xt,wald:$e,iter:we,means:Z,n:R,nevent:I,...A===void 0?{}:{nId:new Set(M.map(G=>A[G])).size},linearPredictors:nt,se:Gt,z:St,p:pt,stdError:Gt,zValue:St,pValue:pt,lower:Rn,upper:$t,logtest:Go(It,he),sctest:Go(Xt,he),waldtest:Go($e,he),rsq:{rsq:1-Math.exp(-It/R),maxrsq:1-Math.exp(2*ut[0]/R)},usedRobust:ye,...ye?{naiveVar:oe,robustSe:je,rscore:Ae,robscore:Go(Ae,he)}:{},warnings:x.list},Ft=se.score.map((G,le)=>$(G/P[le],Y[le]+j));Object.defineProperty(en,"coxZphData",{enumerable:!1,value:{time:D.slice(),...S===void 0?{}:{start:S.slice()},status:M.map(G=>t[G]===1),weights:T.slice(),...q===void 0?{}:{strata:q.slice()},x:M.map(G=>n[G].slice()),sortedToInput:W.slice(),sortedTime:H.time.slice(),...H.start===void 0?{}:{sortedStart:H.start.slice()},sortedEvent:H.event.slice(),sortedWeight:H.weight.slice(),sortedX:H.x.map(G=>G.slice()),stratumIndex:W.map(G=>O[G]),stratumEnd:H.stratumEnd.slice(),betaScaled:te.slice(),inverseCovariateScale:We.map((G,le)=>$(1/G,-ze[le])),weightExp:j,ties:s}});let tn=ne?.scoreResiduals;if(p!==void 0&&o&&tn===void 0){let G=pc(H,te,K);tn=new Array(R),W.forEach((le,Le)=>{tn[le]=G[Le].map((Ge,He)=>$(Ge/We[He],-ze[He]))})}return{result:en,...ne,...tn===void 0?{}:{scoreResiduals:tn},first:Ft,converged:_e}}var Yw=["rho"],Gw=new Set(["group","start","id","weights","ctype","stype","robust","rmean","confType","confInt"]),dc=e=>e==null||typeof e=="number"&&Number.isNaN(e);function Xw(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&&dc(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=lr(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=${Nt(a)}`)}}function Qw(e,t,n,r={}){B(r,Yw,"survdiff");let{rho:o=0}=r;if(!Number.isFinite(o))throw new RangeError("'rho' must be a finite number");let{obs:s,labels:i}=Xw(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 b of s)a[b.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 b=0;b<s.length;){let E=s[b].time,w=b,y=new Array(c).fill(0),x=new Array(c).fill(0),M=0;for(;w<s.length&&s[w].time===E;){let R=s[w];x[R.group]++,R.event&&(y[R.group]++,M++),w++}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 A=0;A<c;A++)for(let k=0;k<c;k++)m[A][k]+=T*u[A]*((A===k?1:0)-u[k]/h)}p*=1-M/h}for(let R=0;R<c;R++)u[R]-=x[R];h-=w-b,b=w}let g=f.map((b,E)=>b>0?E:-1).filter(b=>b>=0),d=0,v=Math.max(g.length-1,0);if(g.length>=2){let b=g.slice(1),E=b.map(y=>l[y]-f[y]),w=b.map(y=>b.map(x=>m[y][x]));try{let y=rt(w,E);d=E.reduce((x,M,R)=>x+M*y[R],0)}catch{({chisq:d,df:v}=Zw(w,E))}}let N=v===0?NaN:Rt(d,v,{lowerTail:!1});return{n:a,groups:i,obs:l,exp:f,var:m,chisq:d,df:v,pvalue:N,pValue:N,warnings:[]}}function Zw(e,t){let n=Ut(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 Lr(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 gn(e[r],e[s])}return e[r]}function fr(e,t,n={}){B(n,Gw,"survfit");let{group:r,start:o,id:s,weights:i,ctype:c=1,stype:a=1,rmean:l="common",confType:f="log",confInt:m=.95}=n;if(typeof f!="string")throw new TypeError("confType must be a string");let u=["log","log-log","plain","none","logit","arcsin"],h=typeof f=="string"?u.filter(q=>q.startsWith(f)):[],p=u.includes(f)?f:h.length===1?h[0]:f;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(!u.some(q=>q===p))throw new RangeError("'arg' should be one of \u201Clog\u201D, \u201Clog-log\u201D, \u201Cplain\u201D, \u201Cnone\u201D, \u201Clogit\u201D, \u201Carcsin\u201D");if(typeof m!="number")throw new TypeError("confInt must be a number");if(!(m>0&&m<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'");let g=n.robust??(i!==void 0&&Array.from(i).some(q=>q!=null&&Number.isFinite(q)&&!Number.isInteger(q)));if(typeof g!="boolean")throw new TypeError("'robust' must be a boolean");if(g&&a===2&&i!==void 0&&Array.from(i).some(q=>q!=null&&Number.isFinite(q)&&!Number.isInteger(q)))throw new RangeError("non-integer weights with stype 2 and the robust variance are not supported");if(typeof l!="number"&&typeof l!="string")throw new TypeError("rmean must be a number or string");if(typeof l=="number"&&!Number.isFinite(l))throw new RangeError("'rmean' must be finite");if(typeof l!="number"&&l!=="none"&&l!=="common"&&l!=="individual")throw new RangeError("'arg' should be one of \u201Cnone\u201D, \u201Ccommon\u201D, \u201Cindividual\u201D");let d=[];for(let q=0;q<e.length;q++){let V=i?.[q];if(i&&V!=null&&typeof V!="number")throw new TypeError("weights must contain numbers");if(i&&(V==null||Number.isNaN(V)))continue;let W=V??1;if(!Number.isFinite(W))throw new RangeError("weights must be finite");if(W<0)throw new RangeError("weights must be non-negative");if(e[q]!=null&&typeof e[q]!="number")throw new TypeError("'time' must contain numbers");if(t[q]!=null&&typeof t[q]!="number")throw new TypeError("'status' must contain numbers");if(o!==void 0&&o[q]!=null&&typeof o[q]!="number")throw new TypeError("'start' must contain numbers");if(!(e[q]==null||t[q]==null||Number.isNaN(e[q])||Number.isNaN(t[q])||o!==void 0&&(o[q]==null||Number.isNaN(o[q]))||r!==void 0&&dc(r[q])||s!==void 0&&dc(s[q]))){if(o!==void 0&&!(o[q]<e[q]))throw new RangeError("stop time must be > start time");if(t[q]!==0&&t[q]!==1)throw new RangeError("'status' must be 0 (censored) or 1 (event)");if(e[q]===-1/0||e[q]===1/0&&t[q]===1)throw new RangeError("event times must be finite");d.push(q)}}if(d.length===0)throw new RangeError("no observations without missing values");let v=r===void 0?[]:Pt(r),N=s===void 0?[]:Array.from(s),b=r===void 0?["all"]:Array.from(new Set(d.map(q=>v[q]))).sort((q,V)=>typeof q=="number"&&typeof V=="number"?q-V:String(q)<String(V)?-1:String(q)>String(V)?1:0).map(q=>`group=${Nt(q)}`),E=new Map;r!==void 0&&Array.from(new Set(d.map(q=>v[q]))).sort((q,V)=>typeof q=="number"&&typeof V=="number"?q-V:String(q)<String(V)?-1:String(q)>String(V)?1:0).forEach((q,V)=>E.set(q,V));let w=new Map;s!==void 0&&Array.from(new Set(d.map(q=>N[q]))).forEach((q,V)=>w.set(q,V));let y=d.flatMap(q=>o===void 0?[e[q]]:[o[q],e[q]]),x=lr(y),M=new Map,R=new Map;d.forEach((q,V)=>{if(o===void 0)R.set(q,x[V]);else if(M.set(q,x[2*V]),R.set(q,x[2*V+1]),!(M.get(q)<R.get(q)))throw new Error("aeqSurv exception, an interval has effective length 0")});let T=d.map(q=>({time:R.get(q),start:o===void 0?-1/0:M.get(q),event:t[q]===1,group:r===void 0?0:E.get(v[q]),id:s===void 0?q:w.get(N[q]),weight:i?.[q]??1})).sort((q,V)=>q.time-V.time),A=s===void 0?void 0:new Map;if(A)for(let q of T){let V=A.get(q.id)??[];V.push(q),A.set(q.id,V)}let k=b,C=T.map(q=>q.time),_=Math.min(...C),I=Math.min(0,_);if(typeof l=="number"&&l<=I)throw new RangeError("'rmean' must be greater than the integration origin");let F=(1-m)/2,L=F<1e-8?ie(F,{lowerTail:!1}):ie(1-F),D=[],S={strata:[],time:[],nRisk:[],nEvent:[],nCensor:[],surv:[],stdErr:D,stdError:D,lower:[],upper:[],cumhaz:[],stdChaz:[],logse:!g||c===2,n:[],records:[],nMax:[],nStart:[],events:[],rmean:[],seRmean:[],median:[],medianRows:k,table:{colNames:[],rowNames:k,table:[]},warnings:[]};i!==void 0&&(S.counts={nRisk:[],nEvent:[],nCensor:[]});let O=[];for(let q=0;q<k.length;q++){let V=T.filter(ge=>ge.group===q);if(!V.some(ge=>ge.weight>0))throw new Error("a group has no positive weight");let W=S.time.length,Q=V.length,re=Math.max(...V.map(ge=>ge.time)),j=0,ee=0,U=1,ue=0,Z=0,P=0;if(o===void 0){let ge=0;for(let oe of V)ge+=oe.weight;j=ge,ee=V.length}let Y=[],z=[],H=[],K=s===void 0?void 0:new Map;if(K)for(let ge of V){let oe=K.get(ge.id)??[];oe.push(ge),K.set(ge.id,oe)}for(let ge=0;ge<V.length;){let oe=V[ge].time,ve=ge,be=0,fe=0,nt=0,X=ee;for(;ve<V.length&&V[ve].time===oe;)ve++;let he=V.slice(ge,ve);if(o!==void 0){let Be=V.filter(je=>je.start<oe&&je.time>=oe);j=Be.reduce((je,St)=>je+St.weight,0),ee=Be.length,X=ee}let Te=he.filter(Be=>Be.event),Ee=he.filter(Be=>!Be.event&&(A===void 0||!(A.get(Be.id)??[]).some(je=>je.start===oe&&je.time>oe)));for(let Be of Te)be+=Be.weight,fe++;for(let Be of Ee)nt+=Be.weight;let Ae=Te.length,ne=Ee.length;if(s!==void 0&&Te.length===0&&Ee.length===0){ge=ve;continue}let ye=0,me=0;if(be>0){if(c===1)ye=be/j,me=be/(j*j);else for(let je=0;je<fe;je++){let St=be/fe,pt=j-je*St;ye+=St/pt,me+=St/(pt*pt)}ue+=ye,P+=me;let Be=fe===X&&X>0?1:be/j;a===1?(U*=1-Be,Z+=Be<1?be/j/(j-be):1/0):U=Math.exp(-ue),H.push({t:oe,d:be,n:j,e:fe,S:U})}let Se=Math.sqrt(a===1?Z:P),Ke=0,$e=0;if(g){let Be=0,je=0,St=K===void 0?V:Array.from(K.values(),pt=>pt[0]);for(let pt of St){let Rn=K?.get(pt.id)??[pt],$t=0,It=0;for(let ut of H){if(ut.t>oe)break;let Xt=Rn.some(Ft=>Ft.start<ut.t&&Ft.time>=ut.t),sn=Rn.some(Ft=>Ft.time===ut.t&&Ft.event),en=ut.d/ut.n;if(c===1)It+=(Number(sn)-Number(Xt)*en)/ut.n;else{let Ft=0,tn=0;for(let G=0;G<ut.e;G++){let le=ut.d/ut.e,Le=ut.n-G*le;Ft+=(Le+G*le)/(ut.e*Le*Le),tn-=le/(Le*Le)}It+=Ft*Number(sn)+tn*Number(Xt)}a===1&&en<1&&($t-=U*(Number(sn)-Number(Xt)*en)/((1-en)*ut.n))}Be+=(pt.weight*$t)**2,je+=(pt.weight*It)**2}Ke=Math.sqrt(Be),$e=Math.sqrt(je)}let st=g?c===2?a===1?Ke:$e:a===1?U===0?NaN:Ke/U:$e:Se,mt=g?c===2?U*(a===1?Ke:$e):a===1?Ke:U*$e:U*Se,Ct=NaN,Gt=NaN;if(p!=="none")if(p==="log")Ct=U===0?mt===0?0:NaN:U*Math.exp(-L*st),Gt=U===0?mt===0?0:NaN:Math.min(1,U*Math.exp(L*st));else if(p==="log-log")if(U===0||U===1)Ct=mt===0?U:NaN,Gt=Ct;else{let Be=Math.log(-Math.log(U));Ct=Math.exp(-Math.exp(Be-L*st/Math.log(U))),Gt=Math.exp(-Math.exp(Be+L*st/Math.log(U)))}else if(p==="plain")U===0&&mt===1/0?(Ct=NaN,Gt=NaN):(Ct=Math.max(0,U-L*mt),Gt=Math.min(1,U+L*mt));else if(p==="logit")if(U===0||U===1)Ct=mt===0?U:NaN,Gt=Ct;else{let Be=Math.log(U/(1-U)),je=L*mt/(U*(1-U));Ct=1-1/(1+Math.exp(Be-je)),Gt=1-1/(1+Math.exp(Be+je))}else if(U>0&&U<1){let Be=Math.asin(Math.sqrt(U)),je=L*mt/(2*Math.sqrt(U*(1-U)));Ct=Math.sin(Math.max(0,Be-je))**2,Gt=Math.sin(Math.min(Math.PI/2,Be+je))**2}else Ct=NaN,Gt=NaN;let Yn=j;S.strata.push(k[q]),S.time.push(oe),S.nRisk.push(Yn),S.nEvent.push(be),S.nCensor.push(nt),S.surv.push(U),S.cumhaz.push(ue),S.stdChaz.push(g?$e:Math.sqrt(P)),S.stdErr.push(g?c===2?U*(a===1?Ke:$e):a===1?Ke:U*$e:U*Se),S.lower.push(Ct),S.upper.push(Gt),S.counts&&(S.counts.nRisk.push(X),S.counts.nEvent.push(Ae),S.counts.nCensor.push(ne)),o===void 0&&(j-=be+nt,ee-=ve-ge),ge=ve}let te=S.time.slice(W),se=S.surv.slice(W),Me=S.lower.slice(W),Ne=S.upper.slice(W);S.median.push([Lr(te,se),p==="none"?NaN:Lr(te,Me),p==="none"?NaN:Lr(te,Ne)]),S.n.push(Q),S.records.push(Q);let de=S.nRisk.slice(W);S.nMax.push(Math.max(...de)),S.nStart.push(de[0]??0),S.events.push(V.reduce((ge,oe)=>ge+(oe.event?oe.weight:0),0));let ce=l==="none"?NaN:l==="common"?Math.max(...C):l==="individual"?re:l;if(l!=="none"&&!Number.isFinite(ce))throw new RangeError("'rmean' must be finite");let qe=V.filter(ge=>Number.isFinite(ge.time)).map(ge=>Math.abs(ge.time)),we=Number.isFinite(ce)?Math.max(Math.abs(ce),Math.abs(I),...qe):Math.max(Math.abs(I),...qe),Oe=we===0?0:Math.max(-1074,Math.min(1023,Math.floor(Math.log2(we)))),xe=we===0?1:2**Oe,_e=Number.isFinite(ce)&&!Number.isFinite(ce-I),Fe=ge=>{let oe=1;for(let ve of H)if(ve.t<=ge)oe=ve.S;else break;return oe},We=ge=>{let oe=1;for(let ve of H)if(ve.t/xe<=ge)oe=ve.S;else break;return oe},ze=Pn();if(_e){let ge=Array.from(new Set([I/xe,...H.map(oe=>oe.t/xe).filter(oe=>oe>I/xe&&oe<ce/xe),ce/xe])).sort((oe,ve)=>oe-ve);for(let oe=0;oe<ge.length-1;oe++){let ve=ge[oe],be=ge[oe+1];dn(ze,We(ve)*(be-ve),ge.length+1)}}else{let ge=Array.from(new Set([I,...H.map(oe=>oe.t).filter(oe=>oe>I&&oe<ce),ce])).sort((oe,ve)=>oe-ve);for(let oe=0;oe<ge.length-1;oe++){let ve=ge[oe],be=ge[oe+1];dn(ze,Fe(ve)*(be-ve),ge.length+1)}}let tt=Vn(ze)*(_e?xe:1),Ue=Pn();if(Number.isFinite(ce))for(let ge of H){if(ge.t>ce||ge.n<=ge.d)continue;let oe=Pn();if(_e){let be=Array.from(new Set([ge.t/xe,...H.map(fe=>fe.t/xe).filter(fe=>fe>ge.t/xe&&fe<ce/xe),ce/xe])).sort((fe,nt)=>fe-nt);for(let fe=0;fe<be.length-1;fe++)dn(oe,We(be[fe])*(be[fe+1]-be[fe]),be.length+1)}else{let be=Array.from(new Set([ge.t,...H.map(fe=>fe.t).filter(fe=>fe>ge.t&&fe<ce),ce])).sort((fe,nt)=>fe-nt);for(let fe=0;fe<be.length-1;fe++)dn(oe,Fe(be[fe])*((be[fe+1]-be[fe])/xe),be.length+1)}let ve=Vn(oe);dn(Ue,ve*ve*ge.d/(ge.n*(ge.n-ge.d)),H.length+1)}let Xe=Vn(Ue);S.rmean.push(l==="none"?NaN:tt),S.seRmean.push(l==="none"?NaN:Math.sqrt(Xe)*xe),O.push(l==="none"?p==="none"?[Q,S.nMax[q],S.nStart[q],S.events[q],S.median[q][0]]:[Q,S.nMax[q],S.nStart[q],S.events[q],S.median[q][0],S.median[q][1],S.median[q][2]]:p==="none"?[Q,S.nMax[q],S.nStart[q],S.events[q],tt,Math.sqrt(Xe)*xe,S.median[q][0]]:[Q,S.nMax[q],S.nStart[q],S.events[q],tt,Math.sqrt(Xe)*xe,S.median[q][0],S.median[q][1],S.median[q][2]])}return S.table={colNames:[...l==="none"?["records","n.max","n.start","events","median"]:["records","n.max","n.start","events","rmean","se(rmean)","median"],...p==="none"?[]:[`${m}LCL`,`${m}UCL`]],rowNames:k,table:O},J("survfit",S)}var Jw=new Set(["newdata","confType","confInt","times","ties","weights","init","control","nocenter","confLevel","robust"]),e1=["eps","iterMax","tolerChol","tolerInf","timefix"];function t1(e,t,n,r={}){B(r,Jw,"survfitCoxph"),B(Kt(r,"control"),e1,"survfitCoxph","control");let{newdata:o,confType:s="log",confInt:i=.95,times:c,...a}=r;if(!["log","log-log","plain","none"].includes(s))throw new RangeError(`invalid 'confType': ${String(s)}`);if(!(i>0&&i<1))throw new RangeError("'confInt' must be between 0 and 1");let l=Ze(()=>no(e,t,n,a)),{ties:f="efron",weights:m}=a,u=(n[0]??[]).map((U,ue)=>n1(n.map(Z=>Z[ue]))),h=n.map(U=>U.map((ue,Z)=>u[Z](ue))),p=Ze(()=>no(e,t,h,a)),g=p.coefficients.length,d=p.coefficients.map(U=>Number.isFinite(U)?U:0),v=p.var.map(U=>U.map(ue=>Number.isFinite(ue)?ue:0)),N=[];for(let U=0;U<e.length;U++)Number.isNaN(e[U])||Number.isNaN(t[U])||n[U].some(ue=>Number.isNaN(ue))||m!==void 0&&Number.isNaN(m[U])||N.push(U);let b=a.control?.timefix===!1?e.slice():lr(e);N.sort((U,ue)=>b[U]-b[ue]);let E=U=>m===void 0?1:m[U],w=U=>U.map((ue,Z)=>ue-p.means[Z]),y=new Map;for(let U of N)y.set(U,Math.exp(w(h[U]).reduce((ue,Z,P)=>ue+Z*d[P],0)));let x=[];for(let U of N)(x.length===0||x[x.length-1]!==b[U])&&x.push(b[U]);let M=x.length,R=new Array(M).fill(0),T=new Array(M).fill(0),A=Array.from({length:M},()=>new Array(g).fill(0)),k=new Array(M).fill(0),C=new Array(M).fill(0),_=new Array(M).fill(0),I=0,F=0,L=new Array(g).fill(0),D=N.length-1;for(let U=M-1;U>=0;U--){let ue=x[U],Z=0,P=0,Y=0,z=new Array(g).fill(0);for(;D>=0&&b[N[D]]===ue;D--){let K=N[D],te=E(K),se=y.get(K),Me=w(h[K]);I+=te*se,F+=te;for(let Ne=0;Ne<g;Ne++)L[Ne]+=te*se*Me[Ne];if(t[K]===1){Z++,P+=te,Y+=te*se;for(let Ne=0;Ne<g;Ne++)z[Ne]+=te*se*Me[Ne]}else _[U]+=te}if(k[U]=F,C[U]=P,Z===0)continue;let H=f==="efron"?Z:1;for(let K=0;K<H;K++){let te=f==="efron"?K/Z:0,se=I-te*Y,Ne=P/H/se;R[U]+=Ne,T[U]+=Ne/se;for(let de=0;de<g;de++)A[U][de]+=Ne*((L[de]-te*z[de])/se)}}if(o?.some(U=>U.length!==g))throw new RangeError("each row of 'newdata' must have one value per column of 'x'");let S=(o??[l.means]).map(U=>U.map((ue,Z)=>u[Z](ue))),O=i<1-1e-8?ie(1-(1-i)/2):ie((1-i)/2,{lowerTail:!1}),q=[],V=[],W=[],Q=[],re=[];for(let U of S){let ue=w(U),Z=Math.exp(ue.reduce((Ne,de,ce)=>Ne+de*d[ce],0)),P=0,Y=0,z=new Array(g).fill(0),H=[],K=[],te=[],se=[],Me=[];for(let Ne=0;Ne<M;Ne++){P+=R[Ne],Y+=T[Ne];for(let _e=0;_e<g;_e++)z[_e]+=ue[_e]*R[Ne]-A[Ne][_e];let de=0;for(let _e=0;_e<g;_e++)for(let Fe=0;Fe<g;Fe++)de+=z[_e]*v[_e][Fe]*z[Fe];let ce=Z*P,qe=Math.exp(-ce),we=Z*Math.sqrt(Y+de);Me.push(ce),H.push(qe),K.push(we);let[Oe,xe]=r1(qe,we,O,s);te.push(Oe),se.push(xe)}q.push(H),V.push(K),W.push(te),Q.push(se),re.push(Me)}let j=q.map((U,ue)=>[Lr(x,U),Lr(x,W[ue]),Lr(x,Q[ue])]),ee={fit:l,time:x,nRisk:k,nEvent:C,nCensor:_,surv:q,stdErr:V,lower:W,upper:Q,cumhaz:re,median:j,records:N.length,nStart:k[0]??0,events:C.reduce((U,ue)=>U+ue,0),warnings:l.warnings.slice()};return c!==void 0&&(ee.at=o1(ee,c)),J("survfitCoxph",ee)}function n1(e){let t=s=>{let i=0;for(let c of s)Number.isFinite(c)&&Math.abs(c)>i&&(i=Math.abs(c));return i>0?Math.floor(Math.log2(i)):0},n=t(e),r=$(e.find(s=>Number.isFinite(s))??0,-n),o=t(e.map(s=>$(s,-n)-r));return s=>$($(s,-n)-r,-o)}function r1(e,t,n,r){if(r==="none"||!Number.isFinite(t))return[NaN,NaN];if(r==="plain")return[Math.max(0,e-n*t*e),Math.min(1,e+n*t*e)];if(e===0)return[NaN,NaN];if(r==="log")return[Math.exp(Math.log(e)-n*t),Math.min(1,Math.exp(Math.log(e)+n*t))];if(e===1)return[NaN,NaN];let o=Math.log(-Math.log(e)),s=t/Math.log(e);return[Math.exp(-Math.exp(o-n*s)),Math.exp(-Math.exp(o+n*s))]}function o1(e,t){let n=[...t].sort((s,i)=>s-i),r={time:[],surv:e.surv.map(()=>[]),stdErr:e.surv.map(()=>[]),lower:e.surv.map(()=>[]),upper:e.surv.map(()=>[]),nRisk:[],nEvent:[]},o=0;for(let s of n){let i=-1;for(;i+1<e.time.length&&e.time[i+1]<=s;)i++;if(s>e.time[e.time.length-1])break;r.time.push(s);let c=e.time.findIndex(l=>l>=s);r.nRisk.push(c<0?0:e.nRisk[c]);let a=0;for(let l=o;l<=i;l++)a+=e.nEvent[l];r.nEvent.push(a),o=i+1,e.surv.forEach((l,f)=>{r.surv[f].push(i<0?1:e.surv[f][i]),r.stdErr[f].push(i<0?0:e.stdErr[f][i]),r.lower[f].push(i<0?1:e.lower[f][i]),r.upper[f].push(i<0?1:e.upper[f][i])})}return r}var s1=["vcov","df"],a1=["parm","level","vcov","df"];function Eu(e){return typeof e=="object"&&e!==null&&!Array.isArray(e)&&Array.isArray(e.vcov)&&Array.isArray(e.names)}function xu(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 Ru=e=>new Set(e).size===e.length;function Tu(e,t){aa(e);let n=e.coefficients,r=ia(e),o=n.length,s=n.map(m=>Number.isNaN(m)),i,c,a=[],l;if(t===void 0){let m=fc(e),u=new Map(m.names.map((p,g)=>[p,g])),h=r.map((p,g)=>s[g]?void 0:u.get(p));i=h.map(p=>h.map(g=>p===void 0||g===void 0?NaN:m.vcov[p][g])),c=r,l=h.map(p=>p===void 0?NaN:lc(m.vcov,p,m.vcov[p][p])??Math.sqrt(m.vcov[p][p]))}else{let m=typeof t=="function"?Ze(()=>t(e)):t,u=Eu(m)?m.vcov:m;if(Eu(m)){if(i=xu(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=xu(m);l=i.map((h,p)=>{let g=h[p],d=typeof u=="object"&&u!==null?lc(u,p,g):void 0;if(d!==void 0&&!Number.isNaN(d))return d;if(!Number.isFinite(g))throw new RangeError(`the variance of coefficient ${p+1} in 'vcov' is not a finite number`);if(g<0)throw new RangeError(`the variance of coefficient ${p+1} in 'vcov' is negative`);return Math.sqrt(g)})}if(c!==void 0&&Ru(c)&&Ru(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 Au(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 Su=e=>Number.isFinite(e)&&e>0;function i1(e){return cr(e)?e.priorWeights.filter(t=>t!==0).length:e.rank+e.dfResidual}function c1(e,t={}){B(t,s1,"coeftest");let{est:n,se:r,names:o,warnings:s}=Tu(e,t.vcov),i=Au(e,t.df),c=Su(i),a=n.map((f,m)=>f/r[m]),l=a.map(f=>Number.isNaN(f)?NaN:c?2*gt(-Math.abs(f),i,!0,!1):2*ae(-Math.abs(f)));return J("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:i1(e),warnings:s},e,typeof t.vcov=="function"?void 0:t.vcov)}function l1(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 f1(e,t={}){B(t,a1,"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=Tu(e,t.vcov),o=Au(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]=Su(o)?[wt(c,o,!0,!1),c<1e-8?wt(c,o,!1,!1):wt(1-c,o,!0,!1)]:[ie(c),c<1e-8?ie(c,{lowerTail:!1}):ie(1-c)],f=(m,u,h)=>m+h*u;return J("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:l1(c),warnings:r.warnings},e,typeof t.vcov=="function"?void 0:t.vcov)}var m1=["test","dispersion"],u1=(e,t)=>bt(e,t/2,2,!1,!1);function h1(e,t,n){let{fit:r,w:o}=ko(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 gc=(e,t)=>rc(t.x,e.fit.workingResiduals,e.fit.workingWeights,e.intercept);function p1(e,t={}){B(t,m1,"glmAnova");let n=Array.isArray(e)?e:[e];if(n.length===0)throw new RangeError("no fits to compare");let r=n.map(Kn),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:Wl(t.dispersion,o),c=i?.s??0,a=d1(s,Array.isArray(e)&&s.length>1,{...t,...i===void 0?{}:{dispersion:i.m}},c);if(o===0&&c===0)return J("glmAnova",a,...n);let l=new Set(["Deviance","Resid. Dev","Rao","Cp"]),f=a.colNames.map(u=>l.has(u)),m=a.table.map(u=>u.map((h,p)=>f[p]?$(h,2*o):h));if(c!==0&&a.colNames.includes("Cp")){let u=a.colNames.indexOf("Cp"),h=a.colNames.indexOf("Resid. Dev"),p=a.colNames.indexOf("Resid. Df"),g=s[0].model.x.length;for(let d of m)d[u]=d[h]+2*(g-d[p])*t.dispersion}return J("glmAnova",{...a,table:m},...n)}function d1(e,t,n,r=0){let o=new At,s=e[0],i=za(s.model.familySpec);if(e.some(M=>{let R=za(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,A)=>T.length===A.length&&T.every((k,C)=>JSON.stringify(k)===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[k,C]=f[A]>0?[e[A-1],T]:[T,e[A-1]],_=L=>L.filter((D,S)=>k.priorWeights[S]>0&&C.priorWeights[S]>0),I=k.model.offset.map((L,D)=>L-C.model.offset[D]),F=I.some((L,D)=>L!==0&&k.priorWeights[D]>0)?k.model.x.map((L,D)=>[...L,I[D]]):k.model.x;Ss(_(F),_(C.model.x))||h.add(A)}),a==="Rao"){let T=e.map(A=>({dfRes:A.dfResidual,dev:A.deviance,fit:ku(A),x:A.model.x,intercept:A.model.intercept}));u=l.map((A,k)=>k===0?NaN:f[k]<0?-gc(T[k],T[k-1]):gc(T[k-1],T[k]))}p=e.reduce((T,A)=>A.dfResidual<T.dfResidual?A:T)}else{let M=s,R=M.model,T=R.x.length>0?R.x[0].length-(R.intercept?1:0):0,A=R.intercept?[0]:[],k=_=>[...A,...Array.from({length:_},(I,F)=>F+A.length)],C=[];for(let _=0;_<=T;_++)_===T?C.push({dfRes:M.dfResidual,dev:M.deviance,fit:ku(M),x:R.x,intercept:R.intercept}):_>0||a==="Rao"?C.push(h1(R,k(_),o)):C.push(void 0);l.push({name:"NULL",dfRes:M.dfNull,dev:M.nullDeviance});for(let _=1;_<=T;_++)l.push({name:`x${_}`,dfRes:C[_].dfRes,dev:C[_].dev});f=l.map((_,I)=>I===0?NaN:l[I-1].dfRes-_.dfRes),m=l.map((_,I)=>I===0?NaN:Math.max(l[I-1].dev-_.dev,0)),a==="Rao"&&(u=l.map((_,I)=>I===0?NaN:gc(C[I-1],C[I]))),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 g=new At,d=n.dispersion??qo(p,g);g.list.forEach(M=>o.add(M));let v=c?1/0:p.dfResidual,N=t?["Resid. Df","Resid. Dev","Df","Deviance"]:["Df","Deviance","Resid. Df","Resid. Dev"],b=l.map((M,R)=>t?[M.dfRes,M.dev,f[R],m[R]]:[f[R],m[R],M.dfRes,M.dev]),E=[...N],w=M=>l.map((R,T)=>{if(T===0||f[T]===0||h.has(T))return NaN;let A=Ve(M[T]/d*Math.sign(f[T]),-2*r);return A<0||Number.isNaN(A)?NaN:u1(A,Math.abs(f[T]))}),y=[];if(a==="Chisq"||a==="LRT")E.push("Pr(>Chi)"),y=[w(m)];else if(a==="Rao")E.push("Rao","Pr(>Chi)"),y=[u.map((M,R)=>h.has(R)?NaN:M),w(u)];else if(a==="F"){if(c&&o.add(`using F test with a '${i.family}' family is inappropriate`),v!==0){let M=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});E.push("F","Pr(>F)"),y=[M,M.map((R,T)=>Number.isNaN(R)?NaN:ht(R,Math.abs(f[T]),v,!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=b.map((M,R)=>[...M,...y.map(T=>T[R])]);return{colNames:E,rowNames:l.map(M=>M.name),table:x,warnings:o.list}}function ku(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 g1=["type"];function qu(e,t,n,r,o=0){let s=[...t,...n];if(s.length===0)return{ss:0,df:0};let i=e.y.flatMap((p,g)=>e.weights===void 0||e.weights[g]!==0?[g]:[]),c=i.map(p=>e.weights===void 0?1:Math.sqrt(e.weights[p])),a=Lt(i.map((p,g)=>s.map(d=>e.x[p][d]*c[g]))),l=ra(e.weights===void 0?void 0:i.map(p=>$(e.weights[p],-o)),i.length),f=a.pivot.slice(0,a.rank),{qty:m}=Zr(f.map(p=>i.map((g,d)=>xt(l[d],e.x[g][s[p]]))),i.map((p,g)=>xt(l[g],$(e.y[p],-r)))),u=[0,0],h=0;return f.forEach((p,g)=>{p>=t.length&&(u=pe(u,Ce(m[g],m[g])),h++)}),{ss:u[0],df:h}}function wc(e,t={}){if(B(t,g1,"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=Ys([e]),i=Gs([e],s)[0];if(o===0)throw new Error("residual df = 0");let c=Ri(e,s),a=y=>{let x=c.map(([T,A])=>[$(T,-y),A]),M=x.reduce((T,[A])=>T+A,0),R=x.reduce((T,[A,k])=>T+A*k,0)/M;return Number.isFinite(M)&&Number.isFinite(R)?x.reduce((T,[A,k])=>T+A*(k-R)**2,0):NaN},l=0,f=a(0);if(Number.isFinite(f)||(l=Xs(c),f=a(l)),xr(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)],g=[],d=[],v=y=>{g.length=0;let x=[];if(r===3){if(e.coefficients.some(M=>Number.isNaN(M)))throw new Error("there are aliased coefficients in the model");for(let M of p){let R=h(new Set([M])),T=h(new Set(p.filter(A=>A!==M)));g.push(M===0?"(Intercept)":e.termLabels[M-1]),x.push({ss:qu(e,T,R,s,y).ss,df:R.length})}}else{let M=R=>e.factors.flatMap((T,A)=>T[R-1]>0?[A]:[]);for(let R of p){if(R===0)continue;let T=M(R),A=p.filter(_=>_!==0&&_!==R&&T.every(I=>e.factors[I][_-1]>0)),k=h(new Set(p.filter(_=>_!==R&&!A.includes(_)))),C=qu(e,k,h(new Set([R])),s,y);g.push(e.termLabels[R-1]),x.push(C)}}return x};d=v(l),l===0&&e.weights!==void 0&&d.some(y=>!Number.isFinite(y.ss))&&(l=Xs(c),d=v(l));let N=y=>$(y,2*s+l),b=$(i,-l),E=b/o,w=d.map(({ss:y,df:x})=>{let M=y/x/E;return[N(y),x,M,ht(M,x,o,!1,!1)]});return w.push([N(b),o,NaN,NaN]),J("Anova",{colNames:["Sum Sq","Df","F value","Pr(>F)"],rowNames:[...g,"Residuals"],table:w,warnings:m},e)}var la=[0,0];var bc=e=>e.map(t=>t.map(n=>n[0]));function Iu(e){let t=e[0]?.length??0;return Array.from({length:t},(n,r)=>e.map(o=>o[r]))}function Cr(e,t){let n=t[0]?.length??0;return e.map(r=>{let o=new Array(n).fill(la);return r.forEach((s,i)=>{let c=t[i];for(let a=0;a<n;a++)o[a]=pe(o[a],Ce(s,c[a]))}),o})}function Fr(e){let t=la;return e.forEach((n,r)=>t=pe(t,n[r])),t}function _u(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=ot(n[a][i],n[i][i]);n[a][i]=l;for(let f=i+1;f<t;f++)n[a][f]=De(n[a][f],Ce(l,n[i][f]))}}return{a:n,perm:r,sign:o,singular:s}}function Lu(e){let{a:t,sign:n,singular:r}=_u(e);if(r)return la;let o=[n,0];return t.forEach((s,i)=>o=Ce(o,s[i])),o}function Cu(e){let t=e.length,{a:n,perm:r,singular:o}=_u(e);if(o)throw new Error("singular matrix");let s=Array.from({length:t},()=>new Array(t).fill(la));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]=De(c[a],Ce(n[a][l],c[l]));for(let a=t-1;a>=0;a--){for(let l=a+1;l<t;l++)c[a]=De(c[a],Ce(n[a][l],c[l]));c[a]=ot(c[a],n[a][a])}for(let a=0;a<t;a++)s[a][i]=c[a]}return s}var w1=["group","idata","M","X"],Fu=(e,t)=>bt(e,t/2,2,!1,!1);function vc(e){let t=e.length,n=ft(Fr(e),t);return Lu(e.map(r=>r.map(o=>ot(o,n))))[0]}function Nc(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=Fu(i,c),l=Fu(i,c+4);return a+s*(l-a)}var b1=(e,t)=>Array.from({length:t},(n,r)=>e.map(o=>o[r]));function v1(e,t,n){let r=e.length,o=v=>v.length===0?0:Lt(Array.from({length:r},(N,b)=>v.map(E=>E[b]))).rank,s=n.length===0?void 0:Lt(Array.from({length:r},(v,N)=>n.map(b=>b[N]))),i=s===void 0?[]:s.pivot.slice(0,s.rank).map(v=>n[v]),c=i.length,a=[...i,...t],l=Lt(Array.from({length:r},(v,N)=>a.map(b=>b[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(v=>a[v]),m=v=>v.map(N=>[N,0]),u=new Array(r).fill([0,0]),h=Wn(f.map(m),u,e.map((v,N)=>e.map(b=>b[N]))).qtv,p=Array.from({length:r},(v,N)=>h.map(b=>b[N])),g=Wn(f.map(m),u,p).qtv,d=Array.from({length:l.rank-c},(v,N)=>c+N);return d.map(v=>d.map(N=>g[v][N]))}function N1(e,t={}){B(t,w1,"mauchlyTest");let n=e.map(w=>Array.from(w)),r=n[0]?.length??0;if(r===0||n.some(w=>w.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=w=>w==null||typeof w=="number"&&Number.isNaN(w),i=n.map((w,y)=>!w.some(s)&&(o===void 0||!s(o[y])));if(n.some((w,y)=>i[y]&&w.some(x=>!Number.isFinite(x))))throw new RangeError("y must not contain infinite values");let c=Ie(n.filter((w,y)=>i[y])).groups,a=c.length,l=o?.filter((w,y)=>i[y]).map(Nt)??c.map(()=>""),f=[...new Set(l)].sort(An),m=c.map(w=>w.map(()=>[0,0]));for(let w of f){let y=l.flatMap((x,M)=>x===w?[M]:[]);for(let x=0;x<r;x++){let M=[0,0];for(let T of y)M=pe(M,[c[T][x],0]);let R=ft(M,y.length);for(let T of y)m[T][x]=De([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},(w,y)=>Array.from({length:r},(x,M)=>{let R=[0,0];for(let T of m)R=pe(R,Ce(T[y],T[M]));return R})),p=t.idata??{index:Array.from({length:r},(w,y)=>y+1)},g=w=>{let y=Ze(()=>Hr(w,p));if(y.x.length!==r)throw new RangeError("idata must have one row per column of y");return b1(y.x,y.colNames.length)},d=t.M===void 0?Array.from({length:r},(w,y)=>Array.from({length:r},(x,M)=>M===y?1:0)):g(t.M),v=g(t.X??"~0"),N=v1(h,d,v),b=N.length;if(b===0)throw new RangeError("X spans M: the test has no dimensions");if(b===1)throw new RangeError("one tested dimension: sphericity holds trivially and the test has no degrees of freedom");if(u<b)throw new RangeError(`too few residual degrees of freedom for the test: ${u} for ${b} dimension(s)`);let E=vc(N);return{statistic:E,statisticName:"W",pValue:Nc(E,b,u,r),method:"Mauchly's test of sphericity",warnings:[]}}var y1=["n","d","type","sigLevel","power","alternative"],M1=["n1","n2","d","sigLevel","power","alternative"],E1=["h","n","sigLevel","power","alternative"],x1=["h","n1","n2","sigLevel","power","alternative"],R1=["h","n","sigLevel","power","alternative"],T1=["d","n","sigLevel","power","alternative"],A1=["n","r","sigLevel","power","alternative"],S1=["w","N","df","sigLevel","power"],k1=["k","n","f","sigLevel","power"],q1=["u","v","f2","sigLevel","power"],I1=["size"],_1={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 Du(e="p",...t){B(Ts(t),I1,"cohenES");let n=Rs(["size"],t).size??"small",r=ke(e,["p","t","r","anov","chisq","f2"]),o=ke(n,["small","medium","large"]);return{test:r,size:o,effectSize:_1[r][["small","medium","large"].indexOf(o)],method:"Conventional effect size from Cohen (1982)"}}function L1(e,t){if(!fa(e)||!fa(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 C1(e,t){if(e.length!==t.length)throw new RangeError("'P0' and 'P1' must have the same length");if(![...e,...t].every(fa))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(fa)))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>=O1)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 O1=2**-1022,fa=e=>e>=0&&e<=1,D1="\u2018sig.level\u2019 must be numeric in [0, 1]",P1="\u2018power\u2019 must be numeric in [0, 1]";function mr(e,t){let n=r=>!Re(r)&&!(typeof r=="number"&&r>=0&&r<=1);if(n(e))throw new RangeError(D1);if(n(t))throw new RangeError(P1)}function ur(e,t){if(typeof e=="number"&&Number.isNaN(e))throw new RangeError("the effect size must not be NaN");return typeof e=="string"?Du(t,e).effectSize:e}var Xo=["two.sided","less","greater"];function ma(e,t){return e==="two.sided"?t:e==="less"?[-10,5]:[-5,10]}function Pu(e,t,n,r){if(r==="less")return yr(Wt(n,e),e,t,!0);if(r==="greater")return yr(Wt(n,e,{lowerTail:!1}),e,t,!1);let o=Wt(n/2,e,{lowerTail:!1});return yr(o,e,t,!1)+yr(-o,e,t,!0)}var Ou=e=>e<1e-8?ie(e,{lowerTail:!1}):ie(1-e);function Vu(e,t,n){return n==="greater"?ae(Ou(t)-e,{lowerTail:!1}):n==="less"?ae(ie(t)-e):ae(Ou(t/2)-e,{lowerTail:!1})+ae(ie(t/2)-e)}function V1(e){let t=1,n=0;for(let r of e){if(r===0)return 0;let o=Math.floor(Math.log2(Math.abs(r)));t*=$(r,-o),n+=o}return $(t,n)}function $1(e={}){B(e,y1,"pwrTTest");let{n:t,power:n}=e,r=ur(e.d,"t"),o=e.sigLevel===void 0?.05:e.sigLevel;if(rn([t,r,n,o],"exactly one of n, d, power, and sig.level must be NULL"),mr(o,n),!Re(t)&&!(t>=2))throw new RangeError("number of observations in each group must be at least 2");let s=ke(e.type,["two.sample","one.sample","paired"]),i=ke(e.alternative,Xo),c=s==="two.sample"?2:1;i==="two.sided"&&!Re(r)&&(r=Math.abs(r));let a=(m,u,h)=>Pu((m-1)*c,Math.sqrt(m/c)*u,h,i);if(Re(n))n=a(t,r,o);else{let m=n;Re(t)?t=Ye(u=>a(u,r,o)-m,2+1e-10,1e9):Re(r)?r=Ye(u=>a(t,u,o)-m,...ma(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 z1(e={}){B(e,M1,"pwrT2nTest");let{n1:t,n2:n,power:r}=e,o=ur(e.d,"t"),s=e.sigLevel===void 0?.05:e.sigLevel;if(rn([t,n,o,r,s],"exactly one of n1, n2, d, power, and sig.level must be NULL"),mr(s,r),!Re(t)&&!(t>=2))throw new RangeError("number of observations in the first group must be at least 2");if(!Re(n)&&!(n>=2))throw new RangeError("number of observations in the second group must be at least 2");let i=ke(e.alternative,Xo);i==="two.sided"&&!Re(o)&&(o=Math.abs(o));let c=(a,l,f,m)=>Pu(a+l-2,f/Math.sqrt(1/a+1/l),m,i);if(Re(r))r=c(t,n,o,s);else{let a=r;Re(t)?t=Ye(l=>c(l,n,o,s)-a,2+1e-10,1e9):Re(n)?n=Ye(l=>c(t,l,o,s)-a,2+1e-10,1e9):Re(o)?o=Ye(l=>c(t,n,l,s)-a,...ma(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 yc(e,t,n,r,o,s){let{n:i,power:c}=t,a=ur(e,n==="h"?"p":"t"),l=t.sigLevel===void 0?.05:t.sigLevel;if(rn([a,i,c,l],`exactly one of ${n}, n, power, and sig.level must be NULL`),mr(l,c),!Re(i)&&!(i>=1))throw new RangeError("number of observations in each group must be at least 1");let f=ke(t.alternative,Xo);f==="two.sided"&&!Re(a)&&(a=Math.abs(a));let m=(u,h,p)=>Vu(u*r(h),p,f);if(Re(c))c=m(a,i,l);else{let u=c;Re(a)?a=Ye(h=>m(h,i,l)-u,...ma(f,[s,10])):Re(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 j1(e={}){B(e,E1,"pwr2pTest");let t=yc(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 K1(e={}){B(e,R1,"pwrPTest");let t=yc(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 B1(e={}){B(e,T1,"pwrNormTest");let t=yc(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 W1(e={}){B(e,x1,"pwr2p2nTest");let{n1:t,n2:n,power:r}=e,o=ur(e.h,"p"),s=e.sigLevel===void 0?.05:e.sigLevel;if(rn([o,t,n,r,s],"exactly one of h, n1, n2, power, and sig.level must be NULL"),mr(s,r),!Re(t)&&!(t>=2))throw new RangeError("number of observations in the first group must be at least 2");if(!Re(n)&&!(n>=2))throw new RangeError("number of observations in the second group must be at least 2");let i=ke(e.alternative,Xo);i==="two.sided"&&!Re(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)=>Vu(l*Math.sqrt(c(f,m)),u,i);if(Re(r))r=a(o,t,n,s);else{let l=r;Re(o)?o=Ye(f=>a(f,t,n,s)-l,...ma(i,[1e-10,10])):Re(t)?t=Ye(f=>a(o,f,n,s)-l,2+1e-10,1e9):Re(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 U1(e={}){B(e,A1,"pwrRTest");let{n:t,power:n}=e,r=ur(e.r,"r"),o=e.sigLevel===void 0?.05:e.sigLevel;if(rn([t,r,n,o],"exactly one of n, r, power, and sig.level must be NULL"),mr(o,n),!Re(t)&&!(t>=4))throw new RangeError("number of observations must be at least 4");if(!Re(r)&&!(Math.abs(r)<=1))throw new RangeError("'r' must be in [-1, 1]");let s=ke(e.alternative,Xo);s==="two.sided"&&!Re(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),g=Math.atanh(l)+l/(2*(a-1)),d=Math.atanh(p),v=ae((g-d)*Math.sqrt(a-3));return m?v+ae((-g-d)*Math.sqrt(a-3)):v},c=(a,l,f)=>s==="two.sided"?i(a,l,f,!0):i(a,s==="less"?-l:l,f,!1);if(Re(n))n=c(t,r,o),s==="less"&&(r=-r);else{let a=n;if(Re(t))t=Ye(l=>c(l,r,o)-a,4+1e-10,1e9);else if(Re(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 H1(e){B(e,S1,"pwrChisqTest");let{N:t,power:n}=e,{df:r}=e,o=ur(e.w,"chisq"),s=e.sigLevel===void 0?.05:e.sigLevel;if(rn([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(mr(s,n),!Re(o)&&!(o>=0))throw new RangeError("w must be positive");if(!Re(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:Rt(On(l,r,{lowerTail:!1}),r,{ncp:f,lowerTail:!1})};if(Re(n))n=i(o,t,s);else{let c=n;Re(o)?o=Ye(a=>i(a,t,s)-c,1e-10,1e9):Re(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 Y1(e={}){B(e,k1,"pwrAnovaTest");let{k:t,n,power:r}=e,o=ur(e.f,"anov"),s=e.sigLevel===void 0?.05:e.sigLevel;if(rn([t,n,o,r,s],"exactly one of k, n, f, power, and sig.level must be NULL"),mr(s,r),!Re(o)&&!(o>=0))throw new RangeError("f must be positive");if(!Re(t)&&!(t>=2))throw new RangeError("number of groups must be at least 2");if(!Re(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=V1([c,a,l,l])),h===1/0?1:Cs(m,u)?Fs(m,u,h,f):Tn(Gn(f,m,u,{lowerTail:!1}),m,u,{ncp:h,lowerTail:!1})};if(Re(r))r=i(t,n,o,s);else{let c=r;Re(t)?t=Ye(a=>i(a,n,o,s)-c,2+1e-10,100):Re(n)?n=Ye(a=>i(t,a,o,s)-c,2+1e-10,1e9):Re(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,q1,"pwrF2Test");let{u:t,v:n,power:r}=e,o=ur(e.f2,"f2"),s=e.sigLevel===void 0?.05:e.sigLevel;if(rn([t,n,o,r,s],"exactly one of u, v, f2, power, and sig.level must be NULL"),mr(s,r),!Re(o)&&!(o>=0))throw new RangeError("f2 must be positive");if(!Re(t)&&!(t>=1))throw new RangeError("degree of freedom u for numerator must be at least 1");if(!Re(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:Cs(c,a)?Fs(c,a,m,f):Tn(Gn(f,c,a,{lowerTail:!1}),c,a,{ncp:m,lowerTail:!1})};if(Re(r))r=i(t,n,o,s);else{let c=r;Re(t)?t=Ye(a=>i(a,n,o,s)-c,1+1e-10,100):Re(n)?n=Ye(a=>i(t,a,o,s)-c,1+1e-10,1e9):Re(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 X1=["id","dv","between","within","type","correction","es","observed"],ua=e=>e===void 0?[]:typeof e=="string"?[e]:[...e],Q1=1e-12,$u=1e-24;function zu(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 Qo=e=>typeof e=="boolean"?e?"TRUE":"FALSE":Nt(e);function Z1(e){let t=[...new Set(e)];return t.every(n=>typeof n=="number")?t.sort((n,r)=>n-r).map(Qo):[...new Set(t.map(Qo))].sort(An)}var ju=(e,t)=>t.split(":").includes(e);function Ku(e,t,n,r,o){let s=l=>r.some(f=>ju(f,l));for(let l of r)if(!e.some(f=>ju(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 J1(e,t){B(t,X1,"aovEz");let n=ua(t.between),r=ua(t.within);if(n.length===0&&r.length===0)throw new Error("Either between or within need to be non-NULL!");for(let[z,H]of[["between",n],["within",r]]){let K=H.filter((te,se)=>H.indexOf(te)!==se);if(K.length>0)throw new RangeError(`duplicated factor names in '${z}': ${[...new Set(K)].join(", ")}`)}let o=n.filter(z=>r.includes(z));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=ua(t.es??"ges");for(let z of c)if(z!=="ges"&&z!=="pes")throw new RangeError(`invalid 'es': ${String(z)}`);let a=ua(t.observed),l=[],f=z=>z==null||typeof z=="number"&&Number.isNaN(z),m=zu(e,t.dv),u=[t.id,...n,...r].map(z=>zu(e,z));for(let z of u)if(z.length!==m.length)throw new RangeError("the columns must have the same length");let h=m.map((z,H)=>u.every(K=>!f(K[H]))),p=z=>z.filter((H,K)=>h[K]),g=Pt(p(u[0])).map(Qo),d=p(m).map(z=>{if(f(z))return NaN;if(typeof z!="number")throw new TypeError(`the response '${t.dv}' must be numeric`);if(!Number.isFinite(z))throw new RangeError(`the response '${t.dv}' must be finite`);return z}),v=n.map((z,H)=>Pt(p(u[1+H]))),N=r.map((z,H)=>Pt(p(u[1+n.length+H]))),b=g.length;for(let z of[d,...v,...N])if(z.length!==b)throw new RangeError("the columns must have the same length");let E=v.map(z=>Z1(z)),w=v.map(z=>z.map(Qo)),y=N.map(z=>z.map(Qo)),x=y.map(z=>[...new Set(z)]);E.forEach((z,H)=>{if(z.length<2)throw new Error(`Factor "${n[H]}" consists of one level only. Remove factor from model?`)}),x.forEach((z,H)=>{if(z.length<2)throw new Error(`Factor "${r[H]}" consists of one level only. Remove factor from model?`)});let M=[...new Set(g)],R=new Map,T=new Set;if(g.forEach((z,H)=>{let K=JSON.stringify(w.map(se=>se[H])),te=R.get(z);te===void 0?R.set(z,K):te!==K&&T.add(z)}),T.size>0)throw new Error(`Following ids are in more than one between subjects condition:
8
+ ${M.filter(z=>T.has(z)).join(", ")}`);let A=[[]];for(let z of x)A=A.flatMap(H=>z.map(K=>[...H,K]));let k=z=>JSON.stringify(y.map(H=>H[z])),C=new Map(A.map((z,H)=>[JSON.stringify(z),H])),_=new Set(g.map((z,H)=>k(H)));if(A.some(z=>!_.has(JSON.stringify(z))))throw new Error("Empty cells in within-subjects design (i.e., bad data structure).");let I=new Map,F=new Set;g.forEach((z,H)=>{let K=I.get(z);K===void 0&&I.set(z,K=new Set),K.add(k(H)),Number.isNaN(d[H])&&F.add(z)});let L=M.filter(z=>F.has(z)||I.get(z).size<A.length),D=new Set(L),S=Ie([d.filter((z,H)=>!D.has(g[H]))]).k,O=S===0?d:d.map(z=>$(z,-S)),q=z=>$(z,2*S),V=A.length,W=new Map;if(g.forEach((z,H)=>{let K=W.get(z);K===void 0&&W.set(z,K={s:A.map(()=>[0,0]),n:A.map(()=>0)});let te=C.get(k(H));K.s[te]=pe(K.s[te],[O[H],0]),K.n[te]++}),[...W.values()].some(z=>z.n.some(H=>H>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."),L.length>0){let z=[...L].sort(An),H=z.length>=10?`${z.slice(0,10).join(", ")}, ... [showing first 10 only]`:z.join(", ");l.push(`Missing values for ${L.length} ID(s), which were removed before analysis:
9
+ ${H}`)}let Q=M.filter(z=>!L.includes(z)),re=new Map;g.forEach((z,H)=>re.has(z)||re.set(z,H));let j=Q.map(z=>{let H=W.get(z);return H.s.map((K,te)=>ft(K,H.n[te]))}),ee={};n.forEach((z,H)=>ee[z]=Q.map(K=>w[H][re.get(K)]));let U=Object.fromEntries(n.map(z=>[z,"contr.sum"])),ue=Object.fromEntries(n.map((z,H)=>[z,E[H]])),Z="Rank deficient model matrix; insufficient data to estimate full model.";if(V===1)return J("aovEz",P());return J("aovEz",Y());function P(){let z="y";for(;n.includes(z);)z+="_";let H={...ee,[z]:j.map(ce=>ce[0][0])},K=Ze(()=>Uo(`${z} ~ ${n.length>0?n.join(" * "):"1"}`,H,{contrasts:U,levels:ue}));if(K.rank<K.coefNames.length)throw new Error(Z);if(K.dfResidual<=0)throw new RangeError(`no residual degrees of freedom: ${j.length} subject(s) for ${K.rank} parameter(s)`);let te=Ze(()=>wc(K,{type:s}));l.push(...te.warnings.filter(ce=>!l.includes(ce)));let se=K.dfResidual,Me=te.rowNames.flatMap((ce,qe)=>{if(ce==="(Intercept)"||ce==="Residuals")return[];let[we,Oe,xe,_e]=te.table[qe];return[{name:ce,ss:we,numDf:Oe,errSS:K.rss,denDf:se,f:xe,p:_e}]}),Ne=te.colNames.indexOf("Sum Sq"),de={anova:Ku(Me,new Map([[0,K.rss]]),c,a,q),univariate:{colNames:te.colNames,rowNames:te.rowNames,table:te.table.map(ce=>ce.map((qe,we)=>we===Ne?q(qe):qe))},within:r,warnings:l};return Yt.set(de,{colNames:te.colNames,rowNames:te.rowNames,table:te.table}),de}function Y(){let z=Q.length,H=j.reduce((Te,Ee)=>Ee.reduce((Ae,ne)=>Ae+ne[0]*ne[0],Te),0),K=n.length>0?Ze(()=>Hr(`~ ${n.join(" * ")}`,ee,{contrasts:U,levels:ue})):{colNames:["(Intercept)"],assign:[0],x:Q.map(()=>[1]),termLabels:[],factors:[]},te=K.colNames.length;if(z===0||Lt(K.x).rank<te)throw new Error(Z);let se=z-te;if(se<=0)throw new RangeError(`no residual degrees of freedom: ${z} subject(s) for ${te} between-subject parameter(s)`);let Me=[0,...K.termLabels.map((Te,Ee)=>Ee+1)],Ne=["(Intercept)",...K.termLabels],de=Te=>K.assign.flatMap((Ee,Ae)=>Te.includes(Ee)?[Ae]:[]),ce=Te=>K.factors.flatMap((Ee,Ae)=>Ee[Te-1]>0?[Ae]:[]),qe=Te=>K.x.map(Ee=>[Ee[Te],0]),we=new Array(z).fill([0,0]),Oe=(Te,Ee,Ae)=>{let{qtv:ne}=Wn([...Te,...Ee].map(qe),we,Ae);return ne.map(ye=>ye.slice(Te.length,Te.length+Ee.length))},xe=Te=>{let{qtv:Ee}=Wn(K.colNames.map((Ae,ne)=>qe(ne)),we,Te);return Ee.map(Ae=>Ae.slice(te))},_e=Object.fromEntries(r.map((Te,Ee)=>[Te,A.map(Ae=>Ae[Ee])])),Fe=Ze(()=>Hr(`~ ${r.join(" * ")}`,_e,{contrasts:Object.fromEntries(r.map(Te=>[Te,"contr.sum"])),levels:Object.fromEntries(r.map((Te,Ee)=>[Te,x[Ee]]))})),We=[0,...Fe.termLabels.map((Te,Ee)=>Ee+1)],ze=["(Intercept)",...Fe.termLabels],tt=Te=>Te.map(Ee=>Te.map(Ae=>{let ne=[0,0];return Ee.forEach((ye,me)=>ne=pe(ne,Ce(ye,Ae[me]))),ne})),Ue=[],Xe=[],ge=[],oe=[],ve=[],be=[],fe=[],nt=new Map,X=!1;for(let Te of We){let Ee=Fe.assign.flatMap((Be,je)=>Be===Te?[je]:[]),Ae=Ee.length,ne=Ee.map(Be=>j.map(je=>je.reduce((St,pt,Rn)=>pe(St,xt(pt,Fe.x[Rn][Be])),[0,0]))),ye=Ee.map(Be=>Ee.map(je=>[Fe.x.reduce((St,pt)=>St+pt[Be]*pt[je],0),0])),me=Cu(ye),Se=tt(xe(ne)),Ke=Cr(Se,me),$e=Fr(Ke)[0],st=NaN,mt=NaN,Ct=NaN,Gt=NaN,Yn=!1;if(Ae>=2){let Be=ze[Te],je=Kl(bc(me)).map(Xt=>Xt.map(sn=>[sn,0])),St=bc(Cr(Cr(je,Se),Iu(je))),pt=Ut(St,{symmetric:!0,onlyValues:!0}).values,Rn=pt.reduce((Xt,sn)=>Math.max(Xt,sn),0),$t=Math.max(Rn*Q1,H*$u),It=Fr(Ke),ut=Fr(Cr(Ke,Ke));Rn>H*$u&&ut[0]>0&&Number.isFinite(ut[0])?(Yn=!0,st=ot(Ce(It,It),xt(ut,Ae))[0],se-Ae*st>0?mt=((se+1)*Ae*st-2)/(Ae*(se-Ae*st)):l.push(`The Huynh-Feldt epsilon of ${Be} is undefined (d - p GG <= 0)${i==="HF"?": corrected with GG":""}`)):l.push(`The error SSP matrix of ${Be} is 0 (or at the rounding level of the data): no sphericity corrections (the p-values are uncorrected)`),Yn&&pt.every(Xt=>Xt>$t)?(Ct=vc(Cr(me,Se)),Gt=Nc(Ct,Ae,se,V)):Yn&&l.push(`The error SSP matrix of ${Be} is singular: no Mauchly test (the epsilons are still defined)`)}for(let Be=0;Be<Me.length;Be++){let je=Me[Be],St;if(s===3)St=Oe(de(Me.filter(Ft=>Ft!==je)),de([je]),ne);else if(je===0)St=Oe([],de([0]),ne);else{let Ft=ce(je),tn=Me.filter(G=>G!==0&&G!==je&&Ft.every(le=>K.factors[le][G-1]>0));St=Oe(de(Me.filter(G=>G!==je&&!tn.includes(G))),de([je]),ne)}let pt=Fr(Cr(tt(St),me))[0],$t=(je===0?1:de([je]).length)*Ae,It=se*Ae,ut=pt/$t/($e/It),Xt=ht(ut,$t,It,!1,!1),sn=Te===0?Ne[Be]:je===0?ze[Te]:`${Ne[Be]}:${ze[Te]}`;Ue.push([q(pt),$t,q($e),It,ut,Xt]),Xe.push([pt,$t,$e,It,ut,Xt]),ge.push(sn),nt.set(Te,$e);let en={name:sn,ss:pt,numDf:$t,errSS:$e,denDf:It,f:ut,p:Xt};if(Yn){let Ft=Math.min(1,mt),tn=ht(ut,st*$t,st*It,!1,!1),G=ht(ut,Ft*$t,Ft*It,!1,!1);oe.push([st,tn,mt,G]),ve.push([Ct,Gt]),be.push(sn),i==="GG"?en={...en,numDf:st*$t,denDf:st*It,p:tn}:i==="HF"&&(mt>1&&(X=!0),en=Number.isNaN(mt)?{...en,numDf:st*$t,denDf:st*It,p:tn}:{...en,numDf:Ft*$t,denDf:Ft*It,p:G})}Te===0&&je===0||fe.push(en)}}X&&l.push("HF eps > 1 treated as 1");let he={anova:Ku(fe,nt,c,a,q),univariate:{colNames:["Sum Sq","num Df","Error SS","den Df","F value","Pr(>F)"],rowNames:ge,table:Ue},within:r,warnings:l};return oe.length>0&&(he.corrections={colNames:["GG eps","Pr(>F[GG])","HF eps","Pr(>F[HF])"],rowNames:be,table:oe},he.sphericity={colNames:["Test statistic","p-value"],rowNames:[...be],table:ve}),Yt.set(he,{colNames:he.univariate.colNames,rowNames:ge,table:Xe}),he}}var eb=["tau","covariates","covariateNames","confLevel"];function Zo(e){return 2*ae(-Math.abs(e))}function Uu(e,t,n){return[e-n*t,e+n*t]}function Bu(e,t,n){return{estimate:e,se:t,confInt:Uu(e,t,n)}}function tb(e,t,n,r){return{estimate:e,confInt:Uu(e,t,n),pValue:Zo(r)}}function Wu(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=Pn();for(let l=0;l<n.time.length&&n.time[l]<t;l++){let f=n.time[l];dn(c,(1-i)*(f-s),n.time.length+1),i=n.surv[l],s=f}dn(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 Mc(e,t,n,r){if(n==="difference"){let l=e.estimate-t.estimate,f=Math.hypot(e.se,t.se);return tb(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:Zo(c/a)}}function nb(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 Ec(e,t,n,r,o,s){let i=e.map(_=>[_.arm,..._.x]),c=i[0].map((_,I)=>{let F=i.reduce((D,S)=>Math.max(D,Math.abs(S[I])),0);if(F===0)return 1;let L=Math.floor(Math.log2(F));return L>500||L<-500?2**L:1}),a=i.map(_=>_.map((I,F)=>I/c[F])),l=e.map((_,I)=>[1,...a[I]]),f=[1,...c],m=nb(e),u=r?Yr(a,n,{family:"poisson",weights:m}):Uo(a,n,{weights:m});if(r&&!u.converged)throw new Error("the adjusted RMST Poisson model did not converge");let p=u.coefficients.map((_,I)=>_/f[I]),g=u.fittedValues;if(p.some(_=>!Number.isFinite(_))||g.some(_=>!Number.isFinite(_)))throw new Error("the adjusted RMST model is singular or has non-finite fitted values");let d=l.map((_,I)=>_.map(F=>m[I]*F*(n[I]-g[I]))),v=d.map(_=>_.slice());for(let _ of[0,1]){let I=e.flatMap((L,D)=>L.arm===_?[D]:[]),F=fr(I.map(L=>e[L].time),I.map(L=>e[L].status===0?1:0),{rmean:"none"});for(let L=0;L<F.time.length;L++){let D=F.time[L],S=F.nEvent[L];if(!(D<t)||S===0)continue;let O=F.nRisk[L];if(!(O>0))continue;let q=new Array(l[0].length).fill(0);for(let W=0;W<q.length;W++){let Q=Pn();for(let re of I)e[re].time>=D&&dn(Q,d[re][W],I.length);q[W]=Vn(Q)}let V=S/O;for(let W of I){let Q=e[W],re=Q.time===D&&Q.status===0,j=Q.time>=D;for(let ee=0;ee<q.length;ee++)v[W][ee]+=q[ee]/O*(Number(re)-(j?V:0))}}}let N=l.map((_,I)=>_.map(F=>F*Math.sqrt(r?g[I]:1))),b=Wa(N),E=Wa(v),w=rt(b),x=Ua(Ua(w,E),w).map((_,I)=>_.map((F,L)=>F/f[I]/f[L])),M=x.map((_,I)=>Math.sqrt(Math.max(0,_[I]))),R=p.map((_,I)=>_/M[I]),T=R.map(Zo),A=p.map((_,I)=>_-s*M[I]),k=p.map((_,I)=>_+s*M[I]),C=r?[A.map(Math.exp),k.map(Math.exp)]:[A,k];return{coef:p,cov:x,table:{names:["intercept","arm",...o],estimate:p,se:M,z:R,pValue:T,confInt:C}}}function rb(e,t,n,r={}){if(B(r,eb,"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((j,ee)=>Number.isFinite(ee)?Math.max(j,Math.abs(ee)):j,0),i=s>2**500?2**Math.floor(Math.log2(s)):1,a=e.every(j=>!Number.isFinite(j)||j===0||j/i!==0)&&(r.tau===void 0||r.tau===0||r.tau/i!==0)?i:1,l=j=>j/a,f=Array.from(t),m=new Set;for(let j=0;j<f.length;j++){let ee=n[j]===!0?1:n[j]===!1?0:n[j];(ee===0||ee===1)&&f[j]===2&&m.add(ee)}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(j=>[j]):r.covariates.map(j=>Array.from(j));if(p&&p.length!==e.length)throw new RangeError("'covariates' must have one row per observation");if(p&&p.some(j=>j.length!==p[0].length))throw new RangeError("'covariates' must have the same number of columns in every row");if(p&&p.some(j=>j.some(ee=>typeof ee!="number")))throw new TypeError("'covariates' must contain numeric values");if(p&&p.some(j=>j.some(ee=>!Number.isFinite(ee)&&!Number.isNaN(ee))))throw new RangeError("'covariates' must be finite");let g=[],d=[],v=[],N=[];for(let j=0;j<e.length;j++){if(e[j]==null||Number.isNaN(e[j])||f[j]==null||Number.isNaN(f[j])||n[j]==null||Number.isNaN(n[j]))throw new Error("missing value where TRUE/FALSE needed");if(typeof e[j]!="number")throw new TypeError("'time' must contain numbers");if(!Number.isFinite(e[j]))throw new RangeError("'time' must be finite");if(typeof n[j]!="number"&&typeof n[j]!="boolean")throw new TypeError("'arm' must contain numbers or booleans");let ee=n[j]===!0?1:n[j]===!1?0:n[j];if(ee!==0&&ee!==1){v.push(l(e[j]));continue}if(typeof f[j]!="number")throw new TypeError("'status' must contain numbers");let U=f[j],ue=U===1?1:0,Z=p?p[j]:[];if(Z.some(Y=>!Number.isFinite(Y)))continue;d.push({time:l(e[j]),status:ue,arm:ee,x:Z,index:j});let P=U;if(m.has(ee))if(U===2)P=1;else if(U===1)P=0;else{N.includes("Invalid status value, converted to NA")||N.push("Invalid status value, converted to NA");continue}else if(P!==0&&P!==1){N.includes("Invalid status value, converted to NA")||N.push("Invalid status value, converted to NA");continue}g.push({time:l(e[j]),status:P,arm:ee,x:Z,index:j})}if(d.some(j=>j.time<0))throw new Error("negative follow-up times are not supported");if(!g.some(j=>j.arm===0)||!g.some(j=>j.arm===1))throw new Error("both arms 0 and 1 are required");let b=[0,1].map(j=>Math.max(...d.filter(ee=>ee.arm===j).map(ee=>ee.time))),E=[0,1].map(j=>{let ee=b[j];return d.some(U=>U.arm===j&&U.time===ee&&U.status===0)}),w=E.some(Boolean)?Math.min(...b.filter((j,ee)=>E[ee])):Math.max(...b),y=r.tau===void 0,x=r.tau===void 0?w:l(r.tau),M=r.tau===void 0?x*a:r.tau;if(v.some(j=>j<=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>w)throw new RangeError(`'tau' must be at most ${w*a}: the minimum of the largest times allowed by both arms`);if(x<Math.min(...d.map(j=>j.time)))throw new RangeError("'tau' must not be below the first observed time");let R=(1-o)/2,T=R<1e-8?ie(R,{lowerTail:!1}):ie(1-R),A=g.filter(j=>j.arm===1),k=g.filter(j=>j.arm===0),C=Wu(A,x),_=Wu(k,x),I=j=>({estimate:j.estimate*a,se:j.se*a}),F={rmst:I(C.rmst),rmtl:I(C.rmtl)},L={rmst:I(_.rmst),rmtl:I(_.rmtl)},D=Bu(F.rmst.estimate,F.rmst.se,T),S=Bu(L.rmst.estimate,L.rmst.se,T),O={estimate:F.rmtl.estimate,se:F.rmtl.se,confInt:[M-D.confInt[1],M-D.confInt[0]]},q={estimate:L.rmtl.estimate,se:L.rmtl.se,confInt:[M-S.confInt[1],M-S.confInt[0]]},V=Mc(F.rmst,L.rmst,"difference",T),W=Mc(F.rmst,L.rmst,"ratio",T),Q=Mc(F.rmtl,L.rmtl,"rmtlRatio",T),re={tau:M,tauDefault:y,confLevel:o,arm1:{rmst:D,rmtl:O},arm0:{rmst:S,rmtl:q},difference:V,ratio:W,rmtlRatio:Q,warnings:N};if(p){let j=r.covariateNames===void 0?h?["covariates"]:Array.from({length:p[0].length},()=>""):Array.from(r.covariateNames);if(j.some(de=>typeof de!="string"))throw new TypeError("'covariateNames' must contain strings");if(j.length!==p[0].length)throw new RangeError("'covariateNames' must have one name per covariate column");let ee=g.map(de=>({...de,time:Math.min(de.time,x),status:de.time>=x?1:de.status})),U=ee.map(de=>de.time),ue=ee.map(de=>x-de.time),Z=Ec(ee,x,U,!1,j,T),P=!(F.rmst.estimate>0&&L.rmst.estimate>0),Y=!(F.rmtl.estimate>0&&L.rmtl.estimate>0),z=P?void 0:Ec(ee,x,U,!0,j,T),H=Y?void 0:Ec(ee,x,ue,!0,j,T),K=(de,ce)=>{let qe=de.coef[1],we=Math.sqrt(Math.max(0,de.cov[1][1]));return ce?{estimate:Math.exp(qe),confInt:[Math.exp(qe-T*we),Math.exp(qe+T*we)],pValue:Zo(qe/we)}:{estimate:qe*a,confInt:[(qe-T*we)*a,(qe+T*we)*a],pValue:Zo(qe/we)}},te=de=>({...de,estimate:de.estimate.map(ce=>ce*a),se:de.se.map(ce=>ce*a),confInt:[de.confInt[0].map(ce=>ce*a),de.confInt[1].map(ce=>ce*a)]}),se=de=>({...de,estimate:de.estimate.map((ce,qe)=>qe===0?ce+Math.log(a):ce),confInt:[de.confInt[0].map((ce,qe)=>qe===0?ce+Math.log(a):ce),de.confInt[1].map((ce,qe)=>qe===0?ce+Math.log(a):ce)]}),Me={names:["intercept","arm",...j],estimate:Array(Z.coef.length).fill(NaN),se:Array(Z.coef.length).fill(NaN),z:Array(Z.coef.length).fill(NaN),pValue:Array(Z.coef.length).fill(NaN),confInt:[Array(Z.coef.length).fill(NaN),Array(Z.coef.length).fill(NaN)]},Ne={estimate:NaN,confInt:[NaN,NaN],pValue:NaN};re.adjusted={difference:K(Z,!1),ratio:P?Ne:K(z,!0),rmtlRatio:Y?Ne:K(H,!0)},re.coefficients={difference:te(Z.table),ratio:z?se(z.table):Me,rmtlRatio:H?se(H.table):Me},delete re.arm1,delete re.arm0}return J("rmst2",re)}var ob=["method","confLevel","control"],sb=["eps","iterMax"];function xc(e,t){let n=t.length,r=0,o=new Array(n).fill(0),s=Array.from({length:n},()=>new Array(n).fill(0));for(let{x:i,cases:c}of e){let a=i.length;if(c===0||c===a)continue;let l=i.map(d=>d.reduce((v,N,b)=>v+N*t[b],0)),f=Math.max(...l),m=new Array(c+1).fill(0),u=new Array(c+1).fill(0),h=Array.from({length:c+1},()=>new Array(n).fill(0)),p=Array.from({length:c+1},()=>Array.from({length:n},()=>new Array(n).fill(0)));m[0]=1;for(let d=0;d<a;d++){let v=i[d],N=Math.exp(l[d]-f);for(let b=Math.min(c,d+1);b>=1;b--){let E=m[b]===0?u[b-1]:Math.max(u[b],u[b-1]),w=m[b]===0?0:2**(u[b]-E),y=N*2**(u[b-1]-E),x=m[b-1],M=h[b-1],R=p[b-1],T=h[b],A=p[b];m[b]=m[b]*w+y*x;for(let k=0;k<n;k++){let C=T[k];T[k]=C*w+y*(v[k]*x+M[k]);for(let _=0;_<n;_++)A[k][_]=A[k][_]*w+y*(v[k]*v[_]*x+v[k]*M[_]+v[_]*M[k]+R[k][_])}if(u[b]=E,m[b]>2**256||m[b]>0&&m[b]<2**-256){let k=Math.floor(Math.log2(m[b])),C=2**-k;m[b]=m[b]*C;for(let _=0;_<n;_++){T[_]=T[_]*C;for(let I=0;I<n;I++)A[_][I]=A[_][I]*C}u[b]=E+k}}}let g=0;for(let d=0;d<c;d++)g+=l[d]-f;r+=g-(Math.log(m[c])+u[c]*Math.LN2);for(let d=0;d<n;d++){let v=0;for(let b=0;b<c;b++)v+=i[b][d];let N=h[c][d]/m[c];o[d]+=v-N;for(let b=0;b<n;b++)s[d][b]+=p[c][d][b]/m[c]-N*(h[c][b]/m[c])}}return{loglik:r,score:o,info:s}}function Rc(e,t){return{statistic:e,df:t,pValue:Rt(e,t,{lowerTail:!1})}}function ab(e,t,n,r={}){B(r,ob,"clogit"),B(Kt(r,"control"),sb,"clogit","control");let{method:o="exact",confLevel:s=.95,control:i={}}=r,{eps:c=1e-9,iterMax:a=20}=i,l=e.length;if(t.length!==l||n.length!==l)throw new RangeError("'y', 'x' and 'strata' must have the same length");if(o!=="exact"&&o!=="approximate"&&o!=="breslow")throw new RangeError(`invalid 'method': ${String(o)}`);e.forEach((O,q)=>{if(O!==0&&O!==1)throw new RangeError(`'y' must be 0 or 1 (element ${q+1})`)});let f=t[0]?.length??0;if(f===0||t.some(O=>O.length!==f||O.some(q=>!Number.isFinite(q))))throw new RangeError("'x' must have the same positive number of finite covariates in every row");let m=ie((1+s)/2),u=(O,q,V,W,Q,re,j,ee)=>{let U=q.map((Z,P)=>Math.sqrt(Z[P])),ue=O.map((Z,P)=>Z/U[P]);return{coefficients:O,var:q,loglik:V,score:W,wald:Q,iter:re,n:l,nevent:e.reduce((Z,P)=>Z+P,0),method:j,se:U,z:ue,p:ue.map(Z=>Rt(Z*Z,1,{lowerTail:!1})),lower:O.map((Z,P)=>Math.exp(Z-m*U[P])),upper:O.map((Z,P)=>Math.exp(Z+m*U[P])),logtest:Rc(2*(V[1]-V[0]),f),sctest:Rc(W,f),waldtest:Rc(Q,f),warnings:ee}};if(o!=="exact"){let O=no(new Array(l).fill(1),e,t,{ties:"breslow",strata:n,confLevel:s,control:{eps:c,iterMax:a}});return u(O.coefficients,O.var,O.loglik,O.score,O.wald,O.iter,"breslow",O.warnings)}let h=Array.from({length:f},(O,q)=>t.reduce((V,W)=>V+W[q],0)/l),p=new Map;for(let O=0;O<l;O++){let q=p.get(n[O]);q===void 0&&(q={cases:[],controls:[]},p.set(n[O],q)),(e[O]===1?q.cases:q.controls).push(t[O].map((V,W)=>V-h[W]))}let g=[...p.values()].map(O=>({x:[...O.cases,...O.controls],cases:O.cases.length})),d=2**-39,v=O=>{let q=O.map(V=>V.slice());return{f:q,rank:Ir(q,d)}},N=new Array(f).fill(0),b=xc(g,N),E=[b.loglik,b.loglik],{f:w}=v(b.info),y=_r(w,b.score),x=b.score.reduce((O,q,V)=>O+q*y[V],0),M=[],R=[N],T=b.loglik,A=N.map((O,q)=>O+y[q]),k=0,C=!1,_=0;for(_=1;_<=a;_++){b=xc(g,A);let O=Number.isFinite(b.loglik)&&b.score.every(Number.isFinite);if(O&&Math.abs(1-T/b.loglik)<=c){C=!0,N=A;break}!O||b.loglik<T?(k++,A=A.map((q,V)=>(q+k*N[V])/(k+1)),R.push(N)):(k=0,T=b.loglik,{f:w}=v(b.info),y=_r(w,b.score),N=A,R.push(N),A=A.map((q,V)=>q+y[V]))}C||(_=a,N=R[Math.max(1,a-1)]??N,M.push("Ran out of iterations and did not converge")),b=xc(g,N),E[1]=b.loglik;let I=v(b.info),F=to(I.f),L=N.map((O,q)=>I.rank<f&&F[q][q]===0?NaN:O),D=L.map((O,q)=>Number.isNaN(O)?-1:q).filter(O=>O>=0),S=0;for(let O of D)for(let q of D)S+=N[O]*b.info[O][q]*N[q];return u(L,F,E,x,S,_,"exact",M)}var Hu=["transform","terms","collapseTerms","global"],Un=e=>new Array(e).fill(0),hr=e=>Array.from({length:e},()=>Un(e));function ib(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 Yu(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 cb(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=Yu(i,a)-Yu(s,a);l.deaths>0&&(c*=1-l.deaths/f)}return n}function lb(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=cb(e);else if(t==="rank")o=ib(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 fb(e,t={}){B(t,Hu,"coxZph"),B(t,Hu,"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((P,Y)=>Number.isNaN(P)?-1:Y).filter(P=>P>=0),c=i.length;if(c===0)throw new Error("the proportional-hazards test has no estimable coefficients");let a=[];for(let P=0;P<s.sortedTime.length;){let Y=P;for(;!s.stratumEnd[Y];)Y++;for(let z=P;z<=Y;){let H=s.sortedTime[z],K=z;for(;K<Y&&s.sortedTime[K+1]===H;)K++;let te=[];for(let se=z;se<=K;se++)s.sortedEvent[se]&&te.push(se);s.start!==void 0&&te.reverse();for(let se of te)a.push(s.sortedToInput[se]);z=K+1}P=Y+1}if(a.length===0)throw new Error("the proportional-hazards test requires events");let l=a.map(P=>s.time[P]);if(new Set(l).size<2)throw new Error("the test needs events at two or more distinct times");let f=[...e.warnings],m=lb(s,n,a,f),u=m[a[0]];if(a.every(P=>m[P]===u))throw new Error("the transform is constant at the event times");let h=a.reduce((P,Y)=>Math.max(P,Math.abs(m[Y])),0),p=h===0?0:Math.floor(Math.log2(h)),g=m.map(P=>Number.isFinite(P)?$(P,-p):P),d=a.reduce((P,Y)=>P+g[Y],0)/a.length,v=g.map(P=>P-d);if(a.every(P=>v[P]===0))throw new Error("the transform is constant at the event times");let N=i.map(P=>s.betaScaled[P]),b=hr(c),E=hr(c),w=hr(c),y=Un(c),x=[];for(let P=0;P<s.sortedTime.length;){let Y=P;for(;!s.stratumEnd[Y];)Y++;for(let z=P;z<=Y;){let H=s.sortedTime[z],K=z;for(;K<Y&&s.sortedTime[K+1]===H;)K++;let te=[];for(let se=z;se<=K;se++)s.sortedEvent[se]&&te.push(se);if(te.length>0){let se=[],Me=-1/0;for(let oe=P;oe<=Y;oe++){if(!((s.sortedStart===void 0||s.sortedStart[oe]<H)&&s.sortedTime[oe]>=H))continue;se.push(oe);let be=0;for(let fe=0;fe<c;fe++)be+=N[fe]*s.sortedX[oe][i[fe]];Me=Math.max(Me,be)}if(se.length===0||!Number.isFinite(Me))throw new Error("empty Cox proportional-hazards risk set");let Ne=Un(c),de=hr(c),ce=Un(c),qe=hr(c),we=0,Oe=0;for(let oe of se){let ve=0;for(let fe=0;fe<c;fe++)ve+=N[fe]*s.sortedX[oe][i[fe]];let be=s.sortedWeight[oe]*Math.exp(ve-Me);we+=be;for(let fe=0;fe<c;fe++){let nt=s.sortedX[oe][i[fe]];Ne[fe]+=be*nt;for(let X=0;X<c;X++)de[fe][X]+=be*nt*s.sortedX[oe][i[X]]}if(s.sortedEvent[oe]&&s.sortedTime[oe]===H){Oe+=be;for(let fe=0;fe<c;fe++){let nt=s.sortedX[oe][i[fe]];ce[fe]+=be*nt;for(let X=0;X<c;X++)qe[fe][X]+=be*nt*s.sortedX[oe][i[X]]}}}let xe=0,_e=Un(c);for(let oe of te){let ve=s.sortedWeight[oe];xe+=ve;for(let be=0;be<c;be++)_e[be]+=ve*s.sortedX[oe][i[be]]}let Fe=te.length,We=s.ties==="efron"&&Fe>1?Fe:1,ze=Un(c),tt=hr(c);for(let oe=0;oe<We;oe++){let ve=oe/We,be=we-ve*Oe,fe=Ne.map((X,he)=>(X-ve*ce[he])/be);for(let X=0;X<c;X++)ze[X]+=fe[X]/We;let nt=xe/We;for(let X=0;X<c;X++)for(let he=0;he<c;he++)tt[X][he]+=nt*((de[X][he]-ve*qe[X][he])/be-fe[X]*fe[he])}let Ue=_e.map((oe,ve)=>oe-xe*ze[ve]),Xe=s.sortedToInput[te[0]],ge=v[Xe];for(let oe=0;oe<c;oe++){y[oe]+=ge*Ue[oe];for(let ve=0;ve<c;ve++)b[oe][ve]+=tt[oe][ve],E[oe][ve]+=ge*tt[oe][ve],w[oe][ve]+=ge*ge*tt[oe][ve]}s.start!==void 0&&te.reverse();for(let oe of te){let ve=s.sortedToInput[oe],be=i.map(fe=>s.sortedX[oe][fe]);for(let fe=0;fe<c;fe++)be[fe]-=ze[fe];x.push({input:ve,time:H,stratum:s.stratumIndex[oe],g:m[ve],residual:be})}}z=K+1}P=Y+1}let M;try{M=rt(b,E)}catch{throw new Error("the proportional-hazards information is singular")}let R=hr(c);for(let P=0;P<c;P++)for(let Y=0;Y<c;Y++){let z=0;for(let H=0;H<c;H++)z+=E[H][P]*M[H][Y];R[P][Y]=w[P][Y]-z}for(let P=0;P<c;P++)for(let Y=0;Y<P;Y++){let z=(R[P][Y]+R[Y][P])/2;R[P][Y]=z,R[Y][P]=z}let T=t.terms===!1||t.terms===void 0?i.map(P=>[P]):t.terms,A=new Set,k=[],C=[];if(T.forEach((P,Y)=>{if(!Array.isArray(P)||P.length===0)throw new RangeError("each terms group must be a non-empty array");let z=[];for(let H of P){if(!Number.isInteger(H)||H<0||H>=e.coefficients.length)throw new RangeError("terms column indices must be zero-based and within x");if(A.has(H))throw new RangeError("terms groups must not overlap");A.add(H),i.includes(H)&&z.push(H)}z.length>0&&(k.push(z),C.push(Y))}),i.forEach(P=>{A.has(P)||(k.push([P]),C.push(T.length))}),k.length===0)throw new Error("the proportional-hazards test has no estimable terms");let _=[];k.forEach((P,Y)=>{if(o&&P.length>1)_.push({col:-1,group:Y,collapsed:!0});else for(let z of P)_.push({col:z,group:Y,collapsed:!1})});let I=_.length,F=Array.from({length:c},()=>Un(I)),L=[],D=[];_.forEach((P,Y)=>{let z=k[P.group],H=C[P.group];if(P.collapsed){for(let K of z){let te=i.indexOf(K);F[te][Y]=N[te]}L.push(1),D.push(`g${H+1}`)}else{let K=i.indexOf(P.col);F[K][Y]=s.inverseCovariateScale[P.col],L.push(e.coefficients[P.col]),D.push(z.length>1?`g${H+1}x${P.col+1}`:`x${P.col+1}`)}});let S=Array.from({length:I},(P,Y)=>{let z=0;for(let H=0;H<c;H++)z=Math.max(z,Math.abs(F[H][Y]));if(!(z>0&&Number.isFinite(z)))throw new Error("the proportional-hazards residual variance is singular");return Math.floor(Math.log2(z))}),O=F.map(P=>P.map((Y,z)=>$(Y,-S[z]))),q=Array.from({length:c},()=>Un(I));for(let P=0;P<c;P++)for(let Y=0;Y<I;Y++)for(let z=0;z<c;z++)q[P][Y]+=b[P][z]*O[z][Y];let V=hr(I);for(let P=0;P<I;P++)for(let Y=0;Y<I;Y++)for(let z=0;z<c;z++)V[P][Y]+=O[z][P]*q[z][Y];for(let P=0;P<I;P++)for(let Y=0;Y<P;Y++){let z=(V[P][Y]+V[Y][P])/2;V[P][Y]=z,V[Y][P]=z}let W;try{W=rt(V)}catch{throw new Error("the proportional-hazards residual variance is singular")}for(let P=0;P<I;P++)for(let Y=0;Y<P;Y++){let z=(W[P][Y]+W[Y][P])/2;W[P][Y]=z,W[Y][P]=z}let Q=W.map((P,Y)=>P.map((z,H)=>a.length*Ve(z,-S[Y]-S[H]-s.weightExp))),re=x.map(P=>{let Y=Un(I);for(let z=0;z<I;z++)for(let H=0;H<c;H++)Y[z]+=O[H][z]*P.residual[H];return L.map((z,H)=>{let K=0;for(let te=0;te<I;te++)K+=Y[te]*W[te][H];return z+a.length*Ve(K,-S[H]-s.weightExp)})}),j=[],ee=[],U=[],ue=_.map(P=>P.collapsed?k[P.group].map(Y=>i.indexOf(Y)):[i.indexOf(P.col)]);for(let P of ue){let Y=P.map(te=>P.map(se=>R[te][se])),z=P.map(te=>y[te]),H;try{H=rt(Y,z)}catch{throw new Error("the proportional-hazards term information is singular")}let K=$(Math.max(0,z.reduce((te,se,Me)=>te+se*H[Me],0)),s.weightExp);j.push(K),ee.push(P.length),U.push(Rt(K,P.length,{lowerTail:!1}))}if(r){let P;try{P=rt(R,y)}catch{throw new Error("the global proportional-hazards information is singular")}let Y=$(Math.max(0,y.reduce((z,H,K)=>z+H*P[K],0)),s.weightExp);j.push(Y),ee.push(c),U.push(Rt(Y,c,{lowerTail:!1})),D.push("GLOBAL")}let Z={rowNames:D,chisq:j,df:ee,p:U,pValue:U,x:x.map(P=>P.g),time:x.map(P=>P.time),...s.strata===void 0?{}:{strata:x.map(P=>Nt(s.strata[P.input]))},y:re,var:Q,transform:n,warnings:f};return J("coxZph",Z,e)}var mb=["center"],ub=2**-1022;function hb(e,t,n={}){B(n,mb,"leveneTest");let r=ke(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"?ks(l):it(l);return l.map(m=>Math.abs(m-f))},i=Ie(o).groups.map(s),c=(l,f)=>l.some(m=>m>0&&m<ub)||l.every(m=>m===0)&&o[f].some(m=>m!==o[f][0]);if(i.some(c)){let l=o.map(u=>{let h=Ie([u]);return{d:s(h.groups[0]),k:h.k}}),f=Math.max(...l.map(u=>u.d.some(h=>h>0)?Math.floor(Math.log2(Math.max(...u.d)))+u.k:-1/0)),m=Number.isFinite(f)?f:0;i=l.map(u=>u.d.map(h=>$(h,u.k-m)))}let a=au(Nr(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 pb=["score"],db=["alternative","exact","variant","levels"];function gb(e,t,...n){B(Ts(n),pb,"propTrendTest");let{score:r}=Rs(["score"],n),o=Array.from(e),s=Array.from(t),i=o.length,c=r===void 0?o.map((N,b)=>b+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,b)=>!(N>=0&&N<=s[b])))throw new RangeError("elements of 'x' must be between 0 and 'n'");if(o.every(N=>N===0)||o.every((N,b)=>N===s[b]))throw new RangeError("all proportions are 0 or all are 1: the test for trend is not defined");let a=s.reduce((N,b)=>N+b,0),l=o.reduce((N,b)=>N+b,0)/a,f=s.map(N=>N/(l*(1-l))),m=o.map((N,b)=>N/s[b]),u=c.flatMap((N,b)=>Number.isNaN(N)?[]:[b]);if(u.length===0)throw new RangeError("no non-missing scores");if(u.some(N=>!Number.isFinite(c[N])))throw new RangeError("non-missing scores must be finite");if(u.every(N=>c[N]===c[u[0]]))throw new RangeError("at least 2 distinct non-missing scores are needed");let h=c,{sw:p,sxy:g,sxx:d}=Tc(f,m,h,u);if(!(d>=2**-1022&&d<1/0&&Math.abs(g)<1/0)){let N=Ie([u.map(b=>c[b])]);N.k!==0&&(h=c.slice(),u.forEach((b,E)=>h[b]=N.groups[0][E]),{sw:p,sxy:g,sxx:d}=Tc(f,m,h,u))}let v=Vt(g,d);if(!(a<1/0)||l>0&&l<1&&!(p<1/0&&Math.abs(g)<1/0&&d<1/0)){let{groups:[N,b],k:E}=Ie([o,s]),w=N.reduce((x,M)=>x+M,0)/b.reduce((x,M)=>x+M,0),y=Tc(b,m,h,u);v=$(y.sxy/w*(y.sxy/y.sxx/(1-w)),E)}return{method:"Chi-squared Test for Trend in Proportions",statistic:v,statisticName:"X-squared",parameter:1,parameterName:"df",pValue:bt(v,.5,2,!1,!1),warnings:[]}}function Tc(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 wb=2**24,bb=2**32,vb=`Sample size > 100 or data with ties
10
+ p-value based on normal approximation. Specify nperm for permutation p-value`,Nb=`Sample data with ties
11
+ p-value based on normal approximation. Specify nperm for permutation p-value.`;function yb(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(Nt);else{let f=[...new Set(i.map(m=>t[m]))];c=f.every(m=>typeof m=="number")?f.sort((m,u)=>m-u).map(Nt):[...new Set(f.map(Nt))].sort(An)}let a=c.map(()=>[]),l=new Map(c.map((f,m)=>[f,m]));for(let f of i){let m=l.get(Nt(t[f]));if(m===void 0)throw new RangeError(`the group '${Nt(t[f])}' is not among 'levels'`);a[m].push(o[f])}return a}function Mb(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,db,"jonckheereTest");let r=ke(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=yb(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,b)=>{m-=N.length,f.push([N.length,m]);for(let E=b+1;E<s.length;E++)for(let w of N)for(let y of s[E])l+=w<y?1:w===y?.5:0});let u=[],h=!a&&c<=100;o==="DescTools"&&n.exact!==void 0?(h=n.exact,h&&a&&(h=!1,u.push(Nb))):h||u.push(vb);let p,g;if(h){let N=1,b=0;for(let[w,y]of f)w===0||y===0||(b+=N*(w*y+1),N+=w*y);if(N>wb||b>bb)throw new RangeError(`the exact distribution of JT has ${N} values and needs ${b} steps, too many: use exact: false`);let E=[1];for(let[w,y]of f){if(w===0||y===0)continue;let x=Array.from({length:w*y+1},(R,T)=>Nl(T,w,y,!1)),M=new Array(E.length+x.length-1).fill(0);E.forEach((R,T)=>x.forEach((A,k)=>M[T+k]+=R*A)),E=M}p=0,g=0,E.forEach((w,y)=>{y<=l&&(p+=w),y>=l&&(g+=w)})}else{let N=f.reduce((w,[y,x])=>w+y*x,0)/2,b=a?Mb(s.map(w=>w.length),i):f.reduce((w,[y,x])=>w+y*x*(y+x+1)/12,0),E=(l-N)/Math.sqrt(b);p=ae(E),g=1-p,g<1e-8&&(g=ae(E,{lowerTail:!1}))}let v=r==="increasing"?g:r==="decreasing"?p:Math.min(1,2*Math.min(g,p,o==="clinfun"?.5:1));return J("jonckheereTest",{method:"Jonckheere-Terpstra test",alternative:r,statistic:l,statisticName:"JT",pValue:v,warnings:u})}var xb=["nClasses","adjust"];function Jo(e){return Dt(Array.from(e)).sort((t,n)=>t-n)}function Gu(e){if(e.length>0&&!(Number.isFinite(e[0])&&Number.isFinite(e[e.length-1])))throw new RangeError("'x' must be finite")}function ha(e){Gu(e);let t=hs(e);if(t.some(Number.isNaN))throw new RangeError("all 'x' values are identical");return t}function Rb(e){let t=Jo(e),n=t.length;if(n<8)throw new RangeError("sample size must be greater than 7");let r=ha(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 Tb(e){let t=Jo(e),n=t.length;if(n<8)throw new RangeError("sample size must be greater than 7");let r=ha(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")),J("cvmTest",{method:"Cramer-von Mises normality test",statistic:o,statisticName:"W",pValue:c,warnings:i})}function Ab(e){let t=Jo(e),n=t.length;if(n<5)throw new RangeError("sample size must be greater than 4");let r=ha(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=Jo(e),n=t.length;if(n<5||n>5e3)throw new RangeError("sample size must be between 5 and 5000");if(Gu(t),t[0]===t[n-1])throw new RangeError("all 'x' values are identical");let r=t.map((m,u)=>ie((u+1-3/8)/(n+1/4))),o=Ie([t]).groups[0],s=Math.min(dl(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=ae((Math.log(1-s)-a)/l,{lowerTail:!1});return{method:"Shapiro-Francia normality test",statistic:s,statisticName:"W",pValue:f,warnings:[]}}function kb(e,t={}){B(t,xb,"pearsonTest");let n=Jo(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 ha(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 qb=["weight"],Ib=["exact"],_b=["model","type","unit","r0","confLevel"];function es(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 pa(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 Qu=e=>2*ae(-Math.abs(e));function Lb(e,t={}){B(t,qb,"kappa2");let n=ke(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=>Pe(x)/o),p=Array.from({length:a},(x,M)=>Pe(f.map(R=>R[M]))/o),g=x=>{let M=[];for(let R=0;R<a;R++)for(let T=0;T<a;T++)M.push(x(R,T));return Pe(M)},d=g((x,M)=>f[x][M]*u[x][M])/o,v=g((x,M)=>h[x]*p[M]*u[x][M]),N=(d-v)/(1-v),b=Array.from({length:a},(x,M)=>Pe(p.map((R,T)=>R*u[M][T]))),E=Array.from({length:a},(x,M)=>Pe(h.map((R,T)=>R*u[T][M]))),w=(g((x,M)=>h[x]*p[M]*(u[x][M]-(b[x]+E[M]))**2)-v*v)/(o*(1-v)**2),y=N/Math.sqrt(w);return{value:N,statistic:y,pValue:Qu(y),warnings:[]}}function Cb(e,t={}){B(t,Ib,"kappamFleiss");let n=t.exact??!1,r=es(e,"ratings");pa(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((w,y)=>w-y),l=new Map(a.map((w,y)=>[w,y])),f=new Map(i.map((w,y)=>[w,l.get(c[y])])),m=a.length,u=e.map(w=>{let y=new Array(m).fill(0);for(let x of w)y[f.get(x)]++;return y}),h=Pe(u.map(w=>(Pe(w.map(y=>y*y))-s)/(s*(s-1))))/o,p=Array.from({length:m},(w,y)=>Pe(u.map(x=>x[y]))/(o*s)),g=Pe(p.map(w=>w*w));if(n){let w=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(w.map(R=>R[M]))*(s-1)/s);g-=Pe(y)/(s-1)}let d=(h-g)/(1-g);if(n)return{value:d,warnings:[]};let v=Pe(p.map(w=>w*(1-w))),N=Pe(p.map(w=>w*(1-w)*(1-w-w))),b=2/(v*v*o*s*(s-1))*(v*v-N),E=d/Math.sqrt(b);return{value:d,statistic:E,pValue:Qu(E),warnings:[]}}function Fb(e,t={}){B(t,_b,"icc");let n=ke(t.model,["oneway","twoway"]),r=ke(t.type,["consistency","agreement"]),o=ke(t.unit,["single","average"]),s=t.r0??0,i=t.confLevel??.95;_t(i),es(e,"ratings"),pa(e,"ratings");let c=Ie(e).groups,a=es(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(O=>it(O)))*f,h=at(a.map(O=>it(O)))*l,p=it(c.map(O=>at(O))),g=at(c.flat())*(l*f-1),d=g-u*(l-1)-h*(f-1),v=d>2**-20*g?d/((l-1)*(f-1)):Vb(c)/((l-1)*(f-1)),N=(O,q,V)=>ht(O,q,V,!1,!1),b=(O,q)=>m/2<1e-8?hn(m/2,O,q,!1,!1):hn(1-m/2,O,q,!0,!1),E=l-1;if(n==="oneway"||r==="consistency"){let O=n==="oneway"?p:v,q=n==="oneway"?l*(f-1):(l-1)*(f-1),V=u/O,W=V/b(E,q),Q=V*b(q,E),[re,j,ee]=o==="single"?[(u-O)/(u+(f-1)*O),V*(1-s)/(1+(f-1)*s),U=>(U-1)/(U+f-1)]:[(u-O)/u,V*(1-s),U=>1-1/U];return{value:re,Fvalue:j,fValue:j,df1:E,df2:q,pValue:N(j,E,q),lbound:ee(W),ubound:ee(Q),warnings:[]}}let w=o==="single"?f:1,y=O=>[w*O/(l*(1-O)),1+w*O*(l-1)/(l*(1-O))],x=(O,q)=>gl([O*h,q*v],[f-1,(l-1)*(f-1)]),M=o==="single"?(u-v)/(u+(f-1)*v+f/l*(h-v)):(u-v)/(u+(h-v)/l),[R,T]=y(s),A=u/(R*h+T*v),k=x(R,T),[C,_]=[f*M/(l*(1-M)),1+f*M*(l-1)/(l*(1-M))],I=x(C,_),F=b(E,I),L=b(I,E),[D,S]=o==="single"?[l*(u-F*v)/(F*(f*h+(f*l-f-l)*v)+l*u),l*(L*u-v)/(f*h+(f*l-f-l)*v+l*L*u)]:[l*(u-F*v)/(F*(h-v)+l*u),l*(L*u-v)/(h-v+l*L*u)];return{value:M,Fvalue:A,fValue:A,df1:E,df2:k,pValue:N(A,E,k),lbound:D,ubound:S,warnings:[]}}function Xu(e,t){let n=t.length,r=Pe(t.flatMap(s=>t.map(i=>e[s][i])));return(1-Pe(t.map(s=>e[s][s]))/r)*n/(n-1)}function Ob(e){let t=es(e,"x").length;if(pa(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=Is(Ie(e).groups),r=Io(e),o=Array.from({length:t},(c,a)=>a),s=Pe(r.flat()),i;try{let c=rt(r);i=o.map(a=>1-1/c[a][a])}catch(c){if(!r.flat().every(Number.isFinite))throw c;let a=Ie(e).groups;i=o.map(l=>{let f=a.map(h=>h[l]),m=Ur(Lt(a.map(h=>[1,...h.filter((p,g)=>g!==l)])),f),u=it(f);return 1-Pe(m.map(h=>h*h))/Pe(f.map(h=>(h-u)**2))})}return J("cronbachAlpha",{rawAlpha:Xu(n,o),stdAlpha:(1-t/s)*t/(t-1),G6:1-(t-Pe(i))/s,averageR:(s-t)/(t*(t-1)),alphaDrop:o.map(c=>Xu(n,o.filter(a=>a!==c))),warnings:Db(e,r)})}function Db(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}=Ut(r.map(c=>r.map(a=>t[c][a])),{symmetric:!0}),s=o.map(c=>c[0]),i=Pe(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 Ab(e,t,n){let r=Qo(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=Ao(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 kb(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 qb=["controls","cases"],Xu=["levels","direction","naRm"],Ib=["partialAuc","focus","correct"],_b=["bestMethod","bestWeights"],nh=new WeakMap;function ha(e){return nh.get(e)}var Lb=["auto","<",">"];function Tc(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 Cb(e){let t=e.filter(s=>Tc(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 Jo(e){return Float64Array.from(e).sort()}function Sc(e,t){let n=Jo(e),r=Jo(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 Ac(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 kc(e,t,n){let r=e.length,o=t.length;return Ac(Sc(e,t),r,n)/(2*o*r)}function qc(e,t,n){return Ac(Sc(e,t),e.length,n)}var Qu=e=>e===0n?0:e.toString(2).length;function no(e,t){if(t<0n&&(e=-e,t=-t),e===0n)return 0;let n=e<0n,r=n?-e:e,o=55-(Qu(r)-Qu(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 Fb(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 Ob(e,t,n){let r=Sc(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:Fb(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:Ac(r,o,n)/(2*s*o)}}function Db(e,t){let n=r=>{let o=Jo(r),s=o.length>>1;return o.length%2===1?o[s]:dn(o[s-1],o[s])};return n(e)<=n(t)?"<":">"}function rh(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"?Db(e,t):n,a={...Ob(e,t,i),direction:i,levels:r,controls:e,cases:t,warnings:s};return nh.set(a,o),a}function oh(e){if(e===void 0)return"auto";if(!Lb.includes(e))throw new RangeError("'arg' should be one of \u201Cauto\u201D, \u201C<\u201D, \u201C>\u201D");return e}function eo(e,t,n){let r=oh(n.direction),o=new Tt,s=e.map(Tc),i;if(n.levels===void 0){if(i=Cb(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=Tc(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 rh(a,l,r,[i[0],i[1]],h,o.list)}function Pb(e,t){let n=vs(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=ql(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 Vb(e){return typeof e=="object"&&e!==null&&!Array.isArray(e)&&!ArrayBuffer.isView(e)}function $b(e,t,n={}){if(ct(e)&&B(e,qb,"roc"),typeof e!="string"&&ct(t)&&B(t,Xu,"roc"),B(n,Xu,"roc"),typeof e=="string"){let r=Pb(e,t);return Z("roc",eo(r.response,r.predictor,n))}if(Vb(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=oh(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 Z("roc",rh(a,l,s,["0","1"],m,[]))}return Z("roc",eo(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 zb(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]],Cr=(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]],Zu=(e,t)=>t[0]<0n?[-e[0]*t[1],-e[1]*t[0]]:[e[0]*t[1],e[1]*t[0]],Zo=(e,t)=>{let n=e[0]*t[1]-t[0]*e[1];return n<0n?-1:n>0n?1:0};function Ju(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 eh(e,t,n,r,o,s){let i=Ju(e,n),c=Ju(t,r);if(i!==void 0&&c!==void 0)return th(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(to),f=t.map(to),m=0n;for(let[,h]of[...l,...f])m=h>m?h:m;let u=h=>h[0]*(m/h[1]);return th(l.map(u),f.map(u),m,m,o,s)}function th(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=to(o),f=to(s),m=(d,v,N)=>hr(c(v),qn(Zu(Cr(d,a(v)),Cr(a(N),a(v))),Cr(c(N),c(v)))),u=[];e.forEach((d,v)=>{Zo(a(v),l)>=0&&Zo(a(v),f)<=0&&u.push(v)});let h;if(u.length===0){let d=e.findIndex((N,b)=>Zo(a(b),f)>=0),v=d-1;h=qn(hr(m(f,v,d),m(l,v,d)),qn(Cr(f,l),[1n,2n]))}else{let d=0n;for(let b=1;b<u.length;b++){let E=u[b-1],g=u[b];d+=(e[g]-e[E])*(c(E)[0]+c(g)[0])}h=[d,2n*n*i];let v=u[u.length-1];if(Zo(a(v),f)!==0){let b=v+1;h=hr(h,qn(Cr(f,a(v)),qn(hr(c(v),m(f,v,b)),[1n,2n])))}let N=u[0];if(Zo(a(N),l)!==0){let b=N-1;h=hr(h,qn(Cr(a(N),l),qn(hr(c(N),m(l,N,b)),[1n,2n])))}}if(h[0]<0n)throw new Error("Partial AUC correction not defined for ROC curves below the diagonal.");let p=qn(qn(Cr(f,l),hr(f,l)),[1n,2n]),w=Zu(hr(p,h),qn(p,[2n,1n]));return no(w[0],w[1])}function jb(e,t={}){B(t,Ib,"auc");let n=Ae(t.focus,["specificity","sensitivity"]),r=t.correct??!1;if(t.partialAuc===void 0)return Z("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"?eh(l,f,u,h,i,s):eh(l,f,h,u,i,s):m=zb(l,f,i,s),Z("auc",{auc:m,partialAuc:[s,i],focus:n,correct:r,warnings:[]})}function to(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 Wb(e,t){let n=Jo(e.controls),r=Jo(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 Bb(e,t="all",n={}){B(n,_b,"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}=Wb(e,a);if(l==="best"){let[d,v]=to(s),[N,b]=to(i),E=(b-N)*v,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),_=BigInt(m-h[A]);return-(C*C*x*x*g+E*_*_*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 Z("coords",{threshold:a,sensitivity:u.map(d=>d/f),specificity:h.map(d=>d/m),accuracy:u.map((d,v)=>(d+h[v])/(f+m)),tn:h,tp:u,fn:p,fp:w,npv:h.map((d,v)=>d/(d+p[v])),ppv:u.map((d,v)=>d/(d+w[v])),fpr:w.map(d=>d/m),lrPos:u.map((d,v)=>d*m/(f*w[v])),lrNeg:p.map((d,v)=>d*m/(f*h[v])),youden:u.map((d,v)=>d/f+c*(h[v]/m)),closestTopleft:p.map((d,v)=>(d/f)**2+c*(w[v]/m)**2),warnings:[]})}var Kb=["confLevel","method","bootN","bootStratified"],sh=["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 Cc(e,t,n,r){return at(e)/(4*n*n)/r+at(t)/(4*r*r)/n}function ah(e){let{k10:t,k01:n}=ga(e);return Cc(t,n,e.controls.length,e.cases.length)}function pa(e,t,n,r,o,s){let i=BigInt(qc(e,t,n)),c=BigInt(qc(r,o,s)),a=BigInt(e.length)*BigInt(t.length),l=BigInt(r.length)*BigInt(o.length);return no(i*l-c*a,2n*a*l)}var _c=e=>e.sensitivities.some((t,n)=>t===1&&e.specificities[n]===1);function oo(e,t){if(e.controls.length<2||e.cases.length<2)throw new Error(`${t} needs at least 2 controls and 2 cases`)}function ch(e){let t=(1-e)/2;return t<1e-8?ie(t,{lowerTail:!1}):ie(1-t)}function lh(e){if(!(Number.isInteger(e)&&e>=1&&e<=2**24))throw new RangeError("bootN must be an integer from 1 to 2^24")}function so(e){let t=new Array(e.length);for(let n=0;n<e.length;n++)t[n]=e[Mo(e.length)];return t}function Lc(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 es(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 Ub(e,t,n,r){let o=es(e,t,n);return o===void 0?NaN:kc(o.controls,o.cases,r)}function ro(e){let t=new Array(e);for(let n=0;n<e;n++)t[n]=Mo(e);return t}function Hb(e,t={}){B(t,Kb,"ciAuc");let n=t.confLevel??.95;cn(n);let r=Ae(t.method,["delong","bootstrap"]),o=new Tt;_c(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"){oo(e,"DeLong's interval");let{k10:m,k01:u}=ga(e),h=e.auc,p=Cc(m,u,e.controls.length,e.cases.length),w=Math.sqrt(p),d=b=>Math.min(1,Math.max(0,b)),v=s/2,N=n===1?[0,1]:w===0?[h,h]:[d(h+ie(v)*w),d(h+ch(n)*w)];return Z("ciAuc",{estimate:h,confInt:N,confLevel:n,method:r,variance:p,warnings:o.list})}let i=t.bootN??2e3;lh(i);let c=t.bootStratified??!0,a=[],l=0;if(c){oo(e,"a stratified bootstrap");for(let m=0;m<i;m++){let u=so(e.controls),h=so(e.cases);a.push(kc(u,h,e.direction))}}else{let{values:m,isCase:u}=Lc(e);for(let h=0;h<i;h++){let p=Ub(m,u,ro(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=ks(a,[s/2,1-s/2]);return Z("ciAuc",{estimate:e.auc,confInt:[f[0],f[1]],confLevel:n,method:r,variance:a.length>1?at(a):NaN,warnings:o.list})}var ih=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 Yb(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)=>eo(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 Ic(e,t){return t==="two.sided"?2*se(-Math.abs(e)):t==="greater"?se(e,{lowerTail:!1}):se(e)}function Gb(e,t,n,r){ct(n)&&B(n,sh,"rocTest"),B(r,sh,"rocTest");let o,s=new Tt,i,c;if(ih(e)){if(!ih(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=eo(R,M.map(S=>y[S]),T),c=eo(R,M.map(S=>x[S]),T);for(let S of i.warnings)s.add(S)}let a=Ze(o.alternative),l=Ae(o.method,["delong","bootstrap"]),f=o.confLevel??.95;cn(f),_c(i)&&_c(c)&&s.add("roc.test() of two ROC curves with AUC == 1 has always p.value = 1 and can be misleading.");let m=Yb(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."),oo(i,"DeLong's test"),oo(c,"DeLong's test"),u){let C=ga(i),_=ga(c),q=C.k10.map((D,k)=>D-_.k10[k]),F=C.k01.map((D,k)=>D-_.k01[k]),L=Math.sqrt(Cc(q,F,i.controls.length,i.cases.length)),O=p===0&&L===0?0:p/L,I=L===0?0:ch(f)*L;return Z("rocTest",{method:"DeLong's test for two correlated ROC curves",statistic:O,statisticName:"Z",pValue:Ic(O,a),confInt:[p-I,p+I],confLevel:f,...h,warnings:s.list})}let g=ah(i),y=ah(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=Ic(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)),Z("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;lh(w);let d=o.bootStratified??!0,v=[],N=0;if(d){oo(i,"a stratified bootstrap"),oo(c,"a stratified bootstrap");for(let g=0;g<w;g++)if(u){let y=ro(i.controls.length),x=ro(i.cases.length);v.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=so(i.controls),x=so(i.cases),M=so(c.controls),R=so(c.cases);v.push(pa(y,x,i.direction,M,R,c.direction))}}else{let g=Lc(i),y=Lc(c);for(let x=0;x<w;x++){let M,R;if(u){let T=ro(g.values.length);M=es(g.values,g.isCase,T),R=es(y.values,y.isCase,T)}else M=es(g.values,g.isCase,ro(g.values.length)),R=es(y.values,y.isCase,ro(y.values.length));M===void 0||R===void 0?N++:v.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.`),v.length<2)throw new Error("fewer than 2 bootstrap replicates had both controls and cases");let b=Math.sqrt(at(v)),E=p===0&&b===0?0:p/b;return Z("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:Ic(E,a),bootStratified:d,...h,warnings:s.list})}var Xb=["method","confLevel","prevalence"];function Qb(e,t={}){B(t,Xb,"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,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?ie(w/2,{lowerTail:!1}):ie(1-w/2),v=(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,b=a/f,E={sensitivity:p(s,l),specificity:p(a,f),ppv:p(s,m),npv:p(a,u),lrPos:v(i===0?N/0:no(BigInt(s)*BigInt(f),BigInt(l)*BigInt(i)),c/(s*l)+a/(i*f)),lrNeg:v(a===0?c/l/b:no(BigInt(c)*BigInt(f),BigInt(l)*BigInt(a)),s/(c*l)+i/(a*f)),accuracy:p(s+a,h),diagnosticOR:v(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=b*(1-g),T=c/l*g;E.ppvAtPrevalence=x/(x+M),E.npvAtPrevalence=R/(R+T)}return Z("diagnosticTests",E)}var Zb=["two","mode","confLevel","ciMethod"],Jb=["package","exact"],In=(e,t)=>t===0?0:e*t;function ev(e,t,n={}){if(B(n,Zb,"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,Jb),c=[],a=[];for(let L=0;L<e.length;L++){let O=e[L],I=t[L];if(typeof O!="number"||typeof I!="number")throw new TypeError("'x' and 'y' must be numeric");if(!(Number.isNaN(O)||Number.isNaN(I))){if(!Number.isFinite(O)||!Number.isFinite(I))throw new RangeError("'x' and 'y' must not contain infinite values");c.push(O),a.push(I)}}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((L,O)=>{let I=(L+a[O])/2;return Number.isFinite(I)?I:L/2+a[O]/2}),h=f.map((L,O)=>L-m[O]),p=!h.every(Number.isFinite),{groups:w,k:d}=ke([p?f.map((L,O)=>L/2-m[O]/2):h]),v=w[0],N=d+(p?1:0),b=it(v),E=Zn(v),g=r*E,y=b-g,x=b+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=[b+In(T,E)/Math.sqrt(l),b+In(S,E)/Math.sqrt(l)],C,_;if(i==="package"){let L=3*E*E/l,O=L>=2**-1022||L===0?Math.sqrt(L):E*Math.sqrt(3/l);C=[y+In(T,O),y+In(S,O)],_=[x+In(T,O),x+In(S,O)]}else{let L=r*Math.sqrt(l),O=_a(R,M,L,!0,!1)/Math.sqrt(l),I=_a(R,M,L,!1,!1)/Math.sqrt(l);C=[b-In(I,E),b-In(O,E)],_=[b+In(O,E),b+In(I,E)]}let q=L=>$(L,N),F=L=>[q(L[0]),q(L[1])];return Z("blandAltman",{means:u,diffs:h,n:l,meanDiff:q(b),lowerLimit:q(y),upperLimit:q(x),criticalDiff:q(g),meanDiffConfInt:F(A),lowerLimitConfInt:F(C),upperLimitConfInt:F(_),confLevel:s,two:r,mode:o,ciMethod:i,warnings:[]})}var tv=new Set(["methodReg","methodCi","methodBootstrapCi","errorRatio","confLevel","slopeMeasure","nsamples","nnested","rngSeed","iterMax","threshold","naRm"]),nv=["PaBa","Deming","WDeming"],rv=["analytical","jackknife","bootstrap","nestedbootstrap"],ov=["quantile","Student","BCa","tBoot"],fh=5793,sv=2**27;function Dc(e,t){let n=e-t;return Math.abs(n)<1e-12*(Math.abs(e)+Math.abs(t))/2?0:n}function av(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 ph(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 Pc(e,t,n){let r=e.length,{u:o,q:s,p:i,mx:c,my:a}=av(e,t),l=ph(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 v of e)w=he(w,_e([v,0],[v,0]));let d=Math.sqrt(p*p*ft(w,r)[0]);return{b0:f,b1:l,se0:d,se1:p,xmean:c[0]}}function mh(e,t,n,r,o,s,i,c,a){let l=e.length,f=Pc(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 I=0;I<l;I++){let D=t[I]-(m+u*e[I]),k=e[I]+n*u*D/y,V=t[I]-D/y;p[I]=r>1?k/r+V:k+r*V,x=Math.min(x,Math.abs(p[I]))}let M=[0,0],R=[0,0],T=[0,0];for(let I=0;I<l;I++){let D=x/p[I],k=[D*D,0];M=he(M,k),R=he(R,_e(k,[e[I],0])),T=he(T,_e(k,[t[I],0]))}let S=ft(R,M[0]+M[1]),A=ft(T,M[0]+M[1]),C=[0,0],_=[0,0],q=[0,0];for(let I=0;I<l;I++){let D=ft(Le([e[I],0],S),p[I]),k=ft(Le([t[I],0],A),p[I]);C=he(C,_e(D,D)),_=he(_,_e(k,k)),q=he(q,_e(D,k))}let F=ph(C[0],_[0],q[0],n),L=Le(A,_e([F,0],S))[0];h=S[0];let O=$(Math.abs(u-F),a-c)<i&&$(Math.abs(m-L),a)<i;if(m=L,u=F,O){w=g+1;break}}let[d,v]=Fc(o),[N,b]=Fc(1+o),E=p.map(g=>{if(g===0)return 1/0;let[y,x]=Fc(Math.abs(g));return r>1?Ve((N/(d*y))**2,2*(b-v-x-a)):Ve((N/y)**2,2*(b-x-c))});return{b0:m,b1:u,xmean:h,weights:E,converged:w<s}}function Fc(e){let t=Math.floor(Math.log2(e));return[Ve(e,-t),t]}var co=e=>Math.atan(e),io=e=>Number.isFinite(e)?e:Math.tan(co(e));function iv(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=Dc(e[u],e[m]),p=Dc(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=co(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 cv(e,t,n){let r=e.length;if(t%2!==0){let c=(t+1)/2;return c>=1&&c<=r?io(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?(io(s)+io(i))/2:Math.tan((co(s)+co(i))/2)}function lv(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 Vc(e,t,n){return Number.isFinite(n)?lv(e.map((r,o)=>Dc(t[o],n*r))):-n}function uh(e,t,n){let r=iv(e,t),o=cv(r.sorted,r.sorted.length+r.offset,n);return{b0:Number.isNaN(o)?NaN:Vc(e,t,o),b1:o,slopes:r}}function fv(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 v=i[d-1],N=i[d];return o?(io(v)+io(N))/2:Math.tan((co(v)+co(N))/2)}return io(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:[Vc(e,t,h),Vc(e,t,u)],defined:!0}}function mv(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 Oc(e,t){if(!(typeof e=="number"&&Number.isInteger(e)&&e>=1))throw new RangeError(`'${t}' must be a positive integer`);return e}var ao=(e,t)=>ks(e,t),hh=e=>Zn(e.filter(t=>!Number.isNaN(t)));function uv(e,t,n={}){B(n,tv,"mcreg");let r=Ae(n.methodReg,nv),o=Ae(n.methodCi??(r==="WDeming"?"jackknife":"analytical"),rv),s=o==="bootstrap"||o==="nestedbootstrap",i=Ae(n.methodBootstrapCi,ov),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=Oc(n.nsamples??999,"nsamples"),h=Oc(n.nnested??25,"nnested"),p=Oc(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=[],v=[];for(let te=0;te<e.length;te++){let P=e[te],Y=t[te];if(typeof P!="number"||typeof Y!="number")throw new TypeError("'x' and 'y' must be numeric");if(Number.isNaN(P)||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(P)||!Number.isFinite(Y))throw new RangeError("'x' and 'y' must not contain infinite values");d.push(P),v.push(Y)}let N=d.length;if(N<=2)throw new RangeError("mcreg needs at least 3 complete pairs");if(wa(d)||wa(v))throw new RangeError("'x' and 'y' must not be constant");if(r==="PaBa"){if(d.some(P=>P<0)||v.some(P=>P<0))throw new RangeError("Passing-Bablok regression needs nonnegative values");if(N>fh)throw new RangeError(`Passing-Bablok regression is limited to ${fh} 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)>sv)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(te=>te<=0)||v.some(te=>te<=0)))throw new RangeError("weighted Deming regression needs positive values");let b=ke([d,v]),[E,g]=f?[ke([d]).k,ke([v]).k]:[b.k,b.k],y=f?d.map(te=>$(te,-E)):b.groups[0],x=f?v.map(te=>$(te,-g)):b.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=(te,P)=>{if(r==="Deming")return Pc(te,P,M);if(r==="WDeming"){let Y=mh(te,P,M,R,m,p,w,E,g);return Y.converged||T.add(S),Y}return uh(te,P,l)},C,_=NaN,q,F,L;if(r==="Deming"){let te=Pc(y,x,M);C=te,_=$(te.xmean,E),F=[te.se0,te.se1]}else if(r==="WDeming"){let te=mh(y,x,M,R,m,p,w,E,g);te.converged||T.add(S),C=te,_=$(te.xmean,E),q=te.weights}else{let te=uh(y,x,l);C=te,L=te.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,I=gt(O/2,N-2,!1,!1),D=ie(O/2,{lowerTail:!1}),k=[C.b0,C.b1],V=[NaN,NaN],K=[[NaN,NaN],[NaN,NaN]],X,re,z,J=()=>{let te=[],P=[];for(let Y=0;Y<N;Y++){let j=A(y.filter((U,W)=>W!==Y),x.filter((U,W)=>W!==Y));te.push(j.b0),P.push(j.b1)}X=[te,P],re=[(N-1)*Zn(te)/Math.sqrt(N),(N-1)*Zn(P)/Math.sqrt(N)]};if(o==="analytical")if(r==="Deming")for(let te of[0,1])V[te]=F[te],K[te]=[k[te]-I*V[te],k[te]+I*V[te]];else{let te=fv(y,x,L,D,l);te.defined||T.add("the Passing-Bablok interval is not defined for so few points: its limits are -Inf and Inf"),K[0]=te.intercept,K[1]=te.slope}else if(o==="jackknife"){J();for(let te of[0,1])V[te]=re[te],K[te]=[k[te]-I*V[te],k[te]+I*V[te]]}else{if(i==="BCa"&&(J(),X.some(de=>de.some(Number.isNaN))))throw new RangeError("Too many identical data points!");let te=r==="Deming"?F:r==="WDeming"&&re!==void 0?re:[NaN,NaN],P=o==="nestedbootstrap",Y=[],j=[],U=[],W=[],ne=0,fe=()=>Array.from({length:N},()=>Mo(N)),Te=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=Te(Fe),Pe;if(P){let Oe=[],Ke=[];for(let ze=0;ze<h;ze++){let et=Te(fe().map(Ue=>Fe[Ue]));Oe.push(et.b0),Ke.push(et.b1)}Pe=[hh(Oe),hh(Ke)]}else r==="Deming"?Pe=[xe.se0,xe.se1]:Pe=te;if(Number.isNaN(xe.b0)||Number.isNaN(xe.b1)){ne++;continue}Y.push(xe.b0),j.push(xe.b1),U.push(Pe[0]),W.push(Pe[1])}};if(n.rngSeed!==void 0){let de=Ml()?Rl():void 0;try{xl({kind:"Mersenne-Twister"}),El(n.rngSeed),Ce()}finally{de!==void 0&&Tl(de)}}else Ce();if(ne>.05*u)throw new RangeError("There were too many resamples with undetermined regression coefficients!");let be=Y.length,me=[Y,j],qe=[U,W];for(let de of[0,1]){let Fe=me[de],xe=k[de];if(i==="quantile")K[de]=[ao(Fe,O/2),ao(Fe,1-O/2)];else if(i==="Student")V[de]=Zn(Fe),K[de]=[xe-I*V[de],xe+I*V[de]];else if(i==="BCa"){let Pe=Fe.filter(le=>le<xe).length;Fe.every(le=>le===xe)?Pe=be/2:Pe===0?Pe=.5:Pe===be&&(Pe=be-.5);let Oe=ie(Pe/be),Ke=X[de],ze=Ke.reduce((le,tt)=>le+tt,0)/Ke.length,et=0,Ue=0;for(let le of Ke){let tt=ze-le;et+=tt*tt,Ue+=tt*tt*tt}let Xe=et===0?0:Ue/(6*et**1.5),pe=-D,oe=D,we=se(Oe-(pe-Oe)/(1+Xe*(pe-Oe))),ge=se(Oe-(oe-Oe)/(1+Xe*(oe-Oe)));K[de]=[ao(Fe,ge),ao(Fe,we)]}else{let Pe=Fe.map((Ke,ze)=>(Ke-xe)/qe[de][ze]),Oe=Number.isNaN(te[de])?Zn(Fe):te[de];V[de]=Oe,K[de]=[xe-ao(Pe,1-O/2)*Oe,xe-ao(Pe,O/2)*Oe]}}z={intercept:Y.map(de=>$(de,g)),slope:j.map(de=>$(de,g-E)),sigmaIntercept:U.map(de=>$(de,g)),sigmaSlope:W.map(de=>$(de,g-E)),globalSigma:[$(te[0],g),$(te[1],g-E)],nsamples:be}}let Q=te=>{let P=Y=>$(Y,te===0?g:g-E);return{estimate:P(k[te]),se:P(V[te]),confInt:[P(K[te][0]),P(K[te][1])]}},Re={methodReg:r,methodCi:o,...s?{methodBootstrapCi:i}:{},confLevel:c,errorRatio:m,n:N,intercept:Q(0),slope:Q(1),xmean:_,...q===void 0?{}:{weights:q},...X===void 0?{}:{jackknife:{intercept:X[0].map(te=>$(te,g)),slope:X[1].map(te=>$(te,g-E))}},...z===void 0?{}:{bootstrap:z},...r==="PaBa"?{cusum:mv(y,x,C.b0,C.b1)}:{},warnings:[...T]};return Z("mcreg",Re)}var hv=["alpha","confLevel","lmer"],pv={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 dv(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 dh=(e,t)=>e.every((n,r)=>n===t[r]);function Fr(e){let t=[0,0];for(let n of e)t=he(t,n);return ft(t,e.length)}var lo=(e,t)=>({v:ft(e.v,t),e:e.e});function fo(...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 gh(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 wh=(e,t)=>Ve(e.v[0]/t.v[0],e.e-t.e),gv=(e,t)=>{let[n,r]=fo(e,t);return n.at<r.at},wv=(e,t)=>{let[n,r]=fo(e,t);return n.at<=r.at};function bh(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 bv(e,t={}){B(t,hv,"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}=ke(e);if(c.every(ee=>dh(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(ce=>ce[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,ce)=>ba(ve,m[0][ce]))),h=m.map(ee=>ee.map(ve=>ba(ve,ee[0]))),p=h.map(ee=>ee.map((ve,ce)=>Le(ve,h[0][ce]))),w=ee=>{let ve=0;for(let ce of ee)for(let Se of ce)ve=Math.max(ve,Math.abs(Se[0]));return ve===0?0:Math.floor(Math.log2(ve))},d=(ee,ve)=>ee.map(ce=>ce.map(Se=>[$(Se[0],-ve),$(Se[1],-ve)])),[v,N,b]=[w(u),w(h),w(p)],[E,g,y]=[d(u,v),d(h,N),d(p,b)],x=ee=>Array.from({length:s},(ve,ce)=>ee.map(Se=>Se[ce])),M=bh(E.map(Fr),Fr(E.flat()),s),R=a.every(ee=>dh(ee,a[0]))?[0,0]:bh(x(g).map(Fr),Fr(g.flat()),i),T=[0,0];if(!dv(c)){let ee=y.map(Fr),ve=x(y).map(Fr),ce=Fr(y.flat());y.forEach((Se,Be)=>Se.forEach(($e,st)=>{let mt=he(Le(Le($e,ee[Be]),ve[st]),ce);T=he(T,_e(mt,mt))}))}let S=(ee,ve)=>({v:ee,e:2*(ve+f)}),A=S(M,v),C=S(R,N),_=S(T,b),q=i-1,F=s-1,L=(i-1)*(s-1),O=lo(A,q),I=lo(C,F),D=lo(_,L),k=lo(gh(C,_),F+L),V=[];if(n){let ee={B:{ss:A,df:q,ms:O,pooled:!1},J:{ss:C,df:F,ms:I,pooled:!1}},ve=_,ce=L;for(let Be of wv(O,I)?["B","J"]:["J","B"]){let $e=ee[Be];gv($e.ms,lo(ve,ce))&&(ve=gh(ve,$e.ss),ce+=$e.df,$e.pooled=!0)}let Se=lo(ve,ce);if(ee.B.pooled&&(O=Se),ee.J.pooled&&(I=Se),D=Se,ee.B.pooled){let[Be,$e]=fo(D,I);k={v:[Be.at+($e.at-Be.at)/i,0],e:Be.E}}}else T[0]===0&&V.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,ce)=>ht(ee,ve,ce,!1,!1),re=ee=>ee===1/0?1:(ee-1)/(ee+s-1),z=ee=>1-1/ee,J=i*(s-1),Q=wh(O,k),Re=wh(O,D),[te,P]=[Q/K(q,J),Q*K(J,q)],[Y,j]=[Re/K(q,L),Re*K(L,q)],[U,W]=fo(O,k).map(ee=>ee.at),[ne,fe]=fo(O,D).map(ee=>ee.at),[Te,Ce,be]=fo(O,I,D).map(ee=>ee.at),me=(U-W)/(U+(s-1)*W),qe=(Te-be)/(Te+(s-1)*be+s*(Ce-be)/i),de=(ne-fe)/(ne+(s-1)*fe),Fe=s*qe*Ce,xe=(i*(1+(s-1)*qe)-s*qe)*be,Pe=Math.max(Math.abs(Fe),Math.abs(xe)),Oe=Pe>0&&Number.isFinite(Pe)?Math.floor(Math.log2(Pe)):0,Ke=$(Fe,-Oe),ze=$(xe,-Oe),et=(i-1)*Ke*Ke+ze*ze,Ue=et===0?s-1:(s-1)*(i-1)*(Ke+ze)**2/et,Xe=K(q,Ue),pe=K(Ue,q),oe=s*Ce+(s*i-s-i)*be,we=(ee,ve)=>ee>.5?1-(i*be+oe)/ve:ee,ge=we(i*(Te/Xe-be)/(oe+i*Te/Xe),oe+i*Te/Xe),le=pe>1?we(i*(Te-be/pe)/(oe/pe+i*Te),oe+i*pe*Te):we(i*(pe*Te-be)/(oe+i*pe*Te),oe+i*pe*Te),tt=ee=>ee*s/(1+ee*(s-1)),G=X(Q,q,J),ue=X(Re,q,L),Me=(ee,ve,ce,Se,Be,$e,st)=>({type:ee,name:pv[ee],ICC:ve,F:ce,df1:q,df2:Se,pValue:Be,lower:$e,upper:st}),Ne=[Me("ICC1",me,Q,J,G,re(te),re(P)),Me("ICC2",qe,Re,L,ue,ge,le),Me("ICC3",de,Re,L,ue,re(Y),re(j)),Me("ICC1k",(U-W)/U,Q,J,G,z(te),z(P)),Me("ICC2k",(Te-be)/(Te+(Ce-be)/i),Re,L,ue,tt(ge),tt(le)),Me("ICC3k",(ne-fe)/ne,Re,L,ue,z(Y),z(j))],Ee=ee=>Ve(ee.v[0],ee.e);return Z("ICC",{results:Ne,MSB:Ee(O),MSJ:Ee(I),MSE:Ee(D),MSW:Ee(k),n:i,k:s,confLevel:o,lmer:n,warnings:V})}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(!Ul(Dl(e)))throw new RangeError("'covmat' must be symmetric")}var vv=["covmat","nObs","rotation","scores","control"],Nv=["lower","start"];function kt(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 $c=(e,t)=>e[0].map((n,r)=>kt(e.map(o=>t(o[r]))));function yh(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 vh(e,t,n){let r=Bt(yh(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 Nh(e,t){let n=$c(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=$c(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 _n=(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})),mo=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(kt(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=mo(o),a=0;for(let m=0;m<1e3;m++){let u=_n(o,i),h=u[0].map((N,b)=>{let E=0;for(let g=0;g<s;g++)E+=u[g][b]*u[g][b];return E}),p=u.map(N=>N.map((b,E)=>b**3-b*h[E]/s)),w=_n(c,p),d=Ss(w);i=_n(d.u,mo(d.v));let v=a;if(a=kt(d.d),a<v*(1+1e-5))break}let l=_n(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 yv(e){let t=Na(e),n=t.loadings,r=n.map(c=>c.map(a=>a*Math.abs(a)**3)),o=xs(Lt(n),r),s=nt(_n(mo(o),o)).map((c,a)=>c[a]),i=o.map(c=>c.map((a,l)=>a*Math.sqrt(s[l])));return{loadings:_n(n,i),rotmat:_n(t.rotmat,i)}}function Mv(e,t,n={}){B(n,vv,"factanal"),B(wn(n,"control"),Nv,"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=As(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(yh(f,x),{symmetric:!0,onlyValues:!0}).values.slice(c);return-(kt(M.map(R=>Math.log(R)-R))-c+i)},p=x=>{let M=vh(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(()=>ko(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,v=Nh(vh(f,d,c)).L,N,b=n.rotation??"varimax";if(b!=="varimax"&&b!=="promax"&&b!=="none")throw new RangeError(`unknown rotation '${String(b)}'`);if(b!=="none"&&c>=2){let x=b==="varimax"?Na(v):yv(v),M=Nh(x.loadings,x.rotmat);v=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=xt(E,a,{lowerTail:!1}));let y={uniquenesses:d,loadings:v,STATISTIC:E,dof:a,PVAL:g,converged:!0,objective:w.value,counts:w.counts,warnings:[]};return r!=="none"&&(y.scores=Ev(e,f,d,v,r)),N!==void 0&&(y.rotmat=N),y}function Ev(e,t,n,r,o){let s=e.length,i=e[0].map((m,u)=>kt(e.map(h=>h[u]))/s),c=e.map(m=>m.map((u,h)=>u-i[h])),a=$c(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 _n(l,nt(t,r));let f=r.map((m,u)=>m.map(h=>h/n[u]));return _n(l,mo(nt(_n(mo(r),f),mo(f))))}var xv=["nfactors","covmat"],uo=2**-52,Rv=`
13
+ To do this, run the function again with the 'check.keys=TRUE' option`)}return n}function Pb(e,t,n){let r=es(e,"x").length;pa(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=Io(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=rt(a,l);for(let m=0;m<2;m++)for(let u=0;u<2;u++)c[m][u]-=Pe(i.map((h,p)=>o[s[m]][h]*f[p][u]))}return c[0][1]/Math.sqrt(c[0][0]*c[1][1])}function Vb(e){let t=e.length,n=e[0].length,r=a=>a.reduce((l,f)=>pe(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=pe(De(De([f,0],o[l]),s[m]),i);c=pe(c,Ce(u,u))})),c[0]+c[1]}var $b=["controls","cases"],Zu=["levels","direction","naRm"],zb=["partialAuc","focus","correct"],jb=["bestMethod","bestWeights"],oh=new WeakMap;function ga(e){return oh.get(e)}var Kb=["auto","<",">"];function Ac(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 Bb(e){let t=e.filter(s=>Ac(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 ns(e){return Float64Array.from(e).sort()}function Sc(e,t){let n=ns(e),r=ns(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 kc(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 qc(e,t,n){let r=e.length,o=t.length;return kc(Sc(e,t),r,n)/(2*o*r)}function Ic(e,t,n){return kc(Sc(e,t),e.length,n)}var Ju=e=>e===0n?0:e.toString(2).length;function so(e,t){if(t<0n&&(e=-e,t=-t),e===0n)return 0;let n=e<0n,r=n?-e:e,o=55-(Ju(r)-Ju(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 Wb(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 Ub(e,t,n){let r=Sc(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:Wb(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:kc(r,o,n)/(2*s*o)}}function Hb(e,t){let n=r=>{let o=ns(r),s=o.length>>1;return o.length%2===1?o[s]:gn(o[s-1],o[s])};return n(e)<=n(t)?"<":">"}function sh(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"?Hb(e,t):n,a={...Ub(e,t,i),direction:i,levels:r,controls:e,cases:t,warnings:s};return oh.set(a,o),a}function ah(e){if(e===void 0)return"auto";if(!Kb.includes(e))throw new RangeError("'arg' should be one of \u201Cauto\u201D, \u201C<\u201D, \u201C>\u201D");return e}function ro(e,t,n){let r=ah(n.direction),o=new At,s=e.map(Ac),i;if(n.levels===void 0){if(i=Bb(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 g=Ac(p);if(g===null)throw new RangeError("'levels' must not be missing");return g})}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,g)=>p===null||Number.isNaN(t[g]));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,g)=>{if(c[g])return;let d=t[g];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(g)});let h={labels:s,predictor:[...t],values:f,isCase:m,kept:u};return sh(a,l,r,[i[0],i[1]],h,o.list)}function Yb(e,t){let n=Ms(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=Il(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 Gb(e){return typeof e=="object"&&e!==null&&!Array.isArray(e)&&!ArrayBuffer.isView(e)}function Xb(e,t,n={}){if(ct(e)&&B(e,$b,"roc"),typeof e!="string"&&ct(t)&&B(t,Zu,"roc"),B(n,Zu,"roc"),typeof e=="string"){let r=Yb(e,t);return J("roc",ro(r.response,r.predictor,n))}if(Gb(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=ah(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 J("roc",sh(a,l,s,["0","1"],m,[]))}return J("roc",ro(Array.from(e),Array.from(t),n))}var da=(e,t,n,r,o)=>t[r]+(n-e[r])/(e[o]-e[r])*(t[o]-t[r]);function Qb(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=da(e,t,r,a,l),m=da(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=da(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=da(e,t,n,l,a);s+=(e[l]-n)*(t[l]+f)/2}return s}var pr=(e,t)=>[e[0]*t[1]+t[0]*e[1],e[1]*t[1]],Or=(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]],eh=(e,t)=>t[0]<0n?[-e[0]*t[1],-e[1]*t[0]]:[e[0]*t[1],e[1]*t[0]],ts=(e,t)=>{let n=e[0]*t[1]-t[0]*e[1];return n<0n?-1:n>0n?1:0};function th(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 nh(e,t,n,r,o,s){let i=th(e,n),c=th(t,r);if(i!==void 0&&c!==void 0)return rh(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(oo),f=t.map(oo),m=0n;for(let[,h]of[...l,...f])m=h>m?h:m;let u=h=>h[0]*(m/h[1]);return rh(l.map(u),f.map(u),m,m,o,s)}function rh(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=oo(o),f=oo(s),m=(d,v,N)=>pr(c(v),qn(eh(Or(d,a(v)),Or(a(N),a(v))),Or(c(N),c(v)))),u=[];e.forEach((d,v)=>{ts(a(v),l)>=0&&ts(a(v),f)<=0&&u.push(v)});let h;if(u.length===0){let d=e.findIndex((N,b)=>ts(a(b),f)>=0),v=d-1;h=qn(pr(m(f,v,d),m(l,v,d)),qn(Or(f,l),[1n,2n]))}else{let d=0n;for(let b=1;b<u.length;b++){let E=u[b-1],w=u[b];d+=(e[w]-e[E])*(c(E)[0]+c(w)[0])}h=[d,2n*n*i];let v=u[u.length-1];if(ts(a(v),f)!==0){let b=v+1;h=pr(h,qn(Or(f,a(v)),qn(pr(c(v),m(f,v,b)),[1n,2n])))}let N=u[0];if(ts(a(N),l)!==0){let b=N-1;h=pr(h,qn(Or(a(N),l),qn(pr(c(N),m(l,N,b)),[1n,2n])))}}if(h[0]<0n)throw new Error("Partial AUC correction not defined for ROC curves below the diagonal.");let p=qn(qn(Or(f,l),pr(f,l)),[1n,2n]),g=eh(pr(p,h),qn(p,[2n,1n]));return so(g[0],g[1])}function Zb(e,t={}){B(t,zb,"auc");let n=ke(t.focus,["specificity","sensitivity"]),r=t.correct??!1;if(t.partialAuc===void 0)return J("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"?nh(l,f,u,h,i,s):nh(l,f,h,u,i,s):m=Qb(l,f,i,s),J("auc",{auc:m,partialAuc:[s,i],focus:n,correct:r,warnings:[]})}function oo(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 Jb(e,t){let n=ns(e.controls),r=ns(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 ev(e,t="all",n={}){B(n,jb,"coords");let r=ke(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=ke(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}=Jb(e,a);if(l==="best"){let[d,v]=oo(s),[N,b]=oo(i),E=(b-N)*v,w=d*N,y=BigInt(f),x=BigInt(m),M=u.map((A,k)=>{if(r==="youden")return BigInt(A)*x*w+E*BigInt(h[k])*y;let C=BigInt(f-A),_=BigInt(m-h[k]);return-(C*C*x*x*w+E*_*_*y*y)}),R=M.reduce((A,k)=>k>A?k:A),T=M.map((A,k)=>A===R?k:-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),g=h.map(d=>m-d);return J("coords",{threshold:a,sensitivity:u.map(d=>d/f),specificity:h.map(d=>d/m),accuracy:u.map((d,v)=>(d+h[v])/(f+m)),tn:h,tp:u,fn:p,fp:g,npv:h.map((d,v)=>d/(d+p[v])),ppv:u.map((d,v)=>d/(d+g[v])),fpr:g.map(d=>d/m),lrPos:u.map((d,v)=>d*m/(f*g[v])),lrNeg:p.map((d,v)=>d*m/(f*h[v])),youden:u.map((d,v)=>d/f+c*(h[v]/m)),closestTopleft:p.map((d,v)=>(d/f)**2+c*(g[v]/m)**2),warnings:[]})}var tv=["confLevel","method","bootN","bootStratified"],ih=["method","alternative","paired","confLevel","bootN","bootStratified","levels","direction"];function va(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 Fc(e,t,n,r){return at(e)/(4*n*n)/r+at(t)/(4*r*r)/n}function ch(e){let{k10:t,k01:n}=va(e);return Fc(t,n,e.controls.length,e.cases.length)}function wa(e,t,n,r,o,s){let i=BigInt(Ic(e,t,n)),c=BigInt(Ic(r,o,s)),a=BigInt(e.length)*BigInt(t.length),l=BigInt(r.length)*BigInt(o.length);return so(i*l-c*a,2n*a*l)}var Lc=e=>e.sensitivities.some((t,n)=>t===1&&e.specificities[n]===1);function io(e,t){if(e.controls.length<2||e.cases.length<2)throw new Error(`${t} needs at least 2 controls and 2 cases`)}function fh(e){let t=(1-e)/2;return t<1e-8?ie(t,{lowerTail:!1}):ie(1-t)}function mh(e){if(!(Number.isInteger(e)&&e>=1&&e<=2**24))throw new RangeError("bootN must be an integer from 1 to 2^24")}function co(e){let t=new Array(e.length);for(let n=0;n<e.length;n++)t[n]=e[Ro(e.length)];return t}function Cc(e){let t=ga(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 rs(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 nv(e,t,n,r){let o=rs(e,t,n);return o===void 0?NaN:qc(o.controls,o.cases,r)}function ao(e){let t=new Array(e);for(let n=0;n<e;n++)t[n]=Ro(e);return t}function rv(e,t={}){B(t,tv,"ciAuc");let n=t.confLevel??.95;ln(n);let r=ke(t.method,["delong","bootstrap"]),o=new At;Lc(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"){io(e,"DeLong's interval");let{k10:m,k01:u}=va(e),h=e.auc,p=Fc(m,u,e.controls.length,e.cases.length),g=Math.sqrt(p),d=b=>Math.min(1,Math.max(0,b)),v=s/2,N=n===1?[0,1]:g===0?[h,h]:[d(h+ie(v)*g),d(h+fh(n)*g)];return J("ciAuc",{estimate:h,confInt:N,confLevel:n,method:r,variance:p,warnings:o.list})}let i=t.bootN??2e3;mh(i);let c=t.bootStratified??!0,a=[],l=0;if(c){io(e,"a stratified bootstrap");for(let m=0;m<i;m++){let u=co(e.controls),h=co(e.cases);a.push(qc(u,h,e.direction))}}else{let{values:m,isCase:u}=Cc(e);for(let h=0;h<i;h++){let p=nv(m,u,ao(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=_s(a,[s/2,1-s/2]);return J("ciAuc",{estimate:e.auc,confInt:[f[0],f[1]],confLevel:n,method:r,variance:a.length>1?at(a):NaN,warnings:o.list})}var lh=e=>typeof e=="object"&&e!==null&&!Array.isArray(e)&&"thresholds"in e&&"sensitivities"in e,ba=(e,t)=>e.length===t.length&&e.every((n,r)=>n===t[r]);function ov(e,t){let n=ga(e),r=ga(t);if(n?.labels==null||r?.labels==null||!ba(e.levels,t.levels))return;let o=c=>c.kept.map(a=>c.labels[a]);if(ba(o(n),o(r))&&ba(n.isCase,r.isCase))return[e,t];if(!ba(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)=>ro(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 _c(e,t){return t==="two.sided"?2*ae(-Math.abs(e)):t==="greater"?ae(e,{lowerTail:!1}):ae(e)}function sv(e,t,n,r){ct(n)&&B(n,ih,"rocTest"),B(r,ih,"rocTest");let o,s=new At,i,c;if(lh(e)){if(!lh(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 w=Array.from(e),y=Array.from(t),x=Array.from(n);if(w.length!==y.length||w.length!==x.length)throw new TypeError("the response and the predictors must have the same length");let M=w.map((A,k)=>k).filter(A=>{let k=w[A];return k!=null&&!(typeof k=="number"&&Number.isNaN(k))&&!Number.isNaN(y[A])&&!Number.isNaN(x[A])}),R=M.map(A=>w[A]),T={levels:o.levels,direction:o.direction};i=ro(R,M.map(A=>y[A]),T),c=ro(R,M.map(A=>x[A]),T);for(let A of i.warnings)s.add(A)}let a=Je(o.alternative),l=ke(o.method,["delong","bootstrap"]),f=o.confLevel??.95;ln(f),Lc(i)&&Lc(c)&&s.add("roc.test() of two ROC curves with AUC == 1 has always p.value = 1 and can be misleading.");let m=ov(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=wa(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."),io(i,"DeLong's test"),io(c,"DeLong's test"),u){let C=va(i),_=va(c),I=C.k10.map((O,q)=>O-_.k10[q]),F=C.k01.map((O,q)=>O-_.k01[q]),L=Math.sqrt(Fc(I,F,i.controls.length,i.cases.length)),D=p===0&&L===0?0:p/L,S=L===0?0:fh(f)*L;return J("rocTest",{method:"DeLong's test for two correlated ROC curves",statistic:D,statisticName:"Z",pValue:_c(D,a),confInt:[p-S,p+S],confLevel:f,...h,warnings:s.list})}let w=ch(i),y=ch(c),x=i.controls.length+i.cases.length,M=c.controls.length+c.cases.length,R=w+y,T,A,k;return R===0?(T=p===0?0:p>0?1/0:-1/0,A=NaN,k=_c(T,a)):(T=p/Math.sqrt(R),A=R*R/(w*w/(x-1)+y*y/(M-1)),k=a==="two.sided"?2*gt(-Math.abs(T),A,!0,!1):a==="greater"?gt(T,A,!1,!1):gt(T,A,!0,!1)),J("rocTest",{method:"DeLong's test for two ROC curves",statistic:T,statisticName:"D",parameter:A,parameterName:"df",pValue:k,...h,warnings:s.list})}let g=o.bootN??2e3;mh(g);let d=o.bootStratified??!0,v=[],N=0;if(d){io(i,"a stratified bootstrap"),io(c,"a stratified bootstrap");for(let w=0;w<g;w++)if(u){let y=ao(i.controls.length),x=ao(i.cases.length);v.push(wa(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=co(i.controls),x=co(i.cases),M=co(c.controls),R=co(c.cases);v.push(wa(y,x,i.direction,M,R,c.direction))}}else{let w=Cc(i),y=Cc(c);for(let x=0;x<g;x++){let M,R;if(u){let T=ao(w.values.length);M=rs(w.values,w.isCase,T),R=rs(y.values,y.isCase,T)}else M=rs(w.values,w.isCase,ao(w.values.length)),R=rs(y.values,y.isCase,ao(y.values.length));M===void 0||R===void 0?N++:v.push(wa(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.`),v.length<2)throw new Error("fewer than 2 bootstrap replicates had both controls and cases");let b=Math.sqrt(at(v)),E=p===0&&b===0?0:p/b;return J("rocTest",{method:u?"Bootstrap test for two correlated ROC curves":"Bootstrap test for two ROC curves",statistic:E,statisticName:"D",parameter:g,parameterName:"boot.n",pValue:_c(E,a),bootStratified:d,...h,warnings:s.list})}var av=["method","confLevel","prevalence"];function iv(e,t={}){B(t,av,"diagnosticTests");let n=ke(t.method,["exact","wilson","jeffreys"]),r=t.confLevel??.95;if(_t(r),!Array.isArray(e)||e.length!==2||e.some(w=>!(Array.isArray(w)||ArrayBuffer.isView(w))||w.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(w=>typeof w!="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=(w,y)=>{let x=Ze(()=>bi(w,y,{method:n==="exact"?"clopper-pearson":n,confLevel:r}));return{estimate:x.estimate,confInt:x.confInt}},g=1-r,d=g/2<1e-8?ie(g/2,{lowerTail:!1}):ie(1-g/2),v=(w,y)=>{let x=d*Math.sqrt(y);return{estimate:w,confInt:[Math.exp(Math.log(w)-x),Math.exp(Math.log(w)+x)]}},N=s/l,b=a/f,E={sensitivity:p(s,l),specificity:p(a,f),ppv:p(s,m),npv:p(a,u),lrPos:v(i===0?N/0:so(BigInt(s)*BigInt(f),BigInt(l)*BigInt(i)),c/(s*l)+a/(i*f)),lrNeg:v(a===0?c/l/b:so(BigInt(c)*BigInt(f),BigInt(l)*BigInt(a)),s/(c*l)+i/(a*f)),accuracy:p(s+a,h),diagnosticOR:v(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 w=t.prevalence;if(!(typeof w=="number"&&w>0&&w<1))throw new RangeError("'prevalence' must be a number in (0, 1)");let y=w<2**-500?2**600:1,x=N*(w*y),M=i/f*((1-w)*y),R=b*(1-w),T=c/l*w;E.ppvAtPrevalence=x/(x+M),E.npvAtPrevalence=R/(R+T)}return J("diagnosticTests",E)}var cv=["two","mode","confLevel","ciMethod"],lv=["package","exact"],In=(e,t)=>t===0?0:e*t;function fv(e,t,n={}){if(B(n,cv,"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;ln(s);let i=ke(n.ciMethod,lv),c=[],a=[];for(let L=0;L<e.length;L++){let D=e[L],S=t[L];if(typeof D!="number"||typeof S!="number")throw new TypeError("'x' and 'y' must be numeric");if(!(Number.isNaN(D)||Number.isNaN(S))){if(!Number.isFinite(D)||!Number.isFinite(S))throw new RangeError("'x' and 'y' must not contain infinite values");c.push(D),a.push(S)}}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((L,D)=>{let S=(L+a[D])/2;return Number.isFinite(S)?S:L/2+a[D]/2}),h=f.map((L,D)=>L-m[D]),p=!h.every(Number.isFinite),{groups:g,k:d}=Ie([p?f.map((L,D)=>L/2-m[D]/2):h]),v=g[0],N=d+(p?1:0),b=it(v),E=Zn(v),w=r*E,y=b-w,x=b+w,M=l-1,R=(1-s)/2,T=wt(R,M,!0,!1),A=R<1e-8?wt(R,M,!1,!1):wt((s+1)/2,M,!0,!1),k=[b+In(T,E)/Math.sqrt(l),b+In(A,E)/Math.sqrt(l)],C,_;if(i==="package"){let L=3*E*E/l,D=L>=2**-1022||L===0?Math.sqrt(L):E*Math.sqrt(3/l);C=[y+In(T,D),y+In(A,D)],_=[x+In(T,D),x+In(A,D)]}else{let L=r*Math.sqrt(l),D=Fa(R,M,L,!0,!1)/Math.sqrt(l),S=Fa(R,M,L,!1,!1)/Math.sqrt(l);C=[b-In(S,E),b-In(D,E)],_=[b+In(D,E),b+In(S,E)]}let I=L=>$(L,N),F=L=>[I(L[0]),I(L[1])];return J("blandAltman",{means:u,diffs:h,n:l,meanDiff:I(b),lowerLimit:I(y),upperLimit:I(x),criticalDiff:I(w),meanDiffConfInt:F(k),lowerLimitConfInt:F(C),upperLimitConfInt:F(_),confLevel:s,two:r,mode:o,ciMethod:i,warnings:[]})}var mv=new Set(["methodReg","methodCi","methodBootstrapCi","errorRatio","confLevel","slopeMeasure","nsamples","nnested","rngSeed","iterMax","threshold","naRm"]),uv=["PaBa","Deming","WDeming"],hv=["analytical","jackknife","bootstrap","nestedbootstrap"],pv=["quantile","Student","BCa","tBoot"],uh=5793,dv=2**27;function Pc(e,t){let n=e-t;return Math.abs(n)<1e-12*(Math.abs(e)+Math.abs(t))/2?0:n}function gv(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=De([e[c],0],n),l=De([t[c],0],r);o=pe(o,Ce(a,a)),s=pe(s,Ce(l,l)),i=pe(i,Ce(a,l))}return{u:o,q:s,p:i,mx:n,my:r}}function gh(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 Vc(e,t,n){let r=e.length,{u:o,q:s,p:i,mx:c,my:a}=gv(e,t),l=gh(o[0],s[0],i[0],n),f=De(a,Ce([l,0],c))[0],m=Ce(o,s),u=i[0]*i[0]/(o[0]*s[0]),h=De(m,Ce(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)),g=[0,0];for(let v of e)g=pe(g,Ce([v,0],[v,0]));let d=Math.sqrt(p*p*ft(g,r)[0]);return{b0:f,b1:l,se0:d,se1:p,xmean:c[0]}}function hh(e,t,n,r,o,s,i,c,a){let l=e.length,f=Vc(e,t,n),m=f.b0,u=f.b1,h=NaN,p=new Array(l),g=s;for(let w=0;w<s;w++){let y=1+n*u*u,x=1/0;for(let S=0;S<l;S++){let O=t[S]-(m+u*e[S]),q=e[S]+n*u*O/y,V=t[S]-O/y;p[S]=r>1?q/r+V:q+r*V,x=Math.min(x,Math.abs(p[S]))}let M=[0,0],R=[0,0],T=[0,0];for(let S=0;S<l;S++){let O=x/p[S],q=[O*O,0];M=pe(M,q),R=pe(R,Ce(q,[e[S],0])),T=pe(T,Ce(q,[t[S],0]))}let A=ft(R,M[0]+M[1]),k=ft(T,M[0]+M[1]),C=[0,0],_=[0,0],I=[0,0];for(let S=0;S<l;S++){let O=ft(De([e[S],0],A),p[S]),q=ft(De([t[S],0],k),p[S]);C=pe(C,Ce(O,O)),_=pe(_,Ce(q,q)),I=pe(I,Ce(O,q))}let F=gh(C[0],_[0],I[0],n),L=De(k,Ce([F,0],A))[0];h=A[0];let D=$(Math.abs(u-F),a-c)<i&&$(Math.abs(m-L),a)<i;if(m=L,u=F,D){g=w+1;break}}let[d,v]=Oc(o),[N,b]=Oc(1+o),E=p.map(w=>{if(w===0)return 1/0;let[y,x]=Oc(Math.abs(w));return r>1?Ve((N/(d*y))**2,2*(b-v-x-a)):Ve((N/y)**2,2*(b-x-c))});return{b0:m,b1:u,xmean:h,weights:E,converged:g<s}}function Oc(e){let t=Math.floor(Math.log2(e));return[Ve(e,-t),t]}var mo=e=>Math.atan(e),fo=e=>Number.isFinite(e)?e:Math.tan(mo(e));function wv(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=Pc(e[u],e[m]),p=Pc(t[u],t[m]),g;if(h!==0)g=p/h;else if(p!==0)g=o?1/0:-1/0;else continue;s[i++]=g;let d=mo(g);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 bv(e,t,n){let r=e.length;if(t%2!==0){let c=(t+1)/2;return c>=1&&c<=r?fo(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?(fo(s)+fo(i))/2:Math.tan((mo(s)+mo(i))/2)}function vv(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)?vv(e.map((r,o)=>Pc(t[o],n*r))):-n}function ph(e,t,n){let r=wv(e,t),o=bv(r.sorted,r.sorted.length+r.offset,n);return{b0:Number.isNaN(o)?NaN:$c(e,t,o),b1:o,slopes:r}}function Nv(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,g)=>{let d=Math.trunc((p+1)/2);if(g?d<2*a+1:d>l)return NaN;if(p%2===0){if(d+1>l)return NaN;let v=i[d-1],N=i[d];return o?(fo(v)+fo(N))/2:Math.tan((mo(v)+mo(N))/2)}return fo(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 yv(e,t,n,r){let o=e.length,s=e.map((p,g)=>t[g]-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,g)=>(t[g]+p/r-n)/Math.sqrt(1+1/(r*r))),m=Array.from({length:o},(p,g)=>g).sort((p,g)=>f[p]-f[g]||p-g),u=0,h=0;for(let p of m){let g=s[p];u+=g>0?a:g<0?l:0,h=Math.max(h,Math.abs(u))}return{nPos:i,nNeg:c,maxCusum:h}}var Na=e=>e.every(t=>t===e[0]);function Dc(e,t){if(!(typeof e=="number"&&Number.isInteger(e)&&e>=1))throw new RangeError(`'${t}' must be a positive integer`);return e}var lo=(e,t)=>_s(e,t),dh=e=>Zn(e.filter(t=>!Number.isNaN(t)));function Mv(e,t,n={}){B(n,mv,"mcreg");let r=ke(n.methodReg,uv),o=ke(n.methodCi??(r==="WDeming"?"jackknife":"analytical"),hv),s=o==="bootstrap"||o==="nestedbootstrap",i=ke(n.methodBootstrapCi,pv),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=ke(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=Dc(n.nsamples??999,"nsamples"),h=Dc(n.nnested??25,"nnested"),p=Dc(n.iterMax??30,"iterMax"),g=n.threshold??1e-6;if(!(typeof g=="number"&&g>=0&&g<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=[],v=[];for(let Z=0;Z<e.length;Z++){let P=e[Z],Y=t[Z];if(typeof P!="number"||typeof Y!="number")throw new TypeError("'x' and 'y' must be numeric");if(Number.isNaN(P)||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(P)||!Number.isFinite(Y))throw new RangeError("'x' and 'y' must not contain infinite values");d.push(P),v.push(Y)}let N=d.length;if(N<=2)throw new RangeError("mcreg needs at least 3 complete pairs");if(Na(d)||Na(v))throw new RangeError("'x' and 'y' must not be constant");if(r==="PaBa"){if(d.some(P=>P<0)||v.some(P=>P<0))throw new RangeError("Passing-Bablok regression needs nonnegative values");if(N>uh)throw new RangeError(`Passing-Bablok regression is limited to ${uh} 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)>dv)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(Z=>Z<=0)||v.some(Z=>Z<=0)))throw new RangeError("weighted Deming regression needs positive values");let b=Ie([d,v]),[E,w]=f?[Ie([d]).k,Ie([v]).k]:[b.k,b.k],y=f?d.map(Z=>$(Z,-E)):b.groups[0],x=f?v.map(Z=>$(Z,-w)):b.groups[1],M=Math.min(Math.max(Ve(m,2*(w-E)),Number.MIN_VALUE),2**1e3),R=Ve(m,w-E),T=new Set,A=`no konvergence after ${p} iterations`,k=(Z,P)=>{if(r==="Deming")return Vc(Z,P,M);if(r==="WDeming"){let Y=hh(Z,P,M,R,m,p,g,E,w);return Y.converged||T.add(A),Y}return ph(Z,P,l)},C,_=NaN,I,F,L;if(r==="Deming"){let Z=Vc(y,x,M);C=Z,_=$(Z.xmean,E),F=[Z.se0,Z.se1]}else if(r==="WDeming"){let Z=hh(y,x,M,R,m,p,g,E,w);Z.converged||T.add(A),C=Z,_=$(Z.xmean,E),I=Z.weights}else{let Z=ph(y,x,l);C=Z,L=Z.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 D=1-c,S=wt(D/2,N-2,!1,!1),O=ie(D/2,{lowerTail:!1}),q=[C.b0,C.b1],V=[NaN,NaN],W=[[NaN,NaN],[NaN,NaN]],Q,re,j,ee=()=>{let Z=[],P=[];for(let Y=0;Y<N;Y++){let z=k(y.filter((H,K)=>K!==Y),x.filter((H,K)=>K!==Y));Z.push(z.b0),P.push(z.b1)}Q=[Z,P],re=[(N-1)*Zn(Z)/Math.sqrt(N),(N-1)*Zn(P)/Math.sqrt(N)]};if(o==="analytical")if(r==="Deming")for(let Z of[0,1])V[Z]=F[Z],W[Z]=[q[Z]-S*V[Z],q[Z]+S*V[Z]];else{let Z=Nv(y,x,L,O,l);Z.defined||T.add("the Passing-Bablok interval is not defined for so few points: its limits are -Inf and Inf"),W[0]=Z.intercept,W[1]=Z.slope}else if(o==="jackknife"){ee();for(let Z of[0,1])V[Z]=re[Z],W[Z]=[q[Z]-S*V[Z],q[Z]+S*V[Z]]}else{if(i==="BCa"&&(ee(),Q.some(we=>we.some(Number.isNaN))))throw new RangeError("Too many identical data points!");let Z=r==="Deming"?F:r==="WDeming"&&re!==void 0?re:[NaN,NaN],P=o==="nestedbootstrap",Y=[],z=[],H=[],K=[],te=0,se=()=>Array.from({length:N},()=>Ro(N)),Me=we=>{let Oe=we.map(_e=>y[_e]),xe=we.map(_e=>x[_e]);if(Na(Oe)||Na(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 k(Oe,xe)},Ne=()=>{for(let we=0;we<u;we++){let Oe=se(),xe=Me(Oe),_e;if(P){let Fe=[],We=[];for(let ze=0;ze<h;ze++){let tt=Me(se().map(Ue=>Oe[Ue]));Fe.push(tt.b0),We.push(tt.b1)}_e=[dh(Fe),dh(We)]}else r==="Deming"?_e=[xe.se0,xe.se1]:_e=Z;if(Number.isNaN(xe.b0)||Number.isNaN(xe.b1)){te++;continue}Y.push(xe.b0),z.push(xe.b1),H.push(_e[0]),K.push(_e[1])}};if(n.rngSeed!==void 0){let we=El()?Tl():void 0;try{Rl({kind:"Mersenne-Twister"}),xl(n.rngSeed),Ne()}finally{we!==void 0&&Al(we)}}else Ne();if(te>.05*u)throw new RangeError("There were too many resamples with undetermined regression coefficients!");let de=Y.length,ce=[Y,z],qe=[H,K];for(let we of[0,1]){let Oe=ce[we],xe=q[we];if(i==="quantile")W[we]=[lo(Oe,D/2),lo(Oe,1-D/2)];else if(i==="Student")V[we]=Zn(Oe),W[we]=[xe-S*V[we],xe+S*V[we]];else if(i==="BCa"){let _e=Oe.filter(fe=>fe<xe).length;Oe.every(fe=>fe===xe)?_e=de/2:_e===0?_e=.5:_e===de&&(_e=de-.5);let Fe=ie(_e/de),We=Q[we],ze=We.reduce((fe,nt)=>fe+nt,0)/We.length,tt=0,Ue=0;for(let fe of We){let nt=ze-fe;tt+=nt*nt,Ue+=nt*nt*nt}let Xe=tt===0?0:Ue/(6*tt**1.5),ge=-O,oe=O,ve=ae(Fe-(ge-Fe)/(1+Xe*(ge-Fe))),be=ae(Fe-(oe-Fe)/(1+Xe*(oe-Fe)));W[we]=[lo(Oe,be),lo(Oe,ve)]}else{let _e=Oe.map((We,ze)=>(We-xe)/qe[we][ze]),Fe=Number.isNaN(Z[we])?Zn(Oe):Z[we];V[we]=Fe,W[we]=[xe-lo(_e,1-D/2)*Fe,xe-lo(_e,D/2)*Fe]}}j={intercept:Y.map(we=>$(we,w)),slope:z.map(we=>$(we,w-E)),sigmaIntercept:H.map(we=>$(we,w)),sigmaSlope:K.map(we=>$(we,w-E)),globalSigma:[$(Z[0],w),$(Z[1],w-E)],nsamples:de}}let U=Z=>{let P=Y=>$(Y,Z===0?w:w-E);return{estimate:P(q[Z]),se:P(V[Z]),confInt:[P(W[Z][0]),P(W[Z][1])]}},ue={methodReg:r,methodCi:o,...s?{methodBootstrapCi:i}:{},confLevel:c,errorRatio:m,n:N,intercept:U(0),slope:U(1),xmean:_,...I===void 0?{}:{weights:I},...Q===void 0?{}:{jackknife:{intercept:Q[0].map(Z=>$(Z,w)),slope:Q[1].map(Z=>$(Z,w-E))}},...j===void 0?{}:{bootstrap:j},...r==="PaBa"?{cusum:yv(y,x,C.b0,C.b1)}:{},warnings:[...T]};return J("mcreg",ue)}var Ev=["alpha","confLevel","lmer"],xv={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 ya(e,t){let n=e-t,r=n-e;return[n,e-(n-r)+(-t-r)]}function Rv(e){let t=e[0];return e.every(n=>n.every((r,o)=>{let[s,i]=ya(r,n[0]),[c,a]=ya(t[o],t[0]);return s===c&&i===a}))}var wh=(e,t)=>e.every((n,r)=>n===t[r]);function Dr(e){let t=[0,0];for(let n of e)t=pe(t,n);return ft(t,e.length)}var uo=(e,t)=>({v:ft(e.v,t),e:e.e});function ho(...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 bh(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:pe(r(e),r(t)),e:n}}var vh=(e,t)=>Ve(e.v[0]/t.v[0],e.e-t.e),Tv=(e,t)=>{let[n,r]=ho(e,t);return n.at<r.at},Av=(e,t)=>{let[n,r]=ho(e,t);return n.at<=r.at};function Nh(e,t,n){let r=[0,0];for(let o of e){let s=De(o,t);r=pe(r,Ce(s,s))}return Ce(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(ne=>Array.isArray(ne)))throw new TypeError("'x' must be a matrix (an array of rows)");let s=e[0].length;if(e.some(ne=>ne.length!==s))throw new TypeError("'x' must be a matrix (rows of equal length)");if(e.some(ne=>ne.some(ye=>typeof ye!="number")))throw new TypeError("'x' must contain numbers");if(e.some(ne=>ne.some(Number.isNaN)))throw new RangeError("'x' contains missing ratings (only complete data are supported)");if(e.some(ne=>ne.some(ye=>!Number.isFinite(ye))))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}=Ie(e);if(c.every(ne=>wh(ne,c[0])))throw new RangeError(c[0].every(ne=>ne===c[0][0])?"all ratings are equal":"every subject has the same ratings: the coefficients are undefined");let a=Array.from({length:s},(ne,ye)=>c.map(me=>me[ye])),l=0;for(let ne of e)for(let ye of ne)l=Math.max(l,Math.abs(ye));let f=l>2**1020?3:0,m=f>0?e.map(ne=>ne.map(ye=>ye/8)):e,u=m.map(ne=>ne.map((ye,me)=>ya(ye,m[0][me]))),h=m.map(ne=>ne.map(ye=>ya(ye,ne[0]))),p=h.map(ne=>ne.map((ye,me)=>De(ye,h[0][me]))),g=ne=>{let ye=0;for(let me of ne)for(let Se of me)ye=Math.max(ye,Math.abs(Se[0]));return ye===0?0:Math.floor(Math.log2(ye))},d=(ne,ye)=>ne.map(me=>me.map(Se=>[$(Se[0],-ye),$(Se[1],-ye)])),[v,N,b]=[g(u),g(h),g(p)],[E,w,y]=[d(u,v),d(h,N),d(p,b)],x=ne=>Array.from({length:s},(ye,me)=>ne.map(Se=>Se[me])),M=Nh(E.map(Dr),Dr(E.flat()),s),R=a.every(ne=>wh(ne,a[0]))?[0,0]:Nh(x(w).map(Dr),Dr(w.flat()),i),T=[0,0];if(!Rv(c)){let ne=y.map(Dr),ye=x(y).map(Dr),me=Dr(y.flat());y.forEach((Se,Ke)=>Se.forEach(($e,st)=>{let mt=pe(De(De($e,ne[Ke]),ye[st]),me);T=pe(T,Ce(mt,mt))}))}let A=(ne,ye)=>({v:ne,e:2*(ye+f)}),k=A(M,v),C=A(R,N),_=A(T,b),I=i-1,F=s-1,L=(i-1)*(s-1),D=uo(k,I),S=uo(C,F),O=uo(_,L),q=uo(bh(C,_),F+L),V=[];if(n){let ne={B:{ss:k,df:I,ms:D,pooled:!1},J:{ss:C,df:F,ms:S,pooled:!1}},ye=_,me=L;for(let Ke of Av(D,S)?["B","J"]:["J","B"]){let $e=ne[Ke];Tv($e.ms,uo(ye,me))&&(ye=bh(ye,$e.ss),me+=$e.df,$e.pooled=!0)}let Se=uo(ye,me);if(ne.B.pooled&&(D=Se),ne.J.pooled&&(S=Se),O=Se,ne.B.pooled){let[Ke,$e]=ho(O,S);q={v:[Ke.at+($e.at-Ke.at)/i,0],e:Ke.E}}}else T[0]===0&&V.push("essentially perfect fit for \u20181\u2019: summary may be unreliable");let W=(ne,ye)=>r/2<1e-8?hn(r/2,ne,ye,!1,!1):hn(1-r/2,ne,ye,!0,!1),Q=(ne,ye,me)=>ht(ne,ye,me,!1,!1),re=ne=>ne===1/0?1:(ne-1)/(ne+s-1),j=ne=>1-1/ne,ee=i*(s-1),U=vh(D,q),ue=vh(D,O),[Z,P]=[U/W(I,ee),U*W(ee,I)],[Y,z]=[ue/W(I,L),ue*W(L,I)],[H,K]=ho(D,q).map(ne=>ne.at),[te,se]=ho(D,O).map(ne=>ne.at),[Me,Ne,de]=ho(D,S,O).map(ne=>ne.at),ce=(H-K)/(H+(s-1)*K),qe=(Me-de)/(Me+(s-1)*de+s*(Ne-de)/i),we=(te-se)/(te+(s-1)*se),Oe=s*qe*Ne,xe=(i*(1+(s-1)*qe)-s*qe)*de,_e=Math.max(Math.abs(Oe),Math.abs(xe)),Fe=_e>0&&Number.isFinite(_e)?Math.floor(Math.log2(_e)):0,We=$(Oe,-Fe),ze=$(xe,-Fe),tt=(i-1)*We*We+ze*ze,Ue=tt===0?s-1:(s-1)*(i-1)*(We+ze)**2/tt,Xe=W(I,Ue),ge=W(Ue,I),oe=s*Ne+(s*i-s-i)*de,ve=(ne,ye)=>ne>.5?1-(i*de+oe)/ye:ne,be=ve(i*(Me/Xe-de)/(oe+i*Me/Xe),oe+i*Me/Xe),fe=ge>1?ve(i*(Me-de/ge)/(oe/ge+i*Me),oe+i*ge*Me):ve(i*(ge*Me-de)/(oe+i*ge*Me),oe+i*ge*Me),nt=ne=>ne*s/(1+ne*(s-1)),X=Q(U,I,ee),he=Q(ue,I,L),Te=(ne,ye,me,Se,Ke,$e,st)=>({type:ne,name:xv[ne],ICC:ye,F:me,df1:I,df2:Se,pValue:Ke,lower:$e,upper:st}),Ee=[Te("ICC1",ce,U,ee,X,re(Z),re(P)),Te("ICC2",qe,ue,L,he,be,fe),Te("ICC3",we,ue,L,he,re(Y),re(z)),Te("ICC1k",(H-K)/H,U,ee,X,j(Z),j(P)),Te("ICC2k",(Me-de)/(Me+(Ne-de)/i),ue,L,he,nt(be),nt(fe)),Te("ICC3k",(te-se)/te,ue,L,he,j(Y),j(z))],Ae=ne=>Ve(ne.v[0],ne.e);return J("ICC",{results:Ee,MSB:Ae(D),MSJ:Ae(S),MSE:Ae(O),MSW:Ae(q),n:i,k:s,confLevel:o,lmer:n,warnings:V})}function Ma(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(!Hl(Pl(e)))throw new RangeError("'covmat' must be symmetric")}var kv=["covmat","nObs","rotation","scores","control"],qv=["lower","start"];function kt(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 zc=(e,t)=>e[0].map((n,r)=>kt(e.map(o=>t(o[r]))));function Eh(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 yh(e,t,n){let r=Ut(Eh(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 Mh(e,t){let n=zc(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=zc(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 _n=(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})),po=e=>e[0].map((t,n)=>e.map(r=>r[n]));function Ea(e){let t=e[0].length,n=e.map(m=>Math.sqrt(kt(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=po(o),a=0;for(let m=0;m<1e3;m++){let u=_n(o,i),h=u[0].map((N,b)=>{let E=0;for(let w=0;w<s;w++)E+=u[w][b]*u[w][b];return E}),p=u.map(N=>N.map((b,E)=>b**3-b*h[E]/s)),g=_n(c,p),d=qs(g);i=_n(d.u,po(d.v));let v=a;if(a=kt(d.d),a<v*(1+1e-5))break}let l=_n(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 Iv(e){let t=Ea(e),n=t.loadings,r=n.map(c=>c.map(a=>a*Math.abs(a)**3)),o=As(Lt(n),r),s=rt(_n(po(o),o)).map((c,a)=>c[a]),i=o.map(c=>c.map((a,l)=>a*Math.sqrt(s[l])));return{loadings:_n(n,i),rotmat:_n(t.rotmat,i)}}function _v(e,t,n={}){B(n,kv,"factanal"),B(Kt(n,"control"),qv,"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");Ma(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=Is(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()??rt(f).map((x,M)=>(1-c/(2*i))/x[M]),h=x=>{let M=Ut(Eh(f,x),{symmetric:!0,onlyValues:!0}).values.slice(c);return-(kt(M.map(R=>Math.log(R)-R))-c+i)},p=x=>{let M=yh(f,x,c);return x.map((R,T)=>{let A=0;for(let k=0;k<c;k++)A+=M[T][k]*M[T][k];return(A+R-f[T][T])/(R*R)})},g=Ze(()=>_o(u,h,{gr:p,method:"L-BFGS-B",lower:m,upper:1,control:{fnscale:1,parscale:new Array(i).fill(.01)}}));if(g.convergence!==0)throw new Error("unable to optimize from this starting value");let d=g.par,v=Mh(yh(f,d,c)).L,N,b=n.rotation??"varimax";if(b!=="varimax"&&b!=="promax"&&b!=="none")throw new RangeError(`unknown rotation '${String(b)}'`);if(b!=="none"&&c>=2){let x=b==="varimax"?Ea(v):Iv(v),M=Mh(x.loadings,x.rotmat);v=M.L,N=M.rot}let E=NaN,w=NaN;!Number.isNaN(s)&&a>0&&(E=(s-1-(2*i+5)/6-2*c/3)*g.value,w=Rt(E,a,{lowerTail:!1}));let y={uniquenesses:d,loadings:v,STATISTIC:E,dof:a,PVAL:w,converged:!0,objective:g.value,counts:g.counts,warnings:[]};return r!=="none"&&(y.scores=Lv(e,f,d,v,r)),N!==void 0&&(y.rotmat=N),y}function Lv(e,t,n,r,o){let s=e.length,i=e[0].map((m,u)=>kt(e.map(h=>h[u]))/s),c=e.map(m=>m.map((u,h)=>u-i[h])),a=zc(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 _n(l,rt(t,r));let f=r.map((m,u)=>m.map(h=>h/n[u]));return _n(l,po(rt(_n(po(r),f),po(f))))}var Cv=["nfactors","covmat"],go=2**-52,Fv=`
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
- `,ho=(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})),ts=e=>e[0].map((t,n)=>e.map(r=>r[n])),ns=(e,t=n=>n)=>e[0].map((n,r)=>kt(e.map(o=>t(o[r])))),Eh=(e,t=n=>n)=>e.map(n=>kt(n.map(t))),po=(e,t=n=>n)=>kt(e[0].flatMap((n,r)=>e.map(o=>t(o[r])))),ya=e=>e<0?-1:1;function Wc(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 xh(e,t){let n=Wc(e),{d:r,u:o,v:s}=Ss(n),i=r.map((a,l)=>l).filter(a=>r[a]>Math.sqrt(uo)*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 zc(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 Tv=e=>ho(e,ts(e));function Mh(e,t,n,r){let o=Wc(e),s=o.length,i=xh(o,r),c=d=>{let v=zc(o,d.map(E=>1-E),t,E=>E<uo?100*uo:E),N=Tv(v),b=[];for(let E=0;E<s;E++)for(let g=E+1;g<s;g++)b.push((o[g][E]-N[g][E])**2);return kt(b)},a=d=>{let v=zc(o,d.map((N,b)=>o[b][b]-N),t,N=>Math.max(N,0));return d.map((N,b)=>{let E=0;for(let g=0;g<t;g++)E+=v[b][g]*v[b][g];return E+N-o[b][b]})},l=d=>{let v=Bt(o.map((T,S)=>T.map((A,C)=>S===C?1-d[S]:A)),{symmetric:!0}),N=v.values,b=v.vectors,E=N.map(T=>T>=uo),g=N.map(T=>T<uo?100*uo:T),y=o.map((T,S)=>T.map((A,C)=>{if(S===C)return 0;let _=0;for(let q=0;q<t;q++)_+=g[q]*b[S][q]*b[C][q];return A-_})),x=ho(y,b),M=(T,S)=>{let A=0;for(let C=0;C<s;C++)A+=b[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 _=C*M(T,A);for(let q=0;q<s;q++)R[q]+=_*b[q][T]*b[q][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(()=>ko(i.map(d=>1-d),c,{gr:a,...m,control:{fnscale:1,parscale:u}}));h.par.some(d=>d===f)&&(h=ot(()=>ko(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=zc(o,p.map((d,v)=>o[v][v]-d),t,d=>Math.max(d,0));if(w=jc(w,t),w=w.map(d=>d.map(v=>v===0?1e-15:v)),Eh(w,d=>d*d).some(d=>d>1)&&pr(n,"An ultra-Heywood case was detected. Examine the results carefully"),t>1){w=jc(Na(w).loadings,t);let d=ns(w,N=>N*N),v=d.map((N,b)=>b).sort((N,b)=>d[b]-d[N]||N-b);w=w.map(N=>v.map(b=>N[b]))}else w=jc(w,t);return w}function jc(e,t){let n=ns(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 Sv(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:po(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 Av(e){let t=e[0].length,n=l=>{let f=nt(l),m=ho(e,ts(f)),{f:u,Gq:h}=Sv(m),p=ts(ho(ho(ts(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=ns(r.map((d,v)=>d.map((N,b)=>N*s.G[v][b]))),m=s.G.map((d,v)=>d.map((N,b)=>N-r[v][b]*f[b])),u=Math.sqrt(po(m,d=>d*d));if(c.push(s.f),u<1e-5){a=!0;break}if(i!==void 0){let d=i,v=r.map((b,E)=>b.map((g,y)=>g-d.T[E][y])),N=m.map((b,E)=>b.map((g,y)=>g-d.Gp[E][y]));po(N,b=>b*b)>0&&(o=po(v,b=>b*b)/Math.abs(po(v.map((b,E)=>b.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 v=r.map((b,E)=>b.map((g,y)=>g-o*m[E][y])),N=ns(v,b=>b*b).map(b=>1/Math.sqrt(b));if(w=v.map(b=>b.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:ho(ts(r),r),converged:a}}function kv(e,t={}){B(t,xv,"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=Wc(t.covmat);else{if(e===null)throw new TypeError("neither 'x' nor 'covmat' supplied");r=Ao(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=Mh(r,n,s,i),a,l,f;if(n===1)a=c.map(k=>k.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 k=Av(c);if(k.converged||pr(s,"oblimin did not converge in 2000 iterations"),a=k.loadings,l=k.phi,n===2){let V=l[0][1],K=Math.sqrt(Math.abs(V));f=[K,ya(V)*K],pr(i,Rv)}else f=Mh(l,1,s,i).map(V=>V[0])}let m=a.map(k=>{let V=0;for(let K=0;K<n;K++)V+=k[K]*f[K];return V}),u=Eh(c,k=>k*k),h=u.map((k,V)=>r[V][V]-k),p=a.map((k,V)=>k.map((K,X)=>{if(n===1)return 0;let re=r[V][V],z=re-(re-K*K)-(K*f[X])**2;return z<0&&(z=0),ya(K)*Math.sqrt(z)})),w=m.map((k,V)=>k*k/u[V]),d=m.map((k,V)=>{let K=[k,...p[V]];return kt(K.map(X=>X*X))**2/kt(K.map(X=>X**4))}),v=m.map(ya);v.some(k=>k<0)&&(r=r.map((k,V)=>k.map((K,X)=>v[V]*K*v[X])));let N=m.map((k,V)=>v[V]*k),b=p.map((k,V)=>k.map(K=>v[V]*K)),E=N.map((k,V)=>[k,...b[V],u[V],h[V],w[V],d[V]]),g=po(r),y=kt(r.map((k,V)=>k[V])),x=kt(N)**2,M=kt(h),R=x/g,T=(g-M)/g,S=x/(g-M),A=(g-y)/g*o/(o-1),C=(g+kt(xh(r,i))-y)/g,_=ns(E.map(k=>k.slice(0,n+1)),k=>k*k),q=_[0]/kt(_),F=b.map(k=>{let V=0;for(let K=1;K<n;K++)Math.abs(k[K])>Math.abs(k[V])&&(V=K);return V}),L=[],O=[];for(let k=0;k<n;k++){let V=F.map((z,J)=>J).filter(z=>F[z]===k),K=kt(V.flatMap(z=>V.map(J=>r[J][z]))),X=kt(V.map(z=>b[z][k])),re=kt(V.map(z=>N[z]));O.push(X*X),L.push([(X*X+re*re)/K,re*re/K,X*X/K])}let I=[[T,kt(N)**2/g,kt(O)/g],...L],D={omegaH:R,omegaLim:S,alpha:A,omegaTot:T,G6:C,ECV:q,sl:E,slNames:["g",...Array.from({length:n},(k,V)=>`F${V+1}*`),"h2","u2","p2","com"],orthog:c,oblique:a,gloading:f,key:v,omegaGroup:I,warnings:s,messages:i};return l!==void 0&&(D.phi=l),Z("omega",D)}var Rh=["ML","stdErr"],qv=["ML","stdErr"],rn=.9999;function Kc(e){let t=De(e),n=[],r=0;for(let o=0;o<e.length-1;o++)r+=e[o],n.push(ie(r/t));return n}var Uc=e=>[...new Set(e)].sort((t,n)=>t-n),Th=(e,t)=>e===1?`1 ${t} with zero marginal removed`:`${e} ${t}s with zero marginals removed`,Sh=e=>Array.isArray(e)||ArrayBuffer.isView(e),Ah=(e,t)=>e>0?se(e,{lowerTail:!1})-se(t,{lowerTail:!1}):se(t)-se(e);function Hc(e){let t=ke([e]).groups[0],n=it(t),r=t.map(s=>s-n),o=Math.sqrt(at(r));return r.map(s=>s/o)}function kh(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 _h(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 Bc={r:0,h:0,k:0,rr:0,rh:0,rk:0,hh:0,hk:0,kk:0};function Iv(e,t,n){if(e===-1/0||t===-1/0||e===1/0&&t===1/0)return Bc;if(e===1/0)return{...Bc,k:Xt(t),kk:-t*Xt(t)};if(t===1/0)return{...Bc,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 qh(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=>Iv(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=yo([t[h],n[p]],[t[h+1],n[p+1]],r),v=new Array(c).fill(0),N=Ma(c);for(let[b,E,g]of[[h+1,p+1,1],[h,p+1,-1],[h+1,p,-1],[h,p,1]]){let y=m[b][E],x=l(b),M=f(E);v[0]+=g*y.r,N[0][0]+=g*y.rr,x>=0&&(v[x]+=g*y.h,N[0][x]+=g*y.rh,N[x][0]+=g*y.rh,N[x][x]+=g*y.hh),M>=0&&(v[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)}_h(a,w,d,v,N),u.push(w*Math.log(d))}return a.value=De(u),a}function _v(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 v=n[p],N=(v-r*w)/a,b=Xt(N),E=(r*v-w)/l,g=1/a,y=v/l+3*r*(r*v-w)/(l*c),x=r/l;if(f[0]+=d*b*E,m[0][0]+=d*b*(y-N*E*E),o){let M=p+1;f[M]+=d*b*g;let R=d*b*(x-N*E*g);m[0][M]+=R,m[M][0]+=R,m[M][M]+=d*b*(-N*g*g)}};for(let p=0;p<e.length;p++){let w=e[p],d=t[p];f.fill(0);for(let b of m)b.fill(0);let v=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),_h(i,1,Ah(N,v),f,m),u.push(Math.log(Ah(N,v)),Xt(w,{log:!0}))}return i.value=De(u),i}function Lv(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 Lh=(e,t)=>e>=t-1e-12*Math.abs(t);function Ih(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=Lv(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(Lh(p.value,o.value)){let w=Nt(h.map((d,v)=>Math.abs(d-r[v])/Math.max(1,Math.abs(r[v]))));r=h,o=p,m=w>1e-12;break}f/=2}if(!m)break}return{theta:r,at:o,bounded:s}}function Ch(e,t){let n=Ih(e,[Math.max(-rn,Math.min(rn,t[0])),...t.slice(1)],!1);if(n.bounded||n.theta[0]===0)return n;let r=Ih(e,[Math.sign(n.theta[0])*rn,...n.theta.slice(1)],!0);return Lh(r.at.value,n.at.value)?r:n}function Fh(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 Oh=e=>`inadmissible correlation set to ${e>0?"0.9999":"-0.9999"}`;function Cv(e,t,n){ct(t)&&B(t,Rh,"polychor"),B(n,Rh,"polychor");let r,o;if(Sh(t)){let L=e,O=t;if(L.length!==O.length)throw new RangeError("'x' and 'y' must have the same length");let I=L.map((X,re)=>!Number.isNaN(X)&&!Number.isNaN(O[re])),D=Pt(L.filter((X,re)=>I[re])),k=Pt(O.filter((X,re)=>I[re])),V=new Map(Uc(D).map((X,re)=>[X,re])),K=new Map(Uc(k).map((X,re)=>[X,re]));r=Array.from({length:V.size},()=>new Array(K.size).fill(0)),D.forEach((X,re)=>r[V.get(X)][K.get(k[re])]++),o=n??{}}else{let L=e;if(!L.every(Sh))throw new TypeError("give a table (array of rows) or two vectors");let O=L[0]?.length??0;if(L.some(I=>I.length!==O))throw new TypeError("the table must have rows of equal length");if(r=L.map(I=>Array.from(I)),r.some(I=>I.some(D=>!(D>=0&&D<1/0))))throw new RangeError("the counts of the table must be nonnegative and finite");o=t??{}}let{ML:s=!1,stdErr:i=!1}=o,c=[],a=0;De(r.map(L=>De(L)))<2**1e3||({groups:r,k:a}=ke(r));let l=r.map(L=>De(L)),f=l.filter(L=>L===0).length;f>0&&(c.push(Th(f,"row")),r=r.filter((L,O)=>l[O]!==0));let m=(r[0]??[]).map((L,O)=>De(r.map(I=>I[O]))),u=m.filter(L=>L===0).length;u>0&&(c.push(Th(u,"column")),r=r.map(L=>L.filter((O,I)=>m[I]!==0)));let h=r.length,p=r[0]?.length??0;if(h<2&&c.push("the table has fewer than 2 rows"),p<2&&c.push("the table has fewer than 2 columns"),h<2||p<2)return Z("polychor",{rho:NaN,warnings:c});let w=[-1/0,...Kc(r.map(L=>De(L))),1/0],d=[-1/0,...Kc(r[0].map((L,O)=>De(r.map(I=>I[O])))),1/0],N=Cl(L=>{let O=Math.max(-rn,Math.min(rn,L)),I=[];for(let D=0;D<h;D++)for(let k=0;k<p;k++){if(r[D][k]===0)continue;let V=yo([w[D],d[k]],[w[D+1],d[k+1]],O);I.push(r[D][k]*Math.log(V))}return-De(I)},-rn,rn).optimum;if(!s&&!i)return Z("polychor",{rho:N,warnings:c});let b=L=>[-1/0,...L,1/0],E=w.slice(1,h),g=d.slice(1,p),y=L=>s?qh(r,b(L.slice(1,h)),b(L.slice(h)),L[0],!0):qh(r,w,d,L[0],!1),{theta:x,at:M,bounded:R}=Ch(y,s?[N,...E,...g]:[N]),T=x[0];if(R&&c.push(Oh(T)),!i)return Z("polychor",{rho:T,warnings:c});let S=s?b(x.slice(1,h)):w,A=s?b(x.slice(h)):d,C=De(r.map(L=>De(L))),_=[],q=[];for(let L=0;L<h;L++)for(let O=0;O<p;O++){let I=r[L][O],D=yo([S[L],A[O]],[S[L+1],A[O+1]],T);if(I===0)_.push(C*D);else if(a===0)_.push(I*Math.log1p(1e-6/I),I*kh(C*D/I-1));else{let k=$(I,a);q.push(k*Math.log1p(1e-6/k)),_.push(I*kh(C*D/I-1))}}let F=Fh(M.hessian,R);return Z("polychor",{rho:T,warnings:c,var:a===0?F:F.map(L=>L.map(O=>$(O,-a))),chisq:a===0?2*De(_):$(2*De(_),a)+2*De(q),df:h*p-h-p,n:$(C,a),rowCuts:S.slice(1,h),colCuts:A.slice(1,p)})}function Fv(e,t,n,r){let o=e.length,s=[ie(.25),ie(.5),ie(.75)],i=(h,p)=>p.filter(w=>w<h).length,c=Hc(e).map(h=>i(h,n)),a=Hc(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 v=yo([f[p],m[d]],[f[p+1],m[d+1]],r);u.push(w*Math.log((w+1e-6)/(v*o)))})),2*De(u)}function Ov(e,t,n={}){if(B(n,qv,"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=Uc(c);if(l.length<2)return Z("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=Kc(u),p=[],w=Math.sqrt((a-1)/a)*Math.sqrt(at(m))*Ol(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 Z("polyserial",{rho:w,warnings:p});let d=Hc(i),v=m.map(M=>M-1),N=M=>_v(d,v,r?M.slice(1):h,M[0],r),{theta:b,at:E,bounded:g}=Ch(N,r?[w,...h]:[w]);if(g&&p.push(Oh(b[0])),!o)return Z("polyserial",{rho:b[0],warnings:p});let y=r?b.slice(1):h,x=l.length;return Z("polyserial",{rho:b[0],warnings:p,var:Fh(E.hessian,g),chisq:Fv(m,d,y,r?b[0]:w),df:4*x-x-1,n:a,cuts:y})}var Dv=["group","strata","rho","cencode"],Pv=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 Yc(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 Vv(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 $v(e,t,n={}){B(n,Dv,"cuminc"),Vv(e,t,n);let{group:r,strata:o,rho:s=0,cencode:i=0}=n,c=[],a=new Tt;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=Yc(f);if(h.labels.length>=2&&!Number.isFinite(s))throw new RangeError("'rho' must be finite when Gray's test is requested");let p=Yc(m),w=Yc(u),d=u[u.length-1],v=w.labels.filter(g=>g!==d);if(v.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),b=[];for(let g of v){let y=vt(g);for(let x=0;x<h.labels.length;x++){let M=N.filter(I=>I.group===x),R=M.reduce((I,D)=>Math.max(I,D.time),-1/0),T=R>0&&R<1?2**Math.min(1023,Math.ceil(-Math.log2(R))):1,S=mr(M.map(I=>I.time*T),M.map(I=>I.status===d?0:1)),A=[0],C=[0],_=[0],q=[],F=1;for(let I=0;I<S.time.length;I++){let D=S.nEvent[I];if(D===0)continue;let k=S.time[I]/T,V=S.nRisk[I],K=M.filter(X=>X.time===k&&X.status===g).length;q.push({t:k,Y:V,d:D,dk:K,sb:F,sa:S.surv[I]}),F=S.surv[I]}let L=0,O=0;for(let I=0;I<q.length;I++){let D=q[I],k=L,V=O;L+=D.sb*D.dk/D.Y;let K=0;for(let X=0;X<=I;X++){let re=q[X],z=re.Y,J=re.d,Q=re.dk,Re=z===J?0:(J*(z-J)+2*Q*(J-Q))/((z-1)*(z-J)**2),te=z===1?Q:Q*(z-Q)/(z*z*(z-1)),P=z===J||z===1?0:Q*(z-Q)/(z*(z-J)*(z-1)),Y=0;for(let U=0;U<=X;U++){let W=q[U];Y+=W.sb*W.dk/W.Y}let j=L-Y;K+=j*j*Re+re.sb*re.sb*te-2*j*re.sb*P}O=Math.max(0,K),D.dk>0&&(A.push(D.t,D.t),C.push(k,L),_.push(V,O))}A.push(R),C.push(L),_.push(O),b.push({name:`${vt(h.labels[x])} ${y}`,group:vt(h.labels[x]),cause:y,time:A,est:C,var:_})}}let E=h.labels.length>=2?zv(N,h.labels,v,p.labels.length,s,d):void 0;return Z("cuminc",{curves:b,...E===void 0?{}:{tests:E},warnings:a.list})}function zv(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),v=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),b=new Array(a).fill(0),E=[];for(let g of v){let y=new Array(a).fill(0),x=new Array(a).fill(0),M=new Array(a).fill(0);for(let I of d)I.time>=g&&y[I.group]++;for(let I of d)I.time===g&&I.status!==s&&(I.status===c?x[I.group]++:M[I.group]++);let R=N.slice(),T=b.slice(),S=y.map((I,D)=>I===0?0:I/R[D]),A=y.map((I,D)=>I===0?0:I*(1-T[D])/R[D]),C=x.reduce((I,D)=>I+D,0),_=S.reduce((I,D)=>I+D,0),q=_===0?0:C/_,F=E.length===0?1:E[E.length-1].Gafter,L=F-q,O=A.reduce((I,D)=>I+D,0);for(let I=0;I<a;I++){let D=F**o;O>0&&(l[I]+=D*(x[I]-A[I]*C/O)),y[I]>0?(N[I]=N[I]*(1-(x[I]+M[I])/y[I]),b[I]+=R[I]*x[I]/y[I]):N[I]=0}E.push({time:g,Y:y,d1:x,d2:M,Sbefore:R,Safter:N.slice(),Fbefore:T,h:S,R:A,dF0:q,Gbefore:F,Gafter:L})}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,Q)=>J+Q,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),_=A*T.h[y]*((y===M?1:0)-T.h[M]/S),q=0,F=0;for(let J=R+1;J<E.length;J++){let Q=E[J],Re=Q.h.reduce((U,W)=>U+W,0);if(!(Re>0))continue;let te=Q.Gbefore**o,P=te*Q.h[g]*((g===M?1:0)-Q.h[M]/Re),Y=te*Q.h[y]*((y===M?1:0)-Q.h[M]/Re),j=Q.Gbefore===0?0:Q.dF0/Q.Gbefore;q+=P*j,F+=Y*j}let L=T.Safter[M]===0?0:T.Gafter/T.Safter[M],O=C+(1-L)*q,I=_+(1-L)*F,D=-L*q,k=-L*F,V=T.Y[M]===0?0:T.Sbefore[M]*T.d2[M]/T.Y[M],K=(O*I*T.dF0+D*k*V)/T.h[M],X=T.d1.reduce((J,Q)=>J+Q,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*I/(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)*(V/T.h[M])*D*k),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,v,N)=>d+v*w[N],0),h=xt(u,m,{lowerTail:!1})}catch{u=NaN,h=NaN}i.push({cause:vt(c),stat:u,df:m,pValue:h})}return i}function jv(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 Ln(e,t=e){return Array.from({length:e},()=>new Array(t).fill(0))}function Wv(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 Gc(e,t){return De(e.map((n,r)=>n*t[r]))}function Bv(e){return e.every(t=>t.every(Number.isFinite))}function Kv(e,t,n,r={}){B(r,Pv,"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 v=[];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");v.push(W)}let N=e.length-v.length;if(v.length===0)throw new RangeError("no complete observations");let b=v.map(W=>e[W]),E=tn([...v.map(W=>t[W]),c,i]),g=E.slice(0,v.length),y=E[E.length-2],x=E[E.length-1],M=v.map(W=>n===void 0?[]:Array.from(n[W])),R=v.map(W=>o===void 0?[]:Array.from(o[W])),T=Array.from(new Set(b.filter((W,ne)=>g[ne]===x))).sort((W,ne)=>W-ne);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)||!Bv(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,ne)=>b.map((fe,Te)=>[...M[Te],...R[Te].map((Ce,be)=>Ce*(S[ne][be]??0))])),C=b.reduce((W,ne)=>Math.max(W,Math.abs(ne)),0),_=C>0&&C<1?2**Math.min(1023,Math.ceil(-Math.log2(C))):1,q=mr(b.map(W=>W*_),g.map(W=>W===y?1:0)),F=W=>{let ne=W*_,fe=1;for(let Te=0;Te<q.time.length&&!(q.time[Te]>=ne);Te++)fe=q.surv[Te];return fe},L=T.map(W=>b.map((ne,fe)=>{if(ne>=W)return 1;if(g[fe]===y||g[fe]===x)return 0;let Te=F(ne);return Te===0?0:F(W)/Te}));for(let W=0;W<T.length;W++)for(let ne=0;ne<b.length;ne++)b[ne]===T[W]&&g[ne]===x&&(L[W][ne]=1);let O=W=>{let ne=Array.from({length:d},()=>[]),fe=Array.from({length:d},()=>Array.from({length:d},()=>[])),Te=[],Ce=Ln(T.length,d),be=[],me=[],qe=[];for(let Oe=0;Oe<T.length;Oe++){let Ke=T[Oe],ze=b.map((ce,Se)=>ce===Ke&&g[Se]===x?Se:-1).filter(ce=>ce>=0),et=ze.length,Ue=A[Oe],Xe=Ue.map(ce=>Gc(W,ce)),pe=L[Oe].slice();for(let ce of ze)pe[ce]=1;let oe=Math.max(...Xe.map((ce,Se)=>pe[Se]>0?ce:-1/0));if(oe===-1/0)throw new Error("empty Fine-Gray risk set");let we=oe<500&&oe>-500,ge=we?Xe.map((ce,Se)=>pe[Se]*Math.exp(ce)):Xe.map((ce,Se)=>pe[Se]*Math.exp(ce-oe)),le=De(ge),tt=Array.from({length:d},(ce,Se)=>De(ge.map((Be,$e)=>Be*Ue[$e][Se])));if(!(le>0))throw new Error("empty Fine-Gray risk set");let G=tt.map(ce=>ce/le),Me=Array.from({length:d},(ce,Se)=>{let Be=Ue[0][Se],$e=0,st=0;for(let mt of Ue)$e=Math.max($e,Math.abs(mt[Se]-Be)),st=Math.max(st,Math.abs(mt[Se]));return $e>0&&st/$e>1e6}).some(Boolean),Ne=Me?Array.from({length:d},(ce,Se)=>Ue.map(Be=>Be[Se]-Ue[0][Se])):[],Ee=Me?Ne.map(ce=>De(ce.map((Se,Be)=>ge[Be]*Se))/le):[],ee=Me?void 0:Array.from({length:d},(ce,Se)=>Array.from({length:d},(Be,$e)=>De(ge.map((st,mt)=>st*Ue[mt][Se]*Ue[mt][$e]))));me.push(G),qe.push(ge.map(ce=>ce/le));let ve=we?et/le:et*Math.exp(-oe)/le;be.push(ve),Te.push(De(ze.map(ce=>Gc(W,Ue[ce])))-et*(we?Math.log(le):oe+Math.log(le)));for(let ce=0;ce<d;ce++){let Be=De(ze.map($e=>Ue[$e][ce]))-et*G[ce];ne[ce].push(Be),Ce[Oe][ce]=Be}for(let ce=0;ce<d;ce++)for(let Se=0;Se<d;Se++){let Be=Me?De(ge.map(($e,st)=>$e*(Ne[ce][st]-Ee[ce])*(Ne[Se][st]-Ee[Se])))/le:ee[ce][Se]/le-G[ce]*G[Se];fe[ce][Se].push(et*Be)}}let de=Ln(b.length,d);for(let Oe=0;Oe<T.length;Oe++){let Ke=A[Oe],ze=qe[Oe],et=me[Oe],Ue=T[Oe];for(let Xe=0;Xe<b.length;Xe++){let pe=b[Xe]===Ue&&g[Xe]===x?1:0,oe=b.filter((ge,le)=>ge===Ue&&g[le]===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=ne.map(De),xe=fe.map(Oe=>Oe.map(De));return{loglik:De(Te),score:Fe,inf:xe,bfitj:be,res:Ce,eta:de,means:me,riskFactors:qe,zByTime:A}},I=f===void 0?new Array(d).fill(0):Array.from(f);if(!I.every(Number.isFinite))throw new RangeError("'init' must contain finite values");let D=I.slice(),k=O(D),V=!1,K=0,X=(W,ne)=>Math.max(...W.score.map((fe,Te)=>Math.abs(fe)*Math.max(Math.abs(ne[Te]),1)))<a*Math.max(Math.abs(W.loglik),1);for(;;){if(X(k,D)){V=!0;break}if(K>=l)break;let W=nt(k.inf,k.score,{tol:0});D=D.map((ne,fe)=>ne+W[fe]),K++,k=O(D)}let re=nt(k.inf),z=Ln(d);if(m){let W=Ln(d),ne=b.length,fe=Array.from(new Set(b)).sort((me,qe)=>me-qe),Te=fe.map(me=>{let qe=b.filter(Fe=>Fe>=me).length,de=b.filter((Fe,xe)=>Fe===me&&g[xe]===y).length;return qe===0?0:de/qe}),Ce=Ln(ne,d);for(let me=0;me<fe.length;me++){let qe=fe[me],de=new Array(d).fill(0);for(let xe=0;xe<ne;xe++)if(!(!(b[xe]<qe)||g[xe]===y||g[xe]===x))for(let Pe=0;Pe<T.length;Pe++){if(T[Pe]<qe)continue;let Oe=k.riskFactors[Pe][xe],Ke=b.filter((Ue,Xe)=>Ue===T[Pe]&&g[Xe]===x).length,ze=k.zByTime[Pe][xe],et=k.means[Pe];for(let Ue=0;Ue<d;Ue++)de[Ue]+=(ze[Ue]-et[Ue])*Oe*Ke}let Fe=b.filter(xe=>xe>=qe).length;if(Fe!==0)for(let xe=0;xe<ne;xe++){let Pe=b[xe]===qe&&g[xe]===y?1:0,Oe=b[xe]>=qe?Te[me]:0;for(let Ke=0;Ke<d;Ke++)Ce[xe][Ke]+=de[Ke]/Fe*(Pe-Oe)}}for(let me=0;me<ne;me++)Wv(W,k.eta[me].map((qe,de)=>qe+Ce[me][de]),k.eta[me].map((qe,de)=>qe+Ce[me][de]),1);z=Ln(d);let be=Dh(re,W);z=Dh(be,re)}else z=Ln(d).map(W=>W.map(()=>NaN));let J=m?z.map((W,ne)=>Math.sqrt(W[ne])):void 0,Q=J===void 0?void 0:D.map((W,ne)=>W/J[ne]),Re=Q===void 0?void 0:Q.map(W=>2*se(-Math.abs(W))),te=m?ie((1+u)/2):void 0,P=te===void 0?void 0:D.map((W,ne)=>Math.exp(W-te*J[ne])),Y=te===void 0?void 0:D.map((W,ne)=>Math.exp(W+te*J[ne])),j=m?D.map(Math.exp):void 0,U=h===void 0?[...Array.from({length:p},(W,ne)=>`cov1${ne+1}`),...Array.from({length:w},(W,ne)=>`cov2${ne+1}*tf${ne+1}`)]:Array.from(h);return Z("crr",{coefficients:D,names:U,var:z,...J===void 0?{}:{se:J,z:Q,pValue:Re,lower:P,upper:Y,expCoef:j},loglik:k.loglik,loglikNull:O(new Array(d).fill(0)).loglik,logtest:{statistic:2*(k.loglik-O(new Array(d).fill(0)).loglik),df:d},score:k.score,inf:m?k.inf:Ln(d).map(W=>W.map(()=>NaN)),invinf:m?re:Ln(d).map(W=>W.map(()=>NaN)),converged:V,iterations:K,n:v.length,nMissing:N,uftime:T,bfitj:k.bfitj,res:m?k.res:null,tfs:S,warnings:[],covariateDimensions:[p,w]})}function Dh(e,t){let n=Ln(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 Uv(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((b,E)=>b*i[h][E])],w=e.bfitj[h],d=Gc(e.coefficients,p),v=w*Math.exp(d),N=Number.isFinite(v)&&(v!==0||w===0)?v:w===0?0:Math.exp(Math.log(w)+d);u+=N}f.push(-Math.expm1(-u))}return f})}var Hv=new Set(["type","time2","dist","weights","offset","strata","scale","init","df","control","intercept","confLevel"]),Yv=["maxiter","relTolerance","tolerChol"],Gv=["type","p","newX","newStrata","newOffset","seFit"],Xv=["type","weighted"],Qc=new WeakMap,Qv=Math.log(2*Math.PI),Zv=e=>Number(e),Jv=e=>e<-Math.LN2?Math.log1p(-Math.exp(e)):Math.log(-Math.expm1(e));function Bh(e,t){return t===-1/0?e:e<t?NaN:e+Jv(t-e)}function eN(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 Ph(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 tN(e,t,n){let r=t-e;return Number.isFinite(r)?r/n:t/n-e/n}function Kh(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 nN(e,t,n){let r=Kh(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 rN(e,t,n){let r=Kh(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 Bh(-Math.exp(i),-Math.exp(c))}function oN(e){return e<-36?e:-Math.log1p(Math.exp(-e))}function sN(e){return e>36?-e:-Math.log1p(Math.exp(e))}function Vh(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"?oN(e):t==="gaussian"||t==="loggaussian"||t==="lognormal"?se(e,{logP:!0}):Qt(e,n,{logP:!0})}function $h(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"?sN(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+Qv):wl(e,n,{log:!0})}function Xc(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 aN(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[_]=Xc(a,s,i),q=xa(a,s,i),F=aN(a,s,i);return{g:q-Math.log(o),e:-_/o,ee:F/(o*o),t:-a*_-1,tt:a*_+a*a*F,et:(_+a*F)/o}}let l=Vh(a,s,i),f=Vh(c,s,i),m=$h(c,s,i),u=$h(a,s,i),h=n===0?m:n===2?l:Bh(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),[v]=Xc(c,s,i),[N]=Xc(a,s,i),b=w===0||c===-1/0?0:w*v,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||b===0?0:c*c*b,M=a===1/0||E===0?0:a*a*E,R=-(d-w)/o,T=(E-b)/(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||b===0?0:c*b))/o-R*(1+S);return{g:h,e:R,ee:T,t:S,tt:A,et:C}}function iN(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 Uh(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 Hh(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 zh(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 rs(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 No(e,t,n)}function Yh(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=ke([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 jh(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"?ie(e):Wt(e,n)}function Wh(e,t,n,r,o,s,i,c,a,l,f,m,u,h,p,w=1e-10){let d=e[0].length,v=d+(m===void 0?a:0),N=Yh(e,w),b=N.values,E=N.exponent,g=u.map((k,V)=>V<d?Ve(k,E[V]):k),y=(k,V)=>{let K=0,X=Array(v).fill(0),re=Array.from({length:v},()=>Array(v).fill(0)),z=Array.from({length:v},()=>Array(v).fill(0));for(let J=0;J<t.length;J++){let Q=c[J],Re=dr(b[J],k.slice(0,d))+i[J],te=m??Math.exp(k[d+Q]),P=Ra(n[J],r[J],o[J],Re,te,l,f),Y=s[J];K+=Y*P.g;let j=Array(v).fill(0);for(let U=0;U<d;U++)j[U]=b[J][U]*P.e;m===void 0&&(j[d+Q]=P.t);for(let U=0;U<v;U++){X[U]+=Y*j[U];for(let W=0;W<v;W++)z[U][W]+=Y*j[U]*j[W]}if(V){for(let U=0;U<d;U++){for(let W=0;W<d;W++)re[U][W]+=Y*b[J][U]*b[J][W]*P.ee;m===void 0&&(re[U][d+Q]+=Y*b[J][U]*P.et)}if(m===void 0){for(let U=0;U<d;U++)re[d+Q][U]+=Y*b[J][U]*P.et;re[d+Q][d+Q]+=Y*P.tt}}}return{ll:K,u:X,H:re,B:z}},x=k=>{let V=k.H.map(X=>X.map(re=>-re)),K=Uh(V,w);return{factor:V,rank:K}},M=k=>{let V=y(k,!0),K=x(V),X=K.rank>=0?Hh(K.factor,V.u):rs(iN(V.B),V.u);return{e:V,delta:X}},R=y(g,!0),T=R.ll,S=0,A=!1,C=!1,_;try{_=M(g)}catch{_={e:R,delta:Array(v).fill(0)}}let q=g.map((k,V)=>k+_.delta[V]);if(h===0){let k=x(R),V=zh(k.factor),K=V.map(re=>re.slice()),X=V.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:V.map((re,z)=>re.map((J,Q)=>Ve(J,(z<d?-E[z]:0)+(Q<d?-E[Q]:0)))),VScaled:K,xExponent:E,score:R.u.map((re,z)=>z<d?Ve(re,E[z]):re),z:X,warn:A}}for(let k=1;k<=h;k++){let V=y(q,!0);if(Number.isFinite(V.ll)&&Math.abs(1-T/V.ll)<=p){g.splice(0,g.length,...q),R=V,T=V.ll,S=k,C=!0;break}if(!Number.isFinite(V.ll)||V.ll<T){let K=q.slice();q=g.map((X,re)=>X+(K[re]-X)/3);for(let X=0;X<a;X++)m===void 0&&(q[d+X]=Math.max(q[d+X],g[d+X]-1.1));S=k;continue}if(g.splice(0,g.length,...q),R=V,T=V.ll,S=k,k<h)try{let K=M(g);q=g.map((X,re)=>X+K.delta[re])}catch{q=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),L=x(F),O=zh(L.factor),I=O.map(k=>k.slice()),D=O.map((k,V)=>k[V]===0?NaN:g[V]/Math.sqrt(Math.max(0,k[V])));return{b:g.map((k,V)=>V<d?Ve(k,-E[V]):k),ll:T,iter:S,V:O.map((k,V)=>k.map((K,X)=>Ve(K,(V<d?-E[V]:0)+(X<d?-E[X]:0)))),VScaled:I,xExponent:E,score:F.u.map((k,V)=>V<d?Ve(k,E[V]):k),z:D,warn:A}}function cN(e,t,n=[],r={}){B(r,Hv,"survreg"),B(wn(r,"control"),Yv,"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 v=[],N=[],b=[],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))),_=[];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"&&Zv(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],ce=h[G];if(Number.isNaN(ve)&&Number.isNaN(ce))continue;if(ve>ce){S("Invalid interval: start > stop, NA created");continue}ve===ce?(Ne=ve,Ee=ce,ee=1):!Number.isFinite(ve)||ve===0?(Ne=-1/0,Ee=ce,ee=2):Number.isFinite(ce)?(Ne=ve,Ee=ce,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))}v.push(G),N.push(ee===1?Ee:Number.isFinite(Ne)&&Number.isFinite(Ee)?dn(Ne,Ee):Number.isFinite(Ne)?Ne:Ee),b.push(Ne),E.push(Ee),g.push(ee),y.push(p[G]),x.push(w[G]),M.push(ue),_.push(C[G])}if(v.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 q=M[0].length,F=q+(c?1:0),L=M.map(G=>c?[1,...G]:G),O=A.length,I=N.map((G,ue)=>G-x[ue]),D=y.reduce((G,ue)=>G+ue,0),k=I.reduce((G,ue,Me)=>G+ue*y[Me],0)/D,V=I.reduce((G,ue,Me)=>G+y[Me]*(ue-k)**2,0)/D,K=Math.max(...I)-Math.min(...I),X=s==="extreme"||s==="weibull"||s==="exponential"||s==="rayleigh"?V/1.64:s==="logistic"||s==="loglogistic"?V/3.2:s==="t"?V*(i-2)/i:V,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]=k),u===void 0)for(let G=0;G<O;G++)J[F+G]=Math.log(re);let Q=r.control??{},Re=Q.maxiter??30,te=Q.relTolerance??1e-9;if(!Number.isInteger(Re)||Re<0||!Number.isFinite(te)||te<0)throw new RangeError("invalid control");if(Q.tolerChol!==void 0&&(!Number.isFinite(Q.tolerChol)||Q.tolerChol<0))throw new RangeError("invalid control");let P=L.map(()=>[1]),Y=L.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(b[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 ne=U.reduce((G,ue)=>G+ue.w*ue.r,0)/W,fe=[r.init!==void 0&&Y?r.init[0]:ne,...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 Te=Y?Re:Math.max(Re,1e3),Ce=Wh(P,N,b,E,g,y,x,_,O,s,i,u,fe,Te,te,Q.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+_[ee]]),ce=Ra(b[ee],E[ee],g[ee],N[ee],ve,s,i),Se=-y[ee]*ce.ee;return Se>0&&Number.isFinite(Se)?{w:Se,r:N[ee]-ce.e/ce.ee-x[ee]}:{w:0,r:0}}),ue=Yh(L,Q.tolerChol??1e-10),Me=Array.from({length:F},(Ee,ee)=>Array.from({length:F},(ve,ce)=>ue.values.reduce((Se,Be,$e)=>Se+G[$e].w*Be[ee]*Be[ce],0))),Ne=Array.from({length:F},(Ee,ee)=>ue.values.reduce((ve,ce,Se)=>ve+G[Se].w*ce[ee]*G[Se].r,0));try{let Ee=Me.map(ce=>ce.slice()),ve=Uh(Ee,Q.tolerChol??1e-10)>=0?Hh(Ee,Ne):rs(nt(Me),Ne);be.splice(0,F,...ve.map((ce,Se)=>Ve(ce,-ue.exponent[Se])))}catch{}}let me=Y?Ce:Wh(L,N,b,E,g,y,x,_,O,s,i,u,be,Re,te,Q.tolerChol??1e-10),qe=f?v.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)=>L.reduce((Me,Ne)=>Me+Ne[ue],0)/L.length),et=L.map((G,ue)=>dr(G,me.b.slice(0,F))+x[ue]),Ue=2*(me.ll+qe-(Ce.ll+qe)),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]),le=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:le},strataLevels:A.map(G=>d.find(ue=>String(ue)===G)),hasOffset:r.offset!==void 0,icoef:u===void 0||v.length===1?Ce.b.slice():[Ce.b[0],Math.log(u)],var:de,loglik:[Ce.ll+qe,me.ll+qe],iter:me.iter,df:F+(u===void 0?O:0),idf:1+(u===void 0?O:0),dfResidual:v.length-(F+(u===void 0?O:0)),n:v.length,linearPredictors:et,means:ze,score:me.score.slice(),tableRowNames:pe,value:oe,se:we,z:ge,p:le,chi:Ue,chiDf:Xe,chiP:xt(Ue,Xe,{lowerTail:!1}),dist:s,distributionDf:i,parms:i,intercept:c,x:L,offset:x,rows:v,weights:y,strata:_,y:N,lower:b,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 Qc.set(tt,{V:me.VScaled,xExponent:me.xExponent}),Z("survreg",tt)}function lN(e,t={}){B(t,Gv,"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(v=>{let N=w+d*jh(v,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]],v=Qc.get(e),N=v?.V??e.var,b=E=>{let g=Array(N.length).fill(0);for(let y=0;y<h.length;y++)g[y]=v?Ve(h[y],-v.xExponent[y]):h[y];return N.length>h.length&&(g[h.length+i[p]]=E*d),Math.sqrt(Math.max(0,dr(g,rs(N,g))))};return n==="lp"||n==="linear"||n==="link"?b(0):n==="response"?a?Ph(w,b(0)):b(0):c.map(E=>{let g=jh(E,e.dist,e.parms),y=b(g),x=w+d*g;return n==="uquantile"?y:a?Ph(x,y):y})}),u=(n==="quantile"||n==="uquantile")&&c.length===1?m.map(h=>h[0]):m;return{fit:f,seFit:u,warnings:[]}}function fN(e,t={}){B(t,Xv,"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=Qc.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),v=Array(s).fill(0);for(let R=0;R<o;R++)v[R]=d[R]*p.e;s>o&&(v[o+e.strata[m]]=p.t);let N=rs(a,v),b=N.map((R,T)=>c&&T<o?Ve(R,-c.xExponent[T]):R),E=dr(v,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 b.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,rs(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=nN(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=tN(e.lower[m],e.upper[m],h);e.dist==="extreme"||e.dist==="weibull"||e.dist==="exponential"||e.dist==="rayleigh"?R=rN(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)?eN(y,u):M)*w});return l}var mN=["n","power","r","type","nfractional","alpha","sidedTest"],uN=["n","power","r","type","nfractional","alpha","sidedTest"],hN=["n","power","r","type","nfractional","alpha","sidedTest"],pN=["n","power","r","type","nfractional","alpha","sidedTest"],dN=["n","power","r","type","nfractional","alpha","sidedTest"],gN=["n","power","r","type","nfractional","alpha","sidedTest"],wN=["sigLevel","alternative","strict","nullOddsRatio","errbound","tsmethod"],bN=["alpha"],vN=["alpha"],NN=["alpha"],yN=["alpha"],MN=["alpha"],EN=["nmax"],Dr=.05,mn=e=>ie(e),Hn=e=>se(e),Or=e=>Number.isFinite(e),qt=e=>Or(e)&&e>=0&&e<=1,Mn=e=>Or(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 Pr(e){Je(e,"alpha",qt,"'alpha' must be a probability in [0, 1]")}function xN(e,t){return[e*t/(1+t),e/(1+t)]}function wo(e,t,n,r,o,s,i={}){let{n:c,power:a}=i,l=i.r??1,f=i.alpha??Dr,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",Or,"'treat' must be finite"),Je(r,"control",Or,"'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"),Pr(f),(e!=="equivalence"||u!=="equality")&&Je(s,"delta",C=>Or(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=>Or(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,v=e==="equivalence"&&u==="equality",N=v?void 0:s,b=e==="noninferiority"?p+N:e==="superiority"?p-N:v?p:N-Math.abs(p);if(e==="noninferiority"&&b<=0)throw new RangeError("Target power is not reachable under the specified hypotheses");let E=e==="superiority"?ie(f/h,{lowerTail:!1}):v?ie(f/2,{lowerTail:!1}):ie(f,{lowerTail:!1}),g=a??.8,y=v||e!=="equivalence"?ie(1-g,{lowerTail:!1}):ie((1-g)/2,{lowerTail:!1}),x,M,R,T,S;if(ye(c))b===0?(R=1/0,T=1/0):(T=(E+y)**2*(w/l+d)/(b*b),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]=xN(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"&&!v)S=Math.max(0,Hn((s-p)/C-mn(1-f))+Hn((s+p)/C-mn(1-f))-1);else if(v)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 _=Math.abs(p-s)/C;S=Hn(_-E)+Hn(-_-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 RN(e,t,n,r={}){return B(r,mN,"epiSsequb"),Z("epiSsequb",wo("equivalence","binary",e,t,void 0,n??void 0,r))}function TN(e,t,n,r,o={}){B(o,uN,"epiSsequc");let s=wo("equivalence","continuous",e,t,n,r??void 0,o);return Z("epiSsequc",s)}function SN(e,t,n,r={}){return B(r,hN,"epiSsninfb"),Z("epiSsninfb",wo("noninferiority","binary",e,t,void 0,n,r))}function AN(e,t,n,r,o={}){return B(o,pN,"epiSsninfc"),Z("epiSsninfc",wo("noninferiority","continuous",e,t,n,r,o))}function kN(e,t,n,r={}){return B(r,dN,"epiSssupb"),Z("epiSssupb",wo("superiority","binary",e,t,void 0,n,r))}function qN(e,t,n,r,o={}){return B(o,gN,"epiSssupc"),Z("epiSssupc",wo("superiority","continuous",e,t,n,r,o))}function go(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 go(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 IN(e,t,n,r={}){B(r,wN,"powerPaired2x2");let o=r.sigLevel??Dr,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",y=>Number.isSafeInteger(y)&&y>=0&&y<=1e5,"'npairs' must be a nonnegative safe integer"),Pr(o),Je(c,"nullOddsRatio",Mn,"'nullOddsRatio' must be positive and finite"),Je(a,"errbound",y=>Or(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=go(n,f),v=0,N=n;if(a>0){let y=0;for(;v<=n&&(y+=d[v])<=a/2;)v++;for(y=0;N>=0&&(y+=d[N])<=a/2;)N--}let b=0;for(let y=v;y<=N;y++){let x=d[y];if(x===0)continue;let M=go(y,m),R=go(y,u),T=0,S=0,A=new Array(y+1),C=new Array(y+1);for(let _=0;_<=y;_++)T+=R[_],A[_]=T;for(let _=y;_>=0;_--)S+=R[_],C[_]=S;for(let _=0;_<=y;_++){let q=A[_],F=C[_],L,O=u===.5&&o===1;w?L=O||(h>0?F<=o:q<=o):p?L=O||q<=o/2||F<=o/2:L=h>0?O?_>Math.floor(y/2):F<=o/2:O?_<Math.ceil(y/2):q<=o/2,L&&(b+=x*M[_])}}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 Z("powerPaired2x2",{power:b,npairs:n,pb:e,pc:t,sigLevel:o,alternative:s,nullOddsRatio:c,note:E,method:g,warnings:[]})}function _N(e,t,n,r,o,s={}){B(s,vN,"powerCT");let i=s.alpha??Dr;Pr(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 Hn(Math.sqrt(c*a)*Math.abs(o-1)/(c*o+1)-mn(1-i/2))}function LN(e,t,n,r,o,s={}){B(s,bN,"ssizeCT");let i=s.alpha??Dr;Pr(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 CN(e,t,n,r,o,s={}){B(s,yN,"powerEpi");let i=s.alpha??Dr;Pr(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 Hn(Math.sqrt(c)-mn(1-i/2))}function FN(e,t,n,r,o,s={}){B(s,NN,"ssizeEpi");let i=s.alpha??Dr;Pr(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 ON(e,t,n,r,o={}){B(o,MN,"numDEpi");let s=o.alpha??Dr;Pr(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 Gh(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 DN(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 Xh(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 PN(e,t,n,r,o={}){B(o,EN,"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)=>go(g,e)),c=Array.from({length:s+1},(E,g)=>go(g,t)),a=i.map(Gh),l=c.map(Gh),f=i.map(DN),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],_=y+x*(1-C),q=A,F=E-1,L=-1;for(;q<=F;){let O=Math.floor((q+F)/2),[,I]=Xh(A,O,M,R,f[x],T);I<=r?(L=O,q=O+1):F=O-1}if(L>=0){let[O]=Xh(A,L,M,R,f[x],T);O>n&&(L=-1)}L>=0&&(g===void 0||_<g.enP0)&&(g={r1:A,n1:y,r:L,n:E,enP0:_,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],v=w.reduce((E,g)=>g.enP0<E.enP0?g:E),N=w,b=[];return v.n>=s-1&&b.push(` Optimal sample size too close to nmax.
18
+ `,wo=(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})),os=e=>e[0].map((t,n)=>e.map(r=>r[n])),ss=(e,t=n=>n)=>e[0].map((n,r)=>kt(e.map(o=>t(o[r])))),Rh=(e,t=n=>n)=>e.map(n=>kt(n.map(t))),bo=(e,t=n=>n)=>kt(e[0].flatMap((n,r)=>e.map(o=>t(o[r])))),xa=e=>e<0?-1:1;function Bc(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 Th(e,t){let n=Bc(e),{d:r,u:o,v:s}=qs(n),i=r.map((a,l)=>l).filter(a=>r[a]>Math.sqrt(go)*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)&&dr(t,"In smc, smcs > 1 were set to 1.0"),c.some(a=>a<0)&&dr(t,"In smc, smcs < 0 were set to .0"),c.map(a=>a>1?1:a<0?0:a)}var dr=(e,t)=>{e.includes(t)||e.push(t)};function jc(e,t,n,r){let o=e.map((c,a)=>c.map((l,f)=>a===f?t[a]:l)),s=Ut(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 Ov=e=>wo(e,os(e));function xh(e,t,n,r){let o=Bc(e),s=o.length,i=Th(o,r),c=d=>{let v=jc(o,d.map(E=>1-E),t,E=>E<go?100*go:E),N=Ov(v),b=[];for(let E=0;E<s;E++)for(let w=E+1;w<s;w++)b.push((o[w][E]-N[w][E])**2);return kt(b)},a=d=>{let v=jc(o,d.map((N,b)=>o[b][b]-N),t,N=>Math.max(N,0));return d.map((N,b)=>{let E=0;for(let w=0;w<t;w++)E+=v[b][w]*v[b][w];return E+N-o[b][b]})},l=d=>{let v=Ut(o.map((T,A)=>T.map((k,C)=>A===C?1-d[A]:k)),{symmetric:!0}),N=v.values,b=v.vectors,E=N.map(T=>T>=go),w=N.map(T=>T<go?100*go:T),y=o.map((T,A)=>T.map((k,C)=>{if(A===C)return 0;let _=0;for(let I=0;I<t;I++)_+=w[I]*b[A][I]*b[C][I];return k-_})),x=wo(y,b),M=(T,A)=>{let k=0;for(let C=0;C<s;C++)k+=b[C][T]*x[C][A];return k},R=new Array(s).fill(0);for(let T=0;T<t;T++){let A=(k,C)=>{if(C===0)return;let _=C*M(T,k);for(let I=0;I<s;I++)R[I]+=_*b[I][T]*b[I][k]};E[T]&&A(T,1);for(let k=T+1;k<t;k++)A(k,2*(E[T]&&E[k]?1:(w[T]-w[k])/(N[T]-N[k])));for(let k=t;k<s;k++)A(k,2*w[T]/(N[T]-N[k]))}return R},f=.005,m={method:"L-BFGS-B",lower:f,upper:Math.max(yt(i),1)},u=new Array(s).fill(.01),h=Ze(()=>_o(i.map(d=>1-d),c,{gr:a,...m,control:{fnscale:1,parscale:u}}));h.par.some(d=>d===f)&&(h=Ze(()=>_o(h.par,c,{gr:l,...m,control:{fnscale:1,parscale:u,factr:1e3}}))),h.convergence!==0&&dr(n,`minres did not converge (optim convergence code ${h.convergence}): examine the results carefully`);let p=h.par,g=jc(o,p.map((d,v)=>o[v][v]-d),t,d=>Math.max(d,0));if(g=Kc(g,t),g=g.map(d=>d.map(v=>v===0?1e-15:v)),Rh(g,d=>d*d).some(d=>d>1)&&dr(n,"An ultra-Heywood case was detected. Examine the results carefully"),t>1){g=Kc(Ea(g).loadings,t);let d=ss(g,N=>N*N),v=d.map((N,b)=>b).sort((N,b)=>d[b]-d[N]||N-b);g=g.map(N=>v.map(b=>N[b]))}else g=Kc(g,t);return g}function Kc(e,t){let n=ss(e).map(xa);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 Dv(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:bo(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 Pv(e){let t=e[0].length,n=l=>{let f=rt(l),m=wo(e,os(f)),{f:u,Gq:h}=Dv(m),p=os(wo(wo(os(m),h),f)).map(g=>g.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=ss(r.map((d,v)=>d.map((N,b)=>N*s.G[v][b]))),m=s.G.map((d,v)=>d.map((N,b)=>N-r[v][b]*f[b])),u=Math.sqrt(bo(m,d=>d*d));if(c.push(s.f),u<1e-5){a=!0;break}if(i!==void 0){let d=i,v=r.map((b,E)=>b.map((w,y)=>w-d.T[E][y])),N=m.map((b,E)=>b.map((w,y)=>w-d.Gp[E][y]));bo(N,b=>b*b)>0&&(o=bo(v,b=>b*b)/Math.abs(bo(v.map((b,E)=>b.map((w,y)=>w*N[E][y])))),o=Math.max(1e-10,Math.min(o,20)))}else o=2*o;let h=yt(c.slice(-10)),p=s,g=r;for(let d=0;d<=10;d++){let v=r.map((b,E)=>b.map((w,y)=>w-o*m[E][y])),N=ss(v,b=>b*b).map(b=>1/Math.sqrt(b));if(g=v.map(b=>b.map((E,w)=>E*N[w])),p=n(g),h-p.f>.5*u*u*o)break;o=o/2}i={T:r,Gp:m},r=g,s=p}return{loadings:s.L,phi:wo(os(r),r),converged:a}}function Vv(e,t={}){B(t,Cv,"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)Ma(t.covmat),r=Bc(t.covmat);else{if(e===null)throw new TypeError("neither 'x' nor 'covmat' supplied");r=Io(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=xh(r,n,s,i),a,l,f;if(n===1)a=c.map(q=>q.slice()),f=[1],dr(i,`Omega_h for 1 factor is not meaningful, just omega_t
19
+ `),dr(s,"Omega_h and Omega_asymptotic are not meaningful with one factor");else{let q=Pv(c);if(q.converged||dr(s,"oblimin did not converge in 2000 iterations"),a=q.loadings,l=q.phi,n===2){let V=l[0][1],W=Math.sqrt(Math.abs(V));f=[W,xa(V)*W],dr(i,Fv)}else f=xh(l,1,s,i).map(V=>V[0])}let m=a.map(q=>{let V=0;for(let W=0;W<n;W++)V+=q[W]*f[W];return V}),u=Rh(c,q=>q*q),h=u.map((q,V)=>r[V][V]-q),p=a.map((q,V)=>q.map((W,Q)=>{if(n===1)return 0;let re=r[V][V],j=re-(re-W*W)-(W*f[Q])**2;return j<0&&(j=0),xa(W)*Math.sqrt(j)})),g=m.map((q,V)=>q*q/u[V]),d=m.map((q,V)=>{let W=[q,...p[V]];return kt(W.map(Q=>Q*Q))**2/kt(W.map(Q=>Q**4))}),v=m.map(xa);v.some(q=>q<0)&&(r=r.map((q,V)=>q.map((W,Q)=>v[V]*W*v[Q])));let N=m.map((q,V)=>v[V]*q),b=p.map((q,V)=>q.map(W=>v[V]*W)),E=N.map((q,V)=>[q,...b[V],u[V],h[V],g[V],d[V]]),w=bo(r),y=kt(r.map((q,V)=>q[V])),x=kt(N)**2,M=kt(h),R=x/w,T=(w-M)/w,A=x/(w-M),k=(w-y)/w*o/(o-1),C=(w+kt(Th(r,i))-y)/w,_=ss(E.map(q=>q.slice(0,n+1)),q=>q*q),I=_[0]/kt(_),F=b.map(q=>{let V=0;for(let W=1;W<n;W++)Math.abs(q[W])>Math.abs(q[V])&&(V=W);return V}),L=[],D=[];for(let q=0;q<n;q++){let V=F.map((j,ee)=>ee).filter(j=>F[j]===q),W=kt(V.flatMap(j=>V.map(ee=>r[ee][j]))),Q=kt(V.map(j=>b[j][q])),re=kt(V.map(j=>N[j]));D.push(Q*Q),L.push([(Q*Q+re*re)/W,re*re/W,Q*Q/W])}let S=[[T,kt(N)**2/w,kt(D)/w],...L],O={omegaH:R,omegaLim:A,alpha:k,omegaTot:T,G6:C,ECV:I,sl:E,slNames:["g",...Array.from({length:n},(q,V)=>`F${V+1}*`),"h2","u2","p2","com"],orthog:c,oblique:a,gloading:f,key:v,omegaGroup:S,warnings:s,messages:i};return l!==void 0&&(O.phi=l),J("omega",O)}var Ah=["ML","stdErr"],$v=["ML","stdErr"],on=.9999;function Uc(e){let t=Pe(e),n=[],r=0;for(let o=0;o<e.length-1;o++)r+=e[o],n.push(ie(r/t));return n}var Hc=e=>[...new Set(e)].sort((t,n)=>t-n),Sh=(e,t)=>e===1?`1 ${t} with zero marginal removed`:`${e} ${t}s with zero marginals removed`,kh=e=>Array.isArray(e)||ArrayBuffer.isView(e),qh=(e,t)=>e>0?ae(e,{lowerTail:!1})-ae(t,{lowerTail:!1}):ae(t)-ae(e);function Yc(e){let t=Ie([e]).groups[0],n=it(t),r=t.map(s=>s-n),o=Math.sqrt(at(r));return r.map(s=>s/o)}function Ih(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 Ra=e=>Array.from({length:e},()=>new Array(e).fill(0));function Ch(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 Wc={r:0,h:0,k:0,rr:0,rh:0,rk:0,hh:0,hk:0,kk:0};function zv(e,t,n){if(e===-1/0||t===-1/0||e===1/0&&t===1/0)return Wc;if(e===1/0)return{...Wc,k:Zt(t),kk:-t*Zt(t)};if(t===1/0)return{...Wc,h:Zt(e),hh:-e*Zt(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=Zt(e),f=Zt(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*Zt(c),hk:i,kk:-t*f*ae(a)-n/o*f*Zt(a)}}function _h(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:Ra(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=>zv(h,p,r))),u=[];for(let h=0;h<s;h++)for(let p=0;p<i;p++){let g=e[h][p];if(g===0)continue;let d=xo([t[h],n[p]],[t[h+1],n[p+1]],r),v=new Array(c).fill(0),N=Ra(c);for(let[b,E,w]of[[h+1,p+1,1],[h,p+1,-1],[h+1,p,-1],[h,p,1]]){let y=m[b][E],x=l(b),M=f(E);v[0]+=w*y.r,N[0][0]+=w*y.rr,x>=0&&(v[x]+=w*y.h,N[0][x]+=w*y.rh,N[x][0]+=w*y.rh,N[x][x]+=w*y.hh),M>=0&&(v[M]+=w*y.k,N[0][M]+=w*y.rk,N[M][0]+=w*y.rk,N[M][M]+=w*y.kk),x>=0&&M>=0&&(N[x][M]+=w*y.hk,N[M][x]+=w*y.hk)}Ch(a,g,d,v,N),u.push(g*Math.log(d))}return a.value=Pe(u),a}function jv(e,t,n,r,o){let s=o?n.length+1:1,i={value:0,gradient:new Array(s).fill(0),hessian:Ra(s)},c=(1-r)*(1+r),a=Math.sqrt(c),l=c*a,f=new Array(s),m=Ra(s),u=[],h=(p,g,d)=>{let v=n[p],N=(v-r*g)/a,b=Zt(N),E=(r*v-g)/l,w=1/a,y=v/l+3*r*(r*v-g)/(l*c),x=r/l;if(f[0]+=d*b*E,m[0][0]+=d*b*(y-N*E*E),o){let M=p+1;f[M]+=d*b*w;let R=d*b*(x-N*E*w);m[0][M]+=R,m[M][0]+=R,m[M][M]+=d*b*(-N*w*w)}};for(let p=0;p<e.length;p++){let g=e[p],d=t[p];f.fill(0);for(let b of m)b.fill(0);let v=d<n.length?(n[d]-r*g)/a:1/0,N=d>0?(n[d-1]-r*g)/a:-1/0;d<n.length&&h(d,g,1),d>0&&h(d-1,g,-1),Ch(i,1,qh(N,v),f,m),u.push(Math.log(qh(N,v)),Zt(g,{log:!0}))}return i.value=Pe(u),i}function Kv(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=rt(o,r)}catch{s=void 0}return(s===void 0||!s.every(Number.isFinite)||Pe(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 Fh=(e,t)=>e>=t-1e-12*Math.abs(t);function Lh(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=Kv(o,a);if(!s&&Math.abs(r[0])===on&&l[0]*r[0]>0){s=!0;continue}let f=Math.abs(r[0]+l[0])>on?(Math.sign(l[0])*on-r[0])/l[0]:1,m=!1;for(let u=0;u<60;u++){let h=r.map((g,d)=>g+f*l[d]);f*l[0]!==0&&Math.abs(h[0])>=on*(1-1e-15)&&(h[0]=Math.sign(h[0])*on);let p=e(h);if(Fh(p.value,o.value)){let g=yt(h.map((d,v)=>Math.abs(d-r[v])/Math.max(1,Math.abs(r[v]))));r=h,o=p,m=g>1e-12;break}f/=2}if(!m)break}return{theta:r,at:o,bounded:s}}function Oh(e,t){let n=Lh(e,[Math.max(-on,Math.min(on,t[0])),...t.slice(1)],!1);if(n.bounded||n.theta[0]===0)return n;let r=Lh(e,[Math.sign(n.theta[0])*on,...n.theta.slice(1)],!0);return Fh(r.at.value,n.at.value)?r:n}function Dh(e,t){let n=()=>e.map(o=>o.map(()=>1/0));if(t)return n();let r;try{r=rt(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 Ph=e=>`inadmissible correlation set to ${e>0?"0.9999":"-0.9999"}`;function Bv(e,t,n){ct(t)&&B(t,Ah,"polychor"),B(n,Ah,"polychor");let r,o;if(kh(t)){let L=e,D=t;if(L.length!==D.length)throw new RangeError("'x' and 'y' must have the same length");let S=L.map((Q,re)=>!Number.isNaN(Q)&&!Number.isNaN(D[re])),O=Pt(L.filter((Q,re)=>S[re])),q=Pt(D.filter((Q,re)=>S[re])),V=new Map(Hc(O).map((Q,re)=>[Q,re])),W=new Map(Hc(q).map((Q,re)=>[Q,re]));r=Array.from({length:V.size},()=>new Array(W.size).fill(0)),O.forEach((Q,re)=>r[V.get(Q)][W.get(q[re])]++),o=n??{}}else{let L=e;if(!L.every(kh))throw new TypeError("give a table (array of rows) or two vectors");let D=L[0]?.length??0;if(L.some(S=>S.length!==D))throw new TypeError("the table must have rows of equal length");if(r=L.map(S=>Array.from(S)),r.some(S=>S.some(O=>!(O>=0&&O<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;Pe(r.map(L=>Pe(L)))<2**1e3||({groups:r,k:a}=Ie(r));let l=r.map(L=>Pe(L)),f=l.filter(L=>L===0).length;f>0&&(c.push(Sh(f,"row")),r=r.filter((L,D)=>l[D]!==0));let m=(r[0]??[]).map((L,D)=>Pe(r.map(S=>S[D]))),u=m.filter(L=>L===0).length;u>0&&(c.push(Sh(u,"column")),r=r.map(L=>L.filter((D,S)=>m[S]!==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 J("polychor",{rho:NaN,warnings:c});let g=[-1/0,...Uc(r.map(L=>Pe(L))),1/0],d=[-1/0,...Uc(r[0].map((L,D)=>Pe(r.map(S=>S[D])))),1/0],N=Fl(L=>{let D=Math.max(-on,Math.min(on,L)),S=[];for(let O=0;O<h;O++)for(let q=0;q<p;q++){if(r[O][q]===0)continue;let V=xo([g[O],d[q]],[g[O+1],d[q+1]],D);S.push(r[O][q]*Math.log(V))}return-Pe(S)},-on,on).optimum;if(!s&&!i)return J("polychor",{rho:N,warnings:c});let b=L=>[-1/0,...L,1/0],E=g.slice(1,h),w=d.slice(1,p),y=L=>s?_h(r,b(L.slice(1,h)),b(L.slice(h)),L[0],!0):_h(r,g,d,L[0],!1),{theta:x,at:M,bounded:R}=Oh(y,s?[N,...E,...w]:[N]),T=x[0];if(R&&c.push(Ph(T)),!i)return J("polychor",{rho:T,warnings:c});let A=s?b(x.slice(1,h)):g,k=s?b(x.slice(h)):d,C=Pe(r.map(L=>Pe(L))),_=[],I=[];for(let L=0;L<h;L++)for(let D=0;D<p;D++){let S=r[L][D],O=xo([A[L],k[D]],[A[L+1],k[D+1]],T);if(S===0)_.push(C*O);else if(a===0)_.push(S*Math.log1p(1e-6/S),S*Ih(C*O/S-1));else{let q=$(S,a);I.push(q*Math.log1p(1e-6/q)),_.push(S*Ih(C*O/S-1))}}let F=Dh(M.hessian,R);return J("polychor",{rho:T,warnings:c,var:a===0?F:F.map(L=>L.map(D=>$(D,-a))),chisq:a===0?2*Pe(_):$(2*Pe(_),a)+2*Pe(I),df:h*p-h-p,n:$(C,a),rowCuts:A.slice(1,h),colCuts:k.slice(1,p)})}function Wv(e,t,n,r){let o=e.length,s=[ie(.25),ie(.5),ie(.75)],i=(h,p)=>p.filter(g=>g<h).length,c=Yc(e).map(h=>i(h,n)),a=Yc(t).map(h=>i(h,s)),l=Array.from({length:n.length+1},()=>new Array(4).fill(0));c.forEach((h,p)=>l[h][a[p]]++);let f=[-1/0,...n,1/0],m=[-1/0,...s,1/0],u=[];return l.forEach((h,p)=>h.forEach((g,d)=>{if(g===0)return;let v=xo([f[p],m[d]],[f[p+1],m[d+1]],r);u.push(g*Math.log((g+1e-6)/(v*o)))})),2*Pe(u)}function Uv(e,t,n={}){if(B(n,$v,"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=Hc(c);if(l.length<2)return J("polyserial",{rho:NaN,warnings:["y has fewer than 2 levels"]});let f=new Map(l.map((M,R)=>[M,R+1])),m=c.map(M=>f.get(M)),u=l.map(()=>0);for(let M of m)u[M-1]++;let h=Uc(u),p=[],g=Math.sqrt((a-1)/a)*Math.sqrt(at(m))*Dl(i,m)[0]/Pe(h.map(M=>Zt(M)));if(Math.abs(g)>on){let M=Math.sign(g)*on;p.push(`initial correlation inadmissible, ${$n(g)}, set to ${$n(M)}`),g=M}if(!r&&!o)return J("polyserial",{rho:g,warnings:p});let d=Yc(i),v=m.map(M=>M-1),N=M=>jv(d,v,r?M.slice(1):h,M[0],r),{theta:b,at:E,bounded:w}=Oh(N,r?[g,...h]:[g]);if(w&&p.push(Ph(b[0])),!o)return J("polyserial",{rho:b[0],warnings:p});let y=r?b.slice(1):h,x=l.length;return J("polyserial",{rho:b[0],warnings:p,var:Dh(E.hessian,w),chisq:Wv(m,d,y,r?b[0]:g),df:4*x-x-1,n:a,cuts:y})}var Hv=["group","strata","rho","cencode"],Yv=new Set(["cov2","tf","failcode","cencode","gtol","maxiter","init","variance","confLevel","names"]);function Ta(e){return e==null||typeof e=="number"&&Number.isNaN(e)}function Gc(e){let t=nn(e),n=Array.from(new Set(t)).sort(zn);return{labels:n,index:new Map(n.map((r,o)=>[r,o]))}}function Gv(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 Xv(e,t,n={}){B(n,Hv,"cuminc"),Gv(e,t,n);let{group:r,strata:o,rho:s=0,cencode:i=0}=n,c=[],a=new At;for(let w=0;w<e.length;w++)if(!(Number.isNaN(e[w])||Ta(t[w])||r!==void 0&&Ta(r[w])||o!==void 0&&Ta(o[w]))){if(!Number.isFinite(e[w]))throw new RangeError("'time' must contain only finite values");if(e[w]<0)throw new RangeError("'time' must be non-negative");c.push(w)}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]:nn(c.map(w=>r[w])),m=o===void 0?[1]:nn(c.map(w=>o[w])),u=nn([...c.map(w=>t[w]),i]),h=Gc(f);if(h.labels.length>=2&&!Number.isFinite(s))throw new RangeError("'rho' must be finite when Gray's test is requested");let p=Gc(m),g=Gc(u),d=u[u.length-1],v=g.labels.filter(w=>w!==d);if(v.length===0)throw new Error("there are no events of any cause");let N=c.map((w,y)=>({time:e[w],status:u[y],group:r===void 0?0:h.index.get(f[y]),strata:o===void 0?0:p.index.get(m[y]),source:w})).sort((w,y)=>w.time-y.time),b=[];for(let w of v){let y=Nt(w);for(let x=0;x<h.labels.length;x++){let M=N.filter(S=>S.group===x),R=M.reduce((S,O)=>Math.max(S,O.time),-1/0),T=R>0&&R<1?2**Math.min(1023,Math.ceil(-Math.log2(R))):1,A=fr(M.map(S=>S.time*T),M.map(S=>S.status===d?0:1)),k=[0],C=[0],_=[0],I=[],F=1;for(let S=0;S<A.time.length;S++){let O=A.nEvent[S];if(O===0)continue;let q=A.time[S]/T,V=A.nRisk[S],W=M.filter(Q=>Q.time===q&&Q.status===w).length;I.push({t:q,Y:V,d:O,dk:W,sb:F,sa:A.surv[S]}),F=A.surv[S]}let L=0,D=0;for(let S=0;S<I.length;S++){let O=I[S],q=L,V=D;L+=O.sb*O.dk/O.Y;let W=0;for(let Q=0;Q<=S;Q++){let re=I[Q],j=re.Y,ee=re.d,U=re.dk,ue=j===ee?0:(ee*(j-ee)+2*U*(ee-U))/((j-1)*(j-ee)**2),Z=j===1?U:U*(j-U)/(j*j*(j-1)),P=j===ee||j===1?0:U*(j-U)/(j*(j-ee)*(j-1)),Y=0;for(let H=0;H<=Q;H++){let K=I[H];Y+=K.sb*K.dk/K.Y}let z=L-Y;W+=z*z*ue+re.sb*re.sb*Z-2*z*re.sb*P}D=Math.max(0,W),O.dk>0&&(k.push(O.t,O.t),C.push(q,L),_.push(V,D))}k.push(R),C.push(L),_.push(D),b.push({name:`${Nt(h.labels[x])} ${y}`,group:Nt(h.labels[x]),cause:y,time:k,est:C,var:_})}}let E=h.labels.length>=2?Qv(N,h.labels,v,p.labels.length,s,d):void 0;return J("cuminc",{curves:b,...E===void 0?{}:{tests:E},warnings:a.list})}function Qv(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 g=0;g<r;g++){let d=e.filter(w=>w.strata===g),v=Array.from(new Set(d.filter(w=>w.status!==s).map(w=>w.time))).sort((w,y)=>w-y),N=new Array(a).fill(1),b=new Array(a).fill(0),E=[];for(let w of v){let y=new Array(a).fill(0),x=new Array(a).fill(0),M=new Array(a).fill(0);for(let S of d)S.time>=w&&y[S.group]++;for(let S of d)S.time===w&&S.status!==s&&(S.status===c?x[S.group]++:M[S.group]++);let R=N.slice(),T=b.slice(),A=y.map((S,O)=>S===0?0:S/R[O]),k=y.map((S,O)=>S===0?0:S*(1-T[O])/R[O]),C=x.reduce((S,O)=>S+O,0),_=A.reduce((S,O)=>S+O,0),I=_===0?0:C/_,F=E.length===0?1:E[E.length-1].Gafter,L=F-I,D=k.reduce((S,O)=>S+O,0);for(let S=0;S<a;S++){let O=F**o;D>0&&(l[S]+=O*(x[S]-k[S]*C/D)),y[S]>0?(N[S]=N[S]*(1-(x[S]+M[S])/y[S]),b[S]+=R[S]*x[S]/y[S]):N[S]=0}E.push({time:w,Y:y,d1:x,d2:M,Sbefore:R,Safter:N.slice(),Fbefore:T,h:A,R:k,dF0:I,Gbefore:F,Gafter:L})}for(let w=0;w<a;w++)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],A=T.h.reduce((ee,U)=>ee+U,0);if(!(A>0)||!(T.h[M]>0))continue;let k=T.Gbefore**o,C=k*T.h[w]*((w===M?1:0)-T.h[M]/A),_=k*T.h[y]*((y===M?1:0)-T.h[M]/A),I=0,F=0;for(let ee=R+1;ee<E.length;ee++){let U=E[ee],ue=U.h.reduce((H,K)=>H+K,0);if(!(ue>0))continue;let Z=U.Gbefore**o,P=Z*U.h[w]*((w===M?1:0)-U.h[M]/ue),Y=Z*U.h[y]*((y===M?1:0)-U.h[M]/ue),z=U.Gbefore===0?0:U.dF0/U.Gbefore;I+=P*z,F+=Y*z}let L=T.Safter[M]===0?0:T.Gafter/T.Safter[M],D=C+(1-L)*I,S=_+(1-L)*F,O=-L*I,q=-L*F,V=T.Y[M]===0?0:T.Sbefore[M]*T.d2[M]/T.Y[M],W=(D*S*T.dF0+O*q*V)/T.h[M],Q=T.d1.reduce((ee,U)=>ee+U,0),re=0;if(Q>1&&T.Y[M]>0&&T.Sbefore[M]>0){let ee=1/T.Sbefore[M];A>ee&&(re=Q*(Q-1)/A*(T.Y[M]*ee*ee/(A-ee))*(D*S/(T.h[M]*T.h[M])))}let j=0;T.d2[M]>1&&T.Y[M]>1&&(j=(T.d2[M]-1)/(T.Y[M]-1)*(V/T.h[M])*O*q),x+=W-re-j}f[w][y]+=x}}let m=a-1,u=NaN,h=NaN,p=f.slice(0,m).map(g=>g.slice(0,m));try{let g=rt(p,l.slice(0,m),{tol:1e-12});u=l.slice(0,m).reduce((d,v,N)=>d+v*g[N],0),h=Rt(u,m,{lowerTail:!1})}catch{u=NaN,h=NaN}i.push({cause:Nt(c),stat:u,df:m,pValue:h})}return i}function Zv(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 Ln(e,t=e){return Array.from({length:e},()=>new Array(t).fill(0))}function Jv(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 Xc(e,t){return Pe(e.map((n,r)=>n*t[r]))}function eN(e){return e.every(t=>t.every(Number.isFinite))}function tN(e,t,n,r={}){B(r,Yv,"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,g=o?.[0]?.length??0,d=p+g;if(d===0)throw new RangeError("at least one covariate is required");if(n!==void 0&&n.some(K=>K.length!==p))throw new RangeError("rows of 'cov1' must have equal length");if(o!==void 0&&o.some(K=>K.length!==g))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 v=[];for(let K=0;K<e.length;K++)if(!(Number.isNaN(e[K])||Ta(t[K])||n!==void 0&&n[K].some(Number.isNaN)||o!==void 0&&o[K].some(Number.isNaN))){if(!Number.isFinite(e[K]))throw new RangeError("'time' must contain only finite values");if(n!==void 0&&!n[K].every(Number.isFinite)||o!==void 0&&!o[K].every(Number.isFinite))throw new RangeError("covariates must be finite");v.push(K)}let N=e.length-v.length;if(v.length===0)throw new RangeError("no complete observations");let b=v.map(K=>e[K]),E=nn([...v.map(K=>t[K]),c,i]),w=E.slice(0,v.length),y=E[E.length-2],x=E[E.length-1],M=v.map(K=>n===void 0?[]:Array.from(n[K])),R=v.map(K=>o===void 0?[]:Array.from(o[K])),T=Array.from(new Set(b.filter((K,te)=>w[te]===x))).sort((K,te)=>K-te);if(T.length===0)throw new RangeError("no observations of 'failcode'");let A=g===0?[[0]]:s(T).map(K=>Array.from(K));if(g>0&&(A.length!==T.length||A.some(K=>K.length!==g)||!eN(A)))throw new RangeError("'tf' must return a finite matrix with one row per unique failure time and one column per cov2 column");let k=T.map((K,te)=>b.map((se,Me)=>[...M[Me],...R[Me].map((Ne,de)=>Ne*(A[te][de]??0))])),C=b.reduce((K,te)=>Math.max(K,Math.abs(te)),0),_=C>0&&C<1?2**Math.min(1023,Math.ceil(-Math.log2(C))):1,I=fr(b.map(K=>K*_),w.map(K=>K===y?1:0)),F=K=>{let te=K*_,se=1;for(let Me=0;Me<I.time.length&&!(I.time[Me]>=te);Me++)se=I.surv[Me];return se},L=T.map(K=>b.map((te,se)=>{if(te>=K)return 1;if(w[se]===y||w[se]===x)return 0;let Me=F(te);return Me===0?0:F(K)/Me}));for(let K=0;K<T.length;K++)for(let te=0;te<b.length;te++)b[te]===T[K]&&w[te]===x&&(L[K][te]=1);let D=K=>{let te=Array.from({length:d},()=>[]),se=Array.from({length:d},()=>Array.from({length:d},()=>[])),Me=[],Ne=Ln(T.length,d),de=[],ce=[],qe=[];for(let Fe=0;Fe<T.length;Fe++){let We=T[Fe],ze=b.map((me,Se)=>me===We&&w[Se]===x?Se:-1).filter(me=>me>=0),tt=ze.length,Ue=k[Fe],Xe=Ue.map(me=>Xc(K,me)),ge=L[Fe].slice();for(let me of ze)ge[me]=1;let oe=Math.max(...Xe.map((me,Se)=>ge[Se]>0?me:-1/0));if(oe===-1/0)throw new Error("empty Fine-Gray risk set");let ve=oe<500&&oe>-500,be=ve?Xe.map((me,Se)=>ge[Se]*Math.exp(me)):Xe.map((me,Se)=>ge[Se]*Math.exp(me-oe)),fe=Pe(be),nt=Array.from({length:d},(me,Se)=>Pe(be.map((Ke,$e)=>Ke*Ue[$e][Se])));if(!(fe>0))throw new Error("empty Fine-Gray risk set");let X=nt.map(me=>me/fe),Te=Array.from({length:d},(me,Se)=>{let Ke=Ue[0][Se],$e=0,st=0;for(let mt of Ue)$e=Math.max($e,Math.abs(mt[Se]-Ke)),st=Math.max(st,Math.abs(mt[Se]));return $e>0&&st/$e>1e6}).some(Boolean),Ee=Te?Array.from({length:d},(me,Se)=>Ue.map(Ke=>Ke[Se]-Ue[0][Se])):[],Ae=Te?Ee.map(me=>Pe(me.map((Se,Ke)=>be[Ke]*Se))/fe):[],ne=Te?void 0:Array.from({length:d},(me,Se)=>Array.from({length:d},(Ke,$e)=>Pe(be.map((st,mt)=>st*Ue[mt][Se]*Ue[mt][$e]))));ce.push(X),qe.push(be.map(me=>me/fe));let ye=ve?tt/fe:tt*Math.exp(-oe)/fe;de.push(ye),Me.push(Pe(ze.map(me=>Xc(K,Ue[me])))-tt*(ve?Math.log(fe):oe+Math.log(fe)));for(let me=0;me<d;me++){let Ke=Pe(ze.map($e=>Ue[$e][me]))-tt*X[me];te[me].push(Ke),Ne[Fe][me]=Ke}for(let me=0;me<d;me++)for(let Se=0;Se<d;Se++){let Ke=Te?Pe(be.map(($e,st)=>$e*(Ee[me][st]-Ae[me])*(Ee[Se][st]-Ae[Se])))/fe:ne[me][Se]/fe-X[me]*X[Se];se[me][Se].push(tt*Ke)}}let we=Ln(b.length,d);for(let Fe=0;Fe<T.length;Fe++){let We=k[Fe],ze=qe[Fe],tt=ce[Fe],Ue=T[Fe];for(let Xe=0;Xe<b.length;Xe++){let ge=b[Xe]===Ue&&w[Xe]===x?1:0,oe=b.filter((be,fe)=>be===Ue&&w[fe]===x).length,ve=ge-ze[Xe]*oe;for(let be=0;be<d;be++)we[Xe][be]+=(We[Xe][be]-tt[be])*ve}}let Oe=te.map(Pe),xe=se.map(Fe=>Fe.map(Pe));return{loglik:Pe(Me),score:Oe,inf:xe,bfitj:de,res:Ne,eta:we,means:ce,riskFactors:qe,zByTime:k}},S=f===void 0?new Array(d).fill(0):Array.from(f);if(!S.every(Number.isFinite))throw new RangeError("'init' must contain finite values");let O=S.slice(),q=D(O),V=!1,W=0,Q=(K,te)=>Math.max(...K.score.map((se,Me)=>Math.abs(se)*Math.max(Math.abs(te[Me]),1)))<a*Math.max(Math.abs(K.loglik),1);for(;;){if(Q(q,O)){V=!0;break}if(W>=l)break;let K=rt(q.inf,q.score,{tol:0});O=O.map((te,se)=>te+K[se]),W++,q=D(O)}let re=rt(q.inf),j=Ln(d);if(m){let K=Ln(d),te=b.length,se=Array.from(new Set(b)).sort((ce,qe)=>ce-qe),Me=se.map(ce=>{let qe=b.filter(Oe=>Oe>=ce).length,we=b.filter((Oe,xe)=>Oe===ce&&w[xe]===y).length;return qe===0?0:we/qe}),Ne=Ln(te,d);for(let ce=0;ce<se.length;ce++){let qe=se[ce],we=new Array(d).fill(0);for(let xe=0;xe<te;xe++)if(!(!(b[xe]<qe)||w[xe]===y||w[xe]===x))for(let _e=0;_e<T.length;_e++){if(T[_e]<qe)continue;let Fe=q.riskFactors[_e][xe],We=b.filter((Ue,Xe)=>Ue===T[_e]&&w[Xe]===x).length,ze=q.zByTime[_e][xe],tt=q.means[_e];for(let Ue=0;Ue<d;Ue++)we[Ue]+=(ze[Ue]-tt[Ue])*Fe*We}let Oe=b.filter(xe=>xe>=qe).length;if(Oe!==0)for(let xe=0;xe<te;xe++){let _e=b[xe]===qe&&w[xe]===y?1:0,Fe=b[xe]>=qe?Me[ce]:0;for(let We=0;We<d;We++)Ne[xe][We]+=we[We]/Oe*(_e-Fe)}}for(let ce=0;ce<te;ce++)Jv(K,q.eta[ce].map((qe,we)=>qe+Ne[ce][we]),q.eta[ce].map((qe,we)=>qe+Ne[ce][we]),1);j=Ln(d);let de=Vh(re,K);j=Vh(de,re)}else j=Ln(d).map(K=>K.map(()=>NaN));let ee=m?j.map((K,te)=>Math.sqrt(K[te])):void 0,U=ee===void 0?void 0:O.map((K,te)=>K/ee[te]),ue=U===void 0?void 0:U.map(K=>2*ae(-Math.abs(K))),Z=m?ie((1+u)/2):void 0,P=Z===void 0?void 0:O.map((K,te)=>Math.exp(K-Z*ee[te])),Y=Z===void 0?void 0:O.map((K,te)=>Math.exp(K+Z*ee[te])),z=m?O.map(Math.exp):void 0,H=h===void 0?[...Array.from({length:p},(K,te)=>`cov1${te+1}`),...Array.from({length:g},(K,te)=>`cov2${te+1}*tf${te+1}`)]:Array.from(h);return J("crr",{coefficients:O,names:H,var:j,...ee===void 0?{}:{se:ee,z:U,pValue:ue,lower:P,upper:Y,expCoef:z},loglik:q.loglik,loglikNull:D(new Array(d).fill(0)).loglik,logtest:{statistic:2*(q.loglik-D(new Array(d).fill(0)).loglik),df:d},score:q.score,inf:m?q.inf:Ln(d).map(K=>K.map(()=>NaN)),invinf:m?re:Ln(d).map(K=>K.map(()=>NaN)),converged:V,iterations:W,n:v.length,nMissing:N,uftime:T,bfitj:q.bfitj,res:m?q.res:null,tfs:A,warnings:[],covariateDimensions:[p,g]})}function Vh(e,t){let n=Ln(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 nN(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((b,E)=>b*i[h][E])],g=e.bfitj[h],d=Xc(e.coefficients,p),v=g*Math.exp(d),N=Number.isFinite(v)&&(v!==0||g===0)?v:g===0?0:Math.exp(Math.log(g)+d);u+=N}f.push(-Math.expm1(-u))}return f})}var rN=new Set(["type","time2","dist","weights","offset","strata","scale","init","df","control","intercept","confLevel"]),oN=["maxiter","relTolerance","tolerChol"],sN=["type","p","newX","newStrata","newOffset","seFit"],aN=["type","weighted"],Zc=new WeakMap,iN=Math.log(2*Math.PI),cN=e=>Number(e),lN=e=>e<-Math.LN2?Math.log1p(-Math.exp(e)):Math.log(-Math.expm1(e));function Uh(e,t){return t===-1/0?e:e<t?NaN:e+lN(t-e)}function fN(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 $h(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 mN(e,t,n){let r=t-e;return Number.isFinite(r)?r/n:t/n-e/n}function Hh(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 uN(e,t,n){let r=Hh(e,t,n),o=r>Math.log(Number.MAX_VALUE)?1/0:Math.exp(r);if(o<1e-8){let s=t-e;return gn(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 hN(e,t,n){let r=Hh(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 Uh(-Math.exp(i),-Math.exp(c))}function pN(e){return e<-36?e:-Math.log1p(Math.exp(-e))}function dN(e){return e>36?-e:-Math.log1p(Math.exp(e))}function zh(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"?pN(e):t==="gaussian"||t==="loggaussian"||t==="lognormal"?ae(e,{logP:!0}):Bt(e,n,{logP:!0})}function jh(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"?dN(e):t==="gaussian"||t==="loggaussian"||t==="lognormal"?ae(e,{lowerTail:!1,logP:!0}):Bt(e,n,{lowerTail:!1,logP:!0})}function Aa(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+iN):bl(e,n,{log:!0})}function Qc(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 gN(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 Sa(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[_]=Qc(a,s,i),I=Aa(a,s,i),F=gN(a,s,i);return{g:I-Math.log(o),e:-_/o,ee:F/(o*o),t:-a*_-1,tt:a*_+a*a*F,et:(_+a*F)/o}}let l=zh(a,s,i),f=zh(c,s,i),m=jh(c,s,i),u=jh(a,s,i),h=n===0?m:n===2?l:Uh(c>0?m:l,c>0?u:f),g=c===-1/0?0:Math.exp(n===0&&(s==="extreme"||s==="weibull"||s==="exponential"||s==="rayleigh")?c:Aa(c,s,i)-h),d=a===1/0?0:Math.exp(Aa(a,s,i)-h),[v]=Qc(c,s,i),[N]=Qc(a,s,i),b=g===0||c===-1/0?0:g*v,E=d===0||a===1/0?0:d*N,w=c===-1/0||g===0?0:c*g,y=a===1/0||d===0?0:a*d,x=c===-1/0||b===0?0:c*c*b,M=a===1/0||E===0?0:a*a*E,R=-(d-g)/o,T=(E-b)/(o*o)-R*R,A=-(y-w),k=M-x-A*(1+A),C=((a===1/0||E===0?0:a*E)-(c===-1/0||b===0?0:c*b))/o-R*(1+A);return{g:h,e:R,ee:T,t:A,tt:k,et:C}}function wN(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 Yh(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 Gh(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 Kh(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 as(e,t){return e.map(n=>gr(n,t))}function gr(e,t){let n=e.reduce((r,o,s)=>r+o*t[s],0);return Eo(e,t,n)}function Xh(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=Ie([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 Bh(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"?ie(e):Wt(e,n)}function Wh(e,t,n,r,o,s,i,c,a,l,f,m,u,h,p,g=1e-10){let d=e[0].length,v=d+(m===void 0?a:0),N=Xh(e,g),b=N.values,E=N.exponent,w=u.map((q,V)=>V<d?Ve(q,E[V]):q),y=(q,V)=>{let W=0,Q=Array(v).fill(0),re=Array.from({length:v},()=>Array(v).fill(0)),j=Array.from({length:v},()=>Array(v).fill(0));for(let ee=0;ee<t.length;ee++){let U=c[ee],ue=gr(b[ee],q.slice(0,d))+i[ee],Z=m??Math.exp(q[d+U]),P=Sa(n[ee],r[ee],o[ee],ue,Z,l,f),Y=s[ee];W+=Y*P.g;let z=Array(v).fill(0);for(let H=0;H<d;H++)z[H]=b[ee][H]*P.e;m===void 0&&(z[d+U]=P.t);for(let H=0;H<v;H++){Q[H]+=Y*z[H];for(let K=0;K<v;K++)j[H][K]+=Y*z[H]*z[K]}if(V){for(let H=0;H<d;H++){for(let K=0;K<d;K++)re[H][K]+=Y*b[ee][H]*b[ee][K]*P.ee;m===void 0&&(re[H][d+U]+=Y*b[ee][H]*P.et)}if(m===void 0){for(let H=0;H<d;H++)re[d+U][H]+=Y*b[ee][H]*P.et;re[d+U][d+U]+=Y*P.tt}}}return{ll:W,u:Q,H:re,B:j}},x=q=>{let V=q.H.map(Q=>Q.map(re=>-re)),W=Yh(V,g);return{factor:V,rank:W}},M=q=>{let V=y(q,!0),W=x(V),Q=W.rank>=0?Gh(W.factor,V.u):as(wN(V.B),V.u);return{e:V,delta:Q}},R=y(w,!0),T=R.ll,A=0,k=!1,C=!1,_;try{_=M(w)}catch{_={e:R,delta:Array(v).fill(0)}}let I=w.map((q,V)=>q+_.delta[V]);if(h===0){let q=x(R),V=Kh(q.factor),W=V.map(re=>re.slice()),Q=V.map((re,j)=>re[j]===0?NaN:w[j]/Math.sqrt(Math.max(0,re[j])));return{b:w.map((re,j)=>j<d?Ve(re,-E[j]):re),ll:R.ll,iter:A,V:V.map((re,j)=>re.map((ee,U)=>Ve(ee,(j<d?-E[j]:0)+(U<d?-E[U]:0)))),VScaled:W,xExponent:E,score:R.u.map((re,j)=>j<d?Ve(re,E[j]):re),z:Q,warn:k}}for(let q=1;q<=h;q++){let V=y(I,!0);if(Number.isFinite(V.ll)&&Math.abs(1-T/V.ll)<=p){w.splice(0,w.length,...I),R=V,T=V.ll,A=q,C=!0;break}if(!Number.isFinite(V.ll)||V.ll<T){let W=I.slice();I=w.map((Q,re)=>Q+(W[re]-Q)/3);for(let Q=0;Q<a;Q++)m===void 0&&(I[d+Q]=Math.max(I[d+Q],w[d+Q]-1.1));A=q;continue}if(w.splice(0,w.length,...I),R=V,T=V.ll,A=q,q<h)try{let W=M(w);I=w.map((Q,re)=>Q+W.delta[re])}catch{I=w.slice()}else try{let W=M(w),Q=w.map((j,ee)=>j+W.delta[ee]),re=y(Q,!1);Number.isFinite(re.ll)&&re.ll>=T&&w.splice(0,w.length,...Q)}catch{}}A===h&&h>=2&&!C&&(k=!0);let F=y(w,!0),L=x(F),D=Kh(L.factor),S=D.map(q=>q.slice()),O=D.map((q,V)=>q[V]===0?NaN:w[V]/Math.sqrt(Math.max(0,q[V])));return{b:w.map((q,V)=>V<d?Ve(q,-E[V]):q),ll:T,iter:A,V:D.map((q,V)=>q.map((W,Q)=>Ve(W,(V<d?-E[V]:0)+(Q<d?-E[Q]:0)))),VScaled:S,xExponent:E,score:F.u.map((q,V)=>V<d?Ve(q,E[V]):q),z:O,warn:k}}function bN(e,t,n=[],r={}){B(r,rN,"survreg"),B(Kt(r,"control"),oN,"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),g=r.offset??Array(a).fill(0),d=r.strata??Array(a).fill("1");for(let[X,he]of[[p,"weights"],[g,"offset"],[d,"strata"]])if(X.length!==a)throw new RangeError(`variable lengths differ (found for '${he}')`);let v=[],N=[],b=[],E=[],w=[],y=[],x=[],M=[],R=[],T=(o==="right"||o==="left")&&l.some(X=>Number(X)===2),A=X=>{R.includes(X)||R.push(X)},k=[...new Set(d.map(String))].sort(),C=d.map(X=>k.indexOf(String(X))),_=[];if(u!==void 0&&k.length>1)throw new Error("The scale argument is not valid with multiple strata");for(let X=0;X<a;X++){let he=n.length===0?[]:Array.from(n[X]);if((o==="interval2"?Number.isNaN(e[X])&&Number.isNaN(h[X]):o==="interval"&&cN(l[X])===3?Number.isNaN(e[X])||Number.isNaN(h[X]):Number.isNaN(e[X]))||o!=="interval2"&&Number.isNaN(l[X])||Number.isNaN(p[X])||Number.isNaN(g[X])||Number.isNaN(d[X])||he.some(Number.isNaN))continue;if(!(p[X]>0))throw new RangeError("Invalid weights, must be >0");if(he.some(ye=>!Number.isFinite(ye)))throw new Error("data contains an infinite predictor");let Ee=e[X],Ae=e[X],ne=Number(l[X]);if(o==="right")if(T){if(ne!==1&&ne!==2){A("Invalid status value, converted to NA");continue}ne===1&&(Ae=1/0),ne=ne===2?1:0}else{if(ne!==0&&ne!==1){A("Invalid status value, converted to NA");continue}ne===0&&(Ae=1/0),ne=ne===1?1:0}else if(o==="left")if(T){if(ne!==1&&ne!==2){A("Invalid status value, converted to NA");continue}ne===1&&(Ee=-1/0),ne=ne===2?1:2}else{if(ne!==0&&ne!==1){A("Invalid status value, converted to NA");continue}ne===0&&(Ee=-1/0),ne=ne===1?1:2}else if(o==="interval"){if(Ae=h[X],ne===0)Ee=e[X],Ae=1/0;else if(ne===1)Ae=Ee;else if(ne===2)Ae=e[X],Ee=-1/0;else if(ne!==3){A("Invalid status value, converted to NA");continue}if(ne===3&&Ee>Ae){A("Invalid interval: start > stop, NA created");continue}if(ne===3&&Ee===Ae)throw new Error("initial iteration failed (use starting estimates?)")}else if(o==="interval2"){let ye=e[X],me=h[X];if(Number.isNaN(ye)&&Number.isNaN(me))continue;if(ye>me){A("Invalid interval: start > stop, NA created");continue}ye===me?(Ee=ye,Ae=me,ne=1):!Number.isFinite(ye)||ye===0?(Ee=-1/0,Ae=me,ne=2):Number.isFinite(me)?(Ee=ye,Ae=me,ne=3):(Ee=ye,Ae=1/0,ne=0)}if(f){if((Number.isFinite(Ee)&&Ee<0||Number.isFinite(Ae)&&Ae<0)&&A("NaNs produced"),Ee===1/0||Ae===-1/0||Number.isFinite(Ee)&&Ee<=0||Number.isFinite(Ae)&&Ae<=0)throw new Error("Invalid survival times for this distribution");Number.isFinite(Ee)&&(Ee=Math.log(Ee)),Number.isFinite(Ae)&&(Ae=Math.log(Ae))}v.push(X),N.push(ne===1?Ae:Number.isFinite(Ee)&&Number.isFinite(Ae)?gn(Ee,Ae):Number.isFinite(Ee)?Ee:Ae),b.push(Ee),E.push(Ae),w.push(ne),y.push(p[X]),x.push(g[X]),M.push(he),_.push(C[X])}if(v.length===0)throw new Error("initial iteration failed (use starting estimates?)");if(!w.some(X=>X===1||X===3))throw new Error("initial iteration failed (use starting estimates?)");if(N.some(X=>!Number.isFinite(X)))throw new Error("initial iteration failed (use starting estimates?)");if(M.some(X=>X.length!==M[0].length))throw new TypeError("all rows of the matrix must have the same length");let I=M[0].length,F=I+(c?1:0),L=M.map(X=>c?[1,...X]:X),D=k.length,S=N.map((X,he)=>X-x[he]),O=y.reduce((X,he)=>X+he,0),q=S.reduce((X,he,Te)=>X+he*y[Te],0)/O,V=S.reduce((X,he,Te)=>X+y[Te]*(he-q)**2,0)/O,W=Math.max(...S)-Math.min(...S),Q=s==="extreme"||s==="weibull"||s==="exponential"||s==="rayleigh"?V/1.64:s==="logistic"||s==="loglogistic"?V/3.2:s==="t"?V*(i-2)/i:V,re=u??2*Math.sqrt(Q);if(u===void 0&&(!(re>0)||!(W>0)))throw new Error("initial iteration failed (use starting estimates?)");let j=F+(u===void 0?D:0),ee=Array(j).fill(0);if(c&&(ee[0]=q),u===void 0)for(let X=0;X<D;X++)ee[F+X]=Math.log(re);let U=r.control??{},ue=U.maxiter??30,Z=U.relTolerance??1e-9;if(!Number.isInteger(ue)||ue<0||!Number.isFinite(Z)||Z<0)throw new RangeError("invalid control");if(U.tolerChol!==void 0&&(!Number.isFinite(U.tolerChol)||U.tolerChol<0))throw new RangeError("invalid control");let P=L.map(()=>[1]),Y=L.every(X=>X.length===1&&X[0]===1);if(r.init!==void 0&&r.init.length!==F&&r.init.length!==j)throw new Error("Wrong length for initial parameters");let H=N.map((X,he)=>Sa(b[he],E[he],w[he],X,u??re,s,i)).map((X,he)=>{let Te=-y[he]*X.ee;return Te>0&&Number.isFinite(Te)?{w:Te,r:N[he]-X.e/X.ee-x[he]}:{w:0,r:0}}),K=H.reduce((X,he)=>X+he.w,0);if(!(K>0))throw new Error("initial iteration failed (use starting estimates?)");let te=H.reduce((X,he)=>X+he.w*he.r,0)/K,se=[r.init!==void 0&&Y?r.init[0]:te,...u===void 0?Array(D).fill(Math.log(re)):[]];if(Y&&r.init!==void 0&&r.init.length===j)for(let X=0;X<D;X++)se[1+X]=r.init[1+X];let Me=Y?ue:Math.max(ue,1e3),Ne=Wh(P,N,b,E,w,y,x,_,D,s,i,u,se,Me,Z,U.tolerChol??1e-10),de=ee.slice();if(u===void 0)for(let X=0;X<D;X++)de[F+X]=Ne.b[1+X];if(!Y&&r.init!==void 0){for(let X=0;X<F;X++)de[X]=r.init[X];if(r.init.length===j)for(let X=F;X<j;X++)de[X]=r.init[X]}else if(!Y&&F>0){let X=M.map((Ae,ne)=>{let ye=u??Math.exp(Ne.b[1+_[ne]]),me=Sa(b[ne],E[ne],w[ne],N[ne],ye,s,i),Se=-y[ne]*me.ee;return Se>0&&Number.isFinite(Se)?{w:Se,r:N[ne]-me.e/me.ee-x[ne]}:{w:0,r:0}}),he=Xh(L,U.tolerChol??1e-10),Te=Array.from({length:F},(Ae,ne)=>Array.from({length:F},(ye,me)=>he.values.reduce((Se,Ke,$e)=>Se+X[$e].w*Ke[ne]*Ke[me],0))),Ee=Array.from({length:F},(Ae,ne)=>he.values.reduce((ye,me,Se)=>ye+X[Se].w*me[ne]*X[Se].r,0));try{let Ae=Te.map(me=>me.slice()),ye=Yh(Ae,U.tolerChol??1e-10)>=0?Gh(Ae,Ee):as(rt(Te),Ee);de.splice(0,F,...ye.map((me,Se)=>Ve(me,-he.exponent[Se])))}catch{}}let ce=Y?Ne:Wh(L,N,b,E,w,y,x,_,D,s,i,u,de,ue,Z,U.tolerChol??1e-10),qe=f?v.reduce((X,he,Te)=>X+(w[Te]===1?y[Te]*Math.log(e[he]):0),0)*-1:0,we=ce.V.map(X=>X.slice()),Oe=ce.b.slice(0,F).map((X,he)=>Number.isNaN(ce.z[he])?NaN:X),xe=Oe.map((X,he)=>(we[he]?.[he]??0)===0?0:Math.sqrt(Math.max(0,we[he]?.[he]??0))),Fe=Oe.map((X,he)=>ce.z[he]).map(X=>2*ae(-Math.abs(X))),We=u!==void 0?[u]:Array.from({length:D},(X,he)=>Math.exp(ce.b[F+he])),ze=Array.from({length:F},(X,he)=>L.reduce((Te,Ee)=>Te+Ee[he],0)/L.length),tt=L.map((X,he)=>gr(X,ce.b.slice(0,F))+x[he]),Ue=2*(ce.ll+qe-(Ne.ll+qe)),Xe=F+(u===void 0?D:0)-(1+(u===void 0?D:0)),ge=[...Oe.map((X,he)=>he===0&&c?"(Intercept)":`x${he+(c?0:1)}`),...u===void 0?D===1?["Log(scale)"]:k:[]],oe=[...Oe,...u===void 0?ce.b.slice(F):[]],ve=oe.map((X,he)=>(we[he]?.[he]??0)===0?0:Math.sqrt(Math.max(0,we[he]?.[he]??0))),be=oe.map((X,he)=>ce.z[he]),fe=be.map(X=>2*ae(-Math.abs(X))),nt={coefficients:Oe,coefNames:ge.slice(0,F),scale:We,table:{value:oe,se:ve,z:be,p:fe},strataLevels:k.map(X=>d.find(he=>String(he)===X)),hasOffset:r.offset!==void 0,icoef:u===void 0||v.length===1?Ne.b.slice():[Ne.b[0],Math.log(u)],var:we,loglik:[Ne.ll+qe,ce.ll+qe],iter:ce.iter,df:F+(u===void 0?D:0),idf:1+(u===void 0?D:0),dfResidual:v.length-(F+(u===void 0?D:0)),n:v.length,linearPredictors:tt,means:ze,score:ce.score.slice(),tableRowNames:ge,value:oe,se:ve,z:be,p:fe,chi:Ue,chiDf:Xe,chiP:Rt(Ue,Xe,{lowerTail:!1}),dist:s,distributionDf:i,parms:i,intercept:c,x:L,offset:x,rows:v,weights:y,strata:_,y:N,lower:b,upper:E,status:w,type:o,warnings:[...R,...ce.warn||Ne.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 Zc.set(nt,{V:ce.VScaled,xExponent:ce.xExponent}),J("survreg",nt)}function vN(e,t={}){B(t,sN,"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 g=t.newX===void 0&&t.newOffset===void 0?e.linearPredictors[p]:gr(h,e.coefficients)+o[p],d=e.scale.length===1?e.scale[0]:e.scale[i[p]];return n==="lp"||n==="linear"||n==="link"?g:n==="response"?a?Math.exp(g):g:c.map(v=>{let N=g+d*Bh(v,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 g=t.newX===void 0&&t.newOffset===void 0?e.linearPredictors[p]:gr(h,e.coefficients)+o[p],d=e.scale.length===1?e.scale[0]:e.scale[i[p]],v=Zc.get(e),N=v?.V??e.var,b=E=>{let w=Array(N.length).fill(0);for(let y=0;y<h.length;y++)w[y]=v?Ve(h[y],-v.xExponent[y]):h[y];return N.length>h.length&&(w[h.length+i[p]]=E*d),Math.sqrt(Math.max(0,gr(w,as(N,w))))};return n==="lp"||n==="linear"||n==="link"?b(0):n==="response"?a?$h(g,b(0)):b(0):c.map(E=>{let w=Bh(E,e.dist,e.parms),y=b(w),x=g+d*w;return n==="uquantile"?y:a?$h(x,y):y})}),u=(n==="quantile"||n==="uquantile")&&c.length===1?m.map(h=>h[0]):m;return{fit:f,seFit:u,warnings:[]}}function NN(e,t={}){B(t,aN,"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=Zc.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=Sa(e.lower[m],e.upper[m],e.status[m],u,h,e.dist,r),g=i?e.weights[m]:1,d=f.map((R,T)=>c?Ve(R,-c.xExponent[T]):R),v=Array(s).fill(0);for(let R=0;R<o;R++)v[R]=d[R]*p.e;s>o&&(v[o+e.strata[m]]=p.t);let N=as(a,v),b=N.map((R,T)=>c&&T<o?Ve(R,-c.xExponent[T]):R),E=gr(v,N);if(n==="matrix")return[p.g,p.e,p.ee,p.t,p.tt,p.et].map(R=>R*g);if(n==="working")return-p.e/p.ee*g;if(n==="dfbeta")return b.map(R=>R*g);if(n==="dfbetas")return N.map((R,T)=>R/Math.sqrt(a[T]?.[T]??NaN)*g);if(n==="ldcase")return E*g;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),gr(R,as(a,R))*g}let w=Aa(0,e.dist,r)-Math.log(h),y=e.status[m]===3?gn(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=uN(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?w:0;if(e.status[m]===3){let T=mN(e.lower[m],e.upper[m],h);e.dist==="extreme"||e.dist==="weibull"||e.dist==="exponential"||e.dist==="rayleigh"?R=hN(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*Bt(T/2,e.distributionDf)-1))}return x*Math.sqrt(Math.max(0,2*(R-p.g)))*g}let M=y-u;return(["weibull","exponential","rayleigh","loggaussian","lognormal","loglogistic"].includes(e.dist)?fN(y,u):M)*g});return l}var yN=["n","power","r","type","nfractional","alpha","sidedTest"],MN=["n","power","r","type","nfractional","alpha","sidedTest"],EN=["n","power","r","type","nfractional","alpha","sidedTest"],xN=["n","power","r","type","nfractional","alpha","sidedTest"],RN=["n","power","r","type","nfractional","alpha","sidedTest"],TN=["n","power","r","type","nfractional","alpha","sidedTest"],AN=["sigLevel","alternative","strict","nullOddsRatio","errbound","tsmethod"],SN=["alpha"],kN=["alpha"],qN=["alpha"],IN=["alpha"],_N=["alpha"],LN=["nmax"],Vr=.05,un=e=>ie(e),Hn=e=>ae(e),Pr=e=>Number.isFinite(e),qt=e=>Pr(e)&&e>=0&&e<=1,Mn=e=>Pr(e)&&e>0;function et(e,t,n,r){if(typeof e!="number")throw new TypeError(`'${t}' must be numeric`);if(!n(e))throw new RangeError(r)}function $r(e){et(e,"alpha",qt,"'alpha' must be a probability in [0, 1]")}function CN(e,t){return[e*t/(1+t),e/(1+t)]}function No(e,t,n,r,o,s,i={}){let{n:c,power:a}=i,l=i.r??1,f=i.alpha??Vr,m=i.nfractional??!1,u=e==="equivalence"?ke(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(et(n,"treat",Pr,"'treat' must be finite"),et(r,"control",Pr,"'control' must be finite"),t==="binary"?(et(n,"treat",qt,"'treat' must be a probability in [0, 1]"),et(r,"control",qt,"'control' must be a probability in [0, 1]")):et(o,"sigma",Mn,"'sigma' must be positive and finite"),et(l,"r",Mn,"'r' must be positive and finite"),$r(f),(e!=="equivalence"||u!=="equality")&&et(s,"delta",C=>Pr(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");rn([c,a],"exactly one of 'n' and 'power' must be NULL"),Re(a)||et(a,"power",C=>qt(C)&&C>0&&C<1,"'power' must be strictly between 0 and 1"),c!=null&&et(c,"n",C=>Pr(C)&&C>0,"'n' must be positive and finite");let p=n-r,g=t==="binary"?n*(1-n):o*o,d=t==="binary"?r*(1-r):o*o,v=e==="equivalence"&&u==="equality",N=v?void 0:s,b=e==="noninferiority"?p+N:e==="superiority"?p-N:v?p:N-Math.abs(p);if(e==="noninferiority"&&b<=0)throw new RangeError("Target power is not reachable under the specified hypotheses");let E=e==="superiority"?ie(f/h,{lowerTail:!1}):v?ie(f/2,{lowerTail:!1}):ie(f,{lowerTail:!1}),w=a??.8,y=v||e!=="equivalence"?ie(1-w,{lowerTail:!1}):ie((1-w)/2,{lowerTail:!1}),x,M,R,T,A;if(Re(c))b===0?(R=1/0,T=1/0):(T=(E+y)**2*(g/l+d)/(b*b),R=l*T),m?(x=R,M=T):(M=Math.ceil(T),x=Math.ceil(e==="equivalence"&&t==="continuous"||e==="superiority"?l*M:R)),A=a;else{[R,T]=CN(c,l),x=m?R:Math.ceil(R),M=m?T:Math.ceil(T);let C=Math.sqrt(g/x+d/M);if(e==="equivalence"&&!v)A=Math.max(0,Hn((s-p)/C-un(1-f))+Hn((s+p)/C-un(1-f))-1);else if(v)A=Hn(Math.abs(p)/C-un(1-f/2))+Hn(-Math.abs(p)/C-un(1-f/2));else if(e==="noninferiority")A=Hn((p+s)/C-un(1-f));else{let _=Math.abs(p-s)/C;A=Hn(_-E)+Hn(-_-E)}}return{nTotal:x+M,nTreat:x,nControl:M,nTotalUnrounded:R+T,nTreatUnrounded:R,nControlUnrounded:T,power:A,...N===void 0?{}:{delta:N},warnings:[]}}function FN(e,t,n,r={}){return B(r,yN,"epiSsequb"),J("epiSsequb",No("equivalence","binary",e,t,void 0,n??void 0,r))}function ON(e,t,n,r,o={}){B(o,MN,"epiSsequc");let s=No("equivalence","continuous",e,t,n,r??void 0,o);return J("epiSsequc",s)}function DN(e,t,n,r={}){return B(r,EN,"epiSsninfb"),J("epiSsninfb",No("noninferiority","binary",e,t,void 0,n,r))}function PN(e,t,n,r,o={}){return B(o,xN,"epiSsninfc"),J("epiSsninfc",No("noninferiority","continuous",e,t,n,r,o))}function VN(e,t,n,r={}){return B(r,RN,"epiSssupb"),J("epiSssupb",No("superiority","binary",e,t,void 0,n,r))}function $N(e,t,n,r,o={}){return B(o,TN,"epiSssupc"),J("epiSssupc",No("superiority","continuous",e,t,n,r,o))}function vo(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 vo(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 zN(e,t,n,r={}){B(r,AN,"powerPaired2x2");let o=r.sigLevel??Vr,s=ke(r.alternative,["two.sided","one.sided"]),i=r.strict??!1,c=r.nullOddsRatio??1,a=r.errbound??1e-6,l=ke(r.tsmethod,["central","minlike","blaker"]);if(et(e,"pb",qt,"'pb' must be in [0, 1]"),et(t,"pc",qt,"'pc' must be in [0, 1]"),et(n,"npairs",y=>Number.isSafeInteger(y)&&y>=0&&y<=1e5,"'npairs' must be a nonnegative safe integer"),$r(o),et(c,"nullOddsRatio",Mn,"'nullOddsRatio' must be positive and finite"),et(a,"errbound",y=>Pr(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",g=s==="one.sided",d=vo(n,f),v=0,N=n;if(a>0){let y=0;for(;v<=n&&(y+=d[v])<=a/2;)v++;for(y=0;N>=0&&(y+=d[N])<=a/2;)N--}let b=0;for(let y=v;y<=N;y++){let x=d[y];if(x===0)continue;let M=vo(y,m),R=vo(y,u),T=0,A=0,k=new Array(y+1),C=new Array(y+1);for(let _=0;_<=y;_++)T+=R[_],k[_]=T;for(let _=y;_>=0;_--)A+=R[_],C[_]=A;for(let _=0;_<=y;_++){let I=k[_],F=C[_],L,D=u===.5&&o===1;g?L=D||(h>0?F<=o:I<=o):p?L=D||I<=o/2||F<=o/2:L=h>0?D?_>Math.floor(y/2):F<=o/2:D?_<Math.ceil(y/2):I<=o/2,L&&(b+=x*M[_])}}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":""}`,w="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 J("powerPaired2x2",{power:b,npairs:n,pb:e,pc:t,sigLevel:o,alternative:s,nullOddsRatio:c,note:E,method:w,warnings:[]})}function jN(e,t,n,r,o,s={}){B(s,kN,"powerCT");let i=s.alpha??Vr;$r(i);for(let[l,f]of[[e,"nE"],[t,"nC"]])et(l,f,Mn,`'${f}' must be positive and finite`);for(let[l,f]of[[n,"pE"],[r,"pC"]])et(l,f,qt,`'${f}' must be in [0, 1]`);et(o,"RR",Mn,"'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)-un(1-i/2))}function KN(e,t,n,r,o,s={}){B(s,SN,"ssizeCT");let i=s.alpha??Vr;$r(i),et(e,"power",f=>qt(f)&&f>0&&f<1,"'power' must be strictly between 0 and 1"),et(t,"k",Mn,"'k' must be positive and finite"),et(n,"pE",qt,"'pE' must be in [0, 1]"),et(r,"pC",qt,"'pC' must be in [0, 1]"),et(o,"RR",Mn,"'RR' must be positive and finite");let c=un(1-i/2)+un(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 BN(e,t,n,r,o,s={}){B(s,IN,"powerEpi");let i=s.alpha??Vr;$r(i),et(e,"n",Mn,"'n' must be positive and finite"),et(t,"theta",Mn,"'theta' must be positive and finite"),et(n,"p",qt,"'p' must be in [0, 1]"),et(r,"psi",qt,"'psi' must be in [0, 1]"),et(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)-un(1-i/2))}function WN(e,t,n,r,o,s={}){B(s,qN,"ssizeEpi");let i=s.alpha??Vr;$r(i),et(e,"power",qt,"'power' must be in [0, 1]"),et(t,"theta",Mn,"'theta' must be positive and finite"),et(n,"p",qt,"'p' must be in [0, 1]"),et(r,"psi",qt,"'psi' must be in [0, 1]"),et(o,"rho2",qt,"'rho2' must be in [0, 1]");let c=Math.log(t)**2*n*(1-n)*r*(1-o);return Math.ceil((un(1-i/2)+un(e))**2/c)}function UN(e,t,n,r,o={}){B(o,_N,"numDEpi");let s=o.alpha??Vr;$r(s),et(e,"power",c=>qt(c)&&c>0&&c<1,"'power' must be strictly between 0 and 1"),et(t,"theta",Mn,"'theta' must be positive and finite"),et(n,"p",qt,"'p' must be in [0, 1]"),et(r,"rho2",qt,"'rho2' must be in [0, 1]");let i=Math.log(t)**2*n*(1-n)*(1-r);return Math.ceil((un(1-s/2)+un(e))**2/i)}function Qh(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 HN(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 Zh(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 YN(e,t,n,r,o={}){B(o,LN,"ph2simon");let s=o.nmax??100;for(let[E,w]of[[e,"pu"],[t,"pa"],[n,"ep1"],[r,"ep2"]])et(E,w,qt,`'${w}' must be in [0, 1]`);et(s,"nmax",E=>Number.isInteger(E)&&E>=1&&E<=1e3,"nmax cannot exceed 1000");let i=Array.from({length:s+1},(E,w)=>vo(w,e)),c=Array.from({length:s+1},(E,w)=>vo(w,t)),a=i.map(Qh),l=c.map(Qh),f=i.map(HN),m=[];for(let E=2;E<=s;E++){let w;for(let y=1;y<E&&(w===void 0||y<w.enP0);y++){let x=E-y,M=i[y],R=c[y],T=l[x],A=a[y];for(let k=0;k<y;k++){let C=A[k],_=y+x*(1-C),I=k,F=E-1,L=-1;for(;I<=F;){let D=Math.floor((I+F)/2),[,S]=Zh(k,D,M,R,f[x],T);S<=r?(L=D,I=D+1):F=D-1}if(L>=0){let[D]=Zh(k,L,M,R,f[x],T);D>n&&(L=-1)}L>=0&&(w===void 0||_<w.enP0)&&(w={r1:k,n1:y,r:L,n:E,enP0:_,petP0:C})}}w&&m.push(w)}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,w)=>!m.some((y,x)=>x!==w&&y.n<=E.n&&y.enP0<=E.enP0&&(y.n<E.n||y.enP0<E.enP0))),h=[],p=(E,w)=>(E.enP0-w.enP0)/(w.n-E.n+(E.enP0-w.enP0));for(let E of u)for(h.push(E);h.length>=3;){let w=h[h.length-3],y=h[h.length-2],x=h[h.length-1];if(p(w,y)>p(y,x))break;h.splice(h.length-2,1)}let g=h.map((E,w)=>{let y=w===h.length-1?0:p(E,h[w+1]),x=w===0?1:p(h[w-1],E);return{...E,qLo:y,qHi:x}}),d=g[0],v=g.reduce((E,w)=>w.enP0<E.enP0?w:E),N=g,b=[];return v.n>=s-1&&b.push(` Optimal sample size too close to nmax.
21
21
  Try increasing nmax (current value = ${s})
22
- `),Z("ph2simon",{out:m,minimax:d,optimal:v,admissible:N,pu:e,pa:t,alpha:n,beta:r,nmax:s,warnings:b})}var VN=new Set(["ai","bi","ci","di","m1i","sd1i","n1i","m2i","sd2i","n2i","ri","ni","add","to","drop00"]),Qh=["method","mods","data","intercept","test","level","control"],Zh=["tau2Init","threshold","maxiter","stepadj","tol","tau2Max"],$N=["type","level"],zN=["newmods","predtype","level"],Jh=["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),os=(e,t)=>e.map(n=>xn(n,t)),rp=e=>Array.from({length:e},(t,n)=>Array.from({length:e},(r,o)=>n===o?1:0)),bo=(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},ep=(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?bo(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,L)=>Math.min(F,L),1/0),a=i.map(F=>c/F),l=a.reduce((F,L)=>F+L,0),f=Array.from({length:s},(F,L)=>Array.from({length:s},(O,I)=>n.reduce((D,k,V)=>D+a[V]*k[L]*k[I],0))),m=Array.from({length:s},(F,L)=>n.reduce((O,I,D)=>O+a[D]*I[L]*e[D],0)),u;try{u=nt(f,rp(s))}catch{throw new RangeError("Model matrix is not full rank.")}let h=os(u,m),p=os(n,h),w=e.map((F,L)=>F-p[L]),d=e.every(F=>F===e[0])?0:w.reduce((F,L,O)=>F+a[O]*L*L,0),v=d/c,N=Array.from({length:o},(F,L)=>Array.from({length:o},(O,I)=>(L===I?a[L]:0)-a[L]*xn(n[L],os(u,n[I]).map(D=>D*a[I])))),b=N.reduce((F,L,O)=>F+L[O],0),E=b/c,g=N.reduce((F,L,O)=>F+L.reduce((I,D,k)=>I+D*N[k][O],0),0),y=g/c/c,x=N.map(F=>xn(F,e)),M=.5*(xn(x,x)-c*b),R=.5*(w.reduce((F,L,O)=>F+L*L*a[O]*a[O],0)-c*a.reduce((F,L)=>F+L,0)),T=a.reduce((F,L)=>F+L*L,0),S=.5*T,A=f.length===1?Math.log(f[0][0]):Math.log(Math.abs(jN(f))),C=-.5*(i.reduce((F,L)=>F+Math.log(L),0)+A-s*Math.log(c)+v),_=u.map(F=>F.map(L=>L*c)),q=u.map((F,L)=>Math.sqrt(Math.max(0,F[L]))*Math.sqrt(c));return{beta:h,cov:_,se:q,fitted:p,residuals:w,qe:v,qeScaled:d,weights:a,minVariance:c,P:N,traceP:E,tracePNorm:b,traceP2:y,traceP2Norm:g,score:M,mlScore:R,mlInfo:S,logLik:C,sumWeightsNorm:l,sumWeightSquaredNorm:T}}function jN(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 Zc(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 WN(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*Zc(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*Zc(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*Zc(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 tp(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((v,N)=>v+N*N,0),m=Math.max(0,(f-t.reduce((v,N,b)=>v+l.P[b][b]*N,0))/Math.max(1,c));if(r==="HE")return m;if(r==="SJ"){let v=f/s,N=Gt(e,t,n,v),b=t.map(E=>v/(E+v));return Math.max(0,b.reduce((E,g,y)=>E+g*N.residuals[y]*N.residuals[y],0)/Math.max(1,c))}let u=v=>Gt(e,t,n,v).qe-c;if(r==="EB"){let v=o.tau2Max??Math.max(100,10*f/s);return Math.max(0,bo(u,0,v,1e-14))}if(r==="PM"){let v=o.tau2Max??Math.max(100,10*f/s);for(;u(v)>0&&v<Number.MAX_VALUE/4;)v*=2;let N=o.tol??1e-14;return Math.max(0,bo(u,0,v,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 v=0;v<p;v++){let N=Gt(e,t,n,h),b=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*b/E);if(!Number.isFinite(g))return h;if(Math.abs(g-h)<w)return g;h=g}return h}function np(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 BN(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=np(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=np(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 el(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))-el(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 op(e,t,n,r){let o=Math.max(e,t,n,r);return o===0?0:2**Math.floor(Math.log2(o))}function Jc(e,t,n,r,o){let s=op(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 KN(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(el(t)-.5*Math.log(t)-el(t-.5))}function UN(e,t){B(t,VN,"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],v=s[4][f],N=s[5][f];if(!(w>1&&N>1&&p>=0&&v>=0))throw new RangeError("invalid means, standard deviations, or sample sizes");if(e==="MD")m=h-d,u=p*p/w+v*v/N;else{let b=w+N-2,E=((w-1)*p*p+(N-1)*v*v)/b,g=(h-d)/Math.sqrt(E);m=KN(b)*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],v=h,N=p,b=w,E=d,g=t.add??.5,y=t.to??"only0",x=[v,N,b,E].some(L=>L===0),M=y==="all"||y==="only0"&&x||y==="if0all"&&i;if(e!=="RD"&&M&&(v+=g,N+=g,b+=g,E+=g),e==="RD"&&M&&(v+=g,N+=g,b+=g,E+=g),t.drop00&&(h===0&&w===0||p===0&&d===0)){a.push(NaN),l.push(NaN);continue}let R=op(v,N,b,E),T=v/R,S=N/R,A=b/R,C=E/R,_=T+S,q=A+C,F=1/R;e==="RR"?(m=Math.log1p(Jc(v,N,b,E,"RR")),u=F*(S/(T*_)+C/(A*q))):e==="OR"?(m=Math.log1p(Jc(v,N,b,E,"OR")),u=F*(1/T+1/S+1/A+1/C)):(m=Jc(v,N,b,E,"RD"),u=F*(T*S/(_*_*_)+A*C/(q*q*q)))}a.push(m),l.push(u)}return Z("escalc",{yi:a,vi:l,measure:e,warnings:n})}function sp(e,t,n={}){ct(t)&&B(t,Qh,"rma"),ct(t)&&B(wn(t,"control"),Zh,"rma","control"),B(n,Qh,"rma"),B(wn(n,"control"),Zh,"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=BN(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 v=Gt(o,i,u,0),N=tp(o,i,u,c,s.control??{}),b=Gt(o,i,u,N),E=s.mods===void 0?N:tp(o,i,Array.from({length:p},()=>[1]),c,s.control??{}),g=a==="knha"&&w>0?Math.max(1,b.qe/w):1,y=b.cov.map(K=>K.map(X=>X*g)),x=b.se.map(K=>K*Math.sqrt(g)),M=b.beta.map((K,X)=>K/x[X]),R=a==="z"?ie(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=b.beta.map((K,X)=>K-R*x[X]),A=b.beta.map((K,X)=>K+R*x[X]),C=d?1:0,_=h===1?1:Math.max(1,h-C),q=0,F=1;if(h===1)q=M[0]*M[0],F=a==="z"?xt(q,1,{lowerTail:!1}):Tn(q,1,w,{lowerTail:!1});else{let K=b.beta.slice(C),X=y.slice(C).map(re=>re.slice(C));try{q=xn(K,nt(X,K))}catch{q=NaN}F=a==="z"?xt(q,_,{lowerTail:!1}):Tn(q/_,_,w,{lowerTail:!1})}let L=v.tracePNorm,O=w>0?w*v.minVariance/L:0,I=c==="EE"||c==="FE"?v.qe>0?100*Math.max(0,(v.qe-w)/v.qe):0:N===0?0:100*N/(N+O),D=c==="EE"||c==="FE"?w>0?Math.max(1,v.qe/w):1:N===0?1:(N+O)/O,k=s.mods===void 0||h===1?null:E>0?100*Math.max(0,(E-N)/E):0,V=WN(o,i,u,c,N,b,v);return Z("rma",{beta:{dim:[h,1],rowNames:h===1?m.names[0]??"intrcpt":m.names,colNames:null,rows:b.beta.map(K=>[K])},se:En(x),zval:En(M),pval:En(T),"ci.lb":En(S),"ci.ub":En(A),tau2:N,"se.tau2":V,QE:v.qe,QEp:w===0?1:xt(v.qe,w,{lowerTail:!1}),QM:q,QMp:F,R2:k,p:h,m:_,I2:I,H2:D,k:p,yi:o,vi:i,mods:u,intercept:d,method:c,test:a,level:l,warnings:m.warnings,covariance:y,tau2Null:E})}function HN(e,t={}){B(t,$N,"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),v=On(1-o/2,i),N=On(o/2,i),b=d.qe,E=g=>Gt(e.yi,e.vi,e.mods,g).qe;c=b<=v?0:ep(g=>E(g)-v,Math.max(e.tau2,1e-8)),a=b<=N?NaN:ep(g=>E(g)-N,Math.max(e.tau2,1e-8))}else{let d=On(r/100,1),v=b=>Gt(e.yi,e.vi,e.mods,b).logLik,N=b=>2*(s.logLik-v(b))-d;if(e.tau2>0){c=N(0)<=0?0:bo(N,0,e.tau2);let b=Math.max(e.tau2*2,1e-8),E=N(b);for(let g=0;g<80&&E<0;g++)b*=2,E=N(b);a=E>=0?bo(N,e.tau2,b):NaN}else{let b=1e-8,E=N(b);for(let g=0;g<80&&E<0;g++)b*=2,E=N(b);a=E>=0?bo(N,0,b):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,v)=>({estimate:u[v],"ci.lb":h[v],"ci.ub":p[v]}))},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 YN(e,t={}){B(t,zN,"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,os(e.covariance,u))))),l=e.test==="z"?ie(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: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 GN(e,t,n={}){ct(t)&&B(t,Jh,"regtest"),B(n,Jh,"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,v;if(s==="lm"){let g=m.map((T,S)=>[1,1/T]),y=a.map((T,S)=>T/f[S]),x=XN(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{v=sp(a,l,{method:"REML",mods:m,intercept:!0,test:"z"}),u=v.beta.rows[0][0],h=Array.isArray(v.se)?v.se[0]:v.se;let g=Array.isArray(v.zval)?v.zval[1]:NaN,y=Array.isArray(v.pval)?v.pval[1]:NaN;p=g,w=NaN,d=y}let N=s==="lm"?Wt(1-(1-c/100)/2,w):ie(1-(1-c/100)/2),b=[];s==="lm"&&(!Number.isFinite(p)||!Number.isFinite(d)||!Number.isFinite(h))&&b.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:b};return Z("regtest",o.retFit&&v!==void 0?{...E,fit:v}:E)}function XN(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,rp(2)),o=os(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}}var QN=["type","opposite","twoSided"];function tl(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=hl((t-2)/2,.5,i,c,!0,!1);return Math.min(1,t*a/2)}function ap(e,t){let n=tl(e,t);return Number.isNaN(n)?NaN:1-n}function ZN(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=>ap(r,t))}function JN(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;tl(o,e)>=1?t=o:n=o}return n}function ey(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 JN(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;ap(s,t)<e?n=s:r=s}return n+(r-n)/2}function ty(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=>ey(r,t))}function ny(e,t={}){B(t,QN,"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=fs(r),l=r.indexOf(c),f=Math.abs(a[l]),m=r.slice();m.splice(l,1);let u=Rt([r,m]),h=(o-2)*u.variance[1]/((o-1)*u.variance[0]),p=tl(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,bv as ICC,ww as TukeyHSD,db as adTest,Si as anova,Vm as ansariTest,dw as aov,I1 as aovEz,Ai as aovSummary,jb as auc,mm as bartlettTest,gi as binomCI,Wd as binomDiffCI,t0 as binomDiffEquivalenceTest,Kd as binomRatioCI,wd as binomTest,ev as blandAltman,P0 as boschlooTest,Hg as brunnerMunzelTest,_p as chisqTest,Hb as ciAuc,X1 as clogit,U0 as cochranQTest,jw as coefci,$w as coeftest,Lu as cohenES,u0 as cohensD,uw as confint,HN as confintRma,Bb as coords,cp as corTest,eb as coxZph,Mc as coxph,Tb as cronbachAlpha,Kv as crr,$v as cuminc,gb as cvmTest,Qb as diagnosticTests,wg as dscfAllPairsTest,yg as dunnTest,dg as dunnettTest,RN as epiSsequb,TN as epiSsequc,SN as epiSsninfb,AN as epiSsninfc,kN as epiSssupb,qN as epiSssupc,y0 as epsilonSquared,m1 as esH,u1 as esW1,h1 as esW2,UN as escalc,v0 as etaSquared,Mv as factanal,Co as fisherTest,pm as flignerTest,Oi as friedmanTest,Rp as gTest,vg as gamesHowellTest,Eg as glhtDunnett,Va as glm,Uw as glmAnova,Nw as glmConfint,mp as glmCooksDistance,ja as glmHatvalues,up as glmPredict,lp as glmResiduals,fp as glmRstandard,za as glmSummary,kp as goodmanKruskalGamma,ny as grubbsTest,h0 as hedgesG,Rb as icc,hb as jonckheereTest,Eb as kappa2,xb as kappamFleiss,Ys as kruskalTest,Ym as ksTest,hg as kwAllPairsDunnTest,pg as kwManyOneDunnTest,Ip as lblTest,rb as leveneTest,wb as lillieTest,Wo as lm,qg as mantelhaenTest,gg as manyOneUTest,Zw as mauchlyTest,z0 as mcnemarTest,uv as mcreg,Kr as modelMatrix,dm as moodTest,ON as numDEpi,Xd as oddsRatio,kv as omega,N0 as omegaSquared,Sm as onewayTest,Hr as pAdjust,K0 as pageTest,rw as pairwisePropTest,tw as pairwiseTTest,nw as pairwiseWilcoxTest,Ab as partialR,vb as pearsonTest,ZN as pgrubbs,PN as ph2simon,Ed as poissonTest,Cv as polychor,Ov as polyserial,Np as powerAnovaTest,_N as powerCT,CN as powerEpi,IN as powerPaired2x2,bp as powerPropTest,wp as powerTTest,hw as predict,Uv as predictCrr,YN as predictRma,pi as propTest,ab as propTrendTest,E1 as pwr2p2nTest,N1 as pwr2pTest,T1 as pwrAnovaTest,R1 as pwrChisqTest,S1 as pwrF2Test,M1 as pwrNormTest,y1 as pwrPTest,x1 as pwrRTest,v1 as pwrT2nTest,b1 as pwrTTest,ty as qgrubbs,hm as quadeTest,GN as regtest,sp as rma,$1 as rmst2,$b as roc,Gb as rocTest,ip as setWarningHandler,bb as sfTest,Rg as shapiroTest,LN as ssizeCT,FN as ssizeEpi,Ap as stuartMaxwellTest,F1 as survdiff,mr as survfit,cN as survreg,lN as survregPredict,fN as survregResiduals,k0 as tTOST,yn as tTest,bg as tamhaneT2Test,jv as timepoints,Jd as tost,q0 as tsumTOST,Ng as tukeyTest,fm as varTest,Ow as vcovCL,Rw as vcovHC,Fo as wilcoxTest,bw as zTest};
22
+ `),J("ph2simon",{out:m,minimax:d,optimal:v,admissible:N,pu:e,pa:t,alpha:n,beta:r,nmax:s,warnings:b})}var GN=new Set(["ai","bi","ci","di","m1i","sd1i","n1i","m2i","sd2i","n2i","ri","ni","add","to","drop00"]),Jh=["method","mods","data","intercept","test","level","control"],ep=["tau2Init","threshold","maxiter","stepadj","tol","tau2Max"],XN=["type","level"],QN=["newmods","predtype","level"],tp=["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),is=(e,t)=>e.map(n=>xn(n,t)),sp=e=>Array.from({length:e},(t,n)=>Array.from({length:e},(r,o)=>n===o?1:0)),yo=(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},np=(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?yo(e,0,n):NaN};function Qt(e,t,n,r){let o=e.length,s=n[0].length,i=t.map(F=>F+r),c=i.reduce((F,L)=>Math.min(F,L),1/0),a=i.map(F=>c/F),l=a.reduce((F,L)=>F+L,0),f=Array.from({length:s},(F,L)=>Array.from({length:s},(D,S)=>n.reduce((O,q,V)=>O+a[V]*q[L]*q[S],0))),m=Array.from({length:s},(F,L)=>n.reduce((D,S,O)=>D+a[O]*S[L]*e[O],0)),u;try{u=rt(f,sp(s))}catch{throw new RangeError("Model matrix is not full rank.")}let h=is(u,m),p=is(n,h),g=e.map((F,L)=>F-p[L]),d=e.every(F=>F===e[0])?0:g.reduce((F,L,D)=>F+a[D]*L*L,0),v=d/c,N=Array.from({length:o},(F,L)=>Array.from({length:o},(D,S)=>(L===S?a[L]:0)-a[L]*xn(n[L],is(u,n[S]).map(O=>O*a[S])))),b=N.reduce((F,L,D)=>F+L[D],0),E=b/c,w=N.reduce((F,L,D)=>F+L.reduce((S,O,q)=>S+O*N[q][D],0),0),y=w/c/c,x=N.map(F=>xn(F,e)),M=.5*(xn(x,x)-c*b),R=.5*(g.reduce((F,L,D)=>F+L*L*a[D]*a[D],0)-c*a.reduce((F,L)=>F+L,0)),T=a.reduce((F,L)=>F+L*L,0),A=.5*T,k=f.length===1?Math.log(f[0][0]):Math.log(Math.abs(ZN(f))),C=-.5*(i.reduce((F,L)=>F+Math.log(L),0)+k-s*Math.log(c)+v),_=u.map(F=>F.map(L=>L*c)),I=u.map((F,L)=>Math.sqrt(Math.max(0,F[L]))*Math.sqrt(c));return{beta:h,cov:_,se:I,fitted:p,residuals:g,qe:v,qeScaled:d,weights:a,minVariance:c,P:N,traceP:E,tracePNorm:b,traceP2:y,traceP2Norm:w,score:M,mlScore:R,mlInfo:A,logLik:C,sumWeightsNorm:l,sumWeightSquaredNorm:T}}function ZN(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 Jc(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 JN(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*Jc(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=Qt(e,Array(c).fill(1),n,0);return a>0?Math.sqrt(2*Jc(m.P,l))/a:NaN}if(r==="SJ"){let m=Qt(e,Array(c).fill(1),n,0),u=m.residuals.reduce((p,g)=>p+g*g,0)/c;if(!(u>0)||a<=0)return NaN;let h=Qt(e,t,n,u);return u/a*Math.sqrt(2*Jc(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 rp(e,t,n,r,o){let s=e.length,i=n[0].length,c=s-i,a=Qt(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=Qt(e,Array(s).fill(1),n,0),f=l.residuals.reduce((v,N)=>v+N*N,0),m=Math.max(0,(f-t.reduce((v,N,b)=>v+l.P[b][b]*N,0))/Math.max(1,c));if(r==="HE")return m;if(r==="SJ"){let v=f/s,N=Qt(e,t,n,v),b=t.map(E=>v/(E+v));return Math.max(0,b.reduce((E,w,y)=>E+w*N.residuals[y]*N.residuals[y],0)/Math.max(1,c))}let u=v=>Qt(e,t,n,v).qe-c;if(r==="EB"){let v=o.tau2Max??Math.max(100,10*f/s);return Math.max(0,yo(u,0,v,1e-14))}if(r==="PM"){let v=o.tau2Max??Math.max(100,10*f/s);for(;u(v)>0&&v<Number.MAX_VALUE/4;)v*=2;let N=o.tol??1e-14;return Math.max(0,yo(u,0,v,N))}let h=Math.max(0,o.tau2Init??m),p=o.maxiter??100,g=o.threshold??1e-5,d=o.stepadj??1;if(p<=1)return h;for(let v=0;v<p;v++){let N=Qt(e,t,n,h),b=r==="REML"?N.score:N.mlScore,E=r==="REML"?.5*N.traceP2Norm:N.mlInfo;if(!(E>0)||!Number.isFinite(E))return h;let w=Math.max(0,h+d*b/E);if(!Number.isFinite(w))return h;if(Math.abs(w-h)<g)return w;h=w}return h}function op(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 ey(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=op(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=op(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 tl(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))-tl(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 ap(e,t,n,r){let o=Math.max(e,t,n,r);return o===0?0:2**Math.floor(Math.log2(o))}function el(e,t,n,r,o){let s=ap(e,t,n,r);if(s===0)return NaN;let i=e/s,c=t/s,a=n/s,l=r/s,f=De(jt(i,l),jt(c,a)),m=Ot(i,c),u=Ot(a,l),h=o==="RR"?Ce([a,0],m):o==="OR"?jt(c,a):Ce(m,u),p=ot(f,h);return p[0]+p[1]}function ty(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(tl(t)-.5*Math.log(t)-tl(t-.5))}function ny(e,t){B(t,GN,"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],g=s[2][f],d=s[3][f],v=s[4][f],N=s[5][f];if(!(g>1&&N>1&&p>=0&&v>=0))throw new RangeError("invalid means, standard deviations, or sample sizes");if(e==="MD")m=h-d,u=p*p/g+v*v/N;else{let b=g+N-2,E=((g-1)*p*p+(N-1)*v*v)/b,w=(h-d)/Math.sqrt(E);m=ty(b)*w,u=(g+N)/(g*N)+m*m/(2*(g+N))}}else{let h=s[0][f],p=s[1][f],g=s[2][f],d=s[3][f],v=h,N=p,b=g,E=d,w=t.add??.5,y=t.to??"only0",x=[v,N,b,E].some(L=>L===0),M=y==="all"||y==="only0"&&x||y==="if0all"&&i;if(e!=="RD"&&M&&(v+=w,N+=w,b+=w,E+=w),e==="RD"&&M&&(v+=w,N+=w,b+=w,E+=w),t.drop00&&(h===0&&g===0||p===0&&d===0)){a.push(NaN),l.push(NaN);continue}let R=ap(v,N,b,E),T=v/R,A=N/R,k=b/R,C=E/R,_=T+A,I=k+C,F=1/R;e==="RR"?(m=Math.log1p(el(v,N,b,E,"RR")),u=F*(A/(T*_)+C/(k*I))):e==="OR"?(m=Math.log1p(el(v,N,b,E,"OR")),u=F*(1/T+1/A+1/k+1/C)):(m=el(v,N,b,E,"RD"),u=F*(T*A/(_*_*_)+k*C/(I*I*I)))}a.push(m),l.push(u)}return J("escalc",{yi:a,vi:l,measure:e,warnings:n})}function ip(e,t,n={}){ct(t)&&B(t,Jh,"rma"),ct(t)&&B(Kt(t,"control"),ep,"rma","control"),B(n,Jh,"rma"),B(Kt(n,"control"),ep,"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(W=>!Number.isFinite(W)))throw new Error("NA/NaN/Inf in foreign function call (arg 1)");if(i.some(W=>!Number.isFinite(W)))throw new Error("NA/NaN/Inf in foreign function call (arg 1)");if(s.method==="DL"&&i.some(W=>W<=0))throw new Error("DL estimator cannot be used when there are non-positive sampling variances in the data.");if(i.some(W=>W<0))throw new RangeError("sampling variances must be non-negative");if(i.some(W=>W===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=ey(o.length,s.mods,f,s.data),u=m.x,h=u[0].length,p=o.length,g=p-h,d=m.names[0]==="intrcpt"||!f&&u.every(W=>W[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(Qt(o,i,u,0).qe))throw new Error("NA/NaN/Inf in foreign function call (arg 1)");let v=Qt(o,i,u,0),N=rp(o,i,u,c,s.control??{}),b=Qt(o,i,u,N),E=s.mods===void 0?N:rp(o,i,Array.from({length:p},()=>[1]),c,s.control??{}),w=a==="knha"&&g>0?Math.max(1,b.qe/g):1,y=b.cov.map(W=>W.map(Q=>Q*w)),x=b.se.map(W=>W*Math.sqrt(w)),M=b.beta.map((W,Q)=>W/x[Q]),R=a==="z"?ie(1-(1-l/100)/2):Wt(1-(1-l/100)/2,g),T=M.map(W=>2*(a==="z"?ae(-Math.abs(W)):Bt(-Math.abs(W),g))),A=b.beta.map((W,Q)=>W-R*x[Q]),k=b.beta.map((W,Q)=>W+R*x[Q]),C=d?1:0,_=h===1?1:Math.max(1,h-C),I=0,F=1;if(h===1)I=M[0]*M[0],F=a==="z"?Rt(I,1,{lowerTail:!1}):Tn(I,1,g,{lowerTail:!1});else{let W=b.beta.slice(C),Q=y.slice(C).map(re=>re.slice(C));try{I=xn(W,rt(Q,W))}catch{I=NaN}F=a==="z"?Rt(I,_,{lowerTail:!1}):Tn(I/_,_,g,{lowerTail:!1})}let L=v.tracePNorm,D=g>0?g*v.minVariance/L:0,S=c==="EE"||c==="FE"?v.qe>0?100*Math.max(0,(v.qe-g)/v.qe):0:N===0?0:100*N/(N+D),O=c==="EE"||c==="FE"?g>0?Math.max(1,v.qe/g):1:N===0?1:(N+D)/D,q=s.mods===void 0||h===1?null:E>0?100*Math.max(0,(E-N)/E):0,V=JN(o,i,u,c,N,b,v);return J("rma",{beta:{dim:[h,1],rowNames:h===1?m.names[0]??"intrcpt":m.names,colNames:null,rows:b.beta.map(W=>[W])},se:En(x),zval:En(M),pval:En(T),"ci.lb":En(A),"ci.ub":En(k),tau2:N,"se.tau2":V,QE:v.qe,QEp:g===0?1:Rt(v.qe,g,{lowerTail:!1}),QM:I,QMp:F,R2:q,p:h,m:_,I2:S,H2:O,k:p,yi:o,vi:i,mods:u,intercept:d,method:c,test:a,level:l,warnings:m.warnings,covariance:y,tau2Null:E})}function ry(e,t={}){B(t,XN,"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=Qt(e.yi,e.vi,e.mods,e.tau2),i=e.k-e.mods[0].length,c=0,a=NaN;if(n==="QP"){let d=Qt(e.yi,e.vi,e.mods,0),v=On(1-o/2,i),N=On(o/2,i),b=d.qe,E=w=>Qt(e.yi,e.vi,e.mods,w).qe;c=b<=v?0:np(w=>E(w)-v,Math.max(e.tau2,1e-8)),a=b<=N?NaN:np(w=>E(w)-N,Math.max(e.tau2,1e-8))}else{let d=On(r/100,1),v=b=>Qt(e.yi,e.vi,e.mods,b).logLik,N=b=>2*(s.logLik-v(b))-d;if(e.tau2>0){c=N(0)<=0?0:yo(N,0,e.tau2);let b=Math.max(e.tau2*2,1e-8),E=N(b);for(let w=0;w<80&&E<0;w++)b*=2,E=N(b);a=E>=0?yo(N,e.tau2,b):NaN}else{let b=1e-8,E=N(b);for(let w=0;w<80&&E<0;w++)b*=2,E=N(b);a=E>=0?yo(N,0,b):NaN}}let l=Qt(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),g=["tau^2","tau","I^2(%)","H^2"];return{random:{dim:[4,3],rowNames:g,colNames:["estimate","ci.lb","ci.ub"],rows:g.map((d,v)=>({estimate:u[v],"ci.lb":h[v],"ci.ub":p[v]}))},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 oy(e,t={}){B(t,QN,"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,is(e.covariance,u))))),l=e.test==="z"?ie(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: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 sy(e,t,n={}){ct(t)&&B(t,tp,"regtest"),B(n,tp,"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(w=>Math.sqrt(w)),m=i==="sei"?f:i==="vi"?l:l.map((w,y)=>1/(y+1)),u,h,p,g,d,v;if(s==="lm"){let w=m.map((T,A)=>[1,1/T]),y=a.map((T,A)=>T/f[A]),x=ay(w,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,g=a.length-2,d=2*Bt(-Math.abs(p),g)}else{v=ip(a,l,{method:"REML",mods:m,intercept:!0,test:"z"}),u=v.beta.rows[0][0],h=Array.isArray(v.se)?v.se[0]:v.se;let w=Array.isArray(v.zval)?v.zval[1]:NaN,y=Array.isArray(v.pval)?v.pval[1]:NaN;p=w,g=NaN,d=y}let N=s==="lm"?Wt(1-(1-c/100)/2,g):ie(1-(1-c/100)/2),b=[];s==="lm"&&(!Number.isFinite(p)||!Number.isFinite(d)||!Number.isFinite(h))&&b.push("NaNs produced");let E={model:s,predictor:i,zval:p,pval:d,dfs:g,est:u,"ci.lb":u-N*h,"ci.ub":u+N*h,warnings:b};return J("regtest",o.retFit&&v!==void 0?{...E,fit:v}:E)}function ay(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=rt(n,sp(2)),o=is(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}}var iy=["type","opposite","twoSided"];function nl(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=xt(jt(e,e),t),s=De(r,o);if(s[0]+s[1]<=0)return 0;let i=ot(s,r),c=ot(o,r),a=pl((t-2)/2,.5,i,c,!0,!1);return Math.min(1,t*a/2)}function cp(e,t){let n=nl(e,t);return Number.isNaN(n)?NaN:1-n}function cy(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 Ia(e,r=>cp(r,t))}function ly(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;nl(o,e)>=1?t=o:n=o}return n}function fy(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 ly(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;cp(s,t)<e?n=s:r=s}return n+(r-n)/2}function my(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 Ia(e,r=>fy(r,t))}function uy(e,t={}){B(t,iy,"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=hs(r),l=r.indexOf(c),f=Math.abs(a[l]),m=r.slice();m.splice(l,1);let u=Tt([r,m]),h=(o-2)*u.variance[1]/((o-1)*u.variance[0]),p=nl(f,o),g=1-p,d=Number.isNaN(f)?NaN:Math.min(1,i?2*Math.min(g,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{wc as Anova,Sv as ICC,vw as TukeyHSD,Rb as adTest,ki as anova,$m as ansariTest,ww as aov,J1 as aovEz,qi as aovSummary,Zb as auc,um as bartlettTest,bi as binomCI,Wd as binomDiffCI,r0 as binomDiffEquivalenceTest,Hd as binomRatioCI,vd as binomTest,fv as blandAltman,$0 as boschlooTest,Gg as brunnerMunzelTest,Cp as chisqTest,rv as ciAuc,ab as clogit,Y0 as cochranQTest,f1 as coefci,c1 as coeftest,Du as cohenES,p0 as cohensD,pw as confint,ry as confintRma,ev as coords,fp as corTest,fb as coxZph,no as coxph,Ob as cronbachAlpha,tN as crr,Xv as cuminc,Tb as cvmTest,iv as diagnosticTests,vg as dscfAllPairsTest,Eg as dunnTest,wg as dunnettTest,FN as epiSsequb,ON as epiSsequc,DN as epiSsninfb,PN as epiSsninfc,VN as epiSssupb,$N as epiSssupc,E0 as epsilonSquared,L1 as esH,C1 as esW1,F1 as esW2,ny as escalc,y0 as etaSquared,_v as factanal,Do as fisherTest,dm as flignerTest,Pi as friedmanTest,Ap as gTest,yg as gamesHowellTest,Rg as glhtDunnett,Yr as glm,p1 as glmAnova,Mw as glmConfint,hp as glmCooksDistance,Ba as glmHatvalues,pp as glmPredict,mp as glmResiduals,up as glmRstandard,Ka as glmSummary,Ip as goodmanKruskalGamma,uy as grubbsTest,d0 as hedgesG,Fb as icc,Eb as jonckheereTest,Lb as kappa2,Cb as kappamFleiss,Qs as kruskalTest,Gm as ksTest,dg as kwAllPairsDunnTest,gg as kwManyOneDunnTest,Lp as lblTest,hb as leveneTest,Ab as lillieTest,Uo as lm,_g as mantelhaenTest,bg as manyOneUTest,N1 as mauchlyTest,K0 as mcnemarTest,Mv as mcreg,Hr as modelMatrix,gm as moodTest,UN as numDEpi,Zd as oddsRatio,Vv as omega,M0 as omegaSquared,Sm as onewayTest,Xr as pAdjust,H0 as pageTest,sw as pairwisePropTest,rw as pairwiseTTest,ow as pairwiseWilcoxTest,Pb as partialR,kb as pearsonTest,cy as pgrubbs,YN as ph2simon,Rd as poissonTest,Bv as polychor,Uv as polyserial,Mp as powerAnovaTest,jN as powerCT,BN as powerEpi,zN as powerPaired2x2,Np as powerPropTest,vp as powerTTest,dw as predict,nN as predictCrr,oy as predictRma,gi as propTest,gb as propTrendTest,W1 as pwr2p2nTest,j1 as pwr2pTest,Y1 as pwrAnovaTest,H1 as pwrChisqTest,G1 as pwrF2Test,B1 as pwrNormTest,K1 as pwrPTest,U1 as pwrRTest,z1 as pwrT2nTest,$1 as pwrTTest,my as qgrubbs,pm as quadeTest,sy as regtest,ip as rma,rb as rmst2,Xb as roc,sv as rocTest,lp as setWarningHandler,Sb as sfTest,Ag as shapiroTest,KN as ssizeCT,WN as ssizeEpi,qp as stuartMaxwellTest,Qw as survdiff,fr as survfit,t1 as survfitCoxph,bN as survreg,vN as survregPredict,NN as survregResiduals,$w as svyglm,I0 as tTOST,yn as tTest,Ng as tamhaneT2Test,Zv as timepoints,t0 as tost,_0 as tsumTOST,Mg as tukeyTest,mm as varTest,Dw as vcovCL,uc as vcovHC,Po as wilcoxTest,Nw as zTest};