@numero-uno-jpn/rstats 1.15.0 → 1.17.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 (96) hide show
  1. package/CHANGELOG.md +22 -0
  2. package/README.ja.md +8 -8
  3. package/README.md +8 -8
  4. package/dist/causal/mediate.d.ts +146 -0
  5. package/dist/causal/mediate.js +399 -0
  6. package/dist/causal/mediationProcedures.d.ts +231 -0
  7. package/dist/causal/mediationProcedures.js +240 -0
  8. package/dist/causal/moderation.d.ts +148 -0
  9. package/dist/causal/moderation.js +165 -0
  10. package/dist/causal.js +2 -2
  11. package/dist/chunks/{chunk-FD375XBW.js → chunk-23QCIPET.js} +2 -2
  12. package/dist/chunks/chunk-3UEYA6VG.js +2 -0
  13. package/dist/chunks/{chunk-SMA62635.js → chunk-3Y4DNK4O.js} +2 -2
  14. package/dist/chunks/{chunk-L7HJGM52.js → chunk-4BGGWQWS.js} +2 -2
  15. package/dist/chunks/chunk-5UFVACSV.js +2 -0
  16. package/dist/chunks/{chunk-5BCXHJPF.js → chunk-5W2OZAHQ.js} +2 -2
  17. package/dist/chunks/{chunk-PZRABNMZ.js → chunk-6MLDKHTY.js} +2 -2
  18. package/dist/chunks/{chunk-XVRRYHLM.js → chunk-72I7GDWL.js} +2 -2
  19. package/dist/chunks/chunk-BWDXJL7T.js +2 -0
  20. package/dist/chunks/{chunk-VO3E4Q6H.js → chunk-DD2GG4EC.js} +1 -1
  21. package/dist/chunks/{chunk-CIBAGTO2.js → chunk-EB45CDFA.js} +2 -2
  22. package/dist/chunks/chunk-EIUK6TKF.js +2 -0
  23. package/dist/chunks/chunk-GIIURG5X.js +2 -0
  24. package/dist/chunks/chunk-GLABB425.js +2 -0
  25. package/dist/chunks/{chunk-OO22LZ7Q.js → chunk-GOYTZ5RB.js} +2 -2
  26. package/dist/chunks/{chunk-HF4TT233.js → chunk-GTSK4IN4.js} +1 -1
  27. package/dist/chunks/{chunk-SNQABDAT.js → chunk-I2KJMF4G.js} +1 -1
  28. package/dist/chunks/chunk-JG4TWDQ4.js +2 -0
  29. package/dist/chunks/{chunk-UFJFVNC6.js → chunk-JHABMYNH.js} +1 -1
  30. package/dist/chunks/chunk-JV3POOET.js +2 -0
  31. package/dist/chunks/chunk-K3YSXDTU.js +2 -0
  32. package/dist/chunks/{chunk-R74QJUYU.js → chunk-KY53MB3J.js} +2 -2
  33. package/dist/chunks/{chunk-AGA7JCRF.js → chunk-L77QFL3C.js} +2 -2
  34. package/dist/chunks/chunk-LKKUUREA.js +2 -0
  35. package/dist/chunks/{chunk-WL3JGBLZ.js → chunk-LY2UZGUL.js} +2 -2
  36. package/dist/chunks/chunk-NCDPZSI2.js +2 -0
  37. package/dist/chunks/{chunk-6SZLUVSL.js → chunk-NCRL33PQ.js} +2 -2
  38. package/dist/chunks/{chunk-RHNGF7R7.js → chunk-NPWWGIFG.js} +2 -2
  39. package/dist/chunks/chunk-O4D43LWM.js +2 -0
  40. package/dist/chunks/chunk-PSQ6MLJM.js +2 -0
  41. package/dist/chunks/{chunk-M3HFLMXL.js → chunk-QCPGV7XH.js} +2 -2
  42. package/dist/chunks/{chunk-JAXYZJV7.js → chunk-RM5D7Q7W.js} +2 -2
  43. package/dist/chunks/{chunk-IMSXMJ43.js → chunk-RNGDVSTS.js} +2 -2
  44. package/dist/chunks/{chunk-3K47QLCY.js → chunk-SA2E5GVO.js} +2 -2
  45. package/dist/chunks/{chunk-VHMFOILD.js → chunk-T5ZALLTJ.js} +2 -2
  46. package/dist/chunks/{chunk-GBNGGBU3.js → chunk-TTOJDLIQ.js} +2 -2
  47. package/dist/chunks/{chunk-FV4ZQPRI.js → chunk-V54MBEJM.js} +2 -2
  48. package/dist/chunks/chunk-VZSQAGL2.js +3 -0
  49. package/dist/chunks/{chunk-NULJXDAR.js → chunk-XEJX5HBJ.js} +2 -2
  50. package/dist/chunks/chunk-XPYZOG5H.js +2 -0
  51. package/dist/chunks/{chunk-4EH3O466.js → chunk-XR7Y4ZV6.js} +2 -2
  52. package/dist/chunks/{chunk-JGZSGGMD.js → chunk-YMI33F7W.js} +2 -2
  53. package/dist/chunks/{chunk-MKCPS5QQ.js → chunk-ZRKXHVTJ.js} +2 -2
  54. package/dist/descriptive.js +2 -2
  55. package/dist/distributions.js +2 -2
  56. package/dist/entries/causal.d.ts +3 -0
  57. package/dist/entries/causal.js +3 -0
  58. package/dist/entries/distributions.d.ts +1 -0
  59. package/dist/entries/distributions.js +1 -0
  60. package/dist/entries/regression.d.ts +3 -0
  61. package/dist/entries/regression.js +6 -0
  62. package/dist/linalg.js +2 -2
  63. package/dist/mixed.js +2 -2
  64. package/dist/models/betareg.d.ts +278 -0
  65. package/dist/models/betareg.js +588 -0
  66. package/dist/models/fitOptions.js +6 -4
  67. package/dist/models/loglin.d.ts +186 -0
  68. package/dist/models/loglin.js +349 -0
  69. package/dist/models/survreg.d.ts +43 -0
  70. package/dist/models/survreg.js +6 -3
  71. package/dist/models/tobit.d.ts +132 -0
  72. package/dist/models/tobit.js +172 -0
  73. package/dist/multivariate.js +2 -2
  74. package/dist/numerics.js +2 -2
  75. package/dist/random/mvrnorm.d.ts +60 -0
  76. package/dist/random/mvrnorm.js +237 -0
  77. package/dist/regression.js +3 -3
  78. package/dist/rstats.js +16 -16
  79. package/dist/special.js +2 -2
  80. package/dist/tests.js +15 -15
  81. package/dist/timeseries.js +2 -2
  82. package/package.json +1 -1
  83. package/dist/chunks/chunk-5JKKIVYL.js +0 -3
  84. package/dist/chunks/chunk-5Q5YVM7S.js +0 -2
  85. package/dist/chunks/chunk-AILN2NGE.js +0 -2
  86. package/dist/chunks/chunk-APOF7BLH.js +0 -2
  87. package/dist/chunks/chunk-HSVVCFEN.js +0 -2
  88. package/dist/chunks/chunk-KQGH6SEW.js +0 -2
  89. package/dist/chunks/chunk-OBTCIG5D.js +0 -2
  90. package/dist/chunks/chunk-P4TKSL4W.js +0 -2
  91. package/dist/chunks/chunk-RV4JZHTE.js +0 -2
  92. package/dist/chunks/chunk-U5UOVXKD.js +0 -2
  93. package/dist/chunks/chunk-VHBIWPB4.js +0 -2
  94. package/dist/chunks/chunk-YYLBBEWZ.js +0 -2
  95. package/dist/chunks/chunk-ZDUVFNFH.js +0 -2
  96. package/dist/chunks/chunk-ZUTVNRML.js +0 -2
package/dist/tests.js CHANGED
@@ -1,22 +1,22 @@
1
- /*! rStats.js 1.15.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 rn,b as Kn,c as or,d as Rr,e as Ds,f as Ga}from"./chunks/chunk-M3HFLMXL.js";import{a as yn,c as cn,h as Qr,i as Dl,m as Pl,n as Zr,o as Vl,s as As,t as $l,u as _o,v as Ts,w as Ss,x as ks}from"./chunks/chunk-VHMFOILD.js";import{b as Ul}from"./chunks/chunk-OO22LZ7Q.js";import{a as ni,e as ri}from"./chunks/chunk-AGA7JCRF.js";import{a as Is,b as rr,c as Oo,d as Gl,e as Xl,f as _s,g as Je,h as fn,i as Mn,j as Lt,k as ef,l as Kp,n as Vs,o as Do}from"./chunks/chunk-ZUTVNRML.js";import{a as $s}from"./chunks/chunk-GBNGGBU3.js";import"./chunks/chunk-FV4ZQPRI.js";import"./chunks/chunk-MKCPS5QQ.js";import{a as Ol,d as Rs}from"./chunks/chunk-5Q5YVM7S.js";import{b as Ut,c as hf}from"./chunks/chunk-SMA62635.js";import{d as rt}from"./chunks/chunk-APOF7BLH.js";import{a as Ps}from"./chunks/chunk-OBTCIG5D.js";import{a as Fo,b as Xa,c as Qa,d as cf,e as lf,f as ff,g as Ar,h as sr,i as Bn,j as Ja,k as ar,l as mf,m as ei,n as Wp,o as ti,p as uf,q as Bp,r as Up,s as Hp}from"./chunks/chunk-P4TKSL4W.js";import{b as Nt,c as Pn}from"./chunks/chunk-5BCXHJPF.js";import{a as Ql,c as Zl,d as Jl,e as xr,g as Po}from"./chunks/chunk-5JKKIVYL.js";import{b as Ha}from"./chunks/chunk-VO3E4Q6H.js";import{a as ct,b as Ls,c as Cs}from"./chunks/chunk-PZRABNMZ.js";import{a as of,b as sf,c as Fs,d as Za}from"./chunks/chunk-CIBAGTO2.js";import{a as Wn,b as to}from"./chunks/chunk-4EH3O466.js";import{d as pt,e as pn,g as dn,h as Zn,j as Jn}from"./chunks/chunk-3K47QLCY.js";import{d as bs,e as vs,g as Ns,h as Xr,m as gn,n as Vn}from"./chunks/chunk-L7HJGM52.js";import{a as af}from"./chunks/chunk-KQGH6SEW.js";import{c as tf,h as nf,j as Ct,m as Ya,n as rf,o as Os,p as eo}from"./chunks/chunk-YYLBBEWZ.js";import{a as Sn,b as Yl,g as Jr}from"./chunks/chunk-R74QJUYU.js";import"./chunks/chunk-VHBIWPB4.js";import{a as Ua,b as wt,c as bt,d as Fl,e as Wt,f as Bt}from"./chunks/chunk-HSVVCFEN.js";import{i as vt,l as ws,t as _l}from"./chunks/chunk-FD375XBW.js";import{a as kn,c as qs}from"./chunks/chunk-JAXYZJV7.js";import{b as jl,c as zl,d as Kl,e as Wl,f as Bl,i as Lo,l as zp,n as J,o as Ye,p as mt}from"./chunks/chunk-6SZLUVSL.js";import{a as Co,b as zn,c as Hl,d as Vt,e as Mt}from"./chunks/chunk-UFJFVNC6.js";import{a as Et,b as bn,c as ln}from"./chunks/chunk-SNQABDAT.js";import"./chunks/chunk-XVRRYHLM.js";import"./chunks/chunk-RV4JZHTE.js";import{b as kl,c as ql}from"./chunks/chunk-ZDUVFNFH.js";import{h as Al,i as Tl,k as Sl,t as Il}from"./chunks/chunk-WL3JGBLZ.js";import{f as za,r as Jt,s as ae,t as ie}from"./chunks/chunk-NULJXDAR.js";import{A as $t,B as xs,C as Ie,D as Ba,c as Ka,d as Io,e as $n,f as wn,g as jn,h as $,i as Ve,j as er,k as it,l as at,m as tr,n as Ll,p as ys,q as Tt,r as Ms,s as Cl,t as Es,u as Wa,v as Pe,w as yt,x as Nn,y as Pt,z as nr}from"./chunks/chunk-JGZSGGMD.js";import{b as Dt,c as Kt,d as pe,e as xl,f as De,g as Ce,h as At,i as ot,j as ft,k as Rl,l as K,m as _t}from"./chunks/chunk-HF4TT233.js";var pf=["alternative","mu","paired","varEqual","confLevel"];function En(e,t,n={}){if(typeof e!="string"&&ct(t)&&K(t,pf,"tTest"),K(n,pf,"tTest"),typeof e=="string")return Yp(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,a=n.paired??!1,c=n.varEqual??!1,i=n.confLevel??.95;if(Number.isNaN(s))throw new RangeError("mu must be a number");if(fn(i),a&&r===void 0)throw new TypeError("y is required when paired is true");let l,f,m;if(a){let[q,C]=nr(Array.from(e),Array.from(r));({d:l,k:m}=Ms(q,C))}else{let q=Ie(r===void 0?[Pt(Array.from(e))]:[Pt(Array.from(e)),Pt(Array.from(r))]);m=q.k,l=q.groups[0],f=q.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=a?"Paired t-test":"One Sample t-test",d=y,v=a?"mean difference":"mean of x",b=a?"mean difference":"mean"}else{let q=f.length;if(q<1||!c&&q<2)throw new Error("not enough 'y' observations");if(c&&E+q<3)throw new Error("not enough observations");let C=it(f),_=q>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+q-2;let I=0;E>1&&(I+=(E-1)*x),q>1&&(I+=(q-1)*_),I/=p,g=Math.sqrt(I*(1/E+1/q))}else{let I=x/E,O=_/q;g=Math.sqrt(Math.sqrt(I)**2+Math.sqrt(O)**2),p=Es([I,O],[E-1,q-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=(q,C)=>{let _=u+(h+C*q)*g;return Number.isFinite(_)&&Math.max(Math.abs(u),Math.abs(h*g))<=16*(Math.abs(M)+q*g)?_:M+C*q*g},A,T;if(o==="less")A=wt(h,p,!0,!1),T=[-1/0,R(bt(i,p,!0,!1),1)];else if(o==="greater")A=wt(h,p,!1,!1),T=[R(bt(i,p,!0,!1),-1),1/0];else{A=2*wt(-Math.abs(h),p,!0,!1);let q=1-i,C=q/2<1e-8?bt(q/2,p,!1,!1):bt(1-q/2,p,!0,!1);T=[R(C,-1),R(C,1)]}return T=[$(T[0],m),$(T[1],m)],d=typeof d=="number"?$(d,m):d.map(q=>$(q,m)),g=$(g,m),{method:N,alternative:o,statistic:h,statisticName:"t",parameter:p,parameterName:"df",pValue:A,confInt:T,confLevel:i,estimate:d,estimateNames:v,nullValue:s,nullValueName:b,stdErr:g,stdError:g,warnings:[]}}function Yp(e,t,n){if(n.paired!==void 0)throw new Error("cannot use 'paired' in formula method");let r=Is(e,t,!0);if(r.kind==="one")return En(r.x,n);if(r.kind==="paired")return En(r.x,r.y,{...n,paired:!0});let[o,s]=r.levels;return{...En(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"]),Xp=["n","p1","p2","sigLevel","power","alternative","strict","tol"],Qp=["groups","n","betweenVar","withinVar","sigLevel","power"],gf=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 mn(e,t,n){if(!Re(e)&&!t(e))throw new RangeError(n)}var Vo=e=>e>=0&&e<=1,js=e=>e>0&&e<1/0;function on(e,t){if(e.filter(Re).length!==1)throw new RangeError(t)}function Ge(e,t,n,r={}){return Jl(e,t,n,r).root}var Tr=(e,t,n,r)=>Wt(e,t,{ncp:n,lowerTail:r});function oi(e,t){if(e!=null&&!(typeof e=="number"&&e>=0&&e<=1))throw new RangeError(t)}var si="'sig.level' must be numeric in [0, 1]";function Zp(e={}){K(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;on([t,n,o,r,s],"exactly one of 'n', 'delta', 'sd', 'power', and 'sig.level' must be NULL"),oi(s,si),mn(r,Vo,"'power' must be a probability"),mn(o,js,"'sd' must be a positive finite number"),mn(t,g=>g>=2,"number of observations in each group must be at least 2"),mn(n,g=>!Number.isNaN(g),"'delta' must not be NaN"),mn(e.tol,js,"'tol' must be a positive finite number");let a=ke(e.type,["two.sample","one.sample","paired"]),c=ke(e.alternative,["two.sided","one.sided"]),i=e.strict??!1,l=e.tol??gf,f=a==="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=Bt(N/m,b,{lowerTail:!1}),w=Math.sqrt(g/f)*d/v;Number.isFinite(w)||(w=Math.sqrt(g/f)*(d/v));let y=Tr(E,b,w,!1);return i&&m===2?y+Tr(-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=Ge(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=Ge(v=>u(t,n,v,s)-g,d*1e-7,d*1e7,{tol:l,extendInt:n>0?"downX":"upX"})}else Re(n)?n=Ge(d=>u(t,d,o,s)-g,o*1e-7,o*1e7,{tol:l,extendInt:"upX"}):s=Ge(d=>u(t,n,o,d)-g,1e-10,1-1e-10,{tol:l,extendInt:"yes"})}let h=a==="paired"?"n is number of *pairs*, sd is std.dev. of *differences* within pairs":a==="two.sample"?"n is number in *each* group":void 0,p={"two.sample":"Two-sample","one.sample":"One-sample",paired:"Paired"}[a]+" 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 Jp(e={}){K(e,Xp,"powerPropTest");let{n:t,p1:n,p2:r,power:o}=e,s=e.sigLevel===void 0?.05:e.sigLevel;on([t,n,r,o,s],"exactly one of 'n', 'p1', 'p2', 'power', and 'sig.level' must be NULL"),oi(s,si),mn(o,Vo,"'power' must be a probability"),mn(n,Vo,"'p1' must be a probability"),mn(r,Vo,"'p2' must be a probability"),mn(t,u=>u>0,"number of observations in each group must be positive"),mn(e.tol,js,"'tol' must be a positive finite number");let a=ke(e.alternative,["two.sided","one.sided"]),c=e.strict??!1,i=e.tol??gf,l=a==="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=Ge(h=>f(h,n,r,s)-u,1,1e7,{tol:i,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=Ge(h=>f(t,h,r,s)-u,0,r,{tol:i,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=Ge(h=>f(t,n,h,s)-u,n,1,{tol:i,extendInt:"yes"}),r>1&&m.push("No p2 in [p1, 1] can be found to achieve the desired power")}else s=Ge(h=>f(t,n,r,h)-u,1e-10,1-1e-10,{tol:i,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:a,note:"n is number in *each* group",method:"Two-sample comparison of proportions power calculation",warnings:m})}var df=1e16,zs=(e,t)=>e>df&&e<1/0&&t>df;function Ks(e,t,n,r){let o=ie(r,{lowerTail:!1}),s=n/e,a=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*a-s)/c,{lowerTail:!1})}function ed(e,t,n,r){if(n===0)return 0;let o=l=>Math.floor(Math.log2(l)),[s,a,c,i]=[o(e),o(t),o(n),o(r)];return $($(e,-s)*$(t,-a)*$(n,-c)/$(r,-i),s+a+c-i)}function td(e={}){K(e,Qp,"powerAnovaTest");let{groups:t,n,betweenVar:r,withinVar:o,power:s}=e,a=e.sigLevel===void 0?.05:e.sigLevel;if(on([t,n,r,o,s,a],"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");oi(a,si),mn(s,Vo,"'power' must be a probability"),mn(r,i=>i>=0&&i<1/0,"'betweenVar' must be a nonnegative finite number"),mn(o,js,"'withinVar' must be a positive finite number");let c=(i,l,f,m,u)=>{let h=(i-1)*l*f/m;h<1/0||(h=(i-1)*l*(f/m)),!(h<1/0)&&Number.isFinite(i)&&Number.isFinite(l)&&(h=ed(i-1,l,f,m));let p=i-1,g=(l-1)*i;return h===1/0?1:zs(p,g)?Ks(p,g,h,u):dn(Zn(u,p,g,{lowerTail:!1}),p,g,{ncp:h,lowerTail:!1})};if(Re(s))s=c(t,n,r,o,a);else{let i=s;if(Re(t))t=Ge(l=>c(l,n,r,o,a)-i,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=Ge(l=>c(t,l,r,o,a)-i,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=Ge(l=>c(t,n,r,l,a)-i,r*1e-7,r*1e7)}else Re(r)?r=Ge(l=>c(t,n,l,o,a)-i,o*1e-7,o*1e7):a=Ge(l=>c(t,n,r,o,l)-i,1e-10,1-1e-10)}return{groups:t,n,betweenVar:r,withinVar:o,sigLevel:a,power:s,note:"n is number in each group",method:"Balanced one-way analysis of variance power calculation",warnings:[]}}var nd=["correct","p","rescaleP"],rd=["confLevel"],od=["alternative","rowScores","colScores"];function en(e){let t=[0,0];for(let n of e)t=pe(t,[n,0]);return t}function ii(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 ci=e=>e.length>0&&typeof e[0]!="number",li=(e,t)=>Number.isNaN(e)?NaN:vt(e,t/2,2,!1,!1);function wf(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 a=(e-t)*((e+t)/o)+e/o;return(.5*(e-t)+.5*a)/((n+r+2*Math.sqrt(t+.25))/4)}function $o(e,t){return e.length>0&&Array.isArray(e[0])?e.map((n,r)=>n.map((o,s)=>wf(o,t[r][s]))):e.map((n,r)=>wf(n,t[r]))}var sd=["none","williams","yates"],vf="Log likelihood ratio (G-test)";function ad(e,t={}){K(t,nd,"gTest");let n=ke(t.correct,sd);if(typeof e!="object"||e===null||typeof e.length!="number")throw new TypeError("'x' must be an array of counts or of rows");if(ci(e)){let o=ii(e);return bf(o.flat()),o.length===1?J("gTest",ai(o[0],n,t)):o[0].length===1?J("gTest",ai(o.map(s=>s[0]),n,t)):J("gTest",id(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 bf(r),J("gTest",ai(r,n,t))}function bf(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 Nf(e,t){return e<1/0?0:Math.ceil(Math.log2(t))+1}function yf(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,Tl(o,s))}return n[0]+n[1]}function id(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=en(e.flat()),s=Nf(o[0],n*r),a=s===0?e:e.map(b=>b.map(E=>$(E,-s))),c=s===0?o:en(a.flat()),i=a.map(b=>en(b)),l=a[0].map((b,E)=>en(a.map(w=>w[E]))),f=i.map(b=>l.map(E=>Ce(b,ot(E,c)))),m=a;if(t==="yates"){let[[b,E],[w,y]]=a,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=yf(m.flat(),f.flat()),h=Number.isNaN(u)?["NaNs produced"]:[],p=1;if(t==="williams"){if(i.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=i.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:`${vf} 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:li(g,d),observed:s===0?m:m.map(b=>b.map(E=>$(E,s))),expected:v,ftResiduals:$o(e,v),warnings:h}}function ai(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 a=en(s);if(o){if(!(a[0]>0&&a[0]<1/0))throw new RangeError("the probabilities to rescale must have a positive finite sum")}else if(Math.abs(a[0]-1+a[1])>Math.sqrt(Number.EPSILON))throw new RangeError("probabilities must sum to 1");let c=en(e),i=Nf(c[0],r),l=i===0?e:e.map(b=>$(b,-i)),f=i===0?c:en(l),m=s.map(b=>o?ot(At(f,b),a):At(f,b)),u=m.map(b=>b[0]+b[1]),h=l;if(t==="yates"){let b=$(.5,-i),E=l[0]-u[0];E>$(.25,-i)?h=[l[0]-b,l[1]+b]:-E>$(.25,-i)&&(h=[l[0]+b,l[1]-b])}let p=yf(h,m);if(!o&&!Number.isNaN(p)){let b=f[0]+f[1];p-=b*(a[0]-1+a[1])}let g=Number.isNaN(p)?["NaNs produced"]:[],d=t==="williams"?1+(r+1)/(6*$(f[0]+f[1],i)):1,v=$(2*p,i)/d,N=u.map(b=>$(b,i));return{method:`${vf} goodness of fit test`,statistic:v,statisticName:"G",parameter:r-1,parameterName:"X-squared df",pValue:li(v,r-1),observed:i===0?h:h.map(b=>$(b,i)),expected:N,ftResiduals:$o(e,N),warnings:g}}function cd(e){if(typeof e!="object"||e===null||!ci(e))throw new RangeError("'x' must be square with at least two rows and columns");let t=ii(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 ld(e){let t=cd(e),n=en(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,a=o.map(w=>en(w)),c=o.map((w,y)=>en(o.map(x=>x[y]))),i=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=i.length;if(l<2)throw new RangeError("Too many equal marginals, cannot compute");let f=i.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[i[w]][i[y]]+o[i[y]][i[w]]>0&&(f[m(w)]=m(y));if(i.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=i.slice(0,u).map(w=>{let y=pe(a[w],[-c[w][0],-c[w][1]]);return y[0]+y[1]}),p=i.slice(0,u).map(w=>i.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=en(i.flatMap(w=>i.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:li(v,E),N:b[0]+b[1],warnings:[]})}function fd(e,t={}){K(t,rd,"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=Mf(e),o=en(r.flat())[0],s=0;(o>2**250||o>0&&o<2**-250)&&(s=2*Math.round((Math.log2(o)-250)/2));let a=s===0?r:r.map(w=>w.map(y=>$(y,-s))),{pc:c,pd:i}=md(a),l=[0,0],f=[0,0];a.forEach((w,y)=>w.forEach((x,M)=>{l=pe(l,At(c[y][M],x)),f=pe(f,At(i[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=a.map((w,y)=>w.map((x,M)=>2*ot(De(Ce(f,c[y][M]),Ce(l,i[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;a.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 Mf(e){if(typeof e!="object"||e===null||!ci(e))throw new TypeError("'table' must be an array of rows");let t=ii(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 md(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}),a=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},i=a(o),l=c(s),f=a(s),m=c(o),u=i.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 ud(e,t={}){K(t,od,"lblTest");let n=Je(t.alternative),r=Mf(e),o=r.length,s=r[0].length,a=(R,A,T)=>{if(R===void 0)return Array.from({length:A},(C,_)=>_+1);let q=Array.from(R);if(q.length!==A)throw new RangeError(`'${T}' must have one score for each ${T==="rowScores"?"row":"column"}`);if(q.some(C=>typeof C!="number"||!Number.isFinite(C)))throw new RangeError(`'${T}' must be finite numbers`);return q},c=a(t.rowScores,o,"rowScores"),i=a(t.colScores,s,"colScores"),f=en(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(A=>$(A,-f))),u=m.map(R=>en(R)).map(R=>R[0]+R[1]),h=m[0].map((R,A)=>en(m.map(T=>T[A]))).map(R=>R[0]+R[1]),p=u.reduce((R,A)=>R+A,0);if(!(f>0||p>1))throw new RangeError("the table must have a total above 1");let g=u.reduce((R,A,T)=>R+A*c[T],0)/p,d=h.reduce((R,A,T)=>R+A*i[T],0)/p,v=c.map(R=>R-g),N=i.map(R=>R-d),b=u.reduce((R,A,T)=>R+A*v[T]*v[T],0),E=h.reduce((R,A,T)=>R+A*N[T]*N[T],0),w=0;if(m.forEach((R,A)=>R.forEach((T,q)=>w+=T*v[A]*N[q])),!(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 Ef=["correct","p","rescaleP","simulatePValue","B","nMinus1"];function vn(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 hd(e,t,n){return ct(t)&&K(t,Ef,"chisqTest"),K(n,Ef,"chisqTest"),Array.isArray(t)?J("chisqTest",pd(e,t,n??{})):J("chisqTest",dd(e,t??{}))}var Af="Chi-squared approximation may be incorrect";function pd(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 a=[...new Set(o)].sort(kn),c=[...new Set(s)].sort(kn);if(a.length<2||c.length<2)throw new RangeError("'x' and 'y' must have at least 2 levels");let i=new Map(a.map((u,h)=>[u,h])),l=new Map(c.map((u,h)=>[u,h])),f=a.map(()=>c.map(()=>0));return o.forEach((u,h)=>f[i.get(u)][l.get(s[h])]++),{...If(f,n),rowLevels:a,colLevels:c}}function dd(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?fi(r[0],t):o===1?fi(r.map(s=>s[0]),t):If(r,t)}return fi(Array.from(e),t)}function Tf(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 Sf(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 kf(e,t){return Number.isNaN(e)?NaN:vt(e,t/2,2,!1,!1)}function qf(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 fi(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=vn(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=vn(o),d=g<1/0?0:Math.ceil(Math.log2(n)),v=d===0?o:o.map(b=>$(b,-d)),N=d===0?g:vn(v);o=v.map(b=>b/N)}else if(Math.abs(vn(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),a=s===0?e:e.map(g=>$(g,-s)),c=s===0?r:vn(a),i=o.map(g=>c*g),l=s===0?i:i.map(g=>$(g,s));qf(l);let f=o.map((g,d)=>i[d]*(1-g)),m=$(vn(a.map((g,d)=>$t(g-i[d],i[d]))),s),u="Chi-squared test for given probabilities",h=n-1,p;if(t.simulatePValue??!1){let g=Tf(t);if(!Number.isInteger(r))throw new RangeError("the simulation needs counts summing to an integer");u+=Co(g),h=NaN,p=Sf(gd(r,o,l,g),m,g)}else p=kf(m,h);return{method:u,statistic:m,statisticName:"X-squared",parameter:h,parameterName:"df",pValue:p,observed:e,expected:l,residuals:a.map((g,d)=>$((g-i[d])/Math.sqrt(i[d]),s/2)),stdres:a.map((g,d)=>$((g-i[d])/Math.sqrt(f[d]),s/2)),ftResiduals:$o(e,l),warnings:Number.isFinite(h)&&l.some(g=>g<5)?[Af]:[]}}function gd(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=Ul(o,t);if(r*e===0)return new Float64Array(0);let a=new Float64Array(r),c=new Float64Array(o);for(let i=0;i<r;i++){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];a[i]=l}return a}function wd(e,t,n,r){let o=e.length,s=t.length,a=e.reduce((f,m)=>f+m,0);if(a>Ts)throw new RangeError("the simulation needs a total count of at most 16777216 (2^24)");let c=Ss(a),i=new Int32Array(o*s),l=new Float64Array(Math.trunc(r));for(let f=0;f<l.length;f++){ks(e,t,c,i);let m=0;for(let u=0;u<s;u++)for(let h=0;h<o;h++){let p=n[h][u];m+=(i[h+u*o]-p)**2/p}l[f]=m}return l}function If(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,a=e.flat();if(a.some(b=>!(b>=0&&b<1/0)))throw new RangeError("all entries of 'x' must be non-negative and finite");let c=vn(a);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 i=e.map(b=>vn(b)),l=e[0].map((b,E)=>vn(e.map(w=>w[E]))),f=i.map(b=>l.map(E=>xs(b,E,c)));qf(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)&&i.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=Tf(t);u+=Co(b),h=vn(m.flatMap((w,y)=>w.map((x,M)=>$t(x,f[y][M])))),p=NaN;let E=wd(i.map(w=>Math.trunc(w)),l.map(w=>Math.trunc(w)),f,b);g=Sf(E,h,b)}else if(v>0)u+=" with Yates' continuity correction",h=vn(m.flatMap((b,E)=>b.map((w,y)=>$t(Math.abs(w)-v,f[E][y]))));else if(h=vn(m.flatMap((b,E)=>b.map((w,y)=>$t(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??kf(h,p),observed:e,expected:f,residuals:m.map((b,E)=>b.map((w,y)=>w/Math.sqrt(f[E][y]))),stdres:bd(e,c,i,l,f,m),ftResiduals:$o(e,f),warnings:Number.isFinite(p)&&f.some(b=>b.some(E=>E<5))?[...N,Af]:N}}function bd(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 a=e.map(p=>xf(p)),c=e[0].map((p,g)=>xf(e.map(d=>d[g]))),i=Nd(a),l=Rf(a),f=Rf(c),m=l.map(p=>ot(p,i)),u=c.map(p=>ot(p,i)),h=f.map(p=>ot(p,i));return e.map((p,g)=>p.map((d,v)=>{let N=Ce(a[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>=vd||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 vd=2**-1022;function xf(e){let t=[0,0];for(let n of e)t=pe(t,[n,0]);return t}function Nd(e){let t=[0,0];for(let n of e)t=pe(t,n);return t}function Rf(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 yd=Math.pow(Number.EPSILON,.25);function Ht(e,t,n,r={}){let o={tol:r.tol??yd,maxiter:r.maxIter??1e3};return r.fLower!==void 0&&(o.fLower=r.fLower),r.fUpper!==void 0&&(o.fUpper=r.fUpper),xr(e,t,n,o).root}function ir(e,t){let n=i=>e(Math.exp(i)),r=0,o=n(0),s=t?-1:1,a=n(s);for(;a!==0&&Math.sign(a)===Math.sign(o);){if(Math.abs(s)>745)return t?0:1/0;r=s,o=a,s*=2,a=n(s)}if(a===0)return Math.exp(s);let c=r<s?Ht(n,r,s,{tol:2**-50,fLower:o,fUpper:a}):Ht(n,s,r,{tol:2**-50,fLower:a,fUpper:o});return Math.exp(c)}var Md=["alternative","confInt","confLevel","or","simulatePValue","B","midp"];function Ko(e,t={}){K(t,Md,"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(ql));if(s.flat().some(f=>f>2147483647))throw new RangeError("'x' has entries too large to be integer");let a=[],c=Ed(o,s);c!==null&&a.push(`'x' has been rounded to integer: ${c}`);let i="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",{...Td(s,t,i),warnings:a});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:i+Co(f),alternative:l,pValue:xd(s,f),warnings:a})}return J("fisherTest",{method:i,alternative:l,pValue:Sd(s),warnings:a})}function Ed(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 a=0;for(let l of r)a+=Math.abs(l)/s;let c="relative";Number.isFinite(a)&&a>n||(a=1,c="absolute");let i=0;for(let l=0;l<s;l++)i+=Math.abs(r[l]-o[l])/(s*a);return Number.isNaN(i)||i>n?`Mean ${c} difference: ${Hl(i)}`:null}function xd(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)),a=o[0].map((d,v)=>o.reduce((N,b)=>N+b[v],0)),c=s.length,i=a.length,l=[0,0];for(let d of o.flat())l=pe(l,[-Rs(d),0]);let f=l[0]/(1+64*Number.EPSILON),m=s.reduce((d,v)=>d+v,0);if(m>Ts)throw new RangeError("the simulation needs a total count of at most 16777216 (2^24)");let u=Ss(m),h=new Int32Array(c*i),p=0,g=Math.trunc(t);for(let d=0;d<g;d++){ks(s,a,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 _f=1+1e-7,Rd=16,Ad=2**24;function jo(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 no=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 a=this.lo;a<=this.hi;a++){let c=a-this.lo;c%Rd===0||!(s>0&&s<1/0)?(o=yn(a,t,n,r,!0),s=1,this.logCentral[c]=o):this.logCentral[c]=o+Math.log(s),s*=(t-a)*(r-a)/((a+1)*(n-r+a+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 i=this.logCentral[c]+(this.lo+c)*r;o[c]=i,i>s&&(s=i)}for(let c=0;c<n;c++){let i=o[c]-s;o[c]=i<-746?0:Math.exp(i)}let a=jo(o,0,n-1);for(let c=0;c<n;c++)o[c]!==0&&(o[c]=o[c]/a);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?cn(t-1,this.m,this.n,this.k,!1,!1):cn(t,this.m,this.n,this.k,!0,!1);if(n===0||n===1/0){let a=n===0?this.lo:this.hi;return(r?t<=a:t>=a)?1:0}let o=this.density(n),s=t-this.lo;return r?jo(o,s,o.length-1):jo(o,0,s)}midTail(t,n,r){if(n===1){let a=.5*yn(t,this.m,this.n,this.k,!1);return(r?cn(t,this.m,this.n,this.k,!1,!1):cn(t-1,this.m,this.n,this.k,!0,!1))+a}if(n===0||n===1/0){let a=n===0?this.lo:this.hi;return t===a?.5:(r?t<a:t>a)?1:0}let o=this.density(n),s=t-this.lo;return(r?jo(o,s+1,o.length-1):jo(o,0,s-1))+.5*o[s]}};function ui(e,t,n){let r=e.density(n),o=r[t-e.lo]*_f,s=[0,0];for(let a of r)a<=o&&(s=pe(s,[a,0]));return s[0]}function Lf(e,t,n,r){let o=Math.max(0,r-n),s=Math.min(r,t),a=g=>yn(g,t,n,r,!0),c=a(e)+Math.log(_f),i=Math.min(s,Math.max(o,Math.floor((r+1)*(t+1)/(t+n+2))));for(;i<s&&a(i+1)>a(i);)i++;for(;i>o&&a(i-1)>a(i);)i--;let l=(g,d)=>{let v=g,N=d+1;for(;N-v>1;){let b=v+Math.floor((N-v)/2);a(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);a(b)<=c?N=b:v=b}return N},m,u;e<=i?(m=l(e,i),u=f(i+1,s)):(u=f(i,e),m=i-1<o||a(o)>c?o-1:l(o,i-1));let h=m<o?0:cn(m,t,n,r,!0,!1),p=u>s?0:cn(u-1,t,n,r,!1,!1);return h+p}function zo(e,t){return ir(e,t)}function Td(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[[a,c],[i,l]]=e,f=a,m=Math.min(a,l)+Math.min(c,i)+1;if(m>Ad)throw new RangeError(`the exact distribution of the 2 x 2 table has ${m} values, more than 2^24`);let u=new no(a+i,c+l,a+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=ui(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:zo(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:zo(R=>u.midTail(f,R,!1)-y,M<y)}let x=u.tail(f,1,!1);return x===y?1:zo(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:zo(R=>u.midTail(f,R,!0)-y,M>y)}let x=u.tail(f,1,!0);return x===y?1:zo(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 Ws=[[0,0]];function qn(e){for(let t=Ws.length;t<=e;t++)Ws.push(pe(Ws[t-1],Rl([t,0])));return Ws[e]}function Bs(e,t,n,r){let o=e+n,s=o-e,a=e-(o-s)+(n-s)+t+r,c=o+a;return[c,a-(c-o)]}var mi=class{scale=-1/0;hi=0;lo=0;add(t,n,r){if(t>this.scale){let i=Number.isFinite(this.scale)?Math.exp(this.scale-t):0;this.hi*=i,this.lo*=i,this.scale=t}let o=r*Math.exp(t-this.scale)*(1+n),s=this.hi+o,a=s-this.hi,c=this.hi-(s-a)+(o-a)+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 Sd(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),a=s.length,c=o.reduce((b,E)=>b+E,0),i=xl(qn(c));for(let b of o)i=pe(i,qn(b));for(let b of s)i=pe(i,qn(b));let l=[0,0];for(let b of t.flat())l=De(l,qn(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:a},()=>new Map),g=(b,E)=>{let w=E.join(","),y=p[b].get(w);if(y!==void 0)return y;let x=pe(i,qn(E.reduce((C,_)=>C+_,0)));for(let C of E)x=De(x,qn(C));for(let C=b;C<a;C++)x=De(x,qn(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===a-1){let C=[0,0];for(let _ of E)C=De(C,qn(_));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,A=new Array(r+1).fill(0);for(let C=r-1;C>=0;C--)A[C]=A[C+1]+E[C];let T=new Array(r).fill(0),q=(C,_,I)=>{if(C===r-1){T[C]=_;let D=De(I,qn(_)),S=E.map((B,Q)=>B-T[Q]).sort((B,Q)=>B-Q),F=g(b+1,S),k=`${S.join(",")}|${D[0]}`,P=R.get(k);P===void 0?R.set(k,{to:F,hi:D[0],lo:D[1],mult:1}):P.mult++;return}let O=Math.max(0,_-A[C+1]),L=Math.min(E[C],_);for(let D=O;D<=L;D++)T[C]=D,q(C+1,_-D,De(I,qn(D)))};if(q(0,s[b],[0,0]),M.edges=[...R.values()],M.max=Et(M.edges.map(C=>C.hi+C.to.max)),M.min=ln(M.edges.map(C=>C.hi+C.to.min)),b===a-2){let C=M.edges.map(F=>{let k=F.to.edges[0];return{v:pe([F.hi,F.lo],[k.hi,k.lo]),mult:F.mult}});C.sort((F,k)=>F.v[0]-k.v[0]||F.v[1]-k.v[1]);let _=C.length,I=C[_-1].v[0],O=new Float64Array(_),L=new Float64Array(_),D=new Float64Array(_),S=[0,0];C.forEach(({v:F,mult:k},P)=>{O[P]=F[0],L[P]=F[1];let B=De(F,[I,0]);S=pe(S,[k*Math.exp(B[0])*(1+B[1]),0]),D[P]=S[0]}),M.dist={hi:O,lo:L,cum:D,vmax:I,kv:pe(i,[I,0])}}return M},d=g(0,[...o].sort((b,E)=>b-E)),v=new mi,N=(b,E,w,y)=>{if(E+b.max<=u-m){let[q,C]=Bs(b.KZ[0],b.KZ[1],E,w);return v.add(q,C,y),!0}if(E+b.min>u+m)return!0;let x=b.dist;if(x===void 0)return!1;let[M,R]=Bs(u,h,-E,-w),A=0,T=x.hi.length;for(;A<T;){let q=A+T>>>1,C=x.hi[q];C<M||C===M&&x.lo[q]<=R?A=q+1:T=q}if(A>0){let[q,C]=Bs(x.kv[0],x.kv[1],E,w);v.add(q,C,y*x.cum[A-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<a-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:A,mult:T}of E.edges)for(let q=0;q<x.length;q++){let[C,_]=Bs(w[q],y[q],R,A),I=x[q]*T;if(N(M,C,_,I))continue;let O=M.index.get(C);O===void 0?(M.index.set(C,M.count.length),M.pastHi.push(C),M.pastLo.push(_),M.count.push(I)):M.count[O]+=I}E.pastHi=E.pastLo=E.count=[],E.index.clear()}}return Math.min(1,v.value())}function Us(e){let t=e.every(n=>Number.isInteger(n))?1:2;return[t,e.map(n=>n*t).sort((n,r)=>n-r)]}function pi(e){return[...e].sort((t,n)=>t-n).join(",")}function hi(e,t,n){let r=n.length,o=new Float64Array(r),s=new Float64Array(r),a=0;for(let c=0;c<r;c++)o[c]=a+=n[c];a=0;for(let c=r-1;c>=0;c--)s[c]=a+=n[c];return{f:e,offset:t,lower:o,upper:s}}function di(e){let[,t]=Us(e),n=0,r=0;for(let o of t)r+=n+=o;return{steps:r,size:n+1}}function gi(e){let[t,n]=Us(e),r=0;for(let a of n)r+=a;let o=new Float64Array(r+1);o[0]=1;let s=0;for(let a of n){s+=a;for(let c=s;c>=a;c--)o[c]=.5*(o[c]+o[c-a]);for(let c=a-1;c>=0;c--)o[c]=.5*o[c]}return hi(t,0,o)}function wi(e,t){let[,n]=Us(e),r=n.length,o=Math.min(t,r-t),s=Cf(n),a=0;for(let c=1;c<=r;c++)for(let i=Math.max(1,o-(r-c));i<=Math.min(c,o);i++)a+=s[c]-s[c-i]-s[i]+1;return{steps:a,size:s[r]-s[r-o]-s[o]+1}}function Cf(e){let t=[0];for(let n of e)t.push(t[t.length-1]+n);return t}function bi(e,t){let[n,r]=Us(e),o=r.length,s=Math.min(t,o-t),a=Cf(r),c=[];for(let h=0;h<=s;h++)c.push(new Float64Array(a[o]-a[o-h]-a[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=a[g],w=a[h]-a[h-g],y=a[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 i=c[s],l=t*(t+1)/2;if(s===t)return hi(n,a[s]/n-l,i);let f=i.length,m=new Float64Array(f);for(let h=0;h<f;h++)m[h]=i[f-1-h];let u=a[o]-a[o-s];return hi(n,(a[o]-u)/n-l,m)}function cr(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 Sr(e,t){let n=cr(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 Of=["alternative","mu","paired","exact","correct","confInt","confLevel","tolRoot"];function Wo(e,t,n={}){if(typeof e!="string"&&ct(t)&&K(t,Of,"wilcoxTest"),K(n,Of,"wilcoxTest"),typeof e=="string"){if(n.paired!==void 0)throw new Error("cannot use 'paired' in formula method");let g=Is(e,t,!0);return g.kind==="one"?Wo(g.x,n):Wo(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,a=n.paired??!1,c=n.exact??null,i=qd(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(a&&r===void 0)throw new TypeError("y is required when paired is true");let u,h;if(a){let[g,d]=nr(Array.from(e),Array.from(r));u=Pt(g.map((v,N)=>v-d[N]))}else u=Pt(Array.from(e)),r!==void 0&&(h=Pt(Array.from(r)));if(u.length<1)throw new Error("not enough (non-missing) 'x' observations");if(h!==void 0&&h.length<1)throw new Error("not enough (non-missing) 'y' observations");let p={alternative:o,mu:s,correct:i,confInt:l,confLevel:f,tolRoot:m,warnings:new mt};return J("wilcoxTest",h===void 0?Od(u,c,a,p):Kd(u,h,c,p))}function qd(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 Hs(e,t,n){return n?t==="two.sided"?Math.sign(e)*.5:t==="greater"?.5:-.5:0}var Pf=10*Number.EPSILON;function Vf(e,t,n=0){let r=i=>Math.min(1,Math.max(i,0)),o=()=>n===0?ae(e):r(ae(e)-n*Jt(e)),s=()=>n===0?ae(e,{lowerTail:!1}):r(ae(e,{lowerTail:!1})+n*Jt(e));if(t==="less")return o();if(t==="greater")return s();let a=o();if(a<=.5)return 2*a;let c=s();return c>=1/64?2*(1-a):2*c}function Id(e){let t=e*e;return[e*(t-3),e*(t*(t-10)+15),e*(t*(t*(t-21)+105)-105)]}function _d(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 $f(e,o,s,n)}function Ld(e,t,n,r){let o=t+n,s=t*n,a=-(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 $f(e,a,c,r)}function $f(e,t,n,r){let[o,s,a]=Id(e),c=t*o;return r>=2&&(c+=n*s),r>=3&&(c+=t*t/2*a),c}function yi(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,vi=5e9,Cd=40;function Ni(e){return new RangeError(`wilcoxTest: the exact conditional ${e} would take too long or too much memory: use exact: false`)}function Ys(e,t){if(e.size>16777216||e.steps>vi)throw Ni(t);return e.steps}function jf(e){let t=e.length,n=yt(e.map(Math.abs)),r=0;for(let a=0;a<t;a++)e[a]>0&&(r+=n[a]);let o=0;for(let a of Nn(n))o+=a*a*a-a;let s=Math.sqrt(t*(t+1)*(2*t+1)/24-o/48);return[r,r-t*(t+1)/4,s]}function Od(e,t,n,r){let{alternative:o,mu:s}=r,a=e.map(w=>w-s);a.some((w,y)=>!Number.isFinite(w)&&Number.isFinite(e[y]))&&(a=e.map(w=>w/2-s/2));let i=a.some(w=>w===0),l=t??a.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=yt(a.map(Math.abs)),y=Nn(w).length>0,x=0;for(let C=0;C<a.length;C++)a[C]>0&&(x+=w[C]);let M="Wilcoxon signed rank exact test";if(!y&&!i){let C=a.length,_=C*(C+1)/2,I;if(o==="less")I=Qr(x,C,!0,!1);else if(o==="greater")I=Qr(x-1,C,!1,!1);else{let L=x>_/2?Qr(x-1,C,!1,!1):Qr(x,C,!0,!1);I=Math.min(1,2*L)}let O=f(M,x,I);return r.confInt?{...O,...Fd(e,r)}:O}let R=w.filter((C,_)=>a[_]!==0);Ys(di(R),"distribution");let A=gi(R),T;if(o==="less")T=cr(A,x);else if(o==="greater")T=Sr(A,x-.25);else{let C=0;for(let _ of R)C+=_/2;T=Math.min(1,2*(x>C?Sr(A,x-.25):cr(A,x)))}let q=f(M,x,T);return r.confInt?{...q,...Pd(e,r)}:q}let m=i?a.filter(w=>w!==0):a,u=m.length;if(u===0)throw new RangeError("no non-zero differences for the signed rank test");let[h,p,g]=jf(m),v=Nn(yt(m.map(Math.abs))).length>0||i?Math.min(r.correct,0):r.correct,N=(p-Hs(p,o,v>=0))/g,b=Vf(N,o,v>=1?_d(N,u,v):0),E=f(`Wilcoxon signed rank test${r.correct>=0?" with continuity correction":""}`,h,b);return r.confInt?{...E,...Vd(e,r)}:E}function Ei(e,t,n){return e==="less"?[-1/0,n]:e==="greater"?[t,1/0]:[t,n]}function Ff(e,t,n){let r={confInt:Ei(e,NaN,NaN),confLevel:0,estimate:t};return n!==void 0&&(r.estimateNames=n),r}function zf(e){if(!(e>0&&e<1/0))throw new RangeError("tolRoot must be a positive finite number")}function Kf(e){let t=e.length,n=[];for(let r=0;r<t;r++)for(let o=r;o<t;o++){let s=bn(e[r],e[o]);Number.isNaN(s)||n.push(s)}return n.sort(Mi)}function Wf(e,t,n,r,o){let{alternative:s,confLevel:a}=r,c=e.length,i=s==="two.sided"?(1-a)/2:1-a,l=n(i);t(l)<=i+Pf&&l++;let f=yi(e);if(l===0)return{confInt:[-1/0,1/0],confLevel:1,estimate:f,estimateNames:o};let m=t(l-1);return{confInt:Ei(s,e[l-1],e[c-l]),confLevel:1-(s==="two.sided"?2*m:m),estimate:f,estimateNames:o}}function Fd(e,t){let n=e.length;return Wf(Kf(e),r=>Qr(r,n,!0,!1),r=>Dl(r,n,!0,!1),t,"(pseudo)median")}function Bf(e){let t=e.length,n=Float64Array.from(e).sort(),r=new Array(t);for(let s=0;s<t;s++){let a=e[s],c=0,i=t;for(;c<i;){let f=c+i>>>1;n[f]<a?c=f+1:i=f}let l=c;for(i=t;l<i;){let f=l+i>>>1;n[f]<=a?l=f+1:i=f}r[s]=(c+1+l)/2}let o="";for(let s=0;s<t;){let a=s;for(;a+1<t&&n[a+1]===n[s];)a++;a>s&&(o+=`${s}:${a-s+1},`),s=a+1}return{ranks:r,key:o}}function Uf(e,t,n,r,o,s,a,c,i,l){let{alternative:f,confLevel:m}=i,u=e.length,p=(f==="two.sided"?(1-m)/2:1-m)+Pf,g=f!=="less",d=f!=="greater",v=I=>bn(e[I-1],e[I]),N=Math.min(u-1,Math.max(1,Math.ceil(a.mean+ie(Math.min(p,1))*a.sd+.1*a.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,O=0,L=Cd*c;for(let[D,S]of[[0,E],[1,w]])if(!(D===0?!g:!d))for(let F=0;F<=N;F++){if(O+=L,O>vi)throw Ni("interval");let k;try{k=t(S(F))}catch{break}if(y[D].set(F,[k.stat,k.key]),I.has(k.key)){I.delete(k.key),I.set(k.key,!0);continue}if(O+=r(k.scores),O>vi)throw Ni("interval");I.set(k.key,!0),I.size>b&&I.delete(I.keys().next().value)}}let x=new Map,M=(I,O)=>{let L=I===0?E(O):w(O),D=y[I].get(O),S;if(D===void 0){let B=t(L);D=[B.stat,B.key],S=B.scores}let[F,k]=D,P=x.get(k);return P!==void 0?x.delete(k):(P=n(S??t(L).scores),x.size>=b&&x.delete(x.keys().next().value)),x.set(k,P),[F,P]},R=I=>{let[O,L]=M(0,I);return o(L,O)},A=I=>{let[O,L]=M(1,I);return s(L,O)},T=-1/0,q=0;if(g){let I=R(0);if(!(I>p)){let O=I,L=1;for(;L<=u-1;L++){let D=R(L);if(D>p)break;O=D}T=e[L-1],q=O}}let C=1/0,_=0;if(d){let I=A(0);if(!(I>p)){let O=I,L=u-1;for(;L>=1;L--){let D=A(u-L);if(D>p)break;O=D}C=e[L],_=O}}return{confInt:[T,C],confLevel:1-(q+_),estimate:yi(e),estimateNames:l}}function Dd(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 Pd(e,t){let n=e.length,r=s=>{let a=Dd(e,s),{ranks:c,key:i}=Bf(a.map(Math.abs)),l=0;for(let f=0;f<n;f++)a[f]>0&&(l+=c[f]);return{stat:l,scores:c,key:i}},o=n*(n+1)/2;return Uf(Kf(e),r,gi,s=>Ys(di(s),"interval"),(s,a)=>Sr(s,a-.25),cr,{mean:o/2,sd:Math.sqrt(n*(n+1)*(2*n+1)/24)},n,t,"(pseudo)median")}function Vd(e,t){let{alternative:n,confLevel:r,tolRoot:o}=t,s=t.correct>=0,a=e.length;if(a===0)return Ff(n,NaN);let c=ln(e),i=Et(e);if(!Number.isFinite(c)||!Number.isFinite(i)||a===1||c===i)return Number.isFinite(c)&&c===i&&t.warnings.add("cannot compute confidence interval when all observations are zero or tied"),Ff(n,bn(c,i),"midrange");let l=(b,E)=>{let[,w,y]=jf(e.map(x=>x-b).filter(x=>x!==0));return(w-Hs(w,n,E))/y},f=l(c,s),m=l(i,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"),zf(o);let d=b=>Ht(E=>l(E,s)-b,c,i,{tol:o,fLower:f-b,fUpper:m-b}),v;if(p>=1){let b=yi([...e].sort(Mi));v=Ei(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,i,{tol:o,fLower:l(c,!1),fUpper:l(i,!1)});return{confInt:v,confLevel:g?1-Math.min(1,p):r,estimate:N,estimateNames:"(pseudo)median"}}function Hf(e,t){let n=e.length,r=n-t,o=0;for(let i=0;i<t;i++)o+=e[i];let s=o-t*(t+1)/2,a=0;for(let i of Nn(e))a+=i*i*i-i;let c=Math.sqrt(t*r/12*(n+1-a/(n*(n-1))));return[s,s-t*r/2,c,a>0]}function $d(e,t){let[n,r,o]=Hf(yt([...e,...t]),e.length);return[n,r,o]}function jd(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]),a=Array.from({length:r},(l,f)=>f);a.sort((l,f)=>s(l,f)||l-f);let c=new Array(r),i=0;for(;i<r;){let l=i;for(;l+1<r&&s(a[l+1],a[i])===0;)l++;let f=(i+l+2)/2;for(let m=i;m<=l;m++)c[a[m]]=f;i=l+1}return c}function zd(e,t,n){return Yf(e,t,n).ranks}function Yf(e,t,n){let r=e.map(o=>o-n);if(r.some(o=>Number.isNaN(o))){let o=r.map((i,l)=>[i,l]).filter(([i])=>!Number.isNaN(i)),s=yt([...o.map(([i])=>i),...t]),a=new Array(e.length+t.length);o.forEach(([,i],l)=>a[i]=s[l]),t.forEach((i,l)=>a[e.length+l]=s[o.length+l]);let c=o.length+t.length;return r.forEach((i,l)=>{Number.isNaN(i)&&(a[l]=++c)}),{ranks:a,key:pi(a)}}if(Number.isFinite(n)&&r.some((o,s)=>!Number.isFinite(o)&&Number.isFinite(e[s]))){let o=jd(e.map(s=>s/2-n/2),t);return{ranks:o,key:pi(o)}}return Bf([...r,...t])}function Kd(e,t,n,r){let{alternative:o,mu:s}=r,a=e.length,c=t.length,i=n??(a<50&&c<50),l=zd(e,t,s),[f,m,u,h]=Hf(l,a),p,g;if(i)if(g="Wilcoxon rank sum exact test",!h)p=Df(()=>o==="less"?Zr(f,a,c,!0,!1):o==="greater"?Zr(f-1,a,c,!1,!1):Math.min(2*Zr(f,a,c,!0,!1),2*Zr(f-1,a,c,!1,!1),1));else{Ys(wi(l,a),"distribution");let N=bi(l,a);o==="less"?p=cr(N,f):o==="greater"?p=Sr(N,f-.25):p=Math.min(2*cr(N,f),2*Sr(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-Hs(m,o,N>=0))/u;p=b?1:Vf(E,o,N>=1?Ld(E,a,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=i?h?Bd(e,t,r):Df(()=>Wd(e,t,r)):Ud(e,t,r);return{...d,...v}}function Df(e){try{return e()}catch(t){throw t instanceof RangeError&&t.message.startsWith("wilcox:")?new RangeError(`${t.message}: use exact: false`):t}}function Gf(e,t,n){let r=[];for(let o of e)for(let s of t){let a=o-s;n&&Number.isNaN(a)||r.push(a)}return r.sort(Mi)}function Wd(e,t,n){let r=e.length,o=t.length;return Wf(Gf(e,t,!1),s=>Zr(s,r,o,!0,!1),s=>Vl(s,r,o,!0,!1),n,"difference in location")}function Bd(e,t,n){let r=e.length,o=t.length,s=a=>{let{ranks:c,key:i}=Yf(e,t,a),l=0;for(let f=0;f<r;f++)l+=c[f];return{stat:l-r*(r+1)/2,scores:c,key:i}};return Uf(Gf(e,t,!0),s,a=>bi(a,r),a=>Ys(wi(a,r),"interval"),(a,c)=>Sr(a,c-.25),cr,{mean:r*o/2,sd:Math.sqrt(r*o*(r+o+1)/12)},r+o,n,"difference in location")}function Ud(e,t,n){let{alternative:r,confLevel:o,tolRoot:s}=n,a=n.correct>=0,c=ln(e)-Et(t),i=Et(e)-ln(t);if(!Number.isFinite(c)||!Number.isFinite(i))throw new Error("cannot compute the confidence interval because the differences are not finite");let l=(h,p)=>{let[,g,d]=$d(e.map(v=>v-h),t);return(g-Hs(g,r,p))/d},f=(h,p)=>{let g=l(c,p)-h,d=l(i,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?i:(zf(s),Ht(v=>l(v,p)-h,c,i,{tol:s,fLower:g,fUpper:d}))},m=1-o,u;return r==="greater"?u=[f(-ie(m),a),1/0]:r==="less"?u=[-1/0,f(ie(m),a)]:u=[f(-ie(m/2),a),f(ie(m/2),a)],{confInt:u,confLevel:o,estimate:f(0,!1),estimateNames:"difference in location"}}var Hd=["method","n"],Xf=["holm","hochberg","hommel","bonferroni","BH","BY","fdr","none"];function Yd(e){if(Xf.includes(e))return e;let t=Xf.filter(n=>n.startsWith(e));if(e.length===0||t.length!==1)throw new RangeError(`invalid method "${e}"`);return t[0]}function ro(e,t={}){K(t,Hd,"pAdjust");let n=typeof t=="string"?{method:t}:t,r=Yd(n.method??"holm"),o=Array.from(e),s=[];for(let m=0;m<o.length;m++)Number.isNaN(o[m])||s.push(m);let a=s.length,c=n.n??a;if(!(c>=a))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||a===0)return o;let i=s.map(m=>o[m]),l=Zf(i,r,c),f=o.slice();return s.forEach((m,u)=>f[m]=l[u]),f}function Gs(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 Zf(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=Gs(e,!1),a=-1/0;return s.forEach((c,i)=>{a=Math.max(a,(n-i)*e[c]),o[c]=Math.min(1,a)}),o}case"hochberg":{let s=Gs(e,!0),a=1/0;return s.forEach((c,i)=>{let l=r-i;a=Math.min(a,(n-l+1)*e[c]),o[c]=Math.min(1,a)}),o}case"BH":case"fdr":case"BY":{let s=1;if(t==="BY"){let m=$n(),u=Math.floor(n);if(u<=1e6){for(let h=1;h<=u;h++)wn(m,1/h,u);s=jn(m)}else{let h=u*u;s=Math.log(u)+.5772156649015329+1/(2*u)-1/(12*h)+1/(120*h*h)}}let a=s*n,c=Number.isFinite(a)?1:1024,i=Number.isFinite(a)?a:s*(n/1024),l=Gs(e,!0),f=1/0;return l.forEach((m,u)=>{let h=r-u;f=Math.min(f,i/h*e[m]*c),o[m]=Math.min(1,f)}),o}case"hommel":return n===2?Zf(e,"hochberg",n):Gd(e,n)}}var Qf=2**16;function Gd(e,t){if(!Number.isInteger(t))throw new RangeError("n must be an integer for the hommel method");if(t>Qf)throw new RangeError(`n must be at most ${Qf} for the hommel method (O(n^2) work)`);let n=e.length,r=[...e,...new Array(t-n).fill(1)],o=Gs(r,!1),s=o.map(i=>r[i]),a=s.slice();for(let i=t;i>=2;i--){let l=1/0;for(let f=1;f<=i;f++)l=Math.min(l,i*s[t-i+f-1]/f);for(let f=t-i;f<t;f++)a[f]=Math.max(a[f],l);for(let f=0;f<t-i;f++)a[f]=Math.max(a[f],Math.min(i*s[f],l))}let c=new Array(n);return o.forEach((i,l)=>{i<n&&(c[i]=a[l])}),c}var Jf=["p","alternative","confLevel"],Xd=1+1e-7;function xi(e,t,n){let r=e-1;return e===0||e-r===1&&Dt(t,-r)[1]===0?Vn(r,t,n,!1,!1):Vn(e,t,n,!1,!1)+gn(e,t,n,!1)}function Qd(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=gn(e,t,n,!1)*Xd,s=i=>gn(i,t,n,!1)<=o;if(e<r){let i=bs(s,Math.ceil(r),t),l=i>t?0:xi(i,t,n);return Math.min(1,Vn(e,t,n,!0,!1)+l)}let a=vs(s,0,Math.floor(r)),c=a<0?0:Vn(a,t,n,!0,!1);return Math.min(1,xi(e,t,n)+c)}function Zd(e,t,n={}){K(t,Jf,"binomTest"),K(n,Jf,"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),!Mn(e[0])||!Mn(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(!Mn(e))throw new RangeError("'x' must be nonnegative and integer");if(r=Math.round(e),!(Mn(t)&&Math.round(t)>=r&&t>0))throw new RangeError("'n' must be a positive integer >= 'x'");o=Math.round(t)}return Ri(r,o,n)}function Ri(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;Lt(s);let a;o==="less"?a=Vn(e,t,r,!0,!1):o==="greater"?a=xi(e,t,r):a=Qd(e,t,r);let c=f=>e===0?0:Jn(f,e,t-e+1,!0,!1),i=f=>e===t?1:f<1e-8?Jn(f,e+1,t-e,!1,!1):Jn(1-f,e+1,t-e,!0,!1),l;if(o==="less")l=[0,i(1-s)];else if(o==="greater")l=[c(1-s),1];else{let f=(1-s)/2;l=[c(f),i(f)]}return{method:"Exact binomial test",alternative:o,statistic:e,statisticName:"number of successes",parameter:t,parameterName:"number of trials",pValue:a,confInt:l,confLevel:s,estimate:e/t,estimateNames:"probability of success",nullValue:r,nullValueName:"probability of success",warnings:[]}}var Jd=["T","r","alternative","confLevel"],e0=1+1e-7;function t0(e,t){if(t===0)return e===0?1:0;if(e===t)return 1;let n=Ns(e,t,!1)*e0,r=a=>Ns(a,t,!1)<=n,o=Math.floor(t);if(e<t){let a=bs(r,Math.max(e+1,o),1/0);return Math.min(1,Xr(e,t,!0,!1)+Ai(a,t))}let s=vs(r,0,Math.min(e-1,o));return Math.min(1,Xr(s,t,!0,!1)+Ai(e,t))}var Bo=2**-1022;function Ai(e,t){return e-(e-1)===1?Xr(e-1,t,!1,!1):Xr(e,t,!1,!1)+Ns(e,t,!1)}function kr(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 n0(e,t,n,r){let o=n+t*r,s=e*t*n/o;if(Number.isFinite(s)&&Number.isFinite(o)&&o>=Bo&&(s!==0||e===0||t===0))return s;let a=e*t,c=1+t*(r/n);if(e===0||t===0)return 0;if(Number.isFinite(a)&&Number.isFinite(c))return a/c;let[i,l]=kr(e),[f,m]=kr(t),[u,h]=kr(n),[p,g]=kr(r),d=f*p,v=h-m-g,N=v>=0?u/(u+$(d,-v)):u/($(u,v)+d);return $(i*f*N,l+m+Math.min(v,0))}function r0(e,t,n,r){let o=e/n,s=t/r,a=o/s;return t===0||Number.isFinite(o)&&Number.isFinite(s)&&s>=Bo&&(o===0||o>=Bo)?a:e===0?0:e/t*(r/n)}function o0(e,t={}){K(t,Jd,"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(Mn))throw new RangeError("'x' must be finite, nonnegative, and integer");let r=n.map(Math.round),o=r.length,s=t.T??1,a=typeof s=="number"?new Array(o).fill(s):Array.from(s);if(a.length!==o)throw new RangeError("'x' and 'T' have incompatible length");if(!a.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 i=Je(t.alternative),l=t.confLevel??.95;if(o===2?Lt(l):fn(l),o===2){let[v,N]=r,[b,E]=a,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>=Bo&&(y===0||y>=Bo)?y/x:1/(1+E/b/c),R=Ri(Math.round(v),Math.round(v)+Math.round(N),{p:M,alternative:i,confLevel:l}),A=E/b,T=q=>{if(q===0)return 0;let C=q/(1-q);if(Number.isFinite(A)||C===1/0)return C*A;let[_,I]=kr(C),[O,L]=kr(b),[D,S]=kr(E);return $(_*D/O,I+S-L)};return{method:"Comparison of Poisson rates",alternative:i,statistic:v,statisticName:"count1",parameter:n0(w,c,b,E),parameterName:"expected count1",pValue:R.pValue,confInt:[T(R.confInt[0]),T(R.confInt[1])],confLevel:l,estimate:r0(v,N,b,E),estimateNames:"rate ratio",nullValue:c,nullValueName:"rate ratio",warnings:[]}}let f=r[0],m=a[0],u=c*m,h;i==="less"?h=Xr(f,u,!0,!1):i==="greater"?h=Ai(f,u):h=t0(f,u);let p=v=>f===0?0:ws(v,f,1,!0,!1),g=v=>v<1e-8?ws(v,f+1,1,!1,!1):ws(1-v,f+1,1,!0,!1),d;if(i==="less")d=[0,g(1-l)];else if(i==="greater")d=[p(1-l),1/0];else{let v=(1-l)/2;d=[p(v),g(v)]}return{method:"Exact Poisson test",alternative:i,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 em=["p","alternative","confLevel","correct"],s0="Chi-squared approximation may be incorrect";function Ti(e,t,n={}){ct(t)&&K(t,em,"propTest"),K(n,em,"propTest");let r,o,s=n;if(typeof e!="number"&&e.length>0&&Array.isArray(e[0])){let T=e;if(T.some(q=>q.length!==2))throw new RangeError("'x' must have 2 columns");r=T.map(q=>q[0]),o=T.map(q=>q[0]+q[1]),t!==void 0&&typeof t=="object"&&!Array.isArray(t)&&(s=t)}else{if(t===void 0||typeof t=="object"&&!Array.isArray(t))throw new RangeError("'n' must be given");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 a=r.length,c=[];for(let T=0;T<a;T++)!Number.isNaN(r[T])&&!Number.isNaN(o[T])&&c.push(T);let i=c.length;if(i<1)throw new RangeError("not enough data");if(r=c.map(T=>r[T]),o=c.map(T=>o[T]),o.some(T=>!(T>0)))throw new RangeError("elements of 'n' must be positive");if(o.some(T=>T===1/0))throw new RangeError("elements of 'n' must be finite");if(r.some(T=>!(T>=0)))throw new RangeError("elements of 'x' must be nonnegative");if(r.some((T,q)=>T>o[q]))throw new RangeError("elements of 'x' must not be greater than those of 'n'");let l;if(s.p!==void 0){let T=typeof s.p=="number"?[s.p]:Array.from(s.p);if(T.length!==a)throw new RangeError("'p' must have the same length as 'x' and 'n'");if(l=c.map(q=>T[q]),l.some(q=>!(q>0&&q<1)))throw new RangeError("elements of 'p' must be in (0,1)")}let f=l!==void 0;i===1&&l===void 0&&(l=[.5]);let m=s.confLevel??.95;Lt(m);let u=Je(s.alternative);(i>2||i===2&&f)&&(u="two.sided");let h=r.map((T,q)=>T/o[q]),p=i===2?h[0]-h[1]:0,g=i===2?1/o[0]+1/o[1]:0,d=(s.correct??!0)&&i<=2?.5:0,v=!1;if(d>0&&i===1){let T=Math.abs(r[0]-o[0]*l[0]);T<=d&&([d,v]=[T,!0])}else if(d>0&&i===2&&!f){let T=Math.abs(p)/g;T<=d&&([d,v]=[T,!0])}let N=a0(r,o),b=o.some((T,q)=>{let C=l?.[q]??N;return T*C<5||T*(1-C)<5}),E=0;if(!v)for(let T=0;T<i;T++){let q=l?.[T]??N,C=o[T]*q,_=o[T]*(1-q);E+=$t(Math.abs(r[T]-C)-d,C),E+=$t(Math.abs(o[T]-r[T]-_)-d,_)}let w=l!==void 0?i:i-1,y;if(u==="two.sided")y=vt(E,w/2,2,!1,!1);else{let T=Math.sign(i===1?h[0]-l[0]:p);y=ae(T*Math.sqrt(E),{lowerTail:u==="less"})}let x;if(i===1||i===2&&!f){let T=(1-m)/2,q=u==="two.sided"&&T<1e-8?ie(T,{lowerTail:!1}):ie(u==="two.sided"?(1+m)/2:m);if(i===1){let C=o[0],_=q*q,I=(F,k)=>(F+_/(2*C)+k*q*Math.sqrt(F*(1-F)/C+_/(4*C*C)))/(1+_/C),O=h[0]+d/C,L=h[0]-d/C,D=Math.min(1,Math.max(0,O>=1?1:I(O,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)),_=q*C+d*g,I=Math.max(-1,p-_),O=Math.min(1,p+_);x=u==="two.sided"?[I,O]:u==="greater"?[I,1]:[-1,O]}}let M=i===1?"p":c.map(T=>`prop ${T+1}`),A={method:`${i===1?"1-sample proportions test":`${i}-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:i===1?h[0]:h,estimateNames:M,warnings:b?[s0]:[]};return l!==void 0&&(A.nullValue=i===1?l[0]:l,A.nullValueName=M),x!==void 0&&(A.confInt=x,A.confLevel=m),J("propTest",A)}function a0(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 i0=["method","confLevel","sides"],c0=["wilson","wald","wilsoncc","agresti-coull","jeffreys","clopper-pearson","midp"],l0=["two.sided","left","right"];function Uo(e,t){let n=ke(t,l0),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{...Si(s,o),sides:n}}function Si(e,t=1-e){let n=e/2<1e-8,r=n?ie(e/2,{lowerTail:!1}):ie(1-e/2),o=n?Pn(e,1,{lowerTail:!1}):Pn(t,1);return{sides:"two.sided",level:t,alpha:e,kappa:r,chisq:o}}function oo(e,t){if(typeof e!="number")throw new TypeError(`'${t}' must be a number`);if(!Mn(e))throw new RangeError(`'${t}' must be a nonnegative integer`);return Math.round(e)+0}function Un(e,t,n,r){let o=oo(e,n),s=oo(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 Qs(e,t,n){let r=e/t,o=1-r,s=n*n,a=(e+s/2)/(t+s),c=n*Math.sqrt(t)/(t+s)*Math.sqrt(r*o+s/(4*t)),i=a+c,l=a-c;return e===0?l=0:l<a/2&&(l=r*(e/(t+s))/i),[l,e===t?1:i]}function Xs(e,t,n,r){return r?e/2<1e-8?Jn(e/2,t,n,!1,!1):Jn(1-e/2,t,n,!0,!1):Jn(e/2,t,n,!0,!1)}function tm(e,t,n,r){let o=n/2,s=r?f=>{let m=Math.exp(f);return .5*gn(e,t,m,!1)+Vn(e-1,t,m,!0,!1)-o}:f=>{let m=Math.exp(f);return .5*gn(e,t,m,!1)+Vn(e,t,m,!1,!1)-o},a=r?-1:1,c=Math.log(e/t),i=a*s(c);if(i===0)return e/t;if(i>0){let f=c,m=i,u=1;for(;;){let h=c-u;if(h<-745)return 0;let p=a*s(h);if(p<=0)return Math.exp(p===0?h:Ht(g=>a*s(g),h,f,{tol:2**-60,fLower:p,fUpper:m}));f=h,m=p,u*=2}}let l=a*s(0);return l<=0?1:Math.exp(Ht(f=>a*s(f),c,0,{tol:2**-60,fLower:i,fUpper:l}))}function ki(e,t,n={}){K(n,i0,"binomCI");let r=ke(n.method,c0),o=Uo(n.confLevel,n.sides),[s,a]=Un(e,t,"x","n"),{alpha:c,kappa:i}=o,l=s/a,f=1-l,m,u;switch(r){case"wald":{let h=i*Math.sqrt(l*f/a);[m,u]=[l-h,l+h];break}case"wilson":[m,u]=Qs(s,a,i);break;case"wilsoncc":{let h=i*i,p=2*(a+h),g=2*s+h-1,d=i*Math.sqrt(h-2-1/a+4*l*(a*f+1));m=l===0?0:g-d<g/2?(2*s-1)/(2*a)*(2*s-1)/(g+d):(g-d)/p,u=l===1?1:(2*s+h+1+i*Math.sqrt(h+2-1/a+4*l*(a*f-1)))/p;break}case"agresti-coull":{let h=i*i,p=a+h,g=(s+h/2)/p,d=i*Math.sqrt(g*(1-g)/p);[m,u]=[g-d,g+d];break}case"jeffreys":m=s===0?0:Xs(c,s+.5,a-s+.5,!1),u=s===a?1:Xs(c,s+.5,a-s+.5,!0);break;case"clopper-pearson":m=s===0?0:Xs(c,s,a-s+1,!1),u=s===a?1:Xs(c,s+1,a-s,!0);break;case"midp":m=s===0?0:s===a?Math.exp(Math.log(c)/a):tm(s,a,c,!1),u=s===a?1:s===0?-Math.expm1(Math.log(c)/a):tm(s,a,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 Ho(e){return e===1/0?1:e/(1+e)}function Yo(e,t){return e===0?0:t>0?e*e/t:1/0}function f0(e,t,n,r){return e/t+n/r>1}function m0(e,t,n,r){return Js(e,t,n,r,0)}function _i(e,t,n,r,o){if(f0(e,t,n,r)){let[p,g,d,v]=_i(t-e,t,r-n,r,-o);return[g,p,v,d]}let s=o<=0,[a,c,i,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 a>0&&(N+=a/v),a<c&&(N-=(c-a)/(1-v)),i>0&&(N+=i/(v+f)),i<l&&(N-=(l-i)/(m-v)),N},g=v=>{let N=0;return a>0&&(N-=a/(v*v)),a<c&&(N-=(c-a)/((1-v)*(1-v))),i>0&&(N-=i/((v+f)*(v+f))),i<l&&(N-=(l-i)/((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=u0(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 u0(e,t,n,r,o){let s=e/t,a=n/r,c=r/t,i=1+c,l=-(1+c+s+c*a+o*(c+2)),f=o*o+o*(2*s+c+1)+s+c*a,m=-s*o*(1+o),u=l/(3*i),h=u*u*u-l*f/(6*i*i)+m/(2*i),p=Math.sign(h)*Math.sqrt(Math.max(0,u*u-f/(3*i)));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 Li(e,t,n,r,o,s){if(s==="wald")return e/t*((t-e)/t)/t+n/r*((r-n)/r)/r;let[a,c,i,l]=_i(e,t,n,r,o),f=a*c/t+i*l/r;if(s==="mee")return f;let m=t+r;return f*m/(m-1)}function h0(e,t,n,r,o,s,a){let c=De(De(ft([e,0],t),ft([n,0],r)),[o,0]),[i,l,f,m]=_i(e,t,n,r,o),u=pe(ft(Kt(i,l),t),ft(Kt(f,m),r));return s==="mn"&&(u=ft(At(u,t+r),t+r-1)),De(Ce(c,c),At(u,a))[0]}function Js(e,t,n,r,o){return De(De(ft([e,0],t),ft([n,0],r)),[o,0])[0]}function p0(e,t,n,r,o,s){return Yo(Js(e,t,n,r,o),Li(e,t,n,r,o,s))}var Zs=new Float64Array(1),qi=new BigInt64Array(Zs.buffer);function nm(e){return Zs[0]=Math.abs(e),e<0?-qi[0]:qi[0]}function lr(e){return qi[0]=e<0n?-e:e,e<0n?-Zs[0]:Zs[0]}function Ii(e,t,n,r,o){let s=e(t);if(s===0||Number.isNaN(s))return t;let a=s<0===o,c=nm(a?r:n),i=nm(t),l=s,f=i,m=s,u=1n;for(;;){if(f=a?i+u:i-u,(a?f>=c:f<=c)&&(f=c),m=e(lr(f)),m===0)return lr(f);if(m<0!=l<0||f===c)break;if(i=f,l=m,u*=2n,u>1n<<40n)return lr(i)}if(m<0==l<0||Number.isNaN(m))return lr(i);for(;f-i>1n||i-f>1n;){let h=(i+f)/2n,p=e(lr(h));if(p===0)return lr(h);p<0==l<0?(i=h,l=p):(f=h,m=p)}return Math.abs(m)<Math.abs(l)?lr(f):lr(i)}function om(e,t,n,r,o,s){let a=m0(e,t,n,r);if(o===0)return[a,a];let c=Ho(o),i=p=>(p===a?0:Ho(p0(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(i,p,g,{tol:1e-300,fLower:d,fUpper:v});return s==="wald"?N:Ii(b=>h0(e,t,n,r,b,s,o),N,p,g,d<0)},f=i(-1),m=i(1),u=a<=-1||f<=0?-1:l(-1,a,f,-c),h=a>=1||m<=0?1:l(a,1,-c,m);return[u,h]}function rm(e,t,n){let r=t,o=e(t);if(o>=0)return Math.exp(t);let s=1;for(;;){let a=t+n*s;if(Math.abs(a)>745)return n<0?0:1/0;let c=e(a);if(c>=0){if(c===0)return Math.exp(a);let i=n>0?Ht(e,r,a,{tol:2**-60,fLower:o,fUpper:c}):Ht(e,a,r,{tol:2**-60,fLower:c,fUpper:o});return Math.exp(i)}r=a,o=c,s*=2}}function d0(e,t,n,r,o){let s=t+r,a=o*(t+n)+e+r,c=o*(e+n),i=Math.sqrt(Math.max(0,a*a-4*s*c)),l=Math.min(1,o,c===0?0:2*c/(a+i));return[l,Math.min(1,l/o)]}function g0(e,t,n,r,o){let[s,a]=d0(e,t,n,r,o);if(!(o>0&&o<1/0))return[s,1-s,a,1-a];let c=s<=.5&&a<=.5?0:o*(1-a)<=1-s?2:3,i=c===0?a:c===2?1-a:1-s,l=y=>c===0?[y,0]:c===2?Dt(1,-y):ft(Dt(1,-y),o),f=c===3?1/o:1,m=c===0?Math.min(1,1/o):1,u=c===2?o>1?ft(Dt(o,-1),o)[0]:0:c===3&&o<1?Dt(1,-o)[0]:0,h=y=>{let x=l(y),M=At(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),A=0,T=0;return p>0&&(A+=M>0?p/M:1/0,T-=p/(M*M)),e<t&&(A-=x>0?o*(t-e)/x:1/0,T-=o*(t-e)/x*(o/x)),n<r&&(A-=R>0?(r-n)/R:1/0,T-=(r-n)/(R*R)),[g*A,T*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=i,v>y&&v<x||(v=y+(x-y)/2);for(let M=0;M<2e3;M++){let[R,A]=d(v);if(R===0)break;R>0?y=v:x=v;let T=v-R/A;if(T>y&&T<x||(T=y<=0?x/2:x>2*y?Math.sqrt(y*x):y+(x-y)/2),!(T>y&&T<x))break;let q=Math.abs(T-v);if(v=T,q<=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 w0(e,t,n,r,o){let[s,a,c,i]=g0(e,t,n,r,o),l=(f,m,u,h)=>u>.5?m*h-(m-f):f-m*u;return Yo(l(e,t,s,a),t*s*a)+Yo(l(n,r,c,i),r*c*i)}function sm(e,t,n,r,o){let s=e/t/(n/r);return im(s,o,a=>w0(e,t,n,r,a))}function b0(e,t,n,r,o){let s=e+n,a=r*(o-1),c=t*o+r-s*(o-1),i=s===0?0:Math.min(1,2*s/(c+Math.sqrt(Math.max(0,c*c+4*a*s))));return[o===1/0?i>0?1:0:Math.min(1,i*o/(1+i*(o-1))),i]}function v0(e,t,n,r,o){let[s,a]=b0(e,t,n,r,o),c=e-t*s;return Yo(c,t*s*(1-s))+Yo(c,r*a*(1-a))}function am(e,t,n,r,o){let s=e*(r-n)/((t-e)*n);return im(s,o,a=>v0(e,t,n,r,a))}function im(e,t,n){if(t===0)return[e,e];let r=Ho(t),o=l=>n(l)-t,s=l=>l>0&&l<e?Ii(o,l,Number.MIN_VALUE,e,!1):l,a=l=>l>e&&l<1/0?Ii(o,l,e,Number.MAX_VALUE,!0):l;if(e===0||e===1/0){let l=u=>Ho(n(u))-r,f=l(1),m=f===0?1:ir(l,e===0?f>0:f<0);return e===0?[0,a(m)]:[s(m),1/0]}let c=Math.log(e),i=l=>(l===c?0:Ho(n(Math.exp(l))))-r;return[s(rm(i,c,-1)),a(rm(i,c,1))]}var N0=["method","confLevel","sides"],y0=["method","confLevel","sides"],M0=["method","confLevel"],E0=["ac","wald","score","mn","mee"];function Ci(e,t,n,r,o,s){let{kappa:a}=s,c=e/t,i=n/r,l=c-i,f,m;switch(o){case"wald":{let u=a*Math.sqrt(c*(1-c)/t+i*(1-i)/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=a*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]=Qs(e,t,a),[p,g]=Qs(n,r,a);f=l-a*Math.sqrt(u*(1-u)/t+g*(1-g)/r),m=l+a*Math.sqrt(h*(1-h)/t+p*(1-p)/r);break}case"mn":case"mee":[f,m]=om(e,t,n,r,s.chisq,o);break}return[Math.min(f,m),Math.max(f,m)]}function x0(e,t,n,r,o={}){K(o,N0,"binomDiffCI");let s=ke(o.method,E0),a=Uo(o.confLevel,o.sides),[c,i]=Un(e,t,"x1","n1"),[l,f]=Un(n,r,"x2","n2"),m=c/i-l/f,[u,h]=Ci(c,i,l,f,s,a);return a.sides==="left"?h=1:a.sides==="right"&&(u=-1),J("binomDiffCI",{estimate:m,confInt:[u,h],confLevel:o.confLevel??.95,method:s,sides:a.sides,warnings:[]})}var R0=["katz.log","koopman"];function ea(e,t,n,r,o){let s=e/t/(n/r),a=o*Math.sqrt(1/e-1/t+1/n-1/r);return[s*Math.exp(-a),s*Math.exp(a)]}function A0(e,t,n,r,o={}){K(o,y0,"binomRatioCI");let s=ke(o.method,R0),a=Uo(o.confLevel,o.sides),[c,i]=Un(e,t,"x1","n1"),[l,f]=Un(n,r,"x2","n2"),m=a.kappa,u=c===0?0:c/i/(l/f),h,p;return c===0&&l===0?[h,p]=[0,1/0]:s==="katz.log"?c===0?[h,p]=[0,ea(.5,i,l,f,m)[1]]:l===0?[h,p]=[ea(c,i,.5,f,m)[0],1/0]:c===i&&l===f?[h,p]=ea(i-.5,i,f-.5,f,m):[h,p]=ea(c,i,l,f,m):[h,p]=sm(c,i,l,f,a.chisq),a.sides==="left"?p=1/0:a.sides==="right"&&(h=0),J("binomRatioCI",{estimate:u,confInt:[h,p],confLevel:o.confLevel??.95,method:s,sides:a.sides,warnings:[]})}var T0=["midp","fisher","wald","score"],S0={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"},k0=2**23,q0=2**20;function cm(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 I0(e,t={}){K(t,M0,"oddsRatio");let n=ke(t.method,T0);if(e.length!==2||e.some(T=>T.length!==2))throw new TypeError("'table' must be a 2 x 2 table");let r=t.confLevel??.95,{alpha:o,kappa:s,chisq:a}=Uo(r,void 0),c=e.map((T,q)=>Array.from(T,(C,_)=>oo(C,`table[${q}][${_}]`))),[[i,l],[f,m]]=c,u=i+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,i-m),p=i+Math.min(l,f),g=p-h+1;if(n==="midp"||n==="fisher"){if(g>k0)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(i,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=cn(i-1,i+f,l+m,i+l,!0,!1)+.5*yn(i,i+f,l+m,i+l,!1),v=cn(i,i+f,l+m,i+l,!1,!1)+.5*yn(i,i+f,l+m,i+l,!1),N=g<=q0?ui(new no(i+f,l+m,i+l),i,1):Lf(i,i+f,l+m,i+l)}catch(T){throw T instanceof RangeError?new RangeError(`the exact p-values of the table are beyond reach: ${T.message}`):T}let b=2*Math.min(d,v),E=[i+l,f+m],w=[i+f,l+m],y=0,x=!1;for(let T=0;T<2;T++)for(let q=0;q<2;q++){let C=E[q]*w[T]/u;C<5&&(x=!0);let _=Math.abs(c[q][T]-C);y+=_*_/C}let M=vt(y,.5,2,!1,!1),R,A;switch(n){case"wald":{R=i*m/(l*f);let T=s*Math.sqrt(1/i+1/l+1/f+1/m);A=[Math.exp(Math.log(R)-T),Math.exp(Math.log(R)+T)];break}case"fisher":{let T=Ye(()=>Ko(c,{confLevel:r}));R=T.estimate,A=T.confInt;break}case"score":{R=i*m/(l*f),A=(i===0||m===0)&&(l===0||f===0)?[0,1/0]:am(i,i+l,f,f+m,a);break}case"midp":{let T=new no(i+f,l+m,i+l),q=O=>{if(O===1)return[d,v];let L=T.density(O),D=i-h,S=.5*L[D];return[cm(L,0,D-1)+S,cm(L,D+1,L.length-1)+S]},C=(O,L)=>{let D=O(1);return D===0?1:ir(O,L?D>0:D<0)};R=i===h?0:i===p?1/0:C(O=>q(O)[0]-.5,!1);let _=i===h?0:C(O=>q(O)[1]-o/2,!0),I=i===p?1/0:C(O=>q(O)[0]-o/2,!1);A=[_,I];break}}return J("oddsRatio",{estimate:R,confInt:A,confLevel:r,method:S0[n],pValue:{midp:b,fisher:N,chiSquare:M},warnings:x?["Chi-squared approximation may be incorrect"]:[]})}var _0=["epsilon","paired","varEqual","confLevel","alpha"],L0=["margin","method","alpha"];function lm(e,t){if(!(typeof e=="number"&&e>0&&e<.5))throw new RangeError(`'${t}' must be a single number in (0, 0.5)`)}function C0(e,t,n={}){K(n,_0,"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)lm(n.alpha,"alpha"),o=1-n.alpha,s=n.alpha;else{if(o=n.confLevel??.95,Lt(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 a=n.paired??!1,c=n.varEqual??!1,i=Ye(()=>En(e,t,{paired:a,varEqual:c})),l=i.stdErr,f=i.parameter,m=typeof i.estimate=="number"?i.estimate:i.estimate[0]-i.estimate[1],u=wt((r-Math.abs(m))/l,f,!1,!1),h=s<1e-8?bt(s,f,!1,!1):bt(o,f,!0,!1),p=[m-h*l,m+h*l];return J("tost",{method:i.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:i.estimate,estimateNames:a?"mean of the differences":i.estimateNames,epsilon:r,seDiff:l,warnings:[]})}var O0=["mn","mee","wald"];function F0(e,t,n,r,o={}){K(o,L0,"binomDiffEquivalenceTest");let s=ke(o.method,O0),a=o.margin;if(a===void 0)throw new TypeError("'margin' is required");if(!(typeof a=="number"&&a>0&&a<1))throw new RangeError("'margin' must be a single number in (0, 1)");let c=o.alpha??.05;lm(c,"alpha");let[i,l]=Un(e,t,"x1","n1"),[f,m]=Un(n,r,"x2","n2"),u=i/l-f/m,h=E=>Js(i,l,f,m,E)/Math.sqrt(Li(i,l,f,m,E,s)),p=h(-a),g=h(a),d=[ae(p,{lowerTail:!1}),ae(g)],v=Math.max(d[0],d[1]),N=1-2*c,b=Ci(i,l,f,m,s,Si(2*c,N));return J("binomDiffEquivalenceTest",{method:s,estimate:u,margin:a,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 na(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 ra(e,t){return e.map(n=>{if(n.rss>=D0&&n.rss<1/0)return $(n.rss,-2*t);let r=0;return n.residuals.forEach((o,s)=>{let a=n.weights?.[s]??1;if(a===0)return;let c=$(o,-t);r+=a*c*c}),r})}var D0=2**-1022;function Fi(e,t){return e.y.flatMap((n,r)=>(e.weights?.[r]??1)===0?[]:[[e.weights?.[r]??1,$(n,-t)]])}function oa(e){let t=Math.floor(Math.log2(e.reduce((n,[r])=>Math.max(n,r),0)));return t-(t%2+2)%2}function qr(e,t,n=0){let r=a=>e.reduce((c,[i,l])=>c+$(i,-a)*l*l,0),o=n,s=r(o);return Number.isFinite(s)||(o=oa(e),s=r(o)),!($(t,-o)>=1e-24*s)}function fm(e){let t=na([e]),{pivot:n}=e.qr,r=[],o=new Map;for(let s=0;s<e.rank;s++){let a=e.assign[n[s]],c=o.get(a);c===void 0&&(c={term:a,df:0,ss:0},o.set(a,c),r.push(c)),c.df++,c.ss+=$(e.effects[s],-t)**2}return{terms:r,rdf:e.dfResidual,rss:ra([e],t)[0],k:t}}var mm=(e,t,n)=>pt(e,t,n,!1,!1),Di="essentially perfect fit for \u20181\u2019: summary may be unreliable";function Pi(e,t,n){if(!(n>0))return!1;let r=e.length,o=0;for(let i of e)o+=i;o/=r;let s=0;for(let i of e)s+=(i-o)**2;let a=r>1?s/(r-1):0,c=t/n;return Number.isFinite(c)&&c<(o*o+a)*1e-30}var Vi=new WeakMap;function P0(e){let t=fm(e),n=t.terms.filter(o=>o.term!==0),r=0;return e.fittedValues.forEach((o,s)=>{let a=e.weights?.[s]??1;a!==0&&(r+=a*$(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 Oi(e){let t=P0(e),{names:n,rdf:r,k:o,perfect:s}=t,{rows:a,rss:c}=t;if(r>0){let g=Fi(e,o);qr(g,c)&&(c=0,a=a.map(d=>qr(g,d.ss)?{...d,ss:0}:d))}let i=c/r,l=g=>$(g,2*o),f=a.map(({df:g,ss:d})=>{let v=d/g/i;return[g,l(d),l(d/g),v,mm(v,g,r)]});f.push([r,l(c),r===0?NaN:l(i),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=a.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 V0(e){let t=e.map(f=>f.dfResidual),n=na(e),r=ra(e,n),o=f=>$(f,2*n),s=0;t.forEach((f,m)=>{f<t[s]&&(s=m)});let a=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 i=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||!Fs(d(p.x),d(g.x))?NaN:h/u/a;return v<0&&(v=NaN),[t[m],o(r[m]),u,o(h),v,Number.isNaN(v)?NaN:mm(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:i?["ANOVA F-tests on an essentially perfect fit are unreliable"]:[]}}function ta(...e){let t=[];for(let n of e)for(let r of n??[])t.includes(r)||t.push(r);return t}function $i(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=Oi(e),i={...c,warnings:ta(e.warnings,c.warnings)},l=Yt.get(c);return l!==void 0&&Yt.set(i,l),J("anova",i,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,i)=>c===i||c!==void 0&&i!==void 0&&c.length===i.length&&c.every((l,f)=>Object.is(l,i[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(i=>i.response!==e.response).map(i=>i.response))];s.push(`models with response ${c.map(i=>`\u2018${i}\u2019`).join(", ")} removed because response differs from model 1`)}let a=o.length===1?Oi(e):V0(o);return a.warnings=ta(...n.map(c=>c.warnings),s,a.warnings),J("anova",a,...n)}function ji(e){if(typeof e=="object"&&e!==null&&e.multistratum===!0){let a=e,c=ta(a.warnings,Vi.get(a)),i={strata:a.strata.filter(f=>f.stratum!=="Error: (Intercept)").map(f=>({...f})),warnings:c},l=Yt.get(a);return Array.isArray(l)&&Yt.set(i,l.filter((f,m)=>a.strata[m].stratum!=="Error: (Intercept)")),J("aovSummary",i,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=Oi(t),r=fm(t),o=Pi(t.fittedValues.map(a=>$(a,-r.k)),r.rss,r.rdf),s=ta(t.warnings,n.warnings,o?[Di]:[]);if(t.dfResidual>0){let a={...n,warnings:s},c=Yt.get(n);return c!==void 0&&Yt.set(a,c),J("aovSummary",a,e)}return J("aovSummary",{colNames:n.colNames.slice(0,3),rowNames:n.rowNames.slice(0,-1),table:n.table.slice(0,-1).map(a=>a.slice(0,3)),warnings:s},e)}var $0=["pooledSd","mu","paired","ci","alternative","adjust"],j0=["pooledSd","mu","paired","ci","alternative"],z0=["generalized","partial","ci","alternative"],K0=["partial","ci","alternative"],W0=["partial","ci","alternative"];function Wi(e){if(e!==null&&!(e>0&&e<1))throw new RangeError("ci must be a single numeric value between (0, 1)")}var B0="Missing values detected. NAs dropped.",U0=`'y' is numeric but has only 2 unique values.
3
- If this is a grouping variable, convert it to a factor.`,dm={tol:0,maxiter:5e3};function Ir(e){return e===1/0?1:Math.exp(Il(.5,(e-1)/2)-.5*Math.log(e/2))}function um(e,t,n){let r=e-1<e+1?1:Math.abs(e)*Number.EPSILON;return xr(o=>Wt(e,t,{ncp:o})-n,e-r,e+r,{...dm,extendInt:"downX"}).root}function gm(e,t,n,r){let o=n.pooledSd??!0,s=n.mu??0,a=n.paired??!1,c=n.ci===void 0?.95:n.ci;Wi(c);let i=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(Pt(f)).size===2&&m.push(U0),(l.some(Number.isNaN)||(f?.some(Number.isNaN)??!1))&&m.push(B0);let u=T=>T===1/0||T===-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||a){let T;if(f===void 0)({groups:[T],k:N}=Ie([Pt(l)]));else{let[C,_]=nr(l,f);({d:T,k:N}=Ms(C,_))}let q=T.length;if(q<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(T),p=Math.sqrt(at(T)),g=p/Math.sqrt(q),d=1/q,v=q-1}else{let T=Tt([Pt(l),Pt(f)]);N=T.k;let[q,C]=T.n;if(q<2||C<2)throw new RangeError("not enough observations: at least 2 are needed in each group");h=T.mean[0]-T.mean[1];let _=Math.sqrt(T.variance[0]),I=Math.sqrt(T.variance[1]);o?(p=Math.sqrt(((q-1)*_**2+(C-1)*I**2)/(q+C-2)),g=p*Math.sqrt(1/q+1/C),d=1/q+1/C,v=q+C-2):(p=Math.sqrt((_**2+I**2)/2),d=2*(C*_**2+q*I**2)/(q*C*(_**2+I**2)),g=Math.sqrt(Math.sqrt(_**2/q)**2+Math.sqrt(I**2/C)**2),v=Es([_**2/q,I**2/C],[q-1,C-1]))}let b=r?Ir(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=i==="two.sided"?c:2*c-1,M=(h-E)/g,R=um(M,v,(1+x)/2)*Math.sqrt(d)*b,A=um(M,v,(1-x)/2)*Math.sqrt(d)*b;return i==="less"&&(R=-1/0),i==="greater"&&(A=1/0),{name:y,value:w,ci:c,ciLow:R,ciHigh:A,warnings:m}}function H0(e,t,n={}){return K(n,$0,"cohensD"),J("cohensD",gm(e,t,n,n.adjust??!1))}function Y0(e,t,n={}){return K(n,j0,"hedgesG"),J("hedgesG",gm(e,t,n,!0))}var G0=e=>Array.isArray(e.effects),zi=e=>e.multistratum===!0,wm=e=>Array.isArray(e.strata),bm=e=>{let t=e;return typeof t.univariate=="object"&&typeof t.anova=="object"},Ki=e=>({test:t=>t.split(":").includes(e)});function X0(e){let t=e.strata.filter(o=>o.stratum!=="Error: (Intercept)").map(o=>{let s=o.stratum.replace(/^Error: /,""),a=o.colNames.indexOf("Df"),c=o.colNames.indexOf("Sum Sq"),i=o.rowNames.map((f,m)=>({name:f,df:o.table[m][a],ss:o.table[m][c]})),l=i.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:i.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 Q0(e){let t=e.univariate,n=t.colNames.indexOf("Sum Sq"),r=[],o=(i,l,f)=>{let m=r.find(u=>u.name===i);return m===void 0&&r.push(m={name:i,terms:[],sse:l,dfe:f}),m},s=new Map;if(t.colNames.includes("Error SS")){let i=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=>Ki(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[i],ss:h[n]};g.terms.push(d),s.set(m,d)})}else{let i=t.colNames.indexOf("Df"),l=t.rowNames.indexOf("Residuals"),f=o("Subject",t.table[l][n],t.table[l][i]);t.rowNames.forEach((m,u)=>{if(m==="(Intercept)"||m==="Residuals")return;let h={name:m,df:t.table[u][i],ss:t.table[u][n]};f.terms.push(h),s.set(m,h)})}let a=r.find(i=>i.name==="Subject");if(a===void 0)throw new Error("the result has no subject stratum");let c=e.anova.rowNames.map(i=>{let l=s.get(i);if(l===void 0)throw new Error(`there is no univariate test of '${i}'`);return l});return{strata:r,subject:a,order:c}}function vm(e,t){let n=Array.isArray(e)?e:[],r=n.filter(s=>!t.some(a=>Ki(s).test(a.name)));if(r.length>0)throw new RangeError(`Observed variable not in data: ${r.join(", ")}`);let o=s=>n.some(a=>Ki(a).test(s));return{isObserved:o,ssObserved:t.reduce((s,a)=>o(a.name)?s+a.ss:s,0)}}function hm(e,t,n,r){let o=a=>dn(e,t,n,{ncp:a})-r,s=o(0);return Number.isNaN(s)?NaN:s>0?e===1/0?1/0:xr(o,0,Math.max(10,e*t*4),{...dm,extendInt:"downX"}).root:0}function Nm(e,t,n,r,o){let s=o==="two.sided"?r:2*r-1,a=Math.max(0,e),c=a/t/((1-a)/n),i=hm(c,t,n,(1+s)/2),l=hm(c,t,n,(1-s)/2);c<=Zn((1-s)/2,t,n)&&(l=0),c<=Zn((1+s)/2,t,n)&&(i=0);let f=i===1/0?1:i/(i+n),m=l===1/0?1:l/(l+n);return o==="less"&&(f=0),o==="greater"&&(m=1),[e,r,f,m]}function Z0(e){let t=Ie([e.map(n=>n.ss)]).k;if(t!==0)for(let n of e)n.ss=$(n.ss,-t)}var pm="needs a table of anova() or Anova(), a fit from lm() or aov() with a formula, its aovSummary(), or a result of aovEz()";function Bi(e,t,n){if(typeof e!="object"||e===null)throw new TypeError(pm);if(zi(e)||wm(e)||bm(e))return J0(e,t,n);let r=G0(e)?Ye(()=>$i(e)):e;if(!Array.isArray(r.colNames)||!Array.isArray(r.table))throw new TypeError(pm);let o=Yt.get(r),s=o!==void 0&&!Array.isArray(o)?o:r,a=n.ci===void 0?.95:n.ci;Wi(a);let c=ke(n.alternative??"greater",["two.sided","less","greater"]),i=s.colNames.indexOf("Df"),l=s.colNames.indexOf("Sum Sq");if(i<0||l<0)throw new TypeError("the table needs the columns 'Df' and 'Sum Sq'");let f=s.rowNames.map((R,A)=>({name:R,df:s.table[A][i],ss:s.table[A][l]})).filter(R=>R.name!=="(Intercept)");Z0(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,A)=>R+A.ss,0),v=f.reduce((R,A)=>R+A.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}=vm(N,u),x=R=>{let{ss:A,df:T}=R;if(t==="Eta2")return E==="partial"?A/(A+p):E==="plain"?A/d:A/(A+p+y-(w(R.name)?A:0));if(t==="Omega2"){let C=E==="partial"?(A-T*g)/(A+(v-T)*g):(A-T*g)/(d+g);return Math.max(0,C)}let q=E==="partial"?(A-T*g)/(A+p):(A-T*g)/d;return Math.max(0,q)},M=`${t}${E==="plain"?"":`_${E}`}`;return{colNames:a===null?[M]:[M,"CI","CI_low","CI_high"],rowNames:u.map(R=>R.name),table:u.map(R=>a===null?[x(R)]:Nm(x(R),R.df,h,a,c)),warnings:Array.isArray(r.warnings)?[...r.warnings]:[]}}function J0(e,t,n){let r=n.ci===void 0?.95:n.ci;Wi(r);let o=ke(n.alternative??"greater",["two.sided","less","greater"]),s=bm(e),a,c,i;if(s){let w=Yt.get(e),y=Q0(w!==void 0&&!Array.isArray(w)?{...e,univariate:w}:e);({strata:a,subject:c}=y),i=y.order.map(x=>({term:x,stratum:y.strata.find(M=>M.terms.includes(x))}))}else{let w=zi(e)?Ye(()=>ji(e)):e,y=Yt.get(w);({strata:a,subject:c}=X0(Array.isArray(y)?{...w,strata:y.map((x,M)=>({...w.strata[M],...x}))}:w)),i=a.flatMap(x=>x.terms.map(M=>({term:M,stratum:x})))}let l=Ie([[...a.flatMap(w=>w.terms.map(y=>y.ss)),...a.map(w=>w.sse)]]).k;for(let w of a){w.sse=$(w.sse,-l);for(let y of w.terms)y.ss=$(y.ss,-l)}let f=a.flatMap(w=>w.terms),m=a.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}=vm(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 T=y===c?0:1,q=g==="partial"?(x-M*R)/(x+T*y.sse+c.sse+h):(x-M*R)/(u+h);return Math.max(0,q)}let A=g==="partial"?(x-M*R)/(x+y.sse):(x-M*R)/u;return Math.max(0,A)},b=`${t}${g==="plain"?"":`_${g}`}`,E={colNames:r===null?[b]:[b,"CI","CI_low","CI_high"],rowNames:i.map(w=>w.term.name),table:i.map(({term:w,stratum:y})=>{let x=N(w,y);return r===null?[x]:Nm(x,w.df,y.dfe,r,o)}),warnings:[]};return s||(E.groups=i.map(w=>w.stratum.name),E.warnings=[...zi(e)||wm(e)?e.warnings:[]]),E}function eg(e,t={}){return K(t,z0,"etaSquared"),J("etaSquared",Bi(e,"Eta2",t),e)}function tg(e,t={}){return K(t,K0,"omegaSquared"),J("omegaSquared",Bi(e,"Omega2",t),e)}function ng(e,t={}){return K(t,W0,"epsilonSquared"),J("epsilonSquared",Bi(e,"Epsilon2",t),e)}var rg=new Set(["eqb","lowEqbound","highEqbound","hypothesis","paired","varEqual","alpha","mu","eqboundType","biasCorrection","rmCorrection","glass","smdCi"]),og=["m1","sd1","n1","m2","sd2","n2","r12"],sg=new Set(["eqb","lowEqbound","highEqbound","hypothesis","paired","varEqual","alpha","mu","eqboundType","biasCorrection","rmCorrection","glass","smdCi"]),ag="Equivalence interval does not include zero.",ig="Warning: setting bound type to SMD produces biased results!",cg="MET test may have higher error rates than a nil two-tailed test. Consider wider equivalence bounds.";function ym(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 xr(s=>Wt(e,t,{ncp:s,lowerTail:!r})-n,e-o,e+o,{tol:0,maxiter:5e3,extendInt:r?"upX":"downX"}).root}function lg(e,t){let{alpha:n,smdCi:r}=t,o=Math.abs(e.delta),s=(M,R,A)=>(1-(A-2)/(A*M*M))*R*R,a,c,i,l,f,m,u,h,p,g,d,v,N=t.biasCorrection;if(e.design==="two"){let{n1:M,n2:R,s1:A,s2:T}=e,q=A*A,C=T*T,_=2*M*R/(M+R);if(t.glass!==void 0){let I=t.glass==="glass1",[O,L,D,S]=I?[A,M,T,R]:[T,R,A,M];c=O,i=L-1,a=N?"Glass's delta(g)":"Glass's delta(d)",l=N?Ir(i):1,f=l*o/c,p=1/R+1/M;let F=D/O;m=Math.sqrt(F*F/(S-1)+1/(L-1)+f*f/(2*(L-1))),u=o/(O*Math.sqrt(1/L+F*F/S)),h=M+R-2,g=Math.sqrt(2/_)}else if(t.varEqual)c=Math.sqrt(((M-1)*q+(R-1)*C)/(M+R-2)),i=M+R-2,a=N?"Hedges's g":"Cohen's d",l=N?Ir(i):1,f=l*o/c,p=1/M+1/R,m=Math.sqrt(1/M+1/R+s(l,f,i)),u=o/(c*Math.sqrt(p)),h=i,g=Math.sqrt(2*(R*q+M*C)/(M*R*(q+C)));else{c=Math.sqrt((q+C)/2),i=(M-1)*(R-1)*(q+C)**2/((R-1)*q*q+(M-1)*C*C),a=N?"Hedges's g(av)":"Cohen's d(av)",l=N?Ir(i):1,f=l*o/c,p=2*(R*q+M*C)/(M*R*(q+C));let I=c**4;m=Math.sqrt(f*f*(q*q/(M-1)+C*C/(R-1))/(8*I)+(q/(M-1)+C/(R-1))/(c*c)),u=o/Math.sqrt(q/M+C/R),h=i,g=Math.sqrt(2/_)}d=i,v=(I,O)=>Math.sqrt(d/(d-2)*(2/_)*(1+I*I*_/2)-I*I/(O*O))}else if(e.design==="paired"){let{n:M,s1:R,s2:A,sdif:T,oneMinusR:q}=e,C=t.glass!==void 0?"":t.rmCorrection?"(rm)":"(z)";if(t.glass!==void 0?c=t.glass==="glass1"?R:A:c=t.rmCorrection?T/Math.sqrt(2*q):T,a=t.glass!==void 0?N?"Glass's delta(g)":"Glass's delta(d)":N?`Hedges's g${C}`:`Cohen's d${C}`,i=M-1,d=2*M-2,l=N?Ir(r==="goulet"?d:i):1,f=l*o/c,p=1/M,u=o*Math.sqrt(M)/c,h=i,g=Math.sqrt(2*q/M),v=(_,I)=>{let O=r==="goulet"?d:i,L=Math.sqrt(O/(O-2)*(2*q/M)*(1+_*_*M/(2*q))-_*_/(I*I));return t.rmCorrection?L:L*Math.sqrt(2*q)},t.glass!==void 0){let _=T/c;m=Math.sqrt(_*_/(M-1)+f*f/(2*(M-1)))}else!t.rmCorrection&&r==="nct"?m=Math.sqrt(1/M+s(l,f,i)):m=v(f,l)}else{let{n:M,s:R}=e;c=R,i=M-1,d=i,a=N?"Hedges's g":"Cohen's d",l=N?Ir(i):1,f=l*o/c,p=1/M,m=Math.sqrt(1/M+f*f/(2*M)),u=o*Math.sqrt(M)/R,h=i,g=1/Math.sqrt(M),v=(A,T)=>Math.sqrt(i/(i-2)*(1/M)*(1+A*A*M)-A*A/(T*T))}let b,E,w,y=i;if(r==="nct"){w=m,y=h;let M=ym(u,h,n,!0),R=ym(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=Bt(n,d,{ncp:M})*g,E=Bt(n,d,{ncp:M,lowerTail:!1})*g}else{w=m;let M=r==="t"?bt(n,i,!1,!1):ie(n,{lowerTail:!1});b=f-M*w,E=f+M*w}let x=e.delta<0;return{label:a,denom:c,estimate:x?-f:f,se:w,confInt:x?[-E,-b]:[b,E],df:y,J:l}}function Mm(e,t){if(!(Array.isArray(e)||ArrayBuffer.isView(e)))throw new TypeError(`'${t}' must be an array of numbers`);let n=e;for(let r=0;r<n.length;r++){let o=n[r];if(typeof o!="number")throw new TypeError(`'${t}' must be numeric`);if(o===1/0||o===-1/0)throw new RangeError(`'${t}' must not contain infinite values`)}}function Em(e){let{eqb:t}=e,n;if(t!==void 0){let r=typeof t=="number"?[t]:Array.from(t);if(!r.every(o=>typeof o=="number"))throw new TypeError("'eqb' must be a number or an array of 1 or 2 numbers");if(r.length===1)n=[-Math.abs(r[0]),Math.abs(r[0])];else if(r.length===2)n=[Math.min(r[0],r[1]),Math.max(r[0],r[1])];else throw new RangeError("'eqb' must be a number or an array of 1 or 2 numbers")}else{if(e.lowEqbound===void 0||e.highEqbound===void 0)throw new TypeError("the equivalence bounds are required: 'eqb', or 'lowEqbound' and 'highEqbound'");n=[e.lowEqbound,e.highEqbound]}if(!n.every(r=>typeof r=="number"&&Number.isFinite(r)))throw new RangeError("the equivalence bounds must be finite numbers");if(n[0]>n[1])throw new RangeError("'lowEqbound' must not be greater than 'highEqbound'");return n}function xm(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 a=e.mu??0;if(!(typeof a=="number"&&Number.isFinite(a)))throw new RangeError("'mu' must be a finite number");let c=(i,l,f)=>{if(i!==void 0&&typeof i!="boolean")throw new TypeError(`'${l}' must be true or false`);return i??f};return{hypothesis:t,smdBounds:n,smdCi:r,glass:o,alpha:s,mu:a,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 Rm(e,t,n,r,o,s,a,c,i,l,f,m){let{alpha:u,hypothesis:h}=l,p=lg(c,l),g=$(p.denom,i),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))),F=Ve(D,-S);return l.smdBounds?[F*p.denom,S+i]:[F,S]},N=l.smdBounds?f:[f[0]/g,f[1]/g];l.smdBounds&&m.push(ig),d[0]<=0&&d[1]>=0||m.push(ag);let b=D=>$(D/r,n-o),E=D=>{let[S,F]=v(D),k=Math.max(n,F);return Ve((Ve(t,n-k)-Ve(S,F-k))/r,k-o)},w=b(t),y=E(f[0]),x=E(f[1]),M=$(r,o),R=h==="EQU",A={t:w,se:M,df:s,pValue:2*wt(-Math.abs(w),s,!0,!1)},T={t:y,se:M,df:s,pValue:wt(y,s,!R,!1)},q={t:x,se:M,df:s,pValue:wt(x,s,R,!1)},C=R?Math.max(T.pValue,q.pValue):Math.min(T.pValue,q.pValue);!R&&d[0]<=0&&d[1]>=0&&C<=A.pValue&&m.push(cg);let _=bt(u,s,!1,!1),I=Math.max(n,o),[O,L]=[$(t,n-I),$(r,o-I)];return J(e,{tTest:A,lower:T,upper:q,pValue:C,bounds:{raw:d,smd:N},raw:{estimate:$(t,n),se:M,confInt:[$(O-_*L,I),$(O+_*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:a,hypothesis:h,warnings:m})}function fg(e,t,n={}){K(n,rg,"tTOST");let r=xm(n),o=Em(n),{paired:s,varEqual:a,mu:c}=r,i=t===void 0;if(s&&i)throw new TypeError("'y' is required when paired is true");Mm(e,"x"),i||Mm(t,"y");let l=Ye(()=>i?En(e,{mu:c}):En(e,t,{paired:s,varEqual:a,mu:c})),f=l.parameter,m,u,h,p;if(i){let g=Ie([Pt(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]=nr(Array.from(e),Array.from(t)),v=Ie([g,d]);u=v.k;let[N,b]=v.groups,E=N.map((A,T)=>A-b[T]),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([Pt(Array.from(e)),Pt(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(a?((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 Rm("tTOST",h,u,p,u,f,l.method,m,u,r,o,[])}function mg(e,t={}){K(e,og,"tsumTOST"),K(t,sg,"tsumTOST");let n=xm(t),r=Em(t),{m1:o,sd1:s,n1:a,m2:c,sd2:i,n2:l,r12:f}=e,m=(I,O)=>{if(typeof I!="number"||!Number.isFinite(I))throw new RangeError(`'${O}' must be a finite number`);return I},u=(I,O)=>{let L=m(I,O);if(!(Number.isInteger(L)&&L>=2))throw new RangeError(`'${O}' must be an integer of at least 2`);return L},h=(I,O)=>{let L=m(I,O);if(!(L>0))throw new RangeError(`'${O}' must be positive`);return L};m(o,"m1"),h(s,"sd1"),u(a,"n1");let p=c===void 0&&i===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(i,"sd2"),u(l,"n2"));let g=[],{groups:d,k:v}=Ie([[o,...p?[]:[c],n.mu]]),{groups:N,k:b}=Ie([[s,...p?[]:[i]]]),[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),A,T,q,C,_;if(p)A=E-y,T=x/Math.sqrt(a),q=a-1,C="One Sample t-test",_={design:"one",n:a,delta:R(A),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 O=a;a!==l&&(g.push("Unequal number of pairs; using smallest n"),O=Math.min(a,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");A=E-w-y,T=L/Math.sqrt(O),q=O-1,C="Paired t-test",_={design:"paired",n:O,delta:R(A),s1:x,s2:M,sdif:L,oneMinusR:1-I}}else{let I=l,O=x*x,L=M*M;if(A=E-w-y,n.varEqual){let D=((a-1)*O+(I-1)*L)/(a+I-2);T=Math.sqrt(D*(1/a+1/I)),q=a+I-2,C=" Two Sample t-test"}else{let D=O/a,S=L/I;T=Math.sqrt(D+S),q=(D+S)**2/(D*D/(a-1)+S*S/(I-1)),C="Welch Two Sample t-test"}_={design:"two",n1:a,n2:I,delta:R(A),s1:x,s2:M}}return Rm("tsumTOST",A,v,T,b,q,C,_,b,n,r,g)}var ug=["alternative","tsmethod"],hg=["square","central"],pg=1e3,dg=32,gg=.9,Am=1+1e-7,wg=2**24,bg=34e7;function vg(e,t={}){if(K(t,ug,"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,hg),o=e.map((y,x)=>Array.from(y,(M,R)=>oo(M,`table[${x}][${R}]`))),[[s,a],[c,i]]=o,l=s+a,f=c+i;if(l===0||f===0)throw new RangeError("each row (group) of 'table' must have at least one observation");if((l+1)*(f+1)>wg)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(pg,Math.ceil(dg*Math.sqrt(m))),h=y=>Math.min(y+1,40*Math.sqrt(y));if(u*(h(l)+h(f)+4)>bg)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=cn(s,l,f,d,!0,!1):g==="greater"?v=cn(s-1,l,f,d,!1,!1):v=Ye(()=>Ko(o)).pValue;let N=Ng(l,f,s,c,g),{value:b,at:E}=yg(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 Ng(e,t,n,r,o){let s=e+t,a=new Float64Array(s+1);for(let v=2;v<=s;v++)a[v]=Rs(v);let c=t+1,i=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,A=-a[R]-a[e-R]-a[v-R]-a[t-v+R];f[M]=A,A>w&&(w=A)}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++)i[(N+M)*c+v-N-M]=m[M];else if(o==="greater")for(let M=0;M<E;M++)i[(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 A=f[R]*Am,T=-1,q=M;for(;q-T>1;){let O=T+q>>1;f[O]<=A?T=O:q=O}let C=M,_=E;for(;_-C>1;){let O=C+_>>1;f[O]<=A?_=O:C=O}let I=f[M]<=A?1:(T>=0?m[T]:0)+(_<E?u[_]:0);i[(N+R)*c+v-N-R]=Math.min(1,I)}}}let h=i[n*c+r]*Am,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(i[v*c+N]<=h){let b=N;for(;N+1<=t&&i[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 Tm(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]=gn(r,e,t,!1);let o=Dt(1,-t),s=ot([t,0],o),a=ot(o,[t,0]),c=s[0],i=a[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)*i);if(p===0)break;n[h]=p,f=h}let m=s[1]/c,u=a[1]/i;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 yg(e,t,n,r){let{starts:o,from:s,to:a}=e,c=new Float64Array(t+1),i=new Float64Array(n+1),l=new Float64Array(n+1),f=new Float64Array(n+1),m=b=>{let[E,w]=Tm(t,b,c),[y,x]=Tm(n,b,i),M=0;for(let A=y;A<=x;A++)l[A]=M+=i[A];M=0;for(let A=x;A>=y;A--)f[A]=M+=i[A];let R=0;for(let A=E;A<=w;A++){let T=c[A];if(T===0)continue;let q=0;for(let C=o[A];C<o[A+1];C++){let _=s[C],I=a[C];if(!(I<y||_>x))if(_===0)q+=l[Math.min(I,x)];else if(I===n)q+=f[Math.max(_,y)];else for(let O=Math.max(_,y);O<=Math.min(I,x);O++)q+=i[O]}R+=T*q}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=gg*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=Ql(T=>-m(h(T)),w,y,1e-14).x,R=m(h(M)),A=1e-4/Math.sqrt(2*(t+n));for(let T=0;T<2&&(M-A>w&&M+A<y);T++,A*=.01){let q=m(h(M-A)),C=m(h(M+A)),_=q-2*R+C;if(!(_<0))break;let I=A*(q-C)/(2*_);if(!(Math.abs(I)<A))break;let O=m(h(M+I));O>R*(1+2**-50)&&(M+=I,R=O)}R>d&&(d=R,v=h(M))}return{value:d,at:v}}var Sm=["correct"];function Mg(e,t,n={}){ct(t)&&K(t,Sm,"mcnemarTest"),K(n,Sm,"mcnemarTest");let r;if(e.length>0&&Array.isArray(e[0])){t!==void 0&&!Array.isArray(t)&&(n=t),r=e.map(f=>Array.from(f));let l=r.length;if(l<2||r.some(f=>f.length!==l))throw new RangeError("'x' must be square with at least two rows and columns");if(r.some(f=>f.some(m=>!Number.isFinite(m)||m<0)))throw new RangeError("all entries of 'x' must be nonnegative and finite")}else{if(!Array.isArray(t))throw new TypeError("'y' is required when 'x' is not a table");r=Eg(e,t)}let o=n.correct??!0,s=r.length,a,c=o&&s===2&&r[0][1]!==r[1][0];if(c){let l=r[0][1],f=r[1][0];a=km(Math.max(0,Math.abs(l-f)-1),l,f)}else{a=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&&(a+=km(u-h,u,h),l++)}if(l===0)throw new RangeError("the table has no discordant pairs")}let i=s*(s-1)/2;return{method:c?"McNemar's Chi-squared test with continuity correction":"McNemar's Chi-squared test",statistic:a,statisticName:"McNemar's chi-squared",parameter:i,parameterName:"df",pValue:vt(a,i/2,2,!1,!1),warnings:[]}}function km(e,t,n){let r=t+n;return r<1/0?$t(e,r):$t($(e,-520),$(t,-1040)+$(n,-1040))}function Eg(e,t){if(e.length!==t.length)throw new RangeError("'x' and 'y' must have the same length");let n=[],r=rn(e),o=rn(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(Kn),a=s(n.map(m=>m[0])),c=s(n.map(m=>m[1]));if(a.length<2||a.length!==c.length)throw new RangeError("'x' and 'y' must have the same number of levels (minimum 2)");let i=new Map(a.map((m,u)=>[m,u])),l=new Map(c.map((m,u)=>[m,u])),f=a.map(()=>new Array(a.length).fill(0));for(let[m,u]of n)f[i.get(m)][l.get(u)]++;return f}var xg=["ratio","alternative","confLevel"],Ui=2**-1022;function qm(e,t,n={}){if(K(n,xg,"varTest"),typeof e=="string"){let b=rr(e,t);return qm(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 a=Je(n.alternative),c=n.confLevel??.95;Lt(c);let i=r.length-1,l=o.length-1;if(i<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>=Ui&&m>=Ui)){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=pt(p,i,l,!0,!1),d=pt(p,i,l,!1,!1),v,N;if(a==="less")v=g,N=[0,h/pn(1-c,i,l,!0,!1)];else if(a==="greater")v=d,N=[h/pn(c,i,l,!0,!1),1/0];else{v=Math.min(1,2*Math.min(g,d));let b=(1-c)/2,E=b<1e-8?pn(b,i,l,!1,!1):pn(1-b,i,l,!0,!1);N=[h/E,h/pn(b,i,l,!0,!1)]}return{method:"F test to compare two variances",alternative:a,statistic:p,statisticName:"F",parameter:[i,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 Im(e,t){if(typeof e=="string"){let p=Oo(e,t);return Im(Gl(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),a=Tt(o).variance,c=0,i=0,l=0,f=0;for(let p=0;p<r;p++)c+=s[p],i+=s[p]*a[p],l+=s[p]*Math.log(a[p]),f+=1/s[p];i/=c;let m=c*Math.log(i)-l;if(a.some(p=>p<Ui)){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:vt(u,h/2,2,!1,!1),warnings:[]}}var Rg=["alternative"],Hi=["alternative"];function Yi(e,t,n,r){return{method:e,statistic:n,statisticName:t,parameter:r,parameterName:"df",pValue:vt(n,r/2,2,!1,!1),warnings:[]}}function _m(e){let t=0;for(let n of Nn(e))t+=n*n*n-n;return t}function sa(e,t){if(typeof e=="string"){let l=Oo(e,t);return sa(l.values,l.group.codes)}let n=Rr(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,a=yt(o),c=0,i=0;for(let l of n){let f=0;for(let m=0;m<l.length;m++)f+=a[i+m];i+=l.length,c+=f*f/l.length}return c=(12*c/(s*(s+1))-3*(s+1))/(1-_m(o)/(s*s*s-s)),Yi("Kruskal-Wallis rank sum test","Kruskal-Wallis chi-squared",c,r-1)}function Gi(e,t){if(typeof e=="string")return Gi(_s(e,t));let n=Ga(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),a=0;for(let l of n){let f=yt(l);for(let m=0;m<o;m++)s[m]+=f[m];a+=_m(l)}let c=0;for(let l of s)c+=(l-r*(o+1)/2)**2;let i=12*c/(r*o*(o+1)-a/(o-1));return Yi("Friedman rank sum test","Friedman chi-squared",i,o-1)}function Ag(e,t={},n,r={}){typeof e!="string"&&ct(t)&&K(t,Hi,"pageTest"),ct(n)&&K(n,Hi,"pageTest"),K(r,Hi,"pageTest");let o,s;if(typeof e=="string")o=_s(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=rn(d),b=rn(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(Kn),w=Array.from(new Set(b)).sort(Kn);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]),A=y.get(N[M]);if(!Number.isNaN(o[R][A]))throw new RangeError("not an unreplicated complete block design");o[R][A]=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 a=o.length,c=o[0]?.length??0;if(a<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 i=Je(s.alternative),l=new Array(c).fill(0);for(let g of o){let d=yt(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=a*c*(c+1)**2/4,u=a*c**2*(c-1)*(c+1)**2/144,h=(f-m-.5)/Math.sqrt(u),p=i==="less"?ae(h):i==="greater"?ae(h,{lowerTail:!1}):2*ae(-Math.abs(h));return{method:"Page's ordered aligned rank sum test",alternative:i,statistic:h,statisticName:"z",pValue:p,estimate:f,warnings:[]}}function Tg(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{...Gi(e),method:"Cochran's Q test",statisticName:"Q"}}function Lm(e,t){if(typeof e=="string")return Lm(_s(e,t));let n=Ga(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=>Et(g)-ln(g));s.some(g=>g===1/0)&&n.every(g=>g.every(Number.isFinite))&&(s=Ie(n).groups.map(g=>Et(g)-ln(g)));let a=yt(s),c=new Array(o).fill(0),i=0;n.forEach((g,d)=>{let v=yt(g);for(let N=0;N<o;N++){let b=a[d]*(v[N]-(o+1)/2);i+=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(i===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/(i-l),h=pt(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 Cm(e,t){if(typeof e=="string"){let h=Oo(e,t);return Cm(h.values,h.group.codes)}let n=Rr(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=Ds(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 a=s.length,i=yt(s).map(h=>ie((1+h/(a+1))/2)),l=0;for(let h of i)l+=h;let f=l/a,m=0,u=0;for(let h of n){let p=0;for(let g=0;g<h.length;g++)p+=i[u+g];u+=h.length,m+=h.length*(p/h.length-f)**2}return m/=at(i),Yi("Fligner-Killeen test of homogeneity of variances","Fligner-Killeen:med chi-squared",m,r-1)}function Om(e,t,n={}){if(K(n,Rg,"moodTest"),typeof e=="string"){let E=rr(e,t);return Om(E.x,E.y,n)}let r=Array.from(e).filter(Number.isFinite),o=Array.from(t).filter(Number.isFinite),s=Je(n.alternative),a=r.length,c=o.length,i=a+c;if(i<3)throw new RangeError("not enough observations");if(a===0||c===0)throw new RangeError("not enough observations in each sample");let l=r.concat(o),f=Array.from({length:i},(E,w)=>w).sort((E,w)=>l[E]-l[w]),m=new Array(i),u=0;for(;u<i;){let E=u;for(;E+1<i&&l[f[E+1]]===l[f[u]];)E++;let w=0;for(let y=u;y<=E;y++)w+=(y+1-(i+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<a;E++)h+=m[E];let p=0;for(let E of m)p+=E;p/=i;let g=0;for(let E of m)g+=(E-p)**2;let d=a*p,v=a*c/(i*(i-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 _r(e,t){let n=0;for(let r=e.length-1;r>=0;r--)n=n*t+e[r];return n}var Sg=[0,.221157,-.147981,-2.07119,4.434685,-2.706056],kg=[0,.042981,-.293762,-1.752461,5.682633,-3.582633],qg=[.544,-.39978,.025054,-6714e-7],Ig=[1.3822,-.77857,.062767,-.0020322],_g=[-1.5861,-.31082,-.083751,.0038915],Lg=[-.4803,-.082676,.0030302],Cg=[-2.273,.459];function Og(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),a=1/Math.sqrt(e),c=r[0]/s+_r(Sg,a),i=new Float64Array(n);i[0]=c;let l,f;if(e>5){let m=r[1]/s+_r(kg,a);i[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++)i[m]=r[m]/f;for(let m=0;m<n;m++)t[e-1-m]=i[m],t[m]=-i[m];return t}function Fm(e){let t=Ie([e]).groups[0],n=t.length,r=Og(n),o=t[n-1]-t[0],s=0;for(let f of t)s+=f/o;s/=n;let a=0,c=0,i=0;for(let f=0;f<n;f++){let m=t[f]/o-s;a+=r[f]*r[f],c+=m*m,i+=r[f]*m}let l=Math.sqrt(a*c);return(l-i)*(l+i)/(a*c)}var Fg=1.90985931710274,Dg=1.0471975511966;function Dm(e,t){if(Number.isNaN(e)||!(t>=3))return NaN;if(t===3){let a=Fg*(Math.asin(Math.sqrt(1-e))-Dg);return Math.max(a,0)}let n=Math.log(e);if(t<=11){let a=_r(Cg,t);if(n>=a)return 1e-99;let c=_r(qg,t),i=Math.exp(_r(Ig,t));return ae(-Math.log(a-n),{mean:c,sd:i,lowerTail:!1})}let r=Math.log(t),o=_r(_g,r),s=Math.exp(_r(Lg,r));return ae(n,{mean:o,sd:s,lowerTail:!1})}var jm=Math.log(2*Math.PI),Pm=2e-14,Vm=500;function Pg(e){return Math.exp(-.5*e*e-.5*jm)}function Vg(e){return ae(e,{logP:!0})}function zm(e,t=-1/0,n=!1){let r=t===-1/0?Ha(e,0,2,0,Pm,Vm):Ha(e,t,1,0,Pm,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 $m(e,t,n){let r=t.map(Math.sqrt),o=t.map(a=>Math.sqrt(1-a));return zm(a=>{let c=0;for(let i=0;i<r.length;i++){let l=(r[i]*a+e)/o[i];if(!n){c+=Vg(l);continue}let f=(r[i]*a-e)/o[i],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 Pg(a)*-Math.expm1(c)})}function Lr(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,$m(e,t,r));let o=n/2,s=Math.log(2)+.5*Math.log(o)-.5*jm-Al(o);return Math.min(1,zm(c=>{let i=s-o*(Math.expm1(2*c)-2*c);return i<-745?0:Math.exp(i)*$m(e*Math.exp(c),t,r)},-1/0,!0))}var $g=["pAdjustMethod"],jg=["pAdjustMethod","alternative"],zg=["alternative"],Kg=["pAdjustMethod","alternative"],Wg=["method","altp"],Bg=["alternative"],Ug=["welch"],Km="Ties are present. z-quantiles were corrected for ties.";function In(e,t){let{labels:n,groups:r}=or(e,t),o=r.length;if(o<2)throw new RangeError("there must be at least two groups");let s=r.map(a=>a.length);return{labels:n,groups:r,n:s,N:s.reduce((a,c)=>a+c,0),k:o}}function Xi(e){let t=e.groups.flat(),n=yt(t),r=[],o=0;for(let a of e.n){let c=0;for(let i=0;i<a;i++)c+=n[o+i];o+=a,r.push(c/a)}let s=0;for(let a of Nn(t))s+=a*a*a-a;return{R:r,C:s/(12*(e.N-1))}}function Cr(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 Hg(e,t){let n=e.length+1,r=[];for(let c=0;c<n-1;c++)for(let i=c+1;i<n;i++)r.push(e[i-1][c]);let o=ro(r,t),s=0,a=e.map(c=>c.slice());for(let c=0;c<n-1;c++)for(let i=c+1;i<n;i++)a[i-1][c]=o[s++];return a}function Go(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 Qi(e,t,n,r,o,s=[]){return{method:t,statistic:n.map(a=>[a]),pValue:r.map(a=>[a]),rowNames:e.labels.slice(1),colNames:e.labels.slice(0,1),pAdjustMethod:o,warnings:s}}var aa=e=>ae(e,{lowerTail:!1}),Zi=(e,t,n)=>As(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 Xo(n,t.ssWithin),{m:t.mean,s2:t.ssWithin/n,df:e.N-e.k,k:t.k}}function Xo(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 Yg(e,t,n={}){K(n,$g,"kwAllPairsDunnTest");let r=n.pAdjustMethod??"holm",o=In(e,t),{R:s,C:a}=Xi(o),c=o.N*(o.N+1)/12-a,i=Cr(o.k,(f,m)=>Math.abs(s[f]-s[m])/Math.sqrt(c*(1/o.n[f]+1/o.n[m]))),l=Hg(i.map(f=>f.map(m=>2*aa(m))),r);return J("kwAllPairsDunnTest",Go(o,"Dunn's all-pairs test",i,l,r,a!==0?[Km]:[]))}function Gg(e,t,n={}){K(n,jg,"kwManyOneDunnTest");let r=Je(n.alternative),o=n.pAdjustMethod??"single-step",s=In(e,t),{R:a,C:c}=Xi(s),i=s.N*(s.N+1)/12-c,l=s.n.slice(1).map((h,p)=>p+1),f=l.map(h=>(a[h]-a[0])/Math.sqrt(i*(1/s.n[0]+1/s.n[h]))),m=l.map(h=>s.n[h]/(s.n[h]+s.n[0])),u=Wm(f,m,r,o);return J("kwManyOneDunnTest",Qi(s,"Dunn's many-to-one test",f,u,o,c!==0?[Km]:[]))}function Wm(e,t,n,r){return r==="single-step"?e.map(o=>n==="two.sided"?Lr(o,t,1/0,!0):Lr(n==="greater"?o:-o,t,1/0,!1)):ro(e.map(o=>n==="two.sided"?2*aa(Math.abs(o)):aa(n==="greater"?o:-o)),r)}function Xg(e,t,n={}){K(n,zg,"dunnettTest");let r=Je(n.alternative),o=In(e,t),{m:s,s2:a,df:c}=Ji(o),i=Math.sqrt(a),l=o.n.slice(1).map((h,p)=>p+1),f=l.map(h=>(s[h]-s[0])/(i*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*Lr(Math.abs(h),m,c,!1)):Lr(r==="greater"?h:-h,m,c,!1));return J("dunnettTest",Qi(o,`Dunnett's-test for multiple
4
- comparisons with one control`,f,u,"single-step"))}function Qg(e,t,n={}){K(n,Kg,"manyOneUTest");let r=Je(n.alternative),o=n.pAdjustMethod??"single-step",s=In(e,t);if(o==="single-step"&&s.k<3)throw new RangeError(`for 'p.adjust.method = single-step' at least
1
+ /*! rStats.js 1.17.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 en,b as Vn,c as Zn,d as dr,e as ws,f as va}from"./chunks/chunk-QCPGV7XH.js";import{a as xe,b as qe,c as bn,d as it,e as No,f as bh,g as vh,h as wa,i as ba,j as Nh,k as il,l as vs,m as Jn,n as Ns,o as fl,p as wr,q as ml,r as Mh,s as Eh,t as Ma,u as Ea,v as pl,w as dl,x as gl,y as xa,z as kh}from"./chunks/chunk-EIUK6TKF.js";import{a as gn,c as on,h as $r,i as jc,m as Kc,n as zr,o as Wc,s as as,t as Yc,u as go,v as is,w as cs,x as ls}from"./chunks/chunk-T5ZALLTJ.js";import{b as Qc}from"./chunks/chunk-GOYTZ5RB.js";import{a as Ra,e as Ta}from"./chunks/chunk-L77QFL3C.js";import{a as ms,b as Qn,c as vo,d as el,e as tl,f as us,g as Ge,h as an,i as wn,j as It,k as ol,l as yh,n as Ms,o as Mo}from"./chunks/chunk-NCDPZSI2.js";import{a as Es}from"./chunks/chunk-TTOJDLIQ.js";import"./chunks/chunk-V54MBEJM.js";import"./chunks/chunk-ZRKXHVTJ.js";import{a as zc,d as ss}from"./chunks/chunk-3UEYA6VG.js";import{b as un,c as hl}from"./chunks/chunk-3Y4DNK4O.js";import{a as qh,b as Ih,c as _h}from"./chunks/chunk-PSQ6MLJM.js";import{f as ll,g as br,h as er,i as ys,k as yo,l as ul,m as ya,n as xh,o as Rh,q as Th,r as Sh,s as Ah}from"./chunks/chunk-O4D43LWM.js";import{b as Mt,c as Cn}from"./chunks/chunk-5W2OZAHQ.js";import{b as Eo}from"./chunks/chunk-VZSQAGL2.js";import{a as nl,c as rl,e as pr}from"./chunks/chunk-LKKUUREA.js";import{b as ga}from"./chunks/chunk-DD2GG4EC.js";import{a as at,b as hs,c as ps}from"./chunks/chunk-6MLDKHTY.js";import{c as gs,d as Na}from"./chunks/chunk-EB45CDFA.js";import{a as gr,b as Kr}from"./chunks/chunk-XR7Y4ZV6.js";import{d as Zo,e as Jo,g as es,h as Pr,m as fn,n as On}from"./chunks/chunk-4BGGWQWS.js";import{d as wt,e as ln,g as xn,h as Vr,k as Yn}from"./chunks/chunk-SA2E5GVO.js";import{d as rt,h as cl}from"./chunks/chunk-XPYZOG5H.js";import{c as sl,j as jt,m as al,n as ds,t as bs}from"./chunks/chunk-K3YSXDTU.js";import{a as Xn,b as Jc,g as jr}from"./chunks/chunk-KY53MB3J.js";import{a as da,b as vt,c as xt,e as dn,f as rn}from"./chunks/chunk-BWDXJL7T.js";import{i as gt,l as Qo,t as Pc}from"./chunks/chunk-23QCIPET.js";import{a as Rn,c as fs}from"./chunks/chunk-RM5D7Q7W.js";import{b as Bc,c as Uc,d as Gc,e as Hc,f as Xc,i as wo,l as wh,n as H,o as je,p as mt}from"./chunks/chunk-NCRL33PQ.js";import{a as bo,b as Pn,c as Zc,d as Ot,e as Rt}from"./chunks/chunk-JHABMYNH.js";import{a as At,b as Hn,c as sn}from"./chunks/chunk-I2KJMF4G.js";import"./chunks/chunk-72I7GDWL.js";import{b as Oc,c as Fc}from"./chunks/chunk-5UFVACSV.js";import{h as _c,i as Lc,k as Cc,t as Dc}from"./chunks/chunk-LY2UZGUL.js";import{f as ha,r as Xt,s as ie,t as oe}from"./chunks/chunk-XEJX5HBJ.js";import{A as Dt,B as os,C as Re,e as Fn,f as mn,g as Dn,h as $,i as et,j as Bn,k as st,l as ot,m as Un,n as Vc,p as ts,q as Et,r as ns,s as $c,t as rs,u as pa,v as Ie,w as bt,x as pn,y as Ct,z as Gn}from"./chunks/chunk-YMI33F7W.js";import{b as $t,c as Ht,d as ue,e as qc,f as Ce,g as _e,h as zt,i as lt,j as ft,k as Ic,l as V,m as Ft}from"./chunks/chunk-GTSK4IN4.js";var wl=["alternative","mu","paired","varEqual","confLevel"];function vn(e,t,n={}){if(typeof e!="string"&&at(t)&&V(t,wl,"tTest"),V(n,wl,"tTest"),typeof e=="string")return Lh(e,t,n);let r;t!==void 0&&!ArrayBuffer.isView(t)&&!Array.isArray(t)?n=t:r=t;let o=Ge(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(an(a),i&&r===void 0)throw new TypeError("y is required when paired is true");let l,f,m;if(i){let[k,L]=Gn(Array.from(e),Array.from(r));({d:l,k:m}=ns(k,L))}else{let k=Re(r===void 0?[Ct(Array.from(e))]:[Ct(Array.from(e)),Ct(Array.from(r))]);m=k.k,l=k.groups[0],f=k.groups[1]}let u=$(s,-m),h,p,d,g,w,N,v,E=l.length,b=f!==void 0&&c;if(E<1||!b&&E<2)throw new Error("not enough 'x' observations");let M=st(l),x=E>1?ot(l):NaN;if(f===void 0){if(p=E-1,d=Math.sqrt(x/E),d===0||d<10*Number.EPSILON*Math.abs(M))throw new Error("data are essentially constant");h=(M-u)/d,N=i?"Paired t-test":"One Sample t-test",g=M,w=i?"mean difference":"mean of x",v=i?"mean difference":"mean"}else{let k=f.length;if(k<1||!c&&k<2)throw new Error("not enough 'y' observations");if(c&&E+k<3)throw new Error("not enough observations");let L=st(f),_=k>1?ot(f):NaN;if(N=`${c?"":"Welch"} Two Sample t-test`,g=[M,L],w=["mean of x","mean of y"],v="difference in means",c){p=E+k-2;let I=0;E>1&&(I+=(E-1)*x),k>1&&(I+=(k-1)*_),I/=p,d=Math.sqrt(I*(1/E+1/k))}else{let I=x/E,F=_/k;d=Math.sqrt(Math.sqrt(I)**2+Math.sqrt(F)**2),p=rs([I,F],[E-1,k-1])}if(d===0||d<10*Number.EPSILON*Math.max(Math.abs(M),Math.abs(L)))throw new Error("data are essentially constant");h=(M-L-u)/d}let y=typeof g=="number"?g:g[0]-g[1],R=(k,L)=>{let _=u+(h+L*k)*d;return Number.isFinite(_)&&Math.max(Math.abs(u),Math.abs(h*d))<=16*(Math.abs(y)+k*d)?_:y+L*k*d},T,S;if(o==="less")T=vt(h,p,!0,!1),S=[-1/0,R(xt(a,p,!0,!1),1)];else if(o==="greater")T=vt(h,p,!1,!1),S=[R(xt(a,p,!0,!1),-1),1/0];else{T=2*vt(-Math.abs(h),p,!0,!1);let k=1-a,L=k/2<1e-8?xt(k/2,p,!1,!1):xt(1-k/2,p,!0,!1);S=[R(L,-1),R(L,1)]}return S=[$(S[0],m),$(S[1],m)],g=typeof g=="number"?$(g,m):g.map(k=>$(k,m)),d=$(d,m),{method:N,alternative:o,statistic:h,statisticName:"t",parameter:p,parameterName:"df",pValue:T,confInt:S,confLevel:a,estimate:g,estimateNames:w,nullValue:s,nullValueName:v,stdErr:d,stdError:d,warnings:[]}}function Lh(e,t,n){if(n.paired!==void 0)throw new Error("cannot use 'paired' in formula method");let r=ms(e,t,!0);if(r.kind==="one")return vn(r.x,n);if(r.kind==="paired")return vn(r.x,r.y,{...n,paired:!0});let[o,s]=r.levels;return{...vn(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 Ch=["correct","p","rescaleP"],Oh=["confLevel"],Fh=["alternative","rowScores","colScores"];function Qt(e){let t=[0,0];for(let n of e)t=ue(t,[n,0]);return t}function Aa(e){let t=e.map(r=>{if(typeof r!="object"||r===null||typeof r.length!="number")throw new TypeError("'x' must be an array of numbers or an array of rows");return Array.from(r)}),n=t[0].length;if(t.some(r=>r.length!==n))throw new TypeError("all rows of the matrix must have the same length");if(t.flat().some(r=>typeof r!="number"))throw new TypeError("the entries of 'x' must be numbers");return t}var ka=e=>e.length>0&&typeof e[0]!="number",qa=(e,t)=>Number.isNaN(e)?NaN:gt(e,t/2,2,!1,!1);function bl(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 xo(e,t){return e.length>0&&Array.isArray(e[0])?e.map((n,r)=>n.map((o,s)=>bl(o,t[r][s]))):e.map((n,r)=>bl(n,t[r]))}var Dh=["none","williams","yates"],Nl="Log likelihood ratio (G-test)";function Ph(e,t={}){V(t,Ch,"gTest");let n=xe(t.correct,Dh);if(typeof e!="object"||e===null||typeof e.length!="number")throw new TypeError("'x' must be an array of counts or of rows");if(ka(e)){let o=Aa(e);return vl(o.flat()),o.length===1?H("gTest",Sa(o[0],n,t)):o[0].length===1?H("gTest",Sa(o.map(s=>s[0]),n,t)):H("gTest",Vh(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 vl(r),H("gTest",Sa(r,n,t))}function vl(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 yl(e,t){return e<1/0?0:Math.ceil(Math.log2(t))+1}function Ml(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=ue(n,Lc(o,s))}return n[0]+n[1]}function Vh(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=Qt(e.flat()),s=yl(o[0],n*r),i=s===0?e:e.map(v=>v.map(E=>$(E,-s))),c=s===0?o:Qt(i.flat()),a=i.map(v=>Qt(v)),l=i[0].map((v,E)=>Qt(i.map(b=>b[E]))),f=a.map(v=>l.map(E=>_e(v,lt(E,c)))),m=i;if(t==="yates"){let[[v,E],[b,M]]=i,x=v*M-E*b>0?-.5:.5,y=$(.5,-s),R=x<0?-y:y;m=[[v+R,E-R],[b-R,M+R]]}let u=Ml(m.flat(),f.flat()),h=Number.isNaN(u)?["NaNs produced"]:[],p=1;if(t==="williams"){if(a.some(M=>M[0]===0)||l.some(M=>M[0]===0))throw new RangeError("Williams' correction is not defined for a table with an empty row or column");let v=c[0]+c[1],E=a.reduce((M,x)=>M+1/(x[0]+x[1]),0),b=l.reduce((M,x)=>M+1/(x[0]+x[1]),0);p=1+$((v*E-1)*(v*b-1)/(6*v*(n-1)*(r-1)),-s)}let d=$(2*u,s)/p,g=(n-1)*(r-1),w=f.map(v=>v.map(E=>$(E[0]+E[1],s)));return{method:`${Nl} test of independence ${t==="none"?"without correction":t==="williams"?"with Williams' correction":"with Yates' correction"}`,statistic:d,statisticName:"G",parameter:g,parameterName:"X-squared df",pValue:qa(d,g),observed:s===0?m:m.map(v=>v.map(E=>$(E,s))),expected:w,ftResiduals:xo(e,w),warnings:h}}function Sa(e,t,n){let r=e.length;if(r<2)throw new RangeError("'x' must have at least 2 elements");if(t==="yates"&&r!==2)throw new RangeError("Yates' correction requires 2 data values");let o=n.rescaleP??!1;if(typeof o!="boolean")throw new TypeError("'rescaleP' must be a boolean");let s=n.p===void 0?e.map(()=>1/r):Array.from(n.p);if(s.length!==r)throw new RangeError("'x' and 'p' must have the same number of elements");if(s.some(v=>typeof v!="number"||Number.isNaN(v)))throw new RangeError("'p' must not contain missing values");if(s.some(v=>v<0))throw new RangeError("probabilities must be non-negative");if(s.some(v=>v===1/0))throw new RangeError("probabilities must be finite");let i=Qt(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=Qt(e),a=yl(c[0],r),l=a===0?e:e.map(v=>$(v,-a)),f=a===0?c:Qt(l),m=s.map(v=>o?lt(zt(f,v),i):zt(f,v)),u=m.map(v=>v[0]+v[1]),h=l;if(t==="yates"){let v=$(.5,-a),E=l[0]-u[0];E>$(.25,-a)?h=[l[0]-v,l[1]+v]:-E>$(.25,-a)&&(h=[l[0]+v,l[1]-v])}let p=Ml(h,m);if(!o&&!Number.isNaN(p)){let v=f[0]+f[1];p-=v*(i[0]-1+i[1])}let d=Number.isNaN(p)?["NaNs produced"]:[],g=t==="williams"?1+(r+1)/(6*$(f[0]+f[1],a)):1,w=$(2*p,a)/g,N=u.map(v=>$(v,a));return{method:`${Nl} goodness of fit test`,statistic:w,statisticName:"G",parameter:r-1,parameterName:"X-squared df",pValue:qa(w,r-1),observed:a===0?h:h.map(v=>$(v,a)),expected:N,ftResiduals:xo(e,N),warnings:d}}function $h(e){if(typeof e!="object"||e===null||!ka(e))throw new RangeError("'x' must be square with at least two rows and columns");let t=Aa(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 zh(e){let t=$h(e),n=Qt(t.flat())[0],r=n>2**400||n>0&&n<2**-400?Math.round(Math.log2(n)):0,o=r===0?t:t.map(b=>b.map(M=>$(M,-r))),s=o.length,i=o.map(b=>Qt(b)),c=o.map((b,M)=>Qt(o.map(x=>x[M]))),a=o.map((b,M)=>o[M].some((x,y)=>y!==M&&x>0)||o.some((x,y)=>y!==M&&x[M]>0)).flatMap((b,M)=>b?[M]:[]),l=a.length;if(l<2)throw new RangeError("Too many equal marginals, cannot compute");let f=a.map((b,M)=>M),m=b=>f[b]===b?b:f[b]=m(f[b]);for(let b=0;b<l;b++)for(let M=b+1;M<l;M++)o[a[b]][a[M]]+o[a[M]][a[b]]>0&&(f[m(b)]=m(M));if(a.some((b,M)=>m(M)!==m(0)))throw new RangeError("the categories fall into groups without counts between them: the covariance matrix is singular");let u=l-1,h=a.slice(0,u).map(b=>{let M=ue(i[b],[-c[b][0],-c[b][1]]);return M[0]+M[1]}),p=a.slice(0,u).map(b=>a.slice(0,u).map(M=>{if(b!==M)return-(o[b][M]+o[M][b]);let x=0;for(let y=0;y<s;y++)y!==b&&(x+=o[b][y]+o[y][b]);return x})),d=p.map(b=>b.map(()=>0)),g=[];for(let b=0;b<u;b++){let M=p[b][b];for(let x=0;x<b;x++)M-=d[b][x]*d[b][x]*g[x];g.push(M);for(let x=b+1;x<u;x++){let y=p[x][b];for(let R=0;R<b;R++)y-=d[x][R]*d[b][R]*g[R];d[x][b]=y/M}}let w=0,N=[];for(let b=0;b<u;b++){let M=h[b];for(let x=0;x<b;x++)M-=d[b][x]*N[x];N.push(M),w+=M*M/g[b]}let v=Qt(a.flatMap(b=>a.map(M=>t[b][M]))),E=l-1;return w=$(w,r),H("stuartMaxwellTest",{method:"Stuart-Maxwell test",statistic:w,statisticName:"chi-squared",parameter:E,parameterName:"df",pValue:qa(w,E),N:v[0]+v[1],warnings:[]})}function jh(e,t={}){V(t,Oh,"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=El(e),o=Qt(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(b=>b.map(M=>$(M,-s))),{pc:c,pd:a}=Kh(i),l=[0,0],f=[0,0];i.forEach((b,M)=>b.forEach((x,y)=>{l=ue(l,zt(c[M][y],x)),f=ue(f,zt(a[M][y],x))})),l=[l[0]/2,l[1]/2],f=[f[0]/2,f[1]/2];let m=ue(l,f),u=lt(Ce(l,f),m)[0],h=_e(m,m),p=i.map((b,M)=>b.map((x,y)=>2*lt(Ce(_e(f,c[M][y]),_e(l,a[M][y])),h)[0])),d=Math.max(...p.flat().map(Math.abs)),g=d>0&&d<1/0?Math.floor(Math.log2(d)):0,w=0;i.forEach((b,M)=>b.forEach((x,y)=>{let R=$(p[M][y],-g);w+=x*R*R}));let N=$(Math.sqrt(w),g-s/2),v=(1-n)/2,E=v<1e-8?oe(v,{lowerTail:!1}):oe(1-v);return H("goodmanKruskalGamma",{estimate:u,confInt:[Math.max(-1,u-E*N),Math.min(1,u+E*N)],confLevel:n,ase:N,warnings:[]})}function El(e){if(typeof e!="object"||e===null||!ka(e))throw new TypeError("'table' must be an array of rows");let t=Aa(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 Kh(e){let t=e.length,n=e[0].length,r=[0,0],o=e.map(p=>{let d=[],g=r;for(let w=0;w<n;w++)d.push(g),g=ue(g,[p[w],0]);return d}),s=e.map(p=>{let d=new Array(n),g=r;for(let w=n-1;w>=0;w--)d[w]=g,g=ue(g,[p[w],0]);return d}),i=p=>{let d=[],g=new Array(n).fill(r);for(let w=0;w<t;w++)d.push(g),g=g.map((N,v)=>ue(N,p[w][v]));return d},c=p=>{let d=new Array(t),g=new Array(n).fill(r);for(let w=t-1;w>=0;w--)d[w]=g,g=g.map((N,v)=>ue(N,p[w][v]));return d},a=i(o),l=c(s),f=i(s),m=c(o),u=a.map((p,d)=>p.map((g,w)=>ue(g,l[d][w]))),h=f.map((p,d)=>p.map((g,w)=>ue(g,m[d][w])));return{pc:u,pd:h}}function Wh(e,t={}){V(t,Fh,"lblTest");let n=Ge(t.alternative),r=El(e),o=r.length,s=r[0].length,i=(R,T,S)=>{if(R===void 0)return Array.from({length:T},(L,_)=>_+1);let k=Array.from(R);if(k.length!==T)throw new RangeError(`'${S}' must have one score for each ${S==="rowScores"?"row":"column"}`);if(k.some(L=>typeof L!="number"||!Number.isFinite(L)))throw new RangeError(`'${S}' must be finite numbers`);return k},c=i(t.rowScores,o,"rowScores"),a=i(t.colScores,s,"colScores"),f=Qt(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=>Qt(R)).map(R=>R[0]+R[1]),h=m[0].map((R,T)=>Qt(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 d=u.reduce((R,T,S)=>R+T*c[S],0)/p,g=h.reduce((R,T,S)=>R+T*a[S],0)/p,w=c.map(R=>R-d),N=a.map(R=>R-g),v=u.reduce((R,T,S)=>R+T*w[S]*w[S],0),E=h.reduce((R,T,S)=>R+T*N[S]*N[S],0),b=0;if(m.forEach((R,T)=>R.forEach((S,k)=>b+=S*w[T]*N[k])),!(v>0&&E>0))throw new RangeError("the row and column scores must vary over the observations (the variance of the statistic is 0)");let M=b/Math.sqrt(v)/Math.sqrt(E),x=f===0?Math.sqrt(p-1)*M:$(Math.sqrt(p-$(1,-f))*M,f/2),y=n==="less"?ie(x):n==="greater"?ie(x,{lowerTail:!1}):2*ie(-Math.abs(x));return H("lblTest",{method:"Linear-by-Linear Association Test",alternative:n,statistic:x,statisticName:"Z",pValue:y,warnings:[]})}var xl=["correct","p","rescaleP","simulatePValue","B","nMinus1"];function hn(e){let t=[0,0],n=0;for(let r of e)t=ue(t,[r,0]),n+=r;return Number.isFinite(n)?t[0]:n}function Yh(e,t,n){return at(t)&&V(t,xl,"chisqTest"),V(n,xl,"chisqTest"),Array.isArray(t)?H("chisqTest",Bh(e,t,n??{})):H("chisqTest",Uh(e,t??{}))}var Sl="Chi-squared approximation may be incorrect";function Bh(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(Rn),c=[...new Set(s)].sort(Rn);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])]++),{..._l(f,n),rowLevels:i,colLevels:c}}function Uh(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?Ia(r[0],t):o===1?Ia(r.map(s=>s[0]),t):_l(r,t)}return Ia(Array.from(e),t)}function Al(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 kl(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 ql(e,t){return Number.isNaN(e)?NaN:gt(e,t/2,2,!1,!1)}function Il(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 Ia(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(d=>!(d>=0&&d<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=hn(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(d=>d<0))throw new RangeError("probabilities must be non-negative");if(o.some(d=>d===1/0))throw new RangeError("probabilities must be finite");if(t.rescaleP??!1){let d=hn(o),g=d<1/0?0:Math.ceil(Math.log2(n)),w=g===0?o:o.map(v=>$(v,-g)),N=g===0?d:hn(w);o=w.map(v=>v/N)}else if(Math.abs(hn(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(d=>$(d,-s)),c=s===0?r:hn(i),a=o.map(d=>c*d),l=s===0?a:a.map(d=>$(d,s));Il(l);let f=o.map((d,g)=>a[g]*(1-d)),m=$(hn(i.map((d,g)=>Dt(d-a[g],a[g]))),s),u="Chi-squared test for given probabilities",h=n-1,p;if(t.simulatePValue??!1){let d=Al(t);if(!Number.isInteger(r))throw new RangeError("the simulation needs counts summing to an integer");u+=bo(d),h=NaN,p=kl(Gh(r,o,l,d),m,d)}else p=ql(m,h);return{method:u,statistic:m,statisticName:"X-squared",parameter:h,parameterName:"df",pValue:p,observed:e,expected:l,residuals:i.map((d,g)=>$((d-a[g])/Math.sqrt(a[g]),s/2)),stdres:i.map((d,g)=>$((d-a[g])/Math.sqrt(f[g]),s/2)),ftResiduals:xo(e,l),warnings:Number.isFinite(h)&&l.some(d=>d<5)?[Sl]:[]}}function Gh(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=Qc(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 Hh(e,t,n,r){let o=e.length,s=t.length,i=e.reduce((f,m)=>f+m,0);if(i>is)throw new RangeError("the simulation needs a total count of at most 16777216 (2^24)");let c=cs(i),a=new Int32Array(o*s),l=new Float64Array(Math.trunc(r));for(let f=0;f<l.length;f++){ls(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 _l(e,t){let n=t.nMinus1??!1;if(typeof n!="boolean")throw new TypeError("'nMinus1' must be a boolean");if(n&&t.correct===!0)throw new RangeError("'nMinus1' uses no continuity correction: leave 'correct' out or set it to false");if(n&&(t.simulatePValue??!1))throw new RangeError("'nMinus1' cannot be combined with 'simulatePValue'");let r=!n&&(t.correct??!0),o=e.length,s=e[0].length,i=e.flat();if(i.some(v=>!(v>=0&&v<1/0)))throw new RangeError("all entries of 'x' must be non-negative and finite");let c=hn(i);if(!(c>0))throw new RangeError("at least one entry of 'x' must be positive");if(n&&!(c>1))throw new RangeError("'nMinus1' needs a table with a total above 1");let a=e.map(v=>hn(v)),l=e[0].map((v,E)=>hn(e.map(b=>b[E]))),f=a.map(v=>l.map(E=>os(v,E,c)));Il(f.flat());let m=e.map((v,E)=>v.map((b,M)=>b-f[E][M])),u="Pearson's Chi-squared test",h,p=(o-1)*(s-1),d,g=(t.simulatePValue??!1)&&a.every(v=>v>0)&&l.every(v=>v>0),w=!g&&r&&o===2&&s===2?Math.min(.5,...m.flat().map(Math.abs)):0,N=[];if((t.simulatePValue??!1)&&!g&&N.push("cannot compute simulated p-value with zero marginals"),g){if(e.some(b=>b.some(M=>!Number.isInteger(M))))throw new RangeError("the simulation needs integer counts");let v=Al(t);u+=bo(v),h=hn(m.flatMap((b,M)=>b.map((x,y)=>Dt(x,f[M][y])))),p=NaN;let E=Hh(a.map(b=>Math.trunc(b)),l.map(b=>Math.trunc(b)),f,v);d=kl(E,h,v)}else if(w>0)u+=" with Yates' continuity correction",h=hn(m.flatMap((v,E)=>v.map((b,M)=>Dt(Math.abs(b)-w,f[E][M]))));else if(h=hn(m.flatMap((v,E)=>v.map((b,M)=>Dt(b,f[E][M])))),n){u="Pearson's 'N-1' Chi-squared test";let v=h*(c-1);h=Number.isFinite(v)?v/c:h*((c-1)/c)}return{method:u,statistic:h,statisticName:"X-squared",parameter:p,parameterName:"df",pValue:d??ql(h,p),observed:e,expected:f,residuals:m.map((v,E)=>v.map((b,M)=>b/Math.sqrt(f[E][M]))),stdres:Xh(e,c,a,l,f,m),ftResiduals:xo(e,f),warnings:Number.isFinite(p)&&f.some(v=>v.some(E=>E<5))?[...N,Sl]:N}}function Xh(e,t,n,r,o,s){if(!Number.isFinite(t))return s.map((p,d)=>p.map((g,w)=>g/Math.sqrt(o[d][w]*(1-n[d]/t)*(1-r[w]/t))));let i=e.map(p=>Rl(p)),c=e[0].map((p,d)=>Rl(e.map(g=>g[d]))),a=Zh(i),l=Tl(i),f=Tl(c),m=l.map(p=>lt(p,a)),u=c.map(p=>lt(p,a)),h=f.map(p=>lt(p,a));return e.map((p,d)=>p.map((g,w)=>{let N=_e(i[d],u[w]),v=Ce([g,0],N),E=v[0]+v[1],b=_e(_e(N,m[d]),h[w]),M=b[0]+b[1];if(M>=Qh||E===0)return E/Math.sqrt(M);let x=N[0]+N[1],y=m[d][0]+m[d][1],R=h[w][0]+h[w][1];return E/Math.sqrt(x)/Math.sqrt(y)/Math.sqrt(R)}))}var Qh=2**-1022;function Rl(e){let t=[0,0];for(let n of e)t=ue(t,[n,0]);return t}function Zh(e){let t=[0,0];for(let n of e)t=ue(t,n);return t}function Tl(e){let t=e.length,n=new Array(t+1);n[t]=[0,0];for(let s=t-1;s>=0;s--)n[s]=ue(e[s],n[s+1]);let r=[],o=[0,0];for(let s=0;s<t;s++)r.push(ue(o,n[s+1])),o=ue(o,e[s]);return r}var Jh=Math.pow(Number.EPSILON,.25);function Kt(e,t,n,r={}){let o={tol:r.tol??Jh,maxiter:r.maxIter??1e3};return r.fLower!==void 0&&(o.fLower=r.fLower),r.fUpper!==void 0&&(o.fUpper=r.fUpper),pr(e,t,n,o).root}function tr(e,t){let n=a=>e(Math.exp(a)),r=0,o=n(0),s=t?-1:1,i=n(s);for(;i!==0&&Math.sign(i)===Math.sign(o);){if(Math.abs(s)>745)return t?0:1/0;r=s,o=i,s*=2,i=n(s)}if(i===0)return Math.exp(s);let c=r<s?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 ep=["alternative","confInt","confLevel","or","simulatePValue","B","midp"];function So(e,t={}){V(t,ep,"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(Fc));if(s.flat().some(f=>f>2147483647))throw new RangeError("'x' has entries too large to be integer");let i=[],c=tp(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 H("fisherTest",{...sp(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 H("fisherTest",{method:a+bo(f),alternative:l,pValue:np(s,f),warnings:i})}return H("fisherTest",{method:a,alternative:l,pValue:ap(s),warnings:i})}function tp(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: ${Zc(a)}`:null}function np(e,t){let n=e.filter(g=>g.some(w=>w>0)),r=e[0].map((g,w)=>w).filter(g=>e.some(w=>w[g]>0));if(n.length<=1)throw new RangeError("need 2 or more non-zero row marginals");if(r.length<=1)throw new RangeError("need 2 or more non-zero column marginals");let o=n.map(g=>r.map(w=>g[w])),s=o.map(g=>g.reduce((w,N)=>w+N,0)),i=o[0].map((g,w)=>o.reduce((N,v)=>N+v[w],0)),c=s.length,a=i.length,l=[0,0];for(let g of o.flat())l=ue(l,[-ss(g),0]);let f=l[0]/(1+64*Number.EPSILON),m=s.reduce((g,w)=>g+w,0);if(m>is)throw new RangeError("the simulation needs a total count of at most 16777216 (2^24)");let u=cs(m),h=new Int32Array(c*a),p=0,d=Math.trunc(t);for(let g=0;g<d;g++){ls(s,i,u,h);let w=0;for(let N=0;N<h.length;N++)w-=u[h[N]];w<=f&&p++}return Math.max(0,Math.min(1,(1+p)/(d+1)))}var Ll=1+1e-7,rp=16,op=2**24;function Ro(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=ue(r,[e[o],0]));return r[0]}var Wr=class{m;n;k;lo;hi;logCentral;constructor(t,n,r){this.m=t,this.n=n,this.k=r,this.lo=Math.max(0,r-n),this.hi=Math.min(r,t),this.logCentral=new Float64Array(this.hi-this.lo+1);let o=0,s=1;for(let i=this.lo;i<=this.hi;i++){let c=i-this.lo;c%rp===0||!(s>0&&s<1/0)?(o=gn(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=Ro(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=ue(r,[(this.lo+o)*n[o],0]));return r[0]}tail(t,n,r){if(n===1)return r?on(t-1,this.m,this.n,this.k,!1,!1):on(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?Ro(o,s,o.length-1):Ro(o,0,s)}midTail(t,n,r){if(n===1){let i=.5*gn(t,this.m,this.n,this.k,!1);return(r?on(t,this.m,this.n,this.k,!1,!1):on(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?Ro(o,s+1,o.length-1):Ro(o,0,s-1))+.5*o[s]}};function La(e,t,n){let r=e.density(n),o=r[t-e.lo]*Ll,s=[0,0];for(let i of r)i<=o&&(s=ue(s,[i,0]));return s[0]}function Cl(e,t,n,r){let o=Math.max(0,r-n),s=Math.min(r,t),i=d=>gn(d,t,n,r,!0),c=i(e)+Math.log(Ll),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=(d,g)=>{let w=d,N=g+1;for(;N-w>1;){let v=w+Math.floor((N-w)/2);i(v)<=c?w=v:N=v}return w},f=(d,g)=>{let w=d-1,N=g+1;for(;N-w>1;){let v=w+Math.floor((N-w)/2);i(v)<=c?N=v:w=v}return N},m,u;e<=a?(m=l(e,a),u=f(a+1,s)):(u=f(a,e),m=a-1<o||i(o)>c?o-1:l(o,a-1));let h=m<o?0:on(m,t,n,r,!0,!1),p=u>s?0:on(u-1,t,n,r,!1,!1);return h+p}function To(e,t){return tr(e,t)}function sp(e,t,n){let r=Ge(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>op)throw new RangeError(`the exact distribution of the 2 x 2 table has ${m} values, more than 2^24`);let u=new Wr(i+a,c+l,i+c),{lo:h,hi:p}=u,d=t.midp??!1,g;d?(r==="less"?g=u.midTail(f,o,!1):r==="greater"?g=u.midTail(f,o,!0):g=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"?g=u.tail(f,o,!1):r==="greater"?g=u.tail(f,o,!0):o===0?g=f===h?1:0:o===1/0?g=f===p?1:0:g=La(u,f,o);let w;if(f===h)w=0;else if(f===p)w=1/0;else{let M=u.mean(1);w=M===f?1:To(x=>u.mean(x)-f,M>f)}let N={method:n,alternative:r,pValue:g,estimate:w,estimateNames:"odds ratio",nullValue:o,nullValueName:"odds ratio",warnings:[]};if(!(t.confInt??!0))return N;let v=M=>{if(f===p)return 1/0;if(d){if(f===h&&M>=.5)return 0;let y=u.midTail(f,1,!1);return y===M?1:To(R=>u.midTail(f,R,!1)-M,y<M)}let x=u.tail(f,1,!1);return x===M?1:To(y=>u.tail(f,y,!1)-M,x<M)},E=M=>{if(f===h)return 0;if(d){if(f===p&&M>=.5)return 1/0;let y=u.midTail(f,1,!0);return y===M?1:To(R=>u.midTail(f,R,!0)-M,y>M)}let x=u.tail(f,1,!0);return x===M?1:To(y=>u.tail(f,y,!0)-M,x>M)},b;if(r==="less")b=[0,v(1-s)];else if(r==="greater")b=[E(1-s),1/0];else{let M=(1-s)/2;b=[E(M),v(M)]}return{...N,confInt:b,confLevel:s}}var xs=[[0,0]];function Tn(e){for(let t=xs.length;t<=e;t++)xs.push(ue(xs[t-1],Ic([t,0])));return xs[e]}function Rs(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 _a=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 ap(e){let t=e.filter(v=>v.some(E=>E>0));if(t.length===0)throw new RangeError("all entries of the table are zero");let n=t[0].map((v,E)=>t.some(b=>b[E]>0));if(t=t.map(v=>v.filter((E,b)=>n[b])),t.length<2||t[0].length<2)return 1;t.length>t[0].length&&(t=t[0].map((v,E)=>t.map(b=>b[E])));let r=t.length,o=t.map(v=>v.reduce((E,b)=>E+b,0)),s=t[0].map((v,E)=>t.reduce((b,M)=>b+M[E],0)).sort((v,E)=>E-v),i=s.length,c=o.reduce((v,E)=>v+E,0),a=qc(Tn(c));for(let v of o)a=ue(a,Tn(v));for(let v of s)a=ue(a,Tn(v));let l=[0,0];for(let v of t.flat())l=Ce(l,Tn(v));let f=ue(l,[Math.log1p(1e-7),0]),m=1e-9*(1+Math.abs(f[0])),[u,h]=f,p=Array.from({length:i},()=>new Map),d=(v,E)=>{let b=E.join(","),M=p[v].get(b);if(M!==void 0)return M;let x=ue(a,Tn(E.reduce((L,_)=>L+_,0)));for(let L of E)x=Ce(x,Tn(L));for(let L=v;L<i;L++)x=Ce(x,Tn(s[L]));let y={R:E,KZ:x,max:0,min:0,edges:[],pastHi:[],pastLo:[],count:[],index:new Map};if(p[v].set(b,y),v===i-1){let L=[0,0];for(let _ of E)L=Ce(L,Tn(_));return y.max=y.min=L[0],y.edges.push({to:y,hi:L[0],lo:L[1],mult:1}),y}let R=new Map,T=new Array(r+1).fill(0);for(let L=r-1;L>=0;L--)T[L]=T[L+1]+E[L];let S=new Array(r).fill(0),k=(L,_,I)=>{if(L===r-1){S[L]=_;let D=Ce(I,Tn(_)),A=E.map((B,J)=>B-S[J]).sort((B,J)=>B-J),O=d(v+1,A),q=`${A.join(",")}|${D[0]}`,P=R.get(q);P===void 0?R.set(q,{to:O,hi:D[0],lo:D[1],mult:1}):P.mult++;return}let F=Math.max(0,_-T[L+1]),C=Math.min(E[L],_);for(let D=F;D<=C;D++)S[L]=D,k(L+1,_-D,Ce(I,Tn(D)))};if(k(0,s[v],[0,0]),y.edges=[...R.values()],y.max=At(y.edges.map(L=>L.hi+L.to.max)),y.min=sn(y.edges.map(L=>L.hi+L.to.min)),v===i-2){let L=y.edges.map(O=>{let q=O.to.edges[0];return{v:ue([O.hi,O.lo],[q.hi,q.lo]),mult:O.mult}});L.sort((O,q)=>O.v[0]-q.v[0]||O.v[1]-q.v[1]);let _=L.length,I=L[_-1].v[0],F=new Float64Array(_),C=new Float64Array(_),D=new Float64Array(_),A=[0,0];L.forEach(({v:O,mult:q},P)=>{F[P]=O[0],C[P]=O[1];let B=Ce(O,[I,0]);A=ue(A,[q*Math.exp(B[0])*(1+B[1]),0]),D[P]=A[0]}),y.dist={hi:F,lo:C,cum:D,vmax:I,kv:ue(a,[I,0])}}return y},g=d(0,[...o].sort((v,E)=>v-E)),w=new _a,N=(v,E,b,M)=>{if(E+v.max<=u-m){let[k,L]=Rs(v.KZ[0],v.KZ[1],E,b);return w.add(k,L,M),!0}if(E+v.min>u+m)return!0;let x=v.dist;if(x===void 0)return!1;let[y,R]=Rs(u,h,-E,-b),T=0,S=x.hi.length;for(;T<S;){let k=T+S>>>1,L=x.hi[k];L<y||L===y&&x.lo[k]<=R?T=k+1:S=k}if(T>0){let[k,L]=Rs(x.kv[0],x.kv[1],E,b);w.add(k,L,M*x.cum[T-1])}return!0};N(g,0,0,1)||(g.pastHi.push(0),g.pastLo.push(0),g.count.push(1));for(let v=0;v<i-2;v++)for(let E of p[v].values()){let{pastHi:b,pastLo:M,count:x}=E;if(x.length!==0){for(let{to:y,hi:R,lo:T,mult:S}of E.edges)for(let k=0;k<x.length;k++){let[L,_]=Rs(b[k],M[k],R,T),I=x[k]*S;if(N(y,L,_,I))continue;let F=y.index.get(L);F===void 0?(y.index.set(L,y.count.length),y.pastHi.push(L),y.pastLo.push(_),y.count.push(I)):y.count[F]+=I}E.pastHi=E.pastLo=E.count=[],E.index.clear()}}return Math.min(1,w.value())}function Ts(e){let t=e.every(n=>Number.isInteger(n))?1:2;return[t,e.map(n=>n*t).sort((n,r)=>n-r)]}function Oa(e){return[...e].sort((t,n)=>t-n).join(",")}function Ca(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 Fa(e){let[,t]=Ts(e),n=0,r=0;for(let o of t)r+=n+=o;return{steps:r,size:n+1}}function Da(e){let[t,n]=Ts(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 Ca(t,0,o)}function Pa(e,t){let[,n]=Ts(e),r=n.length,o=Math.min(t,r-t),s=Ol(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 Ol(e){let t=[0];for(let n of e)t.push(t[t.length-1]+n);return t}function Va(e,t){let[n,r]=Ts(e),o=r.length,s=Math.min(t,o-t),i=Ol(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 d=Math.min(h,s);d>=Math.max(1,s-(o-h));d--){let g=c[d],w=c[d-1],N=d/h,v=(h-d)/h,E=i[d],b=i[h]-i[h-d],M=i[d-1]+p,x=Math.max(E,M);for(let y=b;y>=x;y--)g[y-E]=v*g[y-E]+N*w[y-M];for(let y=x-1;y>=E;y--)g[y-E]=v*g[y-E]}}let a=c[s],l=t*(t+1)/2;if(s===t)return Ca(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 Ca(n,(i[o]-u)/n-l,m)}function nr(e,t){let n=Math.ceil(e.f*(t-e.offset)+e.f*1e-8)-1;return n<0?0:n>=e.lower.length?1:Math.min(1,e.lower[n])}function vr(e,t){let n=nr(e,t);if(Math.abs(n-1)>=1/64)return 1-n;let r=Math.ceil(e.f*(t-e.offset)+e.f*1e-8);return r>=e.upper.length?0:Math.min(1,e.upper[Math.max(0,r)])}var Fl=["alternative","mu","paired","exact","correct","confInt","confLevel","tolRoot"];function Ao(e,t,n={}){if(typeof e!="string"&&at(t)&&V(t,Fl,"wilcoxTest"),V(n,Fl,"wilcoxTest"),typeof e=="string"){if(n.paired!==void 0)throw new Error("cannot use 'paired' in formula method");let d=ms(e,t,!0);return d.kind==="one"?Ao(d.x,n):Ao(d.x,d.y,d.kind==="paired"?{...n,paired:!0}:n)}let r;t!==void 0&&!ArrayBuffer.isView(t)&&!Array.isArray(t)?n=t:r=t;let o=Ge(n.alternative),s=n.mu??0,i=n.paired??!1,c=n.exact??null,a=cp(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[d,g]=Gn(Array.from(e),Array.from(r));u=Ct(d.map((w,N)=>w-g[N]))}else u=Ct(Array.from(e)),r!==void 0&&(h=Ct(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 mt};return H("wilcoxTest",h===void 0?hp(u,c,i,p):yp(u,h,c,p))}function cp(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 Ss(e,t,n){return n?t==="two.sided"?Math.sign(e)*.5:t==="greater"?.5:-.5:0}var Vl=10*Number.EPSILON;function $l(e,t,n=0){let r=a=>Math.min(1,Math.max(a,0)),o=()=>n===0?ie(e):r(ie(e)-n*Xt(e)),s=()=>n===0?ie(e,{lowerTail:!1}):r(ie(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 lp(e){let t=e*e;return[e*(t-3),e*(t*(t-10)+15),e*(t*(t*(t-21)+105)-105)]}function fp(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 zl(e,o,s,n)}function mp(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 zl(e,i,c,r)}function zl(e,t,n,r){let[o,s,i]=lp(e),c=t*o;return r>=2&&(c+=n*s),r>=3&&(c+=t*t/2*i),c}function ja(e){let t=e.length;if(t===0)return NaN;let n=Math.floor(t/2);return t%2===1?e[n]:st([e[n-1],e[n]])}var Ka=(e,t)=>e-t,$a=5e9,up=40;function za(e){return new RangeError(`wilcoxTest: the exact conditional ${e} would take too long or too much memory: use exact: false`)}function As(e,t){if(e.size>16777216||e.steps>$a)throw za(t);return e.steps}function jl(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 pn(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 hp(e,t,n,r){let{alternative:o,mu:s}=r,i=e.map(b=>b-s);i.some((b,M)=>!Number.isFinite(b)&&Number.isFinite(e[M]))&&(i=e.map(b=>b/2-s/2));let a=i.some(b=>b===0),l=t??i.length<50,f=(b,M,x)=>({method:b,alternative:o,statistic:M,statisticName:"V",pValue:x,nullValue:s,nullValueName:n?"location shift":"location",warnings:r.warnings.list});if(l){let b=bt(i.map(Math.abs)),M=pn(b).length>0,x=0;for(let L=0;L<i.length;L++)i[L]>0&&(x+=b[L]);let y="Wilcoxon signed rank exact test";if(!M&&!a){let L=i.length,_=L*(L+1)/2,I;if(o==="less")I=$r(x,L,!0,!1);else if(o==="greater")I=$r(x-1,L,!1,!1);else{let C=x>_/2?$r(x-1,L,!1,!1):$r(x,L,!0,!1);I=Math.min(1,2*C)}let F=f(y,x,I);return r.confInt?{...F,...pp(e,r)}:F}let R=b.filter((L,_)=>i[_]!==0);As(Fa(R),"distribution");let T=Da(R),S;if(o==="less")S=nr(T,x);else if(o==="greater")S=vr(T,x-.25);else{let L=0;for(let _ of R)L+=_/2;S=Math.min(1,2*(x>L?vr(T,x-.25):nr(T,x)))}let k=f(y,x,S);return r.confInt?{...k,...gp(e,r)}:k}let m=a?i.filter(b=>b!==0):i,u=m.length;if(u===0)throw new RangeError("no non-zero differences for the signed rank test");let[h,p,d]=jl(m),w=pn(bt(m.map(Math.abs))).length>0||a?Math.min(r.correct,0):r.correct,N=(p-Ss(p,o,w>=0))/d,v=$l(N,o,w>=1?fp(N,u,w):0),E=f(`Wilcoxon signed rank test${r.correct>=0?" with continuity correction":""}`,h,v);return r.confInt?{...E,...wp(e,r)}:E}function Wa(e,t,n){return e==="less"?[-1/0,n]:e==="greater"?[t,1/0]:[t,n]}function Dl(e,t,n){let r={confInt:Wa(e,NaN,NaN),confLevel:0,estimate:t};return n!==void 0&&(r.estimateNames=n),r}function Kl(e){if(!(e>0&&e<1/0))throw new RangeError("tolRoot must be a positive finite number")}function Wl(e){let t=e.length,n=[];for(let r=0;r<t;r++)for(let o=r;o<t;o++){let s=Hn(e[r],e[o]);Number.isNaN(s)||n.push(s)}return n.sort(Ka)}function Yl(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+Vl&&l++;let f=ja(e);if(l===0)return{confInt:[-1/0,1/0],confLevel:1,estimate:f,estimateNames:o};let m=t(l-1);return{confInt:Wa(s,e[l-1],e[c-l]),confLevel:1-(s==="two.sided"?2*m:m),estimate:f,estimateNames:o}}function pp(e,t){let n=e.length;return Yl(Wl(e),r=>$r(r,n,!0,!1),r=>jc(r,n,!0,!1),t,"(pseudo)median")}function Bl(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 Ul(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)+Vl,d=f!=="less",g=f!=="greater",w=I=>Hn(e[I-1],e[I]),N=Math.min(u-1,Math.max(1,Math.ceil(i.mean+oe(Math.min(p,1))*i.sd+.1*i.sd+3))),v=8,E=I=>I===0?e[0]-1:w(I),b=I=>I===0?e[u-1]+1:w(u-I),M=[new Map,new Map];{let I=new Map,F=0,C=up*c;for(let[D,A]of[[0,E],[1,b]])if(!(D===0?!d:!g))for(let O=0;O<=N;O++){if(F+=C,F>$a)throw za("interval");let q;try{q=t(A(O))}catch{break}if(M[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>$a)throw za("interval");I.set(q.key,!0),I.size>v&&I.delete(I.keys().next().value)}}let x=new Map,y=(I,F)=>{let C=I===0?E(F):b(F),D=M[I].get(F),A;if(D===void 0){let B=t(C);D=[B.stat,B.key],A=B.scores}let[O,q]=D,P=x.get(q);return P!==void 0?x.delete(q):(P=n(A??t(C).scores),x.size>=v&&x.delete(x.keys().next().value)),x.set(q,P),[O,P]},R=I=>{let[F,C]=y(0,I);return o(C,F)},T=I=>{let[F,C]=y(1,I);return s(C,F)},S=-1/0,k=0;if(d){let I=R(0);if(!(I>p)){let F=I,C=1;for(;C<=u-1;C++){let D=R(C);if(D>p)break;F=D}S=e[C-1],k=F}}let L=1/0,_=0;if(g){let I=T(0);if(!(I>p)){let F=I,C=u-1;for(;C>=1;C--){let D=T(u-C);if(D>p)break;F=D}L=e[C],_=F}}return{confInt:[S,L],confLevel:1-(k+_),estimate:ja(e),estimateNames:l}}function dp(e,t){let n=e.map(r=>r-t);if(n.some(r=>Number.isNaN(r)))throw new Error("cannot compute the exact confidence interval: the shifted data have undefined values (infinite observations)");return Number.isFinite(t)&&n.some((r,o)=>!Number.isFinite(r)&&Number.isFinite(e[o]))&&(n=e.map(r=>r/2-t/2)),n}function gp(e,t){let n=e.length,r=s=>{let i=dp(e,s),{ranks:c,key:a}=Bl(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 Ul(Wl(e),r,Da,s=>As(Fa(s),"interval"),(s,i)=>vr(s,i-.25),nr,{mean:o/2,sd:Math.sqrt(n*(n+1)*(2*n+1)/24)},n,t,"(pseudo)median")}function wp(e,t){let{alternative:n,confLevel:r,tolRoot:o}=t,s=t.correct>=0,i=e.length;if(i===0)return Dl(n,NaN);let c=sn(e),a=At(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"),Dl(n,Hn(c,a),"midrange");let l=(v,E)=>{let[,b,M]=jl(e.map(x=>x-v).filter(x=>x!==0));return(b-Ss(b,n,E))/M},f=l(c,s),m=l(a,s),u=v=>-oe(v),h=v=>n==="greater"?!(f<u(v)):n==="less"?!(m>oe(v/2)):!(f<u(v/2))&&!(m>oe(v/2)),p=1-r,d=!1;for(;!h(p)&&p<1;)p*=2,d=!0;d&&t.warnings.add("requested conf.level not achievable"),Kl(o);let g=v=>Kt(E=>l(E,s)-v,c,a,{tol:o,fLower:f-v,fUpper:m-v}),w;if(p>=1){let v=ja([...e].sort(Ka));w=Wa(n,v,v)}else n==="greater"?w=[g(u(p)),1/0]:n==="less"?w=[-1/0,g(oe(p))]:w=[g(u(p/2)),g(oe(p/2))];let N=Kt(v=>l(v,!1),c,a,{tol:o,fLower:l(c,!1),fUpper:l(a,!1)});return{confInt:w,confLevel:d?1-Math.min(1,p):r,estimate:N,estimateNames:"(pseudo)median"}}function Gl(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 pn(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 bp(e,t){let[n,r,o]=Gl(bt([...e,...t]),e.length);return[n,r,o]}function vp(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 Np(e,t,n){return Hl(e,t,n).ranks}function Hl(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:Oa(i)}}if(Number.isFinite(n)&&r.some((o,s)=>!Number.isFinite(o)&&Number.isFinite(e[s]))){let o=vp(e.map(s=>s/2-n/2),t);return{ranks:o,key:Oa(o)}}return Bl([...r,...t])}function yp(e,t,n,r){let{alternative:o,mu:s}=r,i=e.length,c=t.length,a=n??(i<50&&c<50),l=Np(e,t,s),[f,m,u,h]=Gl(l,i),p,d;if(a)if(d="Wilcoxon rank sum exact test",!h)p=Pl(()=>o==="less"?zr(f,i,c,!0,!1):o==="greater"?zr(f-1,i,c,!1,!1):Math.min(2*zr(f,i,c,!0,!1),2*zr(f-1,i,c,!1,!1),1));else{As(Pa(l,i),"distribution");let N=Va(l,i);o==="less"?p=nr(N,f):o==="greater"?p=vr(N,f-.25):p=Math.min(2*nr(N,f),2*vr(N,f-.25),1)}else{d=`Wilcoxon rank sum test${r.correct>=0?" with continuity correction":""}`;let N=h?Math.min(r.correct,0):r.correct,v=l.every(b=>b===l[0]),E=(m-Ss(m,o,N>=0))/u;p=v?1:$l(E,o,N>=1?mp(E,i,c,N):0)}let g={method:d,alternative:o,statistic:f,statisticName:"W",pValue:p,nullValue:s,nullValueName:"location shift",warnings:r.warnings.list};if(!r.confInt)return g;let w=a?h?Ep(e,t,r):Pl(()=>Mp(e,t,r)):xp(e,t,r);return{...g,...w}}function Pl(e){try{return e()}catch(t){throw t instanceof RangeError&&t.message.startsWith("wilcox:")?new RangeError(`${t.message}: use exact: false`):t}}function Xl(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(Ka)}function Mp(e,t,n){let r=e.length,o=t.length;return Yl(Xl(e,t,!1),s=>zr(s,r,o,!0,!1),s=>Wc(s,r,o,!0,!1),n,"difference in location")}function Ep(e,t,n){let r=e.length,o=t.length,s=i=>{let{ranks:c,key:a}=Hl(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 Ul(Xl(e,t,!0),s,i=>Va(i,r),i=>As(Pa(i,r),"interval"),(i,c)=>vr(i,c-.25),nr,{mean:r*o/2,sd:Math.sqrt(r*o*(r+o+1)/12)},r+o,n,"difference in location")}function xp(e,t,n){let{alternative:r,confLevel:o,tolRoot:s}=n,i=n.correct>=0,c=sn(e)-At(t),a=At(e)-sn(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[,d,g]=bp(e.map(w=>w-h),t);return(d-Ss(d,r,p))/g},f=(h,p)=>{let d=l(c,p)-h,g=l(a,p)-h;if(Number.isNaN(d)||Number.isNaN(g))throw new Error("cannot compute the confidence interval because all observations are tied");return d<=0?c:g>=0?a:(Kl(s),Kt(w=>l(w,p)-h,c,a,{tol:s,fLower:d,fUpper:g}))},m=1-o,u;return r==="greater"?u=[f(-oe(m),i),1/0]:r==="less"?u=[-1/0,f(oe(m),i)]:u=[f(-oe(m/2),i),f(oe(m/2),i)],{confInt:u,confLevel:o,estimate:f(0,!1),estimateNames:"difference in location"}}var Rp=["method","n"],Ql=["holm","hochberg","hommel","bonferroni","BH","BY","fdr","none"];function Tp(e){if(Ql.includes(e))return e;let t=Ql.filter(n=>n.startsWith(e));if(e.length===0||t.length!==1)throw new RangeError(`invalid method "${e}"`);return t[0]}function Yr(e,t={}){V(t,Rp,"pAdjust");let n=typeof t=="string"?{method:t}:t,r=Tp(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=Jl(a,r,c),f=o.slice();return s.forEach((m,u)=>f[m]=l[u]),f}function ks(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 Jl(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=ks(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=ks(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=Fn(),u=Math.floor(n);if(u<=1e6){for(let h=1;h<=u;h++)mn(m,1/h,u);s=Dn(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=ks(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?Jl(e,"hochberg",n):Sp(e,n)}}var Zl=2**16;function Sp(e,t){if(!Number.isInteger(t))throw new RangeError("n must be an integer for the hommel method");if(t>Zl)throw new RangeError(`n must be at most ${Zl} for the hommel method (O(n^2) work)`);let n=e.length,r=[...e,...new Array(t-n).fill(1)],o=ks(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 ef=["p","alternative","confLevel"],Ap=1+1e-7;function Ya(e,t,n){let r=e-1;return e===0||e-r===1&&$t(t,-r)[1]===0?On(r,t,n,!1,!1):On(e,t,n,!1,!1)+fn(e,t,n,!1)}function kp(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=fn(e,t,n,!1)*Ap,s=a=>fn(a,t,n,!1)<=o;if(e<r){let a=Zo(s,Math.ceil(r),t),l=a>t?0:Ya(a,t,n);return Math.min(1,On(e,t,n,!0,!1)+l)}let i=Jo(s,0,Math.floor(r)),c=i<0?0:On(i,t,n,!0,!1);return Math.min(1,Ya(e,t,n)+c)}function qp(e,t,n={}){V(t,ef,"binomTest"),V(n,ef,"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),!wn(e[0])||!wn(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(!wn(e))throw new RangeError("'x' must be nonnegative and integer");if(r=Math.round(e),!(wn(t)&&Math.round(t)>=r&&t>0))throw new RangeError("'n' must be a positive integer >= 'x'");o=Math.round(t)}return Ba(r,o,n)}function Ba(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=Ge(n.alternative),s=n.confLevel??.95;It(s);let i;o==="less"?i=On(e,t,r,!0,!1):o==="greater"?i=Ya(e,t,r):i=kp(e,t,r);let c=f=>e===0?0:Yn(f,e,t-e+1,!0,!1),a=f=>e===t?1:f<1e-8?Yn(f,e+1,t-e,!1,!1):Yn(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 Ip=["T","r","alternative","confLevel"],_p=1+1e-7;function Lp(e,t){if(t===0)return e===0?1:0;if(e===t)return 1;let n=es(e,t,!1)*_p,r=i=>es(i,t,!1)<=n,o=Math.floor(t);if(e<t){let i=Zo(r,Math.max(e+1,o),1/0);return Math.min(1,Pr(e,t,!0,!1)+Ua(i,t))}let s=Jo(r,0,Math.min(e-1,o));return Math.min(1,Pr(s,t,!0,!1)+Ua(e,t))}var ko=2**-1022;function Ua(e,t){return e-(e-1)===1?Pr(e-1,t,!1,!1):Pr(e,t,!1,!1)+es(e,t,!1)}function Nr(e){let t=e<9332636185032189e-317?600:0,n=Math.floor(Math.log2(e*2**t));return[e*2**t*2**-n,n-t]}function Cp(e,t,n,r){let o=n+t*r,s=e*t*n/o;if(Number.isFinite(s)&&Number.isFinite(o)&&o>=ko&&(s!==0||e===0||t===0))return s;let i=e*t,c=1+t*(r/n);if(e===0||t===0)return 0;if(Number.isFinite(i)&&Number.isFinite(c))return i/c;let[a,l]=Nr(e),[f,m]=Nr(t),[u,h]=Nr(n),[p,d]=Nr(r),g=f*p,w=h-m-d,N=w>=0?u/(u+$(g,-w)):u/($(u,w)+g);return $(a*f*N,l+m+Math.min(w,0))}function Op(e,t,n,r){let o=e/n,s=t/r,i=o/s;return t===0||Number.isFinite(o)&&Number.isFinite(s)&&s>=ko&&(o===0||o>=ko)?i:e===0?0:e/t*(r/n)}function Fp(e,t={}){V(t,Ip,"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(wn))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(w=>Number.isFinite(w)&&w>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=Ge(t.alternative),l=t.confLevel??.95;if(o===2?It(l):an(l),o===2){let[w,N]=r,[v,E]=i,b=w+N;if(b===1/0)throw new RangeError("'x' must be finite, nonnegative, and integer");let M=c*v,x=M+E,y=Number.isFinite(x)&&x>=ko&&(M===0||M>=ko)?M/x:1/(1+E/v/c),R=Ba(Math.round(w),Math.round(w)+Math.round(N),{p:y,alternative:a,confLevel:l}),T=E/v,S=k=>{if(k===0)return 0;let L=k/(1-k);if(Number.isFinite(T)||L===1/0)return L*T;let[_,I]=Nr(L),[F,C]=Nr(v),[D,A]=Nr(E);return $(_*D/F,I+A-C)};return{method:"Comparison of Poisson rates",alternative:a,statistic:w,statisticName:"count1",parameter:Cp(b,c,v,E),parameterName:"expected count1",pValue:R.pValue,confInt:[S(R.confInt[0]),S(R.confInt[1])],confLevel:l,estimate:Op(w,N,v,E),estimateNames:"rate ratio",nullValue:c,nullValueName:"rate ratio",warnings:[]}}let f=r[0],m=i[0],u=c*m,h;a==="less"?h=Pr(f,u,!0,!1):a==="greater"?h=Ua(f,u):h=Lp(f,u);let p=w=>f===0?0:Qo(w,f,1,!0,!1),d=w=>w<1e-8?Qo(w,f+1,1,!1,!1):Qo(1-w,f+1,1,!0,!1),g;if(a==="less")g=[0,d(1-l)];else if(a==="greater")g=[p(1-l),1/0];else{let w=(1-l)/2;g=[p(w),d(w)]}return{method:"Exact Poisson test",alternative:a,statistic:f,statisticName:"number of events",parameter:m,parameterName:"time base",pValue:h,confInt:[g[0]/m,g[1]/m],confLevel:l,estimate:f/m,estimateNames:"event rate",nullValue:c,nullValueName:"event rate",warnings:[]}}var tf=["p","alternative","confLevel","correct"],Dp="Chi-squared approximation may be incorrect";function Ga(e,t,n={}){at(t)&&V(t,tf,"propTest"),V(n,tf,"propTest");let r,o,s=n;if(typeof e!="number"&&e.length>0&&Array.isArray(e[0])){let S=e;if(S.some(k=>k.length!==2))throw new RangeError("'x' must have 2 columns");r=S.map(k=>k[0]),o=S.map(k=>k[0]+k[1]),t!==void 0&&typeof t=="object"&&!Array.isArray(t)&&(s=t)}else{if(t===void 0||typeof t=="object"&&!Array.isArray(t))throw new RangeError("'n' must be given");r=typeof e=="number"?[e]:Array.from(e),o=typeof t=="number"?[t]:Array.from(t)}if(r.length!==o.length)throw new RangeError("'x' and 'n' must have the same length");let i=r.length,c=[];for(let S=0;S<i;S++)!Number.isNaN(r[S])&&!Number.isNaN(o[S])&&c.push(S);let a=c.length;if(a<1)throw new RangeError("not enough data");if(r=c.map(S=>r[S]),o=c.map(S=>o[S]),o.some(S=>!(S>0)))throw new RangeError("elements of 'n' must be positive");if(o.some(S=>S===1/0))throw new RangeError("elements of 'n' must be finite");if(r.some(S=>!(S>=0)))throw new RangeError("elements of 'x' must be nonnegative");if(r.some((S,k)=>S>o[k]))throw new RangeError("elements of 'x' must not be greater than those of 'n'");let l;if(s.p!==void 0){let S=typeof s.p=="number"?[s.p]:Array.from(s.p);if(S.length!==i)throw new RangeError("'p' must have the same length as 'x' and 'n'");if(l=c.map(k=>S[k]),l.some(k=>!(k>0&&k<1)))throw new RangeError("elements of 'p' must be in (0,1)")}let f=l!==void 0;a===1&&l===void 0&&(l=[.5]);let m=s.confLevel??.95;It(m);let u=Ge(s.alternative);(a>2||a===2&&f)&&(u="two.sided");let h=r.map((S,k)=>S/o[k]),p=a===2?h[0]-h[1]:0,d=a===2?1/o[0]+1/o[1]:0,g=(s.correct??!0)&&a<=2?.5:0,w=!1;if(g>0&&a===1){let S=Math.abs(r[0]-o[0]*l[0]);S<=g&&([g,w]=[S,!0])}else if(g>0&&a===2&&!f){let S=Math.abs(p)/d;S<=g&&([g,w]=[S,!0])}let N=Pp(r,o),v=o.some((S,k)=>{let L=l?.[k]??N;return S*L<5||S*(1-L)<5}),E=0;if(!w)for(let S=0;S<a;S++){let k=l?.[S]??N,L=o[S]*k,_=o[S]*(1-k);E+=Dt(Math.abs(r[S]-L)-g,L),E+=Dt(Math.abs(o[S]-r[S]-_)-g,_)}let b=l!==void 0?a:a-1,M;if(u==="two.sided")M=gt(E,b/2,2,!1,!1);else{let S=Math.sign(a===1?h[0]-l[0]:p);M=ie(S*Math.sqrt(E),{lowerTail:u==="less"})}let x;if(a===1||a===2&&!f){let S=(1-m)/2,k=u==="two.sided"&&S<1e-8?oe(S,{lowerTail:!1}):oe(u==="two.sided"?(1+m)/2:m);if(a===1){let L=o[0],_=k*k,I=(O,q)=>(O+_/(2*L)+q*k*Math.sqrt(O*(1-O)/L+_/(4*L*L)))/(1+_/L),F=h[0]+g/L,C=h[0]-g/L,D=Math.min(1,Math.max(0,F>=1?1:I(F,1))),A=Math.min(1,Math.max(0,C<=0?0:I(C,-1)));x=u==="two.sided"?[A,D]:u==="greater"?[A,1]:[0,D]}else{let L=Math.sqrt(h.reduce((C,D,A)=>C+D*(1-D)/o[A],0)),_=k*L+g*d,I=Math.max(-1,p-_),F=Math.min(1,p+_);x=u==="two.sided"?[I,F]:u==="greater"?[I,1]:[-1,F]}}let y=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"}`} ${g!==0?"with":"without"} continuity correction`,alternative:u,statistic:E,statisticName:"X-squared",parameter:b,parameterName:"df",pValue:M,estimate:a===1?h[0]:h,estimateNames:y,warnings:v?[Dp]:[]};return l!==void 0&&(T.nullValue=a===1?l[0]:l,T.nullValueName=y),x!==void 0&&(T.confInt=x,T.confLevel=m),H("propTest",T)}function Pp(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 Vp=["method","confLevel","sides"],$p=["wilson","wald","wilsoncc","agresti-coull","jeffreys","clopper-pearson","midp"],zp=["two.sided","left","right"];function qo(e,t){let n=xe(t,zp),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{...Ha(s,o),sides:n}}function Ha(e,t=1-e){let n=e/2<1e-8,r=n?oe(e/2,{lowerTail:!1}):oe(1-e/2),o=n?Cn(e,1,{lowerTail:!1}):Cn(t,1);return{sides:"two.sided",level:t,alpha:e,kappa:r,chisq:o}}function Br(e,t){if(typeof e!="number")throw new TypeError(`'${t}' must be a number`);if(!wn(e))throw new RangeError(`'${t}' must be a nonnegative integer`);return Math.round(e)+0}function $n(e,t,n,r){let o=Br(e,n),s=Br(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 Is(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 qs(e,t,n,r){return r?e/2<1e-8?Yn(e/2,t,n,!1,!1):Yn(1-e/2,t,n,!0,!1):Yn(e/2,t,n,!0,!1)}function nf(e,t,n,r){let o=n/2,s=r?f=>{let m=Math.exp(f);return .5*fn(e,t,m,!1)+On(e-1,t,m,!0,!1)-o}:f=>{let m=Math.exp(f);return .5*fn(e,t,m,!1)+On(e,t,m,!1,!1)-o},i=r?-1:1,c=Math.log(e/t),a=i*s(c);if(a===0)return e/t;if(a>0){let f=c,m=a,u=1;for(;;){let h=c-u;if(h<-745)return 0;let p=i*s(h);if(p<=0)return Math.exp(p===0?h:Kt(d=>i*s(d),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 Xa(e,t,n={}){V(n,Vp,"binomCI");let r=xe(n.method,$p),o=qo(n.confLevel,n.sides),[s,i]=$n(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]=Is(s,i,a);break;case"wilsoncc":{let h=a*a,p=2*(i+h),d=2*s+h-1,g=a*Math.sqrt(h-2-1/i+4*l*(i*f+1));m=l===0?0:d-g<d/2?(2*s-1)/(2*i)*(2*s-1)/(d+g):(d-g)/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,d=(s+h/2)/p,g=a*Math.sqrt(d*(1-d)/p);[m,u]=[d-g,d+g];break}case"jeffreys":m=s===0?0:qs(c,s+.5,i-s+.5,!1),u=s===i?1:qs(c,s+.5,i-s+.5,!0);break;case"clopper-pearson":m=s===0?0:qs(c,s,i-s+1,!1),u=s===i?1:qs(c,s+1,i-s,!0);break;case"midp":m=s===0?0:s===i?Math.exp(Math.log(c)/i):nf(s,i,c,!1),u=s===i?1:s===0?-Math.expm1(Math.log(c)/i):nf(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),H("binomCI",{estimate:l,confInt:[m,u],confLevel:n.confLevel??.95,method:r,sides:o.sides,warnings:[]})}function Io(e){return e===1/0?1:e/(1+e)}function _o(e,t){return e===0?0:t>0?e*e/t:1/0}function jp(e,t,n,r){return e/t+n/r>1}function Kp(e,t,n,r){return Ls(e,t,n,r,0)}function Ja(e,t,n,r,o){if(jp(e,t,n,r)){let[p,d,g,w]=Ja(t-e,t,r-n,r,-o);return[d,p,w,g]}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=w=>{let N=0;return i>0&&(N+=i/w),i<c&&(N-=(c-i)/(1-w)),a>0&&(N+=a/(w+f)),a<l&&(N-=(l-a)/(m-w)),N},d=w=>{let N=0;return i>0&&(N-=i/(w*w)),i<c&&(N-=(c-i)/((1-w)*(1-w))),a>0&&(N-=a/((w+f)*(w+f))),a<l&&(N-=(l-a)/((m-w)*(m-w))),N};if(p(0)<=0)u=0;else if(p(m)>=0)u=m;else{let w=0,N=m,v=Wp(e,t,n,r,o);u=s?v:v-o,u>w&&u<N||(u=N/2);for(let E=0;E<2e3;E++){let b=p(u);if(b===0)break;b>0?w=u:N=u;let M=u-b/d(u);if(M>w&&M<N||(M=w===0?N/2:N>2*w?Math.sqrt(w*N):w+(N-w)/2),!(M>w&&M<N))break;let x=Math.abs(M-u);if(u=M,x<=2**-53*u)break}}}let h=[u,1-u,Math.min(1,u+f),Math.max(0,m-u)];return s?h:[h[2],h[3],h[0],h[1]]}function Wp(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 d=(Math.PI+Math.acos(Math.max(-1,Math.min(1,h/(p*p*p)))))/3;return 2*p*Math.cos(d)-u}function ei(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]=Ja(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 Yp(e,t,n,r,o,s,i){let c=Ce(Ce(ft([e,0],t),ft([n,0],r)),[o,0]),[a,l,f,m]=Ja(e,t,n,r,o),u=ue(ft(Ht(a,l),t),ft(Ht(f,m),r));return s==="mn"&&(u=ft(zt(u,t+r),t+r-1)),Ce(_e(c,c),zt(u,i))[0]}function Ls(e,t,n,r,o){return Ce(Ce(ft([e,0],t),ft([n,0],r)),[o,0])[0]}function Bp(e,t,n,r,o,s){return _o(Ls(e,t,n,r,o),ei(e,t,n,r,o,s))}var _s=new Float64Array(1),Qa=new BigInt64Array(_s.buffer);function rf(e){return _s[0]=Math.abs(e),e<0?-Qa[0]:Qa[0]}function rr(e){return Qa[0]=e<0n?-e:e,e<0n?-_s[0]:_s[0]}function Za(e,t,n,r,o){let s=e(t);if(s===0||Number.isNaN(s))return t;let i=s<0===o,c=rf(i?r:n),a=rf(t),l=s,f=a,m=s,u=1n;for(;;){if(f=i?a+u:a-u,(i?f>=c:f<=c)&&(f=c),m=e(rr(f)),m===0)return rr(f);if(m<0!=l<0||f===c)break;if(a=f,l=m,u*=2n,u>1n<<40n)return rr(a)}if(m<0==l<0||Number.isNaN(m))return rr(a);for(;f-a>1n||a-f>1n;){let h=(a+f)/2n,p=e(rr(h));if(p===0)return rr(h);p<0==l<0?(a=h,l=p):(f=h,m=p)}return Math.abs(m)<Math.abs(l)?rr(f):rr(a)}function sf(e,t,n,r,o,s){let i=Kp(e,t,n,r);if(o===0)return[i,i];let c=Io(o),a=p=>(p===i?0:Io(Bp(e,t,n,r,p,s)))-c,l=(p,d,g,w)=>{if(g===0)return p;if(w===0)return d;let N=Kt(a,p,d,{tol:1e-300,fLower:g,fUpper:w});return s==="wald"?N:Za(v=>Yp(e,t,n,r,v,s,o),N,p,d,g<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 of(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 Up(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 Gp(e,t,n,r,o){let[s,i]=Up(e,t,n,r,o);if(!(o>0&&o<1/0))return[s,1-s,i,1-i];let c=s<=.5&&i<=.5?0:o*(1-i)<=1-s?2:3,a=c===0?i:c===2?1-i:1-s,l=M=>c===0?[M,0]:c===2?$t(1,-M):ft($t(1,-M),o),f=c===3?1/o:1,m=c===0?Math.min(1,1/o):1,u=c===2?o>1?ft($t(o,-1),o)[0]:0:c===3&&o<1?$t(1,-o)[0]:0,h=M=>{let x=l(M),y=zt(x,o);return[y[0],Ce([1,0],y)[0],x[0],Ce([1,0],x)[0]]},p=e+n,d=c<2?1:-1,g=M=>{let[,x,y,R]=h(M),T=0,S=0;return p>0&&(T+=y>0?p/y:1/0,S-=p/(y*y)),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)),[d*T,S*f]},w;if(!(m>u))w=u;else if(!(g(u)[0]>0))w=u;else if(!(g(m)[0]<0))w=m;else{let M=u,x=m;w=a,w>M&&w<x||(w=M+(x-M)/2);for(let y=0;y<2e3;y++){let[R,T]=g(w);if(R===0)break;R>0?M=w:x=w;let S=w-R/T;if(S>M&&S<x||(S=M<=0?x/2:x>2*M?Math.sqrt(M*x):M+(x-M)/2),!(S>M&&S<x))break;let k=Math.abs(S-w);if(w=S,k<=2**-53*w)break}}let[N,v,E,b]=h(w);return[Math.min(1,Math.max(0,N)),Math.min(1,Math.max(0,v)),Math.min(1,Math.max(0,E)),Math.min(1,Math.max(0,b))]}function Hp(e,t,n,r,o){let[s,i,c,a]=Gp(e,t,n,r,o),l=(f,m,u,h)=>u>.5?m*h-(m-f):f-m*u;return _o(l(e,t,s,i),t*s*i)+_o(l(n,r,c,a),r*c*a)}function af(e,t,n,r,o){let s=e/t/(n/r);return lf(s,o,i=>Hp(e,t,n,r,i))}function Xp(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 Qp(e,t,n,r,o){let[s,i]=Xp(e,t,n,r,o),c=e-t*s;return _o(c,t*s*(1-s))+_o(c,r*i*(1-i))}function cf(e,t,n,r,o){let s=e*(r-n)/((t-e)*n);return lf(s,o,i=>Qp(e,t,n,r,i))}function lf(e,t,n){if(t===0)return[e,e];let r=Io(t),o=l=>n(l)-t,s=l=>l>0&&l<e?Za(o,l,Number.MIN_VALUE,e,!1):l,i=l=>l>e&&l<1/0?Za(o,l,e,Number.MAX_VALUE,!0):l;if(e===0||e===1/0){let l=u=>Io(n(u))-r,f=l(1),m=f===0?1:tr(l,e===0?f>0:f<0);return e===0?[0,i(m)]:[s(m),1/0]}let c=Math.log(e),a=l=>(l===c?0:Io(n(Math.exp(l))))-r;return[s(of(a,c,-1)),i(of(a,c,1))]}var Zp=["method","confLevel","sides"],Jp=["method","confLevel","sides"],ed=["method","confLevel"],td=["ac","wald","score","mn","mee"];function ti(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,d=i*Math.sqrt(u*(1-u)/(t+2)+h*(1-h)/(r+2));[f,m]=[Math.max(-1,p-d),Math.min(1,p+d)];break}case"score":{let[u,h]=Is(e,t,i),[p,d]=Is(n,r,i);f=l-i*Math.sqrt(u*(1-u)/t+d*(1-d)/r),m=l+i*Math.sqrt(h*(1-h)/t+p*(1-p)/r);break}case"mn":case"mee":[f,m]=sf(e,t,n,r,s.chisq,o);break}return[Math.min(f,m),Math.max(f,m)]}function nd(e,t,n,r,o={}){V(o,Zp,"binomDiffCI");let s=xe(o.method,td),i=qo(o.confLevel,o.sides),[c,a]=$n(e,t,"x1","n1"),[l,f]=$n(n,r,"x2","n2"),m=c/a-l/f,[u,h]=ti(c,a,l,f,s,i);return i.sides==="left"?h=1:i.sides==="right"&&(u=-1),H("binomDiffCI",{estimate:m,confInt:[u,h],confLevel:o.confLevel??.95,method:s,sides:i.sides,warnings:[]})}var rd=["katz.log","koopman"];function Cs(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 od(e,t,n,r,o={}){V(o,Jp,"binomRatioCI");let s=xe(o.method,rd),i=qo(o.confLevel,o.sides),[c,a]=$n(e,t,"x1","n1"),[l,f]=$n(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,Cs(.5,a,l,f,m)[1]]:l===0?[h,p]=[Cs(c,a,.5,f,m)[0],1/0]:c===a&&l===f?[h,p]=Cs(a-.5,a,f-.5,f,m):[h,p]=Cs(c,a,l,f,m):[h,p]=af(c,a,l,f,i.chisq),i.sides==="left"?p=1/0:i.sides==="right"&&(h=0),H("binomRatioCI",{estimate:u,confInt:[h,p],confLevel:o.confLevel??.95,method:s,sides:i.sides,warnings:[]})}var sd=["midp","fisher","wald","score"],ad={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"},id=2**23,cd=2**20;function ff(e,t,n){let r=[0,0];for(let o=Math.max(0,t);o<=Math.min(n,e.length-1);o++)e[o]!==0&&(r=ue(r,[e[o],0]));return r[0]}function ld(e,t={}){V(t,ed,"oddsRatio");let n=xe(t.method,sd);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}=qo(r,void 0),c=e.map((S,k)=>Array.from(S,(L,_)=>Br(L,`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),d=p-h+1;if(n==="midp"||n==="fisher"){if(d>id)throw new RangeError(`the exact distribution of the 2 x 2 table has ${d} 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 g,w,N;try{g=on(a-1,a+f,l+m,a+l,!0,!1)+.5*gn(a,a+f,l+m,a+l,!1),w=on(a,a+f,l+m,a+l,!1,!1)+.5*gn(a,a+f,l+m,a+l,!1),N=d<=cd?La(new Wr(a+f,l+m,a+l),a,1):Cl(a,a+f,l+m,a+l)}catch(S){throw S instanceof RangeError?new RangeError(`the exact p-values of the table are beyond reach: ${S.message}`):S}let v=2*Math.min(g,w),E=[a+l,f+m],b=[a+f,l+m],M=0,x=!1;for(let S=0;S<2;S++)for(let k=0;k<2;k++){let L=E[k]*b[S]/u;L<5&&(x=!0);let _=Math.abs(c[k][S]-L);M+=_*_/L}let y=gt(M,.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=je(()=>So(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]:cf(a,a+l,f,f+m,i);break}case"midp":{let S=new Wr(a+f,l+m,a+l),k=F=>{if(F===1)return[g,w];let C=S.density(F),D=a-h,A=.5*C[D];return[ff(C,0,D-1)+A,ff(C,D+1,C.length-1)+A]},L=(F,C)=>{let D=F(1);return D===0?1:tr(F,C?D>0:D<0)};R=a===h?0:a===p?1/0:L(F=>k(F)[0]-.5,!1);let _=a===h?0:L(F=>k(F)[1]-o/2,!0),I=a===p?1/0:L(F=>k(F)[0]-o/2,!1);T=[_,I];break}}return H("oddsRatio",{estimate:R,confInt:T,confLevel:r,method:ad[n],pValue:{midp:v,fisher:N,chiSquare:y},warnings:x?["Chi-squared approximation may be incorrect"]:[]})}var fd=["epsilon","paired","varEqual","confLevel","alpha"],md=["margin","method","alpha"];function mf(e,t){if(!(typeof e=="number"&&e>0&&e<.5))throw new RangeError(`'${t}' must be a single number in (0, 0.5)`)}function ud(e,t,n={}){V(n,fd,"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)mf(n.alpha,"alpha"),o=1-n.alpha,s=n.alpha;else{if(o=n.confLevel??.95,It(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=je(()=>vn(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=vt((r-Math.abs(m))/l,f,!1,!1),h=s<1e-8?xt(s,f,!1,!1):xt(o,f,!0,!1),p=[m-h*l,m+h*l];return H("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 hd=["mn","mee","wald"];function pd(e,t,n,r,o={}){V(o,md,"binomDiffEquivalenceTest");let s=xe(o.method,hd),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;mf(c,"alpha");let[a,l]=$n(e,t,"x1","n1"),[f,m]=$n(n,r,"x2","n2"),u=a/l-f/m,h=E=>Ls(a,l,f,m,E)/Math.sqrt(ei(a,l,f,m,E,s)),p=h(-i),d=h(i),g=[ie(p,{lowerTail:!1}),ie(d)],w=Math.max(g[0],g[1]),N=1-2*c,v=ti(a,l,f,m,s,Ha(2*c,N));return H("binomDiffEquivalenceTest",{method:s,estimate:u,margin:i,alpha:c,statistic:[p,d],statisticName:"z",pValues:g,pValue:w,result:g[0]<c&&g[1]<c?"rejected":"not rejected",confInt:v,confLevel:N,warnings:[]})}var Wt=new WeakMap;function Fs(e){let t=(n,r)=>n.weights===void 0?Array.from(r):r.filter((o,s)=>n.weights[s]!==0);return Re(e.flatMap(n=>[t(n,n.fittedValues),t(n,n.residuals)])).k}function Ds(e,t){return e.map(n=>{if(n.rss>=dd&&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 dd=2**-1022;function ri(e,t){return e.y.flatMap((n,r)=>(e.weights?.[r]??1)===0?[]:[[e.weights?.[r]??1,$(n,-t)]])}function Ps(e){let t=Math.floor(Math.log2(e.reduce((n,[r])=>Math.max(n,r),0)));return t-(t%2+2)%2}function yr(e,t,n=0){let r=i=>e.reduce((c,[a,l])=>c+$(a,-i)*l*l,0),o=n,s=r(o);return Number.isFinite(s)||(o=Ps(e),s=r(o)),!($(t,-o)>=1e-24*s)}function uf(e){let t=Fs([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:Ds([e],t)[0],k:t}}var hf=(e,t,n)=>wt(e,t,n,!1,!1),oi="essentially perfect fit for \u20181\u2019: summary may be unreliable";function si(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 ai=new WeakMap;function gd(e){let t=uf(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 ni(e){let t=gd(e),{names:n,rdf:r,k:o,perfect:s}=t,{rows:i,rss:c}=t;if(r>0){let d=ri(e,o);yr(d,c)&&(c=0,i=i.map(g=>yr(d,g.ss)?{...g,ss:0}:g))}let a=c/r,l=d=>$(d,2*o),f=i.map(({df:d,ss:g})=>{let w=g/d/a;return[d,l(g),l(g/d),w,hf(w,d,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:d,ss:g})=>[d,g]);return p.push([r,c]),Wt.set(h,{colNames:["Df","Sum Sq"],rowNames:u,table:p}),h}function wd(e){let t=e.map(f=>f.dfResidual),n=Fs(e),r=Ds(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,d]=u>0?[e[m-1],e[m]]:[e[m],e[m-1]],g=N=>N.filter((v,E)=>(p.weights?.[E]??1)!==0),w=u===0||!gs(g(p.x),g(d.x))?NaN:h/u/i;return w<0&&(w=NaN),[t[m],o(r[m]),u,o(h),w,Number.isNaN(w)?NaN:hf(w,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 Os(...e){let t=[];for(let n of e)for(let r of n??[])t.includes(r)||t.push(r);return t}function ii(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=ni(e),a={...c,warnings:Os(e.warnings,c.warnings)},l=Wt.get(c);return l!==void 0&&Wt.set(a,l),H("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?ni(e):wd(o);return i.warnings=Os(...n.map(c=>c.warnings),s,i.warnings),H("anova",i,...n)}function ci(e){if(typeof e=="object"&&e!==null&&e.multistratum===!0){let i=e,c=Os(i.warnings,ai.get(i)),a={strata:i.strata.filter(f=>f.stratum!=="Error: (Intercept)").map(f=>({...f})),warnings:c},l=Wt.get(i);return Array.isArray(l)&&Wt.set(a,l.filter((f,m)=>i.strata[m].stratum!=="Error: (Intercept)")),H("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=ni(t),r=uf(t),o=si(t.fittedValues.map(i=>$(i,-r.k)),r.rss,r.rdf),s=Os(t.warnings,n.warnings,o?[oi]:[]);if(t.dfResidual>0){let i={...n,warnings:s},c=Wt.get(n);return c!==void 0&&Wt.set(i,c),H("aovSummary",i,e)}return H("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 bd=["pooledSd","mu","paired","ci","alternative","adjust"],vd=["pooledSd","mu","paired","ci","alternative"],Nd=["generalized","partial","ci","alternative"],yd=["partial","ci","alternative"],Md=["partial","ci","alternative"];function mi(e){if(e!==null&&!(e>0&&e<1))throw new RangeError("ci must be a single numeric value between (0, 1)")}var Ed="Missing values detected. NAs dropped.",xd=`'y' is numeric but has only 2 unique values.
3
+ If this is a grouping variable, convert it to a factor.`,wf={tol:0,maxiter:5e3};function Mr(e){return e===1/0?1:Math.exp(Dc(.5,(e-1)/2)-.5*Math.log(e/2))}function pf(e,t,n){let r=e-1<e+1?1:Math.abs(e)*Number.EPSILON;return pr(o=>dn(e,t,{ncp:o})-n,e-r,e+r,{...wf,extendInt:"downX"}).root}function bf(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;mi(c);let a=xe(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(Ct(f)).size===2&&m.push(xd),(l.some(Number.isNaN)||(f?.some(Number.isNaN)??!1))&&m.push(Ed);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,d,g,w,N;if(f===void 0||i){let S;if(f===void 0)({groups:[S],k:N}=Re([Ct(l)]));else{let[L,_]=Gn(l,f);({d:S,k:N}=ns(L,_))}let k=S.length;if(k<2)throw new RangeError(f===void 0?"not enough observations: at least 2 are needed":"not enough complete pairs: at least 2 are needed");h=st(S),p=Math.sqrt(ot(S)),d=p/Math.sqrt(k),g=1/k,w=k-1}else{let S=Et([Ct(l),Ct(f)]);N=S.k;let[k,L]=S.n;if(k<2||L<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]),I=Math.sqrt(S.variance[1]);o?(p=Math.sqrt(((k-1)*_**2+(L-1)*I**2)/(k+L-2)),d=p*Math.sqrt(1/k+1/L),g=1/k+1/L,w=k+L-2):(p=Math.sqrt((_**2+I**2)/2),g=2*(L*_**2+k*I**2)/(k*L*(_**2+I**2)),d=Math.sqrt(Math.sqrt(_**2/k)**2+Math.sqrt(I**2/L)**2),w=rs([_**2/k,I**2/L],[k-1,L-1]))}let v=r?Mr(w):1,E=$(s,-N),b=(h-E)/p*v,M=r?"Hedges_g":"Cohens_d";if(c===null)return{name:M,value:b,warnings:m};let x=a==="two.sided"?c:2*c-1,y=(h-E)/d,R=pf(y,w,(1+x)/2)*Math.sqrt(g)*v,T=pf(y,w,(1-x)/2)*Math.sqrt(g)*v;return a==="less"&&(R=-1/0),a==="greater"&&(T=1/0),{name:M,value:b,ci:c,ciLow:R,ciHigh:T,warnings:m}}function Rd(e,t,n={}){return V(n,bd,"cohensD"),H("cohensD",bf(e,t,n,n.adjust??!1))}function Td(e,t,n={}){return V(n,vd,"hedgesG"),H("hedgesG",bf(e,t,n,!0))}var Sd=e=>Array.isArray(e.effects),li=e=>e.multistratum===!0,vf=e=>Array.isArray(e.strata),Nf=e=>{let t=e;return typeof t.univariate=="object"&&typeof t.anova=="object"},fi=e=>({test:t=>t.split(":").includes(e)});function Ad(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 kd(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(w=>fi(w).test(m)),d=o(p.length===0?"Subject":`Subject:${p.join(":")}`,h[l],h[f]);if(m==="(Intercept)")return;let g={name:m,df:h[a],ss:h[n]};d.terms.push(g),s.set(m,g)})}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 yf(e,t){let n=Array.isArray(e)?e:[],r=n.filter(s=>!t.some(i=>fi(s).test(i.name)));if(r.length>0)throw new RangeError(`Observed variable not in data: ${r.join(", ")}`);let o=s=>n.some(i=>fi(i).test(s));return{isObserved:o,ssObserved:t.reduce((s,i)=>o(i.name)?s+i.ss:s,0)}}function df(e,t,n,r){let o=i=>xn(e,t,n,{ncp:i})-r,s=o(0);return Number.isNaN(s)?NaN:s>0?e===1/0?1/0:pr(o,0,Math.max(10,e*t*4),{...wf,extendInt:"downX"}).root:0}function Mf(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=df(c,t,n,(1+s)/2),l=df(c,t,n,(1-s)/2);c<=Vr((1-s)/2,t,n)&&(l=0),c<=Vr((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 qd(e){let t=Re([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 ui(e,t,n){if(typeof e!="object"||e===null)throw new TypeError(gf);if(li(e)||vf(e)||Nf(e))return Id(e,t,n);let r=Sd(e)?je(()=>ii(e)):e;if(!Array.isArray(r.colNames)||!Array.isArray(r.table))throw new TypeError(gf);let o=Wt.get(r),s=o!==void 0&&!Array.isArray(o)?o:r,i=n.ci===void 0?.95:n.ci;mi(i);let c=xe(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)");qd(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,d=p/h,g=f.reduce((R,T)=>R+T.ss,0),w=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:b,ssObserved:M}=yf(N,u),x=R=>{let{ss:T,df:S}=R;if(t==="Eta2")return E==="partial"?T/(T+p):E==="plain"?T/g:T/(T+p+M-(b(R.name)?T:0));if(t==="Omega2"){let L=E==="partial"?(T-S*d)/(T+(w-S)*d):(T-S*d)/(g+d);return Math.max(0,L)}let k=E==="partial"?(T-S*d)/(T+p):(T-S*d)/g;return Math.max(0,k)},y=`${t}${E==="plain"?"":`_${E}`}`;return{colNames:i===null?[y]:[y,"CI","CI_low","CI_high"],rowNames:u.map(R=>R.name),table:u.map(R=>i===null?[x(R)]:Mf(x(R),R.df,h,i,c)),warnings:Array.isArray(r.warnings)?[...r.warnings]:[]}}function Id(e,t,n){let r=n.ci===void 0?.95:n.ci;mi(r);let o=xe(n.alternative??"greater",["two.sided","less","greater"]),s=Nf(e),i,c,a;if(s){let b=Wt.get(e),M=kd(b!==void 0&&!Array.isArray(b)?{...e,univariate:b}:e);({strata:i,subject:c}=M),a=M.order.map(x=>({term:x,stratum:M.strata.find(y=>y.terms.includes(x))}))}else{let b=li(e)?je(()=>ci(e)):e,M=Wt.get(b);({strata:i,subject:c}=Ad(Array.isArray(M)?{...b,strata:M.map((x,y)=>({...b.strata[y],...x}))}:b)),a=i.flatMap(x=>x.terms.map(y=>({term:y,stratum:x})))}let l=Re([[...i.flatMap(b=>b.terms.map(M=>M.ss)),...i.map(b=>b.sse)]]).k;for(let b of i){b.sse=$(b.sse,-l);for(let M of b.terms)M.ss=$(M.ss,-l)}let f=i.flatMap(b=>b.terms),m=i.reduce((b,M)=>b+M.sse,0),u=f.reduce((b,M)=>b+M.ss,0)+m,h=c.sse/c.dfe,p=t==="Eta2"?n.generalized??!1:!1,d=p!==!1?"generalized":n.partial??!0?"partial":"plain",{isObserved:g,ssObserved:w}=yf(p,f),N=(b,M)=>{let{ss:x,df:y}=b,R=M.sse/M.dfe;if(t==="Eta2")return d==="partial"?x/(x+M.sse):d==="plain"?x/u:x/(x+m+w-(g(b.name)?x:0));if(t==="Omega2"){let S=M===c?0:1,k=d==="partial"?(x-y*R)/(x+S*M.sse+c.sse+h):(x-y*R)/(u+h);return Math.max(0,k)}let T=d==="partial"?(x-y*R)/(x+M.sse):(x-y*R)/u;return Math.max(0,T)},v=`${t}${d==="plain"?"":`_${d}`}`,E={colNames:r===null?[v]:[v,"CI","CI_low","CI_high"],rowNames:a.map(b=>b.term.name),table:a.map(({term:b,stratum:M})=>{let x=N(b,M);return r===null?[x]:Mf(x,b.df,M.dfe,r,o)}),warnings:[]};return s||(E.groups=a.map(b=>b.stratum.name),E.warnings=[...li(e)||vf(e)?e.warnings:[]]),E}function _d(e,t={}){return V(t,Nd,"etaSquared"),H("etaSquared",ui(e,"Eta2",t),e)}function Ld(e,t={}){return V(t,yd,"omegaSquared"),H("omegaSquared",ui(e,"Omega2",t),e)}function Cd(e,t={}){return V(t,Md,"epsilonSquared"),H("epsilonSquared",ui(e,"Epsilon2",t),e)}var Od=new Set(["eqb","lowEqbound","highEqbound","hypothesis","paired","varEqual","alpha","mu","eqboundType","biasCorrection","rmCorrection","glass","smdCi"]),Fd=["m1","sd1","n1","m2","sd2","n2","r12"],Dd=new Set(["eqb","lowEqbound","highEqbound","hypothesis","paired","varEqual","alpha","mu","eqboundType","biasCorrection","rmCorrection","glass","smdCi"]),Pd="Equivalence interval does not include zero.",Vd="Warning: setting bound type to SMD produces biased results!",$d="MET test may have higher error rates than a nil two-tailed test. Consider wider equivalence bounds.";function Ef(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 pr(s=>dn(e,t,{ncp:s,lowerTail:!r})-n,e-o,e+o,{tol:0,maxiter:5e3,extendInt:r?"upX":"downX"}).root}function zd(e,t){let{alpha:n,smdCi:r}=t,o=Math.abs(e.delta),s=(y,R,T)=>(1-(T-2)/(T*y*y))*R*R,i,c,a,l,f,m,u,h,p,d,g,w,N=t.biasCorrection;if(e.design==="two"){let{n1:y,n2:R,s1:T,s2:S}=e,k=T*T,L=S*S,_=2*y*R/(y+R);if(t.glass!==void 0){let I=t.glass==="glass1",[F,C,D,A]=I?[T,y,S,R]:[S,R,T,y];c=F,a=C-1,i=N?"Glass's delta(g)":"Glass's delta(d)",l=N?Mr(a):1,f=l*o/c,p=1/R+1/y;let O=D/F;m=Math.sqrt(O*O/(A-1)+1/(C-1)+f*f/(2*(C-1))),u=o/(F*Math.sqrt(1/C+O*O/A)),h=y+R-2,d=Math.sqrt(2/_)}else if(t.varEqual)c=Math.sqrt(((y-1)*k+(R-1)*L)/(y+R-2)),a=y+R-2,i=N?"Hedges's g":"Cohen's d",l=N?Mr(a):1,f=l*o/c,p=1/y+1/R,m=Math.sqrt(1/y+1/R+s(l,f,a)),u=o/(c*Math.sqrt(p)),h=a,d=Math.sqrt(2*(R*k+y*L)/(y*R*(k+L)));else{c=Math.sqrt((k+L)/2),a=(y-1)*(R-1)*(k+L)**2/((R-1)*k*k+(y-1)*L*L),i=N?"Hedges's g(av)":"Cohen's d(av)",l=N?Mr(a):1,f=l*o/c,p=2*(R*k+y*L)/(y*R*(k+L));let I=c**4;m=Math.sqrt(f*f*(k*k/(y-1)+L*L/(R-1))/(8*I)+(k/(y-1)+L/(R-1))/(c*c)),u=o/Math.sqrt(k/y+L/R),h=a,d=Math.sqrt(2/_)}g=a,w=(I,F)=>Math.sqrt(g/(g-2)*(2/_)*(1+I*I*_/2)-I*I/(F*F))}else if(e.design==="paired"){let{n:y,s1:R,s2:T,sdif:S,oneMinusR:k}=e,L=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*k):S,i=t.glass!==void 0?N?"Glass's delta(g)":"Glass's delta(d)":N?`Hedges's g${L}`:`Cohen's d${L}`,a=y-1,g=2*y-2,l=N?Mr(r==="goulet"?g:a):1,f=l*o/c,p=1/y,u=o*Math.sqrt(y)/c,h=a,d=Math.sqrt(2*k/y),w=(_,I)=>{let F=r==="goulet"?g:a,C=Math.sqrt(F/(F-2)*(2*k/y)*(1+_*_*y/(2*k))-_*_/(I*I));return t.rmCorrection?C:C*Math.sqrt(2*k)},t.glass!==void 0){let _=S/c;m=Math.sqrt(_*_/(y-1)+f*f/(2*(y-1)))}else!t.rmCorrection&&r==="nct"?m=Math.sqrt(1/y+s(l,f,a)):m=w(f,l)}else{let{n:y,s:R}=e;c=R,a=y-1,g=a,i=N?"Hedges's g":"Cohen's d",l=N?Mr(a):1,f=l*o/c,p=1/y,m=Math.sqrt(1/y+f*f/(2*y)),u=o*Math.sqrt(y)/R,h=a,d=1/Math.sqrt(y),w=(T,S)=>Math.sqrt(a/(a-2)*(1/y)*(1+T*T*y)-T*T/(S*S))}let v,E,b,M=a;if(r==="nct"){b=m,M=h;let y=Ef(u,h,n,!0),R=Ef(u,h,n,!1);v=y*Math.sqrt(p)*l,E=R*Math.sqrt(p)*l}else if(r==="goulet"){b=w(f,l),M=g;let y=f/d;v=rn(n,g,{ncp:y})*d,E=rn(n,g,{ncp:y,lowerTail:!1})*d}else{b=m;let y=r==="t"?xt(n,a,!1,!1):oe(n,{lowerTail:!1});v=f-y*b,E=f+y*b}let x=e.delta<0;return{label:i,denom:c,estimate:x?-f:f,se:b,confInt:x?[-E,-v]:[v,E],df:M,J:l}}function xf(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 Rf(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 Tf(e){let t=xe(e.hypothesis,["EQU","MET"]),n=xe(e.eqboundType,["raw","SMD"])==="SMD",r=xe(e.smdCi,["nct","goulet","t","z"]),o=e.glass===void 0?void 0:xe(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 Sf(e,t,n,r,o,s,i,c,a,l,f,m){let{alpha:u,hypothesis:h}=l,p=zd(c,l),d=$(p.denom,a),g=l.smdBounds?[f[0]*d,f[1]*d]:f,w=D=>{if(D===0)return[0,n];let A=Math.floor(Math.log2(Math.abs(D))),O=et(D,-A);return l.smdBounds?[O*p.denom,A+a]:[O,A]},N=l.smdBounds?f:[f[0]/d,f[1]/d];l.smdBounds&&m.push(Vd),g[0]<=0&&g[1]>=0||m.push(Pd);let v=D=>$(D/r,n-o),E=D=>{let[A,O]=w(D),q=Math.max(n,O);return et((et(t,n-q)-et(A,O-q))/r,q-o)},b=v(t),M=E(f[0]),x=E(f[1]),y=$(r,o),R=h==="EQU",T={t:b,se:y,df:s,pValue:2*vt(-Math.abs(b),s,!0,!1)},S={t:M,se:y,df:s,pValue:vt(M,s,!R,!1)},k={t:x,se:y,df:s,pValue:vt(x,s,R,!1)},L=R?Math.max(S.pValue,k.pValue):Math.min(S.pValue,k.pValue);!R&&g[0]<=0&&g[1]>=0&&L<=T.pValue&&m.push($d);let _=xt(u,s,!1,!1),I=Math.max(n,o),[F,C]=[$(t,n-I),$(r,o-I)];return H(e,{tTest:T,lower:S,upper:k,pValue:L,bounds:{raw:g,smd:N},raw:{estimate:$(t,n),se:y,confInt:[$(F-_*C,I),$(F+_*C,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 jd(e,t,n={}){V(n,Od,"tTOST");let r=Tf(n),o=Rf(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");xf(e,"x"),a||xf(t,"y");let l=je(()=>a?vn(e,{mu:c}):vn(e,t,{paired:s,varEqual:i,mu:c})),f=l.parameter,m,u,h,p;if(a){let d=Re([Ct(Array.from(e))]);u=d.k;let g=d.groups[0],w=ot(g);h=st(g)-$(c,-u),p=Math.sqrt(w/g.length),m={design:"one",n:g.length,delta:h,s:Math.sqrt(w)}}else if(s){let[d,g]=Gn(Array.from(e),Array.from(t)),w=Re([d,g]);u=w.k;let[N,v]=w.groups,E=N.map((T,S)=>T-v[S]),b=Math.sqrt(ot(N)),M=Math.sqrt(ot(v)),x=ot(E),y=Math.sqrt(x),R=(y-(b-M))*(y+(b-M))/(2*b*M);h=st(E)-$(c,-u),p=Math.sqrt(x/E.length),m={design:"paired",n:E.length,delta:st(N)-st(v)-$(c,-u),s1:b,s2:M,sdif:y,oneMinusR:R}}else{let d=Re([Ct(Array.from(e)),Ct(Array.from(t))]);u=d.k;let[g,w]=d.groups;if(g.length<2||w.length<2)throw new RangeError("not enough observations: at least 2 are needed in each sample");let[N,v,E,b]=[g.length,w.length,ot(g),ot(w)];h=st(g)-st(w)-$(c,-u),p=Math.sqrt(i?((N-1)*E+(v-1)*b)/(N+v-2)*(1/N+1/v):Math.sqrt(E/N)**2+Math.sqrt(b/v)**2),m={design:"two",n1:N,n2:v,delta:h,s1:Math.sqrt(E),s2:Math.sqrt(b)}}return Sf("tTOST",h,u,p,u,f,l.method,m,u,r,o,[])}function Kd(e,t={}){V(e,Fd,"tsumTOST"),V(t,Dd,"tsumTOST");let n=Tf(t),r=Rf(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 C=m(I,F);if(!(Number.isInteger(C)&&C>=2))throw new RangeError(`'${F}' must be an integer of at least 2`);return C},h=(I,F)=>{let C=m(I,F);if(!(C>0))throw new RangeError(`'${F}' must be positive`);return C};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 d=[],{groups:g,k:w}=Re([[o,...p?[]:[c],n.mu]]),{groups:N,k:v}=Re([[s,...p?[]:[a]]]),[E,b=NaN]=p?[g[0][0]]:[g[0][0],g[0][1]],M=g[0][g[0].length-1],[x,y=NaN]=N[0],R=I=>$(I,w-v),T,S,k,L,_;if(p)T=E-M,S=x/Math.sqrt(i),k=i-1,L="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&&(d.push("Unequal number of pairs; using smallest n"),F=Math.min(i,l));let C=Math.sqrt(x*x+y*y-2*I*x*y);if(!(C>0))throw new RangeError("the standard deviation of the differences is 0");T=E-b-M,S=C/Math.sqrt(F),k=F-1,L="Paired t-test",_={design:"paired",n:F,delta:R(T),s1:x,s2:y,sdif:C,oneMinusR:1-I}}else{let I=l,F=x*x,C=y*y;if(T=E-b-M,n.varEqual){let D=((i-1)*F+(I-1)*C)/(i+I-2);S=Math.sqrt(D*(1/i+1/I)),k=i+I-2,L=" Two Sample t-test"}else{let D=F/i,A=C/I;S=Math.sqrt(D+A),k=(D+A)**2/(D*D/(i-1)+A*A/(I-1)),L="Welch Two Sample t-test"}_={design:"two",n1:i,n2:I,delta:R(T),s1:x,s2:y}}return Sf("tsumTOST",T,w,S,v,k,L,_,v,n,r,d)}var Wd=["alternative","tsmethod"],Yd=["square","central"],Bd=1e3,Ud=32,Gd=.9,Af=1+1e-7,Hd=2**24,Xd=34e7;function Qd(e,t={}){if(V(t,Wd,"boschlooTest"),e.length!==2||e.some(M=>M.length!==2))throw new TypeError("'table' must be a 2 x 2 table");let n=Ge(t.alternative),r=xe(t.tsmethod,Yd),o=e.map((M,x)=>Array.from(M,(y,R)=>Br(y,`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)>Hd)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(Bd,Math.ceil(Ud*Math.sqrt(m))),h=M=>Math.min(M+1,40*Math.sqrt(M));if(u*(h(l)+h(f)+4)>Xd)throw new RangeError(`Boschloo's test of these group sizes (${l} and ${f}) would take too long`);let p=s/l-c/f,d;n!=="two.sided"?d=n:r==="square"?d="square":d=p>0?"greater":"less";let g=s+c,w;d==="less"?w=on(s,l,f,g,!0,!1):d==="greater"?w=on(s-1,l,f,g,!1,!1):w=je(()=>So(o)).pValue;let N=Zd(l,f,s,c,d),{value:v,at:E}=Jd(N,l,f,u),b=n==="two.sided"&&r==="central"?Math.min(1,2*v):v;return H("boschlooTest",{method:"Boschloo's Exact Test",alternative:n,statistic:w,statisticName:"test statistic",pValue:b,estimate:p,estimateNames:"difference in proportion",nullValue:0,nullValueName:"difference in proportion",nuisanceParameter:E,warnings:[]})}function Zd(e,t,n,r,o){let s=e+t,i=new Float64Array(s+1);for(let w=2;w<=s;w++)i[w]=ss(w);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 w=0;w<=s;w++){let N=Math.max(0,w-t),E=Math.min(w,e)-N+1,b=-1/0;for(let y=0;y<E;y++){let R=N+y,T=-i[R]-i[e-R]-i[w-R]-i[t-w+R];f[y]=T,T>b&&(b=T)}let M=0;for(let y=0;y<E;y++)f[y]=Math.exp(f[y]-b),M+=f[y];for(let y=0;y<E;y++)f[y]=f[y]/M;let x=0;for(let y=0;y<E;y++)m[y]=x+=f[y];x=0;for(let y=E-1;y>=0;y--)u[y]=x+=f[y];if(o==="less")for(let y=0;y<E;y++)a[(N+y)*c+w-N-y]=m[y];else if(o==="greater")for(let y=0;y<E;y++)a[(N+y)*c+w-N-y]=u[y];else{let y=0;for(let R=1;R<E;R++)f[R]>f[y]&&(y=R);for(let R=0;R<E;R++){let T=f[R]*Af,S=-1,k=y;for(;k-S>1;){let F=S+k>>1;f[F]<=T?S=F:k=F}let L=y,_=E;for(;_-L>1;){let F=L+_>>1;f[F]<=T?_=F:L=F}let I=f[y]<=T?1:(S>=0?m[S]:0)+(_<E?u[_]:0);a[(N+R)*c+w-N-R]=Math.min(1,I)}}}let h=a[n*c+r]*Af,p=new Int32Array(e+2),d=[],g=[];for(let w=0;w<=e;w++){p[w]=d.length;let N=0;for(;N<=t;){if(a[w*c+N]<=h){let v=N;for(;N+1<=t&&a[w*c+N+1]<=h;)N++;d.push(v),g.push(N)}N++}}return p[e+1]=d.length,{starts:p,from:Int32Array.from(d),to:Int32Array.from(g)}}function kf(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]=fn(r,e,t,!1);let o=$t(1,-t),s=lt([t,0],o),i=lt(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 Jd(e,t,n,r){let{starts:o,from:s,to:i}=e,c=new Float64Array(t+1),a=new Float64Array(n+1),l=new Float64Array(n+1),f=new Float64Array(n+1),m=v=>{let[E,b]=kf(t,v,c),[M,x]=kf(n,v,a),y=0;for(let T=M;T<=x;T++)l[T]=y+=a[T];y=0;for(let T=x;T>=M;T--)f[T]=y+=a[T];let R=0;for(let T=E;T<=b;T++){let S=c[T];if(S===0)continue;let k=0;for(let L=o[T];L<o[T+1];L++){let _=s[L],I=i[L];if(!(I<M||_>x))if(_===0)k+=l[Math.min(I,x)];else if(I===n)k+=f[Math.max(_,M)];else for(let F=Math.max(_,M);F<=Math.min(I,x);F++)k+=a[F]}R+=S*k}return Math.min(1,R)},u=Math.PI/2,h=v=>v<=0?0:v>=u?1:Math.sin(v)**2,p=new Float64Array(r+2),d=0;for(let v=0;v<=r+1;v++)p[v]=m(h(u*v/(r+1))),p[v]>p[d]&&(d=v);let g=p[d],w=h(u*d/(r+1));if(g>=1)return{value:1,at:w};let N=Gd*g;for(let v=0;v<=r+1;v++){let E=p[v];if(E<N||v>0&&!(E>p[v-1])||v<=r&&!(E>=p[v+1]))continue;let b=u*Math.max(0,v-1)/(r+1),M=u*Math.min(r+1,v+1)/(r+1),y=nl(S=>-m(h(S)),b,M,1e-14).x,R=m(h(y)),T=1e-4/Math.sqrt(2*(t+n));for(let S=0;S<2&&(y-T>b&&y+T<M);S++,T*=.01){let k=m(h(y-T)),L=m(h(y+T)),_=k-2*R+L;if(!(_<0))break;let I=T*(k-L)/(2*_);if(!(Math.abs(I)<T))break;let F=m(h(y+I));F>R*(1+2**-50)&&(y+=I,R=F)}R>g&&(g=R,w=h(y))}return{value:g,at:w}}var qf=["correct"];function e0(e,t,n={}){at(t)&&V(t,qf,"mcnemarTest"),V(n,qf,"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=t0(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=If(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+=If(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:gt(i,a/2,2,!1,!1),warnings:[]}}function If(e,t,n){let r=t+n;return r<1/0?Dt(e,r):Dt($(e,-520),$(t,-1040)+$(n,-1040))}function t0(e,t){if(e.length!==t.length)throw new RangeError("'x' and 'y' must have the same length");let n=[],r=en(e),o=en(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(Vn),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 n0=["ratio","alternative","confLevel"],hi=2**-1022;function _f(e,t,n={}){if(V(n,n0,"varTest"),typeof e=="string"){let v=Qn(e,t);return _f(v.x,v.y,n)}let r=Array.from(e).filter(Number.isFinite),o=Array.from(t).filter(Number.isFinite),s=n.ratio??1;if(!(Number.isFinite(s)&&s>0))throw new RangeError("'ratio' must be a single positive number");let i=Ge(n.alternative),c=n.confLevel??.95;It(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]=Et([r,o]).variance,u=0;if(!(f>=hi&&m>=hi)){let v=Et([r]),E=Et([o]);f=v.variance[0],m=E.variance[0],u=2*(v.k-E.k)}if(f===0&&m===0)throw new RangeError("both samples have zero variance");let h=u===0?f/m:et(f/m,u),p=h/s,d=wt(p,a,l,!0,!1),g=wt(p,a,l,!1,!1),w,N;if(i==="less")w=d,N=[0,h/ln(1-c,a,l,!0,!1)];else if(i==="greater")w=g,N=[h/ln(c,a,l,!0,!1),1/0];else{w=Math.min(1,2*Math.min(d,g));let v=(1-c)/2,E=v<1e-8?ln(v,a,l,!1,!1):ln(1-v,a,l,!0,!1);N=[h/E,h/ln(v,a,l,!0,!1)]}return{method:"F test to compare two variances",alternative:i,statistic:p,statisticName:"F",parameter:[a,l],parameterName:["num df","denom df"],pValue:w,confInt:N,confLevel:c,estimate:h,estimateNames:"ratio of variances",nullValue:s,nullValueName:"ratio of variances",warnings:[]}}function Lf(e,t){if(typeof e=="string"){let p=vo(e,t);return Lf(el(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=Et(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<hi)){let p=o.map(E=>{let b=Et([E]);return{w:b.variance[0],e:b.k}}),d=Math.max(...p.map(E=>E.e)),g=p.map(E=>Math.log(E.w)+2*(E.e-d)*Math.LN2),w=g.map((E,b)=>Math.log(s[b])+E),N=Math.max(...w),v=N+Math.log(w.reduce((E,b)=>E+Math.exp(b-N),0))-Math.log(c);m=c*v-g.reduce((E,b,M)=>E+s[M]*b,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:gt(u,h/2,2,!1,!1),warnings:[]}}var r0=["alternative"],pi=["alternative"];function di(e,t,n,r){return{method:e,statistic:n,statisticName:t,parameter:r,parameterName:"df",pValue:gt(n,r/2,2,!1,!1),warnings:[]}}function Cf(e){let t=0;for(let n of pn(e))t+=n*n*n-n;return t}function Vs(e,t){if(typeof e=="string"){let l=vo(e,t);return Vs(l.values,l.group.codes)}let n=dr(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-Cf(o)/(s*s*s-s)),di("Kruskal-Wallis rank sum test","Kruskal-Wallis chi-squared",c,r-1)}function gi(e,t){if(typeof e=="string")return gi(us(e,t));let n=va(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+=Cf(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 o0(e,t={},n,r={}){typeof e!="string"&&at(t)&&V(t,pi,"pageTest"),at(n)&&V(n,pi,"pageTest"),V(r,pi,"pageTest");let o,s;if(typeof e=="string")o=us(e,t),s=n??{};else if(Array.isArray(n)){let d=Array.from(e),g=t,w=n;if(d.length!==g.length||d.length!==w.length)throw new RangeError("'x', 'groups' and 'blocks' must have the same length");if(!d.every(Number.isFinite))throw new RangeError("missing or non-finite values are not allowed");let N=en(g),v=en(w);if(N.some(y=>typeof y=="number"&&Number.isNaN(y))||v.some(y=>typeof y=="number"&&Number.isNaN(y)))throw new RangeError("missing treatment or block labels are not allowed");let E=Array.from(new Set(N)).sort(Vn),b=Array.from(new Set(v)).sort(Vn);if(E.length<2||b.length<1)throw new RangeError("at least two treatments and one block are required");let M=new Map(E.map((y,R)=>[y,R])),x=new Map(b.map((y,R)=>[y,R]));o=b.map(()=>new Array(E.length).fill(NaN));for(let y=0;y<d.length;y++){let R=x.get(v[y]),T=M.get(N[y]);if(!Number.isNaN(o[R][T]))throw new RangeError("not an unreplicated complete block design");o[R][T]=d[y]}if(o.some(y=>y.some(R=>Number.isNaN(R))))throw new RangeError("not an unreplicated complete block design");s=r}else o=e.map(d=>Array.from(d)),s=t;let i=o.length,c=o[0]?.length??0;if(i<1||c<2||o.some(d=>d.length!==c))throw new RangeError("'y' must be a non-empty matrix with at least two columns");if(o.some(d=>d.some(g=>!Number.isFinite(g))))throw new RangeError("missing or non-finite values are not allowed in a block");let a=Ge(s.alternative),l=new Array(c).fill(0);for(let d of o){let g=bt(d);for(let w=0;w<c;w++)l[w]+=g[w]}let f=0;for(let d=0;d<c;d++)f+=(d+1)*l[d];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"?ie(h):a==="greater"?ie(h,{lowerTail:!1}):2*ie(-Math.abs(h));return{method:"Page's ordered aligned rank sum test",alternative:a,statistic:h,statisticName:"z",pValue:p,estimate:f,warnings:[]}}function s0(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{...gi(e),method:"Cochran's Q test",statisticName:"Q"}}function Of(e,t){if(typeof e=="string")return Of(us(e,t));let n=va(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(d=>At(d)-sn(d));s.some(d=>d===1/0)&&n.every(d=>d.every(Number.isFinite))&&(s=Re(n).groups.map(d=>At(d)-sn(d)));let i=bt(s),c=new Array(o).fill(0),a=0;n.forEach((d,g)=>{let w=bt(d);for(let N=0;N<o;N++){let v=i[g]*(w[N]-(o+1)/2);a+=v*v,c[N]+=v}});let l=0;for(let d of c)l+=d*d;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 d=0;for(let g=2;g<=o;g++)d+=Math.log(g);h=Math.exp((1-r)*d)}else u=(r-1)*l/(a-l),h=wt(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 Ff(e,t){if(typeof e=="string"){let h=vo(e,t);return Ff(h.values,h.group.codes)}let n=dr(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 d=ws(p);return p.map(g=>Math.abs(g-d))}),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(Re(n).groups));let i=s.length,a=bt(s).map(h=>oe((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 d=0;d<h.length;d++)p+=a[u+d];u+=h.length,m+=h.length*(p/h.length-f)**2}return m/=ot(a),di("Fligner-Killeen test of homogeneity of variances","Fligner-Killeen:med chi-squared",m,r-1)}function Df(e,t,n={}){if(V(n,r0,"moodTest"),typeof e=="string"){let E=Qn(e,t);return Df(E.x,E.y,n)}let r=Array.from(e).filter(Number.isFinite),o=Array.from(t).filter(Number.isFinite),s=Ge(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,b)=>b).sort((E,b)=>l[E]-l[b]),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 b=0;for(let M=u;M<=E;M++)b+=(M+1-(a+1)/2)**2;b/=E-u+1;for(let M=u;M<=E;M++)m[f[M]]=b;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 d=0;for(let E of m)d+=(E-p)**2;let g=i*p,w=i*c/(a*(a-1))*d,N=(h-g)/Math.sqrt(w),v=s==="less"?ie(N):s==="greater"?ie(N,{lowerTail:!1}):2*ie(-Math.abs(N));return{method:"Mood two-sample test of scale",alternative:s,statistic:N,statisticName:"Z",pValue:v,warnings:[]}}function Er(e,t){let n=0;for(let r=e.length-1;r>=0;r--)n=n*t+e[r];return n}var a0=[0,.221157,-.147981,-2.07119,4.434685,-2.706056],i0=[0,.042981,-.293762,-1.752461,5.682633,-3.582633],c0=[.544,-.39978,.025054,-6714e-7],l0=[1.3822,-.77857,.062767,-.0020322],f0=[-1.5861,-.31082,-.083751,.0038915],m0=[-.4803,-.082676,.0030302],u0=[-2.273,.459];function h0(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]=-oe((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+Er(a0,i),a=new Float64Array(n);a[0]=c;let l,f;if(e>5){let m=r[1]/s+Er(i0,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 Pf(e){let t=Re([e]).groups[0],n=t.length,r=h0(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 p0=1.90985931710274,d0=1.0471975511966;function Vf(e,t){if(Number.isNaN(e)||!(t>=3))return NaN;if(t===3){let i=p0*(Math.asin(Math.sqrt(1-e))-d0);return Math.max(i,0)}let n=Math.log(e);if(t<=11){let i=Er(u0,t);if(n>=i)return 1e-99;let c=Er(c0,t),a=Math.exp(Er(l0,t));return ie(-Math.log(i-n),{mean:c,sd:a,lowerTail:!1})}let r=Math.log(t),o=Er(f0,r),s=Math.exp(Er(m0,r));return ie(n,{mean:o,sd:s,lowerTail:!1})}var Kf=Math.log(2*Math.PI),$f=2e-14,zf=500;function g0(e){return Math.exp(-.5*e*e-.5*Kf)}function w0(e){return ie(e,{logP:!0})}function Wf(e,t=-1/0,n=!1){let r=t===-1/0?ga(e,0,2,0,$f,zf):ga(e,t,1,0,$f,zf),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 jf(e,t,n){let r=t.map(Math.sqrt),o=t.map(i=>Math.sqrt(1-i));return Wf(i=>{let c=0;for(let a=0;a<r.length;a++){let l=(r[a]*i+e)/o[a];if(!n){c+=w0(l);continue}let f=(r[a]*i-e)/o[a],m=ie(f)+ie(l,{lowerTail:!1});m<.5?c+=Math.log1p(-m):c+=Math.log(Math.max(0,l+f<0?ie(l)-ie(f):ie(f,{lowerTail:!1})-ie(l,{lowerTail:!1})))}return g0(i)*-Math.expm1(c)})}function xr(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,jf(e,t,r));let o=n/2,s=Math.log(2)+.5*Math.log(o)-.5*Kf-_c(o);return Math.min(1,Wf(c=>{let a=s-o*(Math.expm1(2*c)-2*c);return a<-745?0:Math.exp(a)*jf(e*Math.exp(c),t,r)},-1/0,!0))}var b0=["pAdjustMethod"],v0=["pAdjustMethod","alternative"],N0=["alternative"],y0=["pAdjustMethod","alternative"],M0=["method","altp"],E0=["alternative"],x0=["welch"],Yf="Ties are present. z-quantiles were corrected for ties.";function Sn(e,t){let{labels:n,groups:r}=Zn(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 wi(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 pn(t))s+=i*i*i-i;return{R:r,C:s/(12*(e.N-1))}}function Rr(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 R0(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=Yr(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 Lo(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 bi(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 $s=e=>ie(e,{lowerTail:!1}),vi=(e,t,n)=>as(Math.abs(e),t,n,{lowerTail:!1});function Ni(e){let t=Et(e.groups),n=0;for(let r of t.n)r>1&&(n+=r-1);return Co(n,t.ssWithin),{m:t.mean,s2:t.ssWithin/n,df:e.N-e.k,k:t.k}}function Co(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 T0(e,t,n={}){V(n,b0,"kwAllPairsDunnTest");let r=n.pAdjustMethod??"holm",o=Sn(e,t),{R:s,C:i}=wi(o),c=o.N*(o.N+1)/12-i,a=Rr(o.k,(f,m)=>Math.abs(s[f]-s[m])/Math.sqrt(c*(1/o.n[f]+1/o.n[m]))),l=R0(a.map(f=>f.map(m=>2*$s(m))),r);return H("kwAllPairsDunnTest",Lo(o,"Dunn's all-pairs test",a,l,r,i!==0?[Yf]:[]))}function S0(e,t,n={}){V(n,v0,"kwManyOneDunnTest");let r=Ge(n.alternative),o=n.pAdjustMethod??"single-step",s=Sn(e,t),{R:i,C:c}=wi(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=Bf(f,m,r,o);return H("kwManyOneDunnTest",bi(s,"Dunn's many-to-one test",f,u,o,c!==0?[Yf]:[]))}function Bf(e,t,n,r){return r==="single-step"?e.map(o=>n==="two.sided"?xr(o,t,1/0,!0):xr(n==="greater"?o:-o,t,1/0,!1)):Yr(e.map(o=>n==="two.sided"?2*$s(Math.abs(o)):$s(n==="greater"?o:-o)),r)}function A0(e,t,n={}){V(n,N0,"dunnettTest");let r=Ge(n.alternative),o=Sn(e,t),{m:s,s2:i,df:c}=Ni(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*xr(Math.abs(h),m,c,!1)):xr(r==="greater"?h:-h,m,c,!1));return H("dunnettTest",bi(o,`Dunnett's-test for multiple
4
+ comparisons with one control`,f,u,"single-step"))}function k0(e,t,n={}){V(n,y0,"manyOneUTest");let r=Ge(n.alternative),o=n.pAdjustMethod??"single-step",s=Sn(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 a=s.n[0],c=s.n.slice(1).map((m,u)=>u+1),i=c.map(m=>{let u=s.n[m],h=[...s.groups[0],...s.groups[m]],p=yt(h),g=0;for(let x=0;x<a;x++)g+=p[x];let d=a+u,v=d*(d+1)/2-g,N=a*u+a*(a+1)/2-g,b=a*u+u*(u+1)/2-v,E=0;for(let x of Nn(h))E+=x*x*x-x;let w=a*u/(d*(d-1))*((d*d*d-d)/12-E/12),y=(Math.min(N,b)-a*u/2)/Math.sqrt(w);return g<v?-y:y}),l=c.map(m=>s.n[m]/(s.n[m]+a)),f=Wm(i,l,r,o);return J("manyOneUTest",Qi(s,`Wilcoxon, Mann, Whittney U-test
7
- for multiple comparisons with one control`,i,f,o))}function Zg(e,t){let n=In(e,t),r=Cr(n.k,(s,a)=>{let c=n.n[s],i=n.n[a],l=[...n.groups[s],...n.groups[a]],f=yt(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*i-u),p=c+i,g=0;for(let v of Nn(l))g+=v*v*v-v;let d=c*i/(p*(p-1))*((p*p*p-p)/12-g/12);return Math.SQRT2*(h-c*i/2)/Math.sqrt(d)}),o=r.map(s=>s.map(a=>Number.isNaN(a)?NaN:Zi(a,n.k,1/0)));return J("dscfAllPairsTest",Go(n,"Dwass-Steele-Critchlow-Fligner all-pairs test",r,o,"single-step"))}function Jg(e,t,n={}){K(n,Ug,"tamhaneT2Test");let r=(()=>{if(t!==void 0)return In(e,t);let f=or(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),a=n.welch??!0,c=r.k*(r.k-1)/2,i=Cr(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=Cr(r.k,(f,m)=>{let u=i[f-1][m];if(Number.isNaN(u))return NaN;let h=a?Wa([s.variance[f],s.variance[m]],[r.n[f],r.n[m]]):r.n[f]+r.n[m]-2,p=2*Wt(Math.abs(u),h,{lowerTail:!1});return p>=1?1:-Math.expm1(c*Math.log1p(-p))});return J("tamhaneT2Test",Go(r,a?"Tamhane's T2-test for unequal variances":"Tamhane's T2'-test for unequal variances",i,l,a?"T2 (Sidak)":"T2' (Sidak)"))}function ew(e,t){let n=In(e,t),r=Tt(n.groups),o=r.mean,s=r.variance.map((i,l)=>i/n.n[l]),a=Cr(n.k,(i,l)=>(o[i]-o[l])/Math.sqrt(s[i]+s[l])*Math.SQRT2),c=Cr(n.k,(i,l)=>{let f=Wa([r.variance[i],r.variance[l]],[n.n[i],n.n[l]]);return Zi(a[i-1][l],n.k,f)});return J("gamesHowellTest",Go(n,"Games-Howell test",a,c,"none"))}function tw(e,t){let n=In(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 a=Cr(n.k,(i,l)=>(r[i]-r[l])/Math.sqrt(o*(1/n.n[i]+1/n.n[l])/2)),c=a.map(i=>i.map(l=>Number.isNaN(l)?NaN:Zi(l,n.k,s)));return J("tukeyTest",Go(n,"Tukey's test",a,c,"single-step"))}function nw(e,t,n={}){K(n,Wg,"dunnTest");let r=n.method??"none",o=n.altp??!1,s=In(e,t),a=sa(e,t).statistic,{R:c,C:i}=Xi(s),l=s.N*(s.N+1)/12-i,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)*aa(Math.abs(p))),h=rw(u,r);return{chi2:a,Z:f,P:u,PAdjusted:h,zValue:f,pValue:u,pAdjusted:h,comparisons:m,warnings:[]}}function rw(e,t){let n=e.length,r=(s,a)=>Math.min(1,-Math.expm1(a*Math.log1p(-s))),o=s=>{let a=e.map((i,l)=>l).sort((i,l)=>(s?e[l]-e[i]:e[i]-e[l])||i-l),c=new Array(n);return a.forEach((i,l)=>c[i]=l>0&&e[i]===e[a[l-1]]?c[a[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((a,c)=>Math.min(1,(n+1-s[c])*a))}case"hs":{let s=o(!1);return e.map((a,c)=>r(a,n+1-s[c]))}case"hochberg":{let s=o(!0);return e.map((a,c)=>Math.min(1,s[c]*a))}case"bh":{let s=o(!0);return e.map((a,c)=>Math.min(1,n/(n+1-s[c])*a))}case"by":{let s=o(!0),a=0;for(let c=1;c<=n;c++)a+=1/c;return e.map((c,i)=>Math.min(1,n/(n+1-s[i])*a*c))}default:throw new RangeError(`invalid method "${String(t)}"`)}}function ow(e,t,n={}){K(n,Bg,"glhtDunnett");let r=Je(n.alternative),o=In(e,t),{m:s,s2:a,df:c,k:i}=Ji(o),l=Math.sqrt(a),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,i)),g=u.map(N=>$(N,i)),d=f.map(N=>o.n[N]/(o.n[N]+o.n[0])),v=h.map(N=>r==="two.sided"?Lr(N,d,c,!0):Lr(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 Bm=["varEqual"],sw=2**-1022;function Hm(e,t,n={}){if(typeof e!="string"&&ct(t)&&K(t,Bm,"onewayTest"),K(n,Bm,"onewayTest"),typeof e=="string"){let d=Xl(e,t);return Hm(d.values,d.groups,n)}let r;t!==void 0&&!Array.isArray(t)?n=t:r=t;let o=Rr(e,r),s=Tt(o),a=s.groups,c=n.varEqual??!1,i=a.length;if(i<2)throw new RangeError("not enough groups");let l=a.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<i;b++)v+=l[b]*(f[b]-d)**2,N+=(l[b]-1)*m[b];p=u-i,Xo(p,s.ssWithin),h=v/(i-1)/(N/p)}else if(m.some(d=>!(d>=sw))&&(g=iw(o))!==void 0){let d=g.map((M,R)=>Um(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<i;M++){let R=N.reduce((A,T,q)=>q===M?A:A+T,0);E+=(R/b)**2/(l[M]-1)}E/=i*i-1;let w=[];for(let M=0;M<i;M++)for(let R=M+1;R<i;R++){let[A,T]=g[M].k>=g[R].k?[g[M],g[R]]:[g[R],g[M]],q=A.mean-Ve(T.mean,T.k-A.k);if(q===0)continue;let C=Um(q,A.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))/((i-1)*(1+2*(i-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<i;E++)E!==v&&(N+=1/(l[E]-1),b+=l[E]/m[E]*(f[E]-f[v])**2);N/=i*i-1,h=b/((i-1)*(1+2*(i-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<i;w++)N+=(1-d[w]/v)**2/(l[w]-1),b+=d[w]*f[w];N/=i*i-1,b/=v;let E=0;for(let w=0;w<i;w++)E+=d[w]*(f[w]-b)**2;h=E/((i-1)*(1+2*(i-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:[i-1,p],parameterName:["num df","denom df"],pValue:pt(h,i-1,p,!1,!1),warnings:[]}}function aw(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=Fm(t);return{method:"Shapiro-Wilk normality test",statistic:1-r,statisticName:"W",pValue:Dm(r,n),warnings:[]}}function iw(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 Um(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 ec(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 cw([[e,t],[n,r]]){let o=e+n,s=t+r,a=e+t,c=Math.max(0,a-s),i=Math.min(a,o),l=new Float64Array(i-c+1);for(let f=c;f<=i;f++)l[f-c]=yn(f,o,s,a,!1);return{lo:c,d:l}}function lw(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 ia=class{lo;hi;logNull;constructor(t){let n=0,r=new Float64Array([1]);for(let o of t){let s=cw(o);n+=s.lo,r=lw(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 i=this.logNull[c]+(this.lo+c)*o;r[c]=i,i>s&&(s=i)}for(let c=0;c<n;c++)r[c]=Math.exp(r[c]-s);let a=ec(r,0,n);for(let c=0;c<n;c++)r[c]=r[c]/a;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?ec(o,s,o.length):ec(o,0,s+1)}};function tc(e,t){return ir(e,t)}function Ym(e,t){if(t===e.lo)return 0;if(t===e.hi)return 1/0;let n=e.mean(1);return n===t?1:tc(r=>e.mean(r)-t,n>t)}function nc(e,t,n){if(t===e.hi)return 1/0;let r=e.cdf(t,1,!1);return r===n?1:tc(o=>n-e.cdf(t,o,!1),r<n)}function rc(e,t,n){if(t===e.lo)return 0;let r=e.cdf(t,1,!0);return r===n?1:tc(o=>e.cdf(t,o,!0)-n,r>n)}var fw=["alternative","correct","exact","confLevel"];function mw(e,t={}){K(t,fw,"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(oc);if(s.some(f=>f<2))throw new RangeError("sample size in each stratum must be > 1");let a=Je(t.alternative),c=t.correct??!0,i=t.exact??!1,l=t.confLevel??.95;if(Lt(l),r===2&&o===2){if(i&&e.some(m=>m.some(u=>u.some(h=>!Number.isInteger(h)))))throw new RangeError("the exact test needs integer counts");if(i&&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 i?pw(f,a,l):uw(f,s,a,c,l)}return dw(e,s,r,o)}function oc(e){return e.reduce((t,n)=>t+n.reduce((r,o)=>r+o,0),0)}function Qm(e){let t=0,n=0;for(let o of e)for(let s of o)for(let a of s)t=Math.max(t,a),n++;let r=Math.floor(Math.log2(t))+1+Math.ceil(Math.log2(8*n))-1020;return r<=0?0:r+r%2}function Zm(e,t){return e.map(n=>n.map(r=>r.map(o=>$(o,-t))))}function Gm(e,t,n){let r=0,o=0,s=0,a=0,c=0,i=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,A=u*h/d,T=Number.isFinite(R)?R:m/d*p,q=Number.isFinite(A)?A:u/d*h;a+=T,c+=q,i+=x*T,l+=x*q+M*T,f+=M*q}),{sa:r,se:o,sv:s,sR:a,sS:c,sPR:i,sPSQR:l,sQS:f}}function uw(e,t,n,r,o){let s=Gm(e,t,1),a=0;if(!t.every(Number.isFinite)||!Object.values(s).every(Number.isFinite)){a=Qm(e);let x=Zm(e,a);s=Gm(x,x.map(oc),$(1,-a))}let{sa:c,se:i,sv:l,sR:f,sS:m,sPR:u,sPSQR:h,sQS:p}=s,g=c-i,d=$(.5,-a),v=r&&Math.abs(g)>=d?d:0,N=$($t(Math.abs(g)-v,l),a),b=n==="two.sided"?vt(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)),-a/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 hw=2**24;function pw(e,t,n){let r=e.reduce((i,[[l,f],[m,u]])=>i+Math.min(l,u)+Math.min(f,m),1);if(r>hw)throw new RangeError(`the exact distribution of S has ${r} values, more than 2^24: use exact: false`);let o=new ia(e),s=e.reduce((i,l)=>i+l[0][0],0),a;if(t==="two.sided"){let i=o.density(1),l=i[s-o.lo]*(1+1e-7),f=[0,0];for(let m of i)m<=l&&(f=pe(f,[m,0]));a=Math.min(1,f[0])}else a=o.cdf(s,1,t==="greater");let c;if(t==="less")c=[0,nc(o,s,1-n)];else if(t==="greater")c=[rc(o,s,1-n),1/0];else{let i=(1-n)/2;c=[rc(o,s,i),nc(o,s,i)]}return{method:"Exact conditional test of independence in 2 x 2 x k tables",alternative:t,statistic:s,statisticName:"S",pValue:a,confInt:c,confLevel:n,estimate:Ym(o,s),estimateNames:"common odds ratio",nullValue:1,nullValueName:"common odds ratio",warnings:[]}}function dw(e,t,n,r){let{diff:o,V:s}=Xm(e,t,n,r,1),a=0;if(!t.every(Number.isFinite)||!o.every(Number.isFinite)||!s.every(f=>f.every(Number.isFinite))){a=Qm(e);let f=Zm(e,a);({diff:o,V:s}=Xm(f,f.map(oc),n,r,$(1,-a)))}let c=o.length,i=rt(s,o),l=0;for(let f=0;f<c;f++)l+=o[f]*i[f];return l=$(l,a),{method:"Cochran-Mantel-Haenszel test",statistic:l,statisticName:"Cochran-Mantel-Haenszel M^2",parameter:c,parameterName:"df",pValue:vt(l,c/2,2,!1,!1),warnings:[]}}var gw=2**-1022;function Xm(e,t,n,r,o){let s=(n-1)*(r-1),a=new Array(s).fill(0),c=Array.from({length:s},()=>new Array(s).fill(0));return e.forEach((i,l)=>{let f=t[l],m=i.map(p=>p.reduce((g,d)=>g+d,0)),u=Array.from({length:r},(p,g)=>i.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;a[d]+=i[g][p]-(Number.isFinite(v)?v:xs(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>=gw?y:m[g]*((g===b?1:0)-m[b]/f)*(((p===N?1:0)-u[N]/f)*(u[p]/(f-o)))}}}),{diff:a,V:c}}var Jm=new Map,ww=2**24,bw=2**32;function tu(e,t){let n=e+t,r=(e+1)*(Math.floor((n+1)**2/4)+1);if(2*r>ww||n*r>bw)throw new RangeError(`the exact distribution for m = ${e} and n = ${t} is too large to compute: use exact: false`)}function vw(e,t){let n=`${e},${t}`,r=Jm.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 a=s.reduce((u,h)=>u+h,0),c=Array.from({length:e+1},()=>new Float64Array(a+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(a+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<=a;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 i=c[e],l=0;for(;l<i.length&&i[l]===0;)l++;let f=i.length-1;for(;f>l&&i[f]===0;)f--;let m={min:l,probs:i.slice(l,f+1)};return Jm.set(n,m),m}function sc(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}=vw(t,n),a=kl(e)-o,c=0;if(r)for(let i=0;i<=Math.min(a,s.length-1);i++)c+=s[i];else for(let i=Math.max(a+1,0);i<s.length;i++)c+=s[i];return Math.min(c,1)}var eu=new Map;function Nw(e,t){let n=`${e},${t}`,r=eu.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 a=Array.from({length:e+1},()=>new Float64Array(s+1));a[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=a[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))}}a=h}let c=a[e],i=0;for(;c[i]===0;)i++;let l=c.length-1;for(;c[l]===0;)l--;let f={min:i,counts:c.slice(i,l+1)};return eu.set(n,f),f}function ca(e,t,n){let{min:r,counts:o}=Nw(t,n);if(e<=0)return r;if(e>=1)return r+o.length-1;let s=Ol(t+n,t),a=0;for(let c=0;c<o.length;c++)if(a+=o[c]/s,a>=e)return r+c;return r+o.length-1}var yw=["alternative","exact","confInt","confLevel"];function su(e,t,n={}){if(K(n,yw,"ansariTest"),typeof e=="string"){let w=rr(e,t);return su(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,a=o.length;if(s<1)throw new RangeError("not enough 'x' observations");if(a<1)throw new RangeError("not enough 'y' observations");let c=Je(n.alternative),i=n.confInt??!1,l=n.confLevel??.95;i&&Lt(l);let f=s+a,m=yt(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&&a<50&&!p),d=n.exact??(s<50&&a<50),v=new mt,N,b;if(g&&!p){tu(s,a);let w=x=>sc(x-1,s,a,!1),y=x=>sc(x,s,a,!0);if(c==="two.sided"){let x=Math.floor((s+1)**2/4)+Math.floor(s*a/2)/2;N=Math.min(1,2*(h>x?w(h):y(h)))}else c==="less"?N=w(h):N=y(h);i&&(b=Rw(r,o,c,l,v))}else{let w=au(h,u,p,s,a);N=c==="two.sided"?2*Math.min(ae(w),ae(w,{lowerTail:!1})):c==="less"?ae(w,{lowerTail:!1}):ae(w),i&&(b=Aw(r,o,c,l,d?new mt:v)),d&&p&&(v.add("cannot compute exact p-value with ties"),i&&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 au(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 a=0;for(let l of t)a+=l;let c=a/s,i=0;for(let l of t)i+=(l-c)**2;return(e-r*c)/Math.sqrt(r*o*(i/(s-1))/s)}function iu(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)<Mw))?Ew(e.map(f=>nu(f,n)).concat(t.map(f=>nu(f,1)))):xw(s.concat(t)),i=c.map(f=>Math.min(f,o-f+1)),l=0;for(let f=0;f<r;f++)l+=i[f];return{ab:l,a:i,ties:new Set(c).size<o}}var Mw=2**-1022;function nu(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]=ru(Math.abs(e)),[s,a]=ru(t),c=o/a,i=r-s;return c<1&&(c*=2,i-=1),[n,i,c]}function ru(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 ou(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 Ew(e){let t=e.map((r,o)=>o).sort((r,o)=>ou(e[r],e[o])),n=new Array(e.length);for(let r=0;r<t.length;){let o=r+1;for(;o<t.length&&ou(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 xw(e){let t=e.filter(s=>!Number.isNaN(s));if(t.length===e.length)return yt(e);let n=yt(t),r=0,o=t.length;return e.map(s=>Number.isNaN(s)?++o:n[r++])}function la(e,t,n){return e==="two.sided"?n(t):e==="greater"?[n(2*t)[0],1/0]:[0,n(2*t)[1]]}function Rw(e,t,n,r,o){let s=e.length,a=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 i=c.length;if(i===0)return o.add("cannot compute confidence set, returning NA"),{confInt:la(n,1-r,()=>[NaN,NaN]),estimate:NaN};let l=c.map((v,N)=>iu(e,t,v+(N<i-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=ca(v/2,s,a),b=ca(1-v/2,s,a);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=la(n,1-r,m),h=ca(.5,s,a),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 Aw(e,t,n,r,o){let s=e.length,a=t.length,c=(v,N)=>{let{ab:b,a:E,ties:w}=iu(e,t,v);return au(b,E,w,s,a)-N},i=[],l=(v,N)=>{v.length===0||N.length===0||i.push(ln(v)/Et(N),Et(v)/ln(N))};if(l(e.filter(v=>v>0),t.filter(v=>v>0)),l(e.filter(v=>v<=0),t.filter(v=>v<0)),i.some(v=>!Number.isFinite(v))){o.add("cannot compute asymptotic confidence set or estimator");return}if(i.length===0)return o.add("samples differ in location: cannot compute confidence set, returning NA"),{confInt:la(n,1-r,()=>[NaN,NaN]),estimate:NaN};let f=ln(i),m=Et(i),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=la(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 Tw=["alternative","confLevel"];function Sw(e,t,n={}){K(n,Tw,"brunnerMunzelTest");let r=Array.from(e).filter(M=>!Number.isNaN(M)),o=Array.from(t).filter(M=>!Number.isNaN(M)),s=r.length,a=o.length;if(s<2)throw new RangeError("not enough 'x' observations");if(a<2)throw new RangeError("not enough 'y' observations");let c=Je(n.alternative),i=n.confLevel??.95;fn(i);let l=yt(r.concat(o)),f=l.slice(0,s),m=l.slice(s),u=M=>M.reduce((R,A)=>R+A,0)/M.length,h=u(f),p=u(m),g=(M,R,A)=>{let T=(M.length+1)/2,q=0;for(let C=0;C<M.length;C++)q+=(M[C]-R[C]-A+T)**2;return q/(M.length-1)},d=g(f,yt(r),h),v=g(m,yt(o),p),N=(p-(a+1)/2)/s,b=s*d+a*v,E=s*a*(p-h)/(s+a)/Math.sqrt(b),w=b**2/((s*d)**2/(s-1)+(a*v)**2/(a-1)),y;c==="greater"?y=wt(E,w,!0,!1):c==="less"?y=wt(E,w,!1,!1):y=2*wt(-Math.abs(E),w,!0,!1);let x=bt((1-i)/2,w,!1,!1)*Math.sqrt(d/(s*a**2)+v/(a*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:i,estimate:N,estimateNames:"P(X<Y)+.5*P(X=Y)",warnings:[]}}var kw=[1.2337005501361697,7831619385924639e-32];function qw(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,a=0,c=e.slice(),i=0,l=n;for(;l>0;)l&1&&(s=s===void 0?c.slice():r(s,c),a+=i,a+=o(s)),l>>=1,l>0&&(c=r(c,c),i*=2,i+=o(c));return[s,a]}var Iw=2**33;function lu(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>Iw)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 fu(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]=Kt(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[a,c]=qw(s,r,e),i=a[(n-1)*r+n-1],l=c;for(let f=1;f<=e;f++)i=i*f/e,Math.abs(i)<2**-500&&(i*=2**500,l-=500);for(;l>1e3;)i*=2**1e3,l-=1e3;for(;l<-1e3;)i*=2**-1e3,l+=1e3;return i*2**l}function ac(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(kw,Kt(e,e)),s=o[0]+o[1],a=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+a))break}if(a+c===0)return t?0:1;let i=Math.sqrt(2*Math.PI)/e*(a+c);return t?i:1-i}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 ic(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]=Kt(e,t),o=Math.floor(e-n-r),s=0;for(let a=0;a<=o;a++){let c=pe(Dt(e-a,-n),[-r,0]);if(c[0]<=0)break;let i=pe(Dt(a,n),[r,0]),l=ot(i,[e,0]),f=ot(c,[e,0]);s+=Sl(a,e-a,l,f,!1)/(l[0]+l[1])}return Math.min(t*s,1)}var cu=2**28;function mu(e,t){let[n,r]=Kt(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)>cu)return NaN;s.sort((h,p)=>h.t-p.t);let a=o+1,c=new Float64Array(e+1),i=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);i.fill(0);for(let d=l;d<a;d++){let v=c[d];if(v===0)continue;let N=e-d,b=Math.min(N,Math.floor((N+1)*p)),E=a-d,w=gn(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:i[d+y]+=v*w,u++;w=b>0?gn(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:i[d+y]+=v*w,u++}if(u>2*cu)return NaN;[c,i]=[i,c]}if(f=h.t,h.up)a=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 uu(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,a;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);a=new Array(s+1).fill(!1);for(let h=1;h<s;h++)a[h]=u[h]!==u[h-1]}let c=e*t*n*(1-1e-10),i=(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);(a===void 0?p<s:a[p])&&i(u,h)?(g>0&&(m+=g*yn(u,t,n,p,!1)),f[h]=0):f[h]=g}[l,f]=[f,l],f.fill(0)}return Math.min(m,1)}var _w=["alternative","exact"];function hu(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 pu(e,t,n={}){if(K(n,_w,"ksTest"),typeof e=="string"){let N=rr(e,t);return pu(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),a=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&&lu(o,E),s==="two.sided")if(!y)x=ac(Math.sqrt(o)*E,!1);else{let M=2*ic(o,E);if(E>=.5)x=Math.min(1,M);else{let R=M<.01?mu(o,E):NaN;x=Number.isNaN(R)?1-fu(o,E):R}}else x=y?ic(o,E):Math.exp(-2*o*E*E);return J("ksTest",{method:`${y?"Exact":"Asymptotic"} one-sample Kolmogorov-Smirnov test`,alternative:hu(s,!0),exact:y,statistic:E,statisticName:a,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)),i=c.length;if(i<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/i;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*i<1e4,v;if(d)v=uu(p,o,i,g?{alternative:s,z:l.map(N=>N[0])}:{alternative:s});else{let N=o*i/(o+i);v=s==="two.sided"?ac(Math.sqrt(N)*p,!1):Math.exp(-2*N*p*p)}return J("ksTest",{method:`${d?"Exact":"Asymptotic"} two-sample Kolmogorov-Smirnov test`,alternative:hu(s,!1),exact:d,statistic:p,statisticName:a,pValue:Math.min(1,Math.max(0,v)),warnings:g&&!d?["p-value will be approximate in the presence of ties"]:[]})}var Lw=["poolSd","paired","pAdjustMethod","alternative"],Cw=["mu","paired","exact","correct","confLevel","tolRoot","pAdjustMethod","alternative"],Ow=["pAdjustMethod","correct"];function du(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=rn(t);for(let c=0;c<r.length;c++){let i=s[c];if(typeof i=="number"&&Number.isNaN(i)||n&&Number.isNaN(r[c]))continue;let l=o.get(i);l===void 0&&o.set(i,l=[]),l.push(r[c])}let a=Array.from(o.keys()).sort(Kn);return{levels:a,groups:a.map(c=>o.get(c))}}function cc(e,t,n){let r=[],o=[];for(let c=0;c<e-1;c++)for(let i=c+1;i<e;i++)r.push(t(i,c)),o.push([i-1,c]);let s=ro(r,n),a=Array.from({length:e-1},()=>new Array(e-1).fill(NaN));return o.forEach(([c,i],l)=>a[c][i]=s[l]),a}function lc(e,t,n,r,o=[]){return{method:e,pValue:n,rowNames:t.slice(1).map(Mt),colNames:t.slice(0,-1).map(Mt),pAdjustMethod:r,warnings:o}}function Fw(e,t,n={}){K(n,Lw,"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),a=n.pAdjustMethod??"holm",{levels:c,groups:i}=du(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=i.map(v=>v.length),h=Tt(i),p=h.mean,g=0;for(let v of u)v>1&&(g+=v-1);Xo(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"?wt(b,g,!0,!1):s==="greater"?wt(b,g,!1,!1):2*wt(-Math.abs(b),g,!0,!1)}}else m=r?"paired t tests":"t tests with non-pooled SD",f=(u,h)=>Ye(()=>En(i[u],i[h],{paired:r,alternative:s})).pValue;return lc(m,c,cc(l,f,a),a)}function Dw(e,t,n={}){K(n,Cw,"pairwiseWilcoxTest");let{pAdjustMethod:r,alternative:o,...s}=n,a=r??"holm",{levels:c,groups:i}=du(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 mt,u=cc(l,(h,p)=>{let g=Ye(()=>Wo(i[h],i[p],{...s,...o===void 0?{}:{alternative:o}}));f=g.method;for(let d of g.warnings)m.add(d);return g.pValue},a);return J("pairwiseWilcoxTest",lc(f,c,u,a,m.list))}function Pw(e,t,n={}){if(K(n,Ow,"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 a=n.pAdjustMethod??"holm",c=n.correct??!0,i=new mt,l=cc(s,(m,u)=>{let h=Ye(()=>Ti([r[m],r[u]],[o[m],o[u]],{correct:c}));for(let p of h.warnings)i.add(p);return h.pValue},a),f=Array.from({length:s},(m,u)=>u+1);return J("pairwisePropTest",lc("Pairwise comparison of proportions",f,l,a,i.list))}var Qo=[0,0];function fa(e){if(!(e[0]>0))return[Math.sqrt(e[0]),0];let t=Math.sqrt(e[0]),n=De(e,Kt(t,t));return Dt(t,n[0]/(2*t))}function ma(e,t){return Array.from({length:t},(n,r)=>e===void 0?[1,0]:fa([e[r],0]))}function so(e,t){return Hn(e,t,[]).fit}function Hn(e,t,n){let r=t.length,o=Math.min(e.length,r),s=e.map(f=>f.slice()),a=t.slice(),c=n.map(f=>f.slice()),i=[];for(let f=0;f<o;f++){let m=s[f],u=Qo;for(let v=f;v<r;v++)u=pe(u,Ce(m[v],m[v]));if(u[0]===0){i.push(Qo);continue}let h=fa(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=Qo;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(a);for(let v of c)d(v);i.push([-p[0],-p[1]])}let l=new Array(o).fill(Qo);for(let f=o-1;f>=0;f--){let m=a[f];for(let u=f+1;u<o;u++)m=De(m,Ce(s[u][f],l[u]));l[f]=ot(m,i[f])}return{fit:{qty:a,coef:l},qtv:c}}function ao(e,t=0,n=e.length){let r=Qo;for(let o=t;o<n;o++)r=pe(r,Ce(e[o],e[o]));return r}var Vw=1e-14;function $w(e,t,n,r,o,s,a,c){let i=y=>$(y,2*a),l=t.length,f=n.flatMap((y,x)=>ao(y)[0]>Vw*s[x]?[x]:[]),m=0,u=[],h=t.slice(),p=new Array(l).fill(0);if(f.length>0){let y=Ct(Array.from({length:l},(M,R)=>f.map(A=>n[A][R][0])));m=y.rank,u=y.pivot.slice(0,m).map(M=>f[M]);let x=so(u.map(M=>n[M]),t);h=x.qty,u.forEach((M,R)=>{for(let A=0;A<l;A++)p[A]=p[A]+n[M][A][0]*x.coef[R][0]})}let g=[];u.forEach((y,x)=>{let M=r[y],R=g.find(A=>A.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=ao(h,m)[0],N=Pi(p,v,d),b=g.filter(y=>y.term!==0);d>0&&qr(c,v)&&(v=0,b=b.map(y=>qr(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:A})=>{let T=A[0]/R/x;return[R,y(A[0]),y(A[0]/R),T,pt(T,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(i),w(y=>y),N]}function gu(e,t,n,r,o,s){if(s.weights!==void 0)throw new Error("weights are not supported in a multistratum aov() fit");let a=n??{type:"num",value:1,text:"1"},c=Jr({response:t.response,rhs:a},Object.keys(o)).variables,i=Wn(t,o,{...s,dropUnusedLevels:!0,contrastVariables:c},a),l=i.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=Wn({response:t.response,rhs:r},o,{...s.levels===void 0?{}:{levels:s.levels},allContrasts:"contr.helmert",intercept:i.terms.intercept,extraVariables:i.terms.variables,dropUnusedLevels:!0}),m=l.length,u=[...i.warnings,...f.warnings.filter(I=>!i.warnings.includes(I))],h=f.colNames.length,p=f.colNames.map((I,O)=>Ie([f.x.map(L=>L[O])]).groups[0]),g=f.x.map((I,O)=>p.map(L=>L[O])),d=h>0?Ct(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(O=>[O,0]),E=i.colNames.map((I,O)=>b(Ie([i.x.map(L=>L[O])]).groups[0])),{groups:w,k:y}=Ie([l]),{fit:x,qtv:M}=Hn(N.map(I=>b(p[I])),b(w[0]),E),R=w[0].map(I=>[1,I]),A=E.map(I=>ao(I)[0]),T=[];N.forEach((I,O)=>{let L=f.assign[I],D=L===0?"(Intercept)":f.terms.termLabels[L-1],S=T.find(F=>F.name===D);S===void 0&&T.push(S={name:D,idx:[]}),S.idx.push(O)}),m>v&&T.push({name:"Within",idx:Array.from({length:m-v},(I,O)=>v+O)});let q=T.map(I=>$w(I.name,I.idx.map(O=>x.qty[O]),M.map(O=>I.idx.map(L=>O[L])),i.assign,i.terms.termLabels,A,y,R)),C=q.map(I=>I[0]),_={formula:e,multistratum:!0,strata:C,rows:i.frame.rows,warnings:u};return Yt.set(_,q.map(I=>I[1])),q.some((I,O)=>I[2]&&T[O].name!=="(Intercept)")&&Vi.set(_,[Di]),_}var jw=["weights","contrasts","levels","intercept"],zw=["level"],Kw=["interval","level"],fc=2**-1022;function mc(e,t,n){let r=t.y;if(r===void 0)throw new TypeError("the formula has no response");let o=ua(t.x,r,t.frame.weights,t.terms.intercept,!0),s=o.result,a=[...t.warnings];if(!n){let i=o.yScale??0,l=o.fitRows.map(h=>$(s.fittedValues[h],-i)),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)&&a.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:a};return t.frame.weights!==void 0&&(c.weights=t.frame.weights),c}function Or(e,t,n={}){if(K(n,jw,"lm"),typeof e=="string"){let r=Wn(Sn(e),t,{...n,dropUnusedLevels:!0}),o=lf(mc(e,r,!1),n);return J("lm",Xa(o,cf(t,r.terms.variables)))}return Ww(e,t,n)}function Ww(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 a=n.weights===void 0?void 0:Array.from(n.weights);if(a!==void 0&&a.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))&&(a===void 0||!Number.isNaN(a[h]))),i=a?.filter((u,h)=>c[h]);if(i?.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=ua(l,f,i,r).result;return m.rows=c.flatMap((u,h)=>u?[h]:[]),Xa(m,s.length)}function Bw(e,t,n,r,o,s,a){let c=s.rank,i=s.pivot.slice(0,c),l=n?.map(w=>$(w,-r)),f=ma(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]]>=fc)return w;let M=fa([x,0]);return[$(M[0],-r/2),$(M[1],-r/2)]}),m=i.map(w=>{let y=o.map((M,R)=>At(f[R],e[M][w])),x=nf(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}=so(m.map(({c:w})=>w),o.map((w,y)=>At(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)??[];i.forEach((w,y)=>g[w]=p[y][0]);let d=e.map(w=>{let y=[0,0];return i.forEach((x,M)=>y=pe(y,At(p[M],w[x]))),y}),v=ao(u,c)[0],N=o.map(w=>l?.[w]??1),b=[0,0];if(a){let w=[0,0],y=[0,0];o.forEach((x,M)=>{w=pe(w,[N[M],0]),y=pe(y,At(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,At(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 ua(e,t,n,r,o=!1){let s=Ie([t.filter((l,f)=>n===void 0||n[f]!==0)]).k;if(s===0)return wu(bu(e,t,n,r,o),e,t,n);let a=bu(e,t.map(l=>$(l,-s)),n,r,o),c=a.result,i=l=>$(l,s);return c.coefficients=c.coefficients.map(i),c.residuals=c.residuals.map(i),c.fittedValues=c.fittedValues.map(i),c.sigma=i(c.sigma),c.coefTable.estimate=c.coefTable.estimate.map(i),c.coefTable.stdError=c.coefTable.stdError.map(i),wu({...a,effects:a.effects.map(i),rss:$(a.rss,2*s),yScale:s,rssScaled:a.rss},e,t,n)}function wu(e,t,n,r){if(r===void 0)return e;let o=e.result,s=o.coefficients.flatMap((a,c)=>Number.isNaN(a)?[]:[c]);return r.forEach((a,c)=>{if(a!==0)return;let i=!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)?Io(s.map(m=>l[m]),s.map(m=>o.coefficients[m]),f):f,i=!0}(i||!Number.isFinite(o.residuals[c]))&&(o.residuals[c]=n[c]-o.fittedValues[c])}),e}var uc=new WeakMap;function hc(e){return uc.get(e)??0}function Uw(e){return e.qr.every(t=>t.every(Number.isFinite))&&e.qraux.every(Number.isFinite)}function bu(e,t,n,r,o){if(t.length===0)throw new RangeError("0 (non-NA) cases");let s=t.map((V,Y)=>n===void 0||n[Y]!==0),a=e.filter((V,Y)=>s[Y]),c=t.filter((V,Y)=>s[Y]),i=n?.filter((V,Y)=>s[Y]),l=c.length;if(l===0)throw new RangeError("all weights are zero");if(a.some(V=>V.some(Y=>!Number.isFinite(Y))))throw new RangeError("NA/NaN/Inf in 'x'");if(c.some(V=>!Number.isFinite(V)))throw new RangeError("NA/NaN/Inf in 'y'");if(i?.some(V=>!Number.isFinite(V)))throw new RangeError("non-finite weights not allowed");let f=t.flatMap((V,Y)=>s[Y]?[Y]:[]),m=i===void 0?1:Et(i),u=Number.isFinite(m)?2*Math.floor(Math.floor(Math.log2(m))/2):0,h=0,p=i?.map(Math.sqrt),g=p===void 0?a:a.map((V,Y)=>V.map(j=>j*p[Y])),d=Ct(g);i!==void 0&&Number.isFinite(m)&&u+2>0&&!Uw(d)&&(h=u+2,p=i.map(V=>$(Math.sqrt(V),-h/2)),g=a.map((V,Y)=>V.map(j=>j*p[Y])),d=Ct(g),uc.set(d,h));let v=p===void 0?c:c.map((V,Y)=>V*p[Y]),N=d.rank,b=V=>$(V,-u),E=o?Bw(e,t,n,u,f,d,r):void 0,w=E?.coefficients??Os(d,v),y=E?.residuals??[],x=E?.fittedValues??[],M=l-N,R=i?.map(b)??new Array(l).fill(1),A=E?.rss??0;if(E===void 0){let V=eo(d,v),Y=p===void 0?V:V.map((H,W)=>H/p[W]),j=0;t.forEach((H,W)=>{if(s[W])y.push(Y[j]),x.push(H-Y[j]),j++;else{let te=e[W].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++)A+=R[H]*Y[H]**2}let T=x.filter((V,Y)=>s[Y]),q=0;if(i!==void 0&&i.some(V=>V!==0&&$(V,-u)<fc)){let V=f.flatMap((Y,j)=>{let H=y[Y];return H===0?[]:[{w:i[j],r:H,a:Math.floor(Math.log2(Math.abs(H)))}]});if(V.length>0&&V.every(Y=>Number.isFinite(Y.r))){let Y=Math.max(...V.map(W=>Math.log2(W.w)+2*W.a)),j=2*Math.floor(Math.floor(Y)/2),H=0;for(let W of V)H+=$(W.w,2*W.a-j)*$(W.r,-W.a)**2;A=H,q=j-u}}let C=A/M,_=$(Math.sqrt(C),(u+q)/2),I=h===0?0:(h-u)/2,O=d.qr.slice(0,N).map(V=>V.slice(0,N).map(Y=>h===0?$(Y,-u/2):Y)),{inv:L,e:D}=N>0?Fo(O):{inv:[],e:[]},F=d.pivot.slice(0,N).map((V,Y)=>[V,Y]).sort((V,Y)=>V[0]-Y[0]),k=F.map(([V])=>w[V]),P=F.map(([,V])=>$(Math.sqrt(L[V][V]*C),q/2-D[V]-I)),B=k.map((V,Y)=>V/P[Y]),Q=B.map(V=>2*wt(-Math.abs(V),M,!0,!1)),re={coefficients:w,residuals:y,fittedValues:x,rank:N,dfResidual:M,coefTable:{estimate:k,stdError:P,tValue:B,pValue:Q},sigma:_,rSquared:0,adjRSquared:0,intercept:r,qr:d,x:e,rows:e.map((V,Y)=>Y)};n!==void 0&&(re.weights=n);let z=r?1:0,ee=N-z,U={result:re,fitRows:f,effects:(E?.effects??[]).map(V=>$(V,u/2)),rss:$(A,u+q)};if(ee===0)return U;let ue=0;if(E!==void 0)ue=E.mss;else if(r){let V=R.reduce((j,H)=>j+H,0),Y=0;for(let j=0;j<l;j++)Y+=R[j]*T[j]/V;for(let j=0;j<l;j++)ue+=R[j]*(T[j]-Y)**2}else for(let V=0;V<l;V++)ue+=R[V]*T[V]**2;re.rSquared=ue/(ue+$(A,q)),re.adjRSquared=1-(1-re.rSquared)*((l-z)/M);let Z=$(ue/ee/C,-q);return re.fStatistic={value:Z,numdf:ee,dendf:M},re.fPValue=pt(Z,ee,M,!1,!1),U}function Nu(e,t){let n=(1-e)/2;return n<1e-8?bt(n,t,!1,!1):bt((1+e)/2,t,!0,!1)}function Hw(e,t={}){K(t,zw,"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,a=e.coefficients.map(()=>NaN),c=e.coefficients.map(()=>NaN),i=0;return e.coefficients.forEach((l,f)=>{Number.isNaN(l)||(a[f]=o[i]-r*s[i],c[f]=o[i]+r*s[i],i++)}),{lower:a,upper:c}}function vu(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 Yw(e,t,n={}){K(n,Kw,"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,a=e.qr.pivot.slice(0,s),c=e.qr.qr.slice(0,s).map(b=>b.slice(0,s)),i=-(uc.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 Ka(E,0)?Io(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=a.map(x=>b[x]),w=Ba(m(E));if(w>=fc&&w<1/0||!E.every(Number.isFinite)||E.every(x=>x===0))return i===0||!(w>0&&w<1/0)?w*e.sigma:vu(w,e.sigma,i);let y=Math.floor(Math.log2(Et(E.map(Math.abs))));for(let x of[0,600]){let M=Ba(m(E.map(R=>$(R,-(y+x)))));if(M<1/0)return M===0?M*e.sigma:vu(M,e.sigma,y+x+i)}return w*e.sigma}),h=n.interval??"none";if(h!=="none"&&h!=="confidence"&&h!=="prediction")throw new RangeError(`invalid 'interval': ${String(h)}`);let p=sf(e.qr,e.rank,o),g=p.length>0?{nonEstimable:p,warnings:[of]}:{};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"],yu=["confLevel"],Mu=["sigmaX","sigmaY","mu","alternative","confLevel"];function Xw(e,t,n={}){return K(n,Gw,"aov"),typeof e=="string"?J("aov",Eu(e,t,n)):J("aov",Qw(e,t))}function Eu(e,t,n){let r=Sn(e,!0),{treatment:o,errors:s}=Yl(r.rhs);if(s.length>1)throw new Error(`there are ${s.length} Error terms: only 1 is allowed`);return s.length===1?gu(e,r,o,s[0],t,n):mc(e,Wn(r,t,{...n,dropUnusedLevels:!0},o),!0)}function Qw(e,t){let{labels:n,groups:r}=or(e,t);if(r.length<2)throw new RangeError("contrasts can be applied only to factors with 2 or more levels");let o=r.flat(),s=r.flatMap((a,c)=>a.map(()=>n[c]));return Eu("y ~ g",{y:o,g:s},{levels:{g:n}})}function xu(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),a=t-1,c=n-t,i=r/a,l=o/c,f=i/l,m=u=>$(u,2*s);return{df:[a,c],sumSq:[m(r),m(o)],meanSq:[m(i),m(l)],fValue:f,pValue:c>0?pt(f,a,c,!1,!1):NaN}}function Zw(e,t,n={}){ct(t)&&K(t,yu,"TukeyHSD"),K(n,yu,"TukeyHSD");let r;t!==void 0&&!Array.isArray(t)?n=t:r=t;let{labels:o,groups:s}=or(e,r),a=s.length;if(a<2)throw new RangeError("contrasts can be applied only to factors with 2 or more levels");let c=n.confLevel??.95;fn(c);let l=s.reduce((v,N)=>v+N.length,0)-a;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=$l(c,a,l),g=[],d={names:[],i:[],j:[],diff:[],lwr:[],upr:[],pAdj:g,pAdjusted:g,warnings:[]};for(let v=0;v<a-1;v++)for(let N=v+1;N<a;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(As(Math.abs(b)/E,a,l,{lowerTail:!1}))}return d}function Jw(e,t,n={}){ct(t)&&K(t,Mu,"zTest"),K(n,Mu,"zTest");let r;t!==void 0&&!ArrayBuffer.isView(t)&&!Array.isArray(t)?n=t:r=t;let{sigmaX:o,sigmaY:s}=n,a=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 A of[o,s])if(A!==void 0&&!(A>0&&A<1/0))throw new RangeError("sigmaX and sigmaY must be positive finite numbers");if(Number.isNaN(a))throw new RangeError("mu must be a number");let c=Je(n.alternative),i=n.confLevel??.95;if(!(i>=0&&i<=1))throw new RangeError("confLevel must be between 0 and 1");let l=Array.from(e).filter(A=>!Number.isNaN(A)),f=r===void 0?void 0:Array.from(r).filter(A=>!Number.isNaN(A));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=er(h)[0],N=f===void 0?NaN:er(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-$(a,-u))/b,y,x;if(c==="less")y=ae(w),x=[-1/0,E+ie(i)*b];else if(c==="greater")y=ae(w,{lowerTail:!1}),x=[E-ie(i)*b,1/0];else{y=2*ae(-Math.abs(w));let A=(1-i)/2,T=(A<1e-8?ie(A,{lowerTail:!1}):ie(1-(1-i)/2))*b;x=[E-T,E+T]}x=[$(x[0],u),$(x[1],u)];let M=f!==void 0,R=(A,T)=>{if(!(u>0&&A.some((C,_)=>C!==0&&Math.abs(T[_])<2**-1022)))return $(er(T)[0],u);let q=Math.max(Ie([A]).k,0);return $(er(q===0?A:A.map(C=>$(C,-q)))[0],q)};return{method:M?"Two-sample z-Test":"One-sample z-Test",alternative:c,statistic:w,statisticName:"z",pValue:y,confInt:x,confLevel:i,estimate:M?[R(l,h),R(f,p)]:R(l,h),estimateNames:M?["mean of x","mean of y"]:"mean of x",nullValue:a,nullValueName:M?"difference in means":"mean",warnings:[]}}var e1=["parm","level","test"],Ru={epsilon:1e-300,maxit:100,untilCoefficients:!0};function pc(e,t,n,r,o=1e-11){let s=[];for(let g=0;g<t.length;g++)n[g]>0&&s.push(g);let a=0,c=0;for(let g of s)a+=n[g],c+=n[g]*t[g];if(r&&!(Number.isFinite(a)&&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;a=0,c=0;for(let v of s){let N=$(n[v],-d);a+=N,c+=N*t[v]}}}let i=r?c/a:0,l=0;for(let g of s)l+=n[g]*(t[g]-i)**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=Ct(s.map((g,d)=>e[g].map(v=>v*m[d])),{tol:o}),h=eo(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]-i)),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 t1(e,t={}){K(t,e1,"glmConfint");let{fit:n,k:r}=Bn(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 a=n.coefficients.length,c=t.parm??Array.from({length:a},(_,I)=>I);for(let _ of c)if(!Number.isInteger(_)||_<0||_>=a)throw new RangeError(`invalid coefficient index ${_}`);let i=n.model,l=n.coefficients.map((_,I)=>I).filter(_=>!Number.isNaN(n.coefficients[_])),f=new mt,m=Ar(i,l,f,{...Ru,start:i.start===void 0?void 0:l.map(_=>i.start[_])}),u=new Array(a).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=Ye(()=>ei(p)),d=g.dispersion,v=new Array(a).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=i.x.map(_=>l.map(I=>_[I])),E=m.fit.eta,w=(_,I)=>{let O=l.filter(S=>S!==_),L=Ar(i,O,new mt,{...Ru,start:void 0,etastart:E,extraOffset:i.x.map(S=>S[_]*I)}).fit;if(!L.converged)return h=!0,NaN;let D=s==="LRT"?L.deviance-m.fit.deviance:pc(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,A=R?[-x,x]:[ie(y),y<1e-8?ie(y,{lowerTail:!1}):ie((1+o)/2)],T=c.map(()=>NaN),q=c.map(()=>NaN),C=(_,I,O)=>{let L=O===0?-1:1;h=!1;let D=NaN,S=NaN;for(let z=0;z<=30;z++){let ee=u[_]+L*2**z*v[_],U=w(_,ee);if(Math.abs(U)>=Math.abs(I)){D=ee,S=U;break}}if(Number.isNaN(D)||h)return NaN;let F=z=>z===u[_]?-I:w(_,z)-I,[k,P]=O===0?[D,u[_]]:[u[_],D],[B,Q]=O===0?[S-I,-I]:[-I,S-I],re;try{re=Ht(F,k,P,{tol:1e-14*v[_],maxIter:500,fLower:B,fUpper:Q})}catch(z){if(!h)throw z;re=NaN}return h?NaN:re};return c.forEach((_,I)=>{if(Number.isNaN(u[_]))return;if(!R){T[I]=C(_,A[0],0),q[I]=C(_,A[1],1);return}let O=[-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 F=0;for(let k=0;k<5;k++){let P=1;for(let B=0;B<5;B++)B!==k&&(P*=(S-O[B])/(O[k]-O[B]));F+=P*L[k]}return u[_]+F};T[I]=D(A[0]),q[I]=D(A[1])}),{lower:T.map(_=>$(_,r)),upper:q.map(_=>$(_,r))}}var n1=["scope","direction","k","scale","steps"],r1=["lower","upper"],o1=["scope","direction","k","scale","steps"],s1=["lower","upper"],a1=["scope","test","k","scale"],i1=["scope","test","k","scale"],c1=["k","scale"],Gt=e=>"linearPredictors"in e;function bc(e,t,n){if(typeof e!="object"||e===null||typeof e.formula!="string"||!Array.isArray(e.termLabels))throw new TypeError(`${n} needs a fit of lm or glm with a formula`);if(typeof t!="object"||t===null)throw new TypeError("data must be an object of columns");let r=Gt(e),o=ff(e);if(o===void 0&&((r?e.model.priorGiven.some(f=>f!==1)||e.model.hasOffset:e.weights!==void 0)||r))throw new TypeError(`${n} needs the fit as returned by lm or glm (its options are not known)`);let s=e.formula.indexOf("~"),a=e.formula.slice(0,s).trim(),c=r?e.model.intercept:e.coefNames[0]==="(Intercept)",i=Object.values(t)[0]?.length??0;return{data:t,glm:r,lhs:a,intercept:c,options:o??{},nAll:i}}function dc(e,t){return t.length===0?`${e.lhs} ~ ${e.intercept?"1":"0"}`:`${e.lhs} ~ ${t.join(" + ")}${e.intercept?"":" - 1"}`}function Au(e,t){let n=s=>s instanceof Float64Array?Float64Array.from(t,a=>s[a]):Array.isArray(s)&&s.length===e.nAll?t.map(a=>s[a]):s,r=Object.fromEntries(Object.entries(e.data).map(([s,a])=>[s,n(a)])),o={...e.options};for(let s of["weights","offset"])typeof o[s]=="object"&&o[s]!==null&&(o[s]=n(o[s]));return{data:r,options:o}}function Zo(e,t,n){let{data:r,options:o}=n===void 0?{data:e.data,options:{...e.options}}:Au(e,n),s=dc(e,t),a=Ye(()=>e.glm?sr(s,r,o):Or(s,r,o));if(n!==void 0){let i=(e.glm?a.model.rows:a.rows).map(l=>n[l]);e.glm?a.model.rows=i:a.rows=i}return a}var Tu=e=>Gt(e)?e.model.rows:e.rows,vc=e=>Tu(e).length,ha=e=>Gt(e)?e.priorWeights.filter(t=>t>0).length:e.nObs,fr=e=>Gt(e)?e.deviance:e.rss,Su=e=>e.rank;function Nc(e){let t=e.rss;if(t>1e-290&&t<1/0)return Math.log(t);let n=e.weights,r=[];if(e.residuals.forEach((s,a)=>{let c=n?.[a]??1;c>0&&s!==0&&r.push(Math.log(Math.abs(s))+.5*Math.log(c))}),r.length===0)return-1/0;let o=Math.max(...r);return 2*o+Math.log(r.reduce((s,a)=>s+Math.exp(2*(a-o)),0))}function l1(e){return e.rss>1e-290&&e.rss<1/0?Math.log(e.rss):e.yScale!==void 0&&e.rssScaled!==void 0&&e.rssScaled>0?Math.log(e.rssScaled)+2*e.yScale*Math.LN2:Math.log(e.rss)}function pa(e,t,n){let r=vc(e),o=r-e.dfResidual;return Gt(e)?[o,e.aic+(t-2)*o]:n>0?[o,e.rss/n-r+t*o]:[o,r*(Nc(e)-Math.log(r))+t*o]}function es(e){return e.termLabels.map((t,n)=>{let r=e.variables.filter((o,s)=>(e.factors[s]?.[n]??0)!==0);return{label:t,vars:r,key:[...r].sort().join("\0")}})}function Jo(e,t,n){if(typeof e!="string")throw new TypeError(`the ${n} scope must be a formula such as "~ x1 + x2"`);let r=e.includes("~")?e:`~ ${e}`,o=Sn(r),s=c=>typeof c=="object"&&c!==null&&(c.type==="dot"||Object.values(c).some(i=>typeof i=="object"&&s(i)));if(s(o.rhs))throw new RangeError(`'.' is not supported in the ${n} scope: list its terms`);let a=Jr({response:void 0,rhs:o.rhs},Object.keys(t));return a.termLabels.map((c,i)=>{let l=a.variables.filter((f,m)=>(a.factors[m]?.[i]??0)!==0);return{label:c,vars:l,key:[...l].sort().join("\0")}})}var ku=(e,t)=>t.vars.length<e.vars.length&&t.vars.every(n=>e.vars.includes(n));function qu(e,t){return e.filter(n=>!t.some(r=>r.key===n.key)&&!e.some(r=>ku(r,n)))}function Iu(e,t){let n=r=>e.some(o=>o.key===r.key);return t.filter(r=>!n(r)&&t.every(o=>!ku(r,o)||n(o)))}function _u(e,t){if(Gt(e))return;let n=e.weights,r=0;e.y.forEach((o,s)=>r+=(n?.[s]??1)*o*o),e.rss<1e-10*r&&t.add("attempting model selection on an essentially perfect fit is nonsense")}function io(e,t){for(let n of t.warnings)(Gt(t)||n!=="essentially perfect fit: summary may be unreliable")&&e.add(n)}var Lu=e=>e.priorWeights.filter(t=>t>0).length,yc=e=>e.family==="binomial"||e.family==="poisson";function Cu(e,t){return e.family==="gaussian"?Lu(e)*Math.log(t/e.deviance):yc(e)?t-e.deviance:(t-e.deviance)/ar(e)}function gc(e,t,n){let r=vc(e),o=Su(e),s=Gt(e)?NaN:Nc(e),a=Gt(e)?e.aic+(t-2)*o:n>0?e.rss/n-r+t*o:r*(s-Math.log(r))+t*o;return{label:"<none>",df:NaN,sumOfSq:NaN,deviance:fr(e),aic:a,rank:o,dfResidual:e.dfResidual,logDev:s,fit:e}}function wc(e,t,n,r,o,s,a,c,i=Math.log(n),l){let f=e.fit,m=s?r-e.rank:e.rank-r,u;if(Gt(f))u=e.aic+Cu(f,n)+a*(r-e.rank);else{let h=vc(f);u=c>0?n/c-h+a*r:h*(i-Math.log(h))+a*r}return{label:t,df:m,sumOfSq:s?e.deviance-n:n-e.deviance,deviance:n,aic:u,rank:r,dfResidual:o,logDev:i,fit:l}}function Ou(e,t){Gt(e)&&!yc(e)&&ar(e,t)}function Fu(e,t,n,r,o){_u(e,o),Ou(e,o);let s=gc(e,n,r),a=es(e),c=[s];for(let i of t){let l=a.findIndex(m=>m.key===i.key)+1,f=e.assign.flatMap((m,u)=>m===l?[]:[u]);if(Gt(e)){let m=new mt,u=Ar(e.model,f,m);for(let h of m.list)o.add(h);c.push(wc(s,i.label,u.fit.deviance,u.fit.rank,Lu(e)-u.fit.rank,!1,n,r))}else{let m=ua(e.x.map(u=>f.map(h=>u[h])),e.y,e.weights,e.coefNames[0]==="(Intercept)"&&f[0]===0,!0);c.push(wc(s,i.label,m.rss,m.result.rank,m.result.dfResidual,!1,n,r,l1(m)))}}return c}function Du(e,t,n,r,o,s){let a=Tu(t),c=t,i=[...new Set(n.flatMap(m=>m.vars))].filter(m=>!t.variables.includes(m));if(i.length>0){let m=e.options.levels,u=Au(e,a).data,h=qs(u,i,m===void 0?{}:{levels:m}).rows.map(p=>a[p]);h.length<a.length&&(s.add(`using the ${h.length}/${a.length} rows from a combined fit`),a=h,c=Zo(e,t.termLabels,a))}_u(c,s),Ou(c,s);let l=gc(c,r,o);Gt(c)&&c!==t&&(l.aic=gc(t,r,o).aic);let f=[l];for(let m of n){let u=Zo(e,[...t.termLabels,m.label],a);io(s,u),f.push(wc(l,m.label,fr(u),Su(u),u.dfResidual,!0,r,o,Gt(u)?NaN:Nc(u),u))}return f}function Pu(e,t,n){let r=e[0],o=r.fit,s=[NaN],a=[NaN];for(let i of e.slice(1)){let l=n?i:r,f=Math.abs(i.deviance-r.deviance),m,u;t==="F"?(m=f/i.df/(l.deviance/l.dfResidual),u=dn(m,i.df,l.dfResidual,{lowerTail:!1})):(m=Gt(o)?Math.abs(Cu(o,i.deviance)):f/(l.deviance/l.dfResidual),u=Nt(m,i.df,{lowerTail:!1})),s.push(i.df===0?NaN:m),a.push(i.df===0?NaN:u)}let c=t==="F"?"F value":Gt(o)&&yc(o)?"LRT":"scaled dev.";return{statistic:s,pValue:a,statisticName:c}}function Vu(e,t,n,r){let o={term:e.map(s=>s.label),df:e.map(s=>s.df),deviance:e.map(s=>s.deviance),aic:e.map(s=>s.aic),criterion:n>0&&!t?"Cp":"AIC",warnings:r.list};return t||(o.sumOfSq=e.map(s=>s.sumOfSq)),o}function Yn(e,t,n=!1){if(typeof e!="number"||Number.isNaN(e))throw new TypeError(`${t} must be a number`);if(n?!(e>=0):!Number.isFinite(e))throw new RangeError(`${t} must be ${n?"non-negative":"finite"}`);return e}function f1(e,t={}){K(t,c1,"extractAIC");let n=Yn(t.k??2,"k"),r=Yn(t.scale??0,"scale",!0);return pa(e,n,r)}function m1(e,t,n={}){K(n,a1,"drop1");let r=bc(e,t,"drop1"),o=Yn(n.k??2,"k"),s=r.glm?0:Yn(n.scale??0,"scale",!0),a=es(e),c=n.scope===void 0?qu(a,[]):Jo(n.scope,t,"drop1").map(m=>{let u=a.find(h=>h.key===m.key);if(u===void 0)throw new RangeError("scope is not a subset of term labels");return u}),i=new mt;io(i,e);let l=Fu(e,c,o,s,i),f=Vu(l,r.glm,s,i);return n.test!==void 0&&n.test!=="none"&&Object.assign(f,{test:n.test==="F"?"F":"Chisq"},Pu(l,n.test==="F"?"F":"Chisq",!1)),f.warnings=i.list.filter(m=>!e.warnings.includes(m)),J("drop1",f,e)}function u1(e,t,n={}){K(n,i1,"add1");let r=bc(e,t,"add1");if(n.scope===void 0)throw new TypeError("no terms in scope for adding to object");let o=Yn(n.k??2,"k"),s=r.glm?0:Yn(n.scale??0,"scale",!0),a=Iu(es(e),Jo(n.scope,t,"add1")),c=new mt;io(c,e);let i=Du(r,e,a,o,s,c),l=Vu(i,r.glm,s,c);return n.test!==void 0&&n.test!=="none"&&Object.assign(l,{test:n.test==="F"?"F":"Chisq"},Pu(i,n.test==="F"?"F":"Chisq",!0)),l.warnings=c.list.filter(f=>!e.warnings.includes(f)),J("add1",l,e)}function $u(e,t,n,r){let o=bc(e,t,r),s=n.direction??"both";if(!["both","backward","forward"].includes(s))throw new RangeError(`'direction' should be one of "both", "backward", "forward"`);let a=Yn(n.k??2,"k"),c=o.glm?0:Yn(n.scale??0,"scale",!0),i=Yn(n.steps??1e3,"steps",!0),l=es(e),f=[],m=l,u=n.scope;if(u!==void 0)if(typeof u=="string")m=Jo(u,t,"upper");else if(typeof u=="object"&&u!==null)u.lower!==void 0&&(f=Jo(u.lower,t,"lower")),u.upper!==void 0&&(m=Jo(u.upper,t,"upper"));else throw new TypeError("scope must be a formula or { lower, upper }");for(let M of l)if(!m.some(R=>R.key===M.key))throw new RangeError(`upper scope has term \u2018${M.label}\u2019 not included in model`);for(let M of f)if(!l.some(R=>R.key===M.key))throw new RangeError(`lower scope has term \u2018${M.label}\u2019 not included in model`);let h=s!=="forward",p=s!=="backward",g=new mt;io(g,e);let d=e,[,v]=pa(d,a,c);if(Number.isNaN(v))throw new RangeError(`AIC is not defined for this model, so '${r==="step"?"step":"stepAIC"}' cannot proceed`);if(v===-1/0)throw new RangeError(`AIC is -infinity for this model, so '${r==="step"?"step":"stepAIC"}' cannot proceed`);let N=ha(d),b={step:[""],df:[NaN],deviance:[NaN],residDf:[ha(d)-pa(d,a,c)[0]],residDeviance:[fr(d)],aic:[v]},E=[{formula:dc(o,d.termLabels),termLabels:[...d.termLabels],aic:v}],w=(M,R,A)=>{if(ha(M)!==N)throw new RangeError("number of rows in use has changed: remove missing values?");let[T,q]=pa(M,a,c),C=M.dfResidual-d.dfResidual;b.step.push(R),b.df.push(r==="step"?C:Math.abs(C)),b.deviance.push(A?fr(M)-fr(d):fr(d)-fr(M)),b.residDf.push(ha(M)-T),b.residDeviance.push(fr(M)),b.aic.push(q),d=M,v=q,E.push({formula:dc(o,d.termLabels),termLabels:[...d.termLabels],aic:v})},y=0;for(;y<i;){let M=es(d),R=M.filter((F,k)=>{let P=(Gt(d),d.coefNames).map((B,Q)=>Q).filter(B=>d.assign[B]===k+1);return P.length>0&&P.every(B=>Number.isNaN(d.coefficients[B]))});if(R.length>0){let F=R[R.length-1],k=Zo(o,d.termLabels.filter(P=>P!==F.label));io(g,k),w(k,`- ${F.label}`,!0),y++;continue}let A=h?qu(M,f):[],T=p?Iu(M,m):[],q=[];if(A.length>0&&(q=Fu(d,A,a,c,g).map((F,k)=>k===0?F:{...F,label:`- ${F.label}`})),T.length>0){let F=Du(o,d,T,a,c,g).map((k,P)=>P===0?k:{...k,label:`+ ${k.label}`});q=q.length===0?F.filter((k,P)=>P===0||k.df>0):[...q,...F.slice(1).filter(k=>k.df>0)]}if(q.length===0)break;let C=q.map((F,k)=>k).sort((F,k)=>h1(q[F].aic,q[k].aic)||F-k),_=C.map(F=>q[F]),I=E[E.length-1];if(I.table={move:_.map(F=>F.label),df:_.map(F=>F.df),deviance:_.map(F=>F.deviance),aic:_.map(F=>F.aic)},o.glm||(I.table.sumOfSq=_.map(F=>F.sumOfSq)),C[0]===0)break;let O=_[0],L=O.label.startsWith("- "),D=O.label.slice(2),S=L?Zo(o,d.termLabels.filter(F=>F!==D)):Zo(o,[...d.termLabels,D]);io(g,S),w(S,O.label,L),y++}let x={fit:d,anova:b,criterion:c>0&&!o.glm?"Cp":"AIC",visits:E,warnings:g.list.filter(M=>!e.warnings.includes(M))};return J(r,x,e)}var h1=(e,t)=>Number.isNaN(e)?Number.isNaN(t)?0:1:Number.isNaN(t)?-1:e-t;function p1(e,t,n={}){return K(n,n1,"step"),K(_t(n,"scope"),r1,"step","scope"),$u(e,t,n,"step")}function d1(e,t,n={}){return K(n,o1,"stepAIC"),K(_t(n,"scope"),s1,"stepAIC","scope"),$u(e,t,n,"stepAIC")}var g1=["type","omega","sandwich"],w1=["cluster","type","cadjust","multi0","fix","sandwich"],da=2**-52;function xc(e,t){if(t===0)return e;let n=Ac.get(e)??{mant:e,exp:e.map(()=>0)};return Fr(n.mant,n.exp.map(r=>r+t))}var ga=e=>Math.floor(Math.log2(Math.abs(e)));function un(e){if(e===0||!Number.isFinite(e))return[e,0];let t=ga(e);return[$(e,-t),t]}var ts=(e,t)=>[e[0]*t[0],e[1]+t[1]];function mr(e,t=!1){let n=-1/0;for(let[o,s]of e)o!==0&&Number.isFinite(o)&&(n=Math.max(n,s+ga(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 ur=(e,t)=>Math.abs(t)<=2046?$(e,t):Ve(e,t),Ac=new WeakMap;function Fr(e,t){let n=e.map((r,o)=>r.map((s,a)=>ur(s,t[o]+t[a])));return Ac.set(n,{mant:e,exp:t}),n}function Tc(e,t,n){let r=Ac.get(e),o=r?.mant[t]?.[t];if(!(r===void 0||o===void 0||!Object.is(ur(o,2*r.exp[t]),n)))return ur(Math.sqrt(o),r.exp[t])}function hr(e){return typeof e=="object"&&e!==null&&"family"in e&&"model"in e}function wa(e){if(!(typeof e=="object"&&e!==null&&Array.isArray(e.coefficients)&&"qr"in e&&(hr(e)||"sigma"in e&&"x"in e&&"rows"in e)))throw new TypeError("'fit' must be the result of lm or glm")}function Wu(e,t){return e.pivot.slice(0,t).map((n,r)=>[n,r]).sort((n,r)=>n[0]-r[0])}function Bu(e,t){let{inv:n,e:r}=Fo(e.qr.slice(0,t).map(s=>s.slice(0,t))),o=Wu(e,t);return{inv:o.map(([,s])=>o.map(([,a])=>n[s][a])),e:o.map(([,s])=>r[s]),cols:o.map(([s])=>s)}}function ju(e,t,n){let{inv:r,e:o,cols:s}=Bu(e,t);return{A:r.map((a,c)=>a.map((i,l)=>$(i,-o[c]-o[l]-n))),cols:s}}function zu(e,t){let n=e.qr.slice(0,t).map(a=>a.slice(0,t)),r=n.map((a,c)=>{let i=0;for(let l=0;l<=c;l++)i=Math.max(i,Math.abs(n[l][c]));return i>0&&Number.isFinite(i)?ga(i):0}),{inv:o}=Fo(n.map(a=>a.map((c,i)=>$(c,-r[i])))),s=Wu(e,t);return{B:s.map(([,a])=>s.map(([,c])=>o[a][c])),e:s.map(([,a])=>r[a])}}function Uu(e,t){let n=e.qr.length;return rf(e,Array.from({length:n},(r,o)=>Array.from({length:t},(s,a)=>o===a?1:0)))}function b1(e,t){let n=e.qr.length;return t===0?new Array(n).fill(0):Uu(e,t).map(o=>{let s=o.reduce((a,c)=>a+c*c,0);return s>1-10*da?1:s})}function Ku(e,t,n,r){return n.map((o,s)=>{if(o>=1)return 0;let a=r(s);if(o<=.5||e===void 0||a<0)return 1-o;let c=e.qr.length,i=new Array(c).fill(0);i[a]=1;let l=Ya(e,i),f=0;for(let m=t;m<c;m++)f+=l[m]*l[m];return f})}function ba(e){if(hr(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 Sc(e){let t=ba(e);if(e.qr===void 0||e.rank===0)return{vcov:[],names:[]};let{inv:n,e:r,cols:o}=Bu(e.qr,e.rank),s=hr(e)?0:hc(e.qr),a,c;if(hr(e)){let l=Bn(e),f=Ja(e).fixedDispersion?1:ar(l.fit),m=f===0||!Number.isFinite(f)?0:2*Math.floor(ga(f)/2);a=$(f,-m),c=m+2*l.k}else{let[l,f]=un(e.sigma);a=l*l,c=2*f}let i=n.map(l=>l.map(f=>f*a));return{vcov:Fr(i,r.map(l=>(c-s)/2-l)),names:o.map(l=>t[l])}}function Hu(e,t=!1){let n=ba(e);if(hr(e)){let g=t?Bn(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:[]}:ju(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]),A=v.family==="binomial"||v.family==="poisson",T=A?1:R.reduce((L,D)=>L+D*D,0)/M.reduce((L,D)=>L+D,0),q=d.model.rows,C=ti(d),_=uf(d),I=()=>{let L=Bn(d),D=E.map(z=>L.fit.residuals[z]),S=M.map(Math.sqrt),{B:F,e:k}=d.qr===void 0||d.rank===0?{B:[],e:[]}:zu(d.qr,d.rank),P=x.map((z,ee)=>z.map((U,ue)=>$(S[ee]*U,-k[ue]))),{v:B,s:Q}=mr(S.map((z,ee)=>z===0?[0,0]:ts(un(z),un(D[ee])))),re=()=>{let z=[1,0];if(!A){let ee=mr(M.map((ue,Z)=>ue===0?[0,0]:ts(un(ue),un(D[Z])))),U=mr(M.map(un));z=[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)=>ur(U,-k[ue]))),factor:[z[0]*z[0],2*z[1]]}};return{T:P,B:F,e:k,u:B,s:Q+L.k,omegaPart:re}},O;if(Dr(T)&&R.every(Number.isFinite))O=R.map(L=>L/T);else{let L=Bn(d),D=mr(M.map((S,F)=>S===0?[0,0]:ts(un(S),un(L.fit.residuals[E[F]]))));if(A)O=D.v.map(S=>ur(S,D.s+L.k));else{let S=mr(M.map(un)),F=D.v.reduce((k,P)=>k+P*P,0)/S.v.reduce((k,P)=>k+P,0);O=D.v.map(k=>ur(k/F,S.s-D.s-L.k))}}return{glm:!0,names:y.map(L=>n[L]),X:x,W:M,res:O,phiS:T,A:w,h:C,omh:Ku(d.qr,d.rank,C,L=>_[L]),qr:d.qr,rank:d.rank,qrRow:L=>_[L],inputRows:E.map(L=>q[L]),nInput:Qa(e,q),nonZero:b,dispersion:()=>ar(d),scaled:I,k:g.k}}let r=e,o=r.weights,s=r.residuals.map((g,d)=>o===void 0||o[d]!==0),a=s.flatMap((g,d)=>g?[d]:[]),c=hc(r.qr),{A:i,cols:l}=r.rank===0?{A:[],cols:[]}:ju(r.qr,r.rank,c),f=a.map(g=>l.map(d=>r.x[g][d])),m=a.map(g=>o?.[g]??1),u=a.map((g,d)=>m[d]*r.residuals[g]),h=b1(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:[]}:zu(r.qr,r.rank),N=f.map((y,x)=>y.map((M,R)=>$(g[x]*M,-v[R]))),{v:b,s:E}=mr(g.map((y,x)=>ts(un(y),un(r.residuals[a[x]]))));return{T:N,B:d,e:v,u:b,s:E,omegaPart:()=>({T:f.map(y=>y.map((x,M)=>ur(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:i,h,omh:Ku(r.qr,r.rank,h,g=>g),qr:r.qr,rank:r.rank,qrRow:g=>g,inputRows:a.map(g=>r.rows[g]),nInput:Qa(r,r.rows),nonZero:s,dispersion:()=>r.sigma*r.sigma,scaled:p,k:0}}var kc=2**-969,Dr=e=>Math.abs(e)>=kc&&Math.abs(e)<1/0;function Yu(e,t,n,r){if(!(e.phiS>=kc&&e.phiS<1/0)||!Dr(e.X.length*e.phiS*e.phiS))return!1;for(let o=0;o<n.length;o++)if(!Dr(e.A[o][o])||!r&&(!Dr(t[o][o])||!Dr(n[o][o]))||!n[o].every(Number.isFinite))return!1;return!0}function ns(e,t,n){let r=e.length,s=e.map(a=>Array.from({length:r},(c,i)=>a.reduce((l,f,m)=>l+f*t[m][i],0))).map(a=>Array.from({length:r},(c,i)=>n*a.reduce((l,f,m)=>l+f*e[m][i],0)));for(let a=0;a<r;a++)for(let c=0;c<a;c++)s[a][c]=s[c][a];return s}function co(e,t,n){let r=e[0]?.length??0,o=Array.from({length:r},()=>new Array(r).fill(0));e.forEach((s,a)=>{let c=t[a];for(let i=0;i<r;i++)for(let l=i;l<r;l++)o[i][l]+=c*s[i]*s[l]});for(let s=0;s<r;s++)for(let a=s;a<r;a++)o[s][a]=o[a][s]=o[s][a]/n;return o}function Mc(e){let t=e.slice(0,10).map(n=>String(n+1));return e.length>10?`${t.join(", ")}, ...`:t.join(", ")}var v1=["HC3","const","HC","HC0","HC1","HC2","HC4","HC4m","HC5"];function qc(e,t={}){K(t,g1,"vcovHC"),wa(e);let n=ke(t.type,v1),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),a=s.X.length,c=s.names.length,i=[],{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(),a-c):(g=t.omega,g.length!==a&&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!==a)throw new RangeError(`'omega' must have one value per observation (${a})`);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(da)?[s.inputRows[b]]:[]);if(g.length>0&&i.push(r==="HC0"||r==="HC1"?`${r} covariances become (close to) singular if hat values are (close to) 1 as for observation(s) ${Mc(g)}`:`${r} covariances are numerically unstable for hat values close to 1 (and undefined if exactly 1) as for observation(s) ${Mc(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) ${Mc(N)}`)}if(r==="HC1"&&a<=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*a/(a-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,a*f[E]/d));case"HC4m":return N.map((b,E)=>b/m[E]**(Math.min(1,a*f[E]/d)+Math.min(1.5,a*f[E]/d)));default:return N.map((b,E)=>b/Math.sqrt(m[E]**Math.min(a*f[E]/d,Math.max(4,a*.7*v/d))))}},u=h(l.map(N=>N*N))}let p;if(o&&r==="const"&&t.omega===void 0)p=Sc(e).vcov;else if(o){let g=co(s.X,u,a);if(p=ns(s.A,g,a*s.phiS*s.phiS),!Yu(s,g,p,(h===void 0?u:l).every(d=>d===0))){let d=s.scaled();if(h!==void 0){let v=ns(d.B,co(d.T,h(d.u.map(N=>N*N)),a),a);p=Fr(v,d.e.map(N=>d.s-N))}else{let{T:v,factor:N}=d.omegaPart(),b=mr(u.map(E=>ts(un(E),N)),!0);p=Fr(ns(d.B,co(v,b.v,a),a),d.e.map(E=>b.s/2-E))}}p=xc(p,s.k)}else{let g=co(s.X,u,a);if(Qu(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}=Zu(d,co(d.T,h(d.u.map(b=>b*b)),a));p=Fr(v,N)}else{let v=mr(u.map(un),!0);p=Fr(co(d.omegaPart().T,v.v,a),d.e.map(N=>v.s/2+N))}}p=xc(p,-s.k)}return J("vcovHC",{vcov:p,names:s.names,warnings:i},e)}var N1=["HC","HC0","HC1","HC2","HC3"];function y1(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 Mt(e);if(typeof e=="string")return e;throw new TypeError("cluster labels must be numbers, strings or booleans")}function M1(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 E1(e){let t=[],n=(r,o,s)=>{if(s.length===r){t.push(s.slice());return}for(let a=o;a<e;a++)s.push(a),n(r,a+1,s),s.pop()};for(let r=1;r<=e;r++)n(r,0,[]);return t}var Gu=da**(1/1.3);function Xu(e,t){let{values:n,vectors:r}=Ut(e,{symmetric:!0});if(n.some(a=>!(a>=Gu)))return;let o=r,s=n.map(a=>a**t);return e.map((a,c)=>e.map((i,l)=>o[c].reduce((f,m,u)=>f+m*s[u]*o[l][u],0)))}function x1(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 R1(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 A1(e,t,n,r,o){let s=e.names.length,a=Array.from({length:r},()=>[]);n.forEach((f,m)=>a[f].push(m));let c=Math.sqrt((r-1)/r),i=r===n.length,l;return a.map((f,m)=>{if(f.length===1){let N=f[0];if(i?e.h[N]>=1:!(e.omh[N]>=Gu))throw Ec(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=T1(e,l,f,h,o);if(N===null)throw Ec(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=Xu(g,o);if(d===void 0)throw Ec(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 T1(e,t,n,r,o){let s=e.qr,a=e.rank,c=new Array(a).fill(0),i=n.map(w=>{let y=e.qrRow(w);return y<0?c:t[y]}),l=Array.from({length:a},(w,y)=>Array.from({length:a},(x,M)=>i.reduce((R,A)=>R+A[y]*A[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=i.map(M=>M.reduce((R,A,T)=>R+A*u[T][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((A,T,q)=>A+T*r[q],0);x.forEach((A,T)=>h[T]+=R*A)}});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])}),Ya(s,y).slice(a)}),v=d.map(w=>d.map(y=>w.reduce((x,M,R)=>x+M*y[R],0))),N=Xu(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 Ec(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 S1(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 Rc(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,a)=>e.map((c,i)=>r[a].reduce((l,f,m)=>l+f*o[m]*r[i][m],0)))}function k1(e,t){let n=e.length;if(n===0)return{mant:e,exp:t};let r=e.map((i,l)=>i[l]!==0?Math.sqrt(Math.abs(i[l])):1),o=e.map((i,l)=>i.map((f,m)=>f/r[l]/r[m])),{values:s}=Ut(o,{symmetric:!0}),a=s.reduce((i,l)=>Math.max(i,Math.abs(l)),0);if(s.every(i=>i>=-16*n*da*a))return{mant:e,exp:t};let c=Math.max(...t);return{mant:Rc(e.map((i,l)=>i.map((f,m)=>ur(f,t[l]+t[m]-2*c)))),exp:t.map(()=>c)}}function Qu(e,t){return t||e.every((n,r)=>Dr(n[r])&&n.every(Number.isFinite))}function Zu(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 q1(e,t={}){K(t,w1,"vcovCL"),wa(e);let n=Hu(e,!0),r=n.X.length,o=n.names.length,s=t.type===void 0?n.glm?"HC0":"HC1":ke(t.type,N1),a=s==="HC"?"HC0":s,c=t.cadjust??!0,i=t.multi0??!1,l=t.fix??!1,f=t.sandwich??!0;for(let[b,E]of[["cadjust",c],["multi0",i],["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=>y1(y[x]))})}if(a==="HC1"&&r<=o)throw new RangeError("HC1 covariances need more observations than coefficients");let h=m.length,p=E1(h).map((b,E)=>{let w=i&&h>1&&E===2**h-2,y=w?n.inputRows.map(String):n.inputRows.map((A,T)=>b.length===1?m[b[0]][T]:JSON.stringify(b.map(q=>m[q][T]))),{g:x,G:M}=M1(y);if(M<2)throw new RangeError("at least two clusters are needed");let R=w?a==="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;a==="HC2"||a==="HC3"?R=A1(n,b,w,y,a==="HC2"?-.5:-1):y===r?R=n.X.map((T,q)=>b.psi(q)):(R=Array.from({length:y},()=>new Array(o).fill(0)),n.X.forEach((T,q)=>{let C=R[w[q]],_=b.psi(q);for(let I=0;I<o;I++)C[I]+=_[I]}));let A=S1(R,o);E=E.map((T,q)=>T.map((C,_)=>C+M*x*A[q][_]/r))}return a==="HC1"&&(E=E.map(w=>w.map(y=>y*(r-1)/(r-o)))),E},d=n.res.every(b=>b===0),v=n.phiS>=kc&&n.phiS<1/0&&n.A.every((b,E)=>Dr(b[E]))&&n.res.every(Number.isFinite),N;if(v){let b=g(x1(n));if(!f)Qu(b,d)&&(N=l?Rc(b):b);else{let E=ns(n.A,b,r*n.phiS*n.phiS);Yu(n,b,E,d)&&(N=l?Rc(E):E)}}if(N===void 0){let b=n.scaled(),E=g(R1(b)),{mant:w,exp:y}=f?{mant:ns(b.B,E,r),exp:b.e.map(M=>b.s-M)}:Zu(b,E),x=l?k1(w,y):{mant:w,exp:y};N=Fr(x.mant,x.exp)}return N=xc(N,f?n.k:-n.k),J("vcovCL",{vcov:N,names:n.names,warnings:[]},e)}var I1=["weights","family","link","epsilon","maxit"],_1=["family","link","variance"],Ju=["power"];function L1(e,t,n){K(n,I1,"svyglm"),K(_t(n,"family"),_1,"svyglm","family"),K(_t(n,"family.link"),Ju,"svyglm","family.link"),K(_t(n,"link"),Ju,"svyglm","link");let{weights:r,family:o,link:s,epsilon:a,maxit:c}=n;if(r===void 0)throw new RangeError("'weights' must be given");let i=Array.from(r);if(i.length!==t.length)throw new RangeError("'weights' must have the length of 'y'");if(i.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=i.length,f=i.reduce((S,F)=>S+F,0)/l,m=i.map(S=>S/f),u=Ye(()=>sr(e,t,{weights:m,...o===void 0?{}:{family:o},...s===void 0?{}:{link:s},...a===void 0?{}:{epsilon:a},...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 F=0;for(let k=0;k<S.length;k++)Number.isFinite(S[k])&&Math.abs(S[k])>F&&(F=Math.abs(S[k]));return F>0?Math.floor(Math.log2(F)):0},g=(h[0]??[]).map((S,F)=>p(h.map(k=>k[F]))),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:Ye(()=>sr(h.map(S=>S.map((F,k)=>$(F,-g[k]))),Array.from(t,S=>$(S,-N)),{weights:m,...o===void 0?{}:{family:o},...s===void 0?{}:{link:s},...a===void 0?{}:{epsilon:a},...c===void 0?{}:{maxit:c}})),E=Ye(()=>qc(b,{type:"HC0"})).vcov,w=[...u.coefficients.length>g.length?[N]:[],...g.map(S=>N-S)],y=E.map((S,F)=>S.map((k,P)=>$(k*l/(l-1),w[F]+w[P]))),x=u.coefficients.length,M=l-x,R=E.map((S,F)=>$(Math.sqrt(S[F]*l/(l-1)),w[F])),A=b.coefficients.map((S,F)=>S/Math.sqrt(E[F][F]*l/(l-1))),T=A.map(S=>2*Wt(-Math.abs(S),M)),q=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=q.map((S,F)=>(C[F]-S)/Math.sqrt(_(S))),O=I.reduce((S,F,k)=>S+m[k]*F,0)/l,L=0;for(let S=0;S<l;S++)L+=m[S]*(I[S]-O)**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:A,pValue:T,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 pr(e){let t=Math.sqrt(2220446049250313e-31),n=Array.from(new Set(e.filter(s=>Number.isFinite(s)))).sort((s,a)=>s-a);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 a=n[s+1]-n[s];a<=t||a/r<=t||o.push(n[s+1])}return e.map(s=>{if(!Number.isFinite(s))return s;let a=0,c=o.length-1;for(;a<c;){let i=Math.floor((a+c+1)/2);o[i]<=s?a=i:c=i-1}return o[a]})}var C1=new Set(["start","id","ties","weights","strata","init","control","nocenter","confLevel","robust","cluster"]),O1=["eps","iterMax","tolerChol","tolerInf","timefix"],Ic=e=>e==null||typeof e=="number"&&Number.isNaN(e),F1=(e,t)=>typeof e=="number"&&typeof t=="number"?e-t:String(e)<String(t)?-1:String(e)>String(t)?1:0,D1=2**64,P1=2**-1022,eh=700;function va(e,t,n){let{time:r,event:o,weight:s,x:a,stratumEnd:c,logUnit:i,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 A=Math.exp(x-R);v*=A,y*=A;for(let T=0;T<m;T++){N[T]*=A,E[T]*=A;let q=b[T],C=w[T];for(let _=0;_<=T;_++)q[_]*=A,C[_]*=A}x=R};for(let R=f-1;R>=0;){c[R]&&(x=0,v=0,N=u(),b=h());let A=r[R],T=!1;if(l!==void 0){let I=R;for(;I>0&&r[I-1]===A&&!c[I-1];)I--;for(let O=I;O<=R;O++)o[O]&&(T=!0)}if(l!==void 0&&T){let I=R;for(;I>0&&!c[I-1];)I--;let O=R;for(;!c[O];)O++;let L=S=>{let F=0;for(let k=0;k<m;k++)F+=t[k]*a[S][k];return F},D=-1/0;for(let S=I;S<=O;S++)l[S]<A&&r[S]>=A&&(D=Math.max(D,L(S)));x=Number.isFinite(D)?D:0,v=0,N=u(),b=h();for(let S=I;S<=O;S++){if(!(l[S]<A&&r[S]>=A)||r[S]===A&&o[S])continue;let F=Math.exp(L(S)-x)*s[S];v+=F;for(let k=0;k<m;k++){N[k]+=F*a[S][k];for(let P=0;P<=k;P++)b[k][P]+=F*a[S][k]*a[S][P]}}}let q=0,C=0;y=0;let _=R;do{let I=a[_],O=0;for(let S=0;S<m;S++)O+=t[S]*I[S];let L=s[_];Number.isFinite(O)&&(O-x>eh?M(O):O-x<-eh&&v===0&&y===0&&(x=O));let D=Math.exp(O-x)*L;if(o[_]){q++,C+=L,y+=D,d+=L*O;for(let S=0;S<m;S++){p[S]+=L*I[S],E[S]+=D*I[S];let F=w[S];for(let k=0;k<=S;k++)F[k]+=D*I[S]*I[k]}}else if(l===void 0){v+=D;for(let S=0;S<m;S++){N[S]+=D*I[S];let F=b[S];for(let k=0;k<=S;k++)F[k]+=D*I[S]*I[k]}}_--}while(_>=0&&r[_]===A&&!c[_]);if(q>0){let I=n?q:1,O=n?q:1,L=C/I;for(let D=0;D<I;D++){v+=n?y/O:y,d-=L*(Math.log(v)+x+i);for(let S=0;S<m;S++){N[S]+=n?E[S]/O:E[S];let F=N[S]/v;p[S]-=L*F;let k=b[S],P=w[S],B=g[S];for(let Q=0;Q<=S;Q++){k[Q]+=n?P[Q]/O:P[Q];let re=k[Q]-F*N[Q],z=L*re;B[Q]+=z<1/0&&z>-1/0&&(Math.abs(z)>=P1||re===0)||!Number.isFinite(L)?z/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 A=0;A<R;A++)g[A][R]=g[R][A];return{loglik:d,score:p,info:g}}function th(e,t,n){let{time:r,event:o,x:s,stratumEnd:a}=e,c=Ie([e.weight]).groups[0],i=r.length,l=t.length,f=Array.from({length:i},()=>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<i;){let h=u;for(;!a[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 A=Number.isFinite(E)?E:0;for(let q=g[R];q<d(R);q++)E=Math.max(E,p[q-u]);let T=Number.isFinite(E)?E:0;if(R<g.length-1&&T!==A){let q=Math.exp(A-T);w*=q;for(let C=0;C<l;C++)y[C]*=q}for(let q=g[R];q<d(R);q++){let C=c[q]*Math.exp(p[q-u]-T);w+=C;for(let _=0;_<l;_++)y[_]+=C*s[q][_]}v[R]=w,N[R]=y.slice(),b[R]=T}let x=0,M=new Array(l).fill(0);for(let R=0;R<g.length;R++){let A=b[R];if(R>0&&A!==b[R-1]){let S=Math.exp(A-b[R-1]);x*=S;for(let F=0;F<l;F++)M[F]*=S}let T=g[R],q=d(R),C=[];for(let S=T;S<q;S++)C.push(Math.exp(p[S-u]-A));let _=0,I=0,O=0,L=new Array(l).fill(0);for(let S=T;S<q;S++){if(!o[S])continue;let F=c[S]*C[S-T];_++,I+=c[S],O+=F;for(let k=0;k<l;k++)L[k]+=F*s[S][k]}for(let S=T;S<q;S++){let F=C[S-T],k=s[S],P=f[S];for(let B=0;B<l;B++)P[B]=-F*(k[B]*x-M[B])}if(_===0)continue;let D=n?_:1;for(let S=0;S<D;S++){let F=S/D,k=v[R]-F*O,P=N[R].map((Q,re)=>(Q-F*L[re])/k),B=I/D/k;for(let Q=T;Q<q;Q++){let re=C[Q-T],z=s[Q],ee=f[Q];if(o[Q])for(let U=0;U<l;U++){let ue=z[U]-P[U];ee[U]+=ue/D-(1-F)*re*ue*B}else for(let U=0;U<l;U++)ee[U]-=re*(z[U]-P[U])*B}x+=B;for(let Q=0;Q<l;Q++)M[Q]+=B*P[Q]}}u=h+1}return f}function _c(e,t,n){let r=e.start,{time:o,event:s,x:a,stratumEnd:c}=e,i=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]*a[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 T of N){let q=Math.exp(u[T]-E),C=i[T]*q;w+=C;for(let _=0;_<f;_++)y[_]+=C*a[T][_];if(s[T]&&o[T]===d){R+=i[T],x+=C;for(let _=0;_<f;_++)M[_]+=C*a[T][_]}}let A=n?b.length:1;for(let T=0;T<A;T++){let q=T/A,C=w-q*x,_=R/A/C,I=y.map((O,L)=>(O-q*M[L])/C);for(let O of N){let L=Math.exp(u[O]-E),D=s[O]&&o[O]===d;for(let S=0;S<f;S++){let F=a[O][S]-I[S];D?m[O][S]+=F/A-(1-q)*L*F*_:m[O][S]-=L*F*_}}}}g=v+1}h=p+1}return m}function Pr(e,t){let n=e.length,r=0,o=!1;for(let i=0;i<n;i++){let l=e[i][i];l>0&&((!o||l>r)&&(r=l),o=!0)}let s=o?r*t:t,a=0,c=!0;for(let i=0;i<n;i++){let l=e[i][i];if(!Number.isFinite(l)||l<s){e[i][i]=0,l<-8*s&&(c=!1);for(let f=i+1;f<n;f++)e[f][i]=0;continue}a++;for(let f=i+1;f<n;f++){let m=e[f][i]/l;e[f][i]=m,e[f][f]-=m*m*l;for(let u=f+1;u<n;u++)e[u][f]-=m*e[u][i]}}return c?a:-a}function Vr(e,t){let n=e.length,r=t.slice();for(let o=0;o<n;o++){let s=r[o];for(let a=0;a<o;a++)s-=r[a]*e[o][a];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 a=o+1;a<n;a++)s-=r[a]*e[a][o];r[o]=s}return r}function lo(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 rs=(e,t)=>({statistic:e,df:t,pValue:Nt(e,t,{lowerTail:!1})});function V1(e,t,n){let r=e.length,o=()=>new Array(t).fill(NaN),s=rs(0,0),a=new Array(t).fill(0),c=o(),i=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:a,z:c,p:i,stdError:a,zValue:c,pValue:i,lower:o(),upper:o(),logtest:s,sctest:{...s},waldtest:{...s},rsq:{rsq:0,maxrsq:0},usedRobust:!1,warnings:n}}function fo(e,t,n,r={}){return K(r,C1,"coxph"),K(_t(r,"control"),O1,"coxph","control"),J("coxph",$1(e,t,n,r).result)}function $1(e,t,n,r={},o=!1){let{ties:s="efron",weights:a,strata:c,init:i,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(a!==void 0&&a.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(i!==void 0&&i.length!==y)throw new RangeError("'init' must have one value per column of 'x'");if(i?.some(G=>!Number.isFinite(G)))throw new RangeError("'init' must contain finite values");let x=new mt,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])||a!==void 0&&Number.isNaN(a[G])||c!==void 0&&Ic(c[G])||h!==void 0&&Ic(h[G])||g!==void 0&&Ic(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 A=M.map(G=>a===void 0?1:a[G]);if(A.some(G=>!(G>0)))throw new RangeError("Invalid weights, must be >0");if(A.some(G=>G===1/0))throw new RangeError("'weights' must be finite");let T=g===void 0?void 0:Vt(g),q=new Map;if(T!==void 0)for(let G of M){if(t[G]!==1)continue;let le=T[G];q.set(le,(q.get(le)??0)+1)}let C=p!==void 0&&[...q.values()].some(G=>G>1),_=u??(h!==void 0||A.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 O=M.map(G=>e[G]),L=p===void 0?void 0:M.map(G=>p[G]),D,S;if(L===void 0)D=E?pr(O):O;else if(!E)D=O,S=L;else{let G=pr([...L,...O]);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=V1(M.map(le=>n[le]),y,x.list);return T!==void 0&&(G.nId=new Set(M.map(le=>T[le])).size),{result:G}}let F=new Array(R).fill(0),k,P=[];if(c!==void 0){let G=Vt(c);P=Array.from(new Set(M.map(Le=>G[Le]))).sort(F1);let le=new Map;P.forEach((Le,Xe)=>le.set(Le,Xe)),k=M.map(Le=>G[Le]),F=M.map(Le=>le.get(G[Le]))}let B=Array.from({length:R},(G,le)=>le).sort((G,le)=>F[G]-F[le]||D[G]-D[le]||G-le),Q=B.map(G=>A[G]),{groups:re,k:z}=Et(Q)>=D1?Ie([Q]):{groups:[Q],k:0},ee=re[0],U=0;for(let G of A)U+=z===0?G:$(G,-z);let ue=B.map(G=>n[M[G]].slice()),Z=new Array(y).fill(0),V=new Array(y).fill(1),Y=new Array(y).fill(0),j=new Set(f);for(let G=0;G<y;G++){if(ue.every(He=>j.has(He[G])))continue;let le=ue.map(He=>He[G]),Le=He=>{let zt=0;for(let Dn=0;Dn<R;Dn++)zt+=(p===void 0?ee[Dn]:1)*He[Dn];let Fn=p===void 0?U:R,Mr=zt/Fn,Yr=0,$a=He.map((Dn,gs)=>(Yr+=(p===void 0?ee[gs]:1)*Math.abs(Dn-Mr),Dn-Mr)),Gr=Yr===0?1:Fn/Yr;return{m:Mr,s:Gr,d:$a,ok:Number.isFinite(Mr)&&Gr>=2**-1022&&Gr<1/0}},Xe=Le(le);if(!Xe.ok){let{groups:He,k:zt}=Ie([le]);Xe=Le(He[0]),Y[G]=zt}for(let He=0;He<R;He++)ue[He][G]=Xe.d[He]*Xe.s;Z[G]=$(Xe.m,Y[G]),V[G]=Xe.s}let H={time:B.map(G=>D[G]),...S===void 0?{}:{start:B.map(G=>S[G])},event:B.map(G=>t[M[G]]===1),weight:ee,x:ue,stratumEnd:B.map((G,le)=>le===R-1||F[B[le+1]]!==F[G]),logUnit:z*Math.LN2},W=s==="efron",te=i===void 0?new Array(y).fill(0):i.map((G,le)=>$(G/V[le],Y[le])),se=va(H,te,W),Me=[se.loglik,se.loglik],Ne=se.info.map(G=>G.slice()),de=Pr(Ne,N),ce=Vr(Ne,se.score),qe=0;for(let G=0;G<y;G++)qe+=se.score[G]*ce[G];let we=0,Fe,xe=!1,_e=v===0;if(v===0||!Number.isFinite(se.loglik))Fe=lo(Ne);else{let G=se.loglik,le=te.map((Xe,He)=>Xe+ce[He]),Le=0;for(_e=!1,we=1;we<=v;we++){se=va(H,le,W),Ne=se.info.map(He=>He.slice()),de=Pr(Ne,N);let Xe=Number.isFinite(se.loglik)&&se.score.every(Number.isFinite)&&se.info.every(He=>He.every(Number.isFinite));if(Xe&&(se.loglik===G||Math.abs(1-G/se.loglik)<=d)){_e=!0,Me[1]=se.loglik,te=le;break}!Xe||se.loglik<G?(Le++,le=le.map((He,zt)=>(He+Le*te[zt])/(Le+1))):(Le=0,G=se.loglik,Me[1]=G,ce=Vr(Ne,se.score),te=le,le=le.map((He,zt)=>He+ce[zt]))}_e?Fe=lo(Ne):(we=p===void 0?v+1:v,xe=!0,p!==void 0?(se=va(H,te,W),Me[1]=se.loglik,Ne=se.info.map(Xe=>Xe.slice()),de=Pr(Ne,N),Fe=lo(Ne)):v>1?(se=va(H,te,W),Me[1]=se.loglik,Fe=se.info.map((Xe,He)=>Xe.map((zt,Fn)=>He===Fn?Xe[He]>0?1/Xe[He]:0:-0))):Fe=lo(Ne))}let Oe=V.map(G=>Math.floor(Math.log2(G))),Be=V.map((G,le)=>$(G,-Oe[le])),je=Oe.map((G,le)=>G-Y[le]),tt=te.map((G,le)=>G*Be[le]),Ue=se.score.map((G,le)=>G/Be[le]),Qe=Fe.map((G,le)=>G.map((Le,Xe)=>Le*Be[le]*Be[Xe])),ge=tt.map((G,le)=>$(G,je[le])),oe=Qe.map((G,le)=>G.map((Le,Xe)=>$(Le,je[le]+je[Xe]-z))),ve=ge.slice();if(v>0&&Math.abs(de)<y)for(let G=0;G<y;G++)Fe[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=Qe.map((le,Le)=>{let Xe=0;for(let He=0;He<y;He++)Xe+=Ue[He]*Qe[He][Le];return Math.abs(Xe)});if(xe)(p===void 0||v>1)&&x.add("Ran out of iterations and did not converge"),p===void 0&&(Et(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 Xe=G[Le];(!Number.isFinite(Ue[Le])||$(Xe,je[Le])>d&&Xe>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,Ae,Ee=0,Te=0,ne;if(_&&he>0){let G=B.map((lt,Ze)=>Ze),le=R,Le=h??(p===void 0?void 0:g);if(Le!==void 0){let lt=Vt(Le),Ze=new Map;G=B.map(gt=>{let xt=lt[M[gt]],Rt=Ze.get(xt);return Rt===void 0&&(Rt=Ze.size,Ze.set(xt,Rt)),Rt}),le=Ze.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 Xe=lt=>lt.map(Ze=>Ze.map((gt,xt)=>gt/Be[xt])),He=Xe(p===void 0?th(H,te,W):_c(H,te,W)),zt=He.map(lt=>Qe.map((Ze,gt)=>{let xt=0;for(let Rt=0;Rt<y;Rt++)xt+=lt[Rt]*Qe[Rt][gt];return xt})),Fn;if(z===0)Fn=zt.map((lt,Ze)=>lt.map(gt=>ee[Ze]*gt));else{let lt=gt=>gt===0||!Number.isFinite(gt)?0:Math.floor(Math.log2(Math.abs(gt))),Ze=-1/0;zt.forEach((gt,xt)=>gt.forEach(Rt=>Rt!==0&&Number.isFinite(Rt)&&(Ze=Math.max(Ze,lt(ee[xt])+lt(Rt))))),Ee=Ze===-1/0?0:-Ze,Fn=zt.map((gt,xt)=>{let Rt=lt(ee[xt]);return gt.map(Er=>{let El=lt(Er);return $($(ee[xt],-Rt)*$(Er,-El),Rt+El+Ee)})})}let Mr=lt=>{let Ze=Array.from({length:le},()=>new Array(y).fill(0));return lt.forEach((gt,xt)=>{let Rt=Ze[G[xt]];for(let Er=0;Er<y;Er++)Rt[Er]+=gt[Er]}),Ze},Yr=lt=>{let Ze=Array.from({length:y},()=>new Array(y).fill(0));for(let gt of lt)for(let xt=0;xt<y;xt++)for(let Rt=0;Rt<y;Rt++)Ze[xt][Rt]+=gt[xt]*gt[Rt];return Ze};Ae=Yr(Mr(Fn));let $a=Ie([Q]).groups[0],Gr=i===void 0?new Array(y).fill(0):i.map((lt,Ze)=>$(lt/V[Ze],Y[Ze])),Dn=p===void 0?th(H,Gr,W):_c(H,Gr,W),gs=Mr(Xe(Dn).map((lt,Ze)=>lt.map(gt=>$a[Ze]*gt))),qo=new Array(y).fill(0);for(let lt of gs)for(let Ze=0;Ze<y;Ze++)qo[Ze]+=lt[Ze];let ja=Yr(gs);if(y===1)Te=qo[0]*qo[0]/ja[0][0];else{Pr(ja,N);let lt=Vr(ja,qo);for(let Ze=0;Ze<y;Ze++)Te+=qo[Ze]*lt[Ze]}let yl=new Array(R),Ml=new Array(R);B.forEach((lt,Ze)=>{yl[lt]=He[Ze].map((gt,xt)=>$(gt,-je[xt])),Ml[lt]=Fn[Ze].map((gt,xt)=>$(gt,je[xt]-Ee))}),ne={scoreResiduals:yl,dfbeta:Ml}}let ye=Ae!==void 0,me=Ae??Qe,Se=Ae===void 0?-z:-2*Ee,Ke=X.map(G=>tt[G]-(i===void 0?0:$(i[G],-je[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(Xe=>me[Le][Xe]));Pr(G,N);let le=Vr(G,Ke);for(let Le=0;Le<he;Le++)$e+=Ke[Le]*le[Le]}$e=$($e,-Se);let st=(1-m)/2,ut=st<1e-8?ie(st,{lowerTail:!1}):ie((1+m)/2),Ot=(G,le)=>{let Le=Math.floor(le/2),Xe=G.map((He,zt)=>Math.sqrt($(He[zt],le-2*Le)));return{unit:Xe,h:Le,se:Xe.map((He,zt)=>$(He,je[zt]+Le))}},Xt=Ot(Qe,-z).se,{unit:Qn,h:We,se:ze}=Ot(me,Se),St=ve.map((G,le)=>$((Number.isNaN(G)?G:tt[le])/Qn[le],-We)),dt=St.map(G=>Nt(G*G,1,{lowerTail:!1})),Tn=ve.map((G,le)=>Math.exp(G-ut*ze[le])),jt=ve.map((G,le)=>Math.exp(G+ut*ze[le])),It=$(-2*(Me[0]-Me[1]),z),ht=[$(Me[0],z),$(Me[1],z)],Qt=$(qe,z),an=Ae?.map((G,le)=>G.map((Le,Xe)=>$(Le,je[le]+je[Xe]-2*Ee))),tn={coefficients:ve,var:an??oe,loglik:ht,score:Qt,wald:$e,iter:we,means:Z,n:R,nevent:I,...T===void 0?{}:{nId:new Set(M.map(G=>T[G])).size},linearPredictors:nt,se:Xt,z:St,p:dt,stdError:Xt,zValue:St,pValue:dt,lower:Tn,upper:jt,logtest:rs(It,he),sctest:rs(Qt,he),waldtest:rs($e,he),rsq:{rsq:1-Math.exp(-It/R),maxrsq:1-Math.exp(2*ht[0]/R)},usedRobust:ye,...ye?{naiveVar:oe,robustSe:ze,rscore:Te,robscore:rs(Te,he)}:{},warnings:x.list},Ft=se.score.map((G,le)=>$(G/V[le],Y[le]+z));Object.defineProperty(tn,"coxZphData",{enumerable:!1,value:{time:D.slice(),...S===void 0?{}:{start:S.slice()},status:M.map(G=>t[G]===1),weights:A.slice(),...k===void 0?{}:{strata:k.slice()},x:M.map(G=>n[G].slice()),sortedToInput:B.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:B.map(G=>F[G]),stratumEnd:H.stratumEnd.slice(),betaScaled:te.slice(),inverseCovariateScale:Be.map((G,le)=>$(1/G,-je[le])),weightExp:z,ties:s}});let nn=ne?.scoreResiduals;if(p!==void 0&&o&&nn===void 0){let G=_c(H,te,W);nn=new Array(R),B.forEach((le,Le)=>{nn[le]=G[Le].map((Xe,He)=>$(Xe/Be[He],-je[He]))})}return{result:tn,...ne,...nn===void 0?{}:{scoreResiduals:nn},first:Ft,converged:_e}}var j1=["rho"],z1=new Set(["group","start","id","weights","ctype","stype","robust","rmean","confType","confInt"]),Lc=e=>e==null||typeof e=="number"&&Number.isNaN(e);function K1(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 i=0;i<e.length;i++){if(Number.isNaN(e[i])||Number.isNaN(t[i])||n!==void 0&&Lc(n[i]))continue;let l=t[i];if(l!==0&&l!==1)throw new RangeError("'status' must be 0 (censored) or 1 (event)");if(e[i]===-1/0||e[i]===1/0&&l===1)throw new RangeError("event times must be finite");r.push(i)}if(r.length===0)throw new RangeError("no observations without missing values");let o=[],s=new Map;n!==void 0&&(n=Vt(n)),n!==void 0&&(o=Array.from(new Set(r.map(i=>n[i]))).sort((i,l)=>typeof i=="number"&&typeof l=="number"?i-l:String(i)<String(l)?-1:String(i)>String(l)?1:0),o.forEach((i,l)=>s.set(i,l)));let a=pr(r.map(i=>e[i])),c=r.map((i,l)=>({time:a[l],event:t[i]===1,group:n===void 0?0:s.get(n[i])}));return c.sort((i,l)=>i.time-l.time),{obs:c,labels:o.map(i=>`group=${Mt(i)}`)}}function W1(e,t,n,r={}){K(r,j1,"survdiff");let{rho:o=0}=r;if(!Number.isFinite(o))throw new RangeError("'rho' must be a finite number");let{obs:s,labels:a}=K1(e,t,n),c=a.length;if(c<2)throw new RangeError("there is only 1 group");let i=new Array(c).fill(0);for(let b of s)i[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=i.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 A=0;A<c;A++)l[A]+=R*y[A],f[A]+=R*M*u[A]/h;if(h>1){let A=R*R*M*(h-M)/(h*(h-1));for(let T=0;T<c;T++)for(let q=0;q<c;q++)m[T][q]+=A*u[T]*((T===q?1:0)-u[q]/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}=B1(w,E))}}let N=v===0?NaN:Nt(d,v,{lowerTail:!1});return{n:i,groups:a,obs:l,exp:f,var:m,chisq:d,df:v,pvalue:N,pValue:N,warnings:[]}}function B1(e,t){let n=Ut(e,{symmetric:!0}),r=n.values,o=r.reduce((c,i)=>Math.max(c,i),0),s=0,a=0;return r.forEach((c,i)=>{if(!(o>0&&c>1e-10*o))return;let l=t.reduce((f,m,u)=>f+m*n.vectors[u][i],0);s+=l*l/c,a++}),{chisq:s,df:a}}function $r(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 bn(e[r],e[s])}return e[r]}function dr(e,t,n={}){K(n,z1,"survfit");let{group:r,start:o,id:s,weights:a,ctype:c=1,stype:i=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(k=>k.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 i!="number")throw new TypeError("stype must be a number");if(c!==1&&c!==2)throw new RangeError("ctype must be 1 or 2");if(i!==1&&i!==2)throw new RangeError("stype must be 1 or 2");if(!u.some(k=>k===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(a!==void 0&&!Array.isArray(a))throw new TypeError("'weights' must be an array");if(a!==void 0&&a.length!==e.length)throw new RangeError("'weights' must have the same length as 'time'");let g=n.robust??(a!==void 0&&Array.from(a).some(k=>k!=null&&Number.isFinite(k)&&!Number.isInteger(k)));if(typeof g!="boolean")throw new TypeError("'robust' must be a boolean");if(g&&i===2&&a!==void 0&&Array.from(a).some(k=>k!=null&&Number.isFinite(k)&&!Number.isInteger(k)))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 k=0;k<e.length;k++){let P=a?.[k];if(a&&P!=null&&typeof P!="number")throw new TypeError("weights must contain numbers");if(a&&(P==null||Number.isNaN(P)))continue;let B=P??1;if(!Number.isFinite(B))throw new RangeError("weights must be finite");if(B<0)throw new RangeError("weights must be non-negative");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&&Lc(r[k])||s!==void 0&&Lc(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?[]:Vt(r),N=s===void 0?[]:Array.from(s),b=r===void 0?["all"]:Array.from(new Set(d.map(k=>v[k]))).sort((k,P)=>typeof k=="number"&&typeof P=="number"?k-P:String(k)<String(P)?-1:String(k)>String(P)?1:0).map(k=>`group=${Mt(k)}`),E=new Map;r!==void 0&&Array.from(new Set(d.map(k=>v[k]))).sort((k,P)=>typeof k=="number"&&typeof P=="number"?k-P:String(k)<String(P)?-1:String(k)>String(P)?1:0).forEach((k,P)=>E.set(k,P));let w=new Map;s!==void 0&&Array.from(new Set(d.map(k=>N[k]))).forEach((k,P)=>w.set(k,P));let y=d.flatMap(k=>o===void 0?[e[k]]:[o[k],e[k]]),x=pr(y),M=new Map,R=new Map;d.forEach((k,P)=>{if(o===void 0)R.set(k,x[P]);else if(M.set(k,x[2*P]),R.set(k,x[2*P+1]),!(M.get(k)<R.get(k)))throw new Error("aeqSurv exception, an interval has effective length 0")});let A=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:w.get(N[k]),weight:a?.[k]??1})).sort((k,P)=>k.time-P.time),T=s===void 0?void 0:new Map;if(T)for(let k of A){let P=T.get(k.id)??[];P.push(k),T.set(k.id,P)}let q=b,C=A.map(k=>k.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 O=(1-m)/2,L=O<1e-8?ie(O,{lowerTail:!1}):ie(1-O),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:q,table:{colNames:[],rowNames:q,table:[]},warnings:[]};a!==void 0&&(S.counts={nRisk:[],nEvent:[],nCensor:[]});let F=[];for(let k=0;k<q.length;k++){let P=A.filter(ge=>ge.group===k);if(!P.some(ge=>ge.weight>0))throw new Error("a group has no positive weight");let B=S.time.length,Q=P.length,re=Math.max(...P.map(ge=>ge.time)),z=0,ee=0,U=1,ue=0,Z=0,V=0;if(o===void 0){let ge=0;for(let oe of P)ge+=oe.weight;z=ge,ee=P.length}let Y=[],j=[],H=[],W=s===void 0?void 0:new Map;if(W)for(let ge of P){let oe=W.get(ge.id)??[];oe.push(ge),W.set(ge.id,oe)}for(let ge=0;ge<P.length;){let oe=P[ge].time,ve=ge,be=0,fe=0,nt=0,X=ee;for(;ve<P.length&&P[ve].time===oe;)ve++;let he=P.slice(ge,ve);if(o!==void 0){let We=P.filter(ze=>ze.start<oe&&ze.time>=oe);z=We.reduce((ze,St)=>ze+St.weight,0),ee=We.length,X=ee}let Ae=he.filter(We=>We.event),Ee=he.filter(We=>!We.event&&(T===void 0||!(T.get(We.id)??[]).some(ze=>ze.start===oe&&ze.time>oe)));for(let We of Ae)be+=We.weight,fe++;for(let We of Ee)nt+=We.weight;let Te=Ae.length,ne=Ee.length;if(s!==void 0&&Ae.length===0&&Ee.length===0){ge=ve;continue}let ye=0,me=0;if(be>0){if(c===1)ye=be/z,me=be/(z*z);else for(let ze=0;ze<fe;ze++){let St=be/fe,dt=z-ze*St;ye+=St/dt,me+=St/(dt*dt)}ue+=ye,V+=me;let We=fe===X&&X>0?1:be/z;i===1?(U*=1-We,Z+=We<1?be/z/(z-be):1/0):U=Math.exp(-ue),H.push({t:oe,d:be,n:z,e:fe,S:U})}let Se=Math.sqrt(i===1?Z:V),Ke=0,$e=0;if(g){let We=0,ze=0,St=W===void 0?P:Array.from(W.values(),dt=>dt[0]);for(let dt of St){let Tn=W?.get(dt.id)??[dt],jt=0,It=0;for(let ht of H){if(ht.t>oe)break;let Qt=Tn.some(Ft=>Ft.start<ht.t&&Ft.time>=ht.t),an=Tn.some(Ft=>Ft.time===ht.t&&Ft.event),tn=ht.d/ht.n;if(c===1)It+=(Number(an)-Number(Qt)*tn)/ht.n;else{let Ft=0,nn=0;for(let G=0;G<ht.e;G++){let le=ht.d/ht.e,Le=ht.n-G*le;Ft+=(Le+G*le)/(ht.e*Le*Le),nn-=le/(Le*Le)}It+=Ft*Number(an)+nn*Number(Qt)}i===1&&tn<1&&(jt-=U*(Number(an)-Number(Qt)*tn)/((1-tn)*ht.n))}We+=(dt.weight*jt)**2,ze+=(dt.weight*It)**2}Ke=Math.sqrt(We),$e=Math.sqrt(ze)}let st=g?c===2?i===1?Ke:$e:i===1?U===0?NaN:Ke/U:$e:Se,ut=g?c===2?U*(i===1?Ke:$e):i===1?Ke:U*$e:U*Se,Ot=NaN,Xt=NaN;if(p!=="none")if(p==="log")Ot=U===0?ut===0?0:NaN:U*Math.exp(-L*st),Xt=U===0?ut===0?0:NaN:Math.min(1,U*Math.exp(L*st));else if(p==="log-log")if(U===0||U===1)Ot=ut===0?U:NaN,Xt=Ot;else{let We=Math.log(-Math.log(U));Ot=Math.exp(-Math.exp(We-L*st/Math.log(U))),Xt=Math.exp(-Math.exp(We+L*st/Math.log(U)))}else if(p==="plain")U===0&&ut===1/0?(Ot=NaN,Xt=NaN):(Ot=Math.max(0,U-L*ut),Xt=Math.min(1,U+L*ut));else if(p==="logit")if(U===0||U===1)Ot=ut===0?U:NaN,Xt=Ot;else{let We=Math.log(U/(1-U)),ze=L*ut/(U*(1-U));Ot=1-1/(1+Math.exp(We-ze)),Xt=1-1/(1+Math.exp(We+ze))}else if(U>0&&U<1){let We=Math.asin(Math.sqrt(U)),ze=L*ut/(2*Math.sqrt(U*(1-U)));Ot=Math.sin(Math.max(0,We-ze))**2,Xt=Math.sin(Math.min(Math.PI/2,We+ze))**2}else Ot=NaN,Xt=NaN;let Qn=z;S.strata.push(q[k]),S.time.push(oe),S.nRisk.push(Qn),S.nEvent.push(be),S.nCensor.push(nt),S.surv.push(U),S.cumhaz.push(ue),S.stdChaz.push(g?$e:Math.sqrt(V)),S.stdErr.push(g?c===2?U*(i===1?Ke:$e):i===1?Ke:U*$e:U*Se),S.lower.push(Ot),S.upper.push(Xt),S.counts&&(S.counts.nRisk.push(X),S.counts.nEvent.push(Te),S.counts.nCensor.push(ne)),o===void 0&&(z-=be+nt,ee-=ve-ge),ge=ve}let te=S.time.slice(B),se=S.surv.slice(B),Me=S.lower.slice(B),Ne=S.upper.slice(B);S.median.push([$r(te,se),p==="none"?NaN:$r(te,Me),p==="none"?NaN:$r(te,Ne)]),S.n.push(Q),S.records.push(Q);let de=S.nRisk.slice(B);S.nMax.push(Math.max(...de)),S.nStart.push(de[0]??0),S.events.push(P.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=P.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),Fe=we===0?0:Math.max(-1074,Math.min(1023,Math.floor(Math.log2(we)))),xe=we===0?1:2**Fe,_e=Number.isFinite(ce)&&!Number.isFinite(ce-I),Oe=ge=>{let oe=1;for(let ve of H)if(ve.t<=ge)oe=ve.S;else break;return oe},Be=ge=>{let oe=1;for(let ve of H)if(ve.t/xe<=ge)oe=ve.S;else break;return oe},je=$n();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];wn(je,Be(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];wn(je,Oe(ve)*(be-ve),ge.length+1)}}let tt=jn(je)*(_e?xe:1),Ue=$n();if(Number.isFinite(ce))for(let ge of H){if(ge.t>ce||ge.n<=ge.d)continue;let oe=$n();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++)wn(oe,Be(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++)wn(oe,Oe(be[fe])*((be[fe+1]-be[fe])/xe),be.length+1)}let ve=jn(oe);wn(Ue,ve*ve*ge.d/(ge.n*(ge.n-ge.d)),H.length+1)}let Qe=jn(Ue);S.rmean.push(l==="none"?NaN:tt),S.seRmean.push(l==="none"?NaN:Math.sqrt(Qe)*xe),F.push(l==="none"?p==="none"?[Q,S.nMax[k],S.nStart[k],S.events[k],S.median[k][0]]:[Q,S.nMax[k],S.nStart[k],S.events[k],S.median[k][0],S.median[k][1],S.median[k][2]]:p==="none"?[Q,S.nMax[k],S.nStart[k],S.events[k],tt,Math.sqrt(Qe)*xe,S.median[k][0]]:[Q,S.nMax[k],S.nStart[k],S.events[k],tt,Math.sqrt(Qe)*xe,S.median[k][0],S.median[k][1],S.median[k][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:q,table:F},J("survfit",S)}var U1=new Set(["newdata","confType","confInt","times","ties","weights","init","control","nocenter","confLevel","robust"]),H1=["eps","iterMax","tolerChol","tolerInf","timefix"];function Y1(e,t,n,r={}){K(r,U1,"survfitCoxph"),K(_t(r,"control"),H1,"survfitCoxph","control");let{newdata:o,confType:s="log",confInt:a=.95,times:c,...i}=r;if(!["log","log-log","plain","none"].includes(s))throw new RangeError(`invalid 'confType': ${String(s)}`);if(!(a>0&&a<1))throw new RangeError("'confInt' must be between 0 and 1");let l=Ye(()=>fo(e,t,n,i)),{ties:f="efron",weights:m}=i,u=(n[0]??[]).map((U,ue)=>G1(n.map(Z=>Z[ue]))),h=n.map(U=>U.map((ue,Z)=>u[Z](ue))),p=Ye(()=>fo(e,t,h,i)),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=i.control?.timefix===!1?e.slice():pr(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,V)=>ue+Z*d[V],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),A=new Array(M).fill(0),T=Array.from({length:M},()=>new Array(g).fill(0)),q=new Array(M).fill(0),C=new Array(M).fill(0),_=new Array(M).fill(0),I=0,O=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,V=0,Y=0,j=new Array(g).fill(0);for(;D>=0&&b[N[D]]===ue;D--){let W=N[D],te=E(W),se=y.get(W),Me=w(h[W]);I+=te*se,O+=te;for(let Ne=0;Ne<g;Ne++)L[Ne]+=te*se*Me[Ne];if(t[W]===1){Z++,V+=te,Y+=te*se;for(let Ne=0;Ne<g;Ne++)j[Ne]+=te*se*Me[Ne]}else _[U]+=te}if(q[U]=O,C[U]=V,Z===0)continue;let H=f==="efron"?Z:1;for(let W=0;W<H;W++){let te=f==="efron"?W/Z:0,se=I-te*Y,Ne=V/H/se;R[U]+=Ne,A[U]+=Ne/se;for(let de=0;de<g;de++)T[U][de]+=Ne*((L[de]-te*j[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))),F=a<1-1e-8?ie(1-(1-a)/2):ie((1-a)/2,{lowerTail:!1}),k=[],P=[],B=[],Q=[],re=[];for(let U of S){let ue=w(U),Z=Math.exp(ue.reduce((Ne,de,ce)=>Ne+de*d[ce],0)),V=0,Y=0,j=new Array(g).fill(0),H=[],W=[],te=[],se=[],Me=[];for(let Ne=0;Ne<M;Ne++){V+=R[Ne],Y+=A[Ne];for(let _e=0;_e<g;_e++)j[_e]+=ue[_e]*R[Ne]-T[Ne][_e];let de=0;for(let _e=0;_e<g;_e++)for(let Oe=0;Oe<g;Oe++)de+=j[_e]*v[_e][Oe]*j[Oe];let ce=Z*V,qe=Math.exp(-ce),we=Z*Math.sqrt(Y+de);Me.push(ce),H.push(qe),W.push(we);let[Fe,xe]=X1(qe,we,F,s);te.push(Fe),se.push(xe)}k.push(H),P.push(W),B.push(te),Q.push(se),re.push(Me)}let z=k.map((U,ue)=>[$r(x,U),$r(x,B[ue]),$r(x,Q[ue])]),ee={fit:l,time:x,nRisk:q,nEvent:C,nCensor:_,surv:k,stdErr:P,lower:B,upper:Q,cumhaz:re,median:z,records:N.length,nStart:q[0]??0,events:C.reduce((U,ue)=>U+ue,0),warnings:l.warnings.slice()};return c!==void 0&&(ee.at=Q1(ee,c)),J("survfitCoxph",ee)}function G1(e){let t=s=>{let a=0;for(let c of s)Number.isFinite(c)&&Math.abs(c)>a&&(a=Math.abs(c));return a>0?Math.floor(Math.log2(a)):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 X1(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 Q1(e,t){let n=[...t].sort((s,a)=>s-a),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 a=-1;for(;a+1<e.time.length&&e.time[a+1]<=s;)a++;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 i=0;for(let l=o;l<=a;l++)i+=e.nEvent[l];r.nEvent.push(i),o=a+1,e.surv.forEach((l,f)=>{r.surv[f].push(a<0?1:e.surv[f][a]),r.stdErr[f].push(a<0?0:e.stdErr[f][a]),r.lower[f].push(a<0?1:e.lower[f][a]),r.upper[f].push(a<0?1:e.upper[f][a])})}return r}var Z1=["vcov","df"],J1=["parm","level","vcov","df"];function nh(e){return typeof e=="object"&&e!==null&&!Array.isArray(e)&&Array.isArray(e.vcov)&&Array.isArray(e.names)}function rh(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 oh=e=>new Set(e).size===e.length;function sh(e,t){wa(e);let n=e.coefficients,r=ba(e),o=n.length,s=n.map(m=>Number.isNaN(m)),a,c,i=[],l;if(t===void 0){let m=Sc(e),u=new Map(m.names.map((p,g)=>[p,g])),h=r.map((p,g)=>s[g]?void 0:u.get(p));a=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:Tc(m.vcov,p,m.vcov[p][p])??Math.sqrt(m.vcov[p][p]))}else{let m=typeof t=="function"?Ye(()=>t(e)):t,u=nh(m)?m.vcov:m;if(nh(m)){if(a=rh(m.vcov),c=m.names.slice(),c.length!==a.length)throw new RangeError("'vcov' must have one name per row");i=[...m.warnings??[]]}else a=rh(m);l=a.map((h,p)=>{let g=h[p],d=typeof u=="object"&&u!==null?Tc(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&&oh(c)&&oh(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:i}}let f;if(a.length===o)f=n.map((m,u)=>u);else if(a.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:i}}function ah(e,t){if(t===void 0)return hr(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 ih=e=>Number.isFinite(e)&&e>0;function eb(e){return hr(e)?e.priorWeights.filter(t=>t!==0).length:e.rank+e.dfResidual}function tb(e,t={}){K(t,Z1,"coeftest");let{est:n,se:r,names:o,warnings:s}=sh(e,t.vcov),a=ah(e,t.df),c=ih(a),i=n.map((f,m)=>f/r[m]),l=i.map(f=>Number.isNaN(f)?NaN:c?2*wt(-Math.abs(f),a,!0,!1):2*ae(-Math.abs(f)));return J("coeftest",{estimate:n,stdError:r,statistic:i,pValue:l,names:o,statisticName:c?"t value":"z value",df:a,method:c?"t test of coefficients":"z test of coefficients",nobs:eb(e),warnings:s},e,typeof t.vcov=="function"?void 0:t.vcov)}function nb(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))),a=o.toFixed(Math.max(0,2-s));return(a.includes(".")?a.replace(/0+$/,"").split(".")[1]:"").length}),r=Math.max(...n);return[`${t[0].toFixed(r)} %`,`${t[1].toFixed(r)} %`]}function rb(e,t={}){K(t,J1,"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=sh(e,t.vcov),o=ah(e,t.df),s=r.est.map((m,u)=>u),a=t.parm;if(a!==void 0){let m=Array.isArray(a)?a:[a];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,[i,l]=ih(o)?[bt(c,o,!0,!1),c<1e-8?bt(c,o,!1,!1):bt(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],i)),upper:s.map(m=>f(r.est[m],r.se[m],l)),names:s.map(m=>r.names[m]),colNames:nb(c),warnings:r.warnings},e,typeof t.vcov=="function"?void 0:t.vcov)}var ob=["test","dispersion"],sb=(e,t)=>vt(e,t/2,2,!1,!1);function ab(e,t,n){let{fit:r,w:o}=Ar(e,t,n,{start:void 0});return{dfRes:o.filter(a=>a>0).length-r.rank,dev:r.deviance,fit:r,x:e.x.map(a=>t.map(c=>a[c])),intercept:e.intercept}}var Cc=(e,t)=>pc(t.x,e.fit.workingResiduals,e.fit.workingWeights,e.intercept);function ib(e,t={}){K(t,ob,"glmAnova");let n=Array.isArray(e)?e:[e];if(n.length===0)throw new RangeError("no fits to compare");let r=n.map(Bn),o=r.every(u=>u.k===r[0].k)?r[0].k:0,s=o===0?n:r.map(u=>u.fit),a=t.dispersion===void 0?void 0:mf(t.dispersion,o),c=a?.s??0,i=cb(s,Array.isArray(e)&&s.length>1,{...t,...a===void 0?{}:{dispersion:a.m}},c);if(o===0&&c===0)return J("glmAnova",i,...n);let l=new Set(["Deviance","Resid. Dev","Rao","Cp"]),f=i.colNames.map(u=>l.has(u)),m=i.table.map(u=>u.map((h,p)=>f[p]?$(h,2*o):h));if(c!==0&&i.colNames.includes("Cp")){let u=i.colNames.indexOf("Cp"),h=i.colNames.indexOf("Resid. Dev"),p=i.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",{...i,table:m},...n)}function cb(e,t,n,r=0){let o=new mt,s=e[0],a=Za(s.model.familySpec);if(e.some(M=>{let R=Za(M.model.familySpec);return R.family!==a.family||R.name!==a.name||R.varianceName!==a.varianceName}))throw new RangeError("models were not all fitted with the same family and link");let c=a.fixedDispersion,i=n.test===void 0?n.dispersion!==void 0||c?"Chisq":"F":n.test;if(i!==!1&&!["Rao","LRT","Chisq","F","Cp"].includes(i))throw new RangeError(`invalid 'test': ${String(i)}`);let l=[],f,m,u=[],h=new Set,p;if(t){let M=e[0].model.x.length;if(e.some(A=>A.model.x.length!==M))throw new RangeError("models were not all fitted to the same size of dataset");let R=(A,T)=>A.length===T.length&&A.every((q,C)=>JSON.stringify(q)===JSON.stringify(T[C]));if(e.some(A=>!R(A.model.yGiven,e[0].model.yGiven)))throw new RangeError("models were not all fitted to the same response");if(e.some(A=>!R(A.model.priorGiven,e[0].model.priorGiven)))throw new RangeError("models were not all fitted with the same prior weights");if(e.forEach((A,T)=>l.push({name:String(T+1),dfRes:A.dfResidual,dev:A.deviance})),f=l.map((A,T)=>T===0?NaN:l[T-1].dfRes-A.dfRes),m=l.map((A,T)=>T===0?NaN:l[T-1].dev-A.dev),e.forEach((A,T)=>{if(T===0||f[T]===0)return;let[q,C]=f[T]>0?[e[T-1],A]:[A,e[T-1]],_=L=>L.filter((D,S)=>q.priorWeights[S]>0&&C.priorWeights[S]>0),I=q.model.offset.map((L,D)=>L-C.model.offset[D]),O=I.some((L,D)=>L!==0&&q.priorWeights[D]>0)?q.model.x.map((L,D)=>[...L,I[D]]):q.model.x;Fs(_(O),_(C.model.x))||h.add(T)}),i==="Rao"){let A=e.map(T=>({dfRes:T.dfResidual,dev:T.deviance,fit:ch(T),x:T.model.x,intercept:T.model.intercept}));u=l.map((T,q)=>q===0?NaN:f[q]<0?-Cc(A[q],A[q-1]):Cc(A[q-1],A[q]))}p=e.reduce((A,T)=>T.dfResidual<A.dfResidual?T:A)}else{let M=s,R=M.model,A=R.x.length>0?R.x[0].length-(R.intercept?1:0):0,T=R.intercept?[0]:[],q=_=>[...T,...Array.from({length:_},(I,O)=>O+T.length)],C=[];for(let _=0;_<=A;_++)_===A?C.push({dfRes:M.dfResidual,dev:M.deviance,fit:ch(M),x:R.x,intercept:R.intercept}):_>0||i==="Rao"?C.push(ab(R,q(_),o)):C.push(void 0);l.push({name:"NULL",dfRes:M.dfNull,dev:M.nullDeviance});for(let _=1;_<=A;_++)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)),i==="Rao"&&(u=l.map((_,I)=>I===0?NaN:Cc(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 mt,d=n.dispersion??ar(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,A)=>{if(A===0||f[A]===0||h.has(A))return NaN;let T=Ve(M[A]/d*Math.sign(f[A]),-2*r);return T<0||Number.isNaN(T)?NaN:sb(T,Math.abs(f[A]))}),y=[];if(i==="Chisq"||i==="LRT")E.push("Pr(>Chi)"),y=[w(m)];else if(i==="Rao")E.push("Rao","Pr(>Chi)"),y=[u.map((M,R)=>h.has(R)?NaN:M),w(u)];else if(i==="F"){if(c&&o.add(`using F test with a '${a.family}' family is inappropriate`),v!==0){let M=l.map((R,A)=>{let T=Ve(m[A]/f[A]/d,-2*r);return A===0||f[A]===0||h.has(A)||T<0?NaN:T});E.push("F","Pr(>F)"),y=[M,M.map((R,A)=>Number.isNaN(R)?NaN:pt(R,Math.abs(f[A]),v,!1,!1))]}}else if(i==="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(A=>A[R])]);return{colNames:E,rowNames:l.map(M=>M.name),table:x,warnings:o.list}}function ch(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 lb=["type"];function lh(e,t,n,r,o=0){let s=[...t,...n];if(s.length===0)return{ss:0,df:0};let a=e.y.flatMap((p,g)=>e.weights===void 0||e.weights[g]!==0?[g]:[]),c=a.map(p=>e.weights===void 0?1:Math.sqrt(e.weights[p])),i=Ct(a.map((p,g)=>s.map(d=>e.x[p][d]*c[g]))),l=ma(e.weights===void 0?void 0:a.map(p=>$(e.weights[p],-o)),a.length),f=i.pivot.slice(0,i.rank),{qty:m}=so(f.map(p=>a.map((g,d)=>At(l[d],e.x[g][s[p]]))),a.map((p,g)=>At(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 Oc(e,t={}){if(K(t,lb,"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=na([e]),a=ra([e],s)[0];if(o===0)throw new Error("residual df = 0");let c=Fi(e,s),i=y=>{let x=c.map(([A,T])=>[$(A,-y),T]),M=x.reduce((A,[T])=>A+T,0),R=x.reduce((A,[T,q])=>A+T*q,0)/M;return Number.isFinite(M)&&Number.isFinite(R)?x.reduce((A,[T,q])=>A+T*(q-R)**2,0):NaN},l=0,f=i(0);if(Number.isFinite(f)||(l=oa(c),f=i(l)),qr(c,a,l)||a===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])),A=h(new Set(p.filter(T=>T!==M)));g.push(M===0?"(Intercept)":e.termLabels[M-1]),x.push({ss:lh(e,A,R,s,y).ss,df:R.length})}}else{let M=R=>e.factors.flatMap((A,T)=>A[R-1]>0?[T]:[]);for(let R of p){if(R===0)continue;let A=M(R),T=p.filter(_=>_!==0&&_!==R&&A.every(I=>e.factors[I][_-1]>0)),q=h(new Set(p.filter(_=>_!==R&&!T.includes(_)))),C=lh(e,q,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=oa(c),d=v(l));let N=y=>$(y,2*s+l),b=$(a,-l),E=b/o,w=d.map(({ss:y,df:x})=>{let M=y/x/E;return[N(y),x,M,pt(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 Na=[0,0];var Fc=e=>e.map(t=>t.map(n=>n[0]));function fh(e){let t=e[0]?.length??0;return Array.from({length:t},(n,r)=>e.map(o=>o[r]))}function jr(e,t){let n=t[0]?.length??0;return e.map(r=>{let o=new Array(n).fill(Na);return r.forEach((s,a)=>{let c=t[a];for(let i=0;i<n;i++)o[i]=pe(o[i],Ce(s,c[i]))}),o})}function zr(e){let t=Na;return e.forEach((n,r)=>t=pe(t,n[r])),t}function mh(e){let t=e.length,n=e.map(a=>a.slice()),r=Array.from({length:t},(a,c)=>c),o=1,s=!1;for(let a=0;a<t;a++){let c=a;for(let i=a+1;i<t;i++)Math.abs(n[i][a][0])>Math.abs(n[c][a][0])&&(c=i);if(n[c][a][0]===0){s=!0;continue}c!==a&&([n[a],n[c]]=[n[c],n[a]],[r[a],r[c]]=[r[c],r[a]],o=-o);for(let i=a+1;i<t;i++){let l=ot(n[i][a],n[a][a]);n[i][a]=l;for(let f=a+1;f<t;f++)n[i][f]=De(n[i][f],Ce(l,n[a][f]))}}return{a:n,perm:r,sign:o,singular:s}}function uh(e){let{a:t,sign:n,singular:r}=mh(e);if(r)return Na;let o=[n,0];return t.forEach((s,a)=>o=Ce(o,s[a])),o}function hh(e){let t=e.length,{a:n,perm:r,singular:o}=mh(e);if(o)throw new Error("singular matrix");let s=Array.from({length:t},()=>new Array(t).fill(Na));for(let a=0;a<t;a++){let c=r.map(i=>[i===a?1:0,0]);for(let i=0;i<t;i++)for(let l=0;l<i;l++)c[i]=De(c[i],Ce(n[i][l],c[l]));for(let i=t-1;i>=0;i--){for(let l=i+1;l<t;l++)c[i]=De(c[i],Ce(n[i][l],c[l]));c[i]=ot(c[i],n[i][i])}for(let i=0;i<t;i++)s[i][a]=c[i]}return s}var fb=["group","idata","M","X"],ph=(e,t)=>vt(e,t/2,2,!1,!1);function Dc(e){let t=e.length,n=ft(zr(e),t);return uh(e.map(r=>r.map(o=>ot(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),a=-n*o*Math.log(e),c=t*(t+1)/2-1,i=ph(a,c),l=ph(a,c+4);return i+s*(l-i)}var mb=(e,t)=>Array.from({length:t},(n,r)=>e.map(o=>o[r]));function ub(e,t,n){let r=e.length,o=v=>v.length===0?0:Ct(Array.from({length:r},(N,b)=>v.map(E=>E[b]))).rank,s=n.length===0?void 0:Ct(Array.from({length:r},(v,N)=>n.map(b=>b[N]))),a=s===void 0?[]:s.pivot.slice(0,s.rank).map(v=>n[v]),c=a.length,i=[...a,...t],l=Ct(Array.from({length:r},(v,N)=>i.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=>i[v]),m=v=>v.map(N=>[N,0]),u=new Array(r).fill([0,0]),h=Hn(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=Hn(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 hb(e,t={}){K(t,fb,"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),a=n.map((w,y)=>!w.some(s)&&(o===void 0||!s(o[y])));if(n.some((w,y)=>a[y]&&w.some(x=>!Number.isFinite(x))))throw new RangeError("y must not contain infinite values");let c=Ie(n.filter((w,y)=>a[y])).groups,i=c.length,l=o?.filter((w,y)=>a[y]).map(Mt)??c.map(()=>""),f=[...new Set(l)].sort(kn),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 A of y)M=pe(M,[c[A][x],0]);let R=ft(M,y.length);for(let A of y)m[A][x]=De([c[A][x],0],R)}}let u=i-f.length;if(u<=0)throw new RangeError(`no residual degrees of freedom: ${i} 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 A of m)R=pe(R,Ce(A[y],A[M]));return R})),p=t.idata??{index:Array.from({length:r},(w,y)=>y+1)},g=w=>{let y=Ye(()=>to(w,p));if(y.x.length!==r)throw new RangeError("idata must have one row per column of y");return mb(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=ub(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=Dc(N);return{statistic:E,statisticName:"W",pValue:Pc(E,b,u,r),method:"Mauchly's test of sphericity",warnings:[]}}var pb=["n","d","type","sigLevel","power","alternative"],db=["n1","n2","d","sigLevel","power","alternative"],gb=["h","n","sigLevel","power","alternative"],wb=["h","n1","n2","sigLevel","power","alternative"],bb=["h","n","sigLevel","power","alternative"],vb=["d","n","sigLevel","power","alternative"],Nb=["n","r","sigLevel","power","alternative"],yb=["w","N","df","sigLevel","power"],Mb=["k","n","f","sigLevel","power"],Eb=["u","v","f2","sigLevel","power"],xb=["size"],Rb={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 gh(e="p",...t){K(Cs(t),xb,"cohenES");let n=Ls(["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:Rb[r][["small","medium","large"].indexOf(o)],method:"Conventional effect size from Cohen (1982)"}}function Ab(e,t){if(!ya(e)||!ya(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 Tb(e,t){if(e.length!==t.length)throw new RangeError("'P0' and 'P1' must have the same length");if(![...e,...t].every(ya))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+=$t(t[r]-e[r],e[r]));return Math.sqrt(n)}function Sb(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(ya)))throw new RangeError("'P' must contain probabilities in [0, 1]");let t=e.map(s=>s.reduce((a,c)=>a+c,0)),n=e[0]?.length??0,r=Array.from({length:n},(s,a)=>e.reduce((c,i)=>c+i[a],0)),o=0;for(let s=0;s<n;s++)for(let a=0;a<e.length;a++){let c=t[a]*r[s];if(!(c===0&&e[a][s]===0))if(c>=kb)o+=$t(e[a][s]-c,c);else{let i=Math.sqrt(t[a]),l=Math.sqrt(r[s]),f=e[a][s]/i/l-i*l;o+=f*f}}return Math.sqrt(o)}var kb=2**-1022,ya=e=>e>=0&&e<=1,qb="\u2018sig.level\u2019 must be numeric in [0, 1]",Ib="\u2018power\u2019 must be numeric in [0, 1]";function gr(e,t){let n=r=>!Re(r)&&!(typeof r=="number"&&r>=0&&r<=1);if(n(e))throw new RangeError(qb);if(n(t))throw new RangeError(Ib)}function wr(e,t){if(typeof e=="number"&&Number.isNaN(e))throw new RangeError("the effect size must not be NaN");return typeof e=="string"?gh(t,e).effectSize:e}var os=["two.sided","less","greater"];function Ma(e,t){return e==="two.sided"?t:e==="less"?[-10,5]:[-5,10]}function wh(e,t,n,r){if(r==="less")return Tr(Bt(n,e),e,t,!0);if(r==="greater")return Tr(Bt(n,e,{lowerTail:!1}),e,t,!1);let o=Bt(n/2,e,{lowerTail:!1});return Tr(o,e,t,!1)+Tr(-o,e,t,!0)}var dh=e=>e<1e-8?ie(e,{lowerTail:!1}):ie(1-e);function bh(e,t,n){return n==="greater"?ae(dh(t)-e,{lowerTail:!1}):n==="less"?ae(ie(t)-e):ae(dh(t/2)-e,{lowerTail:!1})+ae(ie(t/2)-e)}function _b(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 Lb(e={}){K(e,pb,"pwrTTest");let{n:t,power:n}=e,r=wr(e.d,"t"),o=e.sigLevel===void 0?.05:e.sigLevel;if(on([t,r,n,o],"exactly one of n, d, power, and sig.level must be NULL"),gr(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"]),a=ke(e.alternative,os),c=s==="two.sample"?2:1;a==="two.sided"&&!Re(r)&&(r=Math.abs(r));let i=(m,u,h)=>wh((m-1)*c,Math.sqrt(m/c)*u,h,a);if(Re(n))n=i(t,r,o);else{let m=n;Re(t)?t=Ge(u=>i(u,r,o)-m,2+1e-10,1e9):Re(r)?r=Ge(u=>i(t,u,o)-m,...Ma(a,[1e-7,10])):o=Ge(u=>i(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:a,...l===void 0?{}:{note:l},method:f,warnings:[]}}function Cb(e={}){K(e,db,"pwrT2nTest");let{n1:t,n2:n,power:r}=e,o=wr(e.d,"t"),s=e.sigLevel===void 0?.05:e.sigLevel;if(on([t,n,o,r,s],"exactly one of n1, n2, d, power, and sig.level must be NULL"),gr(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 a=ke(e.alternative,os);a==="two.sided"&&!Re(o)&&(o=Math.abs(o));let c=(i,l,f,m)=>wh(i+l-2,f/Math.sqrt(1/i+1/l),m,a);if(Re(r))r=c(t,n,o,s);else{let i=r;Re(t)?t=Ge(l=>c(l,n,o,s)-i,2+1e-10,1e9):Re(n)?n=Ge(l=>c(t,l,o,s)-i,2+1e-10,1e9):Re(o)?o=Ge(l=>c(t,n,l,s)-i,...Ma(a,[1e-7,10])):s=Ge(l=>c(t,n,o,l)-i,1e-10,1-1e-10)}return{n1:t,n2:n,d:o,sigLevel:s,power:r,alternative:a,method:"t test power calculation",warnings:[]}}function Vc(e,t,n,r,o,s){let{n:a,power:c}=t,i=wr(e,n==="h"?"p":"t"),l=t.sigLevel===void 0?.05:t.sigLevel;if(on([i,a,c,l],`exactly one of ${n}, n, power, and sig.level must be NULL`),gr(l,c),!Re(a)&&!(a>=1))throw new RangeError("number of observations in each group must be at least 1");let f=ke(t.alternative,os);f==="two.sided"&&!Re(i)&&(i=Math.abs(i));let m=(u,h,p)=>bh(u*r(h),p,f);if(Re(c))c=m(i,a,l);else{let u=c;Re(i)?i=Ge(h=>m(h,a,l)-u,...Ma(f,[s,10])):Re(a)?a=Ge(h=>m(i,h,l)-u,o,1e9):l=Ge(h=>m(i,a,h)-u,1e-10,1-1e-10)}return{es:i,n:a,sigLevel:l,power:c,alternative:f}}function Ob(e={}){K(e,gb,"pwr2pTest");let t=Vc(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 Fb(e={}){K(e,bb,"pwrPTest");let t=Vc(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 Db(e={}){K(e,vb,"pwrNormTest");let t=Vc(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 Pb(e={}){K(e,wb,"pwr2p2nTest");let{n1:t,n2:n,power:r}=e,o=wr(e.h,"p"),s=e.sigLevel===void 0?.05:e.sigLevel;if(on([o,t,n,r,s],"exactly one of h, n1, n2, power, and sig.level must be NULL"),gr(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 a=ke(e.alternative,os);a==="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))},i=(l,f,m,u)=>bh(l*Math.sqrt(c(f,m)),u,a);if(Re(r))r=i(o,t,n,s);else{let l=r;Re(o)?o=Ge(f=>i(f,t,n,s)-l,...Ma(a,[1e-10,10])):Re(t)?t=Ge(f=>i(o,f,n,s)-l,2+1e-10,1e9):Re(n)?n=Ge(f=>i(o,t,f,s)-l,2+1e-10,1e9):s=Ge(f=>i(o,t,n,f)-l,1e-10,1-1e-10)}return{h:o,n1:t,n2:n,sigLevel:s,power:r,alternative:a,method:"difference of proportion power calculation for binomial distribution (arcsine transformation)",note:"different sample sizes",warnings:[]}}function Vb(e={}){K(e,Nb,"pwrRTest");let{n:t,power:n}=e,r=wr(e.r,"r"),o=e.sigLevel===void 0?.05:e.sigLevel;if(on([t,r,n,o],"exactly one of n, r, power, and sig.level must be NULL"),gr(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,os);s==="two.sided"&&!Re(r)&&(r=Math.abs(r));let a=(i,l,f,m)=>{let u=Bt(m?f/2:f,i-2,{lowerTail:!1}),h=u**2+i-2,p=h===1/0&&Number.isFinite(u)?1/Math.sqrt(1+(i-2)/u/u):Math.sqrt(u**2/h),g=Math.atanh(l)+l/(2*(i-1)),d=Math.atanh(p),v=ae((g-d)*Math.sqrt(i-3));return m?v+ae((-g-d)*Math.sqrt(i-3)):v},c=(i,l,f)=>s==="two.sided"?a(i,l,f,!0):a(i,s==="less"?-l:l,f,!1);if(Re(n))n=c(t,r,o),s==="less"&&(r=-r);else{let i=n;if(Re(t))t=Ge(l=>c(l,r,o)-i,4+1e-10,1e9);else if(Re(r)){let[l,f]=s==="two.sided"?[1e-10,.9999999999]:[-.9999999999,.9999999999];r=Ge(m=>c(t,m,o)-i,l,f)}else o=Ge(l=>c(t,r,l)-i,1e-10,1-1e-10)}return{n:t,r,sigLevel:o,power:n,alternative:s,method:"approximate correlation power calculation (arctangh transformation)",warnings:[]}}function $b(e){K(e,yb,"pwrChisqTest");let{N:t,power:n}=e,{df:r}=e,o=wr(e.w,"chisq"),s=e.sigLevel===void 0?.05:e.sigLevel;if(on([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(gr(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 a=(c,i,l)=>{let f=i*c**2;return f===1/0?1:Nt(Pn(l,r,{lowerTail:!1}),r,{ncp:f,lowerTail:!1})};if(Re(n))n=a(o,t,s);else{let c=n;Re(o)?o=Ge(i=>a(i,t,s)-c,1e-10,1e9):Re(t)?t=Ge(i=>a(o,i,s)-c,1+1e-10,1e9):s=Ge(i=>a(o,t,i)-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 jb(e={}){K(e,Mb,"pwrAnovaTest");let{k:t,n,power:r}=e,o=wr(e.f,"anov"),s=e.sigLevel===void 0?.05:e.sigLevel;if(on([t,n,o,r,s],"exactly one of k, n, f, power, and sig.level must be NULL"),gr(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 a=(c,i,l,f)=>{let m=c-1,u=(i-1)*c,h=c*i*l**2;return!Number.isFinite(h)&&Number.isFinite(c)&&Number.isFinite(i)&&Number.isFinite(l)&&(h=_b([c,i,l,l])),h===1/0?1:zs(m,u)?Ks(m,u,h,f):dn(Zn(f,m,u,{lowerTail:!1}),m,u,{ncp:h,lowerTail:!1})};if(Re(r))r=a(t,n,o,s);else{let c=r;Re(t)?t=Ge(i=>a(i,n,o,s)-c,2+1e-10,100):Re(n)?n=Ge(i=>a(t,i,o,s)-c,2+1e-10,1e9):Re(o)?o=Ge(i=>a(t,n,i,s)-c,1e-7,1e7):s=Ge(i=>a(t,n,o,i)-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 zb(e={}){K(e,Eb,"pwrF2Test");let{u:t,v:n,power:r}=e,o=wr(e.f2,"f2"),s=e.sigLevel===void 0?.05:e.sigLevel;if(on([t,n,o,r,s],"exactly one of u, v, f2, power, and sig.level must be NULL"),gr(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 a=(c,i,l,f)=>{let m=l*(c+i+1);return!Number.isFinite(m)&&Number.isFinite(c)&&Number.isFinite(i)&&Number.isFinite(l)&&(m=2*(l*(c/2+i/2+.5))),m===1/0?1:zs(c,i)?Ks(c,i,m,f):dn(Zn(f,c,i,{lowerTail:!1}),c,i,{ncp:m,lowerTail:!1})};if(Re(r))r=a(t,n,o,s);else{let c=r;Re(t)?t=Ge(i=>a(i,n,o,s)-c,1+1e-10,100):Re(n)?n=Ge(i=>a(t,i,o,s)-c,1+1e-10,1e9):Re(o)?o=Ge(i=>a(t,n,i,s)-c,1e-7,1e7):s=Ge(i=>a(t,n,o,i)-c,1e-10,1-1e-10)}return{u:t,v:n,f2:o,sigLevel:s,power:r,method:"Multiple regression power calculation",warnings:[]}}var Kb=["id","dv","between","within","type","correction","es","observed"],Ea=e=>e===void 0?[]:typeof e=="string"?[e]:[...e],Wb=1e-12,vh=1e-24;function Nh(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 ss=e=>typeof e=="boolean"?e?"TRUE":"FALSE":Mt(e);function Bb(e){let t=[...new Set(e)];return t.every(n=>typeof n=="number")?t.sort((n,r)=>n-r).map(ss):[...new Set(t.map(ss))].sort(kn)}var yh=(e,t)=>t.split(":").includes(e);function Mh(e,t,n,r,o){let s=l=>r.some(f=>yh(f,l));for(let l of r)if(!e.some(f=>yh(l,f.name)))throw new RangeError(`Observed variable not in data: ${l}`);let a=[...t.values()].reduce((l,f)=>l+f,0),c=e.reduce((l,f)=>s(f.name)?l+f.ss:l,0),i=["pes","ges"].filter(l=>n.includes(l));return{colNames:["num Df","den Df","MSE","F",...i,"Pr(>F)"],rowNames:e.map(l=>l.name),table:e.map(l=>{let f=i.map(m=>m==="pes"?l.ss/(l.ss+l.errSS):l.ss/(l.ss+a+c-(s(l.name)?l.ss:0)));return[l.numDf,l.denDf,o(l.errSS/l.denDf),l.f,...f,l.p]})}}function Ub(e,t){K(t,Kb,"aovEz");let n=Ea(t.between),r=Ea(t.within);if(n.length===0&&r.length===0)throw new Error("Either between or within need to be non-NULL!");for(let[j,H]of[["between",n],["within",r]]){let W=H.filter((te,se)=>H.indexOf(te)!==se);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 a=t.correction??"GG";if(!["GG","HF","none"].includes(a))throw new RangeError(`invalid 'correction': ${String(a)}`);let c=Ea(t.es??"ges");for(let j of c)if(j!=="ges"&&j!=="pes")throw new RangeError(`invalid 'es': ${String(j)}`);let i=Ea(t.observed),l=[],f=j=>j==null||typeof j=="number"&&Number.isNaN(j),m=Nh(e,t.dv),u=[t.id,...n,...r].map(j=>Nh(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,H)=>u.every(W=>!f(W[H]))),p=j=>j.filter((H,W)=>h[W]),g=Vt(p(u[0])).map(ss),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,H)=>Vt(p(u[1+H]))),N=r.map((j,H)=>Vt(p(u[1+n.length+H]))),b=g.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=>Bb(j)),w=v.map(j=>j.map(ss)),y=N.map(j=>j.map(ss)),x=y.map(j=>[...new Set(j)]);E.forEach((j,H)=>{if(j.length<2)throw new Error(`Factor "${n[H]}" consists of one level only. Remove factor from model?`)}),x.forEach((j,H)=>{if(j.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,A=new Set;if(g.forEach((j,H)=>{let W=JSON.stringify(w.map(se=>se[H])),te=R.get(j);te===void 0?R.set(j,W):te!==W&&A.add(j)}),A.size>0)throw new Error(`Following ids are in more than one between subjects condition:
8
- ${M.filter(j=>A.has(j)).join(", ")}`);let T=[[]];for(let j of x)T=T.flatMap(H=>j.map(W=>[...H,W]));let q=j=>JSON.stringify(y.map(H=>H[j])),C=new Map(T.map((j,H)=>[JSON.stringify(j),H])),_=new Set(g.map((j,H)=>q(H)));if(T.some(j=>!_.has(JSON.stringify(j))))throw new Error("Empty cells in within-subjects design (i.e., bad data structure).");let I=new Map,O=new Set;g.forEach((j,H)=>{let W=I.get(j);W===void 0&&I.set(j,W=new Set),W.add(q(H)),Number.isNaN(d[H])&&O.add(j)});let L=M.filter(j=>O.has(j)||I.get(j).size<T.length),D=new Set(L),S=Ie([d.filter((j,H)=>!D.has(g[H]))]).k,F=S===0?d:d.map(j=>$(j,-S)),k=j=>$(j,2*S),P=T.length,B=new Map;if(g.forEach((j,H)=>{let W=B.get(j);W===void 0&&B.set(j,W={s:T.map(()=>[0,0]),n:T.map(()=>0)});let te=C.get(q(H));W.s[te]=pe(W.s[te],[F[H],0]),W.n[te]++}),[...B.values()].some(j=>j.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 j=[...L].sort(kn),H=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
- ${H}`)}let Q=M.filter(j=>!L.includes(j)),re=new Map;g.forEach((j,H)=>re.has(j)||re.set(j,H));let z=Q.map(j=>{let H=B.get(j);return H.s.map((W,te)=>ft(W,H.n[te]))}),ee={};n.forEach((j,H)=>ee[j]=Q.map(W=>w[H][re.get(W)]));let U=Object.fromEntries(n.map(j=>[j,"contr.sum"])),ue=Object.fromEntries(n.map((j,H)=>[j,E[H]])),Z="Rank deficient model matrix; insufficient data to estimate full model.";if(P===1)return J("aovEz",V());return J("aovEz",Y());function V(){let j="y";for(;n.includes(j);)j+="_";let H={...ee,[j]:z.map(ce=>ce[0][0])},W=Ye(()=>Or(`${j} ~ ${n.length>0?n.join(" * "):"1"}`,H,{contrasts:U,levels:ue}));if(W.rank<W.coefNames.length)throw new Error(Z);if(W.dfResidual<=0)throw new RangeError(`no residual degrees of freedom: ${z.length} subject(s) for ${W.rank} parameter(s)`);let te=Ye(()=>Oc(W,{type:s}));l.push(...te.warnings.filter(ce=>!l.includes(ce)));let se=W.dfResidual,Me=te.rowNames.flatMap((ce,qe)=>{if(ce==="(Intercept)"||ce==="Residuals")return[];let[we,Fe,xe,_e]=te.table[qe];return[{name:ce,ss:we,numDf:Fe,errSS:W.rss,denDf:se,f:xe,p:_e}]}),Ne=te.colNames.indexOf("Sum Sq"),de={anova:Mh(Me,new Map([[0,W.rss]]),c,i,k),univariate:{colNames:te.colNames,rowNames:te.rowNames,table:te.table.map(ce=>ce.map((qe,we)=>we===Ne?k(qe):qe))},within:r,warnings:l};return Yt.set(de,{colNames:te.colNames,rowNames:te.rowNames,table:te.table}),de}function Y(){let j=Q.length,H=z.reduce((Ae,Ee)=>Ee.reduce((Te,ne)=>Te+ne[0]*ne[0],Ae),0),W=n.length>0?Ye(()=>to(`~ ${n.join(" * ")}`,ee,{contrasts:U,levels:ue})):{colNames:["(Intercept)"],assign:[0],x:Q.map(()=>[1]),termLabels:[],factors:[]},te=W.colNames.length;if(j===0||Ct(W.x).rank<te)throw new Error(Z);let se=j-te;if(se<=0)throw new RangeError(`no residual degrees of freedom: ${j} subject(s) for ${te} between-subject parameter(s)`);let Me=[0,...W.termLabels.map((Ae,Ee)=>Ee+1)],Ne=["(Intercept)",...W.termLabels],de=Ae=>W.assign.flatMap((Ee,Te)=>Ae.includes(Ee)?[Te]:[]),ce=Ae=>W.factors.flatMap((Ee,Te)=>Ee[Ae-1]>0?[Te]:[]),qe=Ae=>W.x.map(Ee=>[Ee[Ae],0]),we=new Array(j).fill([0,0]),Fe=(Ae,Ee,Te)=>{let{qtv:ne}=Hn([...Ae,...Ee].map(qe),we,Te);return ne.map(ye=>ye.slice(Ae.length,Ae.length+Ee.length))},xe=Ae=>{let{qtv:Ee}=Hn(W.colNames.map((Te,ne)=>qe(ne)),we,Ae);return Ee.map(Te=>Te.slice(te))},_e=Object.fromEntries(r.map((Ae,Ee)=>[Ae,T.map(Te=>Te[Ee])])),Oe=Ye(()=>to(`~ ${r.join(" * ")}`,_e,{contrasts:Object.fromEntries(r.map(Ae=>[Ae,"contr.sum"])),levels:Object.fromEntries(r.map((Ae,Ee)=>[Ae,x[Ee]]))})),Be=[0,...Oe.termLabels.map((Ae,Ee)=>Ee+1)],je=["(Intercept)",...Oe.termLabels],tt=Ae=>Ae.map(Ee=>Ae.map(Te=>{let ne=[0,0];return Ee.forEach((ye,me)=>ne=pe(ne,Ce(ye,Te[me]))),ne})),Ue=[],Qe=[],ge=[],oe=[],ve=[],be=[],fe=[],nt=new Map,X=!1;for(let Ae of Be){let Ee=Oe.assign.flatMap((We,ze)=>We===Ae?[ze]:[]),Te=Ee.length,ne=Ee.map(We=>z.map(ze=>ze.reduce((St,dt,Tn)=>pe(St,At(dt,Oe.x[Tn][We])),[0,0]))),ye=Ee.map(We=>Ee.map(ze=>[Oe.x.reduce((St,dt)=>St+dt[We]*dt[ze],0),0])),me=hh(ye),Se=tt(xe(ne)),Ke=jr(Se,me),$e=zr(Ke)[0],st=NaN,ut=NaN,Ot=NaN,Xt=NaN,Qn=!1;if(Te>=2){let We=je[Ae],ze=af(Fc(me)).map(Qt=>Qt.map(an=>[an,0])),St=Fc(jr(jr(ze,Se),fh(ze))),dt=Ut(St,{symmetric:!0,onlyValues:!0}).values,Tn=dt.reduce((Qt,an)=>Math.max(Qt,an),0),jt=Math.max(Tn*Wb,H*vh),It=zr(Ke),ht=zr(jr(Ke,Ke));Tn>H*vh&&ht[0]>0&&Number.isFinite(ht[0])?(Qn=!0,st=ot(Ce(It,It),At(ht,Te))[0],se-Te*st>0?ut=((se+1)*Te*st-2)/(Te*(se-Te*st)):l.push(`The Huynh-Feldt epsilon of ${We} is undefined (d - p GG <= 0)${a==="HF"?": corrected with GG":""}`)):l.push(`The error SSP matrix of ${We} is 0 (or at the rounding level of the data): no sphericity corrections (the p-values are uncorrected)`),Qn&&dt.every(Qt=>Qt>jt)?(Ot=Dc(jr(me,Se)),Xt=Pc(Ot,Te,se,P)):Qn&&l.push(`The error SSP matrix of ${We} is singular: no Mauchly test (the epsilons are still defined)`)}for(let We=0;We<Me.length;We++){let ze=Me[We],St;if(s===3)St=Fe(de(Me.filter(Ft=>Ft!==ze)),de([ze]),ne);else if(ze===0)St=Fe([],de([0]),ne);else{let Ft=ce(ze),nn=Me.filter(G=>G!==0&&G!==ze&&Ft.every(le=>W.factors[le][G-1]>0));St=Fe(de(Me.filter(G=>G!==ze&&!nn.includes(G))),de([ze]),ne)}let dt=zr(jr(tt(St),me))[0],jt=(ze===0?1:de([ze]).length)*Te,It=se*Te,ht=dt/jt/($e/It),Qt=pt(ht,jt,It,!1,!1),an=Ae===0?Ne[We]:ze===0?je[Ae]:`${Ne[We]}:${je[Ae]}`;Ue.push([k(dt),jt,k($e),It,ht,Qt]),Qe.push([dt,jt,$e,It,ht,Qt]),ge.push(an),nt.set(Ae,$e);let tn={name:an,ss:dt,numDf:jt,errSS:$e,denDf:It,f:ht,p:Qt};if(Qn){let Ft=Math.min(1,ut),nn=pt(ht,st*jt,st*It,!1,!1),G=pt(ht,Ft*jt,Ft*It,!1,!1);oe.push([st,nn,ut,G]),ve.push([Ot,Xt]),be.push(an),a==="GG"?tn={...tn,numDf:st*jt,denDf:st*It,p:nn}:a==="HF"&&(ut>1&&(X=!0),tn=Number.isNaN(ut)?{...tn,numDf:st*jt,denDf:st*It,p:nn}:{...tn,numDf:Ft*jt,denDf:Ft*It,p:G})}Ae===0&&ze===0||fe.push(tn)}}X&&l.push("HF eps > 1 treated as 1");let he={anova:Mh(fe,nt,c,i,k),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:Qe}),he}}var Hb=["tau","covariates","covariateNames","confLevel"];function as(e){return 2*ae(-Math.abs(e))}function Rh(e,t,n){return[e-n*t,e+n*t]}function Eh(e,t,n){return{estimate:e,se:t,confInt:Rh(e,t,n)}}function Yb(e,t,n,r){return{estimate:e,confInt:Rh(e,t,n),pValue:as(r)}}function xh(e,t){let n=dr(e.map(l=>l.time),e.map(l=>l.status),{rmean:t}),r=n.rmean[0],o=n.seRmean[0],s=0,a=1,c=$n();for(let l=0;l<n.time.length&&n.time[l]<t;l++){let f=n.time[l];wn(c,(1-a)*(f-s),n.time.length+1),a=n.surv[l],s=f}wn(c,(1-a)*(t-s),n.time.length+1);let i=jn(c);return{rmst:{estimate:r,se:o},rmtl:{estimate:i,se:o}}}function $c(e,t,n,r){if(n==="difference"){let l=e.estimate-t.estimate,f=Math.hypot(e.se,t.se);return Yb(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 a=o/s,c=Math.log(a),i=Math.hypot(e.se/o,t.se/s);return{estimate:a,confInt:[Math.exp(c-r*i),Math.exp(c+r*i)],pValue:as(c/i)}}function Gb(e){let t=new Array(e.length).fill(0);for(let n of[0,1]){let r=e.flatMap((s,a)=>s.arm===n?[a]:[]),o=dr(r.map(s=>e[s].time),r.map(s=>e[s].status===0?1:0),{rmean:"none"});for(let s of r){let a=e[s];if(a.status===0)continue;let c=1;for(let i=0;i<o.time.length&&o.time[i]<=a.time;i++)c=o.surv[i];if(!(c>0))throw new Error("the censoring survival probability is zero");t[s]=1/c}}return t}function jc(e,t,n,r,o,s){let a=e.map(_=>[_.arm,..._.x]),c=a[0].map((_,I)=>{let O=a.reduce((D,S)=>Math.max(D,Math.abs(S[I])),0);if(O===0)return 1;let L=Math.floor(Math.log2(O));return L>500||L<-500?2**L:1}),i=a.map(_=>_.map((I,O)=>I/c[O])),l=e.map((_,I)=>[1,...i[I]]),f=[1,...c],m=Gb(e),u=r?sr(i,n,{family:"poisson",weights:m}):Or(i,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(O=>m[I]*O*(n[I]-g[I]))),v=d.map(_=>_.slice());for(let _ of[0,1]){let I=e.flatMap((L,D)=>L.arm===_?[D]:[]),O=dr(I.map(L=>e[L].time),I.map(L=>e[L].status===0?1:0),{rmean:"none"});for(let L=0;L<O.time.length;L++){let D=O.time[L],S=O.nEvent[L];if(!(D<t)||S===0)continue;let F=O.nRisk[L];if(!(F>0))continue;let k=new Array(l[0].length).fill(0);for(let B=0;B<k.length;B++){let Q=$n();for(let re of I)e[re].time>=D&&wn(Q,d[re][B],I.length);k[B]=jn(Q)}let P=S/F;for(let B of I){let Q=e[B],re=Q.time===D&&Q.status===0,z=Q.time>=D;for(let ee=0;ee<k.length;ee++)v[B][ee]+=k[ee]/F*(Number(re)-(z?P:0))}}}let N=l.map((_,I)=>_.map(O=>O*Math.sqrt(r?g[I]:1))),b=ni(N),E=ni(v),w=rt(b),x=ri(ri(w,E),w).map((_,I)=>_.map((O,L)=>O/f[I]/f[L])),M=x.map((_,I)=>Math.sqrt(Math.max(0,_[I]))),R=p.map((_,I)=>_/M[I]),A=R.map(as),T=p.map((_,I)=>_-s*M[I]),q=p.map((_,I)=>_+s*M[I]),C=r?[T.map(Math.exp),q.map(Math.exp)]:[T,q];return{coef:p,cov:x,table:{names:["intercept","arm",...o],estimate:p,se:M,z:R,pValue:A,confInt:C}}}function Xb(e,t,n,r={}){if(K(r,Hb,"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,ee)=>Number.isFinite(ee)?Math.max(z,Math.abs(ee)):z,0),a=s>2**500?2**Math.floor(Math.log2(s)):1,i=e.every(z=>!Number.isFinite(z)||z===0||z/a!==0)&&(r.tau===void 0||r.tau===0||r.tau/a!==0)?a:1,l=z=>z/i,f=Array.from(t),m=new Set;for(let z=0;z<f.length;z++){let ee=n[z]===!0?1:n[z]===!1?0:n[z];(ee===0||ee===1)&&f[z]===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(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(ee=>typeof ee!="number")))throw new TypeError("'covariates' must contain numeric values");if(p&&p.some(z=>z.some(ee=>!Number.isFinite(ee)&&!Number.isNaN(ee))))throw new RangeError("'covariates' must be finite");let g=[],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 ee=n[z]===!0?1:n[z]===!1?0:n[z];if(ee!==0&&ee!==1){v.push(l(e[z]));continue}if(typeof f[z]!="number")throw new TypeError("'status' must contain numbers");let U=f[z],ue=U===1?1:0,Z=p?p[z]:[];if(Z.some(Y=>!Number.isFinite(Y)))continue;d.push({time:l(e[z]),status:ue,arm:ee,x:Z,index:z});let V=U;if(m.has(ee))if(U===2)V=1;else if(U===1)V=0;else{N.includes("Invalid status value, converted to NA")||N.push("Invalid status value, converted to NA");continue}else if(V!==0&&V!==1){N.includes("Invalid status value, converted to NA")||N.push("Invalid status value, converted to NA");continue}g.push({time:l(e[z]),status:V,arm:ee,x:Z,index:z})}if(d.some(z=>z.time<0))throw new Error("negative follow-up times are not supported");if(!g.some(z=>z.arm===0)||!g.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(ee=>ee.arm===z).map(ee=>ee.time))),E=[0,1].map(z=>{let ee=b[z];return d.some(U=>U.arm===z&&U.time===ee&&U.status===0)}),w=E.some(Boolean)?Math.min(...b.filter((z,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*i: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>w)throw new RangeError(`'tau' must be at most ${w*i}: 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,A=R<1e-8?ie(R,{lowerTail:!1}):ie(1-R),T=g.filter(z=>z.arm===1),q=g.filter(z=>z.arm===0),C=xh(T,x),_=xh(q,x),I=z=>({estimate:z.estimate*i,se:z.se*i}),O={rmst:I(C.rmst),rmtl:I(C.rmtl)},L={rmst:I(_.rmst),rmtl:I(_.rmtl)},D=Eh(O.rmst.estimate,O.rmst.se,A),S=Eh(L.rmst.estimate,L.rmst.se,A),F={estimate:O.rmtl.estimate,se:O.rmtl.se,confInt:[M-D.confInt[1],M-D.confInt[0]]},k={estimate:L.rmtl.estimate,se:L.rmtl.se,confInt:[M-S.confInt[1],M-S.confInt[0]]},P=$c(O.rmst,L.rmst,"difference",A),B=$c(O.rmst,L.rmst,"ratio",A),Q=$c(O.rmtl,L.rmtl,"rmtlRatio",A),re={tau:M,tauDefault:y,confLevel:o,arm1:{rmst:D,rmtl:F},arm0:{rmst:S,rmtl:k},difference:P,ratio:B,rmtlRatio:Q,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(de=>typeof de!="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 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=jc(ee,x,U,!1,z,A),V=!(O.rmst.estimate>0&&L.rmst.estimate>0),Y=!(O.rmtl.estimate>0&&L.rmtl.estimate>0),j=V?void 0:jc(ee,x,U,!0,z,A),H=Y?void 0:jc(ee,x,ue,!0,z,A),W=(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-A*we),Math.exp(qe+A*we)],pValue:as(qe/we)}:{estimate:qe*i,confInt:[(qe-A*we)*i,(qe+A*we)*i],pValue:as(qe/we)}},te=de=>({...de,estimate:de.estimate.map(ce=>ce*i),se:de.se.map(ce=>ce*i),confInt:[de.confInt[0].map(ce=>ce*i),de.confInt[1].map(ce=>ce*i)]}),se=de=>({...de,estimate:de.estimate.map((ce,qe)=>qe===0?ce+Math.log(i):ce),confInt:[de.confInt[0].map((ce,qe)=>qe===0?ce+Math.log(i):ce),de.confInt[1].map((ce,qe)=>qe===0?ce+Math.log(i):ce)]}),Me={names:["intercept","arm",...z],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:W(Z,!1),ratio:V?Ne:W(j,!0),rmtlRatio:Y?Ne:W(H,!0)},re.coefficients={difference:te(Z.table),ratio:j?se(j.table):Me,rmtlRatio:H?se(H.table):Me},delete re.arm1,delete re.arm0}return J("rmst2",re)}var Qb=["method","confLevel","control"],Zb=["eps","iterMax"];function zc(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:a,cases:c}of e){let i=a.length;if(c===0||c===i)continue;let l=a.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<i;d++){let v=a[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],A=h[b],T=p[b];m[b]=m[b]*w+y*x;for(let q=0;q<n;q++){let C=A[q];A[q]=C*w+y*(v[q]*x+M[q]);for(let _=0;_<n;_++)T[q][_]=T[q][_]*w+y*(v[q]*v[_]*x+v[q]*M[_]+v[_]*M[q]+R[q][_])}if(u[b]=E,m[b]>2**256||m[b]>0&&m[b]<2**-256){let q=Math.floor(Math.log2(m[b])),C=2**-q;m[b]=m[b]*C;for(let _=0;_<n;_++){A[_]=A[_]*C;for(let I=0;I<n;I++)T[_][I]=T[_][I]*C}u[b]=E+q}}}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+=a[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 Kc(e,t){return{statistic:e,df:t,pValue:Nt(e,t,{lowerTail:!1})}}function Jb(e,t,n,r={}){K(r,Qb,"clogit"),K(_t(r,"control"),Zb,"clogit","control");let{method:o="exact",confLevel:s=.95,control:a={}}=r,{eps:c=1e-9,iterMax:i=20}=a,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((F,k)=>{if(F!==0&&F!==1)throw new RangeError(`'y' must be 0 or 1 (element ${k+1})`)});let f=t[0]?.length??0;if(f===0||t.some(F=>F.length!==f||F.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=(F,k,P,B,Q,re,z,ee)=>{let U=k.map((Z,V)=>Math.sqrt(Z[V])),ue=F.map((Z,V)=>Z/U[V]);return{coefficients:F,var:k,loglik:P,score:B,wald:Q,iter:re,n:l,nevent:e.reduce((Z,V)=>Z+V,0),method:z,se:U,z:ue,p:ue.map(Z=>Nt(Z*Z,1,{lowerTail:!1})),lower:F.map((Z,V)=>Math.exp(Z-m*U[V])),upper:F.map((Z,V)=>Math.exp(Z+m*U[V])),logtest:Kc(2*(P[1]-P[0]),f),sctest:Kc(B,f),waldtest:Kc(Q,f),warnings:ee}};if(o!=="exact"){let F=fo(new Array(l).fill(1),e,t,{ties:"breslow",strata:n,confLevel:s,control:{eps:c,iterMax:i}});return u(F.coefficients,F.var,F.loglik,F.score,F.wald,F.iter,"breslow",F.warnings)}let h=Array.from({length:f},(F,k)=>t.reduce((P,B)=>P+B[k],0)/l),p=new Map;for(let F=0;F<l;F++){let k=p.get(n[F]);k===void 0&&(k={cases:[],controls:[]},p.set(n[F],k)),(e[F]===1?k.cases:k.controls).push(t[F].map((P,B)=>P-h[B]))}let g=[...p.values()].map(F=>({x:[...F.cases,...F.controls],cases:F.cases.length})),d=2**-39,v=F=>{let k=F.map(P=>P.slice());return{f:k,rank:Pr(k,d)}},N=new Array(f).fill(0),b=zc(g,N),E=[b.loglik,b.loglik],{f:w}=v(b.info),y=Vr(w,b.score),x=b.score.reduce((F,k,P)=>F+k*y[P],0),M=[],R=[N],A=b.loglik,T=N.map((F,k)=>F+y[k]),q=0,C=!1,_=0;for(_=1;_<=i;_++){b=zc(g,T);let F=Number.isFinite(b.loglik)&&b.score.every(Number.isFinite);if(F&&Math.abs(1-A/b.loglik)<=c){C=!0,N=T;break}!F||b.loglik<A?(q++,T=T.map((k,P)=>(k+q*N[P])/(q+1)),R.push(N)):(q=0,A=b.loglik,{f:w}=v(b.info),y=Vr(w,b.score),N=T,R.push(N),T=T.map((k,P)=>k+y[P]))}C||(_=i,N=R[Math.max(1,i-1)]??N,M.push("Ran out of iterations and did not converge")),b=zc(g,N),E[1]=b.loglik;let I=v(b.info),O=lo(I.f),L=N.map((F,k)=>I.rank<f&&O[k][k]===0?NaN:F),D=L.map((F,k)=>Number.isNaN(F)?-1:k).filter(F=>F>=0),S=0;for(let F of D)for(let k of D)S+=N[F]*b.info[F][k]*N[k];return u(L,O,E,x,S,_,"exact",M)}var Ah=["transform","terms","collapseTerms","global"],Gn=e=>new Array(e).fill(0),br=e=>Array.from({length:e},()=>Gn(e));function ev(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 a=r;a<o;a++)n[t[a]]=s;r=o}return n}function Th(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 tv(e){let t=e.time.length,n=new Array(t),r=new Map;for(let i=0;i<t;i++){let l=e.time[i],f=r.get(l)??{rows:[],deaths:0};f.rows.push(i),e.status[i]&&f.deaths++,r.set(l,f)}let o=[...r.keys()].sort((i,l)=>i-l),s=e.time.slice().sort((i,l)=>i-l),a=(e.start??new Array(t).fill(-1/0)).slice().sort((i,l)=>i-l),c=1;for(let i of o){let l=r.get(i);for(let m of l.rows)n[m]=1-c;let f=Th(a,i)-Th(s,i);l.deaths>0&&(c*=1-l.deaths/f)}return n}function nv(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=tv(e);else if(t==="rank")o=ev(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 rv(e,t={}){K(t,Ah,"coxZph"),K(t,Ah,"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 a=e.coefficients.map((V,Y)=>Number.isNaN(V)?-1:Y).filter(V=>V>=0),c=a.length;if(c===0)throw new Error("the proportional-hazards test has no estimable coefficients");let i=[];for(let V=0;V<s.sortedTime.length;){let Y=V;for(;!s.stratumEnd[Y];)Y++;for(let j=V;j<=Y;){let H=s.sortedTime[j],W=j;for(;W<Y&&s.sortedTime[W+1]===H;)W++;let te=[];for(let se=j;se<=W;se++)s.sortedEvent[se]&&te.push(se);s.start!==void 0&&te.reverse();for(let se of te)i.push(s.sortedToInput[se]);j=W+1}V=Y+1}if(i.length===0)throw new Error("the proportional-hazards test requires events");let l=i.map(V=>s.time[V]);if(new Set(l).size<2)throw new Error("the test needs events at two or more distinct times");let f=[...e.warnings],m=nv(s,n,i,f),u=m[i[0]];if(i.every(V=>m[V]===u))throw new Error("the transform is constant at the event times");let h=i.reduce((V,Y)=>Math.max(V,Math.abs(m[Y])),0),p=h===0?0:Math.floor(Math.log2(h)),g=m.map(V=>Number.isFinite(V)?$(V,-p):V),d=i.reduce((V,Y)=>V+g[Y],0)/i.length,v=g.map(V=>V-d);if(i.every(V=>v[V]===0))throw new Error("the transform is constant at the event times");let N=a.map(V=>s.betaScaled[V]),b=br(c),E=br(c),w=br(c),y=Gn(c),x=[];for(let V=0;V<s.sortedTime.length;){let Y=V;for(;!s.stratumEnd[Y];)Y++;for(let j=V;j<=Y;){let H=s.sortedTime[j],W=j;for(;W<Y&&s.sortedTime[W+1]===H;)W++;let te=[];for(let se=j;se<=W;se++)s.sortedEvent[se]&&te.push(se);if(te.length>0){let se=[],Me=-1/0;for(let oe=V;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][a[fe]];Me=Math.max(Me,be)}if(se.length===0||!Number.isFinite(Me))throw new Error("empty Cox proportional-hazards risk set");let Ne=Gn(c),de=br(c),ce=Gn(c),qe=br(c),we=0,Fe=0;for(let oe of se){let ve=0;for(let fe=0;fe<c;fe++)ve+=N[fe]*s.sortedX[oe][a[fe]];let be=s.sortedWeight[oe]*Math.exp(ve-Me);we+=be;for(let fe=0;fe<c;fe++){let nt=s.sortedX[oe][a[fe]];Ne[fe]+=be*nt;for(let X=0;X<c;X++)de[fe][X]+=be*nt*s.sortedX[oe][a[X]]}if(s.sortedEvent[oe]&&s.sortedTime[oe]===H){Fe+=be;for(let fe=0;fe<c;fe++){let nt=s.sortedX[oe][a[fe]];ce[fe]+=be*nt;for(let X=0;X<c;X++)qe[fe][X]+=be*nt*s.sortedX[oe][a[X]]}}}let xe=0,_e=Gn(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][a[be]]}let Oe=te.length,Be=s.ties==="efron"&&Oe>1?Oe:1,je=Gn(c),tt=br(c);for(let oe=0;oe<Be;oe++){let ve=oe/Be,be=we-ve*Fe,fe=Ne.map((X,he)=>(X-ve*ce[he])/be);for(let X=0;X<c;X++)je[X]+=fe[X]/Be;let nt=xe/Be;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*je[ve]),Qe=s.sortedToInput[te[0]],ge=v[Qe];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=a.map(fe=>s.sortedX[oe][fe]);for(let fe=0;fe<c;fe++)be[fe]-=je[fe];x.push({input:ve,time:H,stratum:s.stratumIndex[oe],g:m[ve],residual:be})}}j=W+1}V=Y+1}let M;try{M=rt(b,E)}catch{throw new Error("the proportional-hazards information is singular")}let R=br(c);for(let V=0;V<c;V++)for(let Y=0;Y<c;Y++){let j=0;for(let H=0;H<c;H++)j+=E[H][V]*M[H][Y];R[V][Y]=w[V][Y]-j}for(let V=0;V<c;V++)for(let Y=0;Y<V;Y++){let j=(R[V][Y]+R[Y][V])/2;R[V][Y]=j,R[Y][V]=j}let A=t.terms===!1||t.terms===void 0?a.map(V=>[V]):t.terms,T=new Set,q=[],C=[];if(A.forEach((V,Y)=>{if(!Array.isArray(V)||V.length===0)throw new RangeError("each terms group must be a non-empty array");let j=[];for(let H of V){if(!Number.isInteger(H)||H<0||H>=e.coefficients.length)throw new RangeError("terms column indices must be zero-based and within x");if(T.has(H))throw new RangeError("terms groups must not overlap");T.add(H),a.includes(H)&&j.push(H)}j.length>0&&(q.push(j),C.push(Y))}),a.forEach(V=>{T.has(V)||(q.push([V]),C.push(A.length))}),q.length===0)throw new Error("the proportional-hazards test has no estimable terms");let _=[];q.forEach((V,Y)=>{if(o&&V.length>1)_.push({col:-1,group:Y,collapsed:!0});else for(let j of V)_.push({col:j,group:Y,collapsed:!1})});let I=_.length,O=Array.from({length:c},()=>Gn(I)),L=[],D=[];_.forEach((V,Y)=>{let j=q[V.group],H=C[V.group];if(V.collapsed){for(let W of j){let te=a.indexOf(W);O[te][Y]=N[te]}L.push(1),D.push(`g${H+1}`)}else{let W=a.indexOf(V.col);O[W][Y]=s.inverseCovariateScale[V.col],L.push(e.coefficients[V.col]),D.push(j.length>1?`g${H+1}x${V.col+1}`:`x${V.col+1}`)}});let S=Array.from({length:I},(V,Y)=>{let j=0;for(let H=0;H<c;H++)j=Math.max(j,Math.abs(O[H][Y]));if(!(j>0&&Number.isFinite(j)))throw new Error("the proportional-hazards residual variance is singular");return Math.floor(Math.log2(j))}),F=O.map(V=>V.map((Y,j)=>$(Y,-S[j]))),k=Array.from({length:c},()=>Gn(I));for(let V=0;V<c;V++)for(let Y=0;Y<I;Y++)for(let j=0;j<c;j++)k[V][Y]+=b[V][j]*F[j][Y];let P=br(I);for(let V=0;V<I;V++)for(let Y=0;Y<I;Y++)for(let j=0;j<c;j++)P[V][Y]+=F[j][V]*k[j][Y];for(let V=0;V<I;V++)for(let Y=0;Y<V;Y++){let j=(P[V][Y]+P[Y][V])/2;P[V][Y]=j,P[Y][V]=j}let B;try{B=rt(P)}catch{throw new Error("the proportional-hazards residual variance is singular")}for(let V=0;V<I;V++)for(let Y=0;Y<V;Y++){let j=(B[V][Y]+B[Y][V])/2;B[V][Y]=j,B[Y][V]=j}let Q=B.map((V,Y)=>V.map((j,H)=>i.length*Ve(j,-S[Y]-S[H]-s.weightExp))),re=x.map(V=>{let Y=Gn(I);for(let j=0;j<I;j++)for(let H=0;H<c;H++)Y[j]+=F[H][j]*V.residual[H];return L.map((j,H)=>{let W=0;for(let te=0;te<I;te++)W+=Y[te]*B[te][H];return j+i.length*Ve(W,-S[H]-s.weightExp)})}),z=[],ee=[],U=[],ue=_.map(V=>V.collapsed?q[V.group].map(Y=>a.indexOf(Y)):[a.indexOf(V.col)]);for(let V of ue){let Y=V.map(te=>V.map(se=>R[te][se])),j=V.map(te=>y[te]),H;try{H=rt(Y,j)}catch{throw new Error("the proportional-hazards term information is singular")}let W=$(Math.max(0,j.reduce((te,se,Me)=>te+se*H[Me],0)),s.weightExp);z.push(W),ee.push(V.length),U.push(Nt(W,V.length,{lowerTail:!1}))}if(r){let V;try{V=rt(R,y)}catch{throw new Error("the global proportional-hazards information is singular")}let Y=$(Math.max(0,y.reduce((j,H,W)=>j+H*V[W],0)),s.weightExp);z.push(Y),ee.push(c),U.push(Nt(Y,c,{lowerTail:!1})),D.push("GLOBAL")}let Z={rowNames:D,chisq:z,df:ee,p:U,pValue:U,x:x.map(V=>V.g),time:x.map(V=>V.time),...s.strata===void 0?{}:{strata:x.map(V=>Mt(s.strata[V.input]))},y:re,var:Q,transform:n,warnings:f};return J("coxZph",Z,e)}var ov=["center"],sv=2**-1022;function av(e,t,n={}){K(n,ov,"leveneTest");let r=ke(n.center??"median",["median","mean"]),{groups:o}=or(e,t);if(o.length<2)throw new RangeError("contrasts can be applied only to factors with 2 or more levels");let s=l=>{let f=r==="median"?Ds(l):it(l);return l.map(m=>Math.abs(m-f))},a=Ie(o).groups.map(s),c=(l,f)=>l.some(m=>m>0&&m<sv)||l.every(m=>m===0)&&o[f].some(m=>m!==o[f][0]);if(a.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;a=l.map(u=>u.d.map(h=>$(h,u.k-m)))}let i=xu(Rr(a));return{colNames:["Df","F value","Pr(>F)"],rowNames:["group",""],table:[[i.df[0],i.fValue,i.pValue],[i.df[1],NaN,NaN]],warnings:[]}}var iv=["score"],cv=["alternative","exact","variant","levels"];function lv(e,t,...n){K(Cs(n),iv,"propTrendTest");let{score:r}=Ls(["score"],n),o=Array.from(e),s=Array.from(t),a=o.length,c=r===void 0?o.map((N,b)=>b+1):Array.from(r);if(s.length!==a||c.length!==a)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 i=s.reduce((N,b)=>N+b,0),l=o.reduce((N,b)=>N+b,0)/i,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}=Wc(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}=Wc(f,m,h,u))}let v=$t(g,d);if(!(i<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=Wc(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:vt(v,.5,2,!1,!1),warnings:[]}}function Wc(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,a=r.reduce((l,f)=>l+e[f]*t[f],0)/o,c=0,i=0;for(let l of r){let f=n[l]-s;c+=e[l]*f*(t[l]-a),i+=e[l]*f*f}return{sw:o,sxy:c,sxx:i}}var fv=2**24,mv=2**32,uv=`Sample size > 100 or data with ties
10
- p-value based on normal approximation. Specify nperm for permutation p-value`,hv=`Sample data with ties
11
- p-value based on normal approximation. Specify nperm for permutation p-value.`;function pv(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)),a=o.flatMap((f,m)=>s(f)||s(t[m])?[]:[m]);n===void 0&&(t=Vt(t));let c;if(n!==void 0)c=n.map(Mt);else{let f=[...new Set(a.map(m=>t[m]))];c=f.every(m=>typeof m=="number")?f.sort((m,u)=>m-u).map(Mt):[...new Set(f.map(Mt))].sort(kn)}let i=c.map(()=>[]),l=new Map(c.map((f,m)=>[f,m]));for(let f of a){let m=l.get(Mt(t[f]));if(m===void 0)throw new RangeError(`the group '${Mt(t[f])}' is not among 'levels'`);i[m].push(o[f])}return i}function dv(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),a=(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)),i=s(e,l=>l*(l-1))*s(r,l=>l*(l-1))/(8*o*(o-1));return a+c+i}function gv(e,t,n={}){K(n,cv,"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=pv(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 a=s.flat(),c=a.length,i=new Set(a).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=!i&&c<=100;o==="DescTools"&&n.exact!==void 0?(h=n.exact,h&&i&&(h=!1,u.push(hv))):h||u.push(uv);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>fv||b>mv)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,A)=>Pl(A,w,y,!1)),M=new Array(E.length+x.length-1).fill(0);E.forEach((R,A)=>x.forEach((T,q)=>M[A+q]+=R*T)),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=i?dv(s.map(w=>w.length),a):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 wv=["nClasses","adjust"];function is(e){return Pt(Array.from(e)).sort((t,n)=>t-n)}function Sh(e){if(e.length>0&&!(Number.isFinite(e[0])&&Number.isFinite(e[e.length-1])))throw new RangeError("'x' must be finite")}function xa(e){Sh(e);let t=ys(e);if(t.some(Number.isNaN))throw new RangeError("all 'x' values are identical");return t}function bv(e){let t=is(e),n=t.length;if(n<8)throw new RangeError("sample size must be greater than 7");let r=xa(t),o=0;for(let i=1;i<=n;i++)o+=(2*i-1)*(ae(r[i-1],{logP:!0})+ae(r[n-i],{lowerTail:!1,logP:!0}));let s=-n-o/n,a=(1+.75/n+2.25/n**2)*s,c;return a<.2?c=1-Math.exp(-13.436+101.14*a-223.73*a**2):a<.34?c=1-Math.exp(-8.318+42.796*a-59.938*a**2):a<.6?c=Math.exp(.9177-4.279*a-1.38*a**2):a<10?c=Math.exp(1.2937-5.709*a+.0186*a**2):c=37e-25,{method:"Anderson-Darling normality test",statistic:s,statisticName:"A",pValue:c,warnings:[]}}function vv(e){let t=is(e),n=t.length;if(n<8)throw new RangeError("sample size must be greater than 7");let r=xa(t),o=1/(12*n);r.forEach((i,l)=>{o+=(ae(i)-(2*(l+1)-1)/(2*n))**2});let s=(1+.5/n)*o,a=[],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,a.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:a})}function Nv(e){let t=is(e),n=t.length;if(n<5)throw new RangeError("sample size must be greater than 4");let r=xa(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 a=Math.max(o,s),c=n<=100?a:a*(n/100)**.49,i=n<=100?n:100,l=Math.exp(-7.01256*c**2*(i+2.78019)+2.99587*c*Math.sqrt(i+2.78019)-.122119+.974598/Math.sqrt(i)+1.67997/i);if(l>.1){let f=(Math.sqrt(n)-.01+.85/Math.sqrt(n))*a;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:a,statisticName:"D",pValue:l,warnings:[]}}function yv(e){let t=is(e),n=t.length;if(n<5||n>5e3)throw new RangeError("sample size must be between 5 and 5000");if(Sh(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(Ll(o,r)**2/(at(o)*at(r)),1),a=Math.log(n),c=Math.log(a),i=-1.2725+1.0521*(c-a),l=1.0308-.26758*(c+2/a),f=ae((Math.log(1-s)-i)/l,{lowerTail:!1});return{method:"Shapiro-Francia normality test",statistic:s,statisticName:"W",pValue:f,warnings:[]}}function Mv(e,t={}){K(t,wv,"pearsonTest");let n=is(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 a=[];for(let f of xa(n)){let m=Math.min(o,Math.floor(1+o*ae(f)));m>=1&&a.push(m)}a.sort((f,m)=>f-m);let c=r/o,i=0;for(let f=1,m=0;f<=o;f++){let u=0;for(;m<a.length&&a[m]===f;)u++,m++;i=i+(u-c)**2/c}let l=o-1-(s?2:0);return{method:"Pearson chi-square normality test",statistic:i,statisticName:"P",pValue:vt(i,l/2,2,!1,!1),warnings:[]}}var Ev=["weight"],xv=["exact"],Rv=["model","type","unit","r0","confLevel"];function cs(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 Ra(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 qh=e=>2*ae(-Math.abs(e));function Av(e,t={}){K(t,Ev,"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=>zn(x[0])),a=r.map(x=>zn(x[1])),c=[...new Set([...s,...a])].sort((x,M)=>x<M?-1:x>M?1:0),i=c.length,l=new Map(c.map((x,M)=>[x,M])),f=Array.from({length:i},()=>new Array(i).fill(0));for(let x=0;x<o;x++)f[l.get(s[x])][l.get(a[x])]++;let m=x=>n==="unweighted"?x===0?1:0:n==="equal"?1-x/(i-1):1-x*x/((i-1)*(i-1)),u=Array.from({length:i},(x,M)=>Array.from({length:i},(R,A)=>m(Math.abs(M-A)))),h=f.map(x=>Pe(x)/o),p=Array.from({length:i},(x,M)=>Pe(f.map(R=>R[M]))/o),g=x=>{let M=[];for(let R=0;R<i;R++)for(let A=0;A<i;A++)M.push(x(R,A));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:i},(x,M)=>Pe(p.map((R,A)=>R*u[M][A]))),E=Array.from({length:i},(x,M)=>Pe(h.map((R,A)=>R*u[A][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:qh(y),warnings:[]}}function Tv(e,t={}){K(t,xv,"kappamFleiss");let n=t.exact??!1,r=cs(e,"ratings");Ra(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 a=e.flat(),c=Vt(a),i=[...new Set(c)].sort((w,y)=>w-y),l=new Map(i.map((w,y)=>[w,y])),f=new Map(a.map((w,y)=>[w,l.get(c[y])])),m=i.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:qh(E),warnings:[]}}function Sv(e,t={}){K(t,Rv,"icc");let n=ke(t.model,["oneway","twoway"]),r=ke(t.type,["consistency","agreement"]),o=ke(t.unit,["single","average"]),s=t.r0??0,a=t.confLevel??.95;Lt(a),cs(e,"ratings"),Ra(e,"ratings");let c=Ie(e).groups,i=cs(c,"ratings"),l=c.length,f=i.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-a,u=at(c.map(F=>it(F)))*f,h=at(i.map(F=>it(F)))*l,p=it(c.map(F=>at(F))),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)):_v(c)/((l-1)*(f-1)),N=(F,k,P)=>pt(F,k,P,!1,!1),b=(F,k)=>m/2<1e-8?pn(m/2,F,k,!1,!1):pn(1-m/2,F,k,!0,!1),E=l-1;if(n==="oneway"||r==="consistency"){let F=n==="oneway"?p:v,k=n==="oneway"?l*(f-1):(l-1)*(f-1),P=u/F,B=P/b(E,k),Q=P*b(k,E),[re,z,ee]=o==="single"?[(u-F)/(u+(f-1)*F),P*(1-s)/(1+(f-1)*s),U=>(U-1)/(U+f-1)]:[(u-F)/u,P*(1-s),U=>1-1/U];return{value:re,Fvalue:z,fValue:z,df1:E,df2:k,pValue:N(z,E,k),lbound:ee(B),ubound:ee(Q),warnings:[]}}let w=o==="single"?f:1,y=F=>[w*F/(l*(1-F)),1+w*F*(l-1)/(l*(1-F))],x=(F,k)=>Cl([F*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,A]=y(s),T=u/(R*h+A*v),q=x(R,A),[C,_]=[f*M/(l*(1-M)),1+f*M*(l-1)/(l*(1-M))],I=x(C,_),O=b(E,I),L=b(I,E),[D,S]=o==="single"?[l*(u-O*v)/(O*(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-O*v)/(O*(h-v)+l*u),l*(L*u-v)/(h-v+l*L*u)];return{value:M,Fvalue:T,fValue:T,df1:E,df2:q,pValue:N(T,E,q),lbound:D,ubound:S,warnings:[]}}function kh(e,t){let n=t.length,r=Pe(t.flatMap(s=>t.map(a=>e[s][a])));return(1-Pe(t.map(s=>e[s][s]))/r)*n/(n-1)}function kv(e){let t=cs(e,"x").length;if(Ra(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=Vs(Ie(e).groups),r=Do(e),o=Array.from({length:t},(c,i)=>i),s=Pe(r.flat()),a;try{let c=rt(r);a=o.map(i=>1-1/c[i][i])}catch(c){if(!r.flat().every(Number.isFinite))throw c;let i=Ie(e).groups;a=o.map(l=>{let f=i.map(h=>h[l]),m=eo(Ct(i.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:kh(n,o),stdAlpha:(1-t/s)*t/(t-1),G6:1-(t-Pe(a))/s,averageR:(s-t)/(t*(t-1)),alphaDrop:o.map(c=>kh(n,o.filter(i=>i!==c))),warnings:qv(e,r)})}function qv(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(i=>t[c][i])),{symmetric:!0}),s=o.map(c=>c[0]),a=Pe(s)<0?-1:1;s.some(c=>a*c<0)&&n.push(`Some items were negatively correlated with the first principal component and probably
6
+ k = 3 groups are required.`);let i=s.n[0],c=s.n.slice(1).map((m,u)=>u+1),a=c.map(m=>{let u=s.n[m],h=[...s.groups[0],...s.groups[m]],p=bt(h),d=0;for(let x=0;x<i;x++)d+=p[x];let g=i+u,w=g*(g+1)/2-d,N=i*u+i*(i+1)/2-d,v=i*u+u*(u+1)/2-w,E=0;for(let x of pn(h))E+=x*x*x-x;let b=i*u/(g*(g-1))*((g*g*g-g)/12-E/12),M=(Math.min(N,v)-i*u/2)/Math.sqrt(b);return d<w?-M:M}),l=c.map(m=>s.n[m]/(s.n[m]+i)),f=Bf(a,l,r,o);return H("manyOneUTest",bi(s,`Wilcoxon, Mann, Whittney U-test
7
+ for multiple comparisons with one control`,a,f,o))}function q0(e,t){let n=Sn(e,t),r=Rr(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 w=0;w<c;w++)m+=f[w];let u=m-c*(c+1)/2,h=Math.min(u,c*a-u),p=c+a,d=0;for(let w of pn(l))d+=w*w*w-w;let g=c*a/(p*(p-1))*((p*p*p-p)/12-d/12);return Math.SQRT2*(h-c*a/2)/Math.sqrt(g)}),o=r.map(s=>s.map(i=>Number.isNaN(i)?NaN:vi(i,n.k,1/0)));return H("dscfAllPairsTest",Lo(n,"Dwass-Steele-Critchlow-Fligner all-pairs test",r,o,"single-step"))}function I0(e,t,n={}){V(n,x0,"tamhaneT2Test");let r=(()=>{if(t!==void 0)return Sn(e,t);let f=Zn(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=Et(r.groups),s=Et(o.groups),i=n.welch??!0,c=r.k*(r.k-1)/2,a=Rr(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=Rr(r.k,(f,m)=>{let u=a[f-1][m];if(Number.isNaN(u))return NaN;let h=i?pa([s.variance[f],s.variance[m]],[r.n[f],r.n[m]]):r.n[f]+r.n[m]-2,p=2*dn(Math.abs(u),h,{lowerTail:!1});return p>=1?1:-Math.expm1(c*Math.log1p(-p))});return H("tamhaneT2Test",Lo(r,i?"Tamhane's T2-test for unequal variances":"Tamhane's T2'-test for unequal variances",a,l,i?"T2 (Sidak)":"T2' (Sidak)"))}function _0(e,t){let n=Sn(e,t),r=Et(n.groups),o=r.mean,s=r.variance.map((a,l)=>a/n.n[l]),i=Rr(n.k,(a,l)=>(o[a]-o[l])/Math.sqrt(s[a]+s[l])*Math.SQRT2),c=Rr(n.k,(a,l)=>{let f=pa([r.variance[a],r.variance[l]],[n.n[a],n.n[l]]);return vi(i[a-1][l],n.k,f)});return H("gamesHowellTest",Lo(n,"Games-Howell test",i,c,"none"))}function L0(e,t){let n=Sn(e,t),{m:r,s2:o,df:s}=Ni(n);if(s<2)throw new RangeError("the studentized range needs at least 2 residual degrees of freedom");let i=Rr(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 H("tukeyTest",Lo(n,"Tukey's test",i,c,"single-step"))}function C0(e,t,n={}){V(n,M0,"dunnTest");let r=n.method??"none",o=n.altp??!1,s=Sn(e,t),i=Vs(e,t).statistic,{R:c,C:a}=wi(s),l=s.N*(s.N+1)/12-a,f=[],m=[];for(let p=1;p<s.k;p++)for(let d=0;d<p;d++)f.push((c[d]-c[p])/Math.sqrt(l*(1/s.n[d]+1/s.n[p]))),m.push(`${s.labels[d]} - ${s.labels[p]}`);let u=f.map(p=>(o?2:1)*$s(Math.abs(p))),h=O0(u,r);return{chi2:i,Z:f,P:u,PAdjusted:h,zValue:f,pValue:u,pAdjusted:h,comparisons:m,warnings:[]}}function O0(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 F0(e,t,n={}){V(n,E0,"glhtDunnett");let r=Ge(n.alternative),o=Sn(e,t),{m:s,s2:i,df:c,k:a}=Ni(o),l=Math.sqrt(i),f=o.n.slice(1).map((N,v)=>v+1),m=f.map(N=>s[N]-s[0]),u=f.map(N=>l*Math.sqrt(1/o.n[N]+1/o.n[0])),h=m.map((N,v)=>N/u[v]),p=m.map(N=>$(N,a)),d=u.map(N=>$(N,a)),g=f.map(N=>o.n[N]/(o.n[N]+o.n[0])),w=h.map(N=>r==="two.sided"?xr(N,g,c,!0):xr(r==="greater"?N:-N,g,c,!1));return{names:f.map(N=>`${o.labels[N]} - ${o.labels[0]}`),estimate:p,se:d,t:h,p:w,stdError:d,tValue:h,pValue:w,df:c,warnings:[]}}var Uf=["varEqual"],D0=2**-1022;function Hf(e,t,n={}){if(typeof e!="string"&&at(t)&&V(t,Uf,"onewayTest"),V(n,Uf,"onewayTest"),typeof e=="string"){let g=tl(e,t);return Hf(g.values,g.groups,n)}let r;t!==void 0&&!Array.isArray(t)?n=t:r=t;let o=dr(e,r),s=Et(o),i=s.groups,c=n.varEqual??!1,a=i.length;if(a<2)throw new RangeError("not enough groups");let l=i.map(g=>g.length);if(l.some(g=>g<2))throw new RangeError("not enough observations");let f=s.mean,m=s.variance,u=l.reduce((g,w)=>g+w,0),h,p,d;if(c){let g=s.grandMean,w=0,N=0;for(let v=0;v<a;v++)w+=l[v]*(f[v]-g)**2,N+=(l[v]-1)*m[v];p=u-a,Co(p,s.ssWithin),h=w/(a-1)/(N/p)}else if(m.some(g=>!(g>=D0))&&(d=V0(o))!==void 0){let g=d.map((y,R)=>Gf(l[R]/y.variance,-2*y.k)),w=Math.max(...g.map(y=>y.e)),N=g.map(y=>et(y.m,y.e-w)),v=N.reduce((y,R)=>y+R,0),E=0;for(let y=0;y<a;y++){let R=N.reduce((T,S,k)=>k===y?T:T+S,0);E+=(R/v)**2/(l[y]-1)}E/=a*a-1;let b=[];for(let y=0;y<a;y++)for(let R=y+1;R<a;R++){let[T,S]=d[y].k>=d[R].k?[d[y],d[R]]:[d[R],d[y]],k=T.mean-et(S.mean,S.k-T.k);if(k===0)continue;let L=Gf(k,T.k);b.push({m:g[y].m*g[R].m*L.m*L.m/v,e:g[y].e+g[R].e+2*L.e-w})}let M=Math.max(...b.map(y=>y.e)),x=b.reduce((y,R)=>y+et(R.m,R.e-M),0);h=(b.length===0?0:et(x,M))/((a-1)*(1+2*(a-2)*E)),p=1/(3*E)}else if(m.some(g=>g===0)){let g=m.flatMap((E,b)=>E===0?[b]:[]);if(g.length>1)throw new RangeError("Welch's test is not defined with two or more constant groups");let w=g[0],N=0,v=0;for(let E=0;E<a;E++)E!==w&&(N+=1/(l[E]-1),v+=l[E]/m[E]*(f[E]-f[w])**2);N/=a*a-1,h=v/((a-1)*(1+2*(a-2)*N)),p=1/(3*N)}else{let g=l.map((b,M)=>b/m[M]),w=g.reduce((b,M)=>b+M,0),N=0,v=0;for(let b=0;b<a;b++)N+=(1-g[b]/w)**2/(l[b]-1),v+=g[b]*f[b];N/=a*a-1,v/=w;let E=0;for(let b=0;b<a;b++)E+=g[b]*(f[b]-v)**2;h=E/((a-1)*(1+2*(a-2)*N)),p=1/(3*N)}return{method:c?"One-way analysis of means":"One-way analysis of means (not assuming equal variances)",statistic:h,statisticName:"F",parameter:[a-1,p],parameterName:["num df","denom df"],pValue:wt(h,a-1,p,!1,!1),warnings:[]}}function P0(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=Pf(t);return{method:"Shapiro-Wilk normality test",statistic:1-r,statisticName:"W",pValue:Vf(r,n),warnings:[]}}function V0(e){let t=[];for(let n of e){let r=Et([n]),o=r.variance[0];if(!(o>0))return;t.push({mean:r.mean[0],variance:o,k:r.k})}return t}function Gf(e,t){let n=Math.floor(Math.log2(Math.abs(e))),r=et(e,-n);return Math.abs(r)>=2?(r/=2,n++):Math.abs(r)<1&&(r*=2,n--),{m:r,e:t+n}}function yi(e,t,n){let r=[0,0];for(let o=Math.max(0,t);o<Math.min(n,e.length);o++)r=ue(r,[e[o],0]);return r[0]}function $0([[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]=gn(f,o,s,i,!1);return{lo:c,d:l}}function z0(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 zs=class{lo;hi;logNull;constructor(t){let n=0,r=new Float64Array([1]);for(let o of t){let s=$0(o);n+=s.lo,r=z0(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=yi(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=ue(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?yi(o,s,o.length):yi(o,0,s+1)}};function Mi(e,t){return tr(e,t)}function Xf(e,t){if(t===e.lo)return 0;if(t===e.hi)return 1/0;let n=e.mean(1);return n===t?1:Mi(r=>e.mean(r)-t,n>t)}function Ei(e,t,n){if(t===e.hi)return 1/0;let r=e.cdf(t,1,!1);return r===n?1:Mi(o=>n-e.cdf(t,o,!1),r<n)}function xi(e,t,n){if(t===e.lo)return 0;let r=e.cdf(t,1,!0);return r===n?1:Mi(o=>e.cdf(t,o,!0)-n,r>n)}var j0=["alternative","correct","exact","confLevel"];function K0(e,t={}){V(t,j0,"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(Ri);if(s.some(f=>f<2))throw new RangeError("sample size in each stratum must be > 1");let i=Ge(t.alternative),c=t.correct??!0,a=t.exact??!1,l=t.confLevel??.95;if(It(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?B0(f,i,l):W0(f,s,i,c,l)}return U0(e,s,r,o)}function Ri(e){return e.reduce((t,n)=>t+n.reduce((r,o)=>r+o,0),0)}function Jf(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 em(e,t){return e.map(n=>n.map(r=>r.map(o=>$(o,-t))))}function Qf(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]],d)=>{let g=t[d],w=m+u,N=h+p,v=m+h,E=u+p;r+=m;let b=w*v/g;o+=Number.isFinite(b)?b:w/g*v;let M=w*N*v*E/(g*g*(g-n));s+=Number.isFinite(M)?M:w/g*(N/g)*(v/(g-n))*E;let x=(m+p)/g,y=(u+h)/g,R=m*p/g,T=u*h/g,S=Number.isFinite(R)?R:m/g*p,k=Number.isFinite(T)?T:u/g*h;i+=S,c+=k,a+=x*S,l+=x*k+y*S,f+=y*k}),{sa:r,se:o,sv:s,sR:i,sS:c,sPR:a,sPSQR:l,sQS:f}}function W0(e,t,n,r,o){let s=Qf(e,t,1),i=0;if(!t.every(Number.isFinite)||!Object.values(s).every(Number.isFinite)){i=Jf(e);let x=em(e,i);s=Qf(x,x.map(Ri),$(1,-i))}let{sa:c,se:a,sv:l,sR:f,sS:m,sPR:u,sPSQR:h,sQS:p}=s,d=c-a,g=$(.5,-i),w=r&&Math.abs(d)>=g?g:0,N=$(Dt(Math.abs(d)-w,l),i),v=n==="two.sided"?gt(N,.5,2,!1,!1):ie(Math.sign(d)*Math.sqrt(N),{lowerTail:n==="less"}),E=f/m,b=$(Math.sqrt(u/(2*f*f)+h/(2*f*m)+p/(2*m*m)),-i/2),M;if(n==="less")M=[0,E*Math.exp(oe(o)*b)];else if(n==="greater")M=[E*Math.exp(oe(o,{lowerTail:!1})*b),1/0];else{let x=(1-o)/2,y=x<1e-8?oe(x,{lowerTail:!1}):oe((1+o)/2);M=[E*Math.exp(-y*b),E*Math.exp(y*b)]}return{method:`Mantel-Haenszel chi-squared test ${w!==0?"with":"without"} continuity correction`,alternative:n,statistic:N,statisticName:"Mantel-Haenszel X-squared",parameter:1,parameterName:"df",pValue:v,confInt:M,confLevel:o,estimate:E,estimateNames:"common odds ratio",nullValue:1,nullValueName:"common odds ratio",warnings:[]}}var Y0=2**24;function B0(e,t,n){let r=e.reduce((a,[[l,f],[m,u]])=>a+Math.min(l,u)+Math.min(f,m),1);if(r>Y0)throw new RangeError(`the exact distribution of S has ${r} values, more than 2^24: use exact: false`);let o=new zs(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=ue(f,[m,0]));i=Math.min(1,f[0])}else i=o.cdf(s,1,t==="greater");let c;if(t==="less")c=[0,Ei(o,s,1-n)];else if(t==="greater")c=[xi(o,s,1-n),1/0];else{let a=(1-n)/2;c=[xi(o,s,a),Ei(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:Xf(o,s),estimateNames:"common odds ratio",nullValue:1,nullValueName:"common odds ratio",warnings:[]}}function U0(e,t,n,r){let{diff:o,V:s}=Zf(e,t,n,r,1),i=0;if(!t.every(Number.isFinite)||!o.every(Number.isFinite)||!s.every(f=>f.every(Number.isFinite))){i=Jf(e);let f=em(e,i);({diff:o,V:s}=Zf(f,f.map(Ri),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:gt(l,c/2,2,!1,!1),warnings:[]}}var G0=2**-1022;function Zf(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((d,g)=>d+g,0)),u=Array.from({length:r},(p,d)=>a.reduce((g,w)=>g+w[d],0)),h=1/(f*f*(f-o));for(let p=0;p<r-1;p++)for(let d=0;d<n-1;d++){let g=d+(n-1)*p,w=m[d]*u[p]/f;i[g]+=a[d][p]-(Number.isFinite(w)?w:os(m[d],u[p],f));for(let N=0;N<r-1;N++)for(let v=0;v<n-1;v++){let E=(d===v?f*m[d]:0)-m[d]*m[v],b=(p===N?f*u[p]:0)-u[p]*u[N],M=h*E*b;c[g][v+(n-1)*N]+=Number.isFinite(M)&&h>=G0?M:m[d]*((d===v?1:0)-m[v]/f)*(((p===N?1:0)-u[N]/f)*(u[p]/(f-o)))}}}),{diff:i,V:c}}var tm=new Map,H0=2**24,X0=2**32;function rm(e,t){let n=e+t,r=(e+1)*(Math.floor((n+1)**2/4)+1);if(2*r>H0||n*r>X0)throw new RangeError(`the exact distribution for m = ${e} and n = ${t} is too large to compute: use exact: false`)}function Q0(e,t){let n=`${e},${t}`,r=tm.get(n);if(r!==void 0)return r;let o=e+t,s=[];for(let u=1;u<=o;u++)s.push(Math.min(u,o+1-u));let i=s.reduce((u,h)=>u+h,0),c=Array.from({length:e+1},()=>new Float64Array(i+1));c[0][0]=1;for(let u=0;u<o;u++){let h=o-u,p=s[u],d=Array.from({length:e+1},()=>new Float64Array(i+1));for(let g=0;g<=e;g++){let w=c[g],N=e-g;if(N>h)continue;let v=N/h;for(let E=0;E<=i;E++){let b=w[E];b!==0&&(N>0&&(d[g+1][E+p]+=b*v),N<h&&(d[g][E]+=b*(1-v)))}}c=d}let a=c[e],l=0;for(;l<a.length&&a[l]===0;)l++;let f=a.length-1;for(;f>l&&a[f]===0;)f--;let m={min:l,probs:a.slice(l,f+1)};return tm.set(n,m),m}function Ti(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}=Q0(t,n),i=Oc(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 nm=new Map;function Z0(e,t){let n=`${e},${t}`,r=nm.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 d=i[p];for(let g=0;g<=s;g++){let w=d[g];w!==0&&(p<e&&(h[p+1][g+u]+=w),e-p<o-m&&(h[p][g]+=w))}}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 nm.set(n,f),f}function js(e,t,n){let{min:r,counts:o}=Z0(t,n);if(e<=0)return r;if(e>=1)return r+o.length-1;let s=zc(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 J0=["alternative","exact","confInt","confLevel"];function im(e,t,n={}){if(V(n,J0,"ansariTest"),typeof e=="string"){let b=Qn(e,t);return im(b.x,b.y,n)}let r=Array.from(e).filter(b=>!Number.isNaN(b)),o=Array.from(t).filter(b=>!Number.isNaN(b)),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=Ge(n.alternative),a=n.confInt??!1,l=n.confLevel??.95;a&&It(l);let f=s+i,m=bt(r.concat(o)),u=m.map(b=>Math.min(b,f-b+1)),h=0;for(let b=0;b<s;b++)h+=u[b];let p=new Set(m).size<f,d=n.exact??(s<50&&i<50&&!p),g=n.exact??(s<50&&i<50),w=new mt,N,v;if(d&&!p){rm(s,i);let b=x=>Ti(x-1,s,i,!1),M=x=>Ti(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?b(h):M(h)))}else c==="less"?N=b(h):N=M(h);a&&(v=rg(r,o,c,l,w))}else{let b=cm(h,u,p,s,i);N=c==="two.sided"?2*Math.min(ie(b),ie(b,{lowerTail:!1})):c==="less"?ie(b,{lowerTail:!1}):ie(b),a&&(v=og(r,o,c,l,g?new mt:w)),g&&p&&(w.add("cannot compute exact p-value with ties"),a&&w.add("cannot compute exact confidence interval with ties"))}let E={method:"Ansari-Bradley test",alternative:c,statistic:h,statisticName:"AB",pValue:N,warnings:w.list};return v!==void 0&&(E.confInt=v.confInt,E.confLevel=l,E.estimate=v.estimate,E.estimateNames="ratio of scales"),E.nullValue=1,E.nullValueName="ratio of scales",H("ansariTest",E)}function cm(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 lm(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)<eg))?tg(e.map(f=>om(f,n)).concat(t.map(f=>om(f,1)))):ng(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 eg=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]=sm(Math.abs(e)),[s,i]=sm(t),c=o/i,a=r-s;return c<1&&(c*=2,a-=1),[n,a,c]}function sm(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 am(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 tg(e){let t=e.map((r,o)=>o).sort((r,o)=>am(e[r],e[o])),n=new Array(e.length);for(let r=0;r<t.length;){let o=r+1;for(;o<t.length&&am(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 ng(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 Ks(e,t,n){return e==="two.sided"?n(t):e==="greater"?[n(2*t)[0],1/0]:[0,n(2*t)[1]]}function rg(e,t,n,r,o){let s=e.length,i=t.length,c=[];for(let w of e)for(let N of t)w/N>=0&&c.push(w/N);c.sort((w,N)=>w-N);let a=c.length;if(a===0)return o.add("cannot compute confidence set, returning NA"),{confInt:Ks(n,1-r,()=>[NaN,NaN]),estimate:NaN};let l=c.map((w,N)=>lm(e,t,w+(N<a-1?(c[N+1]-w)/2:w*1.01)).ab),f=(w,N,v)=>{let E=1/0;for(let M of l)(N?M-w>0:M-w>=0)&&M-w<E&&(E=M-w);let b=v?-1/0:1/0;return l.forEach((M,x)=>{M-w===E&&(b=v?Math.max(b,c[x]):Math.min(b,c[x]))}),b},m=w=>{let N=js(w/2,s,i),v=js(1-w/2,s,i);if(!l.some(M=>M-N>=0)||!l.some(M=>M-v>0))return o.add("samples differ in location: cannot compute confidence set, returning NA"),[NaN,NaN];let E=f(N,!1,!1),b=f(v,!0,!0);return E>b?[f(v,!1,!1),f(N,!0,!0)]:[E,b]},u=Ks(n,1-r,m),h=js(.5,s,i),p=-1/0,d=1/0;l.forEach((w,N)=>{w-h<=0?p=Math.max(p,c[N]):d=Math.min(d,c[N])});let g=p===-1/0||d===1/0?NaN:(d+p)/2;return g===1/0&&(g=d/2+p/2),{confInt:u,estimate:g}}function og(e,t,n,r,o){let s=e.length,i=t.length,c=(w,N)=>{let{ab:v,a:E,ties:b}=lm(e,t,w);return cm(v,E,b,s,i)-N},a=[],l=(w,N)=>{w.length===0||N.length===0||a.push(sn(w)/At(N),At(w)/sn(N))};if(l(e.filter(w=>w>0),t.filter(w=>w>0)),l(e.filter(w=>w<=0),t.filter(w=>w<0)),a.some(w=>!Number.isFinite(w))){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:Ks(n,1-r,()=>[NaN,NaN]),estimate:NaN};let f=sn(a),m=At(a),u=w=>Kt(N=>c(N,w),f,m,{tol:1e-4}),h=w=>{let N=oe(w/2),v=oe(w/2,{lowerTail:!1});if(c(f,N)>0||c(m,v)<0)return o.add("samples differ in location: cannot compute confidence set, returning NA"),[NaN,NaN];let E=u(N),b=u(v);return E<=b?[E,b]:[b,E]},p=Ks(n,1-r,h),d=c(f,0)>0||c(m,0)<0;d&&o.add("cannot compute estimate, returning NA");let g=d?NaN:u(0);return{confInt:p,estimate:g}}var sg=["alternative","confLevel"];function ag(e,t,n={}){V(n,sg,"brunnerMunzelTest");let r=Array.from(e).filter(y=>!Number.isNaN(y)),o=Array.from(t).filter(y=>!Number.isNaN(y)),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=Ge(n.alternative),a=n.confLevel??.95;an(a);let l=bt(r.concat(o)),f=l.slice(0,s),m=l.slice(s),u=y=>y.reduce((R,T)=>R+T,0)/y.length,h=u(f),p=u(m),d=(y,R,T)=>{let S=(y.length+1)/2,k=0;for(let L=0;L<y.length;L++)k+=(y[L]-R[L]-T+S)**2;return k/(y.length-1)},g=d(f,bt(r),h),w=d(m,bt(o),p),N=(p-(i+1)/2)/s,v=s*g+i*w,E=s*i*(p-h)/(s+i)/Math.sqrt(v),b=v**2/((s*g)**2/(s-1)+(i*w)**2/(i-1)),M;c==="greater"?M=vt(E,b,!0,!1):c==="less"?M=vt(E,b,!1,!1):M=2*vt(-Math.abs(E),b,!0,!1);let x=xt((1-a)/2,b,!1,!1)*Math.sqrt(g/(s*i**2)+w/(i*s**2));return{method:"Brunner-Munzel Test",alternative:c,statistic:E,statisticName:"Brunner-Munzel Test Statistic",parameter:b,parameterName:"df",pValue:M,confInt:[N-x,N+x],confLevel:a,estimate:N,estimateNames:"P(X<Y)+.5*P(X=Y)",warnings:[]}}var ig=[1.2337005501361697,7831619385924639e-32];function cg(e,t,n){let r=(f,m)=>{let u=new Float64Array(t*t);for(let h=0;h<t;h++)for(let p=0;p<t;p++){let d=f[h*t+p];if(d!==0)for(let g=0;g<t;g++)u[h*t+g]+=d*m[p*t+g]}return u},o=f=>{let m=0;for(let p of f)m=Math.max(m,Math.abs(p));if(m===0||!Number.isFinite(m))return 0;let u=Math.floor(Math.log2(m));if(Math.abs(u)<500)return 0;let h=2**-u;for(let p=0;p<f.length;p++)f[p]*=h;return u},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 lg=2**33;function mm(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>lg)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 um(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]=Ht(e,t),u=2*f-1+2*m,h=1,p=0;for(let d=1;d<=e;d++)h*=d/e*u,h<2**-500&&(h*=2**500,p-=500);for(;p<-1e3;)h*=2**-1e3,p+=1e3;return h*2**p}let n=Math.floor(e*t)+1,r=2*n-1,o=n-e*t,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]=cg(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 Si(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=lt(ig,Ht(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 Ai(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]=Ht(e,t),o=Math.floor(e-n-r),s=0;for(let i=0;i<=o;i++){let c=ue($t(e-i,-n),[-r,0]);if(c[0]<=0)break;let a=ue($t(i,n),[r,0]),l=lt(a,[e,0]),f=lt(c,[e,0]);s+=Cc(i,e-i,l,f,!1)/(l[0]+l[1])}return Math.min(t*s,1)}var fm=2**28;function hm(e,t){let[n,r]=Ht(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 d=(h+n+r)/e;d>0&&d<1&&s.push({t:d,up:!1,i:h})}if(s.length*(2*o+1)*Math.min(e+1,100)>fm)return NaN;s.sort((h,p)=>h.t-p.t);let i=o+1,c=new Float64Array(e+1),a=new Float64Array(e+1);c[0]=1;let l=0,f=0,m=0,u=0;for(let h of s){let p=(h.t-f)/(1-f);if(p>0){let d=p/(1-p);a.fill(0);for(let g=l;g<i;g++){let w=c[g];if(w===0)continue;let N=e-g,v=Math.min(N,Math.floor((N+1)*p)),E=i-g,b=fn(v,N,p,!1);for(let M=v;M<=N&&b!==0;M++)M>v&&(b=b*(N-M+1)*d/M),M>=E?m+=w*b:a[g+M]+=w*b,u++;b=v>0?fn(v-1,N,p,!1):0;for(let M=v-1;M>=0&&b!==0;M--)M<v-1&&(b=b*(M+1)/((N-M)*d)),M>=E?m+=w*b:a[g+M]+=w*b,u++}if(u>2*fm)return NaN;[c,a]=[a,c]}if(f=h.t,h.up)i=h.i+1;else{for(let d=l;d<=h.i;d++)m+=c[d],c[d]=0;l=Math.max(l,h.i+1)}}return Math.min(m,1)}function pm(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,d=(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)?(d>0&&(m+=d*gn(u,t,n,p,!1)),f[h]=0):f[h]=d}[l,f]=[f,l],f.fill(0)}return Math.min(m,1)}var fg=["alternative","exact"];function dm(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(V(n,fg,"ksTest"),typeof e=="string"){let N=Qn(e,t);return gm(N.x,N.y,n)}let r=Array.from(e).filter(N=>!Number.isNaN(N)).sort((N,v)=>N-v),o=r.length;if(o<1)throw new RangeError("not enough 'x' data");let s=Ge(n.alternative),i=s==="two.sided"?"D":s==="greater"?"D^+":"D^-";if(typeof t=="function"){let N=-1/0,v=-1/0;for(let y=0;y<o;y++){let R=t(r[y]);if(!(R>=0&&R<=1))throw new RangeError(`the distribution function returned ${R} at ${r[y]}, not a value in [0, 1]`);N=Math.max(N,(y+1)/o-R),v=Math.max(v,R-y/o)}let E=s==="two.sided"?Math.max(N,v):s==="greater"?N:v,b=new Set(r).size<o,M=n.exact??(o<100&&!b),x;if(M&&s==="two.sided"&&E<.5&&mm(o,E),s==="two.sided")if(!M)x=Si(Math.sqrt(o)*E,!1);else{let y=2*Ai(o,E);if(E>=.5)x=Math.min(1,y);else{let R=y<.01?hm(o,E):NaN;x=Number.isNaN(R)?1-um(o,E):R}}else x=M?Ai(o,E):Math.exp(-2*o*E*E);return H("ksTest",{method:`${M?"Exact":"Asymptotic"} one-sample Kolmogorov-Smirnov test`,alternative:dm(s,!0),exact:M,statistic:E,statisticName:i,pValue:Math.min(1,Math.max(0,x)),warnings:b?["ties should not be present for the one-sample Kolmogorov-Smirnov test"]:[]})}let c=Array.from(t).filter(N=>!Number.isNaN(N)),a=c.length;if(a<1)throw new RangeError("not enough 'y' data");let l=r.map(N=>[N,0]).concat(c.map(N=>[N,1]));l.sort((N,v)=>N[0]-v[0]);let f=0,m=0,u=0,h=0;for(let N=0;N<l.length;N++){if(l[N][1]===0?f++:m++,N+1<l.length&&l[N+1][0]===l[N][0])continue;let v=f/o-m/a;u=Math.max(u,v),h=Math.max(h,-v)}let p=s==="two.sided"?Math.max(u,h):s==="greater"?u:h,d=new Set(l.map(N=>N[0])).size<l.length,g=n.exact??o*a<1e4,w;if(g)w=pm(p,o,a,d?{alternative:s,z:l.map(N=>N[0])}:{alternative:s});else{let N=o*a/(o+a);w=s==="two.sided"?Si(Math.sqrt(N)*p,!1):Math.exp(-2*N*p*p)}return H("ksTest",{method:`${g?"Exact":"Asymptotic"} two-sample Kolmogorov-Smirnov test`,alternative:dm(s,!1),exact:g,statistic:p,statisticName:i,pValue:Math.min(1,Math.max(0,w)),warnings:d&&!g?["p-value will be approximate in the presence of ties"]:[]})}var mg=["poolSd","paired","pAdjustMethod","alternative"],ug=["mu","paired","exact","correct","confLevel","tolRoot","pAdjustMethod","alternative"],hg=["pAdjustMethod","correct"];function wm(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=en(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(Vn);return{levels:i,groups:i.map(c=>o.get(c))}}function ki(e,t,n){let r=[],o=[];for(let c=0;c<e-1;c++)for(let a=c+1;a<e;a++)r.push(t(a,c)),o.push([a-1,c]);let s=Yr(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 qi(e,t,n,r,o=[]){return{method:e,pValue:n,rowNames:t.slice(1).map(Rt),colNames:t.slice(0,-1).map(Rt),pAdjustMethod:r,warnings:o}}function pg(e,t,n={}){V(n,mg,"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=Ge(n.alternative),i=n.pAdjustMethod??"holm",{levels:c,groups:a}=wm(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(w=>w.length),h=Et(a),p=h.mean,d=0;for(let w of u)w>1&&(d+=w-1);Co(d,h.ssWithin);let g=Math.sqrt(h.ssWithin/d);f=(w,N)=>{let v=(p[w]-p[N])/(g*Math.sqrt(1/u[w]+1/u[N]));return s==="less"?vt(v,d,!0,!1):s==="greater"?vt(v,d,!1,!1):2*vt(-Math.abs(v),d,!0,!1)}}else m=r?"paired t tests":"t tests with non-pooled SD",f=(u,h)=>je(()=>vn(a[u],a[h],{paired:r,alternative:s})).pValue;return qi(m,c,ki(l,f,i),i)}function dg(e,t,n={}){V(n,ug,"pairwiseWilcoxTest");let{pAdjustMethod:r,alternative:o,...s}=n,i=r??"holm",{levels:c,groups:a}=wm(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 mt,u=ki(l,(h,p)=>{let d=je(()=>Ao(a[h],a[p],{...s,...o===void 0?{}:{alternative:o}}));f=d.method;for(let g of d.warnings)m.add(g);return d.pValue},i);return H("pairwiseWilcoxTest",qi(f,c,u,i,m.list))}function gg(e,t,n={}){if(V(n,hg,"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 mt,l=ki(s,(m,u)=>{let h=je(()=>Ga([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 H("pairwisePropTest",qi("Pairwise comparison of proportions",f,l,i,a.list))}var wg=1e-14;function bg(e,t,n,r,o,s,i,c){let a=M=>$(M,2*i),l=t.length,f=n.flatMap((M,x)=>Ns(M)[0]>wg*s[x]?[x]:[]),m=0,u=[],h=t.slice(),p=new Array(l).fill(0);if(f.length>0){let M=jt(Array.from({length:l},(y,R)=>f.map(T=>n[T][R][0])));m=M.rank,u=M.pivot.slice(0,m).map(y=>f[y]);let x=vs(u.map(y=>n[y]),t);h=x.qty,u.forEach((y,R)=>{for(let T=0;T<l;T++)p[T]=p[T]+n[y][T][0]*x.coef[R][0]})}let d=[];u.forEach((M,x)=>{let y=r[M],R=d.find(T=>T.term===y);R===void 0&&d.push(R={term:y,df:0,ss:[0,0]}),R.df++,R.ss=ue(R.ss,_e(h[x],h[x]))});let g=l-m,w=Ns(h,m)[0],N=si(p,w,g),v=d.filter(M=>M.term!==0);g>0&&yr(c,w)&&(w=0,v=v.map(M=>yr(c,M.ss[0])?{...M,ss:[0,0]}:M));let E=v.map(M=>o[M.term-1]),b=M=>{if(g>0){let x=w/g,y=v.map(({df:R,ss:T})=>{let S=T[0]/R/x;return[R,M(T[0]),M(T[0]/R),S,wt(S,R,g,!1,!1)]});return y.push([g,M(w),M(x),NaN,NaN]),{stratum:`Error: ${e}`,colNames:["Df","Sum Sq","Mean Sq","F value","Pr(>F)"],rowNames:[...E,"Residuals"],table:y}}return{stratum:`Error: ${e}`,colNames:["Df","Sum Sq","Mean Sq"],rowNames:E,table:v.map(({df:x,ss:y})=>[x,M(y[0]),M(y[0]/x)])}};return[b(a),b(M=>M),N]}function bm(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=jr({response:t.response,rhs:i},Object.keys(o)).variables,a=gr(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=gr({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)=>Re([f.x.map(C=>C[F])]).groups[0]),d=f.x.map((I,F)=>p.map(C=>C[F])),g=h>0?jt(d):void 0,w=g?.rank??0;w<h&&u.push("Error() model is singular");let N=g?.pivot.slice(0,w)??[],v=I=>I.map(F=>[F,0]),E=a.colNames.map((I,F)=>v(Re([a.x.map(C=>C[F])]).groups[0])),{groups:b,k:M}=Re([l]),{fit:x,qtv:y}=Jn(N.map(I=>v(p[I])),v(b[0]),E),R=b[0].map(I=>[1,I]),T=E.map(I=>Ns(I)[0]),S=[];N.forEach((I,F)=>{let C=f.assign[I],D=C===0?"(Intercept)":f.terms.termLabels[C-1],A=S.find(O=>O.name===D);A===void 0&&S.push(A={name:D,idx:[]}),A.idx.push(F)}),m>w&&S.push({name:"Within",idx:Array.from({length:m-w},(I,F)=>w+F)});let k=S.map(I=>bg(I.name,I.idx.map(F=>x.qty[F]),y.map(F=>I.idx.map(C=>F[C])),a.assign,a.terms.termLabels,T,M,R)),L=k.map(I=>I[0]),_={formula:e,multistratum:!0,strata:L,rows:a.frame.rows,warnings:u};return Wt.set(_,k.map(I=>I[1])),k.some((I,F)=>I[2]&&S[F].name!=="(Intercept)")&&ai.set(_,[oi]),_}var vg=["weights","contrasts","levels"],vm=["confLevel"],Nm=["sigmaX","sigmaY","mu","alternative","confLevel"];function Ng(e,t,n={}){return V(n,vg,"aov"),typeof e=="string"?H("aov",ym(e,t,n)):H("aov",yg(e,t))}function ym(e,t,n){let r=Xn(e,!0),{treatment:o,errors:s}=Jc(r.rhs);if(s.length>1)throw new Error(`there are ${s.length} Error terms: only 1 is allowed`);return s.length===1?bm(e,r,o,s[0],t,n):fl(e,gr(r,t,{...n,dropUnusedLevels:!0},o),!0)}function yg(e,t){let{labels:n,groups:r}=Zn(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 ym("y ~ g",{y:o,g:s},{levels:{g:n}})}function Mm(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}=Et(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?wt(f,i,c,!1,!1):NaN}}function Mg(e,t,n={}){at(t)&&V(t,vm,"TukeyHSD"),V(n,vm,"TukeyHSD");let r;t!==void 0&&!Array.isArray(t)?n=t:r=t;let{labels:o,groups:s}=Zn(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;an(c);let l=s.reduce((w,N)=>w+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}=Et(s),h=f/l,p=Yc(c,i,l),d=[],g={names:[],i:[],j:[],diff:[],lwr:[],upr:[],pAdj:d,pAdjusted:d,warnings:[]};for(let w=0;w<i-1;w++)for(let N=w+1;N<i;N++){let v=m[N]-m[w],E=Math.sqrt(h/2*(1/s[N].length+1/s[w].length));g.names.push(`${o[N]}-${o[w]}`),g.i.push(N+1),g.j.push(w+1),g.diff.push($(v,u)),g.lwr.push($(v-p*E,u)),g.upr.push($(v+p*E,u)),g.pAdj.push(as(Math.abs(v)/E,i,l,{lowerTail:!1}))}return g}function Eg(e,t,n={}){at(t)&&V(t,Nm,"zTest"),V(n,Nm,"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=Ge(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=Re([l,f??[],[o,s??0]]),u=m.k,[h,p,[d,g]]=m.groups,w=Bn(h)[0],N=f===void 0?NaN:Bn(p)[0],v=f===void 0?d/Math.sqrt(l.length):Math.sqrt(d**2/l.length+g**2/f.length),E=f===void 0?w:w-N,b=(E-$(i,-u))/v,M,x;if(c==="less")M=ie(b),x=[-1/0,E+oe(a)*v];else if(c==="greater")M=ie(b,{lowerTail:!1}),x=[E-oe(a)*v,1/0];else{M=2*ie(-Math.abs(b));let T=(1-a)/2,S=(T<1e-8?oe(T,{lowerTail:!1}):oe(1-(1-a)/2))*v;x=[E-S,E+S]}x=[$(x[0],u),$(x[1],u)];let y=f!==void 0,R=(T,S)=>{if(!(u>0&&T.some((L,_)=>L!==0&&Math.abs(S[_])<2**-1022)))return $(Bn(S)[0],u);let k=Math.max(Re([T]).k,0);return $(Bn(k===0?T:T.map(L=>$(L,-k)))[0],k)};return{method:y?"Two-sample z-Test":"One-sample z-Test",alternative:c,statistic:b,statisticName:"z",pValue:M,confInt:x,confLevel:a,estimate:y?[R(l,h),R(f,p)]:R(l,h),estimateNames:y?["mean of x","mean of y"]:"mean of x",nullValue:i,nullValueName:y?"difference in means":"mean",warnings:[]}}var xg=["parm","level","test"],Em={epsilon:1e-300,maxit:100,untilCoefficients:!0};function Ii(e,t,n,r,o=1e-11){let s=[];for(let d=0;d<t.length;d++)n[d]>0&&s.push(d);let i=0,c=0;for(let d of s)i+=n[d],c+=n[d]*t[d];if(r&&!(Number.isFinite(i)&&Number.isFinite(c))){let d=0;for(let g of s)d=Math.max(d,n[g]);if(Number.isFinite(d)){let g=d>1?Math.floor(Math.log2(d)):0;i=0,c=0;for(let w of s){let N=$(n[w],-g);i+=N,c+=N*t[w]}}}let a=r?c/i:0,l=0;for(let d of s)l+=n[d]*(t[d]-a)**2;if((e.length>0?e[0].length:0)===0||s.length===0)return 0;let m=s.map(d=>Math.sqrt(n[d])),u=jt(s.map((d,g)=>e[d].map(w=>w*m[g])),{tol:o}),h=ds(u,s.map((d,g)=>t[d]*m[g])),p=0;for(let d of h)p+=d*d;if(!Number.isFinite(l)){let d=s.map((w,N)=>m[N]*(t[w]-a)),g=0;for(let w of d)g=Math.max(g,Math.abs(w));for(let w of h)g=Math.max(g,Math.abs(w));if(Number.isFinite(g)){let w=g>1?Math.floor(Math.log2(g)):0,N=0;for(let E of d)N+=$(E,-w)**2;let v=0;for(let E of h)v+=$(E,-w)**2;return $($(N-v,w),w)}}return l-p}function Rg(e,t={}){V(t,xg,"glmConfint");let{fit:n,k:r}=ys(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 mt,m=br(a,l,f,{...Em,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},d=je(()=>ya(p)),g=d.dispersion,w=new Array(i).fill(NaN);(m.fit.qr===void 0?[]:m.fit.qr.pivot.slice(0,m.fit.rank)).forEach((_,I)=>w[l[_]]=d.stdError[I]);let v=a.x.map(_=>l.map(I=>_[I])),E=m.fit.eta,b=(_,I)=>{let F=l.filter(A=>A!==_),C=br(a,F,new mt,{...Em,start:void 0,etastart:E,extraOffset:a.x.map(A=>A[_]*I)}).fit;if(!C.converged)return h=!0,NaN;let D=s==="LRT"?C.deviance-m.fit.deviance:Ii(v,C.workingResiduals,C.workingWeights,!1);return Math.sign(I-u[_])*Math.sqrt(Math.max(D/g,0))},M=(1-o)/2,x=Math.sqrt(Math.PI/2)*o*(1+Math.PI/12*o**2+7*Math.PI**2/480*o**4),y=2**-9,R=x<y,T=R?[-x,x]:[oe(M),M<1e-8?oe(M,{lowerTail:!1}):oe((1+o)/2)],S=c.map(()=>NaN),k=c.map(()=>NaN),L=(_,I,F)=>{let C=F===0?-1:1;h=!1;let D=NaN,A=NaN;for(let W=0;W<=30;W++){let te=u[_]+C*2**W*w[_],U=b(_,te);if(Math.abs(U)>=Math.abs(I)){D=te,A=U;break}}if(Number.isNaN(D)||h)return NaN;let O=W=>W===u[_]?-I:b(_,W)-I,[q,P]=F===0?[D,u[_]]:[u[_],D],[B,J]=F===0?[A-I,-I]:[-I,A-I],ce;try{ce=Kt(O,q,P,{tol:1e-14*w[_],maxIter:500,fLower:B,fUpper:J})}catch(W){if(!h)throw W;ce=NaN}return h?NaN:ce};return c.forEach((_,I)=>{if(Number.isNaN(u[_]))return;if(!R){S[I]=L(_,T[0],0),k[I]=L(_,T[1],1);return}let F=[-2*y,-y,0,y,2*y],C=[L(_,-2*y,0),L(_,-y,0),u[_],L(_,y,1),L(_,2*y,1)].map(A=>A-u[_]),D=A=>{let O=0;for(let q=0;q<5;q++){let P=1;for(let B=0;B<5;B++)B!==q&&(P*=(A-F[B])/(F[q]-F[B]));O+=P*C[q]}return u[_]+O};S[I]=D(T[0]),k[I]=D(T[1])}),{lower:S.map(_=>$(_,r)),upper:k.map(_=>$(_,r))}}var Tg=["scope","direction","k","scale","steps"],Sg=["lower","upper"],Ag=["scope","direction","k","scale","steps"],kg=["lower","upper"],qg=["scope","test","k","scale"],Ig=["scope","test","k","scale"],_g=["k","scale"],Yt=e=>"linearPredictors"in e;function Oi(e,t,n){if(typeof e!="object"||e===null||typeof e.formula!="string"||!Array.isArray(e.termLabels))throw new TypeError(`${n} needs a fit of lm or glm with a formula`);if(typeof t!="object"||t===null)throw new TypeError("data must be an object of columns");let r=Yt(e),o=ll(e);if(o===void 0&&((r?e.model.priorGiven.some(f=>f!==1)||e.model.hasOffset:e.weights!==void 0)||r))throw new TypeError(`${n} needs the fit as returned by lm or glm (its options are not known)`);let s=e.formula.indexOf("~"),i=e.formula.slice(0,s).trim(),c=r?e.model.intercept:e.coefNames[0]==="(Intercept)",a=Object.values(t)[0]?.length??0;return{data:t,glm:r,lhs:i,intercept:c,options:o??{},nAll:a}}function _i(e,t){return t.length===0?`${e.lhs} ~ ${e.intercept?"1":"0"}`:`${e.lhs} ~ ${t.join(" + ")}${e.intercept?"":" - 1"}`}function xm(e,t){let n=s=>s instanceof Float64Array?Float64Array.from(t,i=>s[i]):Array.isArray(s)&&s.length===e.nAll?t.map(i=>s[i]):s,r=Object.fromEntries(Object.entries(e.data).map(([s,i])=>[s,n(i)])),o={...e.options};for(let s of["weights","offset"])typeof o[s]=="object"&&o[s]!==null&&(o[s]=n(o[s]));return{data:r,options:o}}function Oo(e,t,n){let{data:r,options:o}=n===void 0?{data:e.data,options:{...e.options}}:xm(e,n),s=_i(e,t),i=je(()=>e.glm?er(s,r,o):wr(s,r,o));if(n!==void 0){let a=(e.glm?i.model.rows:i.rows).map(l=>n[l]);e.glm?i.model.rows=a:i.rows=a}return i}var Rm=e=>Yt(e)?e.model.rows:e.rows,Fi=e=>Rm(e).length,Ws=e=>Yt(e)?e.priorWeights.filter(t=>t>0).length:e.nObs,or=e=>Yt(e)?e.deviance:e.rss,Tm=e=>e.rank;function Di(e){let t=e.rss;if(t>1e-290&&t<1/0)return Math.log(t);let n=e.weights,r=[];if(e.residuals.forEach((s,i)=>{let c=n?.[i]??1;c>0&&s!==0&&r.push(Math.log(Math.abs(s))+.5*Math.log(c))}),r.length===0)return-1/0;let o=Math.max(...r);return 2*o+Math.log(r.reduce((s,i)=>s+Math.exp(2*(i-o)),0))}function Lg(e){return e.rss>1e-290&&e.rss<1/0?Math.log(e.rss):e.yScale!==void 0&&e.rssScaled!==void 0&&e.rssScaled>0?Math.log(e.rssScaled)+2*e.yScale*Math.LN2:Math.log(e.rss)}function Ys(e,t,n){let r=Fi(e),o=r-e.dfResidual;return Yt(e)?[o,e.aic+(t-2)*o]:n>0?[o,e.rss/n-r+t*o]:[o,r*(Di(e)-Math.log(r))+t*o]}function Do(e){return e.termLabels.map((t,n)=>{let r=e.variables.filter((o,s)=>(e.factors[s]?.[n]??0)!==0);return{label:t,vars:r,key:[...r].sort().join("\0")}})}function Fo(e,t,n){if(typeof e!="string")throw new TypeError(`the ${n} scope must be a formula such as "~ x1 + x2"`);let r=e.includes("~")?e:`~ ${e}`,o=Xn(r),s=c=>typeof c=="object"&&c!==null&&(c.type==="dot"||Object.values(c).some(a=>typeof a=="object"&&s(a)));if(s(o.rhs))throw new RangeError(`'.' is not supported in the ${n} scope: list its terms`);let i=jr({response:void 0,rhs:o.rhs},Object.keys(t));return i.termLabels.map((c,a)=>{let l=i.variables.filter((f,m)=>(i.factors[m]?.[a]??0)!==0);return{label:c,vars:l,key:[...l].sort().join("\0")}})}var Sm=(e,t)=>t.vars.length<e.vars.length&&t.vars.every(n=>e.vars.includes(n));function Am(e,t){return e.filter(n=>!t.some(r=>r.key===n.key)&&!e.some(r=>Sm(r,n)))}function km(e,t){let n=r=>e.some(o=>o.key===r.key);return t.filter(r=>!n(r)&&t.every(o=>!Sm(r,o)||n(o)))}function qm(e,t){if(Yt(e))return;let n=e.weights,r=0;e.y.forEach((o,s)=>r+=(n?.[s]??1)*o*o),e.rss<1e-10*r&&t.add("attempting model selection on an essentially perfect fit is nonsense")}function Ur(e,t){for(let n of t.warnings)(Yt(t)||n!=="essentially perfect fit: summary may be unreliable")&&e.add(n)}var Im=e=>e.priorWeights.filter(t=>t>0).length,Pi=e=>e.family==="binomial"||e.family==="poisson";function _m(e,t){return e.family==="gaussian"?Im(e)*Math.log(t/e.deviance):Pi(e)?t-e.deviance:(t-e.deviance)/yo(e)}function Li(e,t,n){let r=Fi(e),o=Tm(e),s=Yt(e)?NaN:Di(e),i=Yt(e)?e.aic+(t-2)*o:n>0?e.rss/n-r+t*o:r*(s-Math.log(r))+t*o;return{label:"<none>",df:NaN,sumOfSq:NaN,deviance:or(e),aic:i,rank:o,dfResidual:e.dfResidual,logDev:s,fit:e}}function Ci(e,t,n,r,o,s,i,c,a=Math.log(n),l){let f=e.fit,m=s?r-e.rank:e.rank-r,u;if(Yt(f))u=e.aic+_m(f,n)+i*(r-e.rank);else{let h=Fi(f);u=c>0?n/c-h+i*r:h*(a-Math.log(h))+i*r}return{label:t,df:m,sumOfSq:s?e.deviance-n:n-e.deviance,deviance:n,aic:u,rank:r,dfResidual:o,logDev:a,fit:l}}function Lm(e,t){Yt(e)&&!Pi(e)&&yo(e,t)}function Cm(e,t,n,r,o){qm(e,o),Lm(e,o);let s=Li(e,n,r),i=Do(e),c=[s];for(let a of t){let l=i.findIndex(m=>m.key===a.key)+1,f=e.assign.flatMap((m,u)=>m===l?[]:[u]);if(Yt(e)){let m=new mt,u=br(e.model,f,m);for(let h of m.list)o.add(h);c.push(Ci(s,a.label,u.fit.deviance,u.fit.rank,Im(e)-u.fit.rank,!1,n,r))}else{let m=ml(e.x.map(u=>f.map(h=>u[h])),e.y,e.weights,e.coefNames[0]==="(Intercept)"&&f[0]===0,!0);c.push(Ci(s,a.label,m.rss,m.result.rank,m.result.dfResidual,!1,n,r,Lg(m)))}}return c}function Om(e,t,n,r,o,s){let i=Rm(t),c=t,a=[...new Set(n.flatMap(m=>m.vars))].filter(m=>!t.variables.includes(m));if(a.length>0){let m=e.options.levels,u=xm(e,i).data,h=fs(u,a,m===void 0?{}:{levels:m}).rows.map(p=>i[p]);h.length<i.length&&(s.add(`using the ${h.length}/${i.length} rows from a combined fit`),i=h,c=Oo(e,t.termLabels,i))}qm(c,s),Lm(c,s);let l=Li(c,r,o);Yt(c)&&c!==t&&(l.aic=Li(t,r,o).aic);let f=[l];for(let m of n){let u=Oo(e,[...t.termLabels,m.label],i);Ur(s,u),f.push(Ci(l,m.label,or(u),Tm(u),u.dfResidual,!0,r,o,Yt(u)?NaN:Di(u),u))}return f}function Fm(e,t,n){let r=e[0],o=r.fit,s=[NaN],i=[NaN];for(let a of e.slice(1)){let l=n?a:r,f=Math.abs(a.deviance-r.deviance),m,u;t==="F"?(m=f/a.df/(l.deviance/l.dfResidual),u=xn(m,a.df,l.dfResidual,{lowerTail:!1})):(m=Yt(o)?Math.abs(_m(o,a.deviance)):f/(l.deviance/l.dfResidual),u=Mt(m,a.df,{lowerTail:!1})),s.push(a.df===0?NaN:m),i.push(a.df===0?NaN:u)}let c=t==="F"?"F value":Yt(o)&&Pi(o)?"LRT":"scaled dev.";return{statistic:s,pValue:i,statisticName:c}}function Dm(e,t,n,r){let o={term:e.map(s=>s.label),df:e.map(s=>s.df),deviance:e.map(s=>s.deviance),aic:e.map(s=>s.aic),criterion:n>0&&!t?"Cp":"AIC",warnings:r.list};return t||(o.sumOfSq=e.map(s=>s.sumOfSq)),o}function zn(e,t,n=!1){if(typeof e!="number"||Number.isNaN(e))throw new TypeError(`${t} must be a number`);if(n?!(e>=0):!Number.isFinite(e))throw new RangeError(`${t} must be ${n?"non-negative":"finite"}`);return e}function Cg(e,t={}){V(t,_g,"extractAIC");let n=zn(t.k??2,"k"),r=zn(t.scale??0,"scale",!0);return Ys(e,n,r)}function Og(e,t,n={}){V(n,qg,"drop1");let r=Oi(e,t,"drop1"),o=zn(n.k??2,"k"),s=r.glm?0:zn(n.scale??0,"scale",!0),i=Do(e),c=n.scope===void 0?Am(i,[]):Fo(n.scope,t,"drop1").map(m=>{let u=i.find(h=>h.key===m.key);if(u===void 0)throw new RangeError("scope is not a subset of term labels");return u}),a=new mt;Ur(a,e);let l=Cm(e,c,o,s,a),f=Dm(l,r.glm,s,a);return n.test!==void 0&&n.test!=="none"&&Object.assign(f,{test:n.test==="F"?"F":"Chisq"},Fm(l,n.test==="F"?"F":"Chisq",!1)),f.warnings=a.list.filter(m=>!e.warnings.includes(m)),H("drop1",f,e)}function Fg(e,t,n={}){V(n,Ig,"add1");let r=Oi(e,t,"add1");if(n.scope===void 0)throw new TypeError("no terms in scope for adding to object");let o=zn(n.k??2,"k"),s=r.glm?0:zn(n.scale??0,"scale",!0),i=km(Do(e),Fo(n.scope,t,"add1")),c=new mt;Ur(c,e);let a=Om(r,e,i,o,s,c),l=Dm(a,r.glm,s,c);return n.test!==void 0&&n.test!=="none"&&Object.assign(l,{test:n.test==="F"?"F":"Chisq"},Fm(a,n.test==="F"?"F":"Chisq",!0)),l.warnings=c.list.filter(f=>!e.warnings.includes(f)),H("add1",l,e)}function Pm(e,t,n,r){let o=Oi(e,t,r),s=n.direction??"both";if(!["both","backward","forward"].includes(s))throw new RangeError(`'direction' should be one of "both", "backward", "forward"`);let i=zn(n.k??2,"k"),c=o.glm?0:zn(n.scale??0,"scale",!0),a=zn(n.steps??1e3,"steps",!0),l=Do(e),f=[],m=l,u=n.scope;if(u!==void 0)if(typeof u=="string")m=Fo(u,t,"upper");else if(typeof u=="object"&&u!==null)u.lower!==void 0&&(f=Fo(u.lower,t,"lower")),u.upper!==void 0&&(m=Fo(u.upper,t,"upper"));else throw new TypeError("scope must be a formula or { lower, upper }");for(let y of l)if(!m.some(R=>R.key===y.key))throw new RangeError(`upper scope has term \u2018${y.label}\u2019 not included in model`);for(let y of f)if(!l.some(R=>R.key===y.key))throw new RangeError(`lower scope has term \u2018${y.label}\u2019 not included in model`);let h=s!=="forward",p=s!=="backward",d=new mt;Ur(d,e);let g=e,[,w]=Ys(g,i,c);if(Number.isNaN(w))throw new RangeError(`AIC is not defined for this model, so '${r==="step"?"step":"stepAIC"}' cannot proceed`);if(w===-1/0)throw new RangeError(`AIC is -infinity for this model, so '${r==="step"?"step":"stepAIC"}' cannot proceed`);let N=Ws(g),v={step:[""],df:[NaN],deviance:[NaN],residDf:[Ws(g)-Ys(g,i,c)[0]],residDeviance:[or(g)],aic:[w]},E=[{formula:_i(o,g.termLabels),termLabels:[...g.termLabels],aic:w}],b=(y,R,T)=>{if(Ws(y)!==N)throw new RangeError("number of rows in use has changed: remove missing values?");let[S,k]=Ys(y,i,c),L=y.dfResidual-g.dfResidual;v.step.push(R),v.df.push(r==="step"?L:Math.abs(L)),v.deviance.push(T?or(y)-or(g):or(g)-or(y)),v.residDf.push(Ws(y)-S),v.residDeviance.push(or(y)),v.aic.push(k),g=y,w=k,E.push({formula:_i(o,g.termLabels),termLabels:[...g.termLabels],aic:w})},M=0;for(;M<a;){let y=Do(g),R=y.filter((O,q)=>{let P=(Yt(g),g.coefNames).map((B,J)=>J).filter(B=>g.assign[B]===q+1);return P.length>0&&P.every(B=>Number.isNaN(g.coefficients[B]))});if(R.length>0){let O=R[R.length-1],q=Oo(o,g.termLabels.filter(P=>P!==O.label));Ur(d,q),b(q,`- ${O.label}`,!0),M++;continue}let T=h?Am(y,f):[],S=p?km(y,m):[],k=[];if(T.length>0&&(k=Cm(g,T,i,c,d).map((O,q)=>q===0?O:{...O,label:`- ${O.label}`})),S.length>0){let O=Om(o,g,S,i,c,d).map((q,P)=>P===0?q:{...q,label:`+ ${q.label}`});k=k.length===0?O.filter((q,P)=>P===0||q.df>0):[...k,...O.slice(1).filter(q=>q.df>0)]}if(k.length===0)break;let L=k.map((O,q)=>q).sort((O,q)=>Dg(k[O].aic,k[q].aic)||O-q),_=L.map(O=>k[O]),I=E[E.length-1];if(I.table={move:_.map(O=>O.label),df:_.map(O=>O.df),deviance:_.map(O=>O.deviance),aic:_.map(O=>O.aic)},o.glm||(I.table.sumOfSq=_.map(O=>O.sumOfSq)),L[0]===0)break;let F=_[0],C=F.label.startsWith("- "),D=F.label.slice(2),A=C?Oo(o,g.termLabels.filter(O=>O!==D)):Oo(o,[...g.termLabels,D]);Ur(d,A),b(A,F.label,C),M++}let x={fit:g,anova:v,criterion:c>0&&!o.glm?"Cp":"AIC",visits:E,warnings:d.list.filter(y=>!e.warnings.includes(y))};return H(r,x,e)}var Dg=(e,t)=>Number.isNaN(e)?Number.isNaN(t)?0:1:Number.isNaN(t)?-1:e-t;function Pg(e,t,n={}){return V(n,Tg,"step"),V(Ft(n,"scope"),Sg,"step","scope"),Pm(e,t,n,"step")}function Vg(e,t,n={}){return V(n,Ag,"stepAIC"),V(Ft(n,"scope"),kg,"stepAIC","scope"),Pm(e,t,n,"stepAIC")}var $g=["weights","family","link","epsilon","maxit"],zg=["family","link","variance"],Vm=["power"];function jg(e,t,n){V(n,$g,"svyglm"),V(Ft(n,"family"),zg,"svyglm","family"),V(Ft(n,"family.link"),Vm,"svyglm","family.link"),V(Ft(n,"link"),Vm,"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(A=>!(A>0)||!Number.isFinite(A)))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((A,O)=>A+O,0)/l,m=a.map(A=>A/f),u=je(()=>er(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(A=>Array.from(A)):Array.from(e,A=>[A]),p=A=>{let O=0;for(let q=0;q<A.length;q++)Number.isFinite(A[q])&&Math.abs(A[q])>O&&(O=Math.abs(A[q]));return O>0?Math.floor(Math.log2(O)):0},d=(h[0]??[]).map((A,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,v=d.every(A=>A===0)&&N===0?u:je(()=>er(h.map(A=>A.map((O,q)=>$(O,-d[q]))),Array.from(t,A=>$(A,-N)),{weights:m,...o===void 0?{}:{family:o},...s===void 0?{}:{link:s},...i===void 0?{}:{epsilon:i},...c===void 0?{}:{maxit:c}})),E=je(()=>xa(v,{type:"HC0"})).vcov,b=[...u.coefficients.length>d.length?[N]:[],...d.map(A=>N-A)],M=E.map((A,O)=>A.map((q,P)=>$(q*l/(l-1),b[O]+b[P]))),x=u.coefficients.length,y=l-x,R=E.map((A,O)=>$(Math.sqrt(A[O]*l/(l-1)),b[O])),T=v.coefficients.map((A,O)=>A/Math.sqrt(E[O][O]*l/(l-1))),S=T.map(A=>2*dn(-Math.abs(A),y)),k=v.fittedValues,L=v.y,_=u.family==="binomial"||u.family==="quasibinomial"?A=>A*(1-A):u.family==="poisson"||u.family==="quasipoisson"?A=>A:u.family==="Gamma"?A=>A*A:u.family==="inverse.gaussian"?A=>A*A*A:()=>1,I=k.map((A,O)=>(L[O]-A)/Math.sqrt(_(A))),F=I.reduce((A,O,q)=>A+m[q]*O,0)/l,C=0;for(let A=0;A<l;A++)C+=m[A]*(I[A]-F)**2;let D=$(C/l*(l/(l-1)),2*N);return H("svyglm",{names:u.model.columnNames.slice(),coefficients:u.coefficients.slice(),se:R,tValue:T,pValue:S,vcov:M,dfResidual:y,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 sr(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=st(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 Kg=new Set(["start","id","ties","weights","strata","init","control","nocenter","confLevel","robust","cluster"]),Wg=["eps","iterMax","tolerChol","tolerInf","timefix"],Vi=e=>e==null||typeof e=="number"&&Number.isNaN(e),Yg=(e,t)=>typeof e=="number"&&typeof t=="number"?e-t:String(e)<String(t)?-1:String(e)>String(t)?1:0,Bg=2**64,Ug=2**-1022,$m=700;function Bs(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(),d=h(),g=0,w=0,N=u(),v=h(),E=u(),b=h(),M=0,x=0,y=R=>{let T=Math.exp(x-R);w*=T,M*=T;for(let S=0;S<m;S++){N[S]*=T,E[S]*=T;let k=v[S],L=b[S];for(let _=0;_<=S;_++)k[_]*=T,L[_]*=T}x=R};for(let R=f-1;R>=0;){c[R]&&(x=0,w=0,N=u(),v=h());let T=r[R],S=!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]&&(S=!0)}if(l!==void 0&&S){let I=R;for(;I>0&&!c[I-1];)I--;let F=R;for(;!c[F];)F++;let C=A=>{let O=0;for(let q=0;q<m;q++)O+=t[q]*i[A][q];return O},D=-1/0;for(let A=I;A<=F;A++)l[A]<T&&r[A]>=T&&(D=Math.max(D,C(A)));x=Number.isFinite(D)?D:0,w=0,N=u(),v=h();for(let A=I;A<=F;A++){if(!(l[A]<T&&r[A]>=T)||r[A]===T&&o[A])continue;let O=Math.exp(C(A)-x)*s[A];w+=O;for(let q=0;q<m;q++){N[q]+=O*i[A][q];for(let P=0;P<=q;P++)v[q][P]+=O*i[A][q]*i[A][P]}}}let k=0,L=0;M=0;let _=R;do{let I=i[_],F=0;for(let A=0;A<m;A++)F+=t[A]*I[A];let C=s[_];Number.isFinite(F)&&(F-x>$m?y(F):F-x<-$m&&w===0&&M===0&&(x=F));let D=Math.exp(F-x)*C;if(o[_]){k++,L+=C,M+=D,g+=C*F;for(let A=0;A<m;A++){p[A]+=C*I[A],E[A]+=D*I[A];let O=b[A];for(let q=0;q<=A;q++)O[q]+=D*I[A]*I[q]}}else if(l===void 0){w+=D;for(let A=0;A<m;A++){N[A]+=D*I[A];let O=v[A];for(let q=0;q<=A;q++)O[q]+=D*I[A]*I[q]}}_--}while(_>=0&&r[_]===T&&!c[_]);if(k>0){let I=n?k:1,F=n?k:1,C=L/I;for(let D=0;D<I;D++){w+=n?M/F:M,g-=C*(Math.log(w)+x+a);for(let A=0;A<m;A++){N[A]+=n?E[A]/F:E[A];let O=N[A]/w;p[A]-=C*O;let q=v[A],P=b[A],B=d[A];for(let J=0;J<=A;J++){q[J]+=n?P[J]/F:P[J];let ce=q[J]-O*N[J],W=C*ce;B[J]+=W<1/0&&W>-1/0&&(Math.abs(W)>=Ug||ce===0)||!Number.isFinite(C)?W/w:C*(ce/w)}}}E.fill(0);for(let D of b)D.fill(0)}R=_}for(let R=0;R<m;R++)for(let T=0;T<R;T++)d[T][R]=d[R][T];return{loglik:g,score:p,info:d}}function zm(e,t,n){let{time:r,event:o,x:s,stratumEnd:i}=e,c=Re([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 d=[];for(let R=u;R<=h;R++)(R===u||r[R]!==r[R-1])&&d.push(R);let g=R=>R+1<d.length?d[R+1]:h+1,w=new Array(d.length),N=new Array(d.length),v=new Array(d.length),E=-1/0,b=0,M=new Array(l).fill(0);for(let R=d.length-1;R>=0;R--){let T=Number.isFinite(E)?E:0;for(let k=d[R];k<g(R);k++)E=Math.max(E,p[k-u]);let S=Number.isFinite(E)?E:0;if(R<d.length-1&&S!==T){let k=Math.exp(T-S);b*=k;for(let L=0;L<l;L++)M[L]*=k}for(let k=d[R];k<g(R);k++){let L=c[k]*Math.exp(p[k-u]-S);b+=L;for(let _=0;_<l;_++)M[_]+=L*s[k][_]}w[R]=b,N[R]=M.slice(),v[R]=S}let x=0,y=new Array(l).fill(0);for(let R=0;R<d.length;R++){let T=v[R];if(R>0&&T!==v[R-1]){let A=Math.exp(T-v[R-1]);x*=A;for(let O=0;O<l;O++)y[O]*=A}let S=d[R],k=g(R),L=[];for(let A=S;A<k;A++)L.push(Math.exp(p[A-u]-T));let _=0,I=0,F=0,C=new Array(l).fill(0);for(let A=S;A<k;A++){if(!o[A])continue;let O=c[A]*L[A-S];_++,I+=c[A],F+=O;for(let q=0;q<l;q++)C[q]+=O*s[A][q]}for(let A=S;A<k;A++){let O=L[A-S],q=s[A],P=f[A];for(let B=0;B<l;B++)P[B]=-O*(q[B]*x-y[B])}if(_===0)continue;let D=n?_:1;for(let A=0;A<D;A++){let O=A/D,q=w[R]-O*F,P=N[R].map((J,ce)=>(J-O*C[ce])/q),B=I/D/q;for(let J=S;J<k;J++){let ce=L[J-S],W=s[J],te=f[J];if(o[J])for(let U=0;U<l;U++){let ve=W[U]-P[U];te[U]+=ve/D-(1-O)*ce*ve*B}else for(let U=0;U<l;U++)te[U]-=ce*(W[U]-P[U])*B}x+=B;for(let J=0;J<l;J++)y[J]+=B*P[J]}}u=h+1}return f}function $i(e,t,n){let r=e.start,{time:o,event:s,x:i,stratumEnd:c}=e,a=Re([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 d=0;for(let g=0;g<f;g++)d+=t[g]*i[p][g];return d});for(let h=0;h<l;){let p=h;for(;!c[p];)p++;for(let d=h;d<=p;){let g=o[d],w=d;for(;w<p&&o[w+1]===g;)w++;let N=[],v=[],E=-1/0;for(let b=d;b<=w;b++)s[b]&&v.push(b);if(v.length>0)for(let b=h;b<=p;b++)r[b]<g&&o[b]>=g&&(N.push(b),E=Math.max(E,u[b]));if(v.length>0&&N.length>0){Number.isFinite(E)||(E=0);let b=0,M=new Array(f).fill(0),x=0,y=new Array(f).fill(0),R=0;for(let S of N){let k=Math.exp(u[S]-E),L=a[S]*k;b+=L;for(let _=0;_<f;_++)M[_]+=L*i[S][_];if(s[S]&&o[S]===g){R+=a[S],x+=L;for(let _=0;_<f;_++)y[_]+=L*i[S][_]}}let T=n?v.length:1;for(let S=0;S<T;S++){let k=S/T,L=b-k*x,_=R/T/L,I=M.map((F,C)=>(F-k*y[C])/L);for(let F of N){let C=Math.exp(u[F]-E),D=s[F]&&o[F]===g;for(let A=0;A<f;A++){let O=i[F][A]-I[A];D?m[F][A]+=O/T-(1-k)*C*O*_:m[F][A]-=C*O*_}}}}d=w+1}h=p+1}return m}function Tr(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 Sr(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 Gr(e){let t=e.length;for(let n=0;n<t;n++)if(e[n][n]>0){e[n][n]=1/e[n][n];for(let r=n+1;r<t;r++){e[r][n]=-e[r][n];for(let o=0;o<n;o++)e[r][o]+=e[r][n]*e[n][o]}}for(let n=0;n<t;n++){if(e[n][n]===0){for(let r=0;r<t;r++)e[n][r]=0,e[r][n]=0;continue}for(let r=n+1;r<t;r++){let o=e[r][n]*e[r][r];e[n][r]=o;for(let s=n;s<r;s++)e[n][s]+=o*e[r][s]}}for(let n=0;n<t;n++)for(let r=0;r<n;r++)e[n][r]=e[r][n];return e}var Po=(e,t)=>({statistic:e,df:t,pValue:Mt(e,t,{lowerTail:!1})});function Gg(e,t,n){let r=e.length,o=()=>new Array(t).fill(NaN),s=Po(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)=>st(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 Hr(e,t,n,r={}){return V(r,Kg,"coxph"),V(Ft(r,"control"),Wg,"coxph","control"),H("coxph",Hg(e,t,n,r).result)}function Hg(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:d}=r,{eps:g=1e-9,iterMax:w=20,tolerChol:N=2**-39}=l,v=l.tolerInf??Math.sqrt(g);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(!(g>0&&g<1/0))throw new RangeError("'eps' must be a positive finite number");if(!(N>0&&N<1/0))throw new RangeError("'tolerChol' must be a positive finite number");if(!(v>0&&v<1/0))throw new RangeError("'tolerInf' must be a positive finite number");if(!(Number.isInteger(w)&&w>=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 b=e.length;if(t.length!==b||n.length!==b)throw new RangeError("'time', 'status' and 'x' must have the same length");if(i!==void 0&&i.length!==b)throw new RangeError("'weights' must have the same length as 'time'");if(c!==void 0&&c.length!==b)throw new RangeError("'strata' must have the same length as 'time'");if(h!==void 0&&h.length!==b)throw new RangeError("'cluster' must have the same length as 'time'");if(p!==void 0&&p.length!==b)throw new RangeError("'start' must have the same length as 'time'");if(d!==void 0&&d.length!==b)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 M=b===0?0:n[0].length;if(M<1)throw new RangeError("'x' must have at least one column");if(n.some(Y=>Y.length!==M))throw new RangeError("the rows of 'x' must have the same length");if(a!==void 0&&a.length!==M)throw new RangeError("'init' must have one value per column of 'x'");if(a?.some(Y=>!Number.isFinite(Y)))throw new RangeError("'init' must contain finite values");let x=new mt,y=[];for(let Y=0;Y<b;Y++){if(Number.isNaN(e[Y])||Number.isNaN(t[Y])||n[Y].some(ye=>Number.isNaN(ye))||p!==void 0&&Number.isNaN(p[Y])||i!==void 0&&Number.isNaN(i[Y])||c!==void 0&&Vi(c[Y])||h!==void 0&&Vi(h[Y])||d!==void 0&&Vi(d[Y]))continue;if(p!==void 0&&!(p[Y]<e[Y]))throw new RangeError("stop time must be > start time");let re=t[Y];if(re!==0&&re!==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[Y]===-1/0||e[Y]===1/0&&re===1)throw new RangeError("event times must be finite");y.push(Y)}let R=y.length;if(R===0)throw new RangeError(p===void 0?"no observations without missing values":"No (non-missing) observations");if(y.some(Y=>n[Y].some(re=>!Number.isFinite(re))))throw new RangeError("'x' contains infinite values");let T=y.map(Y=>i===void 0?1:i[Y]);if(T.some(Y=>!(Y>0)))throw new RangeError("Invalid weights, must be >0");if(T.some(Y=>Y===1/0))throw new RangeError("'weights' must be finite");let S=d===void 0?void 0:Ot(d),k=new Map;if(S!==void 0)for(let Y of y){if(t[Y]!==1)continue;let re=S[Y];k.set(re,(k.get(re)??0)+1)}let L=p!==void 0&&[...k.values()].some(Y=>Y>1),_=u??(h!==void 0||T.some(Y=>!Number.isInteger(Y))||L);if(p!==void 0&&u===!0&&h===void 0&&d===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 Y of y)I+=t[Y];let F=y.map(Y=>e[Y]),C=p===void 0?void 0:y.map(Y=>p[Y]),D,A;if(C===void 0)D=E?sr(F):F;else if(!E)D=F,A=C;else{let Y=sr([...C,...F]);if(A=Y.slice(0,R),D=Y.slice(R),A.some((re,ye)=>re>=D[ye]))throw new Error("aeqSurv exception, an interval has effective length 0")}if(I===0){let Y=Gg(y.map(re=>n[re]),M,x.list);return S!==void 0&&(Y.nId=new Set(y.map(re=>S[re])).size),{result:Y}}let O=new Array(R).fill(0),q,P=[];if(c!==void 0){let Y=Ot(c);P=Array.from(new Set(y.map(ye=>Y[ye]))).sort(Yg);let re=new Map;P.forEach((ye,Ye)=>re.set(ye,Ye)),q=y.map(ye=>Y[ye]),O=y.map(ye=>re.get(Y[ye]))}let B=Array.from({length:R},(Y,re)=>re).sort((Y,re)=>O[Y]-O[re]||D[Y]-D[re]||Y-re),J=B.map(Y=>T[Y]),{groups:ce,k:W}=At(J)>=Bg?Re([J]):{groups:[J],k:0},te=ce[0],U=0;for(let Y of T)U+=W===0?Y:$(Y,-W);let ve=B.map(Y=>n[y[Y]].slice()),X=new Array(M).fill(0),K=new Array(M).fill(1),Z=new Array(M).fill(0),z=new Set(f);for(let Y=0;Y<M;Y++){if(ve.every(ze=>z.has(ze[Y])))continue;let re=ve.map(ze=>ze[Y]),ye=ze=>{let Vt=0;for(let Ln=0;Ln<R;Ln++)Vt+=(p===void 0?te[Ln]:1)*ze[Ln];let _n=p===void 0?U:R,ur=Vt/_n,Fr=0,ma=ze.map((Ln,Xo)=>(Fr+=(p===void 0?te[Xo]:1)*Math.abs(Ln-ur),Ln-ur)),Dr=Fr===0?1:_n/Fr;return{m:ur,s:Dr,d:ma,ok:Number.isFinite(ur)&&Dr>=2**-1022&&Dr<1/0}},Ye=ye(re);if(!Ye.ok){let{groups:ze,k:Vt}=Re([re]);Ye=ye(ze[0]),Z[Y]=Vt}for(let ze=0;ze<R;ze++)ve[ze][Y]=Ye.d[ze]*Ye.s;X[Y]=$(Ye.m,Z[Y]),K[Y]=Ye.s}let G={time:B.map(Y=>D[Y]),...A===void 0?{}:{start:B.map(Y=>A[Y])},event:B.map(Y=>t[y[Y]]===1),weight:te,x:ve,stratumEnd:B.map((Y,re)=>re===R-1||O[B[re+1]]!==O[Y]),logUnit:W*Math.LN2},j=s==="efron",Q=a===void 0?new Array(M).fill(0):a.map((Y,re)=>$(Y/K[re],Z[re])),ne=Bs(G,Q,j),be=[ne.loglik,ne.loglik],Ne=ne.info.map(Y=>Y.slice()),me=Tr(Ne,N),le=Sr(Ne,ne.score),Ee=0;for(let Y=0;Y<M;Y++)Ee+=ne.score[Y]*le[Y];let he=0,Le,we=!1,Me=w===0;if(w===0||!Number.isFinite(ne.loglik))Le=Gr(Ne);else{let Y=ne.loglik,re=Q.map((Ye,ze)=>Ye+le[ze]),ye=0;for(Me=!1,he=1;he<=w;he++){ne=Bs(G,re,j),Ne=ne.info.map(ze=>ze.slice()),me=Tr(Ne,N);let Ye=Number.isFinite(ne.loglik)&&ne.score.every(Number.isFinite)&&ne.info.every(ze=>ze.every(Number.isFinite));if(Ye&&(ne.loglik===Y||Math.abs(1-Y/ne.loglik)<=g)){Me=!0,be[1]=ne.loglik,Q=re;break}!Ye||ne.loglik<Y?(ye++,re=re.map((ze,Vt)=>(ze+ye*Q[Vt])/(ye+1))):(ye=0,Y=ne.loglik,be[1]=Y,le=Sr(Ne,ne.score),Q=re,re=re.map((ze,Vt)=>ze+le[Vt]))}Me?Le=Gr(Ne):(he=p===void 0?w+1:w,we=!0,p!==void 0?(ne=Bs(G,Q,j),be[1]=ne.loglik,Ne=ne.info.map(Ye=>Ye.slice()),me=Tr(Ne,N),Le=Gr(Ne)):w>1?(ne=Bs(G,Q,j),be[1]=ne.loglik,Le=ne.info.map((Ye,ze)=>Ye.map((Vt,_n)=>ze===_n?Ye[ze]>0?1/Ye[ze]:0:-0))):Le=Gr(Ne))}let Se=K.map(Y=>Math.floor(Math.log2(Y))),Ke=K.map((Y,re)=>$(Y,-Se[re])),Pe=Se.map((Y,re)=>Y-Z[re]),Ze=Q.map((Y,re)=>Y*Ke[re]),Be=ne.score.map((Y,re)=>Y/Ke[re]),Qe=Le.map((Y,re)=>Y.map((ye,Ye)=>ye*Ke[re]*Ke[Ye])),fe=Ze.map((Y,re)=>$(Y,Pe[re])),ee=Qe.map((Y,re)=>Y.map((ye,Ye)=>$(ye,Pe[re]+Pe[Ye]-W))),ge=fe.slice();if(w>0&&Math.abs(me)<M)for(let Y=0;Y<M;Y++)Le[Y][Y]===0&&(ge[Y]=NaN);let de=p===void 0?fe:fe.map((Y,re)=>Number.isNaN(ge[re])?0:Y),ae=0;for(let Y=0;Y<M;Y++)ae+=de[Y]*X[Y];let Je=y.map(Y=>{let re=0;for(let ye=0;ye<M;ye++)re+=n[Y][ye]*de[ye];return re-ae});if(w>1){let Y=Qe.map((re,ye)=>{let Ye=0;for(let ze=0;ze<M;ze++)Ye+=Be[ze]*Qe[ze][ye];return Math.abs(Ye)});if(we)(p===void 0||w>1)&&x.add("Ran out of iterations and did not converge"),p===void 0&&(At(Je)>500||Y.some(re=>!Number.isFinite(re)))&&x.add("one or more coefficients may be infinite");else if(p===void 0){let re=[];for(let ye=0;ye<M;ye++){let Ye=Y[ye];(!Number.isFinite(Be[ye])||$(Ye,Pe[ye])>g&&Ye>v*Math.abs(Ze[ye]))&&re.push(ye+1)}re.length>0&&x.add("Loglik converged before variable "+re.join(",")+" ; coefficient may be infinite. ")}}let Oe=ge.map((Y,re)=>Number.isNaN(Y)?-1:re).filter(Y=>Y>=0),tt=Oe.length,ke,$e=0,He=0,se;if(_&&tt>0){let Y=B.map((ct,Ue)=>Ue),re=R,ye=h??(p===void 0?void 0:d);if(ye!==void 0){let ct=Ot(ye),Ue=new Map;Y=B.map(dt=>{let Nt=ct[y[dt]],yt=Ue.get(Nt);return yt===void 0&&(yt=Ue.size,Ue.set(Nt,yt)),yt}),re=Ue.size}if(re<2)throw new RangeError(p===void 0?"at least two clusters are needed for the robust variance":"(subscript) logical subscript too long");let Ye=ct=>ct.map(Ue=>Ue.map((dt,Nt)=>dt/Ke[Nt])),ze=Ye(p===void 0?zm(G,Q,j):$i(G,Q,j)),Vt=ze.map(ct=>Qe.map((Ue,dt)=>{let Nt=0;for(let yt=0;yt<M;yt++)Nt+=ct[yt]*Qe[yt][dt];return Nt})),_n;if(W===0)_n=Vt.map((ct,Ue)=>ct.map(dt=>te[Ue]*dt));else{let ct=dt=>dt===0||!Number.isFinite(dt)?0:Math.floor(Math.log2(Math.abs(dt))),Ue=-1/0;Vt.forEach((dt,Nt)=>dt.forEach(yt=>yt!==0&&Number.isFinite(yt)&&(Ue=Math.max(Ue,ct(te[Nt])+ct(yt))))),$e=Ue===-1/0?0:-Ue,_n=Vt.map((dt,Nt)=>{let yt=ct(te[Nt]);return dt.map(hr=>{let kc=ct(hr);return $($(te[Nt],-yt)*$(hr,-kc),yt+kc+$e)})})}let ur=ct=>{let Ue=Array.from({length:re},()=>new Array(M).fill(0));return ct.forEach((dt,Nt)=>{let yt=Ue[Y[Nt]];for(let hr=0;hr<M;hr++)yt[hr]+=dt[hr]}),Ue},Fr=ct=>{let Ue=Array.from({length:M},()=>new Array(M).fill(0));for(let dt of ct)for(let Nt=0;Nt<M;Nt++)for(let yt=0;yt<M;yt++)Ue[Nt][yt]+=dt[Nt]*dt[yt];return Ue};ke=Fr(ur(_n));let ma=Re([J]).groups[0],Dr=a===void 0?new Array(M).fill(0):a.map((ct,Ue)=>$(ct/K[Ue],Z[Ue])),Ln=p===void 0?zm(G,Dr,j):$i(G,Dr,j),Xo=ur(Ye(Ln).map((ct,Ue)=>ct.map(dt=>ma[Ue]*dt))),po=new Array(M).fill(0);for(let ct of Xo)for(let Ue=0;Ue<M;Ue++)po[Ue]+=ct[Ue];let ua=Fr(Xo);if(M===1)He=po[0]*po[0]/ua[0][0];else{Tr(ua,N);let ct=Sr(ua,po);for(let Ue=0;Ue<M;Ue++)He+=po[Ue]*ct[Ue]}let Sc=new Array(R),Ac=new Array(R);B.forEach((ct,Ue)=>{Sc[ct]=ze[Ue].map((dt,Nt)=>$(dt,-Pe[Nt])),Ac[ct]=_n[Ue].map((dt,Nt)=>$(dt,Pe[Nt]-$e))}),se={scoreResiduals:Sc,dfbeta:Ac}}let Te=ke!==void 0,pe=ke??Qe,Ae=ke===void 0?-W:-2*$e,We=Oe.map(Y=>Ze[Y]-(a===void 0?0:$(a[Y],-Pe[Y]))),Fe=0;if(tt===1)Fe=We[0]*We[0]/pe[Oe[0]][Oe[0]];else if(tt>1){let Y=Oe.map(ye=>Oe.map(Ye=>pe[ye][Ye]));Tr(Y,N);let re=Sr(Y,We);for(let ye=0;ye<tt;ye++)Fe+=We[ye]*re[ye]}Fe=$(Fe,-Ae);let nt=(1-m)/2,ut=nt<1e-8?oe(nt,{lowerTail:!1}):oe((1+m)/2),_t=(Y,re)=>{let ye=Math.floor(re/2),Ye=Y.map((ze,Vt)=>Math.sqrt($(ze[Vt],re-2*ye)));return{unit:Ye,h:ye,se:Ye.map((ze,Vt)=>$(ze,Pe[Vt]+ye))}},Bt=_t(Qe,-W).se,{unit:Wn,h:Ve,se:De}=_t(pe,Ae),Tt=ge.map((Y,re)=>$((Number.isNaN(Y)?Y:Ze[re])/Wn[re],-Ve)),pt=Tt.map(Y=>Mt(Y*Y,1,{lowerTail:!1})),En=ge.map((Y,re)=>Math.exp(Y-ut*De[re])),Pt=ge.map((Y,re)=>Math.exp(Y+ut*De[re])),qt=$(-2*(be[0]-be[1]),W),ht=[$(be[0],W),$(be[1],W)],Ut=$(Ee,W),nn=ke?.map((Y,re)=>Y.map((ye,Ye)=>$(ye,Pe[re]+Pe[Ye]-2*$e))),Zt={coefficients:ge,var:nn??ee,loglik:ht,score:Ut,wald:Fe,iter:he,means:X,n:R,nevent:I,...S===void 0?{}:{nId:new Set(y.map(Y=>S[Y])).size},linearPredictors:Je,se:Bt,z:Tt,p:pt,stdError:Bt,zValue:Tt,pValue:pt,lower:En,upper:Pt,logtest:Po(qt,tt),sctest:Po(Ut,tt),waldtest:Po(Fe,tt),rsq:{rsq:1-Math.exp(-qt/R),maxrsq:1-Math.exp(2*ht[0]/R)},usedRobust:Te,...Te?{naiveVar:ee,robustSe:De,rscore:He,robscore:Po(He,tt)}:{},warnings:x.list},Lt=ne.score.map((Y,re)=>$(Y/K[re],Z[re]+W));Object.defineProperty(Zt,"coxZphData",{enumerable:!1,value:{time:D.slice(),...A===void 0?{}:{start:A.slice()},status:y.map(Y=>t[Y]===1),weights:T.slice(),...q===void 0?{}:{strata:q.slice()},x:y.map(Y=>n[Y].slice()),sortedToInput:B.slice(),sortedTime:G.time.slice(),...G.start===void 0?{}:{sortedStart:G.start.slice()},sortedEvent:G.event.slice(),sortedWeight:G.weight.slice(),sortedX:G.x.map(Y=>Y.slice()),stratumIndex:B.map(Y=>O[Y]),stratumEnd:G.stratumEnd.slice(),betaScaled:Q.slice(),inverseCovariateScale:Ke.map((Y,re)=>$(1/Y,-Pe[re])),weightExp:W,ties:s}});let Jt=se?.scoreResiduals;if(p!==void 0&&o&&Jt===void 0){let Y=$i(G,Q,j);Jt=new Array(R),B.forEach((re,ye)=>{Jt[re]=Y[ye].map((Ye,ze)=>$(Ye/Ke[ze],-Pe[ze]))})}return{result:Zt,...se,...Jt===void 0?{}:{scoreResiduals:Jt},first:Lt,converged:Me}}var Xg=["rho"],Qg=new Set(["group","start","id","weights","ctype","stype","robust","rmean","confType","confInt"]),zi=e=>e==null||typeof e=="number"&&Number.isNaN(e);function Zg(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&&zi(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=Ot(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=sr(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=${Rt(a)}`)}}function Jg(e,t,n,r={}){V(r,Xg,"survdiff");let{rho:o=0}=r;if(!Number.isFinite(o))throw new RangeError("'rho' must be a finite number");let{obs:s,labels:i}=Zg(e,t,n),c=i.length;if(c<2)throw new RangeError("there is only 1 group");let a=new Array(c).fill(0);for(let v of s)a[v.group]++;let l=new Array(c).fill(0),f=new Array(c).fill(0),m=Array.from({length:c},()=>new Array(c).fill(0)),u=a.slice(),h=s.length,p=1;for(let v=0;v<s.length;){let E=s[v].time,b=v,M=new Array(c).fill(0),x=new Array(c).fill(0),y=0;for(;b<s.length&&s[b].time===E;){let R=s[b];x[R.group]++,R.event&&(M[R.group]++,y++),b++}if(y>0){let R=o===0?1:p**o;for(let T=0;T<c;T++)l[T]+=R*M[T],f[T]+=R*y*u[T]/h;if(h>1){let T=R*R*y*(h-y)/(h*(h-1));for(let S=0;S<c;S++)for(let k=0;k<c;k++)m[S][k]+=T*u[S]*((S===k?1:0)-u[k]/h)}p*=1-y/h}for(let R=0;R<c;R++)u[R]-=x[R];h-=b-v,v=b}let d=f.map((v,E)=>v>0?E:-1).filter(v=>v>=0),g=0,w=Math.max(d.length-1,0);if(d.length>=2){let v=d.slice(1),E=v.map(M=>l[M]-f[M]),b=v.map(M=>v.map(x=>m[M][x]));try{let M=rt(b,E);g=E.reduce((x,y,R)=>x+y*M[R],0)}catch{({chisq:g,df:w}=ew(b,E))}}let N=w===0?NaN:Mt(g,w,{lowerTail:!1});return{n:a,groups:i,obs:l,exp:f,var:m,chisq:g,df:w,pvalue:N,pValue:N,warnings:[]}}function ew(e,t){let n=un(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 Ar(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 Hn(e[r],e[s])}return e[r]}function ar(e,t,n={}){V(n,Qg,"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 d=n.robust??(i!==void 0&&Array.from(i).some(q=>q!=null&&Number.isFinite(q)&&!Number.isInteger(q)));if(typeof d!="boolean")throw new TypeError("'robust' must be a boolean");if(d&&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 g=[];for(let q=0;q<e.length;q++){let P=i?.[q];if(i&&P!=null&&typeof P!="number")throw new TypeError("weights must contain numbers");if(i&&(P==null||Number.isNaN(P)))continue;let B=P??1;if(!Number.isFinite(B))throw new RangeError("weights must be finite");if(B<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&&zi(r[q])||s!==void 0&&zi(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");g.push(q)}}if(g.length===0)throw new RangeError("no observations without missing values");let w=r===void 0?[]:Ot(r),N=s===void 0?[]:Array.from(s),v=r===void 0?["all"]:Array.from(new Set(g.map(q=>w[q]))).sort((q,P)=>typeof q=="number"&&typeof P=="number"?q-P:String(q)<String(P)?-1:String(q)>String(P)?1:0).map(q=>`group=${Rt(q)}`),E=new Map;r!==void 0&&Array.from(new Set(g.map(q=>w[q]))).sort((q,P)=>typeof q=="number"&&typeof P=="number"?q-P:String(q)<String(P)?-1:String(q)>String(P)?1:0).forEach((q,P)=>E.set(q,P));let b=new Map;s!==void 0&&Array.from(new Set(g.map(q=>N[q]))).forEach((q,P)=>b.set(q,P));let M=g.flatMap(q=>o===void 0?[e[q]]:[o[q],e[q]]),x=sr(M),y=new Map,R=new Map;g.forEach((q,P)=>{if(o===void 0)R.set(q,x[P]);else if(y.set(q,x[2*P]),R.set(q,x[2*P+1]),!(y.get(q)<R.get(q)))throw new Error("aeqSurv exception, an interval has effective length 0")});let T=g.map(q=>({time:R.get(q),start:o===void 0?-1/0:y.get(q),event:t[q]===1,group:r===void 0?0:E.get(w[q]),id:s===void 0?q:b.get(N[q]),weight:i?.[q]??1})).sort((q,P)=>q.time-P.time),S=s===void 0?void 0:new Map;if(S)for(let q of T){let P=S.get(q.id)??[];P.push(q),S.set(q.id,P)}let k=v,L=T.map(q=>q.time),_=Math.min(...L),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,C=F<1e-8?oe(F,{lowerTail:!1}):oe(1-F),D=[],A={strata:[],time:[],nRisk:[],nEvent:[],nCensor:[],surv:[],stdErr:D,stdError:D,lower:[],upper:[],cumhaz:[],stdChaz:[],logse:!d||c===2,n:[],records:[],nMax:[],nStart:[],events:[],rmean:[],seRmean:[],median:[],medianRows:k,table:{colNames:[],rowNames:k,table:[]},warnings:[]};i!==void 0&&(A.counts={nRisk:[],nEvent:[],nCensor:[]});let O=[];for(let q=0;q<k.length;q++){let P=T.filter(fe=>fe.group===q);if(!P.some(fe=>fe.weight>0))throw new Error("a group has no positive weight");let B=A.time.length,J=P.length,ce=Math.max(...P.map(fe=>fe.time)),W=0,te=0,U=1,ve=0,X=0,K=0;if(o===void 0){let fe=0;for(let ee of P)fe+=ee.weight;W=fe,te=P.length}let Z=[],z=[],G=[],j=s===void 0?void 0:new Map;if(j)for(let fe of P){let ee=j.get(fe.id)??[];ee.push(fe),j.set(fe.id,ee)}for(let fe=0;fe<P.length;){let ee=P[fe].time,ge=fe,de=0,ae=0,Je=0,Oe=te;for(;ge<P.length&&P[ge].time===ee;)ge++;let tt=P.slice(fe,ge);if(o!==void 0){let Ve=P.filter(De=>De.start<ee&&De.time>=ee);W=Ve.reduce((De,Tt)=>De+Tt.weight,0),te=Ve.length,Oe=te}let ke=tt.filter(Ve=>Ve.event),$e=tt.filter(Ve=>!Ve.event&&(S===void 0||!(S.get(Ve.id)??[]).some(De=>De.start===ee&&De.time>ee)));for(let Ve of ke)de+=Ve.weight,ae++;for(let Ve of $e)Je+=Ve.weight;let He=ke.length,se=$e.length;if(s!==void 0&&ke.length===0&&$e.length===0){fe=ge;continue}let Te=0,pe=0;if(de>0){if(c===1)Te=de/W,pe=de/(W*W);else for(let De=0;De<ae;De++){let Tt=de/ae,pt=W-De*Tt;Te+=Tt/pt,pe+=Tt/(pt*pt)}ve+=Te,K+=pe;let Ve=ae===Oe&&Oe>0?1:de/W;a===1?(U*=1-Ve,X+=Ve<1?de/W/(W-de):1/0):U=Math.exp(-ve),G.push({t:ee,d:de,n:W,e:ae,S:U})}let Ae=Math.sqrt(a===1?X:K),We=0,Fe=0;if(d){let Ve=0,De=0,Tt=j===void 0?P:Array.from(j.values(),pt=>pt[0]);for(let pt of Tt){let En=j?.get(pt.id)??[pt],Pt=0,qt=0;for(let ht of G){if(ht.t>ee)break;let Ut=En.some(Lt=>Lt.start<ht.t&&Lt.time>=ht.t),nn=En.some(Lt=>Lt.time===ht.t&&Lt.event),Zt=ht.d/ht.n;if(c===1)qt+=(Number(nn)-Number(Ut)*Zt)/ht.n;else{let Lt=0,Jt=0;for(let Y=0;Y<ht.e;Y++){let re=ht.d/ht.e,ye=ht.n-Y*re;Lt+=(ye+Y*re)/(ht.e*ye*ye),Jt-=re/(ye*ye)}qt+=Lt*Number(nn)+Jt*Number(Ut)}a===1&&Zt<1&&(Pt-=U*(Number(nn)-Number(Ut)*Zt)/((1-Zt)*ht.n))}Ve+=(pt.weight*Pt)**2,De+=(pt.weight*qt)**2}We=Math.sqrt(Ve),Fe=Math.sqrt(De)}let nt=d?c===2?a===1?We:Fe:a===1?U===0?NaN:We/U:Fe:Ae,ut=d?c===2?U*(a===1?We:Fe):a===1?We:U*Fe:U*Ae,_t=NaN,Bt=NaN;if(p!=="none")if(p==="log")_t=U===0?ut===0?0:NaN:U*Math.exp(-C*nt),Bt=U===0?ut===0?0:NaN:Math.min(1,U*Math.exp(C*nt));else if(p==="log-log")if(U===0||U===1)_t=ut===0?U:NaN,Bt=_t;else{let Ve=Math.log(-Math.log(U));_t=Math.exp(-Math.exp(Ve-C*nt/Math.log(U))),Bt=Math.exp(-Math.exp(Ve+C*nt/Math.log(U)))}else if(p==="plain")U===0&&ut===1/0?(_t=NaN,Bt=NaN):(_t=Math.max(0,U-C*ut),Bt=Math.min(1,U+C*ut));else if(p==="logit")if(U===0||U===1)_t=ut===0?U:NaN,Bt=_t;else{let Ve=Math.log(U/(1-U)),De=C*ut/(U*(1-U));_t=1-1/(1+Math.exp(Ve-De)),Bt=1-1/(1+Math.exp(Ve+De))}else if(U>0&&U<1){let Ve=Math.asin(Math.sqrt(U)),De=C*ut/(2*Math.sqrt(U*(1-U)));_t=Math.sin(Math.max(0,Ve-De))**2,Bt=Math.sin(Math.min(Math.PI/2,Ve+De))**2}else _t=NaN,Bt=NaN;let Wn=W;A.strata.push(k[q]),A.time.push(ee),A.nRisk.push(Wn),A.nEvent.push(de),A.nCensor.push(Je),A.surv.push(U),A.cumhaz.push(ve),A.stdChaz.push(d?Fe:Math.sqrt(K)),A.stdErr.push(d?c===2?U*(a===1?We:Fe):a===1?We:U*Fe:U*Ae),A.lower.push(_t),A.upper.push(Bt),A.counts&&(A.counts.nRisk.push(Oe),A.counts.nEvent.push(He),A.counts.nCensor.push(se)),o===void 0&&(W-=de+Je,te-=ge-fe),fe=ge}let Q=A.time.slice(B),ne=A.surv.slice(B),be=A.lower.slice(B),Ne=A.upper.slice(B);A.median.push([Ar(Q,ne),p==="none"?NaN:Ar(Q,be),p==="none"?NaN:Ar(Q,Ne)]),A.n.push(J),A.records.push(J);let me=A.nRisk.slice(B);A.nMax.push(Math.max(...me)),A.nStart.push(me[0]??0),A.events.push(P.reduce((fe,ee)=>fe+(ee.event?ee.weight:0),0));let le=l==="none"?NaN:l==="common"?Math.max(...L):l==="individual"?ce:l;if(l!=="none"&&!Number.isFinite(le))throw new RangeError("'rmean' must be finite");let Ee=P.filter(fe=>Number.isFinite(fe.time)).map(fe=>Math.abs(fe.time)),he=Number.isFinite(le)?Math.max(Math.abs(le),Math.abs(I),...Ee):Math.max(Math.abs(I),...Ee),Le=he===0?0:Math.max(-1074,Math.min(1023,Math.floor(Math.log2(he)))),we=he===0?1:2**Le,Me=Number.isFinite(le)&&!Number.isFinite(le-I),Se=fe=>{let ee=1;for(let ge of G)if(ge.t<=fe)ee=ge.S;else break;return ee},Ke=fe=>{let ee=1;for(let ge of G)if(ge.t/we<=fe)ee=ge.S;else break;return ee},Pe=Fn();if(Me){let fe=Array.from(new Set([I/we,...G.map(ee=>ee.t/we).filter(ee=>ee>I/we&&ee<le/we),le/we])).sort((ee,ge)=>ee-ge);for(let ee=0;ee<fe.length-1;ee++){let ge=fe[ee],de=fe[ee+1];mn(Pe,Ke(ge)*(de-ge),fe.length+1)}}else{let fe=Array.from(new Set([I,...G.map(ee=>ee.t).filter(ee=>ee>I&&ee<le),le])).sort((ee,ge)=>ee-ge);for(let ee=0;ee<fe.length-1;ee++){let ge=fe[ee],de=fe[ee+1];mn(Pe,Se(ge)*(de-ge),fe.length+1)}}let Ze=Dn(Pe)*(Me?we:1),Be=Fn();if(Number.isFinite(le))for(let fe of G){if(fe.t>le||fe.n<=fe.d)continue;let ee=Fn();if(Me){let de=Array.from(new Set([fe.t/we,...G.map(ae=>ae.t/we).filter(ae=>ae>fe.t/we&&ae<le/we),le/we])).sort((ae,Je)=>ae-Je);for(let ae=0;ae<de.length-1;ae++)mn(ee,Ke(de[ae])*(de[ae+1]-de[ae]),de.length+1)}else{let de=Array.from(new Set([fe.t,...G.map(ae=>ae.t).filter(ae=>ae>fe.t&&ae<le),le])).sort((ae,Je)=>ae-Je);for(let ae=0;ae<de.length-1;ae++)mn(ee,Se(de[ae])*((de[ae+1]-de[ae])/we),de.length+1)}let ge=Dn(ee);mn(Be,ge*ge*fe.d/(fe.n*(fe.n-fe.d)),G.length+1)}let Qe=Dn(Be);A.rmean.push(l==="none"?NaN:Ze),A.seRmean.push(l==="none"?NaN:Math.sqrt(Qe)*we),O.push(l==="none"?p==="none"?[J,A.nMax[q],A.nStart[q],A.events[q],A.median[q][0]]:[J,A.nMax[q],A.nStart[q],A.events[q],A.median[q][0],A.median[q][1],A.median[q][2]]:p==="none"?[J,A.nMax[q],A.nStart[q],A.events[q],Ze,Math.sqrt(Qe)*we,A.median[q][0]]:[J,A.nMax[q],A.nStart[q],A.events[q],Ze,Math.sqrt(Qe)*we,A.median[q][0],A.median[q][1],A.median[q][2]])}return A.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},H("survfit",A)}var tw=new Set(["newdata","confType","confInt","times","ties","weights","init","control","nocenter","confLevel","robust"]),nw=["eps","iterMax","tolerChol","tolerInf","timefix"];function rw(e,t,n,r={}){V(r,tw,"survfitCoxph"),V(Ft(r,"control"),nw,"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=je(()=>Hr(e,t,n,a)),{ties:f="efron",weights:m}=a,u=(n[0]??[]).map((U,ve)=>ow(n.map(X=>X[ve]))),h=n.map(U=>U.map((ve,X)=>u[X](ve))),p=je(()=>Hr(e,t,h,a)),d=p.coefficients.length,g=p.coefficients.map(U=>Number.isFinite(U)?U:0),w=p.var.map(U=>U.map(ve=>Number.isFinite(ve)?ve:0)),N=[];for(let U=0;U<e.length;U++)Number.isNaN(e[U])||Number.isNaN(t[U])||n[U].some(ve=>Number.isNaN(ve))||m!==void 0&&Number.isNaN(m[U])||N.push(U);let v=a.control?.timefix===!1?e.slice():sr(e);N.sort((U,ve)=>v[U]-v[ve]);let E=U=>m===void 0?1:m[U],b=U=>U.map((ve,X)=>ve-p.means[X]),M=new Map;for(let U of N)M.set(U,Math.exp(b(h[U]).reduce((ve,X,K)=>ve+X*g[K],0)));let x=[];for(let U of N)(x.length===0||x[x.length-1]!==v[U])&&x.push(v[U]);let y=x.length,R=new Array(y).fill(0),T=new Array(y).fill(0),S=Array.from({length:y},()=>new Array(d).fill(0)),k=new Array(y).fill(0),L=new Array(y).fill(0),_=new Array(y).fill(0),I=0,F=0,C=new Array(d).fill(0),D=N.length-1;for(let U=y-1;U>=0;U--){let ve=x[U],X=0,K=0,Z=0,z=new Array(d).fill(0);for(;D>=0&&v[N[D]]===ve;D--){let j=N[D],Q=E(j),ne=M.get(j),be=b(h[j]);I+=Q*ne,F+=Q;for(let Ne=0;Ne<d;Ne++)C[Ne]+=Q*ne*be[Ne];if(t[j]===1){X++,K+=Q,Z+=Q*ne;for(let Ne=0;Ne<d;Ne++)z[Ne]+=Q*ne*be[Ne]}else _[U]+=Q}if(k[U]=F,L[U]=K,X===0)continue;let G=f==="efron"?X:1;for(let j=0;j<G;j++){let Q=f==="efron"?j/X:0,ne=I-Q*Z,Ne=K/G/ne;R[U]+=Ne,T[U]+=Ne/ne;for(let me=0;me<d;me++)S[U][me]+=Ne*((C[me]-Q*z[me])/ne)}}if(o?.some(U=>U.length!==d))throw new RangeError("each row of 'newdata' must have one value per column of 'x'");let A=(o??[l.means]).map(U=>U.map((ve,X)=>u[X](ve))),O=i<1-1e-8?oe(1-(1-i)/2):oe((1-i)/2,{lowerTail:!1}),q=[],P=[],B=[],J=[],ce=[];for(let U of A){let ve=b(U),X=Math.exp(ve.reduce((Ne,me,le)=>Ne+me*g[le],0)),K=0,Z=0,z=new Array(d).fill(0),G=[],j=[],Q=[],ne=[],be=[];for(let Ne=0;Ne<y;Ne++){K+=R[Ne],Z+=T[Ne];for(let Me=0;Me<d;Me++)z[Me]+=ve[Me]*R[Ne]-S[Ne][Me];let me=0;for(let Me=0;Me<d;Me++)for(let Se=0;Se<d;Se++)me+=z[Me]*w[Me][Se]*z[Se];let le=X*K,Ee=Math.exp(-le),he=X*Math.sqrt(Z+me);be.push(le),G.push(Ee),j.push(he);let[Le,we]=sw(Ee,he,O,s);Q.push(Le),ne.push(we)}q.push(G),P.push(j),B.push(Q),J.push(ne),ce.push(be)}let W=q.map((U,ve)=>[Ar(x,U),Ar(x,B[ve]),Ar(x,J[ve])]),te={fit:l,time:x,nRisk:k,nEvent:L,nCensor:_,surv:q,stdErr:P,lower:B,upper:J,cumhaz:ce,median:W,records:N.length,nStart:k[0]??0,events:L.reduce((U,ve)=>U+ve,0),warnings:l.warnings.slice()};return c!==void 0&&(te.at=aw(te,c)),H("survfitCoxph",te)}function ow(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 sw(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 aw(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 iw=["vcov","df"],cw=["parm","level","vcov","df"];function jm(e){return typeof e=="object"&&e!==null&&!Array.isArray(e)&&Array.isArray(e.vcov)&&Array.isArray(e.names)}function Km(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 Wm=e=>new Set(e).size===e.length;function Ym(e,t){pl(e);let n=e.coefficients,r=dl(e),o=n.length,s=n.map(m=>Number.isNaN(m)),i,c,a=[],l;if(t===void 0){let m=gl(e),u=new Map(m.names.map((p,d)=>[p,d])),h=r.map((p,d)=>s[d]?void 0:u.get(p));i=h.map(p=>h.map(d=>p===void 0||d===void 0?NaN:m.vcov[p][d])),c=r,l=h.map(p=>p===void 0?NaN:Ma(m.vcov,p,m.vcov[p][p])??Math.sqrt(m.vcov[p][p]))}else{let m=typeof t=="function"?je(()=>t(e)):t,u=jm(m)?m.vcov:m;if(jm(m)){if(i=Km(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=Km(m);l=i.map((h,p)=>{let d=h[p],g=typeof u=="object"&&u!==null?Ma(u,p,d):void 0;if(g!==void 0&&!Number.isNaN(g))return g;if(!Number.isFinite(d))throw new RangeError(`the variance of coefficient ${p+1} in 'vcov' is not a finite number`);if(d<0)throw new RangeError(`the variance of coefficient ${p+1} in 'vcov' is negative`);return Math.sqrt(d)})}if(c!==void 0&&Wm(c)&&Wm(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 Bm(e,t){if(t===void 0)return Ea(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 Um=e=>Number.isFinite(e)&&e>0;function lw(e){return Ea(e)?e.priorWeights.filter(t=>t!==0).length:e.rank+e.dfResidual}function fw(e,t={}){V(t,iw,"coeftest");let{est:n,se:r,names:o,warnings:s}=Ym(e,t.vcov),i=Bm(e,t.df),c=Um(i),a=n.map((f,m)=>f/r[m]),l=a.map(f=>Number.isNaN(f)?NaN:c?2*vt(-Math.abs(f),i,!0,!1):2*ie(-Math.abs(f)));return H("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:lw(e),warnings:s},e,typeof t.vcov=="function"?void 0:t.vcov)}function mw(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 uw(e,t={}){V(t,cw,"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=Ym(e,t.vcov),o=Bm(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]=Um(o)?[xt(c,o,!0,!1),c<1e-8?xt(c,o,!1,!1):xt(1-c,o,!0,!1)]:[oe(c),c<1e-8?oe(c,{lowerTail:!1}):oe(1-c)],f=(m,u,h)=>m+h*u;return H("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:mw(c),warnings:r.warnings},e,typeof t.vcov=="function"?void 0:t.vcov)}var hw=["test","dispersion"],pw=(e,t)=>gt(e,t/2,2,!1,!1);function dw(e,t,n){let{fit:r,w:o}=br(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 ji=(e,t)=>Ii(t.x,e.fit.workingResiduals,e.fit.workingWeights,e.intercept);function gw(e,t={}){V(t,hw,"glmAnova");let n=Array.isArray(e)?e:[e];if(n.length===0)throw new RangeError("no fits to compare");let r=n.map(ys),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:ul(t.dispersion,o),c=i?.s??0,a=ww(s,Array.isArray(e)&&s.length>1,{...t,...i===void 0?{}:{dispersion:i.m}},c);if(o===0&&c===0)return H("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"),d=s[0].model.x.length;for(let g of m)g[u]=g[h]+2*(d-g[p])*t.dispersion}return H("glmAnova",{...a,table:m},...n)}function ww(e,t,n,r=0){let o=new mt,s=e[0],i=Na(s.model.familySpec);if(e.some(y=>{let R=Na(y.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 y=e[0].model.x.length;if(e.some(T=>T.model.x.length!==y))throw new RangeError("models were not all fitted to the same size of dataset");let R=(T,S)=>T.length===S.length&&T.every((k,L)=>JSON.stringify(k)===JSON.stringify(S[L]));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[k,L]=f[S]>0?[e[S-1],T]:[T,e[S-1]],_=C=>C.filter((D,A)=>k.priorWeights[A]>0&&L.priorWeights[A]>0),I=k.model.offset.map((C,D)=>C-L.model.offset[D]),F=I.some((C,D)=>C!==0&&k.priorWeights[D]>0)?k.model.x.map((C,D)=>[...C,I[D]]):k.model.x;gs(_(F),_(L.model.x))||h.add(S)}),a==="Rao"){let T=e.map(S=>({dfRes:S.dfResidual,dev:S.deviance,fit:Gm(S),x:S.model.x,intercept:S.model.intercept}));u=l.map((S,k)=>k===0?NaN:f[k]<0?-ji(T[k],T[k-1]):ji(T[k-1],T[k]))}p=e.reduce((T,S)=>S.dfResidual<T.dfResidual?S:T)}else{let y=s,R=y.model,T=R.x.length>0?R.x[0].length-(R.intercept?1:0):0,S=R.intercept?[0]:[],k=_=>[...S,...Array.from({length:_},(I,F)=>F+S.length)],L=[];for(let _=0;_<=T;_++)_===T?L.push({dfRes:y.dfResidual,dev:y.deviance,fit:Gm(y),x:R.x,intercept:R.intercept}):_>0||a==="Rao"?L.push(dw(R,k(_),o)):L.push(void 0);l.push({name:"NULL",dfRes:y.dfNull,dev:y.nullDeviance});for(let _=1;_<=T;_++)l.push({name:`x${_}`,dfRes:L[_].dfRes,dev:L[_].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:ji(L[I-1],L[I]))),p=y}e.forEach((y,R)=>{y.converged||o.add(`model ${R+1} has not converged: its deviance is not that of the maximum likelihood fit`)});let d=new mt,g=n.dispersion??yo(p,d);d.list.forEach(y=>o.add(y));let w=c?1/0:p.dfResidual,N=t?["Resid. Df","Resid. Dev","Df","Deviance"]:["Df","Deviance","Resid. Df","Resid. Dev"],v=l.map((y,R)=>t?[y.dfRes,y.dev,f[R],m[R]]:[f[R],m[R],y.dfRes,y.dev]),E=[...N],b=y=>l.map((R,T)=>{if(T===0||f[T]===0||h.has(T))return NaN;let S=et(y[T]/g*Math.sign(f[T]),-2*r);return S<0||Number.isNaN(S)?NaN:pw(S,Math.abs(f[T]))}),M=[];if(a==="Chisq"||a==="LRT")E.push("Pr(>Chi)"),M=[b(m)];else if(a==="Rao")E.push("Rao","Pr(>Chi)"),M=[u.map((y,R)=>h.has(R)?NaN:y),b(u)];else if(a==="F"){if(c&&o.add(`using F test with a '${i.family}' family is inappropriate`),w!==0){let y=l.map((R,T)=>{let S=et(m[T]/f[T]/g,-2*r);return T===0||f[T]===0||h.has(T)||S<0?NaN:S});E.push("F","Pr(>F)"),M=[y,y.map((R,T)=>Number.isNaN(R)?NaN:wt(R,Math.abs(f[T]),w,!1,!1))]}}else if(a==="Cp"){let y=p.model.x.length;E.push("Cp"),M=[l.map(R=>R.dev+2*(y-R.dfRes)*g)]}let x=v.map((y,R)=>[...y,...M.map(T=>T[R])]);return{colNames:E,rowNames:l.map(y=>y.name),table:x,warnings:o.list}}function Gm(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 bw=["type"];function Hm(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,d)=>e.weights===void 0||e.weights[d]!==0?[d]:[]),c=i.map(p=>e.weights===void 0?1:Math.sqrt(e.weights[p])),a=jt(i.map((p,d)=>s.map(g=>e.x[p][g]*c[d]))),l=il(e.weights===void 0?void 0:i.map(p=>$(e.weights[p],-o)),i.length),f=a.pivot.slice(0,a.rank),{qty:m}=vs(f.map(p=>i.map((d,g)=>zt(l[g],e.x[d][s[p]]))),i.map((p,d)=>zt(l[d],$(e.y[p],-r)))),u=[0,0],h=0;return f.forEach((p,d)=>{p>=t.length&&(u=ue(u,_e(m[d],m[d])),h++)}),{ss:u[0],df:h}}function Ki(e,t={}){if(V(t,bw,"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=Fs([e]),i=Ds([e],s)[0];if(o===0)throw new Error("residual df = 0");let c=ri(e,s),a=M=>{let x=c.map(([T,S])=>[$(T,-M),S]),y=x.reduce((T,[S])=>T+S,0),R=x.reduce((T,[S,k])=>T+S*k,0)/y;return Number.isFinite(y)&&Number.isFinite(R)?x.reduce((T,[S,k])=>T+S*(k-R)**2,0):NaN},l=0,f=a(0);if(Number.isFinite(f)||(l=Ps(c),f=a(l)),yr(c,i,l)||i===0)throw new Error("residual sum of squares is 0 (within rounding error)");let m=[...e.warnings],u=e.termLabels.length;r===2&&u===0&&e.intercept&&(m.push("the model contains only an intercept: Type III test substituted"),r=3);let h=M=>e.assign.flatMap((x,y)=>M.has(x)?[y]:[]),p=[...new Set(e.assign)],d=[],g=[],w=M=>{d.length=0;let x=[];if(r===3){if(e.coefficients.some(y=>Number.isNaN(y)))throw new Error("there are aliased coefficients in the model");for(let y of p){let R=h(new Set([y])),T=h(new Set(p.filter(S=>S!==y)));d.push(y===0?"(Intercept)":e.termLabels[y-1]),x.push({ss:Hm(e,T,R,s,M).ss,df:R.length})}}else{let y=R=>e.factors.flatMap((T,S)=>T[R-1]>0?[S]:[]);for(let R of p){if(R===0)continue;let T=y(R),S=p.filter(_=>_!==0&&_!==R&&T.every(I=>e.factors[I][_-1]>0)),k=h(new Set(p.filter(_=>_!==R&&!S.includes(_)))),L=Hm(e,k,h(new Set([R])),s,M);d.push(e.termLabels[R-1]),x.push(L)}}return x};g=w(l),l===0&&e.weights!==void 0&&g.some(M=>!Number.isFinite(M.ss))&&(l=Ps(c),g=w(l));let N=M=>$(M,2*s+l),v=$(i,-l),E=v/o,b=g.map(({ss:M,df:x})=>{let y=M/x/E;return[N(M),x,y,wt(y,x,o,!1,!1)]});return b.push([N(v),o,NaN,NaN]),H("Anova",{colNames:["Sum Sq","Df","F value","Pr(>F)"],rowNames:[...d,"Residuals"],table:b,warnings:m},e)}var Us=[0,0];var Wi=e=>e.map(t=>t.map(n=>n[0]));function Xm(e){let t=e[0]?.length??0;return Array.from({length:t},(n,r)=>e.map(o=>o[r]))}function kr(e,t){let n=t[0]?.length??0;return e.map(r=>{let o=new Array(n).fill(Us);return r.forEach((s,i)=>{let c=t[i];for(let a=0;a<n;a++)o[a]=ue(o[a],_e(s,c[a]))}),o})}function qr(e){let t=Us;return e.forEach((n,r)=>t=ue(t,n[r])),t}function Qm(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=lt(n[a][i],n[i][i]);n[a][i]=l;for(let f=i+1;f<t;f++)n[a][f]=Ce(n[a][f],_e(l,n[i][f]))}}return{a:n,perm:r,sign:o,singular:s}}function Zm(e){let{a:t,sign:n,singular:r}=Qm(e);if(r)return Us;let o=[n,0];return t.forEach((s,i)=>o=_e(o,s[i])),o}function Jm(e){let t=e.length,{a:n,perm:r,singular:o}=Qm(e);if(o)throw new Error("singular matrix");let s=Array.from({length:t},()=>new Array(t).fill(Us));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]=Ce(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]=Ce(c[a],_e(n[a][l],c[l]));c[a]=lt(c[a],n[a][a])}for(let a=0;a<t;a++)s[a][i]=c[a]}return s}var vw=["group","idata","M","X"],eu=(e,t)=>gt(e,t/2,2,!1,!1);function Yi(e){let t=e.length,n=ft(qr(e),t);return Zm(e.map(r=>r.map(o=>lt(o,n))))[0]}function Bi(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=eu(i,c),l=eu(i,c+4);return a+s*(l-a)}var Nw=(e,t)=>Array.from({length:t},(n,r)=>e.map(o=>o[r]));function yw(e,t,n){let r=e.length,o=w=>w.length===0?0:jt(Array.from({length:r},(N,v)=>w.map(E=>E[v]))).rank,s=n.length===0?void 0:jt(Array.from({length:r},(w,N)=>n.map(v=>v[N]))),i=s===void 0?[]:s.pivot.slice(0,s.rank).map(w=>n[w]),c=i.length,a=[...i,...t],l=jt(Array.from({length:r},(w,N)=>a.map(v=>v[N])));if(l.rank>o(t))throw new Error("X does not define a subspace of M");let f=l.pivot.slice(0,l.rank).map(w=>a[w]),m=w=>w.map(N=>[N,0]),u=new Array(r).fill([0,0]),h=Jn(f.map(m),u,e.map((w,N)=>e.map(v=>v[N]))).qtv,p=Array.from({length:r},(w,N)=>h.map(v=>v[N])),d=Jn(f.map(m),u,p).qtv,g=Array.from({length:l.rank-c},(w,N)=>c+N);return g.map(w=>g.map(N=>d[w][N]))}function Mw(e,t={}){V(t,vw,"mauchlyTest");let n=e.map(b=>Array.from(b)),r=n[0]?.length??0;if(r===0||n.some(b=>b.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=b=>b==null||typeof b=="number"&&Number.isNaN(b),i=n.map((b,M)=>!b.some(s)&&(o===void 0||!s(o[M])));if(n.some((b,M)=>i[M]&&b.some(x=>!Number.isFinite(x))))throw new RangeError("y must not contain infinite values");let c=Re(n.filter((b,M)=>i[M])).groups,a=c.length,l=o?.filter((b,M)=>i[M]).map(Rt)??c.map(()=>""),f=[...new Set(l)].sort(Rn),m=c.map(b=>b.map(()=>[0,0]));for(let b of f){let M=l.flatMap((x,y)=>x===b?[y]:[]);for(let x=0;x<r;x++){let y=[0,0];for(let T of M)y=ue(y,[c[T][x],0]);let R=ft(y,M.length);for(let T of M)m[T][x]=Ce([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},(b,M)=>Array.from({length:r},(x,y)=>{let R=[0,0];for(let T of m)R=ue(R,_e(T[M],T[y]));return R})),p=t.idata??{index:Array.from({length:r},(b,M)=>M+1)},d=b=>{let M=je(()=>Kr(b,p));if(M.x.length!==r)throw new RangeError("idata must have one row per column of y");return Nw(M.x,M.colNames.length)},g=t.M===void 0?Array.from({length:r},(b,M)=>Array.from({length:r},(x,y)=>y===M?1:0)):d(t.M),w=d(t.X??"~0"),N=yw(h,g,w),v=N.length;if(v===0)throw new RangeError("X spans M: the test has no dimensions");if(v===1)throw new RangeError("one tested dimension: sphericity holds trivially and the test has no degrees of freedom");if(u<v)throw new RangeError(`too few residual degrees of freedom for the test: ${u} for ${v} dimension(s)`);let E=Yi(N);return{statistic:E,statisticName:"W",pValue:Bi(E,v,u,r),method:"Mauchly's test of sphericity",warnings:[]}}var Ew=["n","d","type","sigLevel","power","alternative"],xw=["n1","n2","d","sigLevel","power","alternative"],Rw=["h","n","sigLevel","power","alternative"],Tw=["h","n1","n2","sigLevel","power","alternative"],Sw=["h","n","sigLevel","power","alternative"],Aw=["d","n","sigLevel","power","alternative"],kw=["n","r","sigLevel","power","alternative"],qw=["w","N","df","sigLevel","power"],Iw=["k","n","f","sigLevel","power"],_w=["u","v","f2","sigLevel","power"],Lw=["size"],Cw={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 nu(e="p",...t){V(ps(t),Lw,"cohenES");let n=hs(["size"],t).size??"small",r=xe(e,["p","t","r","anov","chisq","f2"]),o=xe(n,["small","medium","large"]);return{test:r,size:o,effectSize:Cw[r][["small","medium","large"].indexOf(o)],method:"Conventional effect size from Cohen (1982)"}}function Ow(e,t){if(!Gs(e)||!Gs(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 Fw(e,t){if(e.length!==t.length)throw new RangeError("'P0' and 'P1' must have the same length");if(![...e,...t].every(Gs))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+=Dt(t[r]-e[r],e[r]));return Math.sqrt(n)}function Dw(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(Gs)))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>=Pw)o+=Dt(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 Pw=2**-1022,Gs=e=>e>=0&&e<=1,Vw="\u2018sig.level\u2019 must be numeric in [0, 1]",$w="\u2018power\u2019 must be numeric in [0, 1]";function ir(e,t){let n=r=>!qe(r)&&!(typeof r=="number"&&r>=0&&r<=1);if(n(e))throw new RangeError(Vw);if(n(t))throw new RangeError($w)}function cr(e,t){if(typeof e=="number"&&Number.isNaN(e))throw new RangeError("the effect size must not be NaN");return typeof e=="string"?nu(t,e).effectSize:e}var Vo=["two.sided","less","greater"];function Hs(e,t){return e==="two.sided"?t:e==="less"?[-10,5]:[-5,10]}function ru(e,t,n,r){if(r==="less")return No(rn(n,e),e,t,!0);if(r==="greater")return No(rn(n,e,{lowerTail:!1}),e,t,!1);let o=rn(n/2,e,{lowerTail:!1});return No(o,e,t,!1)+No(-o,e,t,!0)}var tu=e=>e<1e-8?oe(e,{lowerTail:!1}):oe(1-e);function ou(e,t,n){return n==="greater"?ie(tu(t)-e,{lowerTail:!1}):n==="less"?ie(oe(t)-e):ie(tu(t/2)-e,{lowerTail:!1})+ie(oe(t/2)-e)}function zw(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 jw(e={}){V(e,Ew,"pwrTTest");let{n:t,power:n}=e,r=cr(e.d,"t"),o=e.sigLevel===void 0?.05:e.sigLevel;if(bn([t,r,n,o],"exactly one of n, d, power, and sig.level must be NULL"),ir(o,n),!qe(t)&&!(t>=2))throw new RangeError("number of observations in each group must be at least 2");let s=xe(e.type,["two.sample","one.sample","paired"]),i=xe(e.alternative,Vo),c=s==="two.sample"?2:1;i==="two.sided"&&!qe(r)&&(r=Math.abs(r));let a=(m,u,h)=>ru((m-1)*c,Math.sqrt(m/c)*u,h,i);if(qe(n))n=a(t,r,o);else{let m=n;qe(t)?t=it(u=>a(u,r,o)-m,2+1e-10,1e9):qe(r)?r=it(u=>a(t,u,o)-m,...Hs(i,[1e-7,10])):o=it(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 Kw(e={}){V(e,xw,"pwrT2nTest");let{n1:t,n2:n,power:r}=e,o=cr(e.d,"t"),s=e.sigLevel===void 0?.05:e.sigLevel;if(bn([t,n,o,r,s],"exactly one of n1, n2, d, power, and sig.level must be NULL"),ir(s,r),!qe(t)&&!(t>=2))throw new RangeError("number of observations in the first group must be at least 2");if(!qe(n)&&!(n>=2))throw new RangeError("number of observations in the second group must be at least 2");let i=xe(e.alternative,Vo);i==="two.sided"&&!qe(o)&&(o=Math.abs(o));let c=(a,l,f,m)=>ru(a+l-2,f/Math.sqrt(1/a+1/l),m,i);if(qe(r))r=c(t,n,o,s);else{let a=r;qe(t)?t=it(l=>c(l,n,o,s)-a,2+1e-10,1e9):qe(n)?n=it(l=>c(t,l,o,s)-a,2+1e-10,1e9):qe(o)?o=it(l=>c(t,n,l,s)-a,...Hs(i,[1e-7,10])):s=it(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 Ui(e,t,n,r,o,s){let{n:i,power:c}=t,a=cr(e,n==="h"?"p":"t"),l=t.sigLevel===void 0?.05:t.sigLevel;if(bn([a,i,c,l],`exactly one of ${n}, n, power, and sig.level must be NULL`),ir(l,c),!qe(i)&&!(i>=1))throw new RangeError("number of observations in each group must be at least 1");let f=xe(t.alternative,Vo);f==="two.sided"&&!qe(a)&&(a=Math.abs(a));let m=(u,h,p)=>ou(u*r(h),p,f);if(qe(c))c=m(a,i,l);else{let u=c;qe(a)?a=it(h=>m(h,i,l)-u,...Hs(f,[s,10])):qe(i)?i=it(h=>m(a,h,l)-u,o,1e9):l=it(h=>m(a,i,h)-u,1e-10,1-1e-10)}return{es:a,n:i,sigLevel:l,power:c,alternative:f}}function Ww(e={}){V(e,Rw,"pwr2pTest");let t=Ui(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 Yw(e={}){V(e,Sw,"pwrPTest");let t=Ui(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 Bw(e={}){V(e,Aw,"pwrNormTest");let t=Ui(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 Uw(e={}){V(e,Tw,"pwr2p2nTest");let{n1:t,n2:n,power:r}=e,o=cr(e.h,"p"),s=e.sigLevel===void 0?.05:e.sigLevel;if(bn([o,t,n,r,s],"exactly one of h, n1, n2, power, and sig.level must be NULL"),ir(s,r),!qe(t)&&!(t>=2))throw new RangeError("number of observations in the first group must be at least 2");if(!qe(n)&&!(n>=2))throw new RangeError("number of observations in the second group must be at least 2");let i=xe(e.alternative,Vo);i==="two.sided"&&!qe(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)=>ou(l*Math.sqrt(c(f,m)),u,i);if(qe(r))r=a(o,t,n,s);else{let l=r;qe(o)?o=it(f=>a(f,t,n,s)-l,...Hs(i,[1e-10,10])):qe(t)?t=it(f=>a(o,f,n,s)-l,2+1e-10,1e9):qe(n)?n=it(f=>a(o,t,f,s)-l,2+1e-10,1e9):s=it(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 Gw(e={}){V(e,kw,"pwrRTest");let{n:t,power:n}=e,r=cr(e.r,"r"),o=e.sigLevel===void 0?.05:e.sigLevel;if(bn([t,r,n,o],"exactly one of n, r, power, and sig.level must be NULL"),ir(o,n),!qe(t)&&!(t>=4))throw new RangeError("number of observations must be at least 4");if(!qe(r)&&!(Math.abs(r)<=1))throw new RangeError("'r' must be in [-1, 1]");let s=xe(e.alternative,Vo);s==="two.sided"&&!qe(r)&&(r=Math.abs(r));let i=(a,l,f,m)=>{let u=rn(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),d=Math.atanh(l)+l/(2*(a-1)),g=Math.atanh(p),w=ie((d-g)*Math.sqrt(a-3));return m?w+ie((-d-g)*Math.sqrt(a-3)):w},c=(a,l,f)=>s==="two.sided"?i(a,l,f,!0):i(a,s==="less"?-l:l,f,!1);if(qe(n))n=c(t,r,o),s==="less"&&(r=-r);else{let a=n;if(qe(t))t=it(l=>c(l,r,o)-a,4+1e-10,1e9);else if(qe(r)){let[l,f]=s==="two.sided"?[1e-10,.9999999999]:[-.9999999999,.9999999999];r=it(m=>c(t,m,o)-a,l,f)}else o=it(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 Hw(e){V(e,qw,"pwrChisqTest");let{N:t,power:n}=e,{df:r}=e,o=cr(e.w,"chisq"),s=e.sigLevel===void 0?.05:e.sigLevel;if(bn([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(ir(s,n),!qe(o)&&!(o>=0))throw new RangeError("w must be positive");if(!qe(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:Mt(Cn(l,r,{lowerTail:!1}),r,{ncp:f,lowerTail:!1})};if(qe(n))n=i(o,t,s);else{let c=n;qe(o)?o=it(a=>i(a,t,s)-c,1e-10,1e9):qe(t)?t=it(a=>i(o,a,s)-c,1+1e-10,1e9):s=it(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 Xw(e={}){V(e,Iw,"pwrAnovaTest");let{k:t,n,power:r}=e,o=cr(e.f,"anov"),s=e.sigLevel===void 0?.05:e.sigLevel;if(bn([t,n,o,r,s],"exactly one of k, n, f, power, and sig.level must be NULL"),ir(s,r),!qe(o)&&!(o>=0))throw new RangeError("f must be positive");if(!qe(t)&&!(t>=2))throw new RangeError("number of groups must be at least 2");if(!qe(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=zw([c,a,l,l])),h===1/0?1:wa(m,u)?ba(m,u,h,f):xn(Vr(f,m,u,{lowerTail:!1}),m,u,{ncp:h,lowerTail:!1})};if(qe(r))r=i(t,n,o,s);else{let c=r;qe(t)?t=it(a=>i(a,n,o,s)-c,2+1e-10,100):qe(n)?n=it(a=>i(t,a,o,s)-c,2+1e-10,1e9):qe(o)?o=it(a=>i(t,n,a,s)-c,1e-7,1e7):s=it(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 Qw(e={}){V(e,_w,"pwrF2Test");let{u:t,v:n,power:r}=e,o=cr(e.f2,"f2"),s=e.sigLevel===void 0?.05:e.sigLevel;if(bn([t,n,o,r,s],"exactly one of u, v, f2, power, and sig.level must be NULL"),ir(s,r),!qe(o)&&!(o>=0))throw new RangeError("f2 must be positive");if(!qe(t)&&!(t>=1))throw new RangeError("degree of freedom u for numerator must be at least 1");if(!qe(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:wa(c,a)?ba(c,a,m,f):xn(Vr(f,c,a,{lowerTail:!1}),c,a,{ncp:m,lowerTail:!1})};if(qe(r))r=i(t,n,o,s);else{let c=r;qe(t)?t=it(a=>i(a,n,o,s)-c,1+1e-10,100):qe(n)?n=it(a=>i(t,a,o,s)-c,1+1e-10,1e9):qe(o)?o=it(a=>i(t,n,a,s)-c,1e-7,1e7):s=it(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 Zw=["id","dv","between","within","type","correction","es","observed"],Xs=e=>e===void 0?[]:typeof e=="string"?[e]:[...e],Jw=1e-12,su=1e-24;function au(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 $o=e=>typeof e=="boolean"?e?"TRUE":"FALSE":Rt(e);function e1(e){let t=[...new Set(e)];return t.every(n=>typeof n=="number")?t.sort((n,r)=>n-r).map($o):[...new Set(t.map($o))].sort(Rn)}var iu=(e,t)=>t.split(":").includes(e);function cu(e,t,n,r,o){let s=l=>r.some(f=>iu(f,l));for(let l of r)if(!e.some(f=>iu(l,f.name)))throw new RangeError(`Observed variable not in data: ${l}`);let i=[...t.values()].reduce((l,f)=>l+f,0),c=e.reduce((l,f)=>s(f.name)?l+f.ss:l,0),a=["pes","ges"].filter(l=>n.includes(l));return{colNames:["num Df","den Df","MSE","F",...a,"Pr(>F)"],rowNames:e.map(l=>l.name),table:e.map(l=>{let f=a.map(m=>m==="pes"?l.ss/(l.ss+l.errSS):l.ss/(l.ss+i+c-(s(l.name)?l.ss:0)));return[l.numDf,l.denDf,o(l.errSS/l.denDf),l.f,...f,l.p]})}}function t1(e,t){V(t,Zw,"aovEz");let n=Xs(t.between),r=Xs(t.within);if(n.length===0&&r.length===0)throw new Error("Either between or within need to be non-NULL!");for(let[z,G]of[["between",n],["within",r]]){let j=G.filter((Q,ne)=>G.indexOf(Q)!==ne);if(j.length>0)throw new RangeError(`duplicated factor names in '${z}': ${[...new Set(j)].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=Xs(t.es??"ges");for(let z of c)if(z!=="ges"&&z!=="pes")throw new RangeError(`invalid 'es': ${String(z)}`);let a=Xs(t.observed),l=[],f=z=>z==null||typeof z=="number"&&Number.isNaN(z),m=au(e,t.dv),u=[t.id,...n,...r].map(z=>au(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,G)=>u.every(j=>!f(j[G]))),p=z=>z.filter((G,j)=>h[j]),d=Ot(p(u[0])).map($o),g=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}),w=n.map((z,G)=>Ot(p(u[1+G]))),N=r.map((z,G)=>Ot(p(u[1+n.length+G]))),v=d.length;for(let z of[g,...w,...N])if(z.length!==v)throw new RangeError("the columns must have the same length");let E=w.map(z=>e1(z)),b=w.map(z=>z.map($o)),M=N.map(z=>z.map($o)),x=M.map(z=>[...new Set(z)]);E.forEach((z,G)=>{if(z.length<2)throw new Error(`Factor "${n[G]}" consists of one level only. Remove factor from model?`)}),x.forEach((z,G)=>{if(z.length<2)throw new Error(`Factor "${r[G]}" consists of one level only. Remove factor from model?`)});let y=[...new Set(d)],R=new Map,T=new Set;if(d.forEach((z,G)=>{let j=JSON.stringify(b.map(ne=>ne[G])),Q=R.get(z);Q===void 0?R.set(z,j):Q!==j&&T.add(z)}),T.size>0)throw new Error(`Following ids are in more than one between subjects condition:
8
+ ${y.filter(z=>T.has(z)).join(", ")}`);let S=[[]];for(let z of x)S=S.flatMap(G=>z.map(j=>[...G,j]));let k=z=>JSON.stringify(M.map(G=>G[z])),L=new Map(S.map((z,G)=>[JSON.stringify(z),G])),_=new Set(d.map((z,G)=>k(G)));if(S.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;d.forEach((z,G)=>{let j=I.get(z);j===void 0&&I.set(z,j=new Set),j.add(k(G)),Number.isNaN(g[G])&&F.add(z)});let C=y.filter(z=>F.has(z)||I.get(z).size<S.length),D=new Set(C),A=Re([g.filter((z,G)=>!D.has(d[G]))]).k,O=A===0?g:g.map(z=>$(z,-A)),q=z=>$(z,2*A),P=S.length,B=new Map;if(d.forEach((z,G)=>{let j=B.get(z);j===void 0&&B.set(z,j={s:S.map(()=>[0,0]),n:S.map(()=>0)});let Q=L.get(k(G));j.s[Q]=ue(j.s[Q],[O[G],0]),j.n[Q]++}),[...B.values()].some(z=>z.n.some(G=>G>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."),C.length>0){let z=[...C].sort(Rn),G=z.length>=10?`${z.slice(0,10).join(", ")}, ... [showing first 10 only]`:z.join(", ");l.push(`Missing values for ${C.length} ID(s), which were removed before analysis:
9
+ ${G}`)}let J=y.filter(z=>!C.includes(z)),ce=new Map;d.forEach((z,G)=>ce.has(z)||ce.set(z,G));let W=J.map(z=>{let G=B.get(z);return G.s.map((j,Q)=>ft(j,G.n[Q]))}),te={};n.forEach((z,G)=>te[z]=J.map(j=>b[G][ce.get(j)]));let U=Object.fromEntries(n.map(z=>[z,"contr.sum"])),ve=Object.fromEntries(n.map((z,G)=>[z,E[G]])),X="Rank deficient model matrix; insufficient data to estimate full model.";if(P===1)return H("aovEz",K());return H("aovEz",Z());function K(){let z="y";for(;n.includes(z);)z+="_";let G={...te,[z]:W.map(le=>le[0][0])},j=je(()=>wr(`${z} ~ ${n.length>0?n.join(" * "):"1"}`,G,{contrasts:U,levels:ve}));if(j.rank<j.coefNames.length)throw new Error(X);if(j.dfResidual<=0)throw new RangeError(`no residual degrees of freedom: ${W.length} subject(s) for ${j.rank} parameter(s)`);let Q=je(()=>Ki(j,{type:s}));l.push(...Q.warnings.filter(le=>!l.includes(le)));let ne=j.dfResidual,be=Q.rowNames.flatMap((le,Ee)=>{if(le==="(Intercept)"||le==="Residuals")return[];let[he,Le,we,Me]=Q.table[Ee];return[{name:le,ss:he,numDf:Le,errSS:j.rss,denDf:ne,f:we,p:Me}]}),Ne=Q.colNames.indexOf("Sum Sq"),me={anova:cu(be,new Map([[0,j.rss]]),c,a,q),univariate:{colNames:Q.colNames,rowNames:Q.rowNames,table:Q.table.map(le=>le.map((Ee,he)=>he===Ne?q(Ee):Ee))},within:r,warnings:l};return Wt.set(me,{colNames:Q.colNames,rowNames:Q.rowNames,table:Q.table}),me}function Z(){let z=J.length,G=W.reduce((ke,$e)=>$e.reduce((He,se)=>He+se[0]*se[0],ke),0),j=n.length>0?je(()=>Kr(`~ ${n.join(" * ")}`,te,{contrasts:U,levels:ve})):{colNames:["(Intercept)"],assign:[0],x:J.map(()=>[1]),termLabels:[],factors:[]},Q=j.colNames.length;if(z===0||jt(j.x).rank<Q)throw new Error(X);let ne=z-Q;if(ne<=0)throw new RangeError(`no residual degrees of freedom: ${z} subject(s) for ${Q} between-subject parameter(s)`);let be=[0,...j.termLabels.map((ke,$e)=>$e+1)],Ne=["(Intercept)",...j.termLabels],me=ke=>j.assign.flatMap(($e,He)=>ke.includes($e)?[He]:[]),le=ke=>j.factors.flatMap(($e,He)=>$e[ke-1]>0?[He]:[]),Ee=ke=>j.x.map($e=>[$e[ke],0]),he=new Array(z).fill([0,0]),Le=(ke,$e,He)=>{let{qtv:se}=Jn([...ke,...$e].map(Ee),he,He);return se.map(Te=>Te.slice(ke.length,ke.length+$e.length))},we=ke=>{let{qtv:$e}=Jn(j.colNames.map((He,se)=>Ee(se)),he,ke);return $e.map(He=>He.slice(Q))},Me=Object.fromEntries(r.map((ke,$e)=>[ke,S.map(He=>He[$e])])),Se=je(()=>Kr(`~ ${r.join(" * ")}`,Me,{contrasts:Object.fromEntries(r.map(ke=>[ke,"contr.sum"])),levels:Object.fromEntries(r.map((ke,$e)=>[ke,x[$e]]))})),Ke=[0,...Se.termLabels.map((ke,$e)=>$e+1)],Pe=["(Intercept)",...Se.termLabels],Ze=ke=>ke.map($e=>ke.map(He=>{let se=[0,0];return $e.forEach((Te,pe)=>se=ue(se,_e(Te,He[pe]))),se})),Be=[],Qe=[],fe=[],ee=[],ge=[],de=[],ae=[],Je=new Map,Oe=!1;for(let ke of Ke){let $e=Se.assign.flatMap((Ve,De)=>Ve===ke?[De]:[]),He=$e.length,se=$e.map(Ve=>W.map(De=>De.reduce((Tt,pt,En)=>ue(Tt,zt(pt,Se.x[En][Ve])),[0,0]))),Te=$e.map(Ve=>$e.map(De=>[Se.x.reduce((Tt,pt)=>Tt+pt[Ve]*pt[De],0),0])),pe=Jm(Te),Ae=Ze(we(se)),We=kr(Ae,pe),Fe=qr(We)[0],nt=NaN,ut=NaN,_t=NaN,Bt=NaN,Wn=!1;if(He>=2){let Ve=Pe[ke],De=cl(Wi(pe)).map(Ut=>Ut.map(nn=>[nn,0])),Tt=Wi(kr(kr(De,Ae),Xm(De))),pt=un(Tt,{symmetric:!0,onlyValues:!0}).values,En=pt.reduce((Ut,nn)=>Math.max(Ut,nn),0),Pt=Math.max(En*Jw,G*su),qt=qr(We),ht=qr(kr(We,We));En>G*su&&ht[0]>0&&Number.isFinite(ht[0])?(Wn=!0,nt=lt(_e(qt,qt),zt(ht,He))[0],ne-He*nt>0?ut=((ne+1)*He*nt-2)/(He*(ne-He*nt)):l.push(`The Huynh-Feldt epsilon of ${Ve} is undefined (d - p GG <= 0)${i==="HF"?": corrected with GG":""}`)):l.push(`The error SSP matrix of ${Ve} is 0 (or at the rounding level of the data): no sphericity corrections (the p-values are uncorrected)`),Wn&&pt.every(Ut=>Ut>Pt)?(_t=Yi(kr(pe,Ae)),Bt=Bi(_t,He,ne,P)):Wn&&l.push(`The error SSP matrix of ${Ve} is singular: no Mauchly test (the epsilons are still defined)`)}for(let Ve=0;Ve<be.length;Ve++){let De=be[Ve],Tt;if(s===3)Tt=Le(me(be.filter(Lt=>Lt!==De)),me([De]),se);else if(De===0)Tt=Le([],me([0]),se);else{let Lt=le(De),Jt=be.filter(Y=>Y!==0&&Y!==De&&Lt.every(re=>j.factors[re][Y-1]>0));Tt=Le(me(be.filter(Y=>Y!==De&&!Jt.includes(Y))),me([De]),se)}let pt=qr(kr(Ze(Tt),pe))[0],Pt=(De===0?1:me([De]).length)*He,qt=ne*He,ht=pt/Pt/(Fe/qt),Ut=wt(ht,Pt,qt,!1,!1),nn=ke===0?Ne[Ve]:De===0?Pe[ke]:`${Ne[Ve]}:${Pe[ke]}`;Be.push([q(pt),Pt,q(Fe),qt,ht,Ut]),Qe.push([pt,Pt,Fe,qt,ht,Ut]),fe.push(nn),Je.set(ke,Fe);let Zt={name:nn,ss:pt,numDf:Pt,errSS:Fe,denDf:qt,f:ht,p:Ut};if(Wn){let Lt=Math.min(1,ut),Jt=wt(ht,nt*Pt,nt*qt,!1,!1),Y=wt(ht,Lt*Pt,Lt*qt,!1,!1);ee.push([nt,Jt,ut,Y]),ge.push([_t,Bt]),de.push(nn),i==="GG"?Zt={...Zt,numDf:nt*Pt,denDf:nt*qt,p:Jt}:i==="HF"&&(ut>1&&(Oe=!0),Zt=Number.isNaN(ut)?{...Zt,numDf:nt*Pt,denDf:nt*qt,p:Jt}:{...Zt,numDf:Lt*Pt,denDf:Lt*qt,p:Y})}ke===0&&De===0||ae.push(Zt)}}Oe&&l.push("HF eps > 1 treated as 1");let tt={anova:cu(ae,Je,c,a,q),univariate:{colNames:["Sum Sq","num Df","Error SS","den Df","F value","Pr(>F)"],rowNames:fe,table:Be},within:r,warnings:l};return ee.length>0&&(tt.corrections={colNames:["GG eps","Pr(>F[GG])","HF eps","Pr(>F[HF])"],rowNames:de,table:ee},tt.sphericity={colNames:["Test statistic","p-value"],rowNames:[...de],table:ge}),Wt.set(tt,{colNames:tt.univariate.colNames,rowNames:fe,table:Qe}),tt}}var n1=["tau","covariates","covariateNames","confLevel"];function zo(e){return 2*ie(-Math.abs(e))}function mu(e,t,n){return[e-n*t,e+n*t]}function lu(e,t,n){return{estimate:e,se:t,confInt:mu(e,t,n)}}function r1(e,t,n,r){return{estimate:e,confInt:mu(e,t,n),pValue:zo(r)}}function fu(e,t){let n=ar(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=Fn();for(let l=0;l<n.time.length&&n.time[l]<t;l++){let f=n.time[l];mn(c,(1-i)*(f-s),n.time.length+1),i=n.surv[l],s=f}mn(c,(1-i)*(t-s),n.time.length+1);let a=Dn(c);return{rmst:{estimate:r,se:o},rmtl:{estimate:a,se:o}}}function Gi(e,t,n,r){if(n==="difference"){let l=e.estimate-t.estimate,f=Math.hypot(e.se,t.se);return r1(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 o1(e){let t=new Array(e.length).fill(0);for(let n of[0,1]){let r=e.flatMap((s,i)=>s.arm===n?[i]:[]),o=ar(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 Hi(e,t,n,r,o,s){let i=e.map(_=>[_.arm,..._.x]),c=i[0].map((_,I)=>{let F=i.reduce((D,A)=>Math.max(D,Math.abs(A[I])),0);if(F===0)return 1;let C=Math.floor(Math.log2(F));return C>500||C<-500?2**C:1}),a=i.map(_=>_.map((I,F)=>I/c[F])),l=e.map((_,I)=>[1,...a[I]]),f=[1,...c],m=o1(e),u=r?er(a,n,{family:"poisson",weights:m}):wr(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]),d=u.fittedValues;if(p.some(_=>!Number.isFinite(_))||d.some(_=>!Number.isFinite(_)))throw new Error("the adjusted RMST model is singular or has non-finite fitted values");let g=l.map((_,I)=>_.map(F=>m[I]*F*(n[I]-d[I]))),w=g.map(_=>_.slice());for(let _ of[0,1]){let I=e.flatMap((C,D)=>C.arm===_?[D]:[]),F=ar(I.map(C=>e[C].time),I.map(C=>e[C].status===0?1:0),{rmean:"none"});for(let C=0;C<F.time.length;C++){let D=F.time[C],A=F.nEvent[C];if(!(D<t)||A===0)continue;let O=F.nRisk[C];if(!(O>0))continue;let q=new Array(l[0].length).fill(0);for(let B=0;B<q.length;B++){let J=Fn();for(let ce of I)e[ce].time>=D&&mn(J,g[ce][B],I.length);q[B]=Dn(J)}let P=A/O;for(let B of I){let J=e[B],ce=J.time===D&&J.status===0,W=J.time>=D;for(let te=0;te<q.length;te++)w[B][te]+=q[te]/O*(Number(ce)-(W?P:0))}}}let N=l.map((_,I)=>_.map(F=>F*Math.sqrt(r?d[I]:1))),v=Ra(N),E=Ra(w),b=rt(v),x=Ta(Ta(b,E),b).map((_,I)=>_.map((F,C)=>F/f[I]/f[C])),y=x.map((_,I)=>Math.sqrt(Math.max(0,_[I]))),R=p.map((_,I)=>_/y[I]),T=R.map(zo),S=p.map((_,I)=>_-s*y[I]),k=p.map((_,I)=>_+s*y[I]),L=r?[S.map(Math.exp),k.map(Math.exp)]:[S,k];return{coef:p,cov:x,table:{names:["intercept","arm",...o],estimate:p,se:y,z:R,pValue:T,confInt:L}}}function s1(e,t,n,r={}){if(V(r,n1,"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((W,te)=>Number.isFinite(te)?Math.max(W,Math.abs(te)):W,0),i=s>2**500?2**Math.floor(Math.log2(s)):1,a=e.every(W=>!Number.isFinite(W)||W===0||W/i!==0)&&(r.tau===void 0||r.tau===0||r.tau/i!==0)?i:1,l=W=>W/a,f=Array.from(t),m=new Set;for(let W=0;W<f.length;W++){let te=n[W]===!0?1:n[W]===!1?0:n[W];(te===0||te===1)&&f[W]===2&&m.add(te)}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(W=>[W]):r.covariates.map(W=>Array.from(W));if(p&&p.length!==e.length)throw new RangeError("'covariates' must have one row per observation");if(p&&p.some(W=>W.length!==p[0].length))throw new RangeError("'covariates' must have the same number of columns in every row");if(p&&p.some(W=>W.some(te=>typeof te!="number")))throw new TypeError("'covariates' must contain numeric values");if(p&&p.some(W=>W.some(te=>!Number.isFinite(te)&&!Number.isNaN(te))))throw new RangeError("'covariates' must be finite");let d=[],g=[],w=[],N=[];for(let W=0;W<e.length;W++){if(e[W]==null||Number.isNaN(e[W])||f[W]==null||Number.isNaN(f[W])||n[W]==null||Number.isNaN(n[W]))throw new Error("missing value where TRUE/FALSE needed");if(typeof e[W]!="number")throw new TypeError("'time' must contain numbers");if(!Number.isFinite(e[W]))throw new RangeError("'time' must be finite");if(typeof n[W]!="number"&&typeof n[W]!="boolean")throw new TypeError("'arm' must contain numbers or booleans");let te=n[W]===!0?1:n[W]===!1?0:n[W];if(te!==0&&te!==1){w.push(l(e[W]));continue}if(typeof f[W]!="number")throw new TypeError("'status' must contain numbers");let U=f[W],ve=U===1?1:0,X=p?p[W]:[];if(X.some(Z=>!Number.isFinite(Z)))continue;g.push({time:l(e[W]),status:ve,arm:te,x:X,index:W});let K=U;if(m.has(te))if(U===2)K=1;else if(U===1)K=0;else{N.includes("Invalid status value, converted to NA")||N.push("Invalid status value, converted to NA");continue}else if(K!==0&&K!==1){N.includes("Invalid status value, converted to NA")||N.push("Invalid status value, converted to NA");continue}d.push({time:l(e[W]),status:K,arm:te,x:X,index:W})}if(g.some(W=>W.time<0))throw new Error("negative follow-up times are not supported");if(!d.some(W=>W.arm===0)||!d.some(W=>W.arm===1))throw new Error("both arms 0 and 1 are required");let v=[0,1].map(W=>Math.max(...g.filter(te=>te.arm===W).map(te=>te.time))),E=[0,1].map(W=>{let te=v[W];return g.some(U=>U.arm===W&&U.time===te&&U.status===0)}),b=E.some(Boolean)?Math.min(...v.filter((W,te)=>E[te])):Math.max(...v),M=r.tau===void 0,x=r.tau===void 0?b:l(r.tau),y=r.tau===void 0?x*a:r.tau;if(w.some(W=>W<=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>b)throw new RangeError(`'tau' must be at most ${b*a}: the minimum of the largest times allowed by both arms`);if(x<Math.min(...g.map(W=>W.time)))throw new RangeError("'tau' must not be below the first observed time");let R=(1-o)/2,T=R<1e-8?oe(R,{lowerTail:!1}):oe(1-R),S=d.filter(W=>W.arm===1),k=d.filter(W=>W.arm===0),L=fu(S,x),_=fu(k,x),I=W=>({estimate:W.estimate*a,se:W.se*a}),F={rmst:I(L.rmst),rmtl:I(L.rmtl)},C={rmst:I(_.rmst),rmtl:I(_.rmtl)},D=lu(F.rmst.estimate,F.rmst.se,T),A=lu(C.rmst.estimate,C.rmst.se,T),O={estimate:F.rmtl.estimate,se:F.rmtl.se,confInt:[y-D.confInt[1],y-D.confInt[0]]},q={estimate:C.rmtl.estimate,se:C.rmtl.se,confInt:[y-A.confInt[1],y-A.confInt[0]]},P=Gi(F.rmst,C.rmst,"difference",T),B=Gi(F.rmst,C.rmst,"ratio",T),J=Gi(F.rmtl,C.rmtl,"rmtlRatio",T),ce={tau:y,tauDefault:M,confLevel:o,arm1:{rmst:D,rmtl:O},arm0:{rmst:A,rmtl:q},difference:P,ratio:B,rmtlRatio:J,warnings:N};if(p){let W=r.covariateNames===void 0?h?["covariates"]:Array.from({length:p[0].length},()=>""):Array.from(r.covariateNames);if(W.some(me=>typeof me!="string"))throw new TypeError("'covariateNames' must contain strings");if(W.length!==p[0].length)throw new RangeError("'covariateNames' must have one name per covariate column");let te=d.map(me=>({...me,time:Math.min(me.time,x),status:me.time>=x?1:me.status})),U=te.map(me=>me.time),ve=te.map(me=>x-me.time),X=Hi(te,x,U,!1,W,T),K=!(F.rmst.estimate>0&&C.rmst.estimate>0),Z=!(F.rmtl.estimate>0&&C.rmtl.estimate>0),z=K?void 0:Hi(te,x,U,!0,W,T),G=Z?void 0:Hi(te,x,ve,!0,W,T),j=(me,le)=>{let Ee=me.coef[1],he=Math.sqrt(Math.max(0,me.cov[1][1]));return le?{estimate:Math.exp(Ee),confInt:[Math.exp(Ee-T*he),Math.exp(Ee+T*he)],pValue:zo(Ee/he)}:{estimate:Ee*a,confInt:[(Ee-T*he)*a,(Ee+T*he)*a],pValue:zo(Ee/he)}},Q=me=>({...me,estimate:me.estimate.map(le=>le*a),se:me.se.map(le=>le*a),confInt:[me.confInt[0].map(le=>le*a),me.confInt[1].map(le=>le*a)]}),ne=me=>({...me,estimate:me.estimate.map((le,Ee)=>Ee===0?le+Math.log(a):le),confInt:[me.confInt[0].map((le,Ee)=>Ee===0?le+Math.log(a):le),me.confInt[1].map((le,Ee)=>Ee===0?le+Math.log(a):le)]}),be={names:["intercept","arm",...W],estimate:Array(X.coef.length).fill(NaN),se:Array(X.coef.length).fill(NaN),z:Array(X.coef.length).fill(NaN),pValue:Array(X.coef.length).fill(NaN),confInt:[Array(X.coef.length).fill(NaN),Array(X.coef.length).fill(NaN)]},Ne={estimate:NaN,confInt:[NaN,NaN],pValue:NaN};ce.adjusted={difference:j(X,!1),ratio:K?Ne:j(z,!0),rmtlRatio:Z?Ne:j(G,!0)},ce.coefficients={difference:Q(X.table),ratio:z?ne(z.table):be,rmtlRatio:G?ne(G.table):be},delete ce.arm1,delete ce.arm0}return H("rmst2",ce)}var a1=["method","confLevel","control"],i1=["eps","iterMax"];function Xi(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(g=>g.reduce((w,N,v)=>w+N*t[v],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 g=0;g<a;g++){let w=i[g],N=Math.exp(l[g]-f);for(let v=Math.min(c,g+1);v>=1;v--){let E=m[v]===0?u[v-1]:Math.max(u[v],u[v-1]),b=m[v]===0?0:2**(u[v]-E),M=N*2**(u[v-1]-E),x=m[v-1],y=h[v-1],R=p[v-1],T=h[v],S=p[v];m[v]=m[v]*b+M*x;for(let k=0;k<n;k++){let L=T[k];T[k]=L*b+M*(w[k]*x+y[k]);for(let _=0;_<n;_++)S[k][_]=S[k][_]*b+M*(w[k]*w[_]*x+w[k]*y[_]+w[_]*y[k]+R[k][_])}if(u[v]=E,m[v]>2**256||m[v]>0&&m[v]<2**-256){let k=Math.floor(Math.log2(m[v])),L=2**-k;m[v]=m[v]*L;for(let _=0;_<n;_++){T[_]=T[_]*L;for(let I=0;I<n;I++)S[_][I]=S[_][I]*L}u[v]=E+k}}}let d=0;for(let g=0;g<c;g++)d+=l[g]-f;r+=d-(Math.log(m[c])+u[c]*Math.LN2);for(let g=0;g<n;g++){let w=0;for(let v=0;v<c;v++)w+=i[v][g];let N=h[c][g]/m[c];o[g]+=w-N;for(let v=0;v<n;v++)s[g][v]+=p[c][g][v]/m[c]-N*(h[c][v]/m[c])}}return{loglik:r,score:o,info:s}}function Qi(e,t){return{statistic:e,df:t,pValue:Mt(e,t,{lowerTail:!1})}}function c1(e,t,n,r={}){V(r,a1,"clogit"),V(Ft(r,"control"),i1,"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=oe((1+s)/2),u=(O,q,P,B,J,ce,W,te)=>{let U=q.map((X,K)=>Math.sqrt(X[K])),ve=O.map((X,K)=>X/U[K]);return{coefficients:O,var:q,loglik:P,score:B,wald:J,iter:ce,n:l,nevent:e.reduce((X,K)=>X+K,0),method:W,se:U,z:ve,p:ve.map(X=>Mt(X*X,1,{lowerTail:!1})),lower:O.map((X,K)=>Math.exp(X-m*U[K])),upper:O.map((X,K)=>Math.exp(X+m*U[K])),logtest:Qi(2*(P[1]-P[0]),f),sctest:Qi(B,f),waldtest:Qi(J,f),warnings:te}};if(o!=="exact"){let O=Hr(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((P,B)=>P+B[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((P,B)=>P-h[B]))}let d=[...p.values()].map(O=>({x:[...O.cases,...O.controls],cases:O.cases.length})),g=2**-39,w=O=>{let q=O.map(P=>P.slice());return{f:q,rank:Tr(q,g)}},N=new Array(f).fill(0),v=Xi(d,N),E=[v.loglik,v.loglik],{f:b}=w(v.info),M=Sr(b,v.score),x=v.score.reduce((O,q,P)=>O+q*M[P],0),y=[],R=[N],T=v.loglik,S=N.map((O,q)=>O+M[q]),k=0,L=!1,_=0;for(_=1;_<=a;_++){v=Xi(d,S);let O=Number.isFinite(v.loglik)&&v.score.every(Number.isFinite);if(O&&Math.abs(1-T/v.loglik)<=c){L=!0,N=S;break}!O||v.loglik<T?(k++,S=S.map((q,P)=>(q+k*N[P])/(k+1)),R.push(N)):(k=0,T=v.loglik,{f:b}=w(v.info),M=Sr(b,v.score),N=S,R.push(N),S=S.map((q,P)=>q+M[P]))}L||(_=a,N=R[Math.max(1,a-1)]??N,y.push("Ran out of iterations and did not converge")),v=Xi(d,N),E[1]=v.loglik;let I=w(v.info),F=Gr(I.f),C=N.map((O,q)=>I.rank<f&&F[q][q]===0?NaN:O),D=C.map((O,q)=>Number.isNaN(O)?-1:q).filter(O=>O>=0),A=0;for(let O of D)for(let q of D)A+=N[O]*v.info[O][q]*N[q];return u(C,F,E,x,A,_,"exact",y)}var uu=["transform","terms","collapseTerms","global"],jn=e=>new Array(e).fill(0),lr=e=>Array.from({length:e},()=>jn(e));function l1(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 hu(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 f1(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=hu(i,a)-hu(s,a);l.deaths>0&&(c*=1-l.deaths/f)}return n}function m1(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=f1(e);else if(t==="rank")o=l1(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 u1(e,t={}){V(t,uu,"coxZph"),V(t,uu,"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((K,Z)=>Number.isNaN(K)?-1:Z).filter(K=>K>=0),c=i.length;if(c===0)throw new Error("the proportional-hazards test has no estimable coefficients");let a=[];for(let K=0;K<s.sortedTime.length;){let Z=K;for(;!s.stratumEnd[Z];)Z++;for(let z=K;z<=Z;){let G=s.sortedTime[z],j=z;for(;j<Z&&s.sortedTime[j+1]===G;)j++;let Q=[];for(let ne=z;ne<=j;ne++)s.sortedEvent[ne]&&Q.push(ne);s.start!==void 0&&Q.reverse();for(let ne of Q)a.push(s.sortedToInput[ne]);z=j+1}K=Z+1}if(a.length===0)throw new Error("the proportional-hazards test requires events");let l=a.map(K=>s.time[K]);if(new Set(l).size<2)throw new Error("the test needs events at two or more distinct times");let f=[...e.warnings],m=m1(s,n,a,f),u=m[a[0]];if(a.every(K=>m[K]===u))throw new Error("the transform is constant at the event times");let h=a.reduce((K,Z)=>Math.max(K,Math.abs(m[Z])),0),p=h===0?0:Math.floor(Math.log2(h)),d=m.map(K=>Number.isFinite(K)?$(K,-p):K),g=a.reduce((K,Z)=>K+d[Z],0)/a.length,w=d.map(K=>K-g);if(a.every(K=>w[K]===0))throw new Error("the transform is constant at the event times");let N=i.map(K=>s.betaScaled[K]),v=lr(c),E=lr(c),b=lr(c),M=jn(c),x=[];for(let K=0;K<s.sortedTime.length;){let Z=K;for(;!s.stratumEnd[Z];)Z++;for(let z=K;z<=Z;){let G=s.sortedTime[z],j=z;for(;j<Z&&s.sortedTime[j+1]===G;)j++;let Q=[];for(let ne=z;ne<=j;ne++)s.sortedEvent[ne]&&Q.push(ne);if(Q.length>0){let ne=[],be=-1/0;for(let ee=K;ee<=Z;ee++){if(!((s.sortedStart===void 0||s.sortedStart[ee]<G)&&s.sortedTime[ee]>=G))continue;ne.push(ee);let de=0;for(let ae=0;ae<c;ae++)de+=N[ae]*s.sortedX[ee][i[ae]];be=Math.max(be,de)}if(ne.length===0||!Number.isFinite(be))throw new Error("empty Cox proportional-hazards risk set");let Ne=jn(c),me=lr(c),le=jn(c),Ee=lr(c),he=0,Le=0;for(let ee of ne){let ge=0;for(let ae=0;ae<c;ae++)ge+=N[ae]*s.sortedX[ee][i[ae]];let de=s.sortedWeight[ee]*Math.exp(ge-be);he+=de;for(let ae=0;ae<c;ae++){let Je=s.sortedX[ee][i[ae]];Ne[ae]+=de*Je;for(let Oe=0;Oe<c;Oe++)me[ae][Oe]+=de*Je*s.sortedX[ee][i[Oe]]}if(s.sortedEvent[ee]&&s.sortedTime[ee]===G){Le+=de;for(let ae=0;ae<c;ae++){let Je=s.sortedX[ee][i[ae]];le[ae]+=de*Je;for(let Oe=0;Oe<c;Oe++)Ee[ae][Oe]+=de*Je*s.sortedX[ee][i[Oe]]}}}let we=0,Me=jn(c);for(let ee of Q){let ge=s.sortedWeight[ee];we+=ge;for(let de=0;de<c;de++)Me[de]+=ge*s.sortedX[ee][i[de]]}let Se=Q.length,Ke=s.ties==="efron"&&Se>1?Se:1,Pe=jn(c),Ze=lr(c);for(let ee=0;ee<Ke;ee++){let ge=ee/Ke,de=he-ge*Le,ae=Ne.map((Oe,tt)=>(Oe-ge*le[tt])/de);for(let Oe=0;Oe<c;Oe++)Pe[Oe]+=ae[Oe]/Ke;let Je=we/Ke;for(let Oe=0;Oe<c;Oe++)for(let tt=0;tt<c;tt++)Ze[Oe][tt]+=Je*((me[Oe][tt]-ge*Ee[Oe][tt])/de-ae[Oe]*ae[tt])}let Be=Me.map((ee,ge)=>ee-we*Pe[ge]),Qe=s.sortedToInput[Q[0]],fe=w[Qe];for(let ee=0;ee<c;ee++){M[ee]+=fe*Be[ee];for(let ge=0;ge<c;ge++)v[ee][ge]+=Ze[ee][ge],E[ee][ge]+=fe*Ze[ee][ge],b[ee][ge]+=fe*fe*Ze[ee][ge]}s.start!==void 0&&Q.reverse();for(let ee of Q){let ge=s.sortedToInput[ee],de=i.map(ae=>s.sortedX[ee][ae]);for(let ae=0;ae<c;ae++)de[ae]-=Pe[ae];x.push({input:ge,time:G,stratum:s.stratumIndex[ee],g:m[ge],residual:de})}}z=j+1}K=Z+1}let y;try{y=rt(v,E)}catch{throw new Error("the proportional-hazards information is singular")}let R=lr(c);for(let K=0;K<c;K++)for(let Z=0;Z<c;Z++){let z=0;for(let G=0;G<c;G++)z+=E[G][K]*y[G][Z];R[K][Z]=b[K][Z]-z}for(let K=0;K<c;K++)for(let Z=0;Z<K;Z++){let z=(R[K][Z]+R[Z][K])/2;R[K][Z]=z,R[Z][K]=z}let T=t.terms===!1||t.terms===void 0?i.map(K=>[K]):t.terms,S=new Set,k=[],L=[];if(T.forEach((K,Z)=>{if(!Array.isArray(K)||K.length===0)throw new RangeError("each terms group must be a non-empty array");let z=[];for(let G of K){if(!Number.isInteger(G)||G<0||G>=e.coefficients.length)throw new RangeError("terms column indices must be zero-based and within x");if(S.has(G))throw new RangeError("terms groups must not overlap");S.add(G),i.includes(G)&&z.push(G)}z.length>0&&(k.push(z),L.push(Z))}),i.forEach(K=>{S.has(K)||(k.push([K]),L.push(T.length))}),k.length===0)throw new Error("the proportional-hazards test has no estimable terms");let _=[];k.forEach((K,Z)=>{if(o&&K.length>1)_.push({col:-1,group:Z,collapsed:!0});else for(let z of K)_.push({col:z,group:Z,collapsed:!1})});let I=_.length,F=Array.from({length:c},()=>jn(I)),C=[],D=[];_.forEach((K,Z)=>{let z=k[K.group],G=L[K.group];if(K.collapsed){for(let j of z){let Q=i.indexOf(j);F[Q][Z]=N[Q]}C.push(1),D.push(`g${G+1}`)}else{let j=i.indexOf(K.col);F[j][Z]=s.inverseCovariateScale[K.col],C.push(e.coefficients[K.col]),D.push(z.length>1?`g${G+1}x${K.col+1}`:`x${K.col+1}`)}});let A=Array.from({length:I},(K,Z)=>{let z=0;for(let G=0;G<c;G++)z=Math.max(z,Math.abs(F[G][Z]));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(K=>K.map((Z,z)=>$(Z,-A[z]))),q=Array.from({length:c},()=>jn(I));for(let K=0;K<c;K++)for(let Z=0;Z<I;Z++)for(let z=0;z<c;z++)q[K][Z]+=v[K][z]*O[z][Z];let P=lr(I);for(let K=0;K<I;K++)for(let Z=0;Z<I;Z++)for(let z=0;z<c;z++)P[K][Z]+=O[z][K]*q[z][Z];for(let K=0;K<I;K++)for(let Z=0;Z<K;Z++){let z=(P[K][Z]+P[Z][K])/2;P[K][Z]=z,P[Z][K]=z}let B;try{B=rt(P)}catch{throw new Error("the proportional-hazards residual variance is singular")}for(let K=0;K<I;K++)for(let Z=0;Z<K;Z++){let z=(B[K][Z]+B[Z][K])/2;B[K][Z]=z,B[Z][K]=z}let J=B.map((K,Z)=>K.map((z,G)=>a.length*et(z,-A[Z]-A[G]-s.weightExp))),ce=x.map(K=>{let Z=jn(I);for(let z=0;z<I;z++)for(let G=0;G<c;G++)Z[z]+=O[G][z]*K.residual[G];return C.map((z,G)=>{let j=0;for(let Q=0;Q<I;Q++)j+=Z[Q]*B[Q][G];return z+a.length*et(j,-A[G]-s.weightExp)})}),W=[],te=[],U=[],ve=_.map(K=>K.collapsed?k[K.group].map(Z=>i.indexOf(Z)):[i.indexOf(K.col)]);for(let K of ve){let Z=K.map(Q=>K.map(ne=>R[Q][ne])),z=K.map(Q=>M[Q]),G;try{G=rt(Z,z)}catch{throw new Error("the proportional-hazards term information is singular")}let j=$(Math.max(0,z.reduce((Q,ne,be)=>Q+ne*G[be],0)),s.weightExp);W.push(j),te.push(K.length),U.push(Mt(j,K.length,{lowerTail:!1}))}if(r){let K;try{K=rt(R,M)}catch{throw new Error("the global proportional-hazards information is singular")}let Z=$(Math.max(0,M.reduce((z,G,j)=>z+G*K[j],0)),s.weightExp);W.push(Z),te.push(c),U.push(Mt(Z,c,{lowerTail:!1})),D.push("GLOBAL")}let X={rowNames:D,chisq:W,df:te,p:U,pValue:U,x:x.map(K=>K.g),time:x.map(K=>K.time),...s.strata===void 0?{}:{strata:x.map(K=>Rt(s.strata[K.input]))},y:ce,var:J,transform:n,warnings:f};return H("coxZph",X,e)}var h1=["center"],p1=2**-1022;function d1(e,t,n={}){V(n,h1,"leveneTest");let r=xe(n.center??"median",["median","mean"]),{groups:o}=Zn(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"?ws(l):st(l);return l.map(m=>Math.abs(m-f))},i=Re(o).groups.map(s),c=(l,f)=>l.some(m=>m>0&&m<p1)||l.every(m=>m===0)&&o[f].some(m=>m!==o[f][0]);if(i.some(c)){let l=o.map(u=>{let h=Re([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=Mm(dr(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 g1=["score"],w1=["alternative","exact","variant","levels"];function b1(e,t,...n){V(ps(n),g1,"propTrendTest");let{score:r}=hs(["score"],n),o=Array.from(e),s=Array.from(t),i=o.length,c=r===void 0?o.map((N,v)=>v+1):Array.from(r);if(s.length!==i||c.length!==i)throw new RangeError("'x', 'n' and 'score' must have the same length");if(s.some(N=>!(N>0&&N<1/0)))throw new RangeError("elements of 'n' must be positive and finite");if(o.some((N,v)=>!(N>=0&&N<=s[v])))throw new RangeError("elements of 'x' must be between 0 and 'n'");if(o.every(N=>N===0)||o.every((N,v)=>N===s[v]))throw new RangeError("all proportions are 0 or all are 1: the test for trend is not defined");let a=s.reduce((N,v)=>N+v,0),l=o.reduce((N,v)=>N+v,0)/a,f=s.map(N=>N/(l*(1-l))),m=o.map((N,v)=>N/s[v]),u=c.flatMap((N,v)=>Number.isNaN(N)?[]:[v]);if(u.length===0)throw new RangeError("no non-missing scores");if(u.some(N=>!Number.isFinite(c[N])))throw new RangeError("non-missing scores must be finite");if(u.every(N=>c[N]===c[u[0]]))throw new RangeError("at least 2 distinct non-missing scores are needed");let h=c,{sw:p,sxy:d,sxx:g}=Zi(f,m,h,u);if(!(g>=2**-1022&&g<1/0&&Math.abs(d)<1/0)){let N=Re([u.map(v=>c[v])]);N.k!==0&&(h=c.slice(),u.forEach((v,E)=>h[v]=N.groups[0][E]),{sw:p,sxy:d,sxx:g}=Zi(f,m,h,u))}let w=Dt(d,g);if(!(a<1/0)||l>0&&l<1&&!(p<1/0&&Math.abs(d)<1/0&&g<1/0)){let{groups:[N,v],k:E}=Re([o,s]),b=N.reduce((x,y)=>x+y,0)/v.reduce((x,y)=>x+y,0),M=Zi(v,m,h,u);w=$(M.sxy/b*(M.sxy/M.sxx/(1-b)),E)}return{method:"Chi-squared Test for Trend in Proportions",statistic:w,statisticName:"X-squared",parameter:1,parameterName:"df",pValue:gt(w,.5,2,!1,!1),warnings:[]}}function Zi(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 v1=2**24,N1=2**32,y1=`Sample size > 100 or data with ties
10
+ p-value based on normal approximation. Specify nperm for permutation p-value`,M1=`Sample data with ties
11
+ p-value based on normal approximation. Specify nperm for permutation p-value.`;function E1(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=Ot(t));let c;if(n!==void 0)c=n.map(Rt);else{let f=[...new Set(i.map(m=>t[m]))];c=f.every(m=>typeof m=="number")?f.sort((m,u)=>m-u).map(Rt):[...new Set(f.map(Rt))].sort(Rn)}let a=c.map(()=>[]),l=new Map(c.map((f,m)=>[f,m]));for(let f of i){let m=l.get(Rt(t[f]));if(m===void 0)throw new RangeError(`the group '${Rt(t[f])}' is not among 'levels'`);a[m].push(o[f])}return a}function x1(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 R1(e,t,n={}){V(n,w1,"jonckheereTest");let r=xe(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=E1(e,t,n.levels,o==="clinfun");if(s.filter(N=>N.length>0).length<2)throw new RangeError(o==="clinfun"?"only one group has non-missing data":"all observations are in the same group");let i=s.flat(),c=i.length,a=new Set(i).size<c,l=0,f=[],m=c;s.forEach((N,v)=>{m-=N.length,f.push([N.length,m]);for(let E=v+1;E<s.length;E++)for(let b of N)for(let M of s[E])l+=b<M?1:b===M?.5:0});let u=[],h=!a&&c<=100;o==="DescTools"&&n.exact!==void 0?(h=n.exact,h&&a&&(h=!1,u.push(M1))):h||u.push(y1);let p,d;if(h){let N=1,v=0;for(let[b,M]of f)b===0||M===0||(v+=N*(b*M+1),N+=b*M);if(N>v1||v>N1)throw new RangeError(`the exact distribution of JT has ${N} values and needs ${v} steps, too many: use exact: false`);let E=[1];for(let[b,M]of f){if(b===0||M===0)continue;let x=Array.from({length:b*M+1},(R,T)=>Kc(T,b,M,!1)),y=new Array(E.length+x.length-1).fill(0);E.forEach((R,T)=>x.forEach((S,k)=>y[T+k]+=R*S)),E=y}p=0,d=0,E.forEach((b,M)=>{M<=l&&(p+=b),M>=l&&(d+=b)})}else{let N=f.reduce((b,[M,x])=>b+M*x,0)/2,v=a?x1(s.map(b=>b.length),i):f.reduce((b,[M,x])=>b+M*x*(M+x+1)/12,0),E=(l-N)/Math.sqrt(v);p=ie(E),d=1-p,d<1e-8&&(d=ie(E,{lowerTail:!1}))}let w=r==="increasing"?d:r==="decreasing"?p:Math.min(1,2*Math.min(d,p,o==="clinfun"?.5:1));return H("jonckheereTest",{method:"Jonckheere-Terpstra test",alternative:r,statistic:l,statisticName:"JT",pValue:w,warnings:u})}var T1=["nClasses","adjust"];function jo(e){return Ct(Array.from(e)).sort((t,n)=>t-n)}function pu(e){if(e.length>0&&!(Number.isFinite(e[0])&&Number.isFinite(e[e.length-1])))throw new RangeError("'x' must be finite")}function Qs(e){pu(e);let t=ts(e);if(t.some(Number.isNaN))throw new RangeError("all 'x' values are identical");return t}function S1(e){let t=jo(e),n=t.length;if(n<8)throw new RangeError("sample size must be greater than 7");let r=Qs(t),o=0;for(let a=1;a<=n;a++)o+=(2*a-1)*(ie(r[a-1],{logP:!0})+ie(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 A1(e){let t=jo(e),n=t.length;if(n<8)throw new RangeError("sample size must be greater than 7");let r=Qs(t),o=1/(12*n);r.forEach((a,l)=>{o+=(ie(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")),H("cvmTest",{method:"Cramer-von Mises normality test",statistic:o,statisticName:"W",pValue:c,warnings:i})}function k1(e){let t=jo(e),n=t.length;if(n<5)throw new RangeError("sample size must be greater than 4");let r=Qs(t).map(f=>ie(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 q1(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(pu(t),t[0]===t[n-1])throw new RangeError("all 'x' values are identical");let r=t.map((m,u)=>oe((u+1-3/8)/(n+1/4))),o=Re([t]).groups[0],s=Math.min(Vc(o,r)**2/(ot(o)*ot(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=ie((Math.log(1-s)-a)/l,{lowerTail:!1});return{method:"Shapiro-Francia normality test",statistic:s,statisticName:"W",pValue:f,warnings:[]}}function I1(e,t={}){V(t,T1,"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 Qs(n)){let m=Math.min(o,Math.floor(1+o*ie(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:gt(a,l/2,2,!1,!1),warnings:[]}}var _1=["weight"],L1=["exact"],C1=["model","type","unit","r0","confLevel"];function Ko(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 Zs(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*ie(-Math.abs(e));function O1(e,t={}){V(t,_1,"kappa2");let n=xe(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=>Pn(x[0])),i=r.map(x=>Pn(x[1])),c=[...new Set([...s,...i])].sort((x,y)=>x<y?-1:x>y?1:0),a=c.length,l=new Map(c.map((x,y)=>[x,y])),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,y)=>Array.from({length:a},(R,T)=>m(Math.abs(y-T)))),h=f.map(x=>Ie(x)/o),p=Array.from({length:a},(x,y)=>Ie(f.map(R=>R[y]))/o),d=x=>{let y=[];for(let R=0;R<a;R++)for(let T=0;T<a;T++)y.push(x(R,T));return Ie(y)},g=d((x,y)=>f[x][y]*u[x][y])/o,w=d((x,y)=>h[x]*p[y]*u[x][y]),N=(g-w)/(1-w),v=Array.from({length:a},(x,y)=>Ie(p.map((R,T)=>R*u[y][T]))),E=Array.from({length:a},(x,y)=>Ie(h.map((R,T)=>R*u[T][y]))),b=(d((x,y)=>h[x]*p[y]*(u[x][y]-(v[x]+E[y]))**2)-w*w)/(o*(1-w)**2),M=N/Math.sqrt(b);return{value:N,statistic:M,pValue:gu(M),warnings:[]}}function F1(e,t={}){V(t,L1,"kappamFleiss");let n=t.exact??!1,r=Ko(e,"ratings");Zs(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=Ot(i),a=[...new Set(c)].sort((b,M)=>b-M),l=new Map(a.map((b,M)=>[b,M])),f=new Map(i.map((b,M)=>[b,l.get(c[M])])),m=a.length,u=e.map(b=>{let M=new Array(m).fill(0);for(let x of b)M[f.get(x)]++;return M}),h=Ie(u.map(b=>(Ie(b.map(M=>M*M))-s)/(s*(s-1))))/o,p=Array.from({length:m},(b,M)=>Ie(u.map(x=>x[M]))/(o*s)),d=Ie(p.map(b=>b*b));if(n){let b=r.map(x=>{let y=new Array(m).fill(0);for(let R of x)y[f.get(R)]++;return y.map(R=>R/o)}),M=Array.from({length:m},(x,y)=>ot(b.map(R=>R[y]))*(s-1)/s);d-=Ie(M)/(s-1)}let g=(h-d)/(1-d);if(n)return{value:g,warnings:[]};let w=Ie(p.map(b=>b*(1-b))),N=Ie(p.map(b=>b*(1-b)*(1-b-b))),v=2/(w*w*o*s*(s-1))*(w*w-N),E=g/Math.sqrt(v);return{value:g,statistic:E,pValue:gu(E),warnings:[]}}function D1(e,t={}){V(t,C1,"icc");let n=xe(t.model,["oneway","twoway"]),r=xe(t.type,["consistency","agreement"]),o=xe(t.unit,["single","average"]),s=t.r0??0,i=t.confLevel??.95;It(i),Ko(e,"ratings"),Zs(e,"ratings");let c=Re(e).groups,a=Ko(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=ot(c.map(O=>st(O)))*f,h=ot(a.map(O=>st(O)))*l,p=st(c.map(O=>ot(O))),d=ot(c.flat())*(l*f-1),g=d-u*(l-1)-h*(f-1),w=g>2**-20*d?g/((l-1)*(f-1)):z1(c)/((l-1)*(f-1)),N=(O,q,P)=>wt(O,q,P,!1,!1),v=(O,q)=>m/2<1e-8?ln(m/2,O,q,!1,!1):ln(1-m/2,O,q,!0,!1),E=l-1;if(n==="oneway"||r==="consistency"){let O=n==="oneway"?p:w,q=n==="oneway"?l*(f-1):(l-1)*(f-1),P=u/O,B=P/v(E,q),J=P*v(q,E),[ce,W,te]=o==="single"?[(u-O)/(u+(f-1)*O),P*(1-s)/(1+(f-1)*s),U=>(U-1)/(U+f-1)]:[(u-O)/u,P*(1-s),U=>1-1/U];return{value:ce,Fvalue:W,fValue:W,df1:E,df2:q,pValue:N(W,E,q),lbound:te(B),ubound:te(J),warnings:[]}}let b=o==="single"?f:1,M=O=>[b*O/(l*(1-O)),1+b*O*(l-1)/(l*(1-O))],x=(O,q)=>$c([O*h,q*w],[f-1,(l-1)*(f-1)]),y=o==="single"?(u-w)/(u+(f-1)*w+f/l*(h-w)):(u-w)/(u+(h-w)/l),[R,T]=M(s),S=u/(R*h+T*w),k=x(R,T),[L,_]=[f*y/(l*(1-y)),1+f*y*(l-1)/(l*(1-y))],I=x(L,_),F=v(E,I),C=v(I,E),[D,A]=o==="single"?[l*(u-F*w)/(F*(f*h+(f*l-f-l)*w)+l*u),l*(C*u-w)/(f*h+(f*l-f-l)*w+l*C*u)]:[l*(u-F*w)/(F*(h-w)+l*u),l*(C*u-w)/(h-w+l*C*u)];return{value:y,Fvalue:S,fValue:S,df1:E,df2:k,pValue:N(S,E,k),lbound:D,ubound:A,warnings:[]}}function du(e,t){let n=t.length,r=Ie(t.flatMap(s=>t.map(i=>e[s][i])));return(1-Ie(t.map(s=>e[s][s]))/r)*n/(n-1)}function P1(e){let t=Ko(e,"x").length;if(Zs(e,"x"),t<2)throw new RangeError("'x' needs at least two items");if(e.length<2)throw new RangeError("'x' needs at least two subjects");let n=Ms(Re(e).groups),r=Mo(e),o=Array.from({length:t},(c,a)=>a),s=Ie(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=Re(e).groups;i=o.map(l=>{let f=a.map(h=>h[l]),m=ds(jt(a.map(h=>[1,...h.filter((p,d)=>d!==l)])),f),u=st(f);return 1-Ie(m.map(h=>h*h))/Ie(f.map(h=>(h-u)**2))})}return H("cronbachAlpha",{rawAlpha:du(n,o),stdAlpha:(1-t/s)*t/(t-1),G6:1-(t-Ie(i))/s,averageR:(s-t)/(t*(t-1)),alphaDrop:o.map(c=>du(n,o.filter(a=>a!==c))),warnings:V1(e,r)})}function V1(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}=un(r.map(c=>r.map(a=>t[c][a])),{symmetric:!0}),s=o.map(c=>c[0]),i=Ie(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 Iv(e,t,n){let r=cs(e,"x").length;Ra(e,"x");for(let i of[t,n])if(!Number.isInteger(i)||i<0||i>=r)throw new RangeError(`column index ${i} out of range (0 to ${r-1})`);if(t===n)throw new RangeError("'i' and 'j' must be different columns");let o=Do(e),s=[t,n],a=Array.from({length:r},(i,l)=>l).filter(i=>i!==t&&i!==n),c=s.map(i=>s.map(l=>o[i][l]));if(a.length>0){let i=a.map(m=>a.map(u=>o[m][u])),l=a.map(m=>s.map(u=>o[m][u])),f=rt(i,l);for(let m=0;m<2;m++)for(let u=0;u<2;u++)c[m][u]-=Pe(a.map((h,p)=>o[s[m]][h]*f[p][u]))}return c[0][1]/Math.sqrt(c[0][0]*c[1][1])}function _v(e){let t=e.length,n=e[0].length,r=i=>i.reduce((l,f)=>pe(l,[f,0]),[0,0]),o=e.map(i=>ft(r(i),n)),s=e[0].map((i,l)=>ft(r(e.map(f=>f[l])),t)),a=ft(r(e.flat()),t*n),c=[0,0];return e.forEach((i,l)=>i.forEach((f,m)=>{let u=pe(De(De([f,0],o[l]),s[m]),a);c=pe(c,Ce(u,u))})),c[0]+c[1]}var Lv=["controls","cases"],Ih=["levels","direction","naRm"],Cv=["partialAuc","focus","correct"],Ov=["bestMethod","bestWeights"],Dh=new WeakMap;function Ta(e){return Dh.get(e)}var Fv=["auto","<",">"];function Bc(e){if(e==null)return null;if(typeof e=="number")return Number.isNaN(e)?null:zn(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 Dv(e){let t=e.filter(s=>Bc(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,a)=>s-a).map(zn):r==="boolean"?o=[...new Set(t)].sort((s,a)=>Number(s)-Number(a)).map(s=>s?"TRUE":"FALSE"):o=[...new Set(t)].sort((s,a)=>s<a?-1:s>a?1:0),[...new Set(o)]}function fs(e){return Float64Array.from(e).sort()}function Uc(e,t){let n=fs(e),r=fs(t),o=[],s=[],a=[],c=0,i=0;for(;c<n.length||i<r.length;){let l=i>=r.length||c<n.length&&n[c]<=r[i]?n[c]:r[i];for(;c<n.length&&n[c]===l;)c++;for(;i<r.length&&r[i]===l;)i++;o.push(l),s.push(c),a.push(i)}return{u:o,ctrlLE:s,caseLE:a}}function Hc(e,t,n){let r=0;for(let o=0;o<e.u.length;o++){let s=o>0?e.ctrlLE[o-1]:0,a=e.caseLE[o]-(o>0?e.caseLE[o-1]:0),c=e.ctrlLE[o]-s,i=n==="<"?s:t-e.ctrlLE[o];r+=a*(2*i+c)}return r}function Yc(e,t,n){let r=e.length,o=t.length;return Hc(Uc(e,t),r,n)/(2*o*r)}function Gc(e,t,n){return Hc(Uc(e,t),e.length,n)}var _h=e=>e===0n?0:e.toString(2).length;function ho(e,t){if(t<0n&&(e=-e,t=-t),e===0n)return 0;let n=e<0n,r=n?-e:e,o=55-(_h(r)-_h(t)),s=o>=0?r<<BigInt(o):r,a=o>=0?t:t<<BigInt(-o),c=s/a;c*a!==s&&(c|=1n);let i=Ve(Number(c),-o);return n?-i:i}function Pv(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 Vv(e,t,n){let r=Uc(e,t),o=e.length,s=t.length,a=r.u.length,c=[],i=[],l=[],f=m=>{let u=m===0?-1/0:m===a?1/0:Pv(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),i.push((n==="<"?s-h:h)/s),l.push((n==="<"?p:o-p)/o)};if(n==="<")for(let m=0;m<=a;m++)f(m);else for(let m=a;m>=0;m--)f(m);return{thresholds:c,sensitivities:i,specificities:l,auc:Hc(r,o,n)/(2*s*o)}}function $v(e,t){let n=r=>{let o=fs(r),s=o.length>>1;return o.length%2===1?o[s]:bn(o[s-1],o[s])};return n(e)<=n(t)?"<":">"}function Ph(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 a=n==="auto"?$v(e,t):n,i={...Vv(e,t,a),direction:a,levels:r,controls:e,cases:t,warnings:s};return Dh.set(i,o),i}function Vh(e){if(e===void 0)return"auto";if(!Fv.includes(e))throw new RangeError("'arg' should be one of \u201Cauto\u201D, \u201C<\u201D, \u201C>\u201D");return e}function mo(e,t,n){let r=Vh(n.direction),o=new mt,s=e.map(Bc),a;if(n.levels===void 0){if(a=Dv(e),a.length>2&&(o.add("'response' has more than two levels. Consider setting 'levels' explicitly or using 'multiclass.roc' instead"),a=a.slice(0,2)),a.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");a=n.levels.map(p=>{let g=Bc(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 i=[],l=[],f=[],m=[],u=[];s.forEach((p,g)=>{if(c[g])return;let d=t[g];if(p===a[0])i.push(d);else if(p===a[1])l.push(d);else return;f.push(d),m.push(p===a[1]),u.push(g)});let h={labels:s,predictor:[...t],values:f,isCase:m,kept:u};return Ph(i,l,r,[a[0],a[1]],h,o.list)}function jv(e,t){let n=Jr(Sn(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=qs(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 zv(e){return typeof e=="object"&&e!==null&&!Array.isArray(e)&&!ArrayBuffer.isView(e)}function Kv(e,t,n={}){if(ct(e)&&K(e,Lv,"roc"),typeof e!="string"&&ct(t)&&K(t,Ih,"roc"),K(n,Ih,"roc"),typeof e=="string"){let r=jv(e,t);return J("roc",mo(r.response,r.predictor,n))}if(zv(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=Vh(r.direction),a=Array.from(o.controls),c=Array.from(o.cases);if([...a,...c].some(u=>typeof u!="number"))throw new TypeError("Predictor must be numeric.");if(r.naRm===!1&&[...a,...c].some(Number.isNaN))throw new RangeError("missing values in the controls or the cases with naRm false");let i=a.filter(u=>!Number.isNaN(u)),l=c.filter(u=>!Number.isNaN(u)),f=[...i,...l],m={labels:null,predictor:f,values:f,isCase:f.map((u,h)=>h>=i.length),kept:f.map((u,h)=>h)};return J("roc",Ph(i,l,s,["0","1"],m,[]))}return J("roc",mo(Array.from(e),Array.from(t),n))}var Aa=(e,t,n,r,o)=>t[r]+(n-e[r])/(e[o]-e[r])*(t[o]-t[r]);function Wv(e,t,n,r){let o=[];if(e.forEach((i,l)=>{i>=n&&i<=r&&o.push(l)}),o.length===0){let i=e.findIndex(u=>u>=r),l=i-1,f=Aa(e,t,r,i,l),m=Aa(e,t,n,i,l);return(f+m)/2*(r-n)}let s=0;for(let i=1;i<o.length;i++){let l=o[i-1],f=o[i];s+=(e[f]-e[l])*(t[l]+t[f])/2}let a=o[o.length-1];if(e[a]!==r){let i=e.findIndex(m=>m>=r),l=i-1,f=Aa(e,t,r,i,l);s+=(r-e[l])*(t[l]+f)/2}let c=o[0];if(e[c]!==n){let i=e.findIndex(m=>m>n)-1,l=i+1,f=Aa(e,t,n,l,i);s+=(e[l]-n)*(t[l]+f)/2}return s}var vr=(e,t)=>[e[0]*t[1]+t[0]*e[1],e[1]*t[1]],Kr=(e,t)=>[e[0]*t[1]-t[0]*e[1],e[1]*t[1]],_n=(e,t)=>[e[0]*t[0],e[1]*t[1]],Lh=(e,t)=>t[0]<0n?[-e[0]*t[1],-e[1]*t[0]]:[e[0]*t[1],e[1]*t[0]],ls=(e,t)=>{let n=e[0]*t[1]-t[0]*e[1];return n<0n?-1:n>0n?1:0};function Ch(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 Oh(e,t,n,r,o,s){let a=Ch(e,n),c=Ch(t,r);if(a!==void 0&&c!==void 0)return Fh(a.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(uo),f=t.map(uo),m=0n;for(let[,h]of[...l,...f])m=h>m?h:m;let u=h=>h[0]*(m/h[1]);return Fh(l.map(u),f.map(u),m,m,o,s)}function Fh(e,t,n,r,o,s){let a=n*r,c=d=>[t[d]*n+e[d]*r-a,a],i=d=>[e[d],n],l=uo(o),f=uo(s),m=(d,v,N)=>vr(c(v),_n(Lh(Kr(d,i(v)),Kr(i(N),i(v))),Kr(c(N),c(v)))),u=[];e.forEach((d,v)=>{ls(i(v),l)>=0&&ls(i(v),f)<=0&&u.push(v)});let h;if(u.length===0){let d=e.findIndex((N,b)=>ls(i(b),f)>=0),v=d-1;h=_n(vr(m(f,v,d),m(l,v,d)),_n(Kr(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*a];let v=u[u.length-1];if(ls(i(v),f)!==0){let b=v+1;h=vr(h,_n(Kr(f,i(v)),_n(vr(c(v),m(f,v,b)),[1n,2n])))}let N=u[0];if(ls(i(N),l)!==0){let b=N-1;h=vr(h,_n(Kr(i(N),l),_n(vr(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=_n(_n(Kr(f,l),vr(f,l)),[1n,2n]),g=Lh(vr(p,h),_n(p,[2n,1n]));return ho(g[0],g[1])}function Bv(e,t={}){K(t,Cv,"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]),a=Math.min(o[0],o[1]);if(!(a>=0&&s<=1))throw new RangeError("the limits of partialAuc must be within [0, 1]");if(!(a<s))throw new RangeError("the limits of partialAuc must differ");let c=e.sensitivities,i=e.specificities,[l,f]=n==="specificity"?[i,c]:[[...c].reverse(),[...i].reverse()],m,u=e.controls.length,h=e.cases.length;return r?m=n==="specificity"?Oh(l,f,u,h,a,s):Oh(l,f,h,u,a,s):m=Wv(l,f,a,s),J("auc",{auc:m,partialAuc:[s,a],focus:n,correct:r,warnings:[]})}function uo(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 Uv(e,t){let n=fs(e.controls),r=fs(e.cases),o=(a,c,i)=>{let l=0,f=a.length;for(;l<f;){let m=l+f>>1;(i?a[m]<=c:a[m]<c)?l=m+1:f=m}return l},s=e.direction==="<";return{tp:t.map(a=>s?r.length-o(r,a,!1):o(r,a,!0)),tn:t.map(a=>s?o(n,a,!1):n.length-o(n,a,!0))}}function Hv(e,t="all",n={}){K(n,Ov,"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,a]=o;if(!(a>0&&a<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-a)/(s*a),i,l=null;if(typeof t=="string"&&(l=ke(t,["all","best"])),l!==null)i=[...e.thresholds];else{if(i=typeof t=="number"?[t]:Array.from(t),i.length===0)throw new RangeError("Numeric 'x' has length 0");if(i.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}=Uv(e,i);if(l==="best"){let[d,v]=uo(s),[N,b]=uo(a),E=(b-N)*v,w=d*N,y=BigInt(f),x=BigInt(m),M=u.map((T,q)=>{if(r==="youden")return BigInt(T)*x*w+E*BigInt(h[q])*y;let C=BigInt(f-T),_=BigInt(m-h[q]);return-(C*C*x*x*w+E*_*_*y*y)}),R=M.reduce((T,q)=>q>T?q:T),A=M.map((T,q)=>T===R?q:-1).filter(T=>T>=0);i=A.map(T=>i[T]),u=A.map(T=>u[T]),h=A.map(T=>h[T])}let p=u.map(d=>f-d),g=h.map(d=>m-d);return J("coords",{threshold:i,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 Yv=["confLevel","method","bootN","bootStratified"],$h=["method","alternative","paired","confLevel","bootN","bootStratified","levels","direction"];function qa(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]},a=e.direction==="<",c=e.cases.map(l=>{let[f,m]=s(t,l);return 2*(a?f:r-f-m)+m}),i=e.controls.map(l=>{let[f,m]=s(n,l);return 2*(a?o-f-m:f)+m});return{k10:c,k01:i}}function Jc(e,t,n,r){return at(e)/(4*n*n)/r+at(t)/(4*r*r)/n}function jh(e){let{k10:t,k01:n}=qa(e);return Jc(t,n,e.controls.length,e.cases.length)}function Sa(e,t,n,r,o,s){let a=BigInt(Gc(e,t,n)),c=BigInt(Gc(r,o,s)),i=BigInt(e.length)*BigInt(t.length),l=BigInt(r.length)*BigInt(o.length);return ho(a*l-c*i,2n*i*l)}var Qc=e=>e.sensitivities.some((t,n)=>t===1&&e.specificities[n]===1);function go(e,t){if(e.controls.length<2||e.cases.length<2)throw new Error(`${t} needs at least 2 controls and 2 cases`)}function Kh(e){let t=(1-e)/2;return t<1e-8?ie(t,{lowerTail:!1}):ie(1-t)}function Wh(e){if(!(Number.isInteger(e)&&e>=1&&e<=2**24))throw new RangeError("bootN must be an integer from 1 to 2^24")}function wo(e){let t=new Array(e.length);for(let n=0;n<e.length;n++)t[n]=e[Lo(e.length)];return t}function Zc(e){let t=Ta(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 ms(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 Gv(e,t,n,r){let o=ms(e,t,n);return o===void 0?NaN:Yc(o.controls,o.cases,r)}function po(e){let t=new Array(e);for(let n=0;n<e;n++)t[n]=Lo(e);return t}function Xv(e,t={}){K(t,Yv,"ciAuc");let n=t.confLevel??.95;fn(n);let r=ke(t.method,["delong","bootstrap"]),o=new mt;Qc(e)&&o.add("ci.auc() of a ROC curve with AUC == 1 is always 1-1 and can be misleading.");let s=1-n;if(r==="delong"){go(e,"DeLong's interval");let{k10:m,k01:u}=qa(e),h=e.auc,p=Jc(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+Kh(n)*g)];return J("ciAuc",{estimate:h,confInt:N,confLevel:n,method:r,variance:p,warnings:o.list})}let a=t.bootN??2e3;Wh(a);let c=t.bootStratified??!0,i=[],l=0;if(c){go(e,"a stratified bootstrap");for(let m=0;m<a;m++){let u=wo(e.controls),h=wo(e.cases);i.push(Yc(u,h,e.direction))}}else{let{values:m,isCase:u}=Zc(e);for(let h=0;h<a;h++){let p=Gv(m,u,po(m.length),e.direction);Number.isNaN(p)?l++:i.push(p)}}if(l>0&&o.add("NA value(s) produced during bootstrap were ignored."),i.length===0)throw new Error("no bootstrap replicate had both controls and cases");let f=$s(i,[s/2,1-s/2]);return J("ciAuc",{estimate:e.auc,confInt:[f[0],f[1]],confLevel:n,method:r,variance:i.length>1?at(i):NaN,warnings:o.list})}var zh=e=>typeof e=="object"&&e!==null&&!Array.isArray(e)&&"thresholds"in e&&"sensitivities"in e,ka=(e,t)=>e.length===t.length&&e.every((n,r)=>n===t[r]);function Qv(e,t){let n=Ta(e),r=Ta(t);if(n?.labels==null||r?.labels==null||!ka(e.levels,t.levels))return;let o=c=>c.kept.map(i=>c.labels[i]);if(ka(o(n),o(r))&&ka(n.isCase,r.isCase))return[e,t];if(!ka(n.labels,r.labels))return;let s=n.labels.map((c,i)=>i).filter(c=>n.labels[c]!==null&&!Number.isNaN(n.predictor[c])&&!Number.isNaN(r.predictor[c])),a=(c,i)=>mo(s.map(l=>i.labels[l]),s.map(l=>i.predictor[l]),{levels:c.levels,direction:c.direction});return[a(e,n),a(t,r)]}function Xc(e,t){return t==="two.sided"?2*ae(-Math.abs(e)):t==="greater"?ae(e,{lowerTail:!1}):ae(e)}function Zv(e,t,n,r){ct(n)&&K(n,$h,"rocTest"),K(r,$h,"rocTest");let o,s=new mt,a,c;if(zh(e)){if(!zh(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");a=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((T,q)=>q).filter(T=>{let q=w[T];return q!=null&&!(typeof q=="number"&&Number.isNaN(q))&&!Number.isNaN(y[T])&&!Number.isNaN(x[T])}),R=M.map(T=>w[T]),A={levels:o.levels,direction:o.direction};a=mo(R,M.map(T=>y[T]),A),c=mo(R,M.map(T=>x[T]),A);for(let T of a.warnings)s.add(T)}let i=Je(o.alternative),l=ke(o.method,["delong","bootstrap"]),f=o.confLevel??.95;fn(f),Qc(a)&&Qc(c)&&s.add("roc.test() of two ROC curves with AUC == 1 has always p.value = 1 and can be misleading.");let m=Qv(a,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&&([a,c]=m);let h={alternative:i,estimate:[a.auc,c.auc],estimateNames:["AUC of roc1","AUC of roc2"],nullValue:0,nullValueName:"difference in AUC",roc1:a,roc2:c},p=Sa(a.controls,a.cases,a.direction,c.controls,c.cases,c.direction);if(l==="delong"){if(a.direction!==c.direction&&s.add("DeLong's test should not be applied to ROC curves with a different direction."),go(a,"DeLong's test"),go(c,"DeLong's test"),u){let C=qa(a),_=qa(c),I=C.k10.map((F,k)=>F-_.k10[k]),O=C.k01.map((F,k)=>F-_.k01[k]),L=Math.sqrt(Jc(I,O,a.controls.length,a.cases.length)),D=p===0&&L===0?0:p/L,S=L===0?0:Kh(f)*L;return J("rocTest",{method:"DeLong's test for two correlated ROC curves",statistic:D,statisticName:"Z",pValue:Xc(D,i),confInt:[p-S,p+S],confLevel:f,...h,warnings:s.list})}let w=jh(a),y=jh(c),x=a.controls.length+a.cases.length,M=c.controls.length+c.cases.length,R=w+y,A,T,q;return R===0?(A=p===0?0:p>0?1/0:-1/0,T=NaN,q=Xc(A,i)):(A=p/Math.sqrt(R),T=R*R/(w*w/(x-1)+y*y/(M-1)),q=i==="two.sided"?2*wt(-Math.abs(A),T,!0,!1):i==="greater"?wt(A,T,!1,!1):wt(A,T,!0,!1)),J("rocTest",{method:"DeLong's test for two ROC curves",statistic:A,statisticName:"D",parameter:T,parameterName:"df",pValue:q,...h,warnings:s.list})}let g=o.bootN??2e3;Wh(g);let d=o.bootStratified??!0,v=[],N=0;if(d){go(a,"a stratified bootstrap"),go(c,"a stratified bootstrap");for(let w=0;w<g;w++)if(u){let y=po(a.controls.length),x=po(a.cases.length);v.push(Sa(y.map(M=>a.controls[M]),x.map(M=>a.cases[M]),a.direction,y.map(M=>c.controls[M]),x.map(M=>c.cases[M]),c.direction))}else{let y=wo(a.controls),x=wo(a.cases),M=wo(c.controls),R=wo(c.cases);v.push(Sa(y,x,a.direction,M,R,c.direction))}}else{let w=Zc(a),y=Zc(c);for(let x=0;x<g;x++){let M,R;if(u){let A=po(w.values.length);M=ms(w.values,w.isCase,A),R=ms(y.values,y.isCase,A)}else M=ms(w.values,w.isCase,po(w.values.length)),R=ms(y.values,y.isCase,po(y.values.length));M===void 0||R===void 0?N++:v.push(Sa(M.controls,M.cases,a.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:Xc(E,i),bootStratified:d,...h,warnings:s.list})}var Jv=["method","confLevel","prevalence"];function eN(e,t={}){K(t,Jv,"diagnosticTests");let n=ke(t.method,["exact","wilson","jeffreys"]),r=t.confLevel??.95;if(Lt(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(Mn))throw new RangeError("the counts must be nonnegative integers");let[s,a,c,i]=o.map(Math.round),l=s+c,f=a+i,m=s+a,u=c+i,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=Ye(()=>ki(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=i/f,E={sensitivity:p(s,l),specificity:p(i,f),ppv:p(s,m),npv:p(i,u),lrPos:v(a===0?N/0:ho(BigInt(s)*BigInt(f),BigInt(l)*BigInt(a)),c/(s*l)+i/(a*f)),lrNeg:v(i===0?c/l/b:ho(BigInt(c)*BigInt(f),BigInt(l)*BigInt(i)),s/(c*l)+a/(i*f)),accuracy:p(s+i,h),diagnosticOR:v(s*i/(a*c),1/s+1/a+1/c+1/i),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=a/f*((1-w)*y),R=b*(1-w),A=c/l*w;E.ppvAtPrevalence=x/(x+M),E.npvAtPrevalence=R/(R+A)}return J("diagnosticTests",E)}var tN=["two","mode","confLevel","ciMethod"],nN=["package","exact"],Ln=(e,t)=>t===0?0:e*t;function rN(e,t,n={}){if(K(n,tN,"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;fn(s);let a=ke(n.ciMethod,nN),c=[],i=[];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),i.push(S)}}let l=c.length;if(l<2)throw new RangeError("fewer than 2 complete pairs");let[f,m]=o===1?[c,i]:[i,c],u=c.map((L,D)=>{let S=(L+i[D])/2;return Number.isFinite(S)?S:L/2+i[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=tr(v),w=r*E,y=b-w,x=b+w,M=l-1,R=(1-s)/2,A=bt(R,M,!0,!1),T=R<1e-8?bt(R,M,!1,!1):bt((s+1)/2,M,!0,!1),q=[b+Ln(A,E)/Math.sqrt(l),b+Ln(T,E)/Math.sqrt(l)],C,_;if(a==="package"){let L=3*E*E/l,D=L>=2**-1022||L===0?Math.sqrt(L):E*Math.sqrt(3/l);C=[y+Ln(A,D),y+Ln(T,D)],_=[x+Ln(A,D),x+Ln(T,D)]}else{let L=r*Math.sqrt(l),D=Ua(R,M,L,!0,!1)/Math.sqrt(l),S=Ua(R,M,L,!1,!1)/Math.sqrt(l);C=[b-Ln(S,E),b-Ln(D,E)],_=[b+Ln(D,E),b+Ln(S,E)]}let I=L=>$(L,N),O=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:O(q),lowerLimitConfInt:O(C),upperLimitConfInt:O(_),confLevel:s,two:r,mode:o,ciMethod:a,warnings:[]})}var oN=new Set(["methodReg","methodCi","methodBootstrapCi","errorRatio","confLevel","slopeMeasure","nsamples","nnested","rngSeed","iterMax","threshold","naRm"]),sN=["PaBa","Deming","WDeming"],aN=["analytical","jackknife","bootstrap","nestedbootstrap"],iN=["quantile","Student","BCa","tBoot"],Bh=5793,cN=2**27;function nl(e,t){let n=e-t;return Math.abs(n)<1e-12*(Math.abs(e)+Math.abs(t))/2?0:n}function lN(e,t){let n=er(e),r=er(t),o=[0,0],s=[0,0],a=[0,0];for(let c=0;c<e.length;c++){let i=De([e[c],0],n),l=De([t[c],0],r);o=pe(o,Ce(i,i)),s=pe(s,Ce(l,l)),a=pe(a,Ce(i,l))}return{u:o,q:s,p:a,mx:n,my:r}}function Gh(e,t,n,r){if(n===0)return NaN;if(r<=1){let a=r*t-e,c=Math.sqrt(a*a+4*r*n*n);return a>=0?(a+c)/(2*r*n):2*n/(c-a)}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 rl(e,t,n){let r=e.length,{u:o,q:s,p:a,mx:c,my:i}=lN(e,t),l=Gh(o[0],s[0],a[0],n),f=De(i,Ce([l,0],c))[0],m=Ce(o,s),u=a[0]*a[0]/(o[0]*s[0]),h=De(m,Ce(a,a))[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 Uh(e,t,n,r,o,s,a,c,i){let l=e.length,f=rl(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 F=t[S]-(m+u*e[S]),k=e[S]+n*u*F/y,P=t[S]-F/y;p[S]=r>1?k/r+P:k+r*P,x=Math.min(x,Math.abs(p[S]))}let M=[0,0],R=[0,0],A=[0,0];for(let S=0;S<l;S++){let F=x/p[S],k=[F*F,0];M=pe(M,k),R=pe(R,Ce(k,[e[S],0])),A=pe(A,Ce(k,[t[S],0]))}let T=ft(R,M[0]+M[1]),q=ft(A,M[0]+M[1]),C=[0,0],_=[0,0],I=[0,0];for(let S=0;S<l;S++){let F=ft(De([e[S],0],T),p[S]),k=ft(De([t[S],0],q),p[S]);C=pe(C,Ce(F,F)),_=pe(_,Ce(k,k)),I=pe(I,Ce(F,k))}let O=Gh(C[0],_[0],I[0],n),L=De(q,Ce([O,0],T))[0];h=T[0];let D=$(Math.abs(u-O),i-c)<a&&$(Math.abs(m-L),i)<a;if(m=L,u=O,D){g=w+1;break}}let[d,v]=el(o),[N,b]=el(1+o),E=p.map(w=>{if(w===0)return 1/0;let[y,x]=el(Math.abs(w));return r>1?Ve((N/(d*y))**2,2*(b-v-x-i)):Ve((N/y)**2,2*(b-x-c))});return{b0:m,b1:u,xmean:h,weights:E,converged:g<s}}function el(e){let t=Math.floor(Math.log2(e));return[Ve(e,-t),t]}var No=e=>Math.atan(e),vo=e=>Number.isFinite(e)?e:Math.tan(No(e));function fN(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),a=0,c=0,i=0,l=Math.PI/4;for(let m=0;m<n;m++)for(let u=m+1;u<n;u++){let h=nl(e[u],e[m]),p=nl(t[u],t[m]),g;if(h!==0)g=p/h;else if(p!==0)g=o?1/0:-1/0;else continue;s[a++]=g;let d=No(g);o?d===-l?c++:d<-l&&i++:d===l?c++:d>l&&i++}return{sorted:s.subarray(0,a).sort(),offset:o?2*i+c:-(2*i+c),e:c}}function mN(e,t,n){let r=e.length;if(t%2!==0){let c=(t+1)/2;return c>=1&&c<=r?vo(e[c-1]):NaN}let o=t/2;if(o<1||o+1>r)return NaN;let s=e[o-1],a=e[o];return n?(vo(s)+vo(a))/2:Math.tan((No(s)+No(a))/2)}function uN(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 ol(e,t,n){return Number.isFinite(n)?uN(e.map((r,o)=>nl(t[o],n*r))):-n}function Hh(e,t,n){let r=fN(e,t),o=mN(r.sorted,r.sorted.length+r.offset,n);return{b0:Number.isNaN(o)?NaN:ol(e,t,o),b1:o,slopes:r}}function hN(e,t,n,r,o){let s=e.length,{sorted:a,offset:c,e:i}=n,l=a.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*i+1:d>l)return NaN;if(p%2===0){if(d+1>l)return NaN;let v=a[d-1],N=a[d];return o?(vo(v)+vo(N))/2:Math.tan((No(v)+No(N))/2)}return vo(a[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:[ol(e,t,h),ol(e,t,u)],defined:!0}}function pN(e,t,n,r){let o=e.length,s=e.map((p,g)=>t[g]-n-r*p),a=s.filter(p=>p>0).length,c=s.filter(p=>p<0).length,i=Math.sqrt(c/a),l=-Math.sqrt(a/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?i:g<0?l:0,h=Math.max(h,Math.abs(u))}return{nPos:a,nNeg:c,maxCusum:h}}var Ia=e=>e.every(t=>t===e[0]);function tl(e,t){if(!(typeof e=="number"&&Number.isInteger(e)&&e>=1))throw new RangeError(`'${t}' must be a positive integer`);return e}var bo=(e,t)=>$s(e,t),Yh=e=>tr(e.filter(t=>!Number.isNaN(t)));function dN(e,t,n={}){K(n,oN,"mcreg");let r=ke(n.methodReg,sN),o=ke(n.methodCi??(r==="WDeming"?"jackknife":"analytical"),aN),s=o==="bootstrap"||o==="nestedbootstrap",a=ke(n.methodBootstrapCi,iN),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=tl(n.nsamples??999,"nsamples"),h=tl(n.nnested??25,"nnested"),p=tl(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"&&a==="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 V=e[Z],Y=t[Z];if(typeof V!="number"||typeof Y!="number")throw new TypeError("'x' and 'y' must be numeric");if(Number.isNaN(V)||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(V)||!Number.isFinite(Y))throw new RangeError("'x' and 'y' must not contain infinite values");d.push(V),v.push(Y)}let N=d.length;if(N<=2)throw new RangeError("mcreg needs at least 3 complete pairs");if(Ia(d)||Ia(v))throw new RangeError("'x' and 'y' must not be constant");if(r==="PaBa"){if(d.some(V=>V<0)||v.some(V=>V<0))throw new RangeError("Passing-Bablok regression needs nonnegative values");if(N>Bh)throw new RangeError(`Passing-Bablok regression is limited to ${Bh} points (2^24 pairwise slopes)`);if((1+(o==="jackknife"||s&&a==="BCa"?N:0)+(s?u*(1+(o==="nestedbootstrap"?h:0)):0))*(N*(N-1)/2)>cN)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),A=new Set,T=`no konvergence after ${p} iterations`,q=(Z,V)=>{if(r==="Deming")return rl(Z,V,M);if(r==="WDeming"){let Y=Uh(Z,V,M,R,m,p,g,E,w);return Y.converged||A.add(T),Y}return Hh(Z,V,l)},C,_=NaN,I,O,L;if(r==="Deming"){let Z=rl(y,x,M);C=Z,_=$(Z.xmean,E),O=[Z.se0,Z.se1]}else if(r==="WDeming"){let Z=Uh(y,x,M,R,m,p,g,E,w);Z.converged||A.add(T),C=Z,_=$(Z.xmean,E),I=Z.weights}else{let Z=Hh(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=bt(D/2,N-2,!1,!1),F=ie(D/2,{lowerTail:!1}),k=[C.b0,C.b1],P=[NaN,NaN],B=[[NaN,NaN],[NaN,NaN]],Q,re,z,ee=()=>{let Z=[],V=[];for(let Y=0;Y<N;Y++){let j=q(y.filter((H,W)=>W!==Y),x.filter((H,W)=>W!==Y));Z.push(j.b0),V.push(j.b1)}Q=[Z,V],re=[(N-1)*tr(Z)/Math.sqrt(N),(N-1)*tr(V)/Math.sqrt(N)]};if(o==="analytical")if(r==="Deming")for(let Z of[0,1])P[Z]=O[Z],B[Z]=[k[Z]-S*P[Z],k[Z]+S*P[Z]];else{let Z=hN(y,x,L,F,l);Z.defined||A.add("the Passing-Bablok interval is not defined for so few points: its limits are -Inf and Inf"),B[0]=Z.intercept,B[1]=Z.slope}else if(o==="jackknife"){ee();for(let Z of[0,1])P[Z]=re[Z],B[Z]=[k[Z]-S*P[Z],k[Z]+S*P[Z]]}else{if(a==="BCa"&&(ee(),Q.some(we=>we.some(Number.isNaN))))throw new RangeError("Too many identical data points!");let Z=r==="Deming"?O:r==="WDeming"&&re!==void 0?re:[NaN,NaN],V=o==="nestedbootstrap",Y=[],j=[],H=[],W=[],te=0,se=()=>Array.from({length:N},()=>Lo(N)),Me=we=>{let Fe=we.map(_e=>y[_e]),xe=we.map(_e=>x[_e]);if(Ia(Fe)||Ia(xe)){if(r==="PaBa")return A.add("Resampling PaBa regression: x or y coordinates of all data points in subsample identical - regression coefficients undetermined!"),{b0:NaN,b1:NaN};A.add("Resampling Deming regression: all x coordinates in subsample identical - regression coefficients undetermined!")}return q(Fe,xe)},Ne=()=>{for(let we=0;we<u;we++){let Fe=se(),xe=Me(Fe),_e;if(V){let Oe=[],Be=[];for(let je=0;je<h;je++){let tt=Me(se().map(Ue=>Fe[Ue]));Oe.push(tt.b0),Be.push(tt.b1)}_e=[Yh(Oe),Yh(Be)]}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),j.push(xe.b1),H.push(_e[0]),W.push(_e[1])}};if(n.rngSeed!==void 0){let we=jl()?Wl():void 0;try{Kl({kind:"Mersenne-Twister"}),zl(n.rngSeed),Ne()}finally{we!==void 0&&Bl(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,j],qe=[H,W];for(let we of[0,1]){let Fe=ce[we],xe=k[we];if(a==="quantile")B[we]=[bo(Fe,D/2),bo(Fe,1-D/2)];else if(a==="Student")P[we]=tr(Fe),B[we]=[xe-S*P[we],xe+S*P[we]];else if(a==="BCa"){let _e=Fe.filter(fe=>fe<xe).length;Fe.every(fe=>fe===xe)?_e=de/2:_e===0?_e=.5:_e===de&&(_e=de-.5);let Oe=ie(_e/de),Be=Q[we],je=Be.reduce((fe,nt)=>fe+nt,0)/Be.length,tt=0,Ue=0;for(let fe of Be){let nt=je-fe;tt+=nt*nt,Ue+=nt*nt*nt}let Qe=tt===0?0:Ue/(6*tt**1.5),ge=-F,oe=F,ve=ae(Oe-(ge-Oe)/(1+Qe*(ge-Oe))),be=ae(Oe-(oe-Oe)/(1+Qe*(oe-Oe)));B[we]=[bo(Fe,be),bo(Fe,ve)]}else{let _e=Fe.map((Be,je)=>(Be-xe)/qe[we][je]),Oe=Number.isNaN(Z[we])?tr(Fe):Z[we];P[we]=Oe,B[we]=[xe-bo(_e,1-D/2)*Oe,xe-bo(_e,D/2)*Oe]}}z={intercept:Y.map(we=>$(we,w)),slope:j.map(we=>$(we,w-E)),sigmaIntercept:H.map(we=>$(we,w)),sigmaSlope:W.map(we=>$(we,w-E)),globalSigma:[$(Z[0],w),$(Z[1],w-E)],nsamples:de}}let U=Z=>{let V=Y=>$(Y,Z===0?w:w-E);return{estimate:V(k[Z]),se:V(P[Z]),confInt:[V(B[Z][0]),V(B[Z][1])]}},ue={methodReg:r,methodCi:o,...s?{methodBootstrapCi:a}:{},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))}},...z===void 0?{}:{bootstrap:z},...r==="PaBa"?{cusum:pN(y,x,C.b0,C.b1)}:{},warnings:[...A]};return J("mcreg",ue)}var gN=["alpha","confLevel","lmer"],wN={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 _a(e,t){let n=e-t,r=n-e;return[n,e-(n-r)+(-t-r)]}function bN(e){let t=e[0];return e.every(n=>n.every((r,o)=>{let[s,a]=_a(r,n[0]),[c,i]=_a(t[o],t[0]);return s===c&&a===i}))}var Xh=(e,t)=>e.every((n,r)=>n===t[r]);function Wr(e){let t=[0,0];for(let n of e)t=pe(t,n);return ft(t,e.length)}var yo=(e,t)=>({v:ft(e.v,t),e:e.e});function Mo(...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 Qh(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 Zh=(e,t)=>Ve(e.v[0]/t.v[0],e.e-t.e),vN=(e,t)=>{let[n,r]=Mo(e,t);return n.at<r.at},NN=(e,t)=>{let[n,r]=Mo(e,t);return n.at<=r.at};function Jh(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 yN(e,t={}){K(t,gN,"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,Lt(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 a=e.length;if(a<2||s<2)throw new RangeError("at least 2 subjects and 2 raters are needed");let{groups:c}=Ie(e);if(c.every(ne=>Xh(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 i=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)=>_a(ye,m[0][me]))),h=m.map(ne=>ne.map(ye=>_a(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=Jh(E.map(Wr),Wr(E.flat()),s),R=i.every(ne=>Xh(ne,i[0]))?[0,0]:Jh(x(w).map(Wr),Wr(w.flat()),a),A=[0,0];if(!bN(c)){let ne=y.map(Wr),ye=x(y).map(Wr),me=Wr(y.flat());y.forEach((Se,Ke)=>Se.forEach(($e,st)=>{let ut=pe(De(De($e,ne[Ke]),ye[st]),me);A=pe(A,Ce(ut,ut))}))}let T=(ne,ye)=>({v:ne,e:2*(ye+f)}),q=T(M,v),C=T(R,N),_=T(A,b),I=a-1,O=s-1,L=(a-1)*(s-1),D=yo(q,I),S=yo(C,O),F=yo(_,L),k=yo(Qh(C,_),O+L),P=[];if(n){let ne={B:{ss:q,df:I,ms:D,pooled:!1},J:{ss:C,df:O,ms:S,pooled:!1}},ye=_,me=L;for(let Ke of NN(D,S)?["B","J"]:["J","B"]){let $e=ne[Ke];vN($e.ms,yo(ye,me))&&(ye=Qh(ye,$e.ss),me+=$e.df,$e.pooled=!0)}let Se=yo(ye,me);if(ne.B.pooled&&(D=Se),ne.J.pooled&&(S=Se),F=Se,ne.B.pooled){let[Ke,$e]=Mo(F,S);k={v:[Ke.at+($e.at-Ke.at)/a,0],e:Ke.E}}}else A[0]===0&&P.push("essentially perfect fit for \u20181\u2019: summary may be unreliable");let B=(ne,ye)=>r/2<1e-8?pn(r/2,ne,ye,!1,!1):pn(1-r/2,ne,ye,!0,!1),Q=(ne,ye,me)=>pt(ne,ye,me,!1,!1),re=ne=>ne===1/0?1:(ne-1)/(ne+s-1),z=ne=>1-1/ne,ee=a*(s-1),U=Zh(D,k),ue=Zh(D,F),[Z,V]=[U/B(I,ee),U*B(ee,I)],[Y,j]=[ue/B(I,L),ue*B(L,I)],[H,W]=Mo(D,k).map(ne=>ne.at),[te,se]=Mo(D,F).map(ne=>ne.at),[Me,Ne,de]=Mo(D,S,F).map(ne=>ne.at),ce=(H-W)/(H+(s-1)*W),qe=(Me-de)/(Me+(s-1)*de+s*(Ne-de)/a),we=(te-se)/(te+(s-1)*se),Fe=s*qe*Ne,xe=(a*(1+(s-1)*qe)-s*qe)*de,_e=Math.max(Math.abs(Fe),Math.abs(xe)),Oe=_e>0&&Number.isFinite(_e)?Math.floor(Math.log2(_e)):0,Be=$(Fe,-Oe),je=$(xe,-Oe),tt=(a-1)*Be*Be+je*je,Ue=tt===0?s-1:(s-1)*(a-1)*(Be+je)**2/tt,Qe=B(I,Ue),ge=B(Ue,I),oe=s*Ne+(s*a-s-a)*de,ve=(ne,ye)=>ne>.5?1-(a*de+oe)/ye:ne,be=ve(a*(Me/Qe-de)/(oe+a*Me/Qe),oe+a*Me/Qe),fe=ge>1?ve(a*(Me-de/ge)/(oe/ge+a*Me),oe+a*ge*Me):ve(a*(ge*Me-de)/(oe+a*ge*Me),oe+a*ge*Me),nt=ne=>ne*s/(1+ne*(s-1)),X=Q(U,I,ee),he=Q(ue,I,L),Ae=(ne,ye,me,Se,Ke,$e,st)=>({type:ne,name:wN[ne],ICC:ye,F:me,df1:I,df2:Se,pValue:Ke,lower:$e,upper:st}),Ee=[Ae("ICC1",ce,U,ee,X,re(Z),re(V)),Ae("ICC2",qe,ue,L,he,be,fe),Ae("ICC3",we,ue,L,he,re(Y),re(j)),Ae("ICC1k",(H-W)/H,U,ee,X,z(Z),z(V)),Ae("ICC2k",(Me-de)/(Me+(Ne-de)/a),ue,L,he,nt(be),nt(fe)),Ae("ICC3k",(te-se)/te,ue,L,he,z(Y),z(j))],Te=ne=>Ve(ne.v[0],ne.e);return J("ICC",{results:Ee,MSB:Te(D),MSJ:Te(S),MSE:Te(F),MSW:Te(k),n:a,k:s,confLevel:o,lmer:n,warnings:P})}function La(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(!hf(tf(e)))throw new RangeError("'covmat' must be symmetric")}var MN=["covmat","nObs","rotation","scores","control"],EN=["lower","start"];function kt(e){let t=0,n=0;for(let r of e){let[o,s]=Dt(t,r);t=o,n+=s}return t+n}var sl=(e,t)=>e[0].map((n,r)=>kt(e.map(o=>t(o[r]))));function np(e,t){let n=t.map(r=>1/Math.sqrt(r));return e.map((r,o)=>r.map((s,a)=>n[o]*s*n[a]))}function ep(e,t,n){let r=Ut(np(e,t),{symmetric:!0}),o=r.values.slice(0,n).map(s=>Math.sqrt(Math.max(s-1,0)));return r.vectors.map((s,a)=>s.slice(0,n).map((c,i)=>Math.sqrt(t[a])*(c*o[i])))}function tp(e,t){let n=sl(e,c=>c*c),r=n.map((c,i)=>i).sort((c,i)=>n[i]-n[c]||c-i),o=e.map(c=>r.map(i=>c[i])),s=t?.map(c=>r.map(i=>c[i])),a=sl(o,c=>c).map(c=>c<0?-1:1);return o=o.map(c=>c.map((i,l)=>a[l]<0?-i:i)),s=s?.map(c=>c.map((i,l)=>a[l]<0?-i:i)),s===void 0?{L:o}:{L:o,rot:s}}var Cn=(e,t)=>e.map(n=>t[0].map((r,o)=>{let s=0;for(let a=0;a<n.length;a++)s+=n[a]*t[a][o];return s})),Eo=e=>e[0].map((t,n)=>e.map(r=>r[n]));function Ca(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,a=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:a};let c=Eo(o),i=0;for(let m=0;m<1e3;m++){let u=Cn(o,a),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=Cn(c,p),d=Ps(g);a=Cn(d.u,Eo(d.v));let v=i;if(i=kt(d.d),i<v*(1+1e-5))break}let l=Cn(o,a),f=e.map(m=>m.map(()=>0));return r.forEach((m,u)=>f[m]=l[u].map(h=>h*n[m])),{loadings:f,rotmat:a}}function xN(e){let t=Ca(e),n=t.loadings,r=n.map(c=>c.map(i=>i*Math.abs(i)**3)),o=Os(Ct(n),r),s=rt(Cn(Eo(o),o)).map((c,i)=>c[i]),a=o.map(c=>c.map((i,l)=>i*Math.sqrt(s[l])));return{loadings:Cn(n,a),rotmat:Cn(t.rotmat,a)}}function RN(e,t,n={}){K(n,MN,"factanal"),K(_t(n,"control"),EN,"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");La(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=Vs(e),s=e.length}let a=o.length;if(a<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 i=((a-c)**2-a-c)/2;if(i<0)throw new RangeError(`${c} ${c===1?"factor is":"factors are"} too many for ${a} variables`);let l=o.map((x,M)=>Math.sqrt(x[M])),f=o.map((x,M)=>x.map((R,A)=>R/(l[M]*l[A]))),m=n.control?.lower??.005,u=n.control?.start?.slice()??rt(f).map((x,M)=>(1-c/(2*a))/x[M]),h=x=>{let M=Ut(np(f,x),{symmetric:!0,onlyValues:!0}).values.slice(c);return-(kt(M.map(R=>Math.log(R)-R))-c+a)},p=x=>{let M=ep(f,x,c);return x.map((R,A)=>{let T=0;for(let q=0;q<c;q++)T+=M[A][q]*M[A][q];return(T+R-f[A][A])/(R*R)})},g=Ye(()=>Po(u,h,{gr:p,method:"L-BFGS-B",lower:m,upper:1,control:{fnscale:1,parscale:new Array(a).fill(.01)}}));if(g.convergence!==0)throw new Error("unable to optimize from this starting value");let d=g.par,v=tp(ep(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"?Ca(v):xN(v),M=tp(x.loadings,x.rotmat);v=M.L,N=M.rot}let E=NaN,w=NaN;!Number.isNaN(s)&&i>0&&(E=(s-1-(2*a+5)/6-2*c/3)*g.value,w=Nt(E,i,{lowerTail:!1}));let y={uniquenesses:d,loadings:v,STATISTIC:E,dof:i,PVAL:w,converged:!0,objective:g.value,counts:g.counts,warnings:[]};return r!=="none"&&(y.scores=AN(e,f,d,v,r)),N!==void 0&&(y.rotmat=N),y}function AN(e,t,n,r,o){let s=e.length,a=e[0].map((m,u)=>kt(e.map(h=>h[u]))/s),c=e.map(m=>m.map((u,h)=>u-a[h])),i=sl(c,m=>m*m).map(m=>Math.sqrt(m/(s-1))),l=c.map(m=>m.map((u,h)=>u/i[h]));if(o==="regression")return Cn(l,rt(t,r));let f=r.map((m,u)=>m.map(h=>h/n[u]));return Cn(l,Eo(rt(Cn(Eo(r),f),Eo(f))))}var TN=["nfactors","covmat"],xo=2**-52,SN=`
13
+ To do this, run the function again with the 'check.keys=TRUE' option`)}return n}function $1(e,t,n){let r=Ko(e,"x").length;Zs(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=Mo(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]-=Ie(i.map((h,p)=>o[s[m]][h]*f[p][u]))}return c[0][1]/Math.sqrt(c[0][0]*c[1][1])}function z1(e){let t=e.length,n=e[0].length,r=a=>a.reduce((l,f)=>ue(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=ue(Ce(Ce([f,0],o[l]),s[m]),i);c=ue(c,_e(u,u))})),c[0]+c[1]}var j1=["controls","cases"],wu=["levels","direction","naRm"],K1=["partialAuc","focus","correct"],W1=["bestMethod","bestWeights"],Eu=new WeakMap;function ea(e){return Eu.get(e)}var Y1=["auto","<",">"];function Ji(e){if(e==null)return null;if(typeof e=="number")return Number.isNaN(e)?null:Pn(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 B1(e){let t=e.filter(s=>Ji(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(Pn):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 Yo(e){return Float64Array.from(e).sort()}function ec(e,t){let n=Yo(e),r=Yo(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 tc(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 nc(e,t,n){let r=e.length,o=t.length;return tc(ec(e,t),r,n)/(2*o*r)}function rc(e,t,n){return tc(ec(e,t),e.length,n)}var bu=e=>e===0n?0:e.toString(2).length;function Zr(e,t){if(t<0n&&(e=-e,t=-t),e===0n)return 0;let n=e<0n,r=n?-e:e,o=55-(bu(r)-bu(t)),s=o>=0?r<<BigInt(o):r,i=o>=0?t:t<<BigInt(-o),c=s/i;c*i!==s&&(c|=1n);let a=et(Number(c),-o);return n?-a:a}function U1(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 G1(e,t,n){let r=ec(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:U1(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:tc(r,o,n)/(2*s*o)}}function H1(e,t){let n=r=>{let o=Yo(r),s=o.length>>1;return o.length%2===1?o[s]:Hn(o[s-1],o[s])};return n(e)<=n(t)?"<":">"}function xu(e,t,n,r,o,s){if(e.length===0)throw new Error("No control observation.");if(t.length===0)throw new Error("No case observation.");if(e.some(l=>!Number.isFinite(l))||t.some(l=>!Number.isFinite(l)))throw new RangeError("infinite values in the predictor: cannot build a valid ROC curve");let i=n==="auto"?H1(e,t):n,a={...G1(e,t,i),direction:i,levels:r,controls:e,cases:t,warnings:s};return Eu.set(a,o),a}function Ru(e){if(e===void 0)return"auto";if(!Y1.includes(e))throw new RangeError("'arg' should be one of \u201Cauto\u201D, \u201C<\u201D, \u201C>\u201D");return e}function Xr(e,t,n){let r=Ru(n.direction),o=new mt,s=e.map(Ji),i;if(n.levels===void 0){if(i=B1(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 d=Ji(p);if(d===null)throw new RangeError("'levels' must not be missing");return d})}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,d)=>p===null||Number.isNaN(t[d]));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,d)=>{if(c[d])return;let g=t[d];if(p===i[0])a.push(g);else if(p===i[1])l.push(g);else return;f.push(g),m.push(p===i[1]),u.push(d)});let h={labels:s,predictor:[...t],values:f,isCase:m,kept:u};return xu(a,l,r,[i[0],i[1]],h,o.list)}function X1(e,t){let n=jr(Xn(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=fs(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 Q1(e){return typeof e=="object"&&e!==null&&!Array.isArray(e)&&!ArrayBuffer.isView(e)}function Z1(e,t,n={}){if(at(e)&&V(e,j1,"roc"),typeof e!="string"&&at(t)&&V(t,wu,"roc"),V(n,wu,"roc"),typeof e=="string"){let r=X1(e,t);return H("roc",Xr(r.response,r.predictor,n))}if(Q1(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=Ru(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 H("roc",xu(a,l,s,["0","1"],m,[]))}return H("roc",Xr(Array.from(e),Array.from(t),n))}var Js=(e,t,n,r,o)=>t[r]+(n-e[r])/(e[o]-e[r])*(t[o]-t[r]);function J1(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=Js(e,t,r,a,l),m=Js(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=Js(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=Js(e,t,n,l,a);s+=(e[l]-n)*(t[l]+f)/2}return s}var fr=(e,t)=>[e[0]*t[1]+t[0]*e[1],e[1]*t[1]],Ir=(e,t)=>[e[0]*t[1]-t[0]*e[1],e[1]*t[1]],An=(e,t)=>[e[0]*t[0],e[1]*t[1]],vu=(e,t)=>t[0]<0n?[-e[0]*t[1],-e[1]*t[0]]:[e[0]*t[1],e[1]*t[0]],Wo=(e,t)=>{let n=e[0]*t[1]-t[0]*e[1];return n<0n?-1:n>0n?1:0};function Nu(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 yu(e,t,n,r,o,s){let i=Nu(e,n),c=Nu(t,r);if(i!==void 0&&c!==void 0)return Mu(i.map(BigInt),c.map(BigInt),BigInt(n),BigInt(r),o,s);if(!(e.length>=2&&e.length===t.length&&e[0]===0&&e[e.length-1]===1&&e.every((h,p)=>h>=0&&h<=1&&(p===0||h>=e[p-1]))&&t.every(h=>h>=0&&h<=1)))throw new RangeError("the curve's sensitivities and specificities must be numbers in [0, 1] forming a ROC curve (use a curve from roc)");let l=e.map(Qr),f=t.map(Qr),m=0n;for(let[,h]of[...l,...f])m=h>m?h:m;let u=h=>h[0]*(m/h[1]);return Mu(l.map(u),f.map(u),m,m,o,s)}function Mu(e,t,n,r,o,s){let i=n*r,c=g=>[t[g]*n+e[g]*r-i,i],a=g=>[e[g],n],l=Qr(o),f=Qr(s),m=(g,w,N)=>fr(c(w),An(vu(Ir(g,a(w)),Ir(a(N),a(w))),Ir(c(N),c(w)))),u=[];e.forEach((g,w)=>{Wo(a(w),l)>=0&&Wo(a(w),f)<=0&&u.push(w)});let h;if(u.length===0){let g=e.findIndex((N,v)=>Wo(a(v),f)>=0),w=g-1;h=An(fr(m(f,w,g),m(l,w,g)),An(Ir(f,l),[1n,2n]))}else{let g=0n;for(let v=1;v<u.length;v++){let E=u[v-1],b=u[v];g+=(e[b]-e[E])*(c(E)[0]+c(b)[0])}h=[g,2n*n*i];let w=u[u.length-1];if(Wo(a(w),f)!==0){let v=w+1;h=fr(h,An(Ir(f,a(w)),An(fr(c(w),m(f,w,v)),[1n,2n])))}let N=u[0];if(Wo(a(N),l)!==0){let v=N-1;h=fr(h,An(Ir(a(N),l),An(fr(c(N),m(l,N,v)),[1n,2n])))}}if(h[0]<0n)throw new Error("Partial AUC correction not defined for ROC curves below the diagonal.");let p=An(An(Ir(f,l),fr(f,l)),[1n,2n]),d=vu(fr(p,h),An(p,[2n,1n]));return Zr(d[0],d[1])}function eb(e,t={}){V(t,K1,"auc");let n=xe(t.focus,["specificity","sensitivity"]),r=t.correct??!1;if(t.partialAuc===void 0)return H("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"?yu(l,f,u,h,i,s):yu(l,f,h,u,i,s):m=J1(l,f,i,s),H("auc",{auc:m,partialAuc:[s,i],focus:n,correct:r,warnings:[]})}function Qr(e){if(e===0)return[0n,1n];let t=0,n=e;for(;!Number.isInteger(n);)n*=2,t++;return[BigInt(n),1n<<BigInt(t)]}function tb(e,t){let n=Yo(e.controls),r=Yo(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 nb(e,t="all",n={}){V(n,W1,"coords");let r=xe(n.bestMethod,["youden","closest.topleft","topleft"]),o=n.bestWeights??[1,.5];if(!Array.isArray(o)||o.length!==2||o.some(g=>typeof g!="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=xe(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(g=>typeof g!="number"||Number.isNaN(g)))throw new RangeError("the thresholds must be numbers (not NaN)")}let f=e.cases.length,m=e.controls.length,{tp:u,tn:h}=tb(e,a);if(l==="best"){let[g,w]=Qr(s),[N,v]=Qr(i),E=(v-N)*w,b=g*N,M=BigInt(f),x=BigInt(m),y=u.map((S,k)=>{if(r==="youden")return BigInt(S)*x*b+E*BigInt(h[k])*M;let L=BigInt(f-S),_=BigInt(m-h[k]);return-(L*L*x*x*b+E*_*_*M*M)}),R=y.reduce((S,k)=>k>S?k:S),T=y.map((S,k)=>S===R?k:-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(g=>f-g),d=h.map(g=>m-g);return H("coords",{threshold:a,sensitivity:u.map(g=>g/f),specificity:h.map(g=>g/m),accuracy:u.map((g,w)=>(g+h[w])/(f+m)),tn:h,tp:u,fn:p,fp:d,npv:h.map((g,w)=>g/(g+p[w])),ppv:u.map((g,w)=>g/(g+d[w])),fpr:d.map(g=>g/m),lrPos:u.map((g,w)=>g*m/(f*d[w])),lrNeg:p.map((g,w)=>g*m/(f*h[w])),youden:u.map((g,w)=>g/f+c*(h[w]/m)),closestTopleft:p.map((g,w)=>(g/f)**2+c*(d[w]/m)**2),warnings:[]})}var rb=["confLevel","method","bootN","bootStratified"],Tu=["method","alternative","paired","confLevel","bootN","bootStratified","levels","direction"];function ra(e){let t=Float64Array.from(e.controls).sort(),n=Float64Array.from(e.cases).sort(),r=t.length,o=n.length,s=(l,f)=>{let m=0,u=l.length;for(;m<u;){let p=m+u>>1;l[p]<f?m=p+1:u=p}let h=m;for(;h<l.length&&l[h]===f;)h++;return[m,h-m]},i=e.direction==="<",c=e.cases.map(l=>{let[f,m]=s(t,l);return 2*(i?f:r-f-m)+m}),a=e.controls.map(l=>{let[f,m]=s(n,l);return 2*(i?o-f-m:f)+m});return{k10:c,k01:a}}function ic(e,t,n,r){return ot(e)/(4*n*n)/r+ot(t)/(4*r*r)/n}function Su(e){let{k10:t,k01:n}=ra(e);return ic(t,n,e.controls.length,e.cases.length)}function ta(e,t,n,r,o,s){let i=BigInt(rc(e,t,n)),c=BigInt(rc(r,o,s)),a=BigInt(e.length)*BigInt(t.length),l=BigInt(r.length)*BigInt(o.length);return Zr(i*l-c*a,2n*a*l)}var sc=e=>e.sensitivities.some((t,n)=>t===1&&e.specificities[n]===1);function eo(e,t){if(e.controls.length<2||e.cases.length<2)throw new Error(`${t} needs at least 2 controls and 2 cases`)}function ku(e){let t=(1-e)/2;return t<1e-8?oe(t,{lowerTail:!1}):oe(1-t)}function qu(e){if(!(Number.isInteger(e)&&e>=1&&e<=2**24))throw new RangeError("bootN must be an integer from 1 to 2^24")}function to(e){let t=new Array(e.length);for(let n=0;n<e.length;n++)t[n]=e[wo(e.length)];return t}function ac(e){let t=ea(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 Bo(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 ob(e,t,n,r){let o=Bo(e,t,n);return o===void 0?NaN:nc(o.controls,o.cases,r)}function Jr(e){let t=new Array(e);for(let n=0;n<e;n++)t[n]=wo(e);return t}function sb(e,t={}){V(t,rb,"ciAuc");let n=t.confLevel??.95;an(n);let r=xe(t.method,["delong","bootstrap"]),o=new mt;sc(e)&&o.add("ci.auc() of a ROC curve with AUC == 1 is always 1-1 and can be misleading.");let s=1-n;if(r==="delong"){eo(e,"DeLong's interval");let{k10:m,k01:u}=ra(e),h=e.auc,p=ic(m,u,e.controls.length,e.cases.length),d=Math.sqrt(p),g=v=>Math.min(1,Math.max(0,v)),w=s/2,N=n===1?[0,1]:d===0?[h,h]:[g(h+oe(w)*d),g(h+ku(n)*d)];return H("ciAuc",{estimate:h,confInt:N,confLevel:n,method:r,variance:p,warnings:o.list})}let i=t.bootN??2e3;qu(i);let c=t.bootStratified??!0,a=[],l=0;if(c){eo(e,"a stratified bootstrap");for(let m=0;m<i;m++){let u=to(e.controls),h=to(e.cases);a.push(nc(u,h,e.direction))}}else{let{values:m,isCase:u}=ac(e);for(let h=0;h<i;h++){let p=ob(m,u,Jr(m.length),e.direction);Number.isNaN(p)?l++:a.push(p)}}if(l>0&&o.add("NA value(s) produced during bootstrap were ignored."),a.length===0)throw new Error("no bootstrap replicate had both controls and cases");let f=Es(a,[s/2,1-s/2]);return H("ciAuc",{estimate:e.auc,confInt:[f[0],f[1]],confLevel:n,method:r,variance:a.length>1?ot(a):NaN,warnings:o.list})}var Au=e=>typeof e=="object"&&e!==null&&!Array.isArray(e)&&"thresholds"in e&&"sensitivities"in e,na=(e,t)=>e.length===t.length&&e.every((n,r)=>n===t[r]);function ab(e,t){let n=ea(e),r=ea(t);if(n?.labels==null||r?.labels==null||!na(e.levels,t.levels))return;let o=c=>c.kept.map(a=>c.labels[a]);if(na(o(n),o(r))&&na(n.isCase,r.isCase))return[e,t];if(!na(n.labels,r.labels))return;let s=n.labels.map((c,a)=>a).filter(c=>n.labels[c]!==null&&!Number.isNaN(n.predictor[c])&&!Number.isNaN(r.predictor[c])),i=(c,a)=>Xr(s.map(l=>a.labels[l]),s.map(l=>a.predictor[l]),{levels:c.levels,direction:c.direction});return[i(e,n),i(t,r)]}function oc(e,t){return t==="two.sided"?2*ie(-Math.abs(e)):t==="greater"?ie(e,{lowerTail:!1}):ie(e)}function ib(e,t,n,r){at(n)&&V(n,Tu,"rocTest"),V(r,Tu,"rocTest");let o,s=new mt,i,c;if(Au(e)){if(!Au(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 b=Array.from(e),M=Array.from(t),x=Array.from(n);if(b.length!==M.length||b.length!==x.length)throw new TypeError("the response and the predictors must have the same length");let y=b.map((S,k)=>k).filter(S=>{let k=b[S];return k!=null&&!(typeof k=="number"&&Number.isNaN(k))&&!Number.isNaN(M[S])&&!Number.isNaN(x[S])}),R=y.map(S=>b[S]),T={levels:o.levels,direction:o.direction};i=Xr(R,y.map(S=>M[S]),T),c=Xr(R,y.map(S=>x[S]),T);for(let S of i.warnings)s.add(S)}let a=Ge(o.alternative),l=xe(o.method,["delong","bootstrap"]),f=o.confLevel??.95;an(f),sc(i)&&sc(c)&&s.add("roc.test() of two ROC curves with AUC == 1 has always p.value = 1 and can be misleading.");let m=ab(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=ta(i.controls,i.cases,i.direction,c.controls,c.cases,c.direction);if(l==="delong"){if(i.direction!==c.direction&&s.add("DeLong's test should not be applied to ROC curves with a different direction."),eo(i,"DeLong's test"),eo(c,"DeLong's test"),u){let L=ra(i),_=ra(c),I=L.k10.map((O,q)=>O-_.k10[q]),F=L.k01.map((O,q)=>O-_.k01[q]),C=Math.sqrt(ic(I,F,i.controls.length,i.cases.length)),D=p===0&&C===0?0:p/C,A=C===0?0:ku(f)*C;return H("rocTest",{method:"DeLong's test for two correlated ROC curves",statistic:D,statisticName:"Z",pValue:oc(D,a),confInt:[p-A,p+A],confLevel:f,...h,warnings:s.list})}let b=Su(i),M=Su(c),x=i.controls.length+i.cases.length,y=c.controls.length+c.cases.length,R=b+M,T,S,k;return R===0?(T=p===0?0:p>0?1/0:-1/0,S=NaN,k=oc(T,a)):(T=p/Math.sqrt(R),S=R*R/(b*b/(x-1)+M*M/(y-1)),k=a==="two.sided"?2*vt(-Math.abs(T),S,!0,!1):a==="greater"?vt(T,S,!1,!1):vt(T,S,!0,!1)),H("rocTest",{method:"DeLong's test for two ROC curves",statistic:T,statisticName:"D",parameter:S,parameterName:"df",pValue:k,...h,warnings:s.list})}let d=o.bootN??2e3;qu(d);let g=o.bootStratified??!0,w=[],N=0;if(g){eo(i,"a stratified bootstrap"),eo(c,"a stratified bootstrap");for(let b=0;b<d;b++)if(u){let M=Jr(i.controls.length),x=Jr(i.cases.length);w.push(ta(M.map(y=>i.controls[y]),x.map(y=>i.cases[y]),i.direction,M.map(y=>c.controls[y]),x.map(y=>c.cases[y]),c.direction))}else{let M=to(i.controls),x=to(i.cases),y=to(c.controls),R=to(c.cases);w.push(ta(M,x,i.direction,y,R,c.direction))}}else{let b=ac(i),M=ac(c);for(let x=0;x<d;x++){let y,R;if(u){let T=Jr(b.values.length);y=Bo(b.values,b.isCase,T),R=Bo(M.values,M.isCase,T)}else y=Bo(b.values,b.isCase,Jr(b.values.length)),R=Bo(M.values,M.isCase,Jr(M.values.length));y===void 0||R===void 0?N++:w.push(ta(y.controls,y.cases,i.direction,R.controls,R.cases,c.direction))}}if(N>0&&s.add(`${N} NA value(s) produced during bootstrap were ignored.`),w.length<2)throw new Error("fewer than 2 bootstrap replicates had both controls and cases");let v=Math.sqrt(ot(w)),E=p===0&&v===0?0:p/v;return H("rocTest",{method:u?"Bootstrap test for two correlated ROC curves":"Bootstrap test for two ROC curves",statistic:E,statisticName:"D",parameter:d,parameterName:"boot.n",pValue:oc(E,a),bootStratified:g,...h,warnings:s.list})}var cb=["method","confLevel","prevalence"];function lb(e,t={}){V(t,cb,"diagnosticTests");let n=xe(t.method,["exact","wilson","jeffreys"]),r=t.confLevel??.95;if(It(r),!Array.isArray(e)||e.length!==2||e.some(b=>!(Array.isArray(b)||ArrayBuffer.isView(b))||b.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(b=>typeof b!="number"))throw new TypeError("the counts must be numbers");if(!o.every(wn))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=(b,M)=>{let x=je(()=>Xa(b,M,{method:n==="exact"?"clopper-pearson":n,confLevel:r}));return{estimate:x.estimate,confInt:x.confInt}},d=1-r,g=d/2<1e-8?oe(d/2,{lowerTail:!1}):oe(1-d/2),w=(b,M)=>{let x=g*Math.sqrt(M);return{estimate:b,confInt:[Math.exp(Math.log(b)-x),Math.exp(Math.log(b)+x)]}},N=s/l,v=a/f,E={sensitivity:p(s,l),specificity:p(a,f),ppv:p(s,m),npv:p(a,u),lrPos:w(i===0?N/0:Zr(BigInt(s)*BigInt(f),BigInt(l)*BigInt(i)),c/(s*l)+a/(i*f)),lrNeg:w(a===0?c/l/v:Zr(BigInt(c)*BigInt(f),BigInt(l)*BigInt(a)),s/(c*l)+i/(a*f)),accuracy:p(s+a,h),diagnosticOR:w(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 b=t.prevalence;if(!(typeof b=="number"&&b>0&&b<1))throw new RangeError("'prevalence' must be a number in (0, 1)");let M=b<2**-500?2**600:1,x=N*(b*M),y=i/f*((1-b)*M),R=v*(1-b),T=c/l*b;E.ppvAtPrevalence=x/(x+y),E.npvAtPrevalence=R/(R+T)}return H("diagnosticTests",E)}var fb=["two","mode","confLevel","ciMethod"],mb=["package","exact"],kn=(e,t)=>t===0?0:e*t;function ub(e,t,n={}){if(V(n,fb,"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;an(s);let i=xe(n.ciMethod,mb),c=[],a=[];for(let C=0;C<e.length;C++){let D=e[C],A=t[C];if(typeof D!="number"||typeof A!="number")throw new TypeError("'x' and 'y' must be numeric");if(!(Number.isNaN(D)||Number.isNaN(A))){if(!Number.isFinite(D)||!Number.isFinite(A))throw new RangeError("'x' and 'y' must not contain infinite values");c.push(D),a.push(A)}}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((C,D)=>{let A=(C+a[D])/2;return Number.isFinite(A)?A:C/2+a[D]/2}),h=f.map((C,D)=>C-m[D]),p=!h.every(Number.isFinite),{groups:d,k:g}=Re([p?f.map((C,D)=>C/2-m[D]/2):h]),w=d[0],N=g+(p?1:0),v=st(w),E=Un(w),b=r*E,M=v-b,x=v+b,y=l-1,R=(1-s)/2,T=xt(R,y,!0,!1),S=R<1e-8?xt(R,y,!1,!1):xt((s+1)/2,y,!0,!1),k=[v+kn(T,E)/Math.sqrt(l),v+kn(S,E)/Math.sqrt(l)],L,_;if(i==="package"){let C=3*E*E/l,D=C>=2**-1022||C===0?Math.sqrt(C):E*Math.sqrt(3/l);L=[M+kn(T,D),M+kn(S,D)],_=[x+kn(T,D),x+kn(S,D)]}else{let C=r*Math.sqrt(l),D=da(R,y,C,!0,!1)/Math.sqrt(l),A=da(R,y,C,!1,!1)/Math.sqrt(l);L=[v-kn(A,E),v-kn(D,E)],_=[v+kn(D,E),v+kn(A,E)]}let I=C=>$(C,N),F=C=>[I(C[0]),I(C[1])];return H("blandAltman",{means:u,diffs:h,n:l,meanDiff:I(v),lowerLimit:I(M),upperLimit:I(x),criticalDiff:I(b),meanDiffConfInt:F(k),lowerLimitConfInt:F(L),upperLimitConfInt:F(_),confLevel:s,two:r,mode:o,ciMethod:i,warnings:[]})}var hb=new Set(["methodReg","methodCi","methodBootstrapCi","errorRatio","confLevel","slopeMeasure","nsamples","nnested","rngSeed","iterMax","threshold","naRm"]),pb=["PaBa","Deming","WDeming"],db=["analytical","jackknife","bootstrap","nestedbootstrap"],gb=["quantile","Student","BCa","tBoot"],Iu=5793,wb=2**27;function fc(e,t){let n=e-t;return Math.abs(n)<1e-12*(Math.abs(e)+Math.abs(t))/2?0:n}function bb(e,t){let n=Bn(e),r=Bn(t),o=[0,0],s=[0,0],i=[0,0];for(let c=0;c<e.length;c++){let a=Ce([e[c],0],n),l=Ce([t[c],0],r);o=ue(o,_e(a,a)),s=ue(s,_e(l,l)),i=ue(i,_e(a,l))}return{u:o,q:s,p:i,mx:n,my:r}}function Ou(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 mc(e,t,n){let r=e.length,{u:o,q:s,p:i,mx:c,my:a}=bb(e,t),l=Ou(o[0],s[0],i[0],n),f=Ce(a,_e([l,0],c))[0],m=_e(o,s),u=i[0]*i[0]/(o[0]*s[0]),h=Ce(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)),d=[0,0];for(let w of e)d=ue(d,_e([w,0],[w,0]));let g=Math.sqrt(p*p*ft(d,r)[0]);return{b0:f,b1:l,se0:g,se1:p,xmean:c[0]}}function _u(e,t,n,r,o,s,i,c,a){let l=e.length,f=mc(e,t,n),m=f.b0,u=f.b1,h=NaN,p=new Array(l),d=s;for(let b=0;b<s;b++){let M=1+n*u*u,x=1/0;for(let A=0;A<l;A++){let O=t[A]-(m+u*e[A]),q=e[A]+n*u*O/M,P=t[A]-O/M;p[A]=r>1?q/r+P:q+r*P,x=Math.min(x,Math.abs(p[A]))}let y=[0,0],R=[0,0],T=[0,0];for(let A=0;A<l;A++){let O=x/p[A],q=[O*O,0];y=ue(y,q),R=ue(R,_e(q,[e[A],0])),T=ue(T,_e(q,[t[A],0]))}let S=ft(R,y[0]+y[1]),k=ft(T,y[0]+y[1]),L=[0,0],_=[0,0],I=[0,0];for(let A=0;A<l;A++){let O=ft(Ce([e[A],0],S),p[A]),q=ft(Ce([t[A],0],k),p[A]);L=ue(L,_e(O,O)),_=ue(_,_e(q,q)),I=ue(I,_e(O,q))}let F=Ou(L[0],_[0],I[0],n),C=Ce(k,_e([F,0],S))[0];h=S[0];let D=$(Math.abs(u-F),a-c)<i&&$(Math.abs(m-C),a)<i;if(m=C,u=F,D){d=b+1;break}}let[g,w]=cc(o),[N,v]=cc(1+o),E=p.map(b=>{if(b===0)return 1/0;let[M,x]=cc(Math.abs(b));return r>1?et((N/(g*M))**2,2*(v-w-x-a)):et((N/M)**2,2*(v-x-c))});return{b0:m,b1:u,xmean:h,weights:E,converged:d<s}}function cc(e){let t=Math.floor(Math.log2(e));return[et(e,-t),t]}var oo=e=>Math.atan(e),ro=e=>Number.isFinite(e)?e:Math.tan(oo(e));function vb(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=fc(e[u],e[m]),p=fc(t[u],t[m]),d;if(h!==0)d=p/h;else if(p!==0)d=o?1/0:-1/0;else continue;s[i++]=d;let g=oo(d);o?g===-l?c++:g<-l&&a++:g===l?c++:g>l&&a++}return{sorted:s.subarray(0,i).sort(),offset:o?2*a+c:-(2*a+c),e:c}}function Nb(e,t,n){let r=e.length;if(t%2!==0){let c=(t+1)/2;return c>=1&&c<=r?ro(e[c-1]):NaN}let o=t/2;if(o<1||o+1>r)return NaN;let s=e[o-1],i=e[o];return n?(ro(s)+ro(i))/2:Math.tan((oo(s)+oo(i))/2)}function yb(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 uc(e,t,n){return Number.isFinite(n)?yb(e.map((r,o)=>fc(t[o],n*r))):-n}function Lu(e,t,n){let r=vb(e,t),o=Nb(r.sorted,r.sorted.length+r.offset,n);return{b0:Number.isNaN(o)?NaN:uc(e,t,o),b1:o,slopes:r}}function Mb(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,d)=>{let g=Math.trunc((p+1)/2);if(d?g<2*a+1:g>l)return NaN;if(p%2===0){if(g+1>l)return NaN;let w=i[g-1],N=i[g];return o?(ro(w)+ro(N))/2:Math.tan((oo(w)+oo(N))/2)}return ro(i[g-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:[uc(e,t,h),uc(e,t,u)],defined:!0}}function Eb(e,t,n,r){let o=e.length,s=e.map((p,d)=>t[d]-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,d)=>(t[d]+p/r-n)/Math.sqrt(1+1/(r*r))),m=Array.from({length:o},(p,d)=>d).sort((p,d)=>f[p]-f[d]||p-d),u=0,h=0;for(let p of m){let d=s[p];u+=d>0?a:d<0?l:0,h=Math.max(h,Math.abs(u))}return{nPos:i,nNeg:c,maxCusum:h}}var oa=e=>e.every(t=>t===e[0]);function lc(e,t){if(!(typeof e=="number"&&Number.isInteger(e)&&e>=1))throw new RangeError(`'${t}' must be a positive integer`);return e}var no=(e,t)=>Es(e,t),Cu=e=>Un(e.filter(t=>!Number.isNaN(t)));function xb(e,t,n={}){V(n,hb,"mcreg");let r=xe(n.methodReg,pb),o=xe(n.methodCi??(r==="WDeming"?"jackknife":"analytical"),db),s=o==="bootstrap"||o==="nestedbootstrap",i=xe(n.methodBootstrapCi,gb),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=xe(n.slopeMeasure,["radian","tangent"])==="tangent",f=r!=="PaBa",m=f?n.errorRatio??1:1;if(f&&!(typeof m=="number"&&m>0&&m<1/0))throw new RangeError("'errorRatio' must be a positive finite number");let u=lc(n.nsamples??999,"nsamples"),h=lc(n.nnested??25,"nnested"),p=lc(n.iterMax??30,"iterMax"),d=n.threshold??1e-6;if(!(typeof d=="number"&&d>=0&&d<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 g=[],w=[];for(let X=0;X<e.length;X++){let K=e[X],Z=t[X];if(typeof K!="number"||typeof Z!="number")throw new TypeError("'x' and 'y' must be numeric");if(Number.isNaN(K)||Number.isNaN(Z)){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(K)||!Number.isFinite(Z))throw new RangeError("'x' and 'y' must not contain infinite values");g.push(K),w.push(Z)}let N=g.length;if(N<=2)throw new RangeError("mcreg needs at least 3 complete pairs");if(oa(g)||oa(w))throw new RangeError("'x' and 'y' must not be constant");if(r==="PaBa"){if(g.some(K=>K<0)||w.some(K=>K<0))throw new RangeError("Passing-Bablok regression needs nonnegative values");if(N>Iu)throw new RangeError(`Passing-Bablok regression is limited to ${Iu} 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)>wb)throw new RangeError("the Passing-Bablok fits would take more than 2^27 pairwise slopes in all (reduce nsamples or nnested)")}if(r==="WDeming"&&(g.some(X=>X<=0)||w.some(X=>X<=0)))throw new RangeError("weighted Deming regression needs positive values");let v=Re([g,w]),[E,b]=f?[Re([g]).k,Re([w]).k]:[v.k,v.k],M=f?g.map(X=>$(X,-E)):v.groups[0],x=f?w.map(X=>$(X,-b)):v.groups[1],y=Math.min(Math.max(et(m,2*(b-E)),Number.MIN_VALUE),2**1e3),R=et(m,b-E),T=new Set,S=`no konvergence after ${p} iterations`,k=(X,K)=>{if(r==="Deming")return mc(X,K,y);if(r==="WDeming"){let Z=_u(X,K,y,R,m,p,d,E,b);return Z.converged||T.add(S),Z}return Lu(X,K,l)},L,_=NaN,I,F,C;if(r==="Deming"){let X=mc(M,x,y);L=X,_=$(X.xmean,E),F=[X.se0,X.se1]}else if(r==="WDeming"){let X=_u(M,x,y,R,m,p,d,E,b);X.converged||T.add(S),L=X,_=$(X.xmean,E),I=X.weights}else{let X=Lu(M,x,l);L=X,C=X.slopes}if(!Number.isFinite(L.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,A=xt(D/2,N-2,!1,!1),O=oe(D/2,{lowerTail:!1}),q=[L.b0,L.b1],P=[NaN,NaN],B=[[NaN,NaN],[NaN,NaN]],J,ce,W,te=()=>{let X=[],K=[];for(let Z=0;Z<N;Z++){let z=k(M.filter((G,j)=>j!==Z),x.filter((G,j)=>j!==Z));X.push(z.b0),K.push(z.b1)}J=[X,K],ce=[(N-1)*Un(X)/Math.sqrt(N),(N-1)*Un(K)/Math.sqrt(N)]};if(o==="analytical")if(r==="Deming")for(let X of[0,1])P[X]=F[X],B[X]=[q[X]-A*P[X],q[X]+A*P[X]];else{let X=Mb(M,x,C,O,l);X.defined||T.add("the Passing-Bablok interval is not defined for so few points: its limits are -Inf and Inf"),B[0]=X.intercept,B[1]=X.slope}else if(o==="jackknife"){te();for(let X of[0,1])P[X]=ce[X],B[X]=[q[X]-A*P[X],q[X]+A*P[X]]}else{if(i==="BCa"&&(te(),J.some(he=>he.some(Number.isNaN))))throw new RangeError("Too many identical data points!");let X=r==="Deming"?F:r==="WDeming"&&ce!==void 0?ce:[NaN,NaN],K=o==="nestedbootstrap",Z=[],z=[],G=[],j=[],Q=0,ne=()=>Array.from({length:N},()=>wo(N)),be=he=>{let Le=he.map(Me=>M[Me]),we=he.map(Me=>x[Me]);if(oa(Le)||oa(we)){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(Le,we)},Ne=()=>{for(let he=0;he<u;he++){let Le=ne(),we=be(Le),Me;if(K){let Se=[],Ke=[];for(let Pe=0;Pe<h;Pe++){let Ze=be(ne().map(Be=>Le[Be]));Se.push(Ze.b0),Ke.push(Ze.b1)}Me=[Cu(Se),Cu(Ke)]}else r==="Deming"?Me=[we.se0,we.se1]:Me=X;if(Number.isNaN(we.b0)||Number.isNaN(we.b1)){Q++;continue}Z.push(we.b0),z.push(we.b1),G.push(Me[0]),j.push(Me[1])}};if(n.rngSeed!==void 0){let he=Bc()?Hc():void 0;try{Gc({kind:"Mersenne-Twister"}),Uc(n.rngSeed),Ne()}finally{he!==void 0&&Xc(he)}}else Ne();if(Q>.05*u)throw new RangeError("There were too many resamples with undetermined regression coefficients!");let me=Z.length,le=[Z,z],Ee=[G,j];for(let he of[0,1]){let Le=le[he],we=q[he];if(i==="quantile")B[he]=[no(Le,D/2),no(Le,1-D/2)];else if(i==="Student")P[he]=Un(Le),B[he]=[we-A*P[he],we+A*P[he]];else if(i==="BCa"){let Me=Le.filter(ae=>ae<we).length;Le.every(ae=>ae===we)?Me=me/2:Me===0?Me=.5:Me===me&&(Me=me-.5);let Se=oe(Me/me),Ke=J[he],Pe=Ke.reduce((ae,Je)=>ae+Je,0)/Ke.length,Ze=0,Be=0;for(let ae of Ke){let Je=Pe-ae;Ze+=Je*Je,Be+=Je*Je*Je}let Qe=Ze===0?0:Be/(6*Ze**1.5),fe=-O,ee=O,ge=ie(Se-(fe-Se)/(1+Qe*(fe-Se))),de=ie(Se-(ee-Se)/(1+Qe*(ee-Se)));B[he]=[no(Le,de),no(Le,ge)]}else{let Me=Le.map((Ke,Pe)=>(Ke-we)/Ee[he][Pe]),Se=Number.isNaN(X[he])?Un(Le):X[he];P[he]=Se,B[he]=[we-no(Me,1-D/2)*Se,we-no(Me,D/2)*Se]}}W={intercept:Z.map(he=>$(he,b)),slope:z.map(he=>$(he,b-E)),sigmaIntercept:G.map(he=>$(he,b)),sigmaSlope:j.map(he=>$(he,b-E)),globalSigma:[$(X[0],b),$(X[1],b-E)],nsamples:me}}let U=X=>{let K=Z=>$(Z,X===0?b:b-E);return{estimate:K(q[X]),se:K(P[X]),confInt:[K(B[X][0]),K(B[X][1])]}},ve={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},...J===void 0?{}:{jackknife:{intercept:J[0].map(X=>$(X,b)),slope:J[1].map(X=>$(X,b-E))}},...W===void 0?{}:{bootstrap:W},...r==="PaBa"?{cusum:Eb(M,x,L.b0,L.b1)}:{},warnings:[...T]};return H("mcreg",ve)}var Rb=["alpha","confLevel","lmer"],Tb={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 sa(e,t){let n=e-t,r=n-e;return[n,e-(n-r)+(-t-r)]}function Sb(e){let t=e[0];return e.every(n=>n.every((r,o)=>{let[s,i]=sa(r,n[0]),[c,a]=sa(t[o],t[0]);return s===c&&i===a}))}var Fu=(e,t)=>e.every((n,r)=>n===t[r]);function _r(e){let t=[0,0];for(let n of e)t=ue(t,n);return ft(t,e.length)}var so=(e,t)=>({v:ft(e.v,t),e:e.e});function ao(...e){let t=e.filter(r=>r.v[0]!==0),n=t.length===0?0:Math.max(...t.map(r=>r.e));return e.map(r=>({at:et(r.v[0],r.e-n),E:n}))}function Du(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=>[et(o.v[0],o.e-n),et(o.v[1],o.e-n)];return{v:ue(r(e),r(t)),e:n}}var Pu=(e,t)=>et(e.v[0]/t.v[0],e.e-t.e),Ab=(e,t)=>{let[n,r]=ao(e,t);return n.at<r.at},kb=(e,t)=>{let[n,r]=ao(e,t);return n.at<=r.at};function Vu(e,t,n){let r=[0,0];for(let o of e){let s=Ce(o,t);r=ue(r,_e(s,s))}return _e(r,[n,0])}function qb(e,t={}){V(t,Rb,"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,It(o),r=1-o;if(!Array.isArray(e)||e.length===0||!e.every(se=>Array.isArray(se)))throw new TypeError("'x' must be a matrix (an array of rows)");let s=e[0].length;if(e.some(se=>se.length!==s))throw new TypeError("'x' must be a matrix (rows of equal length)");if(e.some(se=>se.some(Te=>typeof Te!="number")))throw new TypeError("'x' must contain numbers");if(e.some(se=>se.some(Number.isNaN)))throw new RangeError("'x' contains missing ratings (only complete data are supported)");if(e.some(se=>se.some(Te=>!Number.isFinite(Te))))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}=Re(e);if(c.every(se=>Fu(se,c[0])))throw new RangeError(c[0].every(se=>se===c[0][0])?"all ratings are equal":"every subject has the same ratings: the coefficients are undefined");let a=Array.from({length:s},(se,Te)=>c.map(pe=>pe[Te])),l=0;for(let se of e)for(let Te of se)l=Math.max(l,Math.abs(Te));let f=l>2**1020?3:0,m=f>0?e.map(se=>se.map(Te=>Te/8)):e,u=m.map(se=>se.map((Te,pe)=>sa(Te,m[0][pe]))),h=m.map(se=>se.map(Te=>sa(Te,se[0]))),p=h.map(se=>se.map((Te,pe)=>Ce(Te,h[0][pe]))),d=se=>{let Te=0;for(let pe of se)for(let Ae of pe)Te=Math.max(Te,Math.abs(Ae[0]));return Te===0?0:Math.floor(Math.log2(Te))},g=(se,Te)=>se.map(pe=>pe.map(Ae=>[$(Ae[0],-Te),$(Ae[1],-Te)])),[w,N,v]=[d(u),d(h),d(p)],[E,b,M]=[g(u,w),g(h,N),g(p,v)],x=se=>Array.from({length:s},(Te,pe)=>se.map(Ae=>Ae[pe])),y=Vu(E.map(_r),_r(E.flat()),s),R=a.every(se=>Fu(se,a[0]))?[0,0]:Vu(x(b).map(_r),_r(b.flat()),i),T=[0,0];if(!Sb(c)){let se=M.map(_r),Te=x(M).map(_r),pe=_r(M.flat());M.forEach((Ae,We)=>Ae.forEach((Fe,nt)=>{let ut=ue(Ce(Ce(Fe,se[We]),Te[nt]),pe);T=ue(T,_e(ut,ut))}))}let S=(se,Te)=>({v:se,e:2*(Te+f)}),k=S(y,w),L=S(R,N),_=S(T,v),I=i-1,F=s-1,C=(i-1)*(s-1),D=so(k,I),A=so(L,F),O=so(_,C),q=so(Du(L,_),F+C),P=[];if(n){let se={B:{ss:k,df:I,ms:D,pooled:!1},J:{ss:L,df:F,ms:A,pooled:!1}},Te=_,pe=C;for(let We of kb(D,A)?["B","J"]:["J","B"]){let Fe=se[We];Ab(Fe.ms,so(Te,pe))&&(Te=Du(Te,Fe.ss),pe+=Fe.df,Fe.pooled=!0)}let Ae=so(Te,pe);if(se.B.pooled&&(D=Ae),se.J.pooled&&(A=Ae),O=Ae,se.B.pooled){let[We,Fe]=ao(O,A);q={v:[We.at+(Fe.at-We.at)/i,0],e:We.E}}}else T[0]===0&&P.push("essentially perfect fit for \u20181\u2019: summary may be unreliable");let B=(se,Te)=>r/2<1e-8?ln(r/2,se,Te,!1,!1):ln(1-r/2,se,Te,!0,!1),J=(se,Te,pe)=>wt(se,Te,pe,!1,!1),ce=se=>se===1/0?1:(se-1)/(se+s-1),W=se=>1-1/se,te=i*(s-1),U=Pu(D,q),ve=Pu(D,O),[X,K]=[U/B(I,te),U*B(te,I)],[Z,z]=[ve/B(I,C),ve*B(C,I)],[G,j]=ao(D,q).map(se=>se.at),[Q,ne]=ao(D,O).map(se=>se.at),[be,Ne,me]=ao(D,A,O).map(se=>se.at),le=(G-j)/(G+(s-1)*j),Ee=(be-me)/(be+(s-1)*me+s*(Ne-me)/i),he=(Q-ne)/(Q+(s-1)*ne),Le=s*Ee*Ne,we=(i*(1+(s-1)*Ee)-s*Ee)*me,Me=Math.max(Math.abs(Le),Math.abs(we)),Se=Me>0&&Number.isFinite(Me)?Math.floor(Math.log2(Me)):0,Ke=$(Le,-Se),Pe=$(we,-Se),Ze=(i-1)*Ke*Ke+Pe*Pe,Be=Ze===0?s-1:(s-1)*(i-1)*(Ke+Pe)**2/Ze,Qe=B(I,Be),fe=B(Be,I),ee=s*Ne+(s*i-s-i)*me,ge=(se,Te)=>se>.5?1-(i*me+ee)/Te:se,de=ge(i*(be/Qe-me)/(ee+i*be/Qe),ee+i*be/Qe),ae=fe>1?ge(i*(be-me/fe)/(ee/fe+i*be),ee+i*fe*be):ge(i*(fe*be-me)/(ee+i*fe*be),ee+i*fe*be),Je=se=>se*s/(1+se*(s-1)),Oe=J(U,I,te),tt=J(ve,I,C),ke=(se,Te,pe,Ae,We,Fe,nt)=>({type:se,name:Tb[se],ICC:Te,F:pe,df1:I,df2:Ae,pValue:We,lower:Fe,upper:nt}),$e=[ke("ICC1",le,U,te,Oe,ce(X),ce(K)),ke("ICC2",Ee,ve,C,tt,de,ae),ke("ICC3",he,ve,C,tt,ce(Z),ce(z)),ke("ICC1k",(G-j)/G,U,te,Oe,W(X),W(K)),ke("ICC2k",(be-me)/(be+(Ne-me)/i),ve,C,tt,Je(de),Je(ae)),ke("ICC3k",(Q-ne)/Q,ve,C,tt,W(Z),W(z))],He=se=>et(se.v[0],se.e);return H("ICC",{results:$e,MSB:He(D),MSJ:He(A),MSE:He(O),MSW:He(q),n:i,k:s,confLevel:o,lmer:n,warnings:P})}function aa(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(sl(e)))throw new RangeError("'covmat' must be symmetric")}var Ib=["covmat","nObs","rotation","scores","control"],_b=["lower","start"];function St(e){let t=0,n=0;for(let r of e){let[o,s]=$t(t,r);t=o,n+=s}return t+n}var hc=(e,t)=>e[0].map((n,r)=>St(e.map(o=>t(o[r]))));function ju(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 $u(e,t,n){let r=un(ju(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 zu(e,t){let n=hc(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=hc(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 qn=(e,t)=>e.map(n=>t[0].map((r,o)=>{let s=0;for(let i=0;i<n.length;i++)s+=n[i]*t[i][o];return s})),io=e=>e[0].map((t,n)=>e.map(r=>r[n]));function ia(e){let t=e[0].length,n=e.map(m=>Math.sqrt(St(m.map(u=>u*u)))),r=e.flatMap((m,u)=>n[u]>0?[u]:[]),o=r.map(m=>e[m].map(u=>u/n[m])),s=o.length,i=Array.from({length:t},(m,u)=>Array.from({length:t},(h,p)=>u===p?1:0));if(s===0)return{loadings:e.map(m=>m.slice()),rotmat:i};let c=io(o),a=0;for(let m=0;m<1e3;m++){let u=qn(o,i),h=u[0].map((N,v)=>{let E=0;for(let b=0;b<s;b++)E+=u[b][v]*u[b][v];return E}),p=u.map(N=>N.map((v,E)=>v**3-v*h[E]/s)),d=qn(c,p),g=bs(d);i=qn(g.u,io(g.v));let w=a;if(a=St(g.d),a<w*(1+1e-5))break}let l=qn(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 Lb(e){let t=ia(e),n=t.loadings,r=n.map(c=>c.map(a=>a*Math.abs(a)**3)),o=al(jt(n),r),s=rt(qn(io(o),o)).map((c,a)=>c[a]),i=o.map(c=>c.map((a,l)=>a*Math.sqrt(s[l])));return{loadings:qn(n,i),rotmat:qn(t.rotmat,i)}}function Cb(e,t,n={}){V(n,Ib,"factanal"),V(Ft(n,"control"),_b,"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");aa(n.covmat),o=n.covmat.map(x=>x.slice()),s=n.nObs??NaN}else{if(e===null)throw new TypeError("neither 'x' nor 'covmat' supplied");o=Ms(e),s=e.length}let i=o.length;if(i<3)throw new RangeError("factor analysis requires at least three variables");let c=t;if(!Number.isInteger(c)||c<1)throw new RangeError("'factors' must be a positive integer");let a=((i-c)**2-i-c)/2;if(a<0)throw new RangeError(`${c} ${c===1?"factor is":"factors are"} too many for ${i} variables`);let l=o.map((x,y)=>Math.sqrt(x[y])),f=o.map((x,y)=>x.map((R,T)=>R/(l[y]*l[T]))),m=n.control?.lower??.005,u=n.control?.start?.slice()??rt(f).map((x,y)=>(1-c/(2*i))/x[y]),h=x=>{let y=un(ju(f,x),{symmetric:!0,onlyValues:!0}).values.slice(c);return-(St(y.map(R=>Math.log(R)-R))-c+i)},p=x=>{let y=$u(f,x,c);return x.map((R,T)=>{let S=0;for(let k=0;k<c;k++)S+=y[T][k]*y[T][k];return(S+R-f[T][T])/(R*R)})},d=je(()=>Eo(u,h,{gr:p,method:"L-BFGS-B",lower:m,upper:1,control:{fnscale:1,parscale:new Array(i).fill(.01)}}));if(d.convergence!==0)throw new Error("unable to optimize from this starting value");let g=d.par,w=zu($u(f,g,c)).L,N,v=n.rotation??"varimax";if(v!=="varimax"&&v!=="promax"&&v!=="none")throw new RangeError(`unknown rotation '${String(v)}'`);if(v!=="none"&&c>=2){let x=v==="varimax"?ia(w):Lb(w),y=zu(x.loadings,x.rotmat);w=y.L,N=y.rot}let E=NaN,b=NaN;!Number.isNaN(s)&&a>0&&(E=(s-1-(2*i+5)/6-2*c/3)*d.value,b=Mt(E,a,{lowerTail:!1}));let M={uniquenesses:g,loadings:w,STATISTIC:E,dof:a,PVAL:b,converged:!0,objective:d.value,counts:d.counts,warnings:[]};return r!=="none"&&(M.scores=Ob(e,f,g,w,r)),N!==void 0&&(M.rotmat=N),M}function Ob(e,t,n,r,o){let s=e.length,i=e[0].map((m,u)=>St(e.map(h=>h[u]))/s),c=e.map(m=>m.map((u,h)=>u-i[h])),a=hc(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 qn(l,rt(t,r));let f=r.map((m,u)=>m.map(h=>h/n[u]));return qn(l,io(rt(qn(io(r),f),io(f))))}var Fb=["nfactors","covmat"],co=2**-52,Db=`
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
- `,Ro=(e,t)=>e.map(n=>t[0].map((r,o)=>{let s=0;for(let a=0;a<n.length;a++)s+=n[a]*t[a][o];return s})),us=e=>e[0].map((t,n)=>e.map(r=>r[n])),hs=(e,t=n=>n)=>e[0].map((n,r)=>kt(e.map(o=>t(o[r])))),op=(e,t=n=>n)=>e.map(n=>kt(n.map(t))),Ao=(e,t=n=>n)=>kt(e[0].flatMap((n,r)=>e.map(o=>t(o[r])))),Oa=e=>e<0?-1:1;function cl(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 sp(e,t){let n=cl(e),{d:r,u:o,v:s}=Ps(n),a=r.map((i,l)=>l).filter(i=>r[i]>Math.sqrt(xo)*r[0]),c=n.map((i,l)=>{let f=0;for(let m of a)f+=s[l][m]*(1/r[m])*o[l][m];return 1-1/f});return c.some(i=>i>1)&&Nr(t,"In smc, smcs > 1 were set to 1.0"),c.some(i=>i<0)&&Nr(t,"In smc, smcs < 0 were set to .0"),c.map(i=>i>1?1:i<0?0:i)}var Nr=(e,t)=>{e.includes(t)||e.push(t)};function al(e,t,n,r){let o=e.map((c,i)=>c.map((l,f)=>i===f?t[i]:l)),s=Ut(o,{symmetric:!0}),a=s.values.slice(0,n).map(c=>Math.sqrt(r(c)));return s.vectors.map(c=>c.slice(0,n).map((i,l)=>i*a[l]))}var kN=e=>Ro(e,us(e));function rp(e,t,n,r){let o=cl(e),s=o.length,a=sp(o,r),c=d=>{let v=al(o,d.map(E=>1-E),t,E=>E<xo?100*xo:E),N=kN(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)},i=d=>{let v=al(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((A,T)=>A.map((q,C)=>T===C?1-d[T]:q)),{symmetric:!0}),N=v.values,b=v.vectors,E=N.map(A=>A>=xo),w=N.map(A=>A<xo?100*xo:A),y=o.map((A,T)=>A.map((q,C)=>{if(T===C)return 0;let _=0;for(let I=0;I<t;I++)_+=w[I]*b[T][I]*b[C][I];return q-_})),x=Ro(y,b),M=(A,T)=>{let q=0;for(let C=0;C<s;C++)q+=b[C][A]*x[C][T];return q},R=new Array(s).fill(0);for(let A=0;A<t;A++){let T=(q,C)=>{if(C===0)return;let _=C*M(A,q);for(let I=0;I<s;I++)R[I]+=_*b[I][A]*b[I][q]};E[A]&&T(A,1);for(let q=A+1;q<t;q++)T(q,2*(E[A]&&E[q]?1:(w[A]-w[q])/(N[A]-N[q])));for(let q=t;q<s;q++)T(q,2*w[A]/(N[A]-N[q]))}return R},f=.005,m={method:"L-BFGS-B",lower:f,upper:Math.max(Et(a),1)},u=new Array(s).fill(.01),h=Ye(()=>Po(a.map(d=>1-d),c,{gr:i,...m,control:{fnscale:1,parscale:u}}));h.par.some(d=>d===f)&&(h=Ye(()=>Po(h.par,c,{gr:l,...m,control:{fnscale:1,parscale:u,factr:1e3}}))),h.convergence!==0&&Nr(n,`minres did not converge (optim convergence code ${h.convergence}): examine the results carefully`);let p=h.par,g=al(o,p.map((d,v)=>o[v][v]-d),t,d=>Math.max(d,0));if(g=il(g,t),g=g.map(d=>d.map(v=>v===0?1e-15:v)),op(g,d=>d*d).some(d=>d>1)&&Nr(n,"An ultra-Heywood case was detected. Examine the results carefully"),t>1){g=il(Ca(g).loadings,t);let d=hs(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=il(g,t);return g}function il(e,t){let n=hs(e).map(Oa);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 qN(e){let t=e[0].length,n=e.map(o=>o.map(s=>s*s)),r=n.map(o=>o.map((s,a)=>{let c=0;for(let i=0;i<t;i++)i!==a&&(c+=o[i]);return c}));return{f:Ao(n.map((o,s)=>o.map((a,c)=>a*r[s][c])))/4,Gq:e.map((o,s)=>o.map((a,c)=>a*r[s][c]))}}function IN(e){let t=e[0].length,n=l=>{let f=rt(l),m=Ro(e,us(f)),{f:u,Gq:h}=qN(m),p=us(Ro(Ro(us(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),a,c=[],i=!1;for(let l=0;l<=2e3;l++){let f=hs(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(Ao(m,d=>d*d));if(c.push(s.f),u<1e-5){i=!0;break}if(a!==void 0){let d=a,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]));Ao(N,b=>b*b)>0&&(o=Ao(v,b=>b*b)/Math.abs(Ao(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=Et(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=hs(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}a={T:r,Gp:m},r=g,s=p}return{loadings:s.L,phi:Ro(us(r),r),converged:i}}function _N(e,t={}){K(t,TN,"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)La(t.covmat),r=cl(t.covmat);else{if(e===null)throw new TypeError("neither 'x' nor 'covmat' supplied");r=Do(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=[],a=[],c=rp(r,n,s,a),i,l,f;if(n===1)i=c.map(k=>k.slice()),f=[1],Nr(a,`Omega_h for 1 factor is not meaningful, just omega_t
19
- `),Nr(s,"Omega_h and Omega_asymptotic are not meaningful with one factor");else{let k=IN(c);if(k.converged||Nr(s,"oblimin did not converge in 2000 iterations"),i=k.loadings,l=k.phi,n===2){let P=l[0][1],B=Math.sqrt(Math.abs(P));f=[B,Oa(P)*B],Nr(a,SN)}else f=rp(l,1,s,a).map(P=>P[0])}let m=i.map(k=>{let P=0;for(let B=0;B<n;B++)P+=k[B]*f[B];return P}),u=op(c,k=>k*k),h=u.map((k,P)=>r[P][P]-k),p=i.map((k,P)=>k.map((B,Q)=>{if(n===1)return 0;let re=r[P][P],z=re-(re-B*B)-(B*f[Q])**2;return z<0&&(z=0),Oa(B)*Math.sqrt(z)})),g=m.map((k,P)=>k*k/u[P]),d=m.map((k,P)=>{let B=[k,...p[P]];return kt(B.map(Q=>Q*Q))**2/kt(B.map(Q=>Q**4))}),v=m.map(Oa);v.some(k=>k<0)&&(r=r.map((k,P)=>k.map((B,Q)=>v[P]*B*v[Q])));let N=m.map((k,P)=>v[P]*k),b=p.map((k,P)=>k.map(B=>v[P]*B)),E=N.map((k,P)=>[k,...b[P],u[P],h[P],g[P],d[P]]),w=Ao(r),y=kt(r.map((k,P)=>k[P])),x=kt(N)**2,M=kt(h),R=x/w,A=(w-M)/w,T=x/(w-M),q=(w-y)/w*o/(o-1),C=(w+kt(sp(r,a))-y)/w,_=hs(E.map(k=>k.slice(0,n+1)),k=>k*k),I=_[0]/kt(_),O=b.map(k=>{let P=0;for(let B=1;B<n;B++)Math.abs(k[B])>Math.abs(k[P])&&(P=B);return P}),L=[],D=[];for(let k=0;k<n;k++){let P=O.map((z,ee)=>ee).filter(z=>O[z]===k),B=kt(P.flatMap(z=>P.map(ee=>r[ee][z]))),Q=kt(P.map(z=>b[z][k])),re=kt(P.map(z=>N[z]));D.push(Q*Q),L.push([(Q*Q+re*re)/B,re*re/B,Q*Q/B])}let S=[[A,kt(N)**2/w,kt(D)/w],...L],F={omegaH:R,omegaLim:T,alpha:q,omegaTot:A,G6:C,ECV:I,sl:E,slNames:["g",...Array.from({length:n},(k,P)=>`F${P+1}*`),"h2","u2","p2","com"],orthog:c,oblique:i,gloading:f,key:v,omegaGroup:S,warnings:s,messages:a};return l!==void 0&&(F.phi=l),J("omega",F)}var ap=["ML","stdErr"],LN=["ML","stdErr"],sn=.9999;function fl(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 ml=e=>[...new Set(e)].sort((t,n)=>t-n),ip=(e,t)=>e===1?`1 ${t} with zero marginal removed`:`${e} ${t}s with zero marginals removed`,cp=e=>Array.isArray(e)||ArrayBuffer.isView(e),lp=(e,t)=>e>0?ae(e,{lowerTail:!1})-ae(t,{lowerTail:!1}):ae(t)-ae(e);function ul(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 fp(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 Fa=e=>Array.from({length:e},()=>new Array(e).fill(0));function hp(e,t,n,r,o){e.value+=t*Math.log(n);let s=r.length;for(let a=0;a<s;a++){e.gradient[a]+=t*r[a]/n;for(let c=0;c<s;c++)e.hessian[a][c]+=t*(o[a][c]/n-r[a]*r[c]/(n*n))}}var ll={r:0,h:0,k:0,rr:0,rh:0,rk:0,hh:0,hk:0,kk:0};function CN(e,t,n){if(e===-1/0||t===-1/0||e===1/0&&t===1/0)return ll;if(e===1/0)return{...ll,k:Jt(t),kk:-t*Jt(t)};if(t===1/0)return{...ll,h:Jt(e),hh:-e*Jt(e)};let r=(1-n)*(1+n),o=Math.sqrt(r),s=e*e-2*n*e*t+t*t,a=Math.exp(-s/(2*r))/(2*Math.PI*o),c=(t-n*e)/o,i=(e-n*t)/o,l=Jt(e),f=Jt(t);return{r:a,h:l*ae(c),k:f*ae(i),rr:a*(n/r+(e*t*r-n*s)/(r*r)),rh:-a*(e-n*t)/r,rk:-a*(t-n*e)/r,hh:-e*l*ae(c)-n/o*l*Jt(c),hk:a,kk:-t*f*ae(i)-n/o*f*Jt(i)}}function mp(e,t,n,r,o){let s=e.length,a=e[0].length,c=o?s+a-1:1,i={value:0,gradient:new Array(c).fill(0),hessian:Fa(c)},l=h=>o&&h>=1&&h<=s-1?h:-1,f=h=>o&&h>=1&&h<=a-1?s-1+h:-1,m=t.map(h=>n.map(p=>CN(h,p,r))),u=[];for(let h=0;h<s;h++)for(let p=0;p<a;p++){let g=e[h][p];if(g===0)continue;let d=_o([t[h],n[p]],[t[h+1],n[p+1]],r),v=new Array(c).fill(0),N=Fa(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)}hp(i,g,d,v,N),u.push(g*Math.log(d))}return i.value=Pe(u),i}function ON(e,t,n,r,o){let s=o?n.length+1:1,a={value:0,gradient:new Array(s).fill(0),hessian:Fa(s)},c=(1-r)*(1+r),i=Math.sqrt(c),l=c*i,f=new Array(s),m=Fa(s),u=[],h=(p,g,d)=>{let v=n[p],N=(v-r*g)/i,b=Jt(N),E=(r*v-g)/l,w=1/i,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)/i:1/0,N=d>0?(n[d-1]-r*g)/i:-1/0;d<n.length&&h(d,g,1),d>0&&h(d-1,g,-1),hp(a,1,lp(N,v),f,m),u.push(Math.log(lp(N,v)),Jt(g,{log:!0}))}return a.value=Pe(u),a}function FN(e,t){let n=new Array(e.gradient.length).fill(0),r=t.map(a=>e.gradient[a]),o=t.map(a=>t.map(c=>-e.hessian[a][c])),s;try{s=rt(o,r)}catch{s=void 0}return(s===void 0||!s.every(Number.isFinite)||Pe(s.map((a,c)=>a*r[c]))<=0)&&(s=r.map((a,c)=>a/Math.max(Math.abs(o[c][c]),1))),t.forEach((a,c)=>n[a]=s[c]),n}var pp=(e,t)=>e>=t-1e-12*Math.abs(t);function up(e,t,n){let r=[...t],o=e(r),s=n,a=r.map((c,i)=>i);for(let c=0;c<200;c++){let i=s?a.slice(1):a;if(i.length===0)break;let l=FN(o,i);if(!s&&Math.abs(r[0])===sn&&l[0]*r[0]>0){s=!0;continue}let f=Math.abs(r[0]+l[0])>sn?(Math.sign(l[0])*sn-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])>=sn*(1-1e-15)&&(h[0]=Math.sign(h[0])*sn);let p=e(h);if(pp(p.value,o.value)){let g=Et(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 dp(e,t){let n=up(e,[Math.max(-sn,Math.min(sn,t[0])),...t.slice(1)],!1);if(n.bounded||n.theta[0]===0)return n;let r=up(e,[Math.sign(n.theta[0])*sn,...n.theta.slice(1)],!0);return pp(r.at.value,n.at.value)?r:n}function gp(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((a,c)=>(a+r[c][s])/2))}var wp=e=>`inadmissible correlation set to ${e>0?"0.9999":"-0.9999"}`;function DN(e,t,n){ct(t)&&K(t,ap,"polychor"),K(n,ap,"polychor");let r,o;if(cp(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])),F=Vt(L.filter((Q,re)=>S[re])),k=Vt(D.filter((Q,re)=>S[re])),P=new Map(ml(F).map((Q,re)=>[Q,re])),B=new Map(ml(k).map((Q,re)=>[Q,re]));r=Array.from({length:P.size},()=>new Array(B.size).fill(0)),F.forEach((Q,re)=>r[P.get(Q)][B.get(k[re])]++),o=n??{}}else{let L=e;if(!L.every(cp))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(F=>!(F>=0&&F<1/0))))throw new RangeError("the counts of the table must be nonnegative and finite");o=t??{}}let{ML:s=!1,stdErr:a=!1}=o,c=[],i=0;Pe(r.map(L=>Pe(L)))<2**1e3||({groups:r,k:i}=Ie(r));let l=r.map(L=>Pe(L)),f=l.filter(L=>L===0).length;f>0&&(c.push(ip(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(ip(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,...fl(r.map(L=>Pe(L))),1/0],d=[-1/0,...fl(r[0].map((L,D)=>Pe(r.map(S=>S[D])))),1/0],N=Zl(L=>{let D=Math.max(-sn,Math.min(sn,L)),S=[];for(let F=0;F<h;F++)for(let k=0;k<p;k++){if(r[F][k]===0)continue;let P=_o([g[F],d[k]],[g[F+1],d[k+1]],D);S.push(r[F][k]*Math.log(P))}return-Pe(S)},-sn,sn).optimum;if(!s&&!a)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?mp(r,b(L.slice(1,h)),b(L.slice(h)),L[0],!0):mp(r,g,d,L[0],!1),{theta:x,at:M,bounded:R}=dp(y,s?[N,...E,...w]:[N]),A=x[0];if(R&&c.push(wp(A)),!a)return J("polychor",{rho:A,warnings:c});let T=s?b(x.slice(1,h)):g,q=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],F=_o([T[L],q[D]],[T[L+1],q[D+1]],A);if(S===0)_.push(C*F);else if(i===0)_.push(S*Math.log1p(1e-6/S),S*fp(C*F/S-1));else{let k=$(S,i);I.push(k*Math.log1p(1e-6/k)),_.push(S*fp(C*F/S-1))}}let O=gp(M.hessian,R);return J("polychor",{rho:A,warnings:c,var:i===0?O:O.map(L=>L.map(D=>$(D,-i))),chisq:i===0?2*Pe(_):$(2*Pe(_),i)+2*Pe(I),df:h*p-h-p,n:$(C,i),rowCuts:T.slice(1,h),colCuts:q.slice(1,p)})}function PN(e,t,n,r){let o=e.length,s=[ie(.25),ie(.5),ie(.75)],a=(h,p)=>p.filter(g=>g<h).length,c=ul(e).map(h=>a(h,n)),i=ul(t).map(h=>a(h,s)),l=Array.from({length:n.length+1},()=>new Array(4).fill(0));c.forEach((h,p)=>l[h][i[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=_o([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 VN(e,t,n={}){if(K(n,LN,"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])),a=e.filter((M,R)=>s[R]),c=Vt(t.filter((M,R)=>s[R])),i=a.length,l=ml(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=fl(u),p=[],g=Math.sqrt((i-1)/i)*Math.sqrt(at(m))*ef(a,m)[0]/Pe(h.map(M=>Jt(M)));if(Math.abs(g)>sn){let M=Math.sign(g)*sn;p.push(`initial correlation inadmissible, ${zn(g)}, set to ${zn(M)}`),g=M}if(!r&&!o)return J("polyserial",{rho:g,warnings:p});let d=ul(a),v=m.map(M=>M-1),N=M=>ON(d,v,r?M.slice(1):h,M[0],r),{theta:b,at:E,bounded:w}=dp(N,r?[g,...h]:[g]);if(w&&p.push(wp(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:gp(E.hessian,w),chisq:PN(m,d,y,r?b[0]:g),df:4*x-x-1,n:i,cuts:y})}var $N=["group","strata","rho","cencode"],jN=new Set(["cov2","tf","failcode","cencode","gtol","maxiter","init","variance","confLevel","names"]);function Da(e){return e==null||typeof e=="number"&&Number.isNaN(e)}function hl(e){let t=rn(e),n=Array.from(new Set(t)).sort(Kn);return{labels:n,index:new Map(n.map((r,o)=>[r,o]))}}function zN(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 KN(e,t,n={}){K(n,$N,"cuminc"),zN(e,t,n);let{group:r,strata:o,rho:s=0,cencode:a=0}=n,c=[],i=new mt;for(let w=0;w<e.length;w++)if(!(Number.isNaN(e[w])||Da(t[w])||r!==void 0&&Da(r[w])||o!==void 0&&Da(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&&i.add(`${l} cases omitted due to missing values`),c.length===0)throw new RangeError("no complete observations");let f=r===void 0?[1]:rn(c.map(w=>r[w])),m=o===void 0?[1]:rn(c.map(w=>o[w])),u=rn([...c.map(w=>t[w]),a]),h=hl(f);if(h.labels.length>=2&&!Number.isFinite(s))throw new RangeError("'rho' must be finite when Gray's test is requested");let p=hl(m),g=hl(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=Mt(w);for(let x=0;x<h.labels.length;x++){let M=N.filter(S=>S.group===x),R=M.reduce((S,F)=>Math.max(S,F.time),-1/0),A=R>0&&R<1?2**Math.min(1023,Math.ceil(-Math.log2(R))):1,T=dr(M.map(S=>S.time*A),M.map(S=>S.status===d?0:1)),q=[0],C=[0],_=[0],I=[],O=1;for(let S=0;S<T.time.length;S++){let F=T.nEvent[S];if(F===0)continue;let k=T.time[S]/A,P=T.nRisk[S],B=M.filter(Q=>Q.time===k&&Q.status===w).length;I.push({t:k,Y:P,d:F,dk:B,sb:O,sa:T.surv[S]}),O=T.surv[S]}let L=0,D=0;for(let S=0;S<I.length;S++){let F=I[S],k=L,P=D;L+=F.sb*F.dk/F.Y;let B=0;for(let Q=0;Q<=S;Q++){let re=I[Q],z=re.Y,ee=re.d,U=re.dk,ue=z===ee?0:(ee*(z-ee)+2*U*(ee-U))/((z-1)*(z-ee)**2),Z=z===1?U:U*(z-U)/(z*z*(z-1)),V=z===ee||z===1?0:U*(z-U)/(z*(z-ee)*(z-1)),Y=0;for(let H=0;H<=Q;H++){let W=I[H];Y+=W.sb*W.dk/W.Y}let j=L-Y;B+=j*j*ue+re.sb*re.sb*Z-2*j*re.sb*V}D=Math.max(0,B),F.dk>0&&(q.push(F.t,F.t),C.push(k,L),_.push(P,D))}q.push(R),C.push(L),_.push(D),b.push({name:`${Mt(h.labels[x])} ${y}`,group:Mt(h.labels[x]),cause:y,time:q,est:C,var:_})}}let E=h.labels.length>=2?WN(N,h.labels,v,p.labels.length,s,d):void 0;return J("cuminc",{curves:b,...E===void 0?{}:{tests:E},warnings:i.list})}function WN(e,t,n,r,o,s){let a=[];for(let c of n){let i=t.length,l=new Array(i).fill(0),f=Array.from({length:i},()=>new Array(i).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(i).fill(1),b=new Array(i).fill(0),E=[];for(let w of v){let y=new Array(i).fill(0),x=new Array(i).fill(0),M=new Array(i).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(),A=b.slice(),T=y.map((S,F)=>S===0?0:S/R[F]),q=y.map((S,F)=>S===0?0:S*(1-A[F])/R[F]),C=x.reduce((S,F)=>S+F,0),_=T.reduce((S,F)=>S+F,0),I=_===0?0:C/_,O=E.length===0?1:E[E.length-1].Gafter,L=O-I,D=q.reduce((S,F)=>S+F,0);for(let S=0;S<i;S++){let F=O**o;D>0&&(l[S]+=F*(x[S]-q[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:A,h:T,R:q,dF0:I,Gbefore:O,Gafter:L})}for(let w=0;w<i;w++)for(let y=0;y<i;y++){let x=0;for(let M=0;M<i;M++)for(let R=0;R<E.length;R++){let A=E[R],T=A.h.reduce((ee,U)=>ee+U,0);if(!(T>0)||!(A.h[M]>0))continue;let q=A.Gbefore**o,C=q*A.h[w]*((w===M?1:0)-A.h[M]/T),_=q*A.h[y]*((y===M?1:0)-A.h[M]/T),I=0,O=0;for(let ee=R+1;ee<E.length;ee++){let U=E[ee],ue=U.h.reduce((H,W)=>H+W,0);if(!(ue>0))continue;let Z=U.Gbefore**o,V=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),j=U.Gbefore===0?0:U.dF0/U.Gbefore;I+=V*j,O+=Y*j}let L=A.Safter[M]===0?0:A.Gafter/A.Safter[M],D=C+(1-L)*I,S=_+(1-L)*O,F=-L*I,k=-L*O,P=A.Y[M]===0?0:A.Sbefore[M]*A.d2[M]/A.Y[M],B=(D*S*A.dF0+F*k*P)/A.h[M],Q=A.d1.reduce((ee,U)=>ee+U,0),re=0;if(Q>1&&A.Y[M]>0&&A.Sbefore[M]>0){let ee=1/A.Sbefore[M];T>ee&&(re=Q*(Q-1)/T*(A.Y[M]*ee*ee/(T-ee))*(D*S/(A.h[M]*A.h[M])))}let z=0;A.d2[M]>1&&A.Y[M]>1&&(z=(A.d2[M]-1)/(A.Y[M]-1)*(P/A.h[M])*F*k),x+=B-re-z}f[w][y]+=x}}let m=i-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=Nt(u,m,{lowerTail:!1})}catch{u=NaN,h=NaN}a.push({cause:Mt(c),stat:u,df:m,pValue:h})}return a}function BN(e,t){let n=Array.from(new Set(t.filter(s=>!Number.isNaN(s)))).sort((s,a)=>s-a),r=[],o=[];for(let s of e.curves){let a=[],c=[];for(let i of n){let l=-1;for(let f=0;f<s.time.length;f++)s.time[f]<=i&&(l=f);i<0||l<0||i>s.time[s.time.length-1]?(a.push(NaN),c.push(NaN)):(a.push(s.est[l]),c.push(s.var[l]))}r.push(a),o.push(c)}return{curveNames:e.curves.map(s=>s.name),times:n,est:r,var:o}}function On(e,t=e){return Array.from({length:e},()=>new Array(t).fill(0))}function UN(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 pl(e,t){return Pe(e.map((n,r)=>n*t[r]))}function HN(e){return e.every(t=>t.every(Number.isFinite))}function YN(e,t,n,r={}){K(r,jN,"crr");let{cov2:o,tf:s,failcode:a=1,cencode:c=0,gtol:i=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(!(i>0)||!Number.isFinite(i))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(W=>W.length!==p))throw new RangeError("rows of 'cov1' must have equal length");if(o!==void 0&&o.some(W=>W.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 W=0;W<e.length;W++)if(!(Number.isNaN(e[W])||Da(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=rn([...v.map(W=>t[W]),c,a]),w=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])),A=Array.from(new Set(b.filter((W,te)=>w[te]===x))).sort((W,te)=>W-te);if(A.length===0)throw new RangeError("no observations of 'failcode'");let T=g===0?[[0]]:s(A).map(W=>Array.from(W));if(g>0&&(T.length!==A.length||T.some(W=>W.length!==g)||!HN(T)))throw new RangeError("'tf' must return a finite matrix with one row per unique failure time and one column per cov2 column");let q=A.map((W,te)=>b.map((se,Me)=>[...M[Me],...R[Me].map((Ne,de)=>Ne*(T[te][de]??0))])),C=b.reduce((W,te)=>Math.max(W,Math.abs(te)),0),_=C>0&&C<1?2**Math.min(1023,Math.ceil(-Math.log2(C))):1,I=dr(b.map(W=>W*_),w.map(W=>W===y?1:0)),O=W=>{let te=W*_,se=1;for(let Me=0;Me<I.time.length&&!(I.time[Me]>=te);Me++)se=I.surv[Me];return se},L=A.map(W=>b.map((te,se)=>{if(te>=W)return 1;if(w[se]===y||w[se]===x)return 0;let Me=O(te);return Me===0?0:O(W)/Me}));for(let W=0;W<A.length;W++)for(let te=0;te<b.length;te++)b[te]===A[W]&&w[te]===x&&(L[W][te]=1);let D=W=>{let te=Array.from({length:d},()=>[]),se=Array.from({length:d},()=>Array.from({length:d},()=>[])),Me=[],Ne=On(A.length,d),de=[],ce=[],qe=[];for(let Oe=0;Oe<A.length;Oe++){let Be=A[Oe],je=b.map((me,Se)=>me===Be&&w[Se]===x?Se:-1).filter(me=>me>=0),tt=je.length,Ue=q[Oe],Qe=Ue.map(me=>pl(W,me)),ge=L[Oe].slice();for(let me of je)ge[me]=1;let oe=Math.max(...Qe.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?Qe.map((me,Se)=>ge[Se]*Math.exp(me)):Qe.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),Ae=Array.from({length:d},(me,Se)=>{let Ke=Ue[0][Se],$e=0,st=0;for(let ut of Ue)$e=Math.max($e,Math.abs(ut[Se]-Ke)),st=Math.max(st,Math.abs(ut[Se]));return $e>0&&st/$e>1e6}).some(Boolean),Ee=Ae?Array.from({length:d},(me,Se)=>Ue.map(Ke=>Ke[Se]-Ue[0][Se])):[],Te=Ae?Ee.map(me=>Pe(me.map((Se,Ke)=>be[Ke]*Se))/fe):[],ne=Ae?void 0:Array.from({length:d},(me,Se)=>Array.from({length:d},(Ke,$e)=>Pe(be.map((st,ut)=>st*Ue[ut][Se]*Ue[ut][$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(je.map(me=>pl(W,Ue[me])))-tt*(ve?Math.log(fe):oe+Math.log(fe)));for(let me=0;me<d;me++){let Ke=Pe(je.map($e=>Ue[$e][me]))-tt*X[me];te[me].push(Ke),Ne[Oe][me]=Ke}for(let me=0;me<d;me++)for(let Se=0;Se<d;Se++){let Ke=Ae?Pe(be.map(($e,st)=>$e*(Ee[me][st]-Te[me])*(Ee[Se][st]-Te[Se])))/fe:ne[me][Se]/fe-X[me]*X[Se];se[me][Se].push(tt*Ke)}}let we=On(b.length,d);for(let Oe=0;Oe<A.length;Oe++){let Be=q[Oe],je=qe[Oe],tt=ce[Oe],Ue=A[Oe];for(let Qe=0;Qe<b.length;Qe++){let ge=b[Qe]===Ue&&w[Qe]===x?1:0,oe=b.filter((be,fe)=>be===Ue&&w[fe]===x).length,ve=ge-je[Qe]*oe;for(let be=0;be<d;be++)we[Qe][be]+=(Be[Qe][be]-tt[be])*ve}}let Fe=te.map(Pe),xe=se.map(Oe=>Oe.map(Pe));return{loglik:Pe(Me),score:Fe,inf:xe,bfitj:de,res:Ne,eta:we,means:ce,riskFactors:qe,zByTime:q}},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 F=S.slice(),k=D(F),P=!1,B=0,Q=(W,te)=>Math.max(...W.score.map((se,Me)=>Math.abs(se)*Math.max(Math.abs(te[Me]),1)))<i*Math.max(Math.abs(W.loglik),1);for(;;){if(Q(k,F)){P=!0;break}if(B>=l)break;let W=rt(k.inf,k.score,{tol:0});F=F.map((te,se)=>te+W[se]),B++,k=D(F)}let re=rt(k.inf),z=On(d);if(m){let W=On(d),te=b.length,se=Array.from(new Set(b)).sort((ce,qe)=>ce-qe),Me=se.map(ce=>{let qe=b.filter(Fe=>Fe>=ce).length,we=b.filter((Fe,xe)=>Fe===ce&&w[xe]===y).length;return qe===0?0:we/qe}),Ne=On(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<A.length;_e++){if(A[_e]<qe)continue;let Oe=k.riskFactors[_e][xe],Be=b.filter((Ue,Qe)=>Ue===A[_e]&&w[Qe]===x).length,je=k.zByTime[_e][xe],tt=k.means[_e];for(let Ue=0;Ue<d;Ue++)we[Ue]+=(je[Ue]-tt[Ue])*Oe*Be}let Fe=b.filter(xe=>xe>=qe).length;if(Fe!==0)for(let xe=0;xe<te;xe++){let _e=b[xe]===qe&&w[xe]===y?1:0,Oe=b[xe]>=qe?Me[ce]:0;for(let Be=0;Be<d;Be++)Ne[xe][Be]+=we[Be]/Fe*(_e-Oe)}}for(let ce=0;ce<te;ce++)UN(W,k.eta[ce].map((qe,we)=>qe+Ne[ce][we]),k.eta[ce].map((qe,we)=>qe+Ne[ce][we]),1);z=On(d);let de=bp(re,W);z=bp(de,re)}else z=On(d).map(W=>W.map(()=>NaN));let ee=m?z.map((W,te)=>Math.sqrt(W[te])):void 0,U=ee===void 0?void 0:F.map((W,te)=>W/ee[te]),ue=U===void 0?void 0:U.map(W=>2*ae(-Math.abs(W))),Z=m?ie((1+u)/2):void 0,V=Z===void 0?void 0:F.map((W,te)=>Math.exp(W-Z*ee[te])),Y=Z===void 0?void 0:F.map((W,te)=>Math.exp(W+Z*ee[te])),j=m?F.map(Math.exp):void 0,H=h===void 0?[...Array.from({length:p},(W,te)=>`cov1${te+1}`),...Array.from({length:g},(W,te)=>`cov2${te+1}*tf${te+1}`)]:Array.from(h);return J("crr",{coefficients:F,names:H,var:z,...ee===void 0?{}:{se:ee,z:U,pValue:ue,lower:V,upper:Y,expCoef:j},loglik:k.loglik,loglikNull:D(new Array(d).fill(0)).loglik,logtest:{statistic:2*(k.loglik-D(new Array(d).fill(0)).loglik),df:d},score:k.score,inf:m?k.inf:On(d).map(W=>W.map(()=>NaN)),invinf:m?re:On(d).map(W=>W.map(()=>NaN)),converged:P,iterations:B,n:v.length,nMissing:N,uftime:A,bfitj:k.bfitj,res:m?k.res:null,tfs:T,warnings:[],covariateDimensions:[p,g]})}function bp(e,t){let n=On(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 GN(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(i=>i.length!==r))throw new RangeError("'cov1' has the wrong number of columns");if(o>0&&n.some(i=>i.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,a=o===0?e.uftime.map(()=>[0]):e.tfs;return e.uftime.map((i,l)=>{let f=[i];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*a[h][E])],g=e.bfitj[h],d=pl(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 XN=new Set(["type","time2","dist","weights","offset","strata","scale","init","df","control","intercept","confLevel"]),QN=["maxiter","relTolerance","tolerChol"],ZN=["type","p","newX","newStrata","newOffset","seFit"],JN=["type","weighted"],gl=new WeakMap,ey=Math.log(2*Math.PI),ty=e=>Number(e),ny=e=>e<-Math.LN2?Math.log1p(-Math.exp(e)):Math.log(-Math.expm1(e));function Rp(e,t){return t===-1/0?e:e<t?NaN:e+ny(t-e)}function ry(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 vp(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 oy(e,t,n){let r=t-e;return Number.isFinite(r)?r/n:t/n-e/n}function Ap(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 sy(e,t,n){let r=Ap(e,t,n),o=r>Math.log(Number.MAX_VALUE)?1/0:Math.exp(r);if(o<1e-8){let s=t-e;return bn(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 ay(e,t,n){let r=Ap(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)),a=r-s,c=o>50?r-Math.log1p(-Math.exp(-o)):a+o;return Rp(-Math.exp(a),-Math.exp(c))}function iy(e){return e<-36?e:-Math.log1p(Math.exp(-e))}function cy(e){return e>36?-e:-Math.log1p(Math.exp(e))}function Np(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"?iy(e):t==="gaussian"||t==="loggaussian"||t==="lognormal"?ae(e,{logP:!0}):Wt(e,n,{logP:!0})}function yp(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"?cy(e):t==="gaussian"||t==="loggaussian"||t==="lognormal"?ae(e,{lowerTail:!1,logP:!0}):Wt(e,n,{lowerTail:!1,logP:!0})}function Pa(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+ey):Fl(e,n,{log:!0})}function dl(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 ly(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 Va(e,t,n,r,o,s,a){let c=e===-1/0?-1/0:(e-r)/o,i=t===1/0?1/0:(t-r)/o;if(n===1){let[_]=dl(i,s,a),I=Pa(i,s,a),O=ly(i,s,a);return{g:I-Math.log(o),e:-_/o,ee:O/(o*o),t:-i*_-1,tt:i*_+i*i*O,et:(_+i*O)/o}}let l=Np(i,s,a),f=Np(c,s,a),m=yp(c,s,a),u=yp(i,s,a),h=n===0?m:n===2?l:Rp(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:Pa(c,s,a)-h),d=i===1/0?0:Math.exp(Pa(i,s,a)-h),[v]=dl(c,s,a),[N]=dl(i,s,a),b=g===0||c===-1/0?0:g*v,E=d===0||i===1/0?0:d*N,w=c===-1/0||g===0?0:c*g,y=i===1/0||d===0?0:i*d,x=c===-1/0||b===0?0:c*c*b,M=i===1/0||E===0?0:i*i*E,R=-(d-g)/o,A=(E-b)/(o*o)-R*R,T=-(y-w),q=M-x-T*(1+T),C=((i===1/0||E===0?0:i*E)-(c===-1/0||b===0?0:c*b))/o-R*(1+T);return{g:h,e:R,ee:A,t:T,tt:q,et:C}}function fy(e){let t=e.length,n=e.map((r,o)=>[...r,...r.map((s,a)=>o===a?1:0)]);for(let r=0;r<t;r++){let o=r;for(let a=r+1;a<t;a++)Math.abs(n[a][r])>Math.abs(n[o][r])&&(o=a);if(!Number.isFinite(n[o][r])||Math.abs(n[o][r])<=1e-13*Math.max(Number.MIN_VALUE,...e.map((a,c)=>Math.abs(a[c]??0))))throw new Error("singular information matrix");[n[r],n[o]]=[n[o],n[r]];let s=n[r][r];for(let a=0;a<2*t;a++)n[r][a]/=s;for(let a=0;a<t;a++)if(a!==r){let c=n[a][r];for(let i=0;i<2*t;i++)n[a][i]-=c*n[r][i]}}return n.map(r=>r.slice(t))}function Tp(e,t){let n=e.length,r=0,o=!1;for(let i=0;i<n;i++){let l=e[i][i];l>0&&(!o||l>r)&&(r=l,o=!0)}let s=o?r*t:t,a=0,c=!0;for(let i=0;i<n;i++){let l=e[i][i];if(!Number.isFinite(l)||l<=s){e[i][i]=0,l<-8*s&&(c=!1);for(let f=i+1;f<n;f++)e[f][i]=0;continue}a++;for(let f=i+1;f<n;f++){let m=e[f][i]/l;e[f][i]=m,e[f][f]-=m*m*l;for(let u=f+1;u<n;u++)e[u][f]-=m*e[u][i]}}return c?a:-a}function Sp(e,t){let n=e.length,r=t.slice();for(let o=0;o<n;o++){let s=r[o];for(let a=0;a<o;a++)s-=r[a]*e[o][a];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 a=o+1;a<n;a++)s-=r[a]*e[a][o];r[o]=s}return r}function Mp(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 ps(e,t){return e.map(n=>yr(n,t))}function yr(e,t){let n=e.reduce((r,o,s)=>r+o*t[s],0);return Io(e,t,n)}function kp(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 a=e.map(i=>i[s]),c=0;for(let i of a)c=Math.max(c,Math.abs(i));if(c>n||c>0&&c<1/n){let i=Ie([a]);return{values:i.groups[0],exponent:i.k}}return{values:a,exponent:0}});return{values:e.map((o,s)=>r.map(a=>a.values[s])),exponent:r.map(o=>o.exponent)}}function Ep(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):Bt(e,n)}function xp(e,t,n,r,o,s,a,c,i,l,f,m,u,h,p,g=1e-10){let d=e[0].length,v=d+(m===void 0?i:0),N=kp(e,g),b=N.values,E=N.exponent,w=u.map((k,P)=>P<d?Ve(k,E[P]):k),y=(k,P)=>{let B=0,Q=Array(v).fill(0),re=Array.from({length:v},()=>Array(v).fill(0)),z=Array.from({length:v},()=>Array(v).fill(0));for(let ee=0;ee<t.length;ee++){let U=c[ee],ue=yr(b[ee],k.slice(0,d))+a[ee],Z=m??Math.exp(k[d+U]),V=Va(n[ee],r[ee],o[ee],ue,Z,l,f),Y=s[ee];B+=Y*V.g;let j=Array(v).fill(0);for(let H=0;H<d;H++)j[H]=b[ee][H]*V.e;m===void 0&&(j[d+U]=V.t);for(let H=0;H<v;H++){Q[H]+=Y*j[H];for(let W=0;W<v;W++)z[H][W]+=Y*j[H]*j[W]}if(P){for(let H=0;H<d;H++){for(let W=0;W<d;W++)re[H][W]+=Y*b[ee][H]*b[ee][W]*V.ee;m===void 0&&(re[H][d+U]+=Y*b[ee][H]*V.et)}if(m===void 0){for(let H=0;H<d;H++)re[d+U][H]+=Y*b[ee][H]*V.et;re[d+U][d+U]+=Y*V.tt}}}return{ll:B,u:Q,H:re,B:z}},x=k=>{let P=k.H.map(Q=>Q.map(re=>-re)),B=Tp(P,g);return{factor:P,rank:B}},M=k=>{let P=y(k,!0),B=x(P),Q=B.rank>=0?Sp(B.factor,P.u):ps(fy(P.B),P.u);return{e:P,delta:Q}},R=y(w,!0),A=R.ll,T=0,q=!1,C=!1,_;try{_=M(w)}catch{_={e:R,delta:Array(v).fill(0)}}let I=w.map((k,P)=>k+_.delta[P]);if(h===0){let k=x(R),P=Mp(k.factor),B=P.map(re=>re.slice()),Q=P.map((re,z)=>re[z]===0?NaN:w[z]/Math.sqrt(Math.max(0,re[z])));return{b:w.map((re,z)=>z<d?Ve(re,-E[z]):re),ll:R.ll,iter:T,V:P.map((re,z)=>re.map((ee,U)=>Ve(ee,(z<d?-E[z]:0)+(U<d?-E[U]:0)))),VScaled:B,xExponent:E,score:R.u.map((re,z)=>z<d?Ve(re,E[z]):re),z:Q,warn:q}}for(let k=1;k<=h;k++){let P=y(I,!0);if(Number.isFinite(P.ll)&&Math.abs(1-A/P.ll)<=p){w.splice(0,w.length,...I),R=P,A=P.ll,T=k,C=!0;break}if(!Number.isFinite(P.ll)||P.ll<A){let B=I.slice();I=w.map((Q,re)=>Q+(B[re]-Q)/3);for(let Q=0;Q<i;Q++)m===void 0&&(I[d+Q]=Math.max(I[d+Q],w[d+Q]-1.1));T=k;continue}if(w.splice(0,w.length,...I),R=P,A=P.ll,T=k,k<h)try{let B=M(w);I=w.map((Q,re)=>Q+B.delta[re])}catch{I=w.slice()}else try{let B=M(w),Q=w.map((z,ee)=>z+B.delta[ee]),re=y(Q,!1);Number.isFinite(re.ll)&&re.ll>=A&&w.splice(0,w.length,...Q)}catch{}}T===h&&h>=2&&!C&&(q=!0);let O=y(w,!0),L=x(O),D=Mp(L.factor),S=D.map(k=>k.slice()),F=D.map((k,P)=>k[P]===0?NaN:w[P]/Math.sqrt(Math.max(0,k[P])));return{b:w.map((k,P)=>P<d?Ve(k,-E[P]):k),ll:A,iter:T,V:D.map((k,P)=>k.map((B,Q)=>Ve(B,(P<d?-E[P]:0)+(Q<d?-E[Q]:0)))),VScaled:S,xExponent:E,score:O.u.map((k,P)=>P<d?Ve(k,E[P]):k),z:F,warn:q}}function my(e,t,n=[],r={}){K(r,XN,"survreg"),K(_t(r,"control"),QN,"survreg","control");let o=r.type??"right",s=r.dist??"weibull",a=r.df??4,c=r.intercept??!0,i=e.length,l=t===void 0?Array(i).fill(1):Array.from(t,Number);if(l.length!==i||n.length!==0&&n.length!==i)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!==i)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"&&(!(a>=3)||!Number.isFinite(a)))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(i).fill(1),g=r.offset??Array(i).fill(0),d=r.strata??Array(i).fill("1");for(let[X,he]of[[p,"weights"],[g,"offset"],[d,"strata"]])if(X.length!==i)throw new RangeError(`variable lengths differ (found for '${he}')`);let v=[],N=[],b=[],E=[],w=[],y=[],x=[],M=[],R=[],A=(o==="right"||o==="left")&&l.some(X=>Number(X)===2),T=X=>{R.includes(X)||R.push(X)},q=[...new Set(d.map(String))].sort(),C=d.map(X=>q.indexOf(String(X))),_=[];if(u!==void 0&&q.length>1)throw new Error("The scale argument is not valid with multiple strata");for(let X=0;X<i;X++){let he=n.length===0?[]:Array.from(n[X]);if((o==="interval2"?Number.isNaN(e[X])&&Number.isNaN(h[X]):o==="interval"&&ty(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],Te=e[X],ne=Number(l[X]);if(o==="right")if(A){if(ne!==1&&ne!==2){T("Invalid status value, converted to NA");continue}ne===1&&(Te=1/0),ne=ne===2?1:0}else{if(ne!==0&&ne!==1){T("Invalid status value, converted to NA");continue}ne===0&&(Te=1/0),ne=ne===1?1:0}else if(o==="left")if(A){if(ne!==1&&ne!==2){T("Invalid status value, converted to NA");continue}ne===1&&(Ee=-1/0),ne=ne===2?1:2}else{if(ne!==0&&ne!==1){T("Invalid status value, converted to NA");continue}ne===0&&(Ee=-1/0),ne=ne===1?1:2}else if(o==="interval"){if(Te=h[X],ne===0)Ee=e[X],Te=1/0;else if(ne===1)Te=Ee;else if(ne===2)Te=e[X],Ee=-1/0;else if(ne!==3){T("Invalid status value, converted to NA");continue}if(ne===3&&Ee>Te){T("Invalid interval: start > stop, NA created");continue}if(ne===3&&Ee===Te)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){T("Invalid interval: start > stop, NA created");continue}ye===me?(Ee=ye,Te=me,ne=1):!Number.isFinite(ye)||ye===0?(Ee=-1/0,Te=me,ne=2):Number.isFinite(me)?(Ee=ye,Te=me,ne=3):(Ee=ye,Te=1/0,ne=0)}if(f){if((Number.isFinite(Ee)&&Ee<0||Number.isFinite(Te)&&Te<0)&&T("NaNs produced"),Ee===1/0||Te===-1/0||Number.isFinite(Ee)&&Ee<=0||Number.isFinite(Te)&&Te<=0)throw new Error("Invalid survival times for this distribution");Number.isFinite(Ee)&&(Ee=Math.log(Ee)),Number.isFinite(Te)&&(Te=Math.log(Te))}v.push(X),N.push(ne===1?Te:Number.isFinite(Ee)&&Number.isFinite(Te)?bn(Ee,Te):Number.isFinite(Ee)?Ee:Te),b.push(Ee),E.push(Te),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,O=I+(c?1:0),L=M.map(X=>c?[1,...X]:X),D=q.length,S=N.map((X,he)=>X-x[he]),F=y.reduce((X,he)=>X+he,0),k=S.reduce((X,he,Ae)=>X+he*y[Ae],0)/F,P=S.reduce((X,he,Ae)=>X+y[Ae]*(he-k)**2,0)/F,B=Math.max(...S)-Math.min(...S),Q=s==="extreme"||s==="weibull"||s==="exponential"||s==="rayleigh"?P/1.64:s==="logistic"||s==="loglogistic"?P/3.2:s==="t"?P*(a-2)/a:P,re=u??2*Math.sqrt(Q);if(u===void 0&&(!(re>0)||!(B>0)))throw new Error("initial iteration failed (use starting estimates?)");let z=O+(u===void 0?D:0),ee=Array(z).fill(0);if(c&&(ee[0]=k),u===void 0)for(let X=0;X<D;X++)ee[O+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 V=L.map(()=>[1]),Y=L.every(X=>X.length===1&&X[0]===1);if(r.init!==void 0&&r.init.length!==O&&r.init.length!==z)throw new Error("Wrong length for initial parameters");let H=N.map((X,he)=>Va(b[he],E[he],w[he],X,u??re,s,a)).map((X,he)=>{let Ae=-y[he]*X.ee;return Ae>0&&Number.isFinite(Ae)?{w:Ae,r:N[he]-X.e/X.ee-x[he]}:{w:0,r:0}}),W=H.reduce((X,he)=>X+he.w,0);if(!(W>0))throw new Error("initial iteration failed (use starting estimates?)");let te=H.reduce((X,he)=>X+he.w*he.r,0)/W,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===z)for(let X=0;X<D;X++)se[1+X]=r.init[1+X];let Me=Y?ue:Math.max(ue,1e3),Ne=xp(V,N,b,E,w,y,x,_,D,s,a,u,se,Me,Z,U.tolerChol??1e-10),de=ee.slice();if(u===void 0)for(let X=0;X<D;X++)de[O+X]=Ne.b[1+X];if(!Y&&r.init!==void 0){for(let X=0;X<O;X++)de[X]=r.init[X];if(r.init.length===z)for(let X=O;X<z;X++)de[X]=r.init[X]}else if(!Y&&O>0){let X=M.map((Te,ne)=>{let ye=u??Math.exp(Ne.b[1+_[ne]]),me=Va(b[ne],E[ne],w[ne],N[ne],ye,s,a),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=kp(L,U.tolerChol??1e-10),Ae=Array.from({length:O},(Te,ne)=>Array.from({length:O},(ye,me)=>he.values.reduce((Se,Ke,$e)=>Se+X[$e].w*Ke[ne]*Ke[me],0))),Ee=Array.from({length:O},(Te,ne)=>he.values.reduce((ye,me,Se)=>ye+X[Se].w*me[ne]*X[Se].r,0));try{let Te=Ae.map(me=>me.slice()),ye=Tp(Te,U.tolerChol??1e-10)>=0?Sp(Te,Ee):ps(rt(Ae),Ee);de.splice(0,O,...ye.map((me,Se)=>Ve(me,-he.exponent[Se])))}catch{}}let ce=Y?Ne:xp(L,N,b,E,w,y,x,_,D,s,a,u,de,ue,Z,U.tolerChol??1e-10),qe=f?v.reduce((X,he,Ae)=>X+(w[Ae]===1?y[Ae]*Math.log(e[he]):0),0)*-1:0,we=ce.V.map(X=>X.slice()),Fe=ce.b.slice(0,O).map((X,he)=>Number.isNaN(ce.z[he])?NaN:X),xe=Fe.map((X,he)=>(we[he]?.[he]??0)===0?0:Math.sqrt(Math.max(0,we[he]?.[he]??0))),Oe=Fe.map((X,he)=>ce.z[he]).map(X=>2*ae(-Math.abs(X))),Be=u!==void 0?[u]:Array.from({length:D},(X,he)=>Math.exp(ce.b[O+he])),je=Array.from({length:O},(X,he)=>L.reduce((Ae,Ee)=>Ae+Ee[he],0)/L.length),tt=L.map((X,he)=>yr(X,ce.b.slice(0,O))+x[he]),Ue=2*(ce.ll+qe-(Ne.ll+qe)),Qe=O+(u===void 0?D:0)-(1+(u===void 0?D:0)),ge=[...Fe.map((X,he)=>he===0&&c?"(Intercept)":`x${he+(c?0:1)}`),...u===void 0?D===1?["Log(scale)"]:q:[]],oe=[...Fe,...u===void 0?ce.b.slice(O):[]],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:Fe,coefNames:ge.slice(0,O),scale:Be,table:{value:oe,se:ve,z:be,p:fe},strataLevels:q.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:O+(u===void 0?D:0),idf:1+(u===void 0?D:0),dfResidual:v.length-(O+(u===void 0?D:0)),n:v.length,linearPredictors:tt,means:je,score:ce.score.slice(),tableRowNames:ge,value:oe,se:ve,z:be,p:fe,chi:Ue,chiDf:Qe,chiP:Nt(Ue,Qe,{lowerTail:!1}),dist:s,distributionDf:a,parms:a,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 gl.set(nt,{V:ce.VScaled,xExponent:ce.xExponent}),J("survreg",nt)}function uy(e,t={}){K(t,ZN,"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),a=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(a.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),i=["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]:yr(h,e.coefficients)+o[p],d=e.scale.length===1?e.scale[0]:e.scale[a[p]];return n==="lp"||n==="linear"||n==="link"?g:n==="response"?i?Math.exp(g):g:c.map(v=>{let N=g+d*Ep(v,e.dist,e.distributionDf);return n==="uquantile"?N:i?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]:yr(h,e.coefficients)+o[p],d=e.scale.length===1?e.scale[0]:e.scale[a[p]],v=gl.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+a[p]]=E*d),Math.sqrt(Math.max(0,yr(w,ps(N,w))))};return n==="lp"||n==="linear"||n==="link"?b(0):n==="response"?i?vp(g,b(0)):b(0):c.map(E=>{let w=Ep(E,e.dist,e.parms),y=b(w),x=g+d*w;return n==="uquantile"?y:i?vp(x,y):y})}),u=(n==="quantile"||n==="uquantile")&&c.length===1?m.map(h=>h[0]):m;return{fit:f,seFit:u,warnings:[]}}function hy(e,t={}){K(t,JN,"survregResiduals");let n=t.type??"response",r=e.distributionDf,o=e.coefficients.length,s=e.var.length,a=t.weighted??!1;if(!["response","deviance","working","dfbeta","dfbetas","ldcase","ldresp","ldshape","matrix"].includes(n))throw new RangeError("invalid residual type");let c=gl.get(e),i=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=Va(e.lower[m],e.upper[m],e.status[m],u,h,e.dist,r),g=a?e.weights[m]:1,d=f.map((R,A)=>c?Ve(R,-c.xExponent[A]):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=ps(i,v),b=N.map((R,A)=>c&&A<o?Ve(R,-c.xExponent[A]):R),E=yr(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,A)=>R/Math.sqrt(i[A]?.[A]??NaN)*g);if(n==="ldcase")return E*g;if(n==="ldresp"||n==="ldshape"){let R=Array(s).fill(0);for(let A=0;A<o;A++)R[A]=n==="ldresp"?h*(-d[A]*p.ee):d[A]*p.et;return s>o&&(R[o+e.strata[m]]=n==="ldresp"?-h*p.et:p.tt),yr(R,ps(i,R))*g}let w=Pa(0,e.dist,r)-Math.log(h),y=e.status[m]===3?bn(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=sy(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 A=oy(e.lower[m],e.upper[m],h);e.dist==="extreme"||e.dist==="weibull"||e.dist==="exponential"||e.dist==="rayleigh"?R=ay(e.lower[m],e.upper[m],h):e.dist==="logistic"||e.dist==="loglogistic"?R=Math.log(Math.tanh(A/4)):e.dist==="gaussian"||e.dist==="loggaussian"||e.dist==="lognormal"?R=Math.log(2*ae(A/2)-1):e.dist==="t"&&(R=Math.log(2*Wt(A/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)?ry(y,u):M)*g});return l}var py=["n","power","r","type","nfractional","alpha","sidedTest"],dy=["n","power","r","type","nfractional","alpha","sidedTest"],gy=["n","power","r","type","nfractional","alpha","sidedTest"],wy=["n","power","r","type","nfractional","alpha","sidedTest"],by=["n","power","r","type","nfractional","alpha","sidedTest"],vy=["n","power","r","type","nfractional","alpha","sidedTest"],Ny=["sigLevel","alternative","strict","nullOddsRatio","errbound","tsmethod"],yy=["alpha"],My=["alpha"],Ey=["alpha"],xy=["alpha"],Ry=["alpha"],Ay=["nmax"],Ur=.05,hn=e=>ie(e),Xn=e=>ae(e),Br=e=>Number.isFinite(e),qt=e=>Br(e)&&e>=0&&e<=1,xn=e=>Br(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 Hr(e){et(e,"alpha",qt,"'alpha' must be a probability in [0, 1]")}function Ty(e,t){return[e*t/(1+t),e/(1+t)]}function So(e,t,n,r,o,s,a={}){let{n:c,power:i}=a,l=a.r??1,f=a.alpha??Ur,m=a.nfractional??!1,u=e==="equivalence"?ke(a.type,["equivalence","equality"]):"equivalence",h=a.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",Br,"'treat' must be finite"),et(r,"control",Br,"'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",xn,"'sigma' must be positive and finite"),et(l,"r",xn,"'r' must be positive and finite"),Hr(f),(e!=="equivalence"||u!=="equality")&&et(s,"delta",C=>Br(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");on([c,i],"exactly one of 'n' and 'power' must be NULL"),Re(i)||et(i,"power",C=>qt(C)&&C>0&&C<1,"'power' must be strictly between 0 and 1"),c!=null&&et(c,"n",C=>Br(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=i??.8,y=v||e!=="equivalence"?ie(1-w,{lowerTail:!1}):ie((1-w)/2,{lowerTail:!1}),x,M,R,A,T;if(Re(c))b===0?(R=1/0,A=1/0):(A=(E+y)**2*(g/l+d)/(b*b),R=l*A),m?(x=R,M=A):(M=Math.ceil(A),x=Math.ceil(e==="equivalence"&&t==="continuous"||e==="superiority"?l*M:R)),T=i;else{[R,A]=Ty(c,l),x=m?R:Math.ceil(R),M=m?A:Math.ceil(A);let C=Math.sqrt(g/x+d/M);if(e==="equivalence"&&!v)T=Math.max(0,Xn((s-p)/C-hn(1-f))+Xn((s+p)/C-hn(1-f))-1);else if(v)T=Xn(Math.abs(p)/C-hn(1-f/2))+Xn(-Math.abs(p)/C-hn(1-f/2));else if(e==="noninferiority")T=Xn((p+s)/C-hn(1-f));else{let _=Math.abs(p-s)/C;T=Xn(_-E)+Xn(-_-E)}}return{nTotal:x+M,nTreat:x,nControl:M,nTotalUnrounded:R+A,nTreatUnrounded:R,nControlUnrounded:A,power:T,...N===void 0?{}:{delta:N},warnings:[]}}function Sy(e,t,n,r={}){return K(r,py,"epiSsequb"),J("epiSsequb",So("equivalence","binary",e,t,void 0,n??void 0,r))}function ky(e,t,n,r,o={}){K(o,dy,"epiSsequc");let s=So("equivalence","continuous",e,t,n,r??void 0,o);return J("epiSsequc",s)}function qy(e,t,n,r={}){return K(r,gy,"epiSsninfb"),J("epiSsninfb",So("noninferiority","binary",e,t,void 0,n,r))}function Iy(e,t,n,r,o={}){return K(o,wy,"epiSsninfc"),J("epiSsninfc",So("noninferiority","continuous",e,t,n,r,o))}function _y(e,t,n,r={}){return K(r,by,"epiSssupb"),J("epiSssupb",So("superiority","binary",e,t,void 0,n,r))}function Ly(e,t,n,r,o={}){return K(o,vy,"epiSssupc"),J("epiSssupc",So("superiority","continuous",e,t,n,r,o))}function To(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 To(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 Cy(e,t,n,r={}){K(r,Ny,"powerPaired2x2");let o=r.sigLevel??Ur,s=ke(r.alternative,["two.sided","one.sided"]),a=r.strict??!1,c=r.nullOddsRatio??1,i=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"),Hr(o),et(c,"nullOddsRatio",xn,"'nullOddsRatio' must be positive and finite"),et(i,"errbound",y=>Br(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"&&a&&l!=="blaker",g=s==="one.sided",d=To(n,f),v=0,N=n;if(i>0){let y=0;for(;v<=n&&(y+=d[v])<=i/2;)v++;for(y=0;N>=0&&(y+=d[N])<=i/2;)N--}let b=0;for(let y=v;y<=N;y++){let x=d[y];if(x===0)continue;let M=To(y,m),R=To(y,u),A=0,T=0,q=new Array(y+1),C=new Array(y+1);for(let _=0;_<=y;_++)A+=R[_],q[_]=A;for(let _=y;_>=0;_--)T+=R[_],C[_]=T;for(let _=0;_<=y;_++){let I=q[_],O=C[_],L,D=u===.5&&o===1;g?L=D||(h>0?O<=o:I<=o):p?L=D||I<=o/2||O<=o/2:L=h>0?D?_>Math.floor(y/2):O<=o/2:D?_<Math.ceil(y/2):I<=o/2,L&&(b+=x*M[_])}}let E=`errbound= ${i===0?"0":i>=.001?String(i):`${i.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 Oy(e,t,n,r,o,s={}){K(s,My,"powerCT");let a=s.alpha??Ur;Hr(a);for(let[l,f]of[[e,"nE"],[t,"nC"]])et(l,f,xn,`'${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",xn,"'RR' must be positive and finite");let c=e/t,i=e*n+t*r;return Xn(Math.sqrt(c*i)*Math.abs(o-1)/(c*o+1)-hn(1-a/2))}function Fy(e,t,n,r,o,s={}){K(s,yy,"ssizeCT");let a=s.alpha??Ur;Hr(a),et(e,"power",f=>qt(f)&&f>0&&f<1,"'power' must be strictly between 0 and 1"),et(t,"k",xn,"'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",xn,"'RR' must be positive and finite");let c=hn(1-a/2)+hn(e),i=1/t*((t*o+1)/(o-1))**2*(c*c),l=t*n+r;return{nE:Math.ceil(i*t/l),nC:Math.ceil(i/l)}}function Dy(e,t,n,r,o,s={}){K(s,xy,"powerEpi");let a=s.alpha??Ur;Hr(a),et(e,"n",xn,"'n' must be positive and finite"),et(t,"theta",xn,"'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 Xn(Math.sqrt(c)-hn(1-a/2))}function Py(e,t,n,r,o,s={}){K(s,Ey,"ssizeEpi");let a=s.alpha??Ur;Hr(a),et(e,"power",qt,"'power' must be in [0, 1]"),et(t,"theta",xn,"'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((hn(1-a/2)+hn(e))**2/c)}function Vy(e,t,n,r,o={}){K(o,Ry,"numDEpi");let s=o.alpha??Ur;Hr(s),et(e,"power",c=>qt(c)&&c>0&&c<1,"'power' must be strictly between 0 and 1"),et(t,"theta",xn,"'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 a=Math.log(t)**2*n*(1-n)*(1-r);return Math.ceil((hn(1-s/2)+hn(e))**2/a)}function qp(e){let t=new Array(e.length),n=0,r=0;for(let o=0;o<e.length;o++){let s=e[o]-r,a=n+s;r=a-n-s,n=a,t[o]=n}return t}function $y(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,a=n+s;r=a-n-s,n=a,t[o]=n}return t}function Ip(e,t,n,r,o,s){let a=0,c=0;c=r.slice(0,e+1).reduce((i,l)=>i+l,0);for(let i=e+1;i<n.length;i++){let l=t-i,f=l<0?1:l+1>=o.length?0:o[l+1],m=l<0?0:s[Math.min(l,s.length-1)];a+=n[i]*f,c+=r[i]*m}return[a,c]}function jy(e,t,n,r,o={}){K(o,Ay,"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 a=Array.from({length:s+1},(E,w)=>To(w,e)),c=Array.from({length:s+1},(E,w)=>To(w,t)),i=a.map(qp),l=c.map(qp),f=a.map($y),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=a[y],R=c[y],A=l[x],T=i[y];for(let q=0;q<y;q++){let C=T[q],_=y+x*(1-C),I=q,O=E-1,L=-1;for(;I<=O;){let D=Math.floor((I+O)/2),[,S]=Ip(q,D,M,R,f[x],A);S<=r?(L=D,I=D+1):O=D-1}if(L>=0){let[D]=Ip(q,L,M,R,f[x],A);D>n&&(L=-1)}L>=0&&(w===void 0||_<w.enP0)&&(w={r1:q,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.
18
+ `,lo=(e,t)=>e.map(n=>t[0].map((r,o)=>{let s=0;for(let i=0;i<n.length;i++)s+=n[i]*t[i][o];return s})),Uo=e=>e[0].map((t,n)=>e.map(r=>r[n])),Go=(e,t=n=>n)=>e[0].map((n,r)=>St(e.map(o=>t(o[r])))),Wu=(e,t=n=>n)=>e.map(n=>St(n.map(t))),fo=(e,t=n=>n)=>St(e[0].flatMap((n,r)=>e.map(o=>t(o[r])))),ca=e=>e<0?-1:1;function gc(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 Yu(e,t){let n=gc(e),{d:r,u:o,v:s}=bs(n),i=r.map((a,l)=>l).filter(a=>r[a]>Math.sqrt(co)*r[0]),c=n.map((a,l)=>{let f=0;for(let m of i)f+=s[l][m]*(1/r[m])*o[l][m];return 1-1/f});return c.some(a=>a>1)&&mr(t,"In smc, smcs > 1 were set to 1.0"),c.some(a=>a<0)&&mr(t,"In smc, smcs < 0 were set to .0"),c.map(a=>a>1?1:a<0?0:a)}var mr=(e,t)=>{e.includes(t)||e.push(t)};function pc(e,t,n,r){let o=e.map((c,a)=>c.map((l,f)=>a===f?t[a]:l)),s=un(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 Pb=e=>lo(e,Uo(e));function Ku(e,t,n,r){let o=gc(e),s=o.length,i=Yu(o,r),c=g=>{let w=pc(o,g.map(E=>1-E),t,E=>E<co?100*co:E),N=Pb(w),v=[];for(let E=0;E<s;E++)for(let b=E+1;b<s;b++)v.push((o[b][E]-N[b][E])**2);return St(v)},a=g=>{let w=pc(o,g.map((N,v)=>o[v][v]-N),t,N=>Math.max(N,0));return g.map((N,v)=>{let E=0;for(let b=0;b<t;b++)E+=w[v][b]*w[v][b];return E+N-o[v][v]})},l=g=>{let w=un(o.map((T,S)=>T.map((k,L)=>S===L?1-g[S]:k)),{symmetric:!0}),N=w.values,v=w.vectors,E=N.map(T=>T>=co),b=N.map(T=>T<co?100*co:T),M=o.map((T,S)=>T.map((k,L)=>{if(S===L)return 0;let _=0;for(let I=0;I<t;I++)_+=b[I]*v[S][I]*v[L][I];return k-_})),x=lo(M,v),y=(T,S)=>{let k=0;for(let L=0;L<s;L++)k+=v[L][T]*x[L][S];return k},R=new Array(s).fill(0);for(let T=0;T<t;T++){let S=(k,L)=>{if(L===0)return;let _=L*y(T,k);for(let I=0;I<s;I++)R[I]+=_*v[I][T]*v[I][k]};E[T]&&S(T,1);for(let k=T+1;k<t;k++)S(k,2*(E[T]&&E[k]?1:(b[T]-b[k])/(N[T]-N[k])));for(let k=t;k<s;k++)S(k,2*b[T]/(N[T]-N[k]))}return R},f=.005,m={method:"L-BFGS-B",lower:f,upper:Math.max(At(i),1)},u=new Array(s).fill(.01),h=je(()=>Eo(i.map(g=>1-g),c,{gr:a,...m,control:{fnscale:1,parscale:u}}));h.par.some(g=>g===f)&&(h=je(()=>Eo(h.par,c,{gr:l,...m,control:{fnscale:1,parscale:u,factr:1e3}}))),h.convergence!==0&&mr(n,`minres did not converge (optim convergence code ${h.convergence}): examine the results carefully`);let p=h.par,d=pc(o,p.map((g,w)=>o[w][w]-g),t,g=>Math.max(g,0));if(d=dc(d,t),d=d.map(g=>g.map(w=>w===0?1e-15:w)),Wu(d,g=>g*g).some(g=>g>1)&&mr(n,"An ultra-Heywood case was detected. Examine the results carefully"),t>1){d=dc(ia(d).loadings,t);let g=Go(d,N=>N*N),w=g.map((N,v)=>v).sort((N,v)=>g[v]-g[N]||N-v);d=d.map(N=>w.map(v=>N[v]))}else d=dc(d,t);return d}function dc(e,t){let n=Go(e).map(ca);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 Vb(e){let t=e[0].length,n=e.map(o=>o.map(s=>s*s)),r=n.map(o=>o.map((s,i)=>{let c=0;for(let a=0;a<t;a++)a!==i&&(c+=o[a]);return c}));return{f:fo(n.map((o,s)=>o.map((i,c)=>i*r[s][c])))/4,Gq:e.map((o,s)=>o.map((i,c)=>i*r[s][c]))}}function $b(e){let t=e[0].length,n=l=>{let f=rt(l),m=lo(e,Uo(f)),{f:u,Gq:h}=Vb(m),p=Uo(lo(lo(Uo(m),h),f)).map(d=>d.map(g=>-g));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=Go(r.map((g,w)=>g.map((N,v)=>N*s.G[w][v]))),m=s.G.map((g,w)=>g.map((N,v)=>N-r[w][v]*f[v])),u=Math.sqrt(fo(m,g=>g*g));if(c.push(s.f),u<1e-5){a=!0;break}if(i!==void 0){let g=i,w=r.map((v,E)=>v.map((b,M)=>b-g.T[E][M])),N=m.map((v,E)=>v.map((b,M)=>b-g.Gp[E][M]));fo(N,v=>v*v)>0&&(o=fo(w,v=>v*v)/Math.abs(fo(w.map((v,E)=>v.map((b,M)=>b*N[E][M])))),o=Math.max(1e-10,Math.min(o,20)))}else o=2*o;let h=At(c.slice(-10)),p=s,d=r;for(let g=0;g<=10;g++){let w=r.map((v,E)=>v.map((b,M)=>b-o*m[E][M])),N=Go(w,v=>v*v).map(v=>1/Math.sqrt(v));if(d=w.map(v=>v.map((E,b)=>E*N[b])),p=n(d),h-p.f>.5*u*u*o)break;o=o/2}i={T:r,Gp:m},r=d,s=p}return{loadings:s.L,phi:lo(Uo(r),r),converged:a}}function zb(e,t={}){V(t,Fb,"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)aa(t.covmat),r=gc(t.covmat);else{if(e===null)throw new TypeError("neither 'x' nor 'covmat' supplied");r=Mo(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=Ku(r,n,s,i),a,l,f;if(n===1)a=c.map(q=>q.slice()),f=[1],mr(i,`Omega_h for 1 factor is not meaningful, just omega_t
19
+ `),mr(s,"Omega_h and Omega_asymptotic are not meaningful with one factor");else{let q=$b(c);if(q.converged||mr(s,"oblimin did not converge in 2000 iterations"),a=q.loadings,l=q.phi,n===2){let P=l[0][1],B=Math.sqrt(Math.abs(P));f=[B,ca(P)*B],mr(i,Db)}else f=Ku(l,1,s,i).map(P=>P[0])}let m=a.map(q=>{let P=0;for(let B=0;B<n;B++)P+=q[B]*f[B];return P}),u=Wu(c,q=>q*q),h=u.map((q,P)=>r[P][P]-q),p=a.map((q,P)=>q.map((B,J)=>{if(n===1)return 0;let ce=r[P][P],W=ce-(ce-B*B)-(B*f[J])**2;return W<0&&(W=0),ca(B)*Math.sqrt(W)})),d=m.map((q,P)=>q*q/u[P]),g=m.map((q,P)=>{let B=[q,...p[P]];return St(B.map(J=>J*J))**2/St(B.map(J=>J**4))}),w=m.map(ca);w.some(q=>q<0)&&(r=r.map((q,P)=>q.map((B,J)=>w[P]*B*w[J])));let N=m.map((q,P)=>w[P]*q),v=p.map((q,P)=>q.map(B=>w[P]*B)),E=N.map((q,P)=>[q,...v[P],u[P],h[P],d[P],g[P]]),b=fo(r),M=St(r.map((q,P)=>q[P])),x=St(N)**2,y=St(h),R=x/b,T=(b-y)/b,S=x/(b-y),k=(b-M)/b*o/(o-1),L=(b+St(Yu(r,i))-M)/b,_=Go(E.map(q=>q.slice(0,n+1)),q=>q*q),I=_[0]/St(_),F=v.map(q=>{let P=0;for(let B=1;B<n;B++)Math.abs(q[B])>Math.abs(q[P])&&(P=B);return P}),C=[],D=[];for(let q=0;q<n;q++){let P=F.map((W,te)=>te).filter(W=>F[W]===q),B=St(P.flatMap(W=>P.map(te=>r[te][W]))),J=St(P.map(W=>v[W][q])),ce=St(P.map(W=>N[W]));D.push(J*J),C.push([(J*J+ce*ce)/B,ce*ce/B,J*J/B])}let A=[[T,St(N)**2/b,St(D)/b],...C],O={omegaH:R,omegaLim:S,alpha:k,omegaTot:T,G6:L,ECV:I,sl:E,slNames:["g",...Array.from({length:n},(q,P)=>`F${P+1}*`),"h2","u2","p2","com"],orthog:c,oblique:a,gloading:f,key:w,omegaGroup:A,warnings:s,messages:i};return l!==void 0&&(O.phi=l),H("omega",O)}var Bu=["ML","stdErr"],jb=["ML","stdErr"],tn=.9999;function bc(e){let t=Ie(e),n=[],r=0;for(let o=0;o<e.length-1;o++)r+=e[o],n.push(oe(r/t));return n}var vc=e=>[...new Set(e)].sort((t,n)=>t-n),Uu=(e,t)=>e===1?`1 ${t} with zero marginal removed`:`${e} ${t}s with zero marginals removed`,Gu=e=>Array.isArray(e)||ArrayBuffer.isView(e),Hu=(e,t)=>e>0?ie(e,{lowerTail:!1})-ie(t,{lowerTail:!1}):ie(t)-ie(e);function Nc(e){let t=Re([e]).groups[0],n=st(t),r=t.map(s=>s-n),o=Math.sqrt(ot(r));return r.map(s=>s/o)}function Xu(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 la=e=>Array.from({length:e},()=>new Array(e).fill(0));function Ju(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 Kb(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:Xt(t),kk:-t*Xt(t)};if(t===1/0)return{...wc,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*ie(c),k:f*ie(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*ie(c)-n/o*l*Xt(c),hk:i,kk:-t*f*ie(a)-n/o*f*Xt(a)}}function Qu(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:la(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=>Kb(h,p,r))),u=[];for(let h=0;h<s;h++)for(let p=0;p<i;p++){let d=e[h][p];if(d===0)continue;let g=go([t[h],n[p]],[t[h+1],n[p+1]],r),w=new Array(c).fill(0),N=la(c);for(let[v,E,b]of[[h+1,p+1,1],[h,p+1,-1],[h+1,p,-1],[h,p,1]]){let M=m[v][E],x=l(v),y=f(E);w[0]+=b*M.r,N[0][0]+=b*M.rr,x>=0&&(w[x]+=b*M.h,N[0][x]+=b*M.rh,N[x][0]+=b*M.rh,N[x][x]+=b*M.hh),y>=0&&(w[y]+=b*M.k,N[0][y]+=b*M.rk,N[y][0]+=b*M.rk,N[y][y]+=b*M.kk),x>=0&&y>=0&&(N[x][y]+=b*M.hk,N[y][x]+=b*M.hk)}Ju(a,d,g,w,N),u.push(d*Math.log(g))}return a.value=Ie(u),a}function Wb(e,t,n,r,o){let s=o?n.length+1:1,i={value:0,gradient:new Array(s).fill(0),hessian:la(s)},c=(1-r)*(1+r),a=Math.sqrt(c),l=c*a,f=new Array(s),m=la(s),u=[],h=(p,d,g)=>{let w=n[p],N=(w-r*d)/a,v=Xt(N),E=(r*w-d)/l,b=1/a,M=w/l+3*r*(r*w-d)/(l*c),x=r/l;if(f[0]+=g*v*E,m[0][0]+=g*v*(M-N*E*E),o){let y=p+1;f[y]+=g*v*b;let R=g*v*(x-N*E*b);m[0][y]+=R,m[y][0]+=R,m[y][y]+=g*v*(-N*b*b)}};for(let p=0;p<e.length;p++){let d=e[p],g=t[p];f.fill(0);for(let v of m)v.fill(0);let w=g<n.length?(n[g]-r*d)/a:1/0,N=g>0?(n[g-1]-r*d)/a:-1/0;g<n.length&&h(g,d,1),g>0&&h(g-1,d,-1),Ju(i,1,Hu(N,w),f,m),u.push(Math.log(Hu(N,w)),Xt(d,{log:!0}))}return i.value=Ie(u),i}function Yb(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)||Ie(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 eh=(e,t)=>e>=t-1e-12*Math.abs(t);function Zu(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=Yb(o,a);if(!s&&Math.abs(r[0])===tn&&l[0]*r[0]>0){s=!0;continue}let f=Math.abs(r[0]+l[0])>tn?(Math.sign(l[0])*tn-r[0])/l[0]:1,m=!1;for(let u=0;u<60;u++){let h=r.map((d,g)=>d+f*l[g]);f*l[0]!==0&&Math.abs(h[0])>=tn*(1-1e-15)&&(h[0]=Math.sign(h[0])*tn);let p=e(h);if(eh(p.value,o.value)){let d=At(h.map((g,w)=>Math.abs(g-r[w])/Math.max(1,Math.abs(r[w]))));r=h,o=p,m=d>1e-12;break}f/=2}if(!m)break}return{theta:r,at:o,bounded:s}}function th(e,t){let n=Zu(e,[Math.max(-tn,Math.min(tn,t[0])),...t.slice(1)],!1);if(n.bounded||n.theta[0]===0)return n;let r=Zu(e,[Math.sign(n.theta[0])*tn,...n.theta.slice(1)],!0);return eh(r.at.value,n.at.value)?r:n}function nh(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 rh=e=>`inadmissible correlation set to ${e>0?"0.9999":"-0.9999"}`;function Bb(e,t,n){at(t)&&V(t,Bu,"polychor"),V(n,Bu,"polychor");let r,o;if(Gu(t)){let C=e,D=t;if(C.length!==D.length)throw new RangeError("'x' and 'y' must have the same length");let A=C.map((J,ce)=>!Number.isNaN(J)&&!Number.isNaN(D[ce])),O=Ot(C.filter((J,ce)=>A[ce])),q=Ot(D.filter((J,ce)=>A[ce])),P=new Map(vc(O).map((J,ce)=>[J,ce])),B=new Map(vc(q).map((J,ce)=>[J,ce]));r=Array.from({length:P.size},()=>new Array(B.size).fill(0)),O.forEach((J,ce)=>r[P.get(J)][B.get(q[ce])]++),o=n??{}}else{let C=e;if(!C.every(Gu))throw new TypeError("give a table (array of rows) or two vectors");let D=C[0]?.length??0;if(C.some(A=>A.length!==D))throw new TypeError("the table must have rows of equal length");if(r=C.map(A=>Array.from(A)),r.some(A=>A.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;Ie(r.map(C=>Ie(C)))<2**1e3||({groups:r,k:a}=Re(r));let l=r.map(C=>Ie(C)),f=l.filter(C=>C===0).length;f>0&&(c.push(Uu(f,"row")),r=r.filter((C,D)=>l[D]!==0));let m=(r[0]??[]).map((C,D)=>Ie(r.map(A=>A[D]))),u=m.filter(C=>C===0).length;u>0&&(c.push(Uu(u,"column")),r=r.map(C=>C.filter((D,A)=>m[A]!==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 H("polychor",{rho:NaN,warnings:c});let d=[-1/0,...bc(r.map(C=>Ie(C))),1/0],g=[-1/0,...bc(r[0].map((C,D)=>Ie(r.map(A=>A[D])))),1/0],N=rl(C=>{let D=Math.max(-tn,Math.min(tn,C)),A=[];for(let O=0;O<h;O++)for(let q=0;q<p;q++){if(r[O][q]===0)continue;let P=go([d[O],g[q]],[d[O+1],g[q+1]],D);A.push(r[O][q]*Math.log(P))}return-Ie(A)},-tn,tn).optimum;if(!s&&!i)return H("polychor",{rho:N,warnings:c});let v=C=>[-1/0,...C,1/0],E=d.slice(1,h),b=g.slice(1,p),M=C=>s?Qu(r,v(C.slice(1,h)),v(C.slice(h)),C[0],!0):Qu(r,d,g,C[0],!1),{theta:x,at:y,bounded:R}=th(M,s?[N,...E,...b]:[N]),T=x[0];if(R&&c.push(rh(T)),!i)return H("polychor",{rho:T,warnings:c});let S=s?v(x.slice(1,h)):d,k=s?v(x.slice(h)):g,L=Ie(r.map(C=>Ie(C))),_=[],I=[];for(let C=0;C<h;C++)for(let D=0;D<p;D++){let A=r[C][D],O=go([S[C],k[D]],[S[C+1],k[D+1]],T);if(A===0)_.push(L*O);else if(a===0)_.push(A*Math.log1p(1e-6/A),A*Xu(L*O/A-1));else{let q=$(A,a);I.push(q*Math.log1p(1e-6/q)),_.push(A*Xu(L*O/A-1))}}let F=nh(y.hessian,R);return H("polychor",{rho:T,warnings:c,var:a===0?F:F.map(C=>C.map(D=>$(D,-a))),chisq:a===0?2*Ie(_):$(2*Ie(_),a)+2*Ie(I),df:h*p-h-p,n:$(L,a),rowCuts:S.slice(1,h),colCuts:k.slice(1,p)})}function Ub(e,t,n,r){let o=e.length,s=[oe(.25),oe(.5),oe(.75)],i=(h,p)=>p.filter(d=>d<h).length,c=Nc(e).map(h=>i(h,n)),a=Nc(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((d,g)=>{if(d===0)return;let w=go([f[p],m[g]],[f[p+1],m[g+1]],r);u.push(d*Math.log((d+1e-6)/(w*o)))})),2*Ie(u)}function Gb(e,t,n={}){if(V(n,jb,"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((y,R)=>!Number.isNaN(y)&&!Number.isNaN(t[R])),i=e.filter((y,R)=>s[R]),c=Ot(t.filter((y,R)=>s[R])),a=i.length,l=vc(c);if(l.length<2)return H("polyserial",{rho:NaN,warnings:["y has fewer than 2 levels"]});let f=new Map(l.map((y,R)=>[y,R+1])),m=c.map(y=>f.get(y)),u=l.map(()=>0);for(let y of m)u[y-1]++;let h=bc(u),p=[],d=Math.sqrt((a-1)/a)*Math.sqrt(ot(m))*ol(i,m)[0]/Ie(h.map(y=>Xt(y)));if(Math.abs(d)>tn){let y=Math.sign(d)*tn;p.push(`initial correlation inadmissible, ${Pn(d)}, set to ${Pn(y)}`),d=y}if(!r&&!o)return H("polyserial",{rho:d,warnings:p});let g=Nc(i),w=m.map(y=>y-1),N=y=>Wb(g,w,r?y.slice(1):h,y[0],r),{theta:v,at:E,bounded:b}=th(N,r?[d,...h]:[d]);if(b&&p.push(rh(v[0])),!o)return H("polyserial",{rho:v[0],warnings:p});let M=r?v.slice(1):h,x=l.length;return H("polyserial",{rho:v[0],warnings:p,var:nh(E.hessian,b),chisq:Ub(m,g,M,r?v[0]:d),df:4*x-x-1,n:a,cuts:M})}var Hb=["group","strata","rho","cencode"],Xb=new Set(["cov2","tf","failcode","cencode","gtol","maxiter","init","variance","confLevel","names"]);function fa(e){return e==null||typeof e=="number"&&Number.isNaN(e)}function yc(e){let t=en(e),n=Array.from(new Set(t)).sort(Vn);return{labels:n,index:new Map(n.map((r,o)=>[r,o]))}}function Qb(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 Zb(e,t,n={}){V(n,Hb,"cuminc"),Qb(e,t,n);let{group:r,strata:o,rho:s=0,cencode:i=0}=n,c=[],a=new mt;for(let b=0;b<e.length;b++)if(!(Number.isNaN(e[b])||fa(t[b])||r!==void 0&&fa(r[b])||o!==void 0&&fa(o[b]))){if(!Number.isFinite(e[b]))throw new RangeError("'time' must contain only finite values");if(e[b]<0)throw new RangeError("'time' must be non-negative");c.push(b)}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]:en(c.map(b=>r[b])),m=o===void 0?[1]:en(c.map(b=>o[b])),u=en([...c.map(b=>t[b]),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),d=yc(u),g=u[u.length-1],w=d.labels.filter(b=>b!==g);if(w.length===0)throw new Error("there are no events of any cause");let N=c.map((b,M)=>({time:e[b],status:u[M],group:r===void 0?0:h.index.get(f[M]),strata:o===void 0?0:p.index.get(m[M]),source:b})).sort((b,M)=>b.time-M.time),v=[];for(let b of w){let M=Rt(b);for(let x=0;x<h.labels.length;x++){let y=N.filter(A=>A.group===x),R=y.reduce((A,O)=>Math.max(A,O.time),-1/0),T=R>0&&R<1?2**Math.min(1023,Math.ceil(-Math.log2(R))):1,S=ar(y.map(A=>A.time*T),y.map(A=>A.status===g?0:1)),k=[0],L=[0],_=[0],I=[],F=1;for(let A=0;A<S.time.length;A++){let O=S.nEvent[A];if(O===0)continue;let q=S.time[A]/T,P=S.nRisk[A],B=y.filter(J=>J.time===q&&J.status===b).length;I.push({t:q,Y:P,d:O,dk:B,sb:F,sa:S.surv[A]}),F=S.surv[A]}let C=0,D=0;for(let A=0;A<I.length;A++){let O=I[A],q=C,P=D;C+=O.sb*O.dk/O.Y;let B=0;for(let J=0;J<=A;J++){let ce=I[J],W=ce.Y,te=ce.d,U=ce.dk,ve=W===te?0:(te*(W-te)+2*U*(te-U))/((W-1)*(W-te)**2),X=W===1?U:U*(W-U)/(W*W*(W-1)),K=W===te||W===1?0:U*(W-U)/(W*(W-te)*(W-1)),Z=0;for(let G=0;G<=J;G++){let j=I[G];Z+=j.sb*j.dk/j.Y}let z=C-Z;B+=z*z*ve+ce.sb*ce.sb*X-2*z*ce.sb*K}D=Math.max(0,B),O.dk>0&&(k.push(O.t,O.t),L.push(q,C),_.push(P,D))}k.push(R),L.push(C),_.push(D),v.push({name:`${Rt(h.labels[x])} ${M}`,group:Rt(h.labels[x]),cause:M,time:k,est:L,var:_})}}let E=h.labels.length>=2?Jb(N,h.labels,w,p.labels.length,s,g):void 0;return H("cuminc",{curves:v,...E===void 0?{}:{tests:E},warnings:a.list})}function Jb(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 d=0;d<r;d++){let g=e.filter(b=>b.strata===d),w=Array.from(new Set(g.filter(b=>b.status!==s).map(b=>b.time))).sort((b,M)=>b-M),N=new Array(a).fill(1),v=new Array(a).fill(0),E=[];for(let b of w){let M=new Array(a).fill(0),x=new Array(a).fill(0),y=new Array(a).fill(0);for(let A of g)A.time>=b&&M[A.group]++;for(let A of g)A.time===b&&A.status!==s&&(A.status===c?x[A.group]++:y[A.group]++);let R=N.slice(),T=v.slice(),S=M.map((A,O)=>A===0?0:A/R[O]),k=M.map((A,O)=>A===0?0:A*(1-T[O])/R[O]),L=x.reduce((A,O)=>A+O,0),_=S.reduce((A,O)=>A+O,0),I=_===0?0:L/_,F=E.length===0?1:E[E.length-1].Gafter,C=F-I,D=k.reduce((A,O)=>A+O,0);for(let A=0;A<a;A++){let O=F**o;D>0&&(l[A]+=O*(x[A]-k[A]*L/D)),M[A]>0?(N[A]=N[A]*(1-(x[A]+y[A])/M[A]),v[A]+=R[A]*x[A]/M[A]):N[A]=0}E.push({time:b,Y:M,d1:x,d2:y,Sbefore:R,Safter:N.slice(),Fbefore:T,h:S,R:k,dF0:I,Gbefore:F,Gafter:C})}for(let b=0;b<a;b++)for(let M=0;M<a;M++){let x=0;for(let y=0;y<a;y++)for(let R=0;R<E.length;R++){let T=E[R],S=T.h.reduce((te,U)=>te+U,0);if(!(S>0)||!(T.h[y]>0))continue;let k=T.Gbefore**o,L=k*T.h[b]*((b===y?1:0)-T.h[y]/S),_=k*T.h[M]*((M===y?1:0)-T.h[y]/S),I=0,F=0;for(let te=R+1;te<E.length;te++){let U=E[te],ve=U.h.reduce((G,j)=>G+j,0);if(!(ve>0))continue;let X=U.Gbefore**o,K=X*U.h[b]*((b===y?1:0)-U.h[y]/ve),Z=X*U.h[M]*((M===y?1:0)-U.h[y]/ve),z=U.Gbefore===0?0:U.dF0/U.Gbefore;I+=K*z,F+=Z*z}let C=T.Safter[y]===0?0:T.Gafter/T.Safter[y],D=L+(1-C)*I,A=_+(1-C)*F,O=-C*I,q=-C*F,P=T.Y[y]===0?0:T.Sbefore[y]*T.d2[y]/T.Y[y],B=(D*A*T.dF0+O*q*P)/T.h[y],J=T.d1.reduce((te,U)=>te+U,0),ce=0;if(J>1&&T.Y[y]>0&&T.Sbefore[y]>0){let te=1/T.Sbefore[y];S>te&&(ce=J*(J-1)/S*(T.Y[y]*te*te/(S-te))*(D*A/(T.h[y]*T.h[y])))}let W=0;T.d2[y]>1&&T.Y[y]>1&&(W=(T.d2[y]-1)/(T.Y[y]-1)*(P/T.h[y])*O*q),x+=B-ce-W}f[b][M]+=x}}let m=a-1,u=NaN,h=NaN,p=f.slice(0,m).map(d=>d.slice(0,m));try{let d=rt(p,l.slice(0,m),{tol:1e-12});u=l.slice(0,m).reduce((g,w,N)=>g+w*d[N],0),h=Mt(u,m,{lowerTail:!1})}catch{u=NaN,h=NaN}i.push({cause:Rt(c),stat:u,df:m,pValue:h})}return i}function ev(e,t){let n=Array.from(new Set(t.filter(s=>!Number.isNaN(s)))).sort((s,i)=>s-i),r=[],o=[];for(let s of e.curves){let i=[],c=[];for(let a of n){let l=-1;for(let f=0;f<s.time.length;f++)s.time[f]<=a&&(l=f);a<0||l<0||a>s.time[s.time.length-1]?(i.push(NaN),c.push(NaN)):(i.push(s.est[l]),c.push(s.var[l]))}r.push(i),o.push(c)}return{curveNames:e.curves.map(s=>s.name),times:n,est:r,var:o}}function In(e,t=e){return Array.from({length:e},()=>new Array(t).fill(0))}function tv(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 Mc(e,t){return Ie(e.map((n,r)=>n*t[r]))}function nv(e){return e.every(t=>t.every(Number.isFinite))}function rv(e,t,n,r={}){V(r,Xb,"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,d=o?.[0]?.length??0,g=p+d;if(g===0)throw new RangeError("at least one covariate is required");if(n!==void 0&&n.some(j=>j.length!==p))throw new RangeError("rows of 'cov1' must have equal length");if(o!==void 0&&o.some(j=>j.length!==d))throw new RangeError("rows of 'cov2' must have equal length");if(f!==void 0&&f.length!==g)throw new RangeError("'init' must have one value per coefficient");if(h!==void 0&&h.length!==g)throw new RangeError("'names' must have one name per coefficient");let w=[];for(let j=0;j<e.length;j++)if(!(Number.isNaN(e[j])||fa(t[j])||n!==void 0&&n[j].some(Number.isNaN)||o!==void 0&&o[j].some(Number.isNaN))){if(!Number.isFinite(e[j]))throw new RangeError("'time' must contain only finite values");if(n!==void 0&&!n[j].every(Number.isFinite)||o!==void 0&&!o[j].every(Number.isFinite))throw new RangeError("covariates must be finite");w.push(j)}let N=e.length-w.length;if(w.length===0)throw new RangeError("no complete observations");let v=w.map(j=>e[j]),E=en([...w.map(j=>t[j]),c,i]),b=E.slice(0,w.length),M=E[E.length-2],x=E[E.length-1],y=w.map(j=>n===void 0?[]:Array.from(n[j])),R=w.map(j=>o===void 0?[]:Array.from(o[j])),T=Array.from(new Set(v.filter((j,Q)=>b[Q]===x))).sort((j,Q)=>j-Q);if(T.length===0)throw new RangeError("no observations of 'failcode'");let S=d===0?[[0]]:s(T).map(j=>Array.from(j));if(d>0&&(S.length!==T.length||S.some(j=>j.length!==d)||!nv(S)))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((j,Q)=>v.map((ne,be)=>[...y[be],...R[be].map((Ne,me)=>Ne*(S[Q][me]??0))])),L=v.reduce((j,Q)=>Math.max(j,Math.abs(Q)),0),_=L>0&&L<1?2**Math.min(1023,Math.ceil(-Math.log2(L))):1,I=ar(v.map(j=>j*_),b.map(j=>j===M?1:0)),F=j=>{let Q=j*_,ne=1;for(let be=0;be<I.time.length&&!(I.time[be]>=Q);be++)ne=I.surv[be];return ne},C=T.map(j=>v.map((Q,ne)=>{if(Q>=j)return 1;if(b[ne]===M||b[ne]===x)return 0;let be=F(Q);return be===0?0:F(j)/be}));for(let j=0;j<T.length;j++)for(let Q=0;Q<v.length;Q++)v[Q]===T[j]&&b[Q]===x&&(C[j][Q]=1);let D=j=>{let Q=Array.from({length:g},()=>[]),ne=Array.from({length:g},()=>Array.from({length:g},()=>[])),be=[],Ne=In(T.length,g),me=[],le=[],Ee=[];for(let Se=0;Se<T.length;Se++){let Ke=T[Se],Pe=v.map((pe,Ae)=>pe===Ke&&b[Ae]===x?Ae:-1).filter(pe=>pe>=0),Ze=Pe.length,Be=k[Se],Qe=Be.map(pe=>Mc(j,pe)),fe=C[Se].slice();for(let pe of Pe)fe[pe]=1;let ee=Math.max(...Qe.map((pe,Ae)=>fe[Ae]>0?pe:-1/0));if(ee===-1/0)throw new Error("empty Fine-Gray risk set");let ge=ee<500&&ee>-500,de=ge?Qe.map((pe,Ae)=>fe[Ae]*Math.exp(pe)):Qe.map((pe,Ae)=>fe[Ae]*Math.exp(pe-ee)),ae=Ie(de),Je=Array.from({length:g},(pe,Ae)=>Ie(de.map((We,Fe)=>We*Be[Fe][Ae])));if(!(ae>0))throw new Error("empty Fine-Gray risk set");let Oe=Je.map(pe=>pe/ae),ke=Array.from({length:g},(pe,Ae)=>{let We=Be[0][Ae],Fe=0,nt=0;for(let ut of Be)Fe=Math.max(Fe,Math.abs(ut[Ae]-We)),nt=Math.max(nt,Math.abs(ut[Ae]));return Fe>0&&nt/Fe>1e6}).some(Boolean),$e=ke?Array.from({length:g},(pe,Ae)=>Be.map(We=>We[Ae]-Be[0][Ae])):[],He=ke?$e.map(pe=>Ie(pe.map((Ae,We)=>de[We]*Ae))/ae):[],se=ke?void 0:Array.from({length:g},(pe,Ae)=>Array.from({length:g},(We,Fe)=>Ie(de.map((nt,ut)=>nt*Be[ut][Ae]*Be[ut][Fe]))));le.push(Oe),Ee.push(de.map(pe=>pe/ae));let Te=ge?Ze/ae:Ze*Math.exp(-ee)/ae;me.push(Te),be.push(Ie(Pe.map(pe=>Mc(j,Be[pe])))-Ze*(ge?Math.log(ae):ee+Math.log(ae)));for(let pe=0;pe<g;pe++){let We=Ie(Pe.map(Fe=>Be[Fe][pe]))-Ze*Oe[pe];Q[pe].push(We),Ne[Se][pe]=We}for(let pe=0;pe<g;pe++)for(let Ae=0;Ae<g;Ae++){let We=ke?Ie(de.map((Fe,nt)=>Fe*($e[pe][nt]-He[pe])*($e[Ae][nt]-He[Ae])))/ae:se[pe][Ae]/ae-Oe[pe]*Oe[Ae];ne[pe][Ae].push(Ze*We)}}let he=In(v.length,g);for(let Se=0;Se<T.length;Se++){let Ke=k[Se],Pe=Ee[Se],Ze=le[Se],Be=T[Se];for(let Qe=0;Qe<v.length;Qe++){let fe=v[Qe]===Be&&b[Qe]===x?1:0,ee=v.filter((de,ae)=>de===Be&&b[ae]===x).length,ge=fe-Pe[Qe]*ee;for(let de=0;de<g;de++)he[Qe][de]+=(Ke[Qe][de]-Ze[de])*ge}}let Le=Q.map(Ie),we=ne.map(Se=>Se.map(Ie));return{loglik:Ie(be),score:Le,inf:we,bfitj:me,res:Ne,eta:he,means:le,riskFactors:Ee,zByTime:k}},A=f===void 0?new Array(g).fill(0):Array.from(f);if(!A.every(Number.isFinite))throw new RangeError("'init' must contain finite values");let O=A.slice(),q=D(O),P=!1,B=0,J=(j,Q)=>Math.max(...j.score.map((ne,be)=>Math.abs(ne)*Math.max(Math.abs(Q[be]),1)))<a*Math.max(Math.abs(j.loglik),1);for(;;){if(J(q,O)){P=!0;break}if(B>=l)break;let j=rt(q.inf,q.score,{tol:0});O=O.map((Q,ne)=>Q+j[ne]),B++,q=D(O)}let ce=rt(q.inf),W=In(g);if(m){let j=In(g),Q=v.length,ne=Array.from(new Set(v)).sort((le,Ee)=>le-Ee),be=ne.map(le=>{let Ee=v.filter(Le=>Le>=le).length,he=v.filter((Le,we)=>Le===le&&b[we]===M).length;return Ee===0?0:he/Ee}),Ne=In(Q,g);for(let le=0;le<ne.length;le++){let Ee=ne[le],he=new Array(g).fill(0);for(let we=0;we<Q;we++)if(!(!(v[we]<Ee)||b[we]===M||b[we]===x))for(let Me=0;Me<T.length;Me++){if(T[Me]<Ee)continue;let Se=q.riskFactors[Me][we],Ke=v.filter((Be,Qe)=>Be===T[Me]&&b[Qe]===x).length,Pe=q.zByTime[Me][we],Ze=q.means[Me];for(let Be=0;Be<g;Be++)he[Be]+=(Pe[Be]-Ze[Be])*Se*Ke}let Le=v.filter(we=>we>=Ee).length;if(Le!==0)for(let we=0;we<Q;we++){let Me=v[we]===Ee&&b[we]===M?1:0,Se=v[we]>=Ee?be[le]:0;for(let Ke=0;Ke<g;Ke++)Ne[we][Ke]+=he[Ke]/Le*(Me-Se)}}for(let le=0;le<Q;le++)tv(j,q.eta[le].map((Ee,he)=>Ee+Ne[le][he]),q.eta[le].map((Ee,he)=>Ee+Ne[le][he]),1);W=In(g);let me=oh(ce,j);W=oh(me,ce)}else W=In(g).map(j=>j.map(()=>NaN));let te=m?W.map((j,Q)=>Math.sqrt(j[Q])):void 0,U=te===void 0?void 0:O.map((j,Q)=>j/te[Q]),ve=U===void 0?void 0:U.map(j=>2*ie(-Math.abs(j))),X=m?oe((1+u)/2):void 0,K=X===void 0?void 0:O.map((j,Q)=>Math.exp(j-X*te[Q])),Z=X===void 0?void 0:O.map((j,Q)=>Math.exp(j+X*te[Q])),z=m?O.map(Math.exp):void 0,G=h===void 0?[...Array.from({length:p},(j,Q)=>`cov1${Q+1}`),...Array.from({length:d},(j,Q)=>`cov2${Q+1}*tf${Q+1}`)]:Array.from(h);return H("crr",{coefficients:O,names:G,var:W,...te===void 0?{}:{se:te,z:U,pValue:ve,lower:K,upper:Z,expCoef:z},loglik:q.loglik,loglikNull:D(new Array(g).fill(0)).loglik,logtest:{statistic:2*(q.loglik-D(new Array(g).fill(0)).loglik),df:g},score:q.score,inf:m?q.inf:In(g).map(j=>j.map(()=>NaN)),invinf:m?ce:In(g).map(j=>j.map(()=>NaN)),converged:P,iterations:B,n:w.length,nMissing:N,uftime:T,bfitj:q.bfitj,res:m?q.res:null,tfs:S,warnings:[],covariateDimensions:[p,d]})}function oh(e,t){let n=In(e.length,t[0]?.length??0);for(let r=0;r<e.length;r++)for(let o=0;o<(t[0]?.length??0);o++)for(let s=0;s<t.length;s++)n[r][o]+=e[r][s]*t[s][o];return n}function ov(e,t,n){let[r,o]=e.covariateDimensions;if(r>0&&t===void 0)throw new TypeError("'cov1' is required by this fit");if(o>0&&n===void 0)throw new TypeError("'cov2' is required by this fit");if(r>0&&t.some(a=>a.length!==r))throw new RangeError("'cov1' has the wrong number of columns");if(o>0&&n.some(a=>a.length!==o))throw new RangeError("'cov2' has the wrong number of columns");if(r>0&&o>0&&t.length!==n.length)throw new RangeError("'cov1' and 'cov2' must have the same number of rows");let s=r>0?t.length:n.length,i=o===0?e.uftime.map(()=>[0]):e.tfs;return e.uftime.map((a,l)=>{let f=[a];for(let m=0;m<s;m++){let u=0;for(let h=0;h<=l;h++){let p=[...r===0?[]:t[m],...o===0?[]:n[m].map((v,E)=>v*i[h][E])],d=e.bfitj[h],g=Mc(e.coefficients,p),w=d*Math.exp(g),N=Number.isFinite(w)&&(w!==0||d===0)?w:d===0?0:Math.exp(Math.log(d)+g);u+=N}f.push(-Math.expm1(-u))}return f})}var sv=["n","power","r","type","nfractional","alpha","sidedTest"],av=["n","power","r","type","nfractional","alpha","sidedTest"],iv=["n","power","r","type","nfractional","alpha","sidedTest"],cv=["n","power","r","type","nfractional","alpha","sidedTest"],lv=["n","power","r","type","nfractional","alpha","sidedTest"],fv=["n","power","r","type","nfractional","alpha","sidedTest"],mv=["sigLevel","alternative","strict","nullOddsRatio","errbound","tsmethod"],uv=["alpha"],hv=["alpha"],pv=["alpha"],dv=["alpha"],gv=["alpha"],wv=["nmax"],Cr=.05,cn=e=>oe(e),Kn=e=>ie(e),Lr=e=>Number.isFinite(e),kt=e=>Lr(e)&&e>=0&&e<=1,Nn=e=>Lr(e)&&e>0;function Xe(e,t,n,r){if(typeof e!="number")throw new TypeError(`'${t}' must be numeric`);if(!n(e))throw new RangeError(r)}function Or(e){Xe(e,"alpha",kt,"'alpha' must be a probability in [0, 1]")}function bv(e,t){return[e*t/(1+t),e/(1+t)]}function uo(e,t,n,r,o,s,i={}){let{n:c,power:a}=i,l=i.r??1,f=i.alpha??Cr,m=i.nfractional??!1,u=e==="equivalence"?xe(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(Xe(n,"treat",Lr,"'treat' must be finite"),Xe(r,"control",Lr,"'control' must be finite"),t==="binary"?(Xe(n,"treat",kt,"'treat' must be a probability in [0, 1]"),Xe(r,"control",kt,"'control' must be a probability in [0, 1]")):Xe(o,"sigma",Nn,"'sigma' must be positive and finite"),Xe(l,"r",Nn,"'r' must be positive and finite"),Or(f),(e!=="equivalence"||u!=="equality")&&Xe(s,"delta",L=>Lr(L)&&L>=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");bn([c,a],"exactly one of 'n' and 'power' must be NULL"),qe(a)||Xe(a,"power",L=>kt(L)&&L>0&&L<1,"'power' must be strictly between 0 and 1"),c!=null&&Xe(c,"n",L=>Lr(L)&&L>0,"'n' must be positive and finite");let p=n-r,d=t==="binary"?n*(1-n):o*o,g=t==="binary"?r*(1-r):o*o,w=e==="equivalence"&&u==="equality",N=w?void 0:s,v=e==="noninferiority"?p+N:e==="superiority"?p-N:w?p:N-Math.abs(p);if(e==="noninferiority"&&v<=0)throw new RangeError("Target power is not reachable under the specified hypotheses");let E=e==="superiority"?oe(f/h,{lowerTail:!1}):w?oe(f/2,{lowerTail:!1}):oe(f,{lowerTail:!1}),b=a??.8,M=w||e!=="equivalence"?oe(1-b,{lowerTail:!1}):oe((1-b)/2,{lowerTail:!1}),x,y,R,T,S;if(qe(c))v===0?(R=1/0,T=1/0):(T=(E+M)**2*(d/l+g)/(v*v),R=l*T),m?(x=R,y=T):(y=Math.ceil(T),x=Math.ceil(e==="equivalence"&&t==="continuous"||e==="superiority"?l*y:R)),S=a;else{[R,T]=bv(c,l),x=m?R:Math.ceil(R),y=m?T:Math.ceil(T);let L=Math.sqrt(d/x+g/y);if(e==="equivalence"&&!w)S=Math.max(0,Kn((s-p)/L-cn(1-f))+Kn((s+p)/L-cn(1-f))-1);else if(w)S=Kn(Math.abs(p)/L-cn(1-f/2))+Kn(-Math.abs(p)/L-cn(1-f/2));else if(e==="noninferiority")S=Kn((p+s)/L-cn(1-f));else{let _=Math.abs(p-s)/L;S=Kn(_-E)+Kn(-_-E)}}return{nTotal:x+y,nTreat:x,nControl:y,nTotalUnrounded:R+T,nTreatUnrounded:R,nControlUnrounded:T,power:S,...N===void 0?{}:{delta:N},warnings:[]}}function vv(e,t,n,r={}){return V(r,sv,"epiSsequb"),H("epiSsequb",uo("equivalence","binary",e,t,void 0,n??void 0,r))}function Nv(e,t,n,r,o={}){V(o,av,"epiSsequc");let s=uo("equivalence","continuous",e,t,n,r??void 0,o);return H("epiSsequc",s)}function yv(e,t,n,r={}){return V(r,iv,"epiSsninfb"),H("epiSsninfb",uo("noninferiority","binary",e,t,void 0,n,r))}function Mv(e,t,n,r,o={}){return V(o,cv,"epiSsninfc"),H("epiSsninfc",uo("noninferiority","continuous",e,t,n,r,o))}function Ev(e,t,n,r={}){return V(r,lv,"epiSssupb"),H("epiSssupb",uo("superiority","binary",e,t,void 0,n,r))}function xv(e,t,n,r,o={}){return V(o,fv,"epiSssupc"),H("epiSssupc",uo("superiority","continuous",e,t,n,r,o))}function mo(e,t){let n=new Array(e+1).fill(0);if(t===0)return n[0]=1,n;if(t===1)return n[e]=1,n;if(t>.5)return mo(e,1-t).reverse();let r=Math.floor((e+1)*t);n[r]=Math.exp(e*Math.log1p(-t)+r*Math.log(t/(1-t))+Array.from({length:r},(o,s)=>Math.log(e-s)-Math.log(s+1)).reduce((o,s)=>o+s,0));for(let o=r;o>0;o--)n[o-1]=n[o]*(o/(e-o+1))*((1-t)/t);for(let o=r;o<e;o++)n[o+1]=n[o]*((e-o)/(o+1))*(t/(1-t));return n}function Rv(e,t,n,r={}){V(r,mv,"powerPaired2x2");let o=r.sigLevel??Cr,s=xe(r.alternative,["two.sided","one.sided"]),i=r.strict??!1,c=r.nullOddsRatio??1,a=r.errbound??1e-6,l=xe(r.tsmethod,["central","minlike","blaker"]);if(Xe(e,"pb",kt,"'pb' must be in [0, 1]"),Xe(t,"pc",kt,"'pc' must be in [0, 1]"),Xe(n,"npairs",M=>Number.isSafeInteger(M)&&M>=0&&M<=1e5,"'npairs' must be a nonnegative safe integer"),Or(o),Xe(c,"nullOddsRatio",Nn,"'nullOddsRatio' must be positive and finite"),Xe(a,"errbound",M=>Lr(M)&&M>=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",d=s==="one.sided",g=mo(n,f),w=0,N=n;if(a>0){let M=0;for(;w<=n&&(M+=g[w])<=a/2;)w++;for(M=0;N>=0&&(M+=g[N])<=a/2;)N--}let v=0;for(let M=w;M<=N;M++){let x=g[M];if(x===0)continue;let y=mo(M,m),R=mo(M,u),T=0,S=0,k=new Array(M+1),L=new Array(M+1);for(let _=0;_<=M;_++)T+=R[_],k[_]=T;for(let _=M;_>=0;_--)S+=R[_],L[_]=S;for(let _=0;_<=M;_++){let I=k[_],F=L[_],C,D=u===.5&&o===1;d?C=D||(h>0?F<=o:I<=o):p?C=D||I<=o/2||F<=o/2:C=h>0?D?_>Math.floor(M/2):F<=o/2:D?_<Math.ceil(M/2):I<=o/2,C&&(v+=x*y[_])}}let E=`errbound= ${a===0?"0":a>=.001?String(a):`${a.toExponential(0).replace(/e([+-]?)(\\d+)$/,(M,x,y)=>`e${x||"+"}${y.padStart(2,"0")}`)}`}${e+t===1?" no non-informative pairs: pa+pd=0":""}`,b="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 H("powerPaired2x2",{power:v,npairs:n,pb:e,pc:t,sigLevel:o,alternative:s,nullOddsRatio:c,note:E,method:b,warnings:[]})}function Tv(e,t,n,r,o,s={}){V(s,hv,"powerCT");let i=s.alpha??Cr;Or(i);for(let[l,f]of[[e,"nE"],[t,"nC"]])Xe(l,f,Nn,`'${f}' must be positive and finite`);for(let[l,f]of[[n,"pE"],[r,"pC"]])Xe(l,f,kt,`'${f}' must be in [0, 1]`);Xe(o,"RR",Nn,"'RR' must be positive and finite");let c=e/t,a=e*n+t*r;return Kn(Math.sqrt(c*a)*Math.abs(o-1)/(c*o+1)-cn(1-i/2))}function Sv(e,t,n,r,o,s={}){V(s,uv,"ssizeCT");let i=s.alpha??Cr;Or(i),Xe(e,"power",f=>kt(f)&&f>0&&f<1,"'power' must be strictly between 0 and 1"),Xe(t,"k",Nn,"'k' must be positive and finite"),Xe(n,"pE",kt,"'pE' must be in [0, 1]"),Xe(r,"pC",kt,"'pC' must be in [0, 1]"),Xe(o,"RR",Nn,"'RR' must be positive and finite");let c=cn(1-i/2)+cn(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 Av(e,t,n,r,o,s={}){V(s,dv,"powerEpi");let i=s.alpha??Cr;Or(i),Xe(e,"n",Nn,"'n' must be positive and finite"),Xe(t,"theta",Nn,"'theta' must be positive and finite"),Xe(n,"p",kt,"'p' must be in [0, 1]"),Xe(r,"psi",kt,"'psi' must be in [0, 1]"),Xe(o,"rho2",kt,"'rho2' must be in [0, 1]");let c=e*Math.log(t)**2*n*(1-n)*r*(1-o);return Kn(Math.sqrt(c)-cn(1-i/2))}function kv(e,t,n,r,o,s={}){V(s,pv,"ssizeEpi");let i=s.alpha??Cr;Or(i),Xe(e,"power",kt,"'power' must be in [0, 1]"),Xe(t,"theta",Nn,"'theta' must be positive and finite"),Xe(n,"p",kt,"'p' must be in [0, 1]"),Xe(r,"psi",kt,"'psi' must be in [0, 1]"),Xe(o,"rho2",kt,"'rho2' must be in [0, 1]");let c=Math.log(t)**2*n*(1-n)*r*(1-o);return Math.ceil((cn(1-i/2)+cn(e))**2/c)}function qv(e,t,n,r,o={}){V(o,gv,"numDEpi");let s=o.alpha??Cr;Or(s),Xe(e,"power",c=>kt(c)&&c>0&&c<1,"'power' must be strictly between 0 and 1"),Xe(t,"theta",Nn,"'theta' must be positive and finite"),Xe(n,"p",kt,"'p' must be in [0, 1]"),Xe(r,"rho2",kt,"'rho2' must be in [0, 1]");let i=Math.log(t)**2*n*(1-n)*(1-r);return Math.ceil((cn(1-s/2)+cn(e))**2/i)}function sh(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 Iv(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 ah(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 _v(e,t,n,r,o={}){V(o,wv,"ph2simon");let s=o.nmax??100;for(let[E,b]of[[e,"pu"],[t,"pa"],[n,"ep1"],[r,"ep2"]])Xe(E,b,kt,`'${b}' must be in [0, 1]`);Xe(s,"nmax",E=>Number.isInteger(E)&&E>=1&&E<=1e3,"nmax cannot exceed 1000");let i=Array.from({length:s+1},(E,b)=>mo(b,e)),c=Array.from({length:s+1},(E,b)=>mo(b,t)),a=i.map(sh),l=c.map(sh),f=i.map(Iv),m=[];for(let E=2;E<=s;E++){let b;for(let M=1;M<E&&(b===void 0||M<b.enP0);M++){let x=E-M,y=i[M],R=c[M],T=l[x],S=a[M];for(let k=0;k<M;k++){let L=S[k],_=M+x*(1-L),I=k,F=E-1,C=-1;for(;I<=F;){let D=Math.floor((I+F)/2),[,A]=ah(k,D,y,R,f[x],T);A<=r?(C=D,I=D+1):F=D-1}if(C>=0){let[D]=ah(k,C,y,R,f[x],T);D>n&&(C=-1)}C>=0&&(b===void 0||_<b.enP0)&&(b={r1:k,n1:M,r:C,n:E,enP0:_,petP0:L})}}b&&m.push(b)}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,b)=>!m.some((M,x)=>x!==b&&M.n<=E.n&&M.enP0<=E.enP0&&(M.n<E.n||M.enP0<E.enP0))),h=[],p=(E,b)=>(E.enP0-b.enP0)/(b.n-E.n+(E.enP0-b.enP0));for(let E of u)for(h.push(E);h.length>=3;){let b=h[h.length-3],M=h[h.length-2],x=h[h.length-1];if(p(b,M)>p(M,x))break;h.splice(h.length-2,1)}let d=h.map((E,b)=>{let M=b===h.length-1?0:p(E,h[b+1]),x=b===0?1:p(h[b-1],E);return{...E,qLo:M,qHi:x}}),g=d[0],w=d.reduce((E,b)=>b.enP0<E.enP0?b:E),N=d,v=[];return w.n>=s-1&&v.push(` Optimal sample size too close to nmax.
21
21
  Try increasing nmax (current value = ${s})
22
- `),J("ph2simon",{out:m,minimax:d,optimal:v,admissible:N,pu:e,pa:t,alpha:n,beta:r,nmax:s,warnings:b})}var zy=new Set(["ai","bi","ci","di","m1i","sd1i","n1i","m2i","sd2i","n2i","ri","ni","add","to","drop00"]),_p=["method","mods","data","intercept","test","level","control"],Lp=["tau2Init","threshold","maxiter","stepadj","tol","tau2Max"],Ky=["type","level"],Wy=["newmods","predtype","level"],Cp=["model","predictor","retFit","level"],Rn=e=>e.length===1?e[0]:e,An=(e,t)=>e.reduce((n,r,o)=>n+r*t[o],0),ds=(e,t)=>e.map(n=>An(n,t)),Pp=e=>Array.from({length:e},(t,n)=>Array.from({length:e},(r,o)=>n===o?1:0)),ko=(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 a=0;a<180;a++){let c=t+(n-t)/2,i=e(c);if(!Number.isFinite(i)){n=c;continue}if(Math.abs(n-t)<=r*Math.max(1,Math.abs(c))||i===0)return c;o*i<=0?(n=c,s=i):(t=c,o=i)}return t+(n-t)/2},Op=(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?ko(e,0,n):NaN};function Zt(e,t,n,r){let o=e.length,s=n[0].length,a=t.map(O=>O+r),c=a.reduce((O,L)=>Math.min(O,L),1/0),i=a.map(O=>c/O),l=i.reduce((O,L)=>O+L,0),f=Array.from({length:s},(O,L)=>Array.from({length:s},(D,S)=>n.reduce((F,k,P)=>F+i[P]*k[L]*k[S],0))),m=Array.from({length:s},(O,L)=>n.reduce((D,S,F)=>D+i[F]*S[L]*e[F],0)),u;try{u=rt(f,Pp(s))}catch{throw new RangeError("Model matrix is not full rank.")}let h=ds(u,m),p=ds(n,h),g=e.map((O,L)=>O-p[L]),d=e.every(O=>O===e[0])?0:g.reduce((O,L,D)=>O+i[D]*L*L,0),v=d/c,N=Array.from({length:o},(O,L)=>Array.from({length:o},(D,S)=>(L===S?i[L]:0)-i[L]*An(n[L],ds(u,n[S]).map(F=>F*i[S])))),b=N.reduce((O,L,D)=>O+L[D],0),E=b/c,w=N.reduce((O,L,D)=>O+L.reduce((S,F,k)=>S+F*N[k][D],0),0),y=w/c/c,x=N.map(O=>An(O,e)),M=.5*(An(x,x)-c*b),R=.5*(g.reduce((O,L,D)=>O+L*L*i[D]*i[D],0)-c*i.reduce((O,L)=>O+L,0)),A=i.reduce((O,L)=>O+L*L,0),T=.5*A,q=f.length===1?Math.log(f[0][0]):Math.log(Math.abs(By(f))),C=-.5*(a.reduce((O,L)=>O+Math.log(L),0)+q-s*Math.log(c)+v),_=u.map(O=>O.map(L=>L*c)),I=u.map((O,L)=>Math.sqrt(Math.max(0,O[L]))*Math.sqrt(c));return{beta:h,cov:_,se:I,fitted:p,residuals:g,qe:v,qeScaled:d,weights:i,minVariance:c,P:N,traceP:E,tracePNorm:b,traceP2:y,traceP2Norm:w,score:M,mlScore:R,mlInfo:T,logLik:C,sumWeightsNorm:l,sumWeightSquaredNorm:A}}function By(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 a=n[o][o];r*=a;for(let c=o+1;c<t;c++){let i=n[c][o]/a;for(let l=o+1;l<t;l++)n[c][l]-=i*n[o][l]}}return r}function wl(e,t){return e.reduce((n,r,o)=>n+r.reduce((s,a,c)=>s+a*t[c]*e[c][o]*t[o],0),0)}function Uy(e,t,n,r,o,s,a){let c=e.length,i=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*wl(a.P,l)),u=r==="DL"?a.tracePNorm:r==="HS"?a.sumWeightsNorm:a.sumWeightsNorm*(c-1)/c;return u>0?m/u:NaN}if(r==="HE"){let m=Zt(e,Array(c).fill(1),n,0);return i>0?Math.sqrt(2*wl(m.P,l))/i:NaN}if(r==="SJ"){let m=Zt(e,Array(c).fill(1),n,0),u=m.residuals.reduce((p,g)=>p+g*g,0)/c;if(!(u>0)||i<=0)return NaN;let h=Zt(e,t,n,u);return u/i*Math.sqrt(2*wl(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&&i>0?Math.sqrt(2*i)*s.minVariance*s.minVariance/f:NaN}function Fp(e,t,n,r,o){let s=e.length,a=n[0].length,c=s-a,i=Zt(e,t,n,0);if(r==="EE"||r==="FE")return 0;if(r==="DL")return Math.max(0,(i.qeScaled-c*i.minVariance)/i.tracePNorm);if(r==="HS")return Math.max(0,(i.qeScaled-s*i.minVariance)/i.sumWeightsNorm);if(r==="HSk")return Math.max(0,(s/(s-1)*i.qeScaled-s*i.minVariance)/i.sumWeightsNorm);let l=Zt(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=Zt(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=>Zt(e,t,n,v).qe-c;if(r==="EB"){let v=o.tau2Max??Math.max(100,10*f/s);return Math.max(0,ko(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,ko(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=Zt(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 Dp(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(i=>i[r]),s=Math.sqrt(An(o,o)),a=o.slice();for(let i of n){let l=An(a,i);a=a.map((f,m)=>f-l*i[m])}let c=Math.sqrt(An(a,a));s>0&&c>1e-10*s&&(t.push(r),n.push(a.map(i=>i/c)))}return t}function Hy(e,t,n,r){let o,s=[],a=!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=Wn(Sn(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=Dp(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),a=!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),a=!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(a||(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 i=Dp(o);if(i.length<o[0].length&&!a&&c>0){for(let f=0;f<i.length;f++)if(i[f]!==0||!n){s[i[f]]="";break}}return{x:o.map(f=>i.map(m=>f[m])),names:i.map(f=>s[f]??""),warnings:i.length<o[0].length?["Redundant predictors dropped from the model."]:[]}}function vl(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))-vl(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 Vp(e,t,n,r){let o=Math.max(e,t,n,r);return o===0?0:2**Math.floor(Math.log2(o))}function bl(e,t,n,r,o){let s=Vp(e,t,n,r);if(s===0)return NaN;let a=e/s,c=t/s,i=n/s,l=r/s,f=De(Kt(a,l),Kt(c,i)),m=Dt(a,c),u=Dt(i,l),h=o==="RR"?Ce([i,0],m):o==="OR"?Kt(c,i):Ce(m,u),p=ot(f,h);return p[0]+p[1]}function Yy(e){let t=e/2;if(t>1e3){let n=1/t,r=n*n,o=r*n,s=r*r,a=s*n,c=o*o,i=c*n,l=s*s,f=-(3/8)*n-1/8*r-3/64*o-1/64*s-3/640*a-1/384*c-33/14336*i-1/2048*l;return Math.exp(f)}return Math.exp(vl(t)-.5*Math.log(t)-vl(t-.5))}function Gy(e,t){K(t,zy,"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)),a=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 i=[],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=Yy(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"&&a;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)){i.push(NaN),l.push(NaN);continue}let R=Vp(v,N,b,E),A=v/R,T=N/R,q=b/R,C=E/R,_=A+T,I=q+C,O=1/R;e==="RR"?(m=Math.log1p(bl(v,N,b,E,"RR")),u=O*(T/(A*_)+C/(q*I))):e==="OR"?(m=Math.log1p(bl(v,N,b,E,"OR")),u=O*(1/A+1/T+1/q+1/C)):(m=bl(v,N,b,E,"RD"),u=O*(A*T/(_*_*_)+q*C/(I*I*I)))}i.push(m),l.push(u)}return J("escalc",{yi:i,vi:l,measure:e,warnings:n})}function $p(e,t,n={}){ct(t)&&K(t,_p,"rma"),ct(t)&&K(_t(t,"control"),Lp,"rma","control"),K(n,_p,"rma"),K(_t(n,"control"),Lp,"rma","control");let r=typeof e=="object"&&"yi"in e,o=Array.from(r?e.yi:e),s=(r?t:n)??{},a=Array.from(r?e.vi:t??[]);if(o.length===0||a.length!==o.length)throw new RangeError("yi and vi must have the same positive length");if(o.some(B=>!Number.isFinite(B)))throw new Error("NA/NaN/Inf in foreign function call (arg 1)");if(a.some(B=>!Number.isFinite(B)))throw new Error("NA/NaN/Inf in foreign function call (arg 1)");if(s.method==="DL"&&a.some(B=>B<=0))throw new Error("DL estimator cannot be used when there are non-positive sampling variances in the data.");if(a.some(B=>B<0))throw new RangeError("sampling variances must be non-negative");if(a.some(B=>B===0)&&!["EE","FE"].includes(s.method??"REML"))throw new RangeError("random-effects estimators require positive sampling variances");let c=s.method??"REML",i=s.test??"z",l=s.level??95,f=s.intercept??!0,m=Hy(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(B=>B[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(i))throw new RangeError(`unknown test '${i}'`);if(c==="DL"&&!Number.isFinite(Zt(o,a,u,0).qe))throw new Error("NA/NaN/Inf in foreign function call (arg 1)");let v=Zt(o,a,u,0),N=Fp(o,a,u,c,s.control??{}),b=Zt(o,a,u,N),E=s.mods===void 0?N:Fp(o,a,Array.from({length:p},()=>[1]),c,s.control??{}),w=i==="knha"&&g>0?Math.max(1,b.qe/g):1,y=b.cov.map(B=>B.map(Q=>Q*w)),x=b.se.map(B=>B*Math.sqrt(w)),M=b.beta.map((B,Q)=>B/x[Q]),R=i==="z"?ie(1-(1-l/100)/2):Bt(1-(1-l/100)/2,g),A=M.map(B=>2*(i==="z"?ae(-Math.abs(B)):Wt(-Math.abs(B),g))),T=b.beta.map((B,Q)=>B-R*x[Q]),q=b.beta.map((B,Q)=>B+R*x[Q]),C=d?1:0,_=h===1?1:Math.max(1,h-C),I=0,O=1;if(h===1)I=M[0]*M[0],O=i==="z"?Nt(I,1,{lowerTail:!1}):dn(I,1,g,{lowerTail:!1});else{let B=b.beta.slice(C),Q=y.slice(C).map(re=>re.slice(C));try{I=An(B,rt(Q,B))}catch{I=NaN}O=i==="z"?Nt(I,_,{lowerTail:!1}):dn(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),F=c==="EE"||c==="FE"?g>0?Math.max(1,v.qe/g):1:N===0?1:(N+D)/D,k=s.mods===void 0||h===1?null:E>0?100*Math.max(0,(E-N)/E):0,P=Uy(o,a,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(B=>[B])},se:Rn(x),zval:Rn(M),pval:Rn(A),"ci.lb":Rn(T),"ci.ub":Rn(q),tau2:N,"se.tau2":P,QE:v.qe,QEp:g===0?1:Nt(v.qe,g,{lowerTail:!1}),QM:I,QMp:O,R2:k,p:h,m:_,I2:S,H2:F,k:p,yi:o,vi:a,mods:u,intercept:d,method:c,test:i,level:l,warnings:m.warnings,covariance:y,tau2Null:E})}function Xy(e,t={}){K(t,Ky,"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=Zt(e.yi,e.vi,e.mods,e.tau2),a=e.k-e.mods[0].length,c=0,i=NaN;if(n==="QP"){let d=Zt(e.yi,e.vi,e.mods,0),v=Pn(1-o/2,a),N=Pn(o/2,a),b=d.qe,E=w=>Zt(e.yi,e.vi,e.mods,w).qe;c=b<=v?0:Op(w=>E(w)-v,Math.max(e.tau2,1e-8)),i=b<=N?NaN:Op(w=>E(w)-N,Math.max(e.tau2,1e-8))}else{let d=Pn(r/100,1),v=b=>Zt(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:ko(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);i=E>=0?ko(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);i=E>=0?ko(N,0,b):NaN}}let l=Zt(e.yi,e.vi,e.mods,0),f=a>0?a*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(i),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 Qy(e,t={}){K(t,Wy,"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 a=e.beta.rows.map(u=>u[0]),c=s.map(u=>An(u,a)),i=s.map(u=>Math.sqrt(Math.max(0,An(u,ds(e.covariance,u))))),l=e.test==="z"?ie(1-r/2):Bt(1-r/2,e.k-o),f=t.predtype==="riley"?Bt(1-r/2,e.k-o-1):l,m=s.map((u,h)=>Math.sqrt(i[h]**2+e.tau2));return{pred:Rn(c),se:Rn(i),"ci.lb":Rn(c.map((u,h)=>u-l*i[h])),"ci.ub":Rn(c.map((u,h)=>u+l*i[h])),"pi.lb":Rn(c.map((u,h)=>u-f*m[h])),"pi.ub":Rn(c.map((u,h)=>u+f*m[h]))}}function Zy(e,t,n={}){ct(t)&&K(t,Cp,"regtest"),K(n,Cp,"regtest");let r=typeof e=="object"&&"beta"in e,o=(r?t:n)??{},s=o.model??"rma",a=o.predictor??"sei",c=o.level??95,i=Array.from(r?e.yi:e),l=Array.from(r?e.vi:t??[]);if(i.length!==l.length||i.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=a==="sei"?f:a==="vi"?l:l.map((w,y)=>1/(y+1)),u,h,p,g,d,v;if(s==="lm"){let w=m.map((A,T)=>[1,1/A]),y=i.map((A,T)=>A/f[T]),x=Jy(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=i.length-2,d=2*Wt(-Math.abs(p),g)}else{v=$p(i,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"?Bt(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:a,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 Jy(e,t){let n=Array.from({length:2},(c,i)=>Array.from({length:2},(l,f)=>e.reduce((m,u,h)=>m+u[i]*u[f],0))),r=rt(n,Pp(2)),o=ds(r,Array.from({length:2},(c,i)=>e.reduce((l,f,m)=>l+f[i]*t[m],0))),s=t.map((c,i)=>c-An(e[i],o)),a=t.length>2?s.reduce((c,i)=>c+i*i,0)/(t.length-2):NaN;return{beta:o,cov:r,sigma2:a}}var eM=["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=Kt(n,n),o=At(Kt(e,e),t),s=De(r,o);if(s[0]+s[1]<=0)return 0;let a=ot(s,r),c=ot(o,r),i=_l((t-2)/2,.5,a,c,!0,!1);return Math.min(1,t*i/2)}function jp(e,t){let n=Nl(e,t);return Number.isNaN(n)?NaN:1-n}function tM(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 za(e,r=>jp(r,t))}function nM(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 rM(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 nM(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;jp(s,t)<e?n=s:r=s}return n+(r-n)/2}function oM(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 za(e,r=>rM(r,t))}function sM(e,t={}){K(t,eM,"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,a=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 i=ys(r),l=r.indexOf(c),f=Math.abs(i[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,a?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{Oc as Anova,yN as ICC,Zw as TukeyHSD,bv as adTest,u1 as add1,$i as anova,su as ansariTest,Xw as aov,Ub as aovEz,ji as aovSummary,Bv as auc,Im as bartlettTest,ki as binomCI,x0 as binomDiffCI,F0 as binomDiffEquivalenceTest,A0 as binomRatioCI,Zd as binomTest,rN as blandAltman,vg as boschlooTest,Sw as brunnerMunzelTest,hd as chisqTest,Xv as ciAuc,Jb as clogit,Tg as cochranQTest,rb as coefci,tb as coeftest,gh as cohenES,H0 as cohensD,Hw as confint,Xy as confintRma,Hv as coords,Kp as corTest,rv as coxZph,fo as coxph,kv as cronbachAlpha,YN as crr,KN as cuminc,vv as cvmTest,eN as diagnosticTests,m1 as drop1,Zg as dscfAllPairsTest,nw as dunnTest,Xg as dunnettTest,Sy as epiSsequb,ky as epiSsequc,qy as epiSsninfb,Iy as epiSsninfc,_y as epiSssupb,Ly as epiSssupc,ng as epsilonSquared,Ab as esH,Tb as esW1,Sb as esW2,Gy as escalc,eg as etaSquared,f1 as extractAIC,RN as factanal,Ko as fisherTest,Cm as flignerTest,Gi as friedmanTest,ad as gTest,ew as gamesHowellTest,ow as glhtDunnett,sr as glm,ib as glmAnova,t1 as glmConfint,Up as glmCooksDistance,ti as glmHatvalues,Hp as glmPredict,Wp as glmResiduals,Bp as glmRstandard,ei as glmSummary,fd as goodmanKruskalGamma,sM as grubbsTest,Y0 as hedgesG,Sv as icc,gv as jonckheereTest,Av as kappa2,Tv as kappamFleiss,sa as kruskalTest,pu as ksTest,Yg as kwAllPairsDunnTest,Gg as kwManyOneDunnTest,ud as lblTest,av as leveneTest,Nv as lillieTest,Or as lm,mw as mantelhaenTest,Qg as manyOneUTest,hb as mauchlyTest,Mg as mcnemarTest,dN as mcreg,to as modelMatrix,Om as moodTest,Vy as numDEpi,I0 as oddsRatio,_N as omega,tg as omegaSquared,Hm as onewayTest,ro as pAdjust,Ag as pageTest,Pw as pairwisePropTest,Fw as pairwiseTTest,Dw as pairwiseWilcoxTest,Iv as partialR,Mv as pearsonTest,tM as pgrubbs,jy as ph2simon,o0 as poissonTest,DN as polychor,VN as polyserial,td as powerAnovaTest,Oy as powerCT,Dy as powerEpi,Cy as powerPaired2x2,Jp as powerPropTest,Zp as powerTTest,Yw as predict,GN as predictCrr,Qy as predictRma,Ti as propTest,lv as propTrendTest,Pb as pwr2p2nTest,Ob as pwr2pTest,jb as pwrAnovaTest,$b as pwrChisqTest,zb as pwrF2Test,Db as pwrNormTest,Fb as pwrPTest,Vb as pwrRTest,Cb as pwrT2nTest,Lb as pwrTTest,oM as qgrubbs,Lm as quadeTest,Zy as regtest,$p as rma,Xb as rmst2,Kv as roc,Zv as rocTest,zp as setWarningHandler,yv as sfTest,aw as shapiroTest,Fy as ssizeCT,Py as ssizeEpi,p1 as step,d1 as stepAIC,ld as stuartMaxwellTest,W1 as survdiff,dr as survfit,Y1 as survfitCoxph,my as survreg,uy as survregPredict,hy as survregResiduals,L1 as svyglm,fg as tTOST,En as tTest,Jg as tamhaneT2Test,BN as timepoints,C0 as tost,mg as tsumTOST,tw as tukeyTest,qm as varTest,q1 as vcovCL,qc as vcovHC,Wo as wilcoxTest,Jw as zTest};
22
+ `),H("ph2simon",{out:m,minimax:g,optimal:w,admissible:N,pu:e,pa:t,alpha:n,beta:r,nmax:s,warnings:v})}var Lv=new Set(["ai","bi","ci","di","m1i","sd1i","n1i","m2i","sd2i","n2i","ri","ni","add","to","drop00"]),ih=["method","mods","data","intercept","test","level","control"],ch=["tau2Init","threshold","maxiter","stepadj","tol","tau2Max"],Cv=["type","level"],Ov=["newmods","predtype","level"],lh=["model","predictor","retFit","level"],yn=e=>e.length===1?e[0]:e,Mn=(e,t)=>e.reduce((n,r,o)=>n+r*t[o],0),Ho=(e,t)=>e.map(n=>Mn(n,t)),hh=e=>Array.from({length:e},(t,n)=>Array.from({length:e},(r,o)=>n===o?1:0)),ho=(e,t,n,r=1e-14)=>{let o=e(t),s=e(n);if(o===0)return t;if(s===0)return n;if(!(o*s<0))return NaN;for(let i=0;i<180;i++){let c=t+(n-t)/2,a=e(c);if(!Number.isFinite(a)){n=c;continue}if(Math.abs(n-t)<=r*Math.max(1,Math.abs(c))||a===0)return c;o*a<=0?(n=c,s=a):(t=c,o=a)}return t+(n-t)/2},fh=(e,t)=>{let n=Math.max(t,1e-8),r=e(n);for(let o=0;o<80&&r>0;o++)n*=2,r=e(n);return r<=0?ho(e,0,n):NaN};function Gt(e,t,n,r){let o=e.length,s=n[0].length,i=t.map(F=>F+r),c=i.reduce((F,C)=>Math.min(F,C),1/0),a=i.map(F=>c/F),l=a.reduce((F,C)=>F+C,0),f=Array.from({length:s},(F,C)=>Array.from({length:s},(D,A)=>n.reduce((O,q,P)=>O+a[P]*q[C]*q[A],0))),m=Array.from({length:s},(F,C)=>n.reduce((D,A,O)=>D+a[O]*A[C]*e[O],0)),u;try{u=rt(f,hh(s))}catch{throw new RangeError("Model matrix is not full rank.")}let h=Ho(u,m),p=Ho(n,h),d=e.map((F,C)=>F-p[C]),g=e.every(F=>F===e[0])?0:d.reduce((F,C,D)=>F+a[D]*C*C,0),w=g/c,N=Array.from({length:o},(F,C)=>Array.from({length:o},(D,A)=>(C===A?a[C]:0)-a[C]*Mn(n[C],Ho(u,n[A]).map(O=>O*a[A])))),v=N.reduce((F,C,D)=>F+C[D],0),E=v/c,b=N.reduce((F,C,D)=>F+C.reduce((A,O,q)=>A+O*N[q][D],0),0),M=b/c/c,x=N.map(F=>Mn(F,e)),y=.5*(Mn(x,x)-c*v),R=.5*(d.reduce((F,C,D)=>F+C*C*a[D]*a[D],0)-c*a.reduce((F,C)=>F+C,0)),T=a.reduce((F,C)=>F+C*C,0),S=.5*T,k=f.length===1?Math.log(f[0][0]):Math.log(Math.abs(Fv(f))),L=-.5*(i.reduce((F,C)=>F+Math.log(C),0)+k-s*Math.log(c)+w),_=u.map(F=>F.map(C=>C*c)),I=u.map((F,C)=>Math.sqrt(Math.max(0,F[C]))*Math.sqrt(c));return{beta:h,cov:_,se:I,fitted:p,residuals:d,qe:w,qeScaled:g,weights:a,minVariance:c,P:N,traceP:E,tracePNorm:v,traceP2:M,traceP2Norm:b,score:y,mlScore:R,mlInfo:S,logLik:L,sumWeightsNorm:l,sumWeightSquaredNorm:T}}function Fv(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 Ec(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 Dv(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*Ec(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*Ec(m.P,l))/a:NaN}if(r==="SJ"){let m=Gt(e,Array(c).fill(1),n,0),u=m.residuals.reduce((p,d)=>p+d*d,0)/c;if(!(u>0)||a<=0)return NaN;let h=Gt(e,t,n,u);return u/a*Math.sqrt(2*Ec(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 mh(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((w,N)=>w+N*N,0),m=Math.max(0,(f-t.reduce((w,N,v)=>w+l.P[v][v]*N,0))/Math.max(1,c));if(r==="HE")return m;if(r==="SJ"){let w=f/s,N=Gt(e,t,n,w),v=t.map(E=>w/(E+w));return Math.max(0,v.reduce((E,b,M)=>E+b*N.residuals[M]*N.residuals[M],0)/Math.max(1,c))}let u=w=>Gt(e,t,n,w).qe-c;if(r==="EB"){let w=o.tau2Max??Math.max(100,10*f/s);return Math.max(0,ho(u,0,w,1e-14))}if(r==="PM"){let w=o.tau2Max??Math.max(100,10*f/s);for(;u(w)>0&&w<Number.MAX_VALUE/4;)w*=2;let N=o.tol??1e-14;return Math.max(0,ho(u,0,w,N))}let h=Math.max(0,o.tau2Init??m),p=o.maxiter??100,d=o.threshold??1e-5,g=o.stepadj??1;if(p<=1)return h;for(let w=0;w<p;w++){let N=Gt(e,t,n,h),v=r==="REML"?N.score:N.mlScore,E=r==="REML"?.5*N.traceP2Norm:N.mlInfo;if(!(E>0)||!Number.isFinite(E))return h;let b=Math.max(0,h+g*v/E);if(!Number.isFinite(b))return h;if(Math.abs(b-h)<d)return b;h=b}return h}function uh(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(Mn(o,o)),i=o.slice();for(let a of n){let l=Mn(i,a);i=i.map((f,m)=>f-l*a[m])}let c=Math.sqrt(Mn(i,i));s>0&&c>1e-10*s&&(t.push(r),n.push(i.map(a=>a/c)))}return t}function Pv(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=gr(Xn(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=uh(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=uh(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 Rc(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))-Rc(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 ph(e,t,n,r){let o=Math.max(e,t,n,r);return o===0?0:2**Math.floor(Math.log2(o))}function xc(e,t,n,r,o){let s=ph(e,t,n,r);if(s===0)return NaN;let i=e/s,c=t/s,a=n/s,l=r/s,f=Ce(Ht(i,l),Ht(c,a)),m=$t(i,c),u=$t(a,l),h=o==="RR"?_e([a,0],m):o==="OR"?Ht(c,a):_e(m,u),p=lt(f,h);return p[0]+p[1]}function Vv(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(Rc(t)-.5*Math.log(t)-Rc(t-.5))}function $v(e,t){V(t,Lv,"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],d=s[2][f],g=s[3][f],w=s[4][f],N=s[5][f];if(!(d>1&&N>1&&p>=0&&w>=0))throw new RangeError("invalid means, standard deviations, or sample sizes");if(e==="MD")m=h-g,u=p*p/d+w*w/N;else{let v=d+N-2,E=((d-1)*p*p+(N-1)*w*w)/v,b=(h-g)/Math.sqrt(E);m=Vv(v)*b,u=(d+N)/(d*N)+m*m/(2*(d+N))}}else{let h=s[0][f],p=s[1][f],d=s[2][f],g=s[3][f],w=h,N=p,v=d,E=g,b=t.add??.5,M=t.to??"only0",x=[w,N,v,E].some(C=>C===0),y=M==="all"||M==="only0"&&x||M==="if0all"&&i;if(e!=="RD"&&y&&(w+=b,N+=b,v+=b,E+=b),e==="RD"&&y&&(w+=b,N+=b,v+=b,E+=b),t.drop00&&(h===0&&d===0||p===0&&g===0)){a.push(NaN),l.push(NaN);continue}let R=ph(w,N,v,E),T=w/R,S=N/R,k=v/R,L=E/R,_=T+S,I=k+L,F=1/R;e==="RR"?(m=Math.log1p(xc(w,N,v,E,"RR")),u=F*(S/(T*_)+L/(k*I))):e==="OR"?(m=Math.log1p(xc(w,N,v,E,"OR")),u=F*(1/T+1/S+1/k+1/L)):(m=xc(w,N,v,E,"RD"),u=F*(T*S/(_*_*_)+k*L/(I*I*I)))}a.push(m),l.push(u)}return H("escalc",{yi:a,vi:l,measure:e,warnings:n})}function dh(e,t,n={}){at(t)&&V(t,ih,"rma"),at(t)&&V(Ft(t,"control"),ch,"rma","control"),V(n,ih,"rma"),V(Ft(n,"control"),ch,"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(B=>!Number.isFinite(B)))throw new Error("NA/NaN/Inf in foreign function call (arg 1)");if(i.some(B=>!Number.isFinite(B)))throw new Error("NA/NaN/Inf in foreign function call (arg 1)");if(s.method==="DL"&&i.some(B=>B<=0))throw new Error("DL estimator cannot be used when there are non-positive sampling variances in the data.");if(i.some(B=>B<0))throw new RangeError("sampling variances must be non-negative");if(i.some(B=>B===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=Pv(o.length,s.mods,f,s.data),u=m.x,h=u[0].length,p=o.length,d=p-h,g=m.names[0]==="intrcpt"||!f&&u.every(B=>B[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 w=Gt(o,i,u,0),N=mh(o,i,u,c,s.control??{}),v=Gt(o,i,u,N),E=s.mods===void 0?N:mh(o,i,Array.from({length:p},()=>[1]),c,s.control??{}),b=a==="knha"&&d>0?Math.max(1,v.qe/d):1,M=v.cov.map(B=>B.map(J=>J*b)),x=v.se.map(B=>B*Math.sqrt(b)),y=v.beta.map((B,J)=>B/x[J]),R=a==="z"?oe(1-(1-l/100)/2):rn(1-(1-l/100)/2,d),T=y.map(B=>2*(a==="z"?ie(-Math.abs(B)):dn(-Math.abs(B),d))),S=v.beta.map((B,J)=>B-R*x[J]),k=v.beta.map((B,J)=>B+R*x[J]),L=g?1:0,_=h===1?1:Math.max(1,h-L),I=0,F=1;if(h===1)I=y[0]*y[0],F=a==="z"?Mt(I,1,{lowerTail:!1}):xn(I,1,d,{lowerTail:!1});else{let B=v.beta.slice(L),J=M.slice(L).map(ce=>ce.slice(L));try{I=Mn(B,rt(J,B))}catch{I=NaN}F=a==="z"?Mt(I,_,{lowerTail:!1}):xn(I/_,_,d,{lowerTail:!1})}let C=w.tracePNorm,D=d>0?d*w.minVariance/C:0,A=c==="EE"||c==="FE"?w.qe>0?100*Math.max(0,(w.qe-d)/w.qe):0:N===0?0:100*N/(N+D),O=c==="EE"||c==="FE"?d>0?Math.max(1,w.qe/d):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,P=Dv(o,i,u,c,N,v,w);return H("rma",{beta:{dim:[h,1],rowNames:h===1?m.names[0]??"intrcpt":m.names,colNames:null,rows:v.beta.map(B=>[B])},se:yn(x),zval:yn(y),pval:yn(T),"ci.lb":yn(S),"ci.ub":yn(k),tau2:N,"se.tau2":P,QE:w.qe,QEp:d===0?1:Mt(w.qe,d,{lowerTail:!1}),QM:I,QMp:F,R2:q,p:h,m:_,I2:A,H2:O,k:p,yi:o,vi:i,mods:u,intercept:g,method:c,test:a,level:l,warnings:m.warnings,covariance:M,tau2Null:E})}function zv(e,t={}){V(t,Cv,"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 g=Gt(e.yi,e.vi,e.mods,0),w=Cn(1-o/2,i),N=Cn(o/2,i),v=g.qe,E=b=>Gt(e.yi,e.vi,e.mods,b).qe;c=v<=w?0:fh(b=>E(b)-w,Math.max(e.tau2,1e-8)),a=v<=N?NaN:fh(b=>E(b)-N,Math.max(e.tau2,1e-8))}else{let g=Cn(r/100,1),w=v=>Gt(e.yi,e.vi,e.mods,v).logLik,N=v=>2*(s.logLik-w(v))-g;if(e.tau2>0){c=N(0)<=0?0:ho(N,0,e.tau2);let v=Math.max(e.tau2*2,1e-8),E=N(v);for(let b=0;b<80&&E<0;b++)v*=2,E=N(v);a=E>=0?ho(N,e.tau2,v):NaN}else{let v=1e-8,E=N(v);for(let b=0;b<80&&E<0;b++)v*=2,E=N(v);a=E>=0?ho(N,0,v):NaN}}let l=Gt(e.yi,e.vi,e.mods,0),f=i>0?i*l.minVariance/l.tracePNorm:0,m=g=>[g,Math.sqrt(g),100*g/(g+f),(g+f)/f],u=m(e.tau2),h=m(c),p=m(a),d=["tau^2","tau","I^2(%)","H^2"];return{random:{dim:[4,3],rowNames:d,colNames:["estimate","ci.lb","ci.ub"],rows:d.map((g,w)=>({estimate:u[w],"ci.lb":h[w],"ci.ub":p[w]}))},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 jv(e,t={}){V(t,Ov,"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=>Mn(u,i)),a=s.map(u=>Math.sqrt(Math.max(0,Mn(u,Ho(e.covariance,u))))),l=e.test==="z"?oe(1-r/2):rn(1-r/2,e.k-o),f=t.predtype==="riley"?rn(1-r/2,e.k-o-1):l,m=s.map((u,h)=>Math.sqrt(a[h]**2+e.tau2));return{pred:yn(c),se:yn(a),"ci.lb":yn(c.map((u,h)=>u-l*a[h])),"ci.ub":yn(c.map((u,h)=>u+l*a[h])),"pi.lb":yn(c.map((u,h)=>u-f*m[h])),"pi.ub":yn(c.map((u,h)=>u+f*m[h]))}}function Kv(e,t,n={}){at(t)&&V(t,lh,"regtest"),V(n,lh,"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(b=>Math.sqrt(b)),m=i==="sei"?f:i==="vi"?l:l.map((b,M)=>1/(M+1)),u,h,p,d,g,w;if(s==="lm"){let b=m.map((T,S)=>[1,1/T]),M=a.map((T,S)=>T/f[S]),x=Wv(b,M);u=x.beta[1];let y=Math.sqrt(x.cov[0][0]*x.sigma2),R=Math.sqrt(x.cov[1][1]*x.sigma2);p=x.beta[0]/y,h=R,d=a.length-2,g=2*dn(-Math.abs(p),d)}else{w=dh(a,l,{method:"REML",mods:m,intercept:!0,test:"z"}),u=w.beta.rows[0][0],h=Array.isArray(w.se)?w.se[0]:w.se;let b=Array.isArray(w.zval)?w.zval[1]:NaN,M=Array.isArray(w.pval)?w.pval[1]:NaN;p=b,d=NaN,g=M}let N=s==="lm"?rn(1-(1-c/100)/2,d):oe(1-(1-c/100)/2),v=[];s==="lm"&&(!Number.isFinite(p)||!Number.isFinite(g)||!Number.isFinite(h))&&v.push("NaNs produced");let E={model:s,predictor:i,zval:p,pval:g,dfs:d,est:u,"ci.lb":u-N*h,"ci.ub":u+N*h,warnings:v};return H("regtest",o.retFit&&w!==void 0?{...E,fit:w}:E)}function Wv(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,hh(2)),o=Ho(r,Array.from({length:2},(c,a)=>e.reduce((l,f,m)=>l+f[a]*t[m],0))),s=t.map((c,a)=>c-Mn(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 Yv=["type","opposite","twoSided"];function Tc(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=Ht(n,n),o=zt(Ht(e,e),t),s=Ce(r,o);if(s[0]+s[1]<=0)return 0;let i=lt(s,r),c=lt(o,r),a=Pc((t-2)/2,.5,i,c,!0,!1);return Math.min(1,t*a/2)}function gh(e,t){let n=Tc(e,t);return Number.isNaN(n)?NaN:1-n}function Bv(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 ha(e,r=>gh(r,t))}function Uv(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;Tc(o,e)>=1?t=o:n=o}return n}function Gv(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 Uv(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;gh(s,t)<e?n=s:r=s}return n+(r-n)/2}function Hv(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 ha(e,r=>Gv(r,t))}function Xv(e,t={}){V(t,Yv,"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=ts(r),l=r.indexOf(c),f=Math.abs(a[l]),m=r.slice();m.splice(l,1);let u=Et([r,m]),h=(o-2)*u.variance[1]/((o-1)*u.variance[0]),p=Tc(f,o),d=1-p,g=Number.isNaN(f)?NaN:Math.min(1,i?2*Math.min(d,p):p);return{method:"Grubbs test for one outlier",statistic:[f,h],statisticName:["G","U"],pValue:g,alternative:`${s?"lowest":"highest"} value ${String(c)} is an outlier`,warnings:o===2?["NaNs produced"]:[]}}export{Ki as Anova,qb as ICC,Mg as TukeyHSD,S1 as adTest,Fg as add1,ii as anova,im as ansariTest,Ng as aov,t1 as aovEz,ci as aovSummary,eb as auc,Lf as bartlettTest,Xa as binomCI,nd as binomDiffCI,pd as binomDiffEquivalenceTest,od as binomRatioCI,qp as binomTest,ub as blandAltman,Qd as boschlooTest,ag as brunnerMunzelTest,Yh as chisqTest,sb as ciAuc,c1 as clogit,s0 as cochranQTest,uw as coefci,fw as coeftest,nu as cohenES,Rd as cohensD,Mh as confint,zv as confintRma,nb as coords,yh as corTest,u1 as coxZph,Hr as coxph,P1 as cronbachAlpha,rv as crr,Zb as cuminc,A1 as cvmTest,lb as diagnosticTests,Og as drop1,q0 as dscfAllPairsTest,C0 as dunnTest,A0 as dunnettTest,vv as epiSsequb,Nv as epiSsequc,yv as epiSsninfb,Mv as epiSsninfc,Ev as epiSssupb,xv as epiSssupc,Cd as epsilonSquared,Ow as esH,Fw as esW1,Dw as esW2,$v as escalc,_d as etaSquared,Cg as extractAIC,Cb as factanal,So as fisherTest,Ff as flignerTest,gi as friedmanTest,Ph as gTest,_0 as gamesHowellTest,F0 as glhtDunnett,er as glm,gw as glmAnova,Rg as glmConfint,Sh as glmCooksDistance,Rh as glmHatvalues,Ah as glmPredict,xh as glmResiduals,Th as glmRstandard,ya as glmSummary,jh as goodmanKruskalGamma,Xv as grubbsTest,Td as hedgesG,D1 as icc,R1 as jonckheereTest,O1 as kappa2,F1 as kappamFleiss,Vs as kruskalTest,gm as ksTest,T0 as kwAllPairsDunnTest,S0 as kwManyOneDunnTest,Wh as lblTest,d1 as leveneTest,k1 as lillieTest,wr as lm,K0 as mantelhaenTest,k0 as manyOneUTest,Mw as mauchlyTest,e0 as mcnemarTest,xb as mcreg,Kr as modelMatrix,Df as moodTest,qv as numDEpi,ld as oddsRatio,zb as omega,Ld as omegaSquared,Hf as onewayTest,Yr as pAdjust,o0 as pageTest,gg as pairwisePropTest,pg as pairwiseTTest,dg as pairwiseWilcoxTest,$1 as partialR,I1 as pearsonTest,Bv as pgrubbs,_v as ph2simon,Fp as poissonTest,Bb as polychor,Gb as polyserial,Nh as powerAnovaTest,Tv as powerCT,Av as powerEpi,Rv as powerPaired2x2,vh as powerPropTest,bh as powerTTest,Eh as predict,ov as predictCrr,jv as predictRma,Ga as propTest,b1 as propTrendTest,Uw as pwr2p2nTest,Ww as pwr2pTest,Xw as pwrAnovaTest,Hw as pwrChisqTest,Qw as pwrF2Test,Bw as pwrNormTest,Yw as pwrPTest,Gw as pwrRTest,Kw as pwrT2nTest,jw as pwrTTest,Hv as qgrubbs,Of as quadeTest,Kv as regtest,dh as rma,s1 as rmst2,Z1 as roc,ib as rocTest,wh as setWarningHandler,q1 as sfTest,P0 as shapiroTest,Sv as ssizeCT,kv as ssizeEpi,Pg as step,Vg as stepAIC,zh as stuartMaxwellTest,Jg as survdiff,ar as survfit,rw as survfitCoxph,qh as survreg,Ih as survregPredict,_h as survregResiduals,jg as svyglm,jd as tTOST,vn as tTest,I0 as tamhaneT2Test,ev as timepoints,ud as tost,Kd as tsumTOST,L0 as tukeyTest,_f as varTest,kh as vcovCL,xa as vcovHC,Ao as wilcoxTest,Eg as zTest};