@numero-uno-jpn/rstats 1.3.0 → 1.4.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 (56) hide show
  1. package/CHANGELOG.md +47 -0
  2. package/README.ja.md +7 -7
  3. package/README.md +7 -7
  4. package/dist/chunks/{chunk-HBRSUHSU.js → chunk-27VFB3SA.js} +2 -2
  5. package/dist/chunks/{chunk-Z74ZOTUN.js → chunk-56GFP4UU.js} +2 -2
  6. package/dist/chunks/{chunk-JJRZ2TTF.js → chunk-5SW3L6J3.js} +2 -2
  7. package/dist/chunks/{chunk-TZTVDVP4.js → chunk-6FZAJQ4H.js} +2 -2
  8. package/dist/chunks/{chunk-I5NWGQOQ.js → chunk-BQJFFGYP.js} +2 -2
  9. package/dist/chunks/{chunk-2VZW4XPK.js → chunk-F6Z3GLWA.js} +2 -2
  10. package/dist/chunks/{chunk-LA4H6ZSC.js → chunk-GBSMRVUY.js} +2 -2
  11. package/dist/chunks/{chunk-P7ZHH2SF.js → chunk-GEF2YRBV.js} +2 -2
  12. package/dist/chunks/{chunk-6X2ZMPF5.js → chunk-GIER5WMB.js} +2 -2
  13. package/dist/chunks/{chunk-AJZV2SVY.js → chunk-GLGOV3A5.js} +2 -2
  14. package/dist/chunks/{chunk-6TZVS6WC.js → chunk-H72MW7YP.js} +2 -2
  15. package/dist/chunks/{chunk-UJYRU37Z.js → chunk-HWB7XRX2.js} +2 -2
  16. package/dist/chunks/chunk-J72MLFUG.js +2 -0
  17. package/dist/chunks/{chunk-PDHBRDTF.js → chunk-PZ66PUWC.js} +1 -1
  18. package/dist/chunks/{chunk-FFNHG5SB.js → chunk-RUZATICF.js} +1 -1
  19. package/dist/chunks/{chunk-UMDXXT3M.js → chunk-RY7CCWVU.js} +1 -1
  20. package/dist/chunks/{chunk-UOAFEB64.js → chunk-S2YCFUCA.js} +2 -2
  21. package/dist/chunks/chunk-SVS54D6Z.js +2 -0
  22. package/dist/chunks/{chunk-LNUNKFIH.js → chunk-TEHMTQNB.js} +2 -2
  23. package/dist/chunks/{chunk-IJV43Y7Q.js → chunk-UMMCVF53.js} +2 -2
  24. package/dist/chunks/{chunk-BNTBZ5W3.js → chunk-UNP4HBPP.js} +2 -2
  25. package/dist/chunks/{chunk-LMNU4NHZ.js → chunk-V2KVTAVV.js} +2 -2
  26. package/dist/chunks/{chunk-EBRTF7RM.js → chunk-WWJGCBWH.js} +1 -1
  27. package/dist/chunks/chunk-X7K6EF7S.js +2 -0
  28. package/dist/chunks/{chunk-GUM57VVM.js → chunk-YBOSCQF2.js} +1 -1
  29. package/dist/chunks/{chunk-IOGQEMDE.js → chunk-ZQSOMEIM.js} +2 -2
  30. package/dist/chunks/{chunk-RCGW2DCK.js → chunk-ZRFZ5UIB.js} +2 -2
  31. package/dist/descriptive.js +2 -2
  32. package/dist/distributions.js +2 -2
  33. package/dist/entries/tests.d.ts +4 -0
  34. package/dist/entries/tests.js +4 -0
  35. package/dist/linalg.js +2 -2
  36. package/dist/mixed.js +2 -2
  37. package/dist/models/competingRisks.d.ts +188 -0
  38. package/dist/models/competingRisks.js +673 -0
  39. package/dist/models/coxZph.d.ts +67 -0
  40. package/dist/models/coxZph.js +479 -0
  41. package/dist/models/coxph.d.ts +61 -21
  42. package/dist/models/coxph.js +305 -65
  43. package/dist/models/rmst2.d.ts +126 -0
  44. package/dist/models/rmst2.js +365 -0
  45. package/dist/models/survival.d.ts +65 -24
  46. package/dist/models/survival.js +407 -72
  47. package/dist/models/survreg.d.ts +160 -0
  48. package/dist/models/survreg.js +809 -0
  49. package/dist/numerics.js +2 -2
  50. package/dist/rstats.js +16 -16
  51. package/dist/special.js +2 -2
  52. package/dist/tests.js +13 -13
  53. package/llms.txt +8 -0
  54. package/package.json +1 -1
  55. package/dist/chunks/chunk-4OOTEXZU.js +0 -2
  56. package/dist/chunks/chunk-MJXETGT2.js +0 -2
package/dist/tests.js CHANGED
@@ -1,19 +1,19 @@
1
- /*! rStats.js 1.3.0 | MIT License | includes code ported from SciPy, Boost.Math, statsmodels, LAPACK and others: see THIRD_PARTY_NOTICES.md at https://github.com/numero-uno-jpn/rStats.js */
2
- import{B as Po,C as Vo,D as $o,b as hn,c as zr,d as en,f as Vt,k as ir,l as vc,p as Nc,q as cr,r as yc,v as Do,w as Mc,x as Kr,z as Sc}from"./chunks/chunk-RCGW2DCK.js";import{a as zo,b as Tn,c as Yr,d as _c,e as Lc,f as Zs,g as Me,h as jt,i as tn,j as ct,k as Dc,l as Bu,n as Go,o as Xr,p as Yo}from"./chunks/chunk-IJV43Y7Q.js";import"./chunks/chunk-6TZVS6WC.js";import"./chunks/chunk-4OOTEXZU.js";import{a as Oc,c as Cc,d as Fc,e as zn,g as Qr}from"./chunks/chunk-BNTBZ5W3.js";import{b as Qs}from"./chunks/chunk-EBRTF7RM.js";import{b as Ec,c as Rc,d as xc,e as Tc,f as Ac,i as Ur}from"./chunks/chunk-TZTVDVP4.js";import{a as Je,b as fr,c as mr}from"./chunks/chunk-UOAFEB64.js";import{a as bc,d as Fo}from"./chunks/chunk-UJYRU37Z.js";import{a as Bo,b as Ko,c as Uo,d as Wn,e as pr,f as dr,g as zc,h as Wc}from"./chunks/chunk-LNUNKFIH.js";import{A as pn,g as qo,h as Io,j as _o,k as sr,p as We,q as Kt,s as jn,t as yn,v as Mn,z as Ut}from"./chunks/chunk-LMNU4NHZ.js";import{C as $c,E as jc,H as Et,I as Bc,c as Pc,h as Vc,j as et,k as Js,l as Wo,m as ur,n as hr,t as Ho,y as Mt}from"./chunks/chunk-JJRZ2TTF.js";import{a as lr,b as qc,g as jo}from"./chunks/chunk-6X2ZMPF5.js";import{B as ar,C as on,i as Ge,l as ko,x as Xs,y as $e,z as Ke}from"./chunks/chunk-HBRSUHSU.js";import{a as Ht,c as Ic}from"./chunks/chunk-AJZV2SVY.js";import{a as Wu,c as W,d as ke,e as Ue,f as Hr}from"./chunks/chunk-GUM57VVM.js";import{a as Gr,b as dn,c as kc,d as dt,e as it}from"./chunks/chunk-UMDXXT3M.js";import{a as Be,b as sn,c as $t}from"./chunks/chunk-FFNHG5SB.js";import"./chunks/chunk-2VZW4XPK.js";import{b as cc,c as lc}from"./chunks/chunk-LA4H6ZSC.js";import{A as sc,B as ac,D as ic,M as fc,q as _t,r as Y,s as Z}from"./chunks/chunk-P7ZHH2SF.js";import{A as pc,C as dc,D as ht,E as Lo,F as gc,G as Oo,H as wc,I as Ne,J as Ye,K as Jt,L as pt,M as xn,N as wt,O as Co,P as se,Q as Ys,b as kt,c as rn,d as H,e as rc,f as he,g as ce,h as st,i as Pe,j as Ve,k as oc,l as V,m as Nn,p as Wr,q as Br,r as mc,s as uc,t as hc,u as L,v as ye,w as En,x as Ce,y as _e,z as Rn}from"./chunks/chunk-PDHBRDTF.js";var Kc=["alternative","mu","paired","varEqual","confLevel"];function nn(e,t,n={}){if(typeof e!="string"&&Je(t)&&V(t,Kc,"tTest"),V(n,Kc,"tTest"),typeof e=="string")return Ku(e,t,n);let r;t!==void 0&&!ArrayBuffer.isView(t)&&!Array.isArray(t)?n=t:r=t;let o=Me(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(jt(i),a&&r===void 0)throw new TypeError("y is required when paired is true");let l,f,m;if(a){let[S,k]=xn(Array.from(e),Array.from(r));({d:l,k:m}=Lo(S,k))}else{let S=se(r===void 0?[pt(Array.from(e))]:[pt(Array.from(e)),pt(Array.from(r))]);m=S.k,l=S.groups[0],f=S.groups[1]}let u=L(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 M=Ce(l),R=E>1?_e(l):NaN;if(f===void 0){if(p=E-1,g=Math.sqrt(R/E),g===0||g<10*Number.EPSILON*Math.abs(M))throw new Error("data are essentially constant");h=(M-u)/g,N=a?"Paired t-test":"One Sample t-test",d=M,v=a?"mean difference":"mean of x",b=a?"mean difference":"mean"}else{let S=f.length;if(S<1||!c&&S<2)throw new Error("not enough 'y' observations");if(c&&E+S<3)throw new Error("not enough observations");let k=Ce(f),_=S>1?_e(f):NaN;if(N=`${c?"":"Welch"} Two Sample t-test`,d=[M,k],v=["mean of x","mean of y"],b="difference in means",c){p=E+S-2;let I=0;E>1&&(I+=(E-1)*R),S>1&&(I+=(S-1)*_),I/=p,g=Math.sqrt(I*(1/E+1/S))}else{let I=R/E,P=_/S;g=Math.sqrt(Math.sqrt(I)**2+Math.sqrt(P)**2),p=Oo([I,P],[E-1,S-1])}if(g===0||g<10*Number.EPSILON*Math.max(Math.abs(M),Math.abs(k)))throw new Error("data are essentially constant");h=(M-k-u)/g}let y=typeof d=="number"?d:d[0]-d[1],x=(S,k)=>{let _=u+(h+k*S)*g;return Number.isFinite(_)&&Math.max(Math.abs(u),Math.abs(h*g))<=16*(Math.abs(y)+S*g)?_:y+k*S*g},T,A;if(o==="less")T=$e(h,p,!0,!1),A=[-1/0,x(Ke(i,p,!0,!1),1)];else if(o==="greater")T=$e(h,p,!1,!1),A=[x(Ke(i,p,!0,!1),-1),1/0];else{T=2*$e(-Math.abs(h),p,!0,!1);let S=1-i,k=S/2<1e-8?Ke(S/2,p,!1,!1):Ke(1-S/2,p,!0,!1);A=[x(k,-1),x(k,1)]}return A=[L(A[0],m),L(A[1],m)],d=typeof d=="number"?L(d,m):d.map(S=>L(S,m)),g=L(g,m),{method:N,alternative:o,statistic:h,statisticName:"t",parameter:p,parameterName:"df",pValue:T,confInt:A,confLevel:i,estimate:d,estimateNames:v,nullValue:s,nullValueName:b,stdErr:g,stdError:g,warnings:[]}}function Ku(e,t,n){if(n.paired!==void 0)throw new Error("cannot use 'paired' in formula method");let r=zo(e,t,!0);if(r.kind==="one")return nn(r.x,n);if(r.kind==="paired")return nn(r.x,r.y,{...n,paired:!0});let[o,s]=r.levels;return{...nn(r.x,r.y,n),estimateNames:[`mean in group ${o}`,`mean in group ${s}`],nullValueName:`difference in means between group ${o} and group ${s}`}}var Uu=new Set(["n","delta","sd","sigLevel","power","type","alternative","strict","tol"]),Hu=["n","p1","p2","sigLevel","power","alternative","strict","tol"],Gu=["groups","n","betweenVar","withinVar","sigLevel","power"],Hc=Number.EPSILON**.25;function ae(e,t){if(e===void 0)return t[0];let n=t.find(o=>o===e);if(n!==void 0)return n;let r=t.filter(o=>o.startsWith(e));if(e.length===0||r.length!==1)throw new RangeError(`'arg' should be one of ${t.map(o=>`\u201C${o}\u201D`).join(", ")}`);return r[0]}var re=e=>e==null;function zt(e,t,n){if(!re(e)&&!t(e))throw new RangeError(n)}var Zr=e=>e>=0&&e<=1,Xo=e=>e>0&&e<1/0;function Gt(e,t){if(e.filter(re).length!==1)throw new RangeError(t)}function be(e,t,n,r={}){return Fc(e,t,n,r).root}var Bn=(e,t,n,r)=>ar(e,t,{ncp:n,lowerTail:r});function ea(e,t){if(e!=null&&!(typeof e=="number"&&e>=0&&e<=1))throw new RangeError(t)}var ta="'sig.level' must be numeric in [0, 1]";function Yu(e={}){V(e,Uu,"powerTTest");let{n:t,delta:n,power:r}=e,o=e.sd===void 0?1:e.sd,s=e.sigLevel===void 0?.05:e.sigLevel;Gt([t,n,o,r,s],"exactly one of 'n', 'delta', 'sd', 'power', and 'sig.level' must be NULL"),ea(s,ta),zt(r,Zr,"'power' must be a probability"),zt(o,Xo,"'sd' must be a positive finite number"),zt(t,g=>g>=2,"number of observations in each group must be at least 2"),zt(n,g=>!Number.isNaN(g),"'delta' must not be NaN"),zt(e.tol,Xo,"'tol' must be a positive finite number");let a=ae(e.type,["two.sample","one.sample","paired"]),c=ae(e.alternative,["two.sided","one.sided"]),i=e.strict??!1,l=e.tol??Hc,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=on(N/m,b,{lowerTail:!1}),w=Math.sqrt(g/f)*d/v;Number.isFinite(w)||(w=Math.sqrt(g/f)*(d/v));let M=Bn(E,b,w,!1);return i&&m===2?M+Bn(-E,b,w,!0):M};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=be(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=be(v=>u(t,n,v,s)-g,d*1e-7,d*1e7,{tol:l,extendInt:n>0?"downX":"upX"})}else re(n)?n=be(d=>u(t,d,o,s)-g,o*1e-7,o*1e7,{tol:l,extendInt:"upX"}):s=be(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 Xu(e={}){V(e,Hu,"powerPropTest");let{n:t,p1:n,p2:r,power:o}=e,s=e.sigLevel===void 0?.05:e.sigLevel;Gt([t,n,r,o,s],"exactly one of 'n', 'p1', 'p2', 'power', and 'sig.level' must be NULL"),ea(s,ta),zt(o,Zr,"'power' must be a probability"),zt(n,Zr,"'p1' must be a probability"),zt(r,Zr,"'p2' must be a probability"),zt(t,u=>u>0,"number of observations in each group must be positive"),zt(e.tol,Xo,"'tol' must be a positive finite number");let a=ae(e.alternative,["two.sided","one.sided"]),c=e.strict??!1,i=e.tol??Hc,l=a==="two.sided"?2:1,f=(u,h,p,g)=>{let d=Z(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),M=Math.sqrt(N+b);return Y((Math.sqrt(u)*w-d*Math.sqrt(2*E))/M)+Y((Math.sqrt(u)*w+d*Math.sqrt(2*E))/M,{lowerTail:!1})}return Y((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=be(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=be(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=be(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=be(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 W("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 Uc=1e16,Qo=(e,t)=>e>Uc&&e<1/0&&t>Uc;function Zo(e,t,n,r){let o=Z(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 Y((o*a-s)/c,{lowerTail:!1})}function Qu(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 L(L(e,-s)*L(t,-a)*L(n,-c)/L(r,-i),s+a+c-i)}function Zu(e={}){V(e,Gu,"powerAnovaTest");let{groups:t,n,betweenVar:r,withinVar:o,power:s}=e,a=e.sigLevel===void 0?.05:e.sigLevel;if(Gt([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");ea(a,ta),zt(s,Zr,"'power' must be a probability"),zt(r,i=>i>=0&&i<1/0,"'betweenVar' must be a nonnegative finite number"),zt(o,Xo,"'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=Qu(i-1,l,f,m));let p=i-1,g=(l-1)*i;return h===1/0?1:Qo(p,g)?Zo(p,g,h,u):jn(yn(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=be(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=be(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=be(l=>c(t,n,r,l,a)-i,r*1e-7,r*1e7)}else re(r)?r=be(l=>c(t,n,l,o,a)-i,o*1e-7,o*1e7):a=be(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 Ju=["correct","p","rescaleP"],eh=["confLevel"],th=["alternative","rowScores","colScores"];function Lt(e){let t=[0,0];for(let n of e)t=H(t,[n,0]);return t}function ra(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 oa=e=>e.length>0&&typeof e[0]!="number",sa=(e,t)=>Number.isNaN(e)?NaN:Ge(e,t/2,2,!1,!1);function Gc(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 Jr(e,t){return e.length>0&&Array.isArray(e[0])?e.map((n,r)=>n.map((o,s)=>Gc(o,t[r][s]))):e.map((n,r)=>Gc(n,t[r]))}var nh=["none","williams","yates"],Xc="Log likelihood ratio (G-test)";function rh(e,t={}){V(t,Ju,"gTest");let n=ae(t.correct,nh);if(typeof e!="object"||e===null||typeof e.length!="number")throw new TypeError("'x' must be an array of counts or of rows");if(oa(e)){let o=ra(e);return Yc(o.flat()),o.length===1?W("gTest",na(o[0],n,t)):o[0].length===1?W("gTest",na(o.map(s=>s[0]),n,t)):W("gTest",oh(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 Yc(r),W("gTest",na(r,n,t))}function Yc(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 Qc(e,t){return e<1/0?0:Math.ceil(Math.log2(t))+1}function Zc(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=H(n,ac(o,s))}return n[0]+n[1]}function oh(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=Lt(e.flat()),s=Qc(o[0],n*r),a=s===0?e:e.map(b=>b.map(E=>L(E,-s))),c=s===0?o:Lt(a.flat()),i=a.map(b=>Lt(b)),l=a[0].map((b,E)=>Lt(a.map(w=>w[E]))),f=i.map(b=>l.map(E=>ce(b,Pe(E,c)))),m=a;if(t==="yates"){let[[b,E],[w,M]]=a,R=b*M-E*w>0?-.5:.5,y=L(.5,-s),x=R<0?-y:y;m=[[b+x,E-x],[w-x,M+x]]}let u=Zc(m.flat(),f.flat()),h=Number.isNaN(u)?["NaNs produced"]:[],p=1;if(t==="williams"){if(i.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 b=c[0]+c[1],E=i.reduce((M,R)=>M+1/(R[0]+R[1]),0),w=l.reduce((M,R)=>M+1/(R[0]+R[1]),0);p=1+L((b*E-1)*(b*w-1)/(6*b*(n-1)*(r-1)),-s)}let g=L(2*u,s)/p,d=(n-1)*(r-1),v=f.map(b=>b.map(E=>L(E[0]+E[1],s)));return{method:`${Xc} 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:sa(g,d),observed:s===0?m:m.map(b=>b.map(E=>L(E,s))),expected:v,ftResiduals:Jr(e,v),warnings:h}}function na(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=Lt(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=Lt(e),i=Qc(c[0],r),l=i===0?e:e.map(b=>L(b,-i)),f=i===0?c:Lt(l),m=s.map(b=>o?Pe(st(f,b),a):st(f,b)),u=m.map(b=>b[0]+b[1]),h=l;if(t==="yates"){let b=L(.5,-i),E=l[0]-u[0];E>L(.25,-i)?h=[l[0]-b,l[1]+b]:-E>L(.25,-i)&&(h=[l[0]+b,l[1]-b])}let p=Zc(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*L(f[0]+f[1],i)):1,v=L(2*p,i)/d,N=u.map(b=>L(b,i));return{method:`${Xc} goodness of fit test`,statistic:v,statisticName:"G",parameter:r-1,parameterName:"X-squared df",pValue:sa(v,r-1),observed:i===0?h:h.map(b=>L(b,i)),expected:N,ftResiduals:Jr(e,N),warnings:g}}function sh(e){if(typeof e!="object"||e===null||!oa(e))throw new RangeError("'x' must be square with at least two rows and columns");let t=ra(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 ah(e){let t=sh(e),n=Lt(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(M=>L(M,-r))),s=o.length,a=o.map(w=>Lt(w)),c=o.map((w,M)=>Lt(o.map(R=>R[M]))),i=o.map((w,M)=>o[M].some((R,y)=>y!==M&&R>0)||o.some((R,y)=>y!==M&&R[M]>0)).flatMap((w,M)=>w?[M]:[]),l=i.length;if(l<2)throw new RangeError("Too many equal marginals, cannot compute");let f=i.map((w,M)=>M),m=w=>f[w]===w?w:f[w]=m(f[w]);for(let w=0;w<l;w++)for(let M=w+1;M<l;M++)o[i[w]][i[M]]+o[i[M]][i[w]]>0&&(f[m(w)]=m(M));if(i.some((w,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=i.slice(0,u).map(w=>{let M=H(a[w],[-c[w][0],-c[w][1]]);return M[0]+M[1]}),p=i.slice(0,u).map(w=>i.slice(0,u).map(M=>{if(w!==M)return-(o[w][M]+o[M][w]);let R=0;for(let y=0;y<s;y++)y!==w&&(R+=o[w][y]+o[y][w]);return R})),g=p.map(w=>w.map(()=>0)),d=[];for(let w=0;w<u;w++){let M=p[w][w];for(let R=0;R<w;R++)M-=g[w][R]*g[w][R]*d[R];d.push(M);for(let R=w+1;R<u;R++){let y=p[R][w];for(let x=0;x<w;x++)y-=g[R][x]*g[w][x]*d[x];g[R][w]=y/M}}let v=0,N=[];for(let w=0;w<u;w++){let M=h[w];for(let R=0;R<w;R++)M-=g[w][R]*N[R];N.push(M),v+=M*M/d[w]}let b=Lt(i.flatMap(w=>i.map(M=>t[w][M]))),E=l-1;return v=L(v,r),W("stuartMaxwellTest",{method:"Stuart-Maxwell test",statistic:v,statisticName:"chi-squared",parameter:E,parameterName:"df",pValue:sa(v,E),N:b[0]+b[1],warnings:[]})}function ih(e,t={}){V(t,eh,"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=Jc(e),o=Lt(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(M=>L(M,-s))),{pc:c,pd:i}=ch(a),l=[0,0],f=[0,0];a.forEach((w,M)=>w.forEach((R,y)=>{l=H(l,st(c[M][y],R)),f=H(f,st(i[M][y],R))})),l=[l[0]/2,l[1]/2],f=[f[0]/2,f[1]/2];let m=H(l,f),u=Pe(he(l,f),m)[0],h=ce(m,m),p=a.map((w,M)=>w.map((R,y)=>2*Pe(he(ce(f,c[M][y]),ce(l,i[M][y])),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,M)=>w.forEach((R,y)=>{let x=L(p[M][y],-d);v+=R*x*x}));let N=L(Math.sqrt(v),d-s/2),b=(1-n)/2,E=b<1e-8?Z(b,{lowerTail:!1}):Z(1-b);return W("goodmanKruskalGamma",{estimate:u,confInt:[Math.max(-1,u-E*N),Math.min(1,u+E*N)],confLevel:n,ase:N,warnings:[]})}function Jc(e){if(typeof e!="object"||e===null||!oa(e))throw new TypeError("'table' must be an array of rows");let t=ra(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 ch(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=H(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=H(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)=>H(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)=>H(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)=>H(d,l[g][v]))),h=f.map((p,g)=>p.map((d,v)=>H(d,m[g][v])));return{pc:u,pd:h}}function lh(e,t={}){V(t,th,"lblTest");let n=Me(t.alternative),r=Jc(e),o=r.length,s=r[0].length,a=(x,T,A)=>{if(x===void 0)return Array.from({length:T},(k,_)=>_+1);let S=Array.from(x);if(S.length!==T)throw new RangeError(`'${A}' must have one score for each ${A==="rowScores"?"row":"column"}`);if(S.some(k=>typeof k!="number"||!Number.isFinite(k)))throw new RangeError(`'${A}' must be finite numbers`);return S},c=a(t.rowScores,o,"rowScores"),i=a(t.colScores,s,"colScores"),f=Lt(r.flat())[0]>2**1e3?2*Math.ceil((Math.log2(Math.max(...r.flat()))-990)/2):0,m=f===0?r:r.map(x=>x.map(T=>L(T,-f))),u=m.map(x=>Lt(x)).map(x=>x[0]+x[1]),h=m[0].map((x,T)=>Lt(m.map(A=>A[T]))).map(x=>x[0]+x[1]),p=u.reduce((x,T)=>x+T,0);if(!(f>0||p>1))throw new RangeError("the table must have a total above 1");let g=u.reduce((x,T,A)=>x+T*c[A],0)/p,d=h.reduce((x,T,A)=>x+T*i[A],0)/p,v=c.map(x=>x-g),N=i.map(x=>x-d),b=u.reduce((x,T,A)=>x+T*v[A]*v[A],0),E=h.reduce((x,T,A)=>x+T*N[A]*N[A],0),w=0;if(m.forEach((x,T)=>x.forEach((A,S)=>w+=A*v[T]*N[S])),!(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 M=w/Math.sqrt(b)/Math.sqrt(E),R=f===0?Math.sqrt(p-1)*M:L(Math.sqrt(p-L(1,-f))*M,f/2),y=n==="less"?Y(R):n==="greater"?Y(R,{lowerTail:!1}):2*Y(-Math.abs(R));return W("lblTest",{method:"Linear-by-Linear Association Test",alternative:n,statistic:R,statisticName:"Z",pValue:y,warnings:[]})}var el=["correct","p","rescaleP","simulatePValue","B","nMinus1"];function Yt(e){let t=[0,0],n=0;for(let r of e)t=H(t,[r,0]),n+=r;return Number.isFinite(n)?t[0]:n}function fh(e,t,n){return Je(t)&&V(t,el,"chisqTest"),V(n,el,"chisqTest"),Array.isArray(t)?W("chisqTest",mh(e,t,n??{})):W("chisqTest",uh(e,t??{}))}var rl="Chi-squared approximation may be incorrect";function mh(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(Ht),c=[...new Set(s)].sort(Ht);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])]++),{...cl(f,n),rowLevels:a,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?aa(r[0],t):o===1?aa(r.map(s=>s[0]),t):cl(r,t)}return aa(Array.from(e),t)}function ol(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 sl(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 al(e,t){return Number.isNaN(e)?NaN:Ge(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 aa(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=Yt(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=Yt(o),d=g<1/0?0:Math.ceil(Math.log2(n)),v=d===0?o:o.map(b=>L(b,-d)),N=d===0?g:Yt(v);o=v.map(b=>b/N)}else if(Math.abs(Yt(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=>L(g,-s)),c=s===0?r:Yt(a),i=o.map(g=>c*g),l=s===0?i:i.map(g=>L(g,s));il(l);let f=o.map((g,d)=>i[d]*(1-g)),m=L(Yt(a.map((g,d)=>wt(g-i[d],i[d]))),s),u="Chi-squared test for given probabilities",h=n-1,p;if(t.simulatePValue??!1){let g=ol(t);if(!Number.isInteger(r))throw new RangeError("the simulation needs counts summing to an integer");u+=Gr(g),h=NaN,p=sl(hh(r,o,l,g),m,g)}else p=al(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)=>L((g-i[d])/Math.sqrt(i[d]),s/2)),stdres:a.map((g,d)=>L((g-i[d])/Math.sqrt(f[d]),s/2)),ftResiduals:Jr(e,l),warnings:Number.isFinite(h)&&l.some(g=>g<5)?[rl]:[]}}function hh(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=Sc(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 ph(e,t,n,r){let o=e.length,s=t.length,a=e.reduce((f,m)=>f+m,0);if(a>Po)throw new RangeError("the simulation needs a total count of at most 16777216 (2^24)");let c=Vo(a),i=new Int32Array(o*s),l=new Float64Array(Math.trunc(r));for(let f=0;f<l.length;f++){$o(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 cl(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=Yt(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=>Yt(b)),l=e[0].map((b,E)=>Yt(e.map(w=>w[E]))),f=i.map(b=>l.map(E=>Co(b,E,c)));il(f.flat());let m=e.map((b,E)=>b.map((w,M)=>w-f[E][M])),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(M=>!Number.isInteger(M))))throw new RangeError("the simulation needs integer counts");let b=ol(t);u+=Gr(b),h=Yt(m.flatMap((w,M)=>w.map((R,y)=>wt(R,f[M][y])))),p=NaN;let E=ph(i.map(w=>Math.trunc(w)),l.map(w=>Math.trunc(w)),f,b);g=sl(E,h,b)}else if(v>0)u+=" with Yates' continuity correction",h=Yt(m.flatMap((b,E)=>b.map((w,M)=>wt(Math.abs(w)-v,f[E][M]))));else if(h=Yt(m.flatMap((b,E)=>b.map((w,M)=>wt(w,f[E][M])))),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??al(h,p),observed:e,expected:f,residuals:m.map((b,E)=>b.map((w,M)=>w/Math.sqrt(f[E][M]))),stdres:dh(e,c,i,l,f,m),ftResiduals:Jr(e,f),warnings:Number.isFinite(p)&&f.some(b=>b.some(E=>E<5))?[...N,rl]:N}}function dh(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=>tl(p)),c=e[0].map((p,g)=>tl(e.map(d=>d[g]))),i=wh(a),l=nl(a),f=nl(c),m=l.map(p=>Pe(p,i)),u=c.map(p=>Pe(p,i)),h=f.map(p=>Pe(p,i));return e.map((p,g)=>p.map((d,v)=>{let N=ce(a[g],u[v]),b=he([d,0],N),E=b[0]+b[1],w=ce(ce(N,m[g]),h[v]),M=w[0]+w[1];if(M>=gh||E===0)return E/Math.sqrt(M);let R=N[0]+N[1],y=m[g][0]+m[g][1],x=h[v][0]+h[v][1];return E/Math.sqrt(R)/Math.sqrt(y)/Math.sqrt(x)}))}var gh=2**-1022;function tl(e){let t=[0,0];for(let n of e)t=H(t,[n,0]);return t}function wh(e){let t=[0,0];for(let n of e)t=H(t,n);return t}function nl(e){let t=e.length,n=new Array(t+1);n[t]=[0,0];for(let s=t-1;s>=0;s--)n[s]=H(e[s],n[s+1]);let r=[],o=[0,0];for(let s=0;s<t;s++)r.push(H(o,n[s+1])),o=H(o,e[s]);return r}var bh=Math.pow(Number.EPSILON,.25);function Rt(e,t,n,r={}){let o={tol:r.tol??bh,maxiter:r.maxIter??1e3};return r.fLower!==void 0&&(o.fLower=r.fLower),r.fUpper!==void 0&&(o.fUpper=r.fUpper),zn(e,t,n,o).root}function An(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?Rt(n,r,s,{tol:2**-50,fLower:o,fUpper:a}):Rt(n,s,r,{tol:2**-50,fLower:a,fUpper:o});return Math.exp(c)}var vh=["alternative","confInt","confLevel","or","simulatePValue","B","midp"];function no(e,t={}){V(t,vh,"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(lc));if(s.flat().some(f=>f>2147483647))throw new RangeError("'x' has entries too large to be integer");let a=[],c=Nh(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 W("fisherTest",{...Rh(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 W("fisherTest",{method:i+Gr(f),alternative:l,pValue:yh(s,f),warnings:a})}return W("fisherTest",{method:i,alternative:l,pValue:xh(s),warnings:a})}function Nh(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: ${kc(i)}`:null}function yh(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=H(l,[-Fo(d),0]);let f=l[0]/(1+64*Number.EPSILON),m=s.reduce((d,v)=>d+v,0);if(m>Po)throw new RangeError("the simulation needs a total count of at most 16777216 (2^24)");let u=Vo(m),h=new Int32Array(c*i),p=0,g=Math.trunc(t);for(let d=0;d<g;d++){$o(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 ll=1+1e-7,Mh=16,Eh=2**24;function eo(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=H(r,[e[o],0]));return r[0]}var gr=class{m;n;k;lo;hi;logCentral;constructor(t,n,r){this.m=t,this.n=n,this.k=r,this.lo=Math.max(0,r-n),this.hi=Math.min(r,t),this.logCentral=new Float64Array(this.hi-this.lo+1);let o=0,s=1;for(let a=this.lo;a<=this.hi;a++){let c=a-this.lo;c%Mh===0||!(s>0&&s<1/0)?(o=en(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=eo(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=H(r,[(this.lo+o)*n[o],0]));return r[0]}tail(t,n,r){if(n===1)return r?Vt(t-1,this.m,this.n,this.k,!1,!1):Vt(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?eo(o,s,o.length-1):eo(o,0,s)}midTail(t,n,r){if(n===1){let a=.5*en(t,this.m,this.n,this.k,!1);return(r?Vt(t,this.m,this.n,this.k,!1,!1):Vt(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?eo(o,s+1,o.length-1):eo(o,0,s-1))+.5*o[s]}};function ca(e,t,n){let r=e.density(n),o=r[t-e.lo]*ll,s=[0,0];for(let a of r)a<=o&&(s=H(s,[a,0]));return s[0]}function fl(e,t,n,r){let o=Math.max(0,r-n),s=Math.min(r,t),a=g=>en(g,t,n,r,!0),c=a(e)+Math.log(ll),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:Vt(m,t,n,r,!0,!1),p=u>s?0:Vt(u-1,t,n,r,!1,!1);return h+p}function to(e,t){return An(e,t)}function Rh(e,t,n){let r=Me(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>Eh)throw new RangeError(`the exact distribution of the 2 x 2 table has ${m} values, more than 2^24`);let u=new gr(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=ca(u,f,o);let v;if(f===h)v=0;else if(f===p)v=1/0;else{let M=u.mean(1);v=M===f?1:to(R=>u.mean(R)-f,M>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=M=>{if(f===p)return 1/0;if(g){if(f===h&&M>=.5)return 0;let y=u.midTail(f,1,!1);return y===M?1:to(x=>u.midTail(f,x,!1)-M,y<M)}let R=u.tail(f,1,!1);return R===M?1:to(y=>u.tail(f,y,!1)-M,R<M)},E=M=>{if(f===h)return 0;if(g){if(f===p&&M>=.5)return 1/0;let y=u.midTail(f,1,!0);return y===M?1:to(x=>u.midTail(f,x,!0)-M,y>M)}let R=u.tail(f,1,!0);return R===M?1:to(y=>u.tail(f,y,!0)-M,R>M)},w;if(r==="less")w=[0,b(1-s)];else if(r==="greater")w=[E(1-s),1/0];else{let M=(1-s)/2;w=[E(M),b(M)]}return{...N,confInt:w,confLevel:s}}var Jo=[[0,0]];function an(e){for(let t=Jo.length;t<=e;t++)Jo.push(H(Jo[t-1],oc([t,0])));return Jo[e]}function es(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 ia=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 xh(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,M)=>w+M[E],0)).sort((b,E)=>E-b),a=s.length,c=o.reduce((b,E)=>b+E,0),i=rc(an(c));for(let b of o)i=H(i,an(b));for(let b of s)i=H(i,an(b));let l=[0,0];for(let b of t.flat())l=he(l,an(b));let f=H(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(","),M=p[b].get(w);if(M!==void 0)return M;let R=H(i,an(E.reduce((k,_)=>k+_,0)));for(let k of E)R=he(R,an(k));for(let k=b;k<a;k++)R=he(R,an(s[k]));let y={R:E,KZ:R,max:0,min:0,edges:[],pastHi:[],pastLo:[],count:[],index:new Map};if(p[b].set(w,y),b===a-1){let k=[0,0];for(let _ of E)k=he(k,an(_));return y.max=y.min=k[0],y.edges.push({to:y,hi:k[0],lo:k[1],mult:1}),y}let x=new Map,T=new Array(r+1).fill(0);for(let k=r-1;k>=0;k--)T[k]=T[k+1]+E[k];let A=new Array(r).fill(0),S=(k,_,I)=>{if(k===r-1){A[k]=_;let F=he(I,an(_)),C=E.map((B,Q)=>B-A[Q]).sort((B,Q)=>B-Q),D=g(b+1,C),O=`${C.join(",")}|${F[0]}`,z=x.get(O);z===void 0?x.set(O,{to:D,hi:F[0],lo:F[1],mult:1}):z.mult++;return}let P=Math.max(0,_-T[k+1]),q=Math.min(E[k],_);for(let F=P;F<=q;F++)A[k]=F,S(k+1,_-F,he(I,an(F)))};if(S(0,s[b],[0,0]),y.edges=[...x.values()],y.max=Be(y.edges.map(k=>k.hi+k.to.max)),y.min=$t(y.edges.map(k=>k.hi+k.to.min)),b===a-2){let k=y.edges.map(D=>{let O=D.to.edges[0];return{v:H([D.hi,D.lo],[O.hi,O.lo]),mult:D.mult}});k.sort((D,O)=>D.v[0]-O.v[0]||D.v[1]-O.v[1]);let _=k.length,I=k[_-1].v[0],P=new Float64Array(_),q=new Float64Array(_),F=new Float64Array(_),C=[0,0];k.forEach(({v:D,mult:O},z)=>{P[z]=D[0],q[z]=D[1];let B=he(D,[I,0]);C=H(C,[O*Math.exp(B[0])*(1+B[1]),0]),F[z]=C[0]}),y.dist={hi:P,lo:q,cum:F,vmax:I,kv:H(i,[I,0])}}return y},d=g(0,[...o].sort((b,E)=>b-E)),v=new ia,N=(b,E,w,M)=>{if(E+b.max<=u-m){let[S,k]=es(b.KZ[0],b.KZ[1],E,w);return v.add(S,k,M),!0}if(E+b.min>u+m)return!0;let R=b.dist;if(R===void 0)return!1;let[y,x]=es(u,h,-E,-w),T=0,A=R.hi.length;for(;T<A;){let S=T+A>>>1,k=R.hi[S];k<y||k===y&&R.lo[S]<=x?T=S+1:A=S}if(T>0){let[S,k]=es(R.kv[0],R.kv[1],E,w);v.add(S,k,M*R.cum[T-1])}return!0};N(d,0,0,1)||(d.pastHi.push(0),d.pastLo.push(0),d.count.push(1));for(let b=0;b<a-2;b++)for(let E of p[b].values()){let{pastHi:w,pastLo:M,count:R}=E;if(R.length!==0){for(let{to:y,hi:x,lo:T,mult:A}of E.edges)for(let S=0;S<R.length;S++){let[k,_]=es(w[S],M[S],x,T),I=R[S]*A;if(N(y,k,_,I))continue;let P=y.index.get(k);P===void 0?(y.index.set(k,y.count.length),y.pastHi.push(k),y.pastLo.push(_),y.count.push(I)):y.count[P]+=I}E.pastHi=E.pastLo=E.count=[],E.index.clear()}}return Math.min(1,v.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 fa(e){return[...e].sort((t,n)=>t-n).join(",")}function la(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 ma(e){let[,t]=ts(e),n=0,r=0;for(let o of t)r+=n+=o;return{steps:r,size:n+1}}function ua(e){let[t,n]=ts(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 la(t,0,o)}function ha(e,t){let[,n]=ts(e),r=n.length,o=Math.min(t,r-t),s=ml(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 ml(e){let t=[0];for(let n of e)t.push(t[t.length-1]+n);return t}function pa(e,t){let[n,r]=ts(e),o=r.length,s=Math.min(t,o-t),a=ml(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],M=a[g-1]+p,R=Math.max(E,M);for(let y=w;y>=R;y--)d[y-E]=b*d[y-E]+N*v[y-M];for(let y=R-1;y>=E;y--)d[y-E]=b*d[y-E]}}let i=c[s],l=t*(t+1)/2;if(s===t)return la(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 la(n,(a[o]-u)/n-l,m)}function Sn(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 Kn(e,t){let n=Sn(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 ul=["alternative","mu","paired","exact","correct","confInt","confLevel","tolRoot"];function ro(e,t,n={}){if(typeof e!="string"&&Je(t)&&V(t,ul,"wilcoxTest"),V(n,ul,"wilcoxTest"),typeof e=="string"){if(n.paired!==void 0)throw new Error("cannot use 'paired' in formula method");let g=zo(e,t,!0);return g.kind==="one"?ro(g.x,n):ro(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=Me(n.alternative),s=n.mu??0,a=n.paired??!1,c=n.exact??null,i=Ah(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]=xn(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 Ue};return W("wilcoxTest",h===void 0?_h(u,c,a,p):$h(u,h,c,p))}function Ah(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 ns(e,t,n){return n?t==="two.sided"?Math.sign(e)*.5:t==="greater"?.5:-.5:0}var dl=10*Number.EPSILON;function gl(e,t,n=0){let r=i=>Math.min(1,Math.max(i,0)),o=()=>n===0?Y(e):r(Y(e)-n*_t(e)),s=()=>n===0?Y(e,{lowerTail:!1}):r(Y(e,{lowerTail:!1})+n*_t(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 Sh(e){let t=e*e;return[e*(t-3),e*(t*(t-10)+15),e*(t*(t*(t-21)+105)-105)]}function kh(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 wl(e,o,s,n)}function qh(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 wl(e,a,c,r)}function wl(e,t,n,r){let[o,s,a]=Sh(e),c=t*o;return r>=2&&(c+=n*s),r>=3&&(c+=t*t/2*a),c}function wa(e){let t=e.length;if(t===0)return NaN;let n=Math.floor(t/2);return t%2===1?e[n]:Ce([e[n-1],e[n]])}var ba=(e,t)=>e-t,da=5e9,Ih=40;function ga(e){return new RangeError(`wilcoxTest: the exact conditional ${e} would take too long or too much memory: use exact: false`)}function rs(e,t){if(e.size>16777216||e.steps>da)throw ga(t);return e.steps}function bl(e){let t=e.length,n=Ye(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 Jt(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 _h(e,t,n,r){let{alternative:o,mu:s}=r,a=e.map(w=>w-s);a.some((w,M)=>!Number.isFinite(w)&&Number.isFinite(e[M]))&&(a=e.map(w=>w/2-s/2));let i=a.some(w=>w===0),l=t??a.length<50,f=(w,M,R)=>({method:w,alternative:o,statistic:M,statisticName:"V",pValue:R,nullValue:s,nullValueName:n?"location shift":"location",warnings:r.warnings.list});if(l){let w=Ye(a.map(Math.abs)),M=Jt(w).length>0,R=0;for(let k=0;k<a.length;k++)a[k]>0&&(R+=w[k]);let y="Wilcoxon signed rank exact test";if(!M&&!i){let k=a.length,_=k*(k+1)/2,I;if(o==="less")I=ir(R,k,!0,!1);else if(o==="greater")I=ir(R-1,k,!1,!1);else{let q=R>_/2?ir(R-1,k,!1,!1):ir(R,k,!0,!1);I=Math.min(1,2*q)}let P=f(y,R,I);return r.confInt?{...P,...Lh(e,r)}:P}let x=w.filter((k,_)=>a[_]!==0);rs(ma(x),"distribution");let T=ua(x),A;if(o==="less")A=Sn(T,R);else if(o==="greater")A=Kn(T,R-.25);else{let k=0;for(let _ of x)k+=_/2;A=Math.min(1,2*(R>k?Kn(T,R-.25):Sn(T,R)))}let S=f(y,R,A);return r.confInt?{...S,...Ch(e,r)}:S}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]=bl(m),v=Jt(Ye(m.map(Math.abs))).length>0||i?Math.min(r.correct,0):r.correct,N=(p-ns(p,o,v>=0))/g,b=gl(N,o,v>=1?kh(N,u,v):0),E=f(`Wilcoxon signed rank test${r.correct>=0?" with continuity correction":""}`,h,b);return r.confInt?{...E,...Fh(e,r)}:E}function va(e,t,n){return e==="less"?[-1/0,n]:e==="greater"?[t,1/0]:[t,n]}function hl(e,t,n){let r={confInt:va(e,NaN,NaN),confLevel:0,estimate:t};return n!==void 0&&(r.estimateNames=n),r}function vl(e){if(!(e>0&&e<1/0))throw new RangeError("tolRoot must be a positive finite number")}function Nl(e){let t=e.length,n=[];for(let r=0;r<t;r++)for(let o=r;o<t;o++){let s=sn(e[r],e[o]);Number.isNaN(s)||n.push(s)}return n.sort(ba)}function yl(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+dl&&l++;let f=wa(e);if(l===0)return{confInt:[-1/0,1/0],confLevel:1,estimate:f,estimateNames:o};let m=t(l-1);return{confInt:va(s,e[l-1],e[c-l]),confLevel:1-(s==="two.sided"?2*m:m),estimate:f,estimateNames:o}}function Lh(e,t){let n=e.length;return yl(Nl(e),r=>ir(r,n,!0,!1),r=>vc(r,n,!0,!1),t,"(pseudo)median")}function Ml(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 El(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)+dl,g=f!=="less",d=f!=="greater",v=I=>sn(e[I-1],e[I]),N=Math.min(u-1,Math.max(1,Math.ceil(a.mean+Z(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),M=[new Map,new Map];{let I=new Map,P=0,q=Ih*c;for(let[F,C]of[[0,E],[1,w]])if(!(F===0?!g:!d))for(let D=0;D<=N;D++){if(P+=q,P>da)throw ga("interval");let O;try{O=t(C(D))}catch{break}if(M[F].set(D,[O.stat,O.key]),I.has(O.key)){I.delete(O.key),I.set(O.key,!0);continue}if(P+=r(O.scores),P>da)throw ga("interval");I.set(O.key,!0),I.size>b&&I.delete(I.keys().next().value)}}let R=new Map,y=(I,P)=>{let q=I===0?E(P):w(P),F=M[I].get(P),C;if(F===void 0){let B=t(q);F=[B.stat,B.key],C=B.scores}let[D,O]=F,z=R.get(O);return z!==void 0?R.delete(O):(z=n(C??t(q).scores),R.size>=b&&R.delete(R.keys().next().value)),R.set(O,z),[D,z]},x=I=>{let[P,q]=y(0,I);return o(q,P)},T=I=>{let[P,q]=y(1,I);return s(q,P)},A=-1/0,S=0;if(g){let I=x(0);if(!(I>p)){let P=I,q=1;for(;q<=u-1;q++){let F=x(q);if(F>p)break;P=F}A=e[q-1],S=P}}let k=1/0,_=0;if(d){let I=T(0);if(!(I>p)){let P=I,q=u-1;for(;q>=1;q--){let F=T(u-q);if(F>p)break;P=F}k=e[q],_=P}}return{confInt:[A,k],confLevel:1-(S+_),estimate:wa(e),estimateNames:l}}function Oh(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 Ch(e,t){let n=e.length,r=s=>{let a=Oh(e,s),{ranks:c,key:i}=Ml(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 El(Nl(e),r,ua,s=>rs(ma(s),"interval"),(s,a)=>Kn(s,a-.25),Sn,{mean:o/2,sd:Math.sqrt(n*(n+1)*(2*n+1)/24)},n,t,"(pseudo)median")}function Fh(e,t){let{alternative:n,confLevel:r,tolRoot:o}=t,s=t.correct>=0,a=e.length;if(a===0)return hl(n,NaN);let c=$t(e),i=Be(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"),hl(n,sn(c,i),"midrange");let l=(b,E)=>{let[,w,M]=bl(e.map(R=>R-b).filter(R=>R!==0));return(w-ns(w,n,E))/M},f=l(c,s),m=l(i,s),u=b=>-Z(b),h=b=>n==="greater"?!(f<u(b)):n==="less"?!(m>Z(b/2)):!(f<u(b/2))&&!(m>Z(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"),vl(o);let d=b=>Rt(E=>l(E,s)-b,c,i,{tol:o,fLower:f-b,fUpper:m-b}),v;if(p>=1){let b=wa([...e].sort(ba));v=va(n,b,b)}else n==="greater"?v=[d(u(p)),1/0]:n==="less"?v=[-1/0,d(Z(p))]:v=[d(u(p/2)),d(Z(p/2))];let N=Rt(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 Rl(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 Jt(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 Dh(e,t){let[n,r,o]=Rl(Ye([...e,...t]),e.length);return[n,r,o]}function Ph(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 Vh(e,t,n){return xl(e,t,n).ranks}function xl(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=Ye([...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:fa(a)}}if(Number.isFinite(n)&&r.some((o,s)=>!Number.isFinite(o)&&Number.isFinite(e[s]))){let o=Ph(e.map(s=>s/2-n/2),t);return{ranks:o,key:fa(o)}}return Ml([...r,...t])}function $h(e,t,n,r){let{alternative:o,mu:s}=r,a=e.length,c=t.length,i=n??(a<50&&c<50),l=Vh(e,t,s),[f,m,u,h]=Rl(l,a),p,g;if(i)if(g="Wilcoxon rank sum exact test",!h)p=pl(()=>o==="less"?cr(f,a,c,!0,!1):o==="greater"?cr(f-1,a,c,!1,!1):Math.min(2*cr(f,a,c,!0,!1),2*cr(f-1,a,c,!1,!1),1));else{rs(ha(l,a),"distribution");let N=pa(l,a);o==="less"?p=Sn(N,f):o==="greater"?p=Kn(N,f-.25):p=Math.min(2*Sn(N,f),2*Kn(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-ns(m,o,N>=0))/u;p=b?1:gl(E,o,N>=1?qh(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?zh(e,t,r):pl(()=>jh(e,t,r)):Wh(e,t,r);return{...d,...v}}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 Tl(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(ba)}function jh(e,t,n){let r=e.length,o=t.length;return yl(Tl(e,t,!1),s=>cr(s,r,o,!0,!1),s=>yc(s,r,o,!0,!1),n,"difference in location")}function zh(e,t,n){let r=e.length,o=t.length,s=a=>{let{ranks:c,key:i}=xl(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 El(Tl(e,t,!0),s,a=>pa(a,r),a=>rs(ha(a,r),"interval"),(a,c)=>Kn(a,c-.25),Sn,{mean:r*o/2,sd:Math.sqrt(r*o*(r+o+1)/12)},r+o,n,"difference in location")}function Wh(e,t,n){let{alternative:r,confLevel:o,tolRoot:s}=n,a=n.correct>=0,c=$t(e)-Be(t),i=Be(e)-$t(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]=Dh(e.map(v=>v-h),t);return(g-ns(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:(vl(s),Rt(v=>l(v,p)-h,c,i,{tol:s,fLower:g,fUpper:d}))},m=1-o,u;return r==="greater"?u=[f(-Z(m),a),1/0]:r==="less"?u=[-1/0,f(Z(m),a)]:u=[f(-Z(m/2),a),f(Z(m/2),a)],{confInt:u,confLevel:o,estimate:f(0,!1),estimateNames:"difference in location"}}var Bh=["method","n"],Al=["holm","hochberg","hommel","bonferroni","BH","BY","fdr","none"];function Kh(e){if(Al.includes(e))return e;let t=Al.filter(n=>n.startsWith(e));if(e.length===0||t.length!==1)throw new RangeError(`invalid method "${e}"`);return t[0]}function wr(e,t={}){V(t,Bh,"pAdjust");let n=typeof t=="string"?{method:t}:t,r=Kh(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=kl(i,r,c),f=o.slice();return s.forEach((m,u)=>f[m]=l[u]),f}function os(e,t){let n=e.map((r,o)=>o);return n.sort((r,o)=>(t?e[o]-e[r]:e[r]-e[o])||r-o),n}function kl(e,t,n){let r=e.length,o=new Array(r);switch(t){case"none":return e.slice();case"bonferroni":return e.map(s=>Math.min(1,n*s));case"holm":{let s=os(e,!1),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=os(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=mc(),u=Math.floor(n);if(u<=1e6){for(let h=1;h<=u;h++)uc(m,1/h,u);s=hc(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=os(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?kl(e,"hochberg",n):Uh(e,n)}}var Sl=2**16;function Uh(e,t){if(!Number.isInteger(t))throw new RangeError("n must be an integer for the hommel method");if(t>Sl)throw new RangeError(`n must be at most ${Sl} for the hommel method (O(n^2) work)`);let n=e.length,r=[...e,...new Array(t-n).fill(1)],o=os(r,!1),s=o.map(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 ql=["p","alternative","confLevel"],Hh=1+1e-7;function Na(e,t,n){let r=e-1;return e===0||e-r===1&&kt(t,-r)[1]===0?pn(r,t,n,!1,!1):pn(e,t,n,!1,!1)+Ut(e,t,n,!1)}function Gh(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=Ut(e,t,n,!1)*Hh,s=i=>Ut(i,t,n,!1)<=o;if(e<r){let i=qo(s,Math.ceil(r),t),l=i>t?0:Na(i,t,n);return Math.min(1,pn(e,t,n,!0,!1)+l)}let a=Io(s,0,Math.floor(r)),c=a<0?0:pn(a,t,n,!0,!1);return Math.min(1,Na(e,t,n)+c)}function Yh(e,t,n={}){V(t,ql,"binomTest"),V(n,ql,"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),!tn(e[0])||!tn(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(!tn(e))throw new RangeError("'x' must be nonnegative and integer");if(r=Math.round(e),!(tn(t)&&Math.round(t)>=r&&t>0))throw new RangeError("'n' must be a positive integer >= 'x'");o=Math.round(t)}return ya(r,o,n)}function ya(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=Me(n.alternative),s=n.confLevel??.95;ct(s);let a;o==="less"?a=pn(e,t,r,!0,!1):o==="greater"?a=Na(e,t,r):a=Gh(e,t,r);let c=f=>e===0?0:Mn(f,e,t-e+1,!0,!1),i=f=>e===t?1:f<1e-8?Mn(f,e+1,t-e,!1,!1):Mn(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 Xh=["T","r","alternative","confLevel"],Qh=1+1e-7;function Zh(e,t){if(t===0)return e===0?1:0;if(e===t)return 1;let n=_o(e,t,!1)*Qh,r=a=>_o(a,t,!1)<=n,o=Math.floor(t);if(e<t){let a=qo(r,Math.max(e+1,o),1/0);return Math.min(1,sr(e,t,!0,!1)+Ma(a,t))}let s=Io(r,0,Math.min(e-1,o));return Math.min(1,sr(s,t,!0,!1)+Ma(e,t))}var oo=2**-1022;function Ma(e,t){return e-(e-1)===1?sr(e-1,t,!1,!1):sr(e,t,!1,!1)+_o(e,t,!1)}function Un(e){let t=e<9332636185032189e-317?600:0,n=Math.floor(Math.log2(e*2**t));return[e*2**t*2**-n,n-t]}function Jh(e,t,n,r){let o=n+t*r,s=e*t*n/o;if(Number.isFinite(s)&&Number.isFinite(o)&&o>=oo&&(s!==0||e===0||t===0))return s;let 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]=Un(e),[f,m]=Un(t),[u,h]=Un(n),[p,g]=Un(r),d=f*p,v=h-m-g,N=v>=0?u/(u+L(d,-v)):u/(L(u,v)+d);return L(i*f*N,l+m+Math.min(v,0))}function ep(e,t,n,r){let o=e/n,s=t/r,a=o/s;return t===0||Number.isFinite(o)&&Number.isFinite(s)&&s>=oo&&(o===0||o>=oo)?a:e===0?0:e/t*(r/n)}function tp(e,t={}){V(t,Xh,"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(tn))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=Me(t.alternative),l=t.confLevel??.95;if(o===2?ct(l):jt(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 M=c*b,R=M+E,y=Number.isFinite(R)&&R>=oo&&(M===0||M>=oo)?M/R:1/(1+E/b/c),x=ya(Math.round(v),Math.round(v)+Math.round(N),{p:y,alternative:i,confLevel:l}),T=E/b,A=S=>{if(S===0)return 0;let k=S/(1-S);if(Number.isFinite(T)||k===1/0)return k*T;let[_,I]=Un(k),[P,q]=Un(b),[F,C]=Un(E);return L(_*F/P,I+C-q)};return{method:"Comparison of Poisson rates",alternative:i,statistic:v,statisticName:"count1",parameter:Jh(w,c,b,E),parameterName:"expected count1",pValue:x.pValue,confInt:[A(x.confInt[0]),A(x.confInt[1])],confLevel:l,estimate:ep(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=sr(f,u,!0,!1):i==="greater"?h=Ma(f,u):h=Zh(f,u);let p=v=>f===0?0:ko(v,f,1,!0,!1),g=v=>v<1e-8?ko(v,f+1,1,!1,!1):ko(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 Il=["p","alternative","confLevel","correct"],np="Chi-squared approximation may be incorrect";function Ea(e,t,n={}){Je(t)&&V(t,Il,"propTest"),V(n,Il,"propTest");let r,o,s=n;if(typeof e!="number"&&e.length>0&&Array.isArray(e[0])){let A=e;if(A.some(S=>S.length!==2))throw new RangeError("'x' must have 2 columns");r=A.map(S=>S[0]),o=A.map(S=>S[0]+S[1]),t!==void 0&&typeof t=="object"&&!Array.isArray(t)&&(s=t)}else{if(t===void 0||typeof t=="object"&&!Array.isArray(t))throw new RangeError("'n' must be given");r=typeof e=="number"?[e]:Array.from(e),o=typeof t=="number"?[t]:Array.from(t)}if(r.length!==o.length)throw new RangeError("'x' and 'n' must have the same length");let a=r.length,c=[];for(let A=0;A<a;A++)!Number.isNaN(r[A])&&!Number.isNaN(o[A])&&c.push(A);let i=c.length;if(i<1)throw new RangeError("not enough data");if(r=c.map(A=>r[A]),o=c.map(A=>o[A]),o.some(A=>!(A>0)))throw new RangeError("elements of 'n' must be positive");if(o.some(A=>A===1/0))throw new RangeError("elements of 'n' must be finite");if(r.some(A=>!(A>=0)))throw new RangeError("elements of 'x' must be nonnegative");if(r.some((A,S)=>A>o[S]))throw new RangeError("elements of 'x' must not be greater than those of 'n'");let l;if(s.p!==void 0){let A=typeof s.p=="number"?[s.p]:Array.from(s.p);if(A.length!==a)throw new RangeError("'p' must have the same length as 'x' and 'n'");if(l=c.map(S=>A[S]),l.some(S=>!(S>0&&S<1)))throw new RangeError("elements of 'p' must be in (0,1)")}let f=l!==void 0;i===1&&l===void 0&&(l=[.5]);let m=s.confLevel??.95;ct(m);let u=Me(s.alternative);(i>2||i===2&&f)&&(u="two.sided");let h=r.map((A,S)=>A/o[S]),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 A=Math.abs(r[0]-o[0]*l[0]);A<=d&&([d,v]=[A,!0])}else if(d>0&&i===2&&!f){let A=Math.abs(p)/g;A<=d&&([d,v]=[A,!0])}let N=rp(r,o),b=o.some((A,S)=>{let k=l?.[S]??N;return A*k<5||A*(1-k)<5}),E=0;if(!v)for(let A=0;A<i;A++){let S=l?.[A]??N,k=o[A]*S,_=o[A]*(1-S);E+=wt(Math.abs(r[A]-k)-d,k),E+=wt(Math.abs(o[A]-r[A]-_)-d,_)}let w=l!==void 0?i:i-1,M;if(u==="two.sided")M=Ge(E,w/2,2,!1,!1);else{let A=Math.sign(i===1?h[0]-l[0]:p);M=Y(A*Math.sqrt(E),{lowerTail:u==="less"})}let R;if(i===1||i===2&&!f){let A=(1-m)/2,S=u==="two.sided"&&A<1e-8?Z(A,{lowerTail:!1}):Z(u==="two.sided"?(1+m)/2:m);if(i===1){let k=o[0],_=S*S,I=(D,O)=>(D+_/(2*k)+O*S*Math.sqrt(D*(1-D)/k+_/(4*k*k)))/(1+_/k),P=h[0]+d/k,q=h[0]-d/k,F=Math.min(1,Math.max(0,P>=1?1:I(P,1))),C=Math.min(1,Math.max(0,q<=0?0:I(q,-1)));R=u==="two.sided"?[C,F]:u==="greater"?[C,1]:[0,F]}else{let k=Math.sqrt(h.reduce((q,F,C)=>q+F*(1-F)/o[C],0)),_=S*k+d*g,I=Math.max(-1,p-_),P=Math.min(1,p+_);R=u==="two.sided"?[I,P]:u==="greater"?[I,1]:[-1,P]}}let y=i===1?"p":c.map(A=>`prop ${A+1}`),T={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:M,estimate:i===1?h[0]:h,estimateNames:y,warnings:b?[np]:[]};return l!==void 0&&(T.nullValue=i===1?l[0]:l,T.nullValueName=y),R!==void 0&&(T.confInt=R,T.confLevel=m),W("propTest",T)}function rp(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+L(s,-r),0)/t.reduce((o,s)=>o+L(s,-r),0)}var op=["method","confLevel","sides"],sp=["wilson","wald","wilsoncc","agresti-coull","jeffreys","clopper-pearson","midp"],ap=["two.sided","left","right"];function so(e,t){let n=ae(t,ap),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{...Ra(s,o),sides:n}}function Ra(e,t=1-e){let n=e/2<1e-8,r=n?Z(e/2,{lowerTail:!1}):Z(1-e/2),o=n?zr(e,1,{lowerTail:!1}):zr(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(!tn(e))throw new RangeError(`'${t}' must be a nonnegative integer`);return Math.round(e)+0}function gn(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 as(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 ss(e,t,n,r){return r?e/2<1e-8?Mn(e/2,t,n,!1,!1):Mn(1-e/2,t,n,!0,!1):Mn(e/2,t,n,!0,!1)}function _l(e,t,n,r){let o=n/2,s=r?f=>{let m=Math.exp(f);return .5*Ut(e,t,m,!1)+pn(e-1,t,m,!0,!1)-o}:f=>{let m=Math.exp(f);return .5*Ut(e,t,m,!1)+pn(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:Rt(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(Rt(f=>a*s(f),c,0,{tol:2**-60,fLower:i,fUpper:l}))}function xa(e,t,n={}){V(n,op,"binomCI");let r=ae(n.method,sp),o=so(n.confLevel,n.sides),[s,a]=gn(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]=as(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:ss(c,s+.5,a-s+.5,!1),u=s===a?1:ss(c,s+.5,a-s+.5,!0);break;case"clopper-pearson":m=s===0?0:ss(c,s,a-s+1,!1),u=s===a?1:ss(c,s+1,a-s,!0);break;case"midp":m=s===0?0:s===a?Math.exp(Math.log(c)/a):_l(s,a,c,!1),u=s===a?1:s===0?-Math.expm1(Math.log(c)/a):_l(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),W("binomCI",{estimate:l,confInt:[m,u],confLevel:n.confLevel??.95,method:r,sides:o.sides,warnings:[]})}function ao(e){return e===1/0?1:e/(1+e)}function io(e,t){return e===0?0:t>0?e*e/t:1/0}function ip(e,t,n,r){return e/t+n/r>1}function cp(e,t,n,r){return cs(e,t,n,r,0)}function Sa(e,t,n,r,o){if(ip(e,t,n,r)){let[p,g,d,v]=Sa(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=lp(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 M=u-w/g(u);if(M>v&&M<N||(M=v===0?N/2:N>2*v?Math.sqrt(v*N):v+(N-v)/2),!(M>v&&M<N))break;let R=Math.abs(M-u);if(u=M,R<=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 lp(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 ka(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]=Sa(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 fp(e,t,n,r,o,s,a){let c=he(he(Ve([e,0],t),Ve([n,0],r)),[o,0]),[i,l,f,m]=Sa(e,t,n,r,o),u=H(Ve(rn(i,l),t),Ve(rn(f,m),r));return s==="mn"&&(u=Ve(st(u,t+r),t+r-1)),he(ce(c,c),st(u,a))[0]}function cs(e,t,n,r,o){return he(he(Ve([e,0],t),Ve([n,0],r)),[o,0])[0]}function mp(e,t,n,r,o,s){return io(cs(e,t,n,r,o),ka(e,t,n,r,o,s))}var is=new Float64Array(1),Ta=new BigInt64Array(is.buffer);function Ll(e){return is[0]=Math.abs(e),e<0?-Ta[0]:Ta[0]}function kn(e){return Ta[0]=e<0n?-e:e,e<0n?-is[0]:is[0]}function Aa(e,t,n,r,o){let s=e(t);if(s===0||Number.isNaN(s))return t;let a=s<0===o,c=Ll(a?r:n),i=Ll(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(kn(f)),m===0)return kn(f);if(m<0!=l<0||f===c)break;if(i=f,l=m,u*=2n,u>1n<<40n)return kn(i)}if(m<0==l<0||Number.isNaN(m))return kn(i);for(;f-i>1n||i-f>1n;){let h=(i+f)/2n,p=e(kn(h));if(p===0)return kn(h);p<0==l<0?(i=h,l=p):(f=h,m=p)}return Math.abs(m)<Math.abs(l)?kn(f):kn(i)}function Cl(e,t,n,r,o,s){let a=cp(e,t,n,r);if(o===0)return[a,a];let c=ao(o),i=p=>(p===a?0:ao(mp(e,t,n,r,p,s)))-c,l=(p,g,d,v)=>{if(d===0)return p;if(v===0)return g;let N=Rt(i,p,g,{tol:1e-300,fLower:d,fUpper:v});return s==="wald"?N:Aa(b=>fp(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 Ol(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?Rt(e,r,a,{tol:2**-60,fLower:o,fUpper:c}):Rt(e,a,r,{tol:2**-60,fLower:c,fUpper:o});return Math.exp(i)}r=a,o=c,s*=2}}function up(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 hp(e,t,n,r,o){let[s,a]=up(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=M=>c===0?[M,0]:c===2?kt(1,-M):Ve(kt(1,-M),o),f=c===3?1/o:1,m=c===0?Math.min(1,1/o):1,u=c===2?o>1?Ve(kt(o,-1),o)[0]:0:c===3&&o<1?kt(1,-o)[0]:0,h=M=>{let R=l(M),y=st(R,o);return[y[0],he([1,0],y)[0],R[0],he([1,0],R)[0]]},p=e+n,g=c<2?1:-1,d=M=>{let[,R,y,x]=h(M),T=0,A=0;return p>0&&(T+=y>0?p/y:1/0,A-=p/(y*y)),e<t&&(T-=R>0?o*(t-e)/R:1/0,A-=o*(t-e)/R*(o/R)),n<r&&(T-=x>0?(r-n)/x:1/0,A-=(r-n)/(x*x)),[g*T,A*f]},v;if(!(m>u))v=u;else if(!(d(u)[0]>0))v=u;else if(!(d(m)[0]<0))v=m;else{let M=u,R=m;v=i,v>M&&v<R||(v=M+(R-M)/2);for(let y=0;y<2e3;y++){let[x,T]=d(v);if(x===0)break;x>0?M=v:R=v;let A=v-x/T;if(A>M&&A<R||(A=M<=0?R/2:R>2*M?Math.sqrt(M*R):M+(R-M)/2),!(A>M&&A<R))break;let S=Math.abs(A-v);if(v=A,S<=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 pp(e,t,n,r,o){let[s,a,c,i]=hp(e,t,n,r,o),l=(f,m,u,h)=>u>.5?m*h-(m-f):f-m*u;return io(l(e,t,s,a),t*s*a)+io(l(n,r,c,i),r*c*i)}function Fl(e,t,n,r,o){let s=e/t/(n/r);return Pl(s,o,a=>pp(e,t,n,r,a))}function dp(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 gp(e,t,n,r,o){let[s,a]=dp(e,t,n,r,o),c=e-t*s;return io(c,t*s*(1-s))+io(c,r*a*(1-a))}function Dl(e,t,n,r,o){let s=e*(r-n)/((t-e)*n);return Pl(s,o,a=>gp(e,t,n,r,a))}function Pl(e,t,n){if(t===0)return[e,e];let r=ao(t),o=l=>n(l)-t,s=l=>l>0&&l<e?Aa(o,l,Number.MIN_VALUE,e,!1):l,a=l=>l>e&&l<1/0?Aa(o,l,e,Number.MAX_VALUE,!0):l;if(e===0||e===1/0){let l=u=>ao(n(u))-r,f=l(1),m=f===0?1:An(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:ao(n(Math.exp(l))))-r;return[s(Ol(i,c,-1)),a(Ol(i,c,1))]}var wp=["method","confLevel","sides"],bp=["method","confLevel","sides"],vp=["method","confLevel"],Np=["ac","wald","score","mn","mee"];function qa(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]=as(e,t,a),[p,g]=as(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]=Cl(e,t,n,r,s.chisq,o);break}return[Math.min(f,m),Math.max(f,m)]}function yp(e,t,n,r,o={}){V(o,wp,"binomDiffCI");let s=ae(o.method,Np),a=so(o.confLevel,o.sides),[c,i]=gn(e,t,"x1","n1"),[l,f]=gn(n,r,"x2","n2"),m=c/i-l/f,[u,h]=qa(c,i,l,f,s,a);return a.sides==="left"?h=1:a.sides==="right"&&(u=-1),W("binomDiffCI",{estimate:m,confInt:[u,h],confLevel:o.confLevel??.95,method:s,sides:a.sides,warnings:[]})}var Mp=["katz.log","koopman"];function ls(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 Ep(e,t,n,r,o={}){V(o,bp,"binomRatioCI");let s=ae(o.method,Mp),a=so(o.confLevel,o.sides),[c,i]=gn(e,t,"x1","n1"),[l,f]=gn(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,ls(.5,i,l,f,m)[1]]:l===0?[h,p]=[ls(c,i,.5,f,m)[0],1/0]:c===i&&l===f?[h,p]=ls(i-.5,i,f-.5,f,m):[h,p]=ls(c,i,l,f,m):[h,p]=Fl(c,i,l,f,a.chisq),a.sides==="left"?p=1/0:a.sides==="right"&&(h=0),W("binomRatioCI",{estimate:u,confInt:[h,p],confLevel:o.confLevel??.95,method:s,sides:a.sides,warnings:[]})}var Rp=["midp","fisher","wald","score"],xp={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"},Tp=2**23,Ap=2**20;function Vl(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=H(r,[e[o],0]));return r[0]}function Sp(e,t={}){V(t,vp,"oddsRatio");let n=ae(t.method,Rp);if(e.length!==2||e.some(A=>A.length!==2))throw new TypeError("'table' must be a 2 x 2 table");let r=t.confLevel??.95,{alpha:o,kappa:s,chisq:a}=so(r,void 0),c=e.map((A,S)=>Array.from(A,(k,_)=>br(k,`table[${S}][${_}]`))),[[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>Tp)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=Vt(i-1,i+f,l+m,i+l,!0,!1)+.5*en(i,i+f,l+m,i+l,!1),v=Vt(i,i+f,l+m,i+l,!1,!1)+.5*en(i,i+f,l+m,i+l,!1),N=g<=Ap?ca(new gr(i+f,l+m,i+l),i,1):fl(i,i+f,l+m,i+l)}catch(A){throw A instanceof RangeError?new RangeError(`the exact p-values of the table are beyond reach: ${A.message}`):A}let b=2*Math.min(d,v),E=[i+l,f+m],w=[i+f,l+m],M=0,R=!1;for(let A=0;A<2;A++)for(let S=0;S<2;S++){let k=E[S]*w[A]/u;k<5&&(R=!0);let _=Math.abs(c[S][A]-k);M+=_*_/k}let y=Ge(M,.5,2,!1,!1),x,T;switch(n){case"wald":{x=i*m/(l*f);let A=s*Math.sqrt(1/i+1/l+1/f+1/m);T=[Math.exp(Math.log(x)-A),Math.exp(Math.log(x)+A)];break}case"fisher":{let A=ke(()=>no(c,{confLevel:r}));x=A.estimate,T=A.confInt;break}case"score":{x=i*m/(l*f),T=(i===0||m===0)&&(l===0||f===0)?[0,1/0]:Dl(i,i+l,f,f+m,a);break}case"midp":{let A=new gr(i+f,l+m,i+l),S=P=>{if(P===1)return[d,v];let q=A.density(P),F=i-h,C=.5*q[F];return[Vl(q,0,F-1)+C,Vl(q,F+1,q.length-1)+C]},k=(P,q)=>{let F=P(1);return F===0?1:An(P,q?F>0:F<0)};x=i===h?0:i===p?1/0:k(P=>S(P)[0]-.5,!1);let _=i===h?0:k(P=>S(P)[1]-o/2,!0),I=i===p?1/0:k(P=>S(P)[0]-o/2,!1);T=[_,I];break}}return W("oddsRatio",{estimate:x,confInt:T,confLevel:r,method:xp[n],pValue:{midp:b,fisher:N,chiSquare:y},warnings:R?["Chi-squared approximation may be incorrect"]:[]})}var kp=["epsilon","paired","varEqual","confLevel","alpha"],qp=["margin","method","alpha"];function $l(e,t){if(!(typeof e=="number"&&e>0&&e<.5))throw new RangeError(`'${t}' must be a single number in (0, 0.5)`)}function Ip(e,t,n={}){V(n,kp,"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)$l(n.alpha,"alpha"),o=1-n.alpha,s=n.alpha;else{if(o=n.confLevel??.95,ct(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=ke(()=>nn(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=$e((r-Math.abs(m))/l,f,!1,!1),h=s<1e-8?Ke(s,f,!1,!1):Ke(o,f,!0,!1),p=[m-h*l,m+h*l];return W("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 _p=["mn","mee","wald"];function Lp(e,t,n,r,o={}){V(o,qp,"binomDiffEquivalenceTest");let s=ae(o.method,_p),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;$l(c,"alpha");let[i,l]=gn(e,t,"x1","n1"),[f,m]=gn(n,r,"x2","n2"),u=i/l-f/m,h=E=>cs(i,l,f,m,E)/Math.sqrt(ka(i,l,f,m,E,s)),p=h(-a),g=h(a),d=[Y(p,{lowerTail:!1}),Y(g)],v=Math.max(d[0],d[1]),N=1-2*c,b=qa(i,l,f,m,s,Ra(2*c,N));return W("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 xt=new WeakMap;function ms(e){let t=(n,r)=>n.weights===void 0?Array.from(r):r.filter((o,s)=>n.weights[s]!==0);return se(e.flatMap(n=>[t(n,n.fittedValues),t(n,n.residuals)])).k}function us(e,t){return e.map(n=>{if(n.rss>=Op&&n.rss<1/0)return L(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=L(o,-t);r+=a*c*c}),r})}var Op=2**-1022;function _a(e,t){return e.y.flatMap((n,r)=>(e.weights?.[r]??1)===0?[]:[[e.weights?.[r]??1,L(n,-t)]])}function hs(e){let t=Math.floor(Math.log2(e.reduce((n,[r])=>Math.max(n,r),0)));return t-(t%2+2)%2}function Hn(e,t,n=0){let r=a=>e.reduce((c,[i,l])=>c+L(i,-a)*l*l,0),o=n,s=r(o);return Number.isFinite(s)||(o=hs(e),s=r(o)),!(L(t,-o)>=1e-24*s)}function jl(e){let t=ms([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+=L(e.effects[s],-t)**2}return{terms:r,rdf:e.dfResidual,rss:us([e],t)[0],k:t}}var zl=(e,t,n)=>We(e,t,n,!1,!1),La="essentially perfect fit for \u20181\u2019: summary may be unreliable";function Oa(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 Ca=new WeakMap;function Cp(e){let t=jl(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*L(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 Ia(e){let t=Cp(e),{names:n,rdf:r,k:o,perfect:s}=t,{rows:a,rss:c}=t;if(r>0){let g=_a(e,o);Hn(g,c)&&(c=0,a=a.map(d=>Hn(g,d.ss)?{...d,ss:0}:d))}let i=c/r,l=g=>L(g,2*o),f=a.map(({df:g,ss:d})=>{let v=d/g/i;return[g,l(d),l(d/g),v,zl(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]),xt.set(h,{colNames:["Df","Sum Sq"],rowNames:u,table:p}),h}function Fp(e){let t=e.map(f=>f.dfResidual),n=ms(e),r=us(e,n),o=f=>L(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*L(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||!Uo(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:zl(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 fs(...e){let t=[];for(let n of e)for(let r of n??[])t.includes(r)||t.push(r);return t}function Fa(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=Ia(e),i={...c,warnings:fs(e.warnings,c.warnings)},l=xt.get(c);return l!==void 0&&xt.set(i,l),W("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?Ia(e):Fp(o);return a.warnings=fs(...n.map(c=>c.warnings),s,a.warnings),W("anova",a,...n)}function Da(e){if(typeof e=="object"&&e!==null&&e.multistratum===!0){let a=e,c=fs(a.warnings,Ca.get(a)),i={strata:a.strata.filter(f=>f.stratum!=="Error: (Intercept)").map(f=>({...f})),warnings:c},l=xt.get(a);return Array.isArray(l)&&xt.set(i,l.filter((f,m)=>a.strata[m].stratum!=="Error: (Intercept)")),W("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=Ia(t),r=jl(t),o=Oa(t.fittedValues.map(a=>L(a,-r.k)),r.rss,r.rdf),s=fs(t.warnings,n.warnings,o?[La]:[]);if(t.dfResidual>0){let a={...n,warnings:s},c=xt.get(n);return c!==void 0&&xt.set(a,c),W("aovSummary",a,e)}return W("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 Dp=["pooledSd","mu","paired","ci","alternative","adjust"],Pp=["pooledSd","mu","paired","ci","alternative"],Vp=["generalized","partial","ci","alternative"],$p=["partial","ci","alternative"],jp=["partial","ci","alternative"];function $a(e){if(e!==null&&!(e>0&&e<1))throw new RangeError("ci must be a single numeric value between (0, 1)")}var zp="Missing values detected. NAs dropped.",Wp=`'y' is numeric but has only 2 unique values.
3
- If this is a grouping variable, convert it to a factor.`,Ul={tol:0,maxiter:5e3};function Gn(e){return e===1/0?1:Math.exp(fc(.5,(e-1)/2)-.5*Math.log(e/2))}function Wl(e,t,n){let r=e-1<e+1?1:Math.abs(e)*Number.EPSILON;return zn(o=>ar(e,t,{ncp:o})-n,e-r,e+r,{...Ul,extendInt:"downX"}).root}function Hl(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;$a(c);let i=ae(n.alternative,["two.sided","less","greater"]),l=Array.from(e),f=t===void 0?void 0:Array.from(t),m=[];f!==void 0&&new Set(pt(f)).size===2&&m.push(Wp),(l.some(Number.isNaN)||(f?.some(Number.isNaN)??!1))&&m.push(zp);let u=A=>A===1/0||A===-1/0;if(l.some(u)||(f?.some(u)??!1))throw new RangeError("the observations must be finite (NaN is dropped as missing)");let h,p,g,d,v,N;if(f===void 0||a){let A;if(f===void 0)({groups:[A],k:N}=se([pt(l)]));else{let[k,_]=xn(l,f);({d:A,k:N}=Lo(k,_))}let S=A.length;if(S<2)throw new RangeError(f===void 0?"not enough observations: at least 2 are needed":"not enough complete pairs: at least 2 are needed");h=Ce(A),p=Math.sqrt(_e(A)),g=p/Math.sqrt(S),d=1/S,v=S-1}else{let A=ht([pt(l),pt(f)]);N=A.k;let[S,k]=A.n;if(S<2||k<2)throw new RangeError("not enough observations: at least 2 are needed in each group");h=A.mean[0]-A.mean[1];let _=Math.sqrt(A.variance[0]),I=Math.sqrt(A.variance[1]);o?(p=Math.sqrt(((S-1)*_**2+(k-1)*I**2)/(S+k-2)),g=p*Math.sqrt(1/S+1/k),d=1/S+1/k,v=S+k-2):(p=Math.sqrt((_**2+I**2)/2),d=2*(k*_**2+S*I**2)/(S*k*(_**2+I**2)),g=Math.sqrt(Math.sqrt(_**2/S)**2+Math.sqrt(I**2/k)**2),v=Oo([_**2/S,I**2/k],[S-1,k-1]))}let b=r?Gn(v):1,E=L(s,-N),w=(h-E)/p*b,M=r?"Hedges_g":"Cohens_d";if(c===null)return{name:M,value:w,warnings:m};let R=i==="two.sided"?c:2*c-1,y=(h-E)/g,x=Wl(y,v,(1+R)/2)*Math.sqrt(d)*b,T=Wl(y,v,(1-R)/2)*Math.sqrt(d)*b;return i==="less"&&(x=-1/0),i==="greater"&&(T=1/0),{name:M,value:w,ci:c,ciLow:x,ciHigh:T,warnings:m}}function Bp(e,t,n={}){return V(n,Dp,"cohensD"),W("cohensD",Hl(e,t,n,n.adjust??!1))}function Kp(e,t,n={}){return V(n,Pp,"hedgesG"),W("hedgesG",Hl(e,t,n,!0))}var Up=e=>Array.isArray(e.effects),Pa=e=>e.multistratum===!0,Gl=e=>Array.isArray(e.strata),Yl=e=>{let t=e;return typeof t.univariate=="object"&&typeof t.anova=="object"},Va=e=>({test:t=>t.split(":").includes(e)});function Hp(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 Gp(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=>Va(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 Xl(e,t){let n=Array.isArray(e)?e:[],r=n.filter(s=>!t.some(a=>Va(s).test(a.name)));if(r.length>0)throw new RangeError(`Observed variable not in data: ${r.join(", ")}`);let o=s=>n.some(a=>Va(a).test(s));return{isObserved:o,ssObserved:t.reduce((s,a)=>o(a.name)?s+a.ss:s,0)}}function Bl(e,t,n,r){let o=a=>jn(e,t,n,{ncp:a})-r,s=o(0);return Number.isNaN(s)?NaN:s>0?e===1/0?1/0:zn(o,0,Math.max(10,e*t*4),{...Ul,extendInt:"downX"}).root:0}function Ql(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=Bl(c,t,n,(1+s)/2),l=Bl(c,t,n,(1-s)/2);c<=yn((1-s)/2,t,n)&&(l=0),c<=yn((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 Yp(e){let t=se([e.map(n=>n.ss)]).k;if(t!==0)for(let n of e)n.ss=L(n.ss,-t)}var Kl="needs a table of anova() or Anova(), a fit from lm() or aov() with a formula, its aovSummary(), or a result of aovEz()";function ja(e,t,n){if(typeof e!="object"||e===null)throw new TypeError(Kl);if(Pa(e)||Gl(e)||Yl(e))return Xp(e,t,n);let r=Up(e)?ke(()=>Fa(e)):e;if(!Array.isArray(r.colNames)||!Array.isArray(r.table))throw new TypeError(Kl);let o=xt.get(r),s=o!==void 0&&!Array.isArray(o)?o:r,a=n.ci===void 0?.95:n.ci;$a(a);let c=ae(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((x,T)=>({name:x,df:s.table[T][i],ss:s.table[T][l]})).filter(x=>x.name!=="(Intercept)");Yp(f);let m=f.find(x=>x.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(x=>x!==m),h=m.df,p=m.ss,g=p/h,d=f.reduce((x,T)=>x+T.ss,0),v=f.reduce((x,T)=>x+T.df,0)+1,N=t==="Eta2"?n.generalized??!1:!1,E=u.length===1?"plain":N!==!1?"generalized":n.partial??!0?"partial":"plain",{isObserved:w,ssObserved:M}=Xl(N,u),R=x=>{let{ss:T,df:A}=x;if(t==="Eta2")return E==="partial"?T/(T+p):E==="plain"?T/d:T/(T+p+M-(w(x.name)?T:0));if(t==="Omega2"){let k=E==="partial"?(T-A*g)/(T+(v-A)*g):(T-A*g)/(d+g);return Math.max(0,k)}let S=E==="partial"?(T-A*g)/(T+p):(T-A*g)/d;return Math.max(0,S)},y=`${t}${E==="plain"?"":`_${E}`}`;return{colNames:a===null?[y]:[y,"CI","CI_low","CI_high"],rowNames:u.map(x=>x.name),table:u.map(x=>a===null?[R(x)]:Ql(R(x),x.df,h,a,c)),warnings:Array.isArray(r.warnings)?[...r.warnings]:[]}}function Xp(e,t,n){let r=n.ci===void 0?.95:n.ci;$a(r);let o=ae(n.alternative??"greater",["two.sided","less","greater"]),s=Yl(e),a,c,i;if(s){let w=xt.get(e),M=Gp(w!==void 0&&!Array.isArray(w)?{...e,univariate:w}:e);({strata:a,subject:c}=M),i=M.order.map(R=>({term:R,stratum:M.strata.find(y=>y.terms.includes(R))}))}else{let w=Pa(e)?ke(()=>Da(e)):e,M=xt.get(w);({strata:a,subject:c}=Hp(Array.isArray(M)?{...w,strata:M.map((R,y)=>({...w.strata[y],...R}))}:w)),i=a.flatMap(R=>R.terms.map(y=>({term:y,stratum:R})))}let l=se([[...a.flatMap(w=>w.terms.map(M=>M.ss)),...a.map(w=>w.sse)]]).k;for(let w of a){w.sse=L(w.sse,-l);for(let M of w.terms)M.ss=L(M.ss,-l)}let f=a.flatMap(w=>w.terms),m=a.reduce((w,M)=>w+M.sse,0),u=f.reduce((w,M)=>w+M.ss,0)+m,h=c.sse/c.dfe,p=t==="Eta2"?n.generalized??!1:!1,g=p!==!1?"generalized":n.partial??!0?"partial":"plain",{isObserved:d,ssObserved:v}=Xl(p,f),N=(w,M)=>{let{ss:R,df:y}=w,x=M.sse/M.dfe;if(t==="Eta2")return g==="partial"?R/(R+M.sse):g==="plain"?R/u:R/(R+m+v-(d(w.name)?R:0));if(t==="Omega2"){let A=M===c?0:1,S=g==="partial"?(R-y*x)/(R+A*M.sse+c.sse+h):(R-y*x)/(u+h);return Math.max(0,S)}let T=g==="partial"?(R-y*x)/(R+M.sse):(R-y*x)/u;return Math.max(0,T)},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:M})=>{let R=N(w,M);return r===null?[R]:Ql(R,w.df,M.dfe,r,o)}),warnings:[]};return s||(E.groups=i.map(w=>w.stratum.name),E.warnings=[...Pa(e)||Gl(e)?e.warnings:[]]),E}function Qp(e,t={}){return V(t,Vp,"etaSquared"),W("etaSquared",ja(e,"Eta2",t),e)}function Zp(e,t={}){return V(t,$p,"omegaSquared"),W("omegaSquared",ja(e,"Omega2",t),e)}function Jp(e,t={}){return V(t,jp,"epsilonSquared"),W("epsilonSquared",ja(e,"Epsilon2",t),e)}var ed=new Set(["eqb","lowEqbound","highEqbound","hypothesis","paired","varEqual","alpha","mu","eqboundType","biasCorrection","rmCorrection","glass","smdCi"]),td=["m1","sd1","n1","m2","sd2","n2","r12"],nd=new Set(["eqb","lowEqbound","highEqbound","hypothesis","paired","varEqual","alpha","mu","eqboundType","biasCorrection","rmCorrection","glass","smdCi"]),rd="Equivalence interval does not include zero.",od="Warning: setting bound type to SMD produces biased results!",sd="MET test may have higher error rates than a nil two-tailed test. Consider wider equivalence bounds.";function Zl(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 zn(s=>ar(e,t,{ncp:s,lowerTail:!r})-n,e-o,e+o,{tol:0,maxiter:5e3,extendInt:r?"upX":"downX"}).root}function ad(e,t){let{alpha:n,smdCi:r}=t,o=Math.abs(e.delta),s=(y,x,T)=>(1-(T-2)/(T*y*y))*x*x,a,c,i,l,f,m,u,h,p,g,d,v,N=t.biasCorrection;if(e.design==="two"){let{n1:y,n2:x,s1:T,s2:A}=e,S=T*T,k=A*A,_=2*y*x/(y+x);if(t.glass!==void 0){let I=t.glass==="glass1",[P,q,F,C]=I?[T,y,A,x]:[A,x,T,y];c=P,i=q-1,a=N?"Glass's delta(g)":"Glass's delta(d)",l=N?Gn(i):1,f=l*o/c,p=1/x+1/y;let D=F/P;m=Math.sqrt(D*D/(C-1)+1/(q-1)+f*f/(2*(q-1))),u=o/(P*Math.sqrt(1/q+D*D/C)),h=y+x-2,g=Math.sqrt(2/_)}else if(t.varEqual)c=Math.sqrt(((y-1)*S+(x-1)*k)/(y+x-2)),i=y+x-2,a=N?"Hedges's g":"Cohen's d",l=N?Gn(i):1,f=l*o/c,p=1/y+1/x,m=Math.sqrt(1/y+1/x+s(l,f,i)),u=o/(c*Math.sqrt(p)),h=i,g=Math.sqrt(2*(x*S+y*k)/(y*x*(S+k)));else{c=Math.sqrt((S+k)/2),i=(y-1)*(x-1)*(S+k)**2/((x-1)*S*S+(y-1)*k*k),a=N?"Hedges's g(av)":"Cohen's d(av)",l=N?Gn(i):1,f=l*o/c,p=2*(x*S+y*k)/(y*x*(S+k));let I=c**4;m=Math.sqrt(f*f*(S*S/(y-1)+k*k/(x-1))/(8*I)+(S/(y-1)+k/(x-1))/(c*c)),u=o/Math.sqrt(S/y+k/x),h=i,g=Math.sqrt(2/_)}d=i,v=(I,P)=>Math.sqrt(d/(d-2)*(2/_)*(1+I*I*_/2)-I*I/(P*P))}else if(e.design==="paired"){let{n:y,s1:x,s2:T,sdif:A,oneMinusR:S}=e,k=t.glass!==void 0?"":t.rmCorrection?"(rm)":"(z)";if(t.glass!==void 0?c=t.glass==="glass1"?x:T:c=t.rmCorrection?A/Math.sqrt(2*S):A,a=t.glass!==void 0?N?"Glass's delta(g)":"Glass's delta(d)":N?`Hedges's g${k}`:`Cohen's d${k}`,i=y-1,d=2*y-2,l=N?Gn(r==="goulet"?d:i):1,f=l*o/c,p=1/y,u=o*Math.sqrt(y)/c,h=i,g=Math.sqrt(2*S/y),v=(_,I)=>{let P=r==="goulet"?d:i,q=Math.sqrt(P/(P-2)*(2*S/y)*(1+_*_*y/(2*S))-_*_/(I*I));return t.rmCorrection?q:q*Math.sqrt(2*S)},t.glass!==void 0){let _=A/c;m=Math.sqrt(_*_/(y-1)+f*f/(2*(y-1)))}else!t.rmCorrection&&r==="nct"?m=Math.sqrt(1/y+s(l,f,i)):m=v(f,l)}else{let{n:y,s:x}=e;c=x,i=y-1,d=i,a=N?"Hedges's g":"Cohen's d",l=N?Gn(i):1,f=l*o/c,p=1/y,m=Math.sqrt(1/y+f*f/(2*y)),u=o*Math.sqrt(y)/x,h=i,g=1/Math.sqrt(y),v=(T,A)=>Math.sqrt(i/(i-2)*(1/y)*(1+T*T*y)-T*T/(A*A))}let b,E,w,M=i;if(r==="nct"){w=m,M=h;let y=Zl(u,h,n,!0),x=Zl(u,h,n,!1);b=y*Math.sqrt(p)*l,E=x*Math.sqrt(p)*l}else if(r==="goulet"){w=v(f,l),M=d;let y=f/g;b=on(n,d,{ncp:y})*g,E=on(n,d,{ncp:y,lowerTail:!1})*g}else{w=m;let y=r==="t"?Ke(n,i,!1,!1):Z(n,{lowerTail:!1});b=f-y*w,E=f+y*w}let R=e.delta<0;return{label:a,denom:c,estimate:R?-f:f,se:w,confInt:R?[-E,-b]:[b,E],df:M,J:l}}function Jl(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 ef(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=ae(e.hypothesis,["EQU","MET"]),n=ae(e.eqboundType,["raw","SMD"])==="SMD",r=ae(e.smdCi,["nct","goulet","t","z"]),o=e.glass===void 0?void 0:ae(e.glass,["glass1","glass2"]),s=e.alpha??.05;if(!(typeof s=="number"&&s>0&&s<.5))throw new RangeError("'alpha' must be a single number in (0, 0.5)");let 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 nf(e,t,n,r,o,s,a,c,i,l,f,m){let{alpha:u,hypothesis:h}=l,p=ad(c,l),g=L(p.denom,i),d=l.smdBounds?[f[0]*g,f[1]*g]:f,v=F=>{if(F===0)return[0,n];let C=Math.floor(Math.log2(Math.abs(F))),D=ye(F,-C);return l.smdBounds?[D*p.denom,C+i]:[D,C]},N=l.smdBounds?f:[f[0]/g,f[1]/g];l.smdBounds&&m.push(od),d[0]<=0&&d[1]>=0||m.push(rd);let b=F=>L(F/r,n-o),E=F=>{let[C,D]=v(F),O=Math.max(n,D);return ye((ye(t,n-O)-ye(C,D-O))/r,O-o)},w=b(t),M=E(f[0]),R=E(f[1]),y=L(r,o),x=h==="EQU",T={t:w,se:y,df:s,pValue:2*$e(-Math.abs(w),s,!0,!1)},A={t:M,se:y,df:s,pValue:$e(M,s,!x,!1)},S={t:R,se:y,df:s,pValue:$e(R,s,x,!1)},k=x?Math.max(A.pValue,S.pValue):Math.min(A.pValue,S.pValue);!x&&d[0]<=0&&d[1]>=0&&k<=T.pValue&&m.push(sd);let _=Ke(u,s,!1,!1),I=Math.max(n,o),[P,q]=[L(t,n-I),L(r,o-I)];return W(e,{tTest:T,lower:A,upper:S,pValue:k,bounds:{raw:d,smd:N},raw:{estimate:L(t,n),se:y,confInt:[L(P-_*q,I),L(P+_*q,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 id(e,t,n={}){V(n,ed,"tTOST");let r=tf(n),o=ef(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");Jl(e,"x"),i||Jl(t,"y");let l=ke(()=>i?nn(e,{mu:c}):nn(e,t,{paired:s,varEqual:a,mu:c})),f=l.parameter,m,u,h,p;if(i){let g=se([pt(Array.from(e))]);u=g.k;let d=g.groups[0],v=_e(d);h=Ce(d)-L(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]=xn(Array.from(e),Array.from(t)),v=se([g,d]);u=v.k;let[N,b]=v.groups,E=N.map((T,A)=>T-b[A]),w=Math.sqrt(_e(N)),M=Math.sqrt(_e(b)),R=_e(E),y=Math.sqrt(R),x=(y-(w-M))*(y+(w-M))/(2*w*M);h=Ce(E)-L(c,-u),p=Math.sqrt(R/E.length),m={design:"paired",n:E.length,delta:Ce(N)-Ce(b)-L(c,-u),s1:w,s2:M,sdif:y,oneMinusR:x}}else{let g=se([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,_e(d),_e(v)];h=Ce(d)-Ce(v)-L(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 nf("tTOST",h,u,p,u,f,l.method,m,u,r,o,[])}function cd(e,t={}){V(e,td,"tsumTOST"),V(t,nd,"tsumTOST");let n=tf(t),r=ef(t),{m1:o,sd1:s,n1:a,m2:c,sd2:i,n2:l,r12:f}=e,m=(I,P)=>{if(typeof I!="number"||!Number.isFinite(I))throw new RangeError(`'${P}' must be a finite number`);return I},u=(I,P)=>{let q=m(I,P);if(!(Number.isInteger(q)&&q>=2))throw new RangeError(`'${P}' must be an integer of at least 2`);return q},h=(I,P)=>{let q=m(I,P);if(!(q>0))throw new RangeError(`'${P}' must be positive`);return q};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}=se([[o,...p?[]:[c],n.mu]]),{groups:N,k:b}=se([[s,...p?[]:[i]]]),[E,w=NaN]=p?[d[0][0]]:[d[0][0],d[0][1]],M=d[0][d[0].length-1],[R,y=NaN]=N[0],x=I=>L(I,v-b),T,A,S,k,_;if(p)T=E-M,A=R/Math.sqrt(a),S=a-1,k="One Sample t-test",_={design:"one",n:a,delta:x(T),s:R};else if(n.paired){let I=m(f,"r12");if(!(I>-1&&I<1))throw new RangeError("'r12' must be in (-1, 1)");let P=a;a!==l&&(g.push("Unequal number of pairs; using smallest n"),P=Math.min(a,l));let q=Math.sqrt(R*R+y*y-2*I*R*y);if(!(q>0))throw new RangeError("the standard deviation of the differences is 0");T=E-w-M,A=q/Math.sqrt(P),S=P-1,k="Paired t-test",_={design:"paired",n:P,delta:x(T),s1:R,s2:y,sdif:q,oneMinusR:1-I}}else{let I=l,P=R*R,q=y*y;if(T=E-w-M,n.varEqual){let F=((a-1)*P+(I-1)*q)/(a+I-2);A=Math.sqrt(F*(1/a+1/I)),S=a+I-2,k=" Two Sample t-test"}else{let F=P/a,C=q/I;A=Math.sqrt(F+C),S=(F+C)**2/(F*F/(a-1)+C*C/(I-1)),k="Welch Two Sample t-test"}_={design:"two",n1:a,n2:I,delta:x(T),s1:R,s2:y}}return nf("tsumTOST",T,v,A,b,S,k,_,b,n,r,g)}var ld=["alternative","tsmethod"],fd=["square","central"],md=1e3,ud=32,hd=.9,rf=1+1e-7,pd=2**24,dd=34e7;function gd(e,t={}){if(V(t,ld,"boschlooTest"),e.length!==2||e.some(M=>M.length!==2))throw new TypeError("'table' must be a 2 x 2 table");let n=Me(t.alternative),r=ae(t.tsmethod,fd),o=e.map((M,R)=>Array.from(M,(y,x)=>br(y,`table[${R}][${x}]`))),[[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)>pd)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(md,Math.ceil(ud*Math.sqrt(m))),h=M=>Math.min(M+1,40*Math.sqrt(M));if(u*(h(l)+h(f)+4)>dd)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=Vt(s,l,f,d,!0,!1):g==="greater"?v=Vt(s-1,l,f,d,!1,!1):v=ke(()=>no(o)).pValue;let N=wd(l,f,s,c,g),{value:b,at:E}=bd(N,l,f,u),w=n==="two.sided"&&r==="central"?Math.min(1,2*b):b;return W("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 wd(e,t,n,r,o){let s=e+t,a=new Float64Array(s+1);for(let v=2;v<=s;v++)a[v]=Fo(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 y=0;y<E;y++){let x=N+y,T=-a[x]-a[e-x]-a[v-x]-a[t-v+x];f[y]=T,T>w&&(w=T)}let M=0;for(let y=0;y<E;y++)f[y]=Math.exp(f[y]-w),M+=f[y];for(let y=0;y<E;y++)f[y]=f[y]/M;let R=0;for(let y=0;y<E;y++)m[y]=R+=f[y];R=0;for(let y=E-1;y>=0;y--)u[y]=R+=f[y];if(o==="less")for(let y=0;y<E;y++)i[(N+y)*c+v-N-y]=m[y];else if(o==="greater")for(let y=0;y<E;y++)i[(N+y)*c+v-N-y]=u[y];else{let y=0;for(let x=1;x<E;x++)f[x]>f[y]&&(y=x);for(let x=0;x<E;x++){let T=f[x]*rf,A=-1,S=y;for(;S-A>1;){let P=A+S>>1;f[P]<=T?A=P:S=P}let k=y,_=E;for(;_-k>1;){let P=k+_>>1;f[P]<=T?_=P:k=P}let I=f[y]<=T?1:(A>=0?m[A]:0)+(_<E?u[_]:0);i[(N+x)*c+v-N-x]=Math.min(1,I)}}}let h=i[n*c+r]*rf,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 of(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]=Ut(r,e,t,!1);let o=kt(1,-t),s=Pe([t,0],o),a=Pe(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 bd(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]=of(t,b,c),[M,R]=of(n,b,i),y=0;for(let T=M;T<=R;T++)l[T]=y+=i[T];y=0;for(let T=R;T>=M;T--)f[T]=y+=i[T];let x=0;for(let T=E;T<=w;T++){let A=c[T];if(A===0)continue;let S=0;for(let k=o[T];k<o[T+1];k++){let _=s[k],I=a[k];if(!(I<M||_>R))if(_===0)S+=l[Math.min(I,R)];else if(I===n)S+=f[Math.max(_,M)];else for(let P=Math.max(_,M);P<=Math.min(I,R);P++)S+=i[P]}x+=A*S}return Math.min(1,x)},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=hd*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),M=u*Math.min(r+1,b+1)/(r+1),y=Oc(A=>-m(h(A)),w,M,1e-14).x,x=m(h(y)),T=1e-4/Math.sqrt(2*(t+n));for(let A=0;A<2&&(y-T>w&&y+T<M);A++,T*=.01){let S=m(h(y-T)),k=m(h(y+T)),_=S-2*x+k;if(!(_<0))break;let I=T*(S-k)/(2*_);if(!(Math.abs(I)<T))break;let P=m(h(y+I));P>x*(1+2**-50)&&(y+=I,x=P)}x>d&&(d=x,v=h(y))}return{value:d,at:v}}function vr(e){let t=dt(e);return t.some(n=>typeof n=="string")?t.map(n=>typeof n=="number"&&!Number.isNaN(n)?it(n):n):t}function co(e,t){return typeof e=="number"&&typeof t=="number"?e-t:Ht(String(e),String(t))}function qn(e,t,n="drop"){if(t===void 0){let c=e;if(c.length>0&&typeof c[0]=="number")throw new TypeError("'g' is required when 'x' is a single sample");let i=c.map(m=>Array.from(m).filter(u=>!Number.isNaN(u)));if(n==="error"&&i.some(m=>m.length===0))throw new RangeError("all groups must contain data");let l=[],f=[];return i.forEach((m,u)=>{m.length!==0&&(l.push(String(u+1)),f.push(m))}),{labels:l,groups:f}}let r=Array.from(e);if(r.length!==t.length)throw new RangeError("'x' and 'g' must have the same length");let o=vr(t),s=new Map;for(let c=0;c<r.length;c++){let i=r[c],l=o[c];if(Number.isNaN(i)||typeof l=="number"&&Number.isNaN(l))continue;let f=s.get(l);f===void 0&&s.set(l,f=[]),f.push(i)}let a=Array.from(s.keys()).sort(co);return{labels:a.map(it),groups:a.map(c=>s.get(c))}}function Yn(e,t,n="drop"){return qn(e,t,n).groups}function ps(e){let t=[...e].sort((r,o)=>r-o),n=t.length;return n%2===1?t[(n-1)/2]:sn(t[n/2-1],t[n/2])}function za(e){if(e.length===0)throw new RangeError("'y' must have at least one row");let t=e[0].length;if(e.some(n=>n.length!==t))throw new RangeError("all rows of 'y' must have the same length");return e.filter(n=>n.every(r=>!Number.isNaN(r))).map(n=>Array.from(n))}var sf=["correct"];function vd(e,t,n={}){Je(t)&&V(t,sf,"mcnemarTest"),V(n,sf,"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=Nd(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=af(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+=af(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:Ge(a,i/2,2,!1,!1),warnings:[]}}function af(e,t,n){let r=t+n;return r<1/0?wt(e,r):wt(L(e,-520),L(t,-1040)+L(n,-1040))}function Nd(e,t){if(e.length!==t.length)throw new RangeError("'x' and 'y' must have the same length");let n=[],r=vr(e),o=vr(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(co),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 yd=["ratio","alternative","confLevel"],Wa=2**-1022;function cf(e,t,n={}){if(V(n,yd,"varTest"),typeof e=="string"){let b=Tn(e,t);return cf(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=Me(n.alternative),c=n.confLevel??.95;ct(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]=ht([r,o]).variance,u=0;if(!(f>=Wa&&m>=Wa)){let b=ht([r]),E=ht([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:ye(f/m,u),p=h/s,g=We(p,i,l,!0,!1),d=We(p,i,l,!1,!1),v,N;if(a==="less")v=g,N=[0,h/Kt(1-c,i,l,!0,!1)];else if(a==="greater")v=d,N=[h/Kt(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?Kt(b,i,l,!1,!1):Kt(1-b,i,l,!0,!1);N=[h/E,h/Kt(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 lf(e,t){if(typeof e=="string"){let p=Yr(e,t);return lf(_c(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=ht(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<Wa)){let p=o.map(E=>{let w=ht([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,M)=>E+s[M]*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:Ge(u,h/2,2,!1,!1),warnings:[]}}var Md=["alternative"];function Ba(e,t,n,r){return{method:e,statistic:n,statisticName:t,parameter:r,parameterName:"df",pValue:Ge(n,r/2,2,!1,!1),warnings:[]}}function ff(e){let t=0;for(let n of Jt(e))t+=n*n*n-n;return t}function ds(e,t){if(typeof e=="string"){let l=Yr(e,t);return ds(l.values,l.group.codes)}let n=Yn(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=Ye(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-ff(o)/(s*s*s-s)),Ba("Kruskal-Wallis rank sum test","Kruskal-Wallis chi-squared",c,r-1)}function Ka(e,t){if(typeof e=="string")return Ka(Zs(e,t));let n=za(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=Ye(l);for(let m=0;m<o;m++)s[m]+=f[m];a+=ff(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 Ba("Friedman rank sum test","Friedman chi-squared",i,o-1)}function Ed(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{...Ka(e),method:"Cochran's Q test",statisticName:"Q"}}function mf(e,t){if(typeof e=="string")return mf(Zs(e,t));let n=za(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=>Be(g)-$t(g));s.some(g=>g===1/0)&&n.every(g=>g.every(Number.isFinite))&&(s=se(n).groups.map(g=>Be(g)-$t(g)));let a=Ye(s),c=new Array(o).fill(0),i=0;n.forEach((g,d)=>{let v=Ye(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=We(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 uf(e,t){if(typeof e=="string"){let h=Yr(e,t);return uf(h.values,h.group.codes)}let n=Yn(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=ps(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(se(n).groups));let a=s.length,i=Ye(s).map(h=>Z((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/=_e(i),Ba("Fligner-Killeen test of homogeneity of variances","Fligner-Killeen:med chi-squared",m,r-1)}function hf(e,t,n={}){if(V(n,Md,"moodTest"),typeof e=="string"){let E=Tn(e,t);return hf(E.x,E.y,n)}let r=Array.from(e).filter(Number.isFinite),o=Array.from(t).filter(Number.isFinite),s=Me(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 M=u;M<=E;M++)w+=(M+1-(i+1)/2)**2;w/=E-u+1;for(let M=u;M<=E;M++)m[f[M]]=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"?Y(N):s==="greater"?Y(N,{lowerTail:!1}):2*Y(-Math.abs(N));return{method:"Mood two-sample test of scale",alternative:s,statistic:N,statisticName:"Z",pValue:b,warnings:[]}}function Xn(e,t){let n=0;for(let r=e.length-1;r>=0;r--)n=n*t+e[r];return n}var Rd=[0,.221157,-.147981,-2.07119,4.434685,-2.706056],xd=[0,.042981,-.293762,-1.752461,5.682633,-3.582633],Td=[.544,-.39978,.025054,-6714e-7],Ad=[1.3822,-.77857,.062767,-.0020322],Sd=[-1.5861,-.31082,-.083751,.0038915],kd=[-.4803,-.082676,.0030302],qd=[-2.273,.459];function Id(e){if(!(e>=3)||e!==Math.floor(e))throw new RangeError("n must be an integer of at least 3");let t=new Float64Array(e);if(e===3)return t[0]=-Math.SQRT1_2,t[2]=Math.SQRT1_2,t;let n=Math.floor(e/2),r=new Float64Array(n),o=0;for(let m=0;m<n;m++)r[m]=-Z((m+1-.375)/(e+.25)),o+=r[m]*r[m];o*=2;let s=Math.sqrt(o),a=1/Math.sqrt(e),c=r[0]/s+Xn(Rd,a),i=new Float64Array(n);i[0]=c;let l,f;if(e>5){let m=r[1]/s+Xn(xd,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 pf(e){let t=se([e]).groups[0],n=t.length,r=Id(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 _d=1.90985931710274,Ld=1.0471975511966;function df(e,t){if(Number.isNaN(e)||!(t>=3))return NaN;if(t===3){let a=_d*(Math.asin(Math.sqrt(1-e))-Ld);return Math.max(a,0)}let n=Math.log(e);if(t<=11){let a=Xn(qd,t);if(n>=a)return 1e-99;let c=Xn(Td,t),i=Math.exp(Xn(Ad,t));return Y(-Math.log(a-n),{mean:c,sd:i,lowerTail:!1})}let r=Math.log(t),o=Xn(Sd,r),s=Math.exp(Xn(kd,r));return Y(n,{mean:o,sd:s,lowerTail:!1})}var vf=Math.log(2*Math.PI),gf=2e-14,wf=500;function Od(e){return Math.exp(-.5*e*e-.5*vf)}function Cd(e){return Y(e,{logP:!0})}function Nf(e,t=-1/0,n=!1){let r=t===-1/0?Qs(e,0,2,0,gf,wf):Qs(e,t,1,0,gf,wf),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 bf(e,t,n){let r=t.map(Math.sqrt),o=t.map(a=>Math.sqrt(1-a));return Nf(a=>{let c=0;for(let i=0;i<r.length;i++){let l=(r[i]*a+e)/o[i];if(!n){c+=Cd(l);continue}let f=(r[i]*a-e)/o[i],m=Y(f)+Y(l,{lowerTail:!1});m<.5?c+=Math.log1p(-m):c+=Math.log(Math.max(0,l+f<0?Y(l)-Y(f):Y(f,{lowerTail:!1})-Y(l,{lowerTail:!1})))}return Od(a)*-Math.expm1(c)})}function Qn(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,bf(e,t,r));let o=n/2,s=Math.log(2)+.5*Math.log(o)-.5*vf-sc(o);return Math.min(1,Nf(c=>{let i=s-o*(Math.expm1(2*c)-2*c);return i<-745?0:Math.exp(i)*bf(e*Math.exp(c),t,r)},-1/0,!0))}var Fd=["pAdjustMethod"],Dd=["pAdjustMethod","alternative"],Pd=["alternative"],Vd=["pAdjustMethod","alternative"],$d=["method","altp"],jd=["alternative"],yf="Ties are present. z-quantiles were corrected for ties.";function wn(e,t){let{labels:n,groups:r}=qn(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 Ua(e){let t=e.groups.flat(),n=Ye(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 Jt(t))s+=a*a*a-a;return{R:r,C:s/(12*(e.N-1))}}function lo(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 zd(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=wr(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 ws(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 Ha(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 gs=e=>Y(e,{lowerTail:!1}),Ga=(e,t,n)=>Do(Math.abs(e),t,n,{lowerTail:!1});function Ya(e){let t=ht(e.groups),n=0;for(let r of t.n)r>1&&(n+=r-1);return fo(n,t.ssWithin),{m:t.mean,s2:t.ssWithin/n,df:e.N-e.k,k:t.k}}function fo(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 Wd(e,t,n={}){V(n,Fd,"kwAllPairsDunnTest");let r=n.pAdjustMethod??"holm",o=wn(e,t),{R:s,C:a}=Ua(o),c=o.N*(o.N+1)/12-a,i=lo(o.k,(f,m)=>Math.abs(s[f]-s[m])/Math.sqrt(c*(1/o.n[f]+1/o.n[m]))),l=zd(i.map(f=>f.map(m=>2*gs(m))),r);return W("kwAllPairsDunnTest",ws(o,"Dunn's all-pairs test",i,l,r,a!==0?[yf]:[]))}function Bd(e,t,n={}){V(n,Dd,"kwManyOneDunnTest");let r=Me(n.alternative),o=n.pAdjustMethod??"single-step",s=wn(e,t),{R:a,C:c}=Ua(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=Mf(f,m,r,o);return W("kwManyOneDunnTest",Ha(s,"Dunn's many-to-one test",f,u,o,c!==0?[yf]:[]))}function Mf(e,t,n,r){return r==="single-step"?e.map(o=>n==="two.sided"?Qn(o,t,1/0,!0):Qn(n==="greater"?o:-o,t,1/0,!1)):wr(e.map(o=>n==="two.sided"?2*gs(Math.abs(o)):gs(n==="greater"?o:-o)),r)}function Kd(e,t,n={}){V(n,Pd,"dunnettTest");let r=Me(n.alternative),o=wn(e,t),{m:s,s2:a,df:c}=Ya(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*Qn(Math.abs(h),m,c,!1)):Qn(r==="greater"?h:-h,m,c,!1));return W("dunnettTest",Ha(o,`Dunnett's-test for multiple
4
- comparisons with one control`,f,u,"single-step"))}function Ud(e,t,n={}){V(n,Vd,"manyOneUTest");let r=Me(n.alternative),o=n.pAdjustMethod??"single-step",s=wn(e,t);if(o==="single-step"&&s.k<3)throw new RangeError(`for 'p.adjust.method = single-step' at least
1
+ /*! rStats.js 1.4.0 | MIT License | includes code ported from SciPy, Boost.Math, statsmodels, LAPACK and others: see THIRD_PARTY_NOTICES.md at https://github.com/numero-uno-jpn/rStats.js */
2
+ import{B as ms,C as us,D as hs,b as Bt,c as uo,d as pn,f as Jt,k as qr,l as al,p as il,q as Ir,r as cl,v as fs,w as ll,x as po,z as dl}from"./chunks/chunk-ZRFZ5UIB.js";import{a as _a,e as La}from"./chunks/chunk-X7K6EF7S.js";import{a as ds,b as Wn,c as vo,d as vl,e as yl,f as qa,g as Ze,h as tn,i as dn,j as qt,k as xl,l as Kh,n as Ns,o as yo,p as Ms}from"./chunks/chunk-UMMCVF53.js";import"./chunks/chunk-H72MW7YP.js";import"./chunks/chunk-J72MLFUG.js";import{a as Nl,c as Ml,d as El,e as lr,g as No}from"./chunks/chunk-UNP4HBPP.js";import{b as ka}from"./chunks/chunk-WWJGCBWH.js";import{b as fl,c as ml,d as ul,e as hl,f as pl,i as go}from"./chunks/chunk-6FZAJQ4H.js";import{a as xt,b as Lr,c as Cr}from"./chunks/chunk-S2YCFUCA.js";import{a as ol,d as ls}from"./chunks/chunk-HWB7XRX2.js";import{a as ws,b as bs,c as vs,d as fr,e as Dr,f as Vr,g as kl,h as ql}from"./chunks/chunk-TEHMTQNB.js";import{A as Sn,g as rs,h as os,j as ss,k as kr,p as pt,q as on,s as ir,t as Dn,v as Vn,z as sn}from"./chunks/chunk-V2KVTAVV.js";import{C as Tl,E as Sl,H as Vt,I as Il,c as Rl,h as Al,j as Rt,k as Ia,l as gs,m as Or,n as Fr,t as ys,y as ot}from"./chunks/chunk-5SW3L6J3.js";import{a as _r,b as wl,g as ps}from"./chunks/chunk-GIER5WMB.js";import{A as sl,B as yn,C as cn,i as bt,l as ns,x as Sa,y as lt,z as wt}from"./chunks/chunk-27VFB3SA.js";import{a as ln,c as bl}from"./chunks/chunk-GLGOV3A5.js";import{a as Uh,c as Z,d as tt,e as ft,f as wo}from"./chunks/chunk-YBOSCQF2.js";import{a as bo,b as In,c as gl,d as St,e as mt}from"./chunks/chunk-RY7CCWVU.js";import{a as dt,b as Xt,c as en}from"./chunks/chunk-RUZATICF.js";import"./chunks/chunk-F6Z3GLWA.js";import{b as Qc,c as Zc}from"./chunks/chunk-GBSMRVUY.js";import{A as Gc,B as Yc,D as Xc,M as Jc,q as Ut,r as ce,s as pe}from"./chunks/chunk-GEF2YRBV.js";import{A as el,C as tl,D as Lt,E as as,F as nl,G as is,H as rl,I as De,J as vt,K as hn,L as Ct,M as $n,N as Ot,O as cs,P as Te,Q as Ta,b as zt,c as bn,d as ue,e as Kc,f as Pe,g as _e,h as Tt,i as it,j as ct,k as Hc,l as H,m as vn,p as ho,q as cr,r as kn,s as an,t as qn,u as D,v as Le,w as Pn,x as st,y as rt,z as jn}from"./chunks/chunk-PZ66PUWC.js";var _l=["alternative","mu","paired","varEqual","confLevel"];function gn(e,t,n={}){if(typeof e!="string"&&xt(t)&&H(t,_l,"tTest"),H(n,_l,"tTest"),typeof e=="string")return Hh(e,t,n);let r;t!==void 0&&!ArrayBuffer.isView(t)&&!Array.isArray(t)?n=t:r=t;let o=Ze(n.alternative),s=n.mu??0,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(tn(i),a&&r===void 0)throw new TypeError("y is required when paired is true");let l,f,m;if(a){let[k,_]=$n(Array.from(e),Array.from(r));({d:l,k:m}=as(k,_))}else{let k=Te(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=D(s,-m),h,p,w,d,v,N,b,M=l.length,g=f!==void 0&&c;if(M<1||!g&&M<2)throw new Error("not enough 'x' observations");let y=st(l),x=M>1?rt(l):NaN;if(f===void 0){if(p=M-1,w=Math.sqrt(x/M),w===0||w<10*Number.EPSILON*Math.abs(y))throw new Error("data are essentially constant");h=(y-u)/w,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 k=f.length;if(k<1||!c&&k<2)throw new Error("not enough 'y' observations");if(c&&M+k<3)throw new Error("not enough observations");let _=st(f),L=k>1?rt(f):NaN;if(N=`${c?"":"Welch"} Two Sample t-test`,d=[y,_],v=["mean of x","mean of y"],b="difference in means",c){p=M+k-2;let q=0;M>1&&(q+=(M-1)*x),k>1&&(q+=(k-1)*L),q/=p,w=Math.sqrt(q*(1/M+1/k))}else{let q=x/M,F=L/k;w=Math.sqrt(Math.sqrt(q)**2+Math.sqrt(F)**2),p=is([q,F],[M-1,k-1])}if(w===0||w<10*Number.EPSILON*Math.max(Math.abs(y),Math.abs(_)))throw new Error("data are essentially constant");h=(y-_-u)/w}let E=typeof d=="number"?d:d[0]-d[1],R=(k,_)=>{let L=u+(h+_*k)*w;return Number.isFinite(L)&&Math.max(Math.abs(u),Math.abs(h*w))<=16*(Math.abs(E)+k*w)?L:E+_*k*w},A,T;if(o==="less")A=lt(h,p,!0,!1),T=[-1/0,R(wt(i,p,!0,!1),1)];else if(o==="greater")A=lt(h,p,!1,!1),T=[R(wt(i,p,!0,!1),-1),1/0];else{A=2*lt(-Math.abs(h),p,!0,!1);let k=1-i,_=k/2<1e-8?wt(k/2,p,!1,!1):wt(1-k/2,p,!0,!1);T=[R(_,-1),R(_,1)]}return T=[D(T[0],m),D(T[1],m)],d=typeof d=="number"?D(d,m):d.map(k=>D(k,m)),w=D(w,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:w,stdError:w,warnings:[]}}function Hh(e,t,n){if(n.paired!==void 0)throw new Error("cannot use 'paired' in formula method");let r=ds(e,t,!0);if(r.kind==="one")return gn(r.x,n);if(r.kind==="paired")return gn(r.x,r.y,{...n,paired:!0});let[o,s]=r.levels;return{...gn(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 Gh=new Set(["n","delta","sd","sigLevel","power","type","alternative","strict","tol"]),Yh=["n","p1","p2","sigLevel","power","alternative","strict","tol"],Xh=["groups","n","betweenVar","withinVar","sigLevel","power"],Cl=Number.EPSILON**.25;function Ie(e,t){if(e===void 0)return t[0];let n=t.find(o=>o===e);if(n!==void 0)return n;let r=t.filter(o=>o.startsWith(e));if(e.length===0||r.length!==1)throw new RangeError(`'arg' should be one of ${t.map(o=>`\u201C${o}\u201D`).join(", ")}`);return r[0]}var xe=e=>e==null;function nn(e,t,n){if(!xe(e)&&!t(e))throw new RangeError(n)}var Mo=e=>e>=0&&e<=1,Es=e=>e>0&&e<1/0;function fn(e,t){if(e.filter(xe).length!==1)throw new RangeError(t)}function Ge(e,t,n,r={}){return El(e,t,n,r).root}var mr=(e,t,n,r)=>yn(e,t,{ncp:n,lowerTail:r});function Ca(e,t){if(e!=null&&!(typeof e=="number"&&e>=0&&e<=1))throw new RangeError(t)}var Oa="'sig.level' must be numeric in [0, 1]";function Qh(e={}){H(e,Gh,"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;fn([t,n,o,r,s],"exactly one of 'n', 'delta', 'sd', 'power', and 'sig.level' must be NULL"),Ca(s,Oa),nn(r,Mo,"'power' must be a probability"),nn(o,Es,"'sd' must be a positive finite number"),nn(t,w=>w>=2,"number of observations in each group must be at least 2"),nn(n,w=>!Number.isNaN(w),"'delta' must not be NaN"),nn(e.tol,Es,"'tol' must be a positive finite number");let a=Ie(e.type,["two.sample","one.sample","paired"]),c=Ie(e.alternative,["two.sided","one.sided"]),i=e.strict??!1,l=e.tol??Cl,f=a==="two.sample"?2:1,m=c==="two.sided"?2:1;m===2&&!xe(n)&&(n=Math.abs(n));let u=(w,d,v,N)=>{let b=Math.max(1e-7,w-1)*f,M=cn(N/m,b,{lowerTail:!1}),g=Math.sqrt(w/f)*d/v;Number.isFinite(g)||(g=Math.sqrt(w/f)*(d/v));let y=mr(M,b,g,!1);return i&&m===2?y+mr(-M,b,g,!0):y};if(xe(r))r=u(t,n,o,s);else{let w=r;if(xe(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)-w,2,1e7,{tol:l,extendInt:"upX"})}else if(xe(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)-w,d*1e-7,d*1e7,{tol:l,extendInt:n>0?"downX":"upX"})}else xe(n)?n=Ge(d=>u(t,d,o,s)-w,o*1e-7,o*1e7,{tol:l,extendInt:"upX"}):s=Ge(d=>u(t,n,o,d)-w,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 Zh(e={}){H(e,Yh,"powerPropTest");let{n:t,p1:n,p2:r,power:o}=e,s=e.sigLevel===void 0?.05:e.sigLevel;fn([t,n,r,o,s],"exactly one of 'n', 'p1', 'p2', 'power', and 'sig.level' must be NULL"),Ca(s,Oa),nn(o,Mo,"'power' must be a probability"),nn(n,Mo,"'p1' must be a probability"),nn(r,Mo,"'p2' must be a probability"),nn(t,u=>u>0,"number of observations in each group must be positive"),nn(e.tol,Es,"'tol' must be a positive finite number");let a=Ie(e.alternative,["two.sided","one.sided"]),c=e.strict??!1,i=e.tol??Cl,l=a==="two.sided"?2:1,f=(u,h,p,w)=>{let d=pe(w/l,{lowerTail:!1});if(c&&l===2){let v=(h+p)/2,N=h*(1-h),b=p*(1-p),M=v*(1-v),g=Math.abs(h-p),y=Math.sqrt(N+b);return ce((Math.sqrt(u)*g-d*Math.sqrt(2*M))/y)+ce((Math.sqrt(u)*g+d*Math.sqrt(2*M))/y,{lowerTail:!1})}return ce((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(xe(o))o=f(t,n,r,s);else{let u=o;if(xe(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(xe(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(xe(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 Z("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 Ll=1e16,xs=(e,t)=>e>Ll&&e<1/0&&t>Ll;function Rs(e,t,n,r){let o=pe(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 ce((o*a-s)/c,{lowerTail:!1})}function Jh(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 D(D(e,-s)*D(t,-a)*D(n,-c)/D(r,-i),s+a+c-i)}function ep(e={}){H(e,Xh,"powerAnovaTest");let{groups:t,n,betweenVar:r,withinVar:o,power:s}=e,a=e.sigLevel===void 0?.05:e.sigLevel;if(fn([t,n,r,o,s,a],"exactly one of 'groups', 'n', 'between.var', 'within.var', 'power', and 'sig.level' must be NULL"),!xe(t)&&!(t>=2))throw new RangeError("number of groups must be at least 2");if(!xe(n)&&!(n>=2))throw new RangeError("number of observations in each group must be at least 2");Ca(a,Oa),nn(s,Mo,"'power' must be a probability"),nn(r,i=>i>=0&&i<1/0,"'betweenVar' must be a nonnegative finite number"),nn(o,Es,"'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=Jh(i-1,l,f,m));let p=i-1,w=(l-1)*i;return h===1/0?1:xs(p,w)?Rs(p,w,h,u):ir(Dn(u,p,w,{lowerTail:!1}),p,w,{ncp:h,lowerTail:!1})};if(xe(s))s=c(t,n,r,o,a);else{let i=s;if(xe(t))t=Ge(l=>c(l,n,r,o,a)-i,2,100);else if(xe(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(xe(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 xe(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 tp=["correct","p","rescaleP"],np=["confLevel"],rp=["alternative","rowScores","colScores"];function Kt(e){let t=[0,0];for(let n of e)t=ue(t,[n,0]);return t}function Da(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 Va=e=>e.length>0&&typeof e[0]!="number",Pa=(e,t)=>Number.isNaN(e)?NaN:bt(e,t/2,2,!1,!1);function Ol(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 Eo(e,t){return e.length>0&&Array.isArray(e[0])?e.map((n,r)=>n.map((o,s)=>Ol(o,t[r][s]))):e.map((n,r)=>Ol(n,t[r]))}var op=["none","williams","yates"],Dl="Log likelihood ratio (G-test)";function sp(e,t={}){H(t,tp,"gTest");let n=Ie(t.correct,op);if(typeof e!="object"||e===null||typeof e.length!="number")throw new TypeError("'x' must be an array of counts or of rows");if(Va(e)){let o=Da(e);return Fl(o.flat()),o.length===1?Z("gTest",Fa(o[0],n,t)):o[0].length===1?Z("gTest",Fa(o.map(s=>s[0]),n,t)):Z("gTest",ap(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 Fl(r),Z("gTest",Fa(r,n,t))}function Fl(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 Vl(e,t){return e<1/0?0:Math.ceil(Math.log2(t))+1}function Pl(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,Yc(o,s))}return n[0]+n[1]}function ap(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=Kt(e.flat()),s=Vl(o[0],n*r),a=s===0?e:e.map(b=>b.map(M=>D(M,-s))),c=s===0?o:Kt(a.flat()),i=a.map(b=>Kt(b)),l=a[0].map((b,M)=>Kt(a.map(g=>g[M]))),f=i.map(b=>l.map(M=>_e(b,it(M,c)))),m=a;if(t==="yates"){let[[b,M],[g,y]]=a,x=b*y-M*g>0?-.5:.5,E=D(.5,-s),R=x<0?-E:E;m=[[b+R,M-R],[g-R,y+R]]}let u=Pl(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],M=i.reduce((y,x)=>y+1/(x[0]+x[1]),0),g=l.reduce((y,x)=>y+1/(x[0]+x[1]),0);p=1+D((b*M-1)*(b*g-1)/(6*b*(n-1)*(r-1)),-s)}let w=D(2*u,s)/p,d=(n-1)*(r-1),v=f.map(b=>b.map(M=>D(M[0]+M[1],s)));return{method:`${Dl} test of independence ${t==="none"?"without correction":t==="williams"?"with Williams' correction":"with Yates' correction"}`,statistic:w,statisticName:"G",parameter:d,parameterName:"X-squared df",pValue:Pa(w,d),observed:s===0?m:m.map(b=>b.map(M=>D(M,s))),expected:v,ftResiduals:Eo(e,v),warnings:h}}function Fa(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=Kt(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=Kt(e),i=Vl(c[0],r),l=i===0?e:e.map(b=>D(b,-i)),f=i===0?c:Kt(l),m=s.map(b=>o?it(Tt(f,b),a):Tt(f,b)),u=m.map(b=>b[0]+b[1]),h=l;if(t==="yates"){let b=D(.5,-i),M=l[0]-u[0];M>D(.25,-i)?h=[l[0]-b,l[1]+b]:-M>D(.25,-i)&&(h=[l[0]+b,l[1]-b])}let p=Pl(h,m);if(!o&&!Number.isNaN(p)){let b=f[0]+f[1];p-=b*(a[0]-1+a[1])}let w=Number.isNaN(p)?["NaNs produced"]:[],d=t==="williams"?1+(r+1)/(6*D(f[0]+f[1],i)):1,v=D(2*p,i)/d,N=u.map(b=>D(b,i));return{method:`${Dl} goodness of fit test`,statistic:v,statisticName:"G",parameter:r-1,parameterName:"X-squared df",pValue:Pa(v,r-1),observed:i===0?h:h.map(b=>D(b,i)),expected:N,ftResiduals:Eo(e,N),warnings:w}}function ip(e){if(typeof e!="object"||e===null||!Va(e))throw new RangeError("'x' must be square with at least two rows and columns");let t=Da(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 cp(e){let t=ip(e),n=Kt(t.flat())[0],r=n>2**400||n>0&&n<2**-400?Math.round(Math.log2(n)):0,o=r===0?t:t.map(g=>g.map(y=>D(y,-r))),s=o.length,a=o.map(g=>Kt(g)),c=o.map((g,y)=>Kt(o.map(x=>x[y]))),i=o.map((g,y)=>o[y].some((x,E)=>E!==y&&x>0)||o.some((x,E)=>E!==y&&x[y]>0)).flatMap((g,y)=>g?[y]:[]),l=i.length;if(l<2)throw new RangeError("Too many equal marginals, cannot compute");let f=i.map((g,y)=>y),m=g=>f[g]===g?g:f[g]=m(f[g]);for(let g=0;g<l;g++)for(let y=g+1;y<l;y++)o[i[g]][i[y]]+o[i[y]][i[g]]>0&&(f[m(g)]=m(y));if(i.some((g,y)=>m(y)!==m(0)))throw new RangeError("the categories fall into groups without counts between them: the covariance matrix is singular");let u=l-1,h=i.slice(0,u).map(g=>{let y=ue(a[g],[-c[g][0],-c[g][1]]);return y[0]+y[1]}),p=i.slice(0,u).map(g=>i.slice(0,u).map(y=>{if(g!==y)return-(o[g][y]+o[y][g]);let x=0;for(let E=0;E<s;E++)E!==g&&(x+=o[g][E]+o[E][g]);return x})),w=p.map(g=>g.map(()=>0)),d=[];for(let g=0;g<u;g++){let y=p[g][g];for(let x=0;x<g;x++)y-=w[g][x]*w[g][x]*d[x];d.push(y);for(let x=g+1;x<u;x++){let E=p[x][g];for(let R=0;R<g;R++)E-=w[x][R]*w[g][R]*d[R];w[x][g]=E/y}}let v=0,N=[];for(let g=0;g<u;g++){let y=h[g];for(let x=0;x<g;x++)y-=w[g][x]*N[x];N.push(y),v+=y*y/d[g]}let b=Kt(i.flatMap(g=>i.map(y=>t[g][y]))),M=l-1;return v=D(v,r),Z("stuartMaxwellTest",{method:"Stuart-Maxwell test",statistic:v,statisticName:"chi-squared",parameter:M,parameterName:"df",pValue:Pa(v,M),N:b[0]+b[1],warnings:[]})}function lp(e,t={}){H(t,np,"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=jl(e),o=Kt(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(g=>g.map(y=>D(y,-s))),{pc:c,pd:i}=fp(a),l=[0,0],f=[0,0];a.forEach((g,y)=>g.forEach((x,E)=>{l=ue(l,Tt(c[y][E],x)),f=ue(f,Tt(i[y][E],x))})),l=[l[0]/2,l[1]/2],f=[f[0]/2,f[1]/2];let m=ue(l,f),u=it(Pe(l,f),m)[0],h=_e(m,m),p=a.map((g,y)=>g.map((x,E)=>2*it(Pe(_e(f,c[y][E]),_e(l,i[y][E])),h)[0])),w=Math.max(...p.flat().map(Math.abs)),d=w>0&&w<1/0?Math.floor(Math.log2(w)):0,v=0;a.forEach((g,y)=>g.forEach((x,E)=>{let R=D(p[y][E],-d);v+=x*R*R}));let N=D(Math.sqrt(v),d-s/2),b=(1-n)/2,M=b<1e-8?pe(b,{lowerTail:!1}):pe(1-b);return Z("goodmanKruskalGamma",{estimate:u,confInt:[Math.max(-1,u-M*N),Math.min(1,u+M*N)],confLevel:n,ase:N,warnings:[]})}function jl(e){if(typeof e!="object"||e===null||!Va(e))throw new TypeError("'table' must be an array of rows");let t=Da(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 fp(e){let t=e.length,n=e[0].length,r=[0,0],o=e.map(p=>{let w=[],d=r;for(let v=0;v<n;v++)w.push(d),d=ue(d,[p[v],0]);return w}),s=e.map(p=>{let w=new Array(n),d=r;for(let v=n-1;v>=0;v--)w[v]=d,d=ue(d,[p[v],0]);return w}),a=p=>{let w=[],d=new Array(n).fill(r);for(let v=0;v<t;v++)w.push(d),d=d.map((N,b)=>ue(N,p[v][b]));return w},c=p=>{let w=new Array(t),d=new Array(n).fill(r);for(let v=t-1;v>=0;v--)w[v]=d,d=d.map((N,b)=>ue(N,p[v][b]));return w},i=a(o),l=c(s),f=a(s),m=c(o),u=i.map((p,w)=>p.map((d,v)=>ue(d,l[w][v]))),h=f.map((p,w)=>p.map((d,v)=>ue(d,m[w][v])));return{pc:u,pd:h}}function mp(e,t={}){H(t,rp,"lblTest");let n=Ze(t.alternative),r=jl(e),o=r.length,s=r[0].length,a=(R,A,T)=>{if(R===void 0)return Array.from({length:A},(_,L)=>L+1);let k=Array.from(R);if(k.length!==A)throw new RangeError(`'${T}' must have one score for each ${T==="rowScores"?"row":"column"}`);if(k.some(_=>typeof _!="number"||!Number.isFinite(_)))throw new RangeError(`'${T}' must be finite numbers`);return k},c=a(t.rowScores,o,"rowScores"),i=a(t.colScores,s,"colScores"),f=Kt(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=>D(A,-f))),u=m.map(R=>Kt(R)).map(R=>R[0]+R[1]),h=m[0].map((R,A)=>Kt(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 w=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-w),N=i.map(R=>R-d),b=u.reduce((R,A,T)=>R+A*v[T]*v[T],0),M=h.reduce((R,A,T)=>R+A*N[T]*N[T],0),g=0;if(m.forEach((R,A)=>R.forEach((T,k)=>g+=T*v[A]*N[k])),!(b>0&&M>0))throw new RangeError("the row and column scores must vary over the observations (the variance of the statistic is 0)");let y=g/Math.sqrt(b)/Math.sqrt(M),x=f===0?Math.sqrt(p-1)*y:D(Math.sqrt(p-D(1,-f))*y,f/2),E=n==="less"?ce(x):n==="greater"?ce(x,{lowerTail:!1}):2*ce(-Math.abs(x));return Z("lblTest",{method:"Linear-by-Linear Association Test",alternative:n,statistic:x,statisticName:"Z",pValue:E,warnings:[]})}var $l=["correct","p","rescaleP","simulatePValue","B","nMinus1"];function mn(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 up(e,t,n){return xt(t)&&H(t,$l,"chisqTest"),H(n,$l,"chisqTest"),Array.isArray(t)?Z("chisqTest",hp(e,t,n??{})):Z("chisqTest",pp(e,t??{}))}var Bl="Chi-squared approximation may be incorrect";function hp(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(ln),c=[...new Set(s)].sort(ln);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])]++),{...Yl(f,n),rowLevels:a,colLevels:c}}function pp(e,t){if(e.length>0&&typeof e[0]!="number"){let r=e.map(s=>Array.from(s)),o=r[0].length;if(r.some(s=>s.length!==o))throw new TypeError("all rows of the matrix must have the same length");return r.length===1?ja(r[0],t):o===1?ja(r.map(s=>s[0]),t):Yl(r,t)}return ja(Array.from(e),t)}function Ul(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 Hl(e,t){return Number.isNaN(e)?NaN:bt(e,t/2,2,!1,!1)}function Gl(e){if(!e.some(t=>t<5)&&e.some(Number.isNaN))throw new RangeError("missing value where TRUE/FALSE needed (the expected counts contain NaN)")}function ja(e,t){if(t.nMinus1??!1)throw new RangeError("'nMinus1' requires a contingency table with at least two rows and columns");let n=e.length;if(e.some(w=>!(w>=0&&w<1/0)))throw new RangeError("all entries of 'x' must be non-negative and finite");if(n<2)throw new TypeError("'x' must have at least 2 elements");let r=mn(e);if(!(r>0))throw new RangeError("at least one entry of 'x' must be positive");let o=t.p===void 0?e.map(()=>1/n):Array.from(t.p);if(o.length!==n)throw new TypeError("'x' and 'p' must have the same number of elements");if(o.some(Number.isNaN))throw new RangeError("p contains missing values");if(o.some(w=>w<0))throw new RangeError("probabilities must be non-negative");if(o.some(w=>w===1/0))throw new RangeError("probabilities must be finite");if(t.rescaleP??!1){let w=mn(o),d=w<1/0?0:Math.ceil(Math.log2(n)),v=d===0?o:o.map(b=>D(b,-d)),N=d===0?w:mn(v);o=v.map(b=>b/N)}else if(Math.abs(mn(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(w=>D(w,-s)),c=s===0?r:mn(a),i=o.map(w=>c*w),l=s===0?i:i.map(w=>D(w,s));Gl(l);let f=o.map((w,d)=>i[d]*(1-w)),m=D(mn(a.map((w,d)=>Ot(w-i[d],i[d]))),s),u="Chi-squared test for given probabilities",h=n-1,p;if(t.simulatePValue??!1){let w=Ul(t);if(!Number.isInteger(r))throw new RangeError("the simulation needs counts summing to an integer");u+=bo(w),h=NaN,p=Kl(dp(r,o,l,w),m,w)}else p=Hl(m,h);return{method:u,statistic:m,statisticName:"X-squared",parameter:h,parameterName:"df",pValue:p,observed:e,expected:l,residuals:a.map((w,d)=>D((w-i[d])/Math.sqrt(i[d]),s/2)),stdres:a.map((w,d)=>D((w-i[d])/Math.sqrt(f[d]),s/2)),ftResiduals:Eo(e,l),warnings:Number.isFinite(h)&&l.some(w=>w<5)?[Bl]:[]}}function dp(e,t,n,r){let o=t.length;if(r*e>2**27)throw new RangeError("the simulation needs B * sum(x) <= 2^27 (134217728) draws");let s=dl(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 gp(e,t,n,r){let o=e.length,s=t.length,a=e.reduce((f,m)=>f+m,0);if(a>ms)throw new RangeError("the simulation needs a total count of at most 16777216 (2^24)");let c=us(a),i=new Int32Array(o*s),l=new Float64Array(Math.trunc(r));for(let f=0;f<l.length;f++){hs(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 Yl(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=mn(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=>mn(b)),l=e[0].map((b,M)=>mn(e.map(g=>g[M]))),f=i.map(b=>l.map(M=>cs(b,M,c)));Gl(f.flat());let m=e.map((b,M)=>b.map((g,y)=>g-f[M][y])),u="Pearson's Chi-squared test",h,p=(o-1)*(s-1),w,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(g=>g.some(y=>!Number.isInteger(y))))throw new RangeError("the simulation needs integer counts");let b=Ul(t);u+=bo(b),h=mn(m.flatMap((g,y)=>g.map((x,E)=>Ot(x,f[y][E])))),p=NaN;let M=gp(i.map(g=>Math.trunc(g)),l.map(g=>Math.trunc(g)),f,b);w=Kl(M,h,b)}else if(v>0)u+=" with Yates' continuity correction",h=mn(m.flatMap((b,M)=>b.map((g,y)=>Ot(Math.abs(g)-v,f[M][y]))));else if(h=mn(m.flatMap((b,M)=>b.map((g,y)=>Ot(g,f[M][y])))),n){u="Pearson's 'N-1' Chi-squared test";let b=h*(c-1);h=Number.isFinite(b)?b/c:h*((c-1)/c)}return{method:u,statistic:h,statisticName:"X-squared",parameter:p,parameterName:"df",pValue:w??Hl(h,p),observed:e,expected:f,residuals:m.map((b,M)=>b.map((g,y)=>g/Math.sqrt(f[M][y]))),stdres:wp(e,c,i,l,f,m),ftResiduals:Eo(e,f),warnings:Number.isFinite(p)&&f.some(b=>b.some(M=>M<5))?[...N,Bl]:N}}function wp(e,t,n,r,o,s){if(!Number.isFinite(t))return s.map((p,w)=>p.map((d,v)=>d/Math.sqrt(o[w][v]*(1-n[w]/t)*(1-r[v]/t))));let a=e.map(p=>Wl(p)),c=e[0].map((p,w)=>Wl(e.map(d=>d[w]))),i=vp(a),l=zl(a),f=zl(c),m=l.map(p=>it(p,i)),u=c.map(p=>it(p,i)),h=f.map(p=>it(p,i));return e.map((p,w)=>p.map((d,v)=>{let N=_e(a[w],u[v]),b=Pe([d,0],N),M=b[0]+b[1],g=_e(_e(N,m[w]),h[v]),y=g[0]+g[1];if(y>=bp||M===0)return M/Math.sqrt(y);let x=N[0]+N[1],E=m[w][0]+m[w][1],R=h[v][0]+h[v][1];return M/Math.sqrt(x)/Math.sqrt(E)/Math.sqrt(R)}))}var bp=2**-1022;function Wl(e){let t=[0,0];for(let n of e)t=ue(t,[n,0]);return t}function vp(e){let t=[0,0];for(let n of e)t=ue(t,n);return t}function zl(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 yp=Math.pow(Number.EPSILON,.25);function Pt(e,t,n,r={}){let o={tol:r.tol??yp,maxiter:r.maxIter??1e3};return r.fLower!==void 0&&(o.fLower=r.fLower),r.fUpper!==void 0&&(o.fUpper=r.fUpper),lr(e,t,n,o).root}function zn(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?Pt(n,r,s,{tol:2**-50,fLower:o,fUpper:a}):Pt(n,s,r,{tol:2**-50,fLower:a,fUpper:o});return Math.exp(c)}var Np=["alternative","confInt","confLevel","or","simulatePValue","B","midp"];function Ao(e,t={}){H(t,Np,"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(Zc));if(s.flat().some(f=>f>2147483647))throw new RangeError("'x' has entries too large to be integer");let a=[],c=Mp(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 Z("fisherTest",{...Ap(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 Z("fisherTest",{method:i+bo(f),alternative:l,pValue:Ep(s,f),warnings:a})}return Z("fisherTest",{method:i,alternative:l,pValue:Tp(s),warnings:a})}function Mp(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: ${gl(i)}`:null}function Ep(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=ue(l,[-ls(d),0]);let f=l[0]/(1+64*Number.EPSILON),m=s.reduce((d,v)=>d+v,0);if(m>ms)throw new RangeError("the simulation needs a total count of at most 16777216 (2^24)");let u=us(m),h=new Int32Array(c*i),p=0,w=Math.trunc(t);for(let d=0;d<w;d++){hs(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)/(w+1)))}var Xl=1+1e-7,xp=16,Rp=2**24;function xo(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 Pr=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%xp===0||!(s>0&&s<1/0)?(o=pn(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=xo(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=ue(r,[(this.lo+o)*n[o],0]));return r[0]}tail(t,n,r){if(n===1)return r?Jt(t-1,this.m,this.n,this.k,!1,!1):Jt(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?xo(o,s,o.length-1):xo(o,0,s)}midTail(t,n,r){if(n===1){let a=.5*pn(t,this.m,this.n,this.k,!1);return(r?Jt(t,this.m,this.n,this.k,!1,!1):Jt(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?xo(o,s+1,o.length-1):xo(o,0,s-1))+.5*o[s]}};function Wa(e,t,n){let r=e.density(n),o=r[t-e.lo]*Xl,s=[0,0];for(let a of r)a<=o&&(s=ue(s,[a,0]));return s[0]}function Ql(e,t,n,r){let o=Math.max(0,r-n),s=Math.min(r,t),a=w=>pn(w,t,n,r,!0),c=a(e)+Math.log(Xl),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=(w,d)=>{let v=w,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=(w,d)=>{let v=w-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:Jt(m,t,n,r,!0,!1),p=u>s?0:Jt(u-1,t,n,r,!1,!1);return h+p}function Ro(e,t){return zn(e,t)}function Ap(e,t,n){let r=Ze(t.alternative),o=t.or??1;if(!(o>=0))throw new RangeError("'or' must be a single number between 0 and Inf");let s=t.confLevel??.95;if(!(s>0&&s<1))throw new RangeError("confLevel must be a single number between 0 and 1");let[[a,c],[i,l]]=e,f=a,m=Math.min(a,l)+Math.min(c,i)+1;if(m>Rp)throw new RangeError(`the exact distribution of the 2 x 2 table has ${m} values, more than 2^24`);let u=new Pr(a+i,c+l,a+c),{lo:h,hi:p}=u,w=t.midp??!1,d;w?(r==="less"?d=u.midTail(f,o,!1):r==="greater"?d=u.midTail(f,o,!0):d=Math.min(1,2*Math.min(u.midTail(f,o,!1),u.midTail(f,o,!0))),n=r==="two.sided"?"Central Fisher's Exact Test (mid-p version)":"One-sided Fisher's Exact Test (mid-p version)"):r==="less"?d=u.tail(f,o,!1):r==="greater"?d=u.tail(f,o,!0):o===0?d=f===h?1:0:o===1/0?d=f===p?1:0:d=Wa(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:Ro(x=>u.mean(x)-f,y>f)}let N={method:n,alternative:r,pValue:d,estimate:v,estimateNames:"odds ratio",nullValue:o,nullValueName:"odds ratio",warnings:[]};if(!(t.confInt??!0))return N;let b=y=>{if(f===p)return 1/0;if(w){if(f===h&&y>=.5)return 0;let E=u.midTail(f,1,!1);return E===y?1:Ro(R=>u.midTail(f,R,!1)-y,E<y)}let x=u.tail(f,1,!1);return x===y?1:Ro(E=>u.tail(f,E,!1)-y,x<y)},M=y=>{if(f===h)return 0;if(w){if(f===p&&y>=.5)return 1/0;let E=u.midTail(f,1,!0);return E===y?1:Ro(R=>u.midTail(f,R,!0)-y,E>y)}let x=u.tail(f,1,!0);return x===y?1:Ro(E=>u.tail(f,E,!0)-y,x>y)},g;if(r==="less")g=[0,b(1-s)];else if(r==="greater")g=[M(1-s),1/0];else{let y=(1-s)/2;g=[M(y),b(y)]}return{...N,confInt:g,confLevel:s}}var As=[[0,0]];function Nn(e){for(let t=As.length;t<=e;t++)As.push(ue(As[t-1],Hc([t,0])));return As[e]}function Ts(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 $a=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 Tp(e){let t=e.filter(b=>b.some(M=>M>0));if(t.length===0)throw new RangeError("all entries of the table are zero");let n=t[0].map((b,M)=>t.some(g=>g[M]>0));if(t=t.map(b=>b.filter((M,g)=>n[g])),t.length<2||t[0].length<2)return 1;t.length>t[0].length&&(t=t[0].map((b,M)=>t.map(g=>g[M])));let r=t.length,o=t.map(b=>b.reduce((M,g)=>M+g,0)),s=t[0].map((b,M)=>t.reduce((g,y)=>g+y[M],0)).sort((b,M)=>M-b),a=s.length,c=o.reduce((b,M)=>b+M,0),i=Kc(Nn(c));for(let b of o)i=ue(i,Nn(b));for(let b of s)i=ue(i,Nn(b));let l=[0,0];for(let b of t.flat())l=Pe(l,Nn(b));let f=ue(l,[Math.log1p(1e-7),0]),m=1e-9*(1+Math.abs(f[0])),[u,h]=f,p=Array.from({length:a},()=>new Map),w=(b,M)=>{let g=M.join(","),y=p[b].get(g);if(y!==void 0)return y;let x=ue(i,Nn(M.reduce((_,L)=>_+L,0)));for(let _ of M)x=Pe(x,Nn(_));for(let _=b;_<a;_++)x=Pe(x,Nn(s[_]));let E={R:M,KZ:x,max:0,min:0,edges:[],pastHi:[],pastLo:[],count:[],index:new Map};if(p[b].set(g,E),b===a-1){let _=[0,0];for(let L of M)_=Pe(_,Nn(L));return E.max=E.min=_[0],E.edges.push({to:E,hi:_[0],lo:_[1],mult:1}),E}let R=new Map,A=new Array(r+1).fill(0);for(let _=r-1;_>=0;_--)A[_]=A[_+1]+M[_];let T=new Array(r).fill(0),k=(_,L,q)=>{if(_===r-1){T[_]=L;let O=Pe(q,Nn(L)),I=M.map((G,Q)=>G-T[Q]).sort((G,Q)=>G-Q),V=w(b+1,I),S=`${I.join(",")}|${O[0]}`,P=R.get(S);P===void 0?R.set(S,{to:V,hi:O[0],lo:O[1],mult:1}):P.mult++;return}let F=Math.max(0,L-A[_+1]),C=Math.min(M[_],L);for(let O=F;O<=C;O++)T[_]=O,k(_+1,L-O,Pe(q,Nn(O)))};if(k(0,s[b],[0,0]),E.edges=[...R.values()],E.max=dt(E.edges.map(_=>_.hi+_.to.max)),E.min=en(E.edges.map(_=>_.hi+_.to.min)),b===a-2){let _=E.edges.map(V=>{let S=V.to.edges[0];return{v:ue([V.hi,V.lo],[S.hi,S.lo]),mult:V.mult}});_.sort((V,S)=>V.v[0]-S.v[0]||V.v[1]-S.v[1]);let L=_.length,q=_[L-1].v[0],F=new Float64Array(L),C=new Float64Array(L),O=new Float64Array(L),I=[0,0];_.forEach(({v:V,mult:S},P)=>{F[P]=V[0],C[P]=V[1];let G=Pe(V,[q,0]);I=ue(I,[S*Math.exp(G[0])*(1+G[1]),0]),O[P]=I[0]}),E.dist={hi:F,lo:C,cum:O,vmax:q,kv:ue(i,[q,0])}}return E},d=w(0,[...o].sort((b,M)=>b-M)),v=new $a,N=(b,M,g,y)=>{if(M+b.max<=u-m){let[k,_]=Ts(b.KZ[0],b.KZ[1],M,g);return v.add(k,_,y),!0}if(M+b.min>u+m)return!0;let x=b.dist;if(x===void 0)return!1;let[E,R]=Ts(u,h,-M,-g),A=0,T=x.hi.length;for(;A<T;){let k=A+T>>>1,_=x.hi[k];_<E||_===E&&x.lo[k]<=R?A=k+1:T=k}if(A>0){let[k,_]=Ts(x.kv[0],x.kv[1],M,g);v.add(k,_,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 M of p[b].values()){let{pastHi:g,pastLo:y,count:x}=M;if(x.length!==0){for(let{to:E,hi:R,lo:A,mult:T}of M.edges)for(let k=0;k<x.length;k++){let[_,L]=Ts(g[k],y[k],R,A),q=x[k]*T;if(N(E,_,L,q))continue;let F=E.index.get(_);F===void 0?(E.index.set(_,E.count.length),E.pastHi.push(_),E.pastLo.push(L),E.count.push(q)):E.count[F]+=q}M.pastHi=M.pastLo=M.count=[],M.index.clear()}}return Math.min(1,v.value())}function Ss(e){let t=e.every(n=>Number.isInteger(n))?1:2;return[t,e.map(n=>n*t).sort((n,r)=>n-r)]}function Ba(e){return[...e].sort((t,n)=>t-n).join(",")}function za(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 Ua(e){let[,t]=Ss(e),n=0,r=0;for(let o of t)r+=n+=o;return{steps:r,size:n+1}}function Ka(e){let[t,n]=Ss(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 za(t,0,o)}function Ha(e,t){let[,n]=Ss(e),r=n.length,o=Math.min(t,r-t),s=Zl(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 Zl(e){let t=[0];for(let n of e)t.push(t[t.length-1]+n);return t}function Ga(e,t){let[n,r]=Ss(e),o=r.length,s=Math.min(t,o-t),a=Zl(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 w=Math.min(h,s);w>=Math.max(1,s-(o-h));w--){let d=c[w],v=c[w-1],N=w/h,b=(h-w)/h,M=a[w],g=a[h]-a[h-w],y=a[w-1]+p,x=Math.max(M,y);for(let E=g;E>=x;E--)d[E-M]=b*d[E-M]+N*v[E-y];for(let E=x-1;E>=M;E--)d[E-M]=b*d[E-M]}}let i=c[s],l=t*(t+1)/2;if(s===t)return za(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 za(n,(a[o]-u)/n-l,m)}function Bn(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 ur(e,t){let n=Bn(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 Jl=["alternative","mu","paired","exact","correct","confInt","confLevel","tolRoot"];function To(e,t,n={}){if(typeof e!="string"&&xt(t)&&H(t,Jl,"wilcoxTest"),H(n,Jl,"wilcoxTest"),typeof e=="string"){if(n.paired!==void 0)throw new Error("cannot use 'paired' in formula method");let w=ds(e,t,!0);return w.kind==="one"?To(w.x,n):To(w.x,w.y,w.kind==="paired"?{...n,paired:!0}:n)}let r;t!==void 0&&!ArrayBuffer.isView(t)&&!Array.isArray(t)?n=t:r=t;let o=Ze(n.alternative),s=n.mu??0,a=n.paired??!1,c=n.exact??null,i=kp(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[w,d]=$n(Array.from(e),Array.from(r));u=Ct(w.map((v,N)=>v-d[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:i,confInt:l,confLevel:f,tolRoot:m,warnings:new ft};return Z("wilcoxTest",h===void 0?Cp(u,c,a,p):Wp(u,h,c,p))}function kp(e){if(typeof e=="boolean")return e?0:-1;if(!(Number.isInteger(e)&&e>=0&&e<=3))throw new RangeError("'correct' must be an integer between 0 and 3");return e}function ks(e,t,n){return n?t==="two.sided"?Math.sign(e)*.5:t==="greater"?.5:-.5:0}var nf=10*Number.EPSILON;function rf(e,t,n=0){let r=i=>Math.min(1,Math.max(i,0)),o=()=>n===0?ce(e):r(ce(e)-n*Ut(e)),s=()=>n===0?ce(e,{lowerTail:!1}):r(ce(e,{lowerTail:!1})+n*Ut(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 qp(e){let t=e*e;return[e*(t-3),e*(t*(t-10)+15),e*(t*(t*(t-21)+105)-105)]}function Ip(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 of(e,o,s,n)}function _p(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 of(e,a,c,r)}function of(e,t,n,r){let[o,s,a]=qp(e),c=t*o;return r>=2&&(c+=n*s),r>=3&&(c+=t*t/2*a),c}function Qa(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 Za=(e,t)=>e-t,Ya=5e9,Lp=40;function Xa(e){return new RangeError(`wilcoxTest: the exact conditional ${e} would take too long or too much memory: use exact: false`)}function qs(e,t){if(e.size>16777216||e.steps>Ya)throw Xa(t);return e.steps}function sf(e){let t=e.length,n=vt(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 hn(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 Cp(e,t,n,r){let{alternative:o,mu:s}=r,a=e.map(g=>g-s);a.some((g,y)=>!Number.isFinite(g)&&Number.isFinite(e[y]))&&(a=e.map(g=>g/2-s/2));let i=a.some(g=>g===0),l=t??a.length<50,f=(g,y,x)=>({method:g,alternative:o,statistic:y,statisticName:"V",pValue:x,nullValue:s,nullValueName:n?"location shift":"location",warnings:r.warnings.list});if(l){let g=vt(a.map(Math.abs)),y=hn(g).length>0,x=0;for(let _=0;_<a.length;_++)a[_]>0&&(x+=g[_]);let E="Wilcoxon signed rank exact test";if(!y&&!i){let _=a.length,L=_*(_+1)/2,q;if(o==="less")q=qr(x,_,!0,!1);else if(o==="greater")q=qr(x-1,_,!1,!1);else{let C=x>L/2?qr(x-1,_,!1,!1):qr(x,_,!0,!1);q=Math.min(1,2*C)}let F=f(E,x,q);return r.confInt?{...F,...Op(e,r)}:F}let R=g.filter((_,L)=>a[L]!==0);qs(Ua(R),"distribution");let A=Ka(R),T;if(o==="less")T=Bn(A,x);else if(o==="greater")T=ur(A,x-.25);else{let _=0;for(let L of R)_+=L/2;T=Math.min(1,2*(x>_?ur(A,x-.25):Bn(A,x)))}let k=f(E,x,T);return r.confInt?{...k,...Dp(e,r)}:k}let m=i?a.filter(g=>g!==0):a,u=m.length;if(u===0)throw new RangeError("no non-zero differences for the signed rank test");let[h,p,w]=sf(m),v=hn(vt(m.map(Math.abs))).length>0||i?Math.min(r.correct,0):r.correct,N=(p-ks(p,o,v>=0))/w,b=rf(N,o,v>=1?Ip(N,u,v):0),M=f(`Wilcoxon signed rank test${r.correct>=0?" with continuity correction":""}`,h,b);return r.confInt?{...M,...Vp(e,r)}:M}function Ja(e,t,n){return e==="less"?[-1/0,n]:e==="greater"?[t,1/0]:[t,n]}function ef(e,t,n){let r={confInt:Ja(e,NaN,NaN),confLevel:0,estimate:t};return n!==void 0&&(r.estimateNames=n),r}function af(e){if(!(e>0&&e<1/0))throw new RangeError("tolRoot must be a positive finite number")}function cf(e){let t=e.length,n=[];for(let r=0;r<t;r++)for(let o=r;o<t;o++){let s=Xt(e[r],e[o]);Number.isNaN(s)||n.push(s)}return n.sort(Za)}function lf(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+nf&&l++;let f=Qa(e);if(l===0)return{confInt:[-1/0,1/0],confLevel:1,estimate:f,estimateNames:o};let m=t(l-1);return{confInt:Ja(s,e[l-1],e[c-l]),confLevel:1-(s==="two.sided"?2*m:m),estimate:f,estimateNames:o}}function Op(e,t){let n=e.length;return lf(cf(e),r=>qr(r,n,!0,!1),r=>al(r,n,!0,!1),t,"(pseudo)median")}function ff(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 mf(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)+nf,w=f!=="less",d=f!=="greater",v=q=>Xt(e[q-1],e[q]),N=Math.min(u-1,Math.max(1,Math.ceil(a.mean+pe(Math.min(p,1))*a.sd+.1*a.sd+3))),b=8,M=q=>q===0?e[0]-1:v(q),g=q=>q===0?e[u-1]+1:v(u-q),y=[new Map,new Map];{let q=new Map,F=0,C=Lp*c;for(let[O,I]of[[0,M],[1,g]])if(!(O===0?!w:!d))for(let V=0;V<=N;V++){if(F+=C,F>Ya)throw Xa("interval");let S;try{S=t(I(V))}catch{break}if(y[O].set(V,[S.stat,S.key]),q.has(S.key)){q.delete(S.key),q.set(S.key,!0);continue}if(F+=r(S.scores),F>Ya)throw Xa("interval");q.set(S.key,!0),q.size>b&&q.delete(q.keys().next().value)}}let x=new Map,E=(q,F)=>{let C=q===0?M(F):g(F),O=y[q].get(F),I;if(O===void 0){let G=t(C);O=[G.stat,G.key],I=G.scores}let[V,S]=O,P=x.get(S);return P!==void 0?x.delete(S):(P=n(I??t(C).scores),x.size>=b&&x.delete(x.keys().next().value)),x.set(S,P),[V,P]},R=q=>{let[F,C]=E(0,q);return o(C,F)},A=q=>{let[F,C]=E(1,q);return s(C,F)},T=-1/0,k=0;if(w){let q=R(0);if(!(q>p)){let F=q,C=1;for(;C<=u-1;C++){let O=R(C);if(O>p)break;F=O}T=e[C-1],k=F}}let _=1/0,L=0;if(d){let q=A(0);if(!(q>p)){let F=q,C=u-1;for(;C>=1;C--){let O=A(u-C);if(O>p)break;F=O}_=e[C],L=F}}return{confInt:[T,_],confLevel:1-(k+L),estimate:Qa(e),estimateNames:l}}function Fp(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 Dp(e,t){let n=e.length,r=s=>{let a=Fp(e,s),{ranks:c,key:i}=ff(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 mf(cf(e),r,Ka,s=>qs(Ua(s),"interval"),(s,a)=>ur(s,a-.25),Bn,{mean:o/2,sd:Math.sqrt(n*(n+1)*(2*n+1)/24)},n,t,"(pseudo)median")}function Vp(e,t){let{alternative:n,confLevel:r,tolRoot:o}=t,s=t.correct>=0,a=e.length;if(a===0)return ef(n,NaN);let c=en(e),i=dt(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"),ef(n,Xt(c,i),"midrange");let l=(b,M)=>{let[,g,y]=sf(e.map(x=>x-b).filter(x=>x!==0));return(g-ks(g,n,M))/y},f=l(c,s),m=l(i,s),u=b=>-pe(b),h=b=>n==="greater"?!(f<u(b)):n==="less"?!(m>pe(b/2)):!(f<u(b/2))&&!(m>pe(b/2)),p=1-r,w=!1;for(;!h(p)&&p<1;)p*=2,w=!0;w&&t.warnings.add("requested conf.level not achievable"),af(o);let d=b=>Pt(M=>l(M,s)-b,c,i,{tol:o,fLower:f-b,fUpper:m-b}),v;if(p>=1){let b=Qa([...e].sort(Za));v=Ja(n,b,b)}else n==="greater"?v=[d(u(p)),1/0]:n==="less"?v=[-1/0,d(pe(p))]:v=[d(u(p/2)),d(pe(p/2))];let N=Pt(b=>l(b,!1),c,i,{tol:o,fLower:l(c,!1),fUpper:l(i,!1)});return{confInt:v,confLevel:w?1-Math.min(1,p):r,estimate:N,estimateNames:"(pseudo)median"}}function uf(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 hn(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 Pp(e,t){let[n,r,o]=uf(vt([...e,...t]),e.length);return[n,r,o]}function jp(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 $p(e,t,n){return hf(e,t,n).ranks}function hf(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=vt([...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:Ba(a)}}if(Number.isFinite(n)&&r.some((o,s)=>!Number.isFinite(o)&&Number.isFinite(e[s]))){let o=jp(e.map(s=>s/2-n/2),t);return{ranks:o,key:Ba(o)}}return ff([...r,...t])}function Wp(e,t,n,r){let{alternative:o,mu:s}=r,a=e.length,c=t.length,i=n??(a<50&&c<50),l=$p(e,t,s),[f,m,u,h]=uf(l,a),p,w;if(i)if(w="Wilcoxon rank sum exact test",!h)p=tf(()=>o==="less"?Ir(f,a,c,!0,!1):o==="greater"?Ir(f-1,a,c,!1,!1):Math.min(2*Ir(f,a,c,!0,!1),2*Ir(f-1,a,c,!1,!1),1));else{qs(Ha(l,a),"distribution");let N=Ga(l,a);o==="less"?p=Bn(N,f):o==="greater"?p=ur(N,f-.25):p=Math.min(2*Bn(N,f),2*ur(N,f-.25),1)}else{w=`Wilcoxon rank sum test${r.correct>=0?" with continuity correction":""}`;let N=h?Math.min(r.correct,0):r.correct,b=l.every(g=>g===l[0]),M=(m-ks(m,o,N>=0))/u;p=b?1:rf(M,o,N>=1?_p(M,a,c,N):0)}let d={method:w,alternative:o,statistic:f,statisticName:"W",pValue:p,nullValue:s,nullValueName:"location shift",warnings:r.warnings.list};if(!r.confInt)return d;let v=i?h?Bp(e,t,r):tf(()=>zp(e,t,r)):Up(e,t,r);return{...d,...v}}function tf(e){try{return e()}catch(t){throw t instanceof RangeError&&t.message.startsWith("wilcox:")?new RangeError(`${t.message}: use exact: false`):t}}function pf(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(Za)}function zp(e,t,n){let r=e.length,o=t.length;return lf(pf(e,t,!1),s=>Ir(s,r,o,!0,!1),s=>cl(s,r,o,!0,!1),n,"difference in location")}function Bp(e,t,n){let r=e.length,o=t.length,s=a=>{let{ranks:c,key:i}=hf(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 mf(pf(e,t,!0),s,a=>Ga(a,r),a=>qs(Ha(a,r),"interval"),(a,c)=>ur(a,c-.25),Bn,{mean:r*o/2,sd:Math.sqrt(r*o*(r+o+1)/12)},r+o,n,"difference in location")}function Up(e,t,n){let{alternative:r,confLevel:o,tolRoot:s}=n,a=n.correct>=0,c=en(e)-dt(t),i=dt(e)-en(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[,w,d]=Pp(e.map(v=>v-h),t);return(w-ks(w,r,p))/d},f=(h,p)=>{let w=l(c,p)-h,d=l(i,p)-h;if(Number.isNaN(w)||Number.isNaN(d))throw new Error("cannot compute the confidence interval because all observations are tied");return w<=0?c:d>=0?i:(af(s),Pt(v=>l(v,p)-h,c,i,{tol:s,fLower:w,fUpper:d}))},m=1-o,u;return r==="greater"?u=[f(-pe(m),a),1/0]:r==="less"?u=[-1/0,f(pe(m),a)]:u=[f(-pe(m/2),a),f(pe(m/2),a)],{confInt:u,confLevel:o,estimate:f(0,!1),estimateNames:"difference in location"}}var Kp=["method","n"],df=["holm","hochberg","hommel","bonferroni","BH","BY","fdr","none"];function Hp(e){if(df.includes(e))return e;let t=df.filter(n=>n.startsWith(e));if(e.length===0||t.length!==1)throw new RangeError(`invalid method "${e}"`);return t[0]}function jr(e,t={}){H(t,Kp,"pAdjust");let n=typeof t=="string"?{method:t}:t,r=Hp(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=wf(i,r,c),f=o.slice();return s.forEach((m,u)=>f[m]=l[u]),f}function Is(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 wf(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=Is(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=Is(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=kn(),u=Math.floor(n);if(u<=1e6){for(let h=1;h<=u;h++)an(m,1/h,u);s=qn(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=Is(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?wf(e,"hochberg",n):Gp(e,n)}}var gf=2**16;function Gp(e,t){if(!Number.isInteger(t))throw new RangeError("n must be an integer for the hommel method");if(t>gf)throw new RangeError(`n must be at most ${gf} for the hommel method (O(n^2) work)`);let n=e.length,r=[...e,...new Array(t-n).fill(1)],o=Is(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 bf=["p","alternative","confLevel"],Yp=1+1e-7;function ei(e,t,n){let r=e-1;return e===0||e-r===1&&zt(t,-r)[1]===0?Sn(r,t,n,!1,!1):Sn(e,t,n,!1,!1)+sn(e,t,n,!1)}function Xp(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=sn(e,t,n,!1)*Yp,s=i=>sn(i,t,n,!1)<=o;if(e<r){let i=rs(s,Math.ceil(r),t),l=i>t?0:ei(i,t,n);return Math.min(1,Sn(e,t,n,!0,!1)+l)}let a=os(s,0,Math.floor(r)),c=a<0?0:Sn(a,t,n,!0,!1);return Math.min(1,ei(e,t,n)+c)}function Qp(e,t,n={}){H(t,bf,"binomTest"),H(n,bf,"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),!dn(e[0])||!dn(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(!dn(e))throw new RangeError("'x' must be nonnegative and integer");if(r=Math.round(e),!(dn(t)&&Math.round(t)>=r&&t>0))throw new RangeError("'n' must be a positive integer >= 'x'");o=Math.round(t)}return ti(r,o,n)}function ti(e,t,n={}){let r=n.p??.5;if(!(r>=0&&r<=1))throw new RangeError("'p' must be a single number between 0 and 1");let o=Ze(n.alternative),s=n.confLevel??.95;qt(s);let a;o==="less"?a=Sn(e,t,r,!0,!1):o==="greater"?a=ei(e,t,r):a=Xp(e,t,r);let c=f=>e===0?0:Vn(f,e,t-e+1,!0,!1),i=f=>e===t?1:f<1e-8?Vn(f,e+1,t-e,!1,!1):Vn(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 Zp=["T","r","alternative","confLevel"],Jp=1+1e-7;function ed(e,t){if(t===0)return e===0?1:0;if(e===t)return 1;let n=ss(e,t,!1)*Jp,r=a=>ss(a,t,!1)<=n,o=Math.floor(t);if(e<t){let a=rs(r,Math.max(e+1,o),1/0);return Math.min(1,kr(e,t,!0,!1)+ni(a,t))}let s=os(r,0,Math.min(e-1,o));return Math.min(1,kr(s,t,!0,!1)+ni(e,t))}var So=2**-1022;function ni(e,t){return e-(e-1)===1?kr(e-1,t,!1,!1):kr(e,t,!1,!1)+ss(e,t,!1)}function hr(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 td(e,t,n,r){let o=n+t*r,s=e*t*n/o;if(Number.isFinite(s)&&Number.isFinite(o)&&o>=So&&(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]=hr(e),[f,m]=hr(t),[u,h]=hr(n),[p,w]=hr(r),d=f*p,v=h-m-w,N=v>=0?u/(u+D(d,-v)):u/(D(u,v)+d);return D(i*f*N,l+m+Math.min(v,0))}function nd(e,t,n,r){let o=e/n,s=t/r,a=o/s;return t===0||Number.isFinite(o)&&Number.isFinite(s)&&s>=So&&(o===0||o>=So)?a:e===0?0:e/t*(r/n)}function rd(e,t={}){H(t,Zp,"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(dn))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=Ze(t.alternative),l=t.confLevel??.95;if(o===2?qt(l):tn(l),o===2){let[v,N]=r,[b,M]=a,g=v+N;if(g===1/0)throw new RangeError("'x' must be finite, nonnegative, and integer");let y=c*b,x=y+M,E=Number.isFinite(x)&&x>=So&&(y===0||y>=So)?y/x:1/(1+M/b/c),R=ti(Math.round(v),Math.round(v)+Math.round(N),{p:E,alternative:i,confLevel:l}),A=M/b,T=k=>{if(k===0)return 0;let _=k/(1-k);if(Number.isFinite(A)||_===1/0)return _*A;let[L,q]=hr(_),[F,C]=hr(b),[O,I]=hr(M);return D(L*O/F,q+I-C)};return{method:"Comparison of Poisson rates",alternative:i,statistic:v,statisticName:"count1",parameter:td(g,c,b,M),parameterName:"expected count1",pValue:R.pValue,confInt:[T(R.confInt[0]),T(R.confInt[1])],confLevel:l,estimate:nd(v,N,b,M),estimateNames:"rate ratio",nullValue:c,nullValueName:"rate ratio",warnings:[]}}let f=r[0],m=a[0],u=c*m,h;i==="less"?h=kr(f,u,!0,!1):i==="greater"?h=ni(f,u):h=ed(f,u);let p=v=>f===0?0:ns(v,f,1,!0,!1),w=v=>v<1e-8?ns(v,f+1,1,!1,!1):ns(1-v,f+1,1,!0,!1),d;if(i==="less")d=[0,w(1-l)];else if(i==="greater")d=[p(1-l),1/0];else{let v=(1-l)/2;d=[p(v),w(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 vf=["p","alternative","confLevel","correct"],od="Chi-squared approximation may be incorrect";function ri(e,t,n={}){xt(t)&&H(t,vf,"propTest"),H(n,vf,"propTest");let r,o,s=n;if(typeof e!="number"&&e.length>0&&Array.isArray(e[0])){let T=e;if(T.some(k=>k.length!==2))throw new RangeError("'x' must have 2 columns");r=T.map(k=>k[0]),o=T.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 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,k)=>T>o[k]))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(k=>T[k]),l.some(k=>!(k>0&&k<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;qt(m);let u=Ze(s.alternative);(i>2||i===2&&f)&&(u="two.sided");let h=r.map((T,k)=>T/o[k]),p=i===2?h[0]-h[1]:0,w=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)/w;T<=d&&([d,v]=[T,!0])}let N=sd(r,o),b=o.some((T,k)=>{let _=l?.[k]??N;return T*_<5||T*(1-_)<5}),M=0;if(!v)for(let T=0;T<i;T++){let k=l?.[T]??N,_=o[T]*k,L=o[T]*(1-k);M+=Ot(Math.abs(r[T]-_)-d,_),M+=Ot(Math.abs(o[T]-r[T]-L)-d,L)}let g=l!==void 0?i:i-1,y;if(u==="two.sided")y=bt(M,g/2,2,!1,!1);else{let T=Math.sign(i===1?h[0]-l[0]:p);y=ce(T*Math.sqrt(M),{lowerTail:u==="less"})}let x;if(i===1||i===2&&!f){let T=(1-m)/2,k=u==="two.sided"&&T<1e-8?pe(T,{lowerTail:!1}):pe(u==="two.sided"?(1+m)/2:m);if(i===1){let _=o[0],L=k*k,q=(V,S)=>(V+L/(2*_)+S*k*Math.sqrt(V*(1-V)/_+L/(4*_*_)))/(1+L/_),F=h[0]+d/_,C=h[0]-d/_,O=Math.min(1,Math.max(0,F>=1?1:q(F,1))),I=Math.min(1,Math.max(0,C<=0?0:q(C,-1)));x=u==="two.sided"?[I,O]:u==="greater"?[I,1]:[0,O]}else{let _=Math.sqrt(h.reduce((C,O,I)=>C+O*(1-O)/o[I],0)),L=k*_+d*w,q=Math.max(-1,p-L),F=Math.min(1,p+L);x=u==="two.sided"?[q,F]:u==="greater"?[q,1]:[-1,F]}}let E=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:M,statisticName:"X-squared",parameter:g,parameterName:"df",pValue:y,estimate:i===1?h[0]:h,estimateNames:E,warnings:b?[od]:[]};return l!==void 0&&(A.nullValue=i===1?l[0]:l,A.nullValueName=E),x!==void 0&&(A.confInt=x,A.confLevel=m),Z("propTest",A)}function sd(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+D(s,-r),0)/t.reduce((o,s)=>o+D(s,-r),0)}var ad=["method","confLevel","sides"],id=["wilson","wald","wilsoncc","agresti-coull","jeffreys","clopper-pearson","midp"],cd=["two.sided","left","right"];function ko(e,t){let n=Ie(t,cd),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{...oi(s,o),sides:n}}function oi(e,t=1-e){let n=e/2<1e-8,r=n?pe(e/2,{lowerTail:!1}):pe(1-e/2),o=n?uo(e,1,{lowerTail:!1}):uo(t,1);return{sides:"two.sided",level:t,alpha:e,kappa:r,chisq:o}}function $r(e,t){if(typeof e!="number")throw new TypeError(`'${t}' must be a number`);if(!dn(e))throw new RangeError(`'${t}' must be a nonnegative integer`);return Math.round(e)+0}function _n(e,t,n,r){let o=$r(e,n),s=$r(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 Ls(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 _s(e,t,n,r){return r?e/2<1e-8?Vn(e/2,t,n,!1,!1):Vn(1-e/2,t,n,!0,!1):Vn(e/2,t,n,!0,!1)}function yf(e,t,n,r){let o=n/2,s=r?f=>{let m=Math.exp(f);return .5*sn(e,t,m,!1)+Sn(e-1,t,m,!0,!1)-o}:f=>{let m=Math.exp(f);return .5*sn(e,t,m,!1)+Sn(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:Pt(w=>a*s(w),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(Pt(f=>a*s(f),c,0,{tol:2**-60,fLower:i,fUpper:l}))}function si(e,t,n={}){H(n,ad,"binomCI");let r=Ie(n.method,id),o=ko(n.confLevel,n.sides),[s,a]=_n(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]=Ls(s,a,i);break;case"wilsoncc":{let h=i*i,p=2*(a+h),w=2*s+h-1,d=i*Math.sqrt(h-2-1/a+4*l*(a*f+1));m=l===0?0:w-d<w/2?(2*s-1)/(2*a)*(2*s-1)/(w+d):(w-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,w=(s+h/2)/p,d=i*Math.sqrt(w*(1-w)/p);[m,u]=[w-d,w+d];break}case"jeffreys":m=s===0?0:_s(c,s+.5,a-s+.5,!1),u=s===a?1:_s(c,s+.5,a-s+.5,!0);break;case"clopper-pearson":m=s===0?0:_s(c,s,a-s+1,!1),u=s===a?1:_s(c,s+1,a-s,!0);break;case"midp":m=s===0?0:s===a?Math.exp(Math.log(c)/a):yf(s,a,c,!1),u=s===a?1:s===0?-Math.expm1(Math.log(c)/a):yf(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),Z("binomCI",{estimate:l,confInt:[m,u],confLevel:n.confLevel??.95,method:r,sides:o.sides,warnings:[]})}function qo(e){return e===1/0?1:e/(1+e)}function Io(e,t){return e===0?0:t>0?e*e/t:1/0}function ld(e,t,n,r){return e/t+n/r>1}function fd(e,t,n,r){return Os(e,t,n,r,0)}function ci(e,t,n,r,o){if(ld(e,t,n,r)){let[p,w,d,v]=ci(t-e,t,r-n,r,-o);return[w,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},w=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=md(e,t,n,r,o);u=s?b:b-o,u>v&&u<N||(u=N/2);for(let M=0;M<2e3;M++){let g=p(u);if(g===0)break;g>0?v=u:N=u;let y=u-g/w(u);if(y>v&&y<N||(y=v===0?N/2:N>2*v?Math.sqrt(v*N):v+(N-v)/2),!(y>v&&y<N))break;let x=Math.abs(y-u);if(u=y,x<=2**-53*u)break}}}let h=[u,1-u,Math.min(1,u+f),Math.max(0,m-u)];return s?h:[h[2],h[3],h[0],h[1]]}function md(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 w=(Math.PI+Math.acos(Math.max(-1,Math.min(1,h/(p*p*p)))))/3;return 2*p*Math.cos(w)-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]=ci(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 ud(e,t,n,r,o,s,a){let c=Pe(Pe(ct([e,0],t),ct([n,0],r)),[o,0]),[i,l,f,m]=ci(e,t,n,r,o),u=ue(ct(bn(i,l),t),ct(bn(f,m),r));return s==="mn"&&(u=ct(Tt(u,t+r),t+r-1)),Pe(_e(c,c),Tt(u,a))[0]}function Os(e,t,n,r,o){return Pe(Pe(ct([e,0],t),ct([n,0],r)),[o,0])[0]}function hd(e,t,n,r,o,s){return Io(Os(e,t,n,r,o),li(e,t,n,r,o,s))}var Cs=new Float64Array(1),ai=new BigInt64Array(Cs.buffer);function Nf(e){return Cs[0]=Math.abs(e),e<0?-ai[0]:ai[0]}function Un(e){return ai[0]=e<0n?-e:e,e<0n?-Cs[0]:Cs[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=Nf(a?r:n),i=Nf(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(Un(f)),m===0)return Un(f);if(m<0!=l<0||f===c)break;if(i=f,l=m,u*=2n,u>1n<<40n)return Un(i)}if(m<0==l<0||Number.isNaN(m))return Un(i);for(;f-i>1n||i-f>1n;){let h=(i+f)/2n,p=e(Un(h));if(p===0)return Un(h);p<0==l<0?(i=h,l=p):(f=h,m=p)}return Math.abs(m)<Math.abs(l)?Un(f):Un(i)}function Ef(e,t,n,r,o,s){let a=fd(e,t,n,r);if(o===0)return[a,a];let c=qo(o),i=p=>(p===a?0:qo(hd(e,t,n,r,p,s)))-c,l=(p,w,d,v)=>{if(d===0)return p;if(v===0)return w;let N=Pt(i,p,w,{tol:1e-300,fLower:d,fUpper:v});return s==="wald"?N:ii(b=>ud(e,t,n,r,b,s,o),N,p,w,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 Mf(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?Pt(e,r,a,{tol:2**-60,fLower:o,fUpper:c}):Pt(e,a,r,{tol:2**-60,fLower:c,fUpper:o});return Math.exp(i)}r=a,o=c,s*=2}}function pd(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 dd(e,t,n,r,o){let[s,a]=pd(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?zt(1,-y):ct(zt(1,-y),o),f=c===3?1/o:1,m=c===0?Math.min(1,1/o):1,u=c===2?o>1?ct(zt(o,-1),o)[0]:0:c===3&&o<1?zt(1,-o)[0]:0,h=y=>{let x=l(y),E=Tt(x,o);return[E[0],Pe([1,0],E)[0],x[0],Pe([1,0],x)[0]]},p=e+n,w=c<2?1:-1,d=y=>{let[,x,E,R]=h(y),A=0,T=0;return p>0&&(A+=E>0?p/E:1/0,T-=p/(E*E)),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)),[w*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 E=0;E<2e3;E++){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 k=Math.abs(T-v);if(v=T,k<=2**-53*v)break}}let[N,b,M,g]=h(v);return[Math.min(1,Math.max(0,N)),Math.min(1,Math.max(0,b)),Math.min(1,Math.max(0,M)),Math.min(1,Math.max(0,g))]}function gd(e,t,n,r,o){let[s,a,c,i]=dd(e,t,n,r,o),l=(f,m,u,h)=>u>.5?m*h-(m-f):f-m*u;return Io(l(e,t,s,a),t*s*a)+Io(l(n,r,c,i),r*c*i)}function xf(e,t,n,r,o){let s=e/t/(n/r);return Af(s,o,a=>gd(e,t,n,r,a))}function wd(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 bd(e,t,n,r,o){let[s,a]=wd(e,t,n,r,o),c=e-t*s;return Io(c,t*s*(1-s))+Io(c,r*a*(1-a))}function Rf(e,t,n,r,o){let s=e*(r-n)/((t-e)*n);return Af(s,o,a=>bd(e,t,n,r,a))}function Af(e,t,n){if(t===0)return[e,e];let r=qo(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=>qo(n(u))-r,f=l(1),m=f===0?1:zn(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:qo(n(Math.exp(l))))-r;return[s(Mf(i,c,-1)),a(Mf(i,c,1))]}var vd=["method","confLevel","sides"],yd=["method","confLevel","sides"],Nd=["method","confLevel"],Md=["ac","wald","score","mn","mee"];function fi(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,w=a*Math.sqrt(u*(1-u)/(t+2)+h*(1-h)/(r+2));[f,m]=[Math.max(-1,p-w),Math.min(1,p+w)];break}case"score":{let[u,h]=Ls(e,t,a),[p,w]=Ls(n,r,a);f=l-a*Math.sqrt(u*(1-u)/t+w*(1-w)/r),m=l+a*Math.sqrt(h*(1-h)/t+p*(1-p)/r);break}case"mn":case"mee":[f,m]=Ef(e,t,n,r,s.chisq,o);break}return[Math.min(f,m),Math.max(f,m)]}function Ed(e,t,n,r,o={}){H(o,vd,"binomDiffCI");let s=Ie(o.method,Md),a=ko(o.confLevel,o.sides),[c,i]=_n(e,t,"x1","n1"),[l,f]=_n(n,r,"x2","n2"),m=c/i-l/f,[u,h]=fi(c,i,l,f,s,a);return a.sides==="left"?h=1:a.sides==="right"&&(u=-1),Z("binomDiffCI",{estimate:m,confInt:[u,h],confLevel:o.confLevel??.95,method:s,sides:a.sides,warnings:[]})}var xd=["katz.log","koopman"];function Fs(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 Rd(e,t,n,r,o={}){H(o,yd,"binomRatioCI");let s=Ie(o.method,xd),a=ko(o.confLevel,o.sides),[c,i]=_n(e,t,"x1","n1"),[l,f]=_n(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,Fs(.5,i,l,f,m)[1]]:l===0?[h,p]=[Fs(c,i,.5,f,m)[0],1/0]:c===i&&l===f?[h,p]=Fs(i-.5,i,f-.5,f,m):[h,p]=Fs(c,i,l,f,m):[h,p]=xf(c,i,l,f,a.chisq),a.sides==="left"?p=1/0:a.sides==="right"&&(h=0),Z("binomRatioCI",{estimate:u,confInt:[h,p],confLevel:o.confLevel??.95,method:s,sides:a.sides,warnings:[]})}var Ad=["midp","fisher","wald","score"],Td={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"},Sd=2**23,kd=2**20;function Tf(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 qd(e,t={}){H(t,Nd,"oddsRatio");let n=Ie(t.method,Ad);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}=ko(r,void 0),c=e.map((T,k)=>Array.from(T,(_,L)=>$r(_,`table[${k}][${L}]`))),[[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),w=p-h+1;if(n==="midp"||n==="fisher"){if(w>Sd)throw new RangeError(`the exact distribution of the 2 x 2 table has ${w} values, more than 2^23: use method "wald" or "score"`);if(n==="fisher"&&Math.max(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=Jt(i-1,i+f,l+m,i+l,!0,!1)+.5*pn(i,i+f,l+m,i+l,!1),v=Jt(i,i+f,l+m,i+l,!1,!1)+.5*pn(i,i+f,l+m,i+l,!1),N=w<=kd?Wa(new Pr(i+f,l+m,i+l),i,1):Ql(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),M=[i+l,f+m],g=[i+f,l+m],y=0,x=!1;for(let T=0;T<2;T++)for(let k=0;k<2;k++){let _=M[k]*g[T]/u;_<5&&(x=!0);let L=Math.abs(c[k][T]-_);y+=L*L/_}let E=bt(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=tt(()=>Ao(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]:Rf(i,i+l,f,f+m,a);break}case"midp":{let T=new Pr(i+f,l+m,i+l),k=F=>{if(F===1)return[d,v];let C=T.density(F),O=i-h,I=.5*C[O];return[Tf(C,0,O-1)+I,Tf(C,O+1,C.length-1)+I]},_=(F,C)=>{let O=F(1);return O===0?1:zn(F,C?O>0:O<0)};R=i===h?0:i===p?1/0:_(F=>k(F)[0]-.5,!1);let L=i===h?0:_(F=>k(F)[1]-o/2,!0),q=i===p?1/0:_(F=>k(F)[0]-o/2,!1);A=[L,q];break}}return Z("oddsRatio",{estimate:R,confInt:A,confLevel:r,method:Td[n],pValue:{midp:b,fisher:N,chiSquare:E},warnings:x?["Chi-squared approximation may be incorrect"]:[]})}var Id=["epsilon","paired","varEqual","confLevel","alpha"],_d=["margin","method","alpha"];function Sf(e,t){if(!(typeof e=="number"&&e>0&&e<.5))throw new RangeError(`'${t}' must be a single number in (0, 0.5)`)}function Ld(e,t,n={}){H(n,Id,"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)Sf(n.alpha,"alpha"),o=1-n.alpha,s=n.alpha;else{if(o=n.confLevel??.95,qt(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=tt(()=>gn(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=lt((r-Math.abs(m))/l,f,!1,!1),h=s<1e-8?wt(s,f,!1,!1):wt(o,f,!0,!1),p=[m-h*l,m+h*l];return Z("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 Cd=["mn","mee","wald"];function Od(e,t,n,r,o={}){H(o,_d,"binomDiffEquivalenceTest");let s=Ie(o.method,Cd),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;Sf(c,"alpha");let[i,l]=_n(e,t,"x1","n1"),[f,m]=_n(n,r,"x2","n2"),u=i/l-f/m,h=M=>Os(i,l,f,m,M)/Math.sqrt(li(i,l,f,m,M,s)),p=h(-a),w=h(a),d=[ce(p,{lowerTail:!1}),ce(w)],v=Math.max(d[0],d[1]),N=1-2*c,b=fi(i,l,f,m,s,oi(2*c,N));return Z("binomDiffEquivalenceTest",{method:s,estimate:u,margin:a,alpha:c,statistic:[p,w],statisticName:"z",pValues:d,pValue:v,result:d[0]<c&&d[1]<c?"rejected":"not rejected",confInt:b,confLevel:N,warnings:[]})}var jt=new WeakMap;function Vs(e){let t=(n,r)=>n.weights===void 0?Array.from(r):r.filter((o,s)=>n.weights[s]!==0);return Te(e.flatMap(n=>[t(n,n.fittedValues),t(n,n.residuals)])).k}function Ps(e,t){return e.map(n=>{if(n.rss>=Fd&&n.rss<1/0)return D(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=D(o,-t);r+=a*c*c}),r})}var Fd=2**-1022;function ui(e,t){return e.y.flatMap((n,r)=>(e.weights?.[r]??1)===0?[]:[[e.weights?.[r]??1,D(n,-t)]])}function js(e){let t=Math.floor(Math.log2(e.reduce((n,[r])=>Math.max(n,r),0)));return t-(t%2+2)%2}function pr(e,t,n=0){let r=a=>e.reduce((c,[i,l])=>c+D(i,-a)*l*l,0),o=n,s=r(o);return Number.isFinite(s)||(o=js(e),s=r(o)),!(D(t,-o)>=1e-24*s)}function kf(e){let t=Vs([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+=D(e.effects[s],-t)**2}return{terms:r,rdf:e.dfResidual,rss:Ps([e],t)[0],k:t}}var qf=(e,t,n)=>pt(e,t,n,!1,!1),hi="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 di=new WeakMap;function Dd(e){let t=kf(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*D(o,-t.k)**2)}),{names:n.map(o=>e.termLabels[o.term-1]),rows:n,rdf:t.rdf,rss:t.rss,k:t.k,perfect:t.rss<1e-10*r}}function mi(e){let t=Dd(e),{names:n,rdf:r,k:o,perfect:s}=t,{rows:a,rss:c}=t;if(r>0){let w=ui(e,o);pr(w,c)&&(c=0,a=a.map(d=>pr(w,d.ss)?{...d,ss:0}:d))}let i=c/r,l=w=>D(w,2*o),f=a.map(({df:w,ss:d})=>{let v=d/w/i;return[w,l(d),l(d/w),v,qf(v,w,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:w,ss:d})=>[w,d]);return p.push([r,c]),jt.set(h,{colNames:["Df","Sum Sq"],rowNames:u,table:p}),h}function Vd(e){let t=e.map(f=>f.dfResidual),n=Vs(e),r=Ps(e,n),o=f=>D(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*D(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,w]=u>0?[e[m-1],e[m]]:[e[m],e[m-1]],d=N=>N.filter((b,M)=>(p.weights?.[M]??1)!==0),v=u===0||!vs(d(p.x),d(w.x))?NaN:h/u/a;return v<0&&(v=NaN),[t[m],o(r[m]),u,o(h),v,Number.isNaN(v)?NaN:qf(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 Ds(...e){let t=[];for(let n of e)for(let r of n??[])t.includes(r)||t.push(r);return t}function gi(e,...t){let n=[e,...t];for(let c of n)if(typeof c!="object"||c===null||!Array.isArray(c.effects))throw new TypeError("anova() needs fits from lm() or aov() with a formula");if(n.length===1){let c=mi(e),i={...c,warnings:Ds(e.warnings,c.warnings)},l=jt.get(c);return l!==void 0&&jt.set(i,l),Z("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?mi(e):Vd(o);return a.warnings=Ds(...n.map(c=>c.warnings),s,a.warnings),Z("anova",a,...n)}function wi(e){if(typeof e=="object"&&e!==null&&e.multistratum===!0){let a=e,c=Ds(a.warnings,di.get(a)),i={strata:a.strata.filter(f=>f.stratum!=="Error: (Intercept)").map(f=>({...f})),warnings:c},l=jt.get(a);return Array.isArray(l)&&jt.set(i,l.filter((f,m)=>a.strata[m].stratum!=="Error: (Intercept)")),Z("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=mi(t),r=kf(t),o=pi(t.fittedValues.map(a=>D(a,-r.k)),r.rss,r.rdf),s=Ds(t.warnings,n.warnings,o?[hi]:[]);if(t.dfResidual>0){let a={...n,warnings:s},c=jt.get(n);return c!==void 0&&jt.set(a,c),Z("aovSummary",a,e)}return Z("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 Pd=["pooledSd","mu","paired","ci","alternative","adjust"],jd=["pooledSd","mu","paired","ci","alternative"],$d=["generalized","partial","ci","alternative"],Wd=["partial","ci","alternative"],zd=["partial","ci","alternative"];function yi(e){if(e!==null&&!(e>0&&e<1))throw new RangeError("ci must be a single numeric value between (0, 1)")}var Bd="Missing values detected. NAs dropped.",Ud=`'y' is numeric but has only 2 unique values.
3
+ If this is a grouping variable, convert it to a factor.`,Cf={tol:0,maxiter:5e3};function dr(e){return e===1/0?1:Math.exp(Jc(.5,(e-1)/2)-.5*Math.log(e/2))}function If(e,t,n){let r=e-1<e+1?1:Math.abs(e)*Number.EPSILON;return lr(o=>yn(e,t,{ncp:o})-n,e-r,e+r,{...Cf,extendInt:"downX"}).root}function Of(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;yi(c);let i=Ie(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(Ud),(l.some(Number.isNaN)||(f?.some(Number.isNaN)??!1))&&m.push(Bd);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,w,d,v,N;if(f===void 0||a){let T;if(f===void 0)({groups:[T],k:N}=Te([Ct(l)]));else{let[_,L]=$n(l,f);({d:T,k:N}=as(_,L))}let k=T.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(T),p=Math.sqrt(rt(T)),w=p/Math.sqrt(k),d=1/k,v=k-1}else{let T=Lt([Ct(l),Ct(f)]);N=T.k;let[k,_]=T.n;if(k<2||_<2)throw new RangeError("not enough observations: at least 2 are needed in each group");h=T.mean[0]-T.mean[1];let L=Math.sqrt(T.variance[0]),q=Math.sqrt(T.variance[1]);o?(p=Math.sqrt(((k-1)*L**2+(_-1)*q**2)/(k+_-2)),w=p*Math.sqrt(1/k+1/_),d=1/k+1/_,v=k+_-2):(p=Math.sqrt((L**2+q**2)/2),d=2*(_*L**2+k*q**2)/(k*_*(L**2+q**2)),w=Math.sqrt(Math.sqrt(L**2/k)**2+Math.sqrt(q**2/_)**2),v=is([L**2/k,q**2/_],[k-1,_-1]))}let b=r?dr(v):1,M=D(s,-N),g=(h-M)/p*b,y=r?"Hedges_g":"Cohens_d";if(c===null)return{name:y,value:g,warnings:m};let x=i==="two.sided"?c:2*c-1,E=(h-M)/w,R=If(E,v,(1+x)/2)*Math.sqrt(d)*b,A=If(E,v,(1-x)/2)*Math.sqrt(d)*b;return i==="less"&&(R=-1/0),i==="greater"&&(A=1/0),{name:y,value:g,ci:c,ciLow:R,ciHigh:A,warnings:m}}function Kd(e,t,n={}){return H(n,Pd,"cohensD"),Z("cohensD",Of(e,t,n,n.adjust??!1))}function Hd(e,t,n={}){return H(n,jd,"hedgesG"),Z("hedgesG",Of(e,t,n,!0))}var Gd=e=>Array.isArray(e.effects),bi=e=>e.multistratum===!0,Ff=e=>Array.isArray(e.strata),Df=e=>{let t=e;return typeof t.univariate=="object"&&typeof t.anova=="object"},vi=e=>({test:t=>t.split(":").includes(e)});function Yd(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 Xd(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=>vi(v).test(m)),w=o(p.length===0?"Subject":`Subject:${p.join(":")}`,h[l],h[f]);if(m==="(Intercept)")return;let d={name:m,df:h[i],ss:h[n]};w.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 Vf(e,t){let n=Array.isArray(e)?e:[],r=n.filter(s=>!t.some(a=>vi(s).test(a.name)));if(r.length>0)throw new RangeError(`Observed variable not in data: ${r.join(", ")}`);let o=s=>n.some(a=>vi(a).test(s));return{isObserved:o,ssObserved:t.reduce((s,a)=>o(a.name)?s+a.ss:s,0)}}function _f(e,t,n,r){let o=a=>ir(e,t,n,{ncp:a})-r,s=o(0);return Number.isNaN(s)?NaN:s>0?e===1/0?1/0:lr(o,0,Math.max(10,e*t*4),{...Cf,extendInt:"downX"}).root:0}function Pf(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=_f(c,t,n,(1+s)/2),l=_f(c,t,n,(1-s)/2);c<=Dn((1-s)/2,t,n)&&(l=0),c<=Dn((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 Qd(e){let t=Te([e.map(n=>n.ss)]).k;if(t!==0)for(let n of e)n.ss=D(n.ss,-t)}var Lf="needs a table of anova() or Anova(), a fit from lm() or aov() with a formula, its aovSummary(), or a result of aovEz()";function Ni(e,t,n){if(typeof e!="object"||e===null)throw new TypeError(Lf);if(bi(e)||Ff(e)||Df(e))return Zd(e,t,n);let r=Gd(e)?tt(()=>gi(e)):e;if(!Array.isArray(r.colNames)||!Array.isArray(r.table))throw new TypeError(Lf);let o=jt.get(r),s=o!==void 0&&!Array.isArray(o)?o:r,a=n.ci===void 0?.95:n.ci;yi(a);let c=Ie(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)");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,w=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,M=u.length===1?"plain":N!==!1?"generalized":n.partial??!0?"partial":"plain",{isObserved:g,ssObserved:y}=Vf(N,u),x=R=>{let{ss:A,df:T}=R;if(t==="Eta2")return M==="partial"?A/(A+p):M==="plain"?A/d:A/(A+p+y-(g(R.name)?A:0));if(t==="Omega2"){let _=M==="partial"?(A-T*w)/(A+(v-T)*w):(A-T*w)/(d+w);return Math.max(0,_)}let k=M==="partial"?(A-T*w)/(A+p):(A-T*w)/d;return Math.max(0,k)},E=`${t}${M==="plain"?"":`_${M}`}`;return{colNames:a===null?[E]:[E,"CI","CI_low","CI_high"],rowNames:u.map(R=>R.name),table:u.map(R=>a===null?[x(R)]:Pf(x(R),R.df,h,a,c)),warnings:Array.isArray(r.warnings)?[...r.warnings]:[]}}function Zd(e,t,n){let r=n.ci===void 0?.95:n.ci;yi(r);let o=Ie(n.alternative??"greater",["two.sided","less","greater"]),s=Df(e),a,c,i;if(s){let g=jt.get(e),y=Xd(g!==void 0&&!Array.isArray(g)?{...e,univariate:g}:e);({strata:a,subject:c}=y),i=y.order.map(x=>({term:x,stratum:y.strata.find(E=>E.terms.includes(x))}))}else{let g=bi(e)?tt(()=>wi(e)):e,y=jt.get(g);({strata:a,subject:c}=Yd(Array.isArray(y)?{...g,strata:y.map((x,E)=>({...g.strata[E],...x}))}:g)),i=a.flatMap(x=>x.terms.map(E=>({term:E,stratum:x})))}let l=Te([[...a.flatMap(g=>g.terms.map(y=>y.ss)),...a.map(g=>g.sse)]]).k;for(let g of a){g.sse=D(g.sse,-l);for(let y of g.terms)y.ss=D(y.ss,-l)}let f=a.flatMap(g=>g.terms),m=a.reduce((g,y)=>g+y.sse,0),u=f.reduce((g,y)=>g+y.ss,0)+m,h=c.sse/c.dfe,p=t==="Eta2"?n.generalized??!1:!1,w=p!==!1?"generalized":n.partial??!0?"partial":"plain",{isObserved:d,ssObserved:v}=Vf(p,f),N=(g,y)=>{let{ss:x,df:E}=g,R=y.sse/y.dfe;if(t==="Eta2")return w==="partial"?x/(x+y.sse):w==="plain"?x/u:x/(x+m+v-(d(g.name)?x:0));if(t==="Omega2"){let T=y===c?0:1,k=w==="partial"?(x-E*R)/(x+T*y.sse+c.sse+h):(x-E*R)/(u+h);return Math.max(0,k)}let A=w==="partial"?(x-E*R)/(x+y.sse):(x-E*R)/u;return Math.max(0,A)},b=`${t}${w==="plain"?"":`_${w}`}`,M={colNames:r===null?[b]:[b,"CI","CI_low","CI_high"],rowNames:i.map(g=>g.term.name),table:i.map(({term:g,stratum:y})=>{let x=N(g,y);return r===null?[x]:Pf(x,g.df,y.dfe,r,o)}),warnings:[]};return s||(M.groups=i.map(g=>g.stratum.name),M.warnings=[...bi(e)||Ff(e)?e.warnings:[]]),M}function Jd(e,t={}){return H(t,$d,"etaSquared"),Z("etaSquared",Ni(e,"Eta2",t),e)}function e0(e,t={}){return H(t,Wd,"omegaSquared"),Z("omegaSquared",Ni(e,"Omega2",t),e)}function t0(e,t={}){return H(t,zd,"epsilonSquared"),Z("epsilonSquared",Ni(e,"Epsilon2",t),e)}var n0=new Set(["eqb","lowEqbound","highEqbound","hypothesis","paired","varEqual","alpha","mu","eqboundType","biasCorrection","rmCorrection","glass","smdCi"]),r0=["m1","sd1","n1","m2","sd2","n2","r12"],o0=new Set(["eqb","lowEqbound","highEqbound","hypothesis","paired","varEqual","alpha","mu","eqboundType","biasCorrection","rmCorrection","glass","smdCi"]),s0="Equivalence interval does not include zero.",a0="Warning: setting bound type to SMD produces biased results!",i0="MET test may have higher error rates than a nil two-tailed test. Consider wider equivalence bounds.";function jf(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 lr(s=>yn(e,t,{ncp:s,lowerTail:!r})-n,e-o,e+o,{tol:0,maxiter:5e3,extendInt:r?"upX":"downX"}).root}function c0(e,t){let{alpha:n,smdCi:r}=t,o=Math.abs(e.delta),s=(E,R,A)=>(1-(A-2)/(A*E*E))*R*R,a,c,i,l,f,m,u,h,p,w,d,v,N=t.biasCorrection;if(e.design==="two"){let{n1:E,n2:R,s1:A,s2:T}=e,k=A*A,_=T*T,L=2*E*R/(E+R);if(t.glass!==void 0){let q=t.glass==="glass1",[F,C,O,I]=q?[A,E,T,R]:[T,R,A,E];c=F,i=C-1,a=N?"Glass's delta(g)":"Glass's delta(d)",l=N?dr(i):1,f=l*o/c,p=1/R+1/E;let V=O/F;m=Math.sqrt(V*V/(I-1)+1/(C-1)+f*f/(2*(C-1))),u=o/(F*Math.sqrt(1/C+V*V/I)),h=E+R-2,w=Math.sqrt(2/L)}else if(t.varEqual)c=Math.sqrt(((E-1)*k+(R-1)*_)/(E+R-2)),i=E+R-2,a=N?"Hedges's g":"Cohen's d",l=N?dr(i):1,f=l*o/c,p=1/E+1/R,m=Math.sqrt(1/E+1/R+s(l,f,i)),u=o/(c*Math.sqrt(p)),h=i,w=Math.sqrt(2*(R*k+E*_)/(E*R*(k+_)));else{c=Math.sqrt((k+_)/2),i=(E-1)*(R-1)*(k+_)**2/((R-1)*k*k+(E-1)*_*_),a=N?"Hedges's g(av)":"Cohen's d(av)",l=N?dr(i):1,f=l*o/c,p=2*(R*k+E*_)/(E*R*(k+_));let q=c**4;m=Math.sqrt(f*f*(k*k/(E-1)+_*_/(R-1))/(8*q)+(k/(E-1)+_/(R-1))/(c*c)),u=o/Math.sqrt(k/E+_/R),h=i,w=Math.sqrt(2/L)}d=i,v=(q,F)=>Math.sqrt(d/(d-2)*(2/L)*(1+q*q*L/2)-q*q/(F*F))}else if(e.design==="paired"){let{n:E,s1:R,s2:A,sdif:T,oneMinusR:k}=e,_=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*k):T,a=t.glass!==void 0?N?"Glass's delta(g)":"Glass's delta(d)":N?`Hedges's g${_}`:`Cohen's d${_}`,i=E-1,d=2*E-2,l=N?dr(r==="goulet"?d:i):1,f=l*o/c,p=1/E,u=o*Math.sqrt(E)/c,h=i,w=Math.sqrt(2*k/E),v=(L,q)=>{let F=r==="goulet"?d:i,C=Math.sqrt(F/(F-2)*(2*k/E)*(1+L*L*E/(2*k))-L*L/(q*q));return t.rmCorrection?C:C*Math.sqrt(2*k)},t.glass!==void 0){let L=T/c;m=Math.sqrt(L*L/(E-1)+f*f/(2*(E-1)))}else!t.rmCorrection&&r==="nct"?m=Math.sqrt(1/E+s(l,f,i)):m=v(f,l)}else{let{n:E,s:R}=e;c=R,i=E-1,d=i,a=N?"Hedges's g":"Cohen's d",l=N?dr(i):1,f=l*o/c,p=1/E,m=Math.sqrt(1/E+f*f/(2*E)),u=o*Math.sqrt(E)/R,h=i,w=1/Math.sqrt(E),v=(A,T)=>Math.sqrt(i/(i-2)*(1/E)*(1+A*A*E)-A*A/(T*T))}let b,M,g,y=i;if(r==="nct"){g=m,y=h;let E=jf(u,h,n,!0),R=jf(u,h,n,!1);b=E*Math.sqrt(p)*l,M=R*Math.sqrt(p)*l}else if(r==="goulet"){g=v(f,l),y=d;let E=f/w;b=cn(n,d,{ncp:E})*w,M=cn(n,d,{ncp:E,lowerTail:!1})*w}else{g=m;let E=r==="t"?wt(n,i,!1,!1):pe(n,{lowerTail:!1});b=f-E*g,M=f+E*g}let x=e.delta<0;return{label:a,denom:c,estimate:x?-f:f,se:g,confInt:x?[-M,-b]:[b,M],df:y,J:l}}function $f(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 Wf(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 zf(e){let t=Ie(e.hypothesis,["EQU","MET"]),n=Ie(e.eqboundType,["raw","SMD"])==="SMD",r=Ie(e.smdCi,["nct","goulet","t","z"]),o=e.glass===void 0?void 0:Ie(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 Bf(e,t,n,r,o,s,a,c,i,l,f,m){let{alpha:u,hypothesis:h}=l,p=c0(c,l),w=D(p.denom,i),d=l.smdBounds?[f[0]*w,f[1]*w]:f,v=O=>{if(O===0)return[0,n];let I=Math.floor(Math.log2(Math.abs(O))),V=Le(O,-I);return l.smdBounds?[V*p.denom,I+i]:[V,I]},N=l.smdBounds?f:[f[0]/w,f[1]/w];l.smdBounds&&m.push(a0),d[0]<=0&&d[1]>=0||m.push(s0);let b=O=>D(O/r,n-o),M=O=>{let[I,V]=v(O),S=Math.max(n,V);return Le((Le(t,n-S)-Le(I,V-S))/r,S-o)},g=b(t),y=M(f[0]),x=M(f[1]),E=D(r,o),R=h==="EQU",A={t:g,se:E,df:s,pValue:2*lt(-Math.abs(g),s,!0,!1)},T={t:y,se:E,df:s,pValue:lt(y,s,!R,!1)},k={t:x,se:E,df:s,pValue:lt(x,s,R,!1)},_=R?Math.max(T.pValue,k.pValue):Math.min(T.pValue,k.pValue);!R&&d[0]<=0&&d[1]>=0&&_<=A.pValue&&m.push(i0);let L=wt(u,s,!1,!1),q=Math.max(n,o),[F,C]=[D(t,n-q),D(r,o-q)];return Z(e,{tTest:A,lower:T,upper:k,pValue:_,bounds:{raw:d,smd:N},raw:{estimate:D(t,n),se:E,confInt:[D(F-L*C,q),D(F+L*C,q)]},smd:{label:p.label,estimate:p.estimate,se:p.se,confInt:p.confInt,df:p.df,J:p.J},confLevel:1-2*u,method:a,hypothesis:h,warnings:m})}function l0(e,t,n={}){H(n,n0,"tTOST");let r=zf(n),o=Wf(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");$f(e,"x"),i||$f(t,"y");let l=tt(()=>i?gn(e,{mu:c}):gn(e,t,{paired:s,varEqual:a,mu:c})),f=l.parameter,m,u,h,p;if(i){let w=Te([Ct(Array.from(e))]);u=w.k;let d=w.groups[0],v=rt(d);h=st(d)-D(c,-u),p=Math.sqrt(v/d.length),m={design:"one",n:d.length,delta:h,s:Math.sqrt(v)}}else if(s){let[w,d]=$n(Array.from(e),Array.from(t)),v=Te([w,d]);u=v.k;let[N,b]=v.groups,M=N.map((A,T)=>A-b[T]),g=Math.sqrt(rt(N)),y=Math.sqrt(rt(b)),x=rt(M),E=Math.sqrt(x),R=(E-(g-y))*(E+(g-y))/(2*g*y);h=st(M)-D(c,-u),p=Math.sqrt(x/M.length),m={design:"paired",n:M.length,delta:st(N)-st(b)-D(c,-u),s1:g,s2:y,sdif:E,oneMinusR:R}}else{let w=Te([Ct(Array.from(e)),Ct(Array.from(t))]);u=w.k;let[d,v]=w.groups;if(d.length<2||v.length<2)throw new RangeError("not enough observations: at least 2 are needed in each sample");let[N,b,M,g]=[d.length,v.length,rt(d),rt(v)];h=st(d)-st(v)-D(c,-u),p=Math.sqrt(a?((N-1)*M+(b-1)*g)/(N+b-2)*(1/N+1/b):Math.sqrt(M/N)**2+Math.sqrt(g/b)**2),m={design:"two",n1:N,n2:b,delta:h,s1:Math.sqrt(M),s2:Math.sqrt(g)}}return Bf("tTOST",h,u,p,u,f,l.method,m,u,r,o,[])}function f0(e,t={}){H(e,r0,"tsumTOST"),H(t,o0,"tsumTOST");let n=zf(t),r=Wf(t),{m1:o,sd1:s,n1:a,m2:c,sd2:i,n2:l,r12:f}=e,m=(q,F)=>{if(typeof q!="number"||!Number.isFinite(q))throw new RangeError(`'${F}' must be a finite number`);return q},u=(q,F)=>{let C=m(q,F);if(!(Number.isInteger(C)&&C>=2))throw new RangeError(`'${F}' must be an integer of at least 2`);return C},h=(q,F)=>{let C=m(q,F);if(!(C>0))throw new RangeError(`'${F}' must be positive`);return C};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 w=[],{groups:d,k:v}=Te([[o,...p?[]:[c],n.mu]]),{groups:N,k:b}=Te([[s,...p?[]:[i]]]),[M,g=NaN]=p?[d[0][0]]:[d[0][0],d[0][1]],y=d[0][d[0].length-1],[x,E=NaN]=N[0],R=q=>D(q,v-b),A,T,k,_,L;if(p)A=M-y,T=x/Math.sqrt(a),k=a-1,_="One Sample t-test",L={design:"one",n:a,delta:R(A),s:x};else if(n.paired){let q=m(f,"r12");if(!(q>-1&&q<1))throw new RangeError("'r12' must be in (-1, 1)");let F=a;a!==l&&(w.push("Unequal number of pairs; using smallest n"),F=Math.min(a,l));let C=Math.sqrt(x*x+E*E-2*q*x*E);if(!(C>0))throw new RangeError("the standard deviation of the differences is 0");A=M-g-y,T=C/Math.sqrt(F),k=F-1,_="Paired t-test",L={design:"paired",n:F,delta:R(A),s1:x,s2:E,sdif:C,oneMinusR:1-q}}else{let q=l,F=x*x,C=E*E;if(A=M-g-y,n.varEqual){let O=((a-1)*F+(q-1)*C)/(a+q-2);T=Math.sqrt(O*(1/a+1/q)),k=a+q-2,_=" Two Sample t-test"}else{let O=F/a,I=C/q;T=Math.sqrt(O+I),k=(O+I)**2/(O*O/(a-1)+I*I/(q-1)),_="Welch Two Sample t-test"}L={design:"two",n1:a,n2:q,delta:R(A),s1:x,s2:E}}return Bf("tsumTOST",A,v,T,b,k,_,L,b,n,r,w)}var m0=["alternative","tsmethod"],u0=["square","central"],h0=1e3,p0=32,d0=.9,Uf=1+1e-7,g0=2**24,w0=34e7;function b0(e,t={}){if(H(t,m0,"boschlooTest"),e.length!==2||e.some(y=>y.length!==2))throw new TypeError("'table' must be a 2 x 2 table");let n=Ze(t.alternative),r=Ie(t.tsmethod,u0),o=e.map((y,x)=>Array.from(y,(E,R)=>$r(E,`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)>g0)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(h0,Math.ceil(p0*Math.sqrt(m))),h=y=>Math.min(y+1,40*Math.sqrt(y));if(u*(h(l)+h(f)+4)>w0)throw new RangeError(`Boschloo's test of these group sizes (${l} and ${f}) would take too long`);let p=s/l-c/f,w;n!=="two.sided"?w=n:r==="square"?w="square":w=p>0?"greater":"less";let d=s+c,v;w==="less"?v=Jt(s,l,f,d,!0,!1):w==="greater"?v=Jt(s-1,l,f,d,!1,!1):v=tt(()=>Ao(o)).pValue;let N=v0(l,f,s,c,w),{value:b,at:M}=y0(N,l,f,u),g=n==="two.sided"&&r==="central"?Math.min(1,2*b):b;return Z("boschlooTest",{method:"Boschloo's Exact Test",alternative:n,statistic:v,statisticName:"test statistic",pValue:g,estimate:p,estimateNames:"difference in proportion",nullValue:0,nullValueName:"difference in proportion",nuisanceParameter:M,warnings:[]})}function v0(e,t,n,r,o){let s=e+t,a=new Float64Array(s+1);for(let v=2;v<=s;v++)a[v]=ls(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),M=Math.min(v,e)-N+1,g=-1/0;for(let E=0;E<M;E++){let R=N+E,A=-a[R]-a[e-R]-a[v-R]-a[t-v+R];f[E]=A,A>g&&(g=A)}let y=0;for(let E=0;E<M;E++)f[E]=Math.exp(f[E]-g),y+=f[E];for(let E=0;E<M;E++)f[E]=f[E]/y;let x=0;for(let E=0;E<M;E++)m[E]=x+=f[E];x=0;for(let E=M-1;E>=0;E--)u[E]=x+=f[E];if(o==="less")for(let E=0;E<M;E++)i[(N+E)*c+v-N-E]=m[E];else if(o==="greater")for(let E=0;E<M;E++)i[(N+E)*c+v-N-E]=u[E];else{let E=0;for(let R=1;R<M;R++)f[R]>f[E]&&(E=R);for(let R=0;R<M;R++){let A=f[R]*Uf,T=-1,k=E;for(;k-T>1;){let F=T+k>>1;f[F]<=A?T=F:k=F}let _=E,L=M;for(;L-_>1;){let F=_+L>>1;f[F]<=A?L=F:_=F}let q=f[E]<=A?1:(T>=0?m[T]:0)+(L<M?u[L]:0);i[(N+R)*c+v-N-R]=Math.min(1,q)}}}let h=i[n*c+r]*Uf,p=new Int32Array(e+2),w=[],d=[];for(let v=0;v<=e;v++){p[v]=w.length;let N=0;for(;N<=t;){if(i[v*c+N]<=h){let b=N;for(;N+1<=t&&i[v*c+N+1]<=h;)N++;w.push(b),d.push(N)}N++}}return p[e+1]=w.length,{starts:p,from:Int32Array.from(w),to:Int32Array.from(d)}}function 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]=sn(r,e,t,!1);let o=zt(1,-t),s=it([t,0],o),a=it(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 y0(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[M,g]=Kf(t,b,c),[y,x]=Kf(n,b,i),E=0;for(let A=y;A<=x;A++)l[A]=E+=i[A];E=0;for(let A=x;A>=y;A--)f[A]=E+=i[A];let R=0;for(let A=M;A<=g;A++){let T=c[A];if(T===0)continue;let k=0;for(let _=o[A];_<o[A+1];_++){let L=s[_],q=a[_];if(!(q<y||L>x))if(L===0)k+=l[Math.min(q,x)];else if(q===n)k+=f[Math.max(L,y)];else for(let F=Math.max(L,y);F<=Math.min(q,x);F++)k+=i[F]}R+=T*k}return Math.min(1,R)},u=Math.PI/2,h=b=>b<=0?0:b>=u?1:Math.sin(b)**2,p=new Float64Array(r+2),w=0;for(let b=0;b<=r+1;b++)p[b]=m(h(u*b/(r+1))),p[b]>p[w]&&(w=b);let d=p[w],v=h(u*w/(r+1));if(d>=1)return{value:1,at:v};let N=d0*d;for(let b=0;b<=r+1;b++){let M=p[b];if(M<N||b>0&&!(M>p[b-1])||b<=r&&!(M>=p[b+1]))continue;let g=u*Math.max(0,b-1)/(r+1),y=u*Math.min(r+1,b+1)/(r+1),E=Nl(T=>-m(h(T)),g,y,1e-14).x,R=m(h(E)),A=1e-4/Math.sqrt(2*(t+n));for(let T=0;T<2&&(E-A>g&&E+A<y);T++,A*=.01){let k=m(h(E-A)),_=m(h(E+A)),L=k-2*R+_;if(!(L<0))break;let q=A*(k-_)/(2*L);if(!(Math.abs(q)<A))break;let F=m(h(E+q));F>R*(1+2**-50)&&(E+=q,R=F)}R>d&&(d=R,v=h(E))}return{value:d,at:v}}function un(e){let t=St(e);return t.some(n=>typeof n=="string")?t.map(n=>typeof n=="number"&&!Number.isNaN(n)?mt(n):n):t}function gr(e,t){return typeof e=="number"&&typeof t=="number"?e-t:ln(String(e),String(t))}function Kn(e,t,n="drop"){if(t===void 0){let c=e;if(c.length>0&&typeof c[0]=="number")throw new TypeError("'g' is required when 'x' is a single sample");let i=c.map(m=>Array.from(m).filter(u=>!Number.isNaN(u)));if(n==="error"&&i.some(m=>m.length===0))throw new RangeError("all groups must contain data");let l=[],f=[];return i.forEach((m,u)=>{m.length!==0&&(l.push(String(u+1)),f.push(m))}),{labels:l,groups:f}}let r=Array.from(e);if(r.length!==t.length)throw new RangeError("'x' and 'g' must have the same length");let o=un(t),s=new Map;for(let c=0;c<r.length;c++){let i=r[c],l=o[c];if(Number.isNaN(i)||typeof l=="number"&&Number.isNaN(l))continue;let f=s.get(l);f===void 0&&s.set(l,f=[]),f.push(i)}let a=Array.from(s.keys()).sort(gr);return{labels:a.map(mt),groups:a.map(c=>s.get(c))}}function wr(e,t,n="drop"){return Kn(e,t,n).groups}function $s(e){let t=[...e].sort((r,o)=>r-o),n=t.length;return n%2===1?t[(n-1)/2]:Xt(t[n/2-1],t[n/2])}function Mi(e){if(e.length===0)throw new RangeError("'y' must have at least one row");let t=e[0].length;if(e.some(n=>n.length!==t))throw new RangeError("all rows of 'y' must have the same length");return e.filter(n=>n.every(r=>!Number.isNaN(r))).map(n=>Array.from(n))}var Hf=["correct"];function N0(e,t,n={}){xt(t)&&H(t,Hf,"mcnemarTest"),H(n,Hf,"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=M0(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=Gf(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+=Gf(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:bt(a,i/2,2,!1,!1),warnings:[]}}function Gf(e,t,n){let r=t+n;return r<1/0?Ot(e,r):Ot(D(e,-520),D(t,-1040)+D(n,-1040))}function M0(e,t){if(e.length!==t.length)throw new RangeError("'x' and 'y' must have the same length");let n=[],r=un(e),o=un(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(gr),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 E0=["ratio","alternative","confLevel"],Ei=2**-1022;function Yf(e,t,n={}){if(H(n,E0,"varTest"),typeof e=="string"){let b=Wn(e,t);return Yf(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=Ze(n.alternative),c=n.confLevel??.95;qt(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]=Lt([r,o]).variance,u=0;if(!(f>=Ei&&m>=Ei)){let b=Lt([r]),M=Lt([o]);f=b.variance[0],m=M.variance[0],u=2*(b.k-M.k)}if(f===0&&m===0)throw new RangeError("both samples have zero variance");let h=u===0?f/m:Le(f/m,u),p=h/s,w=pt(p,i,l,!0,!1),d=pt(p,i,l,!1,!1),v,N;if(a==="less")v=w,N=[0,h/on(1-c,i,l,!0,!1)];else if(a==="greater")v=d,N=[h/on(c,i,l,!0,!1),1/0];else{v=Math.min(1,2*Math.min(w,d));let b=(1-c)/2,M=b<1e-8?on(b,i,l,!1,!1):on(1-b,i,l,!0,!1);N=[h/M,h/on(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 Xf(e,t){if(typeof e=="string"){let p=vo(e,t);return Xf(vl(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=Lt(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<Ei)){let p=o.map(M=>{let g=Lt([M]);return{w:g.variance[0],e:g.k}}),w=Math.max(...p.map(M=>M.e)),d=p.map(M=>Math.log(M.w)+2*(M.e-w)*Math.LN2),v=d.map((M,g)=>Math.log(s[g])+M),N=Math.max(...v),b=N+Math.log(v.reduce((M,g)=>M+Math.exp(g-N),0))-Math.log(c);m=c*b-d.reduce((M,g,y)=>M+s[y]*g,0)}let u=m/(1+(f-1/c)/(3*(r-1))),h=r-1;return{method:"Bartlett test of homogeneity of variances",statistic:u,statisticName:"Bartlett's K-squared",parameter:h,parameterName:"df",pValue:bt(u,h/2,2,!1,!1),warnings:[]}}var x0=["alternative"];function xi(e,t,n,r){return{method:e,statistic:n,statisticName:t,parameter:r,parameterName:"df",pValue:bt(n,r/2,2,!1,!1),warnings:[]}}function Qf(e){let t=0;for(let n of hn(e))t+=n*n*n-n;return t}function Ws(e,t){if(typeof e=="string"){let l=vo(e,t);return Ws(l.values,l.group.codes)}let n=wr(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=vt(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-Qf(o)/(s*s*s-s)),xi("Kruskal-Wallis rank sum test","Kruskal-Wallis chi-squared",c,r-1)}function Ri(e,t){if(typeof e=="string")return Ri(qa(e,t));let n=Mi(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=vt(l);for(let m=0;m<o;m++)s[m]+=f[m];a+=Qf(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 xi("Friedman rank sum test","Friedman chi-squared",i,o-1)}function R0(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{...Ri(e),method:"Cochran's Q test",statisticName:"Q"}}function Zf(e,t){if(typeof e=="string")return Zf(qa(e,t));let n=Mi(e),r=n.length,o=e[0]?.length??0;if(n.length<2||o<2)throw new RangeError("'y' must have at least two complete rows and two columns");let s=n.map(w=>dt(w)-en(w));s.some(w=>w===1/0)&&n.every(w=>w.every(Number.isFinite))&&(s=Te(n).groups.map(w=>dt(w)-en(w)));let a=vt(s),c=new Array(o).fill(0),i=0;n.forEach((w,d)=>{let v=vt(w);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 w of c)l+=w*w;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 w=0;for(let d=2;d<=o;d++)w+=Math.log(d);h=Math.exp((1-r)*w)}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 Jf(e,t){if(typeof e=="string"){let h=vo(e,t);return Jf(h.values,h.group.codes)}let n=wr(e,t,"error"),r=n.length;if(r<2)throw new RangeError("all observations are in the same group");let o=h=>h.flatMap(p=>{let w=$s(p);return p.map(d=>Math.abs(d-w))}),s=o(n);if(s.some(Number.isNaN))throw new RangeError("a group has an infinite median: its deviations are undefined");s.some(h=>h===1/0)&&n.every(h=>h.every(Number.isFinite))&&(s=o(Te(n).groups));let a=s.length,i=vt(s).map(h=>pe((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 w=0;w<h.length;w++)p+=i[u+w];u+=h.length,m+=h.length*(p/h.length-f)**2}return m/=rt(i),xi("Fligner-Killeen test of homogeneity of variances","Fligner-Killeen:med chi-squared",m,r-1)}function em(e,t,n={}){if(H(n,x0,"moodTest"),typeof e=="string"){let M=Wn(e,t);return em(M.x,M.y,n)}let r=Array.from(e).filter(Number.isFinite),o=Array.from(t).filter(Number.isFinite),s=Ze(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},(M,g)=>g).sort((M,g)=>l[M]-l[g]),m=new Array(i),u=0;for(;u<i;){let M=u;for(;M+1<i&&l[f[M+1]]===l[f[u]];)M++;let g=0;for(let y=u;y<=M;y++)g+=(y+1-(i+1)/2)**2;g/=M-u+1;for(let y=u;y<=M;y++)m[f[y]]=g;u=M+1}let h=0;for(let M=0;M<a;M++)h+=m[M];let p=0;for(let M of m)p+=M;p/=i;let w=0;for(let M of m)w+=(M-p)**2;let d=a*p,v=a*c/(i*(i-1))*w,N=(h-d)/Math.sqrt(v),b=s==="less"?ce(N):s==="greater"?ce(N,{lowerTail:!1}):2*ce(-Math.abs(N));return{method:"Mood two-sample test of scale",alternative:s,statistic:N,statisticName:"Z",pValue:b,warnings:[]}}function br(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],T0=[0,.042981,-.293762,-1.752461,5.682633,-3.582633],S0=[.544,-.39978,.025054,-6714e-7],k0=[1.3822,-.77857,.062767,-.0020322],q0=[-1.5861,-.31082,-.083751,.0038915],I0=[-.4803,-.082676,.0030302],_0=[-2.273,.459];function L0(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]=-pe((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+br(A0,a),i=new Float64Array(n);i[0]=c;let l,f;if(e>5){let m=r[1]/s+br(T0,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 tm(e){let t=Te([e]).groups[0],n=t.length,r=L0(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 C0=1.90985931710274,O0=1.0471975511966;function nm(e,t){if(Number.isNaN(e)||!(t>=3))return NaN;if(t===3){let a=C0*(Math.asin(Math.sqrt(1-e))-O0);return Math.max(a,0)}let n=Math.log(e);if(t<=11){let a=br(_0,t);if(n>=a)return 1e-99;let c=br(S0,t),i=Math.exp(br(k0,t));return ce(-Math.log(a-n),{mean:c,sd:i,lowerTail:!1})}let r=Math.log(t),o=br(q0,r),s=Math.exp(br(I0,r));return ce(n,{mean:o,sd:s,lowerTail:!1})}var am=Math.log(2*Math.PI),rm=2e-14,om=500;function F0(e){return Math.exp(-.5*e*e-.5*am)}function D0(e){return ce(e,{logP:!0})}function im(e,t=-1/0,n=!1){let r=t===-1/0?ka(e,0,2,0,rm,om):ka(e,t,1,0,rm,om),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 sm(e,t,n){let r=t.map(Math.sqrt),o=t.map(a=>Math.sqrt(1-a));return im(a=>{let c=0;for(let i=0;i<r.length;i++){let l=(r[i]*a+e)/o[i];if(!n){c+=D0(l);continue}let f=(r[i]*a-e)/o[i],m=ce(f)+ce(l,{lowerTail:!1});m<.5?c+=Math.log1p(-m):c+=Math.log(Math.max(0,l+f<0?ce(l)-ce(f):ce(f,{lowerTail:!1})-ce(l,{lowerTail:!1})))}return F0(a)*-Math.expm1(c)})}function vr(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,sm(e,t,r));let o=n/2,s=Math.log(2)+.5*Math.log(o)-.5*am-Gc(o);return Math.min(1,im(c=>{let i=s-o*(Math.expm1(2*c)-2*c);return i<-745?0:Math.exp(i)*sm(e*Math.exp(c),t,r)},-1/0,!0))}var V0=["pAdjustMethod"],P0=["pAdjustMethod","alternative"],j0=["alternative"],$0=["pAdjustMethod","alternative"],W0=["method","altp"],z0=["alternative"],cm="Ties are present. z-quantiles were corrected for ties.";function Ln(e,t){let{labels:n,groups:r}=Kn(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 Ai(e){let t=e.groups.flat(),n=vt(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 hn(t))s+=a*a*a-a;return{R:r,C:s/(12*(e.N-1))}}function _o(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 B0(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=jr(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 Bs(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 Ti(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 zs=e=>ce(e,{lowerTail:!1}),Si=(e,t,n)=>fs(Math.abs(e),t,n,{lowerTail:!1});function ki(e){let t=Lt(e.groups),n=0;for(let r of t.n)r>1&&(n+=r-1);return Lo(n,t.ssWithin),{m:t.mean,s2:t.ssWithin/n,df:e.N-e.k,k:t.k}}function Lo(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 U0(e,t,n={}){H(n,V0,"kwAllPairsDunnTest");let r=n.pAdjustMethod??"holm",o=Ln(e,t),{R:s,C:a}=Ai(o),c=o.N*(o.N+1)/12-a,i=_o(o.k,(f,m)=>Math.abs(s[f]-s[m])/Math.sqrt(c*(1/o.n[f]+1/o.n[m]))),l=B0(i.map(f=>f.map(m=>2*zs(m))),r);return Z("kwAllPairsDunnTest",Bs(o,"Dunn's all-pairs test",i,l,r,a!==0?[cm]:[]))}function K0(e,t,n={}){H(n,P0,"kwManyOneDunnTest");let r=Ze(n.alternative),o=n.pAdjustMethod??"single-step",s=Ln(e,t),{R:a,C:c}=Ai(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=lm(f,m,r,o);return Z("kwManyOneDunnTest",Ti(s,"Dunn's many-to-one test",f,u,o,c!==0?[cm]:[]))}function lm(e,t,n,r){return r==="single-step"?e.map(o=>n==="two.sided"?vr(o,t,1/0,!0):vr(n==="greater"?o:-o,t,1/0,!1)):jr(e.map(o=>n==="two.sided"?2*zs(Math.abs(o)):zs(n==="greater"?o:-o)),r)}function H0(e,t,n={}){H(n,j0,"dunnettTest");let r=Ze(n.alternative),o=Ln(e,t),{m:s,s2:a,df:c}=ki(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*vr(Math.abs(h),m,c,!1)):vr(r==="greater"?h:-h,m,c,!1));return Z("dunnettTest",Ti(o,`Dunnett's-test for multiple
4
+ comparisons with one control`,f,u,"single-step"))}function G0(e,t,n={}){H(n,$0,"manyOneUTest");let r=Ze(n.alternative),o=n.pAdjustMethod??"single-step",s=Ln(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=Ye(h),g=0;for(let R=0;R<a;R++)g+=p[R];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 R of Jt(h))E+=R*R*R-R;let w=a*u/(d*(d-1))*((d*d*d-d)/12-E/12),M=(Math.min(N,b)-a*u/2)/Math.sqrt(w);return g<v?-M:M}),l=c.map(m=>s.n[m]/(s.n[m]+a)),f=Mf(i,l,r,o);return W("manyOneUTest",Ha(s,`Wilcoxon, Mann, Whittney U-test
7
- for multiple comparisons with one control`,i,f,o))}function Hd(e,t){let n=wn(e,t),r=lo(n.k,(s,a)=>{let c=n.n[s],i=n.n[a],l=[...n.groups[s],...n.groups[a]],f=Ye(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 Jt(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:Ga(a,n.k,1/0)));return W("dscfAllPairsTest",ws(n,"Dwass-Steele-Critchlow-Fligner all-pairs test",r,o,"single-step"))}function Gd(e,t){let n=wn(e,t),r=ht(n.groups),o=r.mean,s=r.variance.map((i,l)=>i/n.n[l]),a=lo(n.k,(i,l)=>(o[i]-o[l])/Math.sqrt(s[i]+s[l])*Math.SQRT2),c=lo(n.k,(i,l)=>{let f=wc([r.variance[i],r.variance[l]],[n.n[i],n.n[l]]);return Ga(a[i-1][l],n.k,f)});return W("gamesHowellTest",ws(n,"Games-Howell test",a,c,"none"))}function Yd(e,t){let n=wn(e,t),{m:r,s2:o,df:s}=Ya(n);if(s<2)throw new RangeError("the studentized range needs at least 2 residual degrees of freedom");let a=lo(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:Ga(l,n.k,s)));return W("tukeyTest",ws(n,"Tukey's test",a,c,"single-step"))}function Xd(e,t,n={}){V(n,$d,"dunnTest");let r=n.method??"none",o=n.altp??!1,s=wn(e,t),a=ds(e,t).statistic,{R:c,C:i}=Ua(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)*gs(Math.abs(p))),h=Qd(u,r);return{chi2:a,Z:f,P:u,PAdjusted:h,zValue:f,pValue:u,pAdjusted:h,comparisons:m,warnings:[]}}function Qd(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 Zd(e,t,n={}){V(n,jd,"glhtDunnett");let r=Me(n.alternative),o=wn(e,t),{m:s,s2:a,df:c,k:i}=Ya(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=>L(N,i)),g=u.map(N=>L(N,i)),d=f.map(N=>o.n[N]/(o.n[N]+o.n[0])),v=h.map(N=>r==="two.sided"?Qn(N,d,c,!0):Qn(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 Ef=["varEqual"],Jd=2**-1022;function xf(e,t,n={}){if(typeof e!="string"&&Je(t)&&V(t,Ef,"onewayTest"),V(n,Ef,"onewayTest"),typeof e=="string"){let d=Lc(e,t);return xf(d.values,d.groups,n)}let r;t!==void 0&&!Array.isArray(t)?n=t:r=t;let o=Yn(e,r),s=ht(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,fo(p,s.ssWithin),h=v/(i-1)/(N/p)}else if(m.some(d=>!(d>=Jd))&&(g=t0(o))!==void 0){let d=g.map((y,x)=>Rf(l[x]/y.variance,-2*y.k)),v=Math.max(...d.map(y=>y.e)),N=d.map(y=>ye(y.m,y.e-v)),b=N.reduce((y,x)=>y+x,0),E=0;for(let y=0;y<i;y++){let x=N.reduce((T,A,S)=>S===y?T:T+A,0);E+=(x/b)**2/(l[y]-1)}E/=i*i-1;let w=[];for(let y=0;y<i;y++)for(let x=y+1;x<i;x++){let[T,A]=g[y].k>=g[x].k?[g[y],g[x]]:[g[x],g[y]],S=T.mean-ye(A.mean,A.k-T.k);if(S===0)continue;let k=Rf(S,T.k);w.push({m:d[y].m*d[x].m*k.m*k.m/b,e:d[y].e+d[x].e+2*k.e-v})}let M=Math.max(...w.map(y=>y.e)),R=w.reduce((y,x)=>y+ye(x.m,x.e-M),0);h=(w.length===0?0:ye(R,M))/((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,M)=>w/m[M]),v=d.reduce((w,M)=>w+M,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:We(h,i-1,p,!1,!1),warnings:[]}}function e0(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:df(r,n),warnings:[]}}function t0(e){let t=[];for(let n of e){let r=ht([n]),o=r.variance[0];if(!(o>0))return;t.push({mean:r.mean[0],variance:o,k:r.k})}return t}function Rf(e,t){let n=Math.floor(Math.log2(Math.abs(e))),r=ye(e,-n);return Math.abs(r)>=2?(r/=2,n++):Math.abs(r)<1&&(r*=2,n--),{m:r,e:t+n}}function Xa(e,t,n){let r=[0,0];for(let o=Math.max(0,t);o<Math.min(n,e.length);o++)r=H(r,[e[o],0]);return r[0]}function n0([[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]=en(f,o,s,a,!1);return{lo:c,d:l}}function r0(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 bs=class{lo;hi;logNull;constructor(t){let n=0,r=new Float64Array([1]);for(let o of t){let s=n0(o);n+=s.lo,r=r0(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=Xa(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=H(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?Xa(o,s,o.length):Xa(o,0,s+1)}};function Qa(e,t){return An(e,t)}function Tf(e,t){if(t===e.lo)return 0;if(t===e.hi)return 1/0;let n=e.mean(1);return n===t?1:Qa(r=>e.mean(r)-t,n>t)}function Za(e,t,n){if(t===e.hi)return 1/0;let r=e.cdf(t,1,!1);return r===n?1:Qa(o=>n-e.cdf(t,o,!1),r<n)}function Ja(e,t,n){if(t===e.lo)return 0;let r=e.cdf(t,1,!0);return r===n?1:Qa(o=>e.cdf(t,o,!0)-n,r>n)}var o0=["alternative","correct","exact","confLevel"];function s0(e,t={}){V(t,o0,"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(ei);if(s.some(f=>f<2))throw new RangeError("sample size in each stratum must be > 1");let a=Me(t.alternative),c=t.correct??!0,i=t.exact??!1,l=t.confLevel??.95;if(ct(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?c0(f,a,l):a0(f,s,a,c,l)}return l0(e,s,r,o)}function ei(e){return e.reduce((t,n)=>t+n.reduce((r,o)=>r+o,0),0)}function kf(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 qf(e,t){return e.map(n=>n.map(r=>r.map(o=>L(o,-t))))}function Af(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 M=v*N*b*E/(d*d*(d-n));s+=Number.isFinite(M)?M:v/d*(N/d)*(b/(d-n))*E;let R=(m+p)/d,y=(u+h)/d,x=m*p/d,T=u*h/d,A=Number.isFinite(x)?x:m/d*p,S=Number.isFinite(T)?T:u/d*h;a+=A,c+=S,i+=R*A,l+=R*S+y*A,f+=y*S}),{sa:r,se:o,sv:s,sR:a,sS:c,sPR:i,sPSQR:l,sQS:f}}function a0(e,t,n,r,o){let s=Af(e,t,1),a=0;if(!t.every(Number.isFinite)||!Object.values(s).every(Number.isFinite)){a=kf(e);let R=qf(e,a);s=Af(R,R.map(ei),L(1,-a))}let{sa:c,se:i,sv:l,sR:f,sS:m,sPR:u,sPSQR:h,sQS:p}=s,g=c-i,d=L(.5,-a),v=r&&Math.abs(g)>=d?d:0,N=L(wt(Math.abs(g)-v,l),a),b=n==="two.sided"?Ge(N,.5,2,!1,!1):Y(Math.sign(g)*Math.sqrt(N),{lowerTail:n==="less"}),E=f/m,w=L(Math.sqrt(u/(2*f*f)+h/(2*f*m)+p/(2*m*m)),-a/2),M;if(n==="less")M=[0,E*Math.exp(Z(o)*w)];else if(n==="greater")M=[E*Math.exp(Z(o,{lowerTail:!1})*w),1/0];else{let R=(1-o)/2,y=R<1e-8?Z(R,{lowerTail:!1}):Z((1+o)/2);M=[E*Math.exp(-y*w),E*Math.exp(y*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:M,confLevel:o,estimate:E,estimateNames:"common odds ratio",nullValue:1,nullValueName:"common odds ratio",warnings:[]}}var i0=2**24;function c0(e,t,n){let r=e.reduce((i,[[l,f],[m,u]])=>i+Math.min(l,u)+Math.min(f,m),1);if(r>i0)throw new RangeError(`the exact distribution of S has ${r} values, more than 2^24: use exact: false`);let o=new bs(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=H(f,[m,0]));a=Math.min(1,f[0])}else a=o.cdf(s,1,t==="greater");let c;if(t==="less")c=[0,Za(o,s,1-n)];else if(t==="greater")c=[Ja(o,s,1-n),1/0];else{let i=(1-n)/2;c=[Ja(o,s,i),Za(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:Tf(o,s),estimateNames:"common odds ratio",nullValue:1,nullValueName:"common odds ratio",warnings:[]}}function l0(e,t,n,r){let{diff:o,V:s}=Sf(e,t,n,r,1),a=0;if(!t.every(Number.isFinite)||!o.every(Number.isFinite)||!s.every(f=>f.every(Number.isFinite))){a=kf(e);let f=qf(e,a);({diff:o,V:s}=Sf(f,f.map(ei),n,r,L(1,-a)))}let c=o.length,i=Mt(s,o),l=0;for(let f=0;f<c;f++)l+=o[f]*i[f];return l=L(l,a),{method:"Cochran-Mantel-Haenszel test",statistic:l,statisticName:"Cochran-Mantel-Haenszel M^2",parameter:c,parameterName:"df",pValue:Ge(l,c/2,2,!1,!1),warnings:[]}}var f0=2**-1022;function Sf(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:Co(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],M=h*E*w;c[d][b+(n-1)*N]+=Number.isFinite(M)&&h>=f0?M: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 If=new Map,m0=2**24,u0=2**32;function Lf(e,t){let n=e+t,r=(e+1)*(Math.floor((n+1)**2/4)+1);if(2*r>m0||n*r>u0)throw new RangeError(`the exact distribution for m = ${e} and n = ${t} is too large to compute: use exact: false`)}function h0(e,t){let n=`${e},${t}`,r=If.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 If.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}=h0(t,n),a=cc(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 _f=new Map;function p0(e,t){let n=`${e},${t}`,r=_f.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 _f.set(n,f),f}function vs(e,t,n){let{min:r,counts:o}=p0(t,n);if(e<=0)return r;if(e>=1)return r+o.length-1;let s=bc(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 d0=["alternative","exact","confInt","confLevel"];function Df(e,t,n={}){if(V(n,d0,"ansariTest"),typeof e=="string"){let w=Tn(e,t);return Df(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=Me(n.alternative),i=n.confInt??!1,l=n.confLevel??.95;i&&ct(l);let f=s+a,m=Ye(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 Ue,N,b;if(g&&!p){Lf(s,a);let w=R=>ti(R-1,s,a,!1),M=R=>ti(R,s,a,!0);if(c==="two.sided"){let R=Math.floor((s+1)**2/4)+Math.floor(s*a/2)/2;N=Math.min(1,2*(h>R?w(h):M(h)))}else c==="less"?N=w(h):N=M(h);i&&(b=v0(r,o,c,l,v))}else{let w=Pf(h,u,p,s,a);N=c==="two.sided"?2*Math.min(Y(w),Y(w,{lowerTail:!1})):c==="less"?Y(w,{lowerTail:!1}):Y(w),i&&(b=N0(r,o,c,l,d?new Ue: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",W("ansariTest",E)}function Pf(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 Vf(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)<g0))?w0(e.map(f=>Of(f,n)).concat(t.map(f=>Of(f,1)))):b0(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 g0=2**-1022;function Of(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]=Cf(Math.abs(e)),[s,a]=Cf(t),c=o/a,i=r-s;return c<1&&(c*=2,i-=1),[n,i,c]}function Cf(e){let t=Math.floor(Math.log2(e)),n=L(e,-t);return n>=2?(n/=2,t+=1):n<1&&(n*=2,t-=1),[t,n]}function Ff(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 w0(e){let t=e.map((r,o)=>o).sort((r,o)=>Ff(e[r],e[o])),n=new Array(e.length);for(let r=0;r<t.length;){let o=r+1;for(;o<t.length&&Ff(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 b0(e){let t=e.filter(s=>!Number.isNaN(s));if(t.length===e.length)return Ye(e);let n=Ye(t),r=0,o=t.length;return e.map(s=>Number.isNaN(s)?++o:n[r++])}function Ns(e,t,n){return e==="two.sided"?n(t):e==="greater"?[n(2*t)[0],1/0]:[0,n(2*t)[1]]}function v0(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:Ns(n,1-r,()=>[NaN,NaN]),estimate:NaN};let l=c.map((v,N)=>Vf(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 M of l)(N?M-v>0:M-v>=0)&&M-v<E&&(E=M-v);let w=b?-1/0:1/0;return l.forEach((M,R)=>{M-v===E&&(w=b?Math.max(w,c[R]):Math.min(w,c[R]))}),w},m=v=>{let N=vs(v/2,s,a),b=vs(1-v/2,s,a);if(!l.some(M=>M-N>=0)||!l.some(M=>M-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=Ns(n,1-r,m),h=vs(.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 N0(e,t,n,r,o){let s=e.length,a=t.length,c=(v,N)=>{let{ab:b,a:E,ties:w}=Vf(e,t,v);return Pf(b,E,w,s,a)-N},i=[],l=(v,N)=>{v.length===0||N.length===0||i.push($t(v)/Be(N),Be(v)/$t(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:Ns(n,1-r,()=>[NaN,NaN]),estimate:NaN};let f=$t(i),m=Be(i),u=v=>Rt(N=>c(N,v),f,m,{tol:1e-4}),h=v=>{let N=Z(v/2),b=Z(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=Ns(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 y0=["alternative","confLevel"];function M0(e,t,n={}){V(n,y0,"brunnerMunzelTest");let r=Array.from(e).filter(y=>!Number.isNaN(y)),o=Array.from(t).filter(y=>!Number.isNaN(y)),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=Me(n.alternative),i=n.confLevel??.95;jt(i);let l=Ye(r.concat(o)),f=l.slice(0,s),m=l.slice(s),u=y=>y.reduce((x,T)=>x+T,0)/y.length,h=u(f),p=u(m),g=(y,x,T)=>{let A=(y.length+1)/2,S=0;for(let k=0;k<y.length;k++)S+=(y[k]-x[k]-T+A)**2;return S/(y.length-1)},d=g(f,Ye(r),h),v=g(m,Ye(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)),M;c==="greater"?M=$e(E,w,!0,!1):c==="less"?M=$e(E,w,!1,!1):M=2*$e(-Math.abs(E),w,!0,!1);let R=Ke((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:M,confInt:[N-R,N+R],confLevel:i,estimate:N,estimateNames:"P(X<Y)+.5*P(X=Y)",warnings:[]}}var E0=[1.2337005501361697,7831619385924639e-32];function R0(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 x0=2**33;function jf(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>x0)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 zf(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]=rn(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]=R0(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 ni(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=Pe(E0,rn(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 ri(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]=rn(e,t),o=Math.floor(e-n-r),s=0;for(let a=0;a<=o;a++){let c=H(kt(e-a,-n),[-r,0]);if(c[0]<=0)break;let i=H(kt(a,n),[r,0]),l=Pe(i,[e,0]),f=Pe(c,[e,0]);s+=ic(a,e-a,l,f,!1)/(l[0]+l[1])}return Math.min(t*s,1)}var $f=2**28;function Wf(e,t){let[n,r]=rn(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)>$f)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=Ut(b,N,p,!1);for(let M=b;M<=N&&w!==0;M++)M>b&&(w=w*(N-M+1)*g/M),M>=E?m+=v*w:i[d+M]+=v*w,u++;w=b>0?Ut(b-1,N,p,!1):0;for(let M=b-1;M>=0&&w!==0;M--)M<b-1&&(w=w*(M+1)/((N-M)*g)),M>=E?m+=v*w:i[d+M]+=v*w,u++}if(u>2*$f)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 Bf(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*en(u,t,n,p,!1)),f[h]=0):f[h]=g}[l,f]=[f,l],f.fill(0)}return Math.min(m,1)}var T0=["alternative","exact"];function Kf(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 Uf(e,t,n={}){if(V(n,T0,"ksTest"),typeof e=="string"){let N=Tn(e,t);return Uf(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=Me(n.alternative),a=s==="two.sided"?"D":s==="greater"?"D^+":"D^-";if(typeof t=="function"){let N=-1/0,b=-1/0;for(let y=0;y<o;y++){let x=t(r[y]);if(!(x>=0&&x<=1))throw new RangeError(`the distribution function returned ${x} at ${r[y]}, not a value in [0, 1]`);N=Math.max(N,(y+1)/o-x),b=Math.max(b,x-y/o)}let E=s==="two.sided"?Math.max(N,b):s==="greater"?N:b,w=new Set(r).size<o,M=n.exact??(o<100&&!w),R;if(M&&s==="two.sided"&&E<.5&&jf(o,E),s==="two.sided")if(!M)R=ni(Math.sqrt(o)*E,!1);else{let y=2*ri(o,E);if(E>=.5)R=Math.min(1,y);else{let x=y<.01?Wf(o,E):NaN;R=Number.isNaN(x)?1-zf(o,E):x}}else R=M?ri(o,E):Math.exp(-2*o*E*E);return W("ksTest",{method:`${M?"Exact":"Asymptotic"} one-sample Kolmogorov-Smirnov test`,alternative:Kf(s,!0),exact:M,statistic:E,statisticName:a,pValue:Math.min(1,Math.max(0,R)),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=Bf(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"?ni(Math.sqrt(N)*p,!1):Math.exp(-2*N*p*p)}return W("ksTest",{method:`${d?"Exact":"Asymptotic"} two-sample Kolmogorov-Smirnov test`,alternative:Kf(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 A0=["poolSd","paired","pAdjustMethod","alternative"],S0=["mu","paired","exact","correct","confLevel","tolRoot","pAdjustMethod","alternative"],k0=["pAdjustMethod","correct"];function Hf(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=vr(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(co);return{levels:a,groups:a.map(c=>o.get(c))}}function oi(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=wr(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 si(e,t,n,r,o=[]){return{method:e,pValue:n,rowNames:t.slice(1).map(it),colNames:t.slice(0,-1).map(it),pAdjustMethod:r,warnings:o}}function q0(e,t,n={}){V(n,A0,"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=Me(n.alternative),a=n.pAdjustMethod??"holm",{levels:c,groups:i}=Hf(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=ht(i),p=h.mean,g=0;for(let v of u)v>1&&(g+=v-1);fo(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"?$e(b,g,!0,!1):s==="greater"?$e(b,g,!1,!1):2*$e(-Math.abs(b),g,!0,!1)}}else m=r?"paired t tests":"t tests with non-pooled SD",f=(u,h)=>ke(()=>nn(i[u],i[h],{paired:r,alternative:s})).pValue;return si(m,c,oi(l,f,a),a)}function I0(e,t,n={}){V(n,S0,"pairwiseWilcoxTest");let{pAdjustMethod:r,alternative:o,...s}=n,a=r??"holm",{levels:c,groups:i}=Hf(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 Ue,u=oi(l,(h,p)=>{let g=ke(()=>ro(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 W("pairwiseWilcoxTest",si(f,c,u,a,m.list))}function _0(e,t,n={}){if(V(n,k0,"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 Ue,l=oi(s,(m,u)=>{let h=ke(()=>Ea([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 W("pairwisePropTest",si("Pairwise comparison of proportions",f,l,a,i.list))}var mo=[0,0];function ys(e){if(!(e[0]>0))return[Math.sqrt(e[0]),0];let t=Math.sqrt(e[0]),n=he(e,rn(t,t));return kt(t,n[0]/(2*t))}function Ms(e,t){return Array.from({length:t},(n,r)=>e===void 0?[1,0]:ys([e[r],0]))}function Nr(e,t){return bn(e,t,[]).fit}function bn(e,t,n){let r=t.length,o=Math.min(e.length,r),s=e.map(f=>f.slice()),a=t.slice(),c=n.map(f=>f.slice()),i=[];for(let f=0;f<o;f++){let m=s[f],u=mo;for(let v=f;v<r;v++)u=H(u,ce(m[v],m[v]));if(u[0]===0){i.push(mo);continue}let h=ys(u),p=m[f][0]<0?[-h[0],-h[1]]:h;m[f]=H(m[f],p);let g=ce(p,m[f]),d=v=>{let N=mo;for(let E=f;E<r;E++)N=H(N,ce(m[E],v[E]));let b=Pe(N,g);for(let E=f;E<r;E++)v[E]=he(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(mo);for(let f=o-1;f>=0;f--){let m=a[f];for(let u=f+1;u<o;u++)m=he(m,ce(s[u][f],l[u]));l[f]=Pe(m,i[f])}return{fit:{qty:a,coef:l},qtv:c}}function yr(e,t=0,n=e.length){let r=mo;for(let o=t;o<n;o++)r=H(r,ce(e[o],e[o]));return r}var L0=1e-14;function O0(e,t,n,r,o,s,a,c){let i=M=>L(M,2*a),l=t.length,f=n.flatMap((M,R)=>yr(M)[0]>L0*s[R]?[R]:[]),m=0,u=[],h=t.slice(),p=new Array(l).fill(0);if(f.length>0){let M=et(Array.from({length:l},(y,x)=>f.map(T=>n[T][x][0])));m=M.rank,u=M.pivot.slice(0,m).map(y=>f[y]);let R=Nr(u.map(y=>n[y]),t);h=R.qty,u.forEach((y,x)=>{for(let T=0;T<l;T++)p[T]=p[T]+n[y][T][0]*R.coef[x][0]})}let g=[];u.forEach((M,R)=>{let y=r[M],x=g.find(T=>T.term===y);x===void 0&&g.push(x={term:y,df:0,ss:[0,0]}),x.df++,x.ss=H(x.ss,ce(h[R],h[R]))});let d=l-m,v=yr(h,m)[0],N=Oa(p,v,d),b=g.filter(M=>M.term!==0);d>0&&Hn(c,v)&&(v=0,b=b.map(M=>Hn(c,M.ss[0])?{...M,ss:[0,0]}:M));let E=b.map(M=>o[M.term-1]),w=M=>{if(d>0){let R=v/d,y=b.map(({df:x,ss:T})=>{let A=T[0]/x/R;return[x,M(T[0]),M(T[0]/x),A,We(A,x,d,!1,!1)]});return y.push([d,M(v),M(R),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:b.map(({df:R,ss:y})=>[R,M(y[0]),M(y[0]/R)])}};return[w(i),w(M=>M),N]}function Gf(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=jo({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,P)=>se([f.x.map(q=>q[P])]).groups[0]),g=f.x.map((I,P)=>p.map(q=>q[P])),d=h>0?et(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(P=>[P,0]),E=i.colNames.map((I,P)=>b(se([i.x.map(q=>q[P])]).groups[0])),{groups:w,k:M}=se([l]),{fit:R,qtv:y}=bn(N.map(I=>b(p[I])),b(w[0]),E),x=w[0].map(I=>[1,I]),T=E.map(I=>yr(I)[0]),A=[];N.forEach((I,P)=>{let q=f.assign[I],F=q===0?"(Intercept)":f.terms.termLabels[q-1],C=A.find(D=>D.name===F);C===void 0&&A.push(C={name:F,idx:[]}),C.idx.push(P)}),m>v&&A.push({name:"Within",idx:Array.from({length:m-v},(I,P)=>v+P)});let S=A.map(I=>O0(I.name,I.idx.map(P=>R.qty[P]),y.map(P=>I.idx.map(q=>P[q])),i.assign,i.terms.termLabels,T,M,x)),k=S.map(I=>I[0]),_={formula:e,multistratum:!0,strata:k,rows:i.frame.rows,warnings:u};return xt.set(_,S.map(I=>I[1])),S.some((I,P)=>I[2]&&A[P].name!=="(Intercept)")&&Ca.set(_,[La]),_}function Zn(e){let t=e.length,n=new Array(t).fill(0);for(let o=0;o<t;o++){let s=0;for(let a=0;a<=o;a++)s=Math.max(s,Math.abs(e[a][o]));s>0&&Number.isFinite(s)&&(s<2**-400||s>2**400)&&(n[o]=Math.floor(Math.log2(s)))}let r=n.every(o=>o===0)?e.map(o=>o.slice()):e.map(o=>o.map((s,a)=>L(s,-n[a])));return{inv:jc(r),e:n}}var Yf=new WeakMap;function Mr(e,t){return t!==void 0&&Yf.set(e,t),e}function ai(e,t){return Yf.get(e)??(t.length===0?0:t[t.length-1]+1)}function Xf(e,t){for(let n of t)if(Object.hasOwn(e,n))return e[n].length;return Object.values(e)[0]?.length}var C0=["weights","contrasts","levels","intercept"],F0=["level"],D0=["interval","level"],ii=2**-1022;function ci(e,t,n){let r=t.y;if(r===void 0)throw new TypeError("the formula has no response");let o=em(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=>L(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 li(e,t,n={}){if(V(n,C0,"lm"),typeof e=="string"){let r=Wn(lr(e),t,{...n,dropUnusedLevels:!0});return W("lm",Mr(ci(e,r,!1),Xf(t,r.terms.variables)))}return P0(e,t,n)}function P0(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=em(l,f,i,r).result;return m.rows=c.flatMap((u,h)=>u?[h]:[]),Mr(m,s.length)}function V0(e,t,n,r,o,s,a){let c=s.rank,i=s.pivot.slice(0,c),l=n?.map(w=>L(w,-r)),f=Ms(l===void 0?void 0:o.map(w=>l[w]),o.length).map((w,M)=>{if(n===void 0||l===void 0)return w;let R=n[o[M]];if(R===0||l[o[M]]>=ii)return w;let y=ys([R,0]);return[L(y[0],-r/2),L(y[1],-r/2)]}),m=i.map(w=>{let M=o.map((y,x)=>st(f[x],e[y][w])),R=Vc(M.map(y=>y[0]));return{c:R===0?M:M.map(y=>[L(y[0],-R),L(y[1],-R)]),e:R}}),{qty:u,coef:h}=Nr(m.map(({c:w})=>w),o.map((w,M)=>st(f[M],t[w]))),p=h.map((w,M)=>m[M].e===0?w:[L(w[0],-m[M].e),L(w[1],-m[M].e)]),g=e[0]?.map(()=>NaN)??[];i.forEach((w,M)=>g[w]=p[M][0]);let d=e.map(w=>{let M=[0,0];return i.forEach((R,y)=>M=H(M,st(p[y],w[R]))),M}),v=yr(u,c)[0],N=o.map(w=>l?.[w]??1),b=[0,0];if(a){let w=[0,0],M=[0,0];o.forEach((R,y)=>{w=H(w,[N[y],0]),M=H(M,st(d[R],N[y]))}),b=Pe(M,w)}let E=[0,0];return o.forEach((w,M)=>{let R=he(d[w],b);E=H(E,st(ce(R,R),N[M]))}),{coefficients:g,fittedValues:d.map(w=>w[0]),residuals:d.map((w,M)=>he([t[M],0],w)[0]),effects:u.map(w=>w[0]),rss:v,mss:E[0]}}function em(e,t,n,r,o=!1){let s=se([t.filter((l,f)=>n===void 0||n[f]!==0)]).k;if(s===0)return Qf(Zf(e,t,n,r,o),e,t,n);let a=Zf(e,t.map(l=>L(l,-s)),n,r,o),c=a.result,i=l=>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),Qf({...a,effects:a.effects.map(i),rss:L(a.rss,2*s),yScale:s,rssScaled:a.rss},e,t,n)}function Qf(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]=Wr(f,0)?Br(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 fi=new WeakMap;function mi(e){return fi.get(e)??0}function $0(e){return e.qr.every(t=>t.every(Number.isFinite))&&e.qraux.every(Number.isFinite)}function Zf(e,t,n,r,o){if(t.length===0)throw new RangeError("0 (non-NA) cases");let s=t.map((K,te)=>n===void 0||n[te]!==0),a=e.filter((K,te)=>s[te]),c=t.filter((K,te)=>s[te]),i=n?.filter((K,te)=>s[te]),l=c.length;if(l===0)throw new RangeError("all weights are zero");if(a.some(K=>K.some(te=>!Number.isFinite(te))))throw new RangeError("NA/NaN/Inf in 'x'");if(c.some(K=>!Number.isFinite(K)))throw new RangeError("NA/NaN/Inf in 'y'");if(i?.some(K=>!Number.isFinite(K)))throw new RangeError("non-finite weights not allowed");let f=t.flatMap((K,te)=>s[te]?[te]:[]),m=i===void 0?1:Be(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((K,te)=>K.map(j=>j*p[te])),d=et(g);i!==void 0&&Number.isFinite(m)&&u+2>0&&!$0(d)&&(h=u+2,p=i.map(K=>L(Math.sqrt(K),-h/2)),g=a.map((K,te)=>K.map(j=>j*p[te])),d=et(g),fi.set(d,h));let v=p===void 0?c:c.map((K,te)=>K*p[te]),N=d.rank,b=K=>L(K,-u),E=o?V0(e,t,n,u,f,d,r):void 0,w=E?.coefficients??ur(d,v),M=E?.residuals??[],R=E?.fittedValues??[],y=l-N,x=i?.map(b)??new Array(l).fill(1),T=E?.rss??0;if(E===void 0){let K=hr(d,v),te=p===void 0?K:K.map((X,ne)=>X/p[ne]),j=0;t.forEach((X,ne)=>{if(s[ne])M.push(te[j]),R.push(X-te[j]),j++;else{let de=e[ne].reduce((je,Oe,Xe)=>Number.isNaN(w[Xe])?je:je+Oe*w[Xe],0);R.push(de),M.push(X-de)}});for(let X=0;X<l;X++)T+=x[X]*te[X]**2}let A=R.filter((K,te)=>s[te]),S=0;if(i!==void 0&&i.some(K=>K!==0&&L(K,-u)<ii)){let K=f.flatMap((te,j)=>{let X=M[te];return X===0?[]:[{w:i[j],r:X,a:Math.floor(Math.log2(Math.abs(X)))}]});if(K.length>0&&K.every(te=>Number.isFinite(te.r))){let te=Math.max(...K.map(ne=>Math.log2(ne.w)+2*ne.a)),j=2*Math.floor(Math.floor(te)/2),X=0;for(let ne of K)X+=L(ne.w,2*ne.a-j)*L(ne.r,-ne.a)**2;T=X,S=j-u}}let k=T/y,_=L(Math.sqrt(k),(u+S)/2),I=h===0?0:(h-u)/2,P=d.qr.slice(0,N).map(K=>K.slice(0,N).map(te=>h===0?L(te,-u/2):te)),{inv:q,e:F}=N>0?Zn(P):{inv:[],e:[]},D=d.pivot.slice(0,N).map((K,te)=>[K,te]).sort((K,te)=>K[0]-te[0]),O=D.map(([K])=>w[K]),z=D.map(([,K])=>L(Math.sqrt(q[K][K]*k),S/2-F[K]-I)),B=O.map((K,te)=>K/z[te]),Q=B.map(K=>2*$e(-Math.abs(K),y,!0,!1)),J={coefficients:w,residuals:M,fittedValues:R,rank:N,dfResidual:y,coefTable:{estimate:O,stdError:z,tValue:B,pValue:Q},sigma:_,rSquared:0,adjRSquared:0,intercept:r,qr:d,x:e,rows:e.map((K,te)=>te)};n!==void 0&&(J.weights=n);let oe=r?1:0,pe=N-oe,we={result:J,fitRows:f,effects:(E?.effects??[]).map(K=>L(K,u/2)),rss:L(T,u+S)};if(pe===0)return we;let Ee=0;if(E!==void 0)Ee=E.mss;else if(r){let K=x.reduce((j,X)=>j+X,0),te=0;for(let j=0;j<l;j++)te+=x[j]*A[j]/K;for(let j=0;j<l;j++)Ee+=x[j]*(A[j]-te)**2}else for(let K=0;K<l;K++)Ee+=x[K]*A[K]**2;J.rSquared=Ee/(Ee+L(T,S)),J.adjRSquared=1-(1-J.rSquared)*((l-oe)/y);let G=L(Ee/pe/k,-S);return J.fStatistic={value:G,numdf:pe,dendf:y},J.fPValue=We(G,pe,y,!1,!1),we}function tm(e,t){let n=(1-e)/2;return n<1e-8?Ke(n,t,!1,!1):Ke((1+e)/2,t,!0,!1)}function j0(e,t={}){V(t,F0,"confint");let n=t.level??.95;if(!(n>0&&n<1))throw new RangeError("'level' must be between 0 and 1");let r=tm(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 Jf(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 L(L(e,-r)*L(t,-o),r+o+n)}function z0(e,t,n={}){V(n,D0,"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=-(fi.get(e.qr)??0)/2,l=e.coefficients.flatMap((b,E)=>Number.isNaN(b)?[]:[E]),f=o.map(b=>{let E=b.reduce((w,M,R)=>Number.isNaN(e.coefficients[R])?w:w+M*e.coefficients[R],0);return Wr(E,0)?Br(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 M=b[w];for(let R=0;R<w;R++)M-=c[R][w]*E[R];E[w]=M/c[w][w]}return E},u=o.map(b=>{let E=a.map(R=>b[R]),w=Ys(m(E));if(w>=ii&&w<1/0||!E.every(Number.isFinite)||E.every(R=>R===0))return i===0||!(w>0&&w<1/0)?w*e.sigma:Jf(w,e.sigma,i);let M=Math.floor(Math.log2(Be(E.map(Math.abs))));for(let R of[0,600]){let y=Ys(m(E.map(x=>L(x,-(M+R)))));if(y<1/0)return y===0?y*e.sigma:Jf(y,e.sigma,M+R+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=Ko(e.qr,e.rank,o),g=p.length>0?{nonEstimable:p,warnings:[Bo]}:{};if(h==="none")return W("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=tm(d,e.dfResidual),N=u.map(b=>v*(h==="prediction"?Math.hypot(b,e.sigma):b));return W("predict",{fit:f,seFit:u,lwr:f.map((b,E)=>b-N[E]),upr:f.map((b,E)=>b+N[E]),...g},e)}var W0=["weights","contrasts","levels"],nm=["confLevel"],rm=["sigmaX","sigmaY","mu","alternative","confLevel"];function B0(e,t,n={}){return V(n,W0,"aov"),typeof e=="string"?W("aov",om(e,t,n)):W("aov",K0(e,t))}function om(e,t,n){let r=lr(e,!0),{treatment:o,errors:s}=qc(r.rhs);if(s.length>1)throw new Error(`there are ${s.length} Error terms: only 1 is allowed`);return s.length===1?Gf(e,r,o,s[0],t,n):ci(e,Wn(r,t,{...n,dropUnusedLevels:!0},o),!0)}function K0(e,t){let{labels:n,groups:r}=qn(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 om("y ~ g",{y:o,g:s},{levels:{g:n}})}function sm(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}=ht(e),a=t-1,c=n-t,i=r/a,l=o/c,f=i/l,m=u=>L(u,2*s);return{df:[a,c],sumSq:[m(r),m(o)],meanSq:[m(i),m(l)],fValue:f,pValue:c>0?We(f,a,c,!1,!1):NaN}}function U0(e,t,n={}){Je(t)&&V(t,nm,"TukeyHSD"),V(n,nm,"TukeyHSD");let r;t!==void 0&&!Array.isArray(t)?n=t:r=t;let{labels:o,groups:s}=qn(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;jt(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}=ht(s),h=f/l,p=Mc(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(L(b,u)),d.lwr.push(L(b-p*E,u)),d.upr.push(L(b+p*E,u)),d.pAdj.push(Do(Math.abs(b)/E,a,l,{lowerTail:!1}))}return d}function H0(e,t,n={}){Je(t)&&V(t,rm,"zTest"),V(n,rm,"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 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(a))throw new RangeError("mu must be a number");let c=Me(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(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=se([l,f??[],[o,s??0]]),u=m.k,[h,p,[g,d]]=m.groups,v=En(h)[0],N=f===void 0?NaN:En(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-L(a,-u))/b,M,R;if(c==="less")M=Y(w),R=[-1/0,E+Z(i)*b];else if(c==="greater")M=Y(w,{lowerTail:!1}),R=[E-Z(i)*b,1/0];else{M=2*Y(-Math.abs(w));let T=(1-i)/2,A=(T<1e-8?Z(T,{lowerTail:!1}):Z(1-(1-i)/2))*b;R=[E-A,E+A]}R=[L(R[0],u),L(R[1],u)];let y=f!==void 0,x=(T,A)=>{if(!(u>0&&T.some((k,_)=>k!==0&&Math.abs(A[_])<2**-1022)))return L(En(A)[0],u);let S=Math.max(se([T]).k,0);return L(En(S===0?T:T.map(k=>L(k,-S)))[0],S)};return{method:y?"Two-sample z-Test":"One-sample z-Test",alternative:c,statistic:w,statisticName:"z",pValue:M,confInt:R,confLevel:i,estimate:y?[x(l,h),x(f,p)]:x(l,h),estimateNames:y?["mean of x","mean of y"]:"mean of x",nullValue:a,nullValueName:y?"difference in means":"mean",warnings:[]}}var G0=["family","link","weights","offset","start","epsilon","maxit","intercept"],Y0=["family","link","variance"],am=["power"],X0=["dispersion"],Q0=["type"],Z0=["type","dispersion"],J0=["dispersion"],eg=["newdata","newoffset","type","dispersion"],Er=2**-52;function tg(e){let t=e.map(n=>`"${n}"`);return t.length===1?t[0]:`c(${t.join(", ")})`}function uo(e,t,n){return e.map((r,o)=>{let s=0;for(let a=0;a<r.length;a++)s+=r[a]*t[a];return s+n[o]})}function In(e,t,n,r){let o=0;for(let s=0;s<t.length;s++)r[s]!==0&&(o+=e.devContribution(t[s],n[s],r[s]));return o}var lm=2**-1022,Es=e=>e===0||Math.abs(e)>=lm&&Math.abs(e)<1/0;function im(e,t,n,r){let o=e.variance(r),s=n**2,a=Math.sqrt(t*s/o);if(a>0&&a<1/0&&Es(s)&&s!==0&&Es(o)&&o!==0&&Es(t*s))return a;if(e.muPower!==void 0&&e.varPower!==void 0&&r>0){let{c,a:i}=e.muPower;return Math.sqrt(t)*Math.abs(c)*r**((2*i-e.varPower)/2)}return a}function ui(e,t,n,r){let o=(t-n)/r;if(Math.abs(r)>=lm&&Math.abs(r)<1/0)return o;if(e.muPower!==void 0&&n>0){let{c:s,a}=e.muPower,c=n**(a/2);return(t-n)/s/c/c}return o}function ng(e,t,n,r){let o=e.variance(n),s=(t-n)*Math.sqrt(r)/Math.sqrt(o);if(Number.isFinite(s)&&Es(o)&&o!==0||e.varPower===void 0||!(n>0))return s;let a=n**(e.varPower/4);return(t-n)/a*Math.sqrt(r)/a}var rg=.1;function fm(e,t){let{x:n,p:r,y:o,w:s,offset:a,fam:c,epsilon:i,maxit:l}=e,f=o.length;if(o.some((m,u)=>s[u]>0&&!Number.isFinite(m)))throw new RangeError("NA/NaN/Inf in 'y'");if(n.some((m,u)=>s[u]>0&&m.some(h=>!Number.isFinite(h))))throw new RangeError("NA/NaN/Inf in 'x'");return Hr(t,()=>{let m=(k,_)=>k.every(I=>c.etaOk(I))&&_.every(I=>c.muOk(I));if(r===0){let k=a.slice();if(!k.every(q=>c.etaOk(q)))throw new Error("invalid linear predictor values in empty model");let _=k.map(q=>c.linkinv(q));if(!_.every(q=>c.muOk(q)))throw new Error("invalid fitted means in empty model");let I=k.map(q=>c.muEta(q)),P=_.map((q,F)=>im(c,s[F],I[F],q)**2);return{coefficients:[],eta:k,mu:_,deviance:In(c,o,_,s),iter:0,converged:!0,boundary:!0,rank:0,qr:void 0,used:s.map(q=>q>0),workingWeights:P,workingResiduals:o.map((q,F)=>ui(c,q,_[F],I[F]))}}let u,h;if(e.start!==void 0){if(e.start.length!==r)throw new RangeError(`length of 'start' should equal ${r} and correspond to initial coefs for ${tg(e.columnNames)}`);h=e.start.slice(),u=uo(n,h,a)}else e.etastart!==void 0?u=e.etastart.slice():u=e.mustart.map(k=>c.link(k));let p=u.map(k=>c.linkinv(k));if(!m(u,p))throw new Error("cannot find valid starting values: please specify some");let g=In(c,o,p,s),d=g,v=!1,N=!1,b=0,E=new Array(r).fill(0),w,M=s.map(()=>!1),R=[],y=[],x=Math.min(1e-7,i/1e3);for(b=1;b<=l;b++){let k=u.map(D=>c.muEta(D));for(let D=0;D<f;D++){if(!(s[D]>0))continue;let O=c.variance(p[D]);if(Number.isNaN(O))throw new Error("NAs in V(mu)");if(O===0&&!(c.varPower!==void 0&&p[D]>0))throw new Error("0s in V(mu)");if(Number.isNaN(k[D]))throw new Error("NAs in d(mu)/d(eta)")}M=s.map((D,O)=>D>0&&k[O]!==0);let _=[];for(let D=0;D<f;D++)M[D]&&_.push(D);if(_.length===0){t.add(`no observations informative at iteration ${b}`),v=!1;break}R=new Array(f).fill(0);let I=[],P=[];for(let D of _){let O=im(c,s[D],k[D],p[D]);R[D]=O,I.push((u[D]-a[D]+ui(c,o[D],p[D],k[D]))*O),P.push(n[D].map(z=>z*O))}w=et(P,{tol:x});let q=ur(w,I);if(q.some((D,O)=>w.pivot.indexOf(O)<w.rank&&!Number.isFinite(D))){t.add(`non-finite coefficients at iteration ${b}`),v=!1;break}E=q.map(D=>Number.isNaN(D)?0:D),N=!1;let F=u;if(u=uo(n,E,a),p=u.map(D=>c.linkinv(D)),d=In(c,o,p,s),!Number.isFinite(d)){if(h===void 0)throw new Error("no valid set of coefficients has been found: please supply starting values");t.add("step size truncated due to divergence");let D=0;for(;!Number.isFinite(d);){if(D>=l)throw new Error("inner loop 1; cannot correct step size");D++,E=E.map((O,z)=>(O+h[z])/2),u=uo(n,E,a),p=u.map(O=>c.linkinv(O)),d=In(c,o,p,s)}N=!0}if(!m(u,p)){if(h===void 0)throw new Error("no valid set of coefficients has been found: please supply starting values");t.add("step size truncated: out of bounds");let D=0;for(;!m(u,p);){if(D>=l)throw new Error("inner loop 2; cannot correct step size");D++,E=E.map((O,z)=>(O+h[z])/2),u=uo(n,E,a),p=u.map(O=>c.linkinv(O))}N=!0,d=In(c,o,p,s)}let C=Math.max(i,1e-10);if(h!==void 0&&b>1&&(d-g)/(.1+Math.abs(d))>=C){t.add("step size truncated due to increasing deviance");let D=0;for(;(d-g)/(.1+Math.abs(d))>=C&&D<l;)D++,E=E.map((O,z)=>(O+h[z])/2),u=uo(n,E,a),p=u.map(O=>c.linkinv(O)),d=In(c,o,p,s);if((d-g)/(.1+Math.abs(d))>=C){t.add("inner loop 3; cannot correct step size"),v=!1;break}}if(e.untilCoefficients){let D=Be(E.map(Math.abs));if(h!==void 0&&E.every((O,z)=>Math.abs(O-h[z])<=8*Er*D)){v=!0;break}if(y.some(O=>O.every((z,B)=>z===E[B]))){v=!0;break}y.push(E.slice()),y.length>16&&y.shift()}else if(Math.abs(d-g)/(Math.abs(d)+.1)<i){v=!0;let D=0;for(let O=0;O<f;O++)M[O]&&(D=Math.max(D,Math.abs(u[O]-F[O])));D>rg&&t.add("glm.fit: the linear predictor was still changing at convergence: the data may be separated (no finite maximum likelihood estimates)");break}g=d,h=E.slice()}b>l&&(b=l);let T=r,A=E.slice();if(w!==void 0){T=w.rank;for(let k=w.rank;k<r;k++)A[w.pivot[k]]=NaN}let S=u.map(k=>c.muEta(k));return{coefficients:A,eta:u,mu:p,deviance:d,iter:b,converged:v,boundary:N,rank:T,qr:w,used:M,workingWeights:R.map(k=>k*k),workingResiduals:o.map((k,_)=>ui(c,k,p[_],S[_]))}})}function mm(e){return e.length>0&&typeof e[0]!="number"?e.map(t=>Array.from(t)):Array.from(e,t=>[t])}function um(e,t,n,r){r||(e.converged||n.add("glm.fit: algorithm did not converge"),e.boundary&&n.add("glm.fit: algorithm stopped at boundary value"),t.family==="binomial"&&e.mu.some(o=>o>1-10*Er||o<10*Er)&&n.add("glm.fit: fitted probabilities numerically 0 or 1 occurred"),t.family==="poisson"&&e.mu.some(o=>o<10*Er)&&n.add("glm.fit: fitted rates numerically 0 occurred"))}function Rr(e,t,n,r={}){let o=dr(e.familySpec),s="start"in r?r.start:e.start,a=zc(o,e.yGiven,e.priorGiven,s!==void 0||r.etastart!==void 0,n);if(!a.weights.some(f=>f>0))throw new RangeError("all weights are zero");let c=r.extraOffset===void 0?e.offset:e.offset.map((f,m)=>f+r.extraOffset[m]),i=e.x.map(f=>t.map(m=>f[m])),l=fm({x:i,p:t.length,y:a.y,w:a.weights,offset:c,fam:o,epsilon:r.epsilon??e.epsilon,maxit:r.maxit??e.maxit,columnNames:t.map(f=>e.columnNames[f]),start:s,etastart:r.etastart,mustart:a.mustart,untilCoefficients:r.untilCoefficients},n);return um(l,o,n,t.length===0),{fit:l,fam:o,y:a.y,w:a.weights,size:a.size,offset:c}}function og(e,t,n={}){V(n,G0,"glm"),V(Nn(n,"family"),Y0,"glm","family"),V(Nn(n,"family.link"),am,"glm","family.link"),V(Nn(n,"link"),am,"glm","link");let r=n.intercept??!0,o=mm(e);if(o.some(y=>y.length!==o[0].length))throw new TypeError("all rows of x must have the same length");let s=t.length>0&&typeof t[0]!="number",a=s?t.map(y=>Array.from(y)):Array.from(t),c=a.length;if(o.length!==c)throw new RangeError("variable lengths differ (found for 'x1')");let i=n.weights===void 0?void 0:Array.from(n.weights);if(i!==void 0&&i.length!==c)throw new RangeError("variable lengths differ (found for '(weights)')");let l=n.offset===void 0?void 0:Array.from(n.offset);if(l!==void 0&&l.length!==c)throw new RangeError("variable lengths differ (found for '(offset)')");let f=o.map((y,x)=>{let T=a[x];return!(Array.isArray(T)?T.some(S=>Number.isNaN(S)):Number.isNaN(T))&&y.every(S=>!Number.isNaN(S))&&!(i!==void 0&&Number.isNaN(i[x]))&&!(l!==void 0&&Number.isNaN(l[x]))}),m=o.filter((y,x)=>f[x]).map(y=>r?[1,...y]:y),u=a.filter((y,x)=>f[x]),h=u.length;if(h===0)throw new RangeError("0 (non-NA) cases");let p=i===void 0?new Array(h).fill(1):i.filter((y,x)=>f[x]);if(p.some(y=>y<0))throw new RangeError("negative weights not allowed");if(p.some(y=>!Number.isFinite(y)))throw new RangeError("non-finite weights not allowed");let g=l===void 0?new Array(h).fill(0):l.filter((y,x)=>f[x]);if(g.some(y=>!Number.isFinite(y)))throw new RangeError("the offset must be finite");let d=o.length>0?o[0].length:0,v=[...r?["(Intercept)"]:[],...Array.from({length:d},(y,x)=>`x${x+1}`)],N=v.length,b=typeof n.family=="object"?{...n.family}:{family:n.family??"gaussian"};n.link!==void 0&&(b.link=n.link);let E={x:m,columnNames:v,yGiven:u,priorGiven:p,offset:g,hasOffset:l!==void 0,intercept:r,familySpec:b,epsilon:n.epsilon??1e-8,maxit:Math.floor(n.maxit??25),start:n.start===void 0?void 0:Array.from(n.start),rows:f.flatMap((y,x)=>y?[x]:[])};if(!(E.epsilon>0&&E.epsilon<1/0))throw new RangeError("value of 'epsilon' must be > 0 and finite");if(!(E.maxit>0&&E.maxit<1/0))throw new RangeError("maximum number of iterations must be > 0");let w=!s&&sg(b)?se([u]).k:0;if(w===0)return W("glm",Mr(cm(E),c));let M=y=>L(y,-w),R=cm({...E,yGiven:u.map(M),offset:g.map(M),start:E.start?.map(M)});return W("glm",Mr(ag(R,w,{...E,yScale:w}),c))}function cm(e){let{x:t,offset:n,intercept:r}=e,o=t.length,s=e.columnNames.length,a=new Ue,c=Array.from({length:s},(d,v)=>v),{fit:i,fam:l,y:f,w:m,size:u}=Rr(e,c,a),h;if(r&&e.hasOffset){let d=new Ue,v=fm({x:t.map(()=>[1]),p:1,y:f,w:m,offset:n,fam:l,epsilon:e.epsilon,maxit:e.maxit,columnNames:["(Intercept)"],mustart:i.mu},d);um(v,l,d,!1),d.list.forEach(N=>a.add(N)),h=v.deviance,v.converged||a.add("fitting to calculate the null deviance did not converge -- increase 'maxit'?")}else if(r){let d=Math.floor(Math.log2(Be(m))),v=0,N=0;for(let E=0;E<o;E++){if(!(m[E]>0))continue;let w=L(m[E],-d);v+=w,N+=w*f[E]}let b=N/v;if(!Number.isFinite(b)){let E=0;for(let R=0;R<o;R++)m[R]>0&&(E=Math.max(E,Math.abs(f[R])));let w=Math.floor(Math.log2(E)),M=0;for(let R=0;R<o;R++)m[R]>0&&(M+=L(m[R],-d)*L(f[R],-w));b=L(M/v,w)}h=Hr(a,()=>In(l,f,f.map(()=>b),m))}else h=Hr(a,()=>In(l,f,n.map(d=>l.linkinv(d)),m));let p=o-m.filter(d=>d===0).length,g=Wc(l,f,i.mu,m,u,i.deviance,a)+2*i.rank;return hm(i.used,{coefficients:i.coefficients,fittedValues:i.mu,linearPredictors:i.eta,deviance:i.deviance,nullDeviance:h,aic:g,rank:i.rank,dfResidual:p-i.rank,dfNull:p-(r?1:0),iter:i.iter,converged:i.converged,boundary:i.boundary,y:f,priorWeights:m,weights:i.workingWeights,residuals:i.workingResiduals,family:l.family,link:l.name,warnings:a.list,qr:i.qr,model:e})}function sg(e){return(e.link??"identity")!=="identity"?!1:e.family==="gaussian"||e.family==="quasi"&&(e.variance??"constant")==="constant"}function ag(e,t,n){let r=s=>L(s,t),o={...e,coefficients:e.coefficients.map(r),fittedValues:e.fittedValues.map(r),linearPredictors:e.linearPredictors.map(r),deviance:L(e.deviance,2*t),nullDeviance:L(e.nullDeviance,2*t),aic:e.aic+2*e.priorWeights.filter(s=>s>0).length*t*Math.LN2,y:e.y.map(r),residuals:e.residuals.map(r),model:n};return Object.defineProperty(o,"scaled",{value:e,enumerable:!1}),hm(Rs.get(e),o)}var Rs=new WeakMap;function hm(e,t){return e!==void 0&&Rs.set(t,e),t}function Ot(e){let t=e.scaled;return t===void 0?{fit:e,k:0}:{fit:t,k:e.model.yScale??0}}function _n(e){return dr(e.model.familySpec)}function ho(e,t){let n=Ot(e),r=po(n.fit,t);return _n(e).fixedDispersion?r:L(r,2*n.k)}function po(e,t){if(_n(e).fixedDispersion)return 1;if(e.dfResidual<=0)return NaN;let n=0;for(let r=0;r<e.weights.length;r++)e.weights[r]>0&&(n+=e.weights[r]*e.residuals[r]**2);return e.weights.some(r=>r===0)&&t?.add("observations with zero weight not used for calculating dispersion"),n===1/0?ig(e):n/e.dfResidual}function ig(e){let t=[];for(let s=0;s<e.weights.length;s++)e.weights[s]>0&&t.push(s);if(!t.every(s=>Number.isFinite(e.weights[s])&&Number.isFinite(e.residuals[s])))return 1/0;let n=se([t.map(s=>e.residuals[s])]),r=se([t.map((s,a)=>Math.sqrt(e.weights[s])*Math.abs(n.groups[0][a]))]),o=0;for(let s of r.groups[0])o+=s*s;return L(o/e.dfResidual,2*(n.k+r.k))}function cg(e){return e.qr===void 0||e.rank===0?{inv:[],e:[]}:Zn(e.qr.qr.slice(0,e.rank).map(t=>t.slice(0,e.rank)))}function lg(e){if(!(e>=0&&e<1/0))throw new RangeError("'dispersion' must be a non-negative finite number");return e}function xr(e,t){let n=L(lg(e),-2*t);if(n===0?e===0:n>=2**-1022&&n<1/0)return{m:n,s:0};let r=Math.floor(Math.floor(Math.log2(e))/2);return{m:L(e,-2*r),s:r-t}}function hi(e,t={}){V(t,X0,"glmSummary");let{fit:n,k:r}=Ot(e),o=new Ue,s=_n(n),a=t.dispersion===void 0&&!s.fixedDispersion,c=t.dispersion===void 0?void 0:xr(t.dispersion,r),i=c===void 0?po(n,o):c.m,l=c?.s??0,{inv:f,e:m}=cg(n),u=f.map((w,M)=>w.map((R,y)=>L(R,-m[M]-m[y]))),p=(n.qr===void 0?[]:n.qr.pivot.slice(0,n.rank)).map(w=>n.coefficients[w]),g=f.map((w,M)=>L(Math.sqrt(w[M]*i),-m[M])),d=l===0?p.map((w,M)=>w/g[M]):p.map((w,M)=>ye(w/Math.sqrt(f[M][M]*i),m[M]-l)),v=!a,N=d.map(w=>v?2*Y(-Math.abs(w)):n.dfResidual>0?2*$e(-Math.abs(w),n.dfResidual,!0,!1):NaN);a&&n.dfResidual===0&&(g.fill(NaN),d.fill(NaN));let b=w=>L(w,r),E=w=>L(w,2*r);return W("glmSummary",{dispersion:l===0?E(i):t.dispersion,estimate:p.map(b),stdError:l===0?g.map(b):f.map((w,M)=>ye(Math.sqrt(w[M]*i),l+r-m[M])),statistic:d,pValue:N,statisticName:v?"z value":"t value",covUnscaled:u,covScaled:l===0?u.map(w=>w.map(M=>E(M*i))):f.map((w,M)=>w.map((R,y)=>ye(R*i,2*(l+r)-m[M]-m[y]))),dfResidual:n.dfResidual,warnings:o.list},e)}function fg(e,...t){V(mr(t),Q0,"glmResiduals");let n=fr(["type"],t).type??"deviance",r=Ot(e);return pi(r.fit,n).map(o=>L(o,r.k))}function pi(e,t){let n=_n(e),{y:r,fittedValues:o,priorWeights:s}=e;switch(t){case"response":return r.map((a,c)=>a-o[c]);case"working":return e.residuals.slice();case"pearson":return r.map((a,c)=>ng(n,a,o[c],s[c]));case"deviance":return r.map((a,c)=>s[c]===0?0:Math.sign(a-o[c])*Math.sqrt(Math.max(n.devContribution(a,o[c],s[c]),0)));default:throw new RangeError(`unknown type of residuals "${String(t)}"`)}}function go(e){let t=[],n=e.qr,r=[];if(n!==void 0&&e.rank>0){let a=n.qr.length,c=Array.from({length:a},(l,f)=>Array.from({length:e.rank},(m,u)=>f===u?1:0));r=Wo(n,c).map(l=>l.reduce((f,m)=>f+m*m,0))}let o=0,s=Rs.get(e)??e.weights.map(a=>a>0);return e.priorWeights.forEach((a,c)=>{let i=s[c]?r[o++]??0:0;a>0&&t.push(i>1-10*Er?1:i)}),t}function pm(e){let t=[],n=0,r=Rs.get(e)??e.weights.map(o=>o>0);return e.priorWeights.forEach((o,s)=>{let a=r[s]?n++:-1;o>0&&t.push(a)}),t}function mg(e){let t=e.slice().sort((r,o)=>r-o),n=t.length>>1;return t.length%2===1?t[n]:(t[n-1]+t[n])/2}function ug(e,t={}){V(t,Z0,"glmRstandard");let n=t.type??"pearson";if(n!=="pearson"&&n!=="deviance")throw new RangeError(`unknown type "${String(n)}" for glmRstandard`);let{fit:r,k:o}=Ot(e),s=go(r),{m:a,s:c}=t.dispersion===void 0?{m:po(r),s:0}:xr(t.dispersion,o),i=Math.sqrt(a),l=pi(r,n).filter((m,u)=>r.priorWeights[u]>0),f=r.rank>0?100*Er*mg(l.map(Math.abs)):0;return l.map((m,u)=>{let h=ye((Math.abs(m)<f?0:m)/(i*Math.sqrt(1-s[u])),-c);return Number.isFinite(h)?h:NaN})}function hg(e,t={}){V(t,J0,"glmCooksDistance");let{fit:n,k:r}=Ot(e),o=go(n),{m:s,s:a}=t.dispersion===void 0?{m:po(n),s:0}:xr(t.dispersion,r);return pi(n,"pearson").filter((i,l)=>n.priorWeights[l]>0).map((i,l)=>{let f=o[l]/(s*n.rank),m=(i/(1-o[l]))**2*f;return(!Number.isFinite(m)||f<2**-1022)&&(m=pg(i,o[l],s,n.rank)),a!==0&&(m=ye(m,-2*a)),Number.isFinite(m)?m:NaN})}function pg(e,t,n,r){if(!Number.isFinite(e)||!(n>0&&Number.isFinite(n)))return NaN;let o=e===0?0:Math.floor(Math.log2(Math.abs(e))),s=Math.floor(Math.log2(n)),a=L(e,-o)/(1-t);return L(a*a*(t/(L(n,-s)*r)),2*o-s)}function dg(e,t={}){V(t,eg,"glmPredict");let{fit:n,k:r}=Ot(e),o=N=>L(N,r),s=_n(n),a,c;if(t.newdata===void 0)a=n.model.x,c=n.linearPredictors.slice();else{if(a=mm(t.newdata).map(E=>n.model.intercept?[1,...E]:E),a.some(E=>E.length!==n.coefficients.length))throw new RangeError("newdata must have one column per predictor of the fit");let N=t.newoffset===void 0?a.map(()=>0):Array.from(t.newoffset,E=>L(E,-r));if(N.length!==a.length)throw new RangeError("newoffset must have one value per row of newdata");let b=n.coefficients.flatMap((E,w)=>Number.isNaN(E)?[]:[w]);c=a.map((E,w)=>{let M=E.reduce((y,x,T)=>Number.isNaN(n.coefficients[T])?y:y+x*n.coefficients[T],0);return(Wr(M,0)?Br(b.map(y=>E[y]),b.map(y=>n.coefficients[y]),M):M)+N[w]})}let{m:i,s:l}=t.dispersion===void 0?{m:po(n),s:0}:xr(t.dispersion,r),f=N=>L(N,r+l),m=n.rank,u=n.qr===void 0?[]:n.qr.pivot.slice(0,m),h=n.qr?.qr,p=a.map(N=>{let b=new Array(m).fill(0),E=0;for(let w=0;w<m;w++){let M=N[u[w]];for(let R=0;R<w;R++)M-=h[R][w]*b[R];b[w]=M/h[w][w],E+=b[w]*b[w]}if(!(E>=2**-1022&&E<1/0)){let w=b.reduce((M,R)=>Math.max(M,Math.abs(R)),0);if(w>0&&Number.isFinite(w)){let M=Math.sqrt(b.reduce((y,x)=>y+(x/w)**2,0)),R=w*M*Math.sqrt(i);return Number.isFinite(R)?R:w*(M*Math.sqrt(i))}}return Math.sqrt(E)*Math.sqrt(i)}),g=t.type??"link";if(g!=="link"&&g!=="response")throw new RangeError(`invalid 'type': ${String(g)}`);let d=t.newdata===void 0?[]:Ko(n.qr,m,a),v=d.length>0?{nonEstimable:d,warnings:[Bo]}:{};return g==="link"?W("glmPredict",{fit:c.map(o),seFit:p.map(f),...v},e):W("glmPredict",Hr(new Ue,()=>({fit:c.map(N=>o(s.linkinv(N))),seFit:p.map((N,b)=>f(N*Math.abs(s.muEta(c[b])))),...v})),e)}var gg=["parm","level","test"],dm={epsilon:1e-300,maxit:100,untilCoefficients:!0};function di(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=L(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=et(s.map((g,d)=>e[g].map(v=>v*m[d])),{tol:o}),h=hr(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+=L(E,-v)**2;let b=0;for(let E of h)b+=L(E,-v)**2;return L(L(N-b,v),v)}}return l-p}function wg(e,t={}){V(t,gg,"glmConfint");let{fit:n,k:r}=Ot(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 Ue,m=Rr(i,l,f,{...dm,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=ke(()=>hi(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 P=l.filter(C=>C!==_),q=Rr(i,P,new Ue,{...dm,start:void 0,etastart:E,extraOffset:i.x.map(C=>C[_]*I)}).fit;if(!q.converged)return h=!0,NaN;let F=s==="LRT"?q.deviance-m.fit.deviance:di(b,q.workingResiduals,q.workingWeights,!1);return Math.sign(I-u[_])*Math.sqrt(Math.max(F/d,0))},M=(1-o)/2,R=Math.sqrt(Math.PI/2)*o*(1+Math.PI/12*o**2+7*Math.PI**2/480*o**4),y=2**-9,x=R<y,T=x?[-R,R]:[Z(M),M<1e-8?Z(M,{lowerTail:!1}):Z((1+o)/2)],A=c.map(()=>NaN),S=c.map(()=>NaN),k=(_,I,P)=>{let q=P===0?-1:1;h=!1;let F=NaN,C=NaN;for(let oe=0;oe<=30;oe++){let pe=u[_]+q*2**oe*v[_],we=w(_,pe);if(Math.abs(we)>=Math.abs(I)){F=pe,C=we;break}}if(Number.isNaN(F)||h)return NaN;let D=oe=>oe===u[_]?-I:w(_,oe)-I,[O,z]=P===0?[F,u[_]]:[u[_],F],[B,Q]=P===0?[C-I,-I]:[-I,C-I],J;try{J=Rt(D,O,z,{tol:1e-14*v[_],maxIter:500,fLower:B,fUpper:Q})}catch(oe){if(!h)throw oe;J=NaN}return h?NaN:J};return c.forEach((_,I)=>{if(Number.isNaN(u[_]))return;if(!x){A[I]=k(_,T[0],0),S[I]=k(_,T[1],1);return}let P=[-2*y,-y,0,y,2*y],q=[k(_,-2*y,0),k(_,-y,0),u[_],k(_,y,1),k(_,2*y,1)].map(C=>C-u[_]),F=C=>{let D=0;for(let O=0;O<5;O++){let z=1;for(let B=0;B<5;B++)B!==O&&(z*=(C-P[B])/(P[O]-P[B]));D+=z*q[O]}return u[_]+D};A[I]=F(T[0]),S[I]=F(T[1])}),{lower:A.map(_=>L(_,r)),upper:S.map(_=>L(_,r))}}var bg=["type","omega","sandwich"],vg=["cluster","type","cadjust","multi0","fix","sandwich"],xs=2**-52;function bi(e,t){if(t===0)return e;let n=Ni.get(e)??{mant:e,exp:e.map(()=>0)};return Jn(n.mant,n.exp.map(r=>r+t))}var Ts=e=>Math.floor(Math.log2(Math.abs(e)));function Wt(e){if(e===0||!Number.isFinite(e))return[e,0];let t=Ts(e);return[L(e,-t),t]}var wo=(e,t)=>[e[0]*t[0],e[1]+t[1]];function Ln(e,t=!1){let n=-1/0;for(let[o,s]of e)o!==0&&Number.isFinite(o)&&(n=Math.max(n,s+Ts(o)));let r=n===-1/0?0:n;return t&&(r=2*Math.floor(r/2)),{v:e.map(([o,s])=>ye(o,s-r)),s:r}}var On=(e,t)=>Math.abs(t)<=2046?L(e,t):ye(e,t),Ni=new WeakMap;function Jn(e,t){let n=e.map((r,o)=>r.map((s,a)=>On(s,t[o]+t[a])));return Ni.set(n,{mant:e,exp:t}),n}function yi(e,t,n){let r=Ni.get(e),o=r?.mant[t]?.[t];if(!(r===void 0||o===void 0||!Object.is(On(o,2*r.exp[t]),n)))return On(Math.sqrt(o),r.exp[t])}function Cn(e){return typeof e=="object"&&e!==null&&"family"in e&&"model"in e}function As(e){if(!(typeof e=="object"&&e!==null&&Array.isArray(e.coefficients)&&"qr"in e&&(Cn(e)||"sigma"in e&&"x"in e&&"rows"in e)))throw new TypeError("'fit' must be the result of lm or glm")}function vm(e,t){return e.pivot.slice(0,t).map((n,r)=>[n,r]).sort((n,r)=>n[0]-r[0])}function Nm(e,t){let{inv:n,e:r}=Zn(e.qr.slice(0,t).map(s=>s.slice(0,t))),o=vm(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 gm(e,t,n){let{inv:r,e:o,cols:s}=Nm(e,t);return{A:r.map((a,c)=>a.map((i,l)=>L(i,-o[c]-o[l]-n))),cols:s}}function wm(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)?Ts(i):0}),{inv:o}=Zn(n.map(a=>a.map((c,i)=>L(c,-r[i])))),s=vm(e,t);return{B:s.map(([,a])=>s.map(([,c])=>o[a][c])),e:s.map(([,a])=>r[a])}}function ym(e,t){let n=e.qr.length;return Wo(e,Array.from({length:n},(r,o)=>Array.from({length:t},(s,a)=>o===a?1:0)))}function Ng(e,t){let n=e.qr.length;return t===0?new Array(n).fill(0):ym(e,t).map(o=>{let s=o.reduce((a,c)=>a+c*c,0);return s>1-10*xs?1:s})}function bm(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=Js(e,i),f=0;for(let m=t;m<c;m++)f+=l[m]*l[m];return f})}function Ss(e){if(Cn(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 Mi(e){let t=Ss(e);if(e.qr===void 0||e.rank===0)return{vcov:[],names:[]};let{inv:n,e:r,cols:o}=Nm(e.qr,e.rank),s=Cn(e)?0:mi(e.qr),a,c;if(Cn(e)){let l=Ot(e),f=_n(e).fixedDispersion?1:ho(l.fit),m=f===0||!Number.isFinite(f)?0:2*Math.floor(Ts(f)/2);a=L(f,-m),c=m+2*l.k}else{let[l,f]=Wt(e.sigma);a=l*l,c=2*f}let i=n.map(l=>l.map(f=>f*a));return{vcov:Jn(i,r.map(l=>(c-s)/2-l)),names:o.map(l=>t[l])}}function Mm(e,t=!1){let n=Ss(e);if(Cn(e)){let g=t?Ot(e):{fit:e,k:0},d=g.fit,v=_n(d),N=d.priorWeights,b=N.map(q=>q>0),E=N.flatMap((q,F)=>q>0?[F]:[]),{A:w,cols:M}=d.qr===void 0||d.rank===0?{A:[],cols:[]}:gm(d.qr,d.rank,0),R=E.map(q=>M.map(F=>d.model.x[q][F])),y=E.map(q=>d.weights[q]),x=E.map(q=>d.weights[q]*d.residuals[q]),T=v.family==="binomial"||v.family==="poisson",A=T?1:x.reduce((q,F)=>q+F*F,0)/y.reduce((q,F)=>q+F,0),S=d.model.rows,k=go(d),_=pm(d),I=()=>{let q=Ot(d),F=E.map(oe=>q.fit.residuals[oe]),C=y.map(Math.sqrt),{B:D,e:O}=d.qr===void 0||d.rank===0?{B:[],e:[]}:wm(d.qr,d.rank),z=R.map((oe,pe)=>oe.map((we,Ee)=>L(C[pe]*we,-O[Ee]))),{v:B,s:Q}=Ln(C.map((oe,pe)=>oe===0?[0,0]:wo(Wt(oe),Wt(F[pe])))),J=()=>{let oe=[1,0];if(!T){let pe=Ln(y.map((Ee,G)=>Ee===0?[0,0]:wo(Wt(Ee),Wt(F[G])))),we=Ln(y.map(Wt));oe=[pe.v.reduce((Ee,G)=>Ee+G*G,0)/we.v.reduce((Ee,G)=>Ee+G,0),2*pe.s-we.s+2*q.k]}return{T:R.map(pe=>pe.map((we,Ee)=>On(we,-O[Ee]))),factor:[oe[0]*oe[0],2*oe[1]]}};return{T:z,B:D,e:O,u:B,s:Q+q.k,omegaPart:J}},P;if(er(A)&&x.every(Number.isFinite))P=x.map(q=>q/A);else{let q=Ot(d),F=Ln(y.map((C,D)=>C===0?[0,0]:wo(Wt(C),Wt(q.fit.residuals[E[D]]))));if(T)P=F.v.map(C=>On(C,F.s+q.k));else{let C=Ln(y.map(Wt)),D=F.v.reduce((O,z)=>O+z*z,0)/C.v.reduce((O,z)=>O+z,0);P=F.v.map(O=>On(O/D,C.s-F.s-q.k))}}return{glm:!0,names:M.map(q=>n[q]),X:R,W:y,res:P,phiS:A,A:w,h:k,omh:bm(d.qr,d.rank,k,q=>_[q]),qr:d.qr,rank:d.rank,qrRow:q=>_[q],inputRows:E.map(q=>S[q]),nInput:ai(e,S),nonZero:b,dispersion:()=>ho(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=mi(r.qr),{A:i,cols:l}=r.rank===0?{A:[],cols:[]}:gm(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=Ng(r.qr,r.rank),p=()=>{let g=m.map(M=>c===0?Math.sqrt(M):L(Math.sqrt(M),-c/2)),{B:d,e:v}=r.rank===0?{B:[],e:[]}:wm(r.qr,r.rank),N=f.map((M,R)=>M.map((y,x)=>L(g[R]*y,-v[x]))),{v:b,s:E}=Ln(g.map((M,R)=>wo(Wt(M),Wt(r.residuals[a[R]]))));return{T:N,B:d,e:v,u:b,s:E,omegaPart:()=>({T:f.map(M=>M.map((R,y)=>On(R,-v[y]))),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:bm(r.qr,r.rank,h,g=>g),qr:r.qr,rank:r.rank,qrRow:g=>g,inputRows:a.map(g=>r.rows[g]),nInput:ai(r,r.rows),nonZero:s,dispersion:()=>r.sigma*r.sigma,scaled:p,k:0}}var Ei=2**-969,er=e=>Math.abs(e)>=Ei&&Math.abs(e)<1/0;function Em(e,t,n,r){if(!(e.phiS>=Ei&&e.phiS<1/0)||!er(e.X.length*e.phiS*e.phiS))return!1;for(let o=0;o<n.length;o++)if(!er(e.A[o][o])||!r&&(!er(t[o][o])||!er(n[o][o]))||!n[o].every(Number.isFinite))return!1;return!0}function bo(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 Tr(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 gi(e){let t=e.slice(0,10).map(n=>String(n+1));return e.length>10?`${t.join(", ")}, ...`:t.join(", ")}var yg=["HC3","const","HC","HC0","HC1","HC2","HC4","HC4m","HC5"];function Mg(e,t={}){V(t,bg,"vcovHC"),As(e);let n=ae(t.type,yg),r=n==="HC"?"HC0":n,o=t.sandwich??!0;if(typeof o!="boolean")throw new TypeError("'sandwich' must be true or false");let s=Mm(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(xs)?[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) ${gi(g)}`:`${r} covariances are numerically unstable for hat values close to 1 (and undefined if exactly 1) as for observation(s) ${gi(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) ${gi(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=Mi(e).vcov;else if(o){let g=Tr(s.X,u,a);if(p=bo(s.A,g,a*s.phiS*s.phiS),!Em(s,g,p,(h===void 0?u:l).every(d=>d===0))){let d=s.scaled();if(h!==void 0){let v=bo(d.B,Tr(d.T,h(d.u.map(N=>N*N)),a),a);p=Jn(v,d.e.map(N=>d.s-N))}else{let{T:v,factor:N}=d.omegaPart(),b=Ln(u.map(E=>wo(Wt(E),N)),!0);p=Jn(bo(d.B,Tr(v,b.v,a),a),d.e.map(E=>b.s/2-E))}}p=bi(p,s.k)}else{let g=Tr(s.X,u,a);if(Tm(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}=Am(d,Tr(d.T,h(d.u.map(b=>b*b)),a));p=Jn(v,N)}else{let v=Ln(u.map(Wt),!0);p=Jn(Tr(d.omegaPart().T,v.v,a),d.e.map(N=>v.s/2+N))}}p=bi(p,-s.k)}return W("vcovHC",{vcov:p,names:s.names,warnings:i},e)}var Eg=["HC","HC0","HC1","HC2","HC3"];function Rg(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 it(e);if(typeof e=="string")return e;throw new TypeError("cluster labels must be numbers, strings or booleans")}function xg(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 Tg(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 Rm=xs**(1/1.3);function xm(e,t){let{values:n,vectors:r}=Et(e,{symmetric:!0});if(n.some(a=>!(a>=Rm)))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 Ag(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 Sg(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 kg(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]>=Rm))throw wi(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??=ym(e.qr,e.rank);let N=qg(e,l,f,h,o);if(N===null)throw wi(m);if(N!==void 0)return Array.from({length:s},(b,E)=>c*u.reduce((w,M,R)=>w+M[E]*N[R],0))}let p=u.map(N=>Array.from({length:s},(b,E)=>N.reduce((w,M,R)=>w+M*t.A[R][E],0))),g=u.map((N,b)=>u.map((E,w)=>(b===w?1:0)-p[b].reduce((M,R,y)=>M+R*E[y],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=xm(g,o);if(d===void 0)throw wi(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,M)=>E+w[b]*v[M],0))})}function qg(e,t,n,r,o){let s=e.qr,a=e.rank,c=new Array(a).fill(0),i=n.map(w=>{let M=e.qrRow(w);return M<0?c:t[M]}),l=Array.from({length:a},(w,M)=>Array.from({length:a},(R,y)=>i.reduce((x,T)=>x+T[M]*T[y],0))),{values:f,vectors:m}=Et(l,{symmetric:!0});if(!f.some(w=>w>.5))return;let u=m,h=r.slice(),p=[];f.forEach((w,M)=>{let R=i.map(y=>y.reduce((x,T,A)=>x+T*u[A][M],0));if(w>.5)p.push(R.map(y=>y/Math.sqrt(w)));else if(w>0){let x=Math.expm1(o*Math.log1p(-w))/w*R.reduce((T,A,S)=>T+A*r[S],0);R.forEach((T,A)=>h[A]+=x*T)}});let g=s.qr.length,d=p.map(w=>{let M=new Array(g).fill(0);return n.forEach((R,y)=>{let x=e.qrRow(R);x>=0&&(M[x]=w[y])}),Js(s,M).slice(a)}),v=d.map(w=>d.map(M=>w.reduce((R,y,x)=>R+y*M[x],0))),N=xm(v,o);if(N===void 0)return null;let b=p.map(w=>w.reduce((M,R,y)=>M+R*r[y],0)),E=N.map((w,M)=>w.reduce((R,y,x)=>R+y*b[x],0)-b[M]);return p.forEach((w,M)=>w.forEach((R,y)=>h[y]+=E[M]*R)),h}function wi(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 Ig(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 vi(e){if(e.length===0)return e;let{values:t,vectors:n}=Et(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 _g(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}=Et(o,{symmetric:!0}),a=s.reduce((i,l)=>Math.max(i,Math.abs(l)),0);if(s.every(i=>i>=-16*n*xs*a))return{mant:e,exp:t};let c=Math.max(...t);return{mant:vi(e.map((i,l)=>i.map((f,m)=>On(f,t[l]+t[m]-2*c)))),exp:t.map(()=>c)}}function Tm(e,t){return t||e.every((n,r)=>er(n[r])&&n.every(Number.isFinite))}function Am(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 Lg(e,t={}){V(t,vg,"vcovCL"),As(e);let n=Mm(e,!0),r=n.X.length,o=n.names.length,s=t.type===void 0?n.glm?"HC0":"HC1":ae(t.type,Eg),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=M=>Array.isArray(M)||ArrayBuffer.isView(M);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(M=>{if(M.length!==n.nInput)throw new RangeError("number of observations in 'cluster' and 'estfun()' do not match");return n.inputRows.map(R=>Rg(M[R]))})}if(a==="HC1"&&r<=o)throw new RangeError("HC1 covariances need more observations than coefficients");let h=m.length,p=Tg(h).map((b,E)=>{let w=i&&h>1&&E===2**h-2,M=w?n.inputRows.map(String):n.inputRows.map((T,A)=>b.length===1?m[b[0]][A]:JSON.stringify(b.map(S=>m[S][A]))),{g:R,G:y}=xg(M);if(y<2)throw new RangeError("at least two clusters are needed");let x=w?a==="HC1"?(r-o)/(r-1):1:c?y/(y-1):1;return{g:R,G:y,c:x,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:M,c:R,sign:y}of p){let x;a==="HC2"||a==="HC3"?x=kg(n,b,w,M,a==="HC2"?-.5:-1):M===r?x=n.X.map((A,S)=>b.psi(S)):(x=Array.from({length:M},()=>new Array(o).fill(0)),n.X.forEach((A,S)=>{let k=x[w[S]],_=b.psi(S);for(let I=0;I<o;I++)k[I]+=_[I]}));let T=Ig(x,o);E=E.map((A,S)=>A.map((k,_)=>k+y*R*T[S][_]/r))}return a==="HC1"&&(E=E.map(w=>w.map(M=>M*(r-1)/(r-o)))),E},d=n.res.every(b=>b===0),v=n.phiS>=Ei&&n.phiS<1/0&&n.A.every((b,E)=>er(b[E]))&&n.res.every(Number.isFinite),N;if(v){let b=g(Ag(n));if(!f)Tm(b,d)&&(N=l?vi(b):b);else{let E=bo(n.A,b,r*n.phiS*n.phiS);Em(n,b,E,d)&&(N=l?vi(E):E)}}if(N===void 0){let b=n.scaled(),E=g(Sg(b)),{mant:w,exp:M}=f?{mant:bo(b.B,E,r),exp:b.e.map(y=>b.s-y)}:Am(b,E),R=l?_g(w,M):{mant:w,exp:M};N=Jn(R.mant,R.exp)}return N=bi(N,f?n.k:-n.k),W("vcovCL",{vcov:N,names:n.names,warnings:[]},e)}var Og=["vcov","df"],Cg=["parm","level","vcov","df"];function Sm(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 qm=e=>new Set(e).size===e.length;function Im(e,t){As(e);let n=e.coefficients,r=Ss(e),o=n.length,s=n.map(m=>Number.isNaN(m)),a,c,i=[],l;if(t===void 0){let m=Mi(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:yi(m.vcov,p,m.vcov[p][p])??Math.sqrt(m.vcov[p][p]))}else{let m=typeof t=="function"?ke(()=>t(e)):t,u=Sm(m)?m.vcov:m;if(Sm(m)){if(a=km(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=km(m);l=a.map((h,p)=>{let g=h[p],d=typeof u=="object"&&u!==null?yi(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&&qm(c)&&qm(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 _m(e,t){if(t===void 0)return Cn(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 Lm=e=>Number.isFinite(e)&&e>0;function Fg(e){return Cn(e)?e.priorWeights.filter(t=>t!==0).length:e.rank+e.dfResidual}function Dg(e,t={}){V(t,Og,"coeftest");let{est:n,se:r,names:o,warnings:s}=Im(e,t.vcov),a=_m(e,t.df),c=Lm(a),i=n.map((f,m)=>f/r[m]),l=i.map(f=>Number.isNaN(f)?NaN:c?2*$e(-Math.abs(f),a,!0,!1):2*Y(-Math.abs(f)));return W("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:Fg(e),warnings:s},e,typeof t.vcov=="function"?void 0:t.vcov)}function Pg(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 Vg(e,t={}){V(t,Cg,"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=Im(e,t.vcov),o=_m(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]=Lm(o)?[Ke(c,o,!0,!1),c<1e-8?Ke(c,o,!1,!1):Ke(1-c,o,!0,!1)]:[Z(c),c<1e-8?Z(c,{lowerTail:!1}):Z(1-c)],f=(m,u,h)=>m+h*u;return W("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:Pg(c),warnings:r.warnings},e,typeof t.vcov=="function"?void 0:t.vcov)}var $g=["test","dispersion"],jg=(e,t)=>Ge(e,t/2,2,!1,!1);function zg(e,t,n){let{fit:r,w:o}=Rr(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 Ri=(e,t)=>di(t.x,e.fit.workingResiduals,e.fit.workingWeights,e.intercept);function Wg(e,t={}){V(t,$g,"glmAnova");let n=Array.isArray(e)?e:[e];if(n.length===0)throw new RangeError("no fits to compare");let r=n.map(Ot),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:xr(t.dispersion,o),c=a?.s??0,i=Bg(s,Array.isArray(e)&&s.length>1,{...t,...a===void 0?{}:{dispersion:a.m}},c);if(o===0&&c===0)return W("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]?L(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 W("glmAnova",{...i,table:m},...n)}function Bg(e,t,n,r=0){let o=new Ue,s=e[0],a=dr(s.model.familySpec);if(e.some(y=>{let x=dr(y.model.familySpec);return x.family!==a.family||x.name!==a.name||x.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 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 x=(T,A)=>T.length===A.length&&T.every((S,k)=>JSON.stringify(S)===JSON.stringify(A[k]));if(e.some(T=>!x(T.model.yGiven,e[0].model.yGiven)))throw new RangeError("models were not all fitted to the same response");if(e.some(T=>!x(T.model.priorGiven,e[0].model.priorGiven)))throw new RangeError("models were not all fitted with the same prior weights");if(e.forEach((T,A)=>l.push({name:String(A+1),dfRes:T.dfResidual,dev:T.deviance})),f=l.map((T,A)=>A===0?NaN:l[A-1].dfRes-T.dfRes),m=l.map((T,A)=>A===0?NaN:l[A-1].dev-T.dev),e.forEach((T,A)=>{if(A===0||f[A]===0)return;let[S,k]=f[A]>0?[e[A-1],T]:[T,e[A-1]],_=q=>q.filter((F,C)=>S.priorWeights[C]>0&&k.priorWeights[C]>0),I=S.model.offset.map((q,F)=>q-k.model.offset[F]),P=I.some((q,F)=>q!==0&&S.priorWeights[F]>0)?S.model.x.map((q,F)=>[...q,I[F]]):S.model.x;Uo(_(P),_(k.model.x))||h.add(A)}),i==="Rao"){let T=e.map(A=>({dfRes:A.dfResidual,dev:A.deviance,fit:Om(A),x:A.model.x,intercept:A.model.intercept}));u=l.map((A,S)=>S===0?NaN:f[S]<0?-Ri(T[S],T[S-1]):Ri(T[S-1],T[S]))}p=e.reduce((T,A)=>A.dfResidual<T.dfResidual?A:T)}else{let y=s,x=y.model,T=x.x.length>0?x.x[0].length-(x.intercept?1:0):0,A=x.intercept?[0]:[],S=_=>[...A,...Array.from({length:_},(I,P)=>P+A.length)],k=[];for(let _=0;_<=T;_++)_===T?k.push({dfRes:y.dfResidual,dev:y.deviance,fit:Om(y),x:x.x,intercept:x.intercept}):_>0||i==="Rao"?k.push(zg(x,S(_),o)):k.push(void 0);l.push({name:"NULL",dfRes:y.dfNull,dev:y.nullDeviance});for(let _=1;_<=T;_++)l.push({name:`x${_}`,dfRes:k[_].dfRes,dev:k[_].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:Ri(k[I-1],k[I]))),p=y}e.forEach((y,x)=>{y.converged||o.add(`model ${x+1} has not converged: its deviance is not that of the maximum likelihood fit`)});let g=new Ue,d=n.dispersion??ho(p,g);g.list.forEach(y=>o.add(y));let v=c?1/0:p.dfResidual,N=t?["Resid. Df","Resid. Dev","Df","Deviance"]:["Df","Deviance","Resid. Df","Resid. Dev"],b=l.map((y,x)=>t?[y.dfRes,y.dev,f[x],m[x]]:[f[x],m[x],y.dfRes,y.dev]),E=[...N],w=y=>l.map((x,T)=>{if(T===0||f[T]===0||h.has(T))return NaN;let A=ye(y[T]/d*Math.sign(f[T]),-2*r);return A<0||Number.isNaN(A)?NaN:jg(A,Math.abs(f[T]))}),M=[];if(i==="Chisq"||i==="LRT")E.push("Pr(>Chi)"),M=[w(m)];else if(i==="Rao")E.push("Rao","Pr(>Chi)"),M=[u.map((y,x)=>h.has(x)?NaN:y),w(u)];else if(i==="F"){if(c&&o.add(`using F test with a '${a.family}' family is inappropriate`),v!==0){let y=l.map((x,T)=>{let A=ye(m[T]/f[T]/d,-2*r);return T===0||f[T]===0||h.has(T)||A<0?NaN:A});E.push("F","Pr(>F)"),M=[y,y.map((x,T)=>Number.isNaN(x)?NaN:We(x,Math.abs(f[T]),v,!1,!1))]}}else if(i==="Cp"){let y=p.model.x.length;E.push("Cp"),M=[l.map(x=>x.dev+2*(y-x.dfRes)*d)]}let R=b.map((y,x)=>[...y,...M.map(T=>T[x])]);return{colNames:E,rowNames:l.map(y=>y.name),table:R,warnings:o.list}}function Om(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 Kg=["type"];function Cm(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=et(a.map((p,g)=>s.map(d=>e.x[p][d]*c[g]))),l=Ms(e.weights===void 0?void 0:a.map(p=>L(e.weights[p],-o)),a.length),f=i.pivot.slice(0,i.rank),{qty:m}=Nr(f.map(p=>a.map((g,d)=>st(l[d],e.x[g][s[p]]))),a.map((p,g)=>st(l[g],L(e.y[p],-r)))),u=[0,0],h=0;return f.forEach((p,g)=>{p>=t.length&&(u=H(u,ce(m[g],m[g])),h++)}),{ss:u[0],df:h}}function xi(e,t={}){if(V(t,Kg,"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=ms([e]),a=us([e],s)[0];if(o===0)throw new Error("residual df = 0");let c=_a(e,s),i=M=>{let R=c.map(([T,A])=>[L(T,-M),A]),y=R.reduce((T,[A])=>T+A,0),x=R.reduce((T,[A,S])=>T+A*S,0)/y;return Number.isFinite(y)&&Number.isFinite(x)?R.reduce((T,[A,S])=>T+A*(S-x)**2,0):NaN},l=0,f=i(0);if(Number.isFinite(f)||(l=hs(c),f=i(l)),Hn(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=M=>e.assign.flatMap((R,y)=>M.has(R)?[y]:[]),p=[...new Set(e.assign)],g=[],d=[],v=M=>{g.length=0;let R=[];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 x=h(new Set([y])),T=h(new Set(p.filter(A=>A!==y)));g.push(y===0?"(Intercept)":e.termLabels[y-1]),R.push({ss:Cm(e,T,x,s,M).ss,df:x.length})}}else{let y=x=>e.factors.flatMap((T,A)=>T[x-1]>0?[A]:[]);for(let x of p){if(x===0)continue;let T=y(x),A=p.filter(_=>_!==0&&_!==x&&T.every(I=>e.factors[I][_-1]>0)),S=h(new Set(p.filter(_=>_!==x&&!A.includes(_)))),k=Cm(e,S,h(new Set([x])),s,M);g.push(e.termLabels[x-1]),R.push(k)}}return R};d=v(l),l===0&&e.weights!==void 0&&d.some(M=>!Number.isFinite(M.ss))&&(l=hs(c),d=v(l));let N=M=>L(M,2*s+l),b=L(a,-l),E=b/o,w=d.map(({ss:M,df:R})=>{let y=M/R/E;return[N(M),R,y,We(y,R,o,!1,!1)]});return w.push([N(b),o,NaN,NaN]),W("Anova",{colNames:["Sum Sq","Df","F value","Pr(>F)"],rowNames:[...g,"Residuals"],table:w,warnings:m},e)}var ks=[0,0];var Ti=e=>e.map(t=>t.map(n=>n[0]));function Fm(e){let t=e[0]?.length??0;return Array.from({length:t},(n,r)=>e.map(o=>o[r]))}function tr(e,t){let n=t[0]?.length??0;return e.map(r=>{let o=new Array(n).fill(ks);return r.forEach((s,a)=>{let c=t[a];for(let i=0;i<n;i++)o[i]=H(o[i],ce(s,c[i]))}),o})}function nr(e){let t=ks;return e.forEach((n,r)=>t=H(t,n[r])),t}function Dm(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=Pe(n[i][a],n[a][a]);n[i][a]=l;for(let f=a+1;f<t;f++)n[i][f]=he(n[i][f],ce(l,n[a][f]))}}return{a:n,perm:r,sign:o,singular:s}}function Pm(e){let{a:t,sign:n,singular:r}=Dm(e);if(r)return ks;let o=[n,0];return t.forEach((s,a)=>o=ce(o,s[a])),o}function Vm(e){let t=e.length,{a:n,perm:r,singular:o}=Dm(e);if(o)throw new Error("singular matrix");let s=Array.from({length:t},()=>new Array(t).fill(ks));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]=he(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]=he(c[i],ce(n[i][l],c[l]));c[i]=Pe(c[i],n[i][i])}for(let i=0;i<t;i++)s[i][a]=c[i]}return s}var Ug=["group","idata","M","X"],$m=(e,t)=>Ge(e,t/2,2,!1,!1);function Ai(e){let t=e.length,n=Ve(nr(e),t);return Pm(e.map(r=>r.map(o=>Pe(o,n))))[0]}function Si(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=$m(a,c),l=$m(a,c+4);return i+s*(l-i)}var Hg=(e,t)=>Array.from({length:t},(n,r)=>e.map(o=>o[r]));function Gg(e,t,n){let r=e.length,o=v=>v.length===0?0:et(Array.from({length:r},(N,b)=>v.map(E=>E[b]))).rank,s=n.length===0?void 0:et(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=et(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=bn(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=bn(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 Yg(e,t={}){V(t,Ug,"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,M)=>!w.some(s)&&(o===void 0||!s(o[M])));if(n.some((w,M)=>a[M]&&w.some(R=>!Number.isFinite(R))))throw new RangeError("y must not contain infinite values");let c=se(n.filter((w,M)=>a[M])).groups,i=c.length,l=o?.filter((w,M)=>a[M]).map(it)??c.map(()=>""),f=[...new Set(l)].sort(Ht),m=c.map(w=>w.map(()=>[0,0]));for(let w of f){let M=l.flatMap((R,y)=>R===w?[y]:[]);for(let R=0;R<r;R++){let y=[0,0];for(let T of M)y=H(y,[c[T][R],0]);let x=Ve(y,M.length);for(let T of M)m[T][R]=he([c[T][R],0],x)}}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,M)=>Array.from({length:r},(R,y)=>{let x=[0,0];for(let T of m)x=H(x,ce(T[M],T[y]));return x})),p=t.idata??{index:Array.from({length:r},(w,M)=>M+1)},g=w=>{let M=ke(()=>pr(w,p));if(M.x.length!==r)throw new RangeError("idata must have one row per column of y");return Hg(M.x,M.colNames.length)},d=t.M===void 0?Array.from({length:r},(w,M)=>Array.from({length:r},(R,y)=>y===M?1:0)):g(t.M),v=g(t.X??"~0"),N=Gg(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=Ai(N);return{statistic:E,statisticName:"W",pValue:Si(E,b,u,r),method:"Mauchly's test of sphericity",warnings:[]}}var Xg=["n","d","type","sigLevel","power","alternative"],Qg=["n1","n2","d","sigLevel","power","alternative"],Zg=["h","n","sigLevel","power","alternative"],Jg=["h","n1","n2","sigLevel","power","alternative"],ew=["h","n","sigLevel","power","alternative"],tw=["d","n","sigLevel","power","alternative"],nw=["n","r","sigLevel","power","alternative"],rw=["w","N","df","sigLevel","power"],ow=["k","n","f","sigLevel","power"],sw=["u","v","f2","sigLevel","power"],aw=["size"],iw={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 zm(e="p",...t){V(mr(t),aw,"cohenES");let n=fr(["size"],t).size??"small",r=ae(e,["p","t","r","anov","chisq","f2"]),o=ae(n,["small","medium","large"]);return{test:r,size:o,effectSize:iw[r][["small","medium","large"].indexOf(o)],method:"Conventional effect size from Cohen (1982)"}}function cw(e,t){if(!qs(e)||!qs(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 lw(e,t){if(e.length!==t.length)throw new RangeError("'P0' and 'P1' must have the same length");if(![...e,...t].every(qs))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+=wt(t[r]-e[r],e[r]));return Math.sqrt(n)}function fw(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(qs)))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>=mw)o+=wt(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 mw=2**-1022,qs=e=>e>=0&&e<=1,uw="\u2018sig.level\u2019 must be numeric in [0, 1]",hw="\u2018power\u2019 must be numeric in [0, 1]";function Fn(e,t){let n=r=>!re(r)&&!(typeof r=="number"&&r>=0&&r<=1);if(n(e))throw new RangeError(uw);if(n(t))throw new RangeError(hw)}function Dn(e,t){if(typeof e=="number"&&Number.isNaN(e))throw new RangeError("the effect size must not be NaN");return typeof e=="string"?zm(t,e).effectSize:e}var vo=["two.sided","less","greater"];function Is(e,t){return e==="two.sided"?t:e==="less"?[-10,5]:[-5,10]}function Wm(e,t,n,r){if(r==="less")return Bn(on(n,e),e,t,!0);if(r==="greater")return Bn(on(n,e,{lowerTail:!1}),e,t,!1);let o=on(n/2,e,{lowerTail:!1});return Bn(o,e,t,!1)+Bn(-o,e,t,!0)}var jm=e=>e<1e-8?Z(e,{lowerTail:!1}):Z(1-e);function Bm(e,t,n){return n==="greater"?Y(jm(t)-e,{lowerTail:!1}):n==="less"?Y(Z(t)-e):Y(jm(t/2)-e,{lowerTail:!1})+Y(Z(t/2)-e)}function pw(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*=L(r,-o),n+=o}return L(t,n)}function dw(e={}){V(e,Xg,"pwrTTest");let{n:t,power:n}=e,r=Dn(e.d,"t"),o=e.sigLevel===void 0?.05:e.sigLevel;if(Gt([t,r,n,o],"exactly one of n, d, power, and sig.level must be NULL"),Fn(o,n),!re(t)&&!(t>=2))throw new RangeError("number of observations in each group must be at least 2");let s=ae(e.type,["two.sample","one.sample","paired"]),a=ae(e.alternative,vo),c=s==="two.sample"?2:1;a==="two.sided"&&!re(r)&&(r=Math.abs(r));let i=(m,u,h)=>Wm((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=be(u=>i(u,r,o)-m,2+1e-10,1e9):re(r)?r=be(u=>i(t,u,o)-m,...Is(a,[1e-7,10])):o=be(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 gw(e={}){V(e,Qg,"pwrT2nTest");let{n1:t,n2:n,power:r}=e,o=Dn(e.d,"t"),s=e.sigLevel===void 0?.05:e.sigLevel;if(Gt([t,n,o,r,s],"exactly one of n1, n2, d, power, and sig.level must be NULL"),Fn(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=ae(e.alternative,vo);a==="two.sided"&&!re(o)&&(o=Math.abs(o));let c=(i,l,f,m)=>Wm(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=be(l=>c(l,n,o,s)-i,2+1e-10,1e9):re(n)?n=be(l=>c(t,l,o,s)-i,2+1e-10,1e9):re(o)?o=be(l=>c(t,n,l,s)-i,...Is(a,[1e-7,10])):s=be(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 ki(e,t,n,r,o,s){let{n:a,power:c}=t,i=Dn(e,n==="h"?"p":"t"),l=t.sigLevel===void 0?.05:t.sigLevel;if(Gt([i,a,c,l],`exactly one of ${n}, n, power, and sig.level must be NULL`),Fn(l,c),!re(a)&&!(a>=1))throw new RangeError("number of observations in each group must be at least 1");let f=ae(t.alternative,vo);f==="two.sided"&&!re(i)&&(i=Math.abs(i));let m=(u,h,p)=>Bm(u*r(h),p,f);if(re(c))c=m(i,a,l);else{let u=c;re(i)?i=be(h=>m(h,a,l)-u,...Is(f,[s,10])):re(a)?a=be(h=>m(i,h,l)-u,o,1e9):l=be(h=>m(i,a,h)-u,1e-10,1-1e-10)}return{es:i,n:a,sigLevel:l,power:c,alternative:f}}function ww(e={}){V(e,Zg,"pwr2pTest");let t=ki(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 bw(e={}){V(e,ew,"pwrPTest");let t=ki(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 vw(e={}){V(e,tw,"pwrNormTest");let t=ki(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 Nw(e={}){V(e,Jg,"pwr2p2nTest");let{n1:t,n2:n,power:r}=e,o=Dn(e.h,"p"),s=e.sigLevel===void 0?.05:e.sigLevel;if(Gt([o,t,n,r,s],"exactly one of h, n1, n2, power, and sig.level must be NULL"),Fn(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=ae(e.alternative,vo);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)=>Bm(l*Math.sqrt(c(f,m)),u,a);if(re(r))r=i(o,t,n,s);else{let l=r;re(o)?o=be(f=>i(f,t,n,s)-l,...Is(a,[1e-10,10])):re(t)?t=be(f=>i(o,f,n,s)-l,2+1e-10,1e9):re(n)?n=be(f=>i(o,t,f,s)-l,2+1e-10,1e9):s=be(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 yw(e={}){V(e,nw,"pwrRTest");let{n:t,power:n}=e,r=Dn(e.r,"r"),o=e.sigLevel===void 0?.05:e.sigLevel;if(Gt([t,r,n,o],"exactly one of n, r, power, and sig.level must be NULL"),Fn(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=ae(e.alternative,vo);s==="two.sided"&&!re(r)&&(r=Math.abs(r));let a=(i,l,f,m)=>{let u=on(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=Y((g-d)*Math.sqrt(i-3));return m?v+Y((-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=be(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=be(m=>c(t,m,o)-i,l,f)}else o=be(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 Mw(e){V(e,rw,"pwrChisqTest");let{N:t,power:n}=e,{df:r}=e,o=Dn(e.w,"chisq"),s=e.sigLevel===void 0?.05:e.sigLevel;if(Gt([o,t,n,s],"exactly one of w, N, power, and sig.level must be NULL"),typeof r!="number")throw new TypeError('argument "df" is missing, with no default');if(!(r>0&&r<1/0))throw new RangeError("'df' must be a positive finite number");if(Fn(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:hn(zr(l,r,{lowerTail:!1}),r,{ncp:f,lowerTail:!1})};if(re(n))n=a(o,t,s);else{let c=n;re(o)?o=be(i=>a(i,t,s)-c,1e-10,1e9):re(t)?t=be(i=>a(o,i,s)-c,1+1e-10,1e9):s=be(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 Ew(e={}){V(e,ow,"pwrAnovaTest");let{k:t,n,power:r}=e,o=Dn(e.f,"anov"),s=e.sigLevel===void 0?.05:e.sigLevel;if(Gt([t,n,o,r,s],"exactly one of k, n, f, power, and sig.level must be NULL"),Fn(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=pw([c,i,l,l])),h===1/0?1:Qo(m,u)?Zo(m,u,h,f):jn(yn(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=be(i=>a(i,n,o,s)-c,2+1e-10,100):re(n)?n=be(i=>a(t,i,o,s)-c,2+1e-10,1e9):re(o)?o=be(i=>a(t,n,i,s)-c,1e-7,1e7):s=be(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 Rw(e={}){V(e,sw,"pwrF2Test");let{u:t,v:n,power:r}=e,o=Dn(e.f2,"f2"),s=e.sigLevel===void 0?.05:e.sigLevel;if(Gt([t,n,o,r,s],"exactly one of u, v, f2, power, and sig.level must be NULL"),Fn(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:Qo(c,i)?Zo(c,i,m,f):jn(yn(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=be(i=>a(i,n,o,s)-c,1+1e-10,100):re(n)?n=be(i=>a(t,i,o,s)-c,1+1e-10,1e9):re(o)?o=be(i=>a(t,n,i,s)-c,1e-7,1e7):s=be(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 xw=["id","dv","between","within","type","correction","es","observed"],_s=e=>e===void 0?[]:typeof e=="string"?[e]:[...e],Tw=1e-12,Km=1e-24;function Um(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 No=e=>typeof e=="boolean"?e?"TRUE":"FALSE":it(e);function Aw(e){let t=[...new Set(e)];return t.every(n=>typeof n=="number")?t.sort((n,r)=>n-r).map(No):[...new Set(t.map(No))].sort(Ht)}var Hm=(e,t)=>t.split(":").includes(e);function Gm(e,t,n,r,o){let s=l=>r.some(f=>Hm(f,l));for(let l of r)if(!e.some(f=>Hm(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 Sw(e,t){V(t,xw,"aovEz");let n=_s(t.between),r=_s(t.within);if(n.length===0&&r.length===0)throw new Error("Either between or within need to be non-NULL!");for(let[j,X]of[["between",n],["within",r]]){let ne=X.filter((de,je)=>X.indexOf(de)!==je);if(ne.length>0)throw new RangeError(`duplicated factor names in '${j}': ${[...new Set(ne)].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=_s(t.es??"ges");for(let j of c)if(j!=="ges"&&j!=="pes")throw new RangeError(`invalid 'es': ${String(j)}`);let i=_s(t.observed),l=[],f=j=>j==null||typeof j=="number"&&Number.isNaN(j),m=Um(e,t.dv),u=[t.id,...n,...r].map(j=>Um(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,X)=>u.every(ne=>!f(ne[X]))),p=j=>j.filter((X,ne)=>h[ne]),g=dt(p(u[0])).map(No),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,X)=>dt(p(u[1+X]))),N=r.map((j,X)=>dt(p(u[1+n.length+X]))),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=>Aw(j)),w=v.map(j=>j.map(No)),M=N.map(j=>j.map(No)),R=M.map(j=>[...new Set(j)]);E.forEach((j,X)=>{if(j.length<2)throw new Error(`Factor "${n[X]}" consists of one level only. Remove factor from model?`)}),R.forEach((j,X)=>{if(j.length<2)throw new Error(`Factor "${r[X]}" consists of one level only. Remove factor from model?`)});let y=[...new Set(g)],x=new Map,T=new Set;if(g.forEach((j,X)=>{let ne=JSON.stringify(w.map(je=>je[X])),de=x.get(j);de===void 0?x.set(j,ne):de!==ne&&T.add(j)}),T.size>0)throw new Error(`Following ids are in more than one between subjects condition:
8
- ${y.filter(j=>T.has(j)).join(", ")}`);let A=[[]];for(let j of R)A=A.flatMap(X=>j.map(ne=>[...X,ne]));let S=j=>JSON.stringify(M.map(X=>X[j])),k=new Map(A.map((j,X)=>[JSON.stringify(j),X])),_=new Set(g.map((j,X)=>S(X)));if(A.some(j=>!_.has(JSON.stringify(j))))throw new Error("Empty cells in within-subjects design (i.e., bad data structure).");let I=new Map,P=new Set;g.forEach((j,X)=>{let ne=I.get(j);ne===void 0&&I.set(j,ne=new Set),ne.add(S(X)),Number.isNaN(d[X])&&P.add(j)});let q=y.filter(j=>P.has(j)||I.get(j).size<A.length),F=new Set(q),C=se([d.filter((j,X)=>!F.has(g[X]))]).k,D=C===0?d:d.map(j=>L(j,-C)),O=j=>L(j,2*C),z=A.length,B=new Map;if(g.forEach((j,X)=>{let ne=B.get(j);ne===void 0&&B.set(j,ne={s:A.map(()=>[0,0]),n:A.map(()=>0)});let de=k.get(S(X));ne.s[de]=H(ne.s[de],[D[X],0]),ne.n[de]++}),[...B.values()].some(j=>j.n.some(X=>X>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."),q.length>0){let j=[...q].sort(Ht),X=j.length>=10?`${j.slice(0,10).join(", ")}, ... [showing first 10 only]`:j.join(", ");l.push(`Missing values for ${q.length} ID(s), which were removed before analysis:
9
- ${X}`)}let Q=y.filter(j=>!q.includes(j)),J=new Map;g.forEach((j,X)=>J.has(j)||J.set(j,X));let oe=Q.map(j=>{let X=B.get(j);return X.s.map((ne,de)=>Ve(ne,X.n[de]))}),pe={};n.forEach((j,X)=>pe[j]=Q.map(ne=>w[X][J.get(ne)]));let we=Object.fromEntries(n.map(j=>[j,"contr.sum"])),Ee=Object.fromEntries(n.map((j,X)=>[j,E[X]])),G="Rank deficient model matrix; insufficient data to estimate full model.";if(z===1)return W("aovEz",K());return W("aovEz",te());function K(){let j="y";for(;n.includes(j);)j+="_";let X={...pe,[j]:oe.map(gt=>gt[0][0])},ne=ke(()=>li(`${j} ~ ${n.length>0?n.join(" * "):"1"}`,X,{contrasts:we,levels:Ee}));if(ne.rank<ne.coefNames.length)throw new Error(G);if(ne.dfResidual<=0)throw new RangeError(`no residual degrees of freedom: ${oe.length} subject(s) for ${ne.rank} parameter(s)`);let de=ke(()=>xi(ne,{type:s}));l.push(...de.warnings.filter(gt=>!l.includes(gt)));let je=ne.dfResidual,Oe=de.rowNames.flatMap((gt,nt)=>{if(gt==="(Intercept)"||gt==="Residuals")return[];let[ge,ze,Ie,xe]=de.table[nt];return[{name:gt,ss:ge,numDf:ze,errSS:ne.rss,denDf:je,f:Ie,p:xe}]}),Xe=de.colNames.indexOf("Sum Sq"),qe={anova:Gm(Oe,new Map([[0,ne.rss]]),c,i,O),univariate:{colNames:de.colNames,rowNames:de.rowNames,table:de.table.map(gt=>gt.map((nt,ge)=>ge===Xe?O(nt):nt))},within:r,warnings:l};return xt.set(qe,{colNames:de.colNames,rowNames:de.rowNames,table:de.table}),qe}function te(){let j=Q.length,X=oe.reduce((ve,Ae)=>Ae.reduce((Se,U)=>Se+U[0]*U[0],ve),0),ne=n.length>0?ke(()=>pr(`~ ${n.join(" * ")}`,pe,{contrasts:we,levels:Ee})):{colNames:["(Intercept)"],assign:[0],x:Q.map(()=>[1]),termLabels:[],factors:[]},de=ne.colNames.length;if(j===0||et(ne.x).rank<de)throw new Error(G);let je=j-de;if(je<=0)throw new RangeError(`no residual degrees of freedom: ${j} subject(s) for ${de} between-subject parameter(s)`);let Oe=[0,...ne.termLabels.map((ve,Ae)=>Ae+1)],Xe=["(Intercept)",...ne.termLabels],qe=ve=>ne.assign.flatMap((Ae,Se)=>ve.includes(Ae)?[Se]:[]),gt=ve=>ne.factors.flatMap((Ae,Se)=>Ae[ve-1]>0?[Se]:[]),nt=ve=>ne.x.map(Ae=>[Ae[ve],0]),ge=new Array(j).fill([0,0]),ze=(ve,Ae,Se)=>{let{qtv:U}=bn([...ve,...Ae].map(nt),ge,Se);return U.map(me=>me.slice(ve.length,ve.length+Ae.length))},Ie=ve=>{let{qtv:Ae}=bn(ne.colNames.map((Se,U)=>nt(U)),ge,ve);return Ae.map(Se=>Se.slice(de))},xe=Object.fromEntries(r.map((ve,Ae)=>[ve,A.map(Se=>Se[Ae])])),Fe=ke(()=>pr(`~ ${r.join(" * ")}`,xe,{contrasts:Object.fromEntries(r.map(ve=>[ve,"contr.sum"])),levels:Object.fromEntries(r.map((ve,Ae)=>[ve,R[Ae]]))})),at=[0,...Fe.termLabels.map((ve,Ae)=>Ae+1)],lt=["(Intercept)",...Fe.termLabels],qt=ve=>ve.map(Ae=>ve.map(Se=>{let U=[0,0];return Ae.forEach((me,Le)=>U=H(U,ce(me,Se[Le]))),U})),Dt=[],Pt=[],bt=[],ft=[],It=[],Xt=[],Tt=[],At=new Map,vn=!1;for(let ve of at){let Ae=Fe.assign.flatMap((fe,ie)=>fe===ve?[ie]:[]),Se=Ae.length,U=Ae.map(fe=>oe.map(ie=>ie.reduce((Re,He,Ct)=>H(Re,st(He,Fe.x[Ct][fe])),[0,0]))),me=Ae.map(fe=>Ae.map(ie=>[Fe.x.reduce((Re,He)=>Re+He[fe]*He[ie],0),0])),Le=Vm(me),rt=qt(Ie(U)),vt=tr(rt,Le),mt=nr(vt)[0],Nt=NaN,mn=NaN,$=NaN,ee=NaN,le=!1;if(Se>=2){let fe=lt[ve],ie=$c(Ti(Le)).map(yt=>yt.map(Qt=>[Qt,0])),Re=Ti(tr(tr(ie,rt),Fm(ie))),He=Et(Re,{symmetric:!0,onlyValues:!0}).values,Ct=He.reduce((yt,Qt)=>Math.max(yt,Qt),0),St=Math.max(Ct*Tw,X*Km),ut=nr(vt),ot=nr(tr(vt,vt));Ct>X*Km&&ot[0]>0&&Number.isFinite(ot[0])?(le=!0,Nt=Pe(ce(ut,ut),st(ot,Se))[0],je-Se*Nt>0?mn=((je+1)*Se*Nt-2)/(Se*(je-Se*Nt)):l.push(`The Huynh-Feldt epsilon of ${fe} is undefined (d - p GG <= 0)${a==="HF"?": corrected with GG":""}`)):l.push(`The error SSP matrix of ${fe} is 0 (or at the rounding level of the data): no sphericity corrections (the p-values are uncorrected)`),le&&He.every(yt=>yt>St)?($=Ai(tr(Le,rt)),ee=Si($,Se,je,z)):le&&l.push(`The error SSP matrix of ${fe} is singular: no Mauchly test (the epsilons are still defined)`)}for(let fe=0;fe<Oe.length;fe++){let ie=Oe[fe],Re;if(s===3)Re=ze(qe(Oe.filter(Zt=>Zt!==ie)),qe([ie]),U);else if(ie===0)Re=ze([],qe([0]),U);else{let Zt=gt(ie),Te=Oe.filter(ue=>ue!==0&&ue!==ie&&Zt.every(De=>ne.factors[De][ue-1]>0));Re=ze(qe(Oe.filter(ue=>ue!==ie&&!Te.includes(ue))),qe([ie]),U)}let He=nr(tr(qt(Re),Le))[0],St=(ie===0?1:qe([ie]).length)*Se,ut=je*Se,ot=He/St/(mt/ut),yt=We(ot,St,ut,!1,!1),Qt=ve===0?Xe[fe]:ie===0?lt[ve]:`${Xe[fe]}:${lt[ve]}`;Dt.push([O(He),St,O(mt),ut,ot,yt]),Pt.push([He,St,mt,ut,ot,yt]),bt.push(Qt),At.set(ve,mt);let un={name:Qt,ss:He,numDf:St,errSS:mt,denDf:ut,f:ot,p:yt};if(le){let Zt=Math.min(1,mn),Te=We(ot,Nt*St,Nt*ut,!1,!1),ue=We(ot,Zt*St,Zt*ut,!1,!1);ft.push([Nt,Te,mn,ue]),It.push([$,ee]),Xt.push(Qt),a==="GG"?un={...un,numDf:Nt*St,denDf:Nt*ut,p:Te}:a==="HF"&&(mn>1&&(vn=!0),un=Number.isNaN(mn)?{...un,numDf:Nt*St,denDf:Nt*ut,p:Te}:{...un,numDf:Zt*St,denDf:Zt*ut,p:ue})}ve===0&&ie===0||Tt.push(un)}}vn&&l.push("HF eps > 1 treated as 1");let Bt={anova:Gm(Tt,At,c,i,O),univariate:{colNames:["Sum Sq","num Df","Error SS","den Df","F value","Pr(>F)"],rowNames:bt,table:Dt},within:r,warnings:l};return ft.length>0&&(Bt.corrections={colNames:["GG eps","Pr(>F[GG])","HF eps","Pr(>F[HF])"],rowNames:Xt,table:ft},Bt.sphericity={colNames:["Test statistic","p-value"],rowNames:[...Xt],table:It}),xt.set(Bt,{colNames:Bt.univariate.colNames,rowNames:bt,table:Pt}),Bt}}function Ls(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=Ce(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 kw=["rho"],qw=["group","confType","confInt"],Iw=e=>e==null||typeof e=="number"&&Number.isNaN(e);function Ym(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&&Iw(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=dt(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=Ls(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=${it(i)}`)}}function _w(e,t,n,r={}){V(r,kw,"survdiff");let{rho:o=0}=r;if(!Number.isFinite(o))throw new RangeError("'rho' must be a finite number");let{obs:s,labels:a}=Ym(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,M=new Array(c).fill(0),R=new Array(c).fill(0),y=0;for(;w<s.length&&s[w].time===E;){let x=s[w];R[x.group]++,x.event&&(M[x.group]++,y++),w++}if(y>0){let x=o===0?1:p**o;for(let T=0;T<c;T++)l[T]+=x*M[T],f[T]+=x*y*u[T]/h;if(h>1){let T=x*x*y*(h-y)/(h*(h-1));for(let A=0;A<c;A++)for(let S=0;S<c;S++)m[A][S]+=T*u[A]*((A===S?1:0)-u[S]/h)}p*=1-y/h}for(let x=0;x<c;x++)u[x]-=R[x];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(M=>l[M]-f[M]),w=b.map(M=>b.map(R=>m[M][R]));try{let M=Mt(w,E);d=E.reduce((R,y,x)=>R+y*M[x],0)}catch{({chisq:d,df:v}=Lw(w,E))}}let N=v===0?NaN:hn(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 Lw(e,t){let n=Et(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 qi(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 sn(e[r],e[s])}return e[r]}function Ow(e,t,n={}){V(n,qw,"survfit");let{group:r,confType:o="log",confInt:s=.95}=n;if(o!=="log"&&o!=="plain"&&o!=="log-log")throw new RangeError(`invalid 'confType': ${String(o)}`);if(!(s>0&&s<1))throw new RangeError("'confInt' must be between 0 and 1");let{obs:a,labels:c}=Ym(e,t,r),i=r===void 0?["all"]:c,l=(1-s)/2,f=l<1e-8?Z(l,{lowerTail:!1}):Z(1-(1-s)/2),m=[],u={strata:[],time:[],nRisk:[],nEvent:[],nCensor:[],surv:[],stdErr:m,stdError:m,lower:[],upper:[],median:[],medianRows:i};return i.forEach((h,p)=>{let g=a.filter(w=>w.group===p),d=u.time.length,v=g.length,N=1,b=0;for(let w=0;w<g.length;){let M=g[w].time,R=w,y=0;for(;R<g.length&&g[R].time===M;)g[R].event&&y++,R++;let x=R-w-y;y>0&&(N*=1-y/v,b+=y<v?y/(v*(v-y)):1/0);let T=Math.sqrt(b),A,S;if(o==="log")A=N===0?NaN:N*Math.exp(-f*T),S=N===0?NaN:Math.min(1,N*Math.exp(f*T));else if(o==="plain"){let k=f*T*N;A=Number.isNaN(k)?NaN:Math.max(0,N-k),S=Number.isNaN(k)?NaN:Math.min(1,N+k)}else if(N===0)A=NaN,S=NaN;else if(N===1)A=1,S=1;else{let k=Math.log(N),_=Math.log(-k);A=Math.exp(-Math.exp(_-f*T/k)),S=Math.exp(-Math.exp(_+f*T/k))}u.strata.push(h),u.time.push(M),u.nRisk.push(v),u.nEvent.push(y),u.nCensor.push(x),u.surv.push(N),u.stdErr.push(N*T),u.lower.push(A),u.upper.push(S),v-=R-w,w=R}let E=u.time.slice(d);u.median.push([qi(E,u.surv.slice(d)),qi(E,u.lower.slice(d)),qi(E,u.upper.slice(d))])}),u}var Cw=new Set(["ties","weights","strata","init","control","nocenter","confLevel","robust","cluster"]),Fw=["eps","iterMax","tolerChol","tolerInf"],Xm=e=>e==null||typeof e=="number"&&Number.isNaN(e),Dw=(e,t)=>typeof e=="number"&&typeof t=="number"?e-t:String(e)<String(t)?-1:String(e)>String(t)?1:0,Pw=2**64,Vw=2**-1022,Qm=700;function Ii(e,t,n){let{time:r,event:o,weight:s,x:a,stratumEnd:c,logUnit:i}=e,l=r.length,f=t.length,m=()=>new Array(f).fill(0),u=()=>Array.from({length:f},m),h=m(),p=u(),g=0,d=0,v=m(),N=u(),b=m(),E=u(),w=0,M=0,R=y=>{let x=Math.exp(M-y);d*=x,w*=x;for(let T=0;T<f;T++){v[T]*=x,b[T]*=x;let A=N[T],S=E[T];for(let k=0;k<=T;k++)A[k]*=x,S[k]*=x}M=y};for(let y=l-1;y>=0;){c[y]&&(M=0,d=0,v=m(),N=u());let x=r[y],T=0,A=0;w=0;let S=y;do{let k=a[S],_=0;for(let q=0;q<f;q++)_+=t[q]*k[q];let I=s[S];Number.isFinite(_)&&(_-M>Qm?R(_):_-M<-Qm&&d===0&&w===0&&(M=_));let P=Math.exp(_-M)*I;if(o[S]){T++,A+=I,w+=P,g+=I*_;for(let q=0;q<f;q++){h[q]+=I*k[q],b[q]+=P*k[q];let F=E[q];for(let C=0;C<=q;C++)F[C]+=P*k[q]*k[C]}}else{d+=P;for(let q=0;q<f;q++){v[q]+=P*k[q];let F=N[q];for(let C=0;C<=q;C++)F[C]+=P*k[q]*k[C]}}S--}while(S>=0&&r[S]===x&&!c[S]);if(T>0){let k=n?T:1,_=n?T:1,I=A/k;for(let P=0;P<k;P++){d+=n?w/_:w,g-=I*(Math.log(d)+M+i);for(let q=0;q<f;q++){v[q]+=n?b[q]/_:b[q];let F=v[q]/d;h[q]-=I*F;let C=N[q],D=E[q],O=p[q];for(let z=0;z<=q;z++){C[z]+=n?D[z]/_:D[z];let B=C[z]-F*v[z],Q=I*B;O[z]+=Q<1/0&&Q>-1/0&&(Math.abs(Q)>=Vw||B===0)||!Number.isFinite(I)?Q/d:I*(B/d)}}}b.fill(0);for(let P of E)P.fill(0)}y=S}for(let y=0;y<f;y++)for(let x=0;x<y;x++)p[x][y]=p[y][x];return{loglik:g,score:h,info:p}}function Zm(e,t,n){let{time:r,event:o,x:s,stratumEnd:a}=e,c=se([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 x=u;x<=h;x++)p.push(m(x));let g=[];for(let x=u;x<=h;x++)(x===u||r[x]!==r[x-1])&&g.push(x);let d=x=>x+1<g.length?g[x+1]:h+1,v=new Array(g.length),N=new Array(g.length),b=new Array(g.length),E=-1/0,w=0,M=new Array(l).fill(0);for(let x=g.length-1;x>=0;x--){let T=Number.isFinite(E)?E:0;for(let S=g[x];S<d(x);S++)E=Math.max(E,p[S-u]);let A=Number.isFinite(E)?E:0;if(x<g.length-1&&A!==T){let S=Math.exp(T-A);w*=S;for(let k=0;k<l;k++)M[k]*=S}for(let S=g[x];S<d(x);S++){let k=c[S]*Math.exp(p[S-u]-A);w+=k;for(let _=0;_<l;_++)M[_]+=k*s[S][_]}v[x]=w,N[x]=M.slice(),b[x]=A}let R=0,y=new Array(l).fill(0);for(let x=0;x<g.length;x++){let T=b[x];if(x>0&&T!==b[x-1]){let C=Math.exp(T-b[x-1]);R*=C;for(let D=0;D<l;D++)y[D]*=C}let A=g[x],S=d(x),k=[];for(let C=A;C<S;C++)k.push(Math.exp(p[C-u]-T));let _=0,I=0,P=0,q=new Array(l).fill(0);for(let C=A;C<S;C++){if(!o[C])continue;let D=c[C]*k[C-A];_++,I+=c[C],P+=D;for(let O=0;O<l;O++)q[O]+=D*s[C][O]}for(let C=A;C<S;C++){let D=k[C-A],O=s[C],z=f[C];for(let B=0;B<l;B++)z[B]=-D*(O[B]*R-y[B])}if(_===0)continue;let F=n?_:1;for(let C=0;C<F;C++){let D=C/F,O=v[x]-D*P,z=N[x].map((Q,J)=>(Q-D*q[J])/O),B=I/F/O;for(let Q=A;Q<S;Q++){let J=k[Q-A],oe=s[Q],pe=f[Q];if(o[Q])for(let we=0;we<l;we++){let Ee=oe[we]-z[we];pe[we]+=Ee/F-(1-D)*J*Ee*B}else for(let we=0;we<l;we++)pe[we]-=J*(oe[we]-z[we])*B}R+=B;for(let Q=0;Q<l;Q++)y[Q]+=B*z[Q]}}u=h+1}return f}function Os(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 Cs(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 _i(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 yo=(e,t)=>({statistic:e,df:t,pValue:hn(e,t,{lowerTail:!1})});function $w(e,t,n){let r=e.length,o=()=>new Array(t).fill(NaN),s=yo(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)=>Ce(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 jw(e,t,n,r={}){return V(r,Cw,"coxph"),V(Nn(r,"control"),Fw,"coxph","control"),W("coxph",zw(e,t,n,r).result)}function zw(e,t,n,r={}){let{ties:o="efron",weights:s,strata:a,init:c,control:i={},nocenter:l=[-1,0,1],confLevel:f=.95,robust:m,cluster:u}=r,{eps:h=1e-9,iterMax:p=20,tolerChol:g=2**-39}=i,d=i.tolerInf??Math.sqrt(h);if(o!=="efron"&&o!=="breslow")throw new RangeError(`invalid 'ties': ${String(o)}`);if(!(h>0&&h<1/0))throw new RangeError("'eps' must be a positive finite number");if(!(g>0&&g<1/0))throw new RangeError("'tolerChol' must be a positive finite number");if(!(d>0&&d<1/0))throw new RangeError("'tolerInf' must be a positive finite number");if(!(Number.isInteger(p)&&p>=0))throw new RangeError("'iterMax' must be a non-negative integer");if(!(f>0&&f<1))throw new RangeError("'confLevel' must be between 0 and 1");let v=e.length;if(t.length!==v||n.length!==v)throw new RangeError("'time', 'status' and 'x' must have the same length");if(s!==void 0&&s.length!==v)throw new RangeError("'weights' must have the same length as 'time'");if(a!==void 0&&a.length!==v)throw new RangeError("'strata' must have the same length as 'time'");if(u!==void 0&&u.length!==v)throw new RangeError("'cluster' must have the same length as 'time'");if(m!==void 0&&typeof m!="boolean")throw new TypeError("'robust' must be true or false");let N=v===0?0:n[0].length;if(N<1)throw new RangeError("'x' must have at least one column");if(n.some($=>$.length!==N))throw new RangeError("the rows of 'x' must have the same length");if(c!==void 0&&c.length!==N)throw new RangeError("'init' must have one value per column of 'x'");if(c?.some($=>!Number.isFinite($)))throw new RangeError("'init' must contain finite values");let b=[];for(let $=0;$<v;$++){if(Number.isNaN(e[$])||Number.isNaN(t[$])||n[$].some(le=>Number.isNaN(le))||s!==void 0&&Number.isNaN(s[$])||a!==void 0&&Xm(a[$])||u!==void 0&&Xm(u[$]))continue;let ee=t[$];if(ee!==0&&ee!==1)throw new RangeError("'status' must be 0 (censored) or 1 (event)");if(e[$]===-1/0||e[$]===1/0&&ee===1)throw new RangeError("event times must be finite");b.push($)}let E=b.length;if(E===0)throw new RangeError("no observations without missing values");if(b.some($=>n[$].some(ee=>!Number.isFinite(ee))))throw new RangeError("'x' contains infinite values");let w=b.map($=>s===void 0?1:s[$]);if(w.some($=>!($>0)))throw new RangeError("Invalid weights, must be >0");if(w.some($=>$===1/0))throw new RangeError("'weights' must be finite");let M=new Ue,R=m??(u!==void 0||w.some($=>!Number.isInteger($)));u!==void 0&&!R&&M.add("cluster specified with robust=FALSE, cluster ignored");let y=0;for(let $ of b)y+=t[$];if(y===0)return{result:$w(b.map($=>n[$]),N,M.list)};let x=Ls(b.map($=>e[$])),T=new Array(E).fill(0);if(a!==void 0){let $=dt(a),ee=Array.from(new Set(b.map(fe=>$[fe]))).sort(Dw),le=new Map;ee.forEach((fe,ie)=>le.set(fe,ie)),T=b.map(fe=>le.get($[fe]))}let A=Array.from({length:E},($,ee)=>ee).sort(($,ee)=>T[$]-T[ee]||x[$]-x[ee]||$-ee),S=A.map($=>w[$]),{groups:k,k:_}=Be(S)>=Pw?se([S]):{groups:[S],k:0},I=k[0],P=0;for(let $ of w)P+=_===0?$:L($,-_);let q=A.map($=>n[b[$]].slice()),F=new Array(N).fill(0),C=new Array(N).fill(1),D=new Array(N).fill(0),O=new Set(l);for(let $=0;$<N;$++){if(q.every(ie=>O.has(ie[$])))continue;let ee=q.map(ie=>ie[$]),le=ie=>{let Re=0;for(let ot=0;ot<E;ot++)Re+=I[ot]*ie[ot];let He=Re/P,Ct=0,St=ie.map((ot,yt)=>(Ct+=I[yt]*Math.abs(ot-He),ot-He)),ut=Ct===0?1:P/Ct;return{m:He,s:ut,d:St,ok:Number.isFinite(He)&&ut>=2**-1022&&ut<1/0}},fe=le(ee);if(!fe.ok){let{groups:ie,k:Re}=se([ee]);fe=le(ie[0]),D[$]=Re}for(let ie=0;ie<E;ie++)q[ie][$]=fe.d[ie]*fe.s;F[$]=L(fe.m,D[$]),C[$]=fe.s}let z={time:A.map($=>x[$]),event:A.map($=>t[b[$]]===1),weight:I,x:q,stratumEnd:A.map(($,ee)=>ee===E-1||T[A[ee+1]]!==T[$]),logUnit:_*Math.LN2},B=o==="efron",Q=c===void 0?new Array(N).fill(0):c.map(($,ee)=>L($/C[ee],D[ee])),J=Ii(z,Q,B),oe=[J.loglik,J.loglik],pe=J.info.map($=>$.slice()),we=Os(pe,g),Ee=Cs(pe,J.score),G=0;for(let $=0;$<N;$++)G+=J.score[$]*Ee[$];let K=0,te,j=!1;if(p===0||!Number.isFinite(J.loglik))te=_i(pe);else{let $=J.loglik,ee=Q.map((ie,Re)=>ie+Ee[Re]),le=0,fe=!1;for(K=1;K<=p;K++){J=Ii(z,ee,B),pe=J.info.map(Re=>Re.slice()),we=Os(pe,g);let ie=Number.isFinite(J.loglik)&&J.score.every(Number.isFinite)&&J.info.every(Re=>Re.every(Number.isFinite));if(ie&&(J.loglik===$||Math.abs(1-$/J.loglik)<=h)){fe=!0,oe[1]=J.loglik,Q=ee;break}!ie||J.loglik<$?(le++,ee=ee.map((Re,He)=>(Re+le*Q[He])/(le+1))):(le=0,$=J.loglik,oe[1]=$,Ee=Cs(pe,J.score),Q=ee,ee=ee.map((Re,He)=>Re+Ee[He]))}fe?te=_i(pe):(K=p+1,j=!0,p>1?(J=Ii(z,Q,B),oe[1]=J.loglik,te=J.info.map((ie,Re)=>ie.map((He,Ct)=>Re===Ct?ie[Re]>0?1/ie[Re]:0:-0))):te=_i(pe))}let X=C.map($=>Math.floor(Math.log2($))),ne=C.map(($,ee)=>L($,-X[ee])),de=X.map(($,ee)=>$-D[ee]),je=Q.map(($,ee)=>$*ne[ee]),Oe=J.score.map(($,ee)=>$/ne[ee]),Xe=te.map(($,ee)=>$.map((le,fe)=>le*ne[ee]*ne[fe])),qe=je.map(($,ee)=>L($,de[ee])),gt=Xe.map(($,ee)=>$.map((le,fe)=>L(le,de[ee]+de[fe]-_))),nt=qe.slice();if(p>0&&Math.abs(we)<N)for(let $=0;$<N;$++)te[$][$]===0&&(nt[$]=NaN);let ge=0;for(let $=0;$<N;$++)ge+=qe[$]*F[$];let ze=b.map($=>{let ee=0;for(let le=0;le<N;le++)ee+=n[$][le]*qe[le];return ee-ge});if(p>1){let $=Xe.map((ee,le)=>{let fe=0;for(let ie=0;ie<N;ie++)fe+=Oe[ie]*Xe[ie][le];return Math.abs(fe)});if(j)M.add("Ran out of iterations and did not converge"),(Be(ze)>500||$.some(ee=>!Number.isFinite(ee)))&&M.add("one or more coefficients may be infinite");else{let ee=[];for(let le=0;le<N;le++){let fe=$[le];(!Number.isFinite(Oe[le])||L(fe,de[le])>h&&fe>d*Math.abs(je[le]))&&ee.push(le+1)}ee.length>0&&M.add(`Loglik converged before variable ${ee.join(",")} ; coefficient may be infinite. `)}}let Ie=nt.map(($,ee)=>Number.isNaN($)?-1:ee).filter($=>$>=0),xe=Ie.length,Fe,at=0,lt=0,qt;if(R&&xe>0){let $=A.map((Te,ue)=>ue),ee=E;if(u!==void 0){let Te=dt(u),ue=new Map;$=A.map(De=>{let Qe=Te[b[De]],Ze=ue.get(Qe);return Ze===void 0&&(Ze=ue.size,ue.set(Qe,Ze)),Ze}),ee=ue.size}if(ee<2)throw new RangeError("at least two clusters are needed for the robust variance");let le=Te=>Te.map(ue=>ue.map((De,Qe)=>De/ne[Qe])),fe=le(Zm(z,Q,B)),ie=fe.map(Te=>Xe.map((ue,De)=>{let Qe=0;for(let Ze=0;Ze<N;Ze++)Qe+=Te[Ze]*Xe[Ze][De];return Qe})),Re;if(_===0)Re=ie.map((Te,ue)=>Te.map(De=>I[ue]*De));else{let Te=De=>De===0||!Number.isFinite(De)?0:Math.floor(Math.log2(Math.abs(De))),ue=-1/0;ie.forEach((De,Qe)=>De.forEach(Ze=>Ze!==0&&Number.isFinite(Ze)&&(ue=Math.max(ue,Te(I[Qe])+Te(Ze))))),at=ue===-1/0?0:-ue,Re=ie.map((De,Qe)=>{let Ze=Te(I[Qe]);return De.map($n=>{let nc=Te($n);return L(L(I[Qe],-Ze)*L($n,-nc),Ze+nc+at)})})}let He=Te=>{let ue=Array.from({length:ee},()=>new Array(N).fill(0));return Te.forEach((De,Qe)=>{let Ze=ue[$[Qe]];for(let $n=0;$n<N;$n++)Ze[$n]+=De[$n]}),ue},Ct=Te=>{let ue=Array.from({length:N},()=>new Array(N).fill(0));for(let De of Te)for(let Qe=0;Qe<N;Qe++)for(let Ze=0;Ze<N;Ze++)ue[Qe][Ze]+=De[Qe]*De[Ze];return ue};Fe=Ct(He(Re));let St=se([S]).groups[0],ut=c===void 0?new Array(N).fill(0):c.map((Te,ue)=>L(Te/C[ue],D[ue])),ot=He(le(Zm(z,ut,B)).map((Te,ue)=>Te.map(De=>St[ue]*De))),yt=new Array(N).fill(0);for(let Te of ot)for(let ue=0;ue<N;ue++)yt[ue]+=Te[ue];let Qt=Ct(ot);if(N===1)lt=yt[0]*yt[0]/Qt[0][0];else{Os(Qt,g);let Te=Cs(Qt,yt);for(let ue=0;ue<N;ue++)lt+=yt[ue]*Te[ue]}let un=new Array(E),Zt=new Array(E);A.forEach((Te,ue)=>{un[Te]=fe[ue].map((De,Qe)=>L(De,-de[Qe])),Zt[Te]=Re[ue].map((De,Qe)=>L(De,de[Qe]-at))}),qt={scoreResiduals:un,dfbeta:Zt}}let Dt=Fe!==void 0,Pt=Fe??Xe,bt=Fe===void 0?-_:-2*at,ft=Ie.map($=>je[$]-(c===void 0?0:L(c[$],-de[$]))),It=0;if(xe===1)It=ft[0]*ft[0]/Pt[Ie[0]][Ie[0]];else if(xe>1){let $=Ie.map(le=>Ie.map(fe=>Pt[le][fe]));Os($,g);let ee=Cs($,ft);for(let le=0;le<xe;le++)It+=ft[le]*ee[le]}It=L(It,-bt);let Xt=(1-f)/2,Tt=Xt<1e-8?Z(Xt,{lowerTail:!1}):Z((1+f)/2),At=($,ee)=>{let le=Math.floor(ee/2),fe=$.map((ie,Re)=>Math.sqrt(L(ie[Re],ee-2*le)));return{unit:fe,h:le,se:fe.map((ie,Re)=>L(ie,de[Re]+le))}},vn=At(Xe,-_).se,{unit:Bt,h:ve,se:Ae}=At(Pt,bt),Se=nt.map(($,ee)=>L((Number.isNaN($)?$:je[ee])/Bt[ee],-ve)),U=Se.map($=>hn($*$,1,{lowerTail:!1})),me=nt.map(($,ee)=>Math.exp($-Tt*Ae[ee])),Le=nt.map(($,ee)=>Math.exp($+Tt*Ae[ee])),rt=L(-2*(oe[0]-oe[1]),_),vt=[L(oe[0],_),L(oe[1],_)],mt=L(G,_),Nt=Fe?.map(($,ee)=>$.map((le,fe)=>L(le,de[ee]+de[fe]-2*at)));return{result:{coefficients:nt,var:Nt??gt,loglik:vt,score:mt,wald:It,iter:K,means:F,n:E,nevent:y,linearPredictors:ze,se:vn,z:Se,p:U,stdError:vn,zValue:Se,pValue:U,lower:me,upper:Le,logtest:yo(rt,xe),sctest:yo(mt,xe),waldtest:yo(It,xe),rsq:{rsq:1-Math.exp(-rt/E),maxrsq:1-Math.exp(2*vt[0]/E)},usedRobust:Dt,...Dt?{naiveVar:gt,robustSe:Ae,rscore:lt,robscore:yo(lt,xe)}:{},warnings:M.list},...qt}}var Ww=["center"],Bw=2**-1022;function Kw(e,t,n={}){V(n,Ww,"leveneTest");let r=ae(n.center??"median",["median","mean"]),{groups:o}=qn(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"?ps(l):Ce(l);return l.map(m=>Math.abs(m-f))},a=se(o).groups.map(s),c=(l,f)=>l.some(m=>m>0&&m<Bw)||l.every(m=>m===0)&&o[f].some(m=>m!==o[f][0]);if(a.some(c)){let l=o.map(u=>{let h=se([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=>L(h,u.k-m)))}let i=sm(Yn(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 Uw=["score"],Hw=["alternative","exact","variant","levels"];function Gw(e,t,...n){V(mr(n),Uw,"propTrendTest");let{score:r}=fr(["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}=Li(f,m,h,u);if(!(d>=2**-1022&&d<1/0&&Math.abs(g)<1/0)){let N=se([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}=Li(f,m,h,u))}let v=wt(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}=se([o,s]),w=N.reduce((R,y)=>R+y,0)/b.reduce((R,y)=>R+y,0),M=Li(b,m,h,u);v=L(M.sxy/w*(M.sxy/M.sxx/(1-w)),E)}return{method:"Chi-squared Test for Trend in Proportions",statistic:v,statisticName:"X-squared",parameter:1,parameterName:"df",pValue:Ge(v,.5,2,!1,!1),warnings:[]}}function Li(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 Yw=2**24,Xw=2**32,Qw=`Sample size > 100 or data with ties
10
- p-value based on normal approximation. Specify nperm for permutation p-value`,Zw=`Sample data with ties
11
- p-value based on normal approximation. Specify nperm for permutation p-value.`;function Jw(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=dt(t));let c;if(n!==void 0)c=n.map(it);else{let f=[...new Set(a.map(m=>t[m]))];c=f.every(m=>typeof m=="number")?f.sort((m,u)=>m-u).map(it):[...new Set(f.map(it))].sort(Ht)}let i=c.map(()=>[]),l=new Map(c.map((f,m)=>[f,m]));for(let f of a){let m=l.get(it(t[f]));if(m===void 0)throw new RangeError(`the group '${it(t[f])}' is not among 'levels'`);i[m].push(o[f])}return i}function e1(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 t1(e,t,n={}){V(n,Hw,"jonckheereTest");let r=ae(n.alternative,["two.sided","increasing","decreasing"]),o=n.variant??"DescTools";if(o!=="DescTools"&&o!=="clinfun")throw new RangeError(`invalid variant "${String(o)}"`);if(o==="clinfun"&&n.exact!==void 0)throw new TypeError("clinfun's jonckheere.test has no option 'exact'");let s=Jw(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 M of s[E])l+=w<M?1:w===M?.5:0});let u=[],h=!i&&c<=100;o==="DescTools"&&n.exact!==void 0?(h=n.exact,h&&i&&(h=!1,u.push(Zw))):h||u.push(Qw);let p,g;if(h){let N=1,b=0;for(let[w,M]of f)w===0||M===0||(b+=N*(w*M+1),N+=w*M);if(N>Yw||b>Xw)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,M]of f){if(w===0||M===0)continue;let R=Array.from({length:w*M+1},(x,T)=>Nc(T,w,M,!1)),y=new Array(E.length+R.length-1).fill(0);E.forEach((x,T)=>R.forEach((A,S)=>y[T+S]+=x*A)),E=y}p=0,g=0,E.forEach((w,M)=>{M<=l&&(p+=w),M>=l&&(g+=w)})}else{let N=f.reduce((w,[M,R])=>w+M*R,0)/2,b=i?e1(s.map(w=>w.length),a):f.reduce((w,[M,R])=>w+M*R*(M+R+1)/12,0),E=(l-N)/Math.sqrt(b);p=Y(E),g=1-p,g<1e-8&&(g=Y(E,{lowerTail:!1}))}let v=r==="increasing"?g:r==="decreasing"?p:Math.min(1,2*Math.min(g,p,o==="clinfun"?.5:1));return W("jonckheereTest",{method:"Jonckheere-Terpstra test",alternative:r,statistic:l,statisticName:"JT",pValue:v,warnings:u})}var n1=["nClasses","adjust"];function Mo(e){return pt(Array.from(e)).sort((t,n)=>t-n)}function Jm(e){if(e.length>0&&!(Number.isFinite(e[0])&&Number.isFinite(e[e.length-1])))throw new RangeError("'x' must be finite")}function Fs(e){Jm(e);let t=dc(e);if(t.some(Number.isNaN))throw new RangeError("all 'x' values are identical");return t}function r1(e){let t=Mo(e),n=t.length;if(n<8)throw new RangeError("sample size must be greater than 7");let r=Fs(t),o=0;for(let i=1;i<=n;i++)o+=(2*i-1)*(Y(r[i-1],{logP:!0})+Y(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 o1(e){let t=Mo(e),n=t.length;if(n<8)throw new RangeError("sample size must be greater than 7");let r=Fs(t),o=1/(12*n);r.forEach((i,l)=>{o+=(Y(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")),W("cvmTest",{method:"Cramer-von Mises normality test",statistic:o,statisticName:"W",pValue:c,warnings:a})}function s1(e){let t=Mo(e),n=t.length;if(n<5)throw new RangeError("sample size must be greater than 4");let r=Fs(t).map(f=>Y(f)),o=-1/0,s=-1/0;r.forEach((f,m)=>{o=Math.max(o,(m+1)/n-f),s=Math.max(s,f-m/n)});let 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 a1(e){let t=Mo(e),n=t.length;if(n<5||n>5e3)throw new RangeError("sample size must be between 5 and 5000");if(Jm(t),t[0]===t[n-1])throw new RangeError("all 'x' values are identical");let r=t.map((m,u)=>Z((u+1-3/8)/(n+1/4))),o=se([t]).groups[0],s=Math.min(pc(o,r)**2/(_e(o)*_e(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=Y((Math.log(1-s)-i)/l,{lowerTail:!1});return{method:"Shapiro-Francia normality test",statistic:s,statisticName:"W",pValue:f,warnings:[]}}function i1(e,t={}){V(t,n1,"pearsonTest");let n=Mo(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 Fs(n)){let m=Math.min(o,Math.floor(1+o*Y(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:Ge(i,l/2,2,!1,!1),warnings:[]}}var c1=["weight"],l1=["exact"],f1=["model","type","unit","r0","confLevel"];function Eo(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 Ds(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 tu=e=>2*Y(-Math.abs(e));function m1(e,t={}){V(t,c1,"kappa2");let n=ae(t.weight,["unweighted","equal","squared"]);if(e.some(R=>R.length!==2))throw new TypeError("'ratings' must be a matrix with two columns");let r=e.filter(R=>!R.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(R=>dn(R[0])),a=r.map(R=>dn(R[1])),c=[...new Set([...s,...a])].sort((R,y)=>R<y?-1:R>y?1:0),i=c.length,l=new Map(c.map((R,y)=>[R,y])),f=Array.from({length:i},()=>new Array(i).fill(0));for(let R=0;R<o;R++)f[l.get(s[R])][l.get(a[R])]++;let m=R=>n==="unweighted"?R===0?1:0:n==="equal"?1-R/(i-1):1-R*R/((i-1)*(i-1)),u=Array.from({length:i},(R,y)=>Array.from({length:i},(x,T)=>m(Math.abs(y-T)))),h=f.map(R=>Ne(R)/o),p=Array.from({length:i},(R,y)=>Ne(f.map(x=>x[y]))/o),g=R=>{let y=[];for(let x=0;x<i;x++)for(let T=0;T<i;T++)y.push(R(x,T));return Ne(y)},d=g((R,y)=>f[R][y]*u[R][y])/o,v=g((R,y)=>h[R]*p[y]*u[R][y]),N=(d-v)/(1-v),b=Array.from({length:i},(R,y)=>Ne(p.map((x,T)=>x*u[y][T]))),E=Array.from({length:i},(R,y)=>Ne(h.map((x,T)=>x*u[T][y]))),w=(g((R,y)=>h[R]*p[y]*(u[R][y]-(b[R]+E[y]))**2)-v*v)/(o*(1-v)**2),M=N/Math.sqrt(w);return{value:N,statistic:M,pValue:tu(M),warnings:[]}}function u1(e,t={}){V(t,l1,"kappamFleiss");let n=t.exact??!1,r=Eo(e,"ratings");Ds(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=dt(a),i=[...new Set(c)].sort((w,M)=>w-M),l=new Map(i.map((w,M)=>[w,M])),f=new Map(a.map((w,M)=>[w,l.get(c[M])])),m=i.length,u=e.map(w=>{let M=new Array(m).fill(0);for(let R of w)M[f.get(R)]++;return M}),h=Ne(u.map(w=>(Ne(w.map(M=>M*M))-s)/(s*(s-1))))/o,p=Array.from({length:m},(w,M)=>Ne(u.map(R=>R[M]))/(o*s)),g=Ne(p.map(w=>w*w));if(n){let w=r.map(R=>{let y=new Array(m).fill(0);for(let x of R)y[f.get(x)]++;return y.map(x=>x/o)}),M=Array.from({length:m},(R,y)=>_e(w.map(x=>x[y]))*(s-1)/s);g-=Ne(M)/(s-1)}let d=(h-g)/(1-g);if(n)return{value:d,warnings:[]};let v=Ne(p.map(w=>w*(1-w))),N=Ne(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:tu(E),warnings:[]}}function h1(e,t={}){V(t,f1,"icc");let n=ae(t.model,["oneway","twoway"]),r=ae(t.type,["consistency","agreement"]),o=ae(t.unit,["single","average"]),s=t.r0??0,a=t.confLevel??.95;ct(a),Eo(e,"ratings"),Ds(e,"ratings");let c=se(e).groups,i=Eo(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=_e(c.map(D=>Ce(D)))*f,h=_e(i.map(D=>Ce(D)))*l,p=Ce(c.map(D=>_e(D))),g=_e(c.flat())*(l*f-1),d=g-u*(l-1)-h*(f-1),v=d>2**-20*g?d/((l-1)*(f-1)):w1(c)/((l-1)*(f-1)),N=(D,O,z)=>We(D,O,z,!1,!1),b=(D,O)=>m/2<1e-8?Kt(m/2,D,O,!1,!1):Kt(1-m/2,D,O,!0,!1),E=l-1;if(n==="oneway"||r==="consistency"){let D=n==="oneway"?p:v,O=n==="oneway"?l*(f-1):(l-1)*(f-1),z=u/D,B=z/b(E,O),Q=z*b(O,E),[J,oe,pe]=o==="single"?[(u-D)/(u+(f-1)*D),z*(1-s)/(1+(f-1)*s),we=>(we-1)/(we+f-1)]:[(u-D)/u,z*(1-s),we=>1-1/we];return{value:J,Fvalue:oe,fValue:oe,df1:E,df2:O,pValue:N(oe,E,O),lbound:pe(B),ubound:pe(Q),warnings:[]}}let w=o==="single"?f:1,M=D=>[w*D/(l*(1-D)),1+w*D*(l-1)/(l*(1-D))],R=(D,O)=>gc([D*h,O*v],[f-1,(l-1)*(f-1)]),y=o==="single"?(u-v)/(u+(f-1)*v+f/l*(h-v)):(u-v)/(u+(h-v)/l),[x,T]=M(s),A=u/(x*h+T*v),S=R(x,T),[k,_]=[f*y/(l*(1-y)),1+f*y*(l-1)/(l*(1-y))],I=R(k,_),P=b(E,I),q=b(I,E),[F,C]=o==="single"?[l*(u-P*v)/(P*(f*h+(f*l-f-l)*v)+l*u),l*(q*u-v)/(f*h+(f*l-f-l)*v+l*q*u)]:[l*(u-P*v)/(P*(h-v)+l*u),l*(q*u-v)/(h-v+l*q*u)];return{value:y,Fvalue:A,fValue:A,df1:E,df2:S,pValue:N(A,E,S),lbound:F,ubound:C,warnings:[]}}function eu(e,t){let n=t.length,r=Ne(t.flatMap(s=>t.map(a=>e[s][a])));return(1-Ne(t.map(s=>e[s][s]))/r)*n/(n-1)}function p1(e){let t=Eo(e,"x").length;if(Ds(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=Go(se(e).groups),r=Xr(e),o=Array.from({length:t},(c,i)=>i),s=Ne(r.flat()),a;try{let c=Mt(r);a=o.map(i=>1-1/c[i][i])}catch(c){if(!r.flat().every(Number.isFinite))throw c;let i=se(e).groups;a=o.map(l=>{let f=i.map(h=>h[l]),m=hr(et(i.map(h=>[1,...h.filter((p,g)=>g!==l)])),f),u=Ce(f);return 1-Ne(m.map(h=>h*h))/Ne(f.map(h=>(h-u)**2))})}return W("cronbachAlpha",{rawAlpha:eu(n,o),stdAlpha:(1-t/s)*t/(t-1),G6:1-(t-Ne(a))/s,averageR:(s-t)/(t*(t-1)),alphaDrop:o.map(c=>eu(n,o.filter(i=>i!==c))),warnings:d1(e,r)})}function d1(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}=Et(r.map(c=>r.map(i=>t[c][i])),{symmetric:!0}),s=o.map(c=>c[0]),a=Ne(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 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=vt(h),w=0;for(let x=0;x<a;x++)w+=p[x];let d=a+u,v=d*(d+1)/2-w,N=a*u+a*(a+1)/2-w,b=a*u+u*(u+1)/2-v,M=0;for(let x of hn(h))M+=x*x*x-x;let g=a*u/(d*(d-1))*((d*d*d-d)/12-M/12),y=(Math.min(N,b)-a*u/2)/Math.sqrt(g);return w<v?-y:y}),l=c.map(m=>s.n[m]/(s.n[m]+a)),f=lm(i,l,r,o);return Z("manyOneUTest",Ti(s,`Wilcoxon, Mann, Whittney U-test
7
+ for multiple comparisons with one control`,i,f,o))}function Y0(e,t){let n=Ln(e,t),r=_o(n.k,(s,a)=>{let c=n.n[s],i=n.n[a],l=[...n.groups[s],...n.groups[a]],f=vt(l),m=0;for(let v=0;v<c;v++)m+=f[v];let u=m-c*(c+1)/2,h=Math.min(u,c*i-u),p=c+i,w=0;for(let v of hn(l))w+=v*v*v-v;let d=c*i/(p*(p-1))*((p*p*p-p)/12-w/12);return Math.SQRT2*(h-c*i/2)/Math.sqrt(d)}),o=r.map(s=>s.map(a=>Number.isNaN(a)?NaN:Si(a,n.k,1/0)));return Z("dscfAllPairsTest",Bs(n,"Dwass-Steele-Critchlow-Fligner all-pairs test",r,o,"single-step"))}function X0(e,t){let n=Ln(e,t),r=Lt(n.groups),o=r.mean,s=r.variance.map((i,l)=>i/n.n[l]),a=_o(n.k,(i,l)=>(o[i]-o[l])/Math.sqrt(s[i]+s[l])*Math.SQRT2),c=_o(n.k,(i,l)=>{let f=rl([r.variance[i],r.variance[l]],[n.n[i],n.n[l]]);return Si(a[i-1][l],n.k,f)});return Z("gamesHowellTest",Bs(n,"Games-Howell test",a,c,"none"))}function Q0(e,t){let n=Ln(e,t),{m:r,s2:o,df:s}=ki(n);if(s<2)throw new RangeError("the studentized range needs at least 2 residual degrees of freedom");let a=_o(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:Si(l,n.k,s)));return Z("tukeyTest",Bs(n,"Tukey's test",a,c,"single-step"))}function Z0(e,t,n={}){H(n,W0,"dunnTest");let r=n.method??"none",o=n.altp??!1,s=Ln(e,t),a=Ws(e,t).statistic,{R:c,C:i}=Ai(s),l=s.N*(s.N+1)/12-i,f=[],m=[];for(let p=1;p<s.k;p++)for(let w=0;w<p;w++)f.push((c[w]-c[p])/Math.sqrt(l*(1/s.n[w]+1/s.n[p]))),m.push(`${s.labels[w]} - ${s.labels[p]}`);let u=f.map(p=>(o?2:1)*zs(Math.abs(p))),h=J0(u,r);return{chi2:a,Z:f,P:u,PAdjusted:h,zValue:f,pValue:u,pAdjusted:h,comparisons:m,warnings:[]}}function J0(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 eg(e,t,n={}){H(n,z0,"glhtDunnett");let r=Ze(n.alternative),o=Ln(e,t),{m:s,s2:a,df:c,k:i}=ki(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=>D(N,i)),w=u.map(N=>D(N,i)),d=f.map(N=>o.n[N]/(o.n[N]+o.n[0])),v=h.map(N=>r==="two.sided"?vr(N,d,c,!0):vr(r==="greater"?N:-N,d,c,!1));return{names:f.map(N=>`${o.labels[N]} - ${o.labels[0]}`),estimate:p,se:w,t:h,p:v,stdError:w,tValue:h,pValue:v,df:c,warnings:[]}}var fm=["varEqual"],tg=2**-1022;function um(e,t,n={}){if(typeof e!="string"&&xt(t)&&H(t,fm,"onewayTest"),H(n,fm,"onewayTest"),typeof e=="string"){let d=yl(e,t);return um(d.values,d.groups,n)}let r;t!==void 0&&!Array.isArray(t)?n=t:r=t;let o=wr(e,r),s=Lt(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,w;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,Lo(p,s.ssWithin),h=v/(i-1)/(N/p)}else if(m.some(d=>!(d>=tg))&&(w=rg(o))!==void 0){let d=w.map((E,R)=>mm(l[R]/E.variance,-2*E.k)),v=Math.max(...d.map(E=>E.e)),N=d.map(E=>Le(E.m,E.e-v)),b=N.reduce((E,R)=>E+R,0),M=0;for(let E=0;E<i;E++){let R=N.reduce((A,T,k)=>k===E?A:A+T,0);M+=(R/b)**2/(l[E]-1)}M/=i*i-1;let g=[];for(let E=0;E<i;E++)for(let R=E+1;R<i;R++){let[A,T]=w[E].k>=w[R].k?[w[E],w[R]]:[w[R],w[E]],k=A.mean-Le(T.mean,T.k-A.k);if(k===0)continue;let _=mm(k,A.k);g.push({m:d[E].m*d[R].m*_.m*_.m/b,e:d[E].e+d[R].e+2*_.e-v})}let y=Math.max(...g.map(E=>E.e)),x=g.reduce((E,R)=>E+Le(R.m,R.e-y),0);h=(g.length===0?0:Le(x,y))/((i-1)*(1+2*(i-2)*M)),p=1/(3*M)}else if(m.some(d=>d===0)){let d=m.flatMap((M,g)=>M===0?[g]:[]);if(d.length>1)throw new RangeError("Welch's test is not defined with two or more constant groups");let v=d[0],N=0,b=0;for(let M=0;M<i;M++)M!==v&&(N+=1/(l[M]-1),b+=l[M]/m[M]*(f[M]-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((g,y)=>g/m[y]),v=d.reduce((g,y)=>g+y,0),N=0,b=0;for(let g=0;g<i;g++)N+=(1-d[g]/v)**2/(l[g]-1),b+=d[g]*f[g];N/=i*i-1,b/=v;let M=0;for(let g=0;g<i;g++)M+=d[g]*(f[g]-b)**2;h=M/((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 ng(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=tm(t);return{method:"Shapiro-Wilk normality test",statistic:1-r,statisticName:"W",pValue:nm(r,n),warnings:[]}}function rg(e){let t=[];for(let n of e){let r=Lt([n]),o=r.variance[0];if(!(o>0))return;t.push({mean:r.mean[0],variance:o,k:r.k})}return t}function mm(e,t){let n=Math.floor(Math.log2(Math.abs(e))),r=Le(e,-n);return Math.abs(r)>=2?(r/=2,n++):Math.abs(r)<1&&(r*=2,n--),{m:r,e:t+n}}function qi(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 og([[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]=pn(f,o,s,a,!1);return{lo:c,d:l}}function sg(e,t){let n=new Float64Array(e.length+t.length-1);for(let r=0;r<e.length;r++){let o=e[r];if(o!==0)for(let s=0;s<t.length;s++)n[r+s]+=o*t[s]}return n}var Us=class{lo;hi;logNull;constructor(t){let n=0,r=new Float64Array([1]);for(let o of t){let s=og(o);n+=s.lo,r=sg(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=qi(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=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?qi(o,s,o.length):qi(o,0,s+1)}};function Ii(e,t){return zn(e,t)}function hm(e,t){if(t===e.lo)return 0;if(t===e.hi)return 1/0;let n=e.mean(1);return n===t?1:Ii(r=>e.mean(r)-t,n>t)}function _i(e,t,n){if(t===e.hi)return 1/0;let r=e.cdf(t,1,!1);return r===n?1:Ii(o=>n-e.cdf(t,o,!1),r<n)}function Li(e,t,n){if(t===e.lo)return 0;let r=e.cdf(t,1,!0);return r===n?1:Ii(o=>e.cdf(t,o,!0)-n,r>n)}var ag=["alternative","correct","exact","confLevel"];function ig(e,t={}){H(t,ag,"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(Ci);if(s.some(f=>f<2))throw new RangeError("sample size in each stratum must be > 1");let a=Ze(t.alternative),c=t.correct??!0,i=t.exact??!1,l=t.confLevel??.95;if(qt(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?fg(f,a,l):cg(f,s,a,c,l)}return mg(e,s,r,o)}function Ci(e){return e.reduce((t,n)=>t+n.reduce((r,o)=>r+o,0),0)}function gm(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 wm(e,t){return e.map(n=>n.map(r=>r.map(o=>D(o,-t))))}function pm(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]],w)=>{let d=t[w],v=m+u,N=h+p,b=m+h,M=u+p;r+=m;let g=v*b/d;o+=Number.isFinite(g)?g:v/d*b;let y=v*N*b*M/(d*d*(d-n));s+=Number.isFinite(y)?y:v/d*(N/d)*(b/(d-n))*M;let x=(m+p)/d,E=(u+h)/d,R=m*p/d,A=u*h/d,T=Number.isFinite(R)?R:m/d*p,k=Number.isFinite(A)?A:u/d*h;a+=T,c+=k,i+=x*T,l+=x*k+E*T,f+=E*k}),{sa:r,se:o,sv:s,sR:a,sS:c,sPR:i,sPSQR:l,sQS:f}}function cg(e,t,n,r,o){let s=pm(e,t,1),a=0;if(!t.every(Number.isFinite)||!Object.values(s).every(Number.isFinite)){a=gm(e);let x=wm(e,a);s=pm(x,x.map(Ci),D(1,-a))}let{sa:c,se:i,sv:l,sR:f,sS:m,sPR:u,sPSQR:h,sQS:p}=s,w=c-i,d=D(.5,-a),v=r&&Math.abs(w)>=d?d:0,N=D(Ot(Math.abs(w)-v,l),a),b=n==="two.sided"?bt(N,.5,2,!1,!1):ce(Math.sign(w)*Math.sqrt(N),{lowerTail:n==="less"}),M=f/m,g=D(Math.sqrt(u/(2*f*f)+h/(2*f*m)+p/(2*m*m)),-a/2),y;if(n==="less")y=[0,M*Math.exp(pe(o)*g)];else if(n==="greater")y=[M*Math.exp(pe(o,{lowerTail:!1})*g),1/0];else{let x=(1-o)/2,E=x<1e-8?pe(x,{lowerTail:!1}):pe((1+o)/2);y=[M*Math.exp(-E*g),M*Math.exp(E*g)]}return{method:`Mantel-Haenszel chi-squared test ${v!==0?"with":"without"} continuity correction`,alternative:n,statistic:N,statisticName:"Mantel-Haenszel X-squared",parameter:1,parameterName:"df",pValue:b,confInt:y,confLevel:o,estimate:M,estimateNames:"common odds ratio",nullValue:1,nullValueName:"common odds ratio",warnings:[]}}var lg=2**24;function fg(e,t,n){let r=e.reduce((i,[[l,f],[m,u]])=>i+Math.min(l,u)+Math.min(f,m),1);if(r>lg)throw new RangeError(`the exact distribution of S has ${r} values, more than 2^24: use exact: false`);let o=new Us(e),s=e.reduce((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=ue(f,[m,0]));a=Math.min(1,f[0])}else a=o.cdf(s,1,t==="greater");let c;if(t==="less")c=[0,_i(o,s,1-n)];else if(t==="greater")c=[Li(o,s,1-n),1/0];else{let i=(1-n)/2;c=[Li(o,s,i),_i(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:hm(o,s),estimateNames:"common odds ratio",nullValue:1,nullValueName:"common odds ratio",warnings:[]}}function mg(e,t,n,r){let{diff:o,V:s}=dm(e,t,n,r,1),a=0;if(!t.every(Number.isFinite)||!o.every(Number.isFinite)||!s.every(f=>f.every(Number.isFinite))){a=gm(e);let f=wm(e,a);({diff:o,V:s}=dm(f,f.map(Ci),n,r,D(1,-a)))}let c=o.length,i=ot(s,o),l=0;for(let f=0;f<c;f++)l+=o[f]*i[f];return l=D(l,a),{method:"Cochran-Mantel-Haenszel test",statistic:l,statisticName:"Cochran-Mantel-Haenszel M^2",parameter:c,parameterName:"df",pValue:bt(l,c/2,2,!1,!1),warnings:[]}}var ug=2**-1022;function dm(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((w,d)=>w+d,0)),u=Array.from({length:r},(p,w)=>i.reduce((d,v)=>d+v[w],0)),h=1/(f*f*(f-o));for(let p=0;p<r-1;p++)for(let w=0;w<n-1;w++){let d=w+(n-1)*p,v=m[w]*u[p]/f;a[d]+=i[w][p]-(Number.isFinite(v)?v:cs(m[w],u[p],f));for(let N=0;N<r-1;N++)for(let b=0;b<n-1;b++){let M=(w===b?f*m[w]:0)-m[w]*m[b],g=(p===N?f*u[p]:0)-u[p]*u[N],y=h*M*g;c[d][b+(n-1)*N]+=Number.isFinite(y)&&h>=ug?y:m[w]*((w===b?1:0)-m[b]/f)*(((p===N?1:0)-u[N]/f)*(u[p]/(f-o)))}}}),{diff:a,V:c}}var bm=new Map,hg=2**24,pg=2**32;function ym(e,t){let n=e+t,r=(e+1)*(Math.floor((n+1)**2/4)+1);if(2*r>hg||n*r>pg)throw new RangeError(`the exact distribution for m = ${e} and n = ${t} is too large to compute: use exact: false`)}function dg(e,t){let n=`${e},${t}`,r=bm.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],w=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 M=0;M<=a;M++){let g=v[M];g!==0&&(N>0&&(w[d+1][M+p]+=g*b),N<h&&(w[d][M]+=g*(1-b)))}}c=w}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 bm.set(n,m),m}function Oi(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}=dg(t,n),a=Qc(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 vm=new Map;function gg(e,t){let n=`${e},${t}`,r=vm.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 w=a[p];for(let d=0;d<=s;d++){let v=w[d];v!==0&&(p<e&&(h[p+1][d+u]+=v),e-p<o-m&&(h[p][d]+=v))}}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 vm.set(n,f),f}function Ks(e,t,n){let{min:r,counts:o}=gg(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 wg=["alternative","exact","confInt","confLevel"];function xm(e,t,n={}){if(H(n,wg,"ansariTest"),typeof e=="string"){let g=Wn(e,t);return xm(g.x,g.y,n)}let r=Array.from(e).filter(g=>!Number.isNaN(g)),o=Array.from(t).filter(g=>!Number.isNaN(g)),s=r.length,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=Ze(n.alternative),i=n.confInt??!1,l=n.confLevel??.95;i&&qt(l);let f=s+a,m=vt(r.concat(o)),u=m.map(g=>Math.min(g,f-g+1)),h=0;for(let g=0;g<s;g++)h+=u[g];let p=new Set(m).size<f,w=n.exact??(s<50&&a<50&&!p),d=n.exact??(s<50&&a<50),v=new ft,N,b;if(w&&!p){ym(s,a);let g=x=>Oi(x-1,s,a,!1),y=x=>Oi(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?g(h):y(h)))}else c==="less"?N=g(h):N=y(h);i&&(b=Ng(r,o,c,l,v))}else{let g=Rm(h,u,p,s,a);N=c==="two.sided"?2*Math.min(ce(g),ce(g,{lowerTail:!1})):c==="less"?ce(g,{lowerTail:!1}):ce(g),i&&(b=Mg(r,o,c,l,d?new ft:v)),d&&p&&(v.add("cannot compute exact p-value with ties"),i&&v.add("cannot compute exact confidence interval with ties"))}let M={method:"Ansari-Bradley test",alternative:c,statistic:h,statisticName:"AB",pValue:N,warnings:v.list};return b!==void 0&&(M.confInt=b.confInt,M.confLevel=l,M.estimate=b.estimate,M.estimateNames="ratio of scales"),M.nullValue=1,M.nullValueName="ratio of scales",Z("ansariTest",M)}function Rm(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 Am(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)<bg))?vg(e.map(f=>Nm(f,n)).concat(t.map(f=>Nm(f,1)))):yg(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 bg=2**-1022;function Nm(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]=Mm(Math.abs(e)),[s,a]=Mm(t),c=o/a,i=r-s;return c<1&&(c*=2,i-=1),[n,i,c]}function Mm(e){let t=Math.floor(Math.log2(e)),n=D(e,-t);return n>=2?(n/=2,t+=1):n<1&&(n*=2,t-=1),[t,n]}function Em(e,t){if(e[0]!==t[0])return e[0]-t[0];let n=e[1]!==t[1]?e[1]-t[1]:e[2]-t[2];return e[0]*n}function vg(e){let t=e.map((r,o)=>o).sort((r,o)=>Em(e[r],e[o])),n=new Array(e.length);for(let r=0;r<t.length;){let o=r+1;for(;o<t.length&&Em(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 yg(e){let t=e.filter(s=>!Number.isNaN(s));if(t.length===e.length)return vt(e);let n=vt(t),r=0,o=t.length;return e.map(s=>Number.isNaN(s)?++o:n[r++])}function Hs(e,t,n){return e==="two.sided"?n(t):e==="greater"?[n(2*t)[0],1/0]:[0,n(2*t)[1]]}function Ng(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:Hs(n,1-r,()=>[NaN,NaN]),estimate:NaN};let l=c.map((v,N)=>Am(e,t,v+(N<i-1?(c[N+1]-v)/2:v*1.01)).ab),f=(v,N,b)=>{let M=1/0;for(let y of l)(N?y-v>0:y-v>=0)&&y-v<M&&(M=y-v);let g=b?-1/0:1/0;return l.forEach((y,x)=>{y-v===M&&(g=b?Math.max(g,c[x]):Math.min(g,c[x]))}),g},m=v=>{let N=Ks(v/2,s,a),b=Ks(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 M=f(N,!1,!1),g=f(b,!0,!0);return M>g?[f(b,!1,!1),f(N,!0,!0)]:[M,g]},u=Hs(n,1-r,m),h=Ks(.5,s,a),p=-1/0,w=1/0;l.forEach((v,N)=>{v-h<=0?p=Math.max(p,c[N]):w=Math.min(w,c[N])});let d=p===-1/0||w===1/0?NaN:(w+p)/2;return d===1/0&&(d=w/2+p/2),{confInt:u,estimate:d}}function Mg(e,t,n,r,o){let s=e.length,a=t.length,c=(v,N)=>{let{ab:b,a:M,ties:g}=Am(e,t,v);return Rm(b,M,g,s,a)-N},i=[],l=(v,N)=>{v.length===0||N.length===0||i.push(en(v)/dt(N),dt(v)/en(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:Hs(n,1-r,()=>[NaN,NaN]),estimate:NaN};let f=en(i),m=dt(i),u=v=>Pt(N=>c(N,v),f,m,{tol:1e-4}),h=v=>{let N=pe(v/2),b=pe(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 M=u(N),g=u(b);return M<=g?[M,g]:[g,M]},p=Hs(n,1-r,h),w=c(f,0)>0||c(m,0)<0;w&&o.add("cannot compute estimate, returning NA");let d=w?NaN:u(0);return{confInt:p,estimate:d}}var Eg=["alternative","confLevel"];function xg(e,t,n={}){H(n,Eg,"brunnerMunzelTest");let r=Array.from(e).filter(E=>!Number.isNaN(E)),o=Array.from(t).filter(E=>!Number.isNaN(E)),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=Ze(n.alternative),i=n.confLevel??.95;tn(i);let l=vt(r.concat(o)),f=l.slice(0,s),m=l.slice(s),u=E=>E.reduce((R,A)=>R+A,0)/E.length,h=u(f),p=u(m),w=(E,R,A)=>{let T=(E.length+1)/2,k=0;for(let _=0;_<E.length;_++)k+=(E[_]-R[_]-A+T)**2;return k/(E.length-1)},d=w(f,vt(r),h),v=w(m,vt(o),p),N=(p-(a+1)/2)/s,b=s*d+a*v,M=s*a*(p-h)/(s+a)/Math.sqrt(b),g=b**2/((s*d)**2/(s-1)+(a*v)**2/(a-1)),y;c==="greater"?y=lt(M,g,!0,!1):c==="less"?y=lt(M,g,!1,!1):y=2*lt(-Math.abs(M),g,!0,!1);let x=wt((1-i)/2,g,!1,!1)*Math.sqrt(d/(s*a**2)+v/(a*s**2));return{method:"Brunner-Munzel Test",alternative:c,statistic:M,statisticName:"Brunner-Munzel Test Statistic",parameter:g,parameterName:"df",pValue:y,confInt:[N-x,N+x],confLevel:i,estimate:N,estimateNames:"P(X<Y)+.5*P(X=Y)",warnings:[]}}var Rg=[1.2337005501361697,7831619385924639e-32];function Ag(e,t,n){let r=(f,m)=>{let u=new Float64Array(t*t);for(let h=0;h<t;h++)for(let p=0;p<t;p++){let w=f[h*t+p];if(w!==0)for(let d=0;d<t;d++)u[h*t+d]+=w*m[p*t+d]}return u},o=f=>{let m=0;for(let p of f)m=Math.max(m,Math.abs(p));if(m===0||!Number.isFinite(m))return 0;let u=Math.floor(Math.log2(m));if(Math.abs(u)<500)return 0;let h=2**-u;for(let p=0;p<f.length;p++)f[p]*=h;return u},s,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 Tg=2**33;function Sm(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>Tg)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 km(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]=bn(e,t),u=2*f-1+2*m,h=1,p=0;for(let w=1;w<=e;w++)h*=w/e*u,h<2**-500&&(h*=2**500,p-=500);for(;p<-1e3;)h*=2**-1e3,p+=1e3;return h*2**p}let n=Math.floor(e*t)+1,r=2*n-1,o=n-e*t,s=new Float64Array(r*r);for(let f=0;f<r;f++)for(let m=0;m<r;m++)s[f*r+m]=f-m+1>=0?1:0;for(let f=0;f<r;f++)s[f*r]-=Math.pow(o,f+1),s[(r-1)*r+f]-=Math.pow(o,r-f);2*o-1>0&&(s[(r-1)*r]+=Math.pow(2*o-1,r));for(let f=0;f<r;f++)for(let m=0;m<r;m++){let u=f-m+1;if(u>0){let h=1;for(let p=2;p<=u;p++)h*=p;s[f*r+m]/=h}}let[a,c]=Ag(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 Fi(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=it(Rg,bn(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 Di(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]=bn(e,t),o=Math.floor(e-n-r),s=0;for(let a=0;a<=o;a++){let c=ue(zt(e-a,-n),[-r,0]);if(c[0]<=0)break;let i=ue(zt(a,n),[r,0]),l=it(i,[e,0]),f=it(c,[e,0]);s+=Xc(a,e-a,l,f,!1)/(l[0]+l[1])}return Math.min(t*s,1)}var Tm=2**28;function qm(e,t){let[n,r]=bn(e,t),o=Math.floor(n);o===n&&r<0&&(o-=1);let s=[];for(let h=0;h<=e;h++){let p=(h-n-r)/e;p>0&&p<1&&s.push({t:p,up:!0,i:h});let w=(h+n+r)/e;w>0&&w<1&&s.push({t:w,up:!1,i:h})}if(s.length*(2*o+1)*Math.min(e+1,100)>Tm)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 w=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)),M=a-d,g=sn(b,N,p,!1);for(let y=b;y<=N&&g!==0;y++)y>b&&(g=g*(N-y+1)*w/y),y>=M?m+=v*g:i[d+y]+=v*g,u++;g=b>0?sn(b-1,N,p,!1):0;for(let y=b-1;y>=0&&g!==0;y--)y<b-1&&(g=g*(y+1)/((N-y)*w)),y>=M?m+=v*g:i[d+y]+=v*g,u++}if(u>2*Tm)return NaN;[c,i]=[i,c]}if(f=h.t,h.up)a=h.i+1;else{for(let w=l;w<=h.i;w++)m+=c[w],c[w]=0;l=Math.max(l,h.i+1)}}return Math.min(m,1)}function Im(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,w=(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)?(w>0&&(m+=w*pn(u,t,n,p,!1)),f[h]=0):f[h]=w}[l,f]=[f,l],f.fill(0)}return Math.min(m,1)}var Sg=["alternative","exact"];function _m(e,t){return e==="two.sided"?"two-sided":`the CDF of x lies ${e==="less"?"below":"above"} ${t?"the null hypothesis":"that of y"}`}function Lm(e,t,n={}){if(H(n,Sg,"ksTest"),typeof e=="string"){let N=Wn(e,t);return Lm(N.x,N.y,n)}let r=Array.from(e).filter(N=>!Number.isNaN(N)).sort((N,b)=>N-b),o=r.length;if(o<1)throw new RangeError("not enough 'x' data");let s=Ze(n.alternative),a=s==="two.sided"?"D":s==="greater"?"D^+":"D^-";if(typeof t=="function"){let N=-1/0,b=-1/0;for(let E=0;E<o;E++){let R=t(r[E]);if(!(R>=0&&R<=1))throw new RangeError(`the distribution function returned ${R} at ${r[E]}, not a value in [0, 1]`);N=Math.max(N,(E+1)/o-R),b=Math.max(b,R-E/o)}let M=s==="two.sided"?Math.max(N,b):s==="greater"?N:b,g=new Set(r).size<o,y=n.exact??(o<100&&!g),x;if(y&&s==="two.sided"&&M<.5&&Sm(o,M),s==="two.sided")if(!y)x=Fi(Math.sqrt(o)*M,!1);else{let E=2*Di(o,M);if(M>=.5)x=Math.min(1,E);else{let R=E<.01?qm(o,M):NaN;x=Number.isNaN(R)?1-km(o,M):R}}else x=y?Di(o,M):Math.exp(-2*o*M*M);return Z("ksTest",{method:`${y?"Exact":"Asymptotic"} one-sample Kolmogorov-Smirnov test`,alternative:_m(s,!0),exact:y,statistic:M,statisticName:a,pValue:Math.min(1,Math.max(0,x)),warnings:g?["ties should not be present for the one-sample Kolmogorov-Smirnov test"]:[]})}let c=Array.from(t).filter(N=>!Number.isNaN(N)),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,w=new Set(l.map(N=>N[0])).size<l.length,d=n.exact??o*i<1e4,v;if(d)v=Im(p,o,i,w?{alternative:s,z:l.map(N=>N[0])}:{alternative:s});else{let N=o*i/(o+i);v=s==="two.sided"?Fi(Math.sqrt(N)*p,!1):Math.exp(-2*N*p*p)}return Z("ksTest",{method:`${d?"Exact":"Asymptotic"} two-sample Kolmogorov-Smirnov test`,alternative:_m(s,!1),exact:d,statistic:p,statisticName:a,pValue:Math.min(1,Math.max(0,v)),warnings:w&&!d?["p-value will be approximate in the presence of ties"]:[]})}var kg=["poolSd","paired","pAdjustMethod","alternative"],qg=["mu","paired","exact","correct","confLevel","tolRoot","pAdjustMethod","alternative"],Ig=["pAdjustMethod","correct"];function Cm(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=un(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(gr);return{levels:a,groups:a.map(c=>o.get(c))}}function Vi(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=jr(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 Pi(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 _g(e,t,n={}){H(n,kg,"pairwiseTTest");let r=n.paired??!1,o=n.poolSd??!r;if(r&&o)throw new RangeError("pooling of SD is incompatible with paired tests");let s=Ze(n.alternative),a=n.pAdjustMethod??"holm",{levels:c,groups:i}=Cm(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=Lt(i),p=h.mean,w=0;for(let v of u)v>1&&(w+=v-1);Lo(w,h.ssWithin);let d=Math.sqrt(h.ssWithin/w);f=(v,N)=>{let b=(p[v]-p[N])/(d*Math.sqrt(1/u[v]+1/u[N]));return s==="less"?lt(b,w,!0,!1):s==="greater"?lt(b,w,!1,!1):2*lt(-Math.abs(b),w,!0,!1)}}else m=r?"paired t tests":"t tests with non-pooled SD",f=(u,h)=>tt(()=>gn(i[u],i[h],{paired:r,alternative:s})).pValue;return Pi(m,c,Vi(l,f,a),a)}function Lg(e,t,n={}){H(n,qg,"pairwiseWilcoxTest");let{pAdjustMethod:r,alternative:o,...s}=n,a=r??"holm",{levels:c,groups:i}=Cm(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 ft,u=Vi(l,(h,p)=>{let w=tt(()=>To(i[h],i[p],{...s,...o===void 0?{}:{alternative:o}}));f=w.method;for(let d of w.warnings)m.add(d);return w.pValue},a);return Z("pairwiseWilcoxTest",Pi(f,c,u,a,m.list))}function Cg(e,t,n={}){if(H(n,Ig,"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 ft,l=Vi(s,(m,u)=>{let h=tt(()=>ri([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 Z("pairwisePropTest",Pi("Pairwise comparison of proportions",f,l,a,i.list))}var Co=[0,0];function Gs(e){if(!(e[0]>0))return[Math.sqrt(e[0]),0];let t=Math.sqrt(e[0]),n=Pe(e,bn(t,t));return zt(t,n[0]/(2*t))}function Ys(e,t){return Array.from({length:t},(n,r)=>e===void 0?[1,0]:Gs([e[r],0]))}function Wr(e,t){return Cn(e,t,[]).fit}function Cn(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=Co;for(let v=f;v<r;v++)u=ue(u,_e(m[v],m[v]));if(u[0]===0){i.push(Co);continue}let h=Gs(u),p=m[f][0]<0?[-h[0],-h[1]]:h;m[f]=ue(m[f],p);let w=_e(p,m[f]),d=v=>{let N=Co;for(let M=f;M<r;M++)N=ue(N,_e(m[M],v[M]));let b=it(N,w);for(let M=f;M<r;M++)v[M]=Pe(v[M],_e(b,m[M]))};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(Co);for(let f=o-1;f>=0;f--){let m=a[f];for(let u=f+1;u<o;u++)m=Pe(m,_e(s[u][f],l[u]));l[f]=it(m,i[f])}return{fit:{qty:a,coef:l},qtv:c}}function zr(e,t=0,n=e.length){let r=Co;for(let o=t;o<n;o++)r=ue(r,_e(e[o],e[o]));return r}var Og=1e-14;function Fg(e,t,n,r,o,s,a,c){let i=y=>D(y,2*a),l=t.length,f=n.flatMap((y,x)=>zr(y)[0]>Og*s[x]?[x]:[]),m=0,u=[],h=t.slice(),p=new Array(l).fill(0);if(f.length>0){let y=Rt(Array.from({length:l},(E,R)=>f.map(A=>n[A][R][0])));m=y.rank,u=y.pivot.slice(0,m).map(E=>f[E]);let x=Wr(u.map(E=>n[E]),t);h=x.qty,u.forEach((E,R)=>{for(let A=0;A<l;A++)p[A]=p[A]+n[E][A][0]*x.coef[R][0]})}let w=[];u.forEach((y,x)=>{let E=r[y],R=w.find(A=>A.term===E);R===void 0&&w.push(R={term:E,df:0,ss:[0,0]}),R.df++,R.ss=ue(R.ss,_e(h[x],h[x]))});let d=l-m,v=zr(h,m)[0],N=pi(p,v,d),b=w.filter(y=>y.term!==0);d>0&&pr(c,v)&&(v=0,b=b.map(y=>pr(c,y.ss[0])?{...y,ss:[0,0]}:y));let M=b.map(y=>o[y.term-1]),g=y=>{if(d>0){let x=v/d,E=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 E.push([d,y(v),y(x),NaN,NaN]),{stratum:`Error: ${e}`,colNames:["Df","Sum Sq","Mean Sq","F value","Pr(>F)"],rowNames:[...M,"Residuals"],table:E}}return{stratum:`Error: ${e}`,colNames:["Df","Sum Sq","Mean Sq"],rowNames:M,table:b.map(({df:x,ss:E})=>[x,y(E[0]),y(E[0]/x)])}};return[g(i),g(y=>y),N]}function Om(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=ps({response:t.response,rhs:a},Object.keys(o)).variables,i=fr(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(q=>!Number.isFinite(q)))throw new RangeError("NA/NaN/Inf in 'y'");let f=fr({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(q=>!i.warnings.includes(q))],h=f.colNames.length,p=f.colNames.map((q,F)=>Te([f.x.map(C=>C[F])]).groups[0]),w=f.x.map((q,F)=>p.map(C=>C[F])),d=h>0?Rt(w):void 0,v=d?.rank??0;v<h&&u.push("Error() model is singular");let N=d?.pivot.slice(0,v)??[],b=q=>q.map(F=>[F,0]),M=i.colNames.map((q,F)=>b(Te([i.x.map(C=>C[F])]).groups[0])),{groups:g,k:y}=Te([l]),{fit:x,qtv:E}=Cn(N.map(q=>b(p[q])),b(g[0]),M),R=g[0].map(q=>[1,q]),A=M.map(q=>zr(q)[0]),T=[];N.forEach((q,F)=>{let C=f.assign[q],O=C===0?"(Intercept)":f.terms.termLabels[C-1],I=T.find(V=>V.name===O);I===void 0&&T.push(I={name:O,idx:[]}),I.idx.push(F)}),m>v&&T.push({name:"Within",idx:Array.from({length:m-v},(q,F)=>v+F)});let k=T.map(q=>Fg(q.name,q.idx.map(F=>x.qty[F]),E.map(F=>q.idx.map(C=>F[C])),i.assign,i.terms.termLabels,A,y,R)),_=k.map(q=>q[0]),L={formula:e,multistratum:!0,strata:_,rows:i.frame.rows,warnings:u};return jt.set(L,k.map(q=>q[1])),k.some((q,F)=>q[2]&&T[F].name!=="(Intercept)")&&di.set(L,[hi]),L}function yr(e){let t=e.length,n=new Array(t).fill(0);for(let o=0;o<t;o++){let s=0;for(let a=0;a<=o;a++)s=Math.max(s,Math.abs(e[a][o]));s>0&&Number.isFinite(s)&&(s<2**-400||s>2**400)&&(n[o]=Math.floor(Math.log2(s)))}let r=n.every(o=>o===0)?e.map(o=>o.slice()):e.map(o=>o.map((s,a)=>D(s,-n[a])));return{inv:Sl(r),e:n}}var Fm=new WeakMap;function Br(e,t){return t!==void 0&&Fm.set(e,t),e}function ji(e,t){return Fm.get(e)??(t.length===0?0:t[t.length-1]+1)}function Dm(e,t){for(let n of t)if(Object.hasOwn(e,n))return e[n].length;return Object.values(e)[0]?.length}var Dg=["weights","contrasts","levels","intercept"],Vg=["level"],Pg=["interval","level"],$i=2**-1022;function Wi(e,t,n){let r=t.y;if(r===void 0)throw new TypeError("the formula has no response");let o=$m(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=>D(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 Oo(e,t,n={}){if(H(n,Dg,"lm"),typeof e=="string"){let r=fr(_r(e),t,{...n,dropUnusedLevels:!0});return Z("lm",Br(Wi(e,r,!1),Dm(t,r.terms.variables)))}return jg(e,t,n)}function jg(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=$m(l,f,i,r).result;return m.rows=c.flatMap((u,h)=>u?[h]:[]),Br(m,s.length)}function $g(e,t,n,r,o,s,a){let c=s.rank,i=s.pivot.slice(0,c),l=n?.map(g=>D(g,-r)),f=Ys(l===void 0?void 0:o.map(g=>l[g]),o.length).map((g,y)=>{if(n===void 0||l===void 0)return g;let x=n[o[y]];if(x===0||l[o[y]]>=$i)return g;let E=Gs([x,0]);return[D(E[0],-r/2),D(E[1],-r/2)]}),m=i.map(g=>{let y=o.map((E,R)=>Tt(f[R],e[E][g])),x=Al(y.map(E=>E[0]));return{c:x===0?y:y.map(E=>[D(E[0],-x),D(E[1],-x)]),e:x}}),{qty:u,coef:h}=Wr(m.map(({c:g})=>g),o.map((g,y)=>Tt(f[y],t[g]))),p=h.map((g,y)=>m[y].e===0?g:[D(g[0],-m[y].e),D(g[1],-m[y].e)]),w=e[0]?.map(()=>NaN)??[];i.forEach((g,y)=>w[g]=p[y][0]);let d=e.map(g=>{let y=[0,0];return i.forEach((x,E)=>y=ue(y,Tt(p[E],g[x]))),y}),v=zr(u,c)[0],N=o.map(g=>l?.[g]??1),b=[0,0];if(a){let g=[0,0],y=[0,0];o.forEach((x,E)=>{g=ue(g,[N[E],0]),y=ue(y,Tt(d[x],N[E]))}),b=it(y,g)}let M=[0,0];return o.forEach((g,y)=>{let x=Pe(d[g],b);M=ue(M,Tt(_e(x,x),N[y]))}),{coefficients:w,fittedValues:d.map(g=>g[0]),residuals:d.map((g,y)=>Pe([t[y],0],g)[0]),effects:u.map(g=>g[0]),rss:v,mss:M[0]}}function $m(e,t,n,r,o=!1){let s=Te([t.filter((l,f)=>n===void 0||n[f]!==0)]).k;if(s===0)return Vm(Pm(e,t,n,r,o),e,t,n);let a=Pm(e,t.map(l=>D(l,-s)),n,r,o),c=a.result,i=l=>D(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),Vm({...a,effects:a.effects.map(i),rss:D(a.rss,2*s),yScale:s,rssScaled:a.rss},e,t,n)}function Vm(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]=ho(f,0)?cr(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 zi=new WeakMap;function Bi(e){return zi.get(e)??0}function Wg(e){return e.qr.every(t=>t.every(Number.isFinite))&&e.qraux.every(Number.isFinite)}function Pm(e,t,n,r,o){if(t.length===0)throw new RangeError("0 (non-NA) cases");let s=t.map((j,K)=>n===void 0||n[K]!==0),a=e.filter((j,K)=>s[K]),c=t.filter((j,K)=>s[K]),i=n?.filter((j,K)=>s[K]),l=c.length;if(l===0)throw new RangeError("all weights are zero");if(a.some(j=>j.some(K=>!Number.isFinite(K))))throw new RangeError("NA/NaN/Inf in 'x'");if(c.some(j=>!Number.isFinite(j)))throw new RangeError("NA/NaN/Inf in 'y'");if(i?.some(j=>!Number.isFinite(j)))throw new RangeError("non-finite weights not allowed");let f=t.flatMap((j,K)=>s[K]?[K]:[]),m=i===void 0?1:dt(i),u=Number.isFinite(m)?2*Math.floor(Math.floor(Math.log2(m))/2):0,h=0,p=i?.map(Math.sqrt),w=p===void 0?a:a.map((j,K)=>j.map(W=>W*p[K])),d=Rt(w);i!==void 0&&Number.isFinite(m)&&u+2>0&&!Wg(d)&&(h=u+2,p=i.map(j=>D(Math.sqrt(j),-h/2)),w=a.map((j,K)=>j.map(W=>W*p[K])),d=Rt(w),zi.set(d,h));let v=p===void 0?c:c.map((j,K)=>j*p[K]),N=d.rank,b=j=>D(j,-u),M=o?$g(e,t,n,u,f,d,r):void 0,g=M?.coefficients??Or(d,v),y=M?.residuals??[],x=M?.fittedValues??[],E=l-N,R=i?.map(b)??new Array(l).fill(1),A=M?.rss??0;if(M===void 0){let j=Fr(d,v),K=p===void 0?j:j.map((U,z)=>U/p[z]),W=0;t.forEach((U,z)=>{if(s[z])y.push(K[W]),x.push(U-K[W]),W++;else{let ne=e[z].reduce((le,Re,Ce)=>Number.isNaN(g[Ce])?le:le+Re*g[Ce],0);x.push(ne),y.push(U-ne)}});for(let U=0;U<l;U++)A+=R[U]*K[U]**2}let T=x.filter((j,K)=>s[K]),k=0;if(i!==void 0&&i.some(j=>j!==0&&D(j,-u)<$i)){let j=f.flatMap((K,W)=>{let U=y[K];return U===0?[]:[{w:i[W],r:U,a:Math.floor(Math.log2(Math.abs(U)))}]});if(j.length>0&&j.every(K=>Number.isFinite(K.r))){let K=Math.max(...j.map(z=>Math.log2(z.w)+2*z.a)),W=2*Math.floor(Math.floor(K)/2),U=0;for(let z of j)U+=D(z.w,2*z.a-W)*D(z.r,-z.a)**2;A=U,k=W-u}}let _=A/E,L=D(Math.sqrt(_),(u+k)/2),q=h===0?0:(h-u)/2,F=d.qr.slice(0,N).map(j=>j.slice(0,N).map(K=>h===0?D(K,-u/2):K)),{inv:C,e:O}=N>0?yr(F):{inv:[],e:[]},V=d.pivot.slice(0,N).map((j,K)=>[j,K]).sort((j,K)=>j[0]-K[0]),S=V.map(([j])=>g[j]),P=V.map(([,j])=>D(Math.sqrt(C[j][j]*_),k/2-O[j]-q)),G=S.map((j,K)=>j/P[K]),Q=G.map(j=>2*lt(-Math.abs(j),E,!0,!1)),te={coefficients:g,residuals:y,fittedValues:x,rank:N,dfResidual:E,coefTable:{estimate:S,stdError:P,tValue:G,pValue:Q},sigma:L,rSquared:0,adjRSquared:0,intercept:r,qr:d,x:e,rows:e.map((j,K)=>K)};n!==void 0&&(te.weights=n);let $=r?1:0,J=N-$,ee={result:te,fitRows:f,effects:(M?.effects??[]).map(j=>D(j,u/2)),rss:D(A,u+k)};if(J===0)return ee;let Se=0;if(M!==void 0)Se=M.mss;else if(r){let j=R.reduce((W,U)=>W+U,0),K=0;for(let W=0;W<l;W++)K+=R[W]*T[W]/j;for(let W=0;W<l;W++)Se+=R[W]*(T[W]-K)**2}else for(let j=0;j<l;j++)Se+=R[j]*T[j]**2;te.rSquared=Se/(Se+D(A,k)),te.adjRSquared=1-(1-te.rSquared)*((l-$)/E);let re=D(Se/J/_,-k);return te.fStatistic={value:re,numdf:J,dendf:E},te.fPValue=pt(re,J,E,!1,!1),ee}function Wm(e,t){let n=(1-e)/2;return n<1e-8?wt(n,t,!1,!1):wt((1+e)/2,t,!0,!1)}function zg(e,t={}){H(t,Vg,"confint");let n=t.level??.95;if(!(n>0&&n<1))throw new RangeError("'level' must be between 0 and 1");let r=Wm(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 jm(e,t,n){if(t===0||!Number.isFinite(t))return e*t;let r=Math.floor(Math.log2(e)),o=Math.floor(Math.log2(Math.abs(t)));return D(D(e,-r)*D(t,-o),r+o+n)}function Bg(e,t,n={}){H(n,Pg,"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=-(zi.get(e.qr)??0)/2,l=e.coefficients.flatMap((b,M)=>Number.isNaN(b)?[]:[M]),f=o.map(b=>{let M=b.reduce((g,y,x)=>Number.isNaN(e.coefficients[x])?g:g+y*e.coefficients[x],0);return ho(M,0)?cr(l.map(g=>b[g]),l.map(g=>e.coefficients[g]),M):M}),m=b=>{let M=new Array(s).fill(0);for(let g=0;g<s;g++){let y=b[g];for(let x=0;x<g;x++)y-=c[x][g]*M[x];M[g]=y/c[g][g]}return M},u=o.map(b=>{let M=a.map(x=>b[x]),g=Ta(m(M));if(g>=$i&&g<1/0||!M.every(Number.isFinite)||M.every(x=>x===0))return i===0||!(g>0&&g<1/0)?g*e.sigma:jm(g,e.sigma,i);let y=Math.floor(Math.log2(dt(M.map(Math.abs))));for(let x of[0,600]){let E=Ta(m(M.map(R=>D(R,-(y+x)))));if(E<1/0)return E===0?E*e.sigma:jm(E,e.sigma,y+x+i)}return g*e.sigma}),h=n.interval??"none";if(h!=="none"&&h!=="confidence"&&h!=="prediction")throw new RangeError(`invalid 'interval': ${String(h)}`);let p=bs(e.qr,e.rank,o),w=p.length>0?{nonEstimable:p,warnings:[ws]}:{};if(h==="none")return Z("predict",{fit:f,seFit:u,...w},e);let d=n.level??.95;if(!(d>0&&d<1))throw new RangeError("'level' must be between 0 and 1");let v=Wm(d,e.dfResidual),N=u.map(b=>v*(h==="prediction"?Math.hypot(b,e.sigma):b));return Z("predict",{fit:f,seFit:u,lwr:f.map((b,M)=>b-N[M]),upr:f.map((b,M)=>b+N[M]),...w},e)}var Ug=["weights","contrasts","levels"],zm=["confLevel"],Bm=["sigmaX","sigmaY","mu","alternative","confLevel"];function Kg(e,t,n={}){return H(n,Ug,"aov"),typeof e=="string"?Z("aov",Um(e,t,n)):Z("aov",Hg(e,t))}function Um(e,t,n){let r=_r(e,!0),{treatment:o,errors:s}=wl(r.rhs);if(s.length>1)throw new Error(`there are ${s.length} Error terms: only 1 is allowed`);return s.length===1?Om(e,r,o,s[0],t,n):Wi(e,fr(r,t,{...n,dropUnusedLevels:!0},o),!0)}function Hg(e,t){let{labels:n,groups:r}=Kn(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 Um("y ~ g",{y:o,g:s},{levels:{g:n}})}function Km(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}=Lt(e),a=t-1,c=n-t,i=r/a,l=o/c,f=i/l,m=u=>D(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 Gg(e,t,n={}){xt(t)&&H(t,zm,"TukeyHSD"),H(n,zm,"TukeyHSD");let r;t!==void 0&&!Array.isArray(t)?n=t:r=t;let{labels:o,groups:s}=Kn(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;tn(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}=Lt(s),h=f/l,p=ll(c,a,l),w=[],d={names:[],i:[],j:[],diff:[],lwr:[],upr:[],pAdj:w,pAdjusted:w,warnings:[]};for(let v=0;v<a-1;v++)for(let N=v+1;N<a;N++){let b=m[N]-m[v],M=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(D(b,u)),d.lwr.push(D(b-p*M,u)),d.upr.push(D(b+p*M,u)),d.pAdj.push(fs(Math.abs(b)/M,a,l,{lowerTail:!1}))}return d}function Yg(e,t,n={}){xt(t)&&H(t,Bm,"zTest"),H(n,Bm,"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=Ze(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=Te([l,f??[],[o,s??0]]),u=m.k,[h,p,[w,d]]=m.groups,v=Pn(h)[0],N=f===void 0?NaN:Pn(p)[0],b=f===void 0?w/Math.sqrt(l.length):Math.sqrt(w**2/l.length+d**2/f.length),M=f===void 0?v:v-N,g=(M-D(a,-u))/b,y,x;if(c==="less")y=ce(g),x=[-1/0,M+pe(i)*b];else if(c==="greater")y=ce(g,{lowerTail:!1}),x=[M-pe(i)*b,1/0];else{y=2*ce(-Math.abs(g));let A=(1-i)/2,T=(A<1e-8?pe(A,{lowerTail:!1}):pe(1-(1-i)/2))*b;x=[M-T,M+T]}x=[D(x[0],u),D(x[1],u)];let E=f!==void 0,R=(A,T)=>{if(!(u>0&&A.some((_,L)=>_!==0&&Math.abs(T[L])<2**-1022)))return D(Pn(T)[0],u);let k=Math.max(Te([A]).k,0);return D(Pn(k===0?A:A.map(_=>D(_,-k)))[0],k)};return{method:E?"Two-sample z-Test":"One-sample z-Test",alternative:c,statistic:g,statisticName:"z",pValue:y,confInt:x,confLevel:i,estimate:E?[R(l,h),R(f,p)]:R(l,h),estimateNames:E?["mean of x","mean of y"]:"mean of x",nullValue:a,nullValueName:E?"difference in means":"mean",warnings:[]}}var Xg=["family","link","weights","offset","start","epsilon","maxit","intercept"],Qg=["family","link","variance"],Hm=["power"],Zg=["dispersion"],Jg=["type"],ew=["type","dispersion"],tw=["dispersion"],nw=["newdata","newoffset","type","dispersion"],Ur=2**-52;function rw(e){let t=e.map(n=>`"${n}"`);return t.length===1?t[0]:`c(${t.join(", ")})`}function Fo(e,t,n){return e.map((r,o)=>{let s=0;for(let a=0;a<r.length;a++)s+=r[a]*t[a];return s+n[o]})}function Hn(e,t,n,r){let o=0;for(let s=0;s<t.length;s++)r[s]!==0&&(o+=e.devContribution(t[s],n[s],r[s]));return o}var Xm=2**-1022,Xs=e=>e===0||Math.abs(e)>=Xm&&Math.abs(e)<1/0;function Gm(e,t,n,r){let o=e.variance(r),s=n**2,a=Math.sqrt(t*s/o);if(a>0&&a<1/0&&Xs(s)&&s!==0&&Xs(o)&&o!==0&&Xs(t*s))return a;if(e.muPower!==void 0&&e.varPower!==void 0&&r>0){let{c,a:i}=e.muPower;return Math.sqrt(t)*Math.abs(c)*r**((2*i-e.varPower)/2)}return a}function Ui(e,t,n,r){let o=(t-n)/r;if(Math.abs(r)>=Xm&&Math.abs(r)<1/0)return o;if(e.muPower!==void 0&&n>0){let{c:s,a}=e.muPower,c=n**(a/2);return(t-n)/s/c/c}return o}function ow(e,t,n,r){let o=e.variance(n),s=(t-n)*Math.sqrt(r)/Math.sqrt(o);if(Number.isFinite(s)&&Xs(o)&&o!==0||e.varPower===void 0||!(n>0))return s;let a=n**(e.varPower/4);return(t-n)/a*Math.sqrt(r)/a}var sw=.1;function Qm(e,t){let{x:n,p:r,y:o,w:s,offset:a,fam:c,epsilon:i,maxit:l}=e,f=o.length;if(o.some((m,u)=>s[u]>0&&!Number.isFinite(m)))throw new RangeError("NA/NaN/Inf in 'y'");if(n.some((m,u)=>s[u]>0&&m.some(h=>!Number.isFinite(h))))throw new RangeError("NA/NaN/Inf in 'x'");return wo(t,()=>{let m=(_,L)=>_.every(q=>c.etaOk(q))&&L.every(q=>c.muOk(q));if(r===0){let _=a.slice();if(!_.every(C=>c.etaOk(C)))throw new Error("invalid linear predictor values in empty model");let L=_.map(C=>c.linkinv(C));if(!L.every(C=>c.muOk(C)))throw new Error("invalid fitted means in empty model");let q=_.map(C=>c.muEta(C)),F=L.map((C,O)=>Gm(c,s[O],q[O],C)**2);return{coefficients:[],eta:_,mu:L,deviance:Hn(c,o,L,s),iter:0,converged:!0,boundary:!0,rank:0,qr:void 0,used:s.map(C=>C>0),workingWeights:F,workingResiduals:o.map((C,O)=>Ui(c,C,L[O],q[O]))}}let u,h;if(e.start!==void 0){if(e.start.length!==r)throw new RangeError(`length of 'start' should equal ${r} and correspond to initial coefs for ${rw(e.columnNames)}`);h=e.start.slice(),u=Fo(n,h,a)}else e.etastart!==void 0?u=e.etastart.slice():u=e.mustart.map(_=>c.link(_));let p=u.map(_=>c.linkinv(_));if(!m(u,p))throw new Error("cannot find valid starting values: please specify some");let w=Hn(c,o,p,s),d=w,v=!1,N=!1,b=0,M=new Array(r).fill(0),g,y=s.map(()=>!1),x=[],E=[],R=Math.min(1e-7,i/1e3);for(b=1;b<=l;b++){let _=u.map(V=>c.muEta(V));for(let V=0;V<f;V++){if(!(s[V]>0))continue;let S=c.variance(p[V]);if(Number.isNaN(S))throw new Error("NAs in V(mu)");if(S===0&&!(c.varPower!==void 0&&p[V]>0))throw new Error("0s in V(mu)");if(Number.isNaN(_[V]))throw new Error("NAs in d(mu)/d(eta)")}y=s.map((V,S)=>V>0&&_[S]!==0);let L=[];for(let V=0;V<f;V++)y[V]&&L.push(V);if(L.length===0){t.add(`no observations informative at iteration ${b}`),v=!1;break}x=new Array(f).fill(0);let q=[],F=[];for(let V of L){let S=Gm(c,s[V],_[V],p[V]);x[V]=S,q.push((u[V]-a[V]+Ui(c,o[V],p[V],_[V]))*S),F.push(n[V].map(P=>P*S))}g=Rt(F,{tol:R});let C=Or(g,q);if(C.some((V,S)=>g.pivot.indexOf(S)<g.rank&&!Number.isFinite(V))){t.add(`non-finite coefficients at iteration ${b}`),v=!1;break}M=C.map(V=>Number.isNaN(V)?0:V),N=!1;let O=u;if(u=Fo(n,M,a),p=u.map(V=>c.linkinv(V)),d=Hn(c,o,p,s),!Number.isFinite(d)){if(h===void 0)throw new Error("no valid set of coefficients has been found: please supply starting values");t.add("step size truncated due to divergence");let V=0;for(;!Number.isFinite(d);){if(V>=l)throw new Error("inner loop 1; cannot correct step size");V++,M=M.map((S,P)=>(S+h[P])/2),u=Fo(n,M,a),p=u.map(S=>c.linkinv(S)),d=Hn(c,o,p,s)}N=!0}if(!m(u,p)){if(h===void 0)throw new Error("no valid set of coefficients has been found: please supply starting values");t.add("step size truncated: out of bounds");let V=0;for(;!m(u,p);){if(V>=l)throw new Error("inner loop 2; cannot correct step size");V++,M=M.map((S,P)=>(S+h[P])/2),u=Fo(n,M,a),p=u.map(S=>c.linkinv(S))}N=!0,d=Hn(c,o,p,s)}let I=Math.max(i,1e-10);if(h!==void 0&&b>1&&(d-w)/(.1+Math.abs(d))>=I){t.add("step size truncated due to increasing deviance");let V=0;for(;(d-w)/(.1+Math.abs(d))>=I&&V<l;)V++,M=M.map((S,P)=>(S+h[P])/2),u=Fo(n,M,a),p=u.map(S=>c.linkinv(S)),d=Hn(c,o,p,s);if((d-w)/(.1+Math.abs(d))>=I){t.add("inner loop 3; cannot correct step size"),v=!1;break}}if(e.untilCoefficients){let V=dt(M.map(Math.abs));if(h!==void 0&&M.every((S,P)=>Math.abs(S-h[P])<=8*Ur*V)){v=!0;break}if(E.some(S=>S.every((P,G)=>P===M[G]))){v=!0;break}E.push(M.slice()),E.length>16&&E.shift()}else if(Math.abs(d-w)/(Math.abs(d)+.1)<i){v=!0;let V=0;for(let S=0;S<f;S++)y[S]&&(V=Math.max(V,Math.abs(u[S]-O[S])));V>sw&&t.add("glm.fit: the linear predictor was still changing at convergence: the data may be separated (no finite maximum likelihood estimates)");break}w=d,h=M.slice()}b>l&&(b=l);let A=r,T=M.slice();if(g!==void 0){A=g.rank;for(let _=g.rank;_<r;_++)T[g.pivot[_]]=NaN}let k=u.map(_=>c.muEta(_));return{coefficients:T,eta:u,mu:p,deviance:d,iter:b,converged:v,boundary:N,rank:A,qr:g,used:y,workingWeights:x.map(_=>_*_),workingResiduals:o.map((_,L)=>Ui(c,_,p[L],k[L]))}})}function Zm(e){return e.length>0&&typeof e[0]!="number"?e.map(t=>Array.from(t)):Array.from(e,t=>[t])}function Jm(e,t,n,r){r||(e.converged||n.add("glm.fit: algorithm did not converge"),e.boundary&&n.add("glm.fit: algorithm stopped at boundary value"),t.family==="binomial"&&e.mu.some(o=>o>1-10*Ur||o<10*Ur)&&n.add("glm.fit: fitted probabilities numerically 0 or 1 occurred"),t.family==="poisson"&&e.mu.some(o=>o<10*Ur)&&n.add("glm.fit: fitted rates numerically 0 occurred"))}function Kr(e,t,n,r={}){let o=Vr(e.familySpec),s="start"in r?r.start:e.start,a=kl(o,e.yGiven,e.priorGiven,s!==void 0||r.etastart!==void 0,n);if(!a.weights.some(f=>f>0))throw new RangeError("all weights are zero");let c=r.extraOffset===void 0?e.offset:e.offset.map((f,m)=>f+r.extraOffset[m]),i=e.x.map(f=>t.map(m=>f[m])),l=Qm({x:i,p:t.length,y:a.y,w:a.weights,offset:c,fam:o,epsilon:r.epsilon??e.epsilon,maxit:r.maxit??e.maxit,columnNames:t.map(f=>e.columnNames[f]),start:s,etastart:r.etastart,mustart:a.mustart,untilCoefficients:r.untilCoefficients},n);return Jm(l,o,n,t.length===0),{fit:l,fam:o,y:a.y,w:a.weights,size:a.size,offset:c}}function Ki(e,t,n={}){H(n,Xg,"glm"),H(vn(n,"family"),Qg,"glm","family"),H(vn(n,"family.link"),Hm,"glm","family.link"),H(vn(n,"link"),Hm,"glm","link");let r=n.intercept??!0,o=Zm(e);if(o.some(E=>E.length!==o[0].length))throw new TypeError("all rows of x must have the same length");let s=t.length>0&&typeof t[0]!="number",a=s?t.map(E=>Array.from(E)):Array.from(t),c=a.length;if(o.length!==c)throw new RangeError("variable lengths differ (found for 'x1')");let i=n.weights===void 0?void 0:Array.from(n.weights);if(i!==void 0&&i.length!==c)throw new RangeError("variable lengths differ (found for '(weights)')");let l=n.offset===void 0?void 0:Array.from(n.offset);if(l!==void 0&&l.length!==c)throw new RangeError("variable lengths differ (found for '(offset)')");let f=o.map((E,R)=>{let A=a[R];return!(Array.isArray(A)?A.some(k=>Number.isNaN(k)):Number.isNaN(A))&&E.every(k=>!Number.isNaN(k))&&!(i!==void 0&&Number.isNaN(i[R]))&&!(l!==void 0&&Number.isNaN(l[R]))}),m=o.filter((E,R)=>f[R]).map(E=>r?[1,...E]:E),u=a.filter((E,R)=>f[R]),h=u.length;if(h===0)throw new RangeError("0 (non-NA) cases");let p=i===void 0?new Array(h).fill(1):i.filter((E,R)=>f[R]);if(p.some(E=>E<0))throw new RangeError("negative weights not allowed");if(p.some(E=>!Number.isFinite(E)))throw new RangeError("non-finite weights not allowed");let w=l===void 0?new Array(h).fill(0):l.filter((E,R)=>f[R]);if(w.some(E=>!Number.isFinite(E)))throw new RangeError("the offset must be finite");let d=o.length>0?o[0].length:0,v=[...r?["(Intercept)"]:[],...Array.from({length:d},(E,R)=>`x${R+1}`)],N=v.length,b=typeof n.family=="object"?{...n.family}:{family:n.family??"gaussian"};n.link!==void 0&&(b.link=n.link);let M={x:m,columnNames:v,yGiven:u,priorGiven:p,offset:w,hasOffset:l!==void 0,intercept:r,familySpec:b,epsilon:n.epsilon??1e-8,maxit:Math.floor(n.maxit??25),start:n.start===void 0?void 0:Array.from(n.start),rows:f.flatMap((E,R)=>E?[R]:[])};if(!(M.epsilon>0&&M.epsilon<1/0))throw new RangeError("value of 'epsilon' must be > 0 and finite");if(!(M.maxit>0&&M.maxit<1/0))throw new RangeError("maximum number of iterations must be > 0");let g=!s&&aw(b)?Te([u]).k:0;if(g===0)return Z("glm",Br(Ym(M),c));let y=E=>D(E,-g),x=Ym({...M,yGiven:u.map(y),offset:w.map(y),start:M.start?.map(y)});return Z("glm",Br(iw(x,g,{...M,yScale:g}),c))}function Ym(e){let{x:t,offset:n,intercept:r}=e,o=t.length,s=e.columnNames.length,a=new ft,c=Array.from({length:s},(d,v)=>v),{fit:i,fam:l,y:f,w:m,size:u}=Kr(e,c,a),h;if(r&&e.hasOffset){let d=new ft,v=Qm({x:t.map(()=>[1]),p:1,y:f,w:m,offset:n,fam:l,epsilon:e.epsilon,maxit:e.maxit,columnNames:["(Intercept)"],mustart:i.mu},d);Jm(v,l,d,!1),d.list.forEach(N=>a.add(N)),h=v.deviance,v.converged||a.add("fitting to calculate the null deviance did not converge -- increase 'maxit'?")}else if(r){let d=Math.floor(Math.log2(dt(m))),v=0,N=0;for(let M=0;M<o;M++){if(!(m[M]>0))continue;let g=D(m[M],-d);v+=g,N+=g*f[M]}let b=N/v;if(!Number.isFinite(b)){let M=0;for(let x=0;x<o;x++)m[x]>0&&(M=Math.max(M,Math.abs(f[x])));let g=Math.floor(Math.log2(M)),y=0;for(let x=0;x<o;x++)m[x]>0&&(y+=D(m[x],-d)*D(f[x],-g));b=D(y/v,g)}h=wo(a,()=>Hn(l,f,f.map(()=>b),m))}else h=wo(a,()=>Hn(l,f,n.map(d=>l.linkinv(d)),m));let p=o-m.filter(d=>d===0).length,w=ql(l,f,i.mu,m,u,i.deviance,a)+2*i.rank;return eu(i.used,{coefficients:i.coefficients,fittedValues:i.mu,linearPredictors:i.eta,deviance:i.deviance,nullDeviance:h,aic:w,rank:i.rank,dfResidual:p-i.rank,dfNull:p-(r?1:0),iter:i.iter,converged:i.converged,boundary:i.boundary,y:f,priorWeights:m,weights:i.workingWeights,residuals:i.workingResiduals,family:l.family,link:l.name,warnings:a.list,qr:i.qr,model:e})}function aw(e){return(e.link??"identity")!=="identity"?!1:e.family==="gaussian"||e.family==="quasi"&&(e.variance??"constant")==="constant"}function iw(e,t,n){let r=s=>D(s,t),o={...e,coefficients:e.coefficients.map(r),fittedValues:e.fittedValues.map(r),linearPredictors:e.linearPredictors.map(r),deviance:D(e.deviance,2*t),nullDeviance:D(e.nullDeviance,2*t),aic:e.aic+2*e.priorWeights.filter(s=>s>0).length*t*Math.LN2,y:e.y.map(r),residuals:e.residuals.map(r),model:n};return Object.defineProperty(o,"scaled",{value:e,enumerable:!1}),eu(Qs.get(e),o)}var Qs=new WeakMap;function eu(e,t){return e!==void 0&&Qs.set(t,e),t}function Ht(e){let t=e.scaled;return t===void 0?{fit:e,k:0}:{fit:t,k:e.model.yScale??0}}function Gn(e){return Vr(e.model.familySpec)}function Do(e,t){let n=Ht(e),r=Vo(n.fit,t);return Gn(e).fixedDispersion?r:D(r,2*n.k)}function Vo(e,t){if(Gn(e).fixedDispersion)return 1;if(e.dfResidual<=0)return NaN;let n=0;for(let r=0;r<e.weights.length;r++)e.weights[r]>0&&(n+=e.weights[r]*e.residuals[r]**2);return e.weights.some(r=>r===0)&&t?.add("observations with zero weight not used for calculating dispersion"),n===1/0?cw(e):n/e.dfResidual}function cw(e){let t=[];for(let s=0;s<e.weights.length;s++)e.weights[s]>0&&t.push(s);if(!t.every(s=>Number.isFinite(e.weights[s])&&Number.isFinite(e.residuals[s])))return 1/0;let n=Te([t.map(s=>e.residuals[s])]),r=Te([t.map((s,a)=>Math.sqrt(e.weights[s])*Math.abs(n.groups[0][a]))]),o=0;for(let s of r.groups[0])o+=s*s;return D(o/e.dfResidual,2*(n.k+r.k))}function lw(e){return e.qr===void 0||e.rank===0?{inv:[],e:[]}:yr(e.qr.qr.slice(0,e.rank).map(t=>t.slice(0,e.rank)))}function fw(e){if(!(e>=0&&e<1/0))throw new RangeError("'dispersion' must be a non-negative finite number");return e}function Hr(e,t){let n=D(fw(e),-2*t);if(n===0?e===0:n>=2**-1022&&n<1/0)return{m:n,s:0};let r=Math.floor(Math.floor(Math.log2(e))/2);return{m:D(e,-2*r),s:r-t}}function Hi(e,t={}){H(t,Zg,"glmSummary");let{fit:n,k:r}=Ht(e),o=new ft,s=Gn(n),a=t.dispersion===void 0&&!s.fixedDispersion,c=t.dispersion===void 0?void 0:Hr(t.dispersion,r),i=c===void 0?Vo(n,o):c.m,l=c?.s??0,{inv:f,e:m}=lw(n),u=f.map((g,y)=>g.map((x,E)=>D(x,-m[y]-m[E]))),p=(n.qr===void 0?[]:n.qr.pivot.slice(0,n.rank)).map(g=>n.coefficients[g]),w=f.map((g,y)=>D(Math.sqrt(g[y]*i),-m[y])),d=l===0?p.map((g,y)=>g/w[y]):p.map((g,y)=>Le(g/Math.sqrt(f[y][y]*i),m[y]-l)),v=!a,N=d.map(g=>v?2*ce(-Math.abs(g)):n.dfResidual>0?2*lt(-Math.abs(g),n.dfResidual,!0,!1):NaN);a&&n.dfResidual===0&&(w.fill(NaN),d.fill(NaN));let b=g=>D(g,r),M=g=>D(g,2*r);return Z("glmSummary",{dispersion:l===0?M(i):t.dispersion,estimate:p.map(b),stdError:l===0?w.map(b):f.map((g,y)=>Le(Math.sqrt(g[y]*i),l+r-m[y])),statistic:d,pValue:N,statisticName:v?"z value":"t value",covUnscaled:u,covScaled:l===0?u.map(g=>g.map(y=>M(y*i))):f.map((g,y)=>g.map((x,E)=>Le(x*i,2*(l+r)-m[y]-m[E]))),dfResidual:n.dfResidual,warnings:o.list},e)}function mw(e,...t){H(Cr(t),Jg,"glmResiduals");let n=Lr(["type"],t).type??"deviance",r=Ht(e);return Gi(r.fit,n).map(o=>D(o,r.k))}function Gi(e,t){let n=Gn(e),{y:r,fittedValues:o,priorWeights:s}=e;switch(t){case"response":return r.map((a,c)=>a-o[c]);case"working":return e.residuals.slice();case"pearson":return r.map((a,c)=>ow(n,a,o[c],s[c]));case"deviance":return r.map((a,c)=>s[c]===0?0:Math.sign(a-o[c])*Math.sqrt(Math.max(n.devContribution(a,o[c],s[c]),0)));default:throw new RangeError(`unknown type of residuals "${String(t)}"`)}}function Po(e){let t=[],n=e.qr,r=[];if(n!==void 0&&e.rank>0){let a=n.qr.length,c=Array.from({length:a},(l,f)=>Array.from({length:e.rank},(m,u)=>f===u?1:0));r=gs(n,c).map(l=>l.reduce((f,m)=>f+m*m,0))}let o=0,s=Qs.get(e)??e.weights.map(a=>a>0);return e.priorWeights.forEach((a,c)=>{let i=s[c]?r[o++]??0:0;a>0&&t.push(i>1-10*Ur?1:i)}),t}function tu(e){let t=[],n=0,r=Qs.get(e)??e.weights.map(o=>o>0);return e.priorWeights.forEach((o,s)=>{let a=r[s]?n++:-1;o>0&&t.push(a)}),t}function uw(e){let t=e.slice().sort((r,o)=>r-o),n=t.length>>1;return t.length%2===1?t[n]:(t[n-1]+t[n])/2}function hw(e,t={}){H(t,ew,"glmRstandard");let n=t.type??"pearson";if(n!=="pearson"&&n!=="deviance")throw new RangeError(`unknown type "${String(n)}" for glmRstandard`);let{fit:r,k:o}=Ht(e),s=Po(r),{m:a,s:c}=t.dispersion===void 0?{m:Vo(r),s:0}:Hr(t.dispersion,o),i=Math.sqrt(a),l=Gi(r,n).filter((m,u)=>r.priorWeights[u]>0),f=r.rank>0?100*Ur*uw(l.map(Math.abs)):0;return l.map((m,u)=>{let h=Le((Math.abs(m)<f?0:m)/(i*Math.sqrt(1-s[u])),-c);return Number.isFinite(h)?h:NaN})}function pw(e,t={}){H(t,tw,"glmCooksDistance");let{fit:n,k:r}=Ht(e),o=Po(n),{m:s,s:a}=t.dispersion===void 0?{m:Vo(n),s:0}:Hr(t.dispersion,r);return Gi(n,"pearson").filter((i,l)=>n.priorWeights[l]>0).map((i,l)=>{let f=o[l]/(s*n.rank),m=(i/(1-o[l]))**2*f;return(!Number.isFinite(m)||f<2**-1022)&&(m=dw(i,o[l],s,n.rank)),a!==0&&(m=Le(m,-2*a)),Number.isFinite(m)?m:NaN})}function dw(e,t,n,r){if(!Number.isFinite(e)||!(n>0&&Number.isFinite(n)))return NaN;let o=e===0?0:Math.floor(Math.log2(Math.abs(e))),s=Math.floor(Math.log2(n)),a=D(e,-o)/(1-t);return D(a*a*(t/(D(n,-s)*r)),2*o-s)}function gw(e,t={}){H(t,nw,"glmPredict");let{fit:n,k:r}=Ht(e),o=N=>D(N,r),s=Gn(n),a,c;if(t.newdata===void 0)a=n.model.x,c=n.linearPredictors.slice();else{if(a=Zm(t.newdata).map(M=>n.model.intercept?[1,...M]:M),a.some(M=>M.length!==n.coefficients.length))throw new RangeError("newdata must have one column per predictor of the fit");let N=t.newoffset===void 0?a.map(()=>0):Array.from(t.newoffset,M=>D(M,-r));if(N.length!==a.length)throw new RangeError("newoffset must have one value per row of newdata");let b=n.coefficients.flatMap((M,g)=>Number.isNaN(M)?[]:[g]);c=a.map((M,g)=>{let y=M.reduce((E,R,A)=>Number.isNaN(n.coefficients[A])?E:E+R*n.coefficients[A],0);return(ho(y,0)?cr(b.map(E=>M[E]),b.map(E=>n.coefficients[E]),y):y)+N[g]})}let{m:i,s:l}=t.dispersion===void 0?{m:Vo(n),s:0}:Hr(t.dispersion,r),f=N=>D(N,r+l),m=n.rank,u=n.qr===void 0?[]:n.qr.pivot.slice(0,m),h=n.qr?.qr,p=a.map(N=>{let b=new Array(m).fill(0),M=0;for(let g=0;g<m;g++){let y=N[u[g]];for(let x=0;x<g;x++)y-=h[x][g]*b[x];b[g]=y/h[g][g],M+=b[g]*b[g]}if(!(M>=2**-1022&&M<1/0)){let g=b.reduce((y,x)=>Math.max(y,Math.abs(x)),0);if(g>0&&Number.isFinite(g)){let y=Math.sqrt(b.reduce((E,R)=>E+(R/g)**2,0)),x=g*y*Math.sqrt(i);return Number.isFinite(x)?x:g*(y*Math.sqrt(i))}}return Math.sqrt(M)*Math.sqrt(i)}),w=t.type??"link";if(w!=="link"&&w!=="response")throw new RangeError(`invalid 'type': ${String(w)}`);let d=t.newdata===void 0?[]:bs(n.qr,m,a),v=d.length>0?{nonEstimable:d,warnings:[ws]}:{};return w==="link"?Z("glmPredict",{fit:c.map(o),seFit:p.map(f),...v},e):Z("glmPredict",wo(new ft,()=>({fit:c.map(N=>o(s.linkinv(N))),seFit:p.map((N,b)=>f(N*Math.abs(s.muEta(c[b])))),...v})),e)}var ww=["parm","level","test"],nu={epsilon:1e-300,maxit:100,untilCoefficients:!0};function Yi(e,t,n,r,o=1e-11){let s=[];for(let w=0;w<t.length;w++)n[w]>0&&s.push(w);let a=0,c=0;for(let w of s)a+=n[w],c+=n[w]*t[w];if(r&&!(Number.isFinite(a)&&Number.isFinite(c))){let w=0;for(let d of s)w=Math.max(w,n[d]);if(Number.isFinite(w)){let d=w>1?Math.floor(Math.log2(w)):0;a=0,c=0;for(let v of s){let N=D(n[v],-d);a+=N,c+=N*t[v]}}}let i=r?c/a:0,l=0;for(let w of s)l+=n[w]*(t[w]-i)**2;if((e.length>0?e[0].length:0)===0||s.length===0)return 0;let m=s.map(w=>Math.sqrt(n[w])),u=Rt(s.map((w,d)=>e[w].map(v=>v*m[d])),{tol:o}),h=Fr(u,s.map((w,d)=>t[w]*m[d])),p=0;for(let w of h)p+=w*w;if(!Number.isFinite(l)){let w=s.map((v,N)=>m[N]*(t[v]-i)),d=0;for(let v of w)d=Math.max(d,Math.abs(v));for(let v of h)d=Math.max(d,Math.abs(v));if(Number.isFinite(d)){let v=d>1?Math.floor(Math.log2(d)):0,N=0;for(let M of w)N+=D(M,-v)**2;let b=0;for(let M of h)b+=D(M,-v)**2;return D(D(N-b,v),v)}}return l-p}function bw(e,t={}){H(t,ww,"glmConfint");let{fit:n,k:r}=Ht(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},(L,q)=>q);for(let L of c)if(!Number.isInteger(L)||L<0||L>=a)throw new RangeError(`invalid coefficient index ${L}`);let i=n.model,l=n.coefficients.map((L,q)=>q).filter(L=>!Number.isNaN(n.coefficients[L])),f=new ft,m=Kr(i,l,f,{...nu,start:i.start===void 0?void 0:l.map(L=>i.start[L])}),u=new Array(a).fill(NaN);if(l.forEach((L,q)=>u[L]=m.fit.coefficients[q]),!m.fit.converged)return{lower:c.map(()=>NaN),upper:c.map(()=>NaN)};let h=!1,p={...n,coefficients:u,weights:m.fit.workingWeights,residuals:m.fit.workingResiduals,qr:m.fit.qr,rank:m.fit.rank},w=tt(()=>Hi(p)),d=w.dispersion,v=new Array(a).fill(NaN);(m.fit.qr===void 0?[]:m.fit.qr.pivot.slice(0,m.fit.rank)).forEach((L,q)=>v[l[L]]=w.stdError[q]);let b=i.x.map(L=>l.map(q=>L[q])),M=m.fit.eta,g=(L,q)=>{let F=l.filter(I=>I!==L),C=Kr(i,F,new ft,{...nu,start:void 0,etastart:M,extraOffset:i.x.map(I=>I[L]*q)}).fit;if(!C.converged)return h=!0,NaN;let O=s==="LRT"?C.deviance-m.fit.deviance:Yi(b,C.workingResiduals,C.workingWeights,!1);return Math.sign(q-u[L])*Math.sqrt(Math.max(O/d,0))},y=(1-o)/2,x=Math.sqrt(Math.PI/2)*o*(1+Math.PI/12*o**2+7*Math.PI**2/480*o**4),E=2**-9,R=x<E,A=R?[-x,x]:[pe(y),y<1e-8?pe(y,{lowerTail:!1}):pe((1+o)/2)],T=c.map(()=>NaN),k=c.map(()=>NaN),_=(L,q,F)=>{let C=F===0?-1:1;h=!1;let O=NaN,I=NaN;for(let $=0;$<=30;$++){let J=u[L]+C*2**$*v[L],ee=g(L,J);if(Math.abs(ee)>=Math.abs(q)){O=J,I=ee;break}}if(Number.isNaN(O)||h)return NaN;let V=$=>$===u[L]?-q:g(L,$)-q,[S,P]=F===0?[O,u[L]]:[u[L],O],[G,Q]=F===0?[I-q,-q]:[-q,I-q],te;try{te=Pt(V,S,P,{tol:1e-14*v[L],maxIter:500,fLower:G,fUpper:Q})}catch($){if(!h)throw $;te=NaN}return h?NaN:te};return c.forEach((L,q)=>{if(Number.isNaN(u[L]))return;if(!R){T[q]=_(L,A[0],0),k[q]=_(L,A[1],1);return}let F=[-2*E,-E,0,E,2*E],C=[_(L,-2*E,0),_(L,-E,0),u[L],_(L,E,1),_(L,2*E,1)].map(I=>I-u[L]),O=I=>{let V=0;for(let S=0;S<5;S++){let P=1;for(let G=0;G<5;G++)G!==S&&(P*=(I-F[G])/(F[S]-F[G]));V+=P*C[S]}return u[L]+V};T[q]=O(A[0]),k[q]=O(A[1])}),{lower:T.map(L=>D(L,r)),upper:k.map(L=>D(L,r))}}var vw=["type","omega","sandwich"],yw=["cluster","type","cadjust","multi0","fix","sandwich"],Zs=2**-52;function Zi(e,t){if(t===0)return e;let n=ec.get(e)??{mant:e,exp:e.map(()=>0)};return Nr(n.mant,n.exp.map(r=>r+t))}var Js=e=>Math.floor(Math.log2(Math.abs(e)));function rn(e){if(e===0||!Number.isFinite(e))return[e,0];let t=Js(e);return[D(e,-t),t]}var jo=(e,t)=>[e[0]*t[0],e[1]+t[1]];function Yn(e,t=!1){let n=-1/0;for(let[o,s]of e)o!==0&&Number.isFinite(o)&&(n=Math.max(n,s+Js(o)));let r=n===-1/0?0:n;return t&&(r=2*Math.floor(r/2)),{v:e.map(([o,s])=>Le(o,s-r)),s:r}}var Xn=(e,t)=>Math.abs(t)<=2046?D(e,t):Le(e,t),ec=new WeakMap;function Nr(e,t){let n=e.map((r,o)=>r.map((s,a)=>Xn(s,t[o]+t[a])));return ec.set(n,{mant:e,exp:t}),n}function tc(e,t,n){let r=ec.get(e),o=r?.mant[t]?.[t];if(!(r===void 0||o===void 0||!Object.is(Xn(o,2*r.exp[t]),n)))return Xn(Math.sqrt(o),r.exp[t])}function Qn(e){return typeof e=="object"&&e!==null&&"family"in e&&"model"in e}function ea(e){if(!(typeof e=="object"&&e!==null&&Array.isArray(e.coefficients)&&"qr"in e&&(Qn(e)||"sigma"in e&&"x"in e&&"rows"in e)))throw new TypeError("'fit' must be the result of lm or glm")}function au(e,t){return e.pivot.slice(0,t).map((n,r)=>[n,r]).sort((n,r)=>n[0]-r[0])}function iu(e,t){let{inv:n,e:r}=yr(e.qr.slice(0,t).map(s=>s.slice(0,t))),o=au(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 ru(e,t,n){let{inv:r,e:o,cols:s}=iu(e,t);return{A:r.map((a,c)=>a.map((i,l)=>D(i,-o[c]-o[l]-n))),cols:s}}function ou(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)?Js(i):0}),{inv:o}=yr(n.map(a=>a.map((c,i)=>D(c,-r[i])))),s=au(e,t);return{B:s.map(([,a])=>s.map(([,c])=>o[a][c])),e:s.map(([,a])=>r[a])}}function cu(e,t){let n=e.qr.length;return gs(e,Array.from({length:n},(r,o)=>Array.from({length:t},(s,a)=>o===a?1:0)))}function Nw(e,t){let n=e.qr.length;return t===0?new Array(n).fill(0):cu(e,t).map(o=>{let s=o.reduce((a,c)=>a+c*c,0);return s>1-10*Zs?1:s})}function su(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=Ia(e,i),f=0;for(let m=t;m<c;m++)f+=l[m]*l[m];return f})}function ta(e){if(Qn(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 nc(e){let t=ta(e);if(e.qr===void 0||e.rank===0)return{vcov:[],names:[]};let{inv:n,e:r,cols:o}=iu(e.qr,e.rank),s=Qn(e)?0:Bi(e.qr),a,c;if(Qn(e)){let l=Ht(e),f=Gn(e).fixedDispersion?1:Do(l.fit),m=f===0||!Number.isFinite(f)?0:2*Math.floor(Js(f)/2);a=D(f,-m),c=m+2*l.k}else{let[l,f]=rn(e.sigma);a=l*l,c=2*f}let i=n.map(l=>l.map(f=>f*a));return{vcov:Nr(i,r.map(l=>(c-s)/2-l)),names:o.map(l=>t[l])}}function lu(e,t=!1){let n=ta(e);if(Qn(e)){let w=t?Ht(e):{fit:e,k:0},d=w.fit,v=Gn(d),N=d.priorWeights,b=N.map(C=>C>0),M=N.flatMap((C,O)=>C>0?[O]:[]),{A:g,cols:y}=d.qr===void 0||d.rank===0?{A:[],cols:[]}:ru(d.qr,d.rank,0),x=M.map(C=>y.map(O=>d.model.x[C][O])),E=M.map(C=>d.weights[C]),R=M.map(C=>d.weights[C]*d.residuals[C]),A=v.family==="binomial"||v.family==="poisson",T=A?1:R.reduce((C,O)=>C+O*O,0)/E.reduce((C,O)=>C+O,0),k=d.model.rows,_=Po(d),L=tu(d),q=()=>{let C=Ht(d),O=M.map($=>C.fit.residuals[$]),I=E.map(Math.sqrt),{B:V,e:S}=d.qr===void 0||d.rank===0?{B:[],e:[]}:ou(d.qr,d.rank),P=x.map(($,J)=>$.map((ee,Se)=>D(I[J]*ee,-S[Se]))),{v:G,s:Q}=Yn(I.map(($,J)=>$===0?[0,0]:jo(rn($),rn(O[J])))),te=()=>{let $=[1,0];if(!A){let J=Yn(E.map((Se,re)=>Se===0?[0,0]:jo(rn(Se),rn(O[re])))),ee=Yn(E.map(rn));$=[J.v.reduce((Se,re)=>Se+re*re,0)/ee.v.reduce((Se,re)=>Se+re,0),2*J.s-ee.s+2*C.k]}return{T:x.map(J=>J.map((ee,Se)=>Xn(ee,-S[Se]))),factor:[$[0]*$[0],2*$[1]]}};return{T:P,B:V,e:S,u:G,s:Q+C.k,omegaPart:te}},F;if(Mr(T)&&R.every(Number.isFinite))F=R.map(C=>C/T);else{let C=Ht(d),O=Yn(E.map((I,V)=>I===0?[0,0]:jo(rn(I),rn(C.fit.residuals[M[V]]))));if(A)F=O.v.map(I=>Xn(I,O.s+C.k));else{let I=Yn(E.map(rn)),V=O.v.reduce((S,P)=>S+P*P,0)/I.v.reduce((S,P)=>S+P,0);F=O.v.map(S=>Xn(S/V,I.s-O.s-C.k))}}return{glm:!0,names:y.map(C=>n[C]),X:x,W:E,res:F,phiS:T,A:g,h:_,omh:su(d.qr,d.rank,_,C=>L[C]),qr:d.qr,rank:d.rank,qrRow:C=>L[C],inputRows:M.map(C=>k[C]),nInput:ji(e,k),nonZero:b,dispersion:()=>Do(d),scaled:q,k:w.k}}let r=e,o=r.weights,s=r.residuals.map((w,d)=>o===void 0||o[d]!==0),a=s.flatMap((w,d)=>w?[d]:[]),c=Bi(r.qr),{A:i,cols:l}=r.rank===0?{A:[],cols:[]}:ru(r.qr,r.rank,c),f=a.map(w=>l.map(d=>r.x[w][d])),m=a.map(w=>o?.[w]??1),u=a.map((w,d)=>m[d]*r.residuals[w]),h=Nw(r.qr,r.rank),p=()=>{let w=m.map(y=>c===0?Math.sqrt(y):D(Math.sqrt(y),-c/2)),{B:d,e:v}=r.rank===0?{B:[],e:[]}:ou(r.qr,r.rank),N=f.map((y,x)=>y.map((E,R)=>D(w[x]*E,-v[R]))),{v:b,s:M}=Yn(w.map((y,x)=>jo(rn(y),rn(r.residuals[a[x]]))));return{T:N,B:d,e:v,u:b,s:M,omegaPart:()=>({T:f.map(y=>y.map((x,E)=>Xn(x,-v[E]))),factor:[1,-2*c]})}};return{glm:!1,names:l.map(w=>n[w]),X:f,W:m,res:u,phiS:1,A:i,h,omh:su(r.qr,r.rank,h,w=>w),qr:r.qr,rank:r.rank,qrRow:w=>w,inputRows:a.map(w=>r.rows[w]),nInput:ji(r,r.rows),nonZero:s,dispersion:()=>r.sigma*r.sigma,scaled:p,k:0}}var rc=2**-969,Mr=e=>Math.abs(e)>=rc&&Math.abs(e)<1/0;function fu(e,t,n,r){if(!(e.phiS>=rc&&e.phiS<1/0)||!Mr(e.X.length*e.phiS*e.phiS))return!1;for(let o=0;o<n.length;o++)if(!Mr(e.A[o][o])||!r&&(!Mr(t[o][o])||!Mr(n[o][o]))||!n[o].every(Number.isFinite))return!1;return!0}function $o(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 Gr(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 Xi(e){let t=e.slice(0,10).map(n=>String(n+1));return e.length>10?`${t.join(", ")}, ...`:t.join(", ")}var Mw=["HC3","const","HC","HC0","HC1","HC2","HC4","HC4m","HC5"];function Ew(e,t={}){H(t,vw,"vcovHC"),ea(e);let n=Ie(t.type,Mw),r=n==="HC"?"HC0":n,o=t.sandwich??!0;if(typeof o!="boolean")throw new TypeError("'sandwich' must be true or false");let s=lu(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 w;if(typeof t.omega=="function"?w=t.omega(l.slice(),f.slice(),a-c):(w=t.omega,w.length!==a&&w.length===s.nonZero.length&&(w=w.filter((d,v)=>s.nonZero[v]))),!Array.isArray(w)&&!ArrayBuffer.isView(w))throw new TypeError("'omega' must give an array of numbers");if(w.length!==a)throw new RangeError(`'omega' must have one value per observation (${a})`);if(u=Array.from(w,Number),u.some(d=>!Number.isFinite(d)))throw new RangeError("'omega' must be finite")}else if(r==="const"){let w=s.dispersion();u=s.W.map(d=>w*d/(s.phiS*s.phiS))}else{let w=f.flatMap((N,b)=>N>1-Math.sqrt(Zs)?[s.inputRows[b]]:[]);if(w.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) ${Xi(w)}`:`${r} covariances are numerically unstable for hat values close to 1 (and undefined if exactly 1) as for observation(s) ${Xi(w)}`),r!=="HC0"&&r!=="HC1"){let N=f.flatMap((b,M)=>b>=1?[s.inputRows[M]]:[]);if(N.length>0)throw new RangeError(`${r} covariances are undefined for hat values of 1 as for observation(s) ${Xi(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,M)=>b/m[M]);case"HC3":return N.map((b,M)=>b/m[M]**2);case"HC4":return N.map((b,M)=>b/m[M]**Math.min(4,a*f[M]/d));case"HC4m":return N.map((b,M)=>b/m[M]**(Math.min(1,a*f[M]/d)+Math.min(1.5,a*f[M]/d)));default:return N.map((b,M)=>b/Math.sqrt(m[M]**Math.min(a*f[M]/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=nc(e).vcov;else if(o){let w=Gr(s.X,u,a);if(p=$o(s.A,w,a*s.phiS*s.phiS),!fu(s,w,p,(h===void 0?u:l).every(d=>d===0))){let d=s.scaled();if(h!==void 0){let v=$o(d.B,Gr(d.T,h(d.u.map(N=>N*N)),a),a);p=Nr(v,d.e.map(N=>d.s-N))}else{let{T:v,factor:N}=d.omegaPart(),b=Yn(u.map(M=>jo(rn(M),N)),!0);p=Nr($o(d.B,Gr(v,b.v,a),a),d.e.map(M=>b.s/2-M))}}p=Zi(p,s.k)}else{let w=Gr(s.X,u,a);if(hu(w,(h===void 0?u:l).every(d=>d===0)))p=w;else{let d=s.scaled();if(h!==void 0){let{mant:v,exp:N}=pu(d,Gr(d.T,h(d.u.map(b=>b*b)),a));p=Nr(v,N)}else{let v=Yn(u.map(rn),!0);p=Nr(Gr(d.omegaPart().T,v.v,a),d.e.map(N=>v.s/2+N))}}p=Zi(p,-s.k)}return Z("vcovHC",{vcov:p,names:s.names,warnings:i},e)}var xw=["HC","HC0","HC1","HC2","HC3"];function Rw(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 Aw(e){let t=new Map;return{g:e.map(r=>{let o=t.get(r);return o===void 0&&(o=t.size,t.set(r,o)),o}),G:t.size}}function Tw(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 mu=Zs**(1/1.3);function uu(e,t){let{values:n,vectors:r}=Vt(e,{symmetric:!0});if(n.some(a=>!(a>=mu)))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 Sw(e){return{A:e.A,xs:t=>e.X[t].map(n=>n*Math.sqrt(e.W[t])),rs:t=>{let n=Math.sqrt(e.W[t]);return n===0?0:e.res[t]/n},single:(t,n,r)=>e.X[t].map(o=>n*e.res[t]*r*o),psi:t=>e.X[t].map(n=>n*e.res[t])}}function kw(e){return{A:e.B,xs:t=>e.T[t],rs:t=>e.u[t],single:(t,n,r)=>e.T[t].map(o=>n*e.u[t]*r*o),psi:t=>e.T[t].map(n=>n*e.u[t])}}function qw(e,t,n,r,o){let s=e.names.length,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]>=mu))throw Qi(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??=cu(e.qr,e.rank);let N=Iw(e,l,f,h,o);if(N===null)throw Qi(m);if(N!==void 0)return Array.from({length:s},(b,M)=>c*u.reduce((g,y,x)=>g+y[M]*N[x],0))}let p=u.map(N=>Array.from({length:s},(b,M)=>N.reduce((g,y,x)=>g+y*t.A[x][M],0))),w=u.map((N,b)=>u.map((M,g)=>(b===g?1:0)-p[b].reduce((y,x,E)=>y+x*M[E],0)));for(let N=0;N<w.length;N++)for(let b=0;b<N;b++)w[N][b]=w[b][N]=(w[N][b]+w[b][N])/2;let d=uu(w,o);if(d===void 0)throw Qi(m);let v=d.map(N=>N.reduce((b,M,g)=>b+M*h[g],0));return Array.from({length:s},(N,b)=>c*u.reduce((M,g,y)=>M+g[b]*v[y],0))})}function Iw(e,t,n,r,o){let s=e.qr,a=e.rank,c=new Array(a).fill(0),i=n.map(g=>{let y=e.qrRow(g);return y<0?c:t[y]}),l=Array.from({length:a},(g,y)=>Array.from({length:a},(x,E)=>i.reduce((R,A)=>R+A[y]*A[E],0))),{values:f,vectors:m}=Vt(l,{symmetric:!0});if(!f.some(g=>g>.5))return;let u=m,h=r.slice(),p=[];f.forEach((g,y)=>{let x=i.map(E=>E.reduce((R,A,T)=>R+A*u[T][y],0));if(g>.5)p.push(x.map(E=>E/Math.sqrt(g)));else if(g>0){let R=Math.expm1(o*Math.log1p(-g))/g*x.reduce((A,T,k)=>A+T*r[k],0);x.forEach((A,T)=>h[T]+=R*A)}});let w=s.qr.length,d=p.map(g=>{let y=new Array(w).fill(0);return n.forEach((x,E)=>{let R=e.qrRow(x);R>=0&&(y[R]=g[E])}),Ia(s,y).slice(a)}),v=d.map(g=>d.map(y=>g.reduce((x,E,R)=>x+E*y[R],0))),N=uu(v,o);if(N===void 0)return null;let b=p.map(g=>g.reduce((y,x,E)=>y+x*r[E],0)),M=N.map((g,y)=>g.reduce((x,E,R)=>x+E*b[R],0)-b[y]);return p.forEach((g,y)=>g.forEach((x,E)=>h[E]+=M[y]*x)),h}function Qi(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 _w(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 Ji(e){if(e.length===0)return e;let{values:t,vectors:n}=Vt(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 Lw(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}=Vt(o,{symmetric:!0}),a=s.reduce((i,l)=>Math.max(i,Math.abs(l)),0);if(s.every(i=>i>=-16*n*Zs*a))return{mant:e,exp:t};let c=Math.max(...t);return{mant:Ji(e.map((i,l)=>i.map((f,m)=>Xn(f,t[l]+t[m]-2*c)))),exp:t.map(()=>c)}}function hu(e,t){return t||e.every((n,r)=>Mr(n[r])&&n.every(Number.isFinite))}function pu(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 Cw(e,t={}){H(t,yw,"vcovCL"),ea(e);let n=lu(e,!0),r=n.X.length,o=n.names.length,s=t.type===void 0?n.glm?"HC0":"HC1":Ie(t.type,xw),a=s==="HC"?"HC0":s,c=t.cadjust??!0,i=t.multi0??!1,l=t.fix??!1,f=t.sandwich??!0;for(let[b,M]of[["cadjust",c],["multi0",i],["fix",l],["sandwich",f]])if(typeof M!="boolean")throw new TypeError(`'${b}' must be true or false`);let m,u=t.cluster;if(u===void 0)m=[n.inputRows.map(String)];else{let b=y=>Array.isArray(y)||ArrayBuffer.isView(y);if(!b(u))throw new TypeError("'cluster' must be an array of labels or an array of such arrays");let g=u.length>0&&Array.from(u).every(b)?Array.from(u):[u];if(g.length===0)throw new RangeError("'cluster' has no dimension");m=g.map(y=>{if(y.length!==n.nInput)throw new RangeError("number of observations in 'cluster' and 'estfun()' do not match");return n.inputRows.map(x=>Rw(y[x]))})}if(a==="HC1"&&r<=o)throw new RangeError("HC1 covariances need more observations than coefficients");let h=m.length,p=Tw(h).map((b,M)=>{let g=i&&h>1&&M===2**h-2,y=g?n.inputRows.map(String):n.inputRows.map((A,T)=>b.length===1?m[b[0]][T]:JSON.stringify(b.map(k=>m[k][T]))),{g:x,G:E}=Aw(y);if(E<2)throw new RangeError("at least two clusters are needed");let R=g?a==="HC1"?(r-o)/(r-1):1:c?E/(E-1):1;return{g:x,G:E,c:R,sign:b.length%2===1?1:-1}}),w=b=>{let M=Array.from({length:o},()=>new Array(o).fill(0));for(let{g,G:y,c:x,sign:E}of p){let R;a==="HC2"||a==="HC3"?R=qw(n,b,g,y,a==="HC2"?-.5:-1):y===r?R=n.X.map((T,k)=>b.psi(k)):(R=Array.from({length:y},()=>new Array(o).fill(0)),n.X.forEach((T,k)=>{let _=R[g[k]],L=b.psi(k);for(let q=0;q<o;q++)_[q]+=L[q]}));let A=_w(R,o);M=M.map((T,k)=>T.map((_,L)=>_+E*x*A[k][L]/r))}return a==="HC1"&&(M=M.map(g=>g.map(y=>y*(r-1)/(r-o)))),M},d=n.res.every(b=>b===0),v=n.phiS>=rc&&n.phiS<1/0&&n.A.every((b,M)=>Mr(b[M]))&&n.res.every(Number.isFinite),N;if(v){let b=w(Sw(n));if(!f)hu(b,d)&&(N=l?Ji(b):b);else{let M=$o(n.A,b,r*n.phiS*n.phiS);fu(n,b,M,d)&&(N=l?Ji(M):M)}}if(N===void 0){let b=n.scaled(),M=w(kw(b)),{mant:g,exp:y}=f?{mant:$o(b.B,M,r),exp:b.e.map(E=>b.s-E)}:pu(b,M),x=l?Lw(g,y):{mant:g,exp:y};N=Nr(x.mant,x.exp)}return N=Zi(N,f?n.k:-n.k),Z("vcovCL",{vcov:N,names:n.names,warnings:[]},e)}var Ow=["vcov","df"],Fw=["parm","level","vcov","df"];function du(e){return typeof e=="object"&&e!==null&&!Array.isArray(e)&&Array.isArray(e.vcov)&&Array.isArray(e.names)}function gu(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 wu=e=>new Set(e).size===e.length;function bu(e,t){ea(e);let n=e.coefficients,r=ta(e),o=n.length,s=n.map(m=>Number.isNaN(m)),a,c,i=[],l;if(t===void 0){let m=nc(e),u=new Map(m.names.map((p,w)=>[p,w])),h=r.map((p,w)=>s[w]?void 0:u.get(p));a=h.map(p=>h.map(w=>p===void 0||w===void 0?NaN:m.vcov[p][w])),c=r,l=h.map(p=>p===void 0?NaN:tc(m.vcov,p,m.vcov[p][p])??Math.sqrt(m.vcov[p][p]))}else{let m=typeof t=="function"?tt(()=>t(e)):t,u=du(m)?m.vcov:m;if(du(m)){if(a=gu(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=gu(m);l=a.map((h,p)=>{let w=h[p],d=typeof u=="object"&&u!==null?tc(u,p,w):void 0;if(d!==void 0&&!Number.isNaN(d))return d;if(!Number.isFinite(w))throw new RangeError(`the variance of coefficient ${p+1} in 'vcov' is not a finite number`);if(w<0)throw new RangeError(`the variance of coefficient ${p+1} in 'vcov' is negative`);return Math.sqrt(w)})}if(c!==void 0&&wu(c)&&wu(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 vu(e,t){if(t===void 0)return Qn(e)?1/0:e.dfResidual;if(typeof t!="number")throw new TypeError("'df' must be a number");if(Number.isNaN(t))throw new RangeError("'df' must not be NaN");return t}var yu=e=>Number.isFinite(e)&&e>0;function Dw(e){return Qn(e)?e.priorWeights.filter(t=>t!==0).length:e.rank+e.dfResidual}function Vw(e,t={}){H(t,Ow,"coeftest");let{est:n,se:r,names:o,warnings:s}=bu(e,t.vcov),a=vu(e,t.df),c=yu(a),i=n.map((f,m)=>f/r[m]),l=i.map(f=>Number.isNaN(f)?NaN:c?2*lt(-Math.abs(f),a,!0,!1):2*ce(-Math.abs(f)));return Z("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:Dw(e),warnings:s},e,typeof t.vcov=="function"?void 0:t.vcov)}function Pw(e){let t=[100*e,100*(1-e)],n=t.map(o=>{if(o===0||!Number.isFinite(o))return 0;let s=Math.floor(Math.log10(Math.abs(o))),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 jw(e,t={}){H(t,Fw,"coefci");let n=t.level??.95;if(typeof n!="number")throw new TypeError("'level' must be a number");if(!(n>=0&&n<=1))throw new RangeError("'level' must be between 0 and 1");let r=bu(e,t.vcov),o=vu(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]=yu(o)?[wt(c,o,!0,!1),c<1e-8?wt(c,o,!1,!1):wt(1-c,o,!0,!1)]:[pe(c),c<1e-8?pe(c,{lowerTail:!1}):pe(1-c)],f=(m,u,h)=>m+h*u;return Z("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:Pw(c),warnings:r.warnings},e,typeof t.vcov=="function"?void 0:t.vcov)}var $w=["test","dispersion"],Ww=(e,t)=>bt(e,t/2,2,!1,!1);function zw(e,t,n){let{fit:r,w:o}=Kr(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 oc=(e,t)=>Yi(t.x,e.fit.workingResiduals,e.fit.workingWeights,e.intercept);function Bw(e,t={}){H(t,$w,"glmAnova");let n=Array.isArray(e)?e:[e];if(n.length===0)throw new RangeError("no fits to compare");let r=n.map(Ht),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:Hr(t.dispersion,o),c=a?.s??0,i=Uw(s,Array.isArray(e)&&s.length>1,{...t,...a===void 0?{}:{dispersion:a.m}},c);if(o===0&&c===0)return Z("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]?D(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"),w=s[0].model.x.length;for(let d of m)d[u]=d[h]+2*(w-d[p])*t.dispersion}return Z("glmAnova",{...i,table:m},...n)}function Uw(e,t,n,r=0){let o=new ft,s=e[0],a=Vr(s.model.familySpec);if(e.some(E=>{let R=Vr(E.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 E=e[0].model.x.length;if(e.some(A=>A.model.x.length!==E))throw new RangeError("models were not all fitted to the same size of dataset");let R=(A,T)=>A.length===T.length&&A.every((k,_)=>JSON.stringify(k)===JSON.stringify(T[_]));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[k,_]=f[T]>0?[e[T-1],A]:[A,e[T-1]],L=C=>C.filter((O,I)=>k.priorWeights[I]>0&&_.priorWeights[I]>0),q=k.model.offset.map((C,O)=>C-_.model.offset[O]),F=q.some((C,O)=>C!==0&&k.priorWeights[O]>0)?k.model.x.map((C,O)=>[...C,q[O]]):k.model.x;vs(L(F),L(_.model.x))||h.add(T)}),i==="Rao"){let A=e.map(T=>({dfRes:T.dfResidual,dev:T.deviance,fit:Nu(T),x:T.model.x,intercept:T.model.intercept}));u=l.map((T,k)=>k===0?NaN:f[k]<0?-oc(A[k],A[k-1]):oc(A[k-1],A[k]))}p=e.reduce((A,T)=>T.dfResidual<A.dfResidual?T:A)}else{let E=s,R=E.model,A=R.x.length>0?R.x[0].length-(R.intercept?1:0):0,T=R.intercept?[0]:[],k=L=>[...T,...Array.from({length:L},(q,F)=>F+T.length)],_=[];for(let L=0;L<=A;L++)L===A?_.push({dfRes:E.dfResidual,dev:E.deviance,fit:Nu(E),x:R.x,intercept:R.intercept}):L>0||i==="Rao"?_.push(zw(R,k(L),o)):_.push(void 0);l.push({name:"NULL",dfRes:E.dfNull,dev:E.nullDeviance});for(let L=1;L<=A;L++)l.push({name:`x${L}`,dfRes:_[L].dfRes,dev:_[L].dev});f=l.map((L,q)=>q===0?NaN:l[q-1].dfRes-L.dfRes),m=l.map((L,q)=>q===0?NaN:Math.max(l[q-1].dev-L.dev,0)),i==="Rao"&&(u=l.map((L,q)=>q===0?NaN:oc(_[q-1],_[q]))),p=E}e.forEach((E,R)=>{E.converged||o.add(`model ${R+1} has not converged: its deviance is not that of the maximum likelihood fit`)});let w=new ft,d=n.dispersion??Do(p,w);w.list.forEach(E=>o.add(E));let v=c?1/0:p.dfResidual,N=t?["Resid. Df","Resid. Dev","Df","Deviance"]:["Df","Deviance","Resid. Df","Resid. Dev"],b=l.map((E,R)=>t?[E.dfRes,E.dev,f[R],m[R]]:[f[R],m[R],E.dfRes,E.dev]),M=[...N],g=E=>l.map((R,A)=>{if(A===0||f[A]===0||h.has(A))return NaN;let T=Le(E[A]/d*Math.sign(f[A]),-2*r);return T<0||Number.isNaN(T)?NaN:Ww(T,Math.abs(f[A]))}),y=[];if(i==="Chisq"||i==="LRT")M.push("Pr(>Chi)"),y=[g(m)];else if(i==="Rao")M.push("Rao","Pr(>Chi)"),y=[u.map((E,R)=>h.has(R)?NaN:E),g(u)];else if(i==="F"){if(c&&o.add(`using F test with a '${a.family}' family is inappropriate`),v!==0){let E=l.map((R,A)=>{let T=Le(m[A]/f[A]/d,-2*r);return A===0||f[A]===0||h.has(A)||T<0?NaN:T});M.push("F","Pr(>F)"),y=[E,E.map((R,A)=>Number.isNaN(R)?NaN:pt(R,Math.abs(f[A]),v,!1,!1))]}}else if(i==="Cp"){let E=p.model.x.length;M.push("Cp"),y=[l.map(R=>R.dev+2*(E-R.dfRes)*d)]}let x=b.map((E,R)=>[...E,...y.map(A=>A[R])]);return{colNames:M,rowNames:l.map(E=>E.name),table:x,warnings:o.list}}function Nu(e){return{coefficients:e.coefficients,eta:e.linearPredictors,mu:e.fittedValues,deviance:e.deviance,iter:e.iter,converged:e.converged,boundary:e.boundary,rank:e.rank,qr:e.qr,used:e.weights.map(t=>t>0),workingWeights:e.weights,workingResiduals:e.residuals}}var Kw=["type"];function Mu(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,w)=>e.weights===void 0||e.weights[w]!==0?[w]:[]),c=a.map(p=>e.weights===void 0?1:Math.sqrt(e.weights[p])),i=Rt(a.map((p,w)=>s.map(d=>e.x[p][d]*c[w]))),l=Ys(e.weights===void 0?void 0:a.map(p=>D(e.weights[p],-o)),a.length),f=i.pivot.slice(0,i.rank),{qty:m}=Wr(f.map(p=>a.map((w,d)=>Tt(l[d],e.x[w][s[p]]))),a.map((p,w)=>Tt(l[w],D(e.y[p],-r)))),u=[0,0],h=0;return f.forEach((p,w)=>{p>=t.length&&(u=ue(u,_e(m[w],m[w])),h++)}),{ss:u[0],df:h}}function sc(e,t={}){if(H(t,Kw,"Anova"),typeof e!="object"||e===null||!Array.isArray(e.effects))throw new TypeError("Anova() needs a fit from lm() with a formula");let n=t.type??2,r;if(n===2||n==="II")r=2;else if(n===3||n==="III")r=3;else throw new RangeError(`invalid type (${String(n)}): use 2 or 3`);let o=e.dfResidual,s=Vs([e]),a=Ps([e],s)[0];if(o===0)throw new Error("residual df = 0");let c=ui(e,s),i=y=>{let x=c.map(([A,T])=>[D(A,-y),T]),E=x.reduce((A,[T])=>A+T,0),R=x.reduce((A,[T,k])=>A+T*k,0)/E;return Number.isFinite(E)&&Number.isFinite(R)?x.reduce((A,[T,k])=>A+T*(k-R)**2,0):NaN},l=0,f=i(0);if(Number.isFinite(f)||(l=js(c),f=i(l)),pr(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,E)=>y.has(x)?[E]:[]),p=[...new Set(e.assign)],w=[],d=[],v=y=>{w.length=0;let x=[];if(r===3){if(e.coefficients.some(E=>Number.isNaN(E)))throw new Error("there are aliased coefficients in the model");for(let E of p){let R=h(new Set([E])),A=h(new Set(p.filter(T=>T!==E)));w.push(E===0?"(Intercept)":e.termLabels[E-1]),x.push({ss:Mu(e,A,R,s,y).ss,df:R.length})}}else{let E=R=>e.factors.flatMap((A,T)=>A[R-1]>0?[T]:[]);for(let R of p){if(R===0)continue;let A=E(R),T=p.filter(L=>L!==0&&L!==R&&A.every(q=>e.factors[q][L-1]>0)),k=h(new Set(p.filter(L=>L!==R&&!T.includes(L)))),_=Mu(e,k,h(new Set([R])),s,y);w.push(e.termLabels[R-1]),x.push(_)}}return x};d=v(l),l===0&&e.weights!==void 0&&d.some(y=>!Number.isFinite(y.ss))&&(l=js(c),d=v(l));let N=y=>D(y,2*s+l),b=D(a,-l),M=b/o,g=d.map(({ss:y,df:x})=>{let E=y/x/M;return[N(y),x,E,pt(E,x,o,!1,!1)]});return g.push([N(b),o,NaN,NaN]),Z("Anova",{colNames:["Sum Sq","Df","F value","Pr(>F)"],rowNames:[...w,"Residuals"],table:g,warnings:m},e)}var na=[0,0];var ac=e=>e.map(t=>t.map(n=>n[0]));function Eu(e){let t=e[0]?.length??0;return Array.from({length:t},(n,r)=>e.map(o=>o[r]))}function Er(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]=ue(o[i],_e(s,c[i]))}),o})}function xr(e){let t=na;return e.forEach((n,r)=>t=ue(t,n[r])),t}function xu(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=it(n[i][a],n[a][a]);n[i][a]=l;for(let f=a+1;f<t;f++)n[i][f]=Pe(n[i][f],_e(l,n[a][f]))}}return{a:n,perm:r,sign:o,singular:s}}function Ru(e){let{a:t,sign:n,singular:r}=xu(e);if(r)return na;let o=[n,0];return t.forEach((s,a)=>o=_e(o,s[a])),o}function Au(e){let t=e.length,{a:n,perm:r,singular:o}=xu(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]=Pe(c[i],_e(n[i][l],c[l]));for(let i=t-1;i>=0;i--){for(let l=i+1;l<t;l++)c[i]=Pe(c[i],_e(n[i][l],c[l]));c[i]=it(c[i],n[i][i])}for(let i=0;i<t;i++)s[i][a]=c[i]}return s}var Hw=["group","idata","M","X"],Tu=(e,t)=>bt(e,t/2,2,!1,!1);function ic(e){let t=e.length,n=ct(xr(e),t);return Ru(e.map(r=>r.map(o=>it(o,n))))[0]}function cc(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=Tu(a,c),l=Tu(a,c+4);return i+s*(l-i)}var Gw=(e,t)=>Array.from({length:t},(n,r)=>e.map(o=>o[r]));function Yw(e,t,n){let r=e.length,o=v=>v.length===0?0:Rt(Array.from({length:r},(N,b)=>v.map(M=>M[b]))).rank,s=n.length===0?void 0:Rt(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=Rt(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=Cn(f.map(m),u,e.map((v,N)=>e.map(b=>b[N]))).qtv,p=Array.from({length:r},(v,N)=>h.map(b=>b[N])),w=Cn(f.map(m),u,p).qtv,d=Array.from({length:l.rank-c},(v,N)=>c+N);return d.map(v=>d.map(N=>w[v][N]))}function Xw(e,t={}){H(t,Hw,"mauchlyTest");let n=e.map(g=>Array.from(g)),r=n[0]?.length??0;if(r===0||n.some(g=>g.length!==r))throw new TypeError("y must be a matrix with at least one column");let o=t.group;if(o!==void 0&&o.length!==n.length)throw new RangeError("'group' must have one label per row of y");let s=g=>g==null||typeof g=="number"&&Number.isNaN(g),a=n.map((g,y)=>!g.some(s)&&(o===void 0||!s(o[y])));if(n.some((g,y)=>a[y]&&g.some(x=>!Number.isFinite(x))))throw new RangeError("y must not contain infinite values");let c=Te(n.filter((g,y)=>a[y])).groups,i=c.length,l=o?.filter((g,y)=>a[y]).map(mt)??c.map(()=>""),f=[...new Set(l)].sort(ln),m=c.map(g=>g.map(()=>[0,0]));for(let g of f){let y=l.flatMap((x,E)=>x===g?[E]:[]);for(let x=0;x<r;x++){let E=[0,0];for(let A of y)E=ue(E,[c[A][x],0]);let R=ct(E,y.length);for(let A of y)m[A][x]=Pe([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},(g,y)=>Array.from({length:r},(x,E)=>{let R=[0,0];for(let A of m)R=ue(R,_e(A[y],A[E]));return R})),p=t.idata??{index:Array.from({length:r},(g,y)=>y+1)},w=g=>{let y=tt(()=>Dr(g,p));if(y.x.length!==r)throw new RangeError("idata must have one row per column of y");return Gw(y.x,y.colNames.length)},d=t.M===void 0?Array.from({length:r},(g,y)=>Array.from({length:r},(x,E)=>E===y?1:0)):w(t.M),v=w(t.X??"~0"),N=Yw(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 M=ic(N);return{statistic:M,statisticName:"W",pValue:cc(M,b,u,r),method:"Mauchly's test of sphericity",warnings:[]}}var Qw=["n","d","type","sigLevel","power","alternative"],Zw=["n1","n2","d","sigLevel","power","alternative"],Jw=["h","n","sigLevel","power","alternative"],e1=["h","n1","n2","sigLevel","power","alternative"],t1=["h","n","sigLevel","power","alternative"],n1=["d","n","sigLevel","power","alternative"],r1=["n","r","sigLevel","power","alternative"],o1=["w","N","df","sigLevel","power"],s1=["k","n","f","sigLevel","power"],a1=["u","v","f2","sigLevel","power"],i1=["size"],c1={p:[.2,.5,.8],t:[.2,.5,.8],r:[.1,.3,.5],anov:[.1,.25,.4],chisq:[.1,.3,.5],f2:[.02,.15,.35]};function ku(e="p",...t){H(Cr(t),i1,"cohenES");let n=Lr(["size"],t).size??"small",r=Ie(e,["p","t","r","anov","chisq","f2"]),o=Ie(n,["small","medium","large"]);return{test:r,size:o,effectSize:c1[r][["small","medium","large"].indexOf(o)],method:"Conventional effect size from Cohen (1982)"}}function l1(e,t){if(!ra(e)||!ra(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 f1(e,t){if(e.length!==t.length)throw new RangeError("'P0' and 'P1' must have the same length");if(![...e,...t].every(ra))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+=Ot(t[r]-e[r],e[r]));return Math.sqrt(n)}function m1(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(ra)))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>=u1)o+=Ot(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 u1=2**-1022,ra=e=>e>=0&&e<=1,h1="\u2018sig.level\u2019 must be numeric in [0, 1]",p1="\u2018power\u2019 must be numeric in [0, 1]";function Zn(e,t){let n=r=>!xe(r)&&!(typeof r=="number"&&r>=0&&r<=1);if(n(e))throw new RangeError(h1);if(n(t))throw new RangeError(p1)}function Jn(e,t){if(typeof e=="number"&&Number.isNaN(e))throw new RangeError("the effect size must not be NaN");return typeof e=="string"?ku(t,e).effectSize:e}var Wo=["two.sided","less","greater"];function oa(e,t){return e==="two.sided"?t:e==="less"?[-10,5]:[-5,10]}function qu(e,t,n,r){if(r==="less")return mr(cn(n,e),e,t,!0);if(r==="greater")return mr(cn(n,e,{lowerTail:!1}),e,t,!1);let o=cn(n/2,e,{lowerTail:!1});return mr(o,e,t,!1)+mr(-o,e,t,!0)}var Su=e=>e<1e-8?pe(e,{lowerTail:!1}):pe(1-e);function Iu(e,t,n){return n==="greater"?ce(Su(t)-e,{lowerTail:!1}):n==="less"?ce(pe(t)-e):ce(Su(t/2)-e,{lowerTail:!1})+ce(pe(t/2)-e)}function d1(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*=D(r,-o),n+=o}return D(t,n)}function g1(e={}){H(e,Qw,"pwrTTest");let{n:t,power:n}=e,r=Jn(e.d,"t"),o=e.sigLevel===void 0?.05:e.sigLevel;if(fn([t,r,n,o],"exactly one of n, d, power, and sig.level must be NULL"),Zn(o,n),!xe(t)&&!(t>=2))throw new RangeError("number of observations in each group must be at least 2");let s=Ie(e.type,["two.sample","one.sample","paired"]),a=Ie(e.alternative,Wo),c=s==="two.sample"?2:1;a==="two.sided"&&!xe(r)&&(r=Math.abs(r));let i=(m,u,h)=>qu((m-1)*c,Math.sqrt(m/c)*u,h,a);if(xe(n))n=i(t,r,o);else{let m=n;xe(t)?t=Ge(u=>i(u,r,o)-m,2+1e-10,1e9):xe(r)?r=Ge(u=>i(t,u,o)-m,...oa(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 w1(e={}){H(e,Zw,"pwrT2nTest");let{n1:t,n2:n,power:r}=e,o=Jn(e.d,"t"),s=e.sigLevel===void 0?.05:e.sigLevel;if(fn([t,n,o,r,s],"exactly one of n1, n2, d, power, and sig.level must be NULL"),Zn(s,r),!xe(t)&&!(t>=2))throw new RangeError("number of observations in the first group must be at least 2");if(!xe(n)&&!(n>=2))throw new RangeError("number of observations in the second group must be at least 2");let a=Ie(e.alternative,Wo);a==="two.sided"&&!xe(o)&&(o=Math.abs(o));let c=(i,l,f,m)=>qu(i+l-2,f/Math.sqrt(1/i+1/l),m,a);if(xe(r))r=c(t,n,o,s);else{let i=r;xe(t)?t=Ge(l=>c(l,n,o,s)-i,2+1e-10,1e9):xe(n)?n=Ge(l=>c(t,l,o,s)-i,2+1e-10,1e9):xe(o)?o=Ge(l=>c(t,n,l,s)-i,...oa(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 lc(e,t,n,r,o,s){let{n:a,power:c}=t,i=Jn(e,n==="h"?"p":"t"),l=t.sigLevel===void 0?.05:t.sigLevel;if(fn([i,a,c,l],`exactly one of ${n}, n, power, and sig.level must be NULL`),Zn(l,c),!xe(a)&&!(a>=1))throw new RangeError("number of observations in each group must be at least 1");let f=Ie(t.alternative,Wo);f==="two.sided"&&!xe(i)&&(i=Math.abs(i));let m=(u,h,p)=>Iu(u*r(h),p,f);if(xe(c))c=m(i,a,l);else{let u=c;xe(i)?i=Ge(h=>m(h,a,l)-u,...oa(f,[s,10])):xe(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 b1(e={}){H(e,Jw,"pwr2pTest");let t=lc(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 v1(e={}){H(e,t1,"pwrPTest");let t=lc(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 y1(e={}){H(e,n1,"pwrNormTest");let t=lc(e.d,e,"d",n=>Math.sqrt(n),1+1e-10,1e-10);return{d:t.es,n:t.n,sigLevel:t.sigLevel,power:t.power,alternative:t.alternative,method:"Mean power calculation for normal distribution with known variance",warnings:[]}}function N1(e={}){H(e,e1,"pwr2p2nTest");let{n1:t,n2:n,power:r}=e,o=Jn(e.h,"p"),s=e.sigLevel===void 0?.05:e.sigLevel;if(fn([o,t,n,r,s],"exactly one of h, n1, n2, power, and sig.level must be NULL"),Zn(s,r),!xe(t)&&!(t>=2))throw new RangeError("number of observations in the first group must be at least 2");if(!xe(n)&&!(n>=2))throw new RangeError("number of observations in the second group must be at least 2");let a=Ie(e.alternative,Wo);a==="two.sided"&&!xe(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)=>Iu(l*Math.sqrt(c(f,m)),u,a);if(xe(r))r=i(o,t,n,s);else{let l=r;xe(o)?o=Ge(f=>i(f,t,n,s)-l,...oa(a,[1e-10,10])):xe(t)?t=Ge(f=>i(o,f,n,s)-l,2+1e-10,1e9):xe(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 M1(e={}){H(e,r1,"pwrRTest");let{n:t,power:n}=e,r=Jn(e.r,"r"),o=e.sigLevel===void 0?.05:e.sigLevel;if(fn([t,r,n,o],"exactly one of n, r, power, and sig.level must be NULL"),Zn(o,n),!xe(t)&&!(t>=4))throw new RangeError("number of observations must be at least 4");if(!xe(r)&&!(Math.abs(r)<=1))throw new RangeError("'r' must be in [-1, 1]");let s=Ie(e.alternative,Wo);s==="two.sided"&&!xe(r)&&(r=Math.abs(r));let a=(i,l,f,m)=>{let u=cn(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),w=Math.atanh(l)+l/(2*(i-1)),d=Math.atanh(p),v=ce((w-d)*Math.sqrt(i-3));return m?v+ce((-w-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(xe(n))n=c(t,r,o),s==="less"&&(r=-r);else{let i=n;if(xe(t))t=Ge(l=>c(l,r,o)-i,4+1e-10,1e9);else if(xe(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 E1(e){H(e,o1,"pwrChisqTest");let{N:t,power:n}=e,{df:r}=e,o=Jn(e.w,"chisq"),s=e.sigLevel===void 0?.05:e.sigLevel;if(fn([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(Zn(s,n),!xe(o)&&!(o>=0))throw new RangeError("w must be positive");if(!xe(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:Bt(uo(l,r,{lowerTail:!1}),r,{ncp:f,lowerTail:!1})};if(xe(n))n=a(o,t,s);else{let c=n;xe(o)?o=Ge(i=>a(i,t,s)-c,1e-10,1e9):xe(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 x1(e={}){H(e,s1,"pwrAnovaTest");let{k:t,n,power:r}=e,o=Jn(e.f,"anov"),s=e.sigLevel===void 0?.05:e.sigLevel;if(fn([t,n,o,r,s],"exactly one of k, n, f, power, and sig.level must be NULL"),Zn(s,r),!xe(o)&&!(o>=0))throw new RangeError("f must be positive");if(!xe(t)&&!(t>=2))throw new RangeError("number of groups must be at least 2");if(!xe(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=d1([c,i,l,l])),h===1/0?1:xs(m,u)?Rs(m,u,h,f):ir(Dn(f,m,u,{lowerTail:!1}),m,u,{ncp:h,lowerTail:!1})};if(xe(r))r=a(t,n,o,s);else{let c=r;xe(t)?t=Ge(i=>a(i,n,o,s)-c,2+1e-10,100):xe(n)?n=Ge(i=>a(t,i,o,s)-c,2+1e-10,1e9):xe(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 R1(e={}){H(e,a1,"pwrF2Test");let{u:t,v:n,power:r}=e,o=Jn(e.f2,"f2"),s=e.sigLevel===void 0?.05:e.sigLevel;if(fn([t,n,o,r,s],"exactly one of u, v, f2, power, and sig.level must be NULL"),Zn(s,r),!xe(o)&&!(o>=0))throw new RangeError("f2 must be positive");if(!xe(t)&&!(t>=1))throw new RangeError("degree of freedom u for numerator must be at least 1");if(!xe(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:xs(c,i)?Rs(c,i,m,f):ir(Dn(f,c,i,{lowerTail:!1}),c,i,{ncp:m,lowerTail:!1})};if(xe(r))r=a(t,n,o,s);else{let c=r;xe(t)?t=Ge(i=>a(i,n,o,s)-c,1+1e-10,100):xe(n)?n=Ge(i=>a(t,i,o,s)-c,1+1e-10,1e9):xe(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 A1=["id","dv","between","within","type","correction","es","observed"],sa=e=>e===void 0?[]:typeof e=="string"?[e]:[...e],T1=1e-12,_u=1e-24;function Lu(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 zo=e=>typeof e=="boolean"?e?"TRUE":"FALSE":mt(e);function S1(e){let t=[...new Set(e)];return t.every(n=>typeof n=="number")?t.sort((n,r)=>n-r).map(zo):[...new Set(t.map(zo))].sort(ln)}var Cu=(e,t)=>t.split(":").includes(e);function Ou(e,t,n,r,o){let s=l=>r.some(f=>Cu(f,l));for(let l of r)if(!e.some(f=>Cu(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 k1(e,t){H(t,A1,"aovEz");let n=sa(t.between),r=sa(t.within);if(n.length===0&&r.length===0)throw new Error("Either between or within need to be non-NULL!");for(let[W,U]of[["between",n],["within",r]]){let z=U.filter((ne,le)=>U.indexOf(ne)!==le);if(z.length>0)throw new RangeError(`duplicated factor names in '${W}': ${[...new Set(z)].join(", ")}`)}let o=n.filter(W=>r.includes(W));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=sa(t.es??"ges");for(let W of c)if(W!=="ges"&&W!=="pes")throw new RangeError(`invalid 'es': ${String(W)}`);let i=sa(t.observed),l=[],f=W=>W==null||typeof W=="number"&&Number.isNaN(W),m=Lu(e,t.dv),u=[t.id,...n,...r].map(W=>Lu(e,W));for(let W of u)if(W.length!==m.length)throw new RangeError("the columns must have the same length");let h=m.map((W,U)=>u.every(z=>!f(z[U]))),p=W=>W.filter((U,z)=>h[z]),w=St(p(u[0])).map(zo),d=p(m).map(W=>{if(f(W))return NaN;if(typeof W!="number")throw new TypeError(`the response '${t.dv}' must be numeric`);if(!Number.isFinite(W))throw new RangeError(`the response '${t.dv}' must be finite`);return W}),v=n.map((W,U)=>St(p(u[1+U]))),N=r.map((W,U)=>St(p(u[1+n.length+U]))),b=w.length;for(let W of[d,...v,...N])if(W.length!==b)throw new RangeError("the columns must have the same length");let M=v.map(W=>S1(W)),g=v.map(W=>W.map(zo)),y=N.map(W=>W.map(zo)),x=y.map(W=>[...new Set(W)]);M.forEach((W,U)=>{if(W.length<2)throw new Error(`Factor "${n[U]}" consists of one level only. Remove factor from model?`)}),x.forEach((W,U)=>{if(W.length<2)throw new Error(`Factor "${r[U]}" consists of one level only. Remove factor from model?`)});let E=[...new Set(w)],R=new Map,A=new Set;if(w.forEach((W,U)=>{let z=JSON.stringify(g.map(le=>le[U])),ne=R.get(W);ne===void 0?R.set(W,z):ne!==z&&A.add(W)}),A.size>0)throw new Error(`Following ids are in more than one between subjects condition:
8
+ ${E.filter(W=>A.has(W)).join(", ")}`);let T=[[]];for(let W of x)T=T.flatMap(U=>W.map(z=>[...U,z]));let k=W=>JSON.stringify(y.map(U=>U[W])),_=new Map(T.map((W,U)=>[JSON.stringify(W),U])),L=new Set(w.map((W,U)=>k(U)));if(T.some(W=>!L.has(JSON.stringify(W))))throw new Error("Empty cells in within-subjects design (i.e., bad data structure).");let q=new Map,F=new Set;w.forEach((W,U)=>{let z=q.get(W);z===void 0&&q.set(W,z=new Set),z.add(k(U)),Number.isNaN(d[U])&&F.add(W)});let C=E.filter(W=>F.has(W)||q.get(W).size<T.length),O=new Set(C),I=Te([d.filter((W,U)=>!O.has(w[U]))]).k,V=I===0?d:d.map(W=>D(W,-I)),S=W=>D(W,2*I),P=T.length,G=new Map;if(w.forEach((W,U)=>{let z=G.get(W);z===void 0&&G.set(W,z={s:T.map(()=>[0,0]),n:T.map(()=>0)});let ne=_.get(k(U));z.s[ne]=ue(z.s[ne],[V[U],0]),z.n[ne]++}),[...G.values()].some(W=>W.n.some(U=>U>1))&&l.push("More than one observation per design cell, aggregating data using `fun_aggregate = mean`.\nTo turn off this warning, pass `fun_aggregate = mean` explicitly."),C.length>0){let W=[...C].sort(ln),U=W.length>=10?`${W.slice(0,10).join(", ")}, ... [showing first 10 only]`:W.join(", ");l.push(`Missing values for ${C.length} ID(s), which were removed before analysis:
9
+ ${U}`)}let Q=E.filter(W=>!C.includes(W)),te=new Map;w.forEach((W,U)=>te.has(W)||te.set(W,U));let $=Q.map(W=>{let U=G.get(W);return U.s.map((z,ne)=>ct(z,U.n[ne]))}),J={};n.forEach((W,U)=>J[W]=Q.map(z=>g[U][te.get(z)]));let ee=Object.fromEntries(n.map(W=>[W,"contr.sum"])),Se=Object.fromEntries(n.map((W,U)=>[W,M[U]])),re="Rank deficient model matrix; insufficient data to estimate full model.";if(P===1)return Z("aovEz",j());return Z("aovEz",K());function j(){let W="y";for(;n.includes(W);)W+="_";let U={...J,[W]:$.map(fe=>fe[0][0])},z=tt(()=>Oo(`${W} ~ ${n.length>0?n.join(" * "):"1"}`,U,{contrasts:ee,levels:Se}));if(z.rank<z.coefNames.length)throw new Error(re);if(z.dfResidual<=0)throw new RangeError(`no residual degrees of freedom: ${$.length} subject(s) for ${z.rank} parameter(s)`);let ne=tt(()=>sc(z,{type:s}));l.push(...ne.warnings.filter(fe=>!l.includes(fe)));let le=z.dfResidual,Re=ne.rowNames.flatMap((fe,ke)=>{if(fe==="(Intercept)"||fe==="Residuals")return[];let[de,Oe,Ee,Ve]=ne.table[ke];return[{name:fe,ss:de,numDf:Oe,errSS:z.rss,denDf:le,f:Ee,p:Ve}]}),Ce=ne.colNames.indexOf("Sum Sq"),be={anova:Ou(Re,new Map([[0,z.rss]]),c,i,S),univariate:{colNames:ne.colNames,rowNames:ne.rowNames,table:ne.table.map(fe=>fe.map((ke,de)=>de===Ce?S(ke):ke))},within:r,warnings:l};return jt.set(be,{colNames:ne.colNames,rowNames:ne.rowNames,table:ne.table}),be}function K(){let W=Q.length,U=$.reduce((Ne,ye)=>ye.reduce((Me,X)=>Me+X[0]*X[0],Ne),0),z=n.length>0?tt(()=>Dr(`~ ${n.join(" * ")}`,J,{contrasts:ee,levels:Se})):{colNames:["(Intercept)"],assign:[0],x:Q.map(()=>[1]),termLabels:[],factors:[]},ne=z.colNames.length;if(W===0||Rt(z.x).rank<ne)throw new Error(re);let le=W-ne;if(le<=0)throw new RangeError(`no residual degrees of freedom: ${W} subject(s) for ${ne} between-subject parameter(s)`);let Re=[0,...z.termLabels.map((Ne,ye)=>ye+1)],Ce=["(Intercept)",...z.termLabels],be=Ne=>z.assign.flatMap((ye,Me)=>Ne.includes(ye)?[Me]:[]),fe=Ne=>z.factors.flatMap((ye,Me)=>ye[Ne-1]>0?[Me]:[]),ke=Ne=>z.x.map(ye=>[ye[Ne],0]),de=new Array(W).fill([0,0]),Oe=(Ne,ye,Me)=>{let{qtv:X}=Cn([...Ne,...ye].map(ke),de,Me);return X.map(ve=>ve.slice(Ne.length,Ne.length+ye.length))},Ee=Ne=>{let{qtv:ye}=Cn(z.colNames.map((Me,X)=>ke(X)),de,Ne);return ye.map(Me=>Me.slice(ne))},Ve=Object.fromEntries(r.map((Ne,ye)=>[Ne,T.map(Me=>Me[ye])])),Fe=tt(()=>Dr(`~ ${r.join(" * ")}`,Ve,{contrasts:Object.fromEntries(r.map(Ne=>[Ne,"contr.sum"])),levels:Object.fromEntries(r.map((Ne,ye)=>[Ne,x[ye]]))})),Ue=[0,...Fe.termLabels.map((Ne,ye)=>ye+1)],$e=["(Intercept)",...Fe.termLabels],Je=Ne=>Ne.map(ye=>Ne.map(Me=>{let X=[0,0];return ye.forEach((ve,ae)=>X=ue(X,_e(ve,Me[ae]))),X})),Ke=[],Xe=[],he=[],oe=[],we=[],ge=[],ie=[],et=new Map,Y=!1;for(let Ne of Ue){let ye=Fe.assign.flatMap((We,ze)=>We===Ne?[ze]:[]),Me=ye.length,X=ye.map(We=>$.map(ze=>ze.reduce((Mt,Et,wn)=>ue(Mt,Tt(Et,Fe.x[wn][We])),[0,0]))),ve=ye.map(We=>ye.map(ze=>[Fe.x.reduce((Mt,Et)=>Mt+Et[We]*Et[ze],0),0])),ae=Au(ve),Ae=Je(Ee(X)),Be=Er(Ae,ae),je=xr(Be)[0],nt=NaN,ut=NaN,It=NaN,$t=NaN,Fn=!1;if(Me>=2){let We=$e[Ne],ze=Tl(ac(ae)).map(Wt=>Wt.map(Zt=>[Zt,0])),Mt=ac(Er(Er(ze,Ae),Eu(ze))),Et=Vt(Mt,{symmetric:!0,onlyValues:!0}).values,wn=Et.reduce((Wt,Zt)=>Math.max(Wt,Zt),0),Ft=Math.max(wn*T1,U*_u),kt=xr(Be),ht=xr(Er(Be,Be));wn>U*_u&&ht[0]>0&&Number.isFinite(ht[0])?(Fn=!0,nt=it(_e(kt,kt),Tt(ht,Me))[0],le-Me*nt>0?ut=((le+1)*Me*nt-2)/(Me*(le-Me*nt)):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)`),Fn&&Et.every(Wt=>Wt>Ft)?(It=ic(Er(ae,Ae)),$t=cc(It,Me,le,P)):Fn&&l.push(`The error SSP matrix of ${We} is singular: no Mauchly test (the epsilons are still defined)`)}for(let We=0;We<Re.length;We++){let ze=Re[We],Mt;if(s===3)Mt=Oe(be(Re.filter(_t=>_t!==ze)),be([ze]),X);else if(ze===0)Mt=Oe([],be([0]),X);else{let _t=fe(ze),Yt=Re.filter(B=>B!==0&&B!==ze&&_t.every(se=>z.factors[se][B-1]>0));Mt=Oe(be(Re.filter(B=>B!==ze&&!Yt.includes(B))),be([ze]),X)}let Et=xr(Er(Je(Mt),ae))[0],Ft=(ze===0?1:be([ze]).length)*Me,kt=le*Me,ht=Et/Ft/(je/kt),Wt=pt(ht,Ft,kt,!1,!1),Zt=Ne===0?Ce[We]:ze===0?$e[Ne]:`${Ce[We]}:${$e[Ne]}`;Ke.push([S(Et),Ft,S(je),kt,ht,Wt]),Xe.push([Et,Ft,je,kt,ht,Wt]),he.push(Zt),et.set(Ne,je);let Gt={name:Zt,ss:Et,numDf:Ft,errSS:je,denDf:kt,f:ht,p:Wt};if(Fn){let _t=Math.min(1,ut),Yt=pt(ht,nt*Ft,nt*kt,!1,!1),B=pt(ht,_t*Ft,_t*kt,!1,!1);oe.push([nt,Yt,ut,B]),we.push([It,$t]),ge.push(Zt),a==="GG"?Gt={...Gt,numDf:nt*Ft,denDf:nt*kt,p:Yt}:a==="HF"&&(ut>1&&(Y=!0),Gt=Number.isNaN(ut)?{...Gt,numDf:nt*Ft,denDf:nt*kt,p:Yt}:{...Gt,numDf:_t*Ft,denDf:_t*kt,p:B})}Ne===0&&ze===0||ie.push(Gt)}}Y&&l.push("HF eps > 1 treated as 1");let me={anova:Ou(ie,et,c,i,S),univariate:{colNames:["Sum Sq","num Df","Error SS","den Df","F value","Pr(>F)"],rowNames:he,table:Ke},within:r,warnings:l};return oe.length>0&&(me.corrections={colNames:["GG eps","Pr(>F[GG])","HF eps","Pr(>F[HF])"],rowNames:ge,table:oe},me.sphericity={colNames:["Test statistic","p-value"],rowNames:[...ge],table:we}),jt.set(me,{colNames:me.univariate.colNames,rowNames:he,table:Xe}),me}}function Yr(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=st(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 q1=["rho"],I1=new Set(["group","start","id","weights","ctype","stype","robust","rmean","confType","confInt"]),mc=e=>e==null||typeof e=="number"&&Number.isNaN(e);function _1(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&&mc(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=St(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=Yr(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 L1(e,t,n,r={}){H(r,q1,"survdiff");let{rho:o=0}=r;if(!Number.isFinite(o))throw new RangeError("'rho' must be a finite number");let{obs:s,labels:a}=_1(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 M=s[b].time,g=b,y=new Array(c).fill(0),x=new Array(c).fill(0),E=0;for(;g<s.length&&s[g].time===M;){let R=s[g];x[R.group]++,R.event&&(y[R.group]++,E++),g++}if(E>0){let R=o===0?1:p**o;for(let A=0;A<c;A++)l[A]+=R*y[A],f[A]+=R*E*u[A]/h;if(h>1){let A=R*R*E*(h-E)/(h*(h-1));for(let T=0;T<c;T++)for(let k=0;k<c;k++)m[T][k]+=A*u[T]*((T===k?1:0)-u[k]/h)}p*=1-E/h}for(let R=0;R<c;R++)u[R]-=x[R];h-=g-b,b=g}let w=f.map((b,M)=>b>0?M:-1).filter(b=>b>=0),d=0,v=Math.max(w.length-1,0);if(w.length>=2){let b=w.slice(1),M=b.map(y=>l[y]-f[y]),g=b.map(y=>b.map(x=>m[y][x]));try{let y=ot(g,M);d=M.reduce((x,E,R)=>x+E*y[R],0)}catch{({chisq:d,df:v}=C1(g,M))}}let N=v===0?NaN:Bt(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 C1(e,t){let n=Vt(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 fc(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 Xt(e[r],e[s])}return e[r]}function er(e,t,n={}){H(n,I1,"survfit");let{group:r,start:o,id:s,weights:a,ctype:c=1,stype:i=1,robust:l=!1,rmean:f="common",confType:m="log",confInt:u=.95}=n;if(typeof m!="string")throw new TypeError("confType must be a string");let h=["log","log-log","plain","none","logit","arcsin"],p=typeof m=="string"?h.filter(S=>S.startsWith(m)):[],w=h.includes(m)?m:p.length===1?p[0]:m;if(typeof c!="number")throw new TypeError("ctype must be a number");if(typeof 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(!h.some(S=>S===w))throw new RangeError("'arg' should be one of \u201Clog\u201D, \u201Clog-log\u201D, \u201Cplain\u201D, \u201Cnone\u201D, \u201Clogit\u201D, \u201Carcsin\u201D");if(typeof u!="number")throw new TypeError("confInt must be a number");if(!(u>0&&u<1))throw new RangeError("confidence intervals must be between 0 and 1");if(e.length!==t.length)throw new RangeError("'time' and 'status' must have the same length");if(r!==void 0&&r.length!==e.length)throw new RangeError("'group' must have the same length as 'time'");if(o!==void 0&&o.length!==e.length)throw new RangeError("'start' must have the same length as 'time'");if(s!==void 0&&s.length!==e.length)throw new RangeError("'id' must have the same length as 'time'");if(s!==void 0&&o===void 0)throw new RangeError("'id' requires 'start'");if(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'");if(typeof l!="boolean")throw new TypeError("'robust' must be a boolean");if(typeof f!="number"&&typeof f!="string")throw new TypeError("rmean must be a number or string");if(typeof f=="number"&&!Number.isFinite(f))throw new RangeError("'rmean' must be finite");if(typeof f!="number"&&f!=="none"&&f!=="common"&&f!=="individual")throw new RangeError("'arg' should be one of \u201Cnone\u201D, \u201Ccommon\u201D, \u201Cindividual\u201D");let d=[];for(let S=0;S<e.length;S++){let P=a?.[S];if(a&&P!=null&&typeof P!="number")throw new TypeError("weights must contain numbers");if(a&&(P==null||Number.isNaN(P)))continue;let G=P??1;if(!Number.isFinite(G))throw new RangeError("weights must be finite");if(G<0)throw new RangeError("weights must be non-negative");if(!Number.isSafeInteger(G))throw new RangeError("weights must be non-negative safe integers");if(e[S]!=null&&typeof e[S]!="number")throw new TypeError("'time' must contain numbers");if(t[S]!=null&&typeof t[S]!="number")throw new TypeError("'status' must contain numbers");if(o!==void 0&&o[S]!=null&&typeof o[S]!="number")throw new TypeError("'start' must contain numbers");if(!(e[S]==null||t[S]==null||Number.isNaN(e[S])||Number.isNaN(t[S])||o!==void 0&&(o[S]==null||Number.isNaN(o[S]))||r!==void 0&&mc(r[S])||s!==void 0&&mc(s[S]))){if(o!==void 0&&!(o[S]<e[S]))throw new RangeError("stop time must be > start time");if(t[S]!==0&&t[S]!==1)throw new RangeError("'status' must be 0 (censored) or 1 (event)");if(e[S]===-1/0||e[S]===1/0&&t[S]===1)throw new RangeError("event times must be finite");d.push(S)}}if(d.length===0)throw new RangeError("no observations without missing values");let v=r===void 0?[]:St(r),N=s===void 0?[]:Array.from(s),b=r===void 0?["all"]:Array.from(new Set(d.map(S=>v[S]))).sort((S,P)=>typeof S=="number"&&typeof P=="number"?S-P:String(S)<String(P)?-1:String(S)>String(P)?1:0).map(S=>`group=${mt(S)}`),M=new Map;r!==void 0&&Array.from(new Set(d.map(S=>v[S]))).sort((S,P)=>typeof S=="number"&&typeof P=="number"?S-P:String(S)<String(P)?-1:String(S)>String(P)?1:0).forEach((S,P)=>M.set(S,P));let g=new Map;s!==void 0&&Array.from(new Set(d.map(S=>N[S]))).forEach((S,P)=>g.set(S,P));let y=d.flatMap(S=>o===void 0?[e[S]]:[o[S],e[S]]),x=Yr(y),E=new Map,R=new Map;d.forEach((S,P)=>{if(o===void 0)R.set(S,x[P]);else if(E.set(S,x[2*P]),R.set(S,x[2*P+1]),!(E.get(S)<R.get(S)))throw new Error("aeqSurv exception, an interval has effective length 0")});let A=d.map(S=>({time:R.get(S),start:o===void 0?-1/0:E.get(S),event:t[S]===1,group:r===void 0?0:M.get(v[S]),id:s===void 0?S:g.get(N[S]),weight:a?.[S]??1})).sort((S,P)=>S.time-P.time),T=s===void 0?void 0:new Map;if(T)for(let S of A){let P=T.get(S.id)??[];P.push(S),T.set(S.id,P)}let k=b,_=A.map(S=>S.time),L=Math.min(..._),q=Math.min(0,L);if(typeof f=="number"&&f<=q)throw new RangeError("'rmean' must be greater than the integration origin");let F=(1-u)/2,C=F<1e-8?pe(F,{lowerTail:!1}):pe(1-F),O=[],I={strata:[],time:[],nRisk:[],nEvent:[],nCensor:[],surv:[],stdErr:O,stdError:O,lower:[],upper:[],cumhaz:[],stdChaz:[],logse:!l||c===2,n:[],records:[],nMax:[],nStart:[],events:[],rmean:[],seRmean:[],median:[],medianRows:k,table:{colNames:[],rowNames:k,table:[]},warnings:[]};a!==void 0&&(I.counts={nRisk:[],nEvent:[],nCensor:[]});let V=[];for(let S=0;S<k.length;S++){let P=A.filter(he=>he.group===S);if(!P.some(he=>he.weight>0))throw new Error("a group has no positive weight");let G=I.time.length,Q=P.length,te=Math.max(...P.map(he=>he.time)),$=0,J=0,ee=1,Se=0,re=0,j=0;if(o===void 0){let he=0;for(let oe of P)if(he+=oe.weight,!Number.isSafeInteger(he))throw new RangeError("total case weight per group must be a safe integer");$=he,J=P.length}let K=[],W=[],U=[],z=s===void 0?void 0:new Map;if(z)for(let he of P){let oe=z.get(he.id)??[];oe.push(he),z.set(he.id,oe)}for(let he=0;he<P.length;){let oe=P[he].time,we=he,ge=0,ie=0,et=0,Y=J;for(;we<P.length&&P[we].time===oe;)we++;let me=P.slice(he,we);if(o!==void 0){let We=P.filter(ze=>ze.start<oe&&ze.time>=oe);if($=We.reduce((ze,Mt)=>ze+Mt.weight,0),J=We.length,Y=J,!Number.isSafeInteger($))throw new RangeError("total case weight at risk must be a safe integer")}let Ne=me.filter(We=>We.event),ye=me.filter(We=>!We.event&&(T===void 0||!(T.get(We.id)??[]).some(ze=>ze.start===oe&&ze.time>oe)));for(let We of Ne)ge+=We.weight,ie++;for(let We of ye)et+=We.weight;let Me=Ne.length,X=ye.length;if(s!==void 0&&Ne.length===0&&ye.length===0){he=we;continue}let ve=0,ae=0;if(ge>0){if(c===1)ve=ge/$,ae=ge/($*$);else for(let We=0;We<ie;We++){let ze=ge/ie,Mt=$-We*ze;ve+=ze/Mt,ae+=ze/(Mt*Mt)}Se+=ve,j+=ae,i===1?(ee*=1-ge/$,re+=ge<$?ge/($*($-ge)):1/0):ee=Math.exp(-Se),U.push({t:oe,d:ge,n:$,e:ie,S:ee})}let Ae=Math.sqrt(i===1?re:j),Be=0,je=0;if(l){let We=0,ze=0,Mt=z===void 0?P:Array.from(z.values(),Et=>Et[0]);for(let Et of Mt){let wn=z?.get(Et.id)??[Et],Ft=0,kt=0;for(let ht of U){if(ht.t>oe)break;let Wt=wn.some(_t=>_t.start<ht.t&&_t.time>=ht.t),Zt=wn.some(_t=>_t.time===ht.t&&_t.event),Gt=ht.d/ht.n;if(c===1)kt+=(Number(Zt)-Number(Wt)*Gt)/ht.n;else{let _t=0,Yt=0;for(let B=0;B<ht.e;B++){let se=ht.d/ht.e,qe=ht.n-B*se;_t+=(qe+B*se)/(ht.e*qe*qe),Yt-=se/(qe*qe)}kt+=_t*Number(Zt)+Yt*Number(Wt)}i===1&&Gt<1&&(Ft-=ee*(Number(Zt)-Number(Wt)*Gt)/((1-Gt)*ht.n))}We+=(Et.weight*Ft)**2,ze+=(Et.weight*kt)**2}Be=Math.sqrt(We),je=Math.sqrt(ze)}let nt=l?c===2?i===1?Be:je:i===1?ee===0?NaN:Be/ee:je:Ae,ut=l?c===2?ee*(i===1?Be:je):i===1?Be:ee*je:ee*Ae,It=NaN,$t=NaN;if(w!=="none")if(w==="log")It=ee===0?ut===0?0:NaN:ee*Math.exp(-C*nt),$t=ee===0?ut===0?0:NaN:Math.min(1,ee*Math.exp(C*nt));else if(w==="log-log")if(ee===0||ee===1)It=ut===0?ee:NaN,$t=It;else{let We=Math.log(-Math.log(ee));It=Math.exp(-Math.exp(We-C*nt/Math.log(ee))),$t=Math.exp(-Math.exp(We+C*nt/Math.log(ee)))}else if(w==="plain")ee===0&&ut===1/0?(It=NaN,$t=NaN):(It=Math.max(0,ee-C*ut),$t=Math.min(1,ee+C*ut));else if(w==="logit")if(ee===0||ee===1)It=ut===0?ee:NaN,$t=It;else{let We=Math.log(ee/(1-ee)),ze=C*ut/(ee*(1-ee));It=1-1/(1+Math.exp(We-ze)),$t=1-1/(1+Math.exp(We+ze))}else if(ee>0&&ee<1){let We=Math.asin(Math.sqrt(ee)),ze=C*ut/(2*Math.sqrt(ee*(1-ee)));It=Math.sin(Math.max(0,We-ze))**2,$t=Math.sin(Math.min(Math.PI/2,We+ze))**2}else It=NaN,$t=NaN;let Fn=$;I.strata.push(k[S]),I.time.push(oe),I.nRisk.push(Fn),I.nEvent.push(ge),I.nCensor.push(et),I.surv.push(ee),I.cumhaz.push(Se),I.stdChaz.push(l?je:Math.sqrt(j)),I.stdErr.push(l?c===2?ee*(i===1?Be:je):i===1?Be:ee*je:ee*Ae),I.lower.push(It),I.upper.push($t),I.counts&&(I.counts.nRisk.push(Y),I.counts.nEvent.push(Me),I.counts.nCensor.push(X)),o===void 0&&($-=ge+et,J-=we-he),he=we}let ne=I.time.slice(G),le=I.surv.slice(G),Re=I.lower.slice(G),Ce=I.upper.slice(G);I.median.push([fc(ne,le),w==="none"?NaN:fc(ne,Re),w==="none"?NaN:fc(ne,Ce)]),I.n.push(Q),I.records.push(Q);let be=I.nRisk.slice(G);I.nMax.push(Math.max(...be)),I.nStart.push(be[0]??0),I.events.push(P.reduce((he,oe)=>he+(oe.event?oe.weight:0),0));let fe=f==="none"?NaN:f==="common"?Math.max(..._):f==="individual"?te:f;if(f!=="none"&&!Number.isFinite(fe))throw new RangeError("'rmean' must be finite");let ke=P.filter(he=>Number.isFinite(he.time)).map(he=>Math.abs(he.time)),de=Number.isFinite(fe)?Math.max(Math.abs(fe),Math.abs(q),...ke):Math.max(Math.abs(q),...ke),Oe=de===0?0:Math.max(-1074,Math.min(1023,Math.floor(Math.log2(de)))),Ee=de===0?1:2**Oe,Ve=Number.isFinite(fe)&&!Number.isFinite(fe-q),Fe=he=>{let oe=1;for(let we of U)if(we.t<=he)oe=we.S;else break;return oe},Ue=he=>{let oe=1;for(let we of U)if(we.t/Ee<=he)oe=we.S;else break;return oe},$e=kn();if(Ve){let he=Array.from(new Set([q/Ee,...U.map(oe=>oe.t/Ee).filter(oe=>oe>q/Ee&&oe<fe/Ee),fe/Ee])).sort((oe,we)=>oe-we);for(let oe=0;oe<he.length-1;oe++){let we=he[oe],ge=he[oe+1];an($e,Ue(we)*(ge-we),he.length+1)}}else{let he=Array.from(new Set([q,...U.map(oe=>oe.t).filter(oe=>oe>q&&oe<fe),fe])).sort((oe,we)=>oe-we);for(let oe=0;oe<he.length-1;oe++){let we=he[oe],ge=he[oe+1];an($e,Fe(we)*(ge-we),he.length+1)}}let Je=qn($e)*(Ve?Ee:1),Ke=kn();if(Number.isFinite(fe))for(let he of U){if(he.t>fe||he.n<=he.d)continue;let oe=kn();if(Ve){let ge=Array.from(new Set([he.t/Ee,...U.map(ie=>ie.t/Ee).filter(ie=>ie>he.t/Ee&&ie<fe/Ee),fe/Ee])).sort((ie,et)=>ie-et);for(let ie=0;ie<ge.length-1;ie++)an(oe,Ue(ge[ie])*(ge[ie+1]-ge[ie]),ge.length+1)}else{let ge=Array.from(new Set([he.t,...U.map(ie=>ie.t).filter(ie=>ie>he.t&&ie<fe),fe])).sort((ie,et)=>ie-et);for(let ie=0;ie<ge.length-1;ie++)an(oe,Fe(ge[ie])*((ge[ie+1]-ge[ie])/Ee),ge.length+1)}let we=qn(oe);an(Ke,we*we*he.d/(he.n*(he.n-he.d)),U.length+1)}let Xe=qn(Ke);I.rmean.push(f==="none"?NaN:Je),I.seRmean.push(f==="none"?NaN:Math.sqrt(Xe)*Ee),V.push(f==="none"?w==="none"?[Q,I.nMax[S],I.nStart[S],I.events[S],I.median[S][0]]:[Q,I.nMax[S],I.nStart[S],I.events[S],I.median[S][0],I.median[S][1],I.median[S][2]]:w==="none"?[Q,I.nMax[S],I.nStart[S],I.events[S],Je,Math.sqrt(Xe)*Ee,I.median[S][0]]:[Q,I.nMax[S],I.nStart[S],I.events[S],Je,Math.sqrt(Xe)*Ee,I.median[S][0],I.median[S][1],I.median[S][2]])}return I.table={colNames:[...f==="none"?["records","n.max","n.start","events","median"]:["records","n.max","n.start","events","rmean","se(rmean)","median"],...w==="none"?[]:[`${u}LCL`,`${u}UCL`]],rowNames:k,table:V},Z("survfit",I)}var O1=["tau","covariates","covariateNames","confLevel"];function Bo(e){return 2*ce(-Math.abs(e))}function Vu(e,t,n){return[e-n*t,e+n*t]}function Fu(e,t,n){return{estimate:e,se:t,confInt:Vu(e,t,n)}}function F1(e,t,n,r){return{estimate:e,confInt:Vu(e,t,n),pValue:Bo(r)}}function Du(e,t){let n=er(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=kn();for(let l=0;l<n.time.length&&n.time[l]<t;l++){let f=n.time[l];an(c,(1-a)*(f-s),n.time.length+1),a=n.surv[l],s=f}an(c,(1-a)*(t-s),n.time.length+1);let i=qn(c);return{rmst:{estimate:r,se:o},rmtl:{estimate:i,se:o}}}function uc(e,t,n,r){if(n==="difference"){let l=e.estimate-t.estimate,f=Math.hypot(e.se,t.se);return F1(l,f,r,l/f)}let o=e.estimate,s=t.estimate;if(o===0||s===0)return{estimate:o===0?s===0?NaN:0:1/0,confInt:[NaN,NaN],pValue:NaN};if(!(o>0&&s>0))return{estimate:NaN,confInt:[NaN,NaN],pValue:NaN};let 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:Bo(c/i)}}function D1(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=er(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 hc(e,t,n,r,o,s){let a=e.map(L=>[L.arm,...L.x]),c=a[0].map((L,q)=>{let F=a.reduce((O,I)=>Math.max(O,Math.abs(I[q])),0);if(F===0)return 1;let C=Math.floor(Math.log2(F));return C>500||C<-500?2**C:1}),i=a.map(L=>L.map((q,F)=>q/c[F])),l=e.map((L,q)=>[1,...i[q]]),f=[1,...c],m=D1(e),u=r?Ki(i,n,{family:"poisson",weights:m}):Oo(i,n,{weights:m});if(r&&!u.converged)throw new Error("the adjusted RMST Poisson model did not converge");let p=u.coefficients.map((L,q)=>L/f[q]),w=u.fittedValues;if(p.some(L=>!Number.isFinite(L))||w.some(L=>!Number.isFinite(L)))throw new Error("the adjusted RMST model is singular or has non-finite fitted values");let d=l.map((L,q)=>L.map(F=>m[q]*F*(n[q]-w[q]))),v=d.map(L=>L.slice());for(let L of[0,1]){let q=e.flatMap((C,O)=>C.arm===L?[O]:[]),F=er(q.map(C=>e[C].time),q.map(C=>e[C].status===0?1:0),{rmean:"none"});for(let C=0;C<F.time.length;C++){let O=F.time[C],I=F.nEvent[C];if(!(O<t)||I===0)continue;let V=F.nRisk[C];if(!(V>0))continue;let S=new Array(l[0].length).fill(0);for(let G=0;G<S.length;G++){let Q=kn();for(let te of q)e[te].time>=O&&an(Q,d[te][G],q.length);S[G]=qn(Q)}let P=I/V;for(let G of q){let Q=e[G],te=Q.time===O&&Q.status===0,$=Q.time>=O;for(let J=0;J<S.length;J++)v[G][J]+=S[J]/V*(Number(te)-($?P:0))}}}let N=l.map((L,q)=>L.map(F=>F*Math.sqrt(r?w[q]:1))),b=_a(N),M=_a(v),g=ot(b),x=La(La(g,M),g).map((L,q)=>L.map((F,C)=>F/f[q]/f[C])),E=x.map((L,q)=>Math.sqrt(Math.max(0,L[q]))),R=p.map((L,q)=>L/E[q]),A=R.map(Bo),T=p.map((L,q)=>L-s*E[q]),k=p.map((L,q)=>L+s*E[q]),_=r?[T.map(Math.exp),k.map(Math.exp)]:[T,k];return{coef:p,cov:x,table:{names:["intercept","arm",...o],estimate:p,se:E,z:R,pValue:A,confInt:_}}}function V1(e,t,n,r={}){if(H(r,O1,"rmst2"),e.length!==t.length||e.length!==n.length)throw new RangeError("'time', 'status' and 'arm' must have the same length");if(r.tau!==void 0&&typeof r.tau!="number")throw new TypeError("'tau' must be a number");if(r.confLevel!==void 0&&typeof r.confLevel!="number")throw new TypeError("'confLevel' must be a number");if(r.covariateNames!==void 0&&!Array.isArray(r.covariateNames))throw new TypeError("'covariateNames' must be an array of strings");let o=r.confLevel??.95;if(!(o>0&&o<1))throw new RangeError("'confLevel' must be between 0 and 1");let s=e.reduce(($,J)=>Number.isFinite(J)?Math.max($,Math.abs(J)):$,0),a=s>2**500?2**Math.floor(Math.log2(s)):1,i=e.every($=>!Number.isFinite($)||$===0||$/a!==0)&&(r.tau===void 0||r.tau===0||r.tau/a!==0)?a:1,l=$=>$/i,f=Array.from(t),m=new Set;for(let $=0;$<f.length;$++){let J=n[$]===!0?1:n[$]===!1?0:n[$];(J===0||J===1)&&f[$]===2&&m.add(J)}let u=r.covariates?.[0],h=r.covariates!==void 0&&typeof u=="number";if(r.covariates!==void 0&&r.covariates.length>0&&!h&&!Array.isArray(u))throw new TypeError("'covariates' must be a numeric vector or a matrix of numeric rows");let p=r.covariates===void 0?void 0:h?r.covariates.map($=>[$]):r.covariates.map($=>Array.from($));if(p&&p.length!==e.length)throw new RangeError("'covariates' must have one row per observation");if(p&&p.some($=>$.length!==p[0].length))throw new RangeError("'covariates' must have the same number of columns in every row");if(p&&p.some($=>$.some(J=>typeof J!="number")))throw new TypeError("'covariates' must contain numeric values");if(p&&p.some($=>$.some(J=>!Number.isFinite(J)&&!Number.isNaN(J))))throw new RangeError("'covariates' must be finite");let w=[],d=[],v=[],N=[];for(let $=0;$<e.length;$++){if(e[$]==null||Number.isNaN(e[$])||f[$]==null||Number.isNaN(f[$])||n[$]==null||Number.isNaN(n[$]))throw new Error("missing value where TRUE/FALSE needed");if(typeof e[$]!="number")throw new TypeError("'time' must contain numbers");if(!Number.isFinite(e[$]))throw new RangeError("'time' must be finite");if(typeof n[$]!="number"&&typeof n[$]!="boolean")throw new TypeError("'arm' must contain numbers or booleans");let J=n[$]===!0?1:n[$]===!1?0:n[$];if(J!==0&&J!==1){v.push(l(e[$]));continue}if(typeof f[$]!="number")throw new TypeError("'status' must contain numbers");let ee=f[$],Se=ee===1?1:0,re=p?p[$]:[];if(re.some(K=>!Number.isFinite(K)))continue;d.push({time:l(e[$]),status:Se,arm:J,x:re,index:$});let j=ee;if(m.has(J))if(ee===2)j=1;else if(ee===1)j=0;else{N.includes("Invalid status value, converted to NA")||N.push("Invalid status value, converted to NA");continue}else if(j!==0&&j!==1){N.includes("Invalid status value, converted to NA")||N.push("Invalid status value, converted to NA");continue}w.push({time:l(e[$]),status:j,arm:J,x:re,index:$})}if(d.some($=>$.time<0))throw new Error("negative follow-up times are not supported");if(!w.some($=>$.arm===0)||!w.some($=>$.arm===1))throw new Error("both arms 0 and 1 are required");let b=[0,1].map($=>Math.max(...d.filter(J=>J.arm===$).map(J=>J.time))),M=[0,1].map($=>{let J=b[$];return d.some(ee=>ee.arm===$&&ee.time===J&&ee.status===0)}),g=M.some(Boolean)?Math.min(...b.filter(($,J)=>M[J])):Math.max(...b),y=r.tau===void 0,x=r.tau===void 0?g:l(r.tau),E=r.tau===void 0?x*i:r.tau;if(v.some($=>$<=x))throw new RangeError("'arm' must contain only 0 and 1 through tau");if(!(x>0))throw new RangeError("'tau' must be greater than zero");if(x>g)throw new RangeError(`'tau' must be at most ${g*i}: the minimum of the largest times allowed by both arms`);if(x<Math.min(...d.map($=>$.time)))throw new RangeError("'tau' must not be below the first observed time");let R=(1-o)/2,A=R<1e-8?pe(R,{lowerTail:!1}):pe(1-R),T=w.filter($=>$.arm===1),k=w.filter($=>$.arm===0),_=Du(T,x),L=Du(k,x),q=$=>({estimate:$.estimate*i,se:$.se*i}),F={rmst:q(_.rmst),rmtl:q(_.rmtl)},C={rmst:q(L.rmst),rmtl:q(L.rmtl)},O=Fu(F.rmst.estimate,F.rmst.se,A),I=Fu(C.rmst.estimate,C.rmst.se,A),V={estimate:F.rmtl.estimate,se:F.rmtl.se,confInt:[E-O.confInt[1],E-O.confInt[0]]},S={estimate:C.rmtl.estimate,se:C.rmtl.se,confInt:[E-I.confInt[1],E-I.confInt[0]]},P=uc(F.rmst,C.rmst,"difference",A),G=uc(F.rmst,C.rmst,"ratio",A),Q=uc(F.rmtl,C.rmtl,"rmtlRatio",A),te={tau:E,tauDefault:y,confLevel:o,arm1:{rmst:O,rmtl:V},arm0:{rmst:I,rmtl:S},difference:P,ratio:G,rmtlRatio:Q,warnings:N};if(p){let $=r.covariateNames===void 0?h?["covariates"]:Array.from({length:p[0].length},()=>""):Array.from(r.covariateNames);if($.some(be=>typeof be!="string"))throw new TypeError("'covariateNames' must contain strings");if($.length!==p[0].length)throw new RangeError("'covariateNames' must have one name per covariate column");let J=w.map(be=>({...be,time:Math.min(be.time,x),status:be.time>=x?1:be.status})),ee=J.map(be=>be.time),Se=J.map(be=>x-be.time),re=hc(J,x,ee,!1,$,A),j=!(F.rmst.estimate>0&&C.rmst.estimate>0),K=!(F.rmtl.estimate>0&&C.rmtl.estimate>0),W=j?void 0:hc(J,x,ee,!0,$,A),U=K?void 0:hc(J,x,Se,!0,$,A),z=(be,fe)=>{let ke=be.coef[1],de=Math.sqrt(Math.max(0,be.cov[1][1]));return fe?{estimate:Math.exp(ke),confInt:[Math.exp(ke-A*de),Math.exp(ke+A*de)],pValue:Bo(ke/de)}:{estimate:ke*i,confInt:[(ke-A*de)*i,(ke+A*de)*i],pValue:Bo(ke/de)}},ne=be=>({...be,estimate:be.estimate.map(fe=>fe*i),se:be.se.map(fe=>fe*i),confInt:[be.confInt[0].map(fe=>fe*i),be.confInt[1].map(fe=>fe*i)]}),le=be=>({...be,estimate:be.estimate.map((fe,ke)=>ke===0?fe+Math.log(i):fe),confInt:[be.confInt[0].map((fe,ke)=>ke===0?fe+Math.log(i):fe),be.confInt[1].map((fe,ke)=>ke===0?fe+Math.log(i):fe)]}),Re={names:["intercept","arm",...$],estimate:Array(re.coef.length).fill(NaN),se:Array(re.coef.length).fill(NaN),z:Array(re.coef.length).fill(NaN),pValue:Array(re.coef.length).fill(NaN),confInt:[Array(re.coef.length).fill(NaN),Array(re.coef.length).fill(NaN)]},Ce={estimate:NaN,confInt:[NaN,NaN],pValue:NaN};te.adjusted={difference:z(re,!1),ratio:j?Ce:z(W,!0),rmtlRatio:K?Ce:z(U,!0)},te.coefficients={difference:ne(re.table),ratio:W?le(W.table):Re,rmtlRatio:U?le(U.table):Re},delete te.arm1,delete te.arm0}return Z("rmst2",te)}var P1=new Set(["start","id","ties","weights","strata","init","control","nocenter","confLevel","robust","cluster"]),j1=["eps","iterMax","tolerChol","tolerInf","timefix"],pc=e=>e==null||typeof e=="number"&&Number.isNaN(e),$1=(e,t)=>typeof e=="number"&&typeof t=="number"?e-t:String(e)<String(t)?-1:String(e)>String(t)?1:0,W1=2**64,z1=2**-1022,Pu=700;function aa(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(),w=h(),d=0,v=0,N=u(),b=h(),M=u(),g=h(),y=0,x=0,E=R=>{let A=Math.exp(x-R);v*=A,y*=A;for(let T=0;T<m;T++){N[T]*=A,M[T]*=A;let k=b[T],_=g[T];for(let L=0;L<=T;L++)k[L]*=A,_[L]*=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 q=R;for(;q>0&&r[q-1]===A&&!c[q-1];)q--;for(let F=q;F<=R;F++)o[F]&&(T=!0)}if(l!==void 0&&T){let q=R;for(;q>0&&!c[q-1];)q--;let F=R;for(;!c[F];)F++;let C=I=>{let V=0;for(let S=0;S<m;S++)V+=t[S]*a[I][S];return V},O=-1/0;for(let I=q;I<=F;I++)l[I]<A&&r[I]>=A&&(O=Math.max(O,C(I)));x=Number.isFinite(O)?O:0,v=0,N=u(),b=h();for(let I=q;I<=F;I++){if(!(l[I]<A&&r[I]>=A)||r[I]===A&&o[I])continue;let V=Math.exp(C(I)-x)*s[I];v+=V;for(let S=0;S<m;S++){N[S]+=V*a[I][S];for(let P=0;P<=S;P++)b[S][P]+=V*a[I][S]*a[I][P]}}}let k=0,_=0;y=0;let L=R;do{let q=a[L],F=0;for(let I=0;I<m;I++)F+=t[I]*q[I];let C=s[L];Number.isFinite(F)&&(F-x>Pu?E(F):F-x<-Pu&&v===0&&y===0&&(x=F));let O=Math.exp(F-x)*C;if(o[L]){k++,_+=C,y+=O,d+=C*F;for(let I=0;I<m;I++){p[I]+=C*q[I],M[I]+=O*q[I];let V=g[I];for(let S=0;S<=I;S++)V[S]+=O*q[I]*q[S]}}else if(l===void 0){v+=O;for(let I=0;I<m;I++){N[I]+=O*q[I];let V=b[I];for(let S=0;S<=I;S++)V[S]+=O*q[I]*q[S]}}L--}while(L>=0&&r[L]===A&&!c[L]);if(k>0){let q=n?k:1,F=n?k:1,C=_/q;for(let O=0;O<q;O++){v+=n?y/F:y,d-=C*(Math.log(v)+x+i);for(let I=0;I<m;I++){N[I]+=n?M[I]/F:M[I];let V=N[I]/v;p[I]-=C*V;let S=b[I],P=g[I],G=w[I];for(let Q=0;Q<=I;Q++){S[Q]+=n?P[Q]/F:P[Q];let te=S[Q]-V*N[Q],$=C*te;G[Q]+=$<1/0&&$>-1/0&&(Math.abs($)>=z1||te===0)||!Number.isFinite(C)?$/v:C*(te/v)}}}M.fill(0);for(let O of g)O.fill(0)}R=L}for(let R=0;R<m;R++)for(let A=0;A<R;A++)w[A][R]=w[R][A];return{loglik:d,score:p,info:w}}function ju(e,t,n){let{time:r,event:o,x:s,stratumEnd:a}=e,c=Te([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 w=[];for(let R=u;R<=h;R++)(R===u||r[R]!==r[R-1])&&w.push(R);let d=R=>R+1<w.length?w[R+1]:h+1,v=new Array(w.length),N=new Array(w.length),b=new Array(w.length),M=-1/0,g=0,y=new Array(l).fill(0);for(let R=w.length-1;R>=0;R--){let A=Number.isFinite(M)?M:0;for(let k=w[R];k<d(R);k++)M=Math.max(M,p[k-u]);let T=Number.isFinite(M)?M:0;if(R<w.length-1&&T!==A){let k=Math.exp(A-T);g*=k;for(let _=0;_<l;_++)y[_]*=k}for(let k=w[R];k<d(R);k++){let _=c[k]*Math.exp(p[k-u]-T);g+=_;for(let L=0;L<l;L++)y[L]+=_*s[k][L]}v[R]=g,N[R]=y.slice(),b[R]=T}let x=0,E=new Array(l).fill(0);for(let R=0;R<w.length;R++){let A=b[R];if(R>0&&A!==b[R-1]){let I=Math.exp(A-b[R-1]);x*=I;for(let V=0;V<l;V++)E[V]*=I}let T=w[R],k=d(R),_=[];for(let I=T;I<k;I++)_.push(Math.exp(p[I-u]-A));let L=0,q=0,F=0,C=new Array(l).fill(0);for(let I=T;I<k;I++){if(!o[I])continue;let V=c[I]*_[I-T];L++,q+=c[I],F+=V;for(let S=0;S<l;S++)C[S]+=V*s[I][S]}for(let I=T;I<k;I++){let V=_[I-T],S=s[I],P=f[I];for(let G=0;G<l;G++)P[G]=-V*(S[G]*x-E[G])}if(L===0)continue;let O=n?L:1;for(let I=0;I<O;I++){let V=I/O,S=v[R]-V*F,P=N[R].map((Q,te)=>(Q-V*C[te])/S),G=q/O/S;for(let Q=T;Q<k;Q++){let te=_[Q-T],$=s[Q],J=f[Q];if(o[Q])for(let ee=0;ee<l;ee++){let Se=$[ee]-P[ee];J[ee]+=Se/O-(1-V)*te*Se*G}else for(let ee=0;ee<l;ee++)J[ee]-=te*($[ee]-P[ee])*G}x+=G;for(let Q=0;Q<l;Q++)E[Q]+=G*P[Q]}}u=h+1}return f}function dc(e,t,n){let r=e.start,{time:o,event:s,x:a,stratumEnd:c}=e,i=Te([e.weight]).groups[0],l=o.length,f=t.length,m=Array.from({length:l},()=>new Array(f).fill(0)),u=o.map((h,p)=>{let w=0;for(let d=0;d<f;d++)w+=t[d]*a[p][d];return w});for(let h=0;h<l;){let p=h;for(;!c[p];)p++;for(let w=h;w<=p;){let d=o[w],v=w;for(;v<p&&o[v+1]===d;)v++;let N=[],b=[],M=-1/0;for(let g=w;g<=v;g++)s[g]&&b.push(g);if(b.length>0)for(let g=h;g<=p;g++)r[g]<d&&o[g]>=d&&(N.push(g),M=Math.max(M,u[g]));if(b.length>0&&N.length>0){Number.isFinite(M)||(M=0);let g=0,y=new Array(f).fill(0),x=0,E=new Array(f).fill(0),R=0;for(let T of N){let k=Math.exp(u[T]-M),_=i[T]*k;g+=_;for(let L=0;L<f;L++)y[L]+=_*a[T][L];if(s[T]&&o[T]===d){R+=i[T],x+=_;for(let L=0;L<f;L++)E[L]+=_*a[T][L]}}let A=n?b.length:1;for(let T=0;T<A;T++){let k=T/A,_=g-k*x,L=R/A/_,q=y.map((F,C)=>(F-k*E[C])/_);for(let F of N){let C=Math.exp(u[F]-M),O=s[F]&&o[F]===d;for(let I=0;I<f;I++){let V=a[F][I]-q[I];O?m[F][I]+=V/A-(1-k)*C*V*L:m[F][I]-=C*V*L}}}}w=v+1}h=p+1}return m}function Uo(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 ia(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 ca(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 Ko=(e,t)=>({statistic:e,df:t,pValue:Bt(e,t,{lowerTail:!1})});function B1(e,t,n){let r=e.length,o=()=>new Array(t).fill(NaN),s=Ko(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)=>st(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 U1(e,t,n,r={}){return H(r,P1,"coxph"),H(vn(r,"control"),j1,"coxph","control"),Z("coxph",K1(e,t,n,r).result)}function K1(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:w}=r,{eps:d=1e-9,iterMax:v=20,tolerChol:N=2**-39}=l,b=l.tolerInf??Math.sqrt(d);if(l.timefix!==void 0&&typeof l.timefix!="boolean")throw new TypeError("'timefix' must be a boolean");let M=l.timefix??!0;if(s!=="efron"&&s!=="breslow")throw new RangeError(`invalid 'ties': ${String(s)}`);if(!(d>0&&d<1/0))throw new RangeError("'eps' must be a positive finite number");if(!(N>0&&N<1/0))throw new RangeError("'tolerChol' must be a positive finite number");if(!(b>0&&b<1/0))throw new RangeError("'tolerInf' must be a positive finite number");if(!(Number.isInteger(v)&&v>=0))throw new RangeError("'iterMax' must be a non-negative integer");if(!(m>0&&m<1))throw new RangeError("'confLevel' must be between 0 and 1");let g=e.length;if(t.length!==g||n.length!==g)throw new RangeError("'time', 'status' and 'x' must have the same length");if(a!==void 0&&a.length!==g)throw new RangeError("'weights' must have the same length as 'time'");if(c!==void 0&&c.length!==g)throw new RangeError("'strata' must have the same length as 'time'");if(h!==void 0&&h.length!==g)throw new RangeError("'cluster' must have the same length as 'time'");if(p!==void 0&&p.length!==g)throw new RangeError("'start' must have the same length as 'time'");if(w!==void 0&&w.length!==g)throw new RangeError("'id' must have the same length as 'time'");if(u!==void 0&&typeof u!="boolean")throw new TypeError("'robust' must be true or false");let y=g===0?0:n[0].length;if(y<1)throw new RangeError("'x' must have at least one column");if(n.some(B=>B.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(B=>!Number.isFinite(B)))throw new RangeError("'init' must contain finite values");let x=new ft,E=[];for(let B=0;B<g;B++){if(Number.isNaN(e[B])||Number.isNaN(t[B])||n[B].some(qe=>Number.isNaN(qe))||p!==void 0&&Number.isNaN(p[B])||a!==void 0&&Number.isNaN(a[B])||c!==void 0&&pc(c[B])||h!==void 0&&pc(h[B])||w!==void 0&&pc(w[B]))continue;if(p!==void 0&&!(p[B]<e[B]))throw new RangeError("stop time must be > start time");let se=t[B];if(se!==0&&se!==1){if(p===void 0)throw new RangeError("'status' must be 0 (censored) or 1 (event)");x.add("Invalid status value, converted to NA");continue}if(e[B]===-1/0||e[B]===1/0&&se===1)throw new RangeError("event times must be finite");E.push(B)}let R=E.length;if(R===0)throw new RangeError(p===void 0?"no observations without missing values":"No (non-missing) observations");if(E.some(B=>n[B].some(se=>!Number.isFinite(se))))throw new RangeError("'x' contains infinite values");let A=E.map(B=>a===void 0?1:a[B]);if(A.some(B=>!(B>0)))throw new RangeError("Invalid weights, must be >0");if(A.some(B=>B===1/0))throw new RangeError("'weights' must be finite");let T=w===void 0?void 0:St(w),k=new Map;if(T!==void 0)for(let B of E){if(t[B]!==1)continue;let se=T[B];k.set(se,(k.get(se)??0)+1)}let _=p!==void 0&&[...k.values()].some(B=>B>1),L=u??(h!==void 0||A.some(B=>!Number.isInteger(B))||_);if(p!==void 0&&u===!0&&h===void 0&&w===void 0)throw new RangeError("one of cluster or id is needed");h!==void 0&&!L&&x.add("cluster specified with robust=FALSE, cluster ignored");let q=0;for(let B of E)q+=t[B];let F=E.map(B=>e[B]),C=p===void 0?void 0:E.map(B=>p[B]),O,I;if(C===void 0)O=M?Yr(F):F;else if(!M)O=F,I=C;else{let B=Yr([...C,...F]);if(I=B.slice(0,R),O=B.slice(R),I.some((se,qe)=>se>=O[qe]))throw new Error("aeqSurv exception, an interval has effective length 0")}if(q===0){let B=B1(E.map(se=>n[se]),y,x.list);return T!==void 0&&(B.nId=new Set(E.map(se=>T[se])).size),{result:B}}let V=new Array(R).fill(0),S,P=[];if(c!==void 0){let B=St(c);P=Array.from(new Set(E.map(qe=>B[qe]))).sort($1);let se=new Map;P.forEach((qe,Ye)=>se.set(qe,Ye)),S=E.map(qe=>B[qe]),V=E.map(qe=>se.get(B[qe]))}let G=Array.from({length:R},(B,se)=>se).sort((B,se)=>V[B]-V[se]||O[B]-O[se]||B-se),Q=G.map(B=>A[B]),{groups:te,k:$}=dt(Q)>=W1?Te([Q]):{groups:[Q],k:0},J=te[0],ee=0;for(let B of A)ee+=$===0?B:D(B,-$);let Se=G.map(B=>n[E[B]].slice()),re=new Array(y).fill(0),j=new Array(y).fill(1),K=new Array(y).fill(0),W=new Set(f);for(let B=0;B<y;B++){if(Se.every(He=>W.has(He[B])))continue;let se=Se.map(He=>He[B]),qe=He=>{let Dt=0;for(let Tn=0;Tn<R;Tn++)Dt+=(p===void 0?J[Tn]:1)*He[Tn];let An=p===void 0?ee:R,sr=Dt/An,Tr=0,Ra=He.map((Tn,ts)=>(Tr+=(p===void 0?J[ts]:1)*Math.abs(Tn-sr),Tn-sr)),Sr=Tr===0?1:An/Tr;return{m:sr,s:Sr,d:Ra,ok:Number.isFinite(sr)&&Sr>=2**-1022&&Sr<1/0}},Ye=qe(se);if(!Ye.ok){let{groups:He,k:Dt}=Te([se]);Ye=qe(He[0]),K[B]=Dt}for(let He=0;He<R;He++)Se[He][B]=Ye.d[He]*Ye.s;re[B]=D(Ye.m,K[B]),j[B]=Ye.s}let U={time:G.map(B=>O[B]),...I===void 0?{}:{start:G.map(B=>I[B])},event:G.map(B=>t[E[B]]===1),weight:J,x:Se,stratumEnd:G.map((B,se)=>se===R-1||V[G[se+1]]!==V[B]),logUnit:$*Math.LN2},z=s==="efron",ne=i===void 0?new Array(y).fill(0):i.map((B,se)=>D(B/j[se],K[se])),le=aa(U,ne,z),Re=[le.loglik,le.loglik],Ce=le.info.map(B=>B.slice()),be=Uo(Ce,N),fe=ia(Ce,le.score),ke=0;for(let B=0;B<y;B++)ke+=le.score[B]*fe[B];let de=0,Oe,Ee=!1,Ve=v===0;if(v===0||!Number.isFinite(le.loglik))Oe=ca(Ce);else{let B=le.loglik,se=ne.map((Ye,He)=>Ye+fe[He]),qe=0;for(Ve=!1,de=1;de<=v;de++){le=aa(U,se,z),Ce=le.info.map(He=>He.slice()),be=Uo(Ce,N);let Ye=Number.isFinite(le.loglik)&&le.score.every(Number.isFinite)&&le.info.every(He=>He.every(Number.isFinite));if(Ye&&(le.loglik===B||Math.abs(1-B/le.loglik)<=d)){Ve=!0,Re[1]=le.loglik,ne=se;break}!Ye||le.loglik<B?(qe++,se=se.map((He,Dt)=>(He+qe*ne[Dt])/(qe+1))):(qe=0,B=le.loglik,Re[1]=B,fe=ia(Ce,le.score),ne=se,se=se.map((He,Dt)=>He+fe[Dt]))}Ve?Oe=ca(Ce):(de=p===void 0?v+1:v,Ee=!0,p!==void 0?(le=aa(U,ne,z),Re[1]=le.loglik,Ce=le.info.map(Ye=>Ye.slice()),be=Uo(Ce,N),Oe=ca(Ce)):v>1?(le=aa(U,ne,z),Re[1]=le.loglik,Oe=le.info.map((Ye,He)=>Ye.map((Dt,An)=>He===An?Ye[He]>0?1/Ye[He]:0:-0))):Oe=ca(Ce))}let Fe=j.map(B=>Math.floor(Math.log2(B))),Ue=j.map((B,se)=>D(B,-Fe[se])),$e=Fe.map((B,se)=>B-K[se]),Je=ne.map((B,se)=>B*Ue[se]),Ke=le.score.map((B,se)=>B/Ue[se]),Xe=Oe.map((B,se)=>B.map((qe,Ye)=>qe*Ue[se]*Ue[Ye])),he=Je.map((B,se)=>D(B,$e[se])),oe=Xe.map((B,se)=>B.map((qe,Ye)=>D(qe,$e[se]+$e[Ye]-$))),we=he.slice();if(v>0&&Math.abs(be)<y)for(let B=0;B<y;B++)Oe[B][B]===0&&(we[B]=NaN);let ge=p===void 0?he:he.map((B,se)=>Number.isNaN(we[se])?0:B),ie=0;for(let B=0;B<y;B++)ie+=ge[B]*re[B];let et=E.map(B=>{let se=0;for(let qe=0;qe<y;qe++)se+=n[B][qe]*ge[qe];return se-ie});if(v>1){let B=Xe.map((se,qe)=>{let Ye=0;for(let He=0;He<y;He++)Ye+=Ke[He]*Xe[He][qe];return Math.abs(Ye)});if(Ee)(p===void 0||v>1)&&x.add("Ran out of iterations and did not converge"),p===void 0&&(dt(et)>500||B.some(se=>!Number.isFinite(se)))&&x.add("one or more coefficients may be infinite");else if(p===void 0){let se=[];for(let qe=0;qe<y;qe++){let Ye=B[qe];(!Number.isFinite(Ke[qe])||D(Ye,$e[qe])>d&&Ye>b*Math.abs(Je[qe]))&&se.push(qe+1)}se.length>0&&x.add("Loglik converged before variable "+se.join(",")+" ; coefficient may be infinite. ")}}let Y=we.map((B,se)=>Number.isNaN(B)?-1:se).filter(B=>B>=0),me=Y.length,Ne,ye=0,Me=0,X;if(L&&me>0){let B=G.map((at,Qe)=>Qe),se=R,qe=h??(p===void 0?void 0:w);if(qe!==void 0){let at=St(qe),Qe=new Map;B=G.map(gt=>{let yt=at[E[gt]],Nt=Qe.get(yt);return Nt===void 0&&(Nt=Qe.size,Qe.set(yt,Nt)),Nt}),se=Qe.size}if(se<2)throw new RangeError(p===void 0?"at least two clusters are needed for the robust variance":"(subscript) logical subscript too long");let Ye=at=>at.map(Qe=>Qe.map((gt,yt)=>gt/Ue[yt])),He=Ye(p===void 0?ju(U,ne,z):dc(U,ne,z)),Dt=He.map(at=>Xe.map((Qe,gt)=>{let yt=0;for(let Nt=0;Nt<y;Nt++)yt+=at[Nt]*Xe[Nt][gt];return yt})),An;if($===0)An=Dt.map((at,Qe)=>at.map(gt=>J[Qe]*gt));else{let at=gt=>gt===0||!Number.isFinite(gt)?0:Math.floor(Math.log2(Math.abs(gt))),Qe=-1/0;Dt.forEach((gt,yt)=>gt.forEach(Nt=>Nt!==0&&Number.isFinite(Nt)&&(Qe=Math.max(Qe,at(J[yt])+at(Nt))))),ye=Qe===-1/0?0:-Qe,An=Dt.map((gt,yt)=>{let Nt=at(J[yt]);return gt.map(ar=>{let Uc=at(ar);return D(D(J[yt],-Nt)*D(ar,-Uc),Nt+Uc+ye)})})}let sr=at=>{let Qe=Array.from({length:se},()=>new Array(y).fill(0));return at.forEach((gt,yt)=>{let Nt=Qe[B[yt]];for(let ar=0;ar<y;ar++)Nt[ar]+=gt[ar]}),Qe},Tr=at=>{let Qe=Array.from({length:y},()=>new Array(y).fill(0));for(let gt of at)for(let yt=0;yt<y;yt++)for(let Nt=0;Nt<y;Nt++)Qe[yt][Nt]+=gt[yt]*gt[Nt];return Qe};Ne=Tr(sr(An));let Ra=Te([Q]).groups[0],Sr=i===void 0?new Array(y).fill(0):i.map((at,Qe)=>D(at/j[Qe],K[Qe])),Tn=p===void 0?ju(U,Sr,z):dc(U,Sr,z),ts=sr(Ye(Tn).map((at,Qe)=>at.map(gt=>Ra[Qe]*gt))),mo=new Array(y).fill(0);for(let at of ts)for(let Qe=0;Qe<y;Qe++)mo[Qe]+=at[Qe];let Aa=Tr(ts);if(y===1)Me=mo[0]*mo[0]/Aa[0][0];else{Uo(Aa,N);let at=ia(Aa,mo);for(let Qe=0;Qe<y;Qe++)Me+=mo[Qe]*at[Qe]}let zc=new Array(R),Bc=new Array(R);G.forEach((at,Qe)=>{zc[at]=He[Qe].map((gt,yt)=>D(gt,-$e[yt])),Bc[at]=An[Qe].map((gt,yt)=>D(gt,$e[yt]-ye))}),X={scoreResiduals:zc,dfbeta:Bc}}let ve=Ne!==void 0,ae=Ne??Xe,Ae=Ne===void 0?-$:-2*ye,Be=Y.map(B=>Je[B]-(i===void 0?0:D(i[B],-$e[B]))),je=0;if(me===1)je=Be[0]*Be[0]/ae[Y[0]][Y[0]];else if(me>1){let B=Y.map(qe=>Y.map(Ye=>ae[qe][Ye]));Uo(B,N);let se=ia(B,Be);for(let qe=0;qe<me;qe++)je+=Be[qe]*se[qe]}je=D(je,-Ae);let nt=(1-m)/2,ut=nt<1e-8?pe(nt,{lowerTail:!1}):pe((1+m)/2),It=(B,se)=>{let qe=Math.floor(se/2),Ye=B.map((He,Dt)=>Math.sqrt(D(He[Dt],se-2*qe)));return{unit:Ye,h:qe,se:Ye.map((He,Dt)=>D(He,$e[Dt]+qe))}},$t=It(Xe,-$).se,{unit:Fn,h:We,se:ze}=It(ae,Ae),Mt=we.map((B,se)=>D((Number.isNaN(B)?B:Je[se])/Fn[se],-We)),Et=Mt.map(B=>Bt(B*B,1,{lowerTail:!1})),wn=we.map((B,se)=>Math.exp(B-ut*ze[se])),Ft=we.map((B,se)=>Math.exp(B+ut*ze[se])),kt=D(-2*(Re[0]-Re[1]),$),ht=[D(Re[0],$),D(Re[1],$)],Wt=D(ke,$),Zt=Ne?.map((B,se)=>B.map((qe,Ye)=>D(qe,$e[se]+$e[Ye]-2*ye))),Gt={coefficients:we,var:Zt??oe,loglik:ht,score:Wt,wald:je,iter:de,means:re,n:R,nevent:q,...T===void 0?{}:{nId:new Set(E.map(B=>T[B])).size},linearPredictors:et,se:$t,z:Mt,p:Et,stdError:$t,zValue:Mt,pValue:Et,lower:wn,upper:Ft,logtest:Ko(kt,me),sctest:Ko(Wt,me),waldtest:Ko(je,me),rsq:{rsq:1-Math.exp(-kt/R),maxrsq:1-Math.exp(2*ht[0]/R)},usedRobust:ve,...ve?{naiveVar:oe,robustSe:ze,rscore:Me,robscore:Ko(Me,me)}:{},warnings:x.list},_t=le.score.map((B,se)=>D(B/j[se],K[se]+$));Object.defineProperty(Gt,"coxZphData",{enumerable:!1,value:{time:O.slice(),...I===void 0?{}:{start:I.slice()},status:E.map(B=>t[B]===1),weights:A.slice(),...S===void 0?{}:{strata:S.slice()},x:E.map(B=>n[B].slice()),sortedToInput:G.slice(),sortedTime:U.time.slice(),...U.start===void 0?{}:{sortedStart:U.start.slice()},sortedEvent:U.event.slice(),sortedWeight:U.weight.slice(),sortedX:U.x.map(B=>B.slice()),stratumIndex:G.map(B=>V[B]),stratumEnd:U.stratumEnd.slice(),betaScaled:ne.slice(),inverseCovariateScale:Ue.map((B,se)=>D(1/B,-$e[se])),weightExp:$,ties:s}});let Yt=X?.scoreResiduals;if(p!==void 0&&o&&Yt===void 0){let B=dc(U,ne,z);Yt=new Array(R),G.forEach((se,qe)=>{Yt[se]=B[qe].map((Ye,He)=>D(Ye/Ue[He],-$e[He]))})}return{result:Gt,...X,...Yt===void 0?{}:{scoreResiduals:Yt},first:_t,converged:Ve}}var $u=["transform","terms","collapseTerms","global"],On=e=>new Array(e).fill(0),tr=e=>Array.from({length:e},()=>On(e));function H1(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 Wu(e,t){let n=0,r=e.length;for(;n<r;){let o=Math.floor((n+r)/2);e[o]<t?n=o+1:r=o}return n}function G1(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=Wu(a,i)-Wu(s,i);l.deaths>0&&(c*=1-l.deaths/f)}return n}function Y1(e,t,n,r){let o;if(typeof t=="function"){let s=t(e.time.slice());if(!Array.isArray(s)||s.length!==e.time.length)throw new RangeError("transform function must return one value per observation");o=Array.from(s)}else if(t==="km")o=G1(e);else if(t==="rank")o=H1(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 X1(e,t={}){H(t,$u,"coxZph"),H(t,$u,"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((j,K)=>Number.isNaN(j)?-1:K).filter(j=>j>=0),c=a.length;if(c===0)throw new Error("the proportional-hazards test has no estimable coefficients");let i=[];for(let j=0;j<s.sortedTime.length;){let K=j;for(;!s.stratumEnd[K];)K++;for(let W=j;W<=K;){let U=s.sortedTime[W],z=W;for(;z<K&&s.sortedTime[z+1]===U;)z++;let ne=[];for(let le=W;le<=z;le++)s.sortedEvent[le]&&ne.push(le);s.start!==void 0&&ne.reverse();for(let le of ne)i.push(s.sortedToInput[le]);W=z+1}j=K+1}if(i.length===0)throw new Error("the proportional-hazards test requires events");let l=i.map(j=>s.time[j]);if(new Set(l).size<2)throw new Error("the test needs events at two or more distinct times");let f=[...e.warnings],m=Y1(s,n,i,f),u=m[i[0]];if(i.every(j=>m[j]===u))throw new Error("the transform is constant at the event times");let h=i.reduce((j,K)=>Math.max(j,Math.abs(m[K])),0),p=h===0?0:Math.floor(Math.log2(h)),w=m.map(j=>Number.isFinite(j)?D(j,-p):j),d=i.reduce((j,K)=>j+w[K],0)/i.length,v=w.map(j=>j-d);if(i.every(j=>v[j]===0))throw new Error("the transform is constant at the event times");let N=a.map(j=>s.betaScaled[j]),b=tr(c),M=tr(c),g=tr(c),y=On(c),x=[];for(let j=0;j<s.sortedTime.length;){let K=j;for(;!s.stratumEnd[K];)K++;for(let W=j;W<=K;){let U=s.sortedTime[W],z=W;for(;z<K&&s.sortedTime[z+1]===U;)z++;let ne=[];for(let le=W;le<=z;le++)s.sortedEvent[le]&&ne.push(le);if(ne.length>0){let le=[],Re=-1/0;for(let oe=j;oe<=K;oe++){if(!((s.sortedStart===void 0||s.sortedStart[oe]<U)&&s.sortedTime[oe]>=U))continue;le.push(oe);let ge=0;for(let ie=0;ie<c;ie++)ge+=N[ie]*s.sortedX[oe][a[ie]];Re=Math.max(Re,ge)}if(le.length===0||!Number.isFinite(Re))throw new Error("empty Cox proportional-hazards risk set");let Ce=On(c),be=tr(c),fe=On(c),ke=tr(c),de=0,Oe=0;for(let oe of le){let we=0;for(let ie=0;ie<c;ie++)we+=N[ie]*s.sortedX[oe][a[ie]];let ge=s.sortedWeight[oe]*Math.exp(we-Re);de+=ge;for(let ie=0;ie<c;ie++){let et=s.sortedX[oe][a[ie]];Ce[ie]+=ge*et;for(let Y=0;Y<c;Y++)be[ie][Y]+=ge*et*s.sortedX[oe][a[Y]]}if(s.sortedEvent[oe]&&s.sortedTime[oe]===U){Oe+=ge;for(let ie=0;ie<c;ie++){let et=s.sortedX[oe][a[ie]];fe[ie]+=ge*et;for(let Y=0;Y<c;Y++)ke[ie][Y]+=ge*et*s.sortedX[oe][a[Y]]}}}let Ee=0,Ve=On(c);for(let oe of ne){let we=s.sortedWeight[oe];Ee+=we;for(let ge=0;ge<c;ge++)Ve[ge]+=we*s.sortedX[oe][a[ge]]}let Fe=ne.length,Ue=s.ties==="efron"&&Fe>1?Fe:1,$e=On(c),Je=tr(c);for(let oe=0;oe<Ue;oe++){let we=oe/Ue,ge=de-we*Oe,ie=Ce.map((Y,me)=>(Y-we*fe[me])/ge);for(let Y=0;Y<c;Y++)$e[Y]+=ie[Y]/Ue;let et=Ee/Ue;for(let Y=0;Y<c;Y++)for(let me=0;me<c;me++)Je[Y][me]+=et*((be[Y][me]-we*ke[Y][me])/ge-ie[Y]*ie[me])}let Ke=Ve.map((oe,we)=>oe-Ee*$e[we]),Xe=s.sortedToInput[ne[0]],he=v[Xe];for(let oe=0;oe<c;oe++){y[oe]+=he*Ke[oe];for(let we=0;we<c;we++)b[oe][we]+=Je[oe][we],M[oe][we]+=he*Je[oe][we],g[oe][we]+=he*he*Je[oe][we]}s.start!==void 0&&ne.reverse();for(let oe of ne){let we=s.sortedToInput[oe],ge=a.map(ie=>s.sortedX[oe][ie]);for(let ie=0;ie<c;ie++)ge[ie]-=$e[ie];x.push({input:we,time:U,stratum:s.stratumIndex[oe],g:m[we],residual:ge})}}W=z+1}j=K+1}let E;try{E=ot(b,M)}catch{throw new Error("the proportional-hazards information is singular")}let R=tr(c);for(let j=0;j<c;j++)for(let K=0;K<c;K++){let W=0;for(let U=0;U<c;U++)W+=M[U][j]*E[U][K];R[j][K]=g[j][K]-W}for(let j=0;j<c;j++)for(let K=0;K<j;K++){let W=(R[j][K]+R[K][j])/2;R[j][K]=W,R[K][j]=W}let A=t.terms===!1||t.terms===void 0?a.map(j=>[j]):t.terms,T=new Set,k=[],_=[];if(A.forEach((j,K)=>{if(!Array.isArray(j)||j.length===0)throw new RangeError("each terms group must be a non-empty array");let W=[];for(let U of j){if(!Number.isInteger(U)||U<0||U>=e.coefficients.length)throw new RangeError("terms column indices must be zero-based and within x");if(T.has(U))throw new RangeError("terms groups must not overlap");T.add(U),a.includes(U)&&W.push(U)}W.length>0&&(k.push(W),_.push(K))}),a.forEach(j=>{T.has(j)||(k.push([j]),_.push(A.length))}),k.length===0)throw new Error("the proportional-hazards test has no estimable terms");let L=[];k.forEach((j,K)=>{if(o&&j.length>1)L.push({col:-1,group:K,collapsed:!0});else for(let W of j)L.push({col:W,group:K,collapsed:!1})});let q=L.length,F=Array.from({length:c},()=>On(q)),C=[],O=[];L.forEach((j,K)=>{let W=k[j.group],U=_[j.group];if(j.collapsed){for(let z of W){let ne=a.indexOf(z);F[ne][K]=N[ne]}C.push(1),O.push(`g${U+1}`)}else{let z=a.indexOf(j.col);F[z][K]=s.inverseCovariateScale[j.col],C.push(e.coefficients[j.col]),O.push(W.length>1?`g${U+1}x${j.col+1}`:`x${j.col+1}`)}});let I=Array.from({length:q},(j,K)=>{let W=0;for(let U=0;U<c;U++)W=Math.max(W,Math.abs(F[U][K]));if(!(W>0&&Number.isFinite(W)))throw new Error("the proportional-hazards residual variance is singular");return Math.floor(Math.log2(W))}),V=F.map(j=>j.map((K,W)=>D(K,-I[W]))),S=Array.from({length:c},()=>On(q));for(let j=0;j<c;j++)for(let K=0;K<q;K++)for(let W=0;W<c;W++)S[j][K]+=b[j][W]*V[W][K];let P=tr(q);for(let j=0;j<q;j++)for(let K=0;K<q;K++)for(let W=0;W<c;W++)P[j][K]+=V[W][j]*S[W][K];for(let j=0;j<q;j++)for(let K=0;K<j;K++){let W=(P[j][K]+P[K][j])/2;P[j][K]=W,P[K][j]=W}let G;try{G=ot(P)}catch{throw new Error("the proportional-hazards residual variance is singular")}for(let j=0;j<q;j++)for(let K=0;K<j;K++){let W=(G[j][K]+G[K][j])/2;G[j][K]=W,G[K][j]=W}let Q=G.map((j,K)=>j.map((W,U)=>i.length*Le(W,-I[K]-I[U]-s.weightExp))),te=x.map(j=>{let K=On(q);for(let W=0;W<q;W++)for(let U=0;U<c;U++)K[W]+=V[U][W]*j.residual[U];return C.map((W,U)=>{let z=0;for(let ne=0;ne<q;ne++)z+=K[ne]*G[ne][U];return W+i.length*Le(z,-I[U]-s.weightExp)})}),$=[],J=[],ee=[],Se=L.map(j=>j.collapsed?k[j.group].map(K=>a.indexOf(K)):[a.indexOf(j.col)]);for(let j of Se){let K=j.map(ne=>j.map(le=>R[ne][le])),W=j.map(ne=>y[ne]),U;try{U=ot(K,W)}catch{throw new Error("the proportional-hazards term information is singular")}let z=D(Math.max(0,W.reduce((ne,le,Re)=>ne+le*U[Re],0)),s.weightExp);$.push(z),J.push(j.length),ee.push(Bt(z,j.length,{lowerTail:!1}))}if(r){let j;try{j=ot(R,y)}catch{throw new Error("the global proportional-hazards information is singular")}let K=D(Math.max(0,y.reduce((W,U,z)=>W+U*j[z],0)),s.weightExp);$.push(K),J.push(c),ee.push(Bt(K,c,{lowerTail:!1})),O.push("GLOBAL")}let re={rowNames:O,chisq:$,df:J,p:ee,pValue:ee,x:x.map(j=>j.g),time:x.map(j=>j.time),...s.strata===void 0?{}:{strata:x.map(j=>mt(s.strata[j.input]))},y:te,var:Q,transform:n,warnings:f};return Z("coxZph",re,e)}var Q1=["center"],Z1=2**-1022;function J1(e,t,n={}){H(n,Q1,"leveneTest");let r=Ie(n.center??"median",["median","mean"]),{groups:o}=Kn(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"?$s(l):st(l);return l.map(m=>Math.abs(m-f))},a=Te(o).groups.map(s),c=(l,f)=>l.some(m=>m>0&&m<Z1)||l.every(m=>m===0)&&o[f].some(m=>m!==o[f][0]);if(a.some(c)){let l=o.map(u=>{let h=Te([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=>D(h,u.k-m)))}let i=Km(wr(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 eb=["score"],tb=["alternative","exact","variant","levels"];function nb(e,t,...n){H(Cr(n),eb,"propTrendTest");let{score:r}=Lr(["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:w,sxx:d}=gc(f,m,h,u);if(!(d>=2**-1022&&d<1/0&&Math.abs(w)<1/0)){let N=Te([u.map(b=>c[b])]);N.k!==0&&(h=c.slice(),u.forEach((b,M)=>h[b]=N.groups[0][M]),{sw:p,sxy:w,sxx:d}=gc(f,m,h,u))}let v=Ot(w,d);if(!(i<1/0)||l>0&&l<1&&!(p<1/0&&Math.abs(w)<1/0&&d<1/0)){let{groups:[N,b],k:M}=Te([o,s]),g=N.reduce((x,E)=>x+E,0)/b.reduce((x,E)=>x+E,0),y=gc(b,m,h,u);v=D(y.sxy/g*(y.sxy/y.sxx/(1-g)),M)}return{method:"Chi-squared Test for Trend in Proportions",statistic:v,statisticName:"X-squared",parameter:1,parameterName:"df",pValue:bt(v,.5,2,!1,!1),warnings:[]}}function gc(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 rb=2**24,ob=2**32,sb=`Sample size > 100 or data with ties
10
+ p-value based on normal approximation. Specify nperm for permutation p-value`,ab=`Sample data with ties
11
+ p-value based on normal approximation. Specify nperm for permutation p-value.`;function ib(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=St(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(ln)}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 cb(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 lb(e,t,n={}){H(n,tb,"jonckheereTest");let r=Ie(n.alternative,["two.sided","increasing","decreasing"]),o=n.variant??"DescTools";if(o!=="DescTools"&&o!=="clinfun")throw new RangeError(`invalid variant "${String(o)}"`);if(o==="clinfun"&&n.exact!==void 0)throw new TypeError("clinfun's jonckheere.test has no option 'exact'");let s=ib(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 M=b+1;M<s.length;M++)for(let g of N)for(let y of s[M])l+=g<y?1:g===y?.5:0});let u=[],h=!i&&c<=100;o==="DescTools"&&n.exact!==void 0?(h=n.exact,h&&i&&(h=!1,u.push(ab))):h||u.push(sb);let p,w;if(h){let N=1,b=0;for(let[g,y]of f)g===0||y===0||(b+=N*(g*y+1),N+=g*y);if(N>rb||b>ob)throw new RangeError(`the exact distribution of JT has ${N} values and needs ${b} steps, too many: use exact: false`);let M=[1];for(let[g,y]of f){if(g===0||y===0)continue;let x=Array.from({length:g*y+1},(R,A)=>il(A,g,y,!1)),E=new Array(M.length+x.length-1).fill(0);M.forEach((R,A)=>x.forEach((T,k)=>E[A+k]+=R*T)),M=E}p=0,w=0,M.forEach((g,y)=>{y<=l&&(p+=g),y>=l&&(w+=g)})}else{let N=f.reduce((g,[y,x])=>g+y*x,0)/2,b=i?cb(s.map(g=>g.length),a):f.reduce((g,[y,x])=>g+y*x*(y+x+1)/12,0),M=(l-N)/Math.sqrt(b);p=ce(M),w=1-p,w<1e-8&&(w=ce(M,{lowerTail:!1}))}let v=r==="increasing"?w:r==="decreasing"?p:Math.min(1,2*Math.min(w,p,o==="clinfun"?.5:1));return Z("jonckheereTest",{method:"Jonckheere-Terpstra test",alternative:r,statistic:l,statisticName:"JT",pValue:v,warnings:u})}var fb=["nClasses","adjust"];function Ho(e){return Ct(Array.from(e)).sort((t,n)=>t-n)}function zu(e){if(e.length>0&&!(Number.isFinite(e[0])&&Number.isFinite(e[e.length-1])))throw new RangeError("'x' must be finite")}function la(e){zu(e);let t=tl(e);if(t.some(Number.isNaN))throw new RangeError("all 'x' values are identical");return t}function mb(e){let t=Ho(e),n=t.length;if(n<8)throw new RangeError("sample size must be greater than 7");let r=la(t),o=0;for(let i=1;i<=n;i++)o+=(2*i-1)*(ce(r[i-1],{logP:!0})+ce(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 ub(e){let t=Ho(e),n=t.length;if(n<8)throw new RangeError("sample size must be greater than 7");let r=la(t),o=1/(12*n);r.forEach((i,l)=>{o+=(ce(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")),Z("cvmTest",{method:"Cramer-von Mises normality test",statistic:o,statisticName:"W",pValue:c,warnings:a})}function hb(e){let t=Ho(e),n=t.length;if(n<5)throw new RangeError("sample size must be greater than 4");let r=la(t).map(f=>ce(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 pb(e){let t=Ho(e),n=t.length;if(n<5||n>5e3)throw new RangeError("sample size must be between 5 and 5000");if(zu(t),t[0]===t[n-1])throw new RangeError("all 'x' values are identical");let r=t.map((m,u)=>pe((u+1-3/8)/(n+1/4))),o=Te([t]).groups[0],s=Math.min(el(o,r)**2/(rt(o)*rt(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=ce((Math.log(1-s)-i)/l,{lowerTail:!1});return{method:"Shapiro-Francia normality test",statistic:s,statisticName:"W",pValue:f,warnings:[]}}function db(e,t={}){H(t,fb,"pearsonTest");let n=Ho(e),r=n.length,o=t.nClasses??Math.ceil(2*r**(2/5)),s=t.adjust??!0;if(!(Number.isInteger(o)&&o-1-(s?2:0)>0))throw new RangeError("'nClasses' must be an integer leaving positive degrees of freedom");if(o>2147483647)throw new RangeError("attempt to make a table with >= 2^31 elements");let a=[];for(let f of la(n)){let m=Math.min(o,Math.floor(1+o*ce(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:bt(i,l/2,2,!1,!1),warnings:[]}}var gb=["weight"],wb=["exact"],bb=["model","type","unit","r0","confLevel"];function Go(e,t){let n=e[0]?.length??0;if(e.some(r=>r.length!==n))throw new TypeError(`'${t}' must be a matrix (rows of equal length)`);return Array.from({length:n},(r,o)=>e.map(s=>s[o]))}function fa(e,t){if(e.some(n=>n.some(Number.isNaN)))throw new RangeError(`'${t}' contains missing values`);if(e.some(n=>n.some(r=>!Number.isFinite(r))))throw new RangeError(`'${t}' contains infinite values`)}var Uu=e=>2*ce(-Math.abs(e));function vb(e,t={}){H(t,gb,"kappa2");let n=Ie(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=>In(x[0])),a=r.map(x=>In(x[1])),c=[...new Set([...s,...a])].sort((x,E)=>x<E?-1:x>E?1:0),i=c.length,l=new Map(c.map((x,E)=>[x,E])),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,E)=>Array.from({length:i},(R,A)=>m(Math.abs(E-A)))),h=f.map(x=>De(x)/o),p=Array.from({length:i},(x,E)=>De(f.map(R=>R[E]))/o),w=x=>{let E=[];for(let R=0;R<i;R++)for(let A=0;A<i;A++)E.push(x(R,A));return De(E)},d=w((x,E)=>f[x][E]*u[x][E])/o,v=w((x,E)=>h[x]*p[E]*u[x][E]),N=(d-v)/(1-v),b=Array.from({length:i},(x,E)=>De(p.map((R,A)=>R*u[E][A]))),M=Array.from({length:i},(x,E)=>De(h.map((R,A)=>R*u[A][E]))),g=(w((x,E)=>h[x]*p[E]*(u[x][E]-(b[x]+M[E]))**2)-v*v)/(o*(1-v)**2),y=N/Math.sqrt(g);return{value:N,statistic:y,pValue:Uu(y),warnings:[]}}function yb(e,t={}){H(t,wb,"kappamFleiss");let n=t.exact??!1,r=Go(e,"ratings");fa(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=St(a),i=[...new Set(c)].sort((g,y)=>g-y),l=new Map(i.map((g,y)=>[g,y])),f=new Map(a.map((g,y)=>[g,l.get(c[y])])),m=i.length,u=e.map(g=>{let y=new Array(m).fill(0);for(let x of g)y[f.get(x)]++;return y}),h=De(u.map(g=>(De(g.map(y=>y*y))-s)/(s*(s-1))))/o,p=Array.from({length:m},(g,y)=>De(u.map(x=>x[y]))/(o*s)),w=De(p.map(g=>g*g));if(n){let g=r.map(x=>{let E=new Array(m).fill(0);for(let R of x)E[f.get(R)]++;return E.map(R=>R/o)}),y=Array.from({length:m},(x,E)=>rt(g.map(R=>R[E]))*(s-1)/s);w-=De(y)/(s-1)}let d=(h-w)/(1-w);if(n)return{value:d,warnings:[]};let v=De(p.map(g=>g*(1-g))),N=De(p.map(g=>g*(1-g)*(1-g-g))),b=2/(v*v*o*s*(s-1))*(v*v-N),M=d/Math.sqrt(b);return{value:d,statistic:M,pValue:Uu(M),warnings:[]}}function Nb(e,t={}){H(t,bb,"icc");let n=Ie(t.model,["oneway","twoway"]),r=Ie(t.type,["consistency","agreement"]),o=Ie(t.unit,["single","average"]),s=t.r0??0,a=t.confLevel??.95;qt(a),Go(e,"ratings"),fa(e,"ratings");let c=Te(e).groups,i=Go(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=rt(c.map(V=>st(V)))*f,h=rt(i.map(V=>st(V)))*l,p=st(c.map(V=>rt(V))),w=rt(c.flat())*(l*f-1),d=w-u*(l-1)-h*(f-1),v=d>2**-20*w?d/((l-1)*(f-1)):Rb(c)/((l-1)*(f-1)),N=(V,S,P)=>pt(V,S,P,!1,!1),b=(V,S)=>m/2<1e-8?on(m/2,V,S,!1,!1):on(1-m/2,V,S,!0,!1),M=l-1;if(n==="oneway"||r==="consistency"){let V=n==="oneway"?p:v,S=n==="oneway"?l*(f-1):(l-1)*(f-1),P=u/V,G=P/b(M,S),Q=P*b(S,M),[te,$,J]=o==="single"?[(u-V)/(u+(f-1)*V),P*(1-s)/(1+(f-1)*s),ee=>(ee-1)/(ee+f-1)]:[(u-V)/u,P*(1-s),ee=>1-1/ee];return{value:te,Fvalue:$,fValue:$,df1:M,df2:S,pValue:N($,M,S),lbound:J(G),ubound:J(Q),warnings:[]}}let g=o==="single"?f:1,y=V=>[g*V/(l*(1-V)),1+g*V*(l-1)/(l*(1-V))],x=(V,S)=>nl([V*h,S*v],[f-1,(l-1)*(f-1)]),E=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),k=x(R,A),[_,L]=[f*E/(l*(1-E)),1+f*E*(l-1)/(l*(1-E))],q=x(_,L),F=b(M,q),C=b(q,M),[O,I]=o==="single"?[l*(u-F*v)/(F*(f*h+(f*l-f-l)*v)+l*u),l*(C*u-v)/(f*h+(f*l-f-l)*v+l*C*u)]:[l*(u-F*v)/(F*(h-v)+l*u),l*(C*u-v)/(h-v+l*C*u)];return{value:E,Fvalue:T,fValue:T,df1:M,df2:k,pValue:N(T,M,k),lbound:O,ubound:I,warnings:[]}}function Bu(e,t){let n=t.length,r=De(t.flatMap(s=>t.map(a=>e[s][a])));return(1-De(t.map(s=>e[s][s]))/r)*n/(n-1)}function Mb(e){let t=Go(e,"x").length;if(fa(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=Ns(Te(e).groups),r=yo(e),o=Array.from({length:t},(c,i)=>i),s=De(r.flat()),a;try{let c=ot(r);a=o.map(i=>1-1/c[i][i])}catch(c){if(!r.flat().every(Number.isFinite))throw c;let i=Te(e).groups;a=o.map(l=>{let f=i.map(h=>h[l]),m=Fr(Rt(i.map(h=>[1,...h.filter((p,w)=>w!==l)])),f),u=st(f);return 1-De(m.map(h=>h*h))/De(f.map(h=>(h-u)**2))})}return Z("cronbachAlpha",{rawAlpha:Bu(n,o),stdAlpha:(1-t/s)*t/(t-1),G6:1-(t-De(a))/s,averageR:(s-t)/(t*(t-1)),alphaDrop:o.map(c=>Bu(n,o.filter(i=>i!==c))),warnings:Eb(e,r)})}function Eb(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}=Vt(r.map(c=>r.map(i=>t[c][i])),{symmetric:!0}),s=o.map(c=>c[0]),a=De(s)<0?-1:1;s.some(c=>a*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 g1(e,t,n){let r=Eo(e,"x").length;Ds(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=Xr(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=Mt(i,l);for(let m=0;m<2;m++)for(let u=0;u<2;u++)c[m][u]-=Ne(a.map((h,p)=>o[s[m]][h]*f[p][u]))}return c[0][1]/Math.sqrt(c[0][0]*c[1][1])}function w1(e){let t=e.length,n=e[0].length,r=i=>i.reduce((l,f)=>H(l,[f,0]),[0,0]),o=e.map(i=>Ve(r(i),n)),s=e[0].map((i,l)=>Ve(r(e.map(f=>f[l])),t)),a=Ve(r(e.flat()),t*n),c=[0,0];return e.forEach((i,l)=>i.forEach((f,m)=>{let u=H(he(he([f,0],o[l]),s[m]),a);c=H(c,ce(u,u))})),c[0]+c[1]}var b1=["controls","cases"],nu=["levels","direction","naRm"],v1=["partialAuc","focus","correct"],N1=["bestMethod","bestWeights"],cu=new WeakMap;function Vs(e){return cu.get(e)}var y1=["auto","<",">"];function Oi(e){if(e==null)return null;if(typeof e=="number")return Number.isNaN(e)?null:dn(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 M1(e){let t=e.filter(s=>Oi(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(dn):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 xo(e){return Float64Array.from(e).sort()}function Ci(e,t){let n=xo(e),r=xo(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 Fi(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 Di(e,t,n){let r=e.length,o=t.length;return Fi(Ci(e,t),r,n)/(2*o*r)}function Pi(e,t,n){return Fi(Ci(e,t),e.length,n)}var ru=e=>e===0n?0:e.toString(2).length;function kr(e,t){if(t<0n&&(e=-e,t=-t),e===0n)return 0;let n=e<0n,r=n?-e:e,o=55-(ru(r)-ru(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=ye(Number(c),-o);return n?-i:i}function E1(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 R1(e,t,n){let r=Ci(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:E1(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:Fi(r,o,n)/(2*s*o)}}function x1(e,t){let n=r=>{let o=xo(r),s=o.length>>1;return o.length%2===1?o[s]:sn(o[s-1],o[s])};return n(e)<=n(t)?"<":">"}function lu(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"?x1(e,t):n,i={...R1(e,t,a),direction:a,levels:r,controls:e,cases:t,warnings:s};return cu.set(i,o),i}function fu(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 Ar(e,t,n){let r=fu(n.direction),o=new Ue,s=e.map(Oi),a;if(n.levels===void 0){if(a=M1(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=Oi(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 lu(i,l,r,[a[0],a[1]],h,o.list)}function T1(e,t){let n=jo(lr(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=Ic(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 A1(e){return typeof e=="object"&&e!==null&&!Array.isArray(e)&&!ArrayBuffer.isView(e)}function S1(e,t,n={}){if(Je(e)&&V(e,b1,"roc"),typeof e!="string"&&Je(t)&&V(t,nu,"roc"),V(n,nu,"roc"),typeof e=="string"){let r=T1(e,t);return W("roc",Ar(r.response,r.predictor,n))}if(A1(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=fu(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 W("roc",lu(i,l,s,["0","1"],m,[]))}return W("roc",Ar(Array.from(e),Array.from(t),n))}var Ps=(e,t,n,r,o)=>t[r]+(n-e[r])/(e[o]-e[r])*(t[o]-t[r]);function k1(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=Ps(e,t,r,i,l),m=Ps(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=Ps(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=Ps(e,t,n,l,i);s+=(e[l]-n)*(t[l]+f)/2}return s}var Pn=(e,t)=>[e[0]*t[1]+t[0]*e[1],e[1]*t[1]],rr=(e,t)=>[e[0]*t[1]-t[0]*e[1],e[1]*t[1]],cn=(e,t)=>[e[0]*t[0],e[1]*t[1]],ou=(e,t)=>t[0]<0n?[-e[0]*t[1],-e[1]*t[0]]:[e[0]*t[1],e[1]*t[0]],Ro=(e,t)=>{let n=e[0]*t[1]-t[0]*e[1];return n<0n?-1:n>0n?1:0};function su(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 au(e,t,n,r,o,s){let a=su(e,n),c=su(t,r);if(a!==void 0&&c!==void 0)return iu(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(Sr),f=t.map(Sr),m=0n;for(let[,h]of[...l,...f])m=h>m?h:m;let u=h=>h[0]*(m/h[1]);return iu(l.map(u),f.map(u),m,m,o,s)}function iu(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=Sr(o),f=Sr(s),m=(d,v,N)=>Pn(c(v),cn(ou(rr(d,i(v)),rr(i(N),i(v))),rr(c(N),c(v)))),u=[];e.forEach((d,v)=>{Ro(i(v),l)>=0&&Ro(i(v),f)<=0&&u.push(v)});let h;if(u.length===0){let d=e.findIndex((N,b)=>Ro(i(b),f)>=0),v=d-1;h=cn(Pn(m(f,v,d),m(l,v,d)),cn(rr(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(Ro(i(v),f)!==0){let b=v+1;h=Pn(h,cn(rr(f,i(v)),cn(Pn(c(v),m(f,v,b)),[1n,2n])))}let N=u[0];if(Ro(i(N),l)!==0){let b=N-1;h=Pn(h,cn(rr(i(N),l),cn(Pn(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=cn(cn(rr(f,l),Pn(f,l)),[1n,2n]),g=ou(Pn(p,h),cn(p,[2n,1n]));return kr(g[0],g[1])}function q1(e,t={}){V(t,v1,"auc");let n=ae(t.focus,["specificity","sensitivity"]),r=t.correct??!1;if(t.partialAuc===void 0)return W("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"?au(l,f,u,h,a,s):au(l,f,h,u,a,s):m=k1(l,f,a,s),W("auc",{auc:m,partialAuc:[s,a],focus:n,correct:r,warnings:[]})}function Sr(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 I1(e,t){let n=xo(e.controls),r=xo(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 _1(e,t="all",n={}){V(n,N1,"coords");let r=ae(n.bestMethod,["youden","closest.topleft","topleft"]),o=n.bestWeights??[1,.5];if(!Array.isArray(o)||o.length!==2||o.some(d=>typeof d!="number"))throw new TypeError("'best.weights' must be a numeric vector of length 2");let[s,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=ae(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}=I1(e,i);if(l==="best"){let[d,v]=Sr(s),[N,b]=Sr(a),E=(b-N)*v,w=d*N,M=BigInt(f),R=BigInt(m),y=u.map((A,S)=>{if(r==="youden")return BigInt(A)*R*w+E*BigInt(h[S])*M;let k=BigInt(f-A),_=BigInt(m-h[S]);return-(k*k*R*R*w+E*_*_*M*M)}),x=y.reduce((A,S)=>S>A?S:A),T=y.map((A,S)=>A===x?S:-1).filter(A=>A>=0);i=T.map(A=>i[A]),u=T.map(A=>u[A]),h=T.map(A=>h[A])}let p=u.map(d=>f-d),g=h.map(d=>m-d);return W("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 L1=["confLevel","method","bootN","bootStratified"],mu=["method","alternative","paired","confLevel","bootN","bootStratified","levels","direction"];function zs(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 zi(e,t,n,r){return _e(e)/(4*n*n)/r+_e(t)/(4*r*r)/n}function uu(e){let{k10:t,k01:n}=zs(e);return zi(t,n,e.controls.length,e.cases.length)}function $s(e,t,n,r,o,s){let a=BigInt(Pi(e,t,n)),c=BigInt(Pi(r,o,s)),i=BigInt(e.length)*BigInt(t.length),l=BigInt(r.length)*BigInt(o.length);return kr(a*l-c*i,2n*i*l)}var $i=e=>e.sensitivities.some((t,n)=>t===1&&e.specificities[n]===1);function Ir(e,t){if(e.controls.length<2||e.cases.length<2)throw new Error(`${t} needs at least 2 controls and 2 cases`)}function pu(e){let t=(1-e)/2;return t<1e-8?Z(t,{lowerTail:!1}):Z(1-t)}function du(e){if(!(Number.isInteger(e)&&e>=1&&e<=2**24))throw new RangeError("bootN must be an integer from 1 to 2^24")}function _r(e){let t=new Array(e.length);for(let n=0;n<e.length;n++)t[n]=e[Ur(e.length)];return t}function ji(e){let t=Vs(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 To(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 O1(e,t,n,r){let o=To(e,t,n);return o===void 0?NaN:Di(o.controls,o.cases,r)}function qr(e){let t=new Array(e);for(let n=0;n<e;n++)t[n]=Ur(e);return t}function C1(e,t={}){V(t,L1,"ciAuc");let n=t.confLevel??.95;jt(n);let r=ae(t.method,["delong","bootstrap"]),o=new Ue;$i(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"){Ir(e,"DeLong's interval");let{k10:m,k01:u}=zs(e),h=e.auc,p=zi(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+Z(v)*g),d(h+pu(n)*g)];return W("ciAuc",{estimate:h,confInt:N,confLevel:n,method:r,variance:p,warnings:o.list})}let a=t.bootN??2e3;du(a);let c=t.bootStratified??!0,i=[],l=0;if(c){Ir(e,"a stratified bootstrap");for(let m=0;m<a;m++){let u=_r(e.controls),h=_r(e.cases);i.push(Di(u,h,e.direction))}}else{let{values:m,isCase:u}=ji(e);for(let h=0;h<a;h++){let p=O1(m,u,qr(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=Yo(i,[s/2,1-s/2]);return W("ciAuc",{estimate:e.auc,confInt:[f[0],f[1]],confLevel:n,method:r,variance:i.length>1?_e(i):NaN,warnings:o.list})}var hu=e=>typeof e=="object"&&e!==null&&!Array.isArray(e)&&"thresholds"in e&&"sensitivities"in e,js=(e,t)=>e.length===t.length&&e.every((n,r)=>n===t[r]);function F1(e,t){let n=Vs(e),r=Vs(t);if(n?.labels==null||r?.labels==null||!js(e.levels,t.levels))return;let o=c=>c.kept.map(i=>c.labels[i]);if(js(o(n),o(r))&&js(n.isCase,r.isCase))return[e,t];if(!js(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)=>Ar(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 Vi(e,t){return t==="two.sided"?2*Y(-Math.abs(e)):t==="greater"?Y(e,{lowerTail:!1}):Y(e)}function D1(e,t,n,r){Je(n)&&V(n,mu,"rocTest"),V(r,mu,"rocTest");let o,s=new Ue,a,c;if(hu(e)){if(!hu(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),M=Array.from(t),R=Array.from(n);if(w.length!==M.length||w.length!==R.length)throw new TypeError("the response and the predictors must have the same length");let y=w.map((A,S)=>S).filter(A=>{let S=w[A];return S!=null&&!(typeof S=="number"&&Number.isNaN(S))&&!Number.isNaN(M[A])&&!Number.isNaN(R[A])}),x=y.map(A=>w[A]),T={levels:o.levels,direction:o.direction};a=Ar(x,y.map(A=>M[A]),T),c=Ar(x,y.map(A=>R[A]),T);for(let A of a.warnings)s.add(A)}let i=Me(o.alternative),l=ae(o.method,["delong","bootstrap"]),f=o.confLevel??.95;jt(f),$i(a)&&$i(c)&&s.add("roc.test() of two ROC curves with AUC == 1 has always p.value = 1 and can be misleading.");let m=F1(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=$s(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."),Ir(a,"DeLong's test"),Ir(c,"DeLong's test"),u){let k=zs(a),_=zs(c),I=k.k10.map((D,O)=>D-_.k10[O]),P=k.k01.map((D,O)=>D-_.k01[O]),q=Math.sqrt(zi(I,P,a.controls.length,a.cases.length)),F=p===0&&q===0?0:p/q,C=q===0?0:pu(f)*q;return W("rocTest",{method:"DeLong's test for two correlated ROC curves",statistic:F,statisticName:"Z",pValue:Vi(F,i),confInt:[p-C,p+C],confLevel:f,...h,warnings:s.list})}let w=uu(a),M=uu(c),R=a.controls.length+a.cases.length,y=c.controls.length+c.cases.length,x=w+M,T,A,S;return x===0?(T=p===0?0:p>0?1/0:-1/0,A=NaN,S=Vi(T,i)):(T=p/Math.sqrt(x),A=x*x/(w*w/(R-1)+M*M/(y-1)),S=i==="two.sided"?2*$e(-Math.abs(T),A,!0,!1):i==="greater"?$e(T,A,!1,!1):$e(T,A,!0,!1)),W("rocTest",{method:"DeLong's test for two ROC curves",statistic:T,statisticName:"D",parameter:A,parameterName:"df",pValue:S,...h,warnings:s.list})}let g=o.bootN??2e3;du(g);let d=o.bootStratified??!0,v=[],N=0;if(d){Ir(a,"a stratified bootstrap"),Ir(c,"a stratified bootstrap");for(let w=0;w<g;w++)if(u){let M=qr(a.controls.length),R=qr(a.cases.length);v.push($s(M.map(y=>a.controls[y]),R.map(y=>a.cases[y]),a.direction,M.map(y=>c.controls[y]),R.map(y=>c.cases[y]),c.direction))}else{let M=_r(a.controls),R=_r(a.cases),y=_r(c.controls),x=_r(c.cases);v.push($s(M,R,a.direction,y,x,c.direction))}}else{let w=ji(a),M=ji(c);for(let R=0;R<g;R++){let y,x;if(u){let T=qr(w.values.length);y=To(w.values,w.isCase,T),x=To(M.values,M.isCase,T)}else y=To(w.values,w.isCase,qr(w.values.length)),x=To(M.values,M.isCase,qr(M.values.length));y===void 0||x===void 0?N++:v.push($s(y.controls,y.cases,a.direction,x.controls,x.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(_e(v)),E=p===0&&b===0?0:p/b;return W("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:Vi(E,i),bootStratified:d,...h,warnings:s.list})}var P1=["method","confLevel","prevalence"];function V1(e,t={}){V(t,P1,"diagnosticTests");let n=ae(t.method,["exact","wilson","jeffreys"]),r=t.confLevel??.95;if(ct(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(tn))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,M)=>{let R=ke(()=>xa(w,M,{method:n==="exact"?"clopper-pearson":n,confLevel:r}));return{estimate:R.estimate,confInt:R.confInt}},g=1-r,d=g/2<1e-8?Z(g/2,{lowerTail:!1}):Z(1-g/2),v=(w,M)=>{let R=d*Math.sqrt(M);return{estimate:w,confInt:[Math.exp(Math.log(w)-R),Math.exp(Math.log(w)+R)]}},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:kr(BigInt(s)*BigInt(f),BigInt(l)*BigInt(a)),c/(s*l)+i/(a*f)),lrNeg:v(i===0?c/l/b:kr(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 M=w<2**-500?2**600:1,R=N*(w*M),y=a/f*((1-w)*M),x=b*(1-w),T=c/l*w;E.ppvAtPrevalence=R/(R+y),E.npvAtPrevalence=x/(x+T)}return W("diagnosticTests",E)}var $1=["two","mode","confLevel","ciMethod"],j1=["package","exact"],ln=(e,t)=>t===0?0:e*t;function z1(e,t,n={}){if(V(n,$1,"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;jt(s);let a=ae(n.ciMethod,j1),c=[],i=[];for(let q=0;q<e.length;q++){let F=e[q],C=t[q];if(typeof F!="number"||typeof C!="number")throw new TypeError("'x' and 'y' must be numeric");if(!(Number.isNaN(F)||Number.isNaN(C))){if(!Number.isFinite(F)||!Number.isFinite(C))throw new RangeError("'x' and 'y' must not contain infinite values");c.push(F),i.push(C)}}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((q,F)=>{let C=(q+i[F])/2;return Number.isFinite(C)?C:q/2+i[F]/2}),h=f.map((q,F)=>q-m[F]),p=!h.every(Number.isFinite),{groups:g,k:d}=se([p?f.map((q,F)=>q/2-m[F]/2):h]),v=g[0],N=d+(p?1:0),b=Ce(v),E=Rn(v),w=r*E,M=b-w,R=b+w,y=l-1,x=(1-s)/2,T=Ke(x,y,!0,!1),A=x<1e-8?Ke(x,y,!1,!1):Ke((s+1)/2,y,!0,!1),S=[b+ln(T,E)/Math.sqrt(l),b+ln(A,E)/Math.sqrt(l)],k,_;if(a==="package"){let q=3*E*E/l,F=q>=2**-1022||q===0?Math.sqrt(q):E*Math.sqrt(3/l);k=[M+ln(T,F),M+ln(A,F)],_=[R+ln(T,F),R+ln(A,F)]}else{let q=r*Math.sqrt(l),F=Xs(x,y,q,!0,!1)/Math.sqrt(l),C=Xs(x,y,q,!1,!1)/Math.sqrt(l);k=[b-ln(C,E),b-ln(F,E)],_=[b+ln(F,E),b+ln(C,E)]}let I=q=>L(q,N),P=q=>[I(q[0]),I(q[1])];return W("blandAltman",{means:u,diffs:h,n:l,meanDiff:I(b),lowerLimit:I(M),upperLimit:I(R),criticalDiff:I(w),meanDiffConfInt:P(S),lowerLimitConfInt:P(k),upperLimitConfInt:P(_),confLevel:s,two:r,mode:o,ciMethod:a,warnings:[]})}var W1=new Set(["methodReg","methodCi","methodBootstrapCi","errorRatio","confLevel","slopeMeasure","nsamples","nnested","rngSeed","iterMax","threshold","naRm"]),B1=["PaBa","Deming","WDeming"],K1=["analytical","jackknife","bootstrap","nestedbootstrap"],U1=["quantile","Student","BCa","tBoot"],gu=5793,H1=2**27;function Ki(e,t){let n=e-t;return Math.abs(n)<1e-12*(Math.abs(e)+Math.abs(t))/2?0:n}function G1(e,t){let n=En(e),r=En(t),o=[0,0],s=[0,0],a=[0,0];for(let c=0;c<e.length;c++){let i=he([e[c],0],n),l=he([t[c],0],r);o=H(o,ce(i,i)),s=H(s,ce(l,l)),a=H(a,ce(i,l))}return{u:o,q:s,p:a,mx:n,my:r}}function Nu(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 Ui(e,t,n){let r=e.length,{u:o,q:s,p:a,mx:c,my:i}=G1(e,t),l=Nu(o[0],s[0],a[0],n),f=he(i,ce([l,0],c))[0],m=ce(o,s),u=a[0]*a[0]/(o[0]*s[0]),h=he(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=H(g,ce([v,0],[v,0]));let d=Math.sqrt(p*p*Ve(g,r)[0]);return{b0:f,b1:l,se0:d,se1:p,xmean:c[0]}}function wu(e,t,n,r,o,s,a,c,i){let l=e.length,f=Ui(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 M=1+n*u*u,R=1/0;for(let C=0;C<l;C++){let D=t[C]-(m+u*e[C]),O=e[C]+n*u*D/M,z=t[C]-D/M;p[C]=r>1?O/r+z:O+r*z,R=Math.min(R,Math.abs(p[C]))}let y=[0,0],x=[0,0],T=[0,0];for(let C=0;C<l;C++){let D=R/p[C],O=[D*D,0];y=H(y,O),x=H(x,ce(O,[e[C],0])),T=H(T,ce(O,[t[C],0]))}let A=Ve(x,y[0]+y[1]),S=Ve(T,y[0]+y[1]),k=[0,0],_=[0,0],I=[0,0];for(let C=0;C<l;C++){let D=Ve(he([e[C],0],A),p[C]),O=Ve(he([t[C],0],S),p[C]);k=H(k,ce(D,D)),_=H(_,ce(O,O)),I=H(I,ce(D,O))}let P=Nu(k[0],_[0],I[0],n),q=he(S,ce([P,0],A))[0];h=A[0];let F=L(Math.abs(u-P),i-c)<a&&L(Math.abs(m-q),i)<a;if(m=q,u=P,F){g=w+1;break}}let[d,v]=Wi(o),[N,b]=Wi(1+o),E=p.map(w=>{if(w===0)return 1/0;let[M,R]=Wi(Math.abs(w));return r>1?ye((N/(d*M))**2,2*(b-v-R-i)):ye((N/M)**2,2*(b-R-c))});return{b0:m,b1:u,xmean:h,weights:E,converged:g<s}}function Wi(e){let t=Math.floor(Math.log2(e));return[ye(e,-t),t]}var Cr=e=>Math.atan(e),Or=e=>Number.isFinite(e)?e:Math.tan(Cr(e));function Y1(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=Ki(e[u],e[m]),p=Ki(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=Cr(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 X1(e,t,n){let r=e.length;if(t%2!==0){let c=(t+1)/2;return c>=1&&c<=r?Or(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?(Or(s)+Or(a))/2:Math.tan((Cr(s)+Cr(a))/2)}function Q1(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 Hi(e,t,n){return Number.isFinite(n)?Q1(e.map((r,o)=>Ki(t[o],n*r))):-n}function bu(e,t,n){let r=Y1(e,t),o=X1(r.sorted,r.sorted.length+r.offset,n);return{b0:Number.isNaN(o)?NaN:Hi(e,t,o),b1:o,slopes:r}}function Z1(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?(Or(v)+Or(N))/2:Math.tan((Cr(v)+Cr(N))/2)}return Or(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:[Hi(e,t,h),Hi(e,t,u)],defined:!0}}function J1(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 Ws=e=>e.every(t=>t===e[0]);function Bi(e,t){if(!(typeof e=="number"&&Number.isInteger(e)&&e>=1))throw new RangeError(`'${t}' must be a positive integer`);return e}var Lr=(e,t)=>Yo(e,t),vu=e=>Rn(e.filter(t=>!Number.isNaN(t)));function eb(e,t,n={}){V(n,W1,"mcreg");let r=ae(n.methodReg,B1),o=ae(n.methodCi??(r==="WDeming"?"jackknife":"analytical"),K1),s=o==="bootstrap"||o==="nestedbootstrap",a=ae(n.methodBootstrapCi,U1),c=n.confLevel??.95;if(!(typeof c=="number"&&c>0&&c<1))throw new RangeError("'confLevel' must be a single number in (0, 1)");let l=ae(n.slopeMeasure,["radian","tangent"])==="tangent",f=r!=="PaBa",m=f?n.errorRatio??1:1;if(f&&!(typeof m=="number"&&m>0&&m<1/0))throw new RangeError("'errorRatio' must be a positive finite number");let u=Bi(n.nsamples??999,"nsamples"),h=Bi(n.nnested??25,"nnested"),p=Bi(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 G=0;G<e.length;G++){let K=e[G],te=t[G];if(typeof K!="number"||typeof te!="number")throw new TypeError("'x' and 'y' must be numeric");if(Number.isNaN(K)||Number.isNaN(te)){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(te))throw new RangeError("'x' and 'y' must not contain infinite values");d.push(K),v.push(te)}let N=d.length;if(N<=2)throw new RangeError("mcreg needs at least 3 complete pairs");if(Ws(d)||Ws(v))throw new RangeError("'x' and 'y' must not be constant");if(r==="PaBa"){if(d.some(K=>K<0)||v.some(K=>K<0))throw new RangeError("Passing-Bablok regression needs nonnegative values");if(N>gu)throw new RangeError(`Passing-Bablok regression is limited to ${gu} 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)>H1)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(G=>G<=0)||v.some(G=>G<=0)))throw new RangeError("weighted Deming regression needs positive values");let b=se([d,v]),[E,w]=f?[se([d]).k,se([v]).k]:[b.k,b.k],M=f?d.map(G=>L(G,-E)):b.groups[0],R=f?v.map(G=>L(G,-w)):b.groups[1],y=Math.min(Math.max(ye(m,2*(w-E)),Number.MIN_VALUE),2**1e3),x=ye(m,w-E),T=new Set,A=`no konvergence after ${p} iterations`,S=(G,K)=>{if(r==="Deming")return Ui(G,K,y);if(r==="WDeming"){let te=wu(G,K,y,x,m,p,g,E,w);return te.converged||T.add(A),te}return bu(G,K,l)},k,_=NaN,I,P,q;if(r==="Deming"){let G=Ui(M,R,y);k=G,_=L(G.xmean,E),P=[G.se0,G.se1]}else if(r==="WDeming"){let G=wu(M,R,y,x,m,p,g,E,w);G.converged||T.add(A),k=G,_=L(G.xmean,E),I=G.weights}else{let G=bu(M,R,l);k=G,q=G.slopes}if(!Number.isFinite(k.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 F=1-c,C=Ke(F/2,N-2,!1,!1),D=Z(F/2,{lowerTail:!1}),O=[k.b0,k.b1],z=[NaN,NaN],B=[[NaN,NaN],[NaN,NaN]],Q,J,oe,pe=()=>{let G=[],K=[];for(let te=0;te<N;te++){let j=S(M.filter((X,ne)=>ne!==te),R.filter((X,ne)=>ne!==te));G.push(j.b0),K.push(j.b1)}Q=[G,K],J=[(N-1)*Rn(G)/Math.sqrt(N),(N-1)*Rn(K)/Math.sqrt(N)]};if(o==="analytical")if(r==="Deming")for(let G of[0,1])z[G]=P[G],B[G]=[O[G]-C*z[G],O[G]+C*z[G]];else{let G=Z1(M,R,q,D,l);G.defined||T.add("the Passing-Bablok interval is not defined for so few points: its limits are -Inf and Inf"),B[0]=G.intercept,B[1]=G.slope}else if(o==="jackknife"){pe();for(let G of[0,1])z[G]=J[G],B[G]=[O[G]-C*z[G],O[G]+C*z[G]]}else{if(a==="BCa"&&(pe(),Q.some(ge=>ge.some(Number.isNaN))))throw new RangeError("Too many identical data points!");let G=r==="Deming"?P:r==="WDeming"&&J!==void 0?J:[NaN,NaN],K=o==="nestedbootstrap",te=[],j=[],X=[],ne=[],de=0,je=()=>Array.from({length:N},()=>Ur(N)),Oe=ge=>{let ze=ge.map(xe=>M[xe]),Ie=ge.map(xe=>R[xe]);if(Ws(ze)||Ws(Ie)){if(r==="PaBa")return T.add("Resampling PaBa regression: x or y coordinates of all data points in subsample identical - regression coefficients undetermined!"),{b0:NaN,b1:NaN};T.add("Resampling Deming regression: all x coordinates in subsample identical - regression coefficients undetermined!")}return S(ze,Ie)},Xe=()=>{for(let ge=0;ge<u;ge++){let ze=je(),Ie=Oe(ze),xe;if(K){let Fe=[],at=[];for(let lt=0;lt<h;lt++){let qt=Oe(je().map(Dt=>ze[Dt]));Fe.push(qt.b0),at.push(qt.b1)}xe=[vu(Fe),vu(at)]}else r==="Deming"?xe=[Ie.se0,Ie.se1]:xe=G;if(Number.isNaN(Ie.b0)||Number.isNaN(Ie.b1)){de++;continue}te.push(Ie.b0),j.push(Ie.b1),X.push(xe[0]),ne.push(xe[1])}};if(n.rngSeed!==void 0){let ge=Ec()?Tc():void 0;try{xc({kind:"Mersenne-Twister"}),Rc(n.rngSeed),Xe()}finally{ge!==void 0&&Ac(ge)}}else Xe();if(de>.05*u)throw new RangeError("There were too many resamples with undetermined regression coefficients!");let qe=te.length,gt=[te,j],nt=[X,ne];for(let ge of[0,1]){let ze=gt[ge],Ie=O[ge];if(a==="quantile")B[ge]=[Lr(ze,F/2),Lr(ze,1-F/2)];else if(a==="Student")z[ge]=Rn(ze),B[ge]=[Ie-C*z[ge],Ie+C*z[ge]];else if(a==="BCa"){let xe=ze.filter(Tt=>Tt<Ie).length;ze.every(Tt=>Tt===Ie)?xe=qe/2:xe===0?xe=.5:xe===qe&&(xe=qe-.5);let Fe=Z(xe/qe),at=Q[ge],lt=at.reduce((Tt,At)=>Tt+At,0)/at.length,qt=0,Dt=0;for(let Tt of at){let At=lt-Tt;qt+=At*At,Dt+=At*At*At}let Pt=qt===0?0:Dt/(6*qt**1.5),bt=-D,ft=D,It=Y(Fe-(bt-Fe)/(1+Pt*(bt-Fe))),Xt=Y(Fe-(ft-Fe)/(1+Pt*(ft-Fe)));B[ge]=[Lr(ze,Xt),Lr(ze,It)]}else{let xe=ze.map((at,lt)=>(at-Ie)/nt[ge][lt]),Fe=Number.isNaN(G[ge])?Rn(ze):G[ge];z[ge]=Fe,B[ge]=[Ie-Lr(xe,1-F/2)*Fe,Ie-Lr(xe,F/2)*Fe]}}oe={intercept:te.map(ge=>L(ge,w)),slope:j.map(ge=>L(ge,w-E)),sigmaIntercept:X.map(ge=>L(ge,w)),sigmaSlope:ne.map(ge=>L(ge,w-E)),globalSigma:[L(G[0],w),L(G[1],w-E)],nsamples:qe}}let we=G=>{let K=te=>L(te,G===0?w:w-E);return{estimate:K(O[G]),se:K(z[G]),confInt:[K(B[G][0]),K(B[G][1])]}},Ee={methodReg:r,methodCi:o,...s?{methodBootstrapCi:a}:{},confLevel:c,errorRatio:m,n:N,intercept:we(0),slope:we(1),xmean:_,...I===void 0?{}:{weights:I},...Q===void 0?{}:{jackknife:{intercept:Q[0].map(G=>L(G,w)),slope:Q[1].map(G=>L(G,w-E))}},...oe===void 0?{}:{bootstrap:oe},...r==="PaBa"?{cusum:J1(M,R,k.b0,k.b1)}:{},warnings:[...T]};return W("mcreg",Ee)}var tb=["alpha","confLevel","lmer"],nb={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 Bs(e,t){let n=e-t,r=n-e;return[n,e-(n-r)+(-t-r)]}function rb(e){let t=e[0];return e.every(n=>n.every((r,o)=>{let[s,a]=Bs(r,n[0]),[c,i]=Bs(t[o],t[0]);return s===c&&a===i}))}var yu=(e,t)=>e.every((n,r)=>n===t[r]);function or(e){let t=[0,0];for(let n of e)t=H(t,n);return Ve(t,e.length)}var Fr=(e,t)=>({v:Ve(e.v,t),e:e.e});function Dr(...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:ye(r.v[0],r.e-n),E:n}))}function Mu(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=>[ye(o.v[0],o.e-n),ye(o.v[1],o.e-n)];return{v:H(r(e),r(t)),e:n}}var Eu=(e,t)=>ye(e.v[0]/t.v[0],e.e-t.e),ob=(e,t)=>{let[n,r]=Dr(e,t);return n.at<r.at},sb=(e,t)=>{let[n,r]=Dr(e,t);return n.at<=r.at};function Ru(e,t,n){let r=[0,0];for(let o of e){let s=he(o,t);r=H(r,ce(s,s))}return ce(r,[n,0])}function ab(e,t={}){V(t,tb,"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,ct(o),r=1-o;if(!Array.isArray(e)||e.length===0||!e.every(U=>Array.isArray(U)))throw new TypeError("'x' must be a matrix (an array of rows)");let s=e[0].length;if(e.some(U=>U.length!==s))throw new TypeError("'x' must be a matrix (rows of equal length)");if(e.some(U=>U.some(me=>typeof me!="number")))throw new TypeError("'x' must contain numbers");if(e.some(U=>U.some(Number.isNaN)))throw new RangeError("'x' contains missing ratings (only complete data are supported)");if(e.some(U=>U.some(me=>!Number.isFinite(me))))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}=se(e);if(c.every(U=>yu(U,c[0])))throw new RangeError(c[0].every(U=>U===c[0][0])?"all ratings are equal":"every subject has the same ratings: the coefficients are undefined");let i=Array.from({length:s},(U,me)=>c.map(Le=>Le[me])),l=0;for(let U of e)for(let me of U)l=Math.max(l,Math.abs(me));let f=l>2**1020?3:0,m=f>0?e.map(U=>U.map(me=>me/8)):e,u=m.map(U=>U.map((me,Le)=>Bs(me,m[0][Le]))),h=m.map(U=>U.map(me=>Bs(me,U[0]))),p=h.map(U=>U.map((me,Le)=>he(me,h[0][Le]))),g=U=>{let me=0;for(let Le of U)for(let rt of Le)me=Math.max(me,Math.abs(rt[0]));return me===0?0:Math.floor(Math.log2(me))},d=(U,me)=>U.map(Le=>Le.map(rt=>[L(rt[0],-me),L(rt[1],-me)])),[v,N,b]=[g(u),g(h),g(p)],[E,w,M]=[d(u,v),d(h,N),d(p,b)],R=U=>Array.from({length:s},(me,Le)=>U.map(rt=>rt[Le])),y=Ru(E.map(or),or(E.flat()),s),x=i.every(U=>yu(U,i[0]))?[0,0]:Ru(R(w).map(or),or(w.flat()),a),T=[0,0];if(!rb(c)){let U=M.map(or),me=R(M).map(or),Le=or(M.flat());M.forEach((rt,vt)=>rt.forEach((mt,Nt)=>{let mn=H(he(he(mt,U[vt]),me[Nt]),Le);T=H(T,ce(mn,mn))}))}let A=(U,me)=>({v:U,e:2*(me+f)}),S=A(y,v),k=A(x,N),_=A(T,b),I=a-1,P=s-1,q=(a-1)*(s-1),F=Fr(S,I),C=Fr(k,P),D=Fr(_,q),O=Fr(Mu(k,_),P+q),z=[];if(n){let U={B:{ss:S,df:I,ms:F,pooled:!1},J:{ss:k,df:P,ms:C,pooled:!1}},me=_,Le=q;for(let vt of sb(F,C)?["B","J"]:["J","B"]){let mt=U[vt];ob(mt.ms,Fr(me,Le))&&(me=Mu(me,mt.ss),Le+=mt.df,mt.pooled=!0)}let rt=Fr(me,Le);if(U.B.pooled&&(F=rt),U.J.pooled&&(C=rt),D=rt,U.B.pooled){let[vt,mt]=Dr(D,C);O={v:[vt.at+(mt.at-vt.at)/a,0],e:vt.E}}}else T[0]===0&&z.push("essentially perfect fit for \u20181\u2019: summary may be unreliable");let B=(U,me)=>r/2<1e-8?Kt(r/2,U,me,!1,!1):Kt(1-r/2,U,me,!0,!1),Q=(U,me,Le)=>We(U,me,Le,!1,!1),J=U=>U===1/0?1:(U-1)/(U+s-1),oe=U=>1-1/U,pe=a*(s-1),we=Eu(F,O),Ee=Eu(F,D),[G,K]=[we/B(I,pe),we*B(pe,I)],[te,j]=[Ee/B(I,q),Ee*B(q,I)],[X,ne]=Dr(F,O).map(U=>U.at),[de,je]=Dr(F,D).map(U=>U.at),[Oe,Xe,qe]=Dr(F,C,D).map(U=>U.at),gt=(X-ne)/(X+(s-1)*ne),nt=(Oe-qe)/(Oe+(s-1)*qe+s*(Xe-qe)/a),ge=(de-je)/(de+(s-1)*je),ze=s*nt*Xe,Ie=(a*(1+(s-1)*nt)-s*nt)*qe,xe=Math.max(Math.abs(ze),Math.abs(Ie)),Fe=xe>0&&Number.isFinite(xe)?Math.floor(Math.log2(xe)):0,at=L(ze,-Fe),lt=L(Ie,-Fe),qt=(a-1)*at*at+lt*lt,Dt=qt===0?s-1:(s-1)*(a-1)*(at+lt)**2/qt,Pt=B(I,Dt),bt=B(Dt,I),ft=s*Xe+(s*a-s-a)*qe,It=(U,me)=>U>.5?1-(a*qe+ft)/me:U,Xt=It(a*(Oe/Pt-qe)/(ft+a*Oe/Pt),ft+a*Oe/Pt),Tt=bt>1?It(a*(Oe-qe/bt)/(ft/bt+a*Oe),ft+a*bt*Oe):It(a*(bt*Oe-qe)/(ft+a*bt*Oe),ft+a*bt*Oe),At=U=>U*s/(1+U*(s-1)),vn=Q(we,I,pe),Bt=Q(Ee,I,q),ve=(U,me,Le,rt,vt,mt,Nt)=>({type:U,name:nb[U],ICC:me,F:Le,df1:I,df2:rt,pValue:vt,lower:mt,upper:Nt}),Ae=[ve("ICC1",gt,we,pe,vn,J(G),J(K)),ve("ICC2",nt,Ee,q,Bt,Xt,Tt),ve("ICC3",ge,Ee,q,Bt,J(te),J(j)),ve("ICC1k",(X-ne)/X,we,pe,vn,oe(G),oe(K)),ve("ICC2k",(Oe-qe)/(Oe+(Xe-qe)/a),Ee,q,Bt,At(Xt),At(Tt)),ve("ICC3k",(de-je)/de,Ee,q,Bt,oe(te),oe(j))],Se=U=>ye(U.v[0],U.e);return W("ICC",{results:Ae,MSB:Se(F),MSJ:Se(C),MSE:Se(D),MSW:Se(O),n:a,k:s,confLevel:o,lmer:n,warnings:z})}function Ks(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(!Bc(Pc(e)))throw new RangeError("'covmat' must be symmetric")}var ib=["covmat","nObs","rotation","scores","control"],cb=["lower","start"];function tt(e){let t=0,n=0;for(let r of e){let[o,s]=kt(t,r);t=o,n+=s}return t+n}var Gi=(e,t)=>e[0].map((n,r)=>tt(e.map(o=>t(o[r]))));function Au(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 xu(e,t,n){let r=Et(Au(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 Tu(e,t){let n=Gi(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=Gi(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 fn=(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})),Pr=e=>e[0].map((t,n)=>e.map(r=>r[n]));function Us(e){let t=e[0].length,n=e.map(m=>Math.sqrt(tt(m.map(u=>u*u)))),r=e.flatMap((m,u)=>n[u]>0?[u]:[]),o=r.map(m=>e[m].map(u=>u/n[m])),s=o.length,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=Pr(o),i=0;for(let m=0;m<1e3;m++){let u=fn(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=fn(c,p),d=Ho(g);a=fn(d.u,Pr(d.v));let v=i;if(i=tt(d.d),i<v*(1+1e-5))break}let l=fn(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 lb(e){let t=Us(e),n=t.loadings,r=n.map(c=>c.map(i=>i*Math.abs(i)**3)),o=ur(et(n),r),s=Mt(fn(Pr(o),o)).map((c,i)=>c[i]),a=o.map(c=>c.map((i,l)=>i*Math.sqrt(s[l])));return{loadings:fn(n,a),rotmat:fn(t.rotmat,a)}}function fb(e,t,n={}){V(n,ib,"factanal"),V(Nn(n,"control"),cb,"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");Ks(n.covmat),o=n.covmat.map(R=>R.slice()),s=n.nObs??NaN}else{if(e===null)throw new TypeError("neither 'x' nor 'covmat' supplied");o=Go(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((R,y)=>Math.sqrt(R[y])),f=o.map((R,y)=>R.map((x,T)=>x/(l[y]*l[T]))),m=n.control?.lower??.005,u=n.control?.start?.slice()??Mt(f).map((R,y)=>(1-c/(2*a))/R[y]),h=R=>{let y=Et(Au(f,R),{symmetric:!0,onlyValues:!0}).values.slice(c);return-(tt(y.map(x=>Math.log(x)-x))-c+a)},p=R=>{let y=xu(f,R,c);return R.map((x,T)=>{let A=0;for(let S=0;S<c;S++)A+=y[T][S]*y[T][S];return(A+x-f[T][T])/(x*x)})},g=ke(()=>Qr(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=Tu(xu(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 R=b==="varimax"?Us(v):lb(v),y=Tu(R.loadings,R.rotmat);v=y.L,N=y.rot}let E=NaN,w=NaN;!Number.isNaN(s)&&i>0&&(E=(s-1-(2*a+5)/6-2*c/3)*g.value,w=hn(E,i,{lowerTail:!1}));let M={uniquenesses:d,loadings:v,STATISTIC:E,dof:i,PVAL:w,converged:!0,objective:g.value,counts:g.counts,warnings:[]};return r!=="none"&&(M.scores=mb(e,f,d,v,r)),N!==void 0&&(M.rotmat=N),M}function mb(e,t,n,r,o){let s=e.length,a=e[0].map((m,u)=>tt(e.map(h=>h[u]))/s),c=e.map(m=>m.map((u,h)=>u-a[h])),i=Gi(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 fn(l,Mt(t,r));let f=r.map((m,u)=>m.map(h=>h/n[u]));return fn(l,Pr(Mt(fn(Pr(r),f),Pr(f))))}var ub=["nfactors","covmat"],Vr=2**-52,hb=`
13
+ To do this, run the function again with the 'check.keys=TRUE' option`)}return n}function xb(e,t,n){let r=Go(e,"x").length;fa(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=yo(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=ot(i,l);for(let m=0;m<2;m++)for(let u=0;u<2;u++)c[m][u]-=De(a.map((h,p)=>o[s[m]][h]*f[p][u]))}return c[0][1]/Math.sqrt(c[0][0]*c[1][1])}function Rb(e){let t=e.length,n=e[0].length,r=i=>i.reduce((l,f)=>ue(l,[f,0]),[0,0]),o=e.map(i=>ct(r(i),n)),s=e[0].map((i,l)=>ct(r(e.map(f=>f[l])),t)),a=ct(r(e.flat()),t*n),c=[0,0];return e.forEach((i,l)=>i.forEach((f,m)=>{let u=ue(Pe(Pe([f,0],o[l]),s[m]),a);c=ue(c,_e(u,u))})),c[0]+c[1]}var Ab=["controls","cases"],Ku=["levels","direction","naRm"],Tb=["partialAuc","focus","correct"],Sb=["bestMethod","bestWeights"],Zu=new WeakMap;function ua(e){return Zu.get(e)}var kb=["auto","<",">"];function wc(e){if(e==null)return null;if(typeof e=="number")return Number.isNaN(e)?null:In(e);if(typeof e=="boolean")return e?"TRUE":"FALSE";if(typeof e=="string")return e;throw new TypeError("the response must hold numbers, strings or booleans")}function qb(e){let t=e.filter(s=>wc(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(In):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 Xo(e){return Float64Array.from(e).sort()}function bc(e,t){let n=Xo(e),r=Xo(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 vc(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 vc(bc(e,t),r,n)/(2*o*r)}function Nc(e,t,n){return vc(bc(e,t),e.length,n)}var Hu=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-(Hu(r)-Hu(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=Le(Number(c),-o);return n?-i:i}function Ib(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 _b(e,t,n){let r=bc(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:Ib(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:vc(r,o,n)/(2*s*o)}}function Lb(e,t){let n=r=>{let o=Xo(r),s=o.length>>1;return o.length%2===1?o[s]:Xt(o[s-1],o[s])};return n(e)<=n(t)?"<":">"}function Ju(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"?Lb(e,t):n,i={..._b(e,t,a),direction:a,levels:r,controls:e,cases:t,warnings:s};return Zu.set(i,o),i}function eh(e){if(e===void 0)return"auto";if(!kb.includes(e))throw new RangeError("'arg' should be one of \u201Cauto\u201D, \u201C<\u201D, \u201C>\u201D");return e}function Xr(e,t,n){let r=eh(n.direction),o=new ft,s=e.map(wc),a;if(n.levels===void 0){if(a=qb(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 w=wc(p);if(w===null)throw new RangeError("'levels' must not be missing");return w})}if(t.some(p=>typeof p!="number"))throw new TypeError("Predictor must be numeric.");if(s.length!==t.length)throw new TypeError("Response and predictor must be vectors of the same length.");let c=s.map((p,w)=>p===null||Number.isNaN(t[w]));if(n.naRm===!1&&c.some(p=>p))throw new RangeError("missing values in the response or the predictor with naRm false");let i=[],l=[],f=[],m=[],u=[];s.forEach((p,w)=>{if(c[w])return;let d=t[w];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(w)});let h={labels:s,predictor:[...t],values:f,isCase:m,kept:u};return Ju(i,l,r,[a[0],a[1]],h,o.list)}function Cb(e,t){let n=ps(_r(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=bl(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 Ob(e){return typeof e=="object"&&e!==null&&!Array.isArray(e)&&!ArrayBuffer.isView(e)}function Fb(e,t,n={}){if(xt(e)&&H(e,Ab,"roc"),typeof e!="string"&&xt(t)&&H(t,Ku,"roc"),H(n,Ku,"roc"),typeof e=="string"){let r=Cb(e,t);return Z("roc",Xr(r.response,r.predictor,n))}if(Ob(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=eh(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 Z("roc",Ju(i,l,s,["0","1"],m,[]))}return Z("roc",Xr(Array.from(e),Array.from(t),n))}var ma=(e,t,n,r,o)=>t[r]+(n-e[r])/(e[o]-e[r])*(t[o]-t[r]);function Db(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=ma(e,t,r,i,l),m=ma(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=ma(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=ma(e,t,n,l,i);s+=(e[l]-n)*(t[l]+f)/2}return s}var nr=(e,t)=>[e[0]*t[1]+t[0]*e[1],e[1]*t[1]],Rr=(e,t)=>[e[0]*t[1]-t[0]*e[1],e[1]*t[1]],Mn=(e,t)=>[e[0]*t[0],e[1]*t[1]],Gu=(e,t)=>t[0]<0n?[-e[0]*t[1],-e[1]*t[0]]:[e[0]*t[1],e[1]*t[0]],Yo=(e,t)=>{let n=e[0]*t[1]-t[0]*e[1];return n<0n?-1:n>0n?1:0};function Yu(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 Xu(e,t,n,r,o,s){let a=Yu(e,n),c=Yu(t,r);if(a!==void 0&&c!==void 0)return Qu(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(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 Qu(l.map(u),f.map(u),m,m,o,s)}function Qu(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=Qr(o),f=Qr(s),m=(d,v,N)=>nr(c(v),Mn(Gu(Rr(d,i(v)),Rr(i(N),i(v))),Rr(c(N),c(v)))),u=[];e.forEach((d,v)=>{Yo(i(v),l)>=0&&Yo(i(v),f)<=0&&u.push(v)});let h;if(u.length===0){let d=e.findIndex((N,b)=>Yo(i(b),f)>=0),v=d-1;h=Mn(nr(m(f,v,d),m(l,v,d)),Mn(Rr(f,l),[1n,2n]))}else{let d=0n;for(let b=1;b<u.length;b++){let M=u[b-1],g=u[b];d+=(e[g]-e[M])*(c(M)[0]+c(g)[0])}h=[d,2n*n*a];let v=u[u.length-1];if(Yo(i(v),f)!==0){let b=v+1;h=nr(h,Mn(Rr(f,i(v)),Mn(nr(c(v),m(f,v,b)),[1n,2n])))}let N=u[0];if(Yo(i(N),l)!==0){let b=N-1;h=nr(h,Mn(Rr(i(N),l),Mn(nr(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=Mn(Mn(Rr(f,l),nr(f,l)),[1n,2n]),w=Gu(nr(p,h),Mn(p,[2n,1n]));return Zr(w[0],w[1])}function Vb(e,t={}){H(t,Tb,"auc");let n=Ie(t.focus,["specificity","sensitivity"]),r=t.correct??!1;if(t.partialAuc===void 0)return Z("auc",{auc:e.auc,partialAuc:null,focus:n,correct:r,warnings:[]});let o=t.partialAuc;if(!Array.isArray(o)||o.length!==2||o.some(p=>typeof p!="number"))throw new TypeError("partialAuc must be a numeric vector of length 2");let s=Math.max(o[0],o[1]),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"?Xu(l,f,u,h,a,s):Xu(l,f,h,u,a,s):m=Db(l,f,a,s),Z("auc",{auc:m,partialAuc:[s,a],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 Pb(e,t){let n=Xo(e.controls),r=Xo(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 jb(e,t="all",n={}){H(n,Sb,"coords");let r=Ie(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=Ie(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}=Pb(e,i);if(l==="best"){let[d,v]=Qr(s),[N,b]=Qr(a),M=(b-N)*v,g=d*N,y=BigInt(f),x=BigInt(m),E=u.map((T,k)=>{if(r==="youden")return BigInt(T)*x*g+M*BigInt(h[k])*y;let _=BigInt(f-T),L=BigInt(m-h[k]);return-(_*_*x*x*g+M*L*L*y*y)}),R=E.reduce((T,k)=>k>T?k:T),A=E.map((T,k)=>T===R?k:-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),w=h.map(d=>m-d);return Z("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:w,npv:h.map((d,v)=>d/(d+p[v])),ppv:u.map((d,v)=>d/(d+w[v])),fpr:w.map(d=>d/m),lrPos:u.map((d,v)=>d*m/(f*w[v])),lrNeg:p.map((d,v)=>d*m/(f*h[v])),youden:u.map((d,v)=>d/f+c*(h[v]/m)),closestTopleft:p.map((d,v)=>(d/f)**2+c*(w[v]/m)**2),warnings:[]})}var $b=["confLevel","method","bootN","bootStratified"],th=["method","alternative","paired","confLevel","bootN","bootStratified","levels","direction"];function da(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 Rc(e,t,n,r){return rt(e)/(4*n*n)/r+rt(t)/(4*r*r)/n}function nh(e){let{k10:t,k01:n}=da(e);return Rc(t,n,e.controls.length,e.cases.length)}function ha(e,t,n,r,o,s){let a=BigInt(Nc(e,t,n)),c=BigInt(Nc(r,o,s)),i=BigInt(e.length)*BigInt(t.length),l=BigInt(r.length)*BigInt(o.length);return Zr(a*l-c*i,2n*i*l)}var Ec=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 oh(e){let t=(1-e)/2;return t<1e-8?pe(t,{lowerTail:!1}):pe(1-t)}function sh(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[go(e.length)];return t}function xc(e){let t=ua(e);if(t!==void 0)return{values:t.values,isCase:t.isCase};let n=[...e.controls,...e.cases];return{values:n,isCase:n.map((r,o)=>o>=e.controls.length)}}function Qo(e,t,n){let r=[],o=[];for(let s of n)(t[s]?o:r).push(e[s]);if(!(r.length===0||o.length===0))return{controls:r,cases:o}}function Wb(e,t,n,r){let o=Qo(e,t,n);return o===void 0?NaN:yc(o.controls,o.cases,r)}function Jr(e){let t=new Array(e);for(let n=0;n<e;n++)t[n]=go(e);return t}function zb(e,t={}){H(t,$b,"ciAuc");let n=t.confLevel??.95;tn(n);let r=Ie(t.method,["delong","bootstrap"]),o=new ft;Ec(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}=da(e),h=e.auc,p=Rc(m,u,e.controls.length,e.cases.length),w=Math.sqrt(p),d=b=>Math.min(1,Math.max(0,b)),v=s/2,N=n===1?[0,1]:w===0?[h,h]:[d(h+pe(v)*w),d(h+oh(n)*w)];return Z("ciAuc",{estimate:h,confInt:N,confLevel:n,method:r,variance:p,warnings:o.list})}let a=t.bootN??2e3;sh(a);let c=t.bootStratified??!0,i=[],l=0;if(c){eo(e,"a stratified bootstrap");for(let m=0;m<a;m++){let u=to(e.controls),h=to(e.cases);i.push(yc(u,h,e.direction))}}else{let{values:m,isCase:u}=xc(e);for(let h=0;h<a;h++){let p=Wb(m,u,Jr(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=Ms(i,[s/2,1-s/2]);return Z("ciAuc",{estimate:e.auc,confInt:[f[0],f[1]],confLevel:n,method:r,variance:i.length>1?rt(i):NaN,warnings:o.list})}var rh=e=>typeof e=="object"&&e!==null&&!Array.isArray(e)&&"thresholds"in e&&"sensitivities"in e,pa=(e,t)=>e.length===t.length&&e.every((n,r)=>n===t[r]);function Bb(e,t){let n=ua(e),r=ua(t);if(n?.labels==null||r?.labels==null||!pa(e.levels,t.levels))return;let o=c=>c.kept.map(i=>c.labels[i]);if(pa(o(n),o(r))&&pa(n.isCase,r.isCase))return[e,t];if(!pa(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)=>Xr(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 Mc(e,t){return t==="two.sided"?2*ce(-Math.abs(e)):t==="greater"?ce(e,{lowerTail:!1}):ce(e)}function Ub(e,t,n,r){xt(n)&&H(n,th,"rocTest"),H(r,th,"rocTest");let o,s=new ft,a,c;if(rh(e)){if(!rh(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 g=Array.from(e),y=Array.from(t),x=Array.from(n);if(g.length!==y.length||g.length!==x.length)throw new TypeError("the response and the predictors must have the same length");let E=g.map((T,k)=>k).filter(T=>{let k=g[T];return k!=null&&!(typeof k=="number"&&Number.isNaN(k))&&!Number.isNaN(y[T])&&!Number.isNaN(x[T])}),R=E.map(T=>g[T]),A={levels:o.levels,direction:o.direction};a=Xr(R,E.map(T=>y[T]),A),c=Xr(R,E.map(T=>x[T]),A);for(let T of a.warnings)s.add(T)}let i=Ze(o.alternative),l=Ie(o.method,["delong","bootstrap"]),f=o.confLevel??.95;tn(f),Ec(a)&&Ec(c)&&s.add("roc.test() of two ROC curves with AUC == 1 has always p.value = 1 and can be misleading.");let m=Bb(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=ha(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."),eo(a,"DeLong's test"),eo(c,"DeLong's test"),u){let _=da(a),L=da(c),q=_.k10.map((V,S)=>V-L.k10[S]),F=_.k01.map((V,S)=>V-L.k01[S]),C=Math.sqrt(Rc(q,F,a.controls.length,a.cases.length)),O=p===0&&C===0?0:p/C,I=C===0?0:oh(f)*C;return Z("rocTest",{method:"DeLong's test for two correlated ROC curves",statistic:O,statisticName:"Z",pValue:Mc(O,i),confInt:[p-I,p+I],confLevel:f,...h,warnings:s.list})}let g=nh(a),y=nh(c),x=a.controls.length+a.cases.length,E=c.controls.length+c.cases.length,R=g+y,A,T,k;return R===0?(A=p===0?0:p>0?1/0:-1/0,T=NaN,k=Mc(A,i)):(A=p/Math.sqrt(R),T=R*R/(g*g/(x-1)+y*y/(E-1)),k=i==="two.sided"?2*lt(-Math.abs(A),T,!0,!1):i==="greater"?lt(A,T,!1,!1):lt(A,T,!0,!1)),Z("rocTest",{method:"DeLong's test for two ROC curves",statistic:A,statisticName:"D",parameter:T,parameterName:"df",pValue:k,...h,warnings:s.list})}let w=o.bootN??2e3;sh(w);let d=o.bootStratified??!0,v=[],N=0;if(d){eo(a,"a stratified bootstrap"),eo(c,"a stratified bootstrap");for(let g=0;g<w;g++)if(u){let y=Jr(a.controls.length),x=Jr(a.cases.length);v.push(ha(y.map(E=>a.controls[E]),x.map(E=>a.cases[E]),a.direction,y.map(E=>c.controls[E]),x.map(E=>c.cases[E]),c.direction))}else{let y=to(a.controls),x=to(a.cases),E=to(c.controls),R=to(c.cases);v.push(ha(y,x,a.direction,E,R,c.direction))}}else{let g=xc(a),y=xc(c);for(let x=0;x<w;x++){let E,R;if(u){let A=Jr(g.values.length);E=Qo(g.values,g.isCase,A),R=Qo(y.values,y.isCase,A)}else E=Qo(g.values,g.isCase,Jr(g.values.length)),R=Qo(y.values,y.isCase,Jr(y.values.length));E===void 0||R===void 0?N++:v.push(ha(E.controls,E.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(rt(v)),M=p===0&&b===0?0:p/b;return Z("rocTest",{method:u?"Bootstrap test for two correlated ROC curves":"Bootstrap test for two ROC curves",statistic:M,statisticName:"D",parameter:w,parameterName:"boot.n",pValue:Mc(M,i),bootStratified:d,...h,warnings:s.list})}var Kb=["method","confLevel","prevalence"];function Hb(e,t={}){H(t,Kb,"diagnosticTests");let n=Ie(t.method,["exact","wilson","jeffreys"]),r=t.confLevel??.95;if(qt(r),!Array.isArray(e)||e.length!==2||e.some(g=>!(Array.isArray(g)||ArrayBuffer.isView(g))||g.length!==2))throw new TypeError("'table' must be a 2 x 2 table (an array of two rows of two counts)");let o=[e[0][0],e[0][1],e[1][0],e[1][1]];if(o.some(g=>typeof g!="number"))throw new TypeError("the counts must be numbers");if(!o.every(dn))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=(g,y)=>{let x=tt(()=>si(g,y,{method:n==="exact"?"clopper-pearson":n,confLevel:r}));return{estimate:x.estimate,confInt:x.confInt}},w=1-r,d=w/2<1e-8?pe(w/2,{lowerTail:!1}):pe(1-w/2),v=(g,y)=>{let x=d*Math.sqrt(y);return{estimate:g,confInt:[Math.exp(Math.log(g)-x),Math.exp(Math.log(g)+x)]}},N=s/l,b=i/f,M={sensitivity:p(s,l),specificity:p(i,f),ppv:p(s,m),npv:p(i,u),lrPos:v(a===0?N/0:Zr(BigInt(s)*BigInt(f),BigInt(l)*BigInt(a)),c/(s*l)+i/(a*f)),lrNeg:v(i===0?c/l/b:Zr(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 g=t.prevalence;if(!(typeof g=="number"&&g>0&&g<1))throw new RangeError("'prevalence' must be a number in (0, 1)");let y=g<2**-500?2**600:1,x=N*(g*y),E=a/f*((1-g)*y),R=b*(1-g),A=c/l*g;M.ppvAtPrevalence=x/(x+E),M.npvAtPrevalence=R/(R+A)}return Z("diagnosticTests",M)}var Gb=["two","mode","confLevel","ciMethod"],Yb=["package","exact"],En=(e,t)=>t===0?0:e*t;function Xb(e,t,n={}){if(H(n,Gb,"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;tn(s);let a=Ie(n.ciMethod,Yb),c=[],i=[];for(let C=0;C<e.length;C++){let O=e[C],I=t[C];if(typeof O!="number"||typeof I!="number")throw new TypeError("'x' and 'y' must be numeric");if(!(Number.isNaN(O)||Number.isNaN(I))){if(!Number.isFinite(O)||!Number.isFinite(I))throw new RangeError("'x' and 'y' must not contain infinite values");c.push(O),i.push(I)}}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((C,O)=>{let I=(C+i[O])/2;return Number.isFinite(I)?I:C/2+i[O]/2}),h=f.map((C,O)=>C-m[O]),p=!h.every(Number.isFinite),{groups:w,k:d}=Te([p?f.map((C,O)=>C/2-m[O]/2):h]),v=w[0],N=d+(p?1:0),b=st(v),M=jn(v),g=r*M,y=b-g,x=b+g,E=l-1,R=(1-s)/2,A=wt(R,E,!0,!1),T=R<1e-8?wt(R,E,!1,!1):wt((s+1)/2,E,!0,!1),k=[b+En(A,M)/Math.sqrt(l),b+En(T,M)/Math.sqrt(l)],_,L;if(a==="package"){let C=3*M*M/l,O=C>=2**-1022||C===0?Math.sqrt(C):M*Math.sqrt(3/l);_=[y+En(A,O),y+En(T,O)],L=[x+En(A,O),x+En(T,O)]}else{let C=r*Math.sqrt(l),O=Sa(R,E,C,!0,!1)/Math.sqrt(l),I=Sa(R,E,C,!1,!1)/Math.sqrt(l);_=[b-En(I,M),b-En(O,M)],L=[b+En(O,M),b+En(I,M)]}let q=C=>D(C,N),F=C=>[q(C[0]),q(C[1])];return Z("blandAltman",{means:u,diffs:h,n:l,meanDiff:q(b),lowerLimit:q(y),upperLimit:q(x),criticalDiff:q(g),meanDiffConfInt:F(k),lowerLimitConfInt:F(_),upperLimitConfInt:F(L),confLevel:s,two:r,mode:o,ciMethod:a,warnings:[]})}var Qb=new Set(["methodReg","methodCi","methodBootstrapCi","errorRatio","confLevel","slopeMeasure","nsamples","nnested","rngSeed","iterMax","threshold","naRm"]),Zb=["PaBa","Deming","WDeming"],Jb=["analytical","jackknife","bootstrap","nestedbootstrap"],ev=["quantile","Student","BCa","tBoot"],ah=5793,tv=2**27;function Sc(e,t){let n=e-t;return Math.abs(n)<1e-12*(Math.abs(e)+Math.abs(t))/2?0:n}function nv(e,t){let n=Pn(e),r=Pn(t),o=[0,0],s=[0,0],a=[0,0];for(let c=0;c<e.length;c++){let i=Pe([e[c],0],n),l=Pe([t[c],0],r);o=ue(o,_e(i,i)),s=ue(s,_e(l,l)),a=ue(a,_e(i,l))}return{u:o,q:s,p:a,mx:n,my:r}}function fh(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 kc(e,t,n){let r=e.length,{u:o,q:s,p:a,mx:c,my:i}=nv(e,t),l=fh(o[0],s[0],a[0],n),f=Pe(i,_e([l,0],c))[0],m=_e(o,s),u=a[0]*a[0]/(o[0]*s[0]),h=Pe(m,_e(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)),w=[0,0];for(let v of e)w=ue(w,_e([v,0],[v,0]));let d=Math.sqrt(p*p*ct(w,r)[0]);return{b0:f,b1:l,se0:d,se1:p,xmean:c[0]}}function ih(e,t,n,r,o,s,a,c,i){let l=e.length,f=kc(e,t,n),m=f.b0,u=f.b1,h=NaN,p=new Array(l),w=s;for(let g=0;g<s;g++){let y=1+n*u*u,x=1/0;for(let I=0;I<l;I++){let V=t[I]-(m+u*e[I]),S=e[I]+n*u*V/y,P=t[I]-V/y;p[I]=r>1?S/r+P:S+r*P,x=Math.min(x,Math.abs(p[I]))}let E=[0,0],R=[0,0],A=[0,0];for(let I=0;I<l;I++){let V=x/p[I],S=[V*V,0];E=ue(E,S),R=ue(R,_e(S,[e[I],0])),A=ue(A,_e(S,[t[I],0]))}let T=ct(R,E[0]+E[1]),k=ct(A,E[0]+E[1]),_=[0,0],L=[0,0],q=[0,0];for(let I=0;I<l;I++){let V=ct(Pe([e[I],0],T),p[I]),S=ct(Pe([t[I],0],k),p[I]);_=ue(_,_e(V,V)),L=ue(L,_e(S,S)),q=ue(q,_e(V,S))}let F=fh(_[0],L[0],q[0],n),C=Pe(k,_e([F,0],T))[0];h=T[0];let O=D(Math.abs(u-F),i-c)<a&&D(Math.abs(m-C),i)<a;if(m=C,u=F,O){w=g+1;break}}let[d,v]=Ac(o),[N,b]=Ac(1+o),M=p.map(g=>{if(g===0)return 1/0;let[y,x]=Ac(Math.abs(g));return r>1?Le((N/(d*y))**2,2*(b-v-x-i)):Le((N/y)**2,2*(b-x-c))});return{b0:m,b1:u,xmean:h,weights:M,converged:w<s}}function Ac(e){let t=Math.floor(Math.log2(e));return[Le(e,-t),t]}var oo=e=>Math.atan(e),ro=e=>Number.isFinite(e)?e:Math.tan(oo(e));function rv(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=Sc(e[u],e[m]),p=Sc(t[u],t[m]),w;if(h!==0)w=p/h;else if(p!==0)w=o?1/0:-1/0;else continue;s[a++]=w;let d=oo(w);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 ov(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],a=e[o];return n?(ro(s)+ro(a))/2:Math.tan((oo(s)+oo(a))/2)}function sv(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 qc(e,t,n){return Number.isFinite(n)?sv(e.map((r,o)=>Sc(t[o],n*r))):-n}function ch(e,t,n){let r=rv(e,t),o=ov(r.sorted,r.sorted.length+r.offset,n);return{b0:Number.isNaN(o)?NaN:qc(e,t,o),b1:o,slopes:r}}function av(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,w)=>{let d=Math.trunc((p+1)/2);if(w?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?(ro(v)+ro(N))/2:Math.tan((oo(v)+oo(N))/2)}return ro(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:[qc(e,t,h),qc(e,t,u)],defined:!0}}function iv(e,t,n,r){let o=e.length,s=e.map((p,w)=>t[w]-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,w)=>(t[w]+p/r-n)/Math.sqrt(1+1/(r*r))),m=Array.from({length:o},(p,w)=>w).sort((p,w)=>f[p]-f[w]||p-w),u=0,h=0;for(let p of m){let w=s[p];u+=w>0?i:w<0?l:0,h=Math.max(h,Math.abs(u))}return{nPos:a,nNeg:c,maxCusum:h}}var ga=e=>e.every(t=>t===e[0]);function Tc(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)=>Ms(e,t),lh=e=>jn(e.filter(t=>!Number.isNaN(t)));function cv(e,t,n={}){H(n,Qb,"mcreg");let r=Ie(n.methodReg,Zb),o=Ie(n.methodCi??(r==="WDeming"?"jackknife":"analytical"),Jb),s=o==="bootstrap"||o==="nestedbootstrap",a=Ie(n.methodBootstrapCi,ev),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=Ie(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=Tc(n.nsamples??999,"nsamples"),h=Tc(n.nnested??25,"nnested"),p=Tc(n.iterMax??30,"iterMax"),w=n.threshold??1e-6;if(!(typeof w=="number"&&w>=0&&w<1/0))throw new RangeError("'threshold' must be a nonnegative number");if(r==="WDeming"&&o==="analytical")throw new RangeError('there is no analytical interval for weighted Deming regression (use methodCi "jackknife" or a bootstrap)');if(o==="bootstrap"&&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 re=0;re<e.length;re++){let j=e[re],K=t[re];if(typeof j!="number"||typeof K!="number")throw new TypeError("'x' and 'y' must be numeric");if(Number.isNaN(j)||Number.isNaN(K)){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(j)||!Number.isFinite(K))throw new RangeError("'x' and 'y' must not contain infinite values");d.push(j),v.push(K)}let N=d.length;if(N<=2)throw new RangeError("mcreg needs at least 3 complete pairs");if(ga(d)||ga(v))throw new RangeError("'x' and 'y' must not be constant");if(r==="PaBa"){if(d.some(j=>j<0)||v.some(j=>j<0))throw new RangeError("Passing-Bablok regression needs nonnegative values");if(N>ah)throw new RangeError(`Passing-Bablok regression is limited to ${ah} 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)>tv)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(re=>re<=0)||v.some(re=>re<=0)))throw new RangeError("weighted Deming regression needs positive values");let b=Te([d,v]),[M,g]=f?[Te([d]).k,Te([v]).k]:[b.k,b.k],y=f?d.map(re=>D(re,-M)):b.groups[0],x=f?v.map(re=>D(re,-g)):b.groups[1],E=Math.min(Math.max(Le(m,2*(g-M)),Number.MIN_VALUE),2**1e3),R=Le(m,g-M),A=new Set,T=`no konvergence after ${p} iterations`,k=(re,j)=>{if(r==="Deming")return kc(re,j,E);if(r==="WDeming"){let K=ih(re,j,E,R,m,p,w,M,g);return K.converged||A.add(T),K}return ch(re,j,l)},_,L=NaN,q,F,C;if(r==="Deming"){let re=kc(y,x,E);_=re,L=D(re.xmean,M),F=[re.se0,re.se1]}else if(r==="WDeming"){let re=ih(y,x,E,R,m,p,w,M,g);re.converged||A.add(T),_=re,L=D(re.xmean,M),q=re.weights}else{let re=ch(y,x,l);_=re,C=re.slopes}if(!Number.isFinite(_.b1))throw new RangeError(r==="PaBa"?"the Passing-Bablok slope is not defined for these data":"the covariance of x and y is 0: the Deming slope is not defined");let O=1-c,I=wt(O/2,N-2,!1,!1),V=pe(O/2,{lowerTail:!1}),S=[_.b0,_.b1],P=[NaN,NaN],G=[[NaN,NaN],[NaN,NaN]],Q,te,$,J=()=>{let re=[],j=[];for(let K=0;K<N;K++){let W=k(y.filter((U,z)=>z!==K),x.filter((U,z)=>z!==K));re.push(W.b0),j.push(W.b1)}Q=[re,j],te=[(N-1)*jn(re)/Math.sqrt(N),(N-1)*jn(j)/Math.sqrt(N)]};if(o==="analytical")if(r==="Deming")for(let re of[0,1])P[re]=F[re],G[re]=[S[re]-I*P[re],S[re]+I*P[re]];else{let re=av(y,x,C,V,l);re.defined||A.add("the Passing-Bablok interval is not defined for so few points: its limits are -Inf and Inf"),G[0]=re.intercept,G[1]=re.slope}else if(o==="jackknife"){J();for(let re of[0,1])P[re]=te[re],G[re]=[S[re]-I*P[re],S[re]+I*P[re]]}else{if(a==="BCa"&&(J(),Q.some(de=>de.some(Number.isNaN))))throw new RangeError("Too many identical data points!");let re=r==="Deming"?F:r==="WDeming"&&te!==void 0?te:[NaN,NaN],j=o==="nestedbootstrap",K=[],W=[],U=[],z=[],ne=0,le=()=>Array.from({length:N},()=>go(N)),Re=de=>{let Oe=de.map(Ve=>y[Ve]),Ee=de.map(Ve=>x[Ve]);if(ga(Oe)||ga(Ee)){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 k(Oe,Ee)},Ce=()=>{for(let de=0;de<u;de++){let Oe=le(),Ee=Re(Oe),Ve;if(j){let Fe=[],Ue=[];for(let $e=0;$e<h;$e++){let Je=Re(le().map(Ke=>Oe[Ke]));Fe.push(Je.b0),Ue.push(Je.b1)}Ve=[lh(Fe),lh(Ue)]}else r==="Deming"?Ve=[Ee.se0,Ee.se1]:Ve=re;if(Number.isNaN(Ee.b0)||Number.isNaN(Ee.b1)){ne++;continue}K.push(Ee.b0),W.push(Ee.b1),U.push(Ve[0]),z.push(Ve[1])}};if(n.rngSeed!==void 0){let de=fl()?hl():void 0;try{ul({kind:"Mersenne-Twister"}),ml(n.rngSeed),Ce()}finally{de!==void 0&&pl(de)}}else Ce();if(ne>.05*u)throw new RangeError("There were too many resamples with undetermined regression coefficients!");let be=K.length,fe=[K,W],ke=[U,z];for(let de of[0,1]){let Oe=fe[de],Ee=S[de];if(a==="quantile")G[de]=[no(Oe,O/2),no(Oe,1-O/2)];else if(a==="Student")P[de]=jn(Oe),G[de]=[Ee-I*P[de],Ee+I*P[de]];else if(a==="BCa"){let Ve=Oe.filter(ie=>ie<Ee).length;Oe.every(ie=>ie===Ee)?Ve=be/2:Ve===0?Ve=.5:Ve===be&&(Ve=be-.5);let Fe=pe(Ve/be),Ue=Q[de],$e=Ue.reduce((ie,et)=>ie+et,0)/Ue.length,Je=0,Ke=0;for(let ie of Ue){let et=$e-ie;Je+=et*et,Ke+=et*et*et}let Xe=Je===0?0:Ke/(6*Je**1.5),he=-V,oe=V,we=ce(Fe-(he-Fe)/(1+Xe*(he-Fe))),ge=ce(Fe-(oe-Fe)/(1+Xe*(oe-Fe)));G[de]=[no(Oe,ge),no(Oe,we)]}else{let Ve=Oe.map((Ue,$e)=>(Ue-Ee)/ke[de][$e]),Fe=Number.isNaN(re[de])?jn(Oe):re[de];P[de]=Fe,G[de]=[Ee-no(Ve,1-O/2)*Fe,Ee-no(Ve,O/2)*Fe]}}$={intercept:K.map(de=>D(de,g)),slope:W.map(de=>D(de,g-M)),sigmaIntercept:U.map(de=>D(de,g)),sigmaSlope:z.map(de=>D(de,g-M)),globalSigma:[D(re[0],g),D(re[1],g-M)],nsamples:be}}let ee=re=>{let j=K=>D(K,re===0?g:g-M);return{estimate:j(S[re]),se:j(P[re]),confInt:[j(G[re][0]),j(G[re][1])]}},Se={methodReg:r,methodCi:o,...s?{methodBootstrapCi:a}:{},confLevel:c,errorRatio:m,n:N,intercept:ee(0),slope:ee(1),xmean:L,...q===void 0?{}:{weights:q},...Q===void 0?{}:{jackknife:{intercept:Q[0].map(re=>D(re,g)),slope:Q[1].map(re=>D(re,g-M))}},...$===void 0?{}:{bootstrap:$},...r==="PaBa"?{cusum:iv(y,x,_.b0,_.b1)}:{},warnings:[...A]};return Z("mcreg",Se)}var lv=["alpha","confLevel","lmer"],fv={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 wa(e,t){let n=e-t,r=n-e;return[n,e-(n-r)+(-t-r)]}function mv(e){let t=e[0];return e.every(n=>n.every((r,o)=>{let[s,a]=wa(r,n[0]),[c,i]=wa(t[o],t[0]);return s===c&&a===i}))}var mh=(e,t)=>e.every((n,r)=>n===t[r]);function Ar(e){let t=[0,0];for(let n of e)t=ue(t,n);return ct(t,e.length)}var so=(e,t)=>({v:ct(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:Le(r.v[0],r.e-n),E:n}))}function uh(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=>[Le(o.v[0],o.e-n),Le(o.v[1],o.e-n)];return{v:ue(r(e),r(t)),e:n}}var hh=(e,t)=>Le(e.v[0]/t.v[0],e.e-t.e),uv=(e,t)=>{let[n,r]=ao(e,t);return n.at<r.at},hv=(e,t)=>{let[n,r]=ao(e,t);return n.at<=r.at};function ph(e,t,n){let r=[0,0];for(let o of e){let s=Pe(o,t);r=ue(r,_e(s,s))}return _e(r,[n,0])}function pv(e,t={}){H(t,lv,"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,qt(o),r=1-o;if(!Array.isArray(e)||e.length===0||!e.every(X=>Array.isArray(X)))throw new TypeError("'x' must be a matrix (an array of rows)");let s=e[0].length;if(e.some(X=>X.length!==s))throw new TypeError("'x' must be a matrix (rows of equal length)");if(e.some(X=>X.some(ve=>typeof ve!="number")))throw new TypeError("'x' must contain numbers");if(e.some(X=>X.some(Number.isNaN)))throw new RangeError("'x' contains missing ratings (only complete data are supported)");if(e.some(X=>X.some(ve=>!Number.isFinite(ve))))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}=Te(e);if(c.every(X=>mh(X,c[0])))throw new RangeError(c[0].every(X=>X===c[0][0])?"all ratings are equal":"every subject has the same ratings: the coefficients are undefined");let i=Array.from({length:s},(X,ve)=>c.map(ae=>ae[ve])),l=0;for(let X of e)for(let ve of X)l=Math.max(l,Math.abs(ve));let f=l>2**1020?3:0,m=f>0?e.map(X=>X.map(ve=>ve/8)):e,u=m.map(X=>X.map((ve,ae)=>wa(ve,m[0][ae]))),h=m.map(X=>X.map(ve=>wa(ve,X[0]))),p=h.map(X=>X.map((ve,ae)=>Pe(ve,h[0][ae]))),w=X=>{let ve=0;for(let ae of X)for(let Ae of ae)ve=Math.max(ve,Math.abs(Ae[0]));return ve===0?0:Math.floor(Math.log2(ve))},d=(X,ve)=>X.map(ae=>ae.map(Ae=>[D(Ae[0],-ve),D(Ae[1],-ve)])),[v,N,b]=[w(u),w(h),w(p)],[M,g,y]=[d(u,v),d(h,N),d(p,b)],x=X=>Array.from({length:s},(ve,ae)=>X.map(Ae=>Ae[ae])),E=ph(M.map(Ar),Ar(M.flat()),s),R=i.every(X=>mh(X,i[0]))?[0,0]:ph(x(g).map(Ar),Ar(g.flat()),a),A=[0,0];if(!mv(c)){let X=y.map(Ar),ve=x(y).map(Ar),ae=Ar(y.flat());y.forEach((Ae,Be)=>Ae.forEach((je,nt)=>{let ut=ue(Pe(Pe(je,X[Be]),ve[nt]),ae);A=ue(A,_e(ut,ut))}))}let T=(X,ve)=>({v:X,e:2*(ve+f)}),k=T(E,v),_=T(R,N),L=T(A,b),q=a-1,F=s-1,C=(a-1)*(s-1),O=so(k,q),I=so(_,F),V=so(L,C),S=so(uh(_,L),F+C),P=[];if(n){let X={B:{ss:k,df:q,ms:O,pooled:!1},J:{ss:_,df:F,ms:I,pooled:!1}},ve=L,ae=C;for(let Be of hv(O,I)?["B","J"]:["J","B"]){let je=X[Be];uv(je.ms,so(ve,ae))&&(ve=uh(ve,je.ss),ae+=je.df,je.pooled=!0)}let Ae=so(ve,ae);if(X.B.pooled&&(O=Ae),X.J.pooled&&(I=Ae),V=Ae,X.B.pooled){let[Be,je]=ao(V,I);S={v:[Be.at+(je.at-Be.at)/a,0],e:Be.E}}}else A[0]===0&&P.push("essentially perfect fit for \u20181\u2019: summary may be unreliable");let G=(X,ve)=>r/2<1e-8?on(r/2,X,ve,!1,!1):on(1-r/2,X,ve,!0,!1),Q=(X,ve,ae)=>pt(X,ve,ae,!1,!1),te=X=>X===1/0?1:(X-1)/(X+s-1),$=X=>1-1/X,J=a*(s-1),ee=hh(O,S),Se=hh(O,V),[re,j]=[ee/G(q,J),ee*G(J,q)],[K,W]=[Se/G(q,C),Se*G(C,q)],[U,z]=ao(O,S).map(X=>X.at),[ne,le]=ao(O,V).map(X=>X.at),[Re,Ce,be]=ao(O,I,V).map(X=>X.at),fe=(U-z)/(U+(s-1)*z),ke=(Re-be)/(Re+(s-1)*be+s*(Ce-be)/a),de=(ne-le)/(ne+(s-1)*le),Oe=s*ke*Ce,Ee=(a*(1+(s-1)*ke)-s*ke)*be,Ve=Math.max(Math.abs(Oe),Math.abs(Ee)),Fe=Ve>0&&Number.isFinite(Ve)?Math.floor(Math.log2(Ve)):0,Ue=D(Oe,-Fe),$e=D(Ee,-Fe),Je=(a-1)*Ue*Ue+$e*$e,Ke=Je===0?s-1:(s-1)*(a-1)*(Ue+$e)**2/Je,Xe=G(q,Ke),he=G(Ke,q),oe=s*Ce+(s*a-s-a)*be,we=(X,ve)=>X>.5?1-(a*be+oe)/ve:X,ge=we(a*(Re/Xe-be)/(oe+a*Re/Xe),oe+a*Re/Xe),ie=he>1?we(a*(Re-be/he)/(oe/he+a*Re),oe+a*he*Re):we(a*(he*Re-be)/(oe+a*he*Re),oe+a*he*Re),et=X=>X*s/(1+X*(s-1)),Y=Q(ee,q,J),me=Q(Se,q,C),Ne=(X,ve,ae,Ae,Be,je,nt)=>({type:X,name:fv[X],ICC:ve,F:ae,df1:q,df2:Ae,pValue:Be,lower:je,upper:nt}),ye=[Ne("ICC1",fe,ee,J,Y,te(re),te(j)),Ne("ICC2",ke,Se,C,me,ge,ie),Ne("ICC3",de,Se,C,me,te(K),te(W)),Ne("ICC1k",(U-z)/U,ee,J,Y,$(re),$(j)),Ne("ICC2k",(Re-be)/(Re+(Ce-be)/a),Se,C,me,et(ge),et(ie)),Ne("ICC3k",(ne-le)/ne,Se,C,me,$(K),$(W))],Me=X=>Le(X.v[0],X.e);return Z("ICC",{results:ye,MSB:Me(O),MSJ:Me(I),MSE:Me(V),MSW:Me(S),n:a,k:s,confLevel:o,lmer:n,warnings:P})}function ba(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(!Il(Rl(e)))throw new RangeError("'covmat' must be symmetric")}var dv=["covmat","nObs","rotation","scores","control"],gv=["lower","start"];function At(e){let t=0,n=0;for(let r of e){let[o,s]=zt(t,r);t=o,n+=s}return t+n}var Ic=(e,t)=>e[0].map((n,r)=>At(e.map(o=>t(o[r]))));function wh(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 dh(e,t,n){let r=Vt(wh(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 gh(e,t){let n=Ic(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=Ic(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 xn=(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})),io=e=>e[0].map((t,n)=>e.map(r=>r[n]));function va(e){let t=e[0].length,n=e.map(m=>Math.sqrt(At(m.map(u=>u*u)))),r=e.flatMap((m,u)=>n[u]>0?[u]:[]),o=r.map(m=>e[m].map(u=>u/n[m])),s=o.length,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=io(o),i=0;for(let m=0;m<1e3;m++){let u=xn(o,a),h=u[0].map((N,b)=>{let M=0;for(let g=0;g<s;g++)M+=u[g][b]*u[g][b];return M}),p=u.map(N=>N.map((b,M)=>b**3-b*h[M]/s)),w=xn(c,p),d=ys(w);a=xn(d.u,io(d.v));let v=i;if(i=At(d.d),i<v*(1+1e-5))break}let l=xn(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 wv(e){let t=va(e),n=t.loadings,r=n.map(c=>c.map(i=>i*Math.abs(i)**3)),o=Or(Rt(n),r),s=ot(xn(io(o),o)).map((c,i)=>c[i]),a=o.map(c=>c.map((i,l)=>i*Math.sqrt(s[l])));return{loadings:xn(n,a),rotmat:xn(t.rotmat,a)}}function bv(e,t,n={}){H(n,dv,"factanal"),H(vn(n,"control"),gv,"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");ba(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=Ns(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,E)=>Math.sqrt(x[E])),f=o.map((x,E)=>x.map((R,A)=>R/(l[E]*l[A]))),m=n.control?.lower??.005,u=n.control?.start?.slice()??ot(f).map((x,E)=>(1-c/(2*a))/x[E]),h=x=>{let E=Vt(wh(f,x),{symmetric:!0,onlyValues:!0}).values.slice(c);return-(At(E.map(R=>Math.log(R)-R))-c+a)},p=x=>{let E=dh(f,x,c);return x.map((R,A)=>{let T=0;for(let k=0;k<c;k++)T+=E[A][k]*E[A][k];return(T+R-f[A][A])/(R*R)})},w=tt(()=>No(u,h,{gr:p,method:"L-BFGS-B",lower:m,upper:1,control:{fnscale:1,parscale:new Array(a).fill(.01)}}));if(w.convergence!==0)throw new Error("unable to optimize from this starting value");let d=w.par,v=gh(dh(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"?va(v):wv(v),E=gh(x.loadings,x.rotmat);v=E.L,N=E.rot}let M=NaN,g=NaN;!Number.isNaN(s)&&i>0&&(M=(s-1-(2*a+5)/6-2*c/3)*w.value,g=Bt(M,i,{lowerTail:!1}));let y={uniquenesses:d,loadings:v,STATISTIC:M,dof:i,PVAL:g,converged:!0,objective:w.value,counts:w.counts,warnings:[]};return r!=="none"&&(y.scores=vv(e,f,d,v,r)),N!==void 0&&(y.rotmat=N),y}function vv(e,t,n,r,o){let s=e.length,a=e[0].map((m,u)=>At(e.map(h=>h[u]))/s),c=e.map(m=>m.map((u,h)=>u-a[h])),i=Ic(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 xn(l,ot(t,r));let f=r.map((m,u)=>m.map(h=>h/n[u]));return xn(l,io(ot(xn(io(r),f),io(f))))}var yv=["nfactors","covmat"],co=2**-52,Nv=`
14
14
  Three factors are required for identification -- general factor loadings set to be equal.
15
15
  Proceed with caution.
16
16
  Think about redoing the analysis with alternative values of the 'option' setting.
17
17
 
18
- `,$r=(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})),Ao=e=>e[0].map((t,n)=>e.map(r=>r[n])),So=(e,t=n=>n)=>e[0].map((n,r)=>tt(e.map(o=>t(o[r])))),ku=(e,t=n=>n)=>e.map(n=>tt(n.map(t))),jr=(e,t=n=>n)=>tt(e[0].flatMap((n,r)=>e.map(o=>t(o[r])))),Hs=e=>e<0?-1:1;function Qi(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 qu(e,t){let n=Qi(e),{d:r,u:o,v:s}=Ho(n),a=r.map((i,l)=>l).filter(i=>r[i]>Math.sqrt(Vr)*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)&&Vn(t,"In smc, smcs > 1 were set to 1.0"),c.some(i=>i<0)&&Vn(t,"In smc, smcs < 0 were set to .0"),c.map(i=>i>1?1:i<0?0:i)}var Vn=(e,t)=>{e.includes(t)||e.push(t)};function Yi(e,t,n,r){let o=e.map((c,i)=>c.map((l,f)=>i===f?t[i]:l)),s=Et(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 pb=e=>$r(e,Ao(e));function Su(e,t,n,r){let o=Qi(e),s=o.length,a=qu(o,r),c=d=>{let v=Yi(o,d.map(E=>1-E),t,E=>E<Vr?100*Vr:E),N=pb(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 tt(b)},i=d=>{let v=Yi(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=Et(o.map((T,A)=>T.map((S,k)=>A===k?1-d[A]:S)),{symmetric:!0}),N=v.values,b=v.vectors,E=N.map(T=>T>=Vr),w=N.map(T=>T<Vr?100*Vr:T),M=o.map((T,A)=>T.map((S,k)=>{if(A===k)return 0;let _=0;for(let I=0;I<t;I++)_+=w[I]*b[A][I]*b[k][I];return S-_})),R=$r(M,b),y=(T,A)=>{let S=0;for(let k=0;k<s;k++)S+=b[k][T]*R[k][A];return S},x=new Array(s).fill(0);for(let T=0;T<t;T++){let A=(S,k)=>{if(k===0)return;let _=k*y(T,S);for(let I=0;I<s;I++)x[I]+=_*b[I][T]*b[I][S]};E[T]&&A(T,1);for(let S=T+1;S<t;S++)A(S,2*(E[T]&&E[S]?1:(w[T]-w[S])/(N[T]-N[S])));for(let S=t;S<s;S++)A(S,2*w[T]/(N[T]-N[S]))}return x},f=.005,m={method:"L-BFGS-B",lower:f,upper:Math.max(Be(a),1)},u=new Array(s).fill(.01),h=ke(()=>Qr(a.map(d=>1-d),c,{gr:i,...m,control:{fnscale:1,parscale:u}}));h.par.some(d=>d===f)&&(h=ke(()=>Qr(h.par,c,{gr:l,...m,control:{fnscale:1,parscale:u,factr:1e3}}))),h.convergence!==0&&Vn(n,`minres did not converge (optim convergence code ${h.convergence}): examine the results carefully`);let p=h.par,g=Yi(o,p.map((d,v)=>o[v][v]-d),t,d=>Math.max(d,0));if(g=Xi(g,t),g=g.map(d=>d.map(v=>v===0?1e-15:v)),ku(g,d=>d*d).some(d=>d>1)&&Vn(n,"An ultra-Heywood case was detected. Examine the results carefully"),t>1){g=Xi(Us(g).loadings,t);let d=So(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=Xi(g,t);return g}function Xi(e,t){let n=So(e).map(Hs);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 db(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:jr(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 gb(e){let t=e[0].length,n=l=>{let f=Mt(l),m=$r(e,Ao(f)),{f:u,Gq:h}=db(m),p=Ao($r($r(Ao(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=So(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(jr(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,M)=>w-d.T[E][M])),N=m.map((b,E)=>b.map((w,M)=>w-d.Gp[E][M]));jr(N,b=>b*b)>0&&(o=jr(v,b=>b*b)/Math.abs(jr(v.map((b,E)=>b.map((w,M)=>w*N[E][M])))),o=Math.max(1e-10,Math.min(o,20)))}else o=2*o;let h=Be(c.slice(-10)),p=s,g=r;for(let d=0;d<=10;d++){let v=r.map((b,E)=>b.map((w,M)=>w-o*m[E][M])),N=So(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:$r(Ao(r),r),converged:i}}function wb(e,t={}){V(t,ub,"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)Ks(t.covmat),r=Qi(t.covmat);else{if(e===null)throw new TypeError("neither 'x' nor 'covmat' supplied");r=Xr(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=Su(r,n,s,a),i,l,f;if(n===1)i=c.map(O=>O.slice()),f=[1],Vn(a,`Omega_h for 1 factor is not meaningful, just omega_t
19
- `),Vn(s,"Omega_h and Omega_asymptotic are not meaningful with one factor");else{let O=gb(c);if(O.converged||Vn(s,"oblimin did not converge in 2000 iterations"),i=O.loadings,l=O.phi,n===2){let z=l[0][1],B=Math.sqrt(Math.abs(z));f=[B,Hs(z)*B],Vn(a,hb)}else f=Su(l,1,s,a).map(z=>z[0])}let m=i.map(O=>{let z=0;for(let B=0;B<n;B++)z+=O[B]*f[B];return z}),u=ku(c,O=>O*O),h=u.map((O,z)=>r[z][z]-O),p=i.map((O,z)=>O.map((B,Q)=>{if(n===1)return 0;let J=r[z][z],oe=J-(J-B*B)-(B*f[Q])**2;return oe<0&&(oe=0),Hs(B)*Math.sqrt(oe)})),g=m.map((O,z)=>O*O/u[z]),d=m.map((O,z)=>{let B=[O,...p[z]];return tt(B.map(Q=>Q*Q))**2/tt(B.map(Q=>Q**4))}),v=m.map(Hs);v.some(O=>O<0)&&(r=r.map((O,z)=>O.map((B,Q)=>v[z]*B*v[Q])));let N=m.map((O,z)=>v[z]*O),b=p.map((O,z)=>O.map(B=>v[z]*B)),E=N.map((O,z)=>[O,...b[z],u[z],h[z],g[z],d[z]]),w=jr(r),M=tt(r.map((O,z)=>O[z])),R=tt(N)**2,y=tt(h),x=R/w,T=(w-y)/w,A=R/(w-y),S=(w-M)/w*o/(o-1),k=(w+tt(qu(r,a))-M)/w,_=So(E.map(O=>O.slice(0,n+1)),O=>O*O),I=_[0]/tt(_),P=b.map(O=>{let z=0;for(let B=1;B<n;B++)Math.abs(O[B])>Math.abs(O[z])&&(z=B);return z}),q=[],F=[];for(let O=0;O<n;O++){let z=P.map((oe,pe)=>pe).filter(oe=>P[oe]===O),B=tt(z.flatMap(oe=>z.map(pe=>r[pe][oe]))),Q=tt(z.map(oe=>b[oe][O])),J=tt(z.map(oe=>N[oe]));F.push(Q*Q),q.push([(Q*Q+J*J)/B,J*J/B,Q*Q/B])}let C=[[T,tt(N)**2/w,tt(F)/w],...q],D={omegaH:x,omegaLim:A,alpha:S,omegaTot:T,G6:k,ECV:I,sl:E,slNames:["g",...Array.from({length:n},(O,z)=>`F${z+1}*`),"h2","u2","p2","com"],orthog:c,oblique:i,gloading:f,key:v,omegaGroup:C,warnings:s,messages:a};return l!==void 0&&(D.phi=l),W("omega",D)}var Iu=["ML","stdErr"],bb=["ML","stdErr"],Ft=.9999;function Ji(e){let t=Ne(e),n=[],r=0;for(let o=0;o<e.length-1;o++)r+=e[o],n.push(Z(r/t));return n}var ec=e=>[...new Set(e)].sort((t,n)=>t-n),_u=(e,t)=>e===1?`1 ${t} with zero marginal removed`:`${e} ${t}s with zero marginals removed`,Lu=e=>Array.isArray(e)||ArrayBuffer.isView(e),Ou=(e,t)=>e>0?Y(e,{lowerTail:!1})-Y(t,{lowerTail:!1}):Y(t)-Y(e);function tc(e){let t=se([e]).groups[0],n=Ce(t),r=t.map(s=>s-n),o=Math.sqrt(_e(r));return r.map(s=>s/o)}function Cu(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 Gs=e=>Array.from({length:e},()=>new Array(e).fill(0));function Pu(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 Zi={r:0,h:0,k:0,rr:0,rh:0,rk:0,hh:0,hk:0,kk:0};function vb(e,t,n){if(e===-1/0||t===-1/0||e===1/0&&t===1/0)return Zi;if(e===1/0)return{...Zi,k:_t(t),kk:-t*_t(t)};if(t===1/0)return{...Zi,h:_t(e),hh:-e*_t(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=_t(e),f=_t(t);return{r:a,h:l*Y(c),k:f*Y(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*Y(c)-n/o*l*_t(c),hk:a,kk:-t*f*Y(i)-n/o*f*_t(i)}}function Fu(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:Gs(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=>vb(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=Kr([t[h],n[p]],[t[h+1],n[p+1]],r),v=new Array(c).fill(0),N=Gs(c);for(let[b,E,w]of[[h+1,p+1,1],[h,p+1,-1],[h+1,p,-1],[h,p,1]]){let M=m[b][E],R=l(b),y=f(E);v[0]+=w*M.r,N[0][0]+=w*M.rr,R>=0&&(v[R]+=w*M.h,N[0][R]+=w*M.rh,N[R][0]+=w*M.rh,N[R][R]+=w*M.hh),y>=0&&(v[y]+=w*M.k,N[0][y]+=w*M.rk,N[y][0]+=w*M.rk,N[y][y]+=w*M.kk),R>=0&&y>=0&&(N[R][y]+=w*M.hk,N[y][R]+=w*M.hk)}Pu(i,g,d,v,N),u.push(g*Math.log(d))}return i.value=Ne(u),i}function Nb(e,t,n,r,o){let s=o?n.length+1:1,a={value:0,gradient:new Array(s).fill(0),hessian:Gs(s)},c=(1-r)*(1+r),i=Math.sqrt(c),l=c*i,f=new Array(s),m=Gs(s),u=[],h=(p,g,d)=>{let v=n[p],N=(v-r*g)/i,b=_t(N),E=(r*v-g)/l,w=1/i,M=v/l+3*r*(r*v-g)/(l*c),R=r/l;if(f[0]+=d*b*E,m[0][0]+=d*b*(M-N*E*E),o){let y=p+1;f[y]+=d*b*w;let x=d*b*(R-N*E*w);m[0][y]+=x,m[y][0]+=x,m[y][y]+=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),Pu(a,1,Ou(N,v),f,m),u.push(Math.log(Ou(N,v)),_t(g,{log:!0}))}return a.value=Ne(u),a}function yb(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=Mt(o,r)}catch{s=void 0}return(s===void 0||!s.every(Number.isFinite)||Ne(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 Vu=(e,t)=>e>=t-1e-12*Math.abs(t);function Du(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=yb(o,i);if(!s&&Math.abs(r[0])===Ft&&l[0]*r[0]>0){s=!0;continue}let f=Math.abs(r[0]+l[0])>Ft?(Math.sign(l[0])*Ft-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])>=Ft*(1-1e-15)&&(h[0]=Math.sign(h[0])*Ft);let p=e(h);if(Vu(p.value,o.value)){let g=Be(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 $u(e,t){let n=Du(e,[Math.max(-Ft,Math.min(Ft,t[0])),...t.slice(1)],!1);if(n.bounded||n.theta[0]===0)return n;let r=Du(e,[Math.sign(n.theta[0])*Ft,...n.theta.slice(1)],!0);return Vu(r.at.value,n.at.value)?r:n}function ju(e,t){let n=()=>e.map(o=>o.map(()=>1/0));if(t)return n();let r;try{r=Mt(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 zu=e=>`inadmissible correlation set to ${e>0?"0.9999":"-0.9999"}`;function Mb(e,t,n){Je(t)&&V(t,Iu,"polychor"),V(n,Iu,"polychor");let r,o;if(Lu(t)){let q=e,F=t;if(q.length!==F.length)throw new RangeError("'x' and 'y' must have the same length");let C=q.map((Q,J)=>!Number.isNaN(Q)&&!Number.isNaN(F[J])),D=dt(q.filter((Q,J)=>C[J])),O=dt(F.filter((Q,J)=>C[J])),z=new Map(ec(D).map((Q,J)=>[Q,J])),B=new Map(ec(O).map((Q,J)=>[Q,J]));r=Array.from({length:z.size},()=>new Array(B.size).fill(0)),D.forEach((Q,J)=>r[z.get(Q)][B.get(O[J])]++),o=n??{}}else{let q=e;if(!q.every(Lu))throw new TypeError("give a table (array of rows) or two vectors");let F=q[0]?.length??0;if(q.some(C=>C.length!==F))throw new TypeError("the table must have rows of equal length");if(r=q.map(C=>Array.from(C)),r.some(C=>C.some(D=>!(D>=0&&D<1/0))))throw new RangeError("the counts of the table must be nonnegative and finite");o=t??{}}let{ML:s=!1,stdErr:a=!1}=o,c=[],i=0;Ne(r.map(q=>Ne(q)))<2**1e3||({groups:r,k:i}=se(r));let l=r.map(q=>Ne(q)),f=l.filter(q=>q===0).length;f>0&&(c.push(_u(f,"row")),r=r.filter((q,F)=>l[F]!==0));let m=(r[0]??[]).map((q,F)=>Ne(r.map(C=>C[F]))),u=m.filter(q=>q===0).length;u>0&&(c.push(_u(u,"column")),r=r.map(q=>q.filter((F,C)=>m[C]!==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 W("polychor",{rho:NaN,warnings:c});let g=[-1/0,...Ji(r.map(q=>Ne(q))),1/0],d=[-1/0,...Ji(r[0].map((q,F)=>Ne(r.map(C=>C[F])))),1/0],N=Cc(q=>{let F=Math.max(-Ft,Math.min(Ft,q)),C=[];for(let D=0;D<h;D++)for(let O=0;O<p;O++){if(r[D][O]===0)continue;let z=Kr([g[D],d[O]],[g[D+1],d[O+1]],F);C.push(r[D][O]*Math.log(z))}return-Ne(C)},-Ft,Ft).optimum;if(!s&&!a)return W("polychor",{rho:N,warnings:c});let b=q=>[-1/0,...q,1/0],E=g.slice(1,h),w=d.slice(1,p),M=q=>s?Fu(r,b(q.slice(1,h)),b(q.slice(h)),q[0],!0):Fu(r,g,d,q[0],!1),{theta:R,at:y,bounded:x}=$u(M,s?[N,...E,...w]:[N]),T=R[0];if(x&&c.push(zu(T)),!a)return W("polychor",{rho:T,warnings:c});let A=s?b(R.slice(1,h)):g,S=s?b(R.slice(h)):d,k=Ne(r.map(q=>Ne(q))),_=[],I=[];for(let q=0;q<h;q++)for(let F=0;F<p;F++){let C=r[q][F],D=Kr([A[q],S[F]],[A[q+1],S[F+1]],T);if(C===0)_.push(k*D);else if(i===0)_.push(C*Math.log1p(1e-6/C),C*Cu(k*D/C-1));else{let O=L(C,i);I.push(O*Math.log1p(1e-6/O)),_.push(C*Cu(k*D/C-1))}}let P=ju(y.hessian,x);return W("polychor",{rho:T,warnings:c,var:i===0?P:P.map(q=>q.map(F=>L(F,-i))),chisq:i===0?2*Ne(_):L(2*Ne(_),i)+2*Ne(I),df:h*p-h-p,n:L(k,i),rowCuts:A.slice(1,h),colCuts:S.slice(1,p)})}function Eb(e,t,n,r){let o=e.length,s=[Z(.25),Z(.5),Z(.75)],a=(h,p)=>p.filter(g=>g<h).length,c=tc(e).map(h=>a(h,n)),i=tc(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=Kr([f[p],m[d]],[f[p+1],m[d+1]],r);u.push(g*Math.log((g+1e-6)/(v*o)))})),2*Ne(u)}function Rb(e,t,n={}){if(V(n,bb,"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,x)=>!Number.isNaN(y)&&!Number.isNaN(t[x])),a=e.filter((y,x)=>s[x]),c=dt(t.filter((y,x)=>s[x])),i=a.length,l=ec(c);if(l.length<2)return W("polyserial",{rho:NaN,warnings:["y has fewer than 2 levels"]});let f=new Map(l.map((y,x)=>[y,x+1])),m=c.map(y=>f.get(y)),u=l.map(()=>0);for(let y of m)u[y-1]++;let h=Ji(u),p=[],g=Math.sqrt((i-1)/i)*Math.sqrt(_e(m))*Dc(a,m)[0]/Ne(h.map(y=>_t(y)));if(Math.abs(g)>Ft){let y=Math.sign(g)*Ft;p.push(`initial correlation inadmissible, ${dn(g)}, set to ${dn(y)}`),g=y}if(!r&&!o)return W("polyserial",{rho:g,warnings:p});let d=tc(a),v=m.map(y=>y-1),N=y=>Nb(d,v,r?y.slice(1):h,y[0],r),{theta:b,at:E,bounded:w}=$u(N,r?[g,...h]:[g]);if(w&&p.push(zu(b[0])),!o)return W("polyserial",{rho:b[0],warnings:p});let M=r?b.slice(1):h,R=l.length;return W("polyserial",{rho:b[0],warnings:p,var:ju(E.hessian,w),chisq:Eb(m,d,M,r?b[0]:g),df:4*R-R-1,n:i,cuts:M})}export{xi as Anova,ab as ICC,U0 as TukeyHSD,r1 as adTest,Fa as anova,Df as ansariTest,B0 as aov,Sw as aovEz,Da as aovSummary,q1 as auc,lf as bartlettTest,xa as binomCI,yp as binomDiffCI,Lp as binomDiffEquivalenceTest,Ep as binomRatioCI,Yh as binomTest,z1 as blandAltman,gd as boschlooTest,M0 as brunnerMunzelTest,fh as chisqTest,C1 as ciAuc,Ed as cochranQTest,Vg as coefci,Dg as coeftest,zm as cohenES,Bp as cohensD,j0 as confint,_1 as coords,Bu as corTest,jw as coxph,p1 as cronbachAlpha,o1 as cvmTest,V1 as diagnosticTests,Hd as dscfAllPairsTest,Xd as dunnTest,Kd as dunnettTest,Jp as epsilonSquared,cw as esH,lw as esW1,fw as esW2,Qp as etaSquared,fb as factanal,no as fisherTest,uf as flignerTest,Ka as friedmanTest,rh as gTest,Gd as gamesHowellTest,Zd as glhtDunnett,og as glm,Wg as glmAnova,wg as glmConfint,hg as glmCooksDistance,go as glmHatvalues,dg as glmPredict,fg as glmResiduals,ug as glmRstandard,hi as glmSummary,ih as goodmanKruskalGamma,Kp as hedgesG,h1 as icc,t1 as jonckheereTest,m1 as kappa2,u1 as kappamFleiss,ds as kruskalTest,Uf as ksTest,Wd as kwAllPairsDunnTest,Bd as kwManyOneDunnTest,lh as lblTest,Kw as leveneTest,s1 as lillieTest,li as lm,s0 as mantelhaenTest,Ud as manyOneUTest,Yg as mauchlyTest,vd as mcnemarTest,eb as mcreg,pr as modelMatrix,hf as moodTest,Sp as oddsRatio,wb as omega,Zp as omegaSquared,xf as onewayTest,wr as pAdjust,_0 as pairwisePropTest,q0 as pairwiseTTest,I0 as pairwiseWilcoxTest,g1 as partialR,i1 as pearsonTest,tp as poissonTest,Mb as polychor,Rb as polyserial,Zu as powerAnovaTest,Xu as powerPropTest,Yu as powerTTest,z0 as predict,Ea as propTest,Gw as propTrendTest,Nw as pwr2p2nTest,ww as pwr2pTest,Ew as pwrAnovaTest,Mw as pwrChisqTest,Rw as pwrF2Test,vw as pwrNormTest,bw as pwrPTest,yw as pwrRTest,gw as pwrT2nTest,dw as pwrTTest,mf as quadeTest,S1 as roc,D1 as rocTest,Wu as setWarningHandler,a1 as sfTest,e0 as shapiroTest,ah as stuartMaxwellTest,_w as survdiff,Ow as survfit,id as tTOST,nn as tTest,Ip as tost,cd as tsumTOST,Yd as tukeyTest,cf as varTest,Lg as vcovCL,Mg as vcovHC,ro as wilcoxTest,H0 as zTest};
18
+ `,lo=(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})),Zo=e=>e[0].map((t,n)=>e.map(r=>r[n])),Jo=(e,t=n=>n)=>e[0].map((n,r)=>At(e.map(o=>t(o[r])))),vh=(e,t=n=>n)=>e.map(n=>At(n.map(t))),fo=(e,t=n=>n)=>At(e[0].flatMap((n,r)=>e.map(o=>t(o[r])))),ya=e=>e<0?-1:1;function Cc(e){let t=e.map((n,r)=>Math.sqrt(n[r]));return e.map((n,r)=>n.map((o,s)=>r===s?1:o/(t[r]*t[s])))}function yh(e,t){let n=Cc(e),{d:r,u:o,v:s}=ys(n),a=r.map((i,l)=>l).filter(i=>r[i]>Math.sqrt(co)*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)&&rr(t,"In smc, smcs > 1 were set to 1.0"),c.some(i=>i<0)&&rr(t,"In smc, smcs < 0 were set to .0"),c.map(i=>i>1?1:i<0?0:i)}var rr=(e,t)=>{e.includes(t)||e.push(t)};function _c(e,t,n,r){let o=e.map((c,i)=>c.map((l,f)=>i===f?t[i]:l)),s=Vt(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 Mv=e=>lo(e,Zo(e));function bh(e,t,n,r){let o=Cc(e),s=o.length,a=yh(o,r),c=d=>{let v=_c(o,d.map(M=>1-M),t,M=>M<co?100*co:M),N=Mv(v),b=[];for(let M=0;M<s;M++)for(let g=M+1;g<s;g++)b.push((o[g][M]-N[g][M])**2);return At(b)},i=d=>{let v=_c(o,d.map((N,b)=>o[b][b]-N),t,N=>Math.max(N,0));return d.map((N,b)=>{let M=0;for(let g=0;g<t;g++)M+=v[b][g]*v[b][g];return M+N-o[b][b]})},l=d=>{let v=Vt(o.map((A,T)=>A.map((k,_)=>T===_?1-d[T]:k)),{symmetric:!0}),N=v.values,b=v.vectors,M=N.map(A=>A>=co),g=N.map(A=>A<co?100*co:A),y=o.map((A,T)=>A.map((k,_)=>{if(T===_)return 0;let L=0;for(let q=0;q<t;q++)L+=g[q]*b[T][q]*b[_][q];return k-L})),x=lo(y,b),E=(A,T)=>{let k=0;for(let _=0;_<s;_++)k+=b[_][A]*x[_][T];return k},R=new Array(s).fill(0);for(let A=0;A<t;A++){let T=(k,_)=>{if(_===0)return;let L=_*E(A,k);for(let q=0;q<s;q++)R[q]+=L*b[q][A]*b[q][k]};M[A]&&T(A,1);for(let k=A+1;k<t;k++)T(k,2*(M[A]&&M[k]?1:(g[A]-g[k])/(N[A]-N[k])));for(let k=t;k<s;k++)T(k,2*g[A]/(N[A]-N[k]))}return R},f=.005,m={method:"L-BFGS-B",lower:f,upper:Math.max(dt(a),1)},u=new Array(s).fill(.01),h=tt(()=>No(a.map(d=>1-d),c,{gr:i,...m,control:{fnscale:1,parscale:u}}));h.par.some(d=>d===f)&&(h=tt(()=>No(h.par,c,{gr:l,...m,control:{fnscale:1,parscale:u,factr:1e3}}))),h.convergence!==0&&rr(n,`minres did not converge (optim convergence code ${h.convergence}): examine the results carefully`);let p=h.par,w=_c(o,p.map((d,v)=>o[v][v]-d),t,d=>Math.max(d,0));if(w=Lc(w,t),w=w.map(d=>d.map(v=>v===0?1e-15:v)),vh(w,d=>d*d).some(d=>d>1)&&rr(n,"An ultra-Heywood case was detected. Examine the results carefully"),t>1){w=Lc(va(w).loadings,t);let d=Jo(w,N=>N*N),v=d.map((N,b)=>b).sort((N,b)=>d[b]-d[N]||N-b);w=w.map(N=>v.map(b=>N[b]))}else w=Lc(w,t);return w}function Lc(e,t){let n=Jo(e).map(ya);return t===1?n[0]<0?e.map(r=>r.map(o=>-o)):e:e.map(r=>r.map((o,s)=>n[s]*o))}function Ev(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:fo(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 xv(e){let t=e[0].length,n=l=>{let f=ot(l),m=lo(e,Zo(f)),{f:u,Gq:h}=Ev(m),p=Zo(lo(lo(Zo(m),h),f)).map(w=>w.map(d=>-d));return{L:m,f:u,G:p}},r=Array.from({length:t},(l,f)=>Array.from({length:t},(m,u)=>f===u?1:0)),o=1,s=n(r),a,c=[],i=!1;for(let l=0;l<=2e3;l++){let f=Jo(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(fo(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,M)=>b.map((g,y)=>g-d.T[M][y])),N=m.map((b,M)=>b.map((g,y)=>g-d.Gp[M][y]));fo(N,b=>b*b)>0&&(o=fo(v,b=>b*b)/Math.abs(fo(v.map((b,M)=>b.map((g,y)=>g*N[M][y])))),o=Math.max(1e-10,Math.min(o,20)))}else o=2*o;let h=dt(c.slice(-10)),p=s,w=r;for(let d=0;d<=10;d++){let v=r.map((b,M)=>b.map((g,y)=>g-o*m[M][y])),N=Jo(v,b=>b*b).map(b=>1/Math.sqrt(b));if(w=v.map(b=>b.map((M,g)=>M*N[g])),p=n(w),h-p.f>.5*u*u*o)break;o=o/2}a={T:r,Gp:m},r=w,s=p}return{loadings:s.L,phi:lo(Zo(r),r),converged:i}}function Rv(e,t={}){H(t,yv,"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)ba(t.covmat),r=Cc(t.covmat);else{if(e===null)throw new TypeError("neither 'x' nor 'covmat' supplied");r=yo(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=bh(r,n,s,a),i,l,f;if(n===1)i=c.map(S=>S.slice()),f=[1],rr(a,`Omega_h for 1 factor is not meaningful, just omega_t
19
+ `),rr(s,"Omega_h and Omega_asymptotic are not meaningful with one factor");else{let S=xv(c);if(S.converged||rr(s,"oblimin did not converge in 2000 iterations"),i=S.loadings,l=S.phi,n===2){let P=l[0][1],G=Math.sqrt(Math.abs(P));f=[G,ya(P)*G],rr(a,Nv)}else f=bh(l,1,s,a).map(P=>P[0])}let m=i.map(S=>{let P=0;for(let G=0;G<n;G++)P+=S[G]*f[G];return P}),u=vh(c,S=>S*S),h=u.map((S,P)=>r[P][P]-S),p=i.map((S,P)=>S.map((G,Q)=>{if(n===1)return 0;let te=r[P][P],$=te-(te-G*G)-(G*f[Q])**2;return $<0&&($=0),ya(G)*Math.sqrt($)})),w=m.map((S,P)=>S*S/u[P]),d=m.map((S,P)=>{let G=[S,...p[P]];return At(G.map(Q=>Q*Q))**2/At(G.map(Q=>Q**4))}),v=m.map(ya);v.some(S=>S<0)&&(r=r.map((S,P)=>S.map((G,Q)=>v[P]*G*v[Q])));let N=m.map((S,P)=>v[P]*S),b=p.map((S,P)=>S.map(G=>v[P]*G)),M=N.map((S,P)=>[S,...b[P],u[P],h[P],w[P],d[P]]),g=fo(r),y=At(r.map((S,P)=>S[P])),x=At(N)**2,E=At(h),R=x/g,A=(g-E)/g,T=x/(g-E),k=(g-y)/g*o/(o-1),_=(g+At(yh(r,a))-y)/g,L=Jo(M.map(S=>S.slice(0,n+1)),S=>S*S),q=L[0]/At(L),F=b.map(S=>{let P=0;for(let G=1;G<n;G++)Math.abs(S[G])>Math.abs(S[P])&&(P=G);return P}),C=[],O=[];for(let S=0;S<n;S++){let P=F.map(($,J)=>J).filter($=>F[$]===S),G=At(P.flatMap($=>P.map(J=>r[J][$]))),Q=At(P.map($=>b[$][S])),te=At(P.map($=>N[$]));O.push(Q*Q),C.push([(Q*Q+te*te)/G,te*te/G,Q*Q/G])}let I=[[A,At(N)**2/g,At(O)/g],...C],V={omegaH:R,omegaLim:T,alpha:k,omegaTot:A,G6:_,ECV:q,sl:M,slNames:["g",...Array.from({length:n},(S,P)=>`F${P+1}*`),"h2","u2","p2","com"],orthog:c,oblique:i,gloading:f,key:v,omegaGroup:I,warnings:s,messages:a};return l!==void 0&&(V.phi=l),Z("omega",V)}var Nh=["ML","stdErr"],Av=["ML","stdErr"],Qt=.9999;function Fc(e){let t=De(e),n=[],r=0;for(let o=0;o<e.length-1;o++)r+=e[o],n.push(pe(r/t));return n}var Dc=e=>[...new Set(e)].sort((t,n)=>t-n),Mh=(e,t)=>e===1?`1 ${t} with zero marginal removed`:`${e} ${t}s with zero marginals removed`,Eh=e=>Array.isArray(e)||ArrayBuffer.isView(e),xh=(e,t)=>e>0?ce(e,{lowerTail:!1})-ce(t,{lowerTail:!1}):ce(t)-ce(e);function Vc(e){let t=Te([e]).groups[0],n=st(t),r=t.map(s=>s-n),o=Math.sqrt(rt(r));return r.map(s=>s/o)}function Rh(e){if(Math.abs(e)>=.1)return e-Math.log1p(e);let t=e,n=0;for(let r=2;r<40;r++){t*=-e;let o=t/r;if(n+=o,Math.abs(o)<=1e-17*Math.abs(n))break}return-n}var Na=e=>Array.from({length:e},()=>new Array(e).fill(0));function Sh(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 Oc={r:0,h:0,k:0,rr:0,rh:0,rk:0,hh:0,hk:0,kk:0};function Tv(e,t,n){if(e===-1/0||t===-1/0||e===1/0&&t===1/0)return Oc;if(e===1/0)return{...Oc,k:Ut(t),kk:-t*Ut(t)};if(t===1/0)return{...Oc,h:Ut(e),hh:-e*Ut(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=Ut(e),f=Ut(t);return{r:a,h:l*ce(c),k:f*ce(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*ce(c)-n/o*l*Ut(c),hk:a,kk:-t*f*ce(i)-n/o*f*Ut(i)}}function Ah(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:Na(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=>Tv(h,p,r))),u=[];for(let h=0;h<s;h++)for(let p=0;p<a;p++){let w=e[h][p];if(w===0)continue;let d=po([t[h],n[p]],[t[h+1],n[p+1]],r),v=new Array(c).fill(0),N=Na(c);for(let[b,M,g]of[[h+1,p+1,1],[h,p+1,-1],[h+1,p,-1],[h,p,1]]){let y=m[b][M],x=l(b),E=f(M);v[0]+=g*y.r,N[0][0]+=g*y.rr,x>=0&&(v[x]+=g*y.h,N[0][x]+=g*y.rh,N[x][0]+=g*y.rh,N[x][x]+=g*y.hh),E>=0&&(v[E]+=g*y.k,N[0][E]+=g*y.rk,N[E][0]+=g*y.rk,N[E][E]+=g*y.kk),x>=0&&E>=0&&(N[x][E]+=g*y.hk,N[E][x]+=g*y.hk)}Sh(i,w,d,v,N),u.push(w*Math.log(d))}return i.value=De(u),i}function Sv(e,t,n,r,o){let s=o?n.length+1:1,a={value:0,gradient:new Array(s).fill(0),hessian:Na(s)},c=(1-r)*(1+r),i=Math.sqrt(c),l=c*i,f=new Array(s),m=Na(s),u=[],h=(p,w,d)=>{let v=n[p],N=(v-r*w)/i,b=Ut(N),M=(r*v-w)/l,g=1/i,y=v/l+3*r*(r*v-w)/(l*c),x=r/l;if(f[0]+=d*b*M,m[0][0]+=d*b*(y-N*M*M),o){let E=p+1;f[E]+=d*b*g;let R=d*b*(x-N*M*g);m[0][E]+=R,m[E][0]+=R,m[E][E]+=d*b*(-N*g*g)}};for(let p=0;p<e.length;p++){let w=e[p],d=t[p];f.fill(0);for(let b of m)b.fill(0);let v=d<n.length?(n[d]-r*w)/i:1/0,N=d>0?(n[d-1]-r*w)/i:-1/0;d<n.length&&h(d,w,1),d>0&&h(d-1,w,-1),Sh(a,1,xh(N,v),f,m),u.push(Math.log(xh(N,v)),Ut(w,{log:!0}))}return a.value=De(u),a}function kv(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=ot(o,r)}catch{s=void 0}return(s===void 0||!s.every(Number.isFinite)||De(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 kh=(e,t)=>e>=t-1e-12*Math.abs(t);function Th(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=kv(o,i);if(!s&&Math.abs(r[0])===Qt&&l[0]*r[0]>0){s=!0;continue}let f=Math.abs(r[0]+l[0])>Qt?(Math.sign(l[0])*Qt-r[0])/l[0]:1,m=!1;for(let u=0;u<60;u++){let h=r.map((w,d)=>w+f*l[d]);f*l[0]!==0&&Math.abs(h[0])>=Qt*(1-1e-15)&&(h[0]=Math.sign(h[0])*Qt);let p=e(h);if(kh(p.value,o.value)){let w=dt(h.map((d,v)=>Math.abs(d-r[v])/Math.max(1,Math.abs(r[v]))));r=h,o=p,m=w>1e-12;break}f/=2}if(!m)break}return{theta:r,at:o,bounded:s}}function qh(e,t){let n=Th(e,[Math.max(-Qt,Math.min(Qt,t[0])),...t.slice(1)],!1);if(n.bounded||n.theta[0]===0)return n;let r=Th(e,[Math.sign(n.theta[0])*Qt,...n.theta.slice(1)],!0);return kh(r.at.value,n.at.value)?r:n}function Ih(e,t){let n=()=>e.map(o=>o.map(()=>1/0));if(t)return n();let r;try{r=ot(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 _h=e=>`inadmissible correlation set to ${e>0?"0.9999":"-0.9999"}`;function qv(e,t,n){xt(t)&&H(t,Nh,"polychor"),H(n,Nh,"polychor");let r,o;if(Eh(t)){let C=e,O=t;if(C.length!==O.length)throw new RangeError("'x' and 'y' must have the same length");let I=C.map((Q,te)=>!Number.isNaN(Q)&&!Number.isNaN(O[te])),V=St(C.filter((Q,te)=>I[te])),S=St(O.filter((Q,te)=>I[te])),P=new Map(Dc(V).map((Q,te)=>[Q,te])),G=new Map(Dc(S).map((Q,te)=>[Q,te]));r=Array.from({length:P.size},()=>new Array(G.size).fill(0)),V.forEach((Q,te)=>r[P.get(Q)][G.get(S[te])]++),o=n??{}}else{let C=e;if(!C.every(Eh))throw new TypeError("give a table (array of rows) or two vectors");let O=C[0]?.length??0;if(C.some(I=>I.length!==O))throw new TypeError("the table must have rows of equal length");if(r=C.map(I=>Array.from(I)),r.some(I=>I.some(V=>!(V>=0&&V<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;De(r.map(C=>De(C)))<2**1e3||({groups:r,k:i}=Te(r));let l=r.map(C=>De(C)),f=l.filter(C=>C===0).length;f>0&&(c.push(Mh(f,"row")),r=r.filter((C,O)=>l[O]!==0));let m=(r[0]??[]).map((C,O)=>De(r.map(I=>I[O]))),u=m.filter(C=>C===0).length;u>0&&(c.push(Mh(u,"column")),r=r.map(C=>C.filter((O,I)=>m[I]!==0)));let h=r.length,p=r[0]?.length??0;if(h<2&&c.push("the table has fewer than 2 rows"),p<2&&c.push("the table has fewer than 2 columns"),h<2||p<2)return Z("polychor",{rho:NaN,warnings:c});let w=[-1/0,...Fc(r.map(C=>De(C))),1/0],d=[-1/0,...Fc(r[0].map((C,O)=>De(r.map(I=>I[O])))),1/0],N=Ml(C=>{let O=Math.max(-Qt,Math.min(Qt,C)),I=[];for(let V=0;V<h;V++)for(let S=0;S<p;S++){if(r[V][S]===0)continue;let P=po([w[V],d[S]],[w[V+1],d[S+1]],O);I.push(r[V][S]*Math.log(P))}return-De(I)},-Qt,Qt).optimum;if(!s&&!a)return Z("polychor",{rho:N,warnings:c});let b=C=>[-1/0,...C,1/0],M=w.slice(1,h),g=d.slice(1,p),y=C=>s?Ah(r,b(C.slice(1,h)),b(C.slice(h)),C[0],!0):Ah(r,w,d,C[0],!1),{theta:x,at:E,bounded:R}=qh(y,s?[N,...M,...g]:[N]),A=x[0];if(R&&c.push(_h(A)),!a)return Z("polychor",{rho:A,warnings:c});let T=s?b(x.slice(1,h)):w,k=s?b(x.slice(h)):d,_=De(r.map(C=>De(C))),L=[],q=[];for(let C=0;C<h;C++)for(let O=0;O<p;O++){let I=r[C][O],V=po([T[C],k[O]],[T[C+1],k[O+1]],A);if(I===0)L.push(_*V);else if(i===0)L.push(I*Math.log1p(1e-6/I),I*Rh(_*V/I-1));else{let S=D(I,i);q.push(S*Math.log1p(1e-6/S)),L.push(I*Rh(_*V/I-1))}}let F=Ih(E.hessian,R);return Z("polychor",{rho:A,warnings:c,var:i===0?F:F.map(C=>C.map(O=>D(O,-i))),chisq:i===0?2*De(L):D(2*De(L),i)+2*De(q),df:h*p-h-p,n:D(_,i),rowCuts:T.slice(1,h),colCuts:k.slice(1,p)})}function Iv(e,t,n,r){let o=e.length,s=[pe(.25),pe(.5),pe(.75)],a=(h,p)=>p.filter(w=>w<h).length,c=Vc(e).map(h=>a(h,n)),i=Vc(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((w,d)=>{if(w===0)return;let v=po([f[p],m[d]],[f[p+1],m[d+1]],r);u.push(w*Math.log((w+1e-6)/(v*o)))})),2*De(u)}function _v(e,t,n={}){if(H(n,Av,"polyserial"),e.length!==t.length)throw new RangeError("'x' and 'y' must have the same length");let{ML:r=!1,stdErr:o=!1}=n,s=e.map((E,R)=>!Number.isNaN(E)&&!Number.isNaN(t[R])),a=e.filter((E,R)=>s[R]),c=St(t.filter((E,R)=>s[R])),i=a.length,l=Dc(c);if(l.length<2)return Z("polyserial",{rho:NaN,warnings:["y has fewer than 2 levels"]});let f=new Map(l.map((E,R)=>[E,R+1])),m=c.map(E=>f.get(E)),u=l.map(()=>0);for(let E of m)u[E-1]++;let h=Fc(u),p=[],w=Math.sqrt((i-1)/i)*Math.sqrt(rt(m))*xl(a,m)[0]/De(h.map(E=>Ut(E)));if(Math.abs(w)>Qt){let E=Math.sign(w)*Qt;p.push(`initial correlation inadmissible, ${In(w)}, set to ${In(E)}`),w=E}if(!r&&!o)return Z("polyserial",{rho:w,warnings:p});let d=Vc(a),v=m.map(E=>E-1),N=E=>Sv(d,v,r?E.slice(1):h,E[0],r),{theta:b,at:M,bounded:g}=qh(N,r?[w,...h]:[w]);if(g&&p.push(_h(b[0])),!o)return Z("polyserial",{rho:b[0],warnings:p});let y=r?b.slice(1):h,x=l.length;return Z("polyserial",{rho:b[0],warnings:p,var:Ih(M.hessian,g),chisq:Iv(m,d,y,r?b[0]:w),df:4*x-x-1,n:i,cuts:y})}var Lv=["group","strata","rho","cencode"],Cv=new Set(["cov2","tf","failcode","cencode","gtol","maxiter","init","variance","confLevel","names"]);function Ma(e){return e==null||typeof e=="number"&&Number.isNaN(e)}function Pc(e){let t=un(e),n=Array.from(new Set(t)).sort(gr);return{labels:n,index:new Map(n.map((r,o)=>[r,o]))}}function Ov(e,t,n){if(e.length!==t.length)throw new RangeError("'time' and 'status' must have the same length");if(n.group!==void 0&&n.group.length!==e.length)throw new RangeError("'group' must have the same length as 'time'");if(n.strata!==void 0&&n.strata.length!==e.length)throw new RangeError("'strata' must have the same length as 'time'")}function Fv(e,t,n={}){H(n,Lv,"cuminc"),Ov(e,t,n);let{group:r,strata:o,rho:s=0,cencode:a=0}=n,c=[],i=new ft;for(let g=0;g<e.length;g++)if(!(Number.isNaN(e[g])||Ma(t[g])||r!==void 0&&Ma(r[g])||o!==void 0&&Ma(o[g]))){if(!Number.isFinite(e[g]))throw new RangeError("'time' must contain only finite values");if(e[g]<0)throw new RangeError("'time' must be non-negative");c.push(g)}let l=e.length-c.length;if(l>0&&i.add(`${l} cases omitted due to missing values`),c.length===0)throw new RangeError("no complete observations");let f=r===void 0?[1]:un(c.map(g=>r[g])),m=o===void 0?[1]:un(c.map(g=>o[g])),u=un([...c.map(g=>t[g]),a]),h=Pc(f);if(h.labels.length>=2&&!Number.isFinite(s))throw new RangeError("'rho' must be finite when Gray's test is requested");let p=Pc(m),w=Pc(u),d=u[u.length-1],v=w.labels.filter(g=>g!==d);if(v.length===0)throw new Error("there are no events of any cause");let N=c.map(g=>({time:e[g],status:u[c.indexOf(g)],group:r===void 0?0:h.index.get(f[c.indexOf(g)]),strata:o===void 0?0:p.index.get(m[c.indexOf(g)]),source:g})).sort((g,y)=>g.time-y.time),b=[];for(let g of v){let y=mt(g);for(let x=0;x<h.labels.length;x++){let E=N.filter(I=>I.group===x),R=E.reduce((I,V)=>Math.max(I,V.time),-1/0),A=R>0&&R<1?2**Math.min(1023,Math.ceil(-Math.log2(R))):1,T=er(E.map(I=>I.time*A),E.map(I=>I.status===d?0:1)),k=[0],_=[0],L=[0],q=[],F=1;for(let I=0;I<T.time.length;I++){let V=T.nEvent[I];if(V===0)continue;let S=T.time[I]/A,P=T.nRisk[I],G=E.filter(Q=>Q.time===S&&Q.status===g).length;q.push({t:S,Y:P,d:V,dk:G,sb:F,sa:T.surv[I]}),F=T.surv[I]}let C=0,O=0;for(let I=0;I<q.length;I++){let V=q[I],S=C,P=O;C+=V.sb*V.dk/V.Y;let G=0;for(let Q=0;Q<=I;Q++){let te=q[Q],$=te.Y,J=te.d,ee=te.dk,Se=$===J?0:(J*($-J)+2*ee*(J-ee))/(($-1)*($-J)**2),re=$===1?ee:ee*($-ee)/($*$*($-1)),j=$===J||$===1?0:ee*($-ee)/($*($-J)*($-1)),K=0;for(let U=0;U<=Q;U++){let z=q[U];K+=z.sb*z.dk/z.Y}let W=C-K;G+=W*W*Se+te.sb*te.sb*re-2*W*te.sb*j}O=Math.max(0,G),V.dk>0&&(k.push(V.t,V.t),_.push(S,C),L.push(P,O))}k.push(R),_.push(C),L.push(O),b.push({name:`${mt(h.labels[x])} ${y}`,group:mt(h.labels[x]),cause:y,time:k,est:_,var:L})}}let M=h.labels.length>=2?Dv(N,h.labels,v,p.labels.length,s,d):void 0;return Z("cuminc",{curves:b,...M===void 0?{}:{tests:M},warnings:i.list})}function Dv(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 p=0;p<r;p++){let w=e.filter(M=>M.strata===p),d=Array.from(new Set(w.filter(M=>M.status!==s).map(M=>M.time))).sort((M,g)=>M-g),v=new Array(i).fill(1),N=new Array(i).fill(0),b=[];for(let M of d){let g=new Array(i).fill(0),y=new Array(i).fill(0),x=new Array(i).fill(0);for(let O of w)O.time>=M&&g[O.group]++;for(let O of w)O.time===M&&O.status!==s&&(O.status===c?y[O.group]++:x[O.group]++);let E=v.slice(),R=N.slice(),A=g.map((O,I)=>O===0?0:O/E[I]),T=g.map((O,I)=>O===0?0:O*(1-R[I])/E[I]),k=y.reduce((O,I)=>O+I,0),_=A.reduce((O,I)=>O+I,0),L=_===0?0:k/_,q=b.length===0?1:b[b.length-1].Gafter,F=q-L,C=T.reduce((O,I)=>O+I,0);for(let O=0;O<i;O++){let I=q**o;C>0&&(l[O]+=I*(y[O]-T[O]*k/C)),g[O]>0?(v[O]=v[O]*(1-(y[O]+x[O])/g[O]),N[O]+=E[O]*y[O]/g[O]):v[O]=0}b.push({time:M,Y:g,d1:y,d2:x,Sbefore:E,Safter:v.slice(),Fbefore:R,h:A,R:T,dF0:L,Gbefore:q,Gafter:F})}for(let M=0;M<i;M++)for(let g=0;g<i;g++){let y=0;for(let x=0;x<i;x++)for(let E=0;E<b.length;E++){let R=b[E],A=R.h.reduce((te,$)=>te+$,0);if(!(A>0)||!(R.h[x]>0))continue;let T=R.Gbefore**o,k=T*R.h[M]*((M===x?1:0)-R.h[x]/A),_=T*R.h[g]*((g===x?1:0)-R.h[x]/A),L=0,q=0;for(let te=E+1;te<b.length;te++){let $=b[te],J=$.h.reduce((K,W)=>K+W,0);if(!(J>0))continue;let ee=$.Gbefore**o,Se=ee*$.h[M]*((M===x?1:0)-$.h[x]/J),re=ee*$.h[g]*((g===x?1:0)-$.h[x]/J),j=$.Gbefore===0?0:$.dF0/$.Gbefore;L+=Se*j,q+=re*j}let F=R.Safter[x]===0?0:R.Gafter/R.Safter[x],C=k+(1-F)*L,O=_+(1-F)*q,I=-F*L,V=-F*q,S=R.Y[x]===0?0:R.Sbefore[x]*R.d2[x]/R.Y[x],P=C*O,G=R.d1.reduce((te,$)=>te+$,0),Q=R.Y.reduce((te,$)=>te+$,0);G>1&&Q>1&&(P+=((Q-G)/(Q-1)-1)*k*_),y+=(P*R.dF0+I*V*S)/R.h[x]}f[M][g]+=y}}let m=i-1,u=NaN,h=NaN;if(l.every(p=>p===0))u=0,h=1;else{let p=f.slice(0,m).map(w=>w.slice(0,m));try{let w=ot(p,l.slice(0,m),{tol:1e-12});u=l.slice(0,m).reduce((d,v,N)=>d+v*w[N],0),h=Bt(u,m,{lowerTail:!1})}catch{u=NaN,h=NaN}}a.push({cause:mt(c),stat:u,df:m,pValue:h})}return a}function Vv(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 Rn(e,t=e){return Array.from({length:e},()=>new Array(t).fill(0))}function Pv(e,t,n,r){for(let o=0;o<t.length;o++)for(let s=0;s<n.length;s++)e[o][s]+=r*t[o]*n[s]}function jc(e,t){return De(e.map((n,r)=>n*t[r]))}function jv(e){return e.every(t=>t.every(Number.isFinite))}function $v(e,t,n,r={}){H(r,Cv,"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,w=o?.[0]?.length??0,d=p+w;if(d===0)throw new RangeError("at least one covariate is required");if(n!==void 0&&n.some(z=>z.length!==p))throw new RangeError("rows of 'cov1' must have equal length");if(o!==void 0&&o.some(z=>z.length!==w))throw new RangeError("rows of 'cov2' must have equal length");if(f!==void 0&&f.length!==d)throw new RangeError("'init' must have one value per coefficient");if(h!==void 0&&h.length!==d)throw new RangeError("'names' must have one name per coefficient");let v=[];for(let z=0;z<e.length;z++)if(!(Number.isNaN(e[z])||Ma(t[z])||n!==void 0&&n[z].some(Number.isNaN)||o!==void 0&&o[z].some(Number.isNaN))){if(!Number.isFinite(e[z]))throw new RangeError("'time' must contain only finite values");if(n!==void 0&&!n[z].every(Number.isFinite)||o!==void 0&&!o[z].every(Number.isFinite))throw new RangeError("covariates must be finite");v.push(z)}let N=e.length-v.length;if(v.length===0)throw new RangeError("no complete observations");let b=v.map(z=>e[z]),M=un([...v.map(z=>t[z]),c,a]),g=M.slice(0,v.length),y=M[M.length-2],x=M[M.length-1],E=v.map(z=>n===void 0?[]:Array.from(n[z])),R=v.map(z=>o===void 0?[]:Array.from(o[z])),A=Array.from(new Set(b.filter((z,ne)=>g[ne]===x))).sort((z,ne)=>z-ne);if(A.length===0)throw new RangeError("no observations of 'failcode'");let T=w===0?[[0]]:s(A).map(z=>Array.from(z));if(w>0&&(T.length!==A.length||T.some(z=>z.length!==w)||!jv(T)))throw new RangeError("'tf' must return a finite matrix with one row per unique failure time and one column per cov2 column");let k=A.map((z,ne)=>b.map((le,Re)=>[...E[Re],...R[Re].map((Ce,be)=>Ce*(T[ne][be]??0))])),_=b.reduce((z,ne)=>Math.max(z,Math.abs(ne)),0),L=_>0&&_<1?2**Math.min(1023,Math.ceil(-Math.log2(_))):1,q=er(b.map(z=>z*L),g.map(z=>z===y?1:0)),F=z=>{let ne=z*L,le=1;for(let Re=0;Re<q.time.length&&!(q.time[Re]>=ne);Re++)le=q.surv[Re];return le},C=A.map(z=>b.map((ne,le)=>{if(ne>=z)return 1;if(g[le]===y||g[le]===x)return 0;let Re=F(ne);return Re===0?0:F(z)/Re}));for(let z=0;z<A.length;z++)for(let ne=0;ne<b.length;ne++)b[ne]===A[z]&&g[ne]===x&&(C[z][ne]=1);let O=z=>{let ne=Array.from({length:d},()=>[]),le=Array.from({length:d},()=>Array.from({length:d},()=>[])),Re=[],Ce=Rn(A.length,d),be=[],fe=[],ke=[];for(let Fe=0;Fe<A.length;Fe++){let Ue=A[Fe],$e=b.map((ae,Ae)=>ae===Ue&&g[Ae]===x?Ae:-1).filter(ae=>ae>=0),Je=$e.length,Ke=k[Fe],Xe=Ke.map(ae=>jc(z,ae)),he=C[Fe].slice();for(let ae of $e)he[ae]=1;let oe=Math.max(...Xe.map((ae,Ae)=>he[Ae]>0?ae:-1/0));if(oe===-1/0)throw new Error("empty Fine-Gray risk set");let we=oe<500&&oe>-500,ge=we?Xe.map((ae,Ae)=>he[Ae]*Math.exp(ae)):Xe.map((ae,Ae)=>he[Ae]*Math.exp(ae-oe)),ie=De(ge),et=Array.from({length:d},(ae,Ae)=>De(ge.map((Be,je)=>Be*Ke[je][Ae])));if(!(ie>0))throw new Error("empty Fine-Gray risk set");let Y=et.map(ae=>ae/ie),Ne=Array.from({length:d},(ae,Ae)=>{let Be=Ke[0][Ae],je=0,nt=0;for(let ut of Ke)je=Math.max(je,Math.abs(ut[Ae]-Be)),nt=Math.max(nt,Math.abs(ut[Ae]));return je>0&&nt/je>1e6}).some(Boolean),ye=Ne?Array.from({length:d},(ae,Ae)=>Ke.map(Be=>Be[Ae]-Ke[0][Ae])):[],Me=Ne?ye.map(ae=>De(ae.map((Ae,Be)=>ge[Be]*Ae))/ie):[],X=Ne?void 0:Array.from({length:d},(ae,Ae)=>Array.from({length:d},(Be,je)=>De(ge.map((nt,ut)=>nt*Ke[ut][Ae]*Ke[ut][je]))));fe.push(Y),ke.push(ge.map(ae=>ae/ie));let ve=we?Je/ie:Je*Math.exp(-oe)/ie;be.push(ve),Re.push(De($e.map(ae=>jc(z,Ke[ae])))-Je*(we?Math.log(ie):oe+Math.log(ie)));for(let ae=0;ae<d;ae++){let Be=De($e.map(je=>Ke[je][ae]))-Je*Y[ae];ne[ae].push(Be),Ce[Fe][ae]=Be}for(let ae=0;ae<d;ae++)for(let Ae=0;Ae<d;Ae++){let Be=Ne?De(ge.map((je,nt)=>je*(ye[ae][nt]-Me[ae])*(ye[Ae][nt]-Me[Ae])))/ie:X[ae][Ae]/ie-Y[ae]*Y[Ae];le[ae][Ae].push(Je*Be)}}let de=Rn(b.length,d);for(let Fe=0;Fe<A.length;Fe++){let Ue=k[Fe],$e=ke[Fe],Je=fe[Fe],Ke=A[Fe];for(let Xe=0;Xe<b.length;Xe++){let he=b[Xe]===Ke&&g[Xe]===x?1:0,oe=b.filter((ge,ie)=>ge===Ke&&g[ie]===x).length,we=he-$e[Xe]*oe;for(let ge=0;ge<d;ge++)de[Xe][ge]+=(Ue[Xe][ge]-Je[ge])*we}}let Oe=ne.map(De),Ee=le.map(Fe=>Fe.map(De));return{loglik:De(Re),score:Oe,inf:Ee,bfitj:be,res:Ce,eta:de,means:fe,riskFactors:ke,zByTime:k}},I=f===void 0?new Array(d).fill(0):Array.from(f);if(!I.every(Number.isFinite))throw new RangeError("'init' must contain finite values");let V=I.slice(),S=O(V),P=!1,G=0,Q=(z,ne)=>Math.max(...z.score.map((le,Re)=>Math.abs(le)*Math.max(Math.abs(ne[Re]),1)))<i*Math.max(Math.abs(z.loglik),1);for(;;){if(Q(S,V)){P=!0;break}if(G>=l)break;let z=ot(S.inf,S.score,{tol:0});V=V.map((ne,le)=>ne+z[le]),G++,S=O(V)}let te=ot(S.inf),$=Rn(d);if(m){let z=Rn(d),ne=b.length,le=Array.from(new Set(b)).sort((fe,ke)=>fe-ke),Re=le.map(fe=>{let ke=b.filter(Oe=>Oe>=fe).length,de=b.filter((Oe,Ee)=>Oe===fe&&g[Ee]===y).length;return ke===0?0:de/ke}),Ce=Rn(ne,d);for(let fe=0;fe<le.length;fe++){let ke=le[fe],de=new Array(d).fill(0);for(let Ee=0;Ee<ne;Ee++)if(!(!(b[Ee]<ke)||g[Ee]===y||g[Ee]===x))for(let Ve=0;Ve<A.length;Ve++){if(A[Ve]<ke)continue;let Fe=S.riskFactors[Ve][Ee],Ue=b.filter((Ke,Xe)=>Ke===A[Ve]&&g[Xe]===x).length,$e=S.zByTime[Ve][Ee],Je=S.means[Ve];for(let Ke=0;Ke<d;Ke++)de[Ke]+=($e[Ke]-Je[Ke])*Fe*Ue}let Oe=b.filter(Ee=>Ee>=ke).length;if(Oe!==0)for(let Ee=0;Ee<ne;Ee++){let Ve=b[Ee]===ke&&g[Ee]===y?1:0,Fe=b[Ee]>=ke?Re[fe]:0;for(let Ue=0;Ue<d;Ue++)Ce[Ee][Ue]+=de[Ue]/Oe*(Ve-Fe)}}for(let fe=0;fe<ne;fe++)Pv(z,S.eta[fe].map((ke,de)=>ke+Ce[fe][de]),S.eta[fe].map((ke,de)=>ke+Ce[fe][de]),1);$=Rn(d);let be=Lh(te,z);$=Lh(be,te)}else $=Rn(d).map(z=>z.map(()=>NaN));let J=m?$.map((z,ne)=>Math.sqrt(z[ne])):void 0,ee=J===void 0?void 0:V.map((z,ne)=>z/J[ne]),Se=ee===void 0?void 0:ee.map(z=>2*ce(-Math.abs(z))),re=m?pe((1+u)/2):void 0,j=re===void 0?void 0:V.map((z,ne)=>Math.exp(z-re*J[ne])),K=re===void 0?void 0:V.map((z,ne)=>Math.exp(z+re*J[ne])),W=m?V.map(Math.exp):void 0,U=h===void 0?[...Array.from({length:p},(z,ne)=>`cov1${ne+1}`),...Array.from({length:w},(z,ne)=>`cov2${ne+1}*tf${ne+1}`)]:Array.from(h);return Z("crr",{coefficients:V,names:U,var:$,...J===void 0?{}:{se:J,z:ee,pValue:Se,lower:j,upper:K,expCoef:W},loglik:S.loglik,loglikNull:O(new Array(d).fill(0)).loglik,logtest:{statistic:2*(S.loglik-O(new Array(d).fill(0)).loglik),df:d},score:S.score,inf:m?S.inf:Rn(d).map(z=>z.map(()=>NaN)),invinf:m?te:Rn(d).map(z=>z.map(()=>NaN)),converged:P,iterations:G,n:v.length,nMissing:N,uftime:A,bfitj:S.bfitj,res:m?S.res:null,tfs:T,warnings:[],covariateDimensions:[p,w]})}function Lh(e,t){let n=Rn(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 Wv(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,M)=>b*a[h][M])],w=e.bfitj[h],d=jc(e.coefficients,p),v=w*Math.exp(d),N=Number.isFinite(v)&&(v!==0||w===0)?v:w===0?0:Math.exp(Math.log(w)+d);u+=N}f.push(-Math.expm1(-u))}return f})}var zv=new Set(["type","time2","dist","weights","offset","strata","scale","init","df","control","intercept","confLevel"]),Bv=["maxiter","relTolerance","tolerChol"],Uv=["type","p","newX","newStrata","newOffset","seFit"],Kv=["type","weighted"],Wc=new WeakMap,Hv=Math.log(2*Math.PI),Gv=e=>Number(e),Yv=e=>e<-Math.LN2?Math.log1p(-Math.exp(e)):Math.log(-Math.expm1(e));function jh(e,t){return t===-1/0?e:e<t?NaN:e+Yv(t-e)}function Xv(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 Ch(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 Qv(e,t,n){let r=t-e;return Number.isFinite(r)?r/n:t/n-e/n}function $h(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 Zv(e,t,n){let r=$h(e,t,n),o=r>Math.log(Number.MAX_VALUE)?1/0:Math.exp(r);if(o<1e-8){let s=t-e;return Xt(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 Jv(e,t,n){let r=$h(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 jh(-Math.exp(a),-Math.exp(c))}function ey(e){return e<-36?e:-Math.log1p(Math.exp(-e))}function ty(e){return e>36?-e:-Math.log1p(Math.exp(e))}function Oh(e,t,n){if(e===-1/0)return-1/0;if(e===1/0)return 0;if(t==="extreme"||t==="weibull"||t==="exponential"||t==="rayleigh"){let r=Math.exp(e);return r===0?e:Math.log(-Math.expm1(-r))}return t==="logistic"||t==="loglogistic"?ey(e):t==="gaussian"||t==="loggaussian"||t==="lognormal"?ce(e,{logP:!0}):yn(e,n,{logP:!0})}function Fh(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"?ty(e):t==="gaussian"||t==="loggaussian"||t==="lognormal"?ce(e,{lowerTail:!1,logP:!0}):yn(e,n,{lowerTail:!1,logP:!0})}function Ea(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+Hv):sl(e,n,{log:!0})}function $c(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 ny(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 xa(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[L]=$c(i,s,a),q=Ea(i,s,a),F=ny(i,s,a);return{g:q-Math.log(o),e:-L/o,ee:F/(o*o),t:-i*L-1,tt:i*L+i*i*F,et:(L+i*F)/o}}let l=Oh(i,s,a),f=Oh(c,s,a),m=Fh(c,s,a),u=Fh(i,s,a),h=n===0?m:n===2?l:jh(c>0?m:l,c>0?u:f),w=c===-1/0?0:Math.exp(n===0&&(s==="extreme"||s==="weibull"||s==="exponential"||s==="rayleigh")?c:Ea(c,s,a)-h),d=i===1/0?0:Math.exp(Ea(i,s,a)-h),[v]=$c(c,s,a),[N]=$c(i,s,a),b=w===0||c===-1/0?0:w*v,M=d===0||i===1/0?0:d*N,g=c===-1/0||w===0?0:c*w,y=i===1/0||d===0?0:i*d,x=c===-1/0||b===0?0:c*c*b,E=i===1/0||M===0?0:i*i*M,R=-(d-w)/o,A=(M-b)/(o*o)-R*R,T=-(y-g),k=E-x-T*(1+T),_=((i===1/0||M===0?0:i*M)-(c===-1/0||b===0?0:c*b))/o-R*(1+T);return{g:h,e:R,ee:A,t:T,tt:k,et:_}}function ry(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 Wh(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 zh(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 Dh(e){let t=e.length;for(let n=0;n<t;n++)if(e[n][n]>0){e[n][n]=1/e[n][n];for(let r=n+1;r<t;r++){e[r][n]=-e[r][n];for(let o=0;o<n;o++)e[r][o]+=e[r][n]*e[n][o]}}for(let n=0;n<t;n++){if(e[n][n]===0){for(let r=0;r<t;r++)e[n][r]=0,e[r][n]=0;continue}for(let r=n+1;r<t;r++){let o=e[r][n]*e[r][r];e[n][r]=o;for(let s=n;s<r;s++)e[n][s]+=o*e[r][s]}}for(let n=0;n<t;n++)for(let r=0;r<n;r++)e[n][r]=e[r][n];return e}function es(e,t){return e.map(n=>or(n,t))}function or(e,t){let n=e.reduce((r,o,s)=>r+o*t[s],0);return cr(e,t,n)}function Bh(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=Te([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 Vh(e,t,n){return e<=0?-1/0:e>=1?1/0:t==="extreme"||t==="weibull"||t==="exponential"||t==="rayleigh"?Math.log(-Math.log1p(-e)):t==="logistic"||t==="loglogistic"?Math.log(e/(1-e)):t==="gaussian"||t==="loggaussian"||t==="lognormal"?pe(e):cn(e,n)}function Ph(e,t,n,r,o,s,a,c,i,l,f,m,u,h,p,w=1e-10){let d=e[0].length,v=d+(m===void 0?i:0),N=Bh(e,w),b=N.values,M=N.exponent,g=u.map((S,P)=>P<d?Le(S,M[P]):S),y=(S,P)=>{let G=0,Q=Array(v).fill(0),te=Array.from({length:v},()=>Array(v).fill(0)),$=Array.from({length:v},()=>Array(v).fill(0));for(let J=0;J<t.length;J++){let ee=c[J],Se=or(b[J],S.slice(0,d))+a[J],re=m??Math.exp(S[d+ee]),j=xa(n[J],r[J],o[J],Se,re,l,f),K=s[J];G+=K*j.g;let W=Array(v).fill(0);for(let U=0;U<d;U++)W[U]=b[J][U]*j.e;m===void 0&&(W[d+ee]=j.t);for(let U=0;U<v;U++){Q[U]+=K*W[U];for(let z=0;z<v;z++)$[U][z]+=K*W[U]*W[z]}if(P){for(let U=0;U<d;U++){for(let z=0;z<d;z++)te[U][z]+=K*b[J][U]*b[J][z]*j.ee;m===void 0&&(te[U][d+ee]+=K*b[J][U]*j.et)}if(m===void 0){for(let U=0;U<d;U++)te[d+ee][U]+=K*b[J][U]*j.et;te[d+ee][d+ee]+=K*j.tt}}}return{ll:G,u:Q,H:te,B:$}},x=S=>{let P=S.H.map(Q=>Q.map(te=>-te)),G=Wh(P,w);return{factor:P,rank:G}},E=S=>{let P=y(S,!0),G=x(P),Q=G.rank>=0?zh(G.factor,P.u):es(ry(P.B),P.u);return{e:P,delta:Q}},R=y(g,!0),A=R.ll,T=0,k=!1,_=!1,L;try{L=E(g)}catch{L={e:R,delta:Array(v).fill(0)}}let q=g.map((S,P)=>S+L.delta[P]);if(h===0){let S=x(R),P=Dh(S.factor),G=P.map(te=>te.slice()),Q=P.map((te,$)=>te[$]===0?NaN:g[$]/Math.sqrt(Math.max(0,te[$])));return{b:g.map((te,$)=>$<d?Le(te,-M[$]):te),ll:R.ll,iter:T,V:P.map((te,$)=>te.map((J,ee)=>Le(J,($<d?-M[$]:0)+(ee<d?-M[ee]:0)))),VScaled:G,xExponent:M,score:R.u.map((te,$)=>$<d?Le(te,M[$]):te),z:Q,warn:k}}for(let S=1;S<=h;S++){let P=y(q,!0);if(Number.isFinite(P.ll)&&Math.abs(1-A/P.ll)<=p){g.splice(0,g.length,...q),R=P,A=P.ll,T=S,_=!0;break}if(!Number.isFinite(P.ll)||P.ll<A){let G=q.slice();q=g.map((Q,te)=>Q+(G[te]-Q)/3);for(let Q=0;Q<i;Q++)m===void 0&&(q[d+Q]=Math.max(q[d+Q],g[d+Q]-1.1));T=S;continue}if(g.splice(0,g.length,...q),R=P,A=P.ll,T=S,S<h)try{let G=E(g);q=g.map((Q,te)=>Q+G.delta[te])}catch{q=g.slice()}else try{let G=E(g),Q=g.map(($,J)=>$+G.delta[J]),te=y(Q,!1);Number.isFinite(te.ll)&&te.ll>=A&&g.splice(0,g.length,...Q)}catch{}}T===h&&h>=2&&!_&&(k=!0);let F=y(g,!0),C=x(F),O=Dh(C.factor),I=O.map(S=>S.slice()),V=O.map((S,P)=>S[P]===0?NaN:g[P]/Math.sqrt(Math.max(0,S[P])));return{b:g.map((S,P)=>P<d?Le(S,-M[P]):S),ll:A,iter:T,V:O.map((S,P)=>S.map((G,Q)=>Le(G,(P<d?-M[P]:0)+(Q<d?-M[Q]:0)))),VScaled:I,xExponent:M,score:F.u.map((S,P)=>P<d?Le(S,M[P]):S),z:V,warn:k}}function oy(e,t,n=[],r={}){H(r,zv,"survreg"),H(vn(r,"control"),Bv,"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),w=r.offset??Array(i).fill(0),d=r.strata??Array(i).fill("1");for(let[Y,me]of[[p,"weights"],[w,"offset"],[d,"strata"]])if(Y.length!==i)throw new RangeError(`variable lengths differ (found for '${me}')`);let v=[],N=[],b=[],M=[],g=[],y=[],x=[],E=[],R=[],A=(o==="right"||o==="left")&&l.some(Y=>Number(Y)===2),T=Y=>{R.includes(Y)||R.push(Y)},k=[...new Set(d.map(String))].sort(),_=d.map(Y=>k.indexOf(String(Y))),L=[];if(u!==void 0&&k.length>1)throw new Error("The scale argument is not valid with multiple strata");for(let Y=0;Y<i;Y++){let me=n.length===0?[]:Array.from(n[Y]);if((o==="interval2"?Number.isNaN(e[Y])&&Number.isNaN(h[Y]):o==="interval"&&Gv(l[Y])===3?Number.isNaN(e[Y])||Number.isNaN(h[Y]):Number.isNaN(e[Y]))||o!=="interval2"&&Number.isNaN(l[Y])||Number.isNaN(p[Y])||Number.isNaN(w[Y])||Number.isNaN(d[Y])||me.some(Number.isNaN))continue;if(!(p[Y]>0))throw new RangeError("Invalid weights, must be >0");if(me.some(ve=>!Number.isFinite(ve)))throw new Error("data contains an infinite predictor");let ye=e[Y],Me=e[Y],X=Number(l[Y]);if(o==="right")if(A){if(X!==1&&X!==2){T("Invalid status value, converted to NA");continue}X===1&&(Me=1/0),X=X===2?1:0}else{if(X!==0&&X!==1){T("Invalid status value, converted to NA");continue}X===0&&(Me=1/0),X=X===1?1:0}else if(o==="left")if(A){if(X!==1&&X!==2){T("Invalid status value, converted to NA");continue}X===1&&(ye=-1/0),X=X===2?1:2}else{if(X!==0&&X!==1){T("Invalid status value, converted to NA");continue}X===0&&(ye=-1/0),X=X===1?1:2}else if(o==="interval"){if(Me=h[Y],X===0)ye=e[Y],Me=1/0;else if(X===1)Me=ye;else if(X===2)Me=e[Y],ye=-1/0;else if(X!==3){T("Invalid status value, converted to NA");continue}if(X===3&&ye>Me){T("Invalid interval: start > stop, NA created");continue}if(X===3&&ye===Me)throw new Error("initial iteration failed (use starting estimates?)")}else if(o==="interval2"){let ve=e[Y],ae=h[Y];if(Number.isNaN(ve)&&Number.isNaN(ae))continue;if(ve>ae){T("Invalid interval: start > stop, NA created");continue}ve===ae?(ye=ve,Me=ae,X=1):!Number.isFinite(ve)||ve===0?(ye=-1/0,Me=ae,X=2):Number.isFinite(ae)?(ye=ve,Me=ae,X=3):(ye=ve,Me=1/0,X=0)}if(f){if((Number.isFinite(ye)&&ye<0||Number.isFinite(Me)&&Me<0)&&T("NaNs produced"),ye===1/0||Me===-1/0||Number.isFinite(ye)&&ye<=0||Number.isFinite(Me)&&Me<=0)throw new Error("Invalid survival times for this distribution");Number.isFinite(ye)&&(ye=Math.log(ye)),Number.isFinite(Me)&&(Me=Math.log(Me))}v.push(Y),N.push(X===1?Me:Number.isFinite(ye)&&Number.isFinite(Me)?Xt(ye,Me):Number.isFinite(ye)?ye:Me),b.push(ye),M.push(Me),g.push(X),y.push(p[Y]),x.push(w[Y]),E.push(me),L.push(_[Y])}if(v.length===0)throw new Error("initial iteration failed (use starting estimates?)");if(!g.some(Y=>Y===1||Y===3))throw new Error("initial iteration failed (use starting estimates?)");if(N.some(Y=>!Number.isFinite(Y)))throw new Error("initial iteration failed (use starting estimates?)");if(E.some(Y=>Y.length!==E[0].length))throw new TypeError("all rows of the matrix must have the same length");let q=E[0].length,F=q+(c?1:0),C=E.map(Y=>c?[1,...Y]:Y),O=k.length,I=N.map((Y,me)=>Y-x[me]),V=y.reduce((Y,me)=>Y+me,0),S=I.reduce((Y,me,Ne)=>Y+me*y[Ne],0)/V,P=I.reduce((Y,me,Ne)=>Y+y[Ne]*(me-S)**2,0)/V,G=Math.max(...I)-Math.min(...I),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,te=u??2*Math.sqrt(Q);if(u===void 0&&(!(te>0)||!(G>0)))throw new Error("initial iteration failed (use starting estimates?)");let $=F+(u===void 0?O:0),J=Array($).fill(0);if(c&&(J[0]=S),u===void 0)for(let Y=0;Y<O;Y++)J[F+Y]=Math.log(te);let ee=r.control??{},Se=ee.maxiter??30,re=ee.relTolerance??1e-9;if(!Number.isInteger(Se)||Se<0||!Number.isFinite(re)||re<0)throw new RangeError("invalid control");if(ee.tolerChol!==void 0&&(!Number.isFinite(ee.tolerChol)||ee.tolerChol<0))throw new RangeError("invalid control");let j=C.map(()=>[1]),K=C.every(Y=>Y.length===1&&Y[0]===1);if(r.init!==void 0&&r.init.length!==F&&r.init.length!==$)throw new Error("Wrong length for initial parameters");let U=N.map((Y,me)=>xa(b[me],M[me],g[me],Y,u??te,s,a)).map((Y,me)=>{let Ne=-y[me]*Y.ee;return Ne>0&&Number.isFinite(Ne)?{w:Ne,r:N[me]-Y.e/Y.ee-x[me]}:{w:0,r:0}}),z=U.reduce((Y,me)=>Y+me.w,0);if(!(z>0))throw new Error("initial iteration failed (use starting estimates?)");let ne=U.reduce((Y,me)=>Y+me.w*me.r,0)/z,le=[r.init!==void 0&&K?r.init[0]:ne,...u===void 0?Array(O).fill(Math.log(te)):[]];if(K&&r.init!==void 0&&r.init.length===$)for(let Y=0;Y<O;Y++)le[1+Y]=r.init[1+Y];let Re=K?Se:Math.max(Se,1e3),Ce=Ph(j,N,b,M,g,y,x,L,O,s,a,u,le,Re,re,ee.tolerChol??1e-10),be=J.slice();if(u===void 0)for(let Y=0;Y<O;Y++)be[F+Y]=Ce.b[1+Y];if(!K&&r.init!==void 0){for(let Y=0;Y<F;Y++)be[Y]=r.init[Y];if(r.init.length===$)for(let Y=F;Y<$;Y++)be[Y]=r.init[Y]}else if(!K&&F>0){let Y=E.map((Me,X)=>{let ve=u??Math.exp(Ce.b[1+L[X]]),ae=xa(b[X],M[X],g[X],N[X],ve,s,a),Ae=-y[X]*ae.ee;return Ae>0&&Number.isFinite(Ae)?{w:Ae,r:N[X]-ae.e/ae.ee-x[X]}:{w:0,r:0}}),me=Bh(C,ee.tolerChol??1e-10),Ne=Array.from({length:F},(Me,X)=>Array.from({length:F},(ve,ae)=>me.values.reduce((Ae,Be,je)=>Ae+Y[je].w*Be[X]*Be[ae],0))),ye=Array.from({length:F},(Me,X)=>me.values.reduce((ve,ae,Ae)=>ve+Y[Ae].w*ae[X]*Y[Ae].r,0));try{let Me=Ne.map(ae=>ae.slice()),ve=Wh(Me,ee.tolerChol??1e-10)>=0?zh(Me,ye):es(ot(Ne),ye);be.splice(0,F,...ve.map((ae,Ae)=>Le(ae,-me.exponent[Ae])))}catch{}}let fe=K?Ce:Ph(C,N,b,M,g,y,x,L,O,s,a,u,be,Se,re,ee.tolerChol??1e-10),ke=f?v.reduce((Y,me,Ne)=>Y+(g[Ne]===1?y[Ne]*Math.log(e[me]):0),0)*-1:0,de=fe.V.map(Y=>Y.slice()),Oe=fe.b.slice(0,F).map((Y,me)=>Number.isNaN(fe.z[me])?NaN:Y),Ee=Oe.map((Y,me)=>(de[me]?.[me]??0)===0?0:Math.sqrt(Math.max(0,de[me]?.[me]??0))),Fe=Oe.map((Y,me)=>fe.z[me]).map(Y=>2*ce(-Math.abs(Y))),Ue=u!==void 0?[u]:Array.from({length:O},(Y,me)=>Math.exp(fe.b[F+me])),$e=Array.from({length:F},(Y,me)=>C.reduce((Ne,ye)=>Ne+ye[me],0)/C.length),Je=C.map((Y,me)=>or(Y,fe.b.slice(0,F))+x[me]),Ke=2*(fe.ll+ke-(Ce.ll+ke)),Xe=F+(u===void 0?O:0)-(1+(u===void 0?O:0)),he=[...Oe.map((Y,me)=>me===0&&c?"(Intercept)":`x${me+(c?0:1)}`),...u===void 0?O===1?["Log(scale)"]:k:[]],oe=[...Oe,...u===void 0?fe.b.slice(F):[]],we=oe.map((Y,me)=>(de[me]?.[me]??0)===0?0:Math.sqrt(Math.max(0,de[me]?.[me]??0))),ge=oe.map((Y,me)=>fe.z[me]),ie=ge.map(Y=>2*ce(-Math.abs(Y))),et={coefficients:Oe,coefNames:he.slice(0,F),scale:Ue,table:{value:oe,se:we,z:ge,p:ie},strataLevels:k.map(Y=>d.find(me=>String(me)===Y)),hasOffset:r.offset!==void 0,icoef:u===void 0||v.length===1?Ce.b.slice():[Ce.b[0],Math.log(u)],var:de,loglik:[Ce.ll+ke,fe.ll+ke],iter:fe.iter,df:F+(u===void 0?O:0),idf:1+(u===void 0?O:0),dfResidual:v.length-(F+(u===void 0?O:0)),n:v.length,linearPredictors:Je,means:$e,score:fe.score.slice(),tableRowNames:he,value:oe,se:we,z:ge,p:ie,chi:Ke,chiDf:Xe,chiP:Bt(Ke,Xe,{lowerTail:!1}),dist:s,distributionDf:a,parms:a,intercept:c,x:C,offset:x,rows:v,weights:y,strata:L,y:N,lower:b,upper:M,status:g,type:o,warnings:[...R,...fe.warn||Ce.warn?["Ran out of iterations and did not converge"]:[],...s==="exponential"&&r.scale!==void 0&&r.scale>0?["Exponential has a fixed scale, user specified value ignored"]:[]]};return Wc.set(et,{V:fe.VScaled,xExponent:fe.xExponent}),Z("survreg",et)}function sy(e,t={}){H(t,Uv,"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 w=t.newX===void 0&&t.newOffset===void 0?e.linearPredictors[p]:or(h,e.coefficients)+o[p],d=e.scale.length===1?e.scale[0]:e.scale[a[p]];return n==="lp"||n==="linear"||n==="link"?w:n==="response"?i?Math.exp(w):w:c.map(v=>{let N=w+d*Vh(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 w=t.newX===void 0&&t.newOffset===void 0?e.linearPredictors[p]:or(h,e.coefficients)+o[p],d=e.scale.length===1?e.scale[0]:e.scale[a[p]],v=Wc.get(e),N=v?.V??e.var,b=M=>{let g=Array(N.length).fill(0);for(let y=0;y<h.length;y++)g[y]=v?Le(h[y],-v.xExponent[y]):h[y];return N.length>h.length&&(g[h.length+a[p]]=M*d),Math.sqrt(Math.max(0,or(g,es(N,g))))};return n==="lp"||n==="linear"||n==="link"?b(0):n==="response"?i?Ch(w,b(0)):b(0):c.map(M=>{let g=Vh(M,e.dist,e.parms),y=b(g),x=w+d*g;return n==="uquantile"?y:i?Ch(x,y):y})}),u=(n==="quantile"||n==="uquantile")&&c.length===1?m.map(h=>h[0]):m;return{fit:f,seFit:u,warnings:[]}}function ay(e,t={}){H(t,Kv,"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=Wc.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=xa(e.lower[m],e.upper[m],e.status[m],u,h,e.dist,r),w=a?e.weights[m]:1,d=f.map((R,A)=>c?Le(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=es(i,v),b=N.map((R,A)=>c&&A<o?Le(R,-c.xExponent[A]):R),M=or(v,N);if(n==="matrix")return[p.g,p.e,p.ee,p.t,p.tt,p.et].map(R=>R*w);if(n==="working")return-p.e/p.ee*w;if(n==="dfbeta")return b.map(R=>R*w);if(n==="dfbetas")return N.map((R,A)=>R/Math.sqrt(i[A]?.[A]??NaN)*w);if(n==="ldcase")return M*w;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),or(R,es(i,R))*w}let g=Ea(0,e.dist,r)-Math.log(h),y=e.status[m]===3?Xt(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=Zv(e.lower[m],e.upper[m],h));let x=e.status[m]===0?1:e.status[m]===2?-1:Math.sign(y-u);if(n==="deviance"){let R=e.status[m]===1?g:0;if(e.status[m]===3){let A=Qv(e.lower[m],e.upper[m],h);e.dist==="extreme"||e.dist==="weibull"||e.dist==="exponential"||e.dist==="rayleigh"?R=Jv(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*ce(A/2)-1):e.dist==="t"&&(R=Math.log(2*yn(A/2,e.distributionDf)-1))}return x*Math.sqrt(Math.max(0,2*(R-p.g)))*w}let E=y-u;return(["weibull","exponential","rayleigh","loggaussian","lognormal","loglogistic"].includes(e.dist)?Xv(y,u):E)*w});return l}export{sc as Anova,pv as ICC,Gg as TukeyHSD,mb as adTest,gi as anova,xm as ansariTest,Kg as aov,k1 as aovEz,wi as aovSummary,Vb as auc,Xf as bartlettTest,si as binomCI,Ed as binomDiffCI,Od as binomDiffEquivalenceTest,Rd as binomRatioCI,Qp as binomTest,Xb as blandAltman,b0 as boschlooTest,xg as brunnerMunzelTest,up as chisqTest,zb as ciAuc,R0 as cochranQTest,jw as coefci,Vw as coeftest,ku as cohenES,Kd as cohensD,zg as confint,jb as coords,Kh as corTest,X1 as coxZph,U1 as coxph,Mb as cronbachAlpha,$v as crr,Fv as cuminc,ub as cvmTest,Hb as diagnosticTests,Y0 as dscfAllPairsTest,Z0 as dunnTest,H0 as dunnettTest,t0 as epsilonSquared,l1 as esH,f1 as esW1,m1 as esW2,Jd as etaSquared,bv as factanal,Ao as fisherTest,Jf as flignerTest,Ri as friedmanTest,sp as gTest,X0 as gamesHowellTest,eg as glhtDunnett,Ki as glm,Bw as glmAnova,bw as glmConfint,pw as glmCooksDistance,Po as glmHatvalues,gw as glmPredict,mw as glmResiduals,hw as glmRstandard,Hi as glmSummary,lp as goodmanKruskalGamma,Hd as hedgesG,Nb as icc,lb as jonckheereTest,vb as kappa2,yb as kappamFleiss,Ws as kruskalTest,Lm as ksTest,U0 as kwAllPairsDunnTest,K0 as kwManyOneDunnTest,mp as lblTest,J1 as leveneTest,hb as lillieTest,Oo as lm,ig as mantelhaenTest,G0 as manyOneUTest,Xw as mauchlyTest,N0 as mcnemarTest,cv as mcreg,Dr as modelMatrix,em as moodTest,qd as oddsRatio,Rv as omega,e0 as omegaSquared,um as onewayTest,jr as pAdjust,Cg as pairwisePropTest,_g as pairwiseTTest,Lg as pairwiseWilcoxTest,xb as partialR,db as pearsonTest,rd as poissonTest,qv as polychor,_v as polyserial,ep as powerAnovaTest,Zh as powerPropTest,Qh as powerTTest,Bg as predict,Wv as predictCrr,ri as propTest,nb as propTrendTest,N1 as pwr2p2nTest,b1 as pwr2pTest,x1 as pwrAnovaTest,E1 as pwrChisqTest,R1 as pwrF2Test,y1 as pwrNormTest,v1 as pwrPTest,M1 as pwrRTest,w1 as pwrT2nTest,g1 as pwrTTest,Zf as quadeTest,V1 as rmst2,Fb as roc,Ub as rocTest,Uh as setWarningHandler,pb as sfTest,ng as shapiroTest,cp as stuartMaxwellTest,L1 as survdiff,er as survfit,oy as survreg,sy as survregPredict,ay as survregResiduals,l0 as tTOST,gn as tTest,Vv as timepoints,Ld as tost,f0 as tsumTOST,Q0 as tukeyTest,Yf as varTest,Cw as vcovCL,Ew as vcovHC,To as wilcoxTest,Yg as zTest};